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>
39 * $Whistle: ng_cisco.c,v 1.25 1999/11/01 09:24:51 julian Exp $
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/errno.h>
45 #include <sys/kernel.h>
46 #include <sys/socket.h>
47 #include <sys/malloc.h>
49 #include <sys/syslog.h>
53 #include <netinet/in.h>
54 #include <netinet/if_ether.h>
56 #include <netgraph/ng_message.h>
57 #include <netgraph/netgraph.h>
58 #include <netgraph/ng_parse.h>
59 #include <netgraph/ng_cisco.h>
61 #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
62 #define CISCO_UNICAST 0x0f /* Cisco unicast address */
63 #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
64 #define CISCO_ADDR_REQ 0 /* Cisco address request */
65 #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
66 #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
68 #define KEEPALIVE_SECS 10
76 #define CISCO_HEADER_LEN sizeof (struct cisco_header)
87 #define CISCO_PACKET_LEN (sizeof(struct cisco_packet))
90 hook_p hook; /* the hook for this proto */
91 uint16_t af; /* address family, -1 = downstream */
97 uint32_t seqRetries; /* how many times we've been here throwing out
98 * the same sequence number without ack */
100 struct callout handle;
101 struct protoent downstream;
102 struct protoent inet; /* IP information */
103 struct in_addr localip;
104 struct in_addr localmask;
105 struct protoent inet6; /* IPv6 information */
106 struct protoent atalk; /* AppleTalk information */
107 struct protoent ipx; /* IPX information */
109 typedef struct cisco_priv *sc_p;
111 /* Netgraph methods */
112 static ng_constructor_t cisco_constructor;
113 static ng_rcvmsg_t cisco_rcvmsg;
114 static ng_shutdown_t cisco_shutdown;
115 static ng_newhook_t cisco_newhook;
116 static ng_rcvdata_t cisco_rcvdata;
117 static ng_disconnect_t cisco_disconnect;
119 /* Other functions */
120 static int cisco_input(sc_p sc, item_p item);
121 static void cisco_keepalive(node_p node, hook_p hook, void *arg1, int arg2);
122 static int cisco_send(sc_p sc, int type, long par1, long par2);
123 static void cisco_notify(sc_p sc, uint32_t cmd);
125 /* Parse type for struct ng_cisco_ipaddr */
126 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
127 = NG_CISCO_IPADDR_TYPE_INFO;
128 static const struct ng_parse_type ng_cisco_ipaddr_type = {
129 &ng_parse_struct_type,
130 &ng_cisco_ipaddr_type_fields
133 /* Parse type for struct ng_async_stat */
134 static const struct ng_parse_struct_field ng_cisco_stats_type_fields[]
135 = NG_CISCO_STATS_TYPE_INFO;
136 static const struct ng_parse_type ng_cisco_stats_type = {
137 &ng_parse_struct_type,
138 &ng_cisco_stats_type_fields
141 /* List of commands and how to convert arguments to/from ASCII */
142 static const struct ng_cmdlist ng_cisco_cmdlist[] = {
145 NGM_CISCO_SET_IPADDR,
147 &ng_cisco_ipaddr_type,
152 NGM_CISCO_GET_IPADDR,
155 &ng_cisco_ipaddr_type
159 NGM_CISCO_GET_STATUS,
168 static struct ng_type typestruct = {
169 .version = NG_ABI_VERSION,
170 .name = NG_CISCO_NODE_TYPE,
171 .constructor = cisco_constructor,
172 .rcvmsg = cisco_rcvmsg,
173 .shutdown = cisco_shutdown,
174 .newhook = cisco_newhook,
175 .rcvdata = cisco_rcvdata,
176 .disconnect = cisco_disconnect,
177 .cmdlist = ng_cisco_cmdlist,
179 NETGRAPH_INIT(cisco, &typestruct);
185 cisco_constructor(node_p node)
189 sc = malloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
191 ng_callout_init(&sc->handle);
192 NG_NODE_SET_PRIVATE(node, sc);
195 /* Initialise the varous protocol hook holders */
196 sc->downstream.af = 0xffff;
197 sc->inet.af = AF_INET;
198 sc->inet6.af = AF_INET6;
199 sc->atalk.af = AF_APPLETALK;
208 cisco_newhook(node_p node, hook_p hook, const char *name)
210 const sc_p sc = NG_NODE_PRIVATE(node);
212 if (strcmp(name, NG_CISCO_HOOK_DOWNSTREAM) == 0) {
213 sc->downstream.hook = hook;
214 NG_HOOK_SET_PRIVATE(hook, &sc->downstream);
216 /* Start keepalives */
217 ng_callout(&sc->handle, node, NULL, (hz * KEEPALIVE_SECS),
218 &cisco_keepalive, (void *)sc, 0);
219 } else if (strcmp(name, NG_CISCO_HOOK_INET) == 0) {
220 sc->inet.hook = hook;
221 NG_HOOK_SET_PRIVATE(hook, &sc->inet);
222 } else if (strcmp(name, NG_CISCO_HOOK_INET6) == 0) {
223 sc->inet6.hook = hook;
224 NG_HOOK_SET_PRIVATE(hook, &sc->inet6);
225 } else if (strcmp(name, NG_CISCO_HOOK_APPLETALK) == 0) {
226 sc->atalk.hook = hook;
227 NG_HOOK_SET_PRIVATE(hook, &sc->atalk);
228 } else if (strcmp(name, NG_CISCO_HOOK_IPX) == 0) {
230 NG_HOOK_SET_PRIVATE(hook, &sc->ipx);
231 } else if (strcmp(name, NG_CISCO_HOOK_DEBUG) == 0) {
232 NG_HOOK_SET_PRIVATE(hook, NULL); /* unimplemented */
239 * Receive control message.
242 cisco_rcvmsg(node_p node, item_p item, hook_p lasthook)
245 const sc_p sc = NG_NODE_PRIVATE(node);
246 struct ng_mesg *resp = NULL;
249 NGI_GET_MSG(item, msg);
250 switch (msg->header.typecookie) {
251 case NGM_GENERIC_COOKIE:
252 switch (msg->header.cmd) {
253 case NGM_TEXT_STATUS:
258 NG_MKRESPONSE(resp, msg, NG_TEXTRESPONSE, M_NOWAIT);
263 arg = (char *) resp->data;
265 "keepalive period: %d sec; ", KEEPALIVE_SECS);
266 pos += sprintf(arg + pos,
267 "unacknowledged keepalives: %d", sc->seqRetries);
268 resp->header.arglen = pos + 1;
276 case NGM_CISCO_COOKIE:
277 switch (msg->header.cmd) {
278 case NGM_CISCO_GET_IPADDR: /* could be a late reply! */
279 if ((msg->header.flags & NGF_RESP) == 0) {
282 NG_MKRESPONSE(resp, msg,
283 2 * sizeof(*ips), M_NOWAIT);
288 ips = (struct in_addr *) resp->data;
289 ips[0] = sc->localip;
290 ips[1] = sc->localmask;
293 /* FALLTHROUGH */ /* ...if it's a reply */
294 case NGM_CISCO_SET_IPADDR:
296 struct in_addr *const ips = (struct in_addr *)msg->data;
298 if (msg->header.arglen < 2 * sizeof(*ips)) {
302 sc->localip = ips[0];
303 sc->localmask = ips[1];
306 case NGM_CISCO_GET_STATUS:
308 struct ng_cisco_stats *stat;
310 NG_MKRESPONSE(resp, msg, sizeof(*stat), M_NOWAIT);
315 stat = (struct ng_cisco_stats *)resp->data;
316 stat->seqRetries = sc->seqRetries;
317 stat->keepAlivePeriod = KEEPALIVE_SECS;
329 NG_RESPOND_MSG(error, node, item, resp);
338 cisco_rcvdata(hook_p hook, item_p item)
340 const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
341 struct protoent *pep;
342 struct cisco_header *h;
346 if ((pep = NG_HOOK_PRIVATE(hook)) == NULL)
349 /* If it came from our downlink, deal with it separately */
350 if (pep->af == 0xffff)
351 return (cisco_input(sc, item));
353 /* OK so it came from a protocol, heading out. Prepend general data
354 packet header. For now, IP,IPX only */
356 M_PREPEND(m, CISCO_HEADER_LEN, M_NOWAIT);
362 h = mtod(m, struct cisco_header *);
363 h->address = CISCO_UNICAST;
367 case AF_INET: /* Internet Protocol */
368 h->protocol = htons(ETHERTYPE_IP);
371 h->protocol = htons(ETHERTYPE_IPV6);
373 case AF_APPLETALK: /* AppleTalk Protocol */
374 h->protocol = htons(ETHERTYPE_AT);
376 case AF_IPX: /* Novell IPX Protocol */
377 h->protocol = htons(ETHERTYPE_IPX);
380 error = EAFNOSUPPORT;
385 NG_FWD_NEW_DATA(error, item, sc->downstream.hook, m);
397 cisco_shutdown(node_p node)
399 const sc_p sc = NG_NODE_PRIVATE(node);
401 NG_NODE_SET_PRIVATE(node, NULL);
402 NG_NODE_UNREF(sc->node);
403 free(sc, M_NETGRAPH);
408 * Disconnection of a hook
410 * For this type, removal of the last link destroys the node
413 cisco_disconnect(hook_p hook)
415 const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
416 struct protoent *pep;
418 /* Check it's not the debug hook */
419 if ((pep = NG_HOOK_PRIVATE(hook))) {
421 if (pep->af == 0xffff)
422 /* If it is the downstream hook, stop the timers */
423 ng_uncallout(&sc->handle, NG_HOOK_NODE(hook));
426 /* If no more hooks, remove the node */
427 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
428 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
429 ng_rmnode_self(NG_HOOK_NODE(hook));
437 cisco_input(sc_p sc, item_p item)
439 const struct cisco_header *h;
440 struct cisco_header hdrbuf;
441 struct protoent *pep;
448 /* Sanity check header length */
449 if (m->m_pkthdr.len < sizeof(*h)) {
454 /* Get cisco header */
455 if (m->m_len >= sizeof(*h)) /* the common case */
456 h = mtod(m, const struct cisco_header *);
458 m_copydata(m, 0, sizeof(*h), (caddr_t)&hdrbuf);
461 m_adj(m, sizeof(*h));
463 /* Check header address */
464 switch (h->address) {
465 default: /* Invalid Cisco packet. */
468 case CISCO_MULTICAST:
469 /* Don't check the control field here (RFC 1547). */
470 switch (ntohs(h->protocol)) {
473 case CISCO_KEEPALIVE:
475 const struct cisco_packet *p;
476 struct cisco_packet pktbuf;
478 /* Sanity check packet length */
479 if (m->m_pkthdr.len < sizeof(*p)) {
484 /* Get cisco packet */
485 if (m->m_len >= sizeof(*p)) /* the common case */
486 p = mtod(m, const struct cisco_packet *);
488 m_copydata(m, 0, sizeof(*p), (caddr_t)&pktbuf);
492 /* Check packet type */
493 switch (ntohl(p->type)) {
496 "cisco: unknown cisco packet type: 0x%lx\n",
497 (long)ntohl(p->type));
499 case CISCO_ADDR_REPLY:
500 /* Reply on address request, ignore */
502 case CISCO_KEEPALIVE_REQ:
503 sc->remote_seq = ntohl(p->par1);
504 if (sc->local_seq == ntohl(p->par2)) {
506 if (sc->seqRetries > 1)
507 cisco_notify(sc, NGM_LINK_IS_UP);
516 /* Ask inet peer for IP address information */
517 if (sc->inet.hook == NULL)
519 NG_MKMESSAGE(msg, NGM_CISCO_COOKIE,
520 NGM_CISCO_GET_IPADDR, 0, M_NOWAIT);
523 NG_SEND_MSG_HOOK(dummy_error,
524 sc->node, msg, sc->inet.hook, 0);
526 * XXX Now maybe we should set a flag telling
527 * our receiver to send this message when the response comes in
528 * instead of now when the data may be bad.
531 /* Send reply to peer device */
532 error = cisco_send(sc, CISCO_ADDR_REPLY,
533 ntohl(sc->localip.s_addr),
534 ntohl(sc->localmask.s_addr));
556 /* Drop if payload is empty */
557 if (m->m_pkthdr.len == 0) {
563 if (pep->hook == NULL)
565 NG_FWD_NEW_DATA(error, item, pep->hook, m);
574 * Send keepalive packets, every 10 seconds.
577 cisco_keepalive(node_p node, hook_p hook, void *arg1, int arg2)
579 const sc_p sc = arg1;
581 cisco_send(sc, CISCO_KEEPALIVE_REQ, sc->local_seq, sc->remote_seq);
582 if (sc->seqRetries++ > 1)
583 cisco_notify(sc, NGM_LINK_IS_DOWN);
584 ng_callout(&sc->handle, node, NULL, (hz * KEEPALIVE_SECS),
585 &cisco_keepalive, (void *)sc, 0);
589 * Send Cisco keepalive packet.
592 cisco_send(sc_p sc, int type, long par1, long par2)
594 struct cisco_header *h;
595 struct cisco_packet *ch;
601 getmicrouptime(&time);
603 MGETHDR(m, M_NOWAIT, MT_DATA);
607 t = time.tv_sec * 1000 + time.tv_usec / 1000;
608 m->m_pkthdr.len = m->m_len = CISCO_HEADER_LEN + CISCO_PACKET_LEN;
609 m->m_pkthdr.rcvif = 0;
611 h = mtod(m, struct cisco_header *);
612 h->address = CISCO_MULTICAST;
614 h->protocol = htons(CISCO_KEEPALIVE);
616 ch = (struct cisco_packet *) (h + 1);
617 ch->type = htonl(type);
618 ch->par1 = htonl(par1);
619 ch->par2 = htonl(par2);
621 ch->time0 = htons((uint16_t) (t >> 16));
622 ch->time1 = htons((uint16_t) t);
624 NG_SEND_DATA_ONLY(error, sc->downstream.hook, m);
629 * Send linkstate to upstream node.
632 cisco_notify(sc_p sc, uint32_t cmd)
637 if (sc->inet.hook == NULL) /* nothing to notify */
640 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
642 NG_SEND_MSG_HOOK(dummy_error, sc->node, msg, sc->inet.hook, 0);