we do not want to shift by the block size, which is much larger than
[dragonfly.git] / sys / netproto / atalk / at_control.c
blobabd0fd594f7bb43e6d033189785f20c89b894200
1 /*
2 * Copyright (c) 1990,1991 Regents of The University of Michigan.
3 * All Rights Reserved.
5 * $DragonFly: src/sys/netproto/atalk/at_control.c,v 1.12 2006/12/22 23:57:53 swildner Exp $
6 */
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 <sys/thread2.h>
16 #include <net/if.h>
17 #include <net/route.h>
18 #include <netinet/in.h>
19 #undef s_net
20 #include <netinet/if_ether.h>
22 #include "at.h"
23 #include "at_var.h"
24 #include "at_extern.h"
26 struct at_ifaddr *at_ifaddr;
28 static int aa_dorangeroute(struct ifaddr *ifa,
29 u_int first, u_int last, int cmd);
30 static int aa_addsingleroute(struct ifaddr *ifa,
31 struct at_addr *addr, struct at_addr *mask);
32 static int aa_delsingleroute(struct ifaddr *ifa,
33 struct at_addr *addr, struct at_addr *mask);
34 static int aa_dosingleroute(struct ifaddr *ifa, struct at_addr *addr,
35 struct at_addr *mask, int cmd, int flags);
36 static int at_scrub( struct ifnet *ifp, struct at_ifaddr *aa );
37 static int at_ifinit( struct ifnet *ifp, struct at_ifaddr *aa,
38 struct sockaddr_at *sat );
39 static int aa_claim_addr(struct ifaddr *ifa, struct sockaddr *gw);
41 # define sateqaddr(a,b) ((a)->sat_len == (b)->sat_len && \
42 (a)->sat_family == (b)->sat_family && \
43 (a)->sat_addr.s_net == (b)->sat_addr.s_net && \
44 (a)->sat_addr.s_node == (b)->sat_addr.s_node )
46 int
47 at_control(struct socket *so, u_long cmd, caddr_t data,
48 struct ifnet *ifp, struct thread *td )
50 struct ifreq *ifr = (struct ifreq *)data;
51 struct sockaddr_at *sat;
52 struct netrange *nr;
53 struct at_aliasreq *ifra = (struct at_aliasreq *)data;
54 struct at_ifaddr *aa0;
55 struct at_ifaddr *aa = 0;
56 struct ifaddr *ifa, *ifa0;
57 int error;
60 * If we have an ifp, then find the matching at_ifaddr if it exists
62 if ( ifp ) {
63 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
64 if ( aa->aa_ifp == ifp ) break;
69 * In this first switch table we are basically getting ready for
70 * the second one, by getting the atalk-specific things set up
71 * so that they start to look more similar to other protocols etc.
74 switch ( cmd ) {
75 case SIOCAIFADDR:
76 case SIOCDIFADDR:
78 * If we have an appletalk sockaddr, scan forward of where
79 * we are now on the at_ifaddr list to find one with a matching
80 * address on this interface.
81 * This may leave aa pointing to the first address on the
82 * NEXT interface!
84 if ( ifra->ifra_addr.sat_family == AF_APPLETALK ) {
85 for ( ; aa; aa = aa->aa_next ) {
86 if ( aa->aa_ifp == ifp &&
87 sateqaddr( &aa->aa_addr, &ifra->ifra_addr )) {
88 break;
93 * If we a retrying to delete an addres but didn't find such,
94 * then rewurn with an error
96 if ( cmd == SIOCDIFADDR && aa == 0 ) {
97 return( EADDRNOTAVAIL );
99 /*FALLTHROUGH*/
101 case SIOCSIFADDR:
103 * If we are not superuser, then we don't get to do these ops.
105 if (suser(td))
106 return(EPERM);
108 sat = satosat( &ifr->ifr_addr );
109 nr = (struct netrange *)sat->sat_zero;
110 if ( nr->nr_phase == 1 ) {
112 * Look for a phase 1 address on this interface.
113 * This may leave aa pointing to the first address on the
114 * NEXT interface!
116 for ( ; aa; aa = aa->aa_next ) {
117 if ( aa->aa_ifp == ifp &&
118 ( aa->aa_flags & AFA_PHASE2 ) == 0 ) {
119 break;
122 } else { /* default to phase 2 */
124 * Look for a phase 2 address on this interface.
125 * This may leave aa pointing to the first address on the
126 * NEXT interface!
128 for ( ; aa; aa = aa->aa_next ) {
129 if ( aa->aa_ifp == ifp && ( aa->aa_flags & AFA_PHASE2 )) {
130 break;
135 if ( ifp == 0 )
136 panic( "at_control" );
139 * If we failed to find an existing at_ifaddr entry, then we
140 * allocate a fresh one.
142 if ( aa == (struct at_ifaddr *) 0 ) {
143 aa0 = kmalloc(sizeof(struct at_ifaddr), M_IFADDR, M_WAITOK | M_ZERO);
144 callout_init(&aa0->aa_ch);
145 if (( aa = at_ifaddr ) != NULL ) {
147 * Don't let the loopback be first, since the first
148 * address is the machine's default address for
149 * binding.
150 * If it is, stick ourself in front, otherwise
151 * go to the back of the list.
153 if ( at_ifaddr->aa_ifp->if_flags & IFF_LOOPBACK ) {
154 aa = aa0;
155 aa->aa_next = at_ifaddr;
156 at_ifaddr = aa;
157 } else {
158 for ( ; aa->aa_next; aa = aa->aa_next )
160 aa->aa_next = aa0;
162 } else {
163 at_ifaddr = aa0;
166 * Don't Add a reference for the aa itself!
167 * I fell into this trap. IFAFREE tests for <=0
168 * not <= 1 like RTFREE
170 /* aa->aa_ifa.ifa_refcnt++; DON'T DO THIS!! */
171 aa = aa0;
174 * Find the end of the interface's addresses
175 * and link our new one on the end
177 ifa = (struct ifaddr *)aa;
178 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
181 * Add a reference for the linking into the ifp_if_addrlist.
183 ifa->ifa_refcnt++;
186 * As the at_ifaddr contains the actual sockaddrs,
187 * and the ifaddr itself, link them al together correctly.
189 ifa->ifa_addr = (struct sockaddr *)&aa->aa_addr;
190 ifa->ifa_dstaddr = (struct sockaddr *)&aa->aa_addr;
191 ifa->ifa_netmask = (struct sockaddr *)&aa->aa_netmask;
194 * Set/clear the phase 2 bit.
196 if ( nr->nr_phase == 1 ) {
197 aa->aa_flags &= ~AFA_PHASE2;
198 } else {
199 aa->aa_flags |= AFA_PHASE2;
203 * and link it all together
205 aa->aa_ifp = ifp;
206 } else {
208 * If we DID find one then we clobber any routes dependent on it..
210 at_scrub( ifp, aa );
212 break;
214 case SIOCGIFADDR :
215 sat = satosat( &ifr->ifr_addr );
216 nr = (struct netrange *)sat->sat_zero;
217 if ( nr->nr_phase == 1 ) {
219 * If the request is specifying phase 1, then
220 * only look at a phase one address
222 for ( ; aa; aa = aa->aa_next ) {
223 if ( aa->aa_ifp == ifp &&
224 ( aa->aa_flags & AFA_PHASE2 ) == 0 ) {
225 break;
228 } else {
230 * default to phase 2
232 for ( ; aa; aa = aa->aa_next ) {
233 if ( aa->aa_ifp == ifp && ( aa->aa_flags & AFA_PHASE2 )) {
234 break;
239 if ( aa == (struct at_ifaddr *) 0 )
240 return( EADDRNOTAVAIL );
241 break;
245 * By the time this switch is run we should be able to assume that
246 * the "aa" pointer is valid when needed.
248 switch ( cmd ) {
249 case SIOCGIFADDR:
252 * copy the contents of the sockaddr blindly.
254 sat = (struct sockaddr_at *)&ifr->ifr_addr;
255 *sat = aa->aa_addr;
258 * and do some cleanups
260 ((struct netrange *)&sat->sat_zero)->nr_phase
261 = (aa->aa_flags & AFA_PHASE2) ? 2 : 1;
262 ((struct netrange *)&sat->sat_zero)->nr_firstnet = aa->aa_firstnet;
263 ((struct netrange *)&sat->sat_zero)->nr_lastnet = aa->aa_lastnet;
264 break;
266 case SIOCSIFADDR:
267 return( at_ifinit( ifp, aa, (struct sockaddr_at *)&ifr->ifr_addr ));
269 case SIOCAIFADDR:
270 if ( sateqaddr( &ifra->ifra_addr, &aa->aa_addr )) {
271 return( 0 );
273 return( at_ifinit( ifp, aa, (struct sockaddr_at *)&ifr->ifr_addr ));
275 case SIOCDIFADDR:
277 * scrub all routes.. didn't we just DO this? XXX yes, del it
279 at_scrub( ifp, aa );
282 * remove the ifaddr from the interface
284 ifa0 = (struct ifaddr *)aa;
285 TAILQ_REMOVE(&ifp->if_addrhead, ifa0, ifa_link);
288 * refs goes from 1->0 if no external refs. note..
289 * This will not free it ... looks for -1.
291 IFAFREE(ifa0);
294 * Now remove the at_ifaddr from the parallel structure
295 * as well, or we'd be in deep trouble
297 aa0 = aa;
298 if ( aa0 == ( aa = at_ifaddr )) {
299 at_ifaddr = aa->aa_next;
300 } else {
301 while ( aa->aa_next && ( aa->aa_next != aa0 )) {
302 aa = aa->aa_next;
306 * if we found it, remove it, otherwise we screwed up.
308 if ( aa->aa_next ) {
309 aa->aa_next = aa0->aa_next;
310 } else {
311 panic( "at_control" );
316 * Now dump the memory we were using.
317 * Decrement the reference count.
318 * This should probably be the last reference
319 * as the count will go from 0 to -1.
320 * (unless there is still a route referencing this)
322 IFAFREE(ifa0);
323 break;
325 default:
326 if ( ifp == 0 || ifp->if_ioctl == 0 )
327 return( EOPNOTSUPP );
328 lwkt_serialize_enter(ifp->if_serializer);
329 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
330 lwkt_serialize_exit(ifp->if_serializer);
331 return (error);
333 return( 0 );
337 * Given an interface and an at_ifaddr (supposedly on that interface)
338 * remove any routes that depend on this.
339 * Why ifp is needed I'm not sure,
340 * as aa->at_ifaddr.ifa_ifp should be the same.
342 static int
343 at_scrub(struct ifnet *ifp, struct at_ifaddr *aa)
345 int error;
347 if ( aa->aa_flags & AFA_ROUTE ) {
348 if (ifp->if_flags & IFF_LOOPBACK) {
349 if ((error = aa_delsingleroute(&aa->aa_ifa,
350 &aa->aa_addr.sat_addr,
351 &aa->aa_netmask.sat_addr)) != 0) {
352 return( error );
354 } else if (ifp->if_flags & IFF_POINTOPOINT) {
355 if ((error = rtinit( &aa->aa_ifa, RTM_DELETE, RTF_HOST)) != 0)
356 return( error );
357 } else if (ifp->if_flags & IFF_BROADCAST) {
358 error = aa_dorangeroute(&aa->aa_ifa,
359 ntohs(aa->aa_firstnet),
360 ntohs(aa->aa_lastnet),
361 RTM_DELETE );
363 aa->aa_ifa.ifa_flags &= ~IFA_ROUTE;
364 aa->aa_flags &= ~AFA_ROUTE;
366 return( 0 );
370 * given an at_ifaddr,a sockaddr_at and an ifp,
371 * bang them all together at high speed and see what happens
373 static int
374 at_ifinit(struct ifnet *ifp, struct at_ifaddr *aa, struct sockaddr_at *sat)
376 struct netrange nr, onr;
377 struct sockaddr_at oldaddr;
378 int error = 0, i, j;
379 int netinc, nodeinc, nnets;
380 u_short net;
382 crit_enter();
385 * save the old addresses in the at_ifaddr just in case we need them.
387 oldaddr = aa->aa_addr;
388 onr.nr_firstnet = aa->aa_firstnet;
389 onr.nr_lastnet = aa->aa_lastnet;
392 * take the address supplied as an argument, and add it to the
393 * at_ifnet (also given). Remember ing to update
394 * those parts of the at_ifaddr that need special processing
396 bzero( AA_SAT( aa ), sizeof( struct sockaddr_at ));
397 bcopy( sat->sat_zero, &nr, sizeof( struct netrange ));
398 bcopy( sat->sat_zero, AA_SAT( aa )->sat_zero, sizeof( struct netrange ));
399 nnets = ntohs( nr.nr_lastnet ) - ntohs( nr.nr_firstnet ) + 1;
400 aa->aa_firstnet = nr.nr_firstnet;
401 aa->aa_lastnet = nr.nr_lastnet;
403 /* XXX ALC */
404 #if 0
405 kprintf("at_ifinit: %s: %u.%u range %u-%u phase %d\n",
406 ifp->if_name,
407 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
408 ntohs(aa->aa_firstnet), ntohs(aa->aa_lastnet),
409 (aa->aa_flags & AFA_PHASE2) ? 2 : 1);
410 #endif
413 * We could eliminate the need for a second phase 1 probe (post
414 * autoconf) if we check whether we're resetting the node. Note
415 * that phase 1 probes use only nodes, not net.node pairs. Under
416 * phase 2, both the net and node must be the same.
418 if ( ifp->if_flags & IFF_LOOPBACK ) {
419 AA_SAT( aa )->sat_len = sat->sat_len;
420 AA_SAT( aa )->sat_family = AF_APPLETALK;
421 AA_SAT( aa )->sat_addr.s_net = sat->sat_addr.s_net;
422 AA_SAT( aa )->sat_addr.s_node = sat->sat_addr.s_node;
423 #if 0
424 } else if ( fp->if_flags & IFF_POINTOPOINT) {
425 /* unimplemented */
427 * we'd have to copy the dstaddr field over from the sat
428 * but it's not clear that it would contain the right info..
430 #endif
431 } else {
433 * We are a normal (probably ethernet) interface.
434 * apply the new address to the interface structures etc.
435 * We will probe this address on the net first, before
436 * applying it to ensure that it is free.. If it is not, then
437 * we will try a number of other randomly generated addresses
438 * in this net and then increment the net. etc.etc. until
439 * we find an unused address.
441 aa->aa_flags |= AFA_PROBING; /* if not loopback we Must probe? */
442 AA_SAT( aa )->sat_len = sizeof(struct sockaddr_at);
443 AA_SAT( aa )->sat_family = AF_APPLETALK;
444 if ( aa->aa_flags & AFA_PHASE2 ) {
445 if ( sat->sat_addr.s_net == ATADDR_ANYNET ) {
447 * If we are phase 2, and the net was not specified
448 * then we select a random net within the supplied netrange.
449 * XXX use /dev/random?
451 if ( nnets != 1 ) {
452 net = ntohs( nr.nr_firstnet ) + time_second % ( nnets - 1 );
453 } else {
454 net = ntohs( nr.nr_firstnet );
456 } else {
458 * if a net was supplied, then check that it is within
459 * the netrange. If it is not then replace the old values
460 * and return an error
462 if ( ntohs( sat->sat_addr.s_net ) < ntohs( nr.nr_firstnet ) ||
463 ntohs( sat->sat_addr.s_net ) > ntohs( nr.nr_lastnet )) {
464 aa->aa_addr = oldaddr;
465 aa->aa_firstnet = onr.nr_firstnet;
466 aa->aa_lastnet = onr.nr_lastnet;
467 crit_exit();
468 return( EINVAL );
471 * otherwise just use the new net number..
473 net = ntohs( sat->sat_addr.s_net );
475 } else {
477 * we must be phase one, so just use whatever we were given.
478 * I guess it really isn't going to be used... RIGHT?
480 net = ntohs( sat->sat_addr.s_net );
484 * set the node part of the address into the ifaddr.
485 * If it's not specified, be random about it...
486 * XXX use /dev/random?
488 if ( sat->sat_addr.s_node == ATADDR_ANYNODE ) {
489 AA_SAT( aa )->sat_addr.s_node = time_second;
490 } else {
491 AA_SAT( aa )->sat_addr.s_node = sat->sat_addr.s_node;
495 * Copy the phase.
497 AA_SAT( aa )->sat_range.r_netrange.nr_phase
498 = ((aa->aa_flags & AFA_PHASE2) ? 2:1);
501 * step through the nets in the range
502 * starting at the (possibly random) start point.
504 for ( i = nnets, netinc = 1; i > 0; net = ntohs( nr.nr_firstnet ) +
505 (( net - ntohs( nr.nr_firstnet ) + netinc ) % nnets ), i-- ) {
506 AA_SAT( aa )->sat_addr.s_net = htons( net );
509 * using a rather strange stepping method,
510 * stagger through the possible node addresses
511 * Once again, starting at the (possibly random)
512 * initial node address.
514 for ( j = 0, nodeinc = time_second | 1; j < 256;
515 j++, AA_SAT( aa )->sat_addr.s_node += nodeinc ) {
516 if ( AA_SAT( aa )->sat_addr.s_node > 253 ||
517 AA_SAT( aa )->sat_addr.s_node < 1 ) {
518 continue;
520 aa->aa_probcnt = 10;
523 * start off the probes as an asynchronous activity.
524 * though why wait 200mSec?
526 callout_reset(&aa->aa_ch, hz / 5, aarpprobe, ifp);
527 if ( tsleep( aa, PCATCH, "at_ifinit", 0 )) {
529 * theoretically we shouldn't time out here
530 * so if we returned with an error..
532 kprintf( "at_ifinit: why did this happen?!\n" );
533 aa->aa_addr = oldaddr;
534 aa->aa_firstnet = onr.nr_firstnet;
535 aa->aa_lastnet = onr.nr_lastnet;
536 crit_exit();
537 return( EINTR );
541 * The async activity should have woken us up.
542 * We need to see if it was successful in finding
543 * a free spot, or if we need to iterate to the next
544 * address to try.
546 if (( aa->aa_flags & AFA_PROBING ) == 0 ) {
547 break;
552 * of course we need to break out through two loops...
554 if (( aa->aa_flags & AFA_PROBING ) == 0 ) {
555 break;
557 /* reset node for next network */
558 AA_SAT( aa )->sat_addr.s_node = time_second;
562 * if we are still trying to probe, then we have finished all
563 * the possible addresses, so we need to give up
566 if ( aa->aa_flags & AFA_PROBING ) {
567 aa->aa_addr = oldaddr;
568 aa->aa_firstnet = onr.nr_firstnet;
569 aa->aa_lastnet = onr.nr_lastnet;
570 crit_exit();
571 return( EADDRINUSE );
576 * Now that we have selected an address, we need to tell the interface
577 * about it, just in case it needs to adjust something.
579 lwkt_serialize_enter(ifp->if_serializer);
580 if (ifp->if_ioctl &&
581 (error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)aa, NULL))
584 * of course this could mean that it objects violently
585 * so if it does, we back out again..
587 aa->aa_addr = oldaddr;
588 aa->aa_firstnet = onr.nr_firstnet;
589 aa->aa_lastnet = onr.nr_lastnet;
590 lwkt_serialize_exit(ifp->if_serializer);
591 crit_exit();
592 return( error );
594 lwkt_serialize_exit(ifp->if_serializer);
597 * set up the netmask part of the at_ifaddr
598 * and point the appropriate pointer in the ifaddr to it.
599 * probably pointless, but what the heck.. XXX
601 bzero(&aa->aa_netmask, sizeof(aa->aa_netmask));
602 aa->aa_netmask.sat_len = sizeof(struct sockaddr_at);
603 aa->aa_netmask.sat_family = AF_APPLETALK;
604 aa->aa_netmask.sat_addr.s_net = 0xffff;
605 aa->aa_netmask.sat_addr.s_node = 0;
606 aa->aa_ifa.ifa_netmask =(struct sockaddr *) &(aa->aa_netmask); /* XXX */
609 * Initialize broadcast (or remote p2p) address
611 bzero(&aa->aa_broadaddr, sizeof(aa->aa_broadaddr));
612 aa->aa_broadaddr.sat_len = sizeof(struct sockaddr_at);
613 aa->aa_broadaddr.sat_family = AF_APPLETALK;
615 aa->aa_ifa.ifa_metric = ifp->if_metric;
616 if (ifp->if_flags & IFF_BROADCAST) {
617 aa->aa_broadaddr.sat_addr.s_net = htons(0);
618 aa->aa_broadaddr.sat_addr.s_node = 0xff;
619 aa->aa_ifa.ifa_broadaddr = (struct sockaddr *) &aa->aa_broadaddr;
620 /* add the range of routes needed */
621 error = aa_dorangeroute(&aa->aa_ifa,
622 ntohs(aa->aa_firstnet), ntohs(aa->aa_lastnet), RTM_ADD );
624 else if (ifp->if_flags & IFF_POINTOPOINT) {
625 struct at_addr rtaddr, rtmask;
627 bzero(&rtaddr, sizeof(rtaddr));
628 bzero(&rtmask, sizeof(rtmask));
629 /* fill in the far end if we know it here XXX */
630 aa->aa_ifa.ifa_dstaddr = (struct sockaddr *) &aa->aa_dstaddr;
631 error = aa_addsingleroute(&aa->aa_ifa, &rtaddr, &rtmask);
633 else if ( ifp->if_flags & IFF_LOOPBACK ) {
634 struct at_addr rtaddr, rtmask;
636 bzero(&rtaddr, sizeof(rtaddr));
637 bzero(&rtmask, sizeof(rtmask));
638 rtaddr.s_net = AA_SAT( aa )->sat_addr.s_net;
639 rtaddr.s_node = AA_SAT( aa )->sat_addr.s_node;
640 rtmask.s_net = 0xffff;
641 rtmask.s_node = 0x0; /* XXX should not be so.. should be HOST route */
642 error = aa_addsingleroute(&aa->aa_ifa, &rtaddr, &rtmask);
647 * set the address of our "check if this addr is ours" routine.
649 aa->aa_ifa.ifa_claim_addr = aa_claim_addr;
652 * of course if we can't add these routes we back out, but it's getting
653 * risky by now XXX
655 if ( error ) {
656 at_scrub( ifp, aa );
657 aa->aa_addr = oldaddr;
658 aa->aa_firstnet = onr.nr_firstnet;
659 aa->aa_lastnet = onr.nr_lastnet;
660 crit_exit();
661 return( error );
665 * note that the address has a route associated with it....
667 aa->aa_ifa.ifa_flags |= IFA_ROUTE;
668 aa->aa_flags |= AFA_ROUTE;
669 crit_exit();
670 return( 0 );
674 * check whether a given address is a broadcast address for us..
677 at_broadcast(struct sockaddr_at *sat)
679 struct at_ifaddr *aa;
682 * If the node is not right, it can't be a broadcast
684 if ( sat->sat_addr.s_node != ATADDR_BCAST ) {
685 return( 0 );
689 * If the node was right then if the net is right, it's a broadcast
691 if ( sat->sat_addr.s_net == ATADDR_ANYNET ) {
692 return( 1 );
696 * failing that, if the net is one we have, it's a broadcast as well.
698 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
699 if (( aa->aa_ifp->if_flags & IFF_BROADCAST )
700 && ( ntohs( sat->sat_addr.s_net ) >= ntohs( aa->aa_firstnet )
701 && ntohs( sat->sat_addr.s_net ) <= ntohs( aa->aa_lastnet ))) {
702 return( 1 );
705 return( 0 );
709 * aa_dorangeroute()
711 * Add a route for a range of networks from bot to top - 1.
712 * Algorithm:
714 * Split the range into two subranges such that the middle
715 * of the two ranges is the point where the highest bit of difference
716 * between the two addresses makes its transition.
717 * Each of the upper and lower ranges might not exist, or might be
718 * representable by 1 or more netmasks. In addition, if both
719 * ranges can be represented by the same netmask, then they can be merged
720 * by using the next higher netmask..
723 static int
724 aa_dorangeroute(struct ifaddr *ifa, u_int bot, u_int top, int cmd)
726 u_int mask1;
727 struct at_addr addr;
728 struct at_addr mask;
729 int error;
732 * slight sanity check
734 if (bot > top) return (EINVAL);
736 addr.s_node = 0;
737 mask.s_node = 0;
739 * just start out with the lowest boundary
740 * and keep extending the mask till it's too big.
743 while (bot <= top) {
744 mask1 = 1;
745 while ((( bot & ~mask1) >= bot)
746 && (( bot | mask1) <= top)) {
747 mask1 <<= 1;
748 mask1 |= 1;
750 mask1 >>= 1;
751 mask.s_net = htons(~mask1);
752 addr.s_net = htons(bot);
753 if(cmd == RTM_ADD) {
754 error = aa_addsingleroute(ifa,&addr,&mask);
755 if (error) {
756 /* XXX clean up? */
757 return (error);
759 } else {
760 error = aa_delsingleroute(ifa,&addr,&mask);
762 bot = (bot | mask1) + 1;
764 return 0;
767 static int
768 aa_addsingleroute(struct ifaddr *ifa,
769 struct at_addr *addr, struct at_addr *mask)
771 int error;
773 #if 0
774 kprintf("aa_addsingleroute: %x.%x mask %x.%x ...\n",
775 ntohs(addr->s_net), addr->s_node,
776 ntohs(mask->s_net), mask->s_node);
777 #endif
779 error = aa_dosingleroute(ifa, addr, mask, RTM_ADD, RTF_UP);
780 if (error)
781 kprintf("aa_addsingleroute: error %d\n", error);
782 return(error);
785 static int
786 aa_delsingleroute(struct ifaddr *ifa,
787 struct at_addr *addr, struct at_addr *mask)
789 int error;
791 error = aa_dosingleroute(ifa, addr, mask, RTM_DELETE, 0);
792 if (error)
793 kprintf("aa_delsingleroute: error %d\n", error);
794 return(error);
797 static int
798 aa_dosingleroute(struct ifaddr *ifa,
799 struct at_addr *at_addr, struct at_addr *at_mask, int cmd, int flags)
801 struct sockaddr_at addr, mask;
803 bzero(&addr, sizeof(addr));
804 bzero(&mask, sizeof(mask));
805 addr.sat_family = AF_APPLETALK;
806 addr.sat_len = sizeof(struct sockaddr_at);
807 addr.sat_addr.s_net = at_addr->s_net;
808 addr.sat_addr.s_node = at_addr->s_node;
809 mask.sat_family = AF_APPLETALK;
810 mask.sat_len = sizeof(struct sockaddr_at);
811 mask.sat_addr.s_net = at_mask->s_net;
812 mask.sat_addr.s_node = at_mask->s_node;
813 if (at_mask->s_node)
814 flags |= RTF_HOST;
815 return(rtrequest(cmd, (struct sockaddr *) &addr,
816 (flags & RTF_HOST)?(ifa->ifa_dstaddr):(ifa->ifa_addr),
817 (struct sockaddr *) &mask, flags, NULL));
820 #if 0
822 static void
823 aa_clean(void)
825 struct at_ifaddr *aa;
826 struct ifaddr *ifa;
827 struct ifnet *ifp;
829 while ( aa = at_ifaddr ) {
830 ifp = aa->aa_ifp;
831 at_scrub( ifp, aa );
832 at_ifaddr = aa->aa_next;
833 if (( ifa = ifp->if_addrlist ) == (struct ifaddr *)aa ) {
834 ifp->if_addrlist = ifa->ifa_next;
835 } else {
836 while ( ifa->ifa_next &&
837 ( ifa->ifa_next != (struct ifaddr *)aa )) {
838 ifa = ifa->ifa_next;
840 if ( ifa->ifa_next ) {
841 ifa->ifa_next = ((struct ifaddr *)aa)->ifa_next;
842 } else {
843 panic( "at_entry" );
849 #endif
851 static int
852 aa_claim_addr(struct ifaddr *ifa, struct sockaddr *gw0)
854 struct sockaddr_at *addr = (struct sockaddr_at *)ifa->ifa_addr;
855 struct sockaddr_at *gw = (struct sockaddr_at *)gw0;
857 switch (gw->sat_range.r_netrange.nr_phase) {
858 case 1:
859 if(addr->sat_range.r_netrange.nr_phase == 1)
860 return 1;
861 case 0:
862 case 2:
864 * if it's our net (including 0),
865 * or netranges are valid, and we are in the range,
866 * then it's ours.
868 if ((addr->sat_addr.s_net == gw->sat_addr.s_net)
869 || ((addr->sat_range.r_netrange.nr_lastnet)
870 && (ntohs(gw->sat_addr.s_net)
871 >= ntohs(addr->sat_range.r_netrange.nr_firstnet ))
872 && (ntohs(gw->sat_addr.s_net)
873 <= ntohs(addr->sat_range.r_netrange.nr_lastnet )))) {
874 return 1;
876 break;
877 default:
878 kprintf("atalk: bad phase\n");
880 return 0;