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