act_pedit.c 11 KB

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