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