ip_vs_core.c 39 KB

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