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.3 (Berkeley) 1/4/94
34 * $FreeBSD: src/sys/net/if.c,v 1.185 2004/03/13 02:35:03 brooks Exp $
35 * $DragonFly: src/sys/net/if.c,v 1.23 2004/12/21 02:54:14 hsu Exp $
38 #include "opt_compat.h"
39 #include "opt_inet6.h"
42 #include <sys/param.h>
43 #include <sys/malloc.h>
45 #include <sys/systm.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/socketops.h>
51 #include <sys/protosw.h>
52 #include <sys/kernel.h>
53 #include <sys/sockio.h>
54 #include <sys/syslog.h>
55 #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>
64 #include <machine/stdarg.h>
66 #if defined(INET) || defined(INET6)
68 #include <netinet/in.h>
69 #include <netinet/in_var.h>
70 #include <netinet/if_ether.h>
72 #include <machine/clock.h> /* XXX: temporal workaround for fxp issue */
73 #include <netinet6/in6_var.h>
74 #include <netinet6/in6_ifattach.h>
78 #if defined(COMPAT_43)
79 #include <emulation/43bsd/43bsd_socket.h>
80 #endif /* COMPAT_43 */
83 * System initialization
86 static int ifconf (u_long
, caddr_t
, struct thread
*);
87 static void ifinit (void *);
88 static void if_qflush (struct ifqueue
*);
89 static void if_slowtimo (void *);
90 static void link_rtrequest (int, struct rtentry
*, struct rt_addrinfo
*);
91 static int if_rtdel (struct radix_node
*, void *);
93 SYSINIT(interfaces
, SI_SUB_PROTO_IF
, SI_ORDER_FIRST
, ifinit
, NULL
)
95 MALLOC_DEFINE(M_IFADDR
, "ifaddr", "interface address");
96 MALLOC_DEFINE(M_IFMADDR
, "ether_multi", "link-level multicast address");
97 MALLOC_DEFINE(M_CLONE
, "clone", "interface cloning framework");
99 int ifqmaxlen
= IFQ_MAXLEN
;
100 struct ifnethead ifnet
; /* depend on static init XXX */
104 * XXX: declare here to avoid to include many inet6 related files..
105 * should be more generalized?
107 extern void nd6_setmtu (struct ifnet
*);
110 struct if_clone
*if_clone_lookup (const char *, int *);
111 int if_clone_list (struct if_clonereq
*);
113 LIST_HEAD(, if_clone
) if_cloners
= LIST_HEAD_INITIALIZER(if_cloners
);
114 int if_cloners_count
;
116 struct callout if_slowtimo_timer
;
119 * Network interface utility routines.
121 * Routines with ifa_ifwith* names take sockaddr *'s as
131 callout_init(&if_slowtimo_timer
);
134 TAILQ_FOREACH(ifp
, &ifnet
, if_link
) {
135 if (ifp
->if_snd
.ifq_maxlen
== 0) {
136 if_printf(ifp
, "XXX: driver didn't set ifq_maxlen\n");
137 ifp
->if_snd
.ifq_maxlen
= ifqmaxlen
;
146 struct ifaddr
**ifnet_addrs
;
147 struct ifnet
**ifindex2ifnet
= NULL
;
150 * Attach an interface to the
151 * list of "active" interfaces.
154 if_attach(struct ifnet
*ifp
)
156 unsigned socksize
, ifasize
;
157 int namelen
, masklen
;
158 struct sockaddr_dl
*sdl
;
160 static int if_indexlim
= 8;
168 TAILQ_INSERT_TAIL(&ifnet
, ifp
, if_link
);
169 ifp
->if_index
= ++if_index
;
172 * The old code would work if the interface passed a pre-existing
173 * chain of ifaddrs to this code. We don't trust our callers to
174 * properly initialize the tailq, however, so we no longer allow
175 * this unlikely case.
177 TAILQ_INIT(&ifp
->if_addrhead
);
178 TAILQ_INIT(&ifp
->if_prefixhead
);
179 LIST_INIT(&ifp
->if_multiaddrs
);
180 getmicrotime(&ifp
->if_lastchange
);
181 if (ifnet_addrs
== NULL
|| if_index
>= if_indexlim
) {
182 unsigned n
= (if_indexlim
<<= 1) * sizeof(ifa
);
183 caddr_t q
= malloc(n
, M_IFADDR
, M_WAITOK
);
185 if (ifnet_addrs
!= NULL
) {
186 bcopy(ifnet_addrs
, q
, n
/2);
187 free(ifnet_addrs
, M_IFADDR
);
189 ifnet_addrs
= (struct ifaddr
**)q
;
191 /* grow ifindex2ifnet */
192 n
= if_indexlim
* sizeof(struct ifnet
*);
193 q
= malloc(n
, M_IFADDR
, M_WAITOK
);
196 bcopy(ifindex2ifnet
, q
, n
/2);
197 free(ifindex2ifnet
, M_IFADDR
);
199 ifindex2ifnet
= (struct ifnet
**)q
;
202 ifindex2ifnet
[if_index
] = ifp
;
205 * create a Link Level name for this device
207 namelen
= strlen(ifp
->if_xname
);
208 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
209 masklen
= _offsetof(struct sockaddr_dl
, sdl_data
[0]) + namelen
;
210 socksize
= masklen
+ ifp
->if_addrlen
;
211 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
212 if (socksize
< sizeof(*sdl
))
213 socksize
= sizeof(*sdl
);
214 socksize
= ROUNDUP(socksize
);
215 ifasize
= sizeof(*ifa
) + 2 * socksize
;
216 ifa
= (struct ifaddr
*)malloc(ifasize
, M_IFADDR
, M_WAITOK
);
219 sdl
= (struct sockaddr_dl
*)(ifa
+ 1);
220 sdl
->sdl_len
= socksize
;
221 sdl
->sdl_family
= AF_LINK
;
222 bcopy(ifp
->if_xname
, sdl
->sdl_data
, namelen
);
223 sdl
->sdl_nlen
= namelen
;
224 sdl
->sdl_index
= ifp
->if_index
;
225 sdl
->sdl_type
= ifp
->if_type
;
226 ifnet_addrs
[if_index
- 1] = ifa
;
228 ifa
->ifa_rtrequest
= link_rtrequest
;
229 ifa
->ifa_addr
= (struct sockaddr
*)sdl
;
230 sdl
= (struct sockaddr_dl
*)(socksize
+ (caddr_t
)sdl
);
231 ifa
->ifa_netmask
= (struct sockaddr
*)sdl
;
232 sdl
->sdl_len
= masklen
;
234 sdl
->sdl_data
[--namelen
] = 0xff;
235 TAILQ_INSERT_HEAD(&ifp
->if_addrhead
, ifa
, ifa_link
);
238 EVENTHANDLER_INVOKE(ifnet_attach_event
, ifp
);
240 /* Announce the interface. */
241 rt_ifannouncemsg(ifp
, IFAN_ARRIVAL
);
245 * Detach an interface, removing it from the
246 * list of "active" interfaces.
249 if_detach(struct ifnet
*ifp
)
252 struct radix_node_head
*rnh
;
256 EVENTHANDLER_INVOKE(ifnet_detach_event
, ifp
);
259 * Remove routes and flush queues.
265 * Remove address from ifnet_addrs[] and maybe decrement if_index.
266 * Clean up all addresses.
268 ifnet_addrs
[ifp
->if_index
- 1] = 0;
269 while (if_index
> 0 && ifnet_addrs
[if_index
- 1] == 0)
272 for (ifa
= TAILQ_FIRST(&ifp
->if_addrhead
); ifa
;
273 ifa
= TAILQ_FIRST(&ifp
->if_addrhead
)) {
275 /* XXX: Ugly!! ad hoc just for INET */
276 if (ifa
->ifa_addr
&& ifa
->ifa_addr
->sa_family
== AF_INET
) {
277 struct ifaliasreq ifr
;
279 bzero(&ifr
, sizeof ifr
);
280 ifr
.ifra_addr
= *ifa
->ifa_addr
;
281 if (ifa
->ifa_dstaddr
)
282 ifr
.ifra_broadaddr
= *ifa
->ifa_dstaddr
;
283 if (in_control(NULL
, SIOCDIFADDR
, (caddr_t
)&ifr
, ifp
,
289 if (ifa
->ifa_addr
&& ifa
->ifa_addr
->sa_family
== AF_INET6
) {
291 /* ifp_addrhead is already updated */
295 TAILQ_REMOVE(&ifp
->if_addrhead
, ifa
, ifa_link
);
301 * Remove all IPv6 kernel structs related to ifp. This should be done
302 * before removing routing entries below, since IPv6 interface direct
303 * routes are expected to be removed by the IPv6-specific kernel API.
304 * Otherwise, the kernel will detect some inconsistency and bark it.
310 * Delete all remaining routes using this interface
311 * Unfortuneatly the only way to do this is to slog through
312 * the entire routing table looking for routes which point
313 * to this interface...oh well...
315 for (i
= 1; i
<= AF_MAX
; i
++) {
316 if ((rnh
= rt_tables
[i
]) == NULL
)
318 rnh
->rnh_walktree(rnh
, if_rtdel
, ifp
);
321 /* Announce that the interface is gone. */
322 rt_ifannouncemsg(ifp
, IFAN_DEPARTURE
);
324 ifindex2ifnet
[ifp
->if_index
] = NULL
;
326 TAILQ_REMOVE(&ifnet
, ifp
, if_link
);
331 * Delete Routes for a Network Interface
333 * Called for each routing entry via the rnh->rnh_walktree() call above
334 * to delete all route entries referencing a detaching network interface.
337 * rn pointer to node in the routing table
338 * arg argument passed to rnh->rnh_walktree() - detaching interface
342 * errno failed - reason indicated
346 if_rtdel(struct radix_node
*rn
, void *arg
)
348 struct rtentry
*rt
= (struct rtentry
*)rn
;
349 struct ifnet
*ifp
= arg
;
352 if (rt
->rt_ifp
== ifp
) {
355 * Protect (sorta) against walktree recursion problems
358 if (!(rt
->rt_flags
& RTF_UP
))
361 err
= rtrequest(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
362 rt_mask(rt
), rt
->rt_flags
,
363 (struct rtentry
**) NULL
);
365 log(LOG_WARNING
, "if_rtdel: error %d\n", err
);
373 * Create a clone network interface.
376 if_clone_create(char *name
, int len
)
378 struct if_clone
*ifc
;
380 int wildcard
, bytoff
, bitoff
;
384 ifc
= if_clone_lookup(name
, &unit
);
388 if (ifunit(name
) != NULL
)
392 wildcard
= (unit
< 0);
394 * Find a free unit if none was given.
397 while (bytoff
< ifc
->ifc_bmlen
&&
398 ifc
->ifc_units
[bytoff
] == 0xff)
400 if (bytoff
>= ifc
->ifc_bmlen
)
402 while ((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0)
404 unit
= (bytoff
<< 3) + bitoff
;
407 if (unit
> ifc
->ifc_maxunit
)
410 err
= (*ifc
->ifc_create
)(ifc
, unit
);
416 bitoff
= unit
- (bytoff
<< 3);
420 * Allocate the unit in the bitmap.
422 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) == 0,
423 ("%s: bit is already set", __func__
));
424 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
426 /* In the wildcard case, we need to update the name. */
428 for (dp
= name
; *dp
!= '\0'; dp
++);
429 if (snprintf(dp
, len
- (dp
-name
), "%d", unit
) >
430 len
- (dp
-name
) - 1) {
432 * This can only be a programmer error and
433 * there's no straightforward way to recover if
436 panic("if_clone_create(): interface name too long");
441 EVENTHANDLER_INVOKE(if_clone_event
, ifc
);
447 * Destroy a clone network interface.
450 if_clone_destroy(const char *name
)
452 struct if_clone
*ifc
;
457 ifc
= if_clone_lookup(name
, &unit
);
461 if (unit
< ifc
->ifc_minifs
)
468 if (ifc
->ifc_destroy
== NULL
)
471 (*ifc
->ifc_destroy
)(ifp
);
474 * Compute offset in the bitmap and deallocate the unit.
477 bitoff
= unit
- (bytoff
<< 3);
478 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0,
479 ("%s: bit is already cleared", __func__
));
480 ifc
->ifc_units
[bytoff
] &= ~(1 << bitoff
);
485 * Look up a network interface cloner.
488 if_clone_lookup(const char *name
, int *unitp
)
490 struct if_clone
*ifc
;
494 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
;) {
495 for (cp
= name
, i
= 0; i
< ifc
->ifc_namelen
; i
++, cp
++) {
496 if (ifc
->ifc_name
[i
] != *cp
)
501 ifc
= LIST_NEXT(ifc
, ifc_list
);
505 return ((struct if_clone
*)NULL
);
511 for (i
= 0; *cp
!= '\0'; cp
++) {
512 if (*cp
< '0' || *cp
> '9') {
513 /* Bogus unit number. */
516 i
= (i
* 10) + (*cp
- '0');
526 * Register a network interface cloner.
529 if_clone_attach(struct if_clone
*ifc
)
536 KASSERT(ifc
->ifc_minifs
- 1 <= ifc
->ifc_maxunit
,
537 ("%s: %s requested more units then allowed (%d > %d)",
538 __func__
, ifc
->ifc_name
, ifc
->ifc_minifs
,
539 ifc
->ifc_maxunit
+ 1));
541 * Compute bitmap size and allocate it.
543 maxclone
= ifc
->ifc_maxunit
+ 1;
545 if ((len
<< 3) < maxclone
)
547 ifc
->ifc_units
= malloc(len
, M_CLONE
, M_WAITOK
| M_ZERO
);
548 ifc
->ifc_bmlen
= len
;
550 LIST_INSERT_HEAD(&if_cloners
, ifc
, ifc_list
);
553 for (unit
= 0; unit
< ifc
->ifc_minifs
; unit
++) {
554 err
= (*ifc
->ifc_create
)(ifc
, unit
);
556 ("%s: failed to create required interface %s%d",
557 __func__
, ifc
->ifc_name
, unit
));
559 /* Allocate the unit in the bitmap. */
561 bitoff
= unit
- (bytoff
<< 3);
562 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
567 * Unregister a network interface cloner.
570 if_clone_detach(struct if_clone
*ifc
)
573 LIST_REMOVE(ifc
, ifc_list
);
574 free(ifc
->ifc_units
, M_CLONE
);
579 * Provide list of interface cloners to userspace.
582 if_clone_list(struct if_clonereq
*ifcr
)
584 char outbuf
[IFNAMSIZ
], *dst
;
585 struct if_clone
*ifc
;
586 int count
, error
= 0;
588 ifcr
->ifcr_total
= if_cloners_count
;
589 if ((dst
= ifcr
->ifcr_buffer
) == NULL
) {
590 /* Just asking how many there are. */
594 if (ifcr
->ifcr_count
< 0)
597 count
= (if_cloners_count
< ifcr
->ifcr_count
) ?
598 if_cloners_count
: ifcr
->ifcr_count
;
600 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
&& count
!= 0;
601 ifc
= LIST_NEXT(ifc
, ifc_list
), count
--, dst
+= IFNAMSIZ
) {
602 strlcpy(outbuf
, ifc
->ifc_name
, IFNAMSIZ
);
603 error
= copyout(outbuf
, dst
, IFNAMSIZ
);
612 * Locate an interface based on a complete address.
615 ifa_ifwithaddr(struct sockaddr
*addr
)
620 #define equal(a1, a2) \
621 (bcmp((a1), (a2), ((struct sockaddr *)(a1))->sa_len) == 0)
623 TAILQ_FOREACH(ifp
, &ifnet
, if_link
)
624 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
625 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
627 if (equal(addr
, ifa
->ifa_addr
))
629 if ((ifp
->if_flags
& IFF_BROADCAST
) && ifa
->ifa_broadaddr
&&
630 /* IP6 doesn't have broadcast */
631 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
632 equal(ifa
->ifa_broadaddr
, addr
))
635 return ((struct ifaddr
*)0);
638 * Locate the point to point interface with a given destination address.
641 ifa_ifwithdstaddr(struct sockaddr
*addr
)
646 TAILQ_FOREACH(ifp
, &ifnet
, if_link
)
647 if (ifp
->if_flags
& IFF_POINTOPOINT
)
648 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
649 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
651 if (ifa
->ifa_dstaddr
&& equal(addr
, ifa
->ifa_dstaddr
))
654 return ((struct ifaddr
*)0);
658 * Find an interface on a specific network. If many, choice
659 * is most specific found.
662 ifa_ifwithnet(struct sockaddr
*addr
)
666 struct ifaddr
*ifa_maybe
= (struct ifaddr
*) 0;
667 u_int af
= addr
->sa_family
;
668 char *addr_data
= addr
->sa_data
, *cplim
;
671 * AF_LINK addresses can be looked up directly by their index number,
672 * so do that if we can.
675 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)addr
;
676 if (sdl
->sdl_index
&& sdl
->sdl_index
<= if_index
)
677 return (ifnet_addrs
[sdl
->sdl_index
- 1]);
681 * Scan though each interface, looking for ones that have
682 * addresses in this address family.
684 TAILQ_FOREACH(ifp
, &ifnet
, if_link
) {
685 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
686 char *cp
, *cp2
, *cp3
;
688 if (ifa
->ifa_addr
->sa_family
!= af
)
690 if (af
== AF_INET
&& ifp
->if_flags
& IFF_POINTOPOINT
) {
692 * This is a bit broken as it doesn't
693 * take into account that the remote end may
694 * be a single node in the network we are
696 * The trouble is that we don't know the
697 * netmask for the remote end.
699 if (ifa
->ifa_dstaddr
!= 0 &&
700 equal(addr
, ifa
->ifa_dstaddr
))
704 * if we have a special address handler,
705 * then use it instead of the generic one.
707 if (ifa
->ifa_claim_addr
) {
708 if ((*ifa
->ifa_claim_addr
)(ifa
, addr
)) {
716 * Scan all the bits in the ifa's address.
717 * If a bit dissagrees with what we are
718 * looking for, mask it with the netmask
719 * to see if it really matters.
722 if (ifa
->ifa_netmask
== 0)
725 cp2
= ifa
->ifa_addr
->sa_data
;
726 cp3
= ifa
->ifa_netmask
->sa_data
;
727 cplim
= ifa
->ifa_netmask
->sa_len
728 + (char *)ifa
->ifa_netmask
;
730 if ((*cp
++ ^ *cp2
++) & *cp3
++)
731 goto next
; /* next address! */
733 * If the netmask of what we just found
734 * is more specific than what we had before
735 * (if we had one) then remember the new one
736 * before continuing to search
737 * for an even better one.
739 if (ifa_maybe
== 0 ||
740 rn_refines((char *)ifa
->ifa_netmask
,
741 (char *)ifa_maybe
->ifa_netmask
))
750 * Find an interface address specific to an interface best matching
754 ifaof_ifpforaddr(struct sockaddr
*addr
, struct ifnet
*ifp
)
757 char *cp
, *cp2
, *cp3
;
759 struct ifaddr
*ifa_maybe
= 0;
760 u_int af
= addr
->sa_family
;
764 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
765 if (ifa
->ifa_addr
->sa_family
!= af
)
769 if (ifa
->ifa_netmask
== 0) {
770 if (equal(addr
, ifa
->ifa_addr
) ||
771 (ifa
->ifa_dstaddr
&& equal(addr
, ifa
->ifa_dstaddr
)))
775 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
776 if (equal(addr
, ifa
->ifa_dstaddr
))
780 cp2
= ifa
->ifa_addr
->sa_data
;
781 cp3
= ifa
->ifa_netmask
->sa_data
;
782 cplim
= ifa
->ifa_netmask
->sa_len
+ (char *)ifa
->ifa_netmask
;
783 for (; cp3
< cplim
; cp3
++)
784 if ((*cp
++ ^ *cp2
++) & *cp3
)
793 #include <net/route.h>
796 * Default action when installing a route with a Link Level gateway.
797 * Lookup an appropriate real ifa to point to.
798 * This should be moved to /sys/net/link.c eventually.
801 link_rtrequest(int cmd
, struct rtentry
*rt
, struct rt_addrinfo
*info
)
804 struct sockaddr
*dst
;
807 if (cmd
!= RTM_ADD
|| (ifa
= rt
->rt_ifa
) == NULL
||
808 (ifp
= ifa
->ifa_ifp
) == NULL
|| (dst
= rt_key(rt
)) == NULL
)
810 ifa
= ifaof_ifpforaddr(dst
, ifp
);
815 if (ifa
->ifa_rtrequest
&& ifa
->ifa_rtrequest
!= link_rtrequest
)
816 ifa
->ifa_rtrequest(cmd
, rt
, info
);
821 * Mark an interface down and notify protocols of
823 * NOTE: must be called at splnet or eqivalent.
826 if_unroute(struct ifnet
*ifp
, int flag
, int fam
)
830 ifp
->if_flags
&= ~flag
;
831 getmicrotime(&ifp
->if_lastchange
);
832 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
)
833 if (fam
== PF_UNSPEC
|| (fam
== ifa
->ifa_addr
->sa_family
))
834 pfctlinput(PRC_IFDOWN
, ifa
->ifa_addr
);
835 if_qflush(&ifp
->if_snd
);
840 * Mark an interface up and notify protocols of
842 * NOTE: must be called at splnet or eqivalent.
845 if_route(struct ifnet
*ifp
, int flag
, int fam
)
849 ifp
->if_flags
|= flag
;
850 getmicrotime(&ifp
->if_lastchange
);
851 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
)
852 if (fam
== PF_UNSPEC
|| (fam
== ifa
->ifa_addr
->sa_family
))
853 pfctlinput(PRC_IFUP
, ifa
->ifa_addr
);
861 * Mark an interface down and notify protocols of
863 * NOTE: must be called at splnet or eqivalent.
866 if_down(struct ifnet
*ifp
)
869 if_unroute(ifp
, IFF_UP
, AF_UNSPEC
);
873 * Mark an interface up and notify protocols of
875 * NOTE: must be called at splnet or eqivalent.
878 if_up(struct ifnet
*ifp
)
881 if_route(ifp
, IFF_UP
, AF_UNSPEC
);
885 * Flush an interface queue.
888 if_qflush(struct ifqueue
*ifq
)
893 while ((m
= n
) != 0) {
903 * Handle interface watchdog timer routines. Called
904 * from softclock, we decrement timers (if set) and
905 * call the appropriate interface routine on expiration.
908 if_slowtimo(void *arg
)
913 TAILQ_FOREACH(ifp
, &ifnet
, if_link
) {
914 if (ifp
->if_timer
== 0 || --ifp
->if_timer
)
916 if (ifp
->if_watchdog
)
917 (*ifp
->if_watchdog
)(ifp
);
920 callout_reset(&if_slowtimo_timer
, hz
/ IFNET_SLOWHZ
, if_slowtimo
, NULL
);
924 * Map interface name to
925 * interface structure pointer.
928 ifunit(const char *name
)
933 * Search all the interfaces for this name/number
936 TAILQ_FOREACH(ifp
, &ifnet
, if_link
) {
937 if (strncmp(ifp
->if_xname
, name
, IFNAMSIZ
) == 0)
945 * Map interface name in a sockaddr_dl to
946 * interface structure pointer.
949 if_withname(struct sockaddr
*sa
)
951 char ifname
[IFNAMSIZ
+1];
952 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)sa
;
954 if ( (sa
->sa_family
!= AF_LINK
) || (sdl
->sdl_nlen
== 0) ||
955 (sdl
->sdl_nlen
> IFNAMSIZ
) )
959 * ifunit wants a null-terminated name. It may not be null-terminated
960 * in the sockaddr. We don't want to change the caller's sockaddr,
961 * and there might not be room to put the trailing null anyway, so we
962 * make a local copy that we know we can null terminate safely.
965 bcopy(sdl
->sdl_data
, ifname
, sdl
->sdl_nlen
);
966 ifname
[sdl
->sdl_nlen
] = '\0';
967 return ifunit(ifname
);
975 ifioctl(struct socket
*so
, u_long cmd
, caddr_t data
, struct thread
*td
)
983 size_t namelen
, onamelen
;
984 char new_name
[IFNAMSIZ
];
986 struct sockaddr_dl
*sdl
;
992 return (ifconf(cmd
, data
, td
));
994 ifr
= (struct ifreq
*)data
;
999 if ((error
= suser(td
)) != 0)
1001 return ((cmd
== SIOCIFCREATE
) ?
1002 if_clone_create(ifr
->ifr_name
, sizeof(ifr
->ifr_name
)) :
1003 if_clone_destroy(ifr
->ifr_name
));
1005 case SIOCIFGCLONERS
:
1006 return (if_clone_list((struct if_clonereq
*)data
));
1009 ifp
= ifunit(ifr
->ifr_name
);
1015 ifr
->ifr_flags
= ifp
->if_flags
;
1016 ifr
->ifr_flagshigh
= ifp
->if_flags
>> 16;
1020 ifr
->ifr_reqcap
= ifp
->if_capabilities
;
1021 ifr
->ifr_curcap
= ifp
->if_capenable
;
1025 ifr
->ifr_metric
= ifp
->if_metric
;
1029 ifr
->ifr_mtu
= ifp
->if_mtu
;
1033 ifr
->ifr_phys
= ifp
->if_physical
;
1040 new_flags
= (ifr
->ifr_flags
& 0xffff) |
1041 (ifr
->ifr_flagshigh
<< 16);
1042 if (ifp
->if_flags
& IFF_SMART
) {
1043 /* Smart drivers twiddle their own routes */
1044 } else if (ifp
->if_flags
& IFF_UP
&&
1045 (new_flags
& IFF_UP
) == 0) {
1049 } else if (new_flags
& IFF_UP
&&
1050 (ifp
->if_flags
& IFF_UP
) == 0) {
1055 ifp
->if_flags
= (ifp
->if_flags
& IFF_CANTCHANGE
) |
1056 (new_flags
&~ IFF_CANTCHANGE
);
1057 if (new_flags
& IFF_PPROMISC
) {
1058 /* Permanently promiscuous mode requested */
1059 ifp
->if_flags
|= IFF_PROMISC
;
1060 } else if (ifp
->if_pcount
== 0) {
1061 ifp
->if_flags
&= ~IFF_PROMISC
;
1064 (*ifp
->if_ioctl
)(ifp
, cmd
, data
, td
->td_proc
->p_ucred
);
1065 getmicrotime(&ifp
->if_lastchange
);
1072 if (ifr
->ifr_reqcap
& ~ifp
->if_capabilities
)
1074 (*ifp
->if_ioctl
)(ifp
, cmd
, data
, td
->td_proc
->p_ucred
);
1081 error
= copyinstr(ifr
->ifr_data
, new_name
, IFNAMSIZ
, NULL
);
1084 if (new_name
[0] == '\0')
1086 if (ifunit(new_name
) != NULL
)
1089 EVENTHANDLER_INVOKE(ifnet_detach_event
, ifp
);
1091 /* Announce the departure of the interface. */
1092 rt_ifannouncemsg(ifp
, IFAN_DEPARTURE
);
1094 strlcpy(ifp
->if_xname
, new_name
, sizeof(ifp
->if_xname
));
1095 ifa
= TAILQ_FIRST(&ifp
->if_addrhead
);
1096 /* XXX IFA_LOCK(ifa); */
1097 sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
1098 namelen
= strlen(new_name
);
1099 onamelen
= sdl
->sdl_nlen
;
1101 * Move the address if needed. This is safe because we
1102 * allocate space for a name of length IFNAMSIZ when we
1103 * create this in if_attach().
1105 if (namelen
!= onamelen
) {
1106 bcopy(sdl
->sdl_data
+ onamelen
,
1107 sdl
->sdl_data
+ namelen
, sdl
->sdl_alen
);
1109 bcopy(new_name
, sdl
->sdl_data
, namelen
);
1110 sdl
->sdl_nlen
= namelen
;
1111 sdl
= (struct sockaddr_dl
*)ifa
->ifa_netmask
;
1112 bzero(sdl
->sdl_data
, onamelen
);
1113 while (namelen
!= 0)
1114 sdl
->sdl_data
[--namelen
] = 0xff;
1115 /* XXX IFA_UNLOCK(ifa) */
1117 EVENTHANDLER_INVOKE(ifnet_attach_event
, ifp
);
1119 /* Announce the return of the interface. */
1120 rt_ifannouncemsg(ifp
, IFAN_ARRIVAL
);
1127 ifp
->if_metric
= ifr
->ifr_metric
;
1128 getmicrotime(&ifp
->if_lastchange
);
1137 error
= (*ifp
->if_ioctl
)(ifp
, cmd
, data
, td
->td_proc
->p_ucred
);
1139 getmicrotime(&ifp
->if_lastchange
);
1144 u_long oldmtu
= ifp
->if_mtu
;
1149 if (ifp
->if_ioctl
== NULL
)
1150 return (EOPNOTSUPP
);
1151 if (ifr
->ifr_mtu
< IF_MINMTU
|| ifr
->ifr_mtu
> IF_MAXMTU
)
1153 error
= (*ifp
->if_ioctl
)(ifp
, cmd
, data
, td
->td_proc
->p_ucred
);
1155 getmicrotime(&ifp
->if_lastchange
);
1159 * If the link MTU changed, do network layer specific procedure.
1161 if (ifp
->if_mtu
!= oldmtu
) {
1175 /* Don't allow group membership on non-multicast interfaces. */
1176 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0)
1179 /* Don't let users screw up protocols' entries. */
1180 if (ifr
->ifr_addr
.sa_family
!= AF_LINK
)
1183 if (cmd
== SIOCADDMULTI
) {
1184 struct ifmultiaddr
*ifma
;
1185 error
= if_addmulti(ifp
, &ifr
->ifr_addr
, &ifma
);
1187 error
= if_delmulti(ifp
, &ifr
->ifr_addr
);
1190 getmicrotime(&ifp
->if_lastchange
);
1193 case SIOCSIFPHYADDR
:
1194 case SIOCDIFPHYADDR
:
1196 case SIOCSIFPHYADDR_IN6
:
1198 case SIOCSLIFPHYADDR
:
1200 case SIOCSIFGENERIC
:
1204 if (ifp
->if_ioctl
== 0)
1205 return (EOPNOTSUPP
);
1206 error
= (*ifp
->if_ioctl
)(ifp
, cmd
, data
, td
->td_proc
->p_ucred
);
1208 getmicrotime(&ifp
->if_lastchange
);
1212 ifs
= (struct ifstat
*)data
;
1213 ifs
->ascii
[0] = '\0';
1215 case SIOCGIFPSRCADDR
:
1216 case SIOCGIFPDSTADDR
:
1217 case SIOCGLIFPHYADDR
:
1219 case SIOCGIFGENERIC
:
1220 if (ifp
->if_ioctl
== 0)
1221 return (EOPNOTSUPP
);
1222 return ((*ifp
->if_ioctl
)(ifp
, cmd
, data
, td
->td_proc
->p_ucred
));
1228 return if_setlladdr(ifp
,
1229 ifr
->ifr_addr
.sa_data
, ifr
->ifr_addr
.sa_len
);
1232 oif_flags
= ifp
->if_flags
;
1233 if (so
->so_proto
== 0)
1234 return (EOPNOTSUPP
);
1236 error
= so_pru_control(so
, cmd
, data
, ifp
, td
);
1243 case SIOCSIFDSTADDR
:
1245 case SIOCSIFBRDADDR
:
1246 case SIOCSIFNETMASK
:
1247 #if BYTE_ORDER != BIG_ENDIAN
1248 if (ifr
->ifr_addr
.sa_family
== 0 &&
1249 ifr
->ifr_addr
.sa_len
< 16) {
1250 ifr
->ifr_addr
.sa_family
= ifr
->ifr_addr
.sa_len
;
1251 ifr
->ifr_addr
.sa_len
= 16;
1254 if (ifr
->ifr_addr
.sa_len
== 0)
1255 ifr
->ifr_addr
.sa_len
= 16;
1263 case OSIOCGIFDSTADDR
:
1264 cmd
= SIOCGIFDSTADDR
;
1267 case OSIOCGIFBRDADDR
:
1268 cmd
= SIOCGIFBRDADDR
;
1271 case OSIOCGIFNETMASK
:
1272 cmd
= SIOCGIFNETMASK
;
1274 error
= so_pru_control(so
, cmd
, data
, ifp
, td
);
1278 case OSIOCGIFDSTADDR
:
1279 case OSIOCGIFBRDADDR
:
1280 case OSIOCGIFNETMASK
:
1281 *(u_short
*)&ifr
->ifr_addr
= ifr
->ifr_addr
.sa_family
;
1285 #endif /* COMPAT_43 */
1287 if ((oif_flags
^ ifp
->if_flags
) & IFF_UP
) {
1289 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1290 if (ifp
->if_flags
& IFF_UP
) {
1304 * Set/clear promiscuous mode on interface ifp based on the truth value
1305 * of pswitch. The calls are reference counted so that only the first
1306 * "on" request actually has an effect, as does the final "off" request.
1307 * Results are undefined if the "off" and "on" requests are not matched.
1310 ifpromisc(struct ifnet
*ifp
, int pswitch
)
1316 oldflags
= ifp
->if_flags
;
1317 if (ifp
->if_flags
& IFF_PPROMISC
) {
1318 /* Do nothing if device is in permanently promiscuous mode */
1319 ifp
->if_pcount
+= pswitch
? 1 : -1;
1324 * If the device is not configured up, we cannot put it in
1327 if ((ifp
->if_flags
& IFF_UP
) == 0)
1329 if (ifp
->if_pcount
++ != 0)
1331 ifp
->if_flags
|= IFF_PROMISC
;
1332 log(LOG_INFO
, "%s: promiscuous mode enabled\n",
1335 if (--ifp
->if_pcount
> 0)
1337 ifp
->if_flags
&= ~IFF_PROMISC
;
1338 log(LOG_INFO
, "%s: promiscuous mode disabled\n",
1341 ifr
.ifr_flags
= ifp
->if_flags
;
1342 ifr
.ifr_flagshigh
= ifp
->if_flags
>> 16;
1343 error
= (*ifp
->if_ioctl
)(ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
,
1344 (struct ucred
*)NULL
);
1348 ifp
->if_flags
= oldflags
;
1353 * Return interface configuration
1354 * of system. List may be used
1355 * in later ioctl's (above) to get
1356 * other information.
1359 ifconf(u_long cmd
, caddr_t data
, struct thread
*td
)
1361 struct ifconf
*ifc
= (struct ifconf
*)data
;
1364 struct sockaddr
*sa
;
1365 struct ifreq ifr
, *ifrp
;
1366 int space
= ifc
->ifc_len
, error
= 0;
1368 ifrp
= ifc
->ifc_req
;
1369 TAILQ_FOREACH(ifp
, &ifnet
, if_link
) {
1372 if (space
<= sizeof ifr
)
1374 if (strlcpy(ifr
.ifr_name
, ifp
->if_xname
, sizeof(ifr
.ifr_name
))
1375 >= sizeof(ifr
.ifr_name
)) {
1376 error
= ENAMETOOLONG
;
1381 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
1382 if (space
<= sizeof ifr
)
1385 if (td
->td_proc
->p_ucred
->cr_prison
&&
1390 if (cmd
== OSIOCGIFCONF
) {
1391 struct osockaddr
*osa
=
1392 (struct osockaddr
*)&ifr
.ifr_addr
;
1394 osa
->sa_family
= sa
->sa_family
;
1395 error
= copyout(&ifr
, ifrp
, sizeof ifr
);
1399 if (sa
->sa_len
<= sizeof(*sa
)) {
1401 error
= copyout(&ifr
, ifrp
, sizeof ifr
);
1404 if (space
< (sizeof ifr
) + sa
->sa_len
-
1407 space
-= sa
->sa_len
- sizeof(*sa
);
1408 error
= copyout(&ifr
, ifrp
,
1409 sizeof ifr
.ifr_name
);
1411 error
= copyout(sa
, &ifrp
->ifr_addr
,
1413 ifrp
= (struct ifreq
*)
1414 (sa
->sa_len
+ (caddr_t
)&ifrp
->ifr_addr
);
1418 space
-= sizeof ifr
;
1423 bzero(&ifr
.ifr_addr
, sizeof ifr
.ifr_addr
);
1424 error
= copyout(&ifr
, ifrp
, sizeof ifr
);
1427 space
-= sizeof ifr
;
1431 ifc
->ifc_len
-= space
;
1436 * Just like if_promisc(), but for all-multicast-reception mode.
1439 if_allmulti(struct ifnet
*ifp
, int onswitch
)
1446 if (ifp
->if_amcount
++ == 0) {
1447 ifp
->if_flags
|= IFF_ALLMULTI
;
1448 ifr
.ifr_flags
= ifp
->if_flags
;
1449 ifr
.ifr_flagshigh
= ifp
->if_flags
>> 16;
1450 error
= ifp
->if_ioctl(ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
,
1451 (struct ucred
*)NULL
);
1454 if (ifp
->if_amcount
> 1) {
1457 ifp
->if_amcount
= 0;
1458 ifp
->if_flags
&= ~IFF_ALLMULTI
;
1459 ifr
.ifr_flags
= ifp
->if_flags
;
1460 ifr
.ifr_flagshigh
= ifp
->if_flags
>> 16;
1461 error
= ifp
->if_ioctl(ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
,
1462 (struct ucred
*)NULL
);
1473 * Add a multicast listenership to the interface in question.
1474 * The link layer provides a routine which converts
1478 struct ifnet
*ifp
, /* interface to manipulate */
1479 struct sockaddr
*sa
, /* address to add */
1480 struct ifmultiaddr
**retifma
)
1482 struct sockaddr
*llsa
, *dupsa
;
1484 struct ifmultiaddr
*ifma
;
1487 * If the matching multicast address already exists
1488 * then don't add a new one, just add a reference
1490 LIST_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
) {
1491 if (equal(sa
, ifma
->ifma_addr
)) {
1492 ifma
->ifma_refcount
++;
1500 * Give the link layer a chance to accept/reject it, and also
1501 * find out which AF_LINK address this maps to, if it isn't one
1504 if (ifp
->if_resolvemulti
) {
1505 error
= ifp
->if_resolvemulti(ifp
, &llsa
, sa
);
1506 if (error
) return error
;
1511 MALLOC(ifma
, struct ifmultiaddr
*, sizeof *ifma
, M_IFMADDR
, M_WAITOK
);
1512 MALLOC(dupsa
, struct sockaddr
*, sa
->sa_len
, M_IFMADDR
, M_WAITOK
);
1513 bcopy(sa
, dupsa
, sa
->sa_len
);
1515 ifma
->ifma_addr
= dupsa
;
1516 ifma
->ifma_lladdr
= llsa
;
1517 ifma
->ifma_ifp
= ifp
;
1518 ifma
->ifma_refcount
= 1;
1519 ifma
->ifma_protospec
= 0;
1520 rt_newmaddrmsg(RTM_NEWMADDR
, ifma
);
1523 * Some network interfaces can scan the address list at
1524 * interrupt time; lock them out.
1527 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
1532 LIST_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
) {
1533 if (equal(ifma
->ifma_addr
, llsa
))
1537 ifma
->ifma_refcount
++;
1539 MALLOC(ifma
, struct ifmultiaddr
*, sizeof *ifma
,
1540 M_IFMADDR
, M_WAITOK
);
1541 MALLOC(dupsa
, struct sockaddr
*, llsa
->sa_len
,
1542 M_IFMADDR
, M_WAITOK
);
1543 bcopy(llsa
, dupsa
, llsa
->sa_len
);
1544 ifma
->ifma_addr
= dupsa
;
1545 ifma
->ifma_ifp
= ifp
;
1546 ifma
->ifma_refcount
= 1;
1548 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
1553 * We are certain we have added something, so call down to the
1554 * interface to let them know about it.
1557 ifp
->if_ioctl(ifp
, SIOCADDMULTI
, 0, (struct ucred
*)NULL
);
1564 * Remove a reference to a multicast address on this interface. Yell
1565 * if the request does not match an existing membership.
1568 if_delmulti(struct ifnet
*ifp
, struct sockaddr
*sa
)
1570 struct ifmultiaddr
*ifma
;
1573 LIST_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
)
1574 if (equal(sa
, ifma
->ifma_addr
))
1579 if (ifma
->ifma_refcount
> 1) {
1580 ifma
->ifma_refcount
--;
1584 rt_newmaddrmsg(RTM_DELMADDR
, ifma
);
1585 sa
= ifma
->ifma_lladdr
;
1587 LIST_REMOVE(ifma
, ifma_link
);
1589 * Make sure the interface driver is notified
1590 * in the case of a link layer mcast group being left.
1592 if (ifma
->ifma_addr
->sa_family
== AF_LINK
&& sa
== 0)
1593 ifp
->if_ioctl(ifp
, SIOCDELMULTI
, 0, (struct ucred
*)NULL
);
1595 free(ifma
->ifma_addr
, M_IFMADDR
);
1596 free(ifma
, M_IFMADDR
);
1601 * Now look for the link-layer address which corresponds to
1602 * this network address. It had been squirreled away in
1603 * ifma->ifma_lladdr for this purpose (so we don't have
1604 * to call ifp->if_resolvemulti() again), and we saved that
1605 * value in sa above. If some nasty deleted the
1606 * link-layer address out from underneath us, we can deal because
1607 * the address we stored was is not the same as the one which was
1608 * in the record for the link-layer address. (So we don't complain
1611 LIST_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
)
1612 if (equal(sa
, ifma
->ifma_addr
))
1617 if (ifma
->ifma_refcount
> 1) {
1618 ifma
->ifma_refcount
--;
1623 LIST_REMOVE(ifma
, ifma_link
);
1624 ifp
->if_ioctl(ifp
, SIOCDELMULTI
, 0, (struct ucred
*)NULL
);
1626 free(ifma
->ifma_addr
, M_IFMADDR
);
1627 free(sa
, M_IFMADDR
);
1628 free(ifma
, M_IFMADDR
);
1634 * Set the link layer address on an interface.
1636 * At this time we only support certain types of interfaces,
1637 * and we don't allow the length of the address to change.
1640 if_setlladdr(struct ifnet
*ifp
, const u_char
*lladdr
, int len
)
1642 struct sockaddr_dl
*sdl
;
1646 ifa
= ifnet_addrs
[ifp
->if_index
- 1];
1649 sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
1652 if (len
!= sdl
->sdl_alen
) /* don't allow length to change */
1654 switch (ifp
->if_type
) {
1655 case IFT_ETHER
: /* these types use struct arpcom */
1660 bcopy(lladdr
, ((struct arpcom
*)ifp
->if_softc
)->ac_enaddr
, len
);
1663 bcopy(lladdr
, LLADDR(sdl
), len
);
1669 * If the interface is already up, we need
1670 * to re-init it in order to reprogram its
1673 if ((ifp
->if_flags
& IFF_UP
) != 0) {
1674 ifp
->if_flags
&= ~IFF_UP
;
1675 ifr
.ifr_flags
= ifp
->if_flags
;
1676 ifr
.ifr_flagshigh
= ifp
->if_flags
>> 16;
1677 (*ifp
->if_ioctl
)(ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
,
1678 (struct ucred
*)NULL
);
1679 ifp
->if_flags
|= IFF_UP
;
1680 ifr
.ifr_flags
= ifp
->if_flags
;
1681 ifr
.ifr_flagshigh
= ifp
->if_flags
>> 16;
1682 (*ifp
->if_ioctl
)(ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
,
1683 (struct ucred
*)NULL
);
1686 * Also send gratuitous ARPs to notify other nodes about
1687 * the address change.
1689 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
1690 if (ifa
->ifa_addr
!= NULL
&&
1691 ifa
->ifa_addr
->sa_family
== AF_INET
)
1692 arp_ifinit(ifp
, ifa
);
1699 struct ifmultiaddr
*
1700 ifmaof_ifpforaddr(struct sockaddr
*sa
, struct ifnet
*ifp
)
1702 struct ifmultiaddr
*ifma
;
1704 LIST_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
)
1705 if (equal(ifma
->ifma_addr
, sa
))
1712 * The name argument must be a pointer to storage which will last as
1713 * long as the interface does. For physical devices, the result of
1714 * device_get_name(dev) is a good choice and for pseudo-devices a
1715 * static string works well.
1718 if_initname(struct ifnet
*ifp
, const char *name
, int unit
)
1720 ifp
->if_dname
= name
;
1721 ifp
->if_dunit
= unit
;
1722 if (unit
!= IF_DUNIT_NONE
)
1723 snprintf(ifp
->if_xname
, IFNAMSIZ
, "%s%d", name
, unit
);
1725 strlcpy(ifp
->if_xname
, name
, IFNAMSIZ
);
1729 if_printf(struct ifnet
*ifp
, const char *fmt
, ...)
1734 retval
= printf("%s: ", ifp
->if_xname
);
1735 __va_start(ap
, fmt
);
1736 retval
+= vprintf(fmt
, ap
);
1741 SYSCTL_NODE(_net
, PF_LINK
, link
, CTLFLAG_RW
, 0, "Link layers");
1742 SYSCTL_NODE(_net_link
, 0, generic
, CTLFLAG_RW
, 0, "Generic link-management");