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	NGI_GET_M(item, m);
363	M_PREPEND(m, CISCO_HEADER_LEN, M_NOWAIT);
364	if (!m) {
365		error = ENOBUFS;
366		goto out;
367	}
368	NGI_M(item) = m;
369	h = mtod(m, struct cisco_header *);
370	h->address = CISCO_UNICAST;
371	h->control = 0;
372
373	switch (pep->af) {
374	case AF_INET:		/* Internet Protocol */
375		h->protocol = htons(ETHERTYPE_IP);
376		break;
377	case AF_INET6:
378		h->protocol = htons(ETHERTYPE_IPV6);
379		break;
380	case AF_APPLETALK:	/* AppleTalk Protocol */
381		h->protocol = htons(ETHERTYPE_AT);
382		break;
383	case AF_IPX:		/* Novell IPX Protocol */
384		h->protocol = htons(ETHERTYPE_IPX);
385		break;
386	default:
387		error = EAFNOSUPPORT;
388		goto out;
389	}
390
391	/* Send it */
392	NG_FWD_NEW_DATA(error, item,  sc->downstream.hook, m);
393	return (error);
394
395out:
396	NG_FREE_ITEM(item);
397	return (error);
398}
399
400/*
401 * Shutdown node
402 */
403static int
404cisco_shutdown(node_p node)
405{
406	const sc_p sc = NG_NODE_PRIVATE(node);
407
408	NG_NODE_SET_PRIVATE(node, NULL);
409	NG_NODE_UNREF(sc->node);
410	free(sc, M_NETGRAPH);
411	return (0);
412}
413
414/*
415 * Disconnection of a hook
416 *
417 * For this type, removal of the last link destroys the node
418 */
419static int
420cisco_disconnect(hook_p hook)
421{
422	const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
423	struct protoent *pep;
424
425	/* Check it's not the debug hook */
426	if ((pep = NG_HOOK_PRIVATE(hook))) {
427		pep->hook = NULL;
428		if (pep->af == 0xffff)
429			/* If it is the downstream hook, stop the timers */
430			ng_uncallout(&sc->handle, NG_HOOK_NODE(hook));
431	}
432
433	/* If no more hooks, remove the node */
434	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
435	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
436		ng_rmnode_self(NG_HOOK_NODE(hook));
437	return (0);
438}
439
440/*
441 * Receive data
442 */
443static int
444cisco_input(sc_p sc, item_p item)
445{
446	const struct cisco_header *h;
447	struct cisco_header hdrbuf;
448	struct protoent *pep;
449	struct mbuf *m;
450	int error = 0;
451
452	/* Get data */
453	m = NGI_M(item);
454
455	/* Sanity check header length */
456	if (m->m_pkthdr.len < sizeof(*h)) {
457		error = EINVAL;
458		goto drop;
459	}
460
461	/* Get cisco header */
462	if (m->m_len >= sizeof(*h))			/* the common case */
463		h = mtod(m, const struct cisco_header *);
464	else {
465		m_copydata(m, 0, sizeof(*h), (caddr_t)&hdrbuf);
466		h = &hdrbuf;
467	}
468	m_adj(m, sizeof(*h));
469
470	/* Check header address */
471	switch (h->address) {
472	default:		/* Invalid Cisco packet. */
473		goto drop;
474	case CISCO_UNICAST:
475	case CISCO_MULTICAST:
476		/* Don't check the control field here (RFC 1547). */
477		switch (ntohs(h->protocol)) {
478		default:
479			goto drop;
480		case CISCO_KEEPALIVE:
481		    {
482			const struct cisco_packet *p;
483			struct cisco_packet pktbuf;
484
485			/* Sanity check packet length */
486			if (m->m_pkthdr.len < sizeof(*p)) {
487				error = EINVAL;
488				goto drop;
489			}
490
491			/* Get cisco packet */
492			if (m->m_len >= sizeof(*p))	/* the common case */
493				p = mtod(m, const struct cisco_packet *);
494			else {
495				m_copydata(m, 0, sizeof(*p), (caddr_t)&pktbuf);
496				p = &pktbuf;
497			}
498
499			/* Check packet type */
500			switch (ntohl(p->type)) {
501			default:
502				log(LOG_WARNING,
503				    "cisco: unknown cisco packet type: 0x%lx\n",
504				       (long)ntohl(p->type));
505				break;
506			case CISCO_ADDR_REPLY:
507				/* Reply on address request, ignore */
508				break;
509			case CISCO_KEEPALIVE_REQ:
510				sc->remote_seq = ntohl(p->par1);
511				if (sc->local_seq == ntohl(p->par2)) {
512					sc->local_seq++;
513					if (sc->seqRetries > 1)
514						cisco_notify(sc, NGM_LINK_IS_UP);
515					sc->seqRetries = 0;
516				}
517				break;
518			case CISCO_ADDR_REQ:
519			    {
520				struct ng_mesg *msg;
521				int dummy_error = 0;
522
523				/* Ask inet peer for IP address information */
524				if (sc->inet.hook == NULL)
525					goto nomsg;
526				NG_MKMESSAGE(msg, NGM_CISCO_COOKIE,
527				    NGM_CISCO_GET_IPADDR, 0, M_NOWAIT);
528				if (msg == NULL)
529					goto nomsg;
530				NG_SEND_MSG_HOOK(dummy_error,
531				    sc->node, msg, sc->inet.hook, 0);
532		/*
533		 * XXX Now maybe we should set a flag telling
534		 * our receiver to send this message when the response comes in
535		 * instead of now when the data may be bad.
536		 */
537		nomsg:
538				/* Send reply to peer device */
539				error = cisco_send(sc, CISCO_ADDR_REPLY,
540					    ntohl(sc->localip.s_addr),
541					    ntohl(sc->localmask.s_addr));
542				break;
543			    }
544			}
545			goto drop;
546		    }
547		case ETHERTYPE_IP:
548			pep = &sc->inet;
549			break;
550		case ETHERTYPE_IPV6:
551			pep = &sc->inet6;
552			break;
553		case ETHERTYPE_AT:
554			pep = &sc->atalk;
555			break;
556		case ETHERTYPE_IPX:
557			pep = &sc->ipx;
558			break;
559		}
560		break;
561	}
562
563	/* Drop if payload is empty */
564	if (m->m_pkthdr.len == 0) {
565		error = EINVAL;
566		goto drop;
567	}
568
569	/* Send it on */
570	if (pep->hook == NULL)
571		goto drop;
572	NG_FWD_NEW_DATA(error, item, pep->hook, m);
573	return (error);
574
575drop:
576	NG_FREE_ITEM(item);
577	return (error);
578}
579
580
581/*
582 * Send keepalive packets, every 10 seconds.
583 */
584static void
585cisco_keepalive(node_p node, hook_p hook, void *arg1, int arg2)
586{
587	const sc_p sc = arg1;
588
589	cisco_send(sc, CISCO_KEEPALIVE_REQ, sc->local_seq, sc->remote_seq);
590	if (sc->seqRetries++ > 1)
591		cisco_notify(sc, NGM_LINK_IS_DOWN);
592	ng_callout(&sc->handle, node, NULL, (hz * KEEPALIVE_SECS),
593	    &cisco_keepalive, (void *)sc, 0);
594}
595
596/*
597 * Send Cisco keepalive packet.
598 */
599static int
600cisco_send(sc_p sc, int type, long par1, long par2)
601{
602	struct cisco_header *h;
603	struct cisco_packet *ch;
604	struct mbuf *m;
605	struct timeval time;
606	uint32_t t;
607	int     error = 0;
608
609	getmicrouptime(&time);
610
611	MGETHDR(m, M_NOWAIT, MT_DATA);
612	if (!m)
613		return (ENOBUFS);
614
615	t = time.tv_sec * 1000 + time.tv_usec / 1000;
616	m->m_pkthdr.len = m->m_len = CISCO_HEADER_LEN + CISCO_PACKET_LEN;
617	m->m_pkthdr.rcvif = 0;
618
619	h = mtod(m, struct cisco_header *);
620	h->address = CISCO_MULTICAST;
621	h->control = 0;
622	h->protocol = htons(CISCO_KEEPALIVE);
623
624	ch = (struct cisco_packet *) (h + 1);
625	ch->type = htonl(type);
626	ch->par1 = htonl(par1);
627	ch->par2 = htonl(par2);
628	ch->rel = -1;
629	ch->time0 = htons((uint16_t) (t >> 16));
630	ch->time1 = htons((uint16_t) t);
631
632	NG_SEND_DATA_ONLY(error, sc->downstream.hook, m);
633	return (error);
634}
635
636/*
637 * Send linkstate to upstream node.
638 */
639static void
640cisco_notify(sc_p sc, uint32_t cmd)
641{
642	struct ng_mesg *msg;
643	int dummy_error = 0;
644
645	if (sc->inet.hook == NULL) /* nothing to notify */
646		return;
647
648	NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
649	if (msg != NULL)
650		NG_SEND_MSG_HOOK(dummy_error, sc->node, msg, sc->inet.hook, 0);
651}
652