act_pedit.c 5.5 KB

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