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