]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/netfilter/xt_socket.c
tcp/dccp: remove twchain
[mirror_ubuntu-bionic-kernel.git] / net / netfilter / xt_socket.c
CommitLineData
136cdc71
KK
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 */
ff67e4e4 12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
136cdc71
KK
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>
136cdc71 22#include <net/netfilter/ipv4/nf_defrag_ipv4.h>
f6318e55 23
c0cd1156 24#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
f6318e55
KK
25#define XT_SOCKET_HAVE_IPV6 1
26#include <linux/netfilter_ipv6/ip6_tables.h>
93742cf8 27#include <net/inet6_hashtables.h>
b64c9256 28#include <net/netfilter/ipv6/nf_defrag_ipv6.h>
f6318e55 29#endif
136cdc71 30
a31e1ffd
LAT
31#include <linux/netfilter/xt_socket.h>
32
c0cd1156 33#if IS_ENABLED(CONFIG_NF_CONNTRACK)
136cdc71
KK
34#define XT_SOCKET_HAVE_CONNTRACK 1
35#include <net/netfilter/nf_conntrack.h>
36#endif
37
d503b30b
FW
38static void
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}
46
136cdc71 47static int
b64c9256 48extract_icmp4_fields(const struct sk_buff *skb,
136cdc71
KK
49 u8 *protocol,
50 __be32 *raddr,
51 __be32 *laddr,
52 __be16 *rport,
53 __be16 *lport)
54{
55 unsigned int outside_hdrlen = ip_hdrlen(skb);
56 struct iphdr *inside_iph, _inside_iph;
57 struct icmphdr *icmph, _icmph;
58 __be16 *ports, _ports[2];
59
60 icmph = skb_header_pointer(skb, outside_hdrlen,
61 sizeof(_icmph), &_icmph);
62 if (icmph == NULL)
63 return 1;
64
65 switch (icmph->type) {
66 case ICMP_DEST_UNREACH:
67 case ICMP_SOURCE_QUENCH:
68 case ICMP_REDIRECT:
69 case ICMP_TIME_EXCEEDED:
70 case ICMP_PARAMETERPROB:
71 break;
72 default:
73 return 1;
74 }
75
76 inside_iph = skb_header_pointer(skb, outside_hdrlen +
77 sizeof(struct icmphdr),
78 sizeof(_inside_iph), &_inside_iph);
79 if (inside_iph == NULL)
80 return 1;
81
82 if (inside_iph->protocol != IPPROTO_TCP &&
83 inside_iph->protocol != IPPROTO_UDP)
84 return 1;
85
86 ports = skb_header_pointer(skb, outside_hdrlen +
87 sizeof(struct icmphdr) +
88 (inside_iph->ihl << 2),
89 sizeof(_ports), &_ports);
90 if (ports == NULL)
91 return 1;
92
93 /* the inside IP packet is the one quoted from our side, thus
94 * its saddr is the local address */
95 *protocol = inside_iph->protocol;
96 *laddr = inside_iph->saddr;
97 *lport = ports[0];
98 *raddr = inside_iph->daddr;
99 *rport = ports[1];
100
101 return 0;
102}
103
93742cf8
FW
104/* "socket" match based redirection (no specific rule)
105 * ===================================================
106 *
107 * There are connections with dynamic endpoints (e.g. FTP data
108 * connection) that the user is unable to add explicit rules
109 * for. These are taken care of by a generic "socket" rule. It is
110 * assumed that the proxy application is trusted to open such
111 * connections without explicit iptables rule (except of course the
112 * generic 'socket' rule). In this case the following sockets are
113 * matched in preference order:
114 *
115 * - match: if there's a fully established connection matching the
116 * _packet_ tuple
117 *
118 * - match: if there's a non-zero bound listener (possibly with a
119 * non-local address) We don't accept zero-bound listeners, since
120 * then local services could intercept traffic going through the
121 * box.
122 */
123static struct sock *
124xt_socket_get_sock_v4(struct net *net, const u8 protocol,
125 const __be32 saddr, const __be32 daddr,
126 const __be16 sport, const __be16 dport,
127 const struct net_device *in)
128{
129 switch (protocol) {
130 case IPPROTO_TCP:
131 return __inet_lookup(net, &tcp_hashinfo,
132 saddr, sport, daddr, dport,
133 in->ifindex);
134 case IPPROTO_UDP:
135 return udp4_lib_lookup(net, saddr, sport, daddr, dport,
136 in->ifindex);
137 }
138 return NULL;
139}
140
136cdc71 141static bool
62fc8051 142socket_match(const struct sk_buff *skb, struct xt_action_param *par,
a31e1ffd 143 const struct xt_socket_mtinfo1 *info)
136cdc71
KK
144{
145 const struct iphdr *iph = ip_hdr(skb);
146 struct udphdr _hdr, *hp = NULL;
00028aa3 147 struct sock *sk = skb->sk;
6703aa74
PNA
148 __be32 uninitialized_var(daddr), uninitialized_var(saddr);
149 __be16 uninitialized_var(dport), uninitialized_var(sport);
150 u8 uninitialized_var(protocol);
136cdc71
KK
151#ifdef XT_SOCKET_HAVE_CONNTRACK
152 struct nf_conn const *ct;
153 enum ip_conntrack_info ctinfo;
154#endif
155
156 if (iph->protocol == IPPROTO_UDP || iph->protocol == IPPROTO_TCP) {
157 hp = skb_header_pointer(skb, ip_hdrlen(skb),
158 sizeof(_hdr), &_hdr);
159 if (hp == NULL)
160 return false;
161
162 protocol = iph->protocol;
163 saddr = iph->saddr;
164 sport = hp->source;
165 daddr = iph->daddr;
166 dport = hp->dest;
167
168 } else if (iph->protocol == IPPROTO_ICMP) {
b64c9256 169 if (extract_icmp4_fields(skb, &protocol, &saddr, &daddr,
136cdc71
KK
170 &sport, &dport))
171 return false;
172 } else {
173 return false;
174 }
175
176#ifdef XT_SOCKET_HAVE_CONNTRACK
177 /* Do the lookup with the original socket address in case this is a
178 * reply packet of an established SNAT-ted connection. */
179
180 ct = nf_ct_get(skb, &ctinfo);
5bfddbd4 181 if (ct && !nf_ct_is_untracked(ct) &&
136cdc71 182 ((iph->protocol != IPPROTO_ICMP &&
fb048833 183 ctinfo == IP_CT_ESTABLISHED_REPLY) ||
136cdc71 184 (iph->protocol == IPPROTO_ICMP &&
fb048833 185 ctinfo == IP_CT_RELATED_REPLY)) &&
136cdc71
KK
186 (ct->status & IPS_SRC_NAT_DONE)) {
187
188 daddr = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
189 dport = (iph->protocol == IPPROTO_TCP) ?
190 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.tcp.port :
191 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.udp.port;
192 }
193#endif
194
00028aa3 195 if (!sk)
93742cf8 196 sk = xt_socket_get_sock_v4(dev_net(skb->dev), protocol,
00028aa3 197 saddr, daddr, sport, dport,
93742cf8 198 par->in);
00028aa3 199 if (sk) {
a31e1ffd
LAT
200 bool wildcard;
201 bool transparent = true;
202
681f130f
ED
203 /* Ignore sockets listening on INADDR_ANY,
204 * unless XT_SOCKET_NOWILDCARD is set
205 */
206 wildcard = (!(info->flags & XT_SOCKET_NOWILDCARD) &&
207 sk->sk_state != TCP_TIME_WAIT &&
c720c7e8 208 inet_sk(sk)->inet_rcv_saddr == 0);
a31e1ffd
LAT
209
210 /* Ignore non-transparent sockets,
211 if XT_SOCKET_TRANSPARENT is used */
baf60efa 212 if (info->flags & XT_SOCKET_TRANSPARENT)
a31e1ffd
LAT
213 transparent = ((sk->sk_state != TCP_TIME_WAIT &&
214 inet_sk(sk)->transparent) ||
215 (sk->sk_state == TCP_TIME_WAIT &&
216 inet_twsk(sk)->tw_transparent));
136cdc71 217
00028aa3
ED
218 if (sk != skb->sk)
219 xt_socket_put_sk(sk);
a31e1ffd
LAT
220
221 if (wildcard || !transparent)
136cdc71
KK
222 sk = NULL;
223 }
224
b64c9256
BS
225 pr_debug("proto %hhu %pI4:%hu -> %pI4:%hu (orig %pI4:%hu) sock %p\n",
226 protocol, &saddr, ntohs(sport),
227 &daddr, ntohs(dport),
228 &iph->daddr, hp ? ntohs(hp->dest) : 0, sk);
136cdc71
KK
229
230 return (sk != NULL);
231}
232
a31e1ffd 233static bool
b64c9256 234socket_mt4_v0(const struct sk_buff *skb, struct xt_action_param *par)
a31e1ffd 235{
baf60efa
ED
236 static struct xt_socket_mtinfo1 xt_info_v0 = {
237 .flags = 0,
238 };
239
240 return socket_match(skb, par, &xt_info_v0);
a31e1ffd
LAT
241}
242
243static bool
681f130f 244socket_mt4_v1_v2(const struct sk_buff *skb, struct xt_action_param *par)
a31e1ffd
LAT
245{
246 return socket_match(skb, par, par->matchinfo);
247}
248
f6318e55 249#ifdef XT_SOCKET_HAVE_IPV6
b64c9256
BS
250
251static int
252extract_icmp6_fields(const struct sk_buff *skb,
253 unsigned int outside_hdrlen,
089282fb 254 int *protocol,
b64c9256
BS
255 struct in6_addr **raddr,
256 struct in6_addr **laddr,
257 __be16 *rport,
258 __be16 *lport)
259{
260 struct ipv6hdr *inside_iph, _inside_iph;
261 struct icmp6hdr *icmph, _icmph;
262 __be16 *ports, _ports[2];
263 u8 inside_nexthdr;
75f2811c 264 __be16 inside_fragoff;
b64c9256
BS
265 int inside_hdrlen;
266
267 icmph = skb_header_pointer(skb, outside_hdrlen,
268 sizeof(_icmph), &_icmph);
269 if (icmph == NULL)
270 return 1;
271
272 if (icmph->icmp6_type & ICMPV6_INFOMSG_MASK)
273 return 1;
274
275 inside_iph = skb_header_pointer(skb, outside_hdrlen + sizeof(_icmph), sizeof(_inside_iph), &_inside_iph);
276 if (inside_iph == NULL)
277 return 1;
278 inside_nexthdr = inside_iph->nexthdr;
279
75f2811c
JG
280 inside_hdrlen = ipv6_skip_exthdr(skb, outside_hdrlen + sizeof(_icmph) + sizeof(_inside_iph),
281 &inside_nexthdr, &inside_fragoff);
b64c9256
BS
282 if (inside_hdrlen < 0)
283 return 1; /* hjm: Packet has no/incomplete transport layer headers. */
284
285 if (inside_nexthdr != IPPROTO_TCP &&
286 inside_nexthdr != IPPROTO_UDP)
287 return 1;
288
289 ports = skb_header_pointer(skb, inside_hdrlen,
290 sizeof(_ports), &_ports);
291 if (ports == NULL)
292 return 1;
293
294 /* the inside IP packet is the one quoted from our side, thus
295 * its saddr is the local address */
296 *protocol = inside_nexthdr;
297 *laddr = &inside_iph->saddr;
298 *lport = ports[0];
299 *raddr = &inside_iph->daddr;
300 *rport = ports[1];
301
302 return 0;
303}
304
93742cf8
FW
305static struct sock *
306xt_socket_get_sock_v6(struct net *net, const u8 protocol,
307 const struct in6_addr *saddr, const struct in6_addr *daddr,
308 const __be16 sport, const __be16 dport,
309 const struct net_device *in)
310{
311 switch (protocol) {
312 case IPPROTO_TCP:
313 return inet6_lookup(net, &tcp_hashinfo,
314 saddr, sport, daddr, dport,
315 in->ifindex);
316 case IPPROTO_UDP:
317 return udp6_lib_lookup(net, saddr, sport, daddr, dport,
318 in->ifindex);
319 }
320
321 return NULL;
322}
323
b64c9256 324static bool
681f130f 325socket_mt6_v1_v2(const struct sk_buff *skb, struct xt_action_param *par)
b64c9256
BS
326{
327 struct ipv6hdr *iph = ipv6_hdr(skb);
328 struct udphdr _hdr, *hp = NULL;
00028aa3 329 struct sock *sk = skb->sk;
6703aa74
PNA
330 struct in6_addr *daddr = NULL, *saddr = NULL;
331 __be16 uninitialized_var(dport), uninitialized_var(sport);
332 int thoff = 0, uninitialized_var(tproto);
b64c9256
BS
333 const struct xt_socket_mtinfo1 *info = (struct xt_socket_mtinfo1 *) par->matchinfo;
334
84018f55 335 tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL);
b64c9256
BS
336 if (tproto < 0) {
337 pr_debug("unable to find transport header in IPv6 packet, dropping\n");
338 return NF_DROP;
339 }
340
341 if (tproto == IPPROTO_UDP || tproto == IPPROTO_TCP) {
342 hp = skb_header_pointer(skb, thoff,
343 sizeof(_hdr), &_hdr);
344 if (hp == NULL)
345 return false;
346
347 saddr = &iph->saddr;
348 sport = hp->source;
349 daddr = &iph->daddr;
350 dport = hp->dest;
351
352 } else if (tproto == IPPROTO_ICMPV6) {
353 if (extract_icmp6_fields(skb, thoff, &tproto, &saddr, &daddr,
354 &sport, &dport))
355 return false;
356 } else {
357 return false;
358 }
359
00028aa3 360 if (!sk)
93742cf8 361 sk = xt_socket_get_sock_v6(dev_net(skb->dev), tproto,
00028aa3 362 saddr, daddr, sport, dport,
93742cf8 363 par->in);
00028aa3 364 if (sk) {
b64c9256
BS
365 bool wildcard;
366 bool transparent = true;
367
681f130f
ED
368 /* Ignore sockets listening on INADDR_ANY
369 * unless XT_SOCKET_NOWILDCARD is set
370 */
371 wildcard = (!(info->flags & XT_SOCKET_NOWILDCARD) &&
372 sk->sk_state != TCP_TIME_WAIT &&
b64c9256
BS
373 ipv6_addr_any(&inet6_sk(sk)->rcv_saddr));
374
375 /* Ignore non-transparent sockets,
376 if XT_SOCKET_TRANSPARENT is used */
baf60efa 377 if (info->flags & XT_SOCKET_TRANSPARENT)
b64c9256
BS
378 transparent = ((sk->sk_state != TCP_TIME_WAIT &&
379 inet_sk(sk)->transparent) ||
380 (sk->sk_state == TCP_TIME_WAIT &&
381 inet_twsk(sk)->tw_transparent));
382
00028aa3
ED
383 if (sk != skb->sk)
384 xt_socket_put_sk(sk);
b64c9256
BS
385
386 if (wildcard || !transparent)
387 sk = NULL;
388 }
389
089282fb 390 pr_debug("proto %hhd %pI6:%hu -> %pI6:%hu "
b64c9256
BS
391 "(orig %pI6:%hu) sock %p\n",
392 tproto, saddr, ntohs(sport),
393 daddr, ntohs(dport),
394 &iph->daddr, hp ? ntohs(hp->dest) : 0, sk);
395
396 return (sk != NULL);
397}
398#endif
399
681f130f
ED
400static int socket_mt_v1_check(const struct xt_mtchk_param *par)
401{
402 const struct xt_socket_mtinfo1 *info = (struct xt_socket_mtinfo1 *) par->matchinfo;
403
404 if (info->flags & ~XT_SOCKET_FLAGS_V1) {
405 pr_info("unknown flags 0x%x\n", info->flags & ~XT_SOCKET_FLAGS_V1);
406 return -EINVAL;
407 }
408 return 0;
409}
410
411static int socket_mt_v2_check(const struct xt_mtchk_param *par)
412{
413 const struct xt_socket_mtinfo2 *info = (struct xt_socket_mtinfo2 *) par->matchinfo;
414
415 if (info->flags & ~XT_SOCKET_FLAGS_V2) {
416 pr_info("unknown flags 0x%x\n", info->flags & ~XT_SOCKET_FLAGS_V2);
417 return -EINVAL;
418 }
419 return 0;
420}
421
a31e1ffd
LAT
422static struct xt_match socket_mt_reg[] __read_mostly = {
423 {
424 .name = "socket",
425 .revision = 0,
426 .family = NFPROTO_IPV4,
b64c9256 427 .match = socket_mt4_v0,
aa3c487f
JE
428 .hooks = (1 << NF_INET_PRE_ROUTING) |
429 (1 << NF_INET_LOCAL_IN),
a31e1ffd
LAT
430 .me = THIS_MODULE,
431 },
432 {
433 .name = "socket",
434 .revision = 1,
435 .family = NFPROTO_IPV4,
681f130f
ED
436 .match = socket_mt4_v1_v2,
437 .checkentry = socket_mt_v1_check,
a31e1ffd 438 .matchsize = sizeof(struct xt_socket_mtinfo1),
aa3c487f
JE
439 .hooks = (1 << NF_INET_PRE_ROUTING) |
440 (1 << NF_INET_LOCAL_IN),
a31e1ffd
LAT
441 .me = THIS_MODULE,
442 },
f6318e55 443#ifdef XT_SOCKET_HAVE_IPV6
b64c9256
BS
444 {
445 .name = "socket",
446 .revision = 1,
447 .family = NFPROTO_IPV6,
681f130f
ED
448 .match = socket_mt6_v1_v2,
449 .checkentry = socket_mt_v1_check,
450 .matchsize = sizeof(struct xt_socket_mtinfo1),
451 .hooks = (1 << NF_INET_PRE_ROUTING) |
452 (1 << NF_INET_LOCAL_IN),
453 .me = THIS_MODULE,
454 },
455#endif
456 {
457 .name = "socket",
458 .revision = 2,
459 .family = NFPROTO_IPV4,
460 .match = socket_mt4_v1_v2,
461 .checkentry = socket_mt_v2_check,
462 .matchsize = sizeof(struct xt_socket_mtinfo1),
463 .hooks = (1 << NF_INET_PRE_ROUTING) |
464 (1 << NF_INET_LOCAL_IN),
465 .me = THIS_MODULE,
466 },
467#ifdef XT_SOCKET_HAVE_IPV6
468 {
469 .name = "socket",
470 .revision = 2,
471 .family = NFPROTO_IPV6,
472 .match = socket_mt6_v1_v2,
473 .checkentry = socket_mt_v2_check,
b64c9256
BS
474 .matchsize = sizeof(struct xt_socket_mtinfo1),
475 .hooks = (1 << NF_INET_PRE_ROUTING) |
476 (1 << NF_INET_LOCAL_IN),
477 .me = THIS_MODULE,
478 },
479#endif
136cdc71
KK
480};
481
482static int __init socket_mt_init(void)
483{
484 nf_defrag_ipv4_enable();
f6318e55 485#ifdef XT_SOCKET_HAVE_IPV6
b64c9256
BS
486 nf_defrag_ipv6_enable();
487#endif
488
a31e1ffd 489 return xt_register_matches(socket_mt_reg, ARRAY_SIZE(socket_mt_reg));
136cdc71
KK
490}
491
492static void __exit socket_mt_exit(void)
493{
a31e1ffd 494 xt_unregister_matches(socket_mt_reg, ARRAY_SIZE(socket_mt_reg));
136cdc71
KK
495}
496
497module_init(socket_mt_init);
498module_exit(socket_mt_exit);
499
500MODULE_LICENSE("GPL");
501MODULE_AUTHOR("Krisztian Kovacs, Balazs Scheidler");
502MODULE_DESCRIPTION("x_tables socket match module");
503MODULE_ALIAS("ipt_socket");
b64c9256 504MODULE_ALIAS("ip6t_socket");