Théophile Bastian
0b8cf0edc1
Those packets are probably packets truncated because the buffer is not large enough.
198 lines
5.6 KiB
C++
198 lines
5.6 KiB
C++
#include "UdpVpn.hpp"
|
|
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
#include <poll.h>
|
|
#include <errno.h>
|
|
|
|
#include "ip_header.hpp"
|
|
|
|
static const size_t VPN_MTU = 1460; // TODO determine this -- issue #3
|
|
|
|
UdpVpn::UdpVpn()
|
|
: _stopped(false), _vpn_mtu(VPN_MTU), _tun_dev("cvpn%d"), _peer(nullptr)
|
|
{
|
|
_tun_dev.set_mtu(VpnPacket::get_tunnelled_mtu(_vpn_mtu));
|
|
_socket = socket(AF_INET6, SOCK_DGRAM, 0);
|
|
if(_socket < 0)
|
|
throw UdpVpn::InitializationError("Cannot create socket", errno, true);
|
|
}
|
|
|
|
UdpVpn::~UdpVpn() {
|
|
close(_socket);
|
|
}
|
|
|
|
void UdpVpn::run() {
|
|
int rc;
|
|
int start_at_fd = 0; // read from polled fds in round-robin fashion
|
|
int cur_fd;
|
|
int nfds = 2;
|
|
struct pollfd poll_fds[2];
|
|
|
|
// poll_fds[0]: tun device
|
|
poll_fds[0].fd = _tun_dev.get_fd();
|
|
poll_fds[0].events = POLLIN;
|
|
|
|
// poll_fds[1]: UDP socket device
|
|
poll_fds[1].fd = _socket;
|
|
poll_fds[1].events = POLLIN;
|
|
|
|
while(!_stopped) {
|
|
rc = poll(poll_fds, nfds, -1);
|
|
|
|
if(rc < 0) {
|
|
if(errno == EINTR) // Interrupt.
|
|
continue;
|
|
throw UdpVpn::NetError(
|
|
"Error polling from interface", errno, true);
|
|
}
|
|
else if(rc == 0) // Nothing to read
|
|
continue;
|
|
|
|
cur_fd = start_at_fd;
|
|
do {
|
|
if(poll_fds[cur_fd].revents & POLLIN) {
|
|
if(cur_fd == 0)
|
|
receive_from_tun();
|
|
else if(cur_fd == 1)
|
|
receive_from_udp();
|
|
break;
|
|
}
|
|
|
|
cur_fd = (cur_fd + 1) % nfds;
|
|
} while(cur_fd != start_at_fd);
|
|
|
|
start_at_fd = (start_at_fd + 1) % nfds;
|
|
}
|
|
}
|
|
|
|
size_t UdpVpn::read_from_tun(char* buffer, size_t len) {
|
|
// We know that there is data available -- use `read()`
|
|
return _tun_dev.read(buffer, len);
|
|
}
|
|
|
|
size_t UdpVpn::read_from_tun(TunnelledPacket& packet) {
|
|
size_t payload_space = packet.get_payload_space();
|
|
size_t nread = read_from_tun(packet.get_payload(), payload_space);
|
|
packet.set_payload_size(nread);
|
|
if(!packet.parse_as_ipv6()) {
|
|
debugf("Ignoring packet with invalid header\n");
|
|
return 0;
|
|
}
|
|
if(nread != packet.get_ipv6_header().packet_length()) {
|
|
debugf("Ignoring packet with bad size (expected %d, got %d, buffer %d)\n",
|
|
packet.get_ipv6_header().packet_length(), nread, payload_space);
|
|
return 0;
|
|
}
|
|
return nread;
|
|
}
|
|
|
|
size_t UdpVpn::read_from_udp(char* buffer, size_t len,
|
|
sockaddr_in6& peer_addr)
|
|
{
|
|
ssize_t nread;
|
|
socklen_t peer_addr_len = sizeof(peer_addr);
|
|
nread = recvfrom(_socket, buffer, len, 0,
|
|
(struct sockaddr*) &peer_addr, &peer_addr_len);
|
|
|
|
if(nread < 0)
|
|
throw UdpVpn::NetError("Cannot receive datagram", errno, true);
|
|
if(nread == 0)
|
|
return 0;
|
|
|
|
if(peer_addr.sin6_family != AF_INET6) {
|
|
debugf("WARNING: Received non-ipv6 family datagram %d. Ignoring.\n",
|
|
peer_addr.sin6_family);
|
|
return 0;
|
|
}
|
|
if(peer_addr_len != sizeof(peer_addr)) {
|
|
debugf("WARNING: received unexpected source address length %u."
|
|
"Ignoring.\n",
|
|
peer_addr_len);
|
|
return 0;
|
|
}
|
|
|
|
return nread;
|
|
}
|
|
|
|
size_t UdpVpn::read_from_udp(VpnPacket& packet, sockaddr_in6& peer_addr) {
|
|
packet.upon_reception(); // The packet is not read yet, but it has arrived
|
|
size_t nread =
|
|
read_from_udp(packet.get_data(), packet.get_data_space(), peer_addr);
|
|
packet.set_data_size(nread);
|
|
return nread;
|
|
}
|
|
|
|
size_t UdpVpn::transmit_to_peer(VpnPacket& packet) {
|
|
if(!_peer) {
|
|
debugf("Dropping packet: no peer yet.\n");
|
|
return 0;
|
|
}
|
|
return _peer->write(packet);
|
|
}
|
|
|
|
void UdpVpn::receive_from_tun() {
|
|
VpnPacket packet(_vpn_mtu);
|
|
TunnelledPacket tunnelled = TunnelledPacket::create(packet);
|
|
size_t nread = read_from_tun(tunnelled);
|
|
if(nread == 0)
|
|
return;
|
|
|
|
if(!_peer) {
|
|
debugf("Dropping packet: no peer yet.\n");
|
|
return;
|
|
}
|
|
packet.set_peer(_peer.get());
|
|
|
|
kdebugf("Transmitting %s -> %s, size %d\n",
|
|
format_address(tunnelled.get_ipv6_header().source.s6_addr),
|
|
format_address(tunnelled.get_ipv6_header().dest.s6_addr),
|
|
nread);
|
|
|
|
packet.prepare_for_sending();
|
|
transmit_to_peer(packet);
|
|
}
|
|
|
|
void UdpVpn::receive_from_udp() {
|
|
VpnPacket packet(_vpn_mtu);
|
|
sockaddr_in6 peer_ext_addr;
|
|
size_t nread = read_from_udp(packet, peer_ext_addr);
|
|
if(nread == 0)
|
|
return;
|
|
|
|
// If we don't have a peer yet -- we're just setting the peer to nullptr.
|
|
packet.set_peer(_peer.get());
|
|
|
|
for(VpnPacketTLV tlv=packet.first_tlv();
|
|
!tlv.past_the_end();
|
|
tlv.seek_next_tlv())
|
|
{
|
|
switch(tlv.get_type()) {
|
|
case VpnPacket::PAYLOAD_TYPE_TUNNELLED:
|
|
{
|
|
TunnelledPacket tunnelled(tlv);
|
|
acquire_peer(tunnelled, peer_ext_addr);
|
|
receive_tunnelled_tlv(tunnelled);
|
|
}
|
|
break;
|
|
|
|
case VpnPacket::PAYLOAD_TYPE_UNDEF:
|
|
default:
|
|
debugf("#%d+%lu: ignoring TLV with bad type %d.\n",
|
|
packet.get_seqno(), tlv.get_offset(),
|
|
tlv.get_type());
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void UdpVpn::receive_tunnelled_tlv(TunnelledPacket& packet) {
|
|
// Reinject into tun
|
|
kdebugf("Reinjecting tunnelled packet of size %d\n",
|
|
packet.get_payload_size());
|
|
_tun_dev.write(packet.get_payload(), packet.get_payload_size());
|
|
}
|