5 * Copyright (c) 1996-1999 Whistle Communications, Inc.
8 * Subject to the following obligations and disclaimer of warranty, use and
9 * redistribution of this software, in source or object code forms, with or
10 * without modifications are expressly permitted by Whistle Communications;
11 * provided, however, that:
12 * 1. Any and all reproductions of the source or object code must include the
13 * copyright notice above and the following disclaimer of warranties; and
14 * 2. No rights are granted, in any manner or form, to use Whistle
15 * Communications, Inc. trademarks, including the mark "WHISTLE
16 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17 * such appears in the above copyright notice or in the software.
19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
37 * Author: Archie Cobbs <archie@freebsd.org>
39 * $FreeBSD: src/sys/netgraph/ng_iface.c,v 1.7.2.5 2002/07/02 23:44:02 archie Exp $
40 * $Whistle: ng_iface.c,v 1.33 1999/11/01 09:24:51 julian Exp $
44 * This node is also a system networking interface. It has
45 * a hook for each protocol (IP, AppleTalk, IPX, etc). Packets
46 * are simply relayed between the interface and the hooks.
48 * Interfaces are named ng0, ng1, etc. New nodes take the
49 * first available interface name.
51 * This node also includes Berkeley packet filter support.
55 #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>
65 #include <sys/sockio.h>
66 #include <sys/socket.h>
67 #include <sys/syslog.h>
68 #include <sys/libkern.h>
69 #include <sys/thread2.h>
72 #include <net/if_types.h>
73 #include <net/ifq_var.h>
74 #include <net/netisr.h>
77 #include <netinet/in.h>
79 #include <netgraph/ng_message.h>
80 #include <netgraph/netgraph.h>
81 #include <netgraph/ng_parse.h>
83 #include <netgraph/cisco/ng_cisco.h>
85 /* This struct describes one address family */
87 sa_family_t family; /* Address family */
88 const char *hookname; /* Name for hook */
90 typedef const struct iffam *iffam_p;
92 /* List of address families supported by our interface */
93 static const struct iffam gFamilies[] = {
94 { AF_INET, NG_IFACE_HOOK_INET },
95 { AF_INET6, NG_IFACE_HOOK_INET6 },
96 { AF_IPX, NG_IFACE_HOOK_IPX },
97 { AF_ATM, NG_IFACE_HOOK_ATM },
98 { AF_NATM, NG_IFACE_HOOK_NATM },
99 { AF_NS, NG_IFACE_HOOK_NS },
101 #define NUM_FAMILIES NELEM(gFamilies)
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 */
110 typedef struct ng_iface_private *priv_p;
112 /* Interface methods */
113 static void ng_iface_start(struct ifnet *ifp, struct ifaltq_subque *);
114 static int ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data,
116 static int ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
117 struct sockaddr *dst, struct rtentry *rt0);
118 static void ng_iface_bpftap(struct ifnet *ifp,
119 struct mbuf *m, sa_family_t family);
121 static void ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
124 /* Netgraph methods */
125 static ng_constructor_t ng_iface_constructor;
126 static ng_rcvmsg_t ng_iface_rcvmsg;
127 static ng_shutdown_t ng_iface_rmnode;
128 static ng_newhook_t ng_iface_newhook;
129 static ng_rcvdata_t ng_iface_rcvdata;
130 static ng_disconnect_t ng_iface_disconnect;
133 static iffam_p get_iffam_from_af(sa_family_t family);
134 static iffam_p get_iffam_from_hook(priv_p priv, hook_p hook);
135 static iffam_p get_iffam_from_name(const char *name);
136 static hook_p *get_hook_from_iffam(priv_p priv, iffam_p iffam);
138 /* Parse type for struct ng_iface_ifname */
139 static const struct ng_parse_fixedstring_info ng_iface_ifname_info = {
140 NG_IFACE_IFACE_NAME_MAX + 1
142 static const struct ng_parse_type ng_iface_ifname_type = {
143 &ng_parse_fixedstring_type,
144 &ng_iface_ifname_info
147 /* Parse type for struct ng_cisco_ipaddr */
148 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
149 = NG_CISCO_IPADDR_TYPE_INFO;
150 static const struct ng_parse_type ng_cisco_ipaddr_type = {
151 &ng_parse_struct_type,
152 &ng_cisco_ipaddr_type_fields
155 /* List of commands and how to convert arguments to/from ASCII */
156 static const struct ng_cmdlist ng_iface_cmds[] = {
159 NGM_IFACE_GET_IFNAME,
162 &ng_iface_ifname_type
166 NGM_IFACE_POINT2POINT,
180 NGM_CISCO_GET_IPADDR,
183 &ng_cisco_ipaddr_type
188 /* Node type descriptor */
189 static struct ng_type typestruct = {
193 ng_iface_constructor,
204 NETGRAPH_INIT(iface, &typestruct);
206 /* We keep a bitmap indicating which unit numbers are free.
207 One means the unit number is free, zero means it's taken. */
208 static int *ng_iface_units = NULL;
209 static int ng_iface_units_len = 0;
211 #define UNITS_BITSPERWORD (sizeof(*ng_iface_units) * NBBY)
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))
276 * Find the first free unit number for a new interface.
277 * Increase the size of the unit bitmap as necessary.
279 static __inline__ int
280 ng_iface_get_unit(int *unit)
284 for (index = 0; index < ng_iface_units_len
285 && ng_iface_units[index] == 0; index++);
286 if (index == ng_iface_units_len) { /* extend array */
287 int i, *newarray, newlen;
289 newlen = (2 * ng_iface_units_len) + 4;
290 newarray = kmalloc(newlen * sizeof(*ng_iface_units),
291 M_NETGRAPH, M_NOWAIT);
292 if (newarray == NULL)
294 bcopy(ng_iface_units, newarray,
295 ng_iface_units_len * sizeof(*ng_iface_units));
296 for (i = ng_iface_units_len; i < newlen; i++)
298 if (ng_iface_units != NULL)
299 kfree(ng_iface_units, M_NETGRAPH);
300 ng_iface_units = newarray;
301 ng_iface_units_len = newlen;
303 bit = ffs(ng_iface_units[index]) - 1;
304 KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
305 ("%s: word=%d bit=%d", __func__, ng_iface_units[index], bit));
306 ng_iface_units[index] &= ~(1 << bit);
307 *unit = (index * UNITS_BITSPERWORD) + bit;
312 * Free a no longer needed unit number.
314 static __inline__ void
315 ng_iface_free_unit(int unit)
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);
327 * XXX We could think about reducing the size of ng_iface_units[]
328 * XXX here if the last portion is all ones
332 /************************************************************************
334 ************************************************************************/
337 * Process an ioctl for the virtual interface
340 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data,
343 struct ifreq *const ifr = (struct ifreq *) data;
347 ng_iface_print_ioctl(ifp, command, data);
352 /* These two are mostly handled at a higher layer */
354 ifp->if_flags |= (IFF_UP | IFF_RUNNING);
355 ifq_clr_oactive(&ifp->if_snd);
363 * If the interface is marked up and stopped, then start it.
364 * If it is marked down and running, then stop it.
366 if (ifr->ifr_flags & IFF_UP) {
367 if (!(ifp->if_flags & IFF_RUNNING)) {
368 ifq_clr_oactive(&ifp->if_snd);
369 ifp->if_flags |= IFF_RUNNING;
372 if (ifp->if_flags & IFF_RUNNING)
373 ifp->if_flags &= ~IFF_RUNNING;
374 ifq_clr_oactive(&ifp->if_snd);
378 /* Set the interface MTU */
380 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
381 || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
384 ifp->if_mtu = ifr->ifr_mtu;
387 /* Stuff that's not supported */
405 * This routine is called to deliver a packet out the interface.
406 * We simply look at the address family and relay the packet to
407 * the corresponding hook, if it exists and is connected.
411 ng_iface_output_serialized(struct ifnet *ifp, struct mbuf *m,
412 struct sockaddr *dst, struct rtentry *rt0)
414 const priv_p priv = (priv_p) ifp->if_softc;
415 const iffam_p iffam = get_iffam_from_af(dst->sa_family);
419 /* Check interface flags */
420 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
425 /* BPF writes need to be handled specially */
426 if (dst->sa_family == AF_UNSPEC) {
427 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL)
429 dst->sa_family = (sa_family_t)*mtod(m, int32_t *);
432 m->m_pkthdr.len -= 4;
435 /* Berkeley packet filter */
436 ng_iface_bpftap(ifp, m, dst->sa_family);
438 /* Check address family to determine hook (if known) */
441 log(LOG_WARNING, "%s: can't handle af%d\n",
442 ifp->if_xname, (int)dst->sa_family);
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_SEND_DATA(error, *get_hook_from_iffam(priv, iffam), m, meta);
454 IFNET_STAT_INC(ifp, obytes, len);
455 IFNET_STAT_INC(ifp, opackets, 1);
461 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
462 struct sockaddr *dst, struct rtentry *rt0)
464 const struct ifaltq_subque *ifsq = ifq_get_subq_default(&ifp->if_snd);
467 ifnet_serialize_tx(ifp, ifsq);
468 error = ng_iface_output_serialized(ifp, m, dst, rt0);
469 ifnet_deserialize_tx(ifp, ifsq);
475 * This routine should never be called
479 ng_iface_start(struct ifnet *ifp, struct ifaltq_subque *ifsq __unused)
481 kprintf("%s: %s called?", ifp->if_xname, __func__);
485 * Flash a packet by the BPF (requires prepending 4 byte AF header)
488 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
490 int32_t family4 = (int32_t)family;
492 KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
497 bpf_ptap(ifp->if_bpf, m, &family, sizeof(family4));
504 * Display an ioctl to the virtual interface
508 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
512 switch (command & IOC_DIRMASK) {
528 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
533 IOCPARM_LEN(command));
537 /************************************************************************
539 ************************************************************************/
542 * Constructor for a node
545 ng_iface_constructor(node_p *nodep)
547 char ifname[NG_IFACE_IFACE_NAME_MAX + 1];
553 /* Allocate node and interface private structures */
554 priv = kmalloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
557 ifp = kmalloc(sizeof(*ifp), M_NETGRAPH, M_NOWAIT | M_ZERO);
559 kfree(priv, M_NETGRAPH);
563 /* Link them together */
564 ifp->if_softc = priv;
567 /* Get an interface unit number */
568 if ((error = ng_iface_get_unit(&priv->unit)) != 0) {
569 kfree(ifp, M_NETGRAPH);
570 kfree(priv, M_NETGRAPH);
574 /* Call generic node constructor */
575 if ((error = ng_make_node_common(&typestruct, nodep)) != 0) {
576 ng_iface_free_unit(priv->unit);
577 kfree(ifp, M_NETGRAPH);
578 kfree(priv, M_NETGRAPH);
583 /* Link together node and private info */
584 node->private = priv;
587 /* Initialize interface structure */
588 if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
589 ifp->if_output = ng_iface_output;
590 ifp->if_start = ng_iface_start;
591 ifp->if_ioctl = ng_iface_ioctl;
592 ifp->if_watchdog = NULL;
593 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
594 ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
595 ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
596 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */
597 ifp->if_addrlen = 0; /* XXX */
598 ifp->if_hdrlen = 0; /* XXX */
599 ifp->if_baudrate = 64000; /* XXX */
601 /* Give this node the same name as the interface (if possible) */
602 bzero(ifname, sizeof(ifname));
603 strlcpy(ifname, ifp->if_xname, sizeof(ifname));
604 if (ng_name_node(node, ifname) != 0)
605 log(LOG_WARNING, "%s: can't acquire netgraph name\n", ifname);
607 /* Attach the interface */
608 if_attach(ifp, NULL);
609 bpfattach(ifp, DLT_NULL, sizeof(u_int));
616 * Give our ok for a hook to be added
619 ng_iface_newhook(node_p node, hook_p hook, const char *name)
621 const iffam_p iffam = get_iffam_from_name(name);
625 return (EPFNOSUPPORT);
626 hookptr = get_hook_from_iffam((priv_p) node->private, iffam);
627 if (*hookptr != NULL)
634 * Receive a control message
637 ng_iface_rcvmsg(node_p node, struct ng_mesg *msg,
638 const char *retaddr, struct ng_mesg **rptr)
640 const priv_p priv = node->private;
641 struct ifnet *const ifp = priv->ifp;
642 struct ng_mesg *resp = NULL;
645 switch (msg->header.typecookie) {
646 case NGM_IFACE_COOKIE:
647 switch (msg->header.cmd) {
648 case NGM_IFACE_GET_IFNAME:
650 struct ng_iface_ifname *arg;
652 NG_MKRESPONSE(resp, msg, sizeof(*arg), M_NOWAIT);
657 arg = (struct ng_iface_ifname *)resp->data;
658 strlcpy(arg->ngif_name, ifp->if_xname,
659 sizeof(arg->ngif_name));
663 case NGM_IFACE_POINT2POINT:
664 case NGM_IFACE_BROADCAST:
667 /* Deny request if interface is UP */
668 if ((ifp->if_flags & IFF_UP) != 0)
672 switch (msg->header.cmd) {
673 case NGM_IFACE_POINT2POINT:
674 ifp->if_flags |= IFF_POINTOPOINT;
675 ifp->if_flags &= ~IFF_BROADCAST;
677 case NGM_IFACE_BROADCAST:
678 ifp->if_flags &= ~IFF_POINTOPOINT;
679 ifp->if_flags |= IFF_BROADCAST;
690 case NGM_CISCO_COOKIE:
691 switch (msg->header.cmd) {
692 case NGM_CISCO_GET_IPADDR: /* we understand this too */
694 struct ifaddr_container *ifac;
696 /* Return the first configured IP address */
697 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
699 struct ifaddr *ifa = ifac->ifa;
700 struct ng_cisco_ipaddr *ips;
702 if (ifa->ifa_addr->sa_family != AF_INET)
704 NG_MKRESPONSE(resp, msg, sizeof(ips), M_NOWAIT);
709 ips = (struct ng_cisco_ipaddr *)resp->data;
710 ips->ipaddr = ((struct sockaddr_in *)
711 ifa->ifa_addr)->sin_addr;
712 ips->netmask = ((struct sockaddr_in *)
713 ifa->ifa_netmask)->sin_addr;
717 /* No IP addresses on this interface? */
719 error = EADDRNOTAVAIL;
734 kfree(resp, M_NETGRAPH);
735 kfree(msg, M_NETGRAPH);
740 * Recive data from a hook. Pass the packet to the correct input routine.
743 ng_iface_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
745 const priv_p priv = hook->node->private;
746 const iffam_p iffam = get_iffam_from_hook(priv, hook);
747 struct ifnet *const ifp = priv->ifp;
751 KASSERT(iffam != NULL, ("%s: iffam", __func__));
752 KASSERT(m->m_flags & M_PKTHDR, ("%s: not pkthdr", __func__));
755 if ((ifp->if_flags & IFF_UP) == 0) {
756 NG_FREE_DATA(m, meta);
760 /* Update interface stats */
761 IFNET_STAT_INC(ifp, ipackets, 1);
762 IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len);
764 /* Note receiving interface */
765 m->m_pkthdr.rcvif = ifp;
767 /* Berkeley packet filter */
768 ng_iface_bpftap(ifp, m, iffam->family);
770 /* Ignore any meta-data */
774 switch (iffam->family) {
792 return (EAFNOSUPPORT);
794 netisr_queue(isr, m);
799 * Shutdown and remove the node and its associated interface.
802 ng_iface_rmnode(node_p node)
804 const priv_p priv = node->private;
808 bpfdetach(priv->ifp);
809 if_detach(priv->ifp);
810 kfree(priv->ifp, M_NETGRAPH);
812 ng_iface_free_unit(priv->unit);
813 kfree(priv, M_NETGRAPH);
814 node->private = NULL;
820 * Hook disconnection. Note that we do *not* shutdown when all
821 * hooks have been disconnected.
824 ng_iface_disconnect(hook_p hook)
826 const priv_p priv = hook->node->private;
827 const iffam_p iffam = get_iffam_from_hook(priv, hook);
831 *get_hook_from_iffam(priv, iffam) = NULL;