act_pedit.c 11 KB

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