Home | History | Annotate | Line # | Download | only in netatalk
at_control.c revision 1.2
      1 /*	$NetBSD: at_control.c,v 1.2 2000/02/01 22:52:06 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 /*
    334  * Given an interface and an at_ifaddr (supposedly on that interface) remove
    335  * any routes that depend on this. Why ifp is needed I'm not sure, as
    336  * aa->at_ifaddr.ifa_ifp should be the same.
    337  */
    338 static int
    339 at_scrub(ifp, aa)
    340 	struct ifnet   *ifp;
    341 	struct at_ifaddr *aa;
    342 {
    343 	int error = 0;
    344 
    345 	if (aa->aa_flags & AFA_ROUTE) {
    346 		if (ifp->if_flags & IFF_LOOPBACK)
    347 			error = aa_delsingleroute(&aa->aa_ifa,
    348 			    &aa->aa_addr.sat_addr, &aa->aa_netmask.sat_addr);
    349 		else if (ifp->if_flags & IFF_POINTOPOINT)
    350 			error = rtinit(&aa->aa_ifa, RTM_DELETE, RTF_HOST);
    351 		else if (ifp->if_flags & IFF_BROADCAST)
    352 			error = aa_dorangeroute(&aa->aa_ifa,
    353 			    ntohs(aa->aa_firstnet), ntohs(aa->aa_lastnet),
    354 			    RTM_DELETE);
    355 
    356 		aa->aa_ifa.ifa_flags &= ~IFA_ROUTE;
    357 		aa->aa_flags &= ~AFA_ROUTE;
    358 	}
    359 	return error;
    360 }
    361 
    362 /*
    363  * given an at_ifaddr,a sockaddr_at and an ifp,
    364  * bang them all together at high speed and see what happens
    365  */
    366 static int
    367 at_ifinit(ifp, aa, sat)
    368 	struct ifnet   *ifp;
    369 	struct at_ifaddr *aa;
    370 	struct sockaddr_at *sat;
    371 {
    372 	struct netrange nr, onr;
    373 	struct sockaddr_at oldaddr;
    374 	int             s = splimp(), error = 0, i, j;
    375 	int             netinc, nodeinc, nnets;
    376 	u_short         net;
    377 
    378 	/*
    379 	 * save the old addresses in the at_ifaddr just in case we need them.
    380 	 */
    381 	oldaddr = aa->aa_addr;
    382 	onr.nr_firstnet = aa->aa_firstnet;
    383 	onr.nr_lastnet = aa->aa_lastnet;
    384 
    385 	/*
    386          * take the address supplied as an argument, and add it to the
    387          * at_ifnet (also given). Remember ing to update
    388          * those parts of the at_ifaddr that need special processing
    389          */
    390 	bzero(AA_SAT(aa), sizeof(struct sockaddr_at));
    391 	bcopy(sat->sat_zero, &nr, sizeof(struct netrange));
    392 	bcopy(sat->sat_zero, AA_SAT(aa)->sat_zero, sizeof(struct netrange));
    393 	nnets = ntohs(nr.nr_lastnet) - ntohs(nr.nr_firstnet) + 1;
    394 	aa->aa_firstnet = nr.nr_firstnet;
    395 	aa->aa_lastnet = nr.nr_lastnet;
    396 
    397 #ifdef NETATALKDEBUG
    398 	printf("at_ifinit: %s: %u.%u range %u-%u phase %d\n",
    399 	    ifp->if_xname,
    400 	    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
    401 	    ntohs(aa->aa_firstnet), ntohs(aa->aa_lastnet),
    402 	    (aa->aa_flags & AFA_PHASE2) ? 2 : 1);
    403 #endif
    404 
    405 	/*
    406          * We could eliminate the need for a second phase 1 probe (post
    407          * autoconf) if we check whether we're resetting the node. Note
    408          * that phase 1 probes use only nodes, not net.node pairs.  Under
    409          * phase 2, both the net and node must be the same.
    410          */
    411 	AA_SAT(aa)->sat_len = sat->sat_len;
    412 	AA_SAT(aa)->sat_family = AF_APPLETALK;
    413 	if (ifp->if_flags & IFF_LOOPBACK) {
    414 		AA_SAT(aa)->sat_addr.s_net = sat->sat_addr.s_net;
    415 		AA_SAT(aa)->sat_addr.s_node = sat->sat_addr.s_node;
    416 #if 0
    417 	} else if (fp->if_flags & IFF_POINTOPOINT) {
    418 		/* unimplemented */
    419 		/*
    420 		 * we'd have to copy the dstaddr field over from the sat
    421 		 * but it's not clear that it would contain the right info..
    422 		 */
    423 #endif
    424 	} else {
    425 		/*
    426 		 * We are a normal (probably ethernet) interface.
    427 		 * apply the new address to the interface structures etc.
    428 		 * We will probe this address on the net first, before
    429 		 * applying it to ensure that it is free.. If it is not, then
    430 		 * we will try a number of other randomly generated addresses
    431 		 * in this net and then increment the net.  etc.etc. until
    432 		 * we find an unused address.
    433 		 */
    434 		aa->aa_flags |= AFA_PROBING;	/* if not loopback we Must
    435 						 * probe? */
    436 		if (aa->aa_flags & AFA_PHASE2) {
    437 			if (sat->sat_addr.s_net == ATADDR_ANYNET) {
    438 				/*
    439 				 * If we are phase 2, and the net was not
    440 				 * specified * then we select a random net
    441 				 * within the supplied netrange.
    442 				 * XXX use /dev/random?
    443 				 */
    444 				if (nnets != 1) {
    445 					net = ntohs(nr.nr_firstnet) +
    446 					    time.tv_sec % (nnets - 1);
    447 				} else {
    448 					net = ntohs(nr.nr_firstnet);
    449 				}
    450 			} else {
    451 				/*
    452 				 * if a net was supplied, then check that it
    453 				 * is within the netrange. If it is not then
    454 				 * replace the old values and return an error
    455 				 */
    456 				if (ntohs(sat->sat_addr.s_net) <
    457 				    ntohs(nr.nr_firstnet) ||
    458 				    ntohs(sat->sat_addr.s_net) >
    459 				    ntohs(nr.nr_lastnet)) {
    460 					aa->aa_addr = oldaddr;
    461 					aa->aa_firstnet = onr.nr_firstnet;
    462 					aa->aa_lastnet = onr.nr_lastnet;
    463 					splx(s);
    464 					return (EINVAL);
    465 				}
    466 				/*
    467 				 * otherwise just use the new net number..
    468 				 */
    469 				net = ntohs(sat->sat_addr.s_net);
    470 			}
    471 		} else {
    472 			/*
    473 		         * we must be phase one, so just use whatever we were
    474 			 * given. I guess it really isn't going to be used...
    475 			 * RIGHT?
    476 		         */
    477 			net = ntohs(sat->sat_addr.s_net);
    478 		}
    479 
    480 		/*
    481 		 * set the node part of the address into the ifaddr. If it's
    482 		 * not specified, be random about it... XXX use /dev/random?
    483 		 */
    484 		if (sat->sat_addr.s_node == ATADDR_ANYNODE) {
    485 			AA_SAT(aa)->sat_addr.s_node = time.tv_sec;
    486 		} else {
    487 			AA_SAT(aa)->sat_addr.s_node = sat->sat_addr.s_node;
    488 		}
    489 
    490 		/*
    491 		 * step through the nets in the range starting at the
    492 		 * (possibly random) start point.
    493 		 */
    494 		for (i = nnets, netinc = 1; i > 0; net = ntohs(nr.nr_firstnet) +
    495 		     ((net - ntohs(nr.nr_firstnet) + netinc) % nnets), i--) {
    496 			AA_SAT(aa)->sat_addr.s_net = htons(net);
    497 
    498 			/*
    499 		         * using a rather strange stepping method,
    500 		         * stagger through the possible node addresses
    501 		         * Once again, starting at the (possibly random)
    502 		         * initial node address.
    503 		         */
    504 			for (j = 0, nodeinc = time.tv_sec | 1; j < 256;
    505 			     j++, AA_SAT(aa)->sat_addr.s_node += nodeinc) {
    506 				if (AA_SAT(aa)->sat_addr.s_node > 253 ||
    507 				    AA_SAT(aa)->sat_addr.s_node < 1) {
    508 					continue;
    509 				}
    510 				aa->aa_probcnt = 10;
    511 
    512 				/*
    513 				 * start off the probes as an asynchronous
    514 				 * activity. though why wait 200mSec?
    515 				 */
    516 				timeout(aarpprobe, ifp, hz / 5);
    517 				if (tsleep(aa, PPAUSE | PCATCH, "at_ifinit",
    518 				    0)) {
    519 					/*
    520 				         * theoretically we shouldn't time out
    521 					 * here so if we returned with an error.
    522 				         */
    523 					printf("at_ifinit: timeout?!\n");
    524 					aa->aa_addr = oldaddr;
    525 					aa->aa_firstnet = onr.nr_firstnet;
    526 					aa->aa_lastnet = onr.nr_lastnet;
    527 					splx(s);
    528 					return (EINTR);
    529 				}
    530 				/*
    531 				 * The async activity should have woken us
    532 				 * up. We need to see if it was successful in
    533 				 * finding a free spot, or if we need to
    534 				 * iterate to the next address to try.
    535 				 */
    536 				if ((aa->aa_flags & AFA_PROBING) == 0)
    537 					break;
    538 			}
    539 
    540 			/*
    541 		         * of course we need to break out through two loops...
    542 		         */
    543 			if ((aa->aa_flags & AFA_PROBING) == 0)
    544 				break;
    545 
    546 			/* reset node for next network */
    547 			AA_SAT(aa)->sat_addr.s_node = time.tv_sec;
    548 		}
    549 
    550 		/*
    551 		 * if we are still trying to probe, then we have finished all
    552 		 * the possible addresses, so we need to give up
    553 		 */
    554 		if (aa->aa_flags & AFA_PROBING) {
    555 			aa->aa_addr = oldaddr;
    556 			aa->aa_firstnet = onr.nr_firstnet;
    557 			aa->aa_lastnet = onr.nr_lastnet;
    558 			splx(s);
    559 			return (EADDRINUSE);
    560 		}
    561 	}
    562 
    563 	/*
    564 	 * Now that we have selected an address, we need to tell the
    565 	 * interface about it, just in case it needs to adjust something.
    566 	 */
    567 	if (ifp->if_ioctl &&
    568 	    (error = (*ifp->if_ioctl) (ifp, SIOCSIFADDR, (caddr_t) aa))) {
    569 		/*
    570 		 * of course this could mean that it objects violently
    571 		 * so if it does, we back out again..
    572 		 */
    573 		aa->aa_addr = oldaddr;
    574 		aa->aa_firstnet = onr.nr_firstnet;
    575 		aa->aa_lastnet = onr.nr_lastnet;
    576 		splx(s);
    577 		return (error);
    578 	}
    579 	/*
    580 	 * set up the netmask part of the at_ifaddr and point the appropriate
    581 	 * pointer in the ifaddr to it. probably pointless, but what the
    582 	 * heck.. XXX
    583 	 */
    584 	bzero(&aa->aa_netmask, sizeof(aa->aa_netmask));
    585 	aa->aa_netmask.sat_len = sizeof(struct sockaddr_at);
    586 	aa->aa_netmask.sat_family = AF_APPLETALK;
    587 	aa->aa_netmask.sat_addr.s_net = 0xffff;
    588 	aa->aa_netmask.sat_addr.s_node = 0;
    589 #if 0
    590 	aa->aa_ifa.ifa_netmask = (struct sockaddr *) &(aa->aa_netmask);/* XXX */
    591 #endif
    592 
    593 	/*
    594          * Initialize broadcast (or remote p2p) address
    595          */
    596 	bzero(&aa->aa_broadaddr, sizeof(aa->aa_broadaddr));
    597 	aa->aa_broadaddr.sat_len = sizeof(struct sockaddr_at);
    598 	aa->aa_broadaddr.sat_family = AF_APPLETALK;
    599 
    600 	aa->aa_ifa.ifa_metric = ifp->if_metric;
    601 	if (ifp->if_flags & IFF_BROADCAST) {
    602 		aa->aa_broadaddr.sat_addr.s_net = htons(0);
    603 		aa->aa_broadaddr.sat_addr.s_node = 0xff;
    604 		aa->aa_ifa.ifa_broadaddr =
    605 		    (struct sockaddr *) &aa->aa_broadaddr;
    606 		/* add the range of routes needed */
    607 		error = aa_dorangeroute(&aa->aa_ifa,
    608 		    ntohs(aa->aa_firstnet), ntohs(aa->aa_lastnet), RTM_ADD);
    609 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
    610 		struct at_addr  rtaddr, rtmask;
    611 
    612 		bzero(&rtaddr, sizeof(rtaddr));
    613 		bzero(&rtmask, sizeof(rtmask));
    614 		/* fill in the far end if we know it here XXX */
    615 		aa->aa_ifa.ifa_dstaddr = (struct sockaddr *) & aa->aa_dstaddr;
    616 		error = aa_addsingleroute(&aa->aa_ifa, &rtaddr, &rtmask);
    617 	} else if (ifp->if_flags & IFF_LOOPBACK) {
    618 		struct at_addr  rtaddr, rtmask;
    619 
    620 		bzero(&rtaddr, sizeof(rtaddr));
    621 		bzero(&rtmask, sizeof(rtmask));
    622 		rtaddr.s_net = AA_SAT(aa)->sat_addr.s_net;
    623 		rtaddr.s_node = AA_SAT(aa)->sat_addr.s_node;
    624 		rtmask.s_net = 0xffff;
    625 		rtmask.s_node = 0x0;
    626 		error = aa_addsingleroute(&aa->aa_ifa, &rtaddr, &rtmask);
    627 	}
    628 	/*
    629          * of course if we can't add these routes we back out, but it's getting
    630          * risky by now XXX
    631          */
    632 	if (error) {
    633 		at_scrub(ifp, aa);
    634 		aa->aa_addr = oldaddr;
    635 		aa->aa_firstnet = onr.nr_firstnet;
    636 		aa->aa_lastnet = onr.nr_lastnet;
    637 		splx(s);
    638 		return (error);
    639 	}
    640 	/*
    641          * note that the address has a route associated with it....
    642          */
    643 	aa->aa_ifa.ifa_flags |= IFA_ROUTE;
    644 	aa->aa_flags |= AFA_ROUTE;
    645 	splx(s);
    646 	return (0);
    647 }
    648 
    649 /*
    650  * check whether a given address is a broadcast address for us..
    651  */
    652 int
    653 at_broadcast(sat)
    654 	struct sockaddr_at *sat;
    655 {
    656 	struct at_ifaddr *aa;
    657 
    658 	/*
    659          * If the node is not right, it can't be a broadcast
    660          */
    661 	if (sat->sat_addr.s_node != ATADDR_BCAST)
    662 		return 0;
    663 
    664 	/*
    665          * If the node was right then if the net is right, it's a broadcast
    666          */
    667 	if (sat->sat_addr.s_net == ATADDR_ANYNET)
    668 		return 1;
    669 
    670 	/*
    671          * failing that, if the net is one we have, it's a broadcast as well.
    672          */
    673 	for (aa = at_ifaddr.tqh_first; aa; aa = aa->aa_list.tqe_next) {
    674 		if ((aa->aa_ifp->if_flags & IFF_BROADCAST)
    675 		    && (ntohs(sat->sat_addr.s_net) >= ntohs(aa->aa_firstnet)
    676 		  && ntohs(sat->sat_addr.s_net) <= ntohs(aa->aa_lastnet)))
    677 			return 1;
    678 	}
    679 	return 0;
    680 }
    681 
    682 
    683 /*
    684  * aa_dorangeroute()
    685  *
    686  * Add a route for a range of networks from bot to top - 1.
    687  * Algorithm:
    688  *
    689  * Split the range into two subranges such that the middle
    690  * of the two ranges is the point where the highest bit of difference
    691  * between the two addresses, makes it's transition
    692  * Each of the upper and lower ranges might not exist, or might be
    693  * representable by 1 or more netmasks. In addition, if both
    694  * ranges can be represented by the same netmask, then teh can be merged
    695  * by using the next higher netmask..
    696  */
    697 
    698 static int
    699 aa_dorangeroute(ifa, bot, top, cmd)
    700 	struct ifaddr *ifa;
    701 	u_int bot;
    702 	u_int top;
    703 	int cmd;
    704 {
    705 	u_int           mask1;
    706 	struct at_addr  addr;
    707 	struct at_addr  mask;
    708 	int             error;
    709 
    710 	/*
    711 	 * slight sanity check
    712 	 */
    713 	if (bot > top)
    714 		return (EINVAL);
    715 
    716 	addr.s_node = 0;
    717 	mask.s_node = 0;
    718 	/*
    719 	 * just start out with the lowest boundary
    720 	 * and keep extending the mask till it's too big.
    721 	 */
    722 
    723 	while (bot <= top) {
    724 		mask1 = 1;
    725 		while (((bot & ~mask1) >= bot)
    726 		       && ((bot | mask1) <= top)) {
    727 			mask1 <<= 1;
    728 			mask1 |= 1;
    729 		}
    730 		mask1 >>= 1;
    731 		mask.s_net = htons(~mask1);
    732 		addr.s_net = htons(bot);
    733 		if (cmd == RTM_ADD) {
    734 			error = aa_addsingleroute(ifa, &addr, &mask);
    735 			if (error) {
    736 				/* XXX clean up? */
    737 				return (error);
    738 			}
    739 		} else {
    740 			error = aa_delsingleroute(ifa, &addr, &mask);
    741 		}
    742 		bot = (bot | mask1) + 1;
    743 	}
    744 	return 0;
    745 }
    746 
    747 static int
    748 aa_addsingleroute(ifa, addr, mask)
    749 	struct ifaddr *ifa;
    750 	struct at_addr *addr;
    751 	struct at_addr *mask;
    752 {
    753 	int error;
    754 
    755 #ifdef NETATALKDEBUG
    756 	printf("aa_addsingleroute: %x.%x mask %x.%x ...",
    757 	       ntohs(addr->s_net), addr->s_node,
    758 	       ntohs(mask->s_net), mask->s_node);
    759 #endif
    760 
    761 	error = aa_dosingleroute(ifa, addr, mask, RTM_ADD, RTF_UP);
    762 #ifdef NETATALKDEBUG
    763 	if (error)
    764 		printf("aa_addsingleroute: error %d\n", error);
    765 #endif
    766 	return (error);
    767 }
    768 
    769 static int
    770 aa_delsingleroute(ifa, addr, mask)
    771 	struct ifaddr *ifa;
    772 	struct at_addr *addr;
    773 	struct at_addr *mask;
    774 {
    775 	int error;
    776 
    777 #ifdef NETATALKDEBUG
    778 	printf("aa_delsingleroute: %x.%x mask %x.%x ...",
    779 	       ntohs(addr->s_net), addr->s_node,
    780 	       ntohs(mask->s_net), mask->s_node);
    781 #endif
    782 
    783 	error = aa_dosingleroute(ifa, addr, mask, RTM_DELETE, 0);
    784 #ifdef NETATALKDEBUG
    785 	if (error)
    786 		printf("aa_delsingleroute: error %d\n", error);
    787 #endif
    788 	return (error);
    789 }
    790 
    791 static int
    792 aa_dosingleroute(ifa, at_addr, at_mask, cmd, flags)
    793 	struct ifaddr *ifa;
    794 	struct at_addr *at_addr;
    795 	struct at_addr *at_mask;
    796 	int cmd;
    797 	int flags;
    798 {
    799 	struct sockaddr_at addr, mask, *gate;
    800 
    801 	bzero(&addr, sizeof(addr));
    802 	bzero(&mask, sizeof(mask));
    803 	addr.sat_family = AF_APPLETALK;
    804 	addr.sat_len = sizeof(struct sockaddr_at);
    805 	addr.sat_addr.s_net = at_addr->s_net;
    806 	addr.sat_addr.s_node = at_addr->s_node;
    807 	mask.sat_family = AF_APPLETALK;
    808 	mask.sat_len = sizeof(struct sockaddr_at);
    809 	mask.sat_addr.s_net = at_mask->s_net;
    810 	mask.sat_addr.s_node = at_mask->s_node;
    811 
    812 	if (at_mask->s_node) {
    813 		gate = satosat(ifa->ifa_dstaddr);
    814 		flags |= RTF_HOST;
    815 	} else {
    816 		gate = satosat(ifa->ifa_addr);
    817 	}
    818 
    819 #ifdef NETATALKDEBUG
    820 	printf("on %s %x.%x\n", (flags & RTF_HOST) ? "host" : "net",
    821 	       ntohs(gate->sat_addr.s_net), gate->sat_addr.s_node);
    822 #endif
    823 	return (rtrequest(cmd, (struct sockaddr *) &addr,
    824 	    (struct sockaddr *) gate, (struct sockaddr *) &mask, flags, NULL));
    825 }
    826 
    827 #if 0
    828 static void
    829 aa_clean()
    830 {
    831 	struct at_ifaddr *aa;
    832 	struct ifaddr  *ifa;
    833 	struct ifnet   *ifp;
    834 
    835 	while (aa = at_ifaddr) {
    836 		ifp = aa->aa_ifp;
    837 		at_scrub(ifp, aa);
    838 		at_ifaddr = aa->aa_next;
    839 		if ((ifa = ifp->if_addrlist) == (struct ifaddr *) aa) {
    840 			ifp->if_addrlist = ifa->ifa_next;
    841 		} else {
    842 			while (ifa->ifa_next &&
    843 			       (ifa->ifa_next != (struct ifaddr *) aa)) {
    844 				ifa = ifa->ifa_next;
    845 			}
    846 			if (ifa->ifa_next) {
    847 				ifa->ifa_next =
    848 				    ((struct ifaddr *) aa)->ifa_next;
    849 			} else {
    850 				panic("at_entry");
    851 			}
    852 		}
    853 	}
    854 }
    855 #endif
    856