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