act_pedit.c 11 KB

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