blob: bd1d2de75e459deba86c0fcdcfd62cbbbc2364cc [file] [log] [blame]
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001/* 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>
Harald Weltedc808fe2006-03-20 17:56:32 -08005 * (C) 2002-2006 by Harald Welte <laforge@gnumonks.org>
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08006 * (C) 2003 by Patrick Mchardy <kaber@trash.net>
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08007 * (C) 2005-2006 by Pablo Neira Ayuso <pablo@eurodev.net>
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08008 *
9 * I've reworked this stuff to use attributes instead of conntrack
10 * structures. 5.44 am. I need more tea. --pablo 05/07/11.
11 *
12 * Initial connection tracking via netlink development funded and
13 * generally made possible by Network Robots, Inc. (www.networkrobots.com)
14 *
15 * Further development of this code funded by Astaro AG (http://www.astaro.com)
16 *
17 * This software may be used and distributed according to the terms
18 * of the GNU General Public License, incorporated herein by reference.
19 *
20 * Derived from ip_conntrack_netlink.c: Port by Pablo Neira Ayuso (05/11/14)
21 */
22
23#include <linux/init.h>
24#include <linux/module.h>
25#include <linux/kernel.h>
26#include <linux/types.h>
27#include <linux/timer.h>
28#include <linux/skbuff.h>
29#include <linux/errno.h>
30#include <linux/netlink.h>
31#include <linux/spinlock.h>
Yasuyuki Kozakai40a839f2006-06-27 03:00:35 -070032#include <linux/interrupt.h>
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080033#include <linux/notifier.h>
34
35#include <linux/netfilter.h>
36#include <net/netfilter/nf_conntrack.h>
37#include <net/netfilter/nf_conntrack_core.h>
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010038#include <net/netfilter/nf_conntrack_expect.h>
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080039#include <net/netfilter/nf_conntrack_helper.h>
40#include <net/netfilter/nf_conntrack_l3proto.h>
Martin Josefsson605dcad2006-11-29 02:35:06 +010041#include <net/netfilter/nf_conntrack_l4proto.h>
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080042#include <net/netfilter/nf_conntrack_tuple.h>
43#ifdef CONFIG_NF_NAT_NEEDED
44#include <net/netfilter/nf_nat_core.h>
45#include <net/netfilter/nf_nat_protocol.h>
46#endif
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080047
48#include <linux/netfilter/nfnetlink.h>
49#include <linux/netfilter/nfnetlink_conntrack.h>
50
51MODULE_LICENSE("GPL");
52
Harald Weltedc808fe2006-03-20 17:56:32 -080053static char __initdata version[] = "0.93";
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080054
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080055static inline int
56ctnetlink_dump_tuples_proto(struct sk_buff *skb,
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080057 const struct nf_conntrack_tuple *tuple,
Martin Josefsson605dcad2006-11-29 02:35:06 +010058 struct nf_conntrack_l4proto *l4proto)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080059{
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080060 int ret = 0;
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080061 struct nfattr *nest_parms = NFA_NEST(skb, CTA_TUPLE_PROTO);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080062
63 NFA_PUT(skb, CTA_PROTO_NUM, sizeof(u_int8_t), &tuple->dst.protonum);
64
Martin Josefsson605dcad2006-11-29 02:35:06 +010065 if (likely(l4proto->tuple_to_nfattr))
66 ret = l4proto->tuple_to_nfattr(skb, tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080067
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080068 NFA_NEST_END(skb, nest_parms);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080069
70 return ret;
71
72nfattr_failure:
73 return -1;
74}
75
76static inline int
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080077ctnetlink_dump_tuples_ip(struct sk_buff *skb,
78 const struct nf_conntrack_tuple *tuple,
79 struct nf_conntrack_l3proto *l3proto)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080080{
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080081 int ret = 0;
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080082 struct nfattr *nest_parms = NFA_NEST(skb, CTA_TUPLE_IP);
83
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080084 if (likely(l3proto->tuple_to_nfattr))
85 ret = l3proto->tuple_to_nfattr(skb, tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080086
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080087 NFA_NEST_END(skb, nest_parms);
88
89 return ret;
90
91nfattr_failure:
92 return -1;
93}
94
95static inline int
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080096ctnetlink_dump_tuples(struct sk_buff *skb,
97 const struct nf_conntrack_tuple *tuple)
98{
99 int ret;
100 struct nf_conntrack_l3proto *l3proto;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100101 struct nf_conntrack_l4proto *l4proto;
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -0800102
103 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
104 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
105 nf_ct_l3proto_put(l3proto);
106
107 if (unlikely(ret < 0))
108 return ret;
109
Martin Josefsson605dcad2006-11-29 02:35:06 +0100110 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
111 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
112 nf_ct_l4proto_put(l4proto);
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -0800113
114 return ret;
115}
116
117static inline int
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800118ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
119{
Patrick McHardybff9a892006-12-02 22:05:08 -0800120 __be32 status = htonl((u_int32_t) ct->status);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800121 NFA_PUT(skb, CTA_STATUS, sizeof(status), &status);
122 return 0;
123
124nfattr_failure:
125 return -1;
126}
127
128static inline int
129ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
130{
131 long timeout_l = ct->timeout.expires - jiffies;
Patrick McHardybff9a892006-12-02 22:05:08 -0800132 __be32 timeout;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800133
134 if (timeout_l < 0)
135 timeout = 0;
136 else
137 timeout = htonl(timeout_l / HZ);
138
139 NFA_PUT(skb, CTA_TIMEOUT, sizeof(timeout), &timeout);
140 return 0;
141
142nfattr_failure:
143 return -1;
144}
145
146static inline int
147ctnetlink_dump_protoinfo(struct sk_buff *skb, const struct nf_conn *ct)
148{
Martin Josefsson605dcad2006-11-29 02:35:06 +0100149 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);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800150 struct nfattr *nest_proto;
151 int ret;
152
Martin Josefsson605dcad2006-11-29 02:35:06 +0100153 if (!l4proto->to_nfattr) {
154 nf_ct_l4proto_put(l4proto);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800155 return 0;
156 }
157
158 nest_proto = NFA_NEST(skb, CTA_PROTOINFO);
159
Martin Josefsson605dcad2006-11-29 02:35:06 +0100160 ret = l4proto->to_nfattr(skb, nest_proto, ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800161
Martin Josefsson605dcad2006-11-29 02:35:06 +0100162 nf_ct_l4proto_put(l4proto);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800163
164 NFA_NEST_END(skb, nest_proto);
165
166 return ret;
167
168nfattr_failure:
Martin Josefsson605dcad2006-11-29 02:35:06 +0100169 nf_ct_l4proto_put(l4proto);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800170 return -1;
171}
172
173static inline int
174ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
175{
176 struct nfattr *nest_helper;
Harald Weltedc808fe2006-03-20 17:56:32 -0800177 const struct nf_conn_help *help = nfct_help(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800178
Harald Weltedc808fe2006-03-20 17:56:32 -0800179 if (!help || !help->helper)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800180 return 0;
181
182 nest_helper = NFA_NEST(skb, CTA_HELP);
Harald Weltedc808fe2006-03-20 17:56:32 -0800183 NFA_PUT(skb, CTA_HELP_NAME, strlen(help->helper->name), help->helper->name);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800184
Harald Weltedc808fe2006-03-20 17:56:32 -0800185 if (help->helper->to_nfattr)
186 help->helper->to_nfattr(skb, ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800187
188 NFA_NEST_END(skb, nest_helper);
189
190 return 0;
191
192nfattr_failure:
193 return -1;
194}
195
196#ifdef CONFIG_NF_CT_ACCT
197static inline int
198ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
199 enum ip_conntrack_dir dir)
200{
201 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
202 struct nfattr *nest_count = NFA_NEST(skb, type);
Patrick McHardybff9a892006-12-02 22:05:08 -0800203 __be32 tmp;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800204
205 tmp = htonl(ct->counters[dir].packets);
206 NFA_PUT(skb, CTA_COUNTERS32_PACKETS, sizeof(u_int32_t), &tmp);
207
208 tmp = htonl(ct->counters[dir].bytes);
209 NFA_PUT(skb, CTA_COUNTERS32_BYTES, sizeof(u_int32_t), &tmp);
210
211 NFA_NEST_END(skb, nest_count);
212
213 return 0;
214
215nfattr_failure:
216 return -1;
217}
218#else
219#define ctnetlink_dump_counters(a, b, c) (0)
220#endif
221
222#ifdef CONFIG_NF_CONNTRACK_MARK
223static inline int
224ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
225{
Patrick McHardybff9a892006-12-02 22:05:08 -0800226 __be32 mark = htonl(ct->mark);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800227
228 NFA_PUT(skb, CTA_MARK, sizeof(u_int32_t), &mark);
229 return 0;
230
231nfattr_failure:
232 return -1;
233}
234#else
235#define ctnetlink_dump_mark(a, b) (0)
236#endif
237
238static inline int
239ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
240{
Patrick McHardybff9a892006-12-02 22:05:08 -0800241 __be32 id = htonl(ct->id);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800242 NFA_PUT(skb, CTA_ID, sizeof(u_int32_t), &id);
243 return 0;
244
245nfattr_failure:
246 return -1;
247}
248
249static inline int
250ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
251{
Patrick McHardybff9a892006-12-02 22:05:08 -0800252 __be32 use = htonl(atomic_read(&ct->ct_general.use));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800253
254 NFA_PUT(skb, CTA_USE, sizeof(u_int32_t), &use);
255 return 0;
256
257nfattr_failure:
258 return -1;
259}
260
261#define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
262
263static int
264ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
265 int event, int nowait,
266 const struct nf_conn *ct)
267{
268 struct nlmsghdr *nlh;
269 struct nfgenmsg *nfmsg;
270 struct nfattr *nest_parms;
271 unsigned char *b;
272
273 b = skb->tail;
274
275 event |= NFNL_SUBSYS_CTNETLINK << 8;
276 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
277 nfmsg = NLMSG_DATA(nlh);
278
279 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
280 nfmsg->nfgen_family =
281 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
282 nfmsg->version = NFNETLINK_V0;
283 nfmsg->res_id = 0;
284
285 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
286 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
287 goto nfattr_failure;
288 NFA_NEST_END(skb, nest_parms);
289
290 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
291 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
292 goto nfattr_failure;
293 NFA_NEST_END(skb, nest_parms);
294
295 if (ctnetlink_dump_status(skb, ct) < 0 ||
296 ctnetlink_dump_timeout(skb, ct) < 0 ||
297 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
298 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
299 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
300 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
301 ctnetlink_dump_mark(skb, ct) < 0 ||
302 ctnetlink_dump_id(skb, ct) < 0 ||
303 ctnetlink_dump_use(skb, ct) < 0)
304 goto nfattr_failure;
305
306 nlh->nlmsg_len = skb->tail - b;
307 return skb->len;
308
309nlmsg_failure:
310nfattr_failure:
311 skb_trim(skb, b - skb->data);
312 return -1;
313}
314
315#ifdef CONFIG_NF_CONNTRACK_EVENTS
316static int ctnetlink_conntrack_event(struct notifier_block *this,
317 unsigned long events, void *ptr)
318{
319 struct nlmsghdr *nlh;
320 struct nfgenmsg *nfmsg;
321 struct nfattr *nest_parms;
322 struct nf_conn *ct = (struct nf_conn *)ptr;
323 struct sk_buff *skb;
324 unsigned int type;
325 unsigned char *b;
326 unsigned int flags = 0, group;
327
328 /* ignore our fake conntrack entry */
329 if (ct == &nf_conntrack_untracked)
330 return NOTIFY_DONE;
331
332 if (events & IPCT_DESTROY) {
333 type = IPCTNL_MSG_CT_DELETE;
334 group = NFNLGRP_CONNTRACK_DESTROY;
335 } else if (events & (IPCT_NEW | IPCT_RELATED)) {
336 type = IPCTNL_MSG_CT_NEW;
337 flags = NLM_F_CREATE|NLM_F_EXCL;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800338 group = NFNLGRP_CONNTRACK_NEW;
Pablo Neira Ayuso1a315262006-08-22 00:32:23 -0700339 } else if (events & (IPCT_STATUS | IPCT_PROTOINFO)) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800340 type = IPCTNL_MSG_CT_NEW;
341 group = NFNLGRP_CONNTRACK_UPDATE;
342 } else
343 return NOTIFY_DONE;
Patrick McHardya2427692006-03-20 18:03:59 -0800344
345 if (!nfnetlink_has_listeners(group))
346 return NOTIFY_DONE;
347
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800348 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
349 if (!skb)
350 return NOTIFY_DONE;
351
352 b = skb->tail;
353
354 type |= NFNL_SUBSYS_CTNETLINK << 8;
355 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
356 nfmsg = NLMSG_DATA(nlh);
357
358 nlh->nlmsg_flags = flags;
359 nfmsg->nfgen_family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
360 nfmsg->version = NFNETLINK_V0;
361 nfmsg->res_id = 0;
362
363 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
364 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
365 goto nfattr_failure;
366 NFA_NEST_END(skb, nest_parms);
367
368 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
369 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
370 goto nfattr_failure;
371 NFA_NEST_END(skb, nest_parms);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800372
Pablo Neira Ayuso7b621c12006-11-29 02:35:32 +0100373 if (events & IPCT_DESTROY) {
374 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
375 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
376 goto nfattr_failure;
377 } else {
378 if (ctnetlink_dump_status(skb, ct) < 0)
379 goto nfattr_failure;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800380
Pablo Neira Ayuso7b621c12006-11-29 02:35:32 +0100381 if (ctnetlink_dump_timeout(skb, ct) < 0)
382 goto nfattr_failure;
383
384 if (events & IPCT_PROTOINFO
385 && ctnetlink_dump_protoinfo(skb, ct) < 0)
386 goto nfattr_failure;
387
388 if ((events & IPCT_HELPER || nfct_help(ct))
389 && ctnetlink_dump_helpinfo(skb, ct) < 0)
390 goto nfattr_failure;
391
392 if ((events & IPCT_MARK || ct->mark)
393 && ctnetlink_dump_mark(skb, ct) < 0)
394 goto nfattr_failure;
395
396 if (events & IPCT_COUNTER_FILLING &&
397 (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
398 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0))
399 goto nfattr_failure;
400 }
Pablo Neira Ayusob9a37e02006-08-22 00:31:49 -0700401
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800402 nlh->nlmsg_len = skb->tail - b;
403 nfnetlink_send(skb, 0, group, 0);
404 return NOTIFY_DONE;
405
406nlmsg_failure:
407nfattr_failure:
408 kfree_skb(skb);
409 return NOTIFY_DONE;
410}
411#endif /* CONFIG_NF_CONNTRACK_EVENTS */
412
413static int ctnetlink_done(struct netlink_callback *cb)
414{
Patrick McHardy89f2e212006-05-29 18:24:58 -0700415 if (cb->args[1])
416 nf_ct_put((struct nf_conn *)cb->args[1]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800417 return 0;
418}
419
Pablo Neira Ayuso87711cb2006-01-05 12:19:23 -0800420#define L3PROTO(ct) ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num
421
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800422static int
423ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
424{
Patrick McHardy89f2e212006-05-29 18:24:58 -0700425 struct nf_conn *ct, *last;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800426 struct nf_conntrack_tuple_hash *h;
427 struct list_head *i;
Pablo Neira Ayuso87711cb2006-01-05 12:19:23 -0800428 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
429 u_int8_t l3proto = nfmsg->nfgen_family;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800430
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800431 read_lock_bh(&nf_conntrack_lock);
Patrick McHardyd205dc42006-08-17 18:12:38 -0700432 last = (struct nf_conn *)cb->args[1];
Patrick McHardy89f2e212006-05-29 18:24:58 -0700433 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
434restart:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800435 list_for_each_prev(i, &nf_conntrack_hash[cb->args[0]]) {
436 h = (struct nf_conntrack_tuple_hash *) i;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800437 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800438 continue;
439 ct = nf_ct_tuplehash_to_ctrack(h);
Pablo Neira Ayuso87711cb2006-01-05 12:19:23 -0800440 /* Dump entries of a given L3 protocol number.
441 * If it is not specified, ie. l3proto == 0,
442 * then dump everything. */
443 if (l3proto && L3PROTO(ct) != l3proto)
444 continue;
Patrick McHardyd205dc42006-08-17 18:12:38 -0700445 if (cb->args[1]) {
446 if (ct != last)
Patrick McHardy89f2e212006-05-29 18:24:58 -0700447 continue;
Patrick McHardyd205dc42006-08-17 18:12:38 -0700448 cb->args[1] = 0;
Patrick McHardy89f2e212006-05-29 18:24:58 -0700449 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800450 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
451 cb->nlh->nlmsg_seq,
452 IPCTNL_MSG_CT_NEW,
Patrick McHardy89f2e212006-05-29 18:24:58 -0700453 1, ct) < 0) {
454 nf_conntrack_get(&ct->ct_general);
455 cb->args[1] = (unsigned long)ct;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800456 goto out;
Patrick McHardy89f2e212006-05-29 18:24:58 -0700457 }
Pablo Neira Ayuso01f34842006-09-20 12:00:45 -0700458#ifdef CONFIG_NF_CT_ACCT
459 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) ==
460 IPCTNL_MSG_CT_GET_CTRZERO)
461 memset(&ct->counters, 0, sizeof(ct->counters));
462#endif
Patrick McHardy89f2e212006-05-29 18:24:58 -0700463 }
Patrick McHardyd205dc42006-08-17 18:12:38 -0700464 if (cb->args[1]) {
Patrick McHardy89f2e212006-05-29 18:24:58 -0700465 cb->args[1] = 0;
466 goto restart;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800467 }
468 }
Patrick McHardy89f2e212006-05-29 18:24:58 -0700469out:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800470 read_unlock_bh(&nf_conntrack_lock);
Patrick McHardyd205dc42006-08-17 18:12:38 -0700471 if (last)
472 nf_ct_put(last);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800473
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800474 return skb->len;
475}
476
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800477static inline int
478ctnetlink_parse_tuple_ip(struct nfattr *attr, struct nf_conntrack_tuple *tuple)
479{
480 struct nfattr *tb[CTA_IP_MAX];
481 struct nf_conntrack_l3proto *l3proto;
482 int ret = 0;
483
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800484 nfattr_parse_nested(tb, CTA_IP_MAX, attr);
485
486 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
487
488 if (likely(l3proto->nfattr_to_tuple))
489 ret = l3proto->nfattr_to_tuple(tb, tuple);
490
491 nf_ct_l3proto_put(l3proto);
492
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800493 return ret;
494}
495
496static const size_t cta_min_proto[CTA_PROTO_MAX] = {
497 [CTA_PROTO_NUM-1] = sizeof(u_int8_t),
498};
499
500static inline int
501ctnetlink_parse_tuple_proto(struct nfattr *attr,
502 struct nf_conntrack_tuple *tuple)
503{
504 struct nfattr *tb[CTA_PROTO_MAX];
Martin Josefsson605dcad2006-11-29 02:35:06 +0100505 struct nf_conntrack_l4proto *l4proto;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800506 int ret = 0;
507
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800508 nfattr_parse_nested(tb, CTA_PROTO_MAX, attr);
509
510 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
511 return -EINVAL;
512
513 if (!tb[CTA_PROTO_NUM-1])
514 return -EINVAL;
515 tuple->dst.protonum = *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_NUM-1]);
516
Martin Josefsson605dcad2006-11-29 02:35:06 +0100517 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800518
Martin Josefsson605dcad2006-11-29 02:35:06 +0100519 if (likely(l4proto->nfattr_to_tuple))
520 ret = l4proto->nfattr_to_tuple(tb, tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800521
Martin Josefsson605dcad2006-11-29 02:35:06 +0100522 nf_ct_l4proto_put(l4proto);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800523
524 return ret;
525}
526
527static inline int
528ctnetlink_parse_tuple(struct nfattr *cda[], struct nf_conntrack_tuple *tuple,
529 enum ctattr_tuple type, u_int8_t l3num)
530{
531 struct nfattr *tb[CTA_TUPLE_MAX];
532 int err;
533
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800534 memset(tuple, 0, sizeof(*tuple));
535
536 nfattr_parse_nested(tb, CTA_TUPLE_MAX, cda[type-1]);
537
538 if (!tb[CTA_TUPLE_IP-1])
539 return -EINVAL;
540
541 tuple->src.l3num = l3num;
542
543 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP-1], tuple);
544 if (err < 0)
545 return err;
546
547 if (!tb[CTA_TUPLE_PROTO-1])
548 return -EINVAL;
549
550 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO-1], tuple);
551 if (err < 0)
552 return err;
553
554 /* orig and expect tuples get DIR_ORIGINAL */
555 if (type == CTA_TUPLE_REPLY)
556 tuple->dst.dir = IP_CT_DIR_REPLY;
557 else
558 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
559
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800560 return 0;
561}
562
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800563#ifdef CONFIG_NF_NAT_NEEDED
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800564static const size_t cta_min_protonat[CTA_PROTONAT_MAX] = {
565 [CTA_PROTONAT_PORT_MIN-1] = sizeof(u_int16_t),
566 [CTA_PROTONAT_PORT_MAX-1] = sizeof(u_int16_t),
567};
568
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800569static int nfnetlink_parse_nat_proto(struct nfattr *attr,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800570 const struct nf_conn *ct,
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800571 struct nf_nat_range *range)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800572{
573 struct nfattr *tb[CTA_PROTONAT_MAX];
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800574 struct nf_nat_protocol *npt;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800575
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800576 nfattr_parse_nested(tb, CTA_PROTONAT_MAX, attr);
577
578 if (nfattr_bad_size(tb, CTA_PROTONAT_MAX, cta_min_protonat))
579 return -EINVAL;
580
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800581 npt = nf_nat_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800582
583 if (!npt->nfattr_to_range) {
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800584 nf_nat_proto_put(npt);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800585 return 0;
586 }
587
588 /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
589 if (npt->nfattr_to_range(tb, range) > 0)
590 range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
591
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800592 nf_nat_proto_put(npt);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800593
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800594 return 0;
595}
596
597static const size_t cta_min_nat[CTA_NAT_MAX] = {
598 [CTA_NAT_MINIP-1] = sizeof(u_int32_t),
599 [CTA_NAT_MAXIP-1] = sizeof(u_int32_t),
600};
601
602static inline int
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800603nfnetlink_parse_nat(struct nfattr *nat,
604 const struct nf_conn *ct, struct nf_nat_range *range)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800605{
606 struct nfattr *tb[CTA_NAT_MAX];
607 int err;
608
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800609 memset(range, 0, sizeof(*range));
610
Patrick McHardy3726add2006-05-29 18:24:39 -0700611 nfattr_parse_nested(tb, CTA_NAT_MAX, nat);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800612
613 if (nfattr_bad_size(tb, CTA_NAT_MAX, cta_min_nat))
614 return -EINVAL;
615
616 if (tb[CTA_NAT_MINIP-1])
Patrick McHardybff9a892006-12-02 22:05:08 -0800617 range->min_ip = *(__be32 *)NFA_DATA(tb[CTA_NAT_MINIP-1]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800618
619 if (!tb[CTA_NAT_MAXIP-1])
620 range->max_ip = range->min_ip;
621 else
Patrick McHardybff9a892006-12-02 22:05:08 -0800622 range->max_ip = *(__be32 *)NFA_DATA(tb[CTA_NAT_MAXIP-1]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800623
624 if (range->min_ip)
625 range->flags |= IP_NAT_RANGE_MAP_IPS;
626
627 if (!tb[CTA_NAT_PROTO-1])
628 return 0;
629
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800630 err = nfnetlink_parse_nat_proto(tb[CTA_NAT_PROTO-1], ct, range);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800631 if (err < 0)
632 return err;
633
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800634 return 0;
635}
636#endif
637
638static inline int
639ctnetlink_parse_help(struct nfattr *attr, char **helper_name)
640{
641 struct nfattr *tb[CTA_HELP_MAX];
642
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800643 nfattr_parse_nested(tb, CTA_HELP_MAX, attr);
644
645 if (!tb[CTA_HELP_NAME-1])
646 return -EINVAL;
647
648 *helper_name = NFA_DATA(tb[CTA_HELP_NAME-1]);
649
650 return 0;
651}
652
653static const size_t cta_min[CTA_MAX] = {
654 [CTA_STATUS-1] = sizeof(u_int32_t),
655 [CTA_TIMEOUT-1] = sizeof(u_int32_t),
656 [CTA_MARK-1] = sizeof(u_int32_t),
657 [CTA_USE-1] = sizeof(u_int32_t),
658 [CTA_ID-1] = sizeof(u_int32_t)
659};
660
661static int
662ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
663 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
664{
665 struct nf_conntrack_tuple_hash *h;
666 struct nf_conntrack_tuple tuple;
667 struct nf_conn *ct;
668 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
669 u_int8_t u3 = nfmsg->nfgen_family;
670 int err = 0;
671
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800672 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
673 return -EINVAL;
674
675 if (cda[CTA_TUPLE_ORIG-1])
676 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
677 else if (cda[CTA_TUPLE_REPLY-1])
678 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
679 else {
680 /* Flush the whole table */
681 nf_conntrack_flush();
682 return 0;
683 }
684
685 if (err < 0)
686 return err;
687
688 h = nf_conntrack_find_get(&tuple, NULL);
Pablo Neira Ayuso9ea8cfd2006-10-12 14:09:16 -0700689 if (!h)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800690 return -ENOENT;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800691
692 ct = nf_ct_tuplehash_to_ctrack(h);
693
694 if (cda[CTA_ID-1]) {
Patrick McHardybff9a892006-12-02 22:05:08 -0800695 u_int32_t id = ntohl(*(__be32 *)NFA_DATA(cda[CTA_ID-1]));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800696 if (ct->id != id) {
697 nf_ct_put(ct);
698 return -ENOENT;
699 }
700 }
701 if (del_timer(&ct->timeout))
702 ct->timeout.function((unsigned long)ct);
703
704 nf_ct_put(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800705
706 return 0;
707}
708
709static int
710ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
711 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
712{
713 struct nf_conntrack_tuple_hash *h;
714 struct nf_conntrack_tuple tuple;
715 struct nf_conn *ct;
716 struct sk_buff *skb2 = NULL;
717 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
718 u_int8_t u3 = nfmsg->nfgen_family;
719 int err = 0;
720
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800721 if (nlh->nlmsg_flags & NLM_F_DUMP) {
722 u32 rlen;
723
Pablo Neira Ayuso01f34842006-09-20 12:00:45 -0700724#ifndef CONFIG_NF_CT_ACCT
725 if (NFNL_MSG_TYPE(nlh->nlmsg_type) == IPCTNL_MSG_CT_GET_CTRZERO)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800726 return -ENOTSUPP;
727#endif
Pablo Neira Ayuso01f34842006-09-20 12:00:45 -0700728 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
729 ctnetlink_dump_table,
730 ctnetlink_done)) != 0)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800731 return -EINVAL;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800732
733 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
734 if (rlen > skb->len)
735 rlen = skb->len;
736 skb_pull(skb, rlen);
737 return 0;
738 }
739
740 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
741 return -EINVAL;
742
743 if (cda[CTA_TUPLE_ORIG-1])
744 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
745 else if (cda[CTA_TUPLE_REPLY-1])
746 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
747 else
748 return -EINVAL;
749
750 if (err < 0)
751 return err;
752
753 h = nf_conntrack_find_get(&tuple, NULL);
Pablo Neira Ayuso9ea8cfd2006-10-12 14:09:16 -0700754 if (!h)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800755 return -ENOENT;
Pablo Neira Ayuso9ea8cfd2006-10-12 14:09:16 -0700756
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800757 ct = nf_ct_tuplehash_to_ctrack(h);
758
759 err = -ENOMEM;
760 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
761 if (!skb2) {
762 nf_ct_put(ct);
763 return -ENOMEM;
764 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800765
766 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
767 IPCTNL_MSG_CT_NEW, 1, ct);
768 nf_ct_put(ct);
769 if (err <= 0)
770 goto free;
771
772 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
773 if (err < 0)
774 goto out;
775
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800776 return 0;
777
778free:
779 kfree_skb(skb2);
780out:
781 return err;
782}
783
784static inline int
785ctnetlink_change_status(struct nf_conn *ct, struct nfattr *cda[])
786{
787 unsigned long d;
Patrick McHardybff9a892006-12-02 22:05:08 -0800788 unsigned int status = ntohl(*(__be32 *)NFA_DATA(cda[CTA_STATUS-1]));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800789 d = ct->status ^ status;
790
791 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
792 /* unchangeable */
793 return -EINVAL;
794
795 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
796 /* SEEN_REPLY bit can only be set */
797 return -EINVAL;
798
799
800 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
801 /* ASSURED bit can only be set */
802 return -EINVAL;
803
Patrick McHardy3726add2006-05-29 18:24:39 -0700804 if (cda[CTA_NAT_SRC-1] || cda[CTA_NAT_DST-1]) {
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800805#ifndef CONFIG_NF_NAT_NEEDED
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800806 return -EINVAL;
807#else
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800808 struct nf_nat_range range;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800809
Patrick McHardy3726add2006-05-29 18:24:39 -0700810 if (cda[CTA_NAT_DST-1]) {
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800811 if (nfnetlink_parse_nat(cda[CTA_NAT_DST-1], ct,
Patrick McHardy3726add2006-05-29 18:24:39 -0700812 &range) < 0)
813 return -EINVAL;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800814 if (nf_nat_initialized(ct,
Patrick McHardy3726add2006-05-29 18:24:39 -0700815 HOOK2MANIP(NF_IP_PRE_ROUTING)))
816 return -EEXIST;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800817 nf_nat_setup_info(ct, &range, NF_IP_PRE_ROUTING);
Patrick McHardy3726add2006-05-29 18:24:39 -0700818 }
819 if (cda[CTA_NAT_SRC-1]) {
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800820 if (nfnetlink_parse_nat(cda[CTA_NAT_SRC-1], ct,
Patrick McHardy3726add2006-05-29 18:24:39 -0700821 &range) < 0)
822 return -EINVAL;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800823 if (nf_nat_initialized(ct,
Patrick McHardy3726add2006-05-29 18:24:39 -0700824 HOOK2MANIP(NF_IP_POST_ROUTING)))
825 return -EEXIST;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800826 nf_nat_setup_info(ct, &range, NF_IP_POST_ROUTING);
Patrick McHardy3726add2006-05-29 18:24:39 -0700827 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800828#endif
829 }
830
831 /* Be careful here, modifying NAT bits can screw up things,
832 * so don't let users modify them directly if they don't pass
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800833 * nf_nat_range. */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800834 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
835 return 0;
836}
837
838
839static inline int
840ctnetlink_change_helper(struct nf_conn *ct, struct nfattr *cda[])
841{
842 struct nf_conntrack_helper *helper;
Harald Weltedc808fe2006-03-20 17:56:32 -0800843 struct nf_conn_help *help = nfct_help(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800844 char *helpname;
845 int err;
846
Harald Weltedc808fe2006-03-20 17:56:32 -0800847 if (!help) {
848 /* FIXME: we need to reallocate and rehash */
849 return -EBUSY;
850 }
851
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800852 /* don't change helper of sibling connections */
853 if (ct->master)
854 return -EINVAL;
855
856 err = ctnetlink_parse_help(cda[CTA_HELP-1], &helpname);
857 if (err < 0)
858 return err;
859
860 helper = __nf_conntrack_helper_find_byname(helpname);
861 if (!helper) {
862 if (!strcmp(helpname, ""))
863 helper = NULL;
864 else
865 return -EINVAL;
866 }
867
Harald Weltedc808fe2006-03-20 17:56:32 -0800868 if (help->helper) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800869 if (!helper) {
870 /* we had a helper before ... */
871 nf_ct_remove_expectations(ct);
Harald Weltedc808fe2006-03-20 17:56:32 -0800872 help->helper = NULL;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800873 } else {
874 /* need to zero data of old helper */
Harald Weltedc808fe2006-03-20 17:56:32 -0800875 memset(&help->help, 0, sizeof(help->help));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800876 }
877 }
878
Harald Weltedc808fe2006-03-20 17:56:32 -0800879 help->helper = helper;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800880
881 return 0;
882}
883
884static inline int
885ctnetlink_change_timeout(struct nf_conn *ct, struct nfattr *cda[])
886{
Patrick McHardybff9a892006-12-02 22:05:08 -0800887 u_int32_t timeout = ntohl(*(__be32 *)NFA_DATA(cda[CTA_TIMEOUT-1]));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800888
889 if (!del_timer(&ct->timeout))
890 return -ETIME;
891
892 ct->timeout.expires = jiffies + timeout * HZ;
893 add_timer(&ct->timeout);
894
895 return 0;
896}
897
898static inline int
899ctnetlink_change_protoinfo(struct nf_conn *ct, struct nfattr *cda[])
900{
901 struct nfattr *tb[CTA_PROTOINFO_MAX], *attr = cda[CTA_PROTOINFO-1];
Martin Josefsson605dcad2006-11-29 02:35:06 +0100902 struct nf_conntrack_l4proto *l4proto;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800903 u_int16_t npt = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
904 u_int16_t l3num = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
905 int err = 0;
906
907 nfattr_parse_nested(tb, CTA_PROTOINFO_MAX, attr);
908
Martin Josefsson605dcad2006-11-29 02:35:06 +0100909 l4proto = nf_ct_l4proto_find_get(l3num, npt);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800910
Martin Josefsson605dcad2006-11-29 02:35:06 +0100911 if (l4proto->from_nfattr)
912 err = l4proto->from_nfattr(tb, ct);
913 nf_ct_l4proto_put(l4proto);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800914
915 return err;
916}
917
918static int
919ctnetlink_change_conntrack(struct nf_conn *ct, struct nfattr *cda[])
920{
921 int err;
922
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800923 if (cda[CTA_HELP-1]) {
924 err = ctnetlink_change_helper(ct, cda);
925 if (err < 0)
926 return err;
927 }
928
929 if (cda[CTA_TIMEOUT-1]) {
930 err = ctnetlink_change_timeout(ct, cda);
931 if (err < 0)
932 return err;
933 }
934
935 if (cda[CTA_STATUS-1]) {
936 err = ctnetlink_change_status(ct, cda);
937 if (err < 0)
938 return err;
939 }
940
941 if (cda[CTA_PROTOINFO-1]) {
942 err = ctnetlink_change_protoinfo(ct, cda);
943 if (err < 0)
944 return err;
945 }
946
Martin Josefssonbcd1e832006-04-01 02:23:21 -0800947#if defined(CONFIG_NF_CONNTRACK_MARK)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800948 if (cda[CTA_MARK-1])
Patrick McHardybff9a892006-12-02 22:05:08 -0800949 ct->mark = ntohl(*(__be32 *)NFA_DATA(cda[CTA_MARK-1]));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800950#endif
951
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800952 return 0;
953}
954
955static int
956ctnetlink_create_conntrack(struct nfattr *cda[],
957 struct nf_conntrack_tuple *otuple,
958 struct nf_conntrack_tuple *rtuple)
959{
960 struct nf_conn *ct;
961 int err = -EINVAL;
Yasuyuki Kozakaidafc7412006-11-27 10:25:32 -0800962 struct nf_conn_help *help;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800963
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800964 ct = nf_conntrack_alloc(otuple, rtuple);
965 if (ct == NULL || IS_ERR(ct))
966 return -ENOMEM;
967
968 if (!cda[CTA_TIMEOUT-1])
969 goto err;
Patrick McHardybff9a892006-12-02 22:05:08 -0800970 ct->timeout.expires = ntohl(*(__be32 *)NFA_DATA(cda[CTA_TIMEOUT-1]));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800971
972 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
973 ct->status |= IPS_CONFIRMED;
974
Pablo Neira Ayusobbb33572006-11-29 02:35:31 +0100975 if (cda[CTA_STATUS-1]) {
976 err = ctnetlink_change_status(ct, cda);
977 if (err < 0)
978 goto err;
979 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800980
981 if (cda[CTA_PROTOINFO-1]) {
982 err = ctnetlink_change_protoinfo(ct, cda);
983 if (err < 0)
984 return err;
985 }
986
Martin Josefssonbcd1e832006-04-01 02:23:21 -0800987#if defined(CONFIG_NF_CONNTRACK_MARK)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800988 if (cda[CTA_MARK-1])
Patrick McHardybff9a892006-12-02 22:05:08 -0800989 ct->mark = ntohl(*(__be32 *)NFA_DATA(cda[CTA_MARK-1]));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800990#endif
991
Yasuyuki Kozakaidafc7412006-11-27 10:25:32 -0800992 help = nfct_help(ct);
993 if (help)
994 help->helper = nf_ct_helper_find_get(rtuple);
995
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800996 add_timer(&ct->timeout);
997 nf_conntrack_hash_insert(ct);
998
Yasuyuki Kozakaidafc7412006-11-27 10:25:32 -0800999 if (help && help->helper)
1000 nf_ct_helper_put(help->helper);
1001
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001002 return 0;
1003
1004err:
1005 nf_conntrack_free(ct);
1006 return err;
1007}
1008
1009static int
1010ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1011 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1012{
1013 struct nf_conntrack_tuple otuple, rtuple;
1014 struct nf_conntrack_tuple_hash *h = NULL;
1015 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1016 u_int8_t u3 = nfmsg->nfgen_family;
1017 int err = 0;
1018
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001019 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
1020 return -EINVAL;
1021
1022 if (cda[CTA_TUPLE_ORIG-1]) {
1023 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1024 if (err < 0)
1025 return err;
1026 }
1027
1028 if (cda[CTA_TUPLE_REPLY-1]) {
1029 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1030 if (err < 0)
1031 return err;
1032 }
1033
1034 write_lock_bh(&nf_conntrack_lock);
1035 if (cda[CTA_TUPLE_ORIG-1])
1036 h = __nf_conntrack_find(&otuple, NULL);
1037 else if (cda[CTA_TUPLE_REPLY-1])
1038 h = __nf_conntrack_find(&rtuple, NULL);
1039
1040 if (h == NULL) {
1041 write_unlock_bh(&nf_conntrack_lock);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001042 err = -ENOENT;
1043 if (nlh->nlmsg_flags & NLM_F_CREATE)
1044 err = ctnetlink_create_conntrack(cda, &otuple, &rtuple);
1045 return err;
1046 }
1047 /* implicit 'else' */
1048
1049 /* we only allow nat config for new conntracks */
Patrick McHardy3726add2006-05-29 18:24:39 -07001050 if (cda[CTA_NAT_SRC-1] || cda[CTA_NAT_DST-1]) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001051 err = -EINVAL;
1052 goto out_unlock;
1053 }
1054
1055 /* We manipulate the conntrack inside the global conntrack table lock,
1056 * so there's no need to increase the refcount */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001057 err = -EEXIST;
1058 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1059 err = ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h), cda);
1060
1061out_unlock:
1062 write_unlock_bh(&nf_conntrack_lock);
1063 return err;
1064}
1065
1066/***********************************************************************
1067 * EXPECT
1068 ***********************************************************************/
1069
1070static inline int
1071ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1072 const struct nf_conntrack_tuple *tuple,
1073 enum ctattr_expect type)
1074{
1075 struct nfattr *nest_parms = NFA_NEST(skb, type);
1076
1077 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1078 goto nfattr_failure;
1079
1080 NFA_NEST_END(skb, nest_parms);
1081
1082 return 0;
1083
1084nfattr_failure:
1085 return -1;
1086}
1087
1088static inline int
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001089ctnetlink_exp_dump_mask(struct sk_buff *skb,
1090 const struct nf_conntrack_tuple *tuple,
1091 const struct nf_conntrack_tuple *mask)
1092{
1093 int ret;
1094 struct nf_conntrack_l3proto *l3proto;
Martin Josefsson605dcad2006-11-29 02:35:06 +01001095 struct nf_conntrack_l4proto *l4proto;
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001096 struct nfattr *nest_parms = NFA_NEST(skb, CTA_EXPECT_MASK);
1097
1098 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
1099 ret = ctnetlink_dump_tuples_ip(skb, mask, l3proto);
1100 nf_ct_l3proto_put(l3proto);
1101
1102 if (unlikely(ret < 0))
1103 goto nfattr_failure;
1104
Martin Josefsson605dcad2006-11-29 02:35:06 +01001105 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
1106 ret = ctnetlink_dump_tuples_proto(skb, mask, l4proto);
1107 nf_ct_l4proto_put(l4proto);
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001108 if (unlikely(ret < 0))
1109 goto nfattr_failure;
1110
1111 NFA_NEST_END(skb, nest_parms);
1112
1113 return 0;
1114
1115nfattr_failure:
1116 return -1;
1117}
1118
1119static inline int
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001120ctnetlink_exp_dump_expect(struct sk_buff *skb,
1121 const struct nf_conntrack_expect *exp)
1122{
1123 struct nf_conn *master = exp->master;
Patrick McHardybff9a892006-12-02 22:05:08 -08001124 __be32 timeout = htonl((exp->timeout.expires - jiffies) / HZ);
1125 __be32 id = htonl(exp->id);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001126
1127 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1128 goto nfattr_failure;
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001129 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001130 goto nfattr_failure;
1131 if (ctnetlink_exp_dump_tuple(skb,
1132 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1133 CTA_EXPECT_MASTER) < 0)
1134 goto nfattr_failure;
1135
1136 NFA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout);
1137 NFA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id);
1138
1139 return 0;
1140
1141nfattr_failure:
1142 return -1;
1143}
1144
1145static int
1146ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1147 int event,
1148 int nowait,
1149 const struct nf_conntrack_expect *exp)
1150{
1151 struct nlmsghdr *nlh;
1152 struct nfgenmsg *nfmsg;
1153 unsigned char *b;
1154
1155 b = skb->tail;
1156
1157 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1158 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1159 nfmsg = NLMSG_DATA(nlh);
1160
1161 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
1162 nfmsg->nfgen_family = exp->tuple.src.l3num;
1163 nfmsg->version = NFNETLINK_V0;
1164 nfmsg->res_id = 0;
1165
1166 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1167 goto nfattr_failure;
1168
1169 nlh->nlmsg_len = skb->tail - b;
1170 return skb->len;
1171
1172nlmsg_failure:
1173nfattr_failure:
1174 skb_trim(skb, b - skb->data);
1175 return -1;
1176}
1177
1178#ifdef CONFIG_NF_CONNTRACK_EVENTS
1179static int ctnetlink_expect_event(struct notifier_block *this,
1180 unsigned long events, void *ptr)
1181{
1182 struct nlmsghdr *nlh;
1183 struct nfgenmsg *nfmsg;
1184 struct nf_conntrack_expect *exp = (struct nf_conntrack_expect *)ptr;
1185 struct sk_buff *skb;
1186 unsigned int type;
1187 unsigned char *b;
1188 int flags = 0;
1189
1190 if (events & IPEXP_NEW) {
1191 type = IPCTNL_MSG_EXP_NEW;
1192 flags = NLM_F_CREATE|NLM_F_EXCL;
1193 } else
1194 return NOTIFY_DONE;
1195
Pablo Neira Ayusob3a27bf2006-08-22 00:32:05 -07001196 if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW))
1197 return NOTIFY_DONE;
1198
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001199 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1200 if (!skb)
1201 return NOTIFY_DONE;
1202
1203 b = skb->tail;
1204
Marcus Sundbergb633ad52006-02-04 02:11:09 -08001205 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001206 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1207 nfmsg = NLMSG_DATA(nlh);
1208
1209 nlh->nlmsg_flags = flags;
1210 nfmsg->nfgen_family = exp->tuple.src.l3num;
1211 nfmsg->version = NFNETLINK_V0;
1212 nfmsg->res_id = 0;
1213
1214 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1215 goto nfattr_failure;
1216
1217 nlh->nlmsg_len = skb->tail - b;
1218 nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1219 return NOTIFY_DONE;
1220
1221nlmsg_failure:
1222nfattr_failure:
1223 kfree_skb(skb);
1224 return NOTIFY_DONE;
1225}
1226#endif
1227
1228static int
1229ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1230{
1231 struct nf_conntrack_expect *exp = NULL;
1232 struct list_head *i;
1233 u_int32_t *id = (u_int32_t *) &cb->args[0];
Pablo Neira Ayuso87711cb2006-01-05 12:19:23 -08001234 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
1235 u_int8_t l3proto = nfmsg->nfgen_family;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001236
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001237 read_lock_bh(&nf_conntrack_lock);
1238 list_for_each_prev(i, &nf_conntrack_expect_list) {
1239 exp = (struct nf_conntrack_expect *) i;
Pablo Neira Ayuso87711cb2006-01-05 12:19:23 -08001240 if (l3proto && exp->tuple.src.l3num != l3proto)
1241 continue;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001242 if (exp->id <= *id)
1243 continue;
1244 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1245 cb->nlh->nlmsg_seq,
1246 IPCTNL_MSG_EXP_NEW,
1247 1, exp) < 0)
1248 goto out;
1249 *id = exp->id;
1250 }
1251out:
1252 read_unlock_bh(&nf_conntrack_lock);
1253
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001254 return skb->len;
1255}
1256
1257static const size_t cta_min_exp[CTA_EXPECT_MAX] = {
1258 [CTA_EXPECT_TIMEOUT-1] = sizeof(u_int32_t),
1259 [CTA_EXPECT_ID-1] = sizeof(u_int32_t)
1260};
1261
1262static int
1263ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1264 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1265{
1266 struct nf_conntrack_tuple tuple;
1267 struct nf_conntrack_expect *exp;
1268 struct sk_buff *skb2;
1269 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1270 u_int8_t u3 = nfmsg->nfgen_family;
1271 int err = 0;
1272
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001273 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1274 return -EINVAL;
1275
1276 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1277 u32 rlen;
1278
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001279 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
1280 ctnetlink_exp_dump_table,
1281 ctnetlink_done)) != 0)
1282 return -EINVAL;
1283 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1284 if (rlen > skb->len)
1285 rlen = skb->len;
1286 skb_pull(skb, rlen);
1287 return 0;
1288 }
1289
1290 if (cda[CTA_EXPECT_MASTER-1])
1291 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1292 else
1293 return -EINVAL;
1294
1295 if (err < 0)
1296 return err;
1297
Yasuyuki Kozakai468ec442006-11-29 02:35:23 +01001298 exp = nf_conntrack_expect_find_get(&tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001299 if (!exp)
1300 return -ENOENT;
1301
1302 if (cda[CTA_EXPECT_ID-1]) {
Patrick McHardybff9a892006-12-02 22:05:08 -08001303 __be32 id = *(__be32 *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001304 if (exp->id != ntohl(id)) {
1305 nf_conntrack_expect_put(exp);
1306 return -ENOENT;
1307 }
1308 }
1309
1310 err = -ENOMEM;
1311 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1312 if (!skb2)
1313 goto out;
Thomas Graf4e9b8262006-11-27 09:25:58 -08001314
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001315 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1316 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1317 1, exp);
1318 if (err <= 0)
1319 goto free;
1320
1321 nf_conntrack_expect_put(exp);
1322
1323 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1324
1325free:
1326 kfree_skb(skb2);
1327out:
1328 nf_conntrack_expect_put(exp);
1329 return err;
1330}
1331
1332static int
1333ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1334 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1335{
1336 struct nf_conntrack_expect *exp, *tmp;
1337 struct nf_conntrack_tuple tuple;
1338 struct nf_conntrack_helper *h;
1339 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1340 u_int8_t u3 = nfmsg->nfgen_family;
1341 int err;
1342
1343 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1344 return -EINVAL;
1345
1346 if (cda[CTA_EXPECT_TUPLE-1]) {
1347 /* delete a single expect by tuple */
1348 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1349 if (err < 0)
1350 return err;
1351
1352 /* bump usage count to 2 */
Yasuyuki Kozakai468ec442006-11-29 02:35:23 +01001353 exp = nf_conntrack_expect_find_get(&tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001354 if (!exp)
1355 return -ENOENT;
1356
1357 if (cda[CTA_EXPECT_ID-1]) {
Patrick McHardybff9a892006-12-02 22:05:08 -08001358 __be32 id = *(__be32 *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001359 if (exp->id != ntohl(id)) {
1360 nf_conntrack_expect_put(exp);
1361 return -ENOENT;
1362 }
1363 }
1364
1365 /* after list removal, usage count == 1 */
1366 nf_conntrack_unexpect_related(exp);
1367 /* have to put what we 'get' above.
1368 * after this line usage count == 0 */
1369 nf_conntrack_expect_put(exp);
1370 } else if (cda[CTA_EXPECT_HELP_NAME-1]) {
1371 char *name = NFA_DATA(cda[CTA_EXPECT_HELP_NAME-1]);
1372
1373 /* delete all expectations for this helper */
1374 write_lock_bh(&nf_conntrack_lock);
1375 h = __nf_conntrack_helper_find_byname(name);
1376 if (!h) {
1377 write_unlock_bh(&nf_conntrack_lock);
1378 return -EINVAL;
1379 }
1380 list_for_each_entry_safe(exp, tmp, &nf_conntrack_expect_list,
1381 list) {
Harald Weltedc808fe2006-03-20 17:56:32 -08001382 struct nf_conn_help *m_help = nfct_help(exp->master);
1383 if (m_help->helper == h
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001384 && del_timer(&exp->timeout)) {
1385 nf_ct_unlink_expect(exp);
1386 nf_conntrack_expect_put(exp);
1387 }
1388 }
1389 write_unlock_bh(&nf_conntrack_lock);
1390 } else {
1391 /* This basically means we have to flush everything*/
1392 write_lock_bh(&nf_conntrack_lock);
1393 list_for_each_entry_safe(exp, tmp, &nf_conntrack_expect_list,
1394 list) {
1395 if (del_timer(&exp->timeout)) {
1396 nf_ct_unlink_expect(exp);
1397 nf_conntrack_expect_put(exp);
1398 }
1399 }
1400 write_unlock_bh(&nf_conntrack_lock);
1401 }
1402
1403 return 0;
1404}
1405static int
1406ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nfattr *cda[])
1407{
1408 return -EOPNOTSUPP;
1409}
1410
1411static int
1412ctnetlink_create_expect(struct nfattr *cda[], u_int8_t u3)
1413{
1414 struct nf_conntrack_tuple tuple, mask, master_tuple;
1415 struct nf_conntrack_tuple_hash *h = NULL;
1416 struct nf_conntrack_expect *exp;
1417 struct nf_conn *ct;
Harald Weltedc808fe2006-03-20 17:56:32 -08001418 struct nf_conn_help *help;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001419 int err = 0;
1420
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001421 /* caller guarantees that those three CTA_EXPECT_* exist */
1422 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1423 if (err < 0)
1424 return err;
1425 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1426 if (err < 0)
1427 return err;
1428 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1429 if (err < 0)
1430 return err;
1431
1432 /* Look for master conntrack of this expectation */
1433 h = nf_conntrack_find_get(&master_tuple, NULL);
1434 if (!h)
1435 return -ENOENT;
1436 ct = nf_ct_tuplehash_to_ctrack(h);
Harald Weltedc808fe2006-03-20 17:56:32 -08001437 help = nfct_help(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001438
Harald Weltedc808fe2006-03-20 17:56:32 -08001439 if (!help || !help->helper) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001440 /* such conntrack hasn't got any helper, abort */
1441 err = -EINVAL;
1442 goto out;
1443 }
1444
1445 exp = nf_conntrack_expect_alloc(ct);
1446 if (!exp) {
1447 err = -ENOMEM;
1448 goto out;
1449 }
1450
1451 exp->expectfn = NULL;
1452 exp->flags = 0;
1453 exp->master = ct;
Patrick McHardy9457d852006-12-02 22:05:25 -08001454 exp->helper = NULL;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001455 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1456 memcpy(&exp->mask, &mask, sizeof(struct nf_conntrack_tuple));
1457
1458 err = nf_conntrack_expect_related(exp);
1459 nf_conntrack_expect_put(exp);
1460
1461out:
1462 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1463 return err;
1464}
1465
1466static int
1467ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1468 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1469{
1470 struct nf_conntrack_tuple tuple;
1471 struct nf_conntrack_expect *exp;
1472 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1473 u_int8_t u3 = nfmsg->nfgen_family;
1474 int err = 0;
1475
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001476 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1477 return -EINVAL;
1478
1479 if (!cda[CTA_EXPECT_TUPLE-1]
1480 || !cda[CTA_EXPECT_MASK-1]
1481 || !cda[CTA_EXPECT_MASTER-1])
1482 return -EINVAL;
1483
1484 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1485 if (err < 0)
1486 return err;
1487
1488 write_lock_bh(&nf_conntrack_lock);
1489 exp = __nf_conntrack_expect_find(&tuple);
1490
1491 if (!exp) {
1492 write_unlock_bh(&nf_conntrack_lock);
1493 err = -ENOENT;
1494 if (nlh->nlmsg_flags & NLM_F_CREATE)
1495 err = ctnetlink_create_expect(cda, u3);
1496 return err;
1497 }
1498
1499 err = -EEXIST;
1500 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1501 err = ctnetlink_change_expect(exp, cda);
1502 write_unlock_bh(&nf_conntrack_lock);
1503
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001504 return err;
1505}
1506
1507#ifdef CONFIG_NF_CONNTRACK_EVENTS
1508static struct notifier_block ctnl_notifier = {
1509 .notifier_call = ctnetlink_conntrack_event,
1510};
1511
1512static struct notifier_block ctnl_notifier_exp = {
1513 .notifier_call = ctnetlink_expect_event,
1514};
1515#endif
1516
1517static struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1518 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1519 .attr_count = CTA_MAX, },
1520 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1521 .attr_count = CTA_MAX, },
1522 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1523 .attr_count = CTA_MAX, },
1524 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1525 .attr_count = CTA_MAX, },
1526};
1527
1528static struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1529 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1530 .attr_count = CTA_EXPECT_MAX, },
1531 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1532 .attr_count = CTA_EXPECT_MAX, },
1533 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1534 .attr_count = CTA_EXPECT_MAX, },
1535};
1536
1537static struct nfnetlink_subsystem ctnl_subsys = {
1538 .name = "conntrack",
1539 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1540 .cb_count = IPCTNL_MSG_MAX,
1541 .cb = ctnl_cb,
1542};
1543
1544static struct nfnetlink_subsystem ctnl_exp_subsys = {
1545 .name = "conntrack_expect",
1546 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1547 .cb_count = IPCTNL_MSG_EXP_MAX,
1548 .cb = ctnl_exp_cb,
1549};
1550
Patrick McHardyd2483dd2006-12-02 22:06:05 -08001551MODULE_ALIAS("ip_conntrack_netlink");
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001552MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
Pablo Neira Ayuso34f9a2e2006-02-04 02:11:41 -08001553MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001554
1555static int __init ctnetlink_init(void)
1556{
1557 int ret;
1558
1559 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1560 ret = nfnetlink_subsys_register(&ctnl_subsys);
1561 if (ret < 0) {
1562 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1563 goto err_out;
1564 }
1565
1566 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1567 if (ret < 0) {
1568 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1569 goto err_unreg_subsys;
1570 }
1571
1572#ifdef CONFIG_NF_CONNTRACK_EVENTS
1573 ret = nf_conntrack_register_notifier(&ctnl_notifier);
1574 if (ret < 0) {
1575 printk("ctnetlink_init: cannot register notifier.\n");
1576 goto err_unreg_exp_subsys;
1577 }
1578
1579 ret = nf_conntrack_expect_register_notifier(&ctnl_notifier_exp);
1580 if (ret < 0) {
1581 printk("ctnetlink_init: cannot expect register notifier.\n");
1582 goto err_unreg_notifier;
1583 }
1584#endif
1585
1586 return 0;
1587
1588#ifdef CONFIG_NF_CONNTRACK_EVENTS
1589err_unreg_notifier:
1590 nf_conntrack_unregister_notifier(&ctnl_notifier);
1591err_unreg_exp_subsys:
1592 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1593#endif
1594err_unreg_subsys:
1595 nfnetlink_subsys_unregister(&ctnl_subsys);
1596err_out:
1597 return ret;
1598}
1599
1600static void __exit ctnetlink_exit(void)
1601{
1602 printk("ctnetlink: unregistering from nfnetlink.\n");
1603
1604#ifdef CONFIG_NF_CONNTRACK_EVENTS
Martin Josefssone64a70b2006-04-01 02:24:48 -08001605 nf_conntrack_expect_unregister_notifier(&ctnl_notifier_exp);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001606 nf_conntrack_unregister_notifier(&ctnl_notifier);
1607#endif
1608
1609 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1610 nfnetlink_subsys_unregister(&ctnl_subsys);
1611 return;
1612}
1613
1614module_init(ctnetlink_init);
1615module_exit(ctnetlink_exit);