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