ip_vs_core.c 30 KB

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