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:
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.
15 #include <vnet/cdp/cdp_node.h>
16 #include <vppinfra/hash.h>
17 #include <vnet/unix/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
31 static void add_device_name_tlv (vnet_hw_interface_t *hw, u8 **t0p)
33 cdp_tlv_t *t = (cdp_tlv_t *) *t0p;
35 t->t = htons(CDP_TLV_device_name);
36 t->l = htons(3 + sizeof (*t));
37 memcpy (&t->v, "VPP", 3);
42 static void add_port_id_tlv (vnet_hw_interface_t *hw, u8 **t0p)
44 cdp_tlv_t *t = (cdp_tlv_t *) *t0p;
46 t->t = htons(CDP_TLV_port_id);
47 t->l = htons(vec_len(hw->name) + sizeof (*t));
48 memcpy (&t->v, hw->name, vec_len (hw->name));
52 static void add_version_tlv (vnet_hw_interface_t *hw, u8 **t0p)
54 cdp_tlv_t *t = (cdp_tlv_t *) *t0p;
56 t->t = htons(CDP_TLV_version);
57 t->l = htons(12 + sizeof (*t));
58 memcpy (&t->v, "VPP Software", 12);
62 static void add_platform_tlv (vnet_hw_interface_t *hw, u8 **t0p)
64 cdp_tlv_t *t = (cdp_tlv_t *) *t0p;
66 t->t = htons(CDP_TLV_platform);
67 t->l = htons(2 + sizeof (*t));
68 memcpy (&t->v, "SW", 2);
72 static void add_capability_tlv (vnet_hw_interface_t *hw, u8 **t0p)
74 cdp_tlv_t *t = (cdp_tlv_t *) *t0p;
77 t->t = htons(CDP_TLV_capabilities);
78 t->l = htons(4 + sizeof (*t));
79 capabilities = CDP_ROUTER_DEVICE;
80 capabilities = htonl (capabilities);
81 memcpy (&t->v, &capabilities, sizeof (capabilities));
85 static void add_tlvs (cdp_main_t *cm, vnet_hw_interface_t *hw, u8 **t0p)
87 add_device_name_tlv (hw, t0p);
88 add_port_id_tlv (hw, t0p);
89 add_version_tlv (hw, t0p);
90 add_platform_tlv (hw, t0p);
91 add_capability_tlv (hw, t0p);
95 * send a cdp pkt on an ethernet interface
98 send_ethernet_hello (cdp_main_t *cm, cdp_neighbor_t *n, int count)
101 ethernet_llc_snap_and_cdp_header_t *h0;
102 vnet_hw_interface_t *hw;
107 int nbytes_to_checksum;
110 vlib_main_t * vm = cm->vlib_main;
111 vnet_main_t * vnm = cm->vnet_main;
113 for (i = 0; i < count; i++) {
115 * see cdp_periodic_init() to understand what's already painted
116 * into the buffer by the packet template mechanism
118 h0 = vlib_packet_template_get_packet
120 &cm->packet_templates[n->packet_template_index], &bi0);
122 /* Add the interface's ethernet source address */
123 hw = vnet_get_sup_hw_interface (vnm, n->sw_if_index);
125 memcpy (h0->ethernet.src_address, hw->hw_address,
126 vec_len (hw->hw_address));
128 t0 = (u8 *) &h0->cdp.data;
131 add_tlvs (cm, hw, &t0);
133 /* add the cdp packet checksum */
134 nbytes_to_checksum = t0 - (u8 *)&h0->cdp;
135 checksum = cdp_checksum (&h0->cdp, nbytes_to_checksum);
136 h0->cdp.checksum = htons (checksum);
138 /* Set the outbound packet length */
139 b0 = vlib_get_buffer (vm, bi0);
140 b0->current_length = nbytes_to_checksum + sizeof (*h0)
141 - sizeof (cdp_hdr_t) ;
143 /* Set the 802.3 ethernet length */
144 h0->ethernet.len = htons (b0->current_length
145 - sizeof (ethernet_802_3_header_t));
147 /* And output the packet on the correct interface */
148 f = vlib_get_frame_to_node (vm, hw->output_node_index);
149 to_next = vlib_frame_vector_args (f);
153 vlib_put_frame_to_node (vm, hw->output_node_index, f);
154 n->last_sent = vlib_time_now (vm);
159 * send a cdp pkt on an hdlc interface
162 send_hdlc_hello (cdp_main_t *cm, cdp_neighbor_t *n, int count)
165 hdlc_and_cdp_header_t *h0;
166 vnet_hw_interface_t *hw;
171 int nbytes_to_checksum;
174 vlib_main_t * vm = cm->vlib_main;
175 vnet_main_t * vnm = cm->vnet_main;
177 for (i = 0; i < count; i++) {
179 * see cdp_periodic_init() to understand what's already painted
180 * into the buffer by the packet template mechanism
182 h0 = vlib_packet_template_get_packet
184 &cm->packet_templates[n->packet_template_index], &bi0);
186 hw = vnet_get_sup_hw_interface (vnm, n->sw_if_index);
188 t0 = (u8 *) &h0->cdp.data;
191 add_tlvs (cm, hw, &t0);
193 /* add the cdp packet checksum */
194 nbytes_to_checksum = t0 - (u8 *)&h0->cdp;
195 checksum = cdp_checksum (&h0->cdp, nbytes_to_checksum);
196 h0->cdp.checksum = htons (checksum);
198 /* Set the outbound packet length */
199 b0 = vlib_get_buffer (vm, bi0);
200 b0->current_length = nbytes_to_checksum + sizeof (*h0)
201 - sizeof (cdp_hdr_t) ;
203 /* And output the packet on the correct interface */
204 f = vlib_get_frame_to_node (vm, hw->output_node_index);
205 to_next = vlib_frame_vector_args (f);
209 vlib_put_frame_to_node (vm, hw->output_node_index, f);
210 n->last_sent = vlib_time_now (vm);
215 * send a cdp pkt on an srp interface
218 send_srp_hello (cdp_main_t *cm, cdp_neighbor_t *n, int count)
221 srp_and_cdp_header_t *h0;
222 vnet_hw_interface_t *hw;
227 int nbytes_to_checksum;
230 vlib_main_t * vm = cm->vlib_main;
231 vnet_main_t * vnm = cm->vnet_main;
233 for (i = 0; i < count; i++) {
235 * see cdp_periodic_init() to understand what's already painted
236 * into the buffer by the packet template mechanism
238 h0 = vlib_packet_template_get_packet
240 &cm->packet_templates[n->packet_template_index], &bi0);
242 hw = vnet_get_sup_hw_interface (vnm, n->sw_if_index);
244 t0 = (u8 *) &h0->cdp.data;
247 add_tlvs (cm, hw, &t0);
249 /* Add the interface's ethernet source address */
250 memcpy (h0->ethernet.src_address, hw->hw_address,
251 vec_len (hw->hw_address));
253 /* add the cdp packet checksum */
254 nbytes_to_checksum = t0 - (u8 *)&h0->cdp;
255 checksum = cdp_checksum (&h0->cdp, nbytes_to_checksum);
256 h0->cdp.checksum = htons (checksum);
258 /* Set the outbound packet length */
259 b0 = vlib_get_buffer (vm, bi0);
260 b0->current_length = nbytes_to_checksum + sizeof (*h0)
261 - sizeof (cdp_hdr_t) ;
263 /* And output the packet on the correct interface */
264 f = vlib_get_frame_to_node (vm, hw->output_node_index);
265 to_next = vlib_frame_vector_args (f);
269 vlib_put_frame_to_node (vm, hw->output_node_index, f);
270 n->last_sent = vlib_time_now (vm);
275 * Decide which cdp packet template to use
277 static int pick_packet_template (cdp_main_t *cm, cdp_neighbor_t *n)
279 n->packet_template_index = CDP_PACKET_TEMPLATE_ETHERNET;
284 /* Send a cdp neighbor announcement */
285 static void send_hello (cdp_main_t *cm, cdp_neighbor_t *n, int count)
287 if (n->packet_template_index == (u8)~0) {
288 /* If we don't know how to talk to this peer, don't try again */
289 if (pick_packet_template (cm, n)) {
295 switch (n->packet_template_index)
297 case CDP_PACKET_TEMPLATE_ETHERNET:
298 send_ethernet_hello (cm, n, count);
301 case CDP_PACKET_TEMPLATE_HDLC:
302 send_hdlc_hello (cm, n, count);
305 case CDP_PACKET_TEMPLATE_SRP:
306 send_srp_hello (cm, n, count);
312 n->last_sent = vlib_time_now (cm->vlib_main);
315 static void delete_neighbor (cdp_main_t *cm, cdp_neighbor_t *n,
318 hash_unset (cm->neighbor_by_sw_if_index, n->sw_if_index);
319 vec_free (n->device_name);
320 vec_free (n->version);
321 vec_free (n->port_id);
322 vec_free (n->platform);
323 vec_free (n->last_rx_pkt);
324 pool_put (cm->neighbors, n);
327 void cdp_periodic (vlib_main_t * vm)
329 cdp_main_t *cm = &cdp_main;
331 f64 now = vlib_time_now (vm);
332 vnet_sw_interface_t *sw;
333 static u32 * delete_list = 0;
335 static cdp_neighbor_t **n_list = 0;
337 pool_foreach (n, cm->neighbors,
339 vec_add1 (n_list, n);
342 /* Across all cdp neighbors known to the system */
343 for (i = 0; i < vec_len (n_list); i++) {
346 /* "no cdp run" provisioned on the interface? */
347 if (n->disabled == 1)
350 sw = vnet_get_sw_interface (cm->vnet_main, n->sw_if_index);
352 /* Interface shutdown or rx timeout? */
353 if (!(sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
354 || (now > (n->last_heard + (f64)n->ttl_in_seconds)))
355 /* add to list of neighbors to delete */
356 vec_add1 (delete_list, n - cm->neighbors);
357 else if (n->last_sent == 0.0)
358 /* First time, send 3 hellos */
359 send_hello (cm, n, 3 /* three to begin with */);
360 else if (now > (n->last_sent + (((f64)n->ttl_in_seconds)/6.0)))
361 /* Normal keepalive, send one */
362 send_hello (cm, n, 1 /* one as a keepalive */);
365 for (i = 0; i < vec_len (delete_list); i++) {
366 n = vec_elt_at_index (cm->neighbors, delete_list[i]);
367 delete_neighbor (cm, n, 1);
370 _vec_len (delete_list) = 0;
372 _vec_len (n_list) = 0;
375 static clib_error_t *cdp_periodic_init (vlib_main_t * vm)
377 cdp_main_t * cm = &cdp_main;
379 /* Create the ethernet cdp hello packet template */
381 ethernet_llc_snap_and_cdp_header_t h;
383 memset (&h, 0, sizeof (h));
385 /* Send to 01:00:0c:cc:cc */
386 h.ethernet.dst_address[0] = 0x01;
387 /* h.ethernet.dst_address[1] = 0x00; (memset) */
388 h.ethernet.dst_address[2] = 0x0C;
389 h.ethernet.dst_address[3] = 0xCC;
390 h.ethernet.dst_address[4] = 0xCC;
391 h.ethernet.dst_address[5] = 0xCC;
393 /* leave src address blank (fill in at send time) */
395 /* leave length blank (fill in at send time) */
398 h.llc.dst_sap = h.llc.src_sap = 0xAA; /* SNAP */
399 h.llc.control = 0x03; /* UI (no extended control bytes) */
402 /* h.snap.oui[0] = 0x00; (memset) */
403 /* h.snap.oui[1] = 0x00; (memset) */
404 h.snap.oui[2] = 0x0C; /* Cisco = 0x00000C */
405 h.snap.protocol = htons (0x2000); /* CDP = 0x2000 */
411 vlib_packet_template_init
413 &cm->packet_templates[CDP_PACKET_TEMPLATE_ETHERNET],
416 /* alloc chunk size */ 8,
420 #if 0 /* retain for reference */
422 /* Create the hdlc cdp hello packet template */
424 hdlc_and_cdp_header_t h;
426 memset (&h, 0, sizeof (h));
428 h.hdlc.address = 0x0f;
429 /* h.hdlc.control = 0; (memset) */
430 h.hdlc.protocol = htons (0x2000); /* CDP = 0x2000 */
436 vlib_packet_template_init
438 &cm->packet_templates[CDP_PACKET_TEMPLATE_HDLC],
441 /* alloc chunk size */ 8,
445 /* Create the srp cdp hello packet template */
447 srp_and_cdp_header_t h;
449 memset (&h, 0, sizeof (h));
451 /* Send to 01:00:0c:cc:cc */
452 h.ethernet.dst_address[0] = 0x01;
453 /* h.ethernet.dst_address[1] = 0x00; (memset) */
454 h.ethernet.dst_address[2] = 0x0C;
455 h.ethernet.dst_address[3] = 0xCC;
456 h.ethernet.dst_address[4] = 0xCC;
457 h.ethernet.dst_address[5] = 0xCC;
459 /* leave src address blank (fill in at send time) */
461 /* The srp header is filled in at xmt */
464 h.srp.mode = SRP_MODE_data;
465 srp_header_compute_parity (&h.srp);
467 /* Inner ring and parity will be set at send time */
469 h.ethernet.type = htons (0x2000); /* CDP = 0x2000 */
475 vlib_packet_template_init
477 &cm->packet_templates[CDP_PACKET_TEMPLATE_SRP],
480 /* alloc chunk size */ 8,
488 VLIB_INIT_FUNCTION (cdp_periodic_init);