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