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> |
70e9942f | 7 | * (C) 2005-2011 by Pablo Neira Ayuso <pablo@netfilter.org> |
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> | |
711bbdd6 | 21 | #include <linux/rculist.h> |
ea781f19 | 22 | #include <linux/rculist_nulls.h> |
c1d10adb PNA |
23 | #include <linux/types.h> |
24 | #include <linux/timer.h> | |
1cc63249 | 25 | #include <linux/security.h> |
c1d10adb PNA |
26 | #include <linux/skbuff.h> |
27 | #include <linux/errno.h> | |
28 | #include <linux/netlink.h> | |
29 | #include <linux/spinlock.h> | |
40a839fd | 30 | #include <linux/interrupt.h> |
5a0e3ad6 | 31 | #include <linux/slab.h> |
c1d10adb PNA |
32 | |
33 | #include <linux/netfilter.h> | |
dc5fc579 | 34 | #include <net/netlink.h> |
9592a5c0 | 35 | #include <net/sock.h> |
c1d10adb PNA |
36 | #include <net/netfilter/nf_conntrack.h> |
37 | #include <net/netfilter/nf_conntrack_core.h> | |
77ab9cff | 38 | #include <net/netfilter/nf_conntrack_expect.h> |
c1d10adb PNA |
39 | #include <net/netfilter/nf_conntrack_helper.h> |
40 | #include <net/netfilter/nf_conntrack_l3proto.h> | |
605dcad6 | 41 | #include <net/netfilter/nf_conntrack_l4proto.h> |
5b1158e9 | 42 | #include <net/netfilter/nf_conntrack_tuple.h> |
58401572 | 43 | #include <net/netfilter/nf_conntrack_acct.h> |
ef00f89f | 44 | #include <net/netfilter/nf_conntrack_zones.h> |
a992ca2a | 45 | #include <net/netfilter/nf_conntrack_timestamp.h> |
5b1158e9 JK |
46 | #ifdef CONFIG_NF_NAT_NEEDED |
47 | #include <net/netfilter/nf_nat_core.h> | |
48 | #include <net/netfilter/nf_nat_protocol.h> | |
49 | #endif | |
c1d10adb PNA |
50 | |
51 | #include <linux/netfilter/nfnetlink.h> | |
52 | #include <linux/netfilter/nfnetlink_conntrack.h> | |
53 | ||
54 | MODULE_LICENSE("GPL"); | |
55 | ||
dc808fe2 | 56 | static char __initdata version[] = "0.93"; |
c1d10adb | 57 | |
c1d10adb | 58 | static inline int |
601e68e1 | 59 | ctnetlink_dump_tuples_proto(struct sk_buff *skb, |
1cde6436 | 60 | const struct nf_conntrack_tuple *tuple, |
605dcad6 | 61 | struct nf_conntrack_l4proto *l4proto) |
c1d10adb | 62 | { |
c1d10adb | 63 | int ret = 0; |
df6fb868 | 64 | struct nlattr *nest_parms; |
c1d10adb | 65 | |
df6fb868 PM |
66 | nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED); |
67 | if (!nest_parms) | |
68 | goto nla_put_failure; | |
77236b6e | 69 | NLA_PUT_U8(skb, CTA_PROTO_NUM, tuple->dst.protonum); |
c1d10adb | 70 | |
fdf70832 PM |
71 | if (likely(l4proto->tuple_to_nlattr)) |
72 | ret = l4proto->tuple_to_nlattr(skb, tuple); | |
601e68e1 | 73 | |
df6fb868 | 74 | nla_nest_end(skb, nest_parms); |
c1d10adb PNA |
75 | |
76 | return ret; | |
77 | ||
df6fb868 | 78 | nla_put_failure: |
c1d10adb PNA |
79 | return -1; |
80 | } | |
81 | ||
82 | static inline int | |
1cde6436 PNA |
83 | ctnetlink_dump_tuples_ip(struct sk_buff *skb, |
84 | const struct nf_conntrack_tuple *tuple, | |
85 | struct nf_conntrack_l3proto *l3proto) | |
c1d10adb | 86 | { |
c1d10adb | 87 | int ret = 0; |
df6fb868 PM |
88 | struct nlattr *nest_parms; |
89 | ||
90 | nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED); | |
91 | if (!nest_parms) | |
92 | goto nla_put_failure; | |
1cde6436 | 93 | |
fdf70832 PM |
94 | if (likely(l3proto->tuple_to_nlattr)) |
95 | ret = l3proto->tuple_to_nlattr(skb, tuple); | |
1cde6436 | 96 | |
df6fb868 | 97 | nla_nest_end(skb, nest_parms); |
c1d10adb | 98 | |
1cde6436 PNA |
99 | return ret; |
100 | ||
df6fb868 | 101 | nla_put_failure: |
1cde6436 PNA |
102 | return -1; |
103 | } | |
104 | ||
bb5cf80e | 105 | static int |
1cde6436 PNA |
106 | ctnetlink_dump_tuples(struct sk_buff *skb, |
107 | const struct nf_conntrack_tuple *tuple) | |
108 | { | |
109 | int ret; | |
110 | struct nf_conntrack_l3proto *l3proto; | |
605dcad6 | 111 | struct nf_conntrack_l4proto *l4proto; |
1cde6436 | 112 | |
528a3a6f | 113 | l3proto = __nf_ct_l3proto_find(tuple->src.l3num); |
1cde6436 | 114 | ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto); |
c1d10adb PNA |
115 | |
116 | if (unlikely(ret < 0)) | |
117 | return ret; | |
118 | ||
528a3a6f | 119 | l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum); |
605dcad6 | 120 | ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto); |
c1d10adb PNA |
121 | |
122 | return ret; | |
c1d10adb PNA |
123 | } |
124 | ||
125 | static inline int | |
126 | ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct) | |
127 | { | |
77236b6e | 128 | NLA_PUT_BE32(skb, CTA_STATUS, htonl(ct->status)); |
c1d10adb PNA |
129 | return 0; |
130 | ||
df6fb868 | 131 | nla_put_failure: |
c1d10adb PNA |
132 | return -1; |
133 | } | |
134 | ||
135 | static inline int | |
136 | ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct) | |
137 | { | |
c1216382 | 138 | long timeout = ((long)ct->timeout.expires - (long)jiffies) / HZ; |
c1d10adb | 139 | |
77236b6e | 140 | if (timeout < 0) |
c1d10adb | 141 | timeout = 0; |
601e68e1 | 142 | |
77236b6e | 143 | NLA_PUT_BE32(skb, CTA_TIMEOUT, htonl(timeout)); |
c1d10adb PNA |
144 | return 0; |
145 | ||
df6fb868 | 146 | nla_put_failure: |
c1d10adb PNA |
147 | return -1; |
148 | } | |
149 | ||
150 | static inline int | |
440f0d58 | 151 | ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct) |
c1d10adb | 152 | { |
5e8fbe2a | 153 | struct nf_conntrack_l4proto *l4proto; |
df6fb868 | 154 | struct nlattr *nest_proto; |
c1d10adb PNA |
155 | int ret; |
156 | ||
528a3a6f PNA |
157 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); |
158 | if (!l4proto->to_nlattr) | |
c1d10adb | 159 | return 0; |
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 | |
fdf70832 | 165 | ret = l4proto->to_nlattr(skb, nest_proto, ct); |
c1d10adb | 166 | |
df6fb868 | 167 | nla_nest_end(skb, nest_proto); |
c1d10adb PNA |
168 | |
169 | return ret; | |
170 | ||
df6fb868 | 171 | nla_put_failure: |
c1d10adb PNA |
172 | return -1; |
173 | } | |
174 | ||
175 | static inline int | |
176 | ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct) | |
177 | { | |
df6fb868 | 178 | struct nlattr *nest_helper; |
dc808fe2 | 179 | const struct nf_conn_help *help = nfct_help(ct); |
3c158f7f | 180 | struct nf_conntrack_helper *helper; |
c1d10adb | 181 | |
3c158f7f | 182 | if (!help) |
c1d10adb | 183 | return 0; |
601e68e1 | 184 | |
3c158f7f PM |
185 | helper = rcu_dereference(help->helper); |
186 | if (!helper) | |
187 | goto out; | |
188 | ||
df6fb868 PM |
189 | nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED); |
190 | if (!nest_helper) | |
191 | goto nla_put_failure; | |
77236b6e | 192 | NLA_PUT_STRING(skb, CTA_HELP_NAME, helper->name); |
c1d10adb | 193 | |
fdf70832 PM |
194 | if (helper->to_nlattr) |
195 | helper->to_nlattr(skb, ct); | |
c1d10adb | 196 | |
df6fb868 | 197 | nla_nest_end(skb, nest_helper); |
3c158f7f | 198 | out: |
c1d10adb PNA |
199 | return 0; |
200 | ||
df6fb868 | 201 | nla_put_failure: |
c1d10adb PNA |
202 | return -1; |
203 | } | |
204 | ||
bb5cf80e | 205 | static int |
80e60e67 PNA |
206 | dump_counters(struct sk_buff *skb, u64 pkts, u64 bytes, |
207 | enum ip_conntrack_dir dir) | |
c1d10adb PNA |
208 | { |
209 | enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG; | |
df6fb868 | 210 | struct nlattr *nest_count; |
c1d10adb | 211 | |
df6fb868 PM |
212 | nest_count = nla_nest_start(skb, type | NLA_F_NESTED); |
213 | if (!nest_count) | |
214 | goto nla_put_failure; | |
215 | ||
80e60e67 PNA |
216 | NLA_PUT_BE64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts)); |
217 | NLA_PUT_BE64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes)); | |
c1d10adb | 218 | |
df6fb868 | 219 | nla_nest_end(skb, nest_count); |
c1d10adb PNA |
220 | |
221 | return 0; | |
222 | ||
df6fb868 | 223 | nla_put_failure: |
c1d10adb PNA |
224 | return -1; |
225 | } | |
c1d10adb | 226 | |
80e60e67 PNA |
227 | static int |
228 | ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct, | |
229 | enum ip_conntrack_dir dir, int type) | |
230 | { | |
231 | struct nf_conn_counter *acct; | |
232 | u64 pkts, bytes; | |
233 | ||
234 | acct = nf_conn_acct_find(ct); | |
235 | if (!acct) | |
236 | return 0; | |
237 | ||
238 | if (type == IPCTNL_MSG_CT_GET_CTRZERO) { | |
239 | pkts = atomic64_xchg(&acct[dir].packets, 0); | |
240 | bytes = atomic64_xchg(&acct[dir].bytes, 0); | |
241 | } else { | |
242 | pkts = atomic64_read(&acct[dir].packets); | |
243 | bytes = atomic64_read(&acct[dir].bytes); | |
244 | } | |
245 | return dump_counters(skb, pkts, bytes, dir); | |
246 | } | |
247 | ||
a992ca2a PNA |
248 | static int |
249 | ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct) | |
250 | { | |
251 | struct nlattr *nest_count; | |
252 | const struct nf_conn_tstamp *tstamp; | |
253 | ||
254 | tstamp = nf_conn_tstamp_find(ct); | |
255 | if (!tstamp) | |
256 | return 0; | |
257 | ||
258 | nest_count = nla_nest_start(skb, CTA_TIMESTAMP | NLA_F_NESTED); | |
259 | if (!nest_count) | |
260 | goto nla_put_failure; | |
261 | ||
262 | NLA_PUT_BE64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start)); | |
263 | if (tstamp->stop != 0) { | |
264 | NLA_PUT_BE64(skb, CTA_TIMESTAMP_STOP, | |
265 | cpu_to_be64(tstamp->stop)); | |
266 | } | |
267 | nla_nest_end(skb, nest_count); | |
268 | ||
269 | return 0; | |
270 | ||
271 | nla_put_failure: | |
272 | return -1; | |
273 | } | |
274 | ||
c1d10adb PNA |
275 | #ifdef CONFIG_NF_CONNTRACK_MARK |
276 | static inline int | |
277 | ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct) | |
278 | { | |
77236b6e | 279 | NLA_PUT_BE32(skb, CTA_MARK, htonl(ct->mark)); |
c1d10adb PNA |
280 | return 0; |
281 | ||
df6fb868 | 282 | nla_put_failure: |
c1d10adb PNA |
283 | return -1; |
284 | } | |
285 | #else | |
286 | #define ctnetlink_dump_mark(a, b) (0) | |
287 | #endif | |
288 | ||
37fccd85 PNA |
289 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
290 | static inline int | |
1cc63249 | 291 | ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct) |
37fccd85 | 292 | { |
1cc63249 EP |
293 | struct nlattr *nest_secctx; |
294 | int len, ret; | |
295 | char *secctx; | |
296 | ||
297 | ret = security_secid_to_secctx(ct->secmark, &secctx, &len); | |
298 | if (ret) | |
cba85b53 | 299 | return 0; |
1cc63249 EP |
300 | |
301 | ret = -1; | |
302 | nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED); | |
303 | if (!nest_secctx) | |
304 | goto nla_put_failure; | |
37fccd85 | 305 | |
1cc63249 EP |
306 | NLA_PUT_STRING(skb, CTA_SECCTX_NAME, secctx); |
307 | nla_nest_end(skb, nest_secctx); | |
308 | ||
309 | ret = 0; | |
37fccd85 | 310 | nla_put_failure: |
1cc63249 EP |
311 | security_release_secctx(secctx, len); |
312 | return ret; | |
37fccd85 PNA |
313 | } |
314 | #else | |
1cc63249 | 315 | #define ctnetlink_dump_secctx(a, b) (0) |
37fccd85 PNA |
316 | #endif |
317 | ||
0f417ce9 PNA |
318 | #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple) |
319 | ||
320 | static inline int | |
321 | ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct) | |
322 | { | |
323 | struct nlattr *nest_parms; | |
324 | ||
325 | if (!(ct->status & IPS_EXPECTED)) | |
326 | return 0; | |
327 | ||
328 | nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED); | |
329 | if (!nest_parms) | |
330 | goto nla_put_failure; | |
331 | if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0) | |
332 | goto nla_put_failure; | |
333 | nla_nest_end(skb, nest_parms); | |
334 | ||
335 | return 0; | |
336 | ||
337 | nla_put_failure: | |
338 | return -1; | |
339 | } | |
340 | ||
13eae15a | 341 | #ifdef CONFIG_NF_NAT_NEEDED |
bb5cf80e | 342 | static int |
13eae15a PNA |
343 | dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type) |
344 | { | |
13eae15a PNA |
345 | struct nlattr *nest_parms; |
346 | ||
347 | nest_parms = nla_nest_start(skb, type | NLA_F_NESTED); | |
348 | if (!nest_parms) | |
349 | goto nla_put_failure; | |
350 | ||
77236b6e PM |
351 | NLA_PUT_BE32(skb, CTA_NAT_SEQ_CORRECTION_POS, |
352 | htonl(natseq->correction_pos)); | |
353 | NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_BEFORE, | |
354 | htonl(natseq->offset_before)); | |
355 | NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_AFTER, | |
356 | htonl(natseq->offset_after)); | |
13eae15a PNA |
357 | |
358 | nla_nest_end(skb, nest_parms); | |
359 | ||
360 | return 0; | |
361 | ||
362 | nla_put_failure: | |
363 | return -1; | |
364 | } | |
365 | ||
366 | static inline int | |
367 | ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct) | |
368 | { | |
369 | struct nf_nat_seq *natseq; | |
370 | struct nf_conn_nat *nat = nfct_nat(ct); | |
371 | ||
372 | if (!(ct->status & IPS_SEQ_ADJUST) || !nat) | |
373 | return 0; | |
374 | ||
375 | natseq = &nat->seq[IP_CT_DIR_ORIGINAL]; | |
376 | if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1) | |
377 | return -1; | |
378 | ||
379 | natseq = &nat->seq[IP_CT_DIR_REPLY]; | |
380 | if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1) | |
381 | return -1; | |
382 | ||
383 | return 0; | |
384 | } | |
385 | #else | |
386 | #define ctnetlink_dump_nat_seq_adj(a, b) (0) | |
387 | #endif | |
388 | ||
c1d10adb PNA |
389 | static inline int |
390 | ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct) | |
391 | { | |
77236b6e | 392 | NLA_PUT_BE32(skb, CTA_ID, htonl((unsigned long)ct)); |
c1d10adb PNA |
393 | return 0; |
394 | ||
df6fb868 | 395 | nla_put_failure: |
c1d10adb PNA |
396 | return -1; |
397 | } | |
398 | ||
399 | static inline int | |
400 | ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct) | |
401 | { | |
77236b6e | 402 | NLA_PUT_BE32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use))); |
c1d10adb PNA |
403 | return 0; |
404 | ||
df6fb868 | 405 | nla_put_failure: |
c1d10adb PNA |
406 | return -1; |
407 | } | |
408 | ||
c1d10adb | 409 | static int |
80e60e67 PNA |
410 | ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq, u32 type, |
411 | struct nf_conn *ct) | |
c1d10adb PNA |
412 | { |
413 | struct nlmsghdr *nlh; | |
414 | struct nfgenmsg *nfmsg; | |
df6fb868 | 415 | struct nlattr *nest_parms; |
80e60e67 | 416 | unsigned int flags = pid ? NLM_F_MULTI : 0, event; |
c1d10adb | 417 | |
80e60e67 | 418 | event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_NEW); |
96bcf938 PNA |
419 | nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags); |
420 | if (nlh == NULL) | |
421 | goto nlmsg_failure; | |
c1d10adb | 422 | |
96bcf938 | 423 | nfmsg = nlmsg_data(nlh); |
5e8fbe2a | 424 | nfmsg->nfgen_family = nf_ct_l3num(ct); |
c1d10adb PNA |
425 | nfmsg->version = NFNETLINK_V0; |
426 | nfmsg->res_id = 0; | |
427 | ||
df6fb868 PM |
428 | nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED); |
429 | if (!nest_parms) | |
430 | goto nla_put_failure; | |
f2f3e38c | 431 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) |
df6fb868 PM |
432 | goto nla_put_failure; |
433 | nla_nest_end(skb, nest_parms); | |
601e68e1 | 434 | |
df6fb868 PM |
435 | nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED); |
436 | if (!nest_parms) | |
437 | goto nla_put_failure; | |
f2f3e38c | 438 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0) |
df6fb868 PM |
439 | goto nla_put_failure; |
440 | nla_nest_end(skb, nest_parms); | |
c1d10adb | 441 | |
ef00f89f PM |
442 | if (nf_ct_zone(ct)) |
443 | NLA_PUT_BE16(skb, CTA_ZONE, htons(nf_ct_zone(ct))); | |
444 | ||
c1d10adb PNA |
445 | if (ctnetlink_dump_status(skb, ct) < 0 || |
446 | ctnetlink_dump_timeout(skb, ct) < 0 || | |
80e60e67 PNA |
447 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL, type) < 0 || |
448 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY, type) < 0 || | |
a992ca2a | 449 | ctnetlink_dump_timestamp(skb, ct) < 0 || |
c1d10adb PNA |
450 | ctnetlink_dump_protoinfo(skb, ct) < 0 || |
451 | ctnetlink_dump_helpinfo(skb, ct) < 0 || | |
452 | ctnetlink_dump_mark(skb, ct) < 0 || | |
1cc63249 | 453 | ctnetlink_dump_secctx(skb, ct) < 0 || |
c1d10adb | 454 | ctnetlink_dump_id(skb, ct) < 0 || |
13eae15a | 455 | ctnetlink_dump_use(skb, ct) < 0 || |
0f417ce9 | 456 | ctnetlink_dump_master(skb, ct) < 0 || |
13eae15a | 457 | ctnetlink_dump_nat_seq_adj(skb, ct) < 0) |
df6fb868 | 458 | goto nla_put_failure; |
c1d10adb | 459 | |
96bcf938 | 460 | nlmsg_end(skb, nlh); |
c1d10adb PNA |
461 | return skb->len; |
462 | ||
463 | nlmsg_failure: | |
df6fb868 | 464 | nla_put_failure: |
96bcf938 | 465 | nlmsg_cancel(skb, nlh); |
c1d10adb PNA |
466 | return -1; |
467 | } | |
468 | ||
469 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
03b64f51 PNA |
470 | static inline size_t |
471 | ctnetlink_proto_size(const struct nf_conn *ct) | |
2732c4e4 HE |
472 | { |
473 | struct nf_conntrack_l3proto *l3proto; | |
474 | struct nf_conntrack_l4proto *l4proto; | |
03b64f51 PNA |
475 | size_t len = 0; |
476 | ||
477 | rcu_read_lock(); | |
478 | l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct)); | |
479 | len += l3proto->nla_size; | |
480 | ||
481 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); | |
482 | len += l4proto->nla_size; | |
483 | rcu_read_unlock(); | |
484 | ||
485 | return len; | |
486 | } | |
487 | ||
d26e6a02 JP |
488 | static inline size_t |
489 | ctnetlink_counters_size(const struct nf_conn *ct) | |
490 | { | |
491 | if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT)) | |
492 | return 0; | |
493 | return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */ | |
494 | + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */ | |
495 | + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */ | |
496 | ; | |
497 | } | |
498 | ||
cba85b53 PNA |
499 | static inline int |
500 | ctnetlink_secctx_size(const struct nf_conn *ct) | |
1cc63249 | 501 | { |
cba85b53 PNA |
502 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
503 | int len, ret; | |
1cc63249 | 504 | |
cba85b53 PNA |
505 | ret = security_secid_to_secctx(ct->secmark, NULL, &len); |
506 | if (ret) | |
507 | return 0; | |
1cc63249 | 508 | |
cba85b53 PNA |
509 | return nla_total_size(0) /* CTA_SECCTX */ |
510 | + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */ | |
511 | #else | |
512 | return 0; | |
1cc63249 | 513 | #endif |
cba85b53 | 514 | } |
1cc63249 | 515 | |
a992ca2a PNA |
516 | static inline size_t |
517 | ctnetlink_timestamp_size(const struct nf_conn *ct) | |
518 | { | |
519 | #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP | |
520 | if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP)) | |
521 | return 0; | |
522 | return nla_total_size(0) + 2 * nla_total_size(sizeof(uint64_t)); | |
523 | #else | |
524 | return 0; | |
525 | #endif | |
526 | } | |
527 | ||
03b64f51 PNA |
528 | static inline size_t |
529 | ctnetlink_nlmsg_size(const struct nf_conn *ct) | |
530 | { | |
531 | return NLMSG_ALIGN(sizeof(struct nfgenmsg)) | |
532 | + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */ | |
533 | + 3 * nla_total_size(0) /* CTA_TUPLE_IP */ | |
534 | + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */ | |
535 | + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */ | |
536 | + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */ | |
537 | + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */ | |
d26e6a02 | 538 | + ctnetlink_counters_size(ct) |
a992ca2a | 539 | + ctnetlink_timestamp_size(ct) |
03b64f51 PNA |
540 | + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */ |
541 | + nla_total_size(0) /* CTA_PROTOINFO */ | |
542 | + nla_total_size(0) /* CTA_HELP */ | |
543 | + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */ | |
cba85b53 | 544 | + ctnetlink_secctx_size(ct) |
d271e8bd | 545 | #ifdef CONFIG_NF_NAT_NEEDED |
03b64f51 PNA |
546 | + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */ |
547 | + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */ | |
d271e8bd HE |
548 | #endif |
549 | #ifdef CONFIG_NF_CONNTRACK_MARK | |
03b64f51 | 550 | + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */ |
d271e8bd | 551 | #endif |
03b64f51 PNA |
552 | + ctnetlink_proto_size(ct) |
553 | ; | |
2732c4e4 HE |
554 | } |
555 | ||
e34d5c1a PNA |
556 | static int |
557 | ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item) | |
c1d10adb | 558 | { |
9592a5c0 | 559 | struct net *net; |
c1d10adb PNA |
560 | struct nlmsghdr *nlh; |
561 | struct nfgenmsg *nfmsg; | |
df6fb868 | 562 | struct nlattr *nest_parms; |
19abb7b0 | 563 | struct nf_conn *ct = item->ct; |
c1d10adb PNA |
564 | struct sk_buff *skb; |
565 | unsigned int type; | |
c1d10adb | 566 | unsigned int flags = 0, group; |
dd7669a9 | 567 | int err; |
c1d10adb PNA |
568 | |
569 | /* ignore our fake conntrack entry */ | |
5bfddbd4 | 570 | if (nf_ct_is_untracked(ct)) |
e34d5c1a | 571 | return 0; |
c1d10adb | 572 | |
a0891aa6 | 573 | if (events & (1 << IPCT_DESTROY)) { |
c1d10adb PNA |
574 | type = IPCTNL_MSG_CT_DELETE; |
575 | group = NFNLGRP_CONNTRACK_DESTROY; | |
a0891aa6 | 576 | } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) { |
c1d10adb PNA |
577 | type = IPCTNL_MSG_CT_NEW; |
578 | flags = NLM_F_CREATE|NLM_F_EXCL; | |
c1d10adb | 579 | group = NFNLGRP_CONNTRACK_NEW; |
17e6e4ea | 580 | } else if (events) { |
c1d10adb PNA |
581 | type = IPCTNL_MSG_CT_NEW; |
582 | group = NFNLGRP_CONNTRACK_UPDATE; | |
583 | } else | |
e34d5c1a | 584 | return 0; |
a2427692 | 585 | |
9592a5c0 AD |
586 | net = nf_ct_net(ct); |
587 | if (!item->report && !nfnetlink_has_listeners(net, group)) | |
e34d5c1a | 588 | return 0; |
a2427692 | 589 | |
03b64f51 PNA |
590 | skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC); |
591 | if (skb == NULL) | |
150ace0d | 592 | goto errout; |
c1d10adb | 593 | |
c1d10adb | 594 | type |= NFNL_SUBSYS_CTNETLINK << 8; |
96bcf938 PNA |
595 | nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags); |
596 | if (nlh == NULL) | |
597 | goto nlmsg_failure; | |
c1d10adb | 598 | |
96bcf938 | 599 | nfmsg = nlmsg_data(nlh); |
5e8fbe2a | 600 | nfmsg->nfgen_family = nf_ct_l3num(ct); |
c1d10adb PNA |
601 | nfmsg->version = NFNETLINK_V0; |
602 | nfmsg->res_id = 0; | |
603 | ||
528a3a6f | 604 | rcu_read_lock(); |
df6fb868 PM |
605 | nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED); |
606 | if (!nest_parms) | |
607 | goto nla_put_failure; | |
f2f3e38c | 608 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) |
df6fb868 PM |
609 | goto nla_put_failure; |
610 | nla_nest_end(skb, nest_parms); | |
601e68e1 | 611 | |
df6fb868 PM |
612 | nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED); |
613 | if (!nest_parms) | |
614 | goto nla_put_failure; | |
f2f3e38c | 615 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0) |
df6fb868 PM |
616 | goto nla_put_failure; |
617 | nla_nest_end(skb, nest_parms); | |
c1d10adb | 618 | |
ef00f89f PM |
619 | if (nf_ct_zone(ct)) |
620 | NLA_PUT_BE16(skb, CTA_ZONE, htons(nf_ct_zone(ct))); | |
621 | ||
1eedf699 EL |
622 | if (ctnetlink_dump_id(skb, ct) < 0) |
623 | goto nla_put_failure; | |
624 | ||
e57dce60 FH |
625 | if (ctnetlink_dump_status(skb, ct) < 0) |
626 | goto nla_put_failure; | |
627 | ||
a0891aa6 | 628 | if (events & (1 << IPCT_DESTROY)) { |
80e60e67 PNA |
629 | if (ctnetlink_dump_counters(skb, ct, |
630 | IP_CT_DIR_ORIGINAL, type) < 0 || | |
631 | ctnetlink_dump_counters(skb, ct, | |
632 | IP_CT_DIR_REPLY, type) < 0 || | |
a992ca2a | 633 | ctnetlink_dump_timestamp(skb, ct) < 0) |
df6fb868 | 634 | goto nla_put_failure; |
7b621c1e | 635 | } else { |
7b621c1e | 636 | if (ctnetlink_dump_timeout(skb, ct) < 0) |
df6fb868 | 637 | goto nla_put_failure; |
7b621c1e | 638 | |
a0891aa6 | 639 | if (events & (1 << IPCT_PROTOINFO) |
7b621c1e | 640 | && ctnetlink_dump_protoinfo(skb, ct) < 0) |
df6fb868 | 641 | goto nla_put_failure; |
7b621c1e | 642 | |
a0891aa6 | 643 | if ((events & (1 << IPCT_HELPER) || nfct_help(ct)) |
7b621c1e | 644 | && ctnetlink_dump_helpinfo(skb, ct) < 0) |
df6fb868 | 645 | goto nla_put_failure; |
7b621c1e | 646 | |
ff660c80 | 647 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
a0891aa6 | 648 | if ((events & (1 << IPCT_SECMARK) || ct->secmark) |
1cc63249 | 649 | && ctnetlink_dump_secctx(skb, ct) < 0) |
37fccd85 | 650 | goto nla_put_failure; |
ff660c80 | 651 | #endif |
7b621c1e | 652 | |
a0891aa6 | 653 | if (events & (1 << IPCT_RELATED) && |
0f417ce9 PNA |
654 | ctnetlink_dump_master(skb, ct) < 0) |
655 | goto nla_put_failure; | |
656 | ||
a0891aa6 | 657 | if (events & (1 << IPCT_NATSEQADJ) && |
13eae15a PNA |
658 | ctnetlink_dump_nat_seq_adj(skb, ct) < 0) |
659 | goto nla_put_failure; | |
7b621c1e | 660 | } |
b9a37e0c | 661 | |
a83099a6 | 662 | #ifdef CONFIG_NF_CONNTRACK_MARK |
a0891aa6 | 663 | if ((events & (1 << IPCT_MARK) || ct->mark) |
a83099a6 EL |
664 | && ctnetlink_dump_mark(skb, ct) < 0) |
665 | goto nla_put_failure; | |
666 | #endif | |
528a3a6f | 667 | rcu_read_unlock(); |
a83099a6 | 668 | |
96bcf938 | 669 | nlmsg_end(skb, nlh); |
9592a5c0 | 670 | err = nfnetlink_send(skb, net, item->pid, group, item->report, |
cd8c20b6 | 671 | GFP_ATOMIC); |
dd7669a9 PNA |
672 | if (err == -ENOBUFS || err == -EAGAIN) |
673 | return -ENOBUFS; | |
674 | ||
e34d5c1a | 675 | return 0; |
c1d10adb | 676 | |
df6fb868 | 677 | nla_put_failure: |
528a3a6f | 678 | rcu_read_unlock(); |
96bcf938 | 679 | nlmsg_cancel(skb, nlh); |
528a3a6f | 680 | nlmsg_failure: |
c1d10adb | 681 | kfree_skb(skb); |
150ace0d | 682 | errout: |
37b7ef72 PNA |
683 | if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0) |
684 | return -ENOBUFS; | |
685 | ||
e34d5c1a | 686 | return 0; |
c1d10adb PNA |
687 | } |
688 | #endif /* CONFIG_NF_CONNTRACK_EVENTS */ | |
689 | ||
690 | static int ctnetlink_done(struct netlink_callback *cb) | |
691 | { | |
89f2e218 PM |
692 | if (cb->args[1]) |
693 | nf_ct_put((struct nf_conn *)cb->args[1]); | |
0f298a28 PNA |
694 | if (cb->data) |
695 | kfree(cb->data); | |
c1d10adb PNA |
696 | return 0; |
697 | } | |
698 | ||
0f298a28 PNA |
699 | struct ctnetlink_dump_filter { |
700 | struct { | |
701 | u_int32_t val; | |
702 | u_int32_t mask; | |
703 | } mark; | |
704 | }; | |
705 | ||
c1d10adb PNA |
706 | static int |
707 | ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb) | |
708 | { | |
9592a5c0 | 709 | struct net *net = sock_net(skb->sk); |
89f2e218 | 710 | struct nf_conn *ct, *last; |
c1d10adb | 711 | struct nf_conntrack_tuple_hash *h; |
ea781f19 | 712 | struct hlist_nulls_node *n; |
96bcf938 | 713 | struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); |
87711cb8 | 714 | u_int8_t l3proto = nfmsg->nfgen_family; |
0f298a28 PNA |
715 | #ifdef CONFIG_NF_CONNTRACK_MARK |
716 | const struct ctnetlink_dump_filter *filter = cb->data; | |
717 | #endif | |
13ee6ac5 | 718 | spin_lock_bh(&nf_conntrack_lock); |
d205dc40 | 719 | last = (struct nf_conn *)cb->args[1]; |
9ab99d5a | 720 | for (; cb->args[0] < net->ct.htable_size; cb->args[0]++) { |
89f2e218 | 721 | restart: |
13ee6ac5 | 722 | hlist_nulls_for_each_entry(h, n, &net->ct.hash[cb->args[0]], |
ea781f19 | 723 | hnnode) { |
5b1158e9 | 724 | if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL) |
c1d10adb PNA |
725 | continue; |
726 | ct = nf_ct_tuplehash_to_ctrack(h); | |
87711cb8 PNA |
727 | /* Dump entries of a given L3 protocol number. |
728 | * If it is not specified, ie. l3proto == 0, | |
729 | * then dump everything. */ | |
5e8fbe2a | 730 | if (l3proto && nf_ct_l3num(ct) != l3proto) |
13ee6ac5 | 731 | continue; |
d205dc40 PM |
732 | if (cb->args[1]) { |
733 | if (ct != last) | |
13ee6ac5 | 734 | continue; |
d205dc40 | 735 | cb->args[1] = 0; |
89f2e218 | 736 | } |
0f298a28 PNA |
737 | #ifdef CONFIG_NF_CONNTRACK_MARK |
738 | if (filter && !((ct->mark & filter->mark.mask) == | |
739 | filter->mark.val)) { | |
740 | continue; | |
741 | } | |
742 | #endif | |
c1d10adb | 743 | if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid, |
601e68e1 | 744 | cb->nlh->nlmsg_seq, |
80e60e67 PNA |
745 | NFNL_MSG_TYPE( |
746 | cb->nlh->nlmsg_type), | |
747 | ct) < 0) { | |
c71caf41 | 748 | nf_conntrack_get(&ct->ct_general); |
89f2e218 | 749 | cb->args[1] = (unsigned long)ct; |
c1d10adb | 750 | goto out; |
89f2e218 PM |
751 | } |
752 | } | |
d205dc40 | 753 | if (cb->args[1]) { |
89f2e218 PM |
754 | cb->args[1] = 0; |
755 | goto restart; | |
c1d10adb PNA |
756 | } |
757 | } | |
89f2e218 | 758 | out: |
13ee6ac5 | 759 | spin_unlock_bh(&nf_conntrack_lock); |
d205dc40 PM |
760 | if (last) |
761 | nf_ct_put(last); | |
c1d10adb | 762 | |
c1d10adb PNA |
763 | return skb->len; |
764 | } | |
765 | ||
c1d10adb | 766 | static inline int |
df6fb868 | 767 | ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple) |
c1d10adb | 768 | { |
df6fb868 | 769 | struct nlattr *tb[CTA_IP_MAX+1]; |
c1d10adb PNA |
770 | struct nf_conntrack_l3proto *l3proto; |
771 | int ret = 0; | |
772 | ||
df6fb868 | 773 | nla_parse_nested(tb, CTA_IP_MAX, attr, NULL); |
c1d10adb | 774 | |
cd91566e FW |
775 | rcu_read_lock(); |
776 | l3proto = __nf_ct_l3proto_find(tuple->src.l3num); | |
c1d10adb | 777 | |
f73e924c PM |
778 | if (likely(l3proto->nlattr_to_tuple)) { |
779 | ret = nla_validate_nested(attr, CTA_IP_MAX, | |
780 | l3proto->nla_policy); | |
781 | if (ret == 0) | |
782 | ret = l3proto->nlattr_to_tuple(tb, tuple); | |
783 | } | |
c1d10adb | 784 | |
cd91566e | 785 | rcu_read_unlock(); |
c1d10adb | 786 | |
c1d10adb PNA |
787 | return ret; |
788 | } | |
789 | ||
f73e924c PM |
790 | static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = { |
791 | [CTA_PROTO_NUM] = { .type = NLA_U8 }, | |
c1d10adb PNA |
792 | }; |
793 | ||
794 | static inline int | |
df6fb868 | 795 | ctnetlink_parse_tuple_proto(struct nlattr *attr, |
c1d10adb PNA |
796 | struct nf_conntrack_tuple *tuple) |
797 | { | |
df6fb868 | 798 | struct nlattr *tb[CTA_PROTO_MAX+1]; |
605dcad6 | 799 | struct nf_conntrack_l4proto *l4proto; |
c1d10adb PNA |
800 | int ret = 0; |
801 | ||
f73e924c PM |
802 | ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy); |
803 | if (ret < 0) | |
804 | return ret; | |
c1d10adb | 805 | |
df6fb868 | 806 | if (!tb[CTA_PROTO_NUM]) |
c1d10adb | 807 | return -EINVAL; |
77236b6e | 808 | tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]); |
c1d10adb | 809 | |
cd91566e FW |
810 | rcu_read_lock(); |
811 | l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum); | |
c1d10adb | 812 | |
f73e924c PM |
813 | if (likely(l4proto->nlattr_to_tuple)) { |
814 | ret = nla_validate_nested(attr, CTA_PROTO_MAX, | |
815 | l4proto->nla_policy); | |
816 | if (ret == 0) | |
817 | ret = l4proto->nlattr_to_tuple(tb, tuple); | |
818 | } | |
c1d10adb | 819 | |
cd91566e | 820 | rcu_read_unlock(); |
601e68e1 | 821 | |
c1d10adb PNA |
822 | return ret; |
823 | } | |
824 | ||
d0b0268f PM |
825 | static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = { |
826 | [CTA_TUPLE_IP] = { .type = NLA_NESTED }, | |
827 | [CTA_TUPLE_PROTO] = { .type = NLA_NESTED }, | |
828 | }; | |
829 | ||
bb5cf80e | 830 | static int |
39938324 PM |
831 | ctnetlink_parse_tuple(const struct nlattr * const cda[], |
832 | struct nf_conntrack_tuple *tuple, | |
a00f1f36 | 833 | enum ctattr_type type, u_int8_t l3num) |
c1d10adb | 834 | { |
df6fb868 | 835 | struct nlattr *tb[CTA_TUPLE_MAX+1]; |
c1d10adb PNA |
836 | int err; |
837 | ||
c1d10adb PNA |
838 | memset(tuple, 0, sizeof(*tuple)); |
839 | ||
d0b0268f | 840 | nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy); |
c1d10adb | 841 | |
df6fb868 | 842 | if (!tb[CTA_TUPLE_IP]) |
c1d10adb PNA |
843 | return -EINVAL; |
844 | ||
845 | tuple->src.l3num = l3num; | |
846 | ||
df6fb868 | 847 | err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple); |
c1d10adb PNA |
848 | if (err < 0) |
849 | return err; | |
850 | ||
df6fb868 | 851 | if (!tb[CTA_TUPLE_PROTO]) |
c1d10adb PNA |
852 | return -EINVAL; |
853 | ||
df6fb868 | 854 | err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple); |
c1d10adb PNA |
855 | if (err < 0) |
856 | return err; | |
857 | ||
858 | /* orig and expect tuples get DIR_ORIGINAL */ | |
859 | if (type == CTA_TUPLE_REPLY) | |
860 | tuple->dst.dir = IP_CT_DIR_REPLY; | |
861 | else | |
862 | tuple->dst.dir = IP_CT_DIR_ORIGINAL; | |
863 | ||
c1d10adb PNA |
864 | return 0; |
865 | } | |
866 | ||
ef00f89f PM |
867 | static int |
868 | ctnetlink_parse_zone(const struct nlattr *attr, u16 *zone) | |
869 | { | |
870 | if (attr) | |
871 | #ifdef CONFIG_NF_CONNTRACK_ZONES | |
872 | *zone = ntohs(nla_get_be16(attr)); | |
873 | #else | |
874 | return -EOPNOTSUPP; | |
875 | #endif | |
876 | else | |
877 | *zone = 0; | |
878 | ||
879 | return 0; | |
880 | } | |
881 | ||
d0b0268f PM |
882 | static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = { |
883 | [CTA_HELP_NAME] = { .type = NLA_NUL_STRING }, | |
884 | }; | |
885 | ||
c1d10adb | 886 | static inline int |
39938324 | 887 | ctnetlink_parse_help(const struct nlattr *attr, char **helper_name) |
c1d10adb | 888 | { |
df6fb868 | 889 | struct nlattr *tb[CTA_HELP_MAX+1]; |
c1d10adb | 890 | |
d0b0268f | 891 | nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy); |
c1d10adb | 892 | |
df6fb868 | 893 | if (!tb[CTA_HELP_NAME]) |
c1d10adb PNA |
894 | return -EINVAL; |
895 | ||
df6fb868 | 896 | *helper_name = nla_data(tb[CTA_HELP_NAME]); |
c1d10adb PNA |
897 | |
898 | return 0; | |
899 | } | |
900 | ||
f73e924c | 901 | static const struct nla_policy ct_nla_policy[CTA_MAX+1] = { |
d0b0268f PM |
902 | [CTA_TUPLE_ORIG] = { .type = NLA_NESTED }, |
903 | [CTA_TUPLE_REPLY] = { .type = NLA_NESTED }, | |
f73e924c | 904 | [CTA_STATUS] = { .type = NLA_U32 }, |
d0b0268f PM |
905 | [CTA_PROTOINFO] = { .type = NLA_NESTED }, |
906 | [CTA_HELP] = { .type = NLA_NESTED }, | |
907 | [CTA_NAT_SRC] = { .type = NLA_NESTED }, | |
f73e924c PM |
908 | [CTA_TIMEOUT] = { .type = NLA_U32 }, |
909 | [CTA_MARK] = { .type = NLA_U32 }, | |
f73e924c | 910 | [CTA_ID] = { .type = NLA_U32 }, |
d0b0268f PM |
911 | [CTA_NAT_DST] = { .type = NLA_NESTED }, |
912 | [CTA_TUPLE_MASTER] = { .type = NLA_NESTED }, | |
ef00f89f | 913 | [CTA_ZONE] = { .type = NLA_U16 }, |
0f298a28 | 914 | [CTA_MARK_MASK] = { .type = NLA_U32 }, |
c1d10adb PNA |
915 | }; |
916 | ||
917 | static int | |
601e68e1 | 918 | ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb, |
39938324 PM |
919 | const struct nlmsghdr *nlh, |
920 | const struct nlattr * const cda[]) | |
c1d10adb | 921 | { |
9592a5c0 | 922 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
923 | struct nf_conntrack_tuple_hash *h; |
924 | struct nf_conntrack_tuple tuple; | |
925 | struct nf_conn *ct; | |
96bcf938 | 926 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb | 927 | u_int8_t u3 = nfmsg->nfgen_family; |
ef00f89f PM |
928 | u16 zone; |
929 | int err; | |
930 | ||
931 | err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone); | |
932 | if (err < 0) | |
933 | return err; | |
c1d10adb | 934 | |
df6fb868 | 935 | if (cda[CTA_TUPLE_ORIG]) |
c1d10adb | 936 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3); |
df6fb868 | 937 | else if (cda[CTA_TUPLE_REPLY]) |
c1d10adb PNA |
938 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3); |
939 | else { | |
940 | /* Flush the whole table */ | |
9592a5c0 | 941 | nf_conntrack_flush_report(net, |
274d383b PNA |
942 | NETLINK_CB(skb).pid, |
943 | nlmsg_report(nlh)); | |
c1d10adb PNA |
944 | return 0; |
945 | } | |
946 | ||
947 | if (err < 0) | |
948 | return err; | |
949 | ||
ef00f89f | 950 | h = nf_conntrack_find_get(net, zone, &tuple); |
9ea8cfd6 | 951 | if (!h) |
c1d10adb | 952 | return -ENOENT; |
c1d10adb PNA |
953 | |
954 | ct = nf_ct_tuplehash_to_ctrack(h); | |
601e68e1 | 955 | |
df6fb868 | 956 | if (cda[CTA_ID]) { |
77236b6e | 957 | u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID])); |
7f85f914 | 958 | if (id != (u32)(unsigned long)ct) { |
c1d10adb PNA |
959 | nf_ct_put(ct); |
960 | return -ENOENT; | |
961 | } | |
601e68e1 | 962 | } |
c1d10adb | 963 | |
dd7669a9 PNA |
964 | if (nf_conntrack_event_report(IPCT_DESTROY, ct, |
965 | NETLINK_CB(skb).pid, | |
966 | nlmsg_report(nlh)) < 0) { | |
967 | nf_ct_delete_from_lists(ct); | |
968 | /* we failed to report the event, try later */ | |
969 | nf_ct_insert_dying_list(ct); | |
970 | nf_ct_put(ct); | |
971 | return 0; | |
972 | } | |
19abb7b0 PNA |
973 | |
974 | /* death_by_timeout would report the event again */ | |
975 | set_bit(IPS_DYING_BIT, &ct->status); | |
976 | ||
51091764 | 977 | nf_ct_kill(ct); |
c1d10adb | 978 | nf_ct_put(ct); |
c1d10adb PNA |
979 | |
980 | return 0; | |
981 | } | |
982 | ||
983 | static int | |
601e68e1 | 984 | ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb, |
39938324 PM |
985 | const struct nlmsghdr *nlh, |
986 | const struct nlattr * const cda[]) | |
c1d10adb | 987 | { |
9592a5c0 | 988 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
989 | struct nf_conntrack_tuple_hash *h; |
990 | struct nf_conntrack_tuple tuple; | |
991 | struct nf_conn *ct; | |
992 | struct sk_buff *skb2 = NULL; | |
96bcf938 | 993 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb | 994 | u_int8_t u3 = nfmsg->nfgen_family; |
ef00f89f PM |
995 | u16 zone; |
996 | int err; | |
c1d10adb | 997 | |
80d326fa PNA |
998 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
999 | struct netlink_dump_control c = { | |
1000 | .dump = ctnetlink_dump_table, | |
1001 | .done = ctnetlink_done, | |
1002 | }; | |
0f298a28 PNA |
1003 | #ifdef CONFIG_NF_CONNTRACK_MARK |
1004 | if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) { | |
1005 | struct ctnetlink_dump_filter *filter; | |
1006 | ||
1007 | filter = kzalloc(sizeof(struct ctnetlink_dump_filter), | |
1008 | GFP_ATOMIC); | |
1009 | if (filter == NULL) | |
1010 | return -ENOMEM; | |
1011 | ||
1012 | filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK])); | |
1013 | filter->mark.mask = | |
1014 | ntohl(nla_get_be32(cda[CTA_MARK_MASK])); | |
1015 | c.data = filter; | |
1016 | } | |
1017 | #endif | |
80d326fa PNA |
1018 | return netlink_dump_start(ctnl, skb, nlh, &c); |
1019 | } | |
c1d10adb | 1020 | |
ef00f89f PM |
1021 | err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone); |
1022 | if (err < 0) | |
1023 | return err; | |
1024 | ||
df6fb868 | 1025 | if (cda[CTA_TUPLE_ORIG]) |
c1d10adb | 1026 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3); |
df6fb868 | 1027 | else if (cda[CTA_TUPLE_REPLY]) |
c1d10adb PNA |
1028 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3); |
1029 | else | |
1030 | return -EINVAL; | |
1031 | ||
1032 | if (err < 0) | |
1033 | return err; | |
1034 | ||
ef00f89f | 1035 | h = nf_conntrack_find_get(net, zone, &tuple); |
9ea8cfd6 | 1036 | if (!h) |
c1d10adb | 1037 | return -ENOENT; |
9ea8cfd6 | 1038 | |
c1d10adb PNA |
1039 | ct = nf_ct_tuplehash_to_ctrack(h); |
1040 | ||
1041 | err = -ENOMEM; | |
96bcf938 PNA |
1042 | skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
1043 | if (skb2 == NULL) { | |
c1d10adb PNA |
1044 | nf_ct_put(ct); |
1045 | return -ENOMEM; | |
1046 | } | |
c1d10adb | 1047 | |
528a3a6f | 1048 | rcu_read_lock(); |
601e68e1 | 1049 | err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq, |
80e60e67 | 1050 | NFNL_MSG_TYPE(nlh->nlmsg_type), ct); |
528a3a6f | 1051 | rcu_read_unlock(); |
c1d10adb PNA |
1052 | nf_ct_put(ct); |
1053 | if (err <= 0) | |
1054 | goto free; | |
1055 | ||
1056 | err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT); | |
1057 | if (err < 0) | |
1058 | goto out; | |
1059 | ||
c1d10adb PNA |
1060 | return 0; |
1061 | ||
1062 | free: | |
1063 | kfree_skb(skb2); | |
1064 | out: | |
f31e8d49 PNA |
1065 | /* this avoids a loop in nfnetlink. */ |
1066 | return err == -EAGAIN ? -ENOBUFS : err; | |
c1d10adb PNA |
1067 | } |
1068 | ||
67671841 | 1069 | #ifdef CONFIG_NF_NAT_NEEDED |
e6a7d3c0 PNA |
1070 | static int |
1071 | ctnetlink_parse_nat_setup(struct nf_conn *ct, | |
1072 | enum nf_nat_manip_type manip, | |
39938324 | 1073 | const struct nlattr *attr) |
e6a7d3c0 PNA |
1074 | { |
1075 | typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup; | |
1076 | ||
1077 | parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook); | |
1078 | if (!parse_nat_setup) { | |
95a5afca | 1079 | #ifdef CONFIG_MODULES |
e6a7d3c0 | 1080 | rcu_read_unlock(); |
748085fc | 1081 | spin_unlock_bh(&nf_conntrack_lock); |
e6a7d3c0 PNA |
1082 | nfnl_unlock(); |
1083 | if (request_module("nf-nat-ipv4") < 0) { | |
1084 | nfnl_lock(); | |
748085fc | 1085 | spin_lock_bh(&nf_conntrack_lock); |
e6a7d3c0 PNA |
1086 | rcu_read_lock(); |
1087 | return -EOPNOTSUPP; | |
1088 | } | |
1089 | nfnl_lock(); | |
748085fc | 1090 | spin_lock_bh(&nf_conntrack_lock); |
e6a7d3c0 PNA |
1091 | rcu_read_lock(); |
1092 | if (nfnetlink_parse_nat_setup_hook) | |
1093 | return -EAGAIN; | |
1094 | #endif | |
1095 | return -EOPNOTSUPP; | |
1096 | } | |
1097 | ||
1098 | return parse_nat_setup(ct, manip, attr); | |
1099 | } | |
67671841 | 1100 | #endif |
e6a7d3c0 | 1101 | |
bb5cf80e | 1102 | static int |
39938324 | 1103 | ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[]) |
c1d10adb PNA |
1104 | { |
1105 | unsigned long d; | |
77236b6e | 1106 | unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS])); |
c1d10adb PNA |
1107 | d = ct->status ^ status; |
1108 | ||
1109 | if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING)) | |
1110 | /* unchangeable */ | |
0adf9d67 | 1111 | return -EBUSY; |
601e68e1 | 1112 | |
c1d10adb PNA |
1113 | if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY)) |
1114 | /* SEEN_REPLY bit can only be set */ | |
0adf9d67 | 1115 | return -EBUSY; |
601e68e1 | 1116 | |
c1d10adb PNA |
1117 | if (d & IPS_ASSURED && !(status & IPS_ASSURED)) |
1118 | /* ASSURED bit can only be set */ | |
0adf9d67 | 1119 | return -EBUSY; |
c1d10adb | 1120 | |
c1d10adb PNA |
1121 | /* Be careful here, modifying NAT bits can screw up things, |
1122 | * so don't let users modify them directly if they don't pass | |
5b1158e9 | 1123 | * nf_nat_range. */ |
c1d10adb PNA |
1124 | ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK); |
1125 | return 0; | |
1126 | } | |
1127 | ||
e6a7d3c0 | 1128 | static int |
39938324 | 1129 | ctnetlink_change_nat(struct nf_conn *ct, const struct nlattr * const cda[]) |
e6a7d3c0 PNA |
1130 | { |
1131 | #ifdef CONFIG_NF_NAT_NEEDED | |
1132 | int ret; | |
1133 | ||
1134 | if (cda[CTA_NAT_DST]) { | |
1135 | ret = ctnetlink_parse_nat_setup(ct, | |
cbc9f2f4 | 1136 | NF_NAT_MANIP_DST, |
e6a7d3c0 PNA |
1137 | cda[CTA_NAT_DST]); |
1138 | if (ret < 0) | |
1139 | return ret; | |
1140 | } | |
1141 | if (cda[CTA_NAT_SRC]) { | |
1142 | ret = ctnetlink_parse_nat_setup(ct, | |
cbc9f2f4 | 1143 | NF_NAT_MANIP_SRC, |
e6a7d3c0 PNA |
1144 | cda[CTA_NAT_SRC]); |
1145 | if (ret < 0) | |
1146 | return ret; | |
1147 | } | |
1148 | return 0; | |
1149 | #else | |
1150 | return -EOPNOTSUPP; | |
1151 | #endif | |
1152 | } | |
c1d10adb PNA |
1153 | |
1154 | static inline int | |
39938324 | 1155 | ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[]) |
c1d10adb PNA |
1156 | { |
1157 | struct nf_conntrack_helper *helper; | |
dc808fe2 | 1158 | struct nf_conn_help *help = nfct_help(ct); |
29fe1b48 | 1159 | char *helpname = NULL; |
c1d10adb PNA |
1160 | int err; |
1161 | ||
c1d10adb PNA |
1162 | /* don't change helper of sibling connections */ |
1163 | if (ct->master) | |
0adf9d67 | 1164 | return -EBUSY; |
c1d10adb | 1165 | |
df6fb868 | 1166 | err = ctnetlink_parse_help(cda[CTA_HELP], &helpname); |
c1d10adb PNA |
1167 | if (err < 0) |
1168 | return err; | |
1169 | ||
df293bbb YK |
1170 | if (!strcmp(helpname, "")) { |
1171 | if (help && help->helper) { | |
c1d10adb PNA |
1172 | /* we had a helper before ... */ |
1173 | nf_ct_remove_expectations(ct); | |
a9b3cd7f | 1174 | RCU_INIT_POINTER(help->helper, NULL); |
c1d10adb | 1175 | } |
df293bbb YK |
1176 | |
1177 | return 0; | |
c1d10adb | 1178 | } |
601e68e1 | 1179 | |
794e6871 PM |
1180 | helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), |
1181 | nf_ct_protonum(ct)); | |
226c0c0e PNA |
1182 | if (helper == NULL) { |
1183 | #ifdef CONFIG_MODULES | |
1184 | spin_unlock_bh(&nf_conntrack_lock); | |
1185 | ||
1186 | if (request_module("nfct-helper-%s", helpname) < 0) { | |
1187 | spin_lock_bh(&nf_conntrack_lock); | |
1188 | return -EOPNOTSUPP; | |
1189 | } | |
1190 | ||
1191 | spin_lock_bh(&nf_conntrack_lock); | |
794e6871 PM |
1192 | helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), |
1193 | nf_ct_protonum(ct)); | |
226c0c0e PNA |
1194 | if (helper) |
1195 | return -EAGAIN; | |
1196 | #endif | |
0adf9d67 | 1197 | return -EOPNOTSUPP; |
226c0c0e | 1198 | } |
df293bbb | 1199 | |
ceceae1b YK |
1200 | if (help) { |
1201 | if (help->helper == helper) | |
1202 | return 0; | |
1203 | if (help->helper) | |
1204 | return -EBUSY; | |
1205 | /* need to zero data of old helper */ | |
1206 | memset(&help->help, 0, sizeof(help->help)); | |
1207 | } else { | |
a88e22ad PNA |
1208 | /* we cannot set a helper for an existing conntrack */ |
1209 | return -EOPNOTSUPP; | |
ceceae1b | 1210 | } |
df293bbb | 1211 | |
cf778b00 | 1212 | rcu_assign_pointer(help->helper, helper); |
c1d10adb PNA |
1213 | |
1214 | return 0; | |
1215 | } | |
1216 | ||
1217 | static inline int | |
39938324 | 1218 | ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[]) |
c1d10adb | 1219 | { |
77236b6e | 1220 | u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT])); |
601e68e1 | 1221 | |
c1d10adb PNA |
1222 | if (!del_timer(&ct->timeout)) |
1223 | return -ETIME; | |
1224 | ||
1225 | ct->timeout.expires = jiffies + timeout * HZ; | |
1226 | add_timer(&ct->timeout); | |
1227 | ||
1228 | return 0; | |
1229 | } | |
1230 | ||
d0b0268f PM |
1231 | static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = { |
1232 | [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED }, | |
1233 | [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED }, | |
1234 | [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED }, | |
1235 | }; | |
1236 | ||
c1d10adb | 1237 | static inline int |
39938324 | 1238 | ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[]) |
c1d10adb | 1239 | { |
39938324 PM |
1240 | const struct nlattr *attr = cda[CTA_PROTOINFO]; |
1241 | struct nlattr *tb[CTA_PROTOINFO_MAX+1]; | |
605dcad6 | 1242 | struct nf_conntrack_l4proto *l4proto; |
c1d10adb PNA |
1243 | int err = 0; |
1244 | ||
d0b0268f | 1245 | nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy); |
c1d10adb | 1246 | |
cd91566e FW |
1247 | rcu_read_lock(); |
1248 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); | |
fdf70832 PM |
1249 | if (l4proto->from_nlattr) |
1250 | err = l4proto->from_nlattr(tb, ct); | |
cd91566e | 1251 | rcu_read_unlock(); |
c1d10adb PNA |
1252 | |
1253 | return err; | |
1254 | } | |
1255 | ||
13eae15a | 1256 | #ifdef CONFIG_NF_NAT_NEEDED |
d0b0268f PM |
1257 | static const struct nla_policy nat_seq_policy[CTA_NAT_SEQ_MAX+1] = { |
1258 | [CTA_NAT_SEQ_CORRECTION_POS] = { .type = NLA_U32 }, | |
1259 | [CTA_NAT_SEQ_OFFSET_BEFORE] = { .type = NLA_U32 }, | |
1260 | [CTA_NAT_SEQ_OFFSET_AFTER] = { .type = NLA_U32 }, | |
1261 | }; | |
1262 | ||
13eae15a | 1263 | static inline int |
39938324 | 1264 | change_nat_seq_adj(struct nf_nat_seq *natseq, const struct nlattr * const attr) |
13eae15a PNA |
1265 | { |
1266 | struct nlattr *cda[CTA_NAT_SEQ_MAX+1]; | |
1267 | ||
d0b0268f | 1268 | nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, nat_seq_policy); |
13eae15a PNA |
1269 | |
1270 | if (!cda[CTA_NAT_SEQ_CORRECTION_POS]) | |
1271 | return -EINVAL; | |
1272 | ||
1273 | natseq->correction_pos = | |
77236b6e | 1274 | ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS])); |
13eae15a PNA |
1275 | |
1276 | if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE]) | |
1277 | return -EINVAL; | |
1278 | ||
1279 | natseq->offset_before = | |
77236b6e | 1280 | ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE])); |
13eae15a PNA |
1281 | |
1282 | if (!cda[CTA_NAT_SEQ_OFFSET_AFTER]) | |
1283 | return -EINVAL; | |
1284 | ||
1285 | natseq->offset_after = | |
77236b6e | 1286 | ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER])); |
13eae15a PNA |
1287 | |
1288 | return 0; | |
1289 | } | |
1290 | ||
1291 | static int | |
39938324 PM |
1292 | ctnetlink_change_nat_seq_adj(struct nf_conn *ct, |
1293 | const struct nlattr * const cda[]) | |
13eae15a PNA |
1294 | { |
1295 | int ret = 0; | |
1296 | struct nf_conn_nat *nat = nfct_nat(ct); | |
1297 | ||
1298 | if (!nat) | |
1299 | return 0; | |
1300 | ||
1301 | if (cda[CTA_NAT_SEQ_ADJ_ORIG]) { | |
1302 | ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL], | |
1303 | cda[CTA_NAT_SEQ_ADJ_ORIG]); | |
1304 | if (ret < 0) | |
1305 | return ret; | |
1306 | ||
1307 | ct->status |= IPS_SEQ_ADJUST; | |
1308 | } | |
1309 | ||
1310 | if (cda[CTA_NAT_SEQ_ADJ_REPLY]) { | |
1311 | ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY], | |
1312 | cda[CTA_NAT_SEQ_ADJ_REPLY]); | |
1313 | if (ret < 0) | |
1314 | return ret; | |
1315 | ||
1316 | ct->status |= IPS_SEQ_ADJUST; | |
1317 | } | |
1318 | ||
1319 | return 0; | |
1320 | } | |
1321 | #endif | |
1322 | ||
c1d10adb | 1323 | static int |
39938324 PM |
1324 | ctnetlink_change_conntrack(struct nf_conn *ct, |
1325 | const struct nlattr * const cda[]) | |
c1d10adb PNA |
1326 | { |
1327 | int err; | |
1328 | ||
e098360f PNA |
1329 | /* only allow NAT changes and master assignation for new conntracks */ |
1330 | if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER]) | |
1331 | return -EOPNOTSUPP; | |
1332 | ||
df6fb868 | 1333 | if (cda[CTA_HELP]) { |
c1d10adb PNA |
1334 | err = ctnetlink_change_helper(ct, cda); |
1335 | if (err < 0) | |
1336 | return err; | |
1337 | } | |
1338 | ||
df6fb868 | 1339 | if (cda[CTA_TIMEOUT]) { |
c1d10adb PNA |
1340 | err = ctnetlink_change_timeout(ct, cda); |
1341 | if (err < 0) | |
1342 | return err; | |
1343 | } | |
1344 | ||
df6fb868 | 1345 | if (cda[CTA_STATUS]) { |
c1d10adb PNA |
1346 | err = ctnetlink_change_status(ct, cda); |
1347 | if (err < 0) | |
1348 | return err; | |
1349 | } | |
1350 | ||
df6fb868 | 1351 | if (cda[CTA_PROTOINFO]) { |
c1d10adb PNA |
1352 | err = ctnetlink_change_protoinfo(ct, cda); |
1353 | if (err < 0) | |
1354 | return err; | |
1355 | } | |
1356 | ||
bcd1e830 | 1357 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
df6fb868 | 1358 | if (cda[CTA_MARK]) |
77236b6e | 1359 | ct->mark = ntohl(nla_get_be32(cda[CTA_MARK])); |
c1d10adb PNA |
1360 | #endif |
1361 | ||
13eae15a PNA |
1362 | #ifdef CONFIG_NF_NAT_NEEDED |
1363 | if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) { | |
1364 | err = ctnetlink_change_nat_seq_adj(ct, cda); | |
1365 | if (err < 0) | |
1366 | return err; | |
1367 | } | |
1368 | #endif | |
1369 | ||
c1d10adb PNA |
1370 | return 0; |
1371 | } | |
1372 | ||
f0a3c086 | 1373 | static struct nf_conn * |
ef00f89f | 1374 | ctnetlink_create_conntrack(struct net *net, u16 zone, |
9592a5c0 | 1375 | const struct nlattr * const cda[], |
c1d10adb | 1376 | struct nf_conntrack_tuple *otuple, |
5faa1f4c | 1377 | struct nf_conntrack_tuple *rtuple, |
7ec47496 | 1378 | u8 u3) |
c1d10adb PNA |
1379 | { |
1380 | struct nf_conn *ct; | |
1381 | int err = -EINVAL; | |
ceceae1b | 1382 | struct nf_conntrack_helper *helper; |
315c34da | 1383 | struct nf_conn_tstamp *tstamp; |
c1d10adb | 1384 | |
ef00f89f | 1385 | ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC); |
cd7fcbf1 | 1386 | if (IS_ERR(ct)) |
f0a3c086 | 1387 | return ERR_PTR(-ENOMEM); |
c1d10adb | 1388 | |
df6fb868 | 1389 | if (!cda[CTA_TIMEOUT]) |
0f5b3e85 | 1390 | goto err1; |
77236b6e | 1391 | ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT])); |
c1d10adb PNA |
1392 | |
1393 | ct->timeout.expires = jiffies + ct->timeout.expires * HZ; | |
c1d10adb | 1394 | |
1575e7ea | 1395 | rcu_read_lock(); |
226c0c0e | 1396 | if (cda[CTA_HELP]) { |
29fe1b48 | 1397 | char *helpname = NULL; |
226c0c0e PNA |
1398 | |
1399 | err = ctnetlink_parse_help(cda[CTA_HELP], &helpname); | |
0f5b3e85 PM |
1400 | if (err < 0) |
1401 | goto err2; | |
226c0c0e | 1402 | |
794e6871 PM |
1403 | helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), |
1404 | nf_ct_protonum(ct)); | |
226c0c0e PNA |
1405 | if (helper == NULL) { |
1406 | rcu_read_unlock(); | |
1407 | #ifdef CONFIG_MODULES | |
1408 | if (request_module("nfct-helper-%s", helpname) < 0) { | |
1409 | err = -EOPNOTSUPP; | |
0f5b3e85 | 1410 | goto err1; |
226c0c0e PNA |
1411 | } |
1412 | ||
1413 | rcu_read_lock(); | |
794e6871 PM |
1414 | helper = __nf_conntrack_helper_find(helpname, |
1415 | nf_ct_l3num(ct), | |
1416 | nf_ct_protonum(ct)); | |
226c0c0e | 1417 | if (helper) { |
226c0c0e | 1418 | err = -EAGAIN; |
0f5b3e85 | 1419 | goto err2; |
226c0c0e PNA |
1420 | } |
1421 | rcu_read_unlock(); | |
1422 | #endif | |
1423 | err = -EOPNOTSUPP; | |
0f5b3e85 | 1424 | goto err1; |
226c0c0e PNA |
1425 | } else { |
1426 | struct nf_conn_help *help; | |
1427 | ||
1428 | help = nf_ct_helper_ext_add(ct, GFP_ATOMIC); | |
1429 | if (help == NULL) { | |
226c0c0e | 1430 | err = -ENOMEM; |
0f5b3e85 | 1431 | goto err2; |
226c0c0e PNA |
1432 | } |
1433 | ||
1434 | /* not in hash table yet so not strictly necessary */ | |
a9b3cd7f | 1435 | RCU_INIT_POINTER(help->helper, helper); |
226c0c0e PNA |
1436 | } |
1437 | } else { | |
1438 | /* try an implicit helper assignation */ | |
b2a15a60 | 1439 | err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC); |
0f5b3e85 PM |
1440 | if (err < 0) |
1441 | goto err2; | |
1575e7ea PNA |
1442 | } |
1443 | ||
a88e22ad PNA |
1444 | if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) { |
1445 | err = ctnetlink_change_nat(ct, cda); | |
0f5b3e85 PM |
1446 | if (err < 0) |
1447 | goto err2; | |
e6a7d3c0 PNA |
1448 | } |
1449 | ||
a88e22ad | 1450 | nf_ct_acct_ext_add(ct, GFP_ATOMIC); |
a992ca2a | 1451 | nf_ct_tstamp_ext_add(ct, GFP_ATOMIC); |
a88e22ad PNA |
1452 | nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC); |
1453 | /* we must add conntrack extensions before confirmation. */ | |
1454 | ct->status |= IPS_CONFIRMED; | |
1455 | ||
1456 | if (cda[CTA_STATUS]) { | |
1457 | err = ctnetlink_change_status(ct, cda); | |
0f5b3e85 PM |
1458 | if (err < 0) |
1459 | goto err2; | |
bbb3357d | 1460 | } |
c1d10adb | 1461 | |
c969aa7d PNA |
1462 | #ifdef CONFIG_NF_NAT_NEEDED |
1463 | if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) { | |
1464 | err = ctnetlink_change_nat_seq_adj(ct, cda); | |
0f5b3e85 PM |
1465 | if (err < 0) |
1466 | goto err2; | |
c969aa7d PNA |
1467 | } |
1468 | #endif | |
1469 | ||
e5fc9e7a | 1470 | memset(&ct->proto, 0, sizeof(ct->proto)); |
df6fb868 | 1471 | if (cda[CTA_PROTOINFO]) { |
c1d10adb | 1472 | err = ctnetlink_change_protoinfo(ct, cda); |
0f5b3e85 PM |
1473 | if (err < 0) |
1474 | goto err2; | |
c1d10adb PNA |
1475 | } |
1476 | ||
bcd1e830 | 1477 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
df6fb868 | 1478 | if (cda[CTA_MARK]) |
77236b6e | 1479 | ct->mark = ntohl(nla_get_be32(cda[CTA_MARK])); |
c1d10adb PNA |
1480 | #endif |
1481 | ||
5faa1f4c | 1482 | /* setup master conntrack: this is a confirmed expectation */ |
7ec47496 PNA |
1483 | if (cda[CTA_TUPLE_MASTER]) { |
1484 | struct nf_conntrack_tuple master; | |
1485 | struct nf_conntrack_tuple_hash *master_h; | |
1486 | struct nf_conn *master_ct; | |
1487 | ||
1488 | err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3); | |
1489 | if (err < 0) | |
0f5b3e85 | 1490 | goto err2; |
7ec47496 | 1491 | |
ef00f89f | 1492 | master_h = nf_conntrack_find_get(net, zone, &master); |
7ec47496 PNA |
1493 | if (master_h == NULL) { |
1494 | err = -ENOENT; | |
0f5b3e85 | 1495 | goto err2; |
7ec47496 PNA |
1496 | } |
1497 | master_ct = nf_ct_tuplehash_to_ctrack(master_h); | |
f2a89004 | 1498 | __set_bit(IPS_EXPECTED_BIT, &ct->status); |
5faa1f4c | 1499 | ct->master = master_ct; |
f2a89004 | 1500 | } |
315c34da PNA |
1501 | tstamp = nf_conn_tstamp_find(ct); |
1502 | if (tstamp) | |
1503 | tstamp->start = ktime_to_ns(ktime_get_real()); | |
5faa1f4c | 1504 | |
7d367e06 JK |
1505 | err = nf_conntrack_hash_check_insert(ct); |
1506 | if (err < 0) | |
1507 | goto err2; | |
1508 | ||
58a3c9bb | 1509 | rcu_read_unlock(); |
dafc741c | 1510 | |
f0a3c086 | 1511 | return ct; |
c1d10adb | 1512 | |
0f5b3e85 PM |
1513 | err2: |
1514 | rcu_read_unlock(); | |
1515 | err1: | |
c1d10adb | 1516 | nf_conntrack_free(ct); |
f0a3c086 | 1517 | return ERR_PTR(err); |
c1d10adb PNA |
1518 | } |
1519 | ||
601e68e1 YH |
1520 | static int |
1521 | ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb, | |
39938324 PM |
1522 | const struct nlmsghdr *nlh, |
1523 | const struct nlattr * const cda[]) | |
c1d10adb | 1524 | { |
9592a5c0 | 1525 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
1526 | struct nf_conntrack_tuple otuple, rtuple; |
1527 | struct nf_conntrack_tuple_hash *h = NULL; | |
96bcf938 | 1528 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
7d367e06 | 1529 | struct nf_conn *ct; |
c1d10adb | 1530 | u_int8_t u3 = nfmsg->nfgen_family; |
ef00f89f PM |
1531 | u16 zone; |
1532 | int err; | |
1533 | ||
1534 | err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone); | |
1535 | if (err < 0) | |
1536 | return err; | |
c1d10adb | 1537 | |
df6fb868 | 1538 | if (cda[CTA_TUPLE_ORIG]) { |
c1d10adb PNA |
1539 | err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3); |
1540 | if (err < 0) | |
1541 | return err; | |
1542 | } | |
1543 | ||
df6fb868 | 1544 | if (cda[CTA_TUPLE_REPLY]) { |
c1d10adb PNA |
1545 | err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3); |
1546 | if (err < 0) | |
1547 | return err; | |
1548 | } | |
1549 | ||
df6fb868 | 1550 | if (cda[CTA_TUPLE_ORIG]) |
7d367e06 | 1551 | h = nf_conntrack_find_get(net, zone, &otuple); |
df6fb868 | 1552 | else if (cda[CTA_TUPLE_REPLY]) |
7d367e06 | 1553 | h = nf_conntrack_find_get(net, zone, &rtuple); |
c1d10adb PNA |
1554 | |
1555 | if (h == NULL) { | |
c1d10adb | 1556 | err = -ENOENT; |
f0a3c086 | 1557 | if (nlh->nlmsg_flags & NLM_F_CREATE) { |
fecc1133 | 1558 | enum ip_conntrack_events events; |
5faa1f4c | 1559 | |
ef00f89f | 1560 | ct = ctnetlink_create_conntrack(net, zone, cda, &otuple, |
f0a3c086 | 1561 | &rtuple, u3); |
7d367e06 JK |
1562 | if (IS_ERR(ct)) |
1563 | return PTR_ERR(ct); | |
1564 | ||
f0a3c086 | 1565 | err = 0; |
fecc1133 PNA |
1566 | if (test_bit(IPS_EXPECTED_BIT, &ct->status)) |
1567 | events = IPCT_RELATED; | |
1568 | else | |
1569 | events = IPCT_NEW; | |
1570 | ||
858b3133 PM |
1571 | nf_conntrack_eventmask_report((1 << IPCT_REPLY) | |
1572 | (1 << IPCT_ASSURED) | | |
a0891aa6 PNA |
1573 | (1 << IPCT_HELPER) | |
1574 | (1 << IPCT_PROTOINFO) | | |
1575 | (1 << IPCT_NATSEQADJ) | | |
1576 | (1 << IPCT_MARK) | events, | |
1577 | ct, NETLINK_CB(skb).pid, | |
1578 | nlmsg_report(nlh)); | |
f0a3c086 | 1579 | nf_ct_put(ct); |
7d367e06 | 1580 | } |
5faa1f4c | 1581 | |
c1d10adb PNA |
1582 | return err; |
1583 | } | |
1584 | /* implicit 'else' */ | |
1585 | ||
c1d10adb | 1586 | err = -EEXIST; |
7d367e06 | 1587 | ct = nf_ct_tuplehash_to_ctrack(h); |
ff4ca827 | 1588 | if (!(nlh->nlmsg_flags & NLM_F_EXCL)) { |
7d367e06 | 1589 | spin_lock_bh(&nf_conntrack_lock); |
19abb7b0 | 1590 | err = ctnetlink_change_conntrack(ct, cda); |
7d367e06 | 1591 | spin_unlock_bh(&nf_conntrack_lock); |
19abb7b0 | 1592 | if (err == 0) { |
858b3133 PM |
1593 | nf_conntrack_eventmask_report((1 << IPCT_REPLY) | |
1594 | (1 << IPCT_ASSURED) | | |
a0891aa6 PNA |
1595 | (1 << IPCT_HELPER) | |
1596 | (1 << IPCT_PROTOINFO) | | |
1597 | (1 << IPCT_NATSEQADJ) | | |
1598 | (1 << IPCT_MARK), | |
1599 | ct, NETLINK_CB(skb).pid, | |
1600 | nlmsg_report(nlh)); | |
7d367e06 | 1601 | } |
ff4ca827 | 1602 | } |
c1d10adb | 1603 | |
7d367e06 | 1604 | nf_ct_put(ct); |
c1d10adb PNA |
1605 | return err; |
1606 | } | |
1607 | ||
601e68e1 YH |
1608 | /*********************************************************************** |
1609 | * EXPECT | |
1610 | ***********************************************************************/ | |
c1d10adb PNA |
1611 | |
1612 | static inline int | |
1613 | ctnetlink_exp_dump_tuple(struct sk_buff *skb, | |
1614 | const struct nf_conntrack_tuple *tuple, | |
1615 | enum ctattr_expect type) | |
1616 | { | |
df6fb868 | 1617 | struct nlattr *nest_parms; |
601e68e1 | 1618 | |
df6fb868 PM |
1619 | nest_parms = nla_nest_start(skb, type | NLA_F_NESTED); |
1620 | if (!nest_parms) | |
1621 | goto nla_put_failure; | |
c1d10adb | 1622 | if (ctnetlink_dump_tuples(skb, tuple) < 0) |
df6fb868 PM |
1623 | goto nla_put_failure; |
1624 | nla_nest_end(skb, nest_parms); | |
c1d10adb PNA |
1625 | |
1626 | return 0; | |
1627 | ||
df6fb868 | 1628 | nla_put_failure: |
c1d10adb | 1629 | return -1; |
601e68e1 | 1630 | } |
c1d10adb | 1631 | |
1cde6436 PNA |
1632 | static inline int |
1633 | ctnetlink_exp_dump_mask(struct sk_buff *skb, | |
1634 | const struct nf_conntrack_tuple *tuple, | |
d4156e8c | 1635 | const struct nf_conntrack_tuple_mask *mask) |
1cde6436 PNA |
1636 | { |
1637 | int ret; | |
1638 | struct nf_conntrack_l3proto *l3proto; | |
605dcad6 | 1639 | struct nf_conntrack_l4proto *l4proto; |
d4156e8c | 1640 | struct nf_conntrack_tuple m; |
df6fb868 | 1641 | struct nlattr *nest_parms; |
d4156e8c PM |
1642 | |
1643 | memset(&m, 0xFF, sizeof(m)); | |
d4156e8c | 1644 | memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3)); |
e578756c PM |
1645 | m.src.u.all = mask->src.u.all; |
1646 | m.dst.protonum = tuple->dst.protonum; | |
d4156e8c | 1647 | |
df6fb868 PM |
1648 | nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED); |
1649 | if (!nest_parms) | |
1650 | goto nla_put_failure; | |
1cde6436 | 1651 | |
528a3a6f | 1652 | l3proto = __nf_ct_l3proto_find(tuple->src.l3num); |
d4156e8c | 1653 | ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto); |
1cde6436 PNA |
1654 | |
1655 | if (unlikely(ret < 0)) | |
df6fb868 | 1656 | goto nla_put_failure; |
1cde6436 | 1657 | |
528a3a6f | 1658 | l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum); |
d4156e8c | 1659 | ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto); |
1cde6436 | 1660 | if (unlikely(ret < 0)) |
df6fb868 | 1661 | goto nla_put_failure; |
1cde6436 | 1662 | |
df6fb868 | 1663 | nla_nest_end(skb, nest_parms); |
1cde6436 PNA |
1664 | |
1665 | return 0; | |
1666 | ||
df6fb868 | 1667 | nla_put_failure: |
1cde6436 PNA |
1668 | return -1; |
1669 | } | |
1670 | ||
bb5cf80e | 1671 | static int |
c1d10adb | 1672 | ctnetlink_exp_dump_expect(struct sk_buff *skb, |
601e68e1 | 1673 | const struct nf_conntrack_expect *exp) |
c1d10adb PNA |
1674 | { |
1675 | struct nf_conn *master = exp->master; | |
c1216382 | 1676 | long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ; |
bc01befd | 1677 | struct nf_conn_help *help; |
d978e5da PM |
1678 | |
1679 | if (timeout < 0) | |
1680 | timeout = 0; | |
c1d10adb PNA |
1681 | |
1682 | if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0) | |
df6fb868 | 1683 | goto nla_put_failure; |
1cde6436 | 1684 | if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0) |
df6fb868 | 1685 | goto nla_put_failure; |
c1d10adb PNA |
1686 | if (ctnetlink_exp_dump_tuple(skb, |
1687 | &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple, | |
1688 | CTA_EXPECT_MASTER) < 0) | |
df6fb868 | 1689 | goto nla_put_failure; |
601e68e1 | 1690 | |
d978e5da | 1691 | NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)); |
77236b6e | 1692 | NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)); |
8b008faf | 1693 | NLA_PUT_BE32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)); |
b8c5e52c | 1694 | NLA_PUT_BE32(skb, CTA_EXPECT_CLASS, htonl(exp->class)); |
bc01befd PNA |
1695 | help = nfct_help(master); |
1696 | if (help) { | |
1697 | struct nf_conntrack_helper *helper; | |
1698 | ||
1699 | helper = rcu_dereference(help->helper); | |
1700 | if (helper) | |
1701 | NLA_PUT_STRING(skb, CTA_EXPECT_HELP_NAME, helper->name); | |
1702 | } | |
c1d10adb PNA |
1703 | |
1704 | return 0; | |
601e68e1 | 1705 | |
df6fb868 | 1706 | nla_put_failure: |
c1d10adb PNA |
1707 | return -1; |
1708 | } | |
1709 | ||
1710 | static int | |
1711 | ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq, | |
8b0a231d | 1712 | int event, const struct nf_conntrack_expect *exp) |
c1d10adb PNA |
1713 | { |
1714 | struct nlmsghdr *nlh; | |
1715 | struct nfgenmsg *nfmsg; | |
96bcf938 | 1716 | unsigned int flags = pid ? NLM_F_MULTI : 0; |
c1d10adb PNA |
1717 | |
1718 | event |= NFNL_SUBSYS_CTNETLINK_EXP << 8; | |
96bcf938 PNA |
1719 | nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags); |
1720 | if (nlh == NULL) | |
1721 | goto nlmsg_failure; | |
c1d10adb | 1722 | |
96bcf938 | 1723 | nfmsg = nlmsg_data(nlh); |
c1d10adb PNA |
1724 | nfmsg->nfgen_family = exp->tuple.src.l3num; |
1725 | nfmsg->version = NFNETLINK_V0; | |
1726 | nfmsg->res_id = 0; | |
1727 | ||
1728 | if (ctnetlink_exp_dump_expect(skb, exp) < 0) | |
df6fb868 | 1729 | goto nla_put_failure; |
c1d10adb | 1730 | |
96bcf938 | 1731 | nlmsg_end(skb, nlh); |
c1d10adb PNA |
1732 | return skb->len; |
1733 | ||
1734 | nlmsg_failure: | |
df6fb868 | 1735 | nla_put_failure: |
96bcf938 | 1736 | nlmsg_cancel(skb, nlh); |
c1d10adb PNA |
1737 | return -1; |
1738 | } | |
1739 | ||
1740 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
e34d5c1a PNA |
1741 | static int |
1742 | ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item) | |
c1d10adb | 1743 | { |
9592a5c0 AD |
1744 | struct nf_conntrack_expect *exp = item->exp; |
1745 | struct net *net = nf_ct_exp_net(exp); | |
c1d10adb PNA |
1746 | struct nlmsghdr *nlh; |
1747 | struct nfgenmsg *nfmsg; | |
c1d10adb | 1748 | struct sk_buff *skb; |
ebbf41df | 1749 | unsigned int type, group; |
c1d10adb PNA |
1750 | int flags = 0; |
1751 | ||
ebbf41df PNA |
1752 | if (events & (1 << IPEXP_DESTROY)) { |
1753 | type = IPCTNL_MSG_EXP_DELETE; | |
1754 | group = NFNLGRP_CONNTRACK_EXP_DESTROY; | |
1755 | } else if (events & (1 << IPEXP_NEW)) { | |
c1d10adb PNA |
1756 | type = IPCTNL_MSG_EXP_NEW; |
1757 | flags = NLM_F_CREATE|NLM_F_EXCL; | |
ebbf41df | 1758 | group = NFNLGRP_CONNTRACK_EXP_NEW; |
c1d10adb | 1759 | } else |
e34d5c1a | 1760 | return 0; |
c1d10adb | 1761 | |
ebbf41df | 1762 | if (!item->report && !nfnetlink_has_listeners(net, group)) |
e34d5c1a | 1763 | return 0; |
b3a27bfb | 1764 | |
96bcf938 PNA |
1765 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
1766 | if (skb == NULL) | |
150ace0d | 1767 | goto errout; |
c1d10adb | 1768 | |
b633ad5f | 1769 | type |= NFNL_SUBSYS_CTNETLINK_EXP << 8; |
96bcf938 PNA |
1770 | nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags); |
1771 | if (nlh == NULL) | |
1772 | goto nlmsg_failure; | |
c1d10adb | 1773 | |
96bcf938 | 1774 | nfmsg = nlmsg_data(nlh); |
c1d10adb PNA |
1775 | nfmsg->nfgen_family = exp->tuple.src.l3num; |
1776 | nfmsg->version = NFNETLINK_V0; | |
1777 | nfmsg->res_id = 0; | |
1778 | ||
528a3a6f | 1779 | rcu_read_lock(); |
c1d10adb | 1780 | if (ctnetlink_exp_dump_expect(skb, exp) < 0) |
df6fb868 | 1781 | goto nla_put_failure; |
528a3a6f | 1782 | rcu_read_unlock(); |
c1d10adb | 1783 | |
96bcf938 | 1784 | nlmsg_end(skb, nlh); |
ebbf41df | 1785 | nfnetlink_send(skb, net, item->pid, group, item->report, GFP_ATOMIC); |
e34d5c1a | 1786 | return 0; |
c1d10adb | 1787 | |
df6fb868 | 1788 | nla_put_failure: |
528a3a6f | 1789 | rcu_read_unlock(); |
96bcf938 | 1790 | nlmsg_cancel(skb, nlh); |
528a3a6f | 1791 | nlmsg_failure: |
c1d10adb | 1792 | kfree_skb(skb); |
150ace0d | 1793 | errout: |
9592a5c0 | 1794 | nfnetlink_set_err(net, 0, 0, -ENOBUFS); |
e34d5c1a | 1795 | return 0; |
c1d10adb PNA |
1796 | } |
1797 | #endif | |
cf6994c2 PM |
1798 | static int ctnetlink_exp_done(struct netlink_callback *cb) |
1799 | { | |
31f15875 PM |
1800 | if (cb->args[1]) |
1801 | nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]); | |
cf6994c2 PM |
1802 | return 0; |
1803 | } | |
c1d10adb PNA |
1804 | |
1805 | static int | |
1806 | ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb) | |
1807 | { | |
9592a5c0 | 1808 | struct net *net = sock_net(skb->sk); |
cf6994c2 | 1809 | struct nf_conntrack_expect *exp, *last; |
96bcf938 | 1810 | struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); |
31f15875 | 1811 | struct hlist_node *n; |
87711cb8 | 1812 | u_int8_t l3proto = nfmsg->nfgen_family; |
c1d10adb | 1813 | |
7d0742da | 1814 | rcu_read_lock(); |
31f15875 PM |
1815 | last = (struct nf_conntrack_expect *)cb->args[1]; |
1816 | for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) { | |
cf6994c2 | 1817 | restart: |
9b03f38d | 1818 | hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]], |
31f15875 PM |
1819 | hnode) { |
1820 | if (l3proto && exp->tuple.src.l3num != l3proto) | |
cf6994c2 | 1821 | continue; |
31f15875 PM |
1822 | if (cb->args[1]) { |
1823 | if (exp != last) | |
1824 | continue; | |
1825 | cb->args[1] = 0; | |
1826 | } | |
8b0a231d PNA |
1827 | if (ctnetlink_exp_fill_info(skb, |
1828 | NETLINK_CB(cb->skb).pid, | |
31f15875 PM |
1829 | cb->nlh->nlmsg_seq, |
1830 | IPCTNL_MSG_EXP_NEW, | |
8b0a231d | 1831 | exp) < 0) { |
7d0742da PM |
1832 | if (!atomic_inc_not_zero(&exp->use)) |
1833 | continue; | |
31f15875 PM |
1834 | cb->args[1] = (unsigned long)exp; |
1835 | goto out; | |
1836 | } | |
cf6994c2 | 1837 | } |
31f15875 PM |
1838 | if (cb->args[1]) { |
1839 | cb->args[1] = 0; | |
1840 | goto restart; | |
cf6994c2 PM |
1841 | } |
1842 | } | |
601e68e1 | 1843 | out: |
7d0742da | 1844 | rcu_read_unlock(); |
cf6994c2 PM |
1845 | if (last) |
1846 | nf_ct_expect_put(last); | |
c1d10adb | 1847 | |
c1d10adb PNA |
1848 | return skb->len; |
1849 | } | |
1850 | ||
f73e924c | 1851 | static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = { |
d0b0268f PM |
1852 | [CTA_EXPECT_MASTER] = { .type = NLA_NESTED }, |
1853 | [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED }, | |
1854 | [CTA_EXPECT_MASK] = { .type = NLA_NESTED }, | |
f73e924c PM |
1855 | [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 }, |
1856 | [CTA_EXPECT_ID] = { .type = NLA_U32 }, | |
d0b0268f | 1857 | [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING }, |
bcac0dfa | 1858 | [CTA_EXPECT_ZONE] = { .type = NLA_U16 }, |
8b008faf | 1859 | [CTA_EXPECT_FLAGS] = { .type = NLA_U32 }, |
b8c5e52c | 1860 | [CTA_EXPECT_CLASS] = { .type = NLA_U32 }, |
c1d10adb PNA |
1861 | }; |
1862 | ||
1863 | static int | |
601e68e1 | 1864 | ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb, |
39938324 PM |
1865 | const struct nlmsghdr *nlh, |
1866 | const struct nlattr * const cda[]) | |
c1d10adb | 1867 | { |
9592a5c0 | 1868 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
1869 | struct nf_conntrack_tuple tuple; |
1870 | struct nf_conntrack_expect *exp; | |
1871 | struct sk_buff *skb2; | |
96bcf938 | 1872 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb | 1873 | u_int8_t u3 = nfmsg->nfgen_family; |
ef00f89f PM |
1874 | u16 zone; |
1875 | int err; | |
c1d10adb | 1876 | |
b8f3ab42 | 1877 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
80d326fa PNA |
1878 | struct netlink_dump_control c = { |
1879 | .dump = ctnetlink_exp_dump_table, | |
1880 | .done = ctnetlink_exp_done, | |
1881 | }; | |
1882 | return netlink_dump_start(ctnl, skb, nlh, &c); | |
c1d10adb PNA |
1883 | } |
1884 | ||
ef00f89f PM |
1885 | err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone); |
1886 | if (err < 0) | |
1887 | return err; | |
1888 | ||
35dba1d7 PNA |
1889 | if (cda[CTA_EXPECT_TUPLE]) |
1890 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); | |
1891 | else if (cda[CTA_EXPECT_MASTER]) | |
c1d10adb PNA |
1892 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3); |
1893 | else | |
1894 | return -EINVAL; | |
1895 | ||
1896 | if (err < 0) | |
1897 | return err; | |
1898 | ||
ef00f89f | 1899 | exp = nf_ct_expect_find_get(net, zone, &tuple); |
c1d10adb PNA |
1900 | if (!exp) |
1901 | return -ENOENT; | |
1902 | ||
df6fb868 | 1903 | if (cda[CTA_EXPECT_ID]) { |
77236b6e | 1904 | __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]); |
35832402 | 1905 | if (ntohl(id) != (u32)(unsigned long)exp) { |
6823645d | 1906 | nf_ct_expect_put(exp); |
c1d10adb PNA |
1907 | return -ENOENT; |
1908 | } | |
601e68e1 | 1909 | } |
c1d10adb PNA |
1910 | |
1911 | err = -ENOMEM; | |
96bcf938 | 1912 | skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
81378f72 PNA |
1913 | if (skb2 == NULL) { |
1914 | nf_ct_expect_put(exp); | |
c1d10adb | 1915 | goto out; |
81378f72 | 1916 | } |
4e9b8269 | 1917 | |
528a3a6f | 1918 | rcu_read_lock(); |
601e68e1 | 1919 | err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid, |
8b0a231d | 1920 | nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp); |
528a3a6f | 1921 | rcu_read_unlock(); |
81378f72 | 1922 | nf_ct_expect_put(exp); |
c1d10adb PNA |
1923 | if (err <= 0) |
1924 | goto free; | |
1925 | ||
81378f72 PNA |
1926 | err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT); |
1927 | if (err < 0) | |
1928 | goto out; | |
c1d10adb | 1929 | |
81378f72 | 1930 | return 0; |
c1d10adb PNA |
1931 | |
1932 | free: | |
1933 | kfree_skb(skb2); | |
1934 | out: | |
81378f72 PNA |
1935 | /* this avoids a loop in nfnetlink. */ |
1936 | return err == -EAGAIN ? -ENOBUFS : err; | |
c1d10adb PNA |
1937 | } |
1938 | ||
1939 | static int | |
601e68e1 | 1940 | ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb, |
39938324 PM |
1941 | const struct nlmsghdr *nlh, |
1942 | const struct nlattr * const cda[]) | |
c1d10adb | 1943 | { |
9592a5c0 | 1944 | struct net *net = sock_net(ctnl); |
31f15875 | 1945 | struct nf_conntrack_expect *exp; |
c1d10adb | 1946 | struct nf_conntrack_tuple tuple; |
96bcf938 | 1947 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
31f15875 | 1948 | struct hlist_node *n, *next; |
c1d10adb | 1949 | u_int8_t u3 = nfmsg->nfgen_family; |
31f15875 | 1950 | unsigned int i; |
ef00f89f | 1951 | u16 zone; |
c1d10adb PNA |
1952 | int err; |
1953 | ||
df6fb868 | 1954 | if (cda[CTA_EXPECT_TUPLE]) { |
c1d10adb | 1955 | /* delete a single expect by tuple */ |
ef00f89f PM |
1956 | err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone); |
1957 | if (err < 0) | |
1958 | return err; | |
1959 | ||
c1d10adb PNA |
1960 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); |
1961 | if (err < 0) | |
1962 | return err; | |
1963 | ||
1964 | /* bump usage count to 2 */ | |
ef00f89f | 1965 | exp = nf_ct_expect_find_get(net, zone, &tuple); |
c1d10adb PNA |
1966 | if (!exp) |
1967 | return -ENOENT; | |
1968 | ||
df6fb868 | 1969 | if (cda[CTA_EXPECT_ID]) { |
77236b6e | 1970 | __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]); |
35832402 | 1971 | if (ntohl(id) != (u32)(unsigned long)exp) { |
6823645d | 1972 | nf_ct_expect_put(exp); |
c1d10adb PNA |
1973 | return -ENOENT; |
1974 | } | |
1975 | } | |
1976 | ||
1977 | /* after list removal, usage count == 1 */ | |
ebbf41df PNA |
1978 | spin_lock_bh(&nf_conntrack_lock); |
1979 | if (del_timer(&exp->timeout)) { | |
1980 | nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).pid, | |
1981 | nlmsg_report(nlh)); | |
1982 | nf_ct_expect_put(exp); | |
1983 | } | |
1984 | spin_unlock_bh(&nf_conntrack_lock); | |
601e68e1 | 1985 | /* have to put what we 'get' above. |
c1d10adb | 1986 | * after this line usage count == 0 */ |
6823645d | 1987 | nf_ct_expect_put(exp); |
df6fb868 PM |
1988 | } else if (cda[CTA_EXPECT_HELP_NAME]) { |
1989 | char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]); | |
31f15875 | 1990 | struct nf_conn_help *m_help; |
c1d10adb PNA |
1991 | |
1992 | /* delete all expectations for this helper */ | |
f8ba1aff | 1993 | spin_lock_bh(&nf_conntrack_lock); |
31f15875 PM |
1994 | for (i = 0; i < nf_ct_expect_hsize; i++) { |
1995 | hlist_for_each_entry_safe(exp, n, next, | |
9592a5c0 | 1996 | &net->ct.expect_hash[i], |
31f15875 PM |
1997 | hnode) { |
1998 | m_help = nfct_help(exp->master); | |
794e6871 PM |
1999 | if (!strcmp(m_help->helper->name, name) && |
2000 | del_timer(&exp->timeout)) { | |
ebbf41df PNA |
2001 | nf_ct_unlink_expect_report(exp, |
2002 | NETLINK_CB(skb).pid, | |
2003 | nlmsg_report(nlh)); | |
31f15875 PM |
2004 | nf_ct_expect_put(exp); |
2005 | } | |
c1d10adb PNA |
2006 | } |
2007 | } | |
f8ba1aff | 2008 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb PNA |
2009 | } else { |
2010 | /* This basically means we have to flush everything*/ | |
f8ba1aff | 2011 | spin_lock_bh(&nf_conntrack_lock); |
31f15875 PM |
2012 | for (i = 0; i < nf_ct_expect_hsize; i++) { |
2013 | hlist_for_each_entry_safe(exp, n, next, | |
9592a5c0 | 2014 | &net->ct.expect_hash[i], |
31f15875 PM |
2015 | hnode) { |
2016 | if (del_timer(&exp->timeout)) { | |
ebbf41df PNA |
2017 | nf_ct_unlink_expect_report(exp, |
2018 | NETLINK_CB(skb).pid, | |
2019 | nlmsg_report(nlh)); | |
31f15875 PM |
2020 | nf_ct_expect_put(exp); |
2021 | } | |
c1d10adb PNA |
2022 | } |
2023 | } | |
f8ba1aff | 2024 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb PNA |
2025 | } |
2026 | ||
2027 | return 0; | |
2028 | } | |
2029 | static int | |
39938324 PM |
2030 | ctnetlink_change_expect(struct nf_conntrack_expect *x, |
2031 | const struct nlattr * const cda[]) | |
c1d10adb PNA |
2032 | { |
2033 | return -EOPNOTSUPP; | |
2034 | } | |
2035 | ||
2036 | static int | |
ef00f89f PM |
2037 | ctnetlink_create_expect(struct net *net, u16 zone, |
2038 | const struct nlattr * const cda[], | |
9592a5c0 | 2039 | u_int8_t u3, |
39938324 | 2040 | u32 pid, int report) |
c1d10adb PNA |
2041 | { |
2042 | struct nf_conntrack_tuple tuple, mask, master_tuple; | |
2043 | struct nf_conntrack_tuple_hash *h = NULL; | |
2044 | struct nf_conntrack_expect *exp; | |
2045 | struct nf_conn *ct; | |
dc808fe2 | 2046 | struct nf_conn_help *help; |
660fdb2a | 2047 | struct nf_conntrack_helper *helper = NULL; |
b8c5e52c | 2048 | u_int32_t class = 0; |
c1d10adb PNA |
2049 | int err = 0; |
2050 | ||
c1d10adb PNA |
2051 | /* caller guarantees that those three CTA_EXPECT_* exist */ |
2052 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); | |
2053 | if (err < 0) | |
2054 | return err; | |
2055 | err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3); | |
2056 | if (err < 0) | |
2057 | return err; | |
2058 | err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3); | |
2059 | if (err < 0) | |
2060 | return err; | |
2061 | ||
2062 | /* Look for master conntrack of this expectation */ | |
ef00f89f | 2063 | h = nf_conntrack_find_get(net, zone, &master_tuple); |
c1d10adb PNA |
2064 | if (!h) |
2065 | return -ENOENT; | |
2066 | ct = nf_ct_tuplehash_to_ctrack(h); | |
660fdb2a PNA |
2067 | |
2068 | /* Look for helper of this expectation */ | |
2069 | if (cda[CTA_EXPECT_HELP_NAME]) { | |
2070 | const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]); | |
2071 | ||
2072 | helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), | |
2073 | nf_ct_protonum(ct)); | |
2074 | if (helper == NULL) { | |
2075 | #ifdef CONFIG_MODULES | |
2076 | if (request_module("nfct-helper-%s", helpname) < 0) { | |
2077 | err = -EOPNOTSUPP; | |
2078 | goto out; | |
2079 | } | |
2080 | ||
2081 | helper = __nf_conntrack_helper_find(helpname, | |
2082 | nf_ct_l3num(ct), | |
2083 | nf_ct_protonum(ct)); | |
2084 | if (helper) { | |
2085 | err = -EAGAIN; | |
2086 | goto out; | |
2087 | } | |
2088 | #endif | |
2089 | err = -EOPNOTSUPP; | |
2090 | goto out; | |
2091 | } | |
2092 | } | |
2093 | ||
b8c5e52c PNA |
2094 | if (cda[CTA_EXPECT_CLASS] && helper) { |
2095 | class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS])); | |
2096 | if (class > helper->expect_class_max) { | |
2097 | err = -EINVAL; | |
2098 | goto out; | |
2099 | } | |
2100 | } | |
6823645d | 2101 | exp = nf_ct_expect_alloc(ct); |
c1d10adb PNA |
2102 | if (!exp) { |
2103 | err = -ENOMEM; | |
2104 | goto out; | |
2105 | } | |
bc01befd PNA |
2106 | help = nfct_help(ct); |
2107 | if (!help) { | |
2108 | if (!cda[CTA_EXPECT_TIMEOUT]) { | |
2109 | err = -EINVAL; | |
2110 | goto out; | |
2111 | } | |
2112 | exp->timeout.expires = | |
2113 | jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ; | |
601e68e1 | 2114 | |
bc01befd PNA |
2115 | exp->flags = NF_CT_EXPECT_USERSPACE; |
2116 | if (cda[CTA_EXPECT_FLAGS]) { | |
2117 | exp->flags |= | |
2118 | ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS])); | |
2119 | } | |
2120 | } else { | |
2121 | if (cda[CTA_EXPECT_FLAGS]) { | |
2122 | exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS])); | |
2123 | exp->flags &= ~NF_CT_EXPECT_USERSPACE; | |
2124 | } else | |
2125 | exp->flags = 0; | |
2126 | } | |
601e68e1 | 2127 | |
b8c5e52c | 2128 | exp->class = class; |
c1d10adb | 2129 | exp->expectfn = NULL; |
c1d10adb | 2130 | exp->master = ct; |
660fdb2a | 2131 | exp->helper = helper; |
c1d10adb | 2132 | memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple)); |
d4156e8c PM |
2133 | memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3)); |
2134 | exp->mask.src.u.all = mask.src.u.all; | |
c1d10adb | 2135 | |
19abb7b0 | 2136 | err = nf_ct_expect_related_report(exp, pid, report); |
6823645d | 2137 | nf_ct_expect_put(exp); |
c1d10adb | 2138 | |
601e68e1 | 2139 | out: |
c1d10adb PNA |
2140 | nf_ct_put(nf_ct_tuplehash_to_ctrack(h)); |
2141 | return err; | |
2142 | } | |
2143 | ||
2144 | static int | |
2145 | ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb, | |
39938324 PM |
2146 | const struct nlmsghdr *nlh, |
2147 | const struct nlattr * const cda[]) | |
c1d10adb | 2148 | { |
9592a5c0 | 2149 | struct net *net = sock_net(ctnl); |
c1d10adb PNA |
2150 | struct nf_conntrack_tuple tuple; |
2151 | struct nf_conntrack_expect *exp; | |
96bcf938 | 2152 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
c1d10adb | 2153 | u_int8_t u3 = nfmsg->nfgen_family; |
ef00f89f PM |
2154 | u16 zone; |
2155 | int err; | |
c1d10adb | 2156 | |
df6fb868 PM |
2157 | if (!cda[CTA_EXPECT_TUPLE] |
2158 | || !cda[CTA_EXPECT_MASK] | |
2159 | || !cda[CTA_EXPECT_MASTER]) | |
c1d10adb PNA |
2160 | return -EINVAL; |
2161 | ||
ef00f89f PM |
2162 | err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone); |
2163 | if (err < 0) | |
2164 | return err; | |
2165 | ||
c1d10adb PNA |
2166 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); |
2167 | if (err < 0) | |
2168 | return err; | |
2169 | ||
f8ba1aff | 2170 | spin_lock_bh(&nf_conntrack_lock); |
ef00f89f | 2171 | exp = __nf_ct_expect_find(net, zone, &tuple); |
c1d10adb PNA |
2172 | |
2173 | if (!exp) { | |
f8ba1aff | 2174 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb | 2175 | err = -ENOENT; |
19abb7b0 | 2176 | if (nlh->nlmsg_flags & NLM_F_CREATE) { |
ef00f89f | 2177 | err = ctnetlink_create_expect(net, zone, cda, |
19abb7b0 PNA |
2178 | u3, |
2179 | NETLINK_CB(skb).pid, | |
2180 | nlmsg_report(nlh)); | |
2181 | } | |
c1d10adb PNA |
2182 | return err; |
2183 | } | |
2184 | ||
2185 | err = -EEXIST; | |
2186 | if (!(nlh->nlmsg_flags & NLM_F_EXCL)) | |
2187 | err = ctnetlink_change_expect(exp, cda); | |
f8ba1aff | 2188 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb | 2189 | |
c1d10adb PNA |
2190 | return err; |
2191 | } | |
2192 | ||
2193 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
e34d5c1a PNA |
2194 | static struct nf_ct_event_notifier ctnl_notifier = { |
2195 | .fcn = ctnetlink_conntrack_event, | |
c1d10adb PNA |
2196 | }; |
2197 | ||
e34d5c1a PNA |
2198 | static struct nf_exp_event_notifier ctnl_notifier_exp = { |
2199 | .fcn = ctnetlink_expect_event, | |
c1d10adb PNA |
2200 | }; |
2201 | #endif | |
2202 | ||
7c8d4cb4 | 2203 | static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = { |
c1d10adb | 2204 | [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack, |
f73e924c PM |
2205 | .attr_count = CTA_MAX, |
2206 | .policy = ct_nla_policy }, | |
c1d10adb | 2207 | [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack, |
f73e924c PM |
2208 | .attr_count = CTA_MAX, |
2209 | .policy = ct_nla_policy }, | |
c1d10adb | 2210 | [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack, |
f73e924c PM |
2211 | .attr_count = CTA_MAX, |
2212 | .policy = ct_nla_policy }, | |
c1d10adb | 2213 | [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack, |
f73e924c PM |
2214 | .attr_count = CTA_MAX, |
2215 | .policy = ct_nla_policy }, | |
c1d10adb PNA |
2216 | }; |
2217 | ||
7c8d4cb4 | 2218 | static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = { |
c1d10adb | 2219 | [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect, |
f73e924c PM |
2220 | .attr_count = CTA_EXPECT_MAX, |
2221 | .policy = exp_nla_policy }, | |
c1d10adb | 2222 | [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect, |
f73e924c PM |
2223 | .attr_count = CTA_EXPECT_MAX, |
2224 | .policy = exp_nla_policy }, | |
c1d10adb | 2225 | [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect, |
f73e924c PM |
2226 | .attr_count = CTA_EXPECT_MAX, |
2227 | .policy = exp_nla_policy }, | |
c1d10adb PNA |
2228 | }; |
2229 | ||
7c8d4cb4 | 2230 | static const struct nfnetlink_subsystem ctnl_subsys = { |
c1d10adb PNA |
2231 | .name = "conntrack", |
2232 | .subsys_id = NFNL_SUBSYS_CTNETLINK, | |
2233 | .cb_count = IPCTNL_MSG_MAX, | |
2234 | .cb = ctnl_cb, | |
2235 | }; | |
2236 | ||
7c8d4cb4 | 2237 | static const struct nfnetlink_subsystem ctnl_exp_subsys = { |
c1d10adb PNA |
2238 | .name = "conntrack_expect", |
2239 | .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP, | |
2240 | .cb_count = IPCTNL_MSG_EXP_MAX, | |
2241 | .cb = ctnl_exp_cb, | |
2242 | }; | |
2243 | ||
d2483dde | 2244 | MODULE_ALIAS("ip_conntrack_netlink"); |
c1d10adb | 2245 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK); |
34f9a2e4 | 2246 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP); |
c1d10adb | 2247 | |
70e9942f PNA |
2248 | static int __net_init ctnetlink_net_init(struct net *net) |
2249 | { | |
2250 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
2251 | int ret; | |
2252 | ||
2253 | ret = nf_conntrack_register_notifier(net, &ctnl_notifier); | |
2254 | if (ret < 0) { | |
2255 | pr_err("ctnetlink_init: cannot register notifier.\n"); | |
2256 | goto err_out; | |
2257 | } | |
2258 | ||
2259 | ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp); | |
2260 | if (ret < 0) { | |
2261 | pr_err("ctnetlink_init: cannot expect register notifier.\n"); | |
2262 | goto err_unreg_notifier; | |
2263 | } | |
2264 | #endif | |
2265 | return 0; | |
2266 | ||
2267 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
2268 | err_unreg_notifier: | |
2269 | nf_conntrack_unregister_notifier(net, &ctnl_notifier); | |
2270 | err_out: | |
2271 | return ret; | |
2272 | #endif | |
2273 | } | |
2274 | ||
2275 | static void ctnetlink_net_exit(struct net *net) | |
2276 | { | |
2277 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
2278 | nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp); | |
2279 | nf_conntrack_unregister_notifier(net, &ctnl_notifier); | |
2280 | #endif | |
2281 | } | |
2282 | ||
2283 | static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list) | |
2284 | { | |
2285 | struct net *net; | |
2286 | ||
2287 | list_for_each_entry(net, net_exit_list, exit_list) | |
2288 | ctnetlink_net_exit(net); | |
2289 | } | |
2290 | ||
2291 | static struct pernet_operations ctnetlink_net_ops = { | |
2292 | .init = ctnetlink_net_init, | |
2293 | .exit_batch = ctnetlink_net_exit_batch, | |
2294 | }; | |
2295 | ||
c1d10adb PNA |
2296 | static int __init ctnetlink_init(void) |
2297 | { | |
2298 | int ret; | |
2299 | ||
654d0fbd | 2300 | pr_info("ctnetlink v%s: registering with nfnetlink.\n", version); |
c1d10adb PNA |
2301 | ret = nfnetlink_subsys_register(&ctnl_subsys); |
2302 | if (ret < 0) { | |
654d0fbd | 2303 | pr_err("ctnetlink_init: cannot register with nfnetlink.\n"); |
c1d10adb PNA |
2304 | goto err_out; |
2305 | } | |
2306 | ||
2307 | ret = nfnetlink_subsys_register(&ctnl_exp_subsys); | |
2308 | if (ret < 0) { | |
654d0fbd | 2309 | pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n"); |
c1d10adb PNA |
2310 | goto err_unreg_subsys; |
2311 | } | |
2312 | ||
70e9942f PNA |
2313 | if (register_pernet_subsys(&ctnetlink_net_ops)) { |
2314 | pr_err("ctnetlink_init: cannot register pernet operations\n"); | |
c1d10adb PNA |
2315 | goto err_unreg_exp_subsys; |
2316 | } | |
2317 | ||
c1d10adb PNA |
2318 | return 0; |
2319 | ||
c1d10adb PNA |
2320 | err_unreg_exp_subsys: |
2321 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); | |
c1d10adb PNA |
2322 | err_unreg_subsys: |
2323 | nfnetlink_subsys_unregister(&ctnl_subsys); | |
2324 | err_out: | |
2325 | return ret; | |
2326 | } | |
2327 | ||
2328 | static void __exit ctnetlink_exit(void) | |
2329 | { | |
654d0fbd | 2330 | pr_info("ctnetlink: unregistering from nfnetlink.\n"); |
c1d10adb | 2331 | |
70e9942f | 2332 | unregister_pernet_subsys(&ctnetlink_net_ops); |
c1d10adb PNA |
2333 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); |
2334 | nfnetlink_subsys_unregister(&ctnl_subsys); | |
c1d10adb PNA |
2335 | } |
2336 | ||
2337 | module_init(ctnetlink_init); | |
2338 | module_exit(ctnetlink_exit); |