Deleted Added
full compact
ng_cisco.c (69922) ng_cisco.c (70159)
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 *
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 69922 2000-12-12 18:52:14Z julian $
39 * $FreeBSD: head/sys/netgraph/ng_cisco.c 70159 2000-12-18 20:03:32Z julian $
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_rmnode;
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, struct mbuf *m, meta_p meta);
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_info
132 ng_cisco_ipaddr_type_info = 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_info
136};
137
138/* Parse type for struct ng_async_stat */
139static const struct ng_parse_struct_info
140 ng_cisco_stats_type_info = 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_info,
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 = {
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_rmnode;
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, struct mbuf *m, meta_p meta);
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_info
132 ng_cisco_ipaddr_type_info = 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_info
136};
137
138/* Parse type for struct ng_async_stat */
139static const struct ng_parse_struct_info
140 ng_cisco_stats_type_info = 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_info,
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 NG_VERSION,
174 NG_ABI_VERSION,
175 NG_CISCO_NODE_TYPE,
176 NULL,
177 cisco_constructor,
178 cisco_rcvmsg,
179 cisco_rmnode,
180 cisco_newhook,
181 NULL,
182 NULL,
183 cisco_rcvdata,
184 cisco_disconnect,
185 ng_cisco_cmdlist
186};
187NETGRAPH_INIT(cisco, &typestruct);
188
189/*
190 * Node constructor
191 */
192static int
193cisco_constructor(node_p *nodep)
194{
195 sc_p sc;
196 int error = 0;
197
198 MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO);
199 if (sc == NULL)
200 return (ENOMEM);
201
202 callout_handle_init(&sc->handle);
203 if ((error = ng_make_node_common(&typestruct, nodep))) {
204 FREE(sc, M_NETGRAPH);
205 return (error);
206 }
207 (*nodep)->private = sc;
208 sc->node = *nodep;
209
210 /* Initialise the varous protocol hook holders */
211 sc->downstream.af = 0xffff;
212 sc->inet.af = AF_INET;
213 sc->inet6.af = AF_INET6;
214 sc->atalk.af = AF_APPLETALK;
215 sc->ipx.af = AF_IPX;
216 return (0);
217}
218
219/*
220 * Check new hook
221 */
222static int
223cisco_newhook(node_p node, hook_p hook, const char *name)
224{
225 const sc_p sc = node->private;
226
227 if (strcmp(name, NG_CISCO_HOOK_DOWNSTREAM) == 0) {
228 sc->downstream.hook = hook;
229 hook->private = &sc->downstream;
230
231 /* Start keepalives */
232 sc->handle = timeout(cisco_keepalive, sc, hz * KEEPALIVE_SECS);
233 } else if (strcmp(name, NG_CISCO_HOOK_INET) == 0) {
234 sc->inet.hook = hook;
235 hook->private = &sc->inet;
236 } else if (strcmp(name, NG_CISCO_HOOK_APPLETALK) == 0) {
237 sc->atalk.hook = hook;
238 hook->private = &sc->atalk;
239 } else if (strcmp(name, NG_CISCO_HOOK_IPX) == 0) {
240 sc->ipx.hook = hook;
241 hook->private = &sc->ipx;
242 } else if (strcmp(name, NG_CISCO_HOOK_DEBUG) == 0) {
243 hook->private = NULL; /* unimplemented */
244 } else
245 return (EINVAL);
246 return 0;
247}
248
249/*
250 * Receive control message.
251 */
252static int
253cisco_rcvmsg(node_p node, struct ng_mesg *msg,
254 const char *retaddr, struct ng_mesg **rptr, hook_p lasthook)
255{
256 const sc_p sc = node->private;
257 struct ng_mesg *resp = NULL;
258 int error = 0;
259
260 switch (msg->header.typecookie) {
261 case NGM_GENERIC_COOKIE:
262 switch (msg->header.cmd) {
263 case NGM_TEXT_STATUS:
264 {
265 char *arg;
266 int pos;
267
268 NG_MKRESPONSE(resp, msg, sizeof(struct ng_mesg)
269 + NG_TEXTRESPONSE, M_NOWAIT);
270 if (resp == NULL) {
271 error = ENOMEM;
272 break;
273 }
274 arg = (char *) resp->data;
275 pos = sprintf(arg,
276 "keepalive period: %d sec; ", KEEPALIVE_SECS);
277 pos += sprintf(arg + pos,
278 "unacknowledged keepalives: %ld", sc->seqRetries);
279 resp->header.arglen = pos + 1;
280 break;
281 }
282 default:
283 error = EINVAL;
284 break;
285 }
286 break;
287 case NGM_CISCO_COOKIE:
288 switch (msg->header.cmd) {
289 case NGM_CISCO_GET_IPADDR: /* could be a late reply! */
290 if ((msg->header.flags & NGF_RESP) == 0) {
291 struct in_addr *ips;
292
293 NG_MKRESPONSE(resp, msg,
294 2 * sizeof(*ips), M_NOWAIT);
295 if (!resp) {
296 error = ENOMEM;
297 break;
298 }
299 ips = (struct in_addr *) resp->data;
300 ips[0] = sc->localip;
301 ips[1] = sc->localmask;
302 break;
303 }
304 /* FALLTHROUGH */ /* ...if it's a reply */
305 case NGM_CISCO_SET_IPADDR:
306 {
307 struct in_addr *const ips = (struct in_addr *)msg->data;
308
309 if (msg->header.arglen < 2 * sizeof(*ips)) {
310 error = EINVAL;
311 break;
312 }
313 sc->localip = ips[0];
314 sc->localmask = ips[1];
315 break;
316 }
317 case NGM_CISCO_GET_STATUS:
318 {
319 struct ng_cisco_stats *stat;
320
321 NG_MKRESPONSE(resp, msg, sizeof(*stat), M_NOWAIT);
322 if (!resp) {
323 error = ENOMEM;
324 break;
325 }
326 stat = (struct ng_cisco_stats *)resp->data;
327 stat->seqRetries = sc->seqRetries;
328 stat->keepAlivePeriod = KEEPALIVE_SECS;
329 break;
330 }
331 default:
332 error = EINVAL;
333 break;
334 }
335 break;
336 default:
337 error = EINVAL;
338 break;
339 }
340 if (rptr)
341 *rptr = resp;
342 else if (resp)
343 FREE(resp, M_NETGRAPH);
344 FREE(msg, M_NETGRAPH);
345 return (error);
346}
347
348/*
349 * Receive data
350 */
351static int
352cisco_rcvdata(hook_p hook, struct mbuf *m, meta_p meta,
353 struct mbuf **ret_m, meta_p *ret_meta, struct ng_mesg **resp)
354{
355 const sc_p sc = hook->node->private;
356 struct protoent *pep;
357 struct cisco_header *h;
358 int error = 0;
359
360 if ((pep = hook->private) == NULL)
361 goto out;
362
363 /* If it came from our downlink, deal with it separately */
364 if (pep->af == 0xffff)
365 return (cisco_input(sc, m, meta));
366
367 /* OK so it came from a protocol, heading out. Prepend general data
368 packet header. For now, IP,IPX only */
369 M_PREPEND(m, CISCO_HEADER_LEN, M_DONTWAIT);
370 if (!m) {
371 error = ENOBUFS;
372 goto out;
373 }
374 h = mtod(m, struct cisco_header *);
375 h->address = CISCO_UNICAST;
376 h->control = 0;
377
378 switch (pep->af) {
379 case AF_INET: /* Internet Protocol */
380 h->protocol = htons(ETHERTYPE_IP);
381 break;
382 case AF_INET6:
383 h->protocol = htons(ETHERTYPE_IPV6);
384 break;
385 case AF_APPLETALK: /* AppleTalk Protocol */
386 h->protocol = htons(ETHERTYPE_AT);
387 break;
388 case AF_IPX: /* Novell IPX Protocol */
389 h->protocol = htons(ETHERTYPE_IPX);
390 break;
391 default:
392 error = EAFNOSUPPORT;
393 goto out;
394 }
395
396 /* Send it */
397 NG_SEND_DATA(error, sc->downstream.hook, m, meta);
398 return (error);
399
400out:
401 NG_FREE_DATA(m, meta);
402 return (error);
403}
404
405/*
406 * Shutdown node
407 */
408static int
409cisco_rmnode(node_p node)
410{
411 const sc_p sc = node->private;
412
413 node->flags |= NG_INVALID;
414 ng_cutlinks(node);
415 ng_unname(node);
416 node->private = NULL;
417 ng_unref(sc->node);
418 FREE(sc, M_NETGRAPH);
419 return (0);
420}
421
422/*
423 * Disconnection of a hook
424 *
425 * For this type, removal of the last link destroys the node
426 */
427static int
428cisco_disconnect(hook_p hook)
429{
430 const sc_p sc = hook->node->private;
431 struct protoent *pep;
432
433 /* Check it's not the debug hook */
434 if ((pep = hook->private)) {
435 pep->hook = NULL;
436 if (pep->af == 0xffff) {
437 /* If it is the downstream hook, stop the timers */
438 untimeout(cisco_keepalive, sc, sc->handle);
439 }
440 }
441
442 /* If no more hooks, remove the node */
443 if (hook->node->numhooks == 0)
444 ng_rmnode(hook->node);
445 return (0);
446}
447
448/*
449 * Receive data
450 */
451static int
452cisco_input(sc_p sc, struct mbuf *m, meta_p meta)
453{
454 struct cisco_header *h;
455 struct cisco_packet *p;
456 struct protoent *pep;
457 int error = 0;
458
459 if (m->m_pkthdr.len <= CISCO_HEADER_LEN)
460 goto drop;
461
462 /* Strip off cisco header */
463 h = mtod(m, struct cisco_header *);
464 m_adj(m, CISCO_HEADER_LEN);
465
466 switch (h->address) {
467 default: /* Invalid Cisco packet. */
468 goto drop;
469 case CISCO_UNICAST:
470 case CISCO_MULTICAST:
471 /* Don't check the control field here (RFC 1547). */
472 switch (ntohs(h->protocol)) {
473 default:
474 goto drop;
475 case CISCO_KEEPALIVE:
476 p = mtod(m, struct cisco_packet *);
477 switch (ntohl(p->type)) {
478 default:
479 log(LOG_WARNING,
480 "cisco: unknown cisco packet type: 0x%lx\n",
481 ntohl(p->type));
482 break;
483 case CISCO_ADDR_REPLY:
484 /* Reply on address request, ignore */
485 break;
486 case CISCO_KEEPALIVE_REQ:
487 sc->remote_seq = ntohl(p->par1);
488 if (sc->local_seq == ntohl(p->par2)) {
489 sc->local_seq++;
490 sc->seqRetries = 0;
491 }
492 break;
493 case CISCO_ADDR_REQ:
494 {
495 struct ng_mesg *msg, *resp;
496
497 /* Ask inet peer for IP address information */
498 if (sc->inet.hook == NULL)
499 goto nomsg;
500 NG_MKMESSAGE(msg, NGM_CISCO_COOKIE,
501 NGM_CISCO_GET_IPADDR, 0, M_NOWAIT);
502 if (msg == NULL)
503 goto nomsg;
504 ng_send_msg(sc->node, msg, NULL,
505 sc->inet.hook, NULL, &resp);
506 if (resp != NULL)
507 cisco_rcvmsg(sc->node, resp, ".",
508 NULL, NULL);
509
510 nomsg:
511 /* Send reply to peer device */
512 error = cisco_send(sc, CISCO_ADDR_REPLY,
513 ntohl(sc->localip.s_addr),
514 ntohl(sc->localmask.s_addr));
515 break;
516 }
517 }
518 goto drop;
519 case ETHERTYPE_IP:
520 pep = &sc->inet;
521 break;
522 case ETHERTYPE_IPV6:
523 pep = &sc->inet6;
524 break;
525 case ETHERTYPE_AT:
526 pep = &sc->atalk;
527 break;
528 case ETHERTYPE_IPX:
529 pep = &sc->ipx;
530 break;
531 }
532 break;
533 }
534
535 /* Send it on */
536 if (pep->hook == NULL)
537 goto drop;
538 NG_SEND_DATA(error, pep->hook, m, meta);
539 return (error);
540
541drop:
542 NG_FREE_DATA(m, meta);
543 return (error);
544}
545
546
547/*
548 * Send keepalive packets, every 10 seconds.
549 */
550static void
551cisco_keepalive(void *arg)
552{
553 const sc_p sc = arg;
554 int s = splimp();
555
556 cisco_send(sc, CISCO_KEEPALIVE_REQ, sc->local_seq, sc->remote_seq);
557 sc->seqRetries++;
558 splx(s);
559 sc->handle = timeout(cisco_keepalive, sc, hz * KEEPALIVE_SECS);
560}
561
562/*
563 * Send Cisco keepalive packet.
564 */
565static int
566cisco_send(sc_p sc, int type, long par1, long par2)
567{
568 struct cisco_header *h;
569 struct cisco_packet *ch;
570 struct mbuf *m;
571 u_long t;
572 int error = 0;
573 meta_p meta = NULL;
574 struct timeval time;
575
576 getmicrotime(&time);
577
578 MGETHDR(m, M_DONTWAIT, MT_DATA);
579 if (!m)
580 return (ENOBUFS);
581
582 t = (time.tv_sec - boottime.tv_sec) * 1000;
583 m->m_pkthdr.len = m->m_len = CISCO_HEADER_LEN + CISCO_PACKET_LEN;
584 m->m_pkthdr.rcvif = 0;
585
586 h = mtod(m, struct cisco_header *);
587 h->address = CISCO_MULTICAST;
588 h->control = 0;
589 h->protocol = htons(CISCO_KEEPALIVE);
590
591 ch = (struct cisco_packet *) (h + 1);
592 ch->type = htonl(type);
593 ch->par1 = htonl(par1);
594 ch->par2 = htonl(par2);
595 ch->rel = -1;
596 ch->time0 = htons((u_short) (t >> 16));
597 ch->time1 = htons((u_short) t);
598
599 NG_SEND_DATA(error, sc->downstream.hook, m, meta);
600 return (error);
601}
175 NG_CISCO_NODE_TYPE,
176 NULL,
177 cisco_constructor,
178 cisco_rcvmsg,
179 cisco_rmnode,
180 cisco_newhook,
181 NULL,
182 NULL,
183 cisco_rcvdata,
184 cisco_disconnect,
185 ng_cisco_cmdlist
186};
187NETGRAPH_INIT(cisco, &typestruct);
188
189/*
190 * Node constructor
191 */
192static int
193cisco_constructor(node_p *nodep)
194{
195 sc_p sc;
196 int error = 0;
197
198 MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO);
199 if (sc == NULL)
200 return (ENOMEM);
201
202 callout_handle_init(&sc->handle);
203 if ((error = ng_make_node_common(&typestruct, nodep))) {
204 FREE(sc, M_NETGRAPH);
205 return (error);
206 }
207 (*nodep)->private = sc;
208 sc->node = *nodep;
209
210 /* Initialise the varous protocol hook holders */
211 sc->downstream.af = 0xffff;
212 sc->inet.af = AF_INET;
213 sc->inet6.af = AF_INET6;
214 sc->atalk.af = AF_APPLETALK;
215 sc->ipx.af = AF_IPX;
216 return (0);
217}
218
219/*
220 * Check new hook
221 */
222static int
223cisco_newhook(node_p node, hook_p hook, const char *name)
224{
225 const sc_p sc = node->private;
226
227 if (strcmp(name, NG_CISCO_HOOK_DOWNSTREAM) == 0) {
228 sc->downstream.hook = hook;
229 hook->private = &sc->downstream;
230
231 /* Start keepalives */
232 sc->handle = timeout(cisco_keepalive, sc, hz * KEEPALIVE_SECS);
233 } else if (strcmp(name, NG_CISCO_HOOK_INET) == 0) {
234 sc->inet.hook = hook;
235 hook->private = &sc->inet;
236 } else if (strcmp(name, NG_CISCO_HOOK_APPLETALK) == 0) {
237 sc->atalk.hook = hook;
238 hook->private = &sc->atalk;
239 } else if (strcmp(name, NG_CISCO_HOOK_IPX) == 0) {
240 sc->ipx.hook = hook;
241 hook->private = &sc->ipx;
242 } else if (strcmp(name, NG_CISCO_HOOK_DEBUG) == 0) {
243 hook->private = NULL; /* unimplemented */
244 } else
245 return (EINVAL);
246 return 0;
247}
248
249/*
250 * Receive control message.
251 */
252static int
253cisco_rcvmsg(node_p node, struct ng_mesg *msg,
254 const char *retaddr, struct ng_mesg **rptr, hook_p lasthook)
255{
256 const sc_p sc = node->private;
257 struct ng_mesg *resp = NULL;
258 int error = 0;
259
260 switch (msg->header.typecookie) {
261 case NGM_GENERIC_COOKIE:
262 switch (msg->header.cmd) {
263 case NGM_TEXT_STATUS:
264 {
265 char *arg;
266 int pos;
267
268 NG_MKRESPONSE(resp, msg, sizeof(struct ng_mesg)
269 + NG_TEXTRESPONSE, M_NOWAIT);
270 if (resp == NULL) {
271 error = ENOMEM;
272 break;
273 }
274 arg = (char *) resp->data;
275 pos = sprintf(arg,
276 "keepalive period: %d sec; ", KEEPALIVE_SECS);
277 pos += sprintf(arg + pos,
278 "unacknowledged keepalives: %ld", sc->seqRetries);
279 resp->header.arglen = pos + 1;
280 break;
281 }
282 default:
283 error = EINVAL;
284 break;
285 }
286 break;
287 case NGM_CISCO_COOKIE:
288 switch (msg->header.cmd) {
289 case NGM_CISCO_GET_IPADDR: /* could be a late reply! */
290 if ((msg->header.flags & NGF_RESP) == 0) {
291 struct in_addr *ips;
292
293 NG_MKRESPONSE(resp, msg,
294 2 * sizeof(*ips), M_NOWAIT);
295 if (!resp) {
296 error = ENOMEM;
297 break;
298 }
299 ips = (struct in_addr *) resp->data;
300 ips[0] = sc->localip;
301 ips[1] = sc->localmask;
302 break;
303 }
304 /* FALLTHROUGH */ /* ...if it's a reply */
305 case NGM_CISCO_SET_IPADDR:
306 {
307 struct in_addr *const ips = (struct in_addr *)msg->data;
308
309 if (msg->header.arglen < 2 * sizeof(*ips)) {
310 error = EINVAL;
311 break;
312 }
313 sc->localip = ips[0];
314 sc->localmask = ips[1];
315 break;
316 }
317 case NGM_CISCO_GET_STATUS:
318 {
319 struct ng_cisco_stats *stat;
320
321 NG_MKRESPONSE(resp, msg, sizeof(*stat), M_NOWAIT);
322 if (!resp) {
323 error = ENOMEM;
324 break;
325 }
326 stat = (struct ng_cisco_stats *)resp->data;
327 stat->seqRetries = sc->seqRetries;
328 stat->keepAlivePeriod = KEEPALIVE_SECS;
329 break;
330 }
331 default:
332 error = EINVAL;
333 break;
334 }
335 break;
336 default:
337 error = EINVAL;
338 break;
339 }
340 if (rptr)
341 *rptr = resp;
342 else if (resp)
343 FREE(resp, M_NETGRAPH);
344 FREE(msg, M_NETGRAPH);
345 return (error);
346}
347
348/*
349 * Receive data
350 */
351static int
352cisco_rcvdata(hook_p hook, struct mbuf *m, meta_p meta,
353 struct mbuf **ret_m, meta_p *ret_meta, struct ng_mesg **resp)
354{
355 const sc_p sc = hook->node->private;
356 struct protoent *pep;
357 struct cisco_header *h;
358 int error = 0;
359
360 if ((pep = hook->private) == NULL)
361 goto out;
362
363 /* If it came from our downlink, deal with it separately */
364 if (pep->af == 0xffff)
365 return (cisco_input(sc, m, meta));
366
367 /* OK so it came from a protocol, heading out. Prepend general data
368 packet header. For now, IP,IPX only */
369 M_PREPEND(m, CISCO_HEADER_LEN, M_DONTWAIT);
370 if (!m) {
371 error = ENOBUFS;
372 goto out;
373 }
374 h = mtod(m, struct cisco_header *);
375 h->address = CISCO_UNICAST;
376 h->control = 0;
377
378 switch (pep->af) {
379 case AF_INET: /* Internet Protocol */
380 h->protocol = htons(ETHERTYPE_IP);
381 break;
382 case AF_INET6:
383 h->protocol = htons(ETHERTYPE_IPV6);
384 break;
385 case AF_APPLETALK: /* AppleTalk Protocol */
386 h->protocol = htons(ETHERTYPE_AT);
387 break;
388 case AF_IPX: /* Novell IPX Protocol */
389 h->protocol = htons(ETHERTYPE_IPX);
390 break;
391 default:
392 error = EAFNOSUPPORT;
393 goto out;
394 }
395
396 /* Send it */
397 NG_SEND_DATA(error, sc->downstream.hook, m, meta);
398 return (error);
399
400out:
401 NG_FREE_DATA(m, meta);
402 return (error);
403}
404
405/*
406 * Shutdown node
407 */
408static int
409cisco_rmnode(node_p node)
410{
411 const sc_p sc = node->private;
412
413 node->flags |= NG_INVALID;
414 ng_cutlinks(node);
415 ng_unname(node);
416 node->private = NULL;
417 ng_unref(sc->node);
418 FREE(sc, M_NETGRAPH);
419 return (0);
420}
421
422/*
423 * Disconnection of a hook
424 *
425 * For this type, removal of the last link destroys the node
426 */
427static int
428cisco_disconnect(hook_p hook)
429{
430 const sc_p sc = hook->node->private;
431 struct protoent *pep;
432
433 /* Check it's not the debug hook */
434 if ((pep = hook->private)) {
435 pep->hook = NULL;
436 if (pep->af == 0xffff) {
437 /* If it is the downstream hook, stop the timers */
438 untimeout(cisco_keepalive, sc, sc->handle);
439 }
440 }
441
442 /* If no more hooks, remove the node */
443 if (hook->node->numhooks == 0)
444 ng_rmnode(hook->node);
445 return (0);
446}
447
448/*
449 * Receive data
450 */
451static int
452cisco_input(sc_p sc, struct mbuf *m, meta_p meta)
453{
454 struct cisco_header *h;
455 struct cisco_packet *p;
456 struct protoent *pep;
457 int error = 0;
458
459 if (m->m_pkthdr.len <= CISCO_HEADER_LEN)
460 goto drop;
461
462 /* Strip off cisco header */
463 h = mtod(m, struct cisco_header *);
464 m_adj(m, CISCO_HEADER_LEN);
465
466 switch (h->address) {
467 default: /* Invalid Cisco packet. */
468 goto drop;
469 case CISCO_UNICAST:
470 case CISCO_MULTICAST:
471 /* Don't check the control field here (RFC 1547). */
472 switch (ntohs(h->protocol)) {
473 default:
474 goto drop;
475 case CISCO_KEEPALIVE:
476 p = mtod(m, struct cisco_packet *);
477 switch (ntohl(p->type)) {
478 default:
479 log(LOG_WARNING,
480 "cisco: unknown cisco packet type: 0x%lx\n",
481 ntohl(p->type));
482 break;
483 case CISCO_ADDR_REPLY:
484 /* Reply on address request, ignore */
485 break;
486 case CISCO_KEEPALIVE_REQ:
487 sc->remote_seq = ntohl(p->par1);
488 if (sc->local_seq == ntohl(p->par2)) {
489 sc->local_seq++;
490 sc->seqRetries = 0;
491 }
492 break;
493 case CISCO_ADDR_REQ:
494 {
495 struct ng_mesg *msg, *resp;
496
497 /* Ask inet peer for IP address information */
498 if (sc->inet.hook == NULL)
499 goto nomsg;
500 NG_MKMESSAGE(msg, NGM_CISCO_COOKIE,
501 NGM_CISCO_GET_IPADDR, 0, M_NOWAIT);
502 if (msg == NULL)
503 goto nomsg;
504 ng_send_msg(sc->node, msg, NULL,
505 sc->inet.hook, NULL, &resp);
506 if (resp != NULL)
507 cisco_rcvmsg(sc->node, resp, ".",
508 NULL, NULL);
509
510 nomsg:
511 /* Send reply to peer device */
512 error = cisco_send(sc, CISCO_ADDR_REPLY,
513 ntohl(sc->localip.s_addr),
514 ntohl(sc->localmask.s_addr));
515 break;
516 }
517 }
518 goto drop;
519 case ETHERTYPE_IP:
520 pep = &sc->inet;
521 break;
522 case ETHERTYPE_IPV6:
523 pep = &sc->inet6;
524 break;
525 case ETHERTYPE_AT:
526 pep = &sc->atalk;
527 break;
528 case ETHERTYPE_IPX:
529 pep = &sc->ipx;
530 break;
531 }
532 break;
533 }
534
535 /* Send it on */
536 if (pep->hook == NULL)
537 goto drop;
538 NG_SEND_DATA(error, pep->hook, m, meta);
539 return (error);
540
541drop:
542 NG_FREE_DATA(m, meta);
543 return (error);
544}
545
546
547/*
548 * Send keepalive packets, every 10 seconds.
549 */
550static void
551cisco_keepalive(void *arg)
552{
553 const sc_p sc = arg;
554 int s = splimp();
555
556 cisco_send(sc, CISCO_KEEPALIVE_REQ, sc->local_seq, sc->remote_seq);
557 sc->seqRetries++;
558 splx(s);
559 sc->handle = timeout(cisco_keepalive, sc, hz * KEEPALIVE_SECS);
560}
561
562/*
563 * Send Cisco keepalive packet.
564 */
565static int
566cisco_send(sc_p sc, int type, long par1, long par2)
567{
568 struct cisco_header *h;
569 struct cisco_packet *ch;
570 struct mbuf *m;
571 u_long t;
572 int error = 0;
573 meta_p meta = NULL;
574 struct timeval time;
575
576 getmicrotime(&time);
577
578 MGETHDR(m, M_DONTWAIT, MT_DATA);
579 if (!m)
580 return (ENOBUFS);
581
582 t = (time.tv_sec - boottime.tv_sec) * 1000;
583 m->m_pkthdr.len = m->m_len = CISCO_HEADER_LEN + CISCO_PACKET_LEN;
584 m->m_pkthdr.rcvif = 0;
585
586 h = mtod(m, struct cisco_header *);
587 h->address = CISCO_MULTICAST;
588 h->control = 0;
589 h->protocol = htons(CISCO_KEEPALIVE);
590
591 ch = (struct cisco_packet *) (h + 1);
592 ch->type = htonl(type);
593 ch->par1 = htonl(par1);
594 ch->par2 = htonl(par2);
595 ch->rel = -1;
596 ch->time0 = htons((u_short) (t >> 16));
597 ch->time1 = htons((u_short) t);
598
599 NG_SEND_DATA(error, sc->downstream.hook, m, meta);
600 return (error);
601}