ip_vs_core.c 29 KB

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