Skip to content

Commit 90fb0de

Browse files
committed
Fixed IP header lenght in Nat_dp.c
Update Nat_dp.c
1 parent 77003ff commit 90fb0de

1 file changed

Lines changed: 20 additions & 14 deletions

File tree

src/services/pcn-nat/src/Nat_dp.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) {
1010
void *data_end = (void *)(long)ctx->data_end;
1111

1212
struct eth_hdr *eth = data;
13-
if (data + sizeof(*eth) > data_end)
13+
if ( (void *)eth + sizeof(*eth) > data_end )
1414
goto DROP;
1515

1616
pcn_log(
@@ -49,7 +49,7 @@ static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) {
4949
uint8_t update_session_table = 1;
5050

5151
struct iphdr *ip = data + sizeof(*eth);
52-
if (data + sizeof(*eth) + sizeof(*ip) > data_end)
52+
if ( (void *)ip + sizeof(*ip) > data_end )
5353
goto DROP;
5454

5555
pcn_log(ctx, LOG_TRACE, "Processing IP packet: src %I, dst: %I", ip->saddr,
@@ -61,8 +61,9 @@ static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) {
6161

6262
switch (ip->protocol) {
6363
case IPPROTO_TCP: {
64-
struct tcphdr *tcp = data + sizeof(*eth) + sizeof(*ip);
65-
if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*tcp) > data_end)
64+
uint8_t header_len = 4 * ip->ihl;
65+
struct tcphdr *tcp = data + sizeof(*eth) + header_len;
66+
if ( (void *)tcp + sizeof(*tcp) > data_end )
6667
goto DROP;
6768

6869
pcn_log(ctx, LOG_TRACE, "Packet is TCP: src_port %P, dst_port %P",
@@ -72,8 +73,9 @@ static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) {
7273
break;
7374
}
7475
case IPPROTO_UDP: {
75-
struct udphdr *udp = data + sizeof(*eth) + sizeof(*ip);
76-
if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*udp) > data_end)
76+
uint8_t header_len = 4 * ip->ihl;
77+
struct udphdr *udp = data + sizeof(*eth) + header_len;
78+
if ( (void *)udp + sizeof(*udp) > data_end )
7779
goto DROP;
7880
pcn_log(ctx, LOG_TRACE, "Packet is UDP: src_port %P, dst_port %P",
7981
udp->source, udp->dest);
@@ -82,8 +84,9 @@ static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) {
8284
break;
8385
}
8486
case IPPROTO_ICMP: {
85-
struct icmphdr *icmp = data + sizeof(*eth) + sizeof(*ip);
86-
if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*icmp) > data_end)
87+
uint8_t header_len = 4 * ip->ihl;
88+
struct icmphdr *icmp = data + sizeof(*eth) + header_len;
89+
if ( (void *)icmp + sizeof(*icmp) > data_end )
8790
goto DROP;
8891
pcn_log(ctx, LOG_TRACE, "Packet is ICMP: type %d, id %d", icmp->type,
8992
icmp->un.echo.id);
@@ -302,8 +305,9 @@ apply_nat:;
302305
uint32_t l4sum = pcn_csum_diff(&old_port, 4, &new_port, 4, 0);
303306
switch (proto) {
304307
case IPPROTO_TCP: {
305-
struct tcphdr *tcp = data + sizeof(*eth) + sizeof(*ip);
306-
if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*tcp) > data_end)
308+
uint8_t header_len = 4 * ip->ihl;
309+
struct tcphdr *tcp = data + sizeof(*eth) + header_len;
310+
if ( (void *)tcp + sizeof(*tcp) > data_end )
307311
goto DROP;
308312

309313
if (rule_type == NAT_SRC || rule_type == NAT_MSQ) {
@@ -326,8 +330,9 @@ apply_nat:;
326330
goto proceed;
327331
}
328332
case IPPROTO_UDP: {
329-
struct udphdr *udp = data + sizeof(*eth) + sizeof(*ip);
330-
if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*udp) > data_end)
333+
uint8_t header_len = 4 * ip->ihl;
334+
struct udphdr *udp = data + sizeof(*eth) + header_len;
335+
if ( (void *)udp + sizeof(*udp) > data_end )
331336
goto DROP;
332337
if (rule_type == NAT_SRC || rule_type == NAT_MSQ) {
333338
ip->saddr = new_ip;
@@ -349,8 +354,9 @@ apply_nat:;
349354
goto proceed;
350355
}
351356
case IPPROTO_ICMP: {
352-
struct icmphdr *icmp = data + sizeof(*eth) + sizeof(*ip);
353-
if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*icmp) > data_end)
357+
uint8_t header_len = 4 * ip->ihl;
358+
struct icmphdr *icmp = data + sizeof(*eth) + header_len;
359+
if ( (void *)icmp + sizeof(*icmp) > data_end )
354360
goto DROP;
355361
if (rule_type == NAT_SRC || rule_type == NAT_MSQ) {
356362
ip->saddr = new_ip;

0 commit comments

Comments
 (0)