stn: fix non-NULL terminated string overflow
[vpp.git] / src / plugins / stn / stn.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
16 #include <stn/stn.h>
17
18 #include <vnet/plugin/plugin.h>
19 #include <vpp/app/version.h>
20 #include <vnet/ip/format.h>
21 #include <vnet/ip/punt.h>
22 #include <vnet/ethernet/packet.h>
23
24 stn_main_t stn_main;
25 static vlib_node_registration_t stn_ip4_punt;
26 static vlib_node_registration_t stn_ip6_punt;
27
28 static u8 stn_hw_addr_local[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
29 static u8 stn_hw_addr_dst[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x02};
30
31 static ethernet_header_t stn_ip4_ethernet_header = {};
32 static ethernet_header_t stn_ip6_ethernet_header = {};
33
34 typedef struct {
35   clib_bihash_kv_16_8_t kv;
36 } stn_ip46_punt_trace_t;
37
38 static u8 *
39 format_stn_rule (u8 * s, va_list * args)
40 {
41   stn_rule_t *r = va_arg (*args, stn_rule_t *);
42   stn_main_t *stn = &stn_main;
43   u32 indent = format_get_indent (s);
44   u32 node_index = ip46_address_is_ip4(&r->address)?stn_ip4_punt.index:stn_ip6_punt.index;
45   vlib_node_t *next_node = vlib_get_next_node(vlib_get_main(), node_index, r->next_node_index);
46   s = format (s, "rule_index: %d\n", r - stn->rules);
47   s = format (s, "%Uaddress: %U\n", format_white_space, indent,
48               format_ip46_address, &r->address, IP46_TYPE_ANY);
49   s = format (s, "%Uiface: %U (%d)\n", format_white_space, indent,
50               format_vnet_sw_if_index_name, vnet_get_main(), r->sw_if_index,
51               r->sw_if_index);
52   s = format (s, "%Unext_node: %v (%d)", format_white_space, indent,
53               next_node->name, next_node->index);
54   return s;
55 }
56
57 static_always_inline u8 *
58 format_stn_ip46_punt_trace (u8 * s, va_list * args, u8 is_ipv4)
59 {
60   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
61   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
62   stn_ip46_punt_trace_t *t = va_arg (*args, stn_ip46_punt_trace_t *);
63   u32 indent = format_get_indent (s);
64
65   s = format (s, "dst_address: %U\n", format_ip46_address,
66           (ip46_address_t *)t->kv.key, IP46_TYPE_ANY);
67
68   if (t->kv.value == ~(0L))
69     {
70       s = format (s, "%Urule: none", format_white_space, indent);
71     }
72   else
73     {
74       s = format (s, "%Urule:\n%U%U", format_white_space, indent,
75                      format_white_space, indent + 2,
76                      format_stn_rule, &stn_main.rules[t->kv.value]);
77     }
78   return s;
79 }
80
81 typedef enum
82 {
83   STN_IP_PUNT_DROP,
84   STN_IP_PUNT_N_NEXT,
85 } stn_ip_punt_next_t;
86
87 static_always_inline uword
88 stn_ip46_punt_fn (vlib_main_t * vm,
89                   vlib_node_runtime_t * node,
90                   vlib_frame_t * frame,
91                   u8 is_ipv4)
92 {
93   u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
94   stn_main_t *stn = &stn_main;
95
96   from = vlib_frame_vector_args (frame);
97   n_left_from = frame->n_vectors;
98   next_index = node->cached_next_index;
99
100   while (n_left_from > 0)
101     {
102       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
103
104       /* Single loop */
105       while (n_left_from > 0 && n_left_to_next > 0)
106         {
107           u32 pi0;
108           vlib_buffer_t *p0;
109           clib_bihash_kv_16_8_t kv;
110           u32 next0 = STN_IP_PUNT_DROP;
111
112           pi0 = to_next[0] = from[0];
113           from += 1;
114           n_left_from -= 1;
115           to_next += 1;
116           n_left_to_next -= 1;
117
118           p0 = vlib_get_buffer (vm, pi0);
119
120           if (is_ipv4)
121             {
122               ip4_header_t *hdr = (ip4_header_t *) vlib_buffer_get_current(p0);
123               ip46_address_set_ip4((ip46_address_t *)kv.key, &hdr->dst_address);
124             }
125           else
126             {
127               ip6_header_t *hdr = (ip6_header_t *) vlib_buffer_get_current(p0);
128               kv.key[0] = hdr->dst_address.as_u64[0];
129               kv.key[1] = hdr->dst_address.as_u64[1];
130             }
131
132           kv.value = ~(0L);
133           clib_bihash_search_inline_16_8 (&stn->rule_by_address_table, &kv);
134           if (kv.value != ~(0L))
135             {
136               ethernet_header_t *eth;
137               stn_rule_t *r = &stn->rules[kv.value];
138               vnet_buffer(p0)->sw_if_index[VLIB_TX] = r->sw_if_index;
139               next0 = r->next_node_index;
140               vlib_buffer_advance(p0, -sizeof(*eth));
141               eth = (ethernet_header_t *) vlib_buffer_get_current(p0);
142               if (is_ipv4)
143                 clib_memcpy_fast(eth, &stn_ip4_ethernet_header, sizeof(*eth));
144               else
145                 clib_memcpy_fast(eth, &stn_ip6_ethernet_header, sizeof(*eth));
146             }
147           else
148           {
149               vnet_feature_next (&next0, p0);
150           }
151
152           if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
153             {
154               stn_ip46_punt_trace_t *tr =
155                   vlib_add_trace (vm, node, p0, sizeof (*tr));
156               tr->kv = kv;
157             }
158
159           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
160                                            n_left_to_next, pi0, next0);
161         }
162       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
163     }
164
165   return frame->n_vectors;
166 }
167
168
169 #define foreach_stn_ip_punt_error \
170  _(NONE, "no error")
171
172 typedef enum {
173 #define _(sym,str) STN_IP_punt_ERROR_##sym,
174   foreach_stn_ip_punt_error
175 #undef _
176   STN_IP_PUNT_N_ERROR,
177 } ila_error_t;
178
179 static char *stn_ip_punt_error_strings[] = {
180 #define _(sym,string) string,
181     foreach_stn_ip_punt_error
182 #undef _
183 };
184
185 u8 *
186 format_stn_ip6_punt_trace (u8 * s, va_list * args)
187 {
188   return format_stn_ip46_punt_trace (s, args, 0);
189 }
190
191 static uword
192 stn_ip6_punt_fn (vlib_main_t * vm,
193                    vlib_node_runtime_t * node, vlib_frame_t * frame)
194 {
195   return stn_ip46_punt_fn(vm, node, frame, 0);
196 }
197
198 VLIB_REGISTER_NODE (stn_ip6_punt, static) =
199 {
200   .function = stn_ip6_punt_fn,
201   .name = "stn-ip6-punt",
202   .vector_size = sizeof (u32),
203   .format_trace = format_stn_ip6_punt_trace,
204   .n_errors = STN_IP_PUNT_N_ERROR,
205   .error_strings = stn_ip_punt_error_strings,
206   .n_next_nodes = STN_IP_PUNT_N_NEXT,
207   .next_nodes =
208   {
209       [STN_IP_PUNT_DROP] = "error-drop"
210   },
211 };
212 VNET_FEATURE_INIT (stn_ip6_punt_feat_node, static) = {
213   .arc_name = "ip6-punt",
214   .node_name = "stn-ip6-punt",
215   .runs_before = VNET_FEATURES("ip6-punt-redirect"),
216 };
217
218 u8 *
219 format_stn_ip4_punt_trace (u8 * s, va_list * args)
220 {
221   return format_stn_ip46_punt_trace (s, args, 1);
222 }
223
224 static uword
225 stn_ip4_punt_fn (vlib_main_t * vm,
226                    vlib_node_runtime_t * node, vlib_frame_t * frame)
227 {
228   return stn_ip46_punt_fn(vm, node, frame, 1);
229 }
230
231 VLIB_REGISTER_NODE (stn_ip4_punt, static) =
232 {
233   .function = stn_ip4_punt_fn,
234   .name = "stn-ip4-punt",
235   .vector_size = sizeof (u32),
236   .format_trace = format_stn_ip4_punt_trace,
237   .n_errors = STN_IP_PUNT_N_ERROR,
238   .error_strings = stn_ip_punt_error_strings,
239   .n_next_nodes = STN_IP_PUNT_N_NEXT,
240   .next_nodes =
241   {
242       [STN_IP_PUNT_DROP] = "error-drop",
243   },
244 };
245 VNET_FEATURE_INIT (stn_ip4_punt_feat_node, static) = {
246   .arc_name = "ip4-punt",
247   .node_name = "stn-ip4-punt",
248   .runs_before = VNET_FEATURES("ip4-punt-redirect"),
249 };
250
251 clib_error_t *
252 stn_init (vlib_main_t * vm)
253 {
254   stn_main_t *stn = &stn_main;
255   stn->rules = 0;
256   clib_bihash_init_16_8(&stn->rule_by_address_table, "stn addresses",
257                         1024, 1<<20);
258
259   clib_memcpy_fast(stn_ip4_ethernet_header.dst_address, stn_hw_addr_dst, 6);
260   clib_memcpy_fast(stn_ip4_ethernet_header.src_address, stn_hw_addr_local, 6);
261   stn_ip4_ethernet_header.type = clib_host_to_net_u16(ETHERNET_TYPE_IP4);
262
263   clib_memcpy_fast(stn_ip6_ethernet_header.dst_address, stn_hw_addr_dst, 6);
264   clib_memcpy_fast(stn_ip6_ethernet_header.src_address, stn_hw_addr_local, 6);
265   stn_ip6_ethernet_header.type = clib_host_to_net_u16(ETHERNET_TYPE_IP6);
266
267   return stn_api_init (vm, stn);
268
269   return NULL;
270 }
271
272 VLIB_INIT_FUNCTION (stn_init);
273
274 VLIB_PLUGIN_REGISTER () = {
275     .version = VPP_BUILD_VER,
276     .description = "VPP Steals the NIC (STN) for Container Integration",
277 };
278
279 int stn_rule_add_del (stn_rule_add_del_args_t *args)
280 {
281   vnet_main_t *vnm = vnet_get_main();
282   vlib_main_t *vm = vlib_get_main();
283   stn_main_t *stn = &stn_main;
284
285   stn_rule_t *r = NULL;
286   clib_bihash_kv_16_8_t kv;
287   kv.key[0] = args->address.as_u64[0];
288   kv.key[1] = args->address.as_u64[1];
289
290   if (clib_bihash_search_inline_16_8 (&stn->rule_by_address_table, &kv) == 0)
291     {
292       r = &stn->rules[kv.value];
293     }
294   else if (!args->del)
295     {
296       pool_get(stn->rules, r);
297       kv.value = r - stn->rules;
298       clib_bihash_add_del_16_8(&stn->rule_by_address_table, &kv, 1);
299       r->address = args->address;
300
301       stn->n_rules++;
302       if (stn->n_rules == 1)
303         {
304             vnet_feature_enable_disable("ip6-punt", "stn-ip6-punt",
305                                         0, 1, 0, 0);
306             vnet_feature_enable_disable("ip4-punt", "stn-ip4-punt",
307                                         0, 1, 0, 0);
308
309             punt_reg_t pr = {
310               .punt = {
311                 .l4 = {
312                   .af = AF_IP4,
313                   .port = ~0,
314                   .protocol = IP_PROTOCOL_UDP,
315                 },
316               },
317               .type = PUNT_TYPE_L4,
318             };
319             vnet_punt_add_del (vm, &pr, 1 /* is_add */);
320             pr.punt.l4.af = AF_IP6;
321             vnet_punt_add_del (vm, &pr, 1 /* is_add */);
322             pr.punt.l4.protocol = IP_PROTOCOL_TCP;
323             vnet_punt_add_del (vm, &pr, 1 /* is_add */);
324             pr.punt.l4.af = AF_IP4;
325             vnet_punt_add_del (vm, &pr, 1 /* is_add */);
326         }
327     }
328
329   if (!args->del)
330     {
331       /* Getting output node and adding it as next */
332       u32 output_node_index =
333           vnet_tx_node_index_for_sw_interface(vnm, args->sw_if_index);
334       u32 node_index = ip46_address_is_ip4(&args->address)?
335           stn_ip4_punt.index : stn_ip6_punt.index;
336
337       r->sw_if_index = args->sw_if_index;
338       r->next_node_index =
339           vlib_node_add_next(vm, node_index, output_node_index);
340
341       /* enabling forwarding on the output node (might not be done since
342        * it is unnumbered) */
343       ip4_sw_interface_enable_disable(args->sw_if_index, 1);
344       ip6_sw_interface_enable_disable(args->sw_if_index, 1);
345     }
346   else if (r)
347     {
348       clib_bihash_add_del_16_8(&stn->rule_by_address_table, &kv, 0);
349       pool_put(stn->rules, r);
350
351       stn->n_rules--;
352       if (stn->n_rules == 0)
353         {
354             vnet_feature_enable_disable("ip6-punt", "stn-ip6-punt",
355                                         0, 0, 0, 0);
356             vnet_feature_enable_disable("ip4-punt", "stn-ip4-punt",
357                                         0, 0, 0, 0);
358         }
359     }
360   else
361     {
362       return VNET_API_ERROR_NO_SUCH_ENTRY;
363     }
364
365   return 0;
366 }
367
368 static clib_error_t *
369 show_stn_rules_fn (vlib_main_t * vm,
370                       unformat_input_t * input, vlib_cli_command_t * cmd)
371 {
372   stn_main_t *stn = &stn_main;
373   u8 *s = 0;
374   stn_rule_t *rule;
375   pool_foreach (rule, stn->rules) {
376       s = format (s, "- %U\n", format_stn_rule, rule);
377   }
378
379   vlib_cli_output(vm, "%v", s);
380
381   vec_free(s);
382   return NULL;
383 }
384
385 VLIB_CLI_COMMAND (show_stn_rules_command, static) =
386 {
387   .path = "show stn rules",
388   .short_help = "",
389   .function = show_stn_rules_fn,
390 };
391
392 static clib_error_t *
393 stn_rule_fn (vlib_main_t * vm,
394                       unformat_input_t * input, vlib_cli_command_t * cmd)
395 {
396   unformat_input_t _line_input, *line_input = &_line_input;
397   clib_error_t *error = 0;
398   stn_rule_add_del_args_t args = {};
399   u8 got_addr = 0;
400   u8 got_iface = 0;
401   int ret;
402
403   if (!unformat_user (input, unformat_line_input, line_input))
404     return 0;
405
406   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
407     {
408       if (unformat (line_input, "address %U", unformat_ip46_address,
409                     &args.address, IP46_TYPE_ANY))
410         got_addr = 1;
411       else if (unformat
412                (line_input, "interface %U", unformat_vnet_sw_interface,
413                 vnet_get_main(), &args.sw_if_index))
414         got_iface = 1;
415       else if (unformat (line_input, "del"))
416         args.del = 1;
417       else
418         {
419           error = clib_error_return (0, "parse error: '%U'",
420                                      format_unformat_error, line_input);
421           goto done;
422         }
423     }
424
425   if (!got_addr)
426     {
427       error = clib_error_return (0, "Missing address");
428       goto done;
429     }
430
431   if (!got_iface)
432     {
433       error = clib_error_return (0, "Missing interface");
434       goto done;
435     }
436
437   if ((ret = stn_rule_add_del (&args)))
438     {
439       error = clib_error_return (0, "stn_rule_add_del returned error %d", ret);
440       goto done;
441     }
442
443 done:
444   unformat_free (line_input);
445   return error;
446 }
447
448 VLIB_CLI_COMMAND (stn_rule_command, static) =
449 {
450   .path = "stn rule",
451   .short_help = "address <addr> interface <iface> [del]",
452   .function = stn_rule_fn,
453 };