6 * Copyright (c) 1996-1999 Whistle Communications, Inc.
9 * Subject to the following obligations and disclaimer of warranty, use and
10 * redistribution of this software, in source or object code forms, with or
11 * without modifications are expressly permitted by Whistle Communications;
12 * provided, however, that:
13 * 1. Any and all reproductions of the source or object code must include the
14 * copyright notice above and the following disclaimer of warranties; and
15 * 2. No rights are granted, in any manner or form, to use Whistle
16 * Communications, Inc. trademarks, including the mark "WHISTLE
17 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18 * such appears in the above copyright notice or in the software.
20 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
38 * Author: Julian Elischer <julian@freebsd.org>
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>
66 #include <netgraph/ng_cisco.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 uint16_t af; /* address family, -1 = downstream */
104 uint32_t seqRetries; /* how many times we've been here throwing out
105 * the same sequence number without ack */
107 struct callout 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_shutdown;
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, item_p item);
128 static void cisco_keepalive(node_p node, hook_p hook, void *arg1, int arg2);
129 static int cisco_send(sc_p sc, int type, long par1, long par2);
130 static void cisco_notify(sc_p sc, uint32_t cmd);
132 /* Parse type for struct ng_cisco_ipaddr */
133 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
134 = NG_CISCO_IPADDR_TYPE_INFO;
135 static const struct ng_parse_type ng_cisco_ipaddr_type = {
136 &ng_parse_struct_type,
137 &ng_cisco_ipaddr_type_fields
140 /* Parse type for struct ng_async_stat */
141 static const struct ng_parse_struct_field ng_cisco_stats_type_fields[]
142 = NG_CISCO_STATS_TYPE_INFO;
143 static const struct ng_parse_type ng_cisco_stats_type = {
144 &ng_parse_struct_type,
145 &ng_cisco_stats_type_fields
148 /* List of commands and how to convert arguments to/from ASCII */
149 static const struct ng_cmdlist ng_cisco_cmdlist[] = {
152 NGM_CISCO_SET_IPADDR,
154 &ng_cisco_ipaddr_type,
159 NGM_CISCO_GET_IPADDR,
162 &ng_cisco_ipaddr_type
166 NGM_CISCO_GET_STATUS,
175 static struct ng_type typestruct = {
176 .version = NG_ABI_VERSION,
177 .name = NG_CISCO_NODE_TYPE,
178 .constructor = cisco_constructor,
179 .rcvmsg = cisco_rcvmsg,
180 .shutdown = cisco_shutdown,
181 .newhook = cisco_newhook,
182 .rcvdata = cisco_rcvdata,
183 .disconnect = cisco_disconnect,
184 .cmdlist = ng_cisco_cmdlist,
186 NETGRAPH_INIT(cisco, &typestruct);
192 cisco_constructor(node_p node)
196 sc = malloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
198 ng_callout_init(&sc->handle);
199 NG_NODE_SET_PRIVATE(node, sc);
202 /* Initialise the varous protocol hook holders */
203 sc->downstream.af = 0xffff;
204 sc->inet.af = AF_INET;
205 sc->inet6.af = AF_INET6;
206 sc->atalk.af = AF_APPLETALK;
215 cisco_newhook(node_p node, hook_p hook, const char *name)
217 const sc_p sc = NG_NODE_PRIVATE(node);
219 if (strcmp(name, NG_CISCO_HOOK_DOWNSTREAM) == 0) {
220 sc->downstream.hook = hook;
221 NG_HOOK_SET_PRIVATE(hook, &sc->downstream);
223 /* Start keepalives */
224 ng_callout(&sc->handle, node, NULL, (hz * KEEPALIVE_SECS),
225 &cisco_keepalive, (void *)sc, 0);
226 } else if (strcmp(name, NG_CISCO_HOOK_INET) == 0) {
227 sc->inet.hook = hook;
228 NG_HOOK_SET_PRIVATE(hook, &sc->inet);
229 } else if (strcmp(name, NG_CISCO_HOOK_INET6) == 0) {
230 sc->inet6.hook = hook;
231 NG_HOOK_SET_PRIVATE(hook, &sc->inet6);
232 } else if (strcmp(name, NG_CISCO_HOOK_APPLETALK) == 0) {
233 sc->atalk.hook = hook;
234 NG_HOOK_SET_PRIVATE(hook, &sc->atalk);
235 } else if (strcmp(name, NG_CISCO_HOOK_IPX) == 0) {
237 NG_HOOK_SET_PRIVATE(hook, &sc->ipx);
238 } else if (strcmp(name, NG_CISCO_HOOK_DEBUG) == 0) {
239 NG_HOOK_SET_PRIVATE(hook, NULL); /* unimplemented */
246 * Receive control message.
249 cisco_rcvmsg(node_p node, item_p item, hook_p lasthook)
252 const sc_p sc = NG_NODE_PRIVATE(node);
253 struct ng_mesg *resp = NULL;
256 NGI_GET_MSG(item, msg);
257 switch (msg->header.typecookie) {
258 case NGM_GENERIC_COOKIE:
259 switch (msg->header.cmd) {
260 case NGM_TEXT_STATUS:
265 NG_MKRESPONSE(resp, msg, NG_TEXTRESPONSE, M_NOWAIT);
270 arg = (char *) resp->data;
272 "keepalive period: %d sec; ", KEEPALIVE_SECS);
273 pos += sprintf(arg + pos,
274 "unacknowledged keepalives: %d", sc->seqRetries);
275 resp->header.arglen = pos + 1;
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) {
289 NG_MKRESPONSE(resp, msg,
290 2 * sizeof(*ips), M_NOWAIT);
295 ips = (struct in_addr *) resp->data;
296 ips[0] = sc->localip;
297 ips[1] = sc->localmask;
300 /* FALLTHROUGH */ /* ...if it's a reply */
301 case NGM_CISCO_SET_IPADDR:
303 struct in_addr *const ips = (struct in_addr *)msg->data;
305 if (msg->header.arglen < 2 * sizeof(*ips)) {
309 sc->localip = ips[0];
310 sc->localmask = ips[1];
313 case NGM_CISCO_GET_STATUS:
315 struct ng_cisco_stats *stat;
317 NG_MKRESPONSE(resp, msg, sizeof(*stat), M_NOWAIT);
322 stat = (struct ng_cisco_stats *)resp->data;
323 stat->seqRetries = sc->seqRetries;
324 stat->keepAlivePeriod = KEEPALIVE_SECS;
336 NG_RESPOND_MSG(error, node, item, resp);
345 cisco_rcvdata(hook_p hook, item_p item)
347 const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
348 struct protoent *pep;
349 struct cisco_header *h;
353 if ((pep = NG_HOOK_PRIVATE(hook)) == NULL)
356 /* If it came from our downlink, deal with it separately */
357 if (pep->af == 0xffff)
358 return (cisco_input(sc, item));
360 /* OK so it came from a protocol, heading out. Prepend general data
361 packet header. For now, IP,IPX only */
363 M_PREPEND(m, CISCO_HEADER_LEN, M_DONTWAIT);
369 h = mtod(m, struct cisco_header *);
370 h->address = CISCO_UNICAST;
374 case AF_INET: /* Internet Protocol */
375 h->protocol = htons(ETHERTYPE_IP);
378 h->protocol = htons(ETHERTYPE_IPV6);
380 case AF_APPLETALK: /* AppleTalk Protocol */
381 h->protocol = htons(ETHERTYPE_AT);
383 case AF_IPX: /* Novell IPX Protocol */
384 h->protocol = htons(ETHERTYPE_IPX);
387 error = EAFNOSUPPORT;
392 NG_FWD_NEW_DATA(error, item, sc->downstream.hook, m);
404 cisco_shutdown(node_p node)
406 const sc_p sc = NG_NODE_PRIVATE(node);
408 NG_NODE_SET_PRIVATE(node, NULL);
409 NG_NODE_UNREF(sc->node);
410 free(sc, M_NETGRAPH);
415 * Disconnection of a hook
417 * For this type, removal of the last link destroys the node
420 cisco_disconnect(hook_p hook)
422 const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
423 struct protoent *pep;
425 /* Check it's not the debug hook */
426 if ((pep = NG_HOOK_PRIVATE(hook))) {
428 if (pep->af == 0xffff)
429 /* If it is the downstream hook, stop the timers */
430 ng_uncallout(&sc->handle, NG_HOOK_NODE(hook));
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));
444 cisco_input(sc_p sc, item_p item)
446 const struct cisco_header *h;
447 struct cisco_header hdrbuf;
448 struct protoent *pep;
455 /* Sanity check header length */
456 if (m->m_pkthdr.len < sizeof(*h)) {
461 /* Get cisco header */
462 if (m->m_len >= sizeof(*h)) /* the common case */
463 h = mtod(m, const struct cisco_header *);
465 m_copydata(m, 0, sizeof(*h), (caddr_t)&hdrbuf);
468 m_adj(m, sizeof(*h));
470 /* Check header address */
471 switch (h->address) {
472 default: /* Invalid Cisco packet. */
475 case CISCO_MULTICAST:
476 /* Don't check the control field here (RFC 1547). */
477 switch (ntohs(h->protocol)) {
480 case CISCO_KEEPALIVE:
482 const struct cisco_packet *p;
483 struct cisco_packet pktbuf;
485 /* Sanity check packet length */
486 if (m->m_pkthdr.len < sizeof(*p)) {
491 /* Get cisco packet */
492 if (m->m_len >= sizeof(*p)) /* the common case */
493 p = mtod(m, const struct cisco_packet *);
495 m_copydata(m, 0, sizeof(*p), (caddr_t)&pktbuf);
499 /* Check packet type */
500 switch (ntohl(p->type)) {
503 "cisco: unknown cisco packet type: 0x%lx\n",
504 (long)ntohl(p->type));
506 case CISCO_ADDR_REPLY:
507 /* Reply on address request, ignore */
509 case CISCO_KEEPALIVE_REQ:
510 sc->remote_seq = ntohl(p->par1);
511 if (sc->local_seq == ntohl(p->par2)) {
513 if (sc->seqRetries > 1)
514 cisco_notify(sc, NGM_LINK_IS_UP);
523 /* Ask inet peer for IP address information */
524 if (sc->inet.hook == NULL)
526 NG_MKMESSAGE(msg, NGM_CISCO_COOKIE,
527 NGM_CISCO_GET_IPADDR, 0, M_NOWAIT);
530 NG_SEND_MSG_HOOK(dummy_error,
531 sc->node, msg, sc->inet.hook, 0);
533 * XXX Now maybe we should set a flag telling
534 * our receiver to send this message when the response comes in
535 * instead of now when the data may be bad.
538 /* Send reply to peer device */
539 error = cisco_send(sc, CISCO_ADDR_REPLY,
540 ntohl(sc->localip.s_addr),
541 ntohl(sc->localmask.s_addr));
563 /* Drop if payload is empty */
564 if (m->m_pkthdr.len == 0) {
570 if (pep->hook == NULL)
572 NG_FWD_NEW_DATA(error, item, pep->hook, m);
582 * Send keepalive packets, every 10 seconds.
585 cisco_keepalive(node_p node, hook_p hook, void *arg1, int arg2)
587 const sc_p sc = arg1;
589 cisco_send(sc, CISCO_KEEPALIVE_REQ, sc->local_seq, sc->remote_seq);
590 if (sc->seqRetries++ > 1)
591 cisco_notify(sc, NGM_LINK_IS_DOWN);
592 ng_callout(&sc->handle, node, NULL, (hz * KEEPALIVE_SECS),
593 &cisco_keepalive, (void *)sc, 0);
597 * Send Cisco keepalive packet.
600 cisco_send(sc_p sc, int type, long par1, long par2)
602 struct cisco_header *h;
603 struct cisco_packet *ch;
609 getmicrouptime(&time);
611 MGETHDR(m, M_DONTWAIT, MT_DATA);
615 t = time.tv_sec * 1000 + time.tv_usec / 1000;
616 m->m_pkthdr.len = m->m_len = CISCO_HEADER_LEN + CISCO_PACKET_LEN;
617 m->m_pkthdr.rcvif = 0;
619 h = mtod(m, struct cisco_header *);
620 h->address = CISCO_MULTICAST;
622 h->protocol = htons(CISCO_KEEPALIVE);
624 ch = (struct cisco_packet *) (h + 1);
625 ch->type = htonl(type);
626 ch->par1 = htonl(par1);
627 ch->par2 = htonl(par2);
629 ch->time0 = htons((uint16_t) (t >> 16));
630 ch->time1 = htons((uint16_t) t);
632 NG_SEND_DATA_ONLY(error, sc->downstream.hook, m);
637 * Send linkstate to upstream node.
640 cisco_notify(sc_p sc, uint32_t cmd)
645 if (sc->inet.hook == NULL) /* nothing to notify */
648 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
650 NG_SEND_MSG_HOOK(dummy_error, sc->node, msg, sc->inet.hook, 0);