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