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>
74 #include <net/ifq_var.h>
76 #include <net/netisr.h>
78 #include <netinet/in.h>
80 #include <netgraph7/ng_message.h>
81 #include <netgraph7/netgraph.h>
82 #include <netgraph7/ng_parse.h>
83 #include <netgraph7/cisco/ng_cisco.h>
86 #ifdef NG_SEPARATE_MALLOC
87 MALLOC_DEFINE(M_NETGRAPH_IFACE, "netgraph_iface", "netgraph iface node ");
89 #define M_NETGRAPH_IFACE M_NETGRAPH
92 /* This struct describes one address family */
94 sa_family_t family; /* Address family */
95 const char *hookname; /* Name for hook */
97 typedef const struct iffam *iffam_p;
99 /* List of address families supported by our interface */
100 static const struct iffam gFamilies[] = {
101 { AF_INET, NG_IFACE_HOOK_INET },
102 { AF_INET6, NG_IFACE_HOOK_INET6 },
103 { AF_IPX, NG_IFACE_HOOK_IPX },
104 { AF_ATM, NG_IFACE_HOOK_ATM },
105 { AF_NATM, NG_IFACE_HOOK_NATM },
107 #define NUM_FAMILIES NELEM(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, struct ifaltq_subque *);
120 static int ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data,
122 static int ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
123 struct sockaddr *dst, struct rtentry *rt0);
124 static void ng_iface_bpftap(struct ifnet *ifp,
125 struct mbuf *m, sa_family_t family);
126 static int ng_iface_send(struct ifnet *ifp, struct mbuf *m,
129 static void ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
132 /* Netgraph methods */
133 static int ng_iface_mod_event(module_t, int, void *);
134 static ng_constructor_t ng_iface_constructor;
135 static ng_rcvmsg_t ng_iface_rcvmsg;
136 static ng_shutdown_t ng_iface_shutdown;
137 static ng_newhook_t ng_iface_newhook;
138 static ng_rcvdata_t ng_iface_rcvdata;
139 static ng_disconnect_t ng_iface_disconnect;
142 static iffam_p get_iffam_from_af(sa_family_t family);
143 static iffam_p get_iffam_from_hook(priv_p priv, hook_p hook);
144 static iffam_p get_iffam_from_name(const char *name);
145 static hook_p *get_hook_from_iffam(priv_p priv, iffam_p iffam);
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_parse_string_type
166 NGM_IFACE_POINT2POINT,
180 NGM_CISCO_GET_IPADDR,
183 &ng_cisco_ipaddr_type
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 /* We keep a bitmap indicating which unit numbers are free.
211 One means the unit number is free, zero means it's taken. */
212 static int *ng_iface_units = NULL;
213 static int ng_iface_units_len = 0;
215 #define UNITS_BITSPERWORD (sizeof(*ng_iface_units) * NBBY)
218 /************************************************************************
220 ************************************************************************/
223 * Get the family descriptor from the family ID
225 static __inline iffam_p
226 get_iffam_from_af(sa_family_t family)
231 for (k = 0; k < NUM_FAMILIES; k++) {
232 iffam = &gFamilies[k];
233 if (iffam->family == family)
240 * Get the family descriptor from the hook
242 static __inline iffam_p
243 get_iffam_from_hook(priv_p priv, hook_p hook)
247 for (k = 0; k < NUM_FAMILIES; k++)
248 if (priv->hooks[k] == hook)
249 return (&gFamilies[k]);
254 * Get the hook from the iffam descriptor
257 static __inline hook_p *
258 get_hook_from_iffam(priv_p priv, iffam_p iffam)
260 return (&priv->hooks[iffam - gFamilies]);
264 * Get the iffam descriptor from the name
266 static __inline iffam_p
267 get_iffam_from_name(const char *name)
272 for (k = 0; k < NUM_FAMILIES; k++) {
273 iffam = &gFamilies[k];
274 if (!strcmp(iffam->hookname, name))
281 * Find the first free unit number for a new interface.
282 * Increase the size of the unit bitmap as necessary.
284 static __inline__ int
285 ng_iface_get_unit(int *unit)
289 for (index = 0; index < ng_iface_units_len
290 && ng_iface_units[index] == 0; index++);
291 if (index == ng_iface_units_len) { /* extend array */
292 int i, *newarray, newlen;
294 newlen = (2 * ng_iface_units_len) + 4;
295 newarray = kmalloc(newlen * sizeof(*ng_iface_units),
296 M_NETGRAPH_IFACE, M_NOWAIT);
297 if (newarray == NULL)
299 bcopy(ng_iface_units, newarray,
300 ng_iface_units_len * sizeof(*ng_iface_units));
301 for (i = ng_iface_units_len; i < newlen; i++)
303 if (ng_iface_units != NULL)
304 kfree(ng_iface_units, M_NETGRAPH_IFACE);
305 ng_iface_units = newarray;
306 ng_iface_units_len = newlen;
308 bit = ffs(ng_iface_units[index]) - 1;
309 KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
310 ("%s: word=%d bit=%d", __func__, ng_iface_units[index], bit));
311 ng_iface_units[index] &= ~(1 << bit);
312 *unit = (index * UNITS_BITSPERWORD) + bit;
317 * Free a no longer needed unit number.
319 static __inline__ void
320 ng_iface_free_unit(int unit)
324 index = unit / UNITS_BITSPERWORD;
325 bit = unit % UNITS_BITSPERWORD;
326 KASSERT(index < ng_iface_units_len,
327 ("%s: unit=%d len=%d", __func__, unit, ng_iface_units_len));
328 KASSERT((ng_iface_units[index] & (1 << bit)) == 0,
329 ("%s: unit=%d is free", __func__, unit));
330 ng_iface_units[index] |= (1 << bit);
332 * XXX We could think about reducing the size of ng_iface_units[]
333 * XXX here if the last portion is all ones
337 /************************************************************************
339 ************************************************************************/
342 * Process an ioctl for the virtual interface
345 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data,
348 struct ifreq *const ifr = (struct ifreq *) data;
352 ng_iface_print_ioctl(ifp, command, data);
357 /* These two are mostly handled at a higher layer */
359 ifp->if_flags |= IFF_UP;
360 ifp->if_flags |= IFF_RUNNING;
361 ifq_clr_oactive(&ifp->if_snd);
369 * If the interface is marked up and stopped, then start it.
370 * If it is marked down and running, then stop it.
372 if (ifr->ifr_flags & IFF_UP) {
373 if (!(ifp->if_flags & IFF_RUNNING)) {
374 ifq_clr_oactive(&ifp->if_snd);
375 ifp->if_flags |= IFF_RUNNING;
378 if (ifp->if_flags & IFF_RUNNING) {
379 ifp->if_flags &= ~IFF_RUNNING;
380 ifq_clr_oactive(&ifp->if_snd);
385 /* Set the interface MTU */
387 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
388 || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
391 ifp->if_mtu = ifr->ifr_mtu;
394 /* Stuff that's not supported */
412 * This routine is called to deliver a packet out the interface.
413 * We simply look at the address family and relay the packet to
414 * the corresponding hook, if it exists and is connected.
418 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
419 struct sockaddr *dst, struct rtentry *rt0)
424 /* Check interface flags */
425 if (!((ifp->if_flags & IFF_UP) && (ifp->if_flags & IFF_RUNNING))) {
430 /* BPF writes need to be handled specially. */
431 if (dst->sa_family == AF_UNSPEC) {
432 bcopy(dst->sa_data, &af, sizeof(af));
436 /* Berkeley packet filter */
437 ng_iface_bpftap(ifp, m, dst->sa_family);
439 if (ifq_is_enabled(&ifp->if_snd)) {
440 M_PREPEND(m, sizeof(sa_family_t), MB_DONTWAIT);
442 IFNET_STAT_INC(ifp, oerrors, 1);
445 *(sa_family_t *)m->m_data = dst->sa_family;
446 error = ifq_dispatch(ifp, m, NULL);
448 error = ng_iface_send(ifp, m, dst->sa_family);
454 * Start method is used only when ALTQ is enabled.
457 ng_iface_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
461 KASSERT(ifq_is_enabled(&ifp->if_snd), ("%s without ALTQ", __func__));
466 m = ifq_dequeue(&ifp->if_snd);
469 sa = *mtod(m, sa_family_t *);
470 m_adj(m, sizeof(sa_family_t));
471 ng_iface_send(ifp, m, sa);
476 * Flash a packet by the BPF (requires prepending 4 byte AF header)
477 * Note the phoney mbuf; this is OK because BPF treats it read-only.
480 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
482 int32_t family4 = (int32_t)family;
484 KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
489 bpf_ptap(ifp->if_bpf, m, &family4, sizeof(family4));
495 * This routine does actual delivery of the packet into the
496 * netgraph(4). It is called from ng_iface_start() and
500 ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa)
502 const priv_p priv = (priv_p) ifp->if_softc;
503 const iffam_p iffam = get_iffam_from_af(sa);
507 /* Check address family to determine hook (if known) */
510 log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa);
511 return (EAFNOSUPPORT);
514 /* Copy length before the mbuf gets invalidated. */
515 len = m->m_pkthdr.len;
517 /* Send packet. If hook is not connected,
518 mbuf will get freed. */
519 NG_SEND_DATA_ONLY(error, *get_hook_from_iffam(priv, iffam), m);
523 IFNET_STAT_INC(ifp, obytes, len);
524 IFNET_STAT_INC(ifp, opackets, 1);
532 * Display an ioctl to the virtual interface
536 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
540 switch (command & IOC_DIRMASK) {
556 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
561 IOCPARM_LEN(command));
565 /************************************************************************
567 ************************************************************************/
570 * Constructor for a node
573 ng_iface_constructor(node_p node)
579 /* Allocate node and interface private structures */
580 priv = kmalloc(sizeof(*priv), M_NETGRAPH_IFACE,
581 M_WAITOK | M_NULLOK | M_ZERO);
584 ifp = kmalloc(sizeof(*ifp), M_NETGRAPH_IFACE, M_WAITOK | M_NULLOK | M_ZERO);
586 kfree(priv, M_NETGRAPH_IFACE);
590 /* Link them together */
591 ifp->if_softc = priv;
594 /* Get an interface unit number */
595 if ((error = ng_iface_get_unit(&priv->unit)) != 0) {
596 kfree(ifp, M_NETGRAPH_IFACE);
597 kfree(priv, M_NETGRAPH_IFACE);
602 /* Link together node and private info */
603 NG_NODE_SET_PRIVATE(node, priv);
606 /* Initialize interface structure */
607 if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
608 ifp->if_output = ng_iface_output;
609 ifp->if_start = ng_iface_start;
610 ifp->if_ioctl = ng_iface_ioctl;
611 ifp->if_watchdog = NULL;
612 ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
613 ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
614 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */
615 ifp->if_addrlen = 0; /* XXX */
616 ifp->if_hdrlen = 0; /* XXX */
617 ifp->if_baudrate = 64000; /* XXX */
618 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
619 ifq_set_ready(&ifp->if_snd);
621 /* Give this node the same name as the interface (if possible) */
622 if (ng_name_node(node, ifp->if_xname) != 0)
623 log(LOG_WARNING, "%s: can't acquire netgraph name\n",
626 /* Attach the interface */
627 if_attach(ifp, NULL);
628 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
635 * Give our ok for a hook to be added
638 ng_iface_newhook(node_p node, hook_p hook, const char *name)
640 const iffam_p iffam = get_iffam_from_name(name);
644 return (EPFNOSUPPORT);
645 hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam);
646 if (*hookptr != NULL)
649 NG_HOOK_HI_STACK(hook);
654 * Receive a control message
657 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook)
659 const priv_p priv = NG_NODE_PRIVATE(node);
660 struct ifnet *const ifp = priv->ifp;
661 struct ng_mesg *resp = NULL;
665 NGI_GET_MSG(item, msg);
666 switch (msg->header.typecookie) {
667 case NGM_IFACE_COOKIE:
668 switch (msg->header.cmd) {
669 case NGM_IFACE_GET_IFNAME:
670 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_WAITOK | M_NULLOK);
675 strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
678 case NGM_IFACE_POINT2POINT:
679 case NGM_IFACE_BROADCAST:
682 /* Deny request if interface is UP */
683 if ((ifp->if_flags & IFF_UP) != 0)
687 switch (msg->header.cmd) {
688 case NGM_IFACE_POINT2POINT:
689 ifp->if_flags |= IFF_POINTOPOINT;
690 ifp->if_flags &= ~IFF_BROADCAST;
692 case NGM_IFACE_BROADCAST:
693 ifp->if_flags &= ~IFF_POINTOPOINT;
694 ifp->if_flags |= IFF_BROADCAST;
700 case NGM_IFACE_GET_IFINDEX:
701 NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_WAITOK | M_NULLOK);
706 *((uint32_t *)resp->data) = priv->ifp->if_index;
714 case NGM_CISCO_COOKIE:
715 switch (msg->header.cmd) {
716 case NGM_CISCO_GET_IPADDR: /* we understand this too */
718 struct ifaddr_container *ifac;
720 /* Return the first configured IP address */
721 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
722 struct ifaddr *ifa = ifac->ifa;
723 struct ng_cisco_ipaddr *ips;
725 if (ifa->ifa_addr->sa_family != AF_INET)
727 NG_MKRESPONSE(resp, msg, sizeof(ips), M_WAITOK | M_NULLOK);
732 ips = (struct ng_cisco_ipaddr *)resp->data;
733 ips->ipaddr = ((struct sockaddr_in *)
734 ifa->ifa_addr)->sin_addr;
735 ips->netmask = ((struct sockaddr_in *)
736 ifa->ifa_netmask)->sin_addr;
740 /* No IP addresses on this interface? */
742 error = EADDRNOTAVAIL;
750 case NGM_FLOW_COOKIE:
751 switch (msg->header.cmd) {
753 ifp->if_flags |= IFF_RUNNING;
755 case NGM_LINK_IS_DOWN:
756 ifp->if_flags &= ~IFF_RUNNING;
766 NG_RESPOND_MSG(error, node, item, resp);
772 * Recive data from a hook. Pass the packet to the correct input routine.
775 ng_iface_rcvdata(hook_p hook, item_p item)
777 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
778 const iffam_p iffam = get_iffam_from_hook(priv, hook);
779 struct ifnet *const ifp = priv->ifp;
786 KASSERT(iffam != NULL, ("%s: iffam", __func__));
788 if ((ifp->if_flags & IFF_UP) == 0) {
793 /* Update interface stats */
794 IFNET_STAT_INC(ifp, ipackets, 1);
795 IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len);
797 /* Note receiving interface */
798 m->m_pkthdr.rcvif = ifp;
800 /* Berkeley packet filter */
801 ng_iface_bpftap(ifp, m, iffam->family);
804 switch (iffam->family) {
822 return (EAFNOSUPPORT);
825 /* First chunk of an mbuf contains good junk */
826 if (harvest.point_to_point)
827 random_harvest(m, 16, 3, 0, RANDOM_NET);
829 netisr_queue(isr, m);
834 * Shutdown and remove the node and its associated interface.
837 ng_iface_shutdown(node_p node)
839 const priv_p priv = NG_NODE_PRIVATE(node);
841 bpfdetach(priv->ifp);
842 if_detach(priv->ifp);
843 kfree(priv->ifp, M_NETGRAPH_IFACE);
845 ng_iface_free_unit(priv->unit);
846 kfree(priv, M_NETGRAPH_IFACE);
847 NG_NODE_SET_PRIVATE(node, NULL);
853 * Hook disconnection. Note that we do *not* shutdown when all
854 * hooks have been disconnected.
857 ng_iface_disconnect(hook_p hook)
859 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
860 const iffam_p iffam = get_iffam_from_hook(priv, hook);
864 *get_hook_from_iffam(priv, iffam) = NULL;
869 * Handle loading and unloading for this node type.
872 ng_iface_mod_event(module_t mod, int event, void *data)