nat: refactor mss_clamping to not depend on snat_main_t
[vpp.git] / src / plugins / nat / lib / nat_inlines.h
1 /*
2  * Copyright (c) 2020 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vnet/tcp/tcp_packet.h>
17 #include <vnet/ip/ip4_packet.h>
18
19 always_inline void
20 mss_clamping (u16 mss_clamping, tcp_header_t * tcp, ip_csum_t * sum)
21 {
22   u8 *data;
23   u8 opt_len, opts_len, kind;
24   u16 mss;
25
26   if (!(mss_clamping && tcp_syn (tcp)))
27     return;
28
29   opts_len = (tcp_doff (tcp) << 2) - sizeof (tcp_header_t);
30   data = (u8 *) (tcp + 1);
31   for (; opts_len > 0; opts_len -= opt_len, data += opt_len)
32     {
33       kind = data[0];
34
35       if (kind == TCP_OPTION_EOL)
36         break;
37       else if (kind == TCP_OPTION_NOOP)
38         {
39           opt_len = 1;
40           continue;
41         }
42       else
43         {
44           if (opts_len < 2)
45             return;
46           opt_len = data[1];
47
48           if (opt_len < 2 || opt_len > opts_len)
49             return;
50         }
51
52       if (kind == TCP_OPTION_MSS)
53         {
54           mss = *(u16 *) (data + 2);
55           if (clib_net_to_host_u16 (mss) > mss_clamping)
56             {
57               u16 mss_value_net = clib_host_to_net_u16(mss_clamping);
58               *sum =
59                 ip_csum_update (*sum, mss, mss_value_net, ip4_header_t,
60                                 length);
61               clib_memcpy_fast (data + 2, &mss_value_net, 2);
62             }
63           return;
64         }
65     }
66 }