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