]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netatalk/at_control.c
This commit was generated by cvs2svn to compensate for changes in r140801,
[FreeBSD/FreeBSD.git] / sys / netatalk / at_control.c
1 /*-
2  * Copyright (c) 1990,1991 Regents of The University of Michigan.
3  * All Rights Reserved.
4  *
5  * Permission to use, copy, modify, and distribute this software and
6  * its documentation for any purpose and without fee is hereby granted,
7  * provided that the above copyright notice appears in all copies and
8  * that both that copyright notice and this permission notice appear
9  * in supporting documentation, and that the name of The University
10  * of Michigan not be used in advertising or publicity pertaining to
11  * distribution of the software without specific, written prior
12  * permission. This software is supplied as is without expressed or
13  * implied warranties of any kind.
14  *
15  * This product includes software developed by the University of
16  * California, Berkeley and its contributors.
17  *
18  *      Research Systems Unix Group
19  *      The University of Michigan
20  *      c/o Wesley Craig
21  *      535 W. William Street
22  *      Ann Arbor, Michigan
23  *      +1-313-764-2278
24  *      netatalk@umich.edu
25  *
26  * $FreeBSD$
27  */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/sockio.h>
32 #include <sys/malloc.h>
33 #include <sys/kernel.h>
34 #include <sys/socket.h>
35 #include <net/if.h>
36 #include <net/route.h>
37 #include <netinet/in.h>
38 #undef s_net
39 #include <netinet/if_ether.h>
40
41 #include <netatalk/at.h>
42 #include <netatalk/at_var.h>
43 #include <netatalk/at_extern.h>
44
45 struct at_ifaddr *at_ifaddr_list;
46
47 static int aa_dorangeroute(struct ifaddr *ifa, u_int first, u_int last,
48             int cmd);
49 static int aa_addsingleroute(struct ifaddr *ifa, struct at_addr *addr,
50             struct at_addr *mask);
51 static int aa_delsingleroute(struct ifaddr *ifa, struct at_addr *addr,
52             struct at_addr *mask);
53 static int aa_dosingleroute(struct ifaddr *ifa, struct at_addr *addr,
54             struct at_addr *mask, int cmd, int flags);
55 static int at_scrub(struct ifnet *ifp, struct at_ifaddr *aa);
56 static int at_ifinit(struct ifnet *ifp, struct at_ifaddr *aa,
57             struct sockaddr_at *sat);
58 static int aa_claim_addr(struct ifaddr *ifa, struct sockaddr *gw);
59
60 #define sateqaddr(a,b)                                                  \
61         ((a)->sat_len == (b)->sat_len &&                                \
62         (a)->sat_family == (b)->sat_family &&                           \
63         (a)->sat_addr.s_net == (b)->sat_addr.s_net &&                   \
64         (a)->sat_addr.s_node == (b)->sat_addr.s_node)
65
66 int
67 at_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
68     struct thread *td)
69 {
70         struct ifreq *ifr = (struct ifreq *)data;
71         struct sockaddr_at *sat;
72         struct netrange *nr;
73         struct at_aliasreq *ifra = (struct at_aliasreq *)data;
74         struct at_ifaddr *aa0;
75         struct at_ifaddr *aa = NULL;
76         struct ifaddr *ifa, *ifa0;
77
78         /*
79          * If we have an ifp, then find the matching at_ifaddr if it exists
80          */
81         if (ifp != NULL) {
82                 for (aa = at_ifaddr_list; aa != NULL; aa = aa->aa_next) {
83                         if (aa->aa_ifp == ifp)
84                                 break;
85                 }
86         }
87
88         /*
89          * In this first switch table we are basically getting ready for
90          * the second one, by getting the atalk-specific things set up
91          * so that they start to look more similar to other protocols etc.
92          */
93
94         switch (cmd) {
95         case SIOCAIFADDR:
96         case SIOCDIFADDR:
97                 /*
98                  * If we have an appletalk sockaddr, scan forward of where we
99                  * are now on the at_ifaddr list to find one with a matching 
100                  * address on this interface.  This may leave aa pointing to
101                  * the first address on the NEXT interface!
102                  */
103                 if (ifra->ifra_addr.sat_family == AF_APPLETALK) {
104                         for (; aa; aa = aa->aa_next) {
105                                 if (aa->aa_ifp == ifp &&
106                                     sateqaddr(&aa->aa_addr, &ifra->ifra_addr))
107                                         break;
108                         }
109                 }
110                 /*
111                  * If we a retrying to delete an addres but didn't find such,
112                  * then rewurn with an error
113                  */
114                 if (cmd == SIOCDIFADDR && aa == NULL)
115                         return (EADDRNOTAVAIL);
116                 /*FALLTHROUGH*/
117
118         case SIOCSIFADDR:
119                 /* 
120                  * If we are not superuser, then we don't get to do these ops.
121                  */
122                 if (suser(td))
123                         return (EPERM);
124
125                 sat = satosat(&ifr->ifr_addr);
126                 nr = (struct netrange *)sat->sat_zero;
127                 if (nr->nr_phase == 1) {
128                         /*
129                          * Look for a phase 1 address on this interface.
130                          * This may leave aa pointing to the first address on
131                          * the NEXT interface!
132                          */
133                         for (; aa; aa = aa->aa_next) {
134                                 if (aa->aa_ifp == ifp &&
135                                     (aa->aa_flags & AFA_PHASE2) == 0)
136                                         break;
137                         }
138                 } else {                /* default to phase 2 */
139                         /*
140                          * Look for a phase 2 address on this interface.
141                          * This may leave aa pointing to the first address on
142                          * the NEXT interface!
143                          */
144                         for (; aa; aa = aa->aa_next) {
145                                 if (aa->aa_ifp == ifp && (aa->aa_flags &
146                                     AFA_PHASE2))
147                                         break;
148                         }
149                 }
150
151                 if (ifp == NULL)
152                         panic("at_control");
153
154                 /*
155                  * If we failed to find an existing at_ifaddr entry, then we 
156                  * allocate a fresh one. 
157                  */
158                 if (aa == NULL) {
159                         aa0 = malloc(sizeof(struct at_ifaddr), M_IFADDR,
160                             M_WAITOK | M_ZERO);
161                         if ((aa = at_ifaddr_list) != NULL) {
162                                 /*
163                                  * Don't let the loopback be first, since the
164                                  * first address is the machine's default
165                                  * address for binding.  If it is, stick
166                                  * ourself in front, otherwise go to the back
167                                  * of the list.
168                                  */
169                                 if (at_ifaddr_list->aa_ifp->if_flags &
170                                     IFF_LOOPBACK) {
171                                         aa = aa0;
172                                         aa->aa_next = at_ifaddr_list;
173                                         at_ifaddr_list = aa;
174                                 } else {
175                                         for (; aa->aa_next; aa = aa->aa_next)
176                                                 ;
177                                         aa->aa_next = aa0;
178                                 }
179                         } else
180                                 at_ifaddr_list = aa0;
181                         aa = aa0;
182
183                         /*
184                          * Find the end of the interface's addresses
185                          * and link our new one on the end 
186                          */
187                         ifa = (struct ifaddr *)aa;
188                         IFA_LOCK_INIT(ifa);
189                         ifa->ifa_refcnt = 1;
190                         TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
191
192                         /*
193                          * As the at_ifaddr contains the actual sockaddrs,
194                          * and the ifaddr itself, link them al together
195                          * correctly.
196                          */
197                         ifa->ifa_addr = (struct sockaddr *)&aa->aa_addr;
198                         ifa->ifa_dstaddr = (struct sockaddr *)&aa->aa_addr;
199                         ifa->ifa_netmask = (struct sockaddr *)&aa->aa_netmask;
200
201                         /*
202                          * Set/clear the phase 2 bit.
203                          */
204                         if (nr->nr_phase == 1)
205                                 aa->aa_flags &= ~AFA_PHASE2;
206                         else
207                                 aa->aa_flags |= AFA_PHASE2;
208
209                         /*
210                          * and link it all together
211                          */
212                         aa->aa_ifp = ifp;
213                 } else {
214                         /*
215                          * If we DID find one then we clobber any routes
216                          * dependent on it..
217                          */
218                         at_scrub(ifp, aa);
219                 }
220                 break;
221
222         case SIOCGIFADDR :
223                 sat = satosat(&ifr->ifr_addr);
224                 nr = (struct netrange *)sat->sat_zero;
225                 if (nr->nr_phase == 1) {
226                         /*
227                          * If the request is specifying phase 1, then
228                          * only look at a phase one address
229                          */
230                         for (; aa; aa = aa->aa_next) {
231                                 if (aa->aa_ifp == ifp &&
232                                     (aa->aa_flags & AFA_PHASE2) == 0)
233                                         break;
234                         }
235                 } else {
236                         /*
237                          * default to phase 2
238                          */
239                         for (; aa; aa = aa->aa_next) {
240                                 if (aa->aa_ifp == ifp && (aa->aa_flags &
241                                     AFA_PHASE2))
242                                         break;
243                         }
244                 }
245
246                 if (aa == NULL)
247                         return (EADDRNOTAVAIL);
248                 break;
249         }
250
251         /*
252          * By the time this switch is run we should be able to assume that
253          * the "aa" pointer is valid when needed.
254          */
255         switch (cmd) {
256         case SIOCGIFADDR:
257
258                 /*
259                  * copy the contents of the sockaddr blindly.
260                  */
261                 sat = (struct sockaddr_at *)&ifr->ifr_addr;
262                 *sat = aa->aa_addr;
263
264                 /* 
265                  * and do some cleanups
266                  */
267                 ((struct netrange *)&sat->sat_zero)->nr_phase
268                     = (aa->aa_flags & AFA_PHASE2) ? 2 : 1;
269                 ((struct netrange *)&sat->sat_zero)->nr_firstnet =
270                     aa->aa_firstnet;
271                 ((struct netrange *)&sat->sat_zero)->nr_lastnet =
272                     aa->aa_lastnet;
273                 break;
274
275         case SIOCSIFADDR:
276                 return (at_ifinit(ifp, aa,
277                     (struct sockaddr_at *)&ifr->ifr_addr));
278
279         case SIOCAIFADDR:
280                 if (sateqaddr(&ifra->ifra_addr, &aa->aa_addr))
281                         return (0);
282                 return (at_ifinit(ifp, aa,
283                     (struct sockaddr_at *)&ifr->ifr_addr));
284
285         case SIOCDIFADDR:
286                 /*
287                  * scrub all routes.. didn't we just DO this? XXX yes, del it
288                  */
289                 at_scrub(ifp, aa);
290
291                 /*
292                  * remove the ifaddr from the interface
293                  */
294                 ifa0 = (struct ifaddr *)aa;
295                 TAILQ_REMOVE(&ifp->if_addrhead, ifa0, ifa_link);
296
297                 /*
298                  * Now remove the at_ifaddr from the parallel structure
299                  * as well, or we'd be in deep trouble
300                  */
301                 aa0 = aa;
302                 if (aa0 == (aa = at_ifaddr_list)) {
303                         at_ifaddr_list = aa->aa_next;
304                 } else {
305                         while (aa->aa_next && (aa->aa_next != aa0))
306                                 aa = aa->aa_next;
307
308                         /*
309                          * if we found it, remove it, otherwise we screwed up.
310                          */
311                         if (aa->aa_next)
312                                 aa->aa_next = aa0->aa_next;
313                         else
314                                 panic("at_control");
315                 }
316
317                 /*
318                  * Now reclaim the reference.
319                  */
320                 IFAFREE(ifa0);
321                 break;
322
323         default:
324                 if (ifp == NULL || ifp->if_ioctl == NULL)
325                         return (EOPNOTSUPP);
326                 return ((*ifp->if_ioctl)(ifp, cmd, data));
327         }
328         return (0);
329 }
330
331 /* 
332  * Given an interface and an at_ifaddr (supposedly on that interface)
333  * remove  any routes that depend on this.
334  * Why ifp is needed I'm not sure,
335  * as aa->at_ifaddr.ifa_ifp should be the same.
336  */
337 static int
338 at_scrub(struct ifnet *ifp, struct at_ifaddr *aa)
339 {
340         int error;
341
342         if (aa->aa_flags & AFA_ROUTE) {
343                 if (ifp->if_flags & IFF_LOOPBACK) {
344                         if ((error = aa_delsingleroute(&aa->aa_ifa,
345                             &aa->aa_addr.sat_addr, &aa->aa_netmask.sat_addr))
346                             != 0)
347                                 return (error);
348                 } else if (ifp->if_flags & IFF_POINTOPOINT) {
349                         if ((error = rtinit(&aa->aa_ifa, RTM_DELETE,
350                             RTF_HOST)) != 0)
351                                 return (error);
352                 } else if (ifp->if_flags & IFF_BROADCAST) {
353                         error = aa_dorangeroute(&aa->aa_ifa,
354                             ntohs(aa->aa_firstnet), ntohs(aa->aa_lastnet),
355                             RTM_DELETE);
356                 }
357                 aa->aa_ifa.ifa_flags &= ~IFA_ROUTE;
358                 aa->aa_flags &= ~AFA_ROUTE;
359         }
360         return (0);
361 }
362
363 /*
364  * given an at_ifaddr,a sockaddr_at and an ifp,
365  * bang them all together at high speed and see what happens
366  */
367 static int 
368 at_ifinit(struct ifnet *ifp, struct at_ifaddr *aa, struct sockaddr_at *sat)
369 {
370         struct netrange nr, onr;
371         struct sockaddr_at oldaddr;
372         int error = 0, i, j;
373         int netinc, nodeinc, nnets;
374         u_short net;
375
376         /* 
377          * save the old addresses in the at_ifaddr just in case we need them.
378          */
379         oldaddr = aa->aa_addr;
380         onr.nr_firstnet = aa->aa_firstnet;
381         onr.nr_lastnet = aa->aa_lastnet;
382
383         /*
384          * take the address supplied as an argument, and add it to the 
385          * at_ifnet (also given). Remember ing to update
386          * those parts of the at_ifaddr that need special processing
387          */
388         bzero(AA_SAT(aa), sizeof(struct sockaddr_at));
389         bcopy(sat->sat_zero, &nr, sizeof(struct netrange));
390         bcopy(sat->sat_zero, AA_SAT(aa)->sat_zero, sizeof(struct netrange));
391         nnets = ntohs(nr.nr_lastnet) - ntohs(nr.nr_firstnet) + 1;
392         aa->aa_firstnet = nr.nr_firstnet;
393         aa->aa_lastnet = nr.nr_lastnet;
394
395 /* XXX ALC */
396 #if 0
397         printf("at_ifinit: %s: %u.%u range %u-%u phase %d\n",
398             ifp->if_name,
399             ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
400             ntohs(aa->aa_firstnet), ntohs(aa->aa_lastnet),
401             (aa->aa_flags & AFA_PHASE2) ? 2 : 1);
402 #endif
403
404         /*
405          * We could eliminate the need for a second phase 1 probe (post
406          * autoconf) if we check whether we're resetting the node. Note
407          * that phase 1 probes use only nodes, not net.node pairs.  Under
408          * phase 2, both the net and node must be the same.
409          */
410         if (ifp->if_flags & IFF_LOOPBACK) {
411                 AA_SAT(aa)->sat_len = sat->sat_len;
412                 AA_SAT(aa)->sat_family = AF_APPLETALK;
413                 AA_SAT(aa)->sat_addr.s_net = sat->sat_addr.s_net;
414                 AA_SAT(aa)->sat_addr.s_node = sat->sat_addr.s_node;
415 #if 0
416         } else if (fp->if_flags & IFF_POINTOPOINT) {
417                 /* unimplemented */
418                 /*
419                  * we'd have to copy the dstaddr field over from the sat 
420                  * but it's not clear that it would contain the right info..
421                  */
422 #endif
423         } else {
424                 /*
425                  * We are a normal (probably ethernet) interface.
426                  * apply the new address to the interface structures etc.
427                  * We will probe this address on the net first, before
428                  * applying it to ensure that it is free.. If it is not, then
429                  * we will try a number of other randomly generated addresses
430                  * in this net and then increment the net.  etc.etc. until
431                  * we find an unused address.
432                  */
433                 aa->aa_flags |= AFA_PROBING; /* not loopback we Must probe? */
434                 AA_SAT(aa)->sat_len = sizeof(struct sockaddr_at);
435                 AA_SAT(aa)->sat_family = AF_APPLETALK;
436                 if (aa->aa_flags & AFA_PHASE2) {
437                         if (sat->sat_addr.s_net == ATADDR_ANYNET) {
438                                 /*
439                                  * If we are phase 2, and the net was not
440                                  * specified then we select a random net
441                                  * within the supplied netrange.
442                                  * XXX use /dev/random?
443                                  */
444                                 if (nnets != 1)
445                                         net = ntohs(nr.nr_firstnet) +
446                                             time_second % (nnets - 1);
447                                 else
448                                         net = ntohs(nr.nr_firstnet);
449                         } else {
450                                 /*
451                                  * if a net was supplied, then check that it
452                                  * is within the netrange. If it is not then
453                                  * replace the old values and return an error
454                                  */
455                                 if (ntohs(sat->sat_addr.s_net) <
456                                     ntohs(nr.nr_firstnet) ||
457                                     ntohs(sat->sat_addr.s_net) >
458                                     ntohs(nr.nr_lastnet)) {
459                                         aa->aa_addr = oldaddr;
460                                         aa->aa_firstnet = onr.nr_firstnet;
461                                         aa->aa_lastnet = onr.nr_lastnet;
462                                         return (EINVAL);
463                                 }
464                                 /*
465                                  * otherwise just use the new net number..
466                                  */
467                                 net = ntohs(sat->sat_addr.s_net);
468                         }
469                 } else {
470                         /*
471                          * we must be phase one, so just use whatever we were
472                          * given.  I guess it really isn't going to be
473                          * used... RIGHT?
474                          */
475                         net = ntohs(sat->sat_addr.s_net);
476                 }
477
478                 /* 
479                  * set the node part of the address into the ifaddr.
480                  * If it's not specified, be random about it...
481                  * XXX use /dev/random?
482                  */
483                 if (sat->sat_addr.s_node == ATADDR_ANYNODE)
484                         AA_SAT(aa)->sat_addr.s_node = time_second;
485                 else
486                         AA_SAT(aa)->sat_addr.s_node = sat->sat_addr.s_node;
487
488                 /* 
489                  * Copy the phase.
490                  */
491                 AA_SAT(aa)->sat_range.r_netrange.nr_phase =
492                     ((aa->aa_flags & AFA_PHASE2) ? 2:1);
493
494                 /* 
495                  * step through the nets in the range
496                  * starting at the (possibly random) start point.
497                  */
498                 for (i = nnets, netinc = 1; i > 0; net =
499                     ntohs(nr.nr_firstnet) + ((net - ntohs(nr.nr_firstnet) +
500                     netinc) % nnets), i--) {
501                         AA_SAT(aa)->sat_addr.s_net = htons(net);
502
503                         /*
504                          * using a rather strange stepping method,
505                          * stagger through the possible node addresses
506                          * Once again, starting at the (possibly random)
507                          * initial node address.
508                          */
509                         for (j = 0, nodeinc = time_second | 1; j < 256;
510                             j++, AA_SAT(aa)->sat_addr.s_node += nodeinc) {
511                                 if (AA_SAT(aa)->sat_addr.s_node > 253 ||
512                                     AA_SAT(aa)->sat_addr.s_node < 1)
513                                         continue;
514                                 aa->aa_probcnt = 10;
515         
516                                 /*
517                                  * start off the probes as an asynchronous
518                                  * activity.  though why wait 200mSec?
519                                  */
520                                 aa->aa_ch = timeout(aarpprobe, (caddr_t)ifp,
521                                     hz / 5);
522                                 if (tsleep(aa, PPAUSE|PCATCH, "at_ifinit",
523                                     0)) {
524                                         /*
525                                          * theoretically we shouldn't time
526                                          * out here so if we returned with an
527                                          * error..
528                                          */
529                                         printf("at_ifinit: why did this "
530                                             "happen?!\n");
531                                         aa->aa_addr = oldaddr;
532                                         aa->aa_firstnet = onr.nr_firstnet;
533                                         aa->aa_lastnet = onr.nr_lastnet;
534                                         return (EINTR);
535                                 }
536
537                                 /* 
538                                  * The async activity should have woken us
539                                  * up.  We need to see if it was successful
540                                  * in finding a free spot, or if we need to
541                                  * iterate to the next address to try.
542                                  */
543                                 if ((aa->aa_flags & AFA_PROBING) == 0)
544                                         break;
545                         }
546
547                         /*
548                          * of course we need to break out through two loops...
549                          */
550                         if ((aa->aa_flags & AFA_PROBING) == 0)
551                                 break;
552                         /* reset node for next network */
553                         AA_SAT(aa)->sat_addr.s_node = time_second;
554                 }
555
556                 /*
557                  * if we are still trying to probe, then we have finished all
558                  * the possible addresses, so we need to give up
559                  */
560                 if (aa->aa_flags & AFA_PROBING) {
561                         aa->aa_addr = oldaddr;
562                         aa->aa_firstnet = onr.nr_firstnet;
563                         aa->aa_lastnet = onr.nr_lastnet;
564                         return (EADDRINUSE);
565                 }
566         }
567
568         /* 
569          * Now that we have selected an address, we need to tell the interface
570          * about it, just in case it needs to adjust something.
571          */
572         if (ifp->if_ioctl != NULL &&
573             (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)aa))) {
574                 /*
575                  * of course this could mean that it objects violently
576                  * so if it does, we back out again..
577                  */
578                 aa->aa_addr = oldaddr;
579                 aa->aa_firstnet = onr.nr_firstnet;
580                 aa->aa_lastnet = onr.nr_lastnet;
581                 return (error);
582         }
583
584         /* 
585          * set up the netmask part of the at_ifaddr
586          * and point the appropriate pointer in the ifaddr to it.
587          * probably pointless, but what the heck.. XXX
588          */
589         bzero(&aa->aa_netmask, sizeof(aa->aa_netmask));
590         aa->aa_netmask.sat_len = sizeof(struct sockaddr_at);
591         aa->aa_netmask.sat_family = AF_APPLETALK;
592         aa->aa_netmask.sat_addr.s_net = 0xffff;
593         aa->aa_netmask.sat_addr.s_node = 0;
594         aa->aa_ifa.ifa_netmask =(struct sockaddr *) &(aa->aa_netmask); /* XXX */
595
596         /*
597          * Initialize broadcast (or remote p2p) address
598          */
599         bzero(&aa->aa_broadaddr, sizeof(aa->aa_broadaddr));
600         aa->aa_broadaddr.sat_len = sizeof(struct sockaddr_at);
601         aa->aa_broadaddr.sat_family = AF_APPLETALK;
602
603         aa->aa_ifa.ifa_metric = ifp->if_metric;
604         if (ifp->if_flags & IFF_BROADCAST) {
605                 aa->aa_broadaddr.sat_addr.s_net = htons(0);
606                 aa->aa_broadaddr.sat_addr.s_node = 0xff;
607                 aa->aa_ifa.ifa_broadaddr = (struct sockaddr *)
608                     &aa->aa_broadaddr;
609                 /* add the range of routes needed */
610                 error = aa_dorangeroute(&aa->aa_ifa, ntohs(aa->aa_firstnet),
611                     ntohs(aa->aa_lastnet), RTM_ADD);
612         } else if (ifp->if_flags & IFF_POINTOPOINT) {
613                 struct at_addr  rtaddr, rtmask;
614
615                 bzero(&rtaddr, sizeof(rtaddr));
616                 bzero(&rtmask, sizeof(rtmask));
617                 /* fill in the far end if we know it here XXX */
618                 aa->aa_ifa.ifa_dstaddr = (struct sockaddr *) &aa->aa_dstaddr;
619                 error = aa_addsingleroute(&aa->aa_ifa, &rtaddr, &rtmask);
620         } else if (ifp->if_flags & IFF_LOOPBACK) {
621                 struct at_addr  rtaddr, rtmask;
622
623                 bzero(&rtaddr, sizeof(rtaddr));
624                 bzero(&rtmask, sizeof(rtmask));
625                 rtaddr.s_net = AA_SAT(aa)->sat_addr.s_net;
626                 rtaddr.s_node = AA_SAT(aa)->sat_addr.s_node;
627                 rtmask.s_net = 0xffff;
628                  /* XXX should not be so.. should be HOST route */
629                 rtmask.s_node = 0x0;
630                 error = aa_addsingleroute(&aa->aa_ifa, &rtaddr, &rtmask);
631         }
632
633         /*
634          * set the address of our "check if this addr is ours" routine.
635          */
636         aa->aa_ifa.ifa_claim_addr = aa_claim_addr;
637
638         /*
639          * of course if we can't add these routes we back out, but it's
640          * getting risky by now XXX
641          */
642         if (error) {
643                 at_scrub(ifp, aa);
644                 aa->aa_addr = oldaddr;
645                 aa->aa_firstnet = onr.nr_firstnet;
646                 aa->aa_lastnet = onr.nr_lastnet;
647                 return (error);
648         }
649
650         /*
651          * note that the address has a route associated with it....
652          */
653         aa->aa_ifa.ifa_flags |= IFA_ROUTE;
654         aa->aa_flags |= AFA_ROUTE;
655         return (0);
656 }
657
658 /*
659  * check whether a given address is a broadcast address for us..
660  */
661 int
662 at_broadcast(struct sockaddr_at *sat)
663 {
664         struct at_ifaddr *aa;
665
666         /*
667          * If the node is not right, it can't be a broadcast 
668          */
669         if (sat->sat_addr.s_node != ATADDR_BCAST)
670                 return (0);
671
672         /*
673          * If the node was right then if the net is right, it's a broadcast
674          */
675         if (sat->sat_addr.s_net == ATADDR_ANYNET)
676                 return (1);
677
678         /*
679          * failing that, if the net is one we have, it's a broadcast as well.
680          */
681         for (aa = at_ifaddr_list; aa != NULL; aa = aa->aa_next) {
682                 if ((aa->aa_ifp->if_flags & IFF_BROADCAST)
683                     && (ntohs(sat->sat_addr.s_net) >= ntohs(aa->aa_firstnet)
684                     && ntohs(sat->sat_addr.s_net) <= ntohs(aa->aa_lastnet)))
685                         return (1);
686         }
687         return (0);
688 }
689
690 /*
691  * aa_dorangeroute()
692  *
693  * Add a route for a range of networks from bot to top - 1.
694  * Algorithm:
695  *
696  * Split the range into two subranges such that the middle
697  * of the two ranges is the point where the highest bit of difference
698  * between the two addresses makes its transition.
699  * Each of the upper and lower ranges might not exist, or might be 
700  * representable by 1 or more netmasks. In addition, if both
701  * ranges can be represented by the same netmask, then they can be merged
702  * by using the next higher netmask..
703  */
704
705 static int
706 aa_dorangeroute(struct ifaddr *ifa, u_int bot, u_int top, int cmd)
707 {
708         u_int mask1;
709         struct at_addr addr;
710         struct at_addr mask;
711         int error;
712
713         /*
714          * slight sanity check
715          */
716         if (bot > top) return (EINVAL);
717
718         addr.s_node = 0;
719         mask.s_node = 0;
720         /*
721          * just start out with the lowest boundary
722          * and keep extending the mask till it's too big.
723          */
724         
725          while (bot <= top) {
726                 mask1 = 1;
727                 while (((bot & ~mask1) >= bot) && ((bot | mask1) <= top)) {
728                         mask1 <<= 1;
729                         mask1 |= 1;
730                 }
731                 mask1 >>= 1;
732                 mask.s_net = htons(~mask1);
733                 addr.s_net = htons(bot);
734                 if (cmd == RTM_ADD) {
735                         error =  aa_addsingleroute(ifa,&addr,&mask);
736                         if (error) {
737                                 /* XXX clean up? */
738                                 return (error);
739                         }
740                 } else
741                         error =  aa_delsingleroute(ifa,&addr,&mask);
742                 bot = (bot | mask1) + 1;
743         }
744         return (0);
745 }
746
747 static int
748 aa_addsingleroute(struct ifaddr *ifa, struct at_addr *addr,
749     struct at_addr *mask)
750 {
751         int error;
752
753 #if 0
754         printf("aa_addsingleroute: %x.%x mask %x.%x ...\n",
755             ntohs(addr->s_net), addr->s_node, ntohs(mask->s_net),
756             mask->s_node);
757 #endif
758
759         error = aa_dosingleroute(ifa, addr, mask, RTM_ADD, RTF_UP);
760         if (error)
761                 printf("aa_addsingleroute: error %d\n", error);
762         return (error);
763 }
764
765 static int
766 aa_delsingleroute(struct ifaddr *ifa, struct at_addr *addr,
767     struct at_addr *mask)
768 {
769         int error;
770
771         error = aa_dosingleroute(ifa, addr, mask, RTM_DELETE, 0);
772         if (error)
773                 printf("aa_delsingleroute: error %d\n", error);
774         return (error);
775 }
776
777 static int
778 aa_dosingleroute(struct ifaddr *ifa, struct at_addr *at_addr,
779     struct at_addr *at_mask, int cmd, int flags)
780 {
781         struct sockaddr_at addr, mask;
782
783         bzero(&addr, sizeof(addr));
784         bzero(&mask, sizeof(mask));
785         addr.sat_family = AF_APPLETALK;
786         addr.sat_len = sizeof(struct sockaddr_at);
787         addr.sat_addr.s_net = at_addr->s_net;
788         addr.sat_addr.s_node = at_addr->s_node;
789         mask.sat_family = AF_APPLETALK;
790         mask.sat_len = sizeof(struct sockaddr_at);
791         mask.sat_addr.s_net = at_mask->s_net;
792         mask.sat_addr.s_node = at_mask->s_node;
793         if (at_mask->s_node)
794                 flags |= RTF_HOST;
795         return (rtrequest(cmd, (struct sockaddr *) &addr,
796             (flags & RTF_HOST)?(ifa->ifa_dstaddr):(ifa->ifa_addr),
797             (struct sockaddr *) &mask, flags, NULL));
798 }
799
800 #if 0
801
802 static void
803 aa_clean(void)
804 {
805         struct at_ifaddr *aa;
806         struct ifaddr *ifa;
807         struct ifnet *ifp;
808
809         while ((aa = at_ifaddr_list) != NULL) {
810                 ifp = aa->aa_ifp;
811                 at_scrub(ifp, aa);
812                 at_ifaddr_list = aa->aa_next;
813                 if ((ifa = ifp->if_addrlist) == (struct ifaddr *)aa)
814                         ifp->if_addrlist = ifa->ifa_next;
815                 else {
816                         while (ifa->ifa_next &&
817                             (ifa->ifa_next != (struct ifaddr *)aa))
818                                 ifa = ifa->ifa_next;
819                         if (ifa->ifa_next)
820                                 ifa->ifa_next =
821                                     ((struct ifaddr *)aa)->ifa_next;
822                         else
823                                 panic("at_entry");
824                 }
825         }
826 }
827
828 #endif
829
830 static int
831 aa_claim_addr(struct ifaddr *ifa, struct sockaddr *gw0)
832 {
833         struct sockaddr_at *addr = (struct sockaddr_at *)ifa->ifa_addr;
834         struct sockaddr_at *gw = (struct sockaddr_at *)gw0;
835
836         switch (gw->sat_range.r_netrange.nr_phase) {
837         case 1:
838                 if(addr->sat_range.r_netrange.nr_phase == 1)
839                         return (1);
840
841         case 0:
842         case 2:
843                 /*
844                  * if it's our net (including 0),
845                  * or netranges are valid, and we are in the range,
846                  * then it's ours.
847                  */
848                 if ((addr->sat_addr.s_net == gw->sat_addr.s_net)
849                     || ((addr->sat_range.r_netrange.nr_lastnet)
850                     && (ntohs(gw->sat_addr.s_net) >=
851                     ntohs(addr->sat_range.r_netrange.nr_firstnet))
852                     && (ntohs(gw->sat_addr.s_net) <=
853                     ntohs(addr->sat_range.r_netrange.nr_lastnet))))
854                         return (1);
855                 break;
856         default:
857                 printf("atalk: bad phase\n");
858         }
859         return (0);
860 }