root/net/ipv4/netfilter/nf_tproxy_ipv4.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. nf_tproxy_handle_time_wait4
  2. nf_tproxy_laddr4
  3. nf_tproxy_get_sock_v4

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

/* [<][>][^][v][top][bottom][index][help] */