ip_vs_core.c 30 KB

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