summaryrefslogtreecommitdiffstats
path: root/include/proto.h
blob: c252a67d6ccdb81f5a58976b48ee7cd7e272d168 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#ifndef NFTABLES_PROTO_H
#define NFTABLES_PROTO_H

#include <nftables.h>
#include <datatype.h>
#include <linux/netfilter/nf_tables.h>

/**
 * enum proto_bases - protocol bases
 *
 * @PROTO_BASE_INVALID:		uninitialised, does not happen
 * @PROTO_BASE_LL_HDR:		link layer header
 * @PROTO_BASE_NETWORK_HDR:	network layer header
 * @PROTO_BASE_TRANSPORT_HDR:	transport layer header
 */
enum proto_bases {
	PROTO_BASE_INVALID,
	PROTO_BASE_LL_HDR,
	PROTO_BASE_NETWORK_HDR,
	PROTO_BASE_TRANSPORT_HDR,
	__PROTO_BASE_MAX
};
#define PROTO_BASE_MAX		(__PROTO_BASE_MAX - 1)

extern const char *proto_base_names[];
extern const char *proto_base_tokens[];

/**
 * struct proto_hdr_template - protocol header field description
 *
 * @token:	parser token describing the header field
 * @dtype:	data type of the header field
 * @offset:	offset of the header field from base
 * @len:	length of header field
 * @meta_key:	special case: meta expression key
 */
struct proto_hdr_template {
	const char			*token;
	const struct datatype		*dtype;
	uint16_t			offset;
	uint16_t			len;
	enum byteorder			byteorder;
	enum nft_meta_keys		meta_key;
};

#define PROTO_HDR_TEMPLATE(__token, __dtype,  __byteorder, __offset, __len)\
	{								\
		.token		= (__token),				\
		.dtype		= (__dtype),				\
		.byteorder	= (__byteorder),			\
		.offset		= (__offset),				\
		.len		= (__len),				\
	}

#define PROTO_META_TEMPLATE(__token, __dtype, __key, __len)		\
	{								\
		.token		= (__token),				\
		.dtype		= (__dtype),				\
		.meta_key	= (__key),				\
		.len		= (__len),				\
	}

#define PROTO_UPPER_MAX		16
#define PROTO_HDRS_MAX		20

/**
 * struct proto_desc - protocol header description
 *
 * @name:	protocol name
 * @base:	header base
 * @checksum_key: key of template containing checksum
 * @protocol_key: key of template containing upper layer protocol description
 * @length:	total size of the header, in bits
 * @protocols:	link to upper layer protocol descriptions indexed by protocol value
 * @templates:	header templates
 */
struct proto_desc {
	const char			*name;
	enum proto_bases		base;
	unsigned int			checksum_key;
	unsigned int			protocol_key;
	unsigned int			length;
	struct {
		unsigned int			num;
		const struct proto_desc		*desc;
	}				protocols[PROTO_UPPER_MAX];
	struct proto_hdr_template	templates[PROTO_HDRS_MAX];
};

#define PROTO_LINK(__num, __desc)	{ .num = (__num), .desc = (__desc), }

/**
 * struct hook_proto_desc - description of protocol constraints imposed by hook family
 *
 * @base:	protocol base of packets
 * @desc:	protocol description of packets
 */
struct hook_proto_desc {
	enum proto_bases		base;
	const struct proto_desc		*desc;
};

#define HOOK_PROTO_DESC(__base, __desc)	{ .base = (__base), .desc = (__desc), }

extern const struct hook_proto_desc hook_proto_desc[];

/**
 * struct dev_proto_desc - description of device LL protocol
 *
 * @desc:	protocol description
 * @type:	arphrd value
 */
struct dev_proto_desc {
	const struct proto_desc		*desc;
	uint16_t			type;
};

#define DEV_PROTO_DESC(__type, __desc)	{ .type = (__type), .desc = (__desc), }

extern int proto_dev_type(const struct proto_desc *desc, uint16_t *res);
extern const struct proto_desc *proto_dev_desc(uint16_t type);

/**
 * struct proto_ctx - protocol context
 *
 * @family:	hook family
 * @location:	location of the relational expression defining the context
 * @desc:	protocol description for this layer
 * @offset:	offset from the base, for stacked headers (eg 8*14 for vlan on top of ether)
 *
 * The location of the context is the location of the relational expression
 * defining it, either directly through a protocol match or indirectly
 * through a dependency.
 */
struct proto_ctx {
	unsigned int			family;
	struct {
		struct location			location;
		const struct proto_desc		*desc;
		unsigned int			offset;
	} protocol[PROTO_BASE_MAX + 1];
};

extern void proto_ctx_init(struct proto_ctx *ctx, unsigned int family);
extern void proto_ctx_update(struct proto_ctx *ctx, enum proto_bases base,
			     const struct location *loc,
			     const struct proto_desc *desc);
extern const struct proto_desc *proto_find_upper(const struct proto_desc *base,
						 unsigned int num);
extern int proto_find_num(const struct proto_desc *base,
			  const struct proto_desc *desc);

