PageRenderTime 78ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/net/netfilter/ipvs/ip_vs_core.c

http://github.com/mirrors/linux
C | 2534 lines | 1761 code | 346 blank | 427 comment | 415 complexity | 74680fb4a34037458ed51e88a81ee483 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * IPVS An implementation of the IP virtual server support for the
  4. * LINUX operating system. IPVS is now implemented as a module
  5. * over the Netfilter framework. IPVS can be used to build a
  6. * high-performance and highly available server based on a
  7. * cluster of servers.
  8. *
  9. * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
  10. * Peter Kese <peter.kese@ijs.si>
  11. * Julian Anastasov <ja@ssi.bg>
  12. *
  13. * The IPVS code for kernel 2.2 was done by Wensong Zhang and Peter Kese,
  14. * with changes/fixes from Julian Anastasov, Lars Marowsky-Bree, Horms
  15. * and others.
  16. *
  17. * Changes:
  18. * Paul `Rusty' Russell properly handle non-linear skbs
  19. * Harald Welte don't use nfcache
  20. */
  21. #define KMSG_COMPONENT "IPVS"
  22. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/ip.h>
  26. #include <linux/tcp.h>
  27. #include <linux/sctp.h>
  28. #include <linux/icmp.h>
  29. #include <linux/slab.h>
  30. #include <net/ip.h>
  31. #include <net/tcp.h>
  32. #include <net/udp.h>
  33. #include <net/icmp.h> /* for icmp_send */
  34. #include <net/gue.h>
  35. #include <net/gre.h>
  36. #include <net/route.h>
  37. #include <net/ip6_checksum.h>
  38. #include <net/netns/generic.h> /* net_generic() */
  39. #include <linux/netfilter.h>
  40. #include <linux/netfilter_ipv4.h>
  41. #ifdef CONFIG_IP_VS_IPV6
  42. #include <net/ipv6.h>
  43. #include <linux/netfilter_ipv6.h>
  44. #include <net/ip6_route.h>
  45. #endif
  46. #include <net/ip_vs.h>
  47. #include <linux/indirect_call_wrapper.h>
  48. EXPORT_SYMBOL(register_ip_vs_scheduler);
  49. EXPORT_SYMBOL(unregister_ip_vs_scheduler);
  50. EXPORT_SYMBOL(ip_vs_proto_name);
  51. EXPORT_SYMBOL(ip_vs_conn_new);
  52. EXPORT_SYMBOL(ip_vs_conn_in_get);
  53. EXPORT_SYMBOL(ip_vs_conn_out_get);
  54. #ifdef CONFIG_IP_VS_PROTO_TCP
  55. EXPORT_SYMBOL(ip_vs_tcp_conn_listen);
  56. #endif
  57. EXPORT_SYMBOL(ip_vs_conn_put);
  58. #ifdef CONFIG_IP_VS_DEBUG
  59. EXPORT_SYMBOL(ip_vs_get_debug_level);
  60. #endif
  61. EXPORT_SYMBOL(ip_vs_new_conn_out);
  62. #ifdef CONFIG_IP_VS_PROTO_TCP
  63. INDIRECT_CALLABLE_DECLARE(int
  64. tcp_snat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp,
  65. struct ip_vs_conn *cp, struct ip_vs_iphdr *iph));
  66. #endif
  67. #ifdef CONFIG_IP_VS_PROTO_UDP
  68. INDIRECT_CALLABLE_DECLARE(int
  69. udp_snat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp,
  70. struct ip_vs_conn *cp, struct ip_vs_iphdr *iph));
  71. #endif
  72. #if defined(CONFIG_IP_VS_PROTO_TCP) && defined(CONFIG_IP_VS_PROTO_UDP)
  73. #define SNAT_CALL(f, ...) \
  74. INDIRECT_CALL_2(f, tcp_snat_handler, udp_snat_handler, __VA_ARGS__)
  75. #elif defined(CONFIG_IP_VS_PROTO_TCP)
  76. #define SNAT_CALL(f, ...) INDIRECT_CALL_1(f, tcp_snat_handler, __VA_ARGS__)
  77. #elif defined(CONFIG_IP_VS_PROTO_UDP)
  78. #define SNAT_CALL(f, ...) INDIRECT_CALL_1(f, udp_snat_handler, __VA_ARGS__)
  79. #else
  80. #define SNAT_CALL(f, ...) f(__VA_ARGS__)
  81. #endif
  82. static unsigned int ip_vs_net_id __read_mostly;
  83. /* netns cnt used for uniqueness */
  84. static atomic_t ipvs_netns_cnt = ATOMIC_INIT(0);
  85. /* ID used in ICMP lookups */
  86. #define icmp_id(icmph) (((icmph)->un).echo.id)
  87. #define icmpv6_id(icmph) (icmph->icmp6_dataun.u_echo.identifier)
  88. const char *ip_vs_proto_name(unsigned int proto)
  89. {
  90. static char buf[20];
  91. switch (proto) {
  92. case IPPROTO_IP:
  93. return "IP";
  94. case IPPROTO_UDP:
  95. return "UDP";
  96. case IPPROTO_TCP:
  97. return "TCP";
  98. case IPPROTO_SCTP:
  99. return "SCTP";
  100. case IPPROTO_ICMP:
  101. return "ICMP";
  102. #ifdef CONFIG_IP_VS_IPV6
  103. case IPPROTO_ICMPV6:
  104. return "ICMPv6";
  105. #endif
  106. default:
  107. sprintf(buf, "IP_%u", proto);
  108. return buf;
  109. }
  110. }
  111. void ip_vs_init_hash_table(struct list_head *table, int rows)
  112. {
  113. while (--rows >= 0)
  114. INIT_LIST_HEAD(&table[rows]);
  115. }
  116. static inline void
  117. ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
  118. {
  119. struct ip_vs_dest *dest = cp->dest;
  120. struct netns_ipvs *ipvs = cp->ipvs;
  121. if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
  122. struct ip_vs_cpu_stats *s;
  123. struct ip_vs_service *svc;
  124. local_bh_disable();
  125. s = this_cpu_ptr(dest->stats.cpustats);
  126. u64_stats_update_begin(&s->syncp);
  127. s->cnt.inpkts++;
  128. s->cnt.inbytes += skb->len;
  129. u64_stats_update_end(&s->syncp);
  130. svc = rcu_dereference(dest->svc);
  131. s = this_cpu_ptr(svc->stats.cpustats);
  132. u64_stats_update_begin(&s->syncp);
  133. s->cnt.inpkts++;
  134. s->cnt.inbytes += skb->len;
  135. u64_stats_update_end(&s->syncp);
  136. s = this_cpu_ptr(ipvs->tot_stats.cpustats);
  137. u64_stats_update_begin(&s->syncp);
  138. s->cnt.inpkts++;
  139. s->cnt.inbytes += skb->len;
  140. u64_stats_update_end(&s->syncp);
  141. local_bh_enable();
  142. }
  143. }
  144. static inline void
  145. ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
  146. {
  147. struct ip_vs_dest *dest = cp->dest;
  148. struct netns_ipvs *ipvs = cp->ipvs;
  149. if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
  150. struct ip_vs_cpu_stats *s;
  151. struct ip_vs_service *svc;
  152. local_bh_disable();
  153. s = this_cpu_ptr(dest->stats.cpustats);
  154. u64_stats_update_begin(&s->syncp);
  155. s->cnt.outpkts++;
  156. s->cnt.outbytes += skb->len;
  157. u64_stats_update_end(&s->syncp);
  158. svc = rcu_dereference(dest->svc);
  159. s = this_cpu_ptr(svc->stats.cpustats);
  160. u64_stats_update_begin(&s->syncp);
  161. s->cnt.outpkts++;
  162. s->cnt.outbytes += skb->len;
  163. u64_stats_update_end(&s->syncp);
  164. s = this_cpu_ptr(ipvs->tot_stats.cpustats);
  165. u64_stats_update_begin(&s->syncp);
  166. s->cnt.outpkts++;
  167. s->cnt.outbytes += skb->len;
  168. u64_stats_update_end(&s->syncp);
  169. local_bh_enable();
  170. }
  171. }
  172. static inline void
  173. ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
  174. {
  175. struct netns_ipvs *ipvs = svc->ipvs;
  176. struct ip_vs_cpu_stats *s;
  177. local_bh_disable();
  178. s = this_cpu_ptr(cp->dest->stats.cpustats);
  179. u64_stats_update_begin(&s->syncp);
  180. s->cnt.conns++;
  181. u64_stats_update_end(&s->syncp);
  182. s = this_cpu_ptr(svc->stats.cpustats);
  183. u64_stats_update_begin(&s->syncp);
  184. s->cnt.conns++;
  185. u64_stats_update_end(&s->syncp);
  186. s = this_cpu_ptr(ipvs->tot_stats.cpustats);
  187. u64_stats_update_begin(&s->syncp);
  188. s->cnt.conns++;
  189. u64_stats_update_end(&s->syncp);
  190. local_bh_enable();
  191. }
  192. static inline void
  193. ip_vs_set_state(struct ip_vs_conn *cp, int direction,
  194. const struct sk_buff *skb,
  195. struct ip_vs_proto_data *pd)
  196. {
  197. if (likely(pd->pp->state_transition))
  198. pd->pp->state_transition(cp, direction, skb, pd);
  199. }
  200. static inline int
  201. ip_vs_conn_fill_param_persist(const struct ip_vs_service *svc,
  202. struct sk_buff *skb, int protocol,
  203. const union nf_inet_addr *caddr, __be16 cport,
  204. const union nf_inet_addr *vaddr, __be16 vport,
  205. struct ip_vs_conn_param *p)
  206. {
  207. ip_vs_conn_fill_param(svc->ipvs, svc->af, protocol, caddr, cport, vaddr,
  208. vport, p);
  209. p->pe = rcu_dereference(svc->pe);
  210. if (p->pe && p->pe->fill_param)
  211. return p->pe->fill_param(p, skb);
  212. return 0;
  213. }
  214. /*
  215. * IPVS persistent scheduling function
  216. * It creates a connection entry according to its template if exists,
  217. * or selects a server and creates a connection entry plus a template.
  218. * Locking: we are svc user (svc->refcnt), so we hold all dests too
  219. * Protocols supported: TCP, UDP
  220. */
  221. static struct ip_vs_conn *
  222. ip_vs_sched_persist(struct ip_vs_service *svc,
  223. struct sk_buff *skb, __be16 src_port, __be16 dst_port,
  224. int *ignored, struct ip_vs_iphdr *iph)
  225. {
  226. struct ip_vs_conn *cp = NULL;
  227. struct ip_vs_dest *dest;
  228. struct ip_vs_conn *ct;
  229. __be16 dport = 0; /* destination port to forward */
  230. unsigned int flags;
  231. struct ip_vs_conn_param param;
  232. const union nf_inet_addr fwmark = { .ip = htonl(svc->fwmark) };
  233. union nf_inet_addr snet; /* source network of the client,
  234. after masking */
  235. const union nf_inet_addr *src_addr, *dst_addr;
  236. if (likely(!ip_vs_iph_inverse(iph))) {
  237. src_addr = &iph->saddr;
  238. dst_addr = &iph->daddr;
  239. } else {
  240. src_addr = &iph->daddr;
  241. dst_addr = &iph->saddr;
  242. }
  243. /* Mask saddr with the netmask to adjust template granularity */
  244. #ifdef CONFIG_IP_VS_IPV6
  245. if (svc->af == AF_INET6)
  246. ipv6_addr_prefix(&snet.in6, &src_addr->in6,
  247. (__force __u32) svc->netmask);
  248. else
  249. #endif
  250. snet.ip = src_addr->ip & svc->netmask;
  251. IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
  252. "mnet %s\n",
  253. IP_VS_DBG_ADDR(svc->af, src_addr), ntohs(src_port),
  254. IP_VS_DBG_ADDR(svc->af, dst_addr), ntohs(dst_port),
  255. IP_VS_DBG_ADDR(svc->af, &snet));
  256. /*
  257. * As far as we know, FTP is a very complicated network protocol, and
  258. * it uses control connection and data connections. For active FTP,
  259. * FTP server initialize data connection to the client, its source port
  260. * is often 20. For passive FTP, FTP server tells the clients the port
  261. * that it passively listens to, and the client issues the data
  262. * connection. In the tunneling or direct routing mode, the load
  263. * balancer is on the client-to-server half of connection, the port
  264. * number is unknown to the load balancer. So, a conn template like
  265. * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
  266. * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
  267. * is created for other persistent services.
  268. */
  269. {
  270. int protocol = iph->protocol;
  271. const union nf_inet_addr *vaddr = dst_addr;
  272. __be16 vport = 0;
  273. if (dst_port == svc->port) {
  274. /* non-FTP template:
  275. * <protocol, caddr, 0, vaddr, vport, daddr, dport>
  276. * FTP template:
  277. * <protocol, caddr, 0, vaddr, 0, daddr, 0>
  278. */
  279. if (svc->port != FTPPORT)
  280. vport = dst_port;
  281. } else {
  282. /* Note: persistent fwmark-based services and
  283. * persistent port zero service are handled here.
  284. * fwmark template:
  285. * <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
  286. * port zero template:
  287. * <protocol,caddr,0,vaddr,0,daddr,0>
  288. */
  289. if (svc->fwmark) {
  290. protocol = IPPROTO_IP;
  291. vaddr = &fwmark;
  292. }
  293. }
  294. /* return *ignored = -1 so NF_DROP can be used */
  295. if (ip_vs_conn_fill_param_persist(svc, skb, protocol, &snet, 0,
  296. vaddr, vport, &param) < 0) {
  297. *ignored = -1;
  298. return NULL;
  299. }
  300. }
  301. /* Check if a template already exists */
  302. ct = ip_vs_ct_in_get(&param);
  303. if (!ct || !ip_vs_check_template(ct, NULL)) {
  304. struct ip_vs_scheduler *sched;
  305. /*
  306. * No template found or the dest of the connection
  307. * template is not available.
  308. * return *ignored=0 i.e. ICMP and NF_DROP
  309. */
  310. sched = rcu_dereference(svc->scheduler);
  311. if (sched) {
  312. /* read svc->sched_data after svc->scheduler */
  313. smp_rmb();
  314. dest = sched->schedule(svc, skb, iph);
  315. } else {
  316. dest = NULL;
  317. }
  318. if (!dest) {
  319. IP_VS_DBG(1, "p-schedule: no dest found.\n");
  320. kfree(param.pe_data);
  321. *ignored = 0;
  322. return NULL;
  323. }
  324. if (dst_port == svc->port && svc->port != FTPPORT)
  325. dport = dest->port;
  326. /* Create a template
  327. * This adds param.pe_data to the template,
  328. * and thus param.pe_data will be destroyed
  329. * when the template expires */
  330. ct = ip_vs_conn_new(&param, dest->af, &dest->addr, dport,
  331. IP_VS_CONN_F_TEMPLATE, dest, skb->mark);
  332. if (ct == NULL) {
  333. kfree(param.pe_data);
  334. *ignored = -1;
  335. return NULL;
  336. }
  337. ct->timeout = svc->timeout;
  338. } else {
  339. /* set destination with the found template */
  340. dest = ct->dest;
  341. kfree(param.pe_data);
  342. }
  343. dport = dst_port;
  344. if (dport == svc->port && dest->port)
  345. dport = dest->port;
  346. flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
  347. && iph->protocol == IPPROTO_UDP) ?
  348. IP_VS_CONN_F_ONE_PACKET : 0;
  349. /*
  350. * Create a new connection according to the template
  351. */
  352. ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol, src_addr,
  353. src_port, dst_addr, dst_port, &param);
  354. cp = ip_vs_conn_new(&param, dest->af, &dest->addr, dport, flags, dest,
  355. skb->mark);
  356. if (cp == NULL) {
  357. ip_vs_conn_put(ct);
  358. *ignored = -1;
  359. return NULL;
  360. }
  361. /*
  362. * Add its control
  363. */
  364. ip_vs_control_add(cp, ct);
  365. ip_vs_conn_put(ct);
  366. ip_vs_conn_stats(cp, svc);
  367. return cp;
  368. }
  369. /*
  370. * IPVS main scheduling function
  371. * It selects a server according to the virtual service, and
  372. * creates a connection entry.
  373. * Protocols supported: TCP, UDP
  374. *
  375. * Usage of *ignored
  376. *
  377. * 1 : protocol tried to schedule (eg. on SYN), found svc but the
  378. * svc/scheduler decides that this packet should be accepted with
  379. * NF_ACCEPT because it must not be scheduled.
  380. *
  381. * 0 : scheduler can not find destination, so try bypass or
  382. * return ICMP and then NF_DROP (ip_vs_leave).
  383. *
  384. * -1 : scheduler tried to schedule but fatal error occurred, eg.
  385. * ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param
  386. * failure such as missing Call-ID, ENOMEM on skb_linearize
  387. * or pe_data. In this case we should return NF_DROP without
  388. * any attempts to send ICMP with ip_vs_leave.
  389. */
  390. struct ip_vs_conn *
  391. ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
  392. struct ip_vs_proto_data *pd, int *ignored,
  393. struct ip_vs_iphdr *iph)
  394. {
  395. struct ip_vs_protocol *pp = pd->pp;
  396. struct ip_vs_conn *cp = NULL;
  397. struct ip_vs_scheduler *sched;
  398. struct ip_vs_dest *dest;
  399. __be16 _ports[2], *pptr, cport, vport;
  400. const void *caddr, *vaddr;
  401. unsigned int flags;
  402. *ignored = 1;
  403. /*
  404. * IPv6 frags, only the first hit here.
  405. */
  406. pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports);
  407. if (pptr == NULL)
  408. return NULL;
  409. if (likely(!ip_vs_iph_inverse(iph))) {
  410. cport = pptr[0];
  411. caddr = &iph->saddr;
  412. vport = pptr[1];
  413. vaddr = &iph->daddr;
  414. } else {
  415. cport = pptr[1];
  416. caddr = &iph->daddr;
  417. vport = pptr[0];
  418. vaddr = &iph->saddr;
  419. }
  420. /*
  421. * FTPDATA needs this check when using local real server.
  422. * Never schedule Active FTPDATA connections from real server.
  423. * For LVS-NAT they must be already created. For other methods
  424. * with persistence the connection is created on SYN+ACK.
  425. */
  426. if (cport == FTPDATA) {
  427. IP_VS_DBG_PKT(12, svc->af, pp, skb, iph->off,
  428. "Not scheduling FTPDATA");
  429. return NULL;
  430. }
  431. /*
  432. * Do not schedule replies from local real server.
  433. */
  434. if ((!skb->dev || skb->dev->flags & IFF_LOOPBACK)) {
  435. iph->hdr_flags ^= IP_VS_HDR_INVERSE;
  436. cp = INDIRECT_CALL_1(pp->conn_in_get,
  437. ip_vs_conn_in_get_proto, svc->ipvs,
  438. svc->af, skb, iph);
  439. iph->hdr_flags ^= IP_VS_HDR_INVERSE;
  440. if (cp) {
  441. IP_VS_DBG_PKT(12, svc->af, pp, skb, iph->off,
  442. "Not scheduling reply for existing"
  443. " connection");
  444. __ip_vs_conn_put(cp);
  445. return NULL;
  446. }
  447. }
  448. /*
  449. * Persistent service
  450. */
  451. if (svc->flags & IP_VS_SVC_F_PERSISTENT)
  452. return ip_vs_sched_persist(svc, skb, cport, vport, ignored,
  453. iph);
  454. *ignored = 0;
  455. /*
  456. * Non-persistent service
  457. */
  458. if (!svc->fwmark && vport != svc->port) {
  459. if (!svc->port)
  460. pr_err("Schedule: port zero only supported "
  461. "in persistent services, "
  462. "check your ipvs configuration\n");
  463. return NULL;
  464. }
  465. sched = rcu_dereference(svc->scheduler);
  466. if (sched) {
  467. /* read svc->sched_data after svc->scheduler */
  468. smp_rmb();
  469. dest = sched->schedule(svc, skb, iph);
  470. } else {
  471. dest = NULL;
  472. }
  473. if (dest == NULL) {
  474. IP_VS_DBG(1, "Schedule: no dest found.\n");
  475. return NULL;
  476. }
  477. flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
  478. && iph->protocol == IPPROTO_UDP) ?
  479. IP_VS_CONN_F_ONE_PACKET : 0;
  480. /*
  481. * Create a connection entry.
  482. */
  483. {
  484. struct ip_vs_conn_param p;
  485. ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol,
  486. caddr, cport, vaddr, vport, &p);
  487. cp = ip_vs_conn_new(&p, dest->af, &dest->addr,
  488. dest->port ? dest->port : vport,
  489. flags, dest, skb->mark);
  490. if (!cp) {
  491. *ignored = -1;
  492. return NULL;
  493. }
  494. }
  495. IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
  496. "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
  497. ip_vs_fwd_tag(cp),
  498. IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
  499. IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
  500. IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport),
  501. cp->flags, refcount_read(&cp->refcnt));
  502. ip_vs_conn_stats(cp, svc);
  503. return cp;
  504. }
  505. static inline int ip_vs_addr_is_unicast(struct net *net, int af,
  506. union nf_inet_addr *addr)
  507. {
  508. #ifdef CONFIG_IP_VS_IPV6
  509. if (af == AF_INET6)
  510. return ipv6_addr_type(&addr->in6) & IPV6_ADDR_UNICAST;
  511. #endif
  512. return (inet_addr_type(net, addr->ip) == RTN_UNICAST);
  513. }
  514. /*
  515. * Pass or drop the packet.
  516. * Called by ip_vs_in, when the virtual service is available but
  517. * no destination is available for a new connection.
  518. */
  519. int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
  520. struct ip_vs_proto_data *pd, struct ip_vs_iphdr *iph)
  521. {
  522. __be16 _ports[2], *pptr, dport;
  523. struct netns_ipvs *ipvs = svc->ipvs;
  524. struct net *net = ipvs->net;
  525. pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports);
  526. if (!pptr)
  527. return NF_DROP;
  528. dport = likely(!ip_vs_iph_inverse(iph)) ? pptr[1] : pptr[0];
  529. /* if it is fwmark-based service, the cache_bypass sysctl is up
  530. and the destination is a non-local unicast, then create
  531. a cache_bypass connection entry */
  532. if (sysctl_cache_bypass(ipvs) && svc->fwmark &&
  533. !(iph->hdr_flags & (IP_VS_HDR_INVERSE | IP_VS_HDR_ICMP)) &&
  534. ip_vs_addr_is_unicast(net, svc->af, &iph->daddr)) {
  535. int ret;
  536. struct ip_vs_conn *cp;
  537. unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET &&
  538. iph->protocol == IPPROTO_UDP) ?
  539. IP_VS_CONN_F_ONE_PACKET : 0;
  540. union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } };
  541. /* create a new connection entry */
  542. IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__);
  543. {
  544. struct ip_vs_conn_param p;
  545. ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol,
  546. &iph->saddr, pptr[0],
  547. &iph->daddr, pptr[1], &p);
  548. cp = ip_vs_conn_new(&p, svc->af, &daddr, 0,
  549. IP_VS_CONN_F_BYPASS | flags,
  550. NULL, skb->mark);
  551. if (!cp)
  552. return NF_DROP;
  553. }
  554. /* statistics */
  555. ip_vs_in_stats(cp, skb);
  556. /* set state */
  557. ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
  558. /* transmit the first SYN packet */
  559. ret = cp->packet_xmit(skb, cp, pd->pp, iph);
  560. /* do not touch skb anymore */
  561. if ((cp->flags & IP_VS_CONN_F_ONE_PACKET) && cp->control)
  562. atomic_inc(&cp->control->in_pkts);
  563. else
  564. atomic_inc(&cp->in_pkts);
  565. ip_vs_conn_put(cp);
  566. return ret;
  567. }
  568. /*
  569. * When the virtual ftp service is presented, packets destined
  570. * for other services on the VIP may get here (except services
  571. * listed in the ipvs table), pass the packets, because it is
  572. * not ipvs job to decide to drop the packets.
  573. */
  574. if (svc->port == FTPPORT && dport != FTPPORT)
  575. return NF_ACCEPT;
  576. if (unlikely(ip_vs_iph_icmp(iph)))
  577. return NF_DROP;
  578. /*
  579. * Notify the client that the destination is unreachable, and
  580. * release the socket buffer.
  581. * Since it is in IP layer, the TCP socket is not actually
  582. * created, the TCP RST packet cannot be sent, instead that
  583. * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
  584. */
  585. #ifdef CONFIG_IP_VS_IPV6
  586. if (svc->af == AF_INET6) {
  587. if (!skb->dev)
  588. skb->dev = net->loopback_dev;
  589. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  590. } else
  591. #endif
  592. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  593. return NF_DROP;
  594. }
  595. #ifdef CONFIG_SYSCTL
  596. static int sysctl_snat_reroute(struct netns_ipvs *ipvs)
  597. {
  598. return ipvs->sysctl_snat_reroute;
  599. }
  600. static int sysctl_nat_icmp_send(struct netns_ipvs *ipvs)
  601. {
  602. return ipvs->sysctl_nat_icmp_send;
  603. }
  604. static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs)
  605. {
  606. return ipvs->sysctl_expire_nodest_conn;
  607. }
  608. #else
  609. static int sysctl_snat_reroute(struct netns_ipvs *ipvs) { return 0; }
  610. static int sysctl_nat_icmp_send(struct netns_ipvs *ipvs) { return 0; }
  611. static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs) { return 0; }
  612. #endif
  613. __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
  614. {
  615. return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
  616. }
  617. static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum)
  618. {
  619. if (NF_INET_LOCAL_IN == hooknum)
  620. return IP_DEFRAG_VS_IN;
  621. if (NF_INET_FORWARD == hooknum)
  622. return IP_DEFRAG_VS_FWD;
  623. return IP_DEFRAG_VS_OUT;
  624. }
  625. static inline int ip_vs_gather_frags(struct netns_ipvs *ipvs,
  626. struct sk_buff *skb, u_int32_t user)
  627. {
  628. int err;
  629. local_bh_disable();
  630. err = ip_defrag(ipvs->net, skb, user);
  631. local_bh_enable();
  632. if (!err)
  633. ip_send_check(ip_hdr(skb));
  634. return err;
  635. }
  636. static int ip_vs_route_me_harder(struct netns_ipvs *ipvs, int af,
  637. struct sk_buff *skb, unsigned int hooknum)
  638. {
  639. if (!sysctl_snat_reroute(ipvs))
  640. return 0;
  641. /* Reroute replies only to remote clients (FORWARD and LOCAL_OUT) */
  642. if (NF_INET_LOCAL_IN == hooknum)
  643. return 0;
  644. #ifdef CONFIG_IP_VS_IPV6
  645. if (af == AF_INET6) {
  646. struct dst_entry *dst = skb_dst(skb);
  647. if (dst->dev && !(dst->dev->flags & IFF_LOOPBACK) &&
  648. ip6_route_me_harder(ipvs->net, skb) != 0)
  649. return 1;
  650. } else
  651. #endif
  652. if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL) &&
  653. ip_route_me_harder(ipvs->net, skb, RTN_LOCAL) != 0)
  654. return 1;
  655. return 0;
  656. }
  657. /*
  658. * Packet has been made sufficiently writable in caller
  659. * - inout: 1=in->out, 0=out->in
  660. */
  661. void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
  662. struct ip_vs_conn *cp, int inout)
  663. {
  664. struct iphdr *iph = ip_hdr(skb);
  665. unsigned int icmp_offset = iph->ihl*4;
  666. struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
  667. icmp_offset);
  668. struct iphdr *ciph = (struct iphdr *)(icmph + 1);
  669. if (inout) {
  670. iph->saddr = cp->vaddr.ip;
  671. ip_send_check(iph);
  672. ciph->daddr = cp->vaddr.ip;
  673. ip_send_check(ciph);
  674. } else {
  675. iph->daddr = cp->daddr.ip;
  676. ip_send_check(iph);
  677. ciph->saddr = cp->daddr.ip;
  678. ip_send_check(ciph);
  679. }
  680. /* the TCP/UDP/SCTP port */
  681. if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol ||
  682. IPPROTO_SCTP == ciph->protocol) {
  683. __be16 *ports = (void *)ciph + ciph->ihl*4;
  684. if (inout)
  685. ports[1] = cp->vport;
  686. else
  687. ports[0] = cp->dport;
  688. }
  689. /* And finally the ICMP checksum */
  690. icmph->checksum = 0;
  691. icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
  692. skb->ip_summed = CHECKSUM_UNNECESSARY;
  693. if (inout)
  694. IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
  695. "Forwarding altered outgoing ICMP");
  696. else
  697. IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
  698. "Forwarding altered incoming ICMP");
  699. }
  700. #ifdef CONFIG_IP_VS_IPV6
  701. void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
  702. struct ip_vs_conn *cp, int inout)
  703. {
  704. struct ipv6hdr *iph = ipv6_hdr(skb);
  705. unsigned int icmp_offset = 0;
  706. unsigned int offs = 0; /* header offset*/
  707. int protocol;
  708. struct icmp6hdr *icmph;
  709. struct ipv6hdr *ciph;
  710. unsigned short fragoffs;
  711. ipv6_find_hdr(skb, &icmp_offset, IPPROTO_ICMPV6, &fragoffs, NULL);
  712. icmph = (struct icmp6hdr *)(skb_network_header(skb) + icmp_offset);
  713. offs = icmp_offset + sizeof(struct icmp6hdr);
  714. ciph = (struct ipv6hdr *)(skb_network_header(skb) + offs);
  715. protocol = ipv6_find_hdr(skb, &offs, -1, &fragoffs, NULL);
  716. if (inout) {
  717. iph->saddr = cp->vaddr.in6;
  718. ciph->daddr = cp->vaddr.in6;
  719. } else {
  720. iph->daddr = cp->daddr.in6;
  721. ciph->saddr = cp->daddr.in6;
  722. }
  723. /* the TCP/UDP/SCTP port */
  724. if (!fragoffs && (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
  725. IPPROTO_SCTP == protocol)) {
  726. __be16 *ports = (void *)(skb_network_header(skb) + offs);
  727. IP_VS_DBG(11, "%s() changed port %d to %d\n", __func__,
  728. ntohs(inout ? ports[1] : ports[0]),
  729. ntohs(inout ? cp->vport : cp->dport));
  730. if (inout)
  731. ports[1] = cp->vport;
  732. else
  733. ports[0] = cp->dport;
  734. }
  735. /* And finally the ICMP checksum */
  736. icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr,
  737. skb->len - icmp_offset,
  738. IPPROTO_ICMPV6, 0);
  739. skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset;
  740. skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
  741. skb->ip_summed = CHECKSUM_PARTIAL;
  742. if (inout)
  743. IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
  744. (void *)ciph - (void *)iph,
  745. "Forwarding altered outgoing ICMPv6");
  746. else
  747. IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
  748. (void *)ciph - (void *)iph,
  749. "Forwarding altered incoming ICMPv6");
  750. }
  751. #endif
  752. /* Handle relevant response ICMP messages - forward to the right
  753. * destination host.
  754. */
  755. static int handle_response_icmp(int af, struct sk_buff *skb,
  756. union nf_inet_addr *snet,
  757. __u8 protocol, struct ip_vs_conn *cp,
  758. struct ip_vs_protocol *pp,
  759. unsigned int offset, unsigned int ihl,
  760. unsigned int hooknum)
  761. {
  762. unsigned int verdict = NF_DROP;
  763. if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
  764. goto ignore_cp;
  765. /* Ensure the checksum is correct */
  766. if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
  767. /* Failed checksum! */
  768. IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
  769. IP_VS_DBG_ADDR(af, snet));
  770. goto out;
  771. }
  772. if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
  773. IPPROTO_SCTP == protocol)
  774. offset += 2 * sizeof(__u16);
  775. if (skb_ensure_writable(skb, offset))
  776. goto out;
  777. #ifdef CONFIG_IP_VS_IPV6
  778. if (af == AF_INET6)
  779. ip_vs_nat_icmp_v6(skb, pp, cp, 1);
  780. else
  781. #endif
  782. ip_vs_nat_icmp(skb, pp, cp, 1);
  783. if (ip_vs_route_me_harder(cp->ipvs, af, skb, hooknum))
  784. goto out;
  785. /* do the statistics and put it back */
  786. ip_vs_out_stats(cp, skb);
  787. skb->ipvs_property = 1;
  788. if (!(cp->flags & IP_VS_CONN_F_NFCT))
  789. ip_vs_notrack(skb);
  790. else
  791. ip_vs_update_conntrack(skb, cp, 0);
  792. ignore_cp:
  793. verdict = NF_ACCEPT;
  794. out:
  795. __ip_vs_conn_put(cp);
  796. return verdict;
  797. }
  798. /*
  799. * Handle ICMP messages in the inside-to-outside direction (outgoing).
  800. * Find any that might be relevant, check against existing connections.
  801. * Currently handles error types - unreachable, quench, ttl exceeded.
  802. */
  803. static int ip_vs_out_icmp(struct netns_ipvs *ipvs, struct sk_buff *skb,
  804. int *related, unsigned int hooknum)
  805. {
  806. struct iphdr *iph;
  807. struct icmphdr _icmph, *ic;
  808. struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
  809. struct ip_vs_iphdr ciph;
  810. struct ip_vs_conn *cp;
  811. struct ip_vs_protocol *pp;
  812. unsigned int offset, ihl;
  813. union nf_inet_addr snet;
  814. *related = 1;
  815. /* reassemble IP fragments */
  816. if (ip_is_fragment(ip_hdr(skb))) {
  817. if (ip_vs_gather_frags(ipvs, skb, ip_vs_defrag_user(hooknum)))
  818. return NF_STOLEN;
  819. }
  820. iph = ip_hdr(skb);
  821. offset = ihl = iph->ihl * 4;
  822. ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
  823. if (ic == NULL)
  824. return NF_DROP;
  825. IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
  826. ic->type, ntohs(icmp_id(ic)),
  827. &iph->saddr, &iph->daddr);
  828. /*
  829. * Work through seeing if this is for us.
  830. * These checks are supposed to be in an order that means easy
  831. * things are checked first to speed up processing.... however
  832. * this means that some packets will manage to get a long way
  833. * down this stack and then be rejected, but that's life.
  834. */
  835. if ((ic->type != ICMP_DEST_UNREACH) &&
  836. (ic->type != ICMP_SOURCE_QUENCH) &&
  837. (ic->type != ICMP_TIME_EXCEEDED)) {
  838. *related = 0;
  839. return NF_ACCEPT;
  840. }
  841. /* Now find the contained IP header */
  842. offset += sizeof(_icmph);
  843. cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
  844. if (cih == NULL)
  845. return NF_ACCEPT; /* The packet looks wrong, ignore */
  846. pp = ip_vs_proto_get(cih->protocol);
  847. if (!pp)
  848. return NF_ACCEPT;
  849. /* Is the embedded protocol header present? */
  850. if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
  851. pp->dont_defrag))
  852. return NF_ACCEPT;
  853. IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
  854. "Checking outgoing ICMP for");
  855. ip_vs_fill_iph_skb_icmp(AF_INET, skb, offset, true, &ciph);
  856. /* The embedded headers contain source and dest in reverse order */
  857. cp = INDIRECT_CALL_1(pp->conn_out_get, ip_vs_conn_out_get_proto,
  858. ipvs, AF_INET, skb, &ciph);
  859. if (!cp)
  860. return NF_ACCEPT;
  861. snet.ip = iph->saddr;
  862. return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
  863. pp, ciph.len, ihl, hooknum);
  864. }
  865. #ifdef CONFIG_IP_VS_IPV6
  866. static int ip_vs_out_icmp_v6(struct netns_ipvs *ipvs, struct sk_buff *skb,
  867. int *related, unsigned int hooknum,
  868. struct ip_vs_iphdr *ipvsh)
  869. {
  870. struct icmp6hdr _icmph, *ic;
  871. struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
  872. struct ip_vs_conn *cp;
  873. struct ip_vs_protocol *pp;
  874. union nf_inet_addr snet;
  875. unsigned int offset;
  876. *related = 1;
  877. ic = frag_safe_skb_hp(skb, ipvsh->len, sizeof(_icmph), &_icmph);
  878. if (ic == NULL)
  879. return NF_DROP;
  880. /*
  881. * Work through seeing if this is for us.
  882. * These checks are supposed to be in an order that means easy
  883. * things are checked first to speed up processing.... however
  884. * this means that some packets will manage to get a long way
  885. * down this stack and then be rejected, but that's life.
  886. */
  887. if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
  888. *related = 0;
  889. return NF_ACCEPT;
  890. }
  891. /* Fragment header that is before ICMP header tells us that:
  892. * it's not an error message since they can't be fragmented.
  893. */
  894. if (ipvsh->flags & IP6_FH_F_FRAG)
  895. return NF_DROP;
  896. IP_VS_DBG(8, "Outgoing ICMPv6 (%d,%d) %pI6c->%pI6c\n",
  897. ic->icmp6_type, ntohs(icmpv6_id(ic)),
  898. &ipvsh->saddr, &ipvsh->daddr);
  899. if (!ip_vs_fill_iph_skb_icmp(AF_INET6, skb, ipvsh->len + sizeof(_icmph),
  900. true, &ciph))
  901. return NF_ACCEPT; /* The packet looks wrong, ignore */
  902. pp = ip_vs_proto_get(ciph.protocol);
  903. if (!pp)
  904. return NF_ACCEPT;
  905. /* The embedded headers contain source and dest in reverse order */
  906. cp = INDIRECT_CALL_1(pp->conn_out_get, ip_vs_conn_out_get_proto,
  907. ipvs, AF_INET6, skb, &ciph);
  908. if (!cp)
  909. return NF_ACCEPT;
  910. snet.in6 = ciph.saddr.in6;
  911. offset = ciph.len;
  912. return handle_response_icmp(AF_INET6, skb, &snet, ciph.protocol, cp,
  913. pp, offset, sizeof(struct ipv6hdr),
  914. hooknum);
  915. }
  916. #endif
  917. /*
  918. * Check if sctp chunc is ABORT chunk
  919. */
  920. static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len)
  921. {
  922. struct sctp_chunkhdr *sch, schunk;
  923. sch = skb_header_pointer(skb, nh_len + sizeof(struct sctphdr),
  924. sizeof(schunk), &schunk);
  925. if (sch == NULL)
  926. return 0;
  927. if (sch->type == SCTP_CID_ABORT)
  928. return 1;
  929. return 0;
  930. }
  931. static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
  932. {
  933. struct tcphdr _tcph, *th;
  934. th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
  935. if (th == NULL)
  936. return 0;
  937. return th->rst;
  938. }
  939. static inline bool is_new_conn(const struct sk_buff *skb,
  940. struct ip_vs_iphdr *iph)
  941. {
  942. switch (iph->protocol) {
  943. case IPPROTO_TCP: {
  944. struct tcphdr _tcph, *th;
  945. th = skb_header_pointer(skb, iph->len, sizeof(_tcph), &_tcph);
  946. if (th == NULL)
  947. return false;
  948. return th->syn;
  949. }
  950. case IPPROTO_SCTP: {
  951. struct sctp_chunkhdr *sch, schunk;
  952. sch = skb_header_pointer(skb, iph->len + sizeof(struct sctphdr),
  953. sizeof(schunk), &schunk);
  954. if (sch == NULL)
  955. return false;
  956. return sch->type == SCTP_CID_INIT;
  957. }
  958. default:
  959. return false;
  960. }
  961. }
  962. static inline bool is_new_conn_expected(const struct ip_vs_conn *cp,
  963. int conn_reuse_mode)
  964. {
  965. /* Controlled (FTP DATA or persistence)? */
  966. if (cp->control)
  967. return false;
  968. switch (cp->protocol) {
  969. case IPPROTO_TCP:
  970. return (cp->state == IP_VS_TCP_S_TIME_WAIT) ||
  971. (cp->state == IP_VS_TCP_S_CLOSE) ||
  972. ((conn_reuse_mode & 2) &&
  973. (cp->state == IP_VS_TCP_S_FIN_WAIT) &&
  974. (cp->flags & IP_VS_CONN_F_NOOUTPUT));
  975. case IPPROTO_SCTP:
  976. return cp->state == IP_VS_SCTP_S_CLOSED;
  977. default:
  978. return false;
  979. }
  980. }
  981. /* Generic function to create new connections for outgoing RS packets
  982. *
  983. * Pre-requisites for successful connection creation:
  984. * 1) Virtual Service is NOT fwmark based:
  985. * In fwmark-VS actual vaddr and vport are unknown to IPVS
  986. * 2) Real Server and Virtual Service were NOT configured without port:
  987. * This is to allow match of different VS to the same RS ip-addr
  988. */
  989. struct ip_vs_conn *ip_vs_new_conn_out(struct ip_vs_service *svc,
  990. struct ip_vs_dest *dest,
  991. struct sk_buff *skb,
  992. const struct ip_vs_iphdr *iph,
  993. __be16 dport,
  994. __be16 cport)
  995. {
  996. struct ip_vs_conn_param param;
  997. struct ip_vs_conn *ct = NULL, *cp = NULL;
  998. const union nf_inet_addr *vaddr, *daddr, *caddr;
  999. union nf_inet_addr snet;
  1000. __be16 vport;
  1001. unsigned int flags;
  1002. EnterFunction(12);
  1003. vaddr = &svc->addr;
  1004. vport = svc->port;
  1005. daddr = &iph->saddr;
  1006. caddr = &iph->daddr;
  1007. /* check pre-requisites are satisfied */
  1008. if (svc->fwmark)
  1009. return NULL;
  1010. if (!vport || !dport)
  1011. return NULL;
  1012. /* for persistent service first create connection template */
  1013. if (svc->flags & IP_VS_SVC_F_PERSISTENT) {
  1014. /* apply netmask the same way ingress-side does */
  1015. #ifdef CONFIG_IP_VS_IPV6
  1016. if (svc->af == AF_INET6)
  1017. ipv6_addr_prefix(&snet.in6, &caddr->in6,
  1018. (__force __u32)svc->netmask);
  1019. else
  1020. #endif
  1021. snet.ip = caddr->ip & svc->netmask;
  1022. /* fill params and create template if not existent */
  1023. if (ip_vs_conn_fill_param_persist(svc, skb, iph->protocol,
  1024. &snet, 0, vaddr,
  1025. vport, &param) < 0)
  1026. return NULL;
  1027. ct = ip_vs_ct_in_get(&param);
  1028. /* check if template exists and points to the same dest */
  1029. if (!ct || !ip_vs_check_template(ct, dest)) {
  1030. ct = ip_vs_conn_new(&param, dest->af, daddr, dport,
  1031. IP_VS_CONN_F_TEMPLATE, dest, 0);
  1032. if (!ct) {
  1033. kfree(param.pe_data);
  1034. return NULL;
  1035. }
  1036. ct->timeout = svc->timeout;
  1037. } else {
  1038. kfree(param.pe_data);
  1039. }
  1040. }
  1041. /* connection flags */
  1042. flags = ((svc->flags & IP_VS_SVC_F_ONEPACKET) &&
  1043. iph->protocol == IPPROTO_UDP) ? IP_VS_CONN_F_ONE_PACKET : 0;
  1044. /* create connection */
  1045. ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol,
  1046. caddr, cport, vaddr, vport, &param);
  1047. cp = ip_vs_conn_new(&param, dest->af, daddr, dport, flags, dest, 0);
  1048. if (!cp) {
  1049. if (ct)
  1050. ip_vs_conn_put(ct);
  1051. return NULL;
  1052. }
  1053. if (ct) {
  1054. ip_vs_control_add(cp, ct);
  1055. ip_vs_conn_put(ct);
  1056. }
  1057. ip_vs_conn_stats(cp, svc);
  1058. /* return connection (will be used to handle outgoing packet) */
  1059. IP_VS_DBG_BUF(6, "New connection RS-initiated:%c c:%s:%u v:%s:%u "
  1060. "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
  1061. ip_vs_fwd_tag(cp),
  1062. IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
  1063. IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
  1064. IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
  1065. cp->flags, refcount_read(&cp->refcnt));
  1066. LeaveFunction(12);
  1067. return cp;
  1068. }
  1069. /* Handle outgoing packets which are considered requests initiated by
  1070. * real servers, so that subsequent responses from external client can be
  1071. * routed to the right real server.
  1072. * Used also for outgoing responses in OPS mode.
  1073. *
  1074. * Connection management is handled by persistent-engine specific callback.
  1075. */
  1076. static struct ip_vs_conn *__ip_vs_rs_conn_out(unsigned int hooknum,
  1077. struct netns_ipvs *ipvs,
  1078. int af, struct sk_buff *skb,
  1079. const struct ip_vs_iphdr *iph)
  1080. {
  1081. struct ip_vs_dest *dest;
  1082. struct ip_vs_conn *cp = NULL;
  1083. __be16 _ports[2], *pptr;
  1084. if (hooknum == NF_INET_LOCAL_IN)
  1085. return NULL;
  1086. pptr = frag_safe_skb_hp(skb, iph->len,
  1087. sizeof(_ports), _ports);
  1088. if (!pptr)
  1089. return NULL;
  1090. dest = ip_vs_find_real_service(ipvs, af, iph->protocol,
  1091. &iph->saddr, pptr[0]);
  1092. if (dest) {
  1093. struct ip_vs_service *svc;
  1094. struct ip_vs_pe *pe;
  1095. svc = rcu_dereference(dest->svc);
  1096. if (svc) {
  1097. pe = rcu_dereference(svc->pe);
  1098. if (pe && pe->conn_out)
  1099. cp = pe->conn_out(svc, dest, skb, iph,
  1100. pptr[0], pptr[1]);
  1101. }
  1102. }
  1103. return cp;
  1104. }
  1105. /* Handle response packets: rewrite addresses and send away...
  1106. */
  1107. static unsigned int
  1108. handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
  1109. struct ip_vs_conn *cp, struct ip_vs_iphdr *iph,
  1110. unsigned int hooknum)
  1111. {
  1112. struct ip_vs_protocol *pp = pd->pp;
  1113. IP_VS_DBG_PKT(11, af, pp, skb, iph->off, "Outgoing packet");
  1114. if (skb_ensure_writable(skb, iph->len))
  1115. goto drop;
  1116. /* mangle the packet */
  1117. if (pp->snat_handler &&
  1118. !SNAT_CALL(pp->snat_handler, skb, pp, cp, iph))
  1119. goto drop;
  1120. #ifdef CONFIG_IP_VS_IPV6
  1121. if (af == AF_INET6)
  1122. ipv6_hdr(skb)->saddr = cp->vaddr.in6;
  1123. else
  1124. #endif
  1125. {
  1126. ip_hdr(skb)->saddr = cp->vaddr.ip;
  1127. ip_send_check(ip_hdr(skb));
  1128. }
  1129. /*
  1130. * nf_iterate does not expect change in the skb->dst->dev.
  1131. * It looks like it is not fatal to enable this code for hooks
  1132. * where our handlers are at the end of the chain list and
  1133. * when all next handlers use skb->dst->dev and not outdev.
  1134. * It will definitely route properly the inout NAT traffic
  1135. * when multiple paths are used.
  1136. */
  1137. /* For policy routing, packets originating from this
  1138. * machine itself may be routed differently to packets
  1139. * passing through. We want this packet to be routed as
  1140. * if it came from this machine itself. So re-compute
  1141. * the routing information.
  1142. */
  1143. if (ip_vs_route_me_harder(cp->ipvs, af, skb, hooknum))
  1144. goto drop;
  1145. IP_VS_DBG_PKT(10, af, pp, skb, iph->off, "After SNAT");
  1146. ip_vs_out_stats(cp, skb);
  1147. ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd);
  1148. skb->ipvs_property = 1;
  1149. if (!(cp->flags & IP_VS_CONN_F_NFCT))
  1150. ip_vs_notrack(skb);
  1151. else
  1152. ip_vs_update_conntrack(skb, cp, 0);
  1153. ip_vs_conn_put(cp);
  1154. LeaveFunction(11);
  1155. return NF_ACCEPT;
  1156. drop:
  1157. ip_vs_conn_put(cp);
  1158. kfree_skb(skb);
  1159. LeaveFunction(11);
  1160. return NF_STOLEN;
  1161. }
  1162. /*
  1163. * Check if outgoing packet belongs to the established ip_vs_conn.
  1164. */
  1165. static unsigned int
  1166. ip_vs_out(struct netns_ipvs *ipvs, unsigned int hooknum, struct sk_buff *skb, int af)
  1167. {
  1168. struct ip_vs_iphdr iph;
  1169. struct ip_vs_protocol *pp;
  1170. struct ip_vs_proto_data *pd;
  1171. struct ip_vs_conn *cp;
  1172. struct sock *sk;
  1173. EnterFunction(11);
  1174. /* Already marked as IPVS request or reply? */
  1175. if (skb->ipvs_property)
  1176. return NF_ACCEPT;
  1177. sk = skb_to_full_sk(skb);
  1178. /* Bad... Do not break raw sockets */
  1179. if (unlikely(sk && hooknum == NF_INET_LOCAL_OUT &&
  1180. af == AF_INET)) {
  1181. if (sk->sk_family == PF_INET && inet_sk(sk)->nodefrag)
  1182. return NF_ACCEPT;
  1183. }
  1184. if (unlikely(!skb_dst(skb)))
  1185. return NF_ACCEPT;
  1186. if (!ipvs->enable)
  1187. return NF_ACCEPT;
  1188. ip_vs_fill_iph_skb(af, skb, false, &iph);
  1189. #ifdef CONFIG_IP_VS_IPV6
  1190. if (af == AF_INET6) {
  1191. if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
  1192. int related;
  1193. int verdict = ip_vs_out_icmp_v6(ipvs, skb, &related,
  1194. hooknum, &iph);
  1195. if (related)
  1196. return verdict;
  1197. }
  1198. } else
  1199. #endif
  1200. if (unlikely(iph.protocol == IPPROTO_ICMP)) {
  1201. int related;
  1202. int verdict = ip_vs_out_icmp(ipvs, skb, &related, hooknum);
  1203. if (related)
  1204. return verdict;
  1205. }
  1206. pd = ip_vs_proto_data_get(ipvs, iph.protocol);
  1207. if (unlikely(!pd))
  1208. return NF_ACCEPT;
  1209. pp = pd->pp;
  1210. /* reassemble IP fragments */
  1211. #ifdef CONFIG_IP_VS_IPV6
  1212. if (af == AF_INET)
  1213. #endif
  1214. if (unlikely(ip_is_fragment(ip_hdr(skb)) && !pp->dont_defrag)) {
  1215. if (ip_vs_gather_frags(ipvs, skb,
  1216. ip_vs_defrag_user(hooknum)))
  1217. return NF_STOLEN;
  1218. ip_vs_fill_iph_skb(AF_INET, skb, false, &iph);
  1219. }
  1220. /*
  1221. * Check if the packet belongs to an existing entry
  1222. */
  1223. cp = INDIRECT_CALL_1(pp->conn_out_get, ip_vs_conn_out_get_proto,
  1224. ipvs, af, skb, &iph);
  1225. if (likely(cp)) {
  1226. if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
  1227. goto ignore_cp;
  1228. return handle_response(af, skb, pd, cp, &iph, hooknum);
  1229. }
  1230. /* Check for real-server-started requests */
  1231. if (atomic_read(&ipvs->conn_out_counter)) {
  1232. /* Currently only for UDP:
  1233. * connection oriented protocols typically use
  1234. * ephemeral ports for outgoing connections, so
  1235. * related incoming responses would not match any VS
  1236. */
  1237. if (pp->protocol == IPPROTO_UDP) {
  1238. cp = __ip_vs_rs_conn_out(hooknum, ipvs, af, skb, &iph);
  1239. if (likely(cp))
  1240. return handle_response(af, skb, pd, cp, &iph,
  1241. hooknum);
  1242. }
  1243. }
  1244. if (sysctl_nat_icmp_send(ipvs) &&
  1245. (pp->protocol == IPPROTO_TCP ||
  1246. pp->protocol == IPPROTO_UDP ||
  1247. pp->protocol == IPPROTO_SCTP)) {
  1248. __be16 _ports[2], *pptr;
  1249. pptr = frag_safe_skb_hp(skb, iph.len,
  1250. sizeof(_ports), _ports);
  1251. if (pptr == NULL)
  1252. return NF_ACCEPT; /* Not for me */
  1253. if (ip_vs_has_real_service(ipvs, af, iph.protocol, &iph.saddr,
  1254. pptr[0])) {
  1255. /*
  1256. * Notify the real server: there is no
  1257. * existing entry if it is not RST
  1258. * packet or not TCP packet.
  1259. */
  1260. if ((iph.protocol != IPPROTO_TCP &&
  1261. iph.protocol != IPPROTO_SCTP)
  1262. || ((iph.protocol == IPPROTO_TCP
  1263. && !is_tcp_reset(skb, iph.len))
  1264. || (iph.protocol == IPPROTO_SCTP
  1265. && !is_sctp_abort(skb,
  1266. iph.len)))) {
  1267. #ifdef CONFIG_IP_VS_IPV6
  1268. if (af == AF_INET6) {
  1269. if (!skb->dev)
  1270. skb->dev = ipvs->net->loopback_dev;
  1271. icmpv6_send(skb,
  1272. ICMPV6_DEST_UNREACH,
  1273. ICMPV6_PORT_UNREACH,
  1274. 0);
  1275. } else
  1276. #endif
  1277. icmp_send(skb,
  1278. ICMP_DEST_UNREACH,
  1279. ICMP_PORT_UNREACH, 0);
  1280. return NF_DROP;
  1281. }
  1282. }
  1283. }
  1284. out:
  1285. IP_VS_DBG_PKT(12, af, pp, skb, iph.off,
  1286. "ip_vs_out: packet continues traversal as normal");
  1287. return NF_ACCEPT;
  1288. ignore_cp:
  1289. __ip_vs_conn_put(cp);
  1290. goto out;
  1291. }
  1292. /*
  1293. * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
  1294. * used only for VS/NAT.
  1295. * Check if packet is reply for established ip_vs_conn.
  1296. */
  1297. static unsigned int
  1298. ip_vs_reply4(void *priv, struct sk_buff *skb,
  1299. const struct nf_hook_state *state)
  1300. {
  1301. return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET);
  1302. }
  1303. /*
  1304. * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
  1305. * Check if packet is reply for established ip_vs_conn.
  1306. */
  1307. static unsigned int
  1308. ip_vs_local_reply4(void *priv, struct sk_buff *skb,
  1309. const struct nf_hook_state *state)
  1310. {
  1311. return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET);
  1312. }
  1313. #ifdef CONFIG_IP_VS_IPV6
  1314. /*
  1315. * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
  1316. * used only for VS/NAT.
  1317. * Check if packet is reply for established ip_vs_conn.
  1318. */
  1319. static unsigned int
  1320. ip_vs_reply6(void *priv, struct sk_buff *skb,
  1321. const struct nf_hook_state *state)
  1322. {
  1323. return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET6);
  1324. }
  1325. /*
  1326. * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
  1327. * Check if packet is reply for established ip_vs_conn.
  1328. */
  1329. static unsigned int
  1330. ip_vs_local_reply6(void *priv, struct sk_buff *skb,
  1331. const struct nf_hook_state *state)
  1332. {
  1333. return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET6);
  1334. }
  1335. #endif
  1336. static unsigned int
  1337. ip_vs_try_to_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb,
  1338. struct ip_vs_proto_data *pd,
  1339. int *verdict, struct ip_vs_conn **cpp,
  1340. struct ip_vs_iphdr *iph)
  1341. {
  1342. struct ip_vs_protocol *pp = pd->pp;
  1343. if (!iph->fragoffs) {
  1344. /* No (second) fragments need to enter here, as nf_defrag_ipv6
  1345. * replayed fragment zero will already have created the cp
  1346. */
  1347. /* Schedule and create new connection entry into cpp */
  1348. if (!pp->conn_schedule(ipvs, af, skb, pd, verdict, cpp, iph))
  1349. return 0;
  1350. }
  1351. if (unlikely(!*cpp)) {
  1352. /* sorry, all this trouble for a no-hit :) */
  1353. IP_VS_DBG_PKT(12, af, pp, skb, iph->off,
  1354. "ip_vs_in: packet continues traversal as normal");
  1355. /* Fragment couldn't be mapped to a conn entry */
  1356. if (iph->fragoffs)
  1357. IP_VS_DBG_PKT(7, af, pp, skb, iph->off,
  1358. "unhandled fragment");
  1359. *verdict = NF_ACCEPT;
  1360. return 0;
  1361. }
  1362. return 1;
  1363. }
  1364. /* Check the UDP tunnel and return its header length */
  1365. static int ipvs_udp_decap(struct netns_ipvs *ipvs, struct sk_buff *skb,
  1366. unsigned int offset, __u16 af,
  1367. const union nf_inet_addr *daddr, __u8 *proto)
  1368. {
  1369. struct udphdr _udph, *udph;
  1370. struct ip_vs_dest *dest;
  1371. udph = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
  1372. if (!udph)
  1373. goto unk;
  1374. offset += sizeof(struct udphdr);
  1375. dest = ip_vs_find_tunnel(ipvs, af, daddr, udph->dest);
  1376. if (!dest)
  1377. goto unk;
  1378. if (dest->tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE) {
  1379. struct guehdr _gueh, *gueh;
  1380. gueh = skb_header_pointer(skb, offset, sizeof(_gueh), &_gueh);
  1381. if (!gueh)
  1382. goto unk;
  1383. if (gueh->control != 0 || gueh->version != 0)
  1384. goto unk;
  1385. /* Later we can support also IPPROTO_IPV6 */
  1386. if (gueh->proto_ctype != IPPROTO_IPIP)
  1387. goto unk;
  1388. *proto = gueh->proto_ctype;
  1389. return sizeof(struct udphdr) + sizeof(struct guehdr) +
  1390. (gueh->hlen << 2);
  1391. }
  1392. unk:
  1393. return 0;
  1394. }
  1395. /* Check the GRE tunnel and return its header length */
  1396. static int ipvs_gre_decap(struct netns_ipvs *ipvs, struct sk_buff *skb,
  1397. unsigned int offset, __u16 af,
  1398. const union nf_inet_addr *daddr, __u8 *proto)
  1399. {
  1400. struct gre_base_hdr _greh, *greh;
  1401. struct ip_vs_dest *dest;
  1402. greh = skb_header_pointer(skb, offset, sizeof(_greh), &_greh);
  1403. if (!greh)
  1404. goto unk;
  1405. dest = ip_vs_find_tunnel(ipvs, af, daddr, 0);
  1406. if (!dest)
  1407. goto unk;
  1408. if (dest->tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GRE) {
  1409. __be16 type;
  1410. /* Only support version 0 and C (csum) */
  1411. if ((greh->flags & ~GRE_CSUM) != 0)
  1412. goto unk;
  1413. type = greh->protocol;
  1414. /* Later we can support also IPPROTO_IPV6 */
  1415. if (type != htons(ETH_P_IP))
  1416. goto unk;
  1417. *proto = IPPROTO_IPIP;
  1418. return gre_calc_hlen(gre_flags_to_tnl_flags(greh->flags));
  1419. }
  1420. unk:
  1421. return 0;
  1422. }
  1423. /*
  1424. * Handle ICMP messages in the outside-to-inside direction (incoming).
  1425. * Find any that might be relevant, check against existing connections,
  1426. * forward to the right destination host if relevant.
  1427. * Currently handles error types - unreachable, quench, ttl exceeded.
  1428. */
  1429. static int
  1430. ip_vs_in_icmp(struct netns_ipvs *ipvs, struct sk_buff *skb, int *related,
  1431. unsigned int hooknum)
  1432. {
  1433. struct iphdr *iph;
  1434. struct icmphdr _icmph, *ic;
  1435. struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
  1436. struct ip_vs_iphdr ciph;
  1437. struct ip_vs_conn *cp;
  1438. struct ip_vs_protocol *pp;
  1439. struct ip_vs_proto_data *pd;
  1440. unsigned int offset, offset2, ihl, verdict;
  1441. bool tunnel, new_cp = false;
  1442. union nf_inet_addr *raddr;
  1443. char *outer_proto = "IPIP";
  1444. *related = 1;
  1445. /* reassemble IP fragments */
  1446. if (ip_is_fragment(ip_hdr(skb))) {
  1447. if (ip_vs_gather_frags(ipvs, skb, ip_vs_defrag_user(hooknum)))
  1448. return NF_STOLEN;
  1449. }
  1450. iph = ip_hdr(skb);
  1451. offset = ihl = iph->ihl * 4;
  1452. ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
  1453. if (ic == NULL)
  1454. return NF_DROP;
  1455. IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
  1456. ic->type, ntohs(icmp_id(ic)),
  1457. &iph->saddr, &iph->daddr);
  1458. /*
  1459. * Work through seeing if this is for us.
  1460. * These checks are supposed to be in an order that means easy
  1461. * things are checked first to speed up processing.... however
  1462. * this means that some packets will manage to get a long way
  1463. * down this stack and then be rejected, but that's life.
  1464. */
  1465. if ((ic->type != ICMP_DEST_UNREACH) &&
  1466. (ic->type != ICMP_SOURCE_QUENCH) &&
  1467. (ic->type != ICMP_TIME_EXCEEDED)) {
  1468. *related = 0;
  1469. return NF_ACCEPT;
  1470. }
  1471. /* Now find the contained IP header */
  1472. offset += sizeof(_icmph);
  1473. cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
  1474. if (cih == NULL)
  1475. return NF_ACCEPT; /* The packet looks wrong, ignore */
  1476. raddr = (union nf_inet_addr *)&cih->daddr;
  1477. /* Special case for errors for IPIP/UDP/GRE tunnel packets */
  1478. tunnel = false;
  1479. if (cih->protocol == IPPROTO_IPIP) {
  1480. struct ip_vs_dest *dest;
  1481. if (unlikely(cih->frag_off & htons(IP_OFFSET)))
  1482. return NF_ACCEPT;
  1483. /* Error for our IPIP must arrive at LOCAL_IN */
  1484. if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL))
  1485. return NF_ACCEPT;
  1486. dest = ip_vs_find_tunnel(ipvs, AF_INET, raddr, 0);
  1487. /* Only for known tunnel */
  1488. if (!dest || dest->tun_type != IP_VS_CONN_F_TUNNEL_TYPE_IPIP)
  1489. return NF_ACCEPT;
  1490. offset += cih->ihl * 4;
  1491. cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
  1492. if (cih == NULL)
  1493. return NF_ACCEPT; /* The packet looks wrong, ignore */
  1494. tunnel = true;
  1495. } else if ((cih->protocol == IPPROTO_UDP || /* Can be UDP encap */
  1496. cih->protocol == IPPROTO_GRE) && /* Can be GRE encap */
  1497. /* Error for our tunnel must arrive at LOCAL_IN */
  1498. (skb_rtable(skb)->rt_flags & RTCF_LOCAL)) {
  1499. __u8 iproto;
  1500. int ulen;
  1501. /* Non-first fragment has no UDP/GRE header */
  1502. if (unlikely(cih->frag_off & htons(IP_OFFSET)))
  1503. return NF_ACCEPT;
  1504. offset2 = offset + cih->ihl * 4;
  1505. if (cih->protocol == IPPROTO_UDP) {
  1506. ulen = ipvs_udp_decap(ipvs, skb, offset2, AF_INET,
  1507. raddr, &iproto);
  1508. outer_proto = "UDP";
  1509. } else {
  1510. ulen = ipvs_gre_decap(ipvs, skb, offset2, AF_INET,
  1511. raddr, &iproto);
  1512. outer_proto = "GRE";
  1513. }
  1514. if (ulen > 0) {
  1515. /* Skip IP and UDP/GRE tunnel headers */
  1516. offset = offset2 + ulen;
  1517. /* Now we should be at the original IP header */
  1518. cih = skb_header_pointer(skb, offset, sizeof(_ciph),
  1519. &_ciph);
  1520. if (cih && cih->version == 4 && cih->ihl >= 5 &&
  1521. iproto == IPPROTO_IPIP)
  1522. tunnel = true;
  1523. else
  1524. return NF_ACCEPT;
  1525. }
  1526. }
  1527. pd = ip_vs_proto_data_get(ipvs, cih->protocol);
  1528. if (!pd)
  1529. return NF_ACCEPT;
  1530. pp = pd->pp;
  1531. /* Is the embedded protocol header present? */
  1532. if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
  1533. pp->dont_defrag))
  1534. return NF_ACCEPT;
  1535. IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
  1536. "Checking incoming ICMP for");
  1537. offset2 = offset;
  1538. ip_vs_fill_iph_skb_icmp(AF_INET, skb, offset, !tunnel, &ciph);
  1539. offset = ciph.len;
  1540. /* The embedded headers contain source and dest in reverse order.
  1541. * For IPIP/UDP/GRE tunnel this is error for request, not for reply.
  1542. */
  1543. cp = INDIRECT_CALL_1(pp->conn_in_get, ip_vs_conn_in_get_proto,
  1544. ipvs, AF_INET, skb, &ciph);
  1545. if (!cp) {
  1546. int v;
  1547. if (tunnel || !sysctl_schedule_icmp(ipvs))
  1548. return NF_ACCEPT;
  1549. if (!ip_vs_try_to_schedule(ipvs, AF_INET, skb, pd, &v, &cp, &ciph))
  1550. return v;
  1551. new_cp = true;
  1552. }
  1553. verdict = NF_DROP;
  1554. /* Ensure the checksum is correct */
  1555. if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
  1556. /* Failed checksum! */
  1557. IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
  1558. &iph->saddr);
  1559. goto out;
  1560. }
  1561. if (tunnel) {
  1562. __be32 info = ic->un.gateway;
  1563. __u8 type = ic->type;
  1564. __u8 code = ic->code;
  1565. /* Update the MTU */
  1566. if (ic->type == ICMP_DEST_UNREACH &&
  1567. ic->code == ICMP_FRAG_NEEDED) {
  1568. struct ip_vs_dest *dest = cp->dest;
  1569. u32 mtu = ntohs(ic->un.frag.mtu);
  1570. __be16 frag_off = cih->frag_off;
  1571. /* Strip outer IP and ICMP, go to IPIP/UDP/GRE header */
  1572. if (pskb_pull(skb, ihl + sizeof(_icmph)) == NULL)
  1573. goto ignore_tunnel;
  1574. offset2 -= ihl + sizeof(_icmph);
  1575. skb_reset_network_header(skb);
  1576. IP_VS_DBG(12, "ICMP for %s %pI4->%pI4: mtu=%u\n",
  1577. outer_proto, &ip_hdr(skb)->saddr,
  1578. &ip_hdr(skb)->daddr, mtu);
  1579. ipv4_update_pmtu(skb, ipvs->net, mtu, 0, 0);
  1580. /* Client uses PMTUD? */
  1581. if (!(frag_off & htons(IP_DF)))
  1582. goto ignore_tunnel;
  1583. /* Prefer the resulting PMTU */
  1584. if (dest) {
  1585. struct ip_vs_dest_dst *dest_dst;
  1586. dest_dst = rcu_dereference(dest->dest_dst);
  1587. if (dest_dst)
  1588. mtu = dst_mtu(dest_dst->dst_cache);
  1589. }
  1590. if (mtu > 68 + sizeof(struct iphdr))
  1591. mtu -= sizeof(struct iphdr);
  1592. info = htonl(mtu);
  1593. }
  1594. /* Strip outer IP, ICMP and IPIP/UDP/GRE, go to IP header of
  1595. * original request.
  1596. */
  1597. if (pskb_pull(skb, offset2) == NULL)
  1598. goto ignore_tunnel;
  1599. skb_reset_network_header(skb);
  1600. IP_VS_DBG(12, "Sending ICMP for %pI4->%pI4: t=%u, c=%u, i=%u\n",
  1601. &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr,
  1602. type, code, ntohl(info));
  1603. icmp_send(skb, type, code, info);
  1604. /* ICMP can be shorter but anyways, account it */
  1605. ip_vs_out_stats(cp, skb);
  1606. ignore_tunnel:
  1607. consume_skb(skb);
  1608. verdict = NF_STOLEN;
  1609. goto out;
  1610. }
  1611. /* do the statistics and put it back */
  1612. ip_vs_in_stats(cp, skb);
  1613. if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol ||
  1614. IPPROTO_SCTP == cih->protocol)
  1615. offset += 2 * sizeof(__u16);
  1616. verdict = ip_vs_icmp_xmit(skb, cp, pp, offset, hooknum, &ciph);
  1617. out:
  1618. if (likely(!new_cp))
  1619. __ip_vs_conn_put(cp);
  1620. else
  1621. ip_vs_conn_put(cp);
  1622. return verdict;
  1623. }
  1624. #ifdef CONFIG_IP_VS_IPV6
  1625. static int ip_vs_in_icmp_v6(struct netns_ipvs *ipvs, struct sk_buff *skb,
  1626. int *related, unsigned int hooknum,
  1627. struct ip_vs_iphdr *iph)
  1628. {
  1629. struct icmp6hdr _icmph, *ic;
  1630. struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
  1631. struct ip_vs_conn *cp;
  1632. struct ip_vs_protocol *pp;
  1633. struct ip_vs_proto_data *pd;
  1634. unsigned int offset, verdict;
  1635. bool new_cp = false;
  1636. *related = 1;
  1637. ic = frag_safe_skb_hp(skb, iph->len, sizeof(_icmph), &_icmph);
  1638. if (ic == NULL)
  1639. return NF_DROP;
  1640. /*
  1641. * Work through seeing if this is for us.
  1642. * These checks are supposed to be in an order that means easy
  1643. * things are checked first to speed up processing.... however
  1644. * this means that some packets will manage to get a long way
  1645. * down this stack and then be rejected, but that's life.
  1646. */
  1647. if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
  1648. *related = 0;
  1649. return NF_ACCEPT;
  1650. }
  1651. /* Fragment header that is before ICMP header tells us that:
  1652. * it's not an error message since they can't be fragmented.
  1653. */
  1654. if (iph->flags & IP6_FH_F_FRAG)
  1655. return NF_DROP;
  1656. IP_VS_DBG(8, "Incoming ICMPv6 (%d,%d) %pI6c->%pI6c\n",
  1657. ic->icmp6_type, ntohs(icmpv6_id(ic)),
  1658. &iph->saddr, &iph->daddr);
  1659. offset = iph->len + sizeof(_icmph);
  1660. if (!ip_vs_fill_iph_skb_icmp(AF_INET6, skb, offset, true, &ciph))
  1661. return NF_ACCEPT;
  1662. pd = ip_vs_proto_data_get(ipvs, ciph.protocol);
  1663. if (!pd)
  1664. return NF_ACCEPT;
  1665. pp = pd->pp;
  1666. /* Cannot handle fragmented embedded protocol */
  1667. if (ciph.fragoffs)
  1668. return NF_ACCEPT;
  1669. IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset,
  1670. "Checking incoming ICMPv6 for");
  1671. /* The embedded headers contain source and dest in reverse order
  1672. * if not from localhost
  1673. */
  1674. cp = INDIRECT_CALL_1(pp->conn_in_get, ip_vs_conn_in_get_proto,
  1675. ipvs, AF_INET6, skb, &ciph);
  1676. if (!cp) {
  1677. int v;
  1678. if (!sysctl_schedule_icmp(ipvs))
  1679. return NF_ACCEPT;
  1680. if (!ip_vs_try_to_schedule(ipvs, AF_INET6, skb, pd, &v, &cp, &ciph))
  1681. return v;
  1682. new_cp = true;
  1683. }
  1684. /* VS/TUN, VS/DR and LOCALNODE just let it go */
  1685. if ((hooknum == NF_INET_LOCAL_OUT) &&
  1686. (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)) {
  1687. verdict = NF_ACCEPT;
  1688. goto out;
  1689. }
  1690. /* do the statistics and put it back */
  1691. ip_vs_in_stats(cp, skb);
  1692. /* Need to mangle contained IPv6 header in ICMPv6 packet */
  1693. offset = ciph.len;
  1694. if (IPPROTO_TCP == ciph.protocol || IPPROTO_UDP == ciph.protocol ||
  1695. IPPROTO_SCTP == ciph.protocol)
  1696. offset += 2 * sizeof(__u16); /* Also mangle ports */
  1697. verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset, hooknum, &ciph);
  1698. out:
  1699. if (likely(!new_cp))
  1700. __ip_vs_conn_put(cp);
  1701. else
  1702. ip_vs_conn_put(cp);
  1703. return verdict;
  1704. }
  1705. #endif
  1706. /*
  1707. * Check if it's for virtual services, look it up,
  1708. * and send it on its way...
  1709. */
  1710. static unsigned int
  1711. ip_vs_in(struct netns_ipvs *ipvs, unsigned int hooknum, struct sk_buff *skb, int af)
  1712. {
  1713. struct ip_vs_iphdr iph;
  1714. struct ip_vs_protocol *pp;
  1715. struct ip_vs_proto_data *pd;
  1716. struct ip_vs_conn *cp;
  1717. int ret, pkts;
  1718. int conn_reuse_mode;
  1719. struct sock *sk;
  1720. /* Already marked as IPVS request or reply? */
  1721. if (skb->ipvs_property)
  1722. return NF_ACCEPT;
  1723. /*
  1724. * Big tappo:
  1725. * - remote client: only PACKET_HOST
  1726. * - route: used for struct net when skb->dev is unset
  1727. */
  1728. if (unlikely((skb->pkt_type != PACKET_HOST &&
  1729. hooknum != NF_INET_LOCAL_OUT) ||
  1730. !skb_dst(skb))) {
  1731. ip_vs_fill_iph_skb(af, skb, false, &iph);
  1732. IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
  1733. " ignored in hook %u\n",
  1734. skb->pkt_type, iph.protocol,
  1735. IP_VS_DBG_ADDR(af, &iph.daddr), hooknum);
  1736. return NF_ACCEPT;
  1737. }
  1738. /* ipvs enabled in this netns ? */
  1739. if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
  1740. return NF_ACCEPT;
  1741. ip_vs_fill_iph_skb(af, skb, false, &iph);
  1742. /* Bad... Do not break raw sockets */
  1743. sk = skb_to_full_sk(skb);
  1744. if (unlikely(sk && hooknum == NF_INET_LOCAL_OUT &&
  1745. af == AF_INET)) {
  1746. if (sk->sk_family == PF_INET && inet_sk(sk)->nodefrag)
  1747. return NF_ACCEPT;
  1748. }
  1749. #ifdef CONFIG_IP_VS_IPV6
  1750. if (af == AF_INET6) {
  1751. if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
  1752. int related;
  1753. int verdict = ip_vs_in_icmp_v6(ipvs, skb, &related,
  1754. hooknum, &iph);
  1755. if (related)
  1756. return verdict;
  1757. }
  1758. } else
  1759. #endif
  1760. if (unlikely(iph.protocol == IPPROTO_ICMP)) {
  1761. int related;
  1762. int verdict = ip_vs_in_icmp(ipvs, skb, &related,
  1763. hooknum);
  1764. if (related)
  1765. return verdict;
  1766. }
  1767. /* Protocol supported? */
  1768. pd = ip_vs_proto_data_get(ipvs, iph.protocol);
  1769. if (unlikely(!pd)) {
  1770. /* The only way we'll see this packet again is if it's
  1771. * encapsulated, so mark it with ipvs_property=1 so we
  1772. * skip it if we're ignoring tunneled packets
  1773. */
  1774. if (sysctl_ignore_tunneled(ipvs))
  1775. skb->ipvs_property = 1;
  1776. return NF_ACCEPT;
  1777. }
  1778. pp = pd->pp;
  1779. /*
  1780. * Check if the packet belongs to an existing connection entry
  1781. */
  1782. cp = INDIRECT_CALL_1(pp->conn_in_get, ip_vs_conn_in_get_proto,
  1783. ipvs, af, skb, &iph);
  1784. conn_reuse_mode = sysctl_conn_reuse_mode(ipvs);
  1785. if (conn_reuse_mode && !iph.fragoffs && is_new_conn(skb, &iph) && cp) {
  1786. bool uses_ct = false, resched = false;
  1787. if (unlikely(sysctl_expire_nodest_conn(ipvs)) && cp->dest &&
  1788. unlikely(!atomic_read(&cp->dest->weight))) {
  1789. resched = true;
  1790. uses_ct = ip_vs_conn_uses_conntrack(cp, skb);
  1791. } else if (is_new_conn_expected(cp, conn_reuse_mode)) {
  1792. uses_ct = ip_vs_conn_uses_conntrack(cp, skb);
  1793. if (!atomic_read(&cp->n_control)) {
  1794. resched = true;
  1795. } else {
  1796. /* Do not reschedule controlling connection
  1797. * that uses conntrack while it is still
  1798. * referenced by controlled connection(s).
  1799. */
  1800. resched = !uses_ct;
  1801. }
  1802. }
  1803. if (resched) {
  1804. if (!atomic_read(&cp->n_control))
  1805. ip_vs_conn_expire_now(cp);
  1806. __ip_vs_conn_put(cp);
  1807. if (uses_ct)
  1808. return NF_DROP;
  1809. cp = NULL;
  1810. }
  1811. }
  1812. if (unlikely(!cp)) {
  1813. int v;
  1814. if (!ip_vs_try_to_schedule(ipvs, af, skb, pd, &v, &cp, &iph))
  1815. return v;
  1816. }
  1817. IP_VS_DBG_PKT(11, af, pp, skb, iph.off, "Incoming packet");
  1818. /* Check the server status */
  1819. if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
  1820. /* the destination server is not available */
  1821. __u32 flags = cp->flags;
  1822. /* when timer already started, silently drop the packet.*/
  1823. if (timer_pending(&cp->timer))
  1824. __ip_vs_conn_put(cp);
  1825. else
  1826. ip_vs_conn_put(cp);
  1827. if (sysctl_expire_nodest_conn(ipvs) &&
  1828. !(flags & IP_VS_CONN_F_ONE_PACKET)) {
  1829. /* try to expire the connection immediately */
  1830. ip_vs_conn_expire_now(cp);
  1831. }
  1832. return NF_DROP;
  1833. }
  1834. ip_vs_in_stats(cp, skb);
  1835. ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
  1836. if (cp->packet_xmit)
  1837. ret = cp->packet_xmit(skb, cp, pp, &iph);
  1838. /* do not touch skb anymore */
  1839. else {
  1840. IP_VS_DBG_RL("warning: packet_xmit is null");
  1841. ret = NF_ACCEPT;
  1842. }
  1843. /* Increase its packet counter and check if it is needed
  1844. * to be synchronized
  1845. *
  1846. * Sync connection if it is about to close to
  1847. * encorage the standby servers to update the connections timeout
  1848. *
  1849. * For ONE_PKT let ip_vs_sync_conn() do the filter work.
  1850. */
  1851. if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
  1852. pkts = sysctl_sync_threshold(ipvs);
  1853. else
  1854. pkts = atomic_add_return(1, &cp->in_pkts);
  1855. if (ipvs->sync_state & IP_VS_STATE_MASTER)
  1856. ip_vs_sync_conn(ipvs, cp, pkts);
  1857. else if ((cp->flags & IP_VS_CONN_F_ONE_PACKET) && cp->control)
  1858. /* increment is done inside ip_vs_sync_conn too */
  1859. atomic_inc(&cp->control->in_pkts);
  1860. ip_vs_conn_put(cp);
  1861. return ret;
  1862. }
  1863. /*
  1864. * AF_INET handler in NF_INET_LOCAL_IN chain
  1865. * Schedule and forward packets from remote clients
  1866. */
  1867. static unsigned int
  1868. ip_vs_remote_request4(void *priv, struct sk_buff *skb,
  1869. const struct nf_hook_state *state)
  1870. {
  1871. return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET);
  1872. }
  1873. /*
  1874. * AF_INET handler in NF_INET_LOCAL_OUT chain
  1875. * Schedule and forward packets from local clients
  1876. */
  1877. static unsigned int
  1878. ip_vs_local_request4(void *priv, struct sk_buff *skb,
  1879. const struct nf_hook_state *state)
  1880. {
  1881. return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET);
  1882. }
  1883. #ifdef CONFIG_IP_VS_IPV6
  1884. /*
  1885. * AF_INET6 handler in NF_INET_LOCAL_IN chain
  1886. * Schedule and forward packets from remote clients
  1887. */
  1888. static unsigned int
  1889. ip_vs_remote_request6(void *priv, struct sk_buff *skb,
  1890. const struct nf_hook_state *state)
  1891. {
  1892. return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET6);
  1893. }
  1894. /*
  1895. * AF_INET6 handler in NF_INET_LOCAL_OUT chain
  1896. * Schedule and forward packets from local clients
  1897. */
  1898. static unsigned int
  1899. ip_vs_local_request6(void *priv, struct sk_buff *skb,
  1900. const struct nf_hook_state *state)
  1901. {
  1902. return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET6);
  1903. }
  1904. #endif
  1905. /*
  1906. * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
  1907. * related packets destined for 0.0.0.0/0.
  1908. * When fwmark-based virtual service is used, such as transparent
  1909. * cache cluster, TCP packets can be marked and routed to ip_vs_in,
  1910. * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
  1911. * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
  1912. * and send them to ip_vs_in_icmp.
  1913. */
  1914. static unsigned int
  1915. ip_vs_forward_icmp(void *priv, struct sk_buff *skb,
  1916. const struct nf_hook_state *state)
  1917. {
  1918. int r;
  1919. struct netns_ipvs *ipvs = net_ipvs(state->net);
  1920. if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
  1921. return NF_ACCEPT;
  1922. /* ipvs enabled in this netns ? */
  1923. if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
  1924. return NF_ACCEPT;
  1925. return ip_vs_in_icmp(ipvs, skb, &r, state->hook);
  1926. }
  1927. #ifdef CONFIG_IP_VS_IPV6
  1928. static unsigned int
  1929. ip_vs_forward_icmp_v6(void *priv, struct sk_buff *skb,
  1930. const struct nf_hook_state *state)
  1931. {
  1932. int r;
  1933. struct netns_ipvs *ipvs = net_ipvs(state->net);
  1934. struct ip_vs_iphdr iphdr;
  1935. ip_vs_fill_iph_skb(AF_INET6, skb, false, &iphdr);
  1936. if (iphdr.protocol != IPPROTO_ICMPV6)
  1937. return NF_ACCEPT;
  1938. /* ipvs enabled in this netns ? */
  1939. if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
  1940. return NF_ACCEPT;
  1941. return ip_vs_in_icmp_v6(ipvs, skb, &r, state->hook, &iphdr);
  1942. }
  1943. #endif
  1944. static const struct nf_hook_ops ip_vs_ops[] = {
  1945. /* After packet filtering, change source only for VS/NAT */
  1946. {
  1947. .hook = ip_vs_reply4,
  1948. .pf = NFPROTO_IPV4,
  1949. .hooknum = NF_INET_LOCAL_IN,
  1950. .priority = NF_IP_PRI_NAT_SRC - 2,
  1951. },
  1952. /* After packet filtering, forward packet through VS/DR, VS/TUN,
  1953. * or VS/NAT(change destination), so that filtering rules can be
  1954. * applied to IPVS. */
  1955. {
  1956. .hook = ip_vs_remote_request4,
  1957. .pf = NFPROTO_IPV4,
  1958. .hooknum = NF_INET_LOCAL_IN,
  1959. .priority = NF_IP_PRI_NAT_SRC - 1,
  1960. },
  1961. /* Before ip_vs_in, change source only for VS/NAT */
  1962. {
  1963. .hook = ip_vs_local_reply4,
  1964. .pf = NFPROTO_IPV4,
  1965. .hooknum = NF_INET_LOCAL_OUT,
  1966. .priority = NF_IP_PRI_NAT_DST + 1,
  1967. },
  1968. /* After mangle, schedule and forward local requests */
  1969. {
  1970. .hook = ip_vs_local_request4,
  1971. .pf = NFPROTO_IPV4,
  1972. .hooknum = NF_INET_LOCAL_OUT,
  1973. .priority = NF_IP_PRI_NAT_DST + 2,
  1974. },
  1975. /* After packet filtering (but before ip_vs_out_icmp), catch icmp
  1976. * destined for 0.0.0.0/0, which is for incoming IPVS connections */
  1977. {
  1978. .hook = ip_vs_forward_icmp,
  1979. .pf = NFPROTO_IPV4,
  1980. .hooknum = NF_INET_FORWARD,
  1981. .priority = 99,
  1982. },
  1983. /* After packet filtering, change source only for VS/NAT */
  1984. {
  1985. .hook = ip_vs_reply4,
  1986. .pf = NFPROTO_IPV4,
  1987. .hooknum = NF_INET_FORWARD,
  1988. .priority = 100,
  1989. },
  1990. #ifdef CONFIG_IP_VS_IPV6
  1991. /* After packet filtering, change source only for VS/NAT */
  1992. {
  1993. .hook = ip_vs_reply6,
  1994. .pf = NFPROTO_IPV6,
  1995. .hooknum = NF_INET_LOCAL_IN,
  1996. .priority = NF_IP6_PRI_NAT_SRC - 2,
  1997. },
  1998. /* After packet filtering, forward packet through VS/DR, VS/TUN,
  1999. * or VS/NAT(change destination), so that filtering rules can be
  2000. * applied to IPVS. */
  2001. {
  2002. .hook = ip_vs_remote_request6,
  2003. .pf = NFPROTO_IPV6,
  2004. .hooknum = NF_INET_LOCAL_IN,
  2005. .priority = NF_IP6_PRI_NAT_SRC - 1,
  2006. },
  2007. /* Before ip_vs_in, change source only for VS/NAT */
  2008. {
  2009. .hook = ip_vs_local_reply6,
  2010. .pf = NFPROTO_IPV6,
  2011. .hooknum = NF_INET_LOCAL_OUT,
  2012. .priority = NF_IP6_PRI_NAT_DST + 1,
  2013. },
  2014. /* After mangle, schedule and forward local requests */
  2015. {
  2016. .hook = ip_vs_local_request6,
  2017. .pf = NFPROTO_IPV6,
  2018. .hooknum = NF_INET_LOCAL_OUT,
  2019. .priority = NF_IP6_PRI_NAT_DST + 2,
  2020. },
  2021. /* After packet filtering (but before ip_vs_out_icmp), catch icmp
  2022. * destined for 0.0.0.0/0, which is for incoming IPVS connections */
  2023. {
  2024. .hook = ip_vs_forward_icmp_v6,
  2025. .pf = NFPROTO_IPV6,
  2026. .hooknum = NF_INET_FORWARD,
  2027. .priority = 99,
  2028. },
  2029. /* After packet filtering, change source only for VS/NAT */
  2030. {
  2031. .hook = ip_vs_reply6,
  2032. .pf = NFPROTO_IPV6,
  2033. .hooknum = NF_INET_FORWARD,
  2034. .priority = 100,
  2035. },
  2036. #endif
  2037. };
  2038. /*
  2039. * Initialize IP Virtual Server netns mem.
  2040. */
  2041. static int __net_init __ip_vs_init(struct net *net)
  2042. {
  2043. struct netns_ipvs *ipvs;
  2044. ipvs = net_generic(net, ip_vs_net_id);
  2045. if (ipvs == NULL)
  2046. return -ENOMEM;
  2047. /* Hold the beast until a service is registerd */
  2048. ipvs->enable = 0;
  2049. ipvs->net = net;
  2050. /* Counters used for creating unique names */
  2051. ipvs->gen = atomic_read(&ipvs_netns_cnt);
  2052. atomic_inc(&ipvs_netns_cnt);
  2053. net->ipvs = ipvs;
  2054. if (ip_vs_estimator_net_init(ipvs) < 0)
  2055. goto estimator_fail;
  2056. if (ip_vs_control_net_init(ipvs) < 0)
  2057. goto control_fail;
  2058. if (ip_vs_protocol_net_init(ipvs) < 0)
  2059. goto protocol_fail;
  2060. if (ip_vs_app_net_init(ipvs) < 0)
  2061. goto app_fail;
  2062. if (ip_vs_conn_net_init(ipvs) < 0)
  2063. goto conn_fail;
  2064. if (ip_vs_sync_net_init(ipvs) < 0)
  2065. goto sync_fail;
  2066. return 0;
  2067. /*
  2068. * Error handling
  2069. */
  2070. sync_fail:
  2071. ip_vs_conn_net_cleanup(ipvs);
  2072. conn_fail:
  2073. ip_vs_app_net_cleanup(ipvs);
  2074. app_fail:
  2075. ip_vs_protocol_net_cleanup(ipvs);
  2076. protocol_fail:
  2077. ip_vs_control_net_cleanup(ipvs);
  2078. control_fail:
  2079. ip_vs_estimator_net_cleanup(ipvs);
  2080. estimator_fail:
  2081. net->ipvs = NULL;
  2082. return -ENOMEM;
  2083. }
  2084. static void __net_exit __ip_vs_cleanup_batch(struct list_head *net_list)
  2085. {
  2086. struct netns_ipvs *ipvs;
  2087. struct net *net;
  2088. ip_vs_service_nets_cleanup(net_list); /* ip_vs_flush() with locks */
  2089. list_for_each_entry(net, net_list, exit_list) {
  2090. ipvs = net_ipvs(net);
  2091. ip_vs_conn_net_cleanup(ipvs);
  2092. ip_vs_app_net_cleanup(ipvs);
  2093. ip_vs_protocol_net_cleanup(ipvs);
  2094. ip_vs_control_net_cleanup(ipvs);
  2095. ip_vs_estimator_net_cleanup(ipvs);
  2096. IP_VS_DBG(2, "ipvs netns %d released\n", ipvs->gen);
  2097. net->ipvs = NULL;
  2098. }
  2099. }
  2100. static int __net_init __ip_vs_dev_init(struct net *net)
  2101. {
  2102. int ret;
  2103. ret = nf_register_net_hooks(net, ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
  2104. if (ret < 0)
  2105. goto hook_fail;
  2106. return 0;
  2107. hook_fail:
  2108. return ret;
  2109. }
  2110. static void __net_exit __ip_vs_dev_cleanup_batch(struct list_head *net_list)
  2111. {
  2112. struct netns_ipvs *ipvs;
  2113. struct net *net;
  2114. EnterFunction(2);
  2115. list_for_each_entry(net, net_list, exit_list) {
  2116. ipvs = net_ipvs(net);
  2117. nf_unregister_net_hooks(net, ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
  2118. ipvs->enable = 0; /* Disable packet reception */
  2119. smp_wmb();
  2120. ip_vs_sync_net_cleanup(ipvs);
  2121. }
  2122. LeaveFunction(2);
  2123. }
  2124. static struct pernet_operations ipvs_core_ops = {
  2125. .init = __ip_vs_init,
  2126. .exit_batch = __ip_vs_cleanup_batch,
  2127. .id = &ip_vs_net_id,
  2128. .size = sizeof(struct netns_ipvs),
  2129. };
  2130. static struct pernet_operations ipvs_core_dev_ops = {
  2131. .init = __ip_vs_dev_init,
  2132. .exit_batch = __ip_vs_dev_cleanup_batch,
  2133. };
  2134. /*
  2135. * Initialize IP Virtual Server
  2136. */
  2137. static int __init ip_vs_init(void)
  2138. {
  2139. int ret;
  2140. ret = ip_vs_control_init();
  2141. if (ret < 0) {
  2142. pr_err("can't setup control.\n");
  2143. goto exit;
  2144. }
  2145. ip_vs_protocol_init();
  2146. ret = ip_vs_conn_init();
  2147. if (ret < 0) {
  2148. pr_err("can't setup connection table.\n");
  2149. goto cleanup_protocol;
  2150. }
  2151. ret = register_pernet_subsys(&ipvs_core_ops); /* Alloc ip_vs struct */
  2152. if (ret < 0)
  2153. goto cleanup_conn;
  2154. ret = register_pernet_device(&ipvs_core_dev_ops);
  2155. if (ret < 0)
  2156. goto cleanup_sub;
  2157. ret = ip_vs_register_nl_ioctl();
  2158. if (ret < 0) {
  2159. pr_err("can't register netlink/ioctl.\n");
  2160. goto cleanup_dev;
  2161. }
  2162. pr_info("ipvs loaded.\n");
  2163. return ret;
  2164. cleanup_dev:
  2165. unregister_pernet_device(&ipvs_core_dev_ops);
  2166. cleanup_sub:
  2167. unregister_pernet_subsys(&ipvs_core_ops);
  2168. cleanup_conn:
  2169. ip_vs_conn_cleanup();
  2170. cleanup_protocol:
  2171. ip_vs_protocol_cleanup();
  2172. ip_vs_control_cleanup();
  2173. exit:
  2174. return ret;
  2175. }
  2176. static void __exit ip_vs_cleanup(void)
  2177. {
  2178. ip_vs_unregister_nl_ioctl();
  2179. unregister_pernet_device(&ipvs_core_dev_ops);
  2180. unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */
  2181. ip_vs_conn_cleanup();
  2182. ip_vs_protocol_cleanup();
  2183. ip_vs_control_cleanup();
  2184. pr_info("ipvs unloaded.\n");
  2185. }
  2186. module_init(ip_vs_init);
  2187. module_exit(ip_vs_cleanup);
  2188. MODULE_LICENSE("GPL");