act_connmark.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 int tcf_connmark(struct sk_buff *skb, const struct tc_action *a,
  30. struct tcf_result *res)
  31. {
  32. const struct nf_conntrack_tuple_hash *thash;
  33. struct nf_conntrack_tuple tuple;
  34. enum ip_conntrack_info ctinfo;
  35. struct tcf_connmark_info *ca = a->priv;
  36. struct nf_conntrack_zone zone;
  37. struct nf_conn *c;
  38. int proto;
  39. spin_lock(&ca->tcf_lock);
  40. ca->tcf_tm.lastuse = jiffies;
  41. bstats_update(&ca->tcf_bstats, skb);
  42. if (skb->protocol == htons(ETH_P_IP)) {
  43. if (skb->len < sizeof(struct iphdr))
  44. goto out;
  45. proto = NFPROTO_IPV4;
  46. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  47. if (skb->len < sizeof(struct ipv6hdr))
  48. goto out;
  49. proto = NFPROTO_IPV6;
  50. } else {
  51. goto out;
  52. }
  53. c = nf_ct_get(skb, &ctinfo);
  54. if (c) {
  55. skb->mark = c->mark;
  56. /* using overlimits stats to count how many packets marked */
  57. ca->tcf_qstats.overlimits++;
  58. goto out;
  59. }
  60. if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb),
  61. proto, ca->net, &tuple))
  62. goto out;
  63. zone.id = ca->zone;
  64. zone.dir = NF_CT_DEFAULT_ZONE_DIR;
  65. thash = nf_conntrack_find_get(ca->net, &zone, &tuple);
  66. if (!thash)
  67. goto out;
  68. c = nf_ct_tuplehash_to_ctrack(thash);
  69. /* using overlimits stats to count how many packets marked */
  70. ca->tcf_qstats.overlimits++;
  71. skb->mark = c->mark;
  72. nf_ct_put(c);
  73. out:
  74. spin_unlock(&ca->tcf_lock);
  75. return ca->tcf_action;
  76. }
  77. static const struct nla_policy connmark_policy[TCA_CONNMARK_MAX + 1] = {
  78. [TCA_CONNMARK_PARMS] = { .len = sizeof(struct tc_connmark) },
  79. };
  80. static int tcf_connmark_init(struct net *net, struct nlattr *nla,
  81. struct nlattr *est, struct tc_action *a,
  82. int ovr, int bind)
  83. {
  84. struct nlattr *tb[TCA_CONNMARK_MAX + 1];
  85. struct tcf_connmark_info *ci;
  86. struct tc_connmark *parm;
  87. int ret = 0;
  88. if (!nla)
  89. return -EINVAL;
  90. ret = nla_parse_nested(tb, TCA_CONNMARK_MAX, nla, connmark_policy);
  91. if (ret < 0)
  92. return ret;
  93. if (!tb[TCA_CONNMARK_PARMS])
  94. return -EINVAL;
  95. parm = nla_data(tb[TCA_CONNMARK_PARMS]);
  96. if (!tcf_hash_check(parm->index, a, bind)) {
  97. ret = tcf_hash_create(parm->index, est, a, sizeof(*ci),
  98. bind, false);
  99. if (ret)
  100. return ret;
  101. ci = to_connmark(a);
  102. ci->tcf_action = parm->action;
  103. ci->net = net;
  104. ci->zone = parm->zone;
  105. tcf_hash_insert(a);
  106. ret = ACT_P_CREATED;
  107. } else {
  108. ci = to_connmark(a);
  109. if (bind)
  110. return 0;
  111. tcf_hash_release(a, bind);
  112. if (!ovr)
  113. return -EEXIST;
  114. /* replacing action and zone */
  115. ci->tcf_action = parm->action;
  116. ci->zone = parm->zone;
  117. }
  118. return ret;
  119. }
  120. static inline int tcf_connmark_dump(struct sk_buff *skb, struct tc_action *a,
  121. int bind, int ref)
  122. {
  123. unsigned char *b = skb_tail_pointer(skb);
  124. struct tcf_connmark_info *ci = a->priv;
  125. struct tc_connmark opt = {
  126. .index = ci->tcf_index,
  127. .refcnt = ci->tcf_refcnt - ref,
  128. .bindcnt = ci->tcf_bindcnt - bind,
  129. .action = ci->tcf_action,
  130. .zone = ci->zone,
  131. };
  132. struct tcf_t t;
  133. if (nla_put(skb, TCA_CONNMARK_PARMS, sizeof(opt), &opt))
  134. goto nla_put_failure;
  135. t.install = jiffies_to_clock_t(jiffies - ci->tcf_tm.install);
  136. t.lastuse = jiffies_to_clock_t(jiffies - ci->tcf_tm.lastuse);
  137. t.expires = jiffies_to_clock_t(ci->tcf_tm.expires);
  138. if (nla_put(skb, TCA_CONNMARK_TM, sizeof(t), &t))
  139. goto nla_put_failure;
  140. return skb->len;
  141. nla_put_failure:
  142. nlmsg_trim(skb, b);
  143. return -1;
  144. }
  145. static struct tc_action_ops act_connmark_ops = {
  146. .kind = "connmark",
  147. .type = TCA_ACT_CONNMARK,
  148. .owner = THIS_MODULE,
  149. .act = tcf_connmark,
  150. .dump = tcf_connmark_dump,
  151. .init = tcf_connmark_init,
  152. };
  153. static int __init connmark_init_module(void)
  154. {
  155. return tcf_register_action(&act_connmark_ops, CONNMARK_TAB_MASK);
  156. }
  157. static void __exit connmark_cleanup_module(void)
  158. {
  159. tcf_unregister_action(&act_connmark_ops);
  160. }
  161. module_init(connmark_init_module);
  162. module_exit(connmark_cleanup_module);
  163. MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
  164. MODULE_DESCRIPTION("Connection tracking mark restoring");
  165. MODULE_LICENSE("GPL");