nat: use SVR
[vpp.git] / src / plugins / nat / dslite_out2in.c
1 /*
2  * Copyright (c) 2017 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 #include <nat/dslite.h>
16 #include <nat/nat_inlines.h>
17
18 typedef enum
19 {
20   DSLITE_OUT2IN_NEXT_IP4_LOOKUP,
21   DSLITE_OUT2IN_NEXT_IP6_LOOKUP,
22   DSLITE_OUT2IN_NEXT_DROP,
23   DSLITE_OUT2IN_N_NEXT,
24 } dslite_out2in_next_t;
25
26 static char *dslite_out2in_error_strings[] = {
27 #define _(sym,string) string,
28   foreach_dslite_error
29 #undef _
30 };
31
32 static inline u32
33 dslite_icmp_out2in (dslite_main_t * dm, ip4_header_t * ip4,
34                     dslite_session_t ** sp, u32 next, u8 * error,
35                     u32 thread_index)
36 {
37   dslite_session_t *s = 0;
38   icmp46_header_t *icmp = ip4_next_header (ip4);
39   clib_bihash_kv_8_8_t kv, value;
40   snat_session_key_t key;
41   u32 n = next;
42   icmp_echo_header_t *echo;
43   u32 new_addr, old_addr;
44   u16 old_id, new_id;
45   ip_csum_t sum;
46
47   echo = (icmp_echo_header_t *) (icmp + 1);
48
49   if (icmp_type_is_error_message (icmp->type)
50       || (icmp->type != ICMP4_echo_reply))
51     {
52       n = DSLITE_OUT2IN_NEXT_DROP;
53       *error = DSLITE_ERROR_BAD_ICMP_TYPE;
54       goto done;
55     }
56
57   key.addr = ip4->dst_address;
58   key.port = echo->identifier;
59   key.protocol = SNAT_PROTOCOL_ICMP;
60   key.fib_index = 0;
61   kv.key = key.as_u64;
62
63   if (clib_bihash_search_8_8
64       (&dm->per_thread_data[thread_index].out2in, &kv, &value))
65     {
66       next = DSLITE_OUT2IN_NEXT_DROP;
67       *error = DSLITE_ERROR_NO_TRANSLATION;
68       goto done;
69     }
70   else
71     {
72       s =
73         pool_elt_at_index (dm->per_thread_data[thread_index].sessions,
74                            value.value);
75     }
76
77   old_id = echo->identifier;
78   echo->identifier = new_id = s->in2out.port;
79   sum = icmp->checksum;
80   sum = ip_csum_update (sum, old_id, new_id, icmp_echo_header_t, identifier);
81   icmp->checksum = ip_csum_fold (sum);
82
83   old_addr = ip4->dst_address.as_u32;
84   ip4->dst_address = s->in2out.addr;
85   new_addr = ip4->dst_address.as_u32;
86
87   sum = ip4->checksum;
88   sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
89   ip4->checksum = ip_csum_fold (sum);
90
91 done:
92   *sp = s;
93   return n;
94 }
95
96 VLIB_NODE_FN (dslite_out2in_node) (vlib_main_t * vm,
97                                    vlib_node_runtime_t * node,
98                                    vlib_frame_t * frame)
99 {
100   u32 n_left_from, *from, *to_next;
101   dslite_out2in_next_t next_index;
102   vlib_node_runtime_t *error_node;
103   u32 thread_index = vm->thread_index;
104   f64 now = vlib_time_now (vm);
105   dslite_main_t *dm = &dslite_main;
106
107   error_node = vlib_node_get_runtime (vm, dm->dslite_out2in_node_index);
108
109   from = vlib_frame_vector_args (frame);
110   n_left_from = frame->n_vectors;
111   next_index = node->cached_next_index;
112
113
114   while (n_left_from > 0)
115     {
116       u32 n_left_to_next;
117
118       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
119
120       while (n_left_from > 0 && n_left_to_next > 0)
121         {
122           u32 bi0;
123           vlib_buffer_t *b0;
124           u32 next0 = DSLITE_OUT2IN_NEXT_IP6_LOOKUP;
125           u8 error0 = DSLITE_ERROR_OUT2IN;
126           ip4_header_t *ip40;
127           ip6_header_t *ip60;
128           u32 proto0;
129           udp_header_t *udp0;
130           tcp_header_t *tcp0;
131           clib_bihash_kv_8_8_t kv0, value0;
132           snat_session_key_t key0;
133           dslite_session_t *s0 = 0;
134           ip_csum_t sum0;
135           u32 new_addr0, old_addr0;
136           u16 new_port0, old_port0;
137
138           /* speculatively enqueue b0 to the current next frame */
139           bi0 = from[0];
140           to_next[0] = bi0;
141           from += 1;
142           to_next += 1;
143           n_left_from -= 1;
144           n_left_to_next -= 1;
145
146           b0 = vlib_get_buffer (vm, bi0);
147           ip40 = vlib_buffer_get_current (b0);
148           proto0 = ip_proto_to_snat_proto (ip40->protocol);
149
150           if (PREDICT_FALSE (proto0 == ~0))
151             {
152               error0 = DSLITE_ERROR_UNSUPPORTED_PROTOCOL;
153               next0 = DSLITE_OUT2IN_NEXT_DROP;
154               goto trace0;
155             }
156
157           if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
158             {
159               next0 =
160                 dslite_icmp_out2in (dm, ip40, &s0, next0, &error0,
161                                     thread_index);
162               if (PREDICT_FALSE (next0 == DSLITE_OUT2IN_NEXT_DROP))
163                 goto trace0;
164
165               goto encap0;
166             }
167
168           udp0 = ip4_next_header (ip40);
169           tcp0 = (tcp_header_t *) udp0;
170
171           key0.addr = ip40->dst_address;
172           key0.port = udp0->dst_port;
173           key0.protocol = proto0;
174           key0.fib_index = 0;
175           kv0.key = key0.as_u64;
176
177           if (clib_bihash_search_8_8
178               (&dm->per_thread_data[thread_index].out2in, &kv0, &value0))
179             {
180               next0 = DSLITE_OUT2IN_NEXT_DROP;
181               error0 = DSLITE_ERROR_NO_TRANSLATION;
182               goto trace0;
183             }
184           else
185             {
186               s0 =
187                 pool_elt_at_index (dm->per_thread_data[thread_index].sessions,
188                                    value0.value);
189             }
190
191           old_addr0 = ip40->dst_address.as_u32;
192           ip40->dst_address = s0->in2out.addr;
193           new_addr0 = ip40->dst_address.as_u32;
194
195           sum0 = ip40->checksum;
196           sum0 =
197             ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
198                             dst_address);
199           ip40->checksum = ip_csum_fold (sum0);
200
201           if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
202             {
203               old_port0 = tcp0->dst_port;
204               tcp0->dst_port = s0->in2out.port;
205               new_port0 = tcp0->dst_port;
206
207               sum0 = tcp0->checksum;
208               sum0 =
209                 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
210                                 dst_address);
211               sum0 =
212                 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
213                                 length);
214               tcp0->checksum = ip_csum_fold (sum0);
215             }
216           else
217             {
218               old_port0 = udp0->dst_port;
219               udp0->dst_port = s0->in2out.port;
220               udp0->checksum = 0;
221             }
222
223         encap0:
224           /* Construct IPv6 header */
225           vlib_buffer_advance (b0, -(sizeof (ip6_header_t)));
226           ip60 = vlib_buffer_get_current (b0);
227           ip60->ip_version_traffic_class_and_flow_label =
228             clib_host_to_net_u32 ((6 << 28) + (ip40->tos << 20));
229           ip60->payload_length = ip40->length;
230           ip60->protocol = IP_PROTOCOL_IP_IN_IP;
231           ip60->hop_limit = ip40->ttl;
232           ip60->src_address.as_u64[0] = dm->aftr_ip6_addr.as_u64[0];
233           ip60->src_address.as_u64[1] = dm->aftr_ip6_addr.as_u64[1];
234           ip60->dst_address.as_u64[0] = s0->in2out.softwire_id.as_u64[0];
235           ip60->dst_address.as_u64[1] = s0->in2out.softwire_id.as_u64[1];
236
237           /* Accounting */
238           s0->last_heard = now;
239           s0->total_pkts++;
240           s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
241           /* Per-B4 LRU list maintenance */
242           clib_dlist_remove (dm->per_thread_data[thread_index].list_pool,
243                              s0->per_b4_index);
244           clib_dlist_addtail (dm->per_thread_data[thread_index].list_pool,
245                               s0->per_b4_list_head_index, s0->per_b4_index);
246         trace0:
247           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
248                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
249             {
250               dslite_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
251               t->next_index = next0;
252               t->session_index = ~0;
253               if (s0)
254                 t->session_index =
255                   s0 - dm->per_thread_data[thread_index].sessions;
256             }
257
258           b0->error = error_node->errors[error0];
259
260           /* verify speculative enqueue, maybe switch current next frame */
261           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
262                                            n_left_to_next, bi0, next0);
263         }
264       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
265     }
266
267   return frame->n_vectors;
268 }
269
270 /* *INDENT-OFF* */
271 VLIB_REGISTER_NODE (dslite_out2in_node) = {
272   .name = "dslite-out2in",
273   .vector_size = sizeof (u32),
274   .format_trace = format_dslite_trace,
275   .type = VLIB_NODE_TYPE_INTERNAL,
276   .n_errors = ARRAY_LEN (dslite_out2in_error_strings),
277   .error_strings = dslite_out2in_error_strings,
278   .n_next_nodes = DSLITE_OUT2IN_N_NEXT,
279   /* edit / add dispositions here */
280   .next_nodes = {
281     [DSLITE_OUT2IN_NEXT_DROP] = "error-drop",
282     [DSLITE_OUT2IN_NEXT_IP4_LOOKUP] = "ip4-lookup",
283     [DSLITE_OUT2IN_NEXT_IP6_LOOKUP] = "ip6-lookup",
284   },
285 };
286 /* *INDENT-ON* */
287
288 /*
289  * fd.io coding-style-patch-verification: ON
290  *
291  * Local Variables:
292  * eval: (c-set-style "gnu")
293  * End:
294  */