summaryrefslogtreecommitdiff
path: root/lua/libnet_decode.c
blob: 125eaf2343cf7c99a3ce2100dfe032b60cf38d44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
Copyright (C) 2010 Wurldtech Security Technologies All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED.IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <libnet.h>

/* push data pblock, if there is any data to push */
static int pushdata(const uint8_t* pkt, size_t pkt_s, libnet_t *l, int ptag)
{
    (void) ptag;

    if(pkt_s)
      return libnet_build_data(pkt, pkt_s, l, 0);

    return 0;
}

int libnet_decode_tcp(const uint8_t* pkt, size_t pkt_s, libnet_t *l)
{
    const struct libnet_tcp_hdr* tcp_hdr = (const struct libnet_tcp_hdr*) pkt;
    size_t th_off = 0;
    const uint8_t* payload = NULL;
    size_t payload_s = 0;
    int otag;
    int ptag;

    if(pkt_s < sizeof(*tcp_hdr))
      return pushdata(pkt, pkt_s, l, 0);

    th_off = tcp_hdr->th_off * 4;

    if(pkt_s < th_off || th_off < LIBNET_TCP_H)
      return pushdata(pkt, pkt_s, l, 0);

    payload = pkt + th_off;
    payload_s = pkt + pkt_s - payload;

    if(th_off > LIBNET_TCP_H) {
	const uint8_t* options = pkt + LIBNET_TCP_H;
	size_t options_s = th_off - LIBNET_TCP_H;
	otag = libnet_build_tcp_options(options, options_s, l, 0);
	if(otag < 0)
	    return otag;
    }

    ptag = libnet_build_tcp(
            ntohs(tcp_hdr->th_sport),
            ntohs(tcp_hdr->th_dport),
            ntohl(tcp_hdr->th_seq),
            ntohl(tcp_hdr->th_ack),
            tcp_hdr->th_flags,
            ntohs(tcp_hdr->th_win),
            ntohs(tcp_hdr->th_sum),
            ntohs(tcp_hdr->th_urp),
            pkt_s,
            payload, payload_s,
            l, 0);

    if(ptag > 0) {
        /* Patch the reserved flags to equal those in the original packet. Even
         * though they should usually be zero.
         */
        struct libnet_tcp_hdr* hdr = (struct libnet_tcp_hdr*) libnet_pblock_find(l, ptag)->buf;
        hdr->th_x2 = tcp_hdr->th_x2;
    }

    if(ptag > 0)
        libnet_pblock_setflags(libnet_pblock_find(l, ptag), LIBNET_PBLOCK_DO_CHECKSUM);

    return ptag;
}

int libnet_decode_udp(const uint8_t* pkt, size_t pkt_s, libnet_t *l)
{
    const struct libnet_udp_hdr* udp_hdr = (const struct libnet_udp_hdr*) pkt;
    const uint8_t* payload = pkt + LIBNET_UDP_H;
    size_t payload_s = pkt + pkt_s - payload;
    int ptag;

    if(pkt_s < sizeof(*udp_hdr))
      return pushdata(pkt, pkt_s, l, 0);

    ptag = libnet_build_udp(
            ntohs(udp_hdr->uh_sport),
            ntohs(udp_hdr->uh_dport),
            ntohs(udp_hdr->uh_ulen),
            ntohs(udp_hdr->uh_sum),
            payload, payload_s,
            l, 0);

    if(ptag > 0)
        libnet_pblock_setflags(libnet_pblock_find(l, ptag), LIBNET_PBLOCK_DO_CHECKSUM);

    return ptag;
}

