act_pedit.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 int tcf_pedit(struct sk_buff *skb, const struct tc_action *a,
  97. struct tcf_result *res)
  98. {
  99. struct tcf_pedit *p = a->priv;
  100. int i;
  101. unsigned int off;
  102. if (skb_unclone(skb, GFP_ATOMIC))
  103. return p->tcf_action;
  104. off = skb_network_offset(skb);
  105. spin_lock(&p->tcf_lock);
  106. p->tcf_tm.lastuse = jiffies;
  107. if (p->tcfp_nkeys > 0) {
  108. struct tc_pedit_key *tkey = p->tcfp_keys;
  109. for (i = p->tcfp_nkeys; i > 0; i--, tkey++) {
  110. u32 *ptr, _data;
  111. int offset = tkey->off;
  112. if (tkey->offmask) {
  113. char *d, _d;
  114. d = skb_header_pointer(skb, off + tkey->at, 1,
  115. &_d);
  116. if (!d)
  117. goto bad;
  118. offset += (*d & tkey->offmask) >> tkey->shift;
  119. }
  120. if (offset % 4) {
  121. pr_info("tc filter pedit"
  122. " offset must be on 32 bit boundaries\n");
  123. goto bad;
  124. }
  125. if (offset > 0 && offset > skb->len) {
  126. pr_info("tc filter pedit"
  127. " offset %d can't exceed pkt length %d\n",
  128. offset, skb->len);
  129. goto bad;
  130. }
  131. ptr = skb_header_pointer(skb, off + offset, 4, &_data);
  132. if (!ptr)
  133. goto bad;
  134. /* just do it, baby */
  135. *ptr = ((*ptr & tkey->mask) ^ tkey->val);
  136. if (ptr == &_data)
  137. skb_store_bits(skb, off + offset, ptr, 4);
  138. }
  139. goto done;
  140. } else
  141. WARN(1, "pedit BUG: index %d\n", p->tcf_index);
  142. bad:
  143. p->tcf_qstats.overlimits++;
  144. done:
  145. bstats_update(&p->tcf_bstats, skb);
  146. spin_unlock(&p->tcf_lock);
  147. return p->tcf_action;
  148. }
  149. static int tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,
  150. int bind, int ref)
  151. {
  152. unsigned char *b = skb_tail_pointer(skb);
  153. struct tcf_pedit *p = a->priv;
  154. struct tc_pedit *opt;
  155. struct tcf_t t;
  156. int s;
  157. s = sizeof(*opt) + p->tcfp_nkeys * sizeof(struct tc_pedit_key);
  158. /* netlink spinlocks held above us - must use ATOMIC */
  159. opt = kzalloc(s, GFP_ATOMIC);
  160. if (unlikely(!opt))
  161. return -ENOBUFS;
  162. memcpy(opt->keys, p->tcfp_keys,
  163. p->tcfp_nkeys * sizeof(struct tc_pedit_key));
  164. opt->index = p->tcf_index;
  165. opt->nkeys = p->tcfp_nkeys;
  166. opt->flags = p->tcfp_flags;
  167. opt->action = p->tcf_action;
  168. opt->refcnt = p->tcf_refcnt - ref;
  169. opt->bindcnt = p->tcf_bindcnt - bind;
  170. if (nla_put(skb, TCA_PEDIT_PARMS, s, opt))
  171. goto nla_put_failure;
  172. t.install = jiffies_to_clock_t(jiffies - p->tcf_tm.install);
  173. t.lastuse = jiffies_to_clock_t(jiffies - p->tcf_tm.lastuse);
  174. t.expires = jiffies_to_clock_t(p->tcf_tm.expires);
  175. if (nla_put(skb, TCA_PEDIT_TM, sizeof(t), &t))
  176. goto nla_put_failure;
  177. kfree(opt);
  178. return skb->len;
  179. nla_put_failure:
  180. nlmsg_trim(skb, b);
  181. kfree(opt);
  182. return -1;
  183. }
  184. static struct tc_action_ops act_pedit_ops = {
  185. .kind = "pedit",
  186. .type = TCA_ACT_PEDIT,
  187. .owner = THIS_MODULE,
  188. .act = tcf_pedit,
  189. .dump = tcf_pedit_dump,
  190. .cleanup = tcf_pedit_cleanup,
  191. .init = tcf_pedit_init,
  192. };
  193. MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
  194. MODULE_DESCRIPTION("Generic Packet Editor actions");
  195. MODULE_LICENSE("GPL");
  196. static int __init pedit_init_module(void)
  197. {
  198. return tcf_register_action(&act_pedit_ops, PEDIT_TAB_MASK);
  199. }
  200. static void __exit pedit_cleanup_module(void)
  201. {
  202. tcf_unregister_action(&act_pedit_ops);
  203. }
  204. module_init(pedit_init_module);
  205. module_exit(pedit_cleanup_module);