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 */
362 m = NGI_M(item); /* still associated with item */
363 M_PREPEND(m, CISCO_HEADER_LEN, M_DONTWAIT);
368 h = mtod(m, struct cisco_header *);
369 h->address = CISCO_UNICAST;
373 case AF_INET: /* Internet Protocol */
374 h->protocol = htons(ETHERTYPE_IP);
377 h->protocol = htons(ETHERTYPE_IPV6);
379 case AF_APPLETALK: /* AppleTalk Protocol */
380 h->protocol = htons(ETHERTYPE_AT);
382 case AF_IPX: /* Novell IPX Protocol */
383 h->protocol = htons(ETHERTYPE_IPX);
386 error = EAFNOSUPPORT;
391 NG_FWD_NEW_DATA(error, item, sc->downstream.hook, m);
403 cisco_shutdown(node_p node)
405 const sc_p sc = NG_NODE_PRIVATE(node);
407 NG_NODE_SET_PRIVATE(node, NULL);
408 NG_NODE_UNREF(sc->node);
409 free(sc, M_NETGRAPH);
414 * Disconnection of a hook
416 * For this type, removal of the last link destroys the node
419 cisco_disconnect(hook_p hook)
421 const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
422 struct protoent *pep;
424 /* Check it's not the debug hook */
425 if ((pep = NG_HOOK_PRIVATE(hook))) {
427 if (pep->af == 0xffff)
428 /* If it is the downstream hook, stop the timers */
429 ng_uncallout(&sc->handle, NG_HOOK_NODE(hook));
432 /* If no more hooks, remove the node */
433 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
434 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
435 ng_rmnode_self(NG_HOOK_NODE(hook));
443 cisco_input(sc_p sc, item_p item)
445 const struct cisco_header *h;
446 struct cisco_header hdrbuf;
447 struct protoent *pep;
454 /* Sanity check header length */
455 if (m->m_pkthdr.len < sizeof(*h)) {
460 /* Get cisco header */
461 if (m->m_len >= sizeof(*h)) /* the common case */
462 h = mtod(m, const struct cisco_header *);
464 m_copydata(m, 0, sizeof(*h), (caddr_t)&hdrbuf);
467 m_adj(m, sizeof(*h));
469 /* Check header address */
470 switch (h->address) {
471 default: /* Invalid Cisco packet. */
474 case CISCO_MULTICAST:
475 /* Don't check the control field here (RFC 1547). */
476 switch (ntohs(h->protocol)) {
479 case CISCO_KEEPALIVE:
481 const struct cisco_packet *p;
482 struct cisco_packet pktbuf;
484 /* Sanity check packet length */
485 if (m->m_pkthdr.len < sizeof(*p)) {
490 /* Get cisco packet */
491 if (m->m_len >= sizeof(*p)) /* the common case */
492 p = mtod(m, const struct cisco_packet *);
494 m_copydata(m, 0, sizeof(*p), (caddr_t)&pktbuf);
498 /* Check packet type */
499 switch (ntohl(p->type)) {
502 "cisco: unknown cisco packet type: 0x%lx\n",
503 (long)ntohl(p->type));
505 case CISCO_ADDR_REPLY:
506 /* Reply on address request, ignore */
508 case CISCO_KEEPALIVE_REQ:
509 sc->remote_seq = ntohl(p->par1);
510 if (sc->local_seq == ntohl(p->par2)) {
512 if (sc->seqRetries > 1)
513 cisco_notify(sc, NGM_LINK_IS_UP);
522 /* Ask inet peer for IP address information */
523 if (sc->inet.hook == NULL)
525 NG_MKMESSAGE(msg, NGM_CISCO_COOKIE,
526 NGM_CISCO_GET_IPADDR, 0, M_NOWAIT);
529 NG_SEND_MSG_HOOK(dummy_error,
530 sc->node, msg, sc->inet.hook, 0);
532 * XXX Now maybe we should set a flag telling
533 * our receiver to send this message when the response comes in
534 * instead of now when the data may be bad.
537 /* Send reply to peer device */
538 error = cisco_send(sc, CISCO_ADDR_REPLY,
539 ntohl(sc->localip.s_addr),
540 ntohl(sc->localmask.s_addr));
562 /* Drop if payload is empty */
563 if (m->m_pkthdr.len == 0) {
569 if (pep->hook == NULL)
571 NG_FWD_NEW_DATA(error, item, pep->hook, m);
581 * Send keepalive packets, every 10 seconds.
584 cisco_keepalive(node_p node, hook_p hook, void *arg1, int arg2)
586 const sc_p sc = arg1;
588 cisco_send(sc, CISCO_KEEPALIVE_REQ, sc->local_seq, sc->remote_seq);
589 if (sc->seqRetries++ > 1)
590 cisco_notify(sc, NGM_LINK_IS_DOWN);
591 ng_callout(&sc->handle, node, NULL, (hz * KEEPALIVE_SECS),
592 &cisco_keepalive, (void *)sc, 0);
596 * Send Cisco keepalive packet.
599 cisco_send(sc_p sc, int type, long par1, long par2)
601 struct cisco_header *h;
602 struct cisco_packet *ch;
608 getmicrouptime(&time);
610 MGETHDR(m, M_DONTWAIT, MT_DATA);
614 t = time.tv_sec * 1000 + time.tv_usec / 1000;
615 m->m_pkthdr.len = m->m_len = CISCO_HEADER_LEN + CISCO_PACKET_LEN;
616 m->m_pkthdr.rcvif = 0;
618 h = mtod(m, struct cisco_header *);
619 h->address = CISCO_MULTICAST;
621 h->protocol = htons(CISCO_KEEPALIVE);
623 ch = (struct cisco_packet *) (h + 1);
624 ch->type = htonl(type);
625 ch->par1 = htonl(par1);
626 ch->par2 = htonl(par2);
628 ch->time0 = htons((uint16_t) (t >> 16));
629 ch->time1 = htons((uint16_t) t);
631 NG_SEND_DATA_ONLY(error, sc->downstream.hook, m);
636 * Send linkstate to upstream node.
639 cisco_notify(sc_p sc, uint32_t cmd)
644 if (sc->inet.hook == NULL) /* nothing to notify */
647 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
649 NG_SEND_MSG_HOOK(dummy_error, sc->node, msg, sc->inet.hook, 0);