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