ng_cisco.c revision 59728
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: head/sys/netgraph/ng_cisco.c 59728 2000-04-28 17:09:00Z julian $ 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 80struct 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 88struct 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 99struct protoent { 100 hook_p hook; /* the hook for this proto */ 101 u_short af; /* address family, -1 = downstream */ 102}; 103 104struct 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}; 118typedef struct cisco_priv *sc_p; 119 120/* Netgraph methods */ 121static ng_constructor_t cisco_constructor; 122static ng_rcvmsg_t cisco_rcvmsg; 123static ng_shutdown_t cisco_rmnode; 124static ng_newhook_t cisco_newhook; 125static ng_rcvdata_t cisco_rcvdata; 126static ng_disconnect_t cisco_disconnect; 127 128/* Other functions */ 129static int cisco_input(sc_p sc, struct mbuf *m, meta_p meta); 130static void cisco_keepalive(void *arg); 131static int cisco_send(sc_p sc, int type, long par1, long par2); 132 133/* Parse type for struct ng_cisco_ipaddr */ 134static const struct ng_parse_struct_info 135 ng_cisco_ipaddr_type_info = NG_CISCO_IPADDR_TYPE_INFO; 136static 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 */ 142static const struct ng_parse_struct_info 143 ng_cisco_stats_type_info = NG_CISCO_STATS_TYPE_INFO; 144static 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 */ 150static 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 */ 176static 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}; 191NETGRAPH_INIT(cisco, &typestruct); 192 193/* 194 * Node constructor 195 */ 196static int 197cisco_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 */ 226static int 227cisco_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 */ 256static int 257cisco_rcvmsg(node_p node, struct ng_mesg *msg, 258 const char *retaddr, struct ng_mesg **rptr, hook_p lasthook) 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 */ 355static int 356cisco_rcvdata(hook_p hook, struct mbuf *m, meta_p meta, 357 struct mbuf **ret_m, meta_p *ret_meta) 358{ 359 const sc_p sc = hook->node->private; 360 struct protoent *pep; 361 struct cisco_header *h; 362 int error = 0; 363 364 if ((pep = hook->private) == NULL) 365 goto out; 366 367 /* If it came from our downlink, deal with it separately */ 368 if (pep->af == 0xffff) 369 return (cisco_input(sc, m, meta)); 370 371 /* OK so it came from a protocol, heading out. Prepend general data 372 packet header. For now, IP,IPX only */ 373 M_PREPEND(m, CISCO_HEADER_LEN, M_DONTWAIT); 374 if (!m) { 375 error = ENOBUFS; 376 goto out; 377 } 378 h = mtod(m, struct cisco_header *); 379 h->address = CISCO_UNICAST; 380 h->control = 0; 381 382 switch (pep->af) { 383 case AF_INET: /* Internet Protocol */ 384 h->protocol = htons(ETHERTYPE_IP); 385 break; 386 case AF_APPLETALK: /* AppleTalk Protocol */ 387 h->protocol = htons(ETHERTYPE_AT); 388 break; 389 case AF_IPX: /* Novell IPX Protocol */ 390 h->protocol = htons(ETHERTYPE_IPX); 391 break; 392 default: 393 error = EAFNOSUPPORT; 394 goto out; 395 } 396 397 /* Send it */ 398 NG_SEND_DATA(error, sc->downstream.hook, m, meta); 399 return (error); 400 401out: 402 NG_FREE_DATA(m, meta); 403 return (error); 404} 405 406/* 407 * Shutdown node 408 */ 409static int 410cisco_rmnode(node_p node) 411{ 412 const sc_p sc = node->private; 413 414 node->flags |= NG_INVALID; 415 ng_cutlinks(node); 416 ng_unname(node); 417 node->private = NULL; 418 ng_unref(sc->node); 419 FREE(sc, M_NETGRAPH); 420 return (0); 421} 422 423/* 424 * Disconnection of a hook 425 * 426 * For this type, removal of the last link destroys the node 427 */ 428static int 429cisco_disconnect(hook_p hook) 430{ 431 const sc_p sc = hook->node->private; 432 struct protoent *pep; 433 434 /* Check it's not the debug hook */ 435 if ((pep = hook->private)) { 436 pep->hook = NULL; 437 if (pep->af == 0xffff) { 438 /* If it is the downstream hook, stop the timers */ 439 untimeout(cisco_keepalive, sc, sc->handle); 440 } 441 } 442 443 /* If no more hooks, remove the node */ 444 if (hook->node->numhooks == 0) 445 ng_rmnode(hook->node); 446 return (0); 447} 448 449/* 450 * Receive data 451 */ 452static int 453cisco_input(sc_p sc, struct mbuf *m, meta_p meta) 454{ 455 struct cisco_header *h; 456 struct cisco_packet *p; 457 struct protoent *pep; 458 int error = 0; 459 460 if (m->m_pkthdr.len <= CISCO_HEADER_LEN) 461 goto drop; 462 463 /* Strip off cisco header */ 464 h = mtod(m, struct cisco_header *); 465 m_adj(m, CISCO_HEADER_LEN); 466 467 switch (h->address) { 468 default: /* Invalid Cisco packet. */ 469 goto drop; 470 case CISCO_UNICAST: 471 case CISCO_MULTICAST: 472 /* Don't check the control field here (RFC 1547). */ 473 switch (ntohs(h->protocol)) { 474 default: 475 goto drop; 476 case CISCO_KEEPALIVE: 477 p = mtod(m, struct cisco_packet *); 478 switch (ntohl(p->type)) { 479 default: 480 log(LOG_WARNING, 481 "cisco: unknown cisco packet type: 0x%lx\n", 482 ntohl(p->type)); 483 break; 484 case CISCO_ADDR_REPLY: 485 /* Reply on address request, ignore */ 486 break; 487 case CISCO_KEEPALIVE_REQ: 488 sc->remote_seq = ntohl(p->par1); 489 if (sc->local_seq == ntohl(p->par2)) { 490 sc->local_seq++; 491 sc->seqRetries = 0; 492 } 493 break; 494 case CISCO_ADDR_REQ: 495 { 496 struct ng_mesg *msg, *resp; 497 498 /* Ask inet peer for IP address information */ 499 if (sc->inet.hook == NULL) 500 goto nomsg; 501 NG_MKMESSAGE(msg, NGM_CISCO_COOKIE, 502 NGM_CISCO_GET_IPADDR, 0, M_NOWAIT); 503 if (msg == NULL) 504 goto nomsg; 505 ng_send_msg(sc->node, msg, 506 NG_CISCO_HOOK_INET, &resp); 507 if (resp != NULL) 508 cisco_rcvmsg(sc->node, resp, ".", 509 NULL, NULL); 510 511 nomsg: 512 /* Send reply to peer device */ 513 error = cisco_send(sc, CISCO_ADDR_REPLY, 514 ntohl(sc->localip.s_addr), 515 ntohl(sc->localmask.s_addr)); 516 break; 517 } 518 } 519 goto drop; 520 case ETHERTYPE_IP: 521 pep = &sc->inet; 522 break; 523 case ETHERTYPE_AT: 524 pep = &sc->atalk; 525 break; 526 case ETHERTYPE_IPX: 527 pep = &sc->ipx; 528 break; 529 } 530 break; 531 } 532 533 /* Send it on */ 534 if (pep->hook == NULL) 535 goto drop; 536 NG_SEND_DATA(error, pep->hook, m, meta); 537 return (error); 538 539drop: 540 NG_FREE_DATA(m, meta); 541 return (error); 542} 543 544 545/* 546 * Send keepalive packets, every 10 seconds. 547 */ 548static void 549cisco_keepalive(void *arg) 550{ 551 const sc_p sc = arg; 552 int s = splimp(); 553 554 cisco_send(sc, CISCO_KEEPALIVE_REQ, sc->local_seq, sc->remote_seq); 555 sc->seqRetries++; 556 splx(s); 557 sc->handle = timeout(cisco_keepalive, sc, hz * KEEPALIVE_SECS); 558} 559 560/* 561 * Send Cisco keepalive packet. 562 */ 563static int 564cisco_send(sc_p sc, int type, long par1, long par2) 565{ 566 struct cisco_header *h; 567 struct cisco_packet *ch; 568 struct mbuf *m; 569 u_long t; 570 int error = 0; 571 meta_p meta = NULL; 572 struct timeval time; 573 574 getmicrotime(&time); 575 576 MGETHDR(m, M_DONTWAIT, MT_DATA); 577 if (!m) 578 return (ENOBUFS); 579 580 t = (time.tv_sec - boottime.tv_sec) * 1000; 581 m->m_pkthdr.len = m->m_len = CISCO_HEADER_LEN + CISCO_PACKET_LEN; 582 m->m_pkthdr.rcvif = 0; 583 584 h = mtod(m, struct cisco_header *); 585 h->address = CISCO_MULTICAST; 586 h->control = 0; 587 h->protocol = htons(CISCO_KEEPALIVE); 588 589 ch = (struct cisco_packet *) (h + 1); 590 ch->type = htonl(type); 591 ch->par1 = htonl(par1); 592 ch->par2 = htonl(par2); 593 ch->rel = -1; 594 ch->time0 = htons((u_short) (t >> 16)); 595 ch->time1 = htons((u_short) t); 596 597 NG_SEND_DATA(error, sc->downstream.hook, m, meta); 598 return (error); 599} 600