ng_cisco.c revision 53913
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 53913 1999-11-30 02:45:32Z archie $ 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#include <netatalk/at_var.h> 63#include <netatalk/at_extern.h> 64 65#include <netipx/ipx.h> 66#include <netipx/ipx_if.h> 67 68#include <netgraph/ng_message.h> 69#include <netgraph/netgraph.h> 70#include <netgraph/ng_parse.h> 71#include <netgraph/ng_cisco.h> 72 73#define CISCO_MULTICAST 0x8f /* Cisco multicast address */ 74#define CISCO_UNICAST 0x0f /* Cisco unicast address */ 75#define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */ 76#define CISCO_ADDR_REQ 0 /* Cisco address request */ 77#define CISCO_ADDR_REPLY 1 /* Cisco address reply */ 78#define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */ 79 80#define KEEPALIVE_SECS 10 81 82struct cisco_header { 83 u_char address; 84 u_char control; 85 u_short protocol; 86}; 87 88#define CISCO_HEADER_LEN sizeof (struct cisco_header) 89 90struct cisco_packet { 91 u_long type; 92 u_long par1; 93 u_long par2; 94 u_short rel; 95 u_short time0; 96 u_short time1; 97}; 98 99#define CISCO_PACKET_LEN (sizeof(struct cisco_packet)) 100 101struct protoent { 102 hook_p hook; /* the hook for this proto */ 103 u_short af; /* address family, -1 = downstream */ 104}; 105 106struct cisco_priv { 107 u_long local_seq; 108 u_long remote_seq; 109 u_long seqRetries; /* how many times we've been here throwing out 110 * the same sequence number without ack */ 111 node_p node; 112 struct callout_handle handle; 113 struct protoent downstream; 114 struct protoent inet; /* IP information */ 115 struct in_addr localip; 116 struct in_addr localmask; 117 struct protoent atalk; /* AppleTalk information */ 118 struct protoent ipx; /* IPX information */ 119}; 120typedef struct cisco_priv *sc_p; 121 122/* Netgraph methods */ 123static ng_constructor_t cisco_constructor; 124static ng_rcvmsg_t cisco_rcvmsg; 125static ng_shutdown_t cisco_rmnode; 126static ng_newhook_t cisco_newhook; 127static ng_rcvdata_t cisco_rcvdata; 128static ng_disconnect_t cisco_disconnect; 129 130/* Other functions */ 131static int cisco_input(sc_p sc, struct mbuf *m, meta_p meta); 132static void cisco_keepalive(void *arg); 133static int cisco_send(sc_p sc, int type, long par1, long par2); 134 135/* Parse type for struct ng_cisco_ipaddr */ 136static const struct ng_parse_struct_info 137 ng_cisco_ipaddr_type_info = NG_CISCO_IPADDR_TYPE_INFO; 138static const struct ng_parse_type ng_cisco_ipaddr_type = { 139 &ng_parse_struct_type, 140 &ng_cisco_ipaddr_type_info 141}; 142 143/* Parse type for struct ng_async_stat */ 144static const struct ng_parse_struct_info 145 ng_cisco_stats_type_info = NG_CISCO_STATS_TYPE_INFO; 146static const struct ng_parse_type ng_cisco_stats_type = { 147 &ng_parse_struct_type, 148 &ng_cisco_stats_type_info, 149}; 150 151/* List of commands and how to convert arguments to/from ASCII */ 152static const struct ng_cmdlist ng_cisco_cmdlist[] = { 153 { 154 NGM_CISCO_COOKIE, 155 NGM_CISCO_SET_IPADDR, 156 "setipaddr", 157 &ng_cisco_ipaddr_type, 158 NULL 159 }, 160 { 161 NGM_CISCO_COOKIE, 162 NGM_CISCO_GET_IPADDR, 163 "getipaddr", 164 NULL, 165 &ng_cisco_ipaddr_type 166 }, 167 { 168 NGM_CISCO_COOKIE, 169 NGM_CISCO_GET_STATUS, 170 "getstats", 171 NULL, 172 &ng_cisco_stats_type 173 }, 174 { 0 } 175}; 176 177/* Node type */ 178static struct ng_type typestruct = { 179 NG_VERSION, 180 NG_CISCO_NODE_TYPE, 181 NULL, 182 cisco_constructor, 183 cisco_rcvmsg, 184 cisco_rmnode, 185 cisco_newhook, 186 NULL, 187 NULL, 188 cisco_rcvdata, 189 cisco_rcvdata, 190 cisco_disconnect, 191 ng_cisco_cmdlist 192}; 193NETGRAPH_INIT(cisco, &typestruct); 194 195/* 196 * Node constructor 197 */ 198static int 199cisco_constructor(node_p *nodep) 200{ 201 sc_p sc; 202 int error = 0; 203 204 MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_WAITOK); 205 if (sc == NULL) 206 return (ENOMEM); 207 bzero(sc, sizeof(struct cisco_priv)); 208 209 callout_handle_init(&sc->handle); 210 if ((error = ng_make_node_common(&typestruct, nodep))) { 211 FREE(sc, M_NETGRAPH); 212 return (error); 213 } 214 (*nodep)->private = sc; 215 sc->node = *nodep; 216 217 /* Initialise the varous protocol hook holders */ 218 sc->downstream.af = 0xffff; 219 sc->inet.af = AF_INET; 220 sc->atalk.af = AF_APPLETALK; 221 sc->ipx.af = AF_IPX; 222 return (0); 223} 224 225/* 226 * Check new hook 227 */ 228static int 229cisco_newhook(node_p node, hook_p hook, const char *name) 230{ 231 const sc_p sc = node->private; 232 233 if (strcmp(name, NG_CISCO_HOOK_DOWNSTREAM) == 0) { 234 sc->downstream.hook = hook; 235 hook->private = &sc->downstream; 236 237 /* Start keepalives */ 238 sc->handle = timeout(cisco_keepalive, sc, hz * KEEPALIVE_SECS); 239 } else if (strcmp(name, NG_CISCO_HOOK_INET) == 0) { 240 sc->inet.hook = hook; 241 hook->private = &sc->inet; 242 } else if (strcmp(name, NG_CISCO_HOOK_APPLETALK) == 0) { 243 sc->atalk.hook = hook; 244 hook->private = &sc->atalk; 245 } else if (strcmp(name, NG_CISCO_HOOK_IPX) == 0) { 246 sc->ipx.hook = hook; 247 hook->private = &sc->ipx; 248 } else if (strcmp(name, NG_CISCO_HOOK_DEBUG) == 0) { 249 hook->private = NULL; /* unimplemented */ 250 } else 251 return (EINVAL); 252 return 0; 253} 254 255/* 256 * Receive control message. 257 */ 258static int 259cisco_rcvmsg(node_p node, struct ng_mesg *msg, 260 const char *retaddr, struct ng_mesg **rptr) 261{ 262 const sc_p sc = node->private; 263 struct ng_mesg *resp = NULL; 264 int error = 0; 265 266 switch (msg->header.typecookie) { 267 case NGM_GENERIC_COOKIE: 268 switch (msg->header.cmd) { 269 case NGM_TEXT_STATUS: 270 { 271 char *arg; 272 int pos; 273 274 NG_MKRESPONSE(resp, msg, sizeof(struct ng_mesg) 275 + NG_TEXTRESPONSE, M_NOWAIT); 276 if (resp == NULL) { 277 error = ENOMEM; 278 break; 279 } 280 arg = (char *) resp->data; 281 pos = sprintf(arg, 282 "keepalive period: %d sec; ", KEEPALIVE_SECS); 283 pos += sprintf(arg + pos, 284 "unacknowledged keepalives: %ld", sc->seqRetries); 285 resp->header.arglen = pos + 1; 286 break; 287 } 288 default: 289 error = EINVAL; 290 break; 291 } 292 break; 293 case NGM_CISCO_COOKIE: 294 switch (msg->header.cmd) { 295 case NGM_CISCO_GET_IPADDR: /* could be a late reply! */ 296 if ((msg->header.flags & NGF_RESP) == 0) { 297 struct in_addr *ips; 298 299 NG_MKRESPONSE(resp, msg, 300 2 * sizeof(*ips), M_NOWAIT); 301 if (!resp) { 302 error = ENOMEM; 303 break; 304 } 305 ips = (struct in_addr *) resp->data; 306 ips[0] = sc->localip; 307 ips[1] = sc->localmask; 308 break; 309 } 310 /* FALLTHROUGH */ /* ...if it's a reply */ 311 case NGM_CISCO_SET_IPADDR: 312 { 313 struct in_addr *const ips = (struct in_addr *)msg->data; 314 315 if (msg->header.arglen < 2 * sizeof(*ips)) { 316 error = EINVAL; 317 break; 318 } 319 sc->localip = ips[0]; 320 sc->localmask = ips[1]; 321 break; 322 } 323 case NGM_CISCO_GET_STATUS: 324 { 325 struct ng_cisco_stats *stat; 326 327 NG_MKRESPONSE(resp, msg, sizeof(*stat), M_NOWAIT); 328 if (!resp) { 329 error = ENOMEM; 330 break; 331 } 332 stat = (struct ng_cisco_stats *)resp->data; 333 stat->seqRetries = sc->seqRetries; 334 stat->keepAlivePeriod = KEEPALIVE_SECS; 335 break; 336 } 337 default: 338 error = EINVAL; 339 break; 340 } 341 break; 342 default: 343 error = EINVAL; 344 break; 345 } 346 if (rptr) 347 *rptr = resp; 348 else if (resp) 349 FREE(resp, M_NETGRAPH); 350 FREE(msg, M_NETGRAPH); 351 return (error); 352} 353 354/* 355 * Receive data 356 */ 357static int 358cisco_rcvdata(hook_p hook, struct mbuf *m, meta_p meta) 359{ 360 const sc_p sc = hook->node->private; 361 struct protoent *pep; 362 struct cisco_header *h; 363 int error = 0; 364 365 if ((pep = hook->private) == NULL) 366 goto out; 367 368 /* If it came from our downlink, deal with it separately */ 369 if (pep->af == 0xffff) 370 return (cisco_input(sc, m, meta)); 371 372 /* OK so it came from a protocol, heading out. Prepend general data 373 packet header. For now, IP,IPX only */ 374 M_PREPEND(m, CISCO_HEADER_LEN, M_DONTWAIT); 375 if (!m) { 376 error = ENOBUFS; 377 goto out; 378 } 379 h = mtod(m, struct cisco_header *); 380 h->address = CISCO_MULTICAST; /* broadcast address */ 381 h->control = 0; 382 383 switch (pep->af) { 384 case AF_INET: /* Internet Protocol */ 385 h->protocol = htons(ETHERTYPE_IP); 386 break; 387 case AF_APPLETALK: /* AppleTalk Protocol */ 388 h->protocol = htons(ETHERTYPE_AT); 389 break; 390 case AF_IPX: /* Novell IPX Protocol */ 391 h->protocol = htons(ETHERTYPE_IPX); 392 break; 393 default: 394 error = EAFNOSUPPORT; 395 goto out; 396 } 397 398 /* Send it */ 399 NG_SEND_DATA(error, sc->downstream.hook, m, meta); 400 return (error); 401 402out: 403 NG_FREE_DATA(m, meta); 404 return (error); 405} 406 407/* 408 * Shutdown node 409 */ 410static int 411cisco_rmnode(node_p node) 412{ 413 const sc_p sc = node->private; 414 415 node->flags |= NG_INVALID; 416 ng_cutlinks(node); 417 ng_unname(node); 418 node->private = NULL; 419 ng_unref(sc->node); 420 FREE(sc, M_NETGRAPH); 421 return (0); 422} 423 424/* 425 * Disconnection of a hook 426 * 427 * For this type, removal of the last link destroys the node 428 */ 429static int 430cisco_disconnect(hook_p hook) 431{ 432 const sc_p sc = hook->node->private; 433 struct protoent *pep; 434 435 /* Check it's not the debug hook */ 436 if ((pep = hook->private)) { 437 pep->hook = NULL; 438 if (pep->af == 0xffff) { 439 /* If it is the downstream hook, stop the timers */ 440 untimeout(cisco_keepalive, sc, sc->handle); 441 } 442 } 443 444 /* If no more hooks, remove the node */ 445 if (hook->node->numhooks == 0) 446 ng_rmnode(hook->node); 447 return (0); 448} 449 450/* 451 * Receive data 452 */ 453static int 454cisco_input(sc_p sc, struct mbuf *m, meta_p meta) 455{ 456 struct cisco_header *h; 457 struct cisco_packet *p; 458 struct protoent *pep; 459 int error = 0; 460 461 if (m->m_pkthdr.len <= CISCO_HEADER_LEN) 462 goto drop; 463 464 /* Strip off cisco header */ 465 h = mtod(m, struct cisco_header *); 466 m_adj(m, CISCO_HEADER_LEN); 467 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 p = mtod(m, struct cisco_packet *); 479 switch (ntohl(p->type)) { 480 default: 481 log(LOG_WARNING, 482 "cisco: unknown cisco packet type: 0x%lx\n", 483 ntohl(p->type)); 484 break; 485 case CISCO_ADDR_REPLY: 486 /* Reply on address request, ignore */ 487 break; 488 case CISCO_KEEPALIVE_REQ: 489 sc->remote_seq = ntohl(p->par1); 490 if (sc->local_seq == ntohl(p->par2)) { 491 sc->local_seq++; 492 sc->seqRetries = 0; 493 } 494 break; 495 case CISCO_ADDR_REQ: 496 { 497 struct ng_mesg *msg, *resp; 498 499 /* Ask inet peer for IP address information */ 500 if (sc->inet.hook == NULL) 501 goto nomsg; 502 NG_MKMESSAGE(msg, NGM_CISCO_COOKIE, 503 NGM_CISCO_GET_IPADDR, 0, M_NOWAIT); 504 if (msg == NULL) 505 goto nomsg; 506 ng_send_msg(sc->node, msg, 507 NG_CISCO_HOOK_INET, &resp); 508 if (resp != NULL) 509 cisco_rcvmsg(sc->node, resp, ".", 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