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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef __included_lib_nat_inlines_h__
17 #define __included_lib_nat_inlines_h__
19 #include <vnet/tcp/tcp_packet.h>
20 #include <vnet/ip/ip4_packet.h>
23 increment_v4_address (ip4_address_t * a)
27 v = clib_net_to_host_u32 (a->as_u32) + 1;
28 a->as_u32 = clib_host_to_net_u32 (v);
32 mss_clamping (u16 mss_clamping, tcp_header_t * tcp, ip_csum_t * sum)
35 u8 opt_len, opts_len, kind;
38 if (!(mss_clamping && tcp_syn (tcp)))
41 opts_len = (tcp_doff (tcp) << 2) - sizeof (tcp_header_t);
42 data = (u8 *) (tcp + 1);
43 for (; opts_len > 0; opts_len -= opt_len, data += opt_len)
47 if (kind == TCP_OPTION_EOL)
49 else if (kind == TCP_OPTION_NOOP)
60 if (opt_len < 2 || opt_len > opts_len)
64 if (kind == TCP_OPTION_MSS)
66 mss = *(u16 *) (data + 2);
67 if (clib_net_to_host_u16 (mss) > mss_clamping)
69 u16 mss_value_net = clib_host_to_net_u16 (mss_clamping);
71 ip_csum_update (*sum, mss, mss_value_net, ip4_header_t,
73 clib_memcpy_fast (data + 2, &mss_value_net, 2);
80 static_always_inline u16
81 nat_random_port (u32 *random_seed, u16 min, u16 max)
86 rwide = random_u32 (random_seed);
88 if (r >= min && r <= max)
91 return min + (rwide % (max - min + 1));
94 #endif /* __included_lib_nat_inlines_h__ */
97 * fd.io coding-style-patch-verification: ON
100 * eval: (c-set-style "gnu")