act_pedit.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #define PEDIT_TAB_MASK 15
  25. static const struct nla_policy pedit_policy[TCA_PEDIT_MAX + 1] = {
  26. [TCA_PEDIT_PARMS] = { .len = sizeof(struct tc_pedit) },
  27. };
  28. static int tcf_pedit_init(struct net *net, struct nlattr *nla,
  29. struct nlattr *est, struct tc_action *a,
  30. int ovr, int bind)
  31. {
  32. struct nlattr *tb[TCA_PEDIT_MAX + 1];
  33. struct tc_pedit *parm;
  34. int ret = 0, err;
  35. struct tcf_pedit *p;
  36. struct tc_pedit_key *keys = NULL;
  37. int ksize;
  38. if (nla == NULL)
  39. return -EINVAL;
  40. err = nla_parse_nested(tb, TCA_PEDIT_MAX, nla, pedit_policy);
  41. if (err < 0)
  42. return err;
  43. if (tb[TCA_PEDIT_PARMS] == NULL)
  44. return -EINVAL;
  45. parm = nla_data(tb[TCA_PEDIT_PARMS]);
  46. ksize = parm->nkeys * sizeof(struct tc_pedit_key);
  47. if (nla_len(tb[TCA_PEDIT_PARMS]) < sizeof(*parm) + ksize)
  48. return -EINVAL;
  49. if (!tcf_hash_check(parm->index, a, bind)) {
  50. if (!parm->nkeys)
  51. return -EINVAL;
  52. ret = tcf_hash_create(parm->index, est, a, sizeof(*p),
  53. bind, false);
  54. if (ret)
  55. return ret;
  56. p = to_pedit(a);
  57. keys = kmalloc(ksize, GFP_KERNEL);
  58. if (keys == NULL) {
  59. tcf_hash_cleanup(a, est);
  60. return -ENOMEM;
  61. }
  62. ret = ACT_P_CREATED;
  63. } else {
  64. if (bind)
  65. return 0;
  66. tcf_hash_release(a, bind);
  67. if (!ovr)
  68. return -EEXIST;
  69. p = to_pedit(a);
  70. if (p->tcfp_nkeys && p->tcfp_nkeys != parm->nkeys) {
  71. keys = kmalloc(ksize, GFP_KERNEL);
  72. if (keys == NULL)
  73. return -ENOMEM;
  74. }
  75. }
  76. spin_lock_bh(&p->tcf_lock);
  77. p->tcfp_flags = parm->flags;
  78. p->tcf_action = parm->action;
  79. if (keys) {
  80. kfree(p->tcfp_keys);
  81. p->tcfp_keys = keys;
  82. p->tcfp_nkeys = parm->nkeys;
  83. }
  84. memcpy(p->tcfp_keys, parm->keys, ksize);
  85. spin_unlock_bh(&p->tcf_lock);
  86. if (ret == ACT_P_CREATED)
  87. tcf_hash_insert(a);
  88. return ret;
  89. }
  90. static void tcf_pedit_cleanup(struct tc_action *a, int bind)
  91. {
  92. struct tcf_pedit *p = a->priv;
  93. struct tc_pedit_key *keys = p->tcfp_keys;
  94. kfree(keys);
  95. }
  96. static bool offset_valid(struct sk_buff *skb, int offset)
  97. {
  98. if (offset > 0 && offset > skb->len)
  99. return false;
  100. if (offset < 0 && -offset > skb_headroom(skb))
  101. return false;
  102. return true;
  103. }
  104. static int tcf_pedit(struct sk_buff *skb, const struct tc_action *a,
  105. struct tcf_result *res)
  106. {
  107. struct tcf_pedit *p = a->priv;
  108. int i;
  109. unsigned int off;
  110. if (skb_unclone(skb, GFP_ATOMIC))
  111. return p->tcf_action;
  112. off = skb_network_offset(skb);
  113. spin_lock(&p->tcf_lock);
  114. p->tcf_tm.lastuse = jiffies;
  115. if (p->tcfp_nkeys > 0) {
  116. struct tc_pedit_key *tkey = p->tcfp_keys;
  117. for (i = p->tcfp_nkeys; i > 0; i--, tkey++) {
  118. u32 *ptr, _data;
  119. int offset = tkey->off;
  120. if (tkey->offmask) {
  121. char *d, _d;
  122. if (!offset_valid(skb, off + tkey->at)) {
  123. pr_info("tc filter pedit 'at' offset %d out of bounds\n",
  124. off + tkey->at);
  125. goto bad;
  126. }
  127. d = skb_header_pointer(skb, off + tkey->at, 1,
  128. &_d);
  129. if (!d)
  130. goto bad;
  131. offset += (*d & tkey->offmask) >> tkey->shift;
  132. }
  133. if (offset % 4) {
  134. pr_info("tc filter pedit"
  135. " offset must be on 32 bit boundaries\n");
  136. goto bad;
  137. }
  138. if (!offset_valid(skb, off + offset)) {
  139. pr_info("tc filter pedit offset %d out of bounds\n",
  140. offset);
  141. goto bad;
  142. }
  143. ptr = skb_header_pointer(skb, off + offset, 4, &_data);
  144. if (!ptr)
  145. goto bad;
  146. /* just do it, baby */
  147. *ptr = ((*ptr & tkey->mask) ^ tkey->val);
  148. if (ptr == &_data)
  149. skb_store_bits(skb, off + offset, ptr, 4);
  150. }
  151. goto done;
  152. } else
  153. WARN(1, "pedit BUG: index %d\n", p->tcf_index);
  154. bad:
  155. p->tcf_qstats.overlimits++;
  156. done:
  157. bstats_update(&p->tcf_bstats, skb);
  158. spin_unlock(&p->tcf_lock);
  159. return p->tcf_action;
  160. }
  161. static int tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,
  162. int bind, int ref)
  163. {
  164. unsigned char *b = skb_tail_pointer(skb);
  165. struct tcf_pedit *p = a->priv;
  166. struct tc_pedit *opt;
  167. struct tcf_t t;
  168. int s;
  169. s = sizeof(*opt) + p->tcfp_nkeys * sizeof(struct tc_pedit_key);
  170. /* netlink spinlocks held above us - must use ATOMIC */
  171. opt = kzalloc(s, GFP_ATOMIC);
  172. if (unlikely(!opt))
  173. return -ENOBUFS;
  174. memcpy(opt->keys, p->tcfp_keys,
  175. p->tcfp_nkeys * sizeof(struct tc_pedit_key));
  176. opt->index = p->tcf_index;
  177. opt->nkeys = p->tcfp_nkeys;
  178. opt->flags = p->tcfp_flags;
  179. opt->action = p->tcf_action;
  180. opt->refcnt = p->tcf_refcnt - ref;
  181. opt->bindcnt = p->tcf_bindcnt - bind;
  182. if (nla_put(skb, TCA_PEDIT_PARMS, s, opt))
  183. goto nla_put_failure;
  184. t.install = jiffies_to_clock_t(jiffies - p->tcf_tm.install);
  185. t.lastuse = jiffies_to_clock_t(jiffies - p->tcf_tm.lastuse);
  186. t.expires = jiffies_to_clock_t(p->tcf_tm.expires);
  187. if (nla_put(skb, TCA_PEDIT_TM, sizeof(t), &t))
  188. goto nla_put_failure;
  189. kfree(opt);
  190. return skb->len;
  191. nla_put_failure:
  192. nlmsg_trim(skb, b);
  193. kfree(opt);
  194. return -1;
  195. }
  196. static struct tc_action_ops act_pedit_ops = {
  197. .kind = "pedit",
  198. .type = TCA_ACT_PEDIT,
  199. .owner = THIS_MODULE,
  200. .act = tcf_pedit,
  201. .dump = tcf_pedit_dump,
  202. .cleanup = tcf_pedit_cleanup,
  203. .init = tcf_pedit_init,
  204. };
  205. MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
  206. MODULE_DESCRIPTION("Generic Packet Editor actions");
  207. MODULE_LICENSE("GPL");
  208. static int __init pedit_init_module(void)
  209. {
  210. return tcf_register_action(&act_pedit_ops, PEDIT_TAB_MASK);
  211. }
  212. static void __exit pedit_cleanup_module(void)
  213. {
  214. tcf_unregister_action(&act_pedit_ops);
  215. }
  216. module_init(pedit_init_module);
  217. module_exit(pedit_cleanup_module);