Skip to content

Commit

Permalink
Fix merge
Browse files Browse the repository at this point in the history
calculate and use use buf len as required for all IPv6 modules
  • Loading branch information
zirngibl authored Jul 18, 2022
1 parent 61ce466 commit 6d6c5d7
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 30 deletions.
6 changes: 5 additions & 1 deletion src/probe_modules/module_icmp6_echoscan.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static int icmp6_echo_init_perthread(void* buf, macaddr_t *src,
return EXIT_SUCCESS;
}

static int icmp6_echo_make_packet(void *buf, UNUSED size_t *buf_len, UNUSED ipaddr_n_t src_ip, UNUSED ipaddr_n_t dst_ip, uint8_t ttl, uint32_t *validation, UNUSED int probe_num, UNUSED void *arg)
static int icmp6_echo_make_packet(void *buf, size_t *buf_len, UNUSED ipaddr_n_t src_ip, UNUSED ipaddr_n_t dst_ip, uint8_t ttl, uint32_t *validation, UNUSED int probe_num, UNUSED void *arg)
{
struct ether_header *eth_header = (struct ether_header *) buf;
struct ip6_hdr *ip6_header = (struct ip6_hdr *)(&eth_header[1]);
Expand All @@ -86,6 +86,10 @@ static int icmp6_echo_make_packet(void *buf, UNUSED size_t *buf_len, UNUSED ipad
2*sizeof(uint32_t)
);

// 8 bytes of data are used in ICMPv6 for validation
*buf_len = sizeof(struct ether_header) + sizeof(struct ip6_hdr) + ICMP_MINLEN + 2*sizeof(uint32_t);


return EXIT_SUCCESS;
}

Expand Down
6 changes: 4 additions & 2 deletions src/probe_modules/module_ipv6_quic_initial.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ int ipv6_quic_initial_init_perthread(void *buf, macaddr_t *src, macaddr_t *gw,
return EXIT_SUCCESS;
}

int ipv6_quic_initial_make_packet(void *buf, UNUSED size_t *buf_len,
int ipv6_quic_initial_make_packet(void *buf, size_t *buf_len,
ipaddr_n_t src_ip, ipaddr_n_t dst_ip,
UNUSED uint8_t ttl, uint32_t *validation,
int probe_num, UNUSED void *arg)
Expand Down Expand Up @@ -182,7 +182,9 @@ int ipv6_quic_initial_make_packet(void *buf, UNUSED size_t *buf_len,
htons(sizeof(struct udphdr) + payload_len);
udp_header->uh_ulen = ntohs(sizeof(struct udphdr) + payload_len);

udp_header->uh_sum = ipv6_udp_checksum(&ip6_header->ip6_src, &ip6_header->ip6_dst, udp_header);
udp_header->uh_sum = ipv6_udp_checksum(&ip6_header->ip6_src, &ip6_header->ip6_dst, udp_header);
size_t headers_len = sizeof(struct ether_header) + sizeof(struct ip6_hdr) + sizeof(struct udphdr);
*buf_len = headers_len + payload_len;

return EXIT_SUCCESS;
}
Expand Down
18 changes: 10 additions & 8 deletions src/probe_modules/module_ipv6_tcp_synopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ probe_module_t module_ipv6_tcp_synopt;
static uint32_t num_ports;

#define MAX_OPT_LEN 40
#define ZMAPV6_TCP_SYNOPT_TCP_HEADER_LEN 20
#define ZMAPV6_TCP_SYNOPT_PACKET_LEN 74

static char *tcp_send_opts = NULL;
static int tcp_send_opts_len = 0;
Expand All @@ -56,8 +58,7 @@ int ipv6_tcp_synopt_global_initialize(struct state_conf *conf)

if (!(conf->probe_args && strlen(conf->probe_args) > 0)){
printf("no args, using empty tcp options\n");
module_ipv6_tcp_synopt.max_packet_length = sizeof(struct ether_header) + sizeof(struct ip6_hdr)
+ sizeof(struct tcphdr);
module_ipv6_tcp_synopt.max_packet_length = ZMAPV6_TCP_SYNOPT_PACKET_LEN;
return(EXIT_SUCCESS);
}
args = strdup(conf->probe_args);
Expand Down Expand Up @@ -100,8 +101,7 @@ int ipv6_tcp_synopt_global_initialize(struct state_conf *conf)
tcp_send_opts_len = MAX_OPT_LEN;
exit(1);
}
module_ipv6_tcp_synopt.max_packet_length = sizeof(struct ether_header) + sizeof(struct ip6_hdr)
+ sizeof(struct tcphdr)+ tcp_send_opts_len;
module_ipv6_tcp_synopt.max_packet_length = ZMAPV6_TCP_SYNOPT_PACKET_LEN + tcp_send_opts_len;

