ip_vs_core.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  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. #ifdef CONFIG_IP_VS_IPV6
  39. #include <net/ipv6.h>
  40. #include <linux/netfilter_ipv6.h>
  41. #endif
  42. #include <net/ip_vs.h>
  43. EXPORT_SYMBOL(register_ip_vs_scheduler);
  44. EXPORT_SYMBOL(unregister_ip_vs_scheduler);
  45. EXPORT_SYMBOL(ip_vs_skb_replace);
  46. EXPORT_SYMBOL(ip_vs_proto_name);
  47. EXPORT_SYMBOL(ip_vs_conn_new);
  48. EXPORT_SYMBOL(ip_vs_conn_in_get);
  49. EXPORT_SYMBOL(ip_vs_conn_out_get);
  50. #ifdef CONFIG_IP_VS_PROTO_TCP
  51. EXPORT_SYMBOL(ip_vs_tcp_conn_listen);
  52. #endif
  53. EXPORT_SYMBOL(ip_vs_conn_put);
  54. #ifdef CONFIG_IP_VS_DEBUG
  55. EXPORT_SYMBOL(ip_vs_get_debug_level);
  56. #endif
  57. /* ID used in ICMP lookups */
  58. #define icmp_id(icmph) (((icmph)->un).echo.id)
  59. #define icmpv6_id(icmph) (icmph->icmp6_dataun.u_echo.identifier)
  60. const char *ip_vs_proto_name(unsigned proto)
  61. {
  62. static char buf[20];
  63. switch (proto) {
  64. case IPPROTO_IP:
  65. return "IP";
  66. case IPPROTO_UDP:
  67. return "UDP";
  68. case IPPROTO_TCP:
  69. return "TCP";
  70. case IPPROTO_ICMP:
  71. return "ICMP";
  72. #ifdef CONFIG_IP_VS_IPV6
  73. case IPPROTO_ICMPV6:
  74. return "ICMPv6";
  75. #endif
  76. default:
  77. sprintf(buf, "IP_%d", proto);
  78. return buf;
  79. }
  80. }
  81. void ip_vs_init_hash_table(struct list_head *table, int rows)
  82. {
  83. while (--rows >= 0)
  84. INIT_LIST_HEAD(&table[rows]);
  85. }
  86. static inline void
  87. ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
  88. {
  89. struct ip_vs_dest *dest = cp->dest;
  90. if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
  91. spin_lock(&dest->stats.lock);
  92. dest->stats.inpkts++;
  93. dest->stats.inbytes += skb->len;
  94. spin_unlock(&dest->stats.lock);
  95. spin_lock(&dest->svc->stats.lock);
  96. dest->svc->stats.inpkts++;
  97. dest->svc->stats.inbytes += skb->len;
  98. spin_unlock(&dest->svc->stats.lock);
  99. spin_lock(&ip_vs_stats.lock);
  100. ip_vs_stats.inpkts++;
  101. ip_vs_stats.inbytes += skb->len;
  102. spin_unlock(&ip_vs_stats.lock);
  103. }
  104. }
  105. static inline void
  106. ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
  107. {
  108. struct ip_vs_dest *dest = cp->dest;
  109. if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
  110. spin_lock(&dest->stats.lock);
  111. dest->stats.outpkts++;
  112. dest->stats.outbytes += skb->len;
  113. spin_unlock(&dest->stats.lock);
  114. spin_lock(&dest->svc->stats.lock);
  115. dest->svc->stats.outpkts++;
  116. dest->svc->stats.outbytes += skb->len;
  117. spin_unlock(&dest->svc->stats.lock);
  118. spin_lock(&ip_vs_stats.lock);
  119. ip_vs_stats.outpkts++;
  120. ip_vs_stats.outbytes += skb->len;
  121. spin_unlock(&ip_vs_stats.lock);
  122. }
  123. }
  124. static inline void
  125. ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
  126. {
  127. spin_lock(&cp->dest->stats.lock);
  128. cp->dest->stats.conns++;
  129. spin_unlock(&cp->dest->stats.lock);
  130. spin_lock(&svc->stats.lock);
  131. svc->stats.conns++;
  132. spin_unlock(&svc->stats.lock);
  133. spin_lock(&ip_vs_stats.lock);
  134. ip_vs_stats.conns++;
  135. spin_unlock(&ip_vs_stats.lock);
  136. }
  137. static inline int
  138. ip_vs_set_state(struct ip_vs_conn *cp, int direction,
  139. const struct sk_buff *skb,
  140. struct ip_vs_protocol *pp)
  141. {
  142. if (unlikely(!pp->state_transition))
  143. return 0;
  144. return pp->state_transition(cp, direction, skb, pp);
  145. }
  146. /*
  147. * IPVS persistent scheduling function
  148. * It creates a connection entry according to its template if exists,
  149. * or selects a server and creates a connection entry plus a template.
  150. * Locking: we are svc user (svc->refcnt), so we hold all dests too
  151. * Protocols supported: TCP, UDP
  152. */
  153. static struct ip_vs_conn *
  154. ip_vs_sched_persist(struct ip_vs_service *svc,
  155. const struct sk_buff *skb,
  156. __be16 ports[2])
  157. {
  158. struct ip_vs_conn *cp = NULL;
  159. struct ip_vs_iphdr iph;
  160. struct ip_vs_dest *dest;
  161. struct ip_vs_conn *ct;
  162. __be16 dport; /* destination port to forward */
  163. union nf_inet_addr snet; /* source network of the client,
  164. after masking */
  165. ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
  166. /* Mask saddr with the netmask to adjust template granularity */
  167. #ifdef CONFIG_IP_VS_IPV6
  168. if (svc->af == AF_INET6)
  169. ipv6_addr_prefix(&snet.in6, &iph.saddr.in6, svc->netmask);
  170. else
  171. #endif
  172. snet.ip = iph.saddr.ip & svc->netmask;
  173. IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
  174. "mnet %s\n",
  175. IP_VS_DBG_ADDR(svc->af, &iph.saddr), ntohs(ports[0]),
  176. IP_VS_DBG_ADDR(svc->af, &iph.daddr), ntohs(ports[1]),
  177. IP_VS_DBG_ADDR(svc->af, &snet));
  178. /*
  179. * As far as we know, FTP is a very complicated network protocol, and
  180. * it uses control connection and data connections. For active FTP,
  181. * FTP server initialize data connection to the client, its source port
  182. * is often 20. For passive FTP, FTP server tells the clients the port
  183. * that it passively listens to, and the client issues the data
  184. * connection. In the tunneling or direct routing mode, the load
  185. * balancer is on the client-to-server half of connection, the port
  186. * number is unknown to the load balancer. So, a conn template like
  187. * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
  188. * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
  189. * is created for other persistent services.
  190. */
  191. if (ports[1] == svc->port) {
  192. /* Check if a template already exists */
  193. if (svc->port != FTPPORT)
  194. ct = ip_vs_ct_in_get(svc->af, iph.protocol, &snet, 0,
  195. &iph.daddr, ports[1]);
  196. else
  197. ct = ip_vs_ct_in_get(svc->af, iph.protocol, &snet, 0,
  198. &iph.daddr, 0);
  199. if (!ct || !ip_vs_check_template(ct)) {
  200. /*
  201. * No template found or the dest of the connection
  202. * template is not available.
  203. */
  204. dest = svc->scheduler->schedule(svc, skb);
  205. if (dest == NULL) {
  206. IP_VS_DBG(1, "p-schedule: no dest found.\n");
  207. return NULL;
  208. }
  209. /*
  210. * Create a template like <protocol,caddr,0,
  211. * vaddr,vport,daddr,dport> for non-ftp service,
  212. * and <protocol,caddr,0,vaddr,0,daddr,0>
  213. * for ftp service.
  214. */
  215. if (svc->port != FTPPORT)
  216. ct = ip_vs_conn_new(svc->af, iph.protocol,
  217. &snet, 0,
  218. &iph.daddr,
  219. ports[1],
  220. &dest->addr, dest->port,
  221. IP_VS_CONN_F_TEMPLATE,
  222. dest);
  223. else
  224. ct = ip_vs_conn_new(svc->af, iph.protocol,
  225. &snet, 0,
  226. &iph.daddr, 0,
  227. &dest->addr, 0,
  228. IP_VS_CONN_F_TEMPLATE,
  229. dest);
  230. if (ct == NULL)
  231. return NULL;
  232. ct->timeout = svc->timeout;
  233. } else {
  234. /* set destination with the found template */
  235. dest = ct->dest;
  236. }
  237. dport = dest->port;
  238. } else {
  239. /*
  240. * Note: persistent fwmark-based services and persistent
  241. * port zero service are handled here.
  242. * fwmark template: <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
  243. * port zero template: <protocol,caddr,0,vaddr,0,daddr,0>
  244. */
  245. if (svc->fwmark) {
  246. union nf_inet_addr fwmark = {
  247. .all = { 0, 0, 0, htonl(svc->fwmark) }
  248. };
  249. ct = ip_vs_ct_in_get(svc->af, IPPROTO_IP, &snet, 0,
  250. &fwmark, 0);
  251. } else
  252. ct = ip_vs_ct_in_get(svc->af, iph.protocol, &snet, 0,
  253. &iph.daddr, 0);
  254. if (!ct || !ip_vs_check_template(ct)) {
  255. /*
  256. * If it is not persistent port zero, return NULL,
  257. * otherwise create a connection template.
  258. */
  259. if (svc->port)
  260. return NULL;
  261. dest = svc->scheduler->schedule(svc, skb);
  262. if (dest == NULL) {
  263. IP_VS_DBG(1, "p-schedule: no dest found.\n");
  264. return NULL;
  265. }
  266. /*
  267. * Create a template according to the service
  268. */
  269. if (svc->fwmark) {
  270. union nf_inet_addr fwmark = {
  271. .all = { 0, 0, 0, htonl(svc->fwmark) }
  272. };
  273. ct = ip_vs_conn_new(svc->af, IPPROTO_IP,
  274. &snet, 0,
  275. &fwmark, 0,
  276. &dest->addr, 0,
  277. IP_VS_CONN_F_TEMPLATE,
  278. dest);
  279. } else
  280. ct = ip_vs_conn_new(svc->af, iph.protocol,
  281. &snet, 0,
  282. &iph.daddr, 0,
  283. &dest->addr, 0,
  284. IP_VS_CONN_F_TEMPLATE,
  285. dest);
  286. if (ct == NULL)
  287. return NULL;
  288. ct->timeout = svc->timeout;
  289. } else {
  290. /* set destination with the found template */
  291. dest = ct->dest;
  292. }
  293. dport = ports[1];
  294. }
  295. /*
  296. * Create a new connection according to the template
  297. */
  298. cp = ip_vs_conn_new(svc->af, iph.protocol,
  299. &iph.saddr, ports[0],
  300. &iph.daddr, ports[1],
  301. &dest->addr, dport,
  302. 0,
  303. dest);
  304. if (cp == NULL) {
  305. ip_vs_conn_put(ct);
  306. return NULL;
  307. }
  308. /*
  309. * Add its control
  310. */
  311. ip_vs_control_add(cp, ct);
  312. ip_vs_conn_put(ct);
  313. ip_vs_conn_stats(cp, svc);
  314. return cp;
  315. }
  316. /*
  317. * IPVS main scheduling function
  318. * It selects a server according to the virtual service, and
  319. * creates a connection entry.
  320. * Protocols supported: TCP, UDP
  321. */
  322. struct ip_vs_conn *
  323. ip_vs_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
  324. {
  325. struct ip_vs_conn *cp = NULL;
  326. struct ip_vs_iphdr iph;
  327. struct ip_vs_dest *dest;
  328. __be16 _ports[2], *pptr;
  329. ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
  330. pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports);
  331. if (pptr == NULL)
  332. return NULL;
  333. /*
  334. * Persistent service
  335. */
  336. if (svc->flags & IP_VS_SVC_F_PERSISTENT)
  337. return ip_vs_sched_persist(svc, skb, pptr);
  338. /*
  339. * Non-persistent service
  340. */
  341. if (!svc->fwmark && pptr[1] != svc->port) {
  342. if (!svc->port)
  343. IP_VS_ERR("Schedule: port zero only supported "
  344. "in persistent services, "
  345. "check your ipvs configuration\n");
  346. return NULL;
  347. }
  348. dest = svc->scheduler->schedule(svc, skb);
  349. if (dest == NULL) {
  350. IP_VS_DBG(1, "Schedule: no dest found.\n");
  351. return NULL;
  352. }
  353. /*
  354. * Create a connection entry.
  355. */
  356. cp = ip_vs_conn_new(svc->af, iph.protocol,
  357. &iph.saddr, pptr[0],
  358. &iph.daddr, pptr[1],
  359. &dest->addr, dest->port ? dest->port : pptr[1],
  360. 0,
  361. dest);
  362. if (cp == NULL)
  363. return NULL;
  364. IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
  365. "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
  366. ip_vs_fwd_tag(cp),
  367. IP_VS_DBG_ADDR(svc->af, &cp->caddr), ntohs(cp->cport),
  368. IP_VS_DBG_ADDR(svc->af, &cp->vaddr), ntohs(cp->vport),
  369. IP_VS_DBG_ADDR(svc->af, &cp->daddr), ntohs(cp->dport),
  370. cp->flags, atomic_read(&cp->refcnt));
  371. ip_vs_conn_stats(cp, svc);
  372. return cp;
  373. }
  374. /*
  375. * Pass or drop the packet.
  376. * Called by ip_vs_in, when the virtual service is available but
  377. * no destination is available for a new connection.
  378. */
  379. int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
  380. struct ip_vs_protocol *pp)
  381. {
  382. __be16 _ports[2], *pptr;
  383. struct ip_vs_iphdr iph;
  384. int unicast;
  385. ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
  386. pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports);
  387. if (pptr == NULL) {
  388. ip_vs_service_put(svc);
  389. return NF_DROP;
  390. }
  391. #ifdef CONFIG_IP_VS_IPV6
  392. if (svc->af == AF_INET6)
  393. unicast = ipv6_addr_type(&iph.daddr.in6) & IPV6_ADDR_UNICAST;
  394. else
  395. #endif
  396. unicast = (inet_addr_type(&init_net, iph.daddr.ip) == RTN_UNICAST);
  397. /* if it is fwmark-based service, the cache_bypass sysctl is up
  398. and the destination is a non-local unicast, then create
  399. a cache_bypass connection entry */
  400. if (sysctl_ip_vs_cache_bypass && svc->fwmark && unicast) {
  401. int ret, cs;
  402. struct ip_vs_conn *cp;
  403. ip_vs_service_put(svc);
  404. /* create a new connection entry */
  405. IP_VS_DBG(6, "ip_vs_leave: create a cache_bypass entry\n");
  406. cp = ip_vs_conn_new(svc->af, iph.protocol,
  407. &iph.saddr, pptr[0],
  408. &iph.daddr, pptr[1],
  409. 0, 0,
  410. IP_VS_CONN_F_BYPASS,
  411. NULL);
  412. if (cp == NULL)
  413. return NF_DROP;
  414. /* statistics */
  415. ip_vs_in_stats(cp, skb);
  416. /* set state */
  417. cs = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp);
  418. /* transmit the first SYN packet */
  419. ret = cp->packet_xmit(skb, cp, pp);
  420. /* do not touch skb anymore */
  421. atomic_inc(&cp->in_pkts);
  422. ip_vs_conn_put(cp);
  423. return ret;
  424. }
  425. /*
  426. * When the virtual ftp service is presented, packets destined
  427. * for other services on the VIP may get here (except services
  428. * listed in the ipvs table), pass the packets, because it is
  429. * not ipvs job to decide to drop the packets.
  430. */
  431. if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT)) {
  432. ip_vs_service_put(svc);
  433. return NF_ACCEPT;
  434. }
  435. ip_vs_service_put(svc);
  436. /*
  437. * Notify the client that the destination is unreachable, and
  438. * release the socket buffer.
  439. * Since it is in IP layer, the TCP socket is not actually
  440. * created, the TCP RST packet cannot be sent, instead that
  441. * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
  442. */
  443. #ifdef CONFIG_IP_VS_IPV6
  444. if (svc->af == AF_INET6)
  445. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0,
  446. skb->dev);
  447. else
  448. #endif
  449. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  450. return NF_DROP;
  451. }
  452. /*
  453. * It is hooked before NF_IP_PRI_NAT_SRC at the NF_INET_POST_ROUTING
  454. * chain, and is used for VS/NAT.
  455. * It detects packets for VS/NAT connections and sends the packets
  456. * immediately. This can avoid that iptable_nat mangles the packets
  457. * for VS/NAT.
  458. */
  459. static unsigned int ip_vs_post_routing(unsigned int hooknum,
  460. struct sk_buff *skb,
  461. const struct net_device *in,
  462. const struct net_device *out,
  463. int (*okfn)(struct sk_buff *))
  464. {
  465. if (!skb->ipvs_property)
  466. return NF_ACCEPT;
  467. /* The packet was sent from IPVS, exit this chain */
  468. return NF_STOP;
  469. }
  470. __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
  471. {
  472. return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
  473. }
  474. static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
  475. {
  476. int err = ip_defrag(skb, user);
  477. if (!err)
  478. ip_send_check(ip_hdr(skb));
  479. return err;
  480. }
  481. #ifdef CONFIG_IP_VS_IPV6
  482. static inline int ip_vs_gather_frags_v6(struct sk_buff *skb, u_int32_t user)
  483. {
  484. /* TODO IPv6: Find out what to do here for IPv6 */
  485. return 0;
  486. }
  487. #endif
  488. /*
  489. * Packet has been made sufficiently writable in caller
  490. * - inout: 1=in->out, 0=out->in
  491. */
  492. void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
  493. struct ip_vs_conn *cp, int inout)
  494. {
  495. struct iphdr *iph = ip_hdr(skb);
  496. unsigned int icmp_offset = iph->ihl*4;
  497. struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
  498. icmp_offset);
  499. struct iphdr *ciph = (struct iphdr *)(icmph + 1);
  500. if (inout) {
  501. iph->saddr = cp->vaddr.ip;
  502. ip_send_check(iph);
  503. ciph->daddr = cp->vaddr.ip;
  504. ip_send_check(ciph);
  505. } else {
  506. iph->daddr = cp->daddr.ip;
  507. ip_send_check(iph);
  508. ciph->saddr = cp->daddr.ip;
  509. ip_send_check(ciph);
  510. }
  511. /* the TCP/UDP port */
  512. if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol) {
  513. __be16 *ports = (void *)ciph + ciph->ihl*4;
  514. if (inout)
  515. ports[1] = cp->vport;
  516. else
  517. ports[0] = cp->dport;
  518. }
  519. /* And finally the ICMP checksum */
  520. icmph->checksum = 0;
  521. icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
  522. skb->ip_summed = CHECKSUM_UNNECESSARY;
  523. if (inout)
  524. IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
  525. "Forwarding altered outgoing ICMP");
  526. else
  527. IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
  528. "Forwarding altered incoming ICMP");
  529. }
  530. #ifdef CONFIG_IP_VS_IPV6
  531. void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
  532. struct ip_vs_conn *cp, int inout)
  533. {
  534. struct ipv6hdr *iph = ipv6_hdr(skb);
  535. unsigned int icmp_offset = sizeof(struct ipv6hdr);
  536. struct icmp6hdr *icmph = (struct icmp6hdr *)(skb_network_header(skb) +
  537. icmp_offset);
  538. struct ipv6hdr *ciph = (struct ipv6hdr *)(icmph + 1);
  539. if (inout) {
  540. iph->saddr = cp->vaddr.in6;
  541. ciph->daddr = cp->vaddr.in6;
  542. } else {
  543. iph->daddr = cp->daddr.in6;
  544. ciph->saddr = cp->daddr.in6;
  545. }
  546. /* the TCP/UDP port */
  547. if (IPPROTO_TCP == ciph->nexthdr || IPPROTO_UDP == ciph->nexthdr) {
  548. __be16 *ports = (void *)ciph + sizeof(struct ipv6hdr);
  549. if (inout)
  550. ports[1] = cp->vport;
  551. else
  552. ports[0] = cp->dport;
  553. }
  554. /* And finally the ICMP checksum */
  555. icmph->icmp6_cksum = 0;
  556. /* TODO IPv6: is this correct for ICMPv6? */
  557. ip_vs_checksum_complete(skb, icmp_offset);
  558. skb->ip_summed = CHECKSUM_UNNECESSARY;
  559. if (inout)
  560. IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
  561. "Forwarding altered outgoing ICMPv6");
  562. else
  563. IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
  564. "Forwarding altered incoming ICMPv6");
  565. }
  566. #endif
  567. /*
  568. * Handle ICMP messages in the inside-to-outside direction (outgoing).
  569. * Find any that might be relevant, check against existing connections,
  570. * forward to the right destination host if relevant.
  571. * Currently handles error types - unreachable, quench, ttl exceeded.
  572. * (Only used in VS/NAT)
  573. */
  574. static int ip_vs_out_icmp(struct sk_buff *skb, int *related)
  575. {
  576. struct iphdr *iph;
  577. struct icmphdr _icmph, *ic;
  578. struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
  579. struct ip_vs_iphdr ciph;
  580. struct ip_vs_conn *cp;
  581. struct ip_vs_protocol *pp;
  582. unsigned int offset, ihl, verdict;
  583. *related = 1;
  584. /* reassemble IP fragments */
  585. if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
  586. if (ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT))
  587. return NF_STOLEN;
  588. }
  589. iph = ip_hdr(skb);
  590. offset = ihl = iph->ihl * 4;
  591. ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
  592. if (ic == NULL)
  593. return NF_DROP;
  594. IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %u.%u.%u.%u->%u.%u.%u.%u\n",
  595. ic->type, ntohs(icmp_id(ic)),
  596. NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
  597. /*
  598. * Work through seeing if this is for us.
  599. * These checks are supposed to be in an order that means easy
  600. * things are checked first to speed up processing.... however
  601. * this means that some packets will manage to get a long way
  602. * down this stack and then be rejected, but that's life.
  603. */
  604. if ((ic->type != ICMP_DEST_UNREACH) &&
  605. (ic->type != ICMP_SOURCE_QUENCH) &&
  606. (ic->type != ICMP_TIME_EXCEEDED)) {
  607. *related = 0;
  608. return NF_ACCEPT;
  609. }
  610. /* Now find the contained IP header */
  611. offset += sizeof(_icmph);
  612. cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
  613. if (cih == NULL)
  614. return NF_ACCEPT; /* The packet looks wrong, ignore */
  615. pp = ip_vs_proto_get(cih->protocol);
  616. if (!pp)
  617. return NF_ACCEPT;
  618. /* Is the embedded protocol header present? */
  619. if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
  620. pp->dont_defrag))
  621. return NF_ACCEPT;
  622. IP_VS_DBG_PKT(11, pp, skb, offset, "Checking outgoing ICMP for");
  623. offset += cih->ihl * 4;
  624. ip_vs_fill_iphdr(AF_INET, cih, &ciph);
  625. /* The embedded headers contain source and dest in reverse order */
  626. cp = pp->conn_out_get(AF_INET, skb, pp, &ciph, offset, 1);
  627. if (!cp)
  628. return NF_ACCEPT;
  629. verdict = NF_DROP;
  630. if (IP_VS_FWD_METHOD(cp) != 0) {
  631. IP_VS_ERR("shouldn't reach here, because the box is on the "
  632. "half connection in the tun/dr module.\n");
  633. }
  634. /* Ensure the checksum is correct */
  635. if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
  636. /* Failed checksum! */
  637. IP_VS_DBG(1, "Forward ICMP: failed checksum from %d.%d.%d.%d!\n",
  638. NIPQUAD(iph->saddr));
  639. goto out;
  640. }
  641. if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
  642. offset += 2 * sizeof(__u16);
  643. if (!skb_make_writable(skb, offset))
  644. goto out;
  645. ip_vs_nat_icmp(skb, pp, cp, 1);
  646. /* do the statistics and put it back */
  647. ip_vs_out_stats(cp, skb);
  648. skb->ipvs_property = 1;
  649. verdict = NF_ACCEPT;
  650. out:
  651. __ip_vs_conn_put(cp);
  652. return verdict;
  653. }
  654. #ifdef CONFIG_IP_VS_IPV6
  655. static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related)
  656. {
  657. struct ipv6hdr *iph;
  658. struct icmp6hdr _icmph, *ic;
  659. struct ipv6hdr _ciph, *cih; /* The ip header contained
  660. within the ICMP */
  661. struct ip_vs_iphdr ciph;
  662. struct ip_vs_conn *cp;
  663. struct ip_vs_protocol *pp;
  664. unsigned int offset, verdict;
  665. *related = 1;
  666. /* reassemble IP fragments */
  667. if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
  668. if (ip_vs_gather_frags_v6(skb, IP_DEFRAG_VS_OUT))
  669. return NF_STOLEN;
  670. }
  671. iph = ipv6_hdr(skb);
  672. offset = sizeof(struct ipv6hdr);
  673. ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
  674. if (ic == NULL)
  675. return NF_DROP;
  676. IP_VS_DBG(12, "Outgoing ICMPv6 (%d,%d) " NIP6_FMT "->" NIP6_FMT "\n",
  677. ic->icmp6_type, ntohs(icmpv6_id(ic)),
  678. NIP6(iph->saddr), NIP6(iph->daddr));
  679. /*
  680. * Work through seeing if this is for us.
  681. * These checks are supposed to be in an order that means easy
  682. * things are checked first to speed up processing.... however
  683. * this means that some packets will manage to get a long way
  684. * down this stack and then be rejected, but that's life.
  685. */
  686. if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
  687. (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
  688. (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
  689. *related = 0;
  690. return NF_ACCEPT;
  691. }
  692. /* Now find the contained IP header */
  693. offset += sizeof(_icmph);
  694. cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
  695. if (cih == NULL)
  696. return NF_ACCEPT; /* The packet looks wrong, ignore */
  697. pp = ip_vs_proto_get(cih->nexthdr);
  698. if (!pp)
  699. return NF_ACCEPT;
  700. /* Is the embedded protocol header present? */
  701. /* TODO: we don't support fragmentation at the moment anyways */
  702. if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
  703. return NF_ACCEPT;
  704. IP_VS_DBG_PKT(11, pp, skb, offset, "Checking outgoing ICMPv6 for");
  705. offset += sizeof(struct ipv6hdr);
  706. ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
  707. /* The embedded headers contain source and dest in reverse order */
  708. cp = pp->conn_out_get(AF_INET6, skb, pp, &ciph, offset, 1);
  709. if (!cp)
  710. return NF_ACCEPT;
  711. verdict = NF_DROP;
  712. if (IP_VS_FWD_METHOD(cp) != 0) {
  713. IP_VS_ERR("shouldn't reach here, because the box is on the "
  714. "half connection in the tun/dr module.\n");
  715. }
  716. /* Ensure the checksum is correct */
  717. if (!skb_csum_unnecessary(skb)
  718. && ip_vs_checksum_complete(skb, sizeof(struct ipv6hdr))) {
  719. /* Failed checksum! */
  720. IP_VS_DBG(1, "Forward ICMPv6: failed checksum from "
  721. NIP6_FMT "!\n",
  722. NIP6(iph->saddr));
  723. goto out;
  724. }
  725. if (IPPROTO_TCP == cih->nexthdr || IPPROTO_UDP == cih->nexthdr)
  726. offset += 2 * sizeof(__u16);
  727. if (!skb_make_writable(skb, offset))
  728. goto out;
  729. ip_vs_nat_icmp_v6(skb, pp, cp, 1);
  730. /* do the statistics and put it back */
  731. ip_vs_out_stats(cp, skb);
  732. skb->ipvs_property = 1;
  733. verdict = NF_ACCEPT;
  734. out:
  735. __ip_vs_conn_put(cp);
  736. return verdict;
  737. }
  738. #endif
  739. static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
  740. {
  741. struct tcphdr _tcph, *th;
  742. th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
  743. if (th == NULL)
  744. return 0;
  745. return th->rst;
  746. }
  747. /*
  748. * It is hooked at the NF_INET_FORWARD chain, used only for VS/NAT.
  749. * Check if outgoing packet belongs to the established ip_vs_conn,
  750. * rewrite addresses of the packet and send it on its way...
  751. */
  752. static unsigned int
  753. ip_vs_out(unsigned int hooknum, struct sk_buff *skb,
  754. const struct net_device *in, const struct net_device *out,
  755. int (*okfn)(struct sk_buff *))
  756. {
  757. struct ip_vs_iphdr iph;
  758. struct ip_vs_protocol *pp;
  759. struct ip_vs_conn *cp;
  760. int af;
  761. EnterFunction(11);
  762. af = (skb->protocol == __constant_htons(ETH_P_IP)) ? AF_INET : AF_INET6;
  763. if (skb->ipvs_property)
  764. return NF_ACCEPT;
  765. ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
  766. #ifdef CONFIG_IP_VS_IPV6
  767. if (af == AF_INET6) {
  768. if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
  769. int related, verdict = ip_vs_out_icmp_v6(skb, &related);
  770. if (related)
  771. return verdict;
  772. ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
  773. }
  774. } else
  775. #endif
  776. if (unlikely(iph.protocol == IPPROTO_ICMP)) {
  777. int related, verdict = ip_vs_out_icmp(skb, &related);
  778. if (related)
  779. return verdict;
  780. ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
  781. }
  782. pp = ip_vs_proto_get(iph.protocol);
  783. if (unlikely(!pp))
  784. return NF_ACCEPT;
  785. /* reassemble IP fragments */
  786. #ifdef CONFIG_IP_VS_IPV6
  787. if (af == AF_INET6) {
  788. if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
  789. int related, verdict = ip_vs_out_icmp_v6(skb, &related);
  790. if (related)
  791. return verdict;
  792. ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
  793. }
  794. } else
  795. #endif
  796. if (unlikely(ip_hdr(skb)->frag_off & htons(IP_MF|IP_OFFSET) &&
  797. !pp->dont_defrag)) {
  798. if (ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT))
  799. return NF_STOLEN;
  800. ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
  801. }
  802. /*
  803. * Check if the packet belongs to an existing entry
  804. */
  805. cp = pp->conn_out_get(af, skb, pp, &iph, iph.len, 0);
  806. if (unlikely(!cp)) {
  807. if (sysctl_ip_vs_nat_icmp_send &&
  808. (pp->protocol == IPPROTO_TCP ||
  809. pp->protocol == IPPROTO_UDP)) {
  810. __be16 _ports[2], *pptr;
  811. pptr = skb_header_pointer(skb, iph.len,
  812. sizeof(_ports), _ports);
  813. if (pptr == NULL)
  814. return NF_ACCEPT; /* Not for me */
  815. if (ip_vs_lookup_real_service(af, iph.protocol,
  816. &iph.saddr,
  817. pptr[0])) {
  818. /*
  819. * Notify the real server: there is no
  820. * existing entry if it is not RST
  821. * packet or not TCP packet.
  822. */
  823. if (iph.protocol != IPPROTO_TCP
  824. || !is_tcp_reset(skb, iph.len)) {
  825. #ifdef CONFIG_IP_VS_IPV6
  826. if (af == AF_INET6)
  827. icmpv6_send(skb,
  828. ICMPV6_DEST_UNREACH,
  829. ICMPV6_PORT_UNREACH,
  830. 0, skb->dev);
  831. else
  832. #endif
  833. icmp_send(skb,
  834. ICMP_DEST_UNREACH,
  835. ICMP_PORT_UNREACH, 0);
  836. return NF_DROP;
  837. }
  838. }
  839. }
  840. IP_VS_DBG_PKT(12, pp, skb, 0,
  841. "packet continues traversal as normal");
  842. return NF_ACCEPT;
  843. }
  844. IP_VS_DBG_PKT(11, pp, skb, 0, "Outgoing packet");
  845. if (!skb_make_writable(skb, iph.len))
  846. goto drop;
  847. /* mangle the packet */
  848. if (pp->snat_handler && !pp->snat_handler(skb, pp, cp))
  849. goto drop;
  850. #ifdef CONFIG_IP_VS_IPV6
  851. if (af == AF_INET6)
  852. ipv6_hdr(skb)->saddr = cp->vaddr.in6;
  853. else
  854. #endif
  855. {
  856. ip_hdr(skb)->saddr = cp->vaddr.ip;
  857. ip_send_check(ip_hdr(skb));
  858. }
  859. /* For policy routing, packets originating from this
  860. * machine itself may be routed differently to packets
  861. * passing through. We want this packet to be routed as
  862. * if it came from this machine itself. So re-compute
  863. * the routing information.
  864. */
  865. #ifdef CONFIG_IP_VS_IPV6
  866. if (af == AF_INET6) {
  867. if (ip6_route_me_harder(skb) != 0)
  868. goto drop;
  869. } else
  870. #endif
  871. if (ip_route_me_harder(skb, RTN_LOCAL) != 0)
  872. goto drop;
  873. IP_VS_DBG_PKT(10, pp, skb, 0, "After SNAT");
  874. ip_vs_out_stats(cp, skb);
  875. ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pp);
  876. ip_vs_conn_put(cp);
  877. skb->ipvs_property = 1;
  878. LeaveFunction(11);
  879. return NF_ACCEPT;
  880. drop:
  881. ip_vs_conn_put(cp);
  882. kfree_skb(skb);
  883. return NF_STOLEN;
  884. }
  885. /*
  886. * Handle ICMP messages in the outside-to-inside direction (incoming).
  887. * Find any that might be relevant, check against existing connections,
  888. * forward to the right destination host if relevant.
  889. * Currently handles error types - unreachable, quench, ttl exceeded.
  890. */
  891. static int
  892. ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
  893. {
  894. struct iphdr *iph;
  895. struct icmphdr _icmph, *ic;
  896. struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
  897. struct ip_vs_iphdr ciph;
  898. struct ip_vs_conn *cp;
  899. struct ip_vs_protocol *pp;
  900. unsigned int offset, ihl, verdict;
  901. *related = 1;
  902. /* reassemble IP fragments */
  903. if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
  904. if (ip_vs_gather_frags(skb, hooknum == NF_INET_LOCAL_IN ?
  905. IP_DEFRAG_VS_IN : IP_DEFRAG_VS_FWD))
  906. return NF_STOLEN;
  907. }
  908. iph = ip_hdr(skb);
  909. offset = ihl = iph->ihl * 4;
  910. ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
  911. if (ic == NULL)
  912. return NF_DROP;
  913. IP_VS_DBG(12, "Incoming ICMP (%d,%d) %u.%u.%u.%u->%u.%u.%u.%u\n",
  914. ic->type, ntohs(icmp_id(ic)),
  915. NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
  916. /*
  917. * Work through seeing if this is for us.
  918. * These checks are supposed to be in an order that means easy
  919. * things are checked first to speed up processing.... however
  920. * this means that some packets will manage to get a long way
  921. * down this stack and then be rejected, but that's life.
  922. */
  923. if ((ic->type != ICMP_DEST_UNREACH) &&
  924. (ic->type != ICMP_SOURCE_QUENCH) &&
  925. (ic->type != ICMP_TIME_EXCEEDED)) {
  926. *related = 0;
  927. return NF_ACCEPT;
  928. }
  929. /* Now find the contained IP header */
  930. offset += sizeof(_icmph);
  931. cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
  932. if (cih == NULL)
  933. return NF_ACCEPT; /* The packet looks wrong, ignore */
  934. pp = ip_vs_proto_get(cih->protocol);
  935. if (!pp)
  936. return NF_ACCEPT;
  937. /* Is the embedded protocol header present? */
  938. if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
  939. pp->dont_defrag))
  940. return NF_ACCEPT;
  941. IP_VS_DBG_PKT(11, pp, skb, offset, "Checking incoming ICMP for");
  942. offset += cih->ihl * 4;
  943. ip_vs_fill_iphdr(AF_INET, cih, &ciph);
  944. /* The embedded headers contain source and dest in reverse order */
  945. cp = pp->conn_in_get(AF_INET, skb, pp, &ciph, offset, 1);
  946. if (!cp)
  947. return NF_ACCEPT;
  948. verdict = NF_DROP;
  949. /* Ensure the checksum is correct */
  950. if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
  951. /* Failed checksum! */
  952. IP_VS_DBG(1, "Incoming ICMP: failed checksum from %d.%d.%d.%d!\n",
  953. NIPQUAD(iph->saddr));
  954. goto out;
  955. }
  956. /* do the statistics and put it back */
  957. ip_vs_in_stats(cp, skb);
  958. if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
  959. offset += 2 * sizeof(__u16);
  960. verdict = ip_vs_icmp_xmit(skb, cp, pp, offset);
  961. /* do not touch skb anymore */
  962. out:
  963. __ip_vs_conn_put(cp);
  964. return verdict;
  965. }
  966. #ifdef CONFIG_IP_VS_IPV6
  967. static int
  968. ip_vs_in_icmp_v6(struct sk_buff *skb, int *related, unsigned int hooknum)
  969. {
  970. struct ipv6hdr *iph;
  971. struct icmp6hdr _icmph, *ic;
  972. struct ipv6hdr _ciph, *cih; /* The ip header contained
  973. within the ICMP */
  974. struct ip_vs_iphdr ciph;
  975. struct ip_vs_conn *cp;
  976. struct ip_vs_protocol *pp;
  977. unsigned int offset, verdict;
  978. *related = 1;
  979. /* reassemble IP fragments */
  980. if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
  981. if (ip_vs_gather_frags_v6(skb, hooknum == NF_INET_LOCAL_IN ?
  982. IP_DEFRAG_VS_IN :
  983. IP_DEFRAG_VS_FWD))
  984. return NF_STOLEN;
  985. }
  986. iph = ipv6_hdr(skb);
  987. offset = sizeof(struct ipv6hdr);
  988. ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
  989. if (ic == NULL)
  990. return NF_DROP;
  991. IP_VS_DBG(12, "Incoming ICMPv6 (%d,%d) " NIP6_FMT "->" NIP6_FMT "\n",
  992. ic->icmp6_type, ntohs(icmpv6_id(ic)),
  993. NIP6(iph->saddr), NIP6(iph->daddr));
  994. /*
  995. * Work through seeing if this is for us.
  996. * These checks are supposed to be in an order that means easy
  997. * things are checked first to speed up processing.... however
  998. * this means that some packets will manage to get a long way
  999. * down this stack and then be rejected, but that's life.
  1000. */
  1001. if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
  1002. (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
  1003. (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
  1004. *related = 0;
  1005. return NF_ACCEPT;
  1006. }
  1007. /* Now find the contained IP header */
  1008. offset += sizeof(_icmph);
  1009. cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
  1010. if (cih == NULL)
  1011. return NF_ACCEPT; /* The packet looks wrong, ignore */
  1012. pp = ip_vs_proto_get(cih->nexthdr);
  1013. if (!pp)
  1014. return NF_ACCEPT;
  1015. /* Is the embedded protocol header present? */
  1016. /* TODO: we don't support fragmentation at the moment anyways */
  1017. if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
  1018. return NF_ACCEPT;
  1019. IP_VS_DBG_PKT(11, pp, skb, offset, "Checking incoming ICMPv6 for");
  1020. offset += sizeof(struct ipv6hdr);
  1021. ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
  1022. /* The embedded headers contain source and dest in reverse order */
  1023. cp = pp->conn_in_get(AF_INET6, skb, pp, &ciph, offset, 1);
  1024. if (!cp)
  1025. return NF_ACCEPT;
  1026. verdict = NF_DROP;
  1027. /* do the statistics and put it back */
  1028. ip_vs_in_stats(cp, skb);
  1029. if (IPPROTO_TCP == cih->nexthdr || IPPROTO_UDP == cih->nexthdr)
  1030. offset += 2 * sizeof(__u16);
  1031. verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset);
  1032. /* do not touch skb anymore */
  1033. __ip_vs_conn_put(cp);
  1034. return verdict;
  1035. }
  1036. #endif
  1037. /*
  1038. * Check if it's for virtual services, look it up,
  1039. * and send it on its way...
  1040. */
  1041. static unsigned int
  1042. ip_vs_in(unsigned int hooknum, struct sk_buff *skb,
  1043. const struct net_device *in, const struct net_device *out,
  1044. int (*okfn)(struct sk_buff *))
  1045. {
  1046. struct ip_vs_iphdr iph;
  1047. struct ip_vs_protocol *pp;
  1048. struct ip_vs_conn *cp;
  1049. int ret, restart, af;
  1050. af = (skb->protocol == __constant_htons(ETH_P_IP)) ? AF_INET : AF_INET6;
  1051. ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
  1052. /*
  1053. * Big tappo: only PACKET_HOST (neither loopback nor mcasts)
  1054. * ... don't know why 1st test DOES NOT include 2nd (?)
  1055. */
  1056. if (unlikely(skb->pkt_type != PACKET_HOST
  1057. || skb->dev->flags & IFF_LOOPBACK || skb->sk)) {
  1058. IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s ignored\n",
  1059. skb->pkt_type,
  1060. iph.protocol,
  1061. IP_VS_DBG_ADDR(af, &iph.daddr));
  1062. return NF_ACCEPT;
  1063. }
  1064. if (unlikely(iph.protocol == IPPROTO_ICMP)) {
  1065. int related, verdict = ip_vs_in_icmp(skb, &related, hooknum);
  1066. if (related)
  1067. return verdict;
  1068. ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
  1069. }
  1070. /* Protocol supported? */
  1071. pp = ip_vs_proto_get(iph.protocol);
  1072. if (unlikely(!pp))
  1073. return NF_ACCEPT;
  1074. /*
  1075. * Check if the packet belongs to an existing connection entry
  1076. */
  1077. cp = pp->conn_in_get(af, skb, pp, &iph, iph.len, 0);
  1078. if (unlikely(!cp)) {
  1079. int v;
  1080. if (!pp->conn_schedule(af, skb, pp, &v, &cp))
  1081. return v;
  1082. }
  1083. if (unlikely(!cp)) {
  1084. /* sorry, all this trouble for a no-hit :) */
  1085. IP_VS_DBG_PKT(12, pp, skb, 0,
  1086. "packet continues traversal as normal");
  1087. return NF_ACCEPT;
  1088. }
  1089. IP_VS_DBG_PKT(11, pp, skb, 0, "Incoming packet");
  1090. /* Check the server status */
  1091. if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
  1092. /* the destination server is not available */
  1093. if (sysctl_ip_vs_expire_nodest_conn) {
  1094. /* try to expire the connection immediately */
  1095. ip_vs_conn_expire_now(cp);
  1096. }
  1097. /* don't restart its timer, and silently
  1098. drop the packet. */
  1099. __ip_vs_conn_put(cp);
  1100. return NF_DROP;
  1101. }
  1102. ip_vs_in_stats(cp, skb);
  1103. restart = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp);
  1104. if (cp->packet_xmit)
  1105. ret = cp->packet_xmit(skb, cp, pp);
  1106. /* do not touch skb anymore */
  1107. else {
  1108. IP_VS_DBG_RL("warning: packet_xmit is null");
  1109. ret = NF_ACCEPT;
  1110. }
  1111. /* Increase its packet counter and check if it is needed
  1112. * to be synchronized
  1113. *
  1114. * Sync connection if it is about to close to
  1115. * encorage the standby servers to update the connections timeout
  1116. */
  1117. atomic_inc(&cp->in_pkts);
  1118. if (af == AF_INET &&
  1119. (ip_vs_sync_state & IP_VS_STATE_MASTER) &&
  1120. (((cp->protocol != IPPROTO_TCP ||
  1121. cp->state == IP_VS_TCP_S_ESTABLISHED) &&
  1122. (atomic_read(&cp->in_pkts) % sysctl_ip_vs_sync_threshold[1]
  1123. == sysctl_ip_vs_sync_threshold[0])) ||
  1124. ((cp->protocol == IPPROTO_TCP) && (cp->old_state != cp->state) &&
  1125. ((cp->state == IP_VS_TCP_S_FIN_WAIT) ||
  1126. (cp->state == IP_VS_TCP_S_CLOSE_WAIT) ||
  1127. (cp->state == IP_VS_TCP_S_TIME_WAIT)))))
  1128. ip_vs_sync_conn(cp);
  1129. cp->old_state = cp->state;
  1130. ip_vs_conn_put(cp);
  1131. return ret;
  1132. }
  1133. /*
  1134. * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
  1135. * related packets destined for 0.0.0.0/0.
  1136. * When fwmark-based virtual service is used, such as transparent
  1137. * cache cluster, TCP packets can be marked and routed to ip_vs_in,
  1138. * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
  1139. * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
  1140. * and send them to ip_vs_in_icmp.
  1141. */
  1142. static unsigned int
  1143. ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff *skb,
  1144. const struct net_device *in, const struct net_device *out,
  1145. int (*okfn)(struct sk_buff *))
  1146. {
  1147. int r;
  1148. if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
  1149. return NF_ACCEPT;
  1150. return ip_vs_in_icmp(skb, &r, hooknum);
  1151. }
  1152. #ifdef CONFIG_IP_VS_IPV6
  1153. static unsigned int
  1154. ip_vs_forward_icmp_v6(unsigned int hooknum, struct sk_buff *skb,
  1155. const struct net_device *in, const struct net_device *out,
  1156. int (*okfn)(struct sk_buff *))
  1157. {
  1158. int r;
  1159. if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6)
  1160. return NF_ACCEPT;
  1161. return ip_vs_in_icmp_v6(skb, &r, hooknum);
  1162. }
  1163. #endif
  1164. static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
  1165. /* After packet filtering, forward packet through VS/DR, VS/TUN,
  1166. * or VS/NAT(change destination), so that filtering rules can be
  1167. * applied to IPVS. */
  1168. {
  1169. .hook = ip_vs_in,
  1170. .owner = THIS_MODULE,
  1171. .pf = PF_INET,
  1172. .hooknum = NF_INET_LOCAL_IN,
  1173. .priority = 100,
  1174. },
  1175. /* After packet filtering, change source only for VS/NAT */
  1176. {
  1177. .hook = ip_vs_out,
  1178. .owner = THIS_MODULE,
  1179. .pf = PF_INET,
  1180. .hooknum = NF_INET_FORWARD,
  1181. .priority = 100,
  1182. },
  1183. /* After packet filtering (but before ip_vs_out_icmp), catch icmp
  1184. * destined for 0.0.0.0/0, which is for incoming IPVS connections */
  1185. {
  1186. .hook = ip_vs_forward_icmp,
  1187. .owner = THIS_MODULE,
  1188. .pf = PF_INET,
  1189. .hooknum = NF_INET_FORWARD,
  1190. .priority = 99,
  1191. },
  1192. /* Before the netfilter connection tracking, exit from POST_ROUTING */
  1193. {
  1194. .hook = ip_vs_post_routing,
  1195. .owner = THIS_MODULE,
  1196. .pf = PF_INET,
  1197. .hooknum = NF_INET_POST_ROUTING,
  1198. .priority = NF_IP_PRI_NAT_SRC-1,
  1199. },
  1200. };
  1201. /*
  1202. * Initialize IP Virtual Server
  1203. */
  1204. static int __init ip_vs_init(void)
  1205. {
  1206. int ret;
  1207. ip_vs_estimator_init();
  1208. ret = ip_vs_control_init();
  1209. if (ret < 0) {
  1210. IP_VS_ERR("can't setup control.\n");
  1211. goto cleanup_estimator;
  1212. }
  1213. ip_vs_protocol_init();
  1214. ret = ip_vs_app_init();
  1215. if (ret < 0) {
  1216. IP_VS_ERR("can't setup application helper.\n");
  1217. goto cleanup_protocol;
  1218. }
  1219. ret = ip_vs_conn_init();
  1220. if (ret < 0) {
  1221. IP_VS_ERR("can't setup connection table.\n");
  1222. goto cleanup_app;
  1223. }
  1224. ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
  1225. if (ret < 0) {
  1226. IP_VS_ERR("can't register hooks.\n");
  1227. goto cleanup_conn;
  1228. }
  1229. IP_VS_INFO("ipvs loaded.\n");
  1230. return ret;
  1231. cleanup_conn:
  1232. ip_vs_conn_cleanup();
  1233. cleanup_app:
  1234. ip_vs_app_cleanup();
  1235. cleanup_protocol:
  1236. ip_vs_protocol_cleanup();
  1237. ip_vs_control_cleanup();
  1238. cleanup_estimator:
  1239. ip_vs_estimator_cleanup();
  1240. return ret;
  1241. }
  1242. static void __exit ip_vs_cleanup(void)
  1243. {
  1244. nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
  1245. ip_vs_conn_cleanup();
  1246. ip_vs_app_cleanup();
  1247. ip_vs_protocol_cleanup();
  1248. ip_vs_control_cleanup();
  1249. ip_vs_estimator_cleanup();
  1250. IP_VS_INFO("ipvs unloaded.\n");
  1251. }
  1252. module_init(ip_vs_init);
  1253. module_exit(ip_vs_cleanup);
  1254. MODULE_LICENSE("GPL");