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