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@whistle.com> 38 * 39 * $FreeBSD$ 40 * $Whistle: ng_cisco.c,v 1.25 1999/11/01 09:24:51 julian Exp $ 41 */ 42 43 #include "opt_inet.h" 44 #include "opt_atalk.h" 45 #include "opt_ipx.h" 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/errno.h> 50 #include <sys/kernel.h> 51 #include <sys/socket.h> 52 #include <sys/malloc.h> 53 #include <sys/mbuf.h> 54 #include <sys/syslog.h> 55 56 #include <net/if.h> 57 58 #include <netinet/in.h> 59 #include <netinet/if_ether.h> 60 61 #include <netatalk/at.h> 62 63 #include <netipx/ipx.h> 64 #include <netipx/ipx_if.h> 65 66 #include <netgraph/ng_message.h> 67 #include <netgraph/netgraph.h> 68 #include <netgraph/ng_parse.h> 69 #include <netgraph/ng_cisco.h> 70 71 #define CISCO_MULTICAST 0x8f /* Cisco multicast address */ 72 #define CISCO_UNICAST 0x0f /* Cisco unicast address */ 73 #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */ 74 #define CISCO_ADDR_REQ 0 /* Cisco address request */ 75 #define CISCO_ADDR_REPLY 1 /* Cisco address reply */ 76 #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */ 77 78 #define KEEPALIVE_SECS 10 79 80 struct cisco_header { 81 u_char address; 82 u_char control; 83 u_short protocol; 84 }; 85 86 #define CISCO_HEADER_LEN sizeof (struct cisco_header) 87 88 struct cisco_packet { 89 u_long type; 90 u_long par1; 91 u_long par2; 92 u_short rel; 93 u_short time0; 94 u_short time1; 95 }; 96 97 #define CISCO_PACKET_LEN (sizeof(struct cisco_packet)) 98 99 struct protoent { 100 hook_p hook; /* the hook for this proto */ 101 u_short af; /* address family, -1 = downstream */ 102 }; 103 104 struct cisco_priv { 105 u_long local_seq; 106 u_long remote_seq; 107 u_long seqRetries; /* how many times we've been here throwing out 108 * the same sequence number without ack */ 109 node_p node; 110 struct callout_handle handle; 111 struct protoent downstream; 112 struct protoent inet; /* IP information */ 113 struct in_addr localip; 114 struct in_addr localmask; 115 struct protoent atalk; /* AppleTalk information */ 116 struct protoent ipx; /* IPX information */ 117 }; 118 typedef struct cisco_priv *sc_p; 119 120 /* Netgraph methods */ 121 static ng_constructor_t cisco_constructor; 122 static ng_rcvmsg_t cisco_rcvmsg; 123 static ng_shutdown_t cisco_rmnode; 124 static ng_newhook_t cisco_newhook; 125 static ng_rcvdata_t cisco_rcvdata; 126 static ng_disconnect_t cisco_disconnect; 127 128 /* Other functions */ 129 static int cisco_input(sc_p sc, struct mbuf *m, meta_p meta); 130 static void cisco_keepalive(void *arg); 131 static int cisco_send(sc_p sc, int type, long par1, long par2); 132 133 /* Parse type for struct ng_cisco_ipaddr */ 134 static const struct ng_parse_struct_info 135 ng_cisco_ipaddr_type_info = NG_CISCO_IPADDR_TYPE_INFO; 136 static const struct ng_parse_type ng_cisco_ipaddr_type = { 137 &ng_parse_struct_type, 138 &ng_cisco_ipaddr_type_info 139 }; 140 141 /* Parse type for struct ng_async_stat */ 142 static const struct ng_parse_struct_info 143 ng_cisco_stats_type_info = NG_CISCO_STATS_TYPE_INFO; 144 static const struct ng_parse_type ng_cisco_stats_type = { 145 &ng_parse_struct_type, 146 &ng_cisco_stats_type_info, 147 }; 148 149 /* List of commands and how to convert arguments to/from ASCII */ 150 static const struct ng_cmdlist ng_cisco_cmdlist[] = { 151 { 152 NGM_CISCO_COOKIE, 153 NGM_CISCO_SET_IPADDR, 154 "setipaddr", 155 &ng_cisco_ipaddr_type, 156 NULL 157 }, 158 { 159 NGM_CISCO_COOKIE, 160 NGM_CISCO_GET_IPADDR, 161 "getipaddr", 162 NULL, 163 &ng_cisco_ipaddr_type 164 }, 165 { 166 NGM_CISCO_COOKIE, 167 NGM_CISCO_GET_STATUS, 168 "getstats", 169 NULL, 170 &ng_cisco_stats_type 171 }, 172 { 0 } 173 }; 174 175 /* Node type */ 176 static struct ng_type typestruct = { 177 NG_VERSION, 178 NG_CISCO_NODE_TYPE, 179 NULL, 180 cisco_constructor, 181 cisco_rcvmsg, 182 cisco_rmnode, 183 cisco_newhook, 184 NULL, 185 NULL, 186 cisco_rcvdata, 187 cisco_rcvdata, 188 cisco_disconnect, 189 ng_cisco_cmdlist 190 }; 191 NETGRAPH_INIT(cisco, &typestruct); 192 193 /* 194 * Node constructor 195 */ 196 static int 197 cisco_constructor(node_p *nodep) 198 { 199 sc_p sc; 200 int error = 0; 201 202 MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_WAITOK); 203 if (sc == NULL) 204 return (ENOMEM); 205 bzero(sc, sizeof(struct cisco_priv)); 206 207 callout_handle_init(&sc->handle); 208 if ((error = ng_make_node_common(&typestruct, nodep))) { 209 FREE(sc, M_NETGRAPH); 210 return (error); 211 } 212 (*nodep)->private = sc; 213 sc->node = *nodep; 214 215 /* Initialise the varous protocol hook holders */ 216 sc->downstream.af = 0xffff; 217 sc->inet.af = AF_INET; 218 sc->atalk.af = AF_APPLETALK; 219 sc->ipx.af = AF_IPX; 220 return (0); 221 } 222 223 /* 224 * Check new hook 225 */ 226 static int 227 cisco_newhook(node_p node, hook_p hook, const char *name) 228 { 229 const sc_p sc = node->private; 230 231 if (strcmp(name, NG_CISCO_HOOK_DOWNSTREAM) == 0) { 232 sc->downstream.hook = hook; 233 hook->private = &sc->downstream; 234 235 /* Start keepalives */ 236 sc->handle = timeout(cisco_keepalive, sc, hz * KEEPALIVE_SECS); 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, M_DONTWAIT); 373 if (!m) { 374 error = ENOBUFS; 375 goto out; 376 } 377 h = mtod(m, struct cisco_header *); 378 h->address = CISCO_MULTICAST; /* broadcast address */ 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_APPLETALK: /* AppleTalk Protocol */ 386 h->protocol = htons(ETHERTYPE_AT); 387 break; 388 case AF_IPX: /* Novell IPX Protocol */ 389 h->protocol = htons(ETHERTYPE_IPX); 390 break; 391 default: 392 error = EAFNOSUPPORT; 393 goto out; 394 } 395 396 /* Send it */ 397 NG_SEND_DATA(error, sc->downstream.hook, m, meta); 398 return (error); 399 400 out: 401 NG_FREE_DATA(m, meta); 402 return (error); 403 } 404 405 /* 406 * Shutdown node 407 */ 408 static int 409 cisco_rmnode(node_p node) 410 { 411 const sc_p sc = node->private; 412 413 node->flags |= NG_INVALID; 414 ng_cutlinks(node); 415 ng_unname(node); 416 node->private = NULL; 417 ng_unref(sc->node); 418 FREE(sc, M_NETGRAPH); 419 return (0); 420 } 421 422 /* 423 * Disconnection of a hook 424 * 425 * For this type, removal of the last link destroys the node 426 */ 427 static int 428 cisco_disconnect(hook_p hook) 429 { 430 const sc_p sc = hook->node->private; 431 struct protoent *pep; 432 433 /* Check it's not the debug hook */ 434 if ((pep = hook->private)) { 435 pep->hook = NULL; 436 if (pep->af == 0xffff) { 437 /* If it is the downstream hook, stop the timers */ 438 untimeout(cisco_keepalive, sc, sc->handle); 439 } 440 } 441 442 /* If no more hooks, remove the node */ 443 if (hook->node->numhooks == 0) 444 ng_rmnode(hook->node); 445 return (0); 446 } 447 448 /* 449 * Receive data 450 */ 451 static int 452 cisco_input(sc_p sc, struct mbuf *m, meta_p meta) 453 { 454 struct cisco_header *h; 455 struct cisco_packet *p; 456 struct protoent *pep; 457 int error = 0; 458 459 if (m->m_pkthdr.len <= CISCO_HEADER_LEN) 460 goto drop; 461 462 /* Strip off cisco header */ 463 h = mtod(m, struct cisco_header *); 464 m_adj(m, CISCO_HEADER_LEN); 465 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 p = mtod(m, struct cisco_packet *); 477 switch (ntohl(p->type)) { 478 default: 479 log(LOG_WARNING, 480 "cisco: unknown cisco packet type: 0x%lx\n", 481 ntohl(p->type)); 482 break; 483 case CISCO_ADDR_REPLY: 484 /* Reply on address request, ignore */ 485 break; 486 case CISCO_KEEPALIVE_REQ: 487 sc->remote_seq = ntohl(p->par1); 488 if (sc->local_seq == ntohl(p->par2)) { 489 sc->local_seq++; 490 sc->seqRetries = 0; 491 } 492 break; 493 case CISCO_ADDR_REQ: 494 { 495 struct ng_mesg *msg, *resp; 496 497 /* Ask inet peer for IP address information */ 498 if (sc->inet.hook == NULL) 499 goto nomsg; 500 NG_MKMESSAGE(msg, NGM_CISCO_COOKIE, 501 NGM_CISCO_GET_IPADDR, 0, M_NOWAIT); 502 if (msg == NULL) 503 goto nomsg; 504 ng_send_msg(sc->node, msg, 505 NG_CISCO_HOOK_INET, &resp); 506 if (resp != NULL) 507 cisco_rcvmsg(sc->node, resp, ".", NULL); 508 509 nomsg: 510 /* Send reply to peer device */ 511 error = cisco_send(sc, CISCO_ADDR_REPLY, 512 ntohl(sc->localip.s_addr), 513 ntohl(sc->localmask.s_addr)); 514 break; 515 } 516 } 517 goto drop; 518 case ETHERTYPE_IP: 519 pep = &sc->inet; 520 break; 521 case ETHERTYPE_AT: 522 pep = &sc->atalk; 523 break; 524 case ETHERTYPE_IPX: 525 pep = &sc->ipx; 526 break; 527 } 528 break; 529 } 530 531 /* Send it on */ 532 if (pep->hook == NULL) 533 goto drop; 534 NG_SEND_DATA(error, pep->hook, m, meta); 535 return (error); 536 537 drop: 538 NG_FREE_DATA(m, meta); 539 return (error); 540 } 541 542 543 /* 544 * Send keepalive packets, every 10 seconds. 545 */ 546 static void 547 cisco_keepalive(void *arg) 548 { 549 const sc_p sc = arg; 550 int s = splimp(); 551 552 cisco_send(sc, CISCO_KEEPALIVE_REQ, sc->local_seq, sc->remote_seq); 553 sc->seqRetries++; 554 splx(s); 555 sc->handle = timeout(cisco_keepalive, sc, hz * KEEPALIVE_SECS); 556 } 557 558 /* 559 * Send Cisco keepalive packet. 560 */ 561 static int 562 cisco_send(sc_p sc, int type, long par1, long par2) 563 { 564 struct cisco_header *h; 565 struct cisco_packet *ch; 566 struct mbuf *m; 567 u_long t; 568 int error = 0; 569 meta_p meta = NULL; 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(error, sc->downstream.hook, m, meta); 596 return (error); 597 } 598