Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --persistent flag to Linux edge #1138

Open
wants to merge 1 commit into
base: 3.0-stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

# n2n

NOTE: edge with `--persistent` flag will make the Linux TUNTAP device persistent, this is useful for routing.

n2n is a light VPN software which makes it easy to create virtual networks bypassing intermediate firewalls.

In order to start using n2n, two elements are required:
Expand Down
2 changes: 2 additions & 0 deletions include/n2n_typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ typedef struct tuntap_dev {
uint32_t device_mask;
uint16_t mtu;
char dev_name[N2N_IFNAMSIZ];
int persistent;
} tuntap_dev;

#define SOCKET int
Expand Down Expand Up @@ -676,6 +677,7 @@ typedef struct n2n_edge_conf {
uint8_t sn_selection_strategy; /**< encodes currently chosen supernode selection strategy. */
uint8_t number_max_sn_pings; /**< Number of maximum concurrently allowed supernode pings. */
uint64_t mgmt_password_hash; /**< contains hash of managament port password. */
uint8_t persistent; /**< Persistent TUNTAP */
} n2n_edge_conf_t;


Expand Down
8 changes: 8 additions & 0 deletions src/edge.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ static void help (int level) {
" | '-m 10:20:30:40:50:60', random otherwise\n");
#if defined(N2N_CAN_NAME_IFACE)
printf(" -d <device> | TAP device name\n");
printf(" --persistent | Persistent TAP device\n");
#endif
printf(" -M <mtu> | specify n2n MTU of TAP interface, default %d\n", DEFAULT_MTU);
printf(" -r | enable packet forwarding through n2n community\n");
Expand Down Expand Up @@ -568,6 +569,11 @@ static int setOption (int optkey, char *optargument, n2n_tuntap_priv_config_t *e
ec->tuntap_dev_name[N2N_IFNAMSIZ - 1] = '\0';
break;
}
case '$': /* persistent TUNTAP */ {
traceEvent(TRACE_NORMAL, "persistent TUNTAP");
conf->persistent = 1;
break;
}
#endif
case 'I': /* Device Description (hint) or username */ {
strncpy((char *)conf->dev_desc, optargument, N2N_DESC_SIZE);
Expand Down Expand Up @@ -807,6 +813,7 @@ static const struct option long_options[] =
{ "help", no_argument, NULL, '@' }, /* internal special character '@' to identify long help case */
{ "select-rtt", no_argument, NULL, '[' }, /* '[' rtt selection strategy */
{ "select-mac", no_argument, NULL, ']' }, /* ']' mac selection strategy */
{ "persistent", no_argument, NULL, '$'}, /* '$' persistent tap device */
{ "management-password", required_argument, NULL, '{' }, /* '{' management port password */
{ NULL, 0, NULL, 0 }
};
Expand Down Expand Up @@ -1109,6 +1116,7 @@ int main (int argc, char* argv[]) {
if(setuid(0) != 0)
traceEvent(TRACE_ERROR, "unable to become root [%u/%s]", errno, strerror(errno));
/* setgid(0); */
tuntap.persistent = conf.persistent;
#endif

if(conf.encrypt_key && !strcmp((char*)conf.community_name, conf.encrypt_key))
Expand Down
9 changes: 8 additions & 1 deletion src/tuntap_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ int tuntap_open (tuntap_dev *device,
return -1;
}

// persistent
rc = ioctl(device->fd, TUNSETPERSIST, device->persistent);
if (rc < 0) {
traceEvent(TRACE_ERROR, "tuntap ioctl(TUNSETPERSIST, IFF_TAP) error: %s[%d]\n", strerror(errno), rc);
close(device->fd);
return -1;
}

// store the device name for later reuse
strncpy(device->dev_name, ifr.ifr_name, MIN(IFNAMSIZ, N2N_IFNAMSIZ));

Expand Down Expand Up @@ -251,7 +259,6 @@ int tuntap_write (struct tuntap_dev *tuntap, unsigned char *buf, int len) {


void tuntap_close (struct tuntap_dev *tuntap) {

close(tuntap->fd);
}

Expand Down