enum eth_hdr_fields {
	ETHHDR_INVALID,
	ETHHDR_DADDR,
	ETHHDR_SADDR,
	ETHHDR_TYPE,
};

enum vlan_hdr_fields {
	VLANHDR_INVALID,
	VLANHDR_PCP,
	VLANHDR_CFI,
	VLANHDR_VID,
	VLANHDR_TYPE,
};

enum arp_hdr_fields {
	ARPHDR_INVALID,
	ARPHDR_HRD,
	ARPHDR_PRO,
	ARPHDR_HLN,
	ARPHDR_PLN,
	ARPHDR_OP,
};

enum ip_hdr_fields {
	IPHDR_INVALID,
	IPHDR_VERSION,
	IPHDR_HDRLENGTH,
	IPHDR_TOS,
	IPHDR_LENGTH,
	IPHDR_ID,
	IPHDR_FRAG_OFF,
	IPHDR_TTL,
	IPHDR_PROTOCOL,
	IPHDR_CHECKSUM,
	IPHDR_SADDR,
	IPHDR_DADDR,
};

enum icmp_hdr_fields {
	ICMPHDR_INVALID,
	ICMPHDR_TYPE,
	ICMPHDR_CODE,
	ICMPHDR_CHECKSUM,
	ICMPHDR_ID,
	ICMPHDR_SEQ,
	ICMPHDR_GATEWAY,
	ICMPHDR_MTU,
};

enum icmp6_hdr_fields {
	ICMP6HDR_INVALID,
	ICMP6HDR_TYPE,
	ICMP6HDR_CODE,
	ICMP6HDR_CHECKSUM,
	ICMP6HDR_PPTR,
	ICMP6HDR_MTU,
	ICMP6HDR_ID,
	ICMP6HDR_SEQ,
	ICMP6HDR_MAXDELAY,
};

enum ip6_hdr_fields {
	IP6HDR_INVALID,
	IP6HDR_VERSION,
	IP6HDR_PRIORITY,
	IP6HDR_FLOWLABEL,
	IP6HDR_LENGTH,
	IP6HDR_NEXTHDR,
	IP6HDR_HOPLIMIT,
	IP6HDR_SADDR,
	IP6HDR_DADDR,
	IP6HDR_PROTOCOL,
};

enum ah_hdr_fields {
	AHHDR_INVALID,
	AHHDR_NEXTHDR,
	AHHDR_HDRLENGTH,
	AHHDR_RESERVED,
	AHHDR_SPI,
	AHHDR_SEQUENCE,
};

enum esp_hdr_fields {
	ESPHDR_INVALID,
	ESPHDR_SPI,
	ESPHDR_SEQUENCE,
};

enum comp_hdr_fields {
	COMPHDR_INVALID,
	COMPHDR_NEXTHDR,
	COMPHDR_FLAGS,
	COMPHDR_CPI,
};

enum udp_hdr_fields {
	UDPHDR_INVALID,
	UDPHDR_SPORT,
	UDPHDR_DPORT,
	UDPHDR_LENGTH,
	UDPHDR_CSUMCOV = UDPHDR_LENGTH,
	UDPHDR_CHECKSUM,
};

enum tcp_hdr_fields {
	TCPHDR_INVALID,
	TCPHDR_UNSPEC = TCPHDR_INVALID,
	TCPHDR_SPORT,
	TCPHDR_DPORT,
	TCPHDR_SEQ,
	TCPHDR_ACKSEQ,
	TCPHDR_DOFF,
	TCPHDR_RESERVED,
	TCPHDR_FLAGS,
	TCPHDR_WINDOW,
	TCPHDR_CHECKSUM,
	TCPHDR_URGPTR,
};

enum dccp_hdr_fields {
	DCCPHDR_INVALID,
	DCCPHDR_SPORT,
	DCCPHDR_DPORT,
	DCCPHDR_TYPE,
};

enum sctp_hdr_fields {
	SCTPHDR_INVALID,
	SCTPHDR_SPORT,
	SCTPHDR_DPORT,
	SCTPHDR_VTAG,
	SCTPHDR_CHECKSUM,
};

extern const struct proto_desc proto_icmp;
extern const struct proto_desc proto_ah;
extern const struct proto_desc proto_esp;
extern const struct proto_desc proto_comp;
extern const struct proto_desc proto_udp;
extern const struct proto_desc proto_udplite;
extern const struct proto_desc proto_tcp;
extern const struct proto_desc proto_dccp;
extern const struct proto_desc proto_sctp;
extern const struct proto_desc proto_icmp6;

extern const struct proto_desc proto_ip;
extern const struct proto_desc proto_ip6;

extern const struct proto_desc proto_inet;
extern const struct proto_desc proto_inet_service;

extern const struct proto_desc proto_arp;

extern const struct proto_desc proto_vlan;
extern const struct proto_desc proto_eth;

extern const struct proto_desc proto_netdev;

extern const struct proto_desc proto_unknown;
extern const struct proto_hdr_template proto_unknown_template;

#endif /* NFTABLES_PROTO_H */