blob: f9c8ddd5973ce20afd542274a7d07c722bd8af27 [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>
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080042#include <linux/netfilter_ipv4/ip_nat_protocol.h>
43
44#include <linux/netfilter/nfnetlink.h>
45#include <linux/netfilter/nfnetlink_conntrack.h>
46
47MODULE_LICENSE("GPL");
48
Harald Weltedc808fe2006-03-20 17:56:32 -080049static char __initdata version[] = "0.93";
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080050
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080051static inline int
52ctnetlink_dump_tuples_proto(struct sk_buff *skb,
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080053 const struct nf_conntrack_tuple *tuple,
Martin Josefsson605dcad2006-11-29 02:35:06 +010054 struct nf_conntrack_l4proto *l4proto)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080055{
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080056 int ret = 0;
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080057 struct nfattr *nest_parms = NFA_NEST(skb, CTA_TUPLE_PROTO);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080058
59 NFA_PUT(skb, CTA_PROTO_NUM, sizeof(u_int8_t), &tuple->dst.protonum);
60
Martin Josefsson605dcad2006-11-29 02:35:06 +010061 if (likely(l4proto->tuple_to_nfattr))
62 ret = l4proto->tuple_to_nfattr(skb, tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080063
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080064 NFA_NEST_END(skb, nest_parms);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080065
66 return ret;
67
68nfattr_failure:
69 return -1;
70}
71
72static inline int
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080073ctnetlink_dump_tuples_ip(struct sk_buff *skb,
74 const struct nf_conntrack_tuple *tuple,
75 struct nf_conntrack_l3proto *l3proto)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080076{
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080077 int ret = 0;
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080078 struct nfattr *nest_parms = NFA_NEST(skb, CTA_TUPLE_IP);
79
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080080 if (likely(l3proto->tuple_to_nfattr))
81 ret = l3proto->tuple_to_nfattr(skb, tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080082
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080083 NFA_NEST_END(skb, nest_parms);
84
85 return ret;
86
87nfattr_failure:
88 return -1;
89}
90
91static inline int
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080092ctnetlink_dump_tuples(struct sk_buff *skb,
93 const struct nf_conntrack_tuple *tuple)
94{
95 int ret;
96 struct nf_conntrack_l3proto *l3proto;
Martin Josefsson605dcad2006-11-29 02:35:06 +010097 struct nf_conntrack_l4proto *l4proto;
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080098
99 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
100 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
101 nf_ct_l3proto_put(l3proto);
102
103 if (unlikely(ret < 0))
104 return ret;
105
Martin Josefsson605dcad2006-11-29 02:35:06 +0100106 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);
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -0800109
110 return ret;
111}
112
113static inline int
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800114ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
115{
Patrick McHardybff9a892006-12-02 22:05:08 -0800116 __be32 status = htonl((u_int32_t) ct->status);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800117 NFA_PUT(skb, CTA_STATUS, sizeof(status), &status);
118 return 0;
119
120nfattr_failure:
121 return -1;
122}
123
124static inline int
125ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
126{
127 long timeout_l = ct->timeout.expires - jiffies;
Patrick McHardybff9a892006-12-02 22:05:08 -0800128 __be32 timeout;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800129
130 if (timeout_l < 0)
131 timeout = 0;
132 else
133 timeout = htonl(timeout_l / HZ);
134
135 NFA_PUT(skb, CTA_TIMEOUT, sizeof(timeout), &timeout);
136 return 0;
137
138nfattr_failure:
139 return -1;
140}
141
142static inline int
143ctnetlink_dump_protoinfo(struct sk_buff *skb, const struct nf_conn *ct)
144{
Martin Josefsson605dcad2006-11-29 02:35:06 +0100145 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 -0800146 struct nfattr *nest_proto;
147 int ret;
148
Martin Josefsson605dcad2006-11-29 02:35:06 +0100149 if (!l4proto->to_nfattr) {
150 nf_ct_l4proto_put(l4proto);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800151 return 0;
152 }
153
154 nest_proto = NFA_NEST(skb, CTA_PROTOINFO);
155
Martin Josefsson605dcad2006-11-29 02:35:06 +0100156 ret = l4proto->to_nfattr(skb, nest_proto, ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800157
Martin Josefsson605dcad2006-11-29 02:35:06 +0100158 nf_ct_l4proto_put(l4proto);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800159
160 NFA_NEST_END(skb, nest_proto);
161
162 return ret;
163
164nfattr_failure:
Martin Josefsson605dcad2006-11-29 02:35:06 +0100165 nf_ct_l4proto_put(l4proto);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800166 return -1;
167}
168
169static inline int
170ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
171{
172 struct nfattr *nest_helper;
Harald Weltedc808fe2006-03-20 17:56:32 -0800173 const struct nf_conn_help *help = nfct_help(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800174
Harald Weltedc808fe2006-03-20 17:56:32 -0800175 if (!help || !help->helper)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800176 return 0;
177
178 nest_helper = NFA_NEST(skb, CTA_HELP);
Harald Weltedc808fe2006-03-20 17:56:32 -0800179 NFA_PUT(skb, CTA_HELP_NAME, strlen(help->helper->name), help->helper->name);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800180
Harald Weltedc808fe2006-03-20 17:56:32 -0800181 if (help->helper->to_nfattr)
182 help->helper->to_nfattr(skb, ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800183
184 NFA_NEST_END(skb, nest_helper);
185
186 return 0;
187
188nfattr_failure:
189 return -1;
190}
191
192#ifdef CONFIG_NF_CT_ACCT
193static inline int
194ctnetlink_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);
Patrick McHardybff9a892006-12-02 22:05:08 -0800199 __be32 tmp;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800200
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
211nfattr_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
219static inline int
220ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
221{
Patrick McHardybff9a892006-12-02 22:05:08 -0800222 __be32 mark = htonl(ct->mark);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800223
224 NFA_PUT(skb, CTA_MARK, sizeof(u_int32_t), &mark);
225 return 0;
226
227nfattr_failure:
228 return -1;
229}
230#else
231#define ctnetlink_dump_mark(a, b) (0)
232#endif
233
234static inline int
235ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
236{
Patrick McHardybff9a892006-12-02 22:05:08 -0800237 __be32 id = htonl(ct->id);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800238 NFA_PUT(skb, CTA_ID, sizeof(u_int32_t), &id);
239 return 0;
240
241nfattr_failure:
242 return -1;
243}
244
245static inline int
246ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
247{
Patrick McHardybff9a892006-12-02 22:05:08 -0800248 __be32 use = htonl(atomic_read(&ct->ct_general.use));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800249
250 NFA_PUT(skb, CTA_USE, sizeof(u_int32_t), &use);
251 return 0;
252
253nfattr_failure:
254 return -1;
255}
256
257#define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
258
259static int
260ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
261 int event, int nowait,
262 const struct nf_conn *ct)
263{
264 struct nlmsghdr *nlh;
265 struct nfgenmsg *nfmsg;
266 struct nfattr *nest_parms;
267 unsigned char *b;
268
269 b = skb->tail;
270
271 event |= NFNL_SUBSYS_CTNETLINK << 8;
272 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
273 nfmsg = NLMSG_DATA(nlh);
274
275 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
276 nfmsg->nfgen_family =
277 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
278 nfmsg->version = NFNETLINK_V0;
279 nfmsg->res_id = 0;
280
281 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
282 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
283 goto nfattr_failure;
284 NFA_NEST_END(skb, nest_parms);
285
286 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
287 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
288 goto nfattr_failure;
289 NFA_NEST_END(skb, nest_parms);
290
291 if (ctnetlink_dump_status(skb, ct) < 0 ||
292 ctnetlink_dump_timeout(skb, ct) < 0 ||
293 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
294 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
295 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
296 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
297 ctnetlink_dump_mark(skb, ct) < 0 ||
298 ctnetlink_dump_id(skb, ct) < 0 ||
299 ctnetlink_dump_use(skb, ct) < 0)
300 goto nfattr_failure;
301
302 nlh->nlmsg_len = skb->tail - b;
303 return skb->len;
304
305nlmsg_failure:
306nfattr_failure:
307 skb_trim(skb, b - skb->data);
308 return -1;
309}
310
311#ifdef CONFIG_NF_CONNTRACK_EVENTS
312static int ctnetlink_conntrack_event(struct notifier_block *this,
313 unsigned long events, void *ptr)
314{
315 struct nlmsghdr *nlh;
316 struct nfgenmsg *nfmsg;
317 struct nfattr *nest_parms;
318 struct nf_conn *ct = (struct nf_conn *)ptr;
319 struct sk_buff *skb;
320 unsigned int type;
321 unsigned char *b;
322 unsigned int flags = 0, group;
323
324 /* ignore our fake conntrack entry */
325 if (ct == &nf_conntrack_untracked)
326 return NOTIFY_DONE;
327
328 if (events & IPCT_DESTROY) {
329 type = IPCTNL_MSG_CT_DELETE;
330 group = NFNLGRP_CONNTRACK_DESTROY;
331 } else if (events & (IPCT_NEW | IPCT_RELATED)) {
332 type = IPCTNL_MSG_CT_NEW;
333 flags = NLM_F_CREATE|NLM_F_EXCL;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800334 group = NFNLGRP_CONNTRACK_NEW;
Pablo Neira Ayuso1a315262006-08-22 00:32:23 -0700335 } else if (events & (IPCT_STATUS | IPCT_PROTOINFO)) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800336 type = IPCTNL_MSG_CT_NEW;
337 group = NFNLGRP_CONNTRACK_UPDATE;
338 } else
339 return NOTIFY_DONE;
Patrick McHardya2427692006-03-20 18:03:59 -0800340
341 if (!nfnetlink_has_listeners(group))
342 return NOTIFY_DONE;
343
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800344 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
345 if (!skb)
346 return NOTIFY_DONE;
347
348 b = skb->tail;
349
350 type |= NFNL_SUBSYS_CTNETLINK << 8;
351 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
352 nfmsg = NLMSG_DATA(nlh);
353
354 nlh->nlmsg_flags = flags;
355 nfmsg->nfgen_family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
356 nfmsg->version = NFNETLINK_V0;
357 nfmsg->res_id = 0;
358
359 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
360 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
361 goto nfattr_failure;
362 NFA_NEST_END(skb, nest_parms);
363
364 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
365 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
366 goto nfattr_failure;
367 NFA_NEST_END(skb, nest_parms);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800368
Pablo Neira Ayuso7b621c12006-11-29 02:35:32 +0100369 if (events & IPCT_DESTROY) {
370 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
371 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
372 goto nfattr_failure;
373 } else {
374 if (ctnetlink_dump_status(skb, ct) < 0)
375 goto nfattr_failure;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800376
Pablo Neira Ayuso7b621c12006-11-29 02:35:32 +0100377 if (ctnetlink_dump_timeout(skb, ct) < 0)
378 goto nfattr_failure;
379
380 if (events & IPCT_PROTOINFO
381 && ctnetlink_dump_protoinfo(skb, ct) < 0)
382 goto nfattr_failure;
383
384 if ((events & IPCT_HELPER || nfct_help(ct))
385 && ctnetlink_dump_helpinfo(skb, ct) < 0)
386 goto nfattr_failure;
387
388 if ((events & IPCT_MARK || ct->mark)
389 && ctnetlink_dump_mark(skb, ct) < 0)
390 goto nfattr_failure;
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 }
Pablo Neira Ayusob9a37e02006-08-22 00:31:49 -0700397
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800398 nlh->nlmsg_len = skb->tail - b;
399 nfnetlink_send(skb, 0, group, 0);
400 return NOTIFY_DONE;
401
402nlmsg_failure:
403nfattr_failure:
404 kfree_skb(skb);
405 return NOTIFY_DONE;
406}
407#endif /* CONFIG_NF_CONNTRACK_EVENTS */
408
409static int ctnetlink_done(struct netlink_callback *cb)
410{
Patrick McHardy89f2e212006-05-29 18:24:58 -0700411 if (cb->args[1])
412 nf_ct_put((struct nf_conn *)cb->args[1]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800413 return 0;
414}
415
Pablo Neira Ayuso87711cb2006-01-05 12:19:23 -0800416#define L3PROTO(ct) ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num
417
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800418static int
419ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
420{
Patrick McHardy89f2e212006-05-29 18:24:58 -0700421 struct nf_conn *ct, *last;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800422 struct nf_conntrack_tuple_hash *h;
423 struct list_head *i;
Pablo Neira Ayuso87711cb2006-01-05 12:19:23 -0800424 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
425 u_int8_t l3proto = nfmsg->nfgen_family;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800426
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800427 read_lock_bh(&nf_conntrack_lock);
Patrick McHardyd205dc42006-08-17 18:12:38 -0700428 last = (struct nf_conn *)cb->args[1];
Patrick McHardy89f2e212006-05-29 18:24:58 -0700429 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
430restart:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800431 list_for_each_prev(i, &nf_conntrack_hash[cb->args[0]]) {
432 h = (struct nf_conntrack_tuple_hash *) i;
433 if (DIRECTION(h) != IP_CT_DIR_ORIGINAL)
434 continue;
435 ct = nf_ct_tuplehash_to_ctrack(h);
Pablo Neira Ayuso87711cb2006-01-05 12:19:23 -0800436 /* 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;
Patrick McHardyd205dc42006-08-17 18:12:38 -0700441 if (cb->args[1]) {
442 if (ct != last)
Patrick McHardy89f2e212006-05-29 18:24:58 -0700443 continue;
Patrick McHardyd205dc42006-08-17 18:12:38 -0700444 cb->args[1] = 0;
Patrick McHardy89f2e212006-05-29 18:24:58 -0700445 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800446 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
447 cb->nlh->nlmsg_seq,
448 IPCTNL_MSG_CT_NEW,
Patrick McHardy89f2e212006-05-29 18:24:58 -0700449 1, ct) < 0) {
450 nf_conntrack_get(&ct->ct_general);
451 cb->args[1] = (unsigned long)ct;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800452 goto out;
Patrick McHardy89f2e212006-05-29 18:24:58 -0700453 }
Pablo Neira Ayuso01f34842006-09-20 12:00:45 -0700454#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
Patrick McHardy89f2e212006-05-29 18:24:58 -0700459 }
Patrick McHardyd205dc42006-08-17 18:12:38 -0700460 if (cb->args[1]) {
Patrick McHardy89f2e212006-05-29 18:24:58 -0700461 cb->args[1] = 0;
462 goto restart;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800463 }
464 }
Patrick McHardy89f2e212006-05-29 18:24:58 -0700465out:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800466 read_unlock_bh(&nf_conntrack_lock);
Patrick McHardyd205dc42006-08-17 18:12:38 -0700467 if (last)
468 nf_ct_put(last);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800469
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800470 return skb->len;
471}
472
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800473static inline int
474ctnetlink_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
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800480 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
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800489 return ret;
490}
491
492static const size_t cta_min_proto[CTA_PROTO_MAX] = {
493 [CTA_PROTO_NUM-1] = sizeof(u_int8_t),
494};
495
496static inline int
497ctnetlink_parse_tuple_proto(struct nfattr *attr,
498 struct nf_conntrack_tuple *tuple)
499{
500 struct nfattr *tb[CTA_PROTO_MAX];
Martin Josefsson605dcad2006-11-29 02:35:06 +0100501 struct nf_conntrack_l4proto *l4proto;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800502 int ret = 0;
503
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800504 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
Martin Josefsson605dcad2006-11-29 02:35:06 +0100513 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800514
Martin Josefsson605dcad2006-11-29 02:35:06 +0100515 if (likely(l4proto->nfattr_to_tuple))
516 ret = l4proto->nfattr_to_tuple(tb, tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800517
Martin Josefsson605dcad2006-11-29 02:35:06 +0100518 nf_ct_l4proto_put(l4proto);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800519
520 return ret;
521}
522
523static inline int
524ctnetlink_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
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800530 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
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800556 return 0;
557}
558
559#ifdef CONFIG_IP_NF_NAT_NEEDED
560static 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
565static int ctnetlink_parse_nat_proto(struct nfattr *attr,
566 const struct nf_conn *ct,
567 struct ip_nat_range *range)
568{
569 struct nfattr *tb[CTA_PROTONAT_MAX];
570 struct ip_nat_protocol *npt;
571
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800572 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
577 npt = ip_nat_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
578
579 if (!npt->nfattr_to_range) {
580 ip_nat_proto_put(npt);
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
588 ip_nat_proto_put(npt);
589
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800590 return 0;
591}
592
593static 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
598static inline int
Patrick McHardy3726add2006-05-29 18:24:39 -0700599ctnetlink_parse_nat(struct nfattr *nat,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800600 const struct nf_conn *ct, struct ip_nat_range *range)
601{
602 struct nfattr *tb[CTA_NAT_MAX];
603 int err;
604
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800605 memset(range, 0, sizeof(*range));
606
Patrick McHardy3726add2006-05-29 18:24:39 -0700607 nfattr_parse_nested(tb, CTA_NAT_MAX, nat);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800608
609 if (nfattr_bad_size(tb, CTA_NAT_MAX, cta_min_nat))
610 return -EINVAL;
611
612 if (tb[CTA_NAT_MINIP-1])
Patrick McHardybff9a892006-12-02 22:05:08 -0800613 range->min_ip = *(__be32 *)NFA_DATA(tb[CTA_NAT_MINIP-1]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800614
615 if (!tb[CTA_NAT_MAXIP-1])
616 range->max_ip = range->min_ip;
617 else
Patrick McHardybff9a892006-12-02 22:05:08 -0800618 range->max_ip = *(__be32 *)NFA_DATA(tb[CTA_NAT_MAXIP-1]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800619
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
626 err = ctnetlink_parse_nat_proto(tb[CTA_NAT_PROTO-1], ct, range);
627 if (err < 0)
628 return err;
629
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800630 return 0;
631}
632#endif
633
634static inline int
635ctnetlink_parse_help(struct nfattr *attr, char **helper_name)
636{
637 struct nfattr *tb[CTA_HELP_MAX];
638
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800639 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
649static 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
657static int
658ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
659 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
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
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800668 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);
Pablo Neira Ayuso9ea8cfd2006-10-12 14:09:16 -0700685 if (!h)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800686 return -ENOENT;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800687
688 ct = nf_ct_tuplehash_to_ctrack(h);
689
690 if (cda[CTA_ID-1]) {
Patrick McHardybff9a892006-12-02 22:05:08 -0800691 u_int32_t id = ntohl(*(__be32 *)NFA_DATA(cda[CTA_ID-1]));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800692 if (ct->id != id) {
693 nf_ct_put(ct);
694 return -ENOENT;
695 }
696 }
697 if (del_timer(&ct->timeout))
698 ct->timeout.function((unsigned long)ct);
699
700 nf_ct_put(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800701
702 return 0;
703}
704
705static int
706ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
707 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
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
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800717 if (nlh->nlmsg_flags & NLM_F_DUMP) {
718 u32 rlen;
719
Pablo Neira Ayuso01f34842006-09-20 12:00:45 -0700720#ifndef CONFIG_NF_CT_ACCT
721 if (NFNL_MSG_TYPE(nlh->nlmsg_type) == IPCTNL_MSG_CT_GET_CTRZERO)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800722 return -ENOTSUPP;
723#endif
Pablo Neira Ayuso01f34842006-09-20 12:00:45 -0700724 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
725 ctnetlink_dump_table,
726 ctnetlink_done)) != 0)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800727 return -EINVAL;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800728
729 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
730 if (rlen > skb->len)
731 rlen = skb->len;
732 skb_pull(skb, rlen);
733 return 0;
734 }
735
736 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
737 return -EINVAL;
738
739 if (cda[CTA_TUPLE_ORIG-1])
740 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
741 else if (cda[CTA_TUPLE_REPLY-1])
742 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
743 else
744 return -EINVAL;
745
746 if (err < 0)
747 return err;
748
749 h = nf_conntrack_find_get(&tuple, NULL);
Pablo Neira Ayuso9ea8cfd2006-10-12 14:09:16 -0700750 if (!h)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800751 return -ENOENT;
Pablo Neira Ayuso9ea8cfd2006-10-12 14:09:16 -0700752
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800753 ct = nf_ct_tuplehash_to_ctrack(h);
754
755 err = -ENOMEM;
756 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
757 if (!skb2) {
758 nf_ct_put(ct);
759 return -ENOMEM;
760 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800761
762 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
763 IPCTNL_MSG_CT_NEW, 1, ct);
764 nf_ct_put(ct);
765 if (err <= 0)
766 goto free;
767
768 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
769 if (err < 0)
770 goto out;
771
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800772 return 0;
773
774free:
775 kfree_skb(skb2);
776out:
777 return err;
778}
779
780static inline int
781ctnetlink_change_status(struct nf_conn *ct, struct nfattr *cda[])
782{
783 unsigned long d;
Patrick McHardybff9a892006-12-02 22:05:08 -0800784 unsigned int status = ntohl(*(__be32 *)NFA_DATA(cda[CTA_STATUS-1]));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800785 d = ct->status ^ status;
786
787 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
788 /* unchangeable */
789 return -EINVAL;
790
791 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
792 /* SEEN_REPLY bit can only be set */
793 return -EINVAL;
794
795
796 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
797 /* ASSURED bit can only be set */
798 return -EINVAL;
799
Patrick McHardy3726add2006-05-29 18:24:39 -0700800 if (cda[CTA_NAT_SRC-1] || cda[CTA_NAT_DST-1]) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800801#ifndef CONFIG_IP_NF_NAT_NEEDED
802 return -EINVAL;
803#else
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800804 struct ip_nat_range range;
805
Patrick McHardy3726add2006-05-29 18:24:39 -0700806 if (cda[CTA_NAT_DST-1]) {
807 if (ctnetlink_parse_nat(cda[CTA_NAT_DST-1], ct,
808 &range) < 0)
809 return -EINVAL;
810 if (ip_nat_initialized(ct,
811 HOOK2MANIP(NF_IP_PRE_ROUTING)))
812 return -EEXIST;
813 ip_nat_setup_info(ct, &range, hooknum);
814 }
815 if (cda[CTA_NAT_SRC-1]) {
816 if (ctnetlink_parse_nat(cda[CTA_NAT_SRC-1], ct,
817 &range) < 0)
818 return -EINVAL;
819 if (ip_nat_initialized(ct,
820 HOOK2MANIP(NF_IP_POST_ROUTING)))
821 return -EEXIST;
822 ip_nat_setup_info(ct, &range, hooknum);
823 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800824#endif
825 }
826
827 /* Be careful here, modifying NAT bits can screw up things,
828 * so don't let users modify them directly if they don't pass
829 * ip_nat_range. */
830 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
831 return 0;
832}
833
834
835static inline int
836ctnetlink_change_helper(struct nf_conn *ct, struct nfattr *cda[])
837{
838 struct nf_conntrack_helper *helper;
Harald Weltedc808fe2006-03-20 17:56:32 -0800839 struct nf_conn_help *help = nfct_help(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800840 char *helpname;
841 int err;
842
Harald Weltedc808fe2006-03-20 17:56:32 -0800843 if (!help) {
844 /* FIXME: we need to reallocate and rehash */
845 return -EBUSY;
846 }
847
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800848 /* don't change helper of sibling connections */
849 if (ct->master)
850 return -EINVAL;
851
852 err = ctnetlink_parse_help(cda[CTA_HELP-1], &helpname);
853 if (err < 0)
854 return err;
855
856 helper = __nf_conntrack_helper_find_byname(helpname);
857 if (!helper) {
858 if (!strcmp(helpname, ""))
859 helper = NULL;
860 else
861 return -EINVAL;
862 }
863
Harald Weltedc808fe2006-03-20 17:56:32 -0800864 if (help->helper) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800865 if (!helper) {
866 /* we had a helper before ... */
867 nf_ct_remove_expectations(ct);
Harald Weltedc808fe2006-03-20 17:56:32 -0800868 help->helper = NULL;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800869 } else {
870 /* need to zero data of old helper */
Harald Weltedc808fe2006-03-20 17:56:32 -0800871 memset(&help->help, 0, sizeof(help->help));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800872 }
873 }
874
Harald Weltedc808fe2006-03-20 17:56:32 -0800875 help->helper = helper;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800876
877 return 0;
878}
879
880static inline int
881ctnetlink_change_timeout(struct nf_conn *ct, struct nfattr *cda[])
882{
Patrick McHardybff9a892006-12-02 22:05:08 -0800883 u_int32_t timeout = ntohl(*(__be32 *)NFA_DATA(cda[CTA_TIMEOUT-1]));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800884
885 if (!del_timer(&ct->timeout))
886 return -ETIME;
887
888 ct->timeout.expires = jiffies + timeout * HZ;
889 add_timer(&ct->timeout);
890
891 return 0;
892}
893
894static inline int
895ctnetlink_change_protoinfo(struct nf_conn *ct, struct nfattr *cda[])
896{
897 struct nfattr *tb[CTA_PROTOINFO_MAX], *attr = cda[CTA_PROTOINFO-1];
Martin Josefsson605dcad2006-11-29 02:35:06 +0100898 struct nf_conntrack_l4proto *l4proto;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800899 u_int16_t npt = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
900 u_int16_t l3num = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
901 int err = 0;
902
903 nfattr_parse_nested(tb, CTA_PROTOINFO_MAX, attr);
904
Martin Josefsson605dcad2006-11-29 02:35:06 +0100905 l4proto = nf_ct_l4proto_find_get(l3num, npt);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800906
Martin Josefsson605dcad2006-11-29 02:35:06 +0100907 if (l4proto->from_nfattr)
908 err = l4proto->from_nfattr(tb, ct);
909 nf_ct_l4proto_put(l4proto);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800910
911 return err;
912}
913
914static int
915ctnetlink_change_conntrack(struct nf_conn *ct, struct nfattr *cda[])
916{
917 int err;
918
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800919 if (cda[CTA_HELP-1]) {
920 err = ctnetlink_change_helper(ct, cda);
921 if (err < 0)
922 return err;
923 }
924
925 if (cda[CTA_TIMEOUT-1]) {
926 err = ctnetlink_change_timeout(ct, cda);
927 if (err < 0)
928 return err;
929 }
930
931 if (cda[CTA_STATUS-1]) {
932 err = ctnetlink_change_status(ct, cda);
933 if (err < 0)
934 return err;
935 }
936
937 if (cda[CTA_PROTOINFO-1]) {
938 err = ctnetlink_change_protoinfo(ct, cda);
939 if (err < 0)
940 return err;
941 }
942
Martin Josefssonbcd1e832006-04-01 02:23:21 -0800943#if defined(CONFIG_NF_CONNTRACK_MARK)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800944 if (cda[CTA_MARK-1])
Patrick McHardybff9a892006-12-02 22:05:08 -0800945 ct->mark = ntohl(*(__be32 *)NFA_DATA(cda[CTA_MARK-1]));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800946#endif
947
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800948 return 0;
949}
950
951static int
952ctnetlink_create_conntrack(struct nfattr *cda[],
953 struct nf_conntrack_tuple *otuple,
954 struct nf_conntrack_tuple *rtuple)
955{
956 struct nf_conn *ct;
957 int err = -EINVAL;
Yasuyuki Kozakaidafc7412006-11-27 10:25:32 -0800958 struct nf_conn_help *help;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800959
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800960 ct = nf_conntrack_alloc(otuple, rtuple);
961 if (ct == NULL || IS_ERR(ct))
962 return -ENOMEM;
963
964 if (!cda[CTA_TIMEOUT-1])
965 goto err;
Patrick McHardybff9a892006-12-02 22:05:08 -0800966 ct->timeout.expires = ntohl(*(__be32 *)NFA_DATA(cda[CTA_TIMEOUT-1]));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800967
968 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
969 ct->status |= IPS_CONFIRMED;
970
Pablo Neira Ayusobbb33572006-11-29 02:35:31 +0100971 if (cda[CTA_STATUS-1]) {
972 err = ctnetlink_change_status(ct, cda);
973 if (err < 0)
974 goto err;
975 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800976
977 if (cda[CTA_PROTOINFO-1]) {
978 err = ctnetlink_change_protoinfo(ct, cda);
979 if (err < 0)
980 return err;
981 }
982
Martin Josefssonbcd1e832006-04-01 02:23:21 -0800983#if defined(CONFIG_NF_CONNTRACK_MARK)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800984 if (cda[CTA_MARK-1])
Patrick McHardybff9a892006-12-02 22:05:08 -0800985 ct->mark = ntohl(*(__be32 *)NFA_DATA(cda[CTA_MARK-1]));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800986#endif
987
Yasuyuki Kozakaidafc7412006-11-27 10:25:32 -0800988 help = nfct_help(ct);
989 if (help)
990 help->helper = nf_ct_helper_find_get(rtuple);
991
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800992 add_timer(&ct->timeout);
993 nf_conntrack_hash_insert(ct);
994
Yasuyuki Kozakaidafc7412006-11-27 10:25:32 -0800995 if (help && help->helper)
996 nf_ct_helper_put(help->helper);
997
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800998 return 0;
999
1000err:
1001 nf_conntrack_free(ct);
1002 return err;
1003}
1004
1005static int
1006ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1007 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1008{
1009 struct nf_conntrack_tuple otuple, rtuple;
1010 struct nf_conntrack_tuple_hash *h = NULL;
1011 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1012 u_int8_t u3 = nfmsg->nfgen_family;
1013 int err = 0;
1014
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001015 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
1016 return -EINVAL;
1017
1018 if (cda[CTA_TUPLE_ORIG-1]) {
1019 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1020 if (err < 0)
1021 return err;
1022 }
1023
1024 if (cda[CTA_TUPLE_REPLY-1]) {
1025 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1026 if (err < 0)
1027 return err;
1028 }
1029
1030 write_lock_bh(&nf_conntrack_lock);
1031 if (cda[CTA_TUPLE_ORIG-1])
1032 h = __nf_conntrack_find(&otuple, NULL);
1033 else if (cda[CTA_TUPLE_REPLY-1])
1034 h = __nf_conntrack_find(&rtuple, NULL);
1035
1036 if (h == NULL) {
1037 write_unlock_bh(&nf_conntrack_lock);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001038 err = -ENOENT;
1039 if (nlh->nlmsg_flags & NLM_F_CREATE)
1040 err = ctnetlink_create_conntrack(cda, &otuple, &rtuple);
1041 return err;
1042 }
1043 /* implicit 'else' */
1044
1045 /* we only allow nat config for new conntracks */
Patrick McHardy3726add2006-05-29 18:24:39 -07001046 if (cda[CTA_NAT_SRC-1] || cda[CTA_NAT_DST-1]) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001047 err = -EINVAL;
1048 goto out_unlock;
1049 }
1050
1051 /* We manipulate the conntrack inside the global conntrack table lock,
1052 * so there's no need to increase the refcount */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001053 err = -EEXIST;
1054 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1055 err = ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h), cda);
1056
1057out_unlock:
1058 write_unlock_bh(&nf_conntrack_lock);
1059 return err;
1060}
1061
1062/***********************************************************************
1063 * EXPECT
1064 ***********************************************************************/
1065
1066static inline int
1067ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1068 const struct nf_conntrack_tuple *tuple,
1069 enum ctattr_expect type)
1070{
1071 struct nfattr *nest_parms = NFA_NEST(skb, type);
1072
1073 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1074 goto nfattr_failure;
1075
1076 NFA_NEST_END(skb, nest_parms);
1077
1078 return 0;
1079
1080nfattr_failure:
1081 return -1;
1082}
1083
1084static inline int
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001085ctnetlink_exp_dump_mask(struct sk_buff *skb,
1086 const struct nf_conntrack_tuple *tuple,
1087 const struct nf_conntrack_tuple *mask)
1088{
1089 int ret;
1090 struct nf_conntrack_l3proto *l3proto;
Martin Josefsson605dcad2006-11-29 02:35:06 +01001091 struct nf_conntrack_l4proto *l4proto;
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001092 struct nfattr *nest_parms = NFA_NEST(skb, CTA_EXPECT_MASK);
1093
1094 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
1095 ret = ctnetlink_dump_tuples_ip(skb, mask, l3proto);
1096 nf_ct_l3proto_put(l3proto);
1097
1098 if (unlikely(ret < 0))
1099 goto nfattr_failure;
1100
Martin Josefsson605dcad2006-11-29 02:35:06 +01001101 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
1102 ret = ctnetlink_dump_tuples_proto(skb, mask, l4proto);
1103 nf_ct_l4proto_put(l4proto);
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001104 if (unlikely(ret < 0))
1105 goto nfattr_failure;
1106
1107 NFA_NEST_END(skb, nest_parms);
1108
1109 return 0;
1110
1111nfattr_failure:
1112 return -1;
1113}
1114
1115static inline int
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001116ctnetlink_exp_dump_expect(struct sk_buff *skb,
1117 const struct nf_conntrack_expect *exp)
1118{
1119 struct nf_conn *master = exp->master;
Patrick McHardybff9a892006-12-02 22:05:08 -08001120 __be32 timeout = htonl((exp->timeout.expires - jiffies) / HZ);
1121 __be32 id = htonl(exp->id);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001122
1123 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1124 goto nfattr_failure;
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001125 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001126 goto nfattr_failure;
1127 if (ctnetlink_exp_dump_tuple(skb,
1128 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1129 CTA_EXPECT_MASTER) < 0)
1130 goto nfattr_failure;
1131
1132 NFA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout);
1133 NFA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id);
1134
1135 return 0;
1136
1137nfattr_failure:
1138 return -1;
1139}
1140
1141static int
1142ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1143 int event,
1144 int nowait,
1145 const struct nf_conntrack_expect *exp)
1146{
1147 struct nlmsghdr *nlh;
1148 struct nfgenmsg *nfmsg;
1149 unsigned char *b;
1150
1151 b = skb->tail;
1152
1153 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1154 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1155 nfmsg = NLMSG_DATA(nlh);
1156
1157 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
1158 nfmsg->nfgen_family = exp->tuple.src.l3num;
1159 nfmsg->version = NFNETLINK_V0;
1160 nfmsg->res_id = 0;
1161
1162 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1163 goto nfattr_failure;
1164
1165 nlh->nlmsg_len = skb->tail - b;
1166 return skb->len;
1167
1168nlmsg_failure:
1169nfattr_failure:
1170 skb_trim(skb, b - skb->data);
1171 return -1;
1172}
1173
1174#ifdef CONFIG_NF_CONNTRACK_EVENTS
1175static int ctnetlink_expect_event(struct notifier_block *this,
1176 unsigned long events, void *ptr)
1177{
1178 struct nlmsghdr *nlh;
1179 struct nfgenmsg *nfmsg;
1180 struct nf_conntrack_expect *exp = (struct nf_conntrack_expect *)ptr;
1181 struct sk_buff *skb;
1182 unsigned int type;
1183 unsigned char *b;
1184 int flags = 0;
1185
1186 if (events & IPEXP_NEW) {
1187 type = IPCTNL_MSG_EXP_NEW;
1188 flags = NLM_F_CREATE|NLM_F_EXCL;
1189 } else
1190 return NOTIFY_DONE;
1191
Pablo Neira Ayusob3a27bf2006-08-22 00:32:05 -07001192 if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW))
1193 return NOTIFY_DONE;
1194
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001195 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1196 if (!skb)
1197 return NOTIFY_DONE;
1198
1199 b = skb->tail;
1200
Marcus Sundbergb633ad52006-02-04 02:11:09 -08001201 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001202 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1203 nfmsg = NLMSG_DATA(nlh);
1204
1205 nlh->nlmsg_flags = flags;
1206 nfmsg->nfgen_family = exp->tuple.src.l3num;
1207 nfmsg->version = NFNETLINK_V0;
1208 nfmsg->res_id = 0;
1209
1210 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1211 goto nfattr_failure;
1212
1213 nlh->nlmsg_len = skb->tail - b;
1214 nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1215 return NOTIFY_DONE;
1216
1217nlmsg_failure:
1218nfattr_failure:
1219 kfree_skb(skb);
1220 return NOTIFY_DONE;
1221}
1222#endif
1223
1224static int
1225ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1226{
1227 struct nf_conntrack_expect *exp = NULL;
1228 struct list_head *i;
1229 u_int32_t *id = (u_int32_t *) &cb->args[0];
Pablo Neira Ayuso87711cb2006-01-05 12:19:23 -08001230 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
1231 u_int8_t l3proto = nfmsg->nfgen_family;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001232
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001233 read_lock_bh(&nf_conntrack_lock);
1234 list_for_each_prev(i, &nf_conntrack_expect_list) {
1235 exp = (struct nf_conntrack_expect *) i;
Pablo Neira Ayuso87711cb2006-01-05 12:19:23 -08001236 if (l3proto && exp->tuple.src.l3num != l3proto)
1237 continue;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001238 if (exp->id <= *id)
1239 continue;
1240 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1241 cb->nlh->nlmsg_seq,
1242 IPCTNL_MSG_EXP_NEW,
1243 1, exp) < 0)
1244 goto out;
1245 *id = exp->id;
1246 }
1247out:
1248 read_unlock_bh(&nf_conntrack_lock);
1249
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001250 return skb->len;
1251}
1252
1253static const size_t cta_min_exp[CTA_EXPECT_MAX] = {
1254 [CTA_EXPECT_TIMEOUT-1] = sizeof(u_int32_t),
1255 [CTA_EXPECT_ID-1] = sizeof(u_int32_t)
1256};
1257
1258static int
1259ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1260 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1261{
1262 struct nf_conntrack_tuple tuple;
1263 struct nf_conntrack_expect *exp;
1264 struct sk_buff *skb2;
1265 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1266 u_int8_t u3 = nfmsg->nfgen_family;
1267 int err = 0;
1268
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001269 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1270 return -EINVAL;
1271
1272 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1273 u32 rlen;
1274
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001275 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
1276 ctnetlink_exp_dump_table,
1277 ctnetlink_done)) != 0)
1278 return -EINVAL;
1279 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1280 if (rlen > skb->len)
1281 rlen = skb->len;
1282 skb_pull(skb, rlen);
1283 return 0;
1284 }
1285
1286 if (cda[CTA_EXPECT_MASTER-1])
1287 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1288 else
1289 return -EINVAL;
1290
1291 if (err < 0)
1292 return err;
1293
Yasuyuki Kozakai468ec442006-11-29 02:35:23 +01001294 exp = nf_conntrack_expect_find_get(&tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001295 if (!exp)
1296 return -ENOENT;
1297
1298 if (cda[CTA_EXPECT_ID-1]) {
Patrick McHardybff9a892006-12-02 22:05:08 -08001299 __be32 id = *(__be32 *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001300 if (exp->id != ntohl(id)) {
1301 nf_conntrack_expect_put(exp);
1302 return -ENOENT;
1303 }
1304 }
1305
1306 err = -ENOMEM;
1307 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1308 if (!skb2)
1309 goto out;
Thomas Graf4e9b8262006-11-27 09:25:58 -08001310
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001311 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1312 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1313 1, exp);
1314 if (err <= 0)
1315 goto free;
1316
1317 nf_conntrack_expect_put(exp);
1318
1319 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1320
1321free:
1322 kfree_skb(skb2);
1323out:
1324 nf_conntrack_expect_put(exp);
1325 return err;
1326}
1327
1328static int
1329ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1330 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1331{
1332 struct nf_conntrack_expect *exp, *tmp;
1333 struct nf_conntrack_tuple tuple;
1334 struct nf_conntrack_helper *h;
1335 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1336 u_int8_t u3 = nfmsg->nfgen_family;
1337 int err;
1338
1339 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1340 return -EINVAL;
1341
1342 if (cda[CTA_EXPECT_TUPLE-1]) {
1343 /* delete a single expect by tuple */
1344 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1345 if (err < 0)
1346 return err;
1347
1348 /* bump usage count to 2 */
Yasuyuki Kozakai468ec442006-11-29 02:35:23 +01001349 exp = nf_conntrack_expect_find_get(&tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001350 if (!exp)
1351 return -ENOENT;
1352
1353 if (cda[CTA_EXPECT_ID-1]) {
Patrick McHardybff9a892006-12-02 22:05:08 -08001354 __be32 id = *(__be32 *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001355 if (exp->id != ntohl(id)) {
1356 nf_conntrack_expect_put(exp);
1357 return -ENOENT;
1358 }
1359 }
1360
1361 /* after list removal, usage count == 1 */
1362 nf_conntrack_unexpect_related(exp);
1363 /* have to put what we 'get' above.
1364 * after this line usage count == 0 */
1365 nf_conntrack_expect_put(exp);
1366 } else if (cda[CTA_EXPECT_HELP_NAME-1]) {
1367 char *name = NFA_DATA(cda[CTA_EXPECT_HELP_NAME-1]);
1368
1369 /* delete all expectations for this helper */
1370 write_lock_bh(&nf_conntrack_lock);
1371 h = __nf_conntrack_helper_find_byname(name);
1372 if (!h) {
1373 write_unlock_bh(&nf_conntrack_lock);
1374 return -EINVAL;
1375 }
1376 list_for_each_entry_safe(exp, tmp, &nf_conntrack_expect_list,
1377 list) {
Harald Weltedc808fe2006-03-20 17:56:32 -08001378 struct nf_conn_help *m_help = nfct_help(exp->master);
1379 if (m_help->helper == h
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001380 && del_timer(&exp->timeout)) {
1381 nf_ct_unlink_expect(exp);
1382 nf_conntrack_expect_put(exp);
1383 }
1384 }
1385 write_unlock_bh(&nf_conntrack_lock);
1386 } else {
1387 /* This basically means we have to flush everything*/
1388 write_lock_bh(&nf_conntrack_lock);
1389 list_for_each_entry_safe(exp, tmp, &nf_conntrack_expect_list,
1390 list) {
1391 if (del_timer(&exp->timeout)) {
1392 nf_ct_unlink_expect(exp);
1393 nf_conntrack_expect_put(exp);
1394 }
1395 }
1396 write_unlock_bh(&nf_conntrack_lock);
1397 }
1398
1399 return 0;
1400}
1401static int
1402ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nfattr *cda[])
1403{
1404 return -EOPNOTSUPP;
1405}
1406
1407static int
1408ctnetlink_create_expect(struct nfattr *cda[], u_int8_t u3)
1409{
1410 struct nf_conntrack_tuple tuple, mask, master_tuple;
1411 struct nf_conntrack_tuple_hash *h = NULL;
1412 struct nf_conntrack_expect *exp;
1413 struct nf_conn *ct;
Harald Weltedc808fe2006-03-20 17:56:32 -08001414 struct nf_conn_help *help;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001415 int err = 0;
1416
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001417 /* caller guarantees that those three CTA_EXPECT_* exist */
1418 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1419 if (err < 0)
1420 return err;
1421 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1422 if (err < 0)
1423 return err;
1424 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1425 if (err < 0)
1426 return err;
1427
1428 /* Look for master conntrack of this expectation */
1429 h = nf_conntrack_find_get(&master_tuple, NULL);
1430 if (!h)
1431 return -ENOENT;
1432 ct = nf_ct_tuplehash_to_ctrack(h);
Harald Weltedc808fe2006-03-20 17:56:32 -08001433 help = nfct_help(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001434
Harald Weltedc808fe2006-03-20 17:56:32 -08001435 if (!help || !help->helper) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001436 /* such conntrack hasn't got any helper, abort */
1437 err = -EINVAL;
1438 goto out;
1439 }
1440
1441 exp = nf_conntrack_expect_alloc(ct);
1442 if (!exp) {
1443 err = -ENOMEM;
1444 goto out;
1445 }
1446
1447 exp->expectfn = NULL;
1448 exp->flags = 0;
1449 exp->master = ct;
Patrick McHardy9457d852006-12-02 22:05:25 -08001450 exp->helper = NULL;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001451 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1452 memcpy(&exp->mask, &mask, sizeof(struct nf_conntrack_tuple));
1453
1454 err = nf_conntrack_expect_related(exp);
1455 nf_conntrack_expect_put(exp);
1456
1457out:
1458 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1459 return err;
1460}
1461
1462static int
1463ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1464 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1465{
1466 struct nf_conntrack_tuple tuple;
1467 struct nf_conntrack_expect *exp;
1468 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1469 u_int8_t u3 = nfmsg->nfgen_family;
1470 int err = 0;
1471
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001472 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1473 return -EINVAL;
1474
1475 if (!cda[CTA_EXPECT_TUPLE-1]
1476 || !cda[CTA_EXPECT_MASK-1]
1477 || !cda[CTA_EXPECT_MASTER-1])
1478 return -EINVAL;
1479
1480 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1481 if (err < 0)
1482 return err;
1483
1484 write_lock_bh(&nf_conntrack_lock);
1485 exp = __nf_conntrack_expect_find(&tuple);
1486
1487 if (!exp) {
1488 write_unlock_bh(&nf_conntrack_lock);
1489 err = -ENOENT;
1490 if (nlh->nlmsg_flags & NLM_F_CREATE)
1491 err = ctnetlink_create_expect(cda, u3);
1492 return err;
1493 }
1494
1495 err = -EEXIST;
1496 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1497 err = ctnetlink_change_expect(exp, cda);
1498 write_unlock_bh(&nf_conntrack_lock);
1499
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001500 return err;
1501}
1502
1503#ifdef CONFIG_NF_CONNTRACK_EVENTS
1504static struct notifier_block ctnl_notifier = {
1505 .notifier_call = ctnetlink_conntrack_event,
1506};
1507
1508static struct notifier_block ctnl_notifier_exp = {
1509 .notifier_call = ctnetlink_expect_event,
1510};
1511#endif
1512
1513static struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1514 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1515 .attr_count = CTA_MAX, },
1516 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1517 .attr_count = CTA_MAX, },
1518 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1519 .attr_count = CTA_MAX, },
1520 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1521 .attr_count = CTA_MAX, },
1522};
1523
1524static struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1525 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1526 .attr_count = CTA_EXPECT_MAX, },
1527 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1528 .attr_count = CTA_EXPECT_MAX, },
1529 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1530 .attr_count = CTA_EXPECT_MAX, },
1531};
1532
1533static struct nfnetlink_subsystem ctnl_subsys = {
1534 .name = "conntrack",
1535 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1536 .cb_count = IPCTNL_MSG_MAX,
1537 .cb = ctnl_cb,
1538};
1539
1540static struct nfnetlink_subsystem ctnl_exp_subsys = {
1541 .name = "conntrack_expect",
1542 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1543 .cb_count = IPCTNL_MSG_EXP_MAX,
1544 .cb = ctnl_exp_cb,
1545};
1546
Patrick McHardyd2483dd2006-12-02 22:06:05 -08001547MODULE_ALIAS("ip_conntrack_netlink");
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001548MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
Pablo Neira Ayuso34f9a2e2006-02-04 02:11:41 -08001549MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001550
1551static int __init ctnetlink_init(void)
1552{
1553 int ret;
1554
1555 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1556 ret = nfnetlink_subsys_register(&ctnl_subsys);
1557 if (ret < 0) {
1558 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1559 goto err_out;
1560 }
1561
1562 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1563 if (ret < 0) {
1564 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1565 goto err_unreg_subsys;
1566 }
1567
1568#ifdef CONFIG_NF_CONNTRACK_EVENTS
1569 ret = nf_conntrack_register_notifier(&ctnl_notifier);
1570 if (ret < 0) {
1571 printk("ctnetlink_init: cannot register notifier.\n");
1572 goto err_unreg_exp_subsys;
1573 }
1574
1575 ret = nf_conntrack_expect_register_notifier(&ctnl_notifier_exp);
1576 if (ret < 0) {
1577 printk("ctnetlink_init: cannot expect register notifier.\n");
1578 goto err_unreg_notifier;
1579 }
1580#endif
1581
1582 return 0;
1583
1584#ifdef CONFIG_NF_CONNTRACK_EVENTS
1585err_unreg_notifier:
1586 nf_conntrack_unregister_notifier(&ctnl_notifier);
1587err_unreg_exp_subsys:
1588 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1589#endif
1590err_unreg_subsys:
1591 nfnetlink_subsys_unregister(&ctnl_subsys);
1592err_out:
1593 return ret;
1594}
1595
1596static void __exit ctnetlink_exit(void)
1597{
1598 printk("ctnetlink: unregistering from nfnetlink.\n");
1599
1600#ifdef CONFIG_NF_CONNTRACK_EVENTS
Martin Josefssone64a70b2006-04-01 02:24:48 -08001601 nf_conntrack_expect_unregister_notifier(&ctnl_notifier_exp);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001602 nf_conntrack_unregister_notifier(&ctnl_notifier);
1603#endif
1604
1605 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1606 nfnetlink_subsys_unregister(&ctnl_subsys);
1607 return;
1608}
1609
1610module_init(ctnetlink_init);
1611module_exit(ctnetlink_exit);