ng_cisco.c revision 70700
1 2/* 3 * ng_cisco.c 4 * 5 * Copyright (c) 1996-1999 Whistle Communications, Inc. 6 * All rights reserved. 7 * 8 * Subject to the following obligations and disclaimer of warranty, use and 9 * redistribution of this software, in source or object code forms, with or 10 * without modifications are expressly permitted by Whistle Communications; 11 * provided, however, that: 12 * 1. Any and all reproductions of the source or object code must include the 13 * copyright notice above and the following disclaimer of warranties; and 14 * 2. No rights are granted, in any manner or form, to use Whistle 15 * Communications, Inc. trademarks, including the mark "WHISTLE 16 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 17 * such appears in the above copyright notice or in the software. 18 * 19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 35 * OF SUCH DAMAGE. 36 * 37 * Author: Julian Elischer <julian@freebsd.org> 38 * 39 * $FreeBSD: head/sys/netgraph/ng_cisco.c 70700 2001-01-06 00:46:47Z julian $ 40 * $Whistle: ng_cisco.c,v 1.25 1999/11/01 09:24:51 julian Exp $ 41 */ 42 43#include <sys/param.h> 44#include <sys/systm.h> 45#include <sys/errno.h> 46#include <sys/kernel.h> 47#include <sys/socket.h> 48#include <sys/malloc.h> 49#include <sys/mbuf.h> 50#include <sys/syslog.h> 51 52#include <net/if.h> 53 54#include <netinet/in.h> 55#include <netinet/if_ether.h> 56 57#include <netatalk/at.h> 58 59#include <netipx/ipx.h> 60#include <netipx/ipx_if.h> 61 62#include <netgraph/ng_message.h> 63#include <netgraph/netgraph.h> 64#include <netgraph/ng_parse.h> 65#include <netgraph/ng_cisco.h> 66 67#define CISCO_MULTICAST 0x8f /* Cisco multicast address */ 68#define CISCO_UNICAST 0x0f /* Cisco unicast address */ 69#define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */ 70#define CISCO_ADDR_REQ 0 /* Cisco address request */ 71#define CISCO_ADDR_REPLY 1 /* Cisco address reply */ 72#define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */ 73 74#define KEEPALIVE_SECS 10 75 76struct cisco_header { 77 u_char address; 78 u_char control; 79 u_short protocol; 80}; 81 82#define CISCO_HEADER_LEN sizeof (struct cisco_header) 83 84struct cisco_packet { 85 u_long type; 86 u_long par1; 87 u_long par2; 88 u_short rel; 89 u_short time0; 90 u_short time1; 91}; 92 93#define CISCO_PACKET_LEN (sizeof(struct cisco_packet)) 94 95struct protoent { 96 hook_p hook; /* the hook for this proto */ 97 u_short af; /* address family, -1 = downstream */ 98}; 99 100struct cisco_priv { 101 u_long local_seq; 102 u_long remote_seq; 103 u_long seqRetries; /* how many times we've been here throwing out 104 * the same sequence number without ack */ 105 node_p node; 106 struct callout_handle handle; 107 struct protoent downstream; 108 struct protoent inet; /* IP information */ 109 struct in_addr localip; 110 struct in_addr localmask; 111 struct protoent inet6; /* IPv6 information */ 112 struct protoent atalk; /* AppleTalk information */ 113 struct protoent ipx; /* IPX information */ 114}; 115typedef struct cisco_priv *sc_p; 116 117/* Netgraph methods */ 118static ng_constructor_t cisco_constructor; 119static ng_rcvmsg_t cisco_rcvmsg; 120static ng_shutdown_t cisco_shutdown; 121static ng_newhook_t cisco_newhook; 122static ng_rcvdata_t cisco_rcvdata; 123static ng_disconnect_t cisco_disconnect; 124 125/* Other functions */ 126static int cisco_input(sc_p sc, item_p item); 127static void cisco_keepalive(void *arg); 128static int cisco_send(sc_p sc, int type, long par1, long par2); 129 130/* Parse type for struct ng_cisco_ipaddr */ 131static const struct ng_parse_struct_info 132 ng_cisco_ipaddr_type_info = NG_CISCO_IPADDR_TYPE_INFO; 133static const struct ng_parse_type ng_cisco_ipaddr_type = { 134 &ng_parse_struct_type, 135 &ng_cisco_ipaddr_type_info 136}; 137 138/* Parse type for struct ng_async_stat */ 139static const struct ng_parse_struct_info 140 ng_cisco_stats_type_info = NG_CISCO_STATS_TYPE_INFO; 141static const struct ng_parse_type ng_cisco_stats_type = { 142 &ng_parse_struct_type, 143 &ng_cisco_stats_type_info, 144}; 145 146/* List of commands and how to convert arguments to/from ASCII */ 147static const struct ng_cmdlist ng_cisco_cmdlist[] = { 148 { 149 NGM_CISCO_COOKIE, 150 NGM_CISCO_SET_IPADDR, 151 "setipaddr", 152 &ng_cisco_ipaddr_type, 153 NULL 154 }, 155 { 156 NGM_CISCO_COOKIE, 157 NGM_CISCO_GET_IPADDR, 158 "getipaddr", 159 NULL, 160 &ng_cisco_ipaddr_type 161 }, 162 { 163 NGM_CISCO_COOKIE, 164 NGM_CISCO_GET_STATUS, 165 "getstats", 166 NULL, 167 &ng_cisco_stats_type 168 }, 169 { 0 } 170}; 171 172/* Node type */ 173static struct ng_type typestruct = { 174 NG_ABI_VERSION, 175 NG_CISCO_NODE_TYPE, 176 NULL, 177 cisco_constructor, 178 cisco_rcvmsg, 179 cisco_shutdown, 180 cisco_newhook, 181 NULL, 182 NULL, 183 cisco_rcvdata, 184 cisco_disconnect, 185 ng_cisco_cmdlist 186}; 187NETGRAPH_INIT(cisco, &typestruct); 188 189/* 190 * Node constructor 191 */ 192static int 193cisco_constructor(node_p node) 194{ 195 sc_p sc; 196 197 MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO); 198 if (sc == NULL) 199 return (ENOMEM); 200 201 callout_handle_init(&sc->handle); 202 node->private = sc; 203 sc->node = node; 204 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); 212} 213 214/* 215 * Check new hook 216 */ 217static int 218cisco_newhook(node_p node, hook_p hook, const char *name) 219{ 220 const sc_p sc = node->private; 221 222 if (strcmp(name, NG_CISCO_HOOK_DOWNSTREAM) == 0) { 223 sc->downstream.hook = hook; 224 hook->private = &sc->downstream; 225 226 /* Start keepalives */ 227 sc->handle = timeout(cisco_keepalive, sc, hz * KEEPALIVE_SECS); 228 } else if (strcmp(name, NG_CISCO_HOOK_INET) == 0) { 229 sc->inet.hook = hook; 230 hook->private = &sc->inet; 231 } else if (strcmp(name, NG_CISCO_HOOK_APPLETALK) == 0) { 232 sc->atalk.hook = hook; 233 hook->private = &sc->atalk; 234 } else if (strcmp(name, NG_CISCO_HOOK_IPX) == 0) { 235 sc->ipx.hook = hook; 236 hook->private = &sc->ipx; 237 } else if (strcmp(name, NG_CISCO_HOOK_DEBUG) == 0) { 238 hook->private = NULL; /* unimplemented */ 239 } else 240 return (EINVAL); 241 return 0; 242} 243 244/* 245 * Receive control message. 246 */ 247static int 248cisco_rcvmsg(node_p node, item_p item, hook_p lasthook) 249{ 250 struct ng_mesg *msg; 251 const sc_p sc = node->private; 252 struct ng_mesg *resp = NULL; 253 int error = 0; 254 255 NGI_GET_MSG(item, msg); 256 switch (msg->header.typecookie) { 257 case NGM_GENERIC_COOKIE: 258 switch (msg->header.cmd) { 259 case NGM_TEXT_STATUS: 260 { 261 char *arg; 262 int pos; 263 264 NG_MKRESPONSE(resp, msg, sizeof(struct ng_mesg) 265 + NG_TEXTRESPONSE, M_NOWAIT); 266 if (resp == NULL) { 267 error = ENOMEM; 268 break; 269 } 270 arg = (char *) resp->data; 271 pos = sprintf(arg, 272 "keepalive period: %d sec; ", KEEPALIVE_SECS); 273 pos += sprintf(arg + pos, 274 "unacknowledged keepalives: %ld", sc->seqRetries); 275 resp->header.arglen = pos + 1; 276 break; 277 } 278 default: 279 error = EINVAL; 280 break; 281 } 282 break; 283 case NGM_CISCO_COOKIE: 284 switch (msg->header.cmd) { 285 case NGM_CISCO_GET_IPADDR: /* could be a late reply! */ 286 if ((msg->header.flags & NGF_RESP) == 0) { 287 struct in_addr *ips; 288 289 NG_MKRESPONSE(resp, msg, 290 2 * sizeof(*ips), M_NOWAIT); 291 if (!resp) { 292 error = ENOMEM; 293 break; 294 } 295 ips = (struct in_addr *) resp->data; 296 ips[0] = sc->localip; 297 ips[1] = sc->localmask; 298 break; 299 } 300 /* FALLTHROUGH */ /* ...if it's a reply */ 301 case NGM_CISCO_SET_IPADDR: 302 { 303 struct in_addr *const ips = (struct in_addr *)msg->data; 304 305 if (msg->header.arglen < 2 * sizeof(*ips)) { 306 error = EINVAL; 307 break; 308 } 309 sc->localip = ips[0]; 310 sc->localmask = ips[1]; 311 break; 312 } 313 case NGM_CISCO_GET_STATUS: 314 { 315 struct ng_cisco_stats *stat; 316 317 NG_MKRESPONSE(resp, msg, sizeof(*stat), M_NOWAIT); 318 if (!resp) { 319 error = ENOMEM; 320 break; 321 } 322 stat = (struct ng_cisco_stats *)resp->data; 323 stat->seqRetries = sc->seqRetries; 324 stat->keepAlivePeriod = KEEPALIVE_SECS; 325 break; 326 } 327 default: 328 error = EINVAL; 329 break; 330 } 331 break; 332 default: 333 error = EINVAL; 334 break; 335 } 336 NG_RESPOND_MSG(error, node, item, resp); 337 NG_FREE_MSG(msg); 338 return (error); 339} 340 341/* 342 * Receive data 343 */ 344static int 345cisco_rcvdata(hook_p hook, item_p item) 346{ 347 const sc_p sc = hook->node->private; 348 struct protoent *pep; 349 struct cisco_header *h; 350 int error = 0; 351 struct mbuf *m; 352 353 if ((pep = hook->private) == NULL) 354 goto out; 355 356 /* If it came from our downlink, deal with it separately */ 357 if (pep->af == 0xffff) 358 return (cisco_input(sc, item)); 359 360 /* OK so it came from a protocol, heading out. Prepend general data 361 packet header. For now, IP,IPX only */ 362 m = NGI_M(item); /* still associated with item */ 363 M_PREPEND(m, CISCO_HEADER_LEN, M_DONTWAIT); 364 if (!m) { 365 error = ENOBUFS; 366 goto out; 367 } 368 h = mtod(m, struct cisco_header *); 369 h->address = CISCO_UNICAST; 370 h->control = 0; 371 372 switch (pep->af) { 373 case AF_INET: /* Internet Protocol */ 374 h->protocol = htons(ETHERTYPE_IP); 375 break; 376 case AF_INET6: 377 h->protocol = htons(ETHERTYPE_IPV6); 378 break; 379 case AF_APPLETALK: /* AppleTalk Protocol */ 380 h->protocol = htons(ETHERTYPE_AT); 381 break; 382 case AF_IPX: /* Novell IPX Protocol */ 383 h->protocol = htons(ETHERTYPE_IPX); 384 break; 385 default: 386 error = EAFNOSUPPORT; 387 goto out; 388 } 389 390 /* Send it */ 391 NG_FWD_NEW_DATA(error, item, sc->downstream.hook, m); 392 return (error); 393 394out: 395 NG_FREE_ITEM(item); 396 return (error); 397} 398 399/* 400 * Shutdown node 401 */ 402static int 403cisco_shutdown(node_p node) 404{ 405 const sc_p sc = node->private; 406 407 node->flags |= NG_INVALID; 408 node->private = NULL; 409 ng_unref(sc->node); 410 FREE(sc, M_NETGRAPH); 411 return (0); 412} 413 414/* 415 * Disconnection of a hook 416 * 417 * For this type, removal of the last link destroys the node 418 */ 419static int 420cisco_disconnect(hook_p hook) 421{ 422 const sc_p sc = hook->node->private; 423 struct protoent *pep; 424 425 /* Check it's not the debug hook */ 426 if ((pep = hook->private)) { 427 pep->hook = NULL; 428 if (pep->af == 0xffff) { 429 /* If it is the downstream hook, stop the timers */ 430 untimeout(cisco_keepalive, sc, sc->handle); 431 } 432 } 433 434 /* If no more hooks, remove the node */ 435 if ((hook->node->numhooks == 0) 436 && ((hook->node->flags & NG_INVALID) == 0)) 437 ng_rmnode_self(hook->node); 438 return (0); 439} 440 441/* 442 * Receive data 443 */ 444static int 445cisco_input(sc_p sc, item_p item) 446{ 447 struct cisco_header *h; 448 struct cisco_packet *p; 449 struct protoent *pep; 450 int error = 0; 451 struct mbuf *m; 452 453 m = NGI_M(item); 454 if (m->m_pkthdr.len <= CISCO_HEADER_LEN) 455 goto drop; 456 457 /* Strip off cisco header */ 458 h = mtod(m, struct cisco_header *); 459 m_adj(m, CISCO_HEADER_LEN); 460 461 switch (h->address) { 462 default: /* Invalid Cisco packet. */ 463 goto drop; 464 case CISCO_UNICAST: 465 case CISCO_MULTICAST: 466 /* Don't check the control field here (RFC 1547). */ 467 switch (ntohs(h->protocol)) { 468 default: 469 goto drop; 470 case CISCO_KEEPALIVE: 471 p = mtod(m, struct cisco_packet *); 472 switch (ntohl(p->type)) { 473 default: 474 log(LOG_WARNING, 475 "cisco: unknown cisco packet type: 0x%x\n", 476 ntohl(p->type)); 477 break; 478 case CISCO_ADDR_REPLY: 479 /* Reply on address request, ignore */ 480 break; 481 case CISCO_KEEPALIVE_REQ: 482 sc->remote_seq = ntohl(p->par1); 483 if (sc->local_seq == ntohl(p->par2)) { 484 sc->local_seq++; 485 sc->seqRetries = 0; 486 } 487 break; 488 case CISCO_ADDR_REQ: 489 { 490 struct ng_mesg *msg; 491 int dummy_error = 0; 492 493 /* Ask inet peer for IP address information */ 494 if (sc->inet.hook == NULL) 495 goto nomsg; 496 NG_MKMESSAGE(msg, NGM_CISCO_COOKIE, 497 NGM_CISCO_GET_IPADDR, 0, M_NOWAIT); 498 if (msg == NULL) 499 goto nomsg; 500 NG_SEND_MSG_HOOK(dummy_error, sc->node, msg, 501 sc->inet.hook, NULL); 502 /* 503 * XXX Now maybe we should set a flag telling 504 * our receiver to send this message when the response comes in 505 * instead of now when the data may be bad. 506 */ 507 nomsg: 508 /* Send reply to peer device */ 509 error = cisco_send(sc, CISCO_ADDR_REPLY, 510 ntohl(sc->localip.s_addr), 511 ntohl(sc->localmask.s_addr)); 512 break; 513 } 514 } 515 goto drop; 516 case ETHERTYPE_IP: 517 pep = &sc->inet; 518 break; 519 case ETHERTYPE_IPV6: 520 pep = &sc->inet6; 521 break; 522 case ETHERTYPE_AT: 523 pep = &sc->atalk; 524 break; 525 case ETHERTYPE_IPX: 526 pep = &sc->ipx; 527 break; 528 } 529 break; 530 } 531 532 /* Send it on */ 533 if (pep->hook == NULL) 534 goto drop; 535 NG_FWD_NEW_DATA(error, item, pep->hook, m); 536 return (error); 537 538drop: 539 NG_FREE_ITEM(item); 540 return (error); 541} 542 543 544/* 545 * Send keepalive packets, every 10 seconds. 546 */ 547static void 548cisco_keepalive(void *arg) 549{ 550 const sc_p sc = arg; 551 int s = splimp(); 552 553 cisco_send(sc, CISCO_KEEPALIVE_REQ, sc->local_seq, sc->remote_seq); 554 sc->seqRetries++; 555 splx(s); 556 sc->handle = timeout(cisco_keepalive, sc, hz * KEEPALIVE_SECS); 557} 558 559/* 560 * Send Cisco keepalive packet. 561 */ 562static int 563cisco_send(sc_p sc, int type, long par1, long par2) 564{ 565 struct cisco_header *h; 566 struct cisco_packet *ch; 567 struct mbuf *m; 568 u_long t; 569 int error = 0; 570 struct timeval time; 571 572 getmicrotime(&time); 573 574 MGETHDR(m, M_DONTWAIT, MT_DATA); 575 if (!m) 576 return (ENOBUFS); 577 578 t = (time.tv_sec - boottime.tv_sec) * 1000; 579 m->m_pkthdr.len = m->m_len = CISCO_HEADER_LEN + CISCO_PACKET_LEN; 580 m->m_pkthdr.rcvif = 0; 581 582 h = mtod(m, struct cisco_header *); 583 h->address = CISCO_MULTICAST; 584 h->control = 0; 585 h->protocol = htons(CISCO_KEEPALIVE); 586 587 ch = (struct cisco_packet *) (h + 1); 588 ch->type = htonl(type); 589 ch->par1 = htonl(par1); 590 ch->par2 = htonl(par2); 591 ch->rel = -1; 592 ch->time0 = htons((u_short) (t >> 16)); 593 ch->time1 = htons((u_short) t); 594 595 NG_SEND_DATA_ONLY(error, sc->downstream.hook, m); 596 return (error); 597} 598