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