nf_conntrack_seqadj.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #include <linux/types.h>
  2. #include <linux/netfilter.h>
  3. #include <net/tcp.h>
  4. #include <net/netfilter/nf_conntrack.h>
  5. #include <net/netfilter/nf_conntrack_extend.h>
  6. #include <net/netfilter/nf_conntrack_seqadj.h>
  7. int nf_ct_seqadj_init(struct nf_conn *ct, enum ip_conntrack_info ctinfo,
  8. s32 off)
  9. {
  10. enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
  11. struct nf_conn_seqadj *seqadj;
  12. struct nf_ct_seqadj *this_way;
  13. if (off == 0)
  14. return 0;
  15. set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
  16. seqadj = nfct_seqadj(ct);
  17. this_way = &seqadj->seq[dir];
  18. this_way->offset_before = off;
  19. this_way->offset_after = off;
  20. return 0;
  21. }
  22. EXPORT_SYMBOL_GPL(nf_ct_seqadj_init);
  23. int nf_ct_seqadj_set(struct nf_conn *ct, enum ip_conntrack_info ctinfo,
  24. __be32 seq, s32 off)
  25. {
  26. struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
  27. enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
  28. struct nf_ct_seqadj *this_way;
  29. if (off == 0)
  30. return 0;
  31. if (unlikely(!seqadj)) {
  32. WARN_ONCE(1, "Missing nfct_seqadj_ext_add() setup call\n");
  33. return 0;
  34. }
  35. set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
  36. spin_lock_bh(&ct->lock);
  37. this_way = &seqadj->seq[dir];
  38. if (this_way->offset_before == this_way->offset_after ||
  39. before(this_way->correction_pos, ntohl(seq))) {
  40. this_way->correction_pos = ntohl(seq);
  41. this_way->offset_before = this_way->offset_after;
  42. this_way->offset_after += off;
  43. }
  44. spin_unlock_bh(&ct->lock);
  45. return 0;
  46. }
  47. EXPORT_SYMBOL_GPL(nf_ct_seqadj_set);
  48. void nf_ct_tcp_seqadj_set(struct sk_buff *skb,
  49. struct nf_conn *ct, enum ip_conntrack_info ctinfo,
  50. s32 off)
  51. {
  52. const struct tcphdr *th;
  53. if (nf_ct_protonum(ct) != IPPROTO_TCP)
  54. return;
  55. th = (struct tcphdr *)(skb_network_header(skb) + ip_hdrlen(skb));
  56. nf_ct_seqadj_set(ct, ctinfo, th->seq, off);
  57. }
  58. EXPORT_SYMBOL_GPL(nf_ct_tcp_seqadj_set);
  59. /* Adjust one found SACK option including checksum correction */
  60. static void nf_ct_sack_block_adjust(struct sk_buff *skb,
  61. struct tcphdr *tcph,
  62. unsigned int sackoff,
  63. unsigned int sackend,
  64. struct nf_ct_seqadj *seq)
  65. {
  66. while (sackoff < sackend) {
  67. struct tcp_sack_block_wire *sack;
  68. __be32 new_start_seq, new_end_seq;
  69. sack = (void *)skb->data + sackoff;
  70. if (after(ntohl(sack->start_seq) - seq->offset_before,
  71. seq->correction_pos))
  72. new_start_seq = htonl(ntohl(sack->start_seq) -
  73. seq->offset_after);
  74. else
  75. new_start_seq = htonl(ntohl(sack->start_seq) -
  76. seq->offset_before);
  77. if (after(ntohl(sack->end_seq) - seq->offset_before,
  78. seq->correction_pos))
  79. new_end_seq = htonl(ntohl(sack->end_seq) -
  80. seq->offset_after);
  81. else
  82. new_end_seq = htonl(ntohl(sack->end_seq) -
  83. seq->offset_before);
  84. pr_debug("sack_adjust: start_seq: %u->%u, end_seq: %u->%u\n",
  85. ntohl(sack->start_seq), ntohl(new_start_seq),
  86. ntohl(sack->end_seq), ntohl(new_end_seq));
  87. inet_proto_csum_replace4(&tcph->check, skb,
  88. sack->start_seq, new_start_seq, false);
  89. inet_proto_csum_replace4(&tcph->check, skb,
  90. sack->end_seq, new_end_seq, false);
  91. sack->start_seq = new_start_seq;
  92. sack->end_seq = new_end_seq;
  93. sackoff += sizeof(*sack);
  94. }
  95. }
  96. /* TCP SACK sequence number adjustment */
  97. static unsigned int nf_ct_sack_adjust(struct sk_buff *skb,
  98. unsigned int protoff,
  99. struct tcphdr *tcph,
  100. struct nf_conn *ct,
  101. enum ip_conntrack_info ctinfo)
  102. {
  103. unsigned int dir, optoff, optend;
  104. struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
  105. optoff = protoff + sizeof(struct tcphdr);
  106. optend = protoff + tcph->doff * 4;
  107. if (!skb_make_writable(skb, optend))
  108. return 0;
  109. dir = CTINFO2DIR(ctinfo);
  110. while (optoff < optend) {
  111. /* Usually: option, length. */
  112. unsigned char *op = skb->data + optoff;
  113. switch (op[0]) {
  114. case TCPOPT_EOL:
  115. return 1;
  116. case TCPOPT_NOP:
  117. optoff++;
  118. continue;
  119. default:
  120. /* no partial options */
  121. if (optoff + 1 == optend ||
  122. optoff + op[1] > optend ||
  123. op[1] < 2)
  124. return 0;
  125. if (op[0] == TCPOPT_SACK &&
  126. op[1] >= 2+TCPOLEN_SACK_PERBLOCK &&
  127. ((op[1] - 2) % TCPOLEN_SACK_PERBLOCK) == 0)
  128. nf_ct_sack_block_adjust(skb, tcph, optoff + 2,
  129. optoff+op[1],
  130. &seqadj->seq[!dir]);
  131. optoff += op[1];
  132. }
  133. }
  134. return 1;
  135. }
  136. /* TCP sequence number adjustment. Returns 1 on success, 0 on failure */
  137. int nf_ct_seq_adjust(struct sk_buff *skb,
  138. struct nf_conn *ct, enum ip_conntrack_info ctinfo,
  139. unsigned int protoff)
  140. {
  141. enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
  142. struct tcphdr *tcph;
  143. __be32 newseq, newack;
  144. s32 seqoff, ackoff;
  145. struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
  146. struct nf_ct_seqadj *this_way, *other_way;
  147. int res;
  148. this_way = &seqadj->seq[dir];
  149. other_way = &seqadj->seq[!dir];
  150. if (!skb_make_writable(skb, protoff + sizeof(*tcph)))
  151. return 0;
  152. tcph = (void *)skb->data + protoff;
  153. spin_lock_bh(&ct->lock);
  154. if (after(ntohl(tcph->seq), this_way->correction_pos))
  155. seqoff = this_way->offset_after;
  156. else
  157. seqoff = this_way->offset_before;
  158. if (after(ntohl(tcph->ack_seq) - other_way->offset_before,
  159. other_way->correction_pos))
  160. ackoff = other_way->offset_after;
  161. else
  162. ackoff = other_way->offset_before;
  163. newseq = htonl(ntohl(tcph->seq) + seqoff);
  164. newack = htonl(ntohl(tcph->ack_seq) - ackoff);
  165. inet_proto_csum_replace4(&tcph->check, skb, tcph->seq, newseq, false);
  166. inet_proto_csum_replace4(&tcph->check, skb, tcph->ack_seq, newack,
  167. false);
  168. pr_debug("Adjusting sequence number from %u->%u, ack from %u->%u\n",
  169. ntohl(tcph->seq), ntohl(newseq), ntohl(tcph->ack_seq),
  170. ntohl(newack));
  171. tcph->seq = newseq;
  172. tcph->ack_seq = newack;
  173. res = nf_ct_sack_adjust(skb, protoff, tcph, ct, ctinfo);
  174. spin_unlock_bh(&ct->lock);
  175. return res;
  176. }
  177. EXPORT_SYMBOL_GPL(nf_ct_seq_adjust);
  178. s32 nf_ct_seq_offset(const struct nf_conn *ct,
  179. enum ip_conntrack_dir dir,
  180. u32 seq)
  181. {
  182. struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
  183. struct nf_ct_seqadj *this_way;
  184. if (!seqadj)
  185. return 0;
  186. this_way = &seqadj->seq[dir];
  187. return after(seq, this_way->correction_pos) ?
  188. this_way->offset_after : this_way->offset_before;
  189. }
  190. EXPORT_SYMBOL_GPL(nf_ct_seq_offset);
  191. static struct nf_ct_ext_type nf_ct_seqadj_extend __read_mostly = {
  192. .len = sizeof(struct nf_conn_seqadj),
  193. .align = __alignof__(struct nf_conn_seqadj),
  194. .id = NF_CT_EXT_SEQADJ,
  195. };
  196. int nf_conntrack_seqadj_init(void)
  197. {
  198. return nf_ct_extend_register(&nf_ct_seqadj_extend);
  199. }
  200. void nf_conntrack_seqadj_fini(void)
  201. {
  202. nf_ct_extend_unregister(&nf_ct_seqadj_extend);
  203. }