return EXIT_SUCCESS;
}
Expand All @@ -114,14 +114,14 @@ int ipv6_tcp_synopt_init_perthread(void* buf, macaddr_t *src,
struct ether_header *eth_header = (struct ether_header *) buf;
make_eth_header_ethertype(eth_header, src, gw, ETHERTYPE_IPV6);
struct ip6_hdr *ip6_header = (struct ip6_hdr*)(&eth_header[1]);
uint16_t payload_len = sizeof(struct tcphdr)+tcp_send_opts_len;
uint16_t payload_len = ZMAPV6_TCP_SYNOPT_TCP_HEADER_LEN+tcp_send_opts_len;
make_ip6_header(ip6_header, IPPROTO_TCP, payload_len);
struct tcphdr *tcp_header = (struct tcphdr*)(&ip6_header[1]);
make_tcp_header(tcp_header, dst_port, TH_SYN);
return EXIT_SUCCESS;
}

int ipv6_tcp_synopt_make_packet(void *buf, UNUSED size_t *buf_len, __attribute__((unused)) ipaddr_n_t src_ip, __attribute__((unused)) ipaddr_n_t dst_ip,
int ipv6_tcp_synopt_make_packet(void *buf, size_t *buf_len, __attribute__((unused)) ipaddr_n_t src_ip, __attribute__((unused)) ipaddr_n_t dst_ip,
uint8_t ttl, uint32_t *validation, int probe_num, void *arg)
{
struct ether_header *eth_header = (struct ether_header *) buf;
Expand All @@ -144,9 +144,11 @@ int ipv6_tcp_synopt_make_packet(void *buf, UNUSED size_t *buf_len, __attribute__


tcp_header->th_sum = 0;
tcp_header->th_sum = tcp6_checksum(sizeof(struct tcphdr)+tcp_send_opts_len,
tcp_header->th_sum = tcp6_checksum(ZMAPV6_TCP_SYNOPT_TCP_HEADER_LEN+tcp_send_opts_len,
&ip6_header->ip6_src, &ip6_header->ip6_dst, tcp_header);

*buf_len = ZMAPV6_TCP_SYNOPT_PACKET_LEN+tcp_send_opts_len;

return EXIT_SUCCESS;
}

Expand Down Expand Up @@ -211,7 +213,7 @@ void ipv6_tcp_synopt_process_packet(const u_char *packet,

probe_module_t module_ipv6_tcp_synopt = {
.name = "ipv6_tcp_synopt",
.max_packet_length = 74, // will be extended at runtime
.max_packet_length = ZMAPV6_TCP_SYNOPT_PACKET_LEN, // will be extended at runtime
.pcap_filter = "ip6 proto 6 && (ip6[53] & 4 != 0 || ip6[53] == 18)",
.pcap_snaplen = 116+10*4, // max option len
.port_args = 1,
Expand Down
13 changes: 9 additions & 4 deletions src/probe_modules/module_ipv6_tcp_synscan.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
#include "probe_modules.h"
#include "packet.h"

#define ZMAPV6_TCP_SYNSCAN_TCP_HEADER_LEN 20
#define ZMAPV6_TCP_SYNSCAN_PACKET_LEN 74

probe_module_t module_ipv6_tcp_synscan;
static uint32_t num_ports;

Expand All @@ -49,14 +52,14 @@ int ipv6_synscan_init_perthread(void* buf, macaddr_t *src,
struct ether_header *eth_header = (struct ether_header *) buf;
make_eth_header_ethertype(eth_header, src, gw, ETHERTYPE_IPV6);
struct ip6_hdr *ip6_header = (struct ip6_hdr*)(&eth_header[1]);
uint16_t payload_len = sizeof(struct tcphdr);
uint16_t payload_len = ZMAPV6_TCP_SYNSCAN_TCP_HEADER_LEN;
make_ip6_header(ip6_header, IPPROTO_TCP, payload_len);
struct tcphdr *tcp_header = (struct tcphdr*)(&ip6_header[1]);
make_tcp_header(tcp_header, dst_port, TH_SYN);
return EXIT_SUCCESS;
}

int ipv6_synscan_make_packet(void *buf, UNUSED size_t *buf_len, UNUSED ipaddr_n_t src_ip, UNUSED ipaddr_n_t dst_ip,
int ipv6_synscan_make_packet(void *buf, size_t *buf_len, UNUSED ipaddr_n_t src_ip, UNUSED ipaddr_n_t dst_ip,
uint8_t ttl, uint32_t *validation, int probe_num, void *arg)
{
struct ether_header *eth_header = (struct ether_header *) buf;
Expand All @@ -72,9 +75,11 @@ int ipv6_synscan_make_packet(void *buf, UNUSED size_t *buf_len, UNUSED ipaddr_n_
probe_num, validation));
tcp_header->th_seq = tcp_seq;
tcp_header->th_sum = 0;
tcp_header->th_sum = tcp6_checksum(sizeof(struct tcphdr),
tcp_header->th_sum = tcp6_checksum(ZMAPV6_TCP_SYNSCAN_TCP_HEADER_LEN,
&ip6_header->ip6_src, &ip6_header->ip6_dst, tcp_header);

*buf_len = ZMAPV6_TCP_SYNSCAN_PACKET_LEN;

return EXIT_SUCCESS;
}

Expand Down Expand Up @@ -159,7 +164,7 @@ static fielddef_t fields[] = {

probe_module_t module_ipv6_tcp_synscan = {
.name = "ipv6_tcp_synscan",
.max_packet_length = 74,
.max_packet_length = ZMAPV6_TCP_SYNSCAN_PACKET_LEN,
.pcap_filter = "ip6 proto 6 && (ip6[53] & 4 != 0 || ip6[53] == 18)",
.pcap_snaplen = 116, // was 96 for IPv4
.port_args = 1,
Expand Down
7 changes: 6 additions & 1 deletion src/probe_modules/module_ipv6_udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ int ipv6_udp_init_perthread(void* buf, macaddr_t *src,
return EXIT_SUCCESS;
}

int ipv6_udp_make_packet(void *buf, UNUSED size_t *buf_len, __attribute__((unused)) ipaddr_n_t src_ip,
int ipv6_udp_make_packet(void *buf, size_t *buf_len, __attribute__((unused)) ipaddr_n_t src_ip,
__attribute__((unused)) ipaddr_n_t dst_ip, uint8_t ttl, uint32_t *validation, int probe_num, void *arg)
{
// From module_ipv6_udp_dns
Expand Down Expand Up @@ -305,6 +305,10 @@ int ipv6_udp_make_packet(void *buf, UNUSED size_t *buf_len, __attribute__((unuse
}
*/
udp_header->uh_sum = ipv6_udp_checksum(&ip6_header->ip6_src, &ip6_header->ip6_dst, udp_header);

size_t headers_len = sizeof(struct ether_header) + sizeof(struct ip6_hdr) +
sizeof(struct udphdr);
*buf_len = headers_len + udp_send_msg_len;

return EXIT_SUCCESS;
}
Expand Down Expand Up @@ -379,6 +383,7 @@ void ipv6_udp_process_packet(const u_char *packet, UNUSED uint32_t len, fieldset
fs_add_string(fs, "icmp_unreach_str", (char *) "unknown", 0);
}
*/
fs_add_null(fs, "icmp_unreach_str");
fs_add_null(fs, "udp_pkt_size");
fs_add_null(fs, "data");
} else {
Expand Down
7 changes: 6 additions & 1 deletion src/probe_modules/module_ipv6_udp_dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ int ipv6_udp_dns_init_perthread(void* buf, macaddr_t *src,
return EXIT_SUCCESS;
}

int ipv6_udp_dns_make_packet(void *buf, UNUSED size_t *buf_len, UNUSED ipaddr_n_t src_ip, UNUSED ipaddr_n_t dst_ip,
int ipv6_udp_dns_make_packet(void *buf, size_t *buf_len, UNUSED ipaddr_n_t src_ip, UNUSED ipaddr_n_t dst_ip,
uint8_t ttl, uint32_t *validation, int probe_num, __attribute__((unused)) void *arg) {
struct ether_header *eth_header = (struct ether_header *) buf;
struct ip6_hdr *ip6_header = (struct ip6_hdr*) (&eth_header[1]);
Expand All @@ -221,6 +221,11 @@ int ipv6_udp_dns_make_packet(void *buf, UNUSED size_t *buf_len, UNUSED ipaddr_n_
udp_header->uh_sport = htons(get_src_port(num_ports, probe_num,
validation));
udp_header->uh_sum = ipv6_udp_checksum(&ip6_header->ip6_src, &ip6_header->ip6_dst, udp_header);

udp_header->uh_sum = ipv6_udp_checksum(&ip6_header->ip6_src, &ip6_header->ip6_dst, udp_header);

size_t headers_len = sizeof(struct ether_header) + sizeof(struct ip6_hdr) + sizeof(struct udphdr);
*buf_len = headers_len + udp_send_msg_len;

return EXIT_SUCCESS;
}
Expand Down
5 changes: 4 additions & 1 deletion src/probe_modules/module_quic_initial.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ int quic_initial_init_perthread(void *buf, macaddr_t *src, macaddr_t *gw,
return EXIT_SUCCESS;
}

int quic_initial_make_packet(void *buf, UNUSED size_t *buf_len,
int quic_initial_make_packet(void *buf, size_t *buf_len,
ipaddr_n_t src_ip, ipaddr_n_t dst_ip,
UNUSED uint8_t ttl, uint32_t *validation,
int probe_num, UNUSED void *arg)
Expand Down Expand Up @@ -177,6 +177,9 @@ int quic_initial_make_packet(void *buf, UNUSED size_t *buf_len,

ip_header->ip_sum = 0;
ip_header->ip_sum = zmap_ip_checksum((unsigned short *)ip_header);

size_t headers_len = sizeof(struct ether_header) + sizeof(struct ip) + sizeof(struct udphdr);
*buf_len = headers_len + payload_len;

return EXIT_SUCCESS;
}
Expand Down
21 changes: 12 additions & 9 deletions src/probe_modules/module_tcp_synopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ static uint32_t num_ports;

#define MAX_OPT_LEN 40

#define ZMAP_TCP_SYNOPT_TCP_HEADER_LEN 20
#define ZMAP_TCP_SYNOPT_PACKET_LEN 54

static char *tcp_send_opts = NULL;
static int tcp_send_opts_len = 0;

Expand All @@ -45,8 +48,7 @@ int tcpsynopt_global_initialize(struct state_conf *conf)

if (!(conf->probe_args && strlen(conf->probe_args) > 0)){
printf("no args, using empty tcp options\n");
module_tcp_synopt.max_packet_length = sizeof(struct ether_header) + sizeof(struct ip)
+ sizeof(struct tcphdr);
module_tcp_synopt.max_packet_length = ZMAP_TCP_SYNOPT_PACKET_LEN;
return(EXIT_SUCCESS);
}
args = strdup(conf->probe_args);
Expand Down Expand Up @@ -90,8 +92,7 @@ int tcpsynopt_global_initialize(struct state_conf *conf)
tcp_send_opts_len = MAX_OPT_LEN;
exit(1);
}
module_tcp_synopt.max_packet_length = sizeof(struct ether_header) + sizeof(struct ip)
+ sizeof(struct tcphdr)+ tcp_send_opts_len;
module_tcp_synopt.max_packet_length = ZMAP_TCP_SYNOPT_PACKET_LEN + tcp_send_opts_len;

return EXIT_SUCCESS;
}
Expand All @@ -105,14 +106,14 @@ int tcpsynopt_init_perthread(void* buf, macaddr_t *src,
struct ether_header *eth_header = (struct ether_header *) buf;
make_eth_header(eth_header, src, gw);
struct ip *ip_header = (struct ip*)(&eth_header[1]);
uint16_t len = htons(sizeof(struct ip) + sizeof(struct tcphdr) + tcp_send_opts_len);
uint16_t len = htons(sizeof(struct ip) + ZMAP_TCP_SYNOPT_TCP_HEADER_LEN + tcp_send_opts_len);
make_ip_header(ip_header, IPPROTO_TCP, len);
struct tcphdr *tcp_header = (struct tcphdr*)(&ip_header[1]);
make_tcp_header(tcp_header, dst_port, TH_SYN);
return EXIT_SUCCESS;
}

int tcpsynopt_make_packet(void *buf, UNUSED size_t *buf_len, ipaddr_n_t src_ip, ipaddr_n_t dst_ip,
int tcpsynopt_make_packet(void *buf, size_t *buf_len, ipaddr_n_t src_ip, ipaddr_n_t dst_ip,
uint8_t ttl, uint32_t *validation, int probe_num, __attribute__((unused)) void *arg)
{
struct ether_header *eth_header = (struct ether_header *)buf;
Expand All @@ -134,12 +135,14 @@ int tcpsynopt_make_packet(void *buf, UNUSED size_t *buf_len, ipaddr_n_t src_ip,
tcp_header->th_off = 5+tcp_send_opts_len/4; // default length = 5 + 9*32 bit options

tcp_header->th_sum = 0;
tcp_header->th_sum = tcp_checksum(sizeof(struct tcphdr)+tcp_send_opts_len,
tcp_header->th_sum = tcp_checksum(ZMAP_TCP_SYNOPT_TCP_HEADER_LEN+tcp_send_opts_len,
ip_header->ip_src.s_addr, ip_header->ip_dst.s_addr, tcp_header);

ip_header->ip_sum = 0;
ip_header->ip_sum = zmap_ip_checksum((unsigned short *) ip_header);

*buf_len = ZMAP_TCP_SYNOPT_PACKET_LEN+tcp_send_opts_len;

return EXIT_SUCCESS;
}

Expand Down Expand Up @@ -167,7 +170,7 @@ int tcpsynopt_validate_packet(const struct ip *ip_hdr, uint32_t len,
}
// (4*5 =) 20 bytes IP header + 20 bytes tcp hdr + 36 bytes = 76 byte
// reply packet may not contain any tcp options!
if ((4*ip_hdr->ip_hl + sizeof(struct tcphdr) + 0) > len) {
if ((4*ip_hdr->ip_hl + ZMAP_TCP_SYNOPT_TCP_HEADER_LEN + 0) > len) {
// buffer not large enough to contain expected tcp header
printf("buffer (%u) not large enough!\n" ,len);
return 0;
Expand Down Expand Up @@ -218,7 +221,7 @@ void tcpsynopt_process_packet(const u_char *packet,

probe_module_t module_tcp_synopt = {
.name = "tcp_synopt",
.max_packet_length = 54, // will be extended at runtime
.max_packet_length = ZMAP_TCP_SYNOPT_PACKET_LEN, // will be extended at runtime
// tcp, ack set or syn+ack (bit random)
.pcap_filter = "tcp && tcp[13] & 4 != 0 || tcp[13] == 18",
.pcap_snaplen = 96+10*4, //max len
Expand Down
6 changes: 3 additions & 3 deletions src/probe_modules/probe_modules.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ void fs_add_ipv6_fields(fieldset_t *fs, struct ip6_hdr *ipv6_hdr)
fs_add_string(fs, "saddr", make_ipv6_str(&(ipv6_hdr->ip6_src)), 1);
// TODO FIXME
// fs_add_uint64(fs, "saddr-raw", (uint64_t) ip->ip_src.s_addr);
fs_add_uint64(fs, "saddr-raw", (uint64_t) 0);
fs_add_uint64(fs, "saddr_raw", (uint64_t) 0);
fs_add_string(fs, "daddr", make_ipv6_str(&(ipv6_hdr->ip6_dst)), 1);
// fs_add_uint64(fs, "daddr-raw", (uint64_t) ip->ip_dst.s_addr);
fs_add_uint64(fs, "daddr-raw", (uint64_t) 0);
// fs_add_uint64(fs, "daddr_raw", (uint64_t) ip->ip_dst.s_addr);
fs_add_uint64(fs, "daddr_raw", (uint64_t) 0);
// fs_add_uint64(fs, "ipid", ntohs(ipv6->ip_id));
fs_add_uint64(fs, "ipid", 0);
fs_add_uint64(fs, "ttl", ipv6_hdr->ip6_ctlun.ip6_un1.ip6_un1_hlim);
Expand Down

0 comments on commit 6d6c5d7

Please sign in to comment.