ip_vs_core.c 29 KB

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