kernel tree reorganization stage 1: Major cvs repository work (not logged as
[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.3 2003/08/07 21:17:31 dillon 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 <netatalk/at.h>
59
60 #include <netipx/ipx.h>
61 #include <netipx/ipx_if.h>
62
63 #include <netgraph/ng_message.h>
64 #include <netgraph/netgraph.h>
65 #include <netgraph/ng_parse.h>
66 #include "ng_cisco.h"
67
68 #define CISCO_MULTICAST         0x8f    /* Cisco multicast address */
69 #define CISCO_UNICAST           0x0f    /* Cisco unicast address */
70 #define CISCO_KEEPALIVE         0x8035  /* Cisco keepalive protocol */
71 #define CISCO_ADDR_REQ          0       /* Cisco address request */
72 #define CISCO_ADDR_REPLY        1       /* Cisco address reply */
73 #define CISCO_KEEPALIVE_REQ     2       /* Cisco keepalive request */
74
75 #define KEEPALIVE_SECS          10
76
77 struct cisco_header {
78         u_char  address;
79         u_char  control;
80         u_short protocol;
81 };
82
83 #define CISCO_HEADER_LEN          sizeof (struct cisco_header)
84
85 struct cisco_packet {
86         u_long  type;
87         u_long  par1;
88         u_long  par2;
89         u_short rel;
90         u_short time0;
91         u_short time1;
92 };
93
94 #define CISCO_PACKET_LEN (sizeof(struct cisco_packet))
95
96 struct protoent {
97         hook_p  hook;           /* the hook for this proto */
98         u_short af;             /* address family, -1 = downstream */
99 };
100
101 struct cisco_priv {
102         u_long  local_seq;
103         u_long  remote_seq;
104         u_long  seqRetries;     /* how many times we've been here throwing out
105                                  * the same sequence number without ack */
106         node_p  node;
107         struct callout_handle handle;
108         struct protoent downstream;
109         struct protoent inet;           /* IP information */
110         struct in_addr localip;
111         struct in_addr localmask;
112         struct protoent inet6;          /* IPv6 information */
113         struct protoent atalk;          /* AppleTalk information */
114         struct protoent ipx;            /* IPX information */
115 };
116 typedef struct cisco_priv *sc_p;
117
118 /* Netgraph methods */
119 static ng_constructor_t         cisco_constructor;
120 static ng_rcvmsg_t              cisco_rcvmsg;
121 static ng_shutdown_t            cisco_rmnode;
122 static ng_newhook_t             cisco_newhook;
123 static ng_rcvdata_t             cisco_rcvdata;
124 static ng_disconnect_t          cisco_disconnect;
125
126 /* Other functions */
127 static int      cisco_input(sc_p sc, struct mbuf *m, meta_p meta);
128 static void     cisco_keepalive(void *arg);
129 static int      cisco_send(sc_p sc, int type, long par1, long par2);
130
131 /* Parse type for struct ng_cisco_ipaddr */
132 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
133         = NG_CISCO_IPADDR_TYPE_INFO;
134 static const struct ng_parse_type ng_cisco_ipaddr_type = {
135         &ng_parse_struct_type,
136         &ng_cisco_ipaddr_type_fields
137 };
138
139 /* Parse type for struct ng_async_stat */
140 static const struct ng_parse_struct_field ng_cisco_stats_type_fields[]
141         = NG_CISCO_STATS_TYPE_INFO;
142 static const struct ng_parse_type ng_cisco_stats_type = {
143         &ng_parse_struct_type,
144         &ng_cisco_stats_type_fields
145 };
146
147 /* List of commands and how to convert arguments to/from ASCII */
148 static const struct ng_cmdlist ng_cisco_cmdlist[] = {
149         {
150           NGM_CISCO_COOKIE,
151           NGM_CISCO_SET_IPADDR,
152           "setipaddr",
153           &ng_cisco_ipaddr_type,
154           NULL
155         },
156         {
157           NGM_CISCO_COOKIE,
158           NGM_CISCO_GET_IPADDR,
159           "getipaddr",
160           NULL,
161           &ng_cisco_ipaddr_type
162         },
163         {
164           NGM_CISCO_COOKIE,
165           NGM_CISCO_GET_STATUS,
166           "getstats",
167           NULL,
168           &ng_cisco_stats_type
169         },
170         { 0 }
171 };
172
173 /* Node type */
174 static struct ng_type typestruct = {
175         NG_VERSION,
176         NG_CISCO_NODE_TYPE,
177         NULL,
178         cisco_constructor,
179         cisco_rcvmsg,
180         cisco_rmnode,
181         cisco_newhook,
182         NULL,
183         NULL,
184         cisco_rcvdata,
185         cisco_rcvdata,
186         cisco_disconnect,
187         ng_cisco_cmdlist
188 };
189 NETGRAPH_INIT(cisco, &typestruct);
190
191 /*
192  * Node constructor
193  */
194 static int
195 cisco_constructor(node_p *nodep)
196 {
197         sc_p sc;
198         int error = 0;
199
200         MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_NOWAIT);
201         if (sc == NULL)
202                 return (ENOMEM);
203         bzero(sc, sizeof(struct cisco_priv));
204
205         callout_handle_init(&sc->handle);
206         if ((error = ng_make_node_common(&typestruct, nodep))) {
207                 FREE(sc, M_NETGRAPH);
208                 return (error);
209         }
210         (*nodep)->private = sc;
211         sc->node = *nodep;
212
213         /* Initialise the varous protocol hook holders */
214         sc->downstream.af = 0xffff;
215         sc->inet.af = AF_INET;
216         sc->inet6.af = AF_INET6;
217         sc->atalk.af = AF_APPLETALK;
218         sc->ipx.af = AF_IPX;
219         return (0);
220 }
221
222 /*
223  * Check new hook
224  */
225 static int
226 cisco_newhook(node_p node, hook_p hook, const char *name)
227 {
228         const sc_p sc = node->private;
229
230         if (strcmp(name, NG_CISCO_HOOK_DOWNSTREAM) == 0) {
231                 sc->downstream.hook = hook;
232                 hook->private = &sc->downstream;
233
234                 /* Start keepalives */
235                 sc->handle = timeout(cisco_keepalive, sc, hz * KEEPALIVE_SECS);
236         } else if (strcmp(name, NG_CISCO_HOOK_INET) == 0) {
237                 sc->inet.hook = hook;
238                 hook->private = &sc->inet;
239         } else if (strcmp(name, NG_CISCO_HOOK_APPLETALK) == 0) {
240                 sc->atalk.hook = hook;
241                 hook->private = &sc->atalk;
242         } else if (strcmp(name, NG_CISCO_HOOK_IPX) == 0) {
243                 sc->ipx.hook = hook;
244                 hook->private = &sc->ipx;
245         } else if (strcmp(name, NG_CISCO_HOOK_DEBUG) == 0) {
246                 hook->private = NULL;   /* unimplemented */
247         } else
248                 return (EINVAL);
249         return 0;
250 }
251
252 /*
253  * Receive control message.
254  */
255 static int
256 cisco_rcvmsg(node_p node, struct ng_mesg *msg,
257         const char *retaddr, struct ng_mesg **rptr)
258 {
259         const sc_p sc = node->private;
260         struct ng_mesg *resp = NULL;
261         int error = 0;
262
263         switch (msg->header.typecookie) {
264         case NGM_GENERIC_COOKIE:
265                 switch (msg->header.cmd) {
266                 case NGM_TEXT_STATUS:
267                     {
268                         char *arg;
269                         int pos;
270
271                         NG_MKRESPONSE(resp, msg, sizeof(struct ng_mesg)
272                             + NG_TEXTRESPONSE, M_NOWAIT);
273                         if (resp == NULL) {
274                                 error = ENOMEM;
275                                 break;
276                         }
277                         arg = (char *) resp->data;
278                         pos = sprintf(arg,
279                           "keepalive period: %d sec; ", KEEPALIVE_SECS);
280                         pos += sprintf(arg + pos,
281                           "unacknowledged keepalives: %ld", sc->seqRetries);
282                         resp->header.arglen = pos + 1;
283                         break;
284                     }
285                 default:
286                         error = EINVAL;
287                         break;
288                 }
289                 break;
290         case NGM_CISCO_COOKIE:
291                 switch (msg->header.cmd) {
292                 case NGM_CISCO_GET_IPADDR:      /* could be a late reply! */
293                         if ((msg->header.flags & NGF_RESP) == 0) {
294                                 struct in_addr *ips;
295
296                                 NG_MKRESPONSE(resp, msg,
297                                     2 * sizeof(*ips), M_NOWAIT);
298                                 if (!resp) {
299                                         error = ENOMEM;
300                                         break;
301                                 }
302                                 ips = (struct in_addr *) resp->data;
303                                 ips[0] = sc->localip;
304                                 ips[1] = sc->localmask;
305                                 break;
306                         }
307                         /* FALLTHROUGH */       /* ...if it's a reply */
308                 case NGM_CISCO_SET_IPADDR:
309                     {
310                         struct in_addr *const ips = (struct in_addr *)msg->data;
311
312                         if (msg->header.arglen < 2 * sizeof(*ips)) {
313                                 error = EINVAL;
314                                 break;
315                         }
316                         sc->localip = ips[0];
317                         sc->localmask = ips[1];
318                         break;
319                     }
320                 case NGM_CISCO_GET_STATUS:
321                     {
322                         struct ng_cisco_stats *stat;
323
324                         NG_MKRESPONSE(resp, msg, sizeof(*stat), M_NOWAIT);
325                         if (!resp) {
326                                 error = ENOMEM;
327                                 break;
328                         }
329                         stat = (struct ng_cisco_stats *)resp->data;
330                         stat->seqRetries = sc->seqRetries;
331                         stat->keepAlivePeriod = KEEPALIVE_SECS;
332                         break;
333                     }
334                 default:
335                         error = EINVAL;
336                         break;
337                 }
338                 break;
339         default:
340                 error = EINVAL;
341                 break;
342         }
343         if (rptr)
344                 *rptr = resp;
345         else if (resp)
346                 FREE(resp, M_NETGRAPH);
347         FREE(msg, M_NETGRAPH);
348         return (error);
349 }
350
351 /*
352  * Receive data
353  */
354 static int
355 cisco_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
356 {
357         const sc_p sc = hook->node->private;
358         struct protoent *pep;
359         struct cisco_header *h;
360         int error = 0;
361
362         if ((pep = hook->private) == NULL)
363                 goto out;
364
365         /* If it came from our downlink, deal with it separately */
366         if (pep->af == 0xffff)
367                 return (cisco_input(sc, m, meta));
368
369         /* OK so it came from a protocol, heading out. Prepend general data
370            packet header. For now, IP,IPX only  */
371         M_PREPEND(m, CISCO_HEADER_LEN, M_DONTWAIT);
372         if (!m) {
373                 error = ENOBUFS;
374                 goto out;
375         }
376         h = mtod(m, struct cisco_header *);
377         h->address = CISCO_UNICAST;
378         h->control = 0;
379
380         switch (pep->af) {
381         case AF_INET:           /* Internet Protocol */
382                 h->protocol = htons(ETHERTYPE_IP);
383                 break;
384         case AF_INET6:
385                 h->protocol = htons(ETHERTYPE_IPV6);
386                 break;
387         case AF_APPLETALK:      /* AppleTalk Protocol */
388                 h->protocol = htons(ETHERTYPE_AT);
389                 break;
390         case AF_IPX:            /* Novell IPX Protocol */
391                 h->protocol = htons(ETHERTYPE_IPX);
392                 break;
393         default:
394                 error = EAFNOSUPPORT;
395                 goto out;
396         }
397
398         /* Send it */
399         NG_SEND_DATA(error, sc->downstream.hook, m, meta);
400         return (error);
401
402 out:
403         NG_FREE_DATA(m, meta);
404         return (error);
405 }
406
407 /*
408  * Shutdown node
409  */
410 static int
411 cisco_rmnode(node_p node)
412 {
413         const sc_p sc = node->private;
414
415         node->flags |= NG_INVALID;
416         ng_cutlinks(node);
417         ng_unname(node);
418         node->private = NULL;
419         ng_unref(sc->node);
420         FREE(sc, M_NETGRAPH);
421         return (0);
422 }
423
424 /*
425  * Disconnection of a hook
426  *
427  * For this type, removal of the last link destroys the node
428  */
429 static int
430 cisco_disconnect(hook_p hook)
431 {
432         const sc_p sc = hook->node->private;
433         struct protoent *pep;
434
435         /* Check it's not the debug hook */
436         if ((pep = hook->private)) {
437                 pep->hook = NULL;
438                 if (pep->af == 0xffff) {
439                         /* If it is the downstream hook, stop the timers */
440                         untimeout(cisco_keepalive, sc, sc->handle);
441                 }
442         }
443
444         /* If no more hooks, remove the node */
445         if (hook->node->numhooks == 0)
446                 ng_rmnode(hook->node);
447         return (0);
448 }
449
450 /*
451  * Receive data
452  */
453 static int
454 cisco_input(sc_p sc, struct mbuf *m, meta_p meta)
455 {
456         const struct cisco_header *h;
457         struct cisco_header hdrbuf;
458         struct protoent *pep;
459         int error = 0;
460
461         /* Sanity check header length */
462         if (m->m_pkthdr.len < sizeof(*h)) {
463                 error = EINVAL;
464                 goto drop;
465         }
466
467         /* Get cisco header */
468         if (m->m_len >= sizeof(*h))                     /* the common case */
469                 h = mtod(m, const struct cisco_header *);
470         else {
471                 m_copydata(m, 0, sizeof(*h), (caddr_t)&hdrbuf);
472                 h = &hdrbuf;
473         }
474         m_adj(m, sizeof(*h));
475
476         /* Check header address */
477         switch (h->address) {
478         default:                /* Invalid Cisco packet. */
479                 goto drop;
480         case CISCO_UNICAST:
481         case CISCO_MULTICAST:
482                 /* Don't check the control field here (RFC 1547). */
483                 switch (ntohs(h->protocol)) {
484                 default:
485                         goto drop;
486                 case CISCO_KEEPALIVE:
487                     {
488                         const struct cisco_packet *p;
489                         struct cisco_packet pktbuf;
490
491                         /* Sanity check packet length */
492                         if (m->m_pkthdr.len < sizeof(*p)) {
493                                 error = EINVAL;
494                                 goto drop;
495                         }
496
497                         /* Get cisco packet */
498                         if (m->m_len >= sizeof(*p))     /* the common case */
499                                 p = mtod(m, const struct cisco_packet *);
500                         else {
501                                 m_copydata(m, 0, sizeof(*p), (caddr_t)&pktbuf);
502                                 p = &pktbuf;
503                         }
504
505                         /* Check packet type */
506                         switch (ntohl(p->type)) {
507                         default:
508                                 log(LOG_WARNING,
509                                     "cisco: unknown cisco packet type: 0x%lx\n",
510                                        ntohl(p->type));
511                                 break;
512                         case CISCO_ADDR_REPLY:
513                                 /* Reply on address request, ignore */
514                                 break;
515                         case CISCO_KEEPALIVE_REQ:
516                                 sc->remote_seq = ntohl(p->par1);
517                                 if (sc->local_seq == ntohl(p->par2)) {
518                                         sc->local_seq++;
519                                         sc->seqRetries = 0;
520                                 }
521                                 break;
522                         case CISCO_ADDR_REQ:
523                             {
524                                 struct ng_mesg *msg, *resp;
525
526                                 /* Ask inet peer for IP address information */
527                                 if (sc->inet.hook == NULL)
528                                         goto nomsg;
529                                 NG_MKMESSAGE(msg, NGM_CISCO_COOKIE,
530                                     NGM_CISCO_GET_IPADDR, 0, M_NOWAIT);
531                                 if (msg == NULL)
532                                         goto nomsg;
533                                 ng_send_msg(sc->node, msg,
534                                     NG_CISCO_HOOK_INET, &resp);
535                                 if (resp != NULL)
536                                         cisco_rcvmsg(sc->node, resp, ".", NULL);
537
538                 nomsg:
539                                 /* Send reply to peer device */
540                                 error = cisco_send(sc, CISCO_ADDR_REPLY,
541                                             ntohl(sc->localip.s_addr),
542                                             ntohl(sc->localmask.s_addr));
543                                 break;
544                             }
545                         }
546                         goto drop;
547                     }
548                 case ETHERTYPE_IP:
549                         pep = &sc->inet;
550                         break;
551                 case ETHERTYPE_IPV6:
552                         pep = &sc->inet6;
553                         break;
554                 case ETHERTYPE_AT:
555                         pep = &sc->atalk;
556                         break;
557                 case ETHERTYPE_IPX:
558                         pep = &sc->ipx;
559                         break;
560                 }
561                 break;
562         }
563
564         /* Drop if payload is empty */
565         if (m->m_pkthdr.len == 0) {
566                 error = EINVAL;
567                 goto drop;
568         }
569
570         /* Send it on */
571         if (pep->hook == NULL)
572                 goto drop;
573         NG_SEND_DATA(error, pep->hook, m, meta);
574         return (error);
575
576 drop:
577         NG_FREE_DATA(m, meta);
578         return (error);
579 }
580
581
582 /*
583  * Send keepalive packets, every 10 seconds.
584  */
585 static void
586 cisco_keepalive(void *arg)
587 {
588         const sc_p sc = arg;
589         int s = splimp();
590
591         cisco_send(sc, CISCO_KEEPALIVE_REQ, sc->local_seq, sc->remote_seq);
592         sc->seqRetries++;
593         splx(s);
594         sc->handle = timeout(cisco_keepalive, sc, hz * KEEPALIVE_SECS);
595 }
596
597 /*
598  * Send Cisco keepalive packet.
599  */
600 static int
601 cisco_send(sc_p sc, int type, long par1, long par2)
602 {
603         struct cisco_header *h;
604         struct cisco_packet *ch;
605         struct mbuf *m;
606         u_long  t;
607         int     error = 0;
608         meta_p  meta = NULL;
609         struct timeval time;
610
611         getmicrotime(&time);
612
613         MGETHDR(m, M_DONTWAIT, MT_DATA);
614         if (!m)
615                 return (ENOBUFS);
616
617         t = (time.tv_sec - boottime.tv_sec) * 1000;
618         m->m_pkthdr.len = m->m_len = CISCO_HEADER_LEN + CISCO_PACKET_LEN;
619         m->m_pkthdr.rcvif = 0;
620
621         h = mtod(m, struct cisco_header *);
622         h->address = CISCO_MULTICAST;
623         h->control = 0;
624         h->protocol = htons(CISCO_KEEPALIVE);
625
626         ch = (struct cisco_packet *) (h + 1);
627         ch->type = htonl(type);
628         ch->par1 = htonl(par1);
629         ch->par2 = htonl(par2);
630         ch->rel = -1;
631         ch->time0 = htons((u_short) (t >> 16));
632         ch->time1 = htons((u_short) t);
633
634         NG_SEND_DATA(error, sc->downstream.hook, m, meta);
635         return (error);
636 }