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>
40 * $FreeBSD: src/sys/netgraph/ng_cisco.c,v 1.29 2007/11/30 23:27:39 julian 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 <netproto/ipx/ipx.h>
59 #include <netproto/ipx/ipx_if.h>
61 #include <netgraph7/ng_message.h>
62 #include <netgraph7/netgraph.h>
63 #include <netgraph7/ng_parse.h>
66 #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
67 #define CISCO_UNICAST 0x0f /* Cisco unicast address */
68 #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
69 #define CISCO_ADDR_REQ 0 /* Cisco address request */
70 #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
71 #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
73 #define KEEPALIVE_SECS 10
81 #define CISCO_HEADER_LEN sizeof (struct cisco_header)
92 #define CISCO_PACKET_LEN (sizeof(struct cisco_packet))
95 hook_p hook; /* the hook for this proto */
96 u_short af; /* address family, -1 = downstream */
102 u_long seqRetries; /* how many times we've been here throwing out
103 * the same sequence number without ack */
105 struct callout handle;
106 struct protoent downstream;
107 struct protoent inet; /* IP information */
108 struct in_addr localip;
109 struct in_addr localmask;
110 struct protoent inet6; /* IPv6 information */
111 struct protoent ipx; /* IPX information */
113 typedef struct cisco_priv *sc_p;
115 /* Netgraph methods */
116 static ng_constructor_t cisco_constructor;
117 static ng_rcvmsg_t cisco_rcvmsg;
118 static ng_shutdown_t cisco_shutdown;
119 static ng_newhook_t cisco_newhook;
120 static ng_rcvdata_t cisco_rcvdata;
121 static ng_disconnect_t cisco_disconnect;
123 /* Other functions */
124 static int cisco_input(sc_p sc, item_p item);
125 static void cisco_keepalive(node_p node, hook_p hook, void *arg1, int arg2);
126 static int cisco_send(sc_p sc, int type, long par1, long par2);
127 static void cisco_notify(sc_p sc, uint32_t cmd);
129 /* Parse type for struct ng_cisco_ipaddr */
130 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
131 = NG_CISCO_IPADDR_TYPE_INFO;
132 static const struct ng_parse_type ng_cisco_ipaddr_type = {
133 &ng_parse_struct_type,
134 &ng_cisco_ipaddr_type_fields
137 /* Parse type for struct ng_async_stat */
138 static const struct ng_parse_struct_field ng_cisco_stats_type_fields[]
139 = NG_CISCO_STATS_TYPE_INFO;
140 static const struct ng_parse_type ng_cisco_stats_type = {
141 &ng_parse_struct_type,
142 &ng_cisco_stats_type_fields
145 /* List of commands and how to convert arguments to/from ASCII */
146 static const struct ng_cmdlist ng_cisco_cmdlist[] = {
149 NGM_CISCO_SET_IPADDR,
151 &ng_cisco_ipaddr_type,
156 NGM_CISCO_GET_IPADDR,
159 &ng_cisco_ipaddr_type
163 NGM_CISCO_GET_STATUS,
172 static struct ng_type typestruct = {
173 .version = NG_ABI_VERSION,
174 .name = NG_CISCO_NODE_TYPE,
175 .constructor = cisco_constructor,
176 .rcvmsg = cisco_rcvmsg,
177 .shutdown = cisco_shutdown,
178 .newhook = cisco_newhook,
179 .rcvdata = cisco_rcvdata,
180 .disconnect = cisco_disconnect,
181 .cmdlist = ng_cisco_cmdlist,
183 NETGRAPH_INIT(cisco, &typestruct);
189 cisco_constructor(node_p node)
193 sc = kmalloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_NULLOK | M_ZERO);
197 ng_callout_init(&sc->handle);
198 NG_NODE_SET_PRIVATE(node, sc);
201 /* Initialise the varous protocol hook holders */
202 sc->downstream.af = 0xffff;
203 sc->inet.af = AF_INET;
204 sc->inet6.af = AF_INET6;
213 cisco_newhook(node_p node, hook_p hook, const char *name)
215 const sc_p sc = NG_NODE_PRIVATE(node);
217 if (strcmp(name, NG_CISCO_HOOK_DOWNSTREAM) == 0) {
218 sc->downstream.hook = hook;
219 NG_HOOK_SET_PRIVATE(hook, &sc->downstream);
221 /* Start keepalives */
222 ng_callout(&sc->handle, node, NULL, (hz * KEEPALIVE_SECS),
223 &cisco_keepalive, (void *)sc, 0);
224 } else if (strcmp(name, NG_CISCO_HOOK_INET) == 0) {
225 sc->inet.hook = hook;
226 NG_HOOK_SET_PRIVATE(hook, &sc->inet);
227 } else if (strcmp(name, NG_CISCO_HOOK_INET6) == 0) {
228 sc->inet6.hook = hook;
229 NG_HOOK_SET_PRIVATE(hook, &sc->inet6);
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_WAITOK | M_NULLOK);
265 arg = (char *) resp->data;
267 "keepalive period: %d sec; ", KEEPALIVE_SECS);
268 pos += sprintf(arg + pos,
269 "unacknowledged keepalives: %ld", 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_WAITOK | M_NULLOK);
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_WAITOK | M_NULLOK);
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 */
357 m = NGI_M(item); /* still associated with item */
358 M_PREPEND(m, CISCO_HEADER_LEN, MB_DONTWAIT);
363 h = mtod(m, struct cisco_header *);
364 h->address = CISCO_UNICAST;
368 case AF_INET: /* Internet Protocol */
369 h->protocol = htons(ETHERTYPE_IP);
372 h->protocol = htons(ETHERTYPE_IPV6);
374 case AF_IPX: /* Novell IPX Protocol */
375 h->protocol = htons(ETHERTYPE_IPX);
378 error = EAFNOSUPPORT;
383 NG_FWD_NEW_DATA(error, item, sc->downstream.hook, m);
395 cisco_shutdown(node_p node)
397 const sc_p sc = NG_NODE_PRIVATE(node);
399 NG_NODE_SET_PRIVATE(node, NULL);
400 NG_NODE_UNREF(sc->node);
401 kfree(sc, M_NETGRAPH);
406 * Disconnection of a hook
408 * For this type, removal of the last link destroys the node
411 cisco_disconnect(hook_p hook)
413 const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
414 struct protoent *pep;
416 /* Check it's not the debug hook */
417 if ((pep = NG_HOOK_PRIVATE(hook))) {
419 if (pep->af == 0xffff)
420 /* If it is the downstream hook, stop the timers */
421 ng_uncallout(&sc->handle, NG_HOOK_NODE(hook));
424 /* If no more hooks, remove the node */
425 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
426 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
427 ng_rmnode_self(NG_HOOK_NODE(hook));
435 cisco_input(sc_p sc, item_p item)
437 const struct cisco_header *h;
438 struct cisco_header hdrbuf;
439 struct protoent *pep;
446 /* Sanity check header length */
447 if (m->m_pkthdr.len < sizeof(*h)) {
452 /* Get cisco header */
453 if (m->m_len >= sizeof(*h)) /* the common case */
454 h = mtod(m, const struct cisco_header *);
456 m_copydata(m, 0, sizeof(*h), (caddr_t)&hdrbuf);
459 m_adj(m, sizeof(*h));
461 /* Check header address */
462 switch (h->address) {
463 default: /* Invalid Cisco packet. */
466 case CISCO_MULTICAST:
467 /* Don't check the control field here (RFC 1547). */
468 switch (ntohs(h->protocol)) {
471 case CISCO_KEEPALIVE:
473 const struct cisco_packet *p;
474 struct cisco_packet pktbuf;
476 /* Sanity check packet length */
477 if (m->m_pkthdr.len < sizeof(*p)) {
482 /* Get cisco packet */
483 if (m->m_len >= sizeof(*p)) /* the common case */
484 p = mtod(m, const struct cisco_packet *);
486 m_copydata(m, 0, sizeof(*p), (caddr_t)&pktbuf);
490 /* Check packet type */
491 switch (ntohl(p->type)) {
494 "cisco: unknown cisco packet type: 0x%lx\n",
495 (long)ntohl(p->type));
497 case CISCO_ADDR_REPLY:
498 /* Reply on address request, ignore */
500 case CISCO_KEEPALIVE_REQ:
501 sc->remote_seq = ntohl(p->par1);
502 if (sc->local_seq == ntohl(p->par2)) {
504 if (sc->seqRetries > 1)
505 cisco_notify(sc, NGM_LINK_IS_UP);
514 /* Ask inet peer for IP address information */
515 if (sc->inet.hook == NULL)
517 NG_MKMESSAGE(msg, NGM_CISCO_COOKIE,
518 NGM_CISCO_GET_IPADDR, 0, M_WAITOK | M_NULLOK);
521 NG_SEND_MSG_HOOK(dummy_error,
522 sc->node, msg, sc->inet.hook, 0);
524 * XXX Now maybe we should set a flag telling
525 * our receiver to send this message when the response comes in
526 * instead of now when the data may be bad.
529 /* Send reply to peer device */
530 error = cisco_send(sc, CISCO_ADDR_REPLY,
531 ntohl(sc->localip.s_addr),
532 ntohl(sc->localmask.s_addr));
551 /* Drop if payload is empty */
552 if (m->m_pkthdr.len == 0) {
558 if (pep->hook == NULL)
560 NG_FWD_NEW_DATA(error, item, pep->hook, m);
570 * Send keepalive packets, every 10 seconds.
573 cisco_keepalive(node_p node, hook_p hook, void *arg1, int arg2)
575 const sc_p sc = arg1;
577 cisco_send(sc, CISCO_KEEPALIVE_REQ, sc->local_seq, sc->remote_seq);
578 if (sc->seqRetries++ > 1)
579 cisco_notify(sc, NGM_LINK_IS_DOWN);
580 ng_callout(&sc->handle, node, NULL, (hz * KEEPALIVE_SECS),
581 &cisco_keepalive, (void *)sc, 0);
585 * Send Cisco keepalive packet.
588 cisco_send(sc_p sc, int type, long par1, long par2)
590 struct cisco_header *h;
591 struct cisco_packet *ch;
597 getmicrouptime(&time);
599 MGETHDR(m, MB_DONTWAIT, MT_DATA);
603 t = time.tv_sec * 1000 + time.tv_usec / 1000;
604 m->m_pkthdr.len = m->m_len = CISCO_HEADER_LEN + CISCO_PACKET_LEN;
605 m->m_pkthdr.rcvif = 0;
607 h = mtod(m, struct cisco_header *);
608 h->address = CISCO_MULTICAST;
610 h->protocol = htons(CISCO_KEEPALIVE);
612 ch = (struct cisco_packet *) (h + 1);
613 ch->type = htonl(type);
614 ch->par1 = htonl(par1);
615 ch->par2 = htonl(par2);
617 ch->time0 = htons((u_short) (t >> 16));
618 ch->time1 = htons((u_short) t);
620 NG_SEND_DATA_ONLY(error, sc->downstream.hook, m);
625 * Send linkstate to upstream node.
628 cisco_notify(sc_p sc, uint32_t cmd)
633 if (sc->inet.hook == NULL) /* nothing to notify */
636 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_WAITOK | M_NULLOK);
638 NG_SEND_MSG_HOOK(dummy_error, sc->node, msg, sc->inet.hook, 0);