treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 500
[linux-2.6-microblaze.git] / net / ipv4 / netfilter / nf_tproxy_ipv4.c
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         struct in_device *indev;
53         __be32 laddr;
54
55         if (user_laddr)
56                 return user_laddr;
57
58         laddr = 0;
59         indev = __in_dev_get_rcu(skb->dev);
60         for_primary_ifa(indev) {
61                 laddr = ifa->ifa_local;
62                 break;
63         } endfor_ifa(indev);
64
65         return laddr ? laddr : daddr;
66 }
67 EXPORT_SYMBOL_GPL(nf_tproxy_laddr4);
68
69 struct sock *
70 nf_tproxy_get_sock_v4(struct net *net, struct sk_buff *skb,
71                       const u8 protocol,
72                       const __be32 saddr, const __be32 daddr,
73                       const __be16 sport, const __be16 dport,
74                       const struct net_device *in,
75                       const enum nf_tproxy_lookup_t lookup_type)
76 {
77         struct sock *sk;
78
79         switch (protocol) {
80         case IPPROTO_TCP: {
81                 struct tcphdr _hdr, *hp;
82
83                 hp = skb_header_pointer(skb, ip_hdrlen(skb),
84                                         sizeof(struct tcphdr), &_hdr);
85                 if (hp == NULL)
86                         return NULL;
87
88                 switch (lookup_type) {
89                 case NF_TPROXY_LOOKUP_LISTENER:
90                         sk = inet_lookup_listener(net, &tcp_hashinfo, skb,
91                                                     ip_hdrlen(skb) +
92                                                       __tcp_hdrlen(hp),
93                                                     saddr, sport,
94                                                     daddr, dport,
95                                                     in->ifindex, 0);
96
97                         if (sk && !refcount_inc_not_zero(&sk->sk_refcnt))
98                                 sk = NULL;
99                         /* NOTE: we return listeners even if bound to
100                          * 0.0.0.0, those are filtered out in
101                          * xt_socket, since xt_TPROXY needs 0 bound
102                          * listeners too
103                          */
104                         break;
105                 case NF_TPROXY_LOOKUP_ESTABLISHED:
106                         sk = inet_lookup_established(net, &tcp_hashinfo,
107                                                     saddr, sport, daddr, dport,
108                                                     in->ifindex);
109                         break;
110                 default:
111                         BUG();
112                 }
113                 break;
114                 }
115         case IPPROTO_UDP:
116                 sk = udp4_lib_lookup(net, saddr, sport, daddr, dport,
117                                      in->ifindex);
118                 if (sk) {
119                         int connected = (sk->sk_state == TCP_ESTABLISHED);
120                         int wildcard = (inet_sk(sk)->inet_rcv_saddr == 0);
121
122                         /* NOTE: we return listeners even if bound to
123                          * 0.0.0.0, those are filtered out in
124                          * xt_socket, since xt_TPROXY needs 0 bound
125                          * listeners too
126                          */
127                         if ((lookup_type == NF_TPROXY_LOOKUP_ESTABLISHED &&
128                               (!connected || wildcard)) ||
129                             (lookup_type == NF_TPROXY_LOOKUP_LISTENER && connected)) {
130                                 sock_put(sk);
131                                 sk = NULL;
132                         }
133                 }
134                 break;
135         default:
136                 WARN_ON(1);
137                 sk = NULL;
138         }
139
140         pr_debug("tproxy socket lookup: proto %u %08x:%u -> %08x:%u, lookup type: %d, sock %p\n",
141                  protocol, ntohl(saddr), ntohs(sport), ntohl(daddr), ntohs(dport), lookup_type, sk);
142
143         return sk;
144 }
145 EXPORT_SYMBOL_GPL(nf_tproxy_get_sock_v4);
146
147 MODULE_LICENSE("GPL");
148 MODULE_AUTHOR("Balazs Scheidler, Krisztian Kovacs");
149 MODULE_DESCRIPTION("Netfilter IPv4 transparent proxy support");