66ae97142c5d0132b0cf57e6648776c2d7848acd
[vpp.git] / src / plugins / cdp / cdp_input.c
1 /*
2  * Copyright (c) 2011-2016 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 <cdp/cdp.h>
16
17 cdp_main_t cdp_main;
18
19 #define DEBUG_TLV_DUMP 0        /* 1=> dump TLV's to stdout while processing them */
20
21 /*
22  * ported from an unspecified Cisco cdp implementation.
23  * Compute / return in HOST byte order. 0 => good checksum.
24  */
25 u16
26 cdp_checksum (void *p, int count)
27 {
28   u32 sum;
29   u16 i, *data;
30
31   data = p;
32   sum = 0;
33   while (count > 1)
34     {
35       sum += ntohs (*data);
36       data++;
37       count -= 2;
38     }
39
40   if (count > 0)
41     sum += *(char *) data;
42
43   while (sum >> 16)
44     {
45       sum = (sum & 0xFFFF) + (sum >> 16);
46     }
47
48   i = (i16) sum;
49   return (~i);
50 }
51
52 /* TLV handler table */
53 typedef struct
54 {
55   char *name;
56   u32 tlv_id;
57   void *format;
58   void *process;
59 } tlv_handler_t;
60
61 static tlv_handler_t tlv_handlers[];
62
63 /* Display a generic TLV as a set of hex bytes */
64 static u8 *
65 format_generic_tlv (u8 * s, va_list * va)
66 {
67   cdp_tlv_t *t = va_arg (*va, cdp_tlv_t *);
68   tlv_handler_t *h = &tlv_handlers[t->t];
69
70   s = format (s, "%s(%d): %U\n", h->name,
71               t->t, format_hex_bytes, t->v, t->l - sizeof (*t));
72   return s;
73 }
74
75 /* Ignore / skip a TLV we don't support */
76 static cdp_error_t
77 process_generic_tlv (cdp_main_t * cm, cdp_neighbor_t * n, cdp_tlv_t * t)
78 {
79 #if DEBUG_TLV_DUMP > 0
80   fformat (stdout, "%U", format_generic_tlv, t);
81 #endif
82
83   return CDP_ERROR_NONE;
84 }
85
86 /* print a text tlv */
87 static u8 *
88 format_text_tlv (u8 * s, va_list * va)
89 {
90   cdp_tlv_t *t = va_arg (*va, cdp_tlv_t *);
91   tlv_handler_t *h = &tlv_handlers[t->t];
92   int i;
93
94   s = format (s, "%s(%d): ", h->name, t->t);
95
96   for (i = 0; i < (t->l - sizeof (*t)); i++)
97     vec_add1 (s, t->v[i]);
98
99   vec_add1 (s, '\n');
100   return s;
101 }
102
103 #if DEBUG_TLV_DUMP == 0
104 /* gcc warning be gone */
105 CLIB_UNUSED (static cdp_error_t
106              process_text_tlv (cdp_main_t * cm, cdp_neighbor_t * n,
107                                cdp_tlv_t * t));
108 #endif
109
110 /* process / skip a generic text TLV that we don't support */
111 static cdp_error_t
112 process_text_tlv (cdp_main_t * cm, cdp_neighbor_t * n, cdp_tlv_t * t)
113 {
114 #if DEBUG_TLV_DUMP > 0
115   fformat (stdout, "%U\n", format_text_tlv, t);
116 #endif
117
118   return CDP_ERROR_NONE;
119 }
120
121 /* per-TLV format function definitions */
122 #define format_unused_tlv format_generic_tlv
123 #define format_device_name_tlv format_text_tlv
124 #define format_address_tlv format_generic_tlv
125 #define format_port_id_tlv format_text_tlv
126 #define format_capabilities_tlv format_generic_tlv
127 #define format_version_tlv format_text_tlv
128 #define format_platform_tlv format_text_tlv
129 #define format_ipprefix_tlv format_generic_tlv
130 #define format_hello_tlv format_generic_tlv
131 #define format_vtp_domain_tlv format_generic_tlv
132 #define format_native_vlan_tlv format_generic_tlv
133 #define format_duplex_tlv format_generic_tlv
134 #define format_appl_vlan_tlv format_generic_tlv
135 #define format_trigger_tlv format_generic_tlv
136 #define format_power_tlv format_generic_tlv
137 #define format_mtu_tlv format_generic_tlv
138 #define format_trust_tlv format_generic_tlv
139 #define format_cos_tlv format_generic_tlv
140 #define format_sysname_tlv format_generic_tlv
141 #define format_sysobject_tlv format_generic_tlv
142 #define format_mgmt_addr_tlv format_generic_tlv
143 #define format_physical_loc_tlv format_generic_tlv
144 #define format_mgmt_addr2_tlv format_generic_tlv
145 #define format_power_requested_tlv format_generic_tlv
146 #define format_power_available_tlv format_generic_tlv
147 #define format_port_unidirectional_tlv format_generic_tlv
148 #define format_unknown_28_tlv format_generic_tlv
149 #define format_energywise_tlv format_generic_tlv
150 #define format_unknown_30_tlv format_generic_tlv
151 #define format_spare_poe_tlv format_generic_tlv
152
153 /* tlv ID=0 is a mistake */
154 static cdp_error_t
155 process_unused_tlv (cdp_main_t * cm, cdp_neighbor_t * n, cdp_tlv_t * t)
156 {
157   return CDP_ERROR_BAD_TLV;
158 }
159
160 /* list of text TLV's that we snapshoot */
161 #define foreach_text_to_struct_tlv              \
162 _(device_name,DEBUG_TLV_DUMP)                   \
163 _(version,DEBUG_TLV_DUMP)                       \
164 _(platform,DEBUG_TLV_DUMP)                      \
165 _(port_id,DEBUG_TLV_DUMP)
166
167 #define _(z,dbg)                                                        \
168 static                                                                  \
169 cdp_error_t process_##z##_tlv (cdp_main_t *cm, cdp_neighbor_t *n,       \
170                                   cdp_tlv_t *t)                         \
171 {                                                                       \
172     int i;                                                              \
173     if (dbg)                                                            \
174        fformat(stdout, "%U\n", format_text_tlv, t);                     \
175                                                                         \
176     if (n->z)                                                           \
177         _vec_len(n->z) = 0;                                             \
178                                                                         \
179     for (i = 0; i < (t->l - sizeof (*t)); i++)                          \
180         vec_add1(n->z, t->v[i]);                                        \
181                                                                         \
182     vec_add1(n->z, 0);                                                  \
183                                                                         \
184     return CDP_ERROR_NONE;                                              \
185 }
186
187 foreach_text_to_struct_tlv
188 #undef _
189 #define process_address_tlv process_generic_tlv
190 #define process_capabilities_tlv process_generic_tlv
191 #define process_ipprefix_tlv process_generic_tlv
192 #define process_hello_tlv process_generic_tlv
193 #define process_vtp_domain_tlv process_generic_tlv
194 #define process_native_vlan_tlv process_generic_tlv
195 #define process_duplex_tlv process_generic_tlv
196 #define process_appl_vlan_tlv process_generic_tlv
197 #define process_trigger_tlv process_generic_tlv
198 #define process_power_tlv process_generic_tlv
199 #define process_mtu_tlv process_generic_tlv
200 #define process_trust_tlv process_generic_tlv
201 #define process_cos_tlv process_generic_tlv
202 #define process_sysname_tlv process_generic_tlv
203 #define process_sysobject_tlv process_generic_tlv
204 #define process_mgmt_addr_tlv process_generic_tlv
205 #define process_physical_loc_tlv process_generic_tlv
206 #define process_mgmt_addr2_tlv process_generic_tlv
207 #define process_power_requested_tlv process_generic_tlv
208 #define process_power_available_tlv process_generic_tlv
209 #define process_port_unidirectional_tlv process_generic_tlv
210 #define process_unknown_28_tlv process_generic_tlv
211 #define process_energywise_tlv process_generic_tlv
212 #define process_unknown_30_tlv process_generic_tlv
213 #define process_spare_poe_tlv process_generic_tlv
214 static tlv_handler_t tlv_handlers[] = {
215 #define _(a) {#a, CDP_TLV_##a, format_##a##_tlv, process_##a##_tlv},
216   foreach_cdp_tlv_type
217 #undef _
218 };
219
220 #if DEBUG_TLV_DUMP == 0
221 CLIB_UNUSED (static u8 * format_cdp_hdr (u8 * s, va_list * va));
222 #endif
223
224 static u8 *
225 format_cdp_hdr (u8 * s, va_list * va)
226 {
227   cdp_hdr_t *h = va_arg (*va, cdp_hdr_t *);
228
229   s = format (s, "version %d, ttl %d(secs), cksum 0x%04x\n",
230               h->version, h->ttl, h->checksum);
231   return s;
232 }
233
234 static cdp_error_t
235 process_cdp_hdr (cdp_main_t * cm, cdp_neighbor_t * n, cdp_hdr_t * h)
236 {
237 #if DEBUG_TLV_DUMP > 0
238   fformat (stdout, "%U", format_cdp_hdr, h);
239 #endif
240
241   if (h->version != 1 && h->version != 2)
242     return CDP_ERROR_PROTOCOL_VERSION;
243
244   n->ttl_in_seconds = h->ttl;
245
246   return CDP_ERROR_NONE;
247 }
248
249 /* scan a cdp packet; header, then tlv's */
250 static int
251 cdp_packet_scan (cdp_main_t * cm, cdp_neighbor_t * n)
252 {
253   u8 *end, *cur = n->last_rx_pkt;
254   cdp_hdr_t *h;
255   cdp_tlv_t *tlv;
256   cdp_error_t e = CDP_ERROR_NONE;
257   tlv_handler_t *handler;
258   cdp_error_t (*fp) (cdp_main_t *, cdp_neighbor_t *, cdp_tlv_t *);
259   u16 computed_checksum;
260
261   computed_checksum = cdp_checksum (cur, vec_len (cur));
262
263   if (computed_checksum)
264     return CDP_ERROR_CHECKSUM;
265
266   h = (cdp_hdr_t *) cur;
267
268   e = process_cdp_hdr (cm, n, h);
269   if (e)
270     return e;
271
272   // there are no tlvs
273   if (vec_len (n->last_rx_pkt) <= 0)
274     return CDP_ERROR_BAD_TLV;
275
276   cur = (u8 *) (h + 1);
277   end = n->last_rx_pkt + vec_len (n->last_rx_pkt) - 1;
278
279   // look ahead 4 bytes (u16 tlv->t + u16 tlv->l)
280   while (cur + 3 <= end)
281     {
282       tlv = (cdp_tlv_t *) cur;
283       tlv->t = ntohs (tlv->t);
284       tlv->l = ntohs (tlv->l);
285
286       /* tlv length includes t, l and v */
287       cur += tlv->l;
288       if ((cur - 1) > end)
289         return CDP_ERROR_BAD_TLV;
290       /*
291        * Only process known TLVs. In practice, certain
292        * devices send tlv->t = 0xFF, perhaps as an EOF of sorts.
293        */
294       if (tlv->t < ARRAY_LEN (tlv_handlers))
295         {
296           handler = &tlv_handlers[tlv->t];
297           fp = handler->process;
298           e = (*fp) (cm, n, tlv);
299           if (e)
300             return e;
301         }
302     }
303
304   // did not process all tlvs or none tlv processed
305   if ((cur - 1) != end)
306     return CDP_ERROR_BAD_TLV;
307
308   return CDP_ERROR_NONE;
309 }
310
311 /*
312  * cdp input routine
313  */
314 cdp_error_t
315 cdp_input (vlib_main_t * vm, vlib_buffer_t * b0, u32 bi0)
316 {
317   cdp_main_t *cm = &cdp_main;
318   cdp_neighbor_t *n;
319   uword *p, nbytes;
320   cdp_error_t e;
321   uword last_packet_signature;
322
323   /* find or create a neighbor pool entry for the (sw) interface
324      upon which we received this pkt */
325   p = hash_get (cm->neighbor_by_sw_if_index,
326                 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
327
328   if (p == 0)
329     {
330       pool_get (cm->neighbors, n);
331       memset (n, 0, sizeof (*n));
332       n->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
333       n->packet_template_index = (u8) ~ 0;
334       hash_set (cm->neighbor_by_sw_if_index, n->sw_if_index,
335                 n - cm->neighbors);
336     }
337   else
338     {
339       n = pool_elt_at_index (cm->neighbors, p[0]);
340     }
341
342   /*
343    * typical clib idiom. Don't repeatedly allocate and free
344    * the per-neighbor rx buffer. Reset its apparent length to zero
345    * and reuse it.
346    */
347
348   if (n->last_rx_pkt)
349     _vec_len (n->last_rx_pkt) = 0;
350
351   /* cdp disabled on this interface, we're done */
352   if (n->disabled)
353     return CDP_ERROR_DISABLED;
354
355   /*
356    * Make sure the per-neighbor rx buffer is big enough to hold
357    * the data we're about to copy
358    */
359   vec_validate (n->last_rx_pkt, vlib_buffer_length_in_chain (vm, b0) - 1);
360
361   /*
362    * Coalesce / copy e the buffer chain into the per-neighbor
363    * rx buffer
364    */
365   nbytes = vlib_buffer_contents (vm, bi0, n->last_rx_pkt);
366   ASSERT (nbytes <= vec_len (n->last_rx_pkt));
367
368   /*
369    * Compute Jenkins hash of the new packet, decide if we need to
370    * actually parse through the TLV's. CDP packets are all identical,
371    * so unless we time out the peer, we don't need to process the packet.
372    */
373   last_packet_signature =
374     hash_memory (n->last_rx_pkt, vec_len (n->last_rx_pkt), 0xd00b);
375
376   if (n->last_packet_signature_valid &&
377       n->last_packet_signature == last_packet_signature)
378     {
379       e = CDP_ERROR_CACHE_HIT;
380     }
381   else
382     {
383       /* Actually scan the packet */
384       e = cdp_packet_scan (cm, n);
385       n->last_packet_signature_valid = 1;
386       n->last_packet_signature = last_packet_signature;
387     }
388
389   if (e == CDP_ERROR_NONE)
390     {
391       n->last_heard = vlib_time_now (vm);
392     }
393
394   return e;
395 }
396
397 /*
398  * setup neighbor hash table
399  */
400 static clib_error_t *
401 cdp_input_init (vlib_main_t * vm)
402 {
403   clib_error_t *error;
404   cdp_main_t *cm = &cdp_main;
405   void vnet_cdp_node_reference (void);
406
407   vnet_cdp_node_reference ();
408
409   if ((error = vlib_call_init_function (vm, cdp_periodic_init)))
410     return error;
411
412   cm->vlib_main = vm;
413   cm->vnet_main = vnet_get_main ();
414   cm->neighbor_by_sw_if_index = hash_create (0, sizeof (uword));
415
416   return 0;
417 }
418
419 VLIB_INIT_FUNCTION (cdp_input_init);
420
421
422 static u8 *
423 format_cdp_neighbors (u8 * s, va_list * va)
424 {
425   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
426   cdp_main_t *cm = va_arg (*va, cdp_main_t *);
427   vnet_main_t *vnm = &vnet_main;
428   cdp_neighbor_t *n;
429   vnet_hw_interface_t *hw;
430
431   s = format (s,
432               "%=25s %=15s %=25s %=10s\n",
433               "Our Port", "Peer System", "Peer Port", "Last Heard");
434
435   /* *INDENT-OFF* */
436   pool_foreach (n, cm->neighbors,
437   ({
438     hw = vnet_get_sup_hw_interface (vnm, n->sw_if_index);
439
440     if (n->disabled == 0)
441       s = format (s, "%=25s %=15s %=25s %=10.1f\n",
442                   hw->name, n->device_name, n->port_id,
443                   n->last_heard);
444   }));
445   /* *INDENT-ON* */
446   return s;
447 }
448
449 static clib_error_t *
450 show_cdp (vlib_main_t * vm,
451           unformat_input_t * input, vlib_cli_command_t * cmd)
452 {
453   cdp_main_t *cm = &cdp_main;
454
455   if (cm->enabled == 0)
456     vlib_cli_output (vm, "CDP is not enabled...");
457   else
458     vlib_cli_output (vm, "%U\n", format_cdp_neighbors, vm, cm);
459
460   return 0;
461 }
462
463 /* *INDENT-OFF* */
464 VLIB_CLI_COMMAND (show_cdp_command, static) = {
465   .path = "show cdp",
466   .short_help = "Show cdp command",
467   .function = show_cdp,
468 };
469 /* *INDENT-ON* */
470
471
472 /*
473  * packet trace format function, very similar to
474  * cdp_packet_scan except that we call the per TLV format
475  * functions instead of the per TLV processing functions
476  */
477 u8 *
478 cdp_input_format_trace (u8 * s, va_list * args)
479 {
480   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
481   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
482   cdp_input_trace_t *t = va_arg (*args, cdp_input_trace_t *);
483   u8 *cur;
484   cdp_hdr_t *h;
485   cdp_tlv_t *tlv;
486   tlv_handler_t *handler;
487   u8 *(*fp) (cdp_tlv_t *);
488
489   cur = t->data;
490
491   h = (cdp_hdr_t *) cur;
492   s = format (s, "%U", format_cdp_hdr, h);
493
494   cur = (u8 *) (h + 1);
495
496   while (cur < t->data + t->len)
497     {
498       tlv = (cdp_tlv_t *) cur;
499       tlv->t = ntohs (tlv->t);
500       tlv->l = ntohs (tlv->l);
501       if (tlv->t >= ARRAY_LEN (tlv_handlers))
502         {
503           s = format (s, "BAD_TLV\n");
504           break;
505         }
506       handler = &tlv_handlers[tlv->t];
507       fp = handler->format;
508       s = format (s, "  %U", fp, tlv);
509       /* tlv length includes (t, l) */
510       cur += tlv->l;
511     }
512
513   return s;
514 }
515
516 /*
517  * fd.io coding-style-patch-verification: ON
518  *
519  * Local Variables:
520  * eval: (c-set-style "gnu")
521  * End:
522  */