KOVACS Krisztian | e843927 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Transparent proxy support for Linux/iptables |
| 3 | * |
Balazs Scheidler | 6ad7889 | 2010-10-21 16:17:26 +0200 | [diff] [blame] | 4 | * Copyright (c) 2006-2010 BalaBit IT Ltd. |
KOVACS Krisztian | e843927 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 5 | * Author: Balazs Scheidler, Krisztian Kovacs |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | */ |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
KOVACS Krisztian | e843927 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 13 | #include <linux/module.h> |
| 14 | #include <linux/skbuff.h> |
| 15 | #include <linux/ip.h> |
| 16 | #include <net/checksum.h> |
| 17 | #include <net/udp.h> |
Florian Westphal | 93742cf | 2013-07-29 15:41:53 +0200 | [diff] [blame] | 18 | #include <net/tcp.h> |
KOVACS Krisztian | e843927 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 19 | #include <net/inet_sock.h> |
Florian Westphal | 93742cf | 2013-07-29 15:41:53 +0200 | [diff] [blame] | 20 | #include <net/inet_hashtables.h> |
Balazs Scheidler | cc6eb43 | 2010-10-21 16:21:10 +0200 | [diff] [blame] | 21 | #include <linux/inetdevice.h> |
KOVACS Krisztian | e843927 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 22 | #include <linux/netfilter/x_tables.h> |
| 23 | #include <linux/netfilter_ipv4/ip_tables.h> |
KOVACS Krisztian | e843927 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 24 | |
| 25 | #include <net/netfilter/ipv4/nf_defrag_ipv4.h> |
KOVACS Krisztian | f6318e5 | 2010-10-24 23:38:32 +0000 | [diff] [blame] | 26 | |
Igor Maravić | c0cd115 | 2011-12-12 02:58:24 +0000 | [diff] [blame] | 27 | #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) |
KOVACS Krisztian | f6318e5 | 2010-10-24 23:38:32 +0000 | [diff] [blame] | 28 | #define XT_TPROXY_HAVE_IPV6 1 |
Balazs Scheidler | cc6eb43 | 2010-10-21 16:21:10 +0200 | [diff] [blame] | 29 | #include <net/if_inet6.h> |
| 30 | #include <net/addrconf.h> |
Florian Westphal | 93742cf | 2013-07-29 15:41:53 +0200 | [diff] [blame] | 31 | #include <net/inet6_hashtables.h> |
Balazs Scheidler | cc6eb43 | 2010-10-21 16:21:10 +0200 | [diff] [blame] | 32 | #include <linux/netfilter_ipv6/ip6_tables.h> |
Balazs Scheidler | 6ad7889 | 2010-10-21 16:17:26 +0200 | [diff] [blame] | 33 | #include <net/netfilter/ipv6/nf_defrag_ipv6.h> |
Balazs Scheidler | cc6eb43 | 2010-10-21 16:21:10 +0200 | [diff] [blame] | 34 | #endif |
| 35 | |
Balazs Scheidler | cc6eb43 | 2010-10-21 16:21:10 +0200 | [diff] [blame] | 36 | #include <linux/netfilter/xt_TPROXY.h> |
| 37 | |
Florian Westphal | 93742cf | 2013-07-29 15:41:53 +0200 | [diff] [blame] | 38 | enum nf_tproxy_lookup_t { |
| 39 | NFT_LOOKUP_LISTENER, |
| 40 | NFT_LOOKUP_ESTABLISHED, |
| 41 | }; |
| 42 | |
Florian Westphal | d503b30 | 2011-02-17 11:32:38 +0100 | [diff] [blame] | 43 | static bool tproxy_sk_is_transparent(struct sock *sk) |
| 44 | { |
Eric Dumazet | 8b58014 | 2015-03-16 21:06:16 -0700 | [diff] [blame] | 45 | switch (sk->sk_state) { |
| 46 | case TCP_TIME_WAIT: |
Florian Westphal | d503b30 | 2011-02-17 11:32:38 +0100 | [diff] [blame] | 47 | if (inet_twsk(sk)->tw_transparent) |
| 48 | return true; |
Eric Dumazet | 8b58014 | 2015-03-16 21:06:16 -0700 | [diff] [blame] | 49 | break; |
| 50 | case TCP_NEW_SYN_RECV: |
| 51 | if (inet_rsk(inet_reqsk(sk))->no_srccheck) |
| 52 | return true; |
| 53 | break; |
| 54 | default: |
| 55 | if (inet_sk(sk)->transparent) |
| 56 | return true; |
Florian Westphal | d503b30 | 2011-02-17 11:32:38 +0100 | [diff] [blame] | 57 | } |
Eric Dumazet | 8b58014 | 2015-03-16 21:06:16 -0700 | [diff] [blame] | 58 | |
| 59 | sock_gen_put(sk); |
Florian Westphal | d503b30 | 2011-02-17 11:32:38 +0100 | [diff] [blame] | 60 | return false; |
| 61 | } |
| 62 | |
Balazs Scheidler | cc6eb43 | 2010-10-21 16:21:10 +0200 | [diff] [blame] | 63 | static inline __be32 |
| 64 | tproxy_laddr4(struct sk_buff *skb, __be32 user_laddr, __be32 daddr) |
| 65 | { |
| 66 | struct in_device *indev; |
| 67 | __be32 laddr; |
| 68 | |
| 69 | if (user_laddr) |
| 70 | return user_laddr; |
| 71 | |
| 72 | laddr = 0; |
| 73 | rcu_read_lock(); |
| 74 | indev = __in_dev_get_rcu(skb->dev); |
| 75 | for_primary_ifa(indev) { |
| 76 | laddr = ifa->ifa_local; |
| 77 | break; |
| 78 | } endfor_ifa(indev); |
| 79 | rcu_read_unlock(); |
| 80 | |
| 81 | return laddr ? laddr : daddr; |
| 82 | } |
KOVACS Krisztian | e843927 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 83 | |
Florian Westphal | 93742cf | 2013-07-29 15:41:53 +0200 | [diff] [blame] | 84 | /* |
| 85 | * This is used when the user wants to intercept a connection matching |
| 86 | * an explicit iptables rule. In this case the sockets are assumed |
| 87 | * matching in preference order: |
| 88 | * |
| 89 | * - match: if there's a fully established connection matching the |
| 90 | * _packet_ tuple, it is returned, assuming the redirection |
| 91 | * already took place and we process a packet belonging to an |
| 92 | * established connection |
| 93 | * |
| 94 | * - match: if there's a listening socket matching the redirection |
| 95 | * (e.g. on-port & on-ip of the connection), it is returned, |
| 96 | * regardless if it was bound to 0.0.0.0 or an explicit |
| 97 | * address. The reasoning is that if there's an explicit rule, it |
| 98 | * does not really matter if the listener is bound to an interface |
| 99 | * or to 0. The user already stated that he wants redirection |
| 100 | * (since he added the rule). |
| 101 | * |
| 102 | * Please note that there's an overlap between what a TPROXY target |
| 103 | * and a socket match will match. Normally if you have both rules the |
| 104 | * "socket" match will be the first one, effectively all packets |
| 105 | * belonging to established connections going through that one. |
| 106 | */ |
| 107 | static inline struct sock * |
Craig Gallek | a583636 | 2016-02-10 11:50:38 -0500 | [diff] [blame^] | 108 | nf_tproxy_get_sock_v4(struct net *net, struct sk_buff *skb, void *hp, |
| 109 | const u8 protocol, |
Florian Westphal | 93742cf | 2013-07-29 15:41:53 +0200 | [diff] [blame] | 110 | const __be32 saddr, const __be32 daddr, |
| 111 | const __be16 sport, const __be16 dport, |
| 112 | const struct net_device *in, |
| 113 | const enum nf_tproxy_lookup_t lookup_type) |
| 114 | { |
| 115 | struct sock *sk; |
Craig Gallek | a583636 | 2016-02-10 11:50:38 -0500 | [diff] [blame^] | 116 | struct tcphdr *tcph; |
Florian Westphal | 93742cf | 2013-07-29 15:41:53 +0200 | [diff] [blame] | 117 | |
| 118 | switch (protocol) { |
| 119 | case IPPROTO_TCP: |
| 120 | switch (lookup_type) { |
| 121 | case NFT_LOOKUP_LISTENER: |
Craig Gallek | a583636 | 2016-02-10 11:50:38 -0500 | [diff] [blame^] | 122 | tcph = hp; |
| 123 | sk = inet_lookup_listener(net, &tcp_hashinfo, skb, |
| 124 | ip_hdrlen(skb) + |
| 125 | __tcp_hdrlen(tcph), |
Florian Westphal | 93742cf | 2013-07-29 15:41:53 +0200 | [diff] [blame] | 126 | saddr, sport, |
| 127 | daddr, dport, |
| 128 | in->ifindex); |
| 129 | |
| 130 | /* NOTE: we return listeners even if bound to |
| 131 | * 0.0.0.0, those are filtered out in |
| 132 | * xt_socket, since xt_TPROXY needs 0 bound |
| 133 | * listeners too |
| 134 | */ |
| 135 | break; |
| 136 | case NFT_LOOKUP_ESTABLISHED: |
| 137 | sk = inet_lookup_established(net, &tcp_hashinfo, |
| 138 | saddr, sport, daddr, dport, |
| 139 | in->ifindex); |
| 140 | break; |
| 141 | default: |
| 142 | BUG(); |
| 143 | } |
| 144 | break; |
| 145 | case IPPROTO_UDP: |
| 146 | sk = udp4_lib_lookup(net, saddr, sport, daddr, dport, |
| 147 | in->ifindex); |
| 148 | if (sk) { |
| 149 | int connected = (sk->sk_state == TCP_ESTABLISHED); |
| 150 | int wildcard = (inet_sk(sk)->inet_rcv_saddr == 0); |
| 151 | |
| 152 | /* NOTE: we return listeners even if bound to |
| 153 | * 0.0.0.0, those are filtered out in |
| 154 | * xt_socket, since xt_TPROXY needs 0 bound |
| 155 | * listeners too |
| 156 | */ |
| 157 | if ((lookup_type == NFT_LOOKUP_ESTABLISHED && (!connected || wildcard)) || |
| 158 | (lookup_type == NFT_LOOKUP_LISTENER && connected)) { |
| 159 | sock_put(sk); |
| 160 | sk = NULL; |
| 161 | } |
| 162 | } |
| 163 | break; |
| 164 | default: |
| 165 | WARN_ON(1); |
| 166 | sk = NULL; |
| 167 | } |
| 168 | |
| 169 | pr_debug("tproxy socket lookup: proto %u %08x:%u -> %08x:%u, lookup type: %d, sock %p\n", |
| 170 | protocol, ntohl(saddr), ntohs(sport), ntohl(daddr), ntohs(dport), lookup_type, sk); |
| 171 | |
| 172 | return sk; |
| 173 | } |
| 174 | |
Florian Westphal | d8b3bfc | 2013-08-04 11:37:29 +0200 | [diff] [blame] | 175 | #ifdef XT_TPROXY_HAVE_IPV6 |
Florian Westphal | 93742cf | 2013-07-29 15:41:53 +0200 | [diff] [blame] | 176 | static inline struct sock * |
Craig Gallek | a583636 | 2016-02-10 11:50:38 -0500 | [diff] [blame^] | 177 | nf_tproxy_get_sock_v6(struct net *net, struct sk_buff *skb, int thoff, void *hp, |
| 178 | const u8 protocol, |
Florian Westphal | 93742cf | 2013-07-29 15:41:53 +0200 | [diff] [blame] | 179 | const struct in6_addr *saddr, const struct in6_addr *daddr, |
| 180 | const __be16 sport, const __be16 dport, |
| 181 | const struct net_device *in, |
| 182 | const enum nf_tproxy_lookup_t lookup_type) |
| 183 | { |
| 184 | struct sock *sk; |
Craig Gallek | a583636 | 2016-02-10 11:50:38 -0500 | [diff] [blame^] | 185 | struct tcphdr *tcph; |
Florian Westphal | 93742cf | 2013-07-29 15:41:53 +0200 | [diff] [blame] | 186 | |
| 187 | switch (protocol) { |
| 188 | case IPPROTO_TCP: |
| 189 | switch (lookup_type) { |
| 190 | case NFT_LOOKUP_LISTENER: |
Craig Gallek | a583636 | 2016-02-10 11:50:38 -0500 | [diff] [blame^] | 191 | tcph = hp; |
| 192 | sk = inet6_lookup_listener(net, &tcp_hashinfo, skb, |
| 193 | thoff + __tcp_hdrlen(tcph), |
Florian Westphal | 93742cf | 2013-07-29 15:41:53 +0200 | [diff] [blame] | 194 | saddr, sport, |
| 195 | daddr, ntohs(dport), |
| 196 | in->ifindex); |
| 197 | |
| 198 | /* NOTE: we return listeners even if bound to |
| 199 | * 0.0.0.0, those are filtered out in |
| 200 | * xt_socket, since xt_TPROXY needs 0 bound |
| 201 | * listeners too |
| 202 | */ |
| 203 | break; |
| 204 | case NFT_LOOKUP_ESTABLISHED: |
| 205 | sk = __inet6_lookup_established(net, &tcp_hashinfo, |
| 206 | saddr, sport, daddr, ntohs(dport), |
| 207 | in->ifindex); |
| 208 | break; |
| 209 | default: |
| 210 | BUG(); |
| 211 | } |
| 212 | break; |
| 213 | case IPPROTO_UDP: |
| 214 | sk = udp6_lib_lookup(net, saddr, sport, daddr, dport, |
| 215 | in->ifindex); |
| 216 | if (sk) { |
| 217 | int connected = (sk->sk_state == TCP_ESTABLISHED); |
Eric Dumazet | efe4208 | 2013-10-03 15:42:29 -0700 | [diff] [blame] | 218 | int wildcard = ipv6_addr_any(&sk->sk_v6_rcv_saddr); |
Florian Westphal | 93742cf | 2013-07-29 15:41:53 +0200 | [diff] [blame] | 219 | |
| 220 | /* NOTE: we return listeners even if bound to |
| 221 | * 0.0.0.0, those are filtered out in |
| 222 | * xt_socket, since xt_TPROXY needs 0 bound |
| 223 | * listeners too |
| 224 | */ |
| 225 | if ((lookup_type == NFT_LOOKUP_ESTABLISHED && (!connected || wildcard)) || |
| 226 | (lookup_type == NFT_LOOKUP_LISTENER && connected)) { |
| 227 | sock_put(sk); |
| 228 | sk = NULL; |
| 229 | } |
| 230 | } |
| 231 | break; |
| 232 | default: |
| 233 | WARN_ON(1); |
| 234 | sk = NULL; |
| 235 | } |
| 236 | |
| 237 | pr_debug("tproxy socket lookup: proto %u %pI6:%u -> %pI6:%u, lookup type: %d, sock %p\n", |
| 238 | protocol, saddr, ntohs(sport), daddr, ntohs(dport), lookup_type, sk); |
| 239 | |
| 240 | return sk; |
| 241 | } |
| 242 | #endif |
| 243 | |
Balazs Scheidler | 106e4c2 | 2010-10-21 12:45:14 +0200 | [diff] [blame] | 244 | /** |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 245 | * tproxy_handle_time_wait4 - handle IPv4 TCP TIME_WAIT reopen redirections |
Balazs Scheidler | 106e4c2 | 2010-10-21 12:45:14 +0200 | [diff] [blame] | 246 | * @skb: The skb being processed. |
Balazs Scheidler | 6ad7889 | 2010-10-21 16:17:26 +0200 | [diff] [blame] | 247 | * @laddr: IPv4 address to redirect to or zero. |
| 248 | * @lport: TCP port to redirect to or zero. |
Balazs Scheidler | 106e4c2 | 2010-10-21 12:45:14 +0200 | [diff] [blame] | 249 | * @sk: The TIME_WAIT TCP socket found by the lookup. |
| 250 | * |
| 251 | * We have to handle SYN packets arriving to TIME_WAIT sockets |
| 252 | * differently: instead of reopening the connection we should rather |
| 253 | * redirect the new connection to the proxy if there's a listener |
| 254 | * socket present. |
| 255 | * |
Balazs Scheidler | 6ad7889 | 2010-10-21 16:17:26 +0200 | [diff] [blame] | 256 | * tproxy_handle_time_wait4() consumes the socket reference passed in. |
Balazs Scheidler | 106e4c2 | 2010-10-21 12:45:14 +0200 | [diff] [blame] | 257 | * |
| 258 | * Returns the listener socket if there's one, the TIME_WAIT socket if |
| 259 | * no such listener is found, or NULL if the TCP header is incomplete. |
| 260 | */ |
| 261 | static struct sock * |
Eric W. Biederman | 686c9b5 | 2015-09-18 14:32:59 -0500 | [diff] [blame] | 262 | tproxy_handle_time_wait4(struct net *net, struct sk_buff *skb, |
| 263 | __be32 laddr, __be16 lport, struct sock *sk) |
Balazs Scheidler | 106e4c2 | 2010-10-21 12:45:14 +0200 | [diff] [blame] | 264 | { |
| 265 | const struct iphdr *iph = ip_hdr(skb); |
Balazs Scheidler | 106e4c2 | 2010-10-21 12:45:14 +0200 | [diff] [blame] | 266 | struct tcphdr _hdr, *hp; |
| 267 | |
| 268 | hp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_hdr), &_hdr); |
| 269 | if (hp == NULL) { |
| 270 | inet_twsk_put(inet_twsk(sk)); |
| 271 | return NULL; |
| 272 | } |
| 273 | |
| 274 | if (hp->syn && !hp->rst && !hp->ack && !hp->fin) { |
| 275 | /* SYN to a TIME_WAIT socket, we'd rather redirect it |
| 276 | * to a listener socket if there's one */ |
| 277 | struct sock *sk2; |
| 278 | |
Craig Gallek | a583636 | 2016-02-10 11:50:38 -0500 | [diff] [blame^] | 279 | sk2 = nf_tproxy_get_sock_v4(net, skb, hp, iph->protocol, |
Balazs Scheidler | 6ad7889 | 2010-10-21 16:17:26 +0200 | [diff] [blame] | 280 | iph->saddr, laddr ? laddr : iph->daddr, |
| 281 | hp->source, lport ? lport : hp->dest, |
| 282 | skb->dev, NFT_LOOKUP_LISTENER); |
Balazs Scheidler | 106e4c2 | 2010-10-21 12:45:14 +0200 | [diff] [blame] | 283 | if (sk2) { |
Eric Dumazet | dbe7faa | 2015-07-08 14:28:30 -0700 | [diff] [blame] | 284 | inet_twsk_deschedule_put(inet_twsk(sk)); |
Balazs Scheidler | 6ad7889 | 2010-10-21 16:17:26 +0200 | [diff] [blame] | 285 | sk = sk2; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | return sk; |
| 290 | } |
| 291 | |
Florian Westphal | fd158d7 | 2013-07-29 15:41:52 +0200 | [diff] [blame] | 292 | /* assign a socket to the skb -- consumes sk */ |
| 293 | static void |
| 294 | nf_tproxy_assign_sock(struct sk_buff *skb, struct sock *sk) |
| 295 | { |
| 296 | skb_orphan(skb); |
| 297 | skb->sk = sk; |
| 298 | skb->destructor = sock_edemux; |
| 299 | } |
| 300 | |
Balazs Scheidler | cc6eb43 | 2010-10-21 16:21:10 +0200 | [diff] [blame] | 301 | static unsigned int |
Eric W. Biederman | 686c9b5 | 2015-09-18 14:32:59 -0500 | [diff] [blame] | 302 | tproxy_tg4(struct net *net, struct sk_buff *skb, __be32 laddr, __be16 lport, |
Balazs Scheidler | cc6eb43 | 2010-10-21 16:21:10 +0200 | [diff] [blame] | 303 | u_int32_t mark_mask, u_int32_t mark_value) |
| 304 | { |
| 305 | const struct iphdr *iph = ip_hdr(skb); |
| 306 | struct udphdr _hdr, *hp; |
| 307 | struct sock *sk; |
| 308 | |
| 309 | hp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_hdr), &_hdr); |
| 310 | if (hp == NULL) |
| 311 | return NF_DROP; |
| 312 | |
| 313 | /* check if there's an ongoing connection on the packet |
| 314 | * addresses, this happens if the redirect already happened |
| 315 | * and the current packet belongs to an already established |
| 316 | * connection */ |
Craig Gallek | a583636 | 2016-02-10 11:50:38 -0500 | [diff] [blame^] | 317 | sk = nf_tproxy_get_sock_v4(net, skb, hp, iph->protocol, |
Balazs Scheidler | cc6eb43 | 2010-10-21 16:21:10 +0200 | [diff] [blame] | 318 | iph->saddr, iph->daddr, |
| 319 | hp->source, hp->dest, |
| 320 | skb->dev, NFT_LOOKUP_ESTABLISHED); |
| 321 | |
| 322 | laddr = tproxy_laddr4(skb, laddr, iph->daddr); |
| 323 | if (!lport) |
| 324 | lport = hp->dest; |
| 325 | |
| 326 | /* UDP has no TCP_TIME_WAIT state, so we never enter here */ |
| 327 | if (sk && sk->sk_state == TCP_TIME_WAIT) |
| 328 | /* reopening a TIME_WAIT connection needs special handling */ |
Eric W. Biederman | 686c9b5 | 2015-09-18 14:32:59 -0500 | [diff] [blame] | 329 | sk = tproxy_handle_time_wait4(net, skb, laddr, lport, sk); |
Balazs Scheidler | cc6eb43 | 2010-10-21 16:21:10 +0200 | [diff] [blame] | 330 | else if (!sk) |
| 331 | /* no, there's no established connection, check if |
| 332 | * there's a listener on the redirected addr/port */ |
Craig Gallek | a583636 | 2016-02-10 11:50:38 -0500 | [diff] [blame^] | 333 | sk = nf_tproxy_get_sock_v4(net, skb, hp, iph->protocol, |
Balazs Scheidler | cc6eb43 | 2010-10-21 16:21:10 +0200 | [diff] [blame] | 334 | iph->saddr, laddr, |
| 335 | hp->source, lport, |
| 336 | skb->dev, NFT_LOOKUP_LISTENER); |
| 337 | |
| 338 | /* NOTE: assign_sock consumes our sk reference */ |
Florian Westphal | d503b30 | 2011-02-17 11:32:38 +0100 | [diff] [blame] | 339 | if (sk && tproxy_sk_is_transparent(sk)) { |
Balazs Scheidler | cc6eb43 | 2010-10-21 16:21:10 +0200 | [diff] [blame] | 340 | /* This should be in a separate target, but we don't do multiple |
| 341 | targets on the same rule yet */ |
| 342 | skb->mark = (skb->mark & ~mark_mask) ^ mark_value; |
| 343 | |
| 344 | pr_debug("redirecting: proto %hhu %pI4:%hu -> %pI4:%hu, mark: %x\n", |
| 345 | iph->protocol, &iph->daddr, ntohs(hp->dest), |
| 346 | &laddr, ntohs(lport), skb->mark); |
Florian Westphal | d503b30 | 2011-02-17 11:32:38 +0100 | [diff] [blame] | 347 | |
| 348 | nf_tproxy_assign_sock(skb, sk); |
Balazs Scheidler | cc6eb43 | 2010-10-21 16:21:10 +0200 | [diff] [blame] | 349 | return NF_ACCEPT; |
| 350 | } |
| 351 | |
| 352 | pr_debug("no socket, dropping: proto %hhu %pI4:%hu -> %pI4:%hu, mark: %x\n", |
| 353 | iph->protocol, &iph->saddr, ntohs(hp->source), |
| 354 | &iph->daddr, ntohs(hp->dest), skb->mark); |
| 355 | return NF_DROP; |
| 356 | } |
| 357 | |
| 358 | static unsigned int |
| 359 | tproxy_tg4_v0(struct sk_buff *skb, const struct xt_action_param *par) |
| 360 | { |
| 361 | const struct xt_tproxy_target_info *tgi = par->targinfo; |
| 362 | |
Eric W. Biederman | 686c9b5 | 2015-09-18 14:32:59 -0500 | [diff] [blame] | 363 | return tproxy_tg4(par->net, skb, tgi->laddr, tgi->lport, tgi->mark_mask, tgi->mark_value); |
Balazs Scheidler | cc6eb43 | 2010-10-21 16:21:10 +0200 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | static unsigned int |
| 367 | tproxy_tg4_v1(struct sk_buff *skb, const struct xt_action_param *par) |
| 368 | { |
| 369 | const struct xt_tproxy_target_info_v1 *tgi = par->targinfo; |
| 370 | |
Eric W. Biederman | 686c9b5 | 2015-09-18 14:32:59 -0500 | [diff] [blame] | 371 | return tproxy_tg4(par->net, skb, tgi->laddr.ip, tgi->lport, tgi->mark_mask, tgi->mark_value); |
Balazs Scheidler | cc6eb43 | 2010-10-21 16:21:10 +0200 | [diff] [blame] | 372 | } |
| 373 | |
KOVACS Krisztian | f6318e5 | 2010-10-24 23:38:32 +0000 | [diff] [blame] | 374 | #ifdef XT_TPROXY_HAVE_IPV6 |
Balazs Scheidler | cc6eb43 | 2010-10-21 16:21:10 +0200 | [diff] [blame] | 375 | |
| 376 | static inline const struct in6_addr * |
| 377 | tproxy_laddr6(struct sk_buff *skb, const struct in6_addr *user_laddr, |
| 378 | const struct in6_addr *daddr) |
| 379 | { |
| 380 | struct inet6_dev *indev; |
| 381 | struct inet6_ifaddr *ifa; |
| 382 | struct in6_addr *laddr; |
| 383 | |
| 384 | if (!ipv6_addr_any(user_laddr)) |
| 385 | return user_laddr; |
| 386 | laddr = NULL; |
| 387 | |
| 388 | rcu_read_lock(); |
| 389 | indev = __in6_dev_get(skb->dev); |
| 390 | if (indev) |
| 391 | list_for_each_entry(ifa, &indev->addr_list, if_list) { |
| 392 | if (ifa->flags & (IFA_F_TENTATIVE | IFA_F_DEPRECATED)) |
| 393 | continue; |
| 394 | |
| 395 | laddr = &ifa->addr; |
| 396 | break; |
| 397 | } |
| 398 | rcu_read_unlock(); |
| 399 | |
| 400 | return laddr ? laddr : daddr; |
| 401 | } |
| 402 | |
Balazs Scheidler | 6ad7889 | 2010-10-21 16:17:26 +0200 | [diff] [blame] | 403 | /** |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 404 | * tproxy_handle_time_wait6 - handle IPv6 TCP TIME_WAIT reopen redirections |
Balazs Scheidler | 6ad7889 | 2010-10-21 16:17:26 +0200 | [diff] [blame] | 405 | * @skb: The skb being processed. |
| 406 | * @tproto: Transport protocol. |
| 407 | * @thoff: Transport protocol header offset. |
| 408 | * @par: Iptables target parameters. |
| 409 | * @sk: The TIME_WAIT TCP socket found by the lookup. |
| 410 | * |
| 411 | * We have to handle SYN packets arriving to TIME_WAIT sockets |
| 412 | * differently: instead of reopening the connection we should rather |
| 413 | * redirect the new connection to the proxy if there's a listener |
| 414 | * socket present. |
| 415 | * |
| 416 | * tproxy_handle_time_wait6() consumes the socket reference passed in. |
| 417 | * |
| 418 | * Returns the listener socket if there's one, the TIME_WAIT socket if |
| 419 | * no such listener is found, or NULL if the TCP header is incomplete. |
| 420 | */ |
| 421 | static struct sock * |
| 422 | tproxy_handle_time_wait6(struct sk_buff *skb, int tproto, int thoff, |
| 423 | const struct xt_action_param *par, |
| 424 | struct sock *sk) |
| 425 | { |
| 426 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
| 427 | struct tcphdr _hdr, *hp; |
| 428 | const struct xt_tproxy_target_info_v1 *tgi = par->targinfo; |
| 429 | |
| 430 | hp = skb_header_pointer(skb, thoff, sizeof(_hdr), &_hdr); |
| 431 | if (hp == NULL) { |
| 432 | inet_twsk_put(inet_twsk(sk)); |
| 433 | return NULL; |
| 434 | } |
| 435 | |
| 436 | if (hp->syn && !hp->rst && !hp->ack && !hp->fin) { |
| 437 | /* SYN to a TIME_WAIT socket, we'd rather redirect it |
| 438 | * to a listener socket if there's one */ |
| 439 | struct sock *sk2; |
| 440 | |
Craig Gallek | a583636 | 2016-02-10 11:50:38 -0500 | [diff] [blame^] | 441 | sk2 = nf_tproxy_get_sock_v6(par->net, skb, thoff, hp, tproto, |
Balazs Scheidler | 6ad7889 | 2010-10-21 16:17:26 +0200 | [diff] [blame] | 442 | &iph->saddr, |
Balazs Scheidler | cc6eb43 | 2010-10-21 16:21:10 +0200 | [diff] [blame] | 443 | tproxy_laddr6(skb, &tgi->laddr.in6, &iph->daddr), |
Balazs Scheidler | 6ad7889 | 2010-10-21 16:17:26 +0200 | [diff] [blame] | 444 | hp->source, |
| 445 | tgi->lport ? tgi->lport : hp->dest, |
| 446 | skb->dev, NFT_LOOKUP_LISTENER); |
| 447 | if (sk2) { |
Eric Dumazet | dbe7faa | 2015-07-08 14:28:30 -0700 | [diff] [blame] | 448 | inet_twsk_deschedule_put(inet_twsk(sk)); |
Balazs Scheidler | 106e4c2 | 2010-10-21 12:45:14 +0200 | [diff] [blame] | 449 | sk = sk2; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | return sk; |
| 454 | } |
| 455 | |
KOVACS Krisztian | e843927 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 456 | static unsigned int |
Balazs Scheidler | 6ad7889 | 2010-10-21 16:17:26 +0200 | [diff] [blame] | 457 | tproxy_tg6_v1(struct sk_buff *skb, const struct xt_action_param *par) |
| 458 | { |
| 459 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
| 460 | const struct xt_tproxy_target_info_v1 *tgi = par->targinfo; |
| 461 | struct udphdr _hdr, *hp; |
| 462 | struct sock *sk; |
Balazs Scheidler | cc6eb43 | 2010-10-21 16:21:10 +0200 | [diff] [blame] | 463 | const struct in6_addr *laddr; |
| 464 | __be16 lport; |
Hans Schillstrom | 84018f5 | 2012-04-23 03:35:26 +0000 | [diff] [blame] | 465 | int thoff = 0; |
Balazs Scheidler | 6ad7889 | 2010-10-21 16:17:26 +0200 | [diff] [blame] | 466 | int tproto; |
| 467 | |
Hans Schillstrom | 84018f5 | 2012-04-23 03:35:26 +0000 | [diff] [blame] | 468 | tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL); |
Balazs Scheidler | 6ad7889 | 2010-10-21 16:17:26 +0200 | [diff] [blame] | 469 | if (tproto < 0) { |
| 470 | pr_debug("unable to find transport header in IPv6 packet, dropping\n"); |
| 471 | return NF_DROP; |
| 472 | } |
| 473 | |
| 474 | hp = skb_header_pointer(skb, thoff, sizeof(_hdr), &_hdr); |
| 475 | if (hp == NULL) { |
| 476 | pr_debug("unable to grab transport header contents in IPv6 packet, dropping\n"); |
| 477 | return NF_DROP; |
| 478 | } |
| 479 | |
| 480 | /* check if there's an ongoing connection on the packet |
| 481 | * addresses, this happens if the redirect already happened |
| 482 | * and the current packet belongs to an already established |
| 483 | * connection */ |
Craig Gallek | a583636 | 2016-02-10 11:50:38 -0500 | [diff] [blame^] | 484 | sk = nf_tproxy_get_sock_v6(par->net, skb, thoff, hp, tproto, |
Balazs Scheidler | 6ad7889 | 2010-10-21 16:17:26 +0200 | [diff] [blame] | 485 | &iph->saddr, &iph->daddr, |
| 486 | hp->source, hp->dest, |
Balazs Scheidler | 106e4c2 | 2010-10-21 12:45:14 +0200 | [diff] [blame] | 487 | par->in, NFT_LOOKUP_ESTABLISHED); |
| 488 | |
Balazs Scheidler | cc6eb43 | 2010-10-21 16:21:10 +0200 | [diff] [blame] | 489 | laddr = tproxy_laddr6(skb, &tgi->laddr.in6, &iph->daddr); |
| 490 | lport = tgi->lport ? tgi->lport : hp->dest; |
| 491 | |
Balazs Scheidler | 106e4c2 | 2010-10-21 12:45:14 +0200 | [diff] [blame] | 492 | /* UDP has no TCP_TIME_WAIT state, so we never enter here */ |
| 493 | if (sk && sk->sk_state == TCP_TIME_WAIT) |
Balazs Scheidler | 6ad7889 | 2010-10-21 16:17:26 +0200 | [diff] [blame] | 494 | /* reopening a TIME_WAIT connection needs special handling */ |
| 495 | sk = tproxy_handle_time_wait6(skb, tproto, thoff, par, sk); |
Balazs Scheidler | 106e4c2 | 2010-10-21 12:45:14 +0200 | [diff] [blame] | 496 | else if (!sk) |
Balazs Scheidler | 6ad7889 | 2010-10-21 16:17:26 +0200 | [diff] [blame] | 497 | /* no there's no established connection, check if |
| 498 | * there's a listener on the redirected addr/port */ |
Craig Gallek | a583636 | 2016-02-10 11:50:38 -0500 | [diff] [blame^] | 499 | sk = nf_tproxy_get_sock_v6(par->net, skb, thoff, hp, |
| 500 | tproto, &iph->saddr, laddr, |
Balazs Scheidler | cc6eb43 | 2010-10-21 16:21:10 +0200 | [diff] [blame] | 501 | hp->source, lport, |
Balazs Scheidler | 106e4c2 | 2010-10-21 12:45:14 +0200 | [diff] [blame] | 502 | par->in, NFT_LOOKUP_LISTENER); |
KOVACS Krisztian | e843927 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 503 | |
| 504 | /* NOTE: assign_sock consumes our sk reference */ |
Florian Westphal | d503b30 | 2011-02-17 11:32:38 +0100 | [diff] [blame] | 505 | if (sk && tproxy_sk_is_transparent(sk)) { |
KOVACS Krisztian | e843927 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 506 | /* This should be in a separate target, but we don't do multiple |
| 507 | targets on the same rule yet */ |
| 508 | skb->mark = (skb->mark & ~tgi->mark_mask) ^ tgi->mark_value; |
| 509 | |
Balazs Scheidler | 6ad7889 | 2010-10-21 16:17:26 +0200 | [diff] [blame] | 510 | pr_debug("redirecting: proto %hhu %pI6:%hu -> %pI6:%hu, mark: %x\n", |
Balazs Scheidler | cc6eb43 | 2010-10-21 16:21:10 +0200 | [diff] [blame] | 511 | tproto, &iph->saddr, ntohs(hp->source), |
| 512 | laddr, ntohs(lport), skb->mark); |
Florian Westphal | d503b30 | 2011-02-17 11:32:38 +0100 | [diff] [blame] | 513 | |
| 514 | nf_tproxy_assign_sock(skb, sk); |
KOVACS Krisztian | e843927 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 515 | return NF_ACCEPT; |
| 516 | } |
| 517 | |
Balazs Scheidler | 6ad7889 | 2010-10-21 16:17:26 +0200 | [diff] [blame] | 518 | pr_debug("no socket, dropping: proto %hhu %pI6:%hu -> %pI6:%hu, mark: %x\n", |
Balazs Scheidler | cc6eb43 | 2010-10-21 16:21:10 +0200 | [diff] [blame] | 519 | tproto, &iph->saddr, ntohs(hp->source), |
| 520 | &iph->daddr, ntohs(hp->dest), skb->mark); |
| 521 | |
KOVACS Krisztian | e843927 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 522 | return NF_DROP; |
| 523 | } |
| 524 | |
Balazs Scheidler | 6ad7889 | 2010-10-21 16:17:26 +0200 | [diff] [blame] | 525 | static int tproxy_tg6_check(const struct xt_tgchk_param *par) |
| 526 | { |
| 527 | const struct ip6t_ip6 *i = par->entryinfo; |
| 528 | |
Pablo Neira Ayuso | 3d8c6dc | 2015-03-20 13:56:06 +0100 | [diff] [blame] | 529 | if ((i->proto == IPPROTO_TCP || i->proto == IPPROTO_UDP) && |
| 530 | !(i->invflags & IP6T_INV_PROTO)) |
Balazs Scheidler | 6ad7889 | 2010-10-21 16:17:26 +0200 | [diff] [blame] | 531 | return 0; |
| 532 | |
| 533 | pr_info("Can be used only in combination with " |
| 534 | "either -p tcp or -p udp\n"); |
| 535 | return -EINVAL; |
| 536 | } |
| 537 | #endif |
| 538 | |
| 539 | static int tproxy_tg4_check(const struct xt_tgchk_param *par) |
KOVACS Krisztian | e843927 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 540 | { |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 541 | const struct ipt_ip *i = par->entryinfo; |
KOVACS Krisztian | e843927 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 542 | |
| 543 | if ((i->proto == IPPROTO_TCP || i->proto == IPPROTO_UDP) |
| 544 | && !(i->invflags & IPT_INV_PROTO)) |
Jan Engelhardt | d6b00a5 | 2010-03-25 16:34:45 +0100 | [diff] [blame] | 545 | return 0; |
KOVACS Krisztian | e843927 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 546 | |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 547 | pr_info("Can be used only in combination with " |
KOVACS Krisztian | e843927 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 548 | "either -p tcp or -p udp\n"); |
Jan Engelhardt | d6b00a5 | 2010-03-25 16:34:45 +0100 | [diff] [blame] | 549 | return -EINVAL; |
KOVACS Krisztian | e843927 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 550 | } |
| 551 | |
Balazs Scheidler | 6ad7889 | 2010-10-21 16:17:26 +0200 | [diff] [blame] | 552 | static struct xt_target tproxy_tg_reg[] __read_mostly = { |
| 553 | { |
| 554 | .name = "TPROXY", |
| 555 | .family = NFPROTO_IPV4, |
| 556 | .table = "mangle", |
| 557 | .target = tproxy_tg4_v0, |
| 558 | .revision = 0, |
| 559 | .targetsize = sizeof(struct xt_tproxy_target_info), |
| 560 | .checkentry = tproxy_tg4_check, |
| 561 | .hooks = 1 << NF_INET_PRE_ROUTING, |
| 562 | .me = THIS_MODULE, |
| 563 | }, |
| 564 | { |
| 565 | .name = "TPROXY", |
| 566 | .family = NFPROTO_IPV4, |
| 567 | .table = "mangle", |
| 568 | .target = tproxy_tg4_v1, |
| 569 | .revision = 1, |
| 570 | .targetsize = sizeof(struct xt_tproxy_target_info_v1), |
| 571 | .checkentry = tproxy_tg4_check, |
| 572 | .hooks = 1 << NF_INET_PRE_ROUTING, |
| 573 | .me = THIS_MODULE, |
| 574 | }, |
KOVACS Krisztian | f6318e5 | 2010-10-24 23:38:32 +0000 | [diff] [blame] | 575 | #ifdef XT_TPROXY_HAVE_IPV6 |
Balazs Scheidler | 6ad7889 | 2010-10-21 16:17:26 +0200 | [diff] [blame] | 576 | { |
| 577 | .name = "TPROXY", |
| 578 | .family = NFPROTO_IPV6, |
| 579 | .table = "mangle", |
| 580 | .target = tproxy_tg6_v1, |
| 581 | .revision = 1, |
| 582 | .targetsize = sizeof(struct xt_tproxy_target_info_v1), |
| 583 | .checkentry = tproxy_tg6_check, |
| 584 | .hooks = 1 << NF_INET_PRE_ROUTING, |
| 585 | .me = THIS_MODULE, |
| 586 | }, |
| 587 | #endif |
| 588 | |
KOVACS Krisztian | e843927 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 589 | }; |
| 590 | |
| 591 | static int __init tproxy_tg_init(void) |
| 592 | { |
| 593 | nf_defrag_ipv4_enable(); |
KOVACS Krisztian | f6318e5 | 2010-10-24 23:38:32 +0000 | [diff] [blame] | 594 | #ifdef XT_TPROXY_HAVE_IPV6 |
Balazs Scheidler | 6ad7889 | 2010-10-21 16:17:26 +0200 | [diff] [blame] | 595 | nf_defrag_ipv6_enable(); |
| 596 | #endif |
| 597 | |
| 598 | return xt_register_targets(tproxy_tg_reg, ARRAY_SIZE(tproxy_tg_reg)); |
KOVACS Krisztian | e843927 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | static void __exit tproxy_tg_exit(void) |
| 602 | { |
Balazs Scheidler | 6ad7889 | 2010-10-21 16:17:26 +0200 | [diff] [blame] | 603 | xt_unregister_targets(tproxy_tg_reg, ARRAY_SIZE(tproxy_tg_reg)); |
KOVACS Krisztian | e843927 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | module_init(tproxy_tg_init); |
| 607 | module_exit(tproxy_tg_exit); |
| 608 | MODULE_LICENSE("GPL"); |
Balazs Scheidler | 6ad7889 | 2010-10-21 16:17:26 +0200 | [diff] [blame] | 609 | MODULE_AUTHOR("Balazs Scheidler, Krisztian Kovacs"); |
KOVACS Krisztian | e843927 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 610 | MODULE_DESCRIPTION("Netfilter transparent proxy (TPROXY) target module."); |
| 611 | MODULE_ALIAS("ipt_TPROXY"); |
Balazs Scheidler | 6ad7889 | 2010-10-21 16:17:26 +0200 | [diff] [blame] | 612 | MODULE_ALIAS("ip6t_TPROXY"); |