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