ng_ether.c revision 139433
1254721Semaste 2254721Semaste/* 3254721Semaste * ng_ether.c 4254721Semaste * 5254721Semaste * Copyright (c) 1996-2000 Whistle Communications, Inc. 6254721Semaste * All rights reserved. 7254721Semaste * 8254721Semaste * Subject to the following obligations and disclaimer of warranty, use and 9254721Semaste * redistribution of this software, in source or object code forms, with or 10254721Semaste * without modifications are expressly permitted by Whistle Communications; 11254721Semaste * provided, however, that: 12254721Semaste * 1. Any and all reproductions of the source or object code must include the 13254721Semaste * copyright notice above and the following disclaimer of warranties; and 14254721Semaste * 2. No rights are granted, in any manner or form, to use Whistle 15254721Semaste * Communications, Inc. trademarks, including the mark "WHISTLE 16254721Semaste * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 17254721Semaste * such appears in the above copyright notice or in the software. 18254721Semaste * 19254721Semaste * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 20254721Semaste * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 21254721Semaste * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 22254721Semaste * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 23254721Semaste * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 24254721Semaste * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 25254721Semaste * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 26254721Semaste * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 27254721Semaste * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 28254721Semaste * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 29254721Semaste * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 30254721Semaste * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 31254721Semaste * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 32254721Semaste * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33254721Semaste * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 34254721Semaste * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 35254721Semaste * OF SUCH DAMAGE. 36254721Semaste * 37254721Semaste * Authors: Archie Cobbs <archie@freebsd.org> 38254721Semaste * Julian Elischer <julian@freebsd.org> 39254721Semaste * 40254721Semaste * $FreeBSD: head/sys/netgraph/ng_ether.c 139433 2004-12-30 12:22:51Z glebius $ 41254721Semaste */ 42254721Semaste 43254721Semaste/* 44254721Semaste * ng_ether(4) netgraph node type 45254721Semaste */ 46254721Semaste 47254721Semaste#include <sys/param.h> 48254721Semaste#include <sys/systm.h> 49254721Semaste#include <sys/kernel.h> 50254721Semaste#include <sys/malloc.h> 51254721Semaste#include <sys/mbuf.h> 52254721Semaste#include <sys/errno.h> 53254721Semaste#include <sys/syslog.h> 54254721Semaste#include <sys/socket.h> 55254721Semaste 56254721Semaste#include <net/bridge.h> 57254721Semaste#include <net/if.h> 58276479Sdim#include <net/if_types.h> 59254721Semaste#include <net/if_arp.h> 60254721Semaste#include <net/if_var.h> 61254721Semaste#include <net/ethernet.h> 62254721Semaste 63254721Semaste#include <netgraph/ng_message.h> 64254721Semaste#include <netgraph/netgraph.h> 65254721Semaste#include <netgraph/ng_parse.h> 66254721Semaste#include <netgraph/ng_ether.h> 67254721Semaste 68254721Semaste#define IFP2NG(ifp) ((struct ng_node *)((struct arpcom *)(ifp))->ac_netgraph) 69254721Semaste#define IFP2NG_SET(ifp, val) (((struct arpcom *)(ifp))->ac_netgraph = (val)) 70254721Semaste 71254721Semaste/* Per-node private data */ 72254721Semastestruct private { 73254721Semaste struct ifnet *ifp; /* associated interface */ 74254721Semaste hook_p upper; /* upper hook connection */ 75254721Semaste hook_p lower; /* lower hook connection */ 76254721Semaste hook_p orphan; /* orphan hook connection */ 77254721Semaste u_char autoSrcAddr; /* always overwrite source address */ 78254721Semaste u_char promisc; /* promiscuous mode enabled */ 79254721Semaste u_long hwassist; /* hardware checksum capabilities */ 80254721Semaste u_int flags; /* flags e.g. really die */ 81254721Semaste}; 82254721Semastetypedef struct private *priv_p; 83254721Semaste 84254721Semaste/* Hook pointers used by if_ethersubr.c to callback to netgraph */ 85254721Semasteextern void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp); 86254721Semasteextern void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m); 87254721Semasteextern int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); 88254721Semasteextern void (*ng_ether_attach_p)(struct ifnet *ifp); 89254721Semasteextern void (*ng_ether_detach_p)(struct ifnet *ifp); 90254721Semaste 91254721Semaste/* Functional hooks called from if_ethersubr.c */ 92254721Semastestatic void ng_ether_input(struct ifnet *ifp, struct mbuf **mp); 93254721Semastestatic void ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m); 94254721Semastestatic int ng_ether_output(struct ifnet *ifp, struct mbuf **mp); 95254721Semastestatic void ng_ether_attach(struct ifnet *ifp); 96254721Semastestatic void ng_ether_detach(struct ifnet *ifp); 97254721Semaste 98254721Semaste/* Other functions */ 99254721Semastestatic int ng_ether_rcv_lower(node_p node, struct mbuf *m); 100254721Semastestatic int ng_ether_rcv_upper(node_p node, struct mbuf *m); 101254721Semaste 102280031Sdim/* Netgraph node methods */ 103254721Semastestatic ng_constructor_t ng_ether_constructor; 104254721Semastestatic ng_rcvmsg_t ng_ether_rcvmsg; 105254721Semastestatic ng_shutdown_t ng_ether_shutdown; 106280031Sdimstatic ng_newhook_t ng_ether_newhook; 107254721Semastestatic ng_connect_t ng_ether_connect; 108254721Semastestatic ng_rcvdata_t ng_ether_rcvdata; 109254721Semastestatic ng_disconnect_t ng_ether_disconnect; 110254721Semastestatic int ng_ether_mod_event(module_t mod, int event, void *data); 111280031Sdim 112280031Sdim/* List of commands and how to convert arguments to/from ASCII */ 113280031Sdimstatic const struct ng_cmdlist ng_ether_cmdlist[] = { 114280031Sdim { 115280031Sdim NGM_ETHER_COOKIE, 116280031Sdim NGM_ETHER_GET_IFNAME, 117280031Sdim "getifname", 118280031Sdim NULL, 119254721Semaste &ng_parse_string_type 120254721Semaste }, 121254721Semaste { 122254721Semaste NGM_ETHER_COOKIE, 123254721Semaste NGM_ETHER_GET_IFINDEX, 124254721Semaste "getifindex", 125254721Semaste NULL, 126254721Semaste &ng_parse_int32_type 127254721Semaste }, 128254721Semaste { 129254721Semaste NGM_ETHER_COOKIE, 130254721Semaste NGM_ETHER_GET_ENADDR, 131254721Semaste "getenaddr", 132254721Semaste NULL, 133254721Semaste &ng_parse_enaddr_type 134254721Semaste }, 135254721Semaste { 136254721Semaste NGM_ETHER_COOKIE, 137254721Semaste NGM_ETHER_SET_ENADDR, 138254721Semaste "setenaddr", 139254721Semaste &ng_parse_enaddr_type, 140254721Semaste NULL 141254721Semaste }, 142254721Semaste { 143254721Semaste NGM_ETHER_COOKIE, 144254721Semaste NGM_ETHER_GET_PROMISC, 145254721Semaste "getpromisc", 146 NULL, 147 &ng_parse_int32_type 148 }, 149 { 150 NGM_ETHER_COOKIE, 151 NGM_ETHER_SET_PROMISC, 152 "setpromisc", 153 &ng_parse_int32_type, 154 NULL 155 }, 156 { 157 NGM_ETHER_COOKIE, 158 NGM_ETHER_GET_AUTOSRC, 159 "getautosrc", 160 NULL, 161 &ng_parse_int32_type 162 }, 163 { 164 NGM_ETHER_COOKIE, 165 NGM_ETHER_SET_AUTOSRC, 166 "setautosrc", 167 &ng_parse_int32_type, 168 NULL 169 }, 170 { 0 } 171}; 172 173static struct ng_type ng_ether_typestruct = { 174 .version = NG_ABI_VERSION, 175 .name = NG_ETHER_NODE_TYPE, 176 .mod_event = ng_ether_mod_event, 177 .constructor = ng_ether_constructor, 178 .rcvmsg = ng_ether_rcvmsg, 179 .shutdown = ng_ether_shutdown, 180 .newhook = ng_ether_newhook, 181 .connect = ng_ether_connect, 182 .rcvdata = ng_ether_rcvdata, 183 .disconnect = ng_ether_disconnect, 184 .cmdlist = ng_ether_cmdlist, 185}; 186MODULE_VERSION(ng_ether, 1); 187NETGRAPH_INIT(ether, &ng_ether_typestruct); 188 189/****************************************************************** 190 ETHERNET FUNCTION HOOKS 191******************************************************************/ 192 193/* 194 * Handle a packet that has come in on an interface. We get to 195 * look at it here before any upper layer protocols do. 196 * 197 * NOTE: this function will get called at splimp() 198 */ 199static void 200ng_ether_input(struct ifnet *ifp, struct mbuf **mp) 201{ 202 const node_p node = IFP2NG(ifp); 203 const priv_p priv = NG_NODE_PRIVATE(node); 204 int error; 205 206 /* If "lower" hook not connected, let packet continue */ 207 if (priv->lower == NULL) 208 return; 209 NG_SEND_DATA_ONLY(error, priv->lower, *mp); /* sets *mp = NULL */ 210} 211 212/* 213 * Handle a packet that has come in on an interface, and which 214 * does not match any of our known protocols (an ``orphan''). 215 * 216 * NOTE: this function will get called at splimp() 217 */ 218static void 219ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m) 220{ 221 const node_p node = IFP2NG(ifp); 222 const priv_p priv = NG_NODE_PRIVATE(node); 223 int error; 224 225 /* If "orphan" hook not connected, discard packet */ 226 if (priv->orphan == NULL) { 227 m_freem(m); 228 return; 229 } 230 NG_SEND_DATA_ONLY(error, priv->orphan, m); 231} 232 233/* 234 * Handle a packet that is going out on an interface. 235 * The Ethernet header is already attached to the mbuf. 236 */ 237static int 238ng_ether_output(struct ifnet *ifp, struct mbuf **mp) 239{ 240 const node_p node = IFP2NG(ifp); 241 const priv_p priv = NG_NODE_PRIVATE(node); 242 int error = 0; 243 244 /* If "upper" hook not connected, let packet continue */ 245 if (priv->upper == NULL) 246 return (0); 247 248 /* Send it out "upper" hook */ 249 NG_SEND_DATA_ONLY(error, priv->upper, *mp); 250 return (error); 251} 252 253/* 254 * A new Ethernet interface has been attached. 255 * Create a new node for it, etc. 256 */ 257static void 258ng_ether_attach(struct ifnet *ifp) 259{ 260 priv_p priv; 261 node_p node; 262 263 /* Create node */ 264 KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__)); 265 if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) { 266 log(LOG_ERR, "%s: can't %s for %s\n", 267 __func__, "create node", ifp->if_xname); 268 return; 269 } 270 271 /* Allocate private data */ 272 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 273 if (priv == NULL) { 274 log(LOG_ERR, "%s: can't %s for %s\n", 275 __func__, "allocate memory", ifp->if_xname); 276 NG_NODE_UNREF(node); 277 return; 278 } 279 NG_NODE_SET_PRIVATE(node, priv); 280 priv->ifp = ifp; 281 IFP2NG_SET(ifp, node); 282 priv->autoSrcAddr = 1; 283 priv->hwassist = ifp->if_hwassist; 284 285 /* Try to give the node the same name as the interface */ 286 if (ng_name_node(node, ifp->if_xname) != 0) { 287 log(LOG_WARNING, "%s: can't name node %s\n", 288 __func__, ifp->if_xname); 289 } 290} 291 292/* 293 * An Ethernet interface is being detached. 294 * REALLY Destroy its node. 295 */ 296static void 297ng_ether_detach(struct ifnet *ifp) 298{ 299 const node_p node = IFP2NG(ifp); 300 const priv_p priv = NG_NODE_PRIVATE(node); 301 302 NG_NODE_REALLY_DIE(node); /* Force real removal of node */ 303 /* 304 * We can't assume the ifnet is still around when we run shutdown 305 * So zap it now. XXX We HOPE that anything running at this time 306 * handles it (as it should in the non netgraph case). 307 */ 308 IFP2NG_SET(ifp, NULL); 309 priv->ifp = NULL; /* XXX race if interrupted an output packet */ 310 ng_rmnode_self(node); /* remove all netgraph parts */ 311} 312 313/****************************************************************** 314 NETGRAPH NODE METHODS 315******************************************************************/ 316 317/* 318 * It is not possible or allowable to create a node of this type. 319 * Nodes get created when the interface is attached (or, when 320 * this node type's KLD is loaded). 321 */ 322static int 323ng_ether_constructor(node_p node) 324{ 325 return (EINVAL); 326} 327 328/* 329 * Check for attaching a new hook. 330 */ 331static int 332ng_ether_newhook(node_p node, hook_p hook, const char *name) 333{ 334 const priv_p priv = NG_NODE_PRIVATE(node); 335 hook_p *hookptr; 336 337 /* Divert hook is an alias for lower */ 338 if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0) 339 name = NG_ETHER_HOOK_LOWER; 340 341 /* Which hook? */ 342 if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0) 343 hookptr = &priv->upper; 344 else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) 345 hookptr = &priv->lower; 346 else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) 347 hookptr = &priv->orphan; 348 else 349 return (EINVAL); 350 351 /* Check if already connected (shouldn't be, but doesn't hurt) */ 352 if (*hookptr != NULL) 353 return (EISCONN); 354 355 /* Disable hardware checksums while 'upper' hook is connected */ 356 if (hookptr == &priv->upper) 357 priv->ifp->if_hwassist = 0; 358 359 /* OK */ 360 *hookptr = hook; 361 return (0); 362} 363 364/* 365 * Hooks are attached, adjust to force queueing. 366 * We don't really care which hook it is. 367 * they should all be queuing for outgoing data. 368 */ 369static int 370ng_ether_connect(hook_p hook) 371{ 372 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook)); 373 return (0); 374} 375 376/* 377 * Receive an incoming control message. 378 */ 379static int 380ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook) 381{ 382 const priv_p priv = NG_NODE_PRIVATE(node); 383 struct ng_mesg *resp = NULL; 384 int error = 0; 385 struct ng_mesg *msg; 386 387 NGI_GET_MSG(item, msg); 388 switch (msg->header.typecookie) { 389 case NGM_ETHER_COOKIE: 390 switch (msg->header.cmd) { 391 case NGM_ETHER_GET_IFNAME: 392 NG_MKRESPONSE(resp, msg, IFNAMSIZ + 1, M_NOWAIT); 393 if (resp == NULL) { 394 error = ENOMEM; 395 break; 396 } 397 strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ + 1); 398 break; 399 case NGM_ETHER_GET_IFINDEX: 400 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 401 if (resp == NULL) { 402 error = ENOMEM; 403 break; 404 } 405 *((u_int32_t *)resp->data) = priv->ifp->if_index; 406 break; 407 case NGM_ETHER_GET_ENADDR: 408 NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT); 409 if (resp == NULL) { 410 error = ENOMEM; 411 break; 412 } 413 bcopy((IFP2AC(priv->ifp))->ac_enaddr, 414 resp->data, ETHER_ADDR_LEN); 415 break; 416 case NGM_ETHER_SET_ENADDR: 417 { 418 if (msg->header.arglen != ETHER_ADDR_LEN) { 419 error = EINVAL; 420 break; 421 } 422 error = if_setlladdr(priv->ifp, 423 (u_char *)msg->data, ETHER_ADDR_LEN); 424 break; 425 } 426 case NGM_ETHER_GET_PROMISC: 427 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 428 if (resp == NULL) { 429 error = ENOMEM; 430 break; 431 } 432 *((u_int32_t *)resp->data) = priv->promisc; 433 break; 434 case NGM_ETHER_SET_PROMISC: 435 { 436 u_char want; 437 438 if (msg->header.arglen != sizeof(u_int32_t)) { 439 error = EINVAL; 440 break; 441 } 442 want = !!*((u_int32_t *)msg->data); 443 if (want ^ priv->promisc) { 444 if ((error = ifpromisc(priv->ifp, want)) != 0) 445 break; 446 priv->promisc = want; 447 } 448 break; 449 } 450 case NGM_ETHER_GET_AUTOSRC: 451 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 452 if (resp == NULL) { 453 error = ENOMEM; 454 break; 455 } 456 *((u_int32_t *)resp->data) = priv->autoSrcAddr; 457 break; 458 case NGM_ETHER_SET_AUTOSRC: 459 if (msg->header.arglen != sizeof(u_int32_t)) { 460 error = EINVAL; 461 break; 462 } 463 priv->autoSrcAddr = !!*((u_int32_t *)msg->data); 464 break; 465 default: 466 error = EINVAL; 467 break; 468 } 469 break; 470 default: 471 error = EINVAL; 472 break; 473 } 474 NG_RESPOND_MSG(error, node, item, resp); 475 NG_FREE_MSG(msg); 476 return (error); 477} 478 479/* 480 * Receive data on a hook. 481 */ 482static int 483ng_ether_rcvdata(hook_p hook, item_p item) 484{ 485 const node_p node = NG_HOOK_NODE(hook); 486 const priv_p priv = NG_NODE_PRIVATE(node); 487 struct mbuf *m; 488 489 NGI_GET_M(item, m); 490 NG_FREE_ITEM(item); 491 492 if (hook == priv->lower || hook == priv->orphan) 493 return ng_ether_rcv_lower(node, m); 494 if (hook == priv->upper) 495 return ng_ether_rcv_upper(node, m); 496 panic("%s: weird hook", __func__); 497#ifdef RESTARTABLE_PANICS /* so we don't get an error msg in LINT */ 498 return (0); 499#endif 500} 501 502/* 503 * Handle an mbuf received on the "lower" or "orphan" hook. 504 */ 505static int 506ng_ether_rcv_lower(node_p node, struct mbuf *m) 507{ 508 const priv_p priv = NG_NODE_PRIVATE(node); 509 struct ifnet *const ifp = priv->ifp; 510 511 /* Check whether interface is ready for packets */ 512 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) { 513 NG_FREE_M(m); 514 return (ENETDOWN); 515 } 516 517 /* Make sure header is fully pulled up */ 518 if (m->m_pkthdr.len < sizeof(struct ether_header)) { 519 NG_FREE_M(m); 520 return (EINVAL); 521 } 522 if (m->m_len < sizeof(struct ether_header) 523 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 524 return (ENOBUFS); 525 526 /* Drop in the MAC address if desired */ 527 if (priv->autoSrcAddr) { 528 529 /* Make the mbuf writable if it's not already */ 530 if (!M_WRITABLE(m) 531 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 532 return (ENOBUFS); 533 534 /* Overwrite source MAC address */ 535 bcopy((IFP2AC(ifp))->ac_enaddr, 536 mtod(m, struct ether_header *)->ether_shost, 537 ETHER_ADDR_LEN); 538 } 539 540 /* Send it on its way */ 541 return ether_output_frame(ifp, m); 542} 543 544/* 545 * Handle an mbuf received on the "upper" hook. 546 */ 547static int 548ng_ether_rcv_upper(node_p node, struct mbuf *m) 549{ 550 const priv_p priv = NG_NODE_PRIVATE(node); 551 552 m->m_pkthdr.rcvif = priv->ifp; 553 554 if (BDG_ACTIVE(priv->ifp) ) 555 if ((m = bridge_in_ptr(priv->ifp, m)) == NULL) 556 return (0); 557 558 /* Route packet back in */ 559 ether_demux(priv->ifp, m); 560 return (0); 561} 562 563/* 564 * Shutdown node. This resets the node but does not remove it 565 * unless the REALLY_DIE flag is set. 566 */ 567static int 568ng_ether_shutdown(node_p node) 569{ 570 const priv_p priv = NG_NODE_PRIVATE(node); 571 572 if (node->nd_flags & NGF_REALLY_DIE) { 573 /* 574 * WE came here because the ethernet card is being unloaded, 575 * so stop being persistant. 576 * Actually undo all the things we did on creation. 577 * Assume the ifp has already been freed. 578 */ 579 NG_NODE_SET_PRIVATE(node, NULL); 580 FREE(priv, M_NETGRAPH); 581 NG_NODE_UNREF(node); /* free node itself */ 582 return (0); 583 } 584 if (priv->promisc) { /* disable promiscuous mode */ 585 (void)ifpromisc(priv->ifp, 0); 586 priv->promisc = 0; 587 } 588 priv->autoSrcAddr = 1; /* reset auto-src-addr flag */ 589 NG_NODE_REVIVE(node); /* Signal ng_rmnode we are persisant */ 590 591 return (0); 592} 593 594/* 595 * Hook disconnection. 596 */ 597static int 598ng_ether_disconnect(hook_p hook) 599{ 600 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 601 602 if (hook == priv->upper) { 603 priv->upper = NULL; 604 if (priv->ifp != NULL) /* restore h/w csum */ 605 priv->ifp->if_hwassist = priv->hwassist; 606 } else if (hook == priv->lower) 607 priv->lower = NULL; 608 else if (hook == priv->orphan) 609 priv->orphan = NULL; 610 else 611 panic("%s: weird hook", __func__); 612 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 613 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) 614 ng_rmnode_self(NG_HOOK_NODE(hook)); /* reset node */ 615 return (0); 616} 617 618/****************************************************************** 619 INITIALIZATION 620******************************************************************/ 621 622/* 623 * Handle loading and unloading for this node type. 624 */ 625static int 626ng_ether_mod_event(module_t mod, int event, void *data) 627{ 628 struct ifnet *ifp; 629 int error = 0; 630 int s; 631 632 s = splnet(); 633 switch (event) { 634 case MOD_LOAD: 635 636 /* Register function hooks */ 637 if (ng_ether_attach_p != NULL) { 638 error = EEXIST; 639 break; 640 } 641 ng_ether_attach_p = ng_ether_attach; 642 ng_ether_detach_p = ng_ether_detach; 643 ng_ether_output_p = ng_ether_output; 644 ng_ether_input_p = ng_ether_input; 645 ng_ether_input_orphan_p = ng_ether_input_orphan; 646 647 /* Create nodes for any already-existing Ethernet interfaces */ 648 IFNET_RLOCK(); 649 TAILQ_FOREACH(ifp, &ifnet, if_link) { 650 if (ifp->if_type == IFT_ETHER 651 || ifp->if_type == IFT_L2VLAN) 652 ng_ether_attach(ifp); 653 } 654 IFNET_RUNLOCK(); 655 break; 656 657 case MOD_UNLOAD: 658 659 /* 660 * Note that the base code won't try to unload us until 661 * all nodes have been removed, and that can't happen 662 * until all Ethernet interfaces are removed. In any 663 * case, we know there are no nodes left if the action 664 * is MOD_UNLOAD, so there's no need to detach any nodes. 665 */ 666 667 /* Unregister function hooks */ 668 ng_ether_attach_p = NULL; 669 ng_ether_detach_p = NULL; 670 ng_ether_output_p = NULL; 671 ng_ether_input_p = NULL; 672 ng_ether_input_orphan_p = NULL; 673 break; 674 675 default: 676 error = EOPNOTSUPP; 677 break; 678 } 679 splx(s); 680 return (error); 681} 682 683