summaryrefslogtreecommitdiffstats
path: root/src/conntrack/filter.c
blob: 57b22945fc3bc89db99d1ee6611f95d12937bcd6 (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
/*
 * (C) 2005-2011 by Pablo Neira Ayuso <pablo@netfilter.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include "internal/internal.h"

static void filter_attr_l4proto(struct nfct_filter *filter, const void *value)
{
	int protonum;

	if (filter->l4proto_len >= __FILTER_L4PROTO_MAX)
		return;

	protonum = *(int *)value;
	if (protonum >= IPPROTO_MAX)
		return;

	set_bit(protonum, filter->l4proto_map);
	filter->l4proto_len++;
}

#ifndef BITS_PER_BYTE
#define BITS_PER_BYTE	8
#endif

static void
filter_attr_l4proto_state(struct nfct_filter *filter, const void *value)
{
	const struct nfct_filter_proto *this = value;

	if (this->state >= sizeof(filter->l4proto_state[0].map) * BITS_PER_BYTE)
		return;

	set_bit_u16(this->state, &filter->l4proto_state[this->proto].map);
	filter->l4proto_state[this->proto].len++;
}

static void filter_attr_src_ipv4(struct nfct_filter *filter, const void *value)
{
	const struct nfct_filter_ipv4 *this = value;

	if (filter->l3proto_elems[0] >= __FILTER_ADDR_MAX)
		return;

	filter->l3proto[0][filter->l3proto_elems[0]].addr = this->addr;
	filter->l3proto[0][filter->l3proto_elems[0]].mask = this->mask;
	filter->l3proto_elems[0]++;
}

static void filter_attr_dst_ipv4(struct nfct_filter *filter, const void *value)
{
	const struct nfct_filter_ipv4 *this = value;

	if (filter->l3proto_elems[1] >= __FILTER_ADDR_MAX)
		return;

	filter->l3proto[1][filter->l3proto_elems[1]].addr = this->addr;
	filter->l3proto[1][filter->l3proto_elems[1]].mask = this->mask;
	filter->l3proto_elems[1]++;
}

static void filter_attr_src_ipv6(struct nfct_filter *filter, const void *value)
{
	const struct nfct_filter_ipv6 *this = value;

	if (filter->l3proto_elems_ipv6[0] >= __FILTER_IPV6_MAX)
		return;

	memcpy(filter->l3proto_ipv6[0][filter->l3proto_elems_ipv6[0]].addr,
	       this->addr, sizeof(uint32_t)*4);
	memcpy(filter->l3proto_ipv6[0][filter->l3proto_elems_ipv6[0]].mask,
	       this->mask, sizeof(uint32_t)*4);
	filter->l3proto_elems_ipv6[0]++;
}

static void filter_attr_dst_ipv6(struct nfct_filter *filter, const void *value)
{
	const struct nfct_filter_ipv6 *this = value;

	if (filter->l3proto_elems_ipv6[1] >= __FILTER_IPV6_MAX)
		return;

	memcpy(filter->l3proto_ipv6[1][filter->l3proto_elems_ipv6[1]].addr,
	       this->addr, sizeof(uint32_t)*4);
	memcpy(filter->l3proto_ipv6[1][filter->l3proto_elems_ipv6[1]].mask,
	       this->mask, sizeof(uint32_t)*4);
	filter->l3proto_elems_ipv6[1]++;
}

static void filter_attr_mark(struct nfct_filter *filter, const void *value)
{
	const struct nfct_filter_dump_mark *this = value;

	if (filter->mark_elems >= __FILTER_MARK_MAX)
		return;

	filter->mark[filter->mark_elems].val = this->val;
	filter->mark[filter->mark_elems].mask = this->mask;
	filter->mark_elems++;
}

const filter_attr filter_attr_array[NFCT_FILTER_MAX] = {
	[NFCT_FILTER_L4PROTO]		= filter_attr_l4proto,
	[NFCT_FILTER_L4PROTO_STATE]	= filter_attr_l4proto_state,
	[NFCT_FILTER_SRC_IPV4]		= filter_attr_src_ipv4,
	[NFCT_FILTER_DST_IPV4]		= filter_attr_dst_ipv4,
	[NFCT_FILTER_SRC_IPV6]		= filter_attr_src_ipv6,
	[NFCT_FILTER_DST_IPV6]		= filter_attr_dst_ipv6,
	[NFCT_FILTER_MARK]		= filter_attr_mark,
};