From 8e797f0a8afcf02f5c4e97fec99134356bd0fcd2 Mon Sep 17 00:00:00 2001
From: Eric Leblond <eric@regit.org>
Date: Mon, 16 Jul 2012 16:11:40 +0200
Subject: [PATCH] Add support for IPv4-in-IPv6

This patch adds support for IPv4-in-IPv6 and should fix #462.
---
 src/decode-ipv6.c |   23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/src/decode-ipv6.c b/src/decode-ipv6.c
index 15701e1..4a8d978 100644
--- a/src/decode-ipv6.c
+++ b/src/decode-ipv6.c
@@ -45,6 +45,23 @@
 #define IPV6_EXTHDRS     ip6eh.ip6_exthdrs
 #define IPV6_EH_CNT      ip6eh.ip6_exthdrs_cnt
 
+static int DecodeIPv4inIPv6(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, uint8_t *pkt, uint16_t plen, PacketQueue *pq)
+{
+    /* Try to detect IPv4 in IPv6 */
+    if (IP_GET_RAW_VER(pkt) == 4) {
+        if (pq != NULL) {
+            Packet *tp = PacketPseudoPktSetup(p, pkt, plen, IPPROTO_IP);
+            if (tp != NULL) {
+                DecodeTunnel(tv, dtv, tp, GET_PKT_DATA(tp),
+                        GET_PKT_LEN(tp), pq, IPPROTO_IP);
+                PacketEnqueue(pq,tp);
+                return 1;
+            }
+        }
+    }
+    return 0;
+}
+
 static void
 DecodeIPV6ExtHdrs(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, uint8_t *pkt, uint16_t len, PacketQueue *pq)
 {
@@ -408,6 +425,9 @@ DecodeIPV6ExtHdrs(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, uint8_t *pkt
                 SCReturn;
 
             default:
+                if (DecodeIPv4inIPv6(tv, dtv, p, pkt, plen, pq) == 1) {
+                    SCReturn;
+                }
                 IPV6_SET_L4PROTO(p,nh);
                 SCReturn;
         }
@@ -492,6 +512,9 @@ void DecodeIPV6(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, uint8_t *pkt,
             DecodeIPV6ExtHdrs(tv, dtv, p, pkt + IPV6_HEADER_LEN, IPV6_GET_PLEN(p), pq);
             break;
         default:
+            if (DecodeIPv4inIPv6(tv, dtv, p, pkt + IPV6_HEADER_LEN, IPV6_GET_PLEN(p), pq) == 1) {
+                SCReturn;
+            }
             p->proto = IPV6_GET_NH(p);
             break;
     }
-- 
1.7.10.4

