nat: Include platform specific headers on FreeBSD
[vpp.git] / src / plugins / nat / pnat / tests / pnat_test_stubs.h
1 /*
2  * Copyright (c) 2021 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 #ifndef included_pnat_test_stubs_h
17 #define included_pnat_test_stubs_h
18
19 void os_panic(void) {}
20 void os_exit(int code) {}
21 u32 ip4_fib_table_get_index_for_sw_if_index(u32 sw_if_index) { return 0; }
22 #include <vnet/feature/feature.h>
23 vnet_feature_main_t feature_main;
24 void classify_get_trace_chain(void){};
25
26 /* Format an IP4 address. */
27 u8 *format_ip4_address(u8 *s, va_list *args) {
28     u8 *a = va_arg(*args, u8 *);
29     return format(s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
30 }
31
32 u8 *format_pnat_match_tuple(u8 *s, va_list *args) { return 0; }
33 u8 *format_pnat_rewrite_tuple(u8 *s, va_list *args) { return 0; }
34
35 vlib_error_desc_t pnat_error_counters[10];
36
37 int ip4_sv_reass_enable_disable_with_refcnt(u32 sw_if_index, int is_enable) {
38     return 0;
39 }
40 int ip4_sv_reass_output_enable_disable_with_refcnt(u32 sw_if_index,
41                                                    int is_enable) {
42     return 0;
43 }
44 int vnet_feature_enable_disable(const char *arc_name, const char *node_name,
45                                 u32 sw_if_index, int enable_disable,
46                                 void *feature_config,
47                                 u32 n_feature_config_bytes) {
48     return 0;
49 }
50 vnet_main_t *vnet_get_main(void) { return 0; }
51
52 /* Compute TCP/UDP/ICMP4 checksum in software. */
53 u16 ip4_tcp_udp_compute_checksum(vlib_main_t *vm, vlib_buffer_t *p0,
54                                  ip4_header_t *ip0) {
55     ip_csum_t sum0;
56     u32 ip_header_length, payload_length_host_byte_order;
57
58     /* Initialize checksum with ip header. */
59     ip_header_length = ip4_header_bytes(ip0);
60     payload_length_host_byte_order =
61         clib_net_to_host_u16(ip0->length) - ip_header_length;
62     sum0 = clib_host_to_net_u32(payload_length_host_byte_order +
63                                 (ip0->protocol << 16));
64
65     if (BITS(uword) == 32) {
66         sum0 = ip_csum_with_carry(sum0,
67                                   clib_mem_unaligned(&ip0->src_address, u32));
68         sum0 = ip_csum_with_carry(sum0,
69                                   clib_mem_unaligned(&ip0->dst_address, u32));
70     } else
71         sum0 = ip_csum_with_carry(sum0,
72                                   clib_mem_unaligned(&ip0->src_address, u64));
73     return ip_calculate_l4_checksum(vm, p0, sum0,
74                                     payload_length_host_byte_order, (u8 *)ip0,
75                                     ip_header_length, NULL);
76 }
77
78 u32 ip4_tcp_udp_validate_checksum(vlib_main_t *vm, vlib_buffer_t *p0) {
79     ip4_header_t *ip0 = vlib_buffer_get_current(p0);
80     udp_header_t *udp0;
81     u16 sum16;
82
83     ASSERT(ip0->protocol == IP_PROTOCOL_TCP ||
84            ip0->protocol == IP_PROTOCOL_UDP);
85
86     udp0 = (void *)(ip0 + 1);
87     if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0) {
88         p0->flags |= (VNET_BUFFER_F_L4_CHECKSUM_COMPUTED |
89                       VNET_BUFFER_F_L4_CHECKSUM_CORRECT);
90         return p0->flags;
91     }
92
93     sum16 = ip4_tcp_udp_compute_checksum(vm, p0, ip0);
94
95     p0->flags |= (VNET_BUFFER_F_L4_CHECKSUM_COMPUTED |
96                   ((sum16 == 0) << VNET_BUFFER_F_LOG2_L4_CHECKSUM_CORRECT));
97
98     return p0->flags;
99 }
100 u8 *format_tcp_header(u8 *s, va_list *args) {
101     tcp_header_t *tcp = va_arg(*args, tcp_header_t *);
102     u32 max_header_bytes = va_arg(*args, u32);
103     u32 header_bytes;
104     u32 indent;
105
106     /* Nothing to do. */
107     if (max_header_bytes < sizeof(tcp[0]))
108         return format(s, "TCP header truncated");
109
110     indent = format_get_indent(s);
111     indent += 2;
112     header_bytes = tcp_header_bytes(tcp);
113
114     s = format(s, "TCP: %d -> %d", clib_net_to_host_u16(tcp->src),
115                clib_net_to_host_u16(tcp->dst));
116
117     s = format(s, "\n%Useq. 0x%08x ack 0x%08x", format_white_space, indent,
118                clib_net_to_host_u32(tcp->seq_number),
119                clib_net_to_host_u32(tcp->ack_number));
120
121     s = format(s, "\n%Utcp header: %d bytes", format_white_space, indent,
122                tcp->flags, header_bytes);
123
124     s = format(s, "\n%Uwindow %d, checksum 0x%04x", format_white_space, indent,
125                clib_net_to_host_u16(tcp->window),
126                clib_net_to_host_u16(tcp->checksum));
127     return s;
128 }
129 /* Format UDP header. */
130 u8 *format_udp_header(u8 *s, va_list *args) {
131     udp_header_t *udp = va_arg(*args, udp_header_t *);
132     u32 max_header_bytes = va_arg(*args, u32);
133     u32 indent;
134
135     /* Nothing to do. */
136     if (max_header_bytes < sizeof(udp[0]))
137         return format(s, "UDP header truncated");
138
139     indent = format_get_indent(s);
140     indent += 2;
141
142     s = format(s, "UDP: %d -> %d", clib_net_to_host_u16(udp->src_port),
143                clib_net_to_host_u16(udp->dst_port));
144
145     s = format(s, "\n%Ulength %d, checksum 0x%04x", format_white_space, indent,
146                clib_net_to_host_u16(udp->length),
147                clib_net_to_host_u16(udp->checksum));
148
149     return s;
150 }
151
152 /* Format an IP4 header. */
153 u8 *format_ip4_header(u8 *s, va_list *args) {
154     ip4_header_t *ip = va_arg(*args, ip4_header_t *);
155     u32 max_header_bytes = va_arg(*args, u32);
156     u32 ip_version, header_bytes;
157     u32 indent;
158
159     /* Nothing to do. */
160     if (max_header_bytes < sizeof(ip[0]))
161         return format(s, "IP header truncated");
162
163     indent = format_get_indent(s);
164     indent += 2;
165
166     ip_version = (ip->ip_version_and_header_length >> 4);
167     header_bytes = (ip->ip_version_and_header_length & 0xf) * sizeof(u32);
168
169     s = format(s, "%d: %U -> %U", ip->protocol, format_ip4_address,
170                ip->src_address.data, format_ip4_address, ip->dst_address.data);
171
172     /* Show IP version and header length only with unexpected values. */
173     if (ip_version != 4 || header_bytes != sizeof(ip4_header_t))
174         s = format(s, "\n%Uversion %d, header length %d", format_white_space,
175                    indent, ip_version, header_bytes);
176
177     s = format(s, "\n%Utos 0x%02x, ttl %d, length %d, checksum 0x%04x",
178                format_white_space, indent, ip->tos, ip->ttl,
179                clib_net_to_host_u16(ip->length),
180                clib_net_to_host_u16(ip->checksum));
181
182     /* Check and report invalid checksums. */
183     {
184         if (!ip4_header_checksum_is_valid(ip))
185             s = format(s, " (should be 0x%04x)",
186                        clib_net_to_host_u16(ip4_header_checksum(ip)));
187     }
188
189     {
190         u32 f = clib_net_to_host_u16(ip->flags_and_fragment_offset);
191         u32 o;
192
193         s = format(s, "\n%Ufragment id 0x%04x", format_white_space, indent,
194                    clib_net_to_host_u16(ip->fragment_id));
195
196         /* Fragment offset. */
197         o = 8 * (f & 0x1fff);
198         f ^= f & 0x1fff;
199         if (o != 0)
200             s = format(s, " offset %d", o);
201
202         if (f != 0) {
203             s = format(s, ", flags ");
204 #define _(l)                                                                   \
205     if (f & IP4_HEADER_FLAG_##l)                                               \
206         s = format(s, #l);
207             _(MORE_FRAGMENTS);
208             _(DONT_FRAGMENT);
209             _(CONGESTION);
210 #undef _
211         }
212         /* Fragment packet but not the first. */
213         if (o != 0)
214             return s;
215     }
216
217     return s;
218 }
219
220 #endif