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