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