nfnetlink_cthelper.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*
  2. * (C) 2012 Pablo Neira Ayuso <pablo@netfilter.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation (or any later at your option).
  7. *
  8. * This software has been sponsored by Vyatta Inc. <http://www.vyatta.com>
  9. */
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/netlink.h>
  15. #include <linux/rculist.h>
  16. #include <linux/slab.h>
  17. #include <linux/types.h>
  18. #include <linux/list.h>
  19. #include <linux/errno.h>
  20. #include <linux/capability.h>
  21. #include <net/netlink.h>
  22. #include <net/sock.h>
  23. #include <net/netfilter/nf_conntrack_helper.h>
  24. #include <net/netfilter/nf_conntrack_expect.h>
  25. #include <net/netfilter/nf_conntrack_ecache.h>
  26. #include <linux/netfilter/nfnetlink.h>
  27. #include <linux/netfilter/nfnetlink_conntrack.h>
  28. #include <linux/netfilter/nfnetlink_cthelper.h>
  29. MODULE_LICENSE("GPL");
  30. MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
  31. MODULE_DESCRIPTION("nfnl_cthelper: User-space connection tracking helpers");
  32. struct nfnl_cthelper {
  33. struct list_head list;
  34. struct nf_conntrack_helper helper;
  35. };
  36. static LIST_HEAD(nfnl_cthelper_list);
  37. static int
  38. nfnl_userspace_cthelper(struct sk_buff *skb, unsigned int protoff,
  39. struct nf_conn *ct, enum ip_conntrack_info ctinfo)
  40. {
  41. const struct nf_conn_help *help;
  42. struct nf_conntrack_helper *helper;
  43. help = nfct_help(ct);
  44. if (help == NULL)
  45. return NF_DROP;
  46. /* rcu_read_lock()ed by nf_hook_thresh */
  47. helper = rcu_dereference(help->helper);
  48. if (helper == NULL)
  49. return NF_DROP;
  50. /* This is an user-space helper not yet configured, skip. */
  51. if ((helper->flags &
  52. (NF_CT_HELPER_F_USERSPACE | NF_CT_HELPER_F_CONFIGURED)) ==
  53. NF_CT_HELPER_F_USERSPACE)
  54. return NF_ACCEPT;
  55. /* If the user-space helper is not available, don't block traffic. */
  56. return NF_QUEUE_NR(helper->queue_num) | NF_VERDICT_FLAG_QUEUE_BYPASS;
  57. }
  58. static const struct nla_policy nfnl_cthelper_tuple_pol[NFCTH_TUPLE_MAX+1] = {
  59. [NFCTH_TUPLE_L3PROTONUM] = { .type = NLA_U16, },
  60. [NFCTH_TUPLE_L4PROTONUM] = { .type = NLA_U8, },
  61. };
  62. static int
  63. nfnl_cthelper_parse_tuple(struct nf_conntrack_tuple *tuple,
  64. const struct nlattr *attr)
  65. {
  66. int err;
  67. struct nlattr *tb[NFCTH_TUPLE_MAX+1];
  68. err = nla_parse_nested(tb, NFCTH_TUPLE_MAX, attr, nfnl_cthelper_tuple_pol);
  69. if (err < 0)
  70. return err;
  71. if (!tb[NFCTH_TUPLE_L3PROTONUM] || !tb[NFCTH_TUPLE_L4PROTONUM])
  72. return -EINVAL;
  73. /* Not all fields are initialized so first zero the tuple */
  74. memset(tuple, 0, sizeof(struct nf_conntrack_tuple));
  75. tuple->src.l3num = ntohs(nla_get_be16(tb[NFCTH_TUPLE_L3PROTONUM]));
  76. tuple->dst.protonum = nla_get_u8(tb[NFCTH_TUPLE_L4PROTONUM]);
  77. return 0;
  78. }
  79. static int
  80. nfnl_cthelper_from_nlattr(struct nlattr *attr, struct nf_conn *ct)
  81. {
  82. struct nf_conn_help *help = nfct_help(ct);
  83. if (attr == NULL)
  84. return -EINVAL;
  85. if (help->helper->data_len == 0)
  86. return -EINVAL;
  87. memcpy(help->data, nla_data(attr), help->helper->data_len);
  88. return 0;
  89. }
  90. static int
  91. nfnl_cthelper_to_nlattr(struct sk_buff *skb, const struct nf_conn *ct)
  92. {
  93. const struct nf_conn_help *help = nfct_help(ct);
  94. if (help->helper->data_len &&
  95. nla_put(skb, CTA_HELP_INFO, help->helper->data_len, &help->data))
  96. goto nla_put_failure;
  97. return 0;
  98. nla_put_failure:
  99. return -ENOSPC;
  100. }
  101. static const struct nla_policy nfnl_cthelper_expect_pol[NFCTH_POLICY_MAX+1] = {
  102. [NFCTH_POLICY_NAME] = { .type = NLA_NUL_STRING,
  103. .len = NF_CT_HELPER_NAME_LEN-1 },
  104. [NFCTH_POLICY_EXPECT_MAX] = { .type = NLA_U32, },
  105. [NFCTH_POLICY_EXPECT_TIMEOUT] = { .type = NLA_U32, },
  106. };
  107. static int
  108. nfnl_cthelper_expect_policy(struct nf_conntrack_expect_policy *expect_policy,
  109. const struct nlattr *attr)
  110. {
  111. int err;
  112. struct nlattr *tb[NFCTH_POLICY_MAX+1];
  113. err = nla_parse_nested(tb, NFCTH_POLICY_MAX, attr, nfnl_cthelper_expect_pol);
  114. if (err < 0)
  115. return err;
  116. if (!tb[NFCTH_POLICY_NAME] ||
  117. !tb[NFCTH_POLICY_EXPECT_MAX] ||
  118. !tb[NFCTH_POLICY_EXPECT_TIMEOUT])
  119. return -EINVAL;
  120. strncpy(expect_policy->name,
  121. nla_data(tb[NFCTH_POLICY_NAME]), NF_CT_HELPER_NAME_LEN);
  122. expect_policy->max_expected =
  123. ntohl(nla_get_be32(tb[NFCTH_POLICY_EXPECT_MAX]));
  124. expect_policy->timeout =
  125. ntohl(nla_get_be32(tb[NFCTH_POLICY_EXPECT_TIMEOUT]));
  126. return 0;
  127. }
  128. static const struct nla_policy
  129. nfnl_cthelper_expect_policy_set[NFCTH_POLICY_SET_MAX+1] = {
  130. [NFCTH_POLICY_SET_NUM] = { .type = NLA_U32, },
  131. };
  132. static int
  133. nfnl_cthelper_parse_expect_policy(struct nf_conntrack_helper *helper,
  134. const struct nlattr *attr)
  135. {
  136. int i, ret;
  137. struct nf_conntrack_expect_policy *expect_policy;
  138. struct nlattr *tb[NFCTH_POLICY_SET_MAX+1];
  139. unsigned int class_max;
  140. ret = nla_parse_nested(tb, NFCTH_POLICY_SET_MAX, attr,
  141. nfnl_cthelper_expect_policy_set);
  142. if (ret < 0)
  143. return ret;
  144. if (!tb[NFCTH_POLICY_SET_NUM])
  145. return -EINVAL;
  146. class_max = ntohl(nla_get_be32(tb[NFCTH_POLICY_SET_NUM]));
  147. if (class_max == 0)
  148. return -EINVAL;
  149. if (class_max > NF_CT_MAX_EXPECT_CLASSES)
  150. return -EOVERFLOW;
  151. expect_policy = kzalloc(sizeof(struct nf_conntrack_expect_policy) *
  152. class_max, GFP_KERNEL);
  153. if (expect_policy == NULL)
  154. return -ENOMEM;
  155. for (i = 0; i < class_max; i++) {
  156. if (!tb[NFCTH_POLICY_SET+i])
  157. goto err;
  158. ret = nfnl_cthelper_expect_policy(&expect_policy[i],
  159. tb[NFCTH_POLICY_SET+i]);
  160. if (ret < 0)
  161. goto err;
  162. }
  163. helper->expect_class_max = class_max - 1;
  164. helper->expect_policy = expect_policy;
  165. return 0;
  166. err:
  167. kfree(expect_policy);
  168. return -EINVAL;
  169. }
  170. static int
  171. nfnl_cthelper_create(const struct nlattr * const tb[],
  172. struct nf_conntrack_tuple *tuple)
  173. {
  174. struct nf_conntrack_helper *helper;
  175. struct nfnl_cthelper *nfcth;
  176. int ret;
  177. if (!tb[NFCTH_TUPLE] || !tb[NFCTH_POLICY] || !tb[NFCTH_PRIV_DATA_LEN])
  178. return -EINVAL;
  179. nfcth = kzalloc(sizeof(*nfcth), GFP_KERNEL);
  180. if (nfcth == NULL)
  181. return -ENOMEM;
  182. helper = &nfcth->helper;
  183. ret = nfnl_cthelper_parse_expect_policy(helper, tb[NFCTH_POLICY]);
  184. if (ret < 0)
  185. goto err1;
  186. strncpy(helper->name, nla_data(tb[NFCTH_NAME]), NF_CT_HELPER_NAME_LEN);
  187. helper->data_len = ntohl(nla_get_be32(tb[NFCTH_PRIV_DATA_LEN]));
  188. helper->flags |= NF_CT_HELPER_F_USERSPACE;
  189. memcpy(&helper->tuple, tuple, sizeof(struct nf_conntrack_tuple));
  190. helper->me = THIS_MODULE;
  191. helper->help = nfnl_userspace_cthelper;
  192. helper->from_nlattr = nfnl_cthelper_from_nlattr;
  193. helper->to_nlattr = nfnl_cthelper_to_nlattr;
  194. /* Default to queue number zero, this can be updated at any time. */
  195. if (tb[NFCTH_QUEUE_NUM])
  196. helper->queue_num = ntohl(nla_get_be32(tb[NFCTH_QUEUE_NUM]));
  197. if (tb[NFCTH_STATUS]) {
  198. int status = ntohl(nla_get_be32(tb[NFCTH_STATUS]));
  199. switch(status) {
  200. case NFCT_HELPER_STATUS_ENABLED:
  201. helper->flags |= NF_CT_HELPER_F_CONFIGURED;
  202. break;
  203. case NFCT_HELPER_STATUS_DISABLED:
  204. helper->flags &= ~NF_CT_HELPER_F_CONFIGURED;
  205. break;
  206. }
  207. }
  208. ret = nf_conntrack_helper_register(helper);
  209. if (ret < 0)
  210. goto err2;
  211. list_add_tail(&nfcth->list, &nfnl_cthelper_list);
  212. return 0;
  213. err2:
  214. kfree(helper->expect_policy);
  215. err1:
  216. kfree(nfcth);
  217. return ret;
  218. }
  219. static int
  220. nfnl_cthelper_update_policy_one(const struct nf_conntrack_expect_policy *policy,
  221. struct nf_conntrack_expect_policy *new_policy,
  222. const struct nlattr *attr)
  223. {
  224. struct nlattr *tb[NFCTH_POLICY_MAX + 1];
  225. int err;
  226. err = nla_parse_nested(tb, NFCTH_POLICY_MAX, attr,
  227. nfnl_cthelper_expect_pol);
  228. if (err < 0)
  229. return err;
  230. if (!tb[NFCTH_POLICY_NAME] ||
  231. !tb[NFCTH_POLICY_EXPECT_MAX] ||
  232. !tb[NFCTH_POLICY_EXPECT_TIMEOUT])
  233. return -EINVAL;
  234. if (nla_strcmp(tb[NFCTH_POLICY_NAME], policy->name))
  235. return -EBUSY;
  236. new_policy->max_expected =
  237. ntohl(nla_get_be32(tb[NFCTH_POLICY_EXPECT_MAX]));
  238. new_policy->timeout =
  239. ntohl(nla_get_be32(tb[NFCTH_POLICY_EXPECT_TIMEOUT]));
  240. return 0;
  241. }
  242. static int nfnl_cthelper_update_policy_all(struct nlattr *tb[],
  243. struct nf_conntrack_helper *helper)
  244. {
  245. struct nf_conntrack_expect_policy new_policy[helper->expect_class_max + 1];
  246. struct nf_conntrack_expect_policy *policy;
  247. int i, err;
  248. /* Check first that all policy attributes are well-formed, so we don't
  249. * leave things in inconsistent state on errors.
  250. */
  251. for (i = 0; i < helper->expect_class_max + 1; i++) {
  252. if (!tb[NFCTH_POLICY_SET + i])
  253. return -EINVAL;
  254. err = nfnl_cthelper_update_policy_one(&helper->expect_policy[i],
  255. &new_policy[i],
  256. tb[NFCTH_POLICY_SET + i]);
  257. if (err < 0)
  258. return err;
  259. }
  260. /* Now we can safely update them. */
  261. for (i = 0; i < helper->expect_class_max + 1; i++) {
  262. policy = (struct nf_conntrack_expect_policy *)
  263. &helper->expect_policy[i];
  264. policy->max_expected = new_policy->max_expected;
  265. policy->timeout = new_policy->timeout;
  266. }
  267. return 0;
  268. }
  269. static int nfnl_cthelper_update_policy(struct nf_conntrack_helper *helper,
  270. const struct nlattr *attr)
  271. {
  272. struct nlattr *tb[NFCTH_POLICY_SET_MAX + 1];
  273. unsigned int class_max;
  274. int err;
  275. err = nla_parse_nested(tb, NFCTH_POLICY_SET_MAX, attr,
  276. nfnl_cthelper_expect_policy_set);
  277. if (err < 0)
  278. return err;
  279. if (!tb[NFCTH_POLICY_SET_NUM])
  280. return -EINVAL;
  281. class_max = ntohl(nla_get_be32(tb[NFCTH_POLICY_SET_NUM]));
  282. if (helper->expect_class_max + 1 != class_max)
  283. return -EBUSY;
  284. return nfnl_cthelper_update_policy_all(tb, helper);
  285. }
  286. static int
  287. nfnl_cthelper_update(const struct nlattr * const tb[],
  288. struct nf_conntrack_helper *helper)
  289. {
  290. int ret;
  291. if (tb[NFCTH_PRIV_DATA_LEN])
  292. return -EBUSY;
  293. if (tb[NFCTH_POLICY]) {
  294. ret = nfnl_cthelper_update_policy(helper, tb[NFCTH_POLICY]);
  295. if (ret < 0)
  296. return ret;
  297. }
  298. if (tb[NFCTH_QUEUE_NUM])
  299. helper->queue_num = ntohl(nla_get_be32(tb[NFCTH_QUEUE_NUM]));
  300. if (tb[NFCTH_STATUS]) {
  301. int status = ntohl(nla_get_be32(tb[NFCTH_STATUS]));
  302. switch(status) {
  303. case NFCT_HELPER_STATUS_ENABLED:
  304. helper->flags |= NF_CT_HELPER_F_CONFIGURED;
  305. break;
  306. case NFCT_HELPER_STATUS_DISABLED:
  307. helper->flags &= ~NF_CT_HELPER_F_CONFIGURED;
  308. break;
  309. }
  310. }
  311. return 0;
  312. }
  313. static int nfnl_cthelper_new(struct net *net, struct sock *nfnl,
  314. struct sk_buff *skb, const struct nlmsghdr *nlh,
  315. const struct nlattr * const tb[])
  316. {
  317. const char *helper_name;
  318. struct nf_conntrack_helper *cur, *helper = NULL;
  319. struct nf_conntrack_tuple tuple;
  320. struct nfnl_cthelper *nlcth;
  321. int ret = 0;
  322. if (!capable(CAP_NET_ADMIN))
  323. return -EPERM;
  324. if (!tb[NFCTH_NAME] || !tb[NFCTH_TUPLE])
  325. return -EINVAL;
  326. helper_name = nla_data(tb[NFCTH_NAME]);
  327. ret = nfnl_cthelper_parse_tuple(&tuple, tb[NFCTH_TUPLE]);
  328. if (ret < 0)
  329. return ret;
  330. list_for_each_entry(nlcth, &nfnl_cthelper_list, list) {
  331. cur = &nlcth->helper;
  332. if (strncmp(cur->name, helper_name, NF_CT_HELPER_NAME_LEN))
  333. continue;
  334. if ((tuple.src.l3num != cur->tuple.src.l3num ||
  335. tuple.dst.protonum != cur->tuple.dst.protonum))
  336. continue;
  337. if (nlh->nlmsg_flags & NLM_F_EXCL)
  338. return -EEXIST;
  339. helper = cur;
  340. break;
  341. }
  342. if (helper == NULL)
  343. ret = nfnl_cthelper_create(tb, &tuple);
  344. else
  345. ret = nfnl_cthelper_update(tb, helper);
  346. return ret;
  347. }
  348. static int
  349. nfnl_cthelper_dump_tuple(struct sk_buff *skb,
  350. struct nf_conntrack_helper *helper)
  351. {
  352. struct nlattr *nest_parms;
  353. nest_parms = nla_nest_start(skb, NFCTH_TUPLE | NLA_F_NESTED);
  354. if (nest_parms == NULL)
  355. goto nla_put_failure;
  356. if (nla_put_be16(skb, NFCTH_TUPLE_L3PROTONUM,
  357. htons(helper->tuple.src.l3num)))
  358. goto nla_put_failure;
  359. if (nla_put_u8(skb, NFCTH_TUPLE_L4PROTONUM, helper->tuple.dst.protonum))
  360. goto nla_put_failure;
  361. nla_nest_end(skb, nest_parms);
  362. return 0;
  363. nla_put_failure:
  364. return -1;
  365. }
  366. static int
  367. nfnl_cthelper_dump_policy(struct sk_buff *skb,
  368. struct nf_conntrack_helper *helper)
  369. {
  370. int i;
  371. struct nlattr *nest_parms1, *nest_parms2;
  372. nest_parms1 = nla_nest_start(skb, NFCTH_POLICY | NLA_F_NESTED);
  373. if (nest_parms1 == NULL)
  374. goto nla_put_failure;
  375. if (nla_put_be32(skb, NFCTH_POLICY_SET_NUM,
  376. htonl(helper->expect_class_max + 1)))
  377. goto nla_put_failure;
  378. for (i = 0; i < helper->expect_class_max + 1; i++) {
  379. nest_parms2 = nla_nest_start(skb,
  380. (NFCTH_POLICY_SET+i) | NLA_F_NESTED);
  381. if (nest_parms2 == NULL)
  382. goto nla_put_failure;
  383. if (nla_put_string(skb, NFCTH_POLICY_NAME,
  384. helper->expect_policy[i].name))
  385. goto nla_put_failure;
  386. if (nla_put_be32(skb, NFCTH_POLICY_EXPECT_MAX,
  387. htonl(helper->expect_policy[i].max_expected)))
  388. goto nla_put_failure;
  389. if (nla_put_be32(skb, NFCTH_POLICY_EXPECT_TIMEOUT,
  390. htonl(helper->expect_policy[i].timeout)))
  391. goto nla_put_failure;
  392. nla_nest_end(skb, nest_parms2);
  393. }
  394. nla_nest_end(skb, nest_parms1);
  395. return 0;
  396. nla_put_failure:
  397. return -1;
  398. }
  399. static int
  400. nfnl_cthelper_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
  401. int event, struct nf_conntrack_helper *helper)
  402. {
  403. struct nlmsghdr *nlh;
  404. struct nfgenmsg *nfmsg;
  405. unsigned int flags = portid ? NLM_F_MULTI : 0;
  406. int status;
  407. event |= NFNL_SUBSYS_CTHELPER << 8;
  408. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
  409. if (nlh == NULL)
  410. goto nlmsg_failure;
  411. nfmsg = nlmsg_data(nlh);
  412. nfmsg->nfgen_family = AF_UNSPEC;
  413. nfmsg->version = NFNETLINK_V0;
  414. nfmsg->res_id = 0;
  415. if (nla_put_string(skb, NFCTH_NAME, helper->name))
  416. goto nla_put_failure;
  417. if (nla_put_be32(skb, NFCTH_QUEUE_NUM, htonl(helper->queue_num)))
  418. goto nla_put_failure;
  419. if (nfnl_cthelper_dump_tuple(skb, helper) < 0)
  420. goto nla_put_failure;
  421. if (nfnl_cthelper_dump_policy(skb, helper) < 0)
  422. goto nla_put_failure;
  423. if (nla_put_be32(skb, NFCTH_PRIV_DATA_LEN, htonl(helper->data_len)))
  424. goto nla_put_failure;
  425. if (helper->flags & NF_CT_HELPER_F_CONFIGURED)
  426. status = NFCT_HELPER_STATUS_ENABLED;
  427. else
  428. status = NFCT_HELPER_STATUS_DISABLED;
  429. if (nla_put_be32(skb, NFCTH_STATUS, htonl(status)))
  430. goto nla_put_failure;
  431. nlmsg_end(skb, nlh);
  432. return skb->len;
  433. nlmsg_failure:
  434. nla_put_failure:
  435. nlmsg_cancel(skb, nlh);
  436. return -1;
  437. }
  438. static int
  439. nfnl_cthelper_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
  440. {
  441. struct nf_conntrack_helper *cur, *last;
  442. rcu_read_lock();
  443. last = (struct nf_conntrack_helper *)cb->args[1];
  444. for (; cb->args[0] < nf_ct_helper_hsize; cb->args[0]++) {
  445. restart:
  446. hlist_for_each_entry_rcu(cur,
  447. &nf_ct_helper_hash[cb->args[0]], hnode) {
  448. /* skip non-userspace conntrack helpers. */
  449. if (!(cur->flags & NF_CT_HELPER_F_USERSPACE))
  450. continue;
  451. if (cb->args[1]) {
  452. if (cur != last)
  453. continue;
  454. cb->args[1] = 0;
  455. }
  456. if (nfnl_cthelper_fill_info(skb,
  457. NETLINK_CB(cb->skb).portid,
  458. cb->nlh->nlmsg_seq,
  459. NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
  460. NFNL_MSG_CTHELPER_NEW, cur) < 0) {
  461. cb->args[1] = (unsigned long)cur;
  462. goto out;
  463. }
  464. }
  465. }
  466. if (cb->args[1]) {
  467. cb->args[1] = 0;
  468. goto restart;
  469. }
  470. out:
  471. rcu_read_unlock();
  472. return skb->len;
  473. }
  474. static int nfnl_cthelper_get(struct net *net, struct sock *nfnl,
  475. struct sk_buff *skb, const struct nlmsghdr *nlh,
  476. const struct nlattr * const tb[])
  477. {
  478. int ret = -ENOENT;
  479. struct nf_conntrack_helper *cur;
  480. struct sk_buff *skb2;
  481. char *helper_name = NULL;
  482. struct nf_conntrack_tuple tuple;
  483. struct nfnl_cthelper *nlcth;
  484. bool tuple_set = false;
  485. if (!capable(CAP_NET_ADMIN))
  486. return -EPERM;
  487. if (nlh->nlmsg_flags & NLM_F_DUMP) {
  488. struct netlink_dump_control c = {
  489. .dump = nfnl_cthelper_dump_table,
  490. };
  491. return netlink_dump_start(nfnl, skb, nlh, &c);
  492. }
  493. if (tb[NFCTH_NAME])
  494. helper_name = nla_data(tb[NFCTH_NAME]);
  495. if (tb[NFCTH_TUPLE]) {
  496. ret = nfnl_cthelper_parse_tuple(&tuple, tb[NFCTH_TUPLE]);
  497. if (ret < 0)
  498. return ret;
  499. tuple_set = true;
  500. }
  501. list_for_each_entry(nlcth, &nfnl_cthelper_list, list) {
  502. cur = &nlcth->helper;
  503. if (helper_name &&
  504. strncmp(cur->name, helper_name, NF_CT_HELPER_NAME_LEN))
  505. continue;
  506. if (tuple_set &&
  507. (tuple.src.l3num != cur->tuple.src.l3num ||
  508. tuple.dst.protonum != cur->tuple.dst.protonum))
  509. continue;
  510. skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  511. if (skb2 == NULL) {
  512. ret = -ENOMEM;
  513. break;
  514. }
  515. ret = nfnl_cthelper_fill_info(skb2, NETLINK_CB(skb).portid,
  516. nlh->nlmsg_seq,
  517. NFNL_MSG_TYPE(nlh->nlmsg_type),
  518. NFNL_MSG_CTHELPER_NEW, cur);
  519. if (ret <= 0) {
  520. kfree_skb(skb2);
  521. break;
  522. }
  523. ret = netlink_unicast(nfnl, skb2, NETLINK_CB(skb).portid,
  524. MSG_DONTWAIT);
  525. if (ret > 0)
  526. ret = 0;
  527. /* this avoids a loop in nfnetlink. */
  528. return ret == -EAGAIN ? -ENOBUFS : ret;
  529. }
  530. return ret;
  531. }
  532. static int nfnl_cthelper_del(struct net *net, struct sock *nfnl,
  533. struct sk_buff *skb, const struct nlmsghdr *nlh,
  534. const struct nlattr * const tb[])
  535. {
  536. char *helper_name = NULL;
  537. struct nf_conntrack_helper *cur;
  538. struct nf_conntrack_tuple tuple;
  539. bool tuple_set = false, found = false;
  540. struct nfnl_cthelper *nlcth, *n;
  541. int j = 0, ret;
  542. if (!capable(CAP_NET_ADMIN))
  543. return -EPERM;
  544. if (tb[NFCTH_NAME])
  545. helper_name = nla_data(tb[NFCTH_NAME]);
  546. if (tb[NFCTH_TUPLE]) {
  547. ret = nfnl_cthelper_parse_tuple(&tuple, tb[NFCTH_TUPLE]);
  548. if (ret < 0)
  549. return ret;
  550. tuple_set = true;
  551. }
  552. list_for_each_entry_safe(nlcth, n, &nfnl_cthelper_list, list) {
  553. cur = &nlcth->helper;
  554. j++;
  555. if (helper_name &&
  556. strncmp(cur->name, helper_name, NF_CT_HELPER_NAME_LEN))
  557. continue;
  558. if (tuple_set &&
  559. (tuple.src.l3num != cur->tuple.src.l3num ||
  560. tuple.dst.protonum != cur->tuple.dst.protonum))
  561. continue;
  562. found = true;
  563. nf_conntrack_helper_unregister(cur);
  564. kfree(cur->expect_policy);
  565. list_del(&nlcth->list);
  566. kfree(nlcth);
  567. }
  568. /* Make sure we return success if we flush and there is no helpers */
  569. return (found || j == 0) ? 0 : -ENOENT;
  570. }
  571. static const struct nla_policy nfnl_cthelper_policy[NFCTH_MAX+1] = {
  572. [NFCTH_NAME] = { .type = NLA_NUL_STRING,
  573. .len = NF_CT_HELPER_NAME_LEN-1 },
  574. [NFCTH_QUEUE_NUM] = { .type = NLA_U32, },
  575. };
  576. static const struct nfnl_callback nfnl_cthelper_cb[NFNL_MSG_CTHELPER_MAX] = {
  577. [NFNL_MSG_CTHELPER_NEW] = { .call = nfnl_cthelper_new,
  578. .attr_count = NFCTH_MAX,
  579. .policy = nfnl_cthelper_policy },
  580. [NFNL_MSG_CTHELPER_GET] = { .call = nfnl_cthelper_get,
  581. .attr_count = NFCTH_MAX,
  582. .policy = nfnl_cthelper_policy },
  583. [NFNL_MSG_CTHELPER_DEL] = { .call = nfnl_cthelper_del,
  584. .attr_count = NFCTH_MAX,
  585. .policy = nfnl_cthelper_policy },
  586. };
  587. static const struct nfnetlink_subsystem nfnl_cthelper_subsys = {
  588. .name = "cthelper",
  589. .subsys_id = NFNL_SUBSYS_CTHELPER,
  590. .cb_count = NFNL_MSG_CTHELPER_MAX,
  591. .cb = nfnl_cthelper_cb,
  592. };
  593. MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTHELPER);
  594. static int __init nfnl_cthelper_init(void)
  595. {
  596. int ret;
  597. ret = nfnetlink_subsys_register(&nfnl_cthelper_subsys);
  598. if (ret < 0) {
  599. pr_err("nfnl_cthelper: cannot register with nfnetlink.\n");
  600. goto err_out;
  601. }
  602. return 0;
  603. err_out:
  604. return ret;
  605. }
  606. static void __exit nfnl_cthelper_exit(void)
  607. {
  608. struct nf_conntrack_helper *cur;
  609. struct nfnl_cthelper *nlcth, *n;
  610. nfnetlink_subsys_unregister(&nfnl_cthelper_subsys);
  611. list_for_each_entry_safe(nlcth, n, &nfnl_cthelper_list, list) {
  612. cur = &nlcth->helper;
  613. nf_conntrack_helper_unregister(cur);
  614. kfree(cur->expect_policy);
  615. kfree(nlcth);
  616. }
  617. }
  618. module_init(nfnl_cthelper_init);
  619. module_exit(nfnl_cthelper_exit);