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