| 1 | /* |
| 2 | * Copyright (c) 1980, 1986 Regents of the University of California. |
| 3 | * 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 | * from: @(#)if.c 7.14 (Berkeley) 4/20/91 |
| 34 | * $Id: if.c,v 1.4 1993/10/16 17:43:10 rgrimes Exp $ |
| 35 | */ |
| 36 | |
| 37 | #include "param.h" |
| 38 | #include "mbuf.h" |
| 39 | #include "systm.h" |
| 40 | #include "socket.h" |
| 41 | #include "socketvar.h" |
| 42 | #include "protosw.h" |
| 43 | #include "proc.h" |
| 44 | #include "kernel.h" |
| 45 | #include "ioctl.h" |
| 46 | |
| 47 | #include "if.h" |
| 48 | #include "if_dl.h" |
| 49 | #include "if_types.h" |
| 50 | |
| 51 | #include "ether.h" |
| 52 | |
| 53 | int ifqmaxlen = IFQ_MAXLEN; |
| 54 | struct ifqueue rawintrq; /* raw packet input queue */ |
| 55 | struct ifnet *ifnet; /* list of configured interfaces */ |
| 56 | |
| 57 | /* |
| 58 | * Network interface utility routines. |
| 59 | * |
| 60 | * Routines with ifa_ifwith* names take sockaddr *'s as |
| 61 | * parameters. |
| 62 | */ |
| 63 | |
| 64 | ifinit() |
| 65 | { |
| 66 | register struct ifnet *ifp; |
| 67 | |
| 68 | for (ifp = ifnet; ifp; ifp = ifp->if_next) |
| 69 | if (ifp->if_snd.ifq_maxlen == 0) |
| 70 | ifp->if_snd.ifq_maxlen = ifqmaxlen; |
| 71 | if_slowtimo(); |
| 72 | } |
| 73 | |
| 74 | #ifdef vax |
| 75 | /* |
| 76 | * Call each interface on a Unibus reset. |
| 77 | */ |
| 78 | ifubareset(uban) |
| 79 | int uban; |
| 80 | { |
| 81 | register struct ifnet *ifp; |
| 82 | |
| 83 | for (ifp = ifnet; ifp; ifp = ifp->if_next) |
| 84 | if (ifp->if_reset) |
| 85 | (*ifp->if_reset)(ifp->if_unit, uban); |
| 86 | } |
| 87 | #endif |
| 88 | |
| 89 | int if_index = 0; |
| 90 | struct ifaddr **ifnet_addrs; |
| 91 | static char *sprint_d(); |
| 92 | |
| 93 | /* |
| 94 | * Attach an interface to the |
| 95 | * list of "active" interfaces. |
| 96 | */ |
| 97 | if_attach(ifp) |
| 98 | struct ifnet *ifp; |
| 99 | { |
| 100 | unsigned socksize, ifasize; |
| 101 | int namelen, unitlen; |
| 102 | char workbuf[12], *unitname; |
| 103 | register struct ifnet **p = &ifnet; |
| 104 | register struct sockaddr_dl *sdl; |
| 105 | register struct ifaddr *ifa; |
| 106 | static int if_indexlim = 8; |
| 107 | extern link_rtrequest(), ether_output(); |
| 108 | |
| 109 | while (*p) |
| 110 | p = &((*p)->if_next); |
| 111 | *p = ifp; |
| 112 | ifp->if_index = ++if_index; |
| 113 | if (ifnet_addrs == 0 || if_index >= if_indexlim) { |
| 114 | unsigned n = (if_indexlim <<= 1) * sizeof(ifa); |
| 115 | struct ifaddr **q = (struct ifaddr **) |
| 116 | malloc(n, M_IFADDR, M_WAITOK); |
| 117 | if (ifnet_addrs) { |
| 118 | bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2); |
| 119 | free((caddr_t)ifnet_addrs, M_IFADDR); |
| 120 | } |
| 121 | ifnet_addrs = q; |
| 122 | } |
| 123 | #if defined(INET) && NETHER > 0 |
| 124 | /* XXX -- Temporary fix before changing 10 ethernet drivers */ |
| 125 | if (ifp->if_output == ether_output) { |
| 126 | ifp->if_type = IFT_ETHER; |
| 127 | ifp->if_addrlen = 6; |
| 128 | ifp->if_hdrlen = 14; |
| 129 | } |
| 130 | #endif |
| 131 | /* |
| 132 | * create a Link Level name for this device |
| 133 | */ |
| 134 | unitname = sprint_d((u_int)ifp->if_unit, workbuf, sizeof(workbuf)); |
| 135 | namelen = strlen(ifp->if_name); |
| 136 | unitlen = strlen(unitname); |
| 137 | #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m)) |
| 138 | socksize = _offsetof(struct sockaddr_dl, sdl_data[0]) + |
| 139 | unitlen + namelen + ifp->if_addrlen; |
| 140 | #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1))) |
| 141 | socksize = ROUNDUP(socksize); |
| 142 | if (socksize < sizeof(*sdl)) |
| 143 | socksize = sizeof(*sdl); |
| 144 | ifasize = sizeof(*ifa) + 2 * socksize; |
| 145 | ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK); |
| 146 | if (ifa == 0) |
| 147 | return; |
| 148 | ifnet_addrs[if_index - 1] = ifa; |
| 149 | bzero((caddr_t)ifa, ifasize); |
| 150 | sdl = (struct sockaddr_dl *)(ifa + 1); |
| 151 | ifa->ifa_addr = (struct sockaddr *)sdl; |
| 152 | ifa->ifa_ifp = ifp; |
| 153 | sdl->sdl_len = socksize; |
| 154 | sdl->sdl_family = AF_LINK; |
| 155 | bcopy(ifp->if_name, sdl->sdl_data, namelen); |
| 156 | bcopy(unitname, namelen + (caddr_t)sdl->sdl_data, unitlen); |
| 157 | sdl->sdl_nlen = (namelen += unitlen); |
| 158 | sdl->sdl_index = ifp->if_index; |
| 159 | sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); |
| 160 | ifa->ifa_netmask = (struct sockaddr *)sdl; |
| 161 | sdl->sdl_len = socksize - ifp->if_addrlen; |
| 162 | while (namelen != 0) |
| 163 | sdl->sdl_data[--namelen] = 0xff; |
| 164 | ifa->ifa_next = ifp->if_addrlist; |
| 165 | ifa->ifa_rtrequest = link_rtrequest; |
| 166 | ifp->if_addrlist = ifa; |
| 167 | } |
| 168 | /* |
| 169 | * Locate an interface based on a complete address. |
| 170 | */ |
| 171 | /*ARGSUSED*/ |
| 172 | struct ifaddr * |
| 173 | ifa_ifwithaddr(addr) |
| 174 | register struct sockaddr *addr; |
| 175 | { |
| 176 | register struct ifnet *ifp; |
| 177 | register struct ifaddr *ifa; |
| 178 | |
| 179 | #define equal(a1, a2) \ |
| 180 | (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0) |
| 181 | for (ifp = ifnet; ifp; ifp = ifp->if_next) |
| 182 | for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) { |
| 183 | if (ifa->ifa_addr->sa_family != addr->sa_family) |
| 184 | continue; |
| 185 | if (equal(addr, ifa->ifa_addr)) |
| 186 | return (ifa); |
| 187 | if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr && |
| 188 | equal(ifa->ifa_broadaddr, addr)) |
| 189 | return (ifa); |
| 190 | } |
| 191 | return ((struct ifaddr *)0); |
| 192 | } |
| 193 | /* |
| 194 | * Locate the point to point interface with a given destination address. |
| 195 | */ |
| 196 | /*ARGSUSED*/ |
| 197 | struct ifaddr * |
| 198 | ifa_ifwithdstaddr(addr) |
| 199 | register struct sockaddr *addr; |
| 200 | { |
| 201 | register struct ifnet *ifp; |
| 202 | register struct ifaddr *ifa; |
| 203 | |
| 204 | for (ifp = ifnet; ifp; ifp = ifp->if_next) |
| 205 | if (ifp->if_flags & IFF_POINTOPOINT) |
| 206 | for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) { |
| 207 | if (ifa->ifa_addr->sa_family != addr->sa_family) |
| 208 | continue; |
| 209 | if (equal(addr, ifa->ifa_dstaddr)) |
| 210 | return (ifa); |
| 211 | } |
| 212 | return ((struct ifaddr *)0); |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Find an interface on a specific network. If many, choice |
| 217 | * is first found. |
| 218 | */ |
| 219 | struct ifaddr * |
| 220 | ifa_ifwithnet(addr) |
| 221 | struct sockaddr *addr; |
| 222 | { |
| 223 | register struct ifnet *ifp; |
| 224 | register struct ifaddr *ifa; |
| 225 | u_int af = addr->sa_family; |
| 226 | |
| 227 | if (af >= AF_MAX) |
| 228 | return (0); |
| 229 | if (af == AF_LINK) { |
| 230 | register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; |
| 231 | if (sdl->sdl_index && sdl->sdl_index <= if_index) |
| 232 | return (ifnet_addrs[sdl->sdl_index - 1]); |
| 233 | } |
| 234 | for (ifp = ifnet; ifp; ifp = ifp->if_next) |
| 235 | for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) { |
| 236 | register char *cp, *cp2, *cp3; |
| 237 | register char *cplim; |
| 238 | if (ifa->ifa_addr->sa_family != af || ifa->ifa_netmask == 0) |
| 239 | continue; |
| 240 | cp = addr->sa_data; |
| 241 | cp2 = ifa->ifa_addr->sa_data; |
| 242 | cp3 = ifa->ifa_netmask->sa_data; |
| 243 | cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; |
| 244 | for (; cp3 < cplim; cp3++) |
| 245 | if ((*cp++ ^ *cp2++) & *cp3) |
| 246 | break; |
| 247 | if (cp3 == cplim) |
| 248 | return (ifa); |
| 249 | } |
| 250 | return ((struct ifaddr *)0); |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * Find an interface using a specific address family |
| 255 | */ |
| 256 | struct ifaddr * |
| 257 | ifa_ifwithaf(af) |
| 258 | register int af; |
| 259 | { |
| 260 | register struct ifnet *ifp; |
| 261 | register struct ifaddr *ifa; |
| 262 | |
| 263 | for (ifp = ifnet; ifp; ifp = ifp->if_next) |
| 264 | for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) |
| 265 | if (ifa->ifa_addr->sa_family == af) |
| 266 | return (ifa); |
| 267 | return ((struct ifaddr *)0); |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | * Find an interface address specific to an interface best matching |
| 272 | * a given address. |
| 273 | */ |
| 274 | struct ifaddr * |
| 275 | ifaof_ifpforaddr(addr, ifp) |
| 276 | struct sockaddr *addr; |
| 277 | register struct ifnet *ifp; |
| 278 | { |
| 279 | register struct ifaddr *ifa; |
| 280 | register char *cp, *cp2, *cp3; |
| 281 | register char *cplim; |
| 282 | struct ifaddr *ifa_maybe = 0; |
| 283 | u_int af = addr->sa_family; |
| 284 | |
| 285 | if (af >= AF_MAX) |
| 286 | return (0); |
| 287 | for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) { |
| 288 | if (ifa->ifa_addr->sa_family != af) |
| 289 | continue; |
| 290 | ifa_maybe = ifa; |
| 291 | if (ifa->ifa_netmask == 0) { |
| 292 | if (equal(addr, ifa->ifa_addr) || |
| 293 | (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))) |
| 294 | return (ifa); |
| 295 | continue; |
| 296 | } |
| 297 | cp = addr->sa_data; |
| 298 | cp2 = ifa->ifa_addr->sa_data; |
| 299 | cp3 = ifa->ifa_netmask->sa_data; |
| 300 | cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; |
| 301 | for (; cp3 < cplim; cp3++) |
| 302 | if ((*cp++ ^ *cp2++) & *cp3) |
| 303 | break; |
| 304 | if (cp3 == cplim) |
| 305 | return (ifa); |
| 306 | } |
| 307 | return (ifa_maybe); |
| 308 | } |
| 309 | #include "route.h" |
| 310 | /* |
| 311 | * Default action when installing a route with a Link Level gateway. |
| 312 | * Lookup an appropriate real ifa to point to. |
| 313 | * This should be moved to /sys/net/link.c eventually. |
| 314 | */ |
| 315 | link_rtrequest(cmd, rt, sa) |
| 316 | register struct rtentry *rt; |
| 317 | struct sockaddr *sa; |
| 318 | { |
| 319 | register struct ifaddr *ifa; |
| 320 | struct sockaddr *dst; |
| 321 | struct ifnet *ifp, *oldifnet = ifnet; |
| 322 | |
| 323 | if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) || |
| 324 | ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) |
| 325 | return; |
| 326 | if (ifa = ifaof_ifpforaddr(dst, ifp)) { |
| 327 | rt->rt_ifa = ifa; |
| 328 | if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) |
| 329 | ifa->ifa_rtrequest(cmd, rt, sa); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * Mark an interface down and notify protocols of |
| 335 | * the transition. |
| 336 | * NOTE: must be called at splnet or eqivalent. |
| 337 | */ |
| 338 | if_down(ifp) |
| 339 | register struct ifnet *ifp; |
| 340 | { |
| 341 | register struct ifaddr *ifa; |
| 342 | |
| 343 | ifp->if_flags &= ~IFF_UP; |
| 344 | for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) |
| 345 | pfctlinput(PRC_IFDOWN, ifa->ifa_addr); |
| 346 | if_qflush(&ifp->if_snd); |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * Flush an interface queue. |
| 351 | */ |
| 352 | if_qflush(ifq) |
| 353 | register struct ifqueue *ifq; |
| 354 | { |
| 355 | register struct mbuf *m, *n; |
| 356 | |
| 357 | n = ifq->ifq_head; |
| 358 | while (m = n) { |
| 359 | n = m->m_act; |
| 360 | m_freem(m); |
| 361 | } |
| 362 | ifq->ifq_head = 0; |
| 363 | ifq->ifq_tail = 0; |
| 364 | ifq->ifq_len = 0; |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * Handle interface watchdog timer routines. Called |
| 369 | * from softclock, we decrement timers (if set) and |
| 370 | * call the appropriate interface routine on expiration. |
| 371 | */ |
| 372 | if_slowtimo() |
| 373 | { |
| 374 | register struct ifnet *ifp; |
| 375 | int s = splimp(); |
| 376 | |
| 377 | for (ifp = ifnet; ifp; ifp = ifp->if_next) { |
| 378 | if (ifp->if_timer == 0 || --ifp->if_timer) |
| 379 | continue; |
| 380 | if (ifp->if_watchdog) |
| 381 | (*ifp->if_watchdog)(ifp->if_unit); |
| 382 | } |
| 383 | splx(s); |
| 384 | timeout(if_slowtimo, (caddr_t)0, hz / IFNET_SLOWHZ); |
| 385 | } |
| 386 | |
| 387 | /* |
| 388 | * Map interface name to |
| 389 | * interface structure pointer. |
| 390 | */ |
| 391 | struct ifnet * |
| 392 | ifunit(name) |
| 393 | register char *name; |
| 394 | { |
| 395 | register char *cp; |
| 396 | register struct ifnet *ifp; |
| 397 | int unit; |
| 398 | unsigned len; |
| 399 | char *ep, c; |
| 400 | |
| 401 | for (cp = name; cp < name + IFNAMSIZ && *cp; cp++) |
| 402 | if (*cp >= '0' && *cp <= '9') |
| 403 | break; |
| 404 | if (*cp == '\0' || cp == name + IFNAMSIZ) |
| 405 | return ((struct ifnet *)0); |
| 406 | /* |
| 407 | * Save first char of unit, and pointer to it, |
| 408 | * so we can put a null there to avoid matching |
| 409 | * initial substrings of interface names. |
| 410 | */ |
| 411 | len = cp - name + 1; |
| 412 | c = *cp; |
| 413 | ep = cp; |
| 414 | for (unit = 0; *cp >= '0' && *cp <= '9'; ) |
| 415 | unit = unit * 10 + *cp++ - '0'; |
| 416 | *ep = 0; |
| 417 | for (ifp = ifnet; ifp; ifp = ifp->if_next) { |
| 418 | if (bcmp(ifp->if_name, name, len)) |
| 419 | continue; |
| 420 | if (unit == ifp->if_unit) |
| 421 | break; |
| 422 | } |
| 423 | *ep = c; |
| 424 | return (ifp); |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * Interface ioctls. |
| 429 | */ |
| 430 | ifioctl(so, cmd, data, p) |
| 431 | struct socket *so; |
| 432 | int cmd; |
| 433 | caddr_t data; |
| 434 | struct proc *p; |
| 435 | { |
| 436 | register struct ifnet *ifp; |
| 437 | register struct ifreq *ifr; |
| 438 | int error; |
| 439 | |
| 440 | switch (cmd) { |
| 441 | |
| 442 | case SIOCGIFCONF: |
| 443 | case OSIOCGIFCONF: |
| 444 | return (ifconf(cmd, data)); |
| 445 | |
| 446 | #if defined(INET) && NETHER > 0 |
| 447 | case SIOCSARP: |
| 448 | case SIOCDARP: |
| 449 | if (error = suser(p->p_ucred, &p->p_acflag)) |
| 450 | return (error); |
| 451 | /* FALL THROUGH */ |
| 452 | case SIOCGARP: |
| 453 | case OSIOCGARP: |
| 454 | return (arpioctl(cmd, data)); |
| 455 | #endif |
| 456 | } |
| 457 | ifr = (struct ifreq *)data; |
| 458 | ifp = ifunit(ifr->ifr_name); |
| 459 | if (ifp == 0) |
| 460 | return (ENXIO); |
| 461 | switch (cmd) { |
| 462 | |
| 463 | case SIOCGIFFLAGS: |
| 464 | ifr->ifr_flags = ifp->if_flags; |
| 465 | break; |
| 466 | |
| 467 | case SIOCGIFMETRIC: |
| 468 | ifr->ifr_metric = ifp->if_metric; |
| 469 | break; |
| 470 | |
| 471 | case SIOCSIFFLAGS: |
| 472 | if (error = suser(p->p_ucred, &p->p_acflag)) |
| 473 | return (error); |
| 474 | if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) { |
| 475 | int s = splimp(); |
| 476 | if_down(ifp); |
| 477 | splx(s); |
| 478 | } |
| 479 | ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | |
| 480 | (ifr->ifr_flags &~ IFF_CANTCHANGE); |
| 481 | if (ifp->if_ioctl) |
| 482 | (void) (*ifp->if_ioctl)(ifp, cmd, data); |
| 483 | break; |
| 484 | |
| 485 | case SIOCSIFMETRIC: |
| 486 | if (error = suser(p->p_ucred, &p->p_acflag)) |
| 487 | return (error); |
| 488 | ifp->if_metric = ifr->ifr_metric; |
| 489 | break; |
| 490 | |
| 491 | case SIOCSIFMTU: |
| 492 | case SIOCGIFMTU: |
| 493 | case SIOCSIFASYNCMAP: |
| 494 | case SIOCGIFASYNCMAP: |
| 495 | if (!ifp->if_ioctl) |
| 496 | return (EOPNOTSUPP); |
| 497 | return ((*ifp->if_ioctl)(ifp, cmd, data)); |
| 498 | break; |
| 499 | |
| 500 | default: |
| 501 | if (so->so_proto == 0) |
| 502 | return (EOPNOTSUPP); |
| 503 | #ifndef COMPAT_43 |
| 504 | return ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL, |
| 505 | cmd, data, ifp)); |
| 506 | #else |
| 507 | { |
| 508 | int ocmd = cmd; |
| 509 | |
| 510 | switch (cmd) { |
| 511 | |
| 512 | case SIOCSIFDSTADDR: |
| 513 | case SIOCSIFADDR: |
| 514 | case SIOCSIFBRDADDR: |
| 515 | case SIOCSIFNETMASK: |
| 516 | #if BYTE_ORDER != BIG_ENDIAN |
| 517 | if (ifr->ifr_addr.sa_family == 0 && |
| 518 | ifr->ifr_addr.sa_len < 16) { |
| 519 | ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; |
| 520 | ifr->ifr_addr.sa_len = 16; |
| 521 | } |
| 522 | #else |
| 523 | if (ifr->ifr_addr.sa_len == 0) |
| 524 | ifr->ifr_addr.sa_len = 16; |
| 525 | #endif |
| 526 | break; |
| 527 | |
| 528 | case OSIOCGIFADDR: |
| 529 | cmd = SIOCGIFADDR; |
| 530 | break; |
| 531 | |
| 532 | case OSIOCGIFDSTADDR: |
| 533 | cmd = SIOCGIFDSTADDR; |
| 534 | break; |
| 535 | |
| 536 | case OSIOCGIFBRDADDR: |
| 537 | cmd = SIOCGIFBRDADDR; |
| 538 | break; |
| 539 | |
| 540 | case OSIOCGIFNETMASK: |
| 541 | cmd = SIOCGIFNETMASK; |
| 542 | } |
| 543 | error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL, |
| 544 | cmd, data, ifp)); |
| 545 | switch (ocmd) { |
| 546 | |
| 547 | case OSIOCGIFADDR: |
| 548 | case OSIOCGIFDSTADDR: |
| 549 | case OSIOCGIFBRDADDR: |
| 550 | case OSIOCGIFNETMASK: |
| 551 | *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; |
| 552 | } |
| 553 | return (error); |
| 554 | |
| 555 | } |
| 556 | #endif |
| 557 | } |
| 558 | return (0); |
| 559 | } |
| 560 | |
| 561 | /* |
| 562 | * Return interface configuration |
| 563 | * of system. List may be used |
| 564 | * in later ioctl's (above) to get |
| 565 | * other information. |
| 566 | */ |
| 567 | /*ARGSUSED*/ |
| 568 | ifconf(cmd, data) |
| 569 | int cmd; |
| 570 | caddr_t data; |
| 571 | { |
| 572 | register struct ifconf *ifc = (struct ifconf *)data; |
| 573 | register struct ifnet *ifp = ifnet; |
| 574 | register struct ifaddr *ifa; |
| 575 | register char *cp, *ep; |
| 576 | struct ifreq ifr, *ifrp; |
| 577 | int space = ifc->ifc_len, error = 0; |
| 578 | |
| 579 | ifrp = ifc->ifc_req; |
| 580 | ep = ifr.ifr_name + sizeof (ifr.ifr_name) - 2; |
| 581 | for (; space > sizeof (ifr) && ifp; ifp = ifp->if_next) { |
| 582 | bcopy(ifp->if_name, ifr.ifr_name, sizeof (ifr.ifr_name) - 2); |
| 583 | for (cp = ifr.ifr_name; cp < ep && *cp; cp++) |
| 584 | ; |
| 585 | *cp++ = '0' + ifp->if_unit; *cp = '\0'; |
| 586 | if ((ifa = ifp->if_addrlist) == 0) { |
| 587 | bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); |
| 588 | error = copyout((caddr_t)&ifr, (caddr_t)ifrp, sizeof (ifr)); |
| 589 | if (error) |
| 590 | break; |
| 591 | space -= sizeof (ifr), ifrp++; |
| 592 | } else |
| 593 | for ( ; space > sizeof (ifr) && ifa; ifa = ifa->ifa_next) { |
| 594 | register struct sockaddr *sa = ifa->ifa_addr; |
| 595 | #ifdef COMPAT_43 |
| 596 | if (cmd == OSIOCGIFCONF) { |
| 597 | struct osockaddr *osa = |
| 598 | (struct osockaddr *)&ifr.ifr_addr; |
| 599 | ifr.ifr_addr = *sa; |
| 600 | osa->sa_family = sa->sa_family; |
| 601 | error = copyout((caddr_t)&ifr, (caddr_t)ifrp, |
| 602 | sizeof (ifr)); |
| 603 | ifrp++; |
| 604 | } else |
| 605 | #endif |
| 606 | if (sa->sa_len <= sizeof(*sa)) { |
| 607 | ifr.ifr_addr = *sa; |
| 608 | error = copyout((caddr_t)&ifr, (caddr_t)ifrp, |
| 609 | sizeof (ifr)); |
| 610 | ifrp++; |
| 611 | } else { |
| 612 | space -= sa->sa_len - sizeof(*sa); |
| 613 | if (space < sizeof (ifr)) |
| 614 | break; |
| 615 | error = copyout((caddr_t)&ifr, (caddr_t)ifrp, |
| 616 | sizeof (ifr.ifr_name)); |
| 617 | if (error == 0) |
| 618 | error = copyout((caddr_t)sa, |
| 619 | (caddr_t)&ifrp->ifr_addr, sa->sa_len); |
| 620 | ifrp = (struct ifreq *) |
| 621 | (sa->sa_len + (caddr_t)&ifrp->ifr_addr); |
| 622 | } |
| 623 | if (error) |
| 624 | break; |
| 625 | space -= sizeof (ifr); |
| 626 | } |
| 627 | } |
| 628 | ifc->ifc_len -= space; |
| 629 | return (error); |
| 630 | } |
| 631 | |
| 632 | static char * |
| 633 | sprint_d(n, buf, buflen) |
| 634 | u_int n; |
| 635 | char *buf; |
| 636 | int buflen; |
| 637 | { |
| 638 | register char *cp = buf + buflen - 1; |
| 639 | |
| 640 | *cp = 0; |
| 641 | do { |
| 642 | cp--; |
| 643 | *cp = "0123456789"[n % 10]; |
| 644 | n /= 10; |
| 645 | } while (n != 0); |
| 646 | return (cp); |
| 647 | } |