Deleted Added
full compact
if.c (167943) if.c (168561)
1/*-
2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)if.c 8.5 (Berkeley) 1/9/95
1/*-
2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)if.c 8.5 (Berkeley) 1/9/95
30 * $FreeBSD: head/sys/net/if.c 167943 2007-03-27 16:11:28Z bms $
30 * $FreeBSD: head/sys/net/if.c 168561 2007-04-10 00:27:25Z thompsa $
31 */
32
33#include "opt_compat.h"
34#include "opt_inet6.h"
35#include "opt_inet.h"
36#include "opt_mac.h"
37#include "opt_carp.h"
38
39#include <sys/param.h>
40#include <sys/types.h>
41#include <sys/conf.h>
42#include <sys/malloc.h>
43#include <sys/sbuf.h>
44#include <sys/bus.h>
45#include <sys/mbuf.h>
46#include <sys/systm.h>
47#include <sys/priv.h>
48#include <sys/proc.h>
49#include <sys/socket.h>
50#include <sys/socketvar.h>
51#include <sys/protosw.h>
52#include <sys/kernel.h>
53#include <sys/sockio.h>
54#include <sys/syslog.h>
55#include <sys/sysctl.h>
56#include <sys/taskqueue.h>
57#include <sys/domain.h>
58#include <sys/jail.h>
59#include <machine/stdarg.h>
60
61#include <net/if.h>
62#include <net/if_clone.h>
63#include <net/if_dl.h>
64#include <net/if_types.h>
65#include <net/if_var.h>
66#include <net/radix.h>
67#include <net/route.h>
68
69#if defined(INET) || defined(INET6)
70/*XXX*/
71#include <netinet/in.h>
72#include <netinet/in_var.h>
73#ifdef INET6
74#include <netinet6/in6_var.h>
75#include <netinet6/in6_ifattach.h>
76#endif
77#endif
78#ifdef INET
79#include <netinet/if_ether.h>
80#endif
81#ifdef DEV_CARP
82#include <netinet/ip_carp.h>
83#endif
84
85#include <security/mac/mac_framework.h>
86
87SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
88SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
89
90/* Log link state change events */
91static int log_link_state_change = 1;
92
93SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
94 &log_link_state_change, 0,
95 "log interface link state change events");
96
97void (*bstp_linkstate_p)(struct ifnet *ifp, int state);
98void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
31 */
32
33#include "opt_compat.h"
34#include "opt_inet6.h"
35#include "opt_inet.h"
36#include "opt_mac.h"
37#include "opt_carp.h"
38
39#include <sys/param.h>
40#include <sys/types.h>
41#include <sys/conf.h>
42#include <sys/malloc.h>
43#include <sys/sbuf.h>
44#include <sys/bus.h>
45#include <sys/mbuf.h>
46#include <sys/systm.h>
47#include <sys/priv.h>
48#include <sys/proc.h>
49#include <sys/socket.h>
50#include <sys/socketvar.h>
51#include <sys/protosw.h>
52#include <sys/kernel.h>
53#include <sys/sockio.h>
54#include <sys/syslog.h>
55#include <sys/sysctl.h>
56#include <sys/taskqueue.h>
57#include <sys/domain.h>
58#include <sys/jail.h>
59#include <machine/stdarg.h>
60
61#include <net/if.h>
62#include <net/if_clone.h>
63#include <net/if_dl.h>
64#include <net/if_types.h>
65#include <net/if_var.h>
66#include <net/radix.h>
67#include <net/route.h>
68
69#if defined(INET) || defined(INET6)
70/*XXX*/
71#include <netinet/in.h>
72#include <netinet/in_var.h>
73#ifdef INET6
74#include <netinet6/in6_var.h>
75#include <netinet6/in6_ifattach.h>
76#endif
77#endif
78#ifdef INET
79#include <netinet/if_ether.h>
80#endif
81#ifdef DEV_CARP
82#include <netinet/ip_carp.h>
83#endif
84
85#include <security/mac/mac_framework.h>
86
87SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
88SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
89
90/* Log link state change events */
91static int log_link_state_change = 1;
92
93SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
94 &log_link_state_change, 0,
95 "log interface link state change events");
96
97void (*bstp_linkstate_p)(struct ifnet *ifp, int state);
98void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
99void (*trunk_linkstate_p)(struct ifnet *ifp, int state);
99
100struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
101
102/*
103 * XXX: Style; these should be sorted alphabetically, and unprototyped
104 * static functions should be prototyped. Currently they are sorted by
105 * declaration order.
106 */
107static void if_attachdomain(void *);
108static void if_attachdomain1(struct ifnet *);
109static void if_purgemaddrs(struct ifnet *);
110static int ifconf(u_long, caddr_t);
111static void if_freemulti(struct ifmultiaddr *);
112static void if_grow(void);
113static void if_init(void *);
114static void if_check(void *);
115static void if_qflush(struct ifaltq *);
116static void if_route(struct ifnet *, int flag, int fam);
117static int if_setflag(struct ifnet *, int, int, int *, int);
118static void if_slowtimo(void *);
119static void if_unroute(struct ifnet *, int flag, int fam);
120static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
121static int if_rtdel(struct radix_node *, void *);
122static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
123static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
124static void if_start_deferred(void *context, int pending);
125static void do_link_state_change(void *, int);
126static int if_getgroup(struct ifgroupreq *, struct ifnet *);
127static int if_getgroupmembers(struct ifgroupreq *);
128#ifdef INET6
129/*
130 * XXX: declare here to avoid to include many inet6 related files..
131 * should be more generalized?
132 */
133extern void nd6_setmtu(struct ifnet *);
134#endif
135
136int if_index = 0;
137struct ifindex_entry *ifindex_table = NULL;
138int ifqmaxlen = IFQ_MAXLEN;
139struct ifnethead ifnet; /* depend on static init XXX */
140struct ifgrouphead ifg_head;
141struct mtx ifnet_lock;
142static if_com_alloc_t *if_com_alloc[256];
143static if_com_free_t *if_com_free[256];
144
145static int if_indexlim = 8;
146static struct knlist ifklist;
147
148static void filt_netdetach(struct knote *kn);
149static int filt_netdev(struct knote *kn, long hint);
150
151static struct filterops netdev_filtops =
152 { 1, NULL, filt_netdetach, filt_netdev };
153
154/*
155 * System initialization
156 */
157SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL)
158SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL)
159
160MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
161MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
162MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
163
164static d_open_t netopen;
165static d_close_t netclose;
166static d_ioctl_t netioctl;
167static d_kqfilter_t netkqfilter;
168
169static struct cdevsw net_cdevsw = {
170 .d_version = D_VERSION,
171 .d_flags = D_NEEDGIANT,
172 .d_open = netopen,
173 .d_close = netclose,
174 .d_ioctl = netioctl,
175 .d_name = "net",
176 .d_kqfilter = netkqfilter,
177};
178
179static int
180netopen(struct cdev *dev, int flag, int mode, struct thread *td)
181{
182 return (0);
183}
184
185static int
186netclose(struct cdev *dev, int flags, int fmt, struct thread *td)
187{
188 return (0);
189}
190
191static int
192netioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
193{
194 struct ifnet *ifp;
195 int error, idx;
196
197 /* only support interface specific ioctls */
198 if (IOCGROUP(cmd) != 'i')
199 return (EOPNOTSUPP);
200 idx = minor(dev);
201 if (idx == 0) {
202 /*
203 * special network device, not interface.
204 */
205 if (cmd == SIOCGIFCONF)
206 return (ifconf(cmd, data)); /* XXX remove cmd */
207#ifdef __amd64__
208 if (cmd == SIOCGIFCONF32)
209 return (ifconf(cmd, data)); /* XXX remove cmd */
210#endif
211 return (EOPNOTSUPP);
212 }
213
214 ifp = ifnet_byindex(idx);
215 if (ifp == NULL)
216 return (ENXIO);
217
218 error = ifhwioctl(cmd, ifp, data, td);
219 if (error == ENOIOCTL)
220 error = EOPNOTSUPP;
221 return (error);
222}
223
224static int
225netkqfilter(struct cdev *dev, struct knote *kn)
226{
227 struct knlist *klist;
228 struct ifnet *ifp;
229 int idx;
230
231 switch (kn->kn_filter) {
232 case EVFILT_NETDEV:
233 kn->kn_fop = &netdev_filtops;
234 break;
235 default:
236 return (EINVAL);
237 }
238
239 idx = minor(dev);
240 if (idx == 0) {
241 klist = &ifklist;
242 } else {
243 ifp = ifnet_byindex(idx);
244 if (ifp == NULL)
245 return (1);
246 klist = &ifp->if_klist;
247 }
248
249 kn->kn_hook = (caddr_t)klist;
250
251 knlist_add(klist, kn, 0);
252
253 return (0);
254}
255
256static void
257filt_netdetach(struct knote *kn)
258{
259 struct knlist *klist = (struct knlist *)kn->kn_hook;
260
261 knlist_remove(klist, kn, 0);
262}
263
264static int
265filt_netdev(struct knote *kn, long hint)
266{
267 struct knlist *klist = (struct knlist *)kn->kn_hook;
268
269 /*
270 * Currently NOTE_EXIT is abused to indicate device detach.
271 */
272 if (hint == NOTE_EXIT) {
273 kn->kn_data = NOTE_LINKINV;
274 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
275 knlist_remove_inevent(klist, kn);
276 return (1);
277 }
278 if (hint != 0)
279 kn->kn_data = hint; /* current status */
280 if (kn->kn_sfflags & hint)
281 kn->kn_fflags |= hint;
282 return (kn->kn_fflags != 0);
283}
284
285/*
286 * Network interface utility routines.
287 *
288 * Routines with ifa_ifwith* names take sockaddr *'s as
289 * parameters.
290 */
291/* ARGSUSED*/
292static void
293if_init(void *dummy __unused)
294{
295
296 IFNET_LOCK_INIT();
297 TAILQ_INIT(&ifnet);
298 TAILQ_INIT(&ifg_head);
299 knlist_init(&ifklist, NULL, NULL, NULL, NULL);
300 if_grow(); /* create initial table */
301 ifdev_byindex(0) = make_dev(&net_cdevsw, 0,
302 UID_ROOT, GID_WHEEL, 0600, "network");
303 if_clone_init();
304}
305
306static void
307if_grow(void)
308{
309 u_int n;
310 struct ifindex_entry *e;
311
312 if_indexlim <<= 1;
313 n = if_indexlim * sizeof(*e);
314 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
315 if (ifindex_table != NULL) {
316 memcpy((caddr_t)e, (caddr_t)ifindex_table, n/2);
317 free((caddr_t)ifindex_table, M_IFNET);
318 }
319 ifindex_table = e;
320}
321
322/* ARGSUSED*/
323static void
324if_check(void *dummy __unused)
325{
326 struct ifnet *ifp;
327 int s;
328
329 s = splimp();
330 IFNET_RLOCK(); /* could sleep on rare error; mostly okay XXX */
331 TAILQ_FOREACH(ifp, &ifnet, if_link) {
332 if (ifp->if_snd.ifq_maxlen == 0) {
333 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
334 ifp->if_snd.ifq_maxlen = ifqmaxlen;
335 }
336 if (!mtx_initialized(&ifp->if_snd.ifq_mtx)) {
337 if_printf(ifp,
338 "XXX: driver didn't initialize queue mtx\n");
339 mtx_init(&ifp->if_snd.ifq_mtx, "unknown",
340 MTX_NETWORK_LOCK, MTX_DEF);
341 }
342 }
343 IFNET_RUNLOCK();
344 splx(s);
345 if_slowtimo(0);
346}
347
348/*
349 * Allocate a struct ifnet and in index for an interface.
350 */
351struct ifnet*
352if_alloc(u_char type)
353{
354 struct ifnet *ifp;
355
356 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
357
358 /*
359 * Try to find an empty slot below if_index. If we fail, take
360 * the next slot.
361 *
362 * XXX: should be locked!
363 */
364 for (ifp->if_index = 1; ifp->if_index <= if_index; ifp->if_index++) {
365 if (ifnet_byindex(ifp->if_index) == NULL)
366 break;
367 }
368 /* Catch if_index overflow. */
369 if (ifp->if_index < 1) {
370 free(ifp, M_IFNET);
371 return (NULL);
372 }
373 if (ifp->if_index > if_index)
374 if_index = ifp->if_index;
375 if (if_index >= if_indexlim)
376 if_grow();
377 ifnet_byindex(ifp->if_index) = ifp;
378
379 ifp->if_type = type;
380
381 if (if_com_alloc[type] != NULL) {
382 ifp->if_l2com = if_com_alloc[type](type, ifp);
383 if (ifp->if_l2com == NULL) {
384 free(ifp, M_IFNET);
385 return (NULL);
386 }
387 }
388 IF_ADDR_LOCK_INIT(ifp);
389
390 return (ifp);
391}
392
393void
394if_free(struct ifnet *ifp)
395{
396
397 /* Do not add code to this function! Add it to if_free_type(). */
398 if_free_type(ifp, ifp->if_type);
399}
400
401void
402if_free_type(struct ifnet *ifp, u_char type)
403{
404
405 if (ifp != ifnet_byindex(ifp->if_index)) {
406 if_printf(ifp, "%s: value was not if_alloced, skipping\n",
407 __func__);
408 return;
409 }
410
411 IF_ADDR_LOCK_DESTROY(ifp);
412
413 ifnet_byindex(ifp->if_index) = NULL;
414
415 /* XXX: should be locked with if_findindex() */
416 while (if_index > 0 && ifnet_byindex(if_index) == NULL)
417 if_index--;
418
419 if (if_com_free[type] != NULL)
420 if_com_free[type](ifp->if_l2com, type);
421
422 free(ifp, M_IFNET);
423};
424
425/*
426 * Attach an interface to the
427 * list of "active" interfaces.
428 */
429void
430if_attach(struct ifnet *ifp)
431{
432 unsigned socksize, ifasize;
433 int namelen, masklen;
434 struct sockaddr_dl *sdl;
435 struct ifaddr *ifa;
436
437 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
438 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
439 ifp->if_xname);
440
441 TASK_INIT(&ifp->if_starttask, 0, if_start_deferred, ifp);
442 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
443 IF_AFDATA_LOCK_INIT(ifp);
444 ifp->if_afdata_initialized = 0;
445 /*
446 * XXX -
447 * The old code would work if the interface passed a pre-existing
448 * chain of ifaddrs to this code. We don't trust our callers to
449 * properly initialize the tailq, however, so we no longer allow
450 * this unlikely case.
451 */
452 TAILQ_INIT(&ifp->if_addrhead);
453 TAILQ_INIT(&ifp->if_prefixhead);
454 TAILQ_INIT(&ifp->if_multiaddrs);
455 TAILQ_INIT(&ifp->if_groups);
456
457 if_addgroup(ifp, IFG_ALL);
458
459 knlist_init(&ifp->if_klist, NULL, NULL, NULL, NULL);
460 getmicrotime(&ifp->if_lastchange);
461 ifp->if_data.ifi_epoch = time_uptime;
462 ifp->if_data.ifi_datalen = sizeof(struct if_data);
463
464#ifdef MAC
465 mac_init_ifnet(ifp);
466 mac_create_ifnet(ifp);
467#endif
468
469 ifdev_byindex(ifp->if_index) = make_dev(&net_cdevsw,
470 unit2minor(ifp->if_index),
471 UID_ROOT, GID_WHEEL, 0600, "%s/%s",
472 net_cdevsw.d_name, ifp->if_xname);
473 make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d",
474 net_cdevsw.d_name, ifp->if_index);
475
476 mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
477
478 /*
479 * create a Link Level name for this device
480 */
481 namelen = strlen(ifp->if_xname);
482 /*
483 * Always save enough space for any possiable name so we can do
484 * a rename in place later.
485 */
486 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
487 socksize = masklen + ifp->if_addrlen;
488 if (socksize < sizeof(*sdl))
489 socksize = sizeof(*sdl);
490 socksize = roundup2(socksize, sizeof(long));
491 ifasize = sizeof(*ifa) + 2 * socksize;
492 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
493 IFA_LOCK_INIT(ifa);
494 sdl = (struct sockaddr_dl *)(ifa + 1);
495 sdl->sdl_len = socksize;
496 sdl->sdl_family = AF_LINK;
497 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
498 sdl->sdl_nlen = namelen;
499 sdl->sdl_index = ifp->if_index;
500 sdl->sdl_type = ifp->if_type;
501 ifp->if_addr = ifa;
502 ifa->ifa_ifp = ifp;
503 ifa->ifa_rtrequest = link_rtrequest;
504 ifa->ifa_addr = (struct sockaddr *)sdl;
505 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
506 ifa->ifa_netmask = (struct sockaddr *)sdl;
507 sdl->sdl_len = masklen;
508 while (namelen != 0)
509 sdl->sdl_data[--namelen] = 0xff;
510 ifa->ifa_refcnt = 1;
511 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
512 ifp->if_broadcastaddr = NULL; /* reliably crash if used uninitialized */
513 ifp->if_snd.altq_type = 0;
514 ifp->if_snd.altq_disc = NULL;
515 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
516 ifp->if_snd.altq_tbr = NULL;
517 ifp->if_snd.altq_ifp = ifp;
518
519 IFNET_WLOCK();
520 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
521 IFNET_WUNLOCK();
522
523 if (domain_init_status >= 2)
524 if_attachdomain1(ifp);
525
526 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
527 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
528
529 /* Announce the interface. */
530 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
531
532 if (ifp->if_watchdog != NULL)
533 if_printf(ifp, "using obsoleted if_watchdog interface\n");
534}
535
536static void
537if_attachdomain(void *dummy)
538{
539 struct ifnet *ifp;
540 int s;
541
542 s = splnet();
543 TAILQ_FOREACH(ifp, &ifnet, if_link)
544 if_attachdomain1(ifp);
545 splx(s);
546}
547SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
548 if_attachdomain, NULL);
549
550static void
551if_attachdomain1(struct ifnet *ifp)
552{
553 struct domain *dp;
554 int s;
555
556 s = splnet();
557
558 /*
559 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
560 * cannot lock ifp->if_afdata initialization, entirely.
561 */
562 if (IF_AFDATA_TRYLOCK(ifp) == 0) {
563 splx(s);
564 return;
565 }
566 if (ifp->if_afdata_initialized >= domain_init_status) {
567 IF_AFDATA_UNLOCK(ifp);
568 splx(s);
569 printf("if_attachdomain called more than once on %s\n",
570 ifp->if_xname);
571 return;
572 }
573 ifp->if_afdata_initialized = domain_init_status;
574 IF_AFDATA_UNLOCK(ifp);
575
576 /* address family dependent data region */
577 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
578 for (dp = domains; dp; dp = dp->dom_next) {
579 if (dp->dom_ifattach)
580 ifp->if_afdata[dp->dom_family] =
581 (*dp->dom_ifattach)(ifp);
582 }
583
584 splx(s);
585}
586
587/*
588 * Remove any unicast or broadcast network addresses from an interface.
589 */
590
591void
592if_purgeaddrs(struct ifnet *ifp)
593{
594 struct ifaddr *ifa, *next;
595
596 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
597 if (ifa->ifa_addr->sa_family == AF_LINK)
598 continue;
599#ifdef INET
600 /* XXX: Ugly!! ad hoc just for INET */
601 if (ifa->ifa_addr->sa_family == AF_INET) {
602 struct ifaliasreq ifr;
603
604 bzero(&ifr, sizeof(ifr));
605 ifr.ifra_addr = *ifa->ifa_addr;
606 if (ifa->ifa_dstaddr)
607 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
608 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
609 NULL) == 0)
610 continue;
611 }
612#endif /* INET */
613#ifdef INET6
614 if (ifa->ifa_addr->sa_family == AF_INET6) {
615 in6_purgeaddr(ifa);
616 /* ifp_addrhead is already updated */
617 continue;
618 }
619#endif /* INET6 */
620 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
621 IFAFREE(ifa);
622 }
623}
624
625/*
626 * Remove any multicast network addresses from an interface.
627 */
628static void
629if_purgemaddrs(struct ifnet *ifp)
630{
631 struct ifmultiaddr *ifma;
632 struct ifmultiaddr *next;
633
634 IF_ADDR_LOCK(ifp);
635 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
636 if_delmulti_locked(ifp, ifma, 1);
637 IF_ADDR_UNLOCK(ifp);
638}
639
640/*
641 * Detach an interface, removing it from the
642 * list of "active" interfaces.
643 *
644 * XXXRW: There are some significant questions about event ordering, and
645 * how to prevent things from starting to use the interface during detach.
646 */
647void
648if_detach(struct ifnet *ifp)
649{
650 struct ifaddr *ifa;
651 struct radix_node_head *rnh;
652 int s;
653 int i;
654 struct domain *dp;
655 struct ifnet *iter;
656 int found = 0;
657
658 IFNET_WLOCK();
659 TAILQ_FOREACH(iter, &ifnet, if_link)
660 if (iter == ifp) {
661 TAILQ_REMOVE(&ifnet, ifp, if_link);
662 found = 1;
663 break;
664 }
665 IFNET_WUNLOCK();
666 if (!found)
667 return;
668
669 /*
670 * Remove/wait for pending events.
671 */
672 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
673
674 /*
675 * Remove routes and flush queues.
676 */
677 s = splnet();
678 if_down(ifp);
679#ifdef ALTQ
680 if (ALTQ_IS_ENABLED(&ifp->if_snd))
681 altq_disable(&ifp->if_snd);
682 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
683 altq_detach(&ifp->if_snd);
684#endif
685
686 if_purgeaddrs(ifp);
687
688#ifdef INET
689 in_ifdetach(ifp);
690#endif
691
692#ifdef INET6
693 /*
694 * Remove all IPv6 kernel structs related to ifp. This should be done
695 * before removing routing entries below, since IPv6 interface direct
696 * routes are expected to be removed by the IPv6-specific kernel API.
697 * Otherwise, the kernel will detect some inconsistency and bark it.
698 */
699 in6_ifdetach(ifp);
700#endif
701 if_purgemaddrs(ifp);
702
703 /*
704 * Remove link ifaddr pointer and maybe decrement if_index.
705 * Clean up all addresses.
706 */
707 ifp->if_addr = NULL;
708 destroy_dev(ifdev_byindex(ifp->if_index));
709 ifdev_byindex(ifp->if_index) = NULL;
710
711 /* We can now free link ifaddr. */
712 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
713 ifa = TAILQ_FIRST(&ifp->if_addrhead);
714 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
715 IFAFREE(ifa);
716 }
717
718 /*
719 * Delete all remaining routes using this interface
720 * Unfortuneatly the only way to do this is to slog through
721 * the entire routing table looking for routes which point
722 * to this interface...oh well...
723 */
724 for (i = 1; i <= AF_MAX; i++) {
725 if ((rnh = rt_tables[i]) == NULL)
726 continue;
727 RADIX_NODE_HEAD_LOCK(rnh);
728 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
729 RADIX_NODE_HEAD_UNLOCK(rnh);
730 }
731
732 /* Announce that the interface is gone. */
733 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
734 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
735 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
736
737 IF_AFDATA_LOCK(ifp);
738 for (dp = domains; dp; dp = dp->dom_next) {
739 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
740 (*dp->dom_ifdetach)(ifp,
741 ifp->if_afdata[dp->dom_family]);
742 }
743 IF_AFDATA_UNLOCK(ifp);
744
745#ifdef MAC
746 mac_destroy_ifnet(ifp);
747#endif /* MAC */
748 KNOTE_UNLOCKED(&ifp->if_klist, NOTE_EXIT);
749 knlist_clear(&ifp->if_klist, 0);
750 knlist_destroy(&ifp->if_klist);
751 mtx_destroy(&ifp->if_snd.ifq_mtx);
752 IF_AFDATA_DESTROY(ifp);
753 splx(s);
754}
755
756/*
757 * Add a group to an interface
758 */
759int
760if_addgroup(struct ifnet *ifp, const char *groupname)
761{
762 struct ifg_list *ifgl;
763 struct ifg_group *ifg = NULL;
764 struct ifg_member *ifgm;
765
766 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
767 groupname[strlen(groupname) - 1] <= '9')
768 return (EINVAL);
769
770 IFNET_WLOCK();
771 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
772 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
773 IFNET_WUNLOCK();
774 return (EEXIST);
775 }
776
777 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
778 M_NOWAIT)) == NULL) {
779 IFNET_WUNLOCK();
780 return (ENOMEM);
781 }
782
783 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
784 M_TEMP, M_NOWAIT)) == NULL) {
785 free(ifgl, M_TEMP);
786 IFNET_WUNLOCK();
787 return (ENOMEM);
788 }
789
790 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
791 if (!strcmp(ifg->ifg_group, groupname))
792 break;
793
794 if (ifg == NULL) {
795 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
796 M_TEMP, M_NOWAIT)) == NULL) {
797 free(ifgl, M_TEMP);
798 free(ifgm, M_TEMP);
799 IFNET_WUNLOCK();
800 return (ENOMEM);
801 }
802 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
803 ifg->ifg_refcnt = 0;
804 TAILQ_INIT(&ifg->ifg_members);
805 EVENTHANDLER_INVOKE(group_attach_event, ifg);
806 TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
807 }
808
809 ifg->ifg_refcnt++;
810 ifgl->ifgl_group = ifg;
811 ifgm->ifgm_ifp = ifp;
812
813 IF_ADDR_LOCK(ifp);
814 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
815 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
816 IF_ADDR_UNLOCK(ifp);
817
818 IFNET_WUNLOCK();
819
820 EVENTHANDLER_INVOKE(group_change_event, groupname);
821
822 return (0);
823}
824
825/*
826 * Remove a group from an interface
827 */
828int
829if_delgroup(struct ifnet *ifp, const char *groupname)
830{
831 struct ifg_list *ifgl;
832 struct ifg_member *ifgm;
833
834 IFNET_WLOCK();
835 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
836 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
837 break;
838 if (ifgl == NULL) {
839 IFNET_WUNLOCK();
840 return (ENOENT);
841 }
842
843 IF_ADDR_LOCK(ifp);
844 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
845 IF_ADDR_UNLOCK(ifp);
846
847 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
848 if (ifgm->ifgm_ifp == ifp)
849 break;
850
851 if (ifgm != NULL) {
852 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
853 free(ifgm, M_TEMP);
854 }
855
856 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
857 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
858 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
859 free(ifgl->ifgl_group, M_TEMP);
860 }
861 IFNET_WUNLOCK();
862
863 free(ifgl, M_TEMP);
864
865 EVENTHANDLER_INVOKE(group_change_event, groupname);
866
867 return (0);
868}
869
870/*
871 * Stores all groups from an interface in memory pointed
872 * to by data
873 */
874static int
875if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
876{
877 int len, error;
878 struct ifg_list *ifgl;
879 struct ifg_req ifgrq, *ifgp;
880 struct ifgroupreq *ifgr = data;
881
882 if (ifgr->ifgr_len == 0) {
883 IF_ADDR_LOCK(ifp);
884 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
885 ifgr->ifgr_len += sizeof(struct ifg_req);
886 IF_ADDR_UNLOCK(ifp);
887 return (0);
888 }
889
890 len = ifgr->ifgr_len;
891 ifgp = ifgr->ifgr_groups;
892 /* XXX: wire */
893 IF_ADDR_LOCK(ifp);
894 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
895 if (len < sizeof(ifgrq)) {
896 IF_ADDR_UNLOCK(ifp);
897 return (EINVAL);
898 }
899 bzero(&ifgrq, sizeof ifgrq);
900 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
901 sizeof(ifgrq.ifgrq_group));
902 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
903 IF_ADDR_UNLOCK(ifp);
904 return (error);
905 }
906 len -= sizeof(ifgrq);
907 ifgp++;
908 }
909 IF_ADDR_UNLOCK(ifp);
910
911 return (0);
912}
913
914/*
915 * Stores all members of a group in memory pointed to by data
916 */
917static int
918if_getgroupmembers(struct ifgroupreq *data)
919{
920 struct ifgroupreq *ifgr = data;
921 struct ifg_group *ifg;
922 struct ifg_member *ifgm;
923 struct ifg_req ifgrq, *ifgp;
924 int len, error;
925
926 IFNET_RLOCK();
927 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
928 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
929 break;
930 if (ifg == NULL) {
931 IFNET_RUNLOCK();
932 return (ENOENT);
933 }
934
935 if (ifgr->ifgr_len == 0) {
936 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
937 ifgr->ifgr_len += sizeof(ifgrq);
938 IFNET_RUNLOCK();
939 return (0);
940 }
941
942 len = ifgr->ifgr_len;
943 ifgp = ifgr->ifgr_groups;
944 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
945 if (len < sizeof(ifgrq)) {
946 IFNET_RUNLOCK();
947 return (EINVAL);
948 }
949 bzero(&ifgrq, sizeof ifgrq);
950 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
951 sizeof(ifgrq.ifgrq_member));
952 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
953 IFNET_RUNLOCK();
954 return (error);
955 }
956 len -= sizeof(ifgrq);
957 ifgp++;
958 }
959 IFNET_RUNLOCK();
960
961 return (0);
962}
963
964/*
965 * Delete Routes for a Network Interface
966 *
967 * Called for each routing entry via the rnh->rnh_walktree() call above
968 * to delete all route entries referencing a detaching network interface.
969 *
970 * Arguments:
971 * rn pointer to node in the routing table
972 * arg argument passed to rnh->rnh_walktree() - detaching interface
973 *
974 * Returns:
975 * 0 successful
976 * errno failed - reason indicated
977 *
978 */
979static int
980if_rtdel(struct radix_node *rn, void *arg)
981{
982 struct rtentry *rt = (struct rtentry *)rn;
983 struct ifnet *ifp = arg;
984 int err;
985
986 if (rt->rt_ifp == ifp) {
987
988 /*
989 * Protect (sorta) against walktree recursion problems
990 * with cloned routes
991 */
992 if ((rt->rt_flags & RTF_UP) == 0)
993 return (0);
994
995 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
996 rt_mask(rt), rt->rt_flags,
997 (struct rtentry **) NULL);
998 if (err) {
999 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1000 }
1001 }
1002
1003 return (0);
1004}
1005
1006/*
1007 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1008 * structs used to represent other address families, it is necessary
1009 * to perform a different comparison.
1010 */
1011
1012#define sa_equal(a1, a2) \
1013 (bcmp((a1), (a2), ((a1))->sa_len) == 0)
1014
1015#define sa_dl_equal(a1, a2) \
1016 ((((struct sockaddr_dl *)(a1))->sdl_len == \
1017 ((struct sockaddr_dl *)(a2))->sdl_len) && \
1018 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
1019 LLADDR((struct sockaddr_dl *)(a2)), \
1020 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1021
1022/*
1023 * Locate an interface based on a complete address.
1024 */
1025/*ARGSUSED*/
1026struct ifaddr *
1027ifa_ifwithaddr(struct sockaddr *addr)
1028{
1029 struct ifnet *ifp;
1030 struct ifaddr *ifa;
1031
1032 IFNET_RLOCK();
1033 TAILQ_FOREACH(ifp, &ifnet, if_link)
1034 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1035 if (ifa->ifa_addr->sa_family != addr->sa_family)
1036 continue;
1037 if (sa_equal(addr, ifa->ifa_addr))
1038 goto done;
1039 /* IP6 doesn't have broadcast */
1040 if ((ifp->if_flags & IFF_BROADCAST) &&
1041 ifa->ifa_broadaddr &&
1042 ifa->ifa_broadaddr->sa_len != 0 &&
1043 sa_equal(ifa->ifa_broadaddr, addr))
1044 goto done;
1045 }
1046 ifa = NULL;
1047done:
1048 IFNET_RUNLOCK();
1049 return (ifa);
1050}
1051
1052/*
1053 * Locate an interface based on the broadcast address.
1054 */
1055/* ARGSUSED */
1056struct ifaddr *
1057ifa_ifwithbroadaddr(struct sockaddr *addr)
1058{
1059 struct ifnet *ifp;
1060 struct ifaddr *ifa;
1061
1062 IFNET_RLOCK();
1063 TAILQ_FOREACH(ifp, &ifnet, if_link)
1064 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1065 if (ifa->ifa_addr->sa_family != addr->sa_family)
1066 continue;
1067 if ((ifp->if_flags & IFF_BROADCAST) &&
1068 ifa->ifa_broadaddr &&
1069 ifa->ifa_broadaddr->sa_len != 0 &&
1070 sa_equal(ifa->ifa_broadaddr, addr))
1071 goto done;
1072 }
1073 ifa = NULL;
1074done:
1075 IFNET_RUNLOCK();
1076 return (ifa);
1077}
1078
1079/*
1080 * Locate the point to point interface with a given destination address.
1081 */
1082/*ARGSUSED*/
1083struct ifaddr *
1084ifa_ifwithdstaddr(struct sockaddr *addr)
1085{
1086 struct ifnet *ifp;
1087 struct ifaddr *ifa;
1088
1089 IFNET_RLOCK();
1090 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1091 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1092 continue;
1093 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1094 if (ifa->ifa_addr->sa_family != addr->sa_family)
1095 continue;
1096 if (ifa->ifa_dstaddr &&
1097 sa_equal(addr, ifa->ifa_dstaddr))
1098 goto done;
1099 }
1100 }
1101 ifa = NULL;
1102done:
1103 IFNET_RUNLOCK();
1104 return (ifa);
1105}
1106
1107/*
1108 * Find an interface on a specific network. If many, choice
1109 * is most specific found.
1110 */
1111struct ifaddr *
1112ifa_ifwithnet(struct sockaddr *addr)
1113{
1114 struct ifnet *ifp;
1115 struct ifaddr *ifa;
1116 struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
1117 u_int af = addr->sa_family;
1118 char *addr_data = addr->sa_data, *cplim;
1119
1120 /*
1121 * AF_LINK addresses can be looked up directly by their index number,
1122 * so do that if we can.
1123 */
1124 if (af == AF_LINK) {
1125 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1126 if (sdl->sdl_index && sdl->sdl_index <= if_index)
1127 return (ifaddr_byindex(sdl->sdl_index));
1128 }
1129
1130 /*
1131 * Scan though each interface, looking for ones that have
1132 * addresses in this address family.
1133 */
1134 IFNET_RLOCK();
1135 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1136 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1137 char *cp, *cp2, *cp3;
1138
1139 if (ifa->ifa_addr->sa_family != af)
1140next: continue;
1141 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1142 /*
1143 * This is a bit broken as it doesn't
1144 * take into account that the remote end may
1145 * be a single node in the network we are
1146 * looking for.
1147 * The trouble is that we don't know the
1148 * netmask for the remote end.
1149 */
1150 if (ifa->ifa_dstaddr != 0 &&
1151 sa_equal(addr, ifa->ifa_dstaddr))
1152 goto done;
1153 } else {
1154 /*
1155 * if we have a special address handler,
1156 * then use it instead of the generic one.
1157 */
1158 if (ifa->ifa_claim_addr) {
1159 if ((*ifa->ifa_claim_addr)(ifa, addr))
1160 goto done;
1161 continue;
1162 }
1163
1164 /*
1165 * Scan all the bits in the ifa's address.
1166 * If a bit dissagrees with what we are
1167 * looking for, mask it with the netmask
1168 * to see if it really matters.
1169 * (A byte at a time)
1170 */
1171 if (ifa->ifa_netmask == 0)
1172 continue;
1173 cp = addr_data;
1174 cp2 = ifa->ifa_addr->sa_data;
1175 cp3 = ifa->ifa_netmask->sa_data;
1176 cplim = ifa->ifa_netmask->sa_len
1177 + (char *)ifa->ifa_netmask;
1178 while (cp3 < cplim)
1179 if ((*cp++ ^ *cp2++) & *cp3++)
1180 goto next; /* next address! */
1181 /*
1182 * If the netmask of what we just found
1183 * is more specific than what we had before
1184 * (if we had one) then remember the new one
1185 * before continuing to search
1186 * for an even better one.
1187 */
1188 if (ifa_maybe == 0 ||
1189 rn_refines((caddr_t)ifa->ifa_netmask,
1190 (caddr_t)ifa_maybe->ifa_netmask))
1191 ifa_maybe = ifa;
1192 }
1193 }
1194 }
1195 ifa = ifa_maybe;
1196done:
1197 IFNET_RUNLOCK();
1198 return (ifa);
1199}
1200
1201/*
1202 * Find an interface address specific to an interface best matching
1203 * a given address.
1204 */
1205struct ifaddr *
1206ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1207{
1208 struct ifaddr *ifa;
1209 char *cp, *cp2, *cp3;
1210 char *cplim;
1211 struct ifaddr *ifa_maybe = 0;
1212 u_int af = addr->sa_family;
1213
1214 if (af >= AF_MAX)
1215 return (0);
1216 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1217 if (ifa->ifa_addr->sa_family != af)
1218 continue;
1219 if (ifa_maybe == 0)
1220 ifa_maybe = ifa;
1221 if (ifa->ifa_netmask == 0) {
1222 if (sa_equal(addr, ifa->ifa_addr) ||
1223 (ifa->ifa_dstaddr &&
1224 sa_equal(addr, ifa->ifa_dstaddr)))
1225 goto done;
1226 continue;
1227 }
1228 if (ifp->if_flags & IFF_POINTOPOINT) {
1229 if (sa_equal(addr, ifa->ifa_dstaddr))
1230 goto done;
1231 } else {
1232 cp = addr->sa_data;
1233 cp2 = ifa->ifa_addr->sa_data;
1234 cp3 = ifa->ifa_netmask->sa_data;
1235 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1236 for (; cp3 < cplim; cp3++)
1237 if ((*cp++ ^ *cp2++) & *cp3)
1238 break;
1239 if (cp3 == cplim)
1240 goto done;
1241 }
1242 }
1243 ifa = ifa_maybe;
1244done:
1245 return (ifa);
1246}
1247
1248#include <net/route.h>
1249
1250/*
1251 * Default action when installing a route with a Link Level gateway.
1252 * Lookup an appropriate real ifa to point to.
1253 * This should be moved to /sys/net/link.c eventually.
1254 */
1255static void
1256link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1257{
1258 struct ifaddr *ifa, *oifa;
1259 struct sockaddr *dst;
1260 struct ifnet *ifp;
1261
1262 RT_LOCK_ASSERT(rt);
1263
1264 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1265 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1266 return;
1267 ifa = ifaof_ifpforaddr(dst, ifp);
1268 if (ifa) {
1269 IFAREF(ifa); /* XXX */
1270 oifa = rt->rt_ifa;
1271 rt->rt_ifa = ifa;
1272 IFAFREE(oifa);
1273 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1274 ifa->ifa_rtrequest(cmd, rt, info);
1275 }
1276}
1277
1278/*
1279 * Mark an interface down and notify protocols of
1280 * the transition.
1281 * NOTE: must be called at splnet or eqivalent.
1282 */
1283static void
1284if_unroute(struct ifnet *ifp, int flag, int fam)
1285{
1286 struct ifaddr *ifa;
1287
1288 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
1289
1290 ifp->if_flags &= ~flag;
1291 getmicrotime(&ifp->if_lastchange);
1292 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1293 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1294 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1295 if_qflush(&ifp->if_snd);
1296#ifdef DEV_CARP
1297 if (ifp->if_carp)
1298 carp_carpdev_state(ifp->if_carp);
1299#endif
1300 rt_ifmsg(ifp);
1301}
1302
1303/*
1304 * Mark an interface up and notify protocols of
1305 * the transition.
1306 * NOTE: must be called at splnet or eqivalent.
1307 */
1308static void
1309if_route(struct ifnet *ifp, int flag, int fam)
1310{
1311 struct ifaddr *ifa;
1312
1313 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
1314
1315 ifp->if_flags |= flag;
1316 getmicrotime(&ifp->if_lastchange);
1317 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1318 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1319 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1320#ifdef DEV_CARP
1321 if (ifp->if_carp)
1322 carp_carpdev_state(ifp->if_carp);
1323#endif
1324 rt_ifmsg(ifp);
1325#ifdef INET6
1326 in6_if_up(ifp);
1327#endif
1328}
1329
1330void (*vlan_link_state_p)(struct ifnet *, int); /* XXX: private from if_vlan */
1331void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
1332
1333/*
1334 * Handle a change in the interface link state. To avoid LORs
1335 * between driver lock and upper layer locks, as well as possible
1336 * recursions, we post event to taskqueue, and all job
1337 * is done in static do_link_state_change().
1338 */
1339void
1340if_link_state_change(struct ifnet *ifp, int link_state)
1341{
1342 /* Return if state hasn't changed. */
1343 if (ifp->if_link_state == link_state)
1344 return;
1345
1346 ifp->if_link_state = link_state;
1347
1348 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
1349}
1350
1351static void
1352do_link_state_change(void *arg, int pending)
1353{
1354 struct ifnet *ifp = (struct ifnet *)arg;
1355 int link_state = ifp->if_link_state;
1356 int link;
1357
1358 /* Notify that the link state has changed. */
1359 rt_ifmsg(ifp);
1360 if (link_state == LINK_STATE_UP)
1361 link = NOTE_LINKUP;
1362 else if (link_state == LINK_STATE_DOWN)
1363 link = NOTE_LINKDOWN;
1364 else
1365 link = NOTE_LINKINV;
1366 KNOTE_UNLOCKED(&ifp->if_klist, link);
1367 if (ifp->if_vlantrunk != NULL)
1368 (*vlan_link_state_p)(ifp, link);
1369
1370 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
1371 IFP2AC(ifp)->ac_netgraph != NULL)
1372 (*ng_ether_link_state_p)(ifp, link_state);
1373#ifdef DEV_CARP
1374 if (ifp->if_carp)
1375 carp_carpdev_state(ifp->if_carp);
1376#endif
1377 if (ifp->if_bridge) {
1378 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!"));
1379 (*bstp_linkstate_p)(ifp, link_state);
1380 }
100
101struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
102
103/*
104 * XXX: Style; these should be sorted alphabetically, and unprototyped
105 * static functions should be prototyped. Currently they are sorted by
106 * declaration order.
107 */
108static void if_attachdomain(void *);
109static void if_attachdomain1(struct ifnet *);
110static void if_purgemaddrs(struct ifnet *);
111static int ifconf(u_long, caddr_t);
112static void if_freemulti(struct ifmultiaddr *);
113static void if_grow(void);
114static void if_init(void *);
115static void if_check(void *);
116static void if_qflush(struct ifaltq *);
117static void if_route(struct ifnet *, int flag, int fam);
118static int if_setflag(struct ifnet *, int, int, int *, int);
119static void if_slowtimo(void *);
120static void if_unroute(struct ifnet *, int flag, int fam);
121static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
122static int if_rtdel(struct radix_node *, void *);
123static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
124static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
125static void if_start_deferred(void *context, int pending);
126static void do_link_state_change(void *, int);
127static int if_getgroup(struct ifgroupreq *, struct ifnet *);
128static int if_getgroupmembers(struct ifgroupreq *);
129#ifdef INET6
130/*
131 * XXX: declare here to avoid to include many inet6 related files..
132 * should be more generalized?
133 */
134extern void nd6_setmtu(struct ifnet *);
135#endif
136
137int if_index = 0;
138struct ifindex_entry *ifindex_table = NULL;
139int ifqmaxlen = IFQ_MAXLEN;
140struct ifnethead ifnet; /* depend on static init XXX */
141struct ifgrouphead ifg_head;
142struct mtx ifnet_lock;
143static if_com_alloc_t *if_com_alloc[256];
144static if_com_free_t *if_com_free[256];
145
146static int if_indexlim = 8;
147static struct knlist ifklist;
148
149static void filt_netdetach(struct knote *kn);
150static int filt_netdev(struct knote *kn, long hint);
151
152static struct filterops netdev_filtops =
153 { 1, NULL, filt_netdetach, filt_netdev };
154
155/*
156 * System initialization
157 */
158SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL)
159SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL)
160
161MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
162MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
163MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
164
165static d_open_t netopen;
166static d_close_t netclose;
167static d_ioctl_t netioctl;
168static d_kqfilter_t netkqfilter;
169
170static struct cdevsw net_cdevsw = {
171 .d_version = D_VERSION,
172 .d_flags = D_NEEDGIANT,
173 .d_open = netopen,
174 .d_close = netclose,
175 .d_ioctl = netioctl,
176 .d_name = "net",
177 .d_kqfilter = netkqfilter,
178};
179
180static int
181netopen(struct cdev *dev, int flag, int mode, struct thread *td)
182{
183 return (0);
184}
185
186static int
187netclose(struct cdev *dev, int flags, int fmt, struct thread *td)
188{
189 return (0);
190}
191
192static int
193netioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
194{
195 struct ifnet *ifp;
196 int error, idx;
197
198 /* only support interface specific ioctls */
199 if (IOCGROUP(cmd) != 'i')
200 return (EOPNOTSUPP);
201 idx = minor(dev);
202 if (idx == 0) {
203 /*
204 * special network device, not interface.
205 */
206 if (cmd == SIOCGIFCONF)
207 return (ifconf(cmd, data)); /* XXX remove cmd */
208#ifdef __amd64__
209 if (cmd == SIOCGIFCONF32)
210 return (ifconf(cmd, data)); /* XXX remove cmd */
211#endif
212 return (EOPNOTSUPP);
213 }
214
215 ifp = ifnet_byindex(idx);
216 if (ifp == NULL)
217 return (ENXIO);
218
219 error = ifhwioctl(cmd, ifp, data, td);
220 if (error == ENOIOCTL)
221 error = EOPNOTSUPP;
222 return (error);
223}
224
225static int
226netkqfilter(struct cdev *dev, struct knote *kn)
227{
228 struct knlist *klist;
229 struct ifnet *ifp;
230 int idx;
231
232 switch (kn->kn_filter) {
233 case EVFILT_NETDEV:
234 kn->kn_fop = &netdev_filtops;
235 break;
236 default:
237 return (EINVAL);
238 }
239
240 idx = minor(dev);
241 if (idx == 0) {
242 klist = &ifklist;
243 } else {
244 ifp = ifnet_byindex(idx);
245 if (ifp == NULL)
246 return (1);
247 klist = &ifp->if_klist;
248 }
249
250 kn->kn_hook = (caddr_t)klist;
251
252 knlist_add(klist, kn, 0);
253
254 return (0);
255}
256
257static void
258filt_netdetach(struct knote *kn)
259{
260 struct knlist *klist = (struct knlist *)kn->kn_hook;
261
262 knlist_remove(klist, kn, 0);
263}
264
265static int
266filt_netdev(struct knote *kn, long hint)
267{
268 struct knlist *klist = (struct knlist *)kn->kn_hook;
269
270 /*
271 * Currently NOTE_EXIT is abused to indicate device detach.
272 */
273 if (hint == NOTE_EXIT) {
274 kn->kn_data = NOTE_LINKINV;
275 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
276 knlist_remove_inevent(klist, kn);
277 return (1);
278 }
279 if (hint != 0)
280 kn->kn_data = hint; /* current status */
281 if (kn->kn_sfflags & hint)
282 kn->kn_fflags |= hint;
283 return (kn->kn_fflags != 0);
284}
285
286/*
287 * Network interface utility routines.
288 *
289 * Routines with ifa_ifwith* names take sockaddr *'s as
290 * parameters.
291 */
292/* ARGSUSED*/
293static void
294if_init(void *dummy __unused)
295{
296
297 IFNET_LOCK_INIT();
298 TAILQ_INIT(&ifnet);
299 TAILQ_INIT(&ifg_head);
300 knlist_init(&ifklist, NULL, NULL, NULL, NULL);
301 if_grow(); /* create initial table */
302 ifdev_byindex(0) = make_dev(&net_cdevsw, 0,
303 UID_ROOT, GID_WHEEL, 0600, "network");
304 if_clone_init();
305}
306
307static void
308if_grow(void)
309{
310 u_int n;
311 struct ifindex_entry *e;
312
313 if_indexlim <<= 1;
314 n = if_indexlim * sizeof(*e);
315 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
316 if (ifindex_table != NULL) {
317 memcpy((caddr_t)e, (caddr_t)ifindex_table, n/2);
318 free((caddr_t)ifindex_table, M_IFNET);
319 }
320 ifindex_table = e;
321}
322
323/* ARGSUSED*/
324static void
325if_check(void *dummy __unused)
326{
327 struct ifnet *ifp;
328 int s;
329
330 s = splimp();
331 IFNET_RLOCK(); /* could sleep on rare error; mostly okay XXX */
332 TAILQ_FOREACH(ifp, &ifnet, if_link) {
333 if (ifp->if_snd.ifq_maxlen == 0) {
334 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
335 ifp->if_snd.ifq_maxlen = ifqmaxlen;
336 }
337 if (!mtx_initialized(&ifp->if_snd.ifq_mtx)) {
338 if_printf(ifp,
339 "XXX: driver didn't initialize queue mtx\n");
340 mtx_init(&ifp->if_snd.ifq_mtx, "unknown",
341 MTX_NETWORK_LOCK, MTX_DEF);
342 }
343 }
344 IFNET_RUNLOCK();
345 splx(s);
346 if_slowtimo(0);
347}
348
349/*
350 * Allocate a struct ifnet and in index for an interface.
351 */
352struct ifnet*
353if_alloc(u_char type)
354{
355 struct ifnet *ifp;
356
357 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
358
359 /*
360 * Try to find an empty slot below if_index. If we fail, take
361 * the next slot.
362 *
363 * XXX: should be locked!
364 */
365 for (ifp->if_index = 1; ifp->if_index <= if_index; ifp->if_index++) {
366 if (ifnet_byindex(ifp->if_index) == NULL)
367 break;
368 }
369 /* Catch if_index overflow. */
370 if (ifp->if_index < 1) {
371 free(ifp, M_IFNET);
372 return (NULL);
373 }
374 if (ifp->if_index > if_index)
375 if_index = ifp->if_index;
376 if (if_index >= if_indexlim)
377 if_grow();
378 ifnet_byindex(ifp->if_index) = ifp;
379
380 ifp->if_type = type;
381
382 if (if_com_alloc[type] != NULL) {
383 ifp->if_l2com = if_com_alloc[type](type, ifp);
384 if (ifp->if_l2com == NULL) {
385 free(ifp, M_IFNET);
386 return (NULL);
387 }
388 }
389 IF_ADDR_LOCK_INIT(ifp);
390
391 return (ifp);
392}
393
394void
395if_free(struct ifnet *ifp)
396{
397
398 /* Do not add code to this function! Add it to if_free_type(). */
399 if_free_type(ifp, ifp->if_type);
400}
401
402void
403if_free_type(struct ifnet *ifp, u_char type)
404{
405
406 if (ifp != ifnet_byindex(ifp->if_index)) {
407 if_printf(ifp, "%s: value was not if_alloced, skipping\n",
408 __func__);
409 return;
410 }
411
412 IF_ADDR_LOCK_DESTROY(ifp);
413
414 ifnet_byindex(ifp->if_index) = NULL;
415
416 /* XXX: should be locked with if_findindex() */
417 while (if_index > 0 && ifnet_byindex(if_index) == NULL)
418 if_index--;
419
420 if (if_com_free[type] != NULL)
421 if_com_free[type](ifp->if_l2com, type);
422
423 free(ifp, M_IFNET);
424};
425
426/*
427 * Attach an interface to the
428 * list of "active" interfaces.
429 */
430void
431if_attach(struct ifnet *ifp)
432{
433 unsigned socksize, ifasize;
434 int namelen, masklen;
435 struct sockaddr_dl *sdl;
436 struct ifaddr *ifa;
437
438 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
439 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
440 ifp->if_xname);
441
442 TASK_INIT(&ifp->if_starttask, 0, if_start_deferred, ifp);
443 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
444 IF_AFDATA_LOCK_INIT(ifp);
445 ifp->if_afdata_initialized = 0;
446 /*
447 * XXX -
448 * The old code would work if the interface passed a pre-existing
449 * chain of ifaddrs to this code. We don't trust our callers to
450 * properly initialize the tailq, however, so we no longer allow
451 * this unlikely case.
452 */
453 TAILQ_INIT(&ifp->if_addrhead);
454 TAILQ_INIT(&ifp->if_prefixhead);
455 TAILQ_INIT(&ifp->if_multiaddrs);
456 TAILQ_INIT(&ifp->if_groups);
457
458 if_addgroup(ifp, IFG_ALL);
459
460 knlist_init(&ifp->if_klist, NULL, NULL, NULL, NULL);
461 getmicrotime(&ifp->if_lastchange);
462 ifp->if_data.ifi_epoch = time_uptime;
463 ifp->if_data.ifi_datalen = sizeof(struct if_data);
464
465#ifdef MAC
466 mac_init_ifnet(ifp);
467 mac_create_ifnet(ifp);
468#endif
469
470 ifdev_byindex(ifp->if_index) = make_dev(&net_cdevsw,
471 unit2minor(ifp->if_index),
472 UID_ROOT, GID_WHEEL, 0600, "%s/%s",
473 net_cdevsw.d_name, ifp->if_xname);
474 make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d",
475 net_cdevsw.d_name, ifp->if_index);
476
477 mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
478
479 /*
480 * create a Link Level name for this device
481 */
482 namelen = strlen(ifp->if_xname);
483 /*
484 * Always save enough space for any possiable name so we can do
485 * a rename in place later.
486 */
487 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
488 socksize = masklen + ifp->if_addrlen;
489 if (socksize < sizeof(*sdl))
490 socksize = sizeof(*sdl);
491 socksize = roundup2(socksize, sizeof(long));
492 ifasize = sizeof(*ifa) + 2 * socksize;
493 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
494 IFA_LOCK_INIT(ifa);
495 sdl = (struct sockaddr_dl *)(ifa + 1);
496 sdl->sdl_len = socksize;
497 sdl->sdl_family = AF_LINK;
498 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
499 sdl->sdl_nlen = namelen;
500 sdl->sdl_index = ifp->if_index;
501 sdl->sdl_type = ifp->if_type;
502 ifp->if_addr = ifa;
503 ifa->ifa_ifp = ifp;
504 ifa->ifa_rtrequest = link_rtrequest;
505 ifa->ifa_addr = (struct sockaddr *)sdl;
506 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
507 ifa->ifa_netmask = (struct sockaddr *)sdl;
508 sdl->sdl_len = masklen;
509 while (namelen != 0)
510 sdl->sdl_data[--namelen] = 0xff;
511 ifa->ifa_refcnt = 1;
512 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
513 ifp->if_broadcastaddr = NULL; /* reliably crash if used uninitialized */
514 ifp->if_snd.altq_type = 0;
515 ifp->if_snd.altq_disc = NULL;
516 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
517 ifp->if_snd.altq_tbr = NULL;
518 ifp->if_snd.altq_ifp = ifp;
519
520 IFNET_WLOCK();
521 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
522 IFNET_WUNLOCK();
523
524 if (domain_init_status >= 2)
525 if_attachdomain1(ifp);
526
527 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
528 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
529
530 /* Announce the interface. */
531 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
532
533 if (ifp->if_watchdog != NULL)
534 if_printf(ifp, "using obsoleted if_watchdog interface\n");
535}
536
537static void
538if_attachdomain(void *dummy)
539{
540 struct ifnet *ifp;
541 int s;
542
543 s = splnet();
544 TAILQ_FOREACH(ifp, &ifnet, if_link)
545 if_attachdomain1(ifp);
546 splx(s);
547}
548SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
549 if_attachdomain, NULL);
550
551static void
552if_attachdomain1(struct ifnet *ifp)
553{
554 struct domain *dp;
555 int s;
556
557 s = splnet();
558
559 /*
560 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
561 * cannot lock ifp->if_afdata initialization, entirely.
562 */
563 if (IF_AFDATA_TRYLOCK(ifp) == 0) {
564 splx(s);
565 return;
566 }
567 if (ifp->if_afdata_initialized >= domain_init_status) {
568 IF_AFDATA_UNLOCK(ifp);
569 splx(s);
570 printf("if_attachdomain called more than once on %s\n",
571 ifp->if_xname);
572 return;
573 }
574 ifp->if_afdata_initialized = domain_init_status;
575 IF_AFDATA_UNLOCK(ifp);
576
577 /* address family dependent data region */
578 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
579 for (dp = domains; dp; dp = dp->dom_next) {
580 if (dp->dom_ifattach)
581 ifp->if_afdata[dp->dom_family] =
582 (*dp->dom_ifattach)(ifp);
583 }
584
585 splx(s);
586}
587
588/*
589 * Remove any unicast or broadcast network addresses from an interface.
590 */
591
592void
593if_purgeaddrs(struct ifnet *ifp)
594{
595 struct ifaddr *ifa, *next;
596
597 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
598 if (ifa->ifa_addr->sa_family == AF_LINK)
599 continue;
600#ifdef INET
601 /* XXX: Ugly!! ad hoc just for INET */
602 if (ifa->ifa_addr->sa_family == AF_INET) {
603 struct ifaliasreq ifr;
604
605 bzero(&ifr, sizeof(ifr));
606 ifr.ifra_addr = *ifa->ifa_addr;
607 if (ifa->ifa_dstaddr)
608 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
609 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
610 NULL) == 0)
611 continue;
612 }
613#endif /* INET */
614#ifdef INET6
615 if (ifa->ifa_addr->sa_family == AF_INET6) {
616 in6_purgeaddr(ifa);
617 /* ifp_addrhead is already updated */
618 continue;
619 }
620#endif /* INET6 */
621 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
622 IFAFREE(ifa);
623 }
624}
625
626/*
627 * Remove any multicast network addresses from an interface.
628 */
629static void
630if_purgemaddrs(struct ifnet *ifp)
631{
632 struct ifmultiaddr *ifma;
633 struct ifmultiaddr *next;
634
635 IF_ADDR_LOCK(ifp);
636 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
637 if_delmulti_locked(ifp, ifma, 1);
638 IF_ADDR_UNLOCK(ifp);
639}
640
641/*
642 * Detach an interface, removing it from the
643 * list of "active" interfaces.
644 *
645 * XXXRW: There are some significant questions about event ordering, and
646 * how to prevent things from starting to use the interface during detach.
647 */
648void
649if_detach(struct ifnet *ifp)
650{
651 struct ifaddr *ifa;
652 struct radix_node_head *rnh;
653 int s;
654 int i;
655 struct domain *dp;
656 struct ifnet *iter;
657 int found = 0;
658
659 IFNET_WLOCK();
660 TAILQ_FOREACH(iter, &ifnet, if_link)
661 if (iter == ifp) {
662 TAILQ_REMOVE(&ifnet, ifp, if_link);
663 found = 1;
664 break;
665 }
666 IFNET_WUNLOCK();
667 if (!found)
668 return;
669
670 /*
671 * Remove/wait for pending events.
672 */
673 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
674
675 /*
676 * Remove routes and flush queues.
677 */
678 s = splnet();
679 if_down(ifp);
680#ifdef ALTQ
681 if (ALTQ_IS_ENABLED(&ifp->if_snd))
682 altq_disable(&ifp->if_snd);
683 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
684 altq_detach(&ifp->if_snd);
685#endif
686
687 if_purgeaddrs(ifp);
688
689#ifdef INET
690 in_ifdetach(ifp);
691#endif
692
693#ifdef INET6
694 /*
695 * Remove all IPv6 kernel structs related to ifp. This should be done
696 * before removing routing entries below, since IPv6 interface direct
697 * routes are expected to be removed by the IPv6-specific kernel API.
698 * Otherwise, the kernel will detect some inconsistency and bark it.
699 */
700 in6_ifdetach(ifp);
701#endif
702 if_purgemaddrs(ifp);
703
704 /*
705 * Remove link ifaddr pointer and maybe decrement if_index.
706 * Clean up all addresses.
707 */
708 ifp->if_addr = NULL;
709 destroy_dev(ifdev_byindex(ifp->if_index));
710 ifdev_byindex(ifp->if_index) = NULL;
711
712 /* We can now free link ifaddr. */
713 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
714 ifa = TAILQ_FIRST(&ifp->if_addrhead);
715 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
716 IFAFREE(ifa);
717 }
718
719 /*
720 * Delete all remaining routes using this interface
721 * Unfortuneatly the only way to do this is to slog through
722 * the entire routing table looking for routes which point
723 * to this interface...oh well...
724 */
725 for (i = 1; i <= AF_MAX; i++) {
726 if ((rnh = rt_tables[i]) == NULL)
727 continue;
728 RADIX_NODE_HEAD_LOCK(rnh);
729 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
730 RADIX_NODE_HEAD_UNLOCK(rnh);
731 }
732
733 /* Announce that the interface is gone. */
734 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
735 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
736 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
737
738 IF_AFDATA_LOCK(ifp);
739 for (dp = domains; dp; dp = dp->dom_next) {
740 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
741 (*dp->dom_ifdetach)(ifp,
742 ifp->if_afdata[dp->dom_family]);
743 }
744 IF_AFDATA_UNLOCK(ifp);
745
746#ifdef MAC
747 mac_destroy_ifnet(ifp);
748#endif /* MAC */
749 KNOTE_UNLOCKED(&ifp->if_klist, NOTE_EXIT);
750 knlist_clear(&ifp->if_klist, 0);
751 knlist_destroy(&ifp->if_klist);
752 mtx_destroy(&ifp->if_snd.ifq_mtx);
753 IF_AFDATA_DESTROY(ifp);
754 splx(s);
755}
756
757/*
758 * Add a group to an interface
759 */
760int
761if_addgroup(struct ifnet *ifp, const char *groupname)
762{
763 struct ifg_list *ifgl;
764 struct ifg_group *ifg = NULL;
765 struct ifg_member *ifgm;
766
767 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
768 groupname[strlen(groupname) - 1] <= '9')
769 return (EINVAL);
770
771 IFNET_WLOCK();
772 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
773 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
774 IFNET_WUNLOCK();
775 return (EEXIST);
776 }
777
778 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
779 M_NOWAIT)) == NULL) {
780 IFNET_WUNLOCK();
781 return (ENOMEM);
782 }
783
784 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
785 M_TEMP, M_NOWAIT)) == NULL) {
786 free(ifgl, M_TEMP);
787 IFNET_WUNLOCK();
788 return (ENOMEM);
789 }
790
791 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
792 if (!strcmp(ifg->ifg_group, groupname))
793 break;
794
795 if (ifg == NULL) {
796 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
797 M_TEMP, M_NOWAIT)) == NULL) {
798 free(ifgl, M_TEMP);
799 free(ifgm, M_TEMP);
800 IFNET_WUNLOCK();
801 return (ENOMEM);
802 }
803 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
804 ifg->ifg_refcnt = 0;
805 TAILQ_INIT(&ifg->ifg_members);
806 EVENTHANDLER_INVOKE(group_attach_event, ifg);
807 TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
808 }
809
810 ifg->ifg_refcnt++;
811 ifgl->ifgl_group = ifg;
812 ifgm->ifgm_ifp = ifp;
813
814 IF_ADDR_LOCK(ifp);
815 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
816 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
817 IF_ADDR_UNLOCK(ifp);
818
819 IFNET_WUNLOCK();
820
821 EVENTHANDLER_INVOKE(group_change_event, groupname);
822
823 return (0);
824}
825
826/*
827 * Remove a group from an interface
828 */
829int
830if_delgroup(struct ifnet *ifp, const char *groupname)
831{
832 struct ifg_list *ifgl;
833 struct ifg_member *ifgm;
834
835 IFNET_WLOCK();
836 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
837 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
838 break;
839 if (ifgl == NULL) {
840 IFNET_WUNLOCK();
841 return (ENOENT);
842 }
843
844 IF_ADDR_LOCK(ifp);
845 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
846 IF_ADDR_UNLOCK(ifp);
847
848 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
849 if (ifgm->ifgm_ifp == ifp)
850 break;
851
852 if (ifgm != NULL) {
853 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
854 free(ifgm, M_TEMP);
855 }
856
857 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
858 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
859 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
860 free(ifgl->ifgl_group, M_TEMP);
861 }
862 IFNET_WUNLOCK();
863
864 free(ifgl, M_TEMP);
865
866 EVENTHANDLER_INVOKE(group_change_event, groupname);
867
868 return (0);
869}
870
871/*
872 * Stores all groups from an interface in memory pointed
873 * to by data
874 */
875static int
876if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
877{
878 int len, error;
879 struct ifg_list *ifgl;
880 struct ifg_req ifgrq, *ifgp;
881 struct ifgroupreq *ifgr = data;
882
883 if (ifgr->ifgr_len == 0) {
884 IF_ADDR_LOCK(ifp);
885 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
886 ifgr->ifgr_len += sizeof(struct ifg_req);
887 IF_ADDR_UNLOCK(ifp);
888 return (0);
889 }
890
891 len = ifgr->ifgr_len;
892 ifgp = ifgr->ifgr_groups;
893 /* XXX: wire */
894 IF_ADDR_LOCK(ifp);
895 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
896 if (len < sizeof(ifgrq)) {
897 IF_ADDR_UNLOCK(ifp);
898 return (EINVAL);
899 }
900 bzero(&ifgrq, sizeof ifgrq);
901 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
902 sizeof(ifgrq.ifgrq_group));
903 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
904 IF_ADDR_UNLOCK(ifp);
905 return (error);
906 }
907 len -= sizeof(ifgrq);
908 ifgp++;
909 }
910 IF_ADDR_UNLOCK(ifp);
911
912 return (0);
913}
914
915/*
916 * Stores all members of a group in memory pointed to by data
917 */
918static int
919if_getgroupmembers(struct ifgroupreq *data)
920{
921 struct ifgroupreq *ifgr = data;
922 struct ifg_group *ifg;
923 struct ifg_member *ifgm;
924 struct ifg_req ifgrq, *ifgp;
925 int len, error;
926
927 IFNET_RLOCK();
928 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
929 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
930 break;
931 if (ifg == NULL) {
932 IFNET_RUNLOCK();
933 return (ENOENT);
934 }
935
936 if (ifgr->ifgr_len == 0) {
937 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
938 ifgr->ifgr_len += sizeof(ifgrq);
939 IFNET_RUNLOCK();
940 return (0);
941 }
942
943 len = ifgr->ifgr_len;
944 ifgp = ifgr->ifgr_groups;
945 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
946 if (len < sizeof(ifgrq)) {
947 IFNET_RUNLOCK();
948 return (EINVAL);
949 }
950 bzero(&ifgrq, sizeof ifgrq);
951 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
952 sizeof(ifgrq.ifgrq_member));
953 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
954 IFNET_RUNLOCK();
955 return (error);
956 }
957 len -= sizeof(ifgrq);
958 ifgp++;
959 }
960 IFNET_RUNLOCK();
961
962 return (0);
963}
964
965/*
966 * Delete Routes for a Network Interface
967 *
968 * Called for each routing entry via the rnh->rnh_walktree() call above
969 * to delete all route entries referencing a detaching network interface.
970 *
971 * Arguments:
972 * rn pointer to node in the routing table
973 * arg argument passed to rnh->rnh_walktree() - detaching interface
974 *
975 * Returns:
976 * 0 successful
977 * errno failed - reason indicated
978 *
979 */
980static int
981if_rtdel(struct radix_node *rn, void *arg)
982{
983 struct rtentry *rt = (struct rtentry *)rn;
984 struct ifnet *ifp = arg;
985 int err;
986
987 if (rt->rt_ifp == ifp) {
988
989 /*
990 * Protect (sorta) against walktree recursion problems
991 * with cloned routes
992 */
993 if ((rt->rt_flags & RTF_UP) == 0)
994 return (0);
995
996 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
997 rt_mask(rt), rt->rt_flags,
998 (struct rtentry **) NULL);
999 if (err) {
1000 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1001 }
1002 }
1003
1004 return (0);
1005}
1006
1007/*
1008 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1009 * structs used to represent other address families, it is necessary
1010 * to perform a different comparison.
1011 */
1012
1013#define sa_equal(a1, a2) \
1014 (bcmp((a1), (a2), ((a1))->sa_len) == 0)
1015
1016#define sa_dl_equal(a1, a2) \
1017 ((((struct sockaddr_dl *)(a1))->sdl_len == \
1018 ((struct sockaddr_dl *)(a2))->sdl_len) && \
1019 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
1020 LLADDR((struct sockaddr_dl *)(a2)), \
1021 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1022
1023/*
1024 * Locate an interface based on a complete address.
1025 */
1026/*ARGSUSED*/
1027struct ifaddr *
1028ifa_ifwithaddr(struct sockaddr *addr)
1029{
1030 struct ifnet *ifp;
1031 struct ifaddr *ifa;
1032
1033 IFNET_RLOCK();
1034 TAILQ_FOREACH(ifp, &ifnet, if_link)
1035 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1036 if (ifa->ifa_addr->sa_family != addr->sa_family)
1037 continue;
1038 if (sa_equal(addr, ifa->ifa_addr))
1039 goto done;
1040 /* IP6 doesn't have broadcast */
1041 if ((ifp->if_flags & IFF_BROADCAST) &&
1042 ifa->ifa_broadaddr &&
1043 ifa->ifa_broadaddr->sa_len != 0 &&
1044 sa_equal(ifa->ifa_broadaddr, addr))
1045 goto done;
1046 }
1047 ifa = NULL;
1048done:
1049 IFNET_RUNLOCK();
1050 return (ifa);
1051}
1052
1053/*
1054 * Locate an interface based on the broadcast address.
1055 */
1056/* ARGSUSED */
1057struct ifaddr *
1058ifa_ifwithbroadaddr(struct sockaddr *addr)
1059{
1060 struct ifnet *ifp;
1061 struct ifaddr *ifa;
1062
1063 IFNET_RLOCK();
1064 TAILQ_FOREACH(ifp, &ifnet, if_link)
1065 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1066 if (ifa->ifa_addr->sa_family != addr->sa_family)
1067 continue;
1068 if ((ifp->if_flags & IFF_BROADCAST) &&
1069 ifa->ifa_broadaddr &&
1070 ifa->ifa_broadaddr->sa_len != 0 &&
1071 sa_equal(ifa->ifa_broadaddr, addr))
1072 goto done;
1073 }
1074 ifa = NULL;
1075done:
1076 IFNET_RUNLOCK();
1077 return (ifa);
1078}
1079
1080/*
1081 * Locate the point to point interface with a given destination address.
1082 */
1083/*ARGSUSED*/
1084struct ifaddr *
1085ifa_ifwithdstaddr(struct sockaddr *addr)
1086{
1087 struct ifnet *ifp;
1088 struct ifaddr *ifa;
1089
1090 IFNET_RLOCK();
1091 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1092 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1093 continue;
1094 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1095 if (ifa->ifa_addr->sa_family != addr->sa_family)
1096 continue;
1097 if (ifa->ifa_dstaddr &&
1098 sa_equal(addr, ifa->ifa_dstaddr))
1099 goto done;
1100 }
1101 }
1102 ifa = NULL;
1103done:
1104 IFNET_RUNLOCK();
1105 return (ifa);
1106}
1107
1108/*
1109 * Find an interface on a specific network. If many, choice
1110 * is most specific found.
1111 */
1112struct ifaddr *
1113ifa_ifwithnet(struct sockaddr *addr)
1114{
1115 struct ifnet *ifp;
1116 struct ifaddr *ifa;
1117 struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
1118 u_int af = addr->sa_family;
1119 char *addr_data = addr->sa_data, *cplim;
1120
1121 /*
1122 * AF_LINK addresses can be looked up directly by their index number,
1123 * so do that if we can.
1124 */
1125 if (af == AF_LINK) {
1126 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1127 if (sdl->sdl_index && sdl->sdl_index <= if_index)
1128 return (ifaddr_byindex(sdl->sdl_index));
1129 }
1130
1131 /*
1132 * Scan though each interface, looking for ones that have
1133 * addresses in this address family.
1134 */
1135 IFNET_RLOCK();
1136 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1137 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1138 char *cp, *cp2, *cp3;
1139
1140 if (ifa->ifa_addr->sa_family != af)
1141next: continue;
1142 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1143 /*
1144 * This is a bit broken as it doesn't
1145 * take into account that the remote end may
1146 * be a single node in the network we are
1147 * looking for.
1148 * The trouble is that we don't know the
1149 * netmask for the remote end.
1150 */
1151 if (ifa->ifa_dstaddr != 0 &&
1152 sa_equal(addr, ifa->ifa_dstaddr))
1153 goto done;
1154 } else {
1155 /*
1156 * if we have a special address handler,
1157 * then use it instead of the generic one.
1158 */
1159 if (ifa->ifa_claim_addr) {
1160 if ((*ifa->ifa_claim_addr)(ifa, addr))
1161 goto done;
1162 continue;
1163 }
1164
1165 /*
1166 * Scan all the bits in the ifa's address.
1167 * If a bit dissagrees with what we are
1168 * looking for, mask it with the netmask
1169 * to see if it really matters.
1170 * (A byte at a time)
1171 */
1172 if (ifa->ifa_netmask == 0)
1173 continue;
1174 cp = addr_data;
1175 cp2 = ifa->ifa_addr->sa_data;
1176 cp3 = ifa->ifa_netmask->sa_data;
1177 cplim = ifa->ifa_netmask->sa_len
1178 + (char *)ifa->ifa_netmask;
1179 while (cp3 < cplim)
1180 if ((*cp++ ^ *cp2++) & *cp3++)
1181 goto next; /* next address! */
1182 /*
1183 * If the netmask of what we just found
1184 * is more specific than what we had before
1185 * (if we had one) then remember the new one
1186 * before continuing to search
1187 * for an even better one.
1188 */
1189 if (ifa_maybe == 0 ||
1190 rn_refines((caddr_t)ifa->ifa_netmask,
1191 (caddr_t)ifa_maybe->ifa_netmask))
1192 ifa_maybe = ifa;
1193 }
1194 }
1195 }
1196 ifa = ifa_maybe;
1197done:
1198 IFNET_RUNLOCK();
1199 return (ifa);
1200}
1201
1202/*
1203 * Find an interface address specific to an interface best matching
1204 * a given address.
1205 */
1206struct ifaddr *
1207ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1208{
1209 struct ifaddr *ifa;
1210 char *cp, *cp2, *cp3;
1211 char *cplim;
1212 struct ifaddr *ifa_maybe = 0;
1213 u_int af = addr->sa_family;
1214
1215 if (af >= AF_MAX)
1216 return (0);
1217 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1218 if (ifa->ifa_addr->sa_family != af)
1219 continue;
1220 if (ifa_maybe == 0)
1221 ifa_maybe = ifa;
1222 if (ifa->ifa_netmask == 0) {
1223 if (sa_equal(addr, ifa->ifa_addr) ||
1224 (ifa->ifa_dstaddr &&
1225 sa_equal(addr, ifa->ifa_dstaddr)))
1226 goto done;
1227 continue;
1228 }
1229 if (ifp->if_flags & IFF_POINTOPOINT) {
1230 if (sa_equal(addr, ifa->ifa_dstaddr))
1231 goto done;
1232 } else {
1233 cp = addr->sa_data;
1234 cp2 = ifa->ifa_addr->sa_data;
1235 cp3 = ifa->ifa_netmask->sa_data;
1236 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1237 for (; cp3 < cplim; cp3++)
1238 if ((*cp++ ^ *cp2++) & *cp3)
1239 break;
1240 if (cp3 == cplim)
1241 goto done;
1242 }
1243 }
1244 ifa = ifa_maybe;
1245done:
1246 return (ifa);
1247}
1248
1249#include <net/route.h>
1250
1251/*
1252 * Default action when installing a route with a Link Level gateway.
1253 * Lookup an appropriate real ifa to point to.
1254 * This should be moved to /sys/net/link.c eventually.
1255 */
1256static void
1257link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1258{
1259 struct ifaddr *ifa, *oifa;
1260 struct sockaddr *dst;
1261 struct ifnet *ifp;
1262
1263 RT_LOCK_ASSERT(rt);
1264
1265 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1266 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1267 return;
1268 ifa = ifaof_ifpforaddr(dst, ifp);
1269 if (ifa) {
1270 IFAREF(ifa); /* XXX */
1271 oifa = rt->rt_ifa;
1272 rt->rt_ifa = ifa;
1273 IFAFREE(oifa);
1274 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1275 ifa->ifa_rtrequest(cmd, rt, info);
1276 }
1277}
1278
1279/*
1280 * Mark an interface down and notify protocols of
1281 * the transition.
1282 * NOTE: must be called at splnet or eqivalent.
1283 */
1284static void
1285if_unroute(struct ifnet *ifp, int flag, int fam)
1286{
1287 struct ifaddr *ifa;
1288
1289 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
1290
1291 ifp->if_flags &= ~flag;
1292 getmicrotime(&ifp->if_lastchange);
1293 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1294 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1295 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1296 if_qflush(&ifp->if_snd);
1297#ifdef DEV_CARP
1298 if (ifp->if_carp)
1299 carp_carpdev_state(ifp->if_carp);
1300#endif
1301 rt_ifmsg(ifp);
1302}
1303
1304/*
1305 * Mark an interface up and notify protocols of
1306 * the transition.
1307 * NOTE: must be called at splnet or eqivalent.
1308 */
1309static void
1310if_route(struct ifnet *ifp, int flag, int fam)
1311{
1312 struct ifaddr *ifa;
1313
1314 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
1315
1316 ifp->if_flags |= flag;
1317 getmicrotime(&ifp->if_lastchange);
1318 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1319 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1320 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1321#ifdef DEV_CARP
1322 if (ifp->if_carp)
1323 carp_carpdev_state(ifp->if_carp);
1324#endif
1325 rt_ifmsg(ifp);
1326#ifdef INET6
1327 in6_if_up(ifp);
1328#endif
1329}
1330
1331void (*vlan_link_state_p)(struct ifnet *, int); /* XXX: private from if_vlan */
1332void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
1333
1334/*
1335 * Handle a change in the interface link state. To avoid LORs
1336 * between driver lock and upper layer locks, as well as possible
1337 * recursions, we post event to taskqueue, and all job
1338 * is done in static do_link_state_change().
1339 */
1340void
1341if_link_state_change(struct ifnet *ifp, int link_state)
1342{
1343 /* Return if state hasn't changed. */
1344 if (ifp->if_link_state == link_state)
1345 return;
1346
1347 ifp->if_link_state = link_state;
1348
1349 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
1350}
1351
1352static void
1353do_link_state_change(void *arg, int pending)
1354{
1355 struct ifnet *ifp = (struct ifnet *)arg;
1356 int link_state = ifp->if_link_state;
1357 int link;
1358
1359 /* Notify that the link state has changed. */
1360 rt_ifmsg(ifp);
1361 if (link_state == LINK_STATE_UP)
1362 link = NOTE_LINKUP;
1363 else if (link_state == LINK_STATE_DOWN)
1364 link = NOTE_LINKDOWN;
1365 else
1366 link = NOTE_LINKINV;
1367 KNOTE_UNLOCKED(&ifp->if_klist, link);
1368 if (ifp->if_vlantrunk != NULL)
1369 (*vlan_link_state_p)(ifp, link);
1370
1371 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
1372 IFP2AC(ifp)->ac_netgraph != NULL)
1373 (*ng_ether_link_state_p)(ifp, link_state);
1374#ifdef DEV_CARP
1375 if (ifp->if_carp)
1376 carp_carpdev_state(ifp->if_carp);
1377#endif
1378 if (ifp->if_bridge) {
1379 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!"));
1380 (*bstp_linkstate_p)(ifp, link_state);
1381 }
1382 if (ifp->if_trunk) {
1383 KASSERT(trunk_linkstate_p != NULL,("if_trunk not loaded!"));
1384 (*trunk_linkstate_p)(ifp, link_state);
1385 }
1381
1382 devctl_notify("IFNET", ifp->if_xname,
1383 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1384 if (pending > 1)
1385 if_printf(ifp, "%d link states coalesced\n", pending);
1386 if (log_link_state_change)
1387 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
1388 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
1389}
1390
1391/*
1392 * Mark an interface down and notify protocols of
1393 * the transition.
1394 * NOTE: must be called at splnet or eqivalent.
1395 */
1396void
1397if_down(struct ifnet *ifp)
1398{
1399
1400 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1401}
1402
1403/*
1404 * Mark an interface up and notify protocols of
1405 * the transition.
1406 * NOTE: must be called at splnet or eqivalent.
1407 */
1408void
1409if_up(struct ifnet *ifp)
1410{
1411
1412 if_route(ifp, IFF_UP, AF_UNSPEC);
1413}
1414
1415/*
1416 * Flush an interface queue.
1417 */
1418static void
1419if_qflush(struct ifaltq *ifq)
1420{
1421 struct mbuf *m, *n;
1422
1423 IFQ_LOCK(ifq);
1424#ifdef ALTQ
1425 if (ALTQ_IS_ENABLED(ifq))
1426 ALTQ_PURGE(ifq);
1427#endif
1428 n = ifq->ifq_head;
1429 while ((m = n) != 0) {
1430 n = m->m_act;
1431 m_freem(m);
1432 }
1433 ifq->ifq_head = 0;
1434 ifq->ifq_tail = 0;
1435 ifq->ifq_len = 0;
1436 IFQ_UNLOCK(ifq);
1437}
1438
1439/*
1440 * Handle interface watchdog timer routines. Called
1441 * from softclock, we decrement timers (if set) and
1442 * call the appropriate interface routine on expiration.
1443 *
1444 * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called
1445 * holding Giant. If we switch to an MPSAFE callout, we likely need to grab
1446 * Giant before entering if_watchdog() on an IFF_NEEDSGIANT interface.
1447 */
1448static void
1449if_slowtimo(void *arg)
1450{
1451 struct ifnet *ifp;
1452 int s = splimp();
1453
1454 IFNET_RLOCK();
1455 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1456 if (ifp->if_timer == 0 || --ifp->if_timer)
1457 continue;
1458 if (ifp->if_watchdog)
1459 (*ifp->if_watchdog)(ifp);
1460 }
1461 IFNET_RUNLOCK();
1462 splx(s);
1463 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
1464}
1465
1466/*
1467 * Map interface name to
1468 * interface structure pointer.
1469 */
1470struct ifnet *
1471ifunit(const char *name)
1472{
1473 struct ifnet *ifp;
1474
1475 IFNET_RLOCK();
1476 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1477 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
1478 break;
1479 }
1480 IFNET_RUNLOCK();
1481 return (ifp);
1482}
1483
1484/*
1485 * Hardware specific interface ioctls.
1486 */
1487static int
1488ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
1489{
1490 struct ifreq *ifr;
1491 struct ifstat *ifs;
1492 int error = 0;
1493 int new_flags, temp_flags;
1494 size_t namelen, onamelen;
1495 char new_name[IFNAMSIZ];
1496 struct ifaddr *ifa;
1497 struct sockaddr_dl *sdl;
1498
1499 ifr = (struct ifreq *)data;
1500 switch (cmd) {
1501 case SIOCGIFINDEX:
1502 ifr->ifr_index = ifp->if_index;
1503 break;
1504
1505 case SIOCGIFFLAGS:
1506 temp_flags = ifp->if_flags | ifp->if_drv_flags;
1507 ifr->ifr_flags = temp_flags & 0xffff;
1508 ifr->ifr_flagshigh = temp_flags >> 16;
1509 break;
1510
1511 case SIOCGIFCAP:
1512 ifr->ifr_reqcap = ifp->if_capabilities;
1513 ifr->ifr_curcap = ifp->if_capenable;
1514 break;
1515
1516#ifdef MAC
1517 case SIOCGIFMAC:
1518 error = mac_ioctl_ifnet_get(td->td_ucred, ifr, ifp);
1519 break;
1520#endif
1521
1522 case SIOCGIFMETRIC:
1523 ifr->ifr_metric = ifp->if_metric;
1524 break;
1525
1526 case SIOCGIFMTU:
1527 ifr->ifr_mtu = ifp->if_mtu;
1528 break;
1529
1530 case SIOCGIFPHYS:
1531 ifr->ifr_phys = ifp->if_physical;
1532 break;
1533
1534 case SIOCSIFFLAGS:
1535 error = priv_check(td, PRIV_NET_SETIFFLAGS);
1536 if (error)
1537 return (error);
1538 /*
1539 * Currently, no driver owned flags pass the IFF_CANTCHANGE
1540 * check, so we don't need special handling here yet.
1541 */
1542 new_flags = (ifr->ifr_flags & 0xffff) |
1543 (ifr->ifr_flagshigh << 16);
1544 if (ifp->if_flags & IFF_SMART) {
1545 /* Smart drivers twiddle their own routes */
1546 } else if (ifp->if_flags & IFF_UP &&
1547 (new_flags & IFF_UP) == 0) {
1548 int s = splimp();
1549 if_down(ifp);
1550 splx(s);
1551 } else if (new_flags & IFF_UP &&
1552 (ifp->if_flags & IFF_UP) == 0) {
1553 int s = splimp();
1554 if_up(ifp);
1555 splx(s);
1556 }
1557 /* See if permanently promiscuous mode bit is about to flip */
1558 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
1559 if (new_flags & IFF_PPROMISC)
1560 ifp->if_flags |= IFF_PROMISC;
1561 else if (ifp->if_pcount == 0)
1562 ifp->if_flags &= ~IFF_PROMISC;
1563 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
1564 ifp->if_xname,
1565 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
1566 }
1567 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1568 (new_flags &~ IFF_CANTCHANGE);
1569 if (ifp->if_ioctl) {
1570 IFF_LOCKGIANT(ifp);
1571 (void) (*ifp->if_ioctl)(ifp, cmd, data);
1572 IFF_UNLOCKGIANT(ifp);
1573 }
1574 getmicrotime(&ifp->if_lastchange);
1575 break;
1576
1577 case SIOCSIFCAP:
1578 error = priv_check(td, PRIV_NET_SETIFCAP);
1579 if (error)
1580 return (error);
1581 if (ifp->if_ioctl == NULL)
1582 return (EOPNOTSUPP);
1583 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1584 return (EINVAL);
1585 IFF_LOCKGIANT(ifp);
1586 error = (*ifp->if_ioctl)(ifp, cmd, data);
1587 IFF_UNLOCKGIANT(ifp);
1588 if (error == 0)
1589 getmicrotime(&ifp->if_lastchange);
1590 break;
1591
1592#ifdef MAC
1593 case SIOCSIFMAC:
1594 error = mac_ioctl_ifnet_set(td->td_ucred, ifr, ifp);
1595 break;
1596#endif
1597
1598 case SIOCSIFNAME:
1599 error = priv_check(td, PRIV_NET_SETIFNAME);
1600 if (error)
1601 return (error);
1602 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1603 if (error != 0)
1604 return (error);
1605 if (new_name[0] == '\0')
1606 return (EINVAL);
1607 if (ifunit(new_name) != NULL)
1608 return (EEXIST);
1609
1610 /* Announce the departure of the interface. */
1611 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1612 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1613
1614 log(LOG_INFO, "%s: changing name to '%s'\n",
1615 ifp->if_xname, new_name);
1616
1617 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1618 ifa = ifp->if_addr;
1619 IFA_LOCK(ifa);
1620 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1621 namelen = strlen(new_name);
1622 onamelen = sdl->sdl_nlen;
1623 /*
1624 * Move the address if needed. This is safe because we
1625 * allocate space for a name of length IFNAMSIZ when we
1626 * create this in if_attach().
1627 */
1628 if (namelen != onamelen) {
1629 bcopy(sdl->sdl_data + onamelen,
1630 sdl->sdl_data + namelen, sdl->sdl_alen);
1631 }
1632 bcopy(new_name, sdl->sdl_data, namelen);
1633 sdl->sdl_nlen = namelen;
1634 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1635 bzero(sdl->sdl_data, onamelen);
1636 while (namelen != 0)
1637 sdl->sdl_data[--namelen] = 0xff;
1638 IFA_UNLOCK(ifa);
1639
1640 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
1641 /* Announce the return of the interface. */
1642 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1643 break;
1644
1645 case SIOCSIFMETRIC:
1646 error = priv_check(td, PRIV_NET_SETIFMETRIC);
1647 if (error)
1648 return (error);
1649 ifp->if_metric = ifr->ifr_metric;
1650 getmicrotime(&ifp->if_lastchange);
1651 break;
1652
1653 case SIOCSIFPHYS:
1654 error = priv_check(td, PRIV_NET_SETIFPHYS);
1655 if (error)
1656 return (error);
1657 if (ifp->if_ioctl == NULL)
1658 return (EOPNOTSUPP);
1659 IFF_LOCKGIANT(ifp);
1660 error = (*ifp->if_ioctl)(ifp, cmd, data);
1661 IFF_UNLOCKGIANT(ifp);
1662 if (error == 0)
1663 getmicrotime(&ifp->if_lastchange);
1664 break;
1665
1666 case SIOCSIFMTU:
1667 {
1668 u_long oldmtu = ifp->if_mtu;
1669
1670 error = priv_check(td, PRIV_NET_SETIFMTU);
1671 if (error)
1672 return (error);
1673 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1674 return (EINVAL);
1675 if (ifp->if_ioctl == NULL)
1676 return (EOPNOTSUPP);
1677 IFF_LOCKGIANT(ifp);
1678 error = (*ifp->if_ioctl)(ifp, cmd, data);
1679 IFF_UNLOCKGIANT(ifp);
1680 if (error == 0) {
1681 getmicrotime(&ifp->if_lastchange);
1682 rt_ifmsg(ifp);
1683 }
1684 /*
1685 * If the link MTU changed, do network layer specific procedure.
1686 */
1687 if (ifp->if_mtu != oldmtu) {
1688#ifdef INET6
1689 nd6_setmtu(ifp);
1690#endif
1691 }
1692 break;
1693 }
1694
1695 case SIOCADDMULTI:
1696 case SIOCDELMULTI:
1697 if (cmd == SIOCADDMULTI)
1698 error = priv_check(td, PRIV_NET_ADDMULTI);
1699 else
1700 error = priv_check(td, PRIV_NET_DELMULTI);
1701 if (error)
1702 return (error);
1703
1704 /* Don't allow group membership on non-multicast interfaces. */
1705 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1706 return (EOPNOTSUPP);
1707
1708 /* Don't let users screw up protocols' entries. */
1709 if (ifr->ifr_addr.sa_family != AF_LINK)
1710 return (EINVAL);
1711
1712 if (cmd == SIOCADDMULTI) {
1713 struct ifmultiaddr *ifma;
1714
1715 /*
1716 * Userland is only permitted to join groups once
1717 * via the if_addmulti() KPI, because it cannot hold
1718 * struct ifmultiaddr * between calls. It may also
1719 * lose a race while we check if the membership
1720 * already exists.
1721 */
1722 IF_ADDR_LOCK(ifp);
1723 ifma = if_findmulti(ifp, &ifr->ifr_addr);
1724 IF_ADDR_UNLOCK(ifp);
1725 if (ifma != NULL)
1726 error = EADDRINUSE;
1727 else
1728 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1729 } else {
1730 error = if_delmulti(ifp, &ifr->ifr_addr);
1731 }
1732 if (error == 0)
1733 getmicrotime(&ifp->if_lastchange);
1734 break;
1735
1736 case SIOCSIFPHYADDR:
1737 case SIOCDIFPHYADDR:
1738#ifdef INET6
1739 case SIOCSIFPHYADDR_IN6:
1740#endif
1741 case SIOCSLIFPHYADDR:
1742 case SIOCSIFMEDIA:
1743 case SIOCSIFGENERIC:
1744 error = priv_check(td, PRIV_NET_HWIOCTL);
1745 if (error)
1746 return (error);
1747 if (ifp->if_ioctl == NULL)
1748 return (EOPNOTSUPP);
1749 IFF_LOCKGIANT(ifp);
1750 error = (*ifp->if_ioctl)(ifp, cmd, data);
1751 IFF_UNLOCKGIANT(ifp);
1752 if (error == 0)
1753 getmicrotime(&ifp->if_lastchange);
1754 break;
1755
1756 case SIOCGIFSTATUS:
1757 ifs = (struct ifstat *)data;
1758 ifs->ascii[0] = '\0';
1759
1760 case SIOCGIFPSRCADDR:
1761 case SIOCGIFPDSTADDR:
1762 case SIOCGLIFPHYADDR:
1763 case SIOCGIFMEDIA:
1764 case SIOCGIFGENERIC:
1765 if (ifp->if_ioctl == NULL)
1766 return (EOPNOTSUPP);
1767 IFF_LOCKGIANT(ifp);
1768 error = (*ifp->if_ioctl)(ifp, cmd, data);
1769 IFF_UNLOCKGIANT(ifp);
1770 break;
1771
1772 case SIOCSIFLLADDR:
1773 error = priv_check(td, PRIV_NET_SETLLADDR);
1774 if (error)
1775 return (error);
1776 error = if_setlladdr(ifp,
1777 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1778 break;
1779
1780 case SIOCAIFGROUP:
1781 {
1782 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
1783
1784 error = priv_check(td, PRIV_NET_ADDIFGROUP);
1785 if (error)
1786 return (error);
1787 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
1788 return (error);
1789 break;
1790 }
1791
1792 case SIOCGIFGROUP:
1793 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
1794 return (error);
1795 break;
1796
1797 case SIOCDIFGROUP:
1798 {
1799 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
1800
1801 error = priv_check(td, PRIV_NET_DELIFGROUP);
1802 if (error)
1803 return (error);
1804 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
1805 return (error);
1806 break;
1807 }
1808
1809 default:
1810 error = ENOIOCTL;
1811 break;
1812 }
1813 return (error);
1814}
1815
1816/*
1817 * Interface ioctls.
1818 */
1819int
1820ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
1821{
1822 struct ifnet *ifp;
1823 struct ifreq *ifr;
1824 int error;
1825 int oif_flags;
1826
1827 switch (cmd) {
1828 case SIOCGIFCONF:
1829 case OSIOCGIFCONF:
1830#ifdef __amd64__
1831 case SIOCGIFCONF32:
1832#endif
1833 return (ifconf(cmd, data));
1834 }
1835 ifr = (struct ifreq *)data;
1836
1837 switch (cmd) {
1838 case SIOCIFCREATE:
1839 case SIOCIFCREATE2:
1840 error = priv_check(td, PRIV_NET_IFCREATE);
1841 if (error)
1842 return (error);
1843 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
1844 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
1845 case SIOCIFDESTROY:
1846 error = priv_check(td, PRIV_NET_IFDESTROY);
1847 if (error)
1848 return (error);
1849 return if_clone_destroy(ifr->ifr_name);
1850
1851 case SIOCIFGCLONERS:
1852 return (if_clone_list((struct if_clonereq *)data));
1853 case SIOCGIFGMEMB:
1854 return (if_getgroupmembers((struct ifgroupreq *)data));
1855 }
1856
1857 ifp = ifunit(ifr->ifr_name);
1858 if (ifp == 0)
1859 return (ENXIO);
1860
1861 error = ifhwioctl(cmd, ifp, data, td);
1862 if (error != ENOIOCTL)
1863 return (error);
1864
1865 oif_flags = ifp->if_flags;
1866 if (so->so_proto == 0)
1867 return (EOPNOTSUPP);
1868#ifndef COMPAT_43
1869 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
1870 data,
1871 ifp, td));
1872#else
1873 {
1874 int ocmd = cmd;
1875
1876 switch (cmd) {
1877
1878 case SIOCSIFDSTADDR:
1879 case SIOCSIFADDR:
1880 case SIOCSIFBRDADDR:
1881 case SIOCSIFNETMASK:
1882#if BYTE_ORDER != BIG_ENDIAN
1883 if (ifr->ifr_addr.sa_family == 0 &&
1884 ifr->ifr_addr.sa_len < 16) {
1885 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1886 ifr->ifr_addr.sa_len = 16;
1887 }
1888#else
1889 if (ifr->ifr_addr.sa_len == 0)
1890 ifr->ifr_addr.sa_len = 16;
1891#endif
1892 break;
1893
1894 case OSIOCGIFADDR:
1895 cmd = SIOCGIFADDR;
1896 break;
1897
1898 case OSIOCGIFDSTADDR:
1899 cmd = SIOCGIFDSTADDR;
1900 break;
1901
1902 case OSIOCGIFBRDADDR:
1903 cmd = SIOCGIFBRDADDR;
1904 break;
1905
1906 case OSIOCGIFNETMASK:
1907 cmd = SIOCGIFNETMASK;
1908 }
1909 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
1910 cmd,
1911 data,
1912 ifp, td));
1913 switch (ocmd) {
1914
1915 case OSIOCGIFADDR:
1916 case OSIOCGIFDSTADDR:
1917 case OSIOCGIFBRDADDR:
1918 case OSIOCGIFNETMASK:
1919 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1920
1921 }
1922 }
1923#endif /* COMPAT_43 */
1924
1925 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1926#ifdef INET6
1927 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1928 if (ifp->if_flags & IFF_UP) {
1929 int s = splimp();
1930 in6_if_up(ifp);
1931 splx(s);
1932 }
1933#endif
1934 }
1935 return (error);
1936}
1937
1938/*
1939 * The code common to handling reference counted flags,
1940 * e.g., in ifpromisc() and if_allmulti().
1941 * The "pflag" argument can specify a permanent mode flag to check,
1942 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
1943 *
1944 * Only to be used on stack-owned flags, not driver-owned flags.
1945 */
1946static int
1947if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
1948{
1949 struct ifreq ifr;
1950 int error;
1951 int oldflags, oldcount;
1952
1953 /* Sanity checks to catch programming errors */
1954 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
1955 ("%s: setting driver-owned flag %d", __func__, flag));
1956
1957 if (onswitch)
1958 KASSERT(*refcount >= 0,
1959 ("%s: increment negative refcount %d for flag %d",
1960 __func__, *refcount, flag));
1961 else
1962 KASSERT(*refcount > 0,
1963 ("%s: decrement non-positive refcount %d for flag %d",
1964 __func__, *refcount, flag));
1965
1966 /* In case this mode is permanent, just touch refcount */
1967 if (ifp->if_flags & pflag) {
1968 *refcount += onswitch ? 1 : -1;
1969 return (0);
1970 }
1971
1972 /* Save ifnet parameters for if_ioctl() may fail */
1973 oldcount = *refcount;
1974 oldflags = ifp->if_flags;
1975
1976 /*
1977 * See if we aren't the only and touching refcount is enough.
1978 * Actually toggle interface flag if we are the first or last.
1979 */
1980 if (onswitch) {
1981 if ((*refcount)++)
1982 return (0);
1983 ifp->if_flags |= flag;
1984 } else {
1985 if (--(*refcount))
1986 return (0);
1987 ifp->if_flags &= ~flag;
1988 }
1989
1990 /* Call down the driver since we've changed interface flags */
1991 if (ifp->if_ioctl == NULL) {
1992 error = EOPNOTSUPP;
1993 goto recover;
1994 }
1995 ifr.ifr_flags = ifp->if_flags & 0xffff;
1996 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1997 IFF_LOCKGIANT(ifp);
1998 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1999 IFF_UNLOCKGIANT(ifp);
2000 if (error)
2001 goto recover;
2002 /* Notify userland that interface flags have changed */
2003 rt_ifmsg(ifp);
2004 return (0);
2005
2006recover:
2007 /* Recover after driver error */
2008 *refcount = oldcount;
2009 ifp->if_flags = oldflags;
2010 return (error);
2011}
2012
2013/*
2014 * Set/clear promiscuous mode on interface ifp based on the truth value
2015 * of pswitch. The calls are reference counted so that only the first
2016 * "on" request actually has an effect, as does the final "off" request.
2017 * Results are undefined if the "off" and "on" requests are not matched.
2018 */
2019int
2020ifpromisc(struct ifnet *ifp, int pswitch)
2021{
2022 int error;
2023 int oldflags = ifp->if_flags;
2024
2025 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2026 &ifp->if_pcount, pswitch);
2027 /* If promiscuous mode status has changed, log a message */
2028 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2029 log(LOG_INFO, "%s: promiscuous mode %s\n",
2030 ifp->if_xname,
2031 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2032 return (error);
2033}
2034
2035/*
2036 * Return interface configuration
2037 * of system. List may be used
2038 * in later ioctl's (above) to get
2039 * other information.
2040 */
2041/*ARGSUSED*/
2042static int
2043ifconf(u_long cmd, caddr_t data)
2044{
2045 struct ifconf *ifc = (struct ifconf *)data;
2046#ifdef __amd64__
2047 struct ifconf32 *ifc32 = (struct ifconf32 *)data;
2048 struct ifconf ifc_swab;
2049#endif
2050 struct ifnet *ifp;
2051 struct ifaddr *ifa;
2052 struct ifreq ifr;
2053 struct sbuf *sb;
2054 int error, full = 0, valid_len, max_len;
2055
2056#ifdef __amd64__
2057 if (cmd == SIOCGIFCONF32) {
2058 ifc_swab.ifc_len = ifc32->ifc_len;
2059 ifc_swab.ifc_buf = (caddr_t)(uintptr_t)ifc32->ifc_buf;
2060 ifc = &ifc_swab;
2061 }
2062#endif
2063 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2064 max_len = MAXPHYS - 1;
2065
2066 /* Prevent hostile input from being able to crash the system */
2067 if (ifc->ifc_len <= 0)
2068 return (EINVAL);
2069
2070again:
2071 if (ifc->ifc_len <= max_len) {
2072 max_len = ifc->ifc_len;
2073 full = 1;
2074 }
2075 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2076 max_len = 0;
2077 valid_len = 0;
2078
2079 IFNET_RLOCK(); /* could sleep XXX */
2080 TAILQ_FOREACH(ifp, &ifnet, if_link) {
2081 int addrs;
2082
2083 /*
2084 * Zero the ifr_name buffer to make sure we don't
2085 * disclose the contents of the stack.
2086 */
2087 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2088
2089 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2090 >= sizeof(ifr.ifr_name)) {
2091 sbuf_delete(sb);
2092 IFNET_RUNLOCK();
2093 return (ENAMETOOLONG);
2094 }
2095
2096 addrs = 0;
2097 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2098 struct sockaddr *sa = ifa->ifa_addr;
2099
2100 if (jailed(curthread->td_ucred) &&
2101 prison_if(curthread->td_ucred, sa))
2102 continue;
2103 addrs++;
2104#ifdef COMPAT_43
2105 if (cmd == OSIOCGIFCONF) {
2106 struct osockaddr *osa =
2107 (struct osockaddr *)&ifr.ifr_addr;
2108 ifr.ifr_addr = *sa;
2109 osa->sa_family = sa->sa_family;
2110 sbuf_bcat(sb, &ifr, sizeof(ifr));
2111 max_len += sizeof(ifr);
2112 } else
2113#endif
2114 if (sa->sa_len <= sizeof(*sa)) {
2115 ifr.ifr_addr = *sa;
2116 sbuf_bcat(sb, &ifr, sizeof(ifr));
2117 max_len += sizeof(ifr);
2118 } else {
2119 sbuf_bcat(sb, &ifr,
2120 offsetof(struct ifreq, ifr_addr));
2121 max_len += offsetof(struct ifreq, ifr_addr);
2122 sbuf_bcat(sb, sa, sa->sa_len);
2123 max_len += sa->sa_len;
2124 }
2125
2126 if (!sbuf_overflowed(sb))
2127 valid_len = sbuf_len(sb);
2128 }
2129 if (addrs == 0) {
2130 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2131 sbuf_bcat(sb, &ifr, sizeof(ifr));
2132 max_len += sizeof(ifr);
2133
2134 if (!sbuf_overflowed(sb))
2135 valid_len = sbuf_len(sb);
2136 }
2137 }
2138 IFNET_RUNLOCK();
2139
2140 /*
2141 * If we didn't allocate enough space (uncommon), try again. If
2142 * we have already allocated as much space as we are allowed,
2143 * return what we've got.
2144 */
2145 if (valid_len != max_len && !full) {
2146 sbuf_delete(sb);
2147 goto again;
2148 }
2149
2150 ifc->ifc_len = valid_len;
2151#ifdef __amd64__
2152 if (cmd == SIOCGIFCONF32)
2153 ifc32->ifc_len = valid_len;
2154#endif
2155 sbuf_finish(sb);
2156 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2157 sbuf_delete(sb);
2158 return (error);
2159}
2160
2161/*
2162 * Just like ifpromisc(), but for all-multicast-reception mode.
2163 */
2164int
2165if_allmulti(struct ifnet *ifp, int onswitch)
2166{
2167
2168 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2169}
2170
2171struct ifmultiaddr *
2172if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
2173{
2174 struct ifmultiaddr *ifma;
2175
2176 IF_ADDR_LOCK_ASSERT(ifp);
2177
2178 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2179 if (sa->sa_family == AF_LINK) {
2180 if (sa_dl_equal(ifma->ifma_addr, sa))
2181 break;
2182 } else {
2183 if (sa_equal(ifma->ifma_addr, sa))
2184 break;
2185 }
2186 }
2187
2188 return ifma;
2189}
2190
2191/*
2192 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
2193 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
2194 * the ifnet multicast address list here, so the caller must do that and
2195 * other setup work (such as notifying the device driver). The reference
2196 * count is initialized to 1.
2197 */
2198static struct ifmultiaddr *
2199if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
2200 int mflags)
2201{
2202 struct ifmultiaddr *ifma;
2203 struct sockaddr *dupsa;
2204
2205 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, mflags |
2206 M_ZERO);
2207 if (ifma == NULL)
2208 return (NULL);
2209
2210 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, mflags);
2211 if (dupsa == NULL) {
2212 FREE(ifma, M_IFMADDR);
2213 return (NULL);
2214 }
2215 bcopy(sa, dupsa, sa->sa_len);
2216 ifma->ifma_addr = dupsa;
2217
2218 ifma->ifma_ifp = ifp;
2219 ifma->ifma_refcount = 1;
2220 ifma->ifma_protospec = NULL;
2221
2222 if (llsa == NULL) {
2223 ifma->ifma_lladdr = NULL;
2224 return (ifma);
2225 }
2226
2227 MALLOC(dupsa, struct sockaddr *, llsa->sa_len, M_IFMADDR, mflags);
2228 if (dupsa == NULL) {
2229 FREE(ifma->ifma_addr, M_IFMADDR);
2230 FREE(ifma, M_IFMADDR);
2231 return (NULL);
2232 }
2233 bcopy(llsa, dupsa, llsa->sa_len);
2234 ifma->ifma_lladdr = dupsa;
2235
2236 return (ifma);
2237}
2238
2239/*
2240 * if_freemulti: free ifmultiaddr structure and possibly attached related
2241 * addresses. The caller is responsible for implementing reference
2242 * counting, notifying the driver, handling routing messages, and releasing
2243 * any dependent link layer state.
2244 */
2245static void
2246if_freemulti(struct ifmultiaddr *ifma)
2247{
2248
2249 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
2250 ifma->ifma_refcount));
2251 KASSERT(ifma->ifma_protospec == NULL,
2252 ("if_freemulti: protospec not NULL"));
2253
2254 if (ifma->ifma_lladdr != NULL)
2255 FREE(ifma->ifma_lladdr, M_IFMADDR);
2256 FREE(ifma->ifma_addr, M_IFMADDR);
2257 FREE(ifma, M_IFMADDR);
2258}
2259
2260/*
2261 * Register an additional multicast address with a network interface.
2262 *
2263 * - If the address is already present, bump the reference count on the
2264 * address and return.
2265 * - If the address is not link-layer, look up a link layer address.
2266 * - Allocate address structures for one or both addresses, and attach to the
2267 * multicast address list on the interface. If automatically adding a link
2268 * layer address, the protocol address will own a reference to the link
2269 * layer address, to be freed when it is freed.
2270 * - Notify the network device driver of an addition to the multicast address
2271 * list.
2272 *
2273 * 'sa' points to caller-owned memory with the desired multicast address.
2274 *
2275 * 'retifma' will be used to return a pointer to the resulting multicast
2276 * address reference, if desired.
2277 */
2278int
2279if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2280 struct ifmultiaddr **retifma)
2281{
2282 struct ifmultiaddr *ifma, *ll_ifma;
2283 struct sockaddr *llsa;
2284 int error;
2285
2286 /*
2287 * If the address is already present, return a new reference to it;
2288 * otherwise, allocate storage and set up a new address.
2289 */
2290 IF_ADDR_LOCK(ifp);
2291 ifma = if_findmulti(ifp, sa);
2292 if (ifma != NULL) {
2293 ifma->ifma_refcount++;
2294 if (retifma != NULL)
2295 *retifma = ifma;
2296 IF_ADDR_UNLOCK(ifp);
2297 return (0);
2298 }
2299
2300 /*
2301 * The address isn't already present; resolve the protocol address
2302 * into a link layer address, and then look that up, bump its
2303 * refcount or allocate an ifma for that also. If 'llsa' was
2304 * returned, we will need to free it later.
2305 */
2306 llsa = NULL;
2307 ll_ifma = NULL;
2308 if (ifp->if_resolvemulti != NULL) {
2309 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2310 if (error)
2311 goto unlock_out;
2312 }
2313
2314 /*
2315 * Allocate the new address. Don't hook it up yet, as we may also
2316 * need to allocate a link layer multicast address.
2317 */
2318 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
2319 if (ifma == NULL) {
2320 error = ENOMEM;
2321 goto free_llsa_out;
2322 }
2323
2324 /*
2325 * If a link layer address is found, we'll need to see if it's
2326 * already present in the address list, or allocate is as well.
2327 * When this block finishes, the link layer address will be on the
2328 * list.
2329 */
2330 if (llsa != NULL) {
2331 ll_ifma = if_findmulti(ifp, llsa);
2332 if (ll_ifma == NULL) {
2333 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
2334 if (ll_ifma == NULL) {
2335 --ifma->ifma_refcount;
2336 if_freemulti(ifma);
2337 error = ENOMEM;
2338 goto free_llsa_out;
2339 }
2340 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
2341 ifma_link);
2342 } else
2343 ll_ifma->ifma_refcount++;
2344 ifma->ifma_llifma = ll_ifma;
2345 }
2346
2347 /*
2348 * We now have a new multicast address, ifma, and possibly a new or
2349 * referenced link layer address. Add the primary address to the
2350 * ifnet address list.
2351 */
2352 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2353
2354 if (retifma != NULL)
2355 *retifma = ifma;
2356
2357 /*
2358 * Must generate the message while holding the lock so that 'ifma'
2359 * pointer is still valid.
2360 */
2361 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2362 IF_ADDR_UNLOCK(ifp);
2363
2364 /*
2365 * We are certain we have added something, so call down to the
2366 * interface to let them know about it.
2367 */
2368 if (ifp->if_ioctl != NULL) {
2369 IFF_LOCKGIANT(ifp);
2370 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
2371 IFF_UNLOCKGIANT(ifp);
2372 }
2373
2374 if (llsa != NULL)
2375 FREE(llsa, M_IFMADDR);
2376
2377 return (0);
2378
2379free_llsa_out:
2380 if (llsa != NULL)
2381 FREE(llsa, M_IFMADDR);
2382
2383unlock_out:
2384 IF_ADDR_UNLOCK(ifp);
2385 return (error);
2386}
2387
2388/*
2389 * Delete a multicast group membership by network-layer group address.
2390 *
2391 * Returns ENOENT if the entry could not be found. If ifp no longer
2392 * exists, results are undefined. This entry point should only be used
2393 * from subsystems which do appropriate locking to hold ifp for the
2394 * duration of the call.
2395 * Network-layer protocol domains must use if_delmulti_ifma().
2396 */
2397int
2398if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2399{
2400 struct ifmultiaddr *ifma;
2401 int lastref;
2402#ifdef INVARIANTS
2403 struct ifnet *oifp;
2404
2405 IFNET_RLOCK();
2406 TAILQ_FOREACH(oifp, &ifnet, if_link)
2407 if (ifp == oifp)
2408 break;
2409 if (ifp != oifp)
2410 ifp = NULL;
2411 IFNET_RUNLOCK();
2412
2413 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
2414#endif
2415 if (ifp == NULL)
2416 return (ENOENT);
2417
2418 IF_ADDR_LOCK(ifp);
2419 lastref = 0;
2420 ifma = if_findmulti(ifp, sa);
2421 if (ifma != NULL)
2422 lastref = if_delmulti_locked(ifp, ifma, 0);
2423 IF_ADDR_UNLOCK(ifp);
2424
2425 if (ifma == NULL)
2426 return (ENOENT);
2427
2428 if (lastref && ifp->if_ioctl != NULL) {
2429 IFF_LOCKGIANT(ifp);
2430 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
2431 IFF_UNLOCKGIANT(ifp);
2432 }
2433
2434 return (0);
2435}
2436
2437/*
2438 * Delete a multicast group membership by group membership pointer.
2439 * Network-layer protocol domains must use this routine.
2440 *
2441 * It is safe to call this routine if the ifp disappeared. Callers should
2442 * hold IFF_LOCKGIANT() to avoid a LOR in case the hardware needs to be
2443 * reconfigured.
2444 */
2445void
2446if_delmulti_ifma(struct ifmultiaddr *ifma)
2447{
2448 struct ifnet *ifp;
2449 int lastref;
2450
2451 ifp = ifma->ifma_ifp;
2452#ifdef DIAGNOSTIC
2453 if (ifp == NULL) {
2454 printf("%s: ifma_ifp seems to be detached\n", __func__);
2455 } else {
2456 struct ifnet *oifp;
2457
2458 IFNET_RLOCK();
2459 TAILQ_FOREACH(oifp, &ifnet, if_link)
2460 if (ifp == oifp)
2461 break;
2462 if (ifp != oifp) {
2463 printf("%s: ifnet %p disappeared\n", __func__, ifp);
2464 ifp = NULL;
2465 }
2466 IFNET_RUNLOCK();
2467 }
2468#endif
2469 /*
2470 * If and only if the ifnet instance exists: Acquire the address lock.
2471 */
2472 if (ifp != NULL)
2473 IF_ADDR_LOCK(ifp);
2474
2475 lastref = if_delmulti_locked(ifp, ifma, 0);
2476
2477 if (ifp != NULL) {
2478 /*
2479 * If and only if the ifnet instance exists:
2480 * Release the address lock.
2481 * If the group was left: update the hardware hash filter.
2482 */
2483 IF_ADDR_UNLOCK(ifp);
2484 if (lastref && ifp->if_ioctl != NULL) {
2485 IFF_LOCKGIANT(ifp);
2486 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
2487 IFF_UNLOCKGIANT(ifp);
2488 }
2489 }
2490}
2491
2492/*
2493 * Perform deletion of network-layer and/or link-layer multicast address.
2494 *
2495 * Return 0 if the reference count was decremented.
2496 * Return 1 if the final reference was released, indicating that the
2497 * hardware hash filter should be reprogrammed.
2498 */
2499static int
2500if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
2501{
2502 struct ifmultiaddr *ll_ifma;
2503
2504 if (ifp != NULL && ifma->ifma_ifp != NULL) {
2505 KASSERT(ifma->ifma_ifp == ifp,
2506 ("%s: inconsistent ifp %p", __func__, ifp));
2507 IF_ADDR_LOCK_ASSERT(ifp);
2508 }
2509
2510 ifp = ifma->ifma_ifp;
2511
2512 /*
2513 * If the ifnet is detaching, null out references to ifnet,
2514 * so that upper protocol layers will notice, and not attempt
2515 * to obtain locks for an ifnet which no longer exists. The
2516 * routing socket announcement must happen before the ifnet
2517 * instance is detached from the system.
2518 */
2519 if (detaching) {
2520#ifdef DIAGNOSTIC
2521 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
2522#endif
2523 /*
2524 * ifp may already be nulled out if we are being reentered
2525 * to delete the ll_ifma.
2526 */
2527 if (ifp != NULL) {
2528 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2529 ifma->ifma_ifp = NULL;
2530 }
2531 }
2532
2533 if (--ifma->ifma_refcount > 0)
2534 return 0;
2535
2536 /*
2537 * If this ifma is a network-layer ifma, a link-layer ifma may
2538 * have been associated with it. Release it first if so.
2539 */
2540 ll_ifma = ifma->ifma_llifma;
2541 if (ll_ifma != NULL) {
2542 KASSERT(ifma->ifma_lladdr != NULL,
2543 ("%s: llifma w/o lladdr", __func__));
2544 if (detaching)
2545 ll_ifma->ifma_ifp = NULL; /* XXX */
2546 if (--ll_ifma->ifma_refcount == 0) {
2547 if (ifp != NULL) {
2548 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
2549 ifma_link);
2550 }
2551 if_freemulti(ll_ifma);
2552 }
2553 }
2554
2555 if (ifp != NULL)
2556 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2557
2558 if_freemulti(ifma);
2559
2560 /*
2561 * The last reference to this instance of struct ifmultiaddr
2562 * was released; the hardware should be notified of this change.
2563 */
2564 return 1;
2565}
2566
2567/*
2568 * Set the link layer address on an interface.
2569 *
2570 * At this time we only support certain types of interfaces,
2571 * and we don't allow the length of the address to change.
2572 */
2573int
2574if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2575{
2576 struct sockaddr_dl *sdl;
2577 struct ifaddr *ifa;
2578 struct ifreq ifr;
2579
2580 ifa = ifp->if_addr;
2581 if (ifa == NULL)
2582 return (EINVAL);
2583 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2584 if (sdl == NULL)
2585 return (EINVAL);
2586 if (len != sdl->sdl_alen) /* don't allow length to change */
2587 return (EINVAL);
2588 switch (ifp->if_type) {
2589 case IFT_ETHER:
2590 case IFT_FDDI:
2591 case IFT_XETHER:
2592 case IFT_ISO88025:
2593 case IFT_L2VLAN:
2594 case IFT_BRIDGE:
2595 case IFT_ARCNET:
1386
1387 devctl_notify("IFNET", ifp->if_xname,
1388 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1389 if (pending > 1)
1390 if_printf(ifp, "%d link states coalesced\n", pending);
1391 if (log_link_state_change)
1392 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
1393 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
1394}
1395
1396/*
1397 * Mark an interface down and notify protocols of
1398 * the transition.
1399 * NOTE: must be called at splnet or eqivalent.
1400 */
1401void
1402if_down(struct ifnet *ifp)
1403{
1404
1405 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1406}
1407
1408/*
1409 * Mark an interface up and notify protocols of
1410 * the transition.
1411 * NOTE: must be called at splnet or eqivalent.
1412 */
1413void
1414if_up(struct ifnet *ifp)
1415{
1416
1417 if_route(ifp, IFF_UP, AF_UNSPEC);
1418}
1419
1420/*
1421 * Flush an interface queue.
1422 */
1423static void
1424if_qflush(struct ifaltq *ifq)
1425{
1426 struct mbuf *m, *n;
1427
1428 IFQ_LOCK(ifq);
1429#ifdef ALTQ
1430 if (ALTQ_IS_ENABLED(ifq))
1431 ALTQ_PURGE(ifq);
1432#endif
1433 n = ifq->ifq_head;
1434 while ((m = n) != 0) {
1435 n = m->m_act;
1436 m_freem(m);
1437 }
1438 ifq->ifq_head = 0;
1439 ifq->ifq_tail = 0;
1440 ifq->ifq_len = 0;
1441 IFQ_UNLOCK(ifq);
1442}
1443
1444/*
1445 * Handle interface watchdog timer routines. Called
1446 * from softclock, we decrement timers (if set) and
1447 * call the appropriate interface routine on expiration.
1448 *
1449 * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called
1450 * holding Giant. If we switch to an MPSAFE callout, we likely need to grab
1451 * Giant before entering if_watchdog() on an IFF_NEEDSGIANT interface.
1452 */
1453static void
1454if_slowtimo(void *arg)
1455{
1456 struct ifnet *ifp;
1457 int s = splimp();
1458
1459 IFNET_RLOCK();
1460 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1461 if (ifp->if_timer == 0 || --ifp->if_timer)
1462 continue;
1463 if (ifp->if_watchdog)
1464 (*ifp->if_watchdog)(ifp);
1465 }
1466 IFNET_RUNLOCK();
1467 splx(s);
1468 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
1469}
1470
1471/*
1472 * Map interface name to
1473 * interface structure pointer.
1474 */
1475struct ifnet *
1476ifunit(const char *name)
1477{
1478 struct ifnet *ifp;
1479
1480 IFNET_RLOCK();
1481 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1482 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
1483 break;
1484 }
1485 IFNET_RUNLOCK();
1486 return (ifp);
1487}
1488
1489/*
1490 * Hardware specific interface ioctls.
1491 */
1492static int
1493ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
1494{
1495 struct ifreq *ifr;
1496 struct ifstat *ifs;
1497 int error = 0;
1498 int new_flags, temp_flags;
1499 size_t namelen, onamelen;
1500 char new_name[IFNAMSIZ];
1501 struct ifaddr *ifa;
1502 struct sockaddr_dl *sdl;
1503
1504 ifr = (struct ifreq *)data;
1505 switch (cmd) {
1506 case SIOCGIFINDEX:
1507 ifr->ifr_index = ifp->if_index;
1508 break;
1509
1510 case SIOCGIFFLAGS:
1511 temp_flags = ifp->if_flags | ifp->if_drv_flags;
1512 ifr->ifr_flags = temp_flags & 0xffff;
1513 ifr->ifr_flagshigh = temp_flags >> 16;
1514 break;
1515
1516 case SIOCGIFCAP:
1517 ifr->ifr_reqcap = ifp->if_capabilities;
1518 ifr->ifr_curcap = ifp->if_capenable;
1519 break;
1520
1521#ifdef MAC
1522 case SIOCGIFMAC:
1523 error = mac_ioctl_ifnet_get(td->td_ucred, ifr, ifp);
1524 break;
1525#endif
1526
1527 case SIOCGIFMETRIC:
1528 ifr->ifr_metric = ifp->if_metric;
1529 break;
1530
1531 case SIOCGIFMTU:
1532 ifr->ifr_mtu = ifp->if_mtu;
1533 break;
1534
1535 case SIOCGIFPHYS:
1536 ifr->ifr_phys = ifp->if_physical;
1537 break;
1538
1539 case SIOCSIFFLAGS:
1540 error = priv_check(td, PRIV_NET_SETIFFLAGS);
1541 if (error)
1542 return (error);
1543 /*
1544 * Currently, no driver owned flags pass the IFF_CANTCHANGE
1545 * check, so we don't need special handling here yet.
1546 */
1547 new_flags = (ifr->ifr_flags & 0xffff) |
1548 (ifr->ifr_flagshigh << 16);
1549 if (ifp->if_flags & IFF_SMART) {
1550 /* Smart drivers twiddle their own routes */
1551 } else if (ifp->if_flags & IFF_UP &&
1552 (new_flags & IFF_UP) == 0) {
1553 int s = splimp();
1554 if_down(ifp);
1555 splx(s);
1556 } else if (new_flags & IFF_UP &&
1557 (ifp->if_flags & IFF_UP) == 0) {
1558 int s = splimp();
1559 if_up(ifp);
1560 splx(s);
1561 }
1562 /* See if permanently promiscuous mode bit is about to flip */
1563 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
1564 if (new_flags & IFF_PPROMISC)
1565 ifp->if_flags |= IFF_PROMISC;
1566 else if (ifp->if_pcount == 0)
1567 ifp->if_flags &= ~IFF_PROMISC;
1568 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
1569 ifp->if_xname,
1570 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
1571 }
1572 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1573 (new_flags &~ IFF_CANTCHANGE);
1574 if (ifp->if_ioctl) {
1575 IFF_LOCKGIANT(ifp);
1576 (void) (*ifp->if_ioctl)(ifp, cmd, data);
1577 IFF_UNLOCKGIANT(ifp);
1578 }
1579 getmicrotime(&ifp->if_lastchange);
1580 break;
1581
1582 case SIOCSIFCAP:
1583 error = priv_check(td, PRIV_NET_SETIFCAP);
1584 if (error)
1585 return (error);
1586 if (ifp->if_ioctl == NULL)
1587 return (EOPNOTSUPP);
1588 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1589 return (EINVAL);
1590 IFF_LOCKGIANT(ifp);
1591 error = (*ifp->if_ioctl)(ifp, cmd, data);
1592 IFF_UNLOCKGIANT(ifp);
1593 if (error == 0)
1594 getmicrotime(&ifp->if_lastchange);
1595 break;
1596
1597#ifdef MAC
1598 case SIOCSIFMAC:
1599 error = mac_ioctl_ifnet_set(td->td_ucred, ifr, ifp);
1600 break;
1601#endif
1602
1603 case SIOCSIFNAME:
1604 error = priv_check(td, PRIV_NET_SETIFNAME);
1605 if (error)
1606 return (error);
1607 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1608 if (error != 0)
1609 return (error);
1610 if (new_name[0] == '\0')
1611 return (EINVAL);
1612 if (ifunit(new_name) != NULL)
1613 return (EEXIST);
1614
1615 /* Announce the departure of the interface. */
1616 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1617 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1618
1619 log(LOG_INFO, "%s: changing name to '%s'\n",
1620 ifp->if_xname, new_name);
1621
1622 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1623 ifa = ifp->if_addr;
1624 IFA_LOCK(ifa);
1625 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1626 namelen = strlen(new_name);
1627 onamelen = sdl->sdl_nlen;
1628 /*
1629 * Move the address if needed. This is safe because we
1630 * allocate space for a name of length IFNAMSIZ when we
1631 * create this in if_attach().
1632 */
1633 if (namelen != onamelen) {
1634 bcopy(sdl->sdl_data + onamelen,
1635 sdl->sdl_data + namelen, sdl->sdl_alen);
1636 }
1637 bcopy(new_name, sdl->sdl_data, namelen);
1638 sdl->sdl_nlen = namelen;
1639 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1640 bzero(sdl->sdl_data, onamelen);
1641 while (namelen != 0)
1642 sdl->sdl_data[--namelen] = 0xff;
1643 IFA_UNLOCK(ifa);
1644
1645 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
1646 /* Announce the return of the interface. */
1647 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1648 break;
1649
1650 case SIOCSIFMETRIC:
1651 error = priv_check(td, PRIV_NET_SETIFMETRIC);
1652 if (error)
1653 return (error);
1654 ifp->if_metric = ifr->ifr_metric;
1655 getmicrotime(&ifp->if_lastchange);
1656 break;
1657
1658 case SIOCSIFPHYS:
1659 error = priv_check(td, PRIV_NET_SETIFPHYS);
1660 if (error)
1661 return (error);
1662 if (ifp->if_ioctl == NULL)
1663 return (EOPNOTSUPP);
1664 IFF_LOCKGIANT(ifp);
1665 error = (*ifp->if_ioctl)(ifp, cmd, data);
1666 IFF_UNLOCKGIANT(ifp);
1667 if (error == 0)
1668 getmicrotime(&ifp->if_lastchange);
1669 break;
1670
1671 case SIOCSIFMTU:
1672 {
1673 u_long oldmtu = ifp->if_mtu;
1674
1675 error = priv_check(td, PRIV_NET_SETIFMTU);
1676 if (error)
1677 return (error);
1678 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1679 return (EINVAL);
1680 if (ifp->if_ioctl == NULL)
1681 return (EOPNOTSUPP);
1682 IFF_LOCKGIANT(ifp);
1683 error = (*ifp->if_ioctl)(ifp, cmd, data);
1684 IFF_UNLOCKGIANT(ifp);
1685 if (error == 0) {
1686 getmicrotime(&ifp->if_lastchange);
1687 rt_ifmsg(ifp);
1688 }
1689 /*
1690 * If the link MTU changed, do network layer specific procedure.
1691 */
1692 if (ifp->if_mtu != oldmtu) {
1693#ifdef INET6
1694 nd6_setmtu(ifp);
1695#endif
1696 }
1697 break;
1698 }
1699
1700 case SIOCADDMULTI:
1701 case SIOCDELMULTI:
1702 if (cmd == SIOCADDMULTI)
1703 error = priv_check(td, PRIV_NET_ADDMULTI);
1704 else
1705 error = priv_check(td, PRIV_NET_DELMULTI);
1706 if (error)
1707 return (error);
1708
1709 /* Don't allow group membership on non-multicast interfaces. */
1710 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1711 return (EOPNOTSUPP);
1712
1713 /* Don't let users screw up protocols' entries. */
1714 if (ifr->ifr_addr.sa_family != AF_LINK)
1715 return (EINVAL);
1716
1717 if (cmd == SIOCADDMULTI) {
1718 struct ifmultiaddr *ifma;
1719
1720 /*
1721 * Userland is only permitted to join groups once
1722 * via the if_addmulti() KPI, because it cannot hold
1723 * struct ifmultiaddr * between calls. It may also
1724 * lose a race while we check if the membership
1725 * already exists.
1726 */
1727 IF_ADDR_LOCK(ifp);
1728 ifma = if_findmulti(ifp, &ifr->ifr_addr);
1729 IF_ADDR_UNLOCK(ifp);
1730 if (ifma != NULL)
1731 error = EADDRINUSE;
1732 else
1733 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1734 } else {
1735 error = if_delmulti(ifp, &ifr->ifr_addr);
1736 }
1737 if (error == 0)
1738 getmicrotime(&ifp->if_lastchange);
1739 break;
1740
1741 case SIOCSIFPHYADDR:
1742 case SIOCDIFPHYADDR:
1743#ifdef INET6
1744 case SIOCSIFPHYADDR_IN6:
1745#endif
1746 case SIOCSLIFPHYADDR:
1747 case SIOCSIFMEDIA:
1748 case SIOCSIFGENERIC:
1749 error = priv_check(td, PRIV_NET_HWIOCTL);
1750 if (error)
1751 return (error);
1752 if (ifp->if_ioctl == NULL)
1753 return (EOPNOTSUPP);
1754 IFF_LOCKGIANT(ifp);
1755 error = (*ifp->if_ioctl)(ifp, cmd, data);
1756 IFF_UNLOCKGIANT(ifp);
1757 if (error == 0)
1758 getmicrotime(&ifp->if_lastchange);
1759 break;
1760
1761 case SIOCGIFSTATUS:
1762 ifs = (struct ifstat *)data;
1763 ifs->ascii[0] = '\0';
1764
1765 case SIOCGIFPSRCADDR:
1766 case SIOCGIFPDSTADDR:
1767 case SIOCGLIFPHYADDR:
1768 case SIOCGIFMEDIA:
1769 case SIOCGIFGENERIC:
1770 if (ifp->if_ioctl == NULL)
1771 return (EOPNOTSUPP);
1772 IFF_LOCKGIANT(ifp);
1773 error = (*ifp->if_ioctl)(ifp, cmd, data);
1774 IFF_UNLOCKGIANT(ifp);
1775 break;
1776
1777 case SIOCSIFLLADDR:
1778 error = priv_check(td, PRIV_NET_SETLLADDR);
1779 if (error)
1780 return (error);
1781 error = if_setlladdr(ifp,
1782 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1783 break;
1784
1785 case SIOCAIFGROUP:
1786 {
1787 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
1788
1789 error = priv_check(td, PRIV_NET_ADDIFGROUP);
1790 if (error)
1791 return (error);
1792 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
1793 return (error);
1794 break;
1795 }
1796
1797 case SIOCGIFGROUP:
1798 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
1799 return (error);
1800 break;
1801
1802 case SIOCDIFGROUP:
1803 {
1804 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
1805
1806 error = priv_check(td, PRIV_NET_DELIFGROUP);
1807 if (error)
1808 return (error);
1809 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
1810 return (error);
1811 break;
1812 }
1813
1814 default:
1815 error = ENOIOCTL;
1816 break;
1817 }
1818 return (error);
1819}
1820
1821/*
1822 * Interface ioctls.
1823 */
1824int
1825ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
1826{
1827 struct ifnet *ifp;
1828 struct ifreq *ifr;
1829 int error;
1830 int oif_flags;
1831
1832 switch (cmd) {
1833 case SIOCGIFCONF:
1834 case OSIOCGIFCONF:
1835#ifdef __amd64__
1836 case SIOCGIFCONF32:
1837#endif
1838 return (ifconf(cmd, data));
1839 }
1840 ifr = (struct ifreq *)data;
1841
1842 switch (cmd) {
1843 case SIOCIFCREATE:
1844 case SIOCIFCREATE2:
1845 error = priv_check(td, PRIV_NET_IFCREATE);
1846 if (error)
1847 return (error);
1848 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
1849 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
1850 case SIOCIFDESTROY:
1851 error = priv_check(td, PRIV_NET_IFDESTROY);
1852 if (error)
1853 return (error);
1854 return if_clone_destroy(ifr->ifr_name);
1855
1856 case SIOCIFGCLONERS:
1857 return (if_clone_list((struct if_clonereq *)data));
1858 case SIOCGIFGMEMB:
1859 return (if_getgroupmembers((struct ifgroupreq *)data));
1860 }
1861
1862 ifp = ifunit(ifr->ifr_name);
1863 if (ifp == 0)
1864 return (ENXIO);
1865
1866 error = ifhwioctl(cmd, ifp, data, td);
1867 if (error != ENOIOCTL)
1868 return (error);
1869
1870 oif_flags = ifp->if_flags;
1871 if (so->so_proto == 0)
1872 return (EOPNOTSUPP);
1873#ifndef COMPAT_43
1874 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
1875 data,
1876 ifp, td));
1877#else
1878 {
1879 int ocmd = cmd;
1880
1881 switch (cmd) {
1882
1883 case SIOCSIFDSTADDR:
1884 case SIOCSIFADDR:
1885 case SIOCSIFBRDADDR:
1886 case SIOCSIFNETMASK:
1887#if BYTE_ORDER != BIG_ENDIAN
1888 if (ifr->ifr_addr.sa_family == 0 &&
1889 ifr->ifr_addr.sa_len < 16) {
1890 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1891 ifr->ifr_addr.sa_len = 16;
1892 }
1893#else
1894 if (ifr->ifr_addr.sa_len == 0)
1895 ifr->ifr_addr.sa_len = 16;
1896#endif
1897 break;
1898
1899 case OSIOCGIFADDR:
1900 cmd = SIOCGIFADDR;
1901 break;
1902
1903 case OSIOCGIFDSTADDR:
1904 cmd = SIOCGIFDSTADDR;
1905 break;
1906
1907 case OSIOCGIFBRDADDR:
1908 cmd = SIOCGIFBRDADDR;
1909 break;
1910
1911 case OSIOCGIFNETMASK:
1912 cmd = SIOCGIFNETMASK;
1913 }
1914 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
1915 cmd,
1916 data,
1917 ifp, td));
1918 switch (ocmd) {
1919
1920 case OSIOCGIFADDR:
1921 case OSIOCGIFDSTADDR:
1922 case OSIOCGIFBRDADDR:
1923 case OSIOCGIFNETMASK:
1924 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1925
1926 }
1927 }
1928#endif /* COMPAT_43 */
1929
1930 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1931#ifdef INET6
1932 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1933 if (ifp->if_flags & IFF_UP) {
1934 int s = splimp();
1935 in6_if_up(ifp);
1936 splx(s);
1937 }
1938#endif
1939 }
1940 return (error);
1941}
1942
1943/*
1944 * The code common to handling reference counted flags,
1945 * e.g., in ifpromisc() and if_allmulti().
1946 * The "pflag" argument can specify a permanent mode flag to check,
1947 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
1948 *
1949 * Only to be used on stack-owned flags, not driver-owned flags.
1950 */
1951static int
1952if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
1953{
1954 struct ifreq ifr;
1955 int error;
1956 int oldflags, oldcount;
1957
1958 /* Sanity checks to catch programming errors */
1959 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
1960 ("%s: setting driver-owned flag %d", __func__, flag));
1961
1962 if (onswitch)
1963 KASSERT(*refcount >= 0,
1964 ("%s: increment negative refcount %d for flag %d",
1965 __func__, *refcount, flag));
1966 else
1967 KASSERT(*refcount > 0,
1968 ("%s: decrement non-positive refcount %d for flag %d",
1969 __func__, *refcount, flag));
1970
1971 /* In case this mode is permanent, just touch refcount */
1972 if (ifp->if_flags & pflag) {
1973 *refcount += onswitch ? 1 : -1;
1974 return (0);
1975 }
1976
1977 /* Save ifnet parameters for if_ioctl() may fail */
1978 oldcount = *refcount;
1979 oldflags = ifp->if_flags;
1980
1981 /*
1982 * See if we aren't the only and touching refcount is enough.
1983 * Actually toggle interface flag if we are the first or last.
1984 */
1985 if (onswitch) {
1986 if ((*refcount)++)
1987 return (0);
1988 ifp->if_flags |= flag;
1989 } else {
1990 if (--(*refcount))
1991 return (0);
1992 ifp->if_flags &= ~flag;
1993 }
1994
1995 /* Call down the driver since we've changed interface flags */
1996 if (ifp->if_ioctl == NULL) {
1997 error = EOPNOTSUPP;
1998 goto recover;
1999 }
2000 ifr.ifr_flags = ifp->if_flags & 0xffff;
2001 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2002 IFF_LOCKGIANT(ifp);
2003 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2004 IFF_UNLOCKGIANT(ifp);
2005 if (error)
2006 goto recover;
2007 /* Notify userland that interface flags have changed */
2008 rt_ifmsg(ifp);
2009 return (0);
2010
2011recover:
2012 /* Recover after driver error */
2013 *refcount = oldcount;
2014 ifp->if_flags = oldflags;
2015 return (error);
2016}
2017
2018/*
2019 * Set/clear promiscuous mode on interface ifp based on the truth value
2020 * of pswitch. The calls are reference counted so that only the first
2021 * "on" request actually has an effect, as does the final "off" request.
2022 * Results are undefined if the "off" and "on" requests are not matched.
2023 */
2024int
2025ifpromisc(struct ifnet *ifp, int pswitch)
2026{
2027 int error;
2028 int oldflags = ifp->if_flags;
2029
2030 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2031 &ifp->if_pcount, pswitch);
2032 /* If promiscuous mode status has changed, log a message */
2033 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2034 log(LOG_INFO, "%s: promiscuous mode %s\n",
2035 ifp->if_xname,
2036 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2037 return (error);
2038}
2039
2040/*
2041 * Return interface configuration
2042 * of system. List may be used
2043 * in later ioctl's (above) to get
2044 * other information.
2045 */
2046/*ARGSUSED*/
2047static int
2048ifconf(u_long cmd, caddr_t data)
2049{
2050 struct ifconf *ifc = (struct ifconf *)data;
2051#ifdef __amd64__
2052 struct ifconf32 *ifc32 = (struct ifconf32 *)data;
2053 struct ifconf ifc_swab;
2054#endif
2055 struct ifnet *ifp;
2056 struct ifaddr *ifa;
2057 struct ifreq ifr;
2058 struct sbuf *sb;
2059 int error, full = 0, valid_len, max_len;
2060
2061#ifdef __amd64__
2062 if (cmd == SIOCGIFCONF32) {
2063 ifc_swab.ifc_len = ifc32->ifc_len;
2064 ifc_swab.ifc_buf = (caddr_t)(uintptr_t)ifc32->ifc_buf;
2065 ifc = &ifc_swab;
2066 }
2067#endif
2068 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2069 max_len = MAXPHYS - 1;
2070
2071 /* Prevent hostile input from being able to crash the system */
2072 if (ifc->ifc_len <= 0)
2073 return (EINVAL);
2074
2075again:
2076 if (ifc->ifc_len <= max_len) {
2077 max_len = ifc->ifc_len;
2078 full = 1;
2079 }
2080 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2081 max_len = 0;
2082 valid_len = 0;
2083
2084 IFNET_RLOCK(); /* could sleep XXX */
2085 TAILQ_FOREACH(ifp, &ifnet, if_link) {
2086 int addrs;
2087
2088 /*
2089 * Zero the ifr_name buffer to make sure we don't
2090 * disclose the contents of the stack.
2091 */
2092 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2093
2094 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2095 >= sizeof(ifr.ifr_name)) {
2096 sbuf_delete(sb);
2097 IFNET_RUNLOCK();
2098 return (ENAMETOOLONG);
2099 }
2100
2101 addrs = 0;
2102 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2103 struct sockaddr *sa = ifa->ifa_addr;
2104
2105 if (jailed(curthread->td_ucred) &&
2106 prison_if(curthread->td_ucred, sa))
2107 continue;
2108 addrs++;
2109#ifdef COMPAT_43
2110 if (cmd == OSIOCGIFCONF) {
2111 struct osockaddr *osa =
2112 (struct osockaddr *)&ifr.ifr_addr;
2113 ifr.ifr_addr = *sa;
2114 osa->sa_family = sa->sa_family;
2115 sbuf_bcat(sb, &ifr, sizeof(ifr));
2116 max_len += sizeof(ifr);
2117 } else
2118#endif
2119 if (sa->sa_len <= sizeof(*sa)) {
2120 ifr.ifr_addr = *sa;
2121 sbuf_bcat(sb, &ifr, sizeof(ifr));
2122 max_len += sizeof(ifr);
2123 } else {
2124 sbuf_bcat(sb, &ifr,
2125 offsetof(struct ifreq, ifr_addr));
2126 max_len += offsetof(struct ifreq, ifr_addr);
2127 sbuf_bcat(sb, sa, sa->sa_len);
2128 max_len += sa->sa_len;
2129 }
2130
2131 if (!sbuf_overflowed(sb))
2132 valid_len = sbuf_len(sb);
2133 }
2134 if (addrs == 0) {
2135 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2136 sbuf_bcat(sb, &ifr, sizeof(ifr));
2137 max_len += sizeof(ifr);
2138
2139 if (!sbuf_overflowed(sb))
2140 valid_len = sbuf_len(sb);
2141 }
2142 }
2143 IFNET_RUNLOCK();
2144
2145 /*
2146 * If we didn't allocate enough space (uncommon), try again. If
2147 * we have already allocated as much space as we are allowed,
2148 * return what we've got.
2149 */
2150 if (valid_len != max_len && !full) {
2151 sbuf_delete(sb);
2152 goto again;
2153 }
2154
2155 ifc->ifc_len = valid_len;
2156#ifdef __amd64__
2157 if (cmd == SIOCGIFCONF32)
2158 ifc32->ifc_len = valid_len;
2159#endif
2160 sbuf_finish(sb);
2161 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2162 sbuf_delete(sb);
2163 return (error);
2164}
2165
2166/*
2167 * Just like ifpromisc(), but for all-multicast-reception mode.
2168 */
2169int
2170if_allmulti(struct ifnet *ifp, int onswitch)
2171{
2172
2173 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2174}
2175
2176struct ifmultiaddr *
2177if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
2178{
2179 struct ifmultiaddr *ifma;
2180
2181 IF_ADDR_LOCK_ASSERT(ifp);
2182
2183 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2184 if (sa->sa_family == AF_LINK) {
2185 if (sa_dl_equal(ifma->ifma_addr, sa))
2186 break;
2187 } else {
2188 if (sa_equal(ifma->ifma_addr, sa))
2189 break;
2190 }
2191 }
2192
2193 return ifma;
2194}
2195
2196/*
2197 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
2198 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
2199 * the ifnet multicast address list here, so the caller must do that and
2200 * other setup work (such as notifying the device driver). The reference
2201 * count is initialized to 1.
2202 */
2203static struct ifmultiaddr *
2204if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
2205 int mflags)
2206{
2207 struct ifmultiaddr *ifma;
2208 struct sockaddr *dupsa;
2209
2210 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, mflags |
2211 M_ZERO);
2212 if (ifma == NULL)
2213 return (NULL);
2214
2215 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, mflags);
2216 if (dupsa == NULL) {
2217 FREE(ifma, M_IFMADDR);
2218 return (NULL);
2219 }
2220 bcopy(sa, dupsa, sa->sa_len);
2221 ifma->ifma_addr = dupsa;
2222
2223 ifma->ifma_ifp = ifp;
2224 ifma->ifma_refcount = 1;
2225 ifma->ifma_protospec = NULL;
2226
2227 if (llsa == NULL) {
2228 ifma->ifma_lladdr = NULL;
2229 return (ifma);
2230 }
2231
2232 MALLOC(dupsa, struct sockaddr *, llsa->sa_len, M_IFMADDR, mflags);
2233 if (dupsa == NULL) {
2234 FREE(ifma->ifma_addr, M_IFMADDR);
2235 FREE(ifma, M_IFMADDR);
2236 return (NULL);
2237 }
2238 bcopy(llsa, dupsa, llsa->sa_len);
2239 ifma->ifma_lladdr = dupsa;
2240
2241 return (ifma);
2242}
2243
2244/*
2245 * if_freemulti: free ifmultiaddr structure and possibly attached related
2246 * addresses. The caller is responsible for implementing reference
2247 * counting, notifying the driver, handling routing messages, and releasing
2248 * any dependent link layer state.
2249 */
2250static void
2251if_freemulti(struct ifmultiaddr *ifma)
2252{
2253
2254 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
2255 ifma->ifma_refcount));
2256 KASSERT(ifma->ifma_protospec == NULL,
2257 ("if_freemulti: protospec not NULL"));
2258
2259 if (ifma->ifma_lladdr != NULL)
2260 FREE(ifma->ifma_lladdr, M_IFMADDR);
2261 FREE(ifma->ifma_addr, M_IFMADDR);
2262 FREE(ifma, M_IFMADDR);
2263}
2264
2265/*
2266 * Register an additional multicast address with a network interface.
2267 *
2268 * - If the address is already present, bump the reference count on the
2269 * address and return.
2270 * - If the address is not link-layer, look up a link layer address.
2271 * - Allocate address structures for one or both addresses, and attach to the
2272 * multicast address list on the interface. If automatically adding a link
2273 * layer address, the protocol address will own a reference to the link
2274 * layer address, to be freed when it is freed.
2275 * - Notify the network device driver of an addition to the multicast address
2276 * list.
2277 *
2278 * 'sa' points to caller-owned memory with the desired multicast address.
2279 *
2280 * 'retifma' will be used to return a pointer to the resulting multicast
2281 * address reference, if desired.
2282 */
2283int
2284if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2285 struct ifmultiaddr **retifma)
2286{
2287 struct ifmultiaddr *ifma, *ll_ifma;
2288 struct sockaddr *llsa;
2289 int error;
2290
2291 /*
2292 * If the address is already present, return a new reference to it;
2293 * otherwise, allocate storage and set up a new address.
2294 */
2295 IF_ADDR_LOCK(ifp);
2296 ifma = if_findmulti(ifp, sa);
2297 if (ifma != NULL) {
2298 ifma->ifma_refcount++;
2299 if (retifma != NULL)
2300 *retifma = ifma;
2301 IF_ADDR_UNLOCK(ifp);
2302 return (0);
2303 }
2304
2305 /*
2306 * The address isn't already present; resolve the protocol address
2307 * into a link layer address, and then look that up, bump its
2308 * refcount or allocate an ifma for that also. If 'llsa' was
2309 * returned, we will need to free it later.
2310 */
2311 llsa = NULL;
2312 ll_ifma = NULL;
2313 if (ifp->if_resolvemulti != NULL) {
2314 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2315 if (error)
2316 goto unlock_out;
2317 }
2318
2319 /*
2320 * Allocate the new address. Don't hook it up yet, as we may also
2321 * need to allocate a link layer multicast address.
2322 */
2323 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
2324 if (ifma == NULL) {
2325 error = ENOMEM;
2326 goto free_llsa_out;
2327 }
2328
2329 /*
2330 * If a link layer address is found, we'll need to see if it's
2331 * already present in the address list, or allocate is as well.
2332 * When this block finishes, the link layer address will be on the
2333 * list.
2334 */
2335 if (llsa != NULL) {
2336 ll_ifma = if_findmulti(ifp, llsa);
2337 if (ll_ifma == NULL) {
2338 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
2339 if (ll_ifma == NULL) {
2340 --ifma->ifma_refcount;
2341 if_freemulti(ifma);
2342 error = ENOMEM;
2343 goto free_llsa_out;
2344 }
2345 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
2346 ifma_link);
2347 } else
2348 ll_ifma->ifma_refcount++;
2349 ifma->ifma_llifma = ll_ifma;
2350 }
2351
2352 /*
2353 * We now have a new multicast address, ifma, and possibly a new or
2354 * referenced link layer address. Add the primary address to the
2355 * ifnet address list.
2356 */
2357 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2358
2359 if (retifma != NULL)
2360 *retifma = ifma;
2361
2362 /*
2363 * Must generate the message while holding the lock so that 'ifma'
2364 * pointer is still valid.
2365 */
2366 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2367 IF_ADDR_UNLOCK(ifp);
2368
2369 /*
2370 * We are certain we have added something, so call down to the
2371 * interface to let them know about it.
2372 */
2373 if (ifp->if_ioctl != NULL) {
2374 IFF_LOCKGIANT(ifp);
2375 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
2376 IFF_UNLOCKGIANT(ifp);
2377 }
2378
2379 if (llsa != NULL)
2380 FREE(llsa, M_IFMADDR);
2381
2382 return (0);
2383
2384free_llsa_out:
2385 if (llsa != NULL)
2386 FREE(llsa, M_IFMADDR);
2387
2388unlock_out:
2389 IF_ADDR_UNLOCK(ifp);
2390 return (error);
2391}
2392
2393/*
2394 * Delete a multicast group membership by network-layer group address.
2395 *
2396 * Returns ENOENT if the entry could not be found. If ifp no longer
2397 * exists, results are undefined. This entry point should only be used
2398 * from subsystems which do appropriate locking to hold ifp for the
2399 * duration of the call.
2400 * Network-layer protocol domains must use if_delmulti_ifma().
2401 */
2402int
2403if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2404{
2405 struct ifmultiaddr *ifma;
2406 int lastref;
2407#ifdef INVARIANTS
2408 struct ifnet *oifp;
2409
2410 IFNET_RLOCK();
2411 TAILQ_FOREACH(oifp, &ifnet, if_link)
2412 if (ifp == oifp)
2413 break;
2414 if (ifp != oifp)
2415 ifp = NULL;
2416 IFNET_RUNLOCK();
2417
2418 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
2419#endif
2420 if (ifp == NULL)
2421 return (ENOENT);
2422
2423 IF_ADDR_LOCK(ifp);
2424 lastref = 0;
2425 ifma = if_findmulti(ifp, sa);
2426 if (ifma != NULL)
2427 lastref = if_delmulti_locked(ifp, ifma, 0);
2428 IF_ADDR_UNLOCK(ifp);
2429
2430 if (ifma == NULL)
2431 return (ENOENT);
2432
2433 if (lastref && ifp->if_ioctl != NULL) {
2434 IFF_LOCKGIANT(ifp);
2435 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
2436 IFF_UNLOCKGIANT(ifp);
2437 }
2438
2439 return (0);
2440}
2441
2442/*
2443 * Delete a multicast group membership by group membership pointer.
2444 * Network-layer protocol domains must use this routine.
2445 *
2446 * It is safe to call this routine if the ifp disappeared. Callers should
2447 * hold IFF_LOCKGIANT() to avoid a LOR in case the hardware needs to be
2448 * reconfigured.
2449 */
2450void
2451if_delmulti_ifma(struct ifmultiaddr *ifma)
2452{
2453 struct ifnet *ifp;
2454 int lastref;
2455
2456 ifp = ifma->ifma_ifp;
2457#ifdef DIAGNOSTIC
2458 if (ifp == NULL) {
2459 printf("%s: ifma_ifp seems to be detached\n", __func__);
2460 } else {
2461 struct ifnet *oifp;
2462
2463 IFNET_RLOCK();
2464 TAILQ_FOREACH(oifp, &ifnet, if_link)
2465 if (ifp == oifp)
2466 break;
2467 if (ifp != oifp) {
2468 printf("%s: ifnet %p disappeared\n", __func__, ifp);
2469 ifp = NULL;
2470 }
2471 IFNET_RUNLOCK();
2472 }
2473#endif
2474 /*
2475 * If and only if the ifnet instance exists: Acquire the address lock.
2476 */
2477 if (ifp != NULL)
2478 IF_ADDR_LOCK(ifp);
2479
2480 lastref = if_delmulti_locked(ifp, ifma, 0);
2481
2482 if (ifp != NULL) {
2483 /*
2484 * If and only if the ifnet instance exists:
2485 * Release the address lock.
2486 * If the group was left: update the hardware hash filter.
2487 */
2488 IF_ADDR_UNLOCK(ifp);
2489 if (lastref && ifp->if_ioctl != NULL) {
2490 IFF_LOCKGIANT(ifp);
2491 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
2492 IFF_UNLOCKGIANT(ifp);
2493 }
2494 }
2495}
2496
2497/*
2498 * Perform deletion of network-layer and/or link-layer multicast address.
2499 *
2500 * Return 0 if the reference count was decremented.
2501 * Return 1 if the final reference was released, indicating that the
2502 * hardware hash filter should be reprogrammed.
2503 */
2504static int
2505if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
2506{
2507 struct ifmultiaddr *ll_ifma;
2508
2509 if (ifp != NULL && ifma->ifma_ifp != NULL) {
2510 KASSERT(ifma->ifma_ifp == ifp,
2511 ("%s: inconsistent ifp %p", __func__, ifp));
2512 IF_ADDR_LOCK_ASSERT(ifp);
2513 }
2514
2515 ifp = ifma->ifma_ifp;
2516
2517 /*
2518 * If the ifnet is detaching, null out references to ifnet,
2519 * so that upper protocol layers will notice, and not attempt
2520 * to obtain locks for an ifnet which no longer exists. The
2521 * routing socket announcement must happen before the ifnet
2522 * instance is detached from the system.
2523 */
2524 if (detaching) {
2525#ifdef DIAGNOSTIC
2526 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
2527#endif
2528 /*
2529 * ifp may already be nulled out if we are being reentered
2530 * to delete the ll_ifma.
2531 */
2532 if (ifp != NULL) {
2533 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2534 ifma->ifma_ifp = NULL;
2535 }
2536 }
2537
2538 if (--ifma->ifma_refcount > 0)
2539 return 0;
2540
2541 /*
2542 * If this ifma is a network-layer ifma, a link-layer ifma may
2543 * have been associated with it. Release it first if so.
2544 */
2545 ll_ifma = ifma->ifma_llifma;
2546 if (ll_ifma != NULL) {
2547 KASSERT(ifma->ifma_lladdr != NULL,
2548 ("%s: llifma w/o lladdr", __func__));
2549 if (detaching)
2550 ll_ifma->ifma_ifp = NULL; /* XXX */
2551 if (--ll_ifma->ifma_refcount == 0) {
2552 if (ifp != NULL) {
2553 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
2554 ifma_link);
2555 }
2556 if_freemulti(ll_ifma);
2557 }
2558 }
2559
2560 if (ifp != NULL)
2561 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2562
2563 if_freemulti(ifma);
2564
2565 /*
2566 * The last reference to this instance of struct ifmultiaddr
2567 * was released; the hardware should be notified of this change.
2568 */
2569 return 1;
2570}
2571
2572/*
2573 * Set the link layer address on an interface.
2574 *
2575 * At this time we only support certain types of interfaces,
2576 * and we don't allow the length of the address to change.
2577 */
2578int
2579if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2580{
2581 struct sockaddr_dl *sdl;
2582 struct ifaddr *ifa;
2583 struct ifreq ifr;
2584
2585 ifa = ifp->if_addr;
2586 if (ifa == NULL)
2587 return (EINVAL);
2588 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2589 if (sdl == NULL)
2590 return (EINVAL);
2591 if (len != sdl->sdl_alen) /* don't allow length to change */
2592 return (EINVAL);
2593 switch (ifp->if_type) {
2594 case IFT_ETHER:
2595 case IFT_FDDI:
2596 case IFT_XETHER:
2597 case IFT_ISO88025:
2598 case IFT_L2VLAN:
2599 case IFT_BRIDGE:
2600 case IFT_ARCNET:
2601 case IFT_IEEE8023ADLAG:
2596 bcopy(lladdr, LLADDR(sdl), len);
2597 break;
2598 default:
2599 return (ENODEV);
2600 }
2601 /*
2602 * If the interface is already up, we need
2603 * to re-init it in order to reprogram its
2604 * address filter.
2605 */
2606 if ((ifp->if_flags & IFF_UP) != 0) {
2607 if (ifp->if_ioctl) {
2608 IFF_LOCKGIANT(ifp);
2609 ifp->if_flags &= ~IFF_UP;
2610 ifr.ifr_flags = ifp->if_flags & 0xffff;
2611 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2612 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2613 ifp->if_flags |= IFF_UP;
2614 ifr.ifr_flags = ifp->if_flags & 0xffff;
2615 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2616 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2617 IFF_UNLOCKGIANT(ifp);
2618 }
2619#ifdef INET
2620 /*
2621 * Also send gratuitous ARPs to notify other nodes about
2622 * the address change.
2623 */
2624 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2625 if (ifa->ifa_addr->sa_family == AF_INET)
2626 arp_ifinit(ifp, ifa);
2627 }
2628#endif
2629 }
2630 return (0);
2631}
2632
2633/*
2634 * The name argument must be a pointer to storage which will last as
2635 * long as the interface does. For physical devices, the result of
2636 * device_get_name(dev) is a good choice and for pseudo-devices a
2637 * static string works well.
2638 */
2639void
2640if_initname(struct ifnet *ifp, const char *name, int unit)
2641{
2642 ifp->if_dname = name;
2643 ifp->if_dunit = unit;
2644 if (unit != IF_DUNIT_NONE)
2645 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2646 else
2647 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2648}
2649
2650int
2651if_printf(struct ifnet *ifp, const char * fmt, ...)
2652{
2653 va_list ap;
2654 int retval;
2655
2656 retval = printf("%s: ", ifp->if_xname);
2657 va_start(ap, fmt);
2658 retval += vprintf(fmt, ap);
2659 va_end(ap);
2660 return (retval);
2661}
2662
2663/*
2664 * When an interface is marked IFF_NEEDSGIANT, its if_start() routine cannot
2665 * be called without Giant. However, we often can't acquire the Giant lock
2666 * at those points; instead, we run it via a task queue that holds Giant via
2667 * if_start_deferred.
2668 *
2669 * XXXRW: We need to make sure that the ifnet isn't fully detached until any
2670 * outstanding if_start_deferred() tasks that will run after the free. This
2671 * probably means waiting in if_detach().
2672 */
2673void
2674if_start(struct ifnet *ifp)
2675{
2676
2677 NET_ASSERT_GIANT();
2678
2679 if ((ifp->if_flags & IFF_NEEDSGIANT) != 0 && debug_mpsafenet != 0) {
2680 if (mtx_owned(&Giant))
2681 (*(ifp)->if_start)(ifp);
2682 else
2683 taskqueue_enqueue(taskqueue_swi_giant,
2684 &ifp->if_starttask);
2685 } else
2686 (*(ifp)->if_start)(ifp);
2687}
2688
2689static void
2690if_start_deferred(void *context, int pending)
2691{
2692 struct ifnet *ifp;
2693
2694 /*
2695 * This code must be entered with Giant, and should never run if
2696 * we're not running with debug.mpsafenet.
2697 */
2698 KASSERT(debug_mpsafenet != 0, ("if_start_deferred: debug.mpsafenet"));
2699 GIANT_REQUIRED;
2700
2701 ifp = context;
2702 (ifp->if_start)(ifp);
2703}
2704
2705int
2706if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
2707{
2708 int active = 0;
2709
2710 IF_LOCK(ifq);
2711 if (_IF_QFULL(ifq)) {
2712 _IF_DROP(ifq);
2713 IF_UNLOCK(ifq);
2714 m_freem(m);
2715 return (0);
2716 }
2717 if (ifp != NULL) {
2718 ifp->if_obytes += m->m_pkthdr.len + adjust;
2719 if (m->m_flags & (M_BCAST|M_MCAST))
2720 ifp->if_omcasts++;
2721 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
2722 }
2723 _IF_ENQUEUE(ifq, m);
2724 IF_UNLOCK(ifq);
2725 if (ifp != NULL && !active)
2726 if_start(ifp);
2727 return (1);
2728}
2729
2730void
2731if_register_com_alloc(u_char type,
2732 if_com_alloc_t *a, if_com_free_t *f)
2733{
2734
2735 KASSERT(if_com_alloc[type] == NULL,
2736 ("if_register_com_alloc: %d already registered", type));
2737 KASSERT(if_com_free[type] == NULL,
2738 ("if_register_com_alloc: %d free already registered", type));
2739
2740 if_com_alloc[type] = a;
2741 if_com_free[type] = f;
2742}
2743
2744void
2745if_deregister_com_alloc(u_char type)
2746{
2747
2748 KASSERT(if_com_alloc[type] != NULL,
2749 ("if_deregister_com_alloc: %d not registered", type));
2750 KASSERT(if_com_free[type] != NULL,
2751 ("if_deregister_com_alloc: %d free not registered", type));
2752 if_com_alloc[type] = NULL;
2753 if_com_free[type] = NULL;
2754}
2602 bcopy(lladdr, LLADDR(sdl), len);
2603 break;
2604 default:
2605 return (ENODEV);
2606 }
2607 /*
2608 * If the interface is already up, we need
2609 * to re-init it in order to reprogram its
2610 * address filter.
2611 */
2612 if ((ifp->if_flags & IFF_UP) != 0) {
2613 if (ifp->if_ioctl) {
2614 IFF_LOCKGIANT(ifp);
2615 ifp->if_flags &= ~IFF_UP;
2616 ifr.ifr_flags = ifp->if_flags & 0xffff;
2617 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2618 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2619 ifp->if_flags |= IFF_UP;
2620 ifr.ifr_flags = ifp->if_flags & 0xffff;
2621 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2622 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2623 IFF_UNLOCKGIANT(ifp);
2624 }
2625#ifdef INET
2626 /*
2627 * Also send gratuitous ARPs to notify other nodes about
2628 * the address change.
2629 */
2630 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2631 if (ifa->ifa_addr->sa_family == AF_INET)
2632 arp_ifinit(ifp, ifa);
2633 }
2634#endif
2635 }
2636 return (0);
2637}
2638
2639/*
2640 * The name argument must be a pointer to storage which will last as
2641 * long as the interface does. For physical devices, the result of
2642 * device_get_name(dev) is a good choice and for pseudo-devices a
2643 * static string works well.
2644 */
2645void
2646if_initname(struct ifnet *ifp, const char *name, int unit)
2647{
2648 ifp->if_dname = name;
2649 ifp->if_dunit = unit;
2650 if (unit != IF_DUNIT_NONE)
2651 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2652 else
2653 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2654}
2655
2656int
2657if_printf(struct ifnet *ifp, const char * fmt, ...)
2658{
2659 va_list ap;
2660 int retval;
2661
2662 retval = printf("%s: ", ifp->if_xname);
2663 va_start(ap, fmt);
2664 retval += vprintf(fmt, ap);
2665 va_end(ap);
2666 return (retval);
2667}
2668
2669/*
2670 * When an interface is marked IFF_NEEDSGIANT, its if_start() routine cannot
2671 * be called without Giant. However, we often can't acquire the Giant lock
2672 * at those points; instead, we run it via a task queue that holds Giant via
2673 * if_start_deferred.
2674 *
2675 * XXXRW: We need to make sure that the ifnet isn't fully detached until any
2676 * outstanding if_start_deferred() tasks that will run after the free. This
2677 * probably means waiting in if_detach().
2678 */
2679void
2680if_start(struct ifnet *ifp)
2681{
2682
2683 NET_ASSERT_GIANT();
2684
2685 if ((ifp->if_flags & IFF_NEEDSGIANT) != 0 && debug_mpsafenet != 0) {
2686 if (mtx_owned(&Giant))
2687 (*(ifp)->if_start)(ifp);
2688 else
2689 taskqueue_enqueue(taskqueue_swi_giant,
2690 &ifp->if_starttask);
2691 } else
2692 (*(ifp)->if_start)(ifp);
2693}
2694
2695static void
2696if_start_deferred(void *context, int pending)
2697{
2698 struct ifnet *ifp;
2699
2700 /*
2701 * This code must be entered with Giant, and should never run if
2702 * we're not running with debug.mpsafenet.
2703 */
2704 KASSERT(debug_mpsafenet != 0, ("if_start_deferred: debug.mpsafenet"));
2705 GIANT_REQUIRED;
2706
2707 ifp = context;
2708 (ifp->if_start)(ifp);
2709}
2710
2711int
2712if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
2713{
2714 int active = 0;
2715
2716 IF_LOCK(ifq);
2717 if (_IF_QFULL(ifq)) {
2718 _IF_DROP(ifq);
2719 IF_UNLOCK(ifq);
2720 m_freem(m);
2721 return (0);
2722 }
2723 if (ifp != NULL) {
2724 ifp->if_obytes += m->m_pkthdr.len + adjust;
2725 if (m->m_flags & (M_BCAST|M_MCAST))
2726 ifp->if_omcasts++;
2727 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
2728 }
2729 _IF_ENQUEUE(ifq, m);
2730 IF_UNLOCK(ifq);
2731 if (ifp != NULL && !active)
2732 if_start(ifp);
2733 return (1);
2734}
2735
2736void
2737if_register_com_alloc(u_char type,
2738 if_com_alloc_t *a, if_com_free_t *f)
2739{
2740
2741 KASSERT(if_com_alloc[type] == NULL,
2742 ("if_register_com_alloc: %d already registered", type));
2743 KASSERT(if_com_free[type] == NULL,
2744 ("if_register_com_alloc: %d free already registered", type));
2745
2746 if_com_alloc[type] = a;
2747 if_com_free[type] = f;
2748}
2749
2750void
2751if_deregister_com_alloc(u_char type)
2752{
2753
2754 KASSERT(if_com_alloc[type] != NULL,
2755 ("if_deregister_com_alloc: %d not registered", type));
2756 KASSERT(if_com_free[type] != NULL,
2757 ("if_deregister_com_alloc: %d free not registered", type));
2758 if_com_alloc[type] = NULL;
2759 if_com_free[type] = NULL;
2760}