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