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 * $DragonFly: src/sys/netgraph7/ng_cisco.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
42 * $Whistle: ng_cisco.c,v 1.25 1999/11/01 09:24:51 julian Exp $
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/errno.h>
48 #include <sys/kernel.h>
49 #include <sys/socket.h>
50 #include <sys/malloc.h>
52 #include <sys/syslog.h>
56 #include <netinet/in.h>
57 #include <netinet/if_ether.h>
59 #include <netatalk/at.h>
61 #include <netipx/ipx.h>
62 #include <netipx/ipx_if.h>
64 #include "ng_message.h"
69 #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
70 #define CISCO_UNICAST 0x0f /* Cisco unicast address */
71 #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
72 #define CISCO_ADDR_REQ 0 /* Cisco address request */
73 #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
74 #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
76 #define KEEPALIVE_SECS 10
84 #define CISCO_HEADER_LEN sizeof (struct cisco_header)
95 #define CISCO_PACKET_LEN (sizeof(struct cisco_packet))
98 hook_p hook
; /* the hook for this proto */
99 u_short af
; /* address family, -1 = downstream */
105 u_long seqRetries
; /* how many times we've been here throwing out
106 * the same sequence number without ack */
108 struct callout handle
;
109 struct protoent downstream
;
110 struct protoent inet
; /* IP information */
111 struct in_addr localip
;
112 struct in_addr localmask
;
113 struct protoent inet6
; /* IPv6 information */
114 struct protoent atalk
; /* AppleTalk information */
115 struct protoent ipx
; /* IPX information */
117 typedef struct cisco_priv
*sc_p
;
119 /* Netgraph methods */
120 static ng_constructor_t cisco_constructor
;
121 static ng_rcvmsg_t cisco_rcvmsg
;
122 static ng_shutdown_t cisco_shutdown
;
123 static ng_newhook_t cisco_newhook
;
124 static ng_rcvdata_t cisco_rcvdata
;
125 static ng_disconnect_t cisco_disconnect
;
127 /* Other functions */
128 static int cisco_input(sc_p sc
, item_p item
);
129 static void cisco_keepalive(node_p node
, hook_p hook
, void *arg1
, int arg2
);
130 static int cisco_send(sc_p sc
, int type
, long par1
, long par2
);
131 static void cisco_notify(sc_p sc
, uint32_t cmd
);
133 /* Parse type for struct ng_cisco_ipaddr */
134 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields
[]
135 = NG_CISCO_IPADDR_TYPE_INFO
;
136 static const struct ng_parse_type ng_cisco_ipaddr_type
= {
137 &ng_parse_struct_type
,
138 &ng_cisco_ipaddr_type_fields
141 /* Parse type for struct ng_async_stat */
142 static const struct ng_parse_struct_field ng_cisco_stats_type_fields
[]
143 = NG_CISCO_STATS_TYPE_INFO
;
144 static const struct ng_parse_type ng_cisco_stats_type
= {
145 &ng_parse_struct_type
,
146 &ng_cisco_stats_type_fields
149 /* List of commands and how to convert arguments to/from ASCII */
150 static const struct ng_cmdlist ng_cisco_cmdlist
[] = {
153 NGM_CISCO_SET_IPADDR
,
155 &ng_cisco_ipaddr_type
,
160 NGM_CISCO_GET_IPADDR
,
163 &ng_cisco_ipaddr_type
167 NGM_CISCO_GET_STATUS
,
176 static struct ng_type typestruct
= {
177 .version
= NG_ABI_VERSION
,
178 .name
= NG_CISCO_NODE_TYPE
,
179 .constructor
= cisco_constructor
,
180 .rcvmsg
= cisco_rcvmsg
,
181 .shutdown
= cisco_shutdown
,
182 .newhook
= cisco_newhook
,
183 .rcvdata
= cisco_rcvdata
,
184 .disconnect
= cisco_disconnect
,
185 .cmdlist
= ng_cisco_cmdlist
,
187 NETGRAPH_INIT(cisco
, &typestruct
);
193 cisco_constructor(node_p node
)
197 MALLOC(sc
, sc_p
, sizeof(*sc
), M_NETGRAPH
, M_WAITOK
| M_NULLOK
| M_ZERO
);
201 ng_callout_init(&sc
->handle
);
202 NG_NODE_SET_PRIVATE(node
, sc
);
205 /* Initialise the varous protocol hook holders */
206 sc
->downstream
.af
= 0xffff;
207 sc
->inet
.af
= AF_INET
;
208 sc
->inet6
.af
= AF_INET6
;
209 sc
->atalk
.af
= AF_APPLETALK
;
218 cisco_newhook(node_p node
, hook_p hook
, const char *name
)
220 const sc_p sc
= NG_NODE_PRIVATE(node
);
222 if (strcmp(name
, NG_CISCO_HOOK_DOWNSTREAM
) == 0) {
223 sc
->downstream
.hook
= hook
;
224 NG_HOOK_SET_PRIVATE(hook
, &sc
->downstream
);
226 /* Start keepalives */
227 ng_callout(&sc
->handle
, node
, NULL
, (hz
* KEEPALIVE_SECS
),
228 &cisco_keepalive
, (void *)sc
, 0);
229 } else if (strcmp(name
, NG_CISCO_HOOK_INET
) == 0) {
230 sc
->inet
.hook
= hook
;
231 NG_HOOK_SET_PRIVATE(hook
, &sc
->inet
);
232 } else if (strcmp(name
, NG_CISCO_HOOK_INET6
) == 0) {
233 sc
->inet6
.hook
= hook
;
234 NG_HOOK_SET_PRIVATE(hook
, &sc
->inet6
);
235 } else if (strcmp(name
, NG_CISCO_HOOK_APPLETALK
) == 0) {
236 sc
->atalk
.hook
= hook
;
237 NG_HOOK_SET_PRIVATE(hook
, &sc
->atalk
);
238 } else if (strcmp(name
, NG_CISCO_HOOK_IPX
) == 0) {
240 NG_HOOK_SET_PRIVATE(hook
, &sc
->ipx
);
241 } else if (strcmp(name
, NG_CISCO_HOOK_DEBUG
) == 0) {
242 NG_HOOK_SET_PRIVATE(hook
, NULL
); /* unimplemented */
249 * Receive control message.
252 cisco_rcvmsg(node_p node
, item_p item
, hook_p lasthook
)
255 const sc_p sc
= NG_NODE_PRIVATE(node
);
256 struct ng_mesg
*resp
= NULL
;
259 NGI_GET_MSG(item
, msg
);
260 switch (msg
->header
.typecookie
) {
261 case NGM_GENERIC_COOKIE
:
262 switch (msg
->header
.cmd
) {
263 case NGM_TEXT_STATUS
:
268 NG_MKRESPONSE(resp
, msg
, NG_TEXTRESPONSE
, M_WAITOK
| M_NULLOK
);
273 arg
= (char *) resp
->data
;
275 "keepalive period: %d sec; ", KEEPALIVE_SECS
);
276 pos
+= sprintf(arg
+ pos
,
277 "unacknowledged keepalives: %ld", sc
->seqRetries
);
278 resp
->header
.arglen
= pos
+ 1;
286 case NGM_CISCO_COOKIE
:
287 switch (msg
->header
.cmd
) {
288 case NGM_CISCO_GET_IPADDR
: /* could be a late reply! */
289 if ((msg
->header
.flags
& NGF_RESP
) == 0) {
292 NG_MKRESPONSE(resp
, msg
,
293 2 * sizeof(*ips
), M_WAITOK
| M_NULLOK
);
298 ips
= (struct in_addr
*) resp
->data
;
299 ips
[0] = sc
->localip
;
300 ips
[1] = sc
->localmask
;
303 /* FALLTHROUGH */ /* ...if it's a reply */
304 case NGM_CISCO_SET_IPADDR
:
306 struct in_addr
*const ips
= (struct in_addr
*)msg
->data
;
308 if (msg
->header
.arglen
< 2 * sizeof(*ips
)) {
312 sc
->localip
= ips
[0];
313 sc
->localmask
= ips
[1];
316 case NGM_CISCO_GET_STATUS
:
318 struct ng_cisco_stats
*stat
;
320 NG_MKRESPONSE(resp
, msg
, sizeof(*stat
), M_WAITOK
| M_NULLOK
);
325 stat
= (struct ng_cisco_stats
*)resp
->data
;
326 stat
->seqRetries
= sc
->seqRetries
;
327 stat
->keepAlivePeriod
= KEEPALIVE_SECS
;
339 NG_RESPOND_MSG(error
, node
, item
, resp
);
348 cisco_rcvdata(hook_p hook
, item_p item
)
350 const sc_p sc
= NG_NODE_PRIVATE(NG_HOOK_NODE(hook
));
351 struct protoent
*pep
;
352 struct cisco_header
*h
;
356 if ((pep
= NG_HOOK_PRIVATE(hook
)) == NULL
)
359 /* If it came from our downlink, deal with it separately */
360 if (pep
->af
== 0xffff)
361 return (cisco_input(sc
, item
));
363 /* OK so it came from a protocol, heading out. Prepend general data
364 packet header. For now, IP,IPX only */
365 m
= NGI_M(item
); /* still associated with item */
366 M_PREPEND(m
, CISCO_HEADER_LEN
, MB_DONTWAIT
);
371 h
= mtod(m
, struct cisco_header
*);
372 h
->address
= CISCO_UNICAST
;
376 case AF_INET
: /* Internet Protocol */
377 h
->protocol
= htons(ETHERTYPE_IP
);
380 h
->protocol
= htons(ETHERTYPE_IPV6
);
382 case AF_APPLETALK
: /* AppleTalk Protocol */
383 h
->protocol
= htons(ETHERTYPE_AT
);
385 case AF_IPX
: /* Novell IPX Protocol */
386 h
->protocol
= htons(ETHERTYPE_IPX
);
389 error
= EAFNOSUPPORT
;
394 NG_FWD_NEW_DATA(error
, item
, sc
->downstream
.hook
, m
);
406 cisco_shutdown(node_p node
)
408 const sc_p sc
= NG_NODE_PRIVATE(node
);
410 NG_NODE_SET_PRIVATE(node
, NULL
);
411 NG_NODE_UNREF(sc
->node
);
412 FREE(sc
, M_NETGRAPH
);
417 * Disconnection of a hook
419 * For this type, removal of the last link destroys the node
422 cisco_disconnect(hook_p hook
)
424 const sc_p sc
= NG_NODE_PRIVATE(NG_HOOK_NODE(hook
));
425 struct protoent
*pep
;
427 /* Check it's not the debug hook */
428 if ((pep
= NG_HOOK_PRIVATE(hook
))) {
430 if (pep
->af
== 0xffff)
431 /* If it is the downstream hook, stop the timers */
432 ng_uncallout(&sc
->handle
, NG_HOOK_NODE(hook
));
435 /* If no more hooks, remove the node */
436 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook
)) == 0)
437 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook
))))
438 ng_rmnode_self(NG_HOOK_NODE(hook
));
446 cisco_input(sc_p sc
, item_p item
)
448 const struct cisco_header
*h
;
449 struct cisco_header hdrbuf
;
450 struct protoent
*pep
;
457 /* Sanity check header length */
458 if (m
->m_pkthdr
.len
< sizeof(*h
)) {
463 /* Get cisco header */
464 if (m
->m_len
>= sizeof(*h
)) /* the common case */
465 h
= mtod(m
, const struct cisco_header
*);
467 m_copydata(m
, 0, sizeof(*h
), (caddr_t
)&hdrbuf
);
470 m_adj(m
, sizeof(*h
));
472 /* Check header address */
473 switch (h
->address
) {
474 default: /* Invalid Cisco packet. */
477 case CISCO_MULTICAST
:
478 /* Don't check the control field here (RFC 1547). */
479 switch (ntohs(h
->protocol
)) {
482 case CISCO_KEEPALIVE
:
484 const struct cisco_packet
*p
;
485 struct cisco_packet pktbuf
;
487 /* Sanity check packet length */
488 if (m
->m_pkthdr
.len
< sizeof(*p
)) {
493 /* Get cisco packet */
494 if (m
->m_len
>= sizeof(*p
)) /* the common case */
495 p
= mtod(m
, const struct cisco_packet
*);
497 m_copydata(m
, 0, sizeof(*p
), (caddr_t
)&pktbuf
);
501 /* Check packet type */
502 switch (ntohl(p
->type
)) {
505 "cisco: unknown cisco packet type: 0x%lx\n",
506 (long)ntohl(p
->type
));
508 case CISCO_ADDR_REPLY
:
509 /* Reply on address request, ignore */
511 case CISCO_KEEPALIVE_REQ
:
512 sc
->remote_seq
= ntohl(p
->par1
);
513 if (sc
->local_seq
== ntohl(p
->par2
)) {
515 if (sc
->seqRetries
> 1)
516 cisco_notify(sc
, NGM_LINK_IS_UP
);
525 /* Ask inet peer for IP address information */
526 if (sc
->inet
.hook
== NULL
)
528 NG_MKMESSAGE(msg
, NGM_CISCO_COOKIE
,
529 NGM_CISCO_GET_IPADDR
, 0, M_WAITOK
| M_NULLOK
);
532 NG_SEND_MSG_HOOK(dummy_error
,
533 sc
->node
, msg
, sc
->inet
.hook
, 0);
535 * XXX Now maybe we should set a flag telling
536 * our receiver to send this message when the response comes in
537 * instead of now when the data may be bad.
540 /* Send reply to peer device */
541 error
= cisco_send(sc
, CISCO_ADDR_REPLY
,
542 ntohl(sc
->localip
.s_addr
),
543 ntohl(sc
->localmask
.s_addr
));
565 /* Drop if payload is empty */
566 if (m
->m_pkthdr
.len
== 0) {
572 if (pep
->hook
== NULL
)
574 NG_FWD_NEW_DATA(error
, item
, pep
->hook
, m
);
584 * Send keepalive packets, every 10 seconds.
587 cisco_keepalive(node_p node
, hook_p hook
, void *arg1
, int arg2
)
589 const sc_p sc
= arg1
;
591 cisco_send(sc
, CISCO_KEEPALIVE_REQ
, sc
->local_seq
, sc
->remote_seq
);
592 if (sc
->seqRetries
++ > 1)
593 cisco_notify(sc
, NGM_LINK_IS_DOWN
);
594 ng_callout(&sc
->handle
, node
, NULL
, (hz
* KEEPALIVE_SECS
),
595 &cisco_keepalive
, (void *)sc
, 0);
599 * Send Cisco keepalive packet.
602 cisco_send(sc_p sc
, int type
, long par1
, long par2
)
604 struct cisco_header
*h
;
605 struct cisco_packet
*ch
;
611 getmicrouptime(&time
);
613 MGETHDR(m
, MB_DONTWAIT
, MT_DATA
);
617 t
= time
.tv_sec
* 1000 + time
.tv_usec
/ 1000;
618 m
->m_pkthdr
.len
= m
->m_len
= CISCO_HEADER_LEN
+ CISCO_PACKET_LEN
;
619 m
->m_pkthdr
.rcvif
= 0;
621 h
= mtod(m
, struct cisco_header
*);
622 h
->address
= CISCO_MULTICAST
;
624 h
->protocol
= htons(CISCO_KEEPALIVE
);
626 ch
= (struct cisco_packet
*) (h
+ 1);
627 ch
->type
= htonl(type
);
628 ch
->par1
= htonl(par1
);
629 ch
->par2
= htonl(par2
);
631 ch
->time0
= htons((u_short
) (t
>> 16));
632 ch
->time1
= htons((u_short
) t
);
634 NG_SEND_DATA_ONLY(error
, sc
->downstream
.hook
, m
);
639 * Send linkstate to upstream node.
642 cisco_notify(sc_p sc
, uint32_t cmd
)
647 if (sc
->inet
.hook
== NULL
) /* nothing to notify */
650 NG_MKMESSAGE(msg
, NGM_FLOW_COOKIE
, cmd
, 0, M_WAITOK
| M_NULLOK
);
652 NG_SEND_MSG_HOOK(dummy_error
, sc
->node
, msg
, sc
->inet
.hook
, 0);