#include <sys/types.h>
#include <stdio.h>
#include <stddef.h>
#include "interface.h"
#include "extract.h"
#define INITIATION 1
#define RESPONSE 2
#define COOKIE 3
#define DATA 4
struct wg_initiation {
uint32_t type;
uint32_t sender;
uint8_t fill[140];
};
struct wg_response {
uint32_t type;
uint32_t sender;
uint32_t receiver;
uint8_t fill[80];
};
struct wg_cookie {
uint32_t type;
uint32_t receiver;
uint8_t fill[56];
};
struct wg_data {
uint32_t type;
uint32_t receiver;
uint64_t nonce;
uint8_t mac[16];
};
uint32_t
wg_match(const u_char *bp, u_int length)
{
uint32_t type;
if (length < sizeof(type))
return 0;
if (snapend - bp < sizeof(type)) {
return (0);
}
type = EXTRACT_LE_32BITS(bp);
if (type == INITIATION && length == sizeof(struct wg_initiation))
return INITIATION;
if (type == RESPONSE && length == sizeof(struct wg_response))
return RESPONSE;
if (type == COOKIE && length == sizeof(struct wg_cookie))
return COOKIE;
if (type == DATA && length >= sizeof(struct wg_data))
return DATA;
return 0;
}
void
wg_print(const u_char *bp, u_int length)
{
uint32_t type;
uint64_t datalength;
struct wg_initiation *initiation = (void *)bp;
struct wg_response *response = (void *)bp;
struct wg_cookie *cookie = (void *)bp;
struct wg_data *data = (void *)bp;
u_int caplen;
caplen = snapend - bp;
if (caplen < sizeof(type))
goto trunc;
if ((type = wg_match(bp, length)) == 0) {
printf("[wg] unknown");
return;
}
switch (type) {
case INITIATION:
printf("[wg] initiation ");
if (caplen < offsetof(struct wg_initiation, fill))
goto trunc;
printf("from 0x%08x", letoh32(initiation->sender));
break;
case RESPONSE:
printf("[wg] response ");
if (caplen < offsetof(struct wg_response, fill))
goto trunc;
printf("from 0x%08x to 0x%08x",
letoh32(response->sender), letoh32(response->receiver));
break;
case COOKIE:
printf("[wg] cookie ");
if (caplen < offsetof(struct wg_cookie, fill))
goto trunc;
printf(" to 0x%08x", letoh32(cookie->receiver));
break;
case DATA:
datalength = length - sizeof(struct wg_data);
if (datalength != 0)
printf("[wg] data length %llu ", datalength);
else
printf("[wg] keepalive ");
if (caplen < offsetof(struct wg_data, mac))
goto trunc;
printf("to 0x%08x nonce %llu",
letoh32(data->receiver), EXTRACT_LE_64BITS(&data->nonce));
break;
}
return;
trunc:
printf("[|wg]");
}