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