IO APIC: Get rid of apic_imen
[dragonfly.git] / sys / netgraph7 / ng_cisco.c
blob29e642f6476a835442141d46be42ab876000a846
1 /*
2 * ng_cisco.c
3 */
5 /*-
6 * Copyright (c) 1996-1999 Whistle Communications, Inc.
7 * All rights reserved.
8 *
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
36 * OF SUCH DAMAGE.
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>
51 #include <sys/mbuf.h>
52 #include <sys/syslog.h>
54 #include <net/if.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"
65 #include "netgraph.h"
66 #include "ng_parse.h"
67 #include "ng_cisco.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
78 struct cisco_header {
79 u_char address;
80 u_char control;
81 u_short protocol;
84 #define CISCO_HEADER_LEN sizeof (struct cisco_header)
86 struct cisco_packet {
87 u_long type;
88 u_long par1;
89 u_long par2;
90 u_short rel;
91 u_short time0;
92 u_short time1;
95 #define CISCO_PACKET_LEN (sizeof(struct cisco_packet))
97 struct protoent {
98 hook_p hook; /* the hook for this proto */
99 u_short af; /* address family, -1 = downstream */
102 struct cisco_priv {
103 u_long local_seq;
104 u_long remote_seq;
105 u_long seqRetries; /* how many times we've been here throwing out
106 * the same sequence number without ack */
107 node_p node;
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[] = {
152 NGM_CISCO_COOKIE,
153 NGM_CISCO_SET_IPADDR,
154 "setipaddr",
155 &ng_cisco_ipaddr_type,
156 NULL
159 NGM_CISCO_COOKIE,
160 NGM_CISCO_GET_IPADDR,
161 "getipaddr",
162 NULL,
163 &ng_cisco_ipaddr_type
166 NGM_CISCO_COOKIE,
167 NGM_CISCO_GET_STATUS,
168 "getstats",
169 NULL,
170 &ng_cisco_stats_type
172 { 0 }
175 /* Node type */
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);
190 * Node constructor
192 static int
193 cisco_constructor(node_p node)
195 sc_p sc;
197 MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_WAITOK | M_NULLOK | M_ZERO);
198 if (sc == NULL)
199 return (ENOMEM);
201 ng_callout_init(&sc->handle);
202 NG_NODE_SET_PRIVATE(node, sc);
203 sc->node = node;
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;
210 sc->ipx.af = AF_IPX;
211 return (0);
215 * Check new hook
217 static int
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) {
239 sc->ipx.hook = hook;
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 */
243 } else
244 return (EINVAL);
245 return 0;
249 * Receive control message.
251 static int
252 cisco_rcvmsg(node_p node, item_p item, hook_p lasthook)
254 struct ng_mesg *msg;
255 const sc_p sc = NG_NODE_PRIVATE(node);
256 struct ng_mesg *resp = NULL;
257 int error = 0;
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:
265 char *arg;
266 int pos;
268 NG_MKRESPONSE(resp, msg, NG_TEXTRESPONSE, M_WAITOK | M_NULLOK);
269 if (resp == NULL) {
270 error = ENOMEM;
271 break;
273 arg = (char *) resp->data;
274 pos = sprintf(arg,
275 "keepalive period: %d sec; ", KEEPALIVE_SECS);
276 pos += sprintf(arg + pos,
277 "unacknowledged keepalives: %ld", sc->seqRetries);
278 resp->header.arglen = pos + 1;
279 break;
281 default:
282 error = EINVAL;
283 break;
285 break;
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) {
290 struct in_addr *ips;
292 NG_MKRESPONSE(resp, msg,
293 2 * sizeof(*ips), M_WAITOK | M_NULLOK);
294 if (!resp) {
295 error = ENOMEM;
296 break;
298 ips = (struct in_addr *) resp->data;
299 ips[0] = sc->localip;
300 ips[1] = sc->localmask;
301 break;
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)) {
309 error = EINVAL;
310 break;
312 sc->localip = ips[0];
313 sc->localmask = ips[1];
314 break;
316 case NGM_CISCO_GET_STATUS:
318 struct ng_cisco_stats *stat;
320 NG_MKRESPONSE(resp, msg, sizeof(*stat), M_WAITOK | M_NULLOK);
321 if (!resp) {
322 error = ENOMEM;
323 break;
325 stat = (struct ng_cisco_stats *)resp->data;
326 stat->seqRetries = sc->seqRetries;
327 stat->keepAlivePeriod = KEEPALIVE_SECS;
328 break;
330 default:
331 error = EINVAL;
332 break;
334 break;
335 default:
336 error = EINVAL;
337 break;
339 NG_RESPOND_MSG(error, node, item, resp);
340 NG_FREE_MSG(msg);
341 return (error);
345 * Receive data
347 static int
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;
353 struct mbuf *m;
354 int error = 0;
356 if ((pep = NG_HOOK_PRIVATE(hook)) == NULL)
357 goto out;
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);
367 if (!m) {
368 error = ENOBUFS;
369 goto out;
371 h = mtod(m, struct cisco_header *);
372 h->address = CISCO_UNICAST;
373 h->control = 0;
375 switch (pep->af) {
376 case AF_INET: /* Internet Protocol */
377 h->protocol = htons(ETHERTYPE_IP);
378 break;
379 case AF_INET6:
380 h->protocol = htons(ETHERTYPE_IPV6);
381 break;
382 case AF_APPLETALK: /* AppleTalk Protocol */
383 h->protocol = htons(ETHERTYPE_AT);
384 break;
385 case AF_IPX: /* Novell IPX Protocol */
386 h->protocol = htons(ETHERTYPE_IPX);
387 break;
388 default:
389 error = EAFNOSUPPORT;
390 goto out;
393 /* Send it */
394 NG_FWD_NEW_DATA(error, item, sc->downstream.hook, m);
395 return (error);
397 out:
398 NG_FREE_ITEM(item);
399 return (error);
403 * Shutdown node
405 static int
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);
413 return (0);
417 * Disconnection of a hook
419 * For this type, removal of the last link destroys the node
421 static int
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))) {
429 pep->hook = NULL;
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));
439 return (0);
443 * Receive data
445 static int
446 cisco_input(sc_p sc, item_p item)
448 const struct cisco_header *h;
449 struct cisco_header hdrbuf;
450 struct protoent *pep;
451 struct mbuf *m;
452 int error = 0;
454 /* Get data */
455 m = NGI_M(item);
457 /* Sanity check header length */
458 if (m->m_pkthdr.len < sizeof(*h)) {
459 error = EINVAL;
460 goto drop;
463 /* Get cisco header */
464 if (m->m_len >= sizeof(*h)) /* the common case */
465 h = mtod(m, const struct cisco_header *);
466 else {
467 m_copydata(m, 0, sizeof(*h), (caddr_t)&hdrbuf);
468 h = &hdrbuf;
470 m_adj(m, sizeof(*h));
472 /* Check header address */
473 switch (h->address) {
474 default: /* Invalid Cisco packet. */
475 goto drop;
476 case CISCO_UNICAST:
477 case CISCO_MULTICAST:
478 /* Don't check the control field here (RFC 1547). */
479 switch (ntohs(h->protocol)) {
480 default:
481 goto drop;
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)) {
489 error = EINVAL;
490 goto drop;
493 /* Get cisco packet */
494 if (m->m_len >= sizeof(*p)) /* the common case */
495 p = mtod(m, const struct cisco_packet *);
496 else {
497 m_copydata(m, 0, sizeof(*p), (caddr_t)&pktbuf);
498 p = &pktbuf;
501 /* Check packet type */
502 switch (ntohl(p->type)) {
503 default:
504 log(LOG_WARNING,
505 "cisco: unknown cisco packet type: 0x%lx\n",
506 (long)ntohl(p->type));
507 break;
508 case CISCO_ADDR_REPLY:
509 /* Reply on address request, ignore */
510 break;
511 case CISCO_KEEPALIVE_REQ:
512 sc->remote_seq = ntohl(p->par1);
513 if (sc->local_seq == ntohl(p->par2)) {
514 sc->local_seq++;
515 if (sc->seqRetries > 1)
516 cisco_notify(sc, NGM_LINK_IS_UP);
517 sc->seqRetries = 0;
519 break;
520 case CISCO_ADDR_REQ:
522 struct ng_mesg *msg;
523 int dummy_error = 0;
525 /* Ask inet peer for IP address information */
526 if (sc->inet.hook == NULL)
527 goto nomsg;
528 NG_MKMESSAGE(msg, NGM_CISCO_COOKIE,
529 NGM_CISCO_GET_IPADDR, 0, M_WAITOK | M_NULLOK);
530 if (msg == NULL)
531 goto nomsg;
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.
539 nomsg:
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));
544 break;
547 goto drop;
549 case ETHERTYPE_IP:
550 pep = &sc->inet;
551 break;
552 case ETHERTYPE_IPV6:
553 pep = &sc->inet6;
554 break;
555 case ETHERTYPE_AT:
556 pep = &sc->atalk;
557 break;
558 case ETHERTYPE_IPX:
559 pep = &sc->ipx;
560 break;
562 break;
565 /* Drop if payload is empty */
566 if (m->m_pkthdr.len == 0) {
567 error = EINVAL;
568 goto drop;
571 /* Send it on */
572 if (pep->hook == NULL)
573 goto drop;
574 NG_FWD_NEW_DATA(error, item, pep->hook, m);
575 return (error);
577 drop:
578 NG_FREE_ITEM(item);
579 return (error);
584 * Send keepalive packets, every 10 seconds.
586 static void
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.
601 static int
602 cisco_send(sc_p sc, int type, long par1, long par2)
604 struct cisco_header *h;
605 struct cisco_packet *ch;
606 struct mbuf *m;
607 struct timeval time;
608 u_long t;
609 int error = 0;
611 getmicrouptime(&time);
613 MGETHDR(m, MB_DONTWAIT, MT_DATA);
614 if (!m)
615 return (ENOBUFS);
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;
623 h->control = 0;
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);
630 ch->rel = -1;
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);
635 return (error);
639 * Send linkstate to upstream node.
641 static void
642 cisco_notify(sc_p sc, uint32_t cmd)
644 struct ng_mesg *msg;
645 int dummy_error = 0;
647 if (sc->inet.hook == NULL) /* nothing to notify */
648 return;
650 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_WAITOK | M_NULLOK);
651 if (msg != NULL)
652 NG_SEND_MSG_HOOK(dummy_error, sc->node, msg, sc->inet.hook, 0);