act_pedit.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * net/sched/act_pedit.c Generic packet editor
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Authors: Jamal Hadi Salim (2002-4)
  10. */
  11. #include <linux/types.h>
  12. #include <linux/kernel.h>
  13. #include <linux/string.h>
  14. #include <linux/errno.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/rtnetlink.h>
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/slab.h>
  20. #include <net/netlink.h>
  21. #include <net/pkt_sched.h>
  22. #include <linux/tc_act/tc_pedit.h>
  23. #include <net/tc_act/tc_pedit.h>
  24. #include <uapi/linux/tc_act/tc_pedit.h>
  25. #define PEDIT_TAB_MASK 15
  26. static unsigned int pedit_net_id;
  27. static struct tc_action_ops act_pedit_ops;
  28. static const struct nla_policy pedit_policy[TCA_PEDIT_MAX + 1] = {
  29. [TCA_PEDIT_PARMS] = { .len = sizeof(struct tc_pedit) },
  30. [TCA_PEDIT_KEYS_EX] = { .type = NLA_NESTED },
  31. };
  32. static const struct nla_policy pedit_key_ex_policy[TCA_PEDIT_KEY_EX_MAX + 1] = {
  33. [TCA_PEDIT_KEY_EX_HTYPE] = { .type = NLA_U16 },
  34. [TCA_PEDIT_KEY_EX_CMD] = { .type = NLA_U16 },
  35. };
  36. static struct tcf_pedit_key_ex *tcf_pedit_keys_ex_parse(struct nlattr *nla,
  37. u8 n)
  38. {
  39. struct tcf_pedit_key_ex *keys_ex;
  40. struct tcf_pedit_key_ex *k;
  41. const struct nlattr *ka;
  42. int err = -EINVAL;
  43. int rem;
  44. if (!nla || !n)
  45. return NULL;
  46. keys_ex = kcalloc(n, sizeof(*k), GFP_KERNEL);
  47. if (!keys_ex)
  48. return ERR_PTR(-ENOMEM);
  49. k = keys_ex;
  50. nla_for_each_nested(ka, nla, rem) {
  51. struct nlattr *tb[TCA_PEDIT_KEY_EX_MAX + 1];
  52. if (!n) {
  53. err = -EINVAL;
  54. goto err_out;
  55. }
  56. n--;
  57. if (nla_type(ka) != TCA_PEDIT_KEY_EX) {
  58. err = -EINVAL;
  59. goto err_out;
  60. }
  61. err = nla_parse_nested(tb, TCA_PEDIT_KEY_EX_MAX, ka,
  62. pedit_key_ex_policy, NULL);
  63. if (err)
  64. goto err_out;
  65. if (!tb[TCA_PEDIT_KEY_EX_HTYPE] ||
  66. !tb[TCA_PEDIT_KEY_EX_CMD]) {
  67. err = -EINVAL;
  68. goto err_out;
  69. }
  70. k->htype = nla_get_u16(tb[TCA_PEDIT_KEY_EX_HTYPE]);
  71. k->cmd = nla_get_u16(tb[TCA_PEDIT_KEY_EX_CMD]);
  72. if (k->htype > TCA_PEDIT_HDR_TYPE_MAX ||
  73. k->cmd > TCA_PEDIT_CMD_MAX) {
  74. err = -EINVAL;
  75. goto err_out;
  76. }
  77. k++;
  78. }
  79. if (n) {
  80. err = -EINVAL;
  81. goto err_out;
  82. }
  83. return keys_ex;
  84. err_out:
  85. kfree(keys_ex);
  86. return ERR_PTR(err);
  87. }
  88. static int tcf_pedit_key_ex_dump(struct sk_buff *skb,
  89. struct tcf_pedit_key_ex *keys_ex, int n)
  90. {
  91. struct nlattr *keys_start = nla_nest_start(skb, TCA_PEDIT_KEYS_EX);
  92. for (; n > 0; n--) {
  93. struct nlattr *key_start;
  94. key_start = nla_nest_start(skb, TCA_PEDIT_KEY_EX);
  95. if (nla_put_u16(skb, TCA_PEDIT_KEY_EX_HTYPE, keys_ex->htype) ||
  96. nla_put_u16(skb, TCA_PEDIT_KEY_EX_CMD, keys_ex->cmd)) {
  97. nlmsg_trim(skb, keys_start);
  98. return -EINVAL;
  99. }
  100. nla_nest_end(skb, key_start);
  101. keys_ex++;
  102. }
  103. nla_nest_end(skb, keys_start);
  104. return 0;
  105. }
  106. static int tcf_pedit_init(struct net *net, struct nlattr *nla,
  107. struct nlattr *est, struct tc_action **a,
  108. int ovr, int bind)
  109. {
  110. struct tc_action_net *tn = net_generic(net, pedit_net_id);
  111. struct nlattr *tb[TCA_PEDIT_MAX + 1];
  112. struct nlattr *pattr;
  113. struct tc_pedit *parm;
  114. int ret = 0, err;
  115. struct tcf_pedit *p;
  116. struct tc_pedit_key *keys = NULL;
  117. struct tcf_pedit_key_ex *keys_ex;
  118. int ksize;
  119. if (nla == NULL)
  120. return -EINVAL;
  121. err = nla_parse_nested(tb, TCA_PEDIT_MAX, nla, pedit_policy, NULL);
  122. if (err < 0)
  123. return err;
  124. pattr = tb[TCA_PEDIT_PARMS];
  125. if (!pattr)
  126. pattr = tb[TCA_PEDIT_PARMS_EX];
  127. if (!pattr)
  128. return -EINVAL;
  129. parm = nla_data(pattr);
  130. ksize = parm->nkeys * sizeof(struct tc_pedit_key);
  131. if (nla_len(pattr) < sizeof(*parm) + ksize)
  132. return -EINVAL;
  133. keys_ex = tcf_pedit_keys_ex_parse(tb[TCA_PEDIT_KEYS_EX], parm->nkeys);
  134. if (IS_ERR(keys_ex))
  135. return PTR_ERR(keys_ex);
  136. if (!tcf_hash_check(tn, parm->index, a, bind)) {
  137. if (!parm->nkeys)
  138. return -EINVAL;
  139. ret = tcf_hash_create(tn, parm->index, est, a,
  140. &act_pedit_ops, bind, false);
  141. if (ret)
  142. return ret;
  143. p = to_pedit(*a);
  144. keys = kmalloc(ksize, GFP_KERNEL);
  145. if (keys == NULL) {
  146. tcf_hash_cleanup(*a, est);
  147. kfree(keys_ex);
  148. return -ENOMEM;
  149. }
  150. ret = ACT_P_CREATED;
  151. } else {
  152. if (bind)
  153. return 0;
  154. tcf_hash_release(*a, bind);
  155. if (!ovr)
  156. return -EEXIST;
  157. p = to_pedit(*a);
  158. if (p->tcfp_nkeys && p->tcfp_nkeys != parm->nkeys) {
  159. keys = kmalloc(ksize, GFP_KERNEL);
  160. if (!keys) {
  161. kfree(keys_ex);
  162. return -ENOMEM;
  163. }
  164. }
  165. }
  166. spin_lock_bh(&p->tcf_lock);
  167. p->tcfp_flags = parm->flags;
  168. p->tcf_action = parm->action;
  169. if (keys) {
  170. kfree(p->tcfp_keys);
  171. p->tcfp_keys = keys;
  172. p->tcfp_nkeys = parm->nkeys;
  173. }
  174. memcpy(p->tcfp_keys, parm->keys, ksize);
  175. kfree(p->tcfp_keys_ex);
  176. p->tcfp_keys_ex = keys_ex;
  177. spin_unlock_bh(&p->tcf_lock);
  178. if (ret == ACT_P_CREATED)
  179. tcf_hash_insert(tn, *a);
  180. return ret;
  181. }
  182. static void tcf_pedit_cleanup(struct tc_action *a, int bind)
  183. {
  184. struct tcf_pedit *p = to_pedit(a);
  185. struct tc_pedit_key *keys = p->tcfp_keys;
  186. kfree(keys);
  187. kfree(p->tcfp_keys_ex);
  188. }
  189. static bool offset_valid(struct sk_buff *skb, int offset)
  190. {
  191. if (offset > 0 && offset > skb->len)
  192. return false;
  193. if (offset < 0 && -offset > skb_headroom(skb))
  194. return false;
  195. return true;
  196. }
  197. static int pedit_skb_hdr_offset(struct sk_buff *skb,
  198. enum pedit_header_type htype, int *hoffset)
  199. {
  200. int ret = -EINVAL;
  201. switch (htype) {
  202. case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH:
  203. if (skb_mac_header_was_set(skb)) {
  204. *hoffset = skb_mac_offset(skb);
  205. ret = 0;
  206. }
  207. break;
  208. case TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK:
  209. case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4:
  210. case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6:
  211. *hoffset = skb_network_offset(skb);
  212. ret = 0;
  213. break;
  214. case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP:
  215. case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP:
  216. if (skb_transport_header_was_set(skb)) {
  217. *hoffset = skb_transport_offset(skb);
  218. ret = 0;
  219. }
  220. break;
  221. default:
  222. ret = -EINVAL;
  223. break;
  224. };
  225. return ret;
  226. }
  227. static int tcf_pedit(struct sk_buff *skb, const struct tc_action *a,
  228. struct tcf_result *res)
  229. {
  230. struct tcf_pedit *p = to_pedit(a);
  231. int i;
  232. if (skb_unclone(skb, GFP_ATOMIC))
  233. return p->tcf_action;
  234. spin_lock(&p->tcf_lock);
  235. tcf_lastuse_update(&p->tcf_tm);
  236. if (p->tcfp_nkeys > 0) {
  237. struct tc_pedit_key *tkey = p->tcfp_keys;
  238. struct tcf_pedit_key_ex *tkey_ex = p->tcfp_keys_ex;
  239. enum pedit_header_type htype = TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK;
  240. enum pedit_cmd cmd = TCA_PEDIT_KEY_EX_CMD_SET;
  241. for (i = p->tcfp_nkeys; i > 0; i--, tkey++) {
  242. u32 *ptr, _data;
  243. int offset = tkey->off;
  244. int hoffset;
  245. u32 val;
  246. int rc;
  247. if (tkey_ex) {
  248. htype = tkey_ex->htype;
  249. cmd = tkey_ex->cmd;
  250. tkey_ex++;
  251. }
  252. rc = pedit_skb_hdr_offset(skb, htype, &hoffset);
  253. if (rc) {
  254. pr_info("tc filter pedit bad header type specified (0x%x)\n",
  255. htype);
  256. goto bad;
  257. }
  258. if (tkey->offmask) {
  259. char *d, _d;
  260. if (!offset_valid(skb, hoffset + tkey->at)) {
  261. pr_info("tc filter pedit 'at' offset %d out of bounds\n",
  262. hoffset + tkey->at);
  263. goto bad;
  264. }
  265. d = skb_header_pointer(skb, hoffset + tkey->at, 1,
  266. &_d);
  267. if (!d)
  268. goto bad;
  269. offset += (*d & tkey->offmask) >> tkey->shift;
  270. }
  271. if (offset % 4) {
  272. pr_info("tc filter pedit"
  273. " offset must be on 32 bit boundaries\n");
  274. goto bad;
  275. }
  276. if (!offset_valid(skb, hoffset + offset)) {
  277. pr_info("tc filter pedit offset %d out of bounds\n",
  278. hoffset + offset);
  279. goto bad;
  280. }
  281. ptr = skb_header_pointer(skb, hoffset + offset, 4, &_data);
  282. if (!ptr)
  283. goto bad;
  284. /* just do it, baby */
  285. switch (cmd) {
  286. case TCA_PEDIT_KEY_EX_CMD_SET:
  287. val = tkey->val;
  288. break;
  289. case TCA_PEDIT_KEY_EX_CMD_ADD:
  290. val = (*ptr + tkey->val) & ~tkey->mask;
  291. break;
  292. default:
  293. pr_info("tc filter pedit bad command (%d)\n",
  294. cmd);
  295. goto bad;
  296. }
  297. *ptr = ((*ptr & tkey->mask) ^ val);
  298. if (ptr == &_data)
  299. skb_store_bits(skb, hoffset + offset, ptr, 4);
  300. }
  301. goto done;
  302. } else
  303. WARN(1, "pedit BUG: index %d\n", p->tcf_index);
  304. bad:
  305. p->tcf_qstats.overlimits++;
  306. done:
  307. bstats_update(&p->tcf_bstats, skb);
  308. spin_unlock(&p->tcf_lock);
  309. return p->tcf_action;
  310. }
  311. static int tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,
  312. int bind, int ref)
  313. {
  314. unsigned char *b = skb_tail_pointer(skb);
  315. struct tcf_pedit *p = to_pedit(a);
  316. struct tc_pedit *opt;
  317. struct tcf_t t;
  318. int s;
  319. s = sizeof(*opt) + p->tcfp_nkeys * sizeof(struct tc_pedit_key);
  320. /* netlink spinlocks held above us - must use ATOMIC */
  321. opt = kzalloc(s, GFP_ATOMIC);
  322. if (unlikely(!opt))
  323. return -ENOBUFS;
  324. memcpy(opt->keys, p->tcfp_keys,
  325. p->tcfp_nkeys * sizeof(struct tc_pedit_key));
  326. opt->index = p->tcf_index;
  327. opt->nkeys = p->tcfp_nkeys;
  328. opt->flags = p->tcfp_flags;
  329. opt->action = p->tcf_action;
  330. opt->refcnt = p->tcf_refcnt - ref;
  331. opt->bindcnt = p->tcf_bindcnt - bind;
  332. if (p->tcfp_keys_ex) {
  333. tcf_pedit_key_ex_dump(skb, p->tcfp_keys_ex, p->tcfp_nkeys);
  334. if (nla_put(skb, TCA_PEDIT_PARMS_EX, s, opt))
  335. goto nla_put_failure;
  336. } else {
  337. if (nla_put(skb, TCA_PEDIT_PARMS, s, opt))
  338. goto nla_put_failure;
  339. }
  340. tcf_tm_dump(&t, &p->tcf_tm);
  341. if (nla_put_64bit(skb, TCA_PEDIT_TM, sizeof(t), &t, TCA_PEDIT_PAD))
  342. goto nla_put_failure;
  343. kfree(opt);
  344. return skb->len;
  345. nla_put_failure:
  346. nlmsg_trim(skb, b);
  347. kfree(opt);
  348. return -1;
  349. }
  350. static int tcf_pedit_walker(struct net *net, struct sk_buff *skb,
  351. struct netlink_callback *cb, int type,
  352. const struct tc_action_ops *ops)
  353. {
  354. struct tc_action_net *tn = net_generic(net, pedit_net_id);
  355. return tcf_generic_walker(tn, skb, cb, type, ops);
  356. }
  357. static int tcf_pedit_search(struct net *net, struct tc_action **a, u32 index)
  358. {
  359. struct tc_action_net *tn = net_generic(net, pedit_net_id);
  360. return tcf_hash_search(tn, a, index);
  361. }
  362. static struct tc_action_ops act_pedit_ops = {
  363. .kind = "pedit",
  364. .type = TCA_ACT_PEDIT,
  365. .owner = THIS_MODULE,
  366. .act = tcf_pedit,
  367. .dump = tcf_pedit_dump,
  368. .cleanup = tcf_pedit_cleanup,
  369. .init = tcf_pedit_init,
  370. .walk = tcf_pedit_walker,
  371. .lookup = tcf_pedit_search,
  372. .size = sizeof(struct tcf_pedit),
  373. };
  374. static __net_init int pedit_init_net(struct net *net)
  375. {
  376. struct tc_action_net *tn = net_generic(net, pedit_net_id);
  377. return tc_action_net_init(tn, &act_pedit_ops, PEDIT_TAB_MASK);
  378. }
  379. static void __net_exit pedit_exit_net(struct net *net)
  380. {
  381. struct tc_action_net *tn = net_generic(net, pedit_net_id);
  382. tc_action_net_exit(tn);
  383. }
  384. static struct pernet_operations pedit_net_ops = {
  385. .init = pedit_init_net,
  386. .exit = pedit_exit_net,
  387. .id = &pedit_net_id,
  388. .size = sizeof(struct tc_action_net),
  389. };
  390. MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
  391. MODULE_DESCRIPTION("Generic Packet Editor actions");
  392. MODULE_LICENSE("GPL");
  393. static int __init pedit_init_module(void)
  394. {
  395. return tcf_register_action(&act_pedit_ops, &pedit_net_ops);
  396. }
  397. static void __exit pedit_cleanup_module(void)
  398. {
  399. tcf_unregister_action(&act_pedit_ops, &pedit_net_ops);
  400. }
  401. module_init(pedit_init_module);
  402. module_exit(pedit_cleanup_module);