6104fb0c9eeb91afcccd99e0abc8892eb6b51fa1
[dragonfly.git] / sys / netgraph / cisco / ng_cisco.c
1
2 /*
3  * ng_cisco.c
4  *
5  * Copyright (c) 1996-1999 Whistle Communications, Inc.
6  * All rights reserved.
7  * 
8  * Subject to the following obligations and disclaimer of warranty, use and
9  * redistribution of this software, in source or object code forms, with or
10  * without modifications are expressly permitted by Whistle Communications;
11  * provided, however, that:
12  * 1. Any and all reproductions of the source or object code must include the
13  *    copyright notice above and the following disclaimer of warranties; and
14  * 2. No rights are granted, in any manner or form, to use Whistle
15  *    Communications, Inc. trademarks, including the mark "WHISTLE
16  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17  *    such appears in the above copyright notice or in the software.
18  * 
19  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35  * OF SUCH DAMAGE.
36  *
37  * Author: Julian Elischer <julian@freebsd.org>
38  *
39  * $FreeBSD: src/sys/netgraph/ng_cisco.c,v 1.4.2.6 2002/07/02 23:44:02 archie Exp $
40  * $DragonFly: src/sys/netgraph/cisco/ng_cisco.c,v 1.10 2008/01/05 14:02:39 swildner Exp $
41  * $Whistle: ng_cisco.c,v 1.25 1999/11/01 09:24:51 julian Exp $
42  */
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/errno.h>
47 #include <sys/kernel.h>
48 #include <sys/socket.h>
49 #include <sys/malloc.h>
50 #include <sys/mbuf.h>
51 #include <sys/syslog.h>
52 #include <sys/thread2.h>
53
54 #include <machine/inttypes.h>
55
56 #include <net/if.h>
57
58 #include <netinet/in.h>
59 #include <netinet/if_ether.h>
60
61 #include <netproto/atalk/at.h>
62
63 #include <netproto/ipx/ipx.h>
64 #include <netproto/ipx/ipx_if.h>
65
66 #include <netgraph/ng_message.h>
67 #include <netgraph/netgraph.h>
68 #include <netgraph/ng_parse.h>
69 #include "ng_cisco.h"
70
71 #define CISCO_MULTICAST         0x8f    /* Cisco multicast address */
72 #define CISCO_UNICAST           0x0f    /* Cisco unicast address */
73 #define CISCO_KEEPALIVE         0x8035  /* Cisco keepalive protocol */
74 #define CISCO_ADDR_REQ          0       /* Cisco address request */
75 #define CISCO_ADDR_REPLY        1       /* Cisco address reply */
76 #define CISCO_KEEPALIVE_REQ     2       /* Cisco keepalive request */
77
78 #define KEEPALIVE_SECS          10
79
80 struct cisco_header {
81         u_char  address;
82         u_char  control;
83         u_short protocol;
84 };
85
86 #define CISCO_HEADER_LEN          sizeof (struct cisco_header)
87
88 struct cisco_packet {
89         u_long  type;
90         u_long  par1;
91         u_long  par2;
92         u_short rel;
93         u_short time0;
94         u_short time1;
95 };
96
97 #define CISCO_PACKET_LEN (sizeof(struct cisco_packet))
98
99 struct protoent {
100         hook_p  hook;           /* the hook for this proto */
101         u_short af;             /* address family, -1 = downstream */
102 };
103
104 struct cisco_priv {
105         u_long  local_seq;
106         u_long  remote_seq;
107         u_long  seqRetries;     /* how many times we've been here throwing out
108                                  * the same sequence number without ack */
109         node_p  node;
110         struct callout timeout;
111         struct protoent downstream;
112         struct protoent inet;           /* IP information */
113         struct in_addr localip;
114         struct in_addr localmask;
115         struct protoent inet6;          /* IPv6 information */
116         struct protoent atalk;          /* AppleTalk information */
117         struct protoent ipx;            /* IPX information */
118 };
119 typedef struct cisco_priv *sc_p;
120
121 /* Netgraph methods */
122 static ng_constructor_t         cisco_constructor;
123 static ng_rcvmsg_t              cisco_rcvmsg;
124 static ng_shutdown_t            cisco_rmnode;
125 static ng_newhook_t             cisco_newhook;
126 static ng_rcvdata_t             cisco_rcvdata;
127 static ng_disconnect_t          cisco_disconnect;
128
129 /* Other functions */
130 static int      cisco_input(sc_p sc, struct mbuf *m, meta_p meta);
131 static void     cisco_keepalive(void *arg);
132 static int      cisco_send(sc_p sc, int type, long par1, long par2);
133
134 /* Parse type for struct ng_cisco_ipaddr */
135 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
136         = NG_CISCO_IPADDR_TYPE_INFO;
137 static const struct ng_parse_type ng_cisco_ipaddr_type = {
138         &ng_parse_struct_type,
139         &ng_cisco_ipaddr_type_fields
140 };
141
142 /* Parse type for struct ng_async_stat */
143 static const struct ng_parse_struct_field ng_cisco_stats_type_fields[]
144         = NG_CISCO_STATS_TYPE_INFO;
145 static const struct ng_parse_type ng_cisco_stats_type = {
146         &ng_parse_struct_type,
147         &ng_cisco_stats_type_fields
148 };
149
150 /* List of commands and how to convert arguments to/from ASCII */
151 static const struct ng_cmdlist ng_cisco_cmdlist[] = {
152         {
153           NGM_CISCO_COOKIE,
154           NGM_CISCO_SET_IPADDR,
155           "setipaddr",
156           &ng_cisco_ipaddr_type,
157           NULL
158         },
159         {
160           NGM_CISCO_COOKIE,
161           NGM_CISCO_GET_IPADDR,
162           "getipaddr",
163           NULL,
164           &ng_cisco_ipaddr_type
165         },
166         {
167           NGM_CISCO_COOKIE,
168           NGM_CISCO_GET_STATUS,
169           "getstats",
170           NULL,
171           &ng_cisco_stats_type
172         },
173         { 0 }
174 };
175
176 /* Node type */
177 static struct ng_type typestruct = {
178         NG_VERSION,
179         NG_CISCO_NODE_TYPE,
180         NULL,
181         cisco_constructor,
182         cisco_rcvmsg,
183         cisco_rmnode,
184         cisco_newhook,
185         NULL,
186         NULL,
187         cisco_rcvdata,
188         cisco_rcvdata,
189         cisco_disconnect,
190         ng_cisco_cmdlist
191 };
192 NETGRAPH_INIT(cisco, &typestruct);
193
194 /*
195  * Node constructor
196  */
197 static int
198 cisco_constructor(node_p *nodep)
199 {
200         sc_p sc;
201         int error = 0;
202
203         MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO);
204         if (sc == NULL)
205                 return (ENOMEM);
206
207         callout_init(&sc->timeout);
208         if ((error = ng_make_node_common(&typestruct, nodep))) {
209                 FREE(sc, M_NETGRAPH);
210                 return (error);
211         }
212         (*nodep)->private = sc;
213         sc->node = *nodep;
214
215         /* Initialise the varous protocol hook holders */
216         sc->downstream.af = 0xffff;
217         sc->inet.af = AF_INET;
218         sc->inet6.af = AF_INET6;
219         sc->atalk.af = AF_APPLETALK;
220         sc->ipx.af = AF_IPX;
221         return (0);
222 }
223
224 /*
225  * Check new hook
226  */
227 static int
228 cisco_newhook(node_p node, hook_p hook, const char *name)
229 {
230         const sc_p sc = node->private;
231
232         if (strcmp(name, NG_CISCO_HOOK_DOWNSTREAM) == 0) {
233                 sc->downstream.hook = hook;
234                 hook->private = &sc->downstream;
235
236                 /* Start keepalives */
237                 callout_reset(&sc->timeout, hz * KEEPALIVE_SECS, 
238                                 cisco_keepalive, sc);
239         } else if (strcmp(name, NG_CISCO_HOOK_INET) == 0) {
240                 sc->inet.hook = hook;
241                 hook->private = &sc->inet;
242         } else if (strcmp(name, NG_CISCO_HOOK_APPLETALK) == 0) {
243                 sc->atalk.hook = hook;
244                 hook->private = &sc->atalk;
245         } else if (strcmp(name, NG_CISCO_HOOK_IPX) == 0) {
246                 sc->ipx.hook = hook;
247                 hook->private = &sc->ipx;
248         } else if (strcmp(name, NG_CISCO_HOOK_DEBUG) == 0) {
249                 hook->private = NULL;   /* unimplemented */
250         } else
251                 return (EINVAL);
252         return 0;
253 }
254
255 /*
256  * Receive control message.
257  */
258 static int
259 cisco_rcvmsg(node_p node, struct ng_mesg *msg,
260         const char *retaddr, struct ng_mesg **rptr)
261 {
262         const sc_p sc = node->private;
263         struct ng_mesg *resp = NULL;
264         int error = 0;
265
266         switch (msg->header.typecookie) {
267         case NGM_GENERIC_COOKIE:
268                 switch (msg->header.cmd) {
269                 case NGM_TEXT_STATUS:
270                     {
271                         char *arg;
272                         int pos;
273
274                         NG_MKRESPONSE(resp, msg, sizeof(struct ng_mesg)
275                             + NG_TEXTRESPONSE, M_NOWAIT);
276                         if (resp == NULL) {
277                                 error = ENOMEM;
278                                 break;
279                         }
280                         arg = (char *) resp->data;
281                         pos = ksprintf(arg,
282                           "keepalive period: %d sec; ", KEEPALIVE_SECS);
283                         pos += ksprintf(arg + pos,
284                           "unacknowledged keepalives: %ld", sc->seqRetries);
285                         resp->header.arglen = pos + 1;
286                         break;
287                     }
288                 default:
289                         error = EINVAL;
290                         break;
291                 }
292                 break;
293         case NGM_CISCO_COOKIE:
294                 switch (msg->header.cmd) {
295                 case NGM_CISCO_GET_IPADDR:      /* could be a late reply! */
296                         if ((msg->header.flags & NGF_RESP) == 0) {
297                                 struct in_addr *ips;
298
299                                 NG_MKRESPONSE(resp, msg,
300                                     2 * sizeof(*ips), M_NOWAIT);
301                                 if (!resp) {
302                                         error = ENOMEM;
303                                         break;
304                                 }
305                                 ips = (struct in_addr *) resp->data;
306                                 ips[0] = sc->localip;
307                                 ips[1] = sc->localmask;
308                                 break;
309                         }
310                         /* FALLTHROUGH */       /* ...if it's a reply */
311                 case NGM_CISCO_SET_IPADDR:
312                     {
313                         struct in_addr *const ips = (struct in_addr *)msg->data;
314
315                         if (msg->header.arglen < 2 * sizeof(*ips)) {
316                                 error = EINVAL;
317                                 break;
318                         }
319                         sc->localip = ips[0];
320                         sc->localmask = ips[1];
321                         break;
322                     }
323                 case NGM_CISCO_GET_STATUS:
324                     {
325                         struct ng_cisco_stats *stat;
326
327                         NG_MKRESPONSE(resp, msg, sizeof(*stat), M_NOWAIT);
328                         if (!resp) {
329                                 error = ENOMEM;
330                                 break;
331                         }
332                         stat = (struct ng_cisco_stats *)resp->data;
333                         stat->seqRetries = sc->seqRetries;
334                         stat->keepAlivePeriod = KEEPALIVE_SECS;
335                         break;
336                     }
337                 default:
338                         error = EINVAL;
339                         break;
340                 }
341                 break;
342         default:
343                 error = EINVAL;
344                 break;
345         }
346         if (rptr)
347                 *rptr = resp;
348         else if (resp)
349                 FREE(resp, M_NETGRAPH);
350         FREE(msg, M_NETGRAPH);
351         return (error);
352 }
353
354 /*
355  * Receive data
356  */
357 static int
358 cisco_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
359 {
360         const sc_p sc = hook->node->private;
361         struct protoent *pep;
362         struct cisco_header *h;
363         int error = 0;
364
365         if ((pep = hook->private) == NULL)
366                 goto out;
367
368         /* If it came from our downlink, deal with it separately */
369         if (pep->af == 0xffff)
370                 return (cisco_input(sc, m, meta));
371
372         /* OK so it came from a protocol, heading out. Prepend general data
373            packet header. For now, IP,IPX only  */
374         M_PREPEND(m, CISCO_HEADER_LEN, MB_DONTWAIT);
375         if (!m) {
376                 error = ENOBUFS;
377                 goto out;
378         }
379         h = mtod(m, struct cisco_header *);
380         h->address = CISCO_UNICAST;
381         h->control = 0;
382
383         switch (pep->af) {
384         case AF_INET:           /* Internet Protocol */
385                 h->protocol = htons(ETHERTYPE_IP);
386                 break;
387         case AF_INET6:
388                 h->protocol = htons(ETHERTYPE_IPV6);
389                 break;
390         case AF_APPLETALK:      /* AppleTalk Protocol */
391                 h->protocol = htons(ETHERTYPE_AT);
392                 break;
393         case AF_IPX:            /* Novell IPX Protocol */
394                 h->protocol = htons(ETHERTYPE_IPX);
395                 break;
396         default:
397                 error = EAFNOSUPPORT;
398                 goto out;
399         }
400
401         /* Send it */
402         NG_SEND_DATA(error, sc->downstream.hook, m, meta);
403         return (error);
404
405 out:
406         NG_FREE_DATA(m, meta);
407         return (error);
408 }
409
410 /*
411  * Shutdown node
412  */
413 static int
414 cisco_rmnode(node_p node)
415 {
416         const sc_p sc = node->private;
417
418         node->flags |= NG_INVALID;
419         ng_cutlinks(node);
420         ng_unname(node);
421         node->private = NULL;
422         ng_unref(sc->node);
423         FREE(sc, M_NETGRAPH);
424         return (0);
425 }
426
427 /*
428  * Disconnection of a hook
429  *
430  * For this type, removal of the last link destroys the node
431  */
432 static int
433 cisco_disconnect(hook_p hook)
434 {
435         const sc_p sc = hook->node->private;
436         struct protoent *pep;
437
438         /* Check it's not the debug hook */
439         if ((pep = hook->private)) {
440                 pep->hook = NULL;
441                 if (pep->af == 0xffff) {
442                         /* If it is the downstream hook, stop the timers */
443                         callout_stop(&sc->timeout);
444                 }
445         }
446
447         /* If no more hooks, remove the node */
448         if (hook->node->numhooks == 0)
449                 ng_rmnode(hook->node);
450         return (0);
451 }
452
453 /*
454  * Receive data
455  */
456 static int
457 cisco_input(sc_p sc, struct mbuf *m, meta_p meta)
458 {
459         const struct cisco_header *h;
460         struct cisco_header hdrbuf;
461         struct protoent *pep;
462         int error = 0;
463
464         /* Sanity check header length */
465         if (m->m_pkthdr.len < sizeof(*h)) {
466                 error = EINVAL;
467                 goto drop;
468         }
469
470         /* Get cisco header */
471         if (m->m_len >= sizeof(*h))                     /* the common case */
472                 h = mtod(m, const struct cisco_header *);
473         else {
474                 m_copydata(m, 0, sizeof(*h), (caddr_t)&hdrbuf);
475                 h = &hdrbuf;
476         }
477         m_adj(m, sizeof(*h));
478
479         /* Check header address */
480         switch (h->address) {
481         default:                /* Invalid Cisco packet. */
482                 goto drop;
483         case CISCO_UNICAST:
484         case CISCO_MULTICAST:
485                 /* Don't check the control field here (RFC 1547). */
486                 switch (ntohs(h->protocol)) {
487                 default:
488                         goto drop;
489                 case CISCO_KEEPALIVE:
490                     {
491                         const struct cisco_packet *p;
492                         struct cisco_packet pktbuf;
493
494                         /* Sanity check packet length */
495                         if (m->m_pkthdr.len < sizeof(*p)) {
496                                 error = EINVAL;
497                                 goto drop;
498                         }
499
500                         /* Get cisco packet */
501                         if (m->m_len >= sizeof(*p))     /* the common case */
502                                 p = mtod(m, const struct cisco_packet *);
503                         else {
504                                 m_copydata(m, 0, sizeof(*p), (caddr_t)&pktbuf);
505                                 p = &pktbuf;
506                         }
507
508                         /* Check packet type */
509                         switch (ntohl(p->type)) {
510                         default:
511                                 log(LOG_WARNING,
512                                     "cisco: unknown cisco packet type: 0x%"PRIx32"\n",
513                                        ntohl(p->type));
514                                 break;
515                         case CISCO_ADDR_REPLY:
516                                 /* Reply on address request, ignore */
517                                 break;
518                         case CISCO_KEEPALIVE_REQ:
519                                 sc->remote_seq = ntohl(p->par1);
520                                 if (sc->local_seq == ntohl(p->par2)) {
521                                         sc->local_seq++;
522                                         sc->seqRetries = 0;
523                                 }
524                                 break;
525                         case CISCO_ADDR_REQ:
526                             {
527                                 struct ng_mesg *msg, *resp;
528
529                                 /* Ask inet peer for IP address information */
530                                 if (sc->inet.hook == NULL)
531                                         goto nomsg;
532                                 NG_MKMESSAGE(msg, NGM_CISCO_COOKIE,
533                                     NGM_CISCO_GET_IPADDR, 0, M_NOWAIT);
534                                 if (msg == NULL)
535                                         goto nomsg;
536                                 ng_send_msg(sc->node, msg,
537                                     NG_CISCO_HOOK_INET, &resp);
538                                 if (resp != NULL)
539                                         cisco_rcvmsg(sc->node, resp, ".", NULL);
540
541                 nomsg:
542                                 /* Send reply to peer device */
543                                 error = cisco_send(sc, CISCO_ADDR_REPLY,
544                                             ntohl(sc->localip.s_addr),
545                                             ntohl(sc->localmask.s_addr));
546                                 break;
547                             }
548                         }
549                         goto drop;
550                     }
551                 case ETHERTYPE_IP:
552                         pep = &sc->inet;
553                         break;
554                 case ETHERTYPE_IPV6:
555                         pep = &sc->inet6;
556                         break;
557                 case ETHERTYPE_AT:
558                         pep = &sc->atalk;
559                         break;
560                 case ETHERTYPE_IPX:
561                         pep = &sc->ipx;
562                         break;
563                 }
564                 break;
565         }
566
567         /* Drop if payload is empty */
568         if (m->m_pkthdr.len == 0) {
569                 error = EINVAL;
570                 goto drop;
571         }
572
573         /* Send it on */
574         if (pep->hook == NULL)
575                 goto drop;
576         NG_SEND_DATA(error, pep->hook, m, meta);
577         return (error);
578
579 drop:
580         NG_FREE_DATA(m, meta);
581         return (error);
582 }
583
584
585 /*
586  * Send keepalive packets, every 10 seconds.
587  */
588 static void
589 cisco_keepalive(void *arg)
590 {
591         const sc_p sc = arg;
592
593         crit_enter();
594         cisco_send(sc, CISCO_KEEPALIVE_REQ, sc->local_seq, sc->remote_seq);
595         sc->seqRetries++;
596         crit_exit();
597         callout_reset(&sc->timeout, hz * KEEPALIVE_SECS, 
598                         cisco_keepalive, sc);
599 }
600
601 /*
602  * Send Cisco keepalive packet.
603  */
604 static int
605 cisco_send(sc_p sc, int type, long par1, long par2)
606 {
607         struct cisco_header *h;
608         struct cisco_packet *ch;
609         struct mbuf *m;
610         u_long  t;
611         int     error = 0;
612         meta_p  meta = NULL;
613         struct timeval time;
614
615         getmicrotime(&time);
616
617         MGETHDR(m, MB_DONTWAIT, MT_DATA);
618         if (!m)
619                 return (ENOBUFS);
620
621         t = (time.tv_sec - boottime.tv_sec) * 1000;
622         m->m_pkthdr.len = m->m_len = CISCO_HEADER_LEN + CISCO_PACKET_LEN;
623         m->m_pkthdr.rcvif = 0;
624
625         h = mtod(m, struct cisco_header *);
626         h->address = CISCO_MULTICAST;
627         h->control = 0;
628         h->protocol = htons(CISCO_KEEPALIVE);
629
630         ch = (struct cisco_packet *) (h + 1);
631         ch->type = htonl(type);
632         ch->par1 = htonl(par1);
633         ch->par2 = htonl(par2);
634         ch->rel = -1;
635         ch->time0 = htons((u_short) (t >> 16));
636         ch->time1 = htons((u_short) t);
637
638         NG_SEND_DATA(error, sc->downstream.hook, m, meta);
639         return (error);
640 }