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.
55 #include "opt_atalk.h"
57 #include "opt_inet6.h"
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/errno.h>
63 #include <sys/kernel.h>
64 #include <sys/malloc.h>
66 #include <sys/errno.h>
67 #include <sys/random.h>
68 #include <sys/sockio.h>
69 #include <sys/socket.h>
70 #include <sys/syslog.h>
71 #include <sys/libkern.h>
74 #include <net/if_types.h>
76 #include <net/netisr.h>
78 #include <netinet/in.h>
80 #include <netgraph/ng_message.h>
81 #include <netgraph/netgraph.h>
82 #include <netgraph/ng_parse.h>
83 #include <netgraph/ng_iface.h>
84 #include <netgraph/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 const static struct iffam gFamilies
[] = {
101 { AF_INET
, NG_IFACE_HOOK_INET
},
102 { AF_INET6
, NG_IFACE_HOOK_INET6
},
103 { AF_APPLETALK
, NG_IFACE_HOOK_ATALK
},
104 { AF_IPX
, NG_IFACE_HOOK_IPX
},
105 { AF_ATM
, NG_IFACE_HOOK_ATM
},
106 { AF_NATM
, NG_IFACE_HOOK_NATM
},
108 #define NUM_FAMILIES (sizeof(gFamilies) / sizeof(*gFamilies))
110 /* Node private data */
111 struct ng_iface_private
{
112 struct ifnet
*ifp
; /* Our interface */
113 int unit
; /* Interface unit number */
114 node_p node
; /* Our netgraph node */
115 hook_p hooks
[NUM_FAMILIES
]; /* Hook for each address family */
117 typedef struct ng_iface_private
*priv_p
;
119 /* Interface methods */
120 static void ng_iface_start(struct ifnet
*ifp
);
121 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 static struct unrhdr
*ng_iface_unit
;
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
))
274 /************************************************************************
276 ************************************************************************/
279 * Process an ioctl for the virtual interface
282 ng_iface_ioctl(struct ifnet
*ifp
, u_long command
, caddr_t data
)
284 struct ifreq
*const ifr
= (struct ifreq
*) data
;
288 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 */
347 * This routine is called to deliver a packet out the interface.
348 * We simply look at the address family and relay the packet to
349 * the corresponding hook, if it exists and is connected.
353 ng_iface_output(struct ifnet
*ifp
, struct mbuf
*m
,
354 struct sockaddr
*dst
, struct rtentry
*rt0
)
359 /* Check interface flags */
360 if (!((ifp
->if_flags
& IFF_UP
) &&
361 (ifp
->if_drv_flags
& IFF_DRV_RUNNING
))) {
366 /* BPF writes need to be handled specially. */
367 if (dst
->sa_family
== AF_UNSPEC
) {
368 bcopy(dst
->sa_data
, &af
, sizeof(af
));
372 /* Berkeley packet filter */
373 ng_iface_bpftap(ifp
, m
, dst
->sa_family
);
375 if (ALTQ_IS_ENABLED(&ifp
->if_snd
)) {
376 M_PREPEND(m
, sizeof(sa_family_t
), M_DONTWAIT
);
378 IFQ_LOCK(&ifp
->if_snd
);
379 IFQ_INC_DROPS(&ifp
->if_snd
);
380 IFQ_UNLOCK(&ifp
->if_snd
);
384 *(sa_family_t
*)m
->m_data
= dst
->sa_family
;
385 IFQ_HANDOFF(ifp
, m
, error
);
387 error
= ng_iface_send(ifp
, m
, dst
->sa_family
);
393 * Start method is used only when ALTQ is enabled.
396 ng_iface_start(struct ifnet
*ifp
)
401 KASSERT(ALTQ_IS_ENABLED(&ifp
->if_snd
), ("%s without ALTQ", __func__
));
404 IFQ_DRV_DEQUEUE(&ifp
->if_snd
, m
);
407 sa
= *mtod(m
, sa_family_t
*);
408 m_adj(m
, sizeof(sa_family_t
));
409 ng_iface_send(ifp
, m
, sa
);
414 * Flash a packet by the BPF (requires prepending 4 byte AF header)
415 * Note the phoney mbuf; this is OK because BPF treats it read-only.
418 ng_iface_bpftap(struct ifnet
*ifp
, struct mbuf
*m
, sa_family_t family
)
420 KASSERT(family
!= AF_UNSPEC
, ("%s: family=AF_UNSPEC", __func__
));
421 if (bpf_peers_present(ifp
->if_bpf
)) {
422 int32_t family4
= (int32_t)family
;
423 bpf_mtap2(ifp
->if_bpf
, &family4
, sizeof(family4
), m
);
428 * This routine does actual delivery of the packet into the
429 * netgraph(4). It is called from ng_iface_start() and
433 ng_iface_send(struct ifnet
*ifp
, struct mbuf
*m
, sa_family_t sa
)
435 const priv_p priv
= (priv_p
) ifp
->if_softc
;
436 const iffam_p iffam
= get_iffam_from_af(sa
);
440 /* Check address family to determine hook (if known) */
443 log(LOG_WARNING
, "%s: can't handle af%d\n", ifp
->if_xname
, sa
);
444 return (EAFNOSUPPORT
);
447 /* Copy length before the mbuf gets invalidated. */
448 len
= m
->m_pkthdr
.len
;
450 /* Send packet. If hook is not connected,
451 mbuf will get freed. */
452 NG_SEND_DATA_ONLY(error
, *get_hook_from_iffam(priv
, iffam
), m
);
456 ifp
->if_obytes
+= len
;
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 MALLOC(priv
, priv_p
, sizeof(*priv
), M_NETGRAPH_IFACE
, M_NOWAIT
|M_ZERO
);
515 ifp
= if_alloc(IFT_PROPVIRTUAL
);
517 FREE(priv
, M_NETGRAPH_IFACE
);
521 /* Link them together */
522 ifp
->if_softc
= priv
;
525 /* Get an interface unit number */
526 priv
->unit
= alloc_unr(ng_iface_unit
);
528 /* Link together node and private info */
529 NG_NODE_SET_PRIVATE(node
, priv
);
532 /* Initialize interface structure */
533 if_initname(ifp
, NG_IFACE_IFACE_NAME
, priv
->unit
);
534 ifp
->if_output
= ng_iface_output
;
535 ifp
->if_start
= ng_iface_start
;
536 ifp
->if_ioctl
= ng_iface_ioctl
;
537 ifp
->if_watchdog
= NULL
;
538 ifp
->if_mtu
= NG_IFACE_MTU_DEFAULT
;
539 ifp
->if_flags
= (IFF_SIMPLEX
|IFF_POINTOPOINT
|IFF_NOARP
|IFF_MULTICAST
);
540 ifp
->if_type
= IFT_PROPVIRTUAL
; /* XXX */
541 ifp
->if_addrlen
= 0; /* XXX */
542 ifp
->if_hdrlen
= 0; /* XXX */
543 ifp
->if_baudrate
= 64000; /* XXX */
544 IFQ_SET_MAXLEN(&ifp
->if_snd
, IFQ_MAXLEN
);
545 ifp
->if_snd
.ifq_drv_maxlen
= IFQ_MAXLEN
;
546 IFQ_SET_READY(&ifp
->if_snd
);
548 /* Give this node the same name as the interface (if possible) */
549 if (ng_name_node(node
, ifp
->if_xname
) != 0)
550 log(LOG_WARNING
, "%s: can't acquire netgraph name\n",
553 /* Attach the interface */
555 bpfattach(ifp
, DLT_NULL
, sizeof(u_int32_t
));
562 * Give our ok for a hook to be added
565 ng_iface_newhook(node_p node
, hook_p hook
, const char *name
)
567 const iffam_p iffam
= get_iffam_from_name(name
);
571 return (EPFNOSUPPORT
);
572 hookptr
= get_hook_from_iffam(NG_NODE_PRIVATE(node
), iffam
);
573 if (*hookptr
!= NULL
)
576 NG_HOOK_HI_STACK(hook
);
581 * Receive a control message
584 ng_iface_rcvmsg(node_p node
, item_p item
, hook_p lasthook
)
586 const priv_p priv
= NG_NODE_PRIVATE(node
);
587 struct ifnet
*const ifp
= priv
->ifp
;
588 struct ng_mesg
*resp
= NULL
;
592 NGI_GET_MSG(item
, msg
);
593 switch (msg
->header
.typecookie
) {
594 case NGM_IFACE_COOKIE
:
595 switch (msg
->header
.cmd
) {
596 case NGM_IFACE_GET_IFNAME
:
597 NG_MKRESPONSE(resp
, msg
, IFNAMSIZ
, M_NOWAIT
);
602 strlcpy(resp
->data
, ifp
->if_xname
, IFNAMSIZ
);
605 case NGM_IFACE_POINT2POINT
:
606 case NGM_IFACE_BROADCAST
:
609 /* Deny request if interface is UP */
610 if ((ifp
->if_flags
& IFF_UP
) != 0)
614 switch (msg
->header
.cmd
) {
615 case NGM_IFACE_POINT2POINT
:
616 ifp
->if_flags
|= IFF_POINTOPOINT
;
617 ifp
->if_flags
&= ~IFF_BROADCAST
;
619 case NGM_IFACE_BROADCAST
:
620 ifp
->if_flags
&= ~IFF_POINTOPOINT
;
621 ifp
->if_flags
|= IFF_BROADCAST
;
627 case NGM_IFACE_GET_IFINDEX
:
628 NG_MKRESPONSE(resp
, msg
, sizeof(uint32_t), M_NOWAIT
);
633 *((uint32_t *)resp
->data
) = priv
->ifp
->if_index
;
641 case NGM_CISCO_COOKIE
:
642 switch (msg
->header
.cmd
) {
643 case NGM_CISCO_GET_IPADDR
: /* we understand this too */
647 /* Return the first configured IP address */
648 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
649 struct ng_cisco_ipaddr
*ips
;
651 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
653 NG_MKRESPONSE(resp
, msg
, sizeof(ips
), M_NOWAIT
);
658 ips
= (struct ng_cisco_ipaddr
*)resp
->data
;
659 ips
->ipaddr
= ((struct sockaddr_in
*)
660 ifa
->ifa_addr
)->sin_addr
;
661 ips
->netmask
= ((struct sockaddr_in
*)
662 ifa
->ifa_netmask
)->sin_addr
;
666 /* No IP addresses on this interface? */
668 error
= EADDRNOTAVAIL
;
676 case NGM_FLOW_COOKIE
:
677 switch (msg
->header
.cmd
) {
679 ifp
->if_drv_flags
|= IFF_DRV_RUNNING
;
681 case NGM_LINK_IS_DOWN
:
682 ifp
->if_drv_flags
&= ~IFF_DRV_RUNNING
;
692 NG_RESPOND_MSG(error
, node
, item
, resp
);
698 * Recive data from a hook. Pass the packet to the correct input routine.
701 ng_iface_rcvdata(hook_p hook
, item_p item
)
703 const priv_p priv
= NG_NODE_PRIVATE(NG_HOOK_NODE(hook
));
704 const iffam_p iffam
= get_iffam_from_hook(priv
, hook
);
705 struct ifnet
*const ifp
= priv
->ifp
;
712 KASSERT(iffam
!= NULL
, ("%s: iffam", __func__
));
714 if ((ifp
->if_flags
& IFF_UP
) == 0) {
719 /* Update interface stats */
721 ifp
->if_ibytes
+= m
->m_pkthdr
.len
;
723 /* Note receiving interface */
724 m
->m_pkthdr
.rcvif
= ifp
;
726 /* Berkeley packet filter */
727 ng_iface_bpftap(ifp
, m
, iffam
->family
);
730 switch (iffam
->family
) {
753 return (EAFNOSUPPORT
);
755 /* First chunk of an mbuf contains good junk */
756 if (harvest
.point_to_point
)
757 random_harvest(m
, 16, 3, 0, RANDOM_NET
);
758 netisr_dispatch(isr
, m
);
763 * Shutdown and remove the node and its associated interface.
766 ng_iface_shutdown(node_p node
)
768 const priv_p priv
= NG_NODE_PRIVATE(node
);
770 bpfdetach(priv
->ifp
);
771 if_detach(priv
->ifp
);
774 free_unr(ng_iface_unit
, priv
->unit
);
775 FREE(priv
, M_NETGRAPH_IFACE
);
776 NG_NODE_SET_PRIVATE(node
, NULL
);
782 * Hook disconnection. Note that we do *not* shutdown when all
783 * hooks have been disconnected.
786 ng_iface_disconnect(hook_p hook
)
788 const priv_p priv
= NG_NODE_PRIVATE(NG_HOOK_NODE(hook
));
789 const iffam_p iffam
= get_iffam_from_hook(priv
, hook
);
793 *get_hook_from_iffam(priv
, iffam
) = NULL
;
798 * Handle loading and unloading for this node type.
801 ng_iface_mod_event(module_t mod
, int event
, void *data
)
807 ng_iface_unit
= new_unrhdr(0, 0xffff, NULL
);
810 delete_unrhdr(ng_iface_unit
);