blob: 45346438b250ec359d9236fdf147be88e50ff74d [file] [log] [blame]
KOVACS Krisztian136cdc72008-10-08 11:35:12 +02001/*
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 Engelhardtff67e4e2010-03-19 21:08:16 +010012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
KOVACS Krisztian136cdc72008-10-08 11:35:12 +020013#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 Krisztian136cdc72008-10-08 11:35:12 +020022#include <net/netfilter/ipv4/nf_defrag_ipv4.h>
KOVACS Krisztianf6318e52010-10-24 23:38:32 +000023
Igor Maravićc0cd1152011-12-12 02:58:24 +000024#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
KOVACS Krisztianf6318e52010-10-24 23:38:32 +000025#define XT_SOCKET_HAVE_IPV6 1
26#include <linux/netfilter_ipv6/ip6_tables.h>
Florian Westphal93742cf2013-07-29 15:41:53 +020027#include <net/inet6_hashtables.h>
Balazs Scheidlerb64c9252010-10-21 16:19:42 +020028#include <net/netfilter/ipv6/nf_defrag_ipv6.h>
KOVACS Krisztianf6318e52010-10-24 23:38:32 +000029#endif
KOVACS Krisztian136cdc72008-10-08 11:35:12 +020030
Laszlo Attila Totha31e1ff2009-06-09 15:16:34 +020031#include <linux/netfilter/xt_socket.h>
32
Igor Maravićc0cd1152011-12-12 02:58:24 +000033#if IS_ENABLED(CONFIG_NF_CONNTRACK)
KOVACS Krisztian136cdc72008-10-08 11:35:12 +020034#define XT_SOCKET_HAVE_CONNTRACK 1
35#include <net/netfilter/nf_conntrack.h>
36#endif
37
John Stultz994ee752014-03-28 16:23:48 -070038void
39xt_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}
46EXPORT_SYMBOL(xt_socket_put_sk);
47
KOVACS Krisztian136cdc72008-10-08 11:35:12 +020048static int
Balazs Scheidlerb64c9252010-10-21 16:19:42 +020049extract_icmp4_fields(const struct sk_buff *skb,
KOVACS Krisztian136cdc72008-10-08 11:35:12 +020050 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 Westphal93742cf2013-07-29 15:41:53 +0200105/* "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 */
124static struct sock *
Craig Galleka5836362016-02-10 11:50:38 -0500125xt_socket_get_sock_v4(struct net *net, struct sk_buff *skb, const int doff,
126 const u8 protocol,
Florian Westphal93742cf2013-07-29 15:41:53 +0200127 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 Dumazet3b24d852016-04-01 08:52:17 -0700133 return inet_lookup(net, &tcp_hashinfo, skb, doff,
134 saddr, sport, daddr, dport,
135 in->ifindex);
Florian Westphal93742cf2013-07-29 15:41:53 +0200136 case IPPROTO_UDP:
137 return udp4_lib_lookup(net, saddr, sport, daddr, dport,
138 in->ifindex);
139 }
140 return NULL;
141}
142
Eric Dumazeta9407002015-03-16 21:06:17 -0700143static 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 Stultz994ee752014-03-28 16:23:48 -0700157struct sock *xt_socket_lookup_slow_v4(struct net *net,
Eric W. Biederman686c9b52015-09-18 14:32:59 -0500158 const struct sk_buff *skb,
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200159 const struct net_device *indev)
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200160{
161 const struct iphdr *iph = ip_hdr(skb);
Craig Galleka5836362016-02-10 11:50:38 -0500162 struct sk_buff *data_skb = NULL;
163 int doff = 0;
Pablo Neira Ayuso6703aa72012-08-29 15:58:29 +0000164 __be32 uninitialized_var(daddr), uninitialized_var(saddr);
165 __be16 uninitialized_var(dport), uninitialized_var(sport);
166 u8 uninitialized_var(protocol);
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200167#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 Borkmannd64d80a2015-04-02 14:28:30 +0200173 struct udphdr _hdr, *hp;
174
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200175 hp = skb_header_pointer(skb, ip_hdrlen(skb),
176 sizeof(_hdr), &_hdr);
177 if (hp == NULL)
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200178 return NULL;
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200179
180 protocol = iph->protocol;
181 saddr = iph->saddr;
182 sport = hp->source;
183 daddr = iph->daddr;
184 dport = hp->dest;
Craig Galleka5836362016-02-10 11:50:38 -0500185 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 Krisztian136cdc72008-10-08 11:35:12 +0200189
190 } else if (iph->protocol == IPPROTO_ICMP) {
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200191 if (extract_icmp4_fields(skb, &protocol, &saddr, &daddr,
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200192 &sport, &dport))
193 return NULL;
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200194 } else {
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200195 return NULL;
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200196 }
197
198#ifdef XT_SOCKET_HAVE_CONNTRACK
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200199 /* 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 Krisztian136cdc72008-10-08 11:35:12 +0200203 ct = nf_ct_get(skb, &ctinfo);
Eric Dumazet5bfddbd2010-06-08 16:09:52 +0200204 if (ct && !nf_ct_is_untracked(ct) &&
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200205 ((iph->protocol != IPPROTO_ICMP &&
Eric Dumazetfb048832011-05-19 15:44:27 +0200206 ctinfo == IP_CT_ESTABLISHED_REPLY) ||
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200207 (iph->protocol == IPPROTO_ICMP &&
Eric Dumazetfb048832011-05-19 15:44:27 +0200208 ctinfo == IP_CT_RELATED_REPLY)) &&
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200209 (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 Galleka5836362016-02-10 11:50:38 -0500218 return xt_socket_get_sock_v4(net, data_skb, doff, protocol, saddr,
219 daddr, sport, dport, indev);
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200220}
John Stultz994ee752014-03-28 16:23:48 -0700221EXPORT_SYMBOL(xt_socket_lookup_slow_v4);
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200222
223static bool
224socket_match(const struct sk_buff *skb, struct xt_action_param *par,
225 const struct xt_socket_mtinfo1 *info)
226{
Harout Hedeshian01555e72015-06-15 18:40:43 -0600227 struct sk_buff *pskb = (struct sk_buff *)skb;
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200228 struct sock *sk = skb->sk;
229
Eric Dumazet00028aa2013-05-22 11:01:06 +0000230 if (!sk)
Eric W. Biederman686c9b52015-09-18 14:32:59 -0500231 sk = xt_socket_lookup_slow_v4(par->net, skb, par->in);
Eric Dumazet00028aa2013-05-22 11:01:06 +0000232 if (sk) {
Laszlo Attila Totha31e1ff2009-06-09 15:16:34 +0200233 bool wildcard;
234 bool transparent = true;
235
Eric Dumazet681f1302013-06-20 05:52:22 -0700236 /* Ignore sockets listening on INADDR_ANY,
237 * unless XT_SOCKET_NOWILDCARD is set
238 */
239 wildcard = (!(info->flags & XT_SOCKET_NOWILDCARD) &&
Eric Dumazeta9407002015-03-16 21:06:17 -0700240 sk_fullsock(sk) &&
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000241 inet_sk(sk)->inet_rcv_saddr == 0);
Laszlo Attila Totha31e1ff2009-06-09 15:16:34 +0200242
243 /* Ignore non-transparent sockets,
Eric Dumazeta9407002015-03-16 21:06:17 -0700244 * if XT_SOCKET_TRANSPARENT is used
245 */
Eric Dumazetbaf60ef2013-07-11 19:22:19 -0700246 if (info->flags & XT_SOCKET_TRANSPARENT)
Eric Dumazeta9407002015-03-16 21:06:17 -0700247 transparent = xt_socket_sk_is_transparent(sk);
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200248
Harout Hedeshian01555e72015-06-15 18:40:43 -0600249 if (info->flags & XT_SOCKET_RESTORESKMARK && !wildcard &&
250 transparent)
251 pskb->mark = sk->sk_mark;
252
Eric Dumazet00028aa2013-05-22 11:01:06 +0000253 if (sk != skb->sk)
Eric Dumazet1a8bf6e2013-10-11 09:03:25 -0700254 sock_gen_put(sk);
Laszlo Attila Totha31e1ff2009-06-09 15:16:34 +0200255
256 if (wildcard || !transparent)
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200257 sk = NULL;
258 }
259
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200260 return sk != NULL;
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200261}
262
Laszlo Attila Totha31e1ff2009-06-09 15:16:34 +0200263static bool
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200264socket_mt4_v0(const struct sk_buff *skb, struct xt_action_param *par)
Laszlo Attila Totha31e1ff2009-06-09 15:16:34 +0200265{
Eric Dumazetbaf60ef2013-07-11 19:22:19 -0700266 static struct xt_socket_mtinfo1 xt_info_v0 = {
267 .flags = 0,
268 };
269
270 return socket_match(skb, par, &xt_info_v0);
Laszlo Attila Totha31e1ff2009-06-09 15:16:34 +0200271}
272
273static bool
Harout Hedeshian01555e72015-06-15 18:40:43 -0600274socket_mt4_v1_v2_v3(const struct sk_buff *skb, struct xt_action_param *par)
Laszlo Attila Totha31e1ff2009-06-09 15:16:34 +0200275{
276 return socket_match(skb, par, par->matchinfo);
277}
278
KOVACS Krisztianf6318e52010-10-24 23:38:32 +0000279#ifdef XT_SOCKET_HAVE_IPV6
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200280
281static int
282extract_icmp6_fields(const struct sk_buff *skb,
283 unsigned int outside_hdrlen,
David S. Miller089282f2010-10-28 12:59:53 -0700284 int *protocol,
Eric Dumazet78296c92015-02-15 19:03:45 -0800285 const struct in6_addr **raddr,
286 const struct in6_addr **laddr,
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200287 __be16 *rport,
Eric Dumazet78296c92015-02-15 19:03:45 -0800288 __be16 *lport,
289 struct ipv6hdr *ipv6_var)
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200290{
Eric Dumazet78296c92015-02-15 19:03:45 -0800291 const struct ipv6hdr *inside_iph;
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200292 struct icmp6hdr *icmph, _icmph;
293 __be16 *ports, _ports[2];
294 u8 inside_nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -0800295 __be16 inside_fragoff;
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200296 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 Dumazet78296c92015-02-15 19:03:45 -0800306 inside_iph = skb_header_pointer(skb, outside_hdrlen + sizeof(_icmph),
307 sizeof(*ipv6_var), ipv6_var);
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200308 if (inside_iph == NULL)
309 return 1;
310 inside_nexthdr = inside_iph->nexthdr;
311
Eric Dumazet78296c92015-02-15 19:03:45 -0800312 inside_hdrlen = ipv6_skip_exthdr(skb, outside_hdrlen + sizeof(_icmph) +
313 sizeof(*ipv6_var),
Jesse Gross75f28112011-11-30 17:05:51 -0800314 &inside_nexthdr, &inside_fragoff);
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200315 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 Westphal93742cf2013-07-29 15:41:53 +0200338static struct sock *
Craig Galleka5836362016-02-10 11:50:38 -0500339xt_socket_get_sock_v6(struct net *net, struct sk_buff *skb, int doff,
340 const u8 protocol,
Florian Westphal93742cf2013-07-29 15:41:53 +0200341 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 Galleka5836362016-02-10 11:50:38 -0500347 return inet6_lookup(net, &tcp_hashinfo, skb, doff,
Florian Westphal93742cf2013-07-29 15:41:53 +0200348 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 Stultz994ee752014-03-28 16:23:48 -0700358struct sock *xt_socket_lookup_slow_v6(struct net *net,
Eric W. Biederman686c9b52015-09-18 14:32:59 -0500359 const struct sk_buff *skb,
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200360 const struct net_device *indev)
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200361{
Pablo Neira Ayuso6703aa72012-08-29 15:58:29 +0000362 __be16 uninitialized_var(dport), uninitialized_var(sport);
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200363 const struct in6_addr *daddr = NULL, *saddr = NULL;
364 struct ipv6hdr *iph = ipv6_hdr(skb);
Craig Galleka5836362016-02-10 11:50:38 -0500365 struct sk_buff *data_skb = NULL;
366 int doff = 0;
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200367 int thoff = 0, tproto;
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200368
Hans Schillstrom84018f52012-04-23 03:35:26 +0000369 tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL);
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200370 if (tproto < 0) {
371 pr_debug("unable to find transport header in IPv6 packet, dropping\n");
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200372 return NULL;
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200373 }
374
375 if (tproto == IPPROTO_UDP || tproto == IPPROTO_TCP) {
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200376 struct udphdr _hdr, *hp;
377
378 hp = skb_header_pointer(skb, thoff, sizeof(_hdr), &_hdr);
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200379 if (hp == NULL)
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200380 return NULL;
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200381
382 saddr = &iph->saddr;
383 sport = hp->source;
384 daddr = &iph->daddr;
385 dport = hp->dest;
Craig Galleka5836362016-02-10 11:50:38 -0500386 data_skb = (struct sk_buff *)skb;
387 doff = tproto == IPPROTO_TCP ?
388 thoff + __tcp_hdrlen((struct tcphdr *)hp) :
389 thoff + sizeof(*hp);
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200390
391 } else if (tproto == IPPROTO_ICMPV6) {
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200392 struct ipv6hdr ipv6_var;
393
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200394 if (extract_icmp6_fields(skb, thoff, &tproto, &saddr, &daddr,
Eric Dumazet78296c92015-02-15 19:03:45 -0800395 &sport, &dport, &ipv6_var))
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200396 return NULL;
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200397 } else {
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200398 return NULL;
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200399 }
400
Craig Galleka5836362016-02-10 11:50:38 -0500401 return xt_socket_get_sock_v6(net, data_skb, doff, tproto, saddr, daddr,
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200402 sport, dport, indev);
403}
John Stultz994ee752014-03-28 16:23:48 -0700404EXPORT_SYMBOL(xt_socket_lookup_slow_v6);
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200405
406static bool
Harout Hedeshian01555e72015-06-15 18:40:43 -0600407socket_mt6_v1_v2_v3(const struct sk_buff *skb, struct xt_action_param *par)
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200408{
409 const struct xt_socket_mtinfo1 *info = (struct xt_socket_mtinfo1 *) par->matchinfo;
Harout Hedeshian01555e72015-06-15 18:40:43 -0600410 struct sk_buff *pskb = (struct sk_buff *)skb;
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200411 struct sock *sk = skb->sk;
412
Eric Dumazet00028aa2013-05-22 11:01:06 +0000413 if (!sk)
Eric W. Biederman686c9b52015-09-18 14:32:59 -0500414 sk = xt_socket_lookup_slow_v6(par->net, skb, par->in);
Eric Dumazet00028aa2013-05-22 11:01:06 +0000415 if (sk) {
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200416 bool wildcard;
417 bool transparent = true;
418
Eric Dumazet681f1302013-06-20 05:52:22 -0700419 /* Ignore sockets listening on INADDR_ANY
420 * unless XT_SOCKET_NOWILDCARD is set
421 */
422 wildcard = (!(info->flags & XT_SOCKET_NOWILDCARD) &&
Eric Dumazeta9407002015-03-16 21:06:17 -0700423 sk_fullsock(sk) &&
Eric Dumazetefe42082013-10-03 15:42:29 -0700424 ipv6_addr_any(&sk->sk_v6_rcv_saddr));
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200425
426 /* Ignore non-transparent sockets,
Eric Dumazeta9407002015-03-16 21:06:17 -0700427 * if XT_SOCKET_TRANSPARENT is used
428 */
Eric Dumazetbaf60ef2013-07-11 19:22:19 -0700429 if (info->flags & XT_SOCKET_TRANSPARENT)
Eric Dumazeta9407002015-03-16 21:06:17 -0700430 transparent = xt_socket_sk_is_transparent(sk);
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200431
Harout Hedeshian01555e72015-06-15 18:40:43 -0600432 if (info->flags & XT_SOCKET_RESTORESKMARK && !wildcard &&
433 transparent)
434 pskb->mark = sk->sk_mark;
435
Eric Dumazet00028aa2013-05-22 11:01:06 +0000436 if (sk != skb->sk)
Eric Dumazet1a8bf6e2013-10-11 09:03:25 -0700437 sock_gen_put(sk);
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200438
439 if (wildcard || !transparent)
440 sk = NULL;
441 }
442
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200443 return sk != NULL;
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200444}
445#endif
446
Eric Dumazet681f1302013-06-20 05:52:22 -0700447static 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
458static 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 Hedeshian01555e72015-06-15 18:40:43 -0600469static 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 Totha31e1ff2009-06-09 15:16:34 +0200482static struct xt_match socket_mt_reg[] __read_mostly = {
483 {
484 .name = "socket",
485 .revision = 0,
486 .family = NFPROTO_IPV4,
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200487 .match = socket_mt4_v0,
Jan Engelhardtaa3c4872009-10-29 15:35:10 +0100488 .hooks = (1 << NF_INET_PRE_ROUTING) |
489 (1 << NF_INET_LOCAL_IN),
Laszlo Attila Totha31e1ff2009-06-09 15:16:34 +0200490 .me = THIS_MODULE,
491 },
492 {
493 .name = "socket",
494 .revision = 1,
495 .family = NFPROTO_IPV4,
Harout Hedeshian01555e72015-06-15 18:40:43 -0600496 .match = socket_mt4_v1_v2_v3,
Eric Dumazet681f1302013-06-20 05:52:22 -0700497 .checkentry = socket_mt_v1_check,
Laszlo Attila Totha31e1ff2009-06-09 15:16:34 +0200498 .matchsize = sizeof(struct xt_socket_mtinfo1),
Jan Engelhardtaa3c4872009-10-29 15:35:10 +0100499 .hooks = (1 << NF_INET_PRE_ROUTING) |
500 (1 << NF_INET_LOCAL_IN),
Laszlo Attila Totha31e1ff2009-06-09 15:16:34 +0200501 .me = THIS_MODULE,
502 },
KOVACS Krisztianf6318e52010-10-24 23:38:32 +0000503#ifdef XT_SOCKET_HAVE_IPV6
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200504 {
505 .name = "socket",
506 .revision = 1,
507 .family = NFPROTO_IPV6,
Harout Hedeshian01555e72015-06-15 18:40:43 -0600508 .match = socket_mt6_v1_v2_v3,
Eric Dumazet681f1302013-06-20 05:52:22 -0700509 .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 Hedeshian01555e72015-06-15 18:40:43 -0600520 .match = socket_mt4_v1_v2_v3,
Eric Dumazet681f1302013-06-20 05:52:22 -0700521 .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 Hedeshian01555e72015-06-15 18:40:43 -0600532 .match = socket_mt6_v1_v2_v3,
Eric Dumazet681f1302013-06-20 05:52:22 -0700533 .checkentry = socket_mt_v2_check,
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200534 .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 Hedeshian01555e72015-06-15 18:40:43 -0600540 {
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 Krisztian136cdc72008-10-08 11:35:12 +0200564};
565
566static int __init socket_mt_init(void)
567{
568 nf_defrag_ipv4_enable();
KOVACS Krisztianf6318e52010-10-24 23:38:32 +0000569#ifdef XT_SOCKET_HAVE_IPV6
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200570 nf_defrag_ipv6_enable();
571#endif
572
Laszlo Attila Totha31e1ff2009-06-09 15:16:34 +0200573 return xt_register_matches(socket_mt_reg, ARRAY_SIZE(socket_mt_reg));
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200574}
575
576static void __exit socket_mt_exit(void)
577{
Laszlo Attila Totha31e1ff2009-06-09 15:16:34 +0200578 xt_unregister_matches(socket_mt_reg, ARRAY_SIZE(socket_mt_reg));
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200579}
580
581module_init(socket_mt_init);
582module_exit(socket_mt_exit);
583
584MODULE_LICENSE("GPL");
585MODULE_AUTHOR("Krisztian Kovacs, Balazs Scheidler");
586MODULE_DESCRIPTION("x_tables socket match module");
587MODULE_ALIAS("ipt_socket");
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200588MODULE_ALIAS("ip6t_socket");