1 /* 2 * ng_eiface.c 3 * 4 * Copyright (c) 1999-2000, Vitaly V Belekhov 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $Id: ng_eiface.c,v 1.14 2000/03/15 12:28:44 vitaly Exp $ 30 * $FreeBSD: src/sys/netgraph/ng_eiface.c,v 1.4.2.5 2002/12/17 21:47:48 julian Exp $ 31 * $DragonFly: src/sys/netgraph/eiface/ng_eiface.c,v 1.8 2005/06/02 22:11:45 swildner Exp $ 32 */ 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/errno.h> 37 #include <sys/kernel.h> 38 #include <sys/malloc.h> 39 #include <sys/mbuf.h> 40 #include <sys/errno.h> 41 #include <sys/sockio.h> 42 #include <sys/socket.h> 43 #include <sys/syslog.h> 44 #include <sys/thread2.h> 45 46 #include <net/if.h> 47 #include <net/if_types.h> 48 #include <net/netisr.h> 49 50 51 #include <netgraph/ng_message.h> 52 #include <netgraph/netgraph.h> 53 #include <netgraph/ng_parse.h> 54 #include "ng_eiface.h" 55 56 #include <net/bpf.h> 57 #include <net/ethernet.h> 58 #include <net/if_arp.h> 59 60 static const struct ng_parse_struct_field ng_eiface_par_fields[] 61 = NG_EIFACE_PAR_FIELDS; 62 63 static const struct ng_parse_type ng_eiface_par_type = { 64 &ng_parse_struct_type, 65 &ng_eiface_par_fields 66 }; 67 68 static const struct ng_cmdlist ng_eiface_cmdlist[] = { 69 { 70 NGM_EIFACE_COOKIE, 71 NGM_EIFACE_SET, 72 "set", 73 &ng_eiface_par_type, 74 NULL 75 }, 76 { 0 } 77 }; 78 79 /* Node private data */ 80 struct ng_eiface_private { 81 struct arpcom arpcom; /* per-interface network data */ 82 struct ifnet *ifp; /* This interface */ 83 node_p node; /* Our netgraph node */ 84 hook_p ether; /* Hook for ethernet stream */ 85 struct private *next; /* When hung on the free list */ 86 }; 87 typedef struct ng_eiface_private *priv_p; 88 89 /* Interface methods */ 90 static void ng_eiface_init(void *xsc); 91 static void ng_eiface_start(struct ifnet *ifp); 92 static int ng_eiface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data); 93 #ifdef DEBUG 94 static void ng_eiface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data); 95 #endif 96 97 /* Netgraph methods */ 98 static ng_constructor_t ng_eiface_constructor; 99 static ng_rcvmsg_t ng_eiface_rcvmsg; 100 static ng_shutdown_t ng_eiface_rmnode; 101 static ng_newhook_t ng_eiface_newhook; 102 static ng_rcvdata_t ng_eiface_rcvdata; 103 static ng_connect_t ng_eiface_connect; 104 static ng_disconnect_t ng_eiface_disconnect; 105 106 /* Node type descriptor */ 107 static struct ng_type typestruct = { 108 NG_VERSION, 109 NG_EIFACE_NODE_TYPE, 110 NULL, 111 ng_eiface_constructor, 112 ng_eiface_rcvmsg, 113 ng_eiface_rmnode, 114 ng_eiface_newhook, 115 NULL, 116 ng_eiface_connect, 117 ng_eiface_rcvdata, 118 ng_eiface_rcvdata, 119 ng_eiface_disconnect, 120 ng_eiface_cmdlist 121 }; 122 NETGRAPH_INIT(eiface, &typestruct); 123 124 static char ng_eiface_ifname[] = NG_EIFACE_EIFACE_NAME; 125 static int ng_eiface_next_unit; 126 127 /************************************************************************ 128 INTERFACE STUFF 129 ************************************************************************/ 130 131 /* 132 * Process an ioctl for the virtual interface 133 */ 134 static int 135 ng_eiface_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 136 { 137 struct ifreq *const ifr = (struct ifreq *) data; 138 int error = 0; 139 140 #ifdef DEBUG 141 ng_eiface_print_ioctl(ifp, command, data); 142 #endif 143 crit_enter(); 144 switch (command) { 145 146 /* These two are mostly handled at a higher layer */ 147 case SIOCSIFADDR: 148 error = ether_ioctl(ifp, command, data); 149 break; 150 case SIOCGIFADDR: 151 break; 152 153 /* Set flags */ 154 case SIOCSIFFLAGS: 155 /* 156 * If the interface is marked up and stopped, then start it. 157 * If it is marked down and running, then stop it. 158 */ 159 if (ifr->ifr_flags & IFF_UP) { 160 if (!(ifp->if_flags & IFF_RUNNING)) { 161 ifp->if_flags &= ~(IFF_OACTIVE); 162 ifp->if_flags |= IFF_RUNNING; 163 } 164 } else { 165 if (ifp->if_flags & IFF_RUNNING) 166 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 167 } 168 break; 169 170 /* Set the interface MTU */ 171 case SIOCSIFMTU: 172 if (ifr->ifr_mtu > NG_EIFACE_MTU_MAX 173 || ifr->ifr_mtu < NG_EIFACE_MTU_MIN) 174 error = EINVAL; 175 else 176 ifp->if_mtu = ifr->ifr_mtu; 177 break; 178 179 /* Stuff that's not supported */ 180 case SIOCADDMULTI: 181 case SIOCDELMULTI: 182 error = 0; 183 break; 184 case SIOCSIFPHYS: 185 error = EOPNOTSUPP; 186 break; 187 188 default: 189 error = EINVAL; 190 break; 191 } 192 crit_exit(); 193 return (error); 194 } 195 196 static void 197 ng_eiface_init(void *xsc) 198 { 199 priv_p sc = xsc; 200 struct ifnet *ifp = sc->ifp; 201 202 crit_enter(); 203 204 ifp->if_flags |= IFF_RUNNING; 205 ifp->if_flags &= ~IFF_OACTIVE; 206 207 crit_exit(); 208 209 } 210 211 /* 212 * This routine is called to deliver a packet out the interface. 213 * We simply relay the packet to 214 * the ether hook, if it is connected. 215 */ 216 217 static void 218 ng_eiface_start(struct ifnet *ifp) 219 { 220 const priv_p priv = (priv_p) ifp->if_softc; 221 meta_p meta = NULL; 222 int len, error = 0; 223 struct mbuf *m; 224 225 /* Check interface flags */ 226 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 227 return; 228 229 /* Don't do anything if output is active */ 230 if( ifp->if_flags & IFF_OACTIVE ) 231 return; 232 233 ifp->if_flags |= IFF_OACTIVE; 234 235 /* 236 * Grab a packet to transmit. 237 */ 238 IF_DEQUEUE(&ifp->if_snd, m); 239 240 /* If there's nothing to send, return. */ 241 if(m == NULL) 242 { 243 ifp->if_flags &= ~IFF_OACTIVE; 244 return; 245 } 246 247 BPF_MTAP(ifp, m); 248 249 /* Copy length before the mbuf gets invalidated */ 250 len = m->m_pkthdr.len; 251 252 /* Send packet; if hook is not connected, mbuf will get freed. */ 253 NG_SEND_DATA(error, priv->ether, m, meta); 254 255 /* Update stats */ 256 if (error == 0) { 257 ifp->if_obytes += len; 258 ifp->if_opackets++; 259 } 260 261 ifp->if_flags &= ~IFF_OACTIVE; 262 263 return; 264 } 265 266 #ifdef DEBUG 267 /* 268 * Display an ioctl to the virtual interface 269 */ 270 271 static void 272 ng_eiface_print_ioctl(struct ifnet *ifp, int command, caddr_t data) 273 { 274 char *str; 275 276 switch (command & IOC_DIRMASK) { 277 case IOC_VOID: 278 str = "IO"; 279 break; 280 case IOC_OUT: 281 str = "IOR"; 282 break; 283 case IOC_IN: 284 str = "IOW"; 285 break; 286 case IOC_INOUT: 287 str = "IORW"; 288 break; 289 default: 290 str = "IO??"; 291 } 292 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n", 293 ifp->if_xname, 294 str, 295 IOCGROUP(command), 296 command & 0xff, 297 IOCPARM_LEN(command)); 298 } 299 #endif /* DEBUG */ 300 301 /************************************************************************ 302 NETGRAPH NODE STUFF 303 ************************************************************************/ 304 305 /* 306 * Constructor for a node 307 */ 308 static int 309 ng_eiface_constructor(node_p *nodep) 310 { 311 struct ifnet *ifp; 312 node_p node; 313 priv_p priv; 314 int error = 0; 315 316 /* Allocate node and interface private structures */ 317 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_WAITOK); 318 if (priv == NULL) 319 return (ENOMEM); 320 bzero(priv, sizeof(*priv)); 321 322 ifp = &(priv->arpcom.ac_if); 323 324 /* Link them together */ 325 ifp->if_softc = priv; 326 priv->ifp = ifp; 327 328 /* Call generic node constructor */ 329 if ((error = ng_make_node_common(&typestruct, nodep))) { 330 FREE(priv, M_NETGRAPH); 331 return (error); 332 } 333 node = *nodep; 334 335 /* Link together node and private info */ 336 node->private = priv; 337 priv->node = node; 338 339 /* Initialize interface structure */ 340 if_initname(ifp, ng_eiface_ifname, ng_eiface_next_unit++); 341 ifp->if_init = ng_eiface_init; 342 ifp->if_start = ng_eiface_start; 343 ifp->if_ioctl = ng_eiface_ioctl; 344 ifp->if_watchdog = NULL; 345 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; 346 ifp->if_flags = (IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST); 347 348 TAILQ_INIT(&ifp->if_addrhead); 349 350 /* Give this node name * 351 bzero(ifname, sizeof(ifname)); 352 sprintf(ifname, "if%s", ifp->if_xname); 353 (void) ng_name_node(node, ifname); 354 */ 355 356 /* Attach the interface */ 357 ether_ifattach(ifp, priv->arpcom.ac_enaddr); 358 359 /* Done */ 360 return (0); 361 } 362 363 /* 364 * Give our ok for a hook to be added 365 */ 366 static int 367 ng_eiface_newhook(node_p node, hook_p hook, const char *name) 368 { 369 priv_p priv = node->private; 370 371 if (strcmp(name, NG_EIFACE_HOOK_ETHER)) 372 return (EPFNOSUPPORT); 373 if (priv->ether != NULL) 374 return (EISCONN); 375 priv->ether = hook; 376 hook->private = &priv->ether; 377 378 return (0); 379 } 380 381 /* 382 * Receive a control message 383 */ 384 static int 385 ng_eiface_rcvmsg(node_p node, struct ng_mesg *msg, 386 const char *retaddr, struct ng_mesg **rptr) 387 { 388 const priv_p priv = node->private; 389 struct ifnet *const ifp = priv->ifp; 390 struct ng_mesg *resp = NULL; 391 int error = 0; 392 393 switch (msg->header.typecookie) { 394 case NGM_EIFACE_COOKIE: 395 switch (msg->header.cmd) { 396 397 case NGM_EIFACE_SET: 398 { 399 struct ng_eiface_par *eaddr; 400 401 if (msg->header.arglen != sizeof(struct ng_eiface_par)) 402 { 403 error = EINVAL; 404 break; 405 } 406 eaddr = (struct ng_eiface_par *)(msg->data); 407 408 priv->arpcom.ac_enaddr[0] = eaddr->oct0; 409 priv->arpcom.ac_enaddr[1] = eaddr->oct1; 410 priv->arpcom.ac_enaddr[2] = eaddr->oct2; 411 priv->arpcom.ac_enaddr[3] = eaddr->oct3; 412 priv->arpcom.ac_enaddr[4] = eaddr->oct4; 413 priv->arpcom.ac_enaddr[5] = eaddr->oct5; 414 415 break; 416 } 417 418 case NGM_EIFACE_GET_IFNAME: 419 { 420 struct ng_eiface_ifname *arg; 421 422 NG_MKRESPONSE(resp, msg, sizeof(*arg), M_NOWAIT); 423 if (resp == NULL) { 424 error = ENOMEM; 425 break; 426 } 427 arg = (struct ng_eiface_ifname *) resp->data; 428 sprintf(arg->ngif_name, 429 "%s", ifp->if_xname); /* XXX: strings */ 430 break; 431 } 432 433 case NGM_EIFACE_GET_IFADDRS: 434 { 435 struct ifaddr *ifa; 436 caddr_t ptr; 437 int buflen; 438 439 #define SA_SIZE(s) ((s)->sa_len<sizeof(*(s))? sizeof(*(s)):(s)->sa_len) 440 441 /* Determine size of response and allocate it */ 442 buflen = 0; 443 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 444 buflen += SA_SIZE(ifa->ifa_addr); 445 NG_MKRESPONSE(resp, msg, buflen, M_NOWAIT); 446 if (resp == NULL) { 447 error = ENOMEM; 448 break; 449 } 450 451 /* Add addresses */ 452 ptr = resp->data; 453 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 454 const int len = SA_SIZE(ifa->ifa_addr); 455 456 if (buflen < len) { 457 log(LOG_ERR, "%s: len changed?\n", 458 ifp->if_xname); 459 break; 460 } 461 bcopy(ifa->ifa_addr, ptr, len); 462 ptr += len; 463 buflen -= len; 464 } 465 break; 466 #undef SA_SIZE 467 } 468 469 default: 470 error = EINVAL; 471 break; 472 } 473 break; 474 default: 475 error = EINVAL; 476 break; 477 } 478 if (rptr) 479 *rptr = resp; 480 else if (resp) 481 FREE(resp, M_NETGRAPH); 482 FREE(msg, M_NETGRAPH); 483 return (error); 484 } 485 486 /* 487 * Recive data from a hook. Pass the packet to the ether_input routine. 488 */ 489 static int 490 ng_eiface_rcvdata(hook_p hook, struct mbuf *m, meta_p meta) 491 { 492 const priv_p priv = hook->node->private; 493 struct ifnet *const ifp = priv->ifp; 494 int error = 0; 495 struct ether_header *eh; 496 u_short ether_type; 497 498 /* Meta-data is end its life here... */ 499 NG_FREE_META(meta); 500 501 if (m == NULL) 502 { 503 printf("ng_eiface: mbuf is null.\n"); 504 return (EINVAL); 505 } 506 507 if ( !(ifp->if_flags & IFF_UP) ) { 508 return (ENETDOWN); 509 } 510 511 /* Note receiving interface */ 512 m->m_pkthdr.rcvif = ifp; 513 514 /* Update interface stats */ 515 ifp->if_ipackets++; 516 517 BPF_MTAP(ifp, m); 518 519 (*ifp->if_input)(ifp, m); 520 521 /* Done */ 522 return (error); 523 } 524 525 /* 526 * Because the BSD networking code doesn't support the removal of 527 * networking interfaces, iface nodes (once created) are persistent. 528 * So this method breaks all connections and marks the interface 529 * down, but does not remove the node. 530 */ 531 static int 532 ng_eiface_rmnode(node_p node) 533 { 534 const priv_p priv = node->private; 535 struct ifnet *const ifp = priv->ifp; 536 537 ng_cutlinks(node); 538 node->flags &= ~NG_INVALID; 539 ifp->if_flags &= ~(IFF_UP | IFF_RUNNING | IFF_OACTIVE); 540 return (0); 541 } 542 543 544 /* 545 * This is called once we've already connected a new hook to the other node. 546 * It gives us a chance to balk at the last minute. 547 */ 548 static int 549 ng_eiface_connect(hook_p hook) 550 { 551 /* be really amiable and just say "YUP that's OK by me! " */ 552 return (0); 553 } 554 555 /* 556 * Hook disconnection 557 */ 558 static int 559 ng_eiface_disconnect(hook_p hook) 560 { 561 const priv_p priv = hook->node->private; 562 563 priv->ether = NULL; 564 return (0); 565 } 566