]>
Commit | Line | Data |
---|---|---|
c1d10adb PNA |
1 | /* Connection tracking via netlink socket. Allows for user space |
2 | * protocol helpers and general trouble making from userspace. | |
3 | * | |
4 | * (C) 2001 by Jay Schulist <jschlst@samba.org> | |
dc808fe2 | 5 | * (C) 2002-2006 by Harald Welte <laforge@gnumonks.org> |
c1d10adb | 6 | * (C) 2003 by Patrick Mchardy <kaber@trash.net> |
1cde6436 | 7 | * (C) 2005-2006 by Pablo Neira Ayuso <pablo@eurodev.net> |
c1d10adb | 8 | * |
601e68e1 | 9 | * Initial connection tracking via netlink development funded and |
c1d10adb PNA |
10 | * generally made possible by Network Robots, Inc. (www.networkrobots.com) |
11 | * | |
12 | * Further development of this code funded by Astaro AG (http://www.astaro.com) | |
13 | * | |
14 | * This software may be used and distributed according to the terms | |
15 | * of the GNU General Public License, incorporated herein by reference. | |
c1d10adb PNA |
16 | */ |
17 | ||
18 | #include <linux/init.h> | |
19 | #include <linux/module.h> | |
20 | #include <linux/kernel.h> | |
21 | #include <linux/types.h> | |
22 | #include <linux/timer.h> | |
23 | #include <linux/skbuff.h> | |
24 | #include <linux/errno.h> | |
25 | #include <linux/netlink.h> | |
26 | #include <linux/spinlock.h> | |
40a839fd | 27 | #include <linux/interrupt.h> |
c1d10adb PNA |
28 | #include <linux/notifier.h> |
29 | ||
30 | #include <linux/netfilter.h> | |
dc5fc579 | 31 | #include <net/netlink.h> |
c1d10adb PNA |
32 | #include <net/netfilter/nf_conntrack.h> |
33 | #include <net/netfilter/nf_conntrack_core.h> | |
77ab9cff | 34 | #include <net/netfilter/nf_conntrack_expect.h> |
c1d10adb PNA |
35 | #include <net/netfilter/nf_conntrack_helper.h> |
36 | #include <net/netfilter/nf_conntrack_l3proto.h> | |
605dcad6 | 37 | #include <net/netfilter/nf_conntrack_l4proto.h> |
5b1158e9 JK |
38 | #include <net/netfilter/nf_conntrack_tuple.h> |
39 | #ifdef CONFIG_NF_NAT_NEEDED | |
40 | #include <net/netfilter/nf_nat_core.h> | |
41 | #include <net/netfilter/nf_nat_protocol.h> | |
42 | #endif | |
c1d10adb PNA |
43 | |
44 | #include <linux/netfilter/nfnetlink.h> | |
45 | #include <linux/netfilter/nfnetlink_conntrack.h> | |
46 | ||
47 | MODULE_LICENSE("GPL"); | |
48 | ||
dc808fe2 | 49 | static char __initdata version[] = "0.93"; |
c1d10adb | 50 | |
c1d10adb | 51 | static inline int |
601e68e1 | 52 | ctnetlink_dump_tuples_proto(struct sk_buff *skb, |
1cde6436 | 53 | const struct nf_conntrack_tuple *tuple, |
605dcad6 | 54 | struct nf_conntrack_l4proto *l4proto) |
c1d10adb | 55 | { |
c1d10adb | 56 | int ret = 0; |
df6fb868 | 57 | struct nlattr *nest_parms; |
c1d10adb | 58 | |
df6fb868 PM |
59 | nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED); |
60 | if (!nest_parms) | |
61 | goto nla_put_failure; | |
62 | NLA_PUT(skb, CTA_PROTO_NUM, sizeof(u_int8_t), &tuple->dst.protonum); | |
c1d10adb | 63 | |
605dcad6 MJ |
64 | if (likely(l4proto->tuple_to_nfattr)) |
65 | ret = l4proto->tuple_to_nfattr(skb, tuple); | |
601e68e1 | 66 | |
df6fb868 | 67 | nla_nest_end(skb, nest_parms); |
c1d10adb PNA |
68 | |
69 | return ret; | |
70 | ||
df6fb868 | 71 | nla_put_failure: |
c1d10adb PNA |
72 | return -1; |
73 | } | |
74 | ||
75 | static inline int | |
1cde6436 PNA |
76 | ctnetlink_dump_tuples_ip(struct sk_buff *skb, |
77 | const struct nf_conntrack_tuple *tuple, | |
78 | struct nf_conntrack_l3proto *l3proto) | |
c1d10adb | 79 | { |
c1d10adb | 80 | int ret = 0; |
df6fb868 PM |
81 | struct nlattr *nest_parms; |
82 | ||
83 | nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED); | |
84 | if (!nest_parms) | |
85 | goto nla_put_failure; | |
1cde6436 | 86 | |
c1d10adb PNA |
87 | if (likely(l3proto->tuple_to_nfattr)) |
88 | ret = l3proto->tuple_to_nfattr(skb, tuple); | |
1cde6436 | 89 | |
df6fb868 | 90 | nla_nest_end(skb, nest_parms); |
c1d10adb | 91 | |
1cde6436 PNA |
92 | return ret; |
93 | ||
df6fb868 | 94 | nla_put_failure: |
1cde6436 PNA |
95 | return -1; |
96 | } | |
97 | ||
98 | static inline int | |
99 | ctnetlink_dump_tuples(struct sk_buff *skb, | |
100 | const struct nf_conntrack_tuple *tuple) | |
101 | { | |
102 | int ret; | |
103 | struct nf_conntrack_l3proto *l3proto; | |
605dcad6 | 104 | struct nf_conntrack_l4proto *l4proto; |
1cde6436 PNA |
105 | |
106 | l3proto = nf_ct_l3proto_find_get(tuple->src.l3num); | |
107 | ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto); | |
c1d10adb PNA |
108 | nf_ct_l3proto_put(l3proto); |
109 | ||
110 | if (unlikely(ret < 0)) | |
111 | return ret; | |
112 | ||
605dcad6 MJ |
113 | l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum); |
114 | ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto); | |
115 | nf_ct_l4proto_put(l4proto); | |
c1d10adb PNA |
116 | |
117 | return ret; | |
c1d10adb PNA |
118 | } |
119 | ||
120 | static inline int | |
121 | ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct) | |
122 | { | |
bff9a89b | 123 | __be32 status = htonl((u_int32_t) ct->status); |
df6fb868 | 124 | NLA_PUT(skb, CTA_STATUS, sizeof(status), &status); |
c1d10adb PNA |
125 | return 0; |
126 | ||
df6fb868 | 127 | nla_put_failure: |
c1d10adb PNA |
128 | return -1; |
129 | } | |
130 | ||
131 | static inline int | |
132 | ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct) | |
133 | { | |
134 | long timeout_l = ct->timeout.expires - jiffies; | |
bff9a89b | 135 | __be32 timeout; |
c1d10adb PNA |
136 | |
137 | if (timeout_l < 0) | |
138 | timeout = 0; | |
139 | else | |
140 | timeout = htonl(timeout_l / HZ); | |
601e68e1 | 141 | |
df6fb868 | 142 | NLA_PUT(skb, CTA_TIMEOUT, sizeof(timeout), &timeout); |
c1d10adb PNA |
143 | return 0; |
144 | ||
df6fb868 | 145 | nla_put_failure: |
c1d10adb PNA |
146 | return -1; |
147 | } | |
148 | ||
149 | static inline int | |
150 | ctnetlink_dump_protoinfo(struct sk_buff *skb, const struct nf_conn *ct) | |
151 | { | |
605dcad6 | 152 | struct nf_conntrack_l4proto *l4proto = nf_ct_l4proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num, ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum); |
df6fb868 | 153 | struct nlattr *nest_proto; |
c1d10adb PNA |
154 | int ret; |
155 | ||
605dcad6 MJ |
156 | if (!l4proto->to_nfattr) { |
157 | nf_ct_l4proto_put(l4proto); | |
c1d10adb PNA |
158 | return 0; |
159 | } | |
601e68e1 | 160 | |
df6fb868 PM |
161 | nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED); |
162 | if (!nest_proto) | |
163 | goto nla_put_failure; | |
c1d10adb | 164 | |
605dcad6 | 165 | ret = l4proto->to_nfattr(skb, nest_proto, ct); |
c1d10adb | 166 | |
605dcad6 | 167 | nf_ct_l4proto_put(l4proto); |
c1d10adb | 168 | |
df6fb868 | 169 | nla_nest_end(skb, nest_proto); |
c1d10adb PNA |
170 | |
171 | return ret; | |
172 | ||
df6fb868 | 173 | nla_put_failure: |
605dcad6 | 174 | nf_ct_l4proto_put(l4proto); |
c1d10adb PNA |
175 | return -1; |
176 | } | |
177 | ||
178 | static inline int | |
179 | ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct) | |
180 | { | |
df6fb868 | 181 | struct nlattr *nest_helper; |
dc808fe2 | 182 | const struct nf_conn_help *help = nfct_help(ct); |
3c158f7f | 183 | struct nf_conntrack_helper *helper; |
c1d10adb | 184 | |
3c158f7f | 185 | if (!help) |
c1d10adb | 186 | return 0; |
601e68e1 | 187 | |
3c158f7f PM |
188 | rcu_read_lock(); |
189 | helper = rcu_dereference(help->helper); | |
190 | if (!helper) | |
191 | goto out; | |
192 | ||
df6fb868 PM |
193 | nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED); |
194 | if (!nest_helper) | |
195 | goto nla_put_failure; | |
196 | NLA_PUT(skb, CTA_HELP_NAME, strlen(helper->name), helper->name); | |
c1d10adb | 197 | |
3c158f7f PM |
198 | if (helper->to_nfattr) |
199 | helper->to_nfattr(skb, ct); | |
c1d10adb | 200 | |
df6fb868 | 201 | nla_nest_end(skb, nest_helper); |
3c158f7f PM |
202 | out: |
203 | rcu_read_unlock(); | |
c1d10adb PNA |
204 | return 0; |
205 | ||
df6fb868 | 206 | nla_put_failure: |
3c158f7f | 207 | rcu_read_unlock(); |
c1d10adb PNA |
208 | return -1; |
209 | } | |
210 | ||
211 | #ifdef CONFIG_NF_CT_ACCT | |
212 | static inline int | |
213 | ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct, | |
214 | enum ip_conntrack_dir dir) | |
215 | { | |
216 | enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG; | |
df6fb868 | 217 | struct nlattr *nest_count; |
bff9a89b | 218 | __be32 tmp; |
c1d10adb | 219 | |
df6fb868 PM |
220 | nest_count = nla_nest_start(skb, type | NLA_F_NESTED); |
221 | if (!nest_count) | |
222 | goto nla_put_failure; | |
223 | ||
c1d10adb | 224 | tmp = htonl(ct->counters[dir].packets); |
df6fb868 | 225 | NLA_PUT(skb, CTA_COUNTERS32_PACKETS, sizeof(u_int32_t), &tmp); |
c1d10adb PNA |
226 | |
227 | tmp = htonl(ct->counters[dir].bytes); | |
df6fb868 | 228 | NLA_PUT(skb, CTA_COUNTERS32_BYTES, sizeof(u_int32_t), &tmp); |
c1d10adb | 229 | |
df6fb868 | 230 | nla_nest_end(skb, nest_count); |
c1d10adb PNA |
231 | |
232 | return 0; | |
233 | ||
df6fb868 | 234 | nla_put_failure: |
c1d10adb PNA |
235 | return -1; |
236 | } | |
237 | #else | |
238 | #define ctnetlink_dump_counters(a, b, c) (0) | |
239 | #endif | |
240 | ||
241 | #ifdef CONFIG_NF_CONNTRACK_MARK | |
242 | static inline int | |
243 | ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct) | |
244 | { | |
bff9a89b | 245 | __be32 mark = htonl(ct->mark); |
c1d10adb | 246 | |
df6fb868 | 247 | NLA_PUT(skb, CTA_MARK, sizeof(u_int32_t), &mark); |
c1d10adb PNA |
248 | return 0; |
249 | ||
df6fb868 | 250 | nla_put_failure: |
c1d10adb PNA |
251 | return -1; |
252 | } | |
253 | #else | |
254 | #define ctnetlink_dump_mark(a, b) (0) | |
255 | #endif | |
256 | ||
257 | static inline int | |
258 | ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct) | |
259 | { | |
bff9a89b | 260 | __be32 id = htonl(ct->id); |
df6fb868 | 261 | NLA_PUT(skb, CTA_ID, sizeof(u_int32_t), &id); |
c1d10adb PNA |
262 | return 0; |
263 | ||
df6fb868 | 264 | nla_put_failure: |
c1d10adb PNA |
265 | return -1; |
266 | } | |
267 | ||
268 | static inline int | |
269 | ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct) | |
270 | { | |
bff9a89b | 271 | __be32 use = htonl(atomic_read(&ct->ct_general.use)); |
601e68e1 | 272 | |
df6fb868 | 273 | NLA_PUT(skb, CTA_USE, sizeof(u_int32_t), &use); |
c1d10adb PNA |
274 | return 0; |
275 | ||
df6fb868 | 276 | nla_put_failure: |
c1d10adb PNA |
277 | return -1; |
278 | } | |
279 | ||
280 | #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple) | |
281 | ||
282 | static int | |
283 | ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq, | |
601e68e1 | 284 | int event, int nowait, |
c1d10adb PNA |
285 | const struct nf_conn *ct) |
286 | { | |
287 | struct nlmsghdr *nlh; | |
288 | struct nfgenmsg *nfmsg; | |
df6fb868 | 289 | struct nlattr *nest_parms; |
27a884dc | 290 | unsigned char *b = skb_tail_pointer(skb); |
c1d10adb PNA |
291 | |
292 | event |= NFNL_SUBSYS_CTNETLINK << 8; | |
293 | nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg)); | |
294 | nfmsg = NLMSG_DATA(nlh); | |
295 | ||
296 | nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0; | |
601e68e1 | 297 | nfmsg->nfgen_family = |
c1d10adb PNA |
298 | ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num; |
299 | nfmsg->version = NFNETLINK_V0; | |
300 | nfmsg->res_id = 0; | |
301 | ||
df6fb868 PM |
302 | nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED); |
303 | if (!nest_parms) | |
304 | goto nla_put_failure; | |
c1d10adb | 305 | if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) |
df6fb868 PM |
306 | goto nla_put_failure; |
307 | nla_nest_end(skb, nest_parms); | |
601e68e1 | 308 | |
df6fb868 PM |
309 | nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED); |
310 | if (!nest_parms) | |
311 | goto nla_put_failure; | |
c1d10adb | 312 | if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0) |
df6fb868 PM |
313 | goto nla_put_failure; |
314 | nla_nest_end(skb, nest_parms); | |
c1d10adb PNA |
315 | |
316 | if (ctnetlink_dump_status(skb, ct) < 0 || | |
317 | ctnetlink_dump_timeout(skb, ct) < 0 || | |
318 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 || | |
319 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 || | |
320 | ctnetlink_dump_protoinfo(skb, ct) < 0 || | |
321 | ctnetlink_dump_helpinfo(skb, ct) < 0 || | |
322 | ctnetlink_dump_mark(skb, ct) < 0 || | |
323 | ctnetlink_dump_id(skb, ct) < 0 || | |
324 | ctnetlink_dump_use(skb, ct) < 0) | |
df6fb868 | 325 | goto nla_put_failure; |
c1d10adb | 326 | |
27a884dc | 327 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
c1d10adb PNA |
328 | return skb->len; |
329 | ||
330 | nlmsg_failure: | |
df6fb868 | 331 | nla_put_failure: |
dc5fc579 | 332 | nlmsg_trim(skb, b); |
c1d10adb PNA |
333 | return -1; |
334 | } | |
335 | ||
336 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
337 | static int ctnetlink_conntrack_event(struct notifier_block *this, | |
601e68e1 | 338 | unsigned long events, void *ptr) |
c1d10adb PNA |
339 | { |
340 | struct nlmsghdr *nlh; | |
341 | struct nfgenmsg *nfmsg; | |
df6fb868 | 342 | struct nlattr *nest_parms; |
c1d10adb PNA |
343 | struct nf_conn *ct = (struct nf_conn *)ptr; |
344 | struct sk_buff *skb; | |
345 | unsigned int type; | |
27a884dc | 346 | sk_buff_data_t b; |
c1d10adb PNA |
347 | unsigned int flags = 0, group; |
348 | ||
349 | /* ignore our fake conntrack entry */ | |
350 | if (ct == &nf_conntrack_untracked) | |
351 | return NOTIFY_DONE; | |
352 | ||
353 | if (events & IPCT_DESTROY) { | |
354 | type = IPCTNL_MSG_CT_DELETE; | |
355 | group = NFNLGRP_CONNTRACK_DESTROY; | |
356 | } else if (events & (IPCT_NEW | IPCT_RELATED)) { | |
357 | type = IPCTNL_MSG_CT_NEW; | |
358 | flags = NLM_F_CREATE|NLM_F_EXCL; | |
c1d10adb | 359 | group = NFNLGRP_CONNTRACK_NEW; |
1a31526b | 360 | } else if (events & (IPCT_STATUS | IPCT_PROTOINFO)) { |
c1d10adb PNA |
361 | type = IPCTNL_MSG_CT_NEW; |
362 | group = NFNLGRP_CONNTRACK_UPDATE; | |
363 | } else | |
364 | return NOTIFY_DONE; | |
a2427692 PM |
365 | |
366 | if (!nfnetlink_has_listeners(group)) | |
367 | return NOTIFY_DONE; | |
368 | ||
c1d10adb PNA |
369 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
370 | if (!skb) | |
371 | return NOTIFY_DONE; | |
372 | ||
373 | b = skb->tail; | |
374 | ||
375 | type |= NFNL_SUBSYS_CTNETLINK << 8; | |
376 | nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg)); | |
377 | nfmsg = NLMSG_DATA(nlh); | |
378 | ||
379 | nlh->nlmsg_flags = flags; | |
380 | nfmsg->nfgen_family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num; | |
381 | nfmsg->version = NFNETLINK_V0; | |
382 | nfmsg->res_id = 0; | |
383 | ||
df6fb868 PM |
384 | nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED); |
385 | if (!nest_parms) | |
386 | goto nla_put_failure; | |
c1d10adb | 387 | if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) |
df6fb868 PM |
388 | goto nla_put_failure; |
389 | nla_nest_end(skb, nest_parms); | |
601e68e1 | 390 | |
df6fb868 PM |
391 | nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED); |
392 | if (!nest_parms) | |
393 | goto nla_put_failure; | |
c1d10adb | 394 | if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0) |
df6fb868 PM |
395 | goto nla_put_failure; |
396 | nla_nest_end(skb, nest_parms); | |
c1d10adb | 397 | |
7b621c1e PNA |
398 | if (events & IPCT_DESTROY) { |
399 | if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 || | |
400 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0) | |
df6fb868 | 401 | goto nla_put_failure; |
7b621c1e PNA |
402 | } else { |
403 | if (ctnetlink_dump_status(skb, ct) < 0) | |
df6fb868 | 404 | goto nla_put_failure; |
7b621c1e PNA |
405 | |
406 | if (ctnetlink_dump_timeout(skb, ct) < 0) | |
df6fb868 | 407 | goto nla_put_failure; |
7b621c1e PNA |
408 | |
409 | if (events & IPCT_PROTOINFO | |
410 | && ctnetlink_dump_protoinfo(skb, ct) < 0) | |
df6fb868 | 411 | goto nla_put_failure; |
7b621c1e PNA |
412 | |
413 | if ((events & IPCT_HELPER || nfct_help(ct)) | |
414 | && ctnetlink_dump_helpinfo(skb, ct) < 0) | |
df6fb868 | 415 | goto nla_put_failure; |
7b621c1e | 416 | |
40e0cb00 | 417 | #ifdef CONFIG_NF_CONNTRACK_MARK |
7b621c1e PNA |
418 | if ((events & IPCT_MARK || ct->mark) |
419 | && ctnetlink_dump_mark(skb, ct) < 0) | |
df6fb868 | 420 | goto nla_put_failure; |
40e0cb00 | 421 | #endif |
7b621c1e PNA |
422 | |
423 | if (events & IPCT_COUNTER_FILLING && | |
424 | (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 || | |
425 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)) | |
df6fb868 | 426 | goto nla_put_failure; |
7b621c1e | 427 | } |
b9a37e0c | 428 | |
c1d10adb PNA |
429 | nlh->nlmsg_len = skb->tail - b; |
430 | nfnetlink_send(skb, 0, group, 0); | |
431 | return NOTIFY_DONE; | |
432 | ||
433 | nlmsg_failure: | |
df6fb868 | 434 | nla_put_failure: |
c1d10adb PNA |
435 | kfree_skb(skb); |
436 | return NOTIFY_DONE; | |
437 | } | |
438 | #endif /* CONFIG_NF_CONNTRACK_EVENTS */ | |
439 | ||
440 | static int ctnetlink_done(struct netlink_callback *cb) | |
441 | { | |
89f2e218 PM |
442 | if (cb->args[1]) |
443 | nf_ct_put((struct nf_conn *)cb->args[1]); | |
c1d10adb PNA |
444 | return 0; |
445 | } | |
446 | ||
87711cb8 PNA |
447 | #define L3PROTO(ct) ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num |
448 | ||
c1d10adb PNA |
449 | static int |
450 | ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb) | |
451 | { | |
89f2e218 | 452 | struct nf_conn *ct, *last; |
c1d10adb | 453 | struct nf_conntrack_tuple_hash *h; |
f205c5e0 | 454 | struct hlist_node *n; |
87711cb8 PNA |
455 | struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh); |
456 | u_int8_t l3proto = nfmsg->nfgen_family; | |
c1d10adb | 457 | |
c1d10adb | 458 | read_lock_bh(&nf_conntrack_lock); |
d205dc40 | 459 | last = (struct nf_conn *)cb->args[1]; |
89f2e218 PM |
460 | for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) { |
461 | restart: | |
f205c5e0 PM |
462 | hlist_for_each_entry(h, n, &nf_conntrack_hash[cb->args[0]], |
463 | hnode) { | |
5b1158e9 | 464 | if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL) |
c1d10adb PNA |
465 | continue; |
466 | ct = nf_ct_tuplehash_to_ctrack(h); | |
87711cb8 PNA |
467 | /* Dump entries of a given L3 protocol number. |
468 | * If it is not specified, ie. l3proto == 0, | |
469 | * then dump everything. */ | |
470 | if (l3proto && L3PROTO(ct) != l3proto) | |
471 | continue; | |
d205dc40 PM |
472 | if (cb->args[1]) { |
473 | if (ct != last) | |
89f2e218 | 474 | continue; |
d205dc40 | 475 | cb->args[1] = 0; |
89f2e218 | 476 | } |
c1d10adb | 477 | if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid, |
601e68e1 | 478 | cb->nlh->nlmsg_seq, |
c1d10adb | 479 | IPCTNL_MSG_CT_NEW, |
89f2e218 PM |
480 | 1, ct) < 0) { |
481 | nf_conntrack_get(&ct->ct_general); | |
482 | cb->args[1] = (unsigned long)ct; | |
c1d10adb | 483 | goto out; |
89f2e218 | 484 | } |
01f34848 PNA |
485 | #ifdef CONFIG_NF_CT_ACCT |
486 | if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == | |
487 | IPCTNL_MSG_CT_GET_CTRZERO) | |
488 | memset(&ct->counters, 0, sizeof(ct->counters)); | |
489 | #endif | |
89f2e218 | 490 | } |
d205dc40 | 491 | if (cb->args[1]) { |
89f2e218 PM |
492 | cb->args[1] = 0; |
493 | goto restart; | |
c1d10adb PNA |
494 | } |
495 | } | |
89f2e218 | 496 | out: |
c1d10adb | 497 | read_unlock_bh(&nf_conntrack_lock); |
d205dc40 PM |
498 | if (last) |
499 | nf_ct_put(last); | |
c1d10adb | 500 | |
c1d10adb PNA |
501 | return skb->len; |
502 | } | |
503 | ||
c1d10adb | 504 | static inline int |
df6fb868 | 505 | ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple) |
c1d10adb | 506 | { |
df6fb868 | 507 | struct nlattr *tb[CTA_IP_MAX+1]; |
c1d10adb PNA |
508 | struct nf_conntrack_l3proto *l3proto; |
509 | int ret = 0; | |
510 | ||
df6fb868 | 511 | nla_parse_nested(tb, CTA_IP_MAX, attr, NULL); |
c1d10adb PNA |
512 | |
513 | l3proto = nf_ct_l3proto_find_get(tuple->src.l3num); | |
514 | ||
515 | if (likely(l3proto->nfattr_to_tuple)) | |
516 | ret = l3proto->nfattr_to_tuple(tb, tuple); | |
517 | ||
518 | nf_ct_l3proto_put(l3proto); | |
519 | ||
c1d10adb PNA |
520 | return ret; |
521 | } | |
522 | ||
df6fb868 PM |
523 | static const size_t cta_min_proto[CTA_PROTO_MAX+1] = { |
524 | [CTA_PROTO_NUM] = sizeof(u_int8_t), | |
c1d10adb PNA |
525 | }; |
526 | ||
527 | static inline int | |
df6fb868 | 528 | ctnetlink_parse_tuple_proto(struct nlattr *attr, |
c1d10adb PNA |
529 | struct nf_conntrack_tuple *tuple) |
530 | { | |
df6fb868 | 531 | struct nlattr *tb[CTA_PROTO_MAX+1]; |
605dcad6 | 532 | struct nf_conntrack_l4proto *l4proto; |
c1d10adb PNA |
533 | int ret = 0; |
534 | ||
df6fb868 | 535 | nla_parse_nested(tb, CTA_PROTO_MAX, attr, NULL); |
c1d10adb PNA |
536 | |
537 | if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto)) | |
538 | return -EINVAL; | |
539 | ||
df6fb868 | 540 | if (!tb[CTA_PROTO_NUM]) |
c1d10adb | 541 | return -EINVAL; |
df6fb868 | 542 | tuple->dst.protonum = *(u_int8_t *)nla_data(tb[CTA_PROTO_NUM]); |
c1d10adb | 543 | |
605dcad6 | 544 | l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum); |
c1d10adb | 545 | |
605dcad6 MJ |
546 | if (likely(l4proto->nfattr_to_tuple)) |
547 | ret = l4proto->nfattr_to_tuple(tb, tuple); | |
c1d10adb | 548 | |
605dcad6 | 549 | nf_ct_l4proto_put(l4proto); |
601e68e1 | 550 | |
c1d10adb PNA |
551 | return ret; |
552 | } | |
553 | ||
554 | static inline int | |
df6fb868 | 555 | ctnetlink_parse_tuple(struct nlattr *cda[], struct nf_conntrack_tuple *tuple, |
c1d10adb PNA |
556 | enum ctattr_tuple type, u_int8_t l3num) |
557 | { | |
df6fb868 | 558 | struct nlattr *tb[CTA_TUPLE_MAX+1]; |
c1d10adb PNA |
559 | int err; |
560 | ||
c1d10adb PNA |
561 | memset(tuple, 0, sizeof(*tuple)); |
562 | ||
df6fb868 | 563 | nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], NULL); |
c1d10adb | 564 | |
df6fb868 | 565 | if (!tb[CTA_TUPLE_IP]) |
c1d10adb PNA |
566 | return -EINVAL; |
567 | ||
568 | tuple->src.l3num = l3num; | |
569 | ||
df6fb868 | 570 | err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple); |
c1d10adb PNA |
571 | if (err < 0) |
572 | return err; | |
573 | ||
df6fb868 | 574 | if (!tb[CTA_TUPLE_PROTO]) |
c1d10adb PNA |
575 | return -EINVAL; |
576 | ||
df6fb868 | 577 | err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple); |
c1d10adb PNA |
578 | if (err < 0) |
579 | return err; | |
580 | ||
581 | /* orig and expect tuples get DIR_ORIGINAL */ | |
582 | if (type == CTA_TUPLE_REPLY) | |
583 | tuple->dst.dir = IP_CT_DIR_REPLY; | |
584 | else | |
585 | tuple->dst.dir = IP_CT_DIR_ORIGINAL; | |
586 | ||
c1d10adb PNA |
587 | return 0; |
588 | } | |
589 | ||
5b1158e9 | 590 | #ifdef CONFIG_NF_NAT_NEEDED |
df6fb868 PM |
591 | static const size_t cta_min_protonat[CTA_PROTONAT_MAX+1] = { |
592 | [CTA_PROTONAT_PORT_MIN] = sizeof(u_int16_t), | |
593 | [CTA_PROTONAT_PORT_MAX] = sizeof(u_int16_t), | |
c1d10adb PNA |
594 | }; |
595 | ||
df6fb868 | 596 | static int nfnetlink_parse_nat_proto(struct nlattr *attr, |
c1d10adb | 597 | const struct nf_conn *ct, |
5b1158e9 | 598 | struct nf_nat_range *range) |
c1d10adb | 599 | { |
df6fb868 | 600 | struct nlattr *tb[CTA_PROTONAT_MAX+1]; |
5b1158e9 | 601 | struct nf_nat_protocol *npt; |
c1d10adb | 602 | |
df6fb868 | 603 | nla_parse_nested(tb, CTA_PROTONAT_MAX, attr, NULL); |
c1d10adb PNA |
604 | |
605 | if (nfattr_bad_size(tb, CTA_PROTONAT_MAX, cta_min_protonat)) | |
606 | return -EINVAL; | |
607 | ||
5b1158e9 | 608 | npt = nf_nat_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum); |
c1d10adb PNA |
609 | |
610 | if (!npt->nfattr_to_range) { | |
5b1158e9 | 611 | nf_nat_proto_put(npt); |
c1d10adb PNA |
612 | return 0; |
613 | } | |
614 | ||
615 | /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */ | |
616 | if (npt->nfattr_to_range(tb, range) > 0) | |
617 | range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED; | |
618 | ||
5b1158e9 | 619 | nf_nat_proto_put(npt); |
c1d10adb | 620 | |
c1d10adb PNA |
621 | return 0; |
622 | } | |
623 | ||
df6fb868 PM |
624 | static const size_t cta_min_nat[CTA_NAT_MAX+1] = { |
625 | [CTA_NAT_MINIP] = sizeof(u_int32_t), | |
626 | [CTA_NAT_MAXIP] = sizeof(u_int32_t), | |
c1d10adb PNA |
627 | }; |
628 | ||
629 | static inline int | |
df6fb868 | 630 | nfnetlink_parse_nat(struct nlattr *nat, |
5b1158e9 | 631 | const struct nf_conn *ct, struct nf_nat_range *range) |
c1d10adb | 632 | { |
df6fb868 | 633 | struct nlattr *tb[CTA_NAT_MAX+1]; |
c1d10adb PNA |
634 | int err; |
635 | ||
c1d10adb | 636 | memset(range, 0, sizeof(*range)); |
601e68e1 | 637 | |
df6fb868 | 638 | nla_parse_nested(tb, CTA_NAT_MAX, nat, NULL); |
c1d10adb PNA |
639 | |
640 | if (nfattr_bad_size(tb, CTA_NAT_MAX, cta_min_nat)) | |
641 | return -EINVAL; | |
642 | ||
df6fb868 PM |
643 | if (tb[CTA_NAT_MINIP]) |
644 | range->min_ip = *(__be32 *)nla_data(tb[CTA_NAT_MINIP]); | |
c1d10adb | 645 | |
df6fb868 | 646 | if (!tb[CTA_NAT_MAXIP]) |
c1d10adb PNA |
647 | range->max_ip = range->min_ip; |
648 | else | |
df6fb868 | 649 | range->max_ip = *(__be32 *)nla_data(tb[CTA_NAT_MAXIP]); |
c1d10adb PNA |
650 | |
651 | if (range->min_ip) | |
652 | range->flags |= IP_NAT_RANGE_MAP_IPS; | |
653 | ||
df6fb868 | 654 | if (!tb[CTA_NAT_PROTO]) |
c1d10adb PNA |
655 | return 0; |
656 | ||
df6fb868 | 657 | err = nfnetlink_parse_nat_proto(tb[CTA_NAT_PROTO], ct, range); |
c1d10adb PNA |
658 | if (err < 0) |
659 | return err; | |
660 | ||
c1d10adb PNA |
661 | return 0; |
662 | } | |
663 | #endif | |
664 | ||
665 | static inline int | |
df6fb868 | 666 | ctnetlink_parse_help(struct nlattr *attr, char **helper_name) |
c1d10adb | 667 | { |
df6fb868 | 668 | struct nlattr *tb[CTA_HELP_MAX+1]; |
c1d10adb | 669 | |
df6fb868 | 670 | nla_parse_nested(tb, CTA_HELP_MAX, attr, NULL); |
c1d10adb | 671 | |
df6fb868 | 672 | if (!tb[CTA_HELP_NAME]) |
c1d10adb PNA |
673 | return -EINVAL; |
674 | ||
df6fb868 | 675 | *helper_name = nla_data(tb[CTA_HELP_NAME]); |
c1d10adb PNA |
676 | |
677 | return 0; | |
678 | } | |
679 | ||
df6fb868 PM |
680 | static const size_t cta_min[CTA_MAX+1] = { |
681 | [CTA_STATUS] = sizeof(u_int32_t), | |
682 | [CTA_TIMEOUT] = sizeof(u_int32_t), | |
683 | [CTA_MARK] = sizeof(u_int32_t), | |
684 | [CTA_USE] = sizeof(u_int32_t), | |
685 | [CTA_ID] = sizeof(u_int32_t) | |
c1d10adb PNA |
686 | }; |
687 | ||
688 | static int | |
601e68e1 | 689 | ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb, |
df6fb868 | 690 | struct nlmsghdr *nlh, struct nlattr *cda[]) |
c1d10adb PNA |
691 | { |
692 | struct nf_conntrack_tuple_hash *h; | |
693 | struct nf_conntrack_tuple tuple; | |
694 | struct nf_conn *ct; | |
695 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); | |
696 | u_int8_t u3 = nfmsg->nfgen_family; | |
697 | int err = 0; | |
698 | ||
c1d10adb PNA |
699 | if (nfattr_bad_size(cda, CTA_MAX, cta_min)) |
700 | return -EINVAL; | |
701 | ||
df6fb868 | 702 | if (cda[CTA_TUPLE_ORIG]) |
c1d10adb | 703 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3); |
df6fb868 | 704 | else if (cda[CTA_TUPLE_REPLY]) |
c1d10adb PNA |
705 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3); |
706 | else { | |
707 | /* Flush the whole table */ | |
708 | nf_conntrack_flush(); | |
709 | return 0; | |
710 | } | |
711 | ||
712 | if (err < 0) | |
713 | return err; | |
714 | ||
330f7db5 | 715 | h = nf_conntrack_find_get(&tuple); |
9ea8cfd6 | 716 | if (!h) |
c1d10adb | 717 | return -ENOENT; |
c1d10adb PNA |
718 | |
719 | ct = nf_ct_tuplehash_to_ctrack(h); | |
601e68e1 | 720 | |
df6fb868 PM |
721 | if (cda[CTA_ID]) { |
722 | u_int32_t id = ntohl(*(__be32 *)nla_data(cda[CTA_ID])); | |
c1d10adb PNA |
723 | if (ct->id != id) { |
724 | nf_ct_put(ct); | |
725 | return -ENOENT; | |
726 | } | |
601e68e1 | 727 | } |
c1d10adb PNA |
728 | if (del_timer(&ct->timeout)) |
729 | ct->timeout.function((unsigned long)ct); | |
730 | ||
731 | nf_ct_put(ct); | |
c1d10adb PNA |
732 | |
733 | return 0; | |
734 | } | |
735 | ||
736 | static int | |
601e68e1 | 737 | ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb, |
df6fb868 | 738 | struct nlmsghdr *nlh, struct nlattr *cda[]) |
c1d10adb PNA |
739 | { |
740 | struct nf_conntrack_tuple_hash *h; | |
741 | struct nf_conntrack_tuple tuple; | |
742 | struct nf_conn *ct; | |
743 | struct sk_buff *skb2 = NULL; | |
744 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); | |
745 | u_int8_t u3 = nfmsg->nfgen_family; | |
746 | int err = 0; | |
747 | ||
c1d10adb | 748 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
01f34848 PNA |
749 | #ifndef CONFIG_NF_CT_ACCT |
750 | if (NFNL_MSG_TYPE(nlh->nlmsg_type) == IPCTNL_MSG_CT_GET_CTRZERO) | |
c1d10adb PNA |
751 | return -ENOTSUPP; |
752 | #endif | |
c702e804 TG |
753 | return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table, |
754 | ctnetlink_done); | |
c1d10adb PNA |
755 | } |
756 | ||
757 | if (nfattr_bad_size(cda, CTA_MAX, cta_min)) | |
758 | return -EINVAL; | |
759 | ||
df6fb868 | 760 | if (cda[CTA_TUPLE_ORIG]) |
c1d10adb | 761 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3); |
df6fb868 | 762 | else if (cda[CTA_TUPLE_REPLY]) |
c1d10adb PNA |
763 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3); |
764 | else | |
765 | return -EINVAL; | |
766 | ||
767 | if (err < 0) | |
768 | return err; | |
769 | ||
330f7db5 | 770 | h = nf_conntrack_find_get(&tuple); |
9ea8cfd6 | 771 | if (!h) |
c1d10adb | 772 | return -ENOENT; |
9ea8cfd6 | 773 | |
c1d10adb PNA |
774 | ct = nf_ct_tuplehash_to_ctrack(h); |
775 | ||
776 | err = -ENOMEM; | |
777 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
778 | if (!skb2) { | |
779 | nf_ct_put(ct); | |
780 | return -ENOMEM; | |
781 | } | |
c1d10adb | 782 | |
601e68e1 | 783 | err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq, |
c1d10adb PNA |
784 | IPCTNL_MSG_CT_NEW, 1, ct); |
785 | nf_ct_put(ct); | |
786 | if (err <= 0) | |
787 | goto free; | |
788 | ||
789 | err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT); | |
790 | if (err < 0) | |
791 | goto out; | |
792 | ||
c1d10adb PNA |
793 | return 0; |
794 | ||
795 | free: | |
796 | kfree_skb(skb2); | |
797 | out: | |
798 | return err; | |
799 | } | |
800 | ||
801 | static inline int | |
df6fb868 | 802 | ctnetlink_change_status(struct nf_conn *ct, struct nlattr *cda[]) |
c1d10adb PNA |
803 | { |
804 | unsigned long d; | |
df6fb868 | 805 | unsigned int status = ntohl(*(__be32 *)nla_data(cda[CTA_STATUS])); |
c1d10adb PNA |
806 | d = ct->status ^ status; |
807 | ||
808 | if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING)) | |
809 | /* unchangeable */ | |
810 | return -EINVAL; | |
601e68e1 | 811 | |
c1d10adb PNA |
812 | if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY)) |
813 | /* SEEN_REPLY bit can only be set */ | |
814 | return -EINVAL; | |
815 | ||
601e68e1 | 816 | |
c1d10adb PNA |
817 | if (d & IPS_ASSURED && !(status & IPS_ASSURED)) |
818 | /* ASSURED bit can only be set */ | |
819 | return -EINVAL; | |
820 | ||
df6fb868 | 821 | if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) { |
5b1158e9 | 822 | #ifndef CONFIG_NF_NAT_NEEDED |
c1d10adb PNA |
823 | return -EINVAL; |
824 | #else | |
5b1158e9 | 825 | struct nf_nat_range range; |
c1d10adb | 826 | |
df6fb868 PM |
827 | if (cda[CTA_NAT_DST]) { |
828 | if (nfnetlink_parse_nat(cda[CTA_NAT_DST], ct, | |
3726add7 PM |
829 | &range) < 0) |
830 | return -EINVAL; | |
5b1158e9 | 831 | if (nf_nat_initialized(ct, |
3726add7 PM |
832 | HOOK2MANIP(NF_IP_PRE_ROUTING))) |
833 | return -EEXIST; | |
5b1158e9 | 834 | nf_nat_setup_info(ct, &range, NF_IP_PRE_ROUTING); |
3726add7 | 835 | } |
df6fb868 PM |
836 | if (cda[CTA_NAT_SRC]) { |
837 | if (nfnetlink_parse_nat(cda[CTA_NAT_SRC], ct, | |
3726add7 PM |
838 | &range) < 0) |
839 | return -EINVAL; | |
5b1158e9 | 840 | if (nf_nat_initialized(ct, |
3726add7 PM |
841 | HOOK2MANIP(NF_IP_POST_ROUTING))) |
842 | return -EEXIST; | |
5b1158e9 | 843 | nf_nat_setup_info(ct, &range, NF_IP_POST_ROUTING); |
3726add7 | 844 | } |
c1d10adb PNA |
845 | #endif |
846 | } | |
847 | ||
848 | /* Be careful here, modifying NAT bits can screw up things, | |
849 | * so don't let users modify them directly if they don't pass | |
5b1158e9 | 850 | * nf_nat_range. */ |
c1d10adb PNA |
851 | ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK); |
852 | return 0; | |
853 | } | |
854 | ||
855 | ||
856 | static inline int | |
df6fb868 | 857 | ctnetlink_change_helper(struct nf_conn *ct, struct nlattr *cda[]) |
c1d10adb PNA |
858 | { |
859 | struct nf_conntrack_helper *helper; | |
dc808fe2 | 860 | struct nf_conn_help *help = nfct_help(ct); |
c1d10adb PNA |
861 | char *helpname; |
862 | int err; | |
863 | ||
c1d10adb PNA |
864 | /* don't change helper of sibling connections */ |
865 | if (ct->master) | |
866 | return -EINVAL; | |
867 | ||
df6fb868 | 868 | err = ctnetlink_parse_help(cda[CTA_HELP], &helpname); |
c1d10adb PNA |
869 | if (err < 0) |
870 | return err; | |
871 | ||
df293bbb YK |
872 | if (!strcmp(helpname, "")) { |
873 | if (help && help->helper) { | |
c1d10adb PNA |
874 | /* we had a helper before ... */ |
875 | nf_ct_remove_expectations(ct); | |
3c158f7f | 876 | rcu_assign_pointer(help->helper, NULL); |
c1d10adb | 877 | } |
df293bbb YK |
878 | |
879 | return 0; | |
c1d10adb | 880 | } |
601e68e1 | 881 | |
df293bbb YK |
882 | helper = __nf_conntrack_helper_find_byname(helpname); |
883 | if (helper == NULL) | |
884 | return -EINVAL; | |
885 | ||
ceceae1b YK |
886 | if (help) { |
887 | if (help->helper == helper) | |
888 | return 0; | |
889 | if (help->helper) | |
890 | return -EBUSY; | |
891 | /* need to zero data of old helper */ | |
892 | memset(&help->help, 0, sizeof(help->help)); | |
893 | } else { | |
b560580a | 894 | help = nf_ct_helper_ext_add(ct, GFP_KERNEL); |
ceceae1b YK |
895 | if (help == NULL) |
896 | return -ENOMEM; | |
897 | } | |
df293bbb | 898 | |
3c158f7f | 899 | rcu_assign_pointer(help->helper, helper); |
c1d10adb PNA |
900 | |
901 | return 0; | |
902 | } | |
903 | ||
904 | static inline int | |
df6fb868 | 905 | ctnetlink_change_timeout(struct nf_conn *ct, struct nlattr *cda[]) |
c1d10adb | 906 | { |
df6fb868 | 907 | u_int32_t timeout = ntohl(*(__be32 *)nla_data(cda[CTA_TIMEOUT])); |
601e68e1 | 908 | |
c1d10adb PNA |
909 | if (!del_timer(&ct->timeout)) |
910 | return -ETIME; | |
911 | ||
912 | ct->timeout.expires = jiffies + timeout * HZ; | |
913 | add_timer(&ct->timeout); | |
914 | ||
915 | return 0; | |
916 | } | |
917 | ||
918 | static inline int | |
df6fb868 | 919 | ctnetlink_change_protoinfo(struct nf_conn *ct, struct nlattr *cda[]) |
c1d10adb | 920 | { |
df6fb868 | 921 | struct nlattr *tb[CTA_PROTOINFO_MAX+1], *attr = cda[CTA_PROTOINFO]; |
605dcad6 | 922 | struct nf_conntrack_l4proto *l4proto; |
c1d10adb PNA |
923 | u_int16_t npt = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum; |
924 | u_int16_t l3num = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num; | |
925 | int err = 0; | |
926 | ||
df6fb868 | 927 | nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, NULL); |
c1d10adb | 928 | |
605dcad6 | 929 | l4proto = nf_ct_l4proto_find_get(l3num, npt); |
c1d10adb | 930 | |
605dcad6 MJ |
931 | if (l4proto->from_nfattr) |
932 | err = l4proto->from_nfattr(tb, ct); | |
933 | nf_ct_l4proto_put(l4proto); | |
c1d10adb PNA |
934 | |
935 | return err; | |
936 | } | |
937 | ||
938 | static int | |
df6fb868 | 939 | ctnetlink_change_conntrack(struct nf_conn *ct, struct nlattr *cda[]) |
c1d10adb PNA |
940 | { |
941 | int err; | |
942 | ||
df6fb868 | 943 | if (cda[CTA_HELP]) { |
c1d10adb PNA |
944 | err = ctnetlink_change_helper(ct, cda); |
945 | if (err < 0) | |
946 | return err; | |
947 | } | |
948 | ||
df6fb868 | 949 | if (cda[CTA_TIMEOUT]) { |
c1d10adb PNA |
950 | err = ctnetlink_change_timeout(ct, cda); |
951 | if (err < 0) | |
952 | return err; | |
953 | } | |
954 | ||
df6fb868 | 955 | if (cda[CTA_STATUS]) { |
c1d10adb PNA |
956 | err = ctnetlink_change_status(ct, cda); |
957 | if (err < 0) | |
958 | return err; | |
959 | } | |
960 | ||
df6fb868 | 961 | if (cda[CTA_PROTOINFO]) { |
c1d10adb PNA |
962 | err = ctnetlink_change_protoinfo(ct, cda); |
963 | if (err < 0) | |
964 | return err; | |
965 | } | |
966 | ||
bcd1e830 | 967 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
df6fb868 PM |
968 | if (cda[CTA_MARK]) |
969 | ct->mark = ntohl(*(__be32 *)nla_data(cda[CTA_MARK])); | |
c1d10adb PNA |
970 | #endif |
971 | ||
c1d10adb PNA |
972 | return 0; |
973 | } | |
974 | ||
975 | static int | |
df6fb868 | 976 | ctnetlink_create_conntrack(struct nlattr *cda[], |
c1d10adb PNA |
977 | struct nf_conntrack_tuple *otuple, |
978 | struct nf_conntrack_tuple *rtuple) | |
979 | { | |
980 | struct nf_conn *ct; | |
981 | int err = -EINVAL; | |
dafc741c | 982 | struct nf_conn_help *help; |
ceceae1b | 983 | struct nf_conntrack_helper *helper; |
c1d10adb | 984 | |
c1d10adb PNA |
985 | ct = nf_conntrack_alloc(otuple, rtuple); |
986 | if (ct == NULL || IS_ERR(ct)) | |
601e68e1 | 987 | return -ENOMEM; |
c1d10adb | 988 | |
df6fb868 | 989 | if (!cda[CTA_TIMEOUT]) |
c1d10adb | 990 | goto err; |
df6fb868 | 991 | ct->timeout.expires = ntohl(*(__be32 *)nla_data(cda[CTA_TIMEOUT])); |
c1d10adb PNA |
992 | |
993 | ct->timeout.expires = jiffies + ct->timeout.expires * HZ; | |
994 | ct->status |= IPS_CONFIRMED; | |
995 | ||
df6fb868 | 996 | if (cda[CTA_STATUS]) { |
bbb3357d PNA |
997 | err = ctnetlink_change_status(ct, cda); |
998 | if (err < 0) | |
999 | goto err; | |
1000 | } | |
c1d10adb | 1001 | |
df6fb868 | 1002 | if (cda[CTA_PROTOINFO]) { |
c1d10adb PNA |
1003 | err = ctnetlink_change_protoinfo(ct, cda); |
1004 | if (err < 0) | |
c54ea3b9 | 1005 | goto err; |
c1d10adb PNA |
1006 | } |
1007 | ||
bcd1e830 | 1008 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
df6fb868 PM |
1009 | if (cda[CTA_MARK]) |
1010 | ct->mark = ntohl(*(__be32 *)nla_data(cda[CTA_MARK])); | |
c1d10adb PNA |
1011 | #endif |
1012 | ||
ceceae1b YK |
1013 | helper = nf_ct_helper_find_get(rtuple); |
1014 | if (helper) { | |
b560580a | 1015 | help = nf_ct_helper_ext_add(ct, GFP_KERNEL); |
ceceae1b YK |
1016 | if (help == NULL) { |
1017 | nf_ct_helper_put(helper); | |
1018 | err = -ENOMEM; | |
1019 | goto err; | |
1020 | } | |
3c158f7f PM |
1021 | /* not in hash table yet so not strictly necessary */ |
1022 | rcu_assign_pointer(help->helper, helper); | |
1023 | } | |
dafc741c | 1024 | |
c1d10adb PNA |
1025 | add_timer(&ct->timeout); |
1026 | nf_conntrack_hash_insert(ct); | |
1027 | ||
3c158f7f PM |
1028 | if (helper) |
1029 | nf_ct_helper_put(helper); | |
dafc741c | 1030 | |
c1d10adb PNA |
1031 | return 0; |
1032 | ||
601e68e1 | 1033 | err: |
c1d10adb PNA |
1034 | nf_conntrack_free(ct); |
1035 | return err; | |
1036 | } | |
1037 | ||
601e68e1 YH |
1038 | static int |
1039 | ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb, | |
df6fb868 | 1040 | struct nlmsghdr *nlh, struct nlattr *cda[]) |
c1d10adb PNA |
1041 | { |
1042 | struct nf_conntrack_tuple otuple, rtuple; | |
1043 | struct nf_conntrack_tuple_hash *h = NULL; | |
1044 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); | |
1045 | u_int8_t u3 = nfmsg->nfgen_family; | |
1046 | int err = 0; | |
1047 | ||
c1d10adb PNA |
1048 | if (nfattr_bad_size(cda, CTA_MAX, cta_min)) |
1049 | return -EINVAL; | |
1050 | ||
df6fb868 | 1051 | if (cda[CTA_TUPLE_ORIG]) { |
c1d10adb PNA |
1052 | err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3); |
1053 | if (err < 0) | |
1054 | return err; | |
1055 | } | |
1056 | ||
df6fb868 | 1057 | if (cda[CTA_TUPLE_REPLY]) { |
c1d10adb PNA |
1058 | err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3); |
1059 | if (err < 0) | |
1060 | return err; | |
1061 | } | |
1062 | ||
1063 | write_lock_bh(&nf_conntrack_lock); | |
df6fb868 | 1064 | if (cda[CTA_TUPLE_ORIG]) |
c1d10adb | 1065 | h = __nf_conntrack_find(&otuple, NULL); |
df6fb868 | 1066 | else if (cda[CTA_TUPLE_REPLY]) |
c1d10adb PNA |
1067 | h = __nf_conntrack_find(&rtuple, NULL); |
1068 | ||
1069 | if (h == NULL) { | |
1070 | write_unlock_bh(&nf_conntrack_lock); | |
c1d10adb PNA |
1071 | err = -ENOENT; |
1072 | if (nlh->nlmsg_flags & NLM_F_CREATE) | |
1073 | err = ctnetlink_create_conntrack(cda, &otuple, &rtuple); | |
1074 | return err; | |
1075 | } | |
1076 | /* implicit 'else' */ | |
1077 | ||
c1d10adb PNA |
1078 | /* We manipulate the conntrack inside the global conntrack table lock, |
1079 | * so there's no need to increase the refcount */ | |
c1d10adb | 1080 | err = -EEXIST; |
ff4ca827 PNA |
1081 | if (!(nlh->nlmsg_flags & NLM_F_EXCL)) { |
1082 | /* we only allow nat config for new conntracks */ | |
df6fb868 | 1083 | if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) { |
ff4ca827 PNA |
1084 | err = -EINVAL; |
1085 | goto out_unlock; | |
1086 | } | |
1087 | err = ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h), | |
1088 | cda); | |
1089 | } | |
c1d10adb PNA |
1090 | |
1091 | out_unlock: | |
1092 | write_unlock_bh(&nf_conntrack_lock); | |
1093 | return err; | |
1094 | } | |
1095 | ||
601e68e1 YH |
1096 | /*********************************************************************** |
1097 | * EXPECT | |
1098 | ***********************************************************************/ | |
c1d10adb PNA |
1099 | |
1100 | static inline int | |
1101 | ctnetlink_exp_dump_tuple(struct sk_buff *skb, | |
1102 | const struct nf_conntrack_tuple *tuple, | |
1103 | enum ctattr_expect type) | |
1104 | { | |
df6fb868 | 1105 | struct nlattr *nest_parms; |
601e68e1 | 1106 | |
df6fb868 PM |
1107 | nest_parms = nla_nest_start(skb, type | NLA_F_NESTED); |
1108 | if (!nest_parms) | |
1109 | goto nla_put_failure; | |
c1d10adb | 1110 | if (ctnetlink_dump_tuples(skb, tuple) < 0) |
df6fb868 PM |
1111 | goto nla_put_failure; |
1112 | nla_nest_end(skb, nest_parms); | |
c1d10adb PNA |
1113 | |
1114 | return 0; | |
1115 | ||
df6fb868 | 1116 | nla_put_failure: |
c1d10adb | 1117 | return -1; |
601e68e1 | 1118 | } |
c1d10adb | 1119 | |
1cde6436 PNA |
1120 | static inline int |
1121 | ctnetlink_exp_dump_mask(struct sk_buff *skb, | |
1122 | const struct nf_conntrack_tuple *tuple, | |
d4156e8c | 1123 | const struct nf_conntrack_tuple_mask *mask) |
1cde6436 PNA |
1124 | { |
1125 | int ret; | |
1126 | struct nf_conntrack_l3proto *l3proto; | |
605dcad6 | 1127 | struct nf_conntrack_l4proto *l4proto; |
d4156e8c | 1128 | struct nf_conntrack_tuple m; |
df6fb868 | 1129 | struct nlattr *nest_parms; |
d4156e8c PM |
1130 | |
1131 | memset(&m, 0xFF, sizeof(m)); | |
1132 | m.src.u.all = mask->src.u.all; | |
1133 | memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3)); | |
1134 | ||
df6fb868 PM |
1135 | nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED); |
1136 | if (!nest_parms) | |
1137 | goto nla_put_failure; | |
1cde6436 PNA |
1138 | |
1139 | l3proto = nf_ct_l3proto_find_get(tuple->src.l3num); | |
d4156e8c | 1140 | ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto); |
1cde6436 PNA |
1141 | nf_ct_l3proto_put(l3proto); |
1142 | ||
1143 | if (unlikely(ret < 0)) | |
df6fb868 | 1144 | goto nla_put_failure; |
1cde6436 | 1145 | |
605dcad6 | 1146 | l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum); |
d4156e8c | 1147 | ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto); |
605dcad6 | 1148 | nf_ct_l4proto_put(l4proto); |
1cde6436 | 1149 | if (unlikely(ret < 0)) |
df6fb868 | 1150 | goto nla_put_failure; |
1cde6436 | 1151 | |
df6fb868 | 1152 | nla_nest_end(skb, nest_parms); |
1cde6436 PNA |
1153 | |
1154 | return 0; | |
1155 | ||
df6fb868 | 1156 | nla_put_failure: |
1cde6436 PNA |
1157 | return -1; |
1158 | } | |
1159 | ||
c1d10adb PNA |
1160 | static inline int |
1161 | ctnetlink_exp_dump_expect(struct sk_buff *skb, | |
601e68e1 | 1162 | const struct nf_conntrack_expect *exp) |
c1d10adb PNA |
1163 | { |
1164 | struct nf_conn *master = exp->master; | |
bff9a89b PM |
1165 | __be32 timeout = htonl((exp->timeout.expires - jiffies) / HZ); |
1166 | __be32 id = htonl(exp->id); | |
c1d10adb PNA |
1167 | |
1168 | if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0) | |
df6fb868 | 1169 | goto nla_put_failure; |
1cde6436 | 1170 | if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0) |
df6fb868 | 1171 | goto nla_put_failure; |
c1d10adb PNA |
1172 | if (ctnetlink_exp_dump_tuple(skb, |
1173 | &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple, | |
1174 | CTA_EXPECT_MASTER) < 0) | |
df6fb868 | 1175 | goto nla_put_failure; |
601e68e1 | 1176 | |
df6fb868 PM |
1177 | NLA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout); |
1178 | NLA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id); | |
c1d10adb PNA |
1179 | |
1180 | return 0; | |
601e68e1 | 1181 | |
df6fb868 | 1182 | nla_put_failure: |
c1d10adb PNA |
1183 | return -1; |
1184 | } | |
1185 | ||
1186 | static int | |
1187 | ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq, | |
601e68e1 YH |
1188 | int event, |
1189 | int nowait, | |
c1d10adb PNA |
1190 | const struct nf_conntrack_expect *exp) |
1191 | { | |
1192 | struct nlmsghdr *nlh; | |
1193 | struct nfgenmsg *nfmsg; | |
27a884dc | 1194 | unsigned char *b = skb_tail_pointer(skb); |
c1d10adb PNA |
1195 | |
1196 | event |= NFNL_SUBSYS_CTNETLINK_EXP << 8; | |
1197 | nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg)); | |
1198 | nfmsg = NLMSG_DATA(nlh); | |
1199 | ||
1200 | nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0; | |
1201 | nfmsg->nfgen_family = exp->tuple.src.l3num; | |
1202 | nfmsg->version = NFNETLINK_V0; | |
1203 | nfmsg->res_id = 0; | |
1204 | ||
1205 | if (ctnetlink_exp_dump_expect(skb, exp) < 0) | |
df6fb868 | 1206 | goto nla_put_failure; |
c1d10adb | 1207 | |
27a884dc | 1208 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
c1d10adb PNA |
1209 | return skb->len; |
1210 | ||
1211 | nlmsg_failure: | |
df6fb868 | 1212 | nla_put_failure: |
dc5fc579 | 1213 | nlmsg_trim(skb, b); |
c1d10adb PNA |
1214 | return -1; |
1215 | } | |
1216 | ||
1217 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
1218 | static int ctnetlink_expect_event(struct notifier_block *this, | |
1219 | unsigned long events, void *ptr) | |
1220 | { | |
1221 | struct nlmsghdr *nlh; | |
1222 | struct nfgenmsg *nfmsg; | |
1223 | struct nf_conntrack_expect *exp = (struct nf_conntrack_expect *)ptr; | |
1224 | struct sk_buff *skb; | |
1225 | unsigned int type; | |
27a884dc | 1226 | sk_buff_data_t b; |
c1d10adb PNA |
1227 | int flags = 0; |
1228 | ||
1229 | if (events & IPEXP_NEW) { | |
1230 | type = IPCTNL_MSG_EXP_NEW; | |
1231 | flags = NLM_F_CREATE|NLM_F_EXCL; | |
1232 | } else | |
1233 | return NOTIFY_DONE; | |
1234 | ||
b3a27bfb PNA |
1235 | if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW)) |
1236 | return NOTIFY_DONE; | |
1237 | ||
c1d10adb PNA |
1238 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
1239 | if (!skb) | |
1240 | return NOTIFY_DONE; | |
1241 | ||
1242 | b = skb->tail; | |
1243 | ||
b633ad5f | 1244 | type |= NFNL_SUBSYS_CTNETLINK_EXP << 8; |
c1d10adb PNA |
1245 | nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg)); |
1246 | nfmsg = NLMSG_DATA(nlh); | |
1247 | ||
1248 | nlh->nlmsg_flags = flags; | |
1249 | nfmsg->nfgen_family = exp->tuple.src.l3num; | |
1250 | nfmsg->version = NFNETLINK_V0; | |
1251 | nfmsg->res_id = 0; | |
1252 | ||
1253 | if (ctnetlink_exp_dump_expect(skb, exp) < 0) | |
df6fb868 | 1254 | goto nla_put_failure; |
c1d10adb PNA |
1255 | |
1256 | nlh->nlmsg_len = skb->tail - b; | |
1257 | nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0); | |
1258 | return NOTIFY_DONE; | |
1259 | ||
1260 | nlmsg_failure: | |
df6fb868 | 1261 | nla_put_failure: |
c1d10adb PNA |
1262 | kfree_skb(skb); |
1263 | return NOTIFY_DONE; | |
1264 | } | |
1265 | #endif | |
cf6994c2 PM |
1266 | static int ctnetlink_exp_done(struct netlink_callback *cb) |
1267 | { | |
31f15875 PM |
1268 | if (cb->args[1]) |
1269 | nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]); | |
cf6994c2 PM |
1270 | return 0; |
1271 | } | |
c1d10adb PNA |
1272 | |
1273 | static int | |
1274 | ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb) | |
1275 | { | |
cf6994c2 | 1276 | struct nf_conntrack_expect *exp, *last; |
87711cb8 | 1277 | struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh); |
31f15875 | 1278 | struct hlist_node *n; |
87711cb8 | 1279 | u_int8_t l3proto = nfmsg->nfgen_family; |
c1d10adb | 1280 | |
c1d10adb | 1281 | read_lock_bh(&nf_conntrack_lock); |
31f15875 PM |
1282 | last = (struct nf_conntrack_expect *)cb->args[1]; |
1283 | for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) { | |
cf6994c2 | 1284 | restart: |
31f15875 PM |
1285 | hlist_for_each_entry(exp, n, &nf_ct_expect_hash[cb->args[0]], |
1286 | hnode) { | |
1287 | if (l3proto && exp->tuple.src.l3num != l3proto) | |
cf6994c2 | 1288 | continue; |
31f15875 PM |
1289 | if (cb->args[1]) { |
1290 | if (exp != last) | |
1291 | continue; | |
1292 | cb->args[1] = 0; | |
1293 | } | |
1294 | if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid, | |
1295 | cb->nlh->nlmsg_seq, | |
1296 | IPCTNL_MSG_EXP_NEW, | |
1297 | 1, exp) < 0) { | |
1298 | atomic_inc(&exp->use); | |
1299 | cb->args[1] = (unsigned long)exp; | |
1300 | goto out; | |
1301 | } | |
cf6994c2 | 1302 | } |
31f15875 PM |
1303 | if (cb->args[1]) { |
1304 | cb->args[1] = 0; | |
1305 | goto restart; | |
cf6994c2 PM |
1306 | } |
1307 | } | |
601e68e1 | 1308 | out: |
c1d10adb | 1309 | read_unlock_bh(&nf_conntrack_lock); |
cf6994c2 PM |
1310 | if (last) |
1311 | nf_ct_expect_put(last); | |
c1d10adb | 1312 | |
c1d10adb PNA |
1313 | return skb->len; |
1314 | } | |
1315 | ||
df6fb868 PM |
1316 | static const size_t cta_min_exp[CTA_EXPECT_MAX+1] = { |
1317 | [CTA_EXPECT_TIMEOUT] = sizeof(u_int32_t), | |
1318 | [CTA_EXPECT_ID] = sizeof(u_int32_t) | |
c1d10adb PNA |
1319 | }; |
1320 | ||
1321 | static int | |
601e68e1 | 1322 | ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb, |
df6fb868 | 1323 | struct nlmsghdr *nlh, struct nlattr *cda[]) |
c1d10adb PNA |
1324 | { |
1325 | struct nf_conntrack_tuple tuple; | |
1326 | struct nf_conntrack_expect *exp; | |
1327 | struct sk_buff *skb2; | |
1328 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); | |
1329 | u_int8_t u3 = nfmsg->nfgen_family; | |
1330 | int err = 0; | |
1331 | ||
c1d10adb PNA |
1332 | if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp)) |
1333 | return -EINVAL; | |
1334 | ||
1335 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
c702e804 TG |
1336 | return netlink_dump_start(ctnl, skb, nlh, |
1337 | ctnetlink_exp_dump_table, | |
cf6994c2 | 1338 | ctnetlink_exp_done); |
c1d10adb PNA |
1339 | } |
1340 | ||
df6fb868 | 1341 | if (cda[CTA_EXPECT_MASTER]) |
c1d10adb PNA |
1342 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3); |
1343 | else | |
1344 | return -EINVAL; | |
1345 | ||
1346 | if (err < 0) | |
1347 | return err; | |
1348 | ||
6823645d | 1349 | exp = nf_ct_expect_find_get(&tuple); |
c1d10adb PNA |
1350 | if (!exp) |
1351 | return -ENOENT; | |
1352 | ||
df6fb868 PM |
1353 | if (cda[CTA_EXPECT_ID]) { |
1354 | __be32 id = *(__be32 *)nla_data(cda[CTA_EXPECT_ID]); | |
c1d10adb | 1355 | if (exp->id != ntohl(id)) { |
6823645d | 1356 | nf_ct_expect_put(exp); |
c1d10adb PNA |
1357 | return -ENOENT; |
1358 | } | |
601e68e1 | 1359 | } |
c1d10adb PNA |
1360 | |
1361 | err = -ENOMEM; | |
1362 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
1363 | if (!skb2) | |
1364 | goto out; | |
4e9b8269 | 1365 | |
601e68e1 | 1366 | err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid, |
c1d10adb PNA |
1367 | nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, |
1368 | 1, exp); | |
1369 | if (err <= 0) | |
1370 | goto free; | |
1371 | ||
6823645d | 1372 | nf_ct_expect_put(exp); |
c1d10adb PNA |
1373 | |
1374 | return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT); | |
1375 | ||
1376 | free: | |
1377 | kfree_skb(skb2); | |
1378 | out: | |
6823645d | 1379 | nf_ct_expect_put(exp); |
c1d10adb PNA |
1380 | return err; |
1381 | } | |
1382 | ||
1383 | static int | |
601e68e1 | 1384 | ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb, |
df6fb868 | 1385 | struct nlmsghdr *nlh, struct nlattr *cda[]) |
c1d10adb | 1386 | { |
31f15875 | 1387 | struct nf_conntrack_expect *exp; |
c1d10adb PNA |
1388 | struct nf_conntrack_tuple tuple; |
1389 | struct nf_conntrack_helper *h; | |
1390 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); | |
31f15875 | 1391 | struct hlist_node *n, *next; |
c1d10adb | 1392 | u_int8_t u3 = nfmsg->nfgen_family; |
31f15875 | 1393 | unsigned int i; |
c1d10adb PNA |
1394 | int err; |
1395 | ||
1396 | if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp)) | |
1397 | return -EINVAL; | |
1398 | ||
df6fb868 | 1399 | if (cda[CTA_EXPECT_TUPLE]) { |
c1d10adb PNA |
1400 | /* delete a single expect by tuple */ |
1401 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); | |
1402 | if (err < 0) | |
1403 | return err; | |
1404 | ||
1405 | /* bump usage count to 2 */ | |
6823645d | 1406 | exp = nf_ct_expect_find_get(&tuple); |
c1d10adb PNA |
1407 | if (!exp) |
1408 | return -ENOENT; | |
1409 | ||
df6fb868 PM |
1410 | if (cda[CTA_EXPECT_ID]) { |
1411 | __be32 id = *(__be32 *)nla_data(cda[CTA_EXPECT_ID]); | |
c1d10adb | 1412 | if (exp->id != ntohl(id)) { |
6823645d | 1413 | nf_ct_expect_put(exp); |
c1d10adb PNA |
1414 | return -ENOENT; |
1415 | } | |
1416 | } | |
1417 | ||
1418 | /* after list removal, usage count == 1 */ | |
6823645d | 1419 | nf_ct_unexpect_related(exp); |
601e68e1 | 1420 | /* have to put what we 'get' above. |
c1d10adb | 1421 | * after this line usage count == 0 */ |
6823645d | 1422 | nf_ct_expect_put(exp); |
df6fb868 PM |
1423 | } else if (cda[CTA_EXPECT_HELP_NAME]) { |
1424 | char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]); | |
31f15875 | 1425 | struct nf_conn_help *m_help; |
c1d10adb PNA |
1426 | |
1427 | /* delete all expectations for this helper */ | |
1428 | write_lock_bh(&nf_conntrack_lock); | |
1429 | h = __nf_conntrack_helper_find_byname(name); | |
1430 | if (!h) { | |
1431 | write_unlock_bh(&nf_conntrack_lock); | |
1432 | return -EINVAL; | |
1433 | } | |
31f15875 PM |
1434 | for (i = 0; i < nf_ct_expect_hsize; i++) { |
1435 | hlist_for_each_entry_safe(exp, n, next, | |
1436 | &nf_ct_expect_hash[i], | |
1437 | hnode) { | |
1438 | m_help = nfct_help(exp->master); | |
1439 | if (m_help->helper == h | |
1440 | && del_timer(&exp->timeout)) { | |
1441 | nf_ct_unlink_expect(exp); | |
1442 | nf_ct_expect_put(exp); | |
1443 | } | |
c1d10adb PNA |
1444 | } |
1445 | } | |
1446 | write_unlock_bh(&nf_conntrack_lock); | |
1447 | } else { | |
1448 | /* This basically means we have to flush everything*/ | |
1449 | write_lock_bh(&nf_conntrack_lock); | |
31f15875 PM |
1450 | for (i = 0; i < nf_ct_expect_hsize; i++) { |
1451 | hlist_for_each_entry_safe(exp, n, next, | |
1452 | &nf_ct_expect_hash[i], | |
1453 | hnode) { | |
1454 | if (del_timer(&exp->timeout)) { | |
1455 | nf_ct_unlink_expect(exp); | |
1456 | nf_ct_expect_put(exp); | |
1457 | } | |
c1d10adb PNA |
1458 | } |
1459 | } | |
1460 | write_unlock_bh(&nf_conntrack_lock); | |
1461 | } | |
1462 | ||
1463 | return 0; | |
1464 | } | |
1465 | static int | |
df6fb868 | 1466 | ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nlattr *cda[]) |
c1d10adb PNA |
1467 | { |
1468 | return -EOPNOTSUPP; | |
1469 | } | |
1470 | ||
1471 | static int | |
df6fb868 | 1472 | ctnetlink_create_expect(struct nlattr *cda[], u_int8_t u3) |
c1d10adb PNA |
1473 | { |
1474 | struct nf_conntrack_tuple tuple, mask, master_tuple; | |
1475 | struct nf_conntrack_tuple_hash *h = NULL; | |
1476 | struct nf_conntrack_expect *exp; | |
1477 | struct nf_conn *ct; | |
dc808fe2 | 1478 | struct nf_conn_help *help; |
c1d10adb PNA |
1479 | int err = 0; |
1480 | ||
c1d10adb PNA |
1481 | /* caller guarantees that those three CTA_EXPECT_* exist */ |
1482 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); | |
1483 | if (err < 0) | |
1484 | return err; | |
1485 | err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3); | |
1486 | if (err < 0) | |
1487 | return err; | |
1488 | err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3); | |
1489 | if (err < 0) | |
1490 | return err; | |
1491 | ||
1492 | /* Look for master conntrack of this expectation */ | |
330f7db5 | 1493 | h = nf_conntrack_find_get(&master_tuple); |
c1d10adb PNA |
1494 | if (!h) |
1495 | return -ENOENT; | |
1496 | ct = nf_ct_tuplehash_to_ctrack(h); | |
dc808fe2 | 1497 | help = nfct_help(ct); |
c1d10adb | 1498 | |
dc808fe2 | 1499 | if (!help || !help->helper) { |
c1d10adb PNA |
1500 | /* such conntrack hasn't got any helper, abort */ |
1501 | err = -EINVAL; | |
1502 | goto out; | |
1503 | } | |
1504 | ||
6823645d | 1505 | exp = nf_ct_expect_alloc(ct); |
c1d10adb PNA |
1506 | if (!exp) { |
1507 | err = -ENOMEM; | |
1508 | goto out; | |
1509 | } | |
601e68e1 | 1510 | |
c1d10adb PNA |
1511 | exp->expectfn = NULL; |
1512 | exp->flags = 0; | |
1513 | exp->master = ct; | |
9457d851 | 1514 | exp->helper = NULL; |
c1d10adb | 1515 | memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple)); |
d4156e8c PM |
1516 | memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3)); |
1517 | exp->mask.src.u.all = mask.src.u.all; | |
c1d10adb | 1518 | |
6823645d PM |
1519 | err = nf_ct_expect_related(exp); |
1520 | nf_ct_expect_put(exp); | |
c1d10adb | 1521 | |
601e68e1 | 1522 | out: |
c1d10adb PNA |
1523 | nf_ct_put(nf_ct_tuplehash_to_ctrack(h)); |
1524 | return err; | |
1525 | } | |
1526 | ||
1527 | static int | |
1528 | ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb, | |
df6fb868 | 1529 | struct nlmsghdr *nlh, struct nlattr *cda[]) |
c1d10adb PNA |
1530 | { |
1531 | struct nf_conntrack_tuple tuple; | |
1532 | struct nf_conntrack_expect *exp; | |
1533 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); | |
1534 | u_int8_t u3 = nfmsg->nfgen_family; | |
1535 | int err = 0; | |
1536 | ||
c1d10adb PNA |
1537 | if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp)) |
1538 | return -EINVAL; | |
1539 | ||
df6fb868 PM |
1540 | if (!cda[CTA_EXPECT_TUPLE] |
1541 | || !cda[CTA_EXPECT_MASK] | |
1542 | || !cda[CTA_EXPECT_MASTER]) | |
c1d10adb PNA |
1543 | return -EINVAL; |
1544 | ||
1545 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); | |
1546 | if (err < 0) | |
1547 | return err; | |
1548 | ||
1549 | write_lock_bh(&nf_conntrack_lock); | |
6823645d | 1550 | exp = __nf_ct_expect_find(&tuple); |
c1d10adb PNA |
1551 | |
1552 | if (!exp) { | |
1553 | write_unlock_bh(&nf_conntrack_lock); | |
1554 | err = -ENOENT; | |
1555 | if (nlh->nlmsg_flags & NLM_F_CREATE) | |
1556 | err = ctnetlink_create_expect(cda, u3); | |
1557 | return err; | |
1558 | } | |
1559 | ||
1560 | err = -EEXIST; | |
1561 | if (!(nlh->nlmsg_flags & NLM_F_EXCL)) | |
1562 | err = ctnetlink_change_expect(exp, cda); | |
1563 | write_unlock_bh(&nf_conntrack_lock); | |
1564 | ||
c1d10adb PNA |
1565 | return err; |
1566 | } | |
1567 | ||
1568 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
1569 | static struct notifier_block ctnl_notifier = { | |
1570 | .notifier_call = ctnetlink_conntrack_event, | |
1571 | }; | |
1572 | ||
1573 | static struct notifier_block ctnl_notifier_exp = { | |
1574 | .notifier_call = ctnetlink_expect_event, | |
1575 | }; | |
1576 | #endif | |
1577 | ||
7c8d4cb4 | 1578 | static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = { |
c1d10adb PNA |
1579 | [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack, |
1580 | .attr_count = CTA_MAX, }, | |
1581 | [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack, | |
1582 | .attr_count = CTA_MAX, }, | |
1583 | [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack, | |
1584 | .attr_count = CTA_MAX, }, | |
1585 | [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack, | |
1586 | .attr_count = CTA_MAX, }, | |
1587 | }; | |
1588 | ||
7c8d4cb4 | 1589 | static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = { |
c1d10adb PNA |
1590 | [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect, |
1591 | .attr_count = CTA_EXPECT_MAX, }, | |
1592 | [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect, | |
1593 | .attr_count = CTA_EXPECT_MAX, }, | |
1594 | [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect, | |
1595 | .attr_count = CTA_EXPECT_MAX, }, | |
1596 | }; | |
1597 | ||
7c8d4cb4 | 1598 | static const struct nfnetlink_subsystem ctnl_subsys = { |
c1d10adb PNA |
1599 | .name = "conntrack", |
1600 | .subsys_id = NFNL_SUBSYS_CTNETLINK, | |
1601 | .cb_count = IPCTNL_MSG_MAX, | |
1602 | .cb = ctnl_cb, | |
1603 | }; | |
1604 | ||
7c8d4cb4 | 1605 | static const struct nfnetlink_subsystem ctnl_exp_subsys = { |
c1d10adb PNA |
1606 | .name = "conntrack_expect", |
1607 | .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP, | |
1608 | .cb_count = IPCTNL_MSG_EXP_MAX, | |
1609 | .cb = ctnl_exp_cb, | |
1610 | }; | |
1611 | ||
d2483dde | 1612 | MODULE_ALIAS("ip_conntrack_netlink"); |
c1d10adb | 1613 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK); |
34f9a2e4 | 1614 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP); |
c1d10adb PNA |
1615 | |
1616 | static int __init ctnetlink_init(void) | |
1617 | { | |
1618 | int ret; | |
1619 | ||
1620 | printk("ctnetlink v%s: registering with nfnetlink.\n", version); | |
1621 | ret = nfnetlink_subsys_register(&ctnl_subsys); | |
1622 | if (ret < 0) { | |
1623 | printk("ctnetlink_init: cannot register with nfnetlink.\n"); | |
1624 | goto err_out; | |
1625 | } | |
1626 | ||
1627 | ret = nfnetlink_subsys_register(&ctnl_exp_subsys); | |
1628 | if (ret < 0) { | |
1629 | printk("ctnetlink_init: cannot register exp with nfnetlink.\n"); | |
1630 | goto err_unreg_subsys; | |
1631 | } | |
1632 | ||
1633 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
1634 | ret = nf_conntrack_register_notifier(&ctnl_notifier); | |
1635 | if (ret < 0) { | |
1636 | printk("ctnetlink_init: cannot register notifier.\n"); | |
1637 | goto err_unreg_exp_subsys; | |
1638 | } | |
1639 | ||
6823645d | 1640 | ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp); |
c1d10adb PNA |
1641 | if (ret < 0) { |
1642 | printk("ctnetlink_init: cannot expect register notifier.\n"); | |
1643 | goto err_unreg_notifier; | |
1644 | } | |
1645 | #endif | |
1646 | ||
1647 | return 0; | |
1648 | ||
1649 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
1650 | err_unreg_notifier: | |
1651 | nf_conntrack_unregister_notifier(&ctnl_notifier); | |
1652 | err_unreg_exp_subsys: | |
1653 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); | |
1654 | #endif | |
1655 | err_unreg_subsys: | |
1656 | nfnetlink_subsys_unregister(&ctnl_subsys); | |
1657 | err_out: | |
1658 | return ret; | |
1659 | } | |
1660 | ||
1661 | static void __exit ctnetlink_exit(void) | |
1662 | { | |
1663 | printk("ctnetlink: unregistering from nfnetlink.\n"); | |
1664 | ||
1665 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
6823645d | 1666 | nf_ct_expect_unregister_notifier(&ctnl_notifier_exp); |
c1d10adb PNA |
1667 | nf_conntrack_unregister_notifier(&ctnl_notifier); |
1668 | #endif | |
1669 | ||
1670 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); | |
1671 | nfnetlink_subsys_unregister(&ctnl_subsys); | |
1672 | return; | |
1673 | } | |
1674 | ||
1675 | module_init(ctnetlink_init); | |
1676 | module_exit(ctnetlink_exit); |