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