~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/netfilter/nf_conntrack_seqadj.c

Version: ~ [ linux-5.10-rc5 ] ~ [ linux-5.9.10 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.79 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.159 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.208 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.245 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.245 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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         if (unlikely(!seqadj)) {
 40                 WARN_ONCE(1, "Missing nfct_seqadj_ext_add() setup call\n");
 41                 return 0;
 42         }
 43 
 44         set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
 45 
 46         spin_lock_bh(&ct->lock);
 47         this_way = &seqadj->seq[dir];
 48         if (this_way->offset_before == this_way->offset_after ||
 49             before(this_way->correction_pos, ntohl(seq))) {
 50                 this_way->correction_pos = ntohl(seq);
 51                 this_way->offset_before  = this_way->offset_after;
 52                 this_way->offset_after  += off;
 53         }
 54         spin_unlock_bh(&ct->lock);
 55         return 0;
 56 }
 57 EXPORT_SYMBOL_GPL(nf_ct_seqadj_set);
 58 
 59 void nf_ct_tcp_seqadj_set(struct sk_buff *skb,
 60                           struct nf_conn *ct, enum ip_conntrack_info ctinfo,
 61                           s32 off)
 62 {
 63         const struct tcphdr *th;
 64 
 65         if (nf_ct_protonum(ct) != IPPROTO_TCP)
 66                 return;
 67 
 68         th = (struct tcphdr *)(skb_network_header(skb) + ip_hdrlen(skb));
 69         nf_ct_seqadj_set(ct, ctinfo, th->seq, off);
 70 }
 71 EXPORT_SYMBOL_GPL(nf_ct_tcp_seqadj_set);
 72 
 73 /* Adjust one found SACK option including checksum correction */
 74 static void nf_ct_sack_block_adjust(struct sk_buff *skb,
 75                                     struct tcphdr *tcph,
 76                                     unsigned int sackoff,
 77                                     unsigned int sackend,
 78                                     struct nf_ct_seqadj *seq)
 79 {
 80         while (sackoff < sackend) {
 81                 struct tcp_sack_block_wire *sack;
 82                 __be32 new_start_seq, new_end_seq;
 83 
 84                 sack = (void *)skb->data + sackoff;
 85                 if (after(ntohl(sack->start_seq) - seq->offset_before,
 86                           seq->correction_pos))
 87                         new_start_seq = htonl(ntohl(sack->start_seq) -
 88                                         seq->offset_after);
 89                 else
 90                         new_start_seq = htonl(ntohl(sack->start_seq) -
 91                                         seq->offset_before);
 92 
 93                 if (after(ntohl(sack->end_seq) - seq->offset_before,
 94                           seq->correction_pos))
 95                         new_end_seq = htonl(ntohl(sack->end_seq) -
 96                                       seq->offset_after);
 97                 else
 98                         new_end_seq = htonl(ntohl(sack->end_seq) -
 99                                       seq->offset_before);
100 
101                 pr_debug("sack_adjust: start_seq: %u->%u, end_seq: %u->%u\n",
102                          ntohl(sack->start_seq), ntohl(new_start_seq),
103                          ntohl(sack->end_seq), ntohl(new_end_seq));
104 
105                 inet_proto_csum_replace4(&tcph->check, skb,
106                                          sack->start_seq, new_start_seq, false);
107                 inet_proto_csum_replace4(&tcph->check, skb,
108                                          sack->end_seq, new_end_seq, false);
109                 sack->start_seq = new_start_seq;
110                 sack->end_seq = new_end_seq;
111                 sackoff += sizeof(*sack);
112         }
113 }
114 
115 /* TCP SACK sequence number adjustment */
116 static unsigned int nf_ct_sack_adjust(struct sk_buff *skb,
117                                       unsigned int protoff,
118                                       struct tcphdr *tcph,
119                                       struct nf_conn *ct,
120                                       enum ip_conntrack_info ctinfo)
121 {
122         unsigned int dir, optoff, optend;
123         struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
124 
125         optoff = protoff + sizeof(struct tcphdr);
126         optend = protoff + tcph->doff * 4;
127 
128         if (!skb_make_writable(skb, optend))
129                 return 0;
130 
131         dir = CTINFO2DIR(ctinfo);
132 
133         while (optoff < optend) {
134                 /* Usually: option, length. */
135                 unsigned char *op = skb->data + optoff;
136 
137                 switch (op[0]) {
138                 case TCPOPT_EOL:
139                         return 1;
140                 case TCPOPT_NOP:
141                         optoff++;
142                         continue;
143                 default:
144                         /* no partial options */
145                         if (optoff + 1 == optend ||
146                             optoff + op[1] > optend ||
147                             op[1] < 2)
148                                 return 0;
149                         if (op[0] == TCPOPT_SACK &&
150                             op[1] >= 2+TCPOLEN_SACK_PERBLOCK &&
151                             ((op[1] - 2) % TCPOLEN_SACK_PERBLOCK) == 0)
152                                 nf_ct_sack_block_adjust(skb, tcph, optoff + 2,
153                                                         optoff+op[1],
154                                                         &seqadj->seq[!dir]);
155                         optoff += op[1];
156                 }
157         }
158         return 1;
159 }
160 
161 /* TCP sequence number adjustment.  Returns 1 on success, 0 on failure */
162 int nf_ct_seq_adjust(struct sk_buff *skb,
163                      struct nf_conn *ct, enum ip_conntrack_info ctinfo,
164                      unsigned int protoff)
165 {
166         enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
167         struct tcphdr *tcph;
168         __be32 newseq, newack;
169         s32 seqoff, ackoff;
170         struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
171         struct nf_ct_seqadj *this_way, *other_way;
172         int res = 1;
173 
174         this_way  = &seqadj->seq[dir];
175         other_way = &seqadj->seq[!dir];
176 
177         if (!skb_make_writable(skb, protoff + sizeof(*tcph)))
178                 return 0;
179 
180         tcph = (void *)skb->data + protoff;
181         spin_lock_bh(&ct->lock);
182         if (after(ntohl(tcph->seq), this_way->correction_pos))
183                 seqoff = this_way->offset_after;
184         else
185                 seqoff = this_way->offset_before;
186 
187         newseq = htonl(ntohl(tcph->seq) + seqoff);
188         inet_proto_csum_replace4(&tcph->check, skb, tcph->seq, newseq, false);
189         pr_debug("Adjusting sequence number from %u->%u\n",
190                  ntohl(tcph->seq), ntohl(newseq));
191         tcph->seq = newseq;
192 
193         if (!tcph->ack)
194                 goto out;
195 
196         if (after(ntohl(tcph->ack_seq) - other_way->offset_before,
197                   other_way->correction_pos))
198                 ackoff = other_way->offset_after;
199         else
200                 ackoff = other_way->offset_before;
201 
202         newack = htonl(ntohl(tcph->ack_seq) - ackoff);
203         inet_proto_csum_replace4(&tcph->check, skb, tcph->ack_seq, newack,
204                                  false);
205         pr_debug("Adjusting ack number from %u->%u, ack from %u->%u\n",
206                  ntohl(tcph->seq), ntohl(newseq), ntohl(tcph->ack_seq),
207                  ntohl(newack));
208         tcph->ack_seq = newack;
209 
210         res = nf_ct_sack_adjust(skb, protoff, tcph, ct, ctinfo);
211 out:
212         spin_unlock_bh(&ct->lock);
213 
214         return res;
215 }
216 EXPORT_SYMBOL_GPL(nf_ct_seq_adjust);
217 
218 s32 nf_ct_seq_offset(const struct nf_conn *ct,
219                      enum ip_conntrack_dir dir,
220                      u32 seq)
221 {
222         struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
223         struct nf_ct_seqadj *this_way;
224 
225         if (!seqadj)
226                 return 0;
227 
228         this_way = &seqadj->seq[dir];
229         return after(seq, this_way->correction_pos) ?
230                  this_way->offset_after : this_way->offset_before;
231 }
232 EXPORT_SYMBOL_GPL(nf_ct_seq_offset);
233 
234 static const struct nf_ct_ext_type nf_ct_seqadj_extend = {
235         .len    = sizeof(struct nf_conn_seqadj),
236         .align  = __alignof__(struct nf_conn_seqadj),
237         .id     = NF_CT_EXT_SEQADJ,
238 };
239 
240 int nf_conntrack_seqadj_init(void)
241 {
242         return nf_ct_extend_register(&nf_ct_seqadj_extend);
243 }
244 
245 void nf_conntrack_seqadj_fini(void)
246 {
247         nf_ct_extend_unregister(&nf_ct_seqadj_extend);
248 }
249 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp