1/*-
2 *
3 * Copyright (c) 1999-2001, Vitaly V Belekhov
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
11 * disclaimer.
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.
15 *
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
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: head/sys/netgraph/ng_eiface.c 215673 2010-11-22 12:32:19Z zec $
29 */
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/errno.h>
34#include <sys/kernel.h>
35#include <sys/malloc.h>
36#include <sys/mbuf.h>
37#include <sys/errno.h>
38#include <sys/proc.h>
39#include <sys/sockio.h>
40#include <sys/socket.h>
41#include <sys/syslog.h>
42
43#include <net/if.h>
44#include <net/if_types.h>
45#include <net/netisr.h>
46#include <net/route.h>
47#include <net/vnet.h>
48
49#include <netgraph/ng_message.h>
50#include <netgraph/netgraph.h>
51#include <netgraph/ng_parse.h>
52#include <netgraph/ng_eiface.h>
53
54#include <net/bpf.h>
55#include <net/ethernet.h>
56#include <net/if_arp.h>
57
58static const struct ng_cmdlist ng_eiface_cmdlist[] = {
59 {
60 NGM_EIFACE_COOKIE,
61 NGM_EIFACE_GET_IFNAME,
62 "getifname",
63 NULL,
64 &ng_parse_string_type
65 },
66 {
67 NGM_EIFACE_COOKIE,
68 NGM_EIFACE_SET,
69 "set",
70 &ng_parse_enaddr_type,
71 NULL
72 },
73 { 0 }
74};
75
76/* Node private data */
77struct ng_eiface_private {
78 struct ifnet *ifp; /* per-interface network data */
79 int unit; /* Interface unit number */
80 node_p node; /* Our netgraph node */
81 hook_p ether; /* Hook for ethernet stream */
82};
83typedef struct ng_eiface_private *priv_p;
84
85/* Interface methods */
86static void ng_eiface_init(void *xsc);
87static void ng_eiface_start(struct ifnet *ifp);
88static int ng_eiface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
89#ifdef DEBUG
90static void ng_eiface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
91#endif
92
93/* Netgraph methods */
94static int ng_eiface_mod_event(module_t, int, void *);
95static ng_constructor_t ng_eiface_constructor;
96static ng_rcvmsg_t ng_eiface_rcvmsg;
97static ng_shutdown_t ng_eiface_rmnode;
98static ng_newhook_t ng_eiface_newhook;
99static ng_rcvdata_t ng_eiface_rcvdata;
100static ng_disconnect_t ng_eiface_disconnect;
101
102/* Node type descriptor */
103static struct ng_type typestruct = {
104 .version = NG_ABI_VERSION,
105 .name = NG_EIFACE_NODE_TYPE,
106 .mod_event = ng_eiface_mod_event,
107 .constructor = ng_eiface_constructor,
108 .rcvmsg = ng_eiface_rcvmsg,
109 .shutdown = ng_eiface_rmnode,
110 .newhook = ng_eiface_newhook,
111 .rcvdata = ng_eiface_rcvdata,
112 .disconnect = ng_eiface_disconnect,
113 .cmdlist = ng_eiface_cmdlist
114};
115NETGRAPH_INIT(eiface, &typestruct);
116
117STATIC_VNET_DEFINE(struct unrhdr *, ng_eiface_unit);
118#define V_ng_eiface_unit VNET(ng_eiface_unit)
119
120/************************************************************************
121 INTERFACE STUFF
122 ************************************************************************/
123
124/*
125 * Process an ioctl for the virtual interface
126 */
127static int
128ng_eiface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
129{
130 struct ifreq *const ifr = (struct ifreq *)data;
131 int s, error = 0;
132
133#ifdef DEBUG
134 ng_eiface_print_ioctl(ifp, command, data);
135#endif
136 s = splimp();
137 switch (command) {
138
139 /* These two are mostly handled at a higher layer */
140 case SIOCSIFADDR:
141 error = ether_ioctl(ifp, command, data);
142 break;
143 case SIOCGIFADDR:
144 break;
145
146 /* Set flags */
147 case SIOCSIFFLAGS:
148 /*
149 * If the interface is marked up and stopped, then start it.
150 * If it is marked down and running, then stop it.
151 */
152 if (ifp->if_flags & IFF_UP) {
153 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
154 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
155 ifp->if_drv_flags |= IFF_DRV_RUNNING;
156 }
157 } else {
158 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
159 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
160 IFF_DRV_OACTIVE);
161 }
162 break;
163
164 /* Set the interface MTU */
165 case SIOCSIFMTU:
166 if (ifr->ifr_mtu > NG_EIFACE_MTU_MAX ||
167 ifr->ifr_mtu < NG_EIFACE_MTU_MIN)
168 error = EINVAL;
169 else
170 ifp->if_mtu = ifr->ifr_mtu;
171 break;
172
173 /* Stuff that's not supported */
174 case SIOCADDMULTI:
175 case SIOCDELMULTI:
176 error = 0;
177 break;
178 case SIOCSIFPHYS:
179 error = EOPNOTSUPP;
180 break;
181
182 default:
183 error = EINVAL;
184 break;
185 }
186 splx(s);
187 return (error);
188}
189
190static void
191ng_eiface_init(void *xsc)
192{
193 priv_p sc = xsc;
194 struct ifnet *ifp = sc->ifp;
195 int s;
196
197 s = splimp();
198
199 ifp->if_drv_flags |= IFF_DRV_RUNNING;
200 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
201
202 splx(s);
203}
204
205/*
206 * We simply relay the packet to the "ether" hook, if it is connected.
207 * We have been through the netgraph locking and are guaranteed to
208 * be the only code running in this node at this time.
209 */
210static void
211ng_eiface_start2(node_p node, hook_p hook, void *arg1, int arg2)
212{
213 struct ifnet *ifp = arg1;
214 const priv_p priv = (priv_p)ifp->if_softc;
215 int error = 0;
216 struct mbuf *m;
217
218 /* Check interface flags */
219
220 if (!((ifp->if_flags & IFF_UP) &&
221 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
222 return;
223
224 for (;;) {
225 /*
226 * Grab a packet to transmit.
227 */
228 IF_DEQUEUE(&ifp->if_snd, m);
229
230 /* If there's nothing to send, break. */
231 if (m == NULL)
232 break;
233
234 /*
235 * Berkeley packet filter.
236 * Pass packet to bpf if there is a listener.
237 * XXX is this safe? locking?
238 */
239 BPF_MTAP(ifp, m);
240
241 if (ifp->if_flags & IFF_MONITOR) {
242 ifp->if_ipackets++;
243 m_freem(m);
244 continue;
245 }
246
247 /*
248 * Send packet; if hook is not connected, mbuf will get
249 * freed.
250 */
251 NG_OUTBOUND_THREAD_REF();
252 NG_SEND_DATA_ONLY(error, priv->ether, m);
253 NG_OUTBOUND_THREAD_UNREF();
254
255 /* Update stats */
256 if (error == 0)
257 ifp->if_opackets++;
258 else
259 ifp->if_oerrors++;
260 }
261
262 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
263
264 return;
265}
266
267/*
268 * This routine is called to deliver a packet out the interface.
269 * We simply queue the netgraph version to be called when netgraph locking
270 * allows it to happen.
271 * Until we know what the rest of the networking code is doing for
272 * locking, we don't know how we will interact with it.
273 * Take comfort from the fact that the ifnet struct is part of our
274 * private info and can't go away while we are queued.
275 * [Though we don't know it is still there now....]
276 * it is possible we don't gain anything from this because
277 * we would like to get the mbuf and queue it as data
278 * somehow, but we can't and if we did would we solve anything?
279 */
280static void
281ng_eiface_start(struct ifnet *ifp)
282{
283
284 const priv_p priv = (priv_p)ifp->if_softc;
285
286 /* Don't do anything if output is active */
287 if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
288 return;
289
290 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
291
292 if (ng_send_fn(priv->node, NULL, &ng_eiface_start2, ifp, 0) != 0)
293 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
294}
295
296#ifdef DEBUG
297/*
298 * Display an ioctl to the virtual interface
299 */
300
301static void
302ng_eiface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
303{
304 char *str;
305
306 switch (command & IOC_DIRMASK) {
307 case IOC_VOID:
308 str = "IO";
309 break;
310 case IOC_OUT:
311 str = "IOR";
312 break;
313 case IOC_IN:
314 str = "IOW";
315 break;
316 case IOC_INOUT:
317 str = "IORW";
318 break;
319 default:
320 str = "IO??";
321 }
322 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
323 ifp->if_xname,
324 str,
325 IOCGROUP(command),
326 command & 0xff,
327 IOCPARM_LEN(command));
328}
329#endif /* DEBUG */
330
331/************************************************************************
332 NETGRAPH NODE STUFF
333 ************************************************************************/
334
335/*
336 * Constructor for a node
337 */
338static int
339ng_eiface_constructor(node_p node)
340{
341 struct ifnet *ifp;
342 priv_p priv;
343 u_char eaddr[6] = {0,0,0,0,0,0};
344
345 /* Allocate node and interface private structures */
346 priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
347 if (priv == NULL)
348 return (ENOMEM);
349
350 ifp = priv->ifp = if_alloc(IFT_ETHER);
351 if (ifp == NULL) {
352 free(priv, M_NETGRAPH);
353 return (ENOSPC);
354 }
355
356 /* Link them together */
357 ifp->if_softc = priv;
358
359 /* Get an interface unit number */
360 priv->unit = alloc_unr(V_ng_eiface_unit);
361
362 /* Link together node and private info */
363 NG_NODE_SET_PRIVATE(node, priv);
364 priv->node = node;
365
366 /* Initialize interface structure */
367 if_initname(ifp, NG_EIFACE_EIFACE_NAME, priv->unit);
368 ifp->if_init = ng_eiface_init;
369 ifp->if_output = ether_output;
370 ifp->if_start = ng_eiface_start;
371 ifp->if_ioctl = ng_eiface_ioctl;
372 ifp->if_snd.ifq_maxlen = ifqmaxlen;
373 ifp->if_flags = (IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST);
374 ifp->if_capabilities = IFCAP_VLAN_MTU | IFCAP_JUMBO_MTU;
375 ifp->if_capenable = IFCAP_VLAN_MTU | IFCAP_JUMBO_MTU;
376
377 /* Give this node the same name as the interface (if possible) */
378 if (ng_name_node(node, ifp->if_xname) != 0)
379 log(LOG_WARNING, "%s: can't acquire netgraph name\n",
380 ifp->if_xname);
381
382 /* Attach the interface */
383 ether_ifattach(ifp, eaddr);
384
385 /* Done */
386 return (0);
387}
388
389/*
390 * Give our ok for a hook to be added
391 */
392static int
393ng_eiface_newhook(node_p node, hook_p hook, const char *name)
394{
395 priv_p priv = NG_NODE_PRIVATE(node);
396 struct ifnet *ifp = priv->ifp;
397
398 if (strcmp(name, NG_EIFACE_HOOK_ETHER))
399 return (EPFNOSUPPORT);
400 if (priv->ether != NULL)
401 return (EISCONN);
402 priv->ether = hook;
403 NG_HOOK_SET_PRIVATE(hook, &priv->ether);
404 NG_HOOK_SET_TO_INBOUND(hook);
405
406 if_link_state_change(ifp, LINK_STATE_UP);
407
408 return (0);
409}
410
411/*
412 * Receive a control message
413 */
414static int
415ng_eiface_rcvmsg(node_p node, item_p item, hook_p lasthook)
416{
417 const priv_p priv = NG_NODE_PRIVATE(node);
418 struct ifnet *const ifp = priv->ifp;
419 struct ng_mesg *resp = NULL;
420 int error = 0;
421 struct ng_mesg *msg;
422
423 NGI_GET_MSG(item, msg);
424 switch (msg->header.typecookie) {
425 case NGM_EIFACE_COOKIE:
426 switch (msg->header.cmd) {
427
428 case NGM_EIFACE_SET:
429 {
430 if (msg->header.arglen != ETHER_ADDR_LEN) {
431 error = EINVAL;
432 break;
433 }
434 error = if_setlladdr(priv->ifp,
435 (u_char *)msg->data, ETHER_ADDR_LEN);
436 EVENTHANDLER_INVOKE(iflladdr_event, priv->ifp);
437 break;
438 }
439
440 case NGM_EIFACE_GET_IFNAME:
441 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
442 if (resp == NULL) {
443 error = ENOMEM;
444 break;
445 }
446 strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
447 break;
448
449 case NGM_EIFACE_GET_IFADDRS:
450 {
451 struct ifaddr *ifa;
452 caddr_t ptr;
453 int buflen;
454
455 /* Determine size of response and allocate it */
456 buflen = 0;
457 if_addr_rlock(ifp);
458 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
459 buflen += SA_SIZE(ifa->ifa_addr);
460 NG_MKRESPONSE(resp, msg, buflen, M_NOWAIT);
461 if (resp == NULL) {
462 if_addr_runlock(ifp);
463 error = ENOMEM;
464 break;
465 }
466
467 /* Add addresses */
468 ptr = resp->data;
469 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
470 const int len = SA_SIZE(ifa->ifa_addr);
471
472 if (buflen < len) {
473 log(LOG_ERR, "%s: len changed?\n",
474 ifp->if_xname);
475 break;
476 }
477 bcopy(ifa->ifa_addr, ptr, len);
478 ptr += len;
479 buflen -= len;
480 }
481 if_addr_runlock(ifp);
482 break;
483 }
484
485 default:
486 error = EINVAL;
487 break;
488 } /* end of inner switch() */
489 break;
490 case NGM_FLOW_COOKIE:
491 switch (msg->header.cmd) {
492 case NGM_LINK_IS_UP:
493 if_link_state_change(ifp, LINK_STATE_UP);
494 break;
495 case NGM_LINK_IS_DOWN:
496 if_link_state_change(ifp, LINK_STATE_DOWN);
497 break;
498 default:
499 break;
500 }
501 break;
502 default:
503 error = EINVAL;
504 break;
505 }
506 NG_RESPOND_MSG(error, node, item, resp);
507 NG_FREE_MSG(msg);
508 return (error);
509}
510
511/*
512 * Receive data from a hook. Pass the packet to the ether_input routine.
513 */
514static int
515ng_eiface_rcvdata(hook_p hook, item_p item)
516{
517 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
518 struct ifnet *const ifp = priv->ifp;
519 struct mbuf *m;
520
521 NGI_GET_M(item, m);
522 NG_FREE_ITEM(item);
523
524 if (!((ifp->if_flags & IFF_UP) &&
525 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
526 NG_FREE_M(m);
527 return (ENETDOWN);
528 }
529
530 if (m->m_len < ETHER_HDR_LEN) {
531 m = m_pullup(m, ETHER_HDR_LEN);
532 if (m == NULL)
533 return (EINVAL);
534 }
535
536 /* Note receiving interface */
537 m->m_pkthdr.rcvif = ifp;
538
539 /* Update interface stats */
540 ifp->if_ipackets++;
541
542 (*ifp->if_input)(ifp, m);
543
544 /* Done */
545 return (0);
546}
547
548/*
549 * Shutdown processing.
550 */
551static int
552ng_eiface_rmnode(node_p node)
553{
554 const priv_p priv = NG_NODE_PRIVATE(node);
555 struct ifnet *const ifp = priv->ifp;
556
557 /*
558 * the ifnet may be in a different vnet than the netgraph node,
559 * hence we have to change the current vnet context here.
560 */
561 CURVNET_SET_QUIET(ifp->if_vnet);
562 ether_ifdetach(ifp);
563 if_free(ifp);
564 CURVNET_RESTORE();
565 free_unr(V_ng_eiface_unit, priv->unit);
566 free(priv, M_NETGRAPH);
567 NG_NODE_SET_PRIVATE(node, NULL);
568 NG_NODE_UNREF(node);
569 return (0);
570}
571
572/*
573 * Hook disconnection
574 */
575static int
576ng_eiface_disconnect(hook_p hook)
577{
578 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
579
580 priv->ether = NULL;
581 return (0);
582}
583
584/*
585 * Handle loading and unloading for this node type.
586 */
587static int
588ng_eiface_mod_event(module_t mod, int event, void *data)
589{
590 int error = 0;
591
592 switch (event) {
593 case MOD_LOAD:
594 case MOD_UNLOAD:
595 break;
596 default:
597 error = EOPNOTSUPP;
598 break;
599 }
600 return (error);
601}
602
603static void
604vnet_ng_eiface_init(const void *unused)
605{
606
607 V_ng_eiface_unit = new_unrhdr(0, 0xffff, NULL);
608}
609VNET_SYSINIT(vnet_ng_eiface_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
610 vnet_ng_eiface_init, NULL);
611
612static void
613vnet_ng_eiface_uninit(const void *unused)
614{
615
616 delete_unrhdr(V_ng_eiface_unit);
617}
618VNET_SYSUNINIT(vnet_ng_eiface_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY,
619 vnet_ng_eiface_uninit, NULL);
2 *
3 * Copyright (c) 1999-2001, Vitaly V Belekhov
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
11 * disclaimer.
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.
15 *
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
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: head/sys/netgraph/ng_eiface.c 215673 2010-11-22 12:32:19Z zec $
29 */
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/errno.h>
34#include <sys/kernel.h>
35#include <sys/malloc.h>
36#include <sys/mbuf.h>
37#include <sys/errno.h>
38#include <sys/proc.h>
39#include <sys/sockio.h>
40#include <sys/socket.h>
41#include <sys/syslog.h>
42
43#include <net/if.h>
44#include <net/if_types.h>
45#include <net/netisr.h>
46#include <net/route.h>
47#include <net/vnet.h>
48
49#include <netgraph/ng_message.h>
50#include <netgraph/netgraph.h>
51#include <netgraph/ng_parse.h>
52#include <netgraph/ng_eiface.h>
53
54#include <net/bpf.h>
55#include <net/ethernet.h>
56#include <net/if_arp.h>
57
58static const struct ng_cmdlist ng_eiface_cmdlist[] = {
59 {
60 NGM_EIFACE_COOKIE,
61 NGM_EIFACE_GET_IFNAME,
62 "getifname",
63 NULL,
64 &ng_parse_string_type
65 },
66 {
67 NGM_EIFACE_COOKIE,
68 NGM_EIFACE_SET,
69 "set",
70 &ng_parse_enaddr_type,
71 NULL
72 },
73 { 0 }
74};
75
76/* Node private data */
77struct ng_eiface_private {
78 struct ifnet *ifp; /* per-interface network data */
79 int unit; /* Interface unit number */
80 node_p node; /* Our netgraph node */
81 hook_p ether; /* Hook for ethernet stream */
82};
83typedef struct ng_eiface_private *priv_p;
84
85/* Interface methods */
86static void ng_eiface_init(void *xsc);
87static void ng_eiface_start(struct ifnet *ifp);
88static int ng_eiface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
89#ifdef DEBUG
90static void ng_eiface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
91#endif
92
93/* Netgraph methods */
94static int ng_eiface_mod_event(module_t, int, void *);
95static ng_constructor_t ng_eiface_constructor;
96static ng_rcvmsg_t ng_eiface_rcvmsg;
97static ng_shutdown_t ng_eiface_rmnode;
98static ng_newhook_t ng_eiface_newhook;
99static ng_rcvdata_t ng_eiface_rcvdata;
100static ng_disconnect_t ng_eiface_disconnect;
101
102/* Node type descriptor */
103static struct ng_type typestruct = {
104 .version = NG_ABI_VERSION,
105 .name = NG_EIFACE_NODE_TYPE,
106 .mod_event = ng_eiface_mod_event,
107 .constructor = ng_eiface_constructor,
108 .rcvmsg = ng_eiface_rcvmsg,
109 .shutdown = ng_eiface_rmnode,
110 .newhook = ng_eiface_newhook,
111 .rcvdata = ng_eiface_rcvdata,
112 .disconnect = ng_eiface_disconnect,
113 .cmdlist = ng_eiface_cmdlist
114};
115NETGRAPH_INIT(eiface, &typestruct);
116
117STATIC_VNET_DEFINE(struct unrhdr *, ng_eiface_unit);
118#define V_ng_eiface_unit VNET(ng_eiface_unit)
119
120/************************************************************************
121 INTERFACE STUFF
122 ************************************************************************/
123
124/*
125 * Process an ioctl for the virtual interface
126 */
127static int
128ng_eiface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
129{
130 struct ifreq *const ifr = (struct ifreq *)data;
131 int s, error = 0;
132
133#ifdef DEBUG
134 ng_eiface_print_ioctl(ifp, command, data);
135#endif
136 s = splimp();
137 switch (command) {
138
139 /* These two are mostly handled at a higher layer */
140 case SIOCSIFADDR:
141 error = ether_ioctl(ifp, command, data);
142 break;
143 case SIOCGIFADDR:
144 break;
145
146 /* Set flags */
147 case SIOCSIFFLAGS:
148 /*
149 * If the interface is marked up and stopped, then start it.
150 * If it is marked down and running, then stop it.
151 */
152 if (ifp->if_flags & IFF_UP) {
153 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
154 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
155 ifp->if_drv_flags |= IFF_DRV_RUNNING;
156 }
157 } else {
158 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
159 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
160 IFF_DRV_OACTIVE);
161 }
162 break;
163
164 /* Set the interface MTU */
165 case SIOCSIFMTU:
166 if (ifr->ifr_mtu > NG_EIFACE_MTU_MAX ||
167 ifr->ifr_mtu < NG_EIFACE_MTU_MIN)
168 error = EINVAL;
169 else
170 ifp->if_mtu = ifr->ifr_mtu;
171 break;
172
173 /* Stuff that's not supported */
174 case SIOCADDMULTI:
175 case SIOCDELMULTI:
176 error = 0;
177 break;
178 case SIOCSIFPHYS:
179 error = EOPNOTSUPP;
180 break;
181
182 default:
183 error = EINVAL;
184 break;
185 }
186 splx(s);
187 return (error);
188}
189
190static void
191ng_eiface_init(void *xsc)
192{
193 priv_p sc = xsc;
194 struct ifnet *ifp = sc->ifp;
195 int s;
196
197 s = splimp();
198
199 ifp->if_drv_flags |= IFF_DRV_RUNNING;
200 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
201
202 splx(s);
203}
204
205/*
206 * We simply relay the packet to the "ether" hook, if it is connected.
207 * We have been through the netgraph locking and are guaranteed to
208 * be the only code running in this node at this time.
209 */
210static void
211ng_eiface_start2(node_p node, hook_p hook, void *arg1, int arg2)
212{
213 struct ifnet *ifp = arg1;
214 const priv_p priv = (priv_p)ifp->if_softc;
215 int error = 0;
216 struct mbuf *m;
217
218 /* Check interface flags */
219
220 if (!((ifp->if_flags & IFF_UP) &&
221 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
222 return;
223
224 for (;;) {
225 /*
226 * Grab a packet to transmit.
227 */
228 IF_DEQUEUE(&ifp->if_snd, m);
229
230 /* If there's nothing to send, break. */
231 if (m == NULL)
232 break;
233
234 /*
235 * Berkeley packet filter.
236 * Pass packet to bpf if there is a listener.
237 * XXX is this safe? locking?
238 */
239 BPF_MTAP(ifp, m);
240
241 if (ifp->if_flags & IFF_MONITOR) {
242 ifp->if_ipackets++;
243 m_freem(m);
244 continue;
245 }
246
247 /*
248 * Send packet; if hook is not connected, mbuf will get
249 * freed.
250 */
251 NG_OUTBOUND_THREAD_REF();
252 NG_SEND_DATA_ONLY(error, priv->ether, m);
253 NG_OUTBOUND_THREAD_UNREF();
254
255 /* Update stats */
256 if (error == 0)
257 ifp->if_opackets++;
258 else
259 ifp->if_oerrors++;
260 }
261
262 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
263
264 return;
265}
266
267/*
268 * This routine is called to deliver a packet out the interface.
269 * We simply queue the netgraph version to be called when netgraph locking
270 * allows it to happen.
271 * Until we know what the rest of the networking code is doing for
272 * locking, we don't know how we will interact with it.
273 * Take comfort from the fact that the ifnet struct is part of our
274 * private info and can't go away while we are queued.
275 * [Though we don't know it is still there now....]
276 * it is possible we don't gain anything from this because
277 * we would like to get the mbuf and queue it as data
278 * somehow, but we can't and if we did would we solve anything?
279 */
280static void
281ng_eiface_start(struct ifnet *ifp)
282{
283
284 const priv_p priv = (priv_p)ifp->if_softc;
285
286 /* Don't do anything if output is active */
287 if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
288 return;
289
290 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
291
292 if (ng_send_fn(priv->node, NULL, &ng_eiface_start2, ifp, 0) != 0)
293 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
294}
295
296#ifdef DEBUG
297/*
298 * Display an ioctl to the virtual interface
299 */
300
301static void
302ng_eiface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
303{
304 char *str;
305
306 switch (command & IOC_DIRMASK) {
307 case IOC_VOID:
308 str = "IO";
309 break;
310 case IOC_OUT:
311 str = "IOR";
312 break;
313 case IOC_IN:
314 str = "IOW";
315 break;
316 case IOC_INOUT:
317 str = "IORW";
318 break;
319 default:
320 str = "IO??";
321 }
322 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
323 ifp->if_xname,
324 str,
325 IOCGROUP(command),
326 command & 0xff,
327 IOCPARM_LEN(command));
328}
329#endif /* DEBUG */
330
331/************************************************************************
332 NETGRAPH NODE STUFF
333 ************************************************************************/
334
335/*
336 * Constructor for a node
337 */
338static int
339ng_eiface_constructor(node_p node)
340{
341 struct ifnet *ifp;
342 priv_p priv;
343 u_char eaddr[6] = {0,0,0,0,0,0};
344
345 /* Allocate node and interface private structures */
346 priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
347 if (priv == NULL)
348 return (ENOMEM);
349
350 ifp = priv->ifp = if_alloc(IFT_ETHER);
351 if (ifp == NULL) {
352 free(priv, M_NETGRAPH);
353 return (ENOSPC);
354 }
355
356 /* Link them together */
357 ifp->if_softc = priv;
358
359 /* Get an interface unit number */
360 priv->unit = alloc_unr(V_ng_eiface_unit);
361
362 /* Link together node and private info */
363 NG_NODE_SET_PRIVATE(node, priv);
364 priv->node = node;
365
366 /* Initialize interface structure */
367 if_initname(ifp, NG_EIFACE_EIFACE_NAME, priv->unit);
368 ifp->if_init = ng_eiface_init;
369 ifp->if_output = ether_output;
370 ifp->if_start = ng_eiface_start;
371 ifp->if_ioctl = ng_eiface_ioctl;
372 ifp->if_snd.ifq_maxlen = ifqmaxlen;
373 ifp->if_flags = (IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST);
374 ifp->if_capabilities = IFCAP_VLAN_MTU | IFCAP_JUMBO_MTU;
375 ifp->if_capenable = IFCAP_VLAN_MTU | IFCAP_JUMBO_MTU;
376
377 /* Give this node the same name as the interface (if possible) */
378 if (ng_name_node(node, ifp->if_xname) != 0)
379 log(LOG_WARNING, "%s: can't acquire netgraph name\n",
380 ifp->if_xname);
381
382 /* Attach the interface */
383 ether_ifattach(ifp, eaddr);
384
385 /* Done */
386 return (0);
387}
388
389/*
390 * Give our ok for a hook to be added
391 */
392static int
393ng_eiface_newhook(node_p node, hook_p hook, const char *name)
394{
395 priv_p priv = NG_NODE_PRIVATE(node);
396 struct ifnet *ifp = priv->ifp;
397
398 if (strcmp(name, NG_EIFACE_HOOK_ETHER))
399 return (EPFNOSUPPORT);
400 if (priv->ether != NULL)
401 return (EISCONN);
402 priv->ether = hook;
403 NG_HOOK_SET_PRIVATE(hook, &priv->ether);
404 NG_HOOK_SET_TO_INBOUND(hook);
405
406 if_link_state_change(ifp, LINK_STATE_UP);
407
408 return (0);
409}
410
411/*
412 * Receive a control message
413 */
414static int
415ng_eiface_rcvmsg(node_p node, item_p item, hook_p lasthook)
416{
417 const priv_p priv = NG_NODE_PRIVATE(node);
418 struct ifnet *const ifp = priv->ifp;
419 struct ng_mesg *resp = NULL;
420 int error = 0;
421 struct ng_mesg *msg;
422
423 NGI_GET_MSG(item, msg);
424 switch (msg->header.typecookie) {
425 case NGM_EIFACE_COOKIE:
426 switch (msg->header.cmd) {
427
428 case NGM_EIFACE_SET:
429 {
430 if (msg->header.arglen != ETHER_ADDR_LEN) {
431 error = EINVAL;
432 break;
433 }
434 error = if_setlladdr(priv->ifp,
435 (u_char *)msg->data, ETHER_ADDR_LEN);
436 EVENTHANDLER_INVOKE(iflladdr_event, priv->ifp);
437 break;
438 }
439
440 case NGM_EIFACE_GET_IFNAME:
441 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
442 if (resp == NULL) {
443 error = ENOMEM;
444 break;
445 }
446 strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
447 break;
448
449 case NGM_EIFACE_GET_IFADDRS:
450 {
451 struct ifaddr *ifa;
452 caddr_t ptr;
453 int buflen;
454
455 /* Determine size of response and allocate it */
456 buflen = 0;
457 if_addr_rlock(ifp);
458 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
459 buflen += SA_SIZE(ifa->ifa_addr);
460 NG_MKRESPONSE(resp, msg, buflen, M_NOWAIT);
461 if (resp == NULL) {
462 if_addr_runlock(ifp);
463 error = ENOMEM;
464 break;
465 }
466
467 /* Add addresses */
468 ptr = resp->data;
469 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
470 const int len = SA_SIZE(ifa->ifa_addr);
471
472 if (buflen < len) {
473 log(LOG_ERR, "%s: len changed?\n",
474 ifp->if_xname);
475 break;
476 }
477 bcopy(ifa->ifa_addr, ptr, len);
478 ptr += len;
479 buflen -= len;
480 }
481 if_addr_runlock(ifp);
482 break;
483 }
484
485 default:
486 error = EINVAL;
487 break;
488 } /* end of inner switch() */
489 break;
490 case NGM_FLOW_COOKIE:
491 switch (msg->header.cmd) {
492 case NGM_LINK_IS_UP:
493 if_link_state_change(ifp, LINK_STATE_UP);
494 break;
495 case NGM_LINK_IS_DOWN:
496 if_link_state_change(ifp, LINK_STATE_DOWN);
497 break;
498 default:
499 break;
500 }
501 break;
502 default:
503 error = EINVAL;
504 break;
505 }
506 NG_RESPOND_MSG(error, node, item, resp);
507 NG_FREE_MSG(msg);
508 return (error);
509}
510
511/*
512 * Receive data from a hook. Pass the packet to the ether_input routine.
513 */
514static int
515ng_eiface_rcvdata(hook_p hook, item_p item)
516{
517 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
518 struct ifnet *const ifp = priv->ifp;
519 struct mbuf *m;
520
521 NGI_GET_M(item, m);
522 NG_FREE_ITEM(item);
523
524 if (!((ifp->if_flags & IFF_UP) &&
525 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
526 NG_FREE_M(m);
527 return (ENETDOWN);
528 }
529
530 if (m->m_len < ETHER_HDR_LEN) {
531 m = m_pullup(m, ETHER_HDR_LEN);
532 if (m == NULL)
533 return (EINVAL);
534 }
535
536 /* Note receiving interface */
537 m->m_pkthdr.rcvif = ifp;
538
539 /* Update interface stats */
540 ifp->if_ipackets++;
541
542 (*ifp->if_input)(ifp, m);
543
544 /* Done */
545 return (0);
546}
547
548/*
549 * Shutdown processing.
550 */
551static int
552ng_eiface_rmnode(node_p node)
553{
554 const priv_p priv = NG_NODE_PRIVATE(node);
555 struct ifnet *const ifp = priv->ifp;
556
557 /*
558 * the ifnet may be in a different vnet than the netgraph node,
559 * hence we have to change the current vnet context here.
560 */
561 CURVNET_SET_QUIET(ifp->if_vnet);
562 ether_ifdetach(ifp);
563 if_free(ifp);
564 CURVNET_RESTORE();
565 free_unr(V_ng_eiface_unit, priv->unit);
566 free(priv, M_NETGRAPH);
567 NG_NODE_SET_PRIVATE(node, NULL);
568 NG_NODE_UNREF(node);
569 return (0);
570}
571
572/*
573 * Hook disconnection
574 */
575static int
576ng_eiface_disconnect(hook_p hook)
577{
578 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
579
580 priv->ether = NULL;
581 return (0);
582}
583
584/*
585 * Handle loading and unloading for this node type.
586 */
587static int
588ng_eiface_mod_event(module_t mod, int event, void *data)
589{
590 int error = 0;
591
592 switch (event) {
593 case MOD_LOAD:
594 case MOD_UNLOAD:
595 break;
596 default:
597 error = EOPNOTSUPP;
598 break;
599 }
600 return (error);
601}
602
603static void
604vnet_ng_eiface_init(const void *unused)
605{
606
607 V_ng_eiface_unit = new_unrhdr(0, 0xffff, NULL);
608}
609VNET_SYSINIT(vnet_ng_eiface_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
610 vnet_ng_eiface_init, NULL);
611
612static void
613vnet_ng_eiface_uninit(const void *unused)
614{
615
616 delete_unrhdr(V_ng_eiface_unit);
617}
618VNET_SYSUNINIT(vnet_ng_eiface_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY,
619 vnet_ng_eiface_uninit, NULL);