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