ng_eiface.c revision 148887
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: head/sys/netgraph/ng_eiface.c 148887 2005-08-09 10:20:02Z rwatson $ 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 42#include <net/if.h> 43#include <net/if_dl.h> 44#include <net/if_types.h> 45#include <net/netisr.h> 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 (ifr->ifr_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 len, 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 /* Don't do anything if output is active */ 222 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) 223 return; 224 225 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 226 227 /* 228 * Grab a packet to transmit. 229 */ 230 IF_DEQUEUE(&ifp->if_snd, m); 231 232 /* If there's nothing to send, return. */ 233 if (m == NULL) { 234 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 235 return; 236 } 237 238 /* 239 * Berkeley packet filter. 240 * Pass packet to bpf if there is a listener. 241 * XXX is this safe? locking? 242 */ 243 BPF_MTAP(ifp, m); 244 245 /* Copy length before the mbuf gets invalidated */ 246 len = m->m_pkthdr.len; 247 248 /* 249 * Send packet; if hook is not connected, mbuf will get 250 * freed. 251 */ 252 NG_SEND_DATA_ONLY(error, priv->ether, m); 253 254 /* Update stats */ 255 if (error == 0) { 256 ifp->if_obytes += len; 257 ifp->if_opackets++; 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 */ 278static void 279ng_eiface_start(struct ifnet *ifp) 280{ 281 282 const priv_p priv = (priv_p)ifp->if_softc; 283 284 ng_send_fn(priv->node, NULL, &ng_eiface_start2, ifp, 0); 285} 286 287#ifdef DEBUG 288/* 289 * Display an ioctl to the virtual interface 290 */ 291 292static void 293ng_eiface_print_ioctl(struct ifnet *ifp, int command, caddr_t data) 294{ 295 char *str; 296 297 switch (command & IOC_DIRMASK) { 298 case IOC_VOID: 299 str = "IO"; 300 break; 301 case IOC_OUT: 302 str = "IOR"; 303 break; 304 case IOC_IN: 305 str = "IOW"; 306 break; 307 case IOC_INOUT: 308 str = "IORW"; 309 break; 310 default: 311 str = "IO??"; 312 } 313 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n", 314 ifp->if_xname, 315 str, 316 IOCGROUP(command), 317 command & 0xff, 318 IOCPARM_LEN(command)); 319} 320#endif /* DEBUG */ 321 322/************************************************************************ 323 NETGRAPH NODE STUFF 324 ************************************************************************/ 325 326/* 327 * Constructor for a node 328 */ 329static int 330ng_eiface_constructor(node_p node) 331{ 332 struct ifnet *ifp; 333 priv_p priv; 334 u_char eaddr[6] = {0,0,0,0,0,0}; 335 336 /* Allocate node and interface private structures */ 337 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 338 if (priv == NULL) 339 return (ENOMEM); 340 341 ifp = priv->ifp = if_alloc(IFT_ETHER); 342 if (ifp == NULL) { 343 free(priv, M_NETGRAPH); 344 return (ENOSPC); 345 } 346 347 /* Link them together */ 348 ifp->if_softc = priv; 349 350 /* Get an interface unit number */ 351 priv->unit = alloc_unr(ng_eiface_unit); 352 353 /* Link together node and private info */ 354 NG_NODE_SET_PRIVATE(node, priv); 355 priv->node = node; 356 357 /* Initialize interface structure */ 358 if_initname(ifp, NG_EIFACE_EIFACE_NAME, priv->unit); 359 ifp->if_init = ng_eiface_init; 360 ifp->if_output = ether_output; 361 ifp->if_start = ng_eiface_start; 362 ifp->if_ioctl = ng_eiface_ioctl; 363 ifp->if_watchdog = NULL; 364 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; 365 ifp->if_flags = (IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST); 366 367#if 0 368 /* Give this node name */ 369 bzero(ifname, sizeof(ifname)); 370 sprintf(ifname, "if%s", ifp->if_xname); 371 (void)ng_name_node(node, ifname); 372#endif 373 374 /* Attach the interface */ 375 ether_ifattach(ifp, eaddr); 376 377 /* Done */ 378 return (0); 379} 380 381/* 382 * Give our ok for a hook to be added 383 */ 384static int 385ng_eiface_newhook(node_p node, hook_p hook, const char *name) 386{ 387 priv_p priv = NG_NODE_PRIVATE(node); 388 struct ifnet *ifp = priv->ifp; 389 390 if (strcmp(name, NG_EIFACE_HOOK_ETHER)) 391 return (EPFNOSUPPORT); 392 if (priv->ether != NULL) 393 return (EISCONN); 394 priv->ether = hook; 395 NG_HOOK_SET_PRIVATE(hook, &priv->ether); 396 397 if_link_state_change(ifp, LINK_STATE_UP); 398 399 return (0); 400} 401 402/* 403 * Receive a control message 404 */ 405static int 406ng_eiface_rcvmsg(node_p node, item_p item, hook_p lasthook) 407{ 408 const priv_p priv = NG_NODE_PRIVATE(node); 409 struct ifnet *const ifp = priv->ifp; 410 struct ng_mesg *resp = NULL; 411 int error = 0; 412 struct ng_mesg *msg; 413 414 NGI_GET_MSG(item, msg); 415 switch (msg->header.typecookie) { 416 case NGM_EIFACE_COOKIE: 417 switch (msg->header.cmd) { 418 419 case NGM_EIFACE_SET: 420 { 421 struct ether_addr *eaddr; 422 struct ifaddr *ifa; 423 struct sockaddr_dl *sdl; 424 425 if (msg->header.arglen != sizeof(struct ether_addr)) { 426 error = EINVAL; 427 break; 428 } 429 eaddr = (struct ether_addr *)(msg->data); 430 bcopy(eaddr, IFP2ENADDR(priv->ifp), 431 ETHER_ADDR_LEN); 432 433 /* And put it in the ifaddr list */ 434 TAILQ_FOREACH(ifa, &(ifp->if_addrhead), ifa_link) { 435 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 436 if (sdl->sdl_type == IFT_ETHER) { 437 bcopy(IFP2ENADDR(ifp), 438 LLADDR(sdl), ifp->if_addrlen); 439 break; 440 } 441 } 442 break; 443 } 444 445 case NGM_EIFACE_GET_IFNAME: 446 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT); 447 if (resp == NULL) { 448 error = ENOMEM; 449 break; 450 } 451 strlcpy(resp->data, ifp->if_xname, IFNAMSIZ); 452 break; 453 454 case NGM_EIFACE_GET_IFADDRS: 455 { 456 struct ifaddr *ifa; 457 caddr_t ptr; 458 int buflen; 459 460#define SA_SIZE(s) ((s)->sa_len<sizeof(*(s))? sizeof(*(s)):(s)->sa_len) 461 462 /* Determine size of response and allocate it */ 463 buflen = 0; 464 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 465 buflen += SA_SIZE(ifa->ifa_addr); 466 NG_MKRESPONSE(resp, msg, buflen, M_NOWAIT); 467 if (resp == NULL) { 468 error = ENOMEM; 469 break; 470 } 471 472 /* Add addresses */ 473 ptr = resp->data; 474 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 475 const int len = SA_SIZE(ifa->ifa_addr); 476 477 if (buflen < len) { 478 log(LOG_ERR, "%s: len changed?\n", 479 ifp->if_xname); 480 break; 481 } 482 bcopy(ifa->ifa_addr, ptr, len); 483 ptr += len; 484 buflen -= len; 485 } 486 break; 487#undef SA_SIZE 488 } 489 490 default: 491 error = EINVAL; 492 break; 493 } /* end of inner switch() */ 494 break; 495 case NGM_FLOW_COOKIE: 496 switch (msg->header.cmd) { 497 case NGM_LINK_IS_UP: 498 if_link_state_change(ifp, LINK_STATE_UP); 499 break; 500 case NGM_LINK_IS_DOWN: 501 if_link_state_change(ifp, LINK_STATE_DOWN); 502 break; 503 default: 504 break; 505 } 506 break; 507 default: 508 error = EINVAL; 509 break; 510 } 511 NG_RESPOND_MSG(error, node, item, resp); 512 NG_FREE_MSG(msg); 513 return (error); 514} 515 516/* 517 * Receive data from a hook. Pass the packet to the ether_input routine. 518 */ 519static int 520ng_eiface_rcvdata(hook_p hook, item_p item) 521{ 522 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 523 struct ifnet *const ifp = priv->ifp; 524 struct mbuf *m; 525 526 NGI_GET_M(item, m); 527 NG_FREE_ITEM(item); 528 529 if (!((ifp->if_flags & IFF_UP) && 530 (ifp->if_drv_flags & IFF_DRV_RUNNING))) { 531 NG_FREE_M(m); 532 return (ENETDOWN); 533 } 534 535 if (m->m_len < ETHER_HDR_LEN) { 536 m = m_pullup(m, ETHER_HDR_LEN); 537 if (m == NULL) 538 return (EINVAL); 539 } 540 541 /* Note receiving interface */ 542 m->m_pkthdr.rcvif = ifp; 543 544 /* Update interface stats */ 545 ifp->if_ipackets++; 546 547 (*ifp->if_input)(ifp, m); 548 549 /* Done */ 550 return (0); 551} 552 553/* 554 * Shutdown processing. 555 */ 556static int 557ng_eiface_rmnode(node_p node) 558{ 559 const priv_p priv = NG_NODE_PRIVATE(node); 560 struct ifnet *const ifp = priv->ifp; 561 562 ether_ifdetach(ifp); 563 if_free(ifp); 564 free_unr(ng_eiface_unit, priv->unit); 565 FREE(priv, M_NETGRAPH); 566 NG_NODE_SET_PRIVATE(node, NULL); 567 NG_NODE_UNREF(node); 568 return (0); 569} 570 571/* 572 * Hook disconnection 573 */ 574static int 575ng_eiface_disconnect(hook_p hook) 576{ 577 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 578 579 priv->ether = NULL; 580 return (0); 581} 582 583/* 584 * Handle loading and unloading for this node type. 585 */ 586static int 587ng_eiface_mod_event(module_t mod, int event, void *data) 588{ 589 int error = 0; 590 591 switch (event) { 592 case MOD_LOAD: 593 ng_eiface_unit = new_unrhdr(0, 0xffff, NULL); 594 break; 595 case MOD_UNLOAD: 596 delete_unrhdr(ng_eiface_unit); 597 break; 598 default: 599 error = EOPNOTSUPP; 600 break; 601 } 602 return (error); 603} 604