act_ipt.c 10 KB

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