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