act_connmark.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * net/sched/act_connmark.c netfilter connmark retriever action
  3. * skb mark is over-written
  4. *
  5. * Copyright (c) 2011 Felix Fietkau <nbd@openwrt.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/rtnetlink.h>
  17. #include <linux/pkt_cls.h>
  18. #include <linux/ip.h>
  19. #include <linux/ipv6.h>
  20. #include <net/netlink.h>
  21. #include <net/pkt_sched.h>
  22. #include <net/act_api.h>
  23. #include <uapi/linux/tc_act/tc_connmark.h>
  24. #include <net/tc_act/tc_connmark.h>
  25. #include <net/netfilter/nf_conntrack.h>
  26. #include <net/netfilter/nf_conntrack_core.h>
  27. #include <net/netfilter/nf_conntrack_zones.h>
  28. #define CONNMARK_TAB_MASK 3
  29. static unsigned int connmark_net_id;
  30. static struct tc_action_ops act_connmark_ops;
  31. static int tcf_connmark(struct sk_buff *skb, const struct tc_action *a,
  32. struct tcf_result *res)
  33. {
  34. const struct nf_conntrack_tuple_hash *thash;
  35. struct nf_conntrack_tuple tuple;
  36. enum ip_conntrack_info ctinfo;
  37. struct tcf_connmark_info *ca = to_connmark(a);
  38. struct nf_conntrack_zone zone;
  39. struct nf_conn *c;
  40. int proto;
  41. spin_lock(&ca->tcf_lock);
  42. tcf_lastuse_update(&ca->tcf_tm);
  43. bstats_update(&ca->tcf_bstats, skb);
  44. if (skb->protocol == htons(ETH_P_IP)) {
  45. if (skb->len < sizeof(struct iphdr))
  46. goto out;
  47. proto = NFPROTO_IPV4;
  48. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  49. if (skb->len < sizeof(struct ipv6hdr))
  50. goto out;
  51. proto = NFPROTO_IPV6;
  52. } else {
  53. goto out;
  54. }
  55. c = nf_ct_get(skb, &ctinfo);
  56. if (c) {
  57. skb->mark = c->mark;
  58. /* using overlimits stats to count how many packets marked */
  59. ca->tcf_qstats.overlimits++;
  60. goto out;
  61. }
  62. if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb),
  63. proto, ca->net, &tuple))
  64. goto out;
  65. zone.id = ca->zone;
  66. zone.dir = NF_CT_DEFAULT_ZONE_DIR;
  67. thash = nf_conntrack_find_get(ca->net, &zone, &tuple);
  68. if (!thash)
  69. goto out;
  70. c = nf_ct_tuplehash_to_ctrack(thash);
  71. /* using overlimits stats to count how many packets marked */
  72. ca->tcf_qstats.overlimits++;
  73. skb->mark = c->mark;
  74. nf_ct_put(c);
  75. out:
  76. spin_unlock(&ca->tcf_lock);
  77. return ca->tcf_action;
  78. }
  79. static const struct nla_policy connmark_policy[TCA_CONNMARK_MAX + 1] = {
  80. [TCA_CONNMARK_PARMS] = { .len = sizeof(struct tc_connmark) },
  81. };
  82. static int tcf_connmark_init(struct net *net, struct nlattr *nla,
  83. struct nlattr *est, struct tc_action **a,
  84. int ovr, int bind)
  85. {
  86. struct tc_action_net *tn = net_generic(net, connmark_net_id);
  87. struct nlattr *tb[TCA_CONNMARK_MAX + 1];
  88. struct tcf_connmark_info *ci;
  89. struct tc_connmark *parm;
  90. int ret = 0;
  91. if (!nla)
  92. return -EINVAL;
  93. ret = nla_parse_nested(tb, TCA_CONNMARK_MAX, nla, connmark_policy);
  94. if (ret < 0)
  95. return ret;
  96. parm = nla_data(tb[TCA_CONNMARK_PARMS]);
  97. if (!tcf_hash_check(tn, parm->index, a, bind)) {
  98. ret = tcf_hash_create(tn, parm->index, est, a,
  99. &act_connmark_ops, bind, false);
  100. if (ret)
  101. return ret;
  102. ci = to_connmark(*a);
  103. ci->tcf_action = parm->action;
  104. ci->net = net;
  105. ci->zone = parm->zone;
  106. tcf_hash_insert(tn, *a);
  107. ret = ACT_P_CREATED;
  108. } else {
  109. ci = to_connmark(*a);
  110. if (bind)
  111. return 0;
  112. tcf_hash_release(*a, bind);
  113. if (!ovr)
  114. return -EEXIST;
  115. /* replacing action and zone */
  116. ci->tcf_action = parm->action;
  117. ci->zone = parm->zone;
  118. }
  119. return ret;
  120. }
  121. static inline int tcf_connmark_dump(struct sk_buff *skb, struct tc_action *a,
  122. int bind, int ref)
  123. {
  124. unsigned char *b = skb_tail_pointer(skb);
  125. struct tcf_connmark_info *ci = to_connmark(a);
  126. struct tc_connmark opt = {
  127. .index = ci->tcf_index,
  128. .refcnt = ci->tcf_refcnt - ref,
  129. .bindcnt = ci->tcf_bindcnt - bind,
  130. .action = ci->tcf_action,
  131. .zone = ci->zone,
  132. };
  133. struct tcf_t t;
  134. if (nla_put(skb, TCA_CONNMARK_PARMS, sizeof(opt), &opt))
  135. goto nla_put_failure;
  136. tcf_tm_dump(&t, &ci->tcf_tm);
  137. if (nla_put_64bit(skb, TCA_CONNMARK_TM, sizeof(t), &t,
  138. TCA_CONNMARK_PAD))
  139. goto nla_put_failure;
  140. return skb->len;
  141. nla_put_failure:
  142. nlmsg_trim(skb, b);
  143. return -1;
  144. }
  145. static int tcf_connmark_walker(struct net *net, struct sk_buff *skb,
  146. struct netlink_callback *cb, int type,
  147. const struct tc_action_ops *ops)
  148. {
  149. struct tc_action_net *tn = net_generic(net, connmark_net_id);
  150. return tcf_generic_walker(tn, skb, cb, type, ops);
  151. }
  152. static int tcf_connmark_search(struct net *net, struct tc_action **a, u32 index)
  153. {
  154. struct tc_action_net *tn = net_generic(net, connmark_net_id);
  155. return tcf_hash_search(tn, a, index);
  156. }
  157. static struct tc_action_ops act_connmark_ops = {
  158. .kind = "connmark",
  159. .type = TCA_ACT_CONNMARK,
  160. .owner = THIS_MODULE,
  161. .act = tcf_connmark,
  162. .dump = tcf_connmark_dump,
  163. .init = tcf_connmark_init,
  164. .walk = tcf_connmark_walker,
  165. .lookup = tcf_connmark_search,
  166. .size = sizeof(struct tcf_connmark_info),
  167. };
  168. static __net_init int connmark_init_net(struct net *net)
  169. {
  170. struct tc_action_net *tn = net_generic(net, connmark_net_id);
  171. return tc_action_net_init(tn, &act_connmark_ops, CONNMARK_TAB_MASK);
  172. }
  173. static void __net_exit connmark_exit_net(struct net *net)
  174. {
  175. struct tc_action_net *tn = net_generic(net, connmark_net_id);
  176. tc_action_net_exit(tn);
  177. }
  178. static struct pernet_operations connmark_net_ops = {
  179. .init = connmark_init_net,
  180. .exit = connmark_exit_net,
  181. .id = &connmark_net_id,
  182. .size = sizeof(struct tc_action_net),
  183. };
  184. static int __init connmark_init_module(void)
  185. {
  186. return tcf_register_action(&act_connmark_ops, &connmark_net_ops);
  187. }
  188. static void __exit connmark_cleanup_module(void)
  189. {
  190. tcf_unregister_action(&act_connmark_ops, &connmark_net_ops);
  191. }
  192. module_init(connmark_init_module);
  193. module_exit(connmark_cleanup_module);
  194. MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
  195. MODULE_DESCRIPTION("Connection tracking mark restoring");
  196. MODULE_LICENSE("GPL");