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