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