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