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