1 2/* 3 * ng_ether.c 4 * 5 * Copyright (c) 1996-2000 Whistle Communications, Inc. 6 * All rights reserved. 7 * 8 * Subject to the following obligations and disclaimer of warranty, use and 9 * redistribution of this software, in source or object code forms, with or 10 * without modifications are expressly permitted by Whistle Communications; 11 * provided, however, that: 12 * 1. Any and all reproductions of the source or object code must include the 13 * copyright notice above and the following disclaimer of warranties; and 14 * 2. No rights are granted, in any manner or form, to use Whistle 15 * Communications, Inc. trademarks, including the mark "WHISTLE 16 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 17 * such appears in the above copyright notice or in the software. 18 * 19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 35 * OF SUCH DAMAGE. 36 * 37 * Authors: Archie Cobbs <archie@freebsd.org> 38 * Julian Elischer <julian@freebsd.org> 39 *
|
41 */ 42 43/* 44 * ng_ether(4) netgraph node type 45 */ 46 47#include <sys/param.h> 48#include <sys/systm.h> 49#include <sys/kernel.h> 50#include <sys/malloc.h> 51#include <sys/mbuf.h> 52#include <sys/errno.h> 53#include <sys/syslog.h> 54#include <sys/socket.h> 55 56#include <net/if.h> 57#include <net/if_types.h> 58#include <net/if_arp.h> 59#include <net/if_var.h> 60#include <net/ethernet.h> 61 62#include <netgraph/ng_message.h> 63#include <netgraph/netgraph.h> 64#include <netgraph/ng_parse.h> 65#include <netgraph/ng_ether.h> 66 67#define IFP2AC(IFP) ((struct arpcom *)IFP) 68#define IFP2NG(ifp) ((struct ng_node *)((struct arpcom *)(ifp))->ac_netgraph) 69 70/* Per-node private data */ 71struct private { 72 struct ifnet *ifp; /* associated interface */ 73 hook_p upper; /* upper hook connection */ 74 hook_p lower; /* lower OR orphan hook connection */ 75 u_char lowerOrphan; /* whether lower is lower or orphan */ 76 u_char autoSrcAddr; /* always overwrite source address */ 77 u_char promisc; /* promiscuous mode enabled */ 78 u_long hwassist; /* hardware checksum capabilities */ 79 u_int flags; /* flags e.g. really die */ 80}; 81typedef struct private *priv_p; 82 83/* Hook pointers used by if_ethersubr.c to callback to netgraph */ 84extern void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp); 85extern void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m); 86extern int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); 87extern void (*ng_ether_attach_p)(struct ifnet *ifp); 88extern void (*ng_ether_detach_p)(struct ifnet *ifp); 89 90/* Functional hooks called from if_ethersubr.c */ 91static void ng_ether_input(struct ifnet *ifp, struct mbuf **mp); 92static void ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m); 93static int ng_ether_output(struct ifnet *ifp, struct mbuf **mp); 94static void ng_ether_attach(struct ifnet *ifp); 95static void ng_ether_detach(struct ifnet *ifp); 96 97/* Other functions */ 98static void ng_ether_input2(node_p node, struct mbuf **mp); 99static int ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta); 100static int ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta); 101 102/* Netgraph node methods */ 103static ng_constructor_t ng_ether_constructor; 104static ng_rcvmsg_t ng_ether_rcvmsg; 105static ng_shutdown_t ng_ether_shutdown; 106static ng_newhook_t ng_ether_newhook; 107static ng_connect_t ng_ether_connect; 108static ng_rcvdata_t ng_ether_rcvdata; 109static ng_disconnect_t ng_ether_disconnect; 110static int ng_ether_mod_event(module_t mod, int event, void *data); 111 112/* List of commands and how to convert arguments to/from ASCII */ 113static const struct ng_cmdlist ng_ether_cmdlist[] = { 114 { 115 NGM_ETHER_COOKIE, 116 NGM_ETHER_GET_IFNAME, 117 "getifname", 118 NULL, 119 &ng_parse_string_type 120 }, 121 { 122 NGM_ETHER_COOKIE, 123 NGM_ETHER_GET_IFINDEX, 124 "getifindex", 125 NULL, 126 &ng_parse_int32_type 127 }, 128 { 129 NGM_ETHER_COOKIE, 130 NGM_ETHER_GET_ENADDR, 131 "getenaddr", 132 NULL, 133 &ng_parse_enaddr_type 134 }, 135 { 136 NGM_ETHER_COOKIE, 137 NGM_ETHER_SET_ENADDR, 138 "setenaddr", 139 &ng_parse_enaddr_type, 140 NULL 141 }, 142 { 143 NGM_ETHER_COOKIE, 144 NGM_ETHER_GET_PROMISC, 145 "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 NG_ABI_VERSION, 175 NG_ETHER_NODE_TYPE, 176 ng_ether_mod_event, 177 ng_ether_constructor, 178 ng_ether_rcvmsg, 179 ng_ether_shutdown, 180 ng_ether_newhook, 181 NULL, 182 ng_ether_connect, 183 ng_ether_rcvdata, 184 ng_ether_disconnect, 185 ng_ether_cmdlist, 186}; 187MODULE_VERSION(ng_ether, 1); 188NETGRAPH_INIT(ether, &ng_ether_typestruct); 189 190/****************************************************************** 191 ETHERNET FUNCTION HOOKS 192******************************************************************/ 193 194/* 195 * Handle a packet that has come in on an interface. We get to 196 * look at it here before any upper layer protocols do. 197 * 198 * NOTE: this function will get called at splimp() 199 */ 200static void 201ng_ether_input(struct ifnet *ifp, struct mbuf **mp) 202{ 203 const node_p node = IFP2NG(ifp); 204 const priv_p priv = NG_NODE_PRIVATE(node); 205 206 /* If "lower" hook not connected, let packet continue */ 207 if (priv->lower == NULL || priv->lowerOrphan) 208 return; 209 ng_ether_input2(node, mp); 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 224 /* If "orphan" hook not connected, let packet continue */ 225 if (priv->lower == NULL || !priv->lowerOrphan) { 226 m_freem(m); 227 return; 228 } 229 ng_ether_input2(node, &m); 230 if (m != NULL) 231 m_freem(m); 232} 233 234/* 235 * Handle a packet that has come in on an ethernet interface. 236 * The Ethernet header has already been detached from the mbuf, 237 * so we have to put it back. 238 * 239 * NOTE: this function will get called at splimp() 240 */ 241static void 242ng_ether_input2(node_p node, struct mbuf **mp) 243{ 244 const priv_p priv = NG_NODE_PRIVATE(node); 245 int error; 246 247 /* Send out lower/orphan hook */ 248 NG_SEND_DATA_ONLY(error, priv->lower, *mp); 249 *mp = NULL; 250} 251 252/* 253 * Handle a packet that is going out on an interface. 254 * The Ethernet header is already attached to the mbuf. 255 */ 256static int 257ng_ether_output(struct ifnet *ifp, struct mbuf **mp) 258{ 259 const node_p node = IFP2NG(ifp); 260 const priv_p priv = NG_NODE_PRIVATE(node); 261 int error = 0; 262 263 /* If "upper" hook not connected, let packet continue */ 264 if (priv->upper == NULL) 265 return (0); 266 267 /* Send it out "upper" hook */ 268 NG_SEND_DATA_ONLY(error, priv->upper, *mp); 269 return (error); 270} 271 272/* 273 * A new Ethernet interface has been attached. 274 * Create a new node for it, etc. 275 */ 276static void 277ng_ether_attach(struct ifnet *ifp) 278{ 279 priv_p priv; 280 node_p node; 281 282 /* Create node */ 283 KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__)); 284 if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) { 285 log(LOG_ERR, "%s: can't %s for %s\n", 286 __func__, "create node", ifp->if_xname); 287 return; 288 } 289 290 /* Allocate private data */ 291 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 292 if (priv == NULL) { 293 log(LOG_ERR, "%s: can't %s for %s\n", 294 __func__, "allocate memory", ifp->if_xname); 295 NG_NODE_UNREF(node); 296 return; 297 } 298 NG_NODE_SET_PRIVATE(node, priv); 299 priv->ifp = ifp; 300 IFP2NG(ifp) = node; 301 priv->autoSrcAddr = 1; 302 priv->hwassist = ifp->if_hwassist; 303 304 /* Try to give the node the same name as the interface */ 305 if (ng_name_node(node, ifp->if_xname) != 0) { 306 log(LOG_WARNING, "%s: can't name node %s\n", 307 __func__, ifp->if_xname); 308 } 309} 310 311/* 312 * An Ethernet interface is being detached. 313 * REALLY Destroy its node. 314 */ 315static void 316ng_ether_detach(struct ifnet *ifp) 317{ 318 const node_p node = IFP2NG(ifp); 319 const priv_p priv = NG_NODE_PRIVATE(node); 320 321 if (node == NULL) /* no node (why not?), ignore */ 322 return; 323 NG_NODE_REALLY_DIE(node); /* Force real removal of node */ 324 /* 325 * We can't assume the ifnet is still around when we run shutdown 326 * So zap it now. XXX We HOPE that anything running at this time 327 * handles it (as it should in the non netgraph case). 328 */ 329 IFP2NG(ifp) = NULL; 330 priv->ifp = NULL; /* XXX race if interrupted an output packet */ 331 ng_rmnode_self(node); /* remove all netgraph parts */ 332} 333 334/****************************************************************** 335 NETGRAPH NODE METHODS 336******************************************************************/ 337 338/* 339 * It is not possible or allowable to create a node of this type. 340 * Nodes get created when the interface is attached (or, when 341 * this node type's KLD is loaded). 342 */ 343static int 344ng_ether_constructor(node_p node) 345{ 346 return (EINVAL); 347} 348 349/* 350 * Check for attaching a new hook. 351 */ 352static int 353ng_ether_newhook(node_p node, hook_p hook, const char *name) 354{ 355 const priv_p priv = NG_NODE_PRIVATE(node); 356 u_char orphan = priv->lowerOrphan; 357 hook_p *hookptr; 358 359 /* Divert hook is an alias for lower */ 360 if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0) 361 name = NG_ETHER_HOOK_LOWER; 362 363 /* Which hook? */ 364 if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0) 365 hookptr = &priv->upper; 366 else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) { 367 hookptr = &priv->lower; 368 orphan = 0; 369 } else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) { 370 hookptr = &priv->lower; 371 orphan = 1; 372 } else 373 return (EINVAL); 374 375 /* Check if already connected (shouldn't be, but doesn't hurt) */ 376 if (*hookptr != NULL) 377 return (EISCONN); 378 379 /* Disable hardware checksums while 'upper' hook is connected */ 380 if (hookptr == &priv->upper) 381 priv->ifp->if_hwassist = 0; 382 383 /* OK */ 384 *hookptr = hook; 385 priv->lowerOrphan = orphan; 386 return (0); 387} 388 389/* 390 * Hooks are attached, adjust to force queueing. 391 * We don't really care which hook it is. 392 * they should all be queuing for outgoing data. 393 */ 394static int 395ng_ether_connect(hook_p hook) 396{ 397 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook)); 398 return (0); 399} 400 401/* 402 * Receive an incoming control message. 403 */ 404static int 405ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook) 406{ 407 const priv_p priv = NG_NODE_PRIVATE(node); 408 struct ng_mesg *resp = NULL; 409 int error = 0; 410 struct ng_mesg *msg; 411 412 NGI_GET_MSG(item, msg); 413 switch (msg->header.typecookie) { 414 case NGM_ETHER_COOKIE: 415 switch (msg->header.cmd) { 416 case NGM_ETHER_GET_IFNAME: 417 NG_MKRESPONSE(resp, msg, IFNAMSIZ + 1, M_NOWAIT); 418 if (resp == NULL) { 419 error = ENOMEM; 420 break; 421 } 422 strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ + 1); 423 break; 424 case NGM_ETHER_GET_IFINDEX: 425 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 426 if (resp == NULL) { 427 error = ENOMEM; 428 break; 429 } 430 *((u_int32_t *)resp->data) = priv->ifp->if_index; 431 break; 432 case NGM_ETHER_GET_ENADDR: 433 NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT); 434 if (resp == NULL) { 435 error = ENOMEM; 436 break; 437 } 438 bcopy((IFP2AC(priv->ifp))->ac_enaddr, 439 resp->data, ETHER_ADDR_LEN); 440 break; 441 case NGM_ETHER_SET_ENADDR: 442 { 443 if (msg->header.arglen != ETHER_ADDR_LEN) { 444 error = EINVAL; 445 break; 446 } 447 error = if_setlladdr(priv->ifp, 448 (u_char *)msg->data, ETHER_ADDR_LEN); 449 break; 450 } 451 case NGM_ETHER_GET_PROMISC: 452 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 453 if (resp == NULL) { 454 error = ENOMEM; 455 break; 456 } 457 *((u_int32_t *)resp->data) = priv->promisc; 458 break; 459 case NGM_ETHER_SET_PROMISC: 460 { 461 u_char want; 462 463 if (msg->header.arglen != sizeof(u_int32_t)) { 464 error = EINVAL; 465 break; 466 } 467 want = !!*((u_int32_t *)msg->data); 468 if (want ^ priv->promisc) { 469 if ((error = ifpromisc(priv->ifp, want)) != 0) 470 break; 471 priv->promisc = want; 472 } 473 break; 474 } 475 case NGM_ETHER_GET_AUTOSRC: 476 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 477 if (resp == NULL) { 478 error = ENOMEM; 479 break; 480 } 481 *((u_int32_t *)resp->data) = priv->autoSrcAddr; 482 break; 483 case NGM_ETHER_SET_AUTOSRC: 484 if (msg->header.arglen != sizeof(u_int32_t)) { 485 error = EINVAL; 486 break; 487 } 488 priv->autoSrcAddr = !!*((u_int32_t *)msg->data); 489 break; 490 default: 491 error = EINVAL; 492 break; 493 } 494 break; 495 default: 496 error = EINVAL; 497 break; 498 } 499 NG_RESPOND_MSG(error, node, item, resp); 500 NG_FREE_MSG(msg); 501 return (error); 502} 503 504/* 505 * Receive data on a hook. 506 */ 507static int 508ng_ether_rcvdata(hook_p hook, item_p item) 509{ 510 const node_p node = NG_HOOK_NODE(hook); 511 const priv_p priv = NG_NODE_PRIVATE(node); 512 struct mbuf *m; 513 meta_p meta; 514 515 NGI_GET_M(item, m); 516 NGI_GET_META(item, meta); 517 NG_FREE_ITEM(item); 518 if (hook == priv->lower) 519 return ng_ether_rcv_lower(node, m, meta); 520 if (hook == priv->upper) 521 return ng_ether_rcv_upper(node, m, meta); 522 panic("%s: weird hook", __func__); 523#ifdef RESTARTABLE_PANICS /* so we don;t get an error msg in LINT */ 524 return NULL; 525#endif 526} 527 528/* 529 * Handle an mbuf received on the "lower" hook. 530 */ 531static int 532ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta) 533{ 534 const priv_p priv = NG_NODE_PRIVATE(node); 535 struct ifnet *const ifp = priv->ifp; 536 537 /* Discard meta info */ 538 NG_FREE_META(meta); 539 540 /* Check whether interface is ready for packets */ 541 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) { 542 NG_FREE_M(m); 543 return (ENETDOWN); 544 } 545 546 /* Make sure header is fully pulled up */ 547 if (m->m_pkthdr.len < sizeof(struct ether_header)) { 548 NG_FREE_M(m); 549 return (EINVAL); 550 } 551 if (m->m_len < sizeof(struct ether_header) 552 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 553 return (ENOBUFS); 554 555 /* Drop in the MAC address if desired */ 556 if (priv->autoSrcAddr) { 557 558 /* Make the mbuf writable if it's not already */ 559 if (!M_WRITABLE(m) 560 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 561 return (ENOBUFS); 562 563 /* Overwrite source MAC address */ 564 bcopy((IFP2AC(ifp))->ac_enaddr, 565 mtod(m, struct ether_header *)->ether_shost, 566 ETHER_ADDR_LEN); 567 } 568 569 /* Send it on its way */ 570 return ether_output_frame(ifp, m); 571} 572 573/* 574 * Handle an mbuf received on the "upper" hook. 575 */ 576static int 577ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta) 578{ 579 const priv_p priv = NG_NODE_PRIVATE(node); 580 581 /* Discard meta info */ 582 NG_FREE_META(meta); 583 584 m->m_pkthdr.rcvif = priv->ifp; 585 586 /* Route packet back in */ 587 ether_demux(priv->ifp, m); 588 return (0); 589} 590 591/* 592 * Shutdown node. This resets the node but does not remove it 593 * unless the REALLY_DIE flag is set. 594 */ 595static int 596ng_ether_shutdown(node_p node) 597{ 598 const priv_p priv = NG_NODE_PRIVATE(node); 599 600 if (node->nd_flags & NG_REALLY_DIE) { 601 /* 602 * WE came here because the ethernet card is being unloaded, 603 * so stop being persistant. 604 * Actually undo all the things we did on creation. 605 * Assume the ifp has already been freed. 606 */ 607 NG_NODE_SET_PRIVATE(node, NULL); 608 FREE(priv, M_NETGRAPH); 609 NG_NODE_UNREF(node); /* free node itself */ 610 return (0); 611 } 612 if (priv->promisc) { /* disable promiscuous mode */ 613 (void)ifpromisc(priv->ifp, 0); 614 priv->promisc = 0; 615 } 616 priv->autoSrcAddr = 1; /* reset auto-src-addr flag */ 617 node->nd_flags &= ~NG_INVALID; /* Signal ng_rmnode we are persisant */ 618 return (0); 619} 620 621/* 622 * Hook disconnection. 623 */ 624static int 625ng_ether_disconnect(hook_p hook) 626{ 627 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 628 629 if (hook == priv->upper) { 630 priv->upper = NULL;
|