9
0
Fork 0

lib/tun.h: Remove non-endian-safe redefinition of IP header

We can simply use 'struct iphdr' from netinet/ip.h to achieve
the same goal (and be portable).

Change-Id: Ieece22e127dc14a7ffdc3b05656628989ad00b32
This commit is contained in:
Harald Welte 2017-08-02 21:10:09 +02:00
parent d12eab9c4e
commit 63ebccdfe3
4 changed files with 39 additions and 20 deletions

View File

@ -112,6 +112,12 @@ AC_EGREP_HEADER(rt_msghdr, net/route.h,
AC_DEFINE([HAVE_RT_MSGHDR])], AC_DEFINE([HAVE_RT_MSGHDR])],
AC_MSG_RESULT(no)) AC_MSG_RESULT(no))
AC_MSG_CHECKING(whether struct iphdr exists)
AH_TEMPLATE(HAVE_IPHDR)
AC_EGREP_HEADER(struct iphdr, netinet/ip.h,
[AC_MSG_RESULT(yes)
AC_DEFINE([HAVE_IPHDR])],
AC_MSG_RESULT(no))
# Checks for library functions. # Checks for library functions.
AC_PROG_GCC_TRADITIONAL AC_PROG_GCC_TRADITIONAL

View File

@ -34,6 +34,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -43,6 +44,7 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <net/if.h> #include <net/if.h>
#include <net/if.h>
#include <errno.h> #include <errno.h>
@ -221,13 +223,13 @@ int cb_tun_ind(struct tun_t *tun, void *pack, unsigned len)
{ {
struct ippoolm_t *ipm; struct ippoolm_t *ipm;
struct in46_addr dst; struct in46_addr dst;
struct tun_packet_t *iph = (struct tun_packet_t *)pack; struct iphdr *iph = (struct iphdr *)pack;
if (iph->ver == 4) { if (iph->version == 4) {
if (len < sizeof(*iph) || len < 4*iph->ihl) if (len < sizeof(*iph) || len < 4*iph->ihl)
return -1; return -1;
dst.len = 4; dst.len = 4;
dst.v4.s_addr = iph->dst; dst.v4.s_addr = iph->daddr;
} else { } else {
LOGP(DGGSN, LOGL_NOTICE, "non-IPv4 packet received from tun\n"); LOGP(DGGSN, LOGL_NOTICE, "non-IPv4 packet received from tun\n");
return -1; return -1;

View File

@ -17,21 +17,31 @@
#define TUN_ADDRSIZE 128 #define TUN_ADDRSIZE 128
#define TUN_NLBUFSIZE 1024 #define TUN_NLBUFSIZE 1024
struct tun_packet_t { #include "config.h"
unsigned int ver:4; #ifndef HAVE_IPHDR
unsigned int ihl:4; struct iphdr
unsigned int dscp:6; {
unsigned int ecn:2; #if __BYTE_ORDER == __LITTLE_ENDIAN
unsigned int length:16; unsigned int ihl:4;
unsigned int id:16; unsigned int version:4;
unsigned int flags:3; #elif __BYTE_ORDER == __BIG_ENDIAN
unsigned int fragment:13; unsigned int version:4;
unsigned int ttl:8; unsigned int ihl:4;
unsigned int protocol:8; #else
unsigned int check:16; # error "Please fix <bits/endian.h>"
unsigned int src:32; #endif
unsigned int dst:32; u_int8_t tos;
}; u_int16_t tot_len;
u_int16_t id;
u_int16_t frag_off;
u_int8_t ttl;
u_int8_t protocol;
u_int16_t check;
u_int32_t saddr;
u_int32_t daddr;
/*The options start here. */
};
#endif /* !HAVE_IPHDR */
/* *********************************************************** /* ***********************************************************
* Information storage for each tun instance * Information storage for each tun instance

View File

@ -29,6 +29,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -1281,10 +1282,10 @@ int cb_tun_ind(struct tun_t *tun, void *pack, unsigned len)
{ {
struct iphash_t *ipm; struct iphash_t *ipm;
struct in46_addr src; struct in46_addr src;
struct tun_packet_t *iph = (struct tun_packet_t *)pack; struct iphdr *iph = (struct iphdr *)pack;
src.len = 4; src.len = 4;
src.v4.s_addr = iph->src; src.v4.s_addr = iph->saddr;
if (ipget(&ipm, &src)) { if (ipget(&ipm, &src)) {
printf("Dropping packet from invalid source address: %s\n", printf("Dropping packet from invalid source address: %s\n",