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