KOVACS Krisztian | 136cdc7 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Transparent proxy support for Linux/iptables |
| 3 | * |
| 4 | * Copyright (C) 2007-2008 BalaBit IT Ltd. |
| 5 | * Author: 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 | 136cdc7 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 13 | #include <linux/module.h> |
| 14 | #include <linux/skbuff.h> |
| 15 | #include <linux/netfilter/x_tables.h> |
| 16 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 17 | #include <net/tcp.h> |
| 18 | #include <net/udp.h> |
| 19 | #include <net/icmp.h> |
| 20 | #include <net/sock.h> |
| 21 | #include <net/inet_sock.h> |
KOVACS Krisztian | 136cdc7 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 22 | #include <net/netfilter/ipv4/nf_defrag_ipv4.h> |
KOVACS Krisztian | f6318e5 | 2010-10-24 23:38:32 +0000 | [diff] [blame] | 23 | |
Igor Maravić | c0cd115 | 2011-12-12 02:58:24 +0000 | [diff] [blame] | 24 | #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) |
KOVACS Krisztian | f6318e5 | 2010-10-24 23:38:32 +0000 | [diff] [blame] | 25 | #define XT_SOCKET_HAVE_IPV6 1 |
| 26 | #include <linux/netfilter_ipv6/ip6_tables.h> |
Florian Westphal | 93742cf | 2013-07-29 15:41:53 +0200 | [diff] [blame] | 27 | #include <net/inet6_hashtables.h> |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 28 | #include <net/netfilter/ipv6/nf_defrag_ipv6.h> |
KOVACS Krisztian | f6318e5 | 2010-10-24 23:38:32 +0000 | [diff] [blame] | 29 | #endif |
KOVACS Krisztian | 136cdc7 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 30 | |
Laszlo Attila Toth | a31e1ff | 2009-06-09 15:16:34 +0200 | [diff] [blame] | 31 | #include <linux/netfilter/xt_socket.h> |
| 32 | |
Igor Maravić | c0cd115 | 2011-12-12 02:58:24 +0000 | [diff] [blame] | 33 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
KOVACS Krisztian | 136cdc7 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 34 | #define XT_SOCKET_HAVE_CONNTRACK 1 |
| 35 | #include <net/netfilter/nf_conntrack.h> |
| 36 | #endif |
| 37 | |
John Stultz | 994ee75 | 2014-03-28 16:23:48 -0700 | [diff] [blame^] | 38 | void |
| 39 | xt_socket_put_sk(struct sock *sk) |
| 40 | { |
| 41 | if (sk->sk_state == TCP_TIME_WAIT) |
| 42 | inet_twsk_put(inet_twsk(sk)); |
| 43 | else |
| 44 | sock_put(sk); |
| 45 | } |
| 46 | EXPORT_SYMBOL(xt_socket_put_sk); |
| 47 | |
KOVACS Krisztian | 136cdc7 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 48 | static int |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 49 | extract_icmp4_fields(const struct sk_buff *skb, |
KOVACS Krisztian | 136cdc7 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 50 | u8 *protocol, |
| 51 | __be32 *raddr, |
| 52 | __be32 *laddr, |
| 53 | __be16 *rport, |
| 54 | __be16 *lport) |
| 55 | { |
| 56 | unsigned int outside_hdrlen = ip_hdrlen(skb); |
| 57 | struct iphdr *inside_iph, _inside_iph; |
| 58 | struct icmphdr *icmph, _icmph; |
| 59 | __be16 *ports, _ports[2]; |
| 60 | |
| 61 | icmph = skb_header_pointer(skb, outside_hdrlen, |
| 62 | sizeof(_icmph), &_icmph); |
| 63 | if (icmph == NULL) |
| 64 | return 1; |
| 65 | |
| 66 | switch (icmph->type) { |
| 67 | case ICMP_DEST_UNREACH: |
| 68 | case ICMP_SOURCE_QUENCH: |
| 69 | case ICMP_REDIRECT: |
| 70 | case ICMP_TIME_EXCEEDED: |
| 71 | case ICMP_PARAMETERPROB: |
| 72 | break; |
| 73 | default: |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | inside_iph = skb_header_pointer(skb, outside_hdrlen + |
| 78 | sizeof(struct icmphdr), |
| 79 | sizeof(_inside_iph), &_inside_iph); |
| 80 | if (inside_iph == NULL) |
| 81 | return 1; |
| 82 | |
| 83 | if (inside_iph->protocol != IPPROTO_TCP && |
| 84 | inside_iph->protocol != IPPROTO_UDP) |
| 85 | return 1; |
| 86 | |
| 87 | ports = skb_header_pointer(skb, outside_hdrlen + |
| 88 | sizeof(struct icmphdr) + |
| 89 | (inside_iph->ihl << 2), |
| 90 | sizeof(_ports), &_ports); |
| 91 | if (ports == NULL) |
| 92 | return 1; |
| 93 | |
| 94 | /* the inside IP packet is the one quoted from our side, thus |
| 95 | * its saddr is the local address */ |
| 96 | *protocol = inside_iph->protocol; |
| 97 | *laddr = inside_iph->saddr; |
| 98 | *lport = ports[0]; |
| 99 | *raddr = inside_iph->daddr; |
| 100 | *rport = ports[1]; |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
Florian Westphal | 93742cf | 2013-07-29 15:41:53 +0200 | [diff] [blame] | 105 | /* "socket" match based redirection (no specific rule) |
| 106 | * =================================================== |
| 107 | * |
| 108 | * There are connections with dynamic endpoints (e.g. FTP data |
| 109 | * connection) that the user is unable to add explicit rules |
| 110 | * for. These are taken care of by a generic "socket" rule. It is |
| 111 | * assumed that the proxy application is trusted to open such |
| 112 | * connections without explicit iptables rule (except of course the |
| 113 | * generic 'socket' rule). In this case the following sockets are |
| 114 | * matched in preference order: |
| 115 | * |
| 116 | * - match: if there's a fully established connection matching the |
| 117 | * _packet_ tuple |
| 118 | * |
| 119 | * - match: if there's a non-zero bound listener (possibly with a |
| 120 | * non-local address) We don't accept zero-bound listeners, since |
| 121 | * then local services could intercept traffic going through the |
| 122 | * box. |
| 123 | */ |
| 124 | static struct sock * |
Craig Gallek | a583636 | 2016-02-10 11:50:38 -0500 | [diff] [blame] | 125 | xt_socket_get_sock_v4(struct net *net, struct sk_buff *skb, const int doff, |
| 126 | const u8 protocol, |
Florian Westphal | 93742cf | 2013-07-29 15:41:53 +0200 | [diff] [blame] | 127 | const __be32 saddr, const __be32 daddr, |
| 128 | const __be16 sport, const __be16 dport, |
| 129 | const struct net_device *in) |
| 130 | { |
| 131 | switch (protocol) { |
| 132 | case IPPROTO_TCP: |
Eric Dumazet | 3b24d85 | 2016-04-01 08:52:17 -0700 | [diff] [blame] | 133 | return inet_lookup(net, &tcp_hashinfo, skb, doff, |
| 134 | saddr, sport, daddr, dport, |
| 135 | in->ifindex); |
Florian Westphal | 93742cf | 2013-07-29 15:41:53 +0200 | [diff] [blame] | 136 | case IPPROTO_UDP: |
| 137 | return udp4_lib_lookup(net, saddr, sport, daddr, dport, |
| 138 | in->ifindex); |
| 139 | } |
| 140 | return NULL; |
| 141 | } |
| 142 | |
Eric Dumazet | a940700 | 2015-03-16 21:06:17 -0700 | [diff] [blame] | 143 | static bool xt_socket_sk_is_transparent(struct sock *sk) |
| 144 | { |
| 145 | switch (sk->sk_state) { |
| 146 | case TCP_TIME_WAIT: |
| 147 | return inet_twsk(sk)->tw_transparent; |
| 148 | |
| 149 | case TCP_NEW_SYN_RECV: |
| 150 | return inet_rsk(inet_reqsk(sk))->no_srccheck; |
| 151 | |
| 152 | default: |
| 153 | return inet_sk(sk)->transparent; |
| 154 | } |
| 155 | } |
| 156 | |
John Stultz | 994ee75 | 2014-03-28 16:23:48 -0700 | [diff] [blame^] | 157 | struct sock *xt_socket_lookup_slow_v4(struct net *net, |
Eric W. Biederman | 686c9b5 | 2015-09-18 14:32:59 -0500 | [diff] [blame] | 158 | const struct sk_buff *skb, |
Daniel Borkmann | d64d80a | 2015-04-02 14:28:30 +0200 | [diff] [blame] | 159 | const struct net_device *indev) |
KOVACS Krisztian | 136cdc7 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 160 | { |
| 161 | const struct iphdr *iph = ip_hdr(skb); |
Craig Gallek | a583636 | 2016-02-10 11:50:38 -0500 | [diff] [blame] | 162 | struct sk_buff *data_skb = NULL; |
| 163 | int doff = 0; |
Pablo Neira Ayuso | 6703aa7 | 2012-08-29 15:58:29 +0000 | [diff] [blame] | 164 | __be32 uninitialized_var(daddr), uninitialized_var(saddr); |
| 165 | __be16 uninitialized_var(dport), uninitialized_var(sport); |
| 166 | u8 uninitialized_var(protocol); |
KOVACS Krisztian | 136cdc7 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 167 | #ifdef XT_SOCKET_HAVE_CONNTRACK |
| 168 | struct nf_conn const *ct; |
| 169 | enum ip_conntrack_info ctinfo; |
| 170 | #endif |
| 171 | |
| 172 | if (iph->protocol == IPPROTO_UDP || iph->protocol == IPPROTO_TCP) { |
Daniel Borkmann | d64d80a | 2015-04-02 14:28:30 +0200 | [diff] [blame] | 173 | struct udphdr _hdr, *hp; |
| 174 | |
KOVACS Krisztian | 136cdc7 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 175 | hp = skb_header_pointer(skb, ip_hdrlen(skb), |
| 176 | sizeof(_hdr), &_hdr); |
| 177 | if (hp == NULL) |
Daniel Borkmann | d64d80a | 2015-04-02 14:28:30 +0200 | [diff] [blame] | 178 | return NULL; |
KOVACS Krisztian | 136cdc7 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 179 | |
| 180 | protocol = iph->protocol; |
| 181 | saddr = iph->saddr; |
| 182 | sport = hp->source; |
| 183 | daddr = iph->daddr; |
| 184 | dport = hp->dest; |
Craig Gallek | a583636 | 2016-02-10 11:50:38 -0500 | [diff] [blame] | 185 | data_skb = (struct sk_buff *)skb; |
| 186 | doff = iph->protocol == IPPROTO_TCP ? |
| 187 | ip_hdrlen(skb) + __tcp_hdrlen((struct tcphdr *)hp) : |
| 188 | ip_hdrlen(skb) + sizeof(*hp); |
KOVACS Krisztian | 136cdc7 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 189 | |
| 190 | } else if (iph->protocol == IPPROTO_ICMP) { |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 191 | if (extract_icmp4_fields(skb, &protocol, &saddr, &daddr, |
Daniel Borkmann | d64d80a | 2015-04-02 14:28:30 +0200 | [diff] [blame] | 192 | &sport, &dport)) |
| 193 | return NULL; |
KOVACS Krisztian | 136cdc7 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 194 | } else { |
Daniel Borkmann | d64d80a | 2015-04-02 14:28:30 +0200 | [diff] [blame] | 195 | return NULL; |
KOVACS Krisztian | 136cdc7 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | #ifdef XT_SOCKET_HAVE_CONNTRACK |
Daniel Borkmann | d64d80a | 2015-04-02 14:28:30 +0200 | [diff] [blame] | 199 | /* Do the lookup with the original socket address in |
| 200 | * case this is a reply packet of an established |
| 201 | * SNAT-ted connection. |
| 202 | */ |
KOVACS Krisztian | 136cdc7 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 203 | ct = nf_ct_get(skb, &ctinfo); |
Eric Dumazet | 5bfddbd | 2010-06-08 16:09:52 +0200 | [diff] [blame] | 204 | if (ct && !nf_ct_is_untracked(ct) && |
KOVACS Krisztian | 136cdc7 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 205 | ((iph->protocol != IPPROTO_ICMP && |
Eric Dumazet | fb04883 | 2011-05-19 15:44:27 +0200 | [diff] [blame] | 206 | ctinfo == IP_CT_ESTABLISHED_REPLY) || |
KOVACS Krisztian | 136cdc7 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 207 | (iph->protocol == IPPROTO_ICMP && |
Eric Dumazet | fb04883 | 2011-05-19 15:44:27 +0200 | [diff] [blame] | 208 | ctinfo == IP_CT_RELATED_REPLY)) && |
KOVACS Krisztian | 136cdc7 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 209 | (ct->status & IPS_SRC_NAT_DONE)) { |
| 210 | |
| 211 | daddr = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip; |
| 212 | dport = (iph->protocol == IPPROTO_TCP) ? |
| 213 | ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.tcp.port : |
| 214 | ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.udp.port; |
| 215 | } |
| 216 | #endif |
| 217 | |
Craig Gallek | a583636 | 2016-02-10 11:50:38 -0500 | [diff] [blame] | 218 | return xt_socket_get_sock_v4(net, data_skb, doff, protocol, saddr, |
| 219 | daddr, sport, dport, indev); |
Daniel Borkmann | d64d80a | 2015-04-02 14:28:30 +0200 | [diff] [blame] | 220 | } |
John Stultz | 994ee75 | 2014-03-28 16:23:48 -0700 | [diff] [blame^] | 221 | EXPORT_SYMBOL(xt_socket_lookup_slow_v4); |
Daniel Borkmann | d64d80a | 2015-04-02 14:28:30 +0200 | [diff] [blame] | 222 | |
| 223 | static bool |
| 224 | socket_match(const struct sk_buff *skb, struct xt_action_param *par, |
| 225 | const struct xt_socket_mtinfo1 *info) |
| 226 | { |
Harout Hedeshian | 01555e7 | 2015-06-15 18:40:43 -0600 | [diff] [blame] | 227 | struct sk_buff *pskb = (struct sk_buff *)skb; |
Daniel Borkmann | d64d80a | 2015-04-02 14:28:30 +0200 | [diff] [blame] | 228 | struct sock *sk = skb->sk; |
| 229 | |
Eric Dumazet | 00028aa | 2013-05-22 11:01:06 +0000 | [diff] [blame] | 230 | if (!sk) |
Eric W. Biederman | 686c9b5 | 2015-09-18 14:32:59 -0500 | [diff] [blame] | 231 | sk = xt_socket_lookup_slow_v4(par->net, skb, par->in); |
Eric Dumazet | 00028aa | 2013-05-22 11:01:06 +0000 | [diff] [blame] | 232 | if (sk) { |
Laszlo Attila Toth | a31e1ff | 2009-06-09 15:16:34 +0200 | [diff] [blame] | 233 | bool wildcard; |
| 234 | bool transparent = true; |
| 235 | |
Eric Dumazet | 681f130 | 2013-06-20 05:52:22 -0700 | [diff] [blame] | 236 | /* Ignore sockets listening on INADDR_ANY, |
| 237 | * unless XT_SOCKET_NOWILDCARD is set |
| 238 | */ |
| 239 | wildcard = (!(info->flags & XT_SOCKET_NOWILDCARD) && |
Eric Dumazet | a940700 | 2015-03-16 21:06:17 -0700 | [diff] [blame] | 240 | sk_fullsock(sk) && |
Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 241 | inet_sk(sk)->inet_rcv_saddr == 0); |
Laszlo Attila Toth | a31e1ff | 2009-06-09 15:16:34 +0200 | [diff] [blame] | 242 | |
| 243 | /* Ignore non-transparent sockets, |
Eric Dumazet | a940700 | 2015-03-16 21:06:17 -0700 | [diff] [blame] | 244 | * if XT_SOCKET_TRANSPARENT is used |
| 245 | */ |
Eric Dumazet | baf60ef | 2013-07-11 19:22:19 -0700 | [diff] [blame] | 246 | if (info->flags & XT_SOCKET_TRANSPARENT) |
Eric Dumazet | a940700 | 2015-03-16 21:06:17 -0700 | [diff] [blame] | 247 | transparent = xt_socket_sk_is_transparent(sk); |
KOVACS Krisztian | 136cdc7 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 248 | |
Harout Hedeshian | 01555e7 | 2015-06-15 18:40:43 -0600 | [diff] [blame] | 249 | if (info->flags & XT_SOCKET_RESTORESKMARK && !wildcard && |
| 250 | transparent) |
| 251 | pskb->mark = sk->sk_mark; |
| 252 | |
Eric Dumazet | 00028aa | 2013-05-22 11:01:06 +0000 | [diff] [blame] | 253 | if (sk != skb->sk) |
Eric Dumazet | 1a8bf6e | 2013-10-11 09:03:25 -0700 | [diff] [blame] | 254 | sock_gen_put(sk); |
Laszlo Attila Toth | a31e1ff | 2009-06-09 15:16:34 +0200 | [diff] [blame] | 255 | |
| 256 | if (wildcard || !transparent) |
KOVACS Krisztian | 136cdc7 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 257 | sk = NULL; |
| 258 | } |
| 259 | |
Daniel Borkmann | d64d80a | 2015-04-02 14:28:30 +0200 | [diff] [blame] | 260 | return sk != NULL; |
KOVACS Krisztian | 136cdc7 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 261 | } |
| 262 | |
Laszlo Attila Toth | a31e1ff | 2009-06-09 15:16:34 +0200 | [diff] [blame] | 263 | static bool |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 264 | socket_mt4_v0(const struct sk_buff *skb, struct xt_action_param *par) |
Laszlo Attila Toth | a31e1ff | 2009-06-09 15:16:34 +0200 | [diff] [blame] | 265 | { |
Eric Dumazet | baf60ef | 2013-07-11 19:22:19 -0700 | [diff] [blame] | 266 | static struct xt_socket_mtinfo1 xt_info_v0 = { |
| 267 | .flags = 0, |
| 268 | }; |
| 269 | |
| 270 | return socket_match(skb, par, &xt_info_v0); |
Laszlo Attila Toth | a31e1ff | 2009-06-09 15:16:34 +0200 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | static bool |
Harout Hedeshian | 01555e7 | 2015-06-15 18:40:43 -0600 | [diff] [blame] | 274 | socket_mt4_v1_v2_v3(const struct sk_buff *skb, struct xt_action_param *par) |
Laszlo Attila Toth | a31e1ff | 2009-06-09 15:16:34 +0200 | [diff] [blame] | 275 | { |
| 276 | return socket_match(skb, par, par->matchinfo); |
| 277 | } |
| 278 | |
KOVACS Krisztian | f6318e5 | 2010-10-24 23:38:32 +0000 | [diff] [blame] | 279 | #ifdef XT_SOCKET_HAVE_IPV6 |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 280 | |
| 281 | static int |
| 282 | extract_icmp6_fields(const struct sk_buff *skb, |
| 283 | unsigned int outside_hdrlen, |
David S. Miller | 089282f | 2010-10-28 12:59:53 -0700 | [diff] [blame] | 284 | int *protocol, |
Eric Dumazet | 78296c9 | 2015-02-15 19:03:45 -0800 | [diff] [blame] | 285 | const struct in6_addr **raddr, |
| 286 | const struct in6_addr **laddr, |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 287 | __be16 *rport, |
Eric Dumazet | 78296c9 | 2015-02-15 19:03:45 -0800 | [diff] [blame] | 288 | __be16 *lport, |
| 289 | struct ipv6hdr *ipv6_var) |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 290 | { |
Eric Dumazet | 78296c9 | 2015-02-15 19:03:45 -0800 | [diff] [blame] | 291 | const struct ipv6hdr *inside_iph; |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 292 | struct icmp6hdr *icmph, _icmph; |
| 293 | __be16 *ports, _ports[2]; |
| 294 | u8 inside_nexthdr; |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 295 | __be16 inside_fragoff; |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 296 | int inside_hdrlen; |
| 297 | |
| 298 | icmph = skb_header_pointer(skb, outside_hdrlen, |
| 299 | sizeof(_icmph), &_icmph); |
| 300 | if (icmph == NULL) |
| 301 | return 1; |
| 302 | |
| 303 | if (icmph->icmp6_type & ICMPV6_INFOMSG_MASK) |
| 304 | return 1; |
| 305 | |
Eric Dumazet | 78296c9 | 2015-02-15 19:03:45 -0800 | [diff] [blame] | 306 | inside_iph = skb_header_pointer(skb, outside_hdrlen + sizeof(_icmph), |
| 307 | sizeof(*ipv6_var), ipv6_var); |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 308 | if (inside_iph == NULL) |
| 309 | return 1; |
| 310 | inside_nexthdr = inside_iph->nexthdr; |
| 311 | |
Eric Dumazet | 78296c9 | 2015-02-15 19:03:45 -0800 | [diff] [blame] | 312 | inside_hdrlen = ipv6_skip_exthdr(skb, outside_hdrlen + sizeof(_icmph) + |
| 313 | sizeof(*ipv6_var), |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 314 | &inside_nexthdr, &inside_fragoff); |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 315 | if (inside_hdrlen < 0) |
| 316 | return 1; /* hjm: Packet has no/incomplete transport layer headers. */ |
| 317 | |
| 318 | if (inside_nexthdr != IPPROTO_TCP && |
| 319 | inside_nexthdr != IPPROTO_UDP) |
| 320 | return 1; |
| 321 | |
| 322 | ports = skb_header_pointer(skb, inside_hdrlen, |
| 323 | sizeof(_ports), &_ports); |
| 324 | if (ports == NULL) |
| 325 | return 1; |
| 326 | |
| 327 | /* the inside IP packet is the one quoted from our side, thus |
| 328 | * its saddr is the local address */ |
| 329 | *protocol = inside_nexthdr; |
| 330 | *laddr = &inside_iph->saddr; |
| 331 | *lport = ports[0]; |
| 332 | *raddr = &inside_iph->daddr; |
| 333 | *rport = ports[1]; |
| 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
Florian Westphal | 93742cf | 2013-07-29 15:41:53 +0200 | [diff] [blame] | 338 | static struct sock * |
Craig Gallek | a583636 | 2016-02-10 11:50:38 -0500 | [diff] [blame] | 339 | xt_socket_get_sock_v6(struct net *net, struct sk_buff *skb, int doff, |
| 340 | const u8 protocol, |
Florian Westphal | 93742cf | 2013-07-29 15:41:53 +0200 | [diff] [blame] | 341 | const struct in6_addr *saddr, const struct in6_addr *daddr, |
| 342 | const __be16 sport, const __be16 dport, |
| 343 | const struct net_device *in) |
| 344 | { |
| 345 | switch (protocol) { |
| 346 | case IPPROTO_TCP: |
Craig Gallek | a583636 | 2016-02-10 11:50:38 -0500 | [diff] [blame] | 347 | return inet6_lookup(net, &tcp_hashinfo, skb, doff, |
Florian Westphal | 93742cf | 2013-07-29 15:41:53 +0200 | [diff] [blame] | 348 | saddr, sport, daddr, dport, |
| 349 | in->ifindex); |
| 350 | case IPPROTO_UDP: |
| 351 | return udp6_lib_lookup(net, saddr, sport, daddr, dport, |
| 352 | in->ifindex); |
| 353 | } |
| 354 | |
| 355 | return NULL; |
| 356 | } |
| 357 | |
John Stultz | 994ee75 | 2014-03-28 16:23:48 -0700 | [diff] [blame^] | 358 | struct sock *xt_socket_lookup_slow_v6(struct net *net, |
Eric W. Biederman | 686c9b5 | 2015-09-18 14:32:59 -0500 | [diff] [blame] | 359 | const struct sk_buff *skb, |
Daniel Borkmann | d64d80a | 2015-04-02 14:28:30 +0200 | [diff] [blame] | 360 | const struct net_device *indev) |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 361 | { |
Pablo Neira Ayuso | 6703aa7 | 2012-08-29 15:58:29 +0000 | [diff] [blame] | 362 | __be16 uninitialized_var(dport), uninitialized_var(sport); |
Daniel Borkmann | d64d80a | 2015-04-02 14:28:30 +0200 | [diff] [blame] | 363 | const struct in6_addr *daddr = NULL, *saddr = NULL; |
| 364 | struct ipv6hdr *iph = ipv6_hdr(skb); |
Craig Gallek | a583636 | 2016-02-10 11:50:38 -0500 | [diff] [blame] | 365 | struct sk_buff *data_skb = NULL; |
| 366 | int doff = 0; |
Daniel Borkmann | d64d80a | 2015-04-02 14:28:30 +0200 | [diff] [blame] | 367 | int thoff = 0, tproto; |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 368 | |
Hans Schillstrom | 84018f5 | 2012-04-23 03:35:26 +0000 | [diff] [blame] | 369 | tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL); |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 370 | if (tproto < 0) { |
| 371 | pr_debug("unable to find transport header in IPv6 packet, dropping\n"); |
Daniel Borkmann | d64d80a | 2015-04-02 14:28:30 +0200 | [diff] [blame] | 372 | return NULL; |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | if (tproto == IPPROTO_UDP || tproto == IPPROTO_TCP) { |
Daniel Borkmann | d64d80a | 2015-04-02 14:28:30 +0200 | [diff] [blame] | 376 | struct udphdr _hdr, *hp; |
| 377 | |
| 378 | hp = skb_header_pointer(skb, thoff, sizeof(_hdr), &_hdr); |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 379 | if (hp == NULL) |
Daniel Borkmann | d64d80a | 2015-04-02 14:28:30 +0200 | [diff] [blame] | 380 | return NULL; |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 381 | |
| 382 | saddr = &iph->saddr; |
| 383 | sport = hp->source; |
| 384 | daddr = &iph->daddr; |
| 385 | dport = hp->dest; |
Craig Gallek | a583636 | 2016-02-10 11:50:38 -0500 | [diff] [blame] | 386 | data_skb = (struct sk_buff *)skb; |
| 387 | doff = tproto == IPPROTO_TCP ? |
| 388 | thoff + __tcp_hdrlen((struct tcphdr *)hp) : |
| 389 | thoff + sizeof(*hp); |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 390 | |
| 391 | } else if (tproto == IPPROTO_ICMPV6) { |
Daniel Borkmann | d64d80a | 2015-04-02 14:28:30 +0200 | [diff] [blame] | 392 | struct ipv6hdr ipv6_var; |
| 393 | |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 394 | if (extract_icmp6_fields(skb, thoff, &tproto, &saddr, &daddr, |
Eric Dumazet | 78296c9 | 2015-02-15 19:03:45 -0800 | [diff] [blame] | 395 | &sport, &dport, &ipv6_var)) |
Daniel Borkmann | d64d80a | 2015-04-02 14:28:30 +0200 | [diff] [blame] | 396 | return NULL; |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 397 | } else { |
Daniel Borkmann | d64d80a | 2015-04-02 14:28:30 +0200 | [diff] [blame] | 398 | return NULL; |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 399 | } |
| 400 | |
Craig Gallek | a583636 | 2016-02-10 11:50:38 -0500 | [diff] [blame] | 401 | return xt_socket_get_sock_v6(net, data_skb, doff, tproto, saddr, daddr, |
Daniel Borkmann | d64d80a | 2015-04-02 14:28:30 +0200 | [diff] [blame] | 402 | sport, dport, indev); |
| 403 | } |
John Stultz | 994ee75 | 2014-03-28 16:23:48 -0700 | [diff] [blame^] | 404 | EXPORT_SYMBOL(xt_socket_lookup_slow_v6); |
Daniel Borkmann | d64d80a | 2015-04-02 14:28:30 +0200 | [diff] [blame] | 405 | |
| 406 | static bool |
Harout Hedeshian | 01555e7 | 2015-06-15 18:40:43 -0600 | [diff] [blame] | 407 | socket_mt6_v1_v2_v3(const struct sk_buff *skb, struct xt_action_param *par) |
Daniel Borkmann | d64d80a | 2015-04-02 14:28:30 +0200 | [diff] [blame] | 408 | { |
| 409 | const struct xt_socket_mtinfo1 *info = (struct xt_socket_mtinfo1 *) par->matchinfo; |
Harout Hedeshian | 01555e7 | 2015-06-15 18:40:43 -0600 | [diff] [blame] | 410 | struct sk_buff *pskb = (struct sk_buff *)skb; |
Daniel Borkmann | d64d80a | 2015-04-02 14:28:30 +0200 | [diff] [blame] | 411 | struct sock *sk = skb->sk; |
| 412 | |
Eric Dumazet | 00028aa | 2013-05-22 11:01:06 +0000 | [diff] [blame] | 413 | if (!sk) |
Eric W. Biederman | 686c9b5 | 2015-09-18 14:32:59 -0500 | [diff] [blame] | 414 | sk = xt_socket_lookup_slow_v6(par->net, skb, par->in); |
Eric Dumazet | 00028aa | 2013-05-22 11:01:06 +0000 | [diff] [blame] | 415 | if (sk) { |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 416 | bool wildcard; |
| 417 | bool transparent = true; |
| 418 | |
Eric Dumazet | 681f130 | 2013-06-20 05:52:22 -0700 | [diff] [blame] | 419 | /* Ignore sockets listening on INADDR_ANY |
| 420 | * unless XT_SOCKET_NOWILDCARD is set |
| 421 | */ |
| 422 | wildcard = (!(info->flags & XT_SOCKET_NOWILDCARD) && |
Eric Dumazet | a940700 | 2015-03-16 21:06:17 -0700 | [diff] [blame] | 423 | sk_fullsock(sk) && |
Eric Dumazet | efe4208 | 2013-10-03 15:42:29 -0700 | [diff] [blame] | 424 | ipv6_addr_any(&sk->sk_v6_rcv_saddr)); |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 425 | |
| 426 | /* Ignore non-transparent sockets, |
Eric Dumazet | a940700 | 2015-03-16 21:06:17 -0700 | [diff] [blame] | 427 | * if XT_SOCKET_TRANSPARENT is used |
| 428 | */ |
Eric Dumazet | baf60ef | 2013-07-11 19:22:19 -0700 | [diff] [blame] | 429 | if (info->flags & XT_SOCKET_TRANSPARENT) |
Eric Dumazet | a940700 | 2015-03-16 21:06:17 -0700 | [diff] [blame] | 430 | transparent = xt_socket_sk_is_transparent(sk); |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 431 | |
Harout Hedeshian | 01555e7 | 2015-06-15 18:40:43 -0600 | [diff] [blame] | 432 | if (info->flags & XT_SOCKET_RESTORESKMARK && !wildcard && |
| 433 | transparent) |
| 434 | pskb->mark = sk->sk_mark; |
| 435 | |
Eric Dumazet | 00028aa | 2013-05-22 11:01:06 +0000 | [diff] [blame] | 436 | if (sk != skb->sk) |
Eric Dumazet | 1a8bf6e | 2013-10-11 09:03:25 -0700 | [diff] [blame] | 437 | sock_gen_put(sk); |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 438 | |
| 439 | if (wildcard || !transparent) |
| 440 | sk = NULL; |
| 441 | } |
| 442 | |
Daniel Borkmann | d64d80a | 2015-04-02 14:28:30 +0200 | [diff] [blame] | 443 | return sk != NULL; |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 444 | } |
| 445 | #endif |
| 446 | |
Eric Dumazet | 681f130 | 2013-06-20 05:52:22 -0700 | [diff] [blame] | 447 | static int socket_mt_v1_check(const struct xt_mtchk_param *par) |
| 448 | { |
| 449 | const struct xt_socket_mtinfo1 *info = (struct xt_socket_mtinfo1 *) par->matchinfo; |
| 450 | |
| 451 | if (info->flags & ~XT_SOCKET_FLAGS_V1) { |
| 452 | pr_info("unknown flags 0x%x\n", info->flags & ~XT_SOCKET_FLAGS_V1); |
| 453 | return -EINVAL; |
| 454 | } |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | static int socket_mt_v2_check(const struct xt_mtchk_param *par) |
| 459 | { |
| 460 | const struct xt_socket_mtinfo2 *info = (struct xt_socket_mtinfo2 *) par->matchinfo; |
| 461 | |
| 462 | if (info->flags & ~XT_SOCKET_FLAGS_V2) { |
| 463 | pr_info("unknown flags 0x%x\n", info->flags & ~XT_SOCKET_FLAGS_V2); |
| 464 | return -EINVAL; |
| 465 | } |
| 466 | return 0; |
| 467 | } |
| 468 | |
Harout Hedeshian | 01555e7 | 2015-06-15 18:40:43 -0600 | [diff] [blame] | 469 | static int socket_mt_v3_check(const struct xt_mtchk_param *par) |
| 470 | { |
| 471 | const struct xt_socket_mtinfo3 *info = |
| 472 | (struct xt_socket_mtinfo3 *)par->matchinfo; |
| 473 | |
| 474 | if (info->flags & ~XT_SOCKET_FLAGS_V3) { |
| 475 | pr_info("unknown flags 0x%x\n", |
| 476 | info->flags & ~XT_SOCKET_FLAGS_V3); |
| 477 | return -EINVAL; |
| 478 | } |
| 479 | return 0; |
| 480 | } |
| 481 | |
Laszlo Attila Toth | a31e1ff | 2009-06-09 15:16:34 +0200 | [diff] [blame] | 482 | static struct xt_match socket_mt_reg[] __read_mostly = { |
| 483 | { |
| 484 | .name = "socket", |
| 485 | .revision = 0, |
| 486 | .family = NFPROTO_IPV4, |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 487 | .match = socket_mt4_v0, |
Jan Engelhardt | aa3c487 | 2009-10-29 15:35:10 +0100 | [diff] [blame] | 488 | .hooks = (1 << NF_INET_PRE_ROUTING) | |
| 489 | (1 << NF_INET_LOCAL_IN), |
Laszlo Attila Toth | a31e1ff | 2009-06-09 15:16:34 +0200 | [diff] [blame] | 490 | .me = THIS_MODULE, |
| 491 | }, |
| 492 | { |
| 493 | .name = "socket", |
| 494 | .revision = 1, |
| 495 | .family = NFPROTO_IPV4, |
Harout Hedeshian | 01555e7 | 2015-06-15 18:40:43 -0600 | [diff] [blame] | 496 | .match = socket_mt4_v1_v2_v3, |
Eric Dumazet | 681f130 | 2013-06-20 05:52:22 -0700 | [diff] [blame] | 497 | .checkentry = socket_mt_v1_check, |
Laszlo Attila Toth | a31e1ff | 2009-06-09 15:16:34 +0200 | [diff] [blame] | 498 | .matchsize = sizeof(struct xt_socket_mtinfo1), |
Jan Engelhardt | aa3c487 | 2009-10-29 15:35:10 +0100 | [diff] [blame] | 499 | .hooks = (1 << NF_INET_PRE_ROUTING) | |
| 500 | (1 << NF_INET_LOCAL_IN), |
Laszlo Attila Toth | a31e1ff | 2009-06-09 15:16:34 +0200 | [diff] [blame] | 501 | .me = THIS_MODULE, |
| 502 | }, |
KOVACS Krisztian | f6318e5 | 2010-10-24 23:38:32 +0000 | [diff] [blame] | 503 | #ifdef XT_SOCKET_HAVE_IPV6 |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 504 | { |
| 505 | .name = "socket", |
| 506 | .revision = 1, |
| 507 | .family = NFPROTO_IPV6, |
Harout Hedeshian | 01555e7 | 2015-06-15 18:40:43 -0600 | [diff] [blame] | 508 | .match = socket_mt6_v1_v2_v3, |
Eric Dumazet | 681f130 | 2013-06-20 05:52:22 -0700 | [diff] [blame] | 509 | .checkentry = socket_mt_v1_check, |
| 510 | .matchsize = sizeof(struct xt_socket_mtinfo1), |
| 511 | .hooks = (1 << NF_INET_PRE_ROUTING) | |
| 512 | (1 << NF_INET_LOCAL_IN), |
| 513 | .me = THIS_MODULE, |
| 514 | }, |
| 515 | #endif |
| 516 | { |
| 517 | .name = "socket", |
| 518 | .revision = 2, |
| 519 | .family = NFPROTO_IPV4, |
Harout Hedeshian | 01555e7 | 2015-06-15 18:40:43 -0600 | [diff] [blame] | 520 | .match = socket_mt4_v1_v2_v3, |
Eric Dumazet | 681f130 | 2013-06-20 05:52:22 -0700 | [diff] [blame] | 521 | .checkentry = socket_mt_v2_check, |
| 522 | .matchsize = sizeof(struct xt_socket_mtinfo1), |
| 523 | .hooks = (1 << NF_INET_PRE_ROUTING) | |
| 524 | (1 << NF_INET_LOCAL_IN), |
| 525 | .me = THIS_MODULE, |
| 526 | }, |
| 527 | #ifdef XT_SOCKET_HAVE_IPV6 |
| 528 | { |
| 529 | .name = "socket", |
| 530 | .revision = 2, |
| 531 | .family = NFPROTO_IPV6, |
Harout Hedeshian | 01555e7 | 2015-06-15 18:40:43 -0600 | [diff] [blame] | 532 | .match = socket_mt6_v1_v2_v3, |
Eric Dumazet | 681f130 | 2013-06-20 05:52:22 -0700 | [diff] [blame] | 533 | .checkentry = socket_mt_v2_check, |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 534 | .matchsize = sizeof(struct xt_socket_mtinfo1), |
| 535 | .hooks = (1 << NF_INET_PRE_ROUTING) | |
| 536 | (1 << NF_INET_LOCAL_IN), |
| 537 | .me = THIS_MODULE, |
| 538 | }, |
| 539 | #endif |
Harout Hedeshian | 01555e7 | 2015-06-15 18:40:43 -0600 | [diff] [blame] | 540 | { |
| 541 | .name = "socket", |
| 542 | .revision = 3, |
| 543 | .family = NFPROTO_IPV4, |
| 544 | .match = socket_mt4_v1_v2_v3, |
| 545 | .checkentry = socket_mt_v3_check, |
| 546 | .matchsize = sizeof(struct xt_socket_mtinfo1), |
| 547 | .hooks = (1 << NF_INET_PRE_ROUTING) | |
| 548 | (1 << NF_INET_LOCAL_IN), |
| 549 | .me = THIS_MODULE, |
| 550 | }, |
| 551 | #ifdef XT_SOCKET_HAVE_IPV6 |
| 552 | { |
| 553 | .name = "socket", |
| 554 | .revision = 3, |
| 555 | .family = NFPROTO_IPV6, |
| 556 | .match = socket_mt6_v1_v2_v3, |
| 557 | .checkentry = socket_mt_v3_check, |
| 558 | .matchsize = sizeof(struct xt_socket_mtinfo1), |
| 559 | .hooks = (1 << NF_INET_PRE_ROUTING) | |
| 560 | (1 << NF_INET_LOCAL_IN), |
| 561 | .me = THIS_MODULE, |
| 562 | }, |
| 563 | #endif |
KOVACS Krisztian | 136cdc7 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 564 | }; |
| 565 | |
| 566 | static int __init socket_mt_init(void) |
| 567 | { |
| 568 | nf_defrag_ipv4_enable(); |
KOVACS Krisztian | f6318e5 | 2010-10-24 23:38:32 +0000 | [diff] [blame] | 569 | #ifdef XT_SOCKET_HAVE_IPV6 |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 570 | nf_defrag_ipv6_enable(); |
| 571 | #endif |
| 572 | |
Laszlo Attila Toth | a31e1ff | 2009-06-09 15:16:34 +0200 | [diff] [blame] | 573 | return xt_register_matches(socket_mt_reg, ARRAY_SIZE(socket_mt_reg)); |
KOVACS Krisztian | 136cdc7 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | static void __exit socket_mt_exit(void) |
| 577 | { |
Laszlo Attila Toth | a31e1ff | 2009-06-09 15:16:34 +0200 | [diff] [blame] | 578 | xt_unregister_matches(socket_mt_reg, ARRAY_SIZE(socket_mt_reg)); |
KOVACS Krisztian | 136cdc7 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | module_init(socket_mt_init); |
| 582 | module_exit(socket_mt_exit); |
| 583 | |
| 584 | MODULE_LICENSE("GPL"); |
| 585 | MODULE_AUTHOR("Krisztian Kovacs, Balazs Scheidler"); |
| 586 | MODULE_DESCRIPTION("x_tables socket match module"); |
| 587 | MODULE_ALIAS("ipt_socket"); |
Balazs Scheidler | b64c925 | 2010-10-21 16:19:42 +0200 | [diff] [blame] | 588 | MODULE_ALIAS("ip6t_socket"); |