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