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 { 0 } 190 }; 191 192 /* Node type descriptor */ 193 static struct ng_type typestruct = { 194 NG_ABI_VERSION, 195 NG_IFACE_NODE_TYPE, 196 NULL, 197 ng_iface_constructor, 198 ng_iface_rcvmsg, 199 ng_iface_shutdown, 200 ng_iface_newhook, 201 NULL, 202 NULL, 203 ng_iface_rcvdata, 204 ng_iface_disconnect, 205 ng_iface_cmds 206 }; 207 NETGRAPH_INIT(iface, &typestruct); 208 209 /* We keep a bitmap indicating which unit numbers are free. 210 One means the unit number is free, zero means it's taken. */ 211 static int *ng_iface_units = NULL; 212 static int ng_iface_units_len = 0; 213 static int ng_units_in_use = 0; 214 215 #define UNITS_BITSPERWORD (sizeof(*ng_iface_units) * NBBY) 216 217 /************************************************************************ 218 HELPER STUFF 219 ************************************************************************/ 220 221 /* 222 * Get the family descriptor from the family ID 223 */ 224 static __inline__ iffam_p 225 get_iffam_from_af(sa_family_t family) 226 { 227 iffam_p iffam; 228 int k; 229 230 for (k = 0; k < NUM_FAMILIES; k++) { 231 iffam = &gFamilies[k]; 232 if (iffam->family == family) 233 return (iffam); 234 } 235 return (NULL); 236 } 237 238 /* 239 * Get the family descriptor from the hook 240 */ 241 static __inline__ iffam_p 242 get_iffam_from_hook(priv_p priv, hook_p hook) 243 { 244 int k; 245 246 for (k = 0; k < NUM_FAMILIES; k++) 247 if (priv->hooks[k] == hook) 248 return (&gFamilies[k]); 249 return (NULL); 250 } 251 252 /* 253 * Get the hook from the iffam descriptor 254 */ 255 256 static __inline__ hook_p * 257 get_hook_from_iffam(priv_p priv, iffam_p iffam) 258 { 259 return (&priv->hooks[iffam - gFamilies]); 260 } 261 262 /* 263 * Get the iffam descriptor from the name 264 */ 265 static __inline__ iffam_p 266 get_iffam_from_name(const char *name) 267 { 268 iffam_p iffam; 269 int k; 270 271 for (k = 0; k < NUM_FAMILIES; k++) { 272 iffam = &gFamilies[k]; 273 if (!strcmp(iffam->hookname, name)) 274 return (iffam); 275 } 276 return (NULL); 277 } 278 279 /* 280 * Find the first free unit number for a new interface. 281 * Increase the size of the unit bitmap as necessary. 282 */ 283 static __inline__ int 284 ng_iface_get_unit(int *unit) 285 { 286 int index, bit; 287 288 for (index = 0; index < ng_iface_units_len 289 && ng_iface_units[index] == 0; index++); 290 if (index == ng_iface_units_len) { /* extend array */ 291 int i, *newarray, newlen; 292 293 newlen = (2 * ng_iface_units_len) + 4; 294 MALLOC(newarray, int *, newlen * sizeof(*ng_iface_units), 295 M_NETGRAPH_IFACE, M_NOWAIT); 296 if (newarray == NULL) 297 return (ENOMEM); 298 bcopy(ng_iface_units, newarray, 299 ng_iface_units_len * sizeof(*ng_iface_units)); 300 for (i = ng_iface_units_len; i < newlen; i++) 301 newarray[i] = ~0; 302 if (ng_iface_units != NULL) 303 FREE(ng_iface_units, M_NETGRAPH_IFACE); 304 ng_iface_units = newarray; 305 ng_iface_units_len = newlen; 306 } 307 bit = ffs(ng_iface_units[index]) - 1; 308 KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1, 309 ("%s: word=%d bit=%d", __func__, ng_iface_units[index], bit)); 310 ng_iface_units[index] &= ~(1 << bit); 311 *unit = (index * UNITS_BITSPERWORD) + bit; 312 ng_units_in_use++; 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 * XXX At least free it if no more units. 335 * Needed if we are to eventually be able to unload. 336 */ 337 ng_units_in_use--; 338 if (ng_units_in_use == 0) { /* XXX make SMP safe */ 339 FREE(ng_iface_units, M_NETGRAPH_IFACE); 340 ng_iface_units_len = 0; 341 ng_iface_units = NULL; 342 } 343 } 344 345 /************************************************************************ 346 INTERFACE STUFF 347 ************************************************************************/ 348 349 /* 350 * Process an ioctl for the virtual interface 351 */ 352 static int 353 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 354 { 355 struct ifreq *const ifr = (struct ifreq *) data; 356 int s, error = 0; 357 358 #ifdef DEBUG 359 ng_iface_print_ioctl(ifp, command, data); 360 #endif 361 s = splimp(); 362 switch (command) { 363 364 /* These two are mostly handled at a higher layer */ 365 case SIOCSIFADDR: 366 ifp->if_flags |= (IFF_UP | IFF_RUNNING); 367 ifp->if_flags &= ~(IFF_OACTIVE); 368 break; 369 case SIOCGIFADDR: 370 break; 371 372 /* Set flags */ 373 case SIOCSIFFLAGS: 374 /* 375 * If the interface is marked up and stopped, then start it. 376 * If it is marked down and running, then stop it. 377 */ 378 if (ifr->ifr_flags & IFF_UP) { 379 if (!(ifp->if_flags & IFF_RUNNING)) { 380 ifp->if_flags &= ~(IFF_OACTIVE); 381 ifp->if_flags |= IFF_RUNNING; 382 } 383 } else { 384 if (ifp->if_flags & IFF_RUNNING) 385 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 386 } 387 break; 388 389 /* Set the interface MTU */ 390 case SIOCSIFMTU: 391 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX 392 || ifr->ifr_mtu < NG_IFACE_MTU_MIN) 393 error = EINVAL; 394 else 395 ifp->if_mtu = ifr->ifr_mtu; 396 break; 397 398 /* Stuff that's not supported */ 399 case SIOCADDMULTI: 400 case SIOCDELMULTI: 401 error = 0; 402 break; 403 case SIOCSIFPHYS: 404 error = EOPNOTSUPP; 405 break; 406 407 default: 408 error = EINVAL; 409 break; 410 } 411 (void) splx(s); 412 return (error); 413 } 414 415 /* 416 * This routine is called to deliver a packet out the interface. 417 * We simply look at the address family and relay the packet to 418 * the corresponding hook, if it exists and is connected. 419 */ 420 421 static int 422 ng_iface_output(struct ifnet *ifp, struct mbuf *m, 423 struct sockaddr *dst, struct rtentry *rt0) 424 { 425 const priv_p priv = (priv_p) ifp->if_softc; 426 const iffam_p iffam = get_iffam_from_af(dst->sa_family); 427 meta_p meta = NULL; 428 int len, error = 0; 429 430 /* Check interface flags */ 431 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) { 432 m_freem(m); 433 return (ENETDOWN); 434 } 435 436 /* BPF writes need to be handled specially */ 437 if (dst->sa_family == AF_UNSPEC) { 438 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) 439 return (ENOBUFS); 440 dst->sa_family = (sa_family_t)*mtod(m, int32_t *); 441 m->m_data += 4; 442 m->m_len -= 4; 443 m->m_pkthdr.len -= 4; 444 } 445 446 /* Berkeley packet filter */ 447 ng_iface_bpftap(ifp, m, dst->sa_family); 448 449 /* Check address family to determine hook (if known) */ 450 if (iffam == NULL) { 451 m_freem(m); 452 log(LOG_WARNING, "%s: can't handle af%d\n", 453 ifp->if_xname, (int)dst->sa_family); 454 return (EAFNOSUPPORT); 455 } 456 457 /* Copy length before the mbuf gets invalidated */ 458 len = m->m_pkthdr.len; 459 460 /* Send packet; if hook is not connected, mbuf will get freed. */ 461 NG_SEND_DATA(error, *get_hook_from_iffam(priv, iffam), m, meta); 462 463 /* Update stats */ 464 if (error == 0) { 465 ifp->if_obytes += len; 466 ifp->if_opackets++; 467 } 468 return (error); 469 } 470 471 /* 472 * This routine should never be called 473 */ 474 475 static void 476 ng_iface_start(struct ifnet *ifp) 477 { 478 if_printf(ifp, "%s called?", __func__); 479 } 480 481 /* 482 * Flash a packet by the BPF (requires prepending 4 byte AF header) 483 * Note the phoney mbuf; this is OK because BPF treats it read-only. 484 */ 485 static void 486 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family) 487 { 488 KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__)); 489 if (ifp->if_bpf != NULL) { 490 int32_t family4 = (int32_t)family; 491 bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m); 492 } 493 } 494 495 #ifdef DEBUG 496 /* 497 * Display an ioctl to the virtual interface 498 */ 499 500 static void 501 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data) 502 { 503 char *str; 504 505 switch (command & IOC_DIRMASK) { 506 case IOC_VOID: 507 str = "IO"; 508 break; 509 case IOC_OUT: 510 str = "IOR"; 511 break; 512 case IOC_IN: 513 str = "IOW"; 514 break; 515 case IOC_INOUT: 516 str = "IORW"; 517 break; 518 default: 519 str = "IO??"; 520 } 521 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n", 522 ifp->if_xname, 523 str, 524 IOCGROUP(command), 525 command & 0xff, 526 IOCPARM_LEN(command)); 527 } 528 #endif /* DEBUG */ 529 530 /************************************************************************ 531 NETGRAPH NODE STUFF 532 ************************************************************************/ 533 534 /* 535 * Constructor for a node 536 */ 537 static int 538 ng_iface_constructor(node_p node) 539 { 540 char ifname[NG_IFACE_IFACE_NAME_MAX + 1]; 541 struct ifnet *ifp; 542 priv_p priv; 543 int error = 0; 544 545 /* Allocate node and interface private structures */ 546 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_IFACE, M_NOWAIT|M_ZERO); 547 if (priv == NULL) 548 return (ENOMEM); 549 MALLOC(ifp, struct ifnet *, sizeof(*ifp), M_NETGRAPH_IFACE, M_NOWAIT|M_ZERO); 550 if (ifp == NULL) { 551 FREE(priv, M_NETGRAPH_IFACE); 552 return (ENOMEM); 553 } 554 555 /* Link them together */ 556 ifp->if_softc = priv; 557 priv->ifp = ifp; 558 559 /* Get an interface unit number */ 560 if ((error = ng_iface_get_unit(&priv->unit)) != 0) { 561 FREE(ifp, M_NETGRAPH_IFACE); 562 FREE(priv, M_NETGRAPH_IFACE); 563 return (error); 564 } 565 566 /* Link together node and private info */ 567 NG_NODE_SET_PRIVATE(node, priv); 568 priv->node = node; 569 570 /* Initialize interface structure */ 571 if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit); 572 ifp->if_output = ng_iface_output; 573 ifp->if_start = ng_iface_start; 574 ifp->if_ioctl = ng_iface_ioctl; 575 ifp->if_watchdog = NULL; 576 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; 577 ifp->if_mtu = NG_IFACE_MTU_DEFAULT; 578 ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST); 579 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */ 580 ifp->if_addrlen = 0; /* XXX */ 581 ifp->if_hdrlen = 0; /* XXX */ 582 ifp->if_baudrate = 64000; /* XXX */ 583 TAILQ_INIT(&ifp->if_addrhead); 584 585 /* Give this node the same name as the interface (if possible) */ 586 bzero(ifname, sizeof(ifname)); 587 strlcpy(ifname, ifp->if_xname, sizeof(ifname)); 588 if (ng_name_node(node, ifname) != 0) 589 log(LOG_WARNING, "%s: can't acquire netgraph name\n", ifname); 590 591 /* Attach the interface */ 592 if_attach(ifp); 593 bpfattach(ifp, DLT_NULL, sizeof(u_int)); 594 595 /* Done */ 596 return (0); 597 } 598 599 /* 600 * Give our ok for a hook to be added 601 */ 602 static int 603 ng_iface_newhook(node_p node, hook_p hook, const char *name) 604 { 605 const iffam_p iffam = get_iffam_from_name(name); 606 hook_p *hookptr; 607 608 if (iffam == NULL) 609 return (EPFNOSUPPORT); 610 hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam); 611 if (*hookptr != NULL) 612 return (EISCONN); 613 *hookptr = hook; 614 return (0); 615 } 616 617 /* 618 * Receive a control message 619 */ 620 static int 621 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook) 622 { 623 const priv_p priv = NG_NODE_PRIVATE(node); 624 struct ifnet *const ifp = priv->ifp; 625 struct ng_mesg *resp = NULL; 626 int error = 0; 627 struct ng_mesg *msg; 628 629 NGI_GET_MSG(item, msg); 630 switch (msg->header.typecookie) { 631 case NGM_IFACE_COOKIE: 632 switch (msg->header.cmd) { 633 case NGM_IFACE_GET_IFNAME: 634 { 635 struct ng_iface_ifname *arg; 636 637 NG_MKRESPONSE(resp, msg, sizeof(*arg), M_NOWAIT); 638 if (resp == NULL) { 639 error = ENOMEM; 640 break; 641 } 642 arg = (struct ng_iface_ifname *)resp->data; 643 strlcpy(arg->ngif_name, ifp->if_xname, 644 sizeof(arg->ngif_name)); 645 break; 646 } 647 648 case NGM_IFACE_POINT2POINT: 649 case NGM_IFACE_BROADCAST: 650 { 651 652 /* Deny request if interface is UP */ 653 if ((ifp->if_flags & IFF_UP) != 0) 654 return (EBUSY); 655 656 /* Change flags */ 657 switch (msg->header.cmd) { 658 case NGM_IFACE_POINT2POINT: 659 ifp->if_flags |= IFF_POINTOPOINT; 660 ifp->if_flags &= ~IFF_BROADCAST; 661 break; 662 case NGM_IFACE_BROADCAST: 663 ifp->if_flags &= ~IFF_POINTOPOINT; 664 ifp->if_flags |= IFF_BROADCAST; 665 break; 666 } 667 break; 668 } 669 670 default: 671 error = EINVAL; 672 break; 673 } 674 break; 675 case NGM_CISCO_COOKIE: 676 switch (msg->header.cmd) { 677 case NGM_CISCO_GET_IPADDR: /* we understand this too */ 678 { 679 struct ifaddr *ifa; 680 681 /* Return the first configured IP address */ 682 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 683 struct ng_cisco_ipaddr *ips; 684 685 if (ifa->ifa_addr->sa_family != AF_INET) 686 continue; 687 NG_MKRESPONSE(resp, msg, sizeof(ips), M_NOWAIT); 688 if (resp == NULL) { 689 error = ENOMEM; 690 break; 691 } 692 ips = (struct ng_cisco_ipaddr *)resp->data; 693 ips->ipaddr = ((struct sockaddr_in *) 694 ifa->ifa_addr)->sin_addr; 695 ips->netmask = ((struct sockaddr_in *) 696 ifa->ifa_netmask)->sin_addr; 697 break; 698 } 699 700 /* No IP addresses on this interface? */ 701 if (ifa == NULL) 702 error = EADDRNOTAVAIL; 703 break; 704 } 705 default: 706 error = EINVAL; 707 break; 708 } 709 break; 710 default: 711 error = EINVAL; 712 break; 713 } 714 NG_RESPOND_MSG(error, node, item, resp); 715 NG_FREE_MSG(msg); 716 return (error); 717 } 718 719 /* 720 * Recive data from a hook. Pass the packet to the correct input routine. 721 */ 722 static int 723 ng_iface_rcvdata(hook_p hook, item_p item) 724 { 725 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 726 const iffam_p iffam = get_iffam_from_hook(priv, hook); 727 struct ifnet *const ifp = priv->ifp; 728 struct mbuf *m; 729 int isr; 730 731 NGI_GET_M(item, m); 732 NG_FREE_ITEM(item); 733 /* Sanity checks */ 734 KASSERT(iffam != NULL, ("%s: iffam", __func__)); 735 M_ASSERTPKTHDR(m); 736 if (m == NULL) 737 return (EINVAL); 738 if ((ifp->if_flags & IFF_UP) == 0) { 739 NG_FREE_M(m); 740 return (ENETDOWN); 741 } 742 743 /* Update interface stats */ 744 ifp->if_ipackets++; 745 ifp->if_ibytes += m->m_pkthdr.len; 746 747 /* Note receiving interface */ 748 m->m_pkthdr.rcvif = ifp; 749 750 /* Berkeley packet filter */ 751 ng_iface_bpftap(ifp, m, iffam->family); 752 753 /* Send packet */ 754 switch (iffam->family) { 755 #ifdef INET 756 case AF_INET: 757 isr = NETISR_IP; 758 break; 759 #endif 760 #ifdef INET6 761 case AF_INET6: 762 isr = NETISR_IPV6; 763 break; 764 #endif 765 #ifdef IPX 766 case AF_IPX: 767 isr = NETISR_IPX; 768 break; 769 #endif 770 #ifdef NETATALK 771 case AF_APPLETALK: 772 isr = NETISR_ATALK2; 773 break; 774 #endif 775 default: 776 m_freem(m); 777 return (EAFNOSUPPORT); 778 } 779 /* First chunk of an mbuf contains good junk */ 780 if (harvest.point_to_point) 781 random_harvest(m, 16, 3, 0, RANDOM_NET); 782 netisr_dispatch(isr, m); 783 return (0); 784 } 785 786 /* 787 * Shutdown and remove the node and its associated interface. 788 */ 789 static int 790 ng_iface_shutdown(node_p node) 791 { 792 const priv_p priv = NG_NODE_PRIVATE(node); 793 794 bpfdetach(priv->ifp); 795 if_detach(priv->ifp); 796 FREE(priv->ifp, M_NETGRAPH_IFACE); 797 priv->ifp = NULL; 798 ng_iface_free_unit(priv->unit); 799 FREE(priv, M_NETGRAPH_IFACE); 800 NG_NODE_SET_PRIVATE(node, NULL); 801 NG_NODE_UNREF(node); 802 return (0); 803 } 804 805 /* 806 * Hook disconnection. Note that we do *not* shutdown when all 807 * hooks have been disconnected. 808 */ 809 static int 810 ng_iface_disconnect(hook_p hook) 811 { 812 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 813 const iffam_p iffam = get_iffam_from_hook(priv, hook); 814 815 if (iffam == NULL) 816 panic(__func__); 817 *get_hook_from_iffam(priv, iffam) = NULL; 818 return (0); 819 } 820 821