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