int libnet_decode_ipv4(const uint8_t* pkt, size_t pkt_s, libnet_t *l)
{
    const struct libnet_ipv4_hdr* ip_hdr = (const struct libnet_ipv4_hdr*) pkt;
    size_t ip_hl = 0;
    const uint8_t* payload = NULL;
    size_t payload_s = 0;
    int ptag = 0; /* payload tag */
    int otag = 0; /* options tag */
    int itag = 0; /* ip tag */

    if(pkt_s < sizeof(*ip_hdr))
      return pushdata(pkt, pkt_s, l, 0);

    ip_hl = ip_hdr->ip_hl * 4;

    if(pkt_s < ip_hl || ip_hl < LIBNET_IPV4_H)
      return pushdata(pkt, pkt_s, l, 0);

    /* pcaps often contain trailing garbage after the IP packet, drop that, but not the ip hdr
     * no matter how damaged the ip_len field is */
    /* TODO this means its not possible to reencode pcaps... the padding that comes
       off the wire for eth frames shorter than 60 bytes gets lost. However, the next-hdr
       only knows its length because of what tcp tells it... maybe ipv4 should push pblock
       with any trailing garbage, afterwards? But that might cause lengths to be reevaluated
       unless there was a specific pblock type for ethernet-padding. */
    if(pkt_s > ntohs(ip_hdr->ip_len))
        pkt_s = ntohs(ip_hdr->ip_len);

    if(pkt_s < ip_hl)
        pkt_s = ip_hl;

    payload = pkt + ip_hl;
    payload_s = pkt + pkt_s - payload;

    switch(ip_hdr->ip_p) {
        case IPPROTO_UDP:
            ptag = libnet_decode_udp(payload, payload_s, l);
            payload_s = 0;
            break;
        case IPPROTO_TCP:
            ptag = libnet_decode_tcp(payload, payload_s, l);
            payload_s = 0;
            break;
    }

    if(ptag < 0) return ptag;

    if(ip_hl > LIBNET_IPV4_H) {
        const uint8_t* options = pkt + LIBNET_IPV4_H;
        size_t options_s = ip_hl - LIBNET_IPV4_H;
        otag = libnet_build_ipv4_options(options, options_s, l, 0);
        if(otag < 0) {
            return otag;
        }
    }

    itag = libnet_build_ipv4(
            ntohs(ip_hdr->ip_len),
            ip_hdr->ip_tos,
            ntohs(ip_hdr->ip_id),
            ntohs(ip_hdr->ip_off),
            ip_hdr->ip_ttl,
            ip_hdr->ip_p,
            ntohs(ip_hdr->ip_sum),
            ip_hdr->ip_src.s_addr,
            ip_hdr->ip_dst.s_addr,
            payload, payload_s,
            l, 0
            );

    if(itag > 0)
        libnet_pblock_setflags(libnet_pblock_find(l, itag), LIBNET_PBLOCK_DO_CHECKSUM);

    return itag;
}

int libnet_decode_ip(const uint8_t* pkt, size_t pkt_s, libnet_t *l)
{
    const struct libnet_ipv4_hdr* ip_hdr = (const struct libnet_ipv4_hdr*) pkt;

    if(pkt_s < sizeof(*ip_hdr))
      return pushdata(pkt, pkt_s, l, 0);

    switch(ip_hdr->ip_v) {
        case 4:
            return libnet_decode_ipv4(pkt, pkt_s, l);
        /* TODO - IPv6 */
    }

    return pushdata(pkt, pkt_s, l, 0);
}

int libnet_decode_eth(const uint8_t* pkt, size_t pkt_s, libnet_t *l)
{
    const struct libnet_ethernet_hdr* hdr = (const struct libnet_ethernet_hdr*) pkt;
    const uint8_t* payload = pkt + LIBNET_ETH_H;
    size_t payload_s = pkt + pkt_s - payload;
    int ptag = 0; /* payload tag */
    int etag = 0; /* eth tag */

    if(pkt_s < sizeof(*hdr))
      return pushdata(pkt, pkt_s, l, 0);

    switch(ntohs(hdr->ether_type)) {
        case ETHERTYPE_IP:
            ptag = libnet_decode_ip(payload, payload_s, l);
            payload_s = 0;
            break;
    }

    if(ptag < 0) return ptag;

    etag = libnet_build_ethernet(
            hdr->ether_dhost,
            hdr->ether_shost,
            ntohs(hdr->ether_type),
            payload, payload_s,
            l, 0
            );

    return etag;
}