|
|
@ -16,14 +16,16 @@ |
|
|
|
* |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
#ifdef __linux__ |
|
|
|
|
|
|
|
|
|
|
|
#include "n2n.h" |
|
|
|
|
|
|
|
/* ********************************** */ |
|
|
|
|
|
|
|
static int setup_ifname (int fd, const char *ifname, const char *ipaddr, |
|
|
|
const char *netmask, uint8_t *mac, int mtu) { |
|
|
|
|
|
|
|
struct ifreq ifr; |
|
|
|
|
|
|
|
memset(&ifr, 0, sizeof(ifr)); |
|
|
@ -36,51 +38,50 @@ static int setup_ifname(int fd, const char *ifname, const char *ipaddr, |
|
|
|
|
|
|
|
if(ioctl(fd, SIOCSIFHWADDR, &ifr) == -1) { |
|
|
|
traceEvent(TRACE_ERROR, "ioctl(SIOCSIFHWADDR) failed [%d]: %s", errno, strerror(errno)); |
|
|
|
return(-1); |
|
|
|
return -1; |
|
|
|
} |
|
|
|
|
|
|
|
ifr.ifr_addr.sa_family = AF_INET; |
|
|
|
|
|
|
|
/* Interface Address */ |
|
|
|
// interface address
|
|
|
|
inet_pton(AF_INET, ipaddr, &((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr); |
|
|
|
if(ioctl(fd, SIOCSIFADDR, &ifr) == -1) { |
|
|
|
traceEvent(TRACE_ERROR, "ioctl(SIOCSIFADDR) failed [%d]: %s", errno, strerror(errno)); |
|
|
|
return(-2); |
|
|
|
return -2; |
|
|
|
} |
|
|
|
|
|
|
|
/* Netmask */ |
|
|
|
// netmask
|
|
|
|
if(netmask && (((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr.s_addr != 0)) { |
|
|
|
inet_pton(AF_INET, netmask, &((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr); |
|
|
|
if(ioctl(fd, SIOCSIFNETMASK, &ifr) == -1) { |
|
|
|
traceEvent(TRACE_ERROR, "ioctl(SIOCSIFNETMASK, %s) failed [%d]: %s", netmask, errno, strerror(errno)); |
|
|
|
return(-3); |
|
|
|
return -3; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/* MTU */ |
|
|
|
// MTU
|
|
|
|
ifr.ifr_mtu = mtu; |
|
|
|
if(ioctl(fd, SIOCSIFMTU, &ifr) == -1) { |
|
|
|
traceEvent(TRACE_ERROR, "ioctl(SIOCSIFMTU) failed [%d]: %s", errno, strerror(errno)); |
|
|
|
return(-4); |
|
|
|
return -4; |
|
|
|
} |
|
|
|
|
|
|
|
/* Set up and running */ |
|
|
|
// set up and running
|
|
|
|
if(ioctl(fd, SIOCGIFFLAGS, &ifr) == -1) { |
|
|
|
traceEvent(TRACE_ERROR, "ioctl(SIOCGIFFLAGS) failed [%d]: %s", errno, strerror(errno)); |
|
|
|
return(-5); |
|
|
|
return -5; |
|
|
|
} |
|
|
|
|
|
|
|
ifr.ifr_flags |= (IFF_UP | IFF_RUNNING); |
|
|
|
|
|
|
|
if(ioctl(fd, SIOCSIFFLAGS, &ifr) == -1) { |
|
|
|
traceEvent(TRACE_ERROR, "ioctl(SIOCSIFFLAGS) failed [%d]: %s", errno, strerror(errno)); |
|
|
|
return(-6); |
|
|
|
return -6; |
|
|
|
} |
|
|
|
|
|
|
|
return(0); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
/* ********************************** */ |
|
|
|
|
|
|
|
/** @brief Open and configure the TAP device for packet read/write.
|
|
|
|
* |
|
|
@ -104,6 +105,7 @@ int tuntap_open(tuntap_dev *device, |
|
|
|
char *device_mask, |
|
|
|
const char * device_mac, |
|
|
|
int mtu) { |
|
|
|
|
|
|
|
char *tuntap_device = "/dev/net/tun"; |
|
|
|
int ioctl_fd; |
|
|
|
struct ifreq ifr; |
|
|
@ -122,7 +124,10 @@ int tuntap_open(tuntap_dev *device, |
|
|
|
} |
|
|
|
|
|
|
|
memset(&ifr, 0, sizeof(ifr)); |
|
|
|
ifr.ifr_flags = IFF_TAP|IFF_NO_PI; /* Want a TAP device for layer 2 frames. */ |
|
|
|
|
|
|
|
// want a TAP device for layer 2 frames
|
|
|
|
ifr.ifr_flags = IFF_TAP|IFF_NO_PI; |
|
|
|
|
|
|
|
strncpy(ifr.ifr_name, dev, IFNAMSIZ-1); |
|
|
|
ifr.ifr_name[IFNAMSIZ-1] = '\0'; |
|
|
|
rc = ioctl(device->fd, TUNSETIFF, (void *)&ifr); |
|
|
@ -133,26 +138,29 @@ int tuntap_open(tuntap_dev *device, |
|
|
|
return -1; |
|
|
|
} |
|
|
|
|
|
|
|
/* Store the device name for later reuse */ |
|
|
|
// store the device name for later reuse
|
|
|
|
strncpy(device->dev_name, ifr.ifr_name, MIN(IFNAMSIZ, N2N_IFNAMSIZ)); |
|
|
|
|
|
|
|
if(device_mac && device_mac[0]) { |
|
|
|
/* Use the user-provided MAC */ |
|
|
|
// use the user-provided MAC
|
|
|
|
str2mac(device->mac_addr, device_mac); |
|
|
|
} else { |
|
|
|
/* Set an explicit random MAC to know the exact MAC in use. Manually
|
|
|
|
* reading the MAC address is not safe as it may change internally |
|
|
|
* also after the TAP interface UP status has been notified. */ |
|
|
|
// set an explicit random MAC to know the exact MAC in use, manually
|
|
|
|
// reading the MAC address is not safe as it may change internally
|
|
|
|
// also after the TAP interface UP status has been notified
|
|
|
|
int i; |
|
|
|
|
|
|
|
for(i = 0; i < 6; i++) |
|
|
|
device->mac_addr[i] = n2n_rand(); |
|
|
|
|
|
|
|
device->mac_addr[0] &= ~0x01; /* Clear multicast bit */ |
|
|
|
device->mac_addr[0] |= 0x02; /* Set locally-assigned bit */ |
|
|
|
// clear multicast bit
|
|
|
|
device->mac_addr[0] &= ~0x01; |
|
|
|
|
|
|
|
// set locally-assigned bit
|
|
|
|
device->mac_addr[0] |= 0x02; |
|
|
|
} |
|
|
|
|
|
|
|
/* Initialize Netlink socket */ |
|
|
|
// initialize netlink socket
|
|
|
|
if((nl_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1) { |
|
|
|
traceEvent(TRACE_ERROR, "netlink socket creation failed [%d]: %s", errno, strerror(errno)); |
|
|
|
return -1; |
|
|
@ -172,7 +180,7 @@ int tuntap_open(tuntap_dev *device, |
|
|
|
msg.msg_iov = &iov; |
|
|
|
msg.msg_iovlen = 1; |
|
|
|
|
|
|
|
/* Subscribe to interface events */ |
|
|
|
// subscribe to interface events
|
|
|
|
if(bind(nl_fd, (struct sockaddr*)&sa, sizeof(sa)) == -1) { |
|
|
|
traceEvent(TRACE_ERROR, "netlink socket bind failed [%d]: %s", errno, strerror(errno)); |
|
|
|
return -1; |
|
|
@ -193,7 +201,7 @@ int tuntap_open(tuntap_dev *device, |
|
|
|
|
|
|
|
close(ioctl_fd); |
|
|
|
|
|
|
|
/* Wait for the up and running notification */ |
|
|
|
// wait for the up and running notification
|
|
|
|
traceEvent(TRACE_INFO, "Waiting for TAP interface to be up and running..."); |
|
|
|
|
|
|
|
while(!up_and_running) { |
|
|
@ -212,7 +220,7 @@ int tuntap_open(tuntap_dev *device, |
|
|
|
if(nh->nlmsg_type == NETLINK_GENERIC) { |
|
|
|
struct ifinfomsg *ifi = NLMSG_DATA(nh); |
|
|
|
|
|
|
|
/* NOTE: skipping interface name check, assuming it's our TAP */ |
|
|
|
// NOTE: skipping interface name check, assuming it's our TAP
|
|
|
|
if((ifi->ifi_flags & IFF_UP) && (ifi->ifi_flags & IFF_RUNNING)) { |
|
|
|
up_and_running = 1; |
|
|
|
traceEvent(TRACE_INFO, "Interface is up and running"); |
|
|
@ -228,32 +236,31 @@ int tuntap_open(tuntap_dev *device, |
|
|
|
device->device_mask = inet_addr(device_mask); |
|
|
|
device->if_idx = if_nametoindex(dev); |
|
|
|
|
|
|
|
return(device->fd); |
|
|
|
return device->fd; |
|
|
|
} |
|
|
|
|
|
|
|
/* *************************************************** */ |
|
|
|
|
|
|
|
int tuntap_read (struct tuntap_dev *tuntap, unsigned char *buf, int len) { |
|
|
|
return(read(tuntap->fd, buf, len)); |
|
|
|
|
|
|
|
return read(tuntap->fd, buf, len); |
|
|
|
} |
|
|
|
|
|
|
|
/* *************************************************** */ |
|
|
|
|
|
|
|
int tuntap_write (struct tuntap_dev *tuntap, unsigned char *buf, int len) { |
|
|
|
return(write(tuntap->fd, buf, len)); |
|
|
|
|
|
|
|
return write(tuntap->fd, buf, len); |
|
|
|
} |
|
|
|
|
|
|
|
/* *************************************************** */ |
|
|
|
|
|
|
|
void tuntap_close (struct tuntap_dev *tuntap) { |
|
|
|
|
|
|
|
close(tuntap->fd); |
|
|
|
} |
|
|
|
|
|
|
|
/* *************************************************** */ |
|
|
|
|
|
|
|
/* Fill out the ip_addr value from the interface. Called to pick up dynamic
|
|
|
|
* address changes. */ |
|
|
|
// fill out the ip_addr value from the interface, called to pick up dynamic address changes
|
|
|
|
void tuntap_get_address (struct tuntap_dev *tuntap) { |
|
|
|
|
|
|
|
struct ifreq ifr; |
|
|
|
int fd; |
|
|
|
|
|
|
@ -272,4 +279,5 @@ void tuntap_get_address(struct tuntap_dev *tuntap) { |
|
|
|
close(fd); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#endif /* #ifdef __linux__ */ |
|
|
|