3d6ca992e45768a5be4ebfc6229f08dcd53369fd
[vpp.git] / src / plugins / nat / dslite / dslite_ce_decap.c
1 /*
2  * Copyright (c) 2018 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/dslite.h>
16 #include <nat/nat_inlines.h>
17
18 typedef enum
19 {
20   DSLITE_CE_DECAP_NEXT_IP4_LOOKUP,
21   DSLITE_IN2OUT_NEXT_IP6_ICMP,
22   DSLITE_CE_DECAP_NEXT_DROP,
23   DSLITE_CE_DECAP_N_NEXT,
24 } dslite_ce_decap_next_t;
25
26 static char *dslite_ce_decap_error_strings[] = {
27 #define _(sym,string) string,
28   foreach_dslite_error
29 #undef _
30 };
31
32 VLIB_NODE_FN (dslite_ce_decap_node) (vlib_main_t * vm,
33                                      vlib_node_runtime_t * node,
34                                      vlib_frame_t * frame)
35 {
36   u32 n_left_from, *from, *to_next;
37   dslite_ce_decap_next_t next_index;
38
39   from = vlib_frame_vector_args (frame);
40   n_left_from = frame->n_vectors;
41   next_index = node->cached_next_index;
42
43   while (n_left_from > 0)
44     {
45       u32 n_left_to_next;
46
47       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
48
49       while (n_left_from > 0 && n_left_to_next > 0)
50         {
51           u32 bi0;
52           vlib_buffer_t *b0;
53           u32 next0 = DSLITE_CE_DECAP_NEXT_IP4_LOOKUP;
54           u8 error0 = DSLITE_ERROR_CE_DECAP;
55           ip4_header_t *ip40;
56           ip6_header_t *ip60;
57           u32 proto0;
58
59           /* speculatively enqueue b0 to the current next frame */
60           bi0 = from[0];
61           to_next[0] = bi0;
62           from += 1;
63           to_next += 1;
64           n_left_from -= 1;
65           n_left_to_next -= 1;
66
67           b0 = vlib_get_buffer (vm, bi0);
68           ip60 = vlib_buffer_get_current (b0);
69
70           if (PREDICT_FALSE (ip60->protocol != IP_PROTOCOL_IP_IN_IP))
71             {
72               if (ip60->protocol == IP_PROTOCOL_ICMP6)
73                 {
74                   next0 = DSLITE_IN2OUT_NEXT_IP6_ICMP;
75                   goto trace0;
76                 }
77               error0 = DSLITE_ERROR_BAD_IP6_PROTOCOL;
78               next0 = DSLITE_CE_DECAP_NEXT_DROP;
79               goto trace0;
80             }
81
82           ip40 = vlib_buffer_get_current (b0) + sizeof (ip6_header_t);
83           proto0 = ip_proto_to_snat_proto (ip40->protocol);
84
85           if (PREDICT_FALSE (proto0 == ~0))
86             {
87               error0 = DSLITE_ERROR_UNSUPPORTED_PROTOCOL;
88               next0 = DSLITE_CE_DECAP_NEXT_DROP;
89               goto trace0;
90             }
91
92           ip40->tos =
93             (clib_net_to_host_u32
94              (ip60->ip_version_traffic_class_and_flow_label) & 0x0ff00000) >>
95             20;
96           vlib_buffer_advance (b0, sizeof (ip6_header_t));
97
98         trace0:
99           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
100                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
101             {
102               dslite_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
103               t->next_index = next0;
104             }
105
106           b0->error = node->errors[error0];
107
108           /* verify speculative enqueue, maybe switch current next frame */
109           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
110                                            n_left_to_next, bi0, next0);
111         }
112       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
113     }
114
115   return frame->n_vectors;
116 }
117
118 /* *INDENT-OFF* */
119 VLIB_REGISTER_NODE (dslite_ce_decap_node) = {
120   .name = "dslite-ce-decap",
121   .vector_size = sizeof (u32),
122   .format_trace = format_dslite_ce_trace,
123   .type = VLIB_NODE_TYPE_INTERNAL,
124   .n_errors = ARRAY_LEN (dslite_ce_decap_error_strings),
125   .error_strings = dslite_ce_decap_error_strings,
126   .n_next_nodes = DSLITE_CE_DECAP_N_NEXT,
127   /* edit / add dispositions here */
128   .next_nodes = {
129     [DSLITE_CE_DECAP_NEXT_DROP] = "error-drop",
130     [DSLITE_CE_DECAP_NEXT_IP4_LOOKUP] = "ip4-lookup",
131     [DSLITE_IN2OUT_NEXT_IP6_ICMP] = "ip6-icmp-input",
132   },
133 };
134 /* *INDENT-ON* */
135
136 /*
137  * fd.io coding-style-patch-verification: ON
138  *
139  * Local Variables:
140  * eval: (c-set-style "gnu")
141  * End:
142  */