1/* 2 * Layer Two Tunnelling Protocol Daemon 3 * Copyright (C) 1998 Adtran, Inc. 4 * Copyright (C) 2002 Jeff McAdams 5 * 6 * Mark Spencer 7 * 8 * This software is distributed under the terms 9 * of the GPL, which you should have received 10 * along with this source. 11 * 12 * Network routines for UDP handling 13 */ 14#include <stdio.h> 15#include <errno.h> 16#include <string.h> 17#include <sys/socket.h> 18#include <netinet/in.h> 19#include <arpa/inet.h> 20#include <netdb.h> 21#include <fcntl.h> 22#include <unistd.h> 23#include <stdlib.h> 24#include <sys/ioctl.h> 25#if (__GLIBC__ < 2) 26# if defined(FREEBSD) 27# include <sys/signal.h> 28# elif defined(LINUX) 29# include <bsd/signal.h> 30# elif defined(SOLARIS) 31# include <signal.h> 32# endif 33#else 34# include <signal.h> 35#endif 36#include "l2tp.h" 37 38char hostname[256]; 39unsigned int listen_addy = INADDR_ANY; /* Address to listen on */ 40struct sockaddr_in server, from; /* Server and transmitter structs */ 41int server_socket; /* Server socket */ 42#ifdef USE_KERNEL 43int kernel_support; /* Kernel Support there or not? */ 44#endif 45 46/* 47 * Debugging info 48 */ 49int debug_tunnel = 0; 50int debug_network = 0; /* Debug networking? */ 51int packet_dump = 0; /* Dump packets? */ 52int debug_avp = 1; /* Debug AVP negotiations? */ 53int debug_state = 0; /* Debug state machine? */ 54 55int init_network (void) 56{ 57 long arg; 58 int length = sizeof (server); 59 gethostname (hostname, sizeof (hostname)); 60 server.sin_family = AF_INET; 61 server.sin_addr.s_addr = htonl (listen_addy); 62 server.sin_port = htons (gconfig.port); 63 if ((server_socket = socket (PF_INET, SOCK_DGRAM, 0)) < 0) 64 { 65 log (LOG_CRIT, "%s: Unable to allocate socket. Terminating.\n", 66 __FUNCTION__); 67 return -EINVAL; 68 }; 69 /* L2TP/IPSec: Set up SA for listening port here? NTB 20011015 70 */ 71 if (bind (server_socket, (struct sockaddr *) &server, sizeof (server))) 72 { 73 close (server_socket); 74 log (LOG_CRIT, "%s: Unable to bind socket. Terminating.\n", 75 __FUNCTION__); 76 return -EINVAL; 77 }; 78 if (getsockname (server_socket, (struct sockaddr *) &server, &length)) 79 { 80 log (LOG_CRIT, "%s: Unable to read socket name.Terminating.\n", 81 __FUNCTION__); 82 return -EINVAL; 83 } 84#ifdef USE_KERNEL 85 if (gconfig.forceuserspace) 86 { 87 log (LOG_LOG, "Not looking for kernel support.\n"); 88 kernel_support = 0; 89 } 90 else 91 { 92 if (ioctl (server_socket, SIOCSETL2TP, NULL) < 0) 93 { 94 log (LOG_LOG, "L2TP kernel support not detected.\n"); 95 kernel_support = 0; 96 } 97 else 98 { 99 log (LOG_LOG, "Using l2tp kernel support.\n"); 100 kernel_support = -1; 101 } 102 } 103#else 104 log (LOG_LOG, "This binary does not support kernel L2TP.\n"); 105#endif 106 arg = fcntl (server_socket, F_GETFL); 107 arg |= O_NONBLOCK; 108 fcntl (server_socket, F_SETFL, arg); 109 gconfig.port = ntohs (server.sin_port); 110 return 0; 111} 112 113inline void extract (void *buf, int *tunnel, int *call) 114{ 115 /* 116 * Extract the tunnel and call #'s, and fix the order of the 117 * version 118 */ 119 120 struct payload_hdr *p = (struct payload_hdr *) buf; 121 if (PLBIT (p->ver)) 122 { 123 *tunnel = p->tid; 124 *call = p->cid; 125 } 126 else 127 { 128 *tunnel = p->length; 129 *call = p->tid; 130 } 131} 132 133inline void fix_hdr (void *buf) 134{ 135 /* 136 * Fix the byte order of the header 137 */ 138 139 struct payload_hdr *p = (struct payload_hdr *) buf; 140 _u16 ver = ntohs (p->ver); 141 if (CTBIT (p->ver)) 142 { 143 /* 144 * Control headers are always 145 * exactly 12 bytes big. 146 */ 147 swaps (buf, 12); 148 } 149 else 150 { 151 int len = 6; 152 if (PSBIT (ver)) 153 len += 4; 154 if (PLBIT (ver)) 155 len += 2; 156 if (PFBIT (ver)) 157 len += 4; 158 swaps (buf, len); 159 } 160} 161 162void dethrottle (void *call) 163{ 164/* struct call *c = (struct call *)call; */ 165/* if (c->throttle) { 166#ifdef DEBUG_FLOW 167 log(LOG_DEBUG, "%s: dethrottling call %d, and setting R-bit\n",__FUNCTION__,c->ourcid); 168#endif c->rbit = RBIT; 169 c->throttle = 0; 170 } else { 171 log(LOG_DEBUG, "%s: call %d already dethrottled?\n",__FUNCTION__,c->ourcid); 172 } */ 173} 174 175void control_xmit (void *b) 176{ 177 struct buffer *buf = (struct buffer *) b; 178 struct tunnel *t; 179 struct timeval tv; 180 int ns; 181 if (!buf) 182 { 183 log (LOG_WARN, "%s: called on NULL buffer!\n", __FUNCTION__); 184 return; 185 } 186 187 buf->retries++; 188 t = buf->tunnel; 189 ns = ntohs (((struct control_hdr *) (buf->start))->Ns); 190 if (t) 191 { 192 if (ns < t->cLr) 193 { 194#ifdef DEBUG_CONTROL_XMIT 195 log (LOG_DEBUG, "%s: Tossing packet %d\n", __FUNCTION__, ns); 196#endif 197 /* Okay, it's been received. Let's toss it now */ 198 toss (buf); 199 return; 200 } 201 } 202 if (buf->retries > DEFAULT_MAX_RETRIES) 203 { 204 /* 205 * Too many retries. Either kill the tunnel, or 206 * if there is no tunnel, just stop retransmitting. 207 */ 208 if (t) 209 { 210 if (t->self->needclose) 211 { 212 log (LOG_DEBUG, 213 "%s: Unable to deliver closing message for tunnel %d. Destroying anyway.\n", 214 __FUNCTION__, t->ourtid); 215 t->self->needclose = 0; 216 t->self->closing = -1; 217 /* Foxconn, added by MJ., for terminate program when time out. */ 218 extern void death_handler (int signal); 219 death_handler(SIGTERM); 220 /* add end, by MJ.*/ 221 } 222 else 223 { 224 log (LOG_DEBUG, 225 "%s: Maximum retries exceeded for tunnel %d. Closing.\n", 226 __FUNCTION__, t->ourtid); 227 strcpy (t->self->errormsg, "Timeout"); 228 t->self->needclose = -1; 229 } 230 } 231 } 232 else 233 { 234 /* 235 * FIXME: How about adaptive timeouts? 236 */ 237 tv.tv_sec = 1; 238 tv.tv_usec = 0; 239 schedule (tv, control_xmit, buf); 240#ifdef DEBUG_CONTROL_XMIT 241 log (LOG_DEBUG, "%s: Scheduling and transmitting packet %d\n", 242 __FUNCTION__, ns); 243#endif 244 udp_xmit (buf); 245 } 246} 247 248void udp_xmit (struct buffer *buf) 249{ 250 /* 251 * Just send it all out. 252 */ 253#if 0 254 struct sockaddr_in to; 255 to.sin_family = AF_INET; 256 to.sin_port = buf->port; 257 /* if (buf->retry>-1) buf->retry++; */ 258 bcopy (&buf->addr, &to.sin_addr, sizeof (buf->addr)); 259#endif 260 sendto (server_socket, buf->start, buf->len, 0, 261 (struct sockaddr *) &buf->peer, sizeof (buf->peer)); 262 263} 264 265void network_thread () 266{ 267 /* 268 * We loop forever waiting on either data from the ppp drivers or from 269 * our network socket. Control handling is no longer done here. 270 */ 271 int fromlen; /* Length of the address */ 272 int tunnel, call; /* Tunnel and call */ 273 int recvsize; /* Length of data received */ 274 struct buffer *buf; /* Payload buffer */ 275 struct call *c, *sc; /* Call to send this off to */ 276 struct tunnel *st; /* Tunnel */ 277 fd_set readfds; /* Descriptors to watch for reading */ 278 int max; /* Highest fd */ 279 struct timeval tv; /* Timeout for select */ 280 /* This one buffer can be recycled for everything except control packets */ 281 buf = new_buf (MAX_RECV_SIZE); 282 for (;;) 283 { 284 /* 285 * First, let's send out any outgoing packets that are waiting on us. 286 * xmit_udp should only 287 * contain control packets in the unthreaded version! 288 */ 289 max = 0; 290 FD_ZERO (&readfds); 291 st = tunnels.head; 292 while (st) 293 { 294 if (st->self->needclose ^ st->self->closing) 295 { 296 if (debug_tunnel) 297 log (LOG_DEBUG, "%S: closing down tunnel %d\n", 298 __FUNCTION__, st->ourtid); 299 call_close (st->self); 300 /* Reset the while loop 301 and check for NULL */ 302 st = tunnels.head; 303 if (!st) 304 break; 305 continue; 306 } 307 sc = st->call_head; 308 while (sc) 309 { 310 if (sc->needclose ^ sc->closing) 311 { 312 call_close (sc); 313 sc = st->call_head; 314 if (!sc) 315 break; 316 continue; 317 } 318 if (sc->fd > -1) 319 { 320/* if (!sc->throttle && !sc->needclose && !sc->closing) { */ 321 if (!sc->needclose && !sc->closing) 322 { 323 if (sc->fd > max) 324 max = sc->fd; 325 FD_SET (sc->fd, &readfds); 326 } 327 } 328 sc = sc->next; 329 } 330 st = st->next; 331 } 332 FD_SET (server_socket, &readfds); 333 if (server_socket > max) 334 max = server_socket; 335 FD_SET (control_fd, &readfds); 336 if (control_fd > max) 337 max = control_fd; 338 tv.tv_sec = 1; 339 tv.tv_usec = 0; 340 341 /*add start, by MJ.*/ 342 extern int is_first_run; 343 if(is_first_run) 344 { 345 int lac_fp; /* to get conn_id which written by acos */ 346 char cmd[64]={0}; 347 char conn_id[64] = "c default"; 348 349 lac_fp = fopen("/tmp/l2tp/l2tpd.info", "r"); 350 351 if (lac_fp != NULL){ 352 //fscanf(lac_fp, "%s", conn_id); 353 fgets(conn_id, sizeof(conn_id), lac_fp); 354 fclose(lac_fp); 355 } 356 else 357 log (LOG_DEBUG, "open /tmp/l2tp/l2tpd.info fialed\n"); 358 359 log (LOG_DEBUG, "%s: -> the first run.\n", __FUNCTION__); 360 361 sprintf(cmd, "c %s", conn_id); 362 363 //do_control("c MJ."); 364 do_control(cmd); 365 //write(control_fd, cmd, strlen(cmd) ); 366 is_first_run = 0; 367 } 368 /*add end. by MJ.*/ 369 370 schedule_unlock (); 371 select (max + 1, &readfds, NULL, NULL, NULL); 372 schedule_lock (); 373 374 if (FD_ISSET (control_fd, &readfds)) 375 { 376 do_control (NULL); 377 } 378 if (FD_ISSET (server_socket, &readfds)) 379 { 380 /* foxconn wklin added start, 04/12/2011 */ 381 extern void connect_pppunit(void); 382 connect_pppunit(); 383 /* foxconn wklin added end, 04/12/2011 */ 384 /* 385 * Okay, now we're ready for reading and processing new data. 386 */ 387 recycle_buf (buf); 388 /* Reserve space for expanding payload packet headers */ 389 buf->start += PAYLOAD_BUF; 390 buf->len -= PAYLOAD_BUF; 391 fromlen = sizeof (from); 392 recvsize = 393 recvfrom (server_socket, buf->start, buf->len, 0, 394 (struct sockaddr *) &from, &fromlen); 395 396 /* Foxconn, by MJ. for debugging.*/ 397 //log (LOG_DEBUG, "receive %d bytes from server_scoket.\n", recvsize); 398 399 400 if (recvsize < MIN_PAYLOAD_HDR_LEN) 401 { 402 if (recvsize < 0) 403 { 404 if (errno != EAGAIN) 405 log (LOG_WARN, 406 "%s: recvfrom returned error %d (%s)\n", 407 __FUNCTION__, errno, strerror (errno)); 408 } 409 else 410 { 411 log (LOG_WARN, "%s: received too small a packet\n", 412 __FUNCTION__); 413 } 414 } 415 else 416 { 417 buf->len = recvsize; 418 fix_hdr (buf->start); 419 extract (buf->start, &tunnel, &call); 420 if (debug_network) 421 { 422 log (LOG_DEBUG, "%s: recv packet from %s, size = %d," 423"tunnel = %d, call = %d\n", __FUNCTION__, inet_ntoa (from.sin_addr), recvsize, tunnel, call); 424 } 425 if (packet_dump) 426 { 427 do_packet_dump (buf); 428 } 429 if (! 430 (c = 431 get_call (tunnel, call, from.sin_addr.s_addr, 432 from.sin_port))) 433 { 434 if ((c = 435 get_tunnel (tunnel, from.sin_addr.s_addr, 436 from.sin_port))) 437 { 438 /* 439 * It is theoretically possible that we could be sent 440 * a control message (say a StopCCN) on a call that we 441 * have already closed or some such nonsense. To prevent 442 * this from closing the tunnel, if we get a call on a valid 443 * tunnel, but not with a valid CID, we'll just send a ZLB 444 * to ack receiving the packet. 445 */ 446 if (debug_tunnel) 447 log (LOG_DEBUG, 448 "%s: no such call %d on tunnel %d. Sending special ZLB\n", 449 __FUNCTION__); 450 handle_special (buf, c, call); 451 } 452 else 453 log (LOG_DEBUG, 454 "%s: unable to find call or tunnel to handle packet. call = %d, tunnel = %d Dumping.\n", 455 __FUNCTION__, call, tunnel); 456 457 } 458 else 459 { 460 buf->peer = from; 461 /* Handle the packet */ 462 c->container->chal_us.vector = NULL; 463 if (handle_packet (buf, c->container, c)) 464 { 465 if (debug_tunnel) 466 log (LOG_DEBUG, "%s: bad packet\n", __FUNCTION__); 467 }; 468 if (c->cnu) 469 { 470 /* Send Zero Byte Packet */ 471 control_zlb (buf, c->container, c); 472 c->cnu = 0; 473 474 475 476 } 477 } 478 } 479 }; 480 481 st = tunnels.head; 482 while (st) 483 { 484 sc = st->call_head; 485 while (sc) 486 { 487 if ((sc->fd >= 0) && FD_ISSET (sc->fd, &readfds)) 488 { 489 /* Got some payload to send */ 490 int result; 491 recycle_payload (buf, sc->container->peer); 492#ifdef DEBUG_FLOW_MORE 493 log (LOG_DEBUG, "%s: rws = %d, pSs = %d, pLr = %d\n", 494 __FUNCTION__, sc->rws, sc->pSs, sc->pLr); 495#endif 496/* if ((sc->rws>0) && (sc->pSs > sc->pLr + sc->rws) && !sc->rbit) { 497#ifdef DEBUG_FLOW 498 log(LOG_DEBUG, "%s: throttling payload (call = %d, tunnel = %d, Lr = %d, Ss = %d, rws = %d)!\n",__FUNCTION__, 499 sc->cid, sc->container->tid, sc->pLr, sc->pSs, sc->rws); 500#endif 501 sc->throttle = -1; 502 We unthrottle in handle_packet if we get a payload packet, 503 valid or ZLB, but we also schedule a dethrottle in which 504 case the R-bit will be set 505 FIXME: Rate Adaptive timeout? 506 tv.tv_sec = 2; 507 tv.tv_usec = 0; 508 sc->dethrottle = schedule(tv, dethrottle, sc); 509 } else */ 510/* while ((result=read_packet(buf,sc->fd,sc->frame & SYNC_FRAMING))>0) { */ 511 while ((result = 512 read_packet (buf, sc->fd, SYNC_FRAMING)) > 0) 513 { 514 add_payload_hdr (sc->container, sc, buf); 515 if (packet_dump) 516 { 517 do_packet_dump (buf); 518 } 519 520 521 sc->prx = sc->data_rec_seq_num; 522 if (sc->zlb_xmit) 523 { 524 deschedule (sc->zlb_xmit); 525 sc->zlb_xmit = NULL; 526 } 527 sc->tx_bytes += buf->len; 528 sc->tx_pkts++; 529 udp_xmit (buf); 530 recycle_payload (buf, sc->container->peer); 531 } 532 if (result != 0) 533 { 534 log (LOG_WARN, 535 "%s: tossing read packet, error = %s (%d). Closing call.\n", 536 __FUNCTION__, strerror (-result), -result); 537 strcpy (sc->errormsg, strerror (-result)); 538 sc->needclose = -1; 539 } 540 } 541 sc = sc->next; 542 } 543 st = st->next; 544 } 545 } 546 547} 548