Annotation of src/sys/net/if.c, Revision 1.207
1.207 ! dyoung 1: /* $NetBSD: if.c,v 1.206 2007/12/05 23:47:17 dyoung Exp $ */
1.53 thorpej 2:
3: /*-
1.89 thorpej 4: * Copyright (c) 1999, 2000, 2001 The NetBSD Foundation, Inc.
1.53 thorpej 5: * All rights reserved.
6: *
7: * This code is derived from software contributed to The NetBSD Foundation
1.150 peter 8: * by William Studenmund and Jason R. Thorpe.
1.53 thorpej 9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: * 3. All advertising materials mentioning features or use of this software
19: * must display the following acknowledgement:
20: * This product includes software developed by the NetBSD
21: * Foundation, Inc. and its contributors.
22: * 4. Neither the name of The NetBSD Foundation nor the names of its
23: * contributors may be used to endorse or promote products derived
24: * from this software without specific prior written permission.
25: *
26: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36: * POSSIBILITY OF SUCH DAMAGE.
37: */
1.49 itojun 38:
39: /*
40: * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
41: * All rights reserved.
1.154 perry 42: *
1.49 itojun 43: * Redistribution and use in source and binary forms, with or without
44: * modification, are permitted provided that the following conditions
45: * are met:
46: * 1. Redistributions of source code must retain the above copyright
47: * notice, this list of conditions and the following disclaimer.
48: * 2. Redistributions in binary form must reproduce the above copyright
49: * notice, this list of conditions and the following disclaimer in the
50: * documentation and/or other materials provided with the distribution.
51: * 3. Neither the name of the project nor the names of its contributors
52: * may be used to endorse or promote products derived from this software
53: * without specific prior written permission.
1.154 perry 54: *
1.49 itojun 55: * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
56: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58: * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
59: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65: * SUCH DAMAGE.
66: */
1.16 cgd 67:
1.1 cgd 68: /*
1.15 mycroft 69: * Copyright (c) 1980, 1986, 1993
70: * The Regents of the University of California. All rights reserved.
1.1 cgd 71: *
72: * Redistribution and use in source and binary forms, with or without
73: * modification, are permitted provided that the following conditions
74: * are met:
75: * 1. Redistributions of source code must retain the above copyright
76: * notice, this list of conditions and the following disclaimer.
77: * 2. Redistributions in binary form must reproduce the above copyright
78: * notice, this list of conditions and the following disclaimer in the
79: * documentation and/or other materials provided with the distribution.
1.126 agc 80: * 3. Neither the name of the University nor the names of its contributors
1.1 cgd 81: * may be used to endorse or promote products derived from this software
82: * without specific prior written permission.
83: *
84: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
85: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
86: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
87: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
88: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
89: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
90: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
91: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
92: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
93: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
94: * SUCH DAMAGE.
95: *
1.44 fvdl 96: * @(#)if.c 8.5 (Berkeley) 1/9/95
1.1 cgd 97: */
1.99 lukem 98:
99: #include <sys/cdefs.h>
1.207 ! dyoung 100: __KERNEL_RCSID(0, "$NetBSD: if.c,v 1.206 2007/12/05 23:47:17 dyoung Exp $");
1.50 thorpej 101:
102: #include "opt_inet.h"
1.46 thorpej 103:
1.51 bouyer 104: #include "opt_atalk.h"
1.120 martin 105: #include "opt_natm.h"
1.87 thorpej 106: #include "opt_pfil_hooks.h"
1.1 cgd 107:
1.8 mycroft 108: #include <sys/param.h>
109: #include <sys/mbuf.h>
110: #include <sys/systm.h>
1.59 thorpej 111: #include <sys/callout.h>
1.15 mycroft 112: #include <sys/proc.h>
1.8 mycroft 113: #include <sys/socket.h>
114: #include <sys/socketvar.h>
1.56 thorpej 115: #include <sys/domain.h>
1.8 mycroft 116: #include <sys/protosw.h>
117: #include <sys/kernel.h>
118: #include <sys/ioctl.h>
1.133 jonathan 119: #include <sys/sysctl.h>
1.159 dyoung 120: #include <sys/syslog.h>
1.165 elad 121: #include <sys/kauth.h>
1.1 cgd 122:
1.8 mycroft 123: #include <net/if.h>
124: #include <net/if_dl.h>
1.66 onoe 125: #include <net/if_ether.h>
1.124 dyoung 126: #include <net/if_media.h>
1.132 dyoung 127: #include <net80211/ieee80211.h>
128: #include <net80211/ieee80211_ioctl.h>
1.8 mycroft 129: #include <net/if_types.h>
1.24 christos 130: #include <net/radix.h>
1.53 thorpej 131: #include <net/route.h>
1.95 itojun 132: #include <net/netisr.h>
1.51 bouyer 133: #ifdef NETATALK
134: #include <netatalk/at_extern.h>
135: #include <netatalk/at.h>
136: #endif
1.143 itojun 137: #include <net/pfil.h>
1.1 cgd 138:
1.49 itojun 139: #ifdef INET6
140: #include <netinet/in.h>
1.72 thorpej 141: #include <netinet6/in6_var.h>
1.108 itojun 142: #include <netinet6/nd6.h>
1.49 itojun 143: #endif
1.117 thorpej 144:
1.166 liamjfoy 145: #include "carp.h"
146: #if NCARP > 0
147: #include <netinet/ip_carp.h>
148: #endif
149:
1.186 christos 150: #include <compat/sys/sockio.h>
1.161 christos 151: #include <compat/sys/socket.h>
152:
1.117 thorpej 153: MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
154: MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
1.49 itojun 155:
1.1 cgd 156: int ifqmaxlen = IFQ_MAXLEN;
1.193 ad 157: callout_t if_slowtimo_ch;
1.49 itojun 158:
1.104 matt 159: int netisr; /* scheduling bits for network */
160:
1.192 dyoung 161: static int if_rt_walktree(struct rtentry *, void *);
1.53 thorpej 162:
1.163 thorpej 163: static struct if_clone *if_clone_lookup(const char *, int *);
164: static int if_clone_list(struct if_clonereq *);
1.63 thorpej 165:
1.163 thorpej 166: static LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
167: static int if_cloners_count;
1.63 thorpej 168:
1.143 itojun 169: #ifdef PFIL_HOOKS
170: struct pfil_head if_pfil; /* packet filtering hook for interfaces */
171: #endif
172:
1.163 thorpej 173: static void if_detach_queues(struct ifnet *, struct ifqueue *);
1.95 itojun 174:
1.1 cgd 175: /*
176: * Network interface utility routines.
177: *
178: * Routines with ifa_ifwith* names take sockaddr *'s as
179: * parameters.
180: */
1.4 andrew 181: void
1.163 thorpej 182: ifinit(void)
1.1 cgd 183: {
184:
1.193 ad 185: callout_init(&if_slowtimo_ch, 0);
1.4 andrew 186: if_slowtimo(NULL);
1.143 itojun 187: #ifdef PFIL_HOOKS
188: if_pfil.ph_type = PFIL_TYPE_IFNET;
189: if_pfil.ph_ifnet = NULL;
190: if (pfil_head_register(&if_pfil) != 0)
191: printf("WARNING: unable to register pfil hook\n");
192: #endif
1.1 cgd 193: }
194:
1.53 thorpej 195: /*
196: * Null routines used while an interface is going away. These routines
197: * just return an error.
198: */
199:
200: int
1.177 christos 201: if_nulloutput(struct ifnet *ifp, struct mbuf *m,
1.181 dyoung 202: const struct sockaddr *so, struct rtentry *rt)
1.53 thorpej 203: {
204:
1.185 dyoung 205: return ENXIO;
1.53 thorpej 206: }
207:
208: void
1.177 christos 209: if_nullinput(struct ifnet *ifp, struct mbuf *m)
1.53 thorpej 210: {
211:
212: /* Nothing. */
213: }
214:
215: void
1.177 christos 216: if_nullstart(struct ifnet *ifp)
1.53 thorpej 217: {
218:
219: /* Nothing. */
220: }
221:
222: int
1.183 christos 223: if_nullioctl(struct ifnet *ifp, u_long cmd, void *data)
1.53 thorpej 224: {
225:
1.185 dyoung 226: return ENXIO;
1.53 thorpej 227: }
228:
229: int
1.177 christos 230: if_nullinit(struct ifnet *ifp)
1.53 thorpej 231: {
232:
1.185 dyoung 233: return ENXIO;
1.53 thorpej 234: }
235:
236: void
1.177 christos 237: if_nullstop(struct ifnet *ifp, int disable)
1.75 thorpej 238: {
239:
240: /* Nothing. */
241: }
242:
243: void
1.177 christos 244: if_nullwatchdog(struct ifnet *ifp)
1.53 thorpej 245: {
246:
247: /* Nothing. */
248: }
249:
250: void
1.177 christos 251: if_nulldrain(struct ifnet *ifp)
1.53 thorpej 252: {
253:
254: /* Nothing. */
255: }
256:
1.137 itojun 257: static u_int if_index = 1;
1.103 simonb 258: struct ifnet_head ifnet;
1.137 itojun 259: size_t if_indexlim = 0;
1.49 itojun 260: struct ifaddr **ifnet_addrs = NULL;
261: struct ifnet **ifindex2ifnet = NULL;
1.148 peter 262: struct ifnet *lo0ifp;
1.1 cgd 263:
264: /*
1.81 thorpej 265: * Allocate the link level name for the specified interface. This
266: * is an attachment helper. It must be called after ifp->if_addrlen
267: * is initialized, which may not be the case when if_attach() is
268: * called.
269: */
270: void
271: if_alloc_sadl(struct ifnet *ifp)
272: {
273: unsigned socksize, ifasize;
1.195 dyoung 274: int addrlen, namelen;
275: struct sockaddr_dl *mask, *sdl;
1.81 thorpej 276: struct ifaddr *ifa;
1.84 thorpej 277:
278: /*
279: * If the interface already has a link name, release it
280: * now. This is useful for interfaces that can change
281: * link types, and thus switch link names often.
282: */
283: if (ifp->if_sadl != NULL)
284: if_free_sadl(ifp);
1.81 thorpej 285:
286: namelen = strlen(ifp->if_xname);
1.195 dyoung 287: addrlen = ifp->if_addrlen;
1.197 dyoung 288: socksize = roundup(sockaddr_dl_measure(namelen, addrlen), sizeof(long));
1.81 thorpej 289: ifasize = sizeof(*ifa) + 2 * socksize;
1.203 dyoung 290: ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK|M_ZERO);
1.195 dyoung 291:
1.81 thorpej 292: sdl = (struct sockaddr_dl *)(ifa + 1);
1.195 dyoung 293: mask = (struct sockaddr_dl *)(socksize + (char *)sdl);
294:
1.197 dyoung 295: sockaddr_dl_init(sdl, socksize, ifp->if_index, ifp->if_type,
1.195 dyoung 296: ifp->if_xname, namelen, NULL, addrlen);
297: mask->sdl_len = sockaddr_dl_measure(namelen, 0);
1.197 dyoung 298: memset(&mask->sdl_data[0], 0xff, namelen);
1.195 dyoung 299:
1.81 thorpej 300: ifnet_addrs[ifp->if_index] = ifa;
301: IFAREF(ifa);
1.207 ! dyoung 302: ifa_insert(ifp, ifa);
1.81 thorpej 303: ifa->ifa_rtrequest = link_rtrequest;
304: ifa->ifa_addr = (struct sockaddr *)sdl;
305: ifp->if_sadl = sdl;
1.195 dyoung 306: ifa->ifa_netmask = (struct sockaddr *)mask;
1.81 thorpej 307: }
308:
309: /*
310: * Free the link level name for the specified interface. This is
311: * a detach helper. This is called from if_detach() or from
312: * link layer type specific detach functions.
313: */
314: void
315: if_free_sadl(struct ifnet *ifp)
316: {
317: struct ifaddr *ifa;
318: int s;
319:
320: ifa = ifnet_addrs[ifp->if_index];
321: if (ifa == NULL) {
322: KASSERT(ifp->if_sadl == NULL);
323: return;
324: }
325:
326: KASSERT(ifp->if_sadl != NULL);
327:
1.88 thorpej 328: s = splnet();
1.81 thorpej 329: rtinit(ifa, RTM_DELETE, 0);
1.207 ! dyoung 330: ifa_remove(ifp, ifa);
1.81 thorpej 331:
332: ifp->if_sadl = NULL;
333:
334: ifnet_addrs[ifp->if_index] = NULL;
335: IFAFREE(ifa);
336: splx(s);
337: }
338:
339: /*
1.1 cgd 340: * Attach an interface to the
341: * list of "active" interfaces.
342: */
1.15 mycroft 343: void
1.163 thorpej 344: if_attach(struct ifnet *ifp)
1.1 cgd 345: {
1.102 atatat 346: int indexlim = 0;
1.1 cgd 347:
1.102 atatat 348: if (if_indexlim == 0) {
1.22 mycroft 349: TAILQ_INIT(&ifnet);
1.102 atatat 350: if_indexlim = 8;
351: }
1.22 mycroft 352: TAILQ_INIT(&ifp->if_addrlist);
1.21 mycroft 353: TAILQ_INSERT_TAIL(&ifnet, ifp, if_list);
1.102 atatat 354: ifp->if_index = if_index;
1.185 dyoung 355: if (ifindex2ifnet == NULL)
1.102 atatat 356: if_index++;
357: else
1.131 itojun 358: while (ifp->if_index < if_indexlim &&
359: ifindex2ifnet[ifp->if_index] != NULL) {
1.102 atatat 360: ++if_index;
361: if (if_index == 0)
362: if_index = 1;
363: /*
364: * If we hit USHRT_MAX, we skip back to 0 since
365: * there are a number of places where the value
366: * of if_index or if_index itself is compared
1.111 itojun 367: * to or stored in an unsigned short. By
1.102 atatat 368: * jumping back, we won't botch those assignments
369: * or comparisons.
370: */
371: else if (if_index == USHRT_MAX) {
372: /*
373: * However, if we have to jump back to
374: * zero *twice* without finding an empty
375: * slot in ifindex2ifnet[], then there
376: * there are too many (>65535) interfaces.
377: */
378: if (indexlim++)
379: panic("too many interfaces");
380: else
381: if_index = 1;
382: }
383: ifp->if_index = if_index;
384: }
1.49 itojun 385:
386: /*
387: * We have some arrays that should be indexed by if_index.
388: * since if_index will grow dynamically, they should grow too.
389: * struct ifadd **ifnet_addrs
390: * struct ifnet **ifindex2ifnet
391: */
1.185 dyoung 392: if (ifnet_addrs == NULL || ifindex2ifnet == NULL ||
1.53 thorpej 393: ifp->if_index >= if_indexlim) {
1.131 itojun 394: size_t m, n, oldlim;
1.183 christos 395: void *q;
1.154 perry 396:
1.131 itojun 397: oldlim = if_indexlim;
1.53 thorpej 398: while (ifp->if_index >= if_indexlim)
1.49 itojun 399: if_indexlim <<= 1;
400:
401: /* grow ifnet_addrs */
1.131 itojun 402: m = oldlim * sizeof(struct ifaddr *);
1.81 thorpej 403: n = if_indexlim * sizeof(struct ifaddr *);
1.203 dyoung 404: q = (void *)malloc(n, M_IFADDR, M_WAITOK|M_ZERO);
1.185 dyoung 405: if (ifnet_addrs != NULL) {
406: memcpy(q, ifnet_addrs, m);
1.183 christos 407: free((void *)ifnet_addrs, M_IFADDR);
1.1 cgd 408: }
1.49 itojun 409: ifnet_addrs = (struct ifaddr **)q;
410:
411: /* grow ifindex2ifnet */
1.131 itojun 412: m = oldlim * sizeof(struct ifnet *);
1.49 itojun 413: n = if_indexlim * sizeof(struct ifnet *);
1.203 dyoung 414: q = (void *)malloc(n, M_IFADDR, M_WAITOK|M_ZERO);
1.185 dyoung 415: if (ifindex2ifnet != NULL) {
416: memcpy(q, (void *)ifindex2ifnet, m);
1.183 christos 417: free((void *)ifindex2ifnet, M_IFADDR);
1.49 itojun 418: }
419: ifindex2ifnet = (struct ifnet **)q;
1.1 cgd 420: }
1.49 itojun 421:
1.53 thorpej 422: ifindex2ifnet[ifp->if_index] = ifp;
1.49 itojun 423:
1.1 cgd 424: /*
1.81 thorpej 425: * Link level name is allocated later by a separate call to
426: * if_alloc_sadl().
1.1 cgd 427: */
1.81 thorpej 428:
1.40 thorpej 429: if (ifp->if_snd.ifq_maxlen == 0)
1.94 itojun 430: ifp->if_snd.ifq_maxlen = ifqmaxlen;
1.42 is 431: ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
1.57 thorpej 432:
433: ifp->if_link_state = LINK_STATE_UNKNOWN;
434:
1.89 thorpej 435: ifp->if_capenable = 0;
1.97 thorpej 436: ifp->if_csum_flags_tx = 0;
437: ifp->if_csum_flags_rx = 0;
1.89 thorpej 438:
1.86 thorpej 439: #ifdef ALTQ
440: ifp->if_snd.altq_type = 0;
441: ifp->if_snd.altq_disc = NULL;
442: ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
443: ifp->if_snd.altq_tbr = NULL;
444: ifp->if_snd.altq_ifp = ifp;
445: #endif
446:
1.87 thorpej 447: #ifdef PFIL_HOOKS
448: ifp->if_pfil.ph_type = PFIL_TYPE_IFNET;
449: ifp->if_pfil.ph_ifnet = ifp;
450: if (pfil_head_register(&ifp->if_pfil) != 0)
451: printf("%s: WARNING: unable to register pfil hook\n",
452: ifp->if_xname);
1.144 yamt 453: (void)pfil_run_hooks(&if_pfil,
454: (struct mbuf **)PFIL_IFNET_ATTACH, ifp, PFIL_IFNET);
1.87 thorpej 455: #endif
456:
1.152 matt 457: if (!STAILQ_EMPTY(&domains))
1.147 tron 458: if_attachdomain1(ifp);
459:
1.107 itojun 460: /* Announce the interface. */
461: rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
462: }
463:
464: void
1.163 thorpej 465: if_attachdomain(void)
1.107 itojun 466: {
467: struct ifnet *ifp;
1.110 itojun 468: int s;
1.107 itojun 469:
1.110 itojun 470: s = splnet();
1.185 dyoung 471: IFNET_FOREACH(ifp)
1.107 itojun 472: if_attachdomain1(ifp);
1.110 itojun 473: splx(s);
1.107 itojun 474: }
475:
476: void
1.163 thorpej 477: if_attachdomain1(struct ifnet *ifp)
1.107 itojun 478: {
479: struct domain *dp;
1.109 itojun 480: int s;
481:
482: s = splnet();
1.107 itojun 483:
1.106 itojun 484: /* address family dependent data region */
485: memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata));
1.152 matt 486: DOMAIN_FOREACH(dp) {
1.185 dyoung 487: if (dp->dom_ifattach != NULL)
1.106 itojun 488: ifp->if_afdata[dp->dom_family] =
489: (*dp->dom_ifattach)(ifp);
490: }
1.109 itojun 491:
492: splx(s);
1.1 cgd 493: }
1.53 thorpej 494:
495: /*
496: * Deactivate an interface. This points all of the procedure
497: * handles at error stubs. May be called from interrupt context.
498: */
499: void
1.163 thorpej 500: if_deactivate(struct ifnet *ifp)
1.53 thorpej 501: {
502: int s;
503:
1.88 thorpej 504: s = splnet();
1.53 thorpej 505:
506: ifp->if_output = if_nulloutput;
507: ifp->if_input = if_nullinput;
508: ifp->if_start = if_nullstart;
509: ifp->if_ioctl = if_nullioctl;
1.75 thorpej 510: ifp->if_init = if_nullinit;
511: ifp->if_stop = if_nullstop;
1.53 thorpej 512: ifp->if_watchdog = if_nullwatchdog;
513: ifp->if_drain = if_nulldrain;
514:
515: /* No more packets may be enqueued. */
516: ifp->if_snd.ifq_maxlen = 0;
517:
518: splx(s);
519: }
520:
1.206 dyoung 521: void
522: if_purgeaddrs(struct ifnet *ifp, int family,
523: void (*purgeaddr)(struct ifaddr *))
524: {
525: struct ifaddr *ifa, *nifa;
526:
527: for (ifa = IFADDR_FIRST(ifp); ifa != NULL; ifa = nifa) {
528: nifa = IFADDR_NEXT(ifa);
529: if (ifa->ifa_addr->sa_family != family)
530: continue;
531: (*purgeaddr)(ifa);
532: }
533: }
534:
1.53 thorpej 535: /*
536: * Detach an interface from the list of "active" interfaces,
537: * freeing any resources as we go along.
538: *
539: * NOTE: This routine must be called with a valid thread context,
540: * as it may block.
541: */
542: void
1.163 thorpej 543: if_detach(struct ifnet *ifp)
1.53 thorpej 544: {
1.56 thorpej 545: struct socket so;
1.178 dyoung 546: struct ifaddr *ifa;
1.53 thorpej 547: #ifdef IFAREF_DEBUG
548: struct ifaddr *last_ifa = NULL;
549: #endif
1.56 thorpej 550: struct domain *dp;
1.141 matt 551: const struct protosw *pr;
1.56 thorpej 552: int s, i, family, purged;
1.53 thorpej 553:
1.56 thorpej 554: /*
555: * XXX It's kind of lame that we have to have the
556: * XXX socket structure...
557: */
558: memset(&so, 0, sizeof(so));
1.53 thorpej 559:
1.88 thorpej 560: s = splnet();
1.53 thorpej 561:
562: /*
563: * Do an if_down() to give protocols a chance to do something.
564: */
565: if_down(ifp);
1.86 thorpej 566:
567: #ifdef ALTQ
568: if (ALTQ_IS_ENABLED(&ifp->if_snd))
569: altq_disable(&ifp->if_snd);
570: if (ALTQ_IS_ATTACHED(&ifp->if_snd))
571: altq_detach(&ifp->if_snd);
1.87 thorpej 572: #endif
573:
1.166 liamjfoy 574:
575: #if NCARP > 0
576: /* Remove the interface from any carp group it is a part of. */
1.185 dyoung 577: if (ifp->if_carp != NULL && ifp->if_type != IFT_CARP)
1.166 liamjfoy 578: carp_ifdetach(ifp);
579: #endif
580:
1.53 thorpej 581: /*
582: * Rip all the addresses off the interface. This should make
583: * all of the routes go away.
1.178 dyoung 584: *
585: * pr_usrreq calls can remove an arbitrary number of ifaddrs
586: * from the list, including our "cursor", ifa. For safety,
587: * and to honor the TAILQ abstraction, I just restart the
588: * loop after each removal. Note that the loop will exit
589: * when all of the remaining ifaddrs belong to the AF_LINK
590: * family. I am counting on the historical fact that at
591: * least one pr_usrreq in each address domain removes at
592: * least one ifaddr.
1.53 thorpej 593: */
1.178 dyoung 594: again:
1.204 dyoung 595: IFADDR_FOREACH(ifa, ifp) {
1.56 thorpej 596: family = ifa->ifa_addr->sa_family;
1.53 thorpej 597: #ifdef IFAREF_DEBUG
598: printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
1.56 thorpej 599: ifa, family, ifa->ifa_refcnt);
1.53 thorpej 600: if (last_ifa != NULL && ifa == last_ifa)
1.56 thorpej 601: panic("if_detach: loop detected");
1.53 thorpej 602: last_ifa = ifa;
603: #endif
1.178 dyoung 604: if (family == AF_LINK)
1.118 itojun 605: continue;
606: dp = pffinddomain(family);
1.56 thorpej 607: #ifdef DIAGNOSTIC
1.118 itojun 608: if (dp == NULL)
609: panic("if_detach: no domain for AF %d",
610: family);
1.56 thorpej 611: #endif
1.160 gdt 612: /*
613: * XXX These PURGEIF calls are redundant with the
614: * purge-all-families calls below, but are left in for
615: * now both to make a smaller change, and to avoid
616: * unplanned interactions with clearing of
617: * ifp->if_addrlist.
618: */
1.118 itojun 619: purged = 0;
620: for (pr = dp->dom_protosw;
621: pr < dp->dom_protoswNPROTOSW; pr++) {
622: so.so_proto = pr;
623: if (pr->pr_usrreq != NULL) {
624: (void) (*pr->pr_usrreq)(&so,
625: PRU_PURGEIF, NULL, NULL,
1.162 christos 626: (struct mbuf *) ifp, curlwp);
1.118 itojun 627: purged = 1;
1.53 thorpej 628: }
1.118 itojun 629: }
630: if (purged == 0) {
631: /*
632: * XXX What's really the best thing to do
1.135 keihan 633: * XXX here? --thorpej@NetBSD.org
1.118 itojun 634: */
635: printf("if_detach: WARNING: AF %d not purged\n",
636: family);
1.207 ! dyoung 637: ifa_remove(ifp, ifa);
1.53 thorpej 638: }
1.178 dyoung 639: goto again;
1.53 thorpej 640: }
1.118 itojun 641:
642: if_free_sadl(ifp);
1.53 thorpej 643:
1.180 dyoung 644: /* Walk the routing table looking for stragglers. */
1.192 dyoung 645: for (i = 0; i <= AF_MAX; i++)
646: (void)rt_walktree(i, if_rt_walktree, ifp);
1.106 itojun 647:
1.152 matt 648: DOMAIN_FOREACH(dp) {
1.185 dyoung 649: if (dp->dom_ifdetach != NULL && ifp->if_afdata[dp->dom_family])
1.106 itojun 650: (*dp->dom_ifdetach)(ifp,
651: ifp->if_afdata[dp->dom_family]);
1.160 gdt 652:
653: /*
654: * One would expect multicast memberships (INET and
655: * INET6) on UDP sockets to be purged by the PURGEIF
656: * calls above, but if all addresses were removed from
657: * the interface prior to destruction, the calls will
658: * not be made (e.g. ppp, for which pppd(8) generally
659: * removes addresses before destroying the interface).
660: * Because there is no invariant that multicast
661: * memberships only exist for interfaces with IPv4
662: * addresses, we must call PURGEIF regardless of
663: * addresses. (Protocols which might store ifnet
664: * pointers are marked with PR_PURGEIF.)
665: */
1.185 dyoung 666: for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
1.160 gdt 667: so.so_proto = pr;
1.185 dyoung 668: if (pr->pr_usrreq != NULL && pr->pr_flags & PR_PURGEIF)
669: (void)(*pr->pr_usrreq)(&so, PRU_PURGEIF, NULL,
670: NULL, (struct mbuf *)ifp, curlwp);
1.160 gdt 671: }
1.53 thorpej 672: }
1.57 thorpej 673:
1.184 dyoung 674: #ifdef PFIL_HOOKS
675: (void)pfil_run_hooks(&if_pfil,
676: (struct mbuf **)PFIL_IFNET_DETACH, ifp, PFIL_IFNET);
677: (void)pfil_head_unregister(&ifp->if_pfil);
678: #endif
679:
1.57 thorpej 680: /* Announce that the interface is gone. */
681: rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1.93 itojun 682:
683: ifindex2ifnet[ifp->if_index] = NULL;
1.53 thorpej 684:
685: TAILQ_REMOVE(&ifnet, ifp, if_list);
686:
1.95 itojun 687: /*
1.168 matt 688: * remove packets that came from ifp, from software interrupt queues.
1.95 itojun 689: */
1.168 matt 690: DOMAIN_FOREACH(dp) {
691: for (i = 0; i < __arraycount(dp->dom_ifqueues); i++) {
692: if (dp->dom_ifqueues[i] == NULL)
693: break;
694: if_detach_queues(ifp, dp->dom_ifqueues[i]);
695: }
696: }
1.95 itojun 697:
1.53 thorpej 698: splx(s);
1.95 itojun 699: }
700:
701: static void
1.163 thorpej 702: if_detach_queues(struct ifnet *ifp, struct ifqueue *q)
1.95 itojun 703: {
704: struct mbuf *m, *prev, *next;
705:
706: prev = NULL;
1.185 dyoung 707: for (m = q->ifq_head; m != NULL; m = next) {
1.95 itojun 708: next = m->m_nextpkt;
709: #ifdef DIAGNOSTIC
1.96 itojun 710: if ((m->m_flags & M_PKTHDR) == 0) {
711: prev = m;
1.95 itojun 712: continue;
1.96 itojun 713: }
1.95 itojun 714: #endif
1.96 itojun 715: if (m->m_pkthdr.rcvif != ifp) {
716: prev = m;
1.95 itojun 717: continue;
1.96 itojun 718: }
1.95 itojun 719:
1.185 dyoung 720: if (prev != NULL)
1.95 itojun 721: prev->m_nextpkt = m->m_nextpkt;
722: else
723: q->ifq_head = m->m_nextpkt;
724: if (q->ifq_tail == m)
725: q->ifq_tail = prev;
726: q->ifq_len--;
727:
728: m->m_nextpkt = NULL;
729: m_freem(m);
730: IF_DROP(q);
731: }
1.53 thorpej 732: }
733:
734: /*
735: * Callback for a radix tree walk to delete all references to an
736: * ifnet.
737: */
1.163 thorpej 738: static int
1.192 dyoung 739: if_rt_walktree(struct rtentry *rt, void *v)
1.53 thorpej 740: {
1.55 itojun 741: struct ifnet *ifp = (struct ifnet *)v;
1.53 thorpej 742: int error;
743:
1.185 dyoung 744: if (rt->rt_ifp != ifp)
745: return 0;
746:
747: /* Delete the entry. */
748: ++rt->rt_refcnt;
1.194 dyoung 749: error = rtrequest(RTM_DELETE, rt_getkey(rt), rt->rt_gateway,
1.185 dyoung 750: rt_mask(rt), rt->rt_flags, NULL);
751: KASSERT((rt->rt_flags & RTF_UP) == 0);
752: rt->rt_ifp = NULL;
753: RTFREE(rt);
754: if (error != 0)
755: printf("%s: warning: unable to delete rtentry @ %p, "
756: "error = %d\n", ifp->if_xname, rt, error);
757: return 0;
1.53 thorpej 758: }
759:
1.1 cgd 760: /*
1.63 thorpej 761: * Create a clone network interface.
762: */
763: int
1.163 thorpej 764: if_clone_create(const char *name)
1.63 thorpej 765: {
766: struct if_clone *ifc;
767: int unit;
768:
769: ifc = if_clone_lookup(name, &unit);
770: if (ifc == NULL)
1.185 dyoung 771: return EINVAL;
1.63 thorpej 772:
773: if (ifunit(name) != NULL)
1.185 dyoung 774: return EEXIST;
1.63 thorpej 775:
1.185 dyoung 776: return (*ifc->ifc_create)(ifc, unit);
1.63 thorpej 777: }
778:
779: /*
780: * Destroy a clone network interface.
781: */
782: int
1.163 thorpej 783: if_clone_destroy(const char *name)
1.63 thorpej 784: {
785: struct if_clone *ifc;
786: struct ifnet *ifp;
787:
788: ifc = if_clone_lookup(name, NULL);
789: if (ifc == NULL)
1.185 dyoung 790: return EINVAL;
1.63 thorpej 791:
792: ifp = ifunit(name);
793: if (ifp == NULL)
1.185 dyoung 794: return ENXIO;
1.63 thorpej 795:
796: if (ifc->ifc_destroy == NULL)
1.185 dyoung 797: return EOPNOTSUPP;
1.63 thorpej 798:
1.185 dyoung 799: return (*ifc->ifc_destroy)(ifp);
1.63 thorpej 800: }
801:
802: /*
803: * Look up a network interface cloner.
804: */
1.163 thorpej 805: static struct if_clone *
806: if_clone_lookup(const char *name, int *unitp)
1.63 thorpej 807: {
808: struct if_clone *ifc;
809: const char *cp;
1.128 itojun 810: int unit;
1.63 thorpej 811:
1.128 itojun 812: /* separate interface name from unit */
813: for (cp = name;
1.130 itojun 814: cp - name < IFNAMSIZ && *cp && (*cp < '0' || *cp > '9');
1.128 itojun 815: cp++)
816: continue;
817:
818: if (cp == name || cp - name == IFNAMSIZ || !*cp)
1.185 dyoung 819: return NULL; /* No name or unit number */
1.128 itojun 820:
821: LIST_FOREACH(ifc, &if_cloners, ifc_list) {
822: if (strlen(ifc->ifc_name) == cp - name &&
1.185 dyoung 823: strncmp(name, ifc->ifc_name, cp - name) == 0)
1.128 itojun 824: break;
1.63 thorpej 825: }
826:
1.128 itojun 827: if (ifc == NULL)
1.185 dyoung 828: return NULL;
1.63 thorpej 829:
1.128 itojun 830: unit = 0;
1.129 itojun 831: while (cp - name < IFNAMSIZ && *cp) {
832: if (*cp < '0' || *cp > '9' || unit > INT_MAX / 10) {
1.63 thorpej 833: /* Bogus unit number. */
1.185 dyoung 834: return NULL;
1.63 thorpej 835: }
1.128 itojun 836: unit = (unit * 10) + (*cp++ - '0');
1.63 thorpej 837: }
838:
839: if (unitp != NULL)
1.128 itojun 840: *unitp = unit;
1.185 dyoung 841: return ifc;
1.63 thorpej 842: }
843:
844: /*
845: * Register a network interface cloner.
846: */
847: void
1.163 thorpej 848: if_clone_attach(struct if_clone *ifc)
1.63 thorpej 849: {
850:
851: LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
1.67 thorpej 852: if_cloners_count++;
1.63 thorpej 853: }
854:
855: /*
856: * Unregister a network interface cloner.
857: */
858: void
1.163 thorpej 859: if_clone_detach(struct if_clone *ifc)
1.63 thorpej 860: {
861:
862: LIST_REMOVE(ifc, ifc_list);
1.67 thorpej 863: if_cloners_count--;
864: }
865:
866: /*
867: * Provide list of interface cloners to userspace.
868: */
1.163 thorpej 869: static int
870: if_clone_list(struct if_clonereq *ifcr)
1.67 thorpej 871: {
872: char outbuf[IFNAMSIZ], *dst;
873: struct if_clone *ifc;
874: int count, error = 0;
875:
876: ifcr->ifcr_total = if_cloners_count;
877: if ((dst = ifcr->ifcr_buffer) == NULL) {
878: /* Just asking how many there are. */
1.185 dyoung 879: return 0;
1.67 thorpej 880: }
881:
882: if (ifcr->ifcr_count < 0)
1.185 dyoung 883: return EINVAL;
1.67 thorpej 884:
885: count = (if_cloners_count < ifcr->ifcr_count) ?
886: if_cloners_count : ifcr->ifcr_count;
887:
888: for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
889: ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
1.175 christos 890: (void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf));
891: if (outbuf[sizeof(outbuf) - 1] != '\0')
892: return ENAMETOOLONG;
1.172 christos 893: error = copyout(outbuf, dst, sizeof(outbuf));
1.185 dyoung 894: if (error != 0)
1.67 thorpej 895: break;
896: }
897:
1.185 dyoung 898: return error;
1.63 thorpej 899: }
900:
1.207 ! dyoung 901: void
! 902: ifa_insert(struct ifnet *ifp, struct ifaddr *ifa)
! 903: {
! 904: ifa->ifa_ifp = ifp;
! 905: TAILQ_INSERT_HEAD(&ifp->if_addrlist, ifa, ifa_list);
! 906: IFAREF(ifa);
! 907: }
! 908:
! 909: void
! 910: ifa_remove(struct ifnet *ifp, struct ifaddr *ifa)
! 911: {
! 912: KASSERT(ifa->ifa_ifp == ifp);
! 913: TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
! 914: IFAFREE(ifa);
! 915: }
! 916:
1.194 dyoung 917: static inline int
918: equal(const struct sockaddr *sa1, const struct sockaddr *sa2)
919: {
920: return sockaddr_cmp(sa1, sa2) == 0;
921: }
922:
1.63 thorpej 923: /*
1.1 cgd 924: * Locate an interface based on a complete address.
925: */
926: /*ARGSUSED*/
927: struct ifaddr *
1.163 thorpej 928: ifa_ifwithaddr(const struct sockaddr *addr)
1.1 cgd 929: {
1.61 augustss 930: struct ifnet *ifp;
931: struct ifaddr *ifa;
1.1 cgd 932:
1.185 dyoung 933: IFNET_FOREACH(ifp) {
1.53 thorpej 934: if (ifp->if_output == if_nulloutput)
1.1 cgd 935: continue;
1.204 dyoung 936: IFADDR_FOREACH(ifa, ifp) {
1.53 thorpej 937: if (ifa->ifa_addr->sa_family != addr->sa_family)
938: continue;
939: if (equal(addr, ifa->ifa_addr))
1.185 dyoung 940: return ifa;
1.53 thorpej 941: if ((ifp->if_flags & IFF_BROADCAST) &&
942: ifa->ifa_broadaddr &&
943: /* IP6 doesn't have broadcast */
944: ifa->ifa_broadaddr->sa_len != 0 &&
945: equal(ifa->ifa_broadaddr, addr))
1.185 dyoung 946: return ifa;
1.53 thorpej 947: }
1.1 cgd 948: }
1.185 dyoung 949: return NULL;
1.1 cgd 950: }
1.49 itojun 951:
1.1 cgd 952: /*
953: * Locate the point to point interface with a given destination address.
954: */
955: /*ARGSUSED*/
956: struct ifaddr *
1.163 thorpej 957: ifa_ifwithdstaddr(const struct sockaddr *addr)
1.1 cgd 958: {
1.61 augustss 959: struct ifnet *ifp;
960: struct ifaddr *ifa;
1.1 cgd 961:
1.185 dyoung 962: IFNET_FOREACH(ifp) {
1.53 thorpej 963: if (ifp->if_output == if_nulloutput)
964: continue;
1.185 dyoung 965: if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
966: continue;
1.204 dyoung 967: IFADDR_FOREACH(ifa, ifp) {
1.185 dyoung 968: if (ifa->ifa_addr->sa_family != addr->sa_family ||
969: ifa->ifa_dstaddr == NULL)
970: continue;
971: if (equal(addr, ifa->ifa_dstaddr))
972: return ifa;
1.53 thorpej 973: }
1.1 cgd 974: }
1.185 dyoung 975: return NULL;
1.1 cgd 976: }
977:
978: /*
979: * Find an interface on a specific network. If many, choice
1.15 mycroft 980: * is most specific found.
1.1 cgd 981: */
982: struct ifaddr *
1.163 thorpej 983: ifa_ifwithnet(const struct sockaddr *addr)
1.1 cgd 984: {
1.61 augustss 985: struct ifnet *ifp;
986: struct ifaddr *ifa;
1.140 matt 987: const struct sockaddr_dl *sdl;
1.15 mycroft 988: struct ifaddr *ifa_maybe = 0;
1.1 cgd 989: u_int af = addr->sa_family;
1.171 pooka 990: const char *addr_data = addr->sa_data, *cplim;
1.1 cgd 991:
992: if (af == AF_LINK) {
1.195 dyoung 993: sdl = satocsdl(addr);
1.137 itojun 994: if (sdl->sdl_index && sdl->sdl_index < if_indexlim &&
995: ifindex2ifnet[sdl->sdl_index] &&
1.53 thorpej 996: ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput)
1.185 dyoung 997: return ifnet_addrs[sdl->sdl_index];
1.1 cgd 998: }
1.51 bouyer 999: #ifdef NETATALK
1000: if (af == AF_APPLETALK) {
1.140 matt 1001: const struct sockaddr_at *sat, *sat2;
1.158 christos 1002: sat = (const struct sockaddr_at *)addr;
1.185 dyoung 1003: IFNET_FOREACH(ifp) {
1.53 thorpej 1004: if (ifp->if_output == if_nulloutput)
1005: continue;
1.158 christos 1006: ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp);
1.62 bouyer 1007: if (ifa == NULL)
1008: continue;
1009: sat2 = (struct sockaddr_at *)ifa->ifa_addr;
1010: if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
1.185 dyoung 1011: return ifa; /* exact match */
1.62 bouyer 1012: if (ifa_maybe == NULL) {
1.112 wiz 1013: /* else keep the if with the right range */
1.62 bouyer 1014: ifa_maybe = ifa;
1015: }
1.51 bouyer 1016: }
1.185 dyoung 1017: return ifa_maybe;
1.51 bouyer 1018: }
1019: #endif
1.185 dyoung 1020: IFNET_FOREACH(ifp) {
1.53 thorpej 1021: if (ifp->if_output == if_nulloutput)
1022: continue;
1.204 dyoung 1023: IFADDR_FOREACH(ifa, ifp) {
1.171 pooka 1024: const char *cp, *cp2, *cp3;
1.15 mycroft 1025:
1026: if (ifa->ifa_addr->sa_family != af ||
1.185 dyoung 1027: ifa->ifa_netmask == NULL)
1.53 thorpej 1028: next: continue;
1.15 mycroft 1029: cp = addr_data;
1030: cp2 = ifa->ifa_addr->sa_data;
1031: cp3 = ifa->ifa_netmask->sa_data;
1.171 pooka 1032: cplim = (const char *)ifa->ifa_netmask +
1.53 thorpej 1033: ifa->ifa_netmask->sa_len;
1034: while (cp3 < cplim) {
1035: if ((*cp++ ^ *cp2++) & *cp3++) {
1036: /* want to continue for() loop */
1.32 mrg 1037: goto next;
1.53 thorpej 1038: }
1039: }
1.185 dyoung 1040: if (ifa_maybe == NULL ||
1.183 christos 1041: rn_refines((void *)ifa->ifa_netmask,
1042: (void *)ifa_maybe->ifa_netmask))
1.15 mycroft 1043: ifa_maybe = ifa;
1044: }
1.53 thorpej 1045: }
1.185 dyoung 1046: return ifa_maybe;
1.26 mrg 1047: }
1.53 thorpej 1048:
1.26 mrg 1049: /*
1050: * Find the interface of the addresss.
1051: */
1052: struct ifaddr *
1.163 thorpej 1053: ifa_ifwithladdr(const struct sockaddr *addr)
1.26 mrg 1054: {
1055: struct ifaddr *ia;
1056:
1.53 thorpej 1057: if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
1058: (ia = ifa_ifwithnet(addr)))
1.185 dyoung 1059: return ia;
1060: return NULL;
1.1 cgd 1061: }
1062:
1063: /*
1064: * Find an interface using a specific address family
1065: */
1066: struct ifaddr *
1.163 thorpej 1067: ifa_ifwithaf(int af)
1.1 cgd 1068: {
1.61 augustss 1069: struct ifnet *ifp;
1070: struct ifaddr *ifa;
1.1 cgd 1071:
1.185 dyoung 1072: IFNET_FOREACH(ifp) {
1.53 thorpej 1073: if (ifp->if_output == if_nulloutput)
1074: continue;
1.204 dyoung 1075: IFADDR_FOREACH(ifa, ifp) {
1.21 mycroft 1076: if (ifa->ifa_addr->sa_family == af)
1.178 dyoung 1077: return ifa;
1.53 thorpej 1078: }
1079: }
1.178 dyoung 1080: return NULL;
1.1 cgd 1081: }
1082:
1083: /*
1084: * Find an interface address specific to an interface best matching
1085: * a given address.
1086: */
1087: struct ifaddr *
1.163 thorpej 1088: ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
1.1 cgd 1089: {
1.61 augustss 1090: struct ifaddr *ifa;
1.140 matt 1091: const char *cp, *cp2, *cp3;
1092: const char *cplim;
1.1 cgd 1093: struct ifaddr *ifa_maybe = 0;
1094: u_int af = addr->sa_family;
1095:
1.53 thorpej 1096: if (ifp->if_output == if_nulloutput)
1.185 dyoung 1097: return NULL;
1.53 thorpej 1098:
1.1 cgd 1099: if (af >= AF_MAX)
1.185 dyoung 1100: return NULL;
1.53 thorpej 1101:
1.204 dyoung 1102: IFADDR_FOREACH(ifa, ifp) {
1.1 cgd 1103: if (ifa->ifa_addr->sa_family != af)
1104: continue;
1105: ifa_maybe = ifa;
1.185 dyoung 1106: if (ifa->ifa_netmask == NULL) {
1.1 cgd 1107: if (equal(addr, ifa->ifa_addr) ||
1.53 thorpej 1108: (ifa->ifa_dstaddr &&
1109: equal(addr, ifa->ifa_dstaddr)))
1.185 dyoung 1110: return ifa;
1.1 cgd 1111: continue;
1112: }
1113: cp = addr->sa_data;
1114: cp2 = ifa->ifa_addr->sa_data;
1115: cp3 = ifa->ifa_netmask->sa_data;
1116: cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1.53 thorpej 1117: for (; cp3 < cplim; cp3++) {
1.1 cgd 1118: if ((*cp++ ^ *cp2++) & *cp3)
1119: break;
1.53 thorpej 1120: }
1.1 cgd 1121: if (cp3 == cplim)
1.185 dyoung 1122: return ifa;
1.1 cgd 1123: }
1.185 dyoung 1124: return ifa_maybe;
1.1 cgd 1125: }
1.9 mycroft 1126:
1.1 cgd 1127: /*
1128: * Default action when installing a route with a Link Level gateway.
1129: * Lookup an appropriate real ifa to point to.
1130: * This should be moved to /sys/net/link.c eventually.
1131: */
1.15 mycroft 1132: void
1.163 thorpej 1133: link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1.1 cgd 1134: {
1.61 augustss 1135: struct ifaddr *ifa;
1.194 dyoung 1136: const struct sockaddr *dst;
1.15 mycroft 1137: struct ifnet *ifp;
1.1 cgd 1138:
1.185 dyoung 1139: if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == NULL) ||
1.194 dyoung 1140: ((ifp = ifa->ifa_ifp) == NULL) || ((dst = rt_getkey(rt)) == NULL))
1.1 cgd 1141: return;
1.24 christos 1142: if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
1.176 dyoung 1143: rt_replace_ifa(rt, ifa);
1.1 cgd 1144: if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1.82 itojun 1145: ifa->ifa_rtrequest(cmd, rt, info);
1.1 cgd 1146: }
1147: }
1148:
1149: /*
1.159 dyoung 1150: * Handle a change in the interface link state.
1151: */
1152: void
1153: if_link_state_change(struct ifnet *ifp, int link_state)
1154: {
1.185 dyoung 1155: if (ifp->if_link_state == link_state)
1156: return;
1157: ifp->if_link_state = link_state;
1.159 dyoung 1158: /* Notify that the link state has changed. */
1.185 dyoung 1159: rt_ifmsg(ifp);
1.166 liamjfoy 1160: #if NCARP > 0
1.185 dyoung 1161: if (ifp->if_carp)
1162: carp_carpdev_state(ifp);
1.166 liamjfoy 1163: #endif
1.159 dyoung 1164: }
1165:
1166: /*
1.1 cgd 1167: * Mark an interface down and notify protocols of
1168: * the transition.
1.23 mycroft 1169: * NOTE: must be called at splsoftnet or equivalent.
1.1 cgd 1170: */
1.15 mycroft 1171: void
1.163 thorpej 1172: if_down(struct ifnet *ifp)
1.1 cgd 1173: {
1.61 augustss 1174: struct ifaddr *ifa;
1.1 cgd 1175:
1176: ifp->if_flags &= ~IFF_UP;
1.91 itojun 1177: microtime(&ifp->if_lastchange);
1.204 dyoung 1178: IFADDR_FOREACH(ifa, ifp)
1.1 cgd 1179: pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1.78 thorpej 1180: IFQ_PURGE(&ifp->if_snd);
1.166 liamjfoy 1181: #if NCARP > 0
1182: if (ifp->if_carp)
1183: carp_carpdev_state(ifp);
1184: #endif
1.15 mycroft 1185: rt_ifmsg(ifp);
1186: }
1187:
1188: /*
1189: * Mark an interface up and notify protocols of
1190: * the transition.
1.23 mycroft 1191: * NOTE: must be called at splsoftnet or equivalent.
1.15 mycroft 1192: */
1193: void
1.163 thorpej 1194: if_up(struct ifnet *ifp)
1.15 mycroft 1195: {
1.24 christos 1196: #ifdef notyet
1.61 augustss 1197: struct ifaddr *ifa;
1.24 christos 1198: #endif
1.15 mycroft 1199:
1200: ifp->if_flags |= IFF_UP;
1.91 itojun 1201: microtime(&ifp->if_lastchange);
1.15 mycroft 1202: #ifdef notyet
1203: /* this has no effect on IP, and will kill all ISO connections XXX */
1.204 dyoung 1204: IFADDR_FOREACH(ifa, ifp)
1.15 mycroft 1205: pfctlinput(PRC_IFUP, ifa->ifa_addr);
1206: #endif
1.166 liamjfoy 1207: #if NCARP > 0
1208: if (ifp->if_carp)
1209: carp_carpdev_state(ifp);
1210: #endif
1.15 mycroft 1211: rt_ifmsg(ifp);
1.49 itojun 1212: #ifdef INET6
1213: in6_if_up(ifp);
1214: #endif
1.1 cgd 1215: }
1216:
1217: /*
1218: * Handle interface watchdog timer routines. Called
1219: * from softclock, we decrement timers (if set) and
1220: * call the appropriate interface routine on expiration.
1221: */
1.4 andrew 1222: void
1.177 christos 1223: if_slowtimo(void *arg)
1.1 cgd 1224: {
1.61 augustss 1225: struct ifnet *ifp;
1.88 thorpej 1226: int s = splnet();
1.1 cgd 1227:
1.185 dyoung 1228: IFNET_FOREACH(ifp) {
1.1 cgd 1229: if (ifp->if_timer == 0 || --ifp->if_timer)
1230: continue;
1.185 dyoung 1231: if (ifp->if_watchdog != NULL)
1.34 thorpej 1232: (*ifp->if_watchdog)(ifp);
1.1 cgd 1233: }
1234: splx(s);
1.185 dyoung 1235: callout_reset(&if_slowtimo_ch, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1.65 thorpej 1236: }
1237:
1238: /*
1239: * Set/clear promiscuous mode on interface ifp based on the truth value
1240: * of pswitch. The calls are reference counted so that only the first
1241: * "on" request actually has an effect, as does the final "off" request.
1242: * Results are undefined if the "off" and "on" requests are not matched.
1243: */
1244: int
1.163 thorpej 1245: ifpromisc(struct ifnet *ifp, int pswitch)
1.65 thorpej 1246: {
1247: int pcount, ret;
1248: short flags;
1249: struct ifreq ifr;
1250:
1251: pcount = ifp->if_pcount;
1252: flags = ifp->if_flags;
1253: if (pswitch) {
1254: /*
1.71 thorpej 1255: * Allow the device to be "placed" into promiscuous
1256: * mode even if it is not configured up. It will
1257: * consult IFF_PROMISC when it is is brought up.
1.65 thorpej 1258: */
1.68 pk 1259: if (ifp->if_pcount++ != 0)
1.185 dyoung 1260: return 0;
1.65 thorpej 1261: ifp->if_flags |= IFF_PROMISC;
1.71 thorpej 1262: if ((ifp->if_flags & IFF_UP) == 0)
1.185 dyoung 1263: return 0;
1.65 thorpej 1264: } else {
1265: if (--ifp->if_pcount > 0)
1.185 dyoung 1266: return 0;
1.65 thorpej 1267: ifp->if_flags &= ~IFF_PROMISC;
1268: /*
1269: * If the device is not configured up, we should not need to
1270: * turn off promiscuous mode (device should have turned it
1271: * off when interface went down; and will look at IFF_PROMISC
1272: * again next time interface comes up).
1273: */
1274: if ((ifp->if_flags & IFF_UP) == 0)
1.185 dyoung 1275: return 0;
1.65 thorpej 1276: }
1277: memset(&ifr, 0, sizeof(ifr));
1278: ifr.ifr_flags = ifp->if_flags;
1.183 christos 1279: ret = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (void *) &ifr);
1.65 thorpej 1280: /* Restore interface state if not successful. */
1281: if (ret != 0) {
1282: ifp->if_pcount = pcount;
1283: ifp->if_flags = flags;
1284: }
1.185 dyoung 1285: return ret;
1.1 cgd 1286: }
1287:
1288: /*
1289: * Map interface name to
1290: * interface structure pointer.
1291: */
1292: struct ifnet *
1.163 thorpej 1293: ifunit(const char *name)
1.1 cgd 1294: {
1.61 augustss 1295: struct ifnet *ifp;
1.105 matt 1296: const char *cp = name;
1297: u_int unit = 0;
1298: u_int i;
1299:
1300: /*
1301: * If the entire name is a number, treat it as an ifindex.
1302: */
1303: for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
1304: unit = unit * 10 + (*cp - '0');
1305: }
1306:
1307: /*
1308: * If the number took all of the name, then it's a valid ifindex.
1309: */
1310: if (i == IFNAMSIZ || (cp != name && *cp == '\0')) {
1.137 itojun 1311: if (unit >= if_indexlim)
1.185 dyoung 1312: return NULL;
1.105 matt 1313: ifp = ifindex2ifnet[unit];
1314: if (ifp == NULL || ifp->if_output == if_nulloutput)
1.185 dyoung 1315: return NULL;
1316: return ifp;
1.105 matt 1317: }
1.34 thorpej 1318:
1.185 dyoung 1319: IFNET_FOREACH(ifp) {
1.53 thorpej 1320: if (ifp->if_output == if_nulloutput)
1321: continue;
1322: if (strcmp(ifp->if_xname, name) == 0)
1.185 dyoung 1323: return ifp;
1.53 thorpej 1324: }
1.185 dyoung 1325: return NULL;
1.1 cgd 1326: }
1.49 itojun 1327:
1.1 cgd 1328: /*
1329: * Interface ioctls.
1330: */
1.15 mycroft 1331: int
1.183 christos 1332: ifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l)
1.1 cgd 1333: {
1.61 augustss 1334: struct ifnet *ifp;
1335: struct ifreq *ifr;
1.89 thorpej 1336: struct ifcapreq *ifcr;
1.105 matt 1337: struct ifdatareq *ifdr;
1.89 thorpej 1338: int s, error = 0;
1.191 christos 1339: #if defined(COMPAT_OSOCK) || defined(COMPAT_OIFREQ)
1340: u_long ocmd = cmd;
1341: #endif
1.49 itojun 1342: short oif_flags;
1.186 christos 1343: #ifdef COMPAT_OIFREQ
1344: struct ifreq ifrb;
1.187 xtraeme 1345: struct oifreq *oifr = NULL;
1.186 christos 1346: #endif
1.1 cgd 1347:
1348: switch (cmd) {
1.186 christos 1349: #ifdef COMPAT_OIFREQ
1350: case OSIOCGIFCONF:
1351: case OOSIOCGIFCONF:
1352: return compat_ifconf(cmd, data);
1353: #endif
1.1 cgd 1354: case SIOCGIFCONF:
1.185 dyoung 1355: return ifconf(cmd, data);
1.1 cgd 1356: }
1.191 christos 1357:
1.186 christos 1358: #ifdef COMPAT_OIFREQ
1.196 skd 1359: cmd = compat_cvtcmd(cmd);
1.186 christos 1360: if (cmd != ocmd) {
1361: oifr = data;
1362: data = ifr = &ifrb;
1363: ifreqo2n(oifr, ifr);
1364: } else
1365: #endif
1366: ifr = data;
1367: ifcr = data;
1368: ifdr = data;
1.63 thorpej 1369:
1.174 elad 1370: ifp = ifunit(ifr->ifr_name);
1371:
1.63 thorpej 1372: switch (cmd) {
1373: case SIOCIFCREATE:
1374: case SIOCIFDESTROY:
1.185 dyoung 1375: if (l != NULL) {
1.174 elad 1376: error = kauth_authorize_network(l->l_cred,
1377: KAUTH_NETWORK_INTERFACE,
1378: KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
1379: (void *)cmd, NULL);
1.185 dyoung 1380: if (error != 0)
1.151 yamt 1381: return error;
1382: }
1.185 dyoung 1383: return (cmd == SIOCIFCREATE) ?
1.64 thorpej 1384: if_clone_create(ifr->ifr_name) :
1.185 dyoung 1385: if_clone_destroy(ifr->ifr_name);
1.67 thorpej 1386:
1387: case SIOCIFGCLONERS:
1.185 dyoung 1388: return if_clone_list((struct if_clonereq *)data);
1.63 thorpej 1389: }
1390:
1.185 dyoung 1391: if (ifp == NULL)
1392: return ENXIO;
1.151 yamt 1393:
1394: switch (cmd) {
1395: case SIOCSIFFLAGS:
1396: case SIOCSIFCAP:
1397: case SIOCSIFMETRIC:
1398: case SIOCZIFDATA:
1399: case SIOCSIFMTU:
1400: case SIOCSIFPHYADDR:
1401: case SIOCDIFPHYADDR:
1402: #ifdef INET6
1403: case SIOCSIFPHYADDR_IN6:
1404: #endif
1405: case SIOCSLIFPHYADDR:
1406: case SIOCADDMULTI:
1407: case SIOCDELMULTI:
1408: case SIOCSIFMEDIA:
1.154 perry 1409: case SIOCSDRVSPEC:
1.196 skd 1410: case SIOCG80211:
1411: case SIOCS80211:
1.151 yamt 1412: case SIOCS80211NWID:
1413: case SIOCS80211NWKEY:
1414: case SIOCS80211POWER:
1415: case SIOCS80211BSSID:
1416: case SIOCS80211CHANNEL:
1.185 dyoung 1417: if (l != NULL) {
1.174 elad 1418: error = kauth_authorize_network(l->l_cred,
1419: KAUTH_NETWORK_INTERFACE,
1420: KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
1421: (void *)cmd, NULL);
1.185 dyoung 1422: if (error != 0)
1.151 yamt 1423: return error;
1424: }
1425: }
1426:
1.49 itojun 1427: oif_flags = ifp->if_flags;
1.1 cgd 1428: switch (cmd) {
1429:
1430: case SIOCGIFFLAGS:
1431: ifr->ifr_flags = ifp->if_flags;
1432: break;
1433:
1434: case SIOCGIFMETRIC:
1435: ifr->ifr_metric = ifp->if_metric;
1436: break;
1.7 hpeyerl 1437:
1.36 mycroft 1438: case SIOCGIFMTU:
1.37 cgd 1439: ifr->ifr_mtu = ifp->if_mtu;
1.80 thorpej 1440: break;
1441:
1442: case SIOCGIFDLT:
1443: ifr->ifr_dlt = ifp->if_dlt;
1.37 cgd 1444: break;
1.36 mycroft 1445:
1.1 cgd 1446: case SIOCSIFFLAGS:
1447: if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
1.89 thorpej 1448: s = splnet();
1.1 cgd 1449: if_down(ifp);
1450: splx(s);
1451: }
1.15 mycroft 1452: if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
1.89 thorpej 1453: s = splnet();
1.15 mycroft 1454: if_up(ifp);
1455: splx(s);
1456: }
1.1 cgd 1457: ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1458: (ifr->ifr_flags &~ IFF_CANTCHANGE);
1459: if (ifp->if_ioctl)
1.185 dyoung 1460: (void)(*ifp->if_ioctl)(ifp, cmd, data);
1.1 cgd 1461: break;
1462:
1.89 thorpej 1463: case SIOCGIFCAP:
1464: ifcr->ifcr_capabilities = ifp->if_capabilities;
1465: ifcr->ifcr_capenable = ifp->if_capenable;
1466: break;
1467:
1468: case SIOCSIFCAP:
1469: if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
1.185 dyoung 1470: return EINVAL;
1.89 thorpej 1471: if (ifp->if_ioctl == NULL)
1.185 dyoung 1472: return EOPNOTSUPP;
1.89 thorpej 1473:
1474: /* Must prevent race with packet reception here. */
1475: s = splnet();
1476: if (ifcr->ifcr_capenable != ifp->if_capenable) {
1477: struct ifreq ifrq;
1478:
1479: ifrq.ifr_flags = ifp->if_flags;
1480: ifp->if_capenable = ifcr->ifcr_capenable;
1481:
1482: /* Pre-compute the checksum flags mask. */
1.97 thorpej 1483: ifp->if_csum_flags_tx = 0;
1484: ifp->if_csum_flags_rx = 0;
1.157 yamt 1485: if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) {
1.97 thorpej 1486: ifp->if_csum_flags_tx |= M_CSUM_IPv4;
1.157 yamt 1487: }
1488: if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) {
1.97 thorpej 1489: ifp->if_csum_flags_rx |= M_CSUM_IPv4;
1490: }
1491:
1.157 yamt 1492: if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) {
1.97 thorpej 1493: ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
1.157 yamt 1494: }
1495: if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) {
1.97 thorpej 1496: ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
1.157 yamt 1497: }
1.97 thorpej 1498:
1.157 yamt 1499: if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) {
1.97 thorpej 1500: ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
1.157 yamt 1501: }
1502: if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) {
1.97 thorpej 1503: ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
1.157 yamt 1504: }
1.97 thorpej 1505:
1.157 yamt 1506: if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) {
1.97 thorpej 1507: ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
1.157 yamt 1508: }
1509: if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) {
1.97 thorpej 1510: ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
1511: }
1512:
1.157 yamt 1513: if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) {
1.97 thorpej 1514: ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
1.157 yamt 1515: }
1516: if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) {
1.97 thorpej 1517: ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
1518: }
1.89 thorpej 1519:
1520: /*
1521: * Only kick the interface if it's up. If it's
1522: * not up now, it will notice the cap enables
1523: * when it is brought up later.
1524: */
1525: if (ifp->if_flags & IFF_UP)
1.185 dyoung 1526: (void)(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS,
1527: (void *)&ifrq);
1.89 thorpej 1528: }
1529: splx(s);
1530: break;
1531:
1.1 cgd 1532: case SIOCSIFMETRIC:
1533: ifp->if_metric = ifr->ifr_metric;
1.105 matt 1534: break;
1535:
1536: case SIOCGIFDATA:
1537: ifdr->ifdr_data = ifp->if_data;
1538: break;
1539:
1540: case SIOCZIFDATA:
1541: ifdr->ifdr_data = ifp->if_data;
1542: /*
1543: * Assumes that the volatile counters that can be
1544: * zero'ed are at the end of if_data.
1545: */
1546: memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) -
1547: offsetof(struct if_data, ifi_ipackets));
1.5 deraadt 1548: break;
1549:
1.36 mycroft 1550: case SIOCSIFMTU:
1.49 itojun 1551: {
1552: u_long oldmtu = ifp->if_mtu;
1553:
1554: if (ifp->if_ioctl == NULL)
1.185 dyoung 1555: return EOPNOTSUPP;
1.49 itojun 1556: error = (*ifp->if_ioctl)(ifp, cmd, data);
1557:
1558: /*
1559: * If the link MTU changed, do network layer specific procedure.
1560: */
1561: if (ifp->if_mtu != oldmtu) {
1562: #ifdef INET6
1563: nd6_setmtu(ifp);
1.154 perry 1564: #endif
1.49 itojun 1565: }
1566: break;
1567: }
1.72 thorpej 1568: case SIOCSIFPHYADDR:
1569: case SIOCDIFPHYADDR:
1570: #ifdef INET6
1571: case SIOCSIFPHYADDR_IN6:
1572: #endif
1.85 itojun 1573: case SIOCSLIFPHYADDR:
1.15 mycroft 1574: case SIOCADDMULTI:
1575: case SIOCDELMULTI:
1.39 thorpej 1576: case SIOCSIFMEDIA:
1.74 itojun 1577: case SIOCGIFPSRCADDR:
1578: case SIOCGIFPDSTADDR:
1.85 itojun 1579: case SIOCGLIFPHYADDR:
1.39 thorpej 1580: case SIOCGIFMEDIA:
1.196 skd 1581: case SIOCG80211:
1582: case SIOCS80211:
1583: case SIOCS80211NWID:
1584: case SIOCS80211NWKEY:
1585: case SIOCS80211POWER:
1586: case SIOCS80211BSSID:
1587: case SIOCS80211CHANNEL:
1.185 dyoung 1588: if (ifp->if_ioctl == NULL)
1589: return EOPNOTSUPP;
1.49 itojun 1590: error = (*ifp->if_ioctl)(ifp, cmd, data);
1591: break;
1.1 cgd 1592:
1.154 perry 1593: case SIOCSDRVSPEC:
1.1 cgd 1594: default:
1.185 dyoung 1595: if (so->so_proto == NULL)
1596: return EOPNOTSUPP;
1.161 christos 1597: #ifdef COMPAT_OSOCK
1.186 christos 1598: error = compat_ifioctl(so, ocmd, cmd, data, l);
1.161 christos 1599: #else
1.49 itojun 1600: error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
1.36 mycroft 1601: (struct mbuf *)cmd, (struct mbuf *)data,
1.162 christos 1602: (struct mbuf *)ifp, l));
1.1 cgd 1603: #endif
1.49 itojun 1604: break;
1605: }
1.1 cgd 1606:
1.49 itojun 1607: if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
1608: #ifdef INET6
1609: if ((ifp->if_flags & IFF_UP) != 0) {
1.89 thorpej 1610: s = splnet();
1.49 itojun 1611: in6_if_up(ifp);
1612: splx(s);
1613: }
1.1 cgd 1614: #endif
1615: }
1.186 christos 1616: #ifdef COMPAT_OIFREQ
1617: if (cmd != ocmd)
1618: ifreqn2o(oifr, ifr);
1619: #endif
1.49 itojun 1620:
1.185 dyoung 1621: return error;
1.1 cgd 1622: }
1623:
1624: /*
1625: * Return interface configuration
1626: * of system. List may be used
1627: * in later ioctl's (above) to get
1628: * other information.
1.200 gdt 1629: *
1630: * Each record is a struct ifreq. Before the addition of
1631: * sockaddr_storage, the API rule was that sockaddr flavors that did
1632: * not fit would extend beyond the struct ifreq, with the next struct
1633: * ifreq starting sa_len beyond the struct sockaddr. Because the
1634: * union in struct ifreq includes struct sockaddr_storage, every kind
1635: * of sockaddr must fit. Thus, there are no longer any overlength
1636: * records.
1637: *
1638: * Records are added to the user buffer if they fit, and ifc_len is
1639: * adjusted to the length that was written. Thus, the user is only
1640: * assured of getting the complete list if ifc_len on return is at
1641: * least sizeof(struct ifreq) less than it was on entry.
1642: *
1643: * If the user buffer pointer is NULL, this routine copies no data and
1644: * returns the amount of space that would be needed.
1645: *
1646: * Invariants:
1647: * ifrp points to the next part of the user's buffer to be used. If
1648: * ifrp != NULL, space holds the number of bytes remaining that we may
1649: * write at ifrp. Otherwise, space holds the number of bytes that
1650: * would have been written had there been adequate space.
1.1 cgd 1651: */
1652: /*ARGSUSED*/
1.15 mycroft 1653: int
1.183 christos 1654: ifconf(u_long cmd, void *data)
1.1 cgd 1655: {
1.61 augustss 1656: struct ifconf *ifc = (struct ifconf *)data;
1657: struct ifnet *ifp;
1658: struct ifaddr *ifa;
1.1 cgd 1659: struct ifreq ifr, *ifrp;
1.190 enami 1660: int space, error = 0;
1.200 gdt 1661: const int sz = (int)sizeof(struct ifreq);
1.1 cgd 1662:
1.190 enami 1663: if ((ifrp = ifc->ifc_req) == NULL)
1.127 christos 1664: space = 0;
1.190 enami 1665: else
1666: space = ifc->ifc_len;
1.153 matt 1667: IFNET_FOREACH(ifp) {
1.175 christos 1668: (void)strncpy(ifr.ifr_name, ifp->if_xname,
1.173 christos 1669: sizeof(ifr.ifr_name));
1.175 christos 1670: if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0')
1671: return ENAMETOOLONG;
1.205 dyoung 1672: if (IFADDR_EMPTY(ifp)) {
1.200 gdt 1673: /* Interface with no addresses - send zero sockaddr. */
1.127 christos 1674: memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1.200 gdt 1675: if (ifrp != NULL)
1676: {
1677: if (space >= sz) {
1678: error = copyout(&ifr, ifrp, sz);
1679: if (error != 0)
1680: return (error);
1681: ifrp++; space -= sz;
1682: }
1.70 mellon 1683: }
1.200 gdt 1684: else
1685: space += sz;
1.127 christos 1686: continue;
1687: }
1688:
1.204 dyoung 1689: IFADDR_FOREACH(ifa, ifp) {
1.61 augustss 1690: struct sockaddr *sa = ifa->ifa_addr;
1.200 gdt 1691: /* all sockaddrs must fit in sockaddr_storage */
1692: KASSERT(sa->sa_len <= sizeof(ifr.ifr_ifru));
1693:
1694: if (ifrp != NULL)
1695: {
1.201 gdt 1696: memcpy(&ifr.ifr_space, sa, sa->sa_len);
1.190 enami 1697: if (space >= sz) {
1.127 christos 1698: error = copyout(&ifr, ifrp, sz);
1.200 gdt 1699: if (error != 0)
1700: return (error);
1701: ifrp++; space -= sz;
1.70 mellon 1702: }
1.1 cgd 1703: }
1.200 gdt 1704: else
1705: space += sz;
1.1 cgd 1706: }
1707: }
1.127 christos 1708: if (ifrp != NULL)
1.200 gdt 1709: {
1710: KASSERT(0 <= space && space <= ifc->ifc_len);
1.127 christos 1711: ifc->ifc_len -= space;
1.200 gdt 1712: }
1.127 christos 1713: else
1.200 gdt 1714: {
1715: KASSERT(space >= 0);
1716: ifc->ifc_len = space;
1717: }
1.190 enami 1718: return (0);
1.1 cgd 1719: }
1.133 jonathan 1720:
1.198 dyoung 1721: int
1722: ifreq_setaddr(const u_long cmd, struct ifreq *ifr, const struct sockaddr *sa)
1723: {
1724: uint8_t len;
1725: u_long ncmd;
1726: const uint8_t osockspace = sizeof(ifr->ifr_addr);
1727: const uint8_t sockspace = sizeof(ifr->ifr_ifru.ifru_space);
1728:
1.199 dyoung 1729: #ifdef INET6
1.198 dyoung 1730: if (cmd == SIOCGIFPSRCADDR_IN6 || cmd == SIOCGIFPDSTADDR_IN6)
1731: len = MIN(sizeof(struct sockaddr_in6), sa->sa_len);
1.199 dyoung 1732: else
1733: #endif /* INET6 */
1734: if ((ncmd = compat_cvtcmd(cmd)) != cmd)
1.202 dyoung 1735: len = MIN(osockspace, sa->sa_len);
1736: else
1.198 dyoung 1737: len = MIN(sockspace, sa->sa_len);
1738: if (len < sa->sa_len)
1739: return EFBIG;
1.202 dyoung 1740: sockaddr_copy(&ifr->ifr_addr, len, sa);
1.198 dyoung 1741: return 0;
1742: }
1743:
1.155 christos 1744: /*
1745: * Queue message on interface, and start output if interface
1746: * not yet active.
1747: */
1748: int
1749: ifq_enqueue(struct ifnet *ifp, struct mbuf *m
1750: ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
1751: {
1752: int len = m->m_pkthdr.len;
1753: int mflags = m->m_flags;
1754: int s = splnet();
1755: int error;
1756:
1.156 christos 1757: IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
1.185 dyoung 1758: if (error != 0)
1759: goto out;
1.155 christos 1760: ifp->if_obytes += len;
1761: if (mflags & M_MCAST)
1762: ifp->if_omcasts++;
1763: if ((ifp->if_flags & IFF_OACTIVE) == 0)
1764: (*ifp->if_start)(ifp);
1.185 dyoung 1765: out:
1.155 christos 1766: splx(s);
1767: return error;
1768: }
1769:
1770: /*
1771: * Queue message on interface, possibly using a second fast queue
1772: */
1773: int
1774: ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m
1775: ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
1776: {
1777: int error = 0;
1778:
1779: if (ifq != NULL
1780: #ifdef ALTQ
1781: && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
1782: #endif
1783: ) {
1784: if (IF_QFULL(ifq)) {
1785: IF_DROP(&ifp->if_snd);
1786: m_freem(m);
1787: if (error == 0)
1788: error = ENOBUFS;
1.185 dyoung 1789: } else
1.155 christos 1790: IF_ENQUEUE(ifq, m);
1791: } else
1.156 christos 1792: IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
1.155 christos 1793: if (error != 0) {
1794: ++ifp->if_oerrors;
1795: return error;
1796: }
1797: return 0;
1798: }
1799:
1800:
1.138 drochner 1801: #if defined(INET) || defined(INET6)
1.136 atatat 1802: static void
1.139 atatat 1803: sysctl_net_ifq_setup(struct sysctllog **clog,
1804: int pf, const char *pfname,
1.136 atatat 1805: int ipn, const char *ipname,
1806: int qid, struct ifqueue *ifq)
1807: {
1808:
1.139 atatat 1809: sysctl_createv(clog, 0, NULL, NULL,
1810: CTLFLAG_PERMANENT,
1.136 atatat 1811: CTLTYPE_NODE, "net", NULL,
1812: NULL, 0, NULL, 0,
1813: CTL_NET, CTL_EOL);
1.139 atatat 1814: sysctl_createv(clog, 0, NULL, NULL,
1815: CTLFLAG_PERMANENT,
1.136 atatat 1816: CTLTYPE_NODE, pfname, NULL,
1817: NULL, 0, NULL, 0,
1818: CTL_NET, pf, CTL_EOL);
1.139 atatat 1819: sysctl_createv(clog, 0, NULL, NULL,
1820: CTLFLAG_PERMANENT,
1.136 atatat 1821: CTLTYPE_NODE, ipname, NULL,
1822: NULL, 0, NULL, 0,
1823: CTL_NET, pf, ipn, CTL_EOL);
1.139 atatat 1824: sysctl_createv(clog, 0, NULL, NULL,
1825: CTLFLAG_PERMANENT,
1.142 atatat 1826: CTLTYPE_NODE, "ifq",
1827: SYSCTL_DESCR("Protocol input queue controls"),
1.139 atatat 1828: NULL, 0, NULL, 0,
1829: CTL_NET, pf, ipn, qid, CTL_EOL);
1.136 atatat 1830:
1.139 atatat 1831: sysctl_createv(clog, 0, NULL, NULL,
1832: CTLFLAG_PERMANENT,
1.142 atatat 1833: CTLTYPE_INT, "len",
1834: SYSCTL_DESCR("Current input queue length"),
1.136 atatat 1835: NULL, 0, &ifq->ifq_len, 0,
1836: CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
1.139 atatat 1837: sysctl_createv(clog, 0, NULL, NULL,
1838: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.142 atatat 1839: CTLTYPE_INT, "maxlen",
1840: SYSCTL_DESCR("Maximum allowed input queue length"),
1.136 atatat 1841: NULL, 0, &ifq->ifq_maxlen, 0,
1842: CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
1.133 jonathan 1843: #ifdef notyet
1.139 atatat 1844: sysctl_createv(clog, 0, NULL, NULL,
1845: CTLFLAG_PERMANENT,
1.142 atatat 1846: CTLTYPE_INT, "peak",
1847: SYSCTL_DESCR("Highest input queue length"),
1.136 atatat 1848: NULL, 0, &ifq->ifq_peak, 0,
1849: CTL_NET, pf, ipn, qid, IFQCTL_PEAK, CTL_EOL);
1850: #endif
1.139 atatat 1851: sysctl_createv(clog, 0, NULL, NULL,
1852: CTLFLAG_PERMANENT,
1.142 atatat 1853: CTLTYPE_INT, "drops",
1854: SYSCTL_DESCR("Packets dropped due to full input queue"),
1.136 atatat 1855: NULL, 0, &ifq->ifq_drops, 0,
1856: CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
1857: }
1858:
1859: #ifdef INET
1860: SYSCTL_SETUP(sysctl_net_inet_ip_ifq_setup,
1861: "sysctl net.inet.ip.ifq subtree setup")
1862: {
1863: extern struct ifqueue ipintrq;
1864:
1.139 atatat 1865: sysctl_net_ifq_setup(clog, PF_INET, "inet", IPPROTO_IP, "ip",
1.136 atatat 1866: IPCTL_IFQ, &ipintrq);
1.133 jonathan 1867: }
1.136 atatat 1868: #endif /* INET */
1.133 jonathan 1869:
1.136 atatat 1870: #ifdef INET6
1871: SYSCTL_SETUP(sysctl_net_inet6_ip6_ifq_setup,
1872: "sysctl net.inet6.ip6.ifq subtree setup")
1873: {
1874: extern struct ifqueue ip6intrq;
1875:
1.139 atatat 1876: sysctl_net_ifq_setup(clog, PF_INET6, "inet6", IPPROTO_IPV6, "ip6",
1.136 atatat 1877: IPV6CTL_IFQ, &ip6intrq);
1878: }
1879: #endif /* INET6 */
1.138 drochner 1880: #endif /* INET || INET6 */
CVSweb <webmaster@jp.NetBSD.org>