e4f64a5efe64d0b38e31dec23395fda28e913834
[vpp.git] / vnet / vnet / ip / udp_local.c
1 /*
2  * node.c: udp packet processing
3  *
4  * Copyright (c) 2013 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <vlib/vlib.h>
19 #include <vnet/pg/pg.h>
20 #include <vnet/ip/udp.h>
21 #include <vnet/ip/udp_packet.h>
22 #include <vppinfra/sparse_vec.h>
23
24 udp_main_t udp_main;
25
26 #define foreach_udp_input_next                  \
27   _ (PUNT, "error-punt")                        \
28   _ (DROP, "error-drop")                        \
29   _ (ICMP4_ERROR, "ip4-icmp-error")             \
30   _ (ICMP6_ERROR, "ip6-icmp-error")
31
32 typedef enum {
33 #define _(s,n) UDP_INPUT_NEXT_##s,
34   foreach_udp_input_next
35 #undef _
36   UDP_INPUT_N_NEXT,
37 } udp_input_next_t;
38
39 typedef struct {
40   u16 src_port;
41   u16 dst_port;
42   u8 bound;
43 } udp_rx_trace_t;
44
45 u8 * format_udp_rx_trace (u8 * s, va_list * args)
46 {
47   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
48   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
49   udp_rx_trace_t * t = va_arg (*args, udp_rx_trace_t *);
50     
51   s = format (s, "UDP: src-port %d dst-port %d%s",
52               clib_net_to_host_u16(t->src_port),
53               clib_net_to_host_u16(t->dst_port),
54               t->bound ? "" : " (no listener)");
55   return s;
56 }
57
58 typedef struct {
59   /* Sparse vector mapping udp dst_port in network byte order
60      to next index. */
61   u16 * next_by_dst_port;
62   u8 punt_unknown;
63 } udp_input_runtime_t;
64
65 vlib_node_registration_t udp4_input_node;
66 vlib_node_registration_t udp6_input_node;
67
68 always_inline uword
69 udp46_input_inline (vlib_main_t * vm,
70                     vlib_node_runtime_t * node,
71                     vlib_frame_t * from_frame,
72                     int is_ip4)
73 {
74   udp_input_runtime_t * rt = is_ip4 ?
75     (void *) vlib_node_get_runtime_data (vm, udp4_input_node.index)
76     : (void *) vlib_node_get_runtime_data (vm, udp6_input_node.index);
77   __attribute__((unused)) u32 n_left_from, next_index, * from, * to_next;
78   word n_no_listener = 0;
79   u8 punt_unknown = rt->punt_unknown;
80
81   from = vlib_frame_vector_args (from_frame);
82   n_left_from = from_frame->n_vectors;
83
84   next_index = node->cached_next_index;
85
86   while (n_left_from > 0)
87     {
88       u32 n_left_to_next;
89
90       vlib_get_next_frame (vm, node, next_index,
91                            to_next, n_left_to_next);
92
93       while (n_left_from >= 4 && n_left_to_next >= 2)
94         {
95           u32 bi0, bi1;
96           vlib_buffer_t * b0, * b1;
97           udp_header_t * h0 = 0, * h1 = 0;
98           u32 i0, i1, dst_port0, dst_port1;
99           u32 advance0, advance1;
100           u32 error0, next0, error1, next1;
101
102           /* Prefetch next iteration. */
103           {
104             vlib_buffer_t * p2, * p3;
105
106             p2 = vlib_get_buffer (vm, from[2]);
107             p3 = vlib_get_buffer (vm, from[3]);
108
109             vlib_prefetch_buffer_header (p2, LOAD);
110             vlib_prefetch_buffer_header (p3, LOAD);
111
112             CLIB_PREFETCH (p2->data, sizeof (h0[0]), LOAD);
113             CLIB_PREFETCH (p3->data, sizeof (h1[0]), LOAD);
114           }
115
116           bi0 = from[0];
117           bi1 = from[1];
118           to_next[0] = bi0;
119           to_next[1] = bi1;
120           from += 2;
121           to_next += 2;
122           n_left_to_next -= 2;
123           n_left_from -= 2;
124
125           b0 = vlib_get_buffer (vm, bi0);
126           b1 = vlib_get_buffer (vm, bi1);
127
128           /* ip4/6_local hands us the ip header, not the udp header */
129           if (is_ip4) 
130             {
131               advance0 = sizeof(ip4_header_t);
132               advance1 = sizeof(ip4_header_t);
133             }
134           else
135             {
136               advance0 = sizeof(ip6_header_t);
137               advance1 = sizeof(ip6_header_t);
138             }          
139
140           if (PREDICT_FALSE(b0->current_length < advance0 + sizeof (*h0)))
141             {
142               error0 = UDP_ERROR_LENGTH_ERROR;
143               next0 = UDP_INPUT_NEXT_DROP;
144             }
145           else
146             {
147               vlib_buffer_advance (b0, advance0);
148               h0 = vlib_buffer_get_current (b0);
149               error0 = next0 = 0;
150               if (PREDICT_FALSE(clib_net_to_host_u16(h0->length) >
151                                 vlib_buffer_length_in_chain(vm, b0)))
152                 {
153                   error0 = UDP_ERROR_LENGTH_ERROR;
154                   next0 = UDP_INPUT_NEXT_DROP;
155                 }
156             }
157
158           if (PREDICT_FALSE(b1->current_length < advance1 + sizeof (*h1)))
159             {
160               error1 = UDP_ERROR_LENGTH_ERROR;
161               next1 = UDP_INPUT_NEXT_DROP;
162             }
163           else
164             {
165               vlib_buffer_advance (b1, advance1);
166               h1 = vlib_buffer_get_current (b1);
167               error1 = next1 = 0;
168               if (PREDICT_FALSE(clib_net_to_host_u16(h1->length) >
169                             vlib_buffer_length_in_chain(vm, b1)))
170                 {
171                   error1 = UDP_ERROR_LENGTH_ERROR;
172                   next1 = UDP_INPUT_NEXT_DROP;
173                 }
174             }
175
176           /* Index sparse array with network byte order. */
177           dst_port0 = (error0 == 0) ? h0->dst_port : 0;
178           dst_port1 = (error1 == 0) ? h1->dst_port : 0;
179           sparse_vec_index2 (rt->next_by_dst_port, dst_port0, dst_port1,
180                              &i0, &i1);
181           next0 = (error0 == 0) ? vec_elt(rt->next_by_dst_port, i0) : next0;
182           next1 = (error1 == 0) ? vec_elt(rt->next_by_dst_port, i1) : next1;
183
184           if (PREDICT_FALSE(i0 == SPARSE_VEC_INVALID_INDEX))
185             {
186               // move the pointer back so icmp-error can find the
187               // ip packet header
188               vlib_buffer_advance (b0, - (word)advance0);
189
190               if (PREDICT_FALSE(punt_unknown))
191                 {
192                   b0->error = node->errors[UDP_ERROR_PUNT];
193                   next0 = UDP_INPUT_NEXT_PUNT;
194                 }
195               else if (is_ip4)
196                 {
197                   icmp4_error_set_vnet_buffer(b0, ICMP4_destination_unreachable,
198                         ICMP4_destination_unreachable_port_unreachable, 0);
199                   next0 = UDP_INPUT_NEXT_ICMP4_ERROR;
200                   n_no_listener ++;
201                 }
202               else
203                 {
204                   icmp6_error_set_vnet_buffer(b0, ICMP6_destination_unreachable,
205                         ICMP6_destination_unreachable_port_unreachable, 0);
206                   next0 = UDP_INPUT_NEXT_ICMP6_ERROR;
207                   n_no_listener ++;
208                 }
209             }
210           else
211             {
212               b0->error = node->errors[UDP_ERROR_NONE];
213               // advance to the payload
214               vlib_buffer_advance (b0, sizeof (*h0));
215             }
216
217           if (PREDICT_FALSE(i1 == SPARSE_VEC_INVALID_INDEX))
218             {
219               // move the pointer back so icmp-error can find the
220               // ip packet header
221               vlib_buffer_advance (b1, - (word)advance1);
222
223               if (PREDICT_FALSE(punt_unknown))
224                 {
225                   b1->error = node->errors[UDP_ERROR_PUNT];
226                   next1 = UDP_INPUT_NEXT_PUNT;
227                 }
228               else if (is_ip4)
229                 {
230                   icmp4_error_set_vnet_buffer(b1, ICMP4_destination_unreachable,
231                         ICMP4_destination_unreachable_port_unreachable, 0);
232                   next1 = UDP_INPUT_NEXT_ICMP4_ERROR;
233                   n_no_listener ++;
234                 }
235               else
236                 {
237                   icmp6_error_set_vnet_buffer(b1, ICMP6_destination_unreachable,
238                         ICMP6_destination_unreachable_port_unreachable, 0);
239                   next1 = UDP_INPUT_NEXT_ICMP6_ERROR;
240                   n_no_listener ++;
241                 }
242             }
243           else
244             {
245               b1->error = node->errors[UDP_ERROR_NONE];
246               // advance to the payload
247               vlib_buffer_advance (b1, sizeof (*h1));
248             }
249           
250           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
251             {
252               udp_rx_trace_t *tr = vlib_add_trace (vm, node, 
253                                                    b0, sizeof (*tr));
254               if (b0->error != node->errors[UDP_ERROR_LENGTH_ERROR])
255                 {
256                   tr->src_port = h0 ? h0->src_port : 0;
257                   tr->dst_port = h0 ? h0->dst_port : 0;
258                   tr->bound = (next0 != UDP_INPUT_NEXT_ICMP4_ERROR &&
259                                next0 != UDP_INPUT_NEXT_ICMP6_ERROR);
260                 }
261             }
262           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED)) 
263             {
264               udp_rx_trace_t *tr = vlib_add_trace (vm, node, 
265                                                    b1, sizeof (*tr));
266               if (b1->error != node->errors[UDP_ERROR_LENGTH_ERROR])
267                 {
268                   tr->src_port = h1 ? h1->src_port : 0;
269                   tr->dst_port = h1 ? h1->dst_port : 0;
270                   tr->bound = (next1 != UDP_INPUT_NEXT_ICMP4_ERROR &&
271                                next1 != UDP_INPUT_NEXT_ICMP6_ERROR);
272                 }
273             }
274
275           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
276                                            to_next, n_left_to_next,
277                                            bi0, bi1, next0, next1);
278         }
279     
280       while (n_left_from > 0 && n_left_to_next > 0)
281         {
282           u32 bi0;
283           vlib_buffer_t * b0;
284           udp_header_t * h0 = 0;
285           u32 i0, next0;
286           u32 advance0;
287
288           bi0 = from[0];
289           to_next[0] = bi0;
290           from += 1;
291           to_next += 1;
292           n_left_from -= 1;
293           n_left_to_next -= 1;
294
295           b0 = vlib_get_buffer (vm, bi0);
296
297           /* ip4/6_local hands us the ip header, not the udp header */
298           if (is_ip4) 
299             advance0 = sizeof(ip4_header_t);
300           else
301             advance0 = sizeof(ip6_header_t);
302
303           if (PREDICT_FALSE(b0->current_length < advance0 + sizeof (*h0)))
304             {
305               b0->error = node->errors[UDP_ERROR_LENGTH_ERROR];
306               next0 = UDP_INPUT_NEXT_DROP;
307               goto trace_x1;
308             }
309
310           vlib_buffer_advance (b0, advance0);
311
312           h0 = vlib_buffer_get_current (b0);
313
314           if (PREDICT_TRUE(clib_net_to_host_u16(h0->length) <= 
315                            vlib_buffer_length_in_chain(vm, b0)))
316             {
317               i0 = sparse_vec_index (rt->next_by_dst_port, h0->dst_port);
318               next0 = vec_elt(rt->next_by_dst_port, i0);
319
320               if (PREDICT_FALSE(i0 == SPARSE_VEC_INVALID_INDEX))
321                 {
322                   // move the pointer back so icmp-error can find the
323                   // ip packet header
324                   vlib_buffer_advance (b0, - (word)advance0);
325
326                   if (PREDICT_FALSE(punt_unknown))
327                     {
328                       b0->error = node->errors[UDP_ERROR_PUNT];
329                       next0 = UDP_INPUT_NEXT_PUNT;
330                     }
331                   else if (is_ip4)
332                     {
333                       icmp4_error_set_vnet_buffer(b0, ICMP4_destination_unreachable,
334                             ICMP4_destination_unreachable_port_unreachable, 0);
335                       next0 = UDP_INPUT_NEXT_ICMP4_ERROR;
336                       n_no_listener ++;
337                     }
338                   else
339                     {
340                       icmp6_error_set_vnet_buffer(b0, ICMP6_destination_unreachable,
341                             ICMP6_destination_unreachable_port_unreachable, 0);
342                       next0 = UDP_INPUT_NEXT_ICMP6_ERROR;
343                       n_no_listener ++;
344                     }
345                 }
346               else
347                 {
348                   b0->error = node->errors[UDP_ERROR_NONE];
349                   // advance to the payload
350                   vlib_buffer_advance (b0, sizeof (*h0));
351                 }
352             }
353           else
354             {
355               b0->error = node->errors[UDP_ERROR_LENGTH_ERROR];
356               next0 = UDP_INPUT_NEXT_DROP;
357             }
358
359         trace_x1:
360           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
361             {
362               udp_rx_trace_t *tr = vlib_add_trace (vm, node, 
363                                                    b0, sizeof (*tr));
364               if (b0->error != node->errors[UDP_ERROR_LENGTH_ERROR])
365                 {
366                   tr->src_port = h0->src_port;
367                   tr->dst_port = h0->dst_port;
368                   tr->bound = (next0 != UDP_INPUT_NEXT_ICMP4_ERROR &&
369                                next0 != UDP_INPUT_NEXT_ICMP6_ERROR);
370                 }
371             }
372
373           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
374                                            to_next, n_left_to_next,
375                                            bi0, next0);
376         }
377
378       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
379     }
380   vlib_error_count(vm, node->node_index, UDP_ERROR_NO_LISTENER, n_no_listener);
381   return from_frame->n_vectors;
382 }
383
384 static char * udp_error_strings[] = {
385 #define udp_error(n,s) s,
386 #include "udp_error.def"
387 #undef udp_error
388 };
389
390 static uword
391 udp4_input (vlib_main_t * vm,
392             vlib_node_runtime_t * node,
393             vlib_frame_t * from_frame)
394 {
395   return udp46_input_inline (vm, node, from_frame, 1 /* is_ip4 */);
396 }
397
398 static uword
399 udp6_input (vlib_main_t * vm,
400             vlib_node_runtime_t * node,
401             vlib_frame_t * from_frame)
402 {
403   return udp46_input_inline (vm, node, from_frame, 0 /* is_ip4 */);
404 }
405
406
407 VLIB_REGISTER_NODE (udp4_input_node) = {
408   .function = udp4_input,
409   .name = "ip4-udp-lookup",
410   /* Takes a vector of packets. */
411   .vector_size = sizeof (u32),
412
413   .runtime_data_bytes = sizeof (udp_input_runtime_t),
414
415   .n_errors = UDP_N_ERROR,
416   .error_strings = udp_error_strings,
417
418   .n_next_nodes = UDP_INPUT_N_NEXT,
419   .next_nodes = {
420 #define _(s,n) [UDP_INPUT_NEXT_##s] = n,
421     foreach_udp_input_next
422 #undef _
423   },
424
425   .format_buffer = format_udp_header,
426   .format_trace = format_udp_rx_trace,
427   .unformat_buffer = unformat_udp_header,
428 };
429
430 VLIB_NODE_FUNCTION_MULTIARCH (udp4_input_node, udp4_input)
431
432 VLIB_REGISTER_NODE (udp6_input_node) = {
433   .function = udp6_input,
434   .name = "ip6-udp-lookup",
435   /* Takes a vector of packets. */
436   .vector_size = sizeof (u32),
437
438   .runtime_data_bytes = sizeof (udp_input_runtime_t),
439
440   .n_errors = UDP_N_ERROR,
441   .error_strings = udp_error_strings,
442
443   .n_next_nodes = UDP_INPUT_N_NEXT,
444   .next_nodes = {
445 #define _(s,n) [UDP_INPUT_NEXT_##s] = n,
446     foreach_udp_input_next
447 #undef _
448   },
449
450   .format_buffer = format_udp_header,
451   .format_trace = format_udp_rx_trace,
452   .unformat_buffer = unformat_udp_header,
453 };
454
455 VLIB_NODE_FUNCTION_MULTIARCH (udp6_input_node, udp6_input)
456
457 static void add_dst_port (udp_main_t * um,
458                           udp_dst_port_t dst_port,
459                           char * dst_port_name, u8 is_ip4)
460 {
461   udp_dst_port_info_t * pi;
462   u32 i;
463
464   vec_add2 (um->dst_port_infos[is_ip4], pi, 1);
465   i = pi - um->dst_port_infos[is_ip4];
466
467   pi->name = dst_port_name;
468   pi->dst_port = dst_port;
469   pi->next_index = pi->node_index = ~0;
470   
471   hash_set (um->dst_port_info_by_dst_port[is_ip4], dst_port, i);
472
473   if (pi->name)
474     hash_set_mem (um->dst_port_info_by_name[is_ip4], pi->name, i);
475 }
476
477 void
478 udp_register_dst_port (vlib_main_t * vm,
479                        udp_dst_port_t dst_port,
480                        u32 node_index, u8 is_ip4)
481 {
482   udp_main_t * um = &udp_main;
483   udp_dst_port_info_t * pi;
484   udp_input_runtime_t * rt;
485   u16 * n;
486
487   {
488     clib_error_t * error = vlib_call_init_function (vm, udp_local_init);
489     if (error)
490       clib_error_report (error);
491   }
492
493   pi = udp_get_dst_port_info (um, dst_port, is_ip4);
494   if (! pi) 
495     {
496       add_dst_port (um, dst_port, 0, is_ip4);
497       pi = udp_get_dst_port_info (um, dst_port, is_ip4);
498       ASSERT (pi);
499     }
500       
501   pi->node_index = node_index;
502   pi->next_index = vlib_node_add_next (vm, 
503                                        is_ip4 ? udp4_input_node.index
504                                        : udp6_input_node.index,
505                                        node_index);
506
507   /* Setup udp protocol -> next index sparse vector mapping. */
508   rt = vlib_node_get_runtime_data 
509     (vm, is_ip4 ? udp4_input_node.index: udp6_input_node.index);
510   n = sparse_vec_validate (rt->next_by_dst_port, 
511                            clib_host_to_net_u16 (dst_port));
512   n[0] = pi->next_index;
513 }
514
515 void
516 udp_punt_unknown(vlib_main_t * vm, u8 is_ip4, u8 is_add)
517 {
518   udp_input_runtime_t * rt;
519
520   {
521     clib_error_t * error = vlib_call_init_function (vm, udp_local_init);
522     if (error)
523       clib_error_report (error);
524   }
525
526   rt = vlib_node_get_runtime_data 
527     (vm, is_ip4 ? udp4_input_node.index: udp6_input_node.index);
528
529   rt->punt_unknown = is_add;
530 }
531
532 /* Parse a UDP header. */
533 uword unformat_udp_header (unformat_input_t * input, va_list * args)
534 {
535   u8 ** result = va_arg (*args, u8 **);
536   udp_header_t * udp;
537   __attribute__((unused)) int old_length;
538   u16 src_port, dst_port;
539
540   /* Allocate space for IP header. */
541   {
542     void * p;
543
544     old_length = vec_len (*result);
545     vec_add2 (*result, p, sizeof (ip4_header_t));
546     udp = p;
547   }
548
549   memset (udp, 0, sizeof (udp[0]));
550   if (unformat (input, "src-port %d dst-port %d", 
551                 &src_port, &dst_port)) 
552     {
553       udp->src_port = clib_host_to_net_u16 (src_port);
554       udp->dst_port = clib_host_to_net_u16 (dst_port);
555       return 1;
556     }
557   return 0;
558 }
559
560 static void
561 udp_setup_node (vlib_main_t * vm, u32 node_index)
562 {
563   vlib_node_t * n = vlib_get_node (vm, node_index);
564   pg_node_t * pn = pg_get_node (node_index);
565
566   n->format_buffer = format_udp_header;
567   n->unformat_buffer = unformat_udp_header;
568   pn->unformat_edit = unformat_pg_udp_header;
569 }
570
571 clib_error_t * udp_local_init (vlib_main_t * vm)
572 {
573   udp_input_runtime_t * rt;
574   udp_main_t * um = &udp_main;
575   int i;
576
577   {
578     clib_error_t * error; 
579     error = vlib_call_init_function (vm, udp_init);
580     if (error)
581       clib_error_report (error);
582   }
583
584
585   for (i = 0; i < 2; i++)
586     {
587       um->dst_port_info_by_name[i] = hash_create_string (0, sizeof(uword));
588       um->dst_port_info_by_dst_port[i] = hash_create (0, sizeof(uword));
589     }
590
591   udp_setup_node (vm, udp4_input_node.index);
592   udp_setup_node (vm, udp6_input_node.index);
593
594   rt = vlib_node_get_runtime_data (vm, udp4_input_node.index);
595
596   rt->next_by_dst_port = sparse_vec_new
597     (/* elt bytes */ sizeof (rt->next_by_dst_port[0]),
598      /* bits in index */ BITS (((udp_header_t *) 0)->dst_port));
599
600   rt->punt_unknown = 0;
601
602 #define _(n,s) add_dst_port (um, UDP_DST_PORT_##s, #s, 1 /* is_ip4 */);
603   foreach_udp4_dst_port
604 #undef _
605
606   rt = vlib_node_get_runtime_data (vm, udp6_input_node.index);
607
608   rt->next_by_dst_port = sparse_vec_new
609     (/* elt bytes */ sizeof (rt->next_by_dst_port[0]),
610      /* bits in index */ BITS (((udp_header_t *) 0)->dst_port));
611
612   rt->punt_unknown = 0;
613
614 #define _(n,s) add_dst_port (um, UDP_DST_PORT_##s, #s, 0 /* is_ip4 */);
615   foreach_udp6_dst_port
616 #undef _
617
618   ip4_register_protocol (IP_PROTOCOL_UDP, udp4_input_node.index);
619   /* Note: ip6 differs from ip4, UDP is hotwired to ip6-udp-lookup */
620   return 0;
621 }
622
623 VLIB_INIT_FUNCTION (udp_local_init);