b3235b2301397f9354b45858f6b8429535c7fac3
[firefly-linux-kernel-4.4.55.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 };
200
201 /* Version 0 definition of packet sizes */
202 #define SIMPLE_CONN_SIZE  (sizeof(struct ip_vs_sync_conn_v0))
203 #define FULL_CONN_SIZE  \
204 (sizeof(struct ip_vs_sync_conn_v0) + sizeof(struct ip_vs_sync_conn_options))
205
206
207 /*
208   The master mulitcasts messages (Datagrams) to the backup load balancers
209   in the following format.
210
211  Version 1:
212   Note, first byte should be Zero, so ver 0 receivers will drop the packet.
213
214        0                   1                   2                   3
215        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
216       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
217       |      0        |    SyncID     |            Size               |
218       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
219       |  Count Conns  |    Version    |    Reserved, set to Zero      |
220       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
221       |                                                               |
222       |                    IPVS Sync Connection (1)                   |
223       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
224       |                            .                                  |
225       ~                            .                                  ~
226       |                            .                                  |
227       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
228       |                                                               |
229       |                    IPVS Sync Connection (n)                   |
230       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
231
232  Version 0 Header
233        0                   1                   2                   3
234        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
235       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
236       |  Count Conns  |    SyncID     |            Size               |
237       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
238       |                    IPVS Sync Connection (1)                   |
239 */
240
241 #define SYNC_MESG_HEADER_LEN    4
242 #define MAX_CONNS_PER_SYNCBUFF  255 /* nr_conns in ip_vs_sync_mesg is 8 bit */
243
244 /* Version 0 header */
245 struct ip_vs_sync_mesg_v0 {
246         __u8                    nr_conns;
247         __u8                    syncid;
248         __u16                   size;
249
250         /* ip_vs_sync_conn entries start here */
251 };
252
253 /* Version 1 header */
254 struct ip_vs_sync_mesg {
255         __u8                    reserved;       /* must be zero */
256         __u8                    syncid;
257         __u16                   size;
258         __u8                    nr_conns;
259         __s8                    version;        /* SYNC_PROTO_VER  */
260         __u16                   spare;
261         /* ip_vs_sync_conn entries start here */
262 };
263
264 struct ip_vs_sync_buff {
265         struct list_head        list;
266         unsigned long           firstuse;
267
268         /* pointers for the message data */
269         struct ip_vs_sync_mesg  *mesg;
270         unsigned char           *head;
271         unsigned char           *end;
272 };
273
274 /* multicast addr */
275 static struct sockaddr_in mcast_addr = {
276         .sin_family             = AF_INET,
277         .sin_port               = cpu_to_be16(IP_VS_SYNC_PORT),
278         .sin_addr.s_addr        = cpu_to_be32(IP_VS_SYNC_GROUP),
279 };
280
281 /*
282  * Copy of struct ip_vs_seq
283  * From unaligned network order to aligned host order
284  */
285 static void ntoh_seq(struct ip_vs_seq *no, struct ip_vs_seq *ho)
286 {
287         ho->init_seq       = get_unaligned_be32(&no->init_seq);
288         ho->delta          = get_unaligned_be32(&no->delta);
289         ho->previous_delta = get_unaligned_be32(&no->previous_delta);
290 }
291
292 /*
293  * Copy of struct ip_vs_seq
294  * From Aligned host order to unaligned network order
295  */
296 static void hton_seq(struct ip_vs_seq *ho, struct ip_vs_seq *no)
297 {
298         put_unaligned_be32(ho->init_seq, &no->init_seq);
299         put_unaligned_be32(ho->delta, &no->delta);
300         put_unaligned_be32(ho->previous_delta, &no->previous_delta);
301 }
302
303 static inline struct ip_vs_sync_buff *sb_dequeue(struct netns_ipvs *ipvs)
304 {
305         struct ip_vs_sync_buff *sb;
306
307         spin_lock_bh(&ipvs->sync_lock);
308         if (list_empty(&ipvs->sync_queue)) {
309                 sb = NULL;
310                 __set_current_state(TASK_INTERRUPTIBLE);
311         } else {
312                 sb = list_entry(ipvs->sync_queue.next,
313                                 struct ip_vs_sync_buff,
314                                 list);
315                 list_del(&sb->list);
316                 ipvs->sync_queue_len--;
317                 if (!ipvs->sync_queue_len)
318                         ipvs->sync_queue_delay = 0;
319         }
320         spin_unlock_bh(&ipvs->sync_lock);
321
322         return sb;
323 }
324
325 /*
326  * Create a new sync buffer for Version 1 proto.
327  */
328 static inline struct ip_vs_sync_buff *
329 ip_vs_sync_buff_create(struct netns_ipvs *ipvs)
330 {
331         struct ip_vs_sync_buff *sb;
332
333         if (!(sb=kmalloc(sizeof(struct ip_vs_sync_buff), GFP_ATOMIC)))
334                 return NULL;
335
336         sb->mesg = kmalloc(ipvs->send_mesg_maxlen, GFP_ATOMIC);
337         if (!sb->mesg) {
338                 kfree(sb);
339                 return NULL;
340         }
341         sb->mesg->reserved = 0;  /* old nr_conns i.e. must be zeo now */
342         sb->mesg->version = SYNC_PROTO_VER;
343         sb->mesg->syncid = ipvs->master_syncid;
344         sb->mesg->size = sizeof(struct ip_vs_sync_mesg);
345         sb->mesg->nr_conns = 0;
346         sb->mesg->spare = 0;
347         sb->head = (unsigned char *)sb->mesg + sizeof(struct ip_vs_sync_mesg);
348         sb->end = (unsigned char *)sb->mesg + ipvs->send_mesg_maxlen;
349
350         sb->firstuse = jiffies;
351         return sb;
352 }
353
354 static inline void ip_vs_sync_buff_release(struct ip_vs_sync_buff *sb)
355 {
356         kfree(sb->mesg);
357         kfree(sb);
358 }
359
360 static inline void sb_queue_tail(struct netns_ipvs *ipvs)
361 {
362         struct ip_vs_sync_buff *sb = ipvs->sync_buff;
363
364         spin_lock(&ipvs->sync_lock);
365         if (ipvs->sync_state & IP_VS_STATE_MASTER &&
366             ipvs->sync_queue_len < sysctl_sync_qlen_max(ipvs)) {
367                 if (!ipvs->sync_queue_len)
368                         schedule_delayed_work(&ipvs->master_wakeup_work,
369                                               max(IPVS_SYNC_SEND_DELAY, 1));
370                 ipvs->sync_queue_len++;
371                 list_add_tail(&sb->list, &ipvs->sync_queue);
372                 if ((++ipvs->sync_queue_delay) == IPVS_SYNC_WAKEUP_RATE)
373                         wake_up_process(ipvs->master_thread);
374         } else
375                 ip_vs_sync_buff_release(sb);
376         spin_unlock(&ipvs->sync_lock);
377 }
378
379 /*
380  *      Get the current sync buffer if it has been created for more
381  *      than the specified time or the specified time is zero.
382  */
383 static inline struct ip_vs_sync_buff *
384 get_curr_sync_buff(struct netns_ipvs *ipvs, unsigned long time)
385 {
386         struct ip_vs_sync_buff *sb;
387
388         spin_lock_bh(&ipvs->sync_buff_lock);
389         if (ipvs->sync_buff &&
390             time_after_eq(jiffies - ipvs->sync_buff->firstuse, time)) {
391                 sb = ipvs->sync_buff;
392                 ipvs->sync_buff = NULL;
393                 __set_current_state(TASK_RUNNING);
394         } else
395                 sb = NULL;
396         spin_unlock_bh(&ipvs->sync_buff_lock);
397         return sb;
398 }
399
400 /*
401  * Switch mode from sending version 0 or 1
402  *  - must handle sync_buf
403  */
404 void ip_vs_sync_switch_mode(struct net *net, int mode)
405 {
406         struct netns_ipvs *ipvs = net_ipvs(net);
407         struct ip_vs_sync_buff *sb;
408
409         spin_lock_bh(&ipvs->sync_buff_lock);
410         if (!(ipvs->sync_state & IP_VS_STATE_MASTER))
411                 goto unlock;
412         sb = ipvs->sync_buff;
413         if (mode == sysctl_sync_ver(ipvs) || !sb)
414                 goto unlock;
415
416         /* Buffer empty ? then let buf_create do the job  */
417         if (sb->mesg->size <= sizeof(struct ip_vs_sync_mesg)) {
418                 ip_vs_sync_buff_release(sb);
419                 ipvs->sync_buff = NULL;
420         } else
421                 sb_queue_tail(ipvs);
422
423 unlock:
424         spin_unlock_bh(&ipvs->sync_buff_lock);
425 }
426
427 /*
428  * Create a new sync buffer for Version 0 proto.
429  */
430 static inline struct ip_vs_sync_buff *
431 ip_vs_sync_buff_create_v0(struct netns_ipvs *ipvs)
432 {
433         struct ip_vs_sync_buff *sb;
434         struct ip_vs_sync_mesg_v0 *mesg;
435
436         if (!(sb=kmalloc(sizeof(struct ip_vs_sync_buff), GFP_ATOMIC)))
437                 return NULL;
438
439         sb->mesg = kmalloc(ipvs->send_mesg_maxlen, GFP_ATOMIC);
440         if (!sb->mesg) {
441                 kfree(sb);
442                 return NULL;
443         }
444         mesg = (struct ip_vs_sync_mesg_v0 *)sb->mesg;
445         mesg->nr_conns = 0;
446         mesg->syncid = ipvs->master_syncid;
447         mesg->size = sizeof(struct ip_vs_sync_mesg_v0);
448         sb->head = (unsigned char *)mesg + sizeof(struct ip_vs_sync_mesg_v0);
449         sb->end = (unsigned char *)mesg + ipvs->send_mesg_maxlen;
450         sb->firstuse = jiffies;
451         return sb;
452 }
453
454 /*
455  *      Version 0 , could be switched in by sys_ctl.
456  *      Add an ip_vs_conn information into the current sync_buff.
457  */
458 void ip_vs_sync_conn_v0(struct net *net, struct ip_vs_conn *cp)
459 {
460         struct netns_ipvs *ipvs = net_ipvs(net);
461         struct ip_vs_sync_mesg_v0 *m;
462         struct ip_vs_sync_conn_v0 *s;
463         int len;
464
465         if (unlikely(cp->af != AF_INET))
466                 return;
467         /* Do not sync ONE PACKET */
468         if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
469                 return;
470
471         spin_lock(&ipvs->sync_buff_lock);
472         if (!ipvs->sync_buff) {
473                 ipvs->sync_buff =
474                         ip_vs_sync_buff_create_v0(ipvs);
475                 if (!ipvs->sync_buff) {
476                         spin_unlock(&ipvs->sync_buff_lock);
477                         pr_err("ip_vs_sync_buff_create failed.\n");
478                         return;
479                 }
480         }
481
482         len = (cp->flags & IP_VS_CONN_F_SEQ_MASK) ? FULL_CONN_SIZE :
483                 SIMPLE_CONN_SIZE;
484         m = (struct ip_vs_sync_mesg_v0 *)ipvs->sync_buff->mesg;
485         s = (struct ip_vs_sync_conn_v0 *)ipvs->sync_buff->head;
486
487         /* copy members */
488         s->reserved = 0;
489         s->protocol = cp->protocol;
490         s->cport = cp->cport;
491         s->vport = cp->vport;
492         s->dport = cp->dport;
493         s->caddr = cp->caddr.ip;
494         s->vaddr = cp->vaddr.ip;
495         s->daddr = cp->daddr.ip;
496         s->flags = htons(cp->flags & ~IP_VS_CONN_F_HASHED);
497         s->state = htons(cp->state);
498         if (cp->flags & IP_VS_CONN_F_SEQ_MASK) {
499                 struct ip_vs_sync_conn_options *opt =
500                         (struct ip_vs_sync_conn_options *)&s[1];
501                 memcpy(opt, &cp->in_seq, sizeof(*opt));
502         }
503
504         m->nr_conns++;
505         m->size += len;
506         ipvs->sync_buff->head += len;
507
508         /* check if there is a space for next one */
509         if (ipvs->sync_buff->head + FULL_CONN_SIZE > ipvs->sync_buff->end) {
510                 sb_queue_tail(ipvs);
511                 ipvs->sync_buff = NULL;
512         }
513         spin_unlock(&ipvs->sync_buff_lock);
514
515         /* synchronize its controller if it has */
516         if (cp->control)
517                 ip_vs_sync_conn(net, cp->control);
518 }
519
520 /*
521  *      Add an ip_vs_conn information into the current sync_buff.
522  *      Called by ip_vs_in.
523  *      Sending Version 1 messages
524  */
525 void ip_vs_sync_conn(struct net *net, struct ip_vs_conn *cp)
526 {
527         struct netns_ipvs *ipvs = net_ipvs(net);
528         struct ip_vs_sync_mesg *m;
529         union ip_vs_sync_conn *s;
530         __u8 *p;
531         unsigned int len, pe_name_len, pad;
532
533         /* Handle old version of the protocol */
534         if (sysctl_sync_ver(ipvs) == 0) {
535                 ip_vs_sync_conn_v0(net, cp);
536                 return;
537         }
538         /* Do not sync ONE PACKET */
539         if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
540                 goto control;
541 sloop:
542         /* Sanity checks */
543         pe_name_len = 0;
544         if (cp->pe_data_len) {
545                 if (!cp->pe_data || !cp->dest) {
546                         IP_VS_ERR_RL("SYNC, connection pe_data invalid\n");
547                         return;
548                 }
549                 pe_name_len = strnlen(cp->pe->name, IP_VS_PENAME_MAXLEN);
550         }
551
552         spin_lock(&ipvs->sync_buff_lock);
553
554 #ifdef CONFIG_IP_VS_IPV6
555         if (cp->af == AF_INET6)
556                 len = sizeof(struct ip_vs_sync_v6);
557         else
558 #endif
559                 len = sizeof(struct ip_vs_sync_v4);
560
561         if (cp->flags & IP_VS_CONN_F_SEQ_MASK)
562                 len += sizeof(struct ip_vs_sync_conn_options) + 2;
563
564         if (cp->pe_data_len)
565                 len += cp->pe_data_len + 2;     /* + Param hdr field */
566         if (pe_name_len)
567                 len += pe_name_len + 2;
568
569         /* check if there is a space for this one  */
570         pad = 0;
571         if (ipvs->sync_buff) {
572                 pad = (4 - (size_t)ipvs->sync_buff->head) & 3;
573                 if (ipvs->sync_buff->head + len + pad > ipvs->sync_buff->end) {
574                         sb_queue_tail(ipvs);
575                         ipvs->sync_buff = NULL;
576                         pad = 0;
577                 }
578         }
579
580         if (!ipvs->sync_buff) {
581                 ipvs->sync_buff = ip_vs_sync_buff_create(ipvs);
582                 if (!ipvs->sync_buff) {
583                         spin_unlock(&ipvs->sync_buff_lock);
584                         pr_err("ip_vs_sync_buff_create failed.\n");
585                         return;
586                 }
587         }
588
589         m = ipvs->sync_buff->mesg;
590         p = ipvs->sync_buff->head;
591         ipvs->sync_buff->head += pad + len;
592         m->size += pad + len;
593         /* Add ev. padding from prev. sync_conn */
594         while (pad--)
595                 *(p++) = 0;
596
597         s = (union ip_vs_sync_conn *)p;
598
599         /* Set message type  & copy members */
600         s->v4.type = (cp->af == AF_INET6 ? STYPE_F_INET6 : 0);
601         s->v4.ver_size = htons(len & SVER_MASK);        /* Version 0 */
602         s->v4.flags = htonl(cp->flags & ~IP_VS_CONN_F_HASHED);
603         s->v4.state = htons(cp->state);
604         s->v4.protocol = cp->protocol;
605         s->v4.cport = cp->cport;
606         s->v4.vport = cp->vport;
607         s->v4.dport = cp->dport;
608         s->v4.fwmark = htonl(cp->fwmark);
609         s->v4.timeout = htonl(cp->timeout / HZ);
610         m->nr_conns++;
611
612 #ifdef CONFIG_IP_VS_IPV6
613         if (cp->af == AF_INET6) {
614                 p += sizeof(struct ip_vs_sync_v6);
615                 s->v6.caddr = cp->caddr.in6;
616                 s->v6.vaddr = cp->vaddr.in6;
617                 s->v6.daddr = cp->daddr.in6;
618         } else
619 #endif
620         {
621                 p += sizeof(struct ip_vs_sync_v4);      /* options ptr */
622                 s->v4.caddr = cp->caddr.ip;
623                 s->v4.vaddr = cp->vaddr.ip;
624                 s->v4.daddr = cp->daddr.ip;
625         }
626         if (cp->flags & IP_VS_CONN_F_SEQ_MASK) {
627                 *(p++) = IPVS_OPT_SEQ_DATA;
628                 *(p++) = sizeof(struct ip_vs_sync_conn_options);
629                 hton_seq((struct ip_vs_seq *)p, &cp->in_seq);
630                 p += sizeof(struct ip_vs_seq);
631                 hton_seq((struct ip_vs_seq *)p, &cp->out_seq);
632                 p += sizeof(struct ip_vs_seq);
633         }
634         /* Handle pe data */
635         if (cp->pe_data_len && cp->pe_data) {
636                 *(p++) = IPVS_OPT_PE_DATA;
637                 *(p++) = cp->pe_data_len;
638                 memcpy(p, cp->pe_data, cp->pe_data_len);
639                 p += cp->pe_data_len;
640                 if (pe_name_len) {
641                         /* Add PE_NAME */
642                         *(p++) = IPVS_OPT_PE_NAME;
643                         *(p++) = pe_name_len;
644                         memcpy(p, cp->pe->name, pe_name_len);
645                         p += pe_name_len;
646                 }
647         }
648
649         spin_unlock(&ipvs->sync_buff_lock);
650
651 control:
652         /* synchronize its controller if it has */
653         cp = cp->control;
654         if (!cp)
655                 return;
656         /*
657          * Reduce sync rate for templates
658          * i.e only increment in_pkts for Templates.
659          */
660         if (cp->flags & IP_VS_CONN_F_TEMPLATE) {
661                 int pkts = atomic_add_return(1, &cp->in_pkts);
662
663                 if (pkts % sysctl_sync_period(ipvs) != 1)
664                         return;
665         }
666         goto sloop;
667 }
668
669 /*
670  *  fill_param used by version 1
671  */
672 static inline int
673 ip_vs_conn_fill_param_sync(struct net *net, int af, union ip_vs_sync_conn *sc,
674                            struct ip_vs_conn_param *p,
675                            __u8 *pe_data, unsigned int pe_data_len,
676                            __u8 *pe_name, unsigned int pe_name_len)
677 {
678 #ifdef CONFIG_IP_VS_IPV6
679         if (af == AF_INET6)
680                 ip_vs_conn_fill_param(net, af, sc->v6.protocol,
681                                       (const union nf_inet_addr *)&sc->v6.caddr,
682                                       sc->v6.cport,
683                                       (const union nf_inet_addr *)&sc->v6.vaddr,
684                                       sc->v6.vport, p);
685         else
686 #endif
687                 ip_vs_conn_fill_param(net, af, sc->v4.protocol,
688                                       (const union nf_inet_addr *)&sc->v4.caddr,
689                                       sc->v4.cport,
690                                       (const union nf_inet_addr *)&sc->v4.vaddr,
691                                       sc->v4.vport, p);
692         /* Handle pe data */
693         if (pe_data_len) {
694                 if (pe_name_len) {
695                         char buff[IP_VS_PENAME_MAXLEN+1];
696
697                         memcpy(buff, pe_name, pe_name_len);
698                         buff[pe_name_len]=0;
699                         p->pe = __ip_vs_pe_getbyname(buff);
700                         if (!p->pe) {
701                                 IP_VS_DBG(3, "BACKUP, no %s engine found/loaded\n",
702                                              buff);
703                                 return 1;
704                         }
705                 } else {
706                         IP_VS_ERR_RL("BACKUP, Invalid PE parameters\n");
707                         return 1;
708                 }
709
710                 p->pe_data = kmemdup(pe_data, pe_data_len, GFP_ATOMIC);
711                 if (!p->pe_data) {
712                         if (p->pe->module)
713                                 module_put(p->pe->module);
714                         return -ENOMEM;
715                 }
716                 p->pe_data_len = pe_data_len;
717         }
718         return 0;
719 }
720
721 /*
722  *  Connection Add / Update.
723  *  Common for version 0 and 1 reception of backup sync_conns.
724  *  Param: ...
725  *         timeout is in sec.
726  */
727 static void ip_vs_proc_conn(struct net *net, struct ip_vs_conn_param *param,
728                             unsigned int flags, unsigned int state,
729                             unsigned int protocol, unsigned int type,
730                             const union nf_inet_addr *daddr, __be16 dport,
731                             unsigned long timeout, __u32 fwmark,
732                             struct ip_vs_sync_conn_options *opt)
733 {
734         struct ip_vs_dest *dest;
735         struct ip_vs_conn *cp;
736         struct netns_ipvs *ipvs = net_ipvs(net);
737
738         if (!(flags & IP_VS_CONN_F_TEMPLATE))
739                 cp = ip_vs_conn_in_get(param);
740         else
741                 cp = ip_vs_ct_in_get(param);
742
743         if (cp) {
744                 /* Free pe_data */
745                 kfree(param->pe_data);
746
747                 dest = cp->dest;
748                 if ((cp->flags ^ flags) & IP_VS_CONN_F_INACTIVE &&
749                     !(flags & IP_VS_CONN_F_TEMPLATE) && dest) {
750                         if (flags & IP_VS_CONN_F_INACTIVE) {
751                                 atomic_dec(&dest->activeconns);
752                                 atomic_inc(&dest->inactconns);
753                         } else {
754                                 atomic_inc(&dest->activeconns);
755                                 atomic_dec(&dest->inactconns);
756                         }
757                 }
758                 flags &= IP_VS_CONN_F_BACKUP_UPD_MASK;
759                 flags |= cp->flags & ~IP_VS_CONN_F_BACKUP_UPD_MASK;
760                 cp->flags = flags;
761                 if (!dest) {
762                         dest = ip_vs_try_bind_dest(cp);
763                         if (dest)
764                                 atomic_dec(&dest->refcnt);
765                 }
766         } else {
767                 /*
768                  * Find the appropriate destination for the connection.
769                  * If it is not found the connection will remain unbound
770                  * but still handled.
771                  */
772                 dest = ip_vs_find_dest(net, type, daddr, dport, param->vaddr,
773                                        param->vport, protocol, fwmark, flags);
774
775                 cp = ip_vs_conn_new(param, daddr, dport, flags, dest, fwmark);
776                 if (dest)
777                         atomic_dec(&dest->refcnt);
778                 if (!cp) {
779                         if (param->pe_data)
780                                 kfree(param->pe_data);
781                         IP_VS_DBG(2, "BACKUP, add new conn. failed\n");
782                         return;
783                 }
784         }
785
786         if (opt)
787                 memcpy(&cp->in_seq, opt, sizeof(*opt));
788         atomic_set(&cp->in_pkts, sysctl_sync_threshold(ipvs));
789         cp->state = state;
790         cp->old_state = cp->state;
791         /*
792          * For Ver 0 messages style
793          *  - Not possible to recover the right timeout for templates
794          *  - can not find the right fwmark
795          *    virtual service. If needed, we can do it for
796          *    non-fwmark persistent services.
797          * Ver 1 messages style.
798          *  - No problem.
799          */
800         if (timeout) {
801                 if (timeout > MAX_SCHEDULE_TIMEOUT / HZ)
802                         timeout = MAX_SCHEDULE_TIMEOUT / HZ;
803                 cp->timeout = timeout*HZ;
804         } else {
805                 struct ip_vs_proto_data *pd;
806
807                 pd = ip_vs_proto_data_get(net, protocol);
808                 if (!(flags & IP_VS_CONN_F_TEMPLATE) && pd && pd->timeout_table)
809                         cp->timeout = pd->timeout_table[state];
810                 else
811                         cp->timeout = (3*60*HZ);
812         }
813         ip_vs_conn_put(cp);
814 }
815
816 /*
817  *  Process received multicast message for Version 0
818  */
819 static void ip_vs_process_message_v0(struct net *net, const char *buffer,
820                                      const size_t buflen)
821 {
822         struct ip_vs_sync_mesg_v0 *m = (struct ip_vs_sync_mesg_v0 *)buffer;
823         struct ip_vs_sync_conn_v0 *s;
824         struct ip_vs_sync_conn_options *opt;
825         struct ip_vs_protocol *pp;
826         struct ip_vs_conn_param param;
827         char *p;
828         int i;
829
830         p = (char *)buffer + sizeof(struct ip_vs_sync_mesg_v0);
831         for (i=0; i<m->nr_conns; i++) {
832                 unsigned int flags, state;
833
834                 if (p + SIMPLE_CONN_SIZE > buffer+buflen) {
835                         IP_VS_ERR_RL("BACKUP v0, bogus conn\n");
836                         return;
837                 }
838                 s = (struct ip_vs_sync_conn_v0 *) p;
839                 flags = ntohs(s->flags) | IP_VS_CONN_F_SYNC;
840                 flags &= ~IP_VS_CONN_F_HASHED;
841                 if (flags & IP_VS_CONN_F_SEQ_MASK) {
842                         opt = (struct ip_vs_sync_conn_options *)&s[1];
843                         p += FULL_CONN_SIZE;
844                         if (p > buffer+buflen) {
845                                 IP_VS_ERR_RL("BACKUP v0, Dropping buffer bogus conn options\n");
846                                 return;
847                         }
848                 } else {
849                         opt = NULL;
850                         p += SIMPLE_CONN_SIZE;
851                 }
852
853                 state = ntohs(s->state);
854                 if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
855                         pp = ip_vs_proto_get(s->protocol);
856                         if (!pp) {
857                                 IP_VS_DBG(2, "BACKUP v0, Unsupported protocol %u\n",
858                                         s->protocol);
859                                 continue;
860                         }
861                         if (state >= pp->num_states) {
862                                 IP_VS_DBG(2, "BACKUP v0, Invalid %s state %u\n",
863                                         pp->name, state);
864                                 continue;
865                         }
866                 } else {
867                         /* protocol in templates is not used for state/timeout */
868                         if (state > 0) {
869                                 IP_VS_DBG(2, "BACKUP v0, Invalid template state %u\n",
870                                         state);
871                                 state = 0;
872                         }
873                 }
874
875                 ip_vs_conn_fill_param(net, AF_INET, s->protocol,
876                                       (const union nf_inet_addr *)&s->caddr,
877                                       s->cport,
878                                       (const union nf_inet_addr *)&s->vaddr,
879                                       s->vport, &param);
880
881                 /* Send timeout as Zero */
882                 ip_vs_proc_conn(net, &param, flags, state, s->protocol, AF_INET,
883                                 (union nf_inet_addr *)&s->daddr, s->dport,
884                                 0, 0, opt);
885         }
886 }
887
888 /*
889  * Handle options
890  */
891 static inline int ip_vs_proc_seqopt(__u8 *p, unsigned int plen,
892                                     __u32 *opt_flags,
893                                     struct ip_vs_sync_conn_options *opt)
894 {
895         struct ip_vs_sync_conn_options *topt;
896
897         topt = (struct ip_vs_sync_conn_options *)p;
898
899         if (plen != sizeof(struct ip_vs_sync_conn_options)) {
900                 IP_VS_DBG(2, "BACKUP, bogus conn options length\n");
901                 return -EINVAL;
902         }
903         if (*opt_flags & IPVS_OPT_F_SEQ_DATA) {
904                 IP_VS_DBG(2, "BACKUP, conn options found twice\n");
905                 return -EINVAL;
906         }
907         ntoh_seq(&topt->in_seq, &opt->in_seq);
908         ntoh_seq(&topt->out_seq, &opt->out_seq);
909         *opt_flags |= IPVS_OPT_F_SEQ_DATA;
910         return 0;
911 }
912
913 static int ip_vs_proc_str(__u8 *p, unsigned int plen, unsigned int *data_len,
914                           __u8 **data, unsigned int maxlen,
915                           __u32 *opt_flags, __u32 flag)
916 {
917         if (plen > maxlen) {
918                 IP_VS_DBG(2, "BACKUP, bogus par.data len > %d\n", maxlen);
919                 return -EINVAL;
920         }
921         if (*opt_flags & flag) {
922                 IP_VS_DBG(2, "BACKUP, Par.data found twice 0x%x\n", flag);
923                 return -EINVAL;
924         }
925         *data_len = plen;
926         *data = p;
927         *opt_flags |= flag;
928         return 0;
929 }
930 /*
931  *   Process a Version 1 sync. connection
932  */
933 static inline int ip_vs_proc_sync_conn(struct net *net, __u8 *p, __u8 *msg_end)
934 {
935         struct ip_vs_sync_conn_options opt;
936         union  ip_vs_sync_conn *s;
937         struct ip_vs_protocol *pp;
938         struct ip_vs_conn_param param;
939         __u32 flags;
940         unsigned int af, state, pe_data_len=0, pe_name_len=0;
941         __u8 *pe_data=NULL, *pe_name=NULL;
942         __u32 opt_flags=0;
943         int retc=0;
944
945         s = (union ip_vs_sync_conn *) p;
946
947         if (s->v6.type & STYPE_F_INET6) {
948 #ifdef CONFIG_IP_VS_IPV6
949                 af = AF_INET6;
950                 p += sizeof(struct ip_vs_sync_v6);
951 #else
952                 IP_VS_DBG(3,"BACKUP, IPv6 msg received, and IPVS is not compiled for IPv6\n");
953                 retc = 10;
954                 goto out;
955 #endif
956         } else if (!s->v4.type) {
957                 af = AF_INET;
958                 p += sizeof(struct ip_vs_sync_v4);
959         } else {
960                 return -10;
961         }
962         if (p > msg_end)
963                 return -20;
964
965         /* Process optional params check Type & Len. */
966         while (p < msg_end) {
967                 int ptype;
968                 int plen;
969
970                 if (p+2 > msg_end)
971                         return -30;
972                 ptype = *(p++);
973                 plen  = *(p++);
974
975                 if (!plen || ((p + plen) > msg_end))
976                         return -40;
977                 /* Handle seq option  p = param data */
978                 switch (ptype & ~IPVS_OPT_F_PARAM) {
979                 case IPVS_OPT_SEQ_DATA:
980                         if (ip_vs_proc_seqopt(p, plen, &opt_flags, &opt))
981                                 return -50;
982                         break;
983
984                 case IPVS_OPT_PE_DATA:
985                         if (ip_vs_proc_str(p, plen, &pe_data_len, &pe_data,
986                                            IP_VS_PEDATA_MAXLEN, &opt_flags,
987                                            IPVS_OPT_F_PE_DATA))
988                                 return -60;
989                         break;
990
991                 case IPVS_OPT_PE_NAME:
992                         if (ip_vs_proc_str(p, plen,&pe_name_len, &pe_name,
993                                            IP_VS_PENAME_MAXLEN, &opt_flags,
994                                            IPVS_OPT_F_PE_NAME))
995                                 return -70;
996                         break;
997
998                 default:
999                         /* Param data mandatory ? */
1000                         if (!(ptype & IPVS_OPT_F_PARAM)) {
1001                                 IP_VS_DBG(3, "BACKUP, Unknown mandatory param %d found\n",
1002                                           ptype & ~IPVS_OPT_F_PARAM);
1003                                 retc = 20;
1004                                 goto out;
1005                         }
1006                 }
1007                 p += plen;  /* Next option */
1008         }
1009
1010         /* Get flags and Mask off unsupported */
1011         flags  = ntohl(s->v4.flags) & IP_VS_CONN_F_BACKUP_MASK;
1012         flags |= IP_VS_CONN_F_SYNC;
1013         state = ntohs(s->v4.state);
1014
1015         if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
1016                 pp = ip_vs_proto_get(s->v4.protocol);
1017                 if (!pp) {
1018                         IP_VS_DBG(3,"BACKUP, Unsupported protocol %u\n",
1019                                 s->v4.protocol);
1020                         retc = 30;
1021                         goto out;
1022                 }
1023                 if (state >= pp->num_states) {
1024                         IP_VS_DBG(3, "BACKUP, Invalid %s state %u\n",
1025                                 pp->name, state);
1026                         retc = 40;
1027                         goto out;
1028                 }
1029         } else {
1030                 /* protocol in templates is not used for state/timeout */
1031                 if (state > 0) {
1032                         IP_VS_DBG(3, "BACKUP, Invalid template state %u\n",
1033                                 state);
1034                         state = 0;
1035                 }
1036         }
1037         if (ip_vs_conn_fill_param_sync(net, af, s, &param, pe_data,
1038                                        pe_data_len, pe_name, pe_name_len)) {
1039                 retc = 50;
1040                 goto out;
1041         }
1042         /* If only IPv4, just silent skip IPv6 */
1043         if (af == AF_INET)
1044                 ip_vs_proc_conn(net, &param, flags, state, s->v4.protocol, af,
1045                                 (union nf_inet_addr *)&s->v4.daddr, s->v4.dport,
1046                                 ntohl(s->v4.timeout), ntohl(s->v4.fwmark),
1047                                 (opt_flags & IPVS_OPT_F_SEQ_DATA ? &opt : NULL)
1048                                 );
1049 #ifdef CONFIG_IP_VS_IPV6
1050         else
1051                 ip_vs_proc_conn(net, &param, flags, state, s->v6.protocol, af,
1052                                 (union nf_inet_addr *)&s->v6.daddr, s->v6.dport,
1053                                 ntohl(s->v6.timeout), ntohl(s->v6.fwmark),
1054                                 (opt_flags & IPVS_OPT_F_SEQ_DATA ? &opt : NULL)
1055                                 );
1056 #endif
1057         return 0;
1058         /* Error exit */
1059 out:
1060         IP_VS_DBG(2, "BACKUP, Single msg dropped err:%d\n", retc);
1061         return retc;
1062
1063 }
1064 /*
1065  *      Process received multicast message and create the corresponding
1066  *      ip_vs_conn entries.
1067  *      Handles Version 0 & 1
1068  */
1069 static void ip_vs_process_message(struct net *net, __u8 *buffer,
1070                                   const size_t buflen)
1071 {
1072         struct netns_ipvs *ipvs = net_ipvs(net);
1073         struct ip_vs_sync_mesg *m2 = (struct ip_vs_sync_mesg *)buffer;
1074         __u8 *p, *msg_end;
1075         int i, nr_conns;
1076
1077         if (buflen < sizeof(struct ip_vs_sync_mesg_v0)) {
1078                 IP_VS_DBG(2, "BACKUP, message header too short\n");
1079                 return;
1080         }
1081         /* Convert size back to host byte order */
1082         m2->size = ntohs(m2->size);
1083
1084         if (buflen != m2->size) {
1085                 IP_VS_DBG(2, "BACKUP, bogus message size\n");
1086                 return;
1087         }
1088         /* SyncID sanity check */
1089         if (ipvs->backup_syncid != 0 && m2->syncid != ipvs->backup_syncid) {
1090                 IP_VS_DBG(7, "BACKUP, Ignoring syncid = %d\n", m2->syncid);
1091                 return;
1092         }
1093         /* Handle version 1  message */
1094         if ((m2->version == SYNC_PROTO_VER) && (m2->reserved == 0)
1095             && (m2->spare == 0)) {
1096
1097                 msg_end = buffer + sizeof(struct ip_vs_sync_mesg);
1098                 nr_conns = m2->nr_conns;
1099
1100                 for (i=0; i<nr_conns; i++) {
1101                         union ip_vs_sync_conn *s;
1102                         unsigned int size;
1103                         int retc;
1104
1105                         p = msg_end;
1106                         if (p + sizeof(s->v4) > buffer+buflen) {
1107                                 IP_VS_ERR_RL("BACKUP, Dropping buffer, to small\n");
1108                                 return;
1109                         }
1110                         s = (union ip_vs_sync_conn *)p;
1111                         size = ntohs(s->v4.ver_size) & SVER_MASK;
1112                         msg_end = p + size;
1113                         /* Basic sanity checks */
1114                         if (msg_end  > buffer+buflen) {
1115                                 IP_VS_ERR_RL("BACKUP, Dropping buffer, msg > buffer\n");
1116                                 return;
1117                         }
1118                         if (ntohs(s->v4.ver_size) >> SVER_SHIFT) {
1119                                 IP_VS_ERR_RL("BACKUP, Dropping buffer, Unknown version %d\n",
1120                                               ntohs(s->v4.ver_size) >> SVER_SHIFT);
1121                                 return;
1122                         }
1123                         /* Process a single sync_conn */
1124                         retc = ip_vs_proc_sync_conn(net, p, msg_end);
1125                         if (retc < 0) {
1126                                 IP_VS_ERR_RL("BACKUP, Dropping buffer, Err: %d in decoding\n",
1127                                              retc);
1128                                 return;
1129                         }
1130                         /* Make sure we have 32 bit alignment */
1131                         msg_end = p + ((size + 3) & ~3);
1132                 }
1133         } else {
1134                 /* Old type of message */
1135                 ip_vs_process_message_v0(net, buffer, buflen);
1136                 return;
1137         }
1138 }
1139
1140
1141 /*
1142  *      Setup sndbuf (mode=1) or rcvbuf (mode=0)
1143  */
1144 static void set_sock_size(struct sock *sk, int mode, int val)
1145 {
1146         /* setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &val, sizeof(val)); */
1147         /* setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &val, sizeof(val)); */
1148         lock_sock(sk);
1149         if (mode) {
1150                 val = clamp_t(int, val, (SOCK_MIN_SNDBUF + 1) / 2,
1151                               sysctl_wmem_max);
1152                 sk->sk_sndbuf = val * 2;
1153                 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
1154         } else {
1155                 val = clamp_t(int, val, (SOCK_MIN_RCVBUF + 1) / 2,
1156                               sysctl_rmem_max);
1157                 sk->sk_rcvbuf = val * 2;
1158                 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
1159         }
1160         release_sock(sk);
1161 }
1162
1163 /*
1164  *      Setup loopback of outgoing multicasts on a sending socket
1165  */
1166 static void set_mcast_loop(struct sock *sk, u_char loop)
1167 {
1168         struct inet_sock *inet = inet_sk(sk);
1169
1170         /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */
1171         lock_sock(sk);
1172         inet->mc_loop = loop ? 1 : 0;
1173         release_sock(sk);
1174 }
1175
1176 /*
1177  *      Specify TTL for outgoing multicasts on a sending socket
1178  */
1179 static void set_mcast_ttl(struct sock *sk, u_char ttl)
1180 {
1181         struct inet_sock *inet = inet_sk(sk);
1182
1183         /* setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); */
1184         lock_sock(sk);
1185         inet->mc_ttl = ttl;
1186         release_sock(sk);
1187 }
1188
1189 /*
1190  *      Specifiy default interface for outgoing multicasts
1191  */
1192 static int set_mcast_if(struct sock *sk, char *ifname)
1193 {
1194         struct net_device *dev;
1195         struct inet_sock *inet = inet_sk(sk);
1196         struct net *net = sock_net(sk);
1197
1198         dev = __dev_get_by_name(net, ifname);
1199         if (!dev)
1200                 return -ENODEV;
1201
1202         if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
1203                 return -EINVAL;
1204
1205         lock_sock(sk);
1206         inet->mc_index = dev->ifindex;
1207         /*  inet->mc_addr  = 0; */
1208         release_sock(sk);
1209
1210         return 0;
1211 }
1212
1213
1214 /*
1215  *      Set the maximum length of sync message according to the
1216  *      specified interface's MTU.
1217  */
1218 static int set_sync_mesg_maxlen(struct net *net, int sync_state)
1219 {
1220         struct netns_ipvs *ipvs = net_ipvs(net);
1221         struct net_device *dev;
1222         int num;
1223
1224         if (sync_state == IP_VS_STATE_MASTER) {
1225                 dev = __dev_get_by_name(net, ipvs->master_mcast_ifn);
1226                 if (!dev)
1227                         return -ENODEV;
1228
1229                 num = (dev->mtu - sizeof(struct iphdr) -
1230                        sizeof(struct udphdr) -
1231                        SYNC_MESG_HEADER_LEN - 20) / SIMPLE_CONN_SIZE;
1232                 ipvs->send_mesg_maxlen = SYNC_MESG_HEADER_LEN +
1233                         SIMPLE_CONN_SIZE * min(num, MAX_CONNS_PER_SYNCBUFF);
1234                 IP_VS_DBG(7, "setting the maximum length of sync sending "
1235                           "message %d.\n", ipvs->send_mesg_maxlen);
1236         } else if (sync_state == IP_VS_STATE_BACKUP) {
1237                 dev = __dev_get_by_name(net, ipvs->backup_mcast_ifn);
1238                 if (!dev)
1239                         return -ENODEV;
1240
1241                 ipvs->recv_mesg_maxlen = dev->mtu -
1242                         sizeof(struct iphdr) - sizeof(struct udphdr);
1243                 IP_VS_DBG(7, "setting the maximum length of sync receiving "
1244                           "message %d.\n", ipvs->recv_mesg_maxlen);
1245         }
1246
1247         return 0;
1248 }
1249
1250
1251 /*
1252  *      Join a multicast group.
1253  *      the group is specified by a class D multicast address 224.0.0.0/8
1254  *      in the in_addr structure passed in as a parameter.
1255  */
1256 static int
1257 join_mcast_group(struct sock *sk, struct in_addr *addr, char *ifname)
1258 {
1259         struct net *net = sock_net(sk);
1260         struct ip_mreqn mreq;
1261         struct net_device *dev;
1262         int ret;
1263
1264         memset(&mreq, 0, sizeof(mreq));
1265         memcpy(&mreq.imr_multiaddr, addr, sizeof(struct in_addr));
1266
1267         dev = __dev_get_by_name(net, ifname);
1268         if (!dev)
1269                 return -ENODEV;
1270         if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
1271                 return -EINVAL;
1272
1273         mreq.imr_ifindex = dev->ifindex;
1274
1275         lock_sock(sk);
1276         ret = ip_mc_join_group(sk, &mreq);
1277         release_sock(sk);
1278
1279         return ret;
1280 }
1281
1282
1283 static int bind_mcastif_addr(struct socket *sock, char *ifname)
1284 {
1285         struct net *net = sock_net(sock->sk);
1286         struct net_device *dev;
1287         __be32 addr;
1288         struct sockaddr_in sin;
1289
1290         dev = __dev_get_by_name(net, ifname);
1291         if (!dev)
1292                 return -ENODEV;
1293
1294         addr = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
1295         if (!addr)
1296                 pr_err("You probably need to specify IP address on "
1297                        "multicast interface.\n");
1298
1299         IP_VS_DBG(7, "binding socket with (%s) %pI4\n",
1300                   ifname, &addr);
1301
1302         /* Now bind the socket with the address of multicast interface */
1303         sin.sin_family       = AF_INET;
1304         sin.sin_addr.s_addr  = addr;
1305         sin.sin_port         = 0;
1306
1307         return sock->ops->bind(sock, (struct sockaddr*)&sin, sizeof(sin));
1308 }
1309
1310 /*
1311  *      Set up sending multicast socket over UDP
1312  */
1313 static struct socket *make_send_sock(struct net *net)
1314 {
1315         struct netns_ipvs *ipvs = net_ipvs(net);
1316         struct socket *sock;
1317         int result;
1318
1319         /* First create a socket move it to right name space later */
1320         result = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
1321         if (result < 0) {
1322                 pr_err("Error during creation of socket; terminating\n");
1323                 return ERR_PTR(result);
1324         }
1325         /*
1326          * Kernel sockets that are a part of a namespace, should not
1327          * hold a reference to a namespace in order to allow to stop it.
1328          * After sk_change_net should be released using sk_release_kernel.
1329          */
1330         sk_change_net(sock->sk, net);
1331         result = set_mcast_if(sock->sk, ipvs->master_mcast_ifn);
1332         if (result < 0) {
1333                 pr_err("Error setting outbound mcast interface\n");
1334                 goto error;
1335         }
1336
1337         set_mcast_loop(sock->sk, 0);
1338         set_mcast_ttl(sock->sk, 1);
1339         result = sysctl_sync_sock_size(ipvs);
1340         if (result > 0)
1341                 set_sock_size(sock->sk, 1, result);
1342
1343         result = bind_mcastif_addr(sock, ipvs->master_mcast_ifn);
1344         if (result < 0) {
1345                 pr_err("Error binding address of the mcast interface\n");
1346                 goto error;
1347         }
1348
1349         result = sock->ops->connect(sock, (struct sockaddr *) &mcast_addr,
1350                         sizeof(struct sockaddr), 0);
1351         if (result < 0) {
1352                 pr_err("Error connecting to the multicast addr\n");
1353                 goto error;
1354         }
1355
1356         return sock;
1357
1358 error:
1359         sk_release_kernel(sock->sk);
1360         return ERR_PTR(result);
1361 }
1362
1363
1364 /*
1365  *      Set up receiving multicast socket over UDP
1366  */
1367 static struct socket *make_receive_sock(struct net *net)
1368 {
1369         struct netns_ipvs *ipvs = net_ipvs(net);
1370         struct socket *sock;
1371         int result;
1372
1373         /* First create a socket */
1374         result = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
1375         if (result < 0) {
1376                 pr_err("Error during creation of socket; terminating\n");
1377                 return ERR_PTR(result);
1378         }
1379         /*
1380          * Kernel sockets that are a part of a namespace, should not
1381          * hold a reference to a namespace in order to allow to stop it.
1382          * After sk_change_net should be released using sk_release_kernel.
1383          */
1384         sk_change_net(sock->sk, net);
1385         /* it is equivalent to the REUSEADDR option in user-space */
1386         sock->sk->sk_reuse = SK_CAN_REUSE;
1387         result = sysctl_sync_sock_size(ipvs);
1388         if (result > 0)
1389                 set_sock_size(sock->sk, 0, result);
1390
1391         result = sock->ops->bind(sock, (struct sockaddr *) &mcast_addr,
1392                         sizeof(struct sockaddr));
1393         if (result < 0) {
1394                 pr_err("Error binding to the multicast addr\n");
1395                 goto error;
1396         }
1397
1398         /* join the multicast group */
1399         result = join_mcast_group(sock->sk,
1400                         (struct in_addr *) &mcast_addr.sin_addr,
1401                         ipvs->backup_mcast_ifn);
1402         if (result < 0) {
1403                 pr_err("Error joining to the multicast group\n");
1404                 goto error;
1405         }
1406
1407         return sock;
1408
1409 error:
1410         sk_release_kernel(sock->sk);
1411         return ERR_PTR(result);
1412 }
1413
1414
1415 static int
1416 ip_vs_send_async(struct socket *sock, const char *buffer, const size_t length)
1417 {
1418         struct msghdr   msg = {.msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL};
1419         struct kvec     iov;
1420         int             len;
1421
1422         EnterFunction(7);
1423         iov.iov_base     = (void *)buffer;
1424         iov.iov_len      = length;
1425
1426         len = kernel_sendmsg(sock, &msg, &iov, 1, (size_t)(length));
1427
1428         LeaveFunction(7);
1429         return len;
1430 }
1431
1432 static int
1433 ip_vs_send_sync_msg(struct socket *sock, struct ip_vs_sync_mesg *msg)
1434 {
1435         int msize;
1436         int ret;
1437
1438         msize = msg->size;
1439
1440         /* Put size in network byte order */
1441         msg->size = htons(msg->size);
1442
1443         ret = ip_vs_send_async(sock, (char *)msg, msize);
1444         if (ret >= 0 || ret == -EAGAIN)
1445                 return ret;
1446         pr_err("ip_vs_send_async error %d\n", ret);
1447         return 0;
1448 }
1449
1450 static int
1451 ip_vs_receive(struct socket *sock, char *buffer, const size_t buflen)
1452 {
1453         struct msghdr           msg = {NULL,};
1454         struct kvec             iov;
1455         int                     len;
1456
1457         EnterFunction(7);
1458
1459         /* Receive a packet */
1460         iov.iov_base     = buffer;
1461         iov.iov_len      = (size_t)buflen;
1462
1463         len = kernel_recvmsg(sock, &msg, &iov, 1, buflen, 0);
1464
1465         if (len < 0)
1466                 return -1;
1467
1468         LeaveFunction(7);
1469         return len;
1470 }
1471
1472 /* Wakeup the master thread for sending */
1473 static void master_wakeup_work_handler(struct work_struct *work)
1474 {
1475         struct netns_ipvs *ipvs = container_of(work, struct netns_ipvs,
1476                                                master_wakeup_work.work);
1477
1478         spin_lock_bh(&ipvs->sync_lock);
1479         if (ipvs->sync_queue_len &&
1480             ipvs->sync_queue_delay < IPVS_SYNC_WAKEUP_RATE) {
1481                 ipvs->sync_queue_delay = IPVS_SYNC_WAKEUP_RATE;
1482                 wake_up_process(ipvs->master_thread);
1483         }
1484         spin_unlock_bh(&ipvs->sync_lock);
1485 }
1486
1487 /* Get next buffer to send */
1488 static inline struct ip_vs_sync_buff *
1489 next_sync_buff(struct netns_ipvs *ipvs)
1490 {
1491         struct ip_vs_sync_buff *sb;
1492
1493         sb = sb_dequeue(ipvs);
1494         if (sb)
1495                 return sb;
1496         /* Do not delay entries in buffer for more than 2 seconds */
1497         return get_curr_sync_buff(ipvs, 2 * HZ);
1498 }
1499
1500 static int sync_thread_master(void *data)
1501 {
1502         struct ip_vs_sync_thread_data *tinfo = data;
1503         struct netns_ipvs *ipvs = net_ipvs(tinfo->net);
1504         struct sock *sk = tinfo->sock->sk;
1505         struct ip_vs_sync_buff *sb;
1506
1507         pr_info("sync thread started: state = MASTER, mcast_ifn = %s, "
1508                 "syncid = %d\n",
1509                 ipvs->master_mcast_ifn, ipvs->master_syncid);
1510
1511         for (;;) {
1512                 sb = next_sync_buff(ipvs);
1513                 if (unlikely(kthread_should_stop()))
1514                         break;
1515                 if (!sb) {
1516                         schedule_timeout(IPVS_SYNC_CHECK_PERIOD);
1517                         continue;
1518                 }
1519                 while (ip_vs_send_sync_msg(tinfo->sock, sb->mesg) < 0) {
1520                         int ret = 0;
1521
1522                         __wait_event_interruptible(*sk_sleep(sk),
1523                                                    sock_writeable(sk) ||
1524                                                    kthread_should_stop(),
1525                                                    ret);
1526                         if (unlikely(kthread_should_stop()))
1527                                 goto done;
1528                 }
1529                 ip_vs_sync_buff_release(sb);
1530         }
1531
1532 done:
1533         __set_current_state(TASK_RUNNING);
1534         if (sb)
1535                 ip_vs_sync_buff_release(sb);
1536
1537         /* clean up the sync_buff queue */
1538         while ((sb = sb_dequeue(ipvs)))
1539                 ip_vs_sync_buff_release(sb);
1540         __set_current_state(TASK_RUNNING);
1541
1542         /* clean up the current sync_buff */
1543         sb = get_curr_sync_buff(ipvs, 0);
1544         if (sb)
1545                 ip_vs_sync_buff_release(sb);
1546
1547         /* release the sending multicast socket */
1548         sk_release_kernel(tinfo->sock->sk);
1549         kfree(tinfo);
1550
1551         return 0;
1552 }
1553
1554
1555 static int sync_thread_backup(void *data)
1556 {
1557         struct ip_vs_sync_thread_data *tinfo = data;
1558         struct netns_ipvs *ipvs = net_ipvs(tinfo->net);
1559         int len;
1560
1561         pr_info("sync thread started: state = BACKUP, mcast_ifn = %s, "
1562                 "syncid = %d\n",
1563                 ipvs->backup_mcast_ifn, ipvs->backup_syncid);
1564
1565         while (!kthread_should_stop()) {
1566                 wait_event_interruptible(*sk_sleep(tinfo->sock->sk),
1567                          !skb_queue_empty(&tinfo->sock->sk->sk_receive_queue)
1568                          || kthread_should_stop());
1569
1570                 /* do we have data now? */
1571                 while (!skb_queue_empty(&(tinfo->sock->sk->sk_receive_queue))) {
1572                         len = ip_vs_receive(tinfo->sock, tinfo->buf,
1573                                         ipvs->recv_mesg_maxlen);
1574                         if (len <= 0) {
1575                                 pr_err("receiving message error\n");
1576                                 break;
1577                         }
1578
1579                         /* disable bottom half, because it accesses the data
1580                            shared by softirq while getting/creating conns */
1581                         local_bh_disable();
1582                         ip_vs_process_message(tinfo->net, tinfo->buf, len);
1583                         local_bh_enable();
1584                 }
1585         }
1586
1587         /* release the sending multicast socket */
1588         sk_release_kernel(tinfo->sock->sk);
1589         kfree(tinfo->buf);
1590         kfree(tinfo);
1591
1592         return 0;
1593 }
1594
1595
1596 int start_sync_thread(struct net *net, int state, char *mcast_ifn, __u8 syncid)
1597 {
1598         struct ip_vs_sync_thread_data *tinfo;
1599         struct task_struct **realtask, *task;
1600         struct socket *sock;
1601         struct netns_ipvs *ipvs = net_ipvs(net);
1602         char *name, *buf = NULL;
1603         int (*threadfn)(void *data);
1604         int result = -ENOMEM;
1605
1606         IP_VS_DBG(7, "%s(): pid %d\n", __func__, task_pid_nr(current));
1607         IP_VS_DBG(7, "Each ip_vs_sync_conn entry needs %Zd bytes\n",
1608                   sizeof(struct ip_vs_sync_conn_v0));
1609
1610
1611         if (state == IP_VS_STATE_MASTER) {
1612                 if (ipvs->master_thread)
1613                         return -EEXIST;
1614
1615                 strlcpy(ipvs->master_mcast_ifn, mcast_ifn,
1616                         sizeof(ipvs->master_mcast_ifn));
1617                 ipvs->master_syncid = syncid;
1618                 realtask = &ipvs->master_thread;
1619                 name = "ipvs_master:%d";
1620                 threadfn = sync_thread_master;
1621                 ipvs->sync_queue_len = 0;
1622                 ipvs->sync_queue_delay = 0;
1623                 INIT_DELAYED_WORK(&ipvs->master_wakeup_work,
1624                                   master_wakeup_work_handler);
1625                 sock = make_send_sock(net);
1626         } else if (state == IP_VS_STATE_BACKUP) {
1627                 if (ipvs->backup_thread)
1628                         return -EEXIST;
1629
1630                 strlcpy(ipvs->backup_mcast_ifn, mcast_ifn,
1631                         sizeof(ipvs->backup_mcast_ifn));
1632                 ipvs->backup_syncid = syncid;
1633                 realtask = &ipvs->backup_thread;
1634                 name = "ipvs_backup:%d";
1635                 threadfn = sync_thread_backup;
1636                 sock = make_receive_sock(net);
1637         } else {
1638                 return -EINVAL;
1639         }
1640
1641         if (IS_ERR(sock)) {
1642                 result = PTR_ERR(sock);
1643                 goto out;
1644         }
1645
1646         set_sync_mesg_maxlen(net, state);
1647         if (state == IP_VS_STATE_BACKUP) {
1648                 buf = kmalloc(ipvs->recv_mesg_maxlen, GFP_KERNEL);
1649                 if (!buf)
1650                         goto outsocket;
1651         }
1652
1653         tinfo = kmalloc(sizeof(*tinfo), GFP_KERNEL);
1654         if (!tinfo)
1655                 goto outbuf;
1656
1657         tinfo->net = net;
1658         tinfo->sock = sock;
1659         tinfo->buf = buf;
1660
1661         task = kthread_run(threadfn, tinfo, name, ipvs->gen);
1662         if (IS_ERR(task)) {
1663                 result = PTR_ERR(task);
1664                 goto outtinfo;
1665         }
1666
1667         /* mark as active */
1668         *realtask = task;
1669         ipvs->sync_state |= state;
1670
1671         /* increase the module use count */
1672         ip_vs_use_count_inc();
1673
1674         return 0;
1675
1676 outtinfo:
1677         kfree(tinfo);
1678 outbuf:
1679         kfree(buf);
1680 outsocket:
1681         sk_release_kernel(sock->sk);
1682 out:
1683         return result;
1684 }
1685
1686
1687 int stop_sync_thread(struct net *net, int state)
1688 {
1689         struct netns_ipvs *ipvs = net_ipvs(net);
1690         int retc = -EINVAL;
1691
1692         IP_VS_DBG(7, "%s(): pid %d\n", __func__, task_pid_nr(current));
1693
1694         if (state == IP_VS_STATE_MASTER) {
1695                 if (!ipvs->master_thread)
1696                         return -ESRCH;
1697
1698                 pr_info("stopping master sync thread %d ...\n",
1699                         task_pid_nr(ipvs->master_thread));
1700
1701                 /*
1702                  * The lock synchronizes with sb_queue_tail(), so that we don't
1703                  * add sync buffers to the queue, when we are already in
1704                  * progress of stopping the master sync daemon.
1705                  */
1706
1707                 spin_lock_bh(&ipvs->sync_lock);
1708                 ipvs->sync_state &= ~IP_VS_STATE_MASTER;
1709                 spin_unlock_bh(&ipvs->sync_lock);
1710                 cancel_delayed_work_sync(&ipvs->master_wakeup_work);
1711                 retc = kthread_stop(ipvs->master_thread);
1712                 ipvs->master_thread = NULL;
1713         } else if (state == IP_VS_STATE_BACKUP) {
1714                 if (!ipvs->backup_thread)
1715                         return -ESRCH;
1716
1717                 pr_info("stopping backup sync thread %d ...\n",
1718                         task_pid_nr(ipvs->backup_thread));
1719
1720                 ipvs->sync_state &= ~IP_VS_STATE_BACKUP;
1721                 retc = kthread_stop(ipvs->backup_thread);
1722                 ipvs->backup_thread = NULL;
1723         }
1724
1725         /* decrease the module use count */
1726         ip_vs_use_count_dec();
1727
1728         return retc;
1729 }
1730
1731 /*
1732  * Initialize data struct for each netns
1733  */
1734 int __net_init ip_vs_sync_net_init(struct net *net)
1735 {
1736         struct netns_ipvs *ipvs = net_ipvs(net);
1737
1738         __mutex_init(&ipvs->sync_mutex, "ipvs->sync_mutex", &__ipvs_sync_key);
1739         INIT_LIST_HEAD(&ipvs->sync_queue);
1740         spin_lock_init(&ipvs->sync_lock);
1741         spin_lock_init(&ipvs->sync_buff_lock);
1742
1743         ipvs->sync_mcast_addr.sin_family = AF_INET;
1744         ipvs->sync_mcast_addr.sin_port = cpu_to_be16(IP_VS_SYNC_PORT);
1745         ipvs->sync_mcast_addr.sin_addr.s_addr = cpu_to_be32(IP_VS_SYNC_GROUP);
1746         return 0;
1747 }
1748
1749 void ip_vs_sync_net_cleanup(struct net *net)
1750 {
1751         int retc;
1752         struct netns_ipvs *ipvs = net_ipvs(net);
1753
1754         mutex_lock(&ipvs->sync_mutex);
1755         retc = stop_sync_thread(net, IP_VS_STATE_MASTER);
1756         if (retc && retc != -ESRCH)
1757                 pr_err("Failed to stop Master Daemon\n");
1758
1759         retc = stop_sync_thread(net, IP_VS_STATE_BACKUP);
1760         if (retc && retc != -ESRCH)
1761                 pr_err("Failed to stop Backup Daemon\n");
1762         mutex_unlock(&ipvs->sync_mutex);
1763 }