libnetfilter_conntrack 1.1.0
filter.c
1/*
2 * (C) 2005-2011 by Pablo Neira Ayuso <pablo@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9
10#include "internal/internal.h"
11
12static void filter_attr_l4proto(struct nfct_filter *filter, const void *value)
13{
14 int protonum;
15
16 if (filter->l4proto_len >= __FILTER_L4PROTO_MAX)
17 return;
18
19 protonum = *(int *)value;
20 if (protonum >= IPPROTO_MAX)
21 return;
22
23 set_bit(protonum, filter->l4proto_map);
24 filter->l4proto_len++;
25}
26
27#ifndef BITS_PER_BYTE
28#define BITS_PER_BYTE 8
29#endif
30
31static void
32filter_attr_l4proto_state(struct nfct_filter *filter, const void *value)
33{
34 const struct nfct_filter_proto *this = value;
35
36 if (this->state >= sizeof(filter->l4proto_state[0].map) * BITS_PER_BYTE)
37 return;
38
39 set_bit_u16(this->state, &filter->l4proto_state[this->proto].map);
40 filter->l4proto_state[this->proto].len++;
41}
42
43static void filter_attr_src_ipv4(struct nfct_filter *filter, const void *value)
44{
45 const struct nfct_filter_ipv4 *this = value;
46
47 if (filter->l3proto_elems[0] >= __FILTER_ADDR_MAX)
48 return;
49
50 filter->l3proto[0][filter->l3proto_elems[0]].addr = this->addr;
51 filter->l3proto[0][filter->l3proto_elems[0]].mask = this->mask;
52 filter->l3proto_elems[0]++;
53}
54
55static void filter_attr_dst_ipv4(struct nfct_filter *filter, const void *value)
56{
57 const struct nfct_filter_ipv4 *this = value;
58
59 if (filter->l3proto_elems[1] >= __FILTER_ADDR_MAX)
60 return;
61
62 filter->l3proto[1][filter->l3proto_elems[1]].addr = this->addr;
63 filter->l3proto[1][filter->l3proto_elems[1]].mask = this->mask;
64 filter->l3proto_elems[1]++;
65}
66
67static void filter_attr_src_ipv6(struct nfct_filter *filter, const void *value)
68{
69 const struct nfct_filter_ipv6 *this = value;
70
71 if (filter->l3proto_elems_ipv6[0] >= __FILTER_IPV6_MAX)
72 return;
73
74 memcpy(filter->l3proto_ipv6[0][filter->l3proto_elems_ipv6[0]].addr,
75 this->addr, sizeof(uint32_t)*4);
76 memcpy(filter->l3proto_ipv6[0][filter->l3proto_elems_ipv6[0]].mask,
77 this->mask, sizeof(uint32_t)*4);
78 filter->l3proto_elems_ipv6[0]++;
79}
80
81static void filter_attr_dst_ipv6(struct nfct_filter *filter, const void *value)
82{
83 const struct nfct_filter_ipv6 *this = value;
84
85 if (filter->l3proto_elems_ipv6[1] >= __FILTER_IPV6_MAX)
86 return;
87
88 memcpy(filter->l3proto_ipv6[1][filter->l3proto_elems_ipv6[1]].addr,
89 this->addr, sizeof(uint32_t)*4);
90 memcpy(filter->l3proto_ipv6[1][filter->l3proto_elems_ipv6[1]].mask,
91 this->mask, sizeof(uint32_t)*4);
92 filter->l3proto_elems_ipv6[1]++;
93}
94
95static void filter_attr_mark(struct nfct_filter *filter, const void *value)
96{
97 const struct nfct_filter_dump_mark *this = value;
98
99 if (filter->mark_elems >= __FILTER_MARK_MAX)
100 return;
101
102 filter->mark[filter->mark_elems].val = this->val;
103 filter->mark[filter->mark_elems].mask = this->mask;
104 filter->mark_elems++;
105}
106
107static void filter_attr_zone(struct nfct_filter *filter, const void *value)
108{
109 if (filter->zone_elems >= __FILTER_ZONE_MAX)
110 return;
111
112 filter->zone[filter->zone_elems] = *(uint16_t *) value;
113 filter->zone_elems++;
114}
115
116const filter_attr filter_attr_array[NFCT_FILTER_MAX] = {
117 [NFCT_FILTER_L4PROTO] = filter_attr_l4proto,
118 [NFCT_FILTER_L4PROTO_STATE] = filter_attr_l4proto_state,
119 [NFCT_FILTER_SRC_IPV4] = filter_attr_src_ipv4,
120 [NFCT_FILTER_DST_IPV4] = filter_attr_dst_ipv4,
121 [NFCT_FILTER_SRC_IPV6] = filter_attr_src_ipv6,
122 [NFCT_FILTER_DST_IPV6] = filter_attr_dst_ipv6,
123 [NFCT_FILTER_MARK] = filter_attr_mark,
124 [NFCT_FILTER_ZONE] = filter_attr_zone,
125};