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