xref: /openbsd-src/usr.sbin/tcpdump/print-wg.c (revision 549eab54c481a9723ec27ef854ab4aaff2e31879)
1*549eab54Svisa /*	$OpenBSD: print-wg.c,v 1.7 2021/09/16 12:35:20 visa Exp $ */
22338d7fcStb 
396cf6233Sdlg /*
496cf6233Sdlg  * Copyright (C) 2015-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
596cf6233Sdlg  * Copyright (C) 2019-2020 Matt Dunwoodie <ncon@noconroy.net>
696cf6233Sdlg  *
796cf6233Sdlg  * Permission to use, copy, modify, and distribute this software for any
896cf6233Sdlg  * purpose with or without fee is hereby granted, provided that the above
996cf6233Sdlg  * copyright notice and this permission notice appear in all copies.
1096cf6233Sdlg  *
1196cf6233Sdlg  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1296cf6233Sdlg  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1396cf6233Sdlg  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1496cf6233Sdlg  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1596cf6233Sdlg  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1696cf6233Sdlg  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1796cf6233Sdlg  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1896cf6233Sdlg  */
1996cf6233Sdlg 
2096cf6233Sdlg #include <sys/types.h>
2196cf6233Sdlg 
2296cf6233Sdlg #include <stdio.h>
237e0f8616Sdlg #include <stddef.h>
2496cf6233Sdlg 
2596cf6233Sdlg #include "interface.h"
2696cf6233Sdlg #include "extract.h"
2796cf6233Sdlg 
287c09302aSbluhm #define INITIATION	1
297c09302aSbluhm #define RESPONSE	2
307c09302aSbluhm #define COOKIE		3
317c09302aSbluhm #define DATA		4
3296cf6233Sdlg 
3396cf6233Sdlg struct wg_initiation {
3496cf6233Sdlg 	uint32_t	type;
3596cf6233Sdlg 	uint32_t	sender;
3696cf6233Sdlg 	uint8_t		fill[140]; /* Includes ephemeral + MAC */
3796cf6233Sdlg };
3896cf6233Sdlg 
3996cf6233Sdlg struct wg_response {
4096cf6233Sdlg 	uint32_t	type;
4196cf6233Sdlg 	uint32_t	sender;
4296cf6233Sdlg 	uint32_t	receiver;
4396cf6233Sdlg 	uint8_t		fill[80]; /* Includes ephemeral + MAC */
4496cf6233Sdlg };
4596cf6233Sdlg 
4696cf6233Sdlg struct wg_cookie {
4796cf6233Sdlg 	uint32_t	type;
4896cf6233Sdlg 	uint32_t	receiver;
4996cf6233Sdlg 	uint8_t		fill[56]; /* Includes nonce + encrypted cookie */
5096cf6233Sdlg };
5196cf6233Sdlg 
5296cf6233Sdlg struct wg_data {
5396cf6233Sdlg 	uint32_t	type;
5496cf6233Sdlg 	uint32_t	receiver;
5596cf6233Sdlg 	uint64_t	nonce;
5696cf6233Sdlg 	/* uint8_t	data[variable]; - Variable length data */
5796cf6233Sdlg 	uint8_t		mac[16];
5896cf6233Sdlg };
5996cf6233Sdlg 
6096cf6233Sdlg /*
6196cf6233Sdlg  * Check if packet is a WireGuard packet, as WireGuard may run on any port.
6296cf6233Sdlg  */
6396cf6233Sdlg uint32_t
wg_match(const u_char * bp,u_int length)6496cf6233Sdlg wg_match(const u_char *bp, u_int length)
6596cf6233Sdlg {
6696cf6233Sdlg 	uint32_t type;
6796cf6233Sdlg 
6822b46301Sdlg 	if (length < sizeof(type))
6996cf6233Sdlg 		return 0;
7096cf6233Sdlg 
7122b46301Sdlg 	if (snapend - bp < sizeof(type)) {
7222b46301Sdlg                 /*
7322b46301Sdlg 		 * we don't have enough bytes to tell if it is wg,
7422b46301Sdlg                  * so don't claim it, and don't claim it's truncated
7522b46301Sdlg                  * wireguard either.
7622b46301Sdlg 		 */
7722b46301Sdlg 		return (0);
7822b46301Sdlg 	}
7922b46301Sdlg 
8096cf6233Sdlg 	type = EXTRACT_LE_32BITS(bp);
8196cf6233Sdlg 
8296cf6233Sdlg 	if (type == INITIATION && length == sizeof(struct wg_initiation))
8396cf6233Sdlg 		return INITIATION;
8496cf6233Sdlg 	if (type == RESPONSE && length == sizeof(struct wg_response))
8596cf6233Sdlg 		return RESPONSE;
8696cf6233Sdlg 	if (type == COOKIE && length == sizeof(struct wg_cookie))
8796cf6233Sdlg 		return COOKIE;
8896cf6233Sdlg 	if (type == DATA && length >= sizeof(struct wg_data))
8996cf6233Sdlg 		return DATA;
9096cf6233Sdlg 	return 0;
9196cf6233Sdlg }
9296cf6233Sdlg 
9396cf6233Sdlg /*
9496cf6233Sdlg  * Print WireGuard packet
9596cf6233Sdlg  */
9696cf6233Sdlg void
wg_print(const u_char * bp,u_int length)9796cf6233Sdlg wg_print(const u_char *bp, u_int length)
9896cf6233Sdlg {
9996cf6233Sdlg 	uint32_t		 type;
10096cf6233Sdlg 	uint64_t		 datalength;
10196cf6233Sdlg 	struct wg_initiation	*initiation = (void *)bp;
10296cf6233Sdlg 	struct wg_response	*response = (void *)bp;
10396cf6233Sdlg 	struct wg_cookie	*cookie = (void *)bp;
10496cf6233Sdlg 	struct wg_data		*data = (void *)bp;
1057e0f8616Sdlg 	u_int			 caplen;
1067e0f8616Sdlg 
1077e0f8616Sdlg 	caplen = snapend - bp;
1087e0f8616Sdlg 	if (caplen < sizeof(type))
1097e0f8616Sdlg 		goto trunc;
11096cf6233Sdlg 
11196cf6233Sdlg 	if ((type = wg_match(bp, length)) == 0) {
11296cf6233Sdlg 		/* doesn't match */
11396cf6233Sdlg 		printf("[wg] unknown");
11496cf6233Sdlg 		return;
11596cf6233Sdlg 	}
11696cf6233Sdlg 
11796cf6233Sdlg 	switch (type) {
11896cf6233Sdlg 	case INITIATION:
1197e0f8616Sdlg 		printf("[wg] initiation ");
1207e0f8616Sdlg 		if (caplen < offsetof(struct wg_initiation, fill))
1217e0f8616Sdlg 			goto trunc;
1227e0f8616Sdlg 		printf("from 0x%08x", letoh32(initiation->sender));
12396cf6233Sdlg 		break;
12496cf6233Sdlg 	case RESPONSE:
1257e0f8616Sdlg 		printf("[wg] response ");
1267e0f8616Sdlg 		if (caplen < offsetof(struct wg_response, fill))
1277e0f8616Sdlg 			goto trunc;
1287e0f8616Sdlg 		printf("from 0x%08x to 0x%08x",
12996cf6233Sdlg 		    letoh32(response->sender), letoh32(response->receiver));
13096cf6233Sdlg 		break;
13196cf6233Sdlg 	case COOKIE:
1327e0f8616Sdlg 		printf("[wg] cookie ");
1337e0f8616Sdlg 		if (caplen < offsetof(struct wg_cookie, fill))
1347e0f8616Sdlg 			goto trunc;
1357e0f8616Sdlg 		printf(" to 0x%08x", letoh32(cookie->receiver));
13696cf6233Sdlg 		break;
13796cf6233Sdlg 	case DATA:
13896cf6233Sdlg 		datalength = length - sizeof(struct wg_data);
13996cf6233Sdlg 		if (datalength != 0)
1407e0f8616Sdlg 			printf("[wg] data length %llu ", datalength);
14196cf6233Sdlg 		else
1427e0f8616Sdlg 			printf("[wg] keepalive ");
1437e0f8616Sdlg 		if (caplen < offsetof(struct wg_data, mac))
1447e0f8616Sdlg 			goto trunc;
145*549eab54Svisa 		/* data->nonce may be unaligned. */
1467e0f8616Sdlg 		printf("to 0x%08x nonce %llu",
147*549eab54Svisa 		    letoh32(data->receiver), EXTRACT_LE_64BITS(&data->nonce));
14896cf6233Sdlg 		break;
14996cf6233Sdlg 	}
15096cf6233Sdlg 	return;
1517e0f8616Sdlg 
1527e0f8616Sdlg trunc:
1537e0f8616Sdlg 	printf("[|wg]");
15496cf6233Sdlg }
155