nf_conntrack_proto_generic.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* (C) 1999-2001 Paul `Rusty' Russell
  2. * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/types.h>
  9. #include <linux/jiffies.h>
  10. #include <linux/timer.h>
  11. #include <linux/netfilter.h>
  12. #include <net/netfilter/nf_conntrack_l4proto.h>
  13. static unsigned int nf_ct_generic_timeout __read_mostly = 600*HZ;
  14. static bool nf_generic_should_process(u8 proto)
  15. {
  16. switch (proto) {
  17. #ifdef CONFIG_NF_CT_PROTO_SCTP_MODULE
  18. case IPPROTO_SCTP:
  19. return false;
  20. #endif
  21. #ifdef CONFIG_NF_CT_PROTO_DCCP_MODULE
  22. case IPPROTO_DCCP:
  23. return false;
  24. #endif
  25. #ifdef CONFIG_NF_CT_PROTO_GRE_MODULE
  26. case IPPROTO_GRE:
  27. return false;
  28. #endif
  29. #ifdef CONFIG_NF_CT_PROTO_UDPLITE_MODULE
  30. case IPPROTO_UDPLITE:
  31. return false;
  32. #endif
  33. default:
  34. return true;
  35. }
  36. }
  37. static inline struct nf_generic_net *generic_pernet(struct net *net)
  38. {
  39. return &net->ct.nf_ct_proto.generic;
  40. }
  41. static bool generic_pkt_to_tuple(const struct sk_buff *skb,
  42. unsigned int dataoff,
  43. struct net *net, struct nf_conntrack_tuple *tuple)
  44. {
  45. tuple->src.u.all = 0;
  46. tuple->dst.u.all = 0;
  47. return true;
  48. }
  49. static bool generic_invert_tuple(struct nf_conntrack_tuple *tuple,
  50. const struct nf_conntrack_tuple *orig)
  51. {
  52. tuple->src.u.all = 0;
  53. tuple->dst.u.all = 0;
  54. return true;
  55. }
  56. /* Print out the per-protocol part of the tuple. */
  57. static void generic_print_tuple(struct seq_file *s,
  58. const struct nf_conntrack_tuple *tuple)
  59. {
  60. }
  61. static unsigned int *generic_get_timeouts(struct net *net)
  62. {
  63. return &(generic_pernet(net)->timeout);
  64. }
  65. /* Returns verdict for packet, or -1 for invalid. */
  66. static int generic_packet(struct nf_conn *ct,
  67. const struct sk_buff *skb,
  68. unsigned int dataoff,
  69. enum ip_conntrack_info ctinfo,
  70. u_int8_t pf,
  71. unsigned int hooknum,
  72. unsigned int *timeout)
  73. {
  74. nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
  75. return NF_ACCEPT;
  76. }
  77. /* Called when a new connection for this protocol found. */
  78. static bool generic_new(struct nf_conn *ct, const struct sk_buff *skb,
  79. unsigned int dataoff, unsigned int *timeouts)
  80. {
  81. bool ret;
  82. ret = nf_generic_should_process(nf_ct_protonum(ct));
  83. if (!ret)
  84. pr_warn_once("conntrack: generic helper won't handle protocol %d. Please consider loading the specific helper module.\n",
  85. nf_ct_protonum(ct));
  86. return ret;
  87. }
  88. #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
  89. #include <linux/netfilter/nfnetlink.h>
  90. #include <linux/netfilter/nfnetlink_cttimeout.h>
  91. static int generic_timeout_nlattr_to_obj(struct nlattr *tb[],
  92. struct net *net, void *data)
  93. {
  94. unsigned int *timeout = data;
  95. struct nf_generic_net *gn = generic_pernet(net);
  96. if (tb[CTA_TIMEOUT_GENERIC_TIMEOUT])
  97. *timeout =
  98. ntohl(nla_get_be32(tb[CTA_TIMEOUT_GENERIC_TIMEOUT])) * HZ;
  99. else {
  100. /* Set default generic timeout. */
  101. *timeout = gn->timeout;
  102. }
  103. return 0;
  104. }
  105. static int
  106. generic_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
  107. {
  108. const unsigned int *timeout = data;
  109. if (nla_put_be32(skb, CTA_TIMEOUT_GENERIC_TIMEOUT, htonl(*timeout / HZ)))
  110. goto nla_put_failure;
  111. return 0;
  112. nla_put_failure:
  113. return -ENOSPC;
  114. }
  115. static const struct nla_policy
  116. generic_timeout_nla_policy[CTA_TIMEOUT_GENERIC_MAX+1] = {
  117. [CTA_TIMEOUT_GENERIC_TIMEOUT] = { .type = NLA_U32 },
  118. };
  119. #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
  120. #ifdef CONFIG_SYSCTL
  121. static struct ctl_table generic_sysctl_table[] = {
  122. {
  123. .procname = "nf_conntrack_generic_timeout",
  124. .maxlen = sizeof(unsigned int),
  125. .mode = 0644,
  126. .proc_handler = proc_dointvec_jiffies,
  127. },
  128. { }
  129. };
  130. #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
  131. static struct ctl_table generic_compat_sysctl_table[] = {
  132. {
  133. .procname = "ip_conntrack_generic_timeout",
  134. .maxlen = sizeof(unsigned int),
  135. .mode = 0644,
  136. .proc_handler = proc_dointvec_jiffies,
  137. },
  138. { }
  139. };
  140. #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
  141. #endif /* CONFIG_SYSCTL */
  142. static int generic_kmemdup_sysctl_table(struct nf_proto_net *pn,
  143. struct nf_generic_net *gn)
  144. {
  145. #ifdef CONFIG_SYSCTL
  146. pn->ctl_table = kmemdup(generic_sysctl_table,
  147. sizeof(generic_sysctl_table),
  148. GFP_KERNEL);
  149. if (!pn->ctl_table)
  150. return -ENOMEM;
  151. pn->ctl_table[0].data = &gn->timeout;
  152. #endif
  153. return 0;
  154. }
  155. static int generic_kmemdup_compat_sysctl_table(struct nf_proto_net *pn,
  156. struct nf_generic_net *gn)
  157. {
  158. #ifdef CONFIG_SYSCTL
  159. #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
  160. pn->ctl_compat_table = kmemdup(generic_compat_sysctl_table,
  161. sizeof(generic_compat_sysctl_table),
  162. GFP_KERNEL);
  163. if (!pn->ctl_compat_table)
  164. return -ENOMEM;
  165. pn->ctl_compat_table[0].data = &gn->timeout;
  166. #endif
  167. #endif
  168. return 0;
  169. }
  170. static int generic_init_net(struct net *net, u_int16_t proto)
  171. {
  172. int ret;
  173. struct nf_generic_net *gn = generic_pernet(net);
  174. struct nf_proto_net *pn = &gn->pn;
  175. gn->timeout = nf_ct_generic_timeout;
  176. ret = generic_kmemdup_compat_sysctl_table(pn, gn);
  177. if (ret < 0)
  178. return ret;
  179. ret = generic_kmemdup_sysctl_table(pn, gn);
  180. if (ret < 0)
  181. nf_ct_kfree_compat_sysctl_table(pn);
  182. return ret;
  183. }
  184. static struct nf_proto_net *generic_get_net_proto(struct net *net)
  185. {
  186. return &net->ct.nf_ct_proto.generic.pn;
  187. }
  188. struct nf_conntrack_l4proto nf_conntrack_l4proto_generic __read_mostly =
  189. {
  190. .l3proto = PF_UNSPEC,
  191. .l4proto = 255,
  192. .name = "unknown",
  193. .pkt_to_tuple = generic_pkt_to_tuple,
  194. .invert_tuple = generic_invert_tuple,
  195. .print_tuple = generic_print_tuple,
  196. .packet = generic_packet,
  197. .get_timeouts = generic_get_timeouts,
  198. .new = generic_new,
  199. #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
  200. .ctnl_timeout = {
  201. .nlattr_to_obj = generic_timeout_nlattr_to_obj,
  202. .obj_to_nlattr = generic_timeout_obj_to_nlattr,
  203. .nlattr_max = CTA_TIMEOUT_GENERIC_MAX,
  204. .obj_size = sizeof(unsigned int),
  205. .nla_policy = generic_timeout_nla_policy,
  206. },
  207. #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
  208. .init_net = generic_init_net,
  209. .get_net_proto = generic_get_net_proto,
  210. };