xref: /freebsd/sys/netgraph/ng_cisco.c (revision a3e8fd0b7f663db7eafff527d5c3ca3bcfa8a537)
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$
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 
76 struct 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 
84 struct 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 
95 struct protoent {
96 	hook_p  hook;		/* the hook for this proto */
97 	u_short af;		/* address family, -1 = downstream */
98 };
99 
100 struct 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 };
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_shutdown;
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, item_p item);
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_ABI_VERSION,
175 	NG_CISCO_NODE_TYPE,
176 	NULL,
177 	cisco_constructor,
178 	cisco_rcvmsg,
179 	cisco_shutdown,
180 	cisco_newhook,
181 	NULL,
182 	NULL,
183 	cisco_rcvdata,
184 	cisco_disconnect,
185 	ng_cisco_cmdlist
186 };
187 NETGRAPH_INIT(cisco, &typestruct);
188 
189 /*
190  * Node constructor
191  */
192 static int
193 cisco_constructor(node_p node)
194 {
195 	sc_p sc;
196 
197 	MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO);
198 	if (sc == NULL)
199 		return (ENOMEM);
200 
201 	callout_handle_init(&sc->handle);
202 	NG_NODE_SET_PRIVATE(node, sc);
203 	sc->node = node;
204 
205 	/* Initialise the varous protocol hook holders */
206 	sc->downstream.af = 0xffff;
207 	sc->inet.af = AF_INET;
208 	sc->inet6.af = AF_INET6;
209 	sc->atalk.af = AF_APPLETALK;
210 	sc->ipx.af = AF_IPX;
211 	return (0);
212 }
213 
214 /*
215  * Check new hook
216  */
217 static int
218 cisco_newhook(node_p node, hook_p hook, const char *name)
219 {
220 	const sc_p sc = NG_NODE_PRIVATE(node);
221 
222 	if (strcmp(name, NG_CISCO_HOOK_DOWNSTREAM) == 0) {
223 		sc->downstream.hook = hook;
224 		NG_HOOK_SET_PRIVATE(hook, &sc->downstream);
225 
226 		/* Start keepalives */
227 		sc->handle = timeout(cisco_keepalive, sc, hz * KEEPALIVE_SECS);
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, sizeof(struct ng_mesg)
265 			    + 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: %ld", 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  */
344 static int
345 cisco_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 	int error = 0;
351 	struct mbuf *m;
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 
394 out:
395 	NG_FREE_ITEM(item);
396 	return (error);
397 }
398 
399 /*
400  * Shutdown node
401  */
402 static int
403 cisco_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  */
418 static int
419 cisco_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 			untimeout(cisco_keepalive, sc, sc->handle);
430 		}
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  */
443 static int
444 cisco_input(sc_p sc, item_p item)
445 {
446 	const struct cisco_header *h;
447 	struct cisco_header hdrbuf;
448 	struct protoent *pep;
449 	int error = 0;
450 	struct mbuf *m;
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 					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(void *arg)
584 {
585 	const sc_p sc = arg;
586 	int s = splimp();
587 
588 	cisco_send(sc, CISCO_KEEPALIVE_REQ, sc->local_seq, sc->remote_seq);
589 	sc->seqRetries++;
590 	splx(s);
591 	sc->handle = timeout(cisco_keepalive, sc, hz * KEEPALIVE_SECS);
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 	u_long  t;
604 	int     error = 0;
605 	struct timeval time;
606 
607 	getmicrotime(&time);
608 
609 	MGETHDR(m, M_DONTWAIT, MT_DATA);
610 	if (!m)
611 		return (ENOBUFS);
612 
613 	t = (time.tv_sec - boottime.tv_sec) * 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