nf_conntrack_proto.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /* L3/L4 protocol support for nf_conntrack. */
  2. /* (C) 1999-2001 Paul `Rusty' Russell
  3. * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
  4. * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org>
  5. * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/types.h>
  12. #include <linux/netfilter.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/mutex.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/stddef.h>
  18. #include <linux/err.h>
  19. #include <linux/percpu.h>
  20. #include <linux/notifier.h>
  21. #include <linux/kernel.h>
  22. #include <linux/netdevice.h>
  23. #include <net/netfilter/nf_conntrack.h>
  24. #include <net/netfilter/nf_conntrack_l3proto.h>
  25. #include <net/netfilter/nf_conntrack_l4proto.h>
  26. #include <net/netfilter/nf_conntrack_core.h>
  27. static struct nf_conntrack_l4proto __rcu **nf_ct_protos[PF_MAX] __read_mostly;
  28. struct nf_conntrack_l3proto __rcu *nf_ct_l3protos[AF_MAX] __read_mostly;
  29. EXPORT_SYMBOL_GPL(nf_ct_l3protos);
  30. static DEFINE_MUTEX(nf_ct_proto_mutex);
  31. #ifdef CONFIG_SYSCTL
  32. static int
  33. nf_ct_register_sysctl(struct net *net,
  34. struct ctl_table_header **header,
  35. const char *path,
  36. struct ctl_table *table)
  37. {
  38. if (*header == NULL) {
  39. *header = register_net_sysctl(net, path, table);
  40. if (*header == NULL)
  41. return -ENOMEM;
  42. }
  43. return 0;
  44. }
  45. static void
  46. nf_ct_unregister_sysctl(struct ctl_table_header **header,
  47. struct ctl_table **table,
  48. unsigned int users)
  49. {
  50. if (users > 0)
  51. return;
  52. unregister_net_sysctl_table(*header);
  53. kfree(*table);
  54. *header = NULL;
  55. *table = NULL;
  56. }
  57. #endif
  58. struct nf_conntrack_l4proto *
  59. __nf_ct_l4proto_find(u_int16_t l3proto, u_int8_t l4proto)
  60. {
  61. if (unlikely(l3proto >= AF_MAX || nf_ct_protos[l3proto] == NULL))
  62. return &nf_conntrack_l4proto_generic;
  63. return rcu_dereference(nf_ct_protos[l3proto][l4proto]);
  64. }
  65. EXPORT_SYMBOL_GPL(__nf_ct_l4proto_find);
  66. /* this is guaranteed to always return a valid protocol helper, since
  67. * it falls back to generic_protocol */
  68. struct nf_conntrack_l3proto *
  69. nf_ct_l3proto_find_get(u_int16_t l3proto)
  70. {
  71. struct nf_conntrack_l3proto *p;
  72. rcu_read_lock();
  73. p = __nf_ct_l3proto_find(l3proto);
  74. if (!try_module_get(p->me))
  75. p = &nf_conntrack_l3proto_generic;
  76. rcu_read_unlock();
  77. return p;
  78. }
  79. EXPORT_SYMBOL_GPL(nf_ct_l3proto_find_get);
  80. int
  81. nf_ct_l3proto_try_module_get(unsigned short l3proto)
  82. {
  83. int ret;
  84. struct nf_conntrack_l3proto *p;
  85. retry: p = nf_ct_l3proto_find_get(l3proto);
  86. if (p == &nf_conntrack_l3proto_generic) {
  87. ret = request_module("nf_conntrack-%d", l3proto);
  88. if (!ret)
  89. goto retry;
  90. return -EPROTOTYPE;
  91. }
  92. return 0;
  93. }
  94. EXPORT_SYMBOL_GPL(nf_ct_l3proto_try_module_get);
  95. void nf_ct_l3proto_module_put(unsigned short l3proto)
  96. {
  97. struct nf_conntrack_l3proto *p;
  98. /* rcu_read_lock not necessary since the caller holds a reference, but
  99. * taken anyways to avoid lockdep warnings in __nf_ct_l3proto_find()
  100. */
  101. rcu_read_lock();
  102. p = __nf_ct_l3proto_find(l3proto);
  103. module_put(p->me);
  104. rcu_read_unlock();
  105. }
  106. EXPORT_SYMBOL_GPL(nf_ct_l3proto_module_put);
  107. struct nf_conntrack_l4proto *
  108. nf_ct_l4proto_find_get(u_int16_t l3num, u_int8_t l4num)
  109. {
  110. struct nf_conntrack_l4proto *p;
  111. rcu_read_lock();
  112. p = __nf_ct_l4proto_find(l3num, l4num);
  113. if (!try_module_get(p->me))
  114. p = &nf_conntrack_l4proto_generic;
  115. rcu_read_unlock();
  116. return p;
  117. }
  118. EXPORT_SYMBOL_GPL(nf_ct_l4proto_find_get);
  119. void nf_ct_l4proto_put(struct nf_conntrack_l4proto *p)
  120. {
  121. module_put(p->me);
  122. }
  123. EXPORT_SYMBOL_GPL(nf_ct_l4proto_put);
  124. static int kill_l3proto(struct nf_conn *i, void *data)
  125. {
  126. return nf_ct_l3num(i) == ((struct nf_conntrack_l3proto *)data)->l3proto;
  127. }
  128. static int kill_l4proto(struct nf_conn *i, void *data)
  129. {
  130. struct nf_conntrack_l4proto *l4proto;
  131. l4proto = (struct nf_conntrack_l4proto *)data;
  132. return nf_ct_protonum(i) == l4proto->l4proto &&
  133. nf_ct_l3num(i) == l4proto->l3proto;
  134. }
  135. static struct nf_ip_net *nf_ct_l3proto_net(struct net *net,
  136. struct nf_conntrack_l3proto *l3proto)
  137. {
  138. if (l3proto->l3proto == PF_INET)
  139. return &net->ct.nf_ct_proto;
  140. else
  141. return NULL;
  142. }
  143. static int nf_ct_l3proto_register_sysctl(struct net *net,
  144. struct nf_conntrack_l3proto *l3proto)
  145. {
  146. int err = 0;
  147. struct nf_ip_net *in = nf_ct_l3proto_net(net, l3proto);
  148. /* nf_conntrack_l3proto_ipv6 doesn't support sysctl */
  149. if (in == NULL)
  150. return 0;
  151. #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
  152. if (in->ctl_table != NULL) {
  153. err = nf_ct_register_sysctl(net,
  154. &in->ctl_table_header,
  155. l3proto->ctl_table_path,
  156. in->ctl_table);
  157. if (err < 0) {
  158. kfree(in->ctl_table);
  159. in->ctl_table = NULL;
  160. }
  161. }
  162. #endif
  163. return err;
  164. }
  165. static void nf_ct_l3proto_unregister_sysctl(struct net *net,
  166. struct nf_conntrack_l3proto *l3proto)
  167. {
  168. struct nf_ip_net *in = nf_ct_l3proto_net(net, l3proto);
  169. if (in == NULL)
  170. return;
  171. #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
  172. if (in->ctl_table_header != NULL)
  173. nf_ct_unregister_sysctl(&in->ctl_table_header,
  174. &in->ctl_table,
  175. 0);
  176. #endif
  177. }
  178. int nf_ct_l3proto_register(struct nf_conntrack_l3proto *proto)
  179. {
  180. int ret = 0;
  181. struct nf_conntrack_l3proto *old;
  182. if (proto->l3proto >= AF_MAX)
  183. return -EBUSY;
  184. if (proto->tuple_to_nlattr && !proto->nlattr_tuple_size)
  185. return -EINVAL;
  186. mutex_lock(&nf_ct_proto_mutex);
  187. old = rcu_dereference_protected(nf_ct_l3protos[proto->l3proto],
  188. lockdep_is_held(&nf_ct_proto_mutex));
  189. if (old != &nf_conntrack_l3proto_generic) {
  190. ret = -EBUSY;
  191. goto out_unlock;
  192. }
  193. if (proto->nlattr_tuple_size)
  194. proto->nla_size = 3 * proto->nlattr_tuple_size();
  195. rcu_assign_pointer(nf_ct_l3protos[proto->l3proto], proto);
  196. out_unlock:
  197. mutex_unlock(&nf_ct_proto_mutex);
  198. return ret;
  199. }
  200. EXPORT_SYMBOL_GPL(nf_ct_l3proto_register);
  201. int nf_ct_l3proto_pernet_register(struct net *net,
  202. struct nf_conntrack_l3proto *proto)
  203. {
  204. int ret = 0;
  205. if (proto->init_net) {
  206. ret = proto->init_net(net);
  207. if (ret < 0)
  208. return ret;
  209. }
  210. return nf_ct_l3proto_register_sysctl(net, proto);
  211. }
  212. EXPORT_SYMBOL_GPL(nf_ct_l3proto_pernet_register);
  213. void nf_ct_l3proto_unregister(struct nf_conntrack_l3proto *proto)
  214. {
  215. BUG_ON(proto->l3proto >= AF_MAX);
  216. mutex_lock(&nf_ct_proto_mutex);
  217. BUG_ON(rcu_dereference_protected(nf_ct_l3protos[proto->l3proto],
  218. lockdep_is_held(&nf_ct_proto_mutex)
  219. ) != proto);
  220. rcu_assign_pointer(nf_ct_l3protos[proto->l3proto],
  221. &nf_conntrack_l3proto_generic);
  222. mutex_unlock(&nf_ct_proto_mutex);
  223. synchronize_rcu();
  224. }
  225. EXPORT_SYMBOL_GPL(nf_ct_l3proto_unregister);
  226. void nf_ct_l3proto_pernet_unregister(struct net *net,
  227. struct nf_conntrack_l3proto *proto)
  228. {
  229. nf_ct_l3proto_unregister_sysctl(net, proto);
  230. /* Remove all contrack entries for this protocol */
  231. nf_ct_iterate_cleanup(net, kill_l3proto, proto, 0, 0);
  232. }
  233. EXPORT_SYMBOL_GPL(nf_ct_l3proto_pernet_unregister);
  234. static struct nf_proto_net *nf_ct_l4proto_net(struct net *net,
  235. struct nf_conntrack_l4proto *l4proto)
  236. {
  237. if (l4proto->get_net_proto) {
  238. /* statically built-in protocols use static per-net */
  239. return l4proto->get_net_proto(net);
  240. } else if (l4proto->net_id) {
  241. /* ... and loadable protocols use dynamic per-net */
  242. return net_generic(net, *l4proto->net_id);
  243. }
  244. return NULL;
  245. }
  246. static
  247. int nf_ct_l4proto_register_sysctl(struct net *net,
  248. struct nf_proto_net *pn,
  249. struct nf_conntrack_l4proto *l4proto)
  250. {
  251. int err = 0;
  252. #ifdef CONFIG_SYSCTL
  253. if (pn->ctl_table != NULL) {
  254. err = nf_ct_register_sysctl(net,
  255. &pn->ctl_table_header,
  256. "net/netfilter",
  257. pn->ctl_table);
  258. if (err < 0) {
  259. if (!pn->users) {
  260. kfree(pn->ctl_table);
  261. pn->ctl_table = NULL;
  262. }
  263. }
  264. }
  265. #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
  266. if (l4proto->l3proto != AF_INET6 && pn->ctl_compat_table != NULL) {
  267. if (err < 0) {
  268. nf_ct_kfree_compat_sysctl_table(pn);
  269. goto out;
  270. }
  271. err = nf_ct_register_sysctl(net,
  272. &pn->ctl_compat_header,
  273. "net/ipv4/netfilter",
  274. pn->ctl_compat_table);
  275. if (err == 0)
  276. goto out;
  277. nf_ct_kfree_compat_sysctl_table(pn);
  278. nf_ct_unregister_sysctl(&pn->ctl_table_header,
  279. &pn->ctl_table,
  280. pn->users);
  281. }
  282. out:
  283. #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
  284. #endif /* CONFIG_SYSCTL */
  285. return err;
  286. }
  287. static
  288. void nf_ct_l4proto_unregister_sysctl(struct net *net,
  289. struct nf_proto_net *pn,
  290. struct nf_conntrack_l4proto *l4proto)
  291. {
  292. #ifdef CONFIG_SYSCTL
  293. if (pn->ctl_table_header != NULL)
  294. nf_ct_unregister_sysctl(&pn->ctl_table_header,
  295. &pn->ctl_table,
  296. pn->users);
  297. #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
  298. if (l4proto->l3proto != AF_INET6 && pn->ctl_compat_header != NULL)
  299. nf_ct_unregister_sysctl(&pn->ctl_compat_header,
  300. &pn->ctl_compat_table,
  301. 0);
  302. #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
  303. #endif /* CONFIG_SYSCTL */
  304. }
  305. /* FIXME: Allow NULL functions and sub in pointers to generic for
  306. them. --RR */
  307. int nf_ct_l4proto_register(struct nf_conntrack_l4proto *l4proto)
  308. {
  309. int ret = 0;
  310. if (l4proto->l3proto >= PF_MAX)
  311. return -EBUSY;
  312. if ((l4proto->to_nlattr && !l4proto->nlattr_size)
  313. || (l4proto->tuple_to_nlattr && !l4proto->nlattr_tuple_size))
  314. return -EINVAL;
  315. mutex_lock(&nf_ct_proto_mutex);
  316. if (!nf_ct_protos[l4proto->l3proto]) {
  317. /* l3proto may be loaded latter. */
  318. struct nf_conntrack_l4proto __rcu **proto_array;
  319. int i;
  320. proto_array = kmalloc(MAX_NF_CT_PROTO *
  321. sizeof(struct nf_conntrack_l4proto *),
  322. GFP_KERNEL);
  323. if (proto_array == NULL) {
  324. ret = -ENOMEM;
  325. goto out_unlock;
  326. }
  327. for (i = 0; i < MAX_NF_CT_PROTO; i++)
  328. RCU_INIT_POINTER(proto_array[i], &nf_conntrack_l4proto_generic);
  329. /* Before making proto_array visible to lockless readers,
  330. * we must make sure its content is committed to memory.
  331. */
  332. smp_wmb();
  333. nf_ct_protos[l4proto->l3proto] = proto_array;
  334. } else if (rcu_dereference_protected(
  335. nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
  336. lockdep_is_held(&nf_ct_proto_mutex)
  337. ) != &nf_conntrack_l4proto_generic) {
  338. ret = -EBUSY;
  339. goto out_unlock;
  340. }
  341. l4proto->nla_size = 0;
  342. if (l4proto->nlattr_size)
  343. l4proto->nla_size += l4proto->nlattr_size();
  344. if (l4proto->nlattr_tuple_size)
  345. l4proto->nla_size += 3 * l4proto->nlattr_tuple_size();
  346. rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
  347. l4proto);
  348. out_unlock:
  349. mutex_unlock(&nf_ct_proto_mutex);
  350. return ret;
  351. }
  352. EXPORT_SYMBOL_GPL(nf_ct_l4proto_register);
  353. int nf_ct_l4proto_pernet_register(struct net *net,
  354. struct nf_conntrack_l4proto *l4proto)
  355. {
  356. int ret = 0;
  357. struct nf_proto_net *pn = NULL;
  358. if (l4proto->init_net) {
  359. ret = l4proto->init_net(net, l4proto->l3proto);
  360. if (ret < 0)
  361. goto out;
  362. }
  363. pn = nf_ct_l4proto_net(net, l4proto);
  364. if (pn == NULL)
  365. goto out;
  366. ret = nf_ct_l4proto_register_sysctl(net, pn, l4proto);
  367. if (ret < 0)
  368. goto out;
  369. pn->users++;
  370. out:
  371. return ret;
  372. }
  373. EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_register);
  374. void nf_ct_l4proto_unregister(struct nf_conntrack_l4proto *l4proto)
  375. {
  376. BUG_ON(l4proto->l3proto >= PF_MAX);
  377. mutex_lock(&nf_ct_proto_mutex);
  378. BUG_ON(rcu_dereference_protected(
  379. nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
  380. lockdep_is_held(&nf_ct_proto_mutex)
  381. ) != l4proto);
  382. rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
  383. &nf_conntrack_l4proto_generic);
  384. mutex_unlock(&nf_ct_proto_mutex);
  385. synchronize_rcu();
  386. }
  387. EXPORT_SYMBOL_GPL(nf_ct_l4proto_unregister);
  388. void nf_ct_l4proto_pernet_unregister(struct net *net,
  389. struct nf_conntrack_l4proto *l4proto)
  390. {
  391. struct nf_proto_net *pn = NULL;
  392. pn = nf_ct_l4proto_net(net, l4proto);
  393. if (pn == NULL)
  394. return;
  395. pn->users--;
  396. nf_ct_l4proto_unregister_sysctl(net, pn, l4proto);
  397. /* Remove all contrack entries for this protocol */
  398. nf_ct_iterate_cleanup(net, kill_l4proto, l4proto, 0, 0);
  399. }
  400. EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_unregister);
  401. int nf_conntrack_proto_pernet_init(struct net *net)
  402. {
  403. int err;
  404. struct nf_proto_net *pn = nf_ct_l4proto_net(net,
  405. &nf_conntrack_l4proto_generic);
  406. err = nf_conntrack_l4proto_generic.init_net(net,
  407. nf_conntrack_l4proto_generic.l3proto);
  408. if (err < 0)
  409. return err;
  410. err = nf_ct_l4proto_register_sysctl(net,
  411. pn,
  412. &nf_conntrack_l4proto_generic);
  413. if (err < 0)
  414. return err;
  415. pn->users++;
  416. return 0;
  417. }
  418. void nf_conntrack_proto_pernet_fini(struct net *net)
  419. {
  420. struct nf_proto_net *pn = nf_ct_l4proto_net(net,
  421. &nf_conntrack_l4proto_generic);
  422. pn->users--;
  423. nf_ct_l4proto_unregister_sysctl(net,
  424. pn,
  425. &nf_conntrack_l4proto_generic);
  426. }
  427. int nf_conntrack_proto_init(void)
  428. {
  429. unsigned int i;
  430. for (i = 0; i < AF_MAX; i++)
  431. rcu_assign_pointer(nf_ct_l3protos[i],
  432. &nf_conntrack_l3proto_generic);
  433. return 0;
  434. }
  435. void nf_conntrack_proto_fini(void)
  436. {
  437. unsigned int i;
  438. /* free l3proto protocol tables */
  439. for (i = 0; i < PF_MAX; i++)
  440. kfree(nf_ct_protos[i]);
  441. }