]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/netfilter/xt_socket.c
ipvs: ip_vs_sh: ip_vs_sh_get_port: check skb_header_pointer for NULL
[karo-tx-linux.git] / net / netfilter / xt_socket.c
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
38 static void
39 xt_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
47 static int
48 extract_icmp4_fields(const struct sk_buff *skb,
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
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  */
123 static struct sock *
124 xt_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
141 static bool
142 socket_match(const struct sk_buff *skb, struct xt_action_param *par,
143              const struct xt_socket_mtinfo1 *info)
144 {
145         const struct iphdr *iph = ip_hdr(skb);
146         struct udphdr _hdr, *hp = NULL;
147         struct sock *sk = skb->sk;
148         __be32 uninitialized_var(daddr), uninitialized_var(saddr);
149         __be16 uninitialized_var(dport), uninitialized_var(sport);
150         u8 uninitialized_var(protocol);
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) {
169                 if (extract_icmp4_fields(skb, &protocol, &saddr, &daddr,
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);
181         if (ct && !nf_ct_is_untracked(ct) &&
182             ((iph->protocol != IPPROTO_ICMP &&
183               ctinfo == IP_CT_ESTABLISHED_REPLY) ||
184              (iph->protocol == IPPROTO_ICMP &&
185               ctinfo == IP_CT_RELATED_REPLY)) &&
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
195         if (!sk)
196                 sk = xt_socket_get_sock_v4(dev_net(skb->dev), protocol,
197                                            saddr, daddr, sport, dport,
198                                            par->in);
199         if (sk) {
200                 bool wildcard;
201                 bool transparent = true;
202
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 &&
208                             inet_sk(sk)->inet_rcv_saddr == 0);
209
210                 /* Ignore non-transparent sockets,
211                    if XT_SOCKET_TRANSPARENT is used */
212                 if (info && info->flags & XT_SOCKET_TRANSPARENT)
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));
217
218                 if (sk != skb->sk)
219                         xt_socket_put_sk(sk);
220
221                 if (wildcard || !transparent)
222                         sk = NULL;
223         }
224
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);
229
230         return (sk != NULL);
231 }
232
233 static bool
234 socket_mt4_v0(const struct sk_buff *skb, struct xt_action_param *par)
235 {
236         return socket_match(skb, par, NULL);
237 }
238
239 static bool
240 socket_mt4_v1_v2(const struct sk_buff *skb, struct xt_action_param *par)
241 {
242         return socket_match(skb, par, par->matchinfo);
243 }
244
245 #ifdef XT_SOCKET_HAVE_IPV6
246
247 static int
248 extract_icmp6_fields(const struct sk_buff *skb,
249                      unsigned int outside_hdrlen,
250                      int *protocol,
251                      struct in6_addr **raddr,
252                      struct in6_addr **laddr,
253                      __be16 *rport,
254                      __be16 *lport)
255 {
256         struct ipv6hdr *inside_iph, _inside_iph;
257         struct icmp6hdr *icmph, _icmph;
258         __be16 *ports, _ports[2];
259         u8 inside_nexthdr;
260         __be16 inside_fragoff;
261         int inside_hdrlen;
262
263         icmph = skb_header_pointer(skb, outside_hdrlen,
264                                    sizeof(_icmph), &_icmph);
265         if (icmph == NULL)
266                 return 1;
267
268         if (icmph->icmp6_type & ICMPV6_INFOMSG_MASK)
269                 return 1;
270
271         inside_iph = skb_header_pointer(skb, outside_hdrlen + sizeof(_icmph), sizeof(_inside_iph), &_inside_iph);
272         if (inside_iph == NULL)
273                 return 1;
274         inside_nexthdr = inside_iph->nexthdr;
275
276         inside_hdrlen = ipv6_skip_exthdr(skb, outside_hdrlen + sizeof(_icmph) + sizeof(_inside_iph),
277                                          &inside_nexthdr, &inside_fragoff);
278         if (inside_hdrlen < 0)
279                 return 1; /* hjm: Packet has no/incomplete transport layer headers. */
280
281         if (inside_nexthdr != IPPROTO_TCP &&
282             inside_nexthdr != IPPROTO_UDP)
283                 return 1;
284
285         ports = skb_header_pointer(skb, inside_hdrlen,
286                                    sizeof(_ports), &_ports);
287         if (ports == NULL)
288                 return 1;
289
290         /* the inside IP packet is the one quoted from our side, thus
291          * its saddr is the local address */
292         *protocol = inside_nexthdr;
293         *laddr = &inside_iph->saddr;
294         *lport = ports[0];
295         *raddr = &inside_iph->daddr;
296         *rport = ports[1];
297
298         return 0;
299 }
300
301 static struct sock *
302 xt_socket_get_sock_v6(struct net *net, const u8 protocol,
303                       const struct in6_addr *saddr, const struct in6_addr *daddr,
304                       const __be16 sport, const __be16 dport,
305                       const struct net_device *in)
306 {
307         switch (protocol) {
308         case IPPROTO_TCP:
309                 return inet6_lookup(net, &tcp_hashinfo,
310                                     saddr, sport, daddr, dport,
311                                     in->ifindex);
312         case IPPROTO_UDP:
313                 return udp6_lib_lookup(net, saddr, sport, daddr, dport,
314                                        in->ifindex);
315         }
316
317         return NULL;
318 }
319
320 static bool
321 socket_mt6_v1_v2(const struct sk_buff *skb, struct xt_action_param *par)
322 {
323         struct ipv6hdr *iph = ipv6_hdr(skb);
324         struct udphdr _hdr, *hp = NULL;
325         struct sock *sk = skb->sk;
326         struct in6_addr *daddr = NULL, *saddr = NULL;
327         __be16 uninitialized_var(dport), uninitialized_var(sport);
328         int thoff = 0, uninitialized_var(tproto);
329         const struct xt_socket_mtinfo1 *info = (struct xt_socket_mtinfo1 *) par->matchinfo;
330
331         tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL);
332         if (tproto < 0) {
333                 pr_debug("unable to find transport header in IPv6 packet, dropping\n");
334                 return NF_DROP;
335         }
336
337         if (tproto == IPPROTO_UDP || tproto == IPPROTO_TCP) {
338                 hp = skb_header_pointer(skb, thoff,
339                                         sizeof(_hdr), &_hdr);
340                 if (hp == NULL)
341                         return false;
342
343                 saddr = &iph->saddr;
344                 sport = hp->source;
345                 daddr = &iph->daddr;
346                 dport = hp->dest;
347
348         } else if (tproto == IPPROTO_ICMPV6) {
349                 if (extract_icmp6_fields(skb, thoff, &tproto, &saddr, &daddr,
350                                          &sport, &dport))
351                         return false;
352         } else {
353                 return false;
354         }
355
356         if (!sk)
357                 sk = xt_socket_get_sock_v6(dev_net(skb->dev), tproto,
358                                            saddr, daddr, sport, dport,
359                                            par->in);
360         if (sk) {
361                 bool wildcard;
362                 bool transparent = true;
363
364                 /* Ignore sockets listening on INADDR_ANY
365                  * unless XT_SOCKET_NOWILDCARD is set
366                  */
367                 wildcard = (!(info->flags & XT_SOCKET_NOWILDCARD) &&
368                             sk->sk_state != TCP_TIME_WAIT &&
369                             ipv6_addr_any(&inet6_sk(sk)->rcv_saddr));
370
371                 /* Ignore non-transparent sockets,
372                    if XT_SOCKET_TRANSPARENT is used */
373                 if (info && info->flags & XT_SOCKET_TRANSPARENT)
374                         transparent = ((sk->sk_state != TCP_TIME_WAIT &&
375                                         inet_sk(sk)->transparent) ||
376                                        (sk->sk_state == TCP_TIME_WAIT &&
377                                         inet_twsk(sk)->tw_transparent));
378
379                 if (sk != skb->sk)
380                         xt_socket_put_sk(sk);
381
382                 if (wildcard || !transparent)
383                         sk = NULL;
384         }
385
386         pr_debug("proto %hhd %pI6:%hu -> %pI6:%hu "
387                  "(orig %pI6:%hu) sock %p\n",
388                  tproto, saddr, ntohs(sport),
389                  daddr, ntohs(dport),
390                  &iph->daddr, hp ? ntohs(hp->dest) : 0, sk);
391
392         return (sk != NULL);
393 }
394 #endif
395
396 static int socket_mt_v1_check(const struct xt_mtchk_param *par)
397 {
398         const struct xt_socket_mtinfo1 *info = (struct xt_socket_mtinfo1 *) par->matchinfo;
399
400         if (info->flags & ~XT_SOCKET_FLAGS_V1) {
401                 pr_info("unknown flags 0x%x\n", info->flags & ~XT_SOCKET_FLAGS_V1);
402                 return -EINVAL;
403         }
404         return 0;
405 }
406
407 static int socket_mt_v2_check(const struct xt_mtchk_param *par)
408 {
409         const struct xt_socket_mtinfo2 *info = (struct xt_socket_mtinfo2 *) par->matchinfo;
410
411         if (info->flags & ~XT_SOCKET_FLAGS_V2) {
412                 pr_info("unknown flags 0x%x\n", info->flags & ~XT_SOCKET_FLAGS_V2);
413                 return -EINVAL;
414         }
415         return 0;
416 }
417
418 static struct xt_match socket_mt_reg[] __read_mostly = {
419         {
420                 .name           = "socket",
421                 .revision       = 0,
422                 .family         = NFPROTO_IPV4,
423                 .match          = socket_mt4_v0,
424                 .hooks          = (1 << NF_INET_PRE_ROUTING) |
425                                   (1 << NF_INET_LOCAL_IN),
426                 .me             = THIS_MODULE,
427         },
428         {
429                 .name           = "socket",
430                 .revision       = 1,
431                 .family         = NFPROTO_IPV4,
432                 .match          = socket_mt4_v1_v2,
433                 .checkentry     = socket_mt_v1_check,
434                 .matchsize      = sizeof(struct xt_socket_mtinfo1),
435                 .hooks          = (1 << NF_INET_PRE_ROUTING) |
436                                   (1 << NF_INET_LOCAL_IN),
437                 .me             = THIS_MODULE,
438         },
439 #ifdef XT_SOCKET_HAVE_IPV6
440         {
441                 .name           = "socket",
442                 .revision       = 1,
443                 .family         = NFPROTO_IPV6,
444                 .match          = socket_mt6_v1_v2,
445                 .checkentry     = socket_mt_v1_check,
446                 .matchsize      = sizeof(struct xt_socket_mtinfo1),
447                 .hooks          = (1 << NF_INET_PRE_ROUTING) |
448                                   (1 << NF_INET_LOCAL_IN),
449                 .me             = THIS_MODULE,
450         },
451 #endif
452         {
453                 .name           = "socket",
454                 .revision       = 2,
455                 .family         = NFPROTO_IPV4,
456                 .match          = socket_mt4_v1_v2,
457                 .checkentry     = socket_mt_v2_check,
458                 .matchsize      = sizeof(struct xt_socket_mtinfo1),
459                 .hooks          = (1 << NF_INET_PRE_ROUTING) |
460                                   (1 << NF_INET_LOCAL_IN),
461                 .me             = THIS_MODULE,
462         },
463 #ifdef XT_SOCKET_HAVE_IPV6
464         {
465                 .name           = "socket",
466                 .revision       = 2,
467                 .family         = NFPROTO_IPV6,
468                 .match          = socket_mt6_v1_v2,
469                 .checkentry     = socket_mt_v2_check,
470                 .matchsize      = sizeof(struct xt_socket_mtinfo1),
471                 .hooks          = (1 << NF_INET_PRE_ROUTING) |
472                                   (1 << NF_INET_LOCAL_IN),
473                 .me             = THIS_MODULE,
474         },
475 #endif
476 };
477
478 static int __init socket_mt_init(void)
479 {
480         nf_defrag_ipv4_enable();
481 #ifdef XT_SOCKET_HAVE_IPV6
482         nf_defrag_ipv6_enable();
483 #endif
484
485         return xt_register_matches(socket_mt_reg, ARRAY_SIZE(socket_mt_reg));
486 }
487
488 static void __exit socket_mt_exit(void)
489 {
490         xt_unregister_matches(socket_mt_reg, ARRAY_SIZE(socket_mt_reg));
491 }
492
493 module_init(socket_mt_init);
494 module_exit(socket_mt_exit);
495
496 MODULE_LICENSE("GPL");
497 MODULE_AUTHOR("Krisztian Kovacs, Balazs Scheidler");
498 MODULE_DESCRIPTION("x_tables socket match module");
499 MODULE_ALIAS("ipt_socket");
500 MODULE_ALIAS("ip6t_socket");