Merge from vendor branch NCURSES:
[dragonfly.git] / sys / netgraph / eiface / ng_eiface.c
1 /*
2  * ng_eiface.c
3  *
4  * Copyright (c) 1999-2000, Vitaly V Belekhov
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      $Id: ng_eiface.c,v 1.14 2000/03/15 12:28:44 vitaly Exp $
30  * $FreeBSD: src/sys/netgraph/ng_eiface.c,v 1.4.2.5 2002/12/17 21:47:48 julian Exp $
31  * $DragonFly: src/sys/netgraph/eiface/ng_eiface.c,v 1.7 2005/01/23 20:23:22 joerg Exp $
32  */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/errno.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/mbuf.h>
40 #include <sys/errno.h>
41 #include <sys/sockio.h>
42 #include <sys/socket.h>
43 #include <sys/syslog.h>
44
45 #include <net/if.h>
46 #include <net/if_types.h>
47 #include <net/netisr.h>
48
49
50 #include <netgraph/ng_message.h>
51 #include <netgraph/netgraph.h>
52 #include <netgraph/ng_parse.h>
53 #include "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_parse_struct_field ng_eiface_par_fields[]
60         = NG_EIFACE_PAR_FIELDS;
61
62 static const struct ng_parse_type ng_eiface_par_type = {
63         &ng_parse_struct_type,
64         &ng_eiface_par_fields
65 };
66
67 static const struct ng_cmdlist ng_eiface_cmdlist[] = {
68         {
69           NGM_EIFACE_COOKIE,
70           NGM_EIFACE_SET,
71           "set",
72           &ng_eiface_par_type,
73           NULL
74         },
75         { 0 }
76 };
77
78 /* Node private data */
79 struct ng_eiface_private {
80         struct arpcom   arpcom;         /* per-interface network data */
81         struct  ifnet *ifp;             /* This interface */
82         node_p  node;                   /* Our netgraph node */
83         hook_p  ether;                  /* Hook for ethernet stream */
84         struct  private *next;          /* When hung on the free list */
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 ng_constructor_t ng_eiface_constructor;
98 static ng_rcvmsg_t      ng_eiface_rcvmsg;
99 static ng_shutdown_t    ng_eiface_rmnode;
100 static ng_newhook_t     ng_eiface_newhook;
101 static ng_rcvdata_t     ng_eiface_rcvdata;
102 static ng_connect_t     ng_eiface_connect;
103 static ng_disconnect_t  ng_eiface_disconnect;
104
105 /* Node type descriptor */
106 static struct ng_type typestruct = {
107         NG_VERSION,
108         NG_EIFACE_NODE_TYPE,
109         NULL,
110         ng_eiface_constructor,
111         ng_eiface_rcvmsg,
112         ng_eiface_rmnode,
113         ng_eiface_newhook,
114         NULL,
115         ng_eiface_connect,
116         ng_eiface_rcvdata,
117         ng_eiface_rcvdata,
118         ng_eiface_disconnect,
119         ng_eiface_cmdlist
120 };
121 NETGRAPH_INIT(eiface, &typestruct);
122
123 static char ng_eiface_ifname[] = NG_EIFACE_EIFACE_NAME;
124 static int ng_eiface_next_unit;
125
126 /************************************************************************
127                         INTERFACE STUFF
128  ************************************************************************/
129
130 /*
131  * Process an ioctl for the virtual interface
132  */
133 static int
134 ng_eiface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
135 {
136         struct ifreq *const ifr = (struct ifreq *) data;
137         int s, error = 0;
138
139 #ifdef DEBUG
140         ng_eiface_print_ioctl(ifp, command, data);
141 #endif
142         s = splimp();
143         switch (command) {
144
145         /* These two are mostly handled at a higher layer */
146         case SIOCSIFADDR:
147                 error = ether_ioctl(ifp, command, data);
148                 break;
149         case SIOCGIFADDR:
150                 break;
151
152         /* Set flags */
153         case SIOCSIFFLAGS:
154                 /*
155                  * If the interface is marked up and stopped, then start it.
156                  * If it is marked down and running, then stop it.
157                  */
158                 if (ifr->ifr_flags & IFF_UP) {
159                         if (!(ifp->if_flags & IFF_RUNNING)) {
160                                 ifp->if_flags &= ~(IFF_OACTIVE);
161                                 ifp->if_flags |= IFF_RUNNING;
162                         }
163                 } else {
164                         if (ifp->if_flags & IFF_RUNNING)
165                                 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
166                 }
167                 break;
168
169         /* Set the interface MTU */
170         case SIOCSIFMTU:
171                 if (ifr->ifr_mtu > NG_EIFACE_MTU_MAX
172                     || ifr->ifr_mtu < NG_EIFACE_MTU_MIN)
173                         error = EINVAL;
174                 else
175                         ifp->if_mtu = ifr->ifr_mtu;
176                 break;
177
178         /* Stuff that's not supported */
179         case SIOCADDMULTI:
180         case SIOCDELMULTI:
181                 error = 0;
182                 break;
183         case SIOCSIFPHYS:
184                 error = EOPNOTSUPP;
185                 break;
186
187         default:
188                 error = EINVAL;
189                 break;
190         }
191         (void) splx(s);
192         return (error);
193 }
194
195 static void
196 ng_eiface_init(void *xsc)
197 {
198         priv_p sc = xsc;
199         struct ifnet *ifp = sc->ifp;
200         int s;
201
202         s = splimp();
203
204         ifp->if_flags |= IFF_RUNNING;
205         ifp->if_flags &= ~IFF_OACTIVE;
206
207         splx(s);
208
209 }
210
211 /*
212  * This routine is called to deliver a packet out the interface.
213  * We simply relay the packet to
214  * the ether hook, if it is connected.
215  */
216
217 static void
218 ng_eiface_start(struct ifnet *ifp)
219 {
220         const priv_p priv = (priv_p) ifp->if_softc;
221         meta_p meta = NULL;
222         int len, error = 0;
223         struct mbuf *m;
224
225         /* Check interface flags */
226         if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
227                 return;
228
229         /* Don't do anything if output is active */
230         if( ifp->if_flags & IFF_OACTIVE )
231                 return;
232
233         ifp->if_flags |= IFF_OACTIVE;
234
235         /*
236          * Grab a packet to transmit.
237          */
238         IF_DEQUEUE(&ifp->if_snd, m);
239
240         /* If there's nothing to send, return. */
241         if(m == NULL)
242         {
243                 ifp->if_flags &= ~IFF_OACTIVE;
244                 return;
245         }
246
247         BPF_MTAP(ifp, m);
248
249         /* Copy length before the mbuf gets invalidated */
250         len = m->m_pkthdr.len;
251
252         /* Send packet; if hook is not connected, mbuf will get freed. */
253         NG_SEND_DATA(error, priv->ether, m, meta);
254
255         /* Update stats */
256         if (error == 0) {
257                 ifp->if_obytes += len;
258                 ifp->if_opackets++;
259         }
260
261         ifp->if_flags &= ~IFF_OACTIVE;
262
263         return;
264 }
265
266 #ifdef DEBUG
267 /*
268  * Display an ioctl to the virtual interface
269  */
270
271 static void
272 ng_eiface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
273 {
274         char   *str;
275
276         switch (command & IOC_DIRMASK) {
277         case IOC_VOID:
278                 str = "IO";
279                 break;
280         case IOC_OUT:
281                 str = "IOR";
282                 break;
283         case IOC_IN:
284                 str = "IOW";
285                 break;
286         case IOC_INOUT:
287                 str = "IORW";
288                 break;
289         default:
290                 str = "IO??";
291         }
292         log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
293                ifp->if_xname,
294                str,
295                IOCGROUP(command),
296                command & 0xff,
297                IOCPARM_LEN(command));
298 }
299 #endif /* DEBUG */
300
301 /************************************************************************
302                         NETGRAPH NODE STUFF
303  ************************************************************************/
304
305 /*
306  * Constructor for a node
307  */
308 static int
309 ng_eiface_constructor(node_p *nodep)
310 {
311         struct ifnet *ifp;
312         node_p node;
313         priv_p priv;
314         int error = 0;
315
316         /* Allocate node and interface private structures */
317         MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_WAITOK);
318         if (priv == NULL)
319                 return (ENOMEM);
320         bzero(priv, sizeof(*priv));
321
322         ifp = &(priv->arpcom.ac_if);
323
324         /* Link them together */
325         ifp->if_softc = priv;
326         priv->ifp = ifp;
327
328         /* Call generic node constructor */
329         if ((error = ng_make_node_common(&typestruct, nodep))) {
330                 FREE(priv, M_NETGRAPH);
331                 return (error);
332         }
333         node = *nodep;
334
335         /* Link together node and private info */
336         node->private = priv;
337         priv->node = node;
338
339         /* Initialize interface structure */
340         if_initname(ifp, ng_eiface_ifname, ng_eiface_next_unit++);
341         ifp->if_init = ng_eiface_init;
342         ifp->if_start = ng_eiface_start;
343         ifp->if_ioctl = ng_eiface_ioctl;
344         ifp->if_watchdog = NULL;
345         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
346         ifp->if_flags = (IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST);
347
348         TAILQ_INIT(&ifp->if_addrhead);
349
350         /* Give this node name *
351         bzero(ifname, sizeof(ifname));
352         sprintf(ifname, "if%s", ifp->if_xname);
353         (void) ng_name_node(node, ifname);
354         */
355
356         /* Attach the interface */
357         ether_ifattach(ifp, priv->arpcom.ac_enaddr);
358
359         /* Done */
360         return (0);
361 }
362
363 /*
364  * Give our ok for a hook to be added
365  */
366 static int
367 ng_eiface_newhook(node_p node, hook_p hook, const char *name)
368 {
369         priv_p priv = node->private;
370
371         if (strcmp(name, NG_EIFACE_HOOK_ETHER))
372                 return (EPFNOSUPPORT);
373         if (priv->ether != NULL)
374                 return (EISCONN);
375         priv->ether = hook;
376         hook->private = &priv->ether;
377
378         return (0);
379 }
380
381 /*
382  * Receive a control message
383  */
384 static int
385 ng_eiface_rcvmsg(node_p node, struct ng_mesg *msg,
386                 const char *retaddr, struct ng_mesg **rptr)
387 {
388         const priv_p priv = node->private;
389         struct ifnet *const ifp = priv->ifp;
390         struct ng_mesg *resp = NULL;
391         int error = 0;
392
393         switch (msg->header.typecookie) {
394         case NGM_EIFACE_COOKIE:
395                 switch (msg->header.cmd) {
396
397                 case NGM_EIFACE_SET:
398                     {
399                       struct ng_eiface_par *eaddr;
400
401                       if (msg->header.arglen != sizeof(struct ng_eiface_par)) 
402                         {
403                           error = EINVAL;
404                           break;
405                         }
406                       eaddr = (struct ng_eiface_par *)(msg->data);
407
408                       priv->arpcom.ac_enaddr[0] = eaddr->oct0;
409                       priv->arpcom.ac_enaddr[1] = eaddr->oct1;
410                       priv->arpcom.ac_enaddr[2] = eaddr->oct2;
411                       priv->arpcom.ac_enaddr[3] = eaddr->oct3;
412                       priv->arpcom.ac_enaddr[4] = eaddr->oct4;
413                       priv->arpcom.ac_enaddr[5] = eaddr->oct5;
414
415                       break;
416                     }
417
418                 case NGM_EIFACE_GET_IFNAME:
419                     {
420                         struct ng_eiface_ifname *arg;
421
422                         NG_MKRESPONSE(resp, msg, sizeof(*arg), M_NOWAIT);
423                         if (resp == NULL) {
424                                 error = ENOMEM;
425                                 break;
426                         }
427                         arg = (struct ng_eiface_ifname *) resp->data;
428                         sprintf(arg->ngif_name,
429                             "%s", ifp->if_xname); /* XXX: strings */
430                         break;
431                     }
432
433                 case NGM_EIFACE_GET_IFADDRS:
434                     {
435                         struct ifaddr *ifa;
436                         caddr_t ptr;
437                         int buflen;
438
439 #define SA_SIZE(s)      ((s)->sa_len<sizeof(*(s))? sizeof(*(s)):(s)->sa_len)
440
441                         /* Determine size of response and allocate it */
442                         buflen = 0;
443                         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
444                                 buflen += SA_SIZE(ifa->ifa_addr);
445                         NG_MKRESPONSE(resp, msg, buflen, M_NOWAIT);
446                         if (resp == NULL) {
447                                 error = ENOMEM;
448                                 break;
449                         }
450
451                         /* Add addresses */
452                         ptr = resp->data;
453                         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
454                                 const int len = SA_SIZE(ifa->ifa_addr);
455
456                                 if (buflen < len) {
457                                         log(LOG_ERR, "%s: len changed?\n",
458                                             ifp->if_xname);
459                                         break;
460                                 }
461                                 bcopy(ifa->ifa_addr, ptr, len);
462                                 ptr += len;
463                                 buflen -= len;
464                         }
465                         break;
466 #undef SA_SIZE
467                     }
468
469                 default:
470                         error = EINVAL;
471                         break;
472                 }
473                 break;
474         default:
475                 error = EINVAL;
476                 break;
477         }
478         if (rptr)
479                 *rptr = resp;
480         else if (resp)
481                 FREE(resp, M_NETGRAPH);
482         FREE(msg, M_NETGRAPH);
483         return (error);
484 }
485
486 /*
487  * Recive data from a hook. Pass the packet to the ether_input routine.
488  */
489 static int
490 ng_eiface_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
491 {
492         const priv_p priv = hook->node->private;
493         struct ifnet *const ifp = priv->ifp;
494         int s, error = 0;
495         struct ether_header *eh;
496         u_short ether_type;
497
498         /* Meta-data is end its life here... */
499         NG_FREE_META(meta);
500
501         if (m == NULL)
502           {
503             printf("ng_eiface: mbuf is null.\n");
504             return (EINVAL);
505           }
506
507         if ( !(ifp->if_flags & IFF_UP) ) {
508                 return (ENETDOWN);
509         }
510
511         /* Note receiving interface */
512         m->m_pkthdr.rcvif = ifp;
513
514         /* Update interface stats */
515         ifp->if_ipackets++;
516
517         BPF_MTAP(ifp, m);
518
519         (*ifp->if_input)(ifp, m);
520
521         /* Done */
522         return (error);
523 }
524
525 /*
526  * Because the BSD networking code doesn't support the removal of
527  * networking interfaces, iface nodes (once created) are persistent.
528  * So this method breaks all connections and marks the interface
529  * down, but does not remove the node.
530  */
531 static int
532 ng_eiface_rmnode(node_p node)
533 {
534         const priv_p priv = node->private;
535         struct ifnet *const ifp = priv->ifp;
536
537         ng_cutlinks(node);
538         node->flags &= ~NG_INVALID;
539         ifp->if_flags &= ~(IFF_UP | IFF_RUNNING | IFF_OACTIVE);
540         return (0);
541 }
542
543
544 /*
545  * This is called once we've already connected a new hook to the other node.
546  * It gives us a chance to balk at the last minute.
547  */
548 static int
549 ng_eiface_connect(hook_p hook)
550 {
551         /* be really amiable and just say "YUP that's OK by me! " */
552         return (0);
553 }
554
555 /*
556  * Hook disconnection
557  */
558 static int
559 ng_eiface_disconnect(hook_p hook)
560 {
561         const priv_p priv = hook->node->private;
562
563         priv->ether = NULL;
564         return (0);
565 }