summaryrefslogtreecommitdiffstats
path: root/pimd/pim6_mld_protocol.h
blob: 08d7871986f50776ed4691bc15a0a09f6e5eae8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * MLD protocol definitions
 * Copyright (C) 2022  David Lamparter for NetDEF, Inc.
 */

#ifndef _PIM6_MLD_PROTOCOL_H
#define _PIM6_MLD_PROTOCOL_H

#include <stdalign.h>
#include <stdint.h>

/* There is a struct icmp6_hdr provided by OS, but it includes 4 bytes of data.
 * Not helpful for us if we want to put the MLD struct after it.
 */

struct icmp6_plain_hdr {
	uint8_t icmp6_type;
	uint8_t icmp6_code;
	uint16_t icmp6_cksum;
};
static_assert(sizeof(struct icmp6_plain_hdr) == 4, "struct mismatch");
static_assert(alignof(struct icmp6_plain_hdr) <= 4, "struct mismatch");

/* for MLDv1 query, report and leave all use the same packet format */
struct mld_v1_pkt {
	uint16_t max_resp_code;
	uint16_t rsvd0;
	struct in6_addr grp;
};
static_assert(sizeof(struct mld_v1_pkt) == 20, "struct mismatch");
static_assert(alignof(struct mld_v1_pkt) <= 4, "struct mismatch");


struct mld_v2_query_hdr {
	uint16_t max_resp_code;
	uint16_t rsvd0;
	struct in6_addr grp;
	uint8_t flags;
	uint8_t qqic;
	uint16_t n_src;
	struct in6_addr srcs[0];
};
static_assert(sizeof(struct mld_v2_query_hdr) == 24, "struct mismatch");
static_assert(alignof(struct mld_v2_query_hdr) <= 4, "struct mismatch");


struct mld_v2_report_hdr {
	uint16_t rsvd;
	uint16_t n_records;
};
static_assert(sizeof(struct mld_v2_report_hdr) == 4, "struct mismatch");
static_assert(alignof(struct mld_v2_report_hdr) <= 4, "struct mismatch");


struct mld_v2_rec_hdr {
	uint8_t type;
	uint8_t aux_len;
	uint16_t n_src;
	struct in6_addr grp;
	struct in6_addr srcs[0];
};
static_assert(sizeof(struct mld_v2_rec_hdr) == 20, "struct mismatch");
static_assert(alignof(struct mld_v2_rec_hdr) <= 4, "struct mismatch");

/* clang-format off */
enum icmp6_mld_type {
	ICMP6_MLD_QUERY			= 130,
	ICMP6_MLD_V1_REPORT		= 131,
	ICMP6_MLD_V1_DONE		= 132,
	ICMP6_MLD_V2_REPORT		= 143,
};

enum mld_v2_rec_type {
	MLD_RECTYPE_IS_INCLUDE		= 1,
	MLD_RECTYPE_IS_EXCLUDE		= 2,
	MLD_RECTYPE_CHANGE_TO_INCLUDE	= 3,
	MLD_RECTYPE_CHANGE_TO_EXCLUDE	= 4,
	MLD_RECTYPE_ALLOW_NEW_SOURCES	= 5,
	MLD_RECTYPE_BLOCK_OLD_SOURCES	= 6,
};
/* clang-format on */

/* helper functions */

static inline unsigned int mld_max_resp_decode(uint16_t wire)
{
	uint16_t code = ntohs(wire);
	uint8_t exp;

	if (code < 0x8000)
		return code;
	exp = (code >> 12) & 0x7;
	return ((code & 0xfff) | 0x1000) << (exp + 3);
}

static inline uint16_t mld_max_resp_encode(uint32_t value)
{
	uint16_t code;
	uint8_t exp;

	if (value < 0x8000)
		code = value;
	else {
		exp = 16 - __builtin_clz(value);
		code = (value >> (exp + 3)) & 0xfff;
		code |= 0x8000 | (exp << 12);
	}
	return htons(code);
}

#endif /* _PIM6_MLD_PROTOCOL_H */