nf_conntrack_proto_udp.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* (C) 1999-2001 Paul `Rusty' Russell
  3. * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
  4. * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
  5. */
  6. #include <linux/types.h>
  7. #include <linux/timer.h>
  8. #include <linux/module.h>
  9. #include <linux/udp.h>
  10. #include <linux/seq_file.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/ipv6.h>
  13. #include <net/ip6_checksum.h>
  14. #include <net/checksum.h>
  15. #include <linux/netfilter.h>
  16. #include <linux/netfilter_ipv4.h>
  17. #include <linux/netfilter_ipv6.h>
  18. #include <net/netfilter/nf_conntrack_l4proto.h>
  19. #include <net/netfilter/nf_conntrack_ecache.h>
  20. #include <net/netfilter/nf_conntrack_timeout.h>
  21. #include <net/netfilter/nf_log.h>
  22. #include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
  23. #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
  24. static const unsigned int udp_timeouts[UDP_CT_MAX] = {
  25. [UDP_CT_UNREPLIED] = 30*HZ,
  26. [UDP_CT_REPLIED] = 120*HZ,
  27. };
  28. static unsigned int *udp_get_timeouts(struct net *net)
  29. {
  30. return nf_udp_pernet(net)->timeouts;
  31. }
  32. static void udp_error_log(const struct sk_buff *skb,
  33. const struct nf_hook_state *state,
  34. const char *msg)
  35. {
  36. nf_l4proto_log_invalid(skb, state->net, state->pf,
  37. IPPROTO_UDP, "%s", msg);
  38. }
  39. static bool udp_error(struct sk_buff *skb,
  40. unsigned int dataoff,
  41. const struct nf_hook_state *state)
  42. {
  43. unsigned int udplen = skb->len - dataoff;
  44. const struct udphdr *hdr;
  45. struct udphdr _hdr;
  46. /* Header is too small? */
  47. hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
  48. if (!hdr) {
  49. udp_error_log(skb, state, "short packet");
  50. return true;
  51. }
  52. /* Truncated/malformed packets */
  53. if (ntohs(hdr->len) > udplen || ntohs(hdr->len) < sizeof(*hdr)) {
  54. udp_error_log(skb, state, "truncated/malformed packet");
  55. return true;
  56. }
  57. /* Packet with no checksum */
  58. if (!hdr->check)
  59. return false;
  60. /* Checksum invalid? Ignore.
  61. * We skip checking packets on the outgoing path
  62. * because the checksum is assumed to be correct.
  63. * FIXME: Source route IP option packets --RR */
  64. if (state->hook == NF_INET_PRE_ROUTING &&
  65. state->net->ct.sysctl_checksum &&
  66. nf_checksum(skb, state->hook, dataoff, IPPROTO_UDP, state->pf)) {
  67. udp_error_log(skb, state, "bad checksum");
  68. return true;
  69. }
  70. return false;
  71. }
  72. /* Returns verdict for packet, and may modify conntracktype */
  73. int nf_conntrack_udp_packet(struct nf_conn *ct,
  74. struct sk_buff *skb,
  75. unsigned int dataoff,
  76. enum ip_conntrack_info ctinfo,
  77. const struct nf_hook_state *state)
  78. {
  79. unsigned int *timeouts;
  80. if (udp_error(skb, dataoff, state))
  81. return -NF_ACCEPT;
  82. timeouts = nf_ct_timeout_lookup(ct);
  83. if (!timeouts)
  84. timeouts = udp_get_timeouts(nf_ct_net(ct));
  85. if (!nf_ct_is_confirmed(ct))
  86. ct->proto.udp.stream_ts = 2 * HZ + jiffies;
  87. /* If we've seen traffic both ways, this is some kind of UDP
  88. * stream. Set Assured.
  89. */
  90. if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
  91. unsigned long extra = timeouts[UDP_CT_UNREPLIED];
  92. bool stream = false;
  93. /* Still active after two seconds? Extend timeout. */
  94. if (time_after(jiffies, ct->proto.udp.stream_ts)) {
  95. extra = timeouts[UDP_CT_REPLIED];
  96. stream = true;
  97. }
  98. nf_ct_refresh_acct(ct, ctinfo, skb, extra);
  99. /* never set ASSURED for IPS_NAT_CLASH, they time out soon */
  100. if (unlikely((ct->status & IPS_NAT_CLASH)))
  101. return NF_ACCEPT;
  102. /* Also, more likely to be important, and not a probe */
  103. if (stream && !test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
  104. nf_conntrack_event_cache(IPCT_ASSURED, ct);
  105. } else {
  106. nf_ct_refresh_acct(ct, ctinfo, skb, timeouts[UDP_CT_UNREPLIED]);
  107. }
  108. return NF_ACCEPT;
  109. }
  110. #ifdef CONFIG_NF_CT_PROTO_UDPLITE
  111. static void udplite_error_log(const struct sk_buff *skb,
  112. const struct nf_hook_state *state,
  113. const char *msg)
  114. {
  115. nf_l4proto_log_invalid(skb, state->net, state->pf,
  116. IPPROTO_UDPLITE, "%s", msg);
  117. }
  118. static bool udplite_error(struct sk_buff *skb,
  119. unsigned int dataoff,
  120. const struct nf_hook_state *state)
  121. {
  122. unsigned int udplen = skb->len - dataoff;
  123. const struct udphdr *hdr;
  124. struct udphdr _hdr;
  125. unsigned int cscov;
  126. /* Header is too small? */
  127. hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
  128. if (!hdr) {
  129. udplite_error_log(skb, state, "short packet");
  130. return true;
  131. }
  132. cscov = ntohs(hdr->len);
  133. if (cscov == 0) {
  134. cscov = udplen;
  135. } else if (cscov < sizeof(*hdr) || cscov > udplen) {
  136. udplite_error_log(skb, state, "invalid checksum coverage");
  137. return true;
  138. }
  139. /* UDPLITE mandates checksums */
  140. if (!hdr->check) {
  141. udplite_error_log(skb, state, "checksum missing");
  142. return true;
  143. }
  144. /* Checksum invalid? Ignore. */
  145. if (state->hook == NF_INET_PRE_ROUTING &&
  146. state->net->ct.sysctl_checksum &&
  147. nf_checksum_partial(skb, state->hook, dataoff, cscov, IPPROTO_UDP,
  148. state->pf)) {
  149. udplite_error_log(skb, state, "bad checksum");
  150. return true;
  151. }
  152. return false;
  153. }
  154. /* Returns verdict for packet, and may modify conntracktype */
  155. int nf_conntrack_udplite_packet(struct nf_conn *ct,
  156. struct sk_buff *skb,
  157. unsigned int dataoff,
  158. enum ip_conntrack_info ctinfo,
  159. const struct nf_hook_state *state)
  160. {
  161. unsigned int *timeouts;
  162. if (udplite_error(skb, dataoff, state))
  163. return -NF_ACCEPT;
  164. timeouts = nf_ct_timeout_lookup(ct);
  165. if (!timeouts)
  166. timeouts = udp_get_timeouts(nf_ct_net(ct));
  167. /* If we've seen traffic both ways, this is some kind of UDP
  168. stream. Extend timeout. */
  169. if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
  170. nf_ct_refresh_acct(ct, ctinfo, skb,
  171. timeouts[UDP_CT_REPLIED]);
  172. if (unlikely((ct->status & IPS_NAT_CLASH)))
  173. return NF_ACCEPT;
  174. /* Also, more likely to be important, and not a probe */
  175. if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
  176. nf_conntrack_event_cache(IPCT_ASSURED, ct);
  177. } else {
  178. nf_ct_refresh_acct(ct, ctinfo, skb, timeouts[UDP_CT_UNREPLIED]);
  179. }
  180. return NF_ACCEPT;
  181. }
  182. #endif
  183. #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
  184. #include <linux/netfilter/nfnetlink.h>
  185. #include <linux/netfilter/nfnetlink_cttimeout.h>
  186. static int udp_timeout_nlattr_to_obj(struct nlattr *tb[],
  187. struct net *net, void *data)
  188. {
  189. unsigned int *timeouts = data;
  190. struct nf_udp_net *un = nf_udp_pernet(net);
  191. if (!timeouts)
  192. timeouts = un->timeouts;
  193. /* set default timeouts for UDP. */
  194. timeouts[UDP_CT_UNREPLIED] = un->timeouts[UDP_CT_UNREPLIED];
  195. timeouts[UDP_CT_REPLIED] = un->timeouts[UDP_CT_REPLIED];
  196. if (tb[CTA_TIMEOUT_UDP_UNREPLIED]) {
  197. timeouts[UDP_CT_UNREPLIED] =
  198. ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDP_UNREPLIED])) * HZ;
  199. }
  200. if (tb[CTA_TIMEOUT_UDP_REPLIED]) {
  201. timeouts[UDP_CT_REPLIED] =
  202. ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDP_REPLIED])) * HZ;
  203. }
  204. return 0;
  205. }
  206. static int
  207. udp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
  208. {
  209. const unsigned int *timeouts = data;
  210. if (nla_put_be32(skb, CTA_TIMEOUT_UDP_UNREPLIED,
  211. htonl(timeouts[UDP_CT_UNREPLIED] / HZ)) ||
  212. nla_put_be32(skb, CTA_TIMEOUT_UDP_REPLIED,
  213. htonl(timeouts[UDP_CT_REPLIED] / HZ)))
  214. goto nla_put_failure;
  215. return 0;
  216. nla_put_failure:
  217. return -ENOSPC;
  218. }
  219. static const struct nla_policy
  220. udp_timeout_nla_policy[CTA_TIMEOUT_UDP_MAX+1] = {
  221. [CTA_TIMEOUT_UDP_UNREPLIED] = { .type = NLA_U32 },
  222. [CTA_TIMEOUT_UDP_REPLIED] = { .type = NLA_U32 },
  223. };
  224. #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
  225. void nf_conntrack_udp_init_net(struct net *net)
  226. {
  227. struct nf_udp_net *un = nf_udp_pernet(net);
  228. int i;
  229. for (i = 0; i < UDP_CT_MAX; i++)
  230. un->timeouts[i] = udp_timeouts[i];
  231. }
  232. const struct nf_conntrack_l4proto nf_conntrack_l4proto_udp =
  233. {
  234. .l4proto = IPPROTO_UDP,
  235. .allow_clash = true,
  236. #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
  237. .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
  238. .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
  239. .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
  240. .nla_policy = nf_ct_port_nla_policy,
  241. #endif
  242. #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
  243. .ctnl_timeout = {
  244. .nlattr_to_obj = udp_timeout_nlattr_to_obj,
  245. .obj_to_nlattr = udp_timeout_obj_to_nlattr,
  246. .nlattr_max = CTA_TIMEOUT_UDP_MAX,
  247. .obj_size = sizeof(unsigned int) * CTA_TIMEOUT_UDP_MAX,
  248. .nla_policy = udp_timeout_nla_policy,
  249. },
  250. #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
  251. };
  252. #ifdef CONFIG_NF_CT_PROTO_UDPLITE
  253. const struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite =
  254. {
  255. .l4proto = IPPROTO_UDPLITE,
  256. .allow_clash = true,
  257. #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
  258. .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
  259. .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
  260. .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
  261. .nla_policy = nf_ct_port_nla_policy,
  262. #endif
  263. #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
  264. .ctnl_timeout = {
  265. .nlattr_to_obj = udp_timeout_nlattr_to_obj,
  266. .obj_to_nlattr = udp_timeout_obj_to_nlattr,
  267. .nlattr_max = CTA_TIMEOUT_UDP_MAX,
  268. .obj_size = sizeof(unsigned int) * CTA_TIMEOUT_UDP_MAX,
  269. .nla_policy = udp_timeout_nla_policy,
  270. },
  271. #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
  272. };
  273. #endif