]>
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> | |
40a839fd | 32 | #include <linux/interrupt.h> |
c1d10adb PNA |
33 | #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> | |
77ab9cff | 38 | #include <net/netfilter/nf_conntrack_expect.h> |
c1d10adb PNA |
39 | #include <net/netfilter/nf_conntrack_helper.h> |
40 | #include <net/netfilter/nf_conntrack_l3proto.h> | |
605dcad6 | 41 | #include <net/netfilter/nf_conntrack_l4proto.h> |
c1d10adb PNA |
42 | #include <linux/netfilter_ipv4/ip_nat_protocol.h> |
43 | ||
44 | #include <linux/netfilter/nfnetlink.h> | |
45 | #include <linux/netfilter/nfnetlink_conntrack.h> | |
46 | ||
47 | MODULE_LICENSE("GPL"); | |
48 | ||
dc808fe2 | 49 | static char __initdata version[] = "0.93"; |
c1d10adb | 50 | |
c1d10adb PNA |
51 | static inline int |
52 | ctnetlink_dump_tuples_proto(struct sk_buff *skb, | |
1cde6436 | 53 | const struct nf_conntrack_tuple *tuple, |
605dcad6 | 54 | struct nf_conntrack_l4proto *l4proto) |
c1d10adb | 55 | { |
c1d10adb | 56 | int ret = 0; |
1cde6436 | 57 | struct nfattr *nest_parms = NFA_NEST(skb, CTA_TUPLE_PROTO); |
c1d10adb PNA |
58 | |
59 | NFA_PUT(skb, CTA_PROTO_NUM, sizeof(u_int8_t), &tuple->dst.protonum); | |
60 | ||
605dcad6 MJ |
61 | if (likely(l4proto->tuple_to_nfattr)) |
62 | ret = l4proto->tuple_to_nfattr(skb, tuple); | |
c1d10adb | 63 | |
1cde6436 | 64 | NFA_NEST_END(skb, nest_parms); |
c1d10adb PNA |
65 | |
66 | return ret; | |
67 | ||
68 | nfattr_failure: | |
69 | return -1; | |
70 | } | |
71 | ||
72 | static inline int | |
1cde6436 PNA |
73 | ctnetlink_dump_tuples_ip(struct sk_buff *skb, |
74 | const struct nf_conntrack_tuple *tuple, | |
75 | struct nf_conntrack_l3proto *l3proto) | |
c1d10adb | 76 | { |
c1d10adb | 77 | int ret = 0; |
1cde6436 PNA |
78 | struct nfattr *nest_parms = NFA_NEST(skb, CTA_TUPLE_IP); |
79 | ||
c1d10adb PNA |
80 | if (likely(l3proto->tuple_to_nfattr)) |
81 | ret = l3proto->tuple_to_nfattr(skb, tuple); | |
1cde6436 | 82 | |
c1d10adb PNA |
83 | NFA_NEST_END(skb, nest_parms); |
84 | ||
1cde6436 PNA |
85 | return ret; |
86 | ||
87 | nfattr_failure: | |
88 | return -1; | |
89 | } | |
90 | ||
91 | static inline int | |
92 | ctnetlink_dump_tuples(struct sk_buff *skb, | |
93 | const struct nf_conntrack_tuple *tuple) | |
94 | { | |
95 | int ret; | |
96 | struct nf_conntrack_l3proto *l3proto; | |
605dcad6 | 97 | struct nf_conntrack_l4proto *l4proto; |
1cde6436 PNA |
98 | |
99 | l3proto = nf_ct_l3proto_find_get(tuple->src.l3num); | |
100 | ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto); | |
c1d10adb PNA |
101 | nf_ct_l3proto_put(l3proto); |
102 | ||
103 | if (unlikely(ret < 0)) | |
104 | return ret; | |
105 | ||
605dcad6 MJ |
106 | l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum); |
107 | ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto); | |
108 | nf_ct_l4proto_put(l4proto); | |
c1d10adb PNA |
109 | |
110 | return ret; | |
c1d10adb PNA |
111 | } |
112 | ||
113 | static inline int | |
114 | ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct) | |
115 | { | |
116 | u_int32_t status = htonl((u_int32_t) ct->status); | |
117 | NFA_PUT(skb, CTA_STATUS, sizeof(status), &status); | |
118 | return 0; | |
119 | ||
120 | nfattr_failure: | |
121 | return -1; | |
122 | } | |
123 | ||
124 | static inline int | |
125 | ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct) | |
126 | { | |
127 | long timeout_l = ct->timeout.expires - jiffies; | |
128 | u_int32_t timeout; | |
129 | ||
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 | ||
138 | nfattr_failure: | |
139 | return -1; | |
140 | } | |
141 | ||
142 | static inline int | |
143 | ctnetlink_dump_protoinfo(struct sk_buff *skb, const struct nf_conn *ct) | |
144 | { | |
605dcad6 | 145 | struct nf_conntrack_l4proto *l4proto = nf_ct_l4proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num, ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum); |
c1d10adb PNA |
146 | struct nfattr *nest_proto; |
147 | int ret; | |
148 | ||
605dcad6 MJ |
149 | if (!l4proto->to_nfattr) { |
150 | nf_ct_l4proto_put(l4proto); | |
c1d10adb PNA |
151 | return 0; |
152 | } | |
153 | ||
154 | nest_proto = NFA_NEST(skb, CTA_PROTOINFO); | |
155 | ||
605dcad6 | 156 | ret = l4proto->to_nfattr(skb, nest_proto, ct); |
c1d10adb | 157 | |
605dcad6 | 158 | nf_ct_l4proto_put(l4proto); |
c1d10adb PNA |
159 | |
160 | NFA_NEST_END(skb, nest_proto); | |
161 | ||
162 | return ret; | |
163 | ||
164 | nfattr_failure: | |
605dcad6 | 165 | nf_ct_l4proto_put(l4proto); |
c1d10adb PNA |
166 | return -1; |
167 | } | |
168 | ||
169 | static inline int | |
170 | ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct) | |
171 | { | |
172 | struct nfattr *nest_helper; | |
dc808fe2 | 173 | const struct nf_conn_help *help = nfct_help(ct); |
c1d10adb | 174 | |
dc808fe2 | 175 | if (!help || !help->helper) |
c1d10adb PNA |
176 | return 0; |
177 | ||
178 | nest_helper = NFA_NEST(skb, CTA_HELP); | |
dc808fe2 | 179 | NFA_PUT(skb, CTA_HELP_NAME, strlen(help->helper->name), help->helper->name); |
c1d10adb | 180 | |
dc808fe2 HW |
181 | if (help->helper->to_nfattr) |
182 | help->helper->to_nfattr(skb, ct); | |
c1d10adb PNA |
183 | |
184 | NFA_NEST_END(skb, nest_helper); | |
185 | ||
186 | return 0; | |
187 | ||
188 | nfattr_failure: | |
189 | return -1; | |
190 | } | |
191 | ||
192 | #ifdef CONFIG_NF_CT_ACCT | |
193 | static inline int | |
194 | ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct, | |
195 | enum ip_conntrack_dir dir) | |
196 | { | |
197 | enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG; | |
198 | struct nfattr *nest_count = NFA_NEST(skb, type); | |
199 | u_int32_t tmp; | |
200 | ||
201 | tmp = htonl(ct->counters[dir].packets); | |
202 | NFA_PUT(skb, CTA_COUNTERS32_PACKETS, sizeof(u_int32_t), &tmp); | |
203 | ||
204 | tmp = htonl(ct->counters[dir].bytes); | |
205 | NFA_PUT(skb, CTA_COUNTERS32_BYTES, sizeof(u_int32_t), &tmp); | |
206 | ||
207 | NFA_NEST_END(skb, nest_count); | |
208 | ||
209 | return 0; | |
210 | ||
211 | nfattr_failure: | |
212 | return -1; | |
213 | } | |
214 | #else | |
215 | #define ctnetlink_dump_counters(a, b, c) (0) | |
216 | #endif | |
217 | ||
218 | #ifdef CONFIG_NF_CONNTRACK_MARK | |
219 | static inline int | |
220 | ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct) | |
221 | { | |
222 | u_int32_t mark = htonl(ct->mark); | |
223 | ||
224 | NFA_PUT(skb, CTA_MARK, sizeof(u_int32_t), &mark); | |
225 | return 0; | |
226 | ||
227 | nfattr_failure: | |
228 | return -1; | |
229 | } | |
230 | #else | |
231 | #define ctnetlink_dump_mark(a, b) (0) | |
232 | #endif | |
233 | ||
234 | static inline int | |
235 | ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct) | |
236 | { | |
237 | u_int32_t id = htonl(ct->id); | |
238 | NFA_PUT(skb, CTA_ID, sizeof(u_int32_t), &id); | |
239 | return 0; | |
240 | ||
241 | nfattr_failure: | |
242 | return -1; | |
243 | } | |
244 | ||
245 | static inline int | |
246 | ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct) | |
247 | { | |
248 | u_int32_t use = htonl(atomic_read(&ct->ct_general.use)); | |
249 | ||
250 | NFA_PUT(skb, CTA_USE, sizeof(u_int32_t), &use); | |
251 | return 0; | |
252 | ||
253 | nfattr_failure: | |
254 | return -1; | |
255 | } | |
256 | ||
257 | #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple) | |
258 | ||
259 | static int | |
260 | ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq, | |
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 | ||
305 | nlmsg_failure: | |
306 | nfattr_failure: | |
307 | skb_trim(skb, b - skb->data); | |
308 | return -1; | |
309 | } | |
310 | ||
311 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
312 | static 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; | |
c1d10adb | 334 | group = NFNLGRP_CONNTRACK_NEW; |
1a31526b | 335 | } else if (events & (IPCT_STATUS | IPCT_PROTOINFO)) { |
c1d10adb PNA |
336 | type = IPCTNL_MSG_CT_NEW; |
337 | group = NFNLGRP_CONNTRACK_UPDATE; | |
338 | } else | |
339 | return NOTIFY_DONE; | |
a2427692 PM |
340 | |
341 | if (!nfnetlink_has_listeners(group)) | |
342 | return NOTIFY_DONE; | |
343 | ||
c1d10adb PNA |
344 | 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); | |
c1d10adb | 368 | |
7b621c1e PNA |
369 | 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; | |
376 | ||
377 | 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 | } | |
b9a37e0c | 397 | |
c1d10adb PNA |
398 | nlh->nlmsg_len = skb->tail - b; |
399 | nfnetlink_send(skb, 0, group, 0); | |
400 | return NOTIFY_DONE; | |
401 | ||
402 | nlmsg_failure: | |
403 | nfattr_failure: | |
404 | kfree_skb(skb); | |
405 | return NOTIFY_DONE; | |
406 | } | |
407 | #endif /* CONFIG_NF_CONNTRACK_EVENTS */ | |
408 | ||
409 | static int ctnetlink_done(struct netlink_callback *cb) | |
410 | { | |
89f2e218 PM |
411 | if (cb->args[1]) |
412 | nf_ct_put((struct nf_conn *)cb->args[1]); | |
c1d10adb PNA |
413 | return 0; |
414 | } | |
415 | ||
87711cb8 PNA |
416 | #define L3PROTO(ct) ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num |
417 | ||
c1d10adb PNA |
418 | static int |
419 | ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb) | |
420 | { | |
89f2e218 | 421 | struct nf_conn *ct, *last; |
c1d10adb PNA |
422 | struct nf_conntrack_tuple_hash *h; |
423 | struct list_head *i; | |
87711cb8 PNA |
424 | struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh); |
425 | u_int8_t l3proto = nfmsg->nfgen_family; | |
c1d10adb | 426 | |
c1d10adb | 427 | read_lock_bh(&nf_conntrack_lock); |
d205dc40 | 428 | last = (struct nf_conn *)cb->args[1]; |
89f2e218 PM |
429 | for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) { |
430 | restart: | |
c1d10adb PNA |
431 | list_for_each_prev(i, &nf_conntrack_hash[cb->args[0]]) { |
432 | h = (struct nf_conntrack_tuple_hash *) i; | |
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; | |
d205dc40 PM |
441 | if (cb->args[1]) { |
442 | if (ct != last) | |
89f2e218 | 443 | continue; |
d205dc40 | 444 | cb->args[1] = 0; |
89f2e218 | 445 | } |
c1d10adb PNA |
446 | if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid, |
447 | cb->nlh->nlmsg_seq, | |
448 | IPCTNL_MSG_CT_NEW, | |
89f2e218 PM |
449 | 1, ct) < 0) { |
450 | nf_conntrack_get(&ct->ct_general); | |
451 | cb->args[1] = (unsigned long)ct; | |
c1d10adb | 452 | goto out; |
89f2e218 | 453 | } |
01f34848 PNA |
454 | #ifdef CONFIG_NF_CT_ACCT |
455 | if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == | |
456 | IPCTNL_MSG_CT_GET_CTRZERO) | |
457 | memset(&ct->counters, 0, sizeof(ct->counters)); | |
458 | #endif | |
89f2e218 | 459 | } |
d205dc40 | 460 | if (cb->args[1]) { |
89f2e218 PM |
461 | cb->args[1] = 0; |
462 | goto restart; | |
c1d10adb PNA |
463 | } |
464 | } | |
89f2e218 | 465 | out: |
c1d10adb | 466 | read_unlock_bh(&nf_conntrack_lock); |
d205dc40 PM |
467 | if (last) |
468 | nf_ct_put(last); | |
c1d10adb | 469 | |
c1d10adb PNA |
470 | return skb->len; |
471 | } | |
472 | ||
c1d10adb PNA |
473 | static inline int |
474 | ctnetlink_parse_tuple_ip(struct nfattr *attr, struct nf_conntrack_tuple *tuple) | |
475 | { | |
476 | struct nfattr *tb[CTA_IP_MAX]; | |
477 | struct nf_conntrack_l3proto *l3proto; | |
478 | int ret = 0; | |
479 | ||
c1d10adb PNA |
480 | nfattr_parse_nested(tb, CTA_IP_MAX, attr); |
481 | ||
482 | l3proto = nf_ct_l3proto_find_get(tuple->src.l3num); | |
483 | ||
484 | if (likely(l3proto->nfattr_to_tuple)) | |
485 | ret = l3proto->nfattr_to_tuple(tb, tuple); | |
486 | ||
487 | nf_ct_l3proto_put(l3proto); | |
488 | ||
c1d10adb PNA |
489 | return ret; |
490 | } | |
491 | ||
492 | static const size_t cta_min_proto[CTA_PROTO_MAX] = { | |
493 | [CTA_PROTO_NUM-1] = sizeof(u_int8_t), | |
494 | }; | |
495 | ||
496 | static inline int | |
497 | ctnetlink_parse_tuple_proto(struct nfattr *attr, | |
498 | struct nf_conntrack_tuple *tuple) | |
499 | { | |
500 | struct nfattr *tb[CTA_PROTO_MAX]; | |
605dcad6 | 501 | struct nf_conntrack_l4proto *l4proto; |
c1d10adb PNA |
502 | int ret = 0; |
503 | ||
c1d10adb PNA |
504 | nfattr_parse_nested(tb, CTA_PROTO_MAX, attr); |
505 | ||
506 | if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto)) | |
507 | return -EINVAL; | |
508 | ||
509 | if (!tb[CTA_PROTO_NUM-1]) | |
510 | return -EINVAL; | |
511 | tuple->dst.protonum = *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_NUM-1]); | |
512 | ||
605dcad6 | 513 | l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum); |
c1d10adb | 514 | |
605dcad6 MJ |
515 | if (likely(l4proto->nfattr_to_tuple)) |
516 | ret = l4proto->nfattr_to_tuple(tb, tuple); | |
c1d10adb | 517 | |
605dcad6 | 518 | nf_ct_l4proto_put(l4proto); |
c1d10adb PNA |
519 | |
520 | return ret; | |
521 | } | |
522 | ||
523 | static inline int | |
524 | ctnetlink_parse_tuple(struct nfattr *cda[], struct nf_conntrack_tuple *tuple, | |
525 | enum ctattr_tuple type, u_int8_t l3num) | |
526 | { | |
527 | struct nfattr *tb[CTA_TUPLE_MAX]; | |
528 | int err; | |
529 | ||
c1d10adb PNA |
530 | memset(tuple, 0, sizeof(*tuple)); |
531 | ||
532 | nfattr_parse_nested(tb, CTA_TUPLE_MAX, cda[type-1]); | |
533 | ||
534 | if (!tb[CTA_TUPLE_IP-1]) | |
535 | return -EINVAL; | |
536 | ||
537 | tuple->src.l3num = l3num; | |
538 | ||
539 | err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP-1], tuple); | |
540 | if (err < 0) | |
541 | return err; | |
542 | ||
543 | if (!tb[CTA_TUPLE_PROTO-1]) | |
544 | return -EINVAL; | |
545 | ||
546 | err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO-1], tuple); | |
547 | if (err < 0) | |
548 | return err; | |
549 | ||
550 | /* orig and expect tuples get DIR_ORIGINAL */ | |
551 | if (type == CTA_TUPLE_REPLY) | |
552 | tuple->dst.dir = IP_CT_DIR_REPLY; | |
553 | else | |
554 | tuple->dst.dir = IP_CT_DIR_ORIGINAL; | |
555 | ||
c1d10adb PNA |
556 | return 0; |
557 | } | |
558 | ||
559 | #ifdef CONFIG_IP_NF_NAT_NEEDED | |
560 | static const size_t cta_min_protonat[CTA_PROTONAT_MAX] = { | |
561 | [CTA_PROTONAT_PORT_MIN-1] = sizeof(u_int16_t), | |
562 | [CTA_PROTONAT_PORT_MAX-1] = sizeof(u_int16_t), | |
563 | }; | |
564 | ||
565 | static 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 | ||
c1d10adb PNA |
572 | nfattr_parse_nested(tb, CTA_PROTONAT_MAX, attr); |
573 | ||
574 | if (nfattr_bad_size(tb, CTA_PROTONAT_MAX, cta_min_protonat)) | |
575 | return -EINVAL; | |
576 | ||
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 | ||
c1d10adb PNA |
590 | return 0; |
591 | } | |
592 | ||
593 | static const size_t cta_min_nat[CTA_NAT_MAX] = { | |
594 | [CTA_NAT_MINIP-1] = sizeof(u_int32_t), | |
595 | [CTA_NAT_MAXIP-1] = sizeof(u_int32_t), | |
596 | }; | |
597 | ||
598 | static inline int | |
3726add7 | 599 | ctnetlink_parse_nat(struct nfattr *nat, |
c1d10adb PNA |
600 | const struct nf_conn *ct, struct ip_nat_range *range) |
601 | { | |
602 | struct nfattr *tb[CTA_NAT_MAX]; | |
603 | int err; | |
604 | ||
c1d10adb PNA |
605 | memset(range, 0, sizeof(*range)); |
606 | ||
3726add7 | 607 | nfattr_parse_nested(tb, CTA_NAT_MAX, nat); |
c1d10adb PNA |
608 | |
609 | if (nfattr_bad_size(tb, CTA_NAT_MAX, cta_min_nat)) | |
610 | return -EINVAL; | |
611 | ||
612 | if (tb[CTA_NAT_MINIP-1]) | |
613 | range->min_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MINIP-1]); | |
614 | ||
615 | if (!tb[CTA_NAT_MAXIP-1]) | |
616 | range->max_ip = range->min_ip; | |
617 | else | |
618 | range->max_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MAXIP-1]); | |
619 | ||
620 | if (range->min_ip) | |
621 | range->flags |= IP_NAT_RANGE_MAP_IPS; | |
622 | ||
623 | if (!tb[CTA_NAT_PROTO-1]) | |
624 | return 0; | |
625 | ||
626 | err = ctnetlink_parse_nat_proto(tb[CTA_NAT_PROTO-1], ct, range); | |
627 | if (err < 0) | |
628 | return err; | |
629 | ||
c1d10adb PNA |
630 | return 0; |
631 | } | |
632 | #endif | |
633 | ||
634 | static inline int | |
635 | ctnetlink_parse_help(struct nfattr *attr, char **helper_name) | |
636 | { | |
637 | struct nfattr *tb[CTA_HELP_MAX]; | |
638 | ||
c1d10adb PNA |
639 | nfattr_parse_nested(tb, CTA_HELP_MAX, attr); |
640 | ||
641 | if (!tb[CTA_HELP_NAME-1]) | |
642 | return -EINVAL; | |
643 | ||
644 | *helper_name = NFA_DATA(tb[CTA_HELP_NAME-1]); | |
645 | ||
646 | return 0; | |
647 | } | |
648 | ||
649 | static const size_t cta_min[CTA_MAX] = { | |
650 | [CTA_STATUS-1] = sizeof(u_int32_t), | |
651 | [CTA_TIMEOUT-1] = sizeof(u_int32_t), | |
652 | [CTA_MARK-1] = sizeof(u_int32_t), | |
653 | [CTA_USE-1] = sizeof(u_int32_t), | |
654 | [CTA_ID-1] = sizeof(u_int32_t) | |
655 | }; | |
656 | ||
657 | static int | |
658 | ctnetlink_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 | ||
c1d10adb PNA |
668 | if (nfattr_bad_size(cda, CTA_MAX, cta_min)) |
669 | return -EINVAL; | |
670 | ||
671 | if (cda[CTA_TUPLE_ORIG-1]) | |
672 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3); | |
673 | else if (cda[CTA_TUPLE_REPLY-1]) | |
674 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3); | |
675 | else { | |
676 | /* Flush the whole table */ | |
677 | nf_conntrack_flush(); | |
678 | return 0; | |
679 | } | |
680 | ||
681 | if (err < 0) | |
682 | return err; | |
683 | ||
684 | h = nf_conntrack_find_get(&tuple, NULL); | |
9ea8cfd6 | 685 | if (!h) |
c1d10adb | 686 | return -ENOENT; |
c1d10adb PNA |
687 | |
688 | ct = nf_ct_tuplehash_to_ctrack(h); | |
689 | ||
690 | if (cda[CTA_ID-1]) { | |
691 | u_int32_t id = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_ID-1])); | |
692 | 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); | |
c1d10adb PNA |
701 | |
702 | return 0; | |
703 | } | |
704 | ||
705 | static int | |
706 | ctnetlink_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 | ||
c1d10adb PNA |
717 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
718 | u32 rlen; | |
719 | ||
01f34848 PNA |
720 | #ifndef CONFIG_NF_CT_ACCT |
721 | if (NFNL_MSG_TYPE(nlh->nlmsg_type) == IPCTNL_MSG_CT_GET_CTRZERO) | |
c1d10adb PNA |
722 | return -ENOTSUPP; |
723 | #endif | |
01f34848 PNA |
724 | if ((*errp = netlink_dump_start(ctnl, skb, nlh, |
725 | ctnetlink_dump_table, | |
726 | ctnetlink_done)) != 0) | |
c1d10adb | 727 | return -EINVAL; |
c1d10adb PNA |
728 | |
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); | |
9ea8cfd6 | 750 | if (!h) |
c1d10adb | 751 | return -ENOENT; |
9ea8cfd6 | 752 | |
c1d10adb PNA |
753 | 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 | } | |
c1d10adb PNA |
761 | |
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 | ||
c1d10adb PNA |
772 | return 0; |
773 | ||
774 | free: | |
775 | kfree_skb(skb2); | |
776 | out: | |
777 | return err; | |
778 | } | |
779 | ||
780 | static inline int | |
781 | ctnetlink_change_status(struct nf_conn *ct, struct nfattr *cda[]) | |
782 | { | |
783 | unsigned long d; | |
784 | unsigned status = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_STATUS-1])); | |
785 | 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 | ||
3726add7 | 800 | if (cda[CTA_NAT_SRC-1] || cda[CTA_NAT_DST-1]) { |
c1d10adb PNA |
801 | #ifndef CONFIG_IP_NF_NAT_NEEDED |
802 | return -EINVAL; | |
803 | #else | |
c1d10adb PNA |
804 | struct ip_nat_range range; |
805 | ||
3726add7 PM |
806 | 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 | } | |
c1d10adb PNA |
824 | #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 | ||
835 | static inline int | |
836 | ctnetlink_change_helper(struct nf_conn *ct, struct nfattr *cda[]) | |
837 | { | |
838 | struct nf_conntrack_helper *helper; | |
dc808fe2 | 839 | struct nf_conn_help *help = nfct_help(ct); |
c1d10adb PNA |
840 | char *helpname; |
841 | int err; | |
842 | ||
dc808fe2 HW |
843 | if (!help) { |
844 | /* FIXME: we need to reallocate and rehash */ | |
845 | return -EBUSY; | |
846 | } | |
847 | ||
c1d10adb PNA |
848 | /* 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 | ||
dc808fe2 | 864 | if (help->helper) { |
c1d10adb PNA |
865 | if (!helper) { |
866 | /* we had a helper before ... */ | |
867 | nf_ct_remove_expectations(ct); | |
dc808fe2 | 868 | help->helper = NULL; |
c1d10adb PNA |
869 | } else { |
870 | /* need to zero data of old helper */ | |
dc808fe2 | 871 | memset(&help->help, 0, sizeof(help->help)); |
c1d10adb PNA |
872 | } |
873 | } | |
874 | ||
dc808fe2 | 875 | help->helper = helper; |
c1d10adb PNA |
876 | |
877 | return 0; | |
878 | } | |
879 | ||
880 | static inline int | |
881 | ctnetlink_change_timeout(struct nf_conn *ct, struct nfattr *cda[]) | |
882 | { | |
883 | u_int32_t timeout = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1])); | |
884 | ||
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 | ||
894 | static inline int | |
895 | ctnetlink_change_protoinfo(struct nf_conn *ct, struct nfattr *cda[]) | |
896 | { | |
897 | struct nfattr *tb[CTA_PROTOINFO_MAX], *attr = cda[CTA_PROTOINFO-1]; | |
605dcad6 | 898 | struct nf_conntrack_l4proto *l4proto; |
c1d10adb PNA |
899 | 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 | ||
605dcad6 | 905 | l4proto = nf_ct_l4proto_find_get(l3num, npt); |
c1d10adb | 906 | |
605dcad6 MJ |
907 | if (l4proto->from_nfattr) |
908 | err = l4proto->from_nfattr(tb, ct); | |
909 | nf_ct_l4proto_put(l4proto); | |
c1d10adb PNA |
910 | |
911 | return err; | |
912 | } | |
913 | ||
914 | static int | |
915 | ctnetlink_change_conntrack(struct nf_conn *ct, struct nfattr *cda[]) | |
916 | { | |
917 | int err; | |
918 | ||
c1d10adb PNA |
919 | 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 | ||
bcd1e830 | 943 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
c1d10adb PNA |
944 | if (cda[CTA_MARK-1]) |
945 | ct->mark = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_MARK-1])); | |
946 | #endif | |
947 | ||
c1d10adb PNA |
948 | return 0; |
949 | } | |
950 | ||
951 | static int | |
952 | ctnetlink_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; | |
dafc741c | 958 | struct nf_conn_help *help; |
c1d10adb | 959 | |
c1d10adb PNA |
960 | 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; | |
966 | ct->timeout.expires = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1])); | |
967 | ||
968 | ct->timeout.expires = jiffies + ct->timeout.expires * HZ; | |
969 | ct->status |= IPS_CONFIRMED; | |
970 | ||
bbb3357d PNA |
971 | if (cda[CTA_STATUS-1]) { |
972 | err = ctnetlink_change_status(ct, cda); | |
973 | if (err < 0) | |
974 | goto err; | |
975 | } | |
c1d10adb PNA |
976 | |
977 | if (cda[CTA_PROTOINFO-1]) { | |
978 | err = ctnetlink_change_protoinfo(ct, cda); | |
979 | if (err < 0) | |
980 | return err; | |
981 | } | |
982 | ||
bcd1e830 | 983 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
c1d10adb PNA |
984 | if (cda[CTA_MARK-1]) |
985 | ct->mark = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_MARK-1])); | |
986 | #endif | |
987 | ||
dafc741c YK |
988 | help = nfct_help(ct); |
989 | if (help) | |
990 | help->helper = nf_ct_helper_find_get(rtuple); | |
991 | ||
c1d10adb PNA |
992 | add_timer(&ct->timeout); |
993 | nf_conntrack_hash_insert(ct); | |
994 | ||
dafc741c YK |
995 | if (help && help->helper) |
996 | nf_ct_helper_put(help->helper); | |
997 | ||
c1d10adb PNA |
998 | return 0; |
999 | ||
1000 | err: | |
1001 | nf_conntrack_free(ct); | |
1002 | return err; | |
1003 | } | |
1004 | ||
1005 | static int | |
1006 | ctnetlink_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 | ||
c1d10adb PNA |
1015 | 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); | |
c1d10adb PNA |
1038 | 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 */ | |
3726add7 | 1046 | if (cda[CTA_NAT_SRC-1] || cda[CTA_NAT_DST-1]) { |
c1d10adb PNA |
1047 | 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 */ | |
c1d10adb PNA |
1053 | err = -EEXIST; |
1054 | if (!(nlh->nlmsg_flags & NLM_F_EXCL)) | |
1055 | err = ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h), cda); | |
1056 | ||
1057 | out_unlock: | |
1058 | write_unlock_bh(&nf_conntrack_lock); | |
1059 | return err; | |
1060 | } | |
1061 | ||
1062 | /*********************************************************************** | |
1063 | * EXPECT | |
1064 | ***********************************************************************/ | |
1065 | ||
1066 | static inline int | |
1067 | ctnetlink_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 | ||
1080 | nfattr_failure: | |
1081 | return -1; | |
1082 | } | |
1083 | ||
1cde6436 PNA |
1084 | static inline int |
1085 | ctnetlink_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; | |
605dcad6 | 1091 | struct nf_conntrack_l4proto *l4proto; |
1cde6436 PNA |
1092 | 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 | ||
605dcad6 MJ |
1101 | 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); | |
1cde6436 PNA |
1104 | if (unlikely(ret < 0)) |
1105 | goto nfattr_failure; | |
1106 | ||
1107 | NFA_NEST_END(skb, nest_parms); | |
1108 | ||
1109 | return 0; | |
1110 | ||
1111 | nfattr_failure: | |
1112 | return -1; | |
1113 | } | |
1114 | ||
c1d10adb PNA |
1115 | static inline int |
1116 | ctnetlink_exp_dump_expect(struct sk_buff *skb, | |
1117 | const struct nf_conntrack_expect *exp) | |
1118 | { | |
1119 | struct nf_conn *master = exp->master; | |
1120 | u_int32_t timeout = htonl((exp->timeout.expires - jiffies) / HZ); | |
1121 | u_int32_t id = htonl(exp->id); | |
1122 | ||
1123 | if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0) | |
1124 | goto nfattr_failure; | |
1cde6436 | 1125 | if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0) |
c1d10adb PNA |
1126 | 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 | ||
1137 | nfattr_failure: | |
1138 | return -1; | |
1139 | } | |
1140 | ||
1141 | static int | |
1142 | ctnetlink_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 | ||
1168 | nlmsg_failure: | |
1169 | nfattr_failure: | |
1170 | skb_trim(skb, b - skb->data); | |
1171 | return -1; | |
1172 | } | |
1173 | ||
1174 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
1175 | static 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 | ||
b3a27bfb PNA |
1192 | if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW)) |
1193 | return NOTIFY_DONE; | |
1194 | ||
c1d10adb PNA |
1195 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
1196 | if (!skb) | |
1197 | return NOTIFY_DONE; | |
1198 | ||
1199 | b = skb->tail; | |
1200 | ||
b633ad5f | 1201 | type |= NFNL_SUBSYS_CTNETLINK_EXP << 8; |
c1d10adb PNA |
1202 | 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 | ||
1217 | nlmsg_failure: | |
1218 | nfattr_failure: | |
1219 | kfree_skb(skb); | |
1220 | return NOTIFY_DONE; | |
1221 | } | |
1222 | #endif | |
1223 | ||
1224 | static int | |
1225 | ctnetlink_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]; | |
87711cb8 PNA |
1230 | struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh); |
1231 | u_int8_t l3proto = nfmsg->nfgen_family; | |
c1d10adb | 1232 | |
c1d10adb PNA |
1233 | read_lock_bh(&nf_conntrack_lock); |
1234 | list_for_each_prev(i, &nf_conntrack_expect_list) { | |
1235 | exp = (struct nf_conntrack_expect *) i; | |
87711cb8 PNA |
1236 | if (l3proto && exp->tuple.src.l3num != l3proto) |
1237 | continue; | |
c1d10adb PNA |
1238 | 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 | } | |
1247 | out: | |
1248 | read_unlock_bh(&nf_conntrack_lock); | |
1249 | ||
c1d10adb PNA |
1250 | return skb->len; |
1251 | } | |
1252 | ||
1253 | static 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 | ||
1258 | static int | |
1259 | ctnetlink_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 | ||
c1d10adb PNA |
1269 | 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 | ||
c1d10adb PNA |
1275 | 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 | ||
468ec44b | 1294 | exp = nf_conntrack_expect_find_get(&tuple); |
c1d10adb PNA |
1295 | if (!exp) |
1296 | return -ENOENT; | |
1297 | ||
1298 | if (cda[CTA_EXPECT_ID-1]) { | |
1299 | u_int32_t id = *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]); | |
1300 | 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; | |
4e9b8269 | 1310 | |
c1d10adb PNA |
1311 | 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 | ||
1321 | free: | |
1322 | kfree_skb(skb2); | |
1323 | out: | |
1324 | nf_conntrack_expect_put(exp); | |
1325 | return err; | |
1326 | } | |
1327 | ||
1328 | static int | |
1329 | ctnetlink_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 */ | |
468ec44b | 1349 | exp = nf_conntrack_expect_find_get(&tuple); |
c1d10adb PNA |
1350 | if (!exp) |
1351 | return -ENOENT; | |
1352 | ||
1353 | if (cda[CTA_EXPECT_ID-1]) { | |
1354 | u_int32_t id = | |
1355 | *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]); | |
1356 | if (exp->id != ntohl(id)) { | |
1357 | nf_conntrack_expect_put(exp); | |
1358 | return -ENOENT; | |
1359 | } | |
1360 | } | |
1361 | ||
1362 | /* after list removal, usage count == 1 */ | |
1363 | nf_conntrack_unexpect_related(exp); | |
1364 | /* have to put what we 'get' above. | |
1365 | * after this line usage count == 0 */ | |
1366 | nf_conntrack_expect_put(exp); | |
1367 | } else if (cda[CTA_EXPECT_HELP_NAME-1]) { | |
1368 | char *name = NFA_DATA(cda[CTA_EXPECT_HELP_NAME-1]); | |
1369 | ||
1370 | /* delete all expectations for this helper */ | |
1371 | write_lock_bh(&nf_conntrack_lock); | |
1372 | h = __nf_conntrack_helper_find_byname(name); | |
1373 | if (!h) { | |
1374 | write_unlock_bh(&nf_conntrack_lock); | |
1375 | return -EINVAL; | |
1376 | } | |
1377 | list_for_each_entry_safe(exp, tmp, &nf_conntrack_expect_list, | |
1378 | list) { | |
dc808fe2 HW |
1379 | struct nf_conn_help *m_help = nfct_help(exp->master); |
1380 | if (m_help->helper == h | |
c1d10adb PNA |
1381 | && del_timer(&exp->timeout)) { |
1382 | nf_ct_unlink_expect(exp); | |
1383 | nf_conntrack_expect_put(exp); | |
1384 | } | |
1385 | } | |
1386 | write_unlock_bh(&nf_conntrack_lock); | |
1387 | } else { | |
1388 | /* This basically means we have to flush everything*/ | |
1389 | write_lock_bh(&nf_conntrack_lock); | |
1390 | list_for_each_entry_safe(exp, tmp, &nf_conntrack_expect_list, | |
1391 | list) { | |
1392 | if (del_timer(&exp->timeout)) { | |
1393 | nf_ct_unlink_expect(exp); | |
1394 | nf_conntrack_expect_put(exp); | |
1395 | } | |
1396 | } | |
1397 | write_unlock_bh(&nf_conntrack_lock); | |
1398 | } | |
1399 | ||
1400 | return 0; | |
1401 | } | |
1402 | static int | |
1403 | ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nfattr *cda[]) | |
1404 | { | |
1405 | return -EOPNOTSUPP; | |
1406 | } | |
1407 | ||
1408 | static int | |
1409 | ctnetlink_create_expect(struct nfattr *cda[], u_int8_t u3) | |
1410 | { | |
1411 | struct nf_conntrack_tuple tuple, mask, master_tuple; | |
1412 | struct nf_conntrack_tuple_hash *h = NULL; | |
1413 | struct nf_conntrack_expect *exp; | |
1414 | struct nf_conn *ct; | |
dc808fe2 | 1415 | struct nf_conn_help *help; |
c1d10adb PNA |
1416 | int err = 0; |
1417 | ||
c1d10adb PNA |
1418 | /* caller guarantees that those three CTA_EXPECT_* exist */ |
1419 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); | |
1420 | if (err < 0) | |
1421 | return err; | |
1422 | err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3); | |
1423 | if (err < 0) | |
1424 | return err; | |
1425 | err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3); | |
1426 | if (err < 0) | |
1427 | return err; | |
1428 | ||
1429 | /* Look for master conntrack of this expectation */ | |
1430 | h = nf_conntrack_find_get(&master_tuple, NULL); | |
1431 | if (!h) | |
1432 | return -ENOENT; | |
1433 | ct = nf_ct_tuplehash_to_ctrack(h); | |
dc808fe2 | 1434 | help = nfct_help(ct); |
c1d10adb | 1435 | |
dc808fe2 | 1436 | if (!help || !help->helper) { |
c1d10adb PNA |
1437 | /* such conntrack hasn't got any helper, abort */ |
1438 | err = -EINVAL; | |
1439 | goto out; | |
1440 | } | |
1441 | ||
1442 | exp = nf_conntrack_expect_alloc(ct); | |
1443 | if (!exp) { | |
1444 | err = -ENOMEM; | |
1445 | goto out; | |
1446 | } | |
1447 | ||
1448 | exp->expectfn = NULL; | |
1449 | exp->flags = 0; | |
1450 | exp->master = ct; | |
1451 | 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 | ||
1457 | out: | |
1458 | nf_ct_put(nf_ct_tuplehash_to_ctrack(h)); | |
1459 | return err; | |
1460 | } | |
1461 | ||
1462 | static int | |
1463 | ctnetlink_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 | ||
c1d10adb PNA |
1472 | 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 | ||
c1d10adb PNA |
1500 | return err; |
1501 | } | |
1502 | ||
1503 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
1504 | static struct notifier_block ctnl_notifier = { | |
1505 | .notifier_call = ctnetlink_conntrack_event, | |
1506 | }; | |
1507 | ||
1508 | static struct notifier_block ctnl_notifier_exp = { | |
1509 | .notifier_call = ctnetlink_expect_event, | |
1510 | }; | |
1511 | #endif | |
1512 | ||
1513 | static 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 | ||
1524 | static 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 | ||
1533 | static 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 | ||
1540 | static 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 | ||
1547 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK); | |
34f9a2e4 | 1548 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP); |
c1d10adb PNA |
1549 | |
1550 | static int __init ctnetlink_init(void) | |
1551 | { | |
1552 | int ret; | |
1553 | ||
1554 | printk("ctnetlink v%s: registering with nfnetlink.\n", version); | |
1555 | ret = nfnetlink_subsys_register(&ctnl_subsys); | |
1556 | if (ret < 0) { | |
1557 | printk("ctnetlink_init: cannot register with nfnetlink.\n"); | |
1558 | goto err_out; | |
1559 | } | |
1560 | ||
1561 | ret = nfnetlink_subsys_register(&ctnl_exp_subsys); | |
1562 | if (ret < 0) { | |
1563 | printk("ctnetlink_init: cannot register exp with nfnetlink.\n"); | |
1564 | goto err_unreg_subsys; | |
1565 | } | |
1566 | ||
1567 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
1568 | ret = nf_conntrack_register_notifier(&ctnl_notifier); | |
1569 | if (ret < 0) { | |
1570 | printk("ctnetlink_init: cannot register notifier.\n"); | |
1571 | goto err_unreg_exp_subsys; | |
1572 | } | |
1573 | ||
1574 | ret = nf_conntrack_expect_register_notifier(&ctnl_notifier_exp); | |
1575 | if (ret < 0) { | |
1576 | printk("ctnetlink_init: cannot expect register notifier.\n"); | |
1577 | goto err_unreg_notifier; | |
1578 | } | |
1579 | #endif | |
1580 | ||
1581 | return 0; | |
1582 | ||
1583 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
1584 | err_unreg_notifier: | |
1585 | nf_conntrack_unregister_notifier(&ctnl_notifier); | |
1586 | err_unreg_exp_subsys: | |
1587 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); | |
1588 | #endif | |
1589 | err_unreg_subsys: | |
1590 | nfnetlink_subsys_unregister(&ctnl_subsys); | |
1591 | err_out: | |
1592 | return ret; | |
1593 | } | |
1594 | ||
1595 | static void __exit ctnetlink_exit(void) | |
1596 | { | |
1597 | printk("ctnetlink: unregistering from nfnetlink.\n"); | |
1598 | ||
1599 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
e64a70be | 1600 | nf_conntrack_expect_unregister_notifier(&ctnl_notifier_exp); |
c1d10adb PNA |
1601 | nf_conntrack_unregister_notifier(&ctnl_notifier); |
1602 | #endif | |
1603 | ||
1604 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); | |
1605 | nfnetlink_subsys_unregister(&ctnl_subsys); | |
1606 | return; | |
1607 | } | |
1608 | ||
1609 | module_init(ctnetlink_init); | |
1610 | module_exit(ctnetlink_exit); |