Merge tag 'v3.10.91' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / netfilter / ipvs / ip_vs_sync.c
CommitLineData
1da177e4
LT
1/*
2 * IPVS An implementation of the IP virtual server support for the
3 * LINUX operating system. IPVS is now implemented as a module
4 * over the NetFilter framework. IPVS can be used to build a
5 * high-performance and highly available server based on a
6 * cluster of servers.
7 *
b880c1f0
HS
8 * Version 1, is capable of handling both version 0 and 1 messages.
9 * Version 0 is the plain old format.
10 * Note Version 0 receivers will just drop Ver 1 messages.
11 * Version 1 is capable of handle IPv6, Persistence data,
12 * time-outs, and firewall marks.
13 * In ver.1 "ip_vs_sync_conn_options" will be sent in netw. order.
14 * Ver. 0 can be turned on by sysctl -w net.ipv4.vs.sync_version=0
15 *
16 * Definitions Message: is a complete datagram
17 * Sync_conn: is a part of a Message
18 * Param Data is an option to a Sync_conn.
19 *
1da177e4
LT
20 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
21 *
22 * ip_vs_sync: sync connection info from master load balancer to backups
23 * through multicast
24 *
25 * Changes:
26 * Alexandre Cassen : Added master & backup support at a time.
27 * Alexandre Cassen : Added SyncID support for incoming sync
28 * messages filtering.
29 * Justin Ossevoort : Fix endian problem on sync message size.
b880c1f0
HS
30 * Hans Schillstrom : Added Version 1: i.e. IPv6,
31 * Persistence support, fwmark and time-out.
1da177e4
LT
32 */
33
9aada7ac
HE
34#define KMSG_COMPONENT "IPVS"
35#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
36
1da177e4
LT
37#include <linux/module.h>
38#include <linux/slab.h>
14c85021 39#include <linux/inetdevice.h>
1da177e4
LT
40#include <linux/net.h>
41#include <linux/completion.h>
42#include <linux/delay.h>
43#include <linux/skbuff.h>
44#include <linux/in.h>
45#include <linux/igmp.h> /* for ip_mc_join_group */
14c85021 46#include <linux/udp.h>
e6dd731c 47#include <linux/err.h>
998e7a76 48#include <linux/kthread.h>
ba6fd850 49#include <linux/wait.h>
e6f225eb 50#include <linux/kernel.h>
1da177e4 51
fe5e7a1e
HS
52#include <asm/unaligned.h> /* Used for ntoh_seq and hton_seq */
53
1da177e4
LT
54#include <net/ip.h>
55#include <net/sock.h>
1da177e4
LT
56
57#include <net/ip_vs.h>
58
59#define IP_VS_SYNC_GROUP 0xe0000051 /* multicast addr - 224.0.0.81 */
60#define IP_VS_SYNC_PORT 8848 /* multicast port */
61
2981bc9a 62#define SYNC_PROTO_VER 1 /* Protocol version in header */
1da177e4 63
ae1d48b2 64static struct lock_class_key __ipvs_sync_key;
1da177e4
LT
65/*
66 * IPVS sync connection entry
2981bc9a 67 * Version 0, i.e. original version.
1da177e4 68 */
2981bc9a 69struct ip_vs_sync_conn_v0 {
1da177e4
LT
70 __u8 reserved;
71
72 /* Protocol, addresses and port numbers */
73 __u8 protocol; /* Which protocol (TCP/UDP) */
014d730d
AV
74 __be16 cport;
75 __be16 vport;
76 __be16 dport;
77 __be32 caddr; /* client address */
78 __be32 vaddr; /* virtual address */
79 __be32 daddr; /* destination address */
1da177e4
LT
80
81 /* Flags and state transition */
014d730d
AV
82 __be16 flags; /* status flags */
83 __be16 state; /* state info */
1da177e4
LT
84
85 /* The sequence options start here */
86};
87
88struct ip_vs_sync_conn_options {
89 struct ip_vs_seq in_seq; /* incoming seq. struct */
90 struct ip_vs_seq out_seq; /* outgoing seq. struct */
91};
92
2981bc9a
HS
93/*
94 Sync Connection format (sync_conn)
95
96 0 1 2 3
97 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
98 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99 | Type | Protocol | Ver. | Size |
100 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101 | Flags |
102 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103 | State | cport |
104 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105 | vport | dport |
106 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107 | fwmark |
108 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109 | timeout (in sec.) |
110 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
111 | ... |
112 | IP-Addresses (v4 or v6) |
113 | ... |
114 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
115 Optional Parameters.
116 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
117 | Param. Type | Param. Length | Param. data |
118 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
119 | ... |
120 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
121 | | Param Type | Param. Length |
122 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
123 | Param data |
124 | Last Param data should be padded for 32 bit alignment |
125 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
126*/
127
128/*
129 * Type 0, IPv4 sync connection format
130 */
131struct ip_vs_sync_v4 {
132 __u8 type;
133 __u8 protocol; /* Which protocol (TCP/UDP) */
134 __be16 ver_size; /* Version msb 4 bits */
135 /* Flags and state transition */
136 __be32 flags; /* status flags */
137 __be16 state; /* state info */
138 /* Protocol, addresses and port numbers */
139 __be16 cport;
140 __be16 vport;
141 __be16 dport;
142 __be32 fwmark; /* Firewall mark from skb */
143 __be32 timeout; /* cp timeout */
144 __be32 caddr; /* client address */
145 __be32 vaddr; /* virtual address */
146 __be32 daddr; /* destination address */
147 /* The sequence options start here */
148 /* PE data padded to 32bit alignment after seq. options */
149};
150/*
151 * Type 2 messages IPv6
152 */
153struct ip_vs_sync_v6 {
154 __u8 type;
155 __u8 protocol; /* Which protocol (TCP/UDP) */
156 __be16 ver_size; /* Version msb 4 bits */
157 /* Flags and state transition */
158 __be32 flags; /* status flags */
159 __be16 state; /* state info */
160 /* Protocol, addresses and port numbers */
161 __be16 cport;
162 __be16 vport;
163 __be16 dport;
164 __be32 fwmark; /* Firewall mark from skb */
165 __be32 timeout; /* cp timeout */
166 struct in6_addr caddr; /* client address */
167 struct in6_addr vaddr; /* virtual address */
168 struct in6_addr daddr; /* destination address */
169 /* The sequence options start here */
170 /* PE data padded to 32bit alignment after seq. options */
171};
172
173union ip_vs_sync_conn {
174 struct ip_vs_sync_v4 v4;
175 struct ip_vs_sync_v6 v6;
176};
177
178/* Bits in Type field in above */
179#define STYPE_INET6 0
180#define STYPE_F_INET6 (1 << STYPE_INET6)
181
182#define SVER_SHIFT 12 /* Shift to get version */
183#define SVER_MASK 0x0fff /* Mask to strip version */
184
185#define IPVS_OPT_SEQ_DATA 1
186#define IPVS_OPT_PE_DATA 2
187#define IPVS_OPT_PE_NAME 3
188#define IPVS_OPT_PARAM 7
189
190#define IPVS_OPT_F_SEQ_DATA (1 << (IPVS_OPT_SEQ_DATA-1))
191#define IPVS_OPT_F_PE_DATA (1 << (IPVS_OPT_PE_DATA-1))
192#define IPVS_OPT_F_PE_NAME (1 << (IPVS_OPT_PE_NAME-1))
193#define IPVS_OPT_F_PARAM (1 << (IPVS_OPT_PARAM-1))
194
cc0191ae 195struct ip_vs_sync_thread_data {
f131315f 196 struct net *net;
998e7a76
SW
197 struct socket *sock;
198 char *buf;
f73181c8 199 int id;
cc0191ae
NH
200};
201
2981bc9a
HS
202/* Version 0 definition of packet sizes */
203#define SIMPLE_CONN_SIZE (sizeof(struct ip_vs_sync_conn_v0))
1da177e4 204#define FULL_CONN_SIZE \
2981bc9a 205(sizeof(struct ip_vs_sync_conn_v0) + sizeof(struct ip_vs_sync_conn_options))
1da177e4
LT
206
207
208/*
2981bc9a
HS
209 The master mulitcasts messages (Datagrams) to the backup load balancers
210 in the following format.
211
212 Version 1:
213 Note, first byte should be Zero, so ver 0 receivers will drop the packet.
1da177e4
LT
214
215 0 1 2 3
216 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
217 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2981bc9a
HS
218 | 0 | SyncID | Size |
219 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
220 | Count Conns | Version | Reserved, set to Zero |
1da177e4
LT
221 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
222 | |
223 | IPVS Sync Connection (1) |
224 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
225 | . |
2981bc9a 226 ~ . ~
1da177e4
LT
227 | . |
228 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
229 | |
230 | IPVS Sync Connection (n) |
231 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2981bc9a
HS
232
233 Version 0 Header
234 0 1 2 3
235 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
236 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
237 | Count Conns | SyncID | Size |
238 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
239 | IPVS Sync Connection (1) |
1da177e4
LT
240*/
241
242#define SYNC_MESG_HEADER_LEN 4
e6f225eb 243#define MAX_CONNS_PER_SYNCBUFF 255 /* nr_conns in ip_vs_sync_mesg is 8 bit */
1da177e4 244
2981bc9a 245/* Version 0 header */
986a0757 246struct ip_vs_sync_mesg_v0 {
1da177e4
LT
247 __u8 nr_conns;
248 __u8 syncid;
38561437 249 __be16 size;
1da177e4
LT
250
251 /* ip_vs_sync_conn entries start here */
252};
253
2981bc9a 254/* Version 1 header */
986a0757 255struct ip_vs_sync_mesg {
2981bc9a
HS
256 __u8 reserved; /* must be zero */
257 __u8 syncid;
38561437 258 __be16 size;
2981bc9a
HS
259 __u8 nr_conns;
260 __s8 version; /* SYNC_PROTO_VER */
261 __u16 spare;
262 /* ip_vs_sync_conn entries start here */
263};
264
1da177e4
LT
265struct ip_vs_sync_buff {
266 struct list_head list;
267 unsigned long firstuse;
268
269 /* pointers for the message data */
270 struct ip_vs_sync_mesg *mesg;
271 unsigned char *head;
272 unsigned char *end;
273};
274
fe5e7a1e
HS
275/*
276 * Copy of struct ip_vs_seq
277 * From unaligned network order to aligned host order
278 */
279static void ntoh_seq(struct ip_vs_seq *no, struct ip_vs_seq *ho)
280{
281 ho->init_seq = get_unaligned_be32(&no->init_seq);
282 ho->delta = get_unaligned_be32(&no->delta);
283 ho->previous_delta = get_unaligned_be32(&no->previous_delta);
284}
1da177e4 285
986a0757
HS
286/*
287 * Copy of struct ip_vs_seq
288 * From Aligned host order to unaligned network order
289 */
290static void hton_seq(struct ip_vs_seq *ho, struct ip_vs_seq *no)
291{
292 put_unaligned_be32(ho->init_seq, &no->init_seq);
293 put_unaligned_be32(ho->delta, &no->delta);
294 put_unaligned_be32(ho->previous_delta, &no->previous_delta);
295}
296
f73181c8
PNA
297static inline struct ip_vs_sync_buff *
298sb_dequeue(struct netns_ipvs *ipvs, struct ipvs_master_sync_state *ms)
1da177e4
LT
299{
300 struct ip_vs_sync_buff *sb;
301
f131315f 302 spin_lock_bh(&ipvs->sync_lock);
f73181c8 303 if (list_empty(&ms->sync_queue)) {
1da177e4 304 sb = NULL;
1c003b15 305 __set_current_state(TASK_INTERRUPTIBLE);
1da177e4 306 } else {
f73181c8 307 sb = list_entry(ms->sync_queue.next, struct ip_vs_sync_buff,
1da177e4
LT
308 list);
309 list_del(&sb->list);
f73181c8
PNA
310 ms->sync_queue_len--;
311 if (!ms->sync_queue_len)
312 ms->sync_queue_delay = 0;
1da177e4 313 }
f131315f 314 spin_unlock_bh(&ipvs->sync_lock);
1da177e4
LT
315
316 return sb;
317}
318
986a0757
HS
319/*
320 * Create a new sync buffer for Version 1 proto.
321 */
f131315f
HS
322static inline struct ip_vs_sync_buff *
323ip_vs_sync_buff_create(struct netns_ipvs *ipvs)
1da177e4
LT
324{
325 struct ip_vs_sync_buff *sb;
326
327 if (!(sb=kmalloc(sizeof(struct ip_vs_sync_buff), GFP_ATOMIC)))
328 return NULL;
329
f131315f
HS
330 sb->mesg = kmalloc(ipvs->send_mesg_maxlen, GFP_ATOMIC);
331 if (!sb->mesg) {
1da177e4
LT
332 kfree(sb);
333 return NULL;
334 }
f73181c8 335 sb->mesg->reserved = 0; /* old nr_conns i.e. must be zero now */
986a0757 336 sb->mesg->version = SYNC_PROTO_VER;
f131315f 337 sb->mesg->syncid = ipvs->master_syncid;
38561437 338 sb->mesg->size = htons(sizeof(struct ip_vs_sync_mesg));
986a0757
HS
339 sb->mesg->nr_conns = 0;
340 sb->mesg->spare = 0;
341 sb->head = (unsigned char *)sb->mesg + sizeof(struct ip_vs_sync_mesg);
f131315f 342 sb->end = (unsigned char *)sb->mesg + ipvs->send_mesg_maxlen;
986a0757 343
1da177e4
LT
344 sb->firstuse = jiffies;
345 return sb;
346}
347
348static inline void ip_vs_sync_buff_release(struct ip_vs_sync_buff *sb)
349{
350 kfree(sb->mesg);
351 kfree(sb);
352}
353
f73181c8
PNA
354static inline void sb_queue_tail(struct netns_ipvs *ipvs,
355 struct ipvs_master_sync_state *ms)
998e7a76 356{
f73181c8 357 struct ip_vs_sync_buff *sb = ms->sync_buff;
f131315f
HS
358
359 spin_lock(&ipvs->sync_lock);
1c003b15 360 if (ipvs->sync_state & IP_VS_STATE_MASTER &&
f73181c8
PNA
361 ms->sync_queue_len < sysctl_sync_qlen_max(ipvs)) {
362 if (!ms->sync_queue_len)
363 schedule_delayed_work(&ms->master_wakeup_work,
1c003b15 364 max(IPVS_SYNC_SEND_DELAY, 1));
f73181c8
PNA
365 ms->sync_queue_len++;
366 list_add_tail(&sb->list, &ms->sync_queue);
367 if ((++ms->sync_queue_delay) == IPVS_SYNC_WAKEUP_RATE)
368 wake_up_process(ms->master_thread);
1c003b15 369 } else
998e7a76 370 ip_vs_sync_buff_release(sb);
f131315f 371 spin_unlock(&ipvs->sync_lock);
998e7a76
SW
372}
373
1da177e4
LT
374/*
375 * Get the current sync buffer if it has been created for more
376 * than the specified time or the specified time is zero.
377 */
378static inline struct ip_vs_sync_buff *
f73181c8
PNA
379get_curr_sync_buff(struct netns_ipvs *ipvs, struct ipvs_master_sync_state *ms,
380 unsigned long time)
1da177e4
LT
381{
382 struct ip_vs_sync_buff *sb;
383
f131315f 384 spin_lock_bh(&ipvs->sync_buff_lock);
f73181c8
PNA
385 sb = ms->sync_buff;
386 if (sb && time_after_eq(jiffies - sb->firstuse, time)) {
387 ms->sync_buff = NULL;
1c003b15 388 __set_current_state(TASK_RUNNING);
1da177e4
LT
389 } else
390 sb = NULL;
f131315f 391 spin_unlock_bh(&ipvs->sync_buff_lock);
1da177e4
LT
392 return sb;
393}
394
f73181c8
PNA
395static inline int
396select_master_thread_id(struct netns_ipvs *ipvs, struct ip_vs_conn *cp)
f131315f 397{
f73181c8 398 return ((long) cp >> (1 + ilog2(sizeof(*cp)))) & ipvs->threads_mask;
b880c1f0
HS
399}
400
401/*
402 * Create a new sync buffer for Version 0 proto.
403 */
f131315f
HS
404static inline struct ip_vs_sync_buff *
405ip_vs_sync_buff_create_v0(struct netns_ipvs *ipvs)
b880c1f0
HS
406{
407 struct ip_vs_sync_buff *sb;
408 struct ip_vs_sync_mesg_v0 *mesg;
409
410 if (!(sb=kmalloc(sizeof(struct ip_vs_sync_buff), GFP_ATOMIC)))
411 return NULL;
412
f131315f
HS
413 sb->mesg = kmalloc(ipvs->send_mesg_maxlen, GFP_ATOMIC);
414 if (!sb->mesg) {
b880c1f0
HS
415 kfree(sb);
416 return NULL;
417 }
418 mesg = (struct ip_vs_sync_mesg_v0 *)sb->mesg;
419 mesg->nr_conns = 0;
f131315f 420 mesg->syncid = ipvs->master_syncid;
38561437 421 mesg->size = htons(sizeof(struct ip_vs_sync_mesg_v0));
f131315f
HS
422 sb->head = (unsigned char *)mesg + sizeof(struct ip_vs_sync_mesg_v0);
423 sb->end = (unsigned char *)mesg + ipvs->send_mesg_maxlen;
b880c1f0
HS
424 sb->firstuse = jiffies;
425 return sb;
426}
427
749c42b6
JA
428/* Check if conn should be synced.
429 * pkts: conn packets, use sysctl_sync_threshold to avoid packet check
430 * - (1) sync_refresh_period: reduce sync rate. Additionally, retry
431 * sync_retries times with period of sync_refresh_period/8
432 * - (2) if both sync_refresh_period and sync_period are 0 send sync only
433 * for state changes or only once when pkts matches sync_threshold
434 * - (3) templates: rate can be reduced only with sync_refresh_period or
435 * with (2)
436 */
437static int ip_vs_sync_conn_needed(struct netns_ipvs *ipvs,
438 struct ip_vs_conn *cp, int pkts)
439{
440 unsigned long orig = ACCESS_ONCE(cp->sync_endtime);
441 unsigned long now = jiffies;
442 unsigned long n = (now + cp->timeout) & ~3UL;
443 unsigned int sync_refresh_period;
444 int sync_period;
445 int force;
446
447 /* Check if we sync in current state */
448 if (unlikely(cp->flags & IP_VS_CONN_F_TEMPLATE))
449 force = 0;
450 else if (likely(cp->protocol == IPPROTO_TCP)) {
451 if (!((1 << cp->state) &
452 ((1 << IP_VS_TCP_S_ESTABLISHED) |
453 (1 << IP_VS_TCP_S_FIN_WAIT) |
454 (1 << IP_VS_TCP_S_CLOSE) |
455 (1 << IP_VS_TCP_S_CLOSE_WAIT) |
456 (1 << IP_VS_TCP_S_TIME_WAIT))))
457 return 0;
458 force = cp->state != cp->old_state;
459 if (force && cp->state != IP_VS_TCP_S_ESTABLISHED)
460 goto set;
461 } else if (unlikely(cp->protocol == IPPROTO_SCTP)) {
462 if (!((1 << cp->state) &
463 ((1 << IP_VS_SCTP_S_ESTABLISHED) |
464 (1 << IP_VS_SCTP_S_CLOSED) |
465 (1 << IP_VS_SCTP_S_SHUT_ACK_CLI) |
466 (1 << IP_VS_SCTP_S_SHUT_ACK_SER))))
467 return 0;
468 force = cp->state != cp->old_state;
469 if (force && cp->state != IP_VS_SCTP_S_ESTABLISHED)
470 goto set;
471 } else {
472 /* UDP or another protocol with single state */
473 force = 0;
474 }
475
476 sync_refresh_period = sysctl_sync_refresh_period(ipvs);
477 if (sync_refresh_period > 0) {
478 long diff = n - orig;
479 long min_diff = max(cp->timeout >> 1, 10UL * HZ);
480
481 /* Avoid sync if difference is below sync_refresh_period
482 * and below the half timeout.
483 */
484 if (abs(diff) < min_t(long, sync_refresh_period, min_diff)) {
485 int retries = orig & 3;
486
487 if (retries >= sysctl_sync_retries(ipvs))
488 return 0;
489 if (time_before(now, orig - cp->timeout +
490 (sync_refresh_period >> 3)))
491 return 0;
492 n |= retries + 1;
493 }
494 }
495 sync_period = sysctl_sync_period(ipvs);
496 if (sync_period > 0) {
497 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE) &&
498 pkts % sync_period != sysctl_sync_threshold(ipvs))
499 return 0;
500 } else if (sync_refresh_period <= 0 &&
501 pkts != sysctl_sync_threshold(ipvs))
502 return 0;
503
504set:
505 cp->old_state = cp->state;
506 n = cmpxchg(&cp->sync_endtime, orig, n);
507 return n == orig || force;
508}
509
b880c1f0
HS
510/*
511 * Version 0 , could be switched in by sys_ctl.
512 * Add an ip_vs_conn information into the current sync_buff.
513 */
749c42b6
JA
514static void ip_vs_sync_conn_v0(struct net *net, struct ip_vs_conn *cp,
515 int pkts)
b880c1f0 516{
f131315f 517 struct netns_ipvs *ipvs = net_ipvs(net);
b880c1f0
HS
518 struct ip_vs_sync_mesg_v0 *m;
519 struct ip_vs_sync_conn_v0 *s;
f73181c8
PNA
520 struct ip_vs_sync_buff *buff;
521 struct ipvs_master_sync_state *ms;
522 int id;
b880c1f0
HS
523 int len;
524
525 if (unlikely(cp->af != AF_INET))
526 return;
527 /* Do not sync ONE PACKET */
528 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
529 return;
530
749c42b6
JA
531 if (!ip_vs_sync_conn_needed(ipvs, cp, pkts))
532 return;
533
ac69269a 534 spin_lock_bh(&ipvs->sync_buff_lock);
f73181c8 535 if (!(ipvs->sync_state & IP_VS_STATE_MASTER)) {
ac69269a 536 spin_unlock_bh(&ipvs->sync_buff_lock);
f73181c8
PNA
537 return;
538 }
539
540 id = select_master_thread_id(ipvs, cp);
541 ms = &ipvs->ms[id];
542 buff = ms->sync_buff;
543 if (buff) {
544 m = (struct ip_vs_sync_mesg_v0 *) buff->mesg;
545 /* Send buffer if it is for v1 */
546 if (!m->nr_conns) {
547 sb_queue_tail(ipvs, ms);
548 ms->sync_buff = NULL;
549 buff = NULL;
550 }
551 }
552 if (!buff) {
553 buff = ip_vs_sync_buff_create_v0(ipvs);
554 if (!buff) {
ac69269a 555 spin_unlock_bh(&ipvs->sync_buff_lock);
b880c1f0
HS
556 pr_err("ip_vs_sync_buff_create failed.\n");
557 return;
558 }
f73181c8 559 ms->sync_buff = buff;
b880c1f0
HS
560 }
561
562 len = (cp->flags & IP_VS_CONN_F_SEQ_MASK) ? FULL_CONN_SIZE :
563 SIMPLE_CONN_SIZE;
f73181c8
PNA
564 m = (struct ip_vs_sync_mesg_v0 *) buff->mesg;
565 s = (struct ip_vs_sync_conn_v0 *) buff->head;
b880c1f0
HS
566
567 /* copy members */
568 s->reserved = 0;
569 s->protocol = cp->protocol;
570 s->cport = cp->cport;
571 s->vport = cp->vport;
572 s->dport = cp->dport;
573 s->caddr = cp->caddr.ip;
574 s->vaddr = cp->vaddr.ip;
575 s->daddr = cp->daddr.ip;
576 s->flags = htons(cp->flags & ~IP_VS_CONN_F_HASHED);
577 s->state = htons(cp->state);
578 if (cp->flags & IP_VS_CONN_F_SEQ_MASK) {
579 struct ip_vs_sync_conn_options *opt =
580 (struct ip_vs_sync_conn_options *)&s[1];
581 memcpy(opt, &cp->in_seq, sizeof(*opt));
582 }
583
584 m->nr_conns++;
38561437 585 m->size = htons(ntohs(m->size) + len);
f73181c8 586 buff->head += len;
b880c1f0
HS
587
588 /* check if there is a space for next one */
f73181c8
PNA
589 if (buff->head + FULL_CONN_SIZE > buff->end) {
590 sb_queue_tail(ipvs, ms);
591 ms->sync_buff = NULL;
b880c1f0 592 }
ac69269a 593 spin_unlock_bh(&ipvs->sync_buff_lock);
b880c1f0
HS
594
595 /* synchronize its controller if it has */
749c42b6
JA
596 cp = cp->control;
597 if (cp) {
598 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
599 pkts = atomic_add_return(1, &cp->in_pkts);
600 else
601 pkts = sysctl_sync_threshold(ipvs);
2fc9fc91 602 ip_vs_sync_conn(net, cp, pkts);
749c42b6 603 }
b880c1f0
HS
604}
605
1da177e4
LT
606/*
607 * Add an ip_vs_conn information into the current sync_buff.
608 * Called by ip_vs_in.
986a0757 609 * Sending Version 1 messages
1da177e4 610 */
749c42b6 611void ip_vs_sync_conn(struct net *net, struct ip_vs_conn *cp, int pkts)
1da177e4 612{
f131315f 613 struct netns_ipvs *ipvs = net_ipvs(net);
1da177e4 614 struct ip_vs_sync_mesg *m;
986a0757 615 union ip_vs_sync_conn *s;
f73181c8
PNA
616 struct ip_vs_sync_buff *buff;
617 struct ipvs_master_sync_state *ms;
618 int id;
986a0757
HS
619 __u8 *p;
620 unsigned int len, pe_name_len, pad;
621
b880c1f0 622 /* Handle old version of the protocol */
7532e8d4 623 if (sysctl_sync_ver(ipvs) == 0) {
749c42b6 624 ip_vs_sync_conn_v0(net, cp, pkts);
b880c1f0
HS
625 return;
626 }
986a0757
HS
627 /* Do not sync ONE PACKET */
628 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
629 goto control;
630sloop:
749c42b6
JA
631 if (!ip_vs_sync_conn_needed(ipvs, cp, pkts))
632 goto control;
633
986a0757
HS
634 /* Sanity checks */
635 pe_name_len = 0;
636 if (cp->pe_data_len) {
637 if (!cp->pe_data || !cp->dest) {
638 IP_VS_ERR_RL("SYNC, connection pe_data invalid\n");
639 return;
640 }
641 pe_name_len = strnlen(cp->pe->name, IP_VS_PENAME_MAXLEN);
642 }
1da177e4 643
ac69269a 644 spin_lock_bh(&ipvs->sync_buff_lock);
f73181c8 645 if (!(ipvs->sync_state & IP_VS_STATE_MASTER)) {
ac69269a 646 spin_unlock_bh(&ipvs->sync_buff_lock);
f73181c8
PNA
647 return;
648 }
649
650 id = select_master_thread_id(ipvs, cp);
651 ms = &ipvs->ms[id];
986a0757
HS
652
653#ifdef CONFIG_IP_VS_IPV6
654 if (cp->af == AF_INET6)
655 len = sizeof(struct ip_vs_sync_v6);
656 else
657#endif
658 len = sizeof(struct ip_vs_sync_v4);
659
660 if (cp->flags & IP_VS_CONN_F_SEQ_MASK)
661 len += sizeof(struct ip_vs_sync_conn_options) + 2;
662
663 if (cp->pe_data_len)
664 len += cp->pe_data_len + 2; /* + Param hdr field */
665 if (pe_name_len)
666 len += pe_name_len + 2;
667
668 /* check if there is a space for this one */
669 pad = 0;
f73181c8
PNA
670 buff = ms->sync_buff;
671 if (buff) {
672 m = buff->mesg;
673 pad = (4 - (size_t) buff->head) & 3;
674 /* Send buffer if it is for v0 */
675 if (buff->head + len + pad > buff->end || m->reserved) {
676 sb_queue_tail(ipvs, ms);
677 ms->sync_buff = NULL;
678 buff = NULL;
986a0757
HS
679 pad = 0;
680 }
681 }
682
f73181c8
PNA
683 if (!buff) {
684 buff = ip_vs_sync_buff_create(ipvs);
685 if (!buff) {
ac69269a 686 spin_unlock_bh(&ipvs->sync_buff_lock);
1e3e238e 687 pr_err("ip_vs_sync_buff_create failed.\n");
1da177e4
LT
688 return;
689 }
f73181c8
PNA
690 ms->sync_buff = buff;
691 m = buff->mesg;
1da177e4
LT
692 }
693
f73181c8
PNA
694 p = buff->head;
695 buff->head += pad + len;
38561437 696 m->size = htons(ntohs(m->size) + pad + len);
986a0757
HS
697 /* Add ev. padding from prev. sync_conn */
698 while (pad--)
699 *(p++) = 0;
700
701 s = (union ip_vs_sync_conn *)p;
702
703 /* Set message type & copy members */
704 s->v4.type = (cp->af == AF_INET6 ? STYPE_F_INET6 : 0);
705 s->v4.ver_size = htons(len & SVER_MASK); /* Version 0 */
706 s->v4.flags = htonl(cp->flags & ~IP_VS_CONN_F_HASHED);
707 s->v4.state = htons(cp->state);
708 s->v4.protocol = cp->protocol;
709 s->v4.cport = cp->cport;
710 s->v4.vport = cp->vport;
711 s->v4.dport = cp->dport;
712 s->v4.fwmark = htonl(cp->fwmark);
713 s->v4.timeout = htonl(cp->timeout / HZ);
1da177e4 714 m->nr_conns++;
1da177e4 715
986a0757
HS
716#ifdef CONFIG_IP_VS_IPV6
717 if (cp->af == AF_INET6) {
718 p += sizeof(struct ip_vs_sync_v6);
4e3fd7a0
AD
719 s->v6.caddr = cp->caddr.in6;
720 s->v6.vaddr = cp->vaddr.in6;
721 s->v6.daddr = cp->daddr.in6;
986a0757
HS
722 } else
723#endif
724 {
725 p += sizeof(struct ip_vs_sync_v4); /* options ptr */
726 s->v4.caddr = cp->caddr.ip;
727 s->v4.vaddr = cp->vaddr.ip;
728 s->v4.daddr = cp->daddr.ip;
729 }
730 if (cp->flags & IP_VS_CONN_F_SEQ_MASK) {
731 *(p++) = IPVS_OPT_SEQ_DATA;
732 *(p++) = sizeof(struct ip_vs_sync_conn_options);
733 hton_seq((struct ip_vs_seq *)p, &cp->in_seq);
734 p += sizeof(struct ip_vs_seq);
735 hton_seq((struct ip_vs_seq *)p, &cp->out_seq);
736 p += sizeof(struct ip_vs_seq);
1da177e4 737 }
986a0757
HS
738 /* Handle pe data */
739 if (cp->pe_data_len && cp->pe_data) {
740 *(p++) = IPVS_OPT_PE_DATA;
741 *(p++) = cp->pe_data_len;
742 memcpy(p, cp->pe_data, cp->pe_data_len);
743 p += cp->pe_data_len;
744 if (pe_name_len) {
745 /* Add PE_NAME */
746 *(p++) = IPVS_OPT_PE_NAME;
747 *(p++) = pe_name_len;
748 memcpy(p, cp->pe->name, pe_name_len);
749 p += pe_name_len;
750 }
751 }
752
ac69269a 753 spin_unlock_bh(&ipvs->sync_buff_lock);
1da177e4 754
986a0757 755control:
1da177e4 756 /* synchronize its controller if it has */
986a0757
HS
757 cp = cp->control;
758 if (!cp)
759 return;
749c42b6
JA
760 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
761 pkts = atomic_add_return(1, &cp->in_pkts);
762 else
763 pkts = sysctl_sync_threshold(ipvs);
986a0757 764 goto sloop;
1da177e4
LT
765}
766
fe5e7a1e
HS
767/*
768 * fill_param used by version 1
769 */
85999283 770static inline int
6e67e586 771ip_vs_conn_fill_param_sync(struct net *net, int af, union ip_vs_sync_conn *sc,
fe5e7a1e
HS
772 struct ip_vs_conn_param *p,
773 __u8 *pe_data, unsigned int pe_data_len,
774 __u8 *pe_name, unsigned int pe_name_len)
85999283 775{
fe5e7a1e
HS
776#ifdef CONFIG_IP_VS_IPV6
777 if (af == AF_INET6)
6e67e586 778 ip_vs_conn_fill_param(net, af, sc->v6.protocol,
fe5e7a1e
HS
779 (const union nf_inet_addr *)&sc->v6.caddr,
780 sc->v6.cport,
781 (const union nf_inet_addr *)&sc->v6.vaddr,
782 sc->v6.vport, p);
783 else
784#endif
6e67e586 785 ip_vs_conn_fill_param(net, af, sc->v4.protocol,
fe5e7a1e
HS
786 (const union nf_inet_addr *)&sc->v4.caddr,
787 sc->v4.cport,
788 (const union nf_inet_addr *)&sc->v4.vaddr,
789 sc->v4.vport, p);
790 /* Handle pe data */
791 if (pe_data_len) {
792 if (pe_name_len) {
793 char buff[IP_VS_PENAME_MAXLEN+1];
794
795 memcpy(buff, pe_name, pe_name_len);
796 buff[pe_name_len]=0;
797 p->pe = __ip_vs_pe_getbyname(buff);
798 if (!p->pe) {
f131315f
HS
799 IP_VS_DBG(3, "BACKUP, no %s engine found/loaded\n",
800 buff);
fe5e7a1e
HS
801 return 1;
802 }
803 } else {
804 IP_VS_ERR_RL("BACKUP, Invalid PE parameters\n");
805 return 1;
806 }
807
6060c74a 808 p->pe_data = kmemdup(pe_data, pe_data_len, GFP_ATOMIC);
fe5e7a1e
HS
809 if (!p->pe_data) {
810 if (p->pe->module)
811 module_put(p->pe->module);
812 return -ENOMEM;
813 }
fe5e7a1e
HS
814 p->pe_data_len = pe_data_len;
815 }
85999283
SH
816 return 0;
817}
1da177e4
LT
818
819/*
fe5e7a1e
HS
820 * Connection Add / Update.
821 * Common for version 0 and 1 reception of backup sync_conns.
822 * Param: ...
823 * timeout is in sec.
1da177e4 824 */
9bbac6a9
HS
825static void ip_vs_proc_conn(struct net *net, struct ip_vs_conn_param *param,
826 unsigned int flags, unsigned int state,
827 unsigned int protocol, unsigned int type,
fe5e7a1e
HS
828 const union nf_inet_addr *daddr, __be16 dport,
829 unsigned long timeout, __u32 fwmark,
9bbac6a9 830 struct ip_vs_sync_conn_options *opt)
fe5e7a1e
HS
831{
832 struct ip_vs_dest *dest;
833 struct ip_vs_conn *cp;
a0840e2e 834 struct netns_ipvs *ipvs = net_ipvs(net);
fe5e7a1e 835
fe5e7a1e
HS
836 if (!(flags & IP_VS_CONN_F_TEMPLATE))
837 cp = ip_vs_conn_in_get(param);
838 else
839 cp = ip_vs_ct_in_get(param);
840
cdcc5e90
JA
841 if (cp) {
842 /* Free pe_data */
fe5e7a1e 843 kfree(param->pe_data);
cdcc5e90
JA
844
845 dest = cp->dest;
ac69269a 846 spin_lock_bh(&cp->lock);
cdcc5e90
JA
847 if ((cp->flags ^ flags) & IP_VS_CONN_F_INACTIVE &&
848 !(flags & IP_VS_CONN_F_TEMPLATE) && dest) {
849 if (flags & IP_VS_CONN_F_INACTIVE) {
850 atomic_dec(&dest->activeconns);
851 atomic_inc(&dest->inactconns);
852 } else {
853 atomic_inc(&dest->activeconns);
854 atomic_dec(&dest->inactconns);
855 }
856 }
857 flags &= IP_VS_CONN_F_BACKUP_UPD_MASK;
858 flags |= cp->flags & ~IP_VS_CONN_F_BACKUP_UPD_MASK;
859 cp->flags = flags;
ac69269a 860 spin_unlock_bh(&cp->lock);
413c2d04
JA
861 if (!dest)
862 ip_vs_try_bind_dest(cp);
cdcc5e90 863 } else {
fe5e7a1e
HS
864 /*
865 * Find the appropriate destination for the connection.
866 * If it is not found the connection will remain unbound
867 * but still handled.
868 */
413c2d04 869 rcu_read_lock();
f131315f 870 dest = ip_vs_find_dest(net, type, daddr, dport, param->vaddr,
52793dbe 871 param->vport, protocol, fwmark, flags);
fe5e7a1e 872
fe5e7a1e 873 cp = ip_vs_conn_new(param, daddr, dport, flags, dest, fwmark);
413c2d04 874 rcu_read_unlock();
fe5e7a1e
HS
875 if (!cp) {
876 if (param->pe_data)
877 kfree(param->pe_data);
878 IP_VS_DBG(2, "BACKUP, add new conn. failed\n");
879 return;
880 }
2fd83573
JA
881 if (!(flags & IP_VS_CONN_F_TEMPLATE))
882 kfree(param->pe_data);
fe5e7a1e
HS
883 }
884
885 if (opt)
886 memcpy(&cp->in_seq, opt, sizeof(*opt));
59e0350e 887 atomic_set(&cp->in_pkts, sysctl_sync_threshold(ipvs));
fe5e7a1e
HS
888 cp->state = state;
889 cp->old_state = cp->state;
890 /*
891 * For Ver 0 messages style
892 * - Not possible to recover the right timeout for templates
893 * - can not find the right fwmark
894 * virtual service. If needed, we can do it for
895 * non-fwmark persistent services.
896 * Ver 1 messages style.
897 * - No problem.
898 */
899 if (timeout) {
900 if (timeout > MAX_SCHEDULE_TIMEOUT / HZ)
901 timeout = MAX_SCHEDULE_TIMEOUT / HZ;
902 cp->timeout = timeout*HZ;
9bbac6a9
HS
903 } else {
904 struct ip_vs_proto_data *pd;
905
906 pd = ip_vs_proto_data_get(net, protocol);
907 if (!(flags & IP_VS_CONN_F_TEMPLATE) && pd && pd->timeout_table)
908 cp->timeout = pd->timeout_table[state];
909 else
910 cp->timeout = (3*60*HZ);
911 }
fe5e7a1e
HS
912 ip_vs_conn_put(cp);
913}
914
915/*
916 * Process received multicast message for Version 0
917 */
9bbac6a9
HS
918static void ip_vs_process_message_v0(struct net *net, const char *buffer,
919 const size_t buflen)
1da177e4 920{
986a0757 921 struct ip_vs_sync_mesg_v0 *m = (struct ip_vs_sync_mesg_v0 *)buffer;
2981bc9a 922 struct ip_vs_sync_conn_v0 *s;
1da177e4 923 struct ip_vs_sync_conn_options *opt;
5c81833c 924 struct ip_vs_protocol *pp;
f11017ec 925 struct ip_vs_conn_param param;
1da177e4
LT
926 char *p;
927 int i;
928
986a0757 929 p = (char *)buffer + sizeof(struct ip_vs_sync_mesg_v0);
1da177e4 930 for (i=0; i<m->nr_conns; i++) {
95c96174 931 unsigned int flags, state;
87375ab4 932
2ad17def 933 if (p + SIMPLE_CONN_SIZE > buffer+buflen) {
fe5e7a1e 934 IP_VS_ERR_RL("BACKUP v0, bogus conn\n");
2ad17def
JA
935 return;
936 }
2981bc9a 937 s = (struct ip_vs_sync_conn_v0 *) p;
7a4fbb1f 938 flags = ntohs(s->flags) | IP_VS_CONN_F_SYNC;
2ad17def
JA
939 flags &= ~IP_VS_CONN_F_HASHED;
940 if (flags & IP_VS_CONN_F_SEQ_MASK) {
941 opt = (struct ip_vs_sync_conn_options *)&s[1];
942 p += FULL_CONN_SIZE;
943 if (p > buffer+buflen) {
fe5e7a1e 944 IP_VS_ERR_RL("BACKUP v0, Dropping buffer bogus conn options\n");
2ad17def
JA
945 return;
946 }
947 } else {
948 opt = NULL;
949 p += SIMPLE_CONN_SIZE;
950 }
951
b209639e 952 state = ntohs(s->state);
2ad17def
JA
953 if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
954 pp = ip_vs_proto_get(s->protocol);
955 if (!pp) {
fe5e7a1e 956 IP_VS_DBG(2, "BACKUP v0, Unsupported protocol %u\n",
2ad17def
JA
957 s->protocol);
958 continue;
959 }
960 if (state >= pp->num_states) {
fe5e7a1e 961 IP_VS_DBG(2, "BACKUP v0, Invalid %s state %u\n",
2ad17def
JA
962 pp->name, state);
963 continue;
964 }
965 } else {
966 /* protocol in templates is not used for state/timeout */
2ad17def 967 if (state > 0) {
fe5e7a1e 968 IP_VS_DBG(2, "BACKUP v0, Invalid template state %u\n",
2ad17def
JA
969 state);
970 state = 0;
971 }
972 }
973
6e67e586 974 ip_vs_conn_fill_param(net, AF_INET, s->protocol,
fe5e7a1e
HS
975 (const union nf_inet_addr *)&s->caddr,
976 s->cport,
977 (const union nf_inet_addr *)&s->vaddr,
978 s->vport, &param);
979
980 /* Send timeout as Zero */
9bbac6a9 981 ip_vs_proc_conn(net, &param, flags, state, s->protocol, AF_INET,
fe5e7a1e 982 (union nf_inet_addr *)&s->daddr, s->dport,
9bbac6a9 983 0, 0, opt);
fe5e7a1e
HS
984 }
985}
986
987/*
988 * Handle options
989 */
990static inline int ip_vs_proc_seqopt(__u8 *p, unsigned int plen,
991 __u32 *opt_flags,
992 struct ip_vs_sync_conn_options *opt)
993{
994 struct ip_vs_sync_conn_options *topt;
995
996 topt = (struct ip_vs_sync_conn_options *)p;
997
998 if (plen != sizeof(struct ip_vs_sync_conn_options)) {
999 IP_VS_DBG(2, "BACKUP, bogus conn options length\n");
1000 return -EINVAL;
1001 }
1002 if (*opt_flags & IPVS_OPT_F_SEQ_DATA) {
1003 IP_VS_DBG(2, "BACKUP, conn options found twice\n");
1004 return -EINVAL;
1005 }
1006 ntoh_seq(&topt->in_seq, &opt->in_seq);
1007 ntoh_seq(&topt->out_seq, &opt->out_seq);
1008 *opt_flags |= IPVS_OPT_F_SEQ_DATA;
1009 return 0;
1010}
1011
1012static int ip_vs_proc_str(__u8 *p, unsigned int plen, unsigned int *data_len,
1013 __u8 **data, unsigned int maxlen,
1014 __u32 *opt_flags, __u32 flag)
1015{
1016 if (plen > maxlen) {
1017 IP_VS_DBG(2, "BACKUP, bogus par.data len > %d\n", maxlen);
1018 return -EINVAL;
1019 }
1020 if (*opt_flags & flag) {
1021 IP_VS_DBG(2, "BACKUP, Par.data found twice 0x%x\n", flag);
1022 return -EINVAL;
1023 }
1024 *data_len = plen;
1025 *data = p;
1026 *opt_flags |= flag;
1027 return 0;
1028}
1029/*
1030 * Process a Version 1 sync. connection
1031 */
9bbac6a9 1032static inline int ip_vs_proc_sync_conn(struct net *net, __u8 *p, __u8 *msg_end)
fe5e7a1e
HS
1033{
1034 struct ip_vs_sync_conn_options opt;
1035 union ip_vs_sync_conn *s;
1036 struct ip_vs_protocol *pp;
1037 struct ip_vs_conn_param param;
1038 __u32 flags;
1039 unsigned int af, state, pe_data_len=0, pe_name_len=0;
1040 __u8 *pe_data=NULL, *pe_name=NULL;
1041 __u32 opt_flags=0;
1042 int retc=0;
1043
1044 s = (union ip_vs_sync_conn *) p;
1045
1046 if (s->v6.type & STYPE_F_INET6) {
1047#ifdef CONFIG_IP_VS_IPV6
1048 af = AF_INET6;
1049 p += sizeof(struct ip_vs_sync_v6);
1050#else
1051 IP_VS_DBG(3,"BACKUP, IPv6 msg received, and IPVS is not compiled for IPv6\n");
1052 retc = 10;
1053 goto out;
1054#endif
1055 } else if (!s->v4.type) {
1056 af = AF_INET;
1057 p += sizeof(struct ip_vs_sync_v4);
1058 } else {
1059 return -10;
1060 }
1061 if (p > msg_end)
1062 return -20;
1063
1064 /* Process optional params check Type & Len. */
1065 while (p < msg_end) {
1066 int ptype;
1067 int plen;
1068
1069 if (p+2 > msg_end)
1070 return -30;
1071 ptype = *(p++);
1072 plen = *(p++);
1073
1074 if (!plen || ((p + plen) > msg_end))
1075 return -40;
1076 /* Handle seq option p = param data */
1077 switch (ptype & ~IPVS_OPT_F_PARAM) {
1078 case IPVS_OPT_SEQ_DATA:
1079 if (ip_vs_proc_seqopt(p, plen, &opt_flags, &opt))
1080 return -50;
1081 break;
1082
1083 case IPVS_OPT_PE_DATA:
1084 if (ip_vs_proc_str(p, plen, &pe_data_len, &pe_data,
1085 IP_VS_PEDATA_MAXLEN, &opt_flags,
1086 IPVS_OPT_F_PE_DATA))
1087 return -60;
1088 break;
1089
1090 case IPVS_OPT_PE_NAME:
1091 if (ip_vs_proc_str(p, plen,&pe_name_len, &pe_name,
1092 IP_VS_PENAME_MAXLEN, &opt_flags,
1093 IPVS_OPT_F_PE_NAME))
1094 return -70;
1095 break;
1096
1097 default:
1098 /* Param data mandatory ? */
1099 if (!(ptype & IPVS_OPT_F_PARAM)) {
1100 IP_VS_DBG(3, "BACKUP, Unknown mandatory param %d found\n",
1101 ptype & ~IPVS_OPT_F_PARAM);
1102 retc = 20;
1103 goto out;
1104 }
f11017ec 1105 }
fe5e7a1e
HS
1106 p += plen; /* Next option */
1107 }
1108
1109 /* Get flags and Mask off unsupported */
1110 flags = ntohl(s->v4.flags) & IP_VS_CONN_F_BACKUP_MASK;
1111 flags |= IP_VS_CONN_F_SYNC;
1112 state = ntohs(s->v4.state);
1113
1114 if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
1115 pp = ip_vs_proto_get(s->v4.protocol);
1116 if (!pp) {
1117 IP_VS_DBG(3,"BACKUP, Unsupported protocol %u\n",
1118 s->v4.protocol);
1119 retc = 30;
1120 goto out;
1121 }
1122 if (state >= pp->num_states) {
1123 IP_VS_DBG(3, "BACKUP, Invalid %s state %u\n",
1124 pp->name, state);
1125 retc = 40;
1126 goto out;
1127 }
1128 } else {
1129 /* protocol in templates is not used for state/timeout */
fe5e7a1e
HS
1130 if (state > 0) {
1131 IP_VS_DBG(3, "BACKUP, Invalid template state %u\n",
1132 state);
1133 state = 0;
1134 }
1135 }
6e67e586
HS
1136 if (ip_vs_conn_fill_param_sync(net, af, s, &param, pe_data,
1137 pe_data_len, pe_name, pe_name_len)) {
fe5e7a1e
HS
1138 retc = 50;
1139 goto out;
1140 }
1141 /* If only IPv4, just silent skip IPv6 */
1142 if (af == AF_INET)
9bbac6a9 1143 ip_vs_proc_conn(net, &param, flags, state, s->v4.protocol, af,
fe5e7a1e
HS
1144 (union nf_inet_addr *)&s->v4.daddr, s->v4.dport,
1145 ntohl(s->v4.timeout), ntohl(s->v4.fwmark),
9bbac6a9
HS
1146 (opt_flags & IPVS_OPT_F_SEQ_DATA ? &opt : NULL)
1147 );
fe5e7a1e
HS
1148#ifdef CONFIG_IP_VS_IPV6
1149 else
9bbac6a9 1150 ip_vs_proc_conn(net, &param, flags, state, s->v6.protocol, af,
fe5e7a1e
HS
1151 (union nf_inet_addr *)&s->v6.daddr, s->v6.dport,
1152 ntohl(s->v6.timeout), ntohl(s->v6.fwmark),
9bbac6a9
HS
1153 (opt_flags & IPVS_OPT_F_SEQ_DATA ? &opt : NULL)
1154 );
fe5e7a1e 1155#endif
2fd83573 1156 ip_vs_pe_put(param.pe);
fe5e7a1e
HS
1157 return 0;
1158 /* Error exit */
1159out:
1160 IP_VS_DBG(2, "BACKUP, Single msg dropped err:%d\n", retc);
1161 return retc;
1162
1163}
1164/*
1165 * Process received multicast message and create the corresponding
1166 * ip_vs_conn entries.
1167 * Handles Version 0 & 1
1168 */
9bbac6a9
HS
1169static void ip_vs_process_message(struct net *net, __u8 *buffer,
1170 const size_t buflen)
fe5e7a1e 1171{
f131315f 1172 struct netns_ipvs *ipvs = net_ipvs(net);
986a0757 1173 struct ip_vs_sync_mesg *m2 = (struct ip_vs_sync_mesg *)buffer;
fe5e7a1e 1174 __u8 *p, *msg_end;
986a0757 1175 int i, nr_conns;
fe5e7a1e 1176
986a0757 1177 if (buflen < sizeof(struct ip_vs_sync_mesg_v0)) {
fe5e7a1e
HS
1178 IP_VS_DBG(2, "BACKUP, message header too short\n");
1179 return;
1180 }
fe5e7a1e 1181
38561437 1182 if (buflen != ntohs(m2->size)) {
fe5e7a1e
HS
1183 IP_VS_DBG(2, "BACKUP, bogus message size\n");
1184 return;
1185 }
1186 /* SyncID sanity check */
f131315f 1187 if (ipvs->backup_syncid != 0 && m2->syncid != ipvs->backup_syncid) {
fe5e7a1e
HS
1188 IP_VS_DBG(7, "BACKUP, Ignoring syncid = %d\n", m2->syncid);
1189 return;
1190 }
1191 /* Handle version 1 message */
1192 if ((m2->version == SYNC_PROTO_VER) && (m2->reserved == 0)
1193 && (m2->spare == 0)) {
1194
986a0757 1195 msg_end = buffer + sizeof(struct ip_vs_sync_mesg);
fe5e7a1e
HS
1196 nr_conns = m2->nr_conns;
1197
1198 for (i=0; i<nr_conns; i++) {
1199 union ip_vs_sync_conn *s;
95c96174 1200 unsigned int size;
fe5e7a1e
HS
1201 int retc;
1202
1203 p = msg_end;
1204 if (p + sizeof(s->v4) > buffer+buflen) {
1205 IP_VS_ERR_RL("BACKUP, Dropping buffer, to small\n");
1206 return;
b209639e 1207 }
fe5e7a1e
HS
1208 s = (union ip_vs_sync_conn *)p;
1209 size = ntohs(s->v4.ver_size) & SVER_MASK;
1210 msg_end = p + size;
1211 /* Basic sanity checks */
1212 if (msg_end > buffer+buflen) {
1213 IP_VS_ERR_RL("BACKUP, Dropping buffer, msg > buffer\n");
1da177e4
LT
1214 return;
1215 }
fe5e7a1e
HS
1216 if (ntohs(s->v4.ver_size) >> SVER_SHIFT) {
1217 IP_VS_ERR_RL("BACKUP, Dropping buffer, Unknown version %d\n",
1218 ntohs(s->v4.ver_size) >> SVER_SHIFT);
1219 return;
b209639e 1220 }
fe5e7a1e 1221 /* Process a single sync_conn */
9bbac6a9
HS
1222 retc = ip_vs_proc_sync_conn(net, p, msg_end);
1223 if (retc < 0) {
fe5e7a1e
HS
1224 IP_VS_ERR_RL("BACKUP, Dropping buffer, Err: %d in decoding\n",
1225 retc);
1226 return;
2906f66a 1227 }
fe5e7a1e
HS
1228 /* Make sure we have 32 bit alignment */
1229 msg_end = p + ((size + 3) & ~3);
b209639e 1230 }
fe5e7a1e
HS
1231 } else {
1232 /* Old type of message */
9bbac6a9 1233 ip_vs_process_message_v0(net, buffer, buflen);
fe5e7a1e 1234 return;
1da177e4
LT
1235 }
1236}
1237
1238
1c003b15
PNA
1239/*
1240 * Setup sndbuf (mode=1) or rcvbuf (mode=0)
1241 */
1242static void set_sock_size(struct sock *sk, int mode, int val)
1243{
1244 /* setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &val, sizeof(val)); */
1245 /* setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &val, sizeof(val)); */
1246 lock_sock(sk);
1247 if (mode) {
1248 val = clamp_t(int, val, (SOCK_MIN_SNDBUF + 1) / 2,
1249 sysctl_wmem_max);
1250 sk->sk_sndbuf = val * 2;
1251 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
1252 } else {
1253 val = clamp_t(int, val, (SOCK_MIN_RCVBUF + 1) / 2,
1254 sysctl_rmem_max);
1255 sk->sk_rcvbuf = val * 2;
1256 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
1257 }
1258 release_sock(sk);
1259}
1260
1da177e4
LT
1261/*
1262 * Setup loopback of outgoing multicasts on a sending socket
1263 */
1264static void set_mcast_loop(struct sock *sk, u_char loop)
1265{
1266 struct inet_sock *inet = inet_sk(sk);
1267
1268 /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */
1269 lock_sock(sk);
1270 inet->mc_loop = loop ? 1 : 0;
1271 release_sock(sk);
1272}
1273
1274/*
1275 * Specify TTL for outgoing multicasts on a sending socket
1276 */
1277static void set_mcast_ttl(struct sock *sk, u_char ttl)
1278{
1279 struct inet_sock *inet = inet_sk(sk);
1280
1281 /* setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); */
1282 lock_sock(sk);
1283 inet->mc_ttl = ttl;
1284 release_sock(sk);
1285}
1286
1287/*
1288 * Specifiy default interface for outgoing multicasts
1289 */
1290static int set_mcast_if(struct sock *sk, char *ifname)
1291{
1292 struct net_device *dev;
1293 struct inet_sock *inet = inet_sk(sk);
f131315f 1294 struct net *net = sock_net(sk);
1da177e4 1295
f131315f
HS
1296 dev = __dev_get_by_name(net, ifname);
1297 if (!dev)
1da177e4
LT
1298 return -ENODEV;
1299
1300 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
1301 return -EINVAL;
1302
1303 lock_sock(sk);
1304 inet->mc_index = dev->ifindex;
1305 /* inet->mc_addr = 0; */
1306 release_sock(sk);
1307
1308 return 0;
1309}
1310
1311
1312/*
1313 * Set the maximum length of sync message according to the
1314 * specified interface's MTU.
1315 */
f131315f 1316static int set_sync_mesg_maxlen(struct net *net, int sync_state)
1da177e4 1317{
f131315f 1318 struct netns_ipvs *ipvs = net_ipvs(net);
1da177e4
LT
1319 struct net_device *dev;
1320 int num;
1321
1322 if (sync_state == IP_VS_STATE_MASTER) {
f131315f
HS
1323 dev = __dev_get_by_name(net, ipvs->master_mcast_ifn);
1324 if (!dev)
1da177e4
LT
1325 return -ENODEV;
1326
1327 num = (dev->mtu - sizeof(struct iphdr) -
1328 sizeof(struct udphdr) -
1329 SYNC_MESG_HEADER_LEN - 20) / SIMPLE_CONN_SIZE;
f131315f 1330 ipvs->send_mesg_maxlen = SYNC_MESG_HEADER_LEN +
e6f225eb 1331 SIMPLE_CONN_SIZE * min(num, MAX_CONNS_PER_SYNCBUFF);
1da177e4 1332 IP_VS_DBG(7, "setting the maximum length of sync sending "
f131315f 1333 "message %d.\n", ipvs->send_mesg_maxlen);
1da177e4 1334 } else if (sync_state == IP_VS_STATE_BACKUP) {
f131315f
HS
1335 dev = __dev_get_by_name(net, ipvs->backup_mcast_ifn);
1336 if (!dev)
1da177e4
LT
1337 return -ENODEV;
1338
f131315f 1339 ipvs->recv_mesg_maxlen = dev->mtu -
1da177e4
LT
1340 sizeof(struct iphdr) - sizeof(struct udphdr);
1341 IP_VS_DBG(7, "setting the maximum length of sync receiving "
f131315f 1342 "message %d.\n", ipvs->recv_mesg_maxlen);
1da177e4
LT
1343 }
1344
1345 return 0;
1346}
1347
1348
1349/*
1350 * Join a multicast group.
1351 * the group is specified by a class D multicast address 224.0.0.0/8
1352 * in the in_addr structure passed in as a parameter.
1353 */
1354static int
1355join_mcast_group(struct sock *sk, struct in_addr *addr, char *ifname)
1356{
f131315f 1357 struct net *net = sock_net(sk);
1da177e4
LT
1358 struct ip_mreqn mreq;
1359 struct net_device *dev;
1360 int ret;
1361
1362 memset(&mreq, 0, sizeof(mreq));
1363 memcpy(&mreq.imr_multiaddr, addr, sizeof(struct in_addr));
1364
f131315f
HS
1365 dev = __dev_get_by_name(net, ifname);
1366 if (!dev)
1da177e4
LT
1367 return -ENODEV;
1368 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
1369 return -EINVAL;
1370
1371 mreq.imr_ifindex = dev->ifindex;
1372
1373 lock_sock(sk);
1374 ret = ip_mc_join_group(sk, &mreq);
1375 release_sock(sk);
1376
1377 return ret;
1378}
1379
1380
1381static int bind_mcastif_addr(struct socket *sock, char *ifname)
1382{
f131315f 1383 struct net *net = sock_net(sock->sk);
1da177e4 1384 struct net_device *dev;
a61ced5d 1385 __be32 addr;
1da177e4
LT
1386 struct sockaddr_in sin;
1387
f131315f
HS
1388 dev = __dev_get_by_name(net, ifname);
1389 if (!dev)
1da177e4
LT
1390 return -ENODEV;
1391
1392 addr = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
1393 if (!addr)
1e3e238e
HE
1394 pr_err("You probably need to specify IP address on "
1395 "multicast interface.\n");
1da177e4 1396
14d5e834
HH
1397 IP_VS_DBG(7, "binding socket with (%s) %pI4\n",
1398 ifname, &addr);
1da177e4
LT
1399
1400 /* Now bind the socket with the address of multicast interface */
1401 sin.sin_family = AF_INET;
1402 sin.sin_addr.s_addr = addr;
1403 sin.sin_port = 0;
1404
1405 return sock->ops->bind(sock, (struct sockaddr*)&sin, sizeof(sin));
1406}
1407
1408/*
1409 * Set up sending multicast socket over UDP
1410 */
f73181c8 1411static struct socket *make_send_sock(struct net *net, int id)
1da177e4 1412{
f131315f 1413 struct netns_ipvs *ipvs = net_ipvs(net);
f73181c8
PNA
1414 /* multicast addr */
1415 struct sockaddr_in mcast_addr = {
1416 .sin_family = AF_INET,
1417 .sin_port = cpu_to_be16(IP_VS_SYNC_PORT + id),
1418 .sin_addr.s_addr = cpu_to_be32(IP_VS_SYNC_GROUP),
1419 };
1da177e4 1420 struct socket *sock;
e6dd731c 1421 int result;
1da177e4 1422
1ae132b0
HS
1423 /* First create a socket move it to right name space later */
1424 result = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
e6dd731c 1425 if (result < 0) {
1e3e238e 1426 pr_err("Error during creation of socket; terminating\n");
e6dd731c 1427 return ERR_PTR(result);
1da177e4 1428 }
1ae132b0
HS
1429 /*
1430 * Kernel sockets that are a part of a namespace, should not
1431 * hold a reference to a namespace in order to allow to stop it.
1432 * After sk_change_net should be released using sk_release_kernel.
1433 */
1434 sk_change_net(sock->sk, net);
f131315f 1435 result = set_mcast_if(sock->sk, ipvs->master_mcast_ifn);
e6dd731c 1436 if (result < 0) {
1e3e238e 1437 pr_err("Error setting outbound mcast interface\n");
1da177e4
LT
1438 goto error;
1439 }
1440
1441 set_mcast_loop(sock->sk, 0);
1442 set_mcast_ttl(sock->sk, 1);
1c003b15
PNA
1443 result = sysctl_sync_sock_size(ipvs);
1444 if (result > 0)
1445 set_sock_size(sock->sk, 1, result);
1da177e4 1446
f131315f 1447 result = bind_mcastif_addr(sock, ipvs->master_mcast_ifn);
e6dd731c 1448 if (result < 0) {
1e3e238e 1449 pr_err("Error binding address of the mcast interface\n");
1da177e4
LT
1450 goto error;
1451 }
1452
e6dd731c
SW
1453 result = sock->ops->connect(sock, (struct sockaddr *) &mcast_addr,
1454 sizeof(struct sockaddr), 0);
1455 if (result < 0) {
1e3e238e 1456 pr_err("Error connecting to the multicast addr\n");
1da177e4
LT
1457 goto error;
1458 }
1459
1460 return sock;
1461
1ae132b0
HS
1462error:
1463 sk_release_kernel(sock->sk);
e6dd731c 1464 return ERR_PTR(result);
1da177e4
LT
1465}
1466
1467
1468/*
1469 * Set up receiving multicast socket over UDP
1470 */
f73181c8 1471static struct socket *make_receive_sock(struct net *net, int id)
1da177e4 1472{
f131315f 1473 struct netns_ipvs *ipvs = net_ipvs(net);
f73181c8
PNA
1474 /* multicast addr */
1475 struct sockaddr_in mcast_addr = {
1476 .sin_family = AF_INET,
1477 .sin_port = cpu_to_be16(IP_VS_SYNC_PORT + id),
1478 .sin_addr.s_addr = cpu_to_be32(IP_VS_SYNC_GROUP),
1479 };
1da177e4 1480 struct socket *sock;
e6dd731c 1481 int result;
1da177e4
LT
1482
1483 /* First create a socket */
1ae132b0 1484 result = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
e6dd731c 1485 if (result < 0) {
1e3e238e 1486 pr_err("Error during creation of socket; terminating\n");
e6dd731c 1487 return ERR_PTR(result);
1da177e4 1488 }
1ae132b0
HS
1489 /*
1490 * Kernel sockets that are a part of a namespace, should not
1491 * hold a reference to a namespace in order to allow to stop it.
1492 * After sk_change_net should be released using sk_release_kernel.
1493 */
1494 sk_change_net(sock->sk, net);
1da177e4 1495 /* it is equivalent to the REUSEADDR option in user-space */
4a17fd52 1496 sock->sk->sk_reuse = SK_CAN_REUSE;
1c003b15
PNA
1497 result = sysctl_sync_sock_size(ipvs);
1498 if (result > 0)
1499 set_sock_size(sock->sk, 0, result);
1da177e4 1500
e6dd731c
SW
1501 result = sock->ops->bind(sock, (struct sockaddr *) &mcast_addr,
1502 sizeof(struct sockaddr));
1503 if (result < 0) {
1e3e238e 1504 pr_err("Error binding to the multicast addr\n");
1da177e4
LT
1505 goto error;
1506 }
1507
1508 /* join the multicast group */
e6dd731c
SW
1509 result = join_mcast_group(sock->sk,
1510 (struct in_addr *) &mcast_addr.sin_addr,
f131315f 1511 ipvs->backup_mcast_ifn);
e6dd731c 1512 if (result < 0) {
1e3e238e 1513 pr_err("Error joining to the multicast group\n");
1da177e4
LT
1514 goto error;
1515 }
1516
1517 return sock;
1518
1ae132b0
HS
1519error:
1520 sk_release_kernel(sock->sk);
e6dd731c 1521 return ERR_PTR(result);
1da177e4
LT
1522}
1523
1524
1525static int
1526ip_vs_send_async(struct socket *sock, const char *buffer, const size_t length)
1527{
1528 struct msghdr msg = {.msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL};
1529 struct kvec iov;
1530 int len;
1531
1532 EnterFunction(7);
1533 iov.iov_base = (void *)buffer;
1534 iov.iov_len = length;
1535
1536 len = kernel_sendmsg(sock, &msg, &iov, 1, (size_t)(length));
1537
1538 LeaveFunction(7);
1539 return len;
1540}
1541
1c003b15 1542static int
1da177e4
LT
1543ip_vs_send_sync_msg(struct socket *sock, struct ip_vs_sync_mesg *msg)
1544{
1545 int msize;
1c003b15 1546 int ret;
1da177e4 1547
38561437 1548 msize = ntohs(msg->size);
1da177e4 1549
1c003b15
PNA
1550 ret = ip_vs_send_async(sock, (char *)msg, msize);
1551 if (ret >= 0 || ret == -EAGAIN)
1552 return ret;
1553 pr_err("ip_vs_send_async error %d\n", ret);
1554 return 0;
1da177e4
LT
1555}
1556
1557static int
1558ip_vs_receive(struct socket *sock, char *buffer, const size_t buflen)
1559{
1560 struct msghdr msg = {NULL,};
1561 struct kvec iov;
1562 int len;
1563
1564 EnterFunction(7);
1565
1566 /* Receive a packet */
1567 iov.iov_base = buffer;
1568 iov.iov_len = (size_t)buflen;
1569
f73181c8 1570 len = kernel_recvmsg(sock, &msg, &iov, 1, buflen, MSG_DONTWAIT);
1da177e4
LT
1571
1572 if (len < 0)
f73181c8 1573 return len;
1da177e4
LT
1574
1575 LeaveFunction(7);
1576 return len;
1577}
1578
1c003b15
PNA
1579/* Wakeup the master thread for sending */
1580static void master_wakeup_work_handler(struct work_struct *work)
1581{
f73181c8
PNA
1582 struct ipvs_master_sync_state *ms =
1583 container_of(work, struct ipvs_master_sync_state,
1584 master_wakeup_work.work);
1585 struct netns_ipvs *ipvs = ms->ipvs;
1c003b15
PNA
1586
1587 spin_lock_bh(&ipvs->sync_lock);
f73181c8
PNA
1588 if (ms->sync_queue_len &&
1589 ms->sync_queue_delay < IPVS_SYNC_WAKEUP_RATE) {
1590 ms->sync_queue_delay = IPVS_SYNC_WAKEUP_RATE;
1591 wake_up_process(ms->master_thread);
1c003b15
PNA
1592 }
1593 spin_unlock_bh(&ipvs->sync_lock);
1594}
1595
1596/* Get next buffer to send */
1597static inline struct ip_vs_sync_buff *
f73181c8 1598next_sync_buff(struct netns_ipvs *ipvs, struct ipvs_master_sync_state *ms)
1c003b15
PNA
1599{
1600 struct ip_vs_sync_buff *sb;
1601
f73181c8 1602 sb = sb_dequeue(ipvs, ms);
1c003b15
PNA
1603 if (sb)
1604 return sb;
1605 /* Do not delay entries in buffer for more than 2 seconds */
f73181c8 1606 return get_curr_sync_buff(ipvs, ms, IPVS_SYNC_FLUSH_TIME);
1c003b15 1607}
1da177e4 1608
998e7a76 1609static int sync_thread_master(void *data)
1da177e4 1610{
998e7a76 1611 struct ip_vs_sync_thread_data *tinfo = data;
f131315f 1612 struct netns_ipvs *ipvs = net_ipvs(tinfo->net);
f73181c8 1613 struct ipvs_master_sync_state *ms = &ipvs->ms[tinfo->id];
1c003b15 1614 struct sock *sk = tinfo->sock->sk;
1da177e4
LT
1615 struct ip_vs_sync_buff *sb;
1616
1e3e238e 1617 pr_info("sync thread started: state = MASTER, mcast_ifn = %s, "
f73181c8
PNA
1618 "syncid = %d, id = %d\n",
1619 ipvs->master_mcast_ifn, ipvs->master_syncid, tinfo->id);
1da177e4 1620
1c003b15 1621 for (;;) {
f73181c8 1622 sb = next_sync_buff(ipvs, ms);
1c003b15
PNA
1623 if (unlikely(kthread_should_stop()))
1624 break;
1625 if (!sb) {
1626 schedule_timeout(IPVS_SYNC_CHECK_PERIOD);
1627 continue;
1da177e4 1628 }
1c003b15
PNA
1629 while (ip_vs_send_sync_msg(tinfo->sock, sb->mesg) < 0) {
1630 int ret = 0;
1631
1632 __wait_event_interruptible(*sk_sleep(sk),
1633 sock_writeable(sk) ||
1634 kthread_should_stop(),
1635 ret);
1636 if (unlikely(kthread_should_stop()))
1637 goto done;
1da177e4 1638 }
1c003b15 1639 ip_vs_sync_buff_release(sb);
1da177e4
LT
1640 }
1641
1c003b15
PNA
1642done:
1643 __set_current_state(TASK_RUNNING);
1644 if (sb)
1645 ip_vs_sync_buff_release(sb);
1646
1da177e4 1647 /* clean up the sync_buff queue */
f73181c8 1648 while ((sb = sb_dequeue(ipvs, ms)))
1da177e4 1649 ip_vs_sync_buff_release(sb);
1c003b15 1650 __set_current_state(TASK_RUNNING);
1da177e4
LT
1651
1652 /* clean up the current sync_buff */
f73181c8 1653 sb = get_curr_sync_buff(ipvs, ms, 0);
f131315f 1654 if (sb)
1da177e4 1655 ip_vs_sync_buff_release(sb);
1da177e4
LT
1656
1657 /* release the sending multicast socket */
1ae132b0 1658 sk_release_kernel(tinfo->sock->sk);
998e7a76
SW
1659 kfree(tinfo);
1660
1661 return 0;
1da177e4
LT
1662}
1663
1664
998e7a76 1665static int sync_thread_backup(void *data)
1da177e4 1666{
998e7a76 1667 struct ip_vs_sync_thread_data *tinfo = data;
f131315f 1668 struct netns_ipvs *ipvs = net_ipvs(tinfo->net);
1da177e4
LT
1669 int len;
1670
1e3e238e 1671 pr_info("sync thread started: state = BACKUP, mcast_ifn = %s, "
f73181c8
PNA
1672 "syncid = %d, id = %d\n",
1673 ipvs->backup_mcast_ifn, ipvs->backup_syncid, tinfo->id);
1da177e4 1674
998e7a76 1675 while (!kthread_should_stop()) {
aa395145 1676 wait_event_interruptible(*sk_sleep(tinfo->sock->sk),
ba6fd850
SW
1677 !skb_queue_empty(&tinfo->sock->sk->sk_receive_queue)
1678 || kthread_should_stop());
1679
998e7a76
SW
1680 /* do we have data now? */
1681 while (!skb_queue_empty(&(tinfo->sock->sk->sk_receive_queue))) {
1682 len = ip_vs_receive(tinfo->sock, tinfo->buf,
f131315f 1683 ipvs->recv_mesg_maxlen);
998e7a76 1684 if (len <= 0) {
f73181c8
PNA
1685 if (len != -EAGAIN)
1686 pr_err("receiving message error\n");
1da177e4
LT
1687 break;
1688 }
998e7a76 1689
f131315f 1690 ip_vs_process_message(tinfo->net, tinfo->buf, len);
1da177e4 1691 }
1da177e4
LT
1692 }
1693
1694 /* release the sending multicast socket */
1ae132b0 1695 sk_release_kernel(tinfo->sock->sk);
998e7a76
SW
1696 kfree(tinfo->buf);
1697 kfree(tinfo);
1da177e4 1698
998e7a76 1699 return 0;
1da177e4
LT
1700}
1701
1702
f131315f 1703int start_sync_thread(struct net *net, int state, char *mcast_ifn, __u8 syncid)
1da177e4 1704{
998e7a76 1705 struct ip_vs_sync_thread_data *tinfo;
f73181c8 1706 struct task_struct **array = NULL, *task;
998e7a76 1707 struct socket *sock;
f131315f 1708 struct netns_ipvs *ipvs = net_ipvs(net);
f73181c8 1709 char *name;
998e7a76 1710 int (*threadfn)(void *data);
f73181c8 1711 int id, count;
998e7a76 1712 int result = -ENOMEM;
1da177e4 1713
1e3e238e 1714 IP_VS_DBG(7, "%s(): pid %d\n", __func__, task_pid_nr(current));
998e7a76 1715 IP_VS_DBG(7, "Each ip_vs_sync_conn entry needs %Zd bytes\n",
2981bc9a 1716 sizeof(struct ip_vs_sync_conn_v0));
1da177e4 1717
f73181c8
PNA
1718 if (!ipvs->sync_state) {
1719 count = clamp(sysctl_sync_ports(ipvs), 1, IPVS_SYNC_PORTS_MAX);
1720 ipvs->threads_mask = count - 1;
1721 } else
1722 count = ipvs->threads_mask + 1;
ae1d48b2 1723
998e7a76 1724 if (state == IP_VS_STATE_MASTER) {
f73181c8 1725 if (ipvs->ms)
998e7a76 1726 return -EEXIST;
1da177e4 1727
f131315f
HS
1728 strlcpy(ipvs->master_mcast_ifn, mcast_ifn,
1729 sizeof(ipvs->master_mcast_ifn));
1730 ipvs->master_syncid = syncid;
f73181c8 1731 name = "ipvs-m:%d:%d";
998e7a76 1732 threadfn = sync_thread_master;
998e7a76 1733 } else if (state == IP_VS_STATE_BACKUP) {
f73181c8 1734 if (ipvs->backup_threads)
998e7a76
SW
1735 return -EEXIST;
1736
f131315f
HS
1737 strlcpy(ipvs->backup_mcast_ifn, mcast_ifn,
1738 sizeof(ipvs->backup_mcast_ifn));
1739 ipvs->backup_syncid = syncid;
f73181c8 1740 name = "ipvs-b:%d:%d";
998e7a76 1741 threadfn = sync_thread_backup;
1da177e4 1742 } else {
1da177e4
LT
1743 return -EINVAL;
1744 }
1745
f73181c8
PNA
1746 if (state == IP_VS_STATE_MASTER) {
1747 struct ipvs_master_sync_state *ms;
1da177e4 1748
f73181c8
PNA
1749 ipvs->ms = kzalloc(count * sizeof(ipvs->ms[0]), GFP_KERNEL);
1750 if (!ipvs->ms)
1751 goto out;
1752 ms = ipvs->ms;
1753 for (id = 0; id < count; id++, ms++) {
1754 INIT_LIST_HEAD(&ms->sync_queue);
1755 ms->sync_queue_len = 0;
1756 ms->sync_queue_delay = 0;
1757 INIT_DELAYED_WORK(&ms->master_wakeup_work,
1758 master_wakeup_work_handler);
1759 ms->ipvs = ipvs;
1760 }
1761 } else {
1762 array = kzalloc(count * sizeof(struct task_struct *),
1763 GFP_KERNEL);
1764 if (!array)
1765 goto out;
998e7a76 1766 }
f73181c8 1767 set_sync_mesg_maxlen(net, state);
1da177e4 1768
f73181c8
PNA
1769 tinfo = NULL;
1770 for (id = 0; id < count; id++) {
1771 if (state == IP_VS_STATE_MASTER)
1772 sock = make_send_sock(net, id);
1773 else
1774 sock = make_receive_sock(net, id);
1775 if (IS_ERR(sock)) {
1776 result = PTR_ERR(sock);
1777 goto outtinfo;
1778 }
1779 tinfo = kmalloc(sizeof(*tinfo), GFP_KERNEL);
1780 if (!tinfo)
1781 goto outsocket;
1782 tinfo->net = net;
1783 tinfo->sock = sock;
1784 if (state == IP_VS_STATE_BACKUP) {
1785 tinfo->buf = kmalloc(ipvs->recv_mesg_maxlen,
1786 GFP_KERNEL);
1787 if (!tinfo->buf)
1788 goto outtinfo;
b425df4c
DC
1789 } else {
1790 tinfo->buf = NULL;
f73181c8
PNA
1791 }
1792 tinfo->id = id;
1da177e4 1793
f73181c8
PNA
1794 task = kthread_run(threadfn, tinfo, name, ipvs->gen, id);
1795 if (IS_ERR(task)) {
1796 result = PTR_ERR(task);
1797 goto outtinfo;
1798 }
1799 tinfo = NULL;
1800 if (state == IP_VS_STATE_MASTER)
1801 ipvs->ms[id].master_thread = task;
1802 else
1803 array[id] = task;
998e7a76 1804 }
1da177e4 1805
998e7a76 1806 /* mark as active */
f73181c8
PNA
1807
1808 if (state == IP_VS_STATE_BACKUP)
1809 ipvs->backup_threads = array;
1810 spin_lock_bh(&ipvs->sync_buff_lock);
f131315f 1811 ipvs->sync_state |= state;
f73181c8 1812 spin_unlock_bh(&ipvs->sync_buff_lock);
1da177e4 1813
998e7a76
SW
1814 /* increase the module use count */
1815 ip_vs_use_count_inc();
1da177e4
LT
1816
1817 return 0;
1da177e4 1818
998e7a76 1819outsocket:
1ae132b0 1820 sk_release_kernel(sock->sk);
f73181c8
PNA
1821
1822outtinfo:
1823 if (tinfo) {
1824 sk_release_kernel(tinfo->sock->sk);
1825 kfree(tinfo->buf);
1826 kfree(tinfo);
1827 }
1828 count = id;
1829 while (count-- > 0) {
1830 if (state == IP_VS_STATE_MASTER)
1831 kthread_stop(ipvs->ms[count].master_thread);
1832 else
1833 kthread_stop(array[count]);
1834 }
1835 kfree(array);
1836
998e7a76 1837out:
f73181c8
PNA
1838 if (!(ipvs->sync_state & IP_VS_STATE_MASTER)) {
1839 kfree(ipvs->ms);
1840 ipvs->ms = NULL;
1841 }
998e7a76 1842 return result;
1da177e4
LT
1843}
1844
1845
f131315f 1846int stop_sync_thread(struct net *net, int state)
1da177e4 1847{
f131315f 1848 struct netns_ipvs *ipvs = net_ipvs(net);
f73181c8
PNA
1849 struct task_struct **array;
1850 int id;
1ae132b0 1851 int retc = -EINVAL;
f131315f 1852
1e3e238e 1853 IP_VS_DBG(7, "%s(): pid %d\n", __func__, task_pid_nr(current));
1da177e4 1854
1da177e4 1855 if (state == IP_VS_STATE_MASTER) {
f73181c8 1856 if (!ipvs->ms)
998e7a76 1857 return -ESRCH;
cc0191ae 1858
998e7a76
SW
1859 /*
1860 * The lock synchronizes with sb_queue_tail(), so that we don't
1861 * add sync buffers to the queue, when we are already in
1862 * progress of stopping the master sync daemon.
1863 */
1da177e4 1864
f73181c8
PNA
1865 spin_lock_bh(&ipvs->sync_buff_lock);
1866 spin_lock(&ipvs->sync_lock);
f131315f 1867 ipvs->sync_state &= ~IP_VS_STATE_MASTER;
f73181c8
PNA
1868 spin_unlock(&ipvs->sync_lock);
1869 spin_unlock_bh(&ipvs->sync_buff_lock);
1870
1871 retc = 0;
1872 for (id = ipvs->threads_mask; id >= 0; id--) {
1873 struct ipvs_master_sync_state *ms = &ipvs->ms[id];
1874 int ret;
1875
1876 pr_info("stopping master sync thread %d ...\n",
1877 task_pid_nr(ms->master_thread));
1878 cancel_delayed_work_sync(&ms->master_wakeup_work);
1879 ret = kthread_stop(ms->master_thread);
1880 if (retc >= 0)
1881 retc = ret;
1882 }
1883 kfree(ipvs->ms);
1884 ipvs->ms = NULL;
998e7a76 1885 } else if (state == IP_VS_STATE_BACKUP) {
f73181c8 1886 if (!ipvs->backup_threads)
998e7a76
SW
1887 return -ESRCH;
1888
f131315f 1889 ipvs->sync_state &= ~IP_VS_STATE_BACKUP;
f73181c8
PNA
1890 array = ipvs->backup_threads;
1891 retc = 0;
1892 for (id = ipvs->threads_mask; id >= 0; id--) {
1893 int ret;
1894
1895 pr_info("stopping backup sync thread %d ...\n",
1896 task_pid_nr(array[id]));
1897 ret = kthread_stop(array[id]);
1898 if (retc >= 0)
1899 retc = ret;
1900 }
1901 kfree(array);
1902 ipvs->backup_threads = NULL;
998e7a76 1903 }
1da177e4 1904
998e7a76
SW
1905 /* decrease the module use count */
1906 ip_vs_use_count_dec();
1da177e4 1907
1ae132b0 1908 return retc;
1da177e4 1909}
61b1ab45
HS
1910
1911/*
1912 * Initialize data struct for each netns
1913 */
503cf15a 1914int __net_init ip_vs_sync_net_init(struct net *net)
61b1ab45 1915{
f131315f
HS
1916 struct netns_ipvs *ipvs = net_ipvs(net);
1917
ae1d48b2 1918 __mutex_init(&ipvs->sync_mutex, "ipvs->sync_mutex", &__ipvs_sync_key);
f131315f
HS
1919 spin_lock_init(&ipvs->sync_lock);
1920 spin_lock_init(&ipvs->sync_buff_lock);
61b1ab45
HS
1921 return 0;
1922}
1923
503cf15a 1924void ip_vs_sync_net_cleanup(struct net *net)
61b1ab45 1925{
1ae132b0 1926 int retc;
ae1d48b2 1927 struct netns_ipvs *ipvs = net_ipvs(net);
1ae132b0 1928
ae1d48b2 1929 mutex_lock(&ipvs->sync_mutex);
1ae132b0
HS
1930 retc = stop_sync_thread(net, IP_VS_STATE_MASTER);
1931 if (retc && retc != -ESRCH)
1932 pr_err("Failed to stop Master Daemon\n");
1933
1934 retc = stop_sync_thread(net, IP_VS_STATE_BACKUP);
1935 if (retc && retc != -ESRCH)
1936 pr_err("Failed to stop Backup Daemon\n");
ae1d48b2 1937 mutex_unlock(&ipvs->sync_mutex);
61b1ab45 1938}