- Use priority message to carry out ipfw callout.
[dragonfly.git] / sys / net / if.c
CommitLineData
984263bc
MD
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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)if.c 8.3 (Berkeley) 1/4/94
f23061d4 34 * $FreeBSD: src/sys/net/if.c,v 1.185 2004/03/13 02:35:03 brooks Exp $
71fc104f 35 * $DragonFly: src/sys/net/if.c,v 1.81 2008/10/03 00:26:21 hasso Exp $
984263bc
MD
36 */
37
38#include "opt_compat.h"
39#include "opt_inet6.h"
40#include "opt_inet.h"
2b71c8f1 41#include "opt_polling.h"
984263bc
MD
42
43#include <sys/param.h>
44#include <sys/malloc.h>
45#include <sys/mbuf.h>
46#include <sys/systm.h>
47#include <sys/proc.h>
6b6e0885 48#include <sys/protosw.h>
984263bc
MD
49#include <sys/socket.h>
50#include <sys/socketvar.h>
6b6e0885 51#include <sys/socketops.h>
984263bc
MD
52#include <sys/protosw.h>
53#include <sys/kernel.h>
9db4b353 54#include <sys/ktr.h>
984263bc
MD
55#include <sys/sockio.h>
56#include <sys/syslog.h>
57#include <sys/sysctl.h>
698ac46c 58#include <sys/domain.h>
e9cb6d99 59#include <sys/thread.h>
65a24520 60#include <sys/thread2.h>
78195a76 61#include <sys/serialize.h>
b2632176 62#include <sys/msgport2.h>
71fc104f 63#include <sys/bus.h>
984263bc
MD
64
65#include <net/if.h>
66#include <net/if_arp.h>
67#include <net/if_dl.h>
68#include <net/if_types.h>
69#include <net/if_var.h>
4d723e5a 70#include <net/ifq_var.h>
984263bc
MD
71#include <net/radix.h>
72#include <net/route.h>
65a24520 73#include <net/if_clone.h>
9db4b353 74#include <net/netisr.h>
b2632176
SZ
75#include <net/netmsg2.h>
76
984263bc 77#include <machine/stdarg.h>
b2632176 78#include <machine/smp.h>
984263bc
MD
79
80#if defined(INET) || defined(INET6)
81/*XXX*/
82#include <netinet/in.h>
83#include <netinet/in_var.h>
84#include <netinet/if_ether.h>
85#ifdef INET6
984263bc
MD
86#include <netinet6/in6_var.h>
87#include <netinet6/in6_ifattach.h>
88#endif
89#endif
90
9eee10d0
DRJ
91#if defined(COMPAT_43)
92#include <emulation/43bsd/43bsd_socket.h>
93#endif /* COMPAT_43 */
94
b2632176
SZ
95struct netmsg_ifaddr {
96 struct netmsg netmsg;
97 struct ifaddr *ifa;
98 struct ifnet *ifp;
99 int tail;
100};
101
984263bc
MD
102/*
103 * System initialization
104 */
698ac46c
HS
105static void if_attachdomain(void *);
106static void if_attachdomain1(struct ifnet *);
436c57ea
SZ
107static int ifconf(u_long, caddr_t, struct ucred *);
108static void ifinit(void *);
90af4fd3 109static void ifnetinit(void *);
436c57ea
SZ
110static void if_slowtimo(void *);
111static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
112static int if_rtdel(struct radix_node *, void *);
984263bc
MD
113
114#ifdef INET6
115/*
116 * XXX: declare here to avoid to include many inet6 related files..
117 * should be more generalized?
118 */
436c57ea 119extern void nd6_setmtu(struct ifnet *);
984263bc
MD
120#endif
121
436c57ea
SZ
122SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
123SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
124
125SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
b2632176 126/* Must be after netisr_init */
90af4fd3 127SYSINIT(ifnet, SI_SUB_PRE_DRIVERS, SI_ORDER_SECOND, ifnetinit, NULL)
436c57ea
SZ
128
129MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
130MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
984263bc 131
436c57ea 132int ifqmaxlen = IFQ_MAXLEN;
b64bfcc3 133struct ifnethead ifnet = TAILQ_HEAD_INITIALIZER(ifnet);
984263bc 134
9db4b353
SZ
135/* In ifq_dispatch(), try to do direct ifnet.if_start first */
136static int ifq_dispatch_schedonly = 0;
137SYSCTL_INT(_net_link_generic, OID_AUTO, ifq_dispatch_schedonly, CTLFLAG_RW,
138 &ifq_dispatch_schedonly, 0, "");
139
140/* In ifq_dispatch(), schedule ifnet.if_start without checking ifnet.if_snd */
141static int ifq_dispatch_schednochk = 0;
142SYSCTL_INT(_net_link_generic, OID_AUTO, ifq_dispatch_schednochk, CTLFLAG_RW,
143 &ifq_dispatch_schednochk, 0, "");
144
145/* In if_devstart(), try to do direct ifnet.if_start first */
146static int if_devstart_schedonly = 0;
147SYSCTL_INT(_net_link_generic, OID_AUTO, if_devstart_schedonly, CTLFLAG_RW,
148 &if_devstart_schedonly, 0, "");
149
150/* In if_devstart(), schedule ifnet.if_start without checking ifnet.if_snd */
151static int if_devstart_schednochk = 0;
152SYSCTL_INT(_net_link_generic, OID_AUTO, if_devstart_schednochk, CTLFLAG_RW,
153 &if_devstart_schednochk, 0, "");
154
155#ifdef SMP
156/* Schedule ifnet.if_start on the current CPU */
157static int if_start_oncpu_sched = 0;
158SYSCTL_INT(_net_link_generic, OID_AUTO, if_start_oncpu_sched, CTLFLAG_RW,
159 &if_start_oncpu_sched, 0, "");
160#endif
161
436c57ea
SZ
162struct callout if_slowtimo_timer;
163
164int if_index = 0;
165struct ifnet **ifindex2ifnet = NULL;
90af4fd3 166static struct thread ifnet_threads[MAXCPU];
92db3805 167static int ifnet_mpsafe_thread = NETMSG_SERVICE_MPSAFE;
abbb44bb 168
9db4b353
SZ
169#define IFQ_KTR_STRING "ifq=%p"
170#define IFQ_KTR_ARG_SIZE (sizeof(void *))
171#ifndef KTR_IFQ
172#define KTR_IFQ KTR_ALL
173#endif
174KTR_INFO_MASTER(ifq);
175KTR_INFO(KTR_IFQ, ifq, enqueue, 0, IFQ_KTR_STRING, IFQ_KTR_ARG_SIZE);
176KTR_INFO(KTR_IFQ, ifq, dequeue, 1, IFQ_KTR_STRING, IFQ_KTR_ARG_SIZE);
177#define logifq(name, arg) KTR_LOG(ifq_ ## name, arg)
178
179#define IF_START_KTR_STRING "ifp=%p"
180#define IF_START_KTR_ARG_SIZE (sizeof(void *))
181#ifndef KTR_IF_START
182#define KTR_IF_START KTR_ALL
183#endif
184KTR_INFO_MASTER(if_start);
185KTR_INFO(KTR_IF_START, if_start, run, 0,
186 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
187KTR_INFO(KTR_IF_START, if_start, sched, 1,
188 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
189KTR_INFO(KTR_IF_START, if_start, avoid, 2,
190 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
191KTR_INFO(KTR_IF_START, if_start, contend_sched, 3,
192 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
193KTR_INFO(KTR_IF_START, if_start, chase_sched, 4,
194 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
195#define logifstart(name, arg) KTR_LOG(if_start_ ## name, arg)
196
984263bc
MD
197/*
198 * Network interface utility routines.
199 *
200 * Routines with ifa_ifwith* names take sockaddr *'s as
201 * parameters.
202 */
203/* ARGSUSED*/
204void
f23061d4 205ifinit(void *dummy)
984263bc
MD
206{
207 struct ifnet *ifp;
984263bc 208
abbb44bb
JS
209 callout_init(&if_slowtimo_timer);
210
4986965b 211 crit_enter();
984263bc
MD
212 TAILQ_FOREACH(ifp, &ifnet, if_link) {
213 if (ifp->if_snd.ifq_maxlen == 0) {
3e4a09e7 214 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
984263bc
MD
215 ifp->if_snd.ifq_maxlen = ifqmaxlen;
216 }
217 }
4986965b 218 crit_exit();
abbb44bb 219
984263bc
MD
220 if_slowtimo(0);
221}
222
9db4b353
SZ
223static int
224if_start_cpuid(struct ifnet *ifp)
225{
226 return ifp->if_cpuid;
227}
228
229#ifdef DEVICE_POLLING
230static int
231if_start_cpuid_poll(struct ifnet *ifp)
232{
233 int poll_cpuid = ifp->if_poll_cpuid;
234
235 if (poll_cpuid >= 0)
236 return poll_cpuid;
237 else
238 return ifp->if_cpuid;
239}
240#endif
241
242static void
243if_start_ipifunc(void *arg)
244{
245 struct ifnet *ifp = arg;
246 struct lwkt_msg *lmsg = &ifp->if_start_nmsg[mycpuid].nm_lmsg;
247
248 crit_enter();
249 if (lmsg->ms_flags & MSGF_DONE)
d7944f0b 250 lwkt_sendmsg(ifnet_portfn(mycpuid), lmsg);
9db4b353
SZ
251 crit_exit();
252}
253
254/*
255 * Schedule ifnet.if_start on ifnet's CPU
256 */
257static void
258if_start_schedule(struct ifnet *ifp)
259{
260#ifdef SMP
261 int cpu;
262
263 if (if_start_oncpu_sched)
264 cpu = mycpuid;
265 else
266 cpu = ifp->if_start_cpuid(ifp);
267
268 if (cpu != mycpuid)
269 lwkt_send_ipiq(globaldata_find(cpu), if_start_ipifunc, ifp);
270 else
271#endif
272 if_start_ipifunc(ifp);
273}
274
275/*
276 * NOTE:
277 * This function will release ifnet.if_start interlock,
278 * if ifnet.if_start does not need to be scheduled
279 */
280static __inline int
281if_start_need_schedule(struct ifaltq *ifq, int running)
282{
283 if (!running || ifq_is_empty(ifq)
284#ifdef ALTQ
285 || ifq->altq_tbr != NULL
286#endif
287 ) {
288 ALTQ_LOCK(ifq);
289 /*
290 * ifnet.if_start interlock is released, if:
291 * 1) Hardware can not take any packets, due to
292 * o interface is marked down
293 * o hardware queue is full (IFF_OACTIVE)
294 * Under the second situation, hardware interrupt
295 * or polling(4) will call/schedule ifnet.if_start
296 * when hardware queue is ready
297 * 2) There is not packet in the ifnet.if_snd.
298 * Further ifq_dispatch or ifq_handoff will call/
299 * schedule ifnet.if_start
300 * 3) TBR is used and it does not allow further
301 * dequeueing.
302 * TBR callout will call ifnet.if_start
303 */
304 if (!running || !ifq_data_ready(ifq)) {
305 ifq->altq_started = 0;
306 ALTQ_UNLOCK(ifq);
307 return 0;
308 }
309 ALTQ_UNLOCK(ifq);
310 }
311 return 1;
312}
313
314static void
315if_start_dispatch(struct netmsg *nmsg)
316{
317 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
318 struct ifnet *ifp = lmsg->u.ms_resultp;
319 struct ifaltq *ifq = &ifp->if_snd;
320 int running = 0;
321
322 crit_enter();
323 lwkt_replymsg(lmsg, 0); /* reply ASAP */
324 crit_exit();
325
326#ifdef SMP
327 if (!if_start_oncpu_sched && mycpuid != ifp->if_start_cpuid(ifp)) {
328 /*
329 * If the ifnet is still up, we need to
330 * chase its CPU change.
331 */
332 if (ifp->if_flags & IFF_UP) {
333 logifstart(chase_sched, ifp);
334 if_start_schedule(ifp);
335 return;
336 } else {
337 goto check;
338 }
339 }
340#endif
341
342 if (ifp->if_flags & IFF_UP) {
343 lwkt_serialize_enter(ifp->if_serializer); /* XXX try? */
344 if ((ifp->if_flags & IFF_OACTIVE) == 0) {
345 logifstart(run, ifp);
346 ifp->if_start(ifp);
347 if ((ifp->if_flags &
348 (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
349 running = 1;
350 }
351 lwkt_serialize_exit(ifp->if_serializer);
352 }
f21578c4 353#ifdef SMP
9db4b353 354check:
f21578c4 355#endif
9db4b353
SZ
356 if (if_start_need_schedule(ifq, running)) {
357 crit_enter();
358 if (lmsg->ms_flags & MSGF_DONE) { /* XXX necessary? */
359 logifstart(sched, ifp);
d7944f0b 360 lwkt_sendmsg(ifnet_portfn(mycpuid), lmsg);
9db4b353
SZ
361 }
362 crit_exit();
363 }
364}
365
366/* Device driver ifnet.if_start helper function */
367void
368if_devstart(struct ifnet *ifp)
369{
370 struct ifaltq *ifq = &ifp->if_snd;
371 int running = 0;
372
373 ASSERT_SERIALIZED(ifp->if_serializer);
374
375 ALTQ_LOCK(ifq);
376 if (ifq->altq_started || !ifq_data_ready(ifq)) {
377 logifstart(avoid, ifp);
378 ALTQ_UNLOCK(ifq);
379 return;
380 }
381 ifq->altq_started = 1;
382 ALTQ_UNLOCK(ifq);
383
384 if (if_devstart_schedonly) {
385 /*
386 * Always schedule ifnet.if_start on ifnet's CPU,
387 * short circuit the rest of this function.
388 */
389 logifstart(sched, ifp);
390 if_start_schedule(ifp);
391 return;
392 }
393
394 logifstart(run, ifp);
395 ifp->if_start(ifp);
396
397 if ((ifp->if_flags & (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
398 running = 1;
399
400 if (if_devstart_schednochk || if_start_need_schedule(ifq, running)) {
401 /*
402 * More data need to be transmitted, ifnet.if_start is
403 * scheduled on ifnet's CPU, and we keep going.
404 * NOTE: ifnet.if_start interlock is not released.
405 */
406 logifstart(sched, ifp);
407 if_start_schedule(ifp);
408 }
409}
410
984263bc 411/*
78195a76
MD
412 * Attach an interface to the list of "active" interfaces.
413 *
414 * The serializer is optional. If non-NULL access to the interface
415 * may be MPSAFE.
984263bc
MD
416 */
417void
78195a76 418if_attach(struct ifnet *ifp, lwkt_serialize_t serializer)
984263bc
MD
419{
420 unsigned socksize, ifasize;
421 int namelen, masklen;
82ed7fc2
RG
422 struct sockaddr_dl *sdl;
423 struct ifaddr *ifa;
e3e4574a 424 struct ifaltq *ifq;
b2632176 425 int i;
590b8cd4 426
984263bc 427 static int if_indexlim = 8;
984263bc 428
78195a76
MD
429 /*
430 * The serializer can be passed in from the device, allowing the
431 * same serializer to be used for both the interrupt interlock and
432 * the device queue. If not specified, the netif structure will
433 * use an embedded serializer.
434 */
435 if (serializer == NULL) {
436 serializer = &ifp->if_default_serializer;
437 lwkt_serialize_init(serializer);
438 }
439 ifp->if_serializer = serializer;
440
9db4b353
SZ
441 ifp->if_start_cpuid = if_start_cpuid;
442 ifp->if_cpuid = 0;
443
1630efc5
SZ
444#ifdef DEVICE_POLLING
445 /* Device is not in polling mode by default */
446 ifp->if_poll_cpuid = -1;
9db4b353
SZ
447 if (ifp->if_poll != NULL)
448 ifp->if_start_cpuid = if_start_cpuid_poll;
1630efc5
SZ
449#endif
450
9db4b353
SZ
451 ifp->if_start_nmsg = kmalloc(ncpus * sizeof(struct netmsg),
452 M_IFADDR /* XXX */, M_WAITOK);
453 for (i = 0; i < ncpus; ++i) {
454 netmsg_init(&ifp->if_start_nmsg[i], &netisr_adone_rport, 0,
455 if_start_dispatch);
456 ifp->if_start_nmsg[i].nm_lmsg.u.ms_resultp = ifp;
457 }
458
984263bc
MD
459 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
460 ifp->if_index = ++if_index;
b2632176 461
984263bc
MD
462 /*
463 * XXX -
464 * The old code would work if the interface passed a pre-existing
465 * chain of ifaddrs to this code. We don't trust our callers to
466 * properly initialize the tailq, however, so we no longer allow
467 * this unlikely case.
468 */
b2632176
SZ
469 ifp->if_addrheads = kmalloc(ncpus * sizeof(struct ifaddrhead),
470 M_IFADDR, M_WAITOK | M_ZERO);
471 for (i = 0; i < ncpus; ++i)
472 TAILQ_INIT(&ifp->if_addrheads[i]);
473
984263bc
MD
474 TAILQ_INIT(&ifp->if_prefixhead);
475 LIST_INIT(&ifp->if_multiaddrs);
476 getmicrotime(&ifp->if_lastchange);
141697b6 477 if (ifindex2ifnet == NULL || if_index >= if_indexlim) {
590b8cd4 478 unsigned int n;
141697b6 479 struct ifnet **q;
590b8cd4
JH
480
481 if_indexlim <<= 1;
984263bc
MD
482
483 /* grow ifindex2ifnet */
141697b6 484 n = if_indexlim * sizeof(*q);
efda3bd0 485 q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
984263bc 486 if (ifindex2ifnet) {
f23061d4 487 bcopy(ifindex2ifnet, q, n/2);
efda3bd0 488 kfree(ifindex2ifnet, M_IFADDR);
984263bc 489 }
141697b6 490 ifindex2ifnet = q;
984263bc
MD
491 }
492
493 ifindex2ifnet[if_index] = ifp;
494
495 /*
496 * create a Link Level name for this device
497 */
3e4a09e7 498 namelen = strlen(ifp->if_xname);
984263bc
MD
499#define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
500 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
501 socksize = masklen + ifp->if_addrlen;
502#define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
503 if (socksize < sizeof(*sdl))
504 socksize = sizeof(*sdl);
505 socksize = ROUNDUP(socksize);
590b8cd4 506 ifasize = sizeof(struct ifaddr) + 2 * socksize;
b2632176 507 ifa = ifa_create(ifasize, M_WAITOK);
590b8cd4
JH
508 sdl = (struct sockaddr_dl *)(ifa + 1);
509 sdl->sdl_len = socksize;
510 sdl->sdl_family = AF_LINK;
511 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
512 sdl->sdl_nlen = namelen;
513 sdl->sdl_index = ifp->if_index;
514 sdl->sdl_type = ifp->if_type;
141697b6 515 ifp->if_lladdr = ifa;
590b8cd4
JH
516 ifa->ifa_ifp = ifp;
517 ifa->ifa_rtrequest = link_rtrequest;
518 ifa->ifa_addr = (struct sockaddr *)sdl;
519 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
520 ifa->ifa_netmask = (struct sockaddr *)sdl;
521 sdl->sdl_len = masklen;
522 while (namelen != 0)
523 sdl->sdl_data[--namelen] = 0xff;
b2632176 524 ifa_iflink(ifa, ifp, 0 /* Insert head */);
984263bc 525
f2bd8b67 526 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
71fc104f 527 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
f2bd8b67 528
e3e4574a
JS
529 ifq = &ifp->if_snd;
530 ifq->altq_type = 0;
531 ifq->altq_disc = NULL;
532 ifq->altq_flags &= ALTQF_CANTCHANGE;
533 ifq->altq_tbr = NULL;
534 ifq->altq_ifp = ifp;
9db4b353
SZ
535 ifq->altq_started = 0;
536 ifq->altq_prepended = NULL;
537 ALTQ_LOCK_INIT(ifq);
b2f93efe 538 ifq_set_classic(ifq);
4d723e5a 539
9c70fe43 540 if (!SLIST_EMPTY(&domains))
698ac46c
HS
541 if_attachdomain1(ifp);
542
984263bc
MD
543 /* Announce the interface. */
544 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
545}
546
698ac46c
HS
547static void
548if_attachdomain(void *dummy)
549{
550 struct ifnet *ifp;
698ac46c 551
4986965b
JS
552 crit_enter();
553 TAILQ_FOREACH(ifp, &ifnet, if_list)
698ac46c 554 if_attachdomain1(ifp);
4986965b 555 crit_exit();
698ac46c
HS
556}
557SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
558 if_attachdomain, NULL);
559
560static void
561if_attachdomain1(struct ifnet *ifp)
562{
563 struct domain *dp;
698ac46c 564
4986965b 565 crit_enter();
698ac46c
HS
566
567 /* address family dependent data region */
568 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
9c70fe43 569 SLIST_FOREACH(dp, &domains, dom_next)
698ac46c
HS
570 if (dp->dom_ifattach)
571 ifp->if_afdata[dp->dom_family] =
572 (*dp->dom_ifattach)(ifp);
4986965b 573 crit_exit();
698ac46c
HS
574}
575
c727e142
SZ
576/*
577 * Purge all addresses whose type is _not_ AF_LINK
578 */
579void
580if_purgeaddrs_nolink(struct ifnet *ifp)
581{
b2632176
SZ
582 struct ifaddr_container *ifac, *next;
583
584 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
585 ifa_link, next) {
586 struct ifaddr *ifa = ifac->ifa;
c727e142 587
c727e142
SZ
588 /* Leave link ifaddr as it is */
589 if (ifa->ifa_addr->sa_family == AF_LINK)
590 continue;
591#ifdef INET
592 /* XXX: Ugly!! ad hoc just for INET */
593 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
594 struct ifaliasreq ifr;
b2632176
SZ
595#ifdef IFADDR_DEBUG_VERBOSE
596 int i;
597
598 kprintf("purge in4 addr %p: ", ifa);
599 for (i = 0; i < ncpus; ++i)
600 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
601 kprintf("\n");
602#endif
c727e142
SZ
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 && ifa->ifa_addr->sa_family == AF_INET6) {
b2632176
SZ
615#ifdef IFADDR_DEBUG_VERBOSE
616 int i;
617
618 kprintf("purge in6 addr %p: ", ifa);
619 for (i = 0; i < ncpus; ++i)
620 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
621 kprintf("\n");
622#endif
623
c727e142
SZ
624 in6_purgeaddr(ifa);
625 /* ifp_addrhead is already updated */
626 continue;
627 }
628#endif /* INET6 */
b2632176
SZ
629 ifa_ifunlink(ifa, ifp);
630 ifa_destroy(ifa);
c727e142
SZ
631 }
632}
633
984263bc
MD
634/*
635 * Detach an interface, removing it from the
636 * list of "active" interfaces.
637 */
638void
f23061d4 639if_detach(struct ifnet *ifp)
984263bc 640{
984263bc 641 struct radix_node_head *rnh;
984263bc 642 int i;
ecdefdda 643 int cpu, origcpu;
698ac46c 644 struct domain *dp;
984263bc 645
f2bd8b67
JS
646 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
647
984263bc
MD
648 /*
649 * Remove routes and flush queues.
650 */
4986965b 651 crit_enter();
323f031d 652#ifdef DEVICE_POLLING
9c095379
MD
653 if (ifp->if_flags & IFF_POLLING)
654 ether_poll_deregister(ifp);
323f031d 655#endif
984263bc
MD
656 if_down(ifp);
657
4d723e5a
JS
658 if (ifq_is_enabled(&ifp->if_snd))
659 altq_disable(&ifp->if_snd);
660 if (ifq_is_attached(&ifp->if_snd))
661 altq_detach(&ifp->if_snd);
662
984263bc 663 /*
984263bc
MD
664 * Clean up all addresses.
665 */
141697b6 666 ifp->if_lladdr = NULL;
984263bc 667
c727e142 668 if_purgeaddrs_nolink(ifp);
b2632176 669 if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
c727e142
SZ
670 struct ifaddr *ifa;
671
b2632176 672 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
c727e142 673 KASSERT(ifa->ifa_addr->sa_family == AF_LINK,
27eaa4f1 674 ("non-link ifaddr is left on if_addrheads"));
984263bc 675
b2632176
SZ
676 ifa_ifunlink(ifa, ifp);
677 ifa_destroy(ifa);
678 KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]),
27eaa4f1 679 ("there are still ifaddrs left on if_addrheads"));
984263bc
MD
680 }
681
a98eb818
JS
682#ifdef INET
683 /*
684 * Remove all IPv4 kernel structures related to ifp.
685 */
686 in_ifdetach(ifp);
687#endif
688
984263bc
MD
689#ifdef INET6
690 /*
691 * Remove all IPv6 kernel structs related to ifp. This should be done
692 * before removing routing entries below, since IPv6 interface direct
693 * routes are expected to be removed by the IPv6-specific kernel API.
694 * Otherwise, the kernel will detect some inconsistency and bark it.
695 */
696 in6_ifdetach(ifp);
697#endif
698
699 /*
700 * Delete all remaining routes using this interface
701 * Unfortuneatly the only way to do this is to slog through
702 * the entire routing table looking for routes which point
703 * to this interface...oh well...
704 */
ecdefdda
MD
705 origcpu = mycpuid;
706 for (cpu = 0; cpu < ncpus2; cpu++) {
707 lwkt_migratecpu(cpu);
708 for (i = 1; i <= AF_MAX; i++) {
b2632176 709 if ((rnh = rt_tables[cpu][i]) == NULL)
ecdefdda
MD
710 continue;
711 rnh->rnh_walktree(rnh, if_rtdel, ifp);
712 }
984263bc 713 }
ecdefdda 714 lwkt_migratecpu(origcpu);
984263bc
MD
715
716 /* Announce that the interface is gone. */
717 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
71fc104f 718 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
984263bc 719
9c70fe43 720 SLIST_FOREACH(dp, &domains, dom_next)
698ac46c
HS
721 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
722 (*dp->dom_ifdetach)(ifp,
723 ifp->if_afdata[dp->dom_family]);
698ac46c 724
141697b6
JS
725 /*
726 * Remove interface from ifindex2ifp[] and maybe decrement if_index.
727 */
75857e7c 728 ifindex2ifnet[ifp->if_index] = NULL;
141697b6
JS
729 while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
730 if_index--;
75857e7c 731
984263bc 732 TAILQ_REMOVE(&ifnet, ifp, if_link);
b2632176 733 kfree(ifp->if_addrheads, M_IFADDR);
9db4b353 734 kfree(ifp->if_start_nmsg, M_IFADDR);
4986965b 735 crit_exit();
984263bc
MD
736}
737
738/*
739 * Delete Routes for a Network Interface
f23061d4 740 *
984263bc
MD
741 * Called for each routing entry via the rnh->rnh_walktree() call above
742 * to delete all route entries referencing a detaching network interface.
743 *
744 * Arguments:
745 * rn pointer to node in the routing table
746 * arg argument passed to rnh->rnh_walktree() - detaching interface
747 *
748 * Returns:
749 * 0 successful
750 * errno failed - reason indicated
751 *
752 */
753static int
f23061d4 754if_rtdel(struct radix_node *rn, void *arg)
984263bc
MD
755{
756 struct rtentry *rt = (struct rtentry *)rn;
757 struct ifnet *ifp = arg;
758 int err;
759
760 if (rt->rt_ifp == ifp) {
761
762 /*
763 * Protect (sorta) against walktree recursion problems
764 * with cloned routes
765 */
f23061d4 766 if (!(rt->rt_flags & RTF_UP))
984263bc
MD
767 return (0);
768
769 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
770 rt_mask(rt), rt->rt_flags,
771 (struct rtentry **) NULL);
772 if (err) {
773 log(LOG_WARNING, "if_rtdel: error %d\n", err);
774 }
775 }
776
777 return (0);
778}
779
984263bc
MD
780/*
781 * Locate an interface based on a complete address.
782 */
984263bc 783struct ifaddr *
f23061d4 784ifa_ifwithaddr(struct sockaddr *addr)
984263bc 785{
82ed7fc2 786 struct ifnet *ifp;
984263bc 787
b2632176
SZ
788 TAILQ_FOREACH(ifp, &ifnet, if_link) {
789 struct ifaddr_container *ifac;
790
791 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
792 struct ifaddr *ifa = ifac->ifa;
793
794 if (ifa->ifa_addr->sa_family != addr->sa_family)
795 continue;
796 if (sa_equal(addr, ifa->ifa_addr))
797 return (ifa);
798 if ((ifp->if_flags & IFF_BROADCAST) &&
799 ifa->ifa_broadaddr &&
800 /* IPv6 doesn't have broadcast */
801 ifa->ifa_broadaddr->sa_len != 0 &&
802 sa_equal(ifa->ifa_broadaddr, addr))
803 return (ifa);
804 }
984263bc 805 }
b2632176 806 return (NULL);
984263bc
MD
807}
808/*
809 * Locate the point to point interface with a given destination address.
810 */
984263bc 811struct ifaddr *
f23061d4 812ifa_ifwithdstaddr(struct sockaddr *addr)
984263bc 813{
82ed7fc2 814 struct ifnet *ifp;
984263bc 815
b2632176
SZ
816 TAILQ_FOREACH(ifp, &ifnet, if_link) {
817 struct ifaddr_container *ifac;
818
819 if (!(ifp->if_flags & IFF_POINTOPOINT))
820 continue;
821
822 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
823 struct ifaddr *ifa = ifac->ifa;
824
984263bc
MD
825 if (ifa->ifa_addr->sa_family != addr->sa_family)
826 continue;
0c3c561c
JH
827 if (ifa->ifa_dstaddr &&
828 sa_equal(addr, ifa->ifa_dstaddr))
984263bc 829 return (ifa);
b2632176 830 }
984263bc 831 }
b2632176 832 return (NULL);
984263bc
MD
833}
834
835/*
836 * Find an interface on a specific network. If many, choice
837 * is most specific found.
838 */
839struct ifaddr *
f23061d4 840ifa_ifwithnet(struct sockaddr *addr)
984263bc 841{
82ed7fc2 842 struct ifnet *ifp;
b2632176 843 struct ifaddr *ifa_maybe = NULL;
984263bc
MD
844 u_int af = addr->sa_family;
845 char *addr_data = addr->sa_data, *cplim;
846
847 /*
848 * AF_LINK addresses can be looked up directly by their index number,
849 * so do that if we can.
850 */
851 if (af == AF_LINK) {
b2632176 852 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
590b8cd4 853
b2632176
SZ
854 if (sdl->sdl_index && sdl->sdl_index <= if_index)
855 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
984263bc
MD
856 }
857
858 /*
859 * Scan though each interface, looking for ones that have
860 * addresses in this address family.
861 */
862 TAILQ_FOREACH(ifp, &ifnet, if_link) {
b2632176
SZ
863 struct ifaddr_container *ifac;
864
865 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
866 struct ifaddr *ifa = ifac->ifa;
82ed7fc2 867 char *cp, *cp2, *cp3;
984263bc
MD
868
869 if (ifa->ifa_addr->sa_family != af)
870next: continue;
871 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
872 /*
873 * This is a bit broken as it doesn't
874 * take into account that the remote end may
875 * be a single node in the network we are
876 * looking for.
877 * The trouble is that we don't know the
878 * netmask for the remote end.
879 */
0c3c561c
JH
880 if (ifa->ifa_dstaddr != NULL &&
881 sa_equal(addr, ifa->ifa_dstaddr))
f23061d4 882 return (ifa);
984263bc
MD
883 } else {
884 /*
885 * if we have a special address handler,
886 * then use it instead of the generic one.
887 */
f23061d4 888 if (ifa->ifa_claim_addr) {
984263bc
MD
889 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
890 return (ifa);
891 } else {
892 continue;
893 }
894 }
895
896 /*
897 * Scan all the bits in the ifa's address.
898 * If a bit dissagrees with what we are
899 * looking for, mask it with the netmask
900 * to see if it really matters.
901 * (A byte at a time)
902 */
903 if (ifa->ifa_netmask == 0)
904 continue;
905 cp = addr_data;
906 cp2 = ifa->ifa_addr->sa_data;
907 cp3 = ifa->ifa_netmask->sa_data;
590b8cd4
JH
908 cplim = ifa->ifa_netmask->sa_len +
909 (char *)ifa->ifa_netmask;
984263bc
MD
910 while (cp3 < cplim)
911 if ((*cp++ ^ *cp2++) & *cp3++)
912 goto next; /* next address! */
913 /*
914 * If the netmask of what we just found
915 * is more specific than what we had before
916 * (if we had one) then remember the new one
917 * before continuing to search
918 * for an even better one.
919 */
920 if (ifa_maybe == 0 ||
f23061d4
JH
921 rn_refines((char *)ifa->ifa_netmask,
922 (char *)ifa_maybe->ifa_netmask))
984263bc
MD
923 ifa_maybe = ifa;
924 }
925 }
926 }
927 return (ifa_maybe);
928}
929
930/*
931 * Find an interface address specific to an interface best matching
932 * a given address.
933 */
934struct ifaddr *
f23061d4 935ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
984263bc 936{
b2632176 937 struct ifaddr_container *ifac;
82ed7fc2
RG
938 char *cp, *cp2, *cp3;
939 char *cplim;
984263bc
MD
940 struct ifaddr *ifa_maybe = 0;
941 u_int af = addr->sa_family;
942
943 if (af >= AF_MAX)
944 return (0);
b2632176
SZ
945 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
946 struct ifaddr *ifa = ifac->ifa;
947
984263bc
MD
948 if (ifa->ifa_addr->sa_family != af)
949 continue;
950 if (ifa_maybe == 0)
951 ifa_maybe = ifa;
0c3c561c
JH
952 if (ifa->ifa_netmask == NULL) {
953 if (sa_equal(addr, ifa->ifa_addr) ||
954 (ifa->ifa_dstaddr != NULL &&
955 sa_equal(addr, ifa->ifa_dstaddr)))
984263bc
MD
956 return (ifa);
957 continue;
958 }
959 if (ifp->if_flags & IFF_POINTOPOINT) {
0c3c561c 960 if (sa_equal(addr, ifa->ifa_dstaddr))
984263bc
MD
961 return (ifa);
962 } else {
963 cp = addr->sa_data;
964 cp2 = ifa->ifa_addr->sa_data;
965 cp3 = ifa->ifa_netmask->sa_data;
966 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
967 for (; cp3 < cplim; cp3++)
968 if ((*cp++ ^ *cp2++) & *cp3)
969 break;
970 if (cp3 == cplim)
971 return (ifa);
972 }
973 }
974 return (ifa_maybe);
975}
976
984263bc
MD
977/*
978 * Default action when installing a route with a Link Level gateway.
979 * Lookup an appropriate real ifa to point to.
980 * This should be moved to /sys/net/link.c eventually.
981 */
982static void
f23061d4 983link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
984263bc 984{
82ed7fc2 985 struct ifaddr *ifa;
984263bc
MD
986 struct sockaddr *dst;
987 struct ifnet *ifp;
988
f23061d4
JH
989 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
990 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
984263bc
MD
991 return;
992 ifa = ifaof_ifpforaddr(dst, ifp);
f23061d4 993 if (ifa != NULL) {
984263bc 994 IFAFREE(rt->rt_ifa);
f23061d4 995 IFAREF(ifa);
984263bc 996 rt->rt_ifa = ifa;
984263bc
MD
997 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
998 ifa->ifa_rtrequest(cmd, rt, info);
999 }
1000}
1001
1002/*
1003 * Mark an interface down and notify protocols of
1004 * the transition.
1005 * NOTE: must be called at splnet or eqivalent.
1006 */
1007void
f23061d4 1008if_unroute(struct ifnet *ifp, int flag, int fam)
984263bc 1009{
b2632176 1010 struct ifaddr_container *ifac;
984263bc
MD
1011
1012 ifp->if_flags &= ~flag;
1013 getmicrotime(&ifp->if_lastchange);
b2632176
SZ
1014 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1015 struct ifaddr *ifa = ifac->ifa;
1016
984263bc 1017 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
91be174d 1018 kpfctlinput(PRC_IFDOWN, ifa->ifa_addr);
b2632176 1019 }
4d723e5a 1020 ifq_purge(&ifp->if_snd);
984263bc
MD
1021 rt_ifmsg(ifp);
1022}
1023
1024/*
1025 * Mark an interface up and notify protocols of
1026 * the transition.
1027 * NOTE: must be called at splnet or eqivalent.
1028 */
1029void
f23061d4 1030if_route(struct ifnet *ifp, int flag, int fam)
984263bc 1031{
b2632176 1032 struct ifaddr_container *ifac;
984263bc 1033
68633c85 1034 ifq_purge(&ifp->if_snd);
984263bc
MD
1035 ifp->if_flags |= flag;
1036 getmicrotime(&ifp->if_lastchange);
b2632176
SZ
1037 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1038 struct ifaddr *ifa = ifac->ifa;
1039
984263bc 1040 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
91be174d 1041 kpfctlinput(PRC_IFUP, ifa->ifa_addr);
b2632176 1042 }
984263bc
MD
1043 rt_ifmsg(ifp);
1044#ifdef INET6
1045 in6_if_up(ifp);
1046#endif
1047}
1048
1049/*
5c703385
MD
1050 * Mark an interface down and notify protocols of the transition. An
1051 * interface going down is also considered to be a synchronizing event.
1052 * We must ensure that all packet processing related to the interface
1053 * has completed before we return so e.g. the caller can free the ifnet
1054 * structure that the mbufs may be referencing.
1055 *
984263bc
MD
1056 * NOTE: must be called at splnet or eqivalent.
1057 */
1058void
f23061d4 1059if_down(struct ifnet *ifp)
984263bc 1060{
984263bc 1061 if_unroute(ifp, IFF_UP, AF_UNSPEC);
5c703385 1062 netmsg_service_sync();
984263bc
MD
1063}
1064
1065/*
1066 * Mark an interface up and notify protocols of
1067 * the transition.
1068 * NOTE: must be called at splnet or eqivalent.
1069 */
1070void
f23061d4 1071if_up(struct ifnet *ifp)
984263bc 1072{
984263bc
MD
1073 if_route(ifp, IFF_UP, AF_UNSPEC);
1074}
1075
6de83abe
SZ
1076/*
1077 * Process a link state change.
1078 * NOTE: must be called at splsoftnet or equivalent.
1079 */
1080void
1081if_link_state_change(struct ifnet *ifp)
1082{
71fc104f
HT
1083 int link_state = ifp->if_link_state;
1084
6de83abe 1085 rt_ifmsg(ifp);
71fc104f
HT
1086 devctl_notify("IFNET", ifp->if_xname,
1087 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
6de83abe
SZ
1088}
1089
984263bc
MD
1090/*
1091 * Handle interface watchdog timer routines. Called
1092 * from softclock, we decrement timers (if set) and
1093 * call the appropriate interface routine on expiration.
1094 */
1095static void
f23061d4 1096if_slowtimo(void *arg)
984263bc 1097{
82ed7fc2 1098 struct ifnet *ifp;
4986965b
JS
1099
1100 crit_enter();
984263bc
MD
1101
1102 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1103 if (ifp->if_timer == 0 || --ifp->if_timer)
1104 continue;
78195a76
MD
1105 if (ifp->if_watchdog) {
1106 if (lwkt_serialize_try(ifp->if_serializer)) {
1107 (*ifp->if_watchdog)(ifp);
1108 lwkt_serialize_exit(ifp->if_serializer);
1109 } else {
1110 /* try again next timeout */
1111 ++ifp->if_timer;
1112 }
1113 }
984263bc 1114 }
4986965b
JS
1115
1116 crit_exit();
1117
abbb44bb 1118 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
984263bc
MD
1119}
1120
1121/*
1122 * Map interface name to
1123 * interface structure pointer.
1124 */
1125struct ifnet *
1126ifunit(const char *name)
1127{
984263bc 1128 struct ifnet *ifp;
984263bc 1129
984263bc 1130 /*
3e4a09e7 1131 * Search all the interfaces for this name/number
984263bc 1132 */
3e4a09e7 1133
984263bc 1134 TAILQ_FOREACH(ifp, &ifnet, if_link) {
3e4a09e7 1135 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
984263bc
MD
1136 break;
1137 }
1138 return (ifp);
1139}
1140
1141
1142/*
1143 * Map interface name in a sockaddr_dl to
1144 * interface structure pointer.
1145 */
1146struct ifnet *
f23061d4 1147if_withname(struct sockaddr *sa)
984263bc
MD
1148{
1149 char ifname[IFNAMSIZ+1];
1150 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
1151
1152 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
1153 (sdl->sdl_nlen > IFNAMSIZ) )
1154 return NULL;
1155
1156 /*
1157 * ifunit wants a null-terminated name. It may not be null-terminated
1158 * in the sockaddr. We don't want to change the caller's sockaddr,
1159 * and there might not be room to put the trailing null anyway, so we
1160 * make a local copy that we know we can null terminate safely.
1161 */
1162
1163 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
1164 ifname[sdl->sdl_nlen] = '\0';
1165 return ifunit(ifname);
1166}
1167
1168
1169/*
1170 * Interface ioctls.
1171 */
1172int
87de5057 1173ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
984263bc 1174{
41c20dac
MD
1175 struct ifnet *ifp;
1176 struct ifreq *ifr;
984263bc
MD
1177 struct ifstat *ifs;
1178 int error;
1179 short oif_flags;
1180 int new_flags;
1fdf0954
HP
1181 size_t namelen, onamelen;
1182 char new_name[IFNAMSIZ];
1183 struct ifaddr *ifa;
1184 struct sockaddr_dl *sdl;
984263bc
MD
1185
1186 switch (cmd) {
1187
1188 case SIOCGIFCONF:
1189 case OSIOCGIFCONF:
87de5057 1190 return (ifconf(cmd, data, cred));
984263bc
MD
1191 }
1192 ifr = (struct ifreq *)data;
1193
1194 switch (cmd) {
1195 case SIOCIFCREATE:
1196 case SIOCIFDESTROY:
87de5057 1197 if ((error = suser_cred(cred, 0)) != 0)
984263bc
MD
1198 return (error);
1199 return ((cmd == SIOCIFCREATE) ?
1200 if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
1201 if_clone_destroy(ifr->ifr_name));
f23061d4 1202
984263bc
MD
1203 case SIOCIFGCLONERS:
1204 return (if_clone_list((struct if_clonereq *)data));
1205 }
1206
1207 ifp = ifunit(ifr->ifr_name);
1208 if (ifp == 0)
1209 return (ENXIO);
1210 switch (cmd) {
1211
1212 case SIOCGIFFLAGS:
1213 ifr->ifr_flags = ifp->if_flags;
46f25451 1214 ifr->ifr_flagshigh = ifp->if_flags >> 16;
984263bc
MD
1215 break;
1216
1217 case SIOCGIFCAP:
1218 ifr->ifr_reqcap = ifp->if_capabilities;
1219 ifr->ifr_curcap = ifp->if_capenable;
1220 break;
1221
1222 case SIOCGIFMETRIC:
1223 ifr->ifr_metric = ifp->if_metric;
1224 break;
1225
1226 case SIOCGIFMTU:
1227 ifr->ifr_mtu = ifp->if_mtu;
1228 break;
1229
1230 case SIOCGIFPHYS:
1231 ifr->ifr_phys = ifp->if_physical;
1232 break;
1233
1630efc5
SZ
1234 case SIOCGIFPOLLCPU:
1235#ifdef DEVICE_POLLING
1236 ifr->ifr_pollcpu = ifp->if_poll_cpuid;
1237#else
1238 ifr->ifr_pollcpu = -1;
1239#endif
1240 break;
1241
1242 case SIOCSIFPOLLCPU:
1243#ifdef DEVICE_POLLING
1244 if ((ifp->if_flags & IFF_POLLING) == 0)
1245 ether_pollcpu_register(ifp, ifr->ifr_pollcpu);
1246#endif
1247 break;
1248
984263bc 1249 case SIOCSIFFLAGS:
87de5057 1250 error = suser_cred(cred, 0);
984263bc
MD
1251 if (error)
1252 return (error);
1253 new_flags = (ifr->ifr_flags & 0xffff) |
1254 (ifr->ifr_flagshigh << 16);
1255 if (ifp->if_flags & IFF_SMART) {
1256 /* Smart drivers twiddle their own routes */
1257 } else if (ifp->if_flags & IFF_UP &&
1258 (new_flags & IFF_UP) == 0) {
4986965b 1259 crit_enter();
984263bc 1260 if_down(ifp);
4986965b 1261 crit_exit();
984263bc
MD
1262 } else if (new_flags & IFF_UP &&
1263 (ifp->if_flags & IFF_UP) == 0) {
4986965b 1264 crit_enter();
984263bc 1265 if_up(ifp);
4986965b 1266 crit_exit();
984263bc 1267 }
9c095379
MD
1268
1269#ifdef DEVICE_POLLING
1270 if ((new_flags ^ ifp->if_flags) & IFF_POLLING) {
1271 if (new_flags & IFF_POLLING) {
1272 ether_poll_register(ifp);
1273 } else {
1274 ether_poll_deregister(ifp);
1275 }
1276 }
1277#endif
1278
984263bc
MD
1279 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1280 (new_flags &~ IFF_CANTCHANGE);
984263bc
MD
1281 if (new_flags & IFF_PPROMISC) {
1282 /* Permanently promiscuous mode requested */
1283 ifp->if_flags |= IFF_PROMISC;
1284 } else if (ifp->if_pcount == 0) {
1285 ifp->if_flags &= ~IFF_PROMISC;
1286 }
78195a76
MD
1287 if (ifp->if_ioctl) {
1288 lwkt_serialize_enter(ifp->if_serializer);
87de5057 1289 ifp->if_ioctl(ifp, cmd, data, cred);
78195a76
MD
1290 lwkt_serialize_exit(ifp->if_serializer);
1291 }
984263bc
MD
1292 getmicrotime(&ifp->if_lastchange);
1293 break;
1294
1295 case SIOCSIFCAP:
87de5057 1296 error = suser_cred(cred, 0);
984263bc
MD
1297 if (error)
1298 return (error);
1299 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1300 return (EINVAL);
78195a76 1301 lwkt_serialize_enter(ifp->if_serializer);
87de5057 1302 ifp->if_ioctl(ifp, cmd, data, cred);
78195a76 1303 lwkt_serialize_exit(ifp->if_serializer);
984263bc
MD
1304 break;
1305
f23061d4 1306 case SIOCSIFNAME:
87de5057 1307 error = suser_cred(cred, 0);
f23061d4
JH
1308 if (error != 0)
1309 return (error);
1310 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1311 if (error != 0)
1312 return (error);
1fdf0954
HP
1313 if (new_name[0] == '\0')
1314 return (EINVAL);
f23061d4
JH
1315 if (ifunit(new_name) != NULL)
1316 return (EEXIST);
f2bd8b67
JS
1317
1318 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
f23061d4
JH
1319
1320 /* Announce the departure of the interface. */
1321 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1322
1323 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
b2632176 1324 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
f23061d4
JH
1325 /* XXX IFA_LOCK(ifa); */
1326 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1327 namelen = strlen(new_name);
1328 onamelen = sdl->sdl_nlen;
1329 /*
1330 * Move the address if needed. This is safe because we
1331 * allocate space for a name of length IFNAMSIZ when we
1332 * create this in if_attach().
1333 */
1334 if (namelen != onamelen) {
1335 bcopy(sdl->sdl_data + onamelen,
1336 sdl->sdl_data + namelen, sdl->sdl_alen);
1337 }
1338 bcopy(new_name, sdl->sdl_data, namelen);
1339 sdl->sdl_nlen = namelen;
1340 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1341 bzero(sdl->sdl_data, onamelen);
1342 while (namelen != 0)
1343 sdl->sdl_data[--namelen] = 0xff;
1344 /* XXX IFA_UNLOCK(ifa) */
f2bd8b67
JS
1345
1346 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
f23061d4
JH
1347
1348 /* Announce the return of the interface. */
1349 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1350 break;
1fdf0954 1351
984263bc 1352 case SIOCSIFMETRIC:
87de5057 1353 error = suser_cred(cred, 0);
984263bc
MD
1354 if (error)
1355 return (error);
1356 ifp->if_metric = ifr->ifr_metric;
1357 getmicrotime(&ifp->if_lastchange);
1358 break;
1359
1360 case SIOCSIFPHYS:
87de5057 1361 error = suser_cred(cred, 0);
984263bc
MD
1362 if (error)
1363 return error;
1364 if (!ifp->if_ioctl)
1365 return EOPNOTSUPP;
78195a76 1366 lwkt_serialize_enter(ifp->if_serializer);
87de5057 1367 error = ifp->if_ioctl(ifp, cmd, data, cred);
78195a76 1368 lwkt_serialize_exit(ifp->if_serializer);
984263bc
MD
1369 if (error == 0)
1370 getmicrotime(&ifp->if_lastchange);
f23061d4 1371 return (error);
984263bc
MD
1372
1373 case SIOCSIFMTU:
1374 {
1375 u_long oldmtu = ifp->if_mtu;
1376
87de5057 1377 error = suser_cred(cred, 0);
984263bc
MD
1378 if (error)
1379 return (error);
1380 if (ifp->if_ioctl == NULL)
1381 return (EOPNOTSUPP);
1382 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1383 return (EINVAL);
78195a76 1384 lwkt_serialize_enter(ifp->if_serializer);
87de5057 1385 error = ifp->if_ioctl(ifp, cmd, data, cred);
78195a76 1386 lwkt_serialize_exit(ifp->if_serializer);
984263bc
MD
1387 if (error == 0) {
1388 getmicrotime(&ifp->if_lastchange);
1389 rt_ifmsg(ifp);
1390 }
1391 /*
1392 * If the link MTU changed, do network layer specific procedure.
1393 */
1394 if (ifp->if_mtu != oldmtu) {
1395#ifdef INET6
1396 nd6_setmtu(ifp);
1397#endif
1398 }
1399 return (error);
1400 }
1401
1402 case SIOCADDMULTI:
1403 case SIOCDELMULTI:
87de5057 1404 error = suser_cred(cred, 0);
984263bc
MD
1405 if (error)
1406 return (error);
1407
1408 /* Don't allow group membership on non-multicast interfaces. */
1409 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1410 return EOPNOTSUPP;
1411
1412 /* Don't let users screw up protocols' entries. */
1413 if (ifr->ifr_addr.sa_family != AF_LINK)
1414 return EINVAL;
1415
1416 if (cmd == SIOCADDMULTI) {
1417 struct ifmultiaddr *ifma;
1418 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1419 } else {
1420 error = if_delmulti(ifp, &ifr->ifr_addr);
1421 }
1422 if (error == 0)
1423 getmicrotime(&ifp->if_lastchange);
1424 return error;
1425
1426 case SIOCSIFPHYADDR:
1427 case SIOCDIFPHYADDR:
1428#ifdef INET6
1429 case SIOCSIFPHYADDR_IN6:
1430#endif
1431 case SIOCSLIFPHYADDR:
1432 case SIOCSIFMEDIA:
1433 case SIOCSIFGENERIC:
87de5057 1434 error = suser_cred(cred, 0);
984263bc
MD
1435 if (error)
1436 return (error);
1437 if (ifp->if_ioctl == 0)
1438 return (EOPNOTSUPP);
78195a76 1439 lwkt_serialize_enter(ifp->if_serializer);
87de5057 1440 error = ifp->if_ioctl(ifp, cmd, data, cred);
78195a76 1441 lwkt_serialize_exit(ifp->if_serializer);
984263bc
MD
1442 if (error == 0)
1443 getmicrotime(&ifp->if_lastchange);
1444 return error;
1445
1446 case SIOCGIFSTATUS:
1447 ifs = (struct ifstat *)data;
1448 ifs->ascii[0] = '\0';
f23061d4 1449
984263bc
MD
1450 case SIOCGIFPSRCADDR:
1451 case SIOCGIFPDSTADDR:
1452 case SIOCGLIFPHYADDR:
1453 case SIOCGIFMEDIA:
1454 case SIOCGIFGENERIC:
78195a76 1455 if (ifp->if_ioctl == NULL)
984263bc 1456 return (EOPNOTSUPP);
78195a76 1457 lwkt_serialize_enter(ifp->if_serializer);
87de5057 1458 error = ifp->if_ioctl(ifp, cmd, data, cred);
78195a76
MD
1459 lwkt_serialize_exit(ifp->if_serializer);
1460 return (error);
984263bc
MD
1461
1462 case SIOCSIFLLADDR:
87de5057 1463 error = suser_cred(cred, 0);
984263bc
MD
1464 if (error)
1465 return (error);
1466 return if_setlladdr(ifp,
1467 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1468
1469 default:
1470 oif_flags = ifp->if_flags;
1471 if (so->so_proto == 0)
1472 return (EOPNOTSUPP);
1473#ifndef COMPAT_43
87de5057 1474 error = so_pru_control(so, cmd, data, ifp);
984263bc
MD
1475#else
1476 {
1477 int ocmd = cmd;
1478
1479 switch (cmd) {
1480
1481 case SIOCSIFDSTADDR:
1482 case SIOCSIFADDR:
1483 case SIOCSIFBRDADDR:
1484 case SIOCSIFNETMASK:
1485#if BYTE_ORDER != BIG_ENDIAN
1486 if (ifr->ifr_addr.sa_family == 0 &&
1487 ifr->ifr_addr.sa_len < 16) {
1488 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1489 ifr->ifr_addr.sa_len = 16;
1490 }
1491#else
1492 if (ifr->ifr_addr.sa_len == 0)
1493 ifr->ifr_addr.sa_len = 16;
1494#endif
1495 break;
1496
1497 case OSIOCGIFADDR:
1498 cmd = SIOCGIFADDR;
1499 break;
1500
1501 case OSIOCGIFDSTADDR:
1502 cmd = SIOCGIFDSTADDR;
1503 break;
1504
1505 case OSIOCGIFBRDADDR:
1506 cmd = SIOCGIFBRDADDR;
1507 break;
1508
1509 case OSIOCGIFNETMASK:
1510 cmd = SIOCGIFNETMASK;
1511 }
87de5057 1512 error = so_pru_control(so, cmd, data, ifp);
984263bc
MD
1513 switch (ocmd) {
1514
1515 case OSIOCGIFADDR:
1516 case OSIOCGIFDSTADDR:
1517 case OSIOCGIFBRDADDR:
1518 case OSIOCGIFNETMASK:
1519 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1520
1521 }
1522 }
1523#endif /* COMPAT_43 */
1524
1525 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1526#ifdef INET6
1527 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1528 if (ifp->if_flags & IFF_UP) {
4986965b 1529 crit_enter();
984263bc 1530 in6_if_up(ifp);
4986965b 1531 crit_exit();
984263bc
MD
1532 }
1533#endif
1534 }
1535 return (error);
1536
1537 }
1538 return (0);
1539}
1540
1541/*
1542 * Set/clear promiscuous mode on interface ifp based on the truth value
1543 * of pswitch. The calls are reference counted so that only the first
1544 * "on" request actually has an effect, as does the final "off" request.
1545 * Results are undefined if the "off" and "on" requests are not matched.
1546 */
1547int
f23061d4 1548ifpromisc(struct ifnet *ifp, int pswitch)
984263bc
MD
1549{
1550 struct ifreq ifr;
1551 int error;
1552 int oldflags;
1553
1554 oldflags = ifp->if_flags;
46f25451 1555 if (ifp->if_flags & IFF_PPROMISC) {
984263bc
MD
1556 /* Do nothing if device is in permanently promiscuous mode */
1557 ifp->if_pcount += pswitch ? 1 : -1;
1558 return (0);
1559 }
1560 if (pswitch) {
1561 /*
1562 * If the device is not configured up, we cannot put it in
1563 * promiscuous mode.
1564 */
1565 if ((ifp->if_flags & IFF_UP) == 0)
1566 return (ENETDOWN);
1567 if (ifp->if_pcount++ != 0)
1568 return (0);
1569 ifp->if_flags |= IFF_PROMISC;
3e4a09e7
MD
1570 log(LOG_INFO, "%s: promiscuous mode enabled\n",
1571 ifp->if_xname);
984263bc
MD
1572 } else {
1573 if (--ifp->if_pcount > 0)
1574 return (0);
1575 ifp->if_flags &= ~IFF_PROMISC;
3e4a09e7
MD
1576 log(LOG_INFO, "%s: promiscuous mode disabled\n",
1577 ifp->if_xname);
984263bc
MD
1578 }
1579 ifr.ifr_flags = ifp->if_flags;
46f25451 1580 ifr.ifr_flagshigh = ifp->if_flags >> 16;
78195a76
MD
1581 lwkt_serialize_enter(ifp->if_serializer);
1582 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
bd4539cc 1583 (struct ucred *)NULL);
78195a76 1584 lwkt_serialize_exit(ifp->if_serializer);
984263bc
MD
1585 if (error == 0)
1586 rt_ifmsg(ifp);
1587 else
1588 ifp->if_flags = oldflags;
1589 return error;
1590}
1591
1592/*
1593 * Return interface configuration
1594 * of system. List may be used
1595 * in later ioctl's (above) to get
1596 * other information.
1597 */
984263bc 1598static int
87de5057 1599ifconf(u_long cmd, caddr_t data, struct ucred *cred)
984263bc 1600{
41c20dac
MD
1601 struct ifconf *ifc = (struct ifconf *)data;
1602 struct ifnet *ifp;
984263bc
MD
1603 struct sockaddr *sa;
1604 struct ifreq ifr, *ifrp;
1605 int space = ifc->ifc_len, error = 0;
1606
1607 ifrp = ifc->ifc_req;
1608 TAILQ_FOREACH(ifp, &ifnet, if_link) {
b2632176 1609 struct ifaddr_container *ifac;
3e4a09e7 1610 int addrs;
984263bc 1611
f23061d4 1612 if (space <= sizeof ifr)
984263bc 1613 break;
623c059e
JS
1614
1615 /*
95f018e8
MD
1616 * Zero the stack declared structure first to prevent
1617 * memory disclosure.
623c059e 1618 */
95f018e8 1619 bzero(&ifr, sizeof(ifr));
3e4a09e7
MD
1620 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
1621 >= sizeof(ifr.ifr_name)) {
984263bc
MD
1622 error = ENAMETOOLONG;
1623 break;
984263bc
MD
1624 }
1625
1626 addrs = 0;
b2632176
SZ
1627 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1628 struct ifaddr *ifa = ifac->ifa;
1629
f23061d4 1630 if (space <= sizeof ifr)
984263bc
MD
1631 break;
1632 sa = ifa->ifa_addr;
87de5057
MD
1633 if (cred->cr_prison &&
1634 prison_if(cred, sa))
984263bc
MD
1635 continue;
1636 addrs++;
1637#ifdef COMPAT_43
1638 if (cmd == OSIOCGIFCONF) {
1639 struct osockaddr *osa =
1640 (struct osockaddr *)&ifr.ifr_addr;
1641 ifr.ifr_addr = *sa;
1642 osa->sa_family = sa->sa_family;
f23061d4 1643 error = copyout(&ifr, ifrp, sizeof ifr);
984263bc
MD
1644 ifrp++;
1645 } else
1646#endif
1647 if (sa->sa_len <= sizeof(*sa)) {
1648 ifr.ifr_addr = *sa;
f23061d4 1649 error = copyout(&ifr, ifrp, sizeof ifr);
984263bc
MD
1650 ifrp++;
1651 } else {
f23061d4 1652 if (space < (sizeof ifr) + sa->sa_len -
984263bc
MD
1653 sizeof(*sa))
1654 break;
1655 space -= sa->sa_len - sizeof(*sa);
f23061d4
JH
1656 error = copyout(&ifr, ifrp,
1657 sizeof ifr.ifr_name);
984263bc 1658 if (error == 0)
f23061d4
JH
1659 error = copyout(sa, &ifrp->ifr_addr,
1660 sa->sa_len);
984263bc
MD
1661 ifrp = (struct ifreq *)
1662 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1663 }
1664 if (error)
1665 break;
f23061d4 1666 space -= sizeof ifr;
984263bc
MD
1667 }
1668 if (error)
1669 break;
1670 if (!addrs) {
f23061d4
JH
1671 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
1672 error = copyout(&ifr, ifrp, sizeof ifr);
984263bc
MD
1673 if (error)
1674 break;
f23061d4 1675 space -= sizeof ifr;
984263bc
MD
1676 ifrp++;
1677 }
1678 }
1679 ifc->ifc_len -= space;
1680 return (error);
1681}
1682
1683/*
1684 * Just like if_promisc(), but for all-multicast-reception mode.
1685 */
1686int
f23061d4 1687if_allmulti(struct ifnet *ifp, int onswitch)
984263bc
MD
1688{
1689 int error = 0;
984263bc
MD
1690 struct ifreq ifr;
1691
4986965b
JS
1692 crit_enter();
1693
984263bc
MD
1694 if (onswitch) {
1695 if (ifp->if_amcount++ == 0) {
1696 ifp->if_flags |= IFF_ALLMULTI;
1697 ifr.ifr_flags = ifp->if_flags;
46f25451 1698 ifr.ifr_flagshigh = ifp->if_flags >> 16;
78195a76 1699 lwkt_serialize_enter(ifp->if_serializer);
bd4539cc
JH
1700 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1701 (struct ucred *)NULL);
78195a76 1702 lwkt_serialize_exit(ifp->if_serializer);
984263bc
MD
1703 }
1704 } else {
1705 if (ifp->if_amcount > 1) {
1706 ifp->if_amcount--;
1707 } else {
1708 ifp->if_amcount = 0;
1709 ifp->if_flags &= ~IFF_ALLMULTI;
1710 ifr.ifr_flags = ifp->if_flags;
46f25451 1711 ifr.ifr_flagshigh = ifp->if_flags >> 16;
78195a76 1712 lwkt_serialize_enter(ifp->if_serializer);
bd4539cc
JH
1713 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1714 (struct ucred *)NULL);
78195a76 1715 lwkt_serialize_exit(ifp->if_serializer);
984263bc
MD
1716 }
1717 }
4986965b
JS
1718
1719 crit_exit();
984263bc
MD
1720
1721 if (error == 0)
1722 rt_ifmsg(ifp);
1723 return error;
1724}
1725
1726/*
1727 * Add a multicast listenership to the interface in question.
1728 * The link layer provides a routine which converts
1729 */
1730int
f23061d4
JH
1731if_addmulti(
1732 struct ifnet *ifp, /* interface to manipulate */
1733 struct sockaddr *sa, /* address to add */
1734 struct ifmultiaddr **retifma)
984263bc
MD
1735{
1736 struct sockaddr *llsa, *dupsa;
4986965b 1737 int error;
984263bc
MD
1738 struct ifmultiaddr *ifma;
1739
1740 /*
1741 * If the matching multicast address already exists
1742 * then don't add a new one, just add a reference
1743 */
1744 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
0c3c561c 1745 if (sa_equal(sa, ifma->ifma_addr)) {
984263bc
MD
1746 ifma->ifma_refcount++;
1747 if (retifma)
1748 *retifma = ifma;
1749 return 0;
1750 }
1751 }
1752
1753 /*
1754 * Give the link layer a chance to accept/reject it, and also
1755 * find out which AF_LINK address this maps to, if it isn't one
1756 * already.
1757 */
1758 if (ifp->if_resolvemulti) {
78195a76 1759 lwkt_serialize_enter(ifp->if_serializer);
984263bc 1760 error = ifp->if_resolvemulti(ifp, &llsa, sa);
78195a76
MD
1761 lwkt_serialize_exit(ifp->if_serializer);
1762 if (error)
1763 return error;
984263bc
MD
1764 } else {
1765 llsa = 0;
1766 }
1767
1768 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1769 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1770 bcopy(sa, dupsa, sa->sa_len);
1771
1772 ifma->ifma_addr = dupsa;
1773 ifma->ifma_lladdr = llsa;
1774 ifma->ifma_ifp = ifp;
1775 ifma->ifma_refcount = 1;
1776 ifma->ifma_protospec = 0;
1777 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1778
1779 /*
1780 * Some network interfaces can scan the address list at
1781 * interrupt time; lock them out.
1782 */
4986965b 1783 crit_enter();
984263bc 1784 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
4986965b 1785 crit_exit();
984263bc
MD
1786 *retifma = ifma;
1787
1788 if (llsa != 0) {
1789 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
0c3c561c 1790 if (sa_equal(ifma->ifma_addr, llsa))
984263bc
MD
1791 break;
1792 }
1793 if (ifma) {
1794 ifma->ifma_refcount++;
1795 } else {
1796 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1797 M_IFMADDR, M_WAITOK);
1798 MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1799 M_IFMADDR, M_WAITOK);
1800 bcopy(llsa, dupsa, llsa->sa_len);
1801 ifma->ifma_addr = dupsa;
1802 ifma->ifma_ifp = ifp;
1803 ifma->ifma_refcount = 1;
4986965b 1804 crit_enter();
984263bc 1805 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
4986965b 1806 crit_exit();
984263bc
MD
1807 }
1808 }
1809 /*
1810 * We are certain we have added something, so call down to the
1811 * interface to let them know about it.
1812 */
4986965b 1813 crit_enter();
78195a76 1814 lwkt_serialize_enter(ifp->if_serializer);
bd4539cc 1815 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, (struct ucred *)NULL);
78195a76 1816 lwkt_serialize_exit(ifp->if_serializer);
4986965b 1817 crit_exit();
984263bc
MD
1818
1819 return 0;
1820}
1821
1822/*
1823 * Remove a reference to a multicast address on this interface. Yell
1824 * if the request does not match an existing membership.
1825 */
1826int
f23061d4 1827if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
984263bc
MD
1828{
1829 struct ifmultiaddr *ifma;
984263bc
MD
1830
1831 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
0c3c561c 1832 if (sa_equal(sa, ifma->ifma_addr))
984263bc
MD
1833 break;
1834 if (ifma == 0)
1835 return ENOENT;
1836
1837 if (ifma->ifma_refcount > 1) {
1838 ifma->ifma_refcount--;
1839 return 0;
1840 }
1841
1842 rt_newmaddrmsg(RTM_DELMADDR, ifma);
1843 sa = ifma->ifma_lladdr;
4986965b 1844 crit_enter();
984263bc
MD
1845 LIST_REMOVE(ifma, ifma_link);
1846 /*
1847 * Make sure the interface driver is notified
1848 * in the case of a link layer mcast group being left.
1849 */
78195a76
MD
1850 if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0) {
1851 lwkt_serialize_enter(ifp->if_serializer);
bd4539cc 1852 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL);
78195a76
MD
1853 lwkt_serialize_exit(ifp->if_serializer);
1854 }
4986965b 1855 crit_exit();
efda3bd0
MD
1856 kfree(ifma->ifma_addr, M_IFMADDR);
1857 kfree(ifma, M_IFMADDR);
984263bc
MD
1858 if (sa == 0)
1859 return 0;
1860
1861 /*
1862 * Now look for the link-layer address which corresponds to
1863 * this network address. It had been squirreled away in
1864 * ifma->ifma_lladdr for this purpose (so we don't have
1865 * to call ifp->if_resolvemulti() again), and we saved that
1866 * value in sa above. If some nasty deleted the
1867 * link-layer address out from underneath us, we can deal because
1868 * the address we stored was is not the same as the one which was
1869 * in the record for the link-layer address. (So we don't complain
1870 * in that case.)
1871 */
1872 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
0c3c561c 1873 if (sa_equal(sa, ifma->ifma_addr))
984263bc
MD
1874 break;
1875 if (ifma == 0)
1876 return 0;
1877
1878 if (ifma->ifma_refcount > 1) {
1879 ifma->ifma_refcount--;
1880 return 0;
1881 }
1882
4986965b 1883 crit_enter();
78195a76 1884 lwkt_serialize_enter(ifp->if_serializer);
984263bc 1885 LIST_REMOVE(ifma, ifma_link);
bd4539cc 1886 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL);
78195a76 1887 lwkt_serialize_exit(ifp->if_serializer);
4986965b 1888 crit_exit();
efda3bd0
MD
1889 kfree(ifma->ifma_addr, M_IFMADDR);
1890 kfree(sa, M_IFMADDR);
1891 kfree(ifma, M_IFMADDR);
984263bc
MD
1892
1893 return 0;
1894}
1895
1896/*
1897 * Set the link layer address on an interface.
1898 *
1899 * At this time we only support certain types of interfaces,
1900 * and we don't allow the length of the address to change.
1901 */
1902int
1903if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
1904{
1905 struct sockaddr_dl *sdl;
984263bc
MD
1906 struct ifreq ifr;
1907
f2682cb9 1908 sdl = IF_LLSOCKADDR(ifp);
984263bc
MD
1909 if (sdl == NULL)
1910 return (EINVAL);
1911 if (len != sdl->sdl_alen) /* don't allow length to change */
1912 return (EINVAL);
1913 switch (ifp->if_type) {
1914 case IFT_ETHER: /* these types use struct arpcom */
984263bc 1915 case IFT_XETHER:
984263bc
MD
1916 case IFT_L2VLAN:
1917 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
984263bc
MD
1918 bcopy(lladdr, LLADDR(sdl), len);
1919 break;
1920 default:
1921 return (ENODEV);
1922 }
1923 /*
1924 * If the interface is already up, we need
1925 * to re-init it in order to reprogram its
1926 * address filter.
1927 */
78195a76 1928 lwkt_serialize_enter(ifp->if_serializer);
984263bc 1929 if ((ifp->if_flags & IFF_UP) != 0) {
b2632176
SZ
1930 struct ifaddr_container *ifac;
1931
984263bc
MD
1932 ifp->if_flags &= ~IFF_UP;
1933 ifr.ifr_flags = ifp->if_flags;
46f25451 1934 ifr.ifr_flagshigh = ifp->if_flags >> 16;
78195a76
MD
1935 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1936 (struct ucred *)NULL);
984263bc
MD
1937 ifp->if_flags |= IFF_UP;
1938 ifr.ifr_flags = ifp->if_flags;
46f25451 1939 ifr.ifr_flagshigh = ifp->if_flags >> 16;
78195a76 1940 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
bd4539cc 1941 (struct ucred *)NULL);
984263bc
MD
1942#ifdef INET
1943 /*
1944 * Also send gratuitous ARPs to notify other nodes about
1945 * the address change.
1946 */
b2632176
SZ
1947 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1948 struct ifaddr *ifa = ifac->ifa;
1949
984263bc
MD
1950 if (ifa->ifa_addr != NULL &&
1951 ifa->ifa_addr->sa_family == AF_INET)
1952 arp_ifinit(ifp, ifa);
1953 }
1954#endif
1955 }
78195a76 1956 lwkt_serialize_exit(ifp->if_serializer);
984263bc
MD
1957 return (0);
1958}
1959
1960struct ifmultiaddr *
f23061d4 1961ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
984263bc
MD
1962{
1963 struct ifmultiaddr *ifma;
1964
1965 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
0c3c561c 1966 if (sa_equal(ifma->ifma_addr, sa))
984263bc
MD
1967 break;
1968
1969 return ifma;
1970}
1971
e9bd1548
MD
1972/*
1973 * This function locates the first real ethernet MAC from a network
1974 * card and loads it into node, returning 0 on success or ENOENT if
1975 * no suitable interfaces were found. It is used by the uuid code to
1976 * generate a unique 6-byte number.
1977 */
1978int
1979if_getanyethermac(uint16_t *node, int minlen)
1980{
1981 struct ifnet *ifp;
1982 struct sockaddr_dl *sdl;
1983
1984 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1985 if (ifp->if_type != IFT_ETHER)
1986 continue;
1987 sdl = IF_LLSOCKADDR(ifp);
1988 if (sdl->sdl_alen < minlen)
1989 continue;
1990 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
1991 minlen);
1992 return(0);
1993 }
1994 return (ENOENT);
1995}
1996
1550dfd9
MD
1997/*
1998 * The name argument must be a pointer to storage which will last as
1999 * long as the interface does. For physical devices, the result of
2000 * device_get_name(dev) is a good choice and for pseudo-devices a
2001 * static string works well.
2002 */
2003void
2004if_initname(struct ifnet *ifp, const char *name, int unit)
2005{
3e4a09e7
MD
2006 ifp->if_dname = name;
2007 ifp->if_dunit = unit;
1550dfd9 2008 if (unit != IF_DUNIT_NONE)
f8c7a42d 2009 ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
1550dfd9
MD
2010 else
2011 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2012}
2013
984263bc
MD
2014int
2015if_printf(struct ifnet *ifp, const char *fmt, ...)
2016{
e2565a42 2017 __va_list ap;
984263bc
MD
2018 int retval;
2019
4b1cf444 2020 retval = kprintf("%s: ", ifp->if_xname);
e2565a42 2021 __va_start(ap, fmt);
379210cb 2022 retval += kvprintf(fmt, ap);
e2565a42 2023 __va_end(ap);
984263bc
MD
2024 return (retval);
2025}
2026
b2f93efe
JS
2027void
2028ifq_set_classic(struct ifaltq *ifq)
2029{
2030 ifq->altq_enqueue = ifq_classic_enqueue;
2031 ifq->altq_dequeue = ifq_classic_dequeue;
2032 ifq->altq_request = ifq_classic_request;
2033}
2034
9db4b353 2035int
e3e4574a
JS
2036ifq_classic_enqueue(struct ifaltq *ifq, struct mbuf *m,
2037 struct altq_pktattr *pa __unused)
2038{
9db4b353 2039 logifq(enqueue, ifq);
e3e4574a
JS
2040 if (IF_QFULL(ifq)) {
2041 m_freem(m);
2042 return(ENOBUFS);
2043 } else {
2044 IF_ENQUEUE(ifq, m);
2045 return(0);
2046 }
2047}
2048
9db4b353 2049struct mbuf *
d2c71fa0 2050ifq_classic_dequeue(struct ifaltq *ifq, struct mbuf *mpolled, int op)
e3e4574a
JS
2051{
2052 struct mbuf *m;
2053
2054 switch (op) {
2055 case ALTDQ_POLL:
2056 IF_POLL(ifq, m);
2057 break;
2058 case ALTDQ_REMOVE:
9db4b353 2059 logifq(dequeue, ifq);
e3e4574a
JS
2060 IF_DEQUEUE(ifq, m);
2061 break;
2062 default:
2063 panic("unsupported ALTQ dequeue op: %d", op);
2064 }
d2c71fa0 2065 KKASSERT(mpolled == NULL || mpolled == m);
e3e4574a
JS
2066 return(m);
2067}
2068
9db4b353 2069int
e3e4574a
JS
2070ifq_classic_request(struct ifaltq *ifq, int req, void *arg)
2071{
2072 switch (req) {
2073 case ALTRQ_PURGE:
2074 IF_DRAIN(ifq);
2075 break;
2076 default:
3f625015 2077 panic("unsupported ALTQ request: %d", req);
e3e4574a 2078 }
e3e4574a
JS
2079 return(0);
2080}
b2632176 2081
9db4b353
SZ
2082int
2083ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa)
2084{
2085 struct ifaltq *ifq = &ifp->if_snd;
57dff79c
SZ
2086 int running = 0, error, start = 0;
2087
2088 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
9db4b353
SZ
2089
2090 ALTQ_LOCK(ifq);
2091 error = ifq_enqueue_locked(ifq, m, pa);
2092 if (error) {
2093 ALTQ_UNLOCK(ifq);
2094 return error;
2095 }
2096 if (!ifq->altq_started) {
2097 /*
2098 * Hold the interlock of ifnet.if_start
2099 */
2100 ifq->altq_started = 1;
2101 start = 1;
2102 }
2103 ALTQ_UNLOCK(ifq);
2104
2105 ifp->if_obytes += m->m_pkthdr.len;
2106 if (m->m_flags & M_MCAST)
2107 ifp->if_omcasts++;
2108
2109 if (!start) {
2110 logifstart(avoid, ifp);
2111 return 0;
2112 }
2113
2114 if (ifq_dispatch_schedonly) {
2115 /*
2116 * Always schedule ifnet.if_start on ifnet's CPU,
2117 * short circuit the rest of this function.
2118 */
2119 logifstart(sched, ifp);
2120 if_start_schedule(ifp);
2121 return 0;
2122 }
2123
2124 /*
2125 * Try to do direct ifnet.if_start first, if there is
2126 * contention on ifnet's serializer, ifnet.if_start will
2127 * be scheduled on ifnet's CPU.
2128 */
57dff79c
SZ
2129 if (!lwkt_serialize_try(ifp->if_serializer)) {
2130 /*
2131 * ifnet serializer contention happened,
2132 * ifnet.if_start is scheduled on ifnet's
2133 * CPU, and we keep going.
2134 */
2135 logifstart(contend_sched, ifp);
2136 if_start_schedule(ifp);
2137 return 0;
9db4b353
SZ
2138 }
2139
2140 if ((ifp->if_flags & IFF_OACTIVE) == 0) {
2141 logifstart(run, ifp);
2142 ifp->if_start(ifp);
2143 if ((ifp->if_flags &
57dff79c 2144 (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
9db4b353
SZ
2145 running = 1;
2146 }
2147
57dff79c 2148 lwkt_serialize_exit(ifp->if_serializer);
9db4b353
SZ
2149
2150 if (ifq_dispatch_schednochk || if_start_need_schedule(ifq, running)) {
2151 /*
2152 * More data need to be transmitted, ifnet.if_start is
2153 * scheduled on ifnet's CPU, and we keep going.
2154 * NOTE: ifnet.if_start interlock is not released.
2155 */
2156 logifstart(sched, ifp);
2157 if_start_schedule(ifp);
2158 }
2159 return 0;
2160}
2161
b2632176
SZ
2162void *
2163ifa_create(int size, int flags)
2164{
2165 struct ifaddr *ifa;
2166 int i;
2167
2168 KASSERT(size >= sizeof(*ifa), ("ifaddr size too small\n"));
2169
2170 ifa = kmalloc(size, M_IFADDR, flags | M_ZERO);
2171 if (ifa == NULL)
2172 return NULL;
2173
2174 ifa->ifa_containers = kmalloc(ncpus * sizeof(struct ifaddr_container),
2175 M_IFADDR, M_WAITOK | M_ZERO);
2176 ifa->ifa_cpumask = smp_active_mask;
2177 for (i = 0; i < ncpus; ++i) {
2178 struct ifaddr_container *ifac = &ifa->ifa_containers[i];
2179
2180 ifac->ifa_magic = IFA_CONTAINER_MAGIC;
2181 ifac->ifa = ifa;
2182 ifac->ifa_refcnt = 1;
2183 }
2184#ifdef IFADDR_DEBUG
2185 kprintf("alloc ifa %p %d\n", ifa, size);
2186#endif
2187 return ifa;
2188}
2189
b2632176
SZ
2190static void
2191ifac_free_dispatch(struct netmsg *nmsg)
2192{
8967ddc7
SZ
2193 struct netmsg_ifaddr_free *fmsg = (struct netmsg_ifaddr_free *)nmsg;
2194 struct ifaddr *ifa = fmsg->nm_ifaddr;
b2632176 2195
8967ddc7
SZ
2196 KKASSERT(ifa->ifa_cpumask & (1 << fmsg->nm_cpuid));
2197 ifa->ifa_cpumask &= ~(1 << fmsg->nm_cpuid);
b2632176
SZ
2198 if (ifa->ifa_cpumask == 0) {
2199#ifdef IFADDR_DEBUG
2200 kprintf("free ifa %p\n", ifa);
2201#endif
2202 kfree(ifa->ifa_containers, M_IFADDR);
2203 kfree(ifa, M_IFADDR);
2204 }
8967ddc7 2205 /* Don't reply, 'nmsg' is embedded in ifaddr_container */
b2632176
SZ
2206}
2207
2208void
2209ifac_free(struct ifaddr_container *ifac, int cpu_id)
2210{
8967ddc7 2211 struct netmsg_ifaddr_free *fmsg;
b2632176
SZ
2212
2213 KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC);
2214 KKASSERT(ifac->ifa_refcnt == 0);
40f667f2
SZ
2215 KASSERT(ifac->ifa_listmask == 0,
2216 ("ifa is still on %#x lists\n", ifac->ifa_listmask));
b2632176
SZ
2217
2218 ifac->ifa_magic = IFA_CONTAINER_DEAD;
2219
b2632176 2220#ifdef IFADDR_DEBUG_VERBOSE
8967ddc7 2221 kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id);
b2632176
SZ
2222#endif
2223
8967ddc7
SZ
2224 fmsg = &ifac->ifa_freemsg;
2225 netmsg_init(&fmsg->nm_netmsg, &netisr_apanic_rport, 0,
2226 ifac_free_dispatch);
2227 fmsg->nm_ifaddr = ifac->ifa;
2228 fmsg->nm_cpuid = cpu_id;
b2632176 2229
8967ddc7 2230 ifa_sendmsg(&fmsg->nm_netmsg.nm_lmsg, 0);
b2632176
SZ
2231}
2232
2233static void
2234ifa_iflink_dispatch(struct netmsg *nmsg)
2235{
2236 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2237 struct ifaddr *ifa = msg->ifa;
2238 struct ifnet *ifp = msg->ifp;
2239 int cpu = mycpuid;
40f667f2 2240 struct ifaddr_container *ifac;
b2632176
SZ
2241
2242 crit_enter();
23027d35 2243
40f667f2 2244 ifac = &ifa->ifa_containers[cpu];
2adb7bc2 2245 ASSERT_IFAC_VALID(ifac);
40f667f2
SZ
2246 KASSERT((ifac->ifa_listmask & IFA_LIST_IFADDRHEAD) == 0,
2247 ("ifaddr is on if_addrheads\n"));
23027d35 2248
40f667f2
SZ
2249 ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD;
2250 if (msg->tail)
2251 TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link);
2252 else
2253 TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link);
23027d35 2254
b2632176
SZ
2255 crit_exit();
2256
2257 ifa_forwardmsg(&nmsg->nm_lmsg, cpu + 1);
2258}
2259
2260void
2261ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail)
2262{
2263 struct netmsg_ifaddr msg;
2264
2265 netmsg_init(&msg.netmsg, &curthread->td_msgport, 0,
2266 ifa_iflink_dispatch);
2267 msg.ifa = ifa;
2268 msg.ifp = ifp;
2269 msg.tail = tail;
2270
45e6dc84 2271 ifa_domsg(&msg.netmsg.nm_lmsg, 0);
b2632176
SZ
2272}
2273
2274static void
2275ifa_ifunlink_dispatch(struct netmsg *nmsg)
2276{
2277 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2278 struct ifaddr *ifa = msg->ifa;
2279 struct ifnet *ifp = msg->ifp;
2280 int cpu = mycpuid;
40f667f2 2281 struct ifaddr_container *ifac;
b2632176
SZ
2282
2283 crit_enter();
23027d35 2284
40f667f2 2285 ifac = &ifa->ifa_containers[cpu];
2adb7bc2 2286 ASSERT_IFAC_VALID(ifac);
40f667f2
SZ
2287 KASSERT(ifac->ifa_listmask & IFA_LIST_IFADDRHEAD,
2288 ("ifaddr is not on if_addrhead\n"));
23027d35 2289
40f667f2
SZ
2290 TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link);
2291 ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD;
23027d35 2292
b2632176
SZ
2293 crit_exit();
2294
2295 ifa_forwardmsg(&nmsg->nm_lmsg, cpu + 1);
2296}
2297
2298void
2299ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp)
2300{
2301 struct netmsg_ifaddr msg;
2302
2303 netmsg_init(&msg.netmsg, &curthread->td_msgport, 0,
2304 ifa_ifunlink_dispatch);
2305 msg.ifa = ifa;
2306 msg.ifp = ifp;
2307
45e6dc84 2308 ifa_domsg(&msg.netmsg.nm_lmsg, 0);
b2632176
SZ
2309}
2310
2311static void
2312ifa_destroy_dispatch(struct netmsg *nmsg)
2313{
2314 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2315
2316 IFAFREE(msg->ifa);
2317 ifa_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
2318}
2319
2320void
2321ifa_destroy(struct ifaddr *ifa)
2322{
2323 struct netmsg_ifaddr msg;
2324
2325 netmsg_init(&msg.netmsg, &curthread->td_msgport, 0,
2326 ifa_destroy_dispatch);
2327 msg.ifa = ifa;
2328
45e6dc84 2329 ifa_domsg(&msg.netmsg.nm_lmsg, 0);
b2632176
SZ
2330}
2331
2332struct lwkt_port *
d7944f0b 2333ifnet_portfn(int cpu)
b2632176 2334{
90af4fd3 2335 return &ifnet_threads[cpu].td_msgport;
b2632176
SZ
2336}
2337
c4882b7e
SZ
2338void
2339ifnet_forwardmsg(struct lwkt_msg *lmsg, int next_cpu)
2340{
ea2e6532
SZ
2341 KKASSERT(next_cpu > mycpuid && next_cpu <= ncpus);
2342
c4882b7e
SZ
2343 if (next_cpu < ncpus)
2344 lwkt_forwardmsg(ifnet_portfn(next_cpu), lmsg);
2345 else
2346 lwkt_replymsg(lmsg, 0);
2347}
2348
2349void
2350ifnet_domsg(struct lwkt_msg *lmsg, int cpu)
2351{
2352 KKASSERT(cpu < ncpus);
2353 lwkt_domsg(ifnet_portfn(cpu), lmsg, 0);
2354}
2355
8967ddc7
SZ
2356void
2357ifnet_sendmsg(struct lwkt_msg *lmsg, int cpu)
2358{
2359 KKASSERT(cpu < ncpus);
2360 lwkt_sendmsg(ifnet_portfn(cpu), lmsg);
2361}
2362
b2632176 2363static void
90af4fd3 2364ifnetinit(void *dummy __unused)
b2632176
SZ
2365{
2366 int i;
2367
2368 for (i = 0; i < ncpus; ++i) {
90af4fd3 2369 struct thread *thr = &ifnet_threads[i];
b2632176 2370
92db3805
SZ
2371 lwkt_create(netmsg_service_loop, &ifnet_mpsafe_thread, NULL,
2372 thr, TDF_NETWORK | TDF_MPSAFE, i, "ifnet %d", i);
b2632176
SZ
2373 netmsg_service_port_init(&thr->td_msgport);
2374 }
2375}