ip_vs_core.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  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. __be16 ports[2])
  180. {
  181. struct ip_vs_conn *cp = NULL;
  182. struct iphdr *iph = ip_hdr(skb);
  183. struct ip_vs_dest *dest;
  184. struct ip_vs_conn *ct;
  185. __be16 dport; /* destination port to forward */
  186. __be32 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_ct_in_get(iph->protocol, snet, 0,
  211. iph->daddr, ports[1]);
  212. else
  213. ct = ip_vs_ct_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. IP_VS_CONN_F_TEMPLATE,
  238. dest);
  239. else
  240. ct = ip_vs_conn_new(iph->protocol,
  241. snet, 0,
  242. iph->daddr, 0,
  243. dest->addr, 0,
  244. IP_VS_CONN_F_TEMPLATE,
  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_ct_in_get(IPPROTO_IP, snet, 0,
  263. htonl(svc->fwmark), 0);
  264. else
  265. ct = ip_vs_ct_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. IP_VS_CONN_F_TEMPLATE,
  288. dest);
  289. else
  290. ct = ip_vs_conn_new(iph->protocol,
  291. snet, 0,
  292. iph->daddr, 0,
  293. dest->addr, 0,
  294. IP_VS_CONN_F_TEMPLATE,
  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 = ip_hdr(skb);
  337. struct ip_vs_dest *dest;
  338. __be16 _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 conn->flags:%X conn->refcnt:%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. __be16 _ports[2], *pptr;
  393. struct iphdr *iph = ip_hdr(skb);
  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. return NF_STOP;
  467. }
  468. __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
  469. {
  470. return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
  471. }
  472. static inline struct sk_buff *
  473. ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
  474. {
  475. skb = ip_defrag(skb, user);
  476. if (skb)
  477. ip_send_check(ip_hdr(skb));
  478. return skb;
  479. }
  480. /*
  481. * Packet has been made sufficiently writable in caller
  482. * - inout: 1=in->out, 0=out->in
  483. */
  484. void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
  485. struct ip_vs_conn *cp, int inout)
  486. {
  487. struct iphdr *iph = ip_hdr(skb);
  488. unsigned int icmp_offset = iph->ihl*4;
  489. struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
  490. 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. __be16 *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 (ip_hdr(skb)->frag_off & 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 = ip_hdr(skb);
  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 & 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_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
  592. /* Failed checksum! */
  593. IP_VS_DBG(1, "Forward ICMP: failed checksum from %d.%d.%d.%d!\n",
  594. NIPQUAD(iph->saddr));
  595. goto out;
  596. }
  597. if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
  598. offset += 2 * sizeof(__u16);
  599. if (!ip_vs_make_skb_writable(pskb, offset))
  600. goto out;
  601. skb = *pskb;
  602. ip_vs_nat_icmp(skb, pp, cp, 1);
  603. /* do the statistics and put it back */
  604. ip_vs_out_stats(cp, skb);
  605. skb->ipvs_property = 1;
  606. verdict = NF_ACCEPT;
  607. out:
  608. __ip_vs_conn_put(cp);
  609. return verdict;
  610. }
  611. static inline int is_tcp_reset(const struct sk_buff *skb)
  612. {
  613. struct tcphdr _tcph, *th;
  614. th = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph);
  615. if (th == NULL)
  616. return 0;
  617. return th->rst;
  618. }
  619. /*
  620. * It is hooked at the NF_IP_FORWARD chain, used only for VS/NAT.
  621. * Check if outgoing packet belongs to the established ip_vs_conn,
  622. * rewrite addresses of the packet and send it on its way...
  623. */
  624. static unsigned int
  625. ip_vs_out(unsigned int hooknum, struct sk_buff **pskb,
  626. const struct net_device *in, const struct net_device *out,
  627. int (*okfn)(struct sk_buff *))
  628. {
  629. struct sk_buff *skb = *pskb;
  630. struct iphdr *iph;
  631. struct ip_vs_protocol *pp;
  632. struct ip_vs_conn *cp;
  633. int ihl;
  634. EnterFunction(11);
  635. if (skb->ipvs_property)
  636. return NF_ACCEPT;
  637. iph = ip_hdr(skb);
  638. if (unlikely(iph->protocol == IPPROTO_ICMP)) {
  639. int related, verdict = ip_vs_out_icmp(pskb, &related);
  640. if (related)
  641. return verdict;
  642. skb = *pskb;
  643. iph = ip_hdr(skb);
  644. }
  645. pp = ip_vs_proto_get(iph->protocol);
  646. if (unlikely(!pp))
  647. return NF_ACCEPT;
  648. /* reassemble IP fragments */
  649. if (unlikely(iph->frag_off & htons(IP_MF|IP_OFFSET) &&
  650. !pp->dont_defrag)) {
  651. skb = ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT);
  652. if (!skb)
  653. return NF_STOLEN;
  654. iph = ip_hdr(skb);
  655. *pskb = skb;
  656. }
  657. ihl = iph->ihl << 2;
  658. /*
  659. * Check if the packet belongs to an existing entry
  660. */
  661. cp = pp->conn_out_get(skb, pp, iph, ihl, 0);
  662. if (unlikely(!cp)) {
  663. if (sysctl_ip_vs_nat_icmp_send &&
  664. (pp->protocol == IPPROTO_TCP ||
  665. pp->protocol == IPPROTO_UDP)) {
  666. __be16 _ports[2], *pptr;
  667. pptr = skb_header_pointer(skb, ihl,
  668. sizeof(_ports), _ports);
  669. if (pptr == NULL)
  670. return NF_ACCEPT; /* Not for me */
  671. if (ip_vs_lookup_real_service(iph->protocol,
  672. iph->saddr, pptr[0])) {
  673. /*
  674. * Notify the real server: there is no
  675. * existing entry if it is not RST
  676. * packet or not TCP packet.
  677. */
  678. if (iph->protocol != IPPROTO_TCP
  679. || !is_tcp_reset(skb)) {
  680. icmp_send(skb,ICMP_DEST_UNREACH,
  681. ICMP_PORT_UNREACH, 0);
  682. return NF_DROP;
  683. }
  684. }
  685. }
  686. IP_VS_DBG_PKT(12, pp, skb, 0,
  687. "packet continues traversal as normal");
  688. return NF_ACCEPT;
  689. }
  690. IP_VS_DBG_PKT(11, pp, skb, 0, "Outgoing packet");
  691. if (!ip_vs_make_skb_writable(pskb, ihl))
  692. goto drop;
  693. /* mangle the packet */
  694. if (pp->snat_handler && !pp->snat_handler(pskb, pp, cp))
  695. goto drop;
  696. skb = *pskb;
  697. ip_hdr(skb)->saddr = cp->vaddr;
  698. ip_send_check(ip_hdr(skb));
  699. /* For policy routing, packets originating from this
  700. * machine itself may be routed differently to packets
  701. * passing through. We want this packet to be routed as
  702. * if it came from this machine itself. So re-compute
  703. * the routing information.
  704. */
  705. if (ip_route_me_harder(pskb, RTN_LOCAL) != 0)
  706. goto drop;
  707. skb = *pskb;
  708. IP_VS_DBG_PKT(10, pp, skb, 0, "After SNAT");
  709. ip_vs_out_stats(cp, skb);
  710. ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pp);
  711. ip_vs_conn_put(cp);
  712. skb->ipvs_property = 1;
  713. LeaveFunction(11);
  714. return NF_ACCEPT;
  715. drop:
  716. ip_vs_conn_put(cp);
  717. kfree_skb(*pskb);
  718. return NF_STOLEN;
  719. }
  720. /*
  721. * Handle ICMP messages in the outside-to-inside direction (incoming).
  722. * Find any that might be relevant, check against existing connections,
  723. * forward to the right destination host if relevant.
  724. * Currently handles error types - unreachable, quench, ttl exceeded.
  725. */
  726. static int
  727. ip_vs_in_icmp(struct sk_buff **pskb, int *related, unsigned int hooknum)
  728. {
  729. struct sk_buff *skb = *pskb;
  730. struct iphdr *iph;
  731. struct icmphdr _icmph, *ic;
  732. struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
  733. struct ip_vs_conn *cp;
  734. struct ip_vs_protocol *pp;
  735. unsigned int offset, ihl, verdict;
  736. *related = 1;
  737. /* reassemble IP fragments */
  738. if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
  739. skb = ip_vs_gather_frags(skb,
  740. hooknum == NF_IP_LOCAL_IN ?
  741. IP_DEFRAG_VS_IN : IP_DEFRAG_VS_FWD);
  742. if (!skb)
  743. return NF_STOLEN;
  744. *pskb = skb;
  745. }
  746. iph = ip_hdr(skb);
  747. offset = ihl = iph->ihl * 4;
  748. ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
  749. if (ic == NULL)
  750. return NF_DROP;
  751. IP_VS_DBG(12, "Incoming ICMP (%d,%d) %u.%u.%u.%u->%u.%u.%u.%u\n",
  752. ic->type, ntohs(icmp_id(ic)),
  753. NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
  754. /*
  755. * Work through seeing if this is for us.
  756. * These checks are supposed to be in an order that means easy
  757. * things are checked first to speed up processing.... however
  758. * this means that some packets will manage to get a long way
  759. * down this stack and then be rejected, but that's life.
  760. */
  761. if ((ic->type != ICMP_DEST_UNREACH) &&
  762. (ic->type != ICMP_SOURCE_QUENCH) &&
  763. (ic->type != ICMP_TIME_EXCEEDED)) {
  764. *related = 0;
  765. return NF_ACCEPT;
  766. }
  767. /* Now find the contained IP header */
  768. offset += sizeof(_icmph);
  769. cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
  770. if (cih == NULL)
  771. return NF_ACCEPT; /* The packet looks wrong, ignore */
  772. pp = ip_vs_proto_get(cih->protocol);
  773. if (!pp)
  774. return NF_ACCEPT;
  775. /* Is the embedded protocol header present? */
  776. if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
  777. pp->dont_defrag))
  778. return NF_ACCEPT;
  779. IP_VS_DBG_PKT(11, pp, skb, offset, "Checking incoming ICMP for");
  780. offset += cih->ihl * 4;
  781. /* The embedded headers contain source and dest in reverse order */
  782. cp = pp->conn_in_get(skb, pp, cih, offset, 1);
  783. if (!cp)
  784. return NF_ACCEPT;
  785. verdict = NF_DROP;
  786. /* Ensure the checksum is correct */
  787. if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
  788. /* Failed checksum! */
  789. IP_VS_DBG(1, "Incoming ICMP: failed checksum from %d.%d.%d.%d!\n",
  790. NIPQUAD(iph->saddr));
  791. goto out;
  792. }
  793. /* do the statistics and put it back */
  794. ip_vs_in_stats(cp, skb);
  795. if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
  796. offset += 2 * sizeof(__u16);
  797. verdict = ip_vs_icmp_xmit(skb, cp, pp, offset);
  798. /* do not touch skb anymore */
  799. out:
  800. __ip_vs_conn_put(cp);
  801. return verdict;
  802. }
  803. /*
  804. * Check if it's for virtual services, look it up,
  805. * and send it on its way...
  806. */
  807. static unsigned int
  808. ip_vs_in(unsigned int hooknum, struct sk_buff **pskb,
  809. const struct net_device *in, const struct net_device *out,
  810. int (*okfn)(struct sk_buff *))
  811. {
  812. struct sk_buff *skb = *pskb;
  813. struct iphdr *iph;
  814. struct ip_vs_protocol *pp;
  815. struct ip_vs_conn *cp;
  816. int ret, restart;
  817. int ihl;
  818. /*
  819. * Big tappo: only PACKET_HOST (neither loopback nor mcasts)
  820. * ... don't know why 1st test DOES NOT include 2nd (?)
  821. */
  822. if (unlikely(skb->pkt_type != PACKET_HOST
  823. || skb->dev == &loopback_dev || skb->sk)) {
  824. IP_VS_DBG(12, "packet type=%d proto=%d daddr=%d.%d.%d.%d ignored\n",
  825. skb->pkt_type,
  826. ip_hdr(skb)->protocol,
  827. NIPQUAD(ip_hdr(skb)->daddr));
  828. return NF_ACCEPT;
  829. }
  830. iph = ip_hdr(skb);
  831. if (unlikely(iph->protocol == IPPROTO_ICMP)) {
  832. int related, verdict = ip_vs_in_icmp(pskb, &related, hooknum);
  833. if (related)
  834. return verdict;
  835. skb = *pskb;
  836. iph = ip_hdr(skb);
  837. }
  838. /* Protocol supported? */
  839. pp = ip_vs_proto_get(iph->protocol);
  840. if (unlikely(!pp))
  841. return NF_ACCEPT;
  842. ihl = iph->ihl << 2;
  843. /*
  844. * Check if the packet belongs to an existing connection entry
  845. */
  846. cp = pp->conn_in_get(skb, pp, iph, ihl, 0);
  847. if (unlikely(!cp)) {
  848. int v;
  849. if (!pp->conn_schedule(skb, pp, &v, &cp))
  850. return v;
  851. }
  852. if (unlikely(!cp)) {
  853. /* sorry, all this trouble for a no-hit :) */
  854. IP_VS_DBG_PKT(12, pp, skb, 0,
  855. "packet continues traversal as normal");
  856. return NF_ACCEPT;
  857. }
  858. IP_VS_DBG_PKT(11, pp, skb, 0, "Incoming packet");
  859. /* Check the server status */
  860. if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
  861. /* the destination server is not available */
  862. if (sysctl_ip_vs_expire_nodest_conn) {
  863. /* try to expire the connection immediately */
  864. ip_vs_conn_expire_now(cp);
  865. }
  866. /* don't restart its timer, and silently
  867. drop the packet. */
  868. __ip_vs_conn_put(cp);
  869. return NF_DROP;
  870. }
  871. ip_vs_in_stats(cp, skb);
  872. restart = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp);
  873. if (cp->packet_xmit)
  874. ret = cp->packet_xmit(skb, cp, pp);
  875. /* do not touch skb anymore */
  876. else {
  877. IP_VS_DBG_RL("warning: packet_xmit is null");
  878. ret = NF_ACCEPT;
  879. }
  880. /* increase its packet counter and check if it is needed
  881. to be synchronized */
  882. atomic_inc(&cp->in_pkts);
  883. if ((ip_vs_sync_state & IP_VS_STATE_MASTER) &&
  884. (cp->protocol != IPPROTO_TCP ||
  885. cp->state == IP_VS_TCP_S_ESTABLISHED) &&
  886. (atomic_read(&cp->in_pkts) % sysctl_ip_vs_sync_threshold[1]
  887. == sysctl_ip_vs_sync_threshold[0]))
  888. ip_vs_sync_conn(cp);
  889. ip_vs_conn_put(cp);
  890. return ret;
  891. }
  892. /*
  893. * It is hooked at the NF_IP_FORWARD chain, in order to catch ICMP
  894. * related packets destined for 0.0.0.0/0.
  895. * When fwmark-based virtual service is used, such as transparent
  896. * cache cluster, TCP packets can be marked and routed to ip_vs_in,
  897. * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
  898. * sent to ip_vs_in_icmp. So, catch them at the NF_IP_FORWARD chain
  899. * and send them to ip_vs_in_icmp.
  900. */
  901. static unsigned int
  902. ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff **pskb,
  903. const struct net_device *in, const struct net_device *out,
  904. int (*okfn)(struct sk_buff *))
  905. {
  906. int r;
  907. if (ip_hdr(*pskb)->protocol != IPPROTO_ICMP)
  908. return NF_ACCEPT;
  909. return ip_vs_in_icmp(pskb, &r, hooknum);
  910. }
  911. /* After packet filtering, forward packet through VS/DR, VS/TUN,
  912. or VS/NAT(change destination), so that filtering rules can be
  913. applied to IPVS. */
  914. static struct nf_hook_ops ip_vs_in_ops = {
  915. .hook = ip_vs_in,
  916. .owner = THIS_MODULE,
  917. .pf = PF_INET,
  918. .hooknum = NF_IP_LOCAL_IN,
  919. .priority = 100,
  920. };
  921. /* After packet filtering, change source only for VS/NAT */
  922. static struct nf_hook_ops ip_vs_out_ops = {
  923. .hook = ip_vs_out,
  924. .owner = THIS_MODULE,
  925. .pf = PF_INET,
  926. .hooknum = NF_IP_FORWARD,
  927. .priority = 100,
  928. };
  929. /* After packet filtering (but before ip_vs_out_icmp), catch icmp
  930. destined for 0.0.0.0/0, which is for incoming IPVS connections */
  931. static struct nf_hook_ops ip_vs_forward_icmp_ops = {
  932. .hook = ip_vs_forward_icmp,
  933. .owner = THIS_MODULE,
  934. .pf = PF_INET,
  935. .hooknum = NF_IP_FORWARD,
  936. .priority = 99,
  937. };
  938. /* Before the netfilter connection tracking, exit from POST_ROUTING */
  939. static struct nf_hook_ops ip_vs_post_routing_ops = {
  940. .hook = ip_vs_post_routing,
  941. .owner = THIS_MODULE,
  942. .pf = PF_INET,
  943. .hooknum = NF_IP_POST_ROUTING,
  944. .priority = NF_IP_PRI_NAT_SRC-1,
  945. };
  946. /*
  947. * Initialize IP Virtual Server
  948. */
  949. static int __init ip_vs_init(void)
  950. {
  951. int ret;
  952. ret = ip_vs_control_init();
  953. if (ret < 0) {
  954. IP_VS_ERR("can't setup control.\n");
  955. goto cleanup_nothing;
  956. }
  957. ip_vs_protocol_init();
  958. ret = ip_vs_app_init();
  959. if (ret < 0) {
  960. IP_VS_ERR("can't setup application helper.\n");
  961. goto cleanup_protocol;
  962. }
  963. ret = ip_vs_conn_init();
  964. if (ret < 0) {
  965. IP_VS_ERR("can't setup connection table.\n");
  966. goto cleanup_app;
  967. }
  968. ret = nf_register_hook(&ip_vs_in_ops);
  969. if (ret < 0) {
  970. IP_VS_ERR("can't register in hook.\n");
  971. goto cleanup_conn;
  972. }
  973. ret = nf_register_hook(&ip_vs_out_ops);
  974. if (ret < 0) {
  975. IP_VS_ERR("can't register out hook.\n");
  976. goto cleanup_inops;
  977. }
  978. ret = nf_register_hook(&ip_vs_post_routing_ops);
  979. if (ret < 0) {
  980. IP_VS_ERR("can't register post_routing hook.\n");
  981. goto cleanup_outops;
  982. }
  983. ret = nf_register_hook(&ip_vs_forward_icmp_ops);
  984. if (ret < 0) {
  985. IP_VS_ERR("can't register forward_icmp hook.\n");
  986. goto cleanup_postroutingops;
  987. }
  988. IP_VS_INFO("ipvs loaded.\n");
  989. return ret;
  990. cleanup_postroutingops:
  991. nf_unregister_hook(&ip_vs_post_routing_ops);
  992. cleanup_outops:
  993. nf_unregister_hook(&ip_vs_out_ops);
  994. cleanup_inops:
  995. nf_unregister_hook(&ip_vs_in_ops);
  996. cleanup_conn:
  997. ip_vs_conn_cleanup();
  998. cleanup_app:
  999. ip_vs_app_cleanup();
  1000. cleanup_protocol:
  1001. ip_vs_protocol_cleanup();
  1002. ip_vs_control_cleanup();
  1003. cleanup_nothing:
  1004. return ret;
  1005. }
  1006. static void __exit ip_vs_cleanup(void)
  1007. {
  1008. nf_unregister_hook(&ip_vs_forward_icmp_ops);
  1009. nf_unregister_hook(&ip_vs_post_routing_ops);
  1010. nf_unregister_hook(&ip_vs_out_ops);
  1011. nf_unregister_hook(&ip_vs_in_ops);
  1012. ip_vs_conn_cleanup();
  1013. ip_vs_app_cleanup();
  1014. ip_vs_protocol_cleanup();
  1015. ip_vs_control_cleanup();
  1016. IP_VS_INFO("ipvs unloaded.\n");
  1017. }
  1018. module_init(ip_vs_init);
  1019. module_exit(ip_vs_cleanup);
  1020. MODULE_LICENSE("GPL");