2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)if.c 8.5 (Berkeley) 1/9/95
37 #include "opt_compat.h"
38 #include "opt_inet6.h"
41 #include <sys/param.h>
43 #include <sys/malloc.h>
46 #include <sys/systm.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/protosw.h>
51 #include <sys/kernel.h>
52 #include <sys/sockio.h>
53 #include <sys/syslog.h>
54 #include <sys/sysctl.h>
58 #include <net/if_arp.h>
59 #include <net/if_dl.h>
60 #include <net/if_types.h>
61 #include <net/if_var.h>
62 #include <net/radix.h>
63 #include <net/route.h>
65 #if defined(INET) || defined(INET6)
67 #include <netinet/in.h>
68 #include <netinet/in_var.h>
70 #include <netinet6/in6_var.h>
71 #include <netinet6/in6_ifattach.h>
75 static int ifconf(u_long, caddr_t);
76 static void if_grow(void);
77 static void if_init(void *);
78 static void if_check(void *);
79 static int if_findindex(struct ifnet *);
80 static void if_qflush(struct ifqueue *);
81 static void if_slowtimo(void *);
82 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
83 static int if_rtdel(struct radix_node *, void *);
84 static struct if_clone *if_clone_lookup(const char *, int *);
85 static int if_clone_list(struct if_clonereq *);
86 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
89 * XXX: declare here to avoid to include many inet6 related files..
90 * should be more generalized?
92 extern void nd6_setmtu __P((struct ifnet *));
96 struct ifindex_entry *ifindex_table = NULL;
97 int ifqmaxlen = IFQ_MAXLEN;
98 struct ifnethead ifnet; /* depend on static init XXX */
100 LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
102 static int if_indexlim = 8;
103 static struct klist ifklist;
105 static void filt_netdetach(struct knote *kn);
106 static int filt_netdev(struct knote *kn, long hint);
108 static struct filterops netdev_filtops =
109 { 1, NULL, filt_netdetach, filt_netdev };
112 * System initialization
114 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL)
115 SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL)
117 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
118 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
120 #define CDEV_MAJOR 165
122 static d_open_t netopen;
123 static d_close_t netclose;
124 static d_ioctl_t netioctl;
125 static d_kqfilter_t netkqfilter;
127 static struct cdevsw net_cdevsw = {
129 /* close */ netclose,
132 /* ioctl */ netioctl,
135 /* strategy */ nostrategy,
137 /* maj */ CDEV_MAJOR,
140 /* flags */ D_KQFILTER,
141 /* kqfilter */ netkqfilter,
145 netopen(dev_t dev, int flag, int mode, struct thread *td)
151 netclose(dev_t dev, int flags, int fmt, struct thread *td)
157 netioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
162 /* only support interface specific ioctls */
163 if (IOCGROUP(cmd) != 'i')
168 * special network device, not interface.
170 if (cmd == SIOCGIFCONF)
171 return (ifconf(cmd, data)); /* XXX remove cmd */
175 ifp = ifnet_byindex(idx);
179 error = ifhwioctl(cmd, ifp, data, td);
180 if (error == ENOIOCTL)
186 netkqfilter(dev_t dev, struct knote *kn)
196 ifp = ifnet_byindex(idx);
199 klist = &ifp->if_klist;
202 switch (kn->kn_filter) {
204 kn->kn_fop = &netdev_filtops;
210 kn->kn_hook = (caddr_t)klist;
213 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
219 filt_netdetach(struct knote *kn)
221 struct klist *klist = (struct klist *)kn->kn_hook;
223 if (kn->kn_status & KN_DETACHED)
225 SLIST_REMOVE(klist, kn, knote, kn_selnext);
229 filt_netdev(struct knote *kn, long hint)
233 * Currently NOTE_EXIT is abused to indicate device detach.
235 if (hint == NOTE_EXIT) {
236 kn->kn_data = NOTE_LINKINV;
237 kn->kn_status |= KN_DETACHED;
238 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
241 kn->kn_data = hint; /* current status */
242 if (kn->kn_sfflags & hint)
243 kn->kn_fflags |= hint;
244 return (kn->kn_fflags != 0);
248 * Network interface utility routines.
250 * Routines with ifa_ifwith* names take sockaddr *'s as
260 SLIST_INIT(&ifklist);
261 if_grow(); /* create initial table */
262 ifdev_byindex(0) = make_dev(&net_cdevsw, 0,
263 UID_ROOT, GID_WHEEL, 0600, "network");
270 struct ifindex_entry *e;
273 n = if_indexlim * sizeof(*e);
274 e = malloc(n, M_IFADDR, M_WAITOK | M_ZERO);
275 if (ifindex_table != NULL) {
276 memcpy((caddr_t)e, (caddr_t)ifindex_table, n/2);
277 free((caddr_t)ifindex_table, M_IFADDR);
291 TAILQ_FOREACH(ifp, &ifnet, if_link) {
292 if (ifp->if_snd.ifq_maxlen == 0) {
293 printf("%s%d XXX: driver didn't set ifq_maxlen\n",
294 ifp->if_name, ifp->if_unit);
295 ifp->if_snd.ifq_maxlen = ifqmaxlen;
297 if (!mtx_initialized(&ifp->if_snd.ifq_mtx)) {
298 printf("%s%d XXX: driver didn't initialize queue mtx\n",
299 ifp->if_name, ifp->if_unit);
300 mtx_init(&ifp->if_snd.ifq_mtx, "unknown", MTX_DEF);
308 if_findindex(struct ifnet *ifp)
311 char eaddr[18], devname[32];
312 const char *name, *p;
314 switch (ifp->if_type) {
315 case IFT_ETHER: /* these types use struct arpcom */
320 snprintf(eaddr, 18, "%6D",
321 ((struct arpcom *)ifp->if_softc)->ac_enaddr, ":");
327 snprintf(devname, 32, "%s%d", ifp->if_name, ifp->if_unit);
328 name = net_cdevsw.d_name;
330 while ((resource_find_dev(&i, name, &unit, NULL, NULL)) == 0) {
331 if (resource_string_value(name, unit, "ether", &p) == 0)
332 if (strcmp(p, eaddr) == 0)
334 if (resource_string_value(name, unit, "dev", &p) == 0)
335 if (strcmp(p, devname) == 0)
341 if (ifaddr_byindex(unit) == NULL)
343 printf("%s%d in use, cannot hardwire it to %s.\n",
344 name, unit, devname);
346 for (unit = 1; ; unit++) {
347 if (unit <= if_index && ifaddr_byindex(unit) != NULL)
349 if (resource_string_value(name, unit, "ether", &p) == 0 ||
350 resource_string_value(name, unit, "dev", &p) == 0)
358 * Attach an interface to the
359 * list of "active" interfaces.
365 unsigned socksize, ifasize;
366 int namelen, masklen;
368 register struct sockaddr_dl *sdl;
369 register struct ifaddr *ifa;
371 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
374 * The old code would work if the interface passed a pre-existing
375 * chain of ifaddrs to this code. We don't trust our callers to
376 * properly initialize the tailq, however, so we no longer allow
377 * this unlikely case.
379 TAILQ_INIT(&ifp->if_addrhead);
380 TAILQ_INIT(&ifp->if_prefixhead);
381 TAILQ_INIT(&ifp->if_multiaddrs);
382 SLIST_INIT(&ifp->if_klist);
383 getmicrotime(&ifp->if_lastchange);
384 ifp->if_index = if_findindex(ifp);
385 if (ifp->if_index > if_index)
386 if_index = ifp->if_index;
387 if (if_index >= if_indexlim)
390 ifnet_byindex(ifp->if_index) = ifp;
391 ifdev_byindex(ifp->if_index) = make_dev(&net_cdevsw, ifp->if_index,
392 UID_ROOT, GID_WHEEL, 0600, "%s/%s%d",
393 net_cdevsw.d_name, ifp->if_name, ifp->if_unit);
394 make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d",
395 net_cdevsw.d_name, ifp->if_index);
397 mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_name, MTX_DEF);
400 * create a Link Level name for this device
402 namelen = snprintf(workbuf, sizeof(workbuf),
403 "%s%d", ifp->if_name, ifp->if_unit);
404 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
405 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
406 socksize = masklen + ifp->if_addrlen;
407 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
408 if (socksize < sizeof(*sdl))
409 socksize = sizeof(*sdl);
410 socksize = ROUNDUP(socksize);
411 ifasize = sizeof(*ifa) + 2 * socksize;
412 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
414 sdl = (struct sockaddr_dl *)(ifa + 1);
415 sdl->sdl_len = socksize;
416 sdl->sdl_family = AF_LINK;
417 bcopy(workbuf, sdl->sdl_data, namelen);
418 sdl->sdl_nlen = namelen;
419 sdl->sdl_index = ifp->if_index;
420 sdl->sdl_type = ifp->if_type;
421 ifaddr_byindex(ifp->if_index) = ifa;
423 ifa->ifa_rtrequest = link_rtrequest;
424 ifa->ifa_addr = (struct sockaddr *)sdl;
425 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
426 ifa->ifa_netmask = (struct sockaddr *)sdl;
427 sdl->sdl_len = masklen;
429 sdl->sdl_data[--namelen] = 0xff;
430 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
432 ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
436 * Detach an interface, removing it from the
437 * list of "active" interfaces.
444 struct radix_node_head *rnh;
449 * Remove routes and flush queues.
455 * Remove address from ifindex_table[] and maybe decrement if_index.
456 * Clean up all addresses.
458 ifaddr_byindex(ifp->if_index) = NULL;
459 destroy_dev(ifdev_byindex(ifp->if_index));
460 ifdev_byindex(ifp->if_index) = NULL;
462 while (if_index > 0 && ifaddr_byindex(if_index) == NULL)
465 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
466 ifa = TAILQ_FIRST(&ifp->if_addrhead)) {
468 /* XXX: Ugly!! ad hoc just for INET */
469 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
470 struct ifaliasreq ifr;
472 bzero(&ifr, sizeof(ifr));
473 ifr.ifra_addr = *ifa->ifa_addr;
474 if (ifa->ifa_dstaddr)
475 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
476 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
482 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
484 /* ifp_addrhead is already updated */
488 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
494 * Remove all IPv6 kernel structs related to ifp. This should be done
495 * before removing routing entries below, since IPv6 interface direct
496 * routes are expected to be removed by the IPv6-specific kernel API.
497 * Otherwise, the kernel will detect some inconsistency and bark it.
503 * Delete all remaining routes using this interface
504 * Unfortuneatly the only way to do this is to slog through
505 * the entire routing table looking for routes which point
506 * to this interface...oh well...
508 for (i = 1; i <= AF_MAX; i++) {
509 if ((rnh = rt_tables[i]) == NULL)
511 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
514 KNOTE(&ifp->if_klist, NOTE_EXIT);
515 TAILQ_REMOVE(&ifnet, ifp, if_link);
516 mtx_destroy(&ifp->if_snd.ifq_mtx);
521 * Delete Routes for a Network Interface
523 * Called for each routing entry via the rnh->rnh_walktree() call above
524 * to delete all route entries referencing a detaching network interface.
527 * rn pointer to node in the routing table
528 * arg argument passed to rnh->rnh_walktree() - detaching interface
532 * errno failed - reason indicated
537 struct radix_node *rn;
540 struct rtentry *rt = (struct rtentry *)rn;
541 struct ifnet *ifp = arg;
544 if (rt->rt_ifp == ifp) {
547 * Protect (sorta) against walktree recursion problems
550 if ((rt->rt_flags & RTF_UP) == 0)
553 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
554 rt_mask(rt), rt->rt_flags,
555 (struct rtentry **) NULL);
557 log(LOG_WARNING, "if_rtdel: error %d\n", err);
565 * Create a clone network interface.
568 if_clone_create(name, len)
572 struct if_clone *ifc;
578 ifc = if_clone_lookup(name, &unit);
582 if (ifunit(name) != NULL)
585 wildcard = (unit < 0);
587 err = (*ifc->ifc_create)(ifc, &unit);
591 /* In the wildcard case, we need to update the name. */
593 for (dp = name; *dp != '\0'; dp++);
594 if (snprintf(dp, len - (dp-name), "%d", unit) >
595 len - (dp-name) - 1) {
597 * This can only be a programmer error and
598 * there's no straightforward way to recover if
601 panic("if_clone_create(): interface name too long");
610 * Destroy a clone network interface.
613 if_clone_destroy(name)
616 struct if_clone *ifc;
619 ifc = if_clone_lookup(name, NULL);
627 if (ifc->ifc_destroy == NULL)
630 (*ifc->ifc_destroy)(ifp);
635 * Look up a network interface cloner.
637 static struct if_clone *
638 if_clone_lookup(name, unitp)
642 struct if_clone *ifc;
646 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) {
647 for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) {
648 if (ifc->ifc_name[i] != *cp)
653 ifc = LIST_NEXT(ifc, ifc_list);
657 return ((struct if_clone *)NULL);
663 for (i = 0; *cp != '\0'; cp++) {
664 if (*cp < '0' || *cp > '9') {
665 /* Bogus unit number. */
668 i = (i * 10) + (*cp - '0');
678 * Register a network interface cloner.
682 struct if_clone *ifc;
685 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
690 * Unregister a network interface cloner.
694 struct if_clone *ifc;
697 LIST_REMOVE(ifc, ifc_list);
702 * Provide list of interface cloners to userspace.
706 struct if_clonereq *ifcr;
708 char outbuf[IFNAMSIZ], *dst;
709 struct if_clone *ifc;
710 int count, error = 0;
712 ifcr->ifcr_total = if_cloners_count;
713 if ((dst = ifcr->ifcr_buffer) == NULL) {
714 /* Just asking how many there are. */
718 if (ifcr->ifcr_count < 0)
721 count = (if_cloners_count < ifcr->ifcr_count) ?
722 if_cloners_count : ifcr->ifcr_count;
724 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
725 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
726 strncpy(outbuf, ifc->ifc_name, IFNAMSIZ);
727 outbuf[IFNAMSIZ - 1] = '\0'; /* sanity */
728 error = copyout(outbuf, dst, IFNAMSIZ);
737 * Locate an interface based on a complete address.
742 struct sockaddr *addr;
747 #define equal(a1, a2) \
748 (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
749 TAILQ_FOREACH(ifp, &ifnet, if_link)
750 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
751 if (ifa->ifa_addr->sa_family != addr->sa_family)
753 if (equal(addr, ifa->ifa_addr))
755 /* IP6 doesn't have broadcast */
756 if ((ifp->if_flags & IFF_BROADCAST) &&
757 ifa->ifa_broadaddr &&
758 ifa->ifa_broadaddr->sa_len != 0 &&
759 equal(ifa->ifa_broadaddr, addr))
768 * Locate the point to point interface with a given destination address.
772 ifa_ifwithdstaddr(addr)
773 struct sockaddr *addr;
778 TAILQ_FOREACH(ifp, &ifnet, if_link) {
779 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
781 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
782 if (ifa->ifa_addr->sa_family != addr->sa_family)
784 if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
794 * Find an interface on a specific network. If many, choice
795 * is most specific found.
799 struct sockaddr *addr;
801 register struct ifnet *ifp;
802 register struct ifaddr *ifa;
803 struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
804 u_int af = addr->sa_family;
805 char *addr_data = addr->sa_data, *cplim;
808 * AF_LINK addresses can be looked up directly by their index number,
809 * so do that if we can.
812 register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
813 if (sdl->sdl_index && sdl->sdl_index <= if_index)
814 return (ifaddr_byindex(sdl->sdl_index));
818 * Scan though each interface, looking for ones that have
819 * addresses in this address family.
821 TAILQ_FOREACH(ifp, &ifnet, if_link) {
822 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
823 register char *cp, *cp2, *cp3;
825 if (ifa->ifa_addr->sa_family != af)
828 #ifdef INET6 /* XXX: for maching gif tunnel dst as routing entry gateway */
829 addr->sa_family != AF_INET6 &&
831 ifp->if_flags & IFF_POINTOPOINT) {
833 * This is a bit broken as it doesn't
834 * take into account that the remote end may
835 * be a single node in the network we are
837 * The trouble is that we don't know the
838 * netmask for the remote end.
840 if (ifa->ifa_dstaddr != 0
841 && equal(addr, ifa->ifa_dstaddr))
845 * if we have a special address handler,
846 * then use it instead of the generic one.
848 if (ifa->ifa_claim_addr) {
849 if ((*ifa->ifa_claim_addr)(ifa, addr))
855 * Scan all the bits in the ifa's address.
856 * If a bit dissagrees with what we are
857 * looking for, mask it with the netmask
858 * to see if it really matters.
861 if (ifa->ifa_netmask == 0)
864 cp2 = ifa->ifa_addr->sa_data;
865 cp3 = ifa->ifa_netmask->sa_data;
866 cplim = ifa->ifa_netmask->sa_len
867 + (char *)ifa->ifa_netmask;
869 if ((*cp++ ^ *cp2++) & *cp3++)
870 goto next; /* next address! */
872 * If the netmask of what we just found
873 * is more specific than what we had before
874 * (if we had one) then remember the new one
875 * before continuing to search
876 * for an even better one.
878 if (ifa_maybe == 0 ||
879 rn_refines((caddr_t)ifa->ifa_netmask,
880 (caddr_t)ifa_maybe->ifa_netmask))
891 * Find an interface address specific to an interface best matching
895 ifaof_ifpforaddr(addr, ifp)
896 struct sockaddr *addr;
897 register struct ifnet *ifp;
899 register struct ifaddr *ifa;
900 register char *cp, *cp2, *cp3;
901 register char *cplim;
902 struct ifaddr *ifa_maybe = 0;
903 u_int af = addr->sa_family;
907 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
908 if (ifa->ifa_addr->sa_family != af)
912 if (ifa->ifa_netmask == 0) {
913 if (equal(addr, ifa->ifa_addr) ||
914 (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
918 if (ifp->if_flags & IFF_POINTOPOINT) {
919 if (equal(addr, ifa->ifa_dstaddr))
923 cp2 = ifa->ifa_addr->sa_data;
924 cp3 = ifa->ifa_netmask->sa_data;
925 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
926 for (; cp3 < cplim; cp3++)
927 if ((*cp++ ^ *cp2++) & *cp3)
938 #include <net/route.h>
941 * Default action when installing a route with a Link Level gateway.
942 * Lookup an appropriate real ifa to point to.
943 * This should be moved to /sys/net/link.c eventually.
946 link_rtrequest(cmd, rt, info)
948 register struct rtentry *rt;
949 struct rt_addrinfo *info;
951 register struct ifaddr *ifa;
952 struct sockaddr *dst;
955 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
956 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
958 ifa = ifaof_ifpforaddr(dst, ifp);
963 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
964 ifa->ifa_rtrequest(cmd, rt, info);
969 * Mark an interface down and notify protocols of
971 * NOTE: must be called at splnet or eqivalent.
974 if_unroute(ifp, flag, fam)
975 register struct ifnet *ifp;
978 register struct ifaddr *ifa;
980 ifp->if_flags &= ~flag;
981 getmicrotime(&ifp->if_lastchange);
982 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
983 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
984 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
985 if_qflush(&ifp->if_snd);
990 * Mark an interface up and notify protocols of
992 * NOTE: must be called at splnet or eqivalent.
995 if_route(ifp, flag, fam)
996 register struct ifnet *ifp;
999 register struct ifaddr *ifa;
1001 ifp->if_flags |= flag;
1002 getmicrotime(&ifp->if_lastchange);
1003 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1004 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1005 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1013 * Mark an interface down and notify protocols of
1015 * NOTE: must be called at splnet or eqivalent.
1019 register struct ifnet *ifp;
1022 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1026 * Mark an interface up and notify protocols of
1028 * NOTE: must be called at splnet or eqivalent.
1032 register struct ifnet *ifp;
1035 if_route(ifp, IFF_UP, AF_UNSPEC);
1039 * Flush an interface queue.
1043 register struct ifqueue *ifq;
1045 register struct mbuf *m, *n;
1048 while ((m = n) != 0) {
1058 * Handle interface watchdog timer routines. Called
1059 * from softclock, we decrement timers (if set) and
1060 * call the appropriate interface routine on expiration.
1066 register struct ifnet *ifp;
1069 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1070 if (ifp->if_timer == 0 || --ifp->if_timer)
1072 if (ifp->if_watchdog)
1073 (*ifp->if_watchdog)(ifp);
1076 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
1080 * Map interface name to
1081 * interface structure pointer.
1084 ifunit(const char *name)
1086 char namebuf[IFNAMSIZ + 1];
1091 * Now search all the interfaces for this name/number
1096 * Devices should really be known as /dev/fooN, not /dev/net/fooN.
1098 snprintf(namebuf, IFNAMSIZ, "%s/%s", net_cdevsw.d_name, name);
1099 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1100 dev = ifdev_byindex(ifp->if_index);
1101 if (strcmp(devtoname(dev), namebuf) == 0)
1103 if (dev_named(dev, name))
1110 * Map interface name in a sockaddr_dl to
1111 * interface structure pointer.
1115 struct sockaddr *sa;
1117 char ifname[IFNAMSIZ+1];
1118 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
1120 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
1121 (sdl->sdl_nlen > IFNAMSIZ) )
1125 * ifunit wants a null-terminated name. It may not be null-terminated
1126 * in the sockaddr. We don't want to change the caller's sockaddr,
1127 * and there might not be room to put the trailing null anyway, so we
1128 * make a local copy that we know we can null terminate safely.
1131 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
1132 ifname[sdl->sdl_nlen] = '\0';
1133 return ifunit(ifname);
1137 * Hardware specific interface ioctls.
1140 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
1146 ifr = (struct ifreq *)data;
1149 ifr->ifr_index = ifp->if_index;
1153 ifr->ifr_flags = ifp->if_flags;
1157 ifr->ifr_reqcap = ifp->if_capabilities;
1158 ifr->ifr_curcap = ifp->if_capenable;
1162 ifr->ifr_metric = ifp->if_metric;
1166 ifr->ifr_mtu = ifp->if_mtu;
1170 ifr->ifr_phys = ifp->if_physical;
1174 error = suser_td(td);
1177 ifr->ifr_prevflags = ifp->if_flags;
1178 if (ifp->if_flags & IFF_SMART) {
1179 /* Smart drivers twiddle their own routes */
1180 } else if (ifp->if_flags & IFF_UP &&
1181 (ifr->ifr_flags & IFF_UP) == 0) {
1185 } else if (ifr->ifr_flags & IFF_UP &&
1186 (ifp->if_flags & IFF_UP) == 0) {
1191 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1192 (ifr->ifr_flags &~ IFF_CANTCHANGE);
1194 (void) (*ifp->if_ioctl)(ifp, cmd, data);
1195 getmicrotime(&ifp->if_lastchange);
1199 error = suser_td(td);
1202 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1204 (void) (*ifp->if_ioctl)(ifp, cmd, data);
1208 error = suser_td(td);
1211 ifp->if_metric = ifr->ifr_metric;
1212 getmicrotime(&ifp->if_lastchange);
1216 error = suser_td(td);
1221 error = (*ifp->if_ioctl)(ifp, cmd, data);
1223 getmicrotime(&ifp->if_lastchange);
1228 u_long oldmtu = ifp->if_mtu;
1230 error = suser_td(td);
1233 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1235 if (ifp->if_ioctl == NULL)
1236 return (EOPNOTSUPP);
1237 error = (*ifp->if_ioctl)(ifp, cmd, data);
1239 getmicrotime(&ifp->if_lastchange);
1243 * If the link MTU changed, do network layer specific procedure.
1245 if (ifp->if_mtu != oldmtu) {
1255 error = suser_td(td);
1259 /* Don't allow group membership on non-multicast interfaces. */
1260 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1261 return (EOPNOTSUPP);
1263 /* Don't let users screw up protocols' entries. */
1264 if (ifr->ifr_addr.sa_family != AF_LINK)
1267 if (cmd == SIOCADDMULTI) {
1268 struct ifmultiaddr *ifma;
1269 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1271 error = if_delmulti(ifp, &ifr->ifr_addr);
1274 getmicrotime(&ifp->if_lastchange);
1277 case SIOCSIFPHYADDR:
1278 case SIOCDIFPHYADDR:
1280 case SIOCSIFPHYADDR_IN6:
1282 case SIOCSLIFPHYADDR:
1284 case SIOCSIFGENERIC:
1285 error = suser_td(td);
1288 if (ifp->if_ioctl == NULL)
1289 return (EOPNOTSUPP);
1290 error = (*ifp->if_ioctl)(ifp, cmd, data);
1292 getmicrotime(&ifp->if_lastchange);
1296 ifs = (struct ifstat *)data;
1297 ifs->ascii[0] = '\0';
1299 case SIOCGIFPSRCADDR:
1300 case SIOCGIFPDSTADDR:
1301 case SIOCGLIFPHYADDR:
1303 case SIOCGIFGENERIC:
1304 if (ifp->if_ioctl == 0)
1305 return (EOPNOTSUPP);
1306 error = (*ifp->if_ioctl)(ifp, cmd, data);
1310 error = suser_td(td);
1313 error = if_setlladdr(ifp,
1314 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1328 ifioctl(so, cmd, data, td)
1342 return (ifconf(cmd, data));
1344 ifr = (struct ifreq *)data;
1349 if ((error = suser_td(td)) != 0)
1351 return ((cmd == SIOCIFCREATE) ?
1352 if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
1353 if_clone_destroy(ifr->ifr_name));
1355 case SIOCIFGCLONERS:
1356 return (if_clone_list((struct if_clonereq *)data));
1359 ifp = ifunit(ifr->ifr_name);
1363 error = ifhwioctl(cmd, ifp, data, td);
1364 if (error != ENOIOCTL)
1367 oif_flags = ifp->if_flags;
1368 if (so->so_proto == 0)
1369 return (EOPNOTSUPP);
1371 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
1380 case SIOCSIFDSTADDR:
1382 case SIOCSIFBRDADDR:
1383 case SIOCSIFNETMASK:
1384 #if BYTE_ORDER != BIG_ENDIAN
1385 if (ifr->ifr_addr.sa_family == 0 &&
1386 ifr->ifr_addr.sa_len < 16) {
1387 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1388 ifr->ifr_addr.sa_len = 16;
1391 if (ifr->ifr_addr.sa_len == 0)
1392 ifr->ifr_addr.sa_len = 16;
1400 case OSIOCGIFDSTADDR:
1401 cmd = SIOCGIFDSTADDR;
1404 case OSIOCGIFBRDADDR:
1405 cmd = SIOCGIFBRDADDR;
1408 case OSIOCGIFNETMASK:
1409 cmd = SIOCGIFNETMASK;
1411 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
1418 case OSIOCGIFDSTADDR:
1419 case OSIOCGIFBRDADDR:
1420 case OSIOCGIFNETMASK:
1421 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1425 #endif /* COMPAT_43 */
1427 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1429 DELAY(100);/* XXX: temporal workaround for fxp issue*/
1430 if (ifp->if_flags & IFF_UP) {
1441 * Set/clear promiscuous mode on interface ifp based on the truth value
1442 * of pswitch. The calls are reference counted so that only the first
1443 * "on" request actually has an effect, as does the final "off" request.
1444 * Results are undefined if the "off" and "on" requests are not matched.
1447 ifpromisc(ifp, pswitch)
1453 int oldflags, oldpcount;
1455 oldpcount = ifp->if_pcount;
1456 oldflags = ifp->if_flags;
1459 * If the device is not configured up, we cannot put it in
1462 if ((ifp->if_flags & IFF_UP) == 0)
1464 if (ifp->if_pcount++ != 0)
1466 ifp->if_flags |= IFF_PROMISC;
1468 if (--ifp->if_pcount > 0)
1470 ifp->if_flags &= ~IFF_PROMISC;
1472 ifr.ifr_flags = ifp->if_flags;
1473 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1475 log(LOG_INFO, "%s%d: promiscuous mode %s\n",
1476 ifp->if_name, ifp->if_unit,
1477 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
1480 ifp->if_pcount = oldpcount;
1481 ifp->if_flags = oldflags;
1487 * Return interface configuration
1488 * of system. List may be used
1489 * in later ioctl's (above) to get
1490 * other information.
1498 struct ifconf *ifc = (struct ifconf *)data;
1501 struct ifreq ifr, *ifrp;
1502 int space = ifc->ifc_len, error = 0;
1504 ifrp = ifc->ifc_req;
1505 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1509 if (space < sizeof(ifr))
1511 ifnlen = snprintf(workbuf, sizeof(workbuf),
1512 "%s%d", ifp->if_name, ifp->if_unit);
1513 if(ifnlen + 1 > sizeof ifr.ifr_name) {
1514 error = ENAMETOOLONG;
1517 strcpy(ifr.ifr_name, workbuf);
1521 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1522 struct sockaddr *sa = ifa->ifa_addr;
1524 if (space < sizeof(ifr))
1526 if (jailed(curproc->p_ucred) &&
1527 prison_if(curproc->p_ucred, sa))
1531 if (cmd == OSIOCGIFCONF) {
1532 struct osockaddr *osa =
1533 (struct osockaddr *)&ifr.ifr_addr;
1535 osa->sa_family = sa->sa_family;
1536 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1541 if (sa->sa_len <= sizeof(*sa)) {
1543 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1547 if (space < sizeof (ifr) + sa->sa_len -
1550 space -= sa->sa_len - sizeof(*sa);
1551 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1552 sizeof (ifr.ifr_name));
1554 error = copyout((caddr_t)sa,
1555 (caddr_t)&ifrp->ifr_addr, sa->sa_len);
1556 ifrp = (struct ifreq *)
1557 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1561 space -= sizeof (ifr);
1566 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
1567 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1571 space -= sizeof (ifr);
1575 ifc->ifc_len -= space;
1580 * Just like if_promisc(), but for all-multicast-reception mode.
1583 if_allmulti(ifp, onswitch)
1591 if (ifp->if_amcount++ == 0) {
1592 ifp->if_flags |= IFF_ALLMULTI;
1593 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
1596 if (ifp->if_amcount > 1) {
1599 ifp->if_amcount = 0;
1600 ifp->if_flags &= ~IFF_ALLMULTI;
1601 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
1612 * Add a multicast listenership to the interface in question.
1613 * The link layer provides a routine which converts
1616 if_addmulti(ifp, sa, retifma)
1617 struct ifnet *ifp; /* interface to manipulate */
1618 struct sockaddr *sa; /* address to add */
1619 struct ifmultiaddr **retifma;
1621 struct sockaddr *llsa, *dupsa;
1623 struct ifmultiaddr *ifma;
1626 * If the matching multicast address already exists
1627 * then don't add a new one, just add a reference
1629 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1630 if (equal(sa, ifma->ifma_addr)) {
1631 ifma->ifma_refcount++;
1639 * Give the link layer a chance to accept/reject it, and also
1640 * find out which AF_LINK address this maps to, if it isn't one
1643 if (ifp->if_resolvemulti) {
1644 error = ifp->if_resolvemulti(ifp, &llsa, sa);
1645 if (error) return error;
1650 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1651 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1652 bcopy(sa, dupsa, sa->sa_len);
1654 ifma->ifma_addr = dupsa;
1655 ifma->ifma_lladdr = llsa;
1656 ifma->ifma_ifp = ifp;
1657 ifma->ifma_refcount = 1;
1658 ifma->ifma_protospec = 0;
1659 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1662 * Some network interfaces can scan the address list at
1663 * interrupt time; lock them out.
1666 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1671 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1672 if (equal(ifma->ifma_addr, llsa))
1676 ifma->ifma_refcount++;
1678 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1679 M_IFMADDR, M_WAITOK);
1680 MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1681 M_IFMADDR, M_WAITOK);
1682 bcopy(llsa, dupsa, llsa->sa_len);
1683 ifma->ifma_addr = dupsa;
1684 ifma->ifma_ifp = ifp;
1685 ifma->ifma_refcount = 1;
1687 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1692 * We are certain we have added something, so call down to the
1693 * interface to let them know about it.
1696 ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
1703 * Remove a reference to a multicast address on this interface. Yell
1704 * if the request does not match an existing membership.
1707 if_delmulti(ifp, sa)
1709 struct sockaddr *sa;
1711 struct ifmultiaddr *ifma;
1714 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1715 if (equal(sa, ifma->ifma_addr))
1720 if (ifma->ifma_refcount > 1) {
1721 ifma->ifma_refcount--;
1725 rt_newmaddrmsg(RTM_DELMADDR, ifma);
1726 sa = ifma->ifma_lladdr;
1728 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
1730 * Make sure the interface driver is notified
1731 * in the case of a link layer mcast group being left.
1733 if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0)
1734 ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1736 free(ifma->ifma_addr, M_IFMADDR);
1737 free(ifma, M_IFMADDR);
1742 * Now look for the link-layer address which corresponds to
1743 * this network address. It had been squirreled away in
1744 * ifma->ifma_lladdr for this purpose (so we don't have
1745 * to call ifp->if_resolvemulti() again), and we saved that
1746 * value in sa above. If some nasty deleted the
1747 * link-layer address out from underneath us, we can deal because
1748 * the address we stored was is not the same as the one which was
1749 * in the record for the link-layer address. (So we don't complain
1752 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1753 if (equal(sa, ifma->ifma_addr))
1758 if (ifma->ifma_refcount > 1) {
1759 ifma->ifma_refcount--;
1764 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
1765 ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1767 free(ifma->ifma_addr, M_IFMADDR);
1768 free(sa, M_IFMADDR);
1769 free(ifma, M_IFMADDR);
1775 * Set the link layer address on an interface.
1777 * At this time we only support certain types of interfaces,
1778 * and we don't allow the length of the address to change.
1781 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
1783 struct sockaddr_dl *sdl;
1786 ifa = ifaddr_byindex(ifp->if_index);
1789 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1792 if (len != sdl->sdl_alen) /* don't allow length to change */
1794 switch (ifp->if_type) {
1795 case IFT_ETHER: /* these types use struct arpcom */
1800 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
1801 bcopy(lladdr, LLADDR(sdl), len);
1807 * If the interface is already up, we need
1808 * to re-init it in order to reprogram its
1811 if ((ifp->if_flags & IFF_UP) != 0) {
1812 ifp->if_flags &= ~IFF_UP;
1813 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, NULL);
1814 ifp->if_flags |= IFF_UP;
1815 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, NULL);
1820 struct ifmultiaddr *
1821 ifmaof_ifpforaddr(sa, ifp)
1822 struct sockaddr *sa;
1825 struct ifmultiaddr *ifma;
1827 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1828 if (equal(ifma->ifma_addr, sa))
1834 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
1835 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");