ipvs: Pass ipvs not net to ip_vs_sync_conn
[linux-2.6-block.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 {
37b68e6d 196 struct netns_ipvs *ipvs;
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
d3328817
JA
265union ipvs_sockaddr {
266 struct sockaddr_in in;
267 struct sockaddr_in6 in6;
268};
269
1da177e4
LT
270struct ip_vs_sync_buff {
271 struct list_head list;
272 unsigned long firstuse;
273
274 /* pointers for the message data */
275 struct ip_vs_sync_mesg *mesg;
276 unsigned char *head;
277 unsigned char *end;
278};
279
fe5e7a1e
HS
280/*
281 * Copy of struct ip_vs_seq
282 * From unaligned network order to aligned host order
283 */
284static void ntoh_seq(struct ip_vs_seq *no, struct ip_vs_seq *ho)
285{
286 ho->init_seq = get_unaligned_be32(&no->init_seq);
287 ho->delta = get_unaligned_be32(&no->delta);
288 ho->previous_delta = get_unaligned_be32(&no->previous_delta);
289}
1da177e4 290
986a0757
HS
291/*
292 * Copy of struct ip_vs_seq
293 * From Aligned host order to unaligned network order
294 */
295static void hton_seq(struct ip_vs_seq *ho, struct ip_vs_seq *no)
296{
297 put_unaligned_be32(ho->init_seq, &no->init_seq);
298 put_unaligned_be32(ho->delta, &no->delta);
299 put_unaligned_be32(ho->previous_delta, &no->previous_delta);
300}
301
f73181c8
PNA
302static inline struct ip_vs_sync_buff *
303sb_dequeue(struct netns_ipvs *ipvs, struct ipvs_master_sync_state *ms)
1da177e4
LT
304{
305 struct ip_vs_sync_buff *sb;
306
f131315f 307 spin_lock_bh(&ipvs->sync_lock);
f73181c8 308 if (list_empty(&ms->sync_queue)) {
1da177e4 309 sb = NULL;
1c003b15 310 __set_current_state(TASK_INTERRUPTIBLE);
1da177e4 311 } else {
f73181c8 312 sb = list_entry(ms->sync_queue.next, struct ip_vs_sync_buff,
1da177e4
LT
313 list);
314 list_del(&sb->list);
f73181c8
PNA
315 ms->sync_queue_len--;
316 if (!ms->sync_queue_len)
317 ms->sync_queue_delay = 0;
1da177e4 318 }
f131315f 319 spin_unlock_bh(&ipvs->sync_lock);
1da177e4
LT
320
321 return sb;
322}
323
986a0757
HS
324/*
325 * Create a new sync buffer for Version 1 proto.
326 */
f131315f 327static inline struct ip_vs_sync_buff *
e4ff6751 328ip_vs_sync_buff_create(struct netns_ipvs *ipvs, unsigned int len)
1da177e4
LT
329{
330 struct ip_vs_sync_buff *sb;
331
332 if (!(sb=kmalloc(sizeof(struct ip_vs_sync_buff), GFP_ATOMIC)))
333 return NULL;
334
e4ff6751
JA
335 len = max_t(unsigned int, len + sizeof(struct ip_vs_sync_mesg),
336 ipvs->mcfg.sync_maxlen);
337 sb->mesg = kmalloc(len, GFP_ATOMIC);
f131315f 338 if (!sb->mesg) {
1da177e4
LT
339 kfree(sb);
340 return NULL;
341 }
f73181c8 342 sb->mesg->reserved = 0; /* old nr_conns i.e. must be zero now */
986a0757 343 sb->mesg->version = SYNC_PROTO_VER;
e4ff6751 344 sb->mesg->syncid = ipvs->mcfg.syncid;
38561437 345 sb->mesg->size = htons(sizeof(struct ip_vs_sync_mesg));
986a0757
HS
346 sb->mesg->nr_conns = 0;
347 sb->mesg->spare = 0;
348 sb->head = (unsigned char *)sb->mesg + sizeof(struct ip_vs_sync_mesg);
e4ff6751 349 sb->end = (unsigned char *)sb->mesg + len;
986a0757 350
1da177e4
LT
351 sb->firstuse = jiffies;
352 return sb;
353}
354
355static inline void ip_vs_sync_buff_release(struct ip_vs_sync_buff *sb)
356{
357 kfree(sb->mesg);
358 kfree(sb);
359}
360
f73181c8
PNA
361static inline void sb_queue_tail(struct netns_ipvs *ipvs,
362 struct ipvs_master_sync_state *ms)
998e7a76 363{
f73181c8 364 struct ip_vs_sync_buff *sb = ms->sync_buff;
f131315f
HS
365
366 spin_lock(&ipvs->sync_lock);
1c003b15 367 if (ipvs->sync_state & IP_VS_STATE_MASTER &&
f73181c8
PNA
368 ms->sync_queue_len < sysctl_sync_qlen_max(ipvs)) {
369 if (!ms->sync_queue_len)
370 schedule_delayed_work(&ms->master_wakeup_work,
1c003b15 371 max(IPVS_SYNC_SEND_DELAY, 1));
f73181c8
PNA
372 ms->sync_queue_len++;
373 list_add_tail(&sb->list, &ms->sync_queue);
374 if ((++ms->sync_queue_delay) == IPVS_SYNC_WAKEUP_RATE)
375 wake_up_process(ms->master_thread);
1c003b15 376 } else
998e7a76 377 ip_vs_sync_buff_release(sb);
f131315f 378 spin_unlock(&ipvs->sync_lock);
998e7a76
SW
379}
380
1da177e4
LT
381/*
382 * Get the current sync buffer if it has been created for more
383 * than the specified time or the specified time is zero.
384 */
385static inline struct ip_vs_sync_buff *
f73181c8
PNA
386get_curr_sync_buff(struct netns_ipvs *ipvs, struct ipvs_master_sync_state *ms,
387 unsigned long time)
1da177e4
LT
388{
389 struct ip_vs_sync_buff *sb;
390
f131315f 391 spin_lock_bh(&ipvs->sync_buff_lock);
f73181c8
PNA
392 sb = ms->sync_buff;
393 if (sb && time_after_eq(jiffies - sb->firstuse, time)) {
394 ms->sync_buff = NULL;
1c003b15 395 __set_current_state(TASK_RUNNING);
1da177e4
LT
396 } else
397 sb = NULL;
f131315f 398 spin_unlock_bh(&ipvs->sync_buff_lock);
1da177e4
LT
399 return sb;
400}
401
f73181c8
PNA
402static inline int
403select_master_thread_id(struct netns_ipvs *ipvs, struct ip_vs_conn *cp)
f131315f 404{
f73181c8 405 return ((long) cp >> (1 + ilog2(sizeof(*cp)))) & ipvs->threads_mask;
b880c1f0
HS
406}
407
408/*
409 * Create a new sync buffer for Version 0 proto.
410 */
f131315f 411static inline struct ip_vs_sync_buff *
e4ff6751 412ip_vs_sync_buff_create_v0(struct netns_ipvs *ipvs, unsigned int len)
b880c1f0
HS
413{
414 struct ip_vs_sync_buff *sb;
415 struct ip_vs_sync_mesg_v0 *mesg;
416
417 if (!(sb=kmalloc(sizeof(struct ip_vs_sync_buff), GFP_ATOMIC)))
418 return NULL;
419
e4ff6751
JA
420 len = max_t(unsigned int, len + sizeof(struct ip_vs_sync_mesg_v0),
421 ipvs->mcfg.sync_maxlen);
422 sb->mesg = kmalloc(len, GFP_ATOMIC);
f131315f 423 if (!sb->mesg) {
b880c1f0
HS
424 kfree(sb);
425 return NULL;
426 }
427 mesg = (struct ip_vs_sync_mesg_v0 *)sb->mesg;
428 mesg->nr_conns = 0;
e4ff6751 429 mesg->syncid = ipvs->mcfg.syncid;
38561437 430 mesg->size = htons(sizeof(struct ip_vs_sync_mesg_v0));
f131315f 431 sb->head = (unsigned char *)mesg + sizeof(struct ip_vs_sync_mesg_v0);
e4ff6751 432 sb->end = (unsigned char *)mesg + len;
b880c1f0
HS
433 sb->firstuse = jiffies;
434 return sb;
435}
436
4d0c875d
JA
437/* Check if connection is controlled by persistence */
438static inline bool in_persistence(struct ip_vs_conn *cp)
439{
440 for (cp = cp->control; cp; cp = cp->control) {
441 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
442 return true;
443 }
444 return false;
445}
446
749c42b6
JA
447/* Check if conn should be synced.
448 * pkts: conn packets, use sysctl_sync_threshold to avoid packet check
449 * - (1) sync_refresh_period: reduce sync rate. Additionally, retry
450 * sync_retries times with period of sync_refresh_period/8
451 * - (2) if both sync_refresh_period and sync_period are 0 send sync only
452 * for state changes or only once when pkts matches sync_threshold
453 * - (3) templates: rate can be reduced only with sync_refresh_period or
454 * with (2)
455 */
456static int ip_vs_sync_conn_needed(struct netns_ipvs *ipvs,
457 struct ip_vs_conn *cp, int pkts)
458{
459 unsigned long orig = ACCESS_ONCE(cp->sync_endtime);
460 unsigned long now = jiffies;
461 unsigned long n = (now + cp->timeout) & ~3UL;
462 unsigned int sync_refresh_period;
463 int sync_period;
464 int force;
465
466 /* Check if we sync in current state */
467 if (unlikely(cp->flags & IP_VS_CONN_F_TEMPLATE))
468 force = 0;
4d0c875d
JA
469 else if (unlikely(sysctl_sync_persist_mode(ipvs) && in_persistence(cp)))
470 return 0;
749c42b6
JA
471 else if (likely(cp->protocol == IPPROTO_TCP)) {
472 if (!((1 << cp->state) &
473 ((1 << IP_VS_TCP_S_ESTABLISHED) |
474 (1 << IP_VS_TCP_S_FIN_WAIT) |
475 (1 << IP_VS_TCP_S_CLOSE) |
476 (1 << IP_VS_TCP_S_CLOSE_WAIT) |
477 (1 << IP_VS_TCP_S_TIME_WAIT))))
478 return 0;
479 force = cp->state != cp->old_state;
480 if (force && cp->state != IP_VS_TCP_S_ESTABLISHED)
481 goto set;
482 } else if (unlikely(cp->protocol == IPPROTO_SCTP)) {
483 if (!((1 << cp->state) &
484 ((1 << IP_VS_SCTP_S_ESTABLISHED) |
61e7c420
JA
485 (1 << IP_VS_SCTP_S_SHUTDOWN_SENT) |
486 (1 << IP_VS_SCTP_S_SHUTDOWN_RECEIVED) |
487 (1 << IP_VS_SCTP_S_SHUTDOWN_ACK_SENT) |
488 (1 << IP_VS_SCTP_S_CLOSED))))
749c42b6
JA
489 return 0;
490 force = cp->state != cp->old_state;
491 if (force && cp->state != IP_VS_SCTP_S_ESTABLISHED)
492 goto set;
493 } else {
494 /* UDP or another protocol with single state */
495 force = 0;
496 }
497
498 sync_refresh_period = sysctl_sync_refresh_period(ipvs);
499 if (sync_refresh_period > 0) {
500 long diff = n - orig;
501 long min_diff = max(cp->timeout >> 1, 10UL * HZ);
502
503 /* Avoid sync if difference is below sync_refresh_period
504 * and below the half timeout.
505 */
506 if (abs(diff) < min_t(long, sync_refresh_period, min_diff)) {
507 int retries = orig & 3;
508
509 if (retries >= sysctl_sync_retries(ipvs))
510 return 0;
511 if (time_before(now, orig - cp->timeout +
512 (sync_refresh_period >> 3)))
513 return 0;
514 n |= retries + 1;
515 }
516 }
517 sync_period = sysctl_sync_period(ipvs);
518 if (sync_period > 0) {
519 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE) &&
520 pkts % sync_period != sysctl_sync_threshold(ipvs))
521 return 0;
522 } else if (sync_refresh_period <= 0 &&
523 pkts != sysctl_sync_threshold(ipvs))
524 return 0;
525
526set:
527 cp->old_state = cp->state;
528 n = cmpxchg(&cp->sync_endtime, orig, n);
529 return n == orig || force;
530}
531
b880c1f0
HS
532/*
533 * Version 0 , could be switched in by sys_ctl.
534 * Add an ip_vs_conn information into the current sync_buff.
535 */
72e9481e 536static void ip_vs_sync_conn_v0(struct netns_ipvs *ipvs, struct ip_vs_conn *cp,
749c42b6 537 int pkts)
b880c1f0
HS
538{
539 struct ip_vs_sync_mesg_v0 *m;
540 struct ip_vs_sync_conn_v0 *s;
f73181c8
PNA
541 struct ip_vs_sync_buff *buff;
542 struct ipvs_master_sync_state *ms;
543 int id;
e4ff6751 544 unsigned int len;
b880c1f0
HS
545
546 if (unlikely(cp->af != AF_INET))
547 return;
548 /* Do not sync ONE PACKET */
549 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
550 return;
551
749c42b6
JA
552 if (!ip_vs_sync_conn_needed(ipvs, cp, pkts))
553 return;
554
ac69269a 555 spin_lock_bh(&ipvs->sync_buff_lock);
f73181c8 556 if (!(ipvs->sync_state & IP_VS_STATE_MASTER)) {
ac69269a 557 spin_unlock_bh(&ipvs->sync_buff_lock);
f73181c8
PNA
558 return;
559 }
560
561 id = select_master_thread_id(ipvs, cp);
562 ms = &ipvs->ms[id];
563 buff = ms->sync_buff;
e4ff6751
JA
564 len = (cp->flags & IP_VS_CONN_F_SEQ_MASK) ? FULL_CONN_SIZE :
565 SIMPLE_CONN_SIZE;
f73181c8
PNA
566 if (buff) {
567 m = (struct ip_vs_sync_mesg_v0 *) buff->mesg;
568 /* Send buffer if it is for v1 */
e4ff6751 569 if (buff->head + len > buff->end || !m->nr_conns) {
f73181c8
PNA
570 sb_queue_tail(ipvs, ms);
571 ms->sync_buff = NULL;
572 buff = NULL;
573 }
574 }
575 if (!buff) {
e4ff6751 576 buff = ip_vs_sync_buff_create_v0(ipvs, len);
f73181c8 577 if (!buff) {
ac69269a 578 spin_unlock_bh(&ipvs->sync_buff_lock);
b880c1f0
HS
579 pr_err("ip_vs_sync_buff_create failed.\n");
580 return;
581 }
f73181c8 582 ms->sync_buff = buff;
b880c1f0
HS
583 }
584
f73181c8
PNA
585 m = (struct ip_vs_sync_mesg_v0 *) buff->mesg;
586 s = (struct ip_vs_sync_conn_v0 *) buff->head;
b880c1f0
HS
587
588 /* copy members */
589 s->reserved = 0;
590 s->protocol = cp->protocol;
591 s->cport = cp->cport;
592 s->vport = cp->vport;
593 s->dport = cp->dport;
594 s->caddr = cp->caddr.ip;
595 s->vaddr = cp->vaddr.ip;
596 s->daddr = cp->daddr.ip;
597 s->flags = htons(cp->flags & ~IP_VS_CONN_F_HASHED);
598 s->state = htons(cp->state);
599 if (cp->flags & IP_VS_CONN_F_SEQ_MASK) {
600 struct ip_vs_sync_conn_options *opt =
601 (struct ip_vs_sync_conn_options *)&s[1];
602 memcpy(opt, &cp->in_seq, sizeof(*opt));
603 }
604
605 m->nr_conns++;
38561437 606 m->size = htons(ntohs(m->size) + len);
f73181c8 607 buff->head += len;
ac69269a 608 spin_unlock_bh(&ipvs->sync_buff_lock);
b880c1f0
HS
609
610 /* synchronize its controller if it has */
749c42b6
JA
611 cp = cp->control;
612 if (cp) {
613 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
614 pkts = atomic_add_return(1, &cp->in_pkts);
615 else
616 pkts = sysctl_sync_threshold(ipvs);
b61a8c1a 617 ip_vs_sync_conn(ipvs, cp, pkts);
749c42b6 618 }
b880c1f0
HS
619}
620
1da177e4
LT
621/*
622 * Add an ip_vs_conn information into the current sync_buff.
623 * Called by ip_vs_in.
986a0757 624 * Sending Version 1 messages
1da177e4 625 */
b61a8c1a 626void ip_vs_sync_conn(struct netns_ipvs *ipvs, struct ip_vs_conn *cp, int pkts)
1da177e4
LT
627{
628 struct ip_vs_sync_mesg *m;
986a0757 629 union ip_vs_sync_conn *s;
f73181c8
PNA
630 struct ip_vs_sync_buff *buff;
631 struct ipvs_master_sync_state *ms;
632 int id;
986a0757
HS
633 __u8 *p;
634 unsigned int len, pe_name_len, pad;
635
b880c1f0 636 /* Handle old version of the protocol */
7532e8d4 637 if (sysctl_sync_ver(ipvs) == 0) {
72e9481e 638 ip_vs_sync_conn_v0(ipvs, cp, pkts);
b880c1f0
HS
639 return;
640 }
986a0757
HS
641 /* Do not sync ONE PACKET */
642 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
643 goto control;
644sloop:
749c42b6
JA
645 if (!ip_vs_sync_conn_needed(ipvs, cp, pkts))
646 goto control;
647
986a0757
HS
648 /* Sanity checks */
649 pe_name_len = 0;
650 if (cp->pe_data_len) {
651 if (!cp->pe_data || !cp->dest) {
652 IP_VS_ERR_RL("SYNC, connection pe_data invalid\n");
653 return;
654 }
655 pe_name_len = strnlen(cp->pe->name, IP_VS_PENAME_MAXLEN);
656 }
1da177e4 657
ac69269a 658 spin_lock_bh(&ipvs->sync_buff_lock);
f73181c8 659 if (!(ipvs->sync_state & IP_VS_STATE_MASTER)) {
ac69269a 660 spin_unlock_bh(&ipvs->sync_buff_lock);
f73181c8
PNA
661 return;
662 }
663
664 id = select_master_thread_id(ipvs, cp);
665 ms = &ipvs->ms[id];
986a0757
HS
666
667#ifdef CONFIG_IP_VS_IPV6
668 if (cp->af == AF_INET6)
669 len = sizeof(struct ip_vs_sync_v6);
670 else
671#endif
672 len = sizeof(struct ip_vs_sync_v4);
673
674 if (cp->flags & IP_VS_CONN_F_SEQ_MASK)
675 len += sizeof(struct ip_vs_sync_conn_options) + 2;
676
677 if (cp->pe_data_len)
678 len += cp->pe_data_len + 2; /* + Param hdr field */
679 if (pe_name_len)
680 len += pe_name_len + 2;
681
682 /* check if there is a space for this one */
683 pad = 0;
f73181c8
PNA
684 buff = ms->sync_buff;
685 if (buff) {
686 m = buff->mesg;
687 pad = (4 - (size_t) buff->head) & 3;
688 /* Send buffer if it is for v0 */
689 if (buff->head + len + pad > buff->end || m->reserved) {
690 sb_queue_tail(ipvs, ms);
691 ms->sync_buff = NULL;
692 buff = NULL;
986a0757
HS
693 pad = 0;
694 }
695 }
696
f73181c8 697 if (!buff) {
e4ff6751 698 buff = ip_vs_sync_buff_create(ipvs, len);
f73181c8 699 if (!buff) {
ac69269a 700 spin_unlock_bh(&ipvs->sync_buff_lock);
1e3e238e 701 pr_err("ip_vs_sync_buff_create failed.\n");
1da177e4
LT
702 return;
703 }
f73181c8
PNA
704 ms->sync_buff = buff;
705 m = buff->mesg;
1da177e4
LT
706 }
707
f73181c8
PNA
708 p = buff->head;
709 buff->head += pad + len;
38561437 710 m->size = htons(ntohs(m->size) + pad + len);
986a0757
HS
711 /* Add ev. padding from prev. sync_conn */
712 while (pad--)
713 *(p++) = 0;
714
715 s = (union ip_vs_sync_conn *)p;
716
717 /* Set message type & copy members */
718 s->v4.type = (cp->af == AF_INET6 ? STYPE_F_INET6 : 0);
719 s->v4.ver_size = htons(len & SVER_MASK); /* Version 0 */
720 s->v4.flags = htonl(cp->flags & ~IP_VS_CONN_F_HASHED);
721 s->v4.state = htons(cp->state);
722 s->v4.protocol = cp->protocol;
723 s->v4.cport = cp->cport;
724 s->v4.vport = cp->vport;
725 s->v4.dport = cp->dport;
726 s->v4.fwmark = htonl(cp->fwmark);
727 s->v4.timeout = htonl(cp->timeout / HZ);
1da177e4 728 m->nr_conns++;
1da177e4 729
986a0757
HS
730#ifdef CONFIG_IP_VS_IPV6
731 if (cp->af == AF_INET6) {
732 p += sizeof(struct ip_vs_sync_v6);
4e3fd7a0
AD
733 s->v6.caddr = cp->caddr.in6;
734 s->v6.vaddr = cp->vaddr.in6;
735 s->v6.daddr = cp->daddr.in6;
986a0757
HS
736 } else
737#endif
738 {
739 p += sizeof(struct ip_vs_sync_v4); /* options ptr */
740 s->v4.caddr = cp->caddr.ip;
741 s->v4.vaddr = cp->vaddr.ip;
742 s->v4.daddr = cp->daddr.ip;
743 }
744 if (cp->flags & IP_VS_CONN_F_SEQ_MASK) {
745 *(p++) = IPVS_OPT_SEQ_DATA;
746 *(p++) = sizeof(struct ip_vs_sync_conn_options);
747 hton_seq((struct ip_vs_seq *)p, &cp->in_seq);
748 p += sizeof(struct ip_vs_seq);
749 hton_seq((struct ip_vs_seq *)p, &cp->out_seq);
750 p += sizeof(struct ip_vs_seq);
1da177e4 751 }
986a0757
HS
752 /* Handle pe data */
753 if (cp->pe_data_len && cp->pe_data) {
754 *(p++) = IPVS_OPT_PE_DATA;
755 *(p++) = cp->pe_data_len;
756 memcpy(p, cp->pe_data, cp->pe_data_len);
757 p += cp->pe_data_len;
758 if (pe_name_len) {
759 /* Add PE_NAME */
760 *(p++) = IPVS_OPT_PE_NAME;
761 *(p++) = pe_name_len;
762 memcpy(p, cp->pe->name, pe_name_len);
763 p += pe_name_len;
764 }
765 }
766
ac69269a 767 spin_unlock_bh(&ipvs->sync_buff_lock);
1da177e4 768
986a0757 769control:
1da177e4 770 /* synchronize its controller if it has */
986a0757
HS
771 cp = cp->control;
772 if (!cp)
773 return;
749c42b6
JA
774 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
775 pkts = atomic_add_return(1, &cp->in_pkts);
776 else
777 pkts = sysctl_sync_threshold(ipvs);
986a0757 778 goto sloop;
1da177e4
LT
779}
780
fe5e7a1e
HS
781/*
782 * fill_param used by version 1
783 */
85999283 784static inline int
19913dec 785ip_vs_conn_fill_param_sync(struct netns_ipvs *ipvs, int af, union ip_vs_sync_conn *sc,
fe5e7a1e
HS
786 struct ip_vs_conn_param *p,
787 __u8 *pe_data, unsigned int pe_data_len,
788 __u8 *pe_name, unsigned int pe_name_len)
85999283 789{
fe5e7a1e
HS
790#ifdef CONFIG_IP_VS_IPV6
791 if (af == AF_INET6)
19913dec 792 ip_vs_conn_fill_param(ipvs, af, sc->v6.protocol,
fe5e7a1e
HS
793 (const union nf_inet_addr *)&sc->v6.caddr,
794 sc->v6.cport,
795 (const union nf_inet_addr *)&sc->v6.vaddr,
796 sc->v6.vport, p);
797 else
798#endif
19913dec 799 ip_vs_conn_fill_param(ipvs, af, sc->v4.protocol,
fe5e7a1e
HS
800 (const union nf_inet_addr *)&sc->v4.caddr,
801 sc->v4.cport,
802 (const union nf_inet_addr *)&sc->v4.vaddr,
803 sc->v4.vport, p);
804 /* Handle pe data */
805 if (pe_data_len) {
806 if (pe_name_len) {
807 char buff[IP_VS_PENAME_MAXLEN+1];
808
809 memcpy(buff, pe_name, pe_name_len);
810 buff[pe_name_len]=0;
811 p->pe = __ip_vs_pe_getbyname(buff);
812 if (!p->pe) {
f131315f
HS
813 IP_VS_DBG(3, "BACKUP, no %s engine found/loaded\n",
814 buff);
fe5e7a1e
HS
815 return 1;
816 }
817 } else {
818 IP_VS_ERR_RL("BACKUP, Invalid PE parameters\n");
819 return 1;
820 }
821
6060c74a 822 p->pe_data = kmemdup(pe_data, pe_data_len, GFP_ATOMIC);
fe5e7a1e 823 if (!p->pe_data) {
982f4051 824 module_put(p->pe->module);
fe5e7a1e
HS
825 return -ENOMEM;
826 }
fe5e7a1e
HS
827 p->pe_data_len = pe_data_len;
828 }
85999283
SH
829 return 0;
830}
1da177e4
LT
831
832/*
fe5e7a1e
HS
833 * Connection Add / Update.
834 * Common for version 0 and 1 reception of backup sync_conns.
835 * Param: ...
836 * timeout is in sec.
1da177e4 837 */
9bbac6a9
HS
838static void ip_vs_proc_conn(struct net *net, struct ip_vs_conn_param *param,
839 unsigned int flags, unsigned int state,
840 unsigned int protocol, unsigned int type,
fe5e7a1e
HS
841 const union nf_inet_addr *daddr, __be16 dport,
842 unsigned long timeout, __u32 fwmark,
9bbac6a9 843 struct ip_vs_sync_conn_options *opt)
fe5e7a1e
HS
844{
845 struct ip_vs_dest *dest;
846 struct ip_vs_conn *cp;
a0840e2e 847 struct netns_ipvs *ipvs = net_ipvs(net);
fe5e7a1e 848
d752c364 849 if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
fe5e7a1e 850 cp = ip_vs_conn_in_get(param);
d752c364
MRL
851 if (cp && ((cp->dport != dport) ||
852 !ip_vs_addr_equal(cp->daf, &cp->daddr, daddr))) {
853 if (!(flags & IP_VS_CONN_F_INACTIVE)) {
854 ip_vs_conn_expire_now(cp);
855 __ip_vs_conn_put(cp);
856 cp = NULL;
857 } else {
858 /* This is the expiration message for the
859 * connection that was already replaced, so we
860 * just ignore it.
861 */
862 __ip_vs_conn_put(cp);
863 kfree(param->pe_data);
864 return;
865 }
866 }
867 } else {
fe5e7a1e 868 cp = ip_vs_ct_in_get(param);
d752c364 869 }
fe5e7a1e 870
cdcc5e90
JA
871 if (cp) {
872 /* Free pe_data */
fe5e7a1e 873 kfree(param->pe_data);
cdcc5e90
JA
874
875 dest = cp->dest;
ac69269a 876 spin_lock_bh(&cp->lock);
cdcc5e90
JA
877 if ((cp->flags ^ flags) & IP_VS_CONN_F_INACTIVE &&
878 !(flags & IP_VS_CONN_F_TEMPLATE) && dest) {
879 if (flags & IP_VS_CONN_F_INACTIVE) {
880 atomic_dec(&dest->activeconns);
881 atomic_inc(&dest->inactconns);
882 } else {
883 atomic_inc(&dest->activeconns);
884 atomic_dec(&dest->inactconns);
885 }
886 }
887 flags &= IP_VS_CONN_F_BACKUP_UPD_MASK;
888 flags |= cp->flags & ~IP_VS_CONN_F_BACKUP_UPD_MASK;
889 cp->flags = flags;
ac69269a 890 spin_unlock_bh(&cp->lock);
413c2d04
JA
891 if (!dest)
892 ip_vs_try_bind_dest(cp);
cdcc5e90 893 } else {
fe5e7a1e
HS
894 /*
895 * Find the appropriate destination for the connection.
896 * If it is not found the connection will remain unbound
897 * but still handled.
898 */
413c2d04 899 rcu_read_lock();
655eef10
AG
900 /* This function is only invoked by the synchronization
901 * code. We do not currently support heterogeneous pools
902 * with synchronization, so we can make the assumption that
903 * the svc_af is the same as the dest_af
904 */
dc2add6f 905 dest = ip_vs_find_dest(ipvs, type, type, daddr, dport,
655eef10
AG
906 param->vaddr, param->vport, protocol,
907 fwmark, flags);
fe5e7a1e 908
ba38528a
AG
909 cp = ip_vs_conn_new(param, type, daddr, dport, flags, dest,
910 fwmark);
413c2d04 911 rcu_read_unlock();
fe5e7a1e 912 if (!cp) {
b734427a 913 kfree(param->pe_data);
fe5e7a1e
HS
914 IP_VS_DBG(2, "BACKUP, add new conn. failed\n");
915 return;
916 }
528c943f
JA
917 if (!(flags & IP_VS_CONN_F_TEMPLATE))
918 kfree(param->pe_data);
fe5e7a1e
HS
919 }
920
921 if (opt)
922 memcpy(&cp->in_seq, opt, sizeof(*opt));
59e0350e 923 atomic_set(&cp->in_pkts, sysctl_sync_threshold(ipvs));
fe5e7a1e
HS
924 cp->state = state;
925 cp->old_state = cp->state;
926 /*
927 * For Ver 0 messages style
928 * - Not possible to recover the right timeout for templates
929 * - can not find the right fwmark
930 * virtual service. If needed, we can do it for
931 * non-fwmark persistent services.
932 * Ver 1 messages style.
933 * - No problem.
934 */
935 if (timeout) {
936 if (timeout > MAX_SCHEDULE_TIMEOUT / HZ)
937 timeout = MAX_SCHEDULE_TIMEOUT / HZ;
938 cp->timeout = timeout*HZ;
9bbac6a9
HS
939 } else {
940 struct ip_vs_proto_data *pd;
941
18d6ade6 942 pd = ip_vs_proto_data_get(ipvs, protocol);
9bbac6a9
HS
943 if (!(flags & IP_VS_CONN_F_TEMPLATE) && pd && pd->timeout_table)
944 cp->timeout = pd->timeout_table[state];
945 else
946 cp->timeout = (3*60*HZ);
947 }
fe5e7a1e
HS
948 ip_vs_conn_put(cp);
949}
950
951/*
952 * Process received multicast message for Version 0
953 */
19913dec 954static void ip_vs_process_message_v0(struct netns_ipvs *ipvs, const char *buffer,
9bbac6a9 955 const size_t buflen)
1da177e4 956{
986a0757 957 struct ip_vs_sync_mesg_v0 *m = (struct ip_vs_sync_mesg_v0 *)buffer;
2981bc9a 958 struct ip_vs_sync_conn_v0 *s;
1da177e4 959 struct ip_vs_sync_conn_options *opt;
5c81833c 960 struct ip_vs_protocol *pp;
f11017ec 961 struct ip_vs_conn_param param;
1da177e4
LT
962 char *p;
963 int i;
964
986a0757 965 p = (char *)buffer + sizeof(struct ip_vs_sync_mesg_v0);
1da177e4 966 for (i=0; i<m->nr_conns; i++) {
95c96174 967 unsigned int flags, state;
87375ab4 968
2ad17def 969 if (p + SIMPLE_CONN_SIZE > buffer+buflen) {
fe5e7a1e 970 IP_VS_ERR_RL("BACKUP v0, bogus conn\n");
2ad17def
JA
971 return;
972 }
2981bc9a 973 s = (struct ip_vs_sync_conn_v0 *) p;
7a4fbb1f 974 flags = ntohs(s->flags) | IP_VS_CONN_F_SYNC;
2ad17def
JA
975 flags &= ~IP_VS_CONN_F_HASHED;
976 if (flags & IP_VS_CONN_F_SEQ_MASK) {
977 opt = (struct ip_vs_sync_conn_options *)&s[1];
978 p += FULL_CONN_SIZE;
979 if (p > buffer+buflen) {
fe5e7a1e 980 IP_VS_ERR_RL("BACKUP v0, Dropping buffer bogus conn options\n");
2ad17def
JA
981 return;
982 }
983 } else {
984 opt = NULL;
985 p += SIMPLE_CONN_SIZE;
986 }
987
b209639e 988 state = ntohs(s->state);
2ad17def
JA
989 if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
990 pp = ip_vs_proto_get(s->protocol);
991 if (!pp) {
fe5e7a1e 992 IP_VS_DBG(2, "BACKUP v0, Unsupported protocol %u\n",
2ad17def
JA
993 s->protocol);
994 continue;
995 }
996 if (state >= pp->num_states) {
fe5e7a1e 997 IP_VS_DBG(2, "BACKUP v0, Invalid %s state %u\n",
2ad17def
JA
998 pp->name, state);
999 continue;
1000 }
1001 } else {
1002 /* protocol in templates is not used for state/timeout */
2ad17def 1003 if (state > 0) {
fe5e7a1e 1004 IP_VS_DBG(2, "BACKUP v0, Invalid template state %u\n",
2ad17def
JA
1005 state);
1006 state = 0;
1007 }
1008 }
1009
19913dec 1010 ip_vs_conn_fill_param(ipvs, AF_INET, s->protocol,
fe5e7a1e
HS
1011 (const union nf_inet_addr *)&s->caddr,
1012 s->cport,
1013 (const union nf_inet_addr *)&s->vaddr,
1014 s->vport, &param);
1015
1016 /* Send timeout as Zero */
19913dec 1017 ip_vs_proc_conn(ipvs->net, &param, flags, state, s->protocol, AF_INET,
fe5e7a1e 1018 (union nf_inet_addr *)&s->daddr, s->dport,
9bbac6a9 1019 0, 0, opt);
fe5e7a1e
HS
1020 }
1021}
1022
1023/*
1024 * Handle options
1025 */
1026static inline int ip_vs_proc_seqopt(__u8 *p, unsigned int plen,
1027 __u32 *opt_flags,
1028 struct ip_vs_sync_conn_options *opt)
1029{
1030 struct ip_vs_sync_conn_options *topt;
1031
1032 topt = (struct ip_vs_sync_conn_options *)p;
1033
1034 if (plen != sizeof(struct ip_vs_sync_conn_options)) {
1035 IP_VS_DBG(2, "BACKUP, bogus conn options length\n");
1036 return -EINVAL;
1037 }
1038 if (*opt_flags & IPVS_OPT_F_SEQ_DATA) {
1039 IP_VS_DBG(2, "BACKUP, conn options found twice\n");
1040 return -EINVAL;
1041 }
1042 ntoh_seq(&topt->in_seq, &opt->in_seq);
1043 ntoh_seq(&topt->out_seq, &opt->out_seq);
1044 *opt_flags |= IPVS_OPT_F_SEQ_DATA;
1045 return 0;
1046}
1047
1048static int ip_vs_proc_str(__u8 *p, unsigned int plen, unsigned int *data_len,
1049 __u8 **data, unsigned int maxlen,
1050 __u32 *opt_flags, __u32 flag)
1051{
1052 if (plen > maxlen) {
1053 IP_VS_DBG(2, "BACKUP, bogus par.data len > %d\n", maxlen);
1054 return -EINVAL;
1055 }
1056 if (*opt_flags & flag) {
1057 IP_VS_DBG(2, "BACKUP, Par.data found twice 0x%x\n", flag);
1058 return -EINVAL;
1059 }
1060 *data_len = plen;
1061 *data = p;
1062 *opt_flags |= flag;
1063 return 0;
1064}
1065/*
1066 * Process a Version 1 sync. connection
1067 */
9bbac6a9 1068static inline int ip_vs_proc_sync_conn(struct net *net, __u8 *p, __u8 *msg_end)
fe5e7a1e
HS
1069{
1070 struct ip_vs_sync_conn_options opt;
1071 union ip_vs_sync_conn *s;
1072 struct ip_vs_protocol *pp;
1073 struct ip_vs_conn_param param;
1074 __u32 flags;
1075 unsigned int af, state, pe_data_len=0, pe_name_len=0;
1076 __u8 *pe_data=NULL, *pe_name=NULL;
1077 __u32 opt_flags=0;
1078 int retc=0;
1079
1080 s = (union ip_vs_sync_conn *) p;
1081
1082 if (s->v6.type & STYPE_F_INET6) {
1083#ifdef CONFIG_IP_VS_IPV6
1084 af = AF_INET6;
1085 p += sizeof(struct ip_vs_sync_v6);
1086#else
1087 IP_VS_DBG(3,"BACKUP, IPv6 msg received, and IPVS is not compiled for IPv6\n");
1088 retc = 10;
1089 goto out;
1090#endif
1091 } else if (!s->v4.type) {
1092 af = AF_INET;
1093 p += sizeof(struct ip_vs_sync_v4);
1094 } else {
1095 return -10;
1096 }
1097 if (p > msg_end)
1098 return -20;
1099
1100 /* Process optional params check Type & Len. */
1101 while (p < msg_end) {
1102 int ptype;
1103 int plen;
1104
1105 if (p+2 > msg_end)
1106 return -30;
1107 ptype = *(p++);
1108 plen = *(p++);
1109
1110 if (!plen || ((p + plen) > msg_end))
1111 return -40;
1112 /* Handle seq option p = param data */
1113 switch (ptype & ~IPVS_OPT_F_PARAM) {
1114 case IPVS_OPT_SEQ_DATA:
1115 if (ip_vs_proc_seqopt(p, plen, &opt_flags, &opt))
1116 return -50;
1117 break;
1118
1119 case IPVS_OPT_PE_DATA:
1120 if (ip_vs_proc_str(p, plen, &pe_data_len, &pe_data,
1121 IP_VS_PEDATA_MAXLEN, &opt_flags,
1122 IPVS_OPT_F_PE_DATA))
1123 return -60;
1124 break;
1125
1126 case IPVS_OPT_PE_NAME:
1127 if (ip_vs_proc_str(p, plen,&pe_name_len, &pe_name,
1128 IP_VS_PENAME_MAXLEN, &opt_flags,
1129 IPVS_OPT_F_PE_NAME))
1130 return -70;
1131 break;
1132
1133 default:
1134 /* Param data mandatory ? */
1135 if (!(ptype & IPVS_OPT_F_PARAM)) {
1136 IP_VS_DBG(3, "BACKUP, Unknown mandatory param %d found\n",
1137 ptype & ~IPVS_OPT_F_PARAM);
1138 retc = 20;
1139 goto out;
1140 }
f11017ec 1141 }
fe5e7a1e
HS
1142 p += plen; /* Next option */
1143 }
1144
1145 /* Get flags and Mask off unsupported */
1146 flags = ntohl(s->v4.flags) & IP_VS_CONN_F_BACKUP_MASK;
1147 flags |= IP_VS_CONN_F_SYNC;
1148 state = ntohs(s->v4.state);
1149
1150 if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
1151 pp = ip_vs_proto_get(s->v4.protocol);
1152 if (!pp) {
1153 IP_VS_DBG(3,"BACKUP, Unsupported protocol %u\n",
1154 s->v4.protocol);
1155 retc = 30;
1156 goto out;
1157 }
1158 if (state >= pp->num_states) {
1159 IP_VS_DBG(3, "BACKUP, Invalid %s state %u\n",
1160 pp->name, state);
1161 retc = 40;
1162 goto out;
1163 }
1164 } else {
1165 /* protocol in templates is not used for state/timeout */
fe5e7a1e
HS
1166 if (state > 0) {
1167 IP_VS_DBG(3, "BACKUP, Invalid template state %u\n",
1168 state);
1169 state = 0;
1170 }
1171 }
19913dec 1172 if (ip_vs_conn_fill_param_sync(net_ipvs(net), af, s, &param, pe_data,
6e67e586 1173 pe_data_len, pe_name, pe_name_len)) {
fe5e7a1e
HS
1174 retc = 50;
1175 goto out;
1176 }
1177 /* If only IPv4, just silent skip IPv6 */
1178 if (af == AF_INET)
9bbac6a9 1179 ip_vs_proc_conn(net, &param, flags, state, s->v4.protocol, af,
fe5e7a1e
HS
1180 (union nf_inet_addr *)&s->v4.daddr, s->v4.dport,
1181 ntohl(s->v4.timeout), ntohl(s->v4.fwmark),
9bbac6a9
HS
1182 (opt_flags & IPVS_OPT_F_SEQ_DATA ? &opt : NULL)
1183 );
fe5e7a1e
HS
1184#ifdef CONFIG_IP_VS_IPV6
1185 else
9bbac6a9 1186 ip_vs_proc_conn(net, &param, flags, state, s->v6.protocol, af,
fe5e7a1e
HS
1187 (union nf_inet_addr *)&s->v6.daddr, s->v6.dport,
1188 ntohl(s->v6.timeout), ntohl(s->v6.fwmark),
9bbac6a9
HS
1189 (opt_flags & IPVS_OPT_F_SEQ_DATA ? &opt : NULL)
1190 );
fe5e7a1e 1191#endif
528c943f 1192 ip_vs_pe_put(param.pe);
fe5e7a1e
HS
1193 return 0;
1194 /* Error exit */
1195out:
1196 IP_VS_DBG(2, "BACKUP, Single msg dropped err:%d\n", retc);
1197 return retc;
1198
1199}
1200/*
1201 * Process received multicast message and create the corresponding
1202 * ip_vs_conn entries.
1203 * Handles Version 0 & 1
1204 */
7d537f3a 1205static void ip_vs_process_message(struct netns_ipvs *ipvs, __u8 *buffer,
9bbac6a9 1206 const size_t buflen)
fe5e7a1e 1207{
986a0757 1208 struct ip_vs_sync_mesg *m2 = (struct ip_vs_sync_mesg *)buffer;
fe5e7a1e 1209 __u8 *p, *msg_end;
986a0757 1210 int i, nr_conns;
fe5e7a1e 1211
986a0757 1212 if (buflen < sizeof(struct ip_vs_sync_mesg_v0)) {
fe5e7a1e
HS
1213 IP_VS_DBG(2, "BACKUP, message header too short\n");
1214 return;
1215 }
fe5e7a1e 1216
38561437 1217 if (buflen != ntohs(m2->size)) {
fe5e7a1e
HS
1218 IP_VS_DBG(2, "BACKUP, bogus message size\n");
1219 return;
1220 }
1221 /* SyncID sanity check */
e4ff6751 1222 if (ipvs->bcfg.syncid != 0 && m2->syncid != ipvs->bcfg.syncid) {
fe5e7a1e
HS
1223 IP_VS_DBG(7, "BACKUP, Ignoring syncid = %d\n", m2->syncid);
1224 return;
1225 }
1226 /* Handle version 1 message */
1227 if ((m2->version == SYNC_PROTO_VER) && (m2->reserved == 0)
1228 && (m2->spare == 0)) {
1229
986a0757 1230 msg_end = buffer + sizeof(struct ip_vs_sync_mesg);
fe5e7a1e
HS
1231 nr_conns = m2->nr_conns;
1232
1233 for (i=0; i<nr_conns; i++) {
1234 union ip_vs_sync_conn *s;
95c96174 1235 unsigned int size;
fe5e7a1e
HS
1236 int retc;
1237
1238 p = msg_end;
1239 if (p + sizeof(s->v4) > buffer+buflen) {
1240 IP_VS_ERR_RL("BACKUP, Dropping buffer, to small\n");
1241 return;
b209639e 1242 }
fe5e7a1e
HS
1243 s = (union ip_vs_sync_conn *)p;
1244 size = ntohs(s->v4.ver_size) & SVER_MASK;
1245 msg_end = p + size;
1246 /* Basic sanity checks */
1247 if (msg_end > buffer+buflen) {
1248 IP_VS_ERR_RL("BACKUP, Dropping buffer, msg > buffer\n");
1da177e4
LT
1249 return;
1250 }
fe5e7a1e
HS
1251 if (ntohs(s->v4.ver_size) >> SVER_SHIFT) {
1252 IP_VS_ERR_RL("BACKUP, Dropping buffer, Unknown version %d\n",
1253 ntohs(s->v4.ver_size) >> SVER_SHIFT);
1254 return;
b209639e 1255 }
fe5e7a1e 1256 /* Process a single sync_conn */
7d537f3a 1257 retc = ip_vs_proc_sync_conn(ipvs->net, p, msg_end);
9bbac6a9 1258 if (retc < 0) {
fe5e7a1e
HS
1259 IP_VS_ERR_RL("BACKUP, Dropping buffer, Err: %d in decoding\n",
1260 retc);
1261 return;
2906f66a 1262 }
fe5e7a1e
HS
1263 /* Make sure we have 32 bit alignment */
1264 msg_end = p + ((size + 3) & ~3);
b209639e 1265 }
fe5e7a1e
HS
1266 } else {
1267 /* Old type of message */
19913dec 1268 ip_vs_process_message_v0(ipvs, buffer, buflen);
fe5e7a1e 1269 return;
1da177e4
LT
1270 }
1271}
1272
1273
1c003b15
PNA
1274/*
1275 * Setup sndbuf (mode=1) or rcvbuf (mode=0)
1276 */
1277static void set_sock_size(struct sock *sk, int mode, int val)
1278{
1279 /* setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &val, sizeof(val)); */
1280 /* setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &val, sizeof(val)); */
1281 lock_sock(sk);
1282 if (mode) {
1283 val = clamp_t(int, val, (SOCK_MIN_SNDBUF + 1) / 2,
1284 sysctl_wmem_max);
1285 sk->sk_sndbuf = val * 2;
1286 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
1287 } else {
1288 val = clamp_t(int, val, (SOCK_MIN_RCVBUF + 1) / 2,
1289 sysctl_rmem_max);
1290 sk->sk_rcvbuf = val * 2;
1291 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
1292 }
1293 release_sock(sk);
1294}
1295
1da177e4
LT
1296/*
1297 * Setup loopback of outgoing multicasts on a sending socket
1298 */
1299static void set_mcast_loop(struct sock *sk, u_char loop)
1300{
1301 struct inet_sock *inet = inet_sk(sk);
1302
1303 /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */
1304 lock_sock(sk);
1305 inet->mc_loop = loop ? 1 : 0;
d3328817
JA
1306#ifdef CONFIG_IP_VS_IPV6
1307 if (sk->sk_family == AF_INET6) {
1308 struct ipv6_pinfo *np = inet6_sk(sk);
1309
1310 /* IPV6_MULTICAST_LOOP */
1311 np->mc_loop = loop ? 1 : 0;
1312 }
1313#endif
1da177e4
LT
1314 release_sock(sk);
1315}
1316
1317/*
1318 * Specify TTL for outgoing multicasts on a sending socket
1319 */
1320static void set_mcast_ttl(struct sock *sk, u_char ttl)
1321{
1322 struct inet_sock *inet = inet_sk(sk);
1323
1324 /* setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); */
1325 lock_sock(sk);
1326 inet->mc_ttl = ttl;
d3328817
JA
1327#ifdef CONFIG_IP_VS_IPV6
1328 if (sk->sk_family == AF_INET6) {
1329 struct ipv6_pinfo *np = inet6_sk(sk);
1330
1331 /* IPV6_MULTICAST_HOPS */
1332 np->mcast_hops = ttl;
1333 }
1334#endif
1da177e4
LT
1335 release_sock(sk);
1336}
1337
e4ff6751
JA
1338/* Control fragmentation of messages */
1339static void set_mcast_pmtudisc(struct sock *sk, int val)
1340{
1341 struct inet_sock *inet = inet_sk(sk);
1342
1343 /* setsockopt(sock, SOL_IP, IP_MTU_DISCOVER, &val, sizeof(val)); */
1344 lock_sock(sk);
1345 inet->pmtudisc = val;
d3328817
JA
1346#ifdef CONFIG_IP_VS_IPV6
1347 if (sk->sk_family == AF_INET6) {
1348 struct ipv6_pinfo *np = inet6_sk(sk);
1349
1350 /* IPV6_MTU_DISCOVER */
1351 np->pmtudisc = val;
1352 }
1353#endif
e4ff6751
JA
1354 release_sock(sk);
1355}
1356
1da177e4
LT
1357/*
1358 * Specifiy default interface for outgoing multicasts
1359 */
1360static int set_mcast_if(struct sock *sk, char *ifname)
1361{
1362 struct net_device *dev;
1363 struct inet_sock *inet = inet_sk(sk);
f131315f 1364 struct net *net = sock_net(sk);
1da177e4 1365
f131315f
HS
1366 dev = __dev_get_by_name(net, ifname);
1367 if (!dev)
1da177e4
LT
1368 return -ENODEV;
1369
1370 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
1371 return -EINVAL;
1372
1373 lock_sock(sk);
1374 inet->mc_index = dev->ifindex;
1375 /* inet->mc_addr = 0; */
d3328817
JA
1376#ifdef CONFIG_IP_VS_IPV6
1377 if (sk->sk_family == AF_INET6) {
1378 struct ipv6_pinfo *np = inet6_sk(sk);
1379
1380 /* IPV6_MULTICAST_IF */
1381 np->mcast_oif = dev->ifindex;
1382 }
1383#endif
1da177e4
LT
1384 release_sock(sk);
1385
1386 return 0;
1387}
1388
1389
1da177e4
LT
1390/*
1391 * Join a multicast group.
1392 * the group is specified by a class D multicast address 224.0.0.0/8
1393 * in the in_addr structure passed in as a parameter.
1394 */
1395static int
1396join_mcast_group(struct sock *sk, struct in_addr *addr, char *ifname)
1397{
f131315f 1398 struct net *net = sock_net(sk);
1da177e4
LT
1399 struct ip_mreqn mreq;
1400 struct net_device *dev;
1401 int ret;
1402
1403 memset(&mreq, 0, sizeof(mreq));
1404 memcpy(&mreq.imr_multiaddr, addr, sizeof(struct in_addr));
1405
f131315f
HS
1406 dev = __dev_get_by_name(net, ifname);
1407 if (!dev)
1da177e4
LT
1408 return -ENODEV;
1409 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
1410 return -EINVAL;
1411
1412 mreq.imr_ifindex = dev->ifindex;
1413
1414 lock_sock(sk);
1415 ret = ip_mc_join_group(sk, &mreq);
1416 release_sock(sk);
1417
1418 return ret;
1419}
1420
d3328817
JA
1421#ifdef CONFIG_IP_VS_IPV6
1422static int join_mcast_group6(struct sock *sk, struct in6_addr *addr,
1423 char *ifname)
1424{
1425 struct net *net = sock_net(sk);
1426 struct net_device *dev;
1427 int ret;
1428
1429 dev = __dev_get_by_name(net, ifname);
1430 if (!dev)
1431 return -ENODEV;
1432 if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
1433 return -EINVAL;
1434
1435 lock_sock(sk);
1436 ret = ipv6_sock_mc_join(sk, dev->ifindex, addr);
1437 release_sock(sk);
1438
1439 return ret;
1440}
1441#endif
1da177e4
LT
1442
1443static int bind_mcastif_addr(struct socket *sock, char *ifname)
1444{
f131315f 1445 struct net *net = sock_net(sock->sk);
1da177e4 1446 struct net_device *dev;
a61ced5d 1447 __be32 addr;
1da177e4
LT
1448 struct sockaddr_in sin;
1449
f131315f
HS
1450 dev = __dev_get_by_name(net, ifname);
1451 if (!dev)
1da177e4
LT
1452 return -ENODEV;
1453
1454 addr = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
1455 if (!addr)
1e3e238e
HE
1456 pr_err("You probably need to specify IP address on "
1457 "multicast interface.\n");
1da177e4 1458
14d5e834
HH
1459 IP_VS_DBG(7, "binding socket with (%s) %pI4\n",
1460 ifname, &addr);
1da177e4
LT
1461
1462 /* Now bind the socket with the address of multicast interface */
1463 sin.sin_family = AF_INET;
1464 sin.sin_addr.s_addr = addr;
1465 sin.sin_port = 0;
1466
1467 return sock->ops->bind(sock, (struct sockaddr*)&sin, sizeof(sin));
1468}
1469
d3328817
JA
1470static void get_mcast_sockaddr(union ipvs_sockaddr *sa, int *salen,
1471 struct ipvs_sync_daemon_cfg *c, int id)
1472{
1473 if (AF_INET6 == c->mcast_af) {
1474 sa->in6 = (struct sockaddr_in6) {
1475 .sin6_family = AF_INET6,
1476 .sin6_port = htons(c->mcast_port + id),
1477 };
1478 sa->in6.sin6_addr = c->mcast_group.in6;
1479 *salen = sizeof(sa->in6);
1480 } else {
1481 sa->in = (struct sockaddr_in) {
1482 .sin_family = AF_INET,
1483 .sin_port = htons(c->mcast_port + id),
1484 };
1485 sa->in.sin_addr = c->mcast_group.in;
1486 *salen = sizeof(sa->in);
1487 }
1488}
1489
1da177e4
LT
1490/*
1491 * Set up sending multicast socket over UDP
1492 */
68c76b6a 1493static struct socket *make_send_sock(struct netns_ipvs *ipvs, int id)
1da177e4 1494{
f73181c8 1495 /* multicast addr */
d3328817 1496 union ipvs_sockaddr mcast_addr;
1da177e4 1497 struct socket *sock;
d3328817 1498 int result, salen;
1da177e4 1499
26abe143 1500 /* First create a socket */
68c76b6a 1501 result = sock_create_kern(ipvs->net, ipvs->mcfg.mcast_af, SOCK_DGRAM,
d3328817 1502 IPPROTO_UDP, &sock);
e6dd731c 1503 if (result < 0) {
1e3e238e 1504 pr_err("Error during creation of socket; terminating\n");
e6dd731c 1505 return ERR_PTR(result);
1da177e4 1506 }
e4ff6751 1507 result = set_mcast_if(sock->sk, ipvs->mcfg.mcast_ifn);
e6dd731c 1508 if (result < 0) {
1e3e238e 1509 pr_err("Error setting outbound mcast interface\n");
1da177e4
LT
1510 goto error;
1511 }
1512
1513 set_mcast_loop(sock->sk, 0);
d3328817 1514 set_mcast_ttl(sock->sk, ipvs->mcfg.mcast_ttl);
e4ff6751
JA
1515 /* Allow fragmentation if MTU changes */
1516 set_mcast_pmtudisc(sock->sk, IP_PMTUDISC_DONT);
1c003b15
PNA
1517 result = sysctl_sync_sock_size(ipvs);
1518 if (result > 0)
1519 set_sock_size(sock->sk, 1, result);
1da177e4 1520
d3328817
JA
1521 if (AF_INET == ipvs->mcfg.mcast_af)
1522 result = bind_mcastif_addr(sock, ipvs->mcfg.mcast_ifn);
1523 else
1524 result = 0;
e6dd731c 1525 if (result < 0) {
1e3e238e 1526 pr_err("Error binding address of the mcast interface\n");
1da177e4
LT
1527 goto error;
1528 }
1529
d3328817 1530 get_mcast_sockaddr(&mcast_addr, &salen, &ipvs->mcfg, id);
e6dd731c 1531 result = sock->ops->connect(sock, (struct sockaddr *) &mcast_addr,
d3328817 1532 salen, 0);
e6dd731c 1533 if (result < 0) {
1e3e238e 1534 pr_err("Error connecting to the multicast addr\n");
1da177e4
LT
1535 goto error;
1536 }
1537
1538 return sock;
1539
1ae132b0 1540error:
26abe143 1541 sock_release(sock);
e6dd731c 1542 return ERR_PTR(result);
1da177e4
LT
1543}
1544
1545
1546/*
1547 * Set up receiving multicast socket over UDP
1548 */
fd124e2f 1549static struct socket *make_receive_sock(struct netns_ipvs *ipvs, int id)
1da177e4 1550{
f73181c8 1551 /* multicast addr */
d3328817 1552 union ipvs_sockaddr mcast_addr;
1da177e4 1553 struct socket *sock;
d3328817 1554 int result, salen;
1da177e4
LT
1555
1556 /* First create a socket */
fd124e2f 1557 result = sock_create_kern(ipvs->net, ipvs->bcfg.mcast_af, SOCK_DGRAM,
d3328817 1558 IPPROTO_UDP, &sock);
e6dd731c 1559 if (result < 0) {
1e3e238e 1560 pr_err("Error during creation of socket; terminating\n");
e6dd731c 1561 return ERR_PTR(result);
1da177e4 1562 }
1da177e4 1563 /* it is equivalent to the REUSEADDR option in user-space */
4a17fd52 1564 sock->sk->sk_reuse = SK_CAN_REUSE;
1c003b15
PNA
1565 result = sysctl_sync_sock_size(ipvs);
1566 if (result > 0)
1567 set_sock_size(sock->sk, 0, result);
1da177e4 1568
d3328817
JA
1569 get_mcast_sockaddr(&mcast_addr, &salen, &ipvs->bcfg, id);
1570 result = sock->ops->bind(sock, (struct sockaddr *)&mcast_addr, salen);
e6dd731c 1571 if (result < 0) {
1e3e238e 1572 pr_err("Error binding to the multicast addr\n");
1da177e4
LT
1573 goto error;
1574 }
1575
1576 /* join the multicast group */
d3328817
JA
1577#ifdef CONFIG_IP_VS_IPV6
1578 if (ipvs->bcfg.mcast_af == AF_INET6)
1579 result = join_mcast_group6(sock->sk, &mcast_addr.in6.sin6_addr,
1580 ipvs->bcfg.mcast_ifn);
1581 else
1582#endif
1583 result = join_mcast_group(sock->sk, &mcast_addr.in.sin_addr,
1584 ipvs->bcfg.mcast_ifn);
e6dd731c 1585 if (result < 0) {
1e3e238e 1586 pr_err("Error joining to the multicast group\n");
1da177e4
LT
1587 goto error;
1588 }
1589
1590 return sock;
1591
1ae132b0 1592error:
26abe143 1593 sock_release(sock);
e6dd731c 1594 return ERR_PTR(result);
1da177e4
LT
1595}
1596
1597
1598static int
1599ip_vs_send_async(struct socket *sock, const char *buffer, const size_t length)
1600{
1601 struct msghdr msg = {.msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL};
1602 struct kvec iov;
1603 int len;
1604
1605 EnterFunction(7);
1606 iov.iov_base = (void *)buffer;
1607 iov.iov_len = length;
1608
1609 len = kernel_sendmsg(sock, &msg, &iov, 1, (size_t)(length));
1610
1611 LeaveFunction(7);
1612 return len;
1613}
1614
1c003b15 1615static int
1da177e4
LT
1616ip_vs_send_sync_msg(struct socket *sock, struct ip_vs_sync_mesg *msg)
1617{
1618 int msize;
1c003b15 1619 int ret;
1da177e4 1620
38561437 1621 msize = ntohs(msg->size);
1da177e4 1622
1c003b15
PNA
1623 ret = ip_vs_send_async(sock, (char *)msg, msize);
1624 if (ret >= 0 || ret == -EAGAIN)
1625 return ret;
1626 pr_err("ip_vs_send_async error %d\n", ret);
1627 return 0;
1da177e4
LT
1628}
1629
1630static int
1631ip_vs_receive(struct socket *sock, char *buffer, const size_t buflen)
1632{
1633 struct msghdr msg = {NULL,};
1634 struct kvec iov;
1635 int len;
1636
1637 EnterFunction(7);
1638
1639 /* Receive a packet */
1640 iov.iov_base = buffer;
1641 iov.iov_len = (size_t)buflen;
1642
f73181c8 1643 len = kernel_recvmsg(sock, &msg, &iov, 1, buflen, MSG_DONTWAIT);
1da177e4
LT
1644
1645 if (len < 0)
f73181c8 1646 return len;
1da177e4
LT
1647
1648 LeaveFunction(7);
1649 return len;
1650}
1651
1c003b15
PNA
1652/* Wakeup the master thread for sending */
1653static void master_wakeup_work_handler(struct work_struct *work)
1654{
f73181c8
PNA
1655 struct ipvs_master_sync_state *ms =
1656 container_of(work, struct ipvs_master_sync_state,
1657 master_wakeup_work.work);
1658 struct netns_ipvs *ipvs = ms->ipvs;
1c003b15
PNA
1659
1660 spin_lock_bh(&ipvs->sync_lock);
f73181c8
PNA
1661 if (ms->sync_queue_len &&
1662 ms->sync_queue_delay < IPVS_SYNC_WAKEUP_RATE) {
1663 ms->sync_queue_delay = IPVS_SYNC_WAKEUP_RATE;
1664 wake_up_process(ms->master_thread);
1c003b15
PNA
1665 }
1666 spin_unlock_bh(&ipvs->sync_lock);
1667}
1668
1669/* Get next buffer to send */
1670static inline struct ip_vs_sync_buff *
f73181c8 1671next_sync_buff(struct netns_ipvs *ipvs, struct ipvs_master_sync_state *ms)
1c003b15
PNA
1672{
1673 struct ip_vs_sync_buff *sb;
1674
f73181c8 1675 sb = sb_dequeue(ipvs, ms);
1c003b15
PNA
1676 if (sb)
1677 return sb;
1678 /* Do not delay entries in buffer for more than 2 seconds */
f73181c8 1679 return get_curr_sync_buff(ipvs, ms, IPVS_SYNC_FLUSH_TIME);
1c003b15 1680}
1da177e4 1681
998e7a76 1682static int sync_thread_master(void *data)
1da177e4 1683{
998e7a76 1684 struct ip_vs_sync_thread_data *tinfo = data;
37b68e6d 1685 struct netns_ipvs *ipvs = tinfo->ipvs;
f73181c8 1686 struct ipvs_master_sync_state *ms = &ipvs->ms[tinfo->id];
1c003b15 1687 struct sock *sk = tinfo->sock->sk;
1da177e4
LT
1688 struct ip_vs_sync_buff *sb;
1689
1e3e238e 1690 pr_info("sync thread started: state = MASTER, mcast_ifn = %s, "
f73181c8 1691 "syncid = %d, id = %d\n",
e4ff6751 1692 ipvs->mcfg.mcast_ifn, ipvs->mcfg.syncid, tinfo->id);
1da177e4 1693
1c003b15 1694 for (;;) {
f73181c8 1695 sb = next_sync_buff(ipvs, ms);
1c003b15
PNA
1696 if (unlikely(kthread_should_stop()))
1697 break;
1698 if (!sb) {
1699 schedule_timeout(IPVS_SYNC_CHECK_PERIOD);
1700 continue;
1da177e4 1701 }
1c003b15 1702 while (ip_vs_send_sync_msg(tinfo->sock, sb->mesg) < 0) {
9dcbe1b8
GU
1703 /* (Ab)use interruptible sleep to avoid increasing
1704 * the load avg.
1705 */
1706 __wait_event_interruptible(*sk_sleep(sk),
1c003b15 1707 sock_writeable(sk) ||
35a2af94 1708 kthread_should_stop());
1c003b15
PNA
1709 if (unlikely(kthread_should_stop()))
1710 goto done;
1da177e4 1711 }
1c003b15 1712 ip_vs_sync_buff_release(sb);
1da177e4
LT
1713 }
1714
1c003b15
PNA
1715done:
1716 __set_current_state(TASK_RUNNING);
1717 if (sb)
1718 ip_vs_sync_buff_release(sb);
1719
1da177e4 1720 /* clean up the sync_buff queue */
f73181c8 1721 while ((sb = sb_dequeue(ipvs, ms)))
1da177e4 1722 ip_vs_sync_buff_release(sb);
1c003b15 1723 __set_current_state(TASK_RUNNING);
1da177e4
LT
1724
1725 /* clean up the current sync_buff */
f73181c8 1726 sb = get_curr_sync_buff(ipvs, ms, 0);
f131315f 1727 if (sb)
1da177e4 1728 ip_vs_sync_buff_release(sb);
1da177e4
LT
1729
1730 /* release the sending multicast socket */
26abe143 1731 sock_release(tinfo->sock);
998e7a76
SW
1732 kfree(tinfo);
1733
1734 return 0;
1da177e4
LT
1735}
1736
1737
998e7a76 1738static int sync_thread_backup(void *data)
1da177e4 1739{
998e7a76 1740 struct ip_vs_sync_thread_data *tinfo = data;
37b68e6d 1741 struct netns_ipvs *ipvs = tinfo->ipvs;
1da177e4
LT
1742 int len;
1743
1e3e238e 1744 pr_info("sync thread started: state = BACKUP, mcast_ifn = %s, "
f73181c8 1745 "syncid = %d, id = %d\n",
e4ff6751 1746 ipvs->bcfg.mcast_ifn, ipvs->bcfg.syncid, tinfo->id);
1da177e4 1747
998e7a76 1748 while (!kthread_should_stop()) {
aa395145 1749 wait_event_interruptible(*sk_sleep(tinfo->sock->sk),
ba6fd850
SW
1750 !skb_queue_empty(&tinfo->sock->sk->sk_receive_queue)
1751 || kthread_should_stop());
1752
998e7a76
SW
1753 /* do we have data now? */
1754 while (!skb_queue_empty(&(tinfo->sock->sk->sk_receive_queue))) {
1755 len = ip_vs_receive(tinfo->sock, tinfo->buf,
e4ff6751 1756 ipvs->bcfg.sync_maxlen);
998e7a76 1757 if (len <= 0) {
f73181c8
PNA
1758 if (len != -EAGAIN)
1759 pr_err("receiving message error\n");
1da177e4
LT
1760 break;
1761 }
998e7a76 1762
7d537f3a 1763 ip_vs_process_message(ipvs, tinfo->buf, len);
1da177e4 1764 }
1da177e4
LT
1765 }
1766
1767 /* release the sending multicast socket */
26abe143 1768 sock_release(tinfo->sock);
998e7a76
SW
1769 kfree(tinfo->buf);
1770 kfree(tinfo);
1da177e4 1771
998e7a76 1772 return 0;
1da177e4
LT
1773}
1774
1775
6ac121d7 1776int start_sync_thread(struct netns_ipvs *ipvs, struct ipvs_sync_daemon_cfg *c,
e4ff6751 1777 int state)
1da177e4 1778{
998e7a76 1779 struct ip_vs_sync_thread_data *tinfo;
f73181c8 1780 struct task_struct **array = NULL, *task;
998e7a76 1781 struct socket *sock;
e4ff6751 1782 struct net_device *dev;
f73181c8 1783 char *name;
998e7a76 1784 int (*threadfn)(void *data);
e4ff6751 1785 int id, count, hlen;
998e7a76 1786 int result = -ENOMEM;
e4ff6751 1787 u16 mtu, min_mtu;
1da177e4 1788
1e3e238e 1789 IP_VS_DBG(7, "%s(): pid %d\n", __func__, task_pid_nr(current));
998e7a76 1790 IP_VS_DBG(7, "Each ip_vs_sync_conn entry needs %Zd bytes\n",
2981bc9a 1791 sizeof(struct ip_vs_sync_conn_v0));
1da177e4 1792
f73181c8
PNA
1793 if (!ipvs->sync_state) {
1794 count = clamp(sysctl_sync_ports(ipvs), 1, IPVS_SYNC_PORTS_MAX);
1795 ipvs->threads_mask = count - 1;
1796 } else
1797 count = ipvs->threads_mask + 1;
ae1d48b2 1798
d3328817
JA
1799 if (c->mcast_af == AF_UNSPEC) {
1800 c->mcast_af = AF_INET;
1801 c->mcast_group.ip = cpu_to_be32(IP_VS_SYNC_GROUP);
1802 }
1803 if (!c->mcast_port)
1804 c->mcast_port = IP_VS_SYNC_PORT;
1805 if (!c->mcast_ttl)
1806 c->mcast_ttl = 1;
1807
6ac121d7 1808 dev = __dev_get_by_name(ipvs->net, c->mcast_ifn);
e4ff6751
JA
1809 if (!dev) {
1810 pr_err("Unknown mcast interface: %s\n", c->mcast_ifn);
1811 return -ENODEV;
1812 }
d3328817
JA
1813 hlen = (AF_INET6 == c->mcast_af) ?
1814 sizeof(struct ipv6hdr) + sizeof(struct udphdr) :
1815 sizeof(struct iphdr) + sizeof(struct udphdr);
e4ff6751
JA
1816 mtu = (state == IP_VS_STATE_BACKUP) ?
1817 clamp(dev->mtu, 1500U, 65535U) : 1500U;
1818 min_mtu = (state == IP_VS_STATE_BACKUP) ? 1024 : 1;
1819
1820 if (c->sync_maxlen)
1821 c->sync_maxlen = clamp_t(unsigned int,
1822 c->sync_maxlen, min_mtu,
1823 65535 - hlen);
1824 else
1825 c->sync_maxlen = mtu - hlen;
1826
998e7a76 1827 if (state == IP_VS_STATE_MASTER) {
f73181c8 1828 if (ipvs->ms)
998e7a76 1829 return -EEXIST;
1da177e4 1830
e4ff6751 1831 ipvs->mcfg = *c;
f73181c8 1832 name = "ipvs-m:%d:%d";
998e7a76 1833 threadfn = sync_thread_master;
998e7a76 1834 } else if (state == IP_VS_STATE_BACKUP) {
f73181c8 1835 if (ipvs->backup_threads)
998e7a76
SW
1836 return -EEXIST;
1837
e4ff6751 1838 ipvs->bcfg = *c;
f73181c8 1839 name = "ipvs-b:%d:%d";
998e7a76 1840 threadfn = sync_thread_backup;
1da177e4 1841 } else {
1da177e4
LT
1842 return -EINVAL;
1843 }
1844
f73181c8
PNA
1845 if (state == IP_VS_STATE_MASTER) {
1846 struct ipvs_master_sync_state *ms;
1da177e4 1847
f73181c8
PNA
1848 ipvs->ms = kzalloc(count * sizeof(ipvs->ms[0]), GFP_KERNEL);
1849 if (!ipvs->ms)
1850 goto out;
1851 ms = ipvs->ms;
1852 for (id = 0; id < count; id++, ms++) {
1853 INIT_LIST_HEAD(&ms->sync_queue);
1854 ms->sync_queue_len = 0;
1855 ms->sync_queue_delay = 0;
1856 INIT_DELAYED_WORK(&ms->master_wakeup_work,
1857 master_wakeup_work_handler);
1858 ms->ipvs = ipvs;
1859 }
1860 } else {
1861 array = kzalloc(count * sizeof(struct task_struct *),
1862 GFP_KERNEL);
1863 if (!array)
1864 goto out;
998e7a76 1865 }
1da177e4 1866
f73181c8
PNA
1867 tinfo = NULL;
1868 for (id = 0; id < count; id++) {
1869 if (state == IP_VS_STATE_MASTER)
68c76b6a 1870 sock = make_send_sock(ipvs, id);
f73181c8 1871 else
fd124e2f 1872 sock = make_receive_sock(ipvs, id);
f73181c8
PNA
1873 if (IS_ERR(sock)) {
1874 result = PTR_ERR(sock);
1875 goto outtinfo;
1876 }
1877 tinfo = kmalloc(sizeof(*tinfo), GFP_KERNEL);
1878 if (!tinfo)
1879 goto outsocket;
37b68e6d 1880 tinfo->ipvs = ipvs;
f73181c8
PNA
1881 tinfo->sock = sock;
1882 if (state == IP_VS_STATE_BACKUP) {
e4ff6751 1883 tinfo->buf = kmalloc(ipvs->bcfg.sync_maxlen,
f73181c8
PNA
1884 GFP_KERNEL);
1885 if (!tinfo->buf)
1886 goto outtinfo;
b425df4c
DC
1887 } else {
1888 tinfo->buf = NULL;
f73181c8
PNA
1889 }
1890 tinfo->id = id;
1da177e4 1891
f73181c8
PNA
1892 task = kthread_run(threadfn, tinfo, name, ipvs->gen, id);
1893 if (IS_ERR(task)) {
1894 result = PTR_ERR(task);
1895 goto outtinfo;
1896 }
1897 tinfo = NULL;
1898 if (state == IP_VS_STATE_MASTER)
1899 ipvs->ms[id].master_thread = task;
1900 else
1901 array[id] = task;
998e7a76 1902 }
1da177e4 1903
998e7a76 1904 /* mark as active */
f73181c8
PNA
1905
1906 if (state == IP_VS_STATE_BACKUP)
1907 ipvs->backup_threads = array;
1908 spin_lock_bh(&ipvs->sync_buff_lock);
f131315f 1909 ipvs->sync_state |= state;
f73181c8 1910 spin_unlock_bh(&ipvs->sync_buff_lock);
1da177e4 1911
998e7a76
SW
1912 /* increase the module use count */
1913 ip_vs_use_count_inc();
1da177e4
LT
1914
1915 return 0;
1da177e4 1916
998e7a76 1917outsocket:
26abe143 1918 sock_release(sock);
f73181c8
PNA
1919
1920outtinfo:
1921 if (tinfo) {
26abe143 1922 sock_release(tinfo->sock);
f73181c8
PNA
1923 kfree(tinfo->buf);
1924 kfree(tinfo);
1925 }
1926 count = id;
1927 while (count-- > 0) {
1928 if (state == IP_VS_STATE_MASTER)
1929 kthread_stop(ipvs->ms[count].master_thread);
1930 else
1931 kthread_stop(array[count]);
1932 }
1933 kfree(array);
1934
998e7a76 1935out:
f73181c8
PNA
1936 if (!(ipvs->sync_state & IP_VS_STATE_MASTER)) {
1937 kfree(ipvs->ms);
1938 ipvs->ms = NULL;
1939 }
998e7a76 1940 return result;
1da177e4
LT
1941}
1942
1943
b3cf3cbf 1944int stop_sync_thread(struct netns_ipvs *ipvs, int state)
1da177e4 1945{
f73181c8
PNA
1946 struct task_struct **array;
1947 int id;
1ae132b0 1948 int retc = -EINVAL;
f131315f 1949
1e3e238e 1950 IP_VS_DBG(7, "%s(): pid %d\n", __func__, task_pid_nr(current));
1da177e4 1951
1da177e4 1952 if (state == IP_VS_STATE_MASTER) {
f73181c8 1953 if (!ipvs->ms)
998e7a76 1954 return -ESRCH;
cc0191ae 1955
998e7a76
SW
1956 /*
1957 * The lock synchronizes with sb_queue_tail(), so that we don't
1958 * add sync buffers to the queue, when we are already in
1959 * progress of stopping the master sync daemon.
1960 */
1da177e4 1961
f73181c8
PNA
1962 spin_lock_bh(&ipvs->sync_buff_lock);
1963 spin_lock(&ipvs->sync_lock);
f131315f 1964 ipvs->sync_state &= ~IP_VS_STATE_MASTER;
f73181c8
PNA
1965 spin_unlock(&ipvs->sync_lock);
1966 spin_unlock_bh(&ipvs->sync_buff_lock);
1967
1968 retc = 0;
1969 for (id = ipvs->threads_mask; id >= 0; id--) {
1970 struct ipvs_master_sync_state *ms = &ipvs->ms[id];
1971 int ret;
1972
1973 pr_info("stopping master sync thread %d ...\n",
1974 task_pid_nr(ms->master_thread));
1975 cancel_delayed_work_sync(&ms->master_wakeup_work);
1976 ret = kthread_stop(ms->master_thread);
1977 if (retc >= 0)
1978 retc = ret;
1979 }
1980 kfree(ipvs->ms);
1981 ipvs->ms = NULL;
998e7a76 1982 } else if (state == IP_VS_STATE_BACKUP) {
f73181c8 1983 if (!ipvs->backup_threads)
998e7a76
SW
1984 return -ESRCH;
1985
f131315f 1986 ipvs->sync_state &= ~IP_VS_STATE_BACKUP;
f73181c8
PNA
1987 array = ipvs->backup_threads;
1988 retc = 0;
1989 for (id = ipvs->threads_mask; id >= 0; id--) {
1990 int ret;
1991
1992 pr_info("stopping backup sync thread %d ...\n",
1993 task_pid_nr(array[id]));
1994 ret = kthread_stop(array[id]);
1995 if (retc >= 0)
1996 retc = ret;
1997 }
1998 kfree(array);
1999 ipvs->backup_threads = NULL;
998e7a76 2000 }
1da177e4 2001
998e7a76
SW
2002 /* decrease the module use count */
2003 ip_vs_use_count_dec();
1da177e4 2004
1ae132b0 2005 return retc;
1da177e4 2006}
61b1ab45
HS
2007
2008/*
2009 * Initialize data struct for each netns
2010 */
503cf15a 2011int __net_init ip_vs_sync_net_init(struct net *net)
61b1ab45 2012{
f131315f
HS
2013 struct netns_ipvs *ipvs = net_ipvs(net);
2014
ae1d48b2 2015 __mutex_init(&ipvs->sync_mutex, "ipvs->sync_mutex", &__ipvs_sync_key);
f131315f
HS
2016 spin_lock_init(&ipvs->sync_lock);
2017 spin_lock_init(&ipvs->sync_buff_lock);
61b1ab45
HS
2018 return 0;
2019}
2020
503cf15a 2021void ip_vs_sync_net_cleanup(struct net *net)
61b1ab45 2022{
1ae132b0 2023 int retc;
ae1d48b2 2024 struct netns_ipvs *ipvs = net_ipvs(net);
1ae132b0 2025
ae1d48b2 2026 mutex_lock(&ipvs->sync_mutex);
b3cf3cbf 2027 retc = stop_sync_thread(ipvs, IP_VS_STATE_MASTER);
1ae132b0
HS
2028 if (retc && retc != -ESRCH)
2029 pr_err("Failed to stop Master Daemon\n");
2030
b3cf3cbf 2031 retc = stop_sync_thread(ipvs, IP_VS_STATE_BACKUP);
1ae132b0
HS
2032 if (retc && retc != -ESRCH)
2033 pr_err("Failed to stop Backup Daemon\n");
ae1d48b2 2034 mutex_unlock(&ipvs->sync_mutex);
61b1ab45 2035}