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