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(const struct sk_buff *skb,
147					     const struct net_device *indev)
148{
149	const struct iphdr *iph = ip_hdr(skb);
150	__be32 uninitialized_var(daddr), uninitialized_var(saddr);
151	__be16 uninitialized_var(dport), uninitialized_var(sport);
152	u8 uninitialized_var(protocol);
153#ifdef XT_SOCKET_HAVE_CONNTRACK
154	struct nf_conn const *ct;
155	enum ip_conntrack_info ctinfo;
156#endif
157
158	if (iph->protocol == IPPROTO_UDP || iph->protocol == IPPROTO_TCP) {
159		struct udphdr _hdr, *hp;
160
161		hp = skb_header_pointer(skb, ip_hdrlen(skb),
162					sizeof(_hdr), &_hdr);
163		if (hp == NULL)
164			return NULL;
165
166		protocol = iph->protocol;
167		saddr = iph->saddr;
168		sport = hp->source;
169		daddr = iph->daddr;
170		dport = hp->dest;
171
172	} else if (iph->protocol == IPPROTO_ICMP) {
173		if (extract_icmp4_fields(skb, &protocol, &saddr, &daddr,
174					 &sport, &dport))
175			return NULL;
176	} else {
177		return NULL;
178	}
179
180#ifdef XT_SOCKET_HAVE_CONNTRACK
181	/* Do the lookup with the original socket address in
182	 * case this is a reply packet of an established
183	 * SNAT-ted connection.
184	 */
185	ct = nf_ct_get(skb, &ctinfo);
186	if (ct && !nf_ct_is_untracked(ct) &&
187	    ((iph->protocol != IPPROTO_ICMP &&
188	      ctinfo == IP_CT_ESTABLISHED_REPLY) ||
189	     (iph->protocol == IPPROTO_ICMP &&
190	      ctinfo == IP_CT_RELATED_REPLY)) &&
191	    (ct->status & IPS_SRC_NAT_DONE)) {
192
193		daddr = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
194		dport = (iph->protocol == IPPROTO_TCP) ?
195			ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.tcp.port :
196			ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.udp.port;
197	}
198#endif
199
200	return xt_socket_get_sock_v4(dev_net(skb->dev), protocol, saddr, daddr,
201				     sport, dport, indev);
202}
203
204static bool
205socket_match(const struct sk_buff *skb, struct xt_action_param *par,
206	     const struct xt_socket_mtinfo1 *info)
207{
208	struct sock *sk = skb->sk;
209
210	if (!sk)
211		sk = xt_socket_lookup_slow_v4(skb, par->in);
212	if (sk) {
213		bool wildcard;
214		bool transparent = true;
215
216		/* Ignore sockets listening on INADDR_ANY,
217		 * unless XT_SOCKET_NOWILDCARD is set
218		 */
219		wildcard = (!(info->flags & XT_SOCKET_NOWILDCARD) &&
220			    sk_fullsock(sk) &&
221			    inet_sk(sk)->inet_rcv_saddr == 0);
222
223		/* Ignore non-transparent sockets,
224		 * if XT_SOCKET_TRANSPARENT is used
225		 */
226		if (info->flags & XT_SOCKET_TRANSPARENT)
227			transparent = xt_socket_sk_is_transparent(sk);
228
229		if (sk != skb->sk)
230			sock_gen_put(sk);
231
232		if (wildcard || !transparent)
233			sk = NULL;
234	}
235
236	return sk != NULL;
237}
238
239static bool
240socket_mt4_v0(const struct sk_buff *skb, struct xt_action_param *par)
241{
242	static struct xt_socket_mtinfo1 xt_info_v0 = {
243		.flags = 0,
244	};
245
246	return socket_match(skb, par, &xt_info_v0);
247}
248
249static bool
250socket_mt4_v1_v2(const struct sk_buff *skb, struct xt_action_param *par)
251{
252	return socket_match(skb, par, par->matchinfo);
253}
254
255#ifdef XT_SOCKET_HAVE_IPV6
256
257static int
258extract_icmp6_fields(const struct sk_buff *skb,
259		     unsigned int outside_hdrlen,
260		     int *protocol,
261		     const struct in6_addr **raddr,
262		     const struct in6_addr **laddr,
263		     __be16 *rport,
264		     __be16 *lport,
265		     struct ipv6hdr *ipv6_var)
266{
267	const struct ipv6hdr *inside_iph;
268	struct icmp6hdr *icmph, _icmph;
269	__be16 *ports, _ports[2];
270	u8 inside_nexthdr;
271	__be16 inside_fragoff;
272	int inside_hdrlen;
273
274	icmph = skb_header_pointer(skb, outside_hdrlen,
275				   sizeof(_icmph), &_icmph);
276	if (icmph == NULL)
277		return 1;
278
279	if (icmph->icmp6_type & ICMPV6_INFOMSG_MASK)
280		return 1;
281
282	inside_iph = skb_header_pointer(skb, outside_hdrlen + sizeof(_icmph),
283					sizeof(*ipv6_var), ipv6_var);
284	if (inside_iph == NULL)
285		return 1;
286	inside_nexthdr = inside_iph->nexthdr;
287
288	inside_hdrlen = ipv6_skip_exthdr(skb, outside_hdrlen + sizeof(_icmph) +
289					      sizeof(*ipv6_var),
290					 &inside_nexthdr, &inside_fragoff);
291	if (inside_hdrlen < 0)
292		return 1; /* hjm: Packet has no/incomplete transport layer headers. */
293
294	if (inside_nexthdr != IPPROTO_TCP &&
295	    inside_nexthdr != IPPROTO_UDP)
296		return 1;
297
298	ports = skb_header_pointer(skb, inside_hdrlen,
299				   sizeof(_ports), &_ports);
300	if (ports == NULL)
301		return 1;
302
303	/* the inside IP packet is the one quoted from our side, thus
304	 * its saddr is the local address */
305	*protocol = inside_nexthdr;
306	*laddr = &inside_iph->saddr;
307	*lport = ports[0];
308	*raddr = &inside_iph->daddr;
309	*rport = ports[1];
310
311	return 0;
312}
313
314static struct sock *
315xt_socket_get_sock_v6(struct net *net, const u8 protocol,
316		      const struct in6_addr *saddr, const struct in6_addr *daddr,
317		      const __be16 sport, const __be16 dport,
318		      const struct net_device *in)
319{
320	switch (protocol) {
321	case IPPROTO_TCP:
322		return inet6_lookup(net, &tcp_hashinfo,
323				    saddr, sport, daddr, dport,
324				    in->ifindex);
325	case IPPROTO_UDP:
326		return udp6_lib_lookup(net, saddr, sport, daddr, dport,
327				       in->ifindex);
328	}
329
330	return NULL;
331}
332
333static struct sock *xt_socket_lookup_slow_v6(const struct sk_buff *skb,
334					     const struct net_device *indev)
335{
336	__be16 uninitialized_var(dport), uninitialized_var(sport);
337	const struct in6_addr *daddr = NULL, *saddr = NULL;
338	struct ipv6hdr *iph = ipv6_hdr(skb);
339	int thoff = 0, tproto;
340
341	tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL);
342	if (tproto < 0) {
343		pr_debug("unable to find transport header in IPv6 packet, dropping\n");
344		return NULL;
345	}
346
347	if (tproto == IPPROTO_UDP || tproto == IPPROTO_TCP) {
348		struct udphdr _hdr, *hp;
349
350		hp = skb_header_pointer(skb, thoff, sizeof(_hdr), &_hdr);
351		if (hp == NULL)
352			return NULL;
353
354		saddr = &iph->saddr;
355		sport = hp->source;
356		daddr = &iph->daddr;
357		dport = hp->dest;
358
359	} else if (tproto == IPPROTO_ICMPV6) {
360		struct ipv6hdr ipv6_var;
361
362		if (extract_icmp6_fields(skb, thoff, &tproto, &saddr, &daddr,
363					 &sport, &dport, &ipv6_var))
364			return NULL;
365	} else {
366		return NULL;
367	}
368
369	return xt_socket_get_sock_v6(dev_net(skb->dev), tproto, saddr, daddr,
370				     sport, dport, indev);
371}
372
373static bool
374socket_mt6_v1_v2(const struct sk_buff *skb, struct xt_action_param *par)
375{
376	const struct xt_socket_mtinfo1 *info = (struct xt_socket_mtinfo1 *) par->matchinfo;
377	struct sock *sk = skb->sk;
378
379	if (!sk)
380		sk = xt_socket_lookup_slow_v6(skb, par->in);
381	if (sk) {
382		bool wildcard;
383		bool transparent = true;
384
385		/* Ignore sockets listening on INADDR_ANY
386		 * unless XT_SOCKET_NOWILDCARD is set
387		 */
388		wildcard = (!(info->flags & XT_SOCKET_NOWILDCARD) &&
389			    sk_fullsock(sk) &&
390			    ipv6_addr_any(&sk->sk_v6_rcv_saddr));
391
392		/* Ignore non-transparent sockets,
393		 * if XT_SOCKET_TRANSPARENT is used
394		 */
395		if (info->flags & XT_SOCKET_TRANSPARENT)
396			transparent = xt_socket_sk_is_transparent(sk);
397
398		if (sk != skb->sk)
399			sock_gen_put(sk);
400
401		if (wildcard || !transparent)
402			sk = NULL;
403	}
404
405	return sk != NULL;
406}
407#endif
408
409static int socket_mt_v1_check(const struct xt_mtchk_param *par)
410{
411	const struct xt_socket_mtinfo1 *info = (struct xt_socket_mtinfo1 *) par->matchinfo;
412
413	if (info->flags & ~XT_SOCKET_FLAGS_V1) {
414		pr_info("unknown flags 0x%x\n", info->flags & ~XT_SOCKET_FLAGS_V1);
415		return -EINVAL;
416	}
417	return 0;
418}
419
420static int socket_mt_v2_check(const struct xt_mtchk_param *par)
421{
422	const struct xt_socket_mtinfo2 *info = (struct xt_socket_mtinfo2 *) par->matchinfo;
423
424	if (info->flags & ~XT_SOCKET_FLAGS_V2) {
425		pr_info("unknown flags 0x%x\n", info->flags & ~XT_SOCKET_FLAGS_V2);
426		return -EINVAL;
427	}
428	return 0;
429}
430
431static struct xt_match socket_mt_reg[] __read_mostly = {
432	{
433		.name		= "socket",
434		.revision	= 0,
435		.family		= NFPROTO_IPV4,
436		.match		= socket_mt4_v0,
437		.hooks		= (1 << NF_INET_PRE_ROUTING) |
438				  (1 << NF_INET_LOCAL_IN),
439		.me		= THIS_MODULE,
440	},
441	{
442		.name		= "socket",
443		.revision	= 1,
444		.family		= NFPROTO_IPV4,
445		.match		= socket_mt4_v1_v2,
446		.checkentry	= socket_mt_v1_check,
447		.matchsize	= sizeof(struct xt_socket_mtinfo1),
448		.hooks		= (1 << NF_INET_PRE_ROUTING) |
449				  (1 << NF_INET_LOCAL_IN),
450		.me		= THIS_MODULE,
451	},
452#ifdef XT_SOCKET_HAVE_IPV6
453	{
454		.name		= "socket",
455		.revision	= 1,
456		.family		= NFPROTO_IPV6,
457		.match		= socket_mt6_v1_v2,
458		.checkentry	= socket_mt_v1_check,
459		.matchsize	= sizeof(struct xt_socket_mtinfo1),
460		.hooks		= (1 << NF_INET_PRE_ROUTING) |
461				  (1 << NF_INET_LOCAL_IN),
462		.me		= THIS_MODULE,
463	},
464#endif
465	{
466		.name		= "socket",
467		.revision	= 2,
468		.family		= NFPROTO_IPV4,
469		.match		= socket_mt4_v1_v2,
470		.checkentry	= socket_mt_v2_check,
471		.matchsize	= sizeof(struct xt_socket_mtinfo1),
472		.hooks		= (1 << NF_INET_PRE_ROUTING) |
473				  (1 << NF_INET_LOCAL_IN),
474		.me		= THIS_MODULE,
475	},
476#ifdef XT_SOCKET_HAVE_IPV6
477	{
478		.name		= "socket",
479		.revision	= 2,
480		.family		= NFPROTO_IPV6,
481		.match		= socket_mt6_v1_v2,
482		.checkentry	= socket_mt_v2_check,
483		.matchsize	= sizeof(struct xt_socket_mtinfo1),
484		.hooks		= (1 << NF_INET_PRE_ROUTING) |
485				  (1 << NF_INET_LOCAL_IN),
486		.me		= THIS_MODULE,
487	},
488#endif
489};
490
491static int __init socket_mt_init(void)
492{
493	nf_defrag_ipv4_enable();
494#ifdef XT_SOCKET_HAVE_IPV6
495	nf_defrag_ipv6_enable();
496#endif
497
498	return xt_register_matches(socket_mt_reg, ARRAY_SIZE(socket_mt_reg));
499}
500
501static void __exit socket_mt_exit(void)
502{
503	xt_unregister_matches(socket_mt_reg, ARRAY_SIZE(socket_mt_reg));
504}
505
506module_init(socket_mt_init);
507module_exit(socket_mt_exit);
508
509MODULE_LICENSE("GPL");
510MODULE_AUTHOR("Krisztian Kovacs, Balazs Scheidler");
511MODULE_DESCRIPTION("x_tables socket match module");
512MODULE_ALIAS("ipt_socket");
513MODULE_ALIAS("ip6t_socket");
514