act_connmark.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. static unsigned int connmark_net_id;
  29. static struct tc_action_ops act_connmark_ops;
  30. static int tcf_connmark_act(struct sk_buff *skb, const struct tc_action *a,
  31. struct tcf_result *res)
  32. {
  33. const struct nf_conntrack_tuple_hash *thash;
  34. struct nf_conntrack_tuple tuple;
  35. enum ip_conntrack_info ctinfo;
  36. struct tcf_connmark_info *ca = to_connmark(a);
  37. struct nf_conntrack_zone zone;
  38. struct nf_conn *c;
  39. int proto;
  40. spin_lock(&ca->tcf_lock);
  41. tcf_lastuse_update(&ca->tcf_tm);
  42. bstats_update(&ca->tcf_bstats, skb);
  43. if (skb->protocol == htons(ETH_P_IP)) {
  44. if (skb->len < sizeof(struct iphdr))
  45. goto out;
  46. proto = NFPROTO_IPV4;
  47. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  48. if (skb->len < sizeof(struct ipv6hdr))
  49. goto out;
  50. proto = NFPROTO_IPV6;
  51. } else {
  52. goto out;
  53. }
  54. c = nf_ct_get(skb, &ctinfo);
  55. if (c) {
  56. skb->mark = c->mark;
  57. /* using overlimits stats to count how many packets marked */
  58. ca->tcf_qstats.overlimits++;
  59. goto out;
  60. }
  61. if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb),
  62. proto, ca->net, &tuple))
  63. goto out;
  64. zone.id = ca->zone;
  65. zone.dir = NF_CT_DEFAULT_ZONE_DIR;
  66. thash = nf_conntrack_find_get(ca->net, &zone, &tuple);
  67. if (!thash)
  68. goto out;
  69. c = nf_ct_tuplehash_to_ctrack(thash);
  70. /* using overlimits stats to count how many packets marked */
  71. ca->tcf_qstats.overlimits++;
  72. skb->mark = c->mark;
  73. nf_ct_put(c);
  74. out:
  75. spin_unlock(&ca->tcf_lock);
  76. return ca->tcf_action;
  77. }
  78. static const struct nla_policy connmark_policy[TCA_CONNMARK_MAX + 1] = {
  79. [TCA_CONNMARK_PARMS] = { .len = sizeof(struct tc_connmark) },
  80. };
  81. static int tcf_connmark_init(struct net *net, struct nlattr *nla,
  82. struct nlattr *est, struct tc_action **a,
  83. int ovr, int bind, bool rtnl_held,
  84. struct netlink_ext_ack *extack)
  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. u32 index;
  92. if (!nla)
  93. return -EINVAL;
  94. ret = nla_parse_nested(tb, TCA_CONNMARK_MAX, nla, connmark_policy,
  95. NULL);
  96. if (ret < 0)
  97. return ret;
  98. if (!tb[TCA_CONNMARK_PARMS])
  99. return -EINVAL;
  100. parm = nla_data(tb[TCA_CONNMARK_PARMS]);
  101. index = parm->index;
  102. ret = tcf_idr_check_alloc(tn, &index, a, bind);
  103. if (!ret) {
  104. ret = tcf_idr_create(tn, index, est, a,
  105. &act_connmark_ops, bind, false);
  106. if (ret) {
  107. tcf_idr_cleanup(tn, index);
  108. return ret;
  109. }
  110. ci = to_connmark(*a);
  111. ci->tcf_action = parm->action;
  112. ci->net = net;
  113. ci->zone = parm->zone;
  114. tcf_idr_insert(tn, *a);
  115. ret = ACT_P_CREATED;
  116. } else if (ret > 0) {
  117. ci = to_connmark(*a);
  118. if (bind)
  119. return 0;
  120. if (!ovr) {
  121. tcf_idr_release(*a, bind);
  122. return -EEXIST;
  123. }
  124. /* replacing action and zone */
  125. ci->tcf_action = parm->action;
  126. ci->zone = parm->zone;
  127. ret = 0;
  128. }
  129. return ret;
  130. }
  131. static inline int tcf_connmark_dump(struct sk_buff *skb, struct tc_action *a,
  132. int bind, int ref)
  133. {
  134. unsigned char *b = skb_tail_pointer(skb);
  135. struct tcf_connmark_info *ci = to_connmark(a);
  136. struct tc_connmark opt = {
  137. .index = ci->tcf_index,
  138. .refcnt = refcount_read(&ci->tcf_refcnt) - ref,
  139. .bindcnt = atomic_read(&ci->tcf_bindcnt) - bind,
  140. .action = ci->tcf_action,
  141. .zone = ci->zone,
  142. };
  143. struct tcf_t t;
  144. if (nla_put(skb, TCA_CONNMARK_PARMS, sizeof(opt), &opt))
  145. goto nla_put_failure;
  146. tcf_tm_dump(&t, &ci->tcf_tm);
  147. if (nla_put_64bit(skb, TCA_CONNMARK_TM, sizeof(t), &t,
  148. TCA_CONNMARK_PAD))
  149. goto nla_put_failure;
  150. return skb->len;
  151. nla_put_failure:
  152. nlmsg_trim(skb, b);
  153. return -1;
  154. }
  155. static int tcf_connmark_walker(struct net *net, struct sk_buff *skb,
  156. struct netlink_callback *cb, int type,
  157. const struct tc_action_ops *ops,
  158. struct netlink_ext_ack *extack)
  159. {
  160. struct tc_action_net *tn = net_generic(net, connmark_net_id);
  161. return tcf_generic_walker(tn, skb, cb, type, ops, extack);
  162. }
  163. static int tcf_connmark_search(struct net *net, struct tc_action **a, u32 index,
  164. struct netlink_ext_ack *extack)
  165. {
  166. struct tc_action_net *tn = net_generic(net, connmark_net_id);
  167. return tcf_idr_search(tn, a, index);
  168. }
  169. static struct tc_action_ops act_connmark_ops = {
  170. .kind = "connmark",
  171. .type = TCA_ACT_CONNMARK,
  172. .owner = THIS_MODULE,
  173. .act = tcf_connmark_act,
  174. .dump = tcf_connmark_dump,
  175. .init = tcf_connmark_init,
  176. .walk = tcf_connmark_walker,
  177. .lookup = tcf_connmark_search,
  178. .size = sizeof(struct tcf_connmark_info),
  179. };
  180. static __net_init int connmark_init_net(struct net *net)
  181. {
  182. struct tc_action_net *tn = net_generic(net, connmark_net_id);
  183. return tc_action_net_init(net, tn, &act_connmark_ops);
  184. }
  185. static void __net_exit connmark_exit_net(struct list_head *net_list)
  186. {
  187. tc_action_net_exit(net_list, connmark_net_id);
  188. }
  189. static struct pernet_operations connmark_net_ops = {
  190. .init = connmark_init_net,
  191. .exit_batch = connmark_exit_net,
  192. .id = &connmark_net_id,
  193. .size = sizeof(struct tc_action_net),
  194. };
  195. static int __init connmark_init_module(void)
  196. {
  197. return tcf_register_action(&act_connmark_ops, &connmark_net_ops);
  198. }
  199. static void __exit connmark_cleanup_module(void)
  200. {
  201. tcf_unregister_action(&act_connmark_ops, &connmark_net_ops);
  202. }
  203. module_init(connmark_init_module);
  204. module_exit(connmark_cleanup_module);
  205. MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
  206. MODULE_DESCRIPTION("Connection tracking mark restoring");
  207. MODULE_LICENSE("GPL");