act_pedit.c 6.1 KB

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