diff --git a/lib/core/include/arch/unix/core_arch_network.h b/lib/core/include/arch/unix/core_arch_network.h index a47e1c3a4..63fd90704 100644 --- a/lib/core/include/arch/unix/core_arch_network.h +++ b/lib/core/include/arch/unix/core_arch_network.h @@ -26,7 +26,9 @@ typedef struct _sock_t { index_t index; int family; + int fd; + char ifname[IFNAMSIZ]; c_sockaddr_t local_addr; c_sockaddr_t remote_addr; diff --git a/lib/core/include/core_network.h b/lib/core/include/core_network.h index ec3df6778..8fc6b55c2 100644 --- a/lib/core/include/core_network.h +++ b/lib/core/include/core_network.h @@ -83,10 +83,11 @@ struct c_sockaddr_t { CORE_DECLARE(status_t) sock_init(void); CORE_DECLARE(status_t) sock_final(void); -CORE_DECLARE(status_t) sock_create( - sock_id *id, int family, int type, int protocol); +CORE_DECLARE(status_t) sock_create(sock_id *new); CORE_DECLARE(status_t) sock_delete(sock_id id); +CORE_DECLARE(status_t) sock_socket( + sock_id *id, int family, int type, int protocol); CORE_DECLARE(status_t) sock_bind(sock_id id, c_sockaddr_t *sa); CORE_DECLARE(status_t) sock_connect(sock_id id, c_sockaddr_t *sa); @@ -131,6 +132,14 @@ CORE_DECLARE(int) core_sctp_sendmsg(sock_id id, const void *msg, size_t len, #define CORE_SCTP_REMOTE_CLOSED -2 CORE_DECLARE(int) core_sctp_recvmsg(sock_id id, void *msg, size_t len, c_sockaddr_t *from, c_uint32_t *ppid, c_uint16_t *stream_no); +/* + * TUN Driver + */ +CORE_DECLARE(status_t) tun_open(sock_id *new, + char *ifname, int is_tap); +CORE_DECLARE(status_t) tun_set_ipv4(sock_id id, + c_uint32_t ip_addr, c_uint8_t bits); + /* * Send/Recv */ diff --git a/lib/core/src/Makefile.am b/lib/core/src/Makefile.am index e2609cb97..657fc0824 100644 --- a/lib/core/src/Makefile.am +++ b/lib/core/src/Makefile.am @@ -30,7 +30,7 @@ libcore_la_SOURCES = \ unix/rand.c unix/time.c unix/file.c unix/net_lib.c \ unix/thread.c unix/signal.c \ unix/atomic.c unix/cond.c unix/mutex.c unix/rwlock.c unix/semaphore.c \ - unix/socket.c unix/udp.c unix/tcp.c \ + unix/socket.c unix/udp.c unix/tcp.c unix/tun.c \ $(NULL) if !USRSCTP diff --git a/lib/core/src/unix/sctp.c b/lib/core/src/unix/sctp.c index c464d3c49..5d3bf100e 100644 --- a/lib/core/src/unix/sctp.c +++ b/lib/core/src/unix/sctp.c @@ -18,7 +18,7 @@ status_t sctp_socket(sock_id *new, int family, int type) { status_t rv; - rv = sock_create(new, family, type, IPPROTO_SCTP); + rv = sock_socket(new, family, type, IPPROTO_SCTP); d_assert(rv == CORE_OK, return CORE_ERROR,); rv = subscribe_to_events(*new); diff --git a/lib/core/src/unix/socket.c b/lib/core/src/unix/socket.c index 5bc7f25b3..9c132ea77 100644 --- a/lib/core/src/unix/socket.c +++ b/lib/core/src/unix/socket.c @@ -35,7 +35,7 @@ status_t sock_final(void) return CORE_OK; } -status_t sock_create(sock_id *new, int family, int type, int protocol) +status_t sock_create(sock_id *new) { sock_t *sock = NULL; @@ -43,16 +43,7 @@ status_t sock_create(sock_id *new, int family, int type, int protocol) d_assert(sock, return CORE_ENOMEM,); memset(sock, 0, sizeof(sock_t)); - sock->family = family; - sock->fd = socket(sock->family, type, protocol); - if (sock->fd < 0) - { - d_warn("socket create(%d:%d:%d) failed(%d:%s)", - sock->family, type, protocol, errno, strerror(errno)); - return CORE_ERROR; - } - - d_trace(1, "socket create(%d:%d:%d)\n", sock->family, type, protocol); + sock->fd = -1; *new = (sock_id)sock; @@ -74,6 +65,30 @@ status_t sock_delete(sock_id id) return CORE_OK; } +status_t sock_socket(sock_id *new, int family, int type, int protocol) +{ + status_t rv; + sock_t *sock = NULL; + + rv = sock_create(new); + d_assert(rv == CORE_OK, return CORE_ERROR,); + + sock = (sock_t *)(*new); + + sock->family = family; + sock->fd = socket(sock->family, type, protocol); + if (sock->fd < 0) + { + d_warn("socket create(%d:%d:%d) failed(%d:%s)", + sock->family, type, protocol, errno, strerror(errno)); + return CORE_ERROR; + } + + d_trace(1, "socket create(%d:%d:%d)\n", sock->family, type, protocol); + + return CORE_OK; +} + status_t sock_bind(sock_id id, c_sockaddr_t *sa) { sock_t *sock = (sock_t *)id; diff --git a/lib/core/src/unix/tcp.c b/lib/core/src/unix/tcp.c index c5494c829..ed8280183 100644 --- a/lib/core/src/unix/tcp.c +++ b/lib/core/src/unix/tcp.c @@ -16,7 +16,7 @@ status_t tcp_server(sock_id *new, while(sa) { - rv = sock_create(new, sa->c_sa_family, SOCK_STREAM, IPPROTO_TCP); + rv = sock_socket(new, sa->c_sa_family, SOCK_STREAM, IPPROTO_TCP); if (rv != CORE_OK) continue; sock = (sock_t *)*new; @@ -67,7 +67,7 @@ status_t tcp_client(sock_id *new, while(sa) { - rv = sock_create(new, sa->c_sa_family, SOCK_STREAM, IPPROTO_TCP); + rv = sock_socket(new, sa->c_sa_family, SOCK_STREAM, IPPROTO_TCP); if (rv != CORE_OK) continue; sock = (sock_t *)*new; diff --git a/lib/core/src/unix/tun.c b/lib/core/src/unix/tun.c new file mode 100644 index 000000000..36e6f6018 --- /dev/null +++ b/lib/core/src/unix/tun.c @@ -0,0 +1,185 @@ +#define TRACE_MODULE _core_tun + +#include "core_debug.h" +#include "core_arch_network.h" + +#if HAVE_NET_ROUTE_H +#include +#endif + +status_t tun_open(sock_id *new, char *ifname, int is_tap) +{ + status_t rv; + sock_t *sock = NULL; + int fd = -1; +#if LINUX == 1 + char *dev = "/dev/net/tun"; + int rc; + struct ifreq ifr; + int flags = IFF_NO_PI; + + fd = open(dev, O_RDWR); + if (fd < 0) + { + d_error("Can not open %s",dev); + return -1; + } +#else + char name[C_PATH_MAX]; + int tun = 0; + +#define TUNTAP_ID_MAX 255 + for (tun = 0; tun < TUNTAP_ID_MAX; tun++) + { + (void)snprintf(name, sizeof(name), "/dev/tun%i", tun); + if ((fd = open(name, O_RDWR)) > 0) + { + (void)snprintf(name, sizeof(name), "tun%i", tun); + ifname = name; + break; + } + } +#endif + + rv = sock_create(new); + d_assert(rv == CORE_OK, return CORE_ERROR,); + + sock = (sock_t *)(*new); + d_assert(sock, return CORE_ERROR,); + + /* Save socket descriptor */ + sock->fd = fd; + /* Save the interface name */ + strncpy(sock->ifname, ifname, IFNAMSIZ); + +#if LINUX == 1 + memset(&ifr, 0, sizeof(ifr)); + + ifr.ifr_flags = (is_tap ? (flags | IFF_TAP) : (flags | IFF_TUN)); + strncpy(ifr.ifr_name, ifname, IFNAMSIZ); + + rc = ioctl(sock->fd, TUNSETIFF, (void *)&ifr); + if (rc < 0) + { + d_error("iotcl error(dev:%s flags = %d)", ifname, flags); + goto cleanup; + } +#endif + + return CORE_OK; + +#if LINUX == 1 +cleanup: + sock_delete(*new); + return CORE_ERROR; +#endif +} + +status_t tun_set_ipv4(sock_id id, c_uint32_t ip_addr, c_uint8_t bits) +{ +#if LINUX != 1 + sock_t *sock = NULL; + int fd; + + struct ifaliasreq ifa; + struct ifreq ifr; + struct sockaddr_in addr; + struct sockaddr_in mask; + c_uint32_t mask_addr = htonl(0xffffffff << (32 - bits)); + + char buf[512]; + int len; + struct rt_msghdr *rtm; + struct sockaddr_in dst, gw; + struct sockaddr_in *paddr; + + sock = (sock_t *)id; + d_assert(id, return CORE_ERROR,); + + fd = socket(AF_INET, SOCK_DGRAM, 0); + + (void)memset(&ifa, '\0', sizeof ifa); + (void)strlcpy(ifa.ifra_name, sock->ifname, sizeof ifa.ifra_name); + + (void)memset(&ifr, '\0', sizeof ifr); + (void)strlcpy(ifr.ifr_name, sock->ifname, sizeof ifr.ifr_name); + + /* Delete previously assigned address */ + (void)ioctl(fd, SIOCDIFADDR, &ifr); + + (void)memset(&addr, '\0', sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = ip_addr; + addr.sin_len = sizeof(addr); + (void)memcpy(&ifa.ifra_addr, &addr, sizeof(addr)); + (void)memcpy(&ifa.ifra_broadaddr, &addr, sizeof(addr)); + + (void)memset(&mask, '\0', sizeof(mask)); + mask.sin_family = AF_INET; + mask.sin_addr.s_addr = 0xffffffff; + mask.sin_len = sizeof(mask); + (void)memcpy(&ifa.ifra_mask, &mask, sizeof(ifa.ifra_mask)); + + if (ioctl(fd, SIOCAIFADDR, &ifa) == -1) { + d_error("Can't IP address(dev:%s err:%s)", + sock->ifname, strerror(errno)); + return CORE_ERROR; + } + + close(fd); /* AF_INET, SOCK_DGRAM */ + + fd = socket(PF_ROUTE, SOCK_RAW, 0); + if (fd < 0) + { + d_error("Can't open PF_ROUTE(%s)", strerror(errno)); + return CORE_ERROR; + } + + (void)memset(&buf, 0, sizeof(buf)); + rtm = (struct rt_msghdr *)buf; + rtm->rtm_type = RTM_ADD; + rtm->rtm_version = RTM_VERSION; + rtm->rtm_pid = getpid(); + rtm->rtm_seq = 0; + rtm->rtm_flags = RTF_UP | RTF_GATEWAY; + rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK; + paddr = (struct sockaddr_in *)(rtm + 1); + + (void)memset(&dst, '\0', sizeof(dst)); + dst.sin_family = AF_INET; + dst.sin_addr.s_addr = ip_addr & mask_addr; + dst.sin_len = sizeof(dst); + (void)memcpy(paddr, &dst, sizeof(dst)); + paddr = (struct sockaddr_in *)((char *)paddr + + CORE_ALIGN(sizeof(*paddr), sizeof(c_uintptr_t))); + + (void)memset(&gw, '\0', sizeof(gw)); + gw.sin_family = AF_INET; + gw.sin_addr.s_addr = ip_addr; + gw.sin_len = sizeof(gw); + (void)memcpy(paddr, &gw, sizeof(gw)); + paddr = (struct sockaddr_in *)((char *)paddr + + CORE_ALIGN(sizeof(*paddr), sizeof(c_uintptr_t))); + + (void)memset(&mask, '\0', sizeof(mask)); + mask.sin_family = AF_INET; + mask.sin_addr.s_addr = mask_addr; + mask.sin_len = sizeof(mask); + (void)memcpy(paddr, &mask, sizeof(mask)); + paddr = (struct sockaddr_in *)((char *)paddr + + CORE_ALIGN(sizeof(*paddr), sizeof(c_uintptr_t))); + + len = (char*)paddr - buf; + rtm->rtm_msglen = len; + if (write(fd, buf, len) < 0) + { + d_error("Can't add routing(%s)", strerror(errno)); + return CORE_ERROR; + } + + close(fd); /* PF_ROUTE, SOCK_RAW */ + +#endif /* LINUX == 1 */ + + return 0; +} diff --git a/lib/core/src/unix/udp.c b/lib/core/src/unix/udp.c index e1ae40cc6..b9f734999 100644 --- a/lib/core/src/unix/udp.c +++ b/lib/core/src/unix/udp.c @@ -7,7 +7,7 @@ status_t udp_socket(sock_id *new, int family) { status_t rv; - rv = sock_create(new, family, SOCK_DGRAM, IPPROTO_UDP); + rv = sock_socket(new, family, SOCK_DGRAM, IPPROTO_UDP); d_assert(rv == CORE_OK && new, return CORE_ERROR,); d_trace(1, "udp socket(%d)\n", family); diff --git a/lib/gtp/gtp_path.c b/lib/gtp/gtp_path.c index 60888f7f6..7ed652477 100644 --- a/lib/gtp/gtp_path.c +++ b/lib/gtp/gtp_path.c @@ -144,8 +144,8 @@ status_t gtp_send(gtp_node_t *gnode, pkbuf_t *pkbuf) sock = gnode->sock; d_assert(sock, return CORE_ERROR, "Null param"); - sent = net_sendto(sock, pkbuf->payload, pkbuf->len, - gnode->addr.sin.sin_addr.s_addr, ntohs(gnode->addr.c_sa_port)); + sent = sendto(sock->sock_id, pkbuf->payload, pkbuf->len, 0, + &gnode->addr.sa, sizeof(gnode->addr.sin)); d_trace(50, "Sent %d->%d bytes to [%s:%d]\n", pkbuf->len, sent, CORE_NTOP(&gnode->addr, buf), ntohs(gnode->addr.c_sa_port)); d_trace_hex(50, pkbuf->payload, pkbuf->len); diff --git a/src/pgw/pgw_context.c b/src/pgw/pgw_context.c index e635ecb12..50f7ced30 100644 --- a/src/pgw/pgw_context.c +++ b/src/pgw/pgw_context.c @@ -497,6 +497,7 @@ pgw_sess_t *pgw_sess_add(gtp_f_teid_t *sgw_s5c_teid, sgw->addr.sin.sin_addr.s_addr = addr; sgw->addr.c_sa_port = htons(GTPV2_C_UDP_PORT); + sgw->addr.c_sa_family = AF_INET; sgw->sock = pgw_self()->gtpc_sock; } /* Setup GTP Node between PGW and SGW */ diff --git a/src/pgw/pgw_gtp_path.c b/src/pgw/pgw_gtp_path.c index 4a004a5e3..3e70dc4c1 100644 --- a/src/pgw/pgw_gtp_path.c +++ b/src/pgw/pgw_gtp_path.c @@ -64,6 +64,7 @@ static int _gtpv1_tun_recv_cb(net_link_t *net_link, void *data) /* Send to SGW */ gnode.addr.sin.sin_addr.s_addr = bearer->sgw_s5u_addr; gnode.addr.c_sa_port = htons(GTPV1_U_UDP_PORT); + gnode.addr.c_sa_family = AF_INET; gnode.sock = pgw_self()->gtpu_sock; d_trace(50, "Send S5U PDU (teid = 0x%x)to SGW(%s)\n", bearer->sgw_s5u_teid, diff --git a/src/sgw/sgw_context.c b/src/sgw/sgw_context.c index 2300f92fa..6f5430770 100644 --- a/src/sgw/sgw_context.c +++ b/src/sgw/sgw_context.c @@ -406,6 +406,7 @@ sgw_ue_t* sgw_ue_add(gtp_f_teid_t *mme_s11_teid, d_assert(mme, return NULL, "Can't add MME-GTP node"); mme->addr.sin.sin_addr.s_addr = addr; + mme->addr.c_sa_family = AF_INET; mme->addr.c_sa_port = htons(GTPV2_C_UDP_PORT); mme->sock = sgw_self()->gtpc_sock; } diff --git a/src/sgw/sgw_gtp_path.c b/src/sgw/sgw_gtp_path.c index 5ec907a87..056880808 100644 --- a/src/sgw/sgw_gtp_path.c +++ b/src/sgw/sgw_gtp_path.c @@ -100,6 +100,7 @@ static int _gtpv1_u_recv_cb(net_sock_t *sock, void *data) gnode.addr.sin.sin_addr.s_addr = sock->remote.sin_addr.s_addr; gnode.addr.c_sa_port = sock->remote.sin_port; + gnode.addr.c_sa_family = AF_INET; gnode.sock = sock; gtp_send(&gnode, echo_rsp); @@ -119,6 +120,7 @@ static int _gtpv1_u_recv_cb(net_sock_t *sock, void *data) /* Convert TEID */ gnode.addr.c_sa_port = htons(GTPV1_U_UDP_PORT); + gnode.addr.c_sa_family = AF_INET; gnode.sock = sgw_self()->gtpu_sock; if (tunnel->interface_type == @@ -295,6 +297,7 @@ status_t sgw_gtp_send_end_marker(sgw_bearer_t *bearer) gnode.addr.sin.sin_addr.s_addr = s1u_tunnel->remote_addr; gnode.addr.c_sa_port = htons(GTPV1_U_UDP_PORT); + gnode.addr.c_sa_family = AF_INET; gnode.sock = sgw_self()->gtpu_sock; rv = gtp_send(&gnode, pkbuf); diff --git a/src/sgw/sgw_s11_handler.c b/src/sgw/sgw_s11_handler.c index c80f06935..e8551adcf 100644 --- a/src/sgw/sgw_s11_handler.c +++ b/src/sgw/sgw_s11_handler.c @@ -107,6 +107,7 @@ void sgw_s11_handle_create_session_request(gtp_xact_t *s11_xact, pgw->addr.sin.sin_addr.s_addr = addr; pgw->addr.c_sa_port = htons(GTPV2_C_UDP_PORT); + pgw->addr.c_sa_family = AF_INET; pgw->sock = sgw_self()->gtpc_sock; } diff --git a/test/testpacket.c b/test/testpacket.c index b59a04eac..1dad0d1b0 100644 --- a/test/testpacket.c +++ b/test/testpacket.c @@ -141,6 +141,7 @@ int testgtpu_enb_send(net_sock_t *sock, c_uint32_t src_ip, c_uint32_t dst_ip) gnode.addr.sin.sin_addr.s_addr = bearer->sgw_s1u_addr; gnode.addr.c_sa_port = htons(GTPV1_U_UDP_PORT); + gnode.addr.c_sa_family = AF_INET; gnode.sock = sock; rv = gtp_send(&gnode, pkbuf);