1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#define KMSG_COMPONENT "IPVS"
28#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
29
30#include <linux/module.h>
31#include <linux/kernel.h>
32#include <linux/ip.h>
33#include <linux/tcp.h>
34#include <linux/sctp.h>
35#include <linux/icmp.h>
36#include <linux/slab.h>
37
38#include <net/ip.h>
39#include <net/tcp.h>
40#include <net/udp.h>
41#include <net/icmp.h>
42#include <net/route.h>
43#include <net/ip6_checksum.h>
44#include <net/netns/generic.h>
45
46#include <linux/netfilter.h>
47#include <linux/netfilter_ipv4.h>
48
49#ifdef CONFIG_IP_VS_IPV6
50#include <net/ipv6.h>
51#include <linux/netfilter_ipv6.h>
52#include <net/ip6_route.h>
53#endif
54
55#include <net/ip_vs.h>
56
57
58EXPORT_SYMBOL(register_ip_vs_scheduler);
59EXPORT_SYMBOL(unregister_ip_vs_scheduler);
60EXPORT_SYMBOL(ip_vs_proto_name);
61EXPORT_SYMBOL(ip_vs_conn_new);
62EXPORT_SYMBOL(ip_vs_conn_in_get);
63EXPORT_SYMBOL(ip_vs_conn_out_get);
64#ifdef CONFIG_IP_VS_PROTO_TCP
65EXPORT_SYMBOL(ip_vs_tcp_conn_listen);
66#endif
67EXPORT_SYMBOL(ip_vs_conn_put);
68#ifdef CONFIG_IP_VS_DEBUG
69EXPORT_SYMBOL(ip_vs_get_debug_level);
70#endif
71
72static int ip_vs_net_id __read_mostly;
73
74static atomic_t ipvs_netns_cnt = ATOMIC_INIT(0);
75
76
77#define icmp_id(icmph) (((icmph)->un).echo.id)
78#define icmpv6_id(icmph) (icmph->icmp6_dataun.u_echo.identifier)
79
80const char *ip_vs_proto_name(unsigned int proto)
81{
82 static char buf[20];
83
84 switch (proto) {
85 case IPPROTO_IP:
86 return "IP";
87 case IPPROTO_UDP:
88 return "UDP";
89 case IPPROTO_TCP:
90 return "TCP";
91 case IPPROTO_SCTP:
92 return "SCTP";
93 case IPPROTO_ICMP:
94 return "ICMP";
95#ifdef CONFIG_IP_VS_IPV6
96 case IPPROTO_ICMPV6:
97 return "ICMPv6";
98#endif
99 default:
100 sprintf(buf, "IP_%d", proto);
101 return buf;
102 }
103}
104
105void ip_vs_init_hash_table(struct list_head *table, int rows)
106{
107 while (--rows >= 0)
108 INIT_LIST_HEAD(&table[rows]);
109}
110
111static inline void
112ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
113{
114 struct ip_vs_dest *dest = cp->dest;
115 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
116
117 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
118 struct ip_vs_cpu_stats *s;
119 struct ip_vs_service *svc;
120
121 s = this_cpu_ptr(dest->stats.cpustats);
122 s->ustats.inpkts++;
123 u64_stats_update_begin(&s->syncp);
124 s->ustats.inbytes += skb->len;
125 u64_stats_update_end(&s->syncp);
126
127 rcu_read_lock();
128 svc = rcu_dereference(dest->svc);
129 s = this_cpu_ptr(svc->stats.cpustats);
130 s->ustats.inpkts++;
131 u64_stats_update_begin(&s->syncp);
132 s->ustats.inbytes += skb->len;
133 u64_stats_update_end(&s->syncp);
134 rcu_read_unlock();
135
136 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
137 s->ustats.inpkts++;
138 u64_stats_update_begin(&s->syncp);
139 s->ustats.inbytes += skb->len;
140 u64_stats_update_end(&s->syncp);
141 }
142}
143
144
145static inline void
146ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
147{
148 struct ip_vs_dest *dest = cp->dest;
149 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
150
151 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
152 struct ip_vs_cpu_stats *s;
153 struct ip_vs_service *svc;
154
155 s = this_cpu_ptr(dest->stats.cpustats);
156 s->ustats.outpkts++;
157 u64_stats_update_begin(&s->syncp);
158 s->ustats.outbytes += skb->len;
159 u64_stats_update_end(&s->syncp);
160
161 rcu_read_lock();
162 svc = rcu_dereference(dest->svc);
163 s = this_cpu_ptr(svc->stats.cpustats);
164 s->ustats.outpkts++;
165 u64_stats_update_begin(&s->syncp);
166 s->ustats.outbytes += skb->len;
167 u64_stats_update_end(&s->syncp);
168 rcu_read_unlock();
169
170 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
171 s->ustats.outpkts++;
172 u64_stats_update_begin(&s->syncp);
173 s->ustats.outbytes += skb->len;
174 u64_stats_update_end(&s->syncp);
175 }
176}
177
178
179static inline void
180ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
181{
182 struct netns_ipvs *ipvs = net_ipvs(svc->net);
183 struct ip_vs_cpu_stats *s;
184
185 s = this_cpu_ptr(cp->dest->stats.cpustats);
186 s->ustats.conns++;
187
188 s = this_cpu_ptr(svc->stats.cpustats);
189 s->ustats.conns++;
190
191 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
192 s->ustats.conns++;
193}
194
195
196static inline void
197ip_vs_set_state(struct ip_vs_conn *cp, int direction,
198 const struct sk_buff *skb,
199 struct ip_vs_proto_data *pd)
200{
201 if (likely(pd->pp->state_transition))
202 pd->pp->state_transition(cp, direction, skb, pd);
203}
204
205static inline int
206ip_vs_conn_fill_param_persist(const struct ip_vs_service *svc,
207 struct sk_buff *skb, int protocol,
208 const union nf_inet_addr *caddr, __be16 cport,
209 const union nf_inet_addr *vaddr, __be16 vport,
210 struct ip_vs_conn_param *p)
211{
212 ip_vs_conn_fill_param(svc->net, svc->af, protocol, caddr, cport, vaddr,
213 vport, p);
214 p->pe = rcu_dereference(svc->pe);
215 if (p->pe && p->pe->fill_param)
216 return p->pe->fill_param(p, skb);
217
218 return 0;
219}
220
221
222
223
224
225
226
227
228static struct ip_vs_conn *
229ip_vs_sched_persist(struct ip_vs_service *svc,
230 struct sk_buff *skb, __be16 src_port, __be16 dst_port,
231 int *ignored, struct ip_vs_iphdr *iph)
232{
233 struct ip_vs_conn *cp = NULL;
234 struct ip_vs_dest *dest;
235 struct ip_vs_conn *ct;
236 __be16 dport = 0;
237 unsigned int flags;
238 struct ip_vs_conn_param param;
239 const union nf_inet_addr fwmark = { .ip = htonl(svc->fwmark) };
240 union nf_inet_addr snet;
241
242
243
244#ifdef CONFIG_IP_VS_IPV6
245 if (svc->af == AF_INET6)
246 ipv6_addr_prefix(&snet.in6, &iph->saddr.in6,
247 (__force __u32) svc->netmask);
248 else
249#endif
250 snet.ip = iph->saddr.ip & svc->netmask;
251
252 IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
253 "mnet %s\n",
254 IP_VS_DBG_ADDR(svc->af, &iph->saddr), ntohs(src_port),
255 IP_VS_DBG_ADDR(svc->af, &iph->daddr), ntohs(dst_port),
256 IP_VS_DBG_ADDR(svc->af, &snet));
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271 {
272 int protocol = iph->protocol;
273 const union nf_inet_addr *vaddr = &iph->daddr;
274 __be16 vport = 0;
275
276 if (dst_port == svc->port) {
277
278
279
280
281
282 if (svc->port != FTPPORT)
283 vport = dst_port;
284 } else {
285
286
287
288
289
290
291
292 if (svc->fwmark) {
293 protocol = IPPROTO_IP;
294 vaddr = &fwmark;
295 }
296 }
297
298 if (ip_vs_conn_fill_param_persist(svc, skb, protocol, &snet, 0,
299 vaddr, vport, ¶m) < 0) {
300 *ignored = -1;
301 return NULL;
302 }
303 }
304
305
306 ct = ip_vs_ct_in_get(¶m);
307 if (!ct || !ip_vs_check_template(ct)) {
308 struct ip_vs_scheduler *sched;
309
310
311
312
313
314
315 sched = rcu_dereference(svc->scheduler);
316 dest = sched->schedule(svc, skb, iph);
317 if (!dest) {
318 IP_VS_DBG(1, "p-schedule: no dest found.\n");
319 kfree(param.pe_data);
320 *ignored = 0;
321 return NULL;
322 }
323
324 if (dst_port == svc->port && svc->port != FTPPORT)
325 dport = dest->port;
326
327
328
329
330
331 ct = ip_vs_conn_new(¶m, &dest->addr, dport,
332 IP_VS_CONN_F_TEMPLATE, dest, skb->mark);
333 if (ct == NULL) {
334 kfree(param.pe_data);
335 *ignored = -1;
336 return NULL;
337 }
338
339 ct->timeout = svc->timeout;
340 } else {
341
342 dest = ct->dest;
343 kfree(param.pe_data);
344 }
345
346 dport = dst_port;
347 if (dport == svc->port && dest->port)
348 dport = dest->port;
349
350 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
351 && iph->protocol == IPPROTO_UDP) ?
352 IP_VS_CONN_F_ONE_PACKET : 0;
353
354
355
356
357 ip_vs_conn_fill_param(svc->net, svc->af, iph->protocol, &iph->saddr,
358 src_port, &iph->daddr, dst_port, ¶m);
359
360 cp = ip_vs_conn_new(¶m, &dest->addr, dport, flags, dest, skb->mark);
361 if (cp == NULL) {
362 ip_vs_conn_put(ct);
363 *ignored = -1;
364 return NULL;
365 }
366
367
368
369
370 ip_vs_control_add(cp, ct);
371 ip_vs_conn_put(ct);
372
373 ip_vs_conn_stats(cp, svc);
374 return cp;
375}
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399struct ip_vs_conn *
400ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
401 struct ip_vs_proto_data *pd, int *ignored,
402 struct ip_vs_iphdr *iph)
403{
404 struct ip_vs_protocol *pp = pd->pp;
405 struct ip_vs_conn *cp = NULL;
406 struct ip_vs_scheduler *sched;
407 struct ip_vs_dest *dest;
408 __be16 _ports[2], *pptr;
409 unsigned int flags;
410
411 *ignored = 1;
412
413
414
415 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
416 if (pptr == NULL)
417 return NULL;
418
419
420
421
422
423
424
425 if (pptr[0] == FTPDATA) {
426 IP_VS_DBG_PKT(12, svc->af, pp, skb, 0,
427 "Not scheduling FTPDATA");
428 return NULL;
429 }
430
431
432
433
434 if ((!skb->dev || skb->dev->flags & IFF_LOOPBACK) &&
435 (cp = pp->conn_in_get(svc->af, skb, iph, 1))) {
436 IP_VS_DBG_PKT(12, svc->af, pp, skb, 0,
437 "Not scheduling reply for existing connection");
438 __ip_vs_conn_put(cp);
439 return NULL;
440 }
441
442
443
444
445 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
446 return ip_vs_sched_persist(svc, skb, pptr[0], pptr[1], ignored,
447 iph);
448
449 *ignored = 0;
450
451
452
453
454 if (!svc->fwmark && pptr[1] != svc->port) {
455 if (!svc->port)
456 pr_err("Schedule: port zero only supported "
457 "in persistent services, "
458 "check your ipvs configuration\n");
459 return NULL;
460 }
461
462 sched = rcu_dereference(svc->scheduler);
463 dest = sched->schedule(svc, skb, iph);
464 if (dest == NULL) {
465 IP_VS_DBG(1, "Schedule: no dest found.\n");
466 return NULL;
467 }
468
469 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
470 && iph->protocol == IPPROTO_UDP) ?
471 IP_VS_CONN_F_ONE_PACKET : 0;
472
473
474
475
476 {
477 struct ip_vs_conn_param p;
478
479 ip_vs_conn_fill_param(svc->net, svc->af, iph->protocol,
480 &iph->saddr, pptr[0], &iph->daddr,
481 pptr[1], &p);
482 cp = ip_vs_conn_new(&p, &dest->addr,
483 dest->port ? dest->port : pptr[1],
484 flags, dest, skb->mark);
485 if (!cp) {
486 *ignored = -1;
487 return NULL;
488 }
489 }
490
491 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
492 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
493 ip_vs_fwd_tag(cp),
494 IP_VS_DBG_ADDR(svc->af, &cp->caddr), ntohs(cp->cport),
495 IP_VS_DBG_ADDR(svc->af, &cp->vaddr), ntohs(cp->vport),
496 IP_VS_DBG_ADDR(svc->af, &cp->daddr), ntohs(cp->dport),
497 cp->flags, atomic_read(&cp->refcnt));
498
499 ip_vs_conn_stats(cp, svc);
500 return cp;
501}
502
503
504
505
506
507
508
509int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
510 struct ip_vs_proto_data *pd, struct ip_vs_iphdr *iph)
511{
512 __be16 _ports[2], *pptr;
513#ifdef CONFIG_SYSCTL
514 struct net *net;
515 struct netns_ipvs *ipvs;
516 int unicast;
517#endif
518
519 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
520 if (pptr == NULL) {
521 return NF_DROP;
522 }
523
524#ifdef CONFIG_SYSCTL
525 net = skb_net(skb);
526
527#ifdef CONFIG_IP_VS_IPV6
528 if (svc->af == AF_INET6)
529 unicast = ipv6_addr_type(&iph->daddr.in6) & IPV6_ADDR_UNICAST;
530 else
531#endif
532 unicast = (inet_addr_type(net, iph->daddr.ip) == RTN_UNICAST);
533
534
535
536
537 ipvs = net_ipvs(net);
538 if (ipvs->sysctl_cache_bypass && svc->fwmark && unicast) {
539 int ret;
540 struct ip_vs_conn *cp;
541 unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET &&
542 iph->protocol == IPPROTO_UDP) ?
543 IP_VS_CONN_F_ONE_PACKET : 0;
544 union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } };
545
546
547 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__);
548 {
549 struct ip_vs_conn_param p;
550 ip_vs_conn_fill_param(svc->net, svc->af, iph->protocol,
551 &iph->saddr, pptr[0],
552 &iph->daddr, pptr[1], &p);
553 cp = ip_vs_conn_new(&p, &daddr, 0,
554 IP_VS_CONN_F_BYPASS | flags,
555 NULL, skb->mark);
556 if (!cp)
557 return NF_DROP;
558 }
559
560
561 ip_vs_in_stats(cp, skb);
562
563
564 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
565
566
567 ret = cp->packet_xmit(skb, cp, pd->pp, iph);
568
569
570 atomic_inc(&cp->in_pkts);
571 ip_vs_conn_put(cp);
572 return ret;
573 }
574#endif
575
576
577
578
579
580
581
582 if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT))
583 return NF_ACCEPT;
584
585
586
587
588
589
590
591
592#ifdef CONFIG_IP_VS_IPV6
593 if (svc->af == AF_INET6) {
594 if (!skb->dev) {
595 struct net *net_ = dev_net(skb_dst(skb)->dev);
596
597 skb->dev = net_->loopback_dev;
598 }
599 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
600 } else
601#endif
602 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
603
604 return NF_DROP;
605}
606
607#ifdef CONFIG_SYSCTL
608
609static int sysctl_snat_reroute(struct sk_buff *skb)
610{
611 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
612 return ipvs->sysctl_snat_reroute;
613}
614
615static int sysctl_nat_icmp_send(struct net *net)
616{
617 struct netns_ipvs *ipvs = net_ipvs(net);
618 return ipvs->sysctl_nat_icmp_send;
619}
620
621static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs)
622{
623 return ipvs->sysctl_expire_nodest_conn;
624}
625
626#else
627
628static int sysctl_snat_reroute(struct sk_buff *skb) { return 0; }
629static int sysctl_nat_icmp_send(struct net *net) { return 0; }
630static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs) { return 0; }
631
632#endif
633
634__sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
635{
636 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
637}
638
639static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum)
640{
641 if (NF_INET_LOCAL_IN == hooknum)
642 return IP_DEFRAG_VS_IN;
643 if (NF_INET_FORWARD == hooknum)
644 return IP_DEFRAG_VS_FWD;
645 return IP_DEFRAG_VS_OUT;
646}
647
648static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
649{
650 int err;
651
652 local_bh_disable();
653 err = ip_defrag(skb, user);
654 local_bh_enable();
655 if (!err)
656 ip_send_check(ip_hdr(skb));
657
658 return err;
659}
660
661static int ip_vs_route_me_harder(int af, struct sk_buff *skb)
662{
663#ifdef CONFIG_IP_VS_IPV6
664 if (af == AF_INET6) {
665 if (sysctl_snat_reroute(skb) && ip6_route_me_harder(skb) != 0)
666 return 1;
667 } else
668#endif
669 if ((sysctl_snat_reroute(skb) ||
670 skb_rtable(skb)->rt_flags & RTCF_LOCAL) &&
671 ip_route_me_harder(skb, RTN_LOCAL) != 0)
672 return 1;
673
674 return 0;
675}
676
677
678
679
680
681void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
682 struct ip_vs_conn *cp, int inout)
683{
684 struct iphdr *iph = ip_hdr(skb);
685 unsigned int icmp_offset = iph->ihl*4;
686 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
687 icmp_offset);
688 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
689
690 if (inout) {
691 iph->saddr = cp->vaddr.ip;
692 ip_send_check(iph);
693 ciph->daddr = cp->vaddr.ip;
694 ip_send_check(ciph);
695 } else {
696 iph->daddr = cp->daddr.ip;
697 ip_send_check(iph);
698 ciph->saddr = cp->daddr.ip;
699 ip_send_check(ciph);
700 }
701
702
703 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol ||
704 IPPROTO_SCTP == ciph->protocol) {
705 __be16 *ports = (void *)ciph + ciph->ihl*4;
706
707 if (inout)
708 ports[1] = cp->vport;
709 else
710 ports[0] = cp->dport;
711 }
712
713
714 icmph->checksum = 0;
715 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
716 skb->ip_summed = CHECKSUM_UNNECESSARY;
717
718 if (inout)
719 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
720 "Forwarding altered outgoing ICMP");
721 else
722 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
723 "Forwarding altered incoming ICMP");
724}
725
726#ifdef CONFIG_IP_VS_IPV6
727void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
728 struct ip_vs_conn *cp, int inout)
729{
730 struct ipv6hdr *iph = ipv6_hdr(skb);
731 unsigned int icmp_offset = 0;
732 unsigned int offs = 0;
733 int protocol;
734 struct icmp6hdr *icmph;
735 struct ipv6hdr *ciph;
736 unsigned short fragoffs;
737
738 ipv6_find_hdr(skb, &icmp_offset, IPPROTO_ICMPV6, &fragoffs, NULL);
739 icmph = (struct icmp6hdr *)(skb_network_header(skb) + icmp_offset);
740 offs = icmp_offset + sizeof(struct icmp6hdr);
741 ciph = (struct ipv6hdr *)(skb_network_header(skb) + offs);
742
743 protocol = ipv6_find_hdr(skb, &offs, -1, &fragoffs, NULL);
744
745 if (inout) {
746 iph->saddr = cp->vaddr.in6;
747 ciph->daddr = cp->vaddr.in6;
748 } else {
749 iph->daddr = cp->daddr.in6;
750 ciph->saddr = cp->daddr.in6;
751 }
752
753
754 if (!fragoffs && (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
755 IPPROTO_SCTP == protocol)) {
756 __be16 *ports = (void *)(skb_network_header(skb) + offs);
757
758 IP_VS_DBG(11, "%s() changed port %d to %d\n", __func__,
759 ntohs(inout ? ports[1] : ports[0]),
760 ntohs(inout ? cp->vport : cp->dport));
761 if (inout)
762 ports[1] = cp->vport;
763 else
764 ports[0] = cp->dport;
765 }
766
767
768 icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr,
769 skb->len - icmp_offset,
770 IPPROTO_ICMPV6, 0);
771 skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset;
772 skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
773 skb->ip_summed = CHECKSUM_PARTIAL;
774
775 if (inout)
776 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
777 (void *)ciph - (void *)iph,
778 "Forwarding altered outgoing ICMPv6");
779 else
780 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
781 (void *)ciph - (void *)iph,
782 "Forwarding altered incoming ICMPv6");
783}
784#endif
785
786
787
788
789static int handle_response_icmp(int af, struct sk_buff *skb,
790 union nf_inet_addr *snet,
791 __u8 protocol, struct ip_vs_conn *cp,
792 struct ip_vs_protocol *pp,
793 unsigned int offset, unsigned int ihl)
794{
795 unsigned int verdict = NF_DROP;
796
797 if (IP_VS_FWD_METHOD(cp) != 0) {
798 pr_err("shouldn't reach here, because the box is on the "
799 "half connection in the tun/dr module.\n");
800 }
801
802
803 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
804
805 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
806 IP_VS_DBG_ADDR(af, snet));
807 goto out;
808 }
809
810 if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
811 IPPROTO_SCTP == protocol)
812 offset += 2 * sizeof(__u16);
813 if (!skb_make_writable(skb, offset))
814 goto out;
815
816#ifdef CONFIG_IP_VS_IPV6
817 if (af == AF_INET6)
818 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
819 else
820#endif
821 ip_vs_nat_icmp(skb, pp, cp, 1);
822
823 if (ip_vs_route_me_harder(af, skb))
824 goto out;
825
826
827 ip_vs_out_stats(cp, skb);
828
829 skb->ipvs_property = 1;
830 if (!(cp->flags & IP_VS_CONN_F_NFCT))
831 ip_vs_notrack(skb);
832 else
833 ip_vs_update_conntrack(skb, cp, 0);
834 verdict = NF_ACCEPT;
835
836out:
837 __ip_vs_conn_put(cp);
838
839 return verdict;
840}
841
842
843
844
845
846
847static int ip_vs_out_icmp(struct sk_buff *skb, int *related,
848 unsigned int hooknum)
849{
850 struct iphdr *iph;
851 struct icmphdr _icmph, *ic;
852 struct iphdr _ciph, *cih;
853 struct ip_vs_iphdr ciph;
854 struct ip_vs_conn *cp;
855 struct ip_vs_protocol *pp;
856 unsigned int offset, ihl;
857 union nf_inet_addr snet;
858
859 *related = 1;
860
861
862 if (ip_is_fragment(ip_hdr(skb))) {
863 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
864 return NF_STOLEN;
865 }
866
867 iph = ip_hdr(skb);
868 offset = ihl = iph->ihl * 4;
869 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
870 if (ic == NULL)
871 return NF_DROP;
872
873 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
874 ic->type, ntohs(icmp_id(ic)),
875 &iph->saddr, &iph->daddr);
876
877
878
879
880
881
882
883
884 if ((ic->type != ICMP_DEST_UNREACH) &&
885 (ic->type != ICMP_SOURCE_QUENCH) &&
886 (ic->type != ICMP_TIME_EXCEEDED)) {
887 *related = 0;
888 return NF_ACCEPT;
889 }
890
891
892 offset += sizeof(_icmph);
893 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
894 if (cih == NULL)
895 return NF_ACCEPT;
896
897 pp = ip_vs_proto_get(cih->protocol);
898 if (!pp)
899 return NF_ACCEPT;
900
901
902 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
903 pp->dont_defrag))
904 return NF_ACCEPT;
905
906 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
907 "Checking outgoing ICMP for");
908
909 ip_vs_fill_ip4hdr(cih, &ciph);
910 ciph.len += offset;
911
912 cp = pp->conn_out_get(AF_INET, skb, &ciph, 1);
913 if (!cp)
914 return NF_ACCEPT;
915
916 snet.ip = iph->saddr;
917 return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
918 pp, ciph.len, ihl);
919}
920
921#ifdef CONFIG_IP_VS_IPV6
922static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related,
923 unsigned int hooknum, struct ip_vs_iphdr *ipvsh)
924{
925 struct icmp6hdr _icmph, *ic;
926 struct ipv6hdr _ip6h, *ip6h;
927 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};
928 struct ip_vs_conn *cp;
929 struct ip_vs_protocol *pp;
930 union nf_inet_addr snet;
931 unsigned int writable;
932
933 *related = 1;
934 ic = frag_safe_skb_hp(skb, ipvsh->len, sizeof(_icmph), &_icmph, ipvsh);
935 if (ic == NULL)
936 return NF_DROP;
937
938
939
940
941
942
943
944
945 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
946 *related = 0;
947 return NF_ACCEPT;
948 }
949
950
951
952 if (ipvsh->flags & IP6_FH_F_FRAG)
953 return NF_DROP;
954
955 IP_VS_DBG(8, "Outgoing ICMPv6 (%d,%d) %pI6c->%pI6c\n",
956 ic->icmp6_type, ntohs(icmpv6_id(ic)),
957 &ipvsh->saddr, &ipvsh->daddr);
958
959
960 ciph.len = ipvsh->len + sizeof(_icmph);
961 ip6h = skb_header_pointer(skb, ciph.len, sizeof(_ip6h), &_ip6h);
962 if (ip6h == NULL)
963 return NF_ACCEPT;
964 ciph.saddr.in6 = ip6h->saddr;
965 ciph.daddr.in6 = ip6h->daddr;
966
967 ciph.protocol = ipv6_find_hdr(skb, &ciph.len, -1, &ciph.fragoffs, NULL);
968 if (ciph.protocol < 0)
969 return NF_ACCEPT;
970
971 pp = ip_vs_proto_get(ciph.protocol);
972 if (!pp)
973 return NF_ACCEPT;
974
975
976 cp = pp->conn_out_get(AF_INET6, skb, &ciph, 1);
977 if (!cp)
978 return NF_ACCEPT;
979
980 snet.in6 = ciph.saddr.in6;
981 writable = ciph.len;
982 return handle_response_icmp(AF_INET6, skb, &snet, ciph.protocol, cp,
983 pp, writable, sizeof(struct ipv6hdr));
984}
985#endif
986
987
988
989
990static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len)
991{
992 sctp_chunkhdr_t *sch, schunk;
993 sch = skb_header_pointer(skb, nh_len + sizeof(sctp_sctphdr_t),
994 sizeof(schunk), &schunk);
995 if (sch == NULL)
996 return 0;
997 if (sch->type == SCTP_CID_ABORT)
998 return 1;
999 return 0;
1000}
1001
1002static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
1003{
1004 struct tcphdr _tcph, *th;
1005
1006 th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
1007 if (th == NULL)
1008 return 0;
1009 return th->rst;
1010}
1011
1012static inline bool is_new_conn(const struct sk_buff *skb,
1013 struct ip_vs_iphdr *iph)
1014{
1015 switch (iph->protocol) {
1016 case IPPROTO_TCP: {
1017 struct tcphdr _tcph, *th;
1018
1019 th = skb_header_pointer(skb, iph->len, sizeof(_tcph), &_tcph);
1020 if (th == NULL)
1021 return false;
1022 return th->syn;
1023 }
1024 case IPPROTO_SCTP: {
1025 sctp_chunkhdr_t *sch, schunk;
1026
1027 sch = skb_header_pointer(skb, iph->len + sizeof(sctp_sctphdr_t),
1028 sizeof(schunk), &schunk);
1029 if (sch == NULL)
1030 return false;
1031 return sch->type == SCTP_CID_INIT;
1032 }
1033 default:
1034 return false;
1035 }
1036}
1037
1038
1039
1040static unsigned int
1041handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
1042 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph)
1043{
1044 struct ip_vs_protocol *pp = pd->pp;
1045
1046 IP_VS_DBG_PKT(11, af, pp, skb, 0, "Outgoing packet");
1047
1048 if (!skb_make_writable(skb, iph->len))
1049 goto drop;
1050
1051
1052 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp, iph))
1053 goto drop;
1054
1055#ifdef CONFIG_IP_VS_IPV6
1056 if (af == AF_INET6)
1057 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
1058 else
1059#endif
1060 {
1061 ip_hdr(skb)->saddr = cp->vaddr.ip;
1062 ip_send_check(ip_hdr(skb));
1063 }
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080 if (ip_vs_route_me_harder(af, skb))
1081 goto drop;
1082
1083 IP_VS_DBG_PKT(10, af, pp, skb, 0, "After SNAT");
1084
1085 ip_vs_out_stats(cp, skb);
1086 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd);
1087 skb->ipvs_property = 1;
1088 if (!(cp->flags & IP_VS_CONN_F_NFCT))
1089 ip_vs_notrack(skb);
1090 else
1091 ip_vs_update_conntrack(skb, cp, 0);
1092 ip_vs_conn_put(cp);
1093
1094 LeaveFunction(11);
1095 return NF_ACCEPT;
1096
1097drop:
1098 ip_vs_conn_put(cp);
1099 kfree_skb(skb);
1100 LeaveFunction(11);
1101 return NF_STOLEN;
1102}
1103
1104
1105
1106
1107static unsigned int
1108ip_vs_out(unsigned int hooknum, struct sk_buff *skb, int af)
1109{
1110 struct net *net = NULL;
1111 struct ip_vs_iphdr iph;
1112 struct ip_vs_protocol *pp;
1113 struct ip_vs_proto_data *pd;
1114 struct ip_vs_conn *cp;
1115
1116 EnterFunction(11);
1117
1118
1119 if (skb->ipvs_property)
1120 return NF_ACCEPT;
1121
1122
1123 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1124 af == AF_INET)) {
1125 struct sock *sk = skb->sk;
1126 struct inet_sock *inet = inet_sk(skb->sk);
1127
1128 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1129 return NF_ACCEPT;
1130 }
1131
1132 if (unlikely(!skb_dst(skb)))
1133 return NF_ACCEPT;
1134
1135 net = skb_net(skb);
1136 if (!net_ipvs(net)->enable)
1137 return NF_ACCEPT;
1138
1139 ip_vs_fill_iph_skb(af, skb, &iph);
1140#ifdef CONFIG_IP_VS_IPV6
1141 if (af == AF_INET6) {
1142 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1143 int related;
1144 int verdict = ip_vs_out_icmp_v6(skb, &related,
1145 hooknum, &iph);
1146
1147 if (related)
1148 return verdict;
1149 }
1150 } else
1151#endif
1152 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1153 int related;
1154 int verdict = ip_vs_out_icmp(skb, &related, hooknum);
1155
1156 if (related)
1157 return verdict;
1158 }
1159
1160 pd = ip_vs_proto_data_get(net, iph.protocol);
1161 if (unlikely(!pd))
1162 return NF_ACCEPT;
1163 pp = pd->pp;
1164
1165
1166#ifdef CONFIG_IP_VS_IPV6
1167 if (af == AF_INET)
1168#endif
1169 if (unlikely(ip_is_fragment(ip_hdr(skb)) && !pp->dont_defrag)) {
1170 if (ip_vs_gather_frags(skb,
1171 ip_vs_defrag_user(hooknum)))
1172 return NF_STOLEN;
1173
1174 ip_vs_fill_ip4hdr(skb_network_header(skb), &iph);
1175 }
1176
1177
1178
1179
1180 cp = pp->conn_out_get(af, skb, &iph, 0);
1181
1182 if (likely(cp))
1183 return handle_response(af, skb, pd, cp, &iph);
1184 if (sysctl_nat_icmp_send(net) &&
1185 (pp->protocol == IPPROTO_TCP ||
1186 pp->protocol == IPPROTO_UDP ||
1187 pp->protocol == IPPROTO_SCTP)) {
1188 __be16 _ports[2], *pptr;
1189
1190 pptr = frag_safe_skb_hp(skb, iph.len,
1191 sizeof(_ports), _ports, &iph);
1192 if (pptr == NULL)
1193 return NF_ACCEPT;
1194 if (ip_vs_has_real_service(net, af, iph.protocol, &iph.saddr,
1195 pptr[0])) {
1196
1197
1198
1199
1200
1201 if ((iph.protocol != IPPROTO_TCP &&
1202 iph.protocol != IPPROTO_SCTP)
1203 || ((iph.protocol == IPPROTO_TCP
1204 && !is_tcp_reset(skb, iph.len))
1205 || (iph.protocol == IPPROTO_SCTP
1206 && !is_sctp_abort(skb,
1207 iph.len)))) {
1208#ifdef CONFIG_IP_VS_IPV6
1209 if (af == AF_INET6) {
1210 if (!skb->dev)
1211 skb->dev = net->loopback_dev;
1212 icmpv6_send(skb,
1213 ICMPV6_DEST_UNREACH,
1214 ICMPV6_PORT_UNREACH,
1215 0);
1216 } else
1217#endif
1218 icmp_send(skb,
1219 ICMP_DEST_UNREACH,
1220 ICMP_PORT_UNREACH, 0);
1221 return NF_DROP;
1222 }
1223 }
1224 }
1225 IP_VS_DBG_PKT(12, af, pp, skb, 0,
1226 "ip_vs_out: packet continues traversal as normal");
1227 return NF_ACCEPT;
1228}
1229
1230
1231
1232
1233
1234
1235static unsigned int
1236ip_vs_reply4(const struct nf_hook_ops *ops, struct sk_buff *skb,
1237 const struct net_device *in, const struct net_device *out,
1238 int (*okfn)(struct sk_buff *))
1239{
1240 return ip_vs_out(ops->hooknum, skb, AF_INET);
1241}
1242
1243
1244
1245
1246
1247static unsigned int
1248ip_vs_local_reply4(const struct nf_hook_ops *ops, struct sk_buff *skb,
1249 const struct net_device *in, const struct net_device *out,
1250 int (*okfn)(struct sk_buff *))
1251{
1252 return ip_vs_out(ops->hooknum, skb, AF_INET);
1253}
1254
1255#ifdef CONFIG_IP_VS_IPV6
1256
1257
1258
1259
1260
1261
1262static unsigned int
1263ip_vs_reply6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1264 const struct net_device *in, const struct net_device *out,
1265 int (*okfn)(struct sk_buff *))
1266{
1267 return ip_vs_out(ops->hooknum, skb, AF_INET6);
1268}
1269
1270
1271
1272
1273
1274static unsigned int
1275ip_vs_local_reply6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1276 const struct net_device *in, const struct net_device *out,
1277 int (*okfn)(struct sk_buff *))
1278{
1279 return ip_vs_out(ops->hooknum, skb, AF_INET6);
1280}
1281
1282#endif
1283
1284
1285
1286
1287
1288
1289
1290static int
1291ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
1292{
1293 struct net *net = NULL;
1294 struct iphdr *iph;
1295 struct icmphdr _icmph, *ic;
1296 struct iphdr _ciph, *cih;
1297 struct ip_vs_iphdr ciph;
1298 struct ip_vs_conn *cp;
1299 struct ip_vs_protocol *pp;
1300 struct ip_vs_proto_data *pd;
1301 unsigned int offset, offset2, ihl, verdict;
1302 bool ipip;
1303
1304 *related = 1;
1305
1306
1307 if (ip_is_fragment(ip_hdr(skb))) {
1308 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
1309 return NF_STOLEN;
1310 }
1311
1312 iph = ip_hdr(skb);
1313 offset = ihl = iph->ihl * 4;
1314 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1315 if (ic == NULL)
1316 return NF_DROP;
1317
1318 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
1319 ic->type, ntohs(icmp_id(ic)),
1320 &iph->saddr, &iph->daddr);
1321
1322
1323
1324
1325
1326
1327
1328
1329 if ((ic->type != ICMP_DEST_UNREACH) &&
1330 (ic->type != ICMP_SOURCE_QUENCH) &&
1331 (ic->type != ICMP_TIME_EXCEEDED)) {
1332 *related = 0;
1333 return NF_ACCEPT;
1334 }
1335
1336
1337 offset += sizeof(_icmph);
1338 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1339 if (cih == NULL)
1340 return NF_ACCEPT;
1341
1342 net = skb_net(skb);
1343
1344
1345 ipip = false;
1346 if (cih->protocol == IPPROTO_IPIP) {
1347 if (unlikely(cih->frag_off & htons(IP_OFFSET)))
1348 return NF_ACCEPT;
1349
1350 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL))
1351 return NF_ACCEPT;
1352 offset += cih->ihl * 4;
1353 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1354 if (cih == NULL)
1355 return NF_ACCEPT;
1356 ipip = true;
1357 }
1358
1359 pd = ip_vs_proto_data_get(net, cih->protocol);
1360 if (!pd)
1361 return NF_ACCEPT;
1362 pp = pd->pp;
1363
1364
1365 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
1366 pp->dont_defrag))
1367 return NF_ACCEPT;
1368
1369 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
1370 "Checking incoming ICMP for");
1371
1372 offset2 = offset;
1373 ip_vs_fill_ip4hdr(cih, &ciph);
1374 ciph.len += offset;
1375 offset = ciph.len;
1376
1377
1378
1379 cp = pp->conn_in_get(AF_INET, skb, &ciph, ipip ? 0 : 1);
1380 if (!cp)
1381 return NF_ACCEPT;
1382
1383 verdict = NF_DROP;
1384
1385
1386 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
1387
1388 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1389 &iph->saddr);
1390 goto out;
1391 }
1392
1393 if (ipip) {
1394 __be32 info = ic->un.gateway;
1395 __u8 type = ic->type;
1396 __u8 code = ic->code;
1397
1398
1399 if (ic->type == ICMP_DEST_UNREACH &&
1400 ic->code == ICMP_FRAG_NEEDED) {
1401 struct ip_vs_dest *dest = cp->dest;
1402 u32 mtu = ntohs(ic->un.frag.mtu);
1403 __be16 frag_off = cih->frag_off;
1404
1405
1406 if (pskb_pull(skb, ihl + sizeof(_icmph)) == NULL)
1407 goto ignore_ipip;
1408 offset2 -= ihl + sizeof(_icmph);
1409 skb_reset_network_header(skb);
1410 IP_VS_DBG(12, "ICMP for IPIP %pI4->%pI4: mtu=%u\n",
1411 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr, mtu);
1412 ipv4_update_pmtu(skb, dev_net(skb->dev),
1413 mtu, 0, 0, 0, 0);
1414
1415 if (!(frag_off & htons(IP_DF)))
1416 goto ignore_ipip;
1417
1418 if (dest) {
1419 struct ip_vs_dest_dst *dest_dst;
1420
1421 rcu_read_lock();
1422 dest_dst = rcu_dereference(dest->dest_dst);
1423 if (dest_dst)
1424 mtu = dst_mtu(dest_dst->dst_cache);
1425 rcu_read_unlock();
1426 }
1427 if (mtu > 68 + sizeof(struct iphdr))
1428 mtu -= sizeof(struct iphdr);
1429 info = htonl(mtu);
1430 }
1431
1432
1433
1434 if (pskb_pull(skb, offset2) == NULL)
1435 goto ignore_ipip;
1436 skb_reset_network_header(skb);
1437 IP_VS_DBG(12, "Sending ICMP for %pI4->%pI4: t=%u, c=%u, i=%u\n",
1438 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr,
1439 type, code, ntohl(info));
1440 icmp_send(skb, type, code, info);
1441
1442 ip_vs_out_stats(cp, skb);
1443
1444ignore_ipip:
1445 consume_skb(skb);
1446 verdict = NF_STOLEN;
1447 goto out;
1448 }
1449
1450
1451 ip_vs_in_stats(cp, skb);
1452 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol ||
1453 IPPROTO_SCTP == cih->protocol)
1454 offset += 2 * sizeof(__u16);
1455 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset, hooknum, &ciph);
1456
1457out:
1458 __ip_vs_conn_put(cp);
1459
1460 return verdict;
1461}
1462
1463#ifdef CONFIG_IP_VS_IPV6
1464static int ip_vs_in_icmp_v6(struct sk_buff *skb, int *related,
1465 unsigned int hooknum, struct ip_vs_iphdr *iph)
1466{
1467 struct net *net = NULL;
1468 struct ipv6hdr _ip6h, *ip6h;
1469 struct icmp6hdr _icmph, *ic;
1470 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};
1471 struct ip_vs_conn *cp;
1472 struct ip_vs_protocol *pp;
1473 struct ip_vs_proto_data *pd;
1474 unsigned int offs_ciph, writable, verdict;
1475
1476 *related = 1;
1477
1478 ic = frag_safe_skb_hp(skb, iph->len, sizeof(_icmph), &_icmph, iph);
1479 if (ic == NULL)
1480 return NF_DROP;
1481
1482
1483
1484
1485
1486
1487
1488
1489 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
1490 *related = 0;
1491 return NF_ACCEPT;
1492 }
1493
1494
1495
1496 if (iph->flags & IP6_FH_F_FRAG)
1497 return NF_DROP;
1498
1499 IP_VS_DBG(8, "Incoming ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1500 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1501 &iph->saddr, &iph->daddr);
1502
1503
1504 ciph.len = iph->len + sizeof(_icmph);
1505 offs_ciph = ciph.len;
1506 ip6h = skb_header_pointer(skb, ciph.len, sizeof(_ip6h), &_ip6h);
1507 if (ip6h == NULL)
1508 return NF_ACCEPT;
1509 ciph.saddr.in6 = ip6h->saddr;
1510 ciph.daddr.in6 = ip6h->daddr;
1511
1512 ciph.protocol = ipv6_find_hdr(skb, &ciph.len, -1, &ciph.fragoffs, NULL);
1513 if (ciph.protocol < 0)
1514 return NF_ACCEPT;
1515
1516 net = skb_net(skb);
1517 pd = ip_vs_proto_data_get(net, ciph.protocol);
1518 if (!pd)
1519 return NF_ACCEPT;
1520 pp = pd->pp;
1521
1522
1523 if (ciph.fragoffs)
1524 return NF_ACCEPT;
1525
1526 IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offs_ciph,
1527 "Checking incoming ICMPv6 for");
1528
1529
1530
1531
1532 cp = pp->conn_in_get(AF_INET6, skb, &ciph,
1533 (hooknum == NF_INET_LOCAL_OUT) ? 0 : 1);
1534
1535 if (!cp)
1536 return NF_ACCEPT;
1537
1538 if ((hooknum == NF_INET_LOCAL_OUT) &&
1539 (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)) {
1540 __ip_vs_conn_put(cp);
1541 return NF_ACCEPT;
1542 }
1543
1544
1545 ip_vs_in_stats(cp, skb);
1546
1547
1548 writable = ciph.len;
1549 if (IPPROTO_TCP == ciph.protocol || IPPROTO_UDP == ciph.protocol ||
1550 IPPROTO_SCTP == ciph.protocol)
1551 writable += 2 * sizeof(__u16);
1552
1553 verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, writable, hooknum, &ciph);
1554
1555 __ip_vs_conn_put(cp);
1556
1557 return verdict;
1558}
1559#endif
1560
1561
1562
1563
1564
1565
1566static unsigned int
1567ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af)
1568{
1569 struct net *net;
1570 struct ip_vs_iphdr iph;
1571 struct ip_vs_protocol *pp;
1572 struct ip_vs_proto_data *pd;
1573 struct ip_vs_conn *cp;
1574 int ret, pkts;
1575 struct netns_ipvs *ipvs;
1576
1577
1578 if (skb->ipvs_property)
1579 return NF_ACCEPT;
1580
1581
1582
1583
1584
1585
1586 if (unlikely((skb->pkt_type != PACKET_HOST &&
1587 hooknum != NF_INET_LOCAL_OUT) ||
1588 !skb_dst(skb))) {
1589 ip_vs_fill_iph_skb(af, skb, &iph);
1590 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1591 " ignored in hook %u\n",
1592 skb->pkt_type, iph.protocol,
1593 IP_VS_DBG_ADDR(af, &iph.daddr), hooknum);
1594 return NF_ACCEPT;
1595 }
1596
1597 net = skb_net(skb);
1598 ipvs = net_ipvs(net);
1599 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
1600 return NF_ACCEPT;
1601
1602 ip_vs_fill_iph_skb(af, skb, &iph);
1603
1604
1605 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1606 af == AF_INET)) {
1607 struct sock *sk = skb->sk;
1608 struct inet_sock *inet = inet_sk(skb->sk);
1609
1610 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1611 return NF_ACCEPT;
1612 }
1613
1614#ifdef CONFIG_IP_VS_IPV6
1615 if (af == AF_INET6) {
1616 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1617 int related;
1618 int verdict = ip_vs_in_icmp_v6(skb, &related, hooknum,
1619 &iph);
1620
1621 if (related)
1622 return verdict;
1623 }
1624 } else
1625#endif
1626 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1627 int related;
1628 int verdict = ip_vs_in_icmp(skb, &related, hooknum);
1629
1630 if (related)
1631 return verdict;
1632 }
1633
1634
1635 pd = ip_vs_proto_data_get(net, iph.protocol);
1636 if (unlikely(!pd))
1637 return NF_ACCEPT;
1638 pp = pd->pp;
1639
1640
1641
1642 cp = pp->conn_in_get(af, skb, &iph, 0);
1643
1644 if (unlikely(sysctl_expire_nodest_conn(ipvs)) && cp && cp->dest &&
1645 unlikely(!atomic_read(&cp->dest->weight)) && !iph.fragoffs &&
1646 is_new_conn(skb, &iph)) {
1647 ip_vs_conn_expire_now(cp);
1648 __ip_vs_conn_put(cp);
1649 cp = NULL;
1650 }
1651
1652 if (unlikely(!cp) && !iph.fragoffs) {
1653
1654
1655
1656 int v;
1657
1658
1659 if (!pp->conn_schedule(af, skb, pd, &v, &cp, &iph))
1660 return v;
1661 }
1662
1663 if (unlikely(!cp)) {
1664
1665 IP_VS_DBG_PKT(12, af, pp, skb, 0,
1666 "ip_vs_in: packet continues traversal as normal");
1667 if (iph.fragoffs) {
1668
1669
1670
1671 IP_VS_DBG_RL("Unhandled frag, load nf_defrag_ipv6\n");
1672 IP_VS_DBG_PKT(7, af, pp, skb, 0, "unhandled fragment");
1673 }
1674 return NF_ACCEPT;
1675 }
1676
1677 IP_VS_DBG_PKT(11, af, pp, skb, 0, "Incoming packet");
1678
1679 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1680
1681
1682 if (sysctl_expire_nodest_conn(ipvs)) {
1683
1684 ip_vs_conn_expire_now(cp);
1685 }
1686
1687
1688 __ip_vs_conn_put(cp);
1689 return NF_DROP;
1690 }
1691
1692 ip_vs_in_stats(cp, skb);
1693 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
1694 if (cp->packet_xmit)
1695 ret = cp->packet_xmit(skb, cp, pp, &iph);
1696
1697 else {
1698 IP_VS_DBG_RL("warning: packet_xmit is null");
1699 ret = NF_ACCEPT;
1700 }
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
1712 pkts = sysctl_sync_threshold(ipvs);
1713 else
1714 pkts = atomic_add_return(1, &cp->in_pkts);
1715
1716 if (ipvs->sync_state & IP_VS_STATE_MASTER)
1717 ip_vs_sync_conn(net, cp, pkts);
1718
1719 ip_vs_conn_put(cp);
1720 return ret;
1721}
1722
1723
1724
1725
1726
1727static unsigned int
1728ip_vs_remote_request4(const struct nf_hook_ops *ops, struct sk_buff *skb,
1729 const struct net_device *in,
1730 const struct net_device *out,
1731 int (*okfn)(struct sk_buff *))
1732{
1733 return ip_vs_in(ops->hooknum, skb, AF_INET);
1734}
1735
1736
1737
1738
1739
1740static unsigned int
1741ip_vs_local_request4(const struct nf_hook_ops *ops, struct sk_buff *skb,
1742 const struct net_device *in, const struct net_device *out,
1743 int (*okfn)(struct sk_buff *))
1744{
1745 return ip_vs_in(ops->hooknum, skb, AF_INET);
1746}
1747
1748#ifdef CONFIG_IP_VS_IPV6
1749
1750
1751
1752
1753
1754static unsigned int
1755ip_vs_remote_request6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1756 const struct net_device *in,
1757 const struct net_device *out,
1758 int (*okfn)(struct sk_buff *))
1759{
1760 return ip_vs_in(ops->hooknum, skb, AF_INET6);
1761}
1762
1763
1764
1765
1766
1767static unsigned int
1768ip_vs_local_request6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1769 const struct net_device *in, const struct net_device *out,
1770 int (*okfn)(struct sk_buff *))
1771{
1772 return ip_vs_in(ops->hooknum, skb, AF_INET6);
1773}
1774
1775#endif
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787static unsigned int
1788ip_vs_forward_icmp(const struct nf_hook_ops *ops, struct sk_buff *skb,
1789 const struct net_device *in, const struct net_device *out,
1790 int (*okfn)(struct sk_buff *))
1791{
1792 int r;
1793 struct net *net;
1794 struct netns_ipvs *ipvs;
1795
1796 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
1797 return NF_ACCEPT;
1798
1799
1800 net = skb_net(skb);
1801 ipvs = net_ipvs(net);
1802 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
1803 return NF_ACCEPT;
1804
1805 return ip_vs_in_icmp(skb, &r, ops->hooknum);
1806}
1807
1808#ifdef CONFIG_IP_VS_IPV6
1809static unsigned int
1810ip_vs_forward_icmp_v6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1811 const struct net_device *in, const struct net_device *out,
1812 int (*okfn)(struct sk_buff *))
1813{
1814 int r;
1815 struct net *net;
1816 struct netns_ipvs *ipvs;
1817 struct ip_vs_iphdr iphdr;
1818
1819 ip_vs_fill_iph_skb(AF_INET6, skb, &iphdr);
1820 if (iphdr.protocol != IPPROTO_ICMPV6)
1821 return NF_ACCEPT;
1822
1823
1824 net = skb_net(skb);
1825 ipvs = net_ipvs(net);
1826 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
1827 return NF_ACCEPT;
1828
1829 return ip_vs_in_icmp_v6(skb, &r, ops->hooknum, &iphdr);
1830}
1831#endif
1832
1833
1834static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
1835
1836 {
1837 .hook = ip_vs_reply4,
1838 .owner = THIS_MODULE,
1839 .pf = NFPROTO_IPV4,
1840 .hooknum = NF_INET_LOCAL_IN,
1841 .priority = NF_IP_PRI_NAT_SRC - 2,
1842 },
1843
1844
1845
1846 {
1847 .hook = ip_vs_remote_request4,
1848 .owner = THIS_MODULE,
1849 .pf = NFPROTO_IPV4,
1850 .hooknum = NF_INET_LOCAL_IN,
1851 .priority = NF_IP_PRI_NAT_SRC - 1,
1852 },
1853
1854 {
1855 .hook = ip_vs_local_reply4,
1856 .owner = THIS_MODULE,
1857 .pf = NFPROTO_IPV4,
1858 .hooknum = NF_INET_LOCAL_OUT,
1859 .priority = NF_IP_PRI_NAT_DST + 1,
1860 },
1861
1862 {
1863 .hook = ip_vs_local_request4,
1864 .owner = THIS_MODULE,
1865 .pf = NFPROTO_IPV4,
1866 .hooknum = NF_INET_LOCAL_OUT,
1867 .priority = NF_IP_PRI_NAT_DST + 2,
1868 },
1869
1870
1871 {
1872 .hook = ip_vs_forward_icmp,
1873 .owner = THIS_MODULE,
1874 .pf = NFPROTO_IPV4,
1875 .hooknum = NF_INET_FORWARD,
1876 .priority = 99,
1877 },
1878
1879 {
1880 .hook = ip_vs_reply4,
1881 .owner = THIS_MODULE,
1882 .pf = NFPROTO_IPV4,
1883 .hooknum = NF_INET_FORWARD,
1884 .priority = 100,
1885 },
1886#ifdef CONFIG_IP_VS_IPV6
1887
1888 {
1889 .hook = ip_vs_reply6,
1890 .owner = THIS_MODULE,
1891 .pf = NFPROTO_IPV6,
1892 .hooknum = NF_INET_LOCAL_IN,
1893 .priority = NF_IP6_PRI_NAT_SRC - 2,
1894 },
1895
1896
1897
1898 {
1899 .hook = ip_vs_remote_request6,
1900 .owner = THIS_MODULE,
1901 .pf = NFPROTO_IPV6,
1902 .hooknum = NF_INET_LOCAL_IN,
1903 .priority = NF_IP6_PRI_NAT_SRC - 1,
1904 },
1905
1906 {
1907 .hook = ip_vs_local_reply6,
1908 .owner = THIS_MODULE,
1909 .pf = NFPROTO_IPV4,
1910 .hooknum = NF_INET_LOCAL_OUT,
1911 .priority = NF_IP6_PRI_NAT_DST + 1,
1912 },
1913
1914 {
1915 .hook = ip_vs_local_request6,
1916 .owner = THIS_MODULE,
1917 .pf = NFPROTO_IPV6,
1918 .hooknum = NF_INET_LOCAL_OUT,
1919 .priority = NF_IP6_PRI_NAT_DST + 2,
1920 },
1921
1922
1923 {
1924 .hook = ip_vs_forward_icmp_v6,
1925 .owner = THIS_MODULE,
1926 .pf = NFPROTO_IPV6,
1927 .hooknum = NF_INET_FORWARD,
1928 .priority = 99,
1929 },
1930
1931 {
1932 .hook = ip_vs_reply6,
1933 .owner = THIS_MODULE,
1934 .pf = NFPROTO_IPV6,
1935 .hooknum = NF_INET_FORWARD,
1936 .priority = 100,
1937 },
1938#endif
1939};
1940
1941
1942
1943static int __net_init __ip_vs_init(struct net *net)
1944{
1945 struct netns_ipvs *ipvs;
1946
1947 ipvs = net_generic(net, ip_vs_net_id);
1948 if (ipvs == NULL)
1949 return -ENOMEM;
1950
1951
1952 ipvs->enable = 0;
1953 ipvs->net = net;
1954
1955 ipvs->gen = atomic_read(&ipvs_netns_cnt);
1956 atomic_inc(&ipvs_netns_cnt);
1957 net->ipvs = ipvs;
1958
1959 if (ip_vs_estimator_net_init(net) < 0)
1960 goto estimator_fail;
1961
1962 if (ip_vs_control_net_init(net) < 0)
1963 goto control_fail;
1964
1965 if (ip_vs_protocol_net_init(net) < 0)
1966 goto protocol_fail;
1967
1968 if (ip_vs_app_net_init(net) < 0)
1969 goto app_fail;
1970
1971 if (ip_vs_conn_net_init(net) < 0)
1972 goto conn_fail;
1973
1974 if (ip_vs_sync_net_init(net) < 0)
1975 goto sync_fail;
1976
1977 printk(KERN_INFO "IPVS: Creating netns size=%zu id=%d\n",
1978 sizeof(struct netns_ipvs), ipvs->gen);
1979 return 0;
1980
1981
1982
1983
1984sync_fail:
1985 ip_vs_conn_net_cleanup(net);
1986conn_fail:
1987 ip_vs_app_net_cleanup(net);
1988app_fail:
1989 ip_vs_protocol_net_cleanup(net);
1990protocol_fail:
1991 ip_vs_control_net_cleanup(net);
1992control_fail:
1993 ip_vs_estimator_net_cleanup(net);
1994estimator_fail:
1995 net->ipvs = NULL;
1996 return -ENOMEM;
1997}
1998
1999static void __net_exit __ip_vs_cleanup(struct net *net)
2000{
2001 ip_vs_service_net_cleanup(net);
2002 ip_vs_conn_net_cleanup(net);
2003 ip_vs_app_net_cleanup(net);
2004 ip_vs_protocol_net_cleanup(net);
2005 ip_vs_control_net_cleanup(net);
2006 ip_vs_estimator_net_cleanup(net);
2007 IP_VS_DBG(2, "ipvs netns %d released\n", net_ipvs(net)->gen);
2008 net->ipvs = NULL;
2009}
2010
2011static void __net_exit __ip_vs_dev_cleanup(struct net *net)
2012{
2013 EnterFunction(2);
2014 net_ipvs(net)->enable = 0;
2015 smp_wmb();
2016 ip_vs_sync_net_cleanup(net);
2017 LeaveFunction(2);
2018}
2019
2020static struct pernet_operations ipvs_core_ops = {
2021 .init = __ip_vs_init,
2022 .exit = __ip_vs_cleanup,
2023 .id = &ip_vs_net_id,
2024 .size = sizeof(struct netns_ipvs),
2025};
2026
2027static struct pernet_operations ipvs_core_dev_ops = {
2028 .exit = __ip_vs_dev_cleanup,
2029};
2030
2031
2032
2033
2034static int __init ip_vs_init(void)
2035{
2036 int ret;
2037
2038 ret = ip_vs_control_init();
2039 if (ret < 0) {
2040 pr_err("can't setup control.\n");
2041 goto exit;
2042 }
2043
2044 ip_vs_protocol_init();
2045
2046 ret = ip_vs_conn_init();
2047 if (ret < 0) {
2048 pr_err("can't setup connection table.\n");
2049 goto cleanup_protocol;
2050 }
2051
2052 ret = register_pernet_subsys(&ipvs_core_ops);
2053 if (ret < 0)
2054 goto cleanup_conn;
2055
2056 ret = register_pernet_device(&ipvs_core_dev_ops);
2057 if (ret < 0)
2058 goto cleanup_sub;
2059
2060 ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2061 if (ret < 0) {
2062 pr_err("can't register hooks.\n");
2063 goto cleanup_dev;
2064 }
2065
2066 ret = ip_vs_register_nl_ioctl();
2067 if (ret < 0) {
2068 pr_err("can't register netlink/ioctl.\n");
2069 goto cleanup_hooks;
2070 }
2071
2072 pr_info("ipvs loaded.\n");
2073
2074 return ret;
2075
2076cleanup_hooks:
2077 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2078cleanup_dev:
2079 unregister_pernet_device(&ipvs_core_dev_ops);
2080cleanup_sub:
2081 unregister_pernet_subsys(&ipvs_core_ops);
2082cleanup_conn:
2083 ip_vs_conn_cleanup();
2084cleanup_protocol:
2085 ip_vs_protocol_cleanup();
2086 ip_vs_control_cleanup();
2087exit:
2088 return ret;
2089}
2090
2091static void __exit ip_vs_cleanup(void)
2092{
2093 ip_vs_unregister_nl_ioctl();
2094 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2095 unregister_pernet_device(&ipvs_core_dev_ops);
2096 unregister_pernet_subsys(&ipvs_core_ops);
2097 ip_vs_conn_cleanup();
2098 ip_vs_protocol_cleanup();
2099 ip_vs_control_cleanup();
2100 pr_info("ipvs unloaded.\n");
2101}
2102
2103module_init(ip_vs_init);
2104module_exit(ip_vs_cleanup);
2105MODULE_LICENSE("GPL");
2106