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 <netgraph/ng_message.h>
59 #include <netgraph/netgraph.h>
60 #include <netgraph/ng_parse.h>
61 #include <netgraph/ng_cisco.h>
63 #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
64 #define CISCO_UNICAST 0x0f /* Cisco unicast address */
65 #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
66 #define CISCO_ADDR_REQ 0 /* Cisco address request */
67 #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
68 #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
70 #define KEEPALIVE_SECS 10
78 #define CISCO_HEADER_LEN sizeof (struct cisco_header)
89 #define CISCO_PACKET_LEN (sizeof(struct cisco_packet))
92 hook_p hook; /* the hook for this proto */
93 uint16_t af; /* address family, -1 = downstream */
99 uint32_t seqRetries; /* how many times we've been here throwing out
100 * the same sequence number without ack */
102 struct callout handle;
103 struct protoent downstream;
104 struct protoent inet; /* IP information */
105 struct in_addr localip;
106 struct in_addr localmask;
107 struct protoent inet6; /* IPv6 information */
108 struct protoent atalk; /* AppleTalk information */
109 struct protoent ipx; /* IPX information */
111 typedef struct cisco_priv *sc_p;
113 /* Netgraph methods */
114 static ng_constructor_t cisco_constructor;
115 static ng_rcvmsg_t cisco_rcvmsg;
116 static ng_shutdown_t cisco_shutdown;
117 static ng_newhook_t cisco_newhook;
118 static ng_rcvdata_t cisco_rcvdata;
119 static ng_disconnect_t cisco_disconnect;
121 /* Other functions */
122 static int cisco_input(sc_p sc, item_p item);
123 static void cisco_keepalive(node_p node, hook_p hook, void *arg1, int arg2);
124 static int cisco_send(sc_p sc, int type, long par1, long par2);
125 static void cisco_notify(sc_p sc, uint32_t cmd);
127 /* Parse type for struct ng_cisco_ipaddr */
128 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
129 = NG_CISCO_IPADDR_TYPE_INFO;
130 static const struct ng_parse_type ng_cisco_ipaddr_type = {
131 &ng_parse_struct_type,
132 &ng_cisco_ipaddr_type_fields
135 /* Parse type for struct ng_async_stat */
136 static const struct ng_parse_struct_field ng_cisco_stats_type_fields[]
137 = NG_CISCO_STATS_TYPE_INFO;
138 static const struct ng_parse_type ng_cisco_stats_type = {
139 &ng_parse_struct_type,
140 &ng_cisco_stats_type_fields
143 /* List of commands and how to convert arguments to/from ASCII */
144 static const struct ng_cmdlist ng_cisco_cmdlist[] = {
147 NGM_CISCO_SET_IPADDR,
149 &ng_cisco_ipaddr_type,
154 NGM_CISCO_GET_IPADDR,
157 &ng_cisco_ipaddr_type
161 NGM_CISCO_GET_STATUS,
170 static struct ng_type typestruct = {
171 .version = NG_ABI_VERSION,
172 .name = NG_CISCO_NODE_TYPE,
173 .constructor = cisco_constructor,
174 .rcvmsg = cisco_rcvmsg,
175 .shutdown = cisco_shutdown,
176 .newhook = cisco_newhook,
177 .rcvdata = cisco_rcvdata,
178 .disconnect = cisco_disconnect,
179 .cmdlist = ng_cisco_cmdlist,
181 NETGRAPH_INIT(cisco, &typestruct);
187 cisco_constructor(node_p node)
191 sc = malloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
193 ng_callout_init(&sc->handle);
194 NG_NODE_SET_PRIVATE(node, sc);
197 /* Initialise the varous protocol hook holders */
198 sc->downstream.af = 0xffff;
199 sc->inet.af = AF_INET;
200 sc->inet6.af = AF_INET6;
201 sc->atalk.af = AF_APPLETALK;
210 cisco_newhook(node_p node, hook_p hook, const char *name)
212 const sc_p sc = NG_NODE_PRIVATE(node);
214 if (strcmp(name, NG_CISCO_HOOK_DOWNSTREAM) == 0) {
215 sc->downstream.hook = hook;
216 NG_HOOK_SET_PRIVATE(hook, &sc->downstream);
218 /* Start keepalives */
219 ng_callout(&sc->handle, node, NULL, (hz * KEEPALIVE_SECS),
220 &cisco_keepalive, (void *)sc, 0);
221 } else if (strcmp(name, NG_CISCO_HOOK_INET) == 0) {
222 sc->inet.hook = hook;
223 NG_HOOK_SET_PRIVATE(hook, &sc->inet);
224 } else if (strcmp(name, NG_CISCO_HOOK_INET6) == 0) {
225 sc->inet6.hook = hook;
226 NG_HOOK_SET_PRIVATE(hook, &sc->inet6);
227 } else if (strcmp(name, NG_CISCO_HOOK_APPLETALK) == 0) {
228 sc->atalk.hook = hook;
229 NG_HOOK_SET_PRIVATE(hook, &sc->atalk);
230 } else if (strcmp(name, NG_CISCO_HOOK_IPX) == 0) {
232 NG_HOOK_SET_PRIVATE(hook, &sc->ipx);
233 } else if (strcmp(name, NG_CISCO_HOOK_DEBUG) == 0) {
234 NG_HOOK_SET_PRIVATE(hook, NULL); /* unimplemented */
241 * Receive control message.
244 cisco_rcvmsg(node_p node, item_p item, hook_p lasthook)
247 const sc_p sc = NG_NODE_PRIVATE(node);
248 struct ng_mesg *resp = NULL;
251 NGI_GET_MSG(item, msg);
252 switch (msg->header.typecookie) {
253 case NGM_GENERIC_COOKIE:
254 switch (msg->header.cmd) {
255 case NGM_TEXT_STATUS:
260 NG_MKRESPONSE(resp, msg, NG_TEXTRESPONSE, M_NOWAIT);
265 arg = (char *) resp->data;
267 "keepalive period: %d sec; ", KEEPALIVE_SECS);
268 pos += sprintf(arg + pos,
269 "unacknowledged keepalives: %d", sc->seqRetries);
270 resp->header.arglen = pos + 1;
278 case NGM_CISCO_COOKIE:
279 switch (msg->header.cmd) {
280 case NGM_CISCO_GET_IPADDR: /* could be a late reply! */
281 if ((msg->header.flags & NGF_RESP) == 0) {
284 NG_MKRESPONSE(resp, msg,
285 2 * sizeof(*ips), M_NOWAIT);
290 ips = (struct in_addr *) resp->data;
291 ips[0] = sc->localip;
292 ips[1] = sc->localmask;
295 /* FALLTHROUGH */ /* ...if it's a reply */
296 case NGM_CISCO_SET_IPADDR:
298 struct in_addr *const ips = (struct in_addr *)msg->data;
300 if (msg->header.arglen < 2 * sizeof(*ips)) {
304 sc->localip = ips[0];
305 sc->localmask = ips[1];
308 case NGM_CISCO_GET_STATUS:
310 struct ng_cisco_stats *stat;
312 NG_MKRESPONSE(resp, msg, sizeof(*stat), M_NOWAIT);
317 stat = (struct ng_cisco_stats *)resp->data;
318 stat->seqRetries = sc->seqRetries;
319 stat->keepAlivePeriod = KEEPALIVE_SECS;
331 NG_RESPOND_MSG(error, node, item, resp);
340 cisco_rcvdata(hook_p hook, item_p item)
342 const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
343 struct protoent *pep;
344 struct cisco_header *h;
348 if ((pep = NG_HOOK_PRIVATE(hook)) == NULL)
351 /* If it came from our downlink, deal with it separately */
352 if (pep->af == 0xffff)
353 return (cisco_input(sc, item));
355 /* OK so it came from a protocol, heading out. Prepend general data
356 packet header. For now, IP,IPX only */
358 M_PREPEND(m, CISCO_HEADER_LEN, M_NOWAIT);
364 h = mtod(m, struct cisco_header *);
365 h->address = CISCO_UNICAST;
369 case AF_INET: /* Internet Protocol */
370 h->protocol = htons(ETHERTYPE_IP);
373 h->protocol = htons(ETHERTYPE_IPV6);
375 case AF_APPLETALK: /* AppleTalk Protocol */
376 h->protocol = htons(ETHERTYPE_AT);
378 case AF_IPX: /* Novell IPX Protocol */
379 h->protocol = htons(ETHERTYPE_IPX);
382 error = EAFNOSUPPORT;
387 NG_FWD_NEW_DATA(error, item, sc->downstream.hook, m);
399 cisco_shutdown(node_p node)
401 const sc_p sc = NG_NODE_PRIVATE(node);
403 NG_NODE_SET_PRIVATE(node, NULL);
404 NG_NODE_UNREF(sc->node);
405 free(sc, M_NETGRAPH);
410 * Disconnection of a hook
412 * For this type, removal of the last link destroys the node
415 cisco_disconnect(hook_p hook)
417 const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
418 struct protoent *pep;
420 /* Check it's not the debug hook */
421 if ((pep = NG_HOOK_PRIVATE(hook))) {
423 if (pep->af == 0xffff)
424 /* If it is the downstream hook, stop the timers */
425 ng_uncallout(&sc->handle, NG_HOOK_NODE(hook));
428 /* If no more hooks, remove the node */
429 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
430 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
431 ng_rmnode_self(NG_HOOK_NODE(hook));
439 cisco_input(sc_p sc, item_p item)
441 const struct cisco_header *h;
442 struct cisco_header hdrbuf;
443 struct protoent *pep;
450 /* Sanity check header length */
451 if (m->m_pkthdr.len < sizeof(*h)) {
456 /* Get cisco header */
457 if (m->m_len >= sizeof(*h)) /* the common case */
458 h = mtod(m, const struct cisco_header *);
460 m_copydata(m, 0, sizeof(*h), (caddr_t)&hdrbuf);
463 m_adj(m, sizeof(*h));
465 /* Check header address */
466 switch (h->address) {
467 default: /* Invalid Cisco packet. */
470 case CISCO_MULTICAST:
471 /* Don't check the control field here (RFC 1547). */
472 switch (ntohs(h->protocol)) {
475 case CISCO_KEEPALIVE:
477 const struct cisco_packet *p;
478 struct cisco_packet pktbuf;
480 /* Sanity check packet length */
481 if (m->m_pkthdr.len < sizeof(*p)) {
486 /* Get cisco packet */
487 if (m->m_len >= sizeof(*p)) /* the common case */
488 p = mtod(m, const struct cisco_packet *);
490 m_copydata(m, 0, sizeof(*p), (caddr_t)&pktbuf);
494 /* Check packet type */
495 switch (ntohl(p->type)) {
498 "cisco: unknown cisco packet type: 0x%lx\n",
499 (long)ntohl(p->type));
501 case CISCO_ADDR_REPLY:
502 /* Reply on address request, ignore */
504 case CISCO_KEEPALIVE_REQ:
505 sc->remote_seq = ntohl(p->par1);
506 if (sc->local_seq == ntohl(p->par2)) {
508 if (sc->seqRetries > 1)
509 cisco_notify(sc, NGM_LINK_IS_UP);
518 /* Ask inet peer for IP address information */
519 if (sc->inet.hook == NULL)
521 NG_MKMESSAGE(msg, NGM_CISCO_COOKIE,
522 NGM_CISCO_GET_IPADDR, 0, M_NOWAIT);
525 NG_SEND_MSG_HOOK(dummy_error,
526 sc->node, msg, sc->inet.hook, 0);
528 * XXX Now maybe we should set a flag telling
529 * our receiver to send this message when the response comes in
530 * instead of now when the data may be bad.
533 /* Send reply to peer device */
534 error = cisco_send(sc, CISCO_ADDR_REPLY,
535 ntohl(sc->localip.s_addr),
536 ntohl(sc->localmask.s_addr));
558 /* Drop if payload is empty */
559 if (m->m_pkthdr.len == 0) {
565 if (pep->hook == NULL)
567 NG_FWD_NEW_DATA(error, item, pep->hook, m);
577 * Send keepalive packets, every 10 seconds.
580 cisco_keepalive(node_p node, hook_p hook, void *arg1, int arg2)
582 const sc_p sc = arg1;
584 cisco_send(sc, CISCO_KEEPALIVE_REQ, sc->local_seq, sc->remote_seq);
585 if (sc->seqRetries++ > 1)
586 cisco_notify(sc, NGM_LINK_IS_DOWN);
587 ng_callout(&sc->handle, node, NULL, (hz * KEEPALIVE_SECS),
588 &cisco_keepalive, (void *)sc, 0);
592 * Send Cisco keepalive packet.
595 cisco_send(sc_p sc, int type, long par1, long par2)
597 struct cisco_header *h;
598 struct cisco_packet *ch;
604 getmicrouptime(&time);
606 MGETHDR(m, M_NOWAIT, MT_DATA);
610 t = time.tv_sec * 1000 + time.tv_usec / 1000;
611 m->m_pkthdr.len = m->m_len = CISCO_HEADER_LEN + CISCO_PACKET_LEN;
612 m->m_pkthdr.rcvif = 0;
614 h = mtod(m, struct cisco_header *);
615 h->address = CISCO_MULTICAST;
617 h->protocol = htons(CISCO_KEEPALIVE);
619 ch = (struct cisco_packet *) (h + 1);
620 ch->type = htonl(type);
621 ch->par1 = htonl(par1);
622 ch->par2 = htonl(par2);
624 ch->time0 = htons((uint16_t) (t >> 16));
625 ch->time1 = htons((uint16_t) t);
627 NG_SEND_DATA_ONLY(error, sc->downstream.hook, m);
632 * Send linkstate to upstream node.
635 cisco_notify(sc_p sc, uint32_t cmd)
640 if (sc->inet.hook == NULL) /* nothing to notify */
643 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
645 NG_SEND_MSG_HOOK(dummy_error, sc->node, msg, sc->inet.hook, 0);