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