]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netgraph/ng_cisco.c
MFV r262756:
[FreeBSD/FreeBSD.git] / sys / netgraph / 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$
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 #include <net/if_var.h>         /* XXXGL: ipx? */
55
56 #include <netinet/in.h>
57 #include <netinet/if_ether.h>
58
59 #include <netatalk/at.h>
60
61 #include <netipx/ipx.h>
62 #include <netipx/ipx_if.h>
63
64 #include <netgraph/ng_message.h>
65 #include <netgraph/netgraph.h>
66 #include <netgraph/ng_parse.h>
67 #include <netgraph/ng_cisco.h>
68
69 #define CISCO_MULTICAST         0x8f    /* Cisco multicast address */
70 #define CISCO_UNICAST           0x0f    /* Cisco unicast address */
71 #define CISCO_KEEPALIVE         0x8035  /* Cisco keepalive protocol */
72 #define CISCO_ADDR_REQ          0       /* Cisco address request */
73 #define CISCO_ADDR_REPLY        1       /* Cisco address reply */
74 #define CISCO_KEEPALIVE_REQ     2       /* Cisco keepalive request */
75
76 #define KEEPALIVE_SECS          10
77
78 struct cisco_header {
79         uint8_t  address;
80         uint8_t  control;
81         uint16_t protocol;
82 } __packed;
83
84 #define CISCO_HEADER_LEN        sizeof (struct cisco_header)
85
86 struct cisco_packet {
87         uint32_t type;
88         uint32_t par1;
89         uint32_t par2;
90         uint16_t rel;
91         uint16_t time0;
92         uint16_t time1;
93 } __packed;
94
95 #define CISCO_PACKET_LEN (sizeof(struct cisco_packet))
96
97 struct protoent {
98         hook_p  hook;           /* the hook for this proto */
99         uint16_t af;            /* address family, -1 = downstream */
100 };
101
102 struct cisco_priv {
103         uint32_t local_seq;
104         uint32_t remote_seq;
105         uint32_t seqRetries;    /* how many times we've been here throwing out
106                                  * the same sequence number without ack */
107         node_p  node;
108         struct callout handle;
109         struct protoent downstream;
110         struct protoent inet;           /* IP information */
111         struct in_addr localip;
112         struct in_addr localmask;
113         struct protoent inet6;          /* IPv6 information */
114         struct protoent atalk;          /* AppleTalk information */
115         struct protoent ipx;            /* IPX information */
116 };
117 typedef struct cisco_priv *sc_p;
118
119 /* Netgraph methods */
120 static ng_constructor_t         cisco_constructor;
121 static ng_rcvmsg_t              cisco_rcvmsg;
122 static ng_shutdown_t            cisco_shutdown;
123 static ng_newhook_t             cisco_newhook;
124 static ng_rcvdata_t             cisco_rcvdata;
125 static ng_disconnect_t          cisco_disconnect;
126
127 /* Other functions */
128 static int      cisco_input(sc_p sc, item_p item);
129 static void     cisco_keepalive(node_p node, hook_p hook, void *arg1, int arg2);
130 static int      cisco_send(sc_p sc, int type, long par1, long par2);
131 static void     cisco_notify(sc_p sc, uint32_t cmd);
132
133 /* Parse type for struct ng_cisco_ipaddr */
134 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
135         = NG_CISCO_IPADDR_TYPE_INFO;
136 static const struct ng_parse_type ng_cisco_ipaddr_type = {
137         &ng_parse_struct_type,
138         &ng_cisco_ipaddr_type_fields
139 };
140
141 /* Parse type for struct ng_async_stat */
142 static const struct ng_parse_struct_field ng_cisco_stats_type_fields[]
143         = NG_CISCO_STATS_TYPE_INFO;
144 static const struct ng_parse_type ng_cisco_stats_type = {
145         &ng_parse_struct_type,
146         &ng_cisco_stats_type_fields
147 };
148
149 /* List of commands and how to convert arguments to/from ASCII */
150 static const struct ng_cmdlist ng_cisco_cmdlist[] = {
151         {
152           NGM_CISCO_COOKIE,
153           NGM_CISCO_SET_IPADDR,
154           "setipaddr",
155           &ng_cisco_ipaddr_type,
156           NULL
157         },
158         {
159           NGM_CISCO_COOKIE,
160           NGM_CISCO_GET_IPADDR,
161           "getipaddr",
162           NULL,
163           &ng_cisco_ipaddr_type
164         },
165         {
166           NGM_CISCO_COOKIE,
167           NGM_CISCO_GET_STATUS,
168           "getstats",
169           NULL,
170           &ng_cisco_stats_type
171         },
172         { 0 }
173 };
174
175 /* Node type */
176 static struct ng_type typestruct = {
177         .version =      NG_ABI_VERSION,
178         .name =         NG_CISCO_NODE_TYPE,
179         .constructor =  cisco_constructor,
180         .rcvmsg =       cisco_rcvmsg,
181         .shutdown =     cisco_shutdown,
182         .newhook =      cisco_newhook,
183         .rcvdata =      cisco_rcvdata,
184         .disconnect =   cisco_disconnect,
185         .cmdlist =      ng_cisco_cmdlist,
186 };
187 NETGRAPH_INIT(cisco, &typestruct);
188
189 /*
190  * Node constructor
191  */
192 static int
193 cisco_constructor(node_p node)
194 {
195         sc_p sc;
196
197         sc = malloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
198
199         ng_callout_init(&sc->handle);
200         NG_NODE_SET_PRIVATE(node, sc);
201         sc->node = node;
202
203         /* Initialise the varous protocol hook holders */
204         sc->downstream.af = 0xffff;
205         sc->inet.af = AF_INET;
206         sc->inet6.af = AF_INET6;
207         sc->atalk.af = AF_APPLETALK;
208         sc->ipx.af = AF_IPX;
209         return (0);
210 }
211
212 /*
213  * Check new hook
214  */
215 static int
216 cisco_newhook(node_p node, hook_p hook, const char *name)
217 {
218         const sc_p sc = NG_NODE_PRIVATE(node);
219
220         if (strcmp(name, NG_CISCO_HOOK_DOWNSTREAM) == 0) {
221                 sc->downstream.hook = hook;
222                 NG_HOOK_SET_PRIVATE(hook, &sc->downstream);
223
224                 /* Start keepalives */
225                 ng_callout(&sc->handle, node, NULL, (hz * KEEPALIVE_SECS),
226                     &cisco_keepalive, (void *)sc, 0);
227         } else if (strcmp(name, NG_CISCO_HOOK_INET) == 0) {
228                 sc->inet.hook = hook;
229                 NG_HOOK_SET_PRIVATE(hook, &sc->inet);
230         } else if (strcmp(name, NG_CISCO_HOOK_INET6) == 0) {
231                 sc->inet6.hook = hook;
232                 NG_HOOK_SET_PRIVATE(hook, &sc->inet6);
233         } else if (strcmp(name, NG_CISCO_HOOK_APPLETALK) == 0) {
234                 sc->atalk.hook = hook;
235                 NG_HOOK_SET_PRIVATE(hook, &sc->atalk);
236         } else if (strcmp(name, NG_CISCO_HOOK_IPX) == 0) {
237                 sc->ipx.hook = hook;
238                 NG_HOOK_SET_PRIVATE(hook, &sc->ipx);
239         } else if (strcmp(name, NG_CISCO_HOOK_DEBUG) == 0) {
240                 NG_HOOK_SET_PRIVATE(hook, NULL);        /* unimplemented */
241         } else
242                 return (EINVAL);
243         return 0;
244 }
245
246 /*
247  * Receive control message.
248  */
249 static int
250 cisco_rcvmsg(node_p node, item_p item, hook_p lasthook)
251 {
252         struct ng_mesg *msg;
253         const sc_p sc = NG_NODE_PRIVATE(node);
254         struct ng_mesg *resp = NULL;
255         int error = 0;
256
257         NGI_GET_MSG(item, msg);
258         switch (msg->header.typecookie) {
259         case NGM_GENERIC_COOKIE:
260                 switch (msg->header.cmd) {
261                 case NGM_TEXT_STATUS:
262                     {
263                         char *arg;
264                         int pos;
265
266                         NG_MKRESPONSE(resp, msg, NG_TEXTRESPONSE, M_NOWAIT);
267                         if (resp == NULL) {
268                                 error = ENOMEM;
269                                 break;
270                         }
271                         arg = (char *) resp->data;
272                         pos = sprintf(arg,
273                           "keepalive period: %d sec; ", KEEPALIVE_SECS);
274                         pos += sprintf(arg + pos,
275                           "unacknowledged keepalives: %d", sc->seqRetries);
276                         resp->header.arglen = pos + 1;
277                         break;
278                     }
279                 default:
280                         error = EINVAL;
281                         break;
282                 }
283                 break;
284         case NGM_CISCO_COOKIE:
285                 switch (msg->header.cmd) {
286                 case NGM_CISCO_GET_IPADDR:      /* could be a late reply! */
287                         if ((msg->header.flags & NGF_RESP) == 0) {
288                                 struct in_addr *ips;
289
290                                 NG_MKRESPONSE(resp, msg,
291                                     2 * sizeof(*ips), M_NOWAIT);
292                                 if (!resp) {
293                                         error = ENOMEM;
294                                         break;
295                                 }
296                                 ips = (struct in_addr *) resp->data;
297                                 ips[0] = sc->localip;
298                                 ips[1] = sc->localmask;
299                                 break;
300                         }
301                         /* FALLTHROUGH */       /* ...if it's a reply */
302                 case NGM_CISCO_SET_IPADDR:
303                     {
304                         struct in_addr *const ips = (struct in_addr *)msg->data;
305
306                         if (msg->header.arglen < 2 * sizeof(*ips)) {
307                                 error = EINVAL;
308                                 break;
309                         }
310                         sc->localip = ips[0];
311                         sc->localmask = ips[1];
312                         break;
313                     }
314                 case NGM_CISCO_GET_STATUS:
315                     {
316                         struct ng_cisco_stats *stat;
317
318                         NG_MKRESPONSE(resp, msg, sizeof(*stat), M_NOWAIT);
319                         if (!resp) {
320                                 error = ENOMEM;
321                                 break;
322                         }
323                         stat = (struct ng_cisco_stats *)resp->data;
324                         stat->seqRetries = sc->seqRetries;
325                         stat->keepAlivePeriod = KEEPALIVE_SECS;
326                         break;
327                     }
328                 default:
329                         error = EINVAL;
330                         break;
331                 }
332                 break;
333         default:
334                 error = EINVAL;
335                 break;
336         }
337         NG_RESPOND_MSG(error, node, item, resp);
338         NG_FREE_MSG(msg);
339         return (error);
340 }
341
342 /*
343  * Receive data
344  */
345 static int
346 cisco_rcvdata(hook_p hook, item_p item)
347 {
348         const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
349         struct protoent *pep;
350         struct cisco_header *h;
351         struct mbuf *m;
352         int error = 0;
353
354         if ((pep = NG_HOOK_PRIVATE(hook)) == 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, item));
360
361         /* OK so it came from a protocol, heading out. Prepend general data
362            packet header. For now, IP,IPX only  */
363         NGI_GET_M(item, m);
364         M_PREPEND(m, CISCO_HEADER_LEN, M_NOWAIT);
365         if (!m) {
366                 error = ENOBUFS;
367                 goto out;
368         }
369         NGI_M(item) = m;
370         h = mtod(m, struct cisco_header *);
371         h->address = CISCO_UNICAST;
372         h->control = 0;
373
374         switch (pep->af) {
375         case AF_INET:           /* Internet Protocol */
376                 h->protocol = htons(ETHERTYPE_IP);
377                 break;
378         case AF_INET6:
379                 h->protocol = htons(ETHERTYPE_IPV6);
380                 break;
381         case AF_APPLETALK:      /* AppleTalk Protocol */
382                 h->protocol = htons(ETHERTYPE_AT);
383                 break;
384         case AF_IPX:            /* Novell IPX Protocol */
385                 h->protocol = htons(ETHERTYPE_IPX);
386                 break;
387         default:
388                 error = EAFNOSUPPORT;
389                 goto out;
390         }
391
392         /* Send it */
393         NG_FWD_NEW_DATA(error, item,  sc->downstream.hook, m);
394         return (error);
395
396 out:
397         NG_FREE_ITEM(item);
398         return (error);
399 }
400
401 /*
402  * Shutdown node
403  */
404 static int
405 cisco_shutdown(node_p node)
406 {
407         const sc_p sc = NG_NODE_PRIVATE(node);
408
409         NG_NODE_SET_PRIVATE(node, NULL);
410         NG_NODE_UNREF(sc->node);
411         free(sc, M_NETGRAPH);
412         return (0);
413 }
414
415 /*
416  * Disconnection of a hook
417  *
418  * For this type, removal of the last link destroys the node
419  */
420 static int
421 cisco_disconnect(hook_p hook)
422 {
423         const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
424         struct protoent *pep;
425
426         /* Check it's not the debug hook */
427         if ((pep = NG_HOOK_PRIVATE(hook))) {
428                 pep->hook = NULL;
429                 if (pep->af == 0xffff)
430                         /* If it is the downstream hook, stop the timers */
431                         ng_uncallout(&sc->handle, NG_HOOK_NODE(hook));
432         }
433
434         /* If no more hooks, remove the node */
435         if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
436         && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
437                 ng_rmnode_self(NG_HOOK_NODE(hook));
438         return (0);
439 }
440
441 /*
442  * Receive data
443  */
444 static int
445 cisco_input(sc_p sc, item_p item)
446 {
447         const struct cisco_header *h;
448         struct cisco_header hdrbuf;
449         struct protoent *pep;
450         struct mbuf *m;
451         int error = 0;
452
453         /* Get data */
454         m = NGI_M(item);
455
456         /* Sanity check header length */
457         if (m->m_pkthdr.len < sizeof(*h)) {
458                 error = EINVAL;
459                 goto drop;
460         }
461
462         /* Get cisco header */
463         if (m->m_len >= sizeof(*h))                     /* the common case */
464                 h = mtod(m, const struct cisco_header *);
465         else {
466                 m_copydata(m, 0, sizeof(*h), (caddr_t)&hdrbuf);
467                 h = &hdrbuf;
468         }
469         m_adj(m, sizeof(*h));
470
471         /* Check header address */
472         switch (h->address) {
473         default:                /* Invalid Cisco packet. */
474                 goto drop;
475         case CISCO_UNICAST:
476         case CISCO_MULTICAST:
477                 /* Don't check the control field here (RFC 1547). */
478                 switch (ntohs(h->protocol)) {
479                 default:
480                         goto drop;
481                 case CISCO_KEEPALIVE:
482                     {
483                         const struct cisco_packet *p;
484                         struct cisco_packet pktbuf;
485
486                         /* Sanity check packet length */
487                         if (m->m_pkthdr.len < sizeof(*p)) {
488                                 error = EINVAL;
489                                 goto drop;
490                         }
491
492                         /* Get cisco packet */
493                         if (m->m_len >= sizeof(*p))     /* the common case */
494                                 p = mtod(m, const struct cisco_packet *);
495                         else {
496                                 m_copydata(m, 0, sizeof(*p), (caddr_t)&pktbuf);
497                                 p = &pktbuf;
498                         }
499
500                         /* Check packet type */
501                         switch (ntohl(p->type)) {
502                         default:
503                                 log(LOG_WARNING,
504                                     "cisco: unknown cisco packet type: 0x%lx\n",
505                                        (long)ntohl(p->type));
506                                 break;
507                         case CISCO_ADDR_REPLY:
508                                 /* Reply on address request, ignore */
509                                 break;
510                         case CISCO_KEEPALIVE_REQ:
511                                 sc->remote_seq = ntohl(p->par1);
512                                 if (sc->local_seq == ntohl(p->par2)) {
513                                         sc->local_seq++;
514                                         if (sc->seqRetries > 1)
515                                                 cisco_notify(sc, NGM_LINK_IS_UP);
516                                         sc->seqRetries = 0;
517                                 }
518                                 break;
519                         case CISCO_ADDR_REQ:
520                             {
521                                 struct ng_mesg *msg;
522                                 int dummy_error = 0;
523
524                                 /* Ask inet peer for IP address information */
525                                 if (sc->inet.hook == NULL)
526                                         goto nomsg;
527                                 NG_MKMESSAGE(msg, NGM_CISCO_COOKIE,
528                                     NGM_CISCO_GET_IPADDR, 0, M_NOWAIT);
529                                 if (msg == NULL)
530                                         goto nomsg;
531                                 NG_SEND_MSG_HOOK(dummy_error,
532                                     sc->node, msg, sc->inet.hook, 0);
533                 /*
534                  * XXX Now maybe we should set a flag telling
535                  * our receiver to send this message when the response comes in
536                  * instead of now when the data may be bad.
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_FWD_NEW_DATA(error, item, pep->hook, m);
574         return (error);
575
576 drop:
577         NG_FREE_ITEM(item);
578         return (error);
579 }
580
581
582 /*
583  * Send keepalive packets, every 10 seconds.
584  */
585 static void
586 cisco_keepalive(node_p node, hook_p hook, void *arg1, int arg2)
587 {
588         const sc_p sc = arg1;
589
590         cisco_send(sc, CISCO_KEEPALIVE_REQ, sc->local_seq, sc->remote_seq);
591         if (sc->seqRetries++ > 1)
592                 cisco_notify(sc, NGM_LINK_IS_DOWN);
593         ng_callout(&sc->handle, node, NULL, (hz * KEEPALIVE_SECS),
594             &cisco_keepalive, (void *)sc, 0);
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         struct timeval time;
607         uint32_t t;
608         int     error = 0;
609
610         getmicrouptime(&time);
611
612         MGETHDR(m, M_NOWAIT, MT_DATA);
613         if (!m)
614                 return (ENOBUFS);
615
616         t = time.tv_sec * 1000 + time.tv_usec / 1000;
617         m->m_pkthdr.len = m->m_len = CISCO_HEADER_LEN + CISCO_PACKET_LEN;
618         m->m_pkthdr.rcvif = 0;
619
620         h = mtod(m, struct cisco_header *);
621         h->address = CISCO_MULTICAST;
622         h->control = 0;
623         h->protocol = htons(CISCO_KEEPALIVE);
624
625         ch = (struct cisco_packet *) (h + 1);
626         ch->type = htonl(type);
627         ch->par1 = htonl(par1);
628         ch->par2 = htonl(par2);
629         ch->rel = -1;
630         ch->time0 = htons((uint16_t) (t >> 16));
631         ch->time1 = htons((uint16_t) t);
632
633         NG_SEND_DATA_ONLY(error, sc->downstream.hook, m);
634         return (error);
635 }
636
637 /*
638  * Send linkstate to upstream node.
639  */
640 static void
641 cisco_notify(sc_p sc, uint32_t cmd)
642 {
643         struct ng_mesg *msg;
644         int dummy_error = 0;
645
646         if (sc->inet.hook == NULL) /* nothing to notify */
647                 return;
648                 
649         NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
650         if (msg != NULL)
651                 NG_SEND_MSG_HOOK(dummy_error, sc->node, msg, sc->inet.hook, 0);
652 }