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