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