act_pedit.c 6.1 KB

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