1 /*- 2 * 3 * Copyright (c) 1999-2001, Vitaly V Belekhov 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice unmodified, this list of conditions, and the following 11 * disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/errno.h> 34 #include <sys/kernel.h> 35 #include <sys/malloc.h> 36 #include <sys/mbuf.h> 37 #include <sys/errno.h> 38 #include <sys/sockio.h> 39 #include <sys/socket.h> 40 #include <sys/syslog.h> 41 #include <sys/vimage.h> 42 43 #include <net/if.h> 44 #include <net/if_types.h> 45 #include <net/netisr.h> 46 #include <net/route.h> 47 48 #include <netgraph/ng_message.h> 49 #include <netgraph/netgraph.h> 50 #include <netgraph/ng_parse.h> 51 #include <netgraph/ng_eiface.h> 52 53 #include <net/bpf.h> 54 #include <net/ethernet.h> 55 #include <net/if_arp.h> 56 57 static const struct ng_cmdlist ng_eiface_cmdlist[] = { 58 { 59 NGM_EIFACE_COOKIE, 60 NGM_EIFACE_GET_IFNAME, 61 "getifname", 62 NULL, 63 &ng_parse_string_type 64 }, 65 { 66 NGM_EIFACE_COOKIE, 67 NGM_EIFACE_SET, 68 "set", 69 &ng_parse_enaddr_type, 70 NULL 71 }, 72 { 0 } 73 }; 74 75 /* Node private data */ 76 struct ng_eiface_private { 77 struct ifnet *ifp; /* per-interface network data */ 78 int unit; /* Interface unit number */ 79 node_p node; /* Our netgraph node */ 80 hook_p ether; /* Hook for ethernet stream */ 81 }; 82 typedef struct ng_eiface_private *priv_p; 83 84 /* Interface methods */ 85 static void ng_eiface_init(void *xsc); 86 static void ng_eiface_start(struct ifnet *ifp); 87 static int ng_eiface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data); 88 #ifdef DEBUG 89 static void ng_eiface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data); 90 #endif 91 92 /* Netgraph methods */ 93 static int ng_eiface_mod_event(module_t, int, void *); 94 static ng_constructor_t ng_eiface_constructor; 95 static ng_rcvmsg_t ng_eiface_rcvmsg; 96 static ng_shutdown_t ng_eiface_rmnode; 97 static ng_newhook_t ng_eiface_newhook; 98 static ng_rcvdata_t ng_eiface_rcvdata; 99 static ng_disconnect_t ng_eiface_disconnect; 100 101 /* Node type descriptor */ 102 static struct ng_type typestruct = { 103 .version = NG_ABI_VERSION, 104 .name = NG_EIFACE_NODE_TYPE, 105 .mod_event = ng_eiface_mod_event, 106 .constructor = ng_eiface_constructor, 107 .rcvmsg = ng_eiface_rcvmsg, 108 .shutdown = ng_eiface_rmnode, 109 .newhook = ng_eiface_newhook, 110 .rcvdata = ng_eiface_rcvdata, 111 .disconnect = ng_eiface_disconnect, 112 .cmdlist = ng_eiface_cmdlist 113 }; 114 NETGRAPH_INIT(eiface, &typestruct); 115 116 static vnet_attach_fn ng_eiface_iattach; 117 static vnet_detach_fn ng_eiface_idetach; 118 119 #ifdef VIMAGE_GLOBALS 120 static struct unrhdr *ng_eiface_unit; 121 #endif 122 123 #ifndef VIMAGE_GLOBALS 124 static vnet_modinfo_t vnet_ng_eiface_modinfo = { 125 .vmi_id = VNET_MOD_NG_EIFACE, 126 .vmi_name = "ng_eiface", 127 .vmi_dependson = VNET_MOD_NETGRAPH, 128 .vmi_iattach = ng_eiface_iattach, 129 .vmi_idetach = ng_eiface_idetach 130 }; 131 #endif 132 133 /************************************************************************ 134 INTERFACE STUFF 135 ************************************************************************/ 136 137 /* 138 * Process an ioctl for the virtual interface 139 */ 140 static int 141 ng_eiface_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 142 { 143 struct ifreq *const ifr = (struct ifreq *)data; 144 int s, error = 0; 145 146 #ifdef DEBUG 147 ng_eiface_print_ioctl(ifp, command, data); 148 #endif 149 s = splimp(); 150 switch (command) { 151 152 /* These two are mostly handled at a higher layer */ 153 case SIOCSIFADDR: 154 error = ether_ioctl(ifp, command, data); 155 break; 156 case SIOCGIFADDR: 157 break; 158 159 /* Set flags */ 160 case SIOCSIFFLAGS: 161 /* 162 * If the interface is marked up and stopped, then start it. 163 * If it is marked down and running, then stop it. 164 */ 165 if (ifp->if_flags & IFF_UP) { 166 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 167 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE); 168 ifp->if_drv_flags |= IFF_DRV_RUNNING; 169 } 170 } else { 171 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 172 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | 173 IFF_DRV_OACTIVE); 174 } 175 break; 176 177 /* Set the interface MTU */ 178 case SIOCSIFMTU: 179 if (ifr->ifr_mtu > NG_EIFACE_MTU_MAX || 180 ifr->ifr_mtu < NG_EIFACE_MTU_MIN) 181 error = EINVAL; 182 else 183 ifp->if_mtu = ifr->ifr_mtu; 184 break; 185 186 /* Stuff that's not supported */ 187 case SIOCADDMULTI: 188 case SIOCDELMULTI: 189 error = 0; 190 break; 191 case SIOCSIFPHYS: 192 error = EOPNOTSUPP; 193 break; 194 195 default: 196 error = EINVAL; 197 break; 198 } 199 splx(s); 200 return (error); 201 } 202 203 static void 204 ng_eiface_init(void *xsc) 205 { 206 priv_p sc = xsc; 207 struct ifnet *ifp = sc->ifp; 208 int s; 209 210 s = splimp(); 211 212 ifp->if_drv_flags |= IFF_DRV_RUNNING; 213 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 214 215 splx(s); 216 } 217 218 /* 219 * We simply relay the packet to the "ether" hook, if it is connected. 220 * We have been through the netgraph locking and are guaranteed to 221 * be the only code running in this node at this time. 222 */ 223 static void 224 ng_eiface_start2(node_p node, hook_p hook, void *arg1, int arg2) 225 { 226 struct ifnet *ifp = arg1; 227 const priv_p priv = (priv_p)ifp->if_softc; 228 int error = 0; 229 struct mbuf *m; 230 231 /* Check interface flags */ 232 233 if (!((ifp->if_flags & IFF_UP) && 234 (ifp->if_drv_flags & IFF_DRV_RUNNING))) 235 return; 236 237 for (;;) { 238 /* 239 * Grab a packet to transmit. 240 */ 241 IF_DEQUEUE(&ifp->if_snd, m); 242 243 /* If there's nothing to send, break. */ 244 if (m == NULL) 245 break; 246 247 /* 248 * Berkeley packet filter. 249 * Pass packet to bpf if there is a listener. 250 * XXX is this safe? locking? 251 */ 252 BPF_MTAP(ifp, m); 253 254 if (ifp->if_flags & IFF_MONITOR) { 255 ifp->if_ipackets++; 256 m_freem(m); 257 continue; 258 } 259 260 /* 261 * Send packet; if hook is not connected, mbuf will get 262 * freed. 263 */ 264 NG_SEND_DATA_ONLY(error, priv->ether, m); 265 266 /* Update stats */ 267 if (error == 0) 268 ifp->if_opackets++; 269 else 270 ifp->if_oerrors++; 271 } 272 273 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 274 275 return; 276 } 277 278 /* 279 * This routine is called to deliver a packet out the interface. 280 * We simply queue the netgraph version to be called when netgraph locking 281 * allows it to happen. 282 * Until we know what the rest of the networking code is doing for 283 * locking, we don't know how we will interact with it. 284 * Take comfort from the fact that the ifnet struct is part of our 285 * private info and can't go away while we are queued. 286 * [Though we don't know it is still there now....] 287 * it is possible we don't gain anything from this because 288 * we would like to get the mbuf and queue it as data 289 * somehow, but we can't and if we did would we solve anything? 290 */ 291 static void 292 ng_eiface_start(struct ifnet *ifp) 293 { 294 295 const priv_p priv = (priv_p)ifp->if_softc; 296 297 /* Don't do anything if output is active */ 298 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) 299 return; 300 301 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 302 303 if (ng_send_fn(priv->node, NULL, &ng_eiface_start2, ifp, 0) != 0) 304 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 305 } 306 307 #ifdef DEBUG 308 /* 309 * Display an ioctl to the virtual interface 310 */ 311 312 static void 313 ng_eiface_print_ioctl(struct ifnet *ifp, int command, caddr_t data) 314 { 315 char *str; 316 317 switch (command & IOC_DIRMASK) { 318 case IOC_VOID: 319 str = "IO"; 320 break; 321 case IOC_OUT: 322 str = "IOR"; 323 break; 324 case IOC_IN: 325 str = "IOW"; 326 break; 327 case IOC_INOUT: 328 str = "IORW"; 329 break; 330 default: 331 str = "IO??"; 332 } 333 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n", 334 ifp->if_xname, 335 str, 336 IOCGROUP(command), 337 command & 0xff, 338 IOCPARM_LEN(command)); 339 } 340 #endif /* DEBUG */ 341 342 /************************************************************************ 343 NETGRAPH NODE STUFF 344 ************************************************************************/ 345 346 /* 347 * Constructor for a node 348 */ 349 static int 350 ng_eiface_constructor(node_p node) 351 { 352 INIT_VNET_NETGRAPH(curvnet); 353 struct ifnet *ifp; 354 priv_p priv; 355 u_char eaddr[6] = {0,0,0,0,0,0}; 356 357 /* Allocate node and interface private structures */ 358 priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 359 if (priv == NULL) 360 return (ENOMEM); 361 362 ifp = priv->ifp = if_alloc(IFT_ETHER); 363 if (ifp == NULL) { 364 free(priv, M_NETGRAPH); 365 return (ENOSPC); 366 } 367 368 /* Link them together */ 369 ifp->if_softc = priv; 370 371 /* Get an interface unit number */ 372 priv->unit = alloc_unr(V_ng_eiface_unit); 373 374 /* Link together node and private info */ 375 NG_NODE_SET_PRIVATE(node, priv); 376 priv->node = node; 377 378 /* Initialize interface structure */ 379 if_initname(ifp, NG_EIFACE_EIFACE_NAME, priv->unit); 380 ifp->if_init = ng_eiface_init; 381 ifp->if_output = ether_output; 382 ifp->if_start = ng_eiface_start; 383 ifp->if_ioctl = ng_eiface_ioctl; 384 ifp->if_watchdog = NULL; 385 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; 386 ifp->if_flags = (IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST); 387 388 #if 0 389 /* Give this node name */ 390 bzero(ifname, sizeof(ifname)); 391 sprintf(ifname, "if%s", ifp->if_xname); 392 (void)ng_name_node(node, ifname); 393 #endif 394 395 /* Attach the interface */ 396 ether_ifattach(ifp, eaddr); 397 398 /* Done */ 399 return (0); 400 } 401 402 /* 403 * Give our ok for a hook to be added 404 */ 405 static int 406 ng_eiface_newhook(node_p node, hook_p hook, const char *name) 407 { 408 priv_p priv = NG_NODE_PRIVATE(node); 409 struct ifnet *ifp = priv->ifp; 410 411 if (strcmp(name, NG_EIFACE_HOOK_ETHER)) 412 return (EPFNOSUPPORT); 413 if (priv->ether != NULL) 414 return (EISCONN); 415 priv->ether = hook; 416 NG_HOOK_SET_PRIVATE(hook, &priv->ether); 417 418 if_link_state_change(ifp, LINK_STATE_UP); 419 420 return (0); 421 } 422 423 /* 424 * Receive a control message 425 */ 426 static int 427 ng_eiface_rcvmsg(node_p node, item_p item, hook_p lasthook) 428 { 429 const priv_p priv = NG_NODE_PRIVATE(node); 430 struct ifnet *const ifp = priv->ifp; 431 struct ng_mesg *resp = NULL; 432 int error = 0; 433 struct ng_mesg *msg; 434 435 NGI_GET_MSG(item, msg); 436 switch (msg->header.typecookie) { 437 case NGM_EIFACE_COOKIE: 438 switch (msg->header.cmd) { 439 440 case NGM_EIFACE_SET: 441 { 442 if (msg->header.arglen != ETHER_ADDR_LEN) { 443 error = EINVAL; 444 break; 445 } 446 error = if_setlladdr(priv->ifp, 447 (u_char *)msg->data, ETHER_ADDR_LEN); 448 break; 449 } 450 451 case NGM_EIFACE_GET_IFNAME: 452 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT); 453 if (resp == NULL) { 454 error = ENOMEM; 455 break; 456 } 457 strlcpy(resp->data, ifp->if_xname, IFNAMSIZ); 458 break; 459 460 case NGM_EIFACE_GET_IFADDRS: 461 { 462 struct ifaddr *ifa; 463 caddr_t ptr; 464 int buflen; 465 466 /* Determine size of response and allocate it */ 467 buflen = 0; 468 IF_ADDR_LOCK(ifp); 469 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 470 buflen += SA_SIZE(ifa->ifa_addr); 471 NG_MKRESPONSE(resp, msg, buflen, M_NOWAIT); 472 if (resp == NULL) { 473 IF_ADDR_UNLOCK(ifp); 474 error = ENOMEM; 475 break; 476 } 477 478 /* Add addresses */ 479 ptr = resp->data; 480 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 481 const int len = SA_SIZE(ifa->ifa_addr); 482 483 if (buflen < len) { 484 log(LOG_ERR, "%s: len changed?\n", 485 ifp->if_xname); 486 break; 487 } 488 bcopy(ifa->ifa_addr, ptr, len); 489 ptr += len; 490 buflen -= len; 491 } 492 IF_ADDR_UNLOCK(ifp); 493 break; 494 } 495 496 default: 497 error = EINVAL; 498 break; 499 } /* end of inner switch() */ 500 break; 501 case NGM_FLOW_COOKIE: 502 switch (msg->header.cmd) { 503 case NGM_LINK_IS_UP: 504 if_link_state_change(ifp, LINK_STATE_UP); 505 break; 506 case NGM_LINK_IS_DOWN: 507 if_link_state_change(ifp, LINK_STATE_DOWN); 508 break; 509 default: 510 break; 511 } 512 break; 513 default: 514 error = EINVAL; 515 break; 516 } 517 NG_RESPOND_MSG(error, node, item, resp); 518 NG_FREE_MSG(msg); 519 return (error); 520 } 521 522 /* 523 * Receive data from a hook. Pass the packet to the ether_input routine. 524 */ 525 static int 526 ng_eiface_rcvdata(hook_p hook, item_p item) 527 { 528 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 529 struct ifnet *const ifp = priv->ifp; 530 struct mbuf *m; 531 532 NGI_GET_M(item, m); 533 NG_FREE_ITEM(item); 534 535 if (!((ifp->if_flags & IFF_UP) && 536 (ifp->if_drv_flags & IFF_DRV_RUNNING))) { 537 NG_FREE_M(m); 538 return (ENETDOWN); 539 } 540 541 if (m->m_len < ETHER_HDR_LEN) { 542 m = m_pullup(m, ETHER_HDR_LEN); 543 if (m == NULL) 544 return (EINVAL); 545 } 546 547 /* Note receiving interface */ 548 m->m_pkthdr.rcvif = ifp; 549 550 /* Update interface stats */ 551 ifp->if_ipackets++; 552 553 (*ifp->if_input)(ifp, m); 554 555 /* Done */ 556 return (0); 557 } 558 559 /* 560 * Shutdown processing. 561 */ 562 static int 563 ng_eiface_rmnode(node_p node) 564 { 565 INIT_VNET_NETGRAPH(curvnet); 566 const priv_p priv = NG_NODE_PRIVATE(node); 567 struct ifnet *const ifp = priv->ifp; 568 569 /* 570 * the ifnet may be in a different vnet than the netgraph node, 571 * hence we have to change the current vnet context here. 572 */ 573 CURVNET_SET_QUIET(ifp->if_vnet); 574 ether_ifdetach(ifp); 575 if_free(ifp); 576 CURVNET_RESTORE(); 577 free_unr(V_ng_eiface_unit, priv->unit); 578 free(priv, M_NETGRAPH); 579 NG_NODE_SET_PRIVATE(node, NULL); 580 NG_NODE_UNREF(node); 581 return (0); 582 } 583 584 /* 585 * Hook disconnection 586 */ 587 static int 588 ng_eiface_disconnect(hook_p hook) 589 { 590 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 591 592 priv->ether = NULL; 593 return (0); 594 } 595 596 /* 597 * Handle loading and unloading for this node type. 598 */ 599 static int 600 ng_eiface_mod_event(module_t mod, int event, void *data) 601 { 602 int error = 0; 603 604 switch (event) { 605 case MOD_LOAD: 606 #ifndef VIMAGE_GLOBALS 607 vnet_mod_register(&vnet_ng_eiface_modinfo); 608 #else 609 ng_eiface_iattach(NULL); 610 #endif 611 break; 612 case MOD_UNLOAD: 613 #ifndef VIMAGE_GLOBALS 614 vnet_mod_deregister(&vnet_ng_eiface_modinfo); 615 #else 616 ng_eiface_idetach(NULL); 617 #endif 618 break; 619 default: 620 error = EOPNOTSUPP; 621 break; 622 } 623 return (error); 624 } 625 626 static int ng_eiface_iattach(const void *unused) 627 { 628 INIT_VNET_NETGRAPH(curvnet); 629 630 V_ng_eiface_unit = new_unrhdr(0, 0xffff, NULL); 631 632 return (0); 633 } 634 635 static int ng_eiface_idetach(const void *unused) 636 { 637 INIT_VNET_NETGRAPH(curvnet); 638 639 delete_unrhdr(V_ng_eiface_unit); 640 641 return (0); 642 } 643