ip_vs_core.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559
  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. /* Handle relevant response ICMP messages - forward to the right
  568. * destination host. Used for NAT and local client.
  569. */
  570. static int handle_response_icmp(struct sk_buff *skb, struct iphdr *iph,
  571. struct iphdr *cih, struct ip_vs_conn *cp,
  572. struct ip_vs_protocol *pp,
  573. unsigned int offset, unsigned int ihl)
  574. {
  575. unsigned int verdict = NF_DROP;
  576. if (IP_VS_FWD_METHOD(cp) != 0) {
  577. IP_VS_ERR("shouldn't reach here, because the box is on the "
  578. "half connection in the tun/dr module.\n");
  579. }
  580. /* Ensure the checksum is correct */
  581. if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
  582. /* Failed checksum! */
  583. IP_VS_DBG(1,
  584. "Forward ICMP: failed checksum from %d.%d.%d.%d!\n",
  585. NIPQUAD(iph->saddr));
  586. goto out;
  587. }
  588. if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
  589. offset += 2 * sizeof(__u16);
  590. if (!skb_make_writable(skb, offset))
  591. goto out;
  592. ip_vs_nat_icmp(skb, pp, cp, 1);
  593. /* do the statistics and put it back */
  594. ip_vs_out_stats(cp, skb);
  595. skb->ipvs_property = 1;
  596. verdict = NF_ACCEPT;
  597. out:
  598. __ip_vs_conn_put(cp);
  599. return verdict;
  600. }
  601. /*
  602. * Handle ICMP messages in the inside-to-outside direction (outgoing).
  603. * Find any that might be relevant, check against existing connections.
  604. * Currently handles error types - unreachable, quench, ttl exceeded.
  605. */
  606. static int ip_vs_out_icmp(struct sk_buff *skb, int *related)
  607. {
  608. struct iphdr *iph;
  609. struct icmphdr _icmph, *ic;
  610. struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
  611. struct ip_vs_iphdr ciph;
  612. struct ip_vs_conn *cp;
  613. struct ip_vs_protocol *pp;
  614. unsigned int offset, ihl;
  615. *related = 1;
  616. /* reassemble IP fragments */
  617. if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
  618. if (ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT))
  619. return NF_STOLEN;
  620. }
  621. iph = ip_hdr(skb);
  622. offset = ihl = iph->ihl * 4;
  623. ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
  624. if (ic == NULL)
  625. return NF_DROP;
  626. IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %u.%u.%u.%u->%u.%u.%u.%u\n",
  627. ic->type, ntohs(icmp_id(ic)),
  628. NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
  629. /*
  630. * Work through seeing if this is for us.
  631. * These checks are supposed to be in an order that means easy
  632. * things are checked first to speed up processing.... however
  633. * this means that some packets will manage to get a long way
  634. * down this stack and then be rejected, but that's life.
  635. */
  636. if ((ic->type != ICMP_DEST_UNREACH) &&
  637. (ic->type != ICMP_SOURCE_QUENCH) &&
  638. (ic->type != ICMP_TIME_EXCEEDED)) {
  639. *related = 0;
  640. return NF_ACCEPT;
  641. }
  642. /* Now find the contained IP header */
  643. offset += sizeof(_icmph);
  644. cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
  645. if (cih == NULL)
  646. return NF_ACCEPT; /* The packet looks wrong, ignore */
  647. pp = ip_vs_proto_get(cih->protocol);
  648. if (!pp)
  649. return NF_ACCEPT;
  650. /* Is the embedded protocol header present? */
  651. if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
  652. pp->dont_defrag))
  653. return NF_ACCEPT;
  654. IP_VS_DBG_PKT(11, pp, skb, offset, "Checking outgoing ICMP for");
  655. offset += cih->ihl * 4;
  656. ip_vs_fill_iphdr(AF_INET, cih, &ciph);
  657. /* The embedded headers contain source and dest in reverse order */
  658. cp = pp->conn_out_get(AF_INET, skb, pp, &ciph, offset, 1);
  659. if (!cp)
  660. return NF_ACCEPT;
  661. return handle_response_icmp(skb, iph, cih, cp, pp, offset, ihl);
  662. }
  663. #ifdef CONFIG_IP_VS_IPV6
  664. static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related)
  665. {
  666. struct ipv6hdr *iph;
  667. struct icmp6hdr _icmph, *ic;
  668. struct ipv6hdr _ciph, *cih; /* The ip header contained
  669. within the ICMP */
  670. struct ip_vs_iphdr ciph;
  671. struct ip_vs_conn *cp;
  672. struct ip_vs_protocol *pp;
  673. unsigned int offset, verdict;
  674. *related = 1;
  675. /* reassemble IP fragments */
  676. if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
  677. if (ip_vs_gather_frags_v6(skb, IP_DEFRAG_VS_OUT))
  678. return NF_STOLEN;
  679. }
  680. iph = ipv6_hdr(skb);
  681. offset = sizeof(struct ipv6hdr);
  682. ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
  683. if (ic == NULL)
  684. return NF_DROP;
  685. IP_VS_DBG(12, "Outgoing ICMPv6 (%d,%d) " NIP6_FMT "->" NIP6_FMT "\n",
  686. ic->icmp6_type, ntohs(icmpv6_id(ic)),
  687. NIP6(iph->saddr), NIP6(iph->daddr));
  688. /*
  689. * Work through seeing if this is for us.
  690. * These checks are supposed to be in an order that means easy
  691. * things are checked first to speed up processing.... however
  692. * this means that some packets will manage to get a long way
  693. * down this stack and then be rejected, but that's life.
  694. */
  695. if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
  696. (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
  697. (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
  698. *related = 0;
  699. return NF_ACCEPT;
  700. }
  701. /* Now find the contained IP header */
  702. offset += sizeof(_icmph);
  703. cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
  704. if (cih == NULL)
  705. return NF_ACCEPT; /* The packet looks wrong, ignore */
  706. pp = ip_vs_proto_get(cih->nexthdr);
  707. if (!pp)
  708. return NF_ACCEPT;
  709. /* Is the embedded protocol header present? */
  710. /* TODO: we don't support fragmentation at the moment anyways */
  711. if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
  712. return NF_ACCEPT;
  713. IP_VS_DBG_PKT(11, pp, skb, offset, "Checking outgoing ICMPv6 for");
  714. offset += sizeof(struct ipv6hdr);
  715. ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
  716. /* The embedded headers contain source and dest in reverse order */
  717. cp = pp->conn_out_get(AF_INET6, skb, pp, &ciph, offset, 1);
  718. if (!cp)
  719. return NF_ACCEPT;
  720. verdict = NF_DROP;
  721. if (IP_VS_FWD_METHOD(cp) != 0) {
  722. IP_VS_ERR("shouldn't reach here, because the box is on the "
  723. "half connection in the tun/dr module.\n");
  724. }
  725. /* Ensure the checksum is correct */
  726. if (!skb_csum_unnecessary(skb)
  727. && ip_vs_checksum_complete(skb, sizeof(struct ipv6hdr))) {
  728. /* Failed checksum! */
  729. IP_VS_DBG(1, "Forward ICMPv6: failed checksum from "
  730. NIP6_FMT "!\n",
  731. NIP6(iph->saddr));
  732. goto out;
  733. }
  734. if (IPPROTO_TCP == cih->nexthdr || IPPROTO_UDP == cih->nexthdr)
  735. offset += 2 * sizeof(__u16);
  736. if (!skb_make_writable(skb, offset))
  737. goto out;
  738. ip_vs_nat_icmp_v6(skb, pp, cp, 1);
  739. /* do the statistics and put it back */
  740. ip_vs_out_stats(cp, skb);
  741. skb->ipvs_property = 1;
  742. verdict = NF_ACCEPT;
  743. out:
  744. __ip_vs_conn_put(cp);
  745. return verdict;
  746. }
  747. #endif
  748. static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
  749. {
  750. struct tcphdr _tcph, *th;
  751. th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
  752. if (th == NULL)
  753. return 0;
  754. return th->rst;
  755. }
  756. /* Handle response packets: rewrite addresses and send away...
  757. * Used for NAT and local client.
  758. */
  759. static unsigned int
  760. handle_response(int af, struct sk_buff *skb, struct ip_vs_protocol *pp,
  761. struct ip_vs_conn *cp, int ihl)
  762. {
  763. IP_VS_DBG_PKT(11, pp, skb, 0, "Outgoing packet");
  764. if (!skb_make_writable(skb, ihl))
  765. goto drop;
  766. /* mangle the packet */
  767. if (pp->snat_handler && !pp->snat_handler(skb, pp, cp))
  768. goto drop;
  769. #ifdef CONFIG_IP_VS_IPV6
  770. if (af == AF_INET6)
  771. ipv6_hdr(skb)->saddr = cp->vaddr.in6;
  772. else
  773. #endif
  774. {
  775. ip_hdr(skb)->saddr = cp->vaddr.ip;
  776. ip_send_check(ip_hdr(skb));
  777. }
  778. /* For policy routing, packets originating from this
  779. * machine itself may be routed differently to packets
  780. * passing through. We want this packet to be routed as
  781. * if it came from this machine itself. So re-compute
  782. * the routing information.
  783. */
  784. #ifdef CONFIG_IP_VS_IPV6
  785. if (af == AF_INET6) {
  786. if (ip6_route_me_harder(skb) != 0)
  787. goto drop;
  788. } else
  789. #endif
  790. if (ip_route_me_harder(skb, RTN_LOCAL) != 0)
  791. goto drop;
  792. /* For policy routing, packets originating from this
  793. * machine itself may be routed differently to packets
  794. * passing through. We want this packet to be routed as
  795. * if it came from this machine itself. So re-compute
  796. * the routing information.
  797. */
  798. if (ip_route_me_harder(skb, RTN_LOCAL) != 0)
  799. goto drop;
  800. IP_VS_DBG_PKT(10, pp, skb, 0, "After SNAT");
  801. ip_vs_out_stats(cp, skb);
  802. ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pp);
  803. ip_vs_conn_put(cp);
  804. skb->ipvs_property = 1;
  805. LeaveFunction(11);
  806. return NF_ACCEPT;
  807. drop:
  808. ip_vs_conn_put(cp);
  809. kfree_skb(skb);
  810. return NF_STOLEN;
  811. }
  812. /*
  813. * It is hooked at the NF_INET_FORWARD chain, used only for VS/NAT.
  814. * Check if outgoing packet belongs to the established ip_vs_conn.
  815. */
  816. static unsigned int
  817. ip_vs_out(unsigned int hooknum, struct sk_buff *skb,
  818. const struct net_device *in, const struct net_device *out,
  819. int (*okfn)(struct sk_buff *))
  820. {
  821. struct ip_vs_iphdr iph;
  822. struct ip_vs_protocol *pp;
  823. struct ip_vs_conn *cp;
  824. int af;
  825. EnterFunction(11);
  826. af = (skb->protocol == __constant_htons(ETH_P_IP)) ? AF_INET : AF_INET6;
  827. if (skb->ipvs_property)
  828. return NF_ACCEPT;
  829. ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
  830. #ifdef CONFIG_IP_VS_IPV6
  831. if (af == AF_INET6) {
  832. if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
  833. int related, verdict = ip_vs_out_icmp_v6(skb, &related);
  834. if (related)
  835. return verdict;
  836. ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
  837. }
  838. } else
  839. #endif
  840. if (unlikely(iph.protocol == IPPROTO_ICMP)) {
  841. int related, verdict = ip_vs_out_icmp(skb, &related);
  842. if (related)
  843. return verdict;
  844. ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
  845. }
  846. pp = ip_vs_proto_get(iph.protocol);
  847. if (unlikely(!pp))
  848. return NF_ACCEPT;
  849. /* reassemble IP fragments */
  850. #ifdef CONFIG_IP_VS_IPV6
  851. if (af == AF_INET6) {
  852. if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
  853. int related, verdict = ip_vs_out_icmp_v6(skb, &related);
  854. if (related)
  855. return verdict;
  856. ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
  857. }
  858. } else
  859. #endif
  860. if (unlikely(ip_hdr(skb)->frag_off & htons(IP_MF|IP_OFFSET) &&
  861. !pp->dont_defrag)) {
  862. if (ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT))
  863. return NF_STOLEN;
  864. ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
  865. }
  866. /*
  867. * Check if the packet belongs to an existing entry
  868. */
  869. cp = pp->conn_out_get(af, skb, pp, &iph, iph.len, 0);
  870. if (unlikely(!cp)) {
  871. if (sysctl_ip_vs_nat_icmp_send &&
  872. (pp->protocol == IPPROTO_TCP ||
  873. pp->protocol == IPPROTO_UDP)) {
  874. __be16 _ports[2], *pptr;
  875. pptr = skb_header_pointer(skb, iph.len,
  876. sizeof(_ports), _ports);
  877. if (pptr == NULL)
  878. return NF_ACCEPT; /* Not for me */
  879. if (ip_vs_lookup_real_service(af, iph.protocol,
  880. &iph.saddr,
  881. pptr[0])) {
  882. /*
  883. * Notify the real server: there is no
  884. * existing entry if it is not RST
  885. * packet or not TCP packet.
  886. */
  887. if (iph.protocol != IPPROTO_TCP
  888. || !is_tcp_reset(skb, iph.len)) {
  889. #ifdef CONFIG_IP_VS_IPV6
  890. if (af == AF_INET6)
  891. icmpv6_send(skb,
  892. ICMPV6_DEST_UNREACH,
  893. ICMPV6_PORT_UNREACH,
  894. 0, skb->dev);
  895. else
  896. #endif
  897. icmp_send(skb,
  898. ICMP_DEST_UNREACH,
  899. ICMP_PORT_UNREACH, 0);
  900. return NF_DROP;
  901. }
  902. }
  903. }
  904. IP_VS_DBG_PKT(12, pp, skb, 0,
  905. "packet continues traversal as normal");
  906. return NF_ACCEPT;
  907. }
  908. return handle_response(af, skb, pp, cp, iph.len);
  909. }
  910. /*
  911. * Handle ICMP messages in the outside-to-inside direction (incoming).
  912. * Find any that might be relevant, check against existing connections,
  913. * forward to the right destination host if relevant.
  914. * Currently handles error types - unreachable, quench, ttl exceeded.
  915. */
  916. static int
  917. ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
  918. {
  919. struct iphdr *iph;
  920. struct icmphdr _icmph, *ic;
  921. struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
  922. struct ip_vs_iphdr ciph;
  923. struct ip_vs_conn *cp;
  924. struct ip_vs_protocol *pp;
  925. unsigned int offset, ihl, verdict;
  926. *related = 1;
  927. /* reassemble IP fragments */
  928. if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
  929. if (ip_vs_gather_frags(skb, hooknum == NF_INET_LOCAL_IN ?
  930. IP_DEFRAG_VS_IN : IP_DEFRAG_VS_FWD))
  931. return NF_STOLEN;
  932. }
  933. iph = ip_hdr(skb);
  934. offset = ihl = iph->ihl * 4;
  935. ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
  936. if (ic == NULL)
  937. return NF_DROP;
  938. IP_VS_DBG(12, "Incoming ICMP (%d,%d) %u.%u.%u.%u->%u.%u.%u.%u\n",
  939. ic->type, ntohs(icmp_id(ic)),
  940. NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
  941. /*
  942. * Work through seeing if this is for us.
  943. * These checks are supposed to be in an order that means easy
  944. * things are checked first to speed up processing.... however
  945. * this means that some packets will manage to get a long way
  946. * down this stack and then be rejected, but that's life.
  947. */
  948. if ((ic->type != ICMP_DEST_UNREACH) &&
  949. (ic->type != ICMP_SOURCE_QUENCH) &&
  950. (ic->type != ICMP_TIME_EXCEEDED)) {
  951. *related = 0;
  952. return NF_ACCEPT;
  953. }
  954. /* Now find the contained IP header */
  955. offset += sizeof(_icmph);
  956. cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
  957. if (cih == NULL)
  958. return NF_ACCEPT; /* The packet looks wrong, ignore */
  959. pp = ip_vs_proto_get(cih->protocol);
  960. if (!pp)
  961. return NF_ACCEPT;
  962. /* Is the embedded protocol header present? */
  963. if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
  964. pp->dont_defrag))
  965. return NF_ACCEPT;
  966. IP_VS_DBG_PKT(11, pp, skb, offset, "Checking incoming ICMP for");
  967. offset += cih->ihl * 4;
  968. ip_vs_fill_iphdr(AF_INET, cih, &ciph);
  969. /* The embedded headers contain source and dest in reverse order */
  970. cp = pp->conn_in_get(AF_INET, skb, pp, &ciph, offset, 1);
  971. if (!cp) {
  972. /* The packet could also belong to a local client */
  973. cp = pp->conn_out_get(AF_INET, skb, pp, &ciph, offset, 1);
  974. if (cp)
  975. return handle_response_icmp(skb, iph, cih, cp, pp,
  976. offset, ihl);
  977. return NF_ACCEPT;
  978. }
  979. verdict = NF_DROP;
  980. /* Ensure the checksum is correct */
  981. if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
  982. /* Failed checksum! */
  983. IP_VS_DBG(1, "Incoming ICMP: failed checksum from %d.%d.%d.%d!\n",
  984. NIPQUAD(iph->saddr));
  985. goto out;
  986. }
  987. /* do the statistics and put it back */
  988. ip_vs_in_stats(cp, skb);
  989. if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
  990. offset += 2 * sizeof(__u16);
  991. verdict = ip_vs_icmp_xmit(skb, cp, pp, offset);
  992. /* do not touch skb anymore */
  993. out:
  994. __ip_vs_conn_put(cp);
  995. return verdict;
  996. }
  997. #ifdef CONFIG_IP_VS_IPV6
  998. static int
  999. ip_vs_in_icmp_v6(struct sk_buff *skb, int *related, unsigned int hooknum)
  1000. {
  1001. struct ipv6hdr *iph;
  1002. struct icmp6hdr _icmph, *ic;
  1003. struct ipv6hdr _ciph, *cih; /* The ip header contained
  1004. within the ICMP */
  1005. struct ip_vs_iphdr ciph;
  1006. struct ip_vs_conn *cp;
  1007. struct ip_vs_protocol *pp;
  1008. unsigned int offset, verdict;
  1009. *related = 1;
  1010. /* reassemble IP fragments */
  1011. if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
  1012. if (ip_vs_gather_frags_v6(skb, hooknum == NF_INET_LOCAL_IN ?
  1013. IP_DEFRAG_VS_IN :
  1014. IP_DEFRAG_VS_FWD))
  1015. return NF_STOLEN;
  1016. }
  1017. iph = ipv6_hdr(skb);
  1018. offset = sizeof(struct ipv6hdr);
  1019. ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
  1020. if (ic == NULL)
  1021. return NF_DROP;
  1022. IP_VS_DBG(12, "Incoming ICMPv6 (%d,%d) " NIP6_FMT "->" NIP6_FMT "\n",
  1023. ic->icmp6_type, ntohs(icmpv6_id(ic)),
  1024. NIP6(iph->saddr), NIP6(iph->daddr));
  1025. /*
  1026. * Work through seeing if this is for us.
  1027. * These checks are supposed to be in an order that means easy
  1028. * things are checked first to speed up processing.... however
  1029. * this means that some packets will manage to get a long way
  1030. * down this stack and then be rejected, but that's life.
  1031. */
  1032. if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
  1033. (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
  1034. (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
  1035. *related = 0;
  1036. return NF_ACCEPT;
  1037. }
  1038. /* Now find the contained IP header */
  1039. offset += sizeof(_icmph);
  1040. cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
  1041. if (cih == NULL)
  1042. return NF_ACCEPT; /* The packet looks wrong, ignore */
  1043. pp = ip_vs_proto_get(cih->nexthdr);
  1044. if (!pp)
  1045. return NF_ACCEPT;
  1046. /* Is the embedded protocol header present? */
  1047. /* TODO: we don't support fragmentation at the moment anyways */
  1048. if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
  1049. return NF_ACCEPT;
  1050. IP_VS_DBG_PKT(11, pp, skb, offset, "Checking incoming ICMPv6 for");
  1051. offset += sizeof(struct ipv6hdr);
  1052. ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
  1053. /* The embedded headers contain source and dest in reverse order */
  1054. cp = pp->conn_in_get(AF_INET6, skb, pp, &ciph, offset, 1);
  1055. if (!cp)
  1056. return NF_ACCEPT;
  1057. verdict = NF_DROP;
  1058. /* do the statistics and put it back */
  1059. ip_vs_in_stats(cp, skb);
  1060. if (IPPROTO_TCP == cih->nexthdr || IPPROTO_UDP == cih->nexthdr)
  1061. offset += 2 * sizeof(__u16);
  1062. verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset);
  1063. /* do not touch skb anymore */
  1064. __ip_vs_conn_put(cp);
  1065. return verdict;
  1066. }
  1067. #endif
  1068. /*
  1069. * Check if it's for virtual services, look it up,
  1070. * and send it on its way...
  1071. */
  1072. static unsigned int
  1073. ip_vs_in(unsigned int hooknum, struct sk_buff *skb,
  1074. const struct net_device *in, const struct net_device *out,
  1075. int (*okfn)(struct sk_buff *))
  1076. {
  1077. struct ip_vs_iphdr iph;
  1078. struct ip_vs_protocol *pp;
  1079. struct ip_vs_conn *cp;
  1080. int ret, restart, af;
  1081. af = (skb->protocol == __constant_htons(ETH_P_IP)) ? AF_INET : AF_INET6;
  1082. ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
  1083. /*
  1084. * Big tappo: only PACKET_HOST, including loopback for local client
  1085. * Don't handle local packets on IPv6 for now
  1086. */
  1087. if (unlikely(skb->pkt_type != PACKET_HOST ||
  1088. (af == AF_INET6 || (skb->dev->flags & IFF_LOOPBACK ||
  1089. skb->sk)))) {
  1090. IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s ignored\n",
  1091. skb->pkt_type,
  1092. iph.protocol,
  1093. IP_VS_DBG_ADDR(af, &iph.daddr));
  1094. return NF_ACCEPT;
  1095. }
  1096. if (unlikely(iph.protocol == IPPROTO_ICMP)) {
  1097. int related, verdict = ip_vs_in_icmp(skb, &related, hooknum);
  1098. if (related)
  1099. return verdict;
  1100. ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
  1101. }
  1102. /* Protocol supported? */
  1103. pp = ip_vs_proto_get(iph.protocol);
  1104. if (unlikely(!pp))
  1105. return NF_ACCEPT;
  1106. /*
  1107. * Check if the packet belongs to an existing connection entry
  1108. */
  1109. cp = pp->conn_in_get(af, skb, pp, &iph, iph.len, 0);
  1110. if (unlikely(!cp)) {
  1111. int v;
  1112. /* For local client packets, it could be a response */
  1113. cp = pp->conn_out_get(af, skb, pp, &iph, iph.len, 0);
  1114. if (cp)
  1115. return handle_response(af, skb, pp, cp, iph.len);
  1116. if (!pp->conn_schedule(af, skb, pp, &v, &cp))
  1117. return v;
  1118. }
  1119. if (unlikely(!cp)) {
  1120. /* sorry, all this trouble for a no-hit :) */
  1121. IP_VS_DBG_PKT(12, pp, skb, 0,
  1122. "packet continues traversal as normal");
  1123. return NF_ACCEPT;
  1124. }
  1125. IP_VS_DBG_PKT(11, pp, skb, 0, "Incoming packet");
  1126. /* Check the server status */
  1127. if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
  1128. /* the destination server is not available */
  1129. if (sysctl_ip_vs_expire_nodest_conn) {
  1130. /* try to expire the connection immediately */
  1131. ip_vs_conn_expire_now(cp);
  1132. }
  1133. /* don't restart its timer, and silently
  1134. drop the packet. */
  1135. __ip_vs_conn_put(cp);
  1136. return NF_DROP;
  1137. }
  1138. ip_vs_in_stats(cp, skb);
  1139. restart = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp);
  1140. if (cp->packet_xmit)
  1141. ret = cp->packet_xmit(skb, cp, pp);
  1142. /* do not touch skb anymore */
  1143. else {
  1144. IP_VS_DBG_RL("warning: packet_xmit is null");
  1145. ret = NF_ACCEPT;
  1146. }
  1147. /* Increase its packet counter and check if it is needed
  1148. * to be synchronized
  1149. *
  1150. * Sync connection if it is about to close to
  1151. * encorage the standby servers to update the connections timeout
  1152. */
  1153. atomic_inc(&cp->in_pkts);
  1154. if (af == AF_INET &&
  1155. (ip_vs_sync_state & IP_VS_STATE_MASTER) &&
  1156. (((cp->protocol != IPPROTO_TCP ||
  1157. cp->state == IP_VS_TCP_S_ESTABLISHED) &&
  1158. (atomic_read(&cp->in_pkts) % sysctl_ip_vs_sync_threshold[1]
  1159. == sysctl_ip_vs_sync_threshold[0])) ||
  1160. ((cp->protocol == IPPROTO_TCP) && (cp->old_state != cp->state) &&
  1161. ((cp->state == IP_VS_TCP_S_FIN_WAIT) ||
  1162. (cp->state == IP_VS_TCP_S_CLOSE_WAIT) ||
  1163. (cp->state == IP_VS_TCP_S_TIME_WAIT)))))
  1164. ip_vs_sync_conn(cp);
  1165. cp->old_state = cp->state;
  1166. ip_vs_conn_put(cp);
  1167. return ret;
  1168. }
  1169. /*
  1170. * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
  1171. * related packets destined for 0.0.0.0/0.
  1172. * When fwmark-based virtual service is used, such as transparent
  1173. * cache cluster, TCP packets can be marked and routed to ip_vs_in,
  1174. * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
  1175. * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
  1176. * and send them to ip_vs_in_icmp.
  1177. */
  1178. static unsigned int
  1179. ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff *skb,
  1180. const struct net_device *in, const struct net_device *out,
  1181. int (*okfn)(struct sk_buff *))
  1182. {
  1183. int r;
  1184. if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
  1185. return NF_ACCEPT;
  1186. return ip_vs_in_icmp(skb, &r, hooknum);
  1187. }
  1188. #ifdef CONFIG_IP_VS_IPV6
  1189. static unsigned int
  1190. ip_vs_forward_icmp_v6(unsigned int hooknum, struct sk_buff *skb,
  1191. const struct net_device *in, const struct net_device *out,
  1192. int (*okfn)(struct sk_buff *))
  1193. {
  1194. int r;
  1195. if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6)
  1196. return NF_ACCEPT;
  1197. return ip_vs_in_icmp_v6(skb, &r, hooknum);
  1198. }
  1199. #endif
  1200. static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
  1201. /* After packet filtering, forward packet through VS/DR, VS/TUN,
  1202. * or VS/NAT(change destination), so that filtering rules can be
  1203. * applied to IPVS. */
  1204. {
  1205. .hook = ip_vs_in,
  1206. .owner = THIS_MODULE,
  1207. .pf = PF_INET,
  1208. .hooknum = NF_INET_LOCAL_IN,
  1209. .priority = 100,
  1210. },
  1211. /* After packet filtering, change source only for VS/NAT */
  1212. {
  1213. .hook = ip_vs_out,
  1214. .owner = THIS_MODULE,
  1215. .pf = PF_INET,
  1216. .hooknum = NF_INET_FORWARD,
  1217. .priority = 100,
  1218. },
  1219. /* After packet filtering (but before ip_vs_out_icmp), catch icmp
  1220. * destined for 0.0.0.0/0, which is for incoming IPVS connections */
  1221. {
  1222. .hook = ip_vs_forward_icmp,
  1223. .owner = THIS_MODULE,
  1224. .pf = PF_INET,
  1225. .hooknum = NF_INET_FORWARD,
  1226. .priority = 99,
  1227. },
  1228. /* Before the netfilter connection tracking, exit from POST_ROUTING */
  1229. {
  1230. .hook = ip_vs_post_routing,
  1231. .owner = THIS_MODULE,
  1232. .pf = PF_INET,
  1233. .hooknum = NF_INET_POST_ROUTING,
  1234. .priority = NF_IP_PRI_NAT_SRC-1,
  1235. },
  1236. #ifdef CONFIG_IP_VS_IPV6
  1237. /* After packet filtering, forward packet through VS/DR, VS/TUN,
  1238. * or VS/NAT(change destination), so that filtering rules can be
  1239. * applied to IPVS. */
  1240. {
  1241. .hook = ip_vs_in,
  1242. .owner = THIS_MODULE,
  1243. .pf = PF_INET6,
  1244. .hooknum = NF_INET_LOCAL_IN,
  1245. .priority = 100,
  1246. },
  1247. /* After packet filtering, change source only for VS/NAT */
  1248. {
  1249. .hook = ip_vs_out,
  1250. .owner = THIS_MODULE,
  1251. .pf = PF_INET6,
  1252. .hooknum = NF_INET_FORWARD,
  1253. .priority = 100,
  1254. },
  1255. /* After packet filtering (but before ip_vs_out_icmp), catch icmp
  1256. * destined for 0.0.0.0/0, which is for incoming IPVS connections */
  1257. {
  1258. .hook = ip_vs_forward_icmp_v6,
  1259. .owner = THIS_MODULE,
  1260. .pf = PF_INET6,
  1261. .hooknum = NF_INET_FORWARD,
  1262. .priority = 99,
  1263. },
  1264. /* Before the netfilter connection tracking, exit from POST_ROUTING */
  1265. {
  1266. .hook = ip_vs_post_routing,
  1267. .owner = THIS_MODULE,
  1268. .pf = PF_INET6,
  1269. .hooknum = NF_INET_POST_ROUTING,
  1270. .priority = NF_IP6_PRI_NAT_SRC-1,
  1271. },
  1272. #endif
  1273. };
  1274. /*
  1275. * Initialize IP Virtual Server
  1276. */
  1277. static int __init ip_vs_init(void)
  1278. {
  1279. int ret;
  1280. ip_vs_estimator_init();
  1281. ret = ip_vs_control_init();
  1282. if (ret < 0) {
  1283. IP_VS_ERR("can't setup control.\n");
  1284. goto cleanup_estimator;
  1285. }
  1286. ip_vs_protocol_init();
  1287. ret = ip_vs_app_init();
  1288. if (ret < 0) {
  1289. IP_VS_ERR("can't setup application helper.\n");
  1290. goto cleanup_protocol;
  1291. }
  1292. ret = ip_vs_conn_init();
  1293. if (ret < 0) {
  1294. IP_VS_ERR("can't setup connection table.\n");
  1295. goto cleanup_app;
  1296. }
  1297. ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
  1298. if (ret < 0) {
  1299. IP_VS_ERR("can't register hooks.\n");
  1300. goto cleanup_conn;
  1301. }
  1302. IP_VS_INFO("ipvs loaded.\n");
  1303. return ret;
  1304. cleanup_conn:
  1305. ip_vs_conn_cleanup();
  1306. cleanup_app:
  1307. ip_vs_app_cleanup();
  1308. cleanup_protocol:
  1309. ip_vs_protocol_cleanup();
  1310. ip_vs_control_cleanup();
  1311. cleanup_estimator:
  1312. ip_vs_estimator_cleanup();
  1313. return ret;
  1314. }
  1315. static void __exit ip_vs_cleanup(void)
  1316. {
  1317. nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
  1318. ip_vs_conn_cleanup();
  1319. ip_vs_app_cleanup();
  1320. ip_vs_protocol_cleanup();
  1321. ip_vs_control_cleanup();
  1322. ip_vs_estimator_cleanup();
  1323. IP_VS_INFO("ipvs unloaded.\n");
  1324. }
  1325. module_init(ip_vs_init);
  1326. module_exit(ip_vs_cleanup);
  1327. MODULE_LICENSE("GPL");