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 * 28 * $FreeBSD$ 29 */ 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/errno.h> 34 #include <sys/kernel.h> 35 #include <sys/malloc.h> 36 #include <sys/mbuf.h> 37 #include <sys/errno.h> 38 #include <sys/sockio.h> 39 #include <sys/socket.h> 40 #include <sys/syslog.h> 41 #include <sys/vimage.h> 42 43 #include <net/if.h> 44 #include <net/if_types.h> 45 #include <net/netisr.h> 46 #include <net/route.h> 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 57 static 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 */ 76 struct 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 }; 82 typedef struct ng_eiface_private *priv_p; 83 84 /* Interface methods */ 85 static void ng_eiface_init(void *xsc); 86 static void ng_eiface_start(struct ifnet *ifp); 87 static int ng_eiface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data); 88 #ifdef DEBUG 89 static void ng_eiface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data); 90 #endif 91 92 /* Netgraph methods */ 93 static int ng_eiface_mod_event(module_t, int, void *); 94 static ng_constructor_t ng_eiface_constructor; 95 static ng_rcvmsg_t ng_eiface_rcvmsg; 96 static ng_shutdown_t ng_eiface_rmnode; 97 static ng_newhook_t ng_eiface_newhook; 98 static ng_rcvdata_t ng_eiface_rcvdata; 99 static ng_disconnect_t ng_eiface_disconnect; 100 101 /* Node type descriptor */ 102 static 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 }; 114 NETGRAPH_INIT(eiface, &typestruct); 115 116 #ifdef VIMAGE_GLOBALS 117 static struct unrhdr *ng_eiface_unit; 118 #endif 119 120 /************************************************************************ 121 INTERFACE STUFF 122 ************************************************************************/ 123 124 /* 125 * Process an ioctl for the virtual interface 126 */ 127 static int 128 ng_eiface_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 129 { 130 struct ifreq *const ifr = (struct ifreq *)data; 131 int s, error = 0; 132 133 #ifdef DEBUG 134 ng_eiface_print_ioctl(ifp, command, data); 135 #endif 136 s = splimp(); 137 switch (command) { 138 139 /* These two are mostly handled at a higher layer */ 140 case SIOCSIFADDR: 141 error = ether_ioctl(ifp, command, data); 142 break; 143 case SIOCGIFADDR: 144 break; 145 146 /* Set flags */ 147 case SIOCSIFFLAGS: 148 /* 149 * If the interface is marked up and stopped, then start it. 150 * If it is marked down and running, then stop it. 151 */ 152 if (ifp->if_flags & IFF_UP) { 153 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 154 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE); 155 ifp->if_drv_flags |= IFF_DRV_RUNNING; 156 } 157 } else { 158 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 159 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | 160 IFF_DRV_OACTIVE); 161 } 162 break; 163 164 /* Set the interface MTU */ 165 case SIOCSIFMTU: 166 if (ifr->ifr_mtu > NG_EIFACE_MTU_MAX || 167 ifr->ifr_mtu < NG_EIFACE_MTU_MIN) 168 error = EINVAL; 169 else 170 ifp->if_mtu = ifr->ifr_mtu; 171 break; 172 173 /* Stuff that's not supported */ 174 case SIOCADDMULTI: 175 case SIOCDELMULTI: 176 error = 0; 177 break; 178 case SIOCSIFPHYS: 179 error = EOPNOTSUPP; 180 break; 181 182 default: 183 error = EINVAL; 184 break; 185 } 186 splx(s); 187 return (error); 188 } 189 190 static void 191 ng_eiface_init(void *xsc) 192 { 193 priv_p sc = xsc; 194 struct ifnet *ifp = sc->ifp; 195 int s; 196 197 s = splimp(); 198 199 ifp->if_drv_flags |= IFF_DRV_RUNNING; 200 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 201 202 splx(s); 203 } 204 205 /* 206 * We simply relay the packet to the "ether" hook, if it is connected. 207 * We have been through the netgraph locking and are guaranteed to 208 * be the only code running in this node at this time. 209 */ 210 static void 211 ng_eiface_start2(node_p node, hook_p hook, void *arg1, int arg2) 212 { 213 struct ifnet *ifp = arg1; 214 const priv_p priv = (priv_p)ifp->if_softc; 215 int error = 0; 216 struct mbuf *m; 217 218 /* Check interface flags */ 219 220 if (!((ifp->if_flags & IFF_UP) && 221 (ifp->if_drv_flags & IFF_DRV_RUNNING))) 222 return; 223 224 for (;;) { 225 /* 226 * Grab a packet to transmit. 227 */ 228 IF_DEQUEUE(&ifp->if_snd, m); 229 230 /* If there's nothing to send, break. */ 231 if (m == NULL) 232 break; 233 234 /* 235 * Berkeley packet filter. 236 * Pass packet to bpf if there is a listener. 237 * XXX is this safe? locking? 238 */ 239 BPF_MTAP(ifp, m); 240 241 if (ifp->if_flags & IFF_MONITOR) { 242 ifp->if_ipackets++; 243 m_freem(m); 244 continue; 245 } 246 247 /* 248 * Send packet; if hook is not connected, mbuf will get 249 * freed. 250 */ 251 NG_SEND_DATA_ONLY(error, priv->ether, m); 252 253 /* Update stats */ 254 if (error == 0) 255 ifp->if_opackets++; 256 else 257 ifp->if_oerrors++; 258 } 259 260 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 261 262 return; 263 } 264 265 /* 266 * This routine is called to deliver a packet out the interface. 267 * We simply queue the netgraph version to be called when netgraph locking 268 * allows it to happen. 269 * Until we know what the rest of the networking code is doing for 270 * locking, we don't know how we will interact with it. 271 * Take comfort from the fact that the ifnet struct is part of our 272 * private info and can't go away while we are queued. 273 * [Though we don't know it is still there now....] 274 * it is possible we don't gain anything from this because 275 * we would like to get the mbuf and queue it as data 276 * somehow, but we can't and if we did would we solve anything? 277 */ 278 static void 279 ng_eiface_start(struct ifnet *ifp) 280 { 281 282 const priv_p priv = (priv_p)ifp->if_softc; 283 284 /* Don't do anything if output is active */ 285 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) 286 return; 287 288 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 289 290 if (ng_send_fn(priv->node, NULL, &ng_eiface_start2, ifp, 0) != 0) 291 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 292 } 293 294 #ifdef DEBUG 295 /* 296 * Display an ioctl to the virtual interface 297 */ 298 299 static void 300 ng_eiface_print_ioctl(struct ifnet *ifp, int command, caddr_t data) 301 { 302 char *str; 303 304 switch (command & IOC_DIRMASK) { 305 case IOC_VOID: 306 str = "IO"; 307 break; 308 case IOC_OUT: 309 str = "IOR"; 310 break; 311 case IOC_IN: 312 str = "IOW"; 313 break; 314 case IOC_INOUT: 315 str = "IORW"; 316 break; 317 default: 318 str = "IO??"; 319 } 320 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n", 321 ifp->if_xname, 322 str, 323 IOCGROUP(command), 324 command & 0xff, 325 IOCPARM_LEN(command)); 326 } 327 #endif /* DEBUG */ 328 329 /************************************************************************ 330 NETGRAPH NODE STUFF 331 ************************************************************************/ 332 333 /* 334 * Constructor for a node 335 */ 336 static int 337 ng_eiface_constructor(node_p node) 338 { 339 INIT_VNET_NETGRAPH(curvnet); 340 struct ifnet *ifp; 341 priv_p priv; 342 u_char eaddr[6] = {0,0,0,0,0,0}; 343 344 /* Allocate node and interface private structures */ 345 priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 346 if (priv == NULL) 347 return (ENOMEM); 348 349 ifp = priv->ifp = if_alloc(IFT_ETHER); 350 if (ifp == NULL) { 351 free(priv, M_NETGRAPH); 352 return (ENOSPC); 353 } 354 355 /* Link them together */ 356 ifp->if_softc = priv; 357 358 /* Get an interface unit number */ 359 priv->unit = alloc_unr(V_ng_eiface_unit); 360 361 /* Link together node and private info */ 362 NG_NODE_SET_PRIVATE(node, priv); 363 priv->node = node; 364 365 /* Initialize interface structure */ 366 if_initname(ifp, NG_EIFACE_EIFACE_NAME, priv->unit); 367 ifp->if_init = ng_eiface_init; 368 ifp->if_output = ether_output; 369 ifp->if_start = ng_eiface_start; 370 ifp->if_ioctl = ng_eiface_ioctl; 371 ifp->if_watchdog = NULL; 372 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; 373 ifp->if_flags = (IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST); 374 375 #if 0 376 /* Give this node name */ 377 bzero(ifname, sizeof(ifname)); 378 sprintf(ifname, "if%s", ifp->if_xname); 379 (void)ng_name_node(node, ifname); 380 #endif 381 382 /* Attach the interface */ 383 ether_ifattach(ifp, eaddr); 384 385 /* Done */ 386 return (0); 387 } 388 389 /* 390 * Give our ok for a hook to be added 391 */ 392 static int 393 ng_eiface_newhook(node_p node, hook_p hook, const char *name) 394 { 395 priv_p priv = NG_NODE_PRIVATE(node); 396 struct ifnet *ifp = priv->ifp; 397 398 if (strcmp(name, NG_EIFACE_HOOK_ETHER)) 399 return (EPFNOSUPPORT); 400 if (priv->ether != NULL) 401 return (EISCONN); 402 priv->ether = hook; 403 NG_HOOK_SET_PRIVATE(hook, &priv->ether); 404 405 if_link_state_change(ifp, LINK_STATE_UP); 406 407 return (0); 408 } 409 410 /* 411 * Receive a control message 412 */ 413 static int 414 ng_eiface_rcvmsg(node_p node, item_p item, hook_p lasthook) 415 { 416 const priv_p priv = NG_NODE_PRIVATE(node); 417 struct ifnet *const ifp = priv->ifp; 418 struct ng_mesg *resp = NULL; 419 int error = 0; 420 struct ng_mesg *msg; 421 422 NGI_GET_MSG(item, msg); 423 switch (msg->header.typecookie) { 424 case NGM_EIFACE_COOKIE: 425 switch (msg->header.cmd) { 426 427 case NGM_EIFACE_SET: 428 { 429 if (msg->header.arglen != ETHER_ADDR_LEN) { 430 error = EINVAL; 431 break; 432 } 433 error = if_setlladdr(priv->ifp, 434 (u_char *)msg->data, ETHER_ADDR_LEN); 435 break; 436 } 437 438 case NGM_EIFACE_GET_IFNAME: 439 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT); 440 if (resp == NULL) { 441 error = ENOMEM; 442 break; 443 } 444 strlcpy(resp->data, ifp->if_xname, IFNAMSIZ); 445 break; 446 447 case NGM_EIFACE_GET_IFADDRS: 448 { 449 struct ifaddr *ifa; 450 caddr_t ptr; 451 int buflen; 452 453 /* Determine size of response and allocate it */ 454 buflen = 0; 455 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 456 buflen += SA_SIZE(ifa->ifa_addr); 457 NG_MKRESPONSE(resp, msg, buflen, M_NOWAIT); 458 if (resp == NULL) { 459 error = ENOMEM; 460 break; 461 } 462 463 /* Add addresses */ 464 ptr = resp->data; 465 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 466 const int len = SA_SIZE(ifa->ifa_addr); 467 468 if (buflen < len) { 469 log(LOG_ERR, "%s: len changed?\n", 470 ifp->if_xname); 471 break; 472 } 473 bcopy(ifa->ifa_addr, ptr, len); 474 ptr += len; 475 buflen -= len; 476 } 477 break; 478 } 479 480 default: 481 error = EINVAL; 482 break; 483 } /* end of inner switch() */ 484 break; 485 case NGM_FLOW_COOKIE: 486 switch (msg->header.cmd) { 487 case NGM_LINK_IS_UP: 488 if_link_state_change(ifp, LINK_STATE_UP); 489 break; 490 case NGM_LINK_IS_DOWN: 491 if_link_state_change(ifp, LINK_STATE_DOWN); 492 break; 493 default: 494 break; 495 } 496 break; 497 default: 498 error = EINVAL; 499 break; 500 } 501 NG_RESPOND_MSG(error, node, item, resp); 502 NG_FREE_MSG(msg); 503 return (error); 504 } 505 506 /* 507 * Receive data from a hook. Pass the packet to the ether_input routine. 508 */ 509 static int 510 ng_eiface_rcvdata(hook_p hook, item_p item) 511 { 512 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 513 struct ifnet *const ifp = priv->ifp; 514 struct mbuf *m; 515 516 NGI_GET_M(item, m); 517 NG_FREE_ITEM(item); 518 519 if (!((ifp->if_flags & IFF_UP) && 520 (ifp->if_drv_flags & IFF_DRV_RUNNING))) { 521 NG_FREE_M(m); 522 return (ENETDOWN); 523 } 524 525 if (m->m_len < ETHER_HDR_LEN) { 526 m = m_pullup(m, ETHER_HDR_LEN); 527 if (m == NULL) 528 return (EINVAL); 529 } 530 531 /* Note receiving interface */ 532 m->m_pkthdr.rcvif = ifp; 533 534 /* Update interface stats */ 535 ifp->if_ipackets++; 536 537 (*ifp->if_input)(ifp, m); 538 539 /* Done */ 540 return (0); 541 } 542 543 /* 544 * Shutdown processing. 545 */ 546 static int 547 ng_eiface_rmnode(node_p node) 548 { 549 INIT_VNET_NETGRAPH(curvnet); 550 const priv_p priv = NG_NODE_PRIVATE(node); 551 struct ifnet *const ifp = priv->ifp; 552 553 /* 554 * the ifnet may be in a different vnet than the netgraph node, 555 * hence we have to change the current vnet context here. 556 */ 557 CURVNET_SET_QUIET(ifp->if_vnet); 558 ether_ifdetach(ifp); 559 if_free(ifp); 560 CURVNET_RESTORE(); 561 free_unr(V_ng_eiface_unit, priv->unit); 562 free(priv, M_NETGRAPH); 563 NG_NODE_SET_PRIVATE(node, NULL); 564 NG_NODE_UNREF(node); 565 return (0); 566 } 567 568 /* 569 * Hook disconnection 570 */ 571 static int 572 ng_eiface_disconnect(hook_p hook) 573 { 574 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 575 576 priv->ether = NULL; 577 return (0); 578 } 579 580 /* 581 * Handle loading and unloading for this node type. 582 */ 583 static int 584 ng_eiface_mod_event(module_t mod, int event, void *data) 585 { 586 int error = 0; 587 588 switch (event) { 589 case MOD_LOAD: 590 V_ng_eiface_unit = new_unrhdr(0, 0xffff, NULL); 591 break; 592 case MOD_UNLOAD: 593 delete_unrhdr(V_ng_eiface_unit); 594 break; 595 default: 596 error = EOPNOTSUPP; 597 break; 598 } 599 return (error); 600 } 601