ip_vs_core.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. /*
  2. * IPVS An implementation of the IP virtual server support for the
  3. * LINUX operating system. IPVS is now implemented as a module
  4. * over the Netfilter framework. IPVS can be used to build a
  5. * high-performance and highly available server based on a
  6. * cluster of servers.
  7. *
  8. * Version: $Id: ip_vs_core.c,v 1.34 2003/05/10 03:05:23 wensong Exp $
  9. *
  10. * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
  11. * Peter Kese <peter.kese@ijs.si>
  12. * Julian Anastasov <ja@ssi.bg>
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. *
  19. * The IPVS code for kernel 2.2 was done by Wensong Zhang and Peter Kese,
  20. * with changes/fixes from Julian Anastasov, Lars Marowsky-Bree, Horms
  21. * and others.
  22. *
  23. * Changes:
  24. * Paul `Rusty' Russell properly handle non-linear skbs
  25. * Harald Welte don't use nfcache
  26. *
  27. */
  28. #include <linux/module.h>
  29. #include <linux/kernel.h>
  30. #include <linux/ip.h>
  31. #include <linux/tcp.h>
  32. #include <linux/icmp.h>
  33. #include <net/ip.h>
  34. #include <net/tcp.h>
  35. #include <net/udp.h>
  36. #include <net/icmp.h> /* for icmp_send */
  37. #include <net/route.h>
  38. #include <linux/netfilter.h>
  39. #include <linux/netfilter_ipv4.h>
  40. #include <net/ip_vs.h>
  41. EXPORT_SYMBOL(register_ip_vs_scheduler);
  42. EXPORT_SYMBOL(unregister_ip_vs_scheduler);
  43. EXPORT_SYMBOL(ip_vs_skb_replace);
  44. EXPORT_SYMBOL(ip_vs_proto_name);
  45. EXPORT_SYMBOL(ip_vs_conn_new);
  46. EXPORT_SYMBOL(ip_vs_conn_in_get);
  47. EXPORT_SYMBOL(ip_vs_conn_out_get);
  48. #ifdef CONFIG_IP_VS_PROTO_TCP
  49. EXPORT_SYMBOL(ip_vs_tcp_conn_listen);
  50. #endif
  51. EXPORT_SYMBOL(ip_vs_conn_put);
  52. #ifdef CONFIG_IP_VS_DEBUG
  53. EXPORT_SYMBOL(ip_vs_get_debug_level);
  54. #endif
  55. /* ID used in ICMP lookups */
  56. #define icmp_id(icmph) (((icmph)->un).echo.id)
  57. const char *ip_vs_proto_name(unsigned proto)
  58. {
  59. static char buf[20];
  60. switch (proto) {
  61. case IPPROTO_IP:
  62. return "IP";
  63. case IPPROTO_UDP:
  64. return "UDP";
  65. case IPPROTO_TCP:
  66. return "TCP";
  67. case IPPROTO_ICMP:
  68. return "ICMP";
  69. default:
  70. sprintf(buf, "IP_%d", proto);
  71. return buf;
  72. }
  73. }
  74. void ip_vs_init_hash_table(struct list_head *table, int rows)
  75. {
  76. while (--rows >= 0)
  77. INIT_LIST_HEAD(&table[rows]);
  78. }
  79. static inline void
  80. ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
  81. {
  82. struct ip_vs_dest *dest = cp->dest;
  83. if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
  84. spin_lock(&dest->stats.lock);
  85. dest->stats.inpkts++;
  86. dest->stats.inbytes += skb->len;
  87. spin_unlock(&dest->stats.lock);
  88. spin_lock(&dest->svc->stats.lock);
  89. dest->svc->stats.inpkts++;
  90. dest->svc->stats.inbytes += skb->len;
  91. spin_unlock(&dest->svc->stats.lock);
  92. spin_lock(&ip_vs_stats.lock);
  93. ip_vs_stats.inpkts++;
  94. ip_vs_stats.inbytes += skb->len;
  95. spin_unlock(&ip_vs_stats.lock);
  96. }
  97. }
  98. static inline void
  99. ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
  100. {
  101. struct ip_vs_dest *dest = cp->dest;
  102. if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
  103. spin_lock(&dest->stats.lock);
  104. dest->stats.outpkts++;
  105. dest->stats.outbytes += skb->len;
  106. spin_unlock(&dest->stats.lock);
  107. spin_lock(&dest->svc->stats.lock);
  108. dest->svc->stats.outpkts++;
  109. dest->svc->stats.outbytes += skb->len;
  110. spin_unlock(&dest->svc->stats.lock);
  111. spin_lock(&ip_vs_stats.lock);
  112. ip_vs_stats.outpkts++;
  113. ip_vs_stats.outbytes += skb->len;
  114. spin_unlock(&ip_vs_stats.lock);
  115. }
  116. }
  117. static inline void
  118. ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
  119. {
  120. spin_lock(&cp->dest->stats.lock);
  121. cp->dest->stats.conns++;
  122. spin_unlock(&cp->dest->stats.lock);
  123. spin_lock(&svc->stats.lock);
  124. svc->stats.conns++;
  125. spin_unlock(&svc->stats.lock);
  126. spin_lock(&ip_vs_stats.lock);
  127. ip_vs_stats.conns++;
  128. spin_unlock(&ip_vs_stats.lock);
  129. }
  130. static inline int
  131. ip_vs_set_state(struct ip_vs_conn *cp, int direction,
  132. const struct sk_buff *skb,
  133. struct ip_vs_protocol *pp)
  134. {
  135. if (unlikely(!pp->state_transition))
  136. return 0;
  137. return pp->state_transition(cp, direction, skb, pp);
  138. }
  139. /*
  140. * IPVS persistent scheduling function
  141. * It creates a connection entry according to its template if exists,
  142. * or selects a server and creates a connection entry plus a template.
  143. * Locking: we are svc user (svc->refcnt), so we hold all dests too
  144. * Protocols supported: TCP, UDP
  145. */
  146. static struct ip_vs_conn *
  147. ip_vs_sched_persist(struct ip_vs_service *svc,
  148. const struct sk_buff *skb,
  149. __be16 ports[2])
  150. {
  151. struct ip_vs_conn *cp = NULL;
  152. struct iphdr *iph = ip_hdr(skb);
  153. struct ip_vs_dest *dest;
  154. struct ip_vs_conn *ct;
  155. __be16 dport; /* destination port to forward */
  156. __be32 snet; /* source network of the client, after masking */
  157. /* Mask saddr with the netmask to adjust template granularity */
  158. snet = iph->saddr & svc->netmask;
  159. IP_VS_DBG(6, "p-schedule: src %u.%u.%u.%u:%u dest %u.%u.%u.%u:%u "
  160. "mnet %u.%u.%u.%u\n",
  161. NIPQUAD(iph->saddr), ntohs(ports[0]),
  162. NIPQUAD(iph->daddr), ntohs(ports[1]),
  163. NIPQUAD(snet));
  164. /*
  165. * As far as we know, FTP is a very complicated network protocol, and
  166. * it uses control connection and data connections. For active FTP,
  167. * FTP server initialize data connection to the client, its source port
  168. * is often 20. For passive FTP, FTP server tells the clients the port
  169. * that it passively listens to, and the client issues the data
  170. * connection. In the tunneling or direct routing mode, the load
  171. * balancer is on the client-to-server half of connection, the port
  172. * number is unknown to the load balancer. So, a conn template like
  173. * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
  174. * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
  175. * is created for other persistent services.
  176. */
  177. if (ports[1] == svc->port) {
  178. /* Check if a template already exists */
  179. if (svc->port != FTPPORT)
  180. ct = ip_vs_ct_in_get(iph->protocol, snet, 0,
  181. iph->daddr, ports[1]);
  182. else
  183. ct = ip_vs_ct_in_get(iph->protocol, snet, 0,
  184. iph->daddr, 0);
  185. if (!ct || !ip_vs_check_template(ct)) {
  186. /*
  187. * No template found or the dest of the connection
  188. * template is not available.
  189. */
  190. dest = svc->scheduler->schedule(svc, skb);
  191. if (dest == NULL) {
  192. IP_VS_DBG(1, "p-schedule: no dest found.\n");
  193. return NULL;
  194. }
  195. /*
  196. * Create a template like <protocol,caddr,0,
  197. * vaddr,vport,daddr,dport> for non-ftp service,
  198. * and <protocol,caddr,0,vaddr,0,daddr,0>
  199. * for ftp service.
  200. */
  201. if (svc->port != FTPPORT)
  202. ct = ip_vs_conn_new(iph->protocol,
  203. snet, 0,
  204. iph->daddr,
  205. ports[1],
  206. dest->addr, dest->port,
  207. IP_VS_CONN_F_TEMPLATE,
  208. dest);
  209. else
  210. ct = ip_vs_conn_new(iph->protocol,
  211. snet, 0,
  212. iph->daddr, 0,
  213. dest->addr, 0,
  214. IP_VS_CONN_F_TEMPLATE,
  215. dest);
  216. if (ct == NULL)
  217. return NULL;
  218. ct->timeout = svc->timeout;
  219. } else {
  220. /* set destination with the found template */
  221. dest = ct->dest;
  222. }
  223. dport = dest->port;
  224. } else {
  225. /*
  226. * Note: persistent fwmark-based services and persistent
  227. * port zero service are handled here.
  228. * fwmark template: <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
  229. * port zero template: <protocol,caddr,0,vaddr,0,daddr,0>
  230. */
  231. if (svc->fwmark)
  232. ct = ip_vs_ct_in_get(IPPROTO_IP, snet, 0,
  233. htonl(svc->fwmark), 0);
  234. else
  235. ct = ip_vs_ct_in_get(iph->protocol, snet, 0,
  236. iph->daddr, 0);
  237. if (!ct || !ip_vs_check_template(ct)) {
  238. /*
  239. * If it is not persistent port zero, return NULL,
  240. * otherwise create a connection template.
  241. */
  242. if (svc->port)
  243. return NULL;
  244. dest = svc->scheduler->schedule(svc, skb);
  245. if (dest == NULL) {
  246. IP_VS_DBG(1, "p-schedule: no dest found.\n");
  247. return NULL;
  248. }
  249. /*
  250. * Create a template according to the service
  251. */
  252. if (svc->fwmark)
  253. ct = ip_vs_conn_new(IPPROTO_IP,
  254. snet, 0,
  255. htonl(svc->fwmark), 0,
  256. dest->addr, 0,
  257. IP_VS_CONN_F_TEMPLATE,
  258. dest);
  259. else
  260. ct = ip_vs_conn_new(iph->protocol,
  261. snet, 0,
  262. iph->daddr, 0,
  263. dest->addr, 0,
  264. IP_VS_CONN_F_TEMPLATE,
  265. dest);
  266. if (ct == NULL)
  267. return NULL;
  268. ct->timeout = svc->timeout;
  269. } else {
  270. /* set destination with the found template */
  271. dest = ct->dest;
  272. }
  273. dport = ports[1];
  274. }
  275. /*
  276. * Create a new connection according to the template
  277. */
  278. cp = ip_vs_conn_new(iph->protocol,
  279. iph->saddr, ports[0],
  280. iph->daddr, ports[1],
  281. dest->addr, dport,
  282. 0,
  283. dest);
  284. if (cp == NULL) {
  285. ip_vs_conn_put(ct);
  286. return NULL;
  287. }
  288. /*
  289. * Add its control
  290. */
  291. ip_vs_control_add(cp, ct);
  292. ip_vs_conn_put(ct);
  293. ip_vs_conn_stats(cp, svc);
  294. return cp;
  295. }
  296. /*
  297. * IPVS main scheduling function
  298. * It selects a server according to the virtual service, and
  299. * creates a connection entry.
  300. * Protocols supported: TCP, UDP
  301. */
  302. struct ip_vs_conn *
  303. ip_vs_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
  304. {
  305. struct ip_vs_conn *cp = NULL;
  306. struct iphdr *iph = ip_hdr(skb);
  307. struct ip_vs_dest *dest;
  308. __be16 _ports[2], *pptr;
  309. pptr = skb_header_pointer(skb, iph->ihl*4,
  310. sizeof(_ports), _ports);
  311. if (pptr == NULL)
  312. return NULL;
  313. /*
  314. * Persistent service
  315. */
  316. if (svc->flags & IP_VS_SVC_F_PERSISTENT)
  317. return ip_vs_sched_persist(svc, skb, pptr);
  318. /*
  319. * Non-persistent service
  320. */
  321. if (!svc->fwmark && pptr[1] != svc->port) {
  322. if (!svc->port)
  323. IP_VS_ERR("Schedule: port zero only supported "
  324. "in persistent services, "
  325. "check your ipvs configuration\n");
  326. return NULL;
  327. }
  328. dest = svc->scheduler->schedule(svc, skb);
  329. if (dest == NULL) {
  330. IP_VS_DBG(1, "Schedule: no dest found.\n");
  331. return NULL;
  332. }
  333. /*
  334. * Create a connection entry.
  335. */
  336. cp = ip_vs_conn_new(iph->protocol,
  337. iph->saddr, pptr[0],
  338. iph->daddr, pptr[1],
  339. dest->addr, dest->port?dest->port:pptr[1],
  340. 0,
  341. dest);
  342. if (cp == NULL)
  343. return NULL;
  344. IP_VS_DBG(6, "Schedule fwd:%c c:%u.%u.%u.%u:%u v:%u.%u.%u.%u:%u "
  345. "d:%u.%u.%u.%u:%u conn->flags:%X conn->refcnt:%d\n",
  346. ip_vs_fwd_tag(cp),
  347. NIPQUAD(cp->caddr), ntohs(cp->cport),
  348. NIPQUAD(cp->vaddr), ntohs(cp->vport),
  349. NIPQUAD(cp->daddr), ntohs(cp->dport),
  350. cp->flags, atomic_read(&cp->refcnt));
  351. ip_vs_conn_stats(cp, svc);
  352. return cp;
  353. }
  354. /*
  355. * Pass or drop the packet.
  356. * Called by ip_vs_in, when the virtual service is available but
  357. * no destination is available for a new connection.
  358. */
  359. int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
  360. struct ip_vs_protocol *pp)
  361. {
  362. __be16 _ports[2], *pptr;
  363. struct iphdr *iph = ip_hdr(skb);
  364. pptr = skb_header_pointer(skb, iph->ihl*4,
  365. sizeof(_ports), _ports);
  366. if (pptr == NULL) {
  367. ip_vs_service_put(svc);
  368. return NF_DROP;
  369. }
  370. /* if it is fwmark-based service, the cache_bypass sysctl is up
  371. and the destination is RTN_UNICAST (and not local), then create
  372. a cache_bypass connection entry */
  373. if (sysctl_ip_vs_cache_bypass && svc->fwmark
  374. && (inet_addr_type(&init_net, iph->daddr) == RTN_UNICAST)) {
  375. int ret, cs;
  376. struct ip_vs_conn *cp;
  377. ip_vs_service_put(svc);
  378. /* create a new connection entry */
  379. IP_VS_DBG(6, "ip_vs_leave: create a cache_bypass entry\n");
  380. cp = ip_vs_conn_new(iph->protocol,
  381. iph->saddr, pptr[0],
  382. iph->daddr, pptr[1],
  383. 0, 0,
  384. IP_VS_CONN_F_BYPASS,
  385. NULL);
  386. if (cp == NULL)
  387. return NF_DROP;
  388. /* statistics */
  389. ip_vs_in_stats(cp, skb);
  390. /* set state */
  391. cs = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp);
  392. /* transmit the first SYN packet */
  393. ret = cp->packet_xmit(skb, cp, pp);
  394. /* do not touch skb anymore */
  395. atomic_inc(&cp->in_pkts);
  396. ip_vs_conn_put(cp);
  397. return ret;
  398. }
  399. /*
  400. * When the virtual ftp service is presented, packets destined
  401. * for other services on the VIP may get here (except services
  402. * listed in the ipvs table), pass the packets, because it is
  403. * not ipvs job to decide to drop the packets.
  404. */
  405. if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT)) {
  406. ip_vs_service_put(svc);
  407. return NF_ACCEPT;
  408. }
  409. ip_vs_service_put(svc);
  410. /*
  411. * Notify the client that the destination is unreachable, and
  412. * release the socket buffer.
  413. * Since it is in IP layer, the TCP socket is not actually
  414. * created, the TCP RST packet cannot be sent, instead that
  415. * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
  416. */
  417. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  418. return NF_DROP;
  419. }
  420. /*
  421. * It is hooked before NF_IP_PRI_NAT_SRC at the NF_INET_POST_ROUTING
  422. * chain, and is used for VS/NAT.
  423. * It detects packets for VS/NAT connections and sends the packets
  424. * immediately. This can avoid that iptable_nat mangles the packets
  425. * for VS/NAT.
  426. */
  427. static unsigned int ip_vs_post_routing(unsigned int hooknum,
  428. struct sk_buff *skb,
  429. const struct net_device *in,
  430. const struct net_device *out,
  431. int (*okfn)(struct sk_buff *))
  432. {
  433. if (!skb->ipvs_property)
  434. return NF_ACCEPT;
  435. /* The packet was sent from IPVS, exit this chain */
  436. return NF_STOP;
  437. }
  438. __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
  439. {
  440. return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
  441. }
  442. static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
  443. {
  444. int err = ip_defrag(skb, user);
  445. if (!err)
  446. ip_send_check(ip_hdr(skb));
  447. return err;
  448. }
  449. /*
  450. * Packet has been made sufficiently writable in caller
  451. * - inout: 1=in->out, 0=out->in
  452. */
  453. void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
  454. struct ip_vs_conn *cp, int inout)
  455. {
  456. struct iphdr *iph = ip_hdr(skb);
  457. unsigned int icmp_offset = iph->ihl*4;
  458. struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
  459. icmp_offset);
  460. struct iphdr *ciph = (struct iphdr *)(icmph + 1);
  461. if (inout) {
  462. iph->saddr = cp->vaddr;
  463. ip_send_check(iph);
  464. ciph->daddr = cp->vaddr;
  465. ip_send_check(ciph);
  466. } else {
  467. iph->daddr = cp->daddr;
  468. ip_send_check(iph);
  469. ciph->saddr = cp->daddr;
  470. ip_send_check(ciph);
  471. }
  472. /* the TCP/UDP port */
  473. if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol) {
  474. __be16 *ports = (void *)ciph + ciph->ihl*4;
  475. if (inout)
  476. ports[1] = cp->vport;
  477. else
  478. ports[0] = cp->dport;
  479. }
  480. /* And finally the ICMP checksum */
  481. icmph->checksum = 0;
  482. icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
  483. skb->ip_summed = CHECKSUM_UNNECESSARY;
  484. if (inout)
  485. IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
  486. "Forwarding altered outgoing ICMP");
  487. else
  488. IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
  489. "Forwarding altered incoming ICMP");
  490. }
  491. /*
  492. * Handle ICMP messages in the inside-to-outside direction (outgoing).
  493. * Find any that might be relevant, check against existing connections,
  494. * forward to the right destination host if relevant.
  495. * Currently handles error types - unreachable, quench, ttl exceeded.
  496. * (Only used in VS/NAT)
  497. */
  498. static int ip_vs_out_icmp(struct sk_buff *skb, int *related)
  499. {
  500. struct iphdr *iph;
  501. struct icmphdr _icmph, *ic;
  502. struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
  503. struct ip_vs_conn *cp;
  504. struct ip_vs_protocol *pp;
  505. unsigned int offset, ihl, verdict;
  506. *related = 1;
  507. /* reassemble IP fragments */
  508. if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
  509. if (ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT))
  510. return NF_STOLEN;
  511. }
  512. iph = ip_hdr(skb);
  513. offset = ihl = iph->ihl * 4;
  514. ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
  515. if (ic == NULL)
  516. return NF_DROP;
  517. IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %u.%u.%u.%u->%u.%u.%u.%u\n",
  518. ic->type, ntohs(icmp_id(ic)),
  519. NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
  520. /*
  521. * Work through seeing if this is for us.
  522. * These checks are supposed to be in an order that means easy
  523. * things are checked first to speed up processing.... however
  524. * this means that some packets will manage to get a long way
  525. * down this stack and then be rejected, but that's life.
  526. */
  527. if ((ic->type != ICMP_DEST_UNREACH) &&
  528. (ic->type != ICMP_SOURCE_QUENCH) &&
  529. (ic->type != ICMP_TIME_EXCEEDED)) {
  530. *related = 0;
  531. return NF_ACCEPT;
  532. }
  533. /* Now find the contained IP header */
  534. offset += sizeof(_icmph);
  535. cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
  536. if (cih == NULL)
  537. return NF_ACCEPT; /* The packet looks wrong, ignore */
  538. pp = ip_vs_proto_get(cih->protocol);
  539. if (!pp)
  540. return NF_ACCEPT;
  541. /* Is the embedded protocol header present? */
  542. if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
  543. pp->dont_defrag))
  544. return NF_ACCEPT;
  545. IP_VS_DBG_PKT(11, pp, skb, offset, "Checking outgoing ICMP for");
  546. offset += cih->ihl * 4;
  547. /* The embedded headers contain source and dest in reverse order */
  548. cp = pp->conn_out_get(skb, pp, cih, offset, 1);
  549. if (!cp)
  550. return NF_ACCEPT;
  551. verdict = NF_DROP;
  552. if (IP_VS_FWD_METHOD(cp) != 0) {
  553. IP_VS_ERR("shouldn't reach here, because the box is on the "
  554. "half connection in the tun/dr module.\n");
  555. }
  556. /* Ensure the checksum is correct */
  557. if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
  558. /* Failed checksum! */
  559. IP_VS_DBG(1, "Forward ICMP: failed checksum from %d.%d.%d.%d!\n",
  560. NIPQUAD(iph->saddr));
  561. goto out;
  562. }
  563. if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
  564. offset += 2 * sizeof(__u16);
  565. if (!skb_make_writable(skb, offset))
  566. goto out;
  567. ip_vs_nat_icmp(skb, pp, cp, 1);
  568. /* do the statistics and put it back */
  569. ip_vs_out_stats(cp, skb);
  570. skb->ipvs_property = 1;
  571. verdict = NF_ACCEPT;
  572. out:
  573. __ip_vs_conn_put(cp);
  574. return verdict;
  575. }
  576. static inline int is_tcp_reset(const struct sk_buff *skb)
  577. {
  578. struct tcphdr _tcph, *th;
  579. th = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph);
  580. if (th == NULL)
  581. return 0;
  582. return th->rst;
  583. }
  584. /*
  585. * It is hooked at the NF_INET_FORWARD chain, used only for VS/NAT.
  586. * Check if outgoing packet belongs to the established ip_vs_conn,
  587. * rewrite addresses of the packet and send it on its way...
  588. */
  589. static unsigned int
  590. ip_vs_out(unsigned int hooknum, struct sk_buff *skb,
  591. const struct net_device *in, const struct net_device *out,
  592. int (*okfn)(struct sk_buff *))
  593. {
  594. struct iphdr *iph;
  595. struct ip_vs_protocol *pp;
  596. struct ip_vs_conn *cp;
  597. int ihl;
  598. EnterFunction(11);
  599. if (skb->ipvs_property)
  600. return NF_ACCEPT;
  601. iph = ip_hdr(skb);
  602. if (unlikely(iph->protocol == IPPROTO_ICMP)) {
  603. int related, verdict = ip_vs_out_icmp(skb, &related);
  604. if (related)
  605. return verdict;
  606. iph = ip_hdr(skb);
  607. }
  608. pp = ip_vs_proto_get(iph->protocol);
  609. if (unlikely(!pp))
  610. return NF_ACCEPT;
  611. /* reassemble IP fragments */
  612. if (unlikely(iph->frag_off & htons(IP_MF|IP_OFFSET) &&
  613. !pp->dont_defrag)) {
  614. if (ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT))
  615. return NF_STOLEN;
  616. iph = ip_hdr(skb);
  617. }
  618. ihl = iph->ihl << 2;
  619. /*
  620. * Check if the packet belongs to an existing entry
  621. */
  622. cp = pp->conn_out_get(skb, pp, iph, ihl, 0);
  623. if (unlikely(!cp)) {
  624. if (sysctl_ip_vs_nat_icmp_send &&
  625. (pp->protocol == IPPROTO_TCP ||
  626. pp->protocol == IPPROTO_UDP)) {
  627. __be16 _ports[2], *pptr;
  628. pptr = skb_header_pointer(skb, ihl,
  629. sizeof(_ports), _ports);
  630. if (pptr == NULL)
  631. return NF_ACCEPT; /* Not for me */
  632. if (ip_vs_lookup_real_service(iph->protocol,
  633. iph->saddr, pptr[0])) {
  634. /*
  635. * Notify the real server: there is no
  636. * existing entry if it is not RST
  637. * packet or not TCP packet.
  638. */
  639. if (iph->protocol != IPPROTO_TCP
  640. || !is_tcp_reset(skb)) {
  641. icmp_send(skb,ICMP_DEST_UNREACH,
  642. ICMP_PORT_UNREACH, 0);
  643. return NF_DROP;
  644. }
  645. }
  646. }
  647. IP_VS_DBG_PKT(12, pp, skb, 0,
  648. "packet continues traversal as normal");
  649. return NF_ACCEPT;
  650. }
  651. IP_VS_DBG_PKT(11, pp, skb, 0, "Outgoing packet");
  652. if (!skb_make_writable(skb, ihl))
  653. goto drop;
  654. /* mangle the packet */
  655. if (pp->snat_handler && !pp->snat_handler(skb, pp, cp))
  656. goto drop;
  657. ip_hdr(skb)->saddr = cp->vaddr;
  658. ip_send_check(ip_hdr(skb));
  659. /* For policy routing, packets originating from this
  660. * machine itself may be routed differently to packets
  661. * passing through. We want this packet to be routed as
  662. * if it came from this machine itself. So re-compute
  663. * the routing information.
  664. */
  665. if (ip_route_me_harder(skb, RTN_LOCAL) != 0)
  666. goto drop;
  667. IP_VS_DBG_PKT(10, pp, skb, 0, "After SNAT");
  668. ip_vs_out_stats(cp, skb);
  669. ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pp);
  670. ip_vs_conn_put(cp);
  671. skb->ipvs_property = 1;
  672. LeaveFunction(11);
  673. return NF_ACCEPT;
  674. drop:
  675. ip_vs_conn_put(cp);
  676. kfree_skb(skb);
  677. return NF_STOLEN;
  678. }
  679. /*
  680. * Handle ICMP messages in the outside-to-inside direction (incoming).
  681. * Find any that might be relevant, check against existing connections,
  682. * forward to the right destination host if relevant.
  683. * Currently handles error types - unreachable, quench, ttl exceeded.
  684. */
  685. static int
  686. ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
  687. {
  688. struct iphdr *iph;
  689. struct icmphdr _icmph, *ic;
  690. struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
  691. struct ip_vs_conn *cp;
  692. struct ip_vs_protocol *pp;
  693. unsigned int offset, ihl, verdict;
  694. *related = 1;
  695. /* reassemble IP fragments */
  696. if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
  697. if (ip_vs_gather_frags(skb, hooknum == NF_INET_LOCAL_IN ?
  698. IP_DEFRAG_VS_IN : IP_DEFRAG_VS_FWD))
  699. return NF_STOLEN;
  700. }
  701. iph = ip_hdr(skb);
  702. offset = ihl = iph->ihl * 4;
  703. ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
  704. if (ic == NULL)
  705. return NF_DROP;
  706. IP_VS_DBG(12, "Incoming ICMP (%d,%d) %u.%u.%u.%u->%u.%u.%u.%u\n",
  707. ic->type, ntohs(icmp_id(ic)),
  708. NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
  709. /*
  710. * Work through seeing if this is for us.
  711. * These checks are supposed to be in an order that means easy
  712. * things are checked first to speed up processing.... however
  713. * this means that some packets will manage to get a long way
  714. * down this stack and then be rejected, but that's life.
  715. */
  716. if ((ic->type != ICMP_DEST_UNREACH) &&
  717. (ic->type != ICMP_SOURCE_QUENCH) &&
  718. (ic->type != ICMP_TIME_EXCEEDED)) {
  719. *related = 0;
  720. return NF_ACCEPT;
  721. }
  722. /* Now find the contained IP header */
  723. offset += sizeof(_icmph);
  724. cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
  725. if (cih == NULL)
  726. return NF_ACCEPT; /* The packet looks wrong, ignore */
  727. pp = ip_vs_proto_get(cih->protocol);
  728. if (!pp)
  729. return NF_ACCEPT;
  730. /* Is the embedded protocol header present? */
  731. if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
  732. pp->dont_defrag))
  733. return NF_ACCEPT;
  734. IP_VS_DBG_PKT(11, pp, skb, offset, "Checking incoming ICMP for");
  735. offset += cih->ihl * 4;
  736. /* The embedded headers contain source and dest in reverse order */
  737. cp = pp->conn_in_get(skb, pp, cih, offset, 1);
  738. if (!cp)
  739. return NF_ACCEPT;
  740. verdict = NF_DROP;
  741. /* Ensure the checksum is correct */
  742. if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
  743. /* Failed checksum! */
  744. IP_VS_DBG(1, "Incoming ICMP: failed checksum from %d.%d.%d.%d!\n",
  745. NIPQUAD(iph->saddr));
  746. goto out;
  747. }
  748. /* do the statistics and put it back */
  749. ip_vs_in_stats(cp, skb);
  750. if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
  751. offset += 2 * sizeof(__u16);
  752. verdict = ip_vs_icmp_xmit(skb, cp, pp, offset);
  753. /* do not touch skb anymore */
  754. out:
  755. __ip_vs_conn_put(cp);
  756. return verdict;
  757. }
  758. /*
  759. * Check if it's for virtual services, look it up,
  760. * and send it on its way...
  761. */
  762. static unsigned int
  763. ip_vs_in(unsigned int hooknum, struct sk_buff *skb,
  764. const struct net_device *in, const struct net_device *out,
  765. int (*okfn)(struct sk_buff *))
  766. {
  767. struct iphdr *iph;
  768. struct ip_vs_protocol *pp;
  769. struct ip_vs_conn *cp;
  770. int ret, restart;
  771. int ihl;
  772. /*
  773. * Big tappo: only PACKET_HOST (neither loopback nor mcasts)
  774. * ... don't know why 1st test DOES NOT include 2nd (?)
  775. */
  776. if (unlikely(skb->pkt_type != PACKET_HOST
  777. || skb->dev->flags & IFF_LOOPBACK || skb->sk)) {
  778. IP_VS_DBG(12, "packet type=%d proto=%d daddr=%d.%d.%d.%d ignored\n",
  779. skb->pkt_type,
  780. ip_hdr(skb)->protocol,
  781. NIPQUAD(ip_hdr(skb)->daddr));
  782. return NF_ACCEPT;
  783. }
  784. iph = ip_hdr(skb);
  785. if (unlikely(iph->protocol == IPPROTO_ICMP)) {
  786. int related, verdict = ip_vs_in_icmp(skb, &related, hooknum);
  787. if (related)
  788. return verdict;
  789. iph = ip_hdr(skb);
  790. }
  791. /* Protocol supported? */
  792. pp = ip_vs_proto_get(iph->protocol);
  793. if (unlikely(!pp))
  794. return NF_ACCEPT;
  795. ihl = iph->ihl << 2;
  796. /*
  797. * Check if the packet belongs to an existing connection entry
  798. */
  799. cp = pp->conn_in_get(skb, pp, iph, ihl, 0);
  800. if (unlikely(!cp)) {
  801. int v;
  802. if (!pp->conn_schedule(skb, pp, &v, &cp))
  803. return v;
  804. }
  805. if (unlikely(!cp)) {
  806. /* sorry, all this trouble for a no-hit :) */
  807. IP_VS_DBG_PKT(12, pp, skb, 0,
  808. "packet continues traversal as normal");
  809. return NF_ACCEPT;
  810. }
  811. IP_VS_DBG_PKT(11, pp, skb, 0, "Incoming packet");
  812. /* Check the server status */
  813. if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
  814. /* the destination server is not available */
  815. if (sysctl_ip_vs_expire_nodest_conn) {
  816. /* try to expire the connection immediately */
  817. ip_vs_conn_expire_now(cp);
  818. }
  819. /* don't restart its timer, and silently
  820. drop the packet. */
  821. __ip_vs_conn_put(cp);
  822. return NF_DROP;
  823. }
  824. ip_vs_in_stats(cp, skb);
  825. restart = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp);
  826. if (cp->packet_xmit)
  827. ret = cp->packet_xmit(skb, cp, pp);
  828. /* do not touch skb anymore */
  829. else {
  830. IP_VS_DBG_RL("warning: packet_xmit is null");
  831. ret = NF_ACCEPT;
  832. }
  833. /* Increase its packet counter and check if it is needed
  834. * to be synchronized
  835. *
  836. * Sync connection if it is about to close to
  837. * encorage the standby servers to update the connections timeout
  838. */
  839. atomic_inc(&cp->in_pkts);
  840. if ((ip_vs_sync_state & IP_VS_STATE_MASTER) &&
  841. (((cp->protocol != IPPROTO_TCP ||
  842. cp->state == IP_VS_TCP_S_ESTABLISHED) &&
  843. (atomic_read(&cp->in_pkts) % sysctl_ip_vs_sync_threshold[1]
  844. == sysctl_ip_vs_sync_threshold[0])) ||
  845. ((cp->protocol == IPPROTO_TCP) && (cp->old_state != cp->state) &&
  846. ((cp->state == IP_VS_TCP_S_FIN_WAIT) ||
  847. (cp->state == IP_VS_TCP_S_CLOSE)))))
  848. ip_vs_sync_conn(cp);
  849. cp->old_state = cp->state;
  850. ip_vs_conn_put(cp);
  851. return ret;
  852. }
  853. /*
  854. * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
  855. * related packets destined for 0.0.0.0/0.
  856. * When fwmark-based virtual service is used, such as transparent
  857. * cache cluster, TCP packets can be marked and routed to ip_vs_in,
  858. * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
  859. * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
  860. * and send them to ip_vs_in_icmp.
  861. */
  862. static unsigned int
  863. ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff *skb,
  864. const struct net_device *in, const struct net_device *out,
  865. int (*okfn)(struct sk_buff *))
  866. {
  867. int r;
  868. if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
  869. return NF_ACCEPT;
  870. return ip_vs_in_icmp(skb, &r, hooknum);
  871. }
  872. static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
  873. /* After packet filtering, forward packet through VS/DR, VS/TUN,
  874. * or VS/NAT(change destination), so that filtering rules can be
  875. * applied to IPVS. */
  876. {
  877. .hook = ip_vs_in,
  878. .owner = THIS_MODULE,
  879. .pf = PF_INET,
  880. .hooknum = NF_INET_LOCAL_IN,
  881. .priority = 100,
  882. },
  883. /* After packet filtering, change source only for VS/NAT */
  884. {
  885. .hook = ip_vs_out,
  886. .owner = THIS_MODULE,
  887. .pf = PF_INET,
  888. .hooknum = NF_INET_FORWARD,
  889. .priority = 100,
  890. },
  891. /* After packet filtering (but before ip_vs_out_icmp), catch icmp
  892. * destined for 0.0.0.0/0, which is for incoming IPVS connections */
  893. {
  894. .hook = ip_vs_forward_icmp,
  895. .owner = THIS_MODULE,
  896. .pf = PF_INET,
  897. .hooknum = NF_INET_FORWARD,
  898. .priority = 99,
  899. },
  900. /* Before the netfilter connection tracking, exit from POST_ROUTING */
  901. {
  902. .hook = ip_vs_post_routing,
  903. .owner = THIS_MODULE,
  904. .pf = PF_INET,
  905. .hooknum = NF_INET_POST_ROUTING,
  906. .priority = NF_IP_PRI_NAT_SRC-1,
  907. },
  908. };
  909. /*
  910. * Initialize IP Virtual Server
  911. */
  912. static int __init ip_vs_init(void)
  913. {
  914. int ret;
  915. ret = ip_vs_control_init();
  916. if (ret < 0) {
  917. IP_VS_ERR("can't setup control.\n");
  918. goto cleanup_nothing;
  919. }
  920. ip_vs_protocol_init();
  921. ret = ip_vs_app_init();
  922. if (ret < 0) {
  923. IP_VS_ERR("can't setup application helper.\n");
  924. goto cleanup_protocol;
  925. }
  926. ret = ip_vs_conn_init();
  927. if (ret < 0) {
  928. IP_VS_ERR("can't setup connection table.\n");
  929. goto cleanup_app;
  930. }
  931. ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
  932. if (ret < 0) {
  933. IP_VS_ERR("can't register hooks.\n");
  934. goto cleanup_conn;
  935. }
  936. IP_VS_INFO("ipvs loaded.\n");
  937. return ret;
  938. cleanup_conn:
  939. ip_vs_conn_cleanup();
  940. cleanup_app:
  941. ip_vs_app_cleanup();
  942. cleanup_protocol:
  943. ip_vs_protocol_cleanup();
  944. ip_vs_control_cleanup();
  945. cleanup_nothing:
  946. return ret;
  947. }
  948. static void __exit ip_vs_cleanup(void)
  949. {
  950. nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
  951. ip_vs_conn_cleanup();
  952. ip_vs_app_cleanup();
  953. ip_vs_protocol_cleanup();
  954. ip_vs_control_cleanup();
  955. IP_VS_INFO("ipvs unloaded.\n");
  956. }
  957. module_init(ip_vs_init);
  958. module_exit(ip_vs_cleanup);
  959. MODULE_LICENSE("GPL");