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