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