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