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