1/*- 2 * 3 * Copyright (c) 1999-2001, Vitaly V Belekhov 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice unmodified, this list of conditions, and the following 11 * disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 *
| 1/*- 2 * 3 * Copyright (c) 1999-2001, Vitaly V Belekhov 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice unmodified, this list of conditions, and the following 11 * disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 *
|
46 47#include <netgraph/ng_message.h> 48#include <netgraph/netgraph.h> 49#include <netgraph/ng_parse.h> 50#include <netgraph/ng_eiface.h> 51 52#include <net/bpf.h> 53#include <net/ethernet.h> 54#include <net/if_arp.h> 55 56static const struct ng_cmdlist ng_eiface_cmdlist[] = { 57 { 58 NGM_EIFACE_COOKIE, 59 NGM_EIFACE_GET_IFNAME, 60 "getifname", 61 NULL, 62 &ng_parse_string_type 63 }, 64 { 65 NGM_EIFACE_COOKIE, 66 NGM_EIFACE_SET, 67 "set", 68 &ng_parse_enaddr_type, 69 NULL 70 }, 71 { 0 } 72}; 73 74/* Node private data */ 75struct ng_eiface_private { 76 struct ifnet *ifp; /* per-interface network data */ 77 int unit; /* Interface unit number */ 78 node_p node; /* Our netgraph node */ 79 hook_p ether; /* Hook for ethernet stream */ 80}; 81typedef struct ng_eiface_private *priv_p; 82 83/* Interface methods */ 84static void ng_eiface_init(void *xsc); 85static void ng_eiface_start(struct ifnet *ifp); 86static int ng_eiface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data); 87#ifdef DEBUG 88static void ng_eiface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data); 89#endif 90 91/* Netgraph methods */ 92static int ng_eiface_mod_event(module_t, int, void *); 93static ng_constructor_t ng_eiface_constructor; 94static ng_rcvmsg_t ng_eiface_rcvmsg; 95static ng_shutdown_t ng_eiface_rmnode; 96static ng_newhook_t ng_eiface_newhook; 97static ng_rcvdata_t ng_eiface_rcvdata; 98static ng_disconnect_t ng_eiface_disconnect; 99 100/* Node type descriptor */ 101static struct ng_type typestruct = { 102 .version = NG_ABI_VERSION, 103 .name = NG_EIFACE_NODE_TYPE, 104 .mod_event = ng_eiface_mod_event, 105 .constructor = ng_eiface_constructor, 106 .rcvmsg = ng_eiface_rcvmsg, 107 .shutdown = ng_eiface_rmnode, 108 .newhook = ng_eiface_newhook, 109 .rcvdata = ng_eiface_rcvdata, 110 .disconnect = ng_eiface_disconnect, 111 .cmdlist = ng_eiface_cmdlist 112}; 113NETGRAPH_INIT(eiface, &typestruct); 114 115static struct unrhdr *ng_eiface_unit; 116 117/************************************************************************ 118 INTERFACE STUFF 119 ************************************************************************/ 120 121/* 122 * Process an ioctl for the virtual interface 123 */ 124static int 125ng_eiface_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 126{ 127 struct ifreq *const ifr = (struct ifreq *)data; 128 int s, error = 0; 129 130#ifdef DEBUG 131 ng_eiface_print_ioctl(ifp, command, data); 132#endif 133 s = splimp(); 134 switch (command) { 135 136 /* These two are mostly handled at a higher layer */ 137 case SIOCSIFADDR: 138 error = ether_ioctl(ifp, command, data); 139 break; 140 case SIOCGIFADDR: 141 break; 142 143 /* Set flags */ 144 case SIOCSIFFLAGS: 145 /* 146 * If the interface is marked up and stopped, then start it. 147 * If it is marked down and running, then stop it. 148 */ 149 if (ifp->if_flags & IFF_UP) { 150 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 151 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE); 152 ifp->if_drv_flags |= IFF_DRV_RUNNING; 153 } 154 } else { 155 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 156 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | 157 IFF_DRV_OACTIVE); 158 } 159 break; 160 161 /* Set the interface MTU */ 162 case SIOCSIFMTU: 163 if (ifr->ifr_mtu > NG_EIFACE_MTU_MAX || 164 ifr->ifr_mtu < NG_EIFACE_MTU_MIN) 165 error = EINVAL; 166 else 167 ifp->if_mtu = ifr->ifr_mtu; 168 break; 169 170 /* Stuff that's not supported */ 171 case SIOCADDMULTI: 172 case SIOCDELMULTI: 173 error = 0; 174 break; 175 case SIOCSIFPHYS: 176 error = EOPNOTSUPP; 177 break; 178 179 default: 180 error = EINVAL; 181 break; 182 } 183 splx(s); 184 return (error); 185} 186 187static void 188ng_eiface_init(void *xsc) 189{ 190 priv_p sc = xsc; 191 struct ifnet *ifp = sc->ifp; 192 int s; 193 194 s = splimp(); 195 196 ifp->if_drv_flags |= IFF_DRV_RUNNING; 197 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 198 199 splx(s); 200} 201 202/* 203 * We simply relay the packet to the "ether" hook, if it is connected. 204 * We have been through the netgraph locking and are guaranteed to 205 * be the only code running in this node at this time. 206 */ 207static void 208ng_eiface_start2(node_p node, hook_p hook, void *arg1, int arg2) 209{ 210 struct ifnet *ifp = arg1; 211 const priv_p priv = (priv_p)ifp->if_softc; 212 int error = 0; 213 struct mbuf *m; 214 215 /* Check interface flags */ 216 217 if (!((ifp->if_flags & IFF_UP) && 218 (ifp->if_drv_flags & IFF_DRV_RUNNING))) 219 return; 220 221 for (;;) { 222 /* 223 * Grab a packet to transmit. 224 */ 225 IF_DEQUEUE(&ifp->if_snd, m); 226 227 /* If there's nothing to send, break. */ 228 if (m == NULL) 229 break; 230 231 /* 232 * Berkeley packet filter. 233 * Pass packet to bpf if there is a listener. 234 * XXX is this safe? locking? 235 */ 236 BPF_MTAP(ifp, m); 237 238 if (ifp->if_flags & IFF_MONITOR) { 239 ifp->if_ipackets++; 240 m_freem(m); 241 continue; 242 } 243 244 /* 245 * Send packet; if hook is not connected, mbuf will get 246 * freed. 247 */ 248 NG_SEND_DATA_ONLY(error, priv->ether, m); 249 250 /* Update stats */ 251 if (error == 0) 252 ifp->if_opackets++; 253 else 254 ifp->if_oerrors++; 255 } 256 257 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 258 259 return; 260} 261 262/* 263 * This routine is called to deliver a packet out the interface. 264 * We simply queue the netgraph version to be called when netgraph locking 265 * allows it to happen. 266 * Until we know what the rest of the networking code is doing for 267 * locking, we don't know how we will interact with it. 268 * Take comfort from the fact that the ifnet struct is part of our 269 * private info and can't go away while we are queued. 270 * [Though we don't know it is still there now....] 271 * it is possible we don't gain anything from this because 272 * we would like to get the mbuf and queue it as data 273 * somehow, but we can't and if we did would we solve anything? 274 */ 275static void 276ng_eiface_start(struct ifnet *ifp) 277{ 278 279 const priv_p priv = (priv_p)ifp->if_softc; 280 281 /* Don't do anything if output is active */ 282 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) 283 return; 284 285 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 286 287 if (ng_send_fn(priv->node, NULL, &ng_eiface_start2, ifp, 0) != 0) 288 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 289} 290 291#ifdef DEBUG 292/* 293 * Display an ioctl to the virtual interface 294 */ 295 296static void 297ng_eiface_print_ioctl(struct ifnet *ifp, int command, caddr_t data) 298{ 299 char *str; 300 301 switch (command & IOC_DIRMASK) { 302 case IOC_VOID: 303 str = "IO"; 304 break; 305 case IOC_OUT: 306 str = "IOR"; 307 break; 308 case IOC_IN: 309 str = "IOW"; 310 break; 311 case IOC_INOUT: 312 str = "IORW"; 313 break; 314 default: 315 str = "IO??"; 316 } 317 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n", 318 ifp->if_xname, 319 str, 320 IOCGROUP(command), 321 command & 0xff, 322 IOCPARM_LEN(command)); 323} 324#endif /* DEBUG */ 325 326/************************************************************************ 327 NETGRAPH NODE STUFF 328 ************************************************************************/ 329 330/* 331 * Constructor for a node 332 */ 333static int 334ng_eiface_constructor(node_p node) 335{ 336 INIT_VNET_NETGRAPH(curvnet); 337 struct ifnet *ifp; 338 priv_p priv; 339 u_char eaddr[6] = {0,0,0,0,0,0}; 340 341 /* Allocate node and interface private structures */ 342 priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 343 if (priv == NULL) 344 return (ENOMEM); 345 346 ifp = priv->ifp = if_alloc(IFT_ETHER); 347 if (ifp == NULL) { 348 free(priv, M_NETGRAPH); 349 return (ENOSPC); 350 } 351 352 /* Link them together */ 353 ifp->if_softc = priv; 354 355 /* Get an interface unit number */ 356 priv->unit = alloc_unr(V_ng_eiface_unit); 357 358 /* Link together node and private info */ 359 NG_NODE_SET_PRIVATE(node, priv); 360 priv->node = node; 361 362 /* Initialize interface structure */ 363 if_initname(ifp, NG_EIFACE_EIFACE_NAME, priv->unit); 364 ifp->if_init = ng_eiface_init; 365 ifp->if_output = ether_output; 366 ifp->if_start = ng_eiface_start; 367 ifp->if_ioctl = ng_eiface_ioctl; 368 ifp->if_watchdog = NULL; 369 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; 370 ifp->if_flags = (IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST); 371 372#if 0 373 /* Give this node name */ 374 bzero(ifname, sizeof(ifname)); 375 sprintf(ifname, "if%s", ifp->if_xname); 376 (void)ng_name_node(node, ifname); 377#endif 378 379 /* Attach the interface */ 380 ether_ifattach(ifp, eaddr); 381 382 /* Done */ 383 return (0); 384} 385 386/* 387 * Give our ok for a hook to be added 388 */ 389static int 390ng_eiface_newhook(node_p node, hook_p hook, const char *name) 391{ 392 priv_p priv = NG_NODE_PRIVATE(node); 393 struct ifnet *ifp = priv->ifp; 394 395 if (strcmp(name, NG_EIFACE_HOOK_ETHER)) 396 return (EPFNOSUPPORT); 397 if (priv->ether != NULL) 398 return (EISCONN); 399 priv->ether = hook; 400 NG_HOOK_SET_PRIVATE(hook, &priv->ether); 401 402 if_link_state_change(ifp, LINK_STATE_UP); 403 404 return (0); 405} 406 407/* 408 * Receive a control message 409 */ 410static int 411ng_eiface_rcvmsg(node_p node, item_p item, hook_p lasthook) 412{ 413 const priv_p priv = NG_NODE_PRIVATE(node); 414 struct ifnet *const ifp = priv->ifp; 415 struct ng_mesg *resp = NULL; 416 int error = 0; 417 struct ng_mesg *msg; 418 419 NGI_GET_MSG(item, msg); 420 switch (msg->header.typecookie) { 421 case NGM_EIFACE_COOKIE: 422 switch (msg->header.cmd) { 423 424 case NGM_EIFACE_SET: 425 { 426 if (msg->header.arglen != ETHER_ADDR_LEN) { 427 error = EINVAL; 428 break; 429 } 430 error = if_setlladdr(priv->ifp, 431 (u_char *)msg->data, ETHER_ADDR_LEN); 432 break; 433 } 434 435 case NGM_EIFACE_GET_IFNAME: 436 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT); 437 if (resp == NULL) { 438 error = ENOMEM; 439 break; 440 } 441 strlcpy(resp->data, ifp->if_xname, IFNAMSIZ); 442 break; 443 444 case NGM_EIFACE_GET_IFADDRS: 445 { 446 struct ifaddr *ifa; 447 caddr_t ptr; 448 int buflen; 449 450 /* Determine size of response and allocate it */ 451 buflen = 0; 452 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 453 buflen += SA_SIZE(ifa->ifa_addr); 454 NG_MKRESPONSE(resp, msg, buflen, M_NOWAIT); 455 if (resp == NULL) { 456 error = ENOMEM; 457 break; 458 } 459 460 /* Add addresses */ 461 ptr = resp->data; 462 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 463 const int len = SA_SIZE(ifa->ifa_addr); 464 465 if (buflen < len) { 466 log(LOG_ERR, "%s: len changed?\n", 467 ifp->if_xname); 468 break; 469 } 470 bcopy(ifa->ifa_addr, ptr, len); 471 ptr += len; 472 buflen -= len; 473 } 474 break; 475 } 476 477 default: 478 error = EINVAL; 479 break; 480 } /* end of inner switch() */ 481 break; 482 case NGM_FLOW_COOKIE: 483 switch (msg->header.cmd) { 484 case NGM_LINK_IS_UP: 485 if_link_state_change(ifp, LINK_STATE_UP); 486 break; 487 case NGM_LINK_IS_DOWN: 488 if_link_state_change(ifp, LINK_STATE_DOWN); 489 break; 490 default: 491 break; 492 } 493 break; 494 default: 495 error = EINVAL; 496 break; 497 } 498 NG_RESPOND_MSG(error, node, item, resp); 499 NG_FREE_MSG(msg); 500 return (error); 501} 502 503/* 504 * Receive data from a hook. Pass the packet to the ether_input routine. 505 */ 506static int 507ng_eiface_rcvdata(hook_p hook, item_p item) 508{ 509 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 510 struct ifnet *const ifp = priv->ifp; 511 struct mbuf *m; 512 513 NGI_GET_M(item, m); 514 NG_FREE_ITEM(item); 515 516 if (!((ifp->if_flags & IFF_UP) && 517 (ifp->if_drv_flags & IFF_DRV_RUNNING))) { 518 NG_FREE_M(m); 519 return (ENETDOWN); 520 } 521 522 if (m->m_len < ETHER_HDR_LEN) { 523 m = m_pullup(m, ETHER_HDR_LEN); 524 if (m == NULL) 525 return (EINVAL); 526 } 527 528 /* Note receiving interface */ 529 m->m_pkthdr.rcvif = ifp; 530 531 /* Update interface stats */ 532 ifp->if_ipackets++; 533 534 (*ifp->if_input)(ifp, m); 535 536 /* Done */ 537 return (0); 538} 539 540/* 541 * Shutdown processing. 542 */ 543static int 544ng_eiface_rmnode(node_p node) 545{ 546 INIT_VNET_NETGRAPH(curvnet); 547 const priv_p priv = NG_NODE_PRIVATE(node); 548 struct ifnet *const ifp = priv->ifp; 549 550 /* 551 * the ifnet may be in a different vnet than the netgraph node, 552 * hence we have to change the current vnet context here. 553 */ 554 CURVNET_SET_QUIET(ifp->if_vnet); 555 ether_ifdetach(ifp); 556 if_free(ifp); 557 CURVNET_RESTORE(); 558 free_unr(V_ng_eiface_unit, priv->unit); 559 free(priv, M_NETGRAPH); 560 NG_NODE_SET_PRIVATE(node, NULL); 561 NG_NODE_UNREF(node); 562 return (0); 563} 564 565/* 566 * Hook disconnection 567 */ 568static int 569ng_eiface_disconnect(hook_p hook) 570{ 571 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 572 573 priv->ether = NULL; 574 return (0); 575} 576 577/* 578 * Handle loading and unloading for this node type. 579 */ 580static int 581ng_eiface_mod_event(module_t mod, int event, void *data) 582{ 583 int error = 0; 584 585 switch (event) { 586 case MOD_LOAD: 587 V_ng_eiface_unit = new_unrhdr(0, 0xffff, NULL); 588 break; 589 case MOD_UNLOAD: 590 delete_unrhdr(V_ng_eiface_unit); 591 break; 592 default: 593 error = EOPNOTSUPP; 594 break; 595 } 596 return (error); 597}
| 47 48#include <netgraph/ng_message.h> 49#include <netgraph/netgraph.h> 50#include <netgraph/ng_parse.h> 51#include <netgraph/ng_eiface.h> 52 53#include <net/bpf.h> 54#include <net/ethernet.h> 55#include <net/if_arp.h> 56 57static const struct ng_cmdlist ng_eiface_cmdlist[] = { 58 { 59 NGM_EIFACE_COOKIE, 60 NGM_EIFACE_GET_IFNAME, 61 "getifname", 62 NULL, 63 &ng_parse_string_type 64 }, 65 { 66 NGM_EIFACE_COOKIE, 67 NGM_EIFACE_SET, 68 "set", 69 &ng_parse_enaddr_type, 70 NULL 71 }, 72 { 0 } 73}; 74 75/* Node private data */ 76struct ng_eiface_private { 77 struct ifnet *ifp; /* per-interface network data */ 78 int unit; /* Interface unit number */ 79 node_p node; /* Our netgraph node */ 80 hook_p ether; /* Hook for ethernet stream */ 81}; 82typedef struct ng_eiface_private *priv_p; 83 84/* Interface methods */ 85static void ng_eiface_init(void *xsc); 86static void ng_eiface_start(struct ifnet *ifp); 87static int ng_eiface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data); 88#ifdef DEBUG 89static void ng_eiface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data); 90#endif 91 92/* Netgraph methods */ 93static int ng_eiface_mod_event(module_t, int, void *); 94static ng_constructor_t ng_eiface_constructor; 95static ng_rcvmsg_t ng_eiface_rcvmsg; 96static ng_shutdown_t ng_eiface_rmnode; 97static ng_newhook_t ng_eiface_newhook; 98static ng_rcvdata_t ng_eiface_rcvdata; 99static ng_disconnect_t ng_eiface_disconnect; 100 101/* Node type descriptor */ 102static struct ng_type typestruct = { 103 .version = NG_ABI_VERSION, 104 .name = NG_EIFACE_NODE_TYPE, 105 .mod_event = ng_eiface_mod_event, 106 .constructor = ng_eiface_constructor, 107 .rcvmsg = ng_eiface_rcvmsg, 108 .shutdown = ng_eiface_rmnode, 109 .newhook = ng_eiface_newhook, 110 .rcvdata = ng_eiface_rcvdata, 111 .disconnect = ng_eiface_disconnect, 112 .cmdlist = ng_eiface_cmdlist 113}; 114NETGRAPH_INIT(eiface, &typestruct); 115 116static struct unrhdr *ng_eiface_unit; 117 118/************************************************************************ 119 INTERFACE STUFF 120 ************************************************************************/ 121 122/* 123 * Process an ioctl for the virtual interface 124 */ 125static int 126ng_eiface_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 127{ 128 struct ifreq *const ifr = (struct ifreq *)data; 129 int s, error = 0; 130 131#ifdef DEBUG 132 ng_eiface_print_ioctl(ifp, command, data); 133#endif 134 s = splimp(); 135 switch (command) { 136 137 /* These two are mostly handled at a higher layer */ 138 case SIOCSIFADDR: 139 error = ether_ioctl(ifp, command, data); 140 break; 141 case SIOCGIFADDR: 142 break; 143 144 /* Set flags */ 145 case SIOCSIFFLAGS: 146 /* 147 * If the interface is marked up and stopped, then start it. 148 * If it is marked down and running, then stop it. 149 */ 150 if (ifp->if_flags & IFF_UP) { 151 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 152 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE); 153 ifp->if_drv_flags |= IFF_DRV_RUNNING; 154 } 155 } else { 156 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 157 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | 158 IFF_DRV_OACTIVE); 159 } 160 break; 161 162 /* Set the interface MTU */ 163 case SIOCSIFMTU: 164 if (ifr->ifr_mtu > NG_EIFACE_MTU_MAX || 165 ifr->ifr_mtu < NG_EIFACE_MTU_MIN) 166 error = EINVAL; 167 else 168 ifp->if_mtu = ifr->ifr_mtu; 169 break; 170 171 /* Stuff that's not supported */ 172 case SIOCADDMULTI: 173 case SIOCDELMULTI: 174 error = 0; 175 break; 176 case SIOCSIFPHYS: 177 error = EOPNOTSUPP; 178 break; 179 180 default: 181 error = EINVAL; 182 break; 183 } 184 splx(s); 185 return (error); 186} 187 188static void 189ng_eiface_init(void *xsc) 190{ 191 priv_p sc = xsc; 192 struct ifnet *ifp = sc->ifp; 193 int s; 194 195 s = splimp(); 196 197 ifp->if_drv_flags |= IFF_DRV_RUNNING; 198 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 199 200 splx(s); 201} 202 203/* 204 * We simply relay the packet to the "ether" hook, if it is connected. 205 * We have been through the netgraph locking and are guaranteed to 206 * be the only code running in this node at this time. 207 */ 208static void 209ng_eiface_start2(node_p node, hook_p hook, void *arg1, int arg2) 210{ 211 struct ifnet *ifp = arg1; 212 const priv_p priv = (priv_p)ifp->if_softc; 213 int error = 0; 214 struct mbuf *m; 215 216 /* Check interface flags */ 217 218 if (!((ifp->if_flags & IFF_UP) && 219 (ifp->if_drv_flags & IFF_DRV_RUNNING))) 220 return; 221 222 for (;;) { 223 /* 224 * Grab a packet to transmit. 225 */ 226 IF_DEQUEUE(&ifp->if_snd, m); 227 228 /* If there's nothing to send, break. */ 229 if (m == NULL) 230 break; 231 232 /* 233 * Berkeley packet filter. 234 * Pass packet to bpf if there is a listener. 235 * XXX is this safe? locking? 236 */ 237 BPF_MTAP(ifp, m); 238 239 if (ifp->if_flags & IFF_MONITOR) { 240 ifp->if_ipackets++; 241 m_freem(m); 242 continue; 243 } 244 245 /* 246 * Send packet; if hook is not connected, mbuf will get 247 * freed. 248 */ 249 NG_SEND_DATA_ONLY(error, priv->ether, m); 250 251 /* Update stats */ 252 if (error == 0) 253 ifp->if_opackets++; 254 else 255 ifp->if_oerrors++; 256 } 257 258 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 259 260 return; 261} 262 263/* 264 * This routine is called to deliver a packet out the interface. 265 * We simply queue the netgraph version to be called when netgraph locking 266 * allows it to happen. 267 * Until we know what the rest of the networking code is doing for 268 * locking, we don't know how we will interact with it. 269 * Take comfort from the fact that the ifnet struct is part of our 270 * private info and can't go away while we are queued. 271 * [Though we don't know it is still there now....] 272 * it is possible we don't gain anything from this because 273 * we would like to get the mbuf and queue it as data 274 * somehow, but we can't and if we did would we solve anything? 275 */ 276static void 277ng_eiface_start(struct ifnet *ifp) 278{ 279 280 const priv_p priv = (priv_p)ifp->if_softc; 281 282 /* Don't do anything if output is active */ 283 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) 284 return; 285 286 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 287 288 if (ng_send_fn(priv->node, NULL, &ng_eiface_start2, ifp, 0) != 0) 289 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 290} 291 292#ifdef DEBUG 293/* 294 * Display an ioctl to the virtual interface 295 */ 296 297static void 298ng_eiface_print_ioctl(struct ifnet *ifp, int command, caddr_t data) 299{ 300 char *str; 301 302 switch (command & IOC_DIRMASK) { 303 case IOC_VOID: 304 str = "IO"; 305 break; 306 case IOC_OUT: 307 str = "IOR"; 308 break; 309 case IOC_IN: 310 str = "IOW"; 311 break; 312 case IOC_INOUT: 313 str = "IORW"; 314 break; 315 default: 316 str = "IO??"; 317 } 318 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n", 319 ifp->if_xname, 320 str, 321 IOCGROUP(command), 322 command & 0xff, 323 IOCPARM_LEN(command)); 324} 325#endif /* DEBUG */ 326 327/************************************************************************ 328 NETGRAPH NODE STUFF 329 ************************************************************************/ 330 331/* 332 * Constructor for a node 333 */ 334static int 335ng_eiface_constructor(node_p node) 336{ 337 INIT_VNET_NETGRAPH(curvnet); 338 struct ifnet *ifp; 339 priv_p priv; 340 u_char eaddr[6] = {0,0,0,0,0,0}; 341 342 /* Allocate node and interface private structures */ 343 priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 344 if (priv == NULL) 345 return (ENOMEM); 346 347 ifp = priv->ifp = if_alloc(IFT_ETHER); 348 if (ifp == NULL) { 349 free(priv, M_NETGRAPH); 350 return (ENOSPC); 351 } 352 353 /* Link them together */ 354 ifp->if_softc = priv; 355 356 /* Get an interface unit number */ 357 priv->unit = alloc_unr(V_ng_eiface_unit); 358 359 /* Link together node and private info */ 360 NG_NODE_SET_PRIVATE(node, priv); 361 priv->node = node; 362 363 /* Initialize interface structure */ 364 if_initname(ifp, NG_EIFACE_EIFACE_NAME, priv->unit); 365 ifp->if_init = ng_eiface_init; 366 ifp->if_output = ether_output; 367 ifp->if_start = ng_eiface_start; 368 ifp->if_ioctl = ng_eiface_ioctl; 369 ifp->if_watchdog = NULL; 370 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; 371 ifp->if_flags = (IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST); 372 373#if 0 374 /* Give this node name */ 375 bzero(ifname, sizeof(ifname)); 376 sprintf(ifname, "if%s", ifp->if_xname); 377 (void)ng_name_node(node, ifname); 378#endif 379 380 /* Attach the interface */ 381 ether_ifattach(ifp, eaddr); 382 383 /* Done */ 384 return (0); 385} 386 387/* 388 * Give our ok for a hook to be added 389 */ 390static int 391ng_eiface_newhook(node_p node, hook_p hook, const char *name) 392{ 393 priv_p priv = NG_NODE_PRIVATE(node); 394 struct ifnet *ifp = priv->ifp; 395 396 if (strcmp(name, NG_EIFACE_HOOK_ETHER)) 397 return (EPFNOSUPPORT); 398 if (priv->ether != NULL) 399 return (EISCONN); 400 priv->ether = hook; 401 NG_HOOK_SET_PRIVATE(hook, &priv->ether); 402 403 if_link_state_change(ifp, LINK_STATE_UP); 404 405 return (0); 406} 407 408/* 409 * Receive a control message 410 */ 411static int 412ng_eiface_rcvmsg(node_p node, item_p item, hook_p lasthook) 413{ 414 const priv_p priv = NG_NODE_PRIVATE(node); 415 struct ifnet *const ifp = priv->ifp; 416 struct ng_mesg *resp = NULL; 417 int error = 0; 418 struct ng_mesg *msg; 419 420 NGI_GET_MSG(item, msg); 421 switch (msg->header.typecookie) { 422 case NGM_EIFACE_COOKIE: 423 switch (msg->header.cmd) { 424 425 case NGM_EIFACE_SET: 426 { 427 if (msg->header.arglen != ETHER_ADDR_LEN) { 428 error = EINVAL; 429 break; 430 } 431 error = if_setlladdr(priv->ifp, 432 (u_char *)msg->data, ETHER_ADDR_LEN); 433 break; 434 } 435 436 case NGM_EIFACE_GET_IFNAME: 437 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT); 438 if (resp == NULL) { 439 error = ENOMEM; 440 break; 441 } 442 strlcpy(resp->data, ifp->if_xname, IFNAMSIZ); 443 break; 444 445 case NGM_EIFACE_GET_IFADDRS: 446 { 447 struct ifaddr *ifa; 448 caddr_t ptr; 449 int buflen; 450 451 /* Determine size of response and allocate it */ 452 buflen = 0; 453 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 454 buflen += SA_SIZE(ifa->ifa_addr); 455 NG_MKRESPONSE(resp, msg, buflen, M_NOWAIT); 456 if (resp == NULL) { 457 error = ENOMEM; 458 break; 459 } 460 461 /* Add addresses */ 462 ptr = resp->data; 463 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 464 const int len = SA_SIZE(ifa->ifa_addr); 465 466 if (buflen < len) { 467 log(LOG_ERR, "%s: len changed?\n", 468 ifp->if_xname); 469 break; 470 } 471 bcopy(ifa->ifa_addr, ptr, len); 472 ptr += len; 473 buflen -= len; 474 } 475 break; 476 } 477 478 default: 479 error = EINVAL; 480 break; 481 } /* end of inner switch() */ 482 break; 483 case NGM_FLOW_COOKIE: 484 switch (msg->header.cmd) { 485 case NGM_LINK_IS_UP: 486 if_link_state_change(ifp, LINK_STATE_UP); 487 break; 488 case NGM_LINK_IS_DOWN: 489 if_link_state_change(ifp, LINK_STATE_DOWN); 490 break; 491 default: 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 from a hook. Pass the packet to the ether_input routine. 506 */ 507static int 508ng_eiface_rcvdata(hook_p hook, item_p item) 509{ 510 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 511 struct ifnet *const ifp = priv->ifp; 512 struct mbuf *m; 513 514 NGI_GET_M(item, m); 515 NG_FREE_ITEM(item); 516 517 if (!((ifp->if_flags & IFF_UP) && 518 (ifp->if_drv_flags & IFF_DRV_RUNNING))) { 519 NG_FREE_M(m); 520 return (ENETDOWN); 521 } 522 523 if (m->m_len < ETHER_HDR_LEN) { 524 m = m_pullup(m, ETHER_HDR_LEN); 525 if (m == NULL) 526 return (EINVAL); 527 } 528 529 /* Note receiving interface */ 530 m->m_pkthdr.rcvif = ifp; 531 532 /* Update interface stats */ 533 ifp->if_ipackets++; 534 535 (*ifp->if_input)(ifp, m); 536 537 /* Done */ 538 return (0); 539} 540 541/* 542 * Shutdown processing. 543 */ 544static int 545ng_eiface_rmnode(node_p node) 546{ 547 INIT_VNET_NETGRAPH(curvnet); 548 const priv_p priv = NG_NODE_PRIVATE(node); 549 struct ifnet *const ifp = priv->ifp; 550 551 /* 552 * the ifnet may be in a different vnet than the netgraph node, 553 * hence we have to change the current vnet context here. 554 */ 555 CURVNET_SET_QUIET(ifp->if_vnet); 556 ether_ifdetach(ifp); 557 if_free(ifp); 558 CURVNET_RESTORE(); 559 free_unr(V_ng_eiface_unit, priv->unit); 560 free(priv, M_NETGRAPH); 561 NG_NODE_SET_PRIVATE(node, NULL); 562 NG_NODE_UNREF(node); 563 return (0); 564} 565 566/* 567 * Hook disconnection 568 */ 569static int 570ng_eiface_disconnect(hook_p hook) 571{ 572 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 573 574 priv->ether = NULL; 575 return (0); 576} 577 578/* 579 * Handle loading and unloading for this node type. 580 */ 581static int 582ng_eiface_mod_event(module_t mod, int event, void *data) 583{ 584 int error = 0; 585 586 switch (event) { 587 case MOD_LOAD: 588 V_ng_eiface_unit = new_unrhdr(0, 0xffff, NULL); 589 break; 590 case MOD_UNLOAD: 591 delete_unrhdr(V_ng_eiface_unit); 592 break; 593 default: 594 error = EOPNOTSUPP; 595 break; 596 } 597 return (error); 598}
|