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