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