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