drm: Add linux/sched.h
[dragonfly.git] / sys / netgraph7 / cisco / ng_cisco.c
blob848422bdcccef30305aae30efb31507315955891
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 * $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>
50 #include <sys/mbuf.h>
51 #include <sys/syslog.h>
53 #include <net/if.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>
61 #include "ng_cisco.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
72 struct cisco_header {
73 u_char address;
74 u_char control;
75 u_short protocol;
78 #define CISCO_HEADER_LEN sizeof (struct cisco_header)
80 struct cisco_packet {
81 u_long type;
82 u_long par1;
83 u_long par2;
84 u_short rel;
85 u_short time0;
86 u_short time1;
89 #define CISCO_PACKET_LEN (sizeof(struct cisco_packet))
91 struct protoent {
92 hook_p hook; /* the hook for this proto */
93 u_short af; /* address family, -1 = downstream */
96 struct cisco_priv {
97 u_long local_seq;
98 u_long remote_seq;
99 u_long seqRetries; /* how many times we've been here throwing out
100 * the same sequence number without ack */
101 node_p node;
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[] = {
145 NGM_CISCO_COOKIE,
146 NGM_CISCO_SET_IPADDR,
147 "setipaddr",
148 &ng_cisco_ipaddr_type,
149 NULL
152 NGM_CISCO_COOKIE,
153 NGM_CISCO_GET_IPADDR,
154 "getipaddr",
155 NULL,
156 &ng_cisco_ipaddr_type
159 NGM_CISCO_COOKIE,
160 NGM_CISCO_GET_STATUS,
161 "getstats",
162 NULL,
163 &ng_cisco_stats_type
165 { 0 }
168 /* Node type */
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);
183 * Node constructor
185 static int
186 cisco_constructor(node_p node)
188 sc_p sc;
190 sc = kmalloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_NULLOK | M_ZERO);
191 if (sc == NULL)
192 return (ENOMEM);
194 ng_callout_init(&sc->handle);
195 NG_NODE_SET_PRIVATE(node, sc);
196 sc->node = node;
198 /* Initialise the varous protocol hook holders */
199 sc->downstream.af = 0xffff;
200 sc->inet.af = AF_INET;
201 sc->inet6.af = AF_INET6;
202 sc->ipx.af = AF_IPX;
203 return (0);
207 * Check new hook
209 static int
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) {
228 sc->ipx.hook = hook;
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 */
232 } else
233 return (EINVAL);
234 return 0;
238 * Receive control message.
240 static int
241 cisco_rcvmsg(node_p node, item_p item, hook_p lasthook)
243 struct ng_mesg *msg;
244 const sc_p sc = NG_NODE_PRIVATE(node);
245 struct ng_mesg *resp = NULL;
246 int error = 0;
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:
254 char *arg;
255 int pos;
257 NG_MKRESPONSE(resp, msg, NG_TEXTRESPONSE, M_WAITOK | M_NULLOK);
258 if (resp == NULL) {
259 error = ENOMEM;
260 break;
262 arg = (char *) resp->data;
263 pos = ksprintf(arg,
264 "keepalive period: %d sec; ", KEEPALIVE_SECS);
265 pos += ksprintf(arg + pos,
266 "unacknowledged keepalives: %ld", sc->seqRetries);
267 resp->header.arglen = pos + 1;
268 break;
270 default:
271 error = EINVAL;
272 break;
274 break;
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) {
279 struct in_addr *ips;
281 NG_MKRESPONSE(resp, msg,
282 2 * sizeof(*ips), M_WAITOK | M_NULLOK);
283 if (!resp) {
284 error = ENOMEM;
285 break;
287 ips = (struct in_addr *) resp->data;
288 ips[0] = sc->localip;
289 ips[1] = sc->localmask;
290 break;
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)) {
298 error = EINVAL;
299 break;
301 sc->localip = ips[0];
302 sc->localmask = ips[1];
303 break;
305 case NGM_CISCO_GET_STATUS:
307 struct ng_cisco_stats *stat;
309 NG_MKRESPONSE(resp, msg, sizeof(*stat), M_WAITOK | M_NULLOK);
310 if (!resp) {
311 error = ENOMEM;
312 break;
314 stat = (struct ng_cisco_stats *)resp->data;
315 stat->seqRetries = sc->seqRetries;
316 stat->keepAlivePeriod = KEEPALIVE_SECS;
317 break;
319 default:
320 error = EINVAL;
321 break;
323 break;
324 default:
325 error = EINVAL;
326 break;
328 NG_RESPOND_MSG(error, node, item, resp);
329 NG_FREE_MSG(msg);
330 return (error);
334 * Receive data
336 static int
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;
342 struct mbuf *m;
343 int error = 0;
345 if ((pep = NG_HOOK_PRIVATE(hook)) == NULL)
346 goto out;
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);
356 if (!m) {
357 error = ENOBUFS;
358 goto out;
360 h = mtod(m, struct cisco_header *);
361 h->address = CISCO_UNICAST;
362 h->control = 0;
364 switch (pep->af) {
365 case AF_INET: /* Internet Protocol */
366 h->protocol = htons(ETHERTYPE_IP);
367 break;
368 case AF_INET6:
369 h->protocol = htons(ETHERTYPE_IPV6);
370 break;
371 case AF_IPX: /* Novell IPX Protocol */
372 h->protocol = htons(ETHERTYPE_IPX);
373 break;
374 default:
375 error = EAFNOSUPPORT;
376 goto out;
379 /* Send it */
380 NG_FWD_NEW_DATA(error, item, sc->downstream.hook, m);
381 return (error);
383 out:
384 NG_FREE_ITEM(item);
385 return (error);
389 * Shutdown node
391 static int
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);
399 return (0);
403 * Disconnection of a hook
405 * For this type, removal of the last link destroys the node
407 static int
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))) {
415 pep->hook = NULL;
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));
425 return (0);
429 * Receive data
431 static int
432 cisco_input(sc_p sc, item_p item)
434 const struct cisco_header *h;
435 struct cisco_header hdrbuf;
436 struct protoent *pep;
437 struct mbuf *m;
438 int error = 0;
440 /* Get data */
441 m = NGI_M(item);
443 /* Sanity check header length */
444 if (m->m_pkthdr.len < sizeof(*h)) {
445 error = EINVAL;
446 goto drop;
449 /* Get cisco header */
450 if (m->m_len >= sizeof(*h)) /* the common case */
451 h = mtod(m, const struct cisco_header *);
452 else {
453 m_copydata(m, 0, sizeof(*h), (caddr_t)&hdrbuf);
454 h = &hdrbuf;
456 m_adj(m, sizeof(*h));
458 /* Check header address */
459 switch (h->address) {
460 default: /* Invalid Cisco packet. */
461 goto drop;
462 case CISCO_UNICAST:
463 case CISCO_MULTICAST:
464 /* Don't check the control field here (RFC 1547). */
465 switch (ntohs(h->protocol)) {
466 default:
467 goto drop;
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)) {
475 error = EINVAL;
476 goto drop;
479 /* Get cisco packet */
480 if (m->m_len >= sizeof(*p)) /* the common case */
481 p = mtod(m, const struct cisco_packet *);
482 else {
483 m_copydata(m, 0, sizeof(*p), (caddr_t)&pktbuf);
484 p = &pktbuf;
487 /* Check packet type */
488 switch (ntohl(p->type)) {
489 default:
490 log(LOG_WARNING,
491 "cisco: unknown cisco packet type: 0x%lx\n",
492 (long)ntohl(p->type));
493 break;
494 case CISCO_ADDR_REPLY:
495 /* Reply on address request, ignore */
496 break;
497 case CISCO_KEEPALIVE_REQ:
498 sc->remote_seq = ntohl(p->par1);
499 if (sc->local_seq == ntohl(p->par2)) {
500 sc->local_seq++;
501 if (sc->seqRetries > 1)
502 cisco_notify(sc, NGM_LINK_IS_UP);
503 sc->seqRetries = 0;
505 break;
506 case CISCO_ADDR_REQ:
508 struct ng_mesg *msg;
509 int dummy_error = 0;
511 /* Ask inet peer for IP address information */
512 if (sc->inet.hook == NULL)
513 goto nomsg;
514 NG_MKMESSAGE(msg, NGM_CISCO_COOKIE,
515 NGM_CISCO_GET_IPADDR, 0, M_WAITOK | M_NULLOK);
516 if (msg == NULL)
517 goto nomsg;
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.
525 nomsg:
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));
530 break;
533 goto drop;
535 case ETHERTYPE_IP:
536 pep = &sc->inet;
537 break;
538 case ETHERTYPE_IPV6:
539 pep = &sc->inet6;
540 break;
541 case ETHERTYPE_IPX:
542 pep = &sc->ipx;
543 break;
545 break;
548 /* Drop if payload is empty */
549 if (m->m_pkthdr.len == 0) {
550 error = EINVAL;
551 goto drop;
554 /* Send it on */
555 if (pep->hook == NULL)
556 goto drop;
557 NG_FWD_NEW_DATA(error, item, pep->hook, m);
558 return (error);
560 drop:
561 NG_FREE_ITEM(item);
562 return (error);
567 * Send keepalive packets, every 10 seconds.
569 static void
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.
584 static int
585 cisco_send(sc_p sc, int type, long par1, long par2)
587 struct cisco_header *h;
588 struct cisco_packet *ch;
589 struct mbuf *m;
590 struct timeval time;
591 u_long t;
592 int error = 0;
594 getmicrouptime(&time);
596 MGETHDR(m, MB_DONTWAIT, MT_DATA);
597 if (!m)
598 return (ENOBUFS);
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;
606 h->control = 0;
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);
613 ch->rel = -1;
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);
618 return (error);
622 * Send linkstate to upstream node.
624 static void
625 cisco_notify(sc_p sc, uint32_t cmd)
627 struct ng_mesg *msg;
628 int dummy_error = 0;
630 if (sc->inet.hook == NULL) /* nothing to notify */
631 return;
633 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_WAITOK | M_NULLOK);
634 if (msg != NULL)
635 NG_SEND_MSG_HOOK(dummy_error, sc->node, msg, sc->inet.hook, 0);