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