621-sched_act_connmark.patch 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. --- /dev/null
  2. +++ b/net/sched/act_connmark.c
  3. @@ -0,0 +1,126 @@
  4. +/*
  5. + * Copyright (c) 2011 Felix Fietkau <nbd@nbd.name>
  6. + *
  7. + * This program is free software; you can redistribute it and/or modify it
  8. + * under the terms and conditions of the GNU General Public License,
  9. + * version 2, as published by the Free Software Foundation.
  10. + *
  11. + * This program is distributed in the hope it will be useful, but WITHOUT
  12. + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. + * more details.
  15. + *
  16. + * You should have received a copy of the GNU General Public License along with
  17. + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  18. + * Place - Suite 330, Boston, MA 02111-1307 USA.
  19. + */
  20. +
  21. +#include <linux/module.h>
  22. +#include <linux/init.h>
  23. +#include <linux/kernel.h>
  24. +#include <linux/skbuff.h>
  25. +#include <linux/rtnetlink.h>
  26. +#include <linux/pkt_cls.h>
  27. +#include <linux/ip.h>
  28. +#include <linux/ipv6.h>
  29. +#include <net/netlink.h>
  30. +#include <net/pkt_sched.h>
  31. +#include <net/act_api.h>
  32. +
  33. +#include <net/netfilter/nf_conntrack.h>
  34. +#include <net/netfilter/nf_conntrack_core.h>
  35. +
  36. +#define TCA_ACT_CONNMARK 20
  37. +
  38. +#define CONNMARK_TAB_MASK 3
  39. +
  40. +static int tcf_connmark(struct sk_buff *skb, const struct tc_action *a,
  41. + struct tcf_result *res)
  42. +{
  43. + struct nf_conn *c;
  44. + enum ip_conntrack_info ctinfo;
  45. + int proto;
  46. + int r;
  47. +
  48. + if (skb->protocol == htons(ETH_P_IP)) {
  49. + if (skb->len < sizeof(struct iphdr))
  50. + goto out;
  51. + proto = PF_INET;
  52. + } else if (skb->protocol == htons(ETH_P_IPV6)) {
  53. + if (skb->len < sizeof(struct ipv6hdr))
  54. + goto out;
  55. + proto = PF_INET6;
  56. + } else
  57. + goto out;
  58. +
  59. + r = nf_conntrack_in(dev_net(skb->dev), proto, NF_INET_PRE_ROUTING, skb);
  60. + if (r != NF_ACCEPT)
  61. + goto out;
  62. +
  63. + c = nf_ct_get(skb, &ctinfo);
  64. + if (!c)
  65. + goto out;
  66. +
  67. + skb->mark = c->mark;
  68. + nf_conntrack_put(skb->nfct);
  69. + skb->nfct = NULL;
  70. +
  71. +out:
  72. + return TC_ACT_PIPE;
  73. +}
  74. +
  75. +static int tcf_connmark_init(struct net *net, struct nlattr *nla,
  76. + struct nlattr *est, struct tc_action *a,
  77. + int ovr, int bind)
  78. +{
  79. + int ret = 0;
  80. +
  81. + if (!tcf_hash_check(0, a, bind)) {
  82. + ret = tcf_hash_create(0, est, a, sizeof(struct tcf_common), bind);
  83. + if (ret)
  84. + return ret;
  85. +
  86. + tcf_hash_insert(a);
  87. + ret = ACT_P_CREATED;
  88. + } else {
  89. + if (!ovr) {
  90. + tcf_hash_release(a, bind);
  91. + return -EEXIST;
  92. + }
  93. + }
  94. +
  95. + return ret;
  96. +}
  97. +
  98. +static inline int tcf_connmark_dump(struct sk_buff *skb, struct tc_action *a,
  99. + int bind, int ref)
  100. +{
  101. + return skb->len;
  102. +}
  103. +
  104. +static struct tc_action_ops act_connmark_ops = {
  105. + .kind = "connmark",
  106. + .type = TCA_ACT_CONNMARK,
  107. + .owner = THIS_MODULE,
  108. + .act = tcf_connmark,
  109. + .dump = tcf_connmark_dump,
  110. + .init = tcf_connmark_init,
  111. +};
  112. +
  113. +MODULE_AUTHOR("Felix Fietkau <nbd@nbd.name>");
  114. +MODULE_DESCRIPTION("Connection tracking mark restoring");
  115. +MODULE_LICENSE("GPL");
  116. +
  117. +static int __init connmark_init_module(void)
  118. +{
  119. +
  120. + return tcf_register_action(&act_connmark_ops, CONNMARK_TAB_MASK);
  121. +}
  122. +
  123. +static void __exit connmark_cleanup_module(void)
  124. +{
  125. + tcf_unregister_action(&act_connmark_ops);
  126. +}
  127. +
  128. +module_init(connmark_init_module);
  129. +module_exit(connmark_cleanup_module);
  130. --- a/net/sched/Kconfig
  131. +++ b/net/sched/Kconfig
  132. @@ -686,6 +686,19 @@ config NET_ACT_CSUM
  133. To compile this code as a module, choose M here: the
  134. module will be called act_csum.
  135. +config NET_ACT_CONNMARK
  136. + tristate "Connection Tracking Marking"
  137. + depends on NET_CLS_ACT
  138. + depends on NF_CONNTRACK
  139. + depends on NF_CONNTRACK_MARK
  140. + ---help---
  141. + Say Y here to restore the connmark from a scheduler action
  142. +
  143. + If unsure, say N.
  144. +
  145. + To compile this code as a module, choose M here: the
  146. + module will be called act_connmark.
  147. +
  148. config NET_CLS_IND
  149. bool "Incoming device classification"
  150. depends on NET_CLS_U32 || NET_CLS_FW
  151. --- a/net/sched/Makefile
  152. +++ b/net/sched/Makefile
  153. @@ -16,6 +16,7 @@ obj-$(CONFIG_NET_ACT_PEDIT) += act_pedit
  154. obj-$(CONFIG_NET_ACT_SIMP) += act_simple.o
  155. obj-$(CONFIG_NET_ACT_SKBEDIT) += act_skbedit.o
  156. obj-$(CONFIG_NET_ACT_CSUM) += act_csum.o
  157. +obj-$(CONFIG_NET_ACT_CONNMARK) += act_connmark.o
  158. obj-$(CONFIG_NET_SCH_FIFO) += sch_fifo.o
  159. obj-$(CONFIG_NET_SCH_CBQ) += sch_cbq.o
  160. obj-$(CONFIG_NET_SCH_HTB) += sch_htb.o