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.3 2003/08/07 21:17:31 dillon 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>
55 #include <netinet/in.h>
56 #include <netinet/if_ether.h>
58 #include <netatalk/at.h>
60 #include <netipx/ipx.h>
61 #include <netipx/ipx_if.h>
63 #include <netgraph/ng_message.h>
64 #include <netgraph/netgraph.h>
65 #include <netgraph/ng_parse.h>
68 #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
69 #define CISCO_UNICAST 0x0f /* Cisco unicast address */
70 #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
71 #define CISCO_ADDR_REQ 0 /* Cisco address request */
72 #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
73 #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
75 #define KEEPALIVE_SECS 10
83 #define CISCO_HEADER_LEN sizeof (struct cisco_header)
94 #define CISCO_PACKET_LEN (sizeof(struct cisco_packet))
97 hook_p hook; /* the hook for this proto */
98 u_short af; /* address family, -1 = downstream */
104 u_long seqRetries; /* how many times we've been here throwing out
105 * the same sequence number without ack */
107 struct callout_handle handle;
108 struct protoent downstream;
109 struct protoent inet; /* IP information */
110 struct in_addr localip;
111 struct in_addr localmask;
112 struct protoent inet6; /* IPv6 information */
113 struct protoent atalk; /* AppleTalk information */
114 struct protoent ipx; /* IPX information */
116 typedef struct cisco_priv *sc_p;
118 /* Netgraph methods */
119 static ng_constructor_t cisco_constructor;
120 static ng_rcvmsg_t cisco_rcvmsg;
121 static ng_shutdown_t cisco_rmnode;
122 static ng_newhook_t cisco_newhook;
123 static ng_rcvdata_t cisco_rcvdata;
124 static ng_disconnect_t cisco_disconnect;
126 /* Other functions */
127 static int cisco_input(sc_p sc, struct mbuf *m, meta_p meta);
128 static void cisco_keepalive(void *arg);
129 static int cisco_send(sc_p sc, int type, long par1, long par2);
131 /* Parse type for struct ng_cisco_ipaddr */
132 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
133 = NG_CISCO_IPADDR_TYPE_INFO;
134 static const struct ng_parse_type ng_cisco_ipaddr_type = {
135 &ng_parse_struct_type,
136 &ng_cisco_ipaddr_type_fields
139 /* Parse type for struct ng_async_stat */
140 static const struct ng_parse_struct_field ng_cisco_stats_type_fields[]
141 = NG_CISCO_STATS_TYPE_INFO;
142 static const struct ng_parse_type ng_cisco_stats_type = {
143 &ng_parse_struct_type,
144 &ng_cisco_stats_type_fields
147 /* List of commands and how to convert arguments to/from ASCII */
148 static const struct ng_cmdlist ng_cisco_cmdlist[] = {
151 NGM_CISCO_SET_IPADDR,
153 &ng_cisco_ipaddr_type,
158 NGM_CISCO_GET_IPADDR,
161 &ng_cisco_ipaddr_type
165 NGM_CISCO_GET_STATUS,
174 static struct ng_type typestruct = {
189 NETGRAPH_INIT(cisco, &typestruct);
195 cisco_constructor(node_p *nodep)
200 MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_NOWAIT);
203 bzero(sc, sizeof(struct cisco_priv));
205 callout_handle_init(&sc->handle);
206 if ((error = ng_make_node_common(&typestruct, nodep))) {
207 FREE(sc, M_NETGRAPH);
210 (*nodep)->private = sc;
213 /* Initialise the varous protocol hook holders */
214 sc->downstream.af = 0xffff;
215 sc->inet.af = AF_INET;
216 sc->inet6.af = AF_INET6;
217 sc->atalk.af = AF_APPLETALK;
226 cisco_newhook(node_p node, hook_p hook, const char *name)
228 const sc_p sc = node->private;
230 if (strcmp(name, NG_CISCO_HOOK_DOWNSTREAM) == 0) {
231 sc->downstream.hook = hook;
232 hook->private = &sc->downstream;
234 /* Start keepalives */
235 sc->handle = timeout(cisco_keepalive, sc, hz * KEEPALIVE_SECS);
236 } else if (strcmp(name, NG_CISCO_HOOK_INET) == 0) {
237 sc->inet.hook = hook;
238 hook->private = &sc->inet;
239 } else if (strcmp(name, NG_CISCO_HOOK_APPLETALK) == 0) {
240 sc->atalk.hook = hook;
241 hook->private = &sc->atalk;
242 } else if (strcmp(name, NG_CISCO_HOOK_IPX) == 0) {
244 hook->private = &sc->ipx;
245 } else if (strcmp(name, NG_CISCO_HOOK_DEBUG) == 0) {
246 hook->private = NULL; /* unimplemented */
253 * Receive control message.
256 cisco_rcvmsg(node_p node, struct ng_mesg *msg,
257 const char *retaddr, struct ng_mesg **rptr)
259 const sc_p sc = node->private;
260 struct ng_mesg *resp = NULL;
263 switch (msg->header.typecookie) {
264 case NGM_GENERIC_COOKIE:
265 switch (msg->header.cmd) {
266 case NGM_TEXT_STATUS:
271 NG_MKRESPONSE(resp, msg, sizeof(struct ng_mesg)
272 + NG_TEXTRESPONSE, M_NOWAIT);
277 arg = (char *) resp->data;
279 "keepalive period: %d sec; ", KEEPALIVE_SECS);
280 pos += sprintf(arg + pos,
281 "unacknowledged keepalives: %ld", sc->seqRetries);
282 resp->header.arglen = pos + 1;
290 case NGM_CISCO_COOKIE:
291 switch (msg->header.cmd) {
292 case NGM_CISCO_GET_IPADDR: /* could be a late reply! */
293 if ((msg->header.flags & NGF_RESP) == 0) {
296 NG_MKRESPONSE(resp, msg,
297 2 * sizeof(*ips), M_NOWAIT);
302 ips = (struct in_addr *) resp->data;
303 ips[0] = sc->localip;
304 ips[1] = sc->localmask;
307 /* FALLTHROUGH */ /* ...if it's a reply */
308 case NGM_CISCO_SET_IPADDR:
310 struct in_addr *const ips = (struct in_addr *)msg->data;
312 if (msg->header.arglen < 2 * sizeof(*ips)) {
316 sc->localip = ips[0];
317 sc->localmask = ips[1];
320 case NGM_CISCO_GET_STATUS:
322 struct ng_cisco_stats *stat;
324 NG_MKRESPONSE(resp, msg, sizeof(*stat), M_NOWAIT);
329 stat = (struct ng_cisco_stats *)resp->data;
330 stat->seqRetries = sc->seqRetries;
331 stat->keepAlivePeriod = KEEPALIVE_SECS;
346 FREE(resp, M_NETGRAPH);
347 FREE(msg, M_NETGRAPH);
355 cisco_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
357 const sc_p sc = hook->node->private;
358 struct protoent *pep;
359 struct cisco_header *h;
362 if ((pep = hook->private) == NULL)
365 /* If it came from our downlink, deal with it separately */
366 if (pep->af == 0xffff)
367 return (cisco_input(sc, m, meta));
369 /* OK so it came from a protocol, heading out. Prepend general data
370 packet header. For now, IP,IPX only */
371 M_PREPEND(m, CISCO_HEADER_LEN, M_DONTWAIT);
376 h = mtod(m, struct cisco_header *);
377 h->address = CISCO_UNICAST;
381 case AF_INET: /* Internet Protocol */
382 h->protocol = htons(ETHERTYPE_IP);
385 h->protocol = htons(ETHERTYPE_IPV6);
387 case AF_APPLETALK: /* AppleTalk Protocol */
388 h->protocol = htons(ETHERTYPE_AT);
390 case AF_IPX: /* Novell IPX Protocol */
391 h->protocol = htons(ETHERTYPE_IPX);
394 error = EAFNOSUPPORT;
399 NG_SEND_DATA(error, sc->downstream.hook, m, meta);
403 NG_FREE_DATA(m, meta);
411 cisco_rmnode(node_p node)
413 const sc_p sc = node->private;
415 node->flags |= NG_INVALID;
418 node->private = NULL;
420 FREE(sc, M_NETGRAPH);
425 * Disconnection of a hook
427 * For this type, removal of the last link destroys the node
430 cisco_disconnect(hook_p hook)
432 const sc_p sc = hook->node->private;
433 struct protoent *pep;
435 /* Check it's not the debug hook */
436 if ((pep = hook->private)) {
438 if (pep->af == 0xffff) {
439 /* If it is the downstream hook, stop the timers */
440 untimeout(cisco_keepalive, sc, sc->handle);
444 /* If no more hooks, remove the node */
445 if (hook->node->numhooks == 0)
446 ng_rmnode(hook->node);
454 cisco_input(sc_p sc, struct mbuf *m, meta_p meta)
456 const struct cisco_header *h;
457 struct cisco_header hdrbuf;
458 struct protoent *pep;
461 /* Sanity check header length */
462 if (m->m_pkthdr.len < sizeof(*h)) {
467 /* Get cisco header */
468 if (m->m_len >= sizeof(*h)) /* the common case */
469 h = mtod(m, const struct cisco_header *);
471 m_copydata(m, 0, sizeof(*h), (caddr_t)&hdrbuf);
474 m_adj(m, sizeof(*h));
476 /* Check header address */
477 switch (h->address) {
478 default: /* Invalid Cisco packet. */
481 case CISCO_MULTICAST:
482 /* Don't check the control field here (RFC 1547). */
483 switch (ntohs(h->protocol)) {
486 case CISCO_KEEPALIVE:
488 const struct cisco_packet *p;
489 struct cisco_packet pktbuf;
491 /* Sanity check packet length */
492 if (m->m_pkthdr.len < sizeof(*p)) {
497 /* Get cisco packet */
498 if (m->m_len >= sizeof(*p)) /* the common case */
499 p = mtod(m, const struct cisco_packet *);
501 m_copydata(m, 0, sizeof(*p), (caddr_t)&pktbuf);
505 /* Check packet type */
506 switch (ntohl(p->type)) {
509 "cisco: unknown cisco packet type: 0x%lx\n",
512 case CISCO_ADDR_REPLY:
513 /* Reply on address request, ignore */
515 case CISCO_KEEPALIVE_REQ:
516 sc->remote_seq = ntohl(p->par1);
517 if (sc->local_seq == ntohl(p->par2)) {
524 struct ng_mesg *msg, *resp;
526 /* Ask inet peer for IP address information */
527 if (sc->inet.hook == NULL)
529 NG_MKMESSAGE(msg, NGM_CISCO_COOKIE,
530 NGM_CISCO_GET_IPADDR, 0, M_NOWAIT);
533 ng_send_msg(sc->node, msg,
534 NG_CISCO_HOOK_INET, &resp);
536 cisco_rcvmsg(sc->node, resp, ".", NULL);
539 /* Send reply to peer device */
540 error = cisco_send(sc, CISCO_ADDR_REPLY,
541 ntohl(sc->localip.s_addr),
542 ntohl(sc->localmask.s_addr));
564 /* Drop if payload is empty */
565 if (m->m_pkthdr.len == 0) {
571 if (pep->hook == NULL)
573 NG_SEND_DATA(error, pep->hook, m, meta);
577 NG_FREE_DATA(m, meta);
583 * Send keepalive packets, every 10 seconds.
586 cisco_keepalive(void *arg)
591 cisco_send(sc, CISCO_KEEPALIVE_REQ, sc->local_seq, sc->remote_seq);
594 sc->handle = timeout(cisco_keepalive, sc, hz * KEEPALIVE_SECS);
598 * Send Cisco keepalive packet.
601 cisco_send(sc_p sc, int type, long par1, long par2)
603 struct cisco_header *h;
604 struct cisco_packet *ch;
613 MGETHDR(m, M_DONTWAIT, MT_DATA);
617 t = (time.tv_sec - boottime.tv_sec) * 1000;
618 m->m_pkthdr.len = m->m_len = CISCO_HEADER_LEN + CISCO_PACKET_LEN;
619 m->m_pkthdr.rcvif = 0;
621 h = mtod(m, struct cisco_header *);
622 h->address = CISCO_MULTICAST;
624 h->protocol = htons(CISCO_KEEPALIVE);
626 ch = (struct cisco_packet *) (h + 1);
627 ch->type = htonl(type);
628 ch->par1 = htonl(par1);
629 ch->par2 = htonl(par2);
631 ch->time0 = htons((u_short) (t >> 16));
632 ch->time1 = htons((u_short) t);
634 NG_SEND_DATA(error, sc->downstream.hook, m, meta);