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