2 * Copyright (c) 2011-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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vppinfra/hash.h>
17 #include <vppinfra/pcap.h>
18 #include <vnet/srp/srp.h>
19 #include <vnet/ppp/ppp.h>
20 #include <vnet/hdlc/hdlc.h>
21 #include <vnet/srp/packet.h>
24 * Generate a set of specific CDP TLVs.
26 * $$$ eventually these need to fish better data from
27 * other data structures; e.g. the hostname, software version info
32 add_device_name_tlv (vnet_hw_interface_t * hw, u8 ** t0p)
34 cdp_tlv_t *t = (cdp_tlv_t *) * t0p;
36 t->t = htons (CDP_TLV_device_name);
37 t->l = htons (3 + sizeof (*t));
38 clib_memcpy (&t->v, "VPP", 3);
44 add_port_id_tlv (vnet_hw_interface_t * hw, u8 ** t0p)
46 cdp_tlv_t *t = (cdp_tlv_t *) * t0p;
48 t->t = htons (CDP_TLV_port_id);
49 t->l = htons (vec_len (hw->name) + sizeof (*t));
50 clib_memcpy (&t->v, hw->name, vec_len (hw->name));
55 add_version_tlv (vnet_hw_interface_t * hw, u8 ** t0p)
57 cdp_tlv_t *t = (cdp_tlv_t *) * t0p;
59 t->t = htons (CDP_TLV_version);
60 t->l = htons (12 + sizeof (*t));
61 clib_memcpy (&t->v, "VPP Software", 12);
66 add_platform_tlv (vnet_hw_interface_t * hw, u8 ** t0p)
68 cdp_tlv_t *t = (cdp_tlv_t *) * t0p;
70 t->t = htons (CDP_TLV_platform);
71 t->l = htons (2 + sizeof (*t));
72 clib_memcpy (&t->v, "SW", 2);
77 add_capability_tlv (vnet_hw_interface_t * hw, u8 ** t0p)
79 cdp_tlv_t *t = (cdp_tlv_t *) * t0p;
82 t->t = htons (CDP_TLV_capabilities);
83 t->l = htons (4 + sizeof (*t));
84 capabilities = CDP_ROUTER_DEVICE;
85 capabilities = htonl (capabilities);
86 clib_memcpy (&t->v, &capabilities, sizeof (capabilities));
91 add_tlvs (cdp_main_t * cm, vnet_hw_interface_t * hw, u8 ** t0p)
93 add_device_name_tlv (hw, t0p);
94 add_port_id_tlv (hw, t0p);
95 add_version_tlv (hw, t0p);
96 add_platform_tlv (hw, t0p);
97 add_capability_tlv (hw, t0p);
101 * send a cdp pkt on an ethernet interface
104 send_ethernet_hello (cdp_main_t * cm, cdp_neighbor_t * n, int count)
107 ethernet_llc_snap_and_cdp_header_t *h0;
108 vnet_hw_interface_t *hw;
113 int nbytes_to_checksum;
116 vlib_main_t *vm = cm->vlib_main;
117 vnet_main_t *vnm = cm->vnet_main;
119 for (i = 0; i < count; i++)
122 * see cdp_periodic_init() to understand what's already painted
123 * into the buffer by the packet template mechanism
125 h0 = vlib_packet_template_get_packet
126 (vm, &cm->packet_templates[n->packet_template_index], &bi0);
131 /* Add the interface's ethernet source address */
132 hw = vnet_get_sup_hw_interface (vnm, n->sw_if_index);
134 clib_memcpy (h0->ethernet.src_address, hw->hw_address,
135 vec_len (hw->hw_address));
137 t0 = (u8 *) & h0->cdp.data;
140 add_tlvs (cm, hw, &t0);
142 /* add the cdp packet checksum */
143 nbytes_to_checksum = t0 - (u8 *) & h0->cdp;
144 checksum = cdp_checksum (&h0->cdp, nbytes_to_checksum);
145 h0->cdp.checksum = htons (checksum);
147 /* Set the outbound packet length */
148 b0 = vlib_get_buffer (vm, bi0);
149 b0->current_length = nbytes_to_checksum + sizeof (*h0)
150 - sizeof (cdp_hdr_t);
152 /* And the outbound interface */
153 vnet_buffer (b0)->sw_if_index[VLIB_TX] = hw->sw_if_index;
155 /* Set the 802.3 ethernet length */
156 h0->ethernet.len = htons (b0->current_length
157 - sizeof (ethernet_802_3_header_t));
159 /* And output the packet on the correct interface */
160 f = vlib_get_frame_to_node (vm, hw->output_node_index);
161 to_next = vlib_frame_vector_args (f);
165 vlib_put_frame_to_node (vm, hw->output_node_index, f);
166 n->last_sent = vlib_time_now (vm);
171 * send a cdp pkt on an hdlc interface
174 send_hdlc_hello (cdp_main_t * cm, cdp_neighbor_t * n, int count)
177 hdlc_and_cdp_header_t *h0;
178 vnet_hw_interface_t *hw;
183 int nbytes_to_checksum;
186 vlib_main_t *vm = cm->vlib_main;
187 vnet_main_t *vnm = cm->vnet_main;
189 for (i = 0; i < count; i++)
192 * see cdp_periodic_init() to understand what's already painted
193 * into the buffer by the packet template mechanism
195 h0 = vlib_packet_template_get_packet
196 (vm, &cm->packet_templates[n->packet_template_index], &bi0);
201 hw = vnet_get_sup_hw_interface (vnm, n->sw_if_index);
203 t0 = (u8 *) & h0->cdp.data;
206 add_tlvs (cm, hw, &t0);
208 /* add the cdp packet checksum */
209 nbytes_to_checksum = t0 - (u8 *) & h0->cdp;
210 checksum = cdp_checksum (&h0->cdp, nbytes_to_checksum);
211 h0->cdp.checksum = htons (checksum);
213 /* Set the outbound packet length */
214 b0 = vlib_get_buffer (vm, bi0);
215 b0->current_length = nbytes_to_checksum + sizeof (*h0)
216 - sizeof (cdp_hdr_t);
218 /* And output the packet on the correct interface */
219 f = vlib_get_frame_to_node (vm, hw->output_node_index);
220 to_next = vlib_frame_vector_args (f);
224 vlib_put_frame_to_node (vm, hw->output_node_index, f);
225 n->last_sent = vlib_time_now (vm);
230 * send a cdp pkt on an srp interface
233 send_srp_hello (cdp_main_t * cm, cdp_neighbor_t * n, int count)
236 srp_and_cdp_header_t *h0;
237 vnet_hw_interface_t *hw;
242 int nbytes_to_checksum;
245 vlib_main_t *vm = cm->vlib_main;
246 vnet_main_t *vnm = cm->vnet_main;
248 for (i = 0; i < count; i++)
251 * see cdp_periodic_init() to understand what's already painted
252 * into the buffer by the packet template mechanism
254 h0 = vlib_packet_template_get_packet
255 (vm, &cm->packet_templates[n->packet_template_index], &bi0);
260 hw = vnet_get_sup_hw_interface (vnm, n->sw_if_index);
262 t0 = (u8 *) & h0->cdp.data;
265 add_tlvs (cm, hw, &t0);
267 /* Add the interface's ethernet source address */
268 clib_memcpy (h0->ethernet.src_address, hw->hw_address,
269 vec_len (hw->hw_address));
271 /* add the cdp packet checksum */
272 nbytes_to_checksum = t0 - (u8 *) & h0->cdp;
273 checksum = cdp_checksum (&h0->cdp, nbytes_to_checksum);
274 h0->cdp.checksum = htons (checksum);
276 /* Set the outbound packet length */
277 b0 = vlib_get_buffer (vm, bi0);
278 b0->current_length = nbytes_to_checksum + sizeof (*h0)
279 - sizeof (cdp_hdr_t);
281 /* And output the packet on the correct interface */
282 f = vlib_get_frame_to_node (vm, hw->output_node_index);
283 to_next = vlib_frame_vector_args (f);
287 vlib_put_frame_to_node (vm, hw->output_node_index, f);
288 n->last_sent = vlib_time_now (vm);
293 * Decide which cdp packet template to use
296 pick_packet_template (cdp_main_t * cm, cdp_neighbor_t * n)
298 n->packet_template_index = CDP_PACKET_TEMPLATE_ETHERNET;
303 /* Send a cdp neighbor announcement */
305 send_hello (cdp_main_t * cm, cdp_neighbor_t * n, int count)
307 if (n->packet_template_index == (u8) ~ 0)
309 /* If we don't know how to talk to this peer, don't try again */
310 if (pick_packet_template (cm, n))
317 switch (n->packet_template_index)
319 case CDP_PACKET_TEMPLATE_ETHERNET:
320 send_ethernet_hello (cm, n, count);
323 case CDP_PACKET_TEMPLATE_HDLC:
324 send_hdlc_hello (cm, n, count);
327 case CDP_PACKET_TEMPLATE_SRP:
328 send_srp_hello (cm, n, count);
334 n->last_sent = vlib_time_now (cm->vlib_main);
338 delete_neighbor (cdp_main_t * cm, cdp_neighbor_t * n, int want_broadcast)
340 hash_unset (cm->neighbor_by_sw_if_index, n->sw_if_index);
341 vec_free (n->device_name);
342 vec_free (n->version);
343 vec_free (n->port_id);
344 vec_free (n->platform);
345 vec_free (n->last_rx_pkt);
346 pool_put (cm->neighbors, n);
350 cdp_periodic (vlib_main_t * vm)
352 cdp_main_t *cm = &cdp_main;
354 f64 now = vlib_time_now (vm);
355 vnet_sw_interface_t *sw;
356 static u32 *delete_list = 0;
358 static cdp_neighbor_t **n_list = 0;
361 pool_foreach (n, cm->neighbors,
363 vec_add1 (n_list, n);
367 /* Across all cdp neighbors known to the system */
368 for (i = 0; i < vec_len (n_list); i++)
372 /* "no cdp run" provisioned on the interface? */
373 if (n->disabled == 1)
376 sw = vnet_get_sw_interface (cm->vnet_main, n->sw_if_index);
378 /* Interface shutdown or rx timeout? */
379 if (!(sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
380 || (now > (n->last_heard + (f64) n->ttl_in_seconds)))
381 /* add to list of neighbors to delete */
382 vec_add1 (delete_list, n - cm->neighbors);
383 else if (n->last_sent == 0.0)
384 /* First time, send 3 hellos */
385 send_hello (cm, n, 3 /* three to begin with */ );
386 else if (now > (n->last_sent + (((f64) n->ttl_in_seconds) / 6.0)))
387 /* Normal keepalive, send one */
388 send_hello (cm, n, 1 /* one as a keepalive */ );
391 for (i = 0; i < vec_len (delete_list); i++)
393 n = vec_elt_at_index (cm->neighbors, delete_list[i]);
394 delete_neighbor (cm, n, 1);
397 _vec_len (delete_list) = 0;
399 _vec_len (n_list) = 0;
402 static clib_error_t *
403 cdp_periodic_init (vlib_main_t * vm)
405 cdp_main_t *cm = &cdp_main;
407 /* Create the ethernet cdp hello packet template */
409 ethernet_llc_snap_and_cdp_header_t h;
411 clib_memset (&h, 0, sizeof (h));
413 /* Send to 01:00:0c:cc:cc */
414 h.ethernet.dst_address[0] = 0x01;
415 /* h.ethernet.dst_address[1] = 0x00; (clib_memset) */
416 h.ethernet.dst_address[2] = 0x0C;
417 h.ethernet.dst_address[3] = 0xCC;
418 h.ethernet.dst_address[4] = 0xCC;
419 h.ethernet.dst_address[5] = 0xCC;
421 /* leave src address blank (fill in at send time) */
423 /* leave length blank (fill in at send time) */
426 h.llc.dst_sap = h.llc.src_sap = 0xAA; /* SNAP */
427 h.llc.control = 0x03; /* UI (no extended control bytes) */
430 /* h.snap.oui[0] = 0x00; (clib_memset) */
431 /* h.snap.oui[1] = 0x00; (clib_memset) */
432 h.snap.oui[2] = 0x0C; /* Cisco = 0x00000C */
433 h.snap.protocol = htons (0x2000); /* CDP = 0x2000 */
439 vlib_packet_template_init
440 (vm, &cm->packet_templates[CDP_PACKET_TEMPLATE_ETHERNET],
443 /* alloc chunk size */ 8,
447 #if 0 /* retain for reference */
449 /* Create the hdlc cdp hello packet template */
451 hdlc_and_cdp_header_t h;
453 clib_memset (&h, 0, sizeof (h));
455 h.hdlc.address = 0x0f;
456 /* h.hdlc.control = 0; (clib_memset) */
457 h.hdlc.protocol = htons (0x2000); /* CDP = 0x2000 */
463 vlib_packet_template_init
464 (vm, &cm->packet_templates[CDP_PACKET_TEMPLATE_HDLC],
467 /* alloc chunk size */ 8,
471 /* Create the srp cdp hello packet template */
473 srp_and_cdp_header_t h;
475 clib_memset (&h, 0, sizeof (h));
477 /* Send to 01:00:0c:cc:cc */
478 h.ethernet.dst_address[0] = 0x01;
479 /* h.ethernet.dst_address[1] = 0x00; (clib_memset) */
480 h.ethernet.dst_address[2] = 0x0C;
481 h.ethernet.dst_address[3] = 0xCC;
482 h.ethernet.dst_address[4] = 0xCC;
483 h.ethernet.dst_address[5] = 0xCC;
485 /* leave src address blank (fill in at send time) */
487 /* The srp header is filled in at xmt */
490 h.srp.mode = SRP_MODE_data;
491 srp_header_compute_parity (&h.srp);
493 /* Inner ring and parity will be set at send time */
495 h.ethernet.type = htons (0x2000); /* CDP = 0x2000 */
501 vlib_packet_template_init
502 (vm, &cm->packet_templates[CDP_PACKET_TEMPLATE_SRP],
505 /* alloc chunk size */ 8,
513 VLIB_INIT_FUNCTION (cdp_periodic_init);
516 * fd.io coding-style-patch-verification: ON
519 * eval: (c-set-style "gnu")