act_pedit.c 6.2 KB

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