2 * Copyright (c) 1990,1991 Regents of The University of Michigan.
5 * Permission to use, copy, modify, and distribute this software and
6 * its documentation for any purpose and without fee is hereby granted,
7 * provided that the above copyright notice appears in all copies and
8 * that both that copyright notice and this permission notice appear
9 * in supporting documentation, and that the name of The University
10 * of Michigan not be used in advertising or publicity pertaining to
11 * distribution of the software without specific, written prior
12 * permission. This software is supplied as is without expressed or
13 * implied warranties of any kind.
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
18 * Research Systems Unix Group
19 * The University of Michigan
21 * 535 W. William Street
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/sockio.h>
32 #include <sys/malloc.h>
33 #include <sys/kernel.h>
34 #include <sys/socket.h>
36 #include <net/route.h>
37 #include <netinet/in.h>
39 #include <netinet/if_ether.h>
41 #include <netatalk/at.h>
42 #include <netatalk/at_var.h>
43 #include <netatalk/at_extern.h>
45 struct at_ifaddr *at_ifaddr_list;
47 static int aa_dorangeroute(struct ifaddr *ifa, u_int first, u_int last,
49 static int aa_addsingleroute(struct ifaddr *ifa, struct at_addr *addr,
50 struct at_addr *mask);
51 static int aa_delsingleroute(struct ifaddr *ifa, struct at_addr *addr,
52 struct at_addr *mask);
53 static int aa_dosingleroute(struct ifaddr *ifa, struct at_addr *addr,
54 struct at_addr *mask, int cmd, int flags);
55 static int at_scrub(struct ifnet *ifp, struct at_ifaddr *aa);
56 static int at_ifinit(struct ifnet *ifp, struct at_ifaddr *aa,
57 struct sockaddr_at *sat);
58 static int aa_claim_addr(struct ifaddr *ifa, struct sockaddr *gw);
60 #define sateqaddr(a,b) \
61 ((a)->sat_len == (b)->sat_len && \
62 (a)->sat_family == (b)->sat_family && \
63 (a)->sat_addr.s_net == (b)->sat_addr.s_net && \
64 (a)->sat_addr.s_node == (b)->sat_addr.s_node)
67 at_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
70 struct ifreq *ifr = (struct ifreq *)data;
71 struct sockaddr_at *sat;
73 struct at_aliasreq *ifra = (struct at_aliasreq *)data;
74 struct at_ifaddr *aa0;
75 struct at_ifaddr *aa = NULL;
76 struct ifaddr *ifa, *ifa0;
79 * If we have an ifp, then find the matching at_ifaddr if it exists
82 for (aa = at_ifaddr_list; aa != NULL; aa = aa->aa_next) {
83 if (aa->aa_ifp == ifp)
89 * In this first switch table we are basically getting ready for
90 * the second one, by getting the atalk-specific things set up
91 * so that they start to look more similar to other protocols etc.
98 * If we have an appletalk sockaddr, scan forward of where we
99 * are now on the at_ifaddr list to find one with a matching
100 * address on this interface. This may leave aa pointing to
101 * the first address on the NEXT interface!
103 if (ifra->ifra_addr.sat_family == AF_APPLETALK) {
104 for (; aa; aa = aa->aa_next) {
105 if (aa->aa_ifp == ifp &&
106 sateqaddr(&aa->aa_addr, &ifra->ifra_addr))
111 * If we a retrying to delete an addres but didn't find such,
112 * then rewurn with an error
114 if (cmd == SIOCDIFADDR && aa == NULL)
115 return (EADDRNOTAVAIL);
120 * If we are not superuser, then we don't get to do these ops.
125 sat = satosat(&ifr->ifr_addr);
126 nr = (struct netrange *)sat->sat_zero;
127 if (nr->nr_phase == 1) {
129 * Look for a phase 1 address on this interface.
130 * This may leave aa pointing to the first address on
131 * the NEXT interface!
133 for (; aa; aa = aa->aa_next) {
134 if (aa->aa_ifp == ifp &&
135 (aa->aa_flags & AFA_PHASE2) == 0)
138 } else { /* default to phase 2 */
140 * Look for a phase 2 address on this interface.
141 * This may leave aa pointing to the first address on
142 * the NEXT interface!
144 for (; aa; aa = aa->aa_next) {
145 if (aa->aa_ifp == ifp && (aa->aa_flags &
155 * If we failed to find an existing at_ifaddr entry, then we
156 * allocate a fresh one.
159 aa0 = malloc(sizeof(struct at_ifaddr), M_IFADDR,
161 if ((aa = at_ifaddr_list) != NULL) {
163 * Don't let the loopback be first, since the
164 * first address is the machine's default
165 * address for binding. If it is, stick
166 * ourself in front, otherwise go to the back
169 if (at_ifaddr_list->aa_ifp->if_flags &
172 aa->aa_next = at_ifaddr_list;
175 for (; aa->aa_next; aa = aa->aa_next)
180 at_ifaddr_list = aa0;
184 * Find the end of the interface's addresses
185 * and link our new one on the end
187 ifa = (struct ifaddr *)aa;
190 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
193 * As the at_ifaddr contains the actual sockaddrs,
194 * and the ifaddr itself, link them al together
197 ifa->ifa_addr = (struct sockaddr *)&aa->aa_addr;
198 ifa->ifa_dstaddr = (struct sockaddr *)&aa->aa_addr;
199 ifa->ifa_netmask = (struct sockaddr *)&aa->aa_netmask;
202 * Set/clear the phase 2 bit.
204 if (nr->nr_phase == 1)
205 aa->aa_flags &= ~AFA_PHASE2;
207 aa->aa_flags |= AFA_PHASE2;
210 * and link it all together
215 * If we DID find one then we clobber any routes
223 sat = satosat(&ifr->ifr_addr);
224 nr = (struct netrange *)sat->sat_zero;
225 if (nr->nr_phase == 1) {
227 * If the request is specifying phase 1, then
228 * only look at a phase one address
230 for (; aa; aa = aa->aa_next) {
231 if (aa->aa_ifp == ifp &&
232 (aa->aa_flags & AFA_PHASE2) == 0)
239 for (; aa; aa = aa->aa_next) {
240 if (aa->aa_ifp == ifp && (aa->aa_flags &
247 return (EADDRNOTAVAIL);
252 * By the time this switch is run we should be able to assume that
253 * the "aa" pointer is valid when needed.
259 * copy the contents of the sockaddr blindly.
261 sat = (struct sockaddr_at *)&ifr->ifr_addr;
265 * and do some cleanups
267 ((struct netrange *)&sat->sat_zero)->nr_phase
268 = (aa->aa_flags & AFA_PHASE2) ? 2 : 1;
269 ((struct netrange *)&sat->sat_zero)->nr_firstnet =
271 ((struct netrange *)&sat->sat_zero)->nr_lastnet =
276 return (at_ifinit(ifp, aa,
277 (struct sockaddr_at *)&ifr->ifr_addr));
280 if (sateqaddr(&ifra->ifra_addr, &aa->aa_addr))
282 return (at_ifinit(ifp, aa,
283 (struct sockaddr_at *)&ifr->ifr_addr));
287 * scrub all routes.. didn't we just DO this? XXX yes, del it
292 * remove the ifaddr from the interface
294 ifa0 = (struct ifaddr *)aa;
295 TAILQ_REMOVE(&ifp->if_addrhead, ifa0, ifa_link);
298 * Now remove the at_ifaddr from the parallel structure
299 * as well, or we'd be in deep trouble
302 if (aa0 == (aa = at_ifaddr_list)) {
303 at_ifaddr_list = aa->aa_next;
305 while (aa->aa_next && (aa->aa_next != aa0))
309 * if we found it, remove it, otherwise we screwed up.
312 aa->aa_next = aa0->aa_next;
318 * Now reclaim the reference.
324 if (ifp == NULL || ifp->if_ioctl == NULL)
326 return ((*ifp->if_ioctl)(ifp, cmd, data));
332 * Given an interface and an at_ifaddr (supposedly on that interface)
333 * remove any routes that depend on this.
334 * Why ifp is needed I'm not sure,
335 * as aa->at_ifaddr.ifa_ifp should be the same.
338 at_scrub(struct ifnet *ifp, struct at_ifaddr *aa)
342 if (aa->aa_flags & AFA_ROUTE) {
343 if (ifp->if_flags & IFF_LOOPBACK) {
344 if ((error = aa_delsingleroute(&aa->aa_ifa,
345 &aa->aa_addr.sat_addr, &aa->aa_netmask.sat_addr))
348 } else if (ifp->if_flags & IFF_POINTOPOINT) {
349 if ((error = rtinit(&aa->aa_ifa, RTM_DELETE,
352 } else if (ifp->if_flags & IFF_BROADCAST) {
353 error = aa_dorangeroute(&aa->aa_ifa,
354 ntohs(aa->aa_firstnet), ntohs(aa->aa_lastnet),
357 aa->aa_ifa.ifa_flags &= ~IFA_ROUTE;
358 aa->aa_flags &= ~AFA_ROUTE;
364 * given an at_ifaddr,a sockaddr_at and an ifp,
365 * bang them all together at high speed and see what happens
368 at_ifinit(struct ifnet *ifp, struct at_ifaddr *aa, struct sockaddr_at *sat)
370 struct netrange nr, onr;
371 struct sockaddr_at oldaddr;
373 int netinc, nodeinc, nnets;
377 * save the old addresses in the at_ifaddr just in case we need them.
379 oldaddr = aa->aa_addr;
380 onr.nr_firstnet = aa->aa_firstnet;
381 onr.nr_lastnet = aa->aa_lastnet;
384 * take the address supplied as an argument, and add it to the
385 * at_ifnet (also given). Remember ing to update
386 * those parts of the at_ifaddr that need special processing
388 bzero(AA_SAT(aa), sizeof(struct sockaddr_at));
389 bcopy(sat->sat_zero, &nr, sizeof(struct netrange));
390 bcopy(sat->sat_zero, AA_SAT(aa)->sat_zero, sizeof(struct netrange));
391 nnets = ntohs(nr.nr_lastnet) - ntohs(nr.nr_firstnet) + 1;
392 aa->aa_firstnet = nr.nr_firstnet;
393 aa->aa_lastnet = nr.nr_lastnet;
397 printf("at_ifinit: %s: %u.%u range %u-%u phase %d\n",
399 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
400 ntohs(aa->aa_firstnet), ntohs(aa->aa_lastnet),
401 (aa->aa_flags & AFA_PHASE2) ? 2 : 1);
405 * We could eliminate the need for a second phase 1 probe (post
406 * autoconf) if we check whether we're resetting the node. Note
407 * that phase 1 probes use only nodes, not net.node pairs. Under
408 * phase 2, both the net and node must be the same.
410 if (ifp->if_flags & IFF_LOOPBACK) {
411 AA_SAT(aa)->sat_len = sat->sat_len;
412 AA_SAT(aa)->sat_family = AF_APPLETALK;
413 AA_SAT(aa)->sat_addr.s_net = sat->sat_addr.s_net;
414 AA_SAT(aa)->sat_addr.s_node = sat->sat_addr.s_node;
416 } else if (fp->if_flags & IFF_POINTOPOINT) {
419 * we'd have to copy the dstaddr field over from the sat
420 * but it's not clear that it would contain the right info..
425 * We are a normal (probably ethernet) interface.
426 * apply the new address to the interface structures etc.
427 * We will probe this address on the net first, before
428 * applying it to ensure that it is free.. If it is not, then
429 * we will try a number of other randomly generated addresses
430 * in this net and then increment the net. etc.etc. until
431 * we find an unused address.
433 aa->aa_flags |= AFA_PROBING; /* not loopback we Must probe? */
434 AA_SAT(aa)->sat_len = sizeof(struct sockaddr_at);
435 AA_SAT(aa)->sat_family = AF_APPLETALK;
436 if (aa->aa_flags & AFA_PHASE2) {
437 if (sat->sat_addr.s_net == ATADDR_ANYNET) {
439 * If we are phase 2, and the net was not
440 * specified then we select a random net
441 * within the supplied netrange.
442 * XXX use /dev/random?
445 net = ntohs(nr.nr_firstnet) +
446 time_second % (nnets - 1);
448 net = ntohs(nr.nr_firstnet);
451 * if a net was supplied, then check that it
452 * is within the netrange. If it is not then
453 * replace the old values and return an error
455 if (ntohs(sat->sat_addr.s_net) <
456 ntohs(nr.nr_firstnet) ||
457 ntohs(sat->sat_addr.s_net) >
458 ntohs(nr.nr_lastnet)) {
459 aa->aa_addr = oldaddr;
460 aa->aa_firstnet = onr.nr_firstnet;
461 aa->aa_lastnet = onr.nr_lastnet;
465 * otherwise just use the new net number..
467 net = ntohs(sat->sat_addr.s_net);
471 * we must be phase one, so just use whatever we were
472 * given. I guess it really isn't going to be
475 net = ntohs(sat->sat_addr.s_net);
479 * set the node part of the address into the ifaddr.
480 * If it's not specified, be random about it...
481 * XXX use /dev/random?
483 if (sat->sat_addr.s_node == ATADDR_ANYNODE)
484 AA_SAT(aa)->sat_addr.s_node = time_second;
486 AA_SAT(aa)->sat_addr.s_node = sat->sat_addr.s_node;
491 AA_SAT(aa)->sat_range.r_netrange.nr_phase =
492 ((aa->aa_flags & AFA_PHASE2) ? 2:1);
495 * step through the nets in the range
496 * starting at the (possibly random) start point.
498 for (i = nnets, netinc = 1; i > 0; net =
499 ntohs(nr.nr_firstnet) + ((net - ntohs(nr.nr_firstnet) +
500 netinc) % nnets), i--) {
501 AA_SAT(aa)->sat_addr.s_net = htons(net);
504 * using a rather strange stepping method,
505 * stagger through the possible node addresses
506 * Once again, starting at the (possibly random)
507 * initial node address.
509 for (j = 0, nodeinc = time_second | 1; j < 256;
510 j++, AA_SAT(aa)->sat_addr.s_node += nodeinc) {
511 if (AA_SAT(aa)->sat_addr.s_node > 253 ||
512 AA_SAT(aa)->sat_addr.s_node < 1)
517 * start off the probes as an asynchronous
518 * activity. though why wait 200mSec?
520 aa->aa_ch = timeout(aarpprobe, (caddr_t)ifp,
522 if (tsleep(aa, PPAUSE|PCATCH, "at_ifinit",
525 * theoretically we shouldn't time
526 * out here so if we returned with an
529 printf("at_ifinit: why did this "
531 aa->aa_addr = oldaddr;
532 aa->aa_firstnet = onr.nr_firstnet;
533 aa->aa_lastnet = onr.nr_lastnet;
538 * The async activity should have woken us
539 * up. We need to see if it was successful
540 * in finding a free spot, or if we need to
541 * iterate to the next address to try.
543 if ((aa->aa_flags & AFA_PROBING) == 0)
548 * of course we need to break out through two loops...
550 if ((aa->aa_flags & AFA_PROBING) == 0)
552 /* reset node for next network */
553 AA_SAT(aa)->sat_addr.s_node = time_second;
557 * if we are still trying to probe, then we have finished all
558 * the possible addresses, so we need to give up
560 if (aa->aa_flags & AFA_PROBING) {
561 aa->aa_addr = oldaddr;
562 aa->aa_firstnet = onr.nr_firstnet;
563 aa->aa_lastnet = onr.nr_lastnet;
569 * Now that we have selected an address, we need to tell the interface
570 * about it, just in case it needs to adjust something.
572 if (ifp->if_ioctl != NULL &&
573 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)aa))) {
575 * of course this could mean that it objects violently
576 * so if it does, we back out again..
578 aa->aa_addr = oldaddr;
579 aa->aa_firstnet = onr.nr_firstnet;
580 aa->aa_lastnet = onr.nr_lastnet;
585 * set up the netmask part of the at_ifaddr
586 * and point the appropriate pointer in the ifaddr to it.
587 * probably pointless, but what the heck.. XXX
589 bzero(&aa->aa_netmask, sizeof(aa->aa_netmask));
590 aa->aa_netmask.sat_len = sizeof(struct sockaddr_at);
591 aa->aa_netmask.sat_family = AF_APPLETALK;
592 aa->aa_netmask.sat_addr.s_net = 0xffff;
593 aa->aa_netmask.sat_addr.s_node = 0;
594 aa->aa_ifa.ifa_netmask =(struct sockaddr *) &(aa->aa_netmask); /* XXX */
597 * Initialize broadcast (or remote p2p) address
599 bzero(&aa->aa_broadaddr, sizeof(aa->aa_broadaddr));
600 aa->aa_broadaddr.sat_len = sizeof(struct sockaddr_at);
601 aa->aa_broadaddr.sat_family = AF_APPLETALK;
603 aa->aa_ifa.ifa_metric = ifp->if_metric;
604 if (ifp->if_flags & IFF_BROADCAST) {
605 aa->aa_broadaddr.sat_addr.s_net = htons(0);
606 aa->aa_broadaddr.sat_addr.s_node = 0xff;
607 aa->aa_ifa.ifa_broadaddr = (struct sockaddr *)
609 /* add the range of routes needed */
610 error = aa_dorangeroute(&aa->aa_ifa, ntohs(aa->aa_firstnet),
611 ntohs(aa->aa_lastnet), RTM_ADD);
612 } else if (ifp->if_flags & IFF_POINTOPOINT) {
613 struct at_addr rtaddr, rtmask;
615 bzero(&rtaddr, sizeof(rtaddr));
616 bzero(&rtmask, sizeof(rtmask));
617 /* fill in the far end if we know it here XXX */
618 aa->aa_ifa.ifa_dstaddr = (struct sockaddr *) &aa->aa_dstaddr;
619 error = aa_addsingleroute(&aa->aa_ifa, &rtaddr, &rtmask);
620 } else if (ifp->if_flags & IFF_LOOPBACK) {
621 struct at_addr rtaddr, rtmask;
623 bzero(&rtaddr, sizeof(rtaddr));
624 bzero(&rtmask, sizeof(rtmask));
625 rtaddr.s_net = AA_SAT(aa)->sat_addr.s_net;
626 rtaddr.s_node = AA_SAT(aa)->sat_addr.s_node;
627 rtmask.s_net = 0xffff;
628 /* XXX should not be so.. should be HOST route */
630 error = aa_addsingleroute(&aa->aa_ifa, &rtaddr, &rtmask);
634 * set the address of our "check if this addr is ours" routine.
636 aa->aa_ifa.ifa_claim_addr = aa_claim_addr;
639 * of course if we can't add these routes we back out, but it's
640 * getting risky by now XXX
644 aa->aa_addr = oldaddr;
645 aa->aa_firstnet = onr.nr_firstnet;
646 aa->aa_lastnet = onr.nr_lastnet;
651 * note that the address has a route associated with it....
653 aa->aa_ifa.ifa_flags |= IFA_ROUTE;
654 aa->aa_flags |= AFA_ROUTE;
659 * check whether a given address is a broadcast address for us..
662 at_broadcast(struct sockaddr_at *sat)
664 struct at_ifaddr *aa;
667 * If the node is not right, it can't be a broadcast
669 if (sat->sat_addr.s_node != ATADDR_BCAST)
673 * If the node was right then if the net is right, it's a broadcast
675 if (sat->sat_addr.s_net == ATADDR_ANYNET)
679 * failing that, if the net is one we have, it's a broadcast as well.
681 for (aa = at_ifaddr_list; aa != NULL; aa = aa->aa_next) {
682 if ((aa->aa_ifp->if_flags & IFF_BROADCAST)
683 && (ntohs(sat->sat_addr.s_net) >= ntohs(aa->aa_firstnet)
684 && ntohs(sat->sat_addr.s_net) <= ntohs(aa->aa_lastnet)))
693 * Add a route for a range of networks from bot to top - 1.
696 * Split the range into two subranges such that the middle
697 * of the two ranges is the point where the highest bit of difference
698 * between the two addresses makes its transition.
699 * Each of the upper and lower ranges might not exist, or might be
700 * representable by 1 or more netmasks. In addition, if both
701 * ranges can be represented by the same netmask, then they can be merged
702 * by using the next higher netmask..
706 aa_dorangeroute(struct ifaddr *ifa, u_int bot, u_int top, int cmd)
714 * slight sanity check
716 if (bot > top) return (EINVAL);
721 * just start out with the lowest boundary
722 * and keep extending the mask till it's too big.
727 while (((bot & ~mask1) >= bot) && ((bot | mask1) <= top)) {
732 mask.s_net = htons(~mask1);
733 addr.s_net = htons(bot);
734 if (cmd == RTM_ADD) {
735 error = aa_addsingleroute(ifa,&addr,&mask);
741 error = aa_delsingleroute(ifa,&addr,&mask);
742 bot = (bot | mask1) + 1;
748 aa_addsingleroute(struct ifaddr *ifa, struct at_addr *addr,
749 struct at_addr *mask)
754 printf("aa_addsingleroute: %x.%x mask %x.%x ...\n",
755 ntohs(addr->s_net), addr->s_node, ntohs(mask->s_net),
759 error = aa_dosingleroute(ifa, addr, mask, RTM_ADD, RTF_UP);
761 printf("aa_addsingleroute: error %d\n", error);
766 aa_delsingleroute(struct ifaddr *ifa, struct at_addr *addr,
767 struct at_addr *mask)
771 error = aa_dosingleroute(ifa, addr, mask, RTM_DELETE, 0);
773 printf("aa_delsingleroute: error %d\n", error);
778 aa_dosingleroute(struct ifaddr *ifa, struct at_addr *at_addr,
779 struct at_addr *at_mask, int cmd, int flags)
781 struct sockaddr_at addr, mask;
783 bzero(&addr, sizeof(addr));
784 bzero(&mask, sizeof(mask));
785 addr.sat_family = AF_APPLETALK;
786 addr.sat_len = sizeof(struct sockaddr_at);
787 addr.sat_addr.s_net = at_addr->s_net;
788 addr.sat_addr.s_node = at_addr->s_node;
789 mask.sat_family = AF_APPLETALK;
790 mask.sat_len = sizeof(struct sockaddr_at);
791 mask.sat_addr.s_net = at_mask->s_net;
792 mask.sat_addr.s_node = at_mask->s_node;
795 return (rtrequest(cmd, (struct sockaddr *) &addr,
796 (flags & RTF_HOST)?(ifa->ifa_dstaddr):(ifa->ifa_addr),
797 (struct sockaddr *) &mask, flags, NULL));
805 struct at_ifaddr *aa;
809 while ((aa = at_ifaddr_list) != NULL) {
812 at_ifaddr_list = aa->aa_next;
813 if ((ifa = ifp->if_addrlist) == (struct ifaddr *)aa)
814 ifp->if_addrlist = ifa->ifa_next;
816 while (ifa->ifa_next &&
817 (ifa->ifa_next != (struct ifaddr *)aa))
821 ((struct ifaddr *)aa)->ifa_next;
831 aa_claim_addr(struct ifaddr *ifa, struct sockaddr *gw0)
833 struct sockaddr_at *addr = (struct sockaddr_at *)ifa->ifa_addr;
834 struct sockaddr_at *gw = (struct sockaddr_at *)gw0;
836 switch (gw->sat_range.r_netrange.nr_phase) {
838 if(addr->sat_range.r_netrange.nr_phase == 1)
844 * if it's our net (including 0),
845 * or netranges are valid, and we are in the range,
848 if ((addr->sat_addr.s_net == gw->sat_addr.s_net)
849 || ((addr->sat_range.r_netrange.nr_lastnet)
850 && (ntohs(gw->sat_addr.s_net) >=
851 ntohs(addr->sat_range.r_netrange.nr_firstnet))
852 && (ntohs(gw->sat_addr.s_net) <=
853 ntohs(addr->sat_range.r_netrange.nr_lastnet))))
857 printf("atalk: bad phase\n");