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/eventhandler.h> 33 #include <sys/systm.h> 34 #include <sys/errno.h> 35 #include <sys/kernel.h> 36 #include <sys/malloc.h> 37 #include <sys/mbuf.h> 38 #include <sys/errno.h> 39 #include <sys/proc.h> 40 #include <sys/sockio.h> 41 #include <sys/socket.h> 42 #include <sys/syslog.h> 43 44 #include <net/if.h> 45 #include <net/if_var.h> 46 #include <net/if_media.h> 47 #include <net/if_types.h> 48 #include <net/netisr.h> 49 #include <net/route.h> 50 #include <net/vnet.h> 51 52 #include <netgraph/ng_message.h> 53 #include <netgraph/netgraph.h> 54 #include <netgraph/ng_parse.h> 55 #include <netgraph/ng_eiface.h> 56 57 #include <net/bpf.h> 58 #include <net/ethernet.h> 59 #include <net/if_arp.h> 60 61 static const struct ng_cmdlist ng_eiface_cmdlist[] = { 62 { 63 NGM_EIFACE_COOKIE, 64 NGM_EIFACE_GET_IFNAME, 65 "getifname", 66 NULL, 67 &ng_parse_string_type 68 }, 69 { 70 NGM_EIFACE_COOKIE, 71 NGM_EIFACE_SET, 72 "set", 73 &ng_parse_enaddr_type, 74 NULL 75 }, 76 { 0 } 77 }; 78 79 /* Node private data */ 80 struct ng_eiface_private { 81 struct ifnet *ifp; /* per-interface network data */ 82 struct ifmedia media; /* (fake) media information */ 83 int link_status; /* fake */ 84 int unit; /* Interface unit number */ 85 node_p node; /* Our netgraph node */ 86 hook_p ether; /* Hook for ethernet stream */ 87 }; 88 typedef struct ng_eiface_private *priv_p; 89 90 /* Interface methods */ 91 static void ng_eiface_init(void *xsc); 92 static void ng_eiface_start(struct ifnet *ifp); 93 static int ng_eiface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data); 94 #ifdef DEBUG 95 static void ng_eiface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data); 96 #endif 97 98 /* Netgraph methods */ 99 static int ng_eiface_mod_event(module_t, int, void *); 100 static ng_constructor_t ng_eiface_constructor; 101 static ng_rcvmsg_t ng_eiface_rcvmsg; 102 static ng_shutdown_t ng_eiface_rmnode; 103 static ng_newhook_t ng_eiface_newhook; 104 static ng_rcvdata_t ng_eiface_rcvdata; 105 static ng_disconnect_t ng_eiface_disconnect; 106 107 /* Node type descriptor */ 108 static struct ng_type typestruct = { 109 .version = NG_ABI_VERSION, 110 .name = NG_EIFACE_NODE_TYPE, 111 .mod_event = ng_eiface_mod_event, 112 .constructor = ng_eiface_constructor, 113 .rcvmsg = ng_eiface_rcvmsg, 114 .shutdown = ng_eiface_rmnode, 115 .newhook = ng_eiface_newhook, 116 .rcvdata = ng_eiface_rcvdata, 117 .disconnect = ng_eiface_disconnect, 118 .cmdlist = ng_eiface_cmdlist 119 }; 120 NETGRAPH_INIT(eiface, &typestruct); 121 122 static VNET_DEFINE(struct unrhdr *, ng_eiface_unit); 123 #define V_ng_eiface_unit VNET(ng_eiface_unit) 124 125 /************************************************************************ 126 INTERFACE STUFF 127 ************************************************************************/ 128 129 /* 130 * Process an ioctl for the virtual interface 131 */ 132 static int 133 ng_eiface_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 134 { 135 const priv_p priv = (priv_p)ifp->if_softc; 136 struct ifreq *const ifr = (struct ifreq *)data; 137 int error = 0; 138 139 #ifdef DEBUG 140 ng_eiface_print_ioctl(ifp, command, data); 141 #endif 142 switch (command) { 143 144 /* These two are mostly handled at a higher layer */ 145 case SIOCSIFADDR: 146 error = ether_ioctl(ifp, command, data); 147 break; 148 case SIOCGIFADDR: 149 break; 150 151 /* Set flags */ 152 case SIOCSIFFLAGS: 153 /* 154 * If the interface is marked up and stopped, then start it. 155 * If it is marked down and running, then stop it. 156 */ 157 if (ifp->if_flags & IFF_UP) { 158 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 159 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE); 160 ifp->if_drv_flags |= IFF_DRV_RUNNING; 161 } 162 } else { 163 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 164 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | 165 IFF_DRV_OACTIVE); 166 } 167 break; 168 169 /* Set the interface MTU */ 170 case SIOCSIFMTU: 171 if (ifr->ifr_mtu > NG_EIFACE_MTU_MAX || 172 ifr->ifr_mtu < NG_EIFACE_MTU_MIN) 173 error = EINVAL; 174 else 175 ifp->if_mtu = ifr->ifr_mtu; 176 break; 177 178 /* (Fake) media type manipulation */ 179 case SIOCSIFMEDIA: 180 case SIOCGIFMEDIA: 181 error = ifmedia_ioctl(ifp, ifr, &priv->media, command); 182 break; 183 184 /* Stuff that's not supported */ 185 case SIOCADDMULTI: 186 case SIOCDELMULTI: 187 error = 0; 188 break; 189 case SIOCSIFPHYS: 190 error = EOPNOTSUPP; 191 break; 192 193 default: 194 error = EINVAL; 195 break; 196 } 197 return (error); 198 } 199 200 static void 201 ng_eiface_init(void *xsc) 202 { 203 priv_p sc = xsc; 204 struct ifnet *ifp = sc->ifp; 205 206 ifp->if_drv_flags |= IFF_DRV_RUNNING; 207 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 208 } 209 210 /* 211 * We simply relay the packet to the "ether" hook, if it is connected. 212 * We have been through the netgraph locking and are guaranteed to 213 * be the only code running in this node at this time. 214 */ 215 static void 216 ng_eiface_start2(node_p node, hook_p hook, void *arg1, int arg2) 217 { 218 struct ifnet *ifp = arg1; 219 const priv_p priv = (priv_p)ifp->if_softc; 220 int error = 0; 221 struct mbuf *m; 222 223 /* Check interface flags */ 224 225 if (!((ifp->if_flags & IFF_UP) && 226 (ifp->if_drv_flags & IFF_DRV_RUNNING))) 227 return; 228 229 for (;;) { 230 /* 231 * Grab a packet to transmit. 232 */ 233 IF_DEQUEUE(&ifp->if_snd, m); 234 235 /* If there's nothing to send, break. */ 236 if (m == NULL) 237 break; 238 239 /* Peel the mbuf off any stale tags */ 240 m_tag_delete_chain(m, NULL); 241 242 /* 243 * Berkeley packet filter. 244 * Pass packet to bpf if there is a listener. 245 * XXX is this safe? locking? 246 */ 247 BPF_MTAP(ifp, m); 248 249 if (ifp->if_flags & IFF_MONITOR) { 250 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 251 m_freem(m); 252 continue; 253 } 254 255 /* 256 * Send packet; if hook is not connected, mbuf will get 257 * freed. 258 */ 259 NG_OUTBOUND_THREAD_REF(); 260 NG_SEND_DATA_ONLY(error, priv->ether, m); 261 NG_OUTBOUND_THREAD_UNREF(); 262 263 /* Update stats */ 264 if (error == 0) 265 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 266 else 267 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 268 } 269 270 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 271 272 return; 273 } 274 275 /* 276 * This routine is called to deliver a packet out the interface. 277 * We simply queue the netgraph version to be called when netgraph locking 278 * allows it to happen. 279 * Until we know what the rest of the networking code is doing for 280 * locking, we don't know how we will interact with it. 281 * Take comfort from the fact that the ifnet struct is part of our 282 * private info and can't go away while we are queued. 283 * [Though we don't know it is still there now....] 284 * it is possible we don't gain anything from this because 285 * we would like to get the mbuf and queue it as data 286 * somehow, but we can't and if we did would we solve anything? 287 */ 288 static void 289 ng_eiface_start(struct ifnet *ifp) 290 { 291 const priv_p priv = (priv_p)ifp->if_softc; 292 293 /* Don't do anything if output is active */ 294 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) 295 return; 296 297 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 298 299 if (ng_send_fn(priv->node, NULL, &ng_eiface_start2, ifp, 0) != 0) 300 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 301 } 302 303 #ifdef DEBUG 304 /* 305 * Display an ioctl to the virtual interface 306 */ 307 308 static void 309 ng_eiface_print_ioctl(struct ifnet *ifp, int command, caddr_t data) 310 { 311 char *str; 312 313 switch (command & IOC_DIRMASK) { 314 case IOC_VOID: 315 str = "IO"; 316 break; 317 case IOC_OUT: 318 str = "IOR"; 319 break; 320 case IOC_IN: 321 str = "IOW"; 322 break; 323 case IOC_INOUT: 324 str = "IORW"; 325 break; 326 default: 327 str = "IO??"; 328 } 329 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n", 330 ifp->if_xname, 331 str, 332 IOCGROUP(command), 333 command & 0xff, 334 IOCPARM_LEN(command)); 335 } 336 #endif /* DEBUG */ 337 338 /* 339 * ifmedia stuff 340 */ 341 static int 342 ng_eiface_mediachange(struct ifnet *ifp) 343 { 344 const priv_p priv = (priv_p)ifp->if_softc; 345 struct ifmedia *ifm = &priv->media; 346 347 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) 348 return (EINVAL); 349 if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO) 350 ifp->if_baudrate = ifmedia_baudrate(IFM_ETHER | IFM_1000_T); 351 else 352 ifp->if_baudrate = ifmedia_baudrate(ifm->ifm_media); 353 354 return (0); 355 } 356 357 static void 358 ng_eiface_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr) 359 { 360 const priv_p priv = (priv_p)ifp->if_softc; 361 struct ifmedia *ifm = &priv->media; 362 363 if (ifm->ifm_cur->ifm_media == (IFM_ETHER | IFM_AUTO) && 364 (priv->link_status & IFM_ACTIVE)) 365 ifmr->ifm_active = IFM_ETHER | IFM_1000_T | IFM_FDX; 366 else 367 ifmr->ifm_active = ifm->ifm_cur->ifm_media; 368 ifmr->ifm_status = priv->link_status; 369 370 return; 371 } 372 373 /************************************************************************ 374 NETGRAPH NODE STUFF 375 ************************************************************************/ 376 377 /* 378 * Constructor for a node 379 */ 380 static int 381 ng_eiface_constructor(node_p node) 382 { 383 struct ifnet *ifp; 384 priv_p priv; 385 u_char eaddr[6] = {0,0,0,0,0,0}; 386 387 /* Allocate node and interface private structures */ 388 priv = malloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO); 389 390 ifp = priv->ifp = if_alloc(IFT_ETHER); 391 if (ifp == NULL) { 392 free(priv, M_NETGRAPH); 393 return (ENOSPC); 394 } 395 396 /* Link them together */ 397 ifp->if_softc = priv; 398 399 /* Get an interface unit number */ 400 priv->unit = alloc_unr(V_ng_eiface_unit); 401 402 /* Link together node and private info */ 403 NG_NODE_SET_PRIVATE(node, priv); 404 priv->node = node; 405 406 /* Initialize interface structure */ 407 if_initname(ifp, NG_EIFACE_EIFACE_NAME, priv->unit); 408 ifp->if_init = ng_eiface_init; 409 ifp->if_output = ether_output; 410 ifp->if_start = ng_eiface_start; 411 ifp->if_ioctl = ng_eiface_ioctl; 412 ifp->if_snd.ifq_maxlen = ifqmaxlen; 413 ifp->if_flags = (IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST); 414 ifp->if_capabilities = IFCAP_VLAN_MTU | IFCAP_JUMBO_MTU; 415 ifp->if_capenable = IFCAP_VLAN_MTU | IFCAP_JUMBO_MTU; 416 ifmedia_init(&priv->media, 0, ng_eiface_mediachange, 417 ng_eiface_mediastatus); 418 ifmedia_add(&priv->media, IFM_ETHER | IFM_10_T, 0, NULL); 419 ifmedia_add(&priv->media, IFM_ETHER | IFM_10_T | IFM_FDX, 0, NULL); 420 ifmedia_add(&priv->media, IFM_ETHER | IFM_100_TX, 0, NULL); 421 ifmedia_add(&priv->media, IFM_ETHER | IFM_100_TX | IFM_FDX, 0, NULL); 422 ifmedia_add(&priv->media, IFM_ETHER | IFM_1000_T, 0, NULL); 423 ifmedia_add(&priv->media, IFM_ETHER | IFM_1000_T | IFM_FDX, 0, NULL); 424 ifmedia_add(&priv->media, IFM_ETHER | IFM_10G_T | IFM_FDX, 0, NULL); 425 ifmedia_add(&priv->media, IFM_ETHER | IFM_AUTO, 0, NULL); 426 ifmedia_set(&priv->media, IFM_ETHER | IFM_AUTO); 427 priv->link_status = IFM_AVALID; 428 429 /* Give this node the same name as the interface (if possible) */ 430 if (ng_name_node(node, ifp->if_xname) != 0) 431 log(LOG_WARNING, "%s: can't acquire netgraph name\n", 432 ifp->if_xname); 433 434 /* Attach the interface */ 435 ether_ifattach(ifp, eaddr); 436 ifp->if_baudrate = ifmedia_baudrate(IFM_ETHER | IFM_1000_T); 437 438 /* Done */ 439 return (0); 440 } 441 442 /* 443 * Give our ok for a hook to be added 444 */ 445 static int 446 ng_eiface_newhook(node_p node, hook_p hook, const char *name) 447 { 448 priv_p priv = NG_NODE_PRIVATE(node); 449 struct ifnet *ifp = priv->ifp; 450 451 if (strcmp(name, NG_EIFACE_HOOK_ETHER)) 452 return (EPFNOSUPPORT); 453 if (priv->ether != NULL) 454 return (EISCONN); 455 priv->ether = hook; 456 NG_HOOK_SET_PRIVATE(hook, &priv->ether); 457 NG_HOOK_SET_TO_INBOUND(hook); 458 459 priv->link_status |= IFM_ACTIVE; 460 CURVNET_SET_QUIET(ifp->if_vnet); 461 if_link_state_change(ifp, LINK_STATE_UP); 462 CURVNET_RESTORE(); 463 464 return (0); 465 } 466 467 /* 468 * Receive a control message 469 */ 470 static int 471 ng_eiface_rcvmsg(node_p node, item_p item, hook_p lasthook) 472 { 473 const priv_p priv = NG_NODE_PRIVATE(node); 474 struct ifnet *const ifp = priv->ifp; 475 struct ng_mesg *resp = NULL; 476 int error = 0; 477 struct ng_mesg *msg; 478 479 NGI_GET_MSG(item, msg); 480 switch (msg->header.typecookie) { 481 case NGM_EIFACE_COOKIE: 482 switch (msg->header.cmd) { 483 484 case NGM_EIFACE_SET: 485 { 486 if (msg->header.arglen != ETHER_ADDR_LEN) { 487 error = EINVAL; 488 break; 489 } 490 error = if_setlladdr(priv->ifp, 491 (u_char *)msg->data, ETHER_ADDR_LEN); 492 EVENTHANDLER_INVOKE(iflladdr_event, priv->ifp); 493 break; 494 } 495 496 case NGM_EIFACE_GET_IFNAME: 497 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT); 498 if (resp == NULL) { 499 error = ENOMEM; 500 break; 501 } 502 strlcpy(resp->data, ifp->if_xname, IFNAMSIZ); 503 break; 504 505 case NGM_EIFACE_GET_IFADDRS: 506 { 507 struct ifaddr *ifa; 508 caddr_t ptr; 509 int buflen; 510 511 /* Determine size of response and allocate it */ 512 buflen = 0; 513 if_addr_rlock(ifp); 514 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 515 buflen += SA_SIZE(ifa->ifa_addr); 516 NG_MKRESPONSE(resp, msg, buflen, M_NOWAIT); 517 if (resp == NULL) { 518 if_addr_runlock(ifp); 519 error = ENOMEM; 520 break; 521 } 522 523 /* Add addresses */ 524 ptr = resp->data; 525 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 526 const int len = SA_SIZE(ifa->ifa_addr); 527 528 if (buflen < len) { 529 log(LOG_ERR, "%s: len changed?\n", 530 ifp->if_xname); 531 break; 532 } 533 bcopy(ifa->ifa_addr, ptr, len); 534 ptr += len; 535 buflen -= len; 536 } 537 if_addr_runlock(ifp); 538 break; 539 } 540 541 default: 542 error = EINVAL; 543 break; 544 } /* end of inner switch() */ 545 break; 546 case NGM_FLOW_COOKIE: 547 CURVNET_SET_QUIET(ifp->if_vnet); 548 switch (msg->header.cmd) { 549 case NGM_LINK_IS_UP: 550 priv->link_status |= IFM_ACTIVE; 551 if_link_state_change(ifp, LINK_STATE_UP); 552 break; 553 case NGM_LINK_IS_DOWN: 554 priv->link_status &= ~IFM_ACTIVE; 555 if_link_state_change(ifp, LINK_STATE_DOWN); 556 break; 557 default: 558 break; 559 } 560 CURVNET_RESTORE(); 561 break; 562 default: 563 error = EINVAL; 564 break; 565 } 566 NG_RESPOND_MSG(error, node, item, resp); 567 NG_FREE_MSG(msg); 568 return (error); 569 } 570 571 /* 572 * Receive data from a hook. Pass the packet to the ether_input routine. 573 */ 574 static int 575 ng_eiface_rcvdata(hook_p hook, item_p item) 576 { 577 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 578 struct ifnet *const ifp = priv->ifp; 579 struct mbuf *m; 580 581 NGI_GET_M(item, m); 582 NG_FREE_ITEM(item); 583 584 if (!((ifp->if_flags & IFF_UP) && 585 (ifp->if_drv_flags & IFF_DRV_RUNNING))) { 586 NG_FREE_M(m); 587 return (ENETDOWN); 588 } 589 590 if (m->m_len < ETHER_HDR_LEN) { 591 m = m_pullup(m, ETHER_HDR_LEN); 592 if (m == NULL) 593 return (EINVAL); 594 } 595 596 /* Note receiving interface */ 597 m->m_pkthdr.rcvif = ifp; 598 599 /* Update interface stats */ 600 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 601 602 (*ifp->if_input)(ifp, m); 603 604 /* Done */ 605 return (0); 606 } 607 608 /* 609 * Shutdown processing. 610 */ 611 static int 612 ng_eiface_rmnode(node_p node) 613 { 614 const priv_p priv = NG_NODE_PRIVATE(node); 615 struct ifnet *const ifp = priv->ifp; 616 617 /* 618 * the ifnet may be in a different vnet than the netgraph node, 619 * hence we have to change the current vnet context here. 620 */ 621 CURVNET_SET_QUIET(ifp->if_vnet); 622 ifmedia_removeall(&priv->media); 623 ether_ifdetach(ifp); 624 if_free(ifp); 625 CURVNET_RESTORE(); 626 free_unr(V_ng_eiface_unit, priv->unit); 627 free(priv, M_NETGRAPH); 628 NG_NODE_SET_PRIVATE(node, NULL); 629 NG_NODE_UNREF(node); 630 return (0); 631 } 632 633 /* 634 * Hook disconnection 635 */ 636 static int 637 ng_eiface_disconnect(hook_p hook) 638 { 639 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 640 641 priv->ether = NULL; 642 priv->link_status &= ~IFM_ACTIVE; 643 CURVNET_SET_QUIET(priv->ifp->if_vnet); 644 if_link_state_change(priv->ifp, LINK_STATE_DOWN); 645 CURVNET_RESTORE(); 646 return (0); 647 } 648 649 /* 650 * Handle loading and unloading for this node type. 651 */ 652 static int 653 ng_eiface_mod_event(module_t mod, int event, void *data) 654 { 655 int error = 0; 656 657 switch (event) { 658 case MOD_LOAD: 659 case MOD_UNLOAD: 660 break; 661 default: 662 error = EOPNOTSUPP; 663 break; 664 } 665 return (error); 666 } 667 668 static void 669 vnet_ng_eiface_init(const void *unused) 670 { 671 672 V_ng_eiface_unit = new_unrhdr(0, 0xffff, NULL); 673 } 674 VNET_SYSINIT(vnet_ng_eiface_init, SI_SUB_PSEUDO, SI_ORDER_ANY, 675 vnet_ng_eiface_init, NULL); 676 677 static void 678 vnet_ng_eiface_uninit(const void *unused) 679 { 680 681 delete_unrhdr(V_ng_eiface_unit); 682 } 683 VNET_SYSUNINIT(vnet_ng_eiface_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY, 684 vnet_ng_eiface_uninit, NULL); 685