3 * Copyright (c) 1999-2001, Vitaly V Belekhov
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $FreeBSD: src/sys/netgraph/ng_eiface.c,v 1.39 2007/07/26 10:54:33 glebius Exp $
29 * $DragonFly: src/sys/netgraph7/ng_eiface.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/errno.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
38 #include <sys/errno.h>
39 #include <sys/sockio.h>
40 #include <sys/socket.h>
41 #include <sys/syslog.h>
44 #include <net/if_types.h>
45 #include <net/netisr.h>
47 #include "ng_message.h"
50 #include "ng_eiface.h"
53 #include <net/ethernet.h>
54 #include <net/if_arp.h>
56 static const struct ng_cmdlist ng_eiface_cmdlist
[] = {
59 NGM_EIFACE_GET_IFNAME
,
68 &ng_parse_enaddr_type
,
74 /* Node private data */
75 struct ng_eiface_private
{
76 struct ifnet
*ifp
; /* per-interface network data */
77 int unit
; /* Interface unit number */
78 node_p node
; /* Our netgraph node */
79 hook_p ether
; /* Hook for ethernet stream */
81 typedef struct ng_eiface_private
*priv_p
;
83 /* Interface methods */
84 static void ng_eiface_init(void *xsc
);
85 static void ng_eiface_start(struct ifnet
*ifp
);
86 static int ng_eiface_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
);
88 static void ng_eiface_print_ioctl(struct ifnet
*ifp
, int cmd
, caddr_t data
);
91 /* Netgraph methods */
92 static int ng_eiface_mod_event(module_t
, int, void *);
93 static ng_constructor_t ng_eiface_constructor
;
94 static ng_rcvmsg_t ng_eiface_rcvmsg
;
95 static ng_shutdown_t ng_eiface_rmnode
;
96 static ng_newhook_t ng_eiface_newhook
;
97 static ng_rcvdata_t ng_eiface_rcvdata
;
98 static ng_disconnect_t ng_eiface_disconnect
;
100 /* Node type descriptor */
101 static struct ng_type typestruct
= {
102 .version
= NG_ABI_VERSION
,
103 .name
= NG_EIFACE_NODE_TYPE
,
104 .mod_event
= ng_eiface_mod_event
,
105 .constructor
= ng_eiface_constructor
,
106 .rcvmsg
= ng_eiface_rcvmsg
,
107 .shutdown
= ng_eiface_rmnode
,
108 .newhook
= ng_eiface_newhook
,
109 .rcvdata
= ng_eiface_rcvdata
,
110 .disconnect
= ng_eiface_disconnect
,
111 .cmdlist
= ng_eiface_cmdlist
113 NETGRAPH_INIT(eiface
, &typestruct
);
115 static struct unrhdr
*ng_eiface_unit
;
117 /************************************************************************
119 ************************************************************************/
122 * Process an ioctl for the virtual interface
125 ng_eiface_ioctl(struct ifnet
*ifp
, u_long command
, caddr_t data
)
127 struct ifreq
*const ifr
= (struct ifreq
*)data
;
131 ng_eiface_print_ioctl(ifp
, command
, data
);
136 /* These two are mostly handled at a higher layer */
138 error
= ether_ioctl(ifp
, command
, data
);
146 * If the interface is marked up and stopped, then start it.
147 * If it is marked down and running, then stop it.
149 if (ifp
->if_flags
& IFF_UP
) {
150 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
)) {
151 ifp
->if_drv_flags
&= ~(IFF_DRV_OACTIVE
);
152 ifp
->if_drv_flags
|= IFF_DRV_RUNNING
;
155 if (ifp
->if_drv_flags
& IFF_DRV_RUNNING
)
156 ifp
->if_drv_flags
&= ~(IFF_DRV_RUNNING
|
161 /* Set the interface MTU */
163 if (ifr
->ifr_mtu
> NG_EIFACE_MTU_MAX
||
164 ifr
->ifr_mtu
< NG_EIFACE_MTU_MIN
)
167 ifp
->if_mtu
= ifr
->ifr_mtu
;
170 /* Stuff that's not supported */
188 ng_eiface_init(void *xsc
)
191 struct ifnet
*ifp
= sc
->ifp
;
196 ifp
->if_drv_flags
|= IFF_DRV_RUNNING
;
197 ifp
->if_drv_flags
&= ~IFF_DRV_OACTIVE
;
203 * We simply relay the packet to the "ether" hook, if it is connected.
204 * We have been through the netgraph locking and are guaranteed to
205 * be the only code running in this node at this time.
208 ng_eiface_start2(node_p node
, hook_p hook
, void *arg1
, int arg2
)
210 struct ifnet
*ifp
= arg1
;
211 const priv_p priv
= (priv_p
)ifp
->if_softc
;
215 /* Check interface flags */
217 if (!((ifp
->if_flags
& IFF_UP
) &&
218 (ifp
->if_drv_flags
& IFF_DRV_RUNNING
)))
223 * Grab a packet to transmit.
225 IF_DEQUEUE(&ifp
->if_snd
, m
);
227 /* If there's nothing to send, break. */
232 * Berkeley packet filter.
233 * Pass packet to bpf if there is a listener.
234 * XXX is this safe? locking?
238 if (ifp
->if_flags
& IFF_MONITOR
) {
245 * Send packet; if hook is not connected, mbuf will get
248 NG_SEND_DATA_ONLY(error
, priv
->ether
, m
);
257 ifp
->if_drv_flags
&= ~IFF_DRV_OACTIVE
;
263 * This routine is called to deliver a packet out the interface.
264 * We simply queue the netgraph version to be called when netgraph locking
265 * allows it to happen.
266 * Until we know what the rest of the networking code is doing for
267 * locking, we don't know how we will interact with it.
268 * Take comfort from the fact that the ifnet struct is part of our
269 * private info and can't go away while we are queued.
270 * [Though we don't know it is still there now....]
271 * it is possible we don't gain anything from this because
272 * we would like to get the mbuf and queue it as data
273 * somehow, but we can't and if we did would we solve anything?
276 ng_eiface_start(struct ifnet
*ifp
)
279 const priv_p priv
= (priv_p
)ifp
->if_softc
;
281 /* Don't do anything if output is active */
282 if (ifp
->if_drv_flags
& IFF_DRV_OACTIVE
)
285 ifp
->if_drv_flags
|= IFF_DRV_OACTIVE
;
287 if (ng_send_fn(priv
->node
, NULL
, &ng_eiface_start2
, ifp
, 0) != 0)
288 ifp
->if_drv_flags
&= ~IFF_DRV_OACTIVE
;
293 * Display an ioctl to the virtual interface
297 ng_eiface_print_ioctl(struct ifnet
*ifp
, int command
, caddr_t data
)
301 switch (command
& IOC_DIRMASK
) {
317 log(LOG_DEBUG
, "%s: %s('%c', %d, char[%d])\n",
322 IOCPARM_LEN(command
));
326 /************************************************************************
328 ************************************************************************/
331 * Constructor for a node
334 ng_eiface_constructor(node_p node
)
338 u_char eaddr
[6] = {0,0,0,0,0,0};
340 /* Allocate node and interface private structures */
341 MALLOC(priv
, priv_p
, sizeof(*priv
), M_NETGRAPH
, M_WAITOK
| M_NULLOK
| M_ZERO
);
345 ifp
= priv
->ifp
= if_alloc(IFT_ETHER
);
347 kfree(priv
, M_NETGRAPH
);
351 /* Link them together */
352 ifp
->if_softc
= priv
;
354 /* Get an interface unit number */
355 priv
->unit
= alloc_unr(ng_eiface_unit
);
357 /* Link together node and private info */
358 NG_NODE_SET_PRIVATE(node
, priv
);
361 /* Initialize interface structure */
362 if_initname(ifp
, NG_EIFACE_EIFACE_NAME
, priv
->unit
);
363 ifp
->if_init
= ng_eiface_init
;
364 ifp
->if_output
= ether_output
;
365 ifp
->if_start
= ng_eiface_start
;
366 ifp
->if_ioctl
= ng_eiface_ioctl
;
367 ifp
->if_watchdog
= NULL
;
368 ifp
->if_snd
.ifq_maxlen
= IFQ_MAXLEN
;
369 ifp
->if_flags
= (IFF_SIMPLEX
| IFF_BROADCAST
| IFF_MULTICAST
);
372 /* Give this node name */
373 bzero(ifname
, sizeof(ifname
));
374 sprintf(ifname
, "if%s", ifp
->if_xname
);
375 (void)ng_name_node(node
, ifname
);
378 /* Attach the interface */
379 ether_ifattach(ifp
, eaddr
);
386 * Give our ok for a hook to be added
389 ng_eiface_newhook(node_p node
, hook_p hook
, const char *name
)
391 priv_p priv
= NG_NODE_PRIVATE(node
);
392 struct ifnet
*ifp
= priv
->ifp
;
394 if (strcmp(name
, NG_EIFACE_HOOK_ETHER
))
395 return (EPFNOSUPPORT
);
396 if (priv
->ether
!= NULL
)
399 NG_HOOK_SET_PRIVATE(hook
, &priv
->ether
);
401 if_link_state_change(ifp
, LINK_STATE_UP
);
407 * Receive a control message
410 ng_eiface_rcvmsg(node_p node
, item_p item
, hook_p lasthook
)
412 const priv_p priv
= NG_NODE_PRIVATE(node
);
413 struct ifnet
*const ifp
= priv
->ifp
;
414 struct ng_mesg
*resp
= NULL
;
418 NGI_GET_MSG(item
, msg
);
419 switch (msg
->header
.typecookie
) {
420 case NGM_EIFACE_COOKIE
:
421 switch (msg
->header
.cmd
) {
425 if (msg
->header
.arglen
!= ETHER_ADDR_LEN
) {
429 error
= if_setlladdr(priv
->ifp
,
430 (u_char
*)msg
->data
, ETHER_ADDR_LEN
);
434 case NGM_EIFACE_GET_IFNAME
:
435 NG_MKRESPONSE(resp
, msg
, IFNAMSIZ
, M_WAITOK
| M_NULLOK
);
440 strlcpy(resp
->data
, ifp
->if_xname
, IFNAMSIZ
);
443 case NGM_EIFACE_GET_IFADDRS
:
449 #define SA_SIZE(s) ((s)->sa_len<sizeof(*(s))? sizeof(*(s)):(s)->sa_len)
451 /* Determine size of response and allocate it */
453 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
)
454 buflen
+= SA_SIZE(ifa
->ifa_addr
);
455 NG_MKRESPONSE(resp
, msg
, buflen
, M_WAITOK
| M_NULLOK
);
463 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
464 const int len
= SA_SIZE(ifa
->ifa_addr
);
467 log(LOG_ERR
, "%s: len changed?\n",
471 bcopy(ifa
->ifa_addr
, ptr
, len
);
482 } /* end of inner switch() */
484 case NGM_FLOW_COOKIE
:
485 switch (msg
->header
.cmd
) {
487 if_link_state_change(ifp
, LINK_STATE_UP
);
489 case NGM_LINK_IS_DOWN
:
490 if_link_state_change(ifp
, LINK_STATE_DOWN
);
500 NG_RESPOND_MSG(error
, node
, item
, resp
);
506 * Receive data from a hook. Pass the packet to the ether_input routine.
509 ng_eiface_rcvdata(hook_p hook
, item_p item
)
511 const priv_p priv
= NG_NODE_PRIVATE(NG_HOOK_NODE(hook
));
512 struct ifnet
*const ifp
= priv
->ifp
;
518 if (!((ifp
->if_flags
& IFF_UP
) &&
519 (ifp
->if_drv_flags
& IFF_DRV_RUNNING
))) {
524 if (m
->m_len
< ETHER_HDR_LEN
) {
525 m
= m_pullup(m
, ETHER_HDR_LEN
);
530 /* Note receiving interface */
531 m
->m_pkthdr
.rcvif
= ifp
;
533 /* Update interface stats */
536 (*ifp
->if_input
)(ifp
, m
);
543 * Shutdown processing.
546 ng_eiface_rmnode(node_p node
)
548 const priv_p priv
= NG_NODE_PRIVATE(node
);
549 struct ifnet
*const ifp
= priv
->ifp
;
553 free_unr(ng_eiface_unit
, priv
->unit
);
554 FREE(priv
, M_NETGRAPH
);
555 NG_NODE_SET_PRIVATE(node
, NULL
);
564 ng_eiface_disconnect(hook_p hook
)
566 const priv_p priv
= NG_NODE_PRIVATE(NG_HOOK_NODE(hook
));
573 * Handle loading and unloading for this node type.
576 ng_eiface_mod_event(module_t mod
, int event
, void *data
)
582 ng_eiface_unit
= new_unrhdr(0, 0xffff, NULL
);
585 delete_unrhdr(ng_eiface_unit
);