nf_nat_helper.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /* ip_nat_helper.c - generic support functions for NAT helpers
  2. *
  3. * (C) 2000-2002 Harald Welte <laforge@netfilter.org>
  4. * (C) 2003-2006 Netfilter Core Team <coreteam@netfilter.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kmod.h>
  12. #include <linux/types.h>
  13. #include <linux/timer.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/tcp.h>
  16. #include <linux/udp.h>
  17. #include <net/checksum.h>
  18. #include <net/tcp.h>
  19. #include <linux/netfilter_ipv4.h>
  20. #include <net/netfilter/nf_conntrack.h>
  21. #include <net/netfilter/nf_conntrack_helper.h>
  22. #include <net/netfilter/nf_conntrack_expect.h>
  23. #include <net/netfilter/nf_nat.h>
  24. #include <net/netfilter/nf_nat_protocol.h>
  25. #include <net/netfilter/nf_nat_core.h>
  26. #include <net/netfilter/nf_nat_helper.h>
  27. #if 0
  28. #define DEBUGP printk
  29. #define DUMP_OFFSET(x) printk("offset_before=%d, offset_after=%d, correction_pos=%u\n", x->offset_before, x->offset_after, x->correction_pos);
  30. #else
  31. #define DEBUGP(format, args...)
  32. #define DUMP_OFFSET(x)
  33. #endif
  34. static DEFINE_SPINLOCK(nf_nat_seqofs_lock);
  35. /* Setup TCP sequence correction given this change at this sequence */
  36. static inline void
  37. adjust_tcp_sequence(u32 seq,
  38. int sizediff,
  39. struct nf_conn *ct,
  40. enum ip_conntrack_info ctinfo)
  41. {
  42. int dir;
  43. struct nf_nat_seq *this_way, *other_way;
  44. struct nf_conn_nat *nat = nfct_nat(ct);
  45. DEBUGP("nf_nat_resize_packet: old_size = %u, new_size = %u\n",
  46. (*skb)->len, new_size);
  47. dir = CTINFO2DIR(ctinfo);
  48. this_way = &nat->info.seq[dir];
  49. other_way = &nat->info.seq[!dir];
  50. DEBUGP("nf_nat_resize_packet: Seq_offset before: ");
  51. DUMP_OFFSET(this_way);
  52. spin_lock_bh(&nf_nat_seqofs_lock);
  53. /* SYN adjust. If it's uninitialized, or this is after last
  54. * correction, record it: we don't handle more than one
  55. * adjustment in the window, but do deal with common case of a
  56. * retransmit */
  57. if (this_way->offset_before == this_way->offset_after ||
  58. before(this_way->correction_pos, seq)) {
  59. this_way->correction_pos = seq;
  60. this_way->offset_before = this_way->offset_after;
  61. this_way->offset_after += sizediff;
  62. }
  63. spin_unlock_bh(&nf_nat_seqofs_lock);
  64. DEBUGP("nf_nat_resize_packet: Seq_offset after: ");
  65. DUMP_OFFSET(this_way);
  66. }
  67. /* Frobs data inside this packet, which is linear. */
  68. static void mangle_contents(struct sk_buff *skb,
  69. unsigned int dataoff,
  70. unsigned int match_offset,
  71. unsigned int match_len,
  72. const char *rep_buffer,
  73. unsigned int rep_len)
  74. {
  75. unsigned char *data;
  76. BUG_ON(skb_is_nonlinear(skb));
  77. data = (unsigned char *)skb->nh.iph + dataoff;
  78. /* move post-replacement */
  79. memmove(data + match_offset + rep_len,
  80. data + match_offset + match_len,
  81. skb->tail - (data + match_offset + match_len));
  82. /* insert data from buffer */
  83. memcpy(data + match_offset, rep_buffer, rep_len);
  84. /* update skb info */
  85. if (rep_len > match_len) {
  86. DEBUGP("nf_nat_mangle_packet: Extending packet by "
  87. "%u from %u bytes\n", rep_len - match_len,
  88. skb->len);
  89. skb_put(skb, rep_len - match_len);
  90. } else {
  91. DEBUGP("nf_nat_mangle_packet: Shrinking packet from "
  92. "%u from %u bytes\n", match_len - rep_len,
  93. skb->len);
  94. __skb_trim(skb, skb->len + rep_len - match_len);
  95. }
  96. /* fix IP hdr checksum information */
  97. skb->nh.iph->tot_len = htons(skb->len);
  98. ip_send_check(skb->nh.iph);
  99. }
  100. /* Unusual, but possible case. */
  101. static int enlarge_skb(struct sk_buff **pskb, unsigned int extra)
  102. {
  103. struct sk_buff *nskb;
  104. if ((*pskb)->len + extra > 65535)
  105. return 0;
  106. nskb = skb_copy_expand(*pskb, skb_headroom(*pskb), extra, GFP_ATOMIC);
  107. if (!nskb)
  108. return 0;
  109. /* Transfer socket to new skb. */
  110. if ((*pskb)->sk)
  111. skb_set_owner_w(nskb, (*pskb)->sk);
  112. kfree_skb(*pskb);
  113. *pskb = nskb;
  114. return 1;
  115. }
  116. /* Generic function for mangling variable-length address changes inside
  117. * NATed TCP connections (like the PORT XXX,XXX,XXX,XXX,XXX,XXX
  118. * command in FTP).
  119. *
  120. * Takes care about all the nasty sequence number changes, checksumming,
  121. * skb enlargement, ...
  122. *
  123. * */
  124. int
  125. nf_nat_mangle_tcp_packet(struct sk_buff **pskb,
  126. struct nf_conn *ct,
  127. enum ip_conntrack_info ctinfo,
  128. unsigned int match_offset,
  129. unsigned int match_len,
  130. const char *rep_buffer,
  131. unsigned int rep_len)
  132. {
  133. struct iphdr *iph;
  134. struct tcphdr *tcph;
  135. int oldlen, datalen;
  136. if (!skb_make_writable(pskb, (*pskb)->len))
  137. return 0;
  138. if (rep_len > match_len &&
  139. rep_len - match_len > skb_tailroom(*pskb) &&
  140. !enlarge_skb(pskb, rep_len - match_len))
  141. return 0;
  142. SKB_LINEAR_ASSERT(*pskb);
  143. iph = (*pskb)->nh.iph;
  144. tcph = (void *)iph + iph->ihl*4;
  145. oldlen = (*pskb)->len - iph->ihl*4;
  146. mangle_contents(*pskb, iph->ihl*4 + tcph->doff*4,
  147. match_offset, match_len, rep_buffer, rep_len);
  148. datalen = (*pskb)->len - iph->ihl*4;
  149. if ((*pskb)->ip_summed != CHECKSUM_PARTIAL) {
  150. tcph->check = 0;
  151. tcph->check = tcp_v4_check(datalen,
  152. iph->saddr, iph->daddr,
  153. csum_partial((char *)tcph,
  154. datalen, 0));
  155. } else
  156. nf_proto_csum_replace2(&tcph->check, *pskb,
  157. htons(oldlen), htons(datalen), 1);
  158. if (rep_len != match_len) {
  159. set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
  160. adjust_tcp_sequence(ntohl(tcph->seq),
  161. (int)rep_len - (int)match_len,
  162. ct, ctinfo);
  163. /* Tell TCP window tracking about seq change */
  164. nf_conntrack_tcp_update(*pskb, (*pskb)->nh.iph->ihl*4,
  165. ct, CTINFO2DIR(ctinfo));
  166. }
  167. return 1;
  168. }
  169. EXPORT_SYMBOL(nf_nat_mangle_tcp_packet);
  170. /* Generic function for mangling variable-length address changes inside
  171. * NATed UDP connections (like the CONNECT DATA XXXXX MESG XXXXX INDEX XXXXX
  172. * command in the Amanda protocol)
  173. *
  174. * Takes care about all the nasty sequence number changes, checksumming,
  175. * skb enlargement, ...
  176. *
  177. * XXX - This function could be merged with nf_nat_mangle_tcp_packet which
  178. * should be fairly easy to do.
  179. */
  180. int
  181. nf_nat_mangle_udp_packet(struct sk_buff **pskb,
  182. struct nf_conn *ct,
  183. enum ip_conntrack_info ctinfo,
  184. unsigned int match_offset,
  185. unsigned int match_len,
  186. const char *rep_buffer,
  187. unsigned int rep_len)
  188. {
  189. struct iphdr *iph;
  190. struct udphdr *udph;
  191. int datalen, oldlen;
  192. /* UDP helpers might accidentally mangle the wrong packet */
  193. iph = (*pskb)->nh.iph;
  194. if ((*pskb)->len < iph->ihl*4 + sizeof(*udph) +
  195. match_offset + match_len)
  196. return 0;
  197. if (!skb_make_writable(pskb, (*pskb)->len))
  198. return 0;
  199. if (rep_len > match_len &&
  200. rep_len - match_len > skb_tailroom(*pskb) &&
  201. !enlarge_skb(pskb, rep_len - match_len))
  202. return 0;
  203. iph = (*pskb)->nh.iph;
  204. udph = (void *)iph + iph->ihl*4;
  205. oldlen = (*pskb)->len - iph->ihl*4;
  206. mangle_contents(*pskb, iph->ihl*4 + sizeof(*udph),
  207. match_offset, match_len, rep_buffer, rep_len);
  208. /* update the length of the UDP packet */
  209. datalen = (*pskb)->len - iph->ihl*4;
  210. udph->len = htons(datalen);
  211. /* fix udp checksum if udp checksum was previously calculated */
  212. if (!udph->check && (*pskb)->ip_summed != CHECKSUM_PARTIAL)
  213. return 1;
  214. if ((*pskb)->ip_summed != CHECKSUM_PARTIAL) {
  215. udph->check = 0;
  216. udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
  217. datalen, IPPROTO_UDP,
  218. csum_partial((char *)udph,
  219. datalen, 0));
  220. if (!udph->check)
  221. udph->check = CSUM_MANGLED_0;
  222. } else
  223. nf_proto_csum_replace2(&udph->check, *pskb,
  224. htons(oldlen), htons(datalen), 1);
  225. return 1;
  226. }
  227. EXPORT_SYMBOL(nf_nat_mangle_udp_packet);
  228. /* Adjust one found SACK option including checksum correction */
  229. static void
  230. sack_adjust(struct sk_buff *skb,
  231. struct tcphdr *tcph,
  232. unsigned int sackoff,
  233. unsigned int sackend,
  234. struct nf_nat_seq *natseq)
  235. {
  236. while (sackoff < sackend) {
  237. struct tcp_sack_block_wire *sack;
  238. __be32 new_start_seq, new_end_seq;
  239. sack = (void *)skb->data + sackoff;
  240. if (after(ntohl(sack->start_seq) - natseq->offset_before,
  241. natseq->correction_pos))
  242. new_start_seq = htonl(ntohl(sack->start_seq)
  243. - natseq->offset_after);
  244. else
  245. new_start_seq = htonl(ntohl(sack->start_seq)
  246. - natseq->offset_before);
  247. if (after(ntohl(sack->end_seq) - natseq->offset_before,
  248. natseq->correction_pos))
  249. new_end_seq = htonl(ntohl(sack->end_seq)
  250. - natseq->offset_after);
  251. else
  252. new_end_seq = htonl(ntohl(sack->end_seq)
  253. - natseq->offset_before);
  254. DEBUGP("sack_adjust: start_seq: %d->%d, end_seq: %d->%d\n",
  255. ntohl(sack->start_seq), new_start_seq,
  256. ntohl(sack->end_seq), new_end_seq);
  257. nf_proto_csum_replace4(&tcph->check, skb,
  258. sack->start_seq, new_start_seq, 0);
  259. nf_proto_csum_replace4(&tcph->check, skb,
  260. sack->end_seq, new_end_seq, 0);
  261. sack->start_seq = new_start_seq;
  262. sack->end_seq = new_end_seq;
  263. sackoff += sizeof(*sack);
  264. }
  265. }
  266. /* TCP SACK sequence number adjustment */
  267. static inline unsigned int
  268. nf_nat_sack_adjust(struct sk_buff **pskb,
  269. struct tcphdr *tcph,
  270. struct nf_conn *ct,
  271. enum ip_conntrack_info ctinfo)
  272. {
  273. unsigned int dir, optoff, optend;
  274. struct nf_conn_nat *nat = nfct_nat(ct);
  275. optoff = (*pskb)->nh.iph->ihl*4 + sizeof(struct tcphdr);
  276. optend = (*pskb)->nh.iph->ihl*4 + tcph->doff*4;
  277. if (!skb_make_writable(pskb, optend))
  278. return 0;
  279. dir = CTINFO2DIR(ctinfo);
  280. while (optoff < optend) {
  281. /* Usually: option, length. */
  282. unsigned char *op = (*pskb)->data + optoff;
  283. switch (op[0]) {
  284. case TCPOPT_EOL:
  285. return 1;
  286. case TCPOPT_NOP:
  287. optoff++;
  288. continue;
  289. default:
  290. /* no partial options */
  291. if (optoff + 1 == optend ||
  292. optoff + op[1] > optend ||
  293. op[1] < 2)
  294. return 0;
  295. if (op[0] == TCPOPT_SACK &&
  296. op[1] >= 2+TCPOLEN_SACK_PERBLOCK &&
  297. ((op[1] - 2) % TCPOLEN_SACK_PERBLOCK) == 0)
  298. sack_adjust(*pskb, tcph, optoff+2,
  299. optoff+op[1],
  300. &nat->info.seq[!dir]);
  301. optoff += op[1];
  302. }
  303. }
  304. return 1;
  305. }
  306. /* TCP sequence number adjustment. Returns 1 on success, 0 on failure */
  307. int
  308. nf_nat_seq_adjust(struct sk_buff **pskb,
  309. struct nf_conn *ct,
  310. enum ip_conntrack_info ctinfo)
  311. {
  312. struct tcphdr *tcph;
  313. int dir;
  314. __be32 newseq, newack;
  315. struct nf_conn_nat *nat = nfct_nat(ct);
  316. struct nf_nat_seq *this_way, *other_way;
  317. dir = CTINFO2DIR(ctinfo);
  318. this_way = &nat->info.seq[dir];
  319. other_way = &nat->info.seq[!dir];
  320. if (!skb_make_writable(pskb, (*pskb)->nh.iph->ihl*4+sizeof(*tcph)))
  321. return 0;
  322. tcph = (void *)(*pskb)->data + (*pskb)->nh.iph->ihl*4;
  323. if (after(ntohl(tcph->seq), this_way->correction_pos))
  324. newseq = htonl(ntohl(tcph->seq) + this_way->offset_after);
  325. else
  326. newseq = htonl(ntohl(tcph->seq) + this_way->offset_before);
  327. if (after(ntohl(tcph->ack_seq) - other_way->offset_before,
  328. other_way->correction_pos))
  329. newack = htonl(ntohl(tcph->ack_seq) - other_way->offset_after);
  330. else
  331. newack = htonl(ntohl(tcph->ack_seq) - other_way->offset_before);
  332. nf_proto_csum_replace4(&tcph->check, *pskb, tcph->seq, newseq, 0);
  333. nf_proto_csum_replace4(&tcph->check, *pskb, tcph->ack_seq, newack, 0);
  334. DEBUGP("Adjusting sequence number from %u->%u, ack from %u->%u\n",
  335. ntohl(tcph->seq), ntohl(newseq), ntohl(tcph->ack_seq),
  336. ntohl(newack));
  337. tcph->seq = newseq;
  338. tcph->ack_seq = newack;
  339. if (!nf_nat_sack_adjust(pskb, tcph, ct, ctinfo))
  340. return 0;
  341. nf_conntrack_tcp_update(*pskb, (*pskb)->nh.iph->ihl*4, ct, dir);
  342. return 1;
  343. }
  344. EXPORT_SYMBOL(nf_nat_seq_adjust);
  345. /* Setup NAT on this expected conntrack so it follows master. */
  346. /* If we fail to get a free NAT slot, we'll get dropped on confirm */
  347. void nf_nat_follow_master(struct nf_conn *ct,
  348. struct nf_conntrack_expect *exp)
  349. {
  350. struct nf_nat_range range;
  351. /* This must be a fresh one. */
  352. BUG_ON(ct->status & IPS_NAT_DONE_MASK);
  353. /* Change src to where master sends to */
  354. range.flags = IP_NAT_RANGE_MAP_IPS;
  355. range.min_ip = range.max_ip
  356. = ct->master->tuplehash[!exp->dir].tuple.dst.u3.ip;
  357. /* hook doesn't matter, but it has to do source manip */
  358. nf_nat_setup_info(ct, &range, NF_IP_POST_ROUTING);
  359. /* For DST manip, map port here to where it's expected. */
  360. range.flags = (IP_NAT_RANGE_MAP_IPS | IP_NAT_RANGE_PROTO_SPECIFIED);
  361. range.min = range.max = exp->saved_proto;
  362. range.min_ip = range.max_ip
  363. = ct->master->tuplehash[!exp->dir].tuple.src.u3.ip;
  364. /* hook doesn't matter, but it has to do destination manip */
  365. nf_nat_setup_info(ct, &range, NF_IP_PRE_ROUTING);
  366. }
  367. EXPORT_SYMBOL(nf_nat_follow_master);