| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* |
| 3 |
* ip_vs_nfct.c: Netfilter connection tracking support for IPVS |
| 4 |
* |
| 5 |
* Portions Copyright (C) 2001-2002 |
| 6 |
* Antefacto Ltd, 181 Parnell St, Dublin 1, Ireland. |
| 7 |
* |
| 8 |
* Portions Copyright (C) 2003-2010 |
| 9 |
* Julian Anastasov |
| 10 |
* |
| 11 |
* Authors: |
| 12 |
* Ben North <[email protected]> |
| 13 |
* Julian Anastasov <[email protected]> Reorganize and sync with latest kernels |
| 14 |
* Hannes Eder <[email protected]> Extend NFCT support for FTP, ipvs match |
| 15 |
* |
| 16 |
* Current status: |
| 17 |
* |
| 18 |
* - provide conntrack confirmation for new and related connections, by |
| 19 |
* this way we can see their proper conntrack state in all hooks |
| 20 |
* - support for all forwarding methods, not only NAT |
| 21 |
* - FTP support (NAT), ability to support other NAT apps with expectations |
| 22 |
* - to correctly create expectations for related NAT connections the proper |
| 23 |
* NF conntrack support must be already installed, eg. ip_vs_ftp requires |
| 24 |
* nf_conntrack_ftp ... iptables_nat for the same ports (but no iptables |
| 25 |
* NAT rules are needed) |
| 26 |
* - alter reply for NAT when forwarding packet in original direction: |
| 27 |
* conntrack from client in NEW or RELATED (Passive FTP DATA) state or |
| 28 |
* when RELATED conntrack is created from real server (Active FTP DATA) |
| 29 |
* - if iptables_nat is not loaded the Passive FTP will not work (the |
| 30 |
* PASV response can not be NAT-ed) but Active FTP should work |
| 31 |
*/ |
| 32 |
|
| 33 |
#define pr_fmt(fmt) "IPVS: " fmt |
| 34 |
|
| 35 |
#include <linux/module.h> |
| 36 |
#include <linux/types.h> |
| 37 |
#include <linux/kernel.h> |
| 38 |
#include <linux/errno.h> |
| 39 |
#include <linux/compiler.h> |
| 40 |
#include <linux/vmalloc.h> |
| 41 |
#include <linux/skbuff.h> |
| 42 |
#include <net/ip.h> |
| 43 |
#include <linux/netfilter.h> |
| 44 |
#include <linux/netfilter_ipv4.h> |
| 45 |
#include <net/ip_vs.h> |
| 46 |
#include <net/netfilter/nf_conntrack_core.h> |
| 47 |
#include <net/netfilter/nf_conntrack_expect.h> |
| 48 |
#include <net/netfilter/nf_conntrack_seqadj.h> |
| 49 |
#include <net/netfilter/nf_conntrack_helper.h> |
| 50 |
#include <net/netfilter/nf_conntrack_zones.h> |
| 51 |
|
| 52 |
|
| 53 |
#define FMT_TUPLE "%s:%u->%s:%u/%u" |
| 54 |
#define ARG_TUPLE(T) IP_VS_DBG_ADDR((T)->src.l3num, &(T)->src.u3), \ |
| 55 |
ntohs((T)->src.u.all), \ |
| 56 |
IP_VS_DBG_ADDR((T)->src.l3num, &(T)->dst.u3), \ |
| 57 |
ntohs((T)->dst.u.all), \ |
| 58 |
(T)->dst.protonum |
| 59 |
|
| 60 |
#define FMT_CONN "%s:%u->%s:%u->%s:%u/%u:%u" |
| 61 |
#define ARG_CONN(C) IP_VS_DBG_ADDR((C)->af, &((C)->caddr)), \ |
| 62 |
ntohs((C)->cport), \ |
| 63 |
IP_VS_DBG_ADDR((C)->af, &((C)->vaddr)), \ |
| 64 |
ntohs((C)->vport), \ |
| 65 |
IP_VS_DBG_ADDR((C)->daf, &((C)->daddr)), \ |
| 66 |
ntohs((C)->dport), \ |
| 67 |
(C)->protocol, (C)->state |
| 68 |
|
| 69 |
void |
| 70 |
ip_vs_update_conntrack(struct sk_buff *skb, struct ip_vs_conn *cp, int outin) |
| 71 |
{ |
| 72 |
enum ip_conntrack_info ctinfo; |
| 73 |
struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
| 74 |
struct nf_conntrack_tuple new_tuple; |
| 75 |
|
| 76 |
if (ct == NULL || nf_ct_is_confirmed(ct) || |
| 77 |
nf_ct_is_dying(ct)) |
| 78 |
return; |
| 79 |
|
| 80 |
/* Never alter conntrack for non-NAT conns */ |
| 81 |
if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) |
| 82 |
return; |
| 83 |
|
| 84 |
/* Never alter conntrack for OPS conns (no reply is expected) */ |
| 85 |
if (cp->flags & IP_VS_CONN_F_ONE_PACKET) |
| 86 |
return; |
| 87 |
|
| 88 |
/* Alter reply only in original direction */ |
| 89 |
if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) |
| 90 |
return; |
| 91 |
|
| 92 |
/* Applications may adjust TCP seqs */ |
| 93 |
if (cp->app && nf_ct_protonum(ct) == IPPROTO_TCP && |
| 94 |
!nfct_seqadj(ct) && !nfct_seqadj_ext_add(ct)) |
| 95 |
return; |
| 96 |
|
| 97 |
/* |
| 98 |
* The connection is not yet in the hashtable, so we update it. |
| 99 |
* CIP->VIP will remain the same, so leave the tuple in |
| 100 |
* IP_CT_DIR_ORIGINAL untouched. When the reply comes back from the |
| 101 |
* real-server we will see RIP->DIP. |
| 102 |
*/ |
| 103 |
new_tuple = ct->tuplehash[IP_CT_DIR_REPLY].tuple; |
| 104 |
/* |
| 105 |
* This will also take care of UDP and other protocols. |
| 106 |
*/ |
| 107 |
if (outin) { |
| 108 |
new_tuple.src.u3 = cp->daddr; |
| 109 |
if (new_tuple.dst.protonum != IPPROTO_ICMP && |
| 110 |
new_tuple.dst.protonum != IPPROTO_ICMPV6) |
| 111 |
new_tuple.src.u.tcp.port = cp->dport; |
| 112 |
} else { |
| 113 |
new_tuple.dst.u3 = cp->vaddr; |
| 114 |
if (new_tuple.dst.protonum != IPPROTO_ICMP && |
| 115 |
new_tuple.dst.protonum != IPPROTO_ICMPV6) |
| 116 |
new_tuple.dst.u.tcp.port = cp->vport; |
| 117 |
} |
| 118 |
IP_VS_DBG_BUF(7, "%s: Updating conntrack ct=%p, status=0x%lX, " |
| 119 |
"ctinfo=%d, old reply=" FMT_TUPLE "\n", |
| 120 |
__func__, ct, ct->status, ctinfo, |
| 121 |
ARG_TUPLE(&ct->tuplehash[IP_CT_DIR_REPLY].tuple)); |
| 122 |
IP_VS_DBG_BUF(7, "%s: Updating conntrack ct=%p, status=0x%lX, " |
| 123 |
"ctinfo=%d, new reply=" FMT_TUPLE "\n", |
| 124 |
__func__, ct, ct->status, ctinfo, |
| 125 |
ARG_TUPLE(&new_tuple)); |
| 126 |
nf_conntrack_alter_reply(ct, &new_tuple); |
| 127 |
IP_VS_DBG_BUF(7, "%s: Updated conntrack ct=%p for cp=" FMT_CONN "\n", |
| 128 |
__func__, ct, ARG_CONN(cp)); |
| 129 |
} |
| 130 |
|
| 131 |
int ip_vs_confirm_conntrack(struct sk_buff *skb) |
| 132 |
{ |
| 133 |
return nf_conntrack_confirm(skb); |
| 134 |
} |
| 135 |
|
| 136 |
/* |
| 137 |
* Called from init_conntrack() as expectfn handler. |
| 138 |
*/ |
| 139 |
static void ip_vs_nfct_expect_callback(struct nf_conn *ct, |
| 140 |
struct nf_conntrack_expect *exp) |
| 141 |
{ |
| 142 |
struct nf_conntrack_tuple *orig, new_reply; |
| 143 |
struct ip_vs_conn *cp; |
| 144 |
struct ip_vs_conn_param p; |
| 145 |
struct net *net = nf_ct_net(ct); |
| 146 |
|
| 147 |
/* |
| 148 |
* We assume that no NF locks are held before this callback. |
| 149 |
* ip_vs_conn_out_get and ip_vs_conn_in_get should match their |
| 150 |
* expectations even if they use wildcard values, now we provide the |
| 151 |
* actual values from the newly created original conntrack direction. |
| 152 |
* The conntrack is confirmed when packet reaches IPVS hooks. |
| 153 |
*/ |
| 154 |
|
| 155 |
/* RS->CLIENT */ |
| 156 |
orig = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple; |
| 157 |
ip_vs_conn_fill_param(net_ipvs(net), exp->tuple.src.l3num, orig->dst.protonum, |
| 158 |
&orig->src.u3, orig->src.u.tcp.port, |
| 159 |
&orig->dst.u3, orig->dst.u.tcp.port, &p); |
| 160 |
cp = ip_vs_conn_out_get(&p); |
| 161 |
if (cp) { |
| 162 |
/* Change reply CLIENT->RS to CLIENT->VS */ |
| 163 |
IP_VS_DBG_BUF(7, "%s: for ct=%p, status=0x%lX found inout cp=" |
| 164 |
FMT_CONN "\n", |
| 165 |
__func__, ct, ct->status, ARG_CONN(cp)); |
| 166 |
new_reply = ct->tuplehash[IP_CT_DIR_REPLY].tuple; |
| 167 |
IP_VS_DBG_BUF(7, "%s: ct=%p before alter: reply tuple=" |
| 168 |
FMT_TUPLE "\n", |
| 169 |
__func__, ct, ARG_TUPLE(&new_reply)); |
| 170 |
new_reply.dst.u3 = cp->vaddr; |
| 171 |
new_reply.dst.u.tcp.port = cp->vport; |
| 172 |
goto alter; |
| 173 |
} |
| 174 |
|
| 175 |
/* CLIENT->VS */ |
| 176 |
cp = ip_vs_conn_in_get(&p); |
| 177 |
if (cp) { |
| 178 |
/* Change reply VS->CLIENT to RS->CLIENT */ |
| 179 |
IP_VS_DBG_BUF(7, "%s: for ct=%p, status=0x%lX found outin cp=" |
| 180 |
FMT_CONN "\n", |
| 181 |
__func__, ct, ct->status, ARG_CONN(cp)); |
| 182 |
new_reply = ct->tuplehash[IP_CT_DIR_REPLY].tuple; |
| 183 |
IP_VS_DBG_BUF(7, "%s: ct=%p before alter: reply tuple=" |
| 184 |
FMT_TUPLE "\n", |
| 185 |
__func__, ct, ARG_TUPLE(&new_reply)); |
| 186 |
new_reply.src.u3 = cp->daddr; |
| 187 |
new_reply.src.u.tcp.port = cp->dport; |
| 188 |
goto alter; |
| 189 |
} |
| 190 |
|
| 191 |
IP_VS_DBG_BUF(7, "%s: ct=%p, status=0x%lX, tuple=" FMT_TUPLE |
| 192 |
" - unknown expect\n", |
| 193 |
__func__, ct, ct->status, ARG_TUPLE(orig)); |
| 194 |
return; |
| 195 |
|
| 196 |
alter: |
| 197 |
/* Never alter conntrack for non-NAT conns */ |
| 198 |
if (IP_VS_FWD_METHOD(cp) == IP_VS_CONN_F_MASQ) |
| 199 |
nf_conntrack_alter_reply(ct, &new_reply); |
| 200 |
ip_vs_conn_put(cp); |
| 201 |
return; |
| 202 |
} |
| 203 |
|
| 204 |
/* |
| 205 |
* Create NF conntrack expectation with wildcard (optional) source port. |
| 206 |
* Then the default callback function will alter the reply and will confirm |
| 207 |
* the conntrack entry when the first packet comes. |
| 208 |
* Use port 0 to expect connection from any port. |
| 209 |
*/ |
| 210 |
void ip_vs_nfct_expect_related(struct sk_buff *skb, struct nf_conn *ct, |
| 211 |
struct ip_vs_conn *cp, u_int8_t proto, |
| 212 |
const __be16 port, int from_rs) |
| 213 |
{ |
| 214 |
struct nf_conntrack_expect *exp; |
| 215 |
|
| 216 |
if (ct == NULL) |
| 217 |
return; |
| 218 |
|
| 219 |
exp = nf_ct_expect_alloc(ct); |
| 220 |
if (!exp) |
| 221 |
return; |
| 222 |
|
| 223 |
nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct), |
| 224 |
from_rs ? &cp->daddr : &cp->caddr, |
| 225 |
from_rs ? &cp->caddr : &cp->vaddr, |
| 226 |
proto, port ? &port : NULL, |
| 227 |
from_rs ? &cp->cport : &cp->vport); |
| 228 |
|
| 229 |
exp->expectfn = ip_vs_nfct_expect_callback; |
| 230 |
|
| 231 |
IP_VS_DBG_BUF(7, "%s: ct=%p, expect tuple=" FMT_TUPLE "\n", |
| 232 |
__func__, ct, ARG_TUPLE(&exp->tuple)); |
| 233 |
nf_ct_expect_related(exp, 0); |
| 234 |
nf_ct_expect_put(exp); |
| 235 |
} |
| 236 |
EXPORT_SYMBOL(ip_vs_nfct_expect_related); |
| 237 |
|
| 238 |
/* |
| 239 |
* Our connection was terminated, try to drop the conntrack immediately |
| 240 |
*/ |
| 241 |
void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp) |
| 242 |
{ |
| 243 |
struct nf_conntrack_tuple_hash *h; |
| 244 |
struct nf_conn *ct; |
| 245 |
struct nf_conntrack_tuple tuple; |
| 246 |
|
| 247 |
if (!cp->cport) |
| 248 |
return; |
| 249 |
|
| 250 |
tuple = (struct nf_conntrack_tuple) { |
| 251 |
.dst = { .protonum = cp->protocol, .dir = IP_CT_DIR_ORIGINAL } }; |
| 252 |
tuple.src.u3 = cp->caddr; |
| 253 |
tuple.src.u.all = cp->cport; |
| 254 |
tuple.src.l3num = cp->af; |
| 255 |
tuple.dst.u3 = cp->vaddr; |
| 256 |
tuple.dst.u.all = cp->vport; |
| 257 |
|
| 258 |
IP_VS_DBG_BUF(7, "%s: dropping conntrack for conn " FMT_CONN "\n", |
| 259 |
__func__, ARG_CONN(cp)); |
| 260 |
|
| 261 |
h = nf_conntrack_find_get(cp->ipvs->net, &nf_ct_zone_dflt, &tuple); |
| 262 |
if (h) { |
| 263 |
ct = nf_ct_tuplehash_to_ctrack(h); |
| 264 |
if (nf_ct_kill(ct)) { |
| 265 |
IP_VS_DBG_BUF(7, "%s: ct=%p deleted for tuple=" |
| 266 |
FMT_TUPLE "\n", |
| 267 |
__func__, ct, ARG_TUPLE(&tuple)); |
| 268 |
} else { |
| 269 |
IP_VS_DBG_BUF(7, "%s: ct=%p, no conntrack for tuple=" |
| 270 |
FMT_TUPLE "\n", |
| 271 |
__func__, ct, ARG_TUPLE(&tuple)); |
| 272 |
} |
| 273 |
nf_ct_put(ct); |
| 274 |
} else { |
| 275 |
IP_VS_DBG_BUF(7, "%s: no conntrack for tuple=" FMT_TUPLE "\n", |
| 276 |
__func__, ARG_TUPLE(&tuple)); |
| 277 |
} |
| 278 |
} |
| 279 |
|