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>
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, 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"
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/errno.h>
61 #include <sys/kernel.h>
63 #include <sys/malloc.h>
65 #include <sys/errno.h>
67 #include <sys/random.h>
68 #include <sys/rmlock.h>
69 #include <sys/sockio.h>
70 #include <sys/socket.h>
71 #include <sys/sysctl.h>
72 #include <sys/syslog.h>
73 #include <sys/libkern.h>
76 #include <net/if_var.h>
77 #include <net/if_types.h>
79 #include <net/netisr.h>
80 #include <net/route.h>
83 #include <netinet/in.h>
85 #include <netgraph/ng_message.h>
86 #include <netgraph/netgraph.h>
87 #include <netgraph/ng_parse.h>
88 #include <netgraph/ng_iface.h>
90 #ifdef NG_SEPARATE_MALLOC
91 static MALLOC_DEFINE(M_NETGRAPH_IFACE, "netgraph_iface", "netgraph iface node");
93 #define M_NETGRAPH_IFACE M_NETGRAPH
96 static SYSCTL_NODE(_net_graph, OID_AUTO, iface, CTLFLAG_RW, 0,
97 "Point to point netgraph interface");
98 VNET_DEFINE_STATIC(int, ng_iface_max_nest) = 2;
99 #define V_ng_iface_max_nest VNET(ng_iface_max_nest)
100 SYSCTL_INT(_net_graph_iface, OID_AUTO, max_nesting, CTLFLAG_VNET | CTLFLAG_RW,
101 &VNET_NAME(ng_iface_max_nest), 0, "Max nested tunnels");
103 /* This struct describes one address family */
105 sa_family_t family; /* Address family */
106 const char *hookname; /* Name for hook */
108 typedef const struct iffam *iffam_p;
110 /* List of address families supported by our interface */
111 const static struct iffam gFamilies[] = {
112 { AF_INET, NG_IFACE_HOOK_INET },
113 { AF_INET6, NG_IFACE_HOOK_INET6 },
114 { AF_ATM, NG_IFACE_HOOK_ATM },
115 { AF_NATM, NG_IFACE_HOOK_NATM },
117 #define NUM_FAMILIES nitems(gFamilies)
119 /* Node private data */
120 struct ng_iface_private {
121 struct ifnet *ifp; /* Our interface */
122 int unit; /* Interface unit number */
123 node_p node; /* Our netgraph node */
124 hook_p hooks[NUM_FAMILIES]; /* Hook for each address family */
125 struct rmlock lock; /* Protect private data changes */
127 typedef struct ng_iface_private *priv_p;
129 #define PRIV_RLOCK(priv, t) rm_rlock(&priv->lock, t)
130 #define PRIV_RUNLOCK(priv, t) rm_runlock(&priv->lock, t)
131 #define PRIV_WLOCK(priv) rm_wlock(&priv->lock)
132 #define PRIV_WUNLOCK(priv) rm_wunlock(&priv->lock)
134 /* Interface methods */
135 static void ng_iface_start(struct ifnet *ifp);
136 static int ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
137 static int ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
138 const struct sockaddr *dst, struct route *ro);
139 static void ng_iface_bpftap(struct ifnet *ifp,
140 struct mbuf *m, sa_family_t family);
141 static int ng_iface_send(struct ifnet *ifp, struct mbuf *m,
144 static void ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
147 /* Netgraph methods */
148 static int ng_iface_mod_event(module_t, int, void *);
149 static ng_constructor_t ng_iface_constructor;
150 static ng_rcvmsg_t ng_iface_rcvmsg;
151 static ng_shutdown_t ng_iface_shutdown;
152 static ng_newhook_t ng_iface_newhook;
153 static ng_rcvdata_t ng_iface_rcvdata;
154 static ng_disconnect_t ng_iface_disconnect;
157 static iffam_p get_iffam_from_af(sa_family_t family);
158 static iffam_p get_iffam_from_hook(priv_p priv, hook_p hook);
159 static iffam_p get_iffam_from_name(const char *name);
160 static hook_p *get_hook_from_iffam(priv_p priv, iffam_p iffam);
162 /* List of commands and how to convert arguments to/from ASCII */
163 static const struct ng_cmdlist ng_iface_cmds[] = {
166 NGM_IFACE_GET_IFNAME,
169 &ng_parse_string_type
173 NGM_IFACE_POINT2POINT,
187 NGM_IFACE_GET_IFINDEX,
190 &ng_parse_uint32_type
195 /* Node type descriptor */
196 static struct ng_type typestruct = {
197 .version = NG_ABI_VERSION,
198 .name = NG_IFACE_NODE_TYPE,
199 .mod_event = ng_iface_mod_event,
200 .constructor = ng_iface_constructor,
201 .rcvmsg = ng_iface_rcvmsg,
202 .shutdown = ng_iface_shutdown,
203 .newhook = ng_iface_newhook,
204 .rcvdata = ng_iface_rcvdata,
205 .disconnect = ng_iface_disconnect,
206 .cmdlist = ng_iface_cmds,
208 NETGRAPH_INIT(iface, &typestruct);
210 VNET_DEFINE_STATIC(struct unrhdr *, ng_iface_unit);
211 #define V_ng_iface_unit VNET(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);
293 /* These two are mostly handled at a higher layer */
295 ifp->if_flags |= IFF_UP;
296 ifp->if_drv_flags |= IFF_DRV_RUNNING;
297 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
305 * If the interface is marked up and stopped, then start it.
306 * If it is marked down and running, then stop it.
308 if (ifr->ifr_flags & IFF_UP) {
309 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
310 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
311 ifp->if_drv_flags |= IFF_DRV_RUNNING;
314 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
315 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
320 /* Set the interface MTU */
322 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
323 || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
326 ifp->if_mtu = ifr->ifr_mtu;
329 /* Stuff that's not supported */
346 * This routine is called to deliver a packet out the interface.
347 * We simply look at the address family and relay the packet to
348 * the corresponding hook, if it exists and is connected.
352 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
353 const struct sockaddr *dst, struct route *ro)
358 /* Check interface flags */
359 if (!((ifp->if_flags & IFF_UP) &&
360 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
365 /* Protect from deadly infinite recursion. */
366 error = if_tunnel_check_nesting(ifp, m, NGM_IFACE_COOKIE,
367 V_ng_iface_max_nest);
373 /* BPF writes need to be handled specially. */
374 if (dst->sa_family == AF_UNSPEC)
375 bcopy(dst->sa_data, &af, sizeof(af));
379 /* Berkeley packet filter */
380 ng_iface_bpftap(ifp, m, af);
382 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
383 M_PREPEND(m, sizeof(sa_family_t), M_NOWAIT);
385 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
388 *(sa_family_t *)m->m_data = af;
389 error = (ifp->if_transmit)(ifp, m);
391 error = ng_iface_send(ifp, m, af);
397 * Start method is used only when ALTQ is enabled.
400 ng_iface_start(struct ifnet *ifp)
405 KASSERT(ALTQ_IS_ENABLED(&ifp->if_snd), ("%s without ALTQ", __func__));
408 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
411 sa = *mtod(m, sa_family_t *);
412 m_adj(m, sizeof(sa_family_t));
413 ng_iface_send(ifp, m, sa);
418 * Flash a packet by the BPF (requires prepending 4 byte AF header)
419 * Note the phoney mbuf; this is OK because BPF treats it read-only.
422 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
424 KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
425 if (bpf_peers_present(ifp->if_bpf)) {
426 int32_t family4 = (int32_t)family;
427 bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m);
432 * This routine does actual delivery of the packet into the
433 * netgraph(4). It is called from ng_iface_start() and
437 ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa)
439 struct rm_priotracker priv_tracker;
440 const priv_p priv = (priv_p) ifp->if_softc;
441 const iffam_p iffam = get_iffam_from_af(sa);
446 /* Check address family to determine hook (if known) */
449 log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa);
450 return (EAFNOSUPPORT);
453 /* Copy length before the mbuf gets invalidated. */
454 len = m->m_pkthdr.len;
456 PRIV_RLOCK(priv, &priv_tracker);
457 hook = *get_hook_from_iffam(priv, iffam);
460 PRIV_RUNLOCK(priv, &priv_tracker);
464 PRIV_RUNLOCK(priv, &priv_tracker);
466 NG_OUTBOUND_THREAD_REF();
467 NG_SEND_DATA_ONLY(error, hook, m);
468 NG_OUTBOUND_THREAD_UNREF();
473 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
474 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
482 * Display an ioctl to the virtual interface
486 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
490 switch (command & IOC_DIRMASK) {
506 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
511 IOCPARM_LEN(command));
515 /************************************************************************
517 ************************************************************************/
520 * Constructor for a node
523 ng_iface_constructor(node_p node)
528 /* Allocate node and interface private structures */
529 priv = malloc(sizeof(*priv), M_NETGRAPH_IFACE, M_WAITOK | M_ZERO);
530 ifp = if_alloc(IFT_PROPVIRTUAL);
532 free(priv, M_NETGRAPH_IFACE);
536 rm_init(&priv->lock, "ng_iface private rmlock");
538 /* Link them together */
539 ifp->if_softc = priv;
542 /* Get an interface unit number */
543 priv->unit = alloc_unr(V_ng_iface_unit);
545 /* Link together node and private info */
546 NG_NODE_SET_PRIVATE(node, priv);
549 /* Initialize interface structure */
550 if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
551 ifp->if_output = ng_iface_output;
552 ifp->if_start = ng_iface_start;
553 ifp->if_ioctl = ng_iface_ioctl;
554 ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
555 ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
556 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */
557 ifp->if_addrlen = 0; /* XXX */
558 ifp->if_hdrlen = 0; /* XXX */
559 ifp->if_baudrate = 64000; /* XXX */
560 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
561 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
562 IFQ_SET_READY(&ifp->if_snd);
564 /* Give this node the same name as the interface (if possible) */
565 if (ng_name_node(node, ifp->if_xname) != 0)
566 log(LOG_WARNING, "%s: can't acquire netgraph name\n",
569 /* Attach the interface */
571 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
578 * Give our ok for a hook to be added
581 ng_iface_newhook(node_p node, hook_p hook, const char *name)
583 const iffam_p iffam = get_iffam_from_name(name);
584 const priv_p priv = NG_NODE_PRIVATE(node);
588 return (EPFNOSUPPORT);
590 hookptr = get_hook_from_iffam(priv, iffam);
591 if (*hookptr != NULL) {
596 NG_HOOK_HI_STACK(hook);
597 NG_HOOK_SET_TO_INBOUND(hook);
603 * Receive a control message
606 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook)
608 const priv_p priv = NG_NODE_PRIVATE(node);
609 struct ifnet *const ifp = priv->ifp;
610 struct ng_mesg *resp = NULL;
614 NGI_GET_MSG(item, msg);
615 switch (msg->header.typecookie) {
616 case NGM_IFACE_COOKIE:
617 switch (msg->header.cmd) {
618 case NGM_IFACE_GET_IFNAME:
619 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
624 strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
627 case NGM_IFACE_POINT2POINT:
628 case NGM_IFACE_BROADCAST:
631 /* Deny request if interface is UP */
632 if ((ifp->if_flags & IFF_UP) != 0)
636 switch (msg->header.cmd) {
637 case NGM_IFACE_POINT2POINT:
638 ifp->if_flags |= IFF_POINTOPOINT;
639 ifp->if_flags &= ~IFF_BROADCAST;
641 case NGM_IFACE_BROADCAST:
642 ifp->if_flags &= ~IFF_POINTOPOINT;
643 ifp->if_flags |= IFF_BROADCAST;
649 case NGM_IFACE_GET_IFINDEX:
650 NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT);
655 *((uint32_t *)resp->data) = priv->ifp->if_index;
663 case NGM_FLOW_COOKIE:
664 switch (msg->header.cmd) {
666 if_link_state_change(ifp, LINK_STATE_UP);
668 case NGM_LINK_IS_DOWN:
669 if_link_state_change(ifp, LINK_STATE_DOWN);
679 NG_RESPOND_MSG(error, node, item, resp);
685 * Recive data from a hook. Pass the packet to the correct input routine.
688 ng_iface_rcvdata(hook_p hook, item_p item)
690 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
691 const iffam_p iffam = get_iffam_from_hook(priv, hook);
692 struct ifnet *const ifp = priv->ifp;
699 KASSERT(iffam != NULL, ("%s: iffam", __func__));
701 if ((ifp->if_flags & IFF_UP) == 0) {
706 /* Update interface stats */
707 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
708 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
710 /* Note receiving interface */
711 m->m_pkthdr.rcvif = ifp;
713 /* Berkeley packet filter */
714 ng_iface_bpftap(ifp, m, iffam->family);
717 switch (iffam->family) {
730 return (EAFNOSUPPORT);
732 random_harvest_queue(m, sizeof(*m), RANDOM_NET_NG);
733 M_SETFIB(m, ifp->if_fib);
734 netisr_dispatch(isr, m);
739 * Shutdown and remove the node and its associated interface.
742 ng_iface_shutdown(node_p node)
744 const priv_p priv = NG_NODE_PRIVATE(node);
747 * The ifnet may be in a different vnet than the netgraph node,
748 * hence we have to change the current vnet context here.
750 CURVNET_SET_QUIET(priv->ifp->if_vnet);
751 bpfdetach(priv->ifp);
752 if_detach(priv->ifp);
756 free_unr(V_ng_iface_unit, priv->unit);
757 rm_destroy(&priv->lock);
758 free(priv, M_NETGRAPH_IFACE);
759 NG_NODE_SET_PRIVATE(node, NULL);
765 * Hook disconnection. Note that we do *not* shutdown when all
766 * hooks have been disconnected.
769 ng_iface_disconnect(hook_p hook)
771 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
772 const iffam_p iffam = get_iffam_from_hook(priv, hook);
775 panic("%s", __func__);
777 *get_hook_from_iffam(priv, iffam) = NULL;
783 * Handle loading and unloading for this node type.
786 ng_iface_mod_event(module_t mod, int event, void *data)
802 vnet_ng_iface_init(const void *unused)
805 V_ng_iface_unit = new_unrhdr(0, 0xffff, NULL);
807 VNET_SYSINIT(vnet_ng_iface_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
808 vnet_ng_iface_init, NULL);
811 vnet_ng_iface_uninit(const void *unused)
814 delete_unrhdr(V_ng_iface_unit);
816 VNET_SYSUNINIT(vnet_ng_iface_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
817 vnet_ng_iface_uninit, NULL);