xref: /freebsd/sys/netgraph/ng_cisco.c (revision 4cf49a43559ed9fdad601bdcccd2c55963008675)
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@whistle.com>
38  *
39  * $FreeBSD$
40  * $Whistle: ng_cisco.c,v 1.23 1999/01/28 23:54:53 julian Exp $
41  */
42 
43 #include "opt_inet.h"
44 #include "opt_atalk.h"
45 #include "opt_ipx.h"
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/errno.h>
50 #include <sys/kernel.h>
51 #include <sys/socket.h>
52 #include <sys/malloc.h>
53 #include <sys/mbuf.h>
54 #include <sys/syslog.h>
55 
56 #include <net/if.h>
57 
58 #include <netinet/in.h>
59 #include <netinet/if_ether.h>
60 
61 #include <netatalk/at.h>
62 #include <netatalk/at_var.h>
63 #include <netatalk/at_extern.h>
64 
65 #include <netipx/ipx.h>
66 #include <netipx/ipx_if.h>
67 
68 #include <netgraph/ng_message.h>
69 #include <netgraph/netgraph.h>
70 #include <netgraph/ng_cisco.h>
71 
72 #define CISCO_MULTICAST         0x8f	/* Cisco multicast address */
73 #define CISCO_UNICAST           0x0f	/* Cisco unicast address */
74 #define CISCO_KEEPALIVE         0x8035	/* Cisco keepalive protocol */
75 #define CISCO_ADDR_REQ          0	/* Cisco address request */
76 #define CISCO_ADDR_REPLY        1	/* Cisco address reply */
77 #define CISCO_KEEPALIVE_REQ     2	/* Cisco keepalive request */
78 
79 #define KEEPALIVE_SECS		10
80 
81 struct cisco_header {
82 	u_char  address;
83 	u_char  control;
84 	u_short protocol;
85 };
86 
87 #define CISCO_HEADER_LEN          sizeof (struct cisco_header)
88 
89 struct cisco_packet {
90 	u_long  type;
91 	u_long  par1;
92 	u_long  par2;
93 	u_short rel;
94 	u_short time0;
95 	u_short time1;
96 };
97 
98 #define CISCO_PACKET_LEN (sizeof(struct cisco_packet))
99 
100 struct protoent {
101 	hook_p  hook;		/* the hook for this proto */
102 	u_short af;		/* address family, -1 = downstream */
103 };
104 
105 struct cisco_priv {
106 	u_long  local_seq;
107 	u_long  remote_seq;
108 	u_long  seq_retries;	/* how many times we've been here throwing out
109 				 * the same sequence number without ack */
110 	node_p  node;
111 	struct callout_handle handle;
112 	struct protoent downstream;
113 	struct protoent inet;		/* IP information */
114 	struct in_addr localip;
115 	struct in_addr localmask;
116 	struct protoent atalk;		/* AppleTalk information */
117 	struct protoent ipx;		/* IPX information */
118 };
119 typedef struct cisco_priv *sc_p;
120 
121 /* Netgraph methods */
122 static int	cisco_constructor(node_p *node);
123 static int	cisco_rcvmsg(node_p node, struct ng_mesg *msg,
124 		    const char *retaddr, struct ng_mesg **resp);
125 static int	cisco_rmnode(node_p node);
126 static int	cisco_newhook(node_p node, hook_p hook, const char *name);
127 
128 static int	cisco_rcvdata(hook_p hook, struct mbuf *m, meta_p meta);
129 static int	cisco_disconnect(hook_p hook);
130 
131 /* Other functions */
132 static int	cisco_input(sc_p sc, struct mbuf *m, meta_p meta);
133 static void	cisco_keepalive(void *arg);
134 static int	cisco_send(sc_p sc, int type, long par1, long par2);
135 
136 /* Node type */
137 static struct ng_type typestruct = {
138 	NG_VERSION,
139 	NG_CISCO_NODE_TYPE,
140 	NULL,
141 	cisco_constructor,
142 	cisco_rcvmsg,
143 	cisco_rmnode,
144 	cisco_newhook,
145 	NULL,
146 	NULL,
147 	cisco_rcvdata,
148 	cisco_rcvdata,
149 	cisco_disconnect
150 };
151 NETGRAPH_INIT(cisco, &typestruct);
152 
153 /*
154  * Node constructor
155  */
156 static int
157 cisco_constructor(node_p *nodep)
158 {
159 	sc_p sc;
160 	int error = 0;
161 
162 	MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_WAITOK);
163 	if (sc == NULL)
164 		return (ENOMEM);
165 	bzero(sc, sizeof(struct cisco_priv));
166 
167 	callout_handle_init(&sc->handle);
168 	if ((error = ng_make_node_common(&typestruct, nodep))) {
169 		FREE(sc, M_NETGRAPH);
170 		return (error);
171 	}
172 	(*nodep)->private = sc;
173 	sc->node = *nodep;
174 
175 	/* Initialise the varous protocol hook holders */
176 	sc->downstream.af = 0xffff;
177 	sc->inet.af = AF_INET;
178 	sc->atalk.af = AF_APPLETALK;
179 	sc->ipx.af = AF_IPX;
180 	return (0);
181 }
182 
183 /*
184  * Check new hook
185  */
186 static int
187 cisco_newhook(node_p node, hook_p hook, const char *name)
188 {
189 	const sc_p sc = node->private;
190 
191 	if (strcmp(name, NG_CISCO_HOOK_DOWNSTREAM) == 0) {
192 		sc->downstream.hook = hook;
193 		hook->private = &sc->downstream;
194 
195 		/* Start keepalives */
196 		sc->handle = timeout(cisco_keepalive, sc, hz * KEEPALIVE_SECS);
197 	} else if (strcmp(name, NG_CISCO_HOOK_INET) == 0) {
198 		sc->inet.hook = hook;
199 		hook->private = &sc->inet;
200 	} else if (strcmp(name, NG_CISCO_HOOK_APPLETALK) == 0) {
201 		sc->atalk.hook = hook;
202 		hook->private = &sc->atalk;
203 	} else if (strcmp(name, NG_CISCO_HOOK_IPX) == 0) {
204 		sc->ipx.hook = hook;
205 		hook->private = &sc->ipx;
206 	} else if (strcmp(name, NG_CISCO_HOOK_DEBUG) == 0) {
207 		hook->private = NULL;	/* unimplemented */
208 	} else
209 		return (EINVAL);
210 	return 0;
211 }
212 
213 /*
214  * Receive control message.
215  */
216 static int
217 cisco_rcvmsg(node_p node, struct ng_mesg *msg,
218 	const char *retaddr, struct ng_mesg **rptr)
219 {
220 	const sc_p sc = node->private;
221 	struct ng_mesg *resp = NULL;
222 	int error = 0;
223 
224 	switch (msg->header.typecookie) {
225 	case NGM_GENERIC_COOKIE:
226 		switch (msg->header.cmd) {
227 		case NGM_TEXT_STATUS:
228 		    {
229 			char *arg;
230 			int pos;
231 
232 			NG_MKRESPONSE(resp, msg, sizeof(struct ng_mesg)
233 			    + NG_TEXTRESPONSE, M_NOWAIT);
234 			if (resp == NULL) {
235 				error = ENOMEM;
236 				break;
237 			}
238 			arg = (char *) resp->data;
239 			pos = sprintf(arg,
240 			  "keepalive period: %d sec; ", KEEPALIVE_SECS);
241 			pos += sprintf(arg + pos,
242 			  "unacknowledged keepalives: %ld", sc->seq_retries);
243 			resp->header.arglen = pos + 1;
244 			break;
245 		    }
246 		default:
247 			error = EINVAL;
248 			break;
249 		}
250 		break;
251 	case NGM_CISCO_COOKIE:
252 		switch (msg->header.cmd) {
253 		case NGM_CISCO_GET_IPADDR:	/* could be a late reply! */
254 			if ((msg->header.flags & NGF_RESP) == 0) {
255 				struct in_addr *ips;
256 
257 				NG_MKRESPONSE(resp, msg,
258 				    2 * sizeof(*ips), M_NOWAIT);
259 				if (!resp) {
260 					error = ENOMEM;
261 					break;
262 				}
263 				ips = (struct in_addr *) resp->data;
264 				ips[0] = sc->localip;
265 				ips[1] = sc->localmask;
266 				break;
267 			}
268 			/* FALLTHROUGH */	/* ...if it's a reply */
269 		case NGM_CISCO_SET_IPADDR:
270 		    {
271 			struct in_addr *const ips = (struct in_addr *)msg->data;
272 
273 			if (msg->header.arglen < 2 * sizeof(*ips)) {
274 				error = EINVAL;
275 				break;
276 			}
277 			sc->localip = ips[0];
278 			sc->localmask = ips[1];
279 			break;
280 		    }
281 		case NGM_CISCO_GET_STATUS:
282 		    {
283 			struct ngciscostat *stat;
284 
285 			NG_MKRESPONSE(resp, msg, sizeof(*stat), M_NOWAIT);
286 			if (!resp) {
287 				error = ENOMEM;
288 				break;
289 			}
290 			stat = (struct ngciscostat *) resp->data;
291 			stat->seq_retries = sc->seq_retries;
292 			stat->keepalive_period = KEEPALIVE_SECS;
293 			break;
294 		    }
295 		default:
296 			error = EINVAL;
297 			break;
298 		}
299 		break;
300 	default:
301 		error = EINVAL;
302 		break;
303 	}
304 	if (rptr)
305 		*rptr = resp;
306 	else if (resp)
307 		FREE(resp, M_NETGRAPH);
308 	FREE(msg, M_NETGRAPH);
309 	return (error);
310 }
311 
312 /*
313  * Receive data
314  */
315 static int
316 cisco_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
317 {
318 	const sc_p sc = hook->node->private;
319 	struct protoent *pep;
320 	struct cisco_header *h;
321 	int error = 0;
322 
323 	if ((pep = hook->private) == NULL)
324 		goto out;
325 
326 	/* If it came from our downlink, deal with it separately */
327 	if (pep->af == 0xffff)
328 		return (cisco_input(sc, m, meta));
329 
330 	/* OK so it came from a protocol, heading out. Prepend general data
331 	   packet header. For now, IP,IPX only  */
332 	M_PREPEND(m, CISCO_HEADER_LEN, M_DONTWAIT);
333 	if (!m) {
334 		error = ENOBUFS;
335 		goto out;
336 	}
337 	h = mtod(m, struct cisco_header *);
338 	h->address = CISCO_MULTICAST;		/* broadcast address */
339 	h->control = 0;
340 
341 	switch (pep->af) {
342 	case AF_INET:		/* Internet Protocol */
343 		h->protocol = htons(ETHERTYPE_IP);
344 		break;
345 	case AF_APPLETALK:	/* AppleTalk Protocol */
346 		h->protocol = htons(ETHERTYPE_AT);
347 		break;
348 	case AF_IPX:		/* Novell IPX Protocol */
349 		h->protocol = htons(ETHERTYPE_IPX);
350 		break;
351 	default:
352 		error = EAFNOSUPPORT;
353 		goto out;
354 	}
355 
356 	/* Send it */
357 	NG_SEND_DATA(error, sc->downstream.hook, m, meta);
358 	return (error);
359 
360 out:
361 	NG_FREE_DATA(m, meta);
362 	return (error);
363 }
364 
365 /*
366  * Shutdown node
367  */
368 static int
369 cisco_rmnode(node_p node)
370 {
371 	const sc_p sc = node->private;
372 
373 	node->flags |= NG_INVALID;
374 	ng_cutlinks(node);
375 	ng_unname(node);
376 	node->private = NULL;
377 	ng_unref(sc->node);
378 	FREE(sc, M_NETGRAPH);
379 	return (0);
380 }
381 
382 /*
383  * Disconnection of a hook
384  *
385  * For this type, removal of the last link destroys the node
386  */
387 static int
388 cisco_disconnect(hook_p hook)
389 {
390 	const sc_p sc = hook->node->private;
391 	struct protoent *pep;
392 
393 	/* Check it's not the debug hook */
394 	if ((pep = hook->private)) {
395 		pep->hook = NULL;
396 		if (pep->af == 0xffff) {
397 			/* If it is the downstream hook, stop the timers */
398 			untimeout(cisco_keepalive, sc, sc->handle);
399 		}
400 	}
401 
402 	/* If no more hooks, remove the node */
403 	if (hook->node->numhooks == 0)
404 		ng_rmnode(hook->node);
405 	return (0);
406 }
407 
408 /*
409  * Receive data
410  */
411 static int
412 cisco_input(sc_p sc, struct mbuf *m, meta_p meta)
413 {
414 	struct cisco_header *h;
415 	struct cisco_packet *p;
416 	struct protoent *pep;
417 	int error = 0;
418 
419 	if (m->m_pkthdr.len <= CISCO_HEADER_LEN)
420 		goto drop;
421 
422 	/* Strip off cisco header */
423 	h = mtod(m, struct cisco_header *);
424 	m_adj(m, CISCO_HEADER_LEN);
425 
426 	switch (h->address) {
427 	default:		/* Invalid Cisco packet. */
428 		goto drop;
429 	case CISCO_UNICAST:
430 	case CISCO_MULTICAST:
431 		/* Don't check the control field here (RFC 1547). */
432 		switch (ntohs(h->protocol)) {
433 		default:
434 			goto drop;
435 		case CISCO_KEEPALIVE:
436 			p = mtod(m, struct cisco_packet *);
437 			switch (ntohl(p->type)) {
438 			default:
439 				log(LOG_WARNING,
440 				    "cisco: unknown cisco packet type: 0x%lx\n",
441 				       ntohl(p->type));
442 				break;
443 			case CISCO_ADDR_REPLY:
444 				/* Reply on address request, ignore */
445 				break;
446 			case CISCO_KEEPALIVE_REQ:
447 				sc->remote_seq = ntohl(p->par1);
448 				if (sc->local_seq == ntohl(p->par2)) {
449 					sc->local_seq++;
450 					sc->seq_retries = 0;
451 				}
452 				break;
453 			case CISCO_ADDR_REQ:
454 			    {
455 				struct ng_mesg *msg, *resp;
456 
457 				/* Ask inet peer for IP address information */
458 				if (sc->inet.hook == NULL)
459 					goto nomsg;
460 				NG_MKMESSAGE(msg, NGM_CISCO_COOKIE,
461 				    NGM_CISCO_GET_IPADDR, 0, M_NOWAIT);
462 				if (msg == NULL)
463 					goto nomsg;
464 				ng_send_msg(sc->node, msg,
465 				    NG_CISCO_HOOK_INET, &resp);
466 				if (resp != NULL)
467 					cisco_rcvmsg(sc->node, resp, ".", NULL);
468 
469 		nomsg:
470 				/* Send reply to peer device */
471 				error = cisco_send(sc, CISCO_ADDR_REPLY,
472 					    ntohl(sc->localip.s_addr),
473 					    ntohl(sc->localmask.s_addr));
474 				break;
475 			    }
476 			}
477 			goto drop;
478 		case ETHERTYPE_IP:
479 			pep = &sc->inet;
480 			break;
481 		case ETHERTYPE_AT:
482 			pep = &sc->atalk;
483 			break;
484 		case ETHERTYPE_IPX:
485 			pep = &sc->ipx;
486 			break;
487 		}
488 		break;
489 	}
490 
491 	/* Send it on */
492 	if (pep->hook == NULL)
493 		goto drop;
494 	NG_SEND_DATA(error, pep->hook, m, meta);
495 	return (error);
496 
497 drop:
498 	NG_FREE_DATA(m, meta);
499 	return (error);
500 }
501 
502 
503 /*
504  * Send keepalive packets, every 10 seconds.
505  */
506 static void
507 cisco_keepalive(void *arg)
508 {
509 	const sc_p sc = arg;
510 	int s = splimp();
511 
512 	cisco_send(sc, CISCO_KEEPALIVE_REQ, sc->local_seq, sc->remote_seq);
513 	sc->seq_retries++;
514 	splx(s);
515 	sc->handle = timeout(cisco_keepalive, sc, hz * KEEPALIVE_SECS);
516 }
517 
518 /*
519  * Send Cisco keepalive packet.
520  */
521 static int
522 cisco_send(sc_p sc, int type, long par1, long par2)
523 {
524 	struct cisco_header *h;
525 	struct cisco_packet *ch;
526 	struct mbuf *m;
527 	u_long  t;
528 	int     error = 0;
529 	meta_p  meta = NULL;
530 	struct timeval time;
531 
532 	getmicrotime(&time);
533 
534 	MGETHDR(m, M_DONTWAIT, MT_DATA);
535 	if (!m)
536 		return (ENOBUFS);
537 
538 	t = (time.tv_sec - boottime.tv_sec) * 1000;
539 	m->m_pkthdr.len = m->m_len = CISCO_HEADER_LEN + CISCO_PACKET_LEN;
540 	m->m_pkthdr.rcvif = 0;
541 
542 	h = mtod(m, struct cisco_header *);
543 	h->address = CISCO_MULTICAST;
544 	h->control = 0;
545 	h->protocol = htons(CISCO_KEEPALIVE);
546 
547 	ch = (struct cisco_packet *) (h + 1);
548 	ch->type = htonl(type);
549 	ch->par1 = htonl(par1);
550 	ch->par2 = htonl(par2);
551 	ch->rel = -1;
552 	ch->time0 = htons((u_short) (t >> 16));
553 	ch->time1 = htons((u_short) t);
554 
555 	NG_SEND_DATA(error, sc->downstream.hook, m, meta);
556 	return (error);
557 }
558