1 /* 2 * ng_cisco.c 3 */ 4 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. 19 * 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. 37 * 38 * Author: Julian Elischer <julian@freebsd.org> 39 * 40 * $FreeBSD$ 41 * $Whistle: ng_cisco.c,v 1.25 1999/11/01 09:24:51 julian Exp $ 42 */ 43 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> 52 53 #include <net/if.h> 54 55 #include <netinet/in.h> 56 #include <netinet/if_ether.h> 57 58 #include <netgraph/ng_message.h> 59 #include <netgraph/netgraph.h> 60 #include <netgraph/ng_parse.h> 61 #include <netgraph/ng_cisco.h> 62 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 */ 69 70 #define KEEPALIVE_SECS 10 71 72 struct cisco_header { 73 uint8_t address; 74 uint8_t control; 75 uint16_t protocol; 76 } __packed; 77 78 #define CISCO_HEADER_LEN sizeof (struct cisco_header) 79 80 struct cisco_packet { 81 uint32_t type; 82 uint32_t par1; 83 uint32_t par2; 84 uint16_t rel; 85 uint16_t time0; 86 uint16_t time1; 87 } __packed; 88 89 #define CISCO_PACKET_LEN (sizeof(struct cisco_packet)) 90 91 struct protoent { 92 hook_p hook; /* the hook for this proto */ 93 uint16_t af; /* address family, -1 = downstream */ 94 }; 95 96 struct cisco_priv { 97 uint32_t local_seq; 98 uint32_t remote_seq; 99 uint32_t 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 atalk; /* AppleTalk information */ 109 struct protoent ipx; /* IPX information */ 110 }; 111 typedef struct cisco_priv *sc_p; 112 113 /* Netgraph methods */ 114 static ng_constructor_t cisco_constructor; 115 static ng_rcvmsg_t cisco_rcvmsg; 116 static ng_shutdown_t cisco_shutdown; 117 static ng_newhook_t cisco_newhook; 118 static ng_rcvdata_t cisco_rcvdata; 119 static ng_disconnect_t cisco_disconnect; 120 121 /* Other functions */ 122 static int cisco_input(sc_p sc, item_p item); 123 static void cisco_keepalive(node_p node, hook_p hook, void *arg1, int arg2); 124 static int cisco_send(sc_p sc, int type, long par1, long par2); 125 static void cisco_notify(sc_p sc, uint32_t cmd); 126 127 /* Parse type for struct ng_cisco_ipaddr */ 128 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[] 129 = NG_CISCO_IPADDR_TYPE_INFO; 130 static const struct ng_parse_type ng_cisco_ipaddr_type = { 131 &ng_parse_struct_type, 132 &ng_cisco_ipaddr_type_fields 133 }; 134 135 /* Parse type for struct ng_async_stat */ 136 static const struct ng_parse_struct_field ng_cisco_stats_type_fields[] 137 = NG_CISCO_STATS_TYPE_INFO; 138 static const struct ng_parse_type ng_cisco_stats_type = { 139 &ng_parse_struct_type, 140 &ng_cisco_stats_type_fields 141 }; 142 143 /* List of commands and how to convert arguments to/from ASCII */ 144 static const struct ng_cmdlist ng_cisco_cmdlist[] = { 145 { 146 NGM_CISCO_COOKIE, 147 NGM_CISCO_SET_IPADDR, 148 "setipaddr", 149 &ng_cisco_ipaddr_type, 150 NULL 151 }, 152 { 153 NGM_CISCO_COOKIE, 154 NGM_CISCO_GET_IPADDR, 155 "getipaddr", 156 NULL, 157 &ng_cisco_ipaddr_type 158 }, 159 { 160 NGM_CISCO_COOKIE, 161 NGM_CISCO_GET_STATUS, 162 "getstats", 163 NULL, 164 &ng_cisco_stats_type 165 }, 166 { 0 } 167 }; 168 169 /* Node type */ 170 static struct ng_type typestruct = { 171 .version = NG_ABI_VERSION, 172 .name = NG_CISCO_NODE_TYPE, 173 .constructor = cisco_constructor, 174 .rcvmsg = cisco_rcvmsg, 175 .shutdown = cisco_shutdown, 176 .newhook = cisco_newhook, 177 .rcvdata = cisco_rcvdata, 178 .disconnect = cisco_disconnect, 179 .cmdlist = ng_cisco_cmdlist, 180 }; 181 NETGRAPH_INIT(cisco, &typestruct); 182 183 /* 184 * Node constructor 185 */ 186 static int 187 cisco_constructor(node_p node) 188 { 189 sc_p sc; 190 191 sc = malloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO); 192 193 ng_callout_init(&sc->handle); 194 NG_NODE_SET_PRIVATE(node, sc); 195 sc->node = node; 196 197 /* Initialise the varous protocol hook holders */ 198 sc->downstream.af = 0xffff; 199 sc->inet.af = AF_INET; 200 sc->inet6.af = AF_INET6; 201 sc->atalk.af = AF_APPLETALK; 202 sc->ipx.af = AF_IPX; 203 return (0); 204 } 205 206 /* 207 * Check new hook 208 */ 209 static int 210 cisco_newhook(node_p node, hook_p hook, const char *name) 211 { 212 const sc_p sc = NG_NODE_PRIVATE(node); 213 214 if (strcmp(name, NG_CISCO_HOOK_DOWNSTREAM) == 0) { 215 sc->downstream.hook = hook; 216 NG_HOOK_SET_PRIVATE(hook, &sc->downstream); 217 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_APPLETALK) == 0) { 228 sc->atalk.hook = hook; 229 NG_HOOK_SET_PRIVATE(hook, &sc->atalk); 230 } else if (strcmp(name, NG_CISCO_HOOK_IPX) == 0) { 231 sc->ipx.hook = hook; 232 NG_HOOK_SET_PRIVATE(hook, &sc->ipx); 233 } else if (strcmp(name, NG_CISCO_HOOK_DEBUG) == 0) { 234 NG_HOOK_SET_PRIVATE(hook, NULL); /* unimplemented */ 235 } else 236 return (EINVAL); 237 return 0; 238 } 239 240 /* 241 * Receive control message. 242 */ 243 static int 244 cisco_rcvmsg(node_p node, item_p item, hook_p lasthook) 245 { 246 struct ng_mesg *msg; 247 const sc_p sc = NG_NODE_PRIVATE(node); 248 struct ng_mesg *resp = NULL; 249 int error = 0; 250 251 NGI_GET_MSG(item, msg); 252 switch (msg->header.typecookie) { 253 case NGM_GENERIC_COOKIE: 254 switch (msg->header.cmd) { 255 case NGM_TEXT_STATUS: 256 { 257 char *arg; 258 int pos; 259 260 NG_MKRESPONSE(resp, msg, NG_TEXTRESPONSE, M_NOWAIT); 261 if (resp == NULL) { 262 error = ENOMEM; 263 break; 264 } 265 arg = (char *) resp->data; 266 pos = sprintf(arg, 267 "keepalive period: %d sec; ", KEEPALIVE_SECS); 268 pos += sprintf(arg + pos, 269 "unacknowledged keepalives: %d", sc->seqRetries); 270 resp->header.arglen = pos + 1; 271 break; 272 } 273 default: 274 error = EINVAL; 275 break; 276 } 277 break; 278 case NGM_CISCO_COOKIE: 279 switch (msg->header.cmd) { 280 case NGM_CISCO_GET_IPADDR: /* could be a late reply! */ 281 if ((msg->header.flags & NGF_RESP) == 0) { 282 struct in_addr *ips; 283 284 NG_MKRESPONSE(resp, msg, 285 2 * sizeof(*ips), M_NOWAIT); 286 if (!resp) { 287 error = ENOMEM; 288 break; 289 } 290 ips = (struct in_addr *) resp->data; 291 ips[0] = sc->localip; 292 ips[1] = sc->localmask; 293 break; 294 } 295 /* FALLTHROUGH */ /* ...if it's a reply */ 296 case NGM_CISCO_SET_IPADDR: 297 { 298 struct in_addr *const ips = (struct in_addr *)msg->data; 299 300 if (msg->header.arglen < 2 * sizeof(*ips)) { 301 error = EINVAL; 302 break; 303 } 304 sc->localip = ips[0]; 305 sc->localmask = ips[1]; 306 break; 307 } 308 case NGM_CISCO_GET_STATUS: 309 { 310 struct ng_cisco_stats *stat; 311 312 NG_MKRESPONSE(resp, msg, sizeof(*stat), M_NOWAIT); 313 if (!resp) { 314 error = ENOMEM; 315 break; 316 } 317 stat = (struct ng_cisco_stats *)resp->data; 318 stat->seqRetries = sc->seqRetries; 319 stat->keepAlivePeriod = KEEPALIVE_SECS; 320 break; 321 } 322 default: 323 error = EINVAL; 324 break; 325 } 326 break; 327 default: 328 error = EINVAL; 329 break; 330 } 331 NG_RESPOND_MSG(error, node, item, resp); 332 NG_FREE_MSG(msg); 333 return (error); 334 } 335 336 /* 337 * Receive data 338 */ 339 static int 340 cisco_rcvdata(hook_p hook, item_p item) 341 { 342 const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 343 struct protoent *pep; 344 struct cisco_header *h; 345 struct mbuf *m; 346 int error = 0; 347 348 if ((pep = NG_HOOK_PRIVATE(hook)) == NULL) 349 goto out; 350 351 /* If it came from our downlink, deal with it separately */ 352 if (pep->af == 0xffff) 353 return (cisco_input(sc, item)); 354 355 /* OK so it came from a protocol, heading out. Prepend general data 356 packet header. For now, IP,IPX only */ 357 NGI_GET_M(item, m); 358 M_PREPEND(m, CISCO_HEADER_LEN, M_NOWAIT); 359 if (!m) { 360 error = ENOBUFS; 361 goto out; 362 } 363 NGI_M(item) = m; 364 h = mtod(m, struct cisco_header *); 365 h->address = CISCO_UNICAST; 366 h->control = 0; 367 368 switch (pep->af) { 369 case AF_INET: /* Internet Protocol */ 370 h->protocol = htons(ETHERTYPE_IP); 371 break; 372 case AF_INET6: 373 h->protocol = htons(ETHERTYPE_IPV6); 374 break; 375 case AF_APPLETALK: /* AppleTalk Protocol */ 376 h->protocol = htons(ETHERTYPE_AT); 377 break; 378 case AF_IPX: /* Novell IPX Protocol */ 379 h->protocol = htons(ETHERTYPE_IPX); 380 break; 381 default: 382 error = EAFNOSUPPORT; 383 goto out; 384 } 385 386 /* Send it */ 387 NG_FWD_NEW_DATA(error, item, sc->downstream.hook, m); 388 return (error); 389 390 out: 391 NG_FREE_ITEM(item); 392 return (error); 393 } 394 395 /* 396 * Shutdown node 397 */ 398 static int 399 cisco_shutdown(node_p node) 400 { 401 const sc_p sc = NG_NODE_PRIVATE(node); 402 403 NG_NODE_SET_PRIVATE(node, NULL); 404 NG_NODE_UNREF(sc->node); 405 free(sc, M_NETGRAPH); 406 return (0); 407 } 408 409 /* 410 * Disconnection of a hook 411 * 412 * For this type, removal of the last link destroys the node 413 */ 414 static int 415 cisco_disconnect(hook_p hook) 416 { 417 const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 418 struct protoent *pep; 419 420 /* Check it's not the debug hook */ 421 if ((pep = NG_HOOK_PRIVATE(hook))) { 422 pep->hook = NULL; 423 if (pep->af == 0xffff) 424 /* If it is the downstream hook, stop the timers */ 425 ng_uncallout(&sc->handle, NG_HOOK_NODE(hook)); 426 } 427 428 /* If no more hooks, remove the node */ 429 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 430 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) 431 ng_rmnode_self(NG_HOOK_NODE(hook)); 432 return (0); 433 } 434 435 /* 436 * Receive data 437 */ 438 static int 439 cisco_input(sc_p sc, item_p item) 440 { 441 const struct cisco_header *h; 442 struct cisco_header hdrbuf; 443 struct protoent *pep; 444 struct mbuf *m; 445 int error = 0; 446 447 /* Get data */ 448 m = NGI_M(item); 449 450 /* Sanity check header length */ 451 if (m->m_pkthdr.len < sizeof(*h)) { 452 error = EINVAL; 453 goto drop; 454 } 455 456 /* Get cisco header */ 457 if (m->m_len >= sizeof(*h)) /* the common case */ 458 h = mtod(m, const struct cisco_header *); 459 else { 460 m_copydata(m, 0, sizeof(*h), (caddr_t)&hdrbuf); 461 h = &hdrbuf; 462 } 463 m_adj(m, sizeof(*h)); 464 465 /* Check header address */ 466 switch (h->address) { 467 default: /* Invalid Cisco packet. */ 468 goto drop; 469 case CISCO_UNICAST: 470 case CISCO_MULTICAST: 471 /* Don't check the control field here (RFC 1547). */ 472 switch (ntohs(h->protocol)) { 473 default: 474 goto drop; 475 case CISCO_KEEPALIVE: 476 { 477 const struct cisco_packet *p; 478 struct cisco_packet pktbuf; 479 480 /* Sanity check packet length */ 481 if (m->m_pkthdr.len < sizeof(*p)) { 482 error = EINVAL; 483 goto drop; 484 } 485 486 /* Get cisco packet */ 487 if (m->m_len >= sizeof(*p)) /* the common case */ 488 p = mtod(m, const struct cisco_packet *); 489 else { 490 m_copydata(m, 0, sizeof(*p), (caddr_t)&pktbuf); 491 p = &pktbuf; 492 } 493 494 /* Check packet type */ 495 switch (ntohl(p->type)) { 496 default: 497 log(LOG_WARNING, 498 "cisco: unknown cisco packet type: 0x%lx\n", 499 (long)ntohl(p->type)); 500 break; 501 case CISCO_ADDR_REPLY: 502 /* Reply on address request, ignore */ 503 break; 504 case CISCO_KEEPALIVE_REQ: 505 sc->remote_seq = ntohl(p->par1); 506 if (sc->local_seq == ntohl(p->par2)) { 507 sc->local_seq++; 508 if (sc->seqRetries > 1) 509 cisco_notify(sc, NGM_LINK_IS_UP); 510 sc->seqRetries = 0; 511 } 512 break; 513 case CISCO_ADDR_REQ: 514 { 515 struct ng_mesg *msg; 516 int dummy_error = 0; 517 518 /* Ask inet peer for IP address information */ 519 if (sc->inet.hook == NULL) 520 goto nomsg; 521 NG_MKMESSAGE(msg, NGM_CISCO_COOKIE, 522 NGM_CISCO_GET_IPADDR, 0, M_NOWAIT); 523 if (msg == NULL) 524 goto nomsg; 525 NG_SEND_MSG_HOOK(dummy_error, 526 sc->node, msg, sc->inet.hook, 0); 527 /* 528 * XXX Now maybe we should set a flag telling 529 * our receiver to send this message when the response comes in 530 * instead of now when the data may be bad. 531 */ 532 nomsg: 533 /* Send reply to peer device */ 534 error = cisco_send(sc, CISCO_ADDR_REPLY, 535 ntohl(sc->localip.s_addr), 536 ntohl(sc->localmask.s_addr)); 537 break; 538 } 539 } 540 goto drop; 541 } 542 case ETHERTYPE_IP: 543 pep = &sc->inet; 544 break; 545 case ETHERTYPE_IPV6: 546 pep = &sc->inet6; 547 break; 548 case ETHERTYPE_AT: 549 pep = &sc->atalk; 550 break; 551 case ETHERTYPE_IPX: 552 pep = &sc->ipx; 553 break; 554 } 555 break; 556 } 557 558 /* Drop if payload is empty */ 559 if (m->m_pkthdr.len == 0) { 560 error = EINVAL; 561 goto drop; 562 } 563 564 /* Send it on */ 565 if (pep->hook == NULL) 566 goto drop; 567 NG_FWD_NEW_DATA(error, item, pep->hook, m); 568 return (error); 569 570 drop: 571 NG_FREE_ITEM(item); 572 return (error); 573 } 574 575 /* 576 * Send keepalive packets, every 10 seconds. 577 */ 578 static void 579 cisco_keepalive(node_p node, hook_p hook, void *arg1, int arg2) 580 { 581 const sc_p sc = arg1; 582 583 cisco_send(sc, CISCO_KEEPALIVE_REQ, sc->local_seq, sc->remote_seq); 584 if (sc->seqRetries++ > 1) 585 cisco_notify(sc, NGM_LINK_IS_DOWN); 586 ng_callout(&sc->handle, node, NULL, (hz * KEEPALIVE_SECS), 587 &cisco_keepalive, (void *)sc, 0); 588 } 589 590 /* 591 * Send Cisco keepalive packet. 592 */ 593 static int 594 cisco_send(sc_p sc, int type, long par1, long par2) 595 { 596 struct cisco_header *h; 597 struct cisco_packet *ch; 598 struct mbuf *m; 599 struct timeval time; 600 uint32_t t; 601 int error = 0; 602 603 getmicrouptime(&time); 604 605 MGETHDR(m, M_NOWAIT, MT_DATA); 606 if (!m) 607 return (ENOBUFS); 608 609 t = time.tv_sec * 1000 + time.tv_usec / 1000; 610 m->m_pkthdr.len = m->m_len = CISCO_HEADER_LEN + CISCO_PACKET_LEN; 611 m->m_pkthdr.rcvif = 0; 612 613 h = mtod(m, struct cisco_header *); 614 h->address = CISCO_MULTICAST; 615 h->control = 0; 616 h->protocol = htons(CISCO_KEEPALIVE); 617 618 ch = (struct cisco_packet *) (h + 1); 619 ch->type = htonl(type); 620 ch->par1 = htonl(par1); 621 ch->par2 = htonl(par2); 622 ch->rel = -1; 623 ch->time0 = htons((uint16_t) (t >> 16)); 624 ch->time1 = htons((uint16_t) t); 625 626 NG_SEND_DATA_ONLY(error, sc->downstream.hook, m); 627 return (error); 628 } 629 630 /* 631 * Send linkstate to upstream node. 632 */ 633 static void 634 cisco_notify(sc_p sc, uint32_t cmd) 635 { 636 struct ng_mesg *msg; 637 int dummy_error = 0; 638 639 if (sc->inet.hook == NULL) /* nothing to notify */ 640 return; 641 642 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT); 643 if (msg != NULL) 644 NG_SEND_MSG_HOOK(dummy_error, sc->node, msg, sc->inet.hook, 0); 645 } 646