act_pedit.c 12 KB

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