Skip to content
Snippets Groups Projects
Verified Commit 6be91cea authored by Lukas Jelinek's avatar Lukas Jelinek :file_cabinet:
Browse files

parser: unsupport all protocols other than TCP and UDP

parent e132a6e4
No related merge requests found
Pipeline #102708 passed with stages
in 35 seconds
...@@ -6,6 +6,9 @@ ...@@ -6,6 +6,9 @@
#include "log.h" #include "log.h"
enum parse_result {SUCCESS = 0, LENGTH, PROTOCOL};
size_t max_packet_size() { size_t max_packet_size() {
return \ return \
MAX(sizeof(struct ip), sizeof(struct ip6_hdr)) + MAX(sizeof(struct ip), sizeof(struct ip6_hdr)) +
...@@ -16,7 +19,8 @@ size_t max_packet_size() { ...@@ -16,7 +19,8 @@ size_t max_packet_size() {
__attribute__((nonnull)) __attribute__((nonnull))
static bool handle_ipv4(const void *payload, size_t payload_size, struct packet_data *dt) { static enum parse_result handle_ipv4(const void *payload, size_t payload_size, struct packet_data *dt) {
enum parse_result res = SUCCESS;
const struct ip *p = (struct ip*)payload; const struct ip *p = (struct ip*)payload;
inet_ntop(AF_INET, &p->ip_src, dt->source_ip, sizeof(dt->source_ip)); inet_ntop(AF_INET, &p->ip_src, dt->source_ip, sizeof(dt->source_ip));
...@@ -27,32 +31,40 @@ static bool handle_ipv4(const void *payload, size_t payload_size, struct packet_ ...@@ -27,32 +31,40 @@ static bool handle_ipv4(const void *payload, size_t payload_size, struct packet_
const void *phdr = payload + (4 * p->ip_hl); const void *phdr = payload + (4 * p->ip_hl);
switch (p->ip_p) { switch (p->ip_p) {
case IPPROTO_UDP: { case IPPROTO_UDP: {
if (payload_size < (sizeof(struct ip) + sizeof(struct udphdr))) if (payload_size < (sizeof(struct ip) + sizeof(struct udphdr))) {
return false; res = LENGTH;
break;
}
const struct udphdr *udp = (struct udphdr*)phdr; const struct udphdr *udp = (struct udphdr*)phdr;
dt->source_port = ntohs(udp->source); dt->source_port = ntohs(udp->source);
dt->dest_port = ntohs(udp->dest); dt->dest_port = ntohs(udp->dest);
break; break;
} }
case IPPROTO_TCP: { case IPPROTO_TCP: {
if (payload_size < (sizeof(struct ip) + sizeof(struct tcphdr))) if (payload_size < (sizeof(struct ip) + sizeof(struct tcphdr))) {
return false; res = LENGTH;
break;
}
const struct tcphdr *tcp = (struct tcphdr*)phdr; const struct tcphdr *tcp = (struct tcphdr*)phdr;
dt->source_port = ntohs(tcp->source); dt->source_port = ntohs(tcp->source);
dt->dest_port = ntohs(tcp->dest); dt->dest_port = ntohs(tcp->dest);
break; break;
} }
default: default:
dt->source_port = 0; res = PROTOCOL;
dt->dest_port = 0;
} }
return true; return res;
} }
__attribute__((nonnull)) __attribute__((nonnull))
static bool handle_ipv6(const void *payload, size_t payload_size, struct packet_data *dt) { static enum parse_result handle_ipv6(const void *payload, size_t payload_size, struct packet_data *dt) {
if (payload_size < sizeof(struct ip6_hdr)) enum parse_result res = SUCCESS;
return false;
if (payload_size < sizeof(struct ip6_hdr)) {
res = LENGTH;
return res;
}
const struct ip6_hdr *p = (struct ip6_hdr*)payload; const struct ip6_hdr *p = (struct ip6_hdr*)payload;
inet_ntop(AF_INET6, &p->ip6_src, dt->source_ip, sizeof(dt->source_ip)); inet_ntop(AF_INET6, &p->ip6_src, dt->source_ip, sizeof(dt->source_ip));
...@@ -68,26 +80,29 @@ static bool handle_ipv6(const void *payload, size_t payload_size, struct packet_ ...@@ -68,26 +80,29 @@ static bool handle_ipv6(const void *payload, size_t payload_size, struct packet_
const void *phdr = payload + sizeof(struct ip6_hdr); const void *phdr = payload + sizeof(struct ip6_hdr);
switch (p->ip6_nxt) { switch (p->ip6_nxt) {
case IPPROTO_UDP: { case IPPROTO_UDP: {
if (payload_size < (sizeof(struct ip6_hdr) + sizeof(struct udphdr))) if (payload_size < (sizeof(struct ip6_hdr) + sizeof(struct udphdr))) {
return false; res = LENGTH;
break;
}
const struct udphdr *udp = (struct udphdr*)phdr; const struct udphdr *udp = (struct udphdr*)phdr;
dt->source_port = ntohs(udp->source); dt->source_port = ntohs(udp->source);
dt->dest_port = ntohs(udp->dest); dt->dest_port = ntohs(udp->dest);
break; break;
} }
case IPPROTO_TCP: { case IPPROTO_TCP: {
if (payload_size < (sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) if (payload_size < (sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) {
return false; res = LENGTH;
break;
}
const struct tcphdr *tcp = (struct tcphdr*)phdr; const struct tcphdr *tcp = (struct tcphdr*)phdr;
dt->source_port = ntohs(tcp->source); dt->source_port = ntohs(tcp->source);
dt->dest_port = ntohs(tcp->dest); dt->dest_port = ntohs(tcp->dest);
break; break;
} }
default: default:
dt->source_port = 0; res = PROTOCOL;
dt->dest_port = 0;
} }
return true; return res;
} }
...@@ -98,12 +113,20 @@ bool parse_packet(const void *data, size_t data_size, struct packet_data *packet ...@@ -98,12 +113,20 @@ bool parse_packet(const void *data, size_t data_size, struct packet_data *packet
struct ip *p_ip = (struct ip*)data; struct ip *p_ip = (struct ip*)data;
switch (p_ip->ip_v) { switch (p_ip->ip_v) {
case 4: case 4:
if (!handle_ipv4(data, data_size, packet_data)) switch (handle_ipv4(data, data_size, packet_data)) {
goto invalid_size; case LENGTH:
goto invalid_size;
case PROTOCOL:
goto unsupported_protocol;
}
break; break;
case 6: case 6:
if (!handle_ipv6(data, data_size, packet_data)) switch (handle_ipv6(data, data_size, packet_data)) {
goto invalid_size; case LENGTH:
goto invalid_size;
case PROTOCOL:
goto unsupported_protocol;
}
break; break;
default: default:
debug("Received packet with unknown IP version: %d", p_ip->ip_v); debug("Received packet with unknown IP version: %d", p_ip->ip_v);
...@@ -119,4 +142,8 @@ bool parse_packet(const void *data, size_t data_size, struct packet_data *packet ...@@ -119,4 +142,8 @@ bool parse_packet(const void *data, size_t data_size, struct packet_data *packet
invalid_size: invalid_size:
debug("Received packet has smaller size than expected IP header. Ignoring"); debug("Received packet has smaller size than expected IP header. Ignoring");
return false; return false;
unsupported_protocol:
debug("Unsupported network protocol detected (other than TCP and UDP). Ignoring");
return false;
} }
...@@ -67,17 +67,6 @@ static struct packets { ...@@ -67,17 +67,6 @@ static struct packets {
.dest_port = 53, .dest_port = 53,
} }
}, },
{
.data = icmp_data,
.len = icmp_len,
.expected = (struct packet_data){
.proto = "ICMP",
.source_ip = "192.168.2.1",
.dest_ip = "192.168.2.148",
.source_port = 0,
.dest_port = 0,
}
},
}; };
......
...@@ -71,17 +71,6 @@ static struct packets { ...@@ -71,17 +71,6 @@ static struct packets {
.dest_port = 53, .dest_port = 53,
} }
}, },
{
.data = icmp_data,
.len = icmp_len,
.expected = (struct packet_data){
.proto = "ICMPV6",
.source_ip = "2001:1ae9:a3:df80::1",
.dest_ip = "2001:1ae9:a3:df80::60",
.source_port = 0,
.dest_port = 0,
}
},
}; };
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment