summaryrefslogtreecommitdiffstats
path: root/plugins/vcgn-plugin/vcgn/cnat_v4_functions.c
blob: d3051fba5a7ba04b3dd45ab314cdd871ace6cd95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
 *---------------------------------------------------------------------------
 * cnat_v4_funtions.c
 *
 * Copyright (c) 2008-2013 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *---------------------------------------------------------------------------
 */
#include <vlib/vlib.h>
#include <vnet/vnet.h>
#include <vnet/pg/pg.h>
#include <vppinfra/error.h>


#include "tcp_header_definitions.h"
#include "cnat_db.h"
#include "cnat_config.h"
#include "cnat_v4_functions.h"
#include "dslite_defs.h"
#include "dslite_db.h"

static u32 tcp_logging_count;
static u32 tcp_logging_overflow;

static tcp_logging_struct_t tcp_logging_array[MAX_TCP_LOGGING_COUNT];

/*
 * Function to log TCP pkts checksum changes..
 */
void
tcp_debug_logging (
    u32 seq_num,
    u32 ack_num,
    u32 old_ip,
    u32 new_ip,
    u16 old_port,
    u16 new_port,
    u16 old_ip_crc,
    u16 new_ip_crc,
    u16 old_tcp_crc,
    u16 new_tcp_crc)
{
    tcp_logging_array[tcp_logging_count].seq_num      = seq_num;
    tcp_logging_array[tcp_logging_count].ack_num      = ack_num;
    tcp_logging_array[tcp_logging_count].old_ip       = old_ip;
    tcp_logging_array[tcp_logging_count].new_ip       = new_ip;
    tcp_logging_array[tcp_logging_count].old_port     = old_port;
    tcp_logging_array[tcp_logging_count].new_port     = new_port;
    tcp_logging_array[tcp_logging_count].old_ip_crc   = old_ip_crc;
    tcp_logging_array[tcp_logging_count].new_ip_crc   = new_ip_crc;
    tcp_logging_array[tcp_logging_count].old_tcp_crc  = old_tcp_crc;
    tcp_logging_array[tcp_logging_count].new_tcp_crc  = new_tcp_crc;

    tcp_logging_count++;

    if (tcp_logging_count >= MAX_TCP_LOGGING_COUNT) {
	tcp_logging_overflow = 1;
	tcp_logging_count    = 0;
    }
}

/*
 * Function to dmp TCP pkts logged..
 */
void
tcp_debug_logging_dump (void)
{
    u32 i, total_count, start_entry;

    if (tcp_logging_overflow) {
        total_count = MAX_TCP_LOGGING_COUNT;
        start_entry = tcp_logging_count;
        printf("Logging Entries Wrapped Around, displaying %d entries\n",
               total_count);
    } else {
        total_count = tcp_logging_count;
        start_entry = 0;
        printf("Displaying %d entries\n", total_count);
    }

    printf("SEQ ACK IP_O IP_N PORT_O PORT_N L3_CRC_O L3_CRC_N L4_CRC_O L4_CRC_N\n");

    for (i = 0; i < total_count; i++) {
        u32 entry = (i + start_entry) % MAX_TCP_LOGGING_COUNT;

        printf("%04d: 0x%08x 0x%08x 0x%08x 0x%08x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x\n",
               entry, 
               tcp_logging_array[entry].seq_num,
               tcp_logging_array[entry].ack_num,
               tcp_logging_array[entry].old_ip,
               tcp_logging_array[entry].new_ip,
               tcp_logging_array[entry].old_port,
               tcp_logging_array[entry].new_port,
               tcp_logging_array[entry].old_ip_crc,
               tcp_logging_array[entry].new_ip_crc,
               tcp_logging_array[entry].old_tcp_crc,
               tcp_logging_array[entry].new_tcp_crc);
    }
}

/*
 * Function to enable TCP logging
 */
