act_pedit.c 6.3 KB

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