act_pedit.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * net/sched/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 <net/netlink.h>
  20. #include <net/pkt_sched.h>
  21. #include <linux/tc_act/tc_pedit.h>
  22. #include <net/tc_act/tc_pedit.h>
  23. #define PEDIT_TAB_MASK 15
  24. static struct tcf_common *tcf_pedit_ht[PEDIT_TAB_MASK + 1];
  25. static u32 pedit_idx_gen;
  26. static DEFINE_RWLOCK(pedit_lock);
  27. static struct tcf_hashinfo pedit_hash_info = {
  28. .htab = tcf_pedit_ht,
  29. .hmask = PEDIT_TAB_MASK,
  30. .lock = &pedit_lock,
  31. };
  32. static int tcf_pedit_init(struct rtattr *rta, struct rtattr *est,
  33. struct tc_action *a, int ovr, int bind)
  34. {
  35. struct rtattr *tb[TCA_PEDIT_MAX];
  36. struct tc_pedit *parm;
  37. int ret = 0;
  38. struct tcf_pedit *p;
  39. struct tcf_common *pc;
  40. struct tc_pedit_key *keys = NULL;
  41. int ksize;
  42. if (rta == NULL || rtattr_parse_nested(tb, TCA_PEDIT_MAX, rta) < 0)
  43. return -EINVAL;
  44. if (tb[TCA_PEDIT_PARMS - 1] == NULL ||
  45. RTA_PAYLOAD(tb[TCA_PEDIT_PARMS-1]) < sizeof(*parm))
  46. return -EINVAL;
  47. parm = RTA_DATA(tb[TCA_PEDIT_PARMS-1]);
  48. ksize = parm->nkeys * sizeof(struct tc_pedit_key);
  49. if (RTA_PAYLOAD(tb[TCA_PEDIT_PARMS-1]) < sizeof(*parm) + ksize)
  50. return -EINVAL;
  51. pc = tcf_hash_check(parm->index, a, bind, &pedit_hash_info);
  52. if (!pc) {
  53. if (!parm->nkeys)
  54. return -EINVAL;
  55. pc = tcf_hash_create(parm->index, est, a, sizeof(*p), bind,
  56. &pedit_idx_gen, &pedit_hash_info);
  57. if (unlikely(!pc))
  58. return -ENOMEM;
  59. p = to_pedit(pc);
  60. keys = kmalloc(ksize, GFP_KERNEL);
  61. if (keys == NULL) {
  62. kfree(pc);
  63. return -ENOMEM;
  64. }
  65. ret = ACT_P_CREATED;
  66. } else {
  67. p = to_pedit(pc);
  68. if (!ovr) {
  69. tcf_hash_release(pc, bind, &pedit_hash_info);
  70. return -EEXIST;
  71. }
  72. if (p->tcfp_nkeys && p->tcfp_nkeys != parm->nkeys) {
  73. keys = kmalloc(ksize, GFP_KERNEL);
  74. if (keys == NULL)
  75. return -ENOMEM;
  76. }
  77. }
  78. spin_lock_bh(&p->tcf_lock);
  79. p->tcfp_flags = parm->flags;
  80. p->tcf_action = parm->action;
  81. if (keys) {
  82. kfree(p->tcfp_keys);
  83. p->tcfp_keys = keys;
  84. p->tcfp_nkeys = parm->nkeys;
  85. }
  86. memcpy(p->tcfp_keys, parm->keys, ksize);
  87. spin_unlock_bh(&p->tcf_lock);
  88. if (ret == ACT_P_CREATED)
  89. tcf_hash_insert(pc, &pedit_hash_info);
  90. return ret;
  91. }
  92. static int tcf_pedit_cleanup(struct tc_action *a, int bind)
  93. {
  94. struct tcf_pedit *p = a->priv;
  95. if (p) {
  96. struct tc_pedit_key *keys = p->tcfp_keys;
  97. if (tcf_hash_release(&p->common, bind, &pedit_hash_info)) {
  98. kfree(keys);
  99. return 1;
  100. }
  101. }
  102. return 0;
  103. }
  104. static int tcf_pedit(struct sk_buff *skb, struct tc_action *a,
  105. struct tcf_result *res)
  106. {
  107. struct tcf_pedit *p = a->priv;
  108. int i, munged = 0;
  109. u8 *pptr;
  110. if (!(skb->tc_verd & TC_OK2MUNGE)) {
  111. /* should we set skb->cloned? */
  112. if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
  113. return p->tcf_action;
  114. }
  115. }
  116. pptr = skb_network_header(skb);
  117. spin_lock(&p->tcf_lock);
  118. p->tcf_tm.lastuse = jiffies;
  119. if (p->tcfp_nkeys > 0) {
  120. struct tc_pedit_key *tkey = p->tcfp_keys;
  121. for (i = p->tcfp_nkeys; i > 0; i--, tkey++) {
  122. u32 *ptr;
  123. int offset = tkey->off;
  124. if (tkey->offmask) {
  125. if (skb->len > tkey->at) {
  126. char *j = pptr + tkey->at;
  127. offset += ((*j & tkey->offmask) >>
  128. tkey->shift);
  129. } else {
  130. goto bad;
  131. }
  132. }
  133. if (offset % 4) {
  134. printk("offset must be on 32 bit boundaries\n");
  135. goto bad;
  136. }
  137. if (offset > 0 && offset > skb->len) {
  138. printk("offset %d cant exceed pkt length %d\n",
  139. offset, skb->len);
  140. goto bad;
  141. }
  142. ptr = (u32 *)(pptr+offset);
  143. /* just do it, baby */
  144. *ptr = ((*ptr & tkey->mask) ^ tkey->val);
  145. munged++;
  146. }
  147. if (munged)
  148. skb->tc_verd = SET_TC_MUNGED(skb->tc_verd);
  149. goto done;
  150. } else {
  151. printk("pedit BUG: index %d\n", p->tcf_index);
  152. }
  153. bad:
  154. p->tcf_qstats.overlimits++;
  155. done:
  156. p->tcf_bstats.bytes += skb->len;
  157. p->tcf_bstats.packets++;
  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. RTA_PUT(skb, TCA_PEDIT_PARMS, s, opt);
  183. t.install = jiffies_to_clock_t(jiffies - p->tcf_tm.install);
  184. t.lastuse = jiffies_to_clock_t(jiffies - p->tcf_tm.lastuse);
  185. t.expires = jiffies_to_clock_t(p->tcf_tm.expires);
  186. RTA_PUT(skb, TCA_PEDIT_TM, sizeof(t), &t);
  187. kfree(opt);
  188. return skb->len;
  189. rtattr_failure:
  190. nlmsg_trim(skb, b);
  191. kfree(opt);
  192. return -1;
  193. }
  194. static struct tc_action_ops act_pedit_ops = {
  195. .kind = "pedit",
  196. .hinfo = &pedit_hash_info,
  197. .type = TCA_ACT_PEDIT,
  198. .capab = TCA_CAP_NONE,
  199. .owner = THIS_MODULE,
  200. .act = tcf_pedit,
  201. .dump = tcf_pedit_dump,
  202. .cleanup = tcf_pedit_cleanup,
  203. .lookup = tcf_hash_search,
  204. .init = tcf_pedit_init,
  205. .walk = tcf_generic_walker
  206. };
  207. MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
  208. MODULE_DESCRIPTION("Generic Packet Editor actions");
  209. MODULE_LICENSE("GPL");
  210. static int __init pedit_init_module(void)
  211. {
  212. return tcf_register_action(&act_pedit_ops);
  213. }
  214. static void __exit pedit_cleanup_module(void)
  215. {
  216. tcf_unregister_action(&act_pedit_ops);
  217. }
  218. module_init(pedit_init_module);
  219. module_exit(pedit_cleanup_module);