void
tcp_debug_logging_enable_disable (u32 enable_flag)
{
    switch (enable_flag) { 

    case TCP_LOGGING_DISABLE:
        if (tcp_logging_enable_flag == TCP_LOGGING_DISABLE) {
            printf("\nTCP Logging ALREADY DISABLED\n");
        } else {
            printf("\nTCP Logging DISABLED\n");
        }
        tcp_logging_enable_flag = 0;
        break;

    case TCP_LOGGING_ENABLE:
        if (tcp_logging_enable_flag == TCP_LOGGING_ENABLE) {
            printf("\nTCP Logging ALREADY ENABLED\n");
        } else {
	    tcp_logging_enable_flag = 1;
	    tcp_logging_count    = 0;
	    tcp_logging_overflow = 0;

            printf("\nTCP Logging ENABLED\n");
        }
        break;

    case TCP_LOGGING_PACKET_DUMP:
        tcp_debug_logging_dump();
        break;

    case TCP_LOGGING_SUMMARY_DUMP:
    default:
        printf("\ntcp_logging_enable_flag %d, tcp_log_count %d\n",
               tcp_logging_enable_flag, tcp_logging_count);
        printf("To Enable TCP LOGGING provide a flag value of %d\n",
	       TCP_LOGGING_ENABLE);
        break;
    }
}

void hex_dump (u8 * p, int len) {
    int i;
    for (i=0;i<len;i++) {
        if(i && (i & 0x3 ) == 0) printf(" ");
        if(i && (i & 0xf ) == 0) printf("\n");
        PLATFORM_DEBUG_PRINT("%02X ", p[i]);
    }
    PLATFORM_DEBUG_PRINT("\n");
}

void
print_icmp_pkt (ipv4_header *ip)
{
    u32 i, total_len;

    u8 *pkt = (u8 *) ip;

    total_len = clib_net_to_host_u16(ip->total_len_bytes);

    printf("\n======== PRINTING PKT START======\n");
    printf("======== IP PACKET LEN %d ===========\n", total_len);
    for (i=0; i < 20; i++) {
       printf(" %02X ", *(pkt + i));
    }

    printf("\n======== ICMP HEADER =================\n");
    for (i=20; i < 28; i++) {
       printf(" %02X ", *(pkt + i));
    }

    printf("\n======== ICMP BODY ===================\n");
    for (i=28; i < total_len; i++) {
       printf(" %02X ", *(pkt + i));
    }

    printf("\n======== PRINTING PKT END =======\n");
}

void
print_udp_pkt (ipv4_header *ip)
{
    u32 i, total_len, udp_len;

    u8 *pkt = (u8 *) ip;

    total_len = clib_net_to_host_u16(ip->total_len_bytes);
    udp_len = total_len - 20;

    printf("\n======== PRINTING PKT START======\n");
    printf("======== IP PACKET LEN %d ===========\n", total_len);
    for (i=0; i < 20; i++) {
       printf(" %02X ", *(pkt + i));
    }
    printf("\n======== UDP PSEUDO HEADER ==========\n");
    for (i=12; i < 20; i++) {
       printf(" %02X ", *(pkt + i));
    }
    printf(" 00 11 %02X %02X ", udp_len >> 8, udp_len & 0xff);

    printf("\n======== UDP HEADER =================\n");
    for (i=20; i < 28; i++) {
       printf(" %02X ", *(pkt + i));
    }
    printf("\n======== UDP BODY ===================\n");
    for (i=28; i < total_len; i++) {
       printf(" %02X ", *(pkt + i));
    }

    printf("\n======== PRINTING PKT END =======\n");
}

void
print_tcp_pkt (ipv4_header *ip)
{
    u32 i, total_len, tcp_len;

    u8 *pkt = (u8 *) ip;

    total_len = clib_net_to_host_u16(ip->total_len_bytes);
    tcp_len = total_len - 20;

    printf("\n======== PRINTING PKT START======\n");
    printf("======== IP PACKET LEN %d ===========\n", total_len);
    for (i=0; i < 20; i++) {
       printf(" %02X ", *(pkt + i));
    }
    printf("\n======== TCP PSEUDO HEADER ==========\n");
    for (i=12; i < 20; i++) {
       printf(" %02X ", *(pkt + i));
    }
    printf(" 00 06 %02X %02X ", tcp_len >> 8, tcp_len & 0xff);

    printf("\n======== TCP HEADER =================\n");
    for (i=20; i < 40; i++) {
       printf(" %02X ", *(pkt + i));
    }
    printf("\n======== TCP BODY ===================\n");
    for (i=40; i < total_len; i++) {
       printf(" %02X ", *(pkt + i));
    }

    printf("\n======== PRINTING PKT END =======\n");
}

/* IN: ipv4 and tcp header pointer,
 *     new ipv4 addr and port value
 *     main db index for accessing per vrf mss value 
 * DO:
 *     NAT
 *     mss adjust if needed
 *     ip & tcp checksum update (incremental)
 */ 

inline void tcp_in2out_nat_mss_n_checksum (ipv4_header * ip, 
                                           tcp_hdr_type * tcp, 
                                           u32 ipv4_addr, 
                                           u16 port,
                                           cnat_main_db_entry_t * db)
{
    u8 *mss_ptr;
    u8 check_mss = 0;
    u16 mss_old, mss_new;
    cnat_vrfmap_t * vrf_map_p;

    cnat_v4_recalculate_tcp_checksum(ip,
                                     tcp,
                                     &(ip->src_addr),
                                     &(tcp->src_port),
                                     ipv4_addr,
                                     port);
    u16 frag_offset =
        clib_net_to_host_u16(ip->frag_flags_offset);

    if(PREDICT_FALSE(frag_offset & IP_FRAG_OFFSET_MASK)) {
        return; /* No TCP Header at all */
    }

    /*
     * check SYN bit and if options field is present
     * If yes, proceed to extract the options and get TCP MSS value
     */
    check_mss = ((tcp->flags & TCP_FLAG_SYN) && 
                 (((tcp->hdr_len>>4) << 2) > sizeof(tcp_hdr_type)));

    if (PREDICT_FALSE(check_mss)) {

	/* get per VRF mss config */
        if(PREDICT_FALSE(db->flags & (CNAT_DB_DSLITE_FLAG))) {
            mss_new = dslite_table_db_ptr[db->dslite_nat44_inst_id].tcp_mss;
        } else {
	    vrf_map_p = cnat_map_by_vrf + db->vrfmap_index;
	    mss_new = vrf_map_p->tcp_mss;
        }
        DSLITE_PRINTF(1, "Check MSS true..%u\n", mss_new);
	/*
	 * If TCP MSS is not configured, skip the MSS checks
	 */
	if (PREDICT_FALSE(mss_new != V4_TCP_MSS_NOT_CONFIGURED_VALUE)) {

	    /* if mss_ptr != NULL, then it points to MSS option */
	    mss_ptr = tcp_findoption(tcp, TCP_OPTION_MSS);

	    /* 
	     * TCP option field: | kind 1B | len 1B | value 2B|  
	     *    where kind != [0,1] 
	     */
	    if (PREDICT_TRUE(mss_ptr && (mss_ptr[1] == 4))) {

		u16 *ptr = (u16*)(mss_ptr + 2);

		mss_old = clib_net_to_host_u16(*ptr);

		if (PREDICT_FALSE(mss_old > mss_new)) {
		    u32 sum32;
		    u16 mss_old_r, old_tcp_checksum_r;

		    *ptr = clib_host_to_net_u16(mss_new);

		    mss_old_r = ~mss_old;

		    old_tcp_checksum_r =
		        ~clib_net_to_host_u16(tcp->tcp_checksum);

		    /*
		     * Revise the TCP checksum
		     */
		    sum32 = old_tcp_checksum_r + mss_old_r + mss_new;
		    FILL_CHECKSUM(tcp->tcp_checksum, sum32)

		    if (PREDICT_FALSE(tcp_logging_enable_flag)) {
                        tcp_debug_logging(
                            clib_net_to_host_u32(tcp->seq_num),
                            clib_net_to_host_u32(tcp->ack_num),
                            0,
                            0,
                            mss_old,
                            mss_new,
                            0,
                            0,
                            ~old_tcp_checksum_r,
                            clib_net_to_host_u16(tcp->tcp_checksum));
		    }
		}
	    }
        }
    }
}

u32 get_my_svi_intf_ip_addr() {
    return 0x01010101;
}
8'>1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256