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