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/netisr.h>
76 #include <netinet/in.h>
78 #include <netgraph/ng_message.h>
79 #include <netgraph/netgraph.h>
80 #include <netgraph/ng_parse.h>
82 #include <netgraph/cisco/ng_cisco.h>
84 /* This struct describes one address family */
86 sa_family_t family; /* Address family */
87 const char *hookname; /* Name for hook */
89 typedef const struct iffam *iffam_p;
91 /* List of address families supported by our interface */
92 static const struct iffam gFamilies[] = {
93 { AF_INET, NG_IFACE_HOOK_INET },
94 { AF_INET6, NG_IFACE_HOOK_INET6 },
95 { AF_IPX, NG_IFACE_HOOK_IPX },
96 { AF_ATM, NG_IFACE_HOOK_ATM },
97 { AF_NATM, NG_IFACE_HOOK_NATM },
98 { AF_NS, NG_IFACE_HOOK_NS },
100 #define NUM_FAMILIES NELEM(gFamilies)
102 /* Node private data */
103 struct ng_iface_private {
104 struct ifnet *ifp; /* Our interface */
105 int unit; /* Interface unit number */
106 node_p node; /* Our netgraph node */
107 hook_p hooks[NUM_FAMILIES]; /* Hook for each address family */
109 typedef struct ng_iface_private *priv_p;
111 /* Interface methods */
112 static void ng_iface_start(struct ifnet *ifp);
113 static int ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data,
115 static int ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
116 struct sockaddr *dst, struct rtentry *rt0);
117 static void ng_iface_bpftap(struct ifnet *ifp,
118 struct mbuf *m, sa_family_t family);
120 static void ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
123 /* Netgraph methods */
124 static ng_constructor_t ng_iface_constructor;
125 static ng_rcvmsg_t ng_iface_rcvmsg;
126 static ng_shutdown_t ng_iface_rmnode;
127 static ng_newhook_t ng_iface_newhook;
128 static ng_rcvdata_t ng_iface_rcvdata;
129 static ng_disconnect_t ng_iface_disconnect;
132 static iffam_p get_iffam_from_af(sa_family_t family);
133 static iffam_p get_iffam_from_hook(priv_p priv, hook_p hook);
134 static iffam_p get_iffam_from_name(const char *name);
135 static hook_p *get_hook_from_iffam(priv_p priv, iffam_p iffam);
137 /* Parse type for struct ng_iface_ifname */
138 static const struct ng_parse_fixedstring_info ng_iface_ifname_info = {
139 NG_IFACE_IFACE_NAME_MAX + 1
141 static const struct ng_parse_type ng_iface_ifname_type = {
142 &ng_parse_fixedstring_type,
143 &ng_iface_ifname_info
146 /* Parse type for struct ng_cisco_ipaddr */
147 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
148 = NG_CISCO_IPADDR_TYPE_INFO;
149 static const struct ng_parse_type ng_cisco_ipaddr_type = {
150 &ng_parse_struct_type,
151 &ng_cisco_ipaddr_type_fields
154 /* List of commands and how to convert arguments to/from ASCII */
155 static const struct ng_cmdlist ng_iface_cmds[] = {
158 NGM_IFACE_GET_IFNAME,
161 &ng_iface_ifname_type
165 NGM_IFACE_POINT2POINT,
179 NGM_CISCO_GET_IPADDR,
182 &ng_cisco_ipaddr_type
187 /* Node type descriptor */
188 static struct ng_type typestruct = {
192 ng_iface_constructor,
203 NETGRAPH_INIT(iface, &typestruct);
205 /* We keep a bitmap indicating which unit numbers are free.
206 One means the unit number is free, zero means it's taken. */
207 static int *ng_iface_units = NULL;
208 static int ng_iface_units_len = 0;
210 #define UNITS_BITSPERWORD (sizeof(*ng_iface_units) * NBBY)
212 /************************************************************************
214 ************************************************************************/
217 * Get the family descriptor from the family ID
219 static __inline__ iffam_p
220 get_iffam_from_af(sa_family_t family)
225 for (k = 0; k < NUM_FAMILIES; k++) {
226 iffam = &gFamilies[k];
227 if (iffam->family == family)
234 * Get the family descriptor from the hook
236 static __inline__ iffam_p
237 get_iffam_from_hook(priv_p priv, hook_p hook)
241 for (k = 0; k < NUM_FAMILIES; k++)
242 if (priv->hooks[k] == hook)
243 return (&gFamilies[k]);
248 * Get the hook from the iffam descriptor
251 static __inline__ hook_p *
252 get_hook_from_iffam(priv_p priv, iffam_p iffam)
254 return (&priv->hooks[iffam - gFamilies]);
258 * Get the iffam descriptor from the name
260 static __inline__ iffam_p
261 get_iffam_from_name(const char *name)
266 for (k = 0; k < NUM_FAMILIES; k++) {
267 iffam = &gFamilies[k];
268 if (!strcmp(iffam->hookname, name))
275 * Find the first free unit number for a new interface.
276 * Increase the size of the unit bitmap as necessary.
278 static __inline__ int
279 ng_iface_get_unit(int *unit)
283 for (index = 0; index < ng_iface_units_len
284 && ng_iface_units[index] == 0; index++);
285 if (index == ng_iface_units_len) { /* extend array */
286 int i, *newarray, newlen;
288 newlen = (2 * ng_iface_units_len) + 4;
289 newarray = kmalloc(newlen * sizeof(*ng_iface_units),
290 M_NETGRAPH, M_NOWAIT);
291 if (newarray == NULL)
293 bcopy(ng_iface_units, newarray,
294 ng_iface_units_len * sizeof(*ng_iface_units));
295 for (i = ng_iface_units_len; i < newlen; i++)
297 if (ng_iface_units != NULL)
298 kfree(ng_iface_units, M_NETGRAPH);
299 ng_iface_units = newarray;
300 ng_iface_units_len = newlen;
302 bit = ffs(ng_iface_units[index]) - 1;
303 KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
304 ("%s: word=%d bit=%d", __func__, ng_iface_units[index], bit));
305 ng_iface_units[index] &= ~(1 << bit);
306 *unit = (index * UNITS_BITSPERWORD) + bit;
311 * Free a no longer needed unit number.
313 static __inline__ void
314 ng_iface_free_unit(int unit)
318 index = unit / UNITS_BITSPERWORD;
319 bit = unit % UNITS_BITSPERWORD;
320 KASSERT(index < ng_iface_units_len,
321 ("%s: unit=%d len=%d", __func__, unit, ng_iface_units_len));
322 KASSERT((ng_iface_units[index] & (1 << bit)) == 0,
323 ("%s: unit=%d is free", __func__, unit));
324 ng_iface_units[index] |= (1 << bit);
326 * XXX We could think about reducing the size of ng_iface_units[]
327 * XXX here if the last portion is all ones
331 /************************************************************************
333 ************************************************************************/
336 * Process an ioctl for the virtual interface
339 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data,
342 struct ifreq *const ifr = (struct ifreq *) data;
346 ng_iface_print_ioctl(ifp, command, data);
351 /* These two are mostly handled at a higher layer */
353 ifp->if_flags |= (IFF_UP | IFF_RUNNING);
354 ifp->if_flags &= ~(IFF_OACTIVE);
362 * If the interface is marked up and stopped, then start it.
363 * If it is marked down and running, then stop it.
365 if (ifr->ifr_flags & IFF_UP) {
366 if (!(ifp->if_flags & IFF_RUNNING)) {
367 ifp->if_flags &= ~(IFF_OACTIVE);
368 ifp->if_flags |= IFF_RUNNING;
371 if (ifp->if_flags & IFF_RUNNING)
372 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
376 /* Set the interface MTU */
378 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
379 || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
382 ifp->if_mtu = ifr->ifr_mtu;
385 /* Stuff that's not supported */
403 * This routine is called to deliver a packet out the interface.
404 * We simply look at the address family and relay the packet to
405 * the corresponding hook, if it exists and is connected.
409 ng_iface_output_serialized(struct ifnet *ifp, struct mbuf *m,
410 struct sockaddr *dst, struct rtentry *rt0)
412 const priv_p priv = (priv_p) ifp->if_softc;
413 const iffam_p iffam = get_iffam_from_af(dst->sa_family);
417 /* Check interface flags */
418 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
423 /* BPF writes need to be handled specially */
424 if (dst->sa_family == AF_UNSPEC) {
425 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL)
427 dst->sa_family = (sa_family_t)*mtod(m, int32_t *);
430 m->m_pkthdr.len -= 4;
433 /* Berkeley packet filter */
434 ng_iface_bpftap(ifp, m, dst->sa_family);
436 /* Check address family to determine hook (if known) */
439 log(LOG_WARNING, "%s: can't handle af%d\n",
440 ifp->if_xname, (int)dst->sa_family);
441 return (EAFNOSUPPORT);
444 /* Copy length before the mbuf gets invalidated */
445 len = m->m_pkthdr.len;
447 /* Send packet; if hook is not connected, mbuf will get freed. */
448 NG_SEND_DATA(error, *get_hook_from_iffam(priv, iffam), m, meta);
452 ifp->if_obytes += len;
459 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
460 struct sockaddr *dst, struct rtentry *rt0)
464 ifnet_serialize_tx(ifp);
465 error = ng_iface_output_serialized(ifp, m, dst, rt0);
466 ifnet_deserialize_tx(ifp);
472 * This routine should never be called
476 ng_iface_start(struct ifnet *ifp)
478 kprintf("%s: %s called?", ifp->if_xname, __func__);
482 * Flash a packet by the BPF (requires prepending 4 byte AF header)
485 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
487 int32_t family4 = (int32_t)family;
489 KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
492 bpf_ptap(ifp->if_bpf, m, &family, sizeof(family4));
497 * Display an ioctl to the virtual interface
501 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
505 switch (command & IOC_DIRMASK) {
521 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
526 IOCPARM_LEN(command));
530 /************************************************************************
532 ************************************************************************/
535 * Constructor for a node
538 ng_iface_constructor(node_p *nodep)
540 char ifname[NG_IFACE_IFACE_NAME_MAX + 1];
546 /* Allocate node and interface private structures */
547 priv = kmalloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
550 ifp = kmalloc(sizeof(*ifp), M_NETGRAPH, M_NOWAIT | M_ZERO);
552 kfree(priv, M_NETGRAPH);
556 /* Link them together */
557 ifp->if_softc = priv;
560 /* Get an interface unit number */
561 if ((error = ng_iface_get_unit(&priv->unit)) != 0) {
562 kfree(ifp, M_NETGRAPH);
563 kfree(priv, M_NETGRAPH);
567 /* Call generic node constructor */
568 if ((error = ng_make_node_common(&typestruct, nodep)) != 0) {
569 ng_iface_free_unit(priv->unit);
570 kfree(ifp, M_NETGRAPH);
571 kfree(priv, M_NETGRAPH);
576 /* Link together node and private info */
577 node->private = priv;
580 /* Initialize interface structure */
581 if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
582 ifp->if_output = ng_iface_output;
583 ifp->if_start = ng_iface_start;
584 ifp->if_ioctl = ng_iface_ioctl;
585 ifp->if_watchdog = NULL;
586 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
587 ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
588 ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
589 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */
590 ifp->if_addrlen = 0; /* XXX */
591 ifp->if_hdrlen = 0; /* XXX */
592 ifp->if_baudrate = 64000; /* XXX */
594 /* Give this node the same name as the interface (if possible) */
595 bzero(ifname, sizeof(ifname));
596 strlcpy(ifname, ifp->if_xname, sizeof(ifname));
597 if (ng_name_node(node, ifname) != 0)
598 log(LOG_WARNING, "%s: can't acquire netgraph name\n", ifname);
600 /* Attach the interface */
601 if_attach(ifp, NULL);
602 bpfattach(ifp, DLT_NULL, sizeof(u_int));
609 * Give our ok for a hook to be added
612 ng_iface_newhook(node_p node, hook_p hook, const char *name)
614 const iffam_p iffam = get_iffam_from_name(name);
618 return (EPFNOSUPPORT);
619 hookptr = get_hook_from_iffam((priv_p) node->private, iffam);
620 if (*hookptr != NULL)
627 * Receive a control message
630 ng_iface_rcvmsg(node_p node, struct ng_mesg *msg,
631 const char *retaddr, struct ng_mesg **rptr)
633 const priv_p priv = node->private;
634 struct ifnet *const ifp = priv->ifp;
635 struct ng_mesg *resp = NULL;
638 switch (msg->header.typecookie) {
639 case NGM_IFACE_COOKIE:
640 switch (msg->header.cmd) {
641 case NGM_IFACE_GET_IFNAME:
643 struct ng_iface_ifname *arg;
645 NG_MKRESPONSE(resp, msg, sizeof(*arg), M_NOWAIT);
650 arg = (struct ng_iface_ifname *)resp->data;
651 strlcpy(arg->ngif_name, ifp->if_xname,
652 sizeof(arg->ngif_name));
656 case NGM_IFACE_POINT2POINT:
657 case NGM_IFACE_BROADCAST:
660 /* Deny request if interface is UP */
661 if ((ifp->if_flags & IFF_UP) != 0)
665 switch (msg->header.cmd) {
666 case NGM_IFACE_POINT2POINT:
667 ifp->if_flags |= IFF_POINTOPOINT;
668 ifp->if_flags &= ~IFF_BROADCAST;
670 case NGM_IFACE_BROADCAST:
671 ifp->if_flags &= ~IFF_POINTOPOINT;
672 ifp->if_flags |= IFF_BROADCAST;
683 case NGM_CISCO_COOKIE:
684 switch (msg->header.cmd) {
685 case NGM_CISCO_GET_IPADDR: /* we understand this too */
687 struct ifaddr_container *ifac;
689 /* Return the first configured IP address */
690 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
692 struct ifaddr *ifa = ifac->ifa;
693 struct ng_cisco_ipaddr *ips;
695 if (ifa->ifa_addr->sa_family != AF_INET)
697 NG_MKRESPONSE(resp, msg, sizeof(ips), M_NOWAIT);
702 ips = (struct ng_cisco_ipaddr *)resp->data;
703 ips->ipaddr = ((struct sockaddr_in *)
704 ifa->ifa_addr)->sin_addr;
705 ips->netmask = ((struct sockaddr_in *)
706 ifa->ifa_netmask)->sin_addr;
710 /* No IP addresses on this interface? */
712 error = EADDRNOTAVAIL;
727 kfree(resp, M_NETGRAPH);
728 kfree(msg, M_NETGRAPH);
733 * Recive data from a hook. Pass the packet to the correct input routine.
736 ng_iface_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
738 const priv_p priv = hook->node->private;
739 const iffam_p iffam = get_iffam_from_hook(priv, hook);
740 struct ifnet *const ifp = priv->ifp;
744 KASSERT(iffam != NULL, ("%s: iffam", __func__));
745 KASSERT(m->m_flags & M_PKTHDR, ("%s: not pkthdr", __func__));
748 if ((ifp->if_flags & IFF_UP) == 0) {
749 NG_FREE_DATA(m, meta);
753 /* Update interface stats */
755 ifp->if_ibytes += m->m_pkthdr.len;
757 /* Note receiving interface */
758 m->m_pkthdr.rcvif = ifp;
760 /* Berkeley packet filter */
761 ng_iface_bpftap(ifp, m, iffam->family);
763 /* Ignore any meta-data */
767 switch (iffam->family) {
785 return (EAFNOSUPPORT);
787 netisr_queue(isr, m);
792 * Shutdown and remove the node and its associated interface.
795 ng_iface_rmnode(node_p node)
797 const priv_p priv = node->private;
801 bpfdetach(priv->ifp);
802 if_detach(priv->ifp);
803 kfree(priv->ifp, M_NETGRAPH);
805 ng_iface_free_unit(priv->unit);
806 kfree(priv, M_NETGRAPH);
807 node->private = NULL;
813 * Hook disconnection. Note that we do *not* shutdown when all
814 * hooks have been disconnected.
817 ng_iface_disconnect(hook_p hook)
819 const priv_p priv = hook->node->private;
820 const iffam_p iffam = get_iffam_from_hook(priv, hook);
824 *get_hook_from_iffam(priv, iffam) = NULL;