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 <netgraph7/ng_message.h>
59 #include <netgraph7/netgraph.h>
60 #include <netgraph7/ng_parse.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 u_short af
; /* address family, -1 = downstream */
99 u_long 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 ipx
; /* IPX information */
110 typedef struct cisco_priv
*sc_p
;
112 /* Netgraph methods */
113 static ng_constructor_t cisco_constructor
;
114 static ng_rcvmsg_t cisco_rcvmsg
;
115 static ng_shutdown_t cisco_shutdown
;
116 static ng_newhook_t cisco_newhook
;
117 static ng_rcvdata_t cisco_rcvdata
;
118 static ng_disconnect_t cisco_disconnect
;
120 /* Other functions */
121 static int cisco_input(sc_p sc
, item_p item
);
122 static void cisco_keepalive(node_p node
, hook_p hook
, void *arg1
, int arg2
);
123 static int cisco_send(sc_p sc
, int type
, long par1
, long par2
);
124 static void cisco_notify(sc_p sc
, uint32_t cmd
);
126 /* Parse type for struct ng_cisco_ipaddr */
127 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields
[]
128 = NG_CISCO_IPADDR_TYPE_INFO
;
129 static const struct ng_parse_type ng_cisco_ipaddr_type
= {
130 &ng_parse_struct_type
,
131 &ng_cisco_ipaddr_type_fields
134 /* Parse type for struct ng_async_stat */
135 static const struct ng_parse_struct_field ng_cisco_stats_type_fields
[]
136 = NG_CISCO_STATS_TYPE_INFO
;
137 static const struct ng_parse_type ng_cisco_stats_type
= {
138 &ng_parse_struct_type
,
139 &ng_cisco_stats_type_fields
142 /* List of commands and how to convert arguments to/from ASCII */
143 static const struct ng_cmdlist ng_cisco_cmdlist
[] = {
146 NGM_CISCO_SET_IPADDR
,
148 &ng_cisco_ipaddr_type
,
153 NGM_CISCO_GET_IPADDR
,
156 &ng_cisco_ipaddr_type
160 NGM_CISCO_GET_STATUS
,
169 static struct ng_type typestruct
= {
170 .version
= NG_ABI_VERSION
,
171 .name
= NG_CISCO_NODE_TYPE
,
172 .constructor
= cisco_constructor
,
173 .rcvmsg
= cisco_rcvmsg
,
174 .shutdown
= cisco_shutdown
,
175 .newhook
= cisco_newhook
,
176 .rcvdata
= cisco_rcvdata
,
177 .disconnect
= cisco_disconnect
,
178 .cmdlist
= ng_cisco_cmdlist
,
180 NETGRAPH_INIT(cisco
, &typestruct
);
186 cisco_constructor(node_p node
)
190 sc
= kmalloc(sizeof(*sc
), M_NETGRAPH
, M_WAITOK
| M_NULLOK
| M_ZERO
);
194 ng_callout_init(&sc
->handle
);
195 NG_NODE_SET_PRIVATE(node
, sc
);
198 /* Initialise the varous protocol hook holders */
199 sc
->downstream
.af
= 0xffff;
200 sc
->inet
.af
= AF_INET
;
201 sc
->inet6
.af
= AF_INET6
;
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_IPX
) == 0) {
229 NG_HOOK_SET_PRIVATE(hook
, &sc
->ipx
);
230 } else if (strcmp(name
, NG_CISCO_HOOK_DEBUG
) == 0) {
231 NG_HOOK_SET_PRIVATE(hook
, NULL
); /* unimplemented */
238 * Receive control message.
241 cisco_rcvmsg(node_p node
, item_p item
, hook_p lasthook
)
244 const sc_p sc
= NG_NODE_PRIVATE(node
);
245 struct ng_mesg
*resp
= NULL
;
248 NGI_GET_MSG(item
, msg
);
249 switch (msg
->header
.typecookie
) {
250 case NGM_GENERIC_COOKIE
:
251 switch (msg
->header
.cmd
) {
252 case NGM_TEXT_STATUS
:
257 NG_MKRESPONSE(resp
, msg
, NG_TEXTRESPONSE
, M_WAITOK
| M_NULLOK
);
262 arg
= (char *) resp
->data
;
264 "keepalive period: %d sec; ", KEEPALIVE_SECS
);
265 pos
+= ksprintf(arg
+ pos
,
266 "unacknowledged keepalives: %ld", sc
->seqRetries
);
267 resp
->header
.arglen
= pos
+ 1;
275 case NGM_CISCO_COOKIE
:
276 switch (msg
->header
.cmd
) {
277 case NGM_CISCO_GET_IPADDR
: /* could be a late reply! */
278 if ((msg
->header
.flags
& NGF_RESP
) == 0) {
281 NG_MKRESPONSE(resp
, msg
,
282 2 * sizeof(*ips
), M_WAITOK
| M_NULLOK
);
287 ips
= (struct in_addr
*) resp
->data
;
288 ips
[0] = sc
->localip
;
289 ips
[1] = sc
->localmask
;
292 /* FALLTHROUGH */ /* ...if it's a reply */
293 case NGM_CISCO_SET_IPADDR
:
295 struct in_addr
*const ips
= (struct in_addr
*)msg
->data
;
297 if (msg
->header
.arglen
< 2 * sizeof(*ips
)) {
301 sc
->localip
= ips
[0];
302 sc
->localmask
= ips
[1];
305 case NGM_CISCO_GET_STATUS
:
307 struct ng_cisco_stats
*stat
;
309 NG_MKRESPONSE(resp
, msg
, sizeof(*stat
), M_WAITOK
| M_NULLOK
);
314 stat
= (struct ng_cisco_stats
*)resp
->data
;
315 stat
->seqRetries
= sc
->seqRetries
;
316 stat
->keepAlivePeriod
= KEEPALIVE_SECS
;
328 NG_RESPOND_MSG(error
, node
, item
, resp
);
337 cisco_rcvdata(hook_p hook
, item_p item
)
339 const sc_p sc
= NG_NODE_PRIVATE(NG_HOOK_NODE(hook
));
340 struct protoent
*pep
;
341 struct cisco_header
*h
;
345 if ((pep
= NG_HOOK_PRIVATE(hook
)) == NULL
)
348 /* If it came from our downlink, deal with it separately */
349 if (pep
->af
== 0xffff)
350 return (cisco_input(sc
, item
));
352 /* OK so it came from a protocol, heading out. Prepend general data
353 packet header. For now, IP,IPX only */
354 m
= NGI_M(item
); /* still associated with item */
355 M_PREPEND(m
, CISCO_HEADER_LEN
, MB_DONTWAIT
);
360 h
= mtod(m
, struct cisco_header
*);
361 h
->address
= CISCO_UNICAST
;
365 case AF_INET
: /* Internet Protocol */
366 h
->protocol
= htons(ETHERTYPE_IP
);
369 h
->protocol
= htons(ETHERTYPE_IPV6
);
371 case AF_IPX
: /* Novell IPX Protocol */
372 h
->protocol
= htons(ETHERTYPE_IPX
);
375 error
= EAFNOSUPPORT
;
380 NG_FWD_NEW_DATA(error
, item
, sc
->downstream
.hook
, m
);
392 cisco_shutdown(node_p node
)
394 const sc_p sc
= NG_NODE_PRIVATE(node
);
396 NG_NODE_SET_PRIVATE(node
, NULL
);
397 NG_NODE_UNREF(sc
->node
);
398 kfree(sc
, M_NETGRAPH
);
403 * Disconnection of a hook
405 * For this type, removal of the last link destroys the node
408 cisco_disconnect(hook_p hook
)
410 const sc_p sc
= NG_NODE_PRIVATE(NG_HOOK_NODE(hook
));
411 struct protoent
*pep
;
413 /* Check it's not the debug hook */
414 if ((pep
= NG_HOOK_PRIVATE(hook
))) {
416 if (pep
->af
== 0xffff)
417 /* If it is the downstream hook, stop the timers */
418 ng_uncallout(&sc
->handle
, NG_HOOK_NODE(hook
));
421 /* If no more hooks, remove the node */
422 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook
)) == 0)
423 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook
))))
424 ng_rmnode_self(NG_HOOK_NODE(hook
));
432 cisco_input(sc_p sc
, item_p item
)
434 const struct cisco_header
*h
;
435 struct cisco_header hdrbuf
;
436 struct protoent
*pep
;
443 /* Sanity check header length */
444 if (m
->m_pkthdr
.len
< sizeof(*h
)) {
449 /* Get cisco header */
450 if (m
->m_len
>= sizeof(*h
)) /* the common case */
451 h
= mtod(m
, const struct cisco_header
*);
453 m_copydata(m
, 0, sizeof(*h
), (caddr_t
)&hdrbuf
);
456 m_adj(m
, sizeof(*h
));
458 /* Check header address */
459 switch (h
->address
) {
460 default: /* Invalid Cisco packet. */
463 case CISCO_MULTICAST
:
464 /* Don't check the control field here (RFC 1547). */
465 switch (ntohs(h
->protocol
)) {
468 case CISCO_KEEPALIVE
:
470 const struct cisco_packet
*p
;
471 struct cisco_packet pktbuf
;
473 /* Sanity check packet length */
474 if (m
->m_pkthdr
.len
< sizeof(*p
)) {
479 /* Get cisco packet */
480 if (m
->m_len
>= sizeof(*p
)) /* the common case */
481 p
= mtod(m
, const struct cisco_packet
*);
483 m_copydata(m
, 0, sizeof(*p
), (caddr_t
)&pktbuf
);
487 /* Check packet type */
488 switch (ntohl(p
->type
)) {
491 "cisco: unknown cisco packet type: 0x%lx\n",
492 (long)ntohl(p
->type
));
494 case CISCO_ADDR_REPLY
:
495 /* Reply on address request, ignore */
497 case CISCO_KEEPALIVE_REQ
:
498 sc
->remote_seq
= ntohl(p
->par1
);
499 if (sc
->local_seq
== ntohl(p
->par2
)) {
501 if (sc
->seqRetries
> 1)
502 cisco_notify(sc
, NGM_LINK_IS_UP
);
511 /* Ask inet peer for IP address information */
512 if (sc
->inet
.hook
== NULL
)
514 NG_MKMESSAGE(msg
, NGM_CISCO_COOKIE
,
515 NGM_CISCO_GET_IPADDR
, 0, M_WAITOK
| M_NULLOK
);
518 NG_SEND_MSG_HOOK(dummy_error
,
519 sc
->node
, msg
, sc
->inet
.hook
, 0);
521 * XXX Now maybe we should set a flag telling
522 * our receiver to send this message when the response comes in
523 * instead of now when the data may be bad.
526 /* Send reply to peer device */
527 error
= cisco_send(sc
, CISCO_ADDR_REPLY
,
528 ntohl(sc
->localip
.s_addr
),
529 ntohl(sc
->localmask
.s_addr
));
548 /* Drop if payload is empty */
549 if (m
->m_pkthdr
.len
== 0) {
555 if (pep
->hook
== NULL
)
557 NG_FWD_NEW_DATA(error
, item
, pep
->hook
, m
);
567 * Send keepalive packets, every 10 seconds.
570 cisco_keepalive(node_p node
, hook_p hook
, void *arg1
, int arg2
)
572 const sc_p sc
= arg1
;
574 cisco_send(sc
, CISCO_KEEPALIVE_REQ
, sc
->local_seq
, sc
->remote_seq
);
575 if (sc
->seqRetries
++ > 1)
576 cisco_notify(sc
, NGM_LINK_IS_DOWN
);
577 ng_callout(&sc
->handle
, node
, NULL
, (hz
* KEEPALIVE_SECS
),
578 &cisco_keepalive
, (void *)sc
, 0);
582 * Send Cisco keepalive packet.
585 cisco_send(sc_p sc
, int type
, long par1
, long par2
)
587 struct cisco_header
*h
;
588 struct cisco_packet
*ch
;
594 getmicrouptime(&time
);
596 MGETHDR(m
, MB_DONTWAIT
, MT_DATA
);
600 t
= time
.tv_sec
* 1000 + time
.tv_usec
/ 1000;
601 m
->m_pkthdr
.len
= m
->m_len
= CISCO_HEADER_LEN
+ CISCO_PACKET_LEN
;
602 m
->m_pkthdr
.rcvif
= 0;
604 h
= mtod(m
, struct cisco_header
*);
605 h
->address
= CISCO_MULTICAST
;
607 h
->protocol
= htons(CISCO_KEEPALIVE
);
609 ch
= (struct cisco_packet
*) (h
+ 1);
610 ch
->type
= htonl(type
);
611 ch
->par1
= htonl(par1
);
612 ch
->par2
= htonl(par2
);
614 ch
->time0
= htons((u_short
) (t
>> 16));
615 ch
->time1
= htons((u_short
) t
);
617 NG_SEND_DATA_ONLY(error
, sc
->downstream
.hook
, m
);
622 * Send linkstate to upstream node.
625 cisco_notify(sc_p sc
, uint32_t cmd
)
630 if (sc
->inet
.hook
== NULL
) /* nothing to notify */
633 NG_MKMESSAGE(msg
, NGM_FLOW_COOKIE
, cmd
, 0, M_WAITOK
| M_NULLOK
);
635 NG_SEND_MSG_HOOK(dummy_error
, sc
->node
, msg
, sc
->inet
.hook
, 0);