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