blob: 2ec08f04b816bc863e582fed000a2143d0b688a5 [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
38static int
Balazs Scheidlerb64c9252010-10-21 16:19:42 +020039extract_icmp4_fields(const struct sk_buff *skb,
KOVACS Krisztian136cdc72008-10-08 11:35:12 +020040 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
Florian Westphal93742cf2013-07-29 15:41:53 +020095/* "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
Eric Dumazeta9407002015-03-16 21:06:17 -0700132static 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
Eric W. Biederman686c9b52015-09-18 14:32:59 -0500146static struct sock *xt_socket_lookup_slow_v4(struct net *net,
147 const struct sk_buff *skb,
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200148 const struct net_device *indev)
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200149{
150 const struct iphdr *iph = ip_hdr(skb);
Pablo Neira Ayuso6703aa72012-08-29 15:58:29 +0000151 __be32 uninitialized_var(daddr), uninitialized_var(saddr);
152 __be16 uninitialized_var(dport), uninitialized_var(sport);
153 u8 uninitialized_var(protocol);
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200154#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) {
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200160 struct udphdr _hdr, *hp;
161
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200162 hp = skb_header_pointer(skb, ip_hdrlen(skb),
163 sizeof(_hdr), &_hdr);
164 if (hp == NULL)
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200165 return NULL;
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200166
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) {
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200174 if (extract_icmp4_fields(skb, &protocol, &saddr, &daddr,
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200175 &sport, &dport))
176 return NULL;
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200177 } else {
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200178 return NULL;
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200179 }
180
181#ifdef XT_SOCKET_HAVE_CONNTRACK
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200182 /* Do the lookup with the original socket address in
183 * case this is a reply packet of an established
184 * SNAT-ted connection.
185 */
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200186 ct = nf_ct_get(skb, &ctinfo);
Eric Dumazet5bfddbd2010-06-08 16:09:52 +0200187 if (ct && !nf_ct_is_untracked(ct) &&
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200188 ((iph->protocol != IPPROTO_ICMP &&
Eric Dumazetfb048832011-05-19 15:44:27 +0200189 ctinfo == IP_CT_ESTABLISHED_REPLY) ||
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200190 (iph->protocol == IPPROTO_ICMP &&
Eric Dumazetfb048832011-05-19 15:44:27 +0200191 ctinfo == IP_CT_RELATED_REPLY)) &&
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200192 (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
Eric W. Biederman686c9b52015-09-18 14:32:59 -0500201 return xt_socket_get_sock_v4(net, protocol, saddr, daddr,
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200202 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{
Harout Hedeshian01555e72015-06-15 18:40:43 -0600209 struct sk_buff *pskb = (struct sk_buff *)skb;
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200210 struct sock *sk = skb->sk;
211
Eric Dumazet00028aa2013-05-22 11:01:06 +0000212 if (!sk)
Eric W. Biederman686c9b52015-09-18 14:32:59 -0500213 sk = xt_socket_lookup_slow_v4(par->net, skb, par->in);
Eric Dumazet00028aa2013-05-22 11:01:06 +0000214 if (sk) {
Laszlo Attila Totha31e1ff2009-06-09 15:16:34 +0200215 bool wildcard;
216 bool transparent = true;
217
Eric Dumazet681f1302013-06-20 05:52:22 -0700218 /* Ignore sockets listening on INADDR_ANY,
219 * unless XT_SOCKET_NOWILDCARD is set
220 */
221 wildcard = (!(info->flags & XT_SOCKET_NOWILDCARD) &&
Eric Dumazeta9407002015-03-16 21:06:17 -0700222 sk_fullsock(sk) &&
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000223 inet_sk(sk)->inet_rcv_saddr == 0);
Laszlo Attila Totha31e1ff2009-06-09 15:16:34 +0200224
225 /* Ignore non-transparent sockets,
Eric Dumazeta9407002015-03-16 21:06:17 -0700226 * if XT_SOCKET_TRANSPARENT is used
227 */
Eric Dumazetbaf60ef2013-07-11 19:22:19 -0700228 if (info->flags & XT_SOCKET_TRANSPARENT)
Eric Dumazeta9407002015-03-16 21:06:17 -0700229 transparent = xt_socket_sk_is_transparent(sk);
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200230
Harout Hedeshian01555e72015-06-15 18:40:43 -0600231 if (info->flags & XT_SOCKET_RESTORESKMARK && !wildcard &&
232 transparent)
233 pskb->mark = sk->sk_mark;
234
Eric Dumazet00028aa2013-05-22 11:01:06 +0000235 if (sk != skb->sk)
Eric Dumazet1a8bf6e2013-10-11 09:03:25 -0700236 sock_gen_put(sk);
Laszlo Attila Totha31e1ff2009-06-09 15:16:34 +0200237
238 if (wildcard || !transparent)
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200239 sk = NULL;
240 }
241
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200242 return sk != NULL;
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200243}
244
Laszlo Attila Totha31e1ff2009-06-09 15:16:34 +0200245static bool
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200246socket_mt4_v0(const struct sk_buff *skb, struct xt_action_param *par)
Laszlo Attila Totha31e1ff2009-06-09 15:16:34 +0200247{
Eric Dumazetbaf60ef2013-07-11 19:22:19 -0700248 static struct xt_socket_mtinfo1 xt_info_v0 = {
249 .flags = 0,
250 };
251
252 return socket_match(skb, par, &xt_info_v0);
Laszlo Attila Totha31e1ff2009-06-09 15:16:34 +0200253}
254
255static bool
Harout Hedeshian01555e72015-06-15 18:40:43 -0600256socket_mt4_v1_v2_v3(const struct sk_buff *skb, struct xt_action_param *par)
Laszlo Attila Totha31e1ff2009-06-09 15:16:34 +0200257{
258 return socket_match(skb, par, par->matchinfo);
259}
260
KOVACS Krisztianf6318e52010-10-24 23:38:32 +0000261#ifdef XT_SOCKET_HAVE_IPV6
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200262
263static int
264extract_icmp6_fields(const struct sk_buff *skb,
265 unsigned int outside_hdrlen,
David S. Miller089282f2010-10-28 12:59:53 -0700266 int *protocol,
Eric Dumazet78296c92015-02-15 19:03:45 -0800267 const struct in6_addr **raddr,
268 const struct in6_addr **laddr,
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200269 __be16 *rport,
Eric Dumazet78296c92015-02-15 19:03:45 -0800270 __be16 *lport,
271 struct ipv6hdr *ipv6_var)
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200272{
Eric Dumazet78296c92015-02-15 19:03:45 -0800273 const struct ipv6hdr *inside_iph;
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200274 struct icmp6hdr *icmph, _icmph;
275 __be16 *ports, _ports[2];
276 u8 inside_nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -0800277 __be16 inside_fragoff;
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200278 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
Eric Dumazet78296c92015-02-15 19:03:45 -0800288 inside_iph = skb_header_pointer(skb, outside_hdrlen + sizeof(_icmph),
289 sizeof(*ipv6_var), ipv6_var);
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200290 if (inside_iph == NULL)
291 return 1;
292 inside_nexthdr = inside_iph->nexthdr;
293
Eric Dumazet78296c92015-02-15 19:03:45 -0800294 inside_hdrlen = ipv6_skip_exthdr(skb, outside_hdrlen + sizeof(_icmph) +
295 sizeof(*ipv6_var),
Jesse Gross75f28112011-11-30 17:05:51 -0800296 &inside_nexthdr, &inside_fragoff);
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200297 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
Florian Westphal93742cf2013-07-29 15:41:53 +0200320static 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
Eric W. Biederman686c9b52015-09-18 14:32:59 -0500339static struct sock *xt_socket_lookup_slow_v6(struct net *net,
340 const struct sk_buff *skb,
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200341 const struct net_device *indev)
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200342{
Pablo Neira Ayuso6703aa72012-08-29 15:58:29 +0000343 __be16 uninitialized_var(dport), uninitialized_var(sport);
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200344 const struct in6_addr *daddr = NULL, *saddr = NULL;
345 struct ipv6hdr *iph = ipv6_hdr(skb);
346 int thoff = 0, tproto;
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200347
Hans Schillstrom84018f52012-04-23 03:35:26 +0000348 tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL);
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200349 if (tproto < 0) {
350 pr_debug("unable to find transport header in IPv6 packet, dropping\n");
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200351 return NULL;
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200352 }
353
354 if (tproto == IPPROTO_UDP || tproto == IPPROTO_TCP) {
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200355 struct udphdr _hdr, *hp;
356
357 hp = skb_header_pointer(skb, thoff, sizeof(_hdr), &_hdr);
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200358 if (hp == NULL)
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200359 return NULL;
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200360
361 saddr = &iph->saddr;
362 sport = hp->source;
363 daddr = &iph->daddr;
364 dport = hp->dest;
365
366 } else if (tproto == IPPROTO_ICMPV6) {
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200367 struct ipv6hdr ipv6_var;
368
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200369 if (extract_icmp6_fields(skb, thoff, &tproto, &saddr, &daddr,
Eric Dumazet78296c92015-02-15 19:03:45 -0800370 &sport, &dport, &ipv6_var))
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200371 return NULL;
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200372 } else {
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200373 return NULL;
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200374 }
375
Eric W. Biederman686c9b52015-09-18 14:32:59 -0500376 return xt_socket_get_sock_v6(net, tproto, saddr, daddr,
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200377 sport, dport, indev);
378}
379
380static bool
Harout Hedeshian01555e72015-06-15 18:40:43 -0600381socket_mt6_v1_v2_v3(const struct sk_buff *skb, struct xt_action_param *par)
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200382{
383 const struct xt_socket_mtinfo1 *info = (struct xt_socket_mtinfo1 *) par->matchinfo;
Harout Hedeshian01555e72015-06-15 18:40:43 -0600384 struct sk_buff *pskb = (struct sk_buff *)skb;
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200385 struct sock *sk = skb->sk;
386
Eric Dumazet00028aa2013-05-22 11:01:06 +0000387 if (!sk)
Eric W. Biederman686c9b52015-09-18 14:32:59 -0500388 sk = xt_socket_lookup_slow_v6(par->net, skb, par->in);
Eric Dumazet00028aa2013-05-22 11:01:06 +0000389 if (sk) {
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200390 bool wildcard;
391 bool transparent = true;
392
Eric Dumazet681f1302013-06-20 05:52:22 -0700393 /* Ignore sockets listening on INADDR_ANY
394 * unless XT_SOCKET_NOWILDCARD is set
395 */
396 wildcard = (!(info->flags & XT_SOCKET_NOWILDCARD) &&
Eric Dumazeta9407002015-03-16 21:06:17 -0700397 sk_fullsock(sk) &&
Eric Dumazetefe42082013-10-03 15:42:29 -0700398 ipv6_addr_any(&sk->sk_v6_rcv_saddr));
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200399
400 /* Ignore non-transparent sockets,
Eric Dumazeta9407002015-03-16 21:06:17 -0700401 * if XT_SOCKET_TRANSPARENT is used
402 */
Eric Dumazetbaf60ef2013-07-11 19:22:19 -0700403 if (info->flags & XT_SOCKET_TRANSPARENT)
Eric Dumazeta9407002015-03-16 21:06:17 -0700404 transparent = xt_socket_sk_is_transparent(sk);
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200405
Harout Hedeshian01555e72015-06-15 18:40:43 -0600406 if (info->flags & XT_SOCKET_RESTORESKMARK && !wildcard &&
407 transparent)
408 pskb->mark = sk->sk_mark;
409
Eric Dumazet00028aa2013-05-22 11:01:06 +0000410 if (sk != skb->sk)
Eric Dumazet1a8bf6e2013-10-11 09:03:25 -0700411 sock_gen_put(sk);
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200412
413 if (wildcard || !transparent)
414 sk = NULL;
415 }
416
Daniel Borkmannd64d80a2015-04-02 14:28:30 +0200417 return sk != NULL;
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200418}
419#endif
420
Eric Dumazet681f1302013-06-20 05:52:22 -0700421static 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
Harout Hedeshian01555e72015-06-15 18:40:43 -0600443static 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
Laszlo Attila Totha31e1ff2009-06-09 15:16:34 +0200456static struct xt_match socket_mt_reg[] __read_mostly = {
457 {
458 .name = "socket",
459 .revision = 0,
460 .family = NFPROTO_IPV4,
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200461 .match = socket_mt4_v0,
Jan Engelhardtaa3c4872009-10-29 15:35:10 +0100462 .hooks = (1 << NF_INET_PRE_ROUTING) |
463 (1 << NF_INET_LOCAL_IN),
Laszlo Attila Totha31e1ff2009-06-09 15:16:34 +0200464 .me = THIS_MODULE,
465 },
466 {
467 .name = "socket",
468 .revision = 1,
469 .family = NFPROTO_IPV4,
Harout Hedeshian01555e72015-06-15 18:40:43 -0600470 .match = socket_mt4_v1_v2_v3,
Eric Dumazet681f1302013-06-20 05:52:22 -0700471 .checkentry = socket_mt_v1_check,
Laszlo Attila Totha31e1ff2009-06-09 15:16:34 +0200472 .matchsize = sizeof(struct xt_socket_mtinfo1),
Jan Engelhardtaa3c4872009-10-29 15:35:10 +0100473 .hooks = (1 << NF_INET_PRE_ROUTING) |
474 (1 << NF_INET_LOCAL_IN),
Laszlo Attila Totha31e1ff2009-06-09 15:16:34 +0200475 .me = THIS_MODULE,
476 },
KOVACS Krisztianf6318e52010-10-24 23:38:32 +0000477#ifdef XT_SOCKET_HAVE_IPV6
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200478 {
479 .name = "socket",
480 .revision = 1,
481 .family = NFPROTO_IPV6,
Harout Hedeshian01555e72015-06-15 18:40:43 -0600482 .match = socket_mt6_v1_v2_v3,
Eric Dumazet681f1302013-06-20 05:52:22 -0700483 .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,
Harout Hedeshian01555e72015-06-15 18:40:43 -0600494 .match = socket_mt4_v1_v2_v3,
Eric Dumazet681f1302013-06-20 05:52:22 -0700495 .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,
Harout Hedeshian01555e72015-06-15 18:40:43 -0600506 .match = socket_mt6_v1_v2_v3,
Eric Dumazet681f1302013-06-20 05:52:22 -0700507 .checkentry = socket_mt_v2_check,
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200508 .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
Harout Hedeshian01555e72015-06-15 18:40:43 -0600514 {
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
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200538};
539
540static int __init socket_mt_init(void)
541{
542 nf_defrag_ipv4_enable();
KOVACS Krisztianf6318e52010-10-24 23:38:32 +0000543#ifdef XT_SOCKET_HAVE_IPV6
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200544 nf_defrag_ipv6_enable();
545#endif
546
Laszlo Attila Totha31e1ff2009-06-09 15:16:34 +0200547 return xt_register_matches(socket_mt_reg, ARRAY_SIZE(socket_mt_reg));
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200548}
549
550static void __exit socket_mt_exit(void)
551{
Laszlo Attila Totha31e1ff2009-06-09 15:16:34 +0200552 xt_unregister_matches(socket_mt_reg, ARRAY_SIZE(socket_mt_reg));
KOVACS Krisztian136cdc72008-10-08 11:35:12 +0200553}
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");
Balazs Scheidlerb64c9252010-10-21 16:19:42 +0200562MODULE_ALIAS("ip6t_socket");