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