summaryrefslogtreecommitdiffstats
path: root/src/ipopt.c
blob: 67e904ff3d88f5c882fa4fc6f1922861031991ad (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
#include <stdint.h>

#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/tcp.h>

#include <utils.h>
#include <headers.h>
#include <expression.h>
#include <ipopt.h>

static const struct proto_hdr_template ipopt_unknown_template =
	PROTO_HDR_TEMPLATE("unknown", &invalid_type, BYTEORDER_INVALID, 0, 0);

#define PHT(__token, __offset, __len) \
	PROTO_HDR_TEMPLATE(__token, &integer_type, BYTEORDER_BIG_ENDIAN, \
			   __offset, __len)
static const struct exthdr_desc ipopt_lsrr = {
	.name		= "lsrr",
	.type		= IPOPT_LSRR,
	.templates	= {
		[IPOPT_FIELD_TYPE]		= PHT("type",    0,  8),
		[IPOPT_FIELD_LENGTH]		= PHT("length",  8,  8),
		[IPOPT_FIELD_PTR]		= PHT("ptr",    16,  8),
		[IPOPT_FIELD_ADDR_0]		= PHT("addr",   24, 32),
	},
};

static const struct exthdr_desc ipopt_rr = {
	.name		= "rr",
	.type		= IPOPT_RR,
	.templates	= {
		[IPOPT_FIELD_TYPE]		= PHT("type",   0,   8),
		[IPOPT_FIELD_LENGTH]		= PHT("length",  8,  8),
		[IPOPT_FIELD_PTR]		= PHT("ptr",    16,  8),
		[IPOPT_FIELD_ADDR_0]		= PHT("addr",   24, 32),
	},
};

static const struct exthdr_desc ipopt_ssrr = {
	.name		= "ssrr",
	.type		= IPOPT_SSRR,
	.templates	= {
		[IPOPT_FIELD_TYPE]		= PHT("type",   0,   8),
		[IPOPT_FIELD_LENGTH]		= PHT("length",  8,  8),
		[IPOPT_FIELD_PTR]		= PHT("ptr",    16,  8),
		[IPOPT_FIELD_ADDR_0]		= PHT("addr",   24, 32),
	},
};

static const struct exthdr_desc ipopt_ra = {
	.name		= "ra",
	.type		= IPOPT_RA,
	.templates	= {
		[IPOPT_FIELD_TYPE]		= PHT("type",   0,   8),
		[IPOPT_FIELD_LENGTH]		= PHT("length", 8,   8),
		[IPOPT_FIELD_VALUE]		= PHT("value",  16, 16),
	},
};

const struct exthdr_desc *ipopt_protocols[UINT8_MAX] = {
	[IPOPT_LSRR]		= &ipopt_lsrr,
	[IPOPT_RR]		= &ipopt_rr,
	[IPOPT_SSRR]		= &ipopt_ssrr,
	[IPOPT_RA]		= &ipopt_ra,
};

struct expr *ipopt_expr_alloc(const struct location *loc, uint8_t type,
			       uint8_t field)
{
	const struct proto_hdr_template *tmpl;
	const struct exthdr_desc *desc;
	struct expr *expr;

	desc = ipopt_protocols[type];
	tmpl = &desc->templates[field];
	if (!tmpl)
		return NULL;

	if (!tmpl->len)
		return NULL;

	expr = expr_alloc(loc, EXPR_EXTHDR, tmpl->dtype,
			  BYTEORDER_BIG_ENDIAN, tmpl->len);
	expr->exthdr.desc   = desc;
	expr->exthdr.tmpl   = tmpl;
	expr->exthdr.op     = NFT_EXTHDR_OP_IPV4;
	expr->exthdr.offset = tmpl->offset;
	expr->exthdr.raw_type = desc->type;

	return expr;
}

void ipopt_init_raw(struct expr *expr, uint8_t type, unsigned int offset,
		     unsigned int len, uint32_t flags, bool set_unknown)
{
	const struct proto_hdr_template *tmpl;
	unsigned int i;

	assert(expr->etype == EXPR_EXTHDR);

	expr->len = len;
	expr->exthdr.flags = flags;
	expr->exthdr.offset = offset;

	assert(type < array_size(ipopt_protocols));
	expr->exthdr.desc = ipopt_protocols[type];
	expr->exthdr.flags = flags;

	for (i = 0; i < array_size(expr->exthdr.desc->templates); ++i) {
		tmpl = &expr->exthdr.desc->templates[i];

		/* Make sure that it's the right template based on offset and len */
		if (tmpl->offset != offset || tmpl->len != len)
			continue;

		if (flags & NFT_EXTHDR_F_PRESENT)
			expr->dtype = &boolean_type;
		else
			expr->dtype = tmpl->dtype;
		expr->exthdr.tmpl = tmpl;
		expr->exthdr.op   = NFT_EXTHDR_OP_IPV4;
		break;
	}
	if (i == array_size(expr->exthdr.desc->templates) && set_unknown) {
		expr->exthdr.tmpl = &ipopt_unknown_template;
		expr->exthdr.op   = NFT_EXTHDR_OP_IPV4;
	}
}

bool ipopt_find_template(struct expr *expr, unsigned int offset,
			  unsigned int len)
{
	if (expr->exthdr.tmpl != &ipopt_unknown_template)
		return false;

	ipopt_init_raw(expr, expr->exthdr.desc->type, offset, len, 0, false);

	if (expr->exthdr.tmpl == &ipopt_unknown_template)
		return false;

	return true;
}