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