ng_ether.c revision 246245
1 2/* 3 * ng_ether.c 4 */ 5 6/*- 7 * Copyright (c) 1996-2000 Whistle Communications, Inc. 8 * All rights reserved. 9 * 10 * Subject to the following obligations and disclaimer of warranty, use and 11 * redistribution of this software, in source or object code forms, with or 12 * without modifications are expressly permitted by Whistle Communications; 13 * provided, however, that: 14 * 1. Any and all reproductions of the source or object code must include the 15 * copyright notice above and the following disclaimer of warranties; and 16 * 2. No rights are granted, in any manner or form, to use Whistle 17 * Communications, Inc. trademarks, including the mark "WHISTLE 18 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 19 * such appears in the above copyright notice or in the software. 20 * 21 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 22 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 23 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 24 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 26 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 27 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 28 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 29 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 30 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 31 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 32 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 33 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 36 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 37 * OF SUCH DAMAGE. 38 * 39 * Authors: Archie Cobbs <archie@freebsd.org> 40 * Julian Elischer <julian@freebsd.org> 41 * 42 * $FreeBSD: head/sys/netgraph/ng_ether.c 246245 2013-02-02 11:54:00Z avg $ 43 */ 44 45/* 46 * ng_ether(4) netgraph node type 47 */ 48 49#include <sys/param.h> 50#include <sys/systm.h> 51#include <sys/kernel.h> 52#include <sys/malloc.h> 53#include <sys/mbuf.h> 54#include <sys/errno.h> 55#include <sys/proc.h> 56#include <sys/syslog.h> 57#include <sys/socket.h> 58#include <sys/taskqueue.h> 59 60#include <net/if.h> 61#include <net/if_dl.h> 62#include <net/if_types.h> 63#include <net/if_arp.h> 64#include <net/if_var.h> 65#include <net/ethernet.h> 66#include <net/if_bridgevar.h> 67#include <net/vnet.h> 68 69#include <netgraph/ng_message.h> 70#include <netgraph/netgraph.h> 71#include <netgraph/ng_parse.h> 72#include <netgraph/ng_ether.h> 73 74MODULE_VERSION(ng_ether, 1); 75 76#define IFP2NG(ifp) (IFP2AC((ifp))->ac_netgraph) 77 78/* Per-node private data */ 79struct private { 80 struct ifnet *ifp; /* associated interface */ 81 hook_p upper; /* upper hook connection */ 82 hook_p lower; /* lower hook connection */ 83 hook_p orphan; /* orphan hook connection */ 84 u_char autoSrcAddr; /* always overwrite source address */ 85 u_char promisc; /* promiscuous mode enabled */ 86 u_long hwassist; /* hardware checksum capabilities */ 87 u_int flags; /* flags e.g. really die */ 88}; 89typedef struct private *priv_p; 90 91/* Hook pointers used by if_ethersubr.c to callback to netgraph */ 92extern void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp); 93extern void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m); 94extern int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); 95extern void (*ng_ether_attach_p)(struct ifnet *ifp); 96extern void (*ng_ether_detach_p)(struct ifnet *ifp); 97extern void (*ng_ether_link_state_p)(struct ifnet *ifp, int state); 98 99/* Functional hooks called from if_ethersubr.c */ 100static void ng_ether_input(struct ifnet *ifp, struct mbuf **mp); 101static void ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m); 102static int ng_ether_output(struct ifnet *ifp, struct mbuf **mp); 103static void ng_ether_attach(struct ifnet *ifp); 104static void ng_ether_detach(struct ifnet *ifp); 105static void ng_ether_link_state(struct ifnet *ifp, int state); 106 107/* Other functions */ 108static int ng_ether_rcv_lower(hook_p node, item_p item); 109static int ng_ether_rcv_upper(hook_p node, item_p item); 110 111/* Netgraph node methods */ 112static ng_constructor_t ng_ether_constructor; 113static ng_rcvmsg_t ng_ether_rcvmsg; 114static ng_shutdown_t ng_ether_shutdown; 115static ng_newhook_t ng_ether_newhook; 116static ng_rcvdata_t ng_ether_rcvdata; 117static ng_disconnect_t ng_ether_disconnect; 118static int ng_ether_mod_event(module_t mod, int event, void *data); 119 120static eventhandler_tag ng_ether_ifnet_arrival_cookie; 121 122/* List of commands and how to convert arguments to/from ASCII */ 123static const struct ng_cmdlist ng_ether_cmdlist[] = { 124 { 125 NGM_ETHER_COOKIE, 126 NGM_ETHER_GET_IFNAME, 127 "getifname", 128 NULL, 129 &ng_parse_string_type 130 }, 131 { 132 NGM_ETHER_COOKIE, 133 NGM_ETHER_GET_IFINDEX, 134 "getifindex", 135 NULL, 136 &ng_parse_int32_type 137 }, 138 { 139 NGM_ETHER_COOKIE, 140 NGM_ETHER_GET_ENADDR, 141 "getenaddr", 142 NULL, 143 &ng_parse_enaddr_type 144 }, 145 { 146 NGM_ETHER_COOKIE, 147 NGM_ETHER_SET_ENADDR, 148 "setenaddr", 149 &ng_parse_enaddr_type, 150 NULL 151 }, 152 { 153 NGM_ETHER_COOKIE, 154 NGM_ETHER_GET_PROMISC, 155 "getpromisc", 156 NULL, 157 &ng_parse_int32_type 158 }, 159 { 160 NGM_ETHER_COOKIE, 161 NGM_ETHER_SET_PROMISC, 162 "setpromisc", 163 &ng_parse_int32_type, 164 NULL 165 }, 166 { 167 NGM_ETHER_COOKIE, 168 NGM_ETHER_GET_AUTOSRC, 169 "getautosrc", 170 NULL, 171 &ng_parse_int32_type 172 }, 173 { 174 NGM_ETHER_COOKIE, 175 NGM_ETHER_SET_AUTOSRC, 176 "setautosrc", 177 &ng_parse_int32_type, 178 NULL 179 }, 180 { 181 NGM_ETHER_COOKIE, 182 NGM_ETHER_ADD_MULTI, 183 "addmulti", 184 &ng_parse_enaddr_type, 185 NULL 186 }, 187 { 188 NGM_ETHER_COOKIE, 189 NGM_ETHER_DEL_MULTI, 190 "delmulti", 191 &ng_parse_enaddr_type, 192 NULL 193 }, 194 { 195 NGM_ETHER_COOKIE, 196 NGM_ETHER_DETACH, 197 "detach", 198 NULL, 199 NULL 200 }, 201 { 0 } 202}; 203 204static struct ng_type ng_ether_typestruct = { 205 .version = NG_ABI_VERSION, 206 .name = NG_ETHER_NODE_TYPE, 207 .mod_event = ng_ether_mod_event, 208 .constructor = ng_ether_constructor, 209 .rcvmsg = ng_ether_rcvmsg, 210 .shutdown = ng_ether_shutdown, 211 .newhook = ng_ether_newhook, 212 .rcvdata = ng_ether_rcvdata, 213 .disconnect = ng_ether_disconnect, 214 .cmdlist = ng_ether_cmdlist, 215}; 216NETGRAPH_INIT(ether, &ng_ether_typestruct); 217 218/****************************************************************** 219 UTILITY FUNCTIONS 220******************************************************************/ 221static void 222ng_ether_sanitize_ifname(const char *ifname, char *name) 223{ 224 int i; 225 226 for (i = 0; i < IFNAMSIZ; i++) { 227 if (ifname[i] == '.' || ifname[i] == ':') 228 name[i] = '_'; 229 else 230 name[i] = ifname[i]; 231 if (name[i] == '\0') 232 break; 233 } 234} 235 236/****************************************************************** 237 ETHERNET FUNCTION HOOKS 238******************************************************************/ 239 240/* 241 * Handle a packet that has come in on an interface. We get to 242 * look at it here before any upper layer protocols do. 243 */ 244static void 245ng_ether_input(struct ifnet *ifp, struct mbuf **mp) 246{ 247 const node_p node = IFP2NG(ifp); 248 const priv_p priv = NG_NODE_PRIVATE(node); 249 int error; 250 251 /* If "lower" hook not connected, let packet continue */ 252 if (priv->lower == NULL) 253 return; 254 NG_SEND_DATA_ONLY(error, priv->lower, *mp); /* sets *mp = NULL */ 255} 256 257/* 258 * Handle a packet that has come in on an interface, and which 259 * does not match any of our known protocols (an ``orphan''). 260 */ 261static void 262ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m) 263{ 264 const node_p node = IFP2NG(ifp); 265 const priv_p priv = NG_NODE_PRIVATE(node); 266 int error; 267 268 /* If "orphan" hook not connected, discard packet */ 269 if (priv->orphan == NULL) { 270 m_freem(m); 271 return; 272 } 273 NG_SEND_DATA_ONLY(error, priv->orphan, m); 274} 275 276/* 277 * Handle a packet that is going out on an interface. 278 * The Ethernet header is already attached to the mbuf. 279 */ 280static int 281ng_ether_output(struct ifnet *ifp, struct mbuf **mp) 282{ 283 const node_p node = IFP2NG(ifp); 284 const priv_p priv = NG_NODE_PRIVATE(node); 285 int error = 0; 286 287 /* If "upper" hook not connected, let packet continue */ 288 if (priv->upper == NULL) 289 return (0); 290 291 /* Send it out "upper" hook */ 292 NG_OUTBOUND_THREAD_REF(); 293 NG_SEND_DATA_ONLY(error, priv->upper, *mp); 294 NG_OUTBOUND_THREAD_UNREF(); 295 return (error); 296} 297 298/* 299 * A new Ethernet interface has been attached. 300 * Create a new node for it, etc. 301 */ 302static void 303ng_ether_attach(struct ifnet *ifp) 304{ 305 char name[IFNAMSIZ]; 306 priv_p priv; 307 node_p node; 308 309 /* 310 * Do not create / attach an ether node to this ifnet if 311 * a netgraph node with the same name already exists. 312 * This should prevent ether nodes to become attached to 313 * eiface nodes, which may be problematic due to naming 314 * clashes. 315 */ 316 if ((node = ng_name2noderef(NULL, ifp->if_xname)) != NULL) { 317 NG_NODE_UNREF(node); 318 return; 319 } 320 321 /* Create node */ 322 KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__)); 323 if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) { 324 log(LOG_ERR, "%s: can't %s for %s\n", 325 __func__, "create node", ifp->if_xname); 326 return; 327 } 328 329 /* Allocate private data */ 330 priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 331 if (priv == NULL) { 332 log(LOG_ERR, "%s: can't %s for %s\n", 333 __func__, "allocate memory", ifp->if_xname); 334 NG_NODE_UNREF(node); 335 return; 336 } 337 NG_NODE_SET_PRIVATE(node, priv); 338 priv->ifp = ifp; 339 IFP2NG(ifp) = node; 340 priv->hwassist = ifp->if_hwassist; 341 342 /* Try to give the node the same name as the interface */ 343 ng_ether_sanitize_ifname(ifp->if_xname, name); 344 if (ng_name_node(node, name) != 0) 345 log(LOG_WARNING, "%s: can't name node %s\n", __func__, name); 346} 347 348/* 349 * An Ethernet interface is being detached. 350 * REALLY Destroy its node. 351 */ 352static void 353ng_ether_detach(struct ifnet *ifp) 354{ 355 const node_p node = IFP2NG(ifp); 356 const priv_p priv = NG_NODE_PRIVATE(node); 357 358 taskqueue_drain(taskqueue_swi, &ifp->if_linktask); 359 NG_NODE_REALLY_DIE(node); /* Force real removal of node */ 360 /* 361 * We can't assume the ifnet is still around when we run shutdown 362 * So zap it now. XXX We HOPE that anything running at this time 363 * handles it (as it should in the non netgraph case). 364 */ 365 IFP2NG(ifp) = NULL; 366 priv->ifp = NULL; /* XXX race if interrupted an output packet */ 367 ng_rmnode_self(node); /* remove all netgraph parts */ 368} 369 370/* 371 * Notify graph about link event. 372 * if_link_state_change() has already checked that the state has changed. 373 */ 374static void 375ng_ether_link_state(struct ifnet *ifp, int state) 376{ 377 const node_p node = IFP2NG(ifp); 378 const priv_p priv = NG_NODE_PRIVATE(node); 379 struct ng_mesg *msg; 380 int cmd, dummy_error = 0; 381 382 if (state == LINK_STATE_UP) 383 cmd = NGM_LINK_IS_UP; 384 else if (state == LINK_STATE_DOWN) 385 cmd = NGM_LINK_IS_DOWN; 386 else 387 return; 388 389 if (priv->lower != NULL) { 390 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT); 391 if (msg != NULL) 392 NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->lower, 0); 393 } 394 if (priv->orphan != NULL) { 395 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT); 396 if (msg != NULL) 397 NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->orphan, 0); 398 } 399} 400 401/* 402 * Interface arrival notification handler. 403 * The notification is produced in two cases: 404 * o a new interface arrives 405 * o an existing interface got renamed 406 * Currently the first case is handled by ng_ether_attach via special 407 * hook ng_ether_attach_p. 408 */ 409static void 410ng_ether_ifnet_arrival_event(void *arg __unused, struct ifnet *ifp) 411{ 412 char name[IFNAMSIZ]; 413 node_p node = IFP2NG(ifp); 414 415 /* 416 * Just return if it's a new interface without an ng_ether companion. 417 */ 418 if (node == NULL) 419 return; 420 421 /* Try to give the node the same name as the new interface name */ 422 ng_ether_sanitize_ifname(ifp->if_xname, name); 423 if (ng_name_node(node, name) != 0) 424 log(LOG_WARNING, "%s: can't re-name node %s\n", __func__, name); 425} 426 427/****************************************************************** 428 NETGRAPH NODE METHODS 429******************************************************************/ 430 431/* 432 * It is not possible or allowable to create a node of this type. 433 * Nodes get created when the interface is attached (or, when 434 * this node type's KLD is loaded). 435 */ 436static int 437ng_ether_constructor(node_p node) 438{ 439 return (EINVAL); 440} 441 442/* 443 * Check for attaching a new hook. 444 */ 445static int 446ng_ether_newhook(node_p node, hook_p hook, const char *name) 447{ 448 const priv_p priv = NG_NODE_PRIVATE(node); 449 hook_p *hookptr; 450 451 /* Divert hook is an alias for lower */ 452 if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0) 453 name = NG_ETHER_HOOK_LOWER; 454 455 /* Which hook? */ 456 if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0) { 457 hookptr = &priv->upper; 458 NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_upper); 459 NG_HOOK_SET_TO_INBOUND(hook); 460 } else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) { 461 hookptr = &priv->lower; 462 NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower); 463 } else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) { 464 hookptr = &priv->orphan; 465 NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower); 466 } else 467 return (EINVAL); 468 469 /* Check if already connected (shouldn't be, but doesn't hurt) */ 470 if (*hookptr != NULL) 471 return (EISCONN); 472 473 /* Disable hardware checksums while 'upper' hook is connected */ 474 if (hookptr == &priv->upper) 475 priv->ifp->if_hwassist = 0; 476 NG_HOOK_HI_STACK(hook); 477 /* OK */ 478 *hookptr = hook; 479 return (0); 480} 481 482/* 483 * Receive an incoming control message. 484 */ 485static int 486ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook) 487{ 488 const priv_p priv = NG_NODE_PRIVATE(node); 489 struct ng_mesg *resp = NULL; 490 int error = 0; 491 struct ng_mesg *msg; 492 493 NGI_GET_MSG(item, msg); 494 switch (msg->header.typecookie) { 495 case NGM_ETHER_COOKIE: 496 switch (msg->header.cmd) { 497 case NGM_ETHER_GET_IFNAME: 498 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT); 499 if (resp == NULL) { 500 error = ENOMEM; 501 break; 502 } 503 strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ); 504 break; 505 case NGM_ETHER_GET_IFINDEX: 506 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 507 if (resp == NULL) { 508 error = ENOMEM; 509 break; 510 } 511 *((u_int32_t *)resp->data) = priv->ifp->if_index; 512 break; 513 case NGM_ETHER_GET_ENADDR: 514 NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT); 515 if (resp == NULL) { 516 error = ENOMEM; 517 break; 518 } 519 bcopy(IF_LLADDR(priv->ifp), 520 resp->data, ETHER_ADDR_LEN); 521 break; 522 case NGM_ETHER_SET_ENADDR: 523 { 524 if (msg->header.arglen != ETHER_ADDR_LEN) { 525 error = EINVAL; 526 break; 527 } 528 error = if_setlladdr(priv->ifp, 529 (u_char *)msg->data, ETHER_ADDR_LEN); 530 EVENTHANDLER_INVOKE(iflladdr_event, priv->ifp); 531 break; 532 } 533 case NGM_ETHER_GET_PROMISC: 534 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 535 if (resp == NULL) { 536 error = ENOMEM; 537 break; 538 } 539 *((u_int32_t *)resp->data) = priv->promisc; 540 break; 541 case NGM_ETHER_SET_PROMISC: 542 { 543 u_char want; 544 545 if (msg->header.arglen != sizeof(u_int32_t)) { 546 error = EINVAL; 547 break; 548 } 549 want = !!*((u_int32_t *)msg->data); 550 if (want ^ priv->promisc) { 551 if ((error = ifpromisc(priv->ifp, want)) != 0) 552 break; 553 priv->promisc = want; 554 } 555 break; 556 } 557 case NGM_ETHER_GET_AUTOSRC: 558 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 559 if (resp == NULL) { 560 error = ENOMEM; 561 break; 562 } 563 *((u_int32_t *)resp->data) = priv->autoSrcAddr; 564 break; 565 case NGM_ETHER_SET_AUTOSRC: 566 if (msg->header.arglen != sizeof(u_int32_t)) { 567 error = EINVAL; 568 break; 569 } 570 priv->autoSrcAddr = !!*((u_int32_t *)msg->data); 571 break; 572 case NGM_ETHER_ADD_MULTI: 573 { 574 struct sockaddr_dl sa_dl; 575 struct ifmultiaddr *ifma; 576 577 if (msg->header.arglen != ETHER_ADDR_LEN) { 578 error = EINVAL; 579 break; 580 } 581 bzero(&sa_dl, sizeof(struct sockaddr_dl)); 582 sa_dl.sdl_len = sizeof(struct sockaddr_dl); 583 sa_dl.sdl_family = AF_LINK; 584 sa_dl.sdl_alen = ETHER_ADDR_LEN; 585 bcopy((void *)msg->data, LLADDR(&sa_dl), 586 ETHER_ADDR_LEN); 587 /* 588 * Netgraph is only permitted to join groups once 589 * via the if_addmulti() KPI, because it cannot hold 590 * struct ifmultiaddr * between calls. It may also 591 * lose a race while we check if the membership 592 * already exists. 593 */ 594 if_maddr_rlock(priv->ifp); 595 ifma = if_findmulti(priv->ifp, 596 (struct sockaddr *)&sa_dl); 597 if_maddr_runlock(priv->ifp); 598 if (ifma != NULL) { 599 error = EADDRINUSE; 600 } else { 601 error = if_addmulti(priv->ifp, 602 (struct sockaddr *)&sa_dl, &ifma); 603 } 604 break; 605 } 606 case NGM_ETHER_DEL_MULTI: 607 { 608 struct sockaddr_dl sa_dl; 609 610 if (msg->header.arglen != ETHER_ADDR_LEN) { 611 error = EINVAL; 612 break; 613 } 614 bzero(&sa_dl, sizeof(struct sockaddr_dl)); 615 sa_dl.sdl_len = sizeof(struct sockaddr_dl); 616 sa_dl.sdl_family = AF_LINK; 617 sa_dl.sdl_alen = ETHER_ADDR_LEN; 618 bcopy((void *)msg->data, LLADDR(&sa_dl), 619 ETHER_ADDR_LEN); 620 error = if_delmulti(priv->ifp, 621 (struct sockaddr *)&sa_dl); 622 break; 623 } 624 case NGM_ETHER_DETACH: 625 ng_ether_detach(priv->ifp); 626 break; 627 default: 628 error = EINVAL; 629 break; 630 } 631 break; 632 default: 633 error = EINVAL; 634 break; 635 } 636 NG_RESPOND_MSG(error, node, item, resp); 637 NG_FREE_MSG(msg); 638 return (error); 639} 640 641/* 642 * Receive data on a hook. 643 * Since we use per-hook recveive methods this should never be called. 644 */ 645static int 646ng_ether_rcvdata(hook_p hook, item_p item) 647{ 648 NG_FREE_ITEM(item); 649 650 panic("%s: weird hook", __func__); 651} 652 653/* 654 * Handle an mbuf received on the "lower" or "orphan" hook. 655 */ 656static int 657ng_ether_rcv_lower(hook_p hook, item_p item) 658{ 659 struct mbuf *m; 660 const node_p node = NG_HOOK_NODE(hook); 661 const priv_p priv = NG_NODE_PRIVATE(node); 662 struct ifnet *const ifp = priv->ifp; 663 664 NGI_GET_M(item, m); 665 NG_FREE_ITEM(item); 666 667 /* Check whether interface is ready for packets */ 668 669 if (!((ifp->if_flags & IFF_UP) && 670 (ifp->if_drv_flags & IFF_DRV_RUNNING))) { 671 NG_FREE_M(m); 672 return (ENETDOWN); 673 } 674 675 /* Make sure header is fully pulled up */ 676 if (m->m_pkthdr.len < sizeof(struct ether_header)) { 677 NG_FREE_M(m); 678 return (EINVAL); 679 } 680 if (m->m_len < sizeof(struct ether_header) 681 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 682 return (ENOBUFS); 683 684 /* Drop in the MAC address if desired */ 685 if (priv->autoSrcAddr) { 686 687 /* Make the mbuf writable if it's not already */ 688 if (!M_WRITABLE(m) 689 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 690 return (ENOBUFS); 691 692 /* Overwrite source MAC address */ 693 bcopy(IF_LLADDR(ifp), 694 mtod(m, struct ether_header *)->ether_shost, 695 ETHER_ADDR_LEN); 696 } 697 698 /* Send it on its way */ 699 return ether_output_frame(ifp, m); 700} 701 702/* 703 * Handle an mbuf received on the "upper" hook. 704 */ 705static int 706ng_ether_rcv_upper(hook_p hook, item_p item) 707{ 708 struct mbuf *m; 709 const node_p node = NG_HOOK_NODE(hook); 710 const priv_p priv = NG_NODE_PRIVATE(node); 711 struct ifnet *ifp = priv->ifp; 712 713 NGI_GET_M(item, m); 714 NG_FREE_ITEM(item); 715 716 /* Check length and pull off header */ 717 if (m->m_pkthdr.len < sizeof(struct ether_header)) { 718 NG_FREE_M(m); 719 return (EINVAL); 720 } 721 if (m->m_len < sizeof(struct ether_header) && 722 (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 723 return (ENOBUFS); 724 725 m->m_pkthdr.rcvif = ifp; 726 727 /* Pass the packet to the bridge, it may come back to us */ 728 if (ifp->if_bridge) { 729 BRIDGE_INPUT(ifp, m); 730 if (m == NULL) 731 return (0); 732 } 733 734 /* Route packet back in */ 735 ether_demux(ifp, m); 736 return (0); 737} 738 739/* 740 * Shutdown node. This resets the node but does not remove it 741 * unless the REALLY_DIE flag is set. 742 */ 743static int 744ng_ether_shutdown(node_p node) 745{ 746 const priv_p priv = NG_NODE_PRIVATE(node); 747 748 if (node->nd_flags & NGF_REALLY_DIE) { 749 /* 750 * WE came here because the ethernet card is being unloaded, 751 * so stop being persistant. 752 * Actually undo all the things we did on creation. 753 * Assume the ifp has already been freed. 754 */ 755 NG_NODE_SET_PRIVATE(node, NULL); 756 free(priv, M_NETGRAPH); 757 NG_NODE_UNREF(node); /* free node itself */ 758 return (0); 759 } 760 if (priv->promisc) { /* disable promiscuous mode */ 761 (void)ifpromisc(priv->ifp, 0); 762 priv->promisc = 0; 763 } 764 NG_NODE_REVIVE(node); /* Signal ng_rmnode we are persisant */ 765 766 return (0); 767} 768 769/* 770 * Hook disconnection. 771 */ 772static int 773ng_ether_disconnect(hook_p hook) 774{ 775 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 776 777 if (hook == priv->upper) { 778 priv->upper = NULL; 779 if (priv->ifp != NULL) /* restore h/w csum */ 780 priv->ifp->if_hwassist = priv->hwassist; 781 } else if (hook == priv->lower) 782 priv->lower = NULL; 783 else if (hook == priv->orphan) 784 priv->orphan = NULL; 785 else 786 panic("%s: weird hook", __func__); 787 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 788 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) 789 ng_rmnode_self(NG_HOOK_NODE(hook)); /* reset node */ 790 return (0); 791} 792 793/****************************************************************** 794 INITIALIZATION 795******************************************************************/ 796 797/* 798 * Handle loading and unloading for this node type. 799 */ 800static int 801ng_ether_mod_event(module_t mod, int event, void *data) 802{ 803 int error = 0; 804 805 switch (event) { 806 case MOD_LOAD: 807 808 /* Register function hooks */ 809 if (ng_ether_attach_p != NULL) { 810 error = EEXIST; 811 break; 812 } 813 ng_ether_attach_p = ng_ether_attach; 814 ng_ether_detach_p = ng_ether_detach; 815 ng_ether_output_p = ng_ether_output; 816 ng_ether_input_p = ng_ether_input; 817 ng_ether_input_orphan_p = ng_ether_input_orphan; 818 ng_ether_link_state_p = ng_ether_link_state; 819 820 ng_ether_ifnet_arrival_cookie = 821 EVENTHANDLER_REGISTER(ifnet_arrival_event, 822 ng_ether_ifnet_arrival_event, NULL, EVENTHANDLER_PRI_ANY); 823 break; 824 825 case MOD_UNLOAD: 826 827 /* 828 * Note that the base code won't try to unload us until 829 * all nodes have been removed, and that can't happen 830 * until all Ethernet interfaces are removed. In any 831 * case, we know there are no nodes left if the action 832 * is MOD_UNLOAD, so there's no need to detach any nodes. 833 */ 834 835 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, 836 ng_ether_ifnet_arrival_cookie); 837 838 /* Unregister function hooks */ 839 ng_ether_attach_p = NULL; 840 ng_ether_detach_p = NULL; 841 ng_ether_output_p = NULL; 842 ng_ether_input_p = NULL; 843 ng_ether_input_orphan_p = NULL; 844 ng_ether_link_state_p = NULL; 845 break; 846 847 default: 848 error = EOPNOTSUPP; 849 break; 850 } 851 return (error); 852} 853 854static void 855vnet_ng_ether_init(const void *unused) 856{ 857 struct ifnet *ifp; 858 859 /* If module load was rejected, don't attach to vnets. */ 860 if (ng_ether_attach_p != ng_ether_attach) 861 return; 862 863 /* Create nodes for any already-existing Ethernet interfaces. */ 864 IFNET_RLOCK(); 865 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 866 if (ifp->if_type == IFT_ETHER 867 || ifp->if_type == IFT_L2VLAN) 868 ng_ether_attach(ifp); 869 } 870 IFNET_RUNLOCK(); 871} 872VNET_SYSINIT(vnet_ng_ether_init, SI_SUB_PSEUDO, SI_ORDER_ANY, 873 vnet_ng_ether_init, NULL); 874