/net/sched/act_ipt.c

http://github.com/mirrors/linux · C · 448 lines · 362 code · 70 blank · 16 comment · 44 complexity · d0ffb2f4f99b58127362e87d22c3a959 MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/sched/act_ipt.c iptables target interface
  4. *
  5. *TODO: Add other tables. For now we only support the ipv4 table targets
  6. *
  7. * Copyright: Jamal Hadi Salim (2002-13)
  8. */
  9. #include <linux/types.h>
  10. #include <linux/kernel.h>
  11. #include <linux/string.h>
  12. #include <linux/errno.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/rtnetlink.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <net/netlink.h>
  19. #include <net/pkt_sched.h>
  20. #include <linux/tc_act/tc_ipt.h>
  21. #include <net/tc_act/tc_ipt.h>
  22. #include <linux/netfilter_ipv4/ip_tables.h>
  23. static unsigned int ipt_net_id;
  24. static struct tc_action_ops act_ipt_ops;
  25. static unsigned int xt_net_id;
  26. static struct tc_action_ops act_xt_ops;
  27. static int ipt_init_target(struct net *net, struct xt_entry_target *t,
  28. char *table, unsigned int hook)
  29. {
  30. struct xt_tgchk_param par;
  31. struct xt_target *target;
  32. struct ipt_entry e = {};
  33. int ret = 0;
  34. target = xt_request_find_target(AF_INET, t->u.user.name,
  35. t->u.user.revision);
  36. if (IS_ERR(target))
  37. return PTR_ERR(target);
  38. t->u.kernel.target = target;
  39. memset(&par, 0, sizeof(par));
  40. par.net = net;
  41. par.table = table;
  42. par.entryinfo = &e;
  43. par.target = target;
  44. par.targinfo = t->data;
  45. par.hook_mask = hook;
  46. par.family = NFPROTO_IPV4;
  47. ret = xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
  48. if (ret < 0) {
  49. module_put(t->u.kernel.target->me);
  50. return ret;
  51. }
  52. return 0;
  53. }
  54. static void ipt_destroy_target(struct xt_entry_target *t, struct net *net)
  55. {
  56. struct xt_tgdtor_param par = {
  57. .target = t->u.kernel.target,
  58. .targinfo = t->data,
  59. .family = NFPROTO_IPV4,
  60. .net = net,
  61. };
  62. if (par.target->destroy != NULL)
  63. par.target->destroy(&par);
  64. module_put(par.target->me);
  65. }
  66. static void tcf_ipt_release(struct tc_action *a)
  67. {
  68. struct tcf_ipt *ipt = to_ipt(a);
  69. if (ipt->tcfi_t) {
  70. ipt_destroy_target(ipt->tcfi_t, a->idrinfo->net);
  71. kfree(ipt->tcfi_t);
  72. }
  73. kfree(ipt->tcfi_tname);
  74. }
  75. static const struct nla_policy ipt_policy[TCA_IPT_MAX + 1] = {
  76. [TCA_IPT_TABLE] = { .type = NLA_STRING, .len = IFNAMSIZ },
  77. [TCA_IPT_HOOK] = { .type = NLA_U32 },
  78. [TCA_IPT_INDEX] = { .type = NLA_U32 },
  79. [TCA_IPT_TARG] = { .len = sizeof(struct xt_entry_target) },
  80. };
  81. static int __tcf_ipt_init(struct net *net, unsigned int id, struct nlattr *nla,
  82. struct nlattr *est, struct tc_action **a,
  83. const struct tc_action_ops *ops, int ovr, int bind,
  84. struct tcf_proto *tp, u32 flags)
  85. {
  86. struct tc_action_net *tn = net_generic(net, id);
  87. struct nlattr *tb[TCA_IPT_MAX + 1];
  88. struct tcf_ipt *ipt;
  89. struct xt_entry_target *td, *t;
  90. char *tname;
  91. bool exists = false;
  92. int ret = 0, err;
  93. u32 hook = 0;
  94. u32 index = 0;
  95. if (nla == NULL)
  96. return -EINVAL;
  97. err = nla_parse_nested_deprecated(tb, TCA_IPT_MAX, nla, ipt_policy,
  98. NULL);
  99. if (err < 0)
  100. return err;
  101. if (tb[TCA_IPT_INDEX] != NULL)
  102. index = nla_get_u32(tb[TCA_IPT_INDEX]);
  103. err = tcf_idr_check_alloc(tn, &index, a, bind);
  104. if (err < 0)
  105. return err;
  106. exists = err;
  107. if (exists && bind)
  108. return 0;
  109. if (tb[TCA_IPT_HOOK] == NULL || tb[TCA_IPT_TARG] == NULL) {
  110. if (exists)
  111. tcf_idr_release(*a, bind);
  112. else
  113. tcf_idr_cleanup(tn, index);
  114. return -EINVAL;
  115. }
  116. td = (struct xt_entry_target *)nla_data(tb[TCA_IPT_TARG]);
  117. if (nla_len(tb[TCA_IPT_TARG]) != td->u.target_size) {
  118. if (exists)
  119. tcf_idr_release(*a, bind);
  120. else
  121. tcf_idr_cleanup(tn, index);
  122. return -EINVAL;
  123. }
  124. if (!exists) {
  125. ret = tcf_idr_create(tn, index, est, a, ops, bind,
  126. false, 0);
  127. if (ret) {
  128. tcf_idr_cleanup(tn, index);
  129. return ret;
  130. }
  131. ret = ACT_P_CREATED;
  132. } else {
  133. if (bind)/* dont override defaults */
  134. return 0;
  135. if (!ovr) {
  136. tcf_idr_release(*a, bind);
  137. return -EEXIST;
  138. }
  139. }
  140. hook = nla_get_u32(tb[TCA_IPT_HOOK]);
  141. err = -ENOMEM;
  142. tname = kmalloc(IFNAMSIZ, GFP_KERNEL);
  143. if (unlikely(!tname))
  144. goto err1;
  145. if (tb[TCA_IPT_TABLE] == NULL ||
  146. nla_strlcpy(tname, tb[TCA_IPT_TABLE], IFNAMSIZ) >= IFNAMSIZ)
  147. strcpy(tname, "mangle");
  148. t = kmemdup(td, td->u.target_size, GFP_KERNEL);
  149. if (unlikely(!t))
  150. goto err2;
  151. err = ipt_init_target(net, t, tname, hook);
  152. if (err < 0)
  153. goto err3;
  154. ipt = to_ipt(*a);
  155. spin_lock_bh(&ipt->tcf_lock);
  156. if (ret != ACT_P_CREATED) {
  157. ipt_destroy_target(ipt->tcfi_t, net);
  158. kfree(ipt->tcfi_tname);
  159. kfree(ipt->tcfi_t);
  160. }
  161. ipt->tcfi_tname = tname;
  162. ipt->tcfi_t = t;
  163. ipt->tcfi_hook = hook;
  164. spin_unlock_bh(&ipt->tcf_lock);
  165. if (ret == ACT_P_CREATED)
  166. tcf_idr_insert(tn, *a);
  167. return ret;
  168. err3:
  169. kfree(t);
  170. err2:
  171. kfree(tname);
  172. err1:
  173. tcf_idr_release(*a, bind);
  174. return err;
  175. }
  176. static int tcf_ipt_init(struct net *net, struct nlattr *nla,
  177. struct nlattr *est, struct tc_action **a, int ovr,
  178. int bind, bool rtnl_held, struct tcf_proto *tp,
  179. u32 flags, struct netlink_ext_ack *extack)
  180. {
  181. return __tcf_ipt_init(net, ipt_net_id, nla, est, a, &act_ipt_ops, ovr,
  182. bind, tp, flags);
  183. }
  184. static int tcf_xt_init(struct net *net, struct nlattr *nla,
  185. struct nlattr *est, struct tc_action **a, int ovr,
  186. int bind, bool unlocked, struct tcf_proto *tp,
  187. u32 flags, struct netlink_ext_ack *extack)
  188. {
  189. return __tcf_ipt_init(net, xt_net_id, nla, est, a, &act_xt_ops, ovr,
  190. bind, tp, flags);
  191. }
  192. static int tcf_ipt_act(struct sk_buff *skb, const struct tc_action *a,
  193. struct tcf_result *res)
  194. {
  195. int ret = 0, result = 0;
  196. struct tcf_ipt *ipt = to_ipt(a);
  197. struct xt_action_param par;
  198. struct nf_hook_state state = {
  199. .net = dev_net(skb->dev),
  200. .in = skb->dev,
  201. .hook = ipt->tcfi_hook,
  202. .pf = NFPROTO_IPV4,
  203. };
  204. if (skb_unclone(skb, GFP_ATOMIC))
  205. return TC_ACT_UNSPEC;
  206. spin_lock(&ipt->tcf_lock);
  207. tcf_lastuse_update(&ipt->tcf_tm);
  208. bstats_update(&ipt->tcf_bstats, skb);
  209. /* yes, we have to worry about both in and out dev
  210. * worry later - danger - this API seems to have changed
  211. * from earlier kernels
  212. */
  213. par.state = &state;
  214. par.target = ipt->tcfi_t->u.kernel.target;
  215. par.targinfo = ipt->tcfi_t->data;
  216. ret = par.target->target(skb, &par);
  217. switch (ret) {
  218. case NF_ACCEPT:
  219. result = TC_ACT_OK;
  220. break;
  221. case NF_DROP:
  222. result = TC_ACT_SHOT;
  223. ipt->tcf_qstats.drops++;
  224. break;
  225. case XT_CONTINUE:
  226. result = TC_ACT_PIPE;
  227. break;
  228. default:
  229. net_notice_ratelimited("tc filter: Bogus netfilter code %d assume ACCEPT\n",
  230. ret);
  231. result = TC_ACT_OK;
  232. break;
  233. }
  234. spin_unlock(&ipt->tcf_lock);
  235. return result;
  236. }
  237. static int tcf_ipt_dump(struct sk_buff *skb, struct tc_action *a, int bind,
  238. int ref)
  239. {
  240. unsigned char *b = skb_tail_pointer(skb);
  241. struct tcf_ipt *ipt = to_ipt(a);
  242. struct xt_entry_target *t;
  243. struct tcf_t tm;
  244. struct tc_cnt c;
  245. /* for simple targets kernel size == user size
  246. * user name = target name
  247. * for foolproof you need to not assume this
  248. */
  249. spin_lock_bh(&ipt->tcf_lock);
  250. t = kmemdup(ipt->tcfi_t, ipt->tcfi_t->u.user.target_size, GFP_ATOMIC);
  251. if (unlikely(!t))
  252. goto nla_put_failure;
  253. c.bindcnt = atomic_read(&ipt->tcf_bindcnt) - bind;
  254. c.refcnt = refcount_read(&ipt->tcf_refcnt) - ref;
  255. strcpy(t->u.user.name, ipt->tcfi_t->u.kernel.target->name);
  256. if (nla_put(skb, TCA_IPT_TARG, ipt->tcfi_t->u.user.target_size, t) ||
  257. nla_put_u32(skb, TCA_IPT_INDEX, ipt->tcf_index) ||
  258. nla_put_u32(skb, TCA_IPT_HOOK, ipt->tcfi_hook) ||
  259. nla_put(skb, TCA_IPT_CNT, sizeof(struct tc_cnt), &c) ||
  260. nla_put_string(skb, TCA_IPT_TABLE, ipt->tcfi_tname))
  261. goto nla_put_failure;
  262. tcf_tm_dump(&tm, &ipt->tcf_tm);
  263. if (nla_put_64bit(skb, TCA_IPT_TM, sizeof(tm), &tm, TCA_IPT_PAD))
  264. goto nla_put_failure;
  265. spin_unlock_bh(&ipt->tcf_lock);
  266. kfree(t);
  267. return skb->len;
  268. nla_put_failure:
  269. spin_unlock_bh(&ipt->tcf_lock);
  270. nlmsg_trim(skb, b);
  271. kfree(t);
  272. return -1;
  273. }
  274. static int tcf_ipt_walker(struct net *net, struct sk_buff *skb,
  275. struct netlink_callback *cb, int type,
  276. const struct tc_action_ops *ops,
  277. struct netlink_ext_ack *extack)
  278. {
  279. struct tc_action_net *tn = net_generic(net, ipt_net_id);
  280. return tcf_generic_walker(tn, skb, cb, type, ops, extack);
  281. }
  282. static int tcf_ipt_search(struct net *net, struct tc_action **a, u32 index)
  283. {
  284. struct tc_action_net *tn = net_generic(net, ipt_net_id);
  285. return tcf_idr_search(tn, a, index);
  286. }
  287. static struct tc_action_ops act_ipt_ops = {
  288. .kind = "ipt",
  289. .id = TCA_ID_IPT,
  290. .owner = THIS_MODULE,
  291. .act = tcf_ipt_act,
  292. .dump = tcf_ipt_dump,
  293. .cleanup = tcf_ipt_release,
  294. .init = tcf_ipt_init,
  295. .walk = tcf_ipt_walker,
  296. .lookup = tcf_ipt_search,
  297. .size = sizeof(struct tcf_ipt),
  298. };
  299. static __net_init int ipt_init_net(struct net *net)
  300. {
  301. struct tc_action_net *tn = net_generic(net, ipt_net_id);
  302. return tc_action_net_init(net, tn, &act_ipt_ops);
  303. }
  304. static void __net_exit ipt_exit_net(struct list_head *net_list)
  305. {
  306. tc_action_net_exit(net_list, ipt_net_id);
  307. }
  308. static struct pernet_operations ipt_net_ops = {
  309. .init = ipt_init_net,
  310. .exit_batch = ipt_exit_net,
  311. .id = &ipt_net_id,
  312. .size = sizeof(struct tc_action_net),
  313. };
  314. static int tcf_xt_walker(struct net *net, struct sk_buff *skb,
  315. struct netlink_callback *cb, int type,
  316. const struct tc_action_ops *ops,
  317. struct netlink_ext_ack *extack)
  318. {
  319. struct tc_action_net *tn = net_generic(net, xt_net_id);
  320. return tcf_generic_walker(tn, skb, cb, type, ops, extack);
  321. }
  322. static int tcf_xt_search(struct net *net, struct tc_action **a, u32 index)
  323. {
  324. struct tc_action_net *tn = net_generic(net, xt_net_id);
  325. return tcf_idr_search(tn, a, index);
  326. }
  327. static struct tc_action_ops act_xt_ops = {
  328. .kind = "xt",
  329. .id = TCA_ID_XT,
  330. .owner = THIS_MODULE,
  331. .act = tcf_ipt_act,
  332. .dump = tcf_ipt_dump,
  333. .cleanup = tcf_ipt_release,
  334. .init = tcf_xt_init,
  335. .walk = tcf_xt_walker,
  336. .lookup = tcf_xt_search,
  337. .size = sizeof(struct tcf_ipt),
  338. };
  339. static __net_init int xt_init_net(struct net *net)
  340. {
  341. struct tc_action_net *tn = net_generic(net, xt_net_id);
  342. return tc_action_net_init(net, tn, &act_xt_ops);
  343. }
  344. static void __net_exit xt_exit_net(struct list_head *net_list)
  345. {
  346. tc_action_net_exit(net_list, xt_net_id);
  347. }
  348. static struct pernet_operations xt_net_ops = {
  349. .init = xt_init_net,
  350. .exit_batch = xt_exit_net,
  351. .id = &xt_net_id,
  352. .size = sizeof(struct tc_action_net),
  353. };
  354. MODULE_AUTHOR("Jamal Hadi Salim(2002-13)");
  355. MODULE_DESCRIPTION("Iptables target actions");
  356. MODULE_LICENSE("GPL");
  357. MODULE_ALIAS("act_xt");
  358. static int __init ipt_init_module(void)
  359. {
  360. int ret1, ret2;
  361. ret1 = tcf_register_action(&act_xt_ops, &xt_net_ops);
  362. if (ret1 < 0)
  363. pr_err("Failed to load xt action\n");
  364. ret2 = tcf_register_action(&act_ipt_ops, &ipt_net_ops);
  365. if (ret2 < 0)
  366. pr_err("Failed to load ipt action\n");
  367. if (ret1 < 0 && ret2 < 0) {
  368. return ret1;
  369. } else
  370. return 0;
  371. }
  372. static void __exit ipt_cleanup_module(void)
  373. {
  374. tcf_unregister_action(&act_ipt_ops, &ipt_net_ops);
  375. tcf_unregister_action(&act_xt_ops, &xt_net_ops);
  376. }
  377. module_init(ipt_init_module);
  378. module_exit(ipt_cleanup_module);