open5gs/lib/tun/linux-setup.c

78 lines
1.9 KiB
C
Raw Permalink Normal View History

2019-04-27 14:54:30 +00:00
/*
* Copyright (C) 2019 by Sukchan Lee <acetcom@gmail.com>
*
* This file is part of Open5GS.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "ogs-tun.h"
#undef OGS_LOG_DOMAIN
#define OGS_LOG_DOMAIN __ogs_sock_domain
2019-09-13 12:07:47 +00:00
#include <net/if.h>
#include <net/route.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
2017-11-22 14:11:47 +00:00
2019-09-13 12:07:47 +00:00
#include <linux/if_tun.h>
2017-11-22 13:32:57 +00:00
2019-04-27 14:54:30 +00:00
#ifndef IFNAMSIZ
#define IFNAMSIZ 32
#endif
ogs_socket_t ogs_tun_open(char *ifname, int len, int is_tap)
2017-11-22 13:32:57 +00:00
{
2019-04-27 14:54:30 +00:00
ogs_socket_t fd = INVALID_SOCKET;
const char *dev = "/dev/net/tun";
2017-11-22 13:32:57 +00:00
int rc;
struct ifreq ifr;
int flags = IFF_NO_PI;
2019-04-27 14:54:30 +00:00
ogs_assert(ifname);
2017-11-22 13:32:57 +00:00
fd = open(dev, O_RDWR);
if (fd < 0) {
2019-04-27 14:54:30 +00:00
ogs_log_message(OGS_LOG_ERROR, ogs_socket_errno,
"open() failed : dev[%s]", dev);
return INVALID_SOCKET;
2017-11-22 13:32:57 +00:00
}
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = (is_tap ? (flags | IFF_TAP) : (flags | IFF_TUN));
2018-06-23 12:59:40 +00:00
strncpy(ifr.ifr_name, ifname, IFNAMSIZ-1);
2017-11-22 13:32:57 +00:00
2019-04-27 14:54:30 +00:00
rc = ioctl(fd, TUNSETIFF, (void *)&ifr);
if (rc < 0) {
2019-04-27 14:54:30 +00:00
ogs_log_message(OGS_LOG_ERROR, ogs_socket_errno,
"ioctl() failed : dev[%s] flags[0x%x]", dev, flags);
2017-11-22 13:32:57 +00:00
goto cleanup;
}
2019-04-27 14:54:30 +00:00
return fd;
2017-11-22 13:32:57 +00:00
cleanup:
2019-04-27 14:54:30 +00:00
close(fd);
return INVALID_SOCKET;
2017-11-22 13:32:57 +00:00
}
int ogs_tun_set_ip(char *ifname, ogs_ipsubnet_t *gw, ogs_ipsubnet_t *sub)
2017-11-22 13:32:57 +00:00
{
return OGS_OK;
2017-12-13 07:07:12 +00:00
}