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