xref: /freebsd/sys/netgraph/ng_cisco.c (revision 41466b50c1d5bfd1cf6adaae547a579a75d7c04e)
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_info
132 	ng_cisco_ipaddr_type_info = 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_info
136 };
137 
138 /* Parse type for struct ng_async_stat */
139 static const struct ng_parse_struct_info
140 	ng_cisco_stats_type_info = 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_info,
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 	struct cisco_header *h;
447 	struct cisco_packet *p;
448 	struct protoent *pep;
449 	int error = 0;
450 	struct mbuf *m;
451 
452 	m = NGI_M(item);
453 	if (m->m_pkthdr.len <= CISCO_HEADER_LEN)
454 		goto drop;
455 
456 	/* Strip off cisco header */
457 	h = mtod(m, struct cisco_header *);
458 	m_adj(m, CISCO_HEADER_LEN);
459 
460 	switch (h->address) {
461 	default:		/* Invalid Cisco packet. */
462 		goto drop;
463 	case CISCO_UNICAST:
464 	case CISCO_MULTICAST:
465 		/* Don't check the control field here (RFC 1547). */
466 		switch (ntohs(h->protocol)) {
467 		default:
468 			goto drop;
469 		case CISCO_KEEPALIVE:
470 			p = mtod(m, struct cisco_packet *);
471 			switch (ntohl(p->type)) {
472 			default:
473 				log(LOG_WARNING,
474 				    "cisco: unknown cisco packet type: 0x%lx\n",
475 				       (long)ntohl(p->type));
476 				break;
477 			case CISCO_ADDR_REPLY:
478 				/* Reply on address request, ignore */
479 				break;
480 			case CISCO_KEEPALIVE_REQ:
481 				sc->remote_seq = ntohl(p->par1);
482 				if (sc->local_seq == ntohl(p->par2)) {
483 					sc->local_seq++;
484 					sc->seqRetries = 0;
485 				}
486 				break;
487 			case CISCO_ADDR_REQ:
488 			    {
489 				struct ng_mesg *msg;
490 				int dummy_error = 0;
491 
492 				/* Ask inet peer for IP address information */
493 				if (sc->inet.hook == NULL)
494 					goto nomsg;
495 				NG_MKMESSAGE(msg, NGM_CISCO_COOKIE,
496 				    NGM_CISCO_GET_IPADDR, 0, M_NOWAIT);
497 				if (msg == NULL)
498 					goto nomsg;
499 				NG_SEND_MSG_HOOK(dummy_error, sc->node, msg,
500 					sc->inet.hook, NULL);
501 		/*
502 		 * XXX Now maybe we should set a flag telling
503 		 * our receiver to send this message when the response comes in
504 		 * instead of now when the data may be bad.
505 		 */
506 		nomsg:
507 				/* Send reply to peer device */
508 				error = cisco_send(sc, CISCO_ADDR_REPLY,
509 					    ntohl(sc->localip.s_addr),
510 					    ntohl(sc->localmask.s_addr));
511 				break;
512 			    }
513 			}
514 			goto drop;
515 		case ETHERTYPE_IP:
516 			pep = &sc->inet;
517 			break;
518 		case ETHERTYPE_IPV6:
519 			pep = &sc->inet6;
520 			break;
521 		case ETHERTYPE_AT:
522 			pep = &sc->atalk;
523 			break;
524 		case ETHERTYPE_IPX:
525 			pep = &sc->ipx;
526 			break;
527 		}
528 		break;
529 	}
530 
531 	/* Send it on */
532 	if (pep->hook == NULL)
533 		goto drop;
534 	NG_FWD_NEW_DATA(error, item, pep->hook, m);
535 	return (error);
536 
537 drop:
538 	NG_FREE_ITEM(item);
539 	return (error);
540 }
541 
542 
543 /*
544  * Send keepalive packets, every 10 seconds.
545  */
546 static void
547 cisco_keepalive(void *arg)
548 {
549 	const sc_p sc = arg;
550 	int s = splimp();
551 
552 	cisco_send(sc, CISCO_KEEPALIVE_REQ, sc->local_seq, sc->remote_seq);
553 	sc->seqRetries++;
554 	splx(s);
555 	sc->handle = timeout(cisco_keepalive, sc, hz * KEEPALIVE_SECS);
556 }
557 
558 /*
559  * Send Cisco keepalive packet.
560  */
561 static int
562 cisco_send(sc_p sc, int type, long par1, long par2)
563 {
564 	struct cisco_header *h;
565 	struct cisco_packet *ch;
566 	struct mbuf *m;
567 	u_long  t;
568 	int     error = 0;
569 	struct timeval time;
570 
571 	getmicrotime(&time);
572 
573 	MGETHDR(m, M_DONTWAIT, MT_DATA);
574 	if (!m)
575 		return (ENOBUFS);
576 
577 	t = (time.tv_sec - boottime.tv_sec) * 1000;
578 	m->m_pkthdr.len = m->m_len = CISCO_HEADER_LEN + CISCO_PACKET_LEN;
579 	m->m_pkthdr.rcvif = 0;
580 
581 	h = mtod(m, struct cisco_header *);
582 	h->address = CISCO_MULTICAST;
583 	h->control = 0;
584 	h->protocol = htons(CISCO_KEEPALIVE);
585 
586 	ch = (struct cisco_packet *) (h + 1);
587 	ch->type = htonl(type);
588 	ch->par1 = htonl(par1);
589 	ch->par2 = htonl(par2);
590 	ch->rel = -1;
591 	ch->time0 = htons((u_short) (t >> 16));
592 	ch->time1 = htons((u_short) t);
593 
594 	NG_SEND_DATA_ONLY(error, sc->downstream.hook, m);
595 	return (error);
596 }
597