1 /* 2 * ng_iface.c 3 */ 4 5 /*- 6 * Copyright (c) 1996-1999 Whistle Communications, Inc. 7 * All rights reserved. 8 * 9 * Subject to the following obligations and disclaimer of warranty, use and 10 * redistribution of this software, in source or object code forms, with or 11 * without modifications are expressly permitted by Whistle Communications; 12 * provided, however, that: 13 * 1. Any and all reproductions of the source or object code must include the 14 * copyright notice above and the following disclaimer of warranties; and 15 * 2. No rights are granted, in any manner or form, to use Whistle 16 * Communications, Inc. trademarks, including the mark "WHISTLE 17 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 18 * such appears in the above copyright notice or in the software. 19 * 20 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 21 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 22 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 23 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 25 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 26 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 27 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 28 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 29 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 30 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 31 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 32 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 35 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 36 * OF SUCH DAMAGE. 37 * 38 * Author: Archie Cobbs <archie@freebsd.org> 39 * 40 * $FreeBSD: src/sys/netgraph/ng_iface.c,v 1.48 2008/01/31 08:51:48 mav Exp $ 41 * $Whistle: ng_iface.c,v 1.33 1999/11/01 09:24:51 julian Exp $ 42 */ 43 44 /* 45 * This node is also a system networking interface. It has 46 * a hook for each protocol (IP, AppleTalk, IPX, etc). Packets 47 * are simply relayed between the interface and the hooks. 48 * 49 * Interfaces are named ng0, ng1, etc. New nodes take the 50 * first available interface name. 51 * 52 * This node also includes Berkeley packet filter support. 53 */ 54 55 #include "opt_inet.h" 56 #include "opt_inet6.h" 57 #include "opt_ipx.h" 58 59 #include <sys/param.h> 60 #include <sys/systm.h> 61 #include <sys/errno.h> 62 #include <sys/kernel.h> 63 #include <sys/malloc.h> 64 #include <sys/mbuf.h> 65 #include <sys/errno.h> 66 #include <sys/random.h> 67 #include <sys/sockio.h> 68 #include <sys/socket.h> 69 #include <sys/syslog.h> 70 #include <sys/libkern.h> 71 72 #include <net/if.h> 73 #include <net/if_types.h> 74 #include <net/ifq_var.h> 75 #include <net/bpf.h> 76 #include <net/netisr.h> 77 78 #include <netinet/in.h> 79 80 #include <netgraph7/ng_message.h> 81 #include <netgraph7/netgraph.h> 82 #include <netgraph7/ng_parse.h> 83 #include <netgraph7/cisco/ng_cisco.h> 84 #include "ng_iface.h" 85 86 #ifdef NG_SEPARATE_MALLOC 87 MALLOC_DEFINE(M_NETGRAPH_IFACE, "netgraph_iface", "netgraph iface node "); 88 #else 89 #define M_NETGRAPH_IFACE M_NETGRAPH 90 #endif 91 92 /* This struct describes one address family */ 93 struct iffam { 94 sa_family_t family; /* Address family */ 95 const char *hookname; /* Name for hook */ 96 }; 97 typedef const struct iffam *iffam_p; 98 99 /* List of address families supported by our interface */ 100 static const struct iffam gFamilies[] = { 101 { AF_INET, NG_IFACE_HOOK_INET }, 102 { AF_INET6, NG_IFACE_HOOK_INET6 }, 103 { AF_IPX, NG_IFACE_HOOK_IPX }, 104 { AF_ATM, NG_IFACE_HOOK_ATM }, 105 { AF_NATM, NG_IFACE_HOOK_NATM }, 106 }; 107 #define NUM_FAMILIES NELEM(gFamilies) 108 109 /* Node private data */ 110 struct ng_iface_private { 111 struct ifnet *ifp; /* Our interface */ 112 int unit; /* Interface unit number */ 113 node_p node; /* Our netgraph node */ 114 hook_p hooks[NUM_FAMILIES]; /* Hook for each address family */ 115 }; 116 typedef struct ng_iface_private *priv_p; 117 118 /* Interface methods */ 119 static void ng_iface_start(struct ifnet *ifp); 120 static int ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, 121 struct ucred *cr); 122 static int ng_iface_output(struct ifnet *ifp, struct mbuf *m0, 123 struct sockaddr *dst, struct rtentry *rt0); 124 static void ng_iface_bpftap(struct ifnet *ifp, 125 struct mbuf *m, sa_family_t family); 126 static int ng_iface_send(struct ifnet *ifp, struct mbuf *m, 127 sa_family_t sa); 128 #ifdef DEBUG 129 static void ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data); 130 #endif 131 132 /* Netgraph methods */ 133 static int ng_iface_mod_event(module_t, int, void *); 134 static ng_constructor_t ng_iface_constructor; 135 static ng_rcvmsg_t ng_iface_rcvmsg; 136 static ng_shutdown_t ng_iface_shutdown; 137 static ng_newhook_t ng_iface_newhook; 138 static ng_rcvdata_t ng_iface_rcvdata; 139 static ng_disconnect_t ng_iface_disconnect; 140 141 /* Helper stuff */ 142 static iffam_p get_iffam_from_af(sa_family_t family); 143 static iffam_p get_iffam_from_hook(priv_p priv, hook_p hook); 144 static iffam_p get_iffam_from_name(const char *name); 145 static hook_p *get_hook_from_iffam(priv_p priv, iffam_p iffam); 146 147 /* Parse type for struct ng_cisco_ipaddr */ 148 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[] 149 = NG_CISCO_IPADDR_TYPE_INFO; 150 static const struct ng_parse_type ng_cisco_ipaddr_type = { 151 &ng_parse_struct_type, 152 &ng_cisco_ipaddr_type_fields 153 }; 154 155 /* List of commands and how to convert arguments to/from ASCII */ 156 static const struct ng_cmdlist ng_iface_cmds[] = { 157 { 158 NGM_IFACE_COOKIE, 159 NGM_IFACE_GET_IFNAME, 160 "getifname", 161 NULL, 162 &ng_parse_string_type 163 }, 164 { 165 NGM_IFACE_COOKIE, 166 NGM_IFACE_POINT2POINT, 167 "point2point", 168 NULL, 169 NULL 170 }, 171 { 172 NGM_IFACE_COOKIE, 173 NGM_IFACE_BROADCAST, 174 "broadcast", 175 NULL, 176 NULL 177 }, 178 { 179 NGM_CISCO_COOKIE, 180 NGM_CISCO_GET_IPADDR, 181 "getipaddr", 182 NULL, 183 &ng_cisco_ipaddr_type 184 }, 185 { 186 NGM_IFACE_COOKIE, 187 NGM_IFACE_GET_IFINDEX, 188 "getifindex", 189 NULL, 190 &ng_parse_uint32_type 191 }, 192 { 0 } 193 }; 194 195 /* Node type descriptor */ 196 static struct ng_type typestruct = { 197 .version = NG_ABI_VERSION, 198 .name = NG_IFACE_NODE_TYPE, 199 .mod_event = ng_iface_mod_event, 200 .constructor = ng_iface_constructor, 201 .rcvmsg = ng_iface_rcvmsg, 202 .shutdown = ng_iface_shutdown, 203 .newhook = ng_iface_newhook, 204 .rcvdata = ng_iface_rcvdata, 205 .disconnect = ng_iface_disconnect, 206 .cmdlist = ng_iface_cmds, 207 }; 208 NETGRAPH_INIT(iface, &typestruct); 209 210 /* We keep a bitmap indicating which unit numbers are free. 211 One means the unit number is free, zero means it's taken. */ 212 static int *ng_iface_units = NULL; 213 static int ng_iface_units_len = 0; 214 215 #define UNITS_BITSPERWORD (sizeof(*ng_iface_units) * NBBY) 216 217 218 /************************************************************************ 219 HELPER STUFF 220 ************************************************************************/ 221 222 /* 223 * Get the family descriptor from the family ID 224 */ 225 static __inline iffam_p 226 get_iffam_from_af(sa_family_t family) 227 { 228 iffam_p iffam; 229 int k; 230 231 for (k = 0; k < NUM_FAMILIES; k++) { 232 iffam = &gFamilies[k]; 233 if (iffam->family == family) 234 return (iffam); 235 } 236 return (NULL); 237 } 238 239 /* 240 * Get the family descriptor from the hook 241 */ 242 static __inline iffam_p 243 get_iffam_from_hook(priv_p priv, hook_p hook) 244 { 245 int k; 246 247 for (k = 0; k < NUM_FAMILIES; k++) 248 if (priv->hooks[k] == hook) 249 return (&gFamilies[k]); 250 return (NULL); 251 } 252 253 /* 254 * Get the hook from the iffam descriptor 255 */ 256 257 static __inline hook_p * 258 get_hook_from_iffam(priv_p priv, iffam_p iffam) 259 { 260 return (&priv->hooks[iffam - gFamilies]); 261 } 262 263 /* 264 * Get the iffam descriptor from the name 265 */ 266 static __inline iffam_p 267 get_iffam_from_name(const char *name) 268 { 269 iffam_p iffam; 270 int k; 271 272 for (k = 0; k < NUM_FAMILIES; k++) { 273 iffam = &gFamilies[k]; 274 if (!strcmp(iffam->hookname, name)) 275 return (iffam); 276 } 277 return (NULL); 278 } 279 280 /* 281 * Find the first free unit number for a new interface. 282 * Increase the size of the unit bitmap as necessary. 283 */ 284 static __inline__ int 285 ng_iface_get_unit(int *unit) 286 { 287 int index, bit; 288 289 for (index = 0; index < ng_iface_units_len 290 && ng_iface_units[index] == 0; index++); 291 if (index == ng_iface_units_len) { /* extend array */ 292 int i, *newarray, newlen; 293 294 newlen = (2 * ng_iface_units_len) + 4; 295 newarray = kmalloc(newlen * sizeof(*ng_iface_units), 296 M_NETGRAPH_IFACE, M_NOWAIT); 297 if (newarray == NULL) 298 return (ENOMEM); 299 bcopy(ng_iface_units, newarray, 300 ng_iface_units_len * sizeof(*ng_iface_units)); 301 for (i = ng_iface_units_len; i < newlen; i++) 302 newarray[i] = ~0; 303 if (ng_iface_units != NULL) 304 kfree(ng_iface_units, M_NETGRAPH_IFACE); 305 ng_iface_units = newarray; 306 ng_iface_units_len = newlen; 307 } 308 bit = ffs(ng_iface_units[index]) - 1; 309 KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1, 310 ("%s: word=%d bit=%d", __func__, ng_iface_units[index], bit)); 311 ng_iface_units[index] &= ~(1 << bit); 312 *unit = (index * UNITS_BITSPERWORD) + bit; 313 return (0); 314 } 315 316 /* 317 * Free a no longer needed unit number. 318 */ 319 static __inline__ void 320 ng_iface_free_unit(int unit) 321 { 322 int index, bit; 323 324 index = unit / UNITS_BITSPERWORD; 325 bit = unit % UNITS_BITSPERWORD; 326 KASSERT(index < ng_iface_units_len, 327 ("%s: unit=%d len=%d", __func__, unit, ng_iface_units_len)); 328 KASSERT((ng_iface_units[index] & (1 << bit)) == 0, 329 ("%s: unit=%d is free", __func__, unit)); 330 ng_iface_units[index] |= (1 << bit); 331 /* 332 * XXX We could think about reducing the size of ng_iface_units[] 333 * XXX here if the last portion is all ones 334 */ 335 } 336 337 /************************************************************************ 338 INTERFACE STUFF 339 ************************************************************************/ 340 341 /* 342 * Process an ioctl for the virtual interface 343 */ 344 static int 345 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data, 346 struct ucred *cr) 347 { 348 struct ifreq *const ifr = (struct ifreq *) data; 349 int error = 0; 350 351 #ifdef DEBUG 352 ng_iface_print_ioctl(ifp, command, data); 353 #endif 354 crit_enter(); 355 switch (command) { 356 357 /* These two are mostly handled at a higher layer */ 358 case SIOCSIFADDR: 359 ifp->if_flags |= IFF_UP; 360 ifp->if_flags |= IFF_RUNNING; 361 ifp->if_flags &= ~(IFF_OACTIVE); 362 break; 363 case SIOCGIFADDR: 364 break; 365 366 /* Set flags */ 367 case SIOCSIFFLAGS: 368 /* 369 * If the interface is marked up and stopped, then start it. 370 * If it is marked down and running, then stop it. 371 */ 372 if (ifr->ifr_flags & IFF_UP) { 373 if (!(ifp->if_flags & IFF_RUNNING)) { 374 ifp->if_flags &= ~(IFF_OACTIVE); 375 ifp->if_flags |= IFF_RUNNING; 376 } 377 } else { 378 if (ifp->if_flags & IFF_RUNNING) 379 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 380 } 381 break; 382 383 /* Set the interface MTU */ 384 case SIOCSIFMTU: 385 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX 386 || ifr->ifr_mtu < NG_IFACE_MTU_MIN) 387 error = EINVAL; 388 else 389 ifp->if_mtu = ifr->ifr_mtu; 390 break; 391 392 /* Stuff that's not supported */ 393 case SIOCADDMULTI: 394 case SIOCDELMULTI: 395 error = 0; 396 break; 397 case SIOCSIFPHYS: 398 error = EOPNOTSUPP; 399 break; 400 401 default: 402 error = EINVAL; 403 break; 404 } 405 crit_exit(); 406 return (error); 407 } 408 409 /* 410 * This routine is called to deliver a packet out the interface. 411 * We simply look at the address family and relay the packet to 412 * the corresponding hook, if it exists and is connected. 413 */ 414 415 static int 416 ng_iface_output(struct ifnet *ifp, struct mbuf *m, 417 struct sockaddr *dst, struct rtentry *rt0) 418 { 419 uint32_t af; 420 int error; 421 422 /* Check interface flags */ 423 if (!((ifp->if_flags & IFF_UP) && (ifp->if_flags & IFF_RUNNING))) { 424 m_freem(m); 425 return (ENETDOWN); 426 } 427 428 /* BPF writes need to be handled specially. */ 429 if (dst->sa_family == AF_UNSPEC) { 430 bcopy(dst->sa_data, &af, sizeof(af)); 431 dst->sa_family = af; 432 } 433 434 /* Berkeley packet filter */ 435 ng_iface_bpftap(ifp, m, dst->sa_family); 436 437 if (ifq_is_enabled(&ifp->if_snd)) { 438 M_PREPEND(m, sizeof(sa_family_t), MB_DONTWAIT); 439 if (m == NULL) { 440 IFQ_LOCK(&ifp->if_snd); 441 IF_DROP(&ifp->if_snd); 442 IFQ_UNLOCK(&ifp->if_snd); 443 ifp->if_oerrors++; 444 return (ENOBUFS); 445 } 446 *(sa_family_t *)m->m_data = dst->sa_family; 447 error = ifq_handoff(ifp, m, 0); 448 } else 449 error = ng_iface_send(ifp, m, dst->sa_family); 450 451 return (error); 452 } 453 454 /* 455 * Start method is used only when ALTQ is enabled. 456 */ 457 static void 458 ng_iface_start(struct ifnet *ifp) 459 { 460 struct mbuf *m = NULL; 461 sa_family_t sa; 462 463 KASSERT(ifq_is_enabled(&ifp->if_snd), ("%s without ALTQ", __func__)); 464 465 for(;;) { 466 m = ifq_dequeue(&ifp->if_snd, m); 467 if (m == NULL) 468 break; 469 sa = *mtod(m, sa_family_t *); 470 m_adj(m, sizeof(sa_family_t)); 471 ng_iface_send(ifp, m, sa); 472 } 473 } 474 475 /* 476 * Flash a packet by the BPF (requires prepending 4 byte AF header) 477 * Note the phoney mbuf; this is OK because BPF treats it read-only. 478 */ 479 static void 480 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family) 481 { 482 int32_t family4 = (int32_t)family; 483 484 KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__)); 485 486 if (ifp->if_bpf) { 487 bpf_gettoken(); 488 if (ifp->if_bpf) 489 bpf_ptap(ifp->if_bpf, m, &family4, sizeof(family4)); 490 bpf_reltoken(); 491 } 492 } 493 494 /* 495 * This routine does actual delivery of the packet into the 496 * netgraph(4). It is called from ng_iface_start() and 497 * ng_iface_output(). 498 */ 499 static int 500 ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa) 501 { 502 const priv_p priv = (priv_p) ifp->if_softc; 503 const iffam_p iffam = get_iffam_from_af(sa); 504 int error; 505 int len; 506 507 /* Check address family to determine hook (if known) */ 508 if (iffam == NULL) { 509 m_freem(m); 510 log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa); 511 return (EAFNOSUPPORT); 512 } 513 514 /* Copy length before the mbuf gets invalidated. */ 515 len = m->m_pkthdr.len; 516 517 /* Send packet. If hook is not connected, 518 mbuf will get freed. */ 519 NG_SEND_DATA_ONLY(error, *get_hook_from_iffam(priv, iffam), m); 520 521 /* Update stats. */ 522 if (error == 0) { 523 ifp->if_obytes += len; 524 ifp->if_opackets++; 525 } 526 527 return (error); 528 } 529 530 #ifdef DEBUG 531 /* 532 * Display an ioctl to the virtual interface 533 */ 534 535 static void 536 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data) 537 { 538 char *str; 539 540 switch (command & IOC_DIRMASK) { 541 case IOC_VOID: 542 str = "IO"; 543 break; 544 case IOC_OUT: 545 str = "IOR"; 546 break; 547 case IOC_IN: 548 str = "IOW"; 549 break; 550 case IOC_INOUT: 551 str = "IORW"; 552 break; 553 default: 554 str = "IO??"; 555 } 556 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n", 557 ifp->if_xname, 558 str, 559 IOCGROUP(command), 560 command & 0xff, 561 IOCPARM_LEN(command)); 562 } 563 #endif /* DEBUG */ 564 565 /************************************************************************ 566 NETGRAPH NODE STUFF 567 ************************************************************************/ 568 569 /* 570 * Constructor for a node 571 */ 572 static int 573 ng_iface_constructor(node_p node) 574 { 575 struct ifnet *ifp; 576 priv_p priv; 577 int error; 578 579 /* Allocate node and interface private structures */ 580 priv = kmalloc(sizeof(*priv), M_NETGRAPH_IFACE, 581 M_WAITOK | M_NULLOK | M_ZERO); 582 if (priv == NULL) 583 return (ENOMEM); 584 ifp = kmalloc(sizeof(*ifp), M_NETGRAPH_IFACE, M_WAITOK | M_NULLOK | M_ZERO); 585 if (ifp == NULL) { 586 kfree(priv, M_NETGRAPH_IFACE); 587 return (ENOMEM); 588 } 589 590 /* Link them together */ 591 ifp->if_softc = priv; 592 priv->ifp = ifp; 593 594 /* Get an interface unit number */ 595 if ((error = ng_iface_get_unit(&priv->unit)) != 0) { 596 kfree(ifp, M_NETGRAPH_IFACE); 597 kfree(priv, M_NETGRAPH_IFACE); 598 return (error); 599 } 600 601 602 /* Link together node and private info */ 603 NG_NODE_SET_PRIVATE(node, priv); 604 priv->node = node; 605 606 /* Initialize interface structure */ 607 if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit); 608 ifp->if_output = ng_iface_output; 609 ifp->if_start = ng_iface_start; 610 ifp->if_ioctl = ng_iface_ioctl; 611 ifp->if_watchdog = NULL; 612 ifp->if_mtu = NG_IFACE_MTU_DEFAULT; 613 ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST); 614 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */ 615 ifp->if_addrlen = 0; /* XXX */ 616 ifp->if_hdrlen = 0; /* XXX */ 617 ifp->if_baudrate = 64000; /* XXX */ 618 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN); 619 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; 620 ifq_set_ready(&ifp->if_snd); 621 622 /* Give this node the same name as the interface (if possible) */ 623 if (ng_name_node(node, ifp->if_xname) != 0) 624 log(LOG_WARNING, "%s: can't acquire netgraph name\n", 625 ifp->if_xname); 626 627 /* Attach the interface */ 628 if_attach(ifp, NULL); 629 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t)); 630 631 /* Done */ 632 return (0); 633 } 634 635 /* 636 * Give our ok for a hook to be added 637 */ 638 static int 639 ng_iface_newhook(node_p node, hook_p hook, const char *name) 640 { 641 const iffam_p iffam = get_iffam_from_name(name); 642 hook_p *hookptr; 643 644 if (iffam == NULL) 645 return (EPFNOSUPPORT); 646 hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam); 647 if (*hookptr != NULL) 648 return (EISCONN); 649 *hookptr = hook; 650 NG_HOOK_HI_STACK(hook); 651 return (0); 652 } 653 654 /* 655 * Receive a control message 656 */ 657 static int 658 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook) 659 { 660 const priv_p priv = NG_NODE_PRIVATE(node); 661 struct ifnet *const ifp = priv->ifp; 662 struct ng_mesg *resp = NULL; 663 int error = 0; 664 struct ng_mesg *msg; 665 666 NGI_GET_MSG(item, msg); 667 switch (msg->header.typecookie) { 668 case NGM_IFACE_COOKIE: 669 switch (msg->header.cmd) { 670 case NGM_IFACE_GET_IFNAME: 671 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_WAITOK | M_NULLOK); 672 if (resp == NULL) { 673 error = ENOMEM; 674 break; 675 } 676 strlcpy(resp->data, ifp->if_xname, IFNAMSIZ); 677 break; 678 679 case NGM_IFACE_POINT2POINT: 680 case NGM_IFACE_BROADCAST: 681 { 682 683 /* Deny request if interface is UP */ 684 if ((ifp->if_flags & IFF_UP) != 0) 685 return (EBUSY); 686 687 /* Change flags */ 688 switch (msg->header.cmd) { 689 case NGM_IFACE_POINT2POINT: 690 ifp->if_flags |= IFF_POINTOPOINT; 691 ifp->if_flags &= ~IFF_BROADCAST; 692 break; 693 case NGM_IFACE_BROADCAST: 694 ifp->if_flags &= ~IFF_POINTOPOINT; 695 ifp->if_flags |= IFF_BROADCAST; 696 break; 697 } 698 break; 699 } 700 701 case NGM_IFACE_GET_IFINDEX: 702 NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_WAITOK | M_NULLOK); 703 if (resp == NULL) { 704 error = ENOMEM; 705 break; 706 } 707 *((uint32_t *)resp->data) = priv->ifp->if_index; 708 break; 709 710 default: 711 error = EINVAL; 712 break; 713 } 714 break; 715 case NGM_CISCO_COOKIE: 716 switch (msg->header.cmd) { 717 case NGM_CISCO_GET_IPADDR: /* we understand this too */ 718 { 719 struct ifaddr_container *ifac; 720 721 /* Return the first configured IP address */ 722 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { 723 struct ifaddr *ifa = ifac->ifa; 724 struct ng_cisco_ipaddr *ips; 725 726 if (ifa->ifa_addr->sa_family != AF_INET) 727 continue; 728 NG_MKRESPONSE(resp, msg, sizeof(ips), M_WAITOK | M_NULLOK); 729 if (resp == NULL) { 730 error = ENOMEM; 731 break; 732 } 733 ips = (struct ng_cisco_ipaddr *)resp->data; 734 ips->ipaddr = ((struct sockaddr_in *) 735 ifa->ifa_addr)->sin_addr; 736 ips->netmask = ((struct sockaddr_in *) 737 ifa->ifa_netmask)->sin_addr; 738 break; 739 } 740 741 /* No IP addresses on this interface? */ 742 if (ifac == NULL) 743 error = EADDRNOTAVAIL; 744 break; 745 } 746 default: 747 error = EINVAL; 748 break; 749 } 750 break; 751 case NGM_FLOW_COOKIE: 752 switch (msg->header.cmd) { 753 case NGM_LINK_IS_UP: 754 ifp->if_flags |= IFF_RUNNING; 755 break; 756 case NGM_LINK_IS_DOWN: 757 ifp->if_flags &= ~IFF_RUNNING; 758 break; 759 default: 760 break; 761 } 762 break; 763 default: 764 error = EINVAL; 765 break; 766 } 767 NG_RESPOND_MSG(error, node, item, resp); 768 NG_FREE_MSG(msg); 769 return (error); 770 } 771 772 /* 773 * Recive data from a hook. Pass the packet to the correct input routine. 774 */ 775 static int 776 ng_iface_rcvdata(hook_p hook, item_p item) 777 { 778 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 779 const iffam_p iffam = get_iffam_from_hook(priv, hook); 780 struct ifnet *const ifp = priv->ifp; 781 struct mbuf *m; 782 int isr; 783 784 NGI_GET_M(item, m); 785 NG_FREE_ITEM(item); 786 /* Sanity checks */ 787 KASSERT(iffam != NULL, ("%s: iffam", __func__)); 788 M_ASSERTPKTHDR(m); 789 if ((ifp->if_flags & IFF_UP) == 0) { 790 NG_FREE_M(m); 791 return (ENETDOWN); 792 } 793 794 /* Update interface stats */ 795 ifp->if_ipackets++; 796 ifp->if_ibytes += m->m_pkthdr.len; 797 798 /* Note receiving interface */ 799 m->m_pkthdr.rcvif = ifp; 800 801 /* Berkeley packet filter */ 802 ng_iface_bpftap(ifp, m, iffam->family); 803 804 /* Send packet */ 805 switch (iffam->family) { 806 #ifdef INET 807 case AF_INET: 808 isr = NETISR_IP; 809 break; 810 #endif 811 #ifdef INET6 812 case AF_INET6: 813 isr = NETISR_IPV6; 814 break; 815 #endif 816 #ifdef IPX 817 case AF_IPX: 818 isr = NETISR_IPX; 819 break; 820 #endif 821 default: 822 m_freem(m); 823 return (EAFNOSUPPORT); 824 } 825 #if 0 826 /* First chunk of an mbuf contains good junk */ 827 if (harvest.point_to_point) 828 random_harvest(m, 16, 3, 0, RANDOM_NET); 829 #endif 830 netisr_queue(isr, m); 831 return (0); 832 } 833 834 /* 835 * Shutdown and remove the node and its associated interface. 836 */ 837 static int 838 ng_iface_shutdown(node_p node) 839 { 840 const priv_p priv = NG_NODE_PRIVATE(node); 841 842 bpfdetach(priv->ifp); 843 if_detach(priv->ifp); 844 kfree(priv->ifp, M_NETGRAPH_IFACE); 845 priv->ifp = NULL; 846 ng_iface_free_unit(priv->unit); 847 kfree(priv, M_NETGRAPH_IFACE); 848 NG_NODE_SET_PRIVATE(node, NULL); 849 NG_NODE_UNREF(node); 850 return (0); 851 } 852 853 /* 854 * Hook disconnection. Note that we do *not* shutdown when all 855 * hooks have been disconnected. 856 */ 857 static int 858 ng_iface_disconnect(hook_p hook) 859 { 860 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 861 const iffam_p iffam = get_iffam_from_hook(priv, hook); 862 863 if (iffam == NULL) 864 panic(__func__); 865 *get_hook_from_iffam(priv, iffam) = NULL; 866 return (0); 867 } 868 869 /* 870 * Handle loading and unloading for this node type. 871 */ 872 static int 873 ng_iface_mod_event(module_t mod, int event, void *data) 874 { 875 int error = 0; 876 877 switch (event) { 878 case MOD_LOAD: 879 break; 880 case MOD_UNLOAD: 881 break; 882 default: 883 error = EOPNOTSUPP; 884 break; 885 } 886 return (error); 887 } 888