6 * Copyright (c) 1996-1999 Whistle Communications, Inc.
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.
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
38 * Author: Archie Cobbs <archie@freebsd.org>
40 * $FreeBSD: src/sys/netgraph/ng_iface.c,v 1.48 2008/01/31 08:51:48 mav Exp $
41 * $DragonFly: src/sys/netgraph7/ng_iface.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
42 * $Whistle: ng_iface.c,v 1.33 1999/11/01 09:24:51 julian Exp $
46 * This node is also a system networking interface. It has
47 * a hook for each protocol (IP, AppleTalk, IPX, etc). Packets
48 * are simply relayed between the interface and the hooks.
50 * Interfaces are named ng0, ng1, etc. New nodes take the
51 * first available interface name.
53 * This node also includes Berkeley packet filter support.
56 #include "opt_atalk.h"
58 #include "opt_inet6.h"
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/errno.h>
64 #include <sys/kernel.h>
65 #include <sys/malloc.h>
67 #include <sys/errno.h>
68 #include <sys/random.h>
69 #include <sys/sockio.h>
70 #include <sys/socket.h>
71 #include <sys/syslog.h>
72 #include <sys/libkern.h>
75 #include <net/if_types.h>
77 #include <net/netisr.h>
79 #include <netinet/in.h>
81 #include "ng_message.h"
87 #ifdef NG_SEPARATE_MALLOC
88 MALLOC_DEFINE(M_NETGRAPH_IFACE, "netgraph_iface", "netgraph iface node ");
90 #define M_NETGRAPH_IFACE M_NETGRAPH
93 /* This struct describes one address family */
95 sa_family_t family; /* Address family */
96 const char *hookname; /* Name for hook */
98 typedef const struct iffam *iffam_p;
100 /* List of address families supported by our interface */
101 const static struct iffam gFamilies[] = {
102 { AF_INET, NG_IFACE_HOOK_INET },
103 { AF_INET6, NG_IFACE_HOOK_INET6 },
104 { AF_APPLETALK, NG_IFACE_HOOK_ATALK },
105 { AF_IPX, NG_IFACE_HOOK_IPX },
106 { AF_ATM, NG_IFACE_HOOK_ATM },
107 { AF_NATM, NG_IFACE_HOOK_NATM },
109 #define NUM_FAMILIES (sizeof(gFamilies) / sizeof(*gFamilies))
111 /* Node private data */
112 struct ng_iface_private {
113 struct ifnet *ifp; /* Our interface */
114 int unit; /* Interface unit number */
115 node_p node; /* Our netgraph node */
116 hook_p hooks[NUM_FAMILIES]; /* Hook for each address family */
118 typedef struct ng_iface_private *priv_p;
120 /* Interface methods */
121 static void ng_iface_start(struct ifnet *ifp);
122 static int ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
123 static int ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
124 struct sockaddr *dst, struct rtentry *rt0);
125 static void ng_iface_bpftap(struct ifnet *ifp,
126 struct mbuf *m, sa_family_t family);
127 static int ng_iface_send(struct ifnet *ifp, struct mbuf *m,
130 static void ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
133 /* Netgraph methods */
134 static int ng_iface_mod_event(module_t, int, void *);
135 static ng_constructor_t ng_iface_constructor;
136 static ng_rcvmsg_t ng_iface_rcvmsg;
137 static ng_shutdown_t ng_iface_shutdown;
138 static ng_newhook_t ng_iface_newhook;
139 static ng_rcvdata_t ng_iface_rcvdata;
140 static ng_disconnect_t ng_iface_disconnect;
143 static iffam_p get_iffam_from_af(sa_family_t family);
144 static iffam_p get_iffam_from_hook(priv_p priv, hook_p hook);
145 static iffam_p get_iffam_from_name(const char *name);
146 static hook_p *get_hook_from_iffam(priv_p priv, iffam_p iffam);
148 /* Parse type for struct ng_cisco_ipaddr */
149 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
150 = NG_CISCO_IPADDR_TYPE_INFO;
151 static const struct ng_parse_type ng_cisco_ipaddr_type = {
152 &ng_parse_struct_type,
153 &ng_cisco_ipaddr_type_fields
156 /* List of commands and how to convert arguments to/from ASCII */
157 static const struct ng_cmdlist ng_iface_cmds[] = {
160 NGM_IFACE_GET_IFNAME,
163 &ng_parse_string_type
167 NGM_IFACE_POINT2POINT,
181 NGM_CISCO_GET_IPADDR,
184 &ng_cisco_ipaddr_type
188 NGM_IFACE_GET_IFINDEX,
191 &ng_parse_uint32_type
196 /* Node type descriptor */
197 static struct ng_type typestruct = {
198 .version = NG_ABI_VERSION,
199 .name = NG_IFACE_NODE_TYPE,
200 .mod_event = ng_iface_mod_event,
201 .constructor = ng_iface_constructor,
202 .rcvmsg = ng_iface_rcvmsg,
203 .shutdown = ng_iface_shutdown,
204 .newhook = ng_iface_newhook,
205 .rcvdata = ng_iface_rcvdata,
206 .disconnect = ng_iface_disconnect,
207 .cmdlist = ng_iface_cmds,
209 NETGRAPH_INIT(iface, &typestruct);
211 static struct unrhdr *ng_iface_unit;
213 /************************************************************************
215 ************************************************************************/
218 * Get the family descriptor from the family ID
220 static __inline iffam_p
221 get_iffam_from_af(sa_family_t family)
226 for (k = 0; k < NUM_FAMILIES; k++) {
227 iffam = &gFamilies[k];
228 if (iffam->family == family)
235 * Get the family descriptor from the hook
237 static __inline iffam_p
238 get_iffam_from_hook(priv_p priv, hook_p hook)
242 for (k = 0; k < NUM_FAMILIES; k++)
243 if (priv->hooks[k] == hook)
244 return (&gFamilies[k]);
249 * Get the hook from the iffam descriptor
252 static __inline hook_p *
253 get_hook_from_iffam(priv_p priv, iffam_p iffam)
255 return (&priv->hooks[iffam - gFamilies]);
259 * Get the iffam descriptor from the name
261 static __inline iffam_p
262 get_iffam_from_name(const char *name)
267 for (k = 0; k < NUM_FAMILIES; k++) {
268 iffam = &gFamilies[k];
269 if (!strcmp(iffam->hookname, name))
275 /************************************************************************
277 ************************************************************************/
280 * Process an ioctl for the virtual interface
283 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
285 struct ifreq *const ifr = (struct ifreq *) data;
289 ng_iface_print_ioctl(ifp, command, data);
294 /* These two are mostly handled at a higher layer */
296 ifp->if_flags |= IFF_UP;
297 ifp->if_drv_flags |= IFF_DRV_RUNNING;
298 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
306 * If the interface is marked up and stopped, then start it.
307 * If it is marked down and running, then stop it.
309 if (ifr->ifr_flags & IFF_UP) {
310 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
311 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
312 ifp->if_drv_flags |= IFF_DRV_RUNNING;
315 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
316 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
321 /* Set the interface MTU */
323 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
324 || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
327 ifp->if_mtu = ifr->ifr_mtu;
330 /* Stuff that's not supported */
348 * This routine is called to deliver a packet out the interface.
349 * We simply look at the address family and relay the packet to
350 * the corresponding hook, if it exists and is connected.
354 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
355 struct sockaddr *dst, struct rtentry *rt0)
360 /* Check interface flags */
361 if (!((ifp->if_flags & IFF_UP) &&
362 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
367 /* BPF writes need to be handled specially. */
368 if (dst->sa_family == AF_UNSPEC) {
369 bcopy(dst->sa_data, &af, sizeof(af));
373 /* Berkeley packet filter */
374 ng_iface_bpftap(ifp, m, dst->sa_family);
376 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
377 M_PREPEND(m, sizeof(sa_family_t), MB_DONTWAIT);
379 IFQ_LOCK(&ifp->if_snd);
380 IFQ_INC_DROPS(&ifp->if_snd);
381 IFQ_UNLOCK(&ifp->if_snd);
385 *(sa_family_t *)m->m_data = dst->sa_family;
386 IFQ_HANDOFF(ifp, m, error);
388 error = ng_iface_send(ifp, m, dst->sa_family);
394 * Start method is used only when ALTQ is enabled.
397 ng_iface_start(struct ifnet *ifp)
402 KASSERT(ALTQ_IS_ENABLED(&ifp->if_snd), ("%s without ALTQ", __func__));
405 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
408 sa = *mtod(m, sa_family_t *);
409 m_adj(m, sizeof(sa_family_t));
410 ng_iface_send(ifp, m, sa);
415 * Flash a packet by the BPF (requires prepending 4 byte AF header)
416 * Note the phoney mbuf; this is OK because BPF treats it read-only.
419 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
421 KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
422 if (bpf_peers_present(ifp->if_bpf)) {
423 int32_t family4 = (int32_t)family;
424 bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m);
429 * This routine does actual delivery of the packet into the
430 * netgraph(4). It is called from ng_iface_start() and
434 ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa)
436 const priv_p priv = (priv_p) ifp->if_softc;
437 const iffam_p iffam = get_iffam_from_af(sa);
441 /* Check address family to determine hook (if known) */
444 log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa);
445 return (EAFNOSUPPORT);
448 /* Copy length before the mbuf gets invalidated. */
449 len = m->m_pkthdr.len;
451 /* Send packet. If hook is not connected,
452 mbuf will get freed. */
453 NG_SEND_DATA_ONLY(error, *get_hook_from_iffam(priv, iffam), m);
457 ifp->if_obytes += len;
466 * Display an ioctl to the virtual interface
470 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
474 switch (command & IOC_DIRMASK) {
490 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
495 IOCPARM_LEN(command));
499 /************************************************************************
501 ************************************************************************/
504 * Constructor for a node
507 ng_iface_constructor(node_p node)
512 /* Allocate node and interface private structures */
513 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_IFACE, M_WAITOK | M_NULLOK | M_ZERO);
516 ifp = if_alloc(IFT_PROPVIRTUAL);
518 FREE(priv, M_NETGRAPH_IFACE);
522 /* Link them together */
523 ifp->if_softc = priv;
526 /* Get an interface unit number */
527 priv->unit = alloc_unr(ng_iface_unit);
529 /* Link together node and private info */
530 NG_NODE_SET_PRIVATE(node, priv);
533 /* Initialize interface structure */
534 if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
535 ifp->if_output = ng_iface_output;
536 ifp->if_start = ng_iface_start;
537 ifp->if_ioctl = ng_iface_ioctl;
538 ifp->if_watchdog = NULL;
539 ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
540 ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
541 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */
542 ifp->if_addrlen = 0; /* XXX */
543 ifp->if_hdrlen = 0; /* XXX */
544 ifp->if_baudrate = 64000; /* XXX */
545 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
546 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
547 IFQ_SET_READY(&ifp->if_snd);
549 /* Give this node the same name as the interface (if possible) */
550 if (ng_name_node(node, ifp->if_xname) != 0)
551 log(LOG_WARNING, "%s: can't acquire netgraph name\n",
554 /* Attach the interface */
556 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
563 * Give our ok for a hook to be added
566 ng_iface_newhook(node_p node, hook_p hook, const char *name)
568 const iffam_p iffam = get_iffam_from_name(name);
572 return (EPFNOSUPPORT);
573 hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam);
574 if (*hookptr != NULL)
577 NG_HOOK_HI_STACK(hook);
582 * Receive a control message
585 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook)
587 const priv_p priv = NG_NODE_PRIVATE(node);
588 struct ifnet *const ifp = priv->ifp;
589 struct ng_mesg *resp = NULL;
593 NGI_GET_MSG(item, msg);
594 switch (msg->header.typecookie) {
595 case NGM_IFACE_COOKIE:
596 switch (msg->header.cmd) {
597 case NGM_IFACE_GET_IFNAME:
598 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_WAITOK | M_NULLOK);
603 strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
606 case NGM_IFACE_POINT2POINT:
607 case NGM_IFACE_BROADCAST:
610 /* Deny request if interface is UP */
611 if ((ifp->if_flags & IFF_UP) != 0)
615 switch (msg->header.cmd) {
616 case NGM_IFACE_POINT2POINT:
617 ifp->if_flags |= IFF_POINTOPOINT;
618 ifp->if_flags &= ~IFF_BROADCAST;
620 case NGM_IFACE_BROADCAST:
621 ifp->if_flags &= ~IFF_POINTOPOINT;
622 ifp->if_flags |= IFF_BROADCAST;
628 case NGM_IFACE_GET_IFINDEX:
629 NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_WAITOK | M_NULLOK);
634 *((uint32_t *)resp->data) = priv->ifp->if_index;
642 case NGM_CISCO_COOKIE:
643 switch (msg->header.cmd) {
644 case NGM_CISCO_GET_IPADDR: /* we understand this too */
648 /* Return the first configured IP address */
649 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
650 struct ng_cisco_ipaddr *ips;
652 if (ifa->ifa_addr->sa_family != AF_INET)
654 NG_MKRESPONSE(resp, msg, sizeof(ips), M_WAITOK | M_NULLOK);
659 ips = (struct ng_cisco_ipaddr *)resp->data;
660 ips->ipaddr = ((struct sockaddr_in *)
661 ifa->ifa_addr)->sin_addr;
662 ips->netmask = ((struct sockaddr_in *)
663 ifa->ifa_netmask)->sin_addr;
667 /* No IP addresses on this interface? */
669 error = EADDRNOTAVAIL;
677 case NGM_FLOW_COOKIE:
678 switch (msg->header.cmd) {
680 ifp->if_drv_flags |= IFF_DRV_RUNNING;
682 case NGM_LINK_IS_DOWN:
683 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
693 NG_RESPOND_MSG(error, node, item, resp);
699 * Recive data from a hook. Pass the packet to the correct input routine.
702 ng_iface_rcvdata(hook_p hook, item_p item)
704 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
705 const iffam_p iffam = get_iffam_from_hook(priv, hook);
706 struct ifnet *const ifp = priv->ifp;
713 KASSERT(iffam != NULL, ("%s: iffam", __func__));
715 if ((ifp->if_flags & IFF_UP) == 0) {
720 /* Update interface stats */
722 ifp->if_ibytes += m->m_pkthdr.len;
724 /* Note receiving interface */
725 m->m_pkthdr.rcvif = ifp;
727 /* Berkeley packet filter */
728 ng_iface_bpftap(ifp, m, iffam->family);
731 switch (iffam->family) {
754 return (EAFNOSUPPORT);
756 /* First chunk of an mbuf contains good junk */
757 if (harvest.point_to_point)
758 random_harvest(m, 16, 3, 0, RANDOM_NET);
759 netisr_queue(isr, m);
764 * Shutdown and remove the node and its associated interface.
767 ng_iface_shutdown(node_p node)
769 const priv_p priv = NG_NODE_PRIVATE(node);
771 bpfdetach(priv->ifp);
772 if_detach(priv->ifp);
775 free_unr(ng_iface_unit, priv->unit);
776 FREE(priv, M_NETGRAPH_IFACE);
777 NG_NODE_SET_PRIVATE(node, NULL);
783 * Hook disconnection. Note that we do *not* shutdown when all
784 * hooks have been disconnected.
787 ng_iface_disconnect(hook_p hook)
789 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
790 const iffam_p iffam = get_iffam_from_hook(priv, hook);
794 *get_hook_from_iffam(priv, iffam) = NULL;
799 * Handle loading and unloading for this node type.
802 ng_iface_mod_event(module_t mod, int event, void *data)
808 ng_iface_unit = new_unrhdr(0, 0xffff, NULL);
811 delete_unrhdr(ng_iface_unit);