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