Skip to content

Commit d6a9b65

Browse files
kaberdavem330
authored andcommitted
[NETFILTER]: nf_conntrack: add helper function for expectation initialization
Expectation address masks need to be differently initialized depending on the address family, create helper function to avoid cluttering up the code too much. Signed-off-by: Patrick McHardy <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 55a7332 commit d6a9b65

File tree

3 files changed

+75
-7
lines changed

3 files changed

+75
-7
lines changed

include/net/netfilter/nf_conntrack_expect.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ void nf_conntrack_unexpect_related(struct nf_conntrack_expect *exp);
6868
/* Allocate space for an expectation: this is mandatory before calling
6969
nf_conntrack_expect_related. You will have to call put afterwards. */
7070
struct nf_conntrack_expect *nf_conntrack_expect_alloc(struct nf_conn *me);
71+
void nf_conntrack_expect_init(struct nf_conntrack_expect *, int,
72+
union nf_conntrack_address *,
73+
union nf_conntrack_address *,
74+
u_int8_t, __be16 *, __be16 *);
7175
void nf_conntrack_expect_put(struct nf_conntrack_expect *exp);
7276
int nf_conntrack_expect_related(struct nf_conntrack_expect *expect);
7377

include/net/netfilter/nf_conntrack_tuple.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
/* The l3 protocol-specific manipulable parts of the tuple: always in
2626
network order! */
27-
union nf_conntrack_man_l3proto {
27+
union nf_conntrack_address {
2828
u_int32_t all[NF_CT_TUPLE_L3SIZE];
2929
__be32 ip;
3030
__be32 ip6[4];
@@ -54,7 +54,7 @@ union nf_conntrack_man_proto
5454
/* The manipulable part of the tuple. */
5555
struct nf_conntrack_man
5656
{
57-
union nf_conntrack_man_l3proto u3;
57+
union nf_conntrack_address u3;
5858
union nf_conntrack_man_proto u;
5959
/* Layer 3 protocol */
6060
u_int16_t l3num;
@@ -67,11 +67,7 @@ struct nf_conntrack_tuple
6767

6868
/* These are the parts of the tuple which are fixed. */
6969
struct {
70-
union {
71-
u_int32_t all[NF_CT_TUPLE_L3SIZE];
72-
u_int32_t ip;
73-
u_int32_t ip6[4];
74-
} u3;
70+
union nf_conntrack_address u3;
7571
union {
7672
/* Add other protocols here. */
7773
u_int16_t all;

net/netfilter/nf_conntrack_expect.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,74 @@ struct nf_conntrack_expect *nf_conntrack_expect_alloc(struct nf_conn *me)
196196
return new;
197197
}
198198

199+
void nf_conntrack_expect_init(struct nf_conntrack_expect *exp, int family,
200+
union nf_conntrack_address *saddr,
201+
union nf_conntrack_address *daddr,
202+
u_int8_t proto, __be16 *src, __be16 *dst)
203+
{
204+
int len;
205+
206+
if (family == AF_INET)
207+
len = 4;
208+
else
209+
len = 16;
210+
211+
exp->flags = 0;
212+
exp->expectfn = NULL;
213+
exp->helper = NULL;
214+
exp->tuple.src.l3num = family;
215+
exp->tuple.dst.protonum = proto;
216+
exp->mask.src.l3num = 0xFFFF;
217+
exp->mask.dst.protonum = 0xFF;
218+
219+
if (saddr) {
220+
memcpy(&exp->tuple.src.u3, saddr, len);
221+
if (sizeof(exp->tuple.src.u3) > len)
222+
/* address needs to be cleared for nf_ct_tuple_equal */
223+
memset((void *)&exp->tuple.src.u3 + len, 0x00,
224+
sizeof(exp->tuple.src.u3) - len);
225+
memset(&exp->mask.src.u3, 0xFF, len);
226+
if (sizeof(exp->mask.src.u3) > len)
227+
memset((void *)&exp->mask.src.u3 + len, 0x00,
228+
sizeof(exp->mask.src.u3) - len);
229+
} else {
230+
memset(&exp->tuple.src.u3, 0x00, sizeof(exp->tuple.src.u3));
231+
memset(&exp->mask.src.u3, 0x00, sizeof(exp->mask.src.u3));
232+
}
233+
234+
if (daddr) {
235+
memcpy(&exp->tuple.dst.u3, daddr, len);
236+
if (sizeof(exp->tuple.dst.u3) > len)
237+
/* address needs to be cleared for nf_ct_tuple_equal */
238+
memset((void *)&exp->tuple.dst.u3 + len, 0x00,
239+
sizeof(exp->tuple.dst.u3) - len);
240+
memset(&exp->mask.dst.u3, 0xFF, len);
241+
if (sizeof(exp->mask.dst.u3) > len)
242+
memset((void *)&exp->mask.dst.u3 + len, 0x00,
243+
sizeof(exp->mask.dst.u3) - len);
244+
} else {
245+
memset(&exp->tuple.dst.u3, 0x00, sizeof(exp->tuple.dst.u3));
246+
memset(&exp->mask.dst.u3, 0x00, sizeof(exp->mask.dst.u3));
247+
}
248+
249+
if (src) {
250+
exp->tuple.src.u.all = (__force u16)*src;
251+
exp->mask.src.u.all = 0xFFFF;
252+
} else {
253+
exp->tuple.src.u.all = 0;
254+
exp->mask.src.u.all = 0;
255+
}
256+
257+
if (dst) {
258+
exp->tuple.dst.u.all = (__force u16)*dst;
259+
exp->mask.dst.u.all = 0xFFFF;
260+
} else {
261+
exp->tuple.dst.u.all = 0;
262+
exp->mask.dst.u.all = 0;
263+
}
264+
}
265+
EXPORT_SYMBOL_GPL(nf_conntrack_expect_init);
266+
199267
void nf_conntrack_expect_put(struct nf_conntrack_expect *exp)
200268
{
201269
if (atomic_dec_and_test(&exp->use))

0 commit comments

Comments
 (0)