if.c revision 1.28 1 1.28 mycroft /* $NetBSD: if.c,v 1.28 1996/02/27 08:17:08 mycroft 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.1 cgd if (ifa->ifa_addr->sa_family != addr->sa_family)
183 1.1 cgd continue;
184 1.1 cgd if (equal(addr, ifa->ifa_dstaddr))
185 1.1 cgd return (ifa);
186 1.1 cgd }
187 1.1 cgd return ((struct ifaddr *)0);
188 1.1 cgd }
189 1.1 cgd
190 1.1 cgd /*
191 1.1 cgd * Find an interface on a specific network. If many, choice
192 1.15 mycroft * is most specific found.
193 1.1 cgd */
194 1.1 cgd struct ifaddr *
195 1.1 cgd ifa_ifwithnet(addr)
196 1.1 cgd struct sockaddr *addr;
197 1.1 cgd {
198 1.1 cgd register struct ifnet *ifp;
199 1.1 cgd register struct ifaddr *ifa;
200 1.15 mycroft struct ifaddr *ifa_maybe = 0;
201 1.1 cgd u_int af = addr->sa_family;
202 1.15 mycroft char *addr_data = addr->sa_data, *cplim;
203 1.1 cgd
204 1.1 cgd if (af == AF_LINK) {
205 1.1 cgd register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
206 1.1 cgd if (sdl->sdl_index && sdl->sdl_index <= if_index)
207 1.1 cgd return (ifnet_addrs[sdl->sdl_index - 1]);
208 1.1 cgd }
209 1.21 mycroft for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next)
210 1.21 mycroft for (ifa = ifp->if_addrlist.tqh_first; ifa != 0; ifa = ifa->ifa_list.tqe_next) {
211 1.15 mycroft register char *cp, *cp2, *cp3;
212 1.15 mycroft
213 1.15 mycroft if (ifa->ifa_addr->sa_family != af ||
214 1.15 mycroft ifa->ifa_netmask == 0)
215 1.15 mycroft next: continue;
216 1.15 mycroft cp = addr_data;
217 1.15 mycroft cp2 = ifa->ifa_addr->sa_data;
218 1.15 mycroft cp3 = ifa->ifa_netmask->sa_data;
219 1.15 mycroft cplim = (char *)ifa->ifa_netmask +
220 1.15 mycroft ifa->ifa_netmask->sa_len;
221 1.15 mycroft while (cp3 < cplim)
222 1.15 mycroft if ((*cp++ ^ *cp2++) & *cp3++)
223 1.15 mycroft goto next;
224 1.15 mycroft if (ifa_maybe == 0 ||
225 1.15 mycroft rn_refines((caddr_t)ifa->ifa_netmask,
226 1.15 mycroft (caddr_t)ifa_maybe->ifa_netmask))
227 1.15 mycroft ifa_maybe = ifa;
228 1.15 mycroft }
229 1.15 mycroft return (ifa_maybe);
230 1.26 mrg }
231 1.26 mrg /*
232 1.26 mrg * Find the interface of the addresss.
233 1.26 mrg */
234 1.26 mrg struct ifaddr *
235 1.26 mrg ifa_ifwithladdr(addr)
236 1.26 mrg struct sockaddr *addr;
237 1.26 mrg {
238 1.26 mrg struct ifaddr *ia;
239 1.26 mrg
240 1.26 mrg if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr))
241 1.26 mrg || (ia = ifa_ifwithnet(addr)))
242 1.26 mrg return (ia);
243 1.26 mrg return (NULL);
244 1.1 cgd }
245 1.1 cgd
246 1.1 cgd /*
247 1.1 cgd * Find an interface using a specific address family
248 1.1 cgd */
249 1.1 cgd struct ifaddr *
250 1.1 cgd ifa_ifwithaf(af)
251 1.1 cgd register int af;
252 1.1 cgd {
253 1.1 cgd register struct ifnet *ifp;
254 1.1 cgd register struct ifaddr *ifa;
255 1.1 cgd
256 1.21 mycroft for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next)
257 1.21 mycroft for (ifa = ifp->if_addrlist.tqh_first; ifa != 0; ifa = ifa->ifa_list.tqe_next)
258 1.21 mycroft if (ifa->ifa_addr->sa_family == af)
259 1.21 mycroft return (ifa);
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 address specific to an interface best matching
265 1.1 cgd * a given address.
266 1.1 cgd */
267 1.1 cgd struct ifaddr *
268 1.1 cgd ifaof_ifpforaddr(addr, ifp)
269 1.1 cgd struct sockaddr *addr;
270 1.1 cgd register struct ifnet *ifp;
271 1.1 cgd {
272 1.1 cgd register struct ifaddr *ifa;
273 1.1 cgd register char *cp, *cp2, *cp3;
274 1.1 cgd register char *cplim;
275 1.1 cgd struct ifaddr *ifa_maybe = 0;
276 1.1 cgd u_int af = addr->sa_family;
277 1.1 cgd
278 1.1 cgd if (af >= AF_MAX)
279 1.1 cgd return (0);
280 1.21 mycroft for (ifa = ifp->if_addrlist.tqh_first; ifa != 0; ifa = ifa->ifa_list.tqe_next) {
281 1.1 cgd if (ifa->ifa_addr->sa_family != af)
282 1.1 cgd continue;
283 1.1 cgd ifa_maybe = ifa;
284 1.1 cgd if (ifa->ifa_netmask == 0) {
285 1.1 cgd if (equal(addr, ifa->ifa_addr) ||
286 1.1 cgd (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
287 1.1 cgd return (ifa);
288 1.1 cgd continue;
289 1.1 cgd }
290 1.1 cgd cp = addr->sa_data;
291 1.1 cgd cp2 = ifa->ifa_addr->sa_data;
292 1.1 cgd cp3 = ifa->ifa_netmask->sa_data;
293 1.1 cgd cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
294 1.1 cgd for (; cp3 < cplim; cp3++)
295 1.1 cgd if ((*cp++ ^ *cp2++) & *cp3)
296 1.1 cgd break;
297 1.1 cgd if (cp3 == cplim)
298 1.1 cgd return (ifa);
299 1.1 cgd }
300 1.1 cgd return (ifa_maybe);
301 1.1 cgd }
302 1.9 mycroft
303 1.15 mycroft #include <net/route.h>
304 1.15 mycroft
305 1.1 cgd /*
306 1.1 cgd * Default action when installing a route with a Link Level gateway.
307 1.1 cgd * Lookup an appropriate real ifa to point to.
308 1.1 cgd * This should be moved to /sys/net/link.c eventually.
309 1.1 cgd */
310 1.15 mycroft void
311 1.1 cgd link_rtrequest(cmd, rt, sa)
312 1.15 mycroft int cmd;
313 1.15 mycroft register struct rtentry *rt;
314 1.15 mycroft struct sockaddr *sa;
315 1.1 cgd {
316 1.1 cgd register struct ifaddr *ifa;
317 1.1 cgd struct sockaddr *dst;
318 1.15 mycroft struct ifnet *ifp;
319 1.1 cgd
320 1.1 cgd if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
321 1.1 cgd ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
322 1.1 cgd return;
323 1.24 christos if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
324 1.15 mycroft IFAFREE(rt->rt_ifa);
325 1.1 cgd rt->rt_ifa = ifa;
326 1.15 mycroft ifa->ifa_refcnt++;
327 1.1 cgd if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
328 1.1 cgd ifa->ifa_rtrequest(cmd, rt, sa);
329 1.1 cgd }
330 1.1 cgd }
331 1.1 cgd
332 1.1 cgd /*
333 1.1 cgd * Mark an interface down and notify protocols of
334 1.1 cgd * the transition.
335 1.23 mycroft * NOTE: must be called at splsoftnet or equivalent.
336 1.1 cgd */
337 1.15 mycroft void
338 1.1 cgd if_down(ifp)
339 1.1 cgd register struct ifnet *ifp;
340 1.1 cgd {
341 1.1 cgd register struct ifaddr *ifa;
342 1.1 cgd
343 1.1 cgd ifp->if_flags &= ~IFF_UP;
344 1.21 mycroft for (ifa = ifp->if_addrlist.tqh_first; ifa != 0; ifa = ifa->ifa_list.tqe_next)
345 1.1 cgd pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
346 1.1 cgd if_qflush(&ifp->if_snd);
347 1.15 mycroft rt_ifmsg(ifp);
348 1.15 mycroft }
349 1.15 mycroft
350 1.15 mycroft /*
351 1.15 mycroft * Mark an interface up and notify protocols of
352 1.15 mycroft * the transition.
353 1.23 mycroft * NOTE: must be called at splsoftnet or equivalent.
354 1.15 mycroft */
355 1.15 mycroft void
356 1.15 mycroft if_up(ifp)
357 1.15 mycroft register struct ifnet *ifp;
358 1.15 mycroft {
359 1.24 christos #ifdef notyet
360 1.15 mycroft register struct ifaddr *ifa;
361 1.24 christos #endif
362 1.15 mycroft
363 1.15 mycroft ifp->if_flags |= IFF_UP;
364 1.15 mycroft #ifdef notyet
365 1.15 mycroft /* this has no effect on IP, and will kill all ISO connections XXX */
366 1.24 christos for (ifa = ifp->if_addrlist.tqh_first; ifa != 0;
367 1.24 christos ifa = ifa->ifa_list.tqe_next)
368 1.15 mycroft pfctlinput(PRC_IFUP, ifa->ifa_addr);
369 1.15 mycroft #endif
370 1.15 mycroft rt_ifmsg(ifp);
371 1.1 cgd }
372 1.1 cgd
373 1.1 cgd /*
374 1.1 cgd * Flush an interface queue.
375 1.1 cgd */
376 1.15 mycroft void
377 1.1 cgd if_qflush(ifq)
378 1.1 cgd register struct ifqueue *ifq;
379 1.1 cgd {
380 1.1 cgd register struct mbuf *m, *n;
381 1.1 cgd
382 1.1 cgd n = ifq->ifq_head;
383 1.24 christos while ((m = n) != NULL) {
384 1.1 cgd n = m->m_act;
385 1.1 cgd m_freem(m);
386 1.1 cgd }
387 1.1 cgd ifq->ifq_head = 0;
388 1.1 cgd ifq->ifq_tail = 0;
389 1.1 cgd ifq->ifq_len = 0;
390 1.1 cgd }
391 1.1 cgd
392 1.1 cgd /*
393 1.1 cgd * Handle interface watchdog timer routines. Called
394 1.1 cgd * from softclock, we decrement timers (if set) and
395 1.1 cgd * call the appropriate interface routine on expiration.
396 1.1 cgd */
397 1.4 andrew void
398 1.13 cgd if_slowtimo(arg)
399 1.13 cgd void *arg;
400 1.1 cgd {
401 1.1 cgd register struct ifnet *ifp;
402 1.1 cgd int s = splimp();
403 1.1 cgd
404 1.21 mycroft for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next) {
405 1.1 cgd if (ifp->if_timer == 0 || --ifp->if_timer)
406 1.1 cgd continue;
407 1.1 cgd if (ifp->if_watchdog)
408 1.1 cgd (*ifp->if_watchdog)(ifp->if_unit);
409 1.1 cgd }
410 1.1 cgd splx(s);
411 1.14 mycroft timeout(if_slowtimo, NULL, hz / IFNET_SLOWHZ);
412 1.1 cgd }
413 1.1 cgd
414 1.1 cgd /*
415 1.1 cgd * Map interface name to
416 1.1 cgd * interface structure pointer.
417 1.1 cgd */
418 1.1 cgd struct ifnet *
419 1.1 cgd ifunit(name)
420 1.1 cgd register char *name;
421 1.1 cgd {
422 1.1 cgd register char *cp;
423 1.1 cgd register struct ifnet *ifp;
424 1.1 cgd int unit;
425 1.1 cgd unsigned len;
426 1.1 cgd char *ep, c;
427 1.1 cgd
428 1.1 cgd for (cp = name; cp < name + IFNAMSIZ && *cp; cp++)
429 1.1 cgd if (*cp >= '0' && *cp <= '9')
430 1.1 cgd break;
431 1.1 cgd if (*cp == '\0' || cp == name + IFNAMSIZ)
432 1.1 cgd return ((struct ifnet *)0);
433 1.1 cgd /*
434 1.1 cgd * Save first char of unit, and pointer to it,
435 1.1 cgd * so we can put a null there to avoid matching
436 1.1 cgd * initial substrings of interface names.
437 1.1 cgd */
438 1.1 cgd len = cp - name + 1;
439 1.1 cgd c = *cp;
440 1.1 cgd ep = cp;
441 1.1 cgd for (unit = 0; *cp >= '0' && *cp <= '9'; )
442 1.1 cgd unit = unit * 10 + *cp++ - '0';
443 1.1 cgd *ep = 0;
444 1.21 mycroft for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next) {
445 1.1 cgd if (bcmp(ifp->if_name, name, len))
446 1.1 cgd continue;
447 1.1 cgd if (unit == ifp->if_unit)
448 1.1 cgd break;
449 1.1 cgd }
450 1.1 cgd *ep = c;
451 1.1 cgd return (ifp);
452 1.1 cgd }
453 1.1 cgd
454 1.1 cgd /*
455 1.1 cgd * Interface ioctls.
456 1.1 cgd */
457 1.15 mycroft int
458 1.1 cgd ifioctl(so, cmd, data, p)
459 1.1 cgd struct socket *so;
460 1.18 cgd u_long cmd;
461 1.1 cgd caddr_t data;
462 1.1 cgd struct proc *p;
463 1.1 cgd {
464 1.1 cgd register struct ifnet *ifp;
465 1.1 cgd register struct ifreq *ifr;
466 1.1 cgd int error;
467 1.1 cgd
468 1.1 cgd switch (cmd) {
469 1.1 cgd
470 1.1 cgd case SIOCGIFCONF:
471 1.1 cgd case OSIOCGIFCONF:
472 1.1 cgd return (ifconf(cmd, data));
473 1.1 cgd }
474 1.1 cgd ifr = (struct ifreq *)data;
475 1.1 cgd ifp = ifunit(ifr->ifr_name);
476 1.1 cgd if (ifp == 0)
477 1.1 cgd return (ENXIO);
478 1.1 cgd switch (cmd) {
479 1.1 cgd
480 1.1 cgd case SIOCGIFFLAGS:
481 1.1 cgd ifr->ifr_flags = ifp->if_flags;
482 1.1 cgd break;
483 1.1 cgd
484 1.1 cgd case SIOCGIFMETRIC:
485 1.1 cgd ifr->ifr_metric = ifp->if_metric;
486 1.1 cgd break;
487 1.7 hpeyerl
488 1.1 cgd case SIOCSIFFLAGS:
489 1.24 christos if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
490 1.1 cgd return (error);
491 1.1 cgd if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
492 1.1 cgd int s = splimp();
493 1.1 cgd if_down(ifp);
494 1.1 cgd splx(s);
495 1.1 cgd }
496 1.15 mycroft if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
497 1.15 mycroft int s = splimp();
498 1.15 mycroft if_up(ifp);
499 1.15 mycroft splx(s);
500 1.15 mycroft }
501 1.1 cgd ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
502 1.1 cgd (ifr->ifr_flags &~ IFF_CANTCHANGE);
503 1.1 cgd if (ifp->if_ioctl)
504 1.1 cgd (void) (*ifp->if_ioctl)(ifp, cmd, data);
505 1.1 cgd break;
506 1.1 cgd
507 1.1 cgd case SIOCSIFMETRIC:
508 1.24 christos if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
509 1.1 cgd return (error);
510 1.1 cgd ifp->if_metric = ifr->ifr_metric;
511 1.5 deraadt break;
512 1.5 deraadt
513 1.15 mycroft case SIOCADDMULTI:
514 1.15 mycroft case SIOCDELMULTI:
515 1.24 christos if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
516 1.15 mycroft return (error);
517 1.15 mycroft if (ifp->if_ioctl == 0)
518 1.5 deraadt return (EOPNOTSUPP);
519 1.5 deraadt return ((*ifp->if_ioctl)(ifp, cmd, data));
520 1.1 cgd
521 1.1 cgd default:
522 1.1 cgd if (so->so_proto == 0)
523 1.1 cgd return (EOPNOTSUPP);
524 1.28 mycroft #if !defined(COMPAT_43) && !defined(COMPAT_LINUX)
525 1.1 cgd return ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
526 1.25 christos (struct mbuf *) cmd, (struct mbuf *) data,
527 1.25 christos (struct mbuf *) ifp));
528 1.1 cgd #else
529 1.1 cgd {
530 1.1 cgd int ocmd = cmd;
531 1.1 cgd
532 1.1 cgd switch (cmd) {
533 1.1 cgd
534 1.28 mycroft case SIOCSIFADDR:
535 1.1 cgd case SIOCSIFDSTADDR:
536 1.1 cgd case SIOCSIFBRDADDR:
537 1.1 cgd case SIOCSIFNETMASK:
538 1.1 cgd #if BYTE_ORDER != BIG_ENDIAN
539 1.1 cgd if (ifr->ifr_addr.sa_family == 0 &&
540 1.1 cgd ifr->ifr_addr.sa_len < 16) {
541 1.1 cgd ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
542 1.1 cgd ifr->ifr_addr.sa_len = 16;
543 1.1 cgd }
544 1.1 cgd #else
545 1.1 cgd if (ifr->ifr_addr.sa_len == 0)
546 1.1 cgd ifr->ifr_addr.sa_len = 16;
547 1.1 cgd #endif
548 1.1 cgd break;
549 1.1 cgd
550 1.1 cgd case OSIOCGIFADDR:
551 1.1 cgd cmd = SIOCGIFADDR;
552 1.1 cgd break;
553 1.1 cgd
554 1.1 cgd case OSIOCGIFDSTADDR:
555 1.1 cgd cmd = SIOCGIFDSTADDR;
556 1.1 cgd break;
557 1.1 cgd
558 1.1 cgd case OSIOCGIFBRDADDR:
559 1.1 cgd cmd = SIOCGIFBRDADDR;
560 1.1 cgd break;
561 1.1 cgd
562 1.1 cgd case OSIOCGIFNETMASK:
563 1.1 cgd cmd = SIOCGIFNETMASK;
564 1.1 cgd }
565 1.24 christos error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
566 1.24 christos (struct mbuf *) cmd,
567 1.24 christos (struct mbuf *) data,
568 1.24 christos (struct mbuf *) ifp));
569 1.1 cgd switch (ocmd) {
570 1.1 cgd
571 1.1 cgd case OSIOCGIFADDR:
572 1.1 cgd case OSIOCGIFDSTADDR:
573 1.1 cgd case OSIOCGIFBRDADDR:
574 1.1 cgd case OSIOCGIFNETMASK:
575 1.20 cgd *(u_int16_t *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
576 1.1 cgd }
577 1.1 cgd return (error);
578 1.1 cgd
579 1.1 cgd }
580 1.1 cgd #endif
581 1.1 cgd }
582 1.1 cgd return (0);
583 1.1 cgd }
584 1.1 cgd
585 1.1 cgd /*
586 1.1 cgd * Return interface configuration
587 1.1 cgd * of system. List may be used
588 1.1 cgd * in later ioctl's (above) to get
589 1.1 cgd * other information.
590 1.1 cgd */
591 1.1 cgd /*ARGSUSED*/
592 1.15 mycroft int
593 1.1 cgd ifconf(cmd, data)
594 1.19 mycroft u_long cmd;
595 1.1 cgd caddr_t data;
596 1.1 cgd {
597 1.1 cgd register struct ifconf *ifc = (struct ifconf *)data;
598 1.21 mycroft register struct ifnet *ifp;
599 1.1 cgd register struct ifaddr *ifa;
600 1.1 cgd register char *cp, *ep;
601 1.1 cgd struct ifreq ifr, *ifrp;
602 1.1 cgd int space = ifc->ifc_len, error = 0;
603 1.1 cgd
604 1.1 cgd ifrp = ifc->ifc_req;
605 1.1 cgd ep = ifr.ifr_name + sizeof (ifr.ifr_name) - 2;
606 1.21 mycroft for (ifp = ifnet.tqh_first;
607 1.21 mycroft space > sizeof (ifr) && ifp != 0; ifp = ifp->if_list.tqe_next) {
608 1.15 mycroft strncpy(ifr.ifr_name, ifp->if_name, sizeof(ifr.ifr_name) - 2);
609 1.1 cgd for (cp = ifr.ifr_name; cp < ep && *cp; cp++)
610 1.15 mycroft continue;
611 1.1 cgd *cp++ = '0' + ifp->if_unit; *cp = '\0';
612 1.21 mycroft if ((ifa = ifp->if_addrlist.tqh_first) == 0) {
613 1.1 cgd bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
614 1.15 mycroft error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
615 1.15 mycroft sizeof(ifr));
616 1.1 cgd if (error)
617 1.1 cgd break;
618 1.1 cgd space -= sizeof (ifr), ifrp++;
619 1.1 cgd } else
620 1.21 mycroft for (; space > sizeof (ifr) && ifa != 0; ifa = ifa->ifa_list.tqe_next) {
621 1.1 cgd register struct sockaddr *sa = ifa->ifa_addr;
622 1.27 mycroft #if defined(COMPAT_43) || defined(COMPAT_LINUX)
623 1.1 cgd if (cmd == OSIOCGIFCONF) {
624 1.1 cgd struct osockaddr *osa =
625 1.1 cgd (struct osockaddr *)&ifr.ifr_addr;
626 1.1 cgd ifr.ifr_addr = *sa;
627 1.1 cgd osa->sa_family = sa->sa_family;
628 1.1 cgd error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
629 1.1 cgd sizeof (ifr));
630 1.1 cgd ifrp++;
631 1.1 cgd } else
632 1.1 cgd #endif
633 1.1 cgd if (sa->sa_len <= sizeof(*sa)) {
634 1.1 cgd ifr.ifr_addr = *sa;
635 1.1 cgd error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
636 1.1 cgd sizeof (ifr));
637 1.1 cgd ifrp++;
638 1.1 cgd } else {
639 1.1 cgd space -= sa->sa_len - sizeof(*sa);
640 1.1 cgd if (space < sizeof (ifr))
641 1.1 cgd break;
642 1.1 cgd error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
643 1.1 cgd sizeof (ifr.ifr_name));
644 1.1 cgd if (error == 0)
645 1.1 cgd error = copyout((caddr_t)sa,
646 1.1 cgd (caddr_t)&ifrp->ifr_addr, sa->sa_len);
647 1.1 cgd ifrp = (struct ifreq *)
648 1.1 cgd (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
649 1.1 cgd }
650 1.1 cgd if (error)
651 1.1 cgd break;
652 1.1 cgd space -= sizeof (ifr);
653 1.1 cgd }
654 1.1 cgd }
655 1.1 cgd ifc->ifc_len -= space;
656 1.1 cgd return (error);
657 1.1 cgd }
658 1.1 cgd
659 1.1 cgd static char *
660 1.1 cgd sprint_d(n, buf, buflen)
661 1.1 cgd u_int n;
662 1.1 cgd char *buf;
663 1.1 cgd int buflen;
664 1.1 cgd {
665 1.1 cgd register char *cp = buf + buflen - 1;
666 1.1 cgd
667 1.1 cgd *cp = 0;
668 1.1 cgd do {
669 1.1 cgd cp--;
670 1.1 cgd *cp = "0123456789"[n % 10];
671 1.1 cgd n /= 10;
672 1.1 cgd } while (n != 0);
673 1.1 cgd return (cp);
674 1.1 cgd }
675