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