ng_ether.c revision 63053
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 * 40 * $FreeBSD: head/sys/netgraph/ng_ether.c 63053 2000-07-12 23:55:07Z archie $ 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}; 77typedef struct private *priv_p; 78 79/* Functional hooks called from if_ethersubr.c */ 80static void ng_ether_input(struct ifnet *ifp, 81 struct mbuf **mp, struct ether_header *eh); 82static void ng_ether_input_orphan(struct ifnet *ifp, 83 struct mbuf *m, struct ether_header *eh); 84static int ng_ether_output(struct ifnet *ifp, struct mbuf **mp); 85static void ng_ether_attach(struct ifnet *ifp); 86static void ng_ether_detach(struct ifnet *ifp); 87 88/* Other functions */ 89static void ng_ether_input2(node_p node, 90 struct mbuf **mp, struct ether_header *eh); 91static int ng_ether_glueback_header(struct mbuf **mp, 92 struct ether_header *eh); 93static int ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta); 94static int ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta); 95 96/* Netgraph node methods */ 97static ng_constructor_t ng_ether_constructor; 98static ng_rcvmsg_t ng_ether_rcvmsg; 99static ng_shutdown_t ng_ether_rmnode; 100static ng_newhook_t ng_ether_newhook; 101static ng_rcvdata_t ng_ether_rcvdata; 102static ng_disconnect_t ng_ether_disconnect; 103static int ng_ether_mod_event(module_t mod, int event, void *data); 104 105/* List of commands and how to convert arguments to/from ASCII */ 106static const struct ng_cmdlist ng_ether_cmdlist[] = { 107 { 108 NGM_ETHER_COOKIE, 109 NGM_ETHER_GET_IFNAME, 110 "getifname", 111 NULL, 112 &ng_parse_string_type 113 }, 114 { 115 NGM_ETHER_COOKIE, 116 NGM_ETHER_GET_IFINDEX, 117 "getifindex", 118 NULL, 119 &ng_parse_int32_type 120 }, 121 { 0 } 122}; 123 124static struct ng_type ng_ether_typestruct = { 125 NG_VERSION, 126 NG_ETHER_NODE_TYPE, 127 ng_ether_mod_event, 128 ng_ether_constructor, 129 ng_ether_rcvmsg, 130 ng_ether_rmnode, 131 ng_ether_newhook, 132 NULL, 133 NULL, 134 ng_ether_rcvdata, 135 ng_ether_rcvdata, 136 ng_ether_disconnect, 137 ng_ether_cmdlist, 138}; 139NETGRAPH_INIT(ether, &ng_ether_typestruct); 140 141/****************************************************************** 142 ETHERNET FUNCTION HOOKS 143******************************************************************/ 144 145/* 146 * Handle a packet that has come in on an interface. We get to 147 * look at it here before any upper layer protocols do. 148 * 149 * NOTE: this function will get called at splimp() 150 */ 151static void 152ng_ether_input(struct ifnet *ifp, 153 struct mbuf **mp, struct ether_header *eh) 154{ 155 const node_p node = IFP2NG(ifp); 156 const priv_p priv = node->private; 157 158 /* If "lower" hook not connected, let packet continue */ 159 if (priv->lower == NULL || priv->lowerOrphan) 160 return; 161 ng_ether_input2(node, mp, eh); 162} 163 164/* 165 * Handle a packet that has come in on an interface, and which 166 * does not match any of our known protocols (an ``orphan''). 167 * 168 * NOTE: this function will get called at splimp() 169 */ 170static void 171ng_ether_input_orphan(struct ifnet *ifp, 172 struct mbuf *m, struct ether_header *eh) 173{ 174 const node_p node = IFP2NG(ifp); 175 const priv_p priv = node->private; 176 177 /* If "orphan" hook not connected, let packet continue */ 178 if (priv->lower == NULL || !priv->lowerOrphan) { 179 m_freem(m); 180 return; 181 } 182 ng_ether_input2(node, &m, eh); 183 if (m != NULL) 184 m_freem(m); 185} 186 187/* 188 * Handle a packet that has come in on an interface. 189 * The Ethernet header has already been detached from the mbuf, 190 * so we have to put it back. 191 * 192 * NOTE: this function will get called at splimp() 193 */ 194static void 195ng_ether_input2(node_p node, struct mbuf **mp, struct ether_header *eh) 196{ 197 const priv_p priv = node->private; 198 meta_p meta = NULL; 199 int error; 200 201 /* Glue Ethernet header back on */ 202 if ((error = ng_ether_glueback_header(mp, eh)) != 0) 203 return; 204 205 /* Send out lower/orphan hook */ 206 (void)ng_queue_data(priv->lower, *mp, meta); 207 *mp = NULL; 208} 209 210/* 211 * Handle a packet that is going out on an interface. 212 * The Ethernet header is already attached to the mbuf. 213 */ 214static int 215ng_ether_output(struct ifnet *ifp, struct mbuf **mp) 216{ 217 const node_p node = IFP2NG(ifp); 218 const priv_p priv = node->private; 219 meta_p meta = NULL; 220 int error = 0; 221 222 /* If "upper" hook not connected, let packet continue */ 223 if (priv->upper == NULL) 224 return (0); 225 226 /* Send it out "upper" hook */ 227 NG_SEND_DATA_RET(error, priv->upper, *mp, meta); 228 229 /* If we got a reflected packet back, handle it */ 230 if (error == 0 && *mp != NULL) { 231 error = ng_ether_rcv_upper(node, *mp, meta); 232 *mp = NULL; 233 } 234 return (error); 235} 236 237/* 238 * A new Ethernet interface has been attached. 239 * Create a new node for it, etc. 240 */ 241static void 242ng_ether_attach(struct ifnet *ifp) 243{ 244 char name[IFNAMSIZ + 1]; 245 priv_p priv; 246 node_p node; 247 248 /* Create node */ 249 KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __FUNCTION__)); 250 snprintf(name, sizeof(name), "%s%d", ifp->if_name, ifp->if_unit); 251 if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) { 252 log(LOG_ERR, "%s: can't %s for %s\n", 253 __FUNCTION__, "create node", name); 254 return; 255 } 256 257 /* Allocate private data */ 258 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT); 259 if (priv == NULL) { 260 log(LOG_ERR, "%s: can't %s for %s\n", 261 __FUNCTION__, "allocate memory", name); 262 ng_unref(node); 263 return; 264 } 265 bzero(priv, sizeof(*priv)); 266 node->private = priv; 267 priv->ifp = ifp; 268 IFP2NG(ifp) = node; 269 270 /* Try to give the node the same name as the interface */ 271 if (ng_name_node(node, name) != 0) { 272 log(LOG_WARNING, "%s: can't name node %s\n", 273 __FUNCTION__, name); 274 } 275} 276 277/* 278 * An Ethernet interface is being detached. 279 * Destroy its node. 280 */ 281static void 282ng_ether_detach(struct ifnet *ifp) 283{ 284 const node_p node = IFP2NG(ifp); 285 priv_p priv; 286 287 if (node == NULL) /* no node (why not?), ignore */ 288 return; 289 ng_rmnode(node); /* break all links to other nodes */ 290 IFP2NG(ifp) = NULL; /* detach node from interface */ 291 priv = node->private; /* free node private info */ 292 bzero(priv, sizeof(*priv)); 293 FREE(priv, M_NETGRAPH); 294 node->private = NULL; 295 ng_unref(node); /* free node itself */ 296} 297 298/* 299 * Optimization for gluing the Ethernet header back onto 300 * the front of an incoming packet. 301 */ 302static int 303ng_ether_glueback_header(struct mbuf **mp, struct ether_header *eh) 304{ 305 struct mbuf *m = *mp; 306 uintfptr_t room; 307 int error = 0; 308 309 /* 310 * Possibly the header is already on the front. 311 * If this is the case so just move the markers back 312 * to re-include it. We lucked out. 313 * This allows us to avoid a yucky m_pullup 314 * in later nodes if it works. 315 */ 316 if (eh == mtod(m, struct ether_header *) - 1) { 317 m->m_len += sizeof(*eh); 318 m->m_data -= sizeof(*eh); 319 m->m_pkthdr.len += sizeof(*eh); 320 goto done; 321 } 322 323 /* 324 * Alternatively there may be room even though 325 * it is stored somewhere else. If so, copy it in. 326 * This only safe because we KNOW that this packet has 327 * just been generated by an ethernet card, so there are 328 * no aliases to the buffer (not so for outgoing packets). 329 * Nearly all ethernet cards will end up producing mbufs 330 * that fall into these cases. So we are not optimizing 331 * contorted cases. 332 */ 333 if ((m->m_flags & M_EXT) != 0) { 334 room = mtod(m, caddr_t) - m->m_ext.ext_buf; 335 if (room > m->m_ext.ext_size) /* garbage, fail immediately */ 336 room = 0; 337 } else 338 room = mtod(m, caddr_t) - m->m_pktdat; 339 340 /* 341 * If we have room, just copy it and adjust 342 */ 343 if (room >= sizeof(*eh)) { 344 m->m_len += sizeof(*eh); 345 m->m_data -= sizeof(*eh); 346 m->m_pkthdr.len += sizeof(*eh); 347 goto copy; 348 } 349 350 /* 351 * Doing anything more is likely to get more 352 * expensive than it's worth.. 353 * it's probable that everything else is in one 354 * big lump. The next node will do an m_pullup() 355 * for exactly the amount of data it needs and 356 * hopefully everything after that will not 357 * need one. So let's just use M_PREPEND. 358 */ 359 M_PREPEND(m, sizeof (*eh), M_DONTWAIT); 360 if (m == NULL) { 361 error = ENOBUFS; 362 goto done; 363 } 364 365copy: 366 /* Copy header and return (possibly new) mbuf */ 367 bcopy((caddr_t)eh, mtod(m, struct ether_header *), sizeof(*eh)); 368done: 369 *mp = m; 370 return error; 371} 372 373/****************************************************************** 374 NETGRAPH NODE METHODS 375******************************************************************/ 376 377/* 378 * It is not possible or allowable to create a node of this type. 379 * Nodes get created when the interface is attached (or, when 380 * this node type's KLD is loaded). 381 */ 382static int 383ng_ether_constructor(node_p *nodep) 384{ 385 return (EINVAL); 386} 387 388/* 389 * Check for attaching a new hook. 390 */ 391static int 392ng_ether_newhook(node_p node, hook_p hook, const char *name) 393{ 394 const priv_p priv = node->private; 395 u_char orphan = priv->lowerOrphan; 396 hook_p *hookptr; 397 398 /* Divert hook is an alias for lower */ 399 if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0) 400 name = NG_ETHER_HOOK_LOWER; 401 402 /* Which hook? */ 403 if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0) 404 hookptr = &priv->upper; 405 else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) { 406 hookptr = &priv->lower; 407 orphan = 0; 408 } else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) { 409 hookptr = &priv->lower; 410 orphan = 1; 411 } else 412 return (EINVAL); 413 414 /* Check if already connected (shouldn't be, but doesn't hurt) */ 415 if (*hookptr != NULL) 416 return (EISCONN); 417 418 /* OK */ 419 *hookptr = hook; 420 priv->lowerOrphan = orphan; 421 return (0); 422} 423 424/* 425 * Receive an incoming control message. 426 */ 427static int 428ng_ether_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr, 429 struct ng_mesg **rptr, hook_p lasthook) 430{ 431 const priv_p priv = node->private; 432 struct ng_mesg *resp = NULL; 433 int error = 0; 434 435 switch (msg->header.typecookie) { 436 case NGM_ETHER_COOKIE: 437 switch (msg->header.cmd) { 438 case NGM_ETHER_GET_IFNAME: 439 NG_MKRESPONSE(resp, msg, IFNAMSIZ + 1, M_NOWAIT); 440 if (resp == NULL) { 441 error = ENOMEM; 442 break; 443 } 444 snprintf(resp->data, IFNAMSIZ + 1, 445 "%s%d", priv->ifp->if_name, priv->ifp->if_unit); 446 break; 447 case NGM_ETHER_GET_IFINDEX: 448 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 449 if (resp == NULL) { 450 error = ENOMEM; 451 break; 452 } 453 *((u_int32_t *)resp->data) = priv->ifp->if_index; 454 break; 455 default: 456 error = EINVAL; 457 break; 458 } 459 break; 460 default: 461 error = EINVAL; 462 break; 463 } 464 if (rptr) 465 *rptr = resp; 466 else if (resp != NULL) 467 FREE(resp, M_NETGRAPH); 468 FREE(msg, M_NETGRAPH); 469 return (error); 470} 471 472/* 473 * Receive data on a hook. 474 */ 475static int 476ng_ether_rcvdata(hook_p hook, struct mbuf *m, meta_p meta, 477 struct mbuf **ret_m, meta_p *ret_meta) 478{ 479 const node_p node = hook->node; 480 const priv_p priv = node->private; 481 482 if (hook == priv->lower) 483 return ng_ether_rcv_lower(node, m, meta); 484 if (hook == priv->upper) 485 return ng_ether_rcv_upper(node, m, meta); 486 panic("%s: weird hook", __FUNCTION__); 487} 488 489/* 490 * Handle an mbuf received on the "lower" hook. 491 */ 492static int 493ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta) 494{ 495 const priv_p priv = node->private; 496 struct ether_header *eh; 497 498 /* Make sure header is fully pulled up */ 499 if (m->m_pkthdr.len < sizeof(struct ether_header)) { 500 NG_FREE_DATA(m, meta); 501 return (EINVAL); 502 } 503 if (m->m_len < sizeof(struct ether_header) 504 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) { 505 NG_FREE_META(meta); 506 return (ENOBUFS); 507 } 508 509 /* drop in the MAC address */ 510 eh = mtod(m, struct ether_header *); 511 bcopy((IFP2AC(priv->ifp))->ac_enaddr, eh->ether_shost, 6); 512 513 /* Send it on its way */ 514 NG_FREE_META(meta); 515 return ether_output_frame(priv->ifp, m); 516} 517 518/* 519 * Handle an mbuf received on the "upper" hook. 520 */ 521static int 522ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta) 523{ 524 const priv_p priv = node->private; 525 struct ether_header *eh; 526 527 /* Check length and pull off header */ 528 if (m->m_pkthdr.len < sizeof(*eh)) { 529 NG_FREE_DATA(m, meta); 530 return (EINVAL); 531 } 532 if (m->m_len < sizeof(*eh) && (m = m_pullup(m, sizeof(*eh))) == NULL) { 533 NG_FREE_META(meta); 534 return (ENOBUFS); 535 } 536 eh = mtod(m, struct ether_header *); 537 m->m_data += sizeof(*eh); 538 m->m_len -= sizeof(*eh); 539 m->m_pkthdr.len -= sizeof(*eh); 540 541 /* Route packet back in */ 542 NG_FREE_META(meta); 543 ether_demux(priv->ifp, eh, m); 544 return (0); 545} 546 547/* 548 * Shutdown node. This resets the node but does not remove it. 549 */ 550static int 551ng_ether_rmnode(node_p node) 552{ 553 ng_cutlinks(node); 554 node->flags &= ~NG_INVALID; /* bounce back to life */ 555 return (0); 556} 557 558/* 559 * Hook disconnection. 560 */ 561static int 562ng_ether_disconnect(hook_p hook) 563{ 564 const priv_p priv = hook->node->private; 565 566 if (hook == priv->upper) 567 priv->upper = NULL; 568 else if (hook == priv->lower) { 569 priv->lower = NULL; 570 priv->lowerOrphan = 0; 571 } else 572 panic("%s: weird hook", __FUNCTION__); 573 return (0); 574} 575 576/****************************************************************** 577 INITIALIZATION 578******************************************************************/ 579 580/* 581 * Handle loading and unloading for this node type. 582 */ 583static int 584ng_ether_mod_event(module_t mod, int event, void *data) 585{ 586 struct ifnet *ifp; 587 int error = 0; 588 int s; 589 590 s = splnet(); 591 switch (event) { 592 case MOD_LOAD: 593 594 /* Register function hooks */ 595 if (ng_ether_attach_p != NULL) { 596 error = EEXIST; 597 break; 598 } 599 ng_ether_attach_p = ng_ether_attach; 600 ng_ether_detach_p = ng_ether_detach; 601 ng_ether_output_p = ng_ether_output; 602 ng_ether_input_p = ng_ether_input; 603 ng_ether_input_orphan_p = ng_ether_input_orphan; 604 605 /* Create nodes for any already-existing Ethernet interfaces */ 606 TAILQ_FOREACH(ifp, &ifnet, if_link) { 607 if (ifp->if_type == IFT_ETHER) 608 ng_ether_attach(ifp); 609 } 610 break; 611 612 case MOD_UNLOAD: 613 614 /* 615 * Note that the base code won't try to unload us until 616 * all nodes have been removed, and that can't happen 617 * until all Ethernet interfaces are removed. In any 618 * case, we know there are no nodes left if the action 619 * is MOD_UNLOAD, so there's no need to detach any nodes. 620 */ 621 622 /* Unregister function hooks */ 623 ng_ether_attach_p = NULL; 624 ng_ether_detach_p = NULL; 625 ng_ether_output_p = NULL; 626 ng_ether_input_p = NULL; 627 ng_ether_input_orphan_p = NULL; 628 break; 629 630 default: 631 error = EOPNOTSUPP; 632 break; 633 } 634 splx(s); 635 return (error); 636} 637 638