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>
62 #include <sys/malloc.h>
64 #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_var.h>
74 #include <net/if_types.h>
76 #include <net/netisr.h>
77 #include <net/route.h>
80 #include <netinet/in.h>
82 #include <netgraph/ng_message.h>
83 #include <netgraph/netgraph.h>
84 #include <netgraph/ng_parse.h>
85 #include <netgraph/ng_iface.h>
87 #ifdef NG_SEPARATE_MALLOC
88 static 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_ATM, NG_IFACE_HOOK_ATM },
105 { AF_NATM, NG_IFACE_HOOK_NATM },
107 #define NUM_FAMILIES nitems(gFamilies)
109 /* Node private data */
110 struct ng_iface_private {
111 struct ifnet *ifp; /* Our interface */
112 int unit; /* Interface unit number */
113 node_p node; /* Our netgraph node */
114 hook_p hooks[NUM_FAMILIES]; /* Hook for each address family */
116 typedef struct ng_iface_private *priv_p;
118 /* Interface methods */
119 static void ng_iface_start(struct ifnet *ifp);
120 static int ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
121 static int ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
122 const struct sockaddr *dst, struct route *ro);
123 static void ng_iface_bpftap(struct ifnet *ifp,
124 struct mbuf *m, sa_family_t family);
125 static int ng_iface_send(struct ifnet *ifp, struct mbuf *m,
128 static void ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
131 /* Netgraph methods */
132 static int ng_iface_mod_event(module_t, int, void *);
133 static ng_constructor_t ng_iface_constructor;
134 static ng_rcvmsg_t ng_iface_rcvmsg;
135 static ng_shutdown_t ng_iface_shutdown;
136 static ng_newhook_t ng_iface_newhook;
137 static ng_rcvdata_t ng_iface_rcvdata;
138 static ng_disconnect_t ng_iface_disconnect;
141 static iffam_p get_iffam_from_af(sa_family_t family);
142 static iffam_p get_iffam_from_hook(priv_p priv, hook_p hook);
143 static iffam_p get_iffam_from_name(const char *name);
144 static hook_p *get_hook_from_iffam(priv_p priv, iffam_p iffam);
146 /* List of commands and how to convert arguments to/from ASCII */
147 static const struct ng_cmdlist ng_iface_cmds[] = {
150 NGM_IFACE_GET_IFNAME,
153 &ng_parse_string_type
157 NGM_IFACE_POINT2POINT,
171 NGM_IFACE_GET_IFINDEX,
174 &ng_parse_uint32_type
179 /* Node type descriptor */
180 static struct ng_type typestruct = {
181 .version = NG_ABI_VERSION,
182 .name = NG_IFACE_NODE_TYPE,
183 .mod_event = ng_iface_mod_event,
184 .constructor = ng_iface_constructor,
185 .rcvmsg = ng_iface_rcvmsg,
186 .shutdown = ng_iface_shutdown,
187 .newhook = ng_iface_newhook,
188 .rcvdata = ng_iface_rcvdata,
189 .disconnect = ng_iface_disconnect,
190 .cmdlist = ng_iface_cmds,
192 NETGRAPH_INIT(iface, &typestruct);
194 static VNET_DEFINE(struct unrhdr *, ng_iface_unit);
195 #define V_ng_iface_unit VNET(ng_iface_unit)
197 /************************************************************************
199 ************************************************************************/
202 * Get the family descriptor from the family ID
204 static __inline iffam_p
205 get_iffam_from_af(sa_family_t family)
210 for (k = 0; k < NUM_FAMILIES; k++) {
211 iffam = &gFamilies[k];
212 if (iffam->family == family)
219 * Get the family descriptor from the hook
221 static __inline iffam_p
222 get_iffam_from_hook(priv_p priv, hook_p hook)
226 for (k = 0; k < NUM_FAMILIES; k++)
227 if (priv->hooks[k] == hook)
228 return (&gFamilies[k]);
233 * Get the hook from the iffam descriptor
236 static __inline hook_p *
237 get_hook_from_iffam(priv_p priv, iffam_p iffam)
239 return (&priv->hooks[iffam - gFamilies]);
243 * Get the iffam descriptor from the name
245 static __inline iffam_p
246 get_iffam_from_name(const char *name)
251 for (k = 0; k < NUM_FAMILIES; k++) {
252 iffam = &gFamilies[k];
253 if (!strcmp(iffam->hookname, name))
259 /************************************************************************
261 ************************************************************************/
264 * Process an ioctl for the virtual interface
267 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
269 struct ifreq *const ifr = (struct ifreq *) data;
273 ng_iface_print_ioctl(ifp, command, data);
277 /* These two are mostly handled at a higher layer */
279 ifp->if_flags |= IFF_UP;
280 ifp->if_drv_flags |= IFF_DRV_RUNNING;
281 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
289 * If the interface is marked up and stopped, then start it.
290 * If it is marked down and running, then stop it.
292 if (ifr->ifr_flags & IFF_UP) {
293 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
294 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
295 ifp->if_drv_flags |= IFF_DRV_RUNNING;
298 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
299 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
304 /* Set the interface MTU */
306 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
307 || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
310 ifp->if_mtu = ifr->ifr_mtu;
313 /* Stuff that's not supported */
330 * This routine is called to deliver a packet out the interface.
331 * We simply look at the address family and relay the packet to
332 * the corresponding hook, if it exists and is connected.
336 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
337 const struct sockaddr *dst, struct route *ro)
343 /* Check interface flags */
344 if (!((ifp->if_flags & IFF_UP) &&
345 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
350 /* Protect from deadly infinite recursion. */
352 while ((mtag = m_tag_locate(m, MTAG_NGIF, MTAG_NGIF_CALLED, mtag))) {
353 if (*(struct ifnet **)(mtag + 1) == ifp) {
354 log(LOG_NOTICE, "Loop detected on %s\n", ifp->if_xname);
359 mtag = m_tag_alloc(MTAG_NGIF, MTAG_NGIF_CALLED, sizeof(struct ifnet *),
365 *(struct ifnet **)(mtag + 1) = ifp;
366 m_tag_prepend(m, mtag);
368 /* BPF writes need to be handled specially. */
369 if (dst->sa_family == AF_UNSPEC)
370 bcopy(dst->sa_data, &af, sizeof(af));
374 /* Berkeley packet filter */
375 ng_iface_bpftap(ifp, m, af);
377 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
378 M_PREPEND(m, sizeof(sa_family_t), M_NOWAIT);
380 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
383 *(sa_family_t *)m->m_data = af;
384 error = (ifp->if_transmit)(ifp, m);
386 error = ng_iface_send(ifp, m, af);
392 * Start method is used only when ALTQ is enabled.
395 ng_iface_start(struct ifnet *ifp)
400 KASSERT(ALTQ_IS_ENABLED(&ifp->if_snd), ("%s without ALTQ", __func__));
403 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
406 sa = *mtod(m, sa_family_t *);
407 m_adj(m, sizeof(sa_family_t));
408 ng_iface_send(ifp, m, sa);
413 * Flash a packet by the BPF (requires prepending 4 byte AF header)
414 * Note the phoney mbuf; this is OK because BPF treats it read-only.
417 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
419 KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
420 if (bpf_peers_present(ifp->if_bpf)) {
421 int32_t family4 = (int32_t)family;
422 bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m);
427 * This routine does actual delivery of the packet into the
428 * netgraph(4). It is called from ng_iface_start() and
432 ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa)
434 const priv_p priv = (priv_p) ifp->if_softc;
435 const iffam_p iffam = get_iffam_from_af(sa);
439 /* Check address family to determine hook (if known) */
442 log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa);
443 return (EAFNOSUPPORT);
446 /* Copy length before the mbuf gets invalidated. */
447 len = m->m_pkthdr.len;
449 /* Send packet. If hook is not connected, mbuf will get freed. */
450 NG_OUTBOUND_THREAD_REF();
451 NG_SEND_DATA_ONLY(error, *get_hook_from_iffam(priv, iffam), m);
452 NG_OUTBOUND_THREAD_UNREF();
456 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
457 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
465 * Display an ioctl to the virtual interface
469 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
473 switch (command & IOC_DIRMASK) {
489 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
494 IOCPARM_LEN(command));
498 /************************************************************************
500 ************************************************************************/
503 * Constructor for a node
506 ng_iface_constructor(node_p node)
511 /* Allocate node and interface private structures */
512 priv = malloc(sizeof(*priv), M_NETGRAPH_IFACE, M_WAITOK | 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(V_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_mtu = NG_IFACE_MTU_DEFAULT;
536 ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
537 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */
538 ifp->if_addrlen = 0; /* XXX */
539 ifp->if_hdrlen = 0; /* XXX */
540 ifp->if_baudrate = 64000; /* XXX */
541 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
542 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
543 IFQ_SET_READY(&ifp->if_snd);
545 /* Give this node the same name as the interface (if possible) */
546 if (ng_name_node(node, ifp->if_xname) != 0)
547 log(LOG_WARNING, "%s: can't acquire netgraph name\n",
550 /* Attach the interface */
552 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
559 * Give our ok for a hook to be added
562 ng_iface_newhook(node_p node, hook_p hook, const char *name)
564 const iffam_p iffam = get_iffam_from_name(name);
568 return (EPFNOSUPPORT);
569 hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam);
570 if (*hookptr != NULL)
573 NG_HOOK_HI_STACK(hook);
574 NG_HOOK_SET_TO_INBOUND(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_NOWAIT);
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_NOWAIT);
631 *((uint32_t *)resp->data) = priv->ifp->if_index;
639 case NGM_FLOW_COOKIE:
640 switch (msg->header.cmd) {
642 if_link_state_change(ifp, LINK_STATE_UP);
644 case NGM_LINK_IS_DOWN:
645 if_link_state_change(ifp, LINK_STATE_DOWN);
655 NG_RESPOND_MSG(error, node, item, resp);
661 * Recive data from a hook. Pass the packet to the correct input routine.
664 ng_iface_rcvdata(hook_p hook, item_p item)
666 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
667 const iffam_p iffam = get_iffam_from_hook(priv, hook);
668 struct ifnet *const ifp = priv->ifp;
675 KASSERT(iffam != NULL, ("%s: iffam", __func__));
677 if ((ifp->if_flags & IFF_UP) == 0) {
682 /* Update interface stats */
683 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
684 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
686 /* Note receiving interface */
687 m->m_pkthdr.rcvif = ifp;
689 /* Berkeley packet filter */
690 ng_iface_bpftap(ifp, m, iffam->family);
693 switch (iffam->family) {
706 return (EAFNOSUPPORT);
708 random_harvest_queue(m, sizeof(*m), 2, RANDOM_NET_NG);
709 M_SETFIB(m, ifp->if_fib);
710 netisr_dispatch(isr, m);
715 * Shutdown and remove the node and its associated interface.
718 ng_iface_shutdown(node_p node)
720 const priv_p priv = NG_NODE_PRIVATE(node);
723 * The ifnet may be in a different vnet than the netgraph node,
724 * hence we have to change the current vnet context here.
726 CURVNET_SET_QUIET(priv->ifp->if_vnet);
727 bpfdetach(priv->ifp);
728 if_detach(priv->ifp);
732 free_unr(V_ng_iface_unit, priv->unit);
733 free(priv, M_NETGRAPH_IFACE);
734 NG_NODE_SET_PRIVATE(node, NULL);
740 * Hook disconnection. Note that we do *not* shutdown when all
741 * hooks have been disconnected.
744 ng_iface_disconnect(hook_p hook)
746 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
747 const iffam_p iffam = get_iffam_from_hook(priv, hook);
750 panic("%s", __func__);
751 *get_hook_from_iffam(priv, iffam) = NULL;
756 * Handle loading and unloading for this node type.
759 ng_iface_mod_event(module_t mod, int event, void *data)
775 vnet_ng_iface_init(const void *unused)
778 V_ng_iface_unit = new_unrhdr(0, 0xffff, NULL);
780 VNET_SYSINIT(vnet_ng_iface_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
781 vnet_ng_iface_init, NULL);
784 vnet_ng_iface_uninit(const void *unused)
787 delete_unrhdr(V_ng_iface_unit);
789 VNET_SYSUNINIT(vnet_ng_iface_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
790 vnet_ng_iface_uninit, NULL);