act_pedit.c 6.0 KB

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