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