ip_vs_core.c 40 KB

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