xref: /freebsd/sys/netgraph/ng_cisco.c (revision f0a75d274af375d15b97b830966b99a02b7db911)
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 
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 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 };
116 typedef struct cisco_priv *sc_p;
117 
118 /* Netgraph methods */
119 static ng_constructor_t		cisco_constructor;
120 static ng_rcvmsg_t		cisco_rcvmsg;
121 static ng_shutdown_t		cisco_shutdown;
122 static ng_newhook_t		cisco_newhook;
123 static ng_rcvdata_t		cisco_rcvdata;
124 static ng_disconnect_t		cisco_disconnect;
125 
126 /* Other functions */
127 static int	cisco_input(sc_p sc, item_p item);
128 static void	cisco_keepalive(node_p node, hook_p hook, void *arg1, int arg2);
129 static int	cisco_send(sc_p sc, int type, long par1, long par2);
130 static void	cisco_notify(sc_p sc, uint32_t cmd);
131 
132 /* Parse type for struct ng_cisco_ipaddr */
133 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
134 	= NG_CISCO_IPADDR_TYPE_INFO;
135 static 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 */
141 static const struct ng_parse_struct_field ng_cisco_stats_type_fields[]
142 	= NG_CISCO_STATS_TYPE_INFO;
143 static 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 */
149 static 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 */
175 static 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 };
186 NETGRAPH_INIT(cisco, &typestruct);
187 
188 /*
189  * Node constructor
190  */
191 static int
192 cisco_constructor(node_p node)
193 {
194 	sc_p sc;
195 
196 	MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO);
197 	if (sc == NULL)
198 		return (ENOMEM);
199 
200 	ng_callout_init(&sc->handle);
201 	NG_NODE_SET_PRIVATE(node, sc);
202 	sc->node = node;
203 
204 	/* Initialise the varous protocol hook holders */
205 	sc->downstream.af = 0xffff;
206 	sc->inet.af = AF_INET;
207 	sc->inet6.af = AF_INET6;
208 	sc->atalk.af = AF_APPLETALK;
209 	sc->ipx.af = AF_IPX;
210 	return (0);
211 }
212 
213 /*
214  * Check new hook
215  */
216 static int
217 cisco_newhook(node_p node, hook_p hook, const char *name)
218 {
219 	const sc_p sc = NG_NODE_PRIVATE(node);
220 
221 	if (strcmp(name, NG_CISCO_HOOK_DOWNSTREAM) == 0) {
222 		sc->downstream.hook = hook;
223 		NG_HOOK_SET_PRIVATE(hook, &sc->downstream);
224 
225 		/* Start keepalives */
226 		ng_callout(&sc->handle, node, NULL, (hz * KEEPALIVE_SECS),
227 		    &cisco_keepalive, (void *)sc, 0);
228 	} else if (strcmp(name, NG_CISCO_HOOK_INET) == 0) {
229 		sc->inet.hook = hook;
230 		NG_HOOK_SET_PRIVATE(hook, &sc->inet);
231 	} else if (strcmp(name, NG_CISCO_HOOK_APPLETALK) == 0) {
232 		sc->atalk.hook = hook;
233 		NG_HOOK_SET_PRIVATE(hook, &sc->atalk);
234 	} else if (strcmp(name, NG_CISCO_HOOK_IPX) == 0) {
235 		sc->ipx.hook = hook;
236 		NG_HOOK_SET_PRIVATE(hook, &sc->ipx);
237 	} else if (strcmp(name, NG_CISCO_HOOK_DEBUG) == 0) {
238 		NG_HOOK_SET_PRIVATE(hook, NULL);	/* unimplemented */
239 	} else
240 		return (EINVAL);
241 	return 0;
242 }
243 
244 /*
245  * Receive control message.
246  */
247 static int
248 cisco_rcvmsg(node_p node, item_p item, hook_p lasthook)
249 {
250 	struct ng_mesg *msg;
251 	const sc_p sc = NG_NODE_PRIVATE(node);
252 	struct ng_mesg *resp = NULL;
253 	int error = 0;
254 
255 	NGI_GET_MSG(item, msg);
256 	switch (msg->header.typecookie) {
257 	case NGM_GENERIC_COOKIE:
258 		switch (msg->header.cmd) {
259 		case NGM_TEXT_STATUS:
260 		    {
261 			char *arg;
262 			int pos;
263 
264 			NG_MKRESPONSE(resp, msg, NG_TEXTRESPONSE, M_NOWAIT);
265 			if (resp == NULL) {
266 				error = ENOMEM;
267 				break;
268 			}
269 			arg = (char *) resp->data;
270 			pos = sprintf(arg,
271 			  "keepalive period: %d sec; ", KEEPALIVE_SECS);
272 			pos += sprintf(arg + pos,
273 			  "unacknowledged keepalives: %ld", sc->seqRetries);
274 			resp->header.arglen = pos + 1;
275 			break;
276 		    }
277 		default:
278 			error = EINVAL;
279 			break;
280 		}
281 		break;
282 	case NGM_CISCO_COOKIE:
283 		switch (msg->header.cmd) {
284 		case NGM_CISCO_GET_IPADDR:	/* could be a late reply! */
285 			if ((msg->header.flags & NGF_RESP) == 0) {
286 				struct in_addr *ips;
287 
288 				NG_MKRESPONSE(resp, msg,
289 				    2 * sizeof(*ips), M_NOWAIT);
290 				if (!resp) {
291 					error = ENOMEM;
292 					break;
293 				}
294 				ips = (struct in_addr *) resp->data;
295 				ips[0] = sc->localip;
296 				ips[1] = sc->localmask;
297 				break;
298 			}
299 			/* FALLTHROUGH */	/* ...if it's a reply */
300 		case NGM_CISCO_SET_IPADDR:
301 		    {
302 			struct in_addr *const ips = (struct in_addr *)msg->data;
303 
304 			if (msg->header.arglen < 2 * sizeof(*ips)) {
305 				error = EINVAL;
306 				break;
307 			}
308 			sc->localip = ips[0];
309 			sc->localmask = ips[1];
310 			break;
311 		    }
312 		case NGM_CISCO_GET_STATUS:
313 		    {
314 			struct ng_cisco_stats *stat;
315 
316 			NG_MKRESPONSE(resp, msg, sizeof(*stat), M_NOWAIT);
317 			if (!resp) {
318 				error = ENOMEM;
319 				break;
320 			}
321 			stat = (struct ng_cisco_stats *)resp->data;
322 			stat->seqRetries = sc->seqRetries;
323 			stat->keepAlivePeriod = KEEPALIVE_SECS;
324 			break;
325 		    }
326 		default:
327 			error = EINVAL;
328 			break;
329 		}
330 		break;
331 	default:
332 		error = EINVAL;
333 		break;
334 	}
335 	NG_RESPOND_MSG(error, node, item, resp);
336 	NG_FREE_MSG(msg);
337 	return (error);
338 }
339 
340 /*
341  * Receive data
342  */
343 static int
344 cisco_rcvdata(hook_p hook, item_p item)
345 {
346 	const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
347 	struct protoent *pep;
348 	struct cisco_header *h;
349 	struct mbuf *m;
350 	int error = 0;
351 
352 	if ((pep = NG_HOOK_PRIVATE(hook)) == NULL)
353 		goto out;
354 
355 	/* If it came from our downlink, deal with it separately */
356 	if (pep->af == 0xffff)
357 		return (cisco_input(sc, item));
358 
359 	/* OK so it came from a protocol, heading out. Prepend general data
360 	   packet header. For now, IP,IPX only  */
361 	m = NGI_M(item); /* still associated with item */
362 	M_PREPEND(m, CISCO_HEADER_LEN, M_DONTWAIT);
363 	if (!m) {
364 		error = ENOBUFS;
365 		goto out;
366 	}
367 	h = mtod(m, struct cisco_header *);
368 	h->address = CISCO_UNICAST;
369 	h->control = 0;
370 
371 	switch (pep->af) {
372 	case AF_INET:		/* Internet Protocol */
373 		h->protocol = htons(ETHERTYPE_IP);
374 		break;
375 	case AF_INET6:
376 		h->protocol = htons(ETHERTYPE_IPV6);
377 		break;
378 	case AF_APPLETALK:	/* AppleTalk Protocol */
379 		h->protocol = htons(ETHERTYPE_AT);
380 		break;
381 	case AF_IPX:		/* Novell IPX Protocol */
382 		h->protocol = htons(ETHERTYPE_IPX);
383 		break;
384 	default:
385 		error = EAFNOSUPPORT;
386 		goto out;
387 	}
388 
389 	/* Send it */
390 	NG_FWD_NEW_DATA(error, item,  sc->downstream.hook, m);
391 	return (error);
392 
393 out:
394 	NG_FREE_ITEM(item);
395 	return (error);
396 }
397 
398 /*
399  * Shutdown node
400  */
401 static int
402 cisco_shutdown(node_p node)
403 {
404 	const sc_p sc = NG_NODE_PRIVATE(node);
405 
406 	NG_NODE_SET_PRIVATE(node, NULL);
407 	NG_NODE_UNREF(sc->node);
408 	FREE(sc, M_NETGRAPH);
409 	return (0);
410 }
411 
412 /*
413  * Disconnection of a hook
414  *
415  * For this type, removal of the last link destroys the node
416  */
417 static int
418 cisco_disconnect(hook_p hook)
419 {
420 	const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
421 	struct protoent *pep;
422 
423 	/* Check it's not the debug hook */
424 	if ((pep = NG_HOOK_PRIVATE(hook))) {
425 		pep->hook = NULL;
426 		if (pep->af == 0xffff)
427 			/* If it is the downstream hook, stop the timers */
428 			ng_uncallout(&sc->handle, NG_HOOK_NODE(hook));
429 	}
430 
431 	/* If no more hooks, remove the node */
432 	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
433 	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
434 		ng_rmnode_self(NG_HOOK_NODE(hook));
435 	return (0);
436 }
437 
438 /*
439  * Receive data
440  */
441 static int
442 cisco_input(sc_p sc, item_p item)
443 {
444 	const struct cisco_header *h;
445 	struct cisco_header hdrbuf;
446 	struct protoent *pep;
447 	struct mbuf *m;
448 	int error = 0;
449 
450 	/* Get data */
451 	m = NGI_M(item);
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%lx\n",
502 				       (long)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 					if (sc->seqRetries > 1)
512 						cisco_notify(sc, NGM_LINK_IS_UP);
513 					sc->seqRetries = 0;
514 				}
515 				break;
516 			case CISCO_ADDR_REQ:
517 			    {
518 				struct ng_mesg *msg;
519 				int dummy_error = 0;
520 
521 				/* Ask inet peer for IP address information */
522 				if (sc->inet.hook == NULL)
523 					goto nomsg;
524 				NG_MKMESSAGE(msg, NGM_CISCO_COOKIE,
525 				    NGM_CISCO_GET_IPADDR, 0, M_NOWAIT);
526 				if (msg == NULL)
527 					goto nomsg;
528 				NG_SEND_MSG_HOOK(dummy_error,
529 				    sc->node, msg, sc->inet.hook, 0);
530 		/*
531 		 * XXX Now maybe we should set a flag telling
532 		 * our receiver to send this message when the response comes in
533 		 * instead of now when the data may be bad.
534 		 */
535 		nomsg:
536 				/* Send reply to peer device */
537 				error = cisco_send(sc, CISCO_ADDR_REPLY,
538 					    ntohl(sc->localip.s_addr),
539 					    ntohl(sc->localmask.s_addr));
540 				break;
541 			    }
542 			}
543 			goto drop;
544 		    }
545 		case ETHERTYPE_IP:
546 			pep = &sc->inet;
547 			break;
548 		case ETHERTYPE_IPV6:
549 			pep = &sc->inet6;
550 			break;
551 		case ETHERTYPE_AT:
552 			pep = &sc->atalk;
553 			break;
554 		case ETHERTYPE_IPX:
555 			pep = &sc->ipx;
556 			break;
557 		}
558 		break;
559 	}
560 
561 	/* Drop if payload is empty */
562 	if (m->m_pkthdr.len == 0) {
563 		error = EINVAL;
564 		goto drop;
565 	}
566 
567 	/* Send it on */
568 	if (pep->hook == NULL)
569 		goto drop;
570 	NG_FWD_NEW_DATA(error, item, pep->hook, m);
571 	return (error);
572 
573 drop:
574 	NG_FREE_ITEM(item);
575 	return (error);
576 }
577 
578 
579 /*
580  * Send keepalive packets, every 10 seconds.
581  */
582 static void
583 cisco_keepalive(node_p node, hook_p hook, void *arg1, int arg2)
584 {
585 	const sc_p sc = arg1;
586 
587 	cisco_send(sc, CISCO_KEEPALIVE_REQ, sc->local_seq, sc->remote_seq);
588 	if (sc->seqRetries++ > 1)
589 		cisco_notify(sc, NGM_LINK_IS_DOWN);
590 	ng_callout(&sc->handle, node, NULL, (hz * KEEPALIVE_SECS),
591 	    &cisco_keepalive, (void *)sc, 0);
592 }
593 
594 /*
595  * Send Cisco keepalive packet.
596  */
597 static int
598 cisco_send(sc_p sc, int type, long par1, long par2)
599 {
600 	struct cisco_header *h;
601 	struct cisco_packet *ch;
602 	struct mbuf *m;
603 	struct timeval time;
604 	u_long  t;
605 	int     error = 0;
606 
607 	getmicrouptime(&time);
608 
609 	MGETHDR(m, M_DONTWAIT, MT_DATA);
610 	if (!m)
611 		return (ENOBUFS);
612 
613 	t = time.tv_sec * 1000 + time.tv_usec / 1000;
614 	m->m_pkthdr.len = m->m_len = CISCO_HEADER_LEN + CISCO_PACKET_LEN;
615 	m->m_pkthdr.rcvif = 0;
616 
617 	h = mtod(m, struct cisco_header *);
618 	h->address = CISCO_MULTICAST;
619 	h->control = 0;
620 	h->protocol = htons(CISCO_KEEPALIVE);
621 
622 	ch = (struct cisco_packet *) (h + 1);
623 	ch->type = htonl(type);
624 	ch->par1 = htonl(par1);
625 	ch->par2 = htonl(par2);
626 	ch->rel = -1;
627 	ch->time0 = htons((u_short) (t >> 16));
628 	ch->time1 = htons((u_short) t);
629 
630 	NG_SEND_DATA_ONLY(error, sc->downstream.hook, m);
631 	return (error);
632 }
633 
634 /*
635  * Send linkstate to upstream node.
636  */
637 static void
638 cisco_notify(sc_p sc, uint32_t cmd)
639 {
640 	struct ng_mesg *msg;
641 	int dummy_error = 0;
642 
643 	if (sc->inet.hook == NULL) /* nothing to notify */
644 		return;
645 
646 	NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
647 	if (msg != NULL)
648 		NG_SEND_MSG_HOOK(dummy_error, sc->node, msg, sc->inet.hook, 0);
649 }
650