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