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