pedit.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 **pskb, struct tc_action *a)
  120. {
  121. struct tcf_pedit *p = PRIV(a, pedit);
  122. struct sk_buff *skb = *pskb;
  123. int i, munged = 0;
  124. u8 *pptr;
  125. if (!(skb->tc_verd & TC_OK2MUNGE)) {
  126. /* should we set skb->cloned? */
  127. if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
  128. return p->action;
  129. }
  130. }
  131. pptr = skb->nh.raw;
  132. spin_lock(&p->lock);
  133. p->tm.lastuse = jiffies;
  134. if (p->nkeys > 0) {
  135. struct tc_pedit_key *tkey = p->keys;
  136. for (i = p->nkeys; i > 0; i--, tkey++) {
  137. u32 *ptr;
  138. int offset = tkey->off;
  139. if (tkey->offmask) {
  140. if (skb->len > tkey->at) {
  141. char *j = pptr + tkey->at;
  142. offset += ((*j & tkey->offmask) >>
  143. tkey->shift);
  144. } else {
  145. goto bad;
  146. }
  147. }
  148. if (offset % 4) {
  149. printk("offset must be on 32 bit boundaries\n");
  150. goto bad;
  151. }
  152. if (skb->len < 0 || (offset > 0 && offset > skb->len)) {
  153. printk("offset %d cant exceed pkt length %d\n",
  154. offset, skb->len);
  155. goto bad;
  156. }
  157. ptr = (u32 *)(pptr+offset);
  158. /* just do it, baby */
  159. *ptr = ((*ptr & tkey->mask) ^ tkey->val);
  160. munged++;
  161. }
  162. if (munged)
  163. skb->tc_verd = SET_TC_MUNGED(skb->tc_verd);
  164. goto done;
  165. } else {
  166. printk("pedit BUG: index %d\n",p->index);
  167. }
  168. bad:
  169. p->qstats.overlimits++;
  170. done:
  171. p->bstats.bytes += skb->len;
  172. p->bstats.packets++;
  173. spin_unlock(&p->lock);
  174. return p->action;
  175. }
  176. static int
  177. tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,int bind, int ref)
  178. {
  179. unsigned char *b = skb->tail;
  180. struct tc_pedit *opt;
  181. struct tcf_pedit *p = PRIV(a, pedit);
  182. struct tcf_t t;
  183. int s;
  184. s = sizeof(*opt) + p->nkeys * sizeof(struct tc_pedit_key);
  185. /* netlink spinlocks held above us - must use ATOMIC */
  186. opt = kmalloc(s, GFP_ATOMIC);
  187. if (opt == NULL)
  188. return -ENOBUFS;
  189. memset(opt, 0, s);
  190. memcpy(opt->keys, p->keys, p->nkeys * sizeof(struct tc_pedit_key));
  191. opt->index = p->index;
  192. opt->nkeys = p->nkeys;
  193. opt->flags = p->flags;
  194. opt->action = p->action;
  195. opt->refcnt = p->refcnt - ref;
  196. opt->bindcnt = p->bindcnt - bind;
  197. #ifdef PEDIT_DEB
  198. {
  199. /* Debug - get rid of later */
  200. int i;
  201. struct tc_pedit_key *key = opt->keys;
  202. for (i=0; i<opt->nkeys; i++, key++) {
  203. printk( "\n key #%d",i);
  204. printk( " at %d: val %08x mask %08x",
  205. (unsigned int)key->off,
  206. (unsigned int)key->val,
  207. (unsigned int)key->mask);
  208. }
  209. }
  210. #endif
  211. RTA_PUT(skb, TCA_PEDIT_PARMS, s, opt);
  212. t.install = jiffies_to_clock_t(jiffies - p->tm.install);
  213. t.lastuse = jiffies_to_clock_t(jiffies - p->tm.lastuse);
  214. t.expires = jiffies_to_clock_t(p->tm.expires);
  215. RTA_PUT(skb, TCA_PEDIT_TM, sizeof(t), &t);
  216. return skb->len;
  217. rtattr_failure:
  218. skb_trim(skb, b - skb->data);
  219. return -1;
  220. }
  221. static
  222. struct tc_action_ops act_pedit_ops = {
  223. .kind = "pedit",
  224. .type = TCA_ACT_PEDIT,
  225. .capab = TCA_CAP_NONE,
  226. .owner = THIS_MODULE,
  227. .act = tcf_pedit,
  228. .dump = tcf_pedit_dump,
  229. .cleanup = tcf_pedit_cleanup,
  230. .lookup = tcf_hash_search,
  231. .init = tcf_pedit_init,
  232. .walk = tcf_generic_walker
  233. };
  234. MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
  235. MODULE_DESCRIPTION("Generic Packet Editor actions");
  236. MODULE_LICENSE("GPL");
  237. static int __init
  238. pedit_init_module(void)
  239. {
  240. return tcf_register_action(&act_pedit_ops);
  241. }
  242. static void __exit
  243. pedit_cleanup_module(void)
  244. {
  245. tcf_unregister_action(&act_pedit_ops);
  246. }
  247. module_init(pedit_init_module);
  248. module_exit(pedit_cleanup_module);