• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OISF / suricata / 23374838686

21 Mar 2026 07:29AM UTC coverage: 59.341% (-20.0%) from 79.315%
23374838686

Pull #15075

github

web-flow
Merge 90b4e834f into 6587e363a
Pull Request #15075: Stack 8001 v16.4

38 of 70 new or added lines in 10 files covered. (54.29%)

34165 existing lines in 563 files now uncovered.

119621 of 201584 relevant lines covered (59.34%)

650666.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

58.06
/src/decode-teredo.c
1
/* Copyright (C) 2012-2021 Open Information Security Foundation
2
 *
3
 * You can copy, redistribute or modify this Program under the terms of
4
 * the GNU General Public License version 2 as published by the Free
5
 * Software Foundation.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * version 2 along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15
 * 02110-1301, USA.
16
 */
17

18
/**
19
 * \ingroup decode
20
 *
21
 * @{
22
 */
23

24

25
/**
26
 * \file
27
 *
28
 * \author Eric Leblond <eric@regit.org>
29
 *
30
 * Decode Teredo Tunneling protocol.
31
 *
32
 * This implementation is based upon RFC 4380: http://www.ietf.org/rfc/rfc4380.txt
33
 */
34

35
#include "suricata-common.h"
36
#include "decode.h"
37
#include "decode-ipv6.h"
38
#include "decode-teredo.h"
39

40
#include "util-validate.h"
41
#include "util-debug.h"
42
#include "conf.h"
43

44
#include "detect.h"
45
#include "detect-engine-port.h"
46

47
#define TEREDO_ORIG_INDICATION_LENGTH   8
2,640✔
UNCOV
48
#define TEREDO_MAX_PORTS                4
×
UNCOV
49
#define TEREDO_UNSET_PORT               -1
×
50

51
static bool g_teredo_enabled = true;
52
static bool g_teredo_ports_any = true;
53
static int g_teredo_ports_cnt = 0;
54
static int g_teredo_ports[TEREDO_MAX_PORTS] = { TEREDO_UNSET_PORT, TEREDO_UNSET_PORT,
55
    TEREDO_UNSET_PORT, TEREDO_UNSET_PORT };
56

57
bool DecodeTeredoEnabledForPort(const uint16_t sp, const uint16_t dp)
58
{
115,722✔
59
    SCLogDebug("ports %u->%u ports %d %d %d %d", sp, dp, g_teredo_ports[0], g_teredo_ports[1],
115,722✔
60
            g_teredo_ports[2], g_teredo_ports[3]);
115,722✔
61

62
    if (g_teredo_enabled) {
115,722✔
63
        /* no port config means we are enabled for all ports */
64
        if (g_teredo_ports_any) {
115,722✔
65
            return true;
115,722✔
66
        }
115,722✔
67

UNCOV
68
        for (int i = 0; i < g_teredo_ports_cnt; i++) {
×
UNCOV
69
            if (g_teredo_ports[i] == TEREDO_UNSET_PORT)
×
70
                return false;
×
UNCOV
71
            const int port = g_teredo_ports[i];
×
UNCOV
72
            if (port == (const int)sp || port == (const int)dp)
×
UNCOV
73
                return true;
×
UNCOV
74
        }
×
UNCOV
75
    }
×
UNCOV
76
    return false;
×
77
}
115,722✔
78

79
static void DecodeTeredoConfigPorts(const char *pstr)
UNCOV
80
{
×
UNCOV
81
    SCLogDebug("parsing \'%s\'", pstr);
×
82

UNCOV
83
    if (strcmp(pstr, "any") == 0) {
×
84
        g_teredo_ports_any = true;
×
85
        return;
×
86
    }
×
87

UNCOV
88
    DetectPort *head = NULL;
×
UNCOV
89
    DetectPortParse(NULL, &head, pstr);
×
90

UNCOV
91
    g_teredo_ports_any = false;
×
UNCOV
92
    g_teredo_ports_cnt = 0;
×
UNCOV
93
    for (DetectPort *p = head; p != NULL; p = p->next) {
×
UNCOV
94
        if (g_teredo_ports_cnt >= TEREDO_MAX_PORTS) {
×
95
            SCLogWarning("only %d Teredo ports can be defined", TEREDO_MAX_PORTS);
×
96
            break;
×
97
        }
×
UNCOV
98
        g_teredo_ports[g_teredo_ports_cnt++] = (int)p->port;
×
UNCOV
99
    }
×
100

UNCOV
101
    DetectPortCleanupList(NULL, head);
×
UNCOV
102
}
×
103

