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