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_conntrack_seqadj.h>
30 #include <net/netfilter/nf_log.h>
31 #include <net/ip.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 DEFINE_MUTEX(nf_ct_nat_helpers_mutex);
41 static struct list_head nf_ct_nat_helpers __read_mostly;
42
helper_hash(const char * name,u8 protonum)43 static unsigned int helper_hash(const char *name, u8 protonum)
44 {
45 static u32 seed;
46 u32 initval;
47
48 get_random_once(&seed, sizeof(seed));
49
50 initval = seed ^ protonum;
51
52 return jhash(name, strlen(name), initval) % nf_ct_helper_hsize;
53 }
54
55 struct nf_conntrack_helper *
__nf_conntrack_helper_find(const char * name,u16 l3num,u8 protonum)56 __nf_conntrack_helper_find(const char *name, u16 l3num, u8 protonum)
57 {
58 struct nf_conntrack_helper *h;
59 unsigned int i;
60
61 if (!nf_ct_helper_hash)
62 return NULL;
63
64 i = helper_hash(name, protonum);
65
66 hlist_for_each_entry_rcu(h, &nf_ct_helper_hash[i], hnode) {
67 if (strcmp(h->name, name))
68 continue;
69 if (h->nfproto != NFPROTO_UNSPEC && h->nfproto != l3num)
70 continue;
71 if (h->l4proto == protonum)
72 return h;
73 }
74 return NULL;
75 }
76 EXPORT_SYMBOL_GPL(__nf_conntrack_helper_find);
77
78 struct nf_conntrack_helper *
nf_conntrack_helper_try_module_get(const char * name,u16 l3num,u8 protonum)79 nf_conntrack_helper_try_module_get(const char *name, u16 l3num, u8 protonum)
80 {
81 struct nf_conntrack_helper *h;
82
83 rcu_read_lock();
84
85 h = __nf_conntrack_helper_find(name, l3num, protonum);
86 #ifdef CONFIG_MODULES
87 if (h == NULL) {
88 rcu_read_unlock();
89 if (request_module("nfct-helper-%s", name) == 0) {
90 rcu_read_lock();
91 h = __nf_conntrack_helper_find(name, l3num, protonum);
92 } else {
93 return h;
94 }
95 }
96 #endif
97 if (h != NULL && !try_module_get(h->me))
98 h = NULL;
99 if (h != NULL && !refcount_inc_not_zero(&h->ct_refcnt)) {
100 module_put(h->me);
101 h = NULL;
102 }
103
104 rcu_read_unlock();
105
106 return h;
107 }
108 EXPORT_SYMBOL_GPL(nf_conntrack_helper_try_module_get);
109
nf_conntrack_helper_put(struct nf_conntrack_helper * helper)110 void nf_conntrack_helper_put(struct nf_conntrack_helper *helper)
111 {
112 module_put(helper->me);
113 if (refcount_dec_and_test(&helper->ct_refcnt))
114 kfree_rcu(helper, rcu);
115 }
116 EXPORT_SYMBOL_GPL(nf_conntrack_helper_put);
117
118 static struct nf_conntrack_nat_helper *
nf_conntrack_nat_helper_find(const char * mod_name)119 nf_conntrack_nat_helper_find(const char *mod_name)
120 {
121 struct nf_conntrack_nat_helper *cur;
122 bool found = false;
123
124 list_for_each_entry_rcu(cur, &nf_ct_nat_helpers, list) {
125 if (!strcmp(cur->mod_name, mod_name)) {
126 found = true;
127 break;
128 }
129 }
130 return found ? cur : NULL;
131 }
132
133 int
nf_nat_helper_try_module_get(const char * name,u16 l3num,u8 protonum)134 nf_nat_helper_try_module_get(const char *name, u16 l3num, u8 protonum)
135 {
136 struct nf_conntrack_helper *h;
137 struct nf_conntrack_nat_helper *nat;
138 char mod_name[NF_CT_HELPER_NAME_LEN];
139 int ret = 0;
140
141 rcu_read_lock();
142 h = __nf_conntrack_helper_find(name, l3num, protonum);
143 if (!h) {
144 rcu_read_unlock();
145 return -ENOENT;
146 }
147
148 nat = nf_conntrack_nat_helper_find(h->nat_mod_name);
149 if (!nat) {
150 snprintf(mod_name, sizeof(mod_name), "%s", h->nat_mod_name);
151 rcu_read_unlock();
152 request_module("%s", mod_name);
153
154 rcu_read_lock();
155 nat = nf_conntrack_nat_helper_find(mod_name);
156 if (!nat) {
157 rcu_read_unlock();
158 return -ENOENT;
159 }
160 }
161
162 if (!try_module_get(nat->module))
163 ret = -ENOENT;
164
165 rcu_read_unlock();
166 return ret;
167 }
168 EXPORT_SYMBOL_GPL(nf_nat_helper_try_module_get);
169
nf_nat_helper_put(struct nf_conntrack_helper * helper)170 void nf_nat_helper_put(struct nf_conntrack_helper *helper)
171 {
172 struct nf_conntrack_nat_helper *nat;
173
174 nat = nf_conntrack_nat_helper_find(helper->nat_mod_name);
175 if (WARN_ON_ONCE(!nat))
176 return;
177
178 module_put(nat->module);
179 }
180 EXPORT_SYMBOL_GPL(nf_nat_helper_put);
181
182 struct nf_conn_help *
nf_ct_helper_ext_add(struct nf_conn * ct,gfp_t gfp)183 nf_ct_helper_ext_add(struct nf_conn *ct, gfp_t gfp)
184 {
185 struct nf_conn_help *help;
186
187 help = nf_ct_ext_add(ct, NF_CT_EXT_HELPER, gfp);
188 if (help) {
189 __set_bit(IPS_HELPER_BIT, &ct->status);
190 INIT_HLIST_HEAD(&help->expectations);
191 }
192 return help;
193 }
194 EXPORT_SYMBOL_GPL(nf_ct_helper_ext_add);
195
__nf_ct_try_assign_helper(struct nf_conn * ct,struct nf_conn * tmpl,gfp_t flags)196 int __nf_ct_try_assign_helper(struct nf_conn *ct, struct nf_conn *tmpl,
197 gfp_t flags)
198 {
199 struct nf_conntrack_helper *helper = NULL;
200 struct nf_conn_help *help;
201
202 /* We already got a helper explicitly attached (e.g. nft_ct) */
203 if (test_bit(IPS_HELPER_BIT, &ct->status))
204 return 0;
205
206 if (WARN_ON_ONCE(!tmpl))
207 return 0;
208
209 help = nfct_help(tmpl);
210 if (help)
211 helper = rcu_dereference(help->helper);
212
213 help = nfct_help(ct);
214
215 if (helper == NULL) {
216 if (help) {
217 struct nf_conntrack_helper *tmp = rcu_dereference(help->helper);
218
219 RCU_INIT_POINTER(help->helper, NULL);
220 if (tmp && refcount_dec_and_test(&tmp->ct_refcnt))
221 kfree_rcu(tmp, rcu);
222 }
223 return 0;
224 }
225
226 if (help == NULL) {
227 help = nf_ct_helper_ext_add(ct, flags);
228 if (help == NULL)
229 return -ENOMEM;
230 } else {
231 /* We only allow helper re-assignment of the same sort since
232 * we cannot reallocate the helper extension area.
233 */
234 struct nf_conntrack_helper *tmp = rcu_dereference(help->helper);
235
236 if (tmp) {
237 if (tmp->help != helper->help) {
238 RCU_INIT_POINTER(help->helper, NULL);
239 if (refcount_dec_and_test(&tmp->ct_refcnt))
240 kfree_rcu(tmp, rcu);
241 }
242 return 0;
243 }
244 }
245
246 if (refcount_inc_not_zero(&helper->ct_refcnt))
247 rcu_assign_pointer(help->helper, helper);
248
249 return 0;
250 }
251 EXPORT_SYMBOL_GPL(__nf_ct_try_assign_helper);
252
nf_ct_helper_destroy(struct nf_conn * ct)253 void nf_ct_helper_destroy(struct nf_conn *ct)
254 {
255 struct nf_conn_help *help = nfct_help(ct);
256 struct nf_conntrack_helper *helper;
257
258 if (help) {
259 rcu_read_lock();
260 helper = rcu_dereference(help->helper);
261 if (helper && helper->destroy)
262 helper->destroy(ct);
263 rcu_read_unlock();
264 }
265 }
266
267 static LIST_HEAD(nf_ct_helper_expectfn_list);
268
nf_ct_helper_expectfn_register(struct nf_ct_helper_expectfn * n)269 void nf_ct_helper_expectfn_register(struct nf_ct_helper_expectfn *n)
270 {
271 spin_lock_bh(&nf_conntrack_expect_lock);
272 list_add_rcu(&n->head, &nf_ct_helper_expectfn_list);
273 spin_unlock_bh(&nf_conntrack_expect_lock);
274 }
275 EXPORT_SYMBOL_GPL(nf_ct_helper_expectfn_register);
276
nf_ct_helper_expectfn_unregister(struct nf_ct_helper_expectfn * n)277 void nf_ct_helper_expectfn_unregister(struct nf_ct_helper_expectfn *n)
278 {
279 spin_lock_bh(&nf_conntrack_expect_lock);
280 list_del_rcu(&n->head);
281 spin_unlock_bh(&nf_conntrack_expect_lock);
282 }
283 EXPORT_SYMBOL_GPL(nf_ct_helper_expectfn_unregister);
284
expect_iter_expectfn(struct nf_conntrack_expect * exp,void * data)285 static bool expect_iter_expectfn(struct nf_conntrack_expect *exp, void *data)
286 {
287 const struct nf_ct_helper_expectfn *n = data;
288
289 /* Relies on registered expectfn descriptors having unique ->expectfn
290 * pointers, which holds for the in-tree NAT helpers.
291 */
292 return exp->expectfn == n->expectfn;
293 }
294
295 /* Destroy expectations still pointing at @n->expectfn; call after the
296 * caller's RCU grace period so none outlives the (often modular) callback.
297 */
nf_ct_helper_expectfn_destroy(const struct nf_ct_helper_expectfn * n)298 void nf_ct_helper_expectfn_destroy(const struct nf_ct_helper_expectfn *n)
299 {
300 nf_ct_expect_iterate_destroy(expect_iter_expectfn, (void *)n);
301 }
302 EXPORT_SYMBOL_GPL(nf_ct_helper_expectfn_destroy);
303
304 /* Caller should hold the rcu lock */
305 struct nf_ct_helper_expectfn *
nf_ct_helper_expectfn_find_by_name(const char * name)306 nf_ct_helper_expectfn_find_by_name(const char *name)
307 {
308 struct nf_ct_helper_expectfn *cur;
309 bool found = false;
310
311 list_for_each_entry_rcu(cur, &nf_ct_helper_expectfn_list, head) {
312 if (!strcmp(cur->name, name)) {
313 found = true;
314 break;
315 }
316 }
317 return found ? cur : NULL;
318 }
319 EXPORT_SYMBOL_GPL(nf_ct_helper_expectfn_find_by_name);
320
321 /* Caller should hold the rcu lock */
322 struct nf_ct_helper_expectfn *
nf_ct_helper_expectfn_find_by_symbol(const void * symbol)323 nf_ct_helper_expectfn_find_by_symbol(const void *symbol)
324 {
325 struct nf_ct_helper_expectfn *cur;
326 bool found = false;
327
328 list_for_each_entry_rcu(cur, &nf_ct_helper_expectfn_list, head) {
329 if (cur->expectfn == symbol) {
330 found = true;
331 break;
332 }
333 }
334 return found ? cur : NULL;
335 }
336 EXPORT_SYMBOL_GPL(nf_ct_helper_expectfn_find_by_symbol);
337
338 __printf(3, 4)
nf_ct_helper_log(struct sk_buff * skb,const struct nf_conn * ct,const char * fmt,...)339 void nf_ct_helper_log(struct sk_buff *skb, const struct nf_conn *ct,
340 const char *fmt, ...)
341 {
342 const char *helper_name = "(null)";
343 const struct nf_conn_help *help;
344 struct va_format vaf;
345 va_list args;
346
347 va_start(args, fmt);
348
349 vaf.fmt = fmt;
350 vaf.va = &args;
351
352 help = nfct_help(ct);
353 if (help) {
354 const struct nf_conntrack_helper *helper;
355
356 helper = rcu_dereference(help->helper);
357 if (helper)
358 helper_name = helper->name;
359 }
360
361 nf_log_packet(nf_ct_net(ct), nf_ct_l3num(ct), 0, skb, NULL, NULL, NULL,
362 "helper %s dropping packet: %pV ", helper_name, &vaf);
363
364 va_end(args);
365 }
366 EXPORT_SYMBOL_GPL(nf_ct_helper_log);
367
__nf_conntrack_helper_register(struct nf_conntrack_helper * me)368 int __nf_conntrack_helper_register(struct nf_conntrack_helper *me)
369 {
370 struct nf_conntrack_helper *cur;
371 unsigned int h;
372 int ret = 0, i;
373
374 BUG_ON(me->expect_class_max >= NF_CT_MAX_EXPECT_CLASSES);
375 BUG_ON(strlen(me->name) > NF_CT_HELPER_NAME_LEN - 1);
376
377 if (!nf_ct_helper_hash)
378 return -ENOENT;
379
380 for (i = 0; i <= me->expect_class_max; i++) {
381 if (!me->expect_policy[i].max_expected)
382 me->expect_policy[i].max_expected = NF_CT_EXPECT_MAX_CNT;
383
384 if (me->expect_policy[i].max_expected > NF_CT_EXPECT_MAX_CNT)
385 return -EINVAL;
386 }
387
388 h = helper_hash(me->name, me->l4proto);
389 mutex_lock(&nf_ct_helper_mutex);
390 hlist_for_each_entry(cur, &nf_ct_helper_hash[h], hnode) {
391 if (!strcmp(cur->name, me->name) &&
392 (cur->nfproto == NFPROTO_UNSPEC ||
393 cur->nfproto == me->nfproto) &&
394 cur->l4proto == me->l4proto) {
395 ret = -EBUSY;
396 goto out;
397 }
398 }
399
400 refcount_set(&me->ct_refcnt, 1);
401 hlist_add_head_rcu(&me->hnode, &nf_ct_helper_hash[h]);
402 nf_ct_helper_count++;
403 out:
404 mutex_unlock(&nf_ct_helper_mutex);
405 return ret;
406 }
407 EXPORT_SYMBOL_GPL(__nf_conntrack_helper_register);
408
nf_conntrack_helper_register(struct nf_conntrack_helper * me,struct nf_conntrack_helper ** helper_ptr)409 int nf_conntrack_helper_register(struct nf_conntrack_helper *me,
410 struct nf_conntrack_helper **helper_ptr)
411 {
412 struct nf_conntrack_helper *new_helper;
413 int err;
414
415 new_helper = kzalloc_obj(*new_helper, GFP_KERNEL_ACCOUNT);
416 if (!new_helper)
417 return -ENOMEM;
418
419 memcpy(new_helper, me, sizeof(*new_helper));
420 *helper_ptr = new_helper;
421
422 err = __nf_conntrack_helper_register(new_helper);
423 if (err < 0)
424 goto err_helper;
425
426 return 0;
427
428 err_helper:
429 *helper_ptr = NULL;
430 kfree(new_helper);
431
432 return err;
433 }
434 EXPORT_SYMBOL_GPL(nf_conntrack_helper_register);
435
expect_iter_me(struct nf_conntrack_expect * exp,void * data)436 static bool expect_iter_me(struct nf_conntrack_expect *exp, void *data)
437 {
438 const struct nf_conntrack_helper *me = data;
439 const struct nf_conntrack_helper *this;
440
441 this = rcu_dereference_protected(exp->helper,
442 lockdep_is_held(&nf_conntrack_expect_lock));
443 if (this == me)
444 return true;
445
446 this = rcu_dereference_protected(exp->assign_helper,
447 lockdep_is_held(&nf_conntrack_expect_lock));
448 return this == me;
449 }
450
nf_conntrack_helper_release(struct nf_conntrack_helper * me)451 void nf_conntrack_helper_release(struct nf_conntrack_helper *me)
452 {
453 nf_ct_expect_iterate_destroy(expect_iter_me, me);
454
455 if (refcount_dec_and_test(&me->ct_refcnt))
456 kfree_rcu(me, rcu);
457 }
458 EXPORT_SYMBOL_GPL(nf_conntrack_helper_release);
459
nf_conntrack_helper_unregister(struct nf_conntrack_helper * me)460 void nf_conntrack_helper_unregister(struct nf_conntrack_helper *me)
461 {
462 mutex_lock(&nf_ct_helper_mutex);
463 hlist_del_rcu(&me->hnode);
464 nf_ct_helper_count--;
465 mutex_unlock(&nf_ct_helper_mutex);
466
467 /* This helper is going away, disable it. */
468 rcu_assign_pointer(me->help, NULL);
469
470 /* Make sure every nothing is still using the helper unless its a
471 * connection in the hash.
472 */
473 synchronize_rcu();
474
475 nf_conntrack_helper_release(me);
476 }
477 EXPORT_SYMBOL_GPL(nf_conntrack_helper_unregister);
478
nf_ct_helper_init(struct nf_conntrack_helper * helper,u8 l3num,u16 protonum,const char * name,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)479 void nf_ct_helper_init(struct nf_conntrack_helper *helper,
480 u8 l3num, u16 protonum, const char *name,
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 memset(helper, 0, sizeof(*helper));
491
492 helper->nfproto = l3num;
493 helper->l4proto = protonum;
494
495 rcu_assign_pointer(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 snprintf(helper->name, sizeof(helper->name), "%s", name);
502
503 if (WARN_ON_ONCE(expect_class_max >= NF_CT_MAX_EXPECT_CLASSES))
504 return;
505
506 memcpy(helper->expect_policy, exp_pol,
507 (expect_class_max + 1) * sizeof(*exp_pol));
508 helper->expect_class_max = expect_class_max;
509 }
510 EXPORT_SYMBOL_GPL(nf_ct_helper_init);
511
nf_conntrack_helpers_register(struct nf_conntrack_helper * helper,unsigned int n,struct nf_conntrack_helper ** helper_ptr)512 int nf_conntrack_helpers_register(struct nf_conntrack_helper *helper,
513 unsigned int n, struct nf_conntrack_helper **helper_ptr)
514 {
515 struct nf_conntrack_helper *new_helper;
516 unsigned int i;
517 int err = 0;
518
519 for (i = 0; i < n; i++) {
520 new_helper = kzalloc_obj(*new_helper, GFP_KERNEL_ACCOUNT);
521 if (!new_helper) {
522 err = -ENOMEM;
523 goto err;
524 }
525
526 memcpy(new_helper, &helper[i], sizeof(*new_helper));
527 helper_ptr[i] = new_helper;
528
529 err = __nf_conntrack_helper_register(new_helper);
530 if (err < 0) {
531 helper_ptr[i] = NULL;
532 goto err_helper;
533 }
534 }
535
536 return err;
537 err_helper:
538 kfree(new_helper);
539 err:
540 if (i > 0)
541 nf_conntrack_helpers_unregister(helper_ptr, i);
542 return err;
543 }
544 EXPORT_SYMBOL_GPL(nf_conntrack_helpers_register);
545
nf_conntrack_helpers_unregister(struct nf_conntrack_helper ** helper,unsigned int n)546 void nf_conntrack_helpers_unregister(struct nf_conntrack_helper **helper,
547 unsigned int n)
548 {
549 while (n-- > 0) {
550 nf_conntrack_helper_unregister(helper[n]);
551 helper[n] = NULL;
552 }
553 }
554 EXPORT_SYMBOL_GPL(nf_conntrack_helpers_unregister);
555
nf_nat_helper_register(struct nf_conntrack_nat_helper * nat)556 void nf_nat_helper_register(struct nf_conntrack_nat_helper *nat)
557 {
558 mutex_lock(&nf_ct_nat_helpers_mutex);
559 list_add_rcu(&nat->list, &nf_ct_nat_helpers);
560 mutex_unlock(&nf_ct_nat_helpers_mutex);
561 }
562 EXPORT_SYMBOL_GPL(nf_nat_helper_register);
563
nf_nat_helper_unregister(struct nf_conntrack_nat_helper * nat)564 void nf_nat_helper_unregister(struct nf_conntrack_nat_helper *nat)
565 {
566 mutex_lock(&nf_ct_nat_helpers_mutex);
567 list_del_rcu(&nat->list);
568 mutex_unlock(&nf_ct_nat_helpers_mutex);
569 }
570 EXPORT_SYMBOL_GPL(nf_nat_helper_unregister);
571
nf_conntrack_helper_init(void)572 int nf_conntrack_helper_init(void)
573 {
574 nf_ct_helper_hsize = 1; /* gets rounded up to use one page */
575 nf_ct_helper_hash =
576 nf_ct_alloc_hashtable(&nf_ct_helper_hsize, 0);
577 if (!nf_ct_helper_hash)
578 return -ENOMEM;
579
580 INIT_LIST_HEAD(&nf_ct_nat_helpers);
581 return 0;
582 }
583
nf_conntrack_helper_fini(void)584 void nf_conntrack_helper_fini(void)
585 {
586 kvfree(nf_ct_helper_hash);
587 nf_ct_helper_hash = NULL;
588 }
589