/kern_oII/net/netfilter/ipvs/ip_vs_core.c

http://omnia2droid.googlecode.com/ · C · 1542 lines · 1032 code · 223 blank · 287 comment · 241 complexity · 04bb8ff4f70ef0cd45d3bd7dab88f92d MD5 · raw file

  1. /*
  2. * IPVS An implementation of the IP virtual server support for the
  3. * LINUX operating system. IPVS is now implemented as a module
  4. * over the Netfilter framework. IPVS can be used to build a
  5. * high-performance and highly available server based on a
  6. * cluster of servers.
  7. *
  8. * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
  9. * Peter Kese <peter.kese@ijs.si>
  10. * Julian Anastasov <ja@ssi.bg>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. *
  17. * The IPVS code for kernel 2.2 was done by Wensong Zhang and Peter Kese,
  18. * with changes/fixes from Julian Anastasov, Lars Marowsky-Bree, Horms
  19. * and others.
  20. *
  21. * Changes:
  22. * Paul `Rusty' Russell properly handle non-linear skbs
  23. * Harald Welte don't use nfcache
  24. *
  25. */
  26. #include <linux/module.h>
  27. #include <linux/kernel.h>
  28. #include <linux/ip.h>
  29. #include <linux/tcp.h>
  30. #include <linux/icmp.h>
  31. #include <net/ip.h>
  32. #include <net/tcp.h>
  33. #include <net/udp.h>
  34. #include <net/icmp.h> /* for icmp_send */
  35. #include <net/route.h>
  36. #include <linux/netfilter.h>
  37. #include <linux/netfilter_ipv4.h>
  38. #ifdef CONFIG_IP_VS_IPV6
  39. #include <net/ipv6.h>
  40. #include <linux/netfilter_ipv6.h>
  41. #endif
  42. #include <net/ip_vs.h>
  43. EXPORT_SYMBOL(register_ip_vs_scheduler);
  44. EXPORT_SYMBOL(unregister_ip_vs_scheduler);
  45. EXPORT_SYMBOL(ip_vs_skb_replace);
  46. EXPORT_SYMBOL(ip_vs_proto_name);
  47. EXPORT_SYMBOL(ip_vs_conn_new);
  48. EXPORT_SYMBOL(ip_vs_conn_in_get);
  49. EXPORT_SYMBOL(ip_vs_conn_out_get);
  50. #ifdef CONFIG_IP_VS_PROTO_TCP
  51. EXPORT_SYMBOL(ip_vs_tcp_conn_listen);
  52. #endif
  53. EXPORT_SYMBOL(ip_vs_conn_put);
  54. #ifdef CONFIG_IP_VS_DEBUG
  55. EXPORT_SYMBOL(ip_vs_get_debug_level);
  56. #endif
  57. /* ID used in ICMP lookups */
  58. #define icmp_id(icmph) (((icmph)->un).echo.id)
  59. #define icmpv6_id(icmph) (icmph->icmp6_dataun.u_echo.identifier)
  60. const char *ip_vs_proto_name(unsigned proto)
  61. {
  62. static char buf[20];
  63. switch (proto) {
  64. case IPPROTO_IP:
  65. return "IP";
  66. case IPPROTO_UDP:
  67. return "UDP";
  68. case IPPROTO_TCP:
  69. return "TCP";
  70. case IPPROTO_ICMP:
  71. return "ICMP";
  72. #ifdef CONFIG_IP_VS_IPV6
  73. case IPPROTO_ICMPV6:
  74. return "ICMPv6";
  75. #endif
  76. default:
  77. sprintf(buf, "IP_%d", proto);
  78. return buf;
  79. }
  80. }
  81. void ip_vs_init_hash_table(struct list_head *table, int rows)
  82. {
  83. while (--rows >= 0)
  84. INIT_LIST_HEAD(&table[rows]);
  85. }
  86. static inline void
  87. ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
  88. {
  89. struct ip_vs_dest *dest = cp->dest;
  90. if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
  91. spin_lock(&dest->stats.lock);
  92. dest->stats.ustats.inpkts++;
  93. dest->stats.ustats.inbytes += skb->len;
  94. spin_unlock(&dest->stats.lock);
  95. spin_lock(&dest->svc->stats.lock);
  96. dest->svc->stats.ustats.inpkts++;
  97. dest->svc->stats.ustats.inbytes += skb->len;
  98. spin_unlock(&dest->svc->stats.lock);
  99. spin_lock(&ip_vs_stats.lock);
  100. ip_vs_stats.ustats.inpkts++;
  101. ip_vs_stats.ustats.inbytes += skb->len;
  102. spin_unlock(&ip_vs_stats.lock);
  103. }
  104. }
  105. static inline void
  106. ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
  107. {
  108. struct ip_vs_dest *dest = cp->dest;
  109. if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
  110. spin_lock(&dest->stats.lock);
  111. dest->stats.ustats.outpkts++;
  112. dest->stats.ustats.outbytes += skb->len;
  113. spin_unlock(&dest->stats.lock);
  114. spin_lock(&dest->svc->stats.lock);
  115. dest->svc->stats.ustats.outpkts++;
  116. dest->svc->stats.ustats.outbytes += skb->len;
  117. spin_unlock(&dest->svc->stats.lock);
  118. spin_lock(&ip_vs_stats.lock);
  119. ip_vs_stats.ustats.outpkts++;
  120. ip_vs_stats.ustats.outbytes += skb->len;
  121. spin_unlock(&ip_vs_stats.lock);
  122. }
  123. }
  124. static inline void
  125. ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
  126. {
  127. spin_lock(&cp->dest->stats.lock);
  128. cp->dest->stats.ustats.conns++;
  129. spin_unlock(&cp->dest->stats.lock);
  130. spin_lock(&svc->stats.lock);
  131. svc->stats.ustats.conns++;
  132. spin_unlock(&svc->stats.lock);
  133. spin_lock(&ip_vs_stats.lock);
  134. ip_vs_stats.ustats.conns++;
  135. spin_unlock(&ip_vs_stats.lock);
  136. }
  137. static inline int
  138. ip_vs_set_state(struct ip_vs_conn *cp, int direction,
  139. const struct sk_buff *skb,
  140. struct ip_vs_protocol *pp)
  141. {
  142. if (unlikely(!pp->state_transition))
  143. return 0;
  144. return pp->state_transition(cp, direction, skb, pp);
  145. }
  146. /*
  147. * IPVS persistent scheduling function
  148. * It creates a connection entry according to its template if exists,
  149. * or selects a server and creates a connection entry plus a template.
  150. * Locking: we are svc user (svc->refcnt), so we hold all dests too
  151. * Protocols supported: TCP, UDP
  152. */
  153. static struct ip_vs_conn *
  154. ip_vs_sched_persist(struct ip_vs_service *svc,
  155. const struct sk_buff *skb,
  156. __be16 ports[2])
  157. {
  158. struct ip_vs_conn *cp = NULL;
  159. struct ip_vs_iphdr iph;
  160. struct ip_vs_dest *dest;
  161. struct ip_vs_conn *ct;
  162. __be16 dport; /* destination port to forward */
  163. union nf_inet_addr snet; /* source network of the client,
  164. after masking */
  165. ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
  166. /* Mask saddr with the netmask to adjust template granularity */
  167. #ifdef CONFIG_IP_VS_IPV6
  168. if (svc->af == AF_INET6)
  169. ipv6_addr_prefix(&snet.in6, &iph.saddr.in6, svc->netmask);
  170. else
  171. #endif
  172. snet.ip = iph.saddr.ip & svc->netmask;
  173. IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
  174. "mnet %s\n",
  175. IP_VS_DBG_ADDR(svc->af, &iph.saddr), ntohs(ports[0]),
  176. IP_VS_DBG_ADDR(svc->af, &iph.daddr), ntohs(ports[1]),
  177. IP_VS_DBG_ADDR(svc->af, &snet));
  178. /*
  179. * As far as we know, FTP is a very complicated network protocol, and
  180. * it uses control connection and data connections. For active FTP,
  181. * FTP server initialize data connection to the client, its source port
  182. * is often 20. For passive FTP, FTP server tells the clients the port
  183. * that it passively listens to, and the client issues the data
  184. * connection. In the tunneling or direct routing mode, the load
  185. * balancer is on the client-to-server half of connection, the port
  186. * number is unknown to the load balancer. So, a conn template like
  187. * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
  188. * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
  189. * is created for other persistent services.
  190. */
  191. if (ports[1] == svc->port) {
  192. /* Check if a template already exists */
  193. if (svc->port != FTPPORT)
  194. ct = ip_vs_ct_in_get(svc->af, iph.protocol, &snet, 0,
  195. &iph.daddr, ports[1]);
  196. else
  197. ct = ip_vs_ct_in_get(svc->af, iph.protocol, &snet, 0,
  198. &iph.daddr, 0);
  199. if (!ct || !ip_vs_check_template(ct)) {
  200. /*
  201. * No template found or the dest of the connection
  202. * template is not available.
  203. */
  204. dest = svc->scheduler->schedule(svc, skb);
  205. if (dest == NULL) {
  206. IP_VS_DBG(1, "p-schedule: no dest found.\n");
  207. return NULL;
  208. }
  209. /*
  210. * Create a template like <protocol,caddr,0,
  211. * vaddr,vport,daddr,dport> for non-ftp service,
  212. * and <protocol,caddr,0,vaddr,0,daddr,0>
  213. * for ftp service.
  214. */
  215. if (svc->port != FTPPORT)
  216. ct = ip_vs_conn_new(svc->af, iph.protocol,
  217. &snet, 0,
  218. &iph.daddr,
  219. ports[1],
  220. &dest->addr, dest->port,
  221. IP_VS_CONN_F_TEMPLATE,
  222. dest);
  223. else
  224. ct = ip_vs_conn_new(svc->af, iph.protocol,
  225. &snet, 0,
  226. &iph.daddr, 0,
  227. &dest->addr, 0,
  228. IP_VS_CONN_F_TEMPLATE,
  229. dest);
  230. if (ct == NULL)
  231. return NULL;
  232. ct->timeout = svc->timeout;
  233. } else {
  234. /* set destination with the found template */
  235. dest = ct->dest;
  236. }
  237. dport = dest->port;
  238. } else {
  239. /*
  240. * Note: persistent fwmark-based services and persistent
  241. * port zero service are handled here.
  242. * fwmark template: <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
  243. * port zero template: <protocol,caddr,0,vaddr,0,daddr,0>
  244. */
  245. if (svc->fwmark) {
  246. union nf_inet_addr fwmark = {
  247. .ip = htonl(svc->fwmark)
  248. };
  249. ct = ip_vs_ct_in_get(svc->af, IPPROTO_IP, &snet, 0,
  250. &fwmark, 0);
  251. } else
  252. ct = ip_vs_ct_in_get(svc->af, iph.protocol, &snet, 0,
  253. &iph.daddr, 0);
  254. if (!ct || !ip_vs_check_template(ct)) {
  255. /*
  256. * If it is not persistent port zero, return NULL,
  257. * otherwise create a connection template.
  258. */
  259. if (svc->port)
  260. return NULL;
  261. dest = svc->scheduler->schedule(svc, skb);
  262. if (dest == NULL) {
  263. IP_VS_DBG(1, "p-schedule: no dest found.\n");
  264. return NULL;
  265. }
  266. /*
  267. * Create a template according to the service
  268. */
  269. if (svc->fwmark) {
  270. union nf_inet_addr fwmark = {
  271. .ip = htonl(svc->fwmark)
  272. };
  273. ct = ip_vs_conn_new(svc->af, IPPROTO_IP,
  274. &snet, 0,
  275. &fwmark, 0,
  276. &dest->addr, 0,
  277. IP_VS_CONN_F_TEMPLATE,
  278. dest);
  279. } else
  280. ct = ip_vs_conn_new(svc->af, iph.protocol,
  281. &snet, 0,
  282. &iph.daddr, 0,
  283. &dest->addr, 0,
  284. IP_VS_CONN_F_TEMPLATE,
  285. dest);
  286. if (ct == NULL)
  287. return NULL;
  288. ct->timeout = svc->timeout;
  289. } else {
  290. /* set destination with the found template */
  291. dest = ct->dest;
  292. }
  293. dport = ports[1];
  294. }
  295. /*
  296. * Create a new connection according to the template
  297. */
  298. cp = ip_vs_conn_new(svc->af, iph.protocol,
  299. &iph.saddr, ports[0],
  300. &iph.daddr, ports[1],
  301. &dest->addr, dport,
  302. 0,
  303. dest);
  304. if (cp == NULL) {
  305. ip_vs_conn_put(ct);
  306. return NULL;
  307. }
  308. /*
  309. * Add its control
  310. */
  311. ip_vs_control_add(cp, ct);
  312. ip_vs_conn_put(ct);
  313. ip_vs_conn_stats(cp, svc);
  314. return cp;
  315. }
  316. /*
  317. * IPVS main scheduling function
  318. * It selects a server according to the virtual service, and
  319. * creates a connection entry.
  320. * Protocols supported: TCP, UDP
  321. */
  322. struct ip_vs_conn *
  323. ip_vs_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
  324. {
  325. struct ip_vs_conn *cp = NULL;
  326. struct ip_vs_iphdr iph;
  327. struct ip_vs_dest *dest;
  328. __be16 _ports[2], *pptr;
  329. ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
  330. pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports);
  331. if (pptr == NULL)
  332. return NULL;
  333. /*
  334. * Persistent service
  335. */
  336. if (svc->flags & IP_VS_SVC_F_PERSISTENT)
  337. return ip_vs_sched_persist(svc, skb, pptr);
  338. /*
  339. * Non-persistent service
  340. */
  341. if (!svc->fwmark && pptr[1] != svc->port) {
  342. if (!svc->port)
  343. IP_VS_ERR("Schedule: port zero only supported "
  344. "in persistent services, "
  345. "check your ipvs configuration\n");
  346. return NULL;
  347. }
  348. dest = svc->scheduler->schedule(svc, skb);
  349. if (dest == NULL) {
  350. IP_VS_DBG(1, "Schedule: no dest found.\n");
  351. return NULL;
  352. }
  353. /*
  354. * Create a connection entry.
  355. */
  356. cp = ip_vs_conn_new(svc->af, iph.protocol,
  357. &iph.saddr, pptr[0],
  358. &iph.daddr, pptr[1],
  359. &dest->addr, dest->port ? dest->port : pptr[1],
  360. 0,
  361. dest);
  362. if (cp == NULL)
  363. return NULL;
  364. IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
  365. "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
  366. ip_vs_fwd_tag(cp),
  367. IP_VS_DBG_ADDR(svc->af, &cp->caddr), ntohs(cp->cport),
  368. IP_VS_DBG_ADDR(svc->af, &cp->vaddr), ntohs(cp->vport),
  369. IP_VS_DBG_ADDR(svc->af, &cp->daddr), ntohs(cp->dport),
  370. cp->flags, atomic_read(&cp->refcnt));
  371. ip_vs_conn_stats(cp, svc);
  372. return cp;
  373. }
  374. /*
  375. * Pass or drop the packet.
  376. * Called by ip_vs_in, when the virtual service is available but
  377. * no destination is available for a new connection.
  378. */
  379. int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
  380. struct ip_vs_protocol *pp)
  381. {
  382. __be16 _ports[2], *pptr;
  383. struct ip_vs_iphdr iph;
  384. int unicast;
  385. ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
  386. pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports);
  387. if (pptr == NULL) {
  388. ip_vs_service_put(svc);
  389. return NF_DROP;
  390. }
  391. #ifdef CONFIG_IP_VS_IPV6
  392. if (svc->af == AF_INET6)
  393. unicast = ipv6_addr_type(&iph.daddr.in6) & IPV6_ADDR_UNICAST;
  394. else
  395. #endif
  396. unicast = (inet_addr_type(&init_net, iph.daddr.ip) == RTN_UNICAST);
  397. /* if it is fwmark-based service, the cache_bypass sysctl is up
  398. and the destination is a non-local unicast, then create
  399. a cache_bypass connection entry */
  400. if (sysctl_ip_vs_cache_bypass && svc->fwmark && unicast) {
  401. int ret, cs;
  402. struct ip_vs_conn *cp;
  403. union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } };
  404. ip_vs_service_put(svc);
  405. /* create a new connection entry */
  406. IP_VS_DBG(6, "ip_vs_leave: create a cache_bypass entry\n");
  407. cp = ip_vs_conn_new(svc->af, iph.protocol,
  408. &iph.saddr, pptr[0],
  409. &iph.daddr, pptr[1],
  410. &daddr, 0,
  411. IP_VS_CONN_F_BYPASS,
  412. NULL);
  413. if (cp == NULL)
  414. return NF_DROP;
  415. /* statistics */
  416. ip_vs_in_stats(cp, skb);
  417. /* set state */
  418. cs = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp);
  419. /* transmit the first SYN packet */
  420. ret = cp->packet_xmit(skb, cp, pp);
  421. /* do not touch skb anymore */
  422. atomic_inc(&cp->in_pkts);
  423. ip_vs_conn_put(cp);
  424. return ret;
  425. }
  426. /*
  427. * When the virtual ftp service is presented, packets destined
  428. * for other services on the VIP may get here (except services
  429. * listed in the ipvs table), pass the packets, because it is
  430. * not ipvs job to decide to drop the packets.
  431. */
  432. if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT)) {
  433. ip_vs_service_put(svc);
  434. return NF_ACCEPT;
  435. }
  436. ip_vs_service_put(svc);
  437. /*
  438. * Notify the client that the destination is unreachable, and
  439. * release the socket buffer.
  440. * Since it is in IP layer, the TCP socket is not actually
  441. * created, the TCP RST packet cannot be sent, instead that
  442. * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
  443. */
  444. #ifdef CONFIG_IP_VS_IPV6
  445. if (svc->af == AF_INET6)
  446. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0,
  447. skb->dev);
  448. else
  449. #endif
  450. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  451. return NF_DROP;
  452. }
  453. /*
  454. * It is hooked before NF_IP_PRI_NAT_SRC at the NF_INET_POST_ROUTING
  455. * chain, and is used for VS/NAT.
  456. * It detects packets for VS/NAT connections and sends the packets
  457. * immediately. This can avoid that iptable_nat mangles the packets
  458. * for VS/NAT.
  459. */
  460. static unsigned int ip_vs_post_routing(unsigned int hooknum,
  461. struct sk_buff *skb,
  462. const struct net_device *in,
  463. const struct net_device *out,
  464. int (*okfn)(struct sk_buff *))
  465. {
  466. if (!skb->ipvs_property)
  467. return NF_ACCEPT;
  468. /* The packet was sent from IPVS, exit this chain */
  469. return NF_STOP;
  470. }
  471. __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
  472. {
  473. return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
  474. }
  475. static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
  476. {
  477. int err = ip_defrag(skb, user);
  478. if (!err)
  479. ip_send_check(ip_hdr(skb));
  480. return err;
  481. }
  482. #ifdef CONFIG_IP_VS_IPV6
  483. static inline int ip_vs_gather_frags_v6(struct sk_buff *skb, u_int32_t user)
  484. {
  485. /* TODO IPv6: Find out what to do here for IPv6 */
  486. return 0;
  487. }
  488. #endif
  489. /*
  490. * Packet has been made sufficiently writable in caller
  491. * - inout: 1=in->out, 0=out->in
  492. */
  493. void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
  494. struct ip_vs_conn *cp, int inout)
  495. {
  496. struct iphdr *iph = ip_hdr(skb);
  497. unsigned int icmp_offset = iph->ihl*4;
  498. struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
  499. icmp_offset);
  500. struct iphdr *ciph = (struct iphdr *)(icmph + 1);
  501. if (inout) {
  502. iph->saddr = cp->vaddr.ip;
  503. ip_send_check(iph);
  504. ciph->daddr = cp->vaddr.ip;
  505. ip_send_check(ciph);
  506. } else {
  507. iph->daddr = cp->daddr.ip;
  508. ip_send_check(iph);
  509. ciph->saddr = cp->daddr.ip;
  510. ip_send_check(ciph);
  511. }
  512. /* the TCP/UDP port */
  513. if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol) {
  514. __be16 *ports = (void *)ciph + ciph->ihl*4;
  515. if (inout)
  516. ports[1] = cp->vport;
  517. else
  518. ports[0] = cp->dport;
  519. }
  520. /* And finally the ICMP checksum */
  521. icmph->checksum = 0;
  522. icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
  523. skb->ip_summed = CHECKSUM_UNNECESSARY;
  524. if (inout)
  525. IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
  526. "Forwarding altered outgoing ICMP");
  527. else
  528. IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
  529. "Forwarding altered incoming ICMP");
  530. }
  531. #ifdef CONFIG_IP_VS_IPV6
  532. void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
  533. struct ip_vs_conn *cp, int inout)
  534. {
  535. struct ipv6hdr *iph = ipv6_hdr(skb);
  536. unsigned int icmp_offset = sizeof(struct ipv6hdr);
  537. struct icmp6hdr *icmph = (struct icmp6hdr *)(skb_network_header(skb) +
  538. icmp_offset);
  539. struct ipv6hdr *ciph = (struct ipv6hdr *)(icmph + 1);
  540. if (inout) {
  541. iph->saddr = cp->vaddr.in6;
  542. ciph->daddr = cp->vaddr.in6;
  543. } else {
  544. iph->daddr = cp->daddr.in6;
  545. ciph->saddr = cp->daddr.in6;
  546. }
  547. /* the TCP/UDP port */
  548. if (IPPROTO_TCP == ciph->nexthdr || IPPROTO_UDP == ciph->nexthdr) {
  549. __be16 *ports = (void *)ciph + sizeof(struct ipv6hdr);
  550. if (inout)
  551. ports[1] = cp->vport;
  552. else
  553. ports[0] = cp->dport;
  554. }
  555. /* And finally the ICMP checksum */
  556. icmph->icmp6_cksum = 0;
  557. /* TODO IPv6: is this correct for ICMPv6? */
  558. ip_vs_checksum_complete(skb, icmp_offset);
  559. skb->ip_summed = CHECKSUM_UNNECESSARY;
  560. if (inout)
  561. IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
  562. "Forwarding altered outgoing ICMPv6");
  563. else
  564. IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
  565. "Forwarding altered incoming ICMPv6");
  566. }
  567. #endif
  568. /* Handle relevant response ICMP messages - forward to the right
  569. * destination host. Used for NAT and local client.
  570. */
  571. static int handle_response_icmp(int af, struct sk_buff *skb,
  572. union nf_inet_addr *snet,
  573. __u8 protocol, struct ip_vs_conn *cp,
  574. struct ip_vs_protocol *pp,
  575. unsigned int offset, unsigned int ihl)
  576. {
  577. unsigned int verdict = NF_DROP;
  578. if (IP_VS_FWD_METHOD(cp) != 0) {
  579. IP_VS_ERR("shouldn't reach here, because the box is on the "
  580. "half connection in the tun/dr module.\n");
  581. }
  582. /* Ensure the checksum is correct */
  583. if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
  584. /* Failed checksum! */
  585. IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
  586. IP_VS_DBG_ADDR(af, snet));
  587. goto out;
  588. }
  589. if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol)
  590. offset += 2 * sizeof(__u16);
  591. if (!skb_make_writable(skb, offset))
  592. goto out;
  593. #ifdef CONFIG_IP_VS_IPV6
  594. if (af == AF_INET6)
  595. ip_vs_nat_icmp_v6(skb, pp, cp, 1);
  596. else
  597. #endif
  598. ip_vs_nat_icmp(skb, pp, cp, 1);
  599. /* do the statistics and put it back */
  600. ip_vs_out_stats(cp, skb);
  601. skb->ipvs_property = 1;
  602. verdict = NF_ACCEPT;
  603. out:
  604. __ip_vs_conn_put(cp);
  605. return verdict;
  606. }
  607. /*
  608. * Handle ICMP messages in the inside-to-outside direction (outgoing).
  609. * Find any that might be relevant, check against existing connections.
  610. * Currently handles error types - unreachable, quench, ttl exceeded.
  611. */
  612. static int ip_vs_out_icmp(struct sk_buff *skb, int *related)
  613. {
  614. struct iphdr *iph;
  615. struct icmphdr _icmph, *ic;
  616. struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
  617. struct ip_vs_iphdr ciph;
  618. struct ip_vs_conn *cp;
  619. struct ip_vs_protocol *pp;
  620. unsigned int offset, ihl;
  621. union nf_inet_addr snet;
  622. *related = 1;
  623. /* reassemble IP fragments */
  624. if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
  625. if (ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT))
  626. return NF_STOLEN;
  627. }
  628. iph = ip_hdr(skb);
  629. offset = ihl = iph->ihl * 4;
  630. ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
  631. if (ic == NULL)
  632. return NF_DROP;
  633. IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
  634. ic->type, ntohs(icmp_id(ic)),
  635. &iph->saddr, &iph->daddr);
  636. /*
  637. * Work through seeing if this is for us.
  638. * These checks are supposed to be in an order that means easy
  639. * things are checked first to speed up processing.... however
  640. * this means that some packets will manage to get a long way
  641. * down this stack and then be rejected, but that's life.
  642. */
  643. if ((ic->type != ICMP_DEST_UNREACH) &&
  644. (ic->type != ICMP_SOURCE_QUENCH) &&
  645. (ic->type != ICMP_TIME_EXCEEDED)) {
  646. *related = 0;
  647. return NF_ACCEPT;
  648. }
  649. /* Now find the contained IP header */
  650. offset += sizeof(_icmph);
  651. cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
  652. if (cih == NULL)
  653. return NF_ACCEPT; /* The packet looks wrong, ignore */
  654. pp = ip_vs_proto_get(cih->protocol);
  655. if (!pp)
  656. return NF_ACCEPT;
  657. /* Is the embedded protocol header present? */
  658. if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
  659. pp->dont_defrag))
  660. return NF_ACCEPT;
  661. IP_VS_DBG_PKT(11, pp, skb, offset, "Checking outgoing ICMP for");
  662. offset += cih->ihl * 4;
  663. ip_vs_fill_iphdr(AF_INET, cih, &ciph);
  664. /* The embedded headers contain source and dest in reverse order */
  665. cp = pp->conn_out_get(AF_INET, skb, pp, &ciph, offset, 1);
  666. if (!cp)
  667. return NF_ACCEPT;
  668. snet.ip = iph->saddr;
  669. return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
  670. pp, offset, ihl);
  671. }
  672. #ifdef CONFIG_IP_VS_IPV6
  673. static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related)
  674. {
  675. struct ipv6hdr *iph;
  676. struct icmp6hdr _icmph, *ic;
  677. struct ipv6hdr _ciph, *cih; /* The ip header contained
  678. within the ICMP */
  679. struct ip_vs_iphdr ciph;
  680. struct ip_vs_conn *cp;
  681. struct ip_vs_protocol *pp;
  682. unsigned int offset;
  683. union nf_inet_addr snet;
  684. *related = 1;
  685. /* reassemble IP fragments */
  686. if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
  687. if (ip_vs_gather_frags_v6(skb, IP_DEFRAG_VS_OUT))
  688. return NF_STOLEN;
  689. }
  690. iph = ipv6_hdr(skb);
  691. offset = sizeof(struct ipv6hdr);
  692. ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
  693. if (ic == NULL)
  694. return NF_DROP;
  695. IP_VS_DBG(12, "Outgoing ICMPv6 (%d,%d) %pI6->%pI6\n",
  696. ic->icmp6_type, ntohs(icmpv6_id(ic)),
  697. &iph->saddr, &iph->daddr);
  698. /*
  699. * Work through seeing if this is for us.
  700. * These checks are supposed to be in an order that means easy
  701. * things are checked first to speed up processing.... however
  702. * this means that some packets will manage to get a long way
  703. * down this stack and then be rejected, but that's life.
  704. */
  705. if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
  706. (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
  707. (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
  708. *related = 0;
  709. return NF_ACCEPT;
  710. }
  711. /* Now find the contained IP header */
  712. offset += sizeof(_icmph);
  713. cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
  714. if (cih == NULL)
  715. return NF_ACCEPT; /* The packet looks wrong, ignore */
  716. pp = ip_vs_proto_get(cih->nexthdr);
  717. if (!pp)
  718. return NF_ACCEPT;
  719. /* Is the embedded protocol header present? */
  720. /* TODO: we don't support fragmentation at the moment anyways */
  721. if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
  722. return NF_ACCEPT;
  723. IP_VS_DBG_PKT(11, pp, skb, offset, "Checking outgoing ICMPv6 for");
  724. offset += sizeof(struct ipv6hdr);
  725. ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
  726. /* The embedded headers contain source and dest in reverse order */
  727. cp = pp->conn_out_get(AF_INET6, skb, pp, &ciph, offset, 1);
  728. if (!cp)
  729. return NF_ACCEPT;
  730. ipv6_addr_copy(&snet.in6, &iph->saddr);
  731. return handle_response_icmp(AF_INET6, skb, &snet, cih->nexthdr, cp,
  732. pp, offset, sizeof(struct ipv6hdr));
  733. }
  734. #endif
  735. static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
  736. {
  737. struct tcphdr _tcph, *th;
  738. th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
  739. if (th == NULL)
  740. return 0;
  741. return th->rst;
  742. }
  743. /* Handle response packets: rewrite addresses and send away...
  744. * Used for NAT and local client.
  745. */
  746. static unsigned int
  747. handle_response(int af, struct sk_buff *skb, struct ip_vs_protocol *pp,
  748. struct ip_vs_conn *cp, int ihl)
  749. {
  750. IP_VS_DBG_PKT(11, pp, skb, 0, "Outgoing packet");
  751. if (!skb_make_writable(skb, ihl))
  752. goto drop;
  753. /* mangle the packet */
  754. if (pp->snat_handler && !pp->snat_handler(skb, pp, cp))
  755. goto drop;
  756. #ifdef CONFIG_IP_VS_IPV6
  757. if (af == AF_INET6)
  758. ipv6_hdr(skb)->saddr = cp->vaddr.in6;
  759. else
  760. #endif
  761. {
  762. ip_hdr(skb)->saddr = cp->vaddr.ip;
  763. ip_send_check(ip_hdr(skb));
  764. }
  765. /* For policy routing, packets originating from this
  766. * machine itself may be routed differently to packets
  767. * passing through. We want this packet to be routed as
  768. * if it came from this machine itself. So re-compute
  769. * the routing information.
  770. */
  771. #ifdef CONFIG_IP_VS_IPV6
  772. if (af == AF_INET6) {
  773. if (ip6_route_me_harder(skb) != 0)
  774. goto drop;
  775. } else
  776. #endif
  777. if (ip_route_me_harder(skb, RTN_LOCAL) != 0)
  778. goto drop;
  779. IP_VS_DBG_PKT(10, pp, skb, 0, "After SNAT");
  780. ip_vs_out_stats(cp, skb);
  781. ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pp);
  782. ip_vs_conn_put(cp);
  783. skb->ipvs_property = 1;
  784. LeaveFunction(11);
  785. return NF_ACCEPT;
  786. drop:
  787. ip_vs_conn_put(cp);
  788. kfree_skb(skb);
  789. return NF_STOLEN;
  790. }
  791. /*
  792. * It is hooked at the NF_INET_FORWARD chain, used only for VS/NAT.
  793. * Check if outgoing packet belongs to the established ip_vs_conn.
  794. */
  795. static unsigned int
  796. ip_vs_out(unsigned int hooknum, struct sk_buff *skb,
  797. const struct net_device *in, const struct net_device *out,
  798. int (*okfn)(struct sk_buff *))
  799. {
  800. struct ip_vs_iphdr iph;
  801. struct ip_vs_protocol *pp;
  802. struct ip_vs_conn *cp;
  803. int af;
  804. EnterFunction(11);
  805. af = (skb->protocol == htons(ETH_P_IP)) ? AF_INET : AF_INET6;
  806. if (skb->ipvs_property)
  807. return NF_ACCEPT;
  808. ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
  809. #ifdef CONFIG_IP_VS_IPV6
  810. if (af == AF_INET6) {
  811. if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
  812. int related, verdict = ip_vs_out_icmp_v6(skb, &related);
  813. if (related)
  814. return verdict;
  815. ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
  816. }
  817. } else
  818. #endif
  819. if (unlikely(iph.protocol == IPPROTO_ICMP)) {
  820. int related, verdict = ip_vs_out_icmp(skb, &related);
  821. if (related)
  822. return verdict;
  823. ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
  824. }
  825. pp = ip_vs_proto_get(iph.protocol);
  826. if (unlikely(!pp))
  827. return NF_ACCEPT;
  828. /* reassemble IP fragments */
  829. #ifdef CONFIG_IP_VS_IPV6
  830. if (af == AF_INET6) {
  831. if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
  832. int related, verdict = ip_vs_out_icmp_v6(skb, &related);
  833. if (related)
  834. return verdict;
  835. ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
  836. }
  837. } else
  838. #endif
  839. if (unlikely(ip_hdr(skb)->frag_off & htons(IP_MF|IP_OFFSET) &&
  840. !pp->dont_defrag)) {
  841. if (ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT))
  842. return NF_STOLEN;
  843. ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
  844. }
  845. /*
  846. * Check if the packet belongs to an existing entry
  847. */
  848. cp = pp->conn_out_get(af, skb, pp, &iph, iph.len, 0);
  849. if (unlikely(!cp)) {
  850. if (sysctl_ip_vs_nat_icmp_send &&
  851. (pp->protocol == IPPROTO_TCP ||
  852. pp->protocol == IPPROTO_UDP)) {
  853. __be16 _ports[2], *pptr;
  854. pptr = skb_header_pointer(skb, iph.len,
  855. sizeof(_ports), _ports);
  856. if (pptr == NULL)
  857. return NF_ACCEPT; /* Not for me */
  858. if (ip_vs_lookup_real_service(af, iph.protocol,
  859. &iph.saddr,
  860. pptr[0])) {
  861. /*
  862. * Notify the real server: there is no
  863. * existing entry if it is not RST
  864. * packet or not TCP packet.
  865. */
  866. if (iph.protocol != IPPROTO_TCP
  867. || !is_tcp_reset(skb, iph.len)) {
  868. #ifdef CONFIG_IP_VS_IPV6
  869. if (af == AF_INET6)
  870. icmpv6_send(skb,
  871. ICMPV6_DEST_UNREACH,
  872. ICMPV6_PORT_UNREACH,
  873. 0, skb->dev);
  874. else
  875. #endif
  876. icmp_send(skb,
  877. ICMP_DEST_UNREACH,
  878. ICMP_PORT_UNREACH, 0);
  879. return NF_DROP;
  880. }
  881. }
  882. }
  883. IP_VS_DBG_PKT(12, pp, skb, 0,
  884. "packet continues traversal as normal");
  885. return NF_ACCEPT;
  886. }
  887. return handle_response(af, skb, pp, cp, iph.len);
  888. }
  889. /*
  890. * Handle ICMP messages in the outside-to-inside direction (incoming).
  891. * Find any that might be relevant, check against existing connections,
  892. * forward to the right destination host if relevant.
  893. * Currently handles error types - unreachable, quench, ttl exceeded.
  894. */
  895. static int
  896. ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
  897. {
  898. struct iphdr *iph;
  899. struct icmphdr _icmph, *ic;
  900. struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
  901. struct ip_vs_iphdr ciph;
  902. struct ip_vs_conn *cp;
  903. struct ip_vs_protocol *pp;
  904. unsigned int offset, ihl, verdict;
  905. union nf_inet_addr snet;
  906. *related = 1;
  907. /* reassemble IP fragments */
  908. if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
  909. if (ip_vs_gather_frags(skb, hooknum == NF_INET_LOCAL_IN ?
  910. IP_DEFRAG_VS_IN : IP_DEFRAG_VS_FWD))
  911. return NF_STOLEN;
  912. }
  913. iph = ip_hdr(skb);
  914. offset = ihl = iph->ihl * 4;
  915. ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
  916. if (ic == NULL)
  917. return NF_DROP;
  918. IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
  919. ic->type, ntohs(icmp_id(ic)),
  920. &iph->saddr, &iph->daddr);
  921. /*
  922. * Work through seeing if this is for us.
  923. * These checks are supposed to be in an order that means easy
  924. * things are checked first to speed up processing.... however
  925. * this means that some packets will manage to get a long way
  926. * down this stack and then be rejected, but that's life.
  927. */
  928. if ((ic->type != ICMP_DEST_UNREACH) &&
  929. (ic->type != ICMP_SOURCE_QUENCH) &&
  930. (ic->type != ICMP_TIME_EXCEEDED)) {
  931. *related = 0;
  932. return NF_ACCEPT;
  933. }
  934. /* Now find the contained IP header */
  935. offset += sizeof(_icmph);
  936. cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
  937. if (cih == NULL)
  938. return NF_ACCEPT; /* The packet looks wrong, ignore */
  939. pp = ip_vs_proto_get(cih->protocol);
  940. if (!pp)
  941. return NF_ACCEPT;
  942. /* Is the embedded protocol header present? */
  943. if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
  944. pp->dont_defrag))
  945. return NF_ACCEPT;
  946. IP_VS_DBG_PKT(11, pp, skb, offset, "Checking incoming ICMP for");
  947. offset += cih->ihl * 4;
  948. ip_vs_fill_iphdr(AF_INET, cih, &ciph);
  949. /* The embedded headers contain source and dest in reverse order */
  950. cp = pp->conn_in_get(AF_INET, skb, pp, &ciph, offset, 1);
  951. if (!cp) {
  952. /* The packet could also belong to a local client */
  953. cp = pp->conn_out_get(AF_INET, skb, pp, &ciph, offset, 1);
  954. if (cp) {
  955. snet.ip = iph->saddr;
  956. return handle_response_icmp(AF_INET, skb, &snet,
  957. cih->protocol, cp, pp,
  958. offset, ihl);
  959. }
  960. return NF_ACCEPT;
  961. }
  962. verdict = NF_DROP;
  963. /* Ensure the checksum is correct */
  964. if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
  965. /* Failed checksum! */
  966. IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
  967. &iph->saddr);
  968. goto out;
  969. }
  970. /* do the statistics and put it back */
  971. ip_vs_in_stats(cp, skb);
  972. if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
  973. offset += 2 * sizeof(__u16);
  974. verdict = ip_vs_icmp_xmit(skb, cp, pp, offset);
  975. /* do not touch skb anymore */
  976. out:
  977. __ip_vs_conn_put(cp);
  978. return verdict;
  979. }
  980. #ifdef CONFIG_IP_VS_IPV6
  981. static int
  982. ip_vs_in_icmp_v6(struct sk_buff *skb, int *related, unsigned int hooknum)
  983. {
  984. struct ipv6hdr *iph;
  985. struct icmp6hdr _icmph, *ic;
  986. struct ipv6hdr _ciph, *cih; /* The ip header contained
  987. within the ICMP */
  988. struct ip_vs_iphdr ciph;
  989. struct ip_vs_conn *cp;
  990. struct ip_vs_protocol *pp;
  991. unsigned int offset, verdict;
  992. union nf_inet_addr snet;
  993. *related = 1;
  994. /* reassemble IP fragments */
  995. if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
  996. if (ip_vs_gather_frags_v6(skb, hooknum == NF_INET_LOCAL_IN ?
  997. IP_DEFRAG_VS_IN :
  998. IP_DEFRAG_VS_FWD))
  999. return NF_STOLEN;
  1000. }
  1001. iph = ipv6_hdr(skb);
  1002. offset = sizeof(struct ipv6hdr);
  1003. ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
  1004. if (ic == NULL)
  1005. return NF_DROP;
  1006. IP_VS_DBG(12, "Incoming ICMPv6 (%d,%d) %pI6->%pI6\n",
  1007. ic->icmp6_type, ntohs(icmpv6_id(ic)),
  1008. &iph->saddr, &iph->daddr);
  1009. /*
  1010. * Work through seeing if this is for us.
  1011. * These checks are supposed to be in an order that means easy
  1012. * things are checked first to speed up processing.... however
  1013. * this means that some packets will manage to get a long way
  1014. * down this stack and then be rejected, but that's life.
  1015. */
  1016. if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
  1017. (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
  1018. (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
  1019. *related = 0;
  1020. return NF_ACCEPT;
  1021. }
  1022. /* Now find the contained IP header */
  1023. offset += sizeof(_icmph);
  1024. cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
  1025. if (cih == NULL)
  1026. return NF_ACCEPT; /* The packet looks wrong, ignore */
  1027. pp = ip_vs_proto_get(cih->nexthdr);
  1028. if (!pp)
  1029. return NF_ACCEPT;
  1030. /* Is the embedded protocol header present? */
  1031. /* TODO: we don't support fragmentation at the moment anyways */
  1032. if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
  1033. return NF_ACCEPT;
  1034. IP_VS_DBG_PKT(11, pp, skb, offset, "Checking incoming ICMPv6 for");
  1035. offset += sizeof(struct ipv6hdr);
  1036. ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
  1037. /* The embedded headers contain source and dest in reverse order */
  1038. cp = pp->conn_in_get(AF_INET6, skb, pp, &ciph, offset, 1);
  1039. if (!cp) {
  1040. /* The packet could also belong to a local client */
  1041. cp = pp->conn_out_get(AF_INET6, skb, pp, &ciph, offset, 1);
  1042. if (cp) {
  1043. ipv6_addr_copy(&snet.in6, &iph->saddr);
  1044. return handle_response_icmp(AF_INET6, skb, &snet,
  1045. cih->nexthdr,
  1046. cp, pp, offset,
  1047. sizeof(struct ipv6hdr));
  1048. }
  1049. return NF_ACCEPT;
  1050. }
  1051. verdict = NF_DROP;
  1052. /* do the statistics and put it back */
  1053. ip_vs_in_stats(cp, skb);
  1054. if (IPPROTO_TCP == cih->nexthdr || IPPROTO_UDP == cih->nexthdr)
  1055. offset += 2 * sizeof(__u16);
  1056. verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset);
  1057. /* do not touch skb anymore */
  1058. __ip_vs_conn_put(cp);
  1059. return verdict;
  1060. }
  1061. #endif
  1062. /*
  1063. * Check if it's for virtual services, look it up,
  1064. * and send it on its way...
  1065. */
  1066. static unsigned int
  1067. ip_vs_in(unsigned int hooknum, struct sk_buff *skb,
  1068. const struct net_device *in, const struct net_device *out,
  1069. int (*okfn)(struct sk_buff *))
  1070. {
  1071. struct ip_vs_iphdr iph;
  1072. struct ip_vs_protocol *pp;
  1073. struct ip_vs_conn *cp;
  1074. int ret, restart, af;
  1075. af = (skb->protocol == htons(ETH_P_IP)) ? AF_INET : AF_INET6;
  1076. ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
  1077. /*
  1078. * Big tappo: only PACKET_HOST, including loopback for local client
  1079. * Don't handle local packets on IPv6 for now
  1080. */
  1081. if (unlikely(skb->pkt_type != PACKET_HOST)) {
  1082. IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s ignored\n",
  1083. skb->pkt_type,
  1084. iph.protocol,
  1085. IP_VS_DBG_ADDR(af, &iph.daddr));
  1086. return NF_ACCEPT;
  1087. }
  1088. if (unlikely(iph.protocol == IPPROTO_ICMP)) {
  1089. int related, verdict = ip_vs_in_icmp(skb, &related, hooknum);
  1090. if (related)
  1091. return verdict;
  1092. ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
  1093. }
  1094. /* Protocol supported? */
  1095. pp = ip_vs_proto_get(iph.protocol);
  1096. if (unlikely(!pp))
  1097. return NF_ACCEPT;
  1098. /*
  1099. * Check if the packet belongs to an existing connection entry
  1100. */
  1101. cp = pp->conn_in_get(af, skb, pp, &iph, iph.len, 0);
  1102. if (unlikely(!cp)) {
  1103. int v;
  1104. /* For local client packets, it could be a response */
  1105. cp = pp->conn_out_get(af, skb, pp, &iph, iph.len, 0);
  1106. if (cp)
  1107. return handle_response(af, skb, pp, cp, iph.len);
  1108. if (!pp->conn_schedule(af, skb, pp, &v, &cp))
  1109. return v;
  1110. }
  1111. if (unlikely(!cp)) {
  1112. /* sorry, all this trouble for a no-hit :) */
  1113. IP_VS_DBG_PKT(12, pp, skb, 0,
  1114. "packet continues traversal as normal");
  1115. return NF_ACCEPT;
  1116. }
  1117. IP_VS_DBG_PKT(11, pp, skb, 0, "Incoming packet");
  1118. /* Check the server status */
  1119. if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
  1120. /* the destination server is not available */
  1121. if (sysctl_ip_vs_expire_nodest_conn) {
  1122. /* try to expire the connection immediately */
  1123. ip_vs_conn_expire_now(cp);
  1124. }
  1125. /* don't restart its timer, and silently
  1126. drop the packet. */
  1127. __ip_vs_conn_put(cp);
  1128. return NF_DROP;
  1129. }
  1130. ip_vs_in_stats(cp, skb);
  1131. restart = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp);
  1132. if (cp->packet_xmit)
  1133. ret = cp->packet_xmit(skb, cp, pp);
  1134. /* do not touch skb anymore */
  1135. else {
  1136. IP_VS_DBG_RL("warning: packet_xmit is null");
  1137. ret = NF_ACCEPT;
  1138. }
  1139. /* Increase its packet counter and check if it is needed
  1140. * to be synchronized
  1141. *
  1142. * Sync connection if it is about to close to
  1143. * encorage the standby servers to update the connections timeout
  1144. */
  1145. atomic_inc(&cp->in_pkts);
  1146. if (af == AF_INET &&
  1147. (ip_vs_sync_state & IP_VS_STATE_MASTER) &&
  1148. (((cp->protocol != IPPROTO_TCP ||
  1149. cp->state == IP_VS_TCP_S_ESTABLISHED) &&
  1150. (atomic_read(&cp->in_pkts) % sysctl_ip_vs_sync_threshold[1]
  1151. == sysctl_ip_vs_sync_threshold[0])) ||
  1152. ((cp->protocol == IPPROTO_TCP) && (cp->old_state != cp->state) &&
  1153. ((cp->state == IP_VS_TCP_S_FIN_WAIT) ||
  1154. (cp->state == IP_VS_TCP_S_CLOSE_WAIT) ||
  1155. (cp->state == IP_VS_TCP_S_TIME_WAIT)))))
  1156. ip_vs_sync_conn(cp);
  1157. cp->old_state = cp->state;
  1158. ip_vs_conn_put(cp);
  1159. return ret;
  1160. }
  1161. /*
  1162. * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
  1163. * related packets destined for 0.0.0.0/0.
  1164. * When fwmark-based virtual service is used, such as transparent
  1165. * cache cluster, TCP packets can be marked and routed to ip_vs_in,
  1166. * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
  1167. * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
  1168. * and send them to ip_vs_in_icmp.
  1169. */
  1170. static unsigned int
  1171. ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff *skb,
  1172. const struct net_device *in, const struct net_device *out,
  1173. int (*okfn)(struct sk_buff *))
  1174. {
  1175. int r;
  1176. if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
  1177. return NF_ACCEPT;
  1178. return ip_vs_in_icmp(skb, &r, hooknum);
  1179. }
  1180. #ifdef CONFIG_IP_VS_IPV6
  1181. static unsigned int
  1182. ip_vs_forward_icmp_v6(unsigned int hooknum, struct sk_buff *skb,
  1183. const struct net_device *in, const struct net_device *out,
  1184. int (*okfn)(struct sk_buff *))
  1185. {
  1186. int r;
  1187. if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6)
  1188. return NF_ACCEPT;
  1189. return ip_vs_in_icmp_v6(skb, &r, hooknum);
  1190. }
  1191. #endif
  1192. static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
  1193. /* After packet filtering, forward packet through VS/DR, VS/TUN,
  1194. * or VS/NAT(change destination), so that filtering rules can be
  1195. * applied to IPVS. */
  1196. {
  1197. .hook = ip_vs_in,
  1198. .owner = THIS_MODULE,
  1199. .pf = PF_INET,
  1200. .hooknum = NF_INET_LOCAL_IN,
  1201. .priority = 100,
  1202. },
  1203. /* After packet filtering, change source only for VS/NAT */
  1204. {
  1205. .hook = ip_vs_out,
  1206. .owner = THIS_MODULE,
  1207. .pf = PF_INET,
  1208. .hooknum = NF_INET_FORWARD,
  1209. .priority = 100,
  1210. },
  1211. /* After packet filtering (but before ip_vs_out_icmp), catch icmp
  1212. * destined for 0.0.0.0/0, which is for incoming IPVS connections */
  1213. {
  1214. .hook = ip_vs_forward_icmp,
  1215. .owner = THIS_MODULE,
  1216. .pf = PF_INET,
  1217. .hooknum = NF_INET_FORWARD,
  1218. .priority = 99,
  1219. },
  1220. /* Before the netfilter connection tracking, exit from POST_ROUTING */
  1221. {
  1222. .hook = ip_vs_post_routing,
  1223. .owner = THIS_MODULE,
  1224. .pf = PF_INET,
  1225. .hooknum = NF_INET_POST_ROUTING,
  1226. .priority = NF_IP_PRI_NAT_SRC-1,
  1227. },
  1228. #ifdef CONFIG_IP_VS_IPV6
  1229. /* After packet filtering, forward packet through VS/DR, VS/TUN,
  1230. * or VS/NAT(change destination), so that filtering rules can be
  1231. * applied to IPVS. */
  1232. {
  1233. .hook = ip_vs_in,
  1234. .owner = THIS_MODULE,
  1235. .pf = PF_INET6,
  1236. .hooknum = NF_INET_LOCAL_IN,
  1237. .priority = 100,
  1238. },
  1239. /* After packet filtering, change source only for VS/NAT */
  1240. {
  1241. .hook = ip_vs_out,
  1242. .owner = THIS_MODULE,
  1243. .pf = PF_INET6,
  1244. .hooknum = NF_INET_FORWARD,
  1245. .priority = 100,
  1246. },
  1247. /* After packet filtering (but before ip_vs_out_icmp), catch icmp
  1248. * destined for 0.0.0.0/0, which is for incoming IPVS connections */
  1249. {
  1250. .hook = ip_vs_forward_icmp_v6,
  1251. .owner = THIS_MODULE,
  1252. .pf = PF_INET6,
  1253. .hooknum = NF_INET_FORWARD,
  1254. .priority = 99,
  1255. },
  1256. /* Before the netfilter connection tracking, exit from POST_ROUTING */
  1257. {
  1258. .hook = ip_vs_post_routing,
  1259. .owner = THIS_MODULE,
  1260. .pf = PF_INET6,
  1261. .hooknum = NF_INET_POST_ROUTING,
  1262. .priority = NF_IP6_PRI_NAT_SRC-1,
  1263. },
  1264. #endif
  1265. };
  1266. /*
  1267. * Initialize IP Virtual Server
  1268. */
  1269. static int __init ip_vs_init(void)
  1270. {
  1271. int ret;
  1272. ip_vs_estimator_init();
  1273. ret = ip_vs_control_init();
  1274. if (ret < 0) {
  1275. IP_VS_ERR("can't setup control.\n");
  1276. goto cleanup_estimator;
  1277. }
  1278. ip_vs_protocol_init();
  1279. ret = ip_vs_app_init();
  1280. if (ret < 0) {
  1281. IP_VS_ERR("can't setup application helper.\n");
  1282. goto cleanup_protocol;
  1283. }
  1284. ret = ip_vs_conn_init();
  1285. if (ret < 0) {
  1286. IP_VS_ERR("can't setup connection table.\n");
  1287. goto cleanup_app;
  1288. }
  1289. ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
  1290. if (ret < 0) {
  1291. IP_VS_ERR("can't register hooks.\n");
  1292. goto cleanup_conn;
  1293. }
  1294. IP_VS_INFO("ipvs loaded.\n");
  1295. return ret;
  1296. cleanup_conn:
  1297. ip_vs_conn_cleanup();
  1298. cleanup_app:
  1299. ip_vs_app_cleanup();
  1300. cleanup_protocol:
  1301. ip_vs_protocol_cleanup();
  1302. ip_vs_control_cleanup();
  1303. cleanup_estimator:
  1304. ip_vs_estimator_cleanup();
  1305. return ret;
  1306. }
  1307. static void __exit ip_vs_cleanup(void)
  1308. {
  1309. nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
  1310. ip_vs_conn_cleanup();
  1311. ip_vs_app_cleanup();
  1312. ip_vs_protocol_cleanup();
  1313. ip_vs_control_cleanup();
  1314. ip_vs_estimator_cleanup();
  1315. IP_VS_INFO("ipvs unloaded.\n");
  1316. }
  1317. module_init(ip_vs_init);
  1318. module_exit(ip_vs_cleanup);
  1319. MODULE_LICENSE("GPL");