5 * Copyright (c) 1996-1999 Whistle Communications, Inc.
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.
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
37 * Author: Julian Elischer <julian@freebsd.org>
39 * $FreeBSD: src/sys/netgraph/ng_cisco.c,v 1.4.2.6 2002/07/02 23:44:02 archie Exp $
40 * $DragonFly: src/sys/netgraph/cisco/ng_cisco.c,v 1.8 2005/06/02 22:11:45 swildner Exp $
41 * $Whistle: ng_cisco.c,v 1.25 1999/11/01 09:24:51 julian Exp $
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>
51 #include <sys/syslog.h>
52 #include <sys/thread2.h>
54 #include <machine/inttypes.h>
58 #include <netinet/in.h>
59 #include <netinet/if_ether.h>
61 #include <netproto/atalk/at.h>
63 #include <netproto/ipx/ipx.h>
64 #include <netproto/ipx/ipx_if.h>
66 #include <netgraph/ng_message.h>
67 #include <netgraph/netgraph.h>
68 #include <netgraph/ng_parse.h>
71 #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
72 #define CISCO_UNICAST 0x0f /* Cisco unicast address */
73 #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
74 #define CISCO_ADDR_REQ 0 /* Cisco address request */
75 #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
76 #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
78 #define KEEPALIVE_SECS 10
86 #define CISCO_HEADER_LEN sizeof (struct cisco_header)
97 #define CISCO_PACKET_LEN (sizeof(struct cisco_packet))
100 hook_p hook; /* the hook for this proto */
101 u_short af; /* address family, -1 = downstream */
107 u_long seqRetries; /* how many times we've been here throwing out
108 * the same sequence number without ack */
110 struct callout timeout;
111 struct protoent downstream;
112 struct protoent inet; /* IP information */
113 struct in_addr localip;
114 struct in_addr localmask;
115 struct protoent inet6; /* IPv6 information */
116 struct protoent atalk; /* AppleTalk information */
117 struct protoent ipx; /* IPX information */
119 typedef struct cisco_priv *sc_p;
121 /* Netgraph methods */
122 static ng_constructor_t cisco_constructor;
123 static ng_rcvmsg_t cisco_rcvmsg;
124 static ng_shutdown_t cisco_rmnode;
125 static ng_newhook_t cisco_newhook;
126 static ng_rcvdata_t cisco_rcvdata;
127 static ng_disconnect_t cisco_disconnect;
129 /* Other functions */
130 static int cisco_input(sc_p sc, struct mbuf *m, meta_p meta);
131 static void cisco_keepalive(void *arg);
132 static int cisco_send(sc_p sc, int type, long par1, long par2);
134 /* Parse type for struct ng_cisco_ipaddr */
135 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
136 = NG_CISCO_IPADDR_TYPE_INFO;
137 static const struct ng_parse_type ng_cisco_ipaddr_type = {
138 &ng_parse_struct_type,
139 &ng_cisco_ipaddr_type_fields
142 /* Parse type for struct ng_async_stat */
143 static const struct ng_parse_struct_field ng_cisco_stats_type_fields[]
144 = NG_CISCO_STATS_TYPE_INFO;
145 static const struct ng_parse_type ng_cisco_stats_type = {
146 &ng_parse_struct_type,
147 &ng_cisco_stats_type_fields
150 /* List of commands and how to convert arguments to/from ASCII */
151 static const struct ng_cmdlist ng_cisco_cmdlist[] = {
154 NGM_CISCO_SET_IPADDR,
156 &ng_cisco_ipaddr_type,
161 NGM_CISCO_GET_IPADDR,
164 &ng_cisco_ipaddr_type
168 NGM_CISCO_GET_STATUS,
177 static struct ng_type typestruct = {
192 NETGRAPH_INIT(cisco, &typestruct);
198 cisco_constructor(node_p *nodep)
203 MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_NOWAIT);
206 bzero(sc, sizeof(struct cisco_priv));
208 callout_init(&sc->timeout);
209 if ((error = ng_make_node_common(&typestruct, nodep))) {
210 FREE(sc, M_NETGRAPH);
213 (*nodep)->private = sc;
216 /* Initialise the varous protocol hook holders */
217 sc->downstream.af = 0xffff;
218 sc->inet.af = AF_INET;
219 sc->inet6.af = AF_INET6;
220 sc->atalk.af = AF_APPLETALK;
229 cisco_newhook(node_p node, hook_p hook, const char *name)
231 const sc_p sc = node->private;
233 if (strcmp(name, NG_CISCO_HOOK_DOWNSTREAM) == 0) {
234 sc->downstream.hook = hook;
235 hook->private = &sc->downstream;
237 /* Start keepalives */
238 callout_reset(&sc->timeout, hz * KEEPALIVE_SECS,
239 cisco_keepalive, sc);
240 } else if (strcmp(name, NG_CISCO_HOOK_INET) == 0) {
241 sc->inet.hook = hook;
242 hook->private = &sc->inet;
243 } else if (strcmp(name, NG_CISCO_HOOK_APPLETALK) == 0) {
244 sc->atalk.hook = hook;
245 hook->private = &sc->atalk;
246 } else if (strcmp(name, NG_CISCO_HOOK_IPX) == 0) {
248 hook->private = &sc->ipx;
249 } else if (strcmp(name, NG_CISCO_HOOK_DEBUG) == 0) {
250 hook->private = NULL; /* unimplemented */
257 * Receive control message.
260 cisco_rcvmsg(node_p node, struct ng_mesg *msg,
261 const char *retaddr, struct ng_mesg **rptr)
263 const sc_p sc = node->private;
264 struct ng_mesg *resp = NULL;
267 switch (msg->header.typecookie) {
268 case NGM_GENERIC_COOKIE:
269 switch (msg->header.cmd) {
270 case NGM_TEXT_STATUS:
275 NG_MKRESPONSE(resp, msg, sizeof(struct ng_mesg)
276 + NG_TEXTRESPONSE, M_NOWAIT);
281 arg = (char *) resp->data;
283 "keepalive period: %d sec; ", KEEPALIVE_SECS);
284 pos += sprintf(arg + pos,
285 "unacknowledged keepalives: %ld", sc->seqRetries);
286 resp->header.arglen = pos + 1;
294 case NGM_CISCO_COOKIE:
295 switch (msg->header.cmd) {
296 case NGM_CISCO_GET_IPADDR: /* could be a late reply! */
297 if ((msg->header.flags & NGF_RESP) == 0) {
300 NG_MKRESPONSE(resp, msg,
301 2 * sizeof(*ips), M_NOWAIT);
306 ips = (struct in_addr *) resp->data;
307 ips[0] = sc->localip;
308 ips[1] = sc->localmask;
311 /* FALLTHROUGH */ /* ...if it's a reply */
312 case NGM_CISCO_SET_IPADDR:
314 struct in_addr *const ips = (struct in_addr *)msg->data;
316 if (msg->header.arglen < 2 * sizeof(*ips)) {
320 sc->localip = ips[0];
321 sc->localmask = ips[1];
324 case NGM_CISCO_GET_STATUS:
326 struct ng_cisco_stats *stat;
328 NG_MKRESPONSE(resp, msg, sizeof(*stat), M_NOWAIT);
333 stat = (struct ng_cisco_stats *)resp->data;
334 stat->seqRetries = sc->seqRetries;
335 stat->keepAlivePeriod = KEEPALIVE_SECS;
350 FREE(resp, M_NETGRAPH);
351 FREE(msg, M_NETGRAPH);
359 cisco_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
361 const sc_p sc = hook->node->private;
362 struct protoent *pep;
363 struct cisco_header *h;
366 if ((pep = hook->private) == NULL)
369 /* If it came from our downlink, deal with it separately */
370 if (pep->af == 0xffff)
371 return (cisco_input(sc, m, meta));
373 /* OK so it came from a protocol, heading out. Prepend general data
374 packet header. For now, IP,IPX only */
375 M_PREPEND(m, CISCO_HEADER_LEN, MB_DONTWAIT);
380 h = mtod(m, struct cisco_header *);
381 h->address = CISCO_UNICAST;
385 case AF_INET: /* Internet Protocol */
386 h->protocol = htons(ETHERTYPE_IP);
389 h->protocol = htons(ETHERTYPE_IPV6);
391 case AF_APPLETALK: /* AppleTalk Protocol */
392 h->protocol = htons(ETHERTYPE_AT);
394 case AF_IPX: /* Novell IPX Protocol */
395 h->protocol = htons(ETHERTYPE_IPX);
398 error = EAFNOSUPPORT;
403 NG_SEND_DATA(error, sc->downstream.hook, m, meta);
407 NG_FREE_DATA(m, meta);
415 cisco_rmnode(node_p node)
417 const sc_p sc = node->private;
419 node->flags |= NG_INVALID;
422 node->private = NULL;
424 FREE(sc, M_NETGRAPH);
429 * Disconnection of a hook
431 * For this type, removal of the last link destroys the node
434 cisco_disconnect(hook_p hook)
436 const sc_p sc = hook->node->private;
437 struct protoent *pep;
439 /* Check it's not the debug hook */
440 if ((pep = hook->private)) {
442 if (pep->af == 0xffff) {
443 /* If it is the downstream hook, stop the timers */
444 callout_stop(&sc->timeout);
448 /* If no more hooks, remove the node */
449 if (hook->node->numhooks == 0)
450 ng_rmnode(hook->node);
458 cisco_input(sc_p sc, struct mbuf *m, meta_p meta)
460 const struct cisco_header *h;
461 struct cisco_header hdrbuf;
462 struct protoent *pep;
465 /* Sanity check header length */
466 if (m->m_pkthdr.len < sizeof(*h)) {
471 /* Get cisco header */
472 if (m->m_len >= sizeof(*h)) /* the common case */
473 h = mtod(m, const struct cisco_header *);
475 m_copydata(m, 0, sizeof(*h), (caddr_t)&hdrbuf);
478 m_adj(m, sizeof(*h));
480 /* Check header address */
481 switch (h->address) {
482 default: /* Invalid Cisco packet. */
485 case CISCO_MULTICAST:
486 /* Don't check the control field here (RFC 1547). */
487 switch (ntohs(h->protocol)) {
490 case CISCO_KEEPALIVE:
492 const struct cisco_packet *p;
493 struct cisco_packet pktbuf;
495 /* Sanity check packet length */
496 if (m->m_pkthdr.len < sizeof(*p)) {
501 /* Get cisco packet */
502 if (m->m_len >= sizeof(*p)) /* the common case */
503 p = mtod(m, const struct cisco_packet *);
505 m_copydata(m, 0, sizeof(*p), (caddr_t)&pktbuf);
509 /* Check packet type */
510 switch (ntohl(p->type)) {
513 "cisco: unknown cisco packet type: 0x%"PRIx32"\n",
516 case CISCO_ADDR_REPLY:
517 /* Reply on address request, ignore */
519 case CISCO_KEEPALIVE_REQ:
520 sc->remote_seq = ntohl(p->par1);
521 if (sc->local_seq == ntohl(p->par2)) {
528 struct ng_mesg *msg, *resp;
530 /* Ask inet peer for IP address information */
531 if (sc->inet.hook == NULL)
533 NG_MKMESSAGE(msg, NGM_CISCO_COOKIE,
534 NGM_CISCO_GET_IPADDR, 0, M_NOWAIT);
537 ng_send_msg(sc->node, msg,
538 NG_CISCO_HOOK_INET, &resp);
540 cisco_rcvmsg(sc->node, resp, ".", NULL);
543 /* Send reply to peer device */
544 error = cisco_send(sc, CISCO_ADDR_REPLY,
545 ntohl(sc->localip.s_addr),
546 ntohl(sc->localmask.s_addr));
568 /* Drop if payload is empty */
569 if (m->m_pkthdr.len == 0) {
575 if (pep->hook == NULL)
577 NG_SEND_DATA(error, pep->hook, m, meta);
581 NG_FREE_DATA(m, meta);
587 * Send keepalive packets, every 10 seconds.
590 cisco_keepalive(void *arg)
595 cisco_send(sc, CISCO_KEEPALIVE_REQ, sc->local_seq, sc->remote_seq);
598 callout_reset(&sc->timeout, hz * KEEPALIVE_SECS,
599 cisco_keepalive, sc);
603 * Send Cisco keepalive packet.
606 cisco_send(sc_p sc, int type, long par1, long par2)
608 struct cisco_header *h;
609 struct cisco_packet *ch;
618 MGETHDR(m, MB_DONTWAIT, MT_DATA);
622 t = (time.tv_sec - boottime.tv_sec) * 1000;
623 m->m_pkthdr.len = m->m_len = CISCO_HEADER_LEN + CISCO_PACKET_LEN;
624 m->m_pkthdr.rcvif = 0;
626 h = mtod(m, struct cisco_header *);
627 h->address = CISCO_MULTICAST;
629 h->protocol = htons(CISCO_KEEPALIVE);
631 ch = (struct cisco_packet *) (h + 1);
632 ch->type = htonl(type);
633 ch->par1 = htonl(par1);
634 ch->par2 = htonl(par2);
636 ch->time0 = htons((u_short) (t >> 16));
637 ch->time1 = htons((u_short) t);
639 NG_SEND_DATA(error, sc->downstream.hook, m, meta);