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 * $Whistle: ng_iface.c,v 1.33 1999/11/01 09:24:51 julian Exp $
45 * This node is also a system networking interface. It has
46 * a hook for each protocol (IP, AppleTalk, IPX, etc). Packets
47 * are simply relayed between the interface and the hooks.
49 * Interfaces are named ng0, ng1, etc. New nodes take the
50 * first available interface name.
52 * This node also includes Berkeley packet filter support.
56 #include "opt_inet6.h"
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/errno.h>
62 #include <sys/kernel.h>
63 #include <sys/malloc.h>
65 #include <sys/errno.h>
66 #include <sys/random.h>
67 #include <sys/sockio.h>
68 #include <sys/socket.h>
69 #include <sys/syslog.h>
70 #include <sys/libkern.h>
73 #include <net/if_types.h>
75 #include <net/netisr.h>
77 #include <netinet/in.h>
79 #include "ng_message.h"
85 #ifdef NG_SEPARATE_MALLOC
86 MALLOC_DEFINE(M_NETGRAPH_IFACE, "netgraph_iface", "netgraph iface node ");
88 #define M_NETGRAPH_IFACE M_NETGRAPH
91 /* This struct describes one address family */
93 sa_family_t family; /* Address family */
94 const char *hookname; /* Name for hook */
96 typedef const struct iffam *iffam_p;
98 /* List of address families supported by our interface */
99 const static struct iffam gFamilies[] = {
100 { AF_INET, NG_IFACE_HOOK_INET },
101 { AF_INET6, NG_IFACE_HOOK_INET6 },
102 { AF_IPX, NG_IFACE_HOOK_IPX },
103 { AF_ATM, NG_IFACE_HOOK_ATM },
104 { AF_NATM, NG_IFACE_HOOK_NATM },
106 #define NUM_FAMILIES (sizeof(gFamilies) / sizeof(*gFamilies))
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 */
115 typedef struct ng_iface_private *priv_p;
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 static int ng_iface_send(struct ifnet *ifp, struct mbuf *m,
127 static void ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
130 /* Netgraph methods */
131 static int ng_iface_mod_event(module_t, int, void *);
132 static ng_constructor_t ng_iface_constructor;
133 static ng_rcvmsg_t ng_iface_rcvmsg;
134 static ng_shutdown_t ng_iface_shutdown;
135 static ng_newhook_t ng_iface_newhook;
136 static ng_rcvdata_t ng_iface_rcvdata;
137 static ng_disconnect_t ng_iface_disconnect;
140 static iffam_p get_iffam_from_af(sa_family_t family);
141 static iffam_p get_iffam_from_hook(priv_p priv, hook_p hook);
142 static iffam_p get_iffam_from_name(const char *name);
143 static hook_p *get_hook_from_iffam(priv_p priv, iffam_p iffam);
145 /* Parse type for struct ng_cisco_ipaddr */
146 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
147 = NG_CISCO_IPADDR_TYPE_INFO;
148 static const struct ng_parse_type ng_cisco_ipaddr_type = {
149 &ng_parse_struct_type,
150 &ng_cisco_ipaddr_type_fields
153 /* List of commands and how to convert arguments to/from ASCII */
154 static const struct ng_cmdlist ng_iface_cmds[] = {
157 NGM_IFACE_GET_IFNAME,
160 &ng_parse_string_type
164 NGM_IFACE_POINT2POINT,
178 NGM_CISCO_GET_IPADDR,
181 &ng_cisco_ipaddr_type
185 NGM_IFACE_GET_IFINDEX,
188 &ng_parse_uint32_type
193 /* Node type descriptor */
194 static struct ng_type typestruct = {
195 .version = NG_ABI_VERSION,
196 .name = NG_IFACE_NODE_TYPE,
197 .mod_event = ng_iface_mod_event,
198 .constructor = ng_iface_constructor,
199 .rcvmsg = ng_iface_rcvmsg,
200 .shutdown = ng_iface_shutdown,
201 .newhook = ng_iface_newhook,
202 .rcvdata = ng_iface_rcvdata,
203 .disconnect = ng_iface_disconnect,
204 .cmdlist = ng_iface_cmds,
206 NETGRAPH_INIT(iface, &typestruct);
208 static struct unrhdr *ng_iface_unit;
210 /************************************************************************
212 ************************************************************************/
215 * Get the family descriptor from the family ID
217 static __inline iffam_p
218 get_iffam_from_af(sa_family_t family)
223 for (k = 0; k < NUM_FAMILIES; k++) {
224 iffam = &gFamilies[k];
225 if (iffam->family == family)
232 * Get the family descriptor from the hook
234 static __inline iffam_p
235 get_iffam_from_hook(priv_p priv, hook_p hook)
239 for (k = 0; k < NUM_FAMILIES; k++)
240 if (priv->hooks[k] == hook)
241 return (&gFamilies[k]);
246 * Get the hook from the iffam descriptor
249 static __inline hook_p *
250 get_hook_from_iffam(priv_p priv, iffam_p iffam)
252 return (&priv->hooks[iffam - gFamilies]);
256 * Get the iffam descriptor from the name
258 static __inline iffam_p
259 get_iffam_from_name(const char *name)
264 for (k = 0; k < NUM_FAMILIES; k++) {
265 iffam = &gFamilies[k];
266 if (!strcmp(iffam->hookname, name))
272 /************************************************************************
274 ************************************************************************/
277 * Process an ioctl for the virtual interface
280 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
282 struct ifreq *const ifr = (struct ifreq *) data;
286 ng_iface_print_ioctl(ifp, command, data);
291 /* These two are mostly handled at a higher layer */
293 ifp->if_flags |= IFF_UP;
294 ifp->if_drv_flags |= IFF_DRV_RUNNING;
295 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
303 * If the interface is marked up and stopped, then start it.
304 * If it is marked down and running, then stop it.
306 if (ifr->ifr_flags & IFF_UP) {
307 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
308 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
309 ifp->if_drv_flags |= IFF_DRV_RUNNING;
312 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
313 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
318 /* Set the interface MTU */
320 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
321 || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
324 ifp->if_mtu = ifr->ifr_mtu;
327 /* Stuff that's not supported */
345 * This routine is called to deliver a packet out the interface.
346 * We simply look at the address family and relay the packet to
347 * the corresponding hook, if it exists and is connected.
351 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
352 struct sockaddr *dst, struct rtentry *rt0)
357 /* Check interface flags */
358 if (!((ifp->if_flags & IFF_UP) &&
359 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
364 /* BPF writes need to be handled specially. */
365 if (dst->sa_family == AF_UNSPEC) {
366 bcopy(dst->sa_data, &af, sizeof(af));
370 /* Berkeley packet filter */
371 ng_iface_bpftap(ifp, m, dst->sa_family);
373 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
374 M_PREPEND(m, sizeof(sa_family_t), MB_DONTWAIT);
376 IFQ_LOCK(&ifp->if_snd);
377 IFQ_INC_DROPS(&ifp->if_snd);
378 IFQ_UNLOCK(&ifp->if_snd);
382 *(sa_family_t *)m->m_data = dst->sa_family;
383 IFQ_HANDOFF(ifp, m, error);
385 error = ng_iface_send(ifp, m, dst->sa_family);
391 * Start method is used only when ALTQ is enabled.
394 ng_iface_start(struct ifnet *ifp)
399 KASSERT(ALTQ_IS_ENABLED(&ifp->if_snd), ("%s without ALTQ", __func__));
402 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
405 sa = *mtod(m, sa_family_t *);
406 m_adj(m, sizeof(sa_family_t));
407 ng_iface_send(ifp, m, sa);
412 * Flash a packet by the BPF (requires prepending 4 byte AF header)
413 * Note the phoney mbuf; this is OK because BPF treats it read-only.
416 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
418 KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
419 if (bpf_peers_present(ifp->if_bpf)) {
420 int32_t family4 = (int32_t)family;
421 bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m);
426 * This routine does actual delivery of the packet into the
427 * netgraph(4). It is called from ng_iface_start() and
431 ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa)
433 const priv_p priv = (priv_p) ifp->if_softc;
434 const iffam_p iffam = get_iffam_from_af(sa);
438 /* Check address family to determine hook (if known) */
441 log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa);
442 return (EAFNOSUPPORT);
445 /* Copy length before the mbuf gets invalidated. */
446 len = m->m_pkthdr.len;
448 /* Send packet. If hook is not connected,
449 mbuf will get freed. */
450 NG_SEND_DATA_ONLY(error, *get_hook_from_iffam(priv, iffam), m);
454 ifp->if_obytes += len;
463 * Display an ioctl to the virtual interface
467 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
471 switch (command & IOC_DIRMASK) {
487 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
492 IOCPARM_LEN(command));
496 /************************************************************************
498 ************************************************************************/
501 * Constructor for a node
504 ng_iface_constructor(node_p node)
509 /* Allocate node and interface private structures */
510 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_IFACE, M_WAITOK | M_NULLOK | M_ZERO);
513 ifp = if_alloc(IFT_PROPVIRTUAL);
515 FREE(priv, M_NETGRAPH_IFACE);
519 /* Link them together */
520 ifp->if_softc = priv;
523 /* Get an interface unit number */
524 priv->unit = alloc_unr(ng_iface_unit);
526 /* Link together node and private info */
527 NG_NODE_SET_PRIVATE(node, priv);
530 /* Initialize interface structure */
531 if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
532 ifp->if_output = ng_iface_output;
533 ifp->if_start = ng_iface_start;
534 ifp->if_ioctl = ng_iface_ioctl;
535 ifp->if_watchdog = NULL;
536 ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
537 ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
538 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */
539 ifp->if_addrlen = 0; /* XXX */
540 ifp->if_hdrlen = 0; /* XXX */
541 ifp->if_baudrate = 64000; /* XXX */
542 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
543 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
544 IFQ_SET_READY(&ifp->if_snd);
546 /* Give this node the same name as the interface (if possible) */
547 if (ng_name_node(node, ifp->if_xname) != 0)
548 log(LOG_WARNING, "%s: can't acquire netgraph name\n",
551 /* Attach the interface */
553 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
560 * Give our ok for a hook to be added
563 ng_iface_newhook(node_p node, hook_p hook, const char *name)
565 const iffam_p iffam = get_iffam_from_name(name);
569 return (EPFNOSUPPORT);
570 hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam);
571 if (*hookptr != NULL)
574 NG_HOOK_HI_STACK(hook);
579 * Receive a control message
582 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook)
584 const priv_p priv = NG_NODE_PRIVATE(node);
585 struct ifnet *const ifp = priv->ifp;
586 struct ng_mesg *resp = NULL;
590 NGI_GET_MSG(item, msg);
591 switch (msg->header.typecookie) {
592 case NGM_IFACE_COOKIE:
593 switch (msg->header.cmd) {
594 case NGM_IFACE_GET_IFNAME:
595 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_WAITOK | M_NULLOK);
600 strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
603 case NGM_IFACE_POINT2POINT:
604 case NGM_IFACE_BROADCAST:
607 /* Deny request if interface is UP */
608 if ((ifp->if_flags & IFF_UP) != 0)
612 switch (msg->header.cmd) {
613 case NGM_IFACE_POINT2POINT:
614 ifp->if_flags |= IFF_POINTOPOINT;
615 ifp->if_flags &= ~IFF_BROADCAST;
617 case NGM_IFACE_BROADCAST:
618 ifp->if_flags &= ~IFF_POINTOPOINT;
619 ifp->if_flags |= IFF_BROADCAST;
625 case NGM_IFACE_GET_IFINDEX:
626 NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_WAITOK | M_NULLOK);
631 *((uint32_t *)resp->data) = priv->ifp->if_index;
639 case NGM_CISCO_COOKIE:
640 switch (msg->header.cmd) {
641 case NGM_CISCO_GET_IPADDR: /* we understand this too */
645 /* Return the first configured IP address */
646 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
647 struct ng_cisco_ipaddr *ips;
649 if (ifa->ifa_addr->sa_family != AF_INET)
651 NG_MKRESPONSE(resp, msg, sizeof(ips), M_WAITOK | M_NULLOK);
656 ips = (struct ng_cisco_ipaddr *)resp->data;
657 ips->ipaddr = ((struct sockaddr_in *)
658 ifa->ifa_addr)->sin_addr;
659 ips->netmask = ((struct sockaddr_in *)
660 ifa->ifa_netmask)->sin_addr;
664 /* No IP addresses on this interface? */
666 error = EADDRNOTAVAIL;
674 case NGM_FLOW_COOKIE:
675 switch (msg->header.cmd) {
677 ifp->if_drv_flags |= IFF_DRV_RUNNING;
679 case NGM_LINK_IS_DOWN:
680 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
690 NG_RESPOND_MSG(error, node, item, resp);
696 * Recive data from a hook. Pass the packet to the correct input routine.
699 ng_iface_rcvdata(hook_p hook, item_p item)
701 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
702 const iffam_p iffam = get_iffam_from_hook(priv, hook);
703 struct ifnet *const ifp = priv->ifp;
710 KASSERT(iffam != NULL, ("%s: iffam", __func__));
712 if ((ifp->if_flags & IFF_UP) == 0) {
717 /* Update interface stats */
719 ifp->if_ibytes += m->m_pkthdr.len;
721 /* Note receiving interface */
722 m->m_pkthdr.rcvif = ifp;
724 /* Berkeley packet filter */
725 ng_iface_bpftap(ifp, m, iffam->family);
728 switch (iffam->family) {
746 return (EAFNOSUPPORT);
748 /* First chunk of an mbuf contains good junk */
749 if (harvest.point_to_point)
750 random_harvest(m, 16, 3, 0, RANDOM_NET);
751 netisr_queue(isr, m);
756 * Shutdown and remove the node and its associated interface.
759 ng_iface_shutdown(node_p node)
761 const priv_p priv = NG_NODE_PRIVATE(node);
763 bpfdetach(priv->ifp);
764 if_detach(priv->ifp);
767 free_unr(ng_iface_unit, priv->unit);
768 FREE(priv, M_NETGRAPH_IFACE);
769 NG_NODE_SET_PRIVATE(node, NULL);
775 * Hook disconnection. Note that we do *not* shutdown when all
776 * hooks have been disconnected.
779 ng_iface_disconnect(hook_p hook)
781 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
782 const iffam_p iffam = get_iffam_from_hook(priv, hook);
786 *get_hook_from_iffam(priv, iffam) = NULL;
791 * Handle loading and unloading for this node type.
794 ng_iface_mod_event(module_t mod, int event, void *data)
800 ng_iface_unit = new_unrhdr(0, 0xffff, NULL);
803 delete_unrhdr(ng_iface_unit);