if.c revision 1.50 1 1.50 thorpej /* $NetBSD: if.c,v 1.50 1999/07/09 23:41:16 thorpej Exp $ */
2 1.49 itojun
3 1.49 itojun /*
4 1.49 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 1.49 itojun * All rights reserved.
6 1.49 itojun *
7 1.49 itojun * Redistribution and use in source and binary forms, with or without
8 1.49 itojun * modification, are permitted provided that the following conditions
9 1.49 itojun * are met:
10 1.49 itojun * 1. Redistributions of source code must retain the above copyright
11 1.49 itojun * notice, this list of conditions and the following disclaimer.
12 1.49 itojun * 2. Redistributions in binary form must reproduce the above copyright
13 1.49 itojun * notice, this list of conditions and the following disclaimer in the
14 1.49 itojun * documentation and/or other materials provided with the distribution.
15 1.49 itojun * 3. Neither the name of the project nor the names of its contributors
16 1.49 itojun * may be used to endorse or promote products derived from this software
17 1.49 itojun * without specific prior written permission.
18 1.49 itojun *
19 1.49 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 1.49 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.49 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.49 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 1.49 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.49 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.49 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.49 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.49 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.49 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.49 itojun * SUCH DAMAGE.
30 1.49 itojun */
31 1.16 cgd
32 1.1 cgd /*
33 1.15 mycroft * Copyright (c) 1980, 1986, 1993
34 1.15 mycroft * The Regents of the University of California. All rights reserved.
35 1.1 cgd *
36 1.1 cgd * Redistribution and use in source and binary forms, with or without
37 1.1 cgd * modification, are permitted provided that the following conditions
38 1.1 cgd * are met:
39 1.1 cgd * 1. Redistributions of source code must retain the above copyright
40 1.1 cgd * notice, this list of conditions and the following disclaimer.
41 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
42 1.1 cgd * notice, this list of conditions and the following disclaimer in the
43 1.1 cgd * documentation and/or other materials provided with the distribution.
44 1.1 cgd * 3. All advertising materials mentioning features or use of this software
45 1.1 cgd * must display the following acknowledgement:
46 1.1 cgd * This product includes software developed by the University of
47 1.1 cgd * California, Berkeley and its contributors.
48 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
49 1.1 cgd * may be used to endorse or promote products derived from this software
50 1.1 cgd * without specific prior written permission.
51 1.1 cgd *
52 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 1.1 cgd * SUCH DAMAGE.
63 1.1 cgd *
64 1.44 fvdl * @(#)if.c 8.5 (Berkeley) 1/9/95
65 1.1 cgd */
66 1.50 thorpej
67 1.50 thorpej #include "opt_inet.h"
68 1.46 thorpej
69 1.46 thorpej #include "opt_compat_linux.h"
70 1.47 thorpej #include "opt_compat_svr4.h"
71 1.48 christos #include "opt_compat_43.h"
72 1.1 cgd
73 1.8 mycroft #include <sys/param.h>
74 1.8 mycroft #include <sys/mbuf.h>
75 1.8 mycroft #include <sys/systm.h>
76 1.15 mycroft #include <sys/proc.h>
77 1.8 mycroft #include <sys/socket.h>
78 1.8 mycroft #include <sys/socketvar.h>
79 1.8 mycroft #include <sys/protosw.h>
80 1.8 mycroft #include <sys/kernel.h>
81 1.8 mycroft #include <sys/ioctl.h>
82 1.1 cgd
83 1.8 mycroft #include <net/if.h>
84 1.8 mycroft #include <net/if_dl.h>
85 1.8 mycroft #include <net/if_types.h>
86 1.24 christos #include <net/radix.h>
87 1.1 cgd
88 1.49 itojun #ifdef INET6
89 1.49 itojun /*XXX*/
90 1.49 itojun #include <netinet/in.h>
91 1.49 itojun #endif
92 1.49 itojun
93 1.1 cgd int ifqmaxlen = IFQ_MAXLEN;
94 1.15 mycroft void if_slowtimo __P((void *arg));
95 1.4 andrew
96 1.49 itojun #ifdef INET6
97 1.49 itojun /*
98 1.49 itojun * XXX: declare here to avoid to include many inet6 related files..
99 1.49 itojun * should be more generalized?
100 1.49 itojun */
101 1.49 itojun extern void nd6_setmtu __P((struct ifnet *));
102 1.49 itojun #endif
103 1.49 itojun
104 1.1 cgd /*
105 1.1 cgd * Network interface utility routines.
106 1.1 cgd *
107 1.1 cgd * Routines with ifa_ifwith* names take sockaddr *'s as
108 1.1 cgd * parameters.
109 1.1 cgd */
110 1.4 andrew void
111 1.1 cgd ifinit()
112 1.1 cgd {
113 1.1 cgd
114 1.4 andrew if_slowtimo(NULL);
115 1.1 cgd }
116 1.1 cgd
117 1.1 cgd int if_index = 0;
118 1.49 itojun struct ifaddr **ifnet_addrs = NULL;
119 1.49 itojun struct ifnet **ifindex2ifnet = NULL;
120 1.1 cgd
121 1.1 cgd /*
122 1.1 cgd * Attach an interface to the
123 1.1 cgd * list of "active" interfaces.
124 1.1 cgd */
125 1.15 mycroft void
126 1.1 cgd if_attach(ifp)
127 1.1 cgd struct ifnet *ifp;
128 1.1 cgd {
129 1.1 cgd unsigned socksize, ifasize;
130 1.34 thorpej int namelen, masklen;
131 1.1 cgd register struct sockaddr_dl *sdl;
132 1.1 cgd register struct ifaddr *ifa;
133 1.49 itojun static size_t if_indexlim = 8;
134 1.1 cgd
135 1.22 mycroft if (if_index == 0)
136 1.22 mycroft TAILQ_INIT(&ifnet);
137 1.22 mycroft TAILQ_INIT(&ifp->if_addrlist);
138 1.21 mycroft TAILQ_INSERT_TAIL(&ifnet, ifp, if_list);
139 1.1 cgd ifp->if_index = ++if_index;
140 1.49 itojun
141 1.49 itojun /*
142 1.49 itojun * We have some arrays that should be indexed by if_index.
143 1.49 itojun * since if_index will grow dynamically, they should grow too.
144 1.49 itojun * struct ifadd **ifnet_addrs
145 1.49 itojun * struct ifnet **ifindex2ifnet
146 1.49 itojun */
147 1.49 itojun if (ifnet_addrs == 0 || ifindex2ifnet == 0 || if_index >= if_indexlim) {
148 1.49 itojun size_t n;
149 1.49 itojun caddr_t q;
150 1.49 itojun
151 1.49 itojun while (if_index >= if_indexlim)
152 1.49 itojun if_indexlim <<= 1;
153 1.49 itojun
154 1.49 itojun /* grow ifnet_addrs */
155 1.49 itojun n = if_indexlim * sizeof(ifa);
156 1.49 itojun q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
157 1.49 itojun bzero(q, n);
158 1.1 cgd if (ifnet_addrs) {
159 1.49 itojun bcopy((caddr_t)ifnet_addrs, q, n/2);
160 1.1 cgd free((caddr_t)ifnet_addrs, M_IFADDR);
161 1.1 cgd }
162 1.49 itojun ifnet_addrs = (struct ifaddr **)q;
163 1.49 itojun
164 1.49 itojun /* grow ifindex2ifnet */
165 1.49 itojun n = if_indexlim * sizeof(struct ifnet *);
166 1.49 itojun q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
167 1.49 itojun bzero(q, n);
168 1.49 itojun if (ifindex2ifnet) {
169 1.49 itojun bcopy((caddr_t)ifindex2ifnet, q, n/2);
170 1.49 itojun free((caddr_t)ifindex2ifnet, M_IFADDR);
171 1.49 itojun }
172 1.49 itojun ifindex2ifnet = (struct ifnet **)q;
173 1.1 cgd }
174 1.49 itojun
175 1.49 itojun ifindex2ifnet[if_index] = ifp;
176 1.49 itojun
177 1.1 cgd /*
178 1.1 cgd * create a Link Level name for this device
179 1.1 cgd */
180 1.34 thorpej namelen = strlen(ifp->if_xname);
181 1.43 thorpej masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
182 1.15 mycroft socksize = masklen + ifp->if_addrlen;
183 1.1 cgd #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
184 1.1 cgd if (socksize < sizeof(*sdl))
185 1.1 cgd socksize = sizeof(*sdl);
186 1.20 cgd socksize = ROUNDUP(socksize);
187 1.1 cgd ifasize = sizeof(*ifa) + 2 * socksize;
188 1.1 cgd ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
189 1.1 cgd bzero((caddr_t)ifa, ifasize);
190 1.1 cgd sdl = (struct sockaddr_dl *)(ifa + 1);
191 1.1 cgd sdl->sdl_len = socksize;
192 1.1 cgd sdl->sdl_family = AF_LINK;
193 1.34 thorpej bcopy(ifp->if_xname, sdl->sdl_data, namelen);
194 1.34 thorpej sdl->sdl_nlen = namelen;
195 1.1 cgd sdl->sdl_index = ifp->if_index;
196 1.15 mycroft sdl->sdl_type = ifp->if_type;
197 1.49 itojun ifnet_addrs[if_index] = ifa;
198 1.15 mycroft ifa->ifa_ifp = ifp;
199 1.15 mycroft ifa->ifa_rtrequest = link_rtrequest;
200 1.21 mycroft TAILQ_INSERT_HEAD(&ifp->if_addrlist, ifa, ifa_list);
201 1.15 mycroft ifa->ifa_addr = (struct sockaddr *)sdl;
202 1.38 is ifp->if_sadl = sdl;
203 1.1 cgd sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
204 1.1 cgd ifa->ifa_netmask = (struct sockaddr *)sdl;
205 1.15 mycroft sdl->sdl_len = masklen;
206 1.1 cgd while (namelen != 0)
207 1.1 cgd sdl->sdl_data[--namelen] = 0xff;
208 1.40 thorpej if (ifp->if_snd.ifq_maxlen == 0)
209 1.40 thorpej ifp->if_snd.ifq_maxlen = ifqmaxlen;
210 1.42 is ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
211 1.1 cgd }
212 1.1 cgd /*
213 1.1 cgd * Locate an interface based on a complete address.
214 1.1 cgd */
215 1.1 cgd /*ARGSUSED*/
216 1.1 cgd struct ifaddr *
217 1.1 cgd ifa_ifwithaddr(addr)
218 1.1 cgd register struct sockaddr *addr;
219 1.1 cgd {
220 1.1 cgd register struct ifnet *ifp;
221 1.1 cgd register struct ifaddr *ifa;
222 1.1 cgd
223 1.1 cgd #define equal(a1, a2) \
224 1.1 cgd (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
225 1.21 mycroft for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next)
226 1.21 mycroft for (ifa = ifp->if_addrlist.tqh_first; ifa != 0; ifa = ifa->ifa_list.tqe_next) {
227 1.1 cgd if (ifa->ifa_addr->sa_family != addr->sa_family)
228 1.1 cgd continue;
229 1.1 cgd if (equal(addr, ifa->ifa_addr))
230 1.1 cgd return (ifa);
231 1.1 cgd if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
232 1.49 itojun /* IP6 doesn't have broadcast */
233 1.49 itojun ifa->ifa_broadaddr->sa_len != 0 &&
234 1.1 cgd equal(ifa->ifa_broadaddr, addr))
235 1.1 cgd return (ifa);
236 1.1 cgd }
237 1.1 cgd return ((struct ifaddr *)0);
238 1.1 cgd }
239 1.49 itojun
240 1.1 cgd /*
241 1.1 cgd * Locate the point to point interface with a given destination address.
242 1.1 cgd */
243 1.1 cgd /*ARGSUSED*/
244 1.1 cgd struct ifaddr *
245 1.1 cgd ifa_ifwithdstaddr(addr)
246 1.1 cgd register struct sockaddr *addr;
247 1.1 cgd {
248 1.1 cgd register struct ifnet *ifp;
249 1.1 cgd register struct ifaddr *ifa;
250 1.1 cgd
251 1.21 mycroft for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next)
252 1.1 cgd if (ifp->if_flags & IFF_POINTOPOINT)
253 1.21 mycroft for (ifa = ifp->if_addrlist.tqh_first; ifa != 0; ifa = ifa->ifa_list.tqe_next) {
254 1.30 mrg if (ifa->ifa_addr->sa_family != addr->sa_family ||
255 1.30 mrg ifa->ifa_dstaddr == NULL)
256 1.1 cgd continue;
257 1.1 cgd if (equal(addr, ifa->ifa_dstaddr))
258 1.1 cgd return (ifa);
259 1.1 cgd }
260 1.1 cgd return ((struct ifaddr *)0);
261 1.1 cgd }
262 1.1 cgd
263 1.1 cgd /*
264 1.1 cgd * Find an interface on a specific network. If many, choice
265 1.15 mycroft * is most specific found.
266 1.1 cgd */
267 1.1 cgd struct ifaddr *
268 1.1 cgd ifa_ifwithnet(addr)
269 1.1 cgd struct sockaddr *addr;
270 1.1 cgd {
271 1.1 cgd register struct ifnet *ifp;
272 1.1 cgd register struct ifaddr *ifa;
273 1.15 mycroft struct ifaddr *ifa_maybe = 0;
274 1.1 cgd u_int af = addr->sa_family;
275 1.15 mycroft char *addr_data = addr->sa_data, *cplim;
276 1.1 cgd
277 1.1 cgd if (af == AF_LINK) {
278 1.1 cgd register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
279 1.1 cgd if (sdl->sdl_index && sdl->sdl_index <= if_index)
280 1.49 itojun return (ifnet_addrs[sdl->sdl_index]);
281 1.1 cgd }
282 1.21 mycroft for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next)
283 1.21 mycroft for (ifa = ifp->if_addrlist.tqh_first; ifa != 0; ifa = ifa->ifa_list.tqe_next) {
284 1.15 mycroft register char *cp, *cp2, *cp3;
285 1.15 mycroft
286 1.15 mycroft if (ifa->ifa_addr->sa_family != af ||
287 1.15 mycroft ifa->ifa_netmask == 0)
288 1.32 mrg next: continue;
289 1.15 mycroft cp = addr_data;
290 1.15 mycroft cp2 = ifa->ifa_addr->sa_data;
291 1.15 mycroft cp3 = ifa->ifa_netmask->sa_data;
292 1.15 mycroft cplim = (char *)ifa->ifa_netmask +
293 1.15 mycroft ifa->ifa_netmask->sa_len;
294 1.15 mycroft while (cp3 < cplim)
295 1.15 mycroft if ((*cp++ ^ *cp2++) & *cp3++)
296 1.32 mrg /* want to continue for() loop */
297 1.32 mrg goto next;
298 1.15 mycroft if (ifa_maybe == 0 ||
299 1.15 mycroft rn_refines((caddr_t)ifa->ifa_netmask,
300 1.15 mycroft (caddr_t)ifa_maybe->ifa_netmask))
301 1.15 mycroft ifa_maybe = ifa;
302 1.15 mycroft }
303 1.15 mycroft return (ifa_maybe);
304 1.26 mrg }
305 1.26 mrg /*
306 1.26 mrg * Find the interface of the addresss.
307 1.26 mrg */
308 1.26 mrg struct ifaddr *
309 1.26 mrg ifa_ifwithladdr(addr)
310 1.26 mrg struct sockaddr *addr;
311 1.26 mrg {
312 1.26 mrg struct ifaddr *ia;
313 1.26 mrg
314 1.26 mrg if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr))
315 1.26 mrg || (ia = ifa_ifwithnet(addr)))
316 1.26 mrg return (ia);
317 1.26 mrg return (NULL);
318 1.1 cgd }
319 1.1 cgd
320 1.1 cgd /*
321 1.1 cgd * Find an interface using a specific address family
322 1.1 cgd */
323 1.1 cgd struct ifaddr *
324 1.1 cgd ifa_ifwithaf(af)
325 1.1 cgd register int af;
326 1.1 cgd {
327 1.1 cgd register struct ifnet *ifp;
328 1.1 cgd register struct ifaddr *ifa;
329 1.1 cgd
330 1.21 mycroft for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next)
331 1.21 mycroft for (ifa = ifp->if_addrlist.tqh_first; ifa != 0; ifa = ifa->ifa_list.tqe_next)
332 1.21 mycroft if (ifa->ifa_addr->sa_family == af)
333 1.21 mycroft return (ifa);
334 1.1 cgd return ((struct ifaddr *)0);
335 1.1 cgd }
336 1.1 cgd
337 1.1 cgd /*
338 1.1 cgd * Find an interface address specific to an interface best matching
339 1.1 cgd * a given address.
340 1.1 cgd */
341 1.1 cgd struct ifaddr *
342 1.1 cgd ifaof_ifpforaddr(addr, ifp)
343 1.1 cgd struct sockaddr *addr;
344 1.1 cgd register struct ifnet *ifp;
345 1.1 cgd {
346 1.1 cgd register struct ifaddr *ifa;
347 1.1 cgd register char *cp, *cp2, *cp3;
348 1.1 cgd register char *cplim;
349 1.1 cgd struct ifaddr *ifa_maybe = 0;
350 1.1 cgd u_int af = addr->sa_family;
351 1.1 cgd
352 1.1 cgd if (af >= AF_MAX)
353 1.1 cgd return (0);
354 1.21 mycroft for (ifa = ifp->if_addrlist.tqh_first; ifa != 0; ifa = ifa->ifa_list.tqe_next) {
355 1.1 cgd if (ifa->ifa_addr->sa_family != af)
356 1.1 cgd continue;
357 1.1 cgd ifa_maybe = ifa;
358 1.1 cgd if (ifa->ifa_netmask == 0) {
359 1.1 cgd if (equal(addr, ifa->ifa_addr) ||
360 1.1 cgd (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
361 1.1 cgd return (ifa);
362 1.1 cgd continue;
363 1.1 cgd }
364 1.1 cgd cp = addr->sa_data;
365 1.1 cgd cp2 = ifa->ifa_addr->sa_data;
366 1.1 cgd cp3 = ifa->ifa_netmask->sa_data;
367 1.1 cgd cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
368 1.1 cgd for (; cp3 < cplim; cp3++)
369 1.1 cgd if ((*cp++ ^ *cp2++) & *cp3)
370 1.1 cgd break;
371 1.1 cgd if (cp3 == cplim)
372 1.1 cgd return (ifa);
373 1.1 cgd }
374 1.1 cgd return (ifa_maybe);
375 1.1 cgd }
376 1.9 mycroft
377 1.15 mycroft #include <net/route.h>
378 1.15 mycroft
379 1.1 cgd /*
380 1.1 cgd * Default action when installing a route with a Link Level gateway.
381 1.1 cgd * Lookup an appropriate real ifa to point to.
382 1.1 cgd * This should be moved to /sys/net/link.c eventually.
383 1.1 cgd */
384 1.15 mycroft void
385 1.1 cgd link_rtrequest(cmd, rt, sa)
386 1.15 mycroft int cmd;
387 1.15 mycroft register struct rtentry *rt;
388 1.15 mycroft struct sockaddr *sa;
389 1.1 cgd {
390 1.1 cgd register struct ifaddr *ifa;
391 1.1 cgd struct sockaddr *dst;
392 1.15 mycroft struct ifnet *ifp;
393 1.1 cgd
394 1.1 cgd if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
395 1.1 cgd ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
396 1.1 cgd return;
397 1.24 christos if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
398 1.15 mycroft IFAFREE(rt->rt_ifa);
399 1.1 cgd rt->rt_ifa = ifa;
400 1.15 mycroft ifa->ifa_refcnt++;
401 1.1 cgd if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
402 1.1 cgd ifa->ifa_rtrequest(cmd, rt, sa);
403 1.1 cgd }
404 1.1 cgd }
405 1.1 cgd
406 1.1 cgd /*
407 1.1 cgd * Mark an interface down and notify protocols of
408 1.1 cgd * the transition.
409 1.23 mycroft * NOTE: must be called at splsoftnet or equivalent.
410 1.1 cgd */
411 1.15 mycroft void
412 1.1 cgd if_down(ifp)
413 1.1 cgd register struct ifnet *ifp;
414 1.1 cgd {
415 1.1 cgd register struct ifaddr *ifa;
416 1.1 cgd
417 1.1 cgd ifp->if_flags &= ~IFF_UP;
418 1.21 mycroft for (ifa = ifp->if_addrlist.tqh_first; ifa != 0; ifa = ifa->ifa_list.tqe_next)
419 1.1 cgd pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
420 1.1 cgd if_qflush(&ifp->if_snd);
421 1.15 mycroft rt_ifmsg(ifp);
422 1.15 mycroft }
423 1.15 mycroft
424 1.15 mycroft /*
425 1.15 mycroft * Mark an interface up and notify protocols of
426 1.15 mycroft * the transition.
427 1.23 mycroft * NOTE: must be called at splsoftnet or equivalent.
428 1.15 mycroft */
429 1.15 mycroft void
430 1.15 mycroft if_up(ifp)
431 1.15 mycroft register struct ifnet *ifp;
432 1.15 mycroft {
433 1.24 christos #ifdef notyet
434 1.15 mycroft register struct ifaddr *ifa;
435 1.24 christos #endif
436 1.15 mycroft
437 1.15 mycroft ifp->if_flags |= IFF_UP;
438 1.15 mycroft #ifdef notyet
439 1.15 mycroft /* this has no effect on IP, and will kill all ISO connections XXX */
440 1.24 christos for (ifa = ifp->if_addrlist.tqh_first; ifa != 0;
441 1.24 christos ifa = ifa->ifa_list.tqe_next)
442 1.15 mycroft pfctlinput(PRC_IFUP, ifa->ifa_addr);
443 1.15 mycroft #endif
444 1.15 mycroft rt_ifmsg(ifp);
445 1.49 itojun #ifdef INET6
446 1.49 itojun in6_if_up(ifp);
447 1.49 itojun #endif
448 1.1 cgd }
449 1.1 cgd
450 1.1 cgd /*
451 1.1 cgd * Flush an interface queue.
452 1.1 cgd */
453 1.15 mycroft void
454 1.1 cgd if_qflush(ifq)
455 1.1 cgd register struct ifqueue *ifq;
456 1.1 cgd {
457 1.1 cgd register struct mbuf *m, *n;
458 1.1 cgd
459 1.1 cgd n = ifq->ifq_head;
460 1.24 christos while ((m = n) != NULL) {
461 1.1 cgd n = m->m_act;
462 1.1 cgd m_freem(m);
463 1.1 cgd }
464 1.1 cgd ifq->ifq_head = 0;
465 1.1 cgd ifq->ifq_tail = 0;
466 1.1 cgd ifq->ifq_len = 0;
467 1.1 cgd }
468 1.1 cgd
469 1.1 cgd /*
470 1.1 cgd * Handle interface watchdog timer routines. Called
471 1.1 cgd * from softclock, we decrement timers (if set) and
472 1.1 cgd * call the appropriate interface routine on expiration.
473 1.1 cgd */
474 1.4 andrew void
475 1.13 cgd if_slowtimo(arg)
476 1.13 cgd void *arg;
477 1.1 cgd {
478 1.1 cgd register struct ifnet *ifp;
479 1.1 cgd int s = splimp();
480 1.1 cgd
481 1.21 mycroft for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next) {
482 1.1 cgd if (ifp->if_timer == 0 || --ifp->if_timer)
483 1.1 cgd continue;
484 1.1 cgd if (ifp->if_watchdog)
485 1.34 thorpej (*ifp->if_watchdog)(ifp);
486 1.1 cgd }
487 1.1 cgd splx(s);
488 1.14 mycroft timeout(if_slowtimo, NULL, hz / IFNET_SLOWHZ);
489 1.1 cgd }
490 1.1 cgd
491 1.1 cgd /*
492 1.1 cgd * Map interface name to
493 1.1 cgd * interface structure pointer.
494 1.1 cgd */
495 1.1 cgd struct ifnet *
496 1.1 cgd ifunit(name)
497 1.1 cgd register char *name;
498 1.1 cgd {
499 1.1 cgd register struct ifnet *ifp;
500 1.34 thorpej
501 1.34 thorpej for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next)
502 1.35 thorpej if (strcmp(ifp->if_xname, name) == 0)
503 1.34 thorpej return (ifp);
504 1.34 thorpej
505 1.34 thorpej return (NULL);
506 1.1 cgd }
507 1.1 cgd
508 1.49 itojun
509 1.49 itojun /*
510 1.49 itojun * Map interface name in a sockaddr_dl to
511 1.49 itojun * interface structure pointer.
512 1.49 itojun */
513 1.49 itojun struct ifnet *
514 1.49 itojun if_withname(sa)
515 1.49 itojun struct sockaddr *sa;
516 1.49 itojun {
517 1.49 itojun char ifname[IFNAMSIZ+1];
518 1.49 itojun struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
519 1.49 itojun
520 1.49 itojun if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
521 1.49 itojun (sdl->sdl_nlen > IFNAMSIZ) )
522 1.49 itojun return NULL;
523 1.49 itojun
524 1.49 itojun /*
525 1.49 itojun * ifunit wants a null-terminated name. It may not be null-terminated
526 1.49 itojun * in the sockaddr. We don't want to change the caller's sockaddr,
527 1.49 itojun * and there might not be room to put the trailing null anyway, so we
528 1.49 itojun * make a local copy that we know we can null terminate safely.
529 1.49 itojun */
530 1.49 itojun
531 1.49 itojun bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
532 1.49 itojun ifname[sdl->sdl_nlen] = '\0';
533 1.49 itojun return ifunit(ifname);
534 1.49 itojun }
535 1.49 itojun
536 1.49 itojun
537 1.1 cgd /*
538 1.1 cgd * Interface ioctls.
539 1.1 cgd */
540 1.15 mycroft int
541 1.1 cgd ifioctl(so, cmd, data, p)
542 1.1 cgd struct socket *so;
543 1.18 cgd u_long cmd;
544 1.1 cgd caddr_t data;
545 1.1 cgd struct proc *p;
546 1.1 cgd {
547 1.1 cgd register struct ifnet *ifp;
548 1.1 cgd register struct ifreq *ifr;
549 1.49 itojun int error = 0;
550 1.49 itojun short oif_flags;
551 1.1 cgd
552 1.1 cgd switch (cmd) {
553 1.1 cgd
554 1.1 cgd case SIOCGIFCONF:
555 1.1 cgd case OSIOCGIFCONF:
556 1.1 cgd return (ifconf(cmd, data));
557 1.1 cgd }
558 1.1 cgd ifr = (struct ifreq *)data;
559 1.1 cgd ifp = ifunit(ifr->ifr_name);
560 1.1 cgd if (ifp == 0)
561 1.1 cgd return (ENXIO);
562 1.49 itojun oif_flags = ifp->if_flags;
563 1.1 cgd switch (cmd) {
564 1.1 cgd
565 1.1 cgd case SIOCGIFFLAGS:
566 1.1 cgd ifr->ifr_flags = ifp->if_flags;
567 1.1 cgd break;
568 1.1 cgd
569 1.1 cgd case SIOCGIFMETRIC:
570 1.1 cgd ifr->ifr_metric = ifp->if_metric;
571 1.1 cgd break;
572 1.7 hpeyerl
573 1.36 mycroft case SIOCGIFMTU:
574 1.37 cgd ifr->ifr_mtu = ifp->if_mtu;
575 1.37 cgd break;
576 1.36 mycroft
577 1.1 cgd case SIOCSIFFLAGS:
578 1.24 christos if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
579 1.1 cgd return (error);
580 1.1 cgd if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
581 1.1 cgd int s = splimp();
582 1.1 cgd if_down(ifp);
583 1.1 cgd splx(s);
584 1.1 cgd }
585 1.15 mycroft if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
586 1.15 mycroft int s = splimp();
587 1.15 mycroft if_up(ifp);
588 1.15 mycroft splx(s);
589 1.15 mycroft }
590 1.1 cgd ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
591 1.1 cgd (ifr->ifr_flags &~ IFF_CANTCHANGE);
592 1.1 cgd if (ifp->if_ioctl)
593 1.1 cgd (void) (*ifp->if_ioctl)(ifp, cmd, data);
594 1.1 cgd break;
595 1.1 cgd
596 1.1 cgd case SIOCSIFMETRIC:
597 1.24 christos if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
598 1.1 cgd return (error);
599 1.1 cgd ifp->if_metric = ifr->ifr_metric;
600 1.5 deraadt break;
601 1.5 deraadt
602 1.36 mycroft case SIOCSIFMTU:
603 1.49 itojun {
604 1.49 itojun u_long oldmtu = ifp->if_mtu;
605 1.49 itojun
606 1.49 itojun error = suser(p->p_ucred, &p->p_acflag);
607 1.49 itojun if (error)
608 1.49 itojun return (error);
609 1.49 itojun if (ifp->if_ioctl == NULL)
610 1.49 itojun return (EOPNOTSUPP);
611 1.49 itojun error = (*ifp->if_ioctl)(ifp, cmd, data);
612 1.49 itojun
613 1.49 itojun /*
614 1.49 itojun * If the link MTU changed, do network layer specific procedure.
615 1.49 itojun */
616 1.49 itojun if (ifp->if_mtu != oldmtu) {
617 1.49 itojun #ifdef INET6
618 1.49 itojun nd6_setmtu(ifp);
619 1.49 itojun #endif
620 1.49 itojun }
621 1.49 itojun break;
622 1.49 itojun }
623 1.15 mycroft case SIOCADDMULTI:
624 1.15 mycroft case SIOCDELMULTI:
625 1.39 thorpej case SIOCSIFMEDIA:
626 1.24 christos if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
627 1.15 mycroft return (error);
628 1.39 thorpej /* FALLTHROUGH */
629 1.39 thorpej case SIOCGIFMEDIA:
630 1.15 mycroft if (ifp->if_ioctl == 0)
631 1.5 deraadt return (EOPNOTSUPP);
632 1.49 itojun error = (*ifp->if_ioctl)(ifp, cmd, data);
633 1.49 itojun break;
634 1.1 cgd
635 1.45 kml case SIOCSDRVSPEC:
636 1.45 kml /* XXX: need to pass proc pointer through to driver... */
637 1.45 kml if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
638 1.45 kml return (error);
639 1.45 kml /* FALLTHROUGH */
640 1.1 cgd default:
641 1.1 cgd if (so->so_proto == 0)
642 1.1 cgd return (EOPNOTSUPP);
643 1.33 christos #if !defined(COMPAT_43) && !defined(COMPAT_LINUX) && !defined(COMPAT_SVR4)
644 1.49 itojun error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
645 1.36 mycroft (struct mbuf *)cmd, (struct mbuf *)data,
646 1.36 mycroft (struct mbuf *)ifp, p));
647 1.1 cgd #else
648 1.1 cgd {
649 1.1 cgd int ocmd = cmd;
650 1.1 cgd
651 1.1 cgd switch (cmd) {
652 1.1 cgd
653 1.28 mycroft case SIOCSIFADDR:
654 1.1 cgd case SIOCSIFDSTADDR:
655 1.1 cgd case SIOCSIFBRDADDR:
656 1.1 cgd case SIOCSIFNETMASK:
657 1.1 cgd #if BYTE_ORDER != BIG_ENDIAN
658 1.1 cgd if (ifr->ifr_addr.sa_family == 0 &&
659 1.1 cgd ifr->ifr_addr.sa_len < 16) {
660 1.1 cgd ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
661 1.1 cgd ifr->ifr_addr.sa_len = 16;
662 1.1 cgd }
663 1.1 cgd #else
664 1.1 cgd if (ifr->ifr_addr.sa_len == 0)
665 1.1 cgd ifr->ifr_addr.sa_len = 16;
666 1.1 cgd #endif
667 1.1 cgd break;
668 1.1 cgd
669 1.1 cgd case OSIOCGIFADDR:
670 1.1 cgd cmd = SIOCGIFADDR;
671 1.1 cgd break;
672 1.1 cgd
673 1.1 cgd case OSIOCGIFDSTADDR:
674 1.1 cgd cmd = SIOCGIFDSTADDR;
675 1.1 cgd break;
676 1.1 cgd
677 1.1 cgd case OSIOCGIFBRDADDR:
678 1.1 cgd cmd = SIOCGIFBRDADDR;
679 1.1 cgd break;
680 1.1 cgd
681 1.1 cgd case OSIOCGIFNETMASK:
682 1.1 cgd cmd = SIOCGIFNETMASK;
683 1.1 cgd }
684 1.49 itojun
685 1.24 christos error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
686 1.36 mycroft (struct mbuf *)cmd, (struct mbuf *)data,
687 1.36 mycroft (struct mbuf *)ifp, p));
688 1.49 itojun
689 1.1 cgd switch (ocmd) {
690 1.1 cgd case OSIOCGIFADDR:
691 1.1 cgd case OSIOCGIFDSTADDR:
692 1.1 cgd case OSIOCGIFBRDADDR:
693 1.1 cgd case OSIOCGIFNETMASK:
694 1.20 cgd *(u_int16_t *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
695 1.1 cgd }
696 1.49 itojun }
697 1.49 itojun #endif /* COMPAT_43 */
698 1.49 itojun break;
699 1.49 itojun }
700 1.1 cgd
701 1.49 itojun if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
702 1.49 itojun #ifdef INET6
703 1.49 itojun if ((ifp->if_flags & IFF_UP) != 0) {
704 1.49 itojun int s = splimp();
705 1.49 itojun in6_if_up(ifp);
706 1.49 itojun splx(s);
707 1.49 itojun }
708 1.1 cgd #endif
709 1.1 cgd }
710 1.49 itojun
711 1.49 itojun return (error);
712 1.1 cgd }
713 1.1 cgd
714 1.1 cgd /*
715 1.1 cgd * Return interface configuration
716 1.1 cgd * of system. List may be used
717 1.1 cgd * in later ioctl's (above) to get
718 1.1 cgd * other information.
719 1.1 cgd */
720 1.1 cgd /*ARGSUSED*/
721 1.15 mycroft int
722 1.1 cgd ifconf(cmd, data)
723 1.19 mycroft u_long cmd;
724 1.1 cgd caddr_t data;
725 1.1 cgd {
726 1.1 cgd register struct ifconf *ifc = (struct ifconf *)data;
727 1.21 mycroft register struct ifnet *ifp;
728 1.1 cgd register struct ifaddr *ifa;
729 1.1 cgd struct ifreq ifr, *ifrp;
730 1.1 cgd int space = ifc->ifc_len, error = 0;
731 1.1 cgd
732 1.1 cgd ifrp = ifc->ifc_req;
733 1.21 mycroft for (ifp = ifnet.tqh_first;
734 1.33 christos space >= sizeof (ifr) && ifp != 0; ifp = ifp->if_list.tqe_next) {
735 1.34 thorpej bcopy(ifp->if_xname, ifr.ifr_name, IFNAMSIZ);
736 1.21 mycroft if ((ifa = ifp->if_addrlist.tqh_first) == 0) {
737 1.1 cgd bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
738 1.15 mycroft error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
739 1.15 mycroft sizeof(ifr));
740 1.1 cgd if (error)
741 1.1 cgd break;
742 1.1 cgd space -= sizeof (ifr), ifrp++;
743 1.1 cgd } else
744 1.33 christos for (; space >= sizeof (ifr) && ifa != 0; ifa = ifa->ifa_list.tqe_next) {
745 1.1 cgd register struct sockaddr *sa = ifa->ifa_addr;
746 1.33 christos #if defined(COMPAT_43) || defined(COMPAT_LINUX) || defined(COMPAT_SVR4)
747 1.1 cgd if (cmd == OSIOCGIFCONF) {
748 1.1 cgd struct osockaddr *osa =
749 1.1 cgd (struct osockaddr *)&ifr.ifr_addr;
750 1.1 cgd ifr.ifr_addr = *sa;
751 1.1 cgd osa->sa_family = sa->sa_family;
752 1.1 cgd error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
753 1.1 cgd sizeof (ifr));
754 1.1 cgd ifrp++;
755 1.1 cgd } else
756 1.1 cgd #endif
757 1.1 cgd if (sa->sa_len <= sizeof(*sa)) {
758 1.1 cgd ifr.ifr_addr = *sa;
759 1.1 cgd error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
760 1.1 cgd sizeof (ifr));
761 1.1 cgd ifrp++;
762 1.1 cgd } else {
763 1.1 cgd space -= sa->sa_len - sizeof(*sa);
764 1.1 cgd if (space < sizeof (ifr))
765 1.1 cgd break;
766 1.1 cgd error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
767 1.1 cgd sizeof (ifr.ifr_name));
768 1.1 cgd if (error == 0)
769 1.1 cgd error = copyout((caddr_t)sa,
770 1.1 cgd (caddr_t)&ifrp->ifr_addr, sa->sa_len);
771 1.1 cgd ifrp = (struct ifreq *)
772 1.1 cgd (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
773 1.1 cgd }
774 1.1 cgd if (error)
775 1.1 cgd break;
776 1.1 cgd space -= sizeof (ifr);
777 1.1 cgd }
778 1.1 cgd }
779 1.1 cgd ifc->ifc_len -= space;
780 1.1 cgd return (error);
781 1.1 cgd }
782