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