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