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 *
| 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 *
|
67#define IFP2NG(ifp) ((struct ng_node *)((struct arpcom *)(ifp))->ac_netgraph) 68 69/* Per-node private data */ 70struct private { 71 struct ifnet *ifp; /* associated interface */ 72 hook_p upper; /* upper hook connection */ 73 hook_p lower; /* lower OR orphan hook connection */ 74 u_char lowerOrphan; /* whether lower is lower or orphan */ 75}; 76typedef struct private *priv_p; 77 78/* Functional hooks called from if_ethersubr.c */ 79static void ng_ether_input(struct ifnet *ifp, 80 struct mbuf **mp, struct ether_header *eh); 81static void ng_ether_input_orphan(struct ifnet *ifp, 82 struct mbuf *m, struct ether_header *eh); 83static int ng_ether_output(struct ifnet *ifp, struct mbuf **mp); 84static void ng_ether_attach(struct ifnet *ifp); 85static void ng_ether_detach(struct ifnet *ifp); 86 87/* Other functions */ 88static void ng_ether_input2(node_p node, 89 struct mbuf **mp, struct ether_header *eh); 90static int ng_ether_glueback_header(struct mbuf **mp, 91 struct ether_header *eh); 92static int ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta); 93static int ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta); 94 95/* Netgraph node methods */ 96static ng_constructor_t ng_ether_constructor; 97static ng_rcvmsg_t ng_ether_rcvmsg; 98static ng_shutdown_t ng_ether_rmnode; 99static ng_newhook_t ng_ether_newhook; 100static ng_rcvdata_t ng_ether_rcvdata; 101static ng_disconnect_t ng_ether_disconnect; 102static int ng_ether_mod_event(module_t mod, int event, void *data); 103 104/* List of commands and how to convert arguments to/from ASCII */ 105static const struct ng_cmdlist ng_ether_cmdlist[] = { 106 { 107 NGM_ETHER_COOKIE, 108 NGM_ETHER_GET_IFNAME, 109 "getifname", 110 NULL, 111 &ng_parse_string_type 112 }, 113 { 114 NGM_ETHER_COOKIE, 115 NGM_ETHER_GET_IFINDEX, 116 "getifindex", 117 NULL, 118 &ng_parse_int32_type 119 }, 120 { 0 } 121}; 122 123static struct ng_type ng_ether_typestruct = { 124 NG_VERSION, 125 NG_ETHER_NODE_TYPE, 126 ng_ether_mod_event, 127 ng_ether_constructor, 128 ng_ether_rcvmsg, 129 ng_ether_rmnode, 130 ng_ether_newhook, 131 NULL, 132 NULL, 133 ng_ether_rcvdata, 134 ng_ether_rcvdata, 135 ng_ether_disconnect, 136 ng_ether_cmdlist, 137}; 138NETGRAPH_INIT(ether, &ng_ether_typestruct); 139 140/****************************************************************** 141 ETHERNET FUNCTION HOOKS 142******************************************************************/ 143 144/* 145 * Handle a packet that has come in on an interface. We get to 146 * look at it here before any upper layer protocols do. 147 * 148 * NOTE: this function will get called at splimp() 149 */ 150static void 151ng_ether_input(struct ifnet *ifp, 152 struct mbuf **mp, struct ether_header *eh) 153{ 154 const node_p node = IFP2NG(ifp); 155 const priv_p priv = node->private; 156 157 /* If "lower" hook not connected, let packet continue */ 158 if (priv->lower == NULL || priv->lowerOrphan) 159 return; 160 ng_ether_input2(node, mp, eh); 161} 162 163/* 164 * Handle a packet that has come in on an interface, and which 165 * does not match any of our known protocols (an ``orphan''). 166 * 167 * NOTE: this function will get called at splimp() 168 */ 169static void 170ng_ether_input_orphan(struct ifnet *ifp, 171 struct mbuf *m, struct ether_header *eh) 172{ 173 const node_p node = IFP2NG(ifp); 174 const priv_p priv = node->private; 175 176 /* If "orphan" hook not connected, let packet continue */ 177 if (priv->lower == NULL || !priv->lowerOrphan) { 178 m_freem(m); 179 return; 180 } 181 ng_ether_input2(node, &m, eh); 182 if (m != NULL) 183 m_freem(m); 184} 185 186/* 187 * Handle a packet that has come in on an interface. 188 * The Ethernet header has already been detached from the mbuf, 189 * so we have to put it back. 190 * 191 * NOTE: this function will get called at splimp() 192 */ 193static void 194ng_ether_input2(node_p node, struct mbuf **mp, struct ether_header *eh) 195{ 196 const priv_p priv = node->private; 197 meta_p meta = NULL; 198 int error; 199 200 /* Glue Ethernet header back on */ 201 if ((error = ng_ether_glueback_header(mp, eh)) != 0) 202 return; 203 204 /* Send out lower/orphan hook */ 205 NG_SEND_DATAQ(error, priv->lower, *mp, meta); 206 207 /* Any reflected packet must come later due to queuing */ 208 *mp = NULL; 209} 210 211/* 212 * Handle a packet that is going out on an interface. 213 * The Ethernet header is already attached to the mbuf. 214 */ 215static int 216ng_ether_output(struct ifnet *ifp, struct mbuf **mp) 217{ 218 const node_p node = IFP2NG(ifp); 219 const priv_p priv = node->private; 220 meta_p meta = NULL; 221 int error = 0; 222 223 /* If "upper" hook not connected, let packet continue */ 224 if (priv->upper == NULL) 225 return (0); 226 227 /* Send it out "upper" hook */ 228 NG_SEND_DATA_RET(error, priv->upper, *mp, meta); 229 230 /* If we got a reflected packet back, handle it */ 231 if (error == 0 && *mp != NULL) { 232 error = ng_ether_rcv_upper(node, *mp, meta); 233 *mp = NULL; 234 } 235 return (error); 236} 237 238/* 239 * A new Ethernet interface has been attached. 240 * Create a new node for it, etc. 241 */ 242static void 243ng_ether_attach(struct ifnet *ifp) 244{ 245 char name[IFNAMSIZ + 1]; 246 priv_p priv; 247 node_p node; 248 249 /* Create node */ 250 KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __FUNCTION__)); 251 snprintf(name, sizeof(name), "%s%d", ifp->if_name, ifp->if_unit); 252 if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) { 253 log(LOG_ERR, "%s: can't %s for %s\n", 254 __FUNCTION__, "create node", name); 255 return; 256 } 257 258 /* Allocate private data */ 259 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT); 260 if (priv == NULL) { 261 log(LOG_ERR, "%s: can't %s for %s\n", 262 __FUNCTION__, "allocate memory", name); 263 ng_unref(node); 264 return; 265 } 266 bzero(priv, sizeof(*priv)); 267 node->private = priv; 268 priv->ifp = ifp; 269 IFP2NG(ifp) = node; 270 271 /* Try to give the node the same name as the interface */ 272 if (ng_name_node(node, name) != 0) { 273 log(LOG_WARNING, "%s: can't name node %s\n", 274 __FUNCTION__, name); 275 } 276} 277 278/* 279 * An Ethernet interface is being detached. 280 * Destroy its node. 281 */ 282static void 283ng_ether_detach(struct ifnet *ifp) 284{ 285 const node_p node = IFP2NG(ifp); 286 priv_p priv; 287 288 if (node == NULL) /* no node (why not?), ignore */ 289 return; 290 ng_rmnode(node); /* break all links to other nodes */ 291 IFP2NG(ifp) = NULL; /* detach node from interface */ 292 priv = node->private; /* free node private info */ 293 bzero(priv, sizeof(*priv)); 294 FREE(priv, M_NETGRAPH); 295 node->private = NULL; 296 ng_unref(node); /* free node itself */ 297} 298 299/* 300 * Optimization for gluing the Ethernet header back onto 301 * the front of an incoming packet. 302 */ 303static int 304ng_ether_glueback_header(struct mbuf **mp, struct ether_header *eh) 305{ 306 struct mbuf *m = *mp; 307 uintfptr_t room; 308 int error = 0; 309 310 /* 311 * Possibly the header is already on the front. 312 * If this is the case so just move the markers back 313 * to re-include it. We lucked out. 314 * This allows us to avoid a yucky m_pullup 315 * in later nodes if it works. 316 */ 317 if (eh == mtod(m, struct ether_header *) - 1) { 318 m->m_len += sizeof(*eh); 319 m->m_data -= sizeof(*eh); 320 m->m_pkthdr.len += sizeof(*eh); 321 goto done; 322 } 323 324 /* 325 * Alternatively there may be room even though 326 * it is stored somewhere else. If so, copy it in. 327 * This only safe because we KNOW that this packet has 328 * just been generated by an ethernet card, so there are 329 * no aliases to the buffer (not so for outgoing packets). 330 * Nearly all ethernet cards will end up producing mbufs 331 * that fall into these cases. So we are not optimizing 332 * contorted cases. 333 */ 334 if ((m->m_flags & M_EXT) != 0) { 335 room = mtod(m, caddr_t) - m->m_ext.ext_buf; 336 if (room > m->m_ext.ext_size) /* garbage, fail immediately */ 337 room = 0; 338 } else 339 room = mtod(m, caddr_t) - m->m_pktdat; 340 341 /* 342 * If we have room, just copy it and adjust 343 */ 344 if (room >= sizeof(*eh)) { 345 m->m_len += sizeof(*eh); 346 m->m_data -= sizeof(*eh); 347 m->m_pkthdr.len += sizeof(*eh); 348 goto copy; 349 } 350 351 /* 352 * Doing anything more is likely to get more 353 * expensive than it's worth.. 354 * it's probable that everything else is in one 355 * big lump. The next node will do an m_pullup() 356 * for exactly the amount of data it needs and 357 * hopefully everything after that will not 358 * need one. So let's just use M_PREPEND. 359 */ 360 M_PREPEND(m, sizeof (*eh), M_DONTWAIT); 361 if (m == NULL) { 362 error = ENOBUFS; 363 goto done; 364 } 365 366copy: 367 /* Copy header and return (possibly new) mbuf */ 368 bcopy((caddr_t)eh, mtod(m, struct ether_header *), sizeof(*eh)); 369done: 370 *mp = m; 371 return error; 372} 373 374/****************************************************************** 375 NETGRAPH NODE METHODS 376******************************************************************/ 377 378/* 379 * It is not possible or allowable to create a node of this type. 380 * Nodes get created when the interface is attached (or, when 381 * this node type's KLD is loaded). 382 */ 383static int 384ng_ether_constructor(node_p *nodep) 385{ 386 return (EINVAL); 387} 388 389/* 390 * Check for attaching a new hook. 391 */ 392static int 393ng_ether_newhook(node_p node, hook_p hook, const char *name) 394{ 395 const priv_p priv = node->private; 396 u_char orphan = priv->lowerOrphan; 397 hook_p *hookptr; 398 399 /* Divert hook is an alias for lower */ 400 if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0) 401 name = NG_ETHER_HOOK_LOWER; 402 403 /* Which hook? */ 404 if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0) 405 hookptr = &priv->upper; 406 else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) { 407 hookptr = &priv->lower; 408 orphan = 0; 409 } else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) { 410 hookptr = &priv->lower; 411 orphan = 1; 412 } else 413 return (EINVAL); 414 415 /* Check if already connected (shouldn't be, but doesn't hurt) */ 416 if (*hookptr != NULL) 417 return (EISCONN); 418 419 /* OK */ 420 *hookptr = hook; 421 priv->lowerOrphan = orphan; 422 return (0); 423} 424 425/* 426 * Receive an incoming control message. 427 */ 428static int 429ng_ether_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr, 430 struct ng_mesg **rptr, hook_p lasthook) 431{ 432 const priv_p priv = node->private; 433 struct ng_mesg *resp = NULL; 434 int error = 0; 435 436 switch (msg->header.typecookie) { 437 case NGM_ETHER_COOKIE: 438 switch (msg->header.cmd) { 439 case NGM_ETHER_GET_IFNAME: 440 NG_MKRESPONSE(resp, msg, IFNAMSIZ + 1, M_NOWAIT); 441 if (resp == NULL) { 442 error = ENOMEM; 443 break; 444 } 445 snprintf(resp->data, IFNAMSIZ + 1, 446 "%s%d", priv->ifp->if_name, priv->ifp->if_unit); 447 break; 448 case NGM_ETHER_GET_IFINDEX: 449 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 450 if (resp == NULL) { 451 error = ENOMEM; 452 break; 453 } 454 *((u_int32_t *)resp->data) = priv->ifp->if_index; 455 break; 456 default: 457 error = EINVAL; 458 break; 459 } 460 break; 461 default: 462 error = EINVAL; 463 break; 464 } 465 if (rptr) 466 *rptr = resp; 467 else if (resp != NULL) 468 FREE(resp, M_NETGRAPH); 469 FREE(msg, M_NETGRAPH); 470 return (error); 471} 472 473/* 474 * Receive data on a hook. 475 */ 476static int 477ng_ether_rcvdata(hook_p hook, struct mbuf *m, meta_p meta, 478 struct mbuf **ret_m, meta_p *ret_meta) 479{ 480 const node_p node = hook->node; 481 const priv_p priv = node->private; 482 483 if (hook == priv->lower) 484 return ng_ether_rcv_lower(node, m, meta); 485 if (hook == priv->upper) 486 return ng_ether_rcv_upper(node, m, meta); 487 panic("%s: weird hook", __FUNCTION__); 488} 489 490/* 491 * Handle an mbuf received on the "lower" hook. 492 */ 493static int 494ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta) 495{ 496 const priv_p priv = node->private;
| 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 NG_SEND_DATAQ(error, priv->lower, *mp, meta); 207 208 /* Any reflected packet must come later due to queuing */ 209 *mp = NULL; 210} 211 212/* 213 * Handle a packet that is going out on an interface. 214 * The Ethernet header is already attached to the mbuf. 215 */ 216static int 217ng_ether_output(struct ifnet *ifp, struct mbuf **mp) 218{ 219 const node_p node = IFP2NG(ifp); 220 const priv_p priv = node->private; 221 meta_p meta = NULL; 222 int error = 0; 223 224 /* If "upper" hook not connected, let packet continue */ 225 if (priv->upper == NULL) 226 return (0); 227 228 /* Send it out "upper" hook */ 229 NG_SEND_DATA_RET(error, priv->upper, *mp, meta); 230 231 /* If we got a reflected packet back, handle it */ 232 if (error == 0 && *mp != NULL) { 233 error = ng_ether_rcv_upper(node, *mp, meta); 234 *mp = NULL; 235 } 236 return (error); 237} 238 239/* 240 * A new Ethernet interface has been attached. 241 * Create a new node for it, etc. 242 */ 243static void 244ng_ether_attach(struct ifnet *ifp) 245{ 246 char name[IFNAMSIZ + 1]; 247 priv_p priv; 248 node_p node; 249 250 /* Create node */ 251 KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __FUNCTION__)); 252 snprintf(name, sizeof(name), "%s%d", ifp->if_name, ifp->if_unit); 253 if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) { 254 log(LOG_ERR, "%s: can't %s for %s\n", 255 __FUNCTION__, "create node", name); 256 return; 257 } 258 259 /* Allocate private data */ 260 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT); 261 if (priv == NULL) { 262 log(LOG_ERR, "%s: can't %s for %s\n", 263 __FUNCTION__, "allocate memory", name); 264 ng_unref(node); 265 return; 266 } 267 bzero(priv, sizeof(*priv)); 268 node->private = priv; 269 priv->ifp = ifp; 270 IFP2NG(ifp) = node; 271 272 /* Try to give the node the same name as the interface */ 273 if (ng_name_node(node, name) != 0) { 274 log(LOG_WARNING, "%s: can't name node %s\n", 275 __FUNCTION__, name); 276 } 277} 278 279/* 280 * An Ethernet interface is being detached. 281 * Destroy its node. 282 */ 283static void 284ng_ether_detach(struct ifnet *ifp) 285{ 286 const node_p node = IFP2NG(ifp); 287 priv_p priv; 288 289 if (node == NULL) /* no node (why not?), ignore */ 290 return; 291 ng_rmnode(node); /* break all links to other nodes */ 292 IFP2NG(ifp) = NULL; /* detach node from interface */ 293 priv = node->private; /* free node private info */ 294 bzero(priv, sizeof(*priv)); 295 FREE(priv, M_NETGRAPH); 296 node->private = NULL; 297 ng_unref(node); /* free node itself */ 298} 299 300/* 301 * Optimization for gluing the Ethernet header back onto 302 * the front of an incoming packet. 303 */ 304static int 305ng_ether_glueback_header(struct mbuf **mp, struct ether_header *eh) 306{ 307 struct mbuf *m = *mp; 308 uintfptr_t room; 309 int error = 0; 310 311 /* 312 * Possibly the header is already on the front. 313 * If this is the case so just move the markers back 314 * to re-include it. We lucked out. 315 * This allows us to avoid a yucky m_pullup 316 * in later nodes if it works. 317 */ 318 if (eh == mtod(m, struct ether_header *) - 1) { 319 m->m_len += sizeof(*eh); 320 m->m_data -= sizeof(*eh); 321 m->m_pkthdr.len += sizeof(*eh); 322 goto done; 323 } 324 325 /* 326 * Alternatively there may be room even though 327 * it is stored somewhere else. If so, copy it in. 328 * This only safe because we KNOW that this packet has 329 * just been generated by an ethernet card, so there are 330 * no aliases to the buffer (not so for outgoing packets). 331 * Nearly all ethernet cards will end up producing mbufs 332 * that fall into these cases. So we are not optimizing 333 * contorted cases. 334 */ 335 if ((m->m_flags & M_EXT) != 0) { 336 room = mtod(m, caddr_t) - m->m_ext.ext_buf; 337 if (room > m->m_ext.ext_size) /* garbage, fail immediately */ 338 room = 0; 339 } else 340 room = mtod(m, caddr_t) - m->m_pktdat; 341 342 /* 343 * If we have room, just copy it and adjust 344 */ 345 if (room >= sizeof(*eh)) { 346 m->m_len += sizeof(*eh); 347 m->m_data -= sizeof(*eh); 348 m->m_pkthdr.len += sizeof(*eh); 349 goto copy; 350 } 351 352 /* 353 * Doing anything more is likely to get more 354 * expensive than it's worth.. 355 * it's probable that everything else is in one 356 * big lump. The next node will do an m_pullup() 357 * for exactly the amount of data it needs and 358 * hopefully everything after that will not 359 * need one. So let's just use M_PREPEND. 360 */ 361 M_PREPEND(m, sizeof (*eh), M_DONTWAIT); 362 if (m == NULL) { 363 error = ENOBUFS; 364 goto done; 365 } 366 367copy: 368 /* Copy header and return (possibly new) mbuf */ 369 bcopy((caddr_t)eh, mtod(m, struct ether_header *), sizeof(*eh)); 370done: 371 *mp = m; 372 return error; 373} 374 375/****************************************************************** 376 NETGRAPH NODE METHODS 377******************************************************************/ 378 379/* 380 * It is not possible or allowable to create a node of this type. 381 * Nodes get created when the interface is attached (or, when 382 * this node type's KLD is loaded). 383 */ 384static int 385ng_ether_constructor(node_p *nodep) 386{ 387 return (EINVAL); 388} 389 390/* 391 * Check for attaching a new hook. 392 */ 393static int 394ng_ether_newhook(node_p node, hook_p hook, const char *name) 395{ 396 const priv_p priv = node->private; 397 u_char orphan = priv->lowerOrphan; 398 hook_p *hookptr; 399 400 /* Divert hook is an alias for lower */ 401 if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0) 402 name = NG_ETHER_HOOK_LOWER; 403 404 /* Which hook? */ 405 if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0) 406 hookptr = &priv->upper; 407 else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) { 408 hookptr = &priv->lower; 409 orphan = 0; 410 } else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) { 411 hookptr = &priv->lower; 412 orphan = 1; 413 } else 414 return (EINVAL); 415 416 /* Check if already connected (shouldn't be, but doesn't hurt) */ 417 if (*hookptr != NULL) 418 return (EISCONN); 419 420 /* OK */ 421 *hookptr = hook; 422 priv->lowerOrphan = orphan; 423 return (0); 424} 425 426/* 427 * Receive an incoming control message. 428 */ 429static int 430ng_ether_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr, 431 struct ng_mesg **rptr, hook_p lasthook) 432{ 433 const priv_p priv = node->private; 434 struct ng_mesg *resp = NULL; 435 int error = 0; 436 437 switch (msg->header.typecookie) { 438 case NGM_ETHER_COOKIE: 439 switch (msg->header.cmd) { 440 case NGM_ETHER_GET_IFNAME: 441 NG_MKRESPONSE(resp, msg, IFNAMSIZ + 1, M_NOWAIT); 442 if (resp == NULL) { 443 error = ENOMEM; 444 break; 445 } 446 snprintf(resp->data, IFNAMSIZ + 1, 447 "%s%d", priv->ifp->if_name, priv->ifp->if_unit); 448 break; 449 case NGM_ETHER_GET_IFINDEX: 450 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 451 if (resp == NULL) { 452 error = ENOMEM; 453 break; 454 } 455 *((u_int32_t *)resp->data) = priv->ifp->if_index; 456 break; 457 default: 458 error = EINVAL; 459 break; 460 } 461 break; 462 default: 463 error = EINVAL; 464 break; 465 } 466 if (rptr) 467 *rptr = resp; 468 else if (resp != NULL) 469 FREE(resp, M_NETGRAPH); 470 FREE(msg, M_NETGRAPH); 471 return (error); 472} 473 474/* 475 * Receive data on a hook. 476 */ 477static int 478ng_ether_rcvdata(hook_p hook, struct mbuf *m, meta_p meta, 479 struct mbuf **ret_m, meta_p *ret_meta) 480{ 481 const node_p node = hook->node; 482 const priv_p priv = node->private; 483 484 if (hook == priv->lower) 485 return ng_ether_rcv_lower(node, m, meta); 486 if (hook == priv->upper) 487 return ng_ether_rcv_upper(node, m, meta); 488 panic("%s: weird hook", __FUNCTION__); 489} 490 491/* 492 * Handle an mbuf received on the "lower" hook. 493 */ 494static int 495ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta) 496{ 497 const priv_p priv = node->private;
|