libnetfilter_conntrack  1.0.9
grp_setter.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 
12 static void set_attr_grp_orig_ipv4(struct nf_conntrack *ct, const void *value)
13 {
14  const struct nfct_attr_grp_ipv4 *this = value;
15  ct->head.orig.src.v4 = this->src;
16  ct->head.orig.dst.v4 = this->dst;
17  ct->head.orig.l3protonum = AF_INET;
18 }
19 
20 static void set_attr_grp_repl_ipv4(struct nf_conntrack *ct, const void *value)
21 {
22  const struct nfct_attr_grp_ipv4 *this = value;
23  ct->repl.src.v4 = this->src;
24  ct->repl.dst.v4 = this->dst;
25  ct->repl.l3protonum = AF_INET;
26 }
27 
28 static void set_attr_grp_orig_ipv6(struct nf_conntrack *ct, const void *value)
29 {
30  const struct nfct_attr_grp_ipv6 *this = value;
31  memcpy(&ct->head.orig.src.v6, this->src, sizeof(uint32_t)*4);
32  memcpy(&ct->head.orig.dst.v6, this->dst, sizeof(uint32_t)*4);
33  ct->head.orig.l3protonum = AF_INET6;
34 }
35 
36 static void set_attr_grp_repl_ipv6(struct nf_conntrack *ct, const void *value)
37 {
38  const struct nfct_attr_grp_ipv6 *this = value;
39  memcpy(&ct->repl.src.v6, this->src, sizeof(uint32_t)*4);
40  memcpy(&ct->repl.dst.v6, this->dst, sizeof(uint32_t)*4);
41  ct->repl.l3protonum = AF_INET6;
42 }
43 
44 static void set_attr_grp_orig_port(struct nf_conntrack *ct, const void *value)
45 {
46  const struct nfct_attr_grp_port *this = value;
47  ct->head.orig.l4src.all = this->sport;
48  ct->head.orig.l4dst.all = this->dport;
49 }
50 
51 static void set_attr_grp_repl_port(struct nf_conntrack *ct, const void *value)
52 {
53  const struct nfct_attr_grp_port *this = value;
54  ct->repl.l4src.all = this->sport;
55  ct->repl.l4dst.all = this->dport;
56 }
57 
58 static void set_attr_grp_icmp(struct nf_conntrack *ct, const void *value)
59 {
60  const struct nfct_attr_grp_icmp *this = value;
61  uint8_t rtype = 0;
62 
63  ct->head.orig.l4dst.icmp.type = this->type;
64 
65  switch(ct->head.orig.l3protonum) {
66  case AF_INET:
67  rtype = __icmp_reply_type(this->type);
68  break;
69 
70  case AF_INET6:
71  rtype = __icmpv6_reply_type(this->type);
72  break;
73 
74  default:
75  rtype = 0; /* not found */
76  }
77 
78  if (rtype)
79  ct->repl.l4dst.icmp.type = rtype - 1;
80  else
81  ct->repl.l4dst.icmp.type = 255; /* -EINVAL */
82 
83  ct->head.orig.l4dst.icmp.code = this->code;
84  ct->repl.l4dst.icmp.code = this->code;
85 
86  ct->head.orig.l4src.icmp.id = this->id;
87  ct->repl.l4src.icmp.id = this->id;
88 }
89 
90 static void set_attr_grp_master_ipv4(struct nf_conntrack *ct, const void *value)
91 {
92  const struct nfct_attr_grp_ipv4 *this = value;
93  ct->master.src.v4 = this->src;
94  ct->master.dst.v4 = this->dst;
95  ct->master.l3protonum = AF_INET;
96 }
97 
98 static void set_attr_grp_master_ipv6(struct nf_conntrack *ct, const void *value)
99 {
100  const struct nfct_attr_grp_ipv6 *this = value;
101  memcpy(&ct->master.src.v6, this->src, sizeof(uint32_t)*4);
102  memcpy(&ct->master.dst.v6, this->dst, sizeof(uint32_t)*4);
103  ct->master.l3protonum = AF_INET6;
104 }
105 
106 static void set_attr_grp_master_port(struct nf_conntrack *ct, const void *value)
107 {
108  const struct nfct_attr_grp_port *this = value;
109  ct->master.l4src.all = this->sport;
110  ct->master.l4dst.all = this->dport;
111 }
112 
113 static void set_attr_grp_do_nothing(struct nf_conntrack *ct, const void *value)
114 {
115 }
116 
117 const set_attr_grp set_attr_grp_array[ATTR_GRP_MAX] = {
118  [ATTR_GRP_ORIG_IPV4] = set_attr_grp_orig_ipv4,
119  [ATTR_GRP_REPL_IPV4] = set_attr_grp_repl_ipv4,
120  [ATTR_GRP_ORIG_IPV6] = set_attr_grp_orig_ipv6,
121  [ATTR_GRP_REPL_IPV6] = set_attr_grp_repl_ipv6,
122  [ATTR_GRP_ORIG_PORT] = set_attr_grp_orig_port,
123  [ATTR_GRP_REPL_PORT] = set_attr_grp_repl_port,
124  [ATTR_GRP_ICMP] = set_attr_grp_icmp,
125  [ATTR_GRP_MASTER_IPV4] = set_attr_grp_master_ipv4,
126  [ATTR_GRP_MASTER_IPV6] = set_attr_grp_master_ipv6,
127  [ATTR_GRP_MASTER_PORT] = set_attr_grp_master_port,
128  [ATTR_GRP_ORIG_COUNTERS] = set_attr_grp_do_nothing,
129  [ATTR_GRP_REPL_COUNTERS] = set_attr_grp_do_nothing,
130  [ATTR_GRP_ORIG_ADDR_SRC] = set_attr_grp_do_nothing,
131  [ATTR_GRP_ORIG_ADDR_DST] = set_attr_grp_do_nothing,
132  [ATTR_GRP_REPL_ADDR_SRC] = set_attr_grp_do_nothing,
133  [ATTR_GRP_REPL_ADDR_DST] = set_attr_grp_do_nothing,
134 };