Harald Welte has submitted this change and it was merged.

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


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
---
M configure.ac
M ggsn/ggsn.c
M lib/tun.h
M sgsnemu/sgsnemu.c
4 files changed, 39 insertions(+), 20 deletions(-)

Approvals:
  Harald Welte: Looks good to me, approved
  Jenkins Builder: Verified



diff --git a/configure.ac b/configure.ac
index 45ab22b..b7e6793 100644
--- a/configure.ac
+++ b/configure.ac
@@ -112,6 +112,12 @@
  AC_DEFINE([HAVE_RT_MSGHDR])],
  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.
 AC_PROG_GCC_TRADITIONAL
diff --git a/ggsn/ggsn.c b/ggsn/ggsn.c
index 0d0e569..4fb2066 100644
--- a/ggsn/ggsn.c
+++ b/ggsn/ggsn.c
@@ -34,6 +34,7 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
+#include <netinet/ip.h>
 #include <arpa/inet.h>
 #include <sys/wait.h>
 #include <sys/stat.h>
@@ -42,6 +43,7 @@
 #include <inttypes.h>
 #include <sys/socket.h>
 #include <sys/ioctl.h>
+#include <net/if.h>
 #include <net/if.h>
 
 #include <errno.h>
@@ -221,13 +223,13 @@
 {
        struct ippoolm_t *ipm;
        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)
                        return -1;
                dst.len = 4;
-               dst.v4.s_addr = iph->dst;
+               dst.v4.s_addr = iph->daddr;
        } else {
                LOGP(DGGSN, LOGL_NOTICE, "non-IPv4 packet received from tun\n");
                return -1;
diff --git a/lib/tun.h b/lib/tun.h
index ce7b91c..c50bdf9 100644
--- a/lib/tun.h
+++ b/lib/tun.h
@@ -17,21 +17,31 @@
 #define TUN_ADDRSIZE     128
 #define TUN_NLBUFSIZE   1024
 
-struct tun_packet_t {
-       unsigned int ver:4;
-       unsigned int ihl:4;
-       unsigned int dscp:6;
-       unsigned int ecn:2;
-       unsigned int length:16;
-       unsigned int id:16;
-       unsigned int flags:3;
-       unsigned int fragment:13;
-       unsigned int ttl:8;
-       unsigned int protocol:8;
-       unsigned int check:16;
-       unsigned int src:32;
-       unsigned int dst:32;
-};
+#include "config.h"
+#ifndef HAVE_IPHDR
+struct iphdr
+  {
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+    unsigned int ihl:4;
+    unsigned int version:4;
+#elif __BYTE_ORDER == __BIG_ENDIAN
+    unsigned int version:4;
+    unsigned int ihl:4;
+#else
+# error        "Please fix <bits/endian.h>"
+#endif
+    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
diff --git a/sgsnemu/sgsnemu.c b/sgsnemu/sgsnemu.c
index 90a6200..af8f9fa 100644
--- a/sgsnemu/sgsnemu.c
+++ b/sgsnemu/sgsnemu.c
@@ -29,6 +29,7 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
+#include <netinet/ip.h>
 #include <arpa/inet.h>
 #include <sys/wait.h>
 #include <sys/stat.h>
@@ -1281,10 +1282,10 @@
 {
        struct iphash_t *ipm;
        struct in46_addr src;
-       struct tun_packet_t *iph = (struct tun_packet_t *)pack;
+       struct iphdr *iph = (struct iphdr *)pack;
 
        src.len = 4;
-       src.v4.s_addr = iph->src;
+       src.v4.s_addr = iph->saddr;
 
        if (ipget(&ipm, &src)) {
                printf("Dropping packet from invalid source address: %s\n",

-- 
To view, visit https://gerrit.osmocom.org/3404
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ieece22e127dc14a7ffdc3b05656628989ad00b32
Gerrit-PatchSet: 6
Gerrit-Project: openggsn
Gerrit-Branch: master
Gerrit-Owner: Harald Welte <lafo...@gnumonks.org>
Gerrit-Reviewer: Harald Welte <lafo...@gnumonks.org>
Gerrit-Reviewer: Jenkins Builder

Reply via email to