nf_tproxy_ipv4.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2007-2008 BalaBit IT Ltd.
  4. * Author: Krisztian Kovacs
  5. */
  6. #include <net/netfilter/nf_tproxy.h>
  7. #include <linux/module.h>
  8. #include <linux/skbuff.h>
  9. #include <net/sock.h>
  10. #include <net/inet_sock.h>
  11. #include <linux/ip.h>
  12. #include <net/checksum.h>
  13. #include <net/udp.h>
  14. #include <net/tcp.h>
  15. #include <linux/inetdevice.h>
  16. struct sock *
  17. nf_tproxy_handle_time_wait4(struct net *net, struct sk_buff *skb,
  18. __be32 laddr, __be16 lport, struct sock *sk)
  19. {
  20. const struct iphdr *iph = ip_hdr(skb);
  21. struct tcphdr _hdr, *hp;
  22. hp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_hdr), &_hdr);
  23. if (hp == NULL) {
  24. inet_twsk_put(inet_twsk(sk));
  25. return NULL;
  26. }
  27. if (hp->syn && !hp->rst && !hp->ack && !hp->fin) {
  28. /* SYN to a TIME_WAIT socket, we'd rather redirect it
  29. * to a listener socket if there's one */
  30. struct sock *sk2;
  31. sk2 = nf_tproxy_get_sock_v4(net, skb, iph->protocol,
  32. iph->saddr, laddr ? laddr : iph->daddr,
  33. hp->source, lport ? lport : hp->dest,
  34. skb->dev, NF_TPROXY_LOOKUP_LISTENER);
  35. if (sk2) {
  36. inet_twsk_deschedule_put(inet_twsk(sk));
  37. sk = sk2;
  38. }
  39. }
  40. return sk;
  41. }
  42. EXPORT_SYMBOL_GPL(nf_tproxy_handle_time_wait4);
  43. __be32 nf_tproxy_laddr4(struct sk_buff *skb, __be32 user_laddr, __be32 daddr)
  44. {
  45. const struct in_ifaddr *ifa;
  46. struct in_device *indev;
  47. __be32 laddr;
  48. if (user_laddr)
  49. return user_laddr;
  50. laddr = 0;
  51. indev = __in_dev_get_rcu(skb->dev);
  52. in_dev_for_each_ifa_rcu(ifa, indev) {
  53. if (ifa->ifa_flags & IFA_F_SECONDARY)
  54. continue;
  55. laddr = ifa->ifa_local;
  56. break;
  57. }
  58. return laddr ? laddr : daddr;
  59. }
  60. EXPORT_SYMBOL_GPL(nf_tproxy_laddr4);
  61. struct sock *
  62. nf_tproxy_get_sock_v4(struct net *net, struct sk_buff *skb,
  63. const u8 protocol,
  64. const __be32 saddr, const __be32 daddr,
  65. const __be16 sport, const __be16 dport,
  66. const struct net_device *in,
  67. const enum nf_tproxy_lookup_t lookup_type)
  68. {
  69. struct sock *sk;
  70. switch (protocol) {
  71. case IPPROTO_TCP: {
  72. struct tcphdr _hdr, *hp;
  73. hp = skb_header_pointer(skb, ip_hdrlen(skb),
  74. sizeof(struct tcphdr), &_hdr);
  75. if (hp == NULL)
  76. return NULL;
  77. switch (lookup_type) {
  78. case NF_TPROXY_LOOKUP_LISTENER:
  79. sk = inet_lookup_listener(net, &tcp_hashinfo, skb,
  80. ip_hdrlen(skb) +
  81. __tcp_hdrlen(hp),
  82. saddr, sport,
  83. daddr, dport,
  84. in->ifindex, 0);
  85. if (sk && !refcount_inc_not_zero(&sk->sk_refcnt))
  86. sk = NULL;
  87. /* NOTE: we return listeners even if bound to
  88. * 0.0.0.0, those are filtered out in
  89. * xt_socket, since xt_TPROXY needs 0 bound
  90. * listeners too
  91. */
  92. break;
  93. case NF_TPROXY_LOOKUP_ESTABLISHED:
  94. sk = inet_lookup_established(net, &tcp_hashinfo,
  95. saddr, sport, daddr, dport,
  96. in->ifindex);
  97. break;
  98. default:
  99. BUG();
  100. }
  101. break;
  102. }
  103. case IPPROTO_UDP:
  104. sk = udp4_lib_lookup(net, saddr, sport, daddr, dport,
  105. in->ifindex);
  106. if (sk) {
  107. int connected = (sk->sk_state == TCP_ESTABLISHED);
  108. int wildcard = (inet_sk(sk)->inet_rcv_saddr == 0);
  109. /* NOTE: we return listeners even if bound to
  110. * 0.0.0.0, those are filtered out in
  111. * xt_socket, since xt_TPROXY needs 0 bound
  112. * listeners too
  113. */
  114. if ((lookup_type == NF_TPROXY_LOOKUP_ESTABLISHED &&
  115. (!connected || wildcard)) ||
  116. (lookup_type == NF_TPROXY_LOOKUP_LISTENER && connected)) {
  117. sock_put(sk);
  118. sk = NULL;
  119. }
  120. }
  121. break;
  122. default:
  123. WARN_ON(1);
  124. sk = NULL;
  125. }
  126. pr_debug("tproxy socket lookup: proto %u %08x:%u -> %08x:%u, lookup type: %d, sock %p\n",
  127. protocol, ntohl(saddr), ntohs(sport), ntohl(daddr), ntohs(dport), lookup_type, sk);
  128. return sk;
  129. }
  130. EXPORT_SYMBOL_GPL(nf_tproxy_get_sock_v4);
  131. MODULE_LICENSE("GPL");
  132. MODULE_AUTHOR("Balazs Scheidler, Krisztian Kovacs");
  133. MODULE_DESCRIPTION("Netfilter IPv4 transparent proxy support");