FreeBSD port initial checkin. This is based on 5cd6f3294c,

September 9, 2013, before CMake changes. Includes proto_headers.h.
Most or all of __FREEBSD__ changes should, IMHO, be merged back into Linux version.
This should build on Linux with __FREEBSD__ defined.
This commit is contained in:
William Kelly
2013-10-16 22:12:11 -05:00
parent 5cd6f3294c
commit 833c781d94
17 changed files with 862 additions and 59 deletions

View File

@@ -15,12 +15,14 @@
#include <unistd.h>
#include <string.h>
#ifndef __FREEBSD__
#include <netinet/ether.h>
#endif
#include <netinet/in.h> /* wbk order */
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "probe_modules.h"
@@ -35,11 +37,20 @@ int icmp_echo_init_perthread(void* buf, macaddr_t *src,
{
memset(buf, 0, MAX_PACKET_SIZE);
#ifdef __FREEBSD__
struct zmap_ethhdr *eth_header = (struct zmap_ethhdr *)buf;
#else
struct ethhdr *eth_header = (struct ethhdr *)buf;
#endif
make_eth_header(eth_header, src, gw);
#ifdef __FREEBSD__
struct zmap_iphdr *ip_header = (struct zmap_iphdr*)(&eth_header[1]);
uint16_t len = htons(sizeof(struct zmap_iphdr) + sizeof(struct icmp) - 8);
#else
struct iphdr *ip_header = (struct iphdr*)(&eth_header[1]);
uint16_t len = htons(sizeof(struct iphdr) + sizeof(struct icmp) - 8);
#endif
make_ip_header(ip_header, IPPROTO_ICMP, len);
struct icmp *icmp_header = (struct icmp*)(&ip_header[1]);
@@ -51,13 +62,23 @@ int icmp_echo_init_perthread(void* buf, macaddr_t *src,
int icmp_echo_make_packet(void *buf, ipaddr_n_t src_ip, ipaddr_n_t dst_ip,
uint32_t *validation, __attribute__((unused))int probe_num)
{
#ifdef __FREEBSD__
struct zmap_ethhdr *eth_header = (struct zmap_ethhdr *)buf;
struct zmap_iphdr *ip_header = (struct zmap_iphdr*)(&eth_header[1]);
#else
struct ethhdr *eth_header = (struct ethhdr *)buf;
struct iphdr *ip_header = (struct iphdr*)(&eth_header[1]);
struct icmp *icmp_header = (struct icmp*)(&ip_header[1]);
#endif
struct icmp *icmp_header = (struct icmp*)(&ip_header[1]); /* TODO: struct icmp? */
uint16_t icmp_idnum = validation[2] & 0xFFFF;
#ifdef __FREEBSD__
ip_header->saddr.s_addr = src_ip;
ip_header->daddr.s_addr = dst_ip;
#else
ip_header->saddr = src_ip;
ip_header->daddr = dst_ip;
#endif
icmp_header->icmp_id = icmp_idnum;
icmp_header->icmp_cksum = 0;
@@ -71,8 +92,13 @@ int icmp_echo_make_packet(void *buf, ipaddr_n_t src_ip, ipaddr_n_t dst_ip,
void icmp_echo_print_packet(FILE *fp, void* packet)
{
#ifdef __FREEBSD__
struct zmap_ethhdr *ethh = (struct zmap_ethhdr *) packet;
struct zmap_iphdr *iph = (struct zmap_iphdr *) &ethh[1];
#else
struct ethhdr *ethh = (struct ethhdr *) packet;
struct iphdr *iph = (struct iphdr *) &ethh[1];
#endif
struct icmp *icmp_header = (struct icmp*)(&iph[1]);
fprintf(fp, "icmp { type: %u | code: %u "
@@ -87,10 +113,13 @@ void icmp_echo_print_packet(FILE *fp, void* packet)
fprintf(fp, "------------------------------------------------------\n");
}
#ifdef __FREEBSD__
int icmp_validate_packet(const struct zmap_iphdr *ip_hdr,
uint32_t len, uint32_t *src_ip, uint32_t *validation)
#else
int icmp_validate_packet(const struct iphdr *ip_hdr,
uint32_t len, uint32_t *src_ip, uint32_t *validation)
#endif
{
if (ip_hdr->protocol != IPPROTO_ICMP) {
return 0;
@@ -101,27 +130,50 @@ int icmp_validate_packet(const struct iphdr *ip_hdr,
return 0;
}
#ifdef __FREEBSD__
struct zmap_icmphdr *icmp_h = (struct zmap_icmphdr*)((char *)ip_hdr + 4*ip_hdr->ihl);
#else
struct icmphdr *icmp_h = (struct icmphdr*)((char *)ip_hdr + 4*ip_hdr->ihl);
#endif
uint16_t icmp_idnum = icmp_h->un.echo.id;
// ICMP validation is tricky: for some packet types, we must look inside
// the payload
if (icmp_h->type == ICMP_TIME_EXCEEDED || icmp_h->type == ICMP_DEST_UNREACH) {
if ((4*ip_hdr->ihl + sizeof(struct icmphdr) +
#ifdef __FREEBSD__
sizeof(struct zmap_iphdr)) > len) {
#else
sizeof(struct iphdr)) > len) {
#endif
return 0;
}
#ifdef __FREEBSD__
struct zmap_iphdr *ip_inner = (struct zmap_iphdr *)(icmp_h + 1);
if ((4*ip_hdr->ihl + sizeof(struct zmap_icmphdr) +
4*ip_inner->ihl + sizeof(struct zmap_icmphdr)) > len) {
#else
struct iphdr *ip_inner = (struct iphdr *)(icmp_h + 1);
if ((4*ip_hdr->ihl + sizeof(struct icmphdr) +
4*ip_inner->ihl + sizeof(struct icmphdr)) > len) {
#endif
return 0;
}
#ifdef __FREEBSD__
struct zmap_icmphdr *icmp_inner = (struct zmap_icmphdr*)((char *)ip_inner + 4 *ip_hdr->ihl);
#else
struct icmphdr *icmp_inner = (struct icmphdr*)((char *)ip_inner + 4 *ip_hdr->ihl);
#endif
// Regenerate validation and icmp id based off inner payload
icmp_idnum = icmp_inner->un.echo.id;
#ifdef __FREEBSD__
*src_ip = ip_inner->daddr.s_addr;
validate_gen(ip_hdr->daddr.s_addr, ip_inner->daddr.s_addr, (uint8_t *)validation);
#else
*src_ip = ip_inner->daddr;
validate_gen(ip_hdr->daddr, ip_inner->daddr, (uint8_t *)validation);
#endif
}
// validate icmp id
@@ -135,8 +187,13 @@ int icmp_validate_packet(const struct iphdr *ip_hdr,
void icmp_echo_process_packet(const u_char *packet,
__attribute__((unused)) uint32_t len, fieldset_t *fs)
{
#ifdef __FREEBSD__
struct zmap_iphdr *ip_hdr = (struct zmap_iphdr *)&packet[sizeof(struct zmap_ethhdr)];
struct zmap_icmphdr *icmp_hdr = (struct zmap_icmphdr*)((char *)ip_hdr + 4 *ip_hdr->ihl);
#else
struct iphdr *ip_hdr = (struct iphdr *)&packet[sizeof(struct ethhdr)];
struct icmphdr *icmp_hdr = (struct icmphdr*)((char *)ip_hdr + 4 *ip_hdr->ihl);
#endif
fs_add_uint64(fs, "type", icmp_hdr->type);
fs_add_uint64(fs, "code", icmp_hdr->code);
fs_add_uint64(fs, "icmp-id", ntohs(icmp_hdr->un.echo.id));

View File

@@ -15,9 +15,16 @@
#include <string.h>
#include <assert.h>
#ifdef __FREEBSD__ // TODO: __FREEBSD_INCLUDES__?
/* TODO: This may break Linux, might need to remove #ifdef here entirely */
#include <netinet/in.h> /* wbk needed before netinet/ip.h */
#endif
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/ether.h>
#ifdef __FREEBSD__
#else
#include <netinet/ether.h>
#endif
#include <sys/socket.h>
#include <netinet/in.h>
@@ -40,12 +47,25 @@ int synscan_init_perthread(void* buf, macaddr_t *src,
macaddr_t *gw, port_h_t dst_port)
{
memset(buf, 0, MAX_PACKET_SIZE);
#ifdef __FREEBSD__
struct zmap_ethhdr *eth_header = (struct zmap_ethhdr *)buf;
#else
struct ethhdr *eth_header = (struct ethhdr *)buf;
#endif
make_eth_header(eth_header, src, gw);
#ifdef __FREEBSD__
struct zmap_iphdr *ip_header = (struct zmap_iphdr*)(&eth_header[1]);
uint16_t len = htons(sizeof(struct zmap_iphdr) + sizeof(struct zmap_tcphdr));
#else
struct iphdr *ip_header = (struct iphdr*)(&eth_header[1]);
uint16_t len = htons(sizeof(struct iphdr) + sizeof(struct tcphdr));
#endif
make_ip_header(ip_header, IPPROTO_TCP, len);
#ifdef __FREEBSD__
struct zmap_tcphdr *tcp_header = (struct zmap_tcphdr*)(&ip_header[1]);
#else
struct tcphdr *tcp_header = (struct tcphdr*)(&ip_header[1]);
#endif
make_tcp_header(tcp_header, dst_port);
return EXIT_SUCCESS;
}
@@ -53,20 +73,45 @@ int synscan_init_perthread(void* buf, macaddr_t *src,
int synscan_make_packet(void *buf, ipaddr_n_t src_ip, ipaddr_n_t dst_ip,
uint32_t *validation, int probe_num)
{
#ifdef __FREEBSD__
struct zmap_ethhdr *eth_header = (struct zmap_ethhdr *)buf;
struct zmap_iphdr *ip_header = (struct zmap_iphdr*)(&eth_header[1]);
struct zmap_tcphdr *tcp_header = (struct zmap_tcphdr*)(&ip_header[1]); /* How does this work? What about options? */
#else
struct ethhdr *eth_header = (struct ethhdr *)buf;
struct iphdr *ip_header = (struct iphdr*)(&eth_header[1]);
struct tcphdr *tcp_header = (struct tcphdr*)(&ip_header[1]);
#endif
uint32_t tcp_seq = validation[0];
#ifdef __FREEBSD__
ip_header->saddr.s_addr = src_ip;
ip_header->daddr.s_addr = dst_ip;
#else
ip_header->saddr = src_ip;
ip_header->daddr = dst_ip;
#endif
tcp_header->source = htons(get_src_port(num_ports,
probe_num, validation));
tcp_header->seq = tcp_seq;
tcp_header->check = 0;
#ifdef __FREEBSD__
tcp_header->check = tcp_checksum(sizeof(struct zmap_tcphdr),
ip_header->saddr.s_addr, ip_header->daddr.s_addr, tcp_header);
/* wbk Set TCP data offset. I think Linux SOCK_RAW might have set this for us
on Linux. Hardcoding for now. */
//tcp_header->th_offx2 = 0x50;
//tcp_header->th_flags = 0x02;
/* If we ever add TCP options, we'll need to calculate header length in words and replace
5 with that. */
tcp_header->th_offx2 = (0x5 << 4);
tcp_header->th_flags = TH_SYN;
#else
tcp_header->check = tcp_checksum(sizeof(struct tcphdr),
ip_header->saddr, ip_header->daddr, tcp_header);
#endif
ip_header->check = 0;
ip_header->check = ip_checksum((unsigned short *) ip_header);
@@ -76,9 +121,15 @@ int synscan_make_packet(void *buf, ipaddr_n_t src_ip, ipaddr_n_t dst_ip,
void synscan_print_packet(FILE *fp, void* packet)
{
#ifdef __FREEBSD__
struct zmap_ethhdr *ethh = (struct zmap_ethhdr *) packet;
struct zmap_iphdr *iph = (struct zmap_iphdr *) &ethh[1];
struct zmap_tcphdr *tcph = (struct zmap_tcphdr *) &iph[1];
#else
struct ethhdr *ethh = (struct ethhdr *) packet;
struct iphdr *iph = (struct iphdr *) &ethh[1];
struct tcphdr *tcph = (struct tcphdr *) &iph[1];
#endif
fprintf(fp, "tcp { source: %u | dest: %u | seq: %u | checksum: %u }\n",
ntohs(tcph->source),
ntohs(tcph->dest),
@@ -89,9 +140,15 @@ void synscan_print_packet(FILE *fp, void* packet)
fprintf(fp, "------------------------------------------------------\n");
}
#ifdef __FREEBSD__
int synscan_validate_packet(const struct zmap_iphdr *ip_hdr, uint32_t len,
__attribute__((unused))uint32_t *src_ip,
uint32_t *validation)
#else
int synscan_validate_packet(const struct iphdr *ip_hdr, uint32_t len,
__attribute__((unused))uint32_t *src_ip,
uint32_t *validation)
#endif
{
if (ip_hdr->protocol != IPPROTO_TCP) {
return 0;
@@ -100,7 +157,11 @@ int synscan_validate_packet(const struct iphdr *ip_hdr, uint32_t len,
// buffer not large enough to contain expected tcp header
return 0;
}
#ifdef __FREEBSD__
struct zmap_tcphdr *tcp = (struct zmap_tcphdr*)((char *)ip_hdr + 4*ip_hdr->ihl);/*TODO*/
#else
struct tcphdr *tcp = (struct tcphdr*)((char *)ip_hdr + 4*ip_hdr->ihl);
#endif
uint16_t sport = tcp->source;
uint16_t dport = tcp->dest;
// validate source port
@@ -121,17 +182,31 @@ int synscan_validate_packet(const struct iphdr *ip_hdr, uint32_t len,
void synscan_process_packet(const u_char *packet,
__attribute__((unused)) uint32_t len, fieldset_t *fs)
{
#ifdef __FREEBSD__
struct zmap_iphdr *ip_hdr = (struct zmap_iphdr *)&packet[sizeof(struct zmap_ethhdr)];
struct zmap_tcphdr *tcp = (struct zmap_tcphdr*)((char *)ip_hdr
+ (sizeof(struct zmap_iphdr)));
#else
struct iphdr *ip_hdr = (struct iphdr *)&packet[sizeof(struct ethhdr)];
struct tcphdr *tcp = (struct tcphdr*)((char *)ip_hdr
+ (sizeof(struct iphdr)));
#endif
fs_add_uint64(fs, "sport", (uint64_t) ntohs(tcp->source));
fs_add_uint64(fs, "dport", (uint64_t) ntohs(tcp->dest));
fs_add_uint64(fs, "seqnum", (uint64_t) ntohl(tcp->seq));
fs_add_uint64(fs, "acknum", (uint64_t) ntohl(tcp->ack_seq));
#ifdef __FREEBSD__
fs_add_uint64(fs, "window", (uint64_t) ntohs(tcp->th_win));
#else
fs_add_uint64(fs, "window", (uint64_t) ntohs(tcp->window));
#endif
#ifdef __FREEBSD__
if (tcp->th_flags & TH_RST) { // RST packet
#else
if (tcp->rst) { // RST packet
#endif
fs_add_string(fs, "classification", (char*) "rst", 0);
fs_add_uint64(fs, "success", 0);
} else { // SYNACK packet

View File

@@ -16,12 +16,17 @@
#include <assert.h>
#include <netinet/udp.h>
#include <netinet/in.h> /* wbk order */
#include <netinet/ip.h>
#include <netinet/ether.h>
#include <netinet/ip_icmp.h>
#ifdef __FREEBSD__
#include "proto_headers.h" /* wbk TODO: test */
#else
#include <netinet/ether.h>
#include <netinet/ether.h> /* wbk TODO: Probably need proto_headers.h instead */
#include <netinet/ip_icmp.h>
#endif
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "probe_modules.h"
@@ -159,20 +164,36 @@ int udp_init_perthread(void* buf, macaddr_t *src,
macaddr_t *gw, __attribute__((unused)) port_h_t dst_port)
{
memset(buf, 0, MAX_PACKET_SIZE);
#ifdef __FREEBSD__
struct zmap_ethhdr *eth_header = (struct zmap_ethhdr *)buf;
make_eth_header(eth_header, src, gw);
struct zmap_iphdr *ip_header = (struct zmap_iphdr*)(&eth_header[1]);
uint16_t len = htons(sizeof(struct zmap_iphdr) + sizeof(struct zmap_udphdr) + udp_send_msg_len);
#else
struct ethhdr *eth_header = (struct ethhdr *)buf;
make_eth_header(eth_header, src, gw);
struct iphdr *ip_header = (struct iphdr*)(&eth_header[1]);
uint16_t len = htons(sizeof(struct iphdr) + sizeof(struct udphdr) + udp_send_msg_len);
#endif
make_ip_header(ip_header, IPPROTO_UDP, len);
#ifdef __FREEBSD__
struct zmap_udphdr *udp_header = (struct zmap_udphdr*)(&ip_header[1]);
#else
struct udphdr *udp_header = (struct udphdr*)(&ip_header[1]);
#endif
len = sizeof(struct udphdr) + udp_send_msg_len;
make_udp_header(udp_header, zconf.target_port, len);
char* payload = (char*)(&udp_header[1]);
#ifdef __FREEBSD__
module_udp.packet_length = sizeof(struct zmap_ethhdr) + sizeof(struct zmap_iphdr)
+ sizeof(struct zmap_udphdr) + udp_send_msg_len;
#else
module_udp.packet_length = sizeof(struct ethhdr) + sizeof(struct iphdr)
+ sizeof(struct udphdr) + udp_send_msg_len;
#endif
assert(module_udp.packet_length <= MAX_PACKET_SIZE);
memcpy(payload, udp_send_msg, udp_send_msg_len);
@@ -183,12 +204,24 @@ int udp_init_perthread(void* buf, macaddr_t *src,
int udp_make_packet(void *buf, ipaddr_n_t src_ip, ipaddr_n_t dst_ip,
uint32_t *validation, int probe_num)
{
#ifdef __FREEBSD__
struct zmap_ethhdr *eth_header = (struct zmap_ethhdr *)buf;
struct zmap_iphdr *ip_header = (struct zmap_iphdr*)(&eth_header[1]);
struct zmap_udphdr *udp_header = (struct zmap_udphdr*)(&ip_header[1]);
#else
struct ethhdr *eth_header = (struct ethhdr *)buf;
struct iphdr *ip_header = (struct iphdr*)(&eth_header[1]);
struct udphdr *udp_header = (struct udphdr*)(&ip_header[1]);
#endif
#ifdef __FREEBSD__
ip_header->saddr.s_addr = src_ip;
ip_header->daddr.s_addr = dst_ip;
#else
ip_header->saddr = src_ip;
ip_header->daddr = dst_ip;
#endif
udp_header->source = get_src_port(num_ports, probe_num,
validation);
ip_header->check = 0;
@@ -199,9 +232,15 @@ int udp_make_packet(void *buf, ipaddr_n_t src_ip, ipaddr_n_t dst_ip,
void udp_print_packet(FILE *fp, void* packet)
{
#ifdef __FREEBSD__
struct zmap_ethhdr *ethh = (struct zmap_ethhdr *) packet;
struct zmap_iphdr *iph = (struct zmap_iphdr *) &ethh[1];
struct zmap_udphdr *udph = (struct zmap_udphdr*)(&iph[1]);
#else
struct ethhdr *ethh = (struct ethhdr *) packet;
struct iphdr *iph = (struct iphdr *) &ethh[1];
struct udphdr *udph = (struct udphdr*)(&iph[1]);
#endif
fprintf(fp, "udp { source: %u | dest: %u | checksum: %u }\n",
ntohs(udph->source),
ntohs(udph->dest),
@@ -213,9 +252,16 @@ void udp_print_packet(FILE *fp, void* packet)
void udp_process_packet(const u_char *packet, UNUSED uint32_t len, fieldset_t *fs)
{
struct iphdr *ip_hdr = (struct iphdr *)&packet[sizeof(struct ethhdr)];
#ifdef __FREEBSD__
struct zmap_iphdr *ip_hdr = (struct zmap_iphdr *)&packet[sizeof(struct zmap_ethhdr)];
#else
#endif
if (ip_hdr->protocol == IPPROTO_UDP) {
#ifdef __FREEBSD__
struct zmap_udphdr *udp = (struct zmap_udphdr *)((char *)ip_hdr + ip_hdr->ihl * 4);
#else
struct udphdr *udp = (struct udphdr *)((char *)ip_hdr + ip_hdr->ihl * 4);
#endif
fs_add_string(fs, "classification", (char*) "udp", 0);
fs_add_uint64(fs, "success", 1);
fs_add_uint64(fs, "sport", ntohs(udp->source));
@@ -226,16 +272,21 @@ void udp_process_packet(const u_char *packet, UNUSED uint32_t len, fieldset_t *f
fs_add_null(fs, "icmp_unreach_str");
fs_add_binary(fs, "data", (ntohs(udp->len) - sizeof(struct udphdr)), (void*) &udp[1], 0);
} else if (ip_hdr->protocol == IPPROTO_ICMP) {
#ifdef __FREEBSD__
struct zmap_icmphdr *icmp = (struct zmap_icmphdr *)((char *)ip_hdr + ip_hdr->ihl * 4);
struct zmap_iphdr *ip_inner = (struct zmap_iphdr*)&icmp[1];
#else
struct icmphdr *icmp = (struct icmphdr *)((char *)ip_hdr + ip_hdr->ihl * 4);
struct iphdr *ip_inner = (struct iphdr*)&icmp[1];
#endif
// ICMP unreach comes from another server (not the one we sent a probe to);
// But we will fix up saddr to be who we sent the probe to, in case you care.
fs_modify_string(fs, "saddr", make_ip_str(ip_inner->daddr), 1);
fs_modify_string(fs, "saddr", make_ip_str(ip_inner->daddr.s_addr), 1);
fs_add_string(fs, "classification", (char*) "icmp-unreach", 0);
fs_add_uint64(fs, "success", 0);
fs_add_null(fs, "sport");
fs_add_null(fs, "dport");
fs_add_string(fs, "icmp_responder", make_ip_str(ip_hdr->saddr), 1);
fs_add_string(fs, "icmp_responder", make_ip_str(ip_hdr->saddr.s_addr), 1);
fs_add_uint64(fs, "icmp_type", icmp->type);
fs_add_uint64(fs, "icmp_code", icmp->code);
if (icmp->code <= ICMP_PREC_CUTOFF) {
@@ -257,41 +308,71 @@ void udp_process_packet(const u_char *packet, UNUSED uint32_t len, fieldset_t *f
}
}
#ifdef __FREEBSD__
int udp_validate_packet(const struct zmap_iphdr *ip_hdr, uint32_t len,
#else
int udp_validate_packet(const struct iphdr *ip_hdr, uint32_t len,
#endif
__attribute__((unused))uint32_t *src_ip, uint32_t *validation)
{
uint16_t dport, sport;
if (ip_hdr->protocol == IPPROTO_UDP) {
#ifdef __FREEBSD__
if ((4*ip_hdr->ihl + sizeof(struct zmap_udphdr)) > len) {
#else
if ((4*ip_hdr->ihl + sizeof(struct udphdr)) > len) {
#endif
// buffer not large enough to contain expected udp header
return 0;
}
#ifdef __FREEBSD__
struct zmap_udphdr *udp = (struct zmap_udphdr*)((char *)ip_hdr + 4*ip_hdr->ihl);
#else
struct udphdr *udp = (struct udphdr*)((char *)ip_hdr + 4*ip_hdr->ihl);
#endif
sport = ntohs(udp->dest);
dport = ntohs(udp->source);
} else if (ip_hdr->protocol == IPPROTO_ICMP) {
// UDP can return ICMP Destination unreach
// IP( ICMP( IP( UDP ) ) ) for a destination unreach
#ifdef __FREEBSD__
uint32_t min_len = 4*ip_hdr->ihl + sizeof(struct zmap_icmphdr)
+ sizeof(struct zmap_iphdr) + sizeof(struct zmap_udphdr);
#else
uint32_t min_len = 4*ip_hdr->ihl + sizeof(struct icmphdr)
+ sizeof(struct iphdr) + sizeof(struct udphdr);
#endif
if (len < min_len) {
// Not enough information for us to validate
return 0;
}
#ifdef __FREEBSD__
struct zmap_icmphdr *icmp = (struct zmap_icmphdr*)((char *)ip_hdr + 4*ip_hdr->ihl);
#else
struct icmphdr *icmp = (struct icmphdr*)((char *)ip_hdr + 4*ip_hdr->ihl);
#endif
if (icmp->type != ICMP_DEST_UNREACH) {
return 0;
}
#ifdef __FREEBSD__
struct zmap_iphdr *ip_inner = (struct zmap_iphdr*)&icmp[1];
// Now we know the actual inner ip length, we should recheck the buffer
if (len < 4*ip_inner->ihl - sizeof(struct zmap_iphdr) + min_len) {
#else
struct iphdr *ip_inner = (struct iphdr*)&icmp[1];
// Now we know the actual inner ip length, we should recheck the buffer
if (len < 4*ip_inner->ihl - sizeof(struct iphdr) + min_len) {
#endif
return 0;
}
// This is the packet we sent
#ifdef __FREEBSD__
struct zmap_udphdr *udp = (struct zmap_udphdr *)((char*)ip_inner + 4*ip_inner->ihl);
#else
struct udphdr *udp = (struct udphdr *)((char*)ip_inner + 4*ip_inner->ihl);
#endif
sport = ntohs(udp->source);
dport = ntohs(udp->dest);

View File

@@ -12,7 +12,9 @@
#include <stdio.h>
#include <string.h>
#ifndef __FREEBSD__
#include <netinet/ether.h>
#endif
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
@@ -24,6 +26,16 @@
#include "state.h"
#ifdef __FREEBSD__
#include "../proto_headers.h"
#endif
#ifdef __FREEBSD_INCLUDES__ /* some macros in Linux system headers */
#define ETH_ALEN ETHER_ADDR_LEN
#define ETH_P_IP ETYPE_IPV4 /* EtherType 0x800 */
#endif
#ifndef __FREEBSD__ /* TODO: */
void print_macaddr(struct ifreq* i)
{
printf("Device %s -> Ethernet %02x:%02x:%02x:%02x:%02x:%02x\n",
@@ -35,8 +47,13 @@ void print_macaddr(struct ifreq* i)
(int) ((unsigned char *) &i->ifr_hwaddr.sa_data)[4],
(int) ((unsigned char *) &i->ifr_hwaddr.sa_data)[5]);
}
#endif
#ifdef __FREEBSD__
void fprintf_ip_header(FILE *fp, struct zmap_iphdr *iph)
#else
void fprintf_ip_header(FILE *fp, struct iphdr *iph)
#endif
{
struct in_addr *s = (struct in_addr *) &(iph->saddr);
struct in_addr *d = (struct in_addr *) &(iph->daddr);
@@ -53,7 +70,11 @@ void fprintf_ip_header(FILE *fp, struct iphdr *iph)
ntohl(iph->check));
}
#ifdef __FREEBSD__
void fprintf_eth_header(FILE *fp, struct zmap_ethhdr *ethh)
#else
void fprintf_eth_header(FILE *fp, struct ethhdr *ethh)
#endif
{
fprintf(fp, "eth { shost: %02x:%02x:%02x:%02x:%02x:%02x | "
"dhost: %02x:%02x:%02x:%02x:%02x:%02x }\n",
@@ -71,14 +92,22 @@ void fprintf_eth_header(FILE *fp, struct ethhdr *ethh)
(int) ((unsigned char *) ethh->h_dest)[5]);
}
#ifdef __FREEBSD__
void make_eth_header(struct zmap_ethhdr *ethh, macaddr_t *src, macaddr_t *dst)
#else
void make_eth_header(struct ethhdr *ethh, macaddr_t *src, macaddr_t *dst)
#endif
{
memcpy(ethh->h_source, src, ETH_ALEN);
memcpy(ethh->h_dest, dst, ETH_ALEN);
ethh->h_proto = htons(ETH_P_IP);
}
#ifdef __FREEBSD__
void make_ip_header(struct zmap_iphdr *iph, uint8_t protocol, uint16_t len)
#else
void make_ip_header(struct iphdr *iph, uint8_t protocol, uint16_t len)
#endif
{
iph->ihl = 5; // Internet Header Length
iph->version = 4; // IPv4
@@ -99,7 +128,23 @@ void make_icmp_header(struct icmp *buf)
buf->icmp_code = 0;
buf->icmp_seq = 0;
}
/* TODO: This is sketchy when linking against other translation units.
Probably need to reconcile the struct names properly */
#ifdef __FREEBSD__
void make_tcp_header(struct zmap_tcphdr *tcp_header, port_h_t dest_port)
{
tcp_header->seq = random();
tcp_header->ack_seq = 0;
tcp_header->th_flags = 0;
tcp_header->th_flags = 5 << 4; /* data offset */
tcp_header->th_flags &= TH_SYN;
tcp_header->th_win = htons(65535);
tcp_header->check = 0;
tcp_header->th_urp = 0;
tcp_header->dest = (htons(dest_port));
#else
void make_tcp_header(struct tcphdr *tcp_header, port_h_t dest_port)
{
tcp_header->seq = random();
@@ -111,9 +156,14 @@ void make_tcp_header(struct tcphdr *tcp_header, port_h_t dest_port)
tcp_header->check = 0;
tcp_header->urg_ptr = 0;
tcp_header->dest = htons(dest_port);
#endif
}
#ifdef __FREEBSD__
void make_udp_header(struct zmap_udphdr *udp_header, port_h_t dest_port,
#else
void make_udp_header(struct udphdr *udp_header, port_h_t dest_port,
#endif
uint16_t len)
{
udp_header->dest = htons(dest_port);

View File

@@ -1,6 +1,11 @@
#include "state.h"
#include <netinet/ether.h>
#ifdef __FREEBSD__
#include "proto_headers.h"
#else
#include <netinet/ether.h>
#endif /* __FREEBSD__ */
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/ip_icmp.h>
@@ -11,17 +16,32 @@
#define MAX_PACKET_SIZE 4096
/* wbk - proceed w/ caution here w/ all the sizeof's */
typedef unsigned short __attribute__((__may_alias__)) alias_unsigned_short;
void make_eth_header(struct ethhdr *ethh, macaddr_t *src, macaddr_t *dst);
#ifdef __FREEBSD__
void make_eth_header(struct zmap_ethhdr *ethh, macaddr_t *src, macaddr_t *dst);
void make_ip_header(struct zmap_iphdr *iph, uint8_t, uint16_t);
void make_tcp_header(struct zmap_tcphdr*, port_h_t);
#else
void make_eth_header(struct ethhdr *ethh, macaddr_t *src, macaddr_t *dst);
void make_ip_header(struct iphdr *iph, uint8_t, uint16_t);
void make_tcp_header(struct tcphdr*, port_h_t);
#endif
void make_icmp_header(struct icmp *);
#ifdef __FREEBSD__
void make_udp_header(struct zmap_udphdr *udp_header, port_h_t dest_port,
uint16_t len);
void fprintf_ip_header(FILE *fp, struct zmap_iphdr *iph);
void fprintf_eth_header(FILE *fp, struct zmap_ethhdr *ethh);
#else
void make_udp_header(struct udphdr *udp_header, port_h_t dest_port,
uint16_t len);
void fprintf_ip_header(FILE *fp, struct iphdr *iph);
void fprintf_eth_header(FILE *fp, struct ethhdr *ethh);
#endif
static inline unsigned short in_checksum(unsigned short *ip_pkt, int len)
{
@@ -37,7 +57,11 @@ static inline unsigned short in_checksum(unsigned short *ip_pkt, int len)
__attribute__((unused)) static inline unsigned short ip_checksum(
unsigned short *buf)
{
#ifdef __FREEBSD__
return in_checksum(buf, (int) sizeof(struct zmap_iphdr));
#else
return in_checksum(buf, (int) sizeof(struct iphdr));
#endif
}
__attribute__((unused)) static inline unsigned short icmp_checksum(
@@ -47,7 +71,11 @@ __attribute__((unused)) static inline unsigned short icmp_checksum(
}
static __attribute__((unused)) uint16_t tcp_checksum(unsigned short len_tcp,
#ifdef __FREEBSD__
uint32_t saddr, uint32_t daddr, struct zmap_tcphdr *tcp_pkt)
#else
uint32_t saddr, uint32_t daddr, struct tcphdr *tcp_pkt)
#endif
{
alias_unsigned_short *src_addr = (alias_unsigned_short *) &saddr;
alias_unsigned_short *dest_addr = (alias_unsigned_short *) &daddr;

View File

@@ -16,7 +16,11 @@
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/if_packet.h>
#ifdef __FREEBSD__
#include "../proto_headers.h"
#else
#include <linux/if_packet.h>
#endif
#include "../../lib/logger.h"
#include "../fieldset.h"
@@ -54,10 +58,14 @@ void print_probe_modules(void)
}
}
#ifdef __FREEBSD__
void fs_add_ip_fields(fieldset_t *fs, struct zmap_iphdr *ip)
#else
void fs_add_ip_fields(fieldset_t *fs, struct iphdr *ip)
#endif
{
fs_add_string(fs, "saddr", make_ip_str(ip->saddr), 1);
fs_add_string(fs, "daddr", make_ip_str(ip->daddr), 1);
fs_add_string(fs, "saddr", make_ip_str(ip->saddr.s_addr), 1);
fs_add_string(fs, "daddr", make_ip_str(ip->daddr.s_addr), 1);
fs_add_uint64(fs, "ipid", ntohs(ip->id));
fs_add_uint64(fs, "ttl", ip->ttl);
}

View File

@@ -4,6 +4,8 @@
#ifndef PROBE_MODULES_H
#define PROBE_MODULES_H
#include "../proto_headers.h"
typedef struct probe_response_type {
const uint8_t is_success;
const char *name;
@@ -18,11 +20,15 @@ typedef int (*probe_make_packet_cb)(void* packetbuf, ipaddr_n_t src_ip,
uint32_t *validation, int probe_num);
typedef void (*probe_print_packet_cb)(FILE *, void* packetbuf);
typedef int (*probe_close_cb)(struct state_conf*,
typedef int (*probe_close_cb)(struct state_conf*,
struct state_send*, struct state_recv*);
#ifdef __FREEBSD__
typedef int (*probe_validate_packet_cb)(const struct zmap_iphdr *ip_hdr,
uint32_t len, uint32_t *src_ip, uint32_t *validation);
#else
typedef int (*probe_validate_packet_cb)(const struct iphdr *ip_hdr,
uint32_t len, uint32_t *src_ip, uint32_t *validation);
#endif
typedef void (*probe_classify_packet_cb)(const u_char* packetbuf,
uint32_t len, fieldset_t*);
@@ -51,7 +57,11 @@ typedef struct probe_module {
probe_module_t* get_probe_module_by_name(const char*);
#ifdef __FREEBSD__
void fs_add_ip_fields(fieldset_t *fs, struct zmap_iphdr *ip);
#else
void fs_add_ip_fields(fieldset_t *fs, struct iphdr *ip);
#endif
void fs_add_system_fields(fieldset_t *fs, int is_repeat, int in_cooldown);
void print_probe_modules(void);