netfilter: add API to manage NAT helpers.
[linux-block.git] / net / netfilter / nf_conntrack_helper.c
1 /* Helper handling for netfilter. */
2
3 /* (C) 1999-2001 Paul `Rusty' Russell
4  * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
5  * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org>
6  * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/types.h>
14 #include <linux/netfilter.h>
15 #include <linux/module.h>
16 #include <linux/skbuff.h>
17 #include <linux/vmalloc.h>
18 #include <linux/stddef.h>
19 #include <linux/random.h>
20 #include <linux/err.h>
21 #include <linux/kernel.h>
22 #include <linux/netdevice.h>
23 #include <linux/rculist.h>
24 #include <linux/rtnetlink.h>
25
26 #include <net/netfilter/nf_conntrack.h>
27 #include <net/netfilter/nf_conntrack_l4proto.h>
28 #include <net/netfilter/nf_conntrack_helper.h>
29 #include <net/netfilter/nf_conntrack_core.h>
30 #include <net/netfilter/nf_conntrack_extend.h>
31 #include <net/netfilter/nf_log.h>
32
33 static DEFINE_MUTEX(nf_ct_helper_mutex);
34 struct hlist_head *nf_ct_helper_hash __read_mostly;
35 EXPORT_SYMBOL_GPL(nf_ct_helper_hash);
36 unsigned int nf_ct_helper_hsize __read_mostly;
37 EXPORT_SYMBOL_GPL(nf_ct_helper_hsize);
38 static unsigned int nf_ct_helper_count __read_mostly;
39
40 static bool nf_ct_auto_assign_helper __read_mostly = false;
41 module_param_named(nf_conntrack_helper, nf_ct_auto_assign_helper, bool, 0644);
42 MODULE_PARM_DESC(nf_conntrack_helper,
43                  "Enable automatic conntrack helper assignment (default 0)");
44
45 static DEFINE_MUTEX(nf_ct_nat_helpers_mutex);
46 static struct list_head nf_ct_nat_helpers __read_mostly;
47
48 /* Stupid hash, but collision free for the default registrations of the
49  * helpers currently in the kernel. */
50 static unsigned int helper_hash(const struct nf_conntrack_tuple *tuple)
51 {
52         return (((tuple->src.l3num << 8) | tuple->dst.protonum) ^
53                 (__force __u16)tuple->src.u.all) % nf_ct_helper_hsize;
54 }
55
56 static struct nf_conntrack_helper *
57 __nf_ct_helper_find(const struct nf_conntrack_tuple *tuple)
58 {
59         struct nf_conntrack_helper *helper;
60         struct nf_conntrack_tuple_mask mask = { .src.u.all = htons(0xFFFF) };
61         unsigned int h;
62
63         if (!nf_ct_helper_count)
64                 return NULL;
65
66         h = helper_hash(tuple);
67         hlist_for_each_entry_rcu(helper, &nf_ct_helper_hash[h], hnode) {
68                 if (nf_ct_tuple_src_mask_cmp(tuple, &helper->tuple, &mask))
69                         return helper;
70         }
71         return NULL;
72 }
73
74 struct nf_conntrack_helper *
75 __nf_conntrack_helper_find(const char *name, u16 l3num, u8 protonum)
76 {
77         struct nf_conntrack_helper *h;
78         unsigned int i;
79
80         for (i = 0; i < nf_ct_helper_hsize; i++) {
81                 hlist_for_each_entry_rcu(h, &nf_ct_helper_hash[i], hnode) {
82                         if (strcmp(h->name, name))
83                                 continue;
84
85                         if (h->tuple.src.l3num != NFPROTO_UNSPEC &&
86                             h->tuple.src.l3num != l3num)
87                                 continue;
88
89                         if (h->tuple.dst.protonum == protonum)
90                                 return h;
91                 }
92         }
93         return NULL;
94 }
95 EXPORT_SYMBOL_GPL(__nf_conntrack_helper_find);
96
97 struct nf_conntrack_helper *
98 nf_conntrack_helper_try_module_get(const char *name, u16 l3num, u8 protonum)
99 {
100         struct nf_conntrack_helper *h;
101
102         rcu_read_lock();
103
104         h = __nf_conntrack_helper_find(name, l3num, protonum);
105 #ifdef CONFIG_MODULES
106         if (h == NULL) {
107                 rcu_read_unlock();
108                 if (request_module("nfct-helper-%s", name) == 0) {
109                         rcu_read_lock();
110                         h = __nf_conntrack_helper_find(name, l3num, protonum);
111                 } else {
112                         return h;
113                 }
114         }
115 #endif
116         if (h != NULL && !try_module_get(h->me))
117                 h = NULL;
118         if (h != NULL && !refcount_inc_not_zero(&h->refcnt)) {
119                 module_put(h->me);
120                 h = NULL;
121         }
122
123         rcu_read_unlock();
124
125         return h;
126 }
127 EXPORT_SYMBOL_GPL(nf_conntrack_helper_try_module_get);
128
129 void nf_conntrack_helper_put(struct nf_conntrack_helper *helper)
130 {
131         refcount_dec(&helper->refcnt);
132         module_put(helper->me);
133 }
134 EXPORT_SYMBOL_GPL(nf_conntrack_helper_put);
135
136 static struct nf_conntrack_nat_helper *
137 nf_conntrack_nat_helper_find(const char *mod_name)
138 {
139         struct nf_conntrack_nat_helper *cur;
140         bool found = false;
141
142         list_for_each_entry_rcu(cur, &nf_ct_nat_helpers, list) {
143                 if (!strcmp(cur->mod_name, mod_name)) {
144                         found = true;
145                         break;
146                 }
147         }
148         return found ? cur : NULL;
149 }
150
151 int
152 nf_nat_helper_try_module_get(const char *name, u16 l3num, u8 protonum)
153 {
154         struct nf_conntrack_helper *h;
155         struct nf_conntrack_nat_helper *nat;
156         char mod_name[NF_CT_HELPER_NAME_LEN];
157         int ret = 0;
158
159         rcu_read_lock();
160         h = __nf_conntrack_helper_find(name, l3num, protonum);
161         if (!h) {
162                 rcu_read_unlock();
163                 return -ENOENT;
164         }
165
166         nat = nf_conntrack_nat_helper_find(h->nat_mod_name);
167         if (!nat) {
168                 snprintf(mod_name, sizeof(mod_name), "%s", h->nat_mod_name);
169                 rcu_read_unlock();
170                 request_module(mod_name);
171
172                 rcu_read_lock();
173                 nat = nf_conntrack_nat_helper_find(mod_name);
174                 if (!nat) {
175                         rcu_read_unlock();
176                         return -ENOENT;
177                 }
178         }
179
180         if (!try_module_get(nat->module))
181                 ret = -ENOENT;
182
183         rcu_read_unlock();
184         return ret;
185 }
186 EXPORT_SYMBOL_GPL(nf_nat_helper_try_module_get);
187
188 void nf_nat_helper_put(struct nf_conntrack_helper *helper)
189 {
190         struct nf_conntrack_nat_helper *nat;
191
192         nat = nf_conntrack_nat_helper_find(helper->nat_mod_name);
193         if (WARN_ON_ONCE(!nat))
194                 return;
195
196         module_put(nat->module);
197 }
198 EXPORT_SYMBOL_GPL(nf_nat_helper_put);
199
200 struct nf_conn_help *
201 nf_ct_helper_ext_add(struct nf_conn *ct, gfp_t gfp)
202 {
203         struct nf_conn_help *help;
204
205         help = nf_ct_ext_add(ct, NF_CT_EXT_HELPER, gfp);
206         if (help)
207                 INIT_HLIST_HEAD(&help->expectations);
208         else
209                 pr_debug("failed to add helper extension area");
210         return help;
211 }
212 EXPORT_SYMBOL_GPL(nf_ct_helper_ext_add);
213
214 static struct nf_conntrack_helper *
215 nf_ct_lookup_helper(struct nf_conn *ct, struct net *net)
216 {
217         if (!net->ct.sysctl_auto_assign_helper) {
218                 if (net->ct.auto_assign_helper_warned)
219                         return NULL;
220                 if (!__nf_ct_helper_find(&ct->tuplehash[IP_CT_DIR_REPLY].tuple))
221                         return NULL;
222                 pr_info("nf_conntrack: default automatic helper assignment "
223                         "has been turned off for security reasons and CT-based "
224                         " firewall rule not found. Use the iptables CT target "
225                         "to attach helpers instead.\n");
226                 net->ct.auto_assign_helper_warned = 1;
227                 return NULL;
228         }
229
230         return __nf_ct_helper_find(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
231 }
232
233
234 int __nf_ct_try_assign_helper(struct nf_conn *ct, struct nf_conn *tmpl,
235                               gfp_t flags)
236 {
237         struct nf_conntrack_helper *helper = NULL;
238         struct nf_conn_help *help;
239         struct net *net = nf_ct_net(ct);
240
241         /* We already got a helper explicitly attached. The function
242          * nf_conntrack_alter_reply - in case NAT is in use - asks for looking
243          * the helper up again. Since now the user is in full control of
244          * making consistent helper configurations, skip this automatic
245          * re-lookup, otherwise we'll lose the helper.
246          */
247         if (test_bit(IPS_HELPER_BIT, &ct->status))
248                 return 0;
249
250         if (tmpl != NULL) {
251                 help = nfct_help(tmpl);
252                 if (help != NULL) {
253                         helper = help->helper;
254                         set_bit(IPS_HELPER_BIT, &ct->status);
255                 }
256         }
257
258         help = nfct_help(ct);
259
260         if (helper == NULL) {
261                 helper = nf_ct_lookup_helper(ct, net);
262                 if (helper == NULL) {
263                         if (help)
264                                 RCU_INIT_POINTER(help->helper, NULL);
265                         return 0;
266                 }
267         }
268
269         if (help == NULL) {
270                 help = nf_ct_helper_ext_add(ct, flags);
271                 if (help == NULL)
272                         return -ENOMEM;
273         } else {
274                 /* We only allow helper re-assignment of the same sort since
275                  * we cannot reallocate the helper extension area.
276                  */
277                 struct nf_conntrack_helper *tmp = rcu_dereference(help->helper);
278
279                 if (tmp && tmp->help != helper->help) {
280                         RCU_INIT_POINTER(help->helper, NULL);
281                         return 0;
282                 }
283         }
284
285         rcu_assign_pointer(help->helper, helper);
286
287         return 0;
288 }
289 EXPORT_SYMBOL_GPL(__nf_ct_try_assign_helper);
290
291 /* appropriate ct lock protecting must be taken by caller */
292 static int unhelp(struct nf_conn *ct, void *me)
293 {
294         struct nf_conn_help *help = nfct_help(ct);
295
296         if (help && rcu_dereference_raw(help->helper) == me) {
297                 nf_conntrack_event(IPCT_HELPER, ct);
298                 RCU_INIT_POINTER(help->helper, NULL);
299         }
300
301         /* We are not intended to delete this conntrack. */
302         return 0;
303 }
304
305 void nf_ct_helper_destroy(struct nf_conn *ct)
306 {
307         struct nf_conn_help *help = nfct_help(ct);
308         struct nf_conntrack_helper *helper;
309
310         if (help) {
311                 rcu_read_lock();
312                 helper = rcu_dereference(help->helper);
313                 if (helper && helper->destroy)
314                         helper->destroy(ct);
315                 rcu_read_unlock();
316         }
317 }
318
319 static LIST_HEAD(nf_ct_helper_expectfn_list);
320
321 void nf_ct_helper_expectfn_register(struct nf_ct_helper_expectfn *n)
322 {
323         spin_lock_bh(&nf_conntrack_expect_lock);
324         list_add_rcu(&n->head, &nf_ct_helper_expectfn_list);
325         spin_unlock_bh(&nf_conntrack_expect_lock);
326 }
327 EXPORT_SYMBOL_GPL(nf_ct_helper_expectfn_register);
328
329 void nf_ct_helper_expectfn_unregister(struct nf_ct_helper_expectfn *n)
330 {
331         spin_lock_bh(&nf_conntrack_expect_lock);
332         list_del_rcu(&n->head);
333         spin_unlock_bh(&nf_conntrack_expect_lock);
334 }
335 EXPORT_SYMBOL_GPL(nf_ct_helper_expectfn_unregister);
336
337 /* Caller should hold the rcu lock */
338 struct nf_ct_helper_expectfn *
339 nf_ct_helper_expectfn_find_by_name(const char *name)
340 {
341         struct nf_ct_helper_expectfn *cur;
342         bool found = false;
343
344         list_for_each_entry_rcu(cur, &nf_ct_helper_expectfn_list, head) {
345                 if (!strcmp(cur->name, name)) {
346                         found = true;
347                         break;
348                 }
349         }
350         return found ? cur : NULL;
351 }
352 EXPORT_SYMBOL_GPL(nf_ct_helper_expectfn_find_by_name);
353
354 /* Caller should hold the rcu lock */
355 struct nf_ct_helper_expectfn *
356 nf_ct_helper_expectfn_find_by_symbol(const void *symbol)
357 {
358         struct nf_ct_helper_expectfn *cur;
359         bool found = false;
360
361         list_for_each_entry_rcu(cur, &nf_ct_helper_expectfn_list, head) {
362                 if (cur->expectfn == symbol) {
363                         found = true;
364                         break;
365                 }
366         }
367         return found ? cur : NULL;
368 }
369 EXPORT_SYMBOL_GPL(nf_ct_helper_expectfn_find_by_symbol);
370
371 __printf(3, 4)
372 void nf_ct_helper_log(struct sk_buff *skb, const struct nf_conn *ct,
373                       const char *fmt, ...)
374 {
375         const struct nf_conn_help *help;
376         const struct nf_conntrack_helper *helper;
377         struct va_format vaf;
378         va_list args;
379
380         va_start(args, fmt);
381
382         vaf.fmt = fmt;
383         vaf.va = &args;
384
385         /* Called from the helper function, this call never fails */
386         help = nfct_help(ct);
387
388         /* rcu_read_lock()ed by nf_hook_thresh */
389         helper = rcu_dereference(help->helper);
390
391         nf_log_packet(nf_ct_net(ct), nf_ct_l3num(ct), 0, skb, NULL, NULL, NULL,
392                       "nf_ct_%s: dropping packet: %pV ", helper->name, &vaf);
393
394         va_end(args);
395 }
396 EXPORT_SYMBOL_GPL(nf_ct_helper_log);
397
398 int nf_conntrack_helper_register(struct nf_conntrack_helper *me)
399 {
400         struct nf_conntrack_tuple_mask mask = { .src.u.all = htons(0xFFFF) };
401         unsigned int h = helper_hash(&me->tuple);
402         struct nf_conntrack_helper *cur;
403         int ret = 0, i;
404
405         BUG_ON(me->expect_policy == NULL);
406         BUG_ON(me->expect_class_max >= NF_CT_MAX_EXPECT_CLASSES);
407         BUG_ON(strlen(me->name) > NF_CT_HELPER_NAME_LEN - 1);
408
409         if (me->expect_policy->max_expected > NF_CT_EXPECT_MAX_CNT)
410                 return -EINVAL;
411
412         mutex_lock(&nf_ct_helper_mutex);
413         for (i = 0; i < nf_ct_helper_hsize; i++) {
414                 hlist_for_each_entry(cur, &nf_ct_helper_hash[i], hnode) {
415                         if (!strcmp(cur->name, me->name) &&
416                             (cur->tuple.src.l3num == NFPROTO_UNSPEC ||
417                              cur->tuple.src.l3num == me->tuple.src.l3num) &&
418                             cur->tuple.dst.protonum == me->tuple.dst.protonum) {
419                                 ret = -EEXIST;
420                                 goto out;
421                         }
422                 }
423         }
424
425         /* avoid unpredictable behaviour for auto_assign_helper */
426         if (!(me->flags & NF_CT_HELPER_F_USERSPACE)) {
427                 hlist_for_each_entry(cur, &nf_ct_helper_hash[h], hnode) {
428                         if (nf_ct_tuple_src_mask_cmp(&cur->tuple, &me->tuple,
429                                                      &mask)) {
430                                 ret = -EEXIST;
431                                 goto out;
432                         }
433                 }
434         }
435         refcount_set(&me->refcnt, 1);
436         hlist_add_head_rcu(&me->hnode, &nf_ct_helper_hash[h]);
437         nf_ct_helper_count++;
438 out:
439         mutex_unlock(&nf_ct_helper_mutex);
440         return ret;
441 }
442 EXPORT_SYMBOL_GPL(nf_conntrack_helper_register);
443
444 static bool expect_iter_me(struct nf_conntrack_expect *exp, void *data)
445 {
446         struct nf_conn_help *help = nfct_help(exp->master);
447         const struct nf_conntrack_helper *me = data;
448         const struct nf_conntrack_helper *this;
449
450         if (exp->helper == me)
451                 return true;
452
453         this = rcu_dereference_protected(help->helper,
454                                          lockdep_is_held(&nf_conntrack_expect_lock));
455         return this == me;
456 }
457
458 void nf_conntrack_helper_unregister(struct nf_conntrack_helper *me)
459 {
460         mutex_lock(&nf_ct_helper_mutex);
461         hlist_del_rcu(&me->hnode);
462         nf_ct_helper_count--;
463         mutex_unlock(&nf_ct_helper_mutex);
464
465         /* Make sure every nothing is still using the helper unless its a
466          * connection in the hash.
467          */
468         synchronize_rcu();
469
470         nf_ct_expect_iterate_destroy(expect_iter_me, NULL);
471         nf_ct_iterate_destroy(unhelp, me);
472
473         /* Maybe someone has gotten the helper already when unhelp above.
474          * So need to wait it.
475          */
476         synchronize_rcu();
477 }
478 EXPORT_SYMBOL_GPL(nf_conntrack_helper_unregister);
479
480 void nf_ct_helper_init(struct nf_conntrack_helper *helper,
481                        u16 l3num, u16 protonum, const char *name,
482                        u16 default_port, u16 spec_port, u32 id,
483                        const struct nf_conntrack_expect_policy *exp_pol,
484                        u32 expect_class_max,
485                        int (*help)(struct sk_buff *skb, unsigned int protoff,
486                                    struct nf_conn *ct,
487                                    enum ip_conntrack_info ctinfo),
488                        int (*from_nlattr)(struct nlattr *attr,
489                                           struct nf_conn *ct),
490                        struct module *module)
491 {
492         helper->tuple.src.l3num = l3num;
493         helper->tuple.dst.protonum = protonum;
494         helper->tuple.src.u.all = htons(spec_port);
495         helper->expect_policy = exp_pol;
496         helper->expect_class_max = expect_class_max;
497         helper->help = help;
498         helper->from_nlattr = from_nlattr;
499         helper->me = module;
500         snprintf(helper->nat_mod_name, sizeof(helper->nat_mod_name),
501                  NF_NAT_HELPER_PREFIX "%s", name);
502
503         if (spec_port == default_port)
504                 snprintf(helper->name, sizeof(helper->name), "%s", name);
505         else
506                 snprintf(helper->name, sizeof(helper->name), "%s-%u", name, id);
507 }
508 EXPORT_SYMBOL_GPL(nf_ct_helper_init);
509
510 int nf_conntrack_helpers_register(struct nf_conntrack_helper *helper,
511                                   unsigned int n)
512 {
513         unsigned int i;
514         int err = 0;
515
516         for (i = 0; i < n; i++) {
517                 err = nf_conntrack_helper_register(&helper[i]);
518                 if (err < 0)
519                         goto err;
520         }
521
522         return err;
523 err:
524         if (i > 0)
525                 nf_conntrack_helpers_unregister(helper, i);
526         return err;
527 }
528 EXPORT_SYMBOL_GPL(nf_conntrack_helpers_register);
529
530 void nf_conntrack_helpers_unregister(struct nf_conntrack_helper *helper,
531                                 unsigned int n)
532 {
533         while (n-- > 0)
534                 nf_conntrack_helper_unregister(&helper[n]);
535 }
536 EXPORT_SYMBOL_GPL(nf_conntrack_helpers_unregister);
537
538 void nf_nat_helper_register(struct nf_conntrack_nat_helper *nat)
539 {
540         mutex_lock(&nf_ct_nat_helpers_mutex);
541         list_add_rcu(&nat->list, &nf_ct_nat_helpers);
542         mutex_unlock(&nf_ct_nat_helpers_mutex);
543 }
544 EXPORT_SYMBOL_GPL(nf_nat_helper_register);
545
546 void nf_nat_helper_unregister(struct nf_conntrack_nat_helper *nat)
547 {
548         mutex_lock(&nf_ct_nat_helpers_mutex);
549         list_del_rcu(&nat->list);
550         mutex_unlock(&nf_ct_nat_helpers_mutex);
551 }
552 EXPORT_SYMBOL_GPL(nf_nat_helper_unregister);
553
554 static const struct nf_ct_ext_type helper_extend = {
555         .len    = sizeof(struct nf_conn_help),
556         .align  = __alignof__(struct nf_conn_help),
557         .id     = NF_CT_EXT_HELPER,
558 };
559
560 void nf_conntrack_helper_pernet_init(struct net *net)
561 {
562         net->ct.auto_assign_helper_warned = false;
563         net->ct.sysctl_auto_assign_helper = nf_ct_auto_assign_helper;
564 }
565
566 int nf_conntrack_helper_init(void)
567 {
568         int ret;
569         nf_ct_helper_hsize = 1; /* gets rounded up to use one page */
570         nf_ct_helper_hash =
571                 nf_ct_alloc_hashtable(&nf_ct_helper_hsize, 0);
572         if (!nf_ct_helper_hash)
573                 return -ENOMEM;
574
575         ret = nf_ct_extend_register(&helper_extend);
576         if (ret < 0) {
577                 pr_err("nf_ct_helper: Unable to register helper extension.\n");
578                 goto out_extend;
579         }
580
581         INIT_LIST_HEAD(&nf_ct_nat_helpers);
582         return 0;
583 out_extend:
584         kvfree(nf_ct_helper_hash);
585         return ret;
586 }
587
588 void nf_conntrack_helper_fini(void)
589 {
590         nf_ct_extend_unregister(&helper_extend);
591         kvfree(nf_ct_helper_hash);
592 }