104
void DecodeTeredoConfig(void)
105
{
2✔
106
    int enabled = 0;
2✔
107
    if (SCConfGetBool("decoder.teredo.enabled", &enabled) == 1) {
2✔
UNCOV
108
        if (enabled) {
×
UNCOV
109
            g_teredo_enabled = true;
×
UNCOV
110
        } else {
×
111
            g_teredo_enabled = false;
×
112
        }
×
UNCOV
113
    }
×
114
    if (g_teredo_enabled) {
2✔
115
        SCConfNode *node = SCConfGetNode("decoder.teredo.ports");
2✔
116
        if (node && node->val) {
2✔
UNCOV
117
            DecodeTeredoConfigPorts(node->val);
×
UNCOV
118
        }
×
119
    }
2✔
120
}
2✔
121

122
/**
123
 * \brief Function to decode Teredo packets
124
 *
125
 * \retval TM_ECODE_FAILED if packet is not a Teredo packet, TM_ECODE_OK if it is
126
 */
127
int DecodeTeredo(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p,
128
        const uint8_t *pkt, uint16_t len)
129
{
115,722✔
130
    DEBUG_VALIDATE_BUG_ON(pkt == NULL);
115,722✔
131

132
    if (!g_teredo_enabled)
115,722✔
133
        return TM_ECODE_FAILED;
×
134

135
    const uint8_t *start = pkt;
115,722✔
136

137
    /* Is this packet to short to contain an IPv6 packet ? */
138
    if (len < IPV6_HEADER_LEN)
115,722✔
139
        return TM_ECODE_FAILED;
27,085✔
140

141
    /* Teredo encapsulate IPv6 in UDP and can add some custom message
142
     * part before the IPv6 packet. In our case, we just want to get
143
     * over an ORIGIN indication. So we just make one offset if needed. */
144
    if (start[0] == 0x0) {
88,637✔
145
        /* origin indication: compatible with tunnel */
146
        if (start[1] == 0x0) {
1,917✔
147
            /* offset is not coherent with len and presence of an IPv6 header */
148
            if (len < TEREDO_ORIG_INDICATION_LENGTH + IPV6_HEADER_LEN)
1,374✔
149
                return TM_ECODE_FAILED;
108✔
150

151
            start += TEREDO_ORIG_INDICATION_LENGTH;
1,266✔
152

153
            /* either authentication negotiation not real tunnel or invalid second byte */
154
        } else {
1,266✔
155
            return TM_ECODE_FAILED;
543✔
156
        }
543✔
157
    }
1,917✔
158

159
    /* There is no specific field that we can check to prove that the packet
160
     * is a Teredo packet. We've zapped here all the possible Teredo header
161
     * and we should have an IPv6 packet at the start pointer.
162
     * We then can only do a few checks before sending the encapsulated packets
163
     * to decoding:
164
     *  - The packet has a protocol version which is IPv6.
165
     *  - The IPv6 length of the packet matches what remains in buffer.
166
     *  - HLIM is 0. This would technically be valid, but still weird.
167
     *  - NH 0 (HOP) and not enough data.
168
     *
169
     *  If all these conditions are met, the tunnel decoder will be called.
170
     *  If the packet gets an invalid event set, it will still be rejected.
171
     */
172
    if (IP_GET_RAW_VER(start) == 6) {
87,986✔
173
        IPV6Hdr *thdr = (IPV6Hdr *)start;
3,945✔
174

175
        /* ignore hoplimit 0 packets, most likely an artifact of bad detection */
176
        if (IPV6_GET_RAW_HLIM(thdr) == 0)
3,945✔
177
            return TM_ECODE_FAILED;
866✔
178

179
        /* if nh is 0 (HOP) with little data we have a bogus packet */
180
        if (IPV6_GET_RAW_NH(thdr) == 0 && IPV6_GET_RAW_PLEN(thdr) < 8)
3,079✔
181
            return TM_ECODE_FAILED;
354✔
182

183
        if (len ==  IPV6_HEADER_LEN +
2,725✔
184
                IPV6_GET_RAW_PLEN(thdr) + (start - pkt)) {
2,725✔
185
            uint32_t blen = len - (uint32_t)(start - pkt);
1,098✔
186
            /* spawn off tunnel packet */
187
            Packet *tp = PacketTunnelPktSetup(tv, dtv, p, start, blen,
1,098✔
188
                    DECODE_TUNNEL_IPV6_TEREDO);
1,098✔
189
            if (tp != NULL) {
1,098✔
190
                PKT_SET_SRC(tp, PKT_SRC_DECODER_TEREDO);
916✔
191
                /* add the tp to the packet queue. */
192
                PacketEnqueueNoLock(&tv->decode_pq,tp);
916✔
193
                StatsCounterIncr(&tv->stats, dtv->counter_teredo);
916✔
194
                return TM_ECODE_OK;
916✔
195
            }
916✔
196
        }
1,098✔
197
        return TM_ECODE_FAILED;
1,809✔
198
    }
2,725✔
199

200
    return TM_ECODE_FAILED;
84,041✔
201
}
87,986✔
202

203
/**
204
 * @}
205
 */
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc