ip_vs_core.c 30 KB

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