Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Transparent proxy support for Linux/iptables
0004  *
0005  * Copyright (c) 2006-2010 BalaBit IT Ltd.
0006  * Author: Balazs Scheidler, Krisztian Kovacs
0007  */
0008 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0009 #include <linux/module.h>
0010 #include <linux/skbuff.h>
0011 #include <linux/ip.h>
0012 #include <net/checksum.h>
0013 #include <net/udp.h>
0014 #include <net/tcp.h>
0015 #include <net/inet_sock.h>
0016 #include <net/inet_hashtables.h>
0017 #include <linux/inetdevice.h>
0018 #include <linux/netfilter/x_tables.h>
0019 #include <linux/netfilter_ipv4/ip_tables.h>
0020 
0021 #include <net/netfilter/ipv4/nf_defrag_ipv4.h>
0022 
0023 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
0024 #define XT_TPROXY_HAVE_IPV6 1
0025 #include <net/if_inet6.h>
0026 #include <net/addrconf.h>
0027 #include <net/inet6_hashtables.h>
0028 #include <linux/netfilter_ipv6/ip6_tables.h>
0029 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
0030 #endif
0031 
0032 #include <net/netfilter/nf_tproxy.h>
0033 #include <linux/netfilter/xt_TPROXY.h>
0034 
0035 static unsigned int
0036 tproxy_tg4(struct net *net, struct sk_buff *skb, __be32 laddr, __be16 lport,
0037        u_int32_t mark_mask, u_int32_t mark_value)
0038 {
0039     const struct iphdr *iph = ip_hdr(skb);
0040     struct udphdr _hdr, *hp;
0041     struct sock *sk;
0042 
0043     hp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_hdr), &_hdr);
0044     if (hp == NULL)
0045         return NF_DROP;
0046 
0047     /* check if there's an ongoing connection on the packet
0048      * addresses, this happens if the redirect already happened
0049      * and the current packet belongs to an already established
0050      * connection */
0051     sk = nf_tproxy_get_sock_v4(net, skb, iph->protocol,
0052                    iph->saddr, iph->daddr,
0053                    hp->source, hp->dest,
0054                    skb->dev, NF_TPROXY_LOOKUP_ESTABLISHED);
0055 
0056     laddr = nf_tproxy_laddr4(skb, laddr, iph->daddr);
0057     if (!lport)
0058         lport = hp->dest;
0059 
0060     /* UDP has no TCP_TIME_WAIT state, so we never enter here */
0061     if (sk && sk->sk_state == TCP_TIME_WAIT)
0062         /* reopening a TIME_WAIT connection needs special handling */
0063         sk = nf_tproxy_handle_time_wait4(net, skb, laddr, lport, sk);
0064     else if (!sk)
0065         /* no, there's no established connection, check if
0066          * there's a listener on the redirected addr/port */
0067         sk = nf_tproxy_get_sock_v4(net, skb, iph->protocol,
0068                        iph->saddr, laddr,
0069                        hp->source, lport,
0070                        skb->dev, NF_TPROXY_LOOKUP_LISTENER);
0071 
0072     /* NOTE: assign_sock consumes our sk reference */
0073     if (sk && nf_tproxy_sk_is_transparent(sk)) {
0074         /* This should be in a separate target, but we don't do multiple
0075            targets on the same rule yet */
0076         skb->mark = (skb->mark & ~mark_mask) ^ mark_value;
0077         nf_tproxy_assign_sock(skb, sk);
0078         return NF_ACCEPT;
0079     }
0080 
0081     return NF_DROP;
0082 }
0083 
0084 static unsigned int
0085 tproxy_tg4_v0(struct sk_buff *skb, const struct xt_action_param *par)
0086 {
0087     const struct xt_tproxy_target_info *tgi = par->targinfo;
0088 
0089     return tproxy_tg4(xt_net(par), skb, tgi->laddr, tgi->lport,
0090               tgi->mark_mask, tgi->mark_value);
0091 }
0092 
0093 static unsigned int
0094 tproxy_tg4_v1(struct sk_buff *skb, const struct xt_action_param *par)
0095 {
0096     const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
0097 
0098     return tproxy_tg4(xt_net(par), skb, tgi->laddr.ip, tgi->lport,
0099               tgi->mark_mask, tgi->mark_value);
0100 }
0101 
0102 #ifdef XT_TPROXY_HAVE_IPV6
0103 
0104 static unsigned int
0105 tproxy_tg6_v1(struct sk_buff *skb, const struct xt_action_param *par)
0106 {
0107     const struct ipv6hdr *iph = ipv6_hdr(skb);
0108     const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
0109     struct udphdr _hdr, *hp;
0110     struct sock *sk;
0111     const struct in6_addr *laddr;
0112     __be16 lport;
0113     int thoff = 0;
0114     int tproto;
0115 
0116     tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL);
0117     if (tproto < 0)
0118         return NF_DROP;
0119 
0120     hp = skb_header_pointer(skb, thoff, sizeof(_hdr), &_hdr);
0121     if (!hp)
0122         return NF_DROP;
0123 
0124     /* check if there's an ongoing connection on the packet
0125      * addresses, this happens if the redirect already happened
0126      * and the current packet belongs to an already established
0127      * connection */
0128     sk = nf_tproxy_get_sock_v6(xt_net(par), skb, thoff, tproto,
0129                    &iph->saddr, &iph->daddr,
0130                    hp->source, hp->dest,
0131                    xt_in(par), NF_TPROXY_LOOKUP_ESTABLISHED);
0132 
0133     laddr = nf_tproxy_laddr6(skb, &tgi->laddr.in6, &iph->daddr);
0134     lport = tgi->lport ? tgi->lport : hp->dest;
0135 
0136     /* UDP has no TCP_TIME_WAIT state, so we never enter here */
0137     if (sk && sk->sk_state == TCP_TIME_WAIT) {
0138         const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
0139         /* reopening a TIME_WAIT connection needs special handling */
0140         sk = nf_tproxy_handle_time_wait6(skb, tproto, thoff,
0141                           xt_net(par),
0142                           &tgi->laddr.in6,
0143                           tgi->lport,
0144                           sk);
0145     }
0146     else if (!sk)
0147         /* no there's no established connection, check if
0148          * there's a listener on the redirected addr/port */
0149         sk = nf_tproxy_get_sock_v6(xt_net(par), skb, thoff,
0150                        tproto, &iph->saddr, laddr,
0151                        hp->source, lport,
0152                        xt_in(par), NF_TPROXY_LOOKUP_LISTENER);
0153 
0154     /* NOTE: assign_sock consumes our sk reference */
0155     if (sk && nf_tproxy_sk_is_transparent(sk)) {
0156         /* This should be in a separate target, but we don't do multiple
0157            targets on the same rule yet */
0158         skb->mark = (skb->mark & ~tgi->mark_mask) ^ tgi->mark_value;
0159         nf_tproxy_assign_sock(skb, sk);
0160         return NF_ACCEPT;
0161     }
0162 
0163     return NF_DROP;
0164 }
0165 
0166 static int tproxy_tg6_check(const struct xt_tgchk_param *par)
0167 {
0168     const struct ip6t_ip6 *i = par->entryinfo;
0169     int err;
0170 
0171     err = nf_defrag_ipv6_enable(par->net);
0172     if (err)
0173         return err;
0174 
0175     if ((i->proto == IPPROTO_TCP || i->proto == IPPROTO_UDP) &&
0176         !(i->invflags & IP6T_INV_PROTO))
0177         return 0;
0178 
0179     pr_info_ratelimited("Can be used only with -p tcp or -p udp\n");
0180     return -EINVAL;
0181 }
0182 
0183 static void tproxy_tg6_destroy(const struct xt_tgdtor_param *par)
0184 {
0185     nf_defrag_ipv6_disable(par->net);
0186 }
0187 #endif
0188 
0189 static int tproxy_tg4_check(const struct xt_tgchk_param *par)
0190 {
0191     const struct ipt_ip *i = par->entryinfo;
0192     int err;
0193 
0194     err = nf_defrag_ipv4_enable(par->net);
0195     if (err)
0196         return err;
0197 
0198     if ((i->proto == IPPROTO_TCP || i->proto == IPPROTO_UDP)
0199         && !(i->invflags & IPT_INV_PROTO))
0200         return 0;
0201 
0202     pr_info_ratelimited("Can be used only with -p tcp or -p udp\n");
0203     return -EINVAL;
0204 }
0205 
0206 static void tproxy_tg4_destroy(const struct xt_tgdtor_param *par)
0207 {
0208     nf_defrag_ipv4_disable(par->net);
0209 }
0210 
0211 static struct xt_target tproxy_tg_reg[] __read_mostly = {
0212     {
0213         .name       = "TPROXY",
0214         .family     = NFPROTO_IPV4,
0215         .table      = "mangle",
0216         .target     = tproxy_tg4_v0,
0217         .revision   = 0,
0218         .targetsize = sizeof(struct xt_tproxy_target_info),
0219         .checkentry = tproxy_tg4_check,
0220         .destroy    = tproxy_tg4_destroy,
0221         .hooks      = 1 << NF_INET_PRE_ROUTING,
0222         .me     = THIS_MODULE,
0223     },
0224     {
0225         .name       = "TPROXY",
0226         .family     = NFPROTO_IPV4,
0227         .table      = "mangle",
0228         .target     = tproxy_tg4_v1,
0229         .revision   = 1,
0230         .targetsize = sizeof(struct xt_tproxy_target_info_v1),
0231         .checkentry = tproxy_tg4_check,
0232         .destroy    = tproxy_tg4_destroy,
0233         .hooks      = 1 << NF_INET_PRE_ROUTING,
0234         .me     = THIS_MODULE,
0235     },
0236 #ifdef XT_TPROXY_HAVE_IPV6
0237     {
0238         .name       = "TPROXY",
0239         .family     = NFPROTO_IPV6,
0240         .table      = "mangle",
0241         .target     = tproxy_tg6_v1,
0242         .revision   = 1,
0243         .targetsize = sizeof(struct xt_tproxy_target_info_v1),
0244         .checkentry = tproxy_tg6_check,
0245         .destroy    = tproxy_tg6_destroy,
0246         .hooks      = 1 << NF_INET_PRE_ROUTING,
0247         .me     = THIS_MODULE,
0248     },
0249 #endif
0250 
0251 };
0252 
0253 static int __init tproxy_tg_init(void)
0254 {
0255     return xt_register_targets(tproxy_tg_reg, ARRAY_SIZE(tproxy_tg_reg));
0256 }
0257 
0258 static void __exit tproxy_tg_exit(void)
0259 {
0260     xt_unregister_targets(tproxy_tg_reg, ARRAY_SIZE(tproxy_tg_reg));
0261 }
0262 
0263 module_init(tproxy_tg_init);
0264 module_exit(tproxy_tg_exit);
0265 MODULE_LICENSE("GPL");
0266 MODULE_AUTHOR("Balazs Scheidler, Krisztian Kovacs");
0267 MODULE_DESCRIPTION("Netfilter transparent proxy (TPROXY) target module.");
0268 MODULE_ALIAS("ipt_TPROXY");
0269 MODULE_ALIAS("ip6t_TPROXY");