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