act_pedit.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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_DEB 1
  35. /* use generic hash table */
  36. #define MY_TAB_SIZE 16
  37. #define MY_TAB_MASK 15
  38. static u32 idx_gen;
  39. static struct tcf_pedit *tcf_pedit_ht[MY_TAB_SIZE];
  40. static DEFINE_RWLOCK(pedit_lock);
  41. #define tcf_st tcf_pedit
  42. #define tc_st tc_pedit
  43. #define tcf_t_lock pedit_lock
  44. #define tcf_ht tcf_pedit_ht
  45. #define CONFIG_NET_ACT_INIT 1
  46. #include <net/pkt_act.h>
  47. static int
  48. tcf_pedit_init(struct rtattr *rta, struct rtattr *est, struct tc_action *a,
  49. int ovr, int bind)
  50. {
  51. struct rtattr *tb[TCA_PEDIT_MAX];
  52. struct tc_pedit *parm;
  53. int ret = 0;
  54. struct tcf_pedit *p;
  55. struct tc_pedit_key *keys = NULL;
  56. int ksize;
  57. if (rta == NULL || rtattr_parse_nested(tb, TCA_PEDIT_MAX, rta) < 0)
  58. return -EINVAL;
  59. if (tb[TCA_PEDIT_PARMS - 1] == NULL ||
  60. RTA_PAYLOAD(tb[TCA_PEDIT_PARMS-1]) < sizeof(*parm))
  61. return -EINVAL;
  62. parm = RTA_DATA(tb[TCA_PEDIT_PARMS-1]);
  63. ksize = parm->nkeys * sizeof(struct tc_pedit_key);
  64. if (RTA_PAYLOAD(tb[TCA_PEDIT_PARMS-1]) < sizeof(*parm) + ksize)
  65. return -EINVAL;
  66. p = tcf_hash_check(parm->index, a, ovr, bind);
  67. if (p == NULL) {
  68. if (!parm->nkeys)
  69. return -EINVAL;
  70. p = tcf_hash_create(parm->index, est, a, sizeof(*p), ovr, bind);
  71. if (p == NULL)
  72. return -ENOMEM;
  73. keys = kmalloc(ksize, GFP_KERNEL);
  74. if (keys == NULL) {
  75. kfree(p);
  76. return -ENOMEM;
  77. }
  78. ret = ACT_P_CREATED;
  79. } else {
  80. if (!ovr) {
  81. tcf_hash_release(p, bind);
  82. return -EEXIST;
  83. }
  84. if (p->nkeys && p->nkeys != parm->nkeys) {
  85. keys = kmalloc(ksize, GFP_KERNEL);
  86. if (keys == NULL)
  87. return -ENOMEM;
  88. }
  89. }
  90. spin_lock_bh(&p->lock);
  91. p->flags = parm->flags;
  92. p->action = parm->action;
  93. if (keys) {
  94. kfree(p->keys);
  95. p->keys = keys;
  96. p->nkeys = parm->nkeys;
  97. }
  98. memcpy(p->keys, parm->keys, ksize);
  99. spin_unlock_bh(&p->lock);
  100. if (ret == ACT_P_CREATED)
  101. tcf_hash_insert(p);
  102. return ret;
  103. }
  104. static int
  105. tcf_pedit_cleanup(struct tc_action *a, int bind)
  106. {
  107. struct tcf_pedit *p = PRIV(a, pedit);
  108. if (p != NULL) {
  109. struct tc_pedit_key *keys = p->keys;
  110. if (tcf_hash_release(p, bind)) {
  111. kfree(keys);
  112. return 1;
  113. }
  114. }
  115. return 0;
  116. }
  117. static int
  118. tcf_pedit(struct sk_buff *skb, struct tc_action *a, struct tcf_result *res)
  119. {
  120. struct tcf_pedit *p = PRIV(a, pedit);
  121. int i, munged = 0;
  122. u8 *pptr;
  123. if (!(skb->tc_verd & TC_OK2MUNGE)) {
  124. /* should we set skb->cloned? */
  125. if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
  126. return p->action;
  127. }
  128. }
  129. pptr = skb->nh.raw;
  130. spin_lock(&p->lock);
  131. p->tm.lastuse = jiffies;
  132. if (p->nkeys > 0) {
  133. struct tc_pedit_key *tkey = p->keys;
  134. for (i = p->nkeys; i > 0; i--, tkey++) {
  135. u32 *ptr;
  136. int offset = tkey->off;
  137. if (tkey->offmask) {
  138. if (skb->len > tkey->at) {
  139. char *j = pptr + tkey->at;
  140. offset += ((*j & tkey->offmask) >>
  141. tkey->shift);
  142. } else {
  143. goto bad;
  144. }
  145. }
  146. if (offset % 4) {
  147. printk("offset must be on 32 bit boundaries\n");
  148. goto bad;
  149. }
  150. if (skb->len < 0 || (offset > 0 && offset > skb->len)) {
  151. printk("offset %d cant exceed pkt length %d\n",
  152. offset, skb->len);
  153. goto bad;
  154. }
  155. ptr = (u32 *)(pptr+offset);
  156. /* just do it, baby */
  157. *ptr = ((*ptr & tkey->mask) ^ tkey->val);
  158. munged++;
  159. }
  160. if (munged)
  161. skb->tc_verd = SET_TC_MUNGED(skb->tc_verd);
  162. goto done;
  163. } else {
  164. printk("pedit BUG: index %d\n",p->index);
  165. }
  166. bad:
  167. p->qstats.overlimits++;
  168. done:
  169. p->bstats.bytes += skb->len;
  170. p->bstats.packets++;
  171. spin_unlock(&p->lock);
  172. return p->action;
  173. }
  174. static int
  175. tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,int bind, int ref)
  176. {
  177. unsigned char *b = skb->tail;
  178. struct tc_pedit *opt;
  179. struct tcf_pedit *p = PRIV(a, pedit);
  180. struct tcf_t t;
  181. int s;
  182. s = sizeof(*opt) + p->nkeys * sizeof(struct tc_pedit_key);
  183. /* netlink spinlocks held above us - must use ATOMIC */
  184. opt = kzalloc(s, GFP_ATOMIC);
  185. if (opt == NULL)
  186. return -ENOBUFS;
  187. memcpy(opt->keys, p->keys, p->nkeys * sizeof(struct tc_pedit_key));
  188. opt->index = p->index;
  189. opt->nkeys = p->nkeys;
  190. opt->flags = p->flags;
  191. opt->action = p->action;
  192. opt->refcnt = p->refcnt - ref;
  193. opt->bindcnt = p->bindcnt - bind;
  194. #ifdef PEDIT_DEB
  195. {
  196. /* Debug - get rid of later */
  197. int i;
  198. struct tc_pedit_key *key = opt->keys;
  199. for (i=0; i<opt->nkeys; i++, key++) {
  200. printk( "\n key #%d",i);
  201. printk( " at %d: val %08x mask %08x",
  202. (unsigned int)key->off,
  203. (unsigned int)key->val,
  204. (unsigned int)key->mask);
  205. }
  206. }
  207. #endif
  208. RTA_PUT(skb, TCA_PEDIT_PARMS, s, opt);
  209. t.install = jiffies_to_clock_t(jiffies - p->tm.install);
  210. t.lastuse = jiffies_to_clock_t(jiffies - p->tm.lastuse);
  211. t.expires = jiffies_to_clock_t(p->tm.expires);
  212. RTA_PUT(skb, TCA_PEDIT_TM, sizeof(t), &t);
  213. kfree(opt);
  214. return skb->len;
  215. rtattr_failure:
  216. skb_trim(skb, b - skb->data);
  217. kfree(opt);
  218. return -1;
  219. }
  220. static
  221. struct tc_action_ops act_pedit_ops = {
  222. .kind = "pedit",
  223. .type = TCA_ACT_PEDIT,
  224. .capab = TCA_CAP_NONE,
  225. .owner = THIS_MODULE,
  226. .act = tcf_pedit,
  227. .dump = tcf_pedit_dump,
  228. .cleanup = tcf_pedit_cleanup,
  229. .lookup = tcf_hash_search,
  230. .init = tcf_pedit_init,
  231. .walk = tcf_generic_walker
  232. };
  233. MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
  234. MODULE_DESCRIPTION("Generic Packet Editor actions");
  235. MODULE_LICENSE("GPL");
  236. static int __init
  237. pedit_init_module(void)
  238. {
  239. return tcf_register_action(&act_pedit_ops);
  240. }
  241. static void __exit
  242. pedit_cleanup_module(void)
  243. {
  244. tcf_unregister_action(&act_pedit_ops);
  245. }
  246. module_init(pedit_init_module);
  247. module_exit(pedit_cleanup_module);