ip_vs_core.c 29 KB

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