act_connmark.c 4.6 KB

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