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