2 * Layer Two Tunnelling Protocol Daemon
3 * Copyright (C) 1998 Adtran, Inc.
4 * Copyright (C) 2002 Jeff McAdams
8 * This software is distributed under the terms
9 * of the GPL, which you should have received
10 * along with this source.
12 * Network routines for UDP handling
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
24 #include <sys/ioctl.h>
29 #include "ipsecmast.h"
30 #include "misc.h" /* for IPADDY macro */
33 struct sockaddr_in server
, from
; /* Server and transmitter structs */
34 int server_socket
; /* Server socket */
36 int kernel_support
; /* Kernel Support there or not? */
40 int init_network (void)
43 unsigned int length
= sizeof (server
);
44 gethostname (hostname
, sizeof (hostname
));
45 server
.sin_family
= AF_INET
;
46 server
.sin_addr
.s_addr
= gconfig
.listenaddr
;
47 server
.sin_port
= htons (gconfig
.port
);
48 if ((server_socket
= socket (PF_INET
, SOCK_DGRAM
, 0)) < 0)
50 l2tp_log (LOG_CRIT
, "%s: Unable to allocate socket. Terminating.\n",
55 if (bind (server_socket
, (struct sockaddr
*) &server
, sizeof (server
)))
57 close (server_socket
);
58 l2tp_log (LOG_CRIT
, "%s: Unable to bind socket: %s. Terminating.\n",
59 __FUNCTION__
, strerror(errno
), errno
);
62 if (getsockname (server_socket
, (struct sockaddr
*) &server
, &length
))
64 l2tp_log (LOG_CRIT
, "%s: Unable to read socket name.Terminating.\n",
71 * For L2TP/IPsec with KLIPSng, set the socket to receive IPsec REFINFO
75 if(setsockopt(server_socket
, IPPROTO_IP
, IP_IPSEC_REFINFO
,
76 &arg
, sizeof(arg
)) != 0) {
77 l2tp_log(LOG_CRIT
, "setsockopt recvref[%d]: %s\n", IP_IPSEC_REFINFO
, strerror(errno
));
82 l2tp_log(LOG_INFO
, "No attempt being made to use IPsec SAref's since we're not on a Linux machine.\n");
87 if (gconfig
.forceuserspace
)
89 l2tp_log (LOG_INFO
, "Not looking for kernel support.\n");
94 int kernel_fd
= socket(AF_PPPOX
, SOCK_DGRAM
, PX_PROTO_OL2TP
);
97 l2tp_log (LOG_INFO
, "L2TP kernel support not detected.\n");
103 l2tp_log (LOG_INFO
, "Using l2tp kernel support.\n");
108 l2tp_log (LOG_INFO
, "This binary does not support kernel L2TP.\n");
110 arg
= fcntl (server_socket
, F_GETFL
);
112 fcntl (server_socket
, F_SETFL
, arg
);
113 gconfig
.port
= ntohs (server
.sin_port
);
117 inline void extract (void *buf
, int *tunnel
, int *call
)
120 * Extract the tunnel and call #'s, and fix the order of the
124 struct payload_hdr
*p
= (struct payload_hdr
*) buf
;
137 inline void fix_hdr (void *buf
)
140 * Fix the byte order of the header
143 struct payload_hdr
*p
= (struct payload_hdr
*) buf
;
144 _u16 ver
= ntohs (p
->ver
);
148 * Control headers are always
149 * exactly 12 bytes big.
166 void dethrottle (void *call
)
168 /* struct call *c = (struct call *)call; */
169 /* if (c->throttle) {
171 log(LOG_DEBUG, "%s: dethrottling call %d, and setting R-bit\n",__FUNCTION__,c->ourcid);
172 #endif c->rbit = RBIT;
175 log(LOG_DEBUG, "%s: call %d already dethrottled?\n",__FUNCTION__,c->ourcid);
179 void control_xmit (void *b
)
181 struct buffer
*buf
= (struct buffer
*) b
;
188 l2tp_log (LOG_WARNING
, "%s: called on NULL buffer!\n", __FUNCTION__
);
193 #ifdef DEBUG_CONTROL_XMIT
196 "trying to send control packet to %d\n",
202 ns
= ntohs (((struct control_hdr
*) (buf
->start
))->Ns
);
207 #ifdef DEBUG_CONTROL_XMIT
208 l2tp_log (LOG_DEBUG
, "%s: Tossing packet %d\n", __FUNCTION__
, ns
);
210 /* Okay, it's been received. Let's toss it now */
215 if (buf
->retries
> DEFAULT_MAX_RETRIES
)
218 * Too many retries. Either kill the tunnel, or
219 * if there is no tunnel, just stop retransmitting.
223 if (t
->self
->needclose
)
226 "Unable to deliver closing message for tunnel %d. Destroying anyway.\n",
228 t
->self
->needclose
= 0;
229 t
->self
->closing
= -1;
233 l2tp_log (LOG_NOTICE
,
234 "Maximum retries exceeded for tunnel %d. Closing.\n",
236 strcpy (t
->self
->errormsg
, "Timeout");
237 t
->self
->needclose
= -1;
246 * FIXME: How about adaptive timeouts?
250 schedule (tv
, control_xmit
, buf
);
251 #ifdef DEBUG_CONTROL_XMIT
252 l2tp_log (LOG_DEBUG
, "%s: Scheduling and transmitting packet %d\n",
259 void udp_xmit (struct buffer
*buf
, struct tunnel
*t
)
261 struct cmsghdr
*cmsg
;
262 char cbuf
[CMSG_SPACE(sizeof (unsigned int))];
269 * OKAY, now send a packet with the right SAref values.
271 memset(&msgh
, 0, sizeof(struct msghdr
));
273 msgh
.msg_control
= cbuf
;
274 msgh
.msg_controllen
= 0;
276 if(gconfig
.ipsecsaref
&& t
->refhim
!= IPSEC_SAREF_NULL
) {
277 msgh
.msg_controllen
= sizeof(cbuf
);
279 cmsg
= CMSG_FIRSTHDR(&msgh
);
280 cmsg
->cmsg_level
= IPPROTO_IP
;
281 cmsg
->cmsg_type
= IP_IPSEC_REFINFO
;
282 cmsg
->cmsg_len
= CMSG_LEN(sizeof(unsigned int));
284 if(gconfig
.debug_network
) {
285 l2tp_log(LOG_DEBUG
,"sending with saref=%d\n", t
->refhim
);
287 refp
= (unsigned int *)CMSG_DATA(cmsg
);
290 msgh
.msg_controllen
= cmsg
->cmsg_len
;
293 iov
.iov_base
= buf
->start
;
294 iov
.iov_len
= buf
->len
;
296 /* return packet from whence it came */
297 msgh
.msg_name
= &buf
->peer
;
298 msgh
.msg_namelen
= sizeof(buf
->peer
);
305 /* Receive one packet. */
306 if ((err
= sendmsg(server_socket
, &msgh
, 0)) < 0) {
307 l2tp_log(LOG_ERR
, "udp_xmit failed to %s:%d with err=%d:%s\n",
308 IPADDY(t
->peer
.sin_addr
), ntohs(t
->peer
.sin_port
),
309 err
,strerror(errno
));
313 int build_fdset (fd_set
*readfds
)
324 call
= tun
->call_head
;
327 if (call
->needclose
^ call
->closing
)
330 call
= tun
->call_head
;
337 if (!call
->needclose
&& !call
->closing
)
341 FD_SET (call
->fd
, readfds
);
346 /* Now that call fds have been collected, and checked for
347 * closing, check if the tunnel needs to be closed too
349 if (tun
->self
->needclose
^ tun
->self
->closing
)
351 if (gconfig
.debug_tunnel
)
352 l2tp_log (LOG_DEBUG
, "%s: closing down tunnel %d\n",
353 __FUNCTION__
, tun
->ourtid
);
354 call_close (tun
->self
);
355 /* Reset the while loop
356 * and check for NULL */
364 FD_SET (server_socket
, readfds
);
365 if (server_socket
> max
)
367 FD_SET (control_fd
, readfds
);
368 if (control_fd
> max
)
373 void network_thread ()
376 * We loop forever waiting on either data from the ppp drivers or from
377 * our network socket. Control handling is no longer done here.
379 struct sockaddr_in from
, to
;
380 unsigned int fromlen
, tolen
;
381 int tunnel
, call
; /* Tunnel and call */
382 int recvsize
; /* Length of data received */
383 struct buffer
*buf
; /* Payload buffer */
384 struct call
*c
, *sc
; /* Call to send this off to */
385 struct tunnel
*st
; /* Tunnel */
386 fd_set readfds
; /* Descriptors to watch for reading */
387 int max
; /* Highest fd */
388 struct timeval tv
, *ptv
; /* Timeout for select */
392 unsigned int refme
, refhim
;
394 /* This one buffer can be recycled for everything except control packets */
395 buf
= new_buf (MAX_RECV_SIZE
);
404 max
= build_fdset (&readfds
);
405 ptv
= process_schedule(&tv
);
406 ret
= select (max
+ 1, &readfds
, NULL
, NULL
, ptv
);
411 if (gconfig
.debug_network
)
413 l2tp_log (LOG_DEBUG
, "%s: select timeout\n", __FUNCTION__
);
418 if (gconfig
.debug_network
)
421 "%s: select returned error %d (%s)\n",
422 __FUNCTION__
, errno
, strerror (errno
));
427 if (FD_ISSET (control_fd
, &readfds
))
431 if (FD_ISSET (server_socket
, &readfds
))
434 * Okay, now we're ready for reading and processing new data.
438 /* Reserve space for expanding payload packet headers */
439 buf
->start
+= PAYLOAD_BUF
;
440 buf
->len
-= PAYLOAD_BUF
;
442 memset(&from
, 0, sizeof(from
));
443 memset(&to
, 0, sizeof(to
));
445 fromlen
= sizeof(from
);
448 memset(&msgh
, 0, sizeof(struct msghdr
));
449 iov
.iov_base
= buf
->start
;
450 iov
.iov_len
= buf
->len
;
451 msgh
.msg_control
= cbuf
;
452 msgh
.msg_controllen
= sizeof(cbuf
);
453 msgh
.msg_name
= &from
;
454 msgh
.msg_namelen
= fromlen
;
459 /* Receive one packet. */
460 recvsize
= recvmsg(server_socket
, &msgh
, 0);
462 if (recvsize
< MIN_PAYLOAD_HDR_LEN
)
467 l2tp_log (LOG_WARNING
,
468 "%s: recvfrom returned error %d (%s)\n",
469 __FUNCTION__
, errno
, strerror (errno
));
473 l2tp_log (LOG_WARNING
, "%s: received too small a packet\n",
482 /* extract IPsec info out */
483 if(gconfig
.ipsecsaref
) {
484 struct cmsghdr
*cmsg
;
485 /* Process auxiliary received data in msgh */
486 for (cmsg
= CMSG_FIRSTHDR(&msgh
);
488 cmsg
= CMSG_NXTHDR(&msgh
,cmsg
)) {
489 if (cmsg
->cmsg_level
== IPPROTO_IP
490 && cmsg
->cmsg_type
== IP_IPSEC_REFINFO
) {
493 refp
= (unsigned int *)CMSG_DATA(cmsg
);
501 * some logic could be added here to verify that we only
502 * get L2TP packets inside of IPsec, or to provide different
503 * classes of service to packets not inside of IPsec.
506 fix_hdr (buf
->start
);
507 extract (buf
->start
, &tunnel
, &call
);
509 if (gconfig
.debug_network
)
511 l2tp_log(LOG_DEBUG
, "%s: recv packet from %s, size = %d, "
512 "tunnel = %d, call = %d ref=%u refhim=%u\n",
513 __FUNCTION__
, inet_ntoa (from
.sin_addr
),
514 recvsize
, tunnel
, call
, refme
, refhim
);
517 if (gconfig
.packet_dump
)
519 do_packet_dump (buf
);
522 (c
= get_call (tunnel
, call
, from
.sin_addr
,
523 from
.sin_port
, refme
, refhim
)))
526 get_tunnel (tunnel
, from
.sin_addr
.s_addr
,
530 * It is theoretically possible that we could be sent
531 * a control message (say a StopCCN) on a call that we
532 * have already closed or some such nonsense. To
533 * prevent this from closing the tunnel, if we get a
534 * call on a valid tunnel, but not with a valid CID,
535 * we'll just send a ZLB to ack receiving the packet.
537 if (gconfig
.debug_tunnel
)
539 "%s: no such call %d on tunnel %d. Sending special ZLB\n",
541 handle_special (buf
, c
, call
);
543 /* get a new buffer */
544 buf
= new_buf (MAX_RECV_SIZE
);
548 "%s: unable to find call or tunnel to handle packet. call = %d, tunnel = %d Dumping.\n",
549 __FUNCTION__
, call
, tunnel
);
555 /* Handle the packet */
556 c
->container
->chal_us
.vector
= NULL
;
557 if (handle_packet (buf
, c
->container
, c
))
559 if (gconfig
.debug_tunnel
)
560 l2tp_log (LOG_DEBUG
, "%s: bad packet\n", __FUNCTION__
);
564 /* Send Zero Byte Packet */
565 control_zlb (buf
, c
->container
, c
);
572 * finished obvious sources, look for data from PPP connections.
580 if ((sc
->fd
>= 0) && FD_ISSET (sc
->fd
, &readfds
))
582 /* Got some payload to send */
584 recycle_payload (buf
, sc
->container
->peer
);
586 #ifdef DEBUG_FLOW_MORE
587 l2tp_log (LOG_DEBUG, "%s: rws = %d, pSs = %d, pLr = %d\n",
588 __FUNCTION__, sc->rws, sc->pSs, sc->pLr);
590 if ((sc->rws>0) && (sc->pSs > sc->pLr + sc->rws) && !sc->rbit) {
592 log(LOG_DEBUG, "%s: throttling payload (call = %d, tunnel = %d, Lr = %d, Ss = %d, rws = %d)!\n",__FUNCTION__,
593 sc->cid, sc->container->tid, sc->pLr, sc->pSs, sc->rws);
596 We unthrottle in handle_packet if we get a payload packet,
597 valid or ZLB, but we also schedule a dethrottle in which
598 case the R-bit will be set
599 FIXME: Rate Adaptive timeout?
602 sc->dethrottle = schedule(tv, dethrottle, sc);
604 /* while ((result=read_packet(buf,sc->fd,sc->frame & SYNC_FRAMING))>0) { */
606 read_packet (buf
, sc
->fd
, SYNC_FRAMING
)) > 0)
608 add_payload_hdr (sc
->container
, sc
, buf
);
609 if (gconfig
.packet_dump
)
611 do_packet_dump (buf
);
615 sc
->prx
= sc
->data_rec_seq_num
;
618 deschedule (sc
->zlb_xmit
);
621 sc
->tx_bytes
+= buf
->len
;
624 recycle_payload (buf
, sc
->container
->peer
);
628 l2tp_log (LOG_WARNING
,
629 "%s: tossing read packet, error = %s (%d). Closing call.\n",
630 __FUNCTION__
, strerror (-result
), -result
);
631 strcpy (sc
->errormsg
, strerror (-result
));