Add generated pcidevs files. Fix a small typo in devlist2h.awk.
[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.5 2003/08/07 21:17:33 dillon 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);
142             bzero(aa0, sizeof(struct at_ifaddr));
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 ));
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                 aa->aa_ch = timeout( aarpprobe, (caddr_t)ifp, hz / 5 );
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         /*
580          * of course this could mean that it objects violently
581          * so if it does, we back out again..
582          */
583         aa->aa_addr = oldaddr;
584         aa->aa_firstnet = onr.nr_firstnet;
585         aa->aa_lastnet = onr.nr_lastnet;
586         splx( s );
587         return( error );
588     }
589
590     /* 
591      * set up the netmask part of the at_ifaddr
592      * and point the appropriate pointer in the ifaddr to it.
593      * probably pointless, but what the heck.. XXX
594      */
595     bzero(&aa->aa_netmask, sizeof(aa->aa_netmask));
596     aa->aa_netmask.sat_len = sizeof(struct sockaddr_at);
597     aa->aa_netmask.sat_family = AF_APPLETALK;
598     aa->aa_netmask.sat_addr.s_net = 0xffff;
599     aa->aa_netmask.sat_addr.s_node = 0;
600     aa->aa_ifa.ifa_netmask =(struct sockaddr *) &(aa->aa_netmask); /* XXX */
601
602     /*
603      * Initialize broadcast (or remote p2p) address
604      */
605     bzero(&aa->aa_broadaddr, sizeof(aa->aa_broadaddr));
606     aa->aa_broadaddr.sat_len = sizeof(struct sockaddr_at);
607     aa->aa_broadaddr.sat_family = AF_APPLETALK;
608
609     aa->aa_ifa.ifa_metric = ifp->if_metric;
610     if (ifp->if_flags & IFF_BROADCAST) {
611         aa->aa_broadaddr.sat_addr.s_net = htons(0);
612         aa->aa_broadaddr.sat_addr.s_node = 0xff;
613         aa->aa_ifa.ifa_broadaddr = (struct sockaddr *) &aa->aa_broadaddr;
614         /* add the range of routes needed */
615         error = aa_dorangeroute(&aa->aa_ifa,
616                 ntohs(aa->aa_firstnet), ntohs(aa->aa_lastnet), RTM_ADD );
617     }
618     else if (ifp->if_flags & IFF_POINTOPOINT) {
619         struct at_addr  rtaddr, rtmask;
620
621         bzero(&rtaddr, sizeof(rtaddr));
622         bzero(&rtmask, sizeof(rtmask));
623         /* fill in the far end if we know it here XXX */
624         aa->aa_ifa.ifa_dstaddr = (struct sockaddr *) &aa->aa_dstaddr;
625         error = aa_addsingleroute(&aa->aa_ifa, &rtaddr, &rtmask);
626     }
627     else if ( ifp->if_flags & IFF_LOOPBACK ) {
628         struct at_addr  rtaddr, rtmask;
629
630         bzero(&rtaddr, sizeof(rtaddr));
631         bzero(&rtmask, sizeof(rtmask));
632         rtaddr.s_net = AA_SAT( aa )->sat_addr.s_net;
633         rtaddr.s_node = AA_SAT( aa )->sat_addr.s_node;
634         rtmask.s_net = 0xffff;
635         rtmask.s_node = 0x0; /* XXX should not be so.. should be HOST route */
636         error = aa_addsingleroute(&aa->aa_ifa, &rtaddr, &rtmask);
637     }
638
639
640     /*
641      * set the address of our "check if this addr is ours" routine.
642      */
643     aa->aa_ifa.ifa_claim_addr = aa_claim_addr;
644
645     /*
646      * of course if we can't add these routes we back out, but it's getting
647      * risky by now XXX
648      */
649     if ( error ) {
650         at_scrub( ifp, aa );
651         aa->aa_addr = oldaddr;
652         aa->aa_firstnet = onr.nr_firstnet;
653         aa->aa_lastnet = onr.nr_lastnet;
654         splx( s );
655         return( error );
656     }
657
658     /*
659      * note that the address has a route associated with it....
660      */
661     aa->aa_ifa.ifa_flags |= IFA_ROUTE;
662     aa->aa_flags |= AFA_ROUTE;
663     splx( s );
664     return( 0 );
665 }
666
667 /*
668  * check whether a given address is a broadcast address for us..
669  */
670 int
671 at_broadcast( sat )
672     struct sockaddr_at  *sat;
673 {
674     struct at_ifaddr    *aa;
675
676     /*
677      * If the node is not right, it can't be a broadcast 
678      */
679     if ( sat->sat_addr.s_node != ATADDR_BCAST ) {
680         return( 0 );
681     }
682
683     /*
684      * If the node was right then if the net is right, it's a broadcast
685      */
686     if ( sat->sat_addr.s_net == ATADDR_ANYNET ) {
687         return( 1 );
688     }
689
690     /*
691      * failing that, if the net is one we have, it's a broadcast as well.
692      */
693     for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
694         if (( aa->aa_ifp->if_flags & IFF_BROADCAST )
695          && ( ntohs( sat->sat_addr.s_net ) >= ntohs( aa->aa_firstnet )
696          && ntohs( sat->sat_addr.s_net ) <= ntohs( aa->aa_lastnet ))) {
697                         return( 1 );
698         }
699     }
700     return( 0 );
701 }
702
703 /*
704  * aa_dorangeroute()
705  *
706  * Add a route for a range of networks from bot to top - 1.
707  * Algorithm:
708  *
709  * Split the range into two subranges such that the middle
710  * of the two ranges is the point where the highest bit of difference
711  * between the two addresses makes its transition.
712  * Each of the upper and lower ranges might not exist, or might be 
713  * representable by 1 or more netmasks. In addition, if both
714  * ranges can be represented by the same netmask, then they can be merged
715  * by using the next higher netmask..
716  */
717
718 static int
719 aa_dorangeroute(struct ifaddr *ifa, u_int bot, u_int top, int cmd)
720 {
721         u_int mask1;
722         struct at_addr addr;
723         struct at_addr mask;
724         int error;
725
726         /*
727          * slight sanity check
728          */
729         if (bot > top) return (EINVAL);
730
731         addr.s_node = 0;
732         mask.s_node = 0;
733         /*
734          * just start out with the lowest boundary
735          * and keep extending the mask till it's too big.
736          */
737         
738          while (bot <= top) {
739                 mask1 = 1;
740                 while ((( bot & ~mask1) >= bot)
741                    && (( bot | mask1) <= top)) {
742                         mask1 <<= 1;
743                         mask1 |= 1;
744                 }
745                 mask1 >>= 1;
746                 mask.s_net = htons(~mask1);
747                 addr.s_net = htons(bot);
748                 if(cmd == RTM_ADD) {
749                 error =  aa_addsingleroute(ifa,&addr,&mask);
750                         if (error) {
751                                 /* XXX clean up? */
752                                 return (error);
753                         }
754                 } else {
755                         error =  aa_delsingleroute(ifa,&addr,&mask);
756                 }
757                 bot = (bot | mask1) + 1;
758         }
759         return 0;
760 }
761
762 static int
763 aa_addsingleroute(struct ifaddr *ifa,
764         struct at_addr *addr, struct at_addr *mask)
765 {
766   int   error;
767
768 #if 0
769   printf("aa_addsingleroute: %x.%x mask %x.%x ...\n",
770     ntohs(addr->s_net), addr->s_node,
771     ntohs(mask->s_net), mask->s_node);
772 #endif
773
774   error = aa_dosingleroute(ifa, addr, mask, RTM_ADD, RTF_UP);
775   if (error)
776     printf("aa_addsingleroute: error %d\n", error);
777   return(error);
778 }
779
780 static int
781 aa_delsingleroute(struct ifaddr *ifa,
782         struct at_addr *addr, struct at_addr *mask)
783 {
784   int   error;
785
786   error = aa_dosingleroute(ifa, addr, mask, RTM_DELETE, 0);
787   if (error)
788         printf("aa_delsingleroute: error %d\n", error);
789   return(error);
790 }
791
792 static int
793 aa_dosingleroute(struct ifaddr *ifa,
794         struct at_addr *at_addr, struct at_addr *at_mask, int cmd, int flags)
795 {
796   struct sockaddr_at    addr, mask;
797
798   bzero(&addr, sizeof(addr));
799   bzero(&mask, sizeof(mask));
800   addr.sat_family = AF_APPLETALK;
801   addr.sat_len = sizeof(struct sockaddr_at);
802   addr.sat_addr.s_net = at_addr->s_net;
803   addr.sat_addr.s_node = at_addr->s_node;
804   mask.sat_family = AF_APPLETALK;
805   mask.sat_len = sizeof(struct sockaddr_at);
806   mask.sat_addr.s_net = at_mask->s_net;
807   mask.sat_addr.s_node = at_mask->s_node;
808   if (at_mask->s_node)
809     flags |= RTF_HOST;
810   return(rtrequest(cmd, (struct sockaddr *) &addr,
811         (flags & RTF_HOST)?(ifa->ifa_dstaddr):(ifa->ifa_addr),
812         (struct sockaddr *) &mask, flags, NULL));
813 }
814
815 #if 0
816
817 static void
818 aa_clean(void)
819 {
820     struct at_ifaddr    *aa;
821     struct ifaddr       *ifa;
822     struct ifnet        *ifp;
823
824     while ( aa = at_ifaddr ) {
825         ifp = aa->aa_ifp;
826         at_scrub( ifp, aa );
827         at_ifaddr = aa->aa_next;
828         if (( ifa = ifp->if_addrlist ) == (struct ifaddr *)aa ) {
829             ifp->if_addrlist = ifa->ifa_next;
830         } else {
831             while ( ifa->ifa_next &&
832                     ( ifa->ifa_next != (struct ifaddr *)aa )) {
833                 ifa = ifa->ifa_next;
834             }
835             if ( ifa->ifa_next ) {
836                 ifa->ifa_next = ((struct ifaddr *)aa)->ifa_next;
837             } else {
838                 panic( "at_entry" );
839             }
840         }
841     }
842 }
843
844 #endif
845
846 static int
847 aa_claim_addr(struct ifaddr *ifa, struct sockaddr *gw0)
848 {
849         struct sockaddr_at *addr = (struct sockaddr_at *)ifa->ifa_addr;
850         struct sockaddr_at *gw = (struct sockaddr_at *)gw0;
851
852         switch (gw->sat_range.r_netrange.nr_phase) {
853         case 1:
854                 if(addr->sat_range.r_netrange.nr_phase == 1)
855                         return 1;
856         case 0:
857         case 2:
858                 /*
859                  * if it's our net (including 0),
860                  * or netranges are valid, and we are in the range,
861                  * then it's ours.
862                  */
863                 if ((addr->sat_addr.s_net == gw->sat_addr.s_net)
864                 || ((addr->sat_range.r_netrange.nr_lastnet)
865                   && (ntohs(gw->sat_addr.s_net)
866                         >= ntohs(addr->sat_range.r_netrange.nr_firstnet ))
867                   && (ntohs(gw->sat_addr.s_net)
868                         <= ntohs(addr->sat_range.r_netrange.nr_lastnet )))) {
869                         return 1;
870                 } 
871                 break;
872         default:
873                 printf("atalk: bad phase\n");
874         }
875         return 0;
876 }