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 /* And the outbound interface */
144 vnet_buffer(b0)->sw_if_index[VLIB_TX] = hw->sw_if_index;
146 /* Set the 802.3 ethernet length */
147 h0->ethernet.len = htons (b0->current_length
148 - sizeof (ethernet_802_3_header_t));
150 /* And output the packet on the correct interface */
151 f = vlib_get_frame_to_node (vm, hw->output_node_index);
152 to_next = vlib_frame_vector_args (f);
156 vlib_put_frame_to_node (vm, hw->output_node_index, f);
157 n->last_sent = vlib_time_now (vm);
162 * send a cdp pkt on an hdlc interface
165 send_hdlc_hello (cdp_main_t *cm, cdp_neighbor_t *n, int count)
168 hdlc_and_cdp_header_t *h0;
169 vnet_hw_interface_t *hw;
174 int nbytes_to_checksum;
177 vlib_main_t * vm = cm->vlib_main;
178 vnet_main_t * vnm = cm->vnet_main;
180 for (i = 0; i < count; i++) {
182 * see cdp_periodic_init() to understand what's already painted
183 * into the buffer by the packet template mechanism
185 h0 = vlib_packet_template_get_packet
187 &cm->packet_templates[n->packet_template_index], &bi0);
189 hw = vnet_get_sup_hw_interface (vnm, n->sw_if_index);
191 t0 = (u8 *) &h0->cdp.data;
194 add_tlvs (cm, hw, &t0);
196 /* add the cdp packet checksum */
197 nbytes_to_checksum = t0 - (u8 *)&h0->cdp;
198 checksum = cdp_checksum (&h0->cdp, nbytes_to_checksum);
199 h0->cdp.checksum = htons (checksum);
201 /* Set the outbound packet length */
202 b0 = vlib_get_buffer (vm, bi0);
203 b0->current_length = nbytes_to_checksum + sizeof (*h0)
204 - sizeof (cdp_hdr_t) ;
206 /* And output the packet on the correct interface */
207 f = vlib_get_frame_to_node (vm, hw->output_node_index);
208 to_next = vlib_frame_vector_args (f);
212 vlib_put_frame_to_node (vm, hw->output_node_index, f);
213 n->last_sent = vlib_time_now (vm);
218 * send a cdp pkt on an srp interface
221 send_srp_hello (cdp_main_t *cm, cdp_neighbor_t *n, int count)
224 srp_and_cdp_header_t *h0;
225 vnet_hw_interface_t *hw;
230 int nbytes_to_checksum;
233 vlib_main_t * vm = cm->vlib_main;
234 vnet_main_t * vnm = cm->vnet_main;
236 for (i = 0; i < count; i++) {
238 * see cdp_periodic_init() to understand what's already painted
239 * into the buffer by the packet template mechanism
241 h0 = vlib_packet_template_get_packet
243 &cm->packet_templates[n->packet_template_index], &bi0);
245 hw = vnet_get_sup_hw_interface (vnm, n->sw_if_index);
247 t0 = (u8 *) &h0->cdp.data;
250 add_tlvs (cm, hw, &t0);
252 /* Add the interface's ethernet source address */
253 memcpy (h0->ethernet.src_address, hw->hw_address,
254 vec_len (hw->hw_address));
256 /* add the cdp packet checksum */
257 nbytes_to_checksum = t0 - (u8 *)&h0->cdp;
258 checksum = cdp_checksum (&h0->cdp, nbytes_to_checksum);
259 h0->cdp.checksum = htons (checksum);
261 /* Set the outbound packet length */
262 b0 = vlib_get_buffer (vm, bi0);
263 b0->current_length = nbytes_to_checksum + sizeof (*h0)
264 - sizeof (cdp_hdr_t) ;
266 /* And output the packet on the correct interface */
267 f = vlib_get_frame_to_node (vm, hw->output_node_index);
268 to_next = vlib_frame_vector_args (f);
272 vlib_put_frame_to_node (vm, hw->output_node_index, f);
273 n->last_sent = vlib_time_now (vm);
278 * Decide which cdp packet template to use
280 static int pick_packet_template (cdp_main_t *cm, cdp_neighbor_t *n)
282 n->packet_template_index = CDP_PACKET_TEMPLATE_ETHERNET;
287 /* Send a cdp neighbor announcement */
288 static void send_hello (cdp_main_t *cm, cdp_neighbor_t *n, int count)
290 if (n->packet_template_index == (u8)~0) {
291 /* If we don't know how to talk to this peer, don't try again */
292 if (pick_packet_template (cm, n)) {
298 switch (n->packet_template_index)
300 case CDP_PACKET_TEMPLATE_ETHERNET:
301 send_ethernet_hello (cm, n, count);
304 case CDP_PACKET_TEMPLATE_HDLC:
305 send_hdlc_hello (cm, n, count);
308 case CDP_PACKET_TEMPLATE_SRP:
309 send_srp_hello (cm, n, count);
315 n->last_sent = vlib_time_now (cm->vlib_main);
318 static void delete_neighbor (cdp_main_t *cm, cdp_neighbor_t *n,
321 hash_unset (cm->neighbor_by_sw_if_index, n->sw_if_index);
322 vec_free (n->device_name);
323 vec_free (n->version);
324 vec_free (n->port_id);
325 vec_free (n->platform);
326 vec_free (n->last_rx_pkt);
327 pool_put (cm->neighbors, n);
330 void cdp_periodic (vlib_main_t * vm)
332 cdp_main_t *cm = &cdp_main;
334 f64 now = vlib_time_now (vm);
335 vnet_sw_interface_t *sw;
336 static u32 * delete_list = 0;
338 static cdp_neighbor_t **n_list = 0;
340 pool_foreach (n, cm->neighbors,
342 vec_add1 (n_list, n);
345 /* Across all cdp neighbors known to the system */
346 for (i = 0; i < vec_len (n_list); i++) {
349 /* "no cdp run" provisioned on the interface? */
350 if (n->disabled == 1)
353 sw = vnet_get_sw_interface (cm->vnet_main, n->sw_if_index);
355 /* Interface shutdown or rx timeout? */
356 if (!(sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
357 || (now > (n->last_heard + (f64)n->ttl_in_seconds)))
358 /* add to list of neighbors to delete */
359 vec_add1 (delete_list, n - cm->neighbors);
360 else if (n->last_sent == 0.0)
361 /* First time, send 3 hellos */
362 send_hello (cm, n, 3 /* three to begin with */);
363 else if (now > (n->last_sent + (((f64)n->ttl_in_seconds)/6.0)))
364 /* Normal keepalive, send one */
365 send_hello (cm, n, 1 /* one as a keepalive */);
368 for (i = 0; i < vec_len (delete_list); i++) {
369 n = vec_elt_at_index (cm->neighbors, delete_list[i]);
370 delete_neighbor (cm, n, 1);
373 _vec_len (delete_list) = 0;
375 _vec_len (n_list) = 0;
378 static clib_error_t *cdp_periodic_init (vlib_main_t * vm)
380 cdp_main_t * cm = &cdp_main;
382 /* Create the ethernet cdp hello packet template */
384 ethernet_llc_snap_and_cdp_header_t h;
386 memset (&h, 0, sizeof (h));
388 /* Send to 01:00:0c:cc:cc */
389 h.ethernet.dst_address[0] = 0x01;
390 /* h.ethernet.dst_address[1] = 0x00; (memset) */
391 h.ethernet.dst_address[2] = 0x0C;
392 h.ethernet.dst_address[3] = 0xCC;
393 h.ethernet.dst_address[4] = 0xCC;
394 h.ethernet.dst_address[5] = 0xCC;
396 /* leave src address blank (fill in at send time) */
398 /* leave length blank (fill in at send time) */
401 h.llc.dst_sap = h.llc.src_sap = 0xAA; /* SNAP */
402 h.llc.control = 0x03; /* UI (no extended control bytes) */
405 /* h.snap.oui[0] = 0x00; (memset) */
406 /* h.snap.oui[1] = 0x00; (memset) */
407 h.snap.oui[2] = 0x0C; /* Cisco = 0x00000C */
408 h.snap.protocol = htons (0x2000); /* CDP = 0x2000 */
414 vlib_packet_template_init
416 &cm->packet_templates[CDP_PACKET_TEMPLATE_ETHERNET],
419 /* alloc chunk size */ 8,
423 #if 0 /* retain for reference */
425 /* Create the hdlc cdp hello packet template */
427 hdlc_and_cdp_header_t h;
429 memset (&h, 0, sizeof (h));
431 h.hdlc.address = 0x0f;
432 /* h.hdlc.control = 0; (memset) */
433 h.hdlc.protocol = htons (0x2000); /* CDP = 0x2000 */
439 vlib_packet_template_init
441 &cm->packet_templates[CDP_PACKET_TEMPLATE_HDLC],
444 /* alloc chunk size */ 8,
448 /* Create the srp cdp hello packet template */
450 srp_and_cdp_header_t h;
452 memset (&h, 0, sizeof (h));
454 /* Send to 01:00:0c:cc:cc */
455 h.ethernet.dst_address[0] = 0x01;
456 /* h.ethernet.dst_address[1] = 0x00; (memset) */
457 h.ethernet.dst_address[2] = 0x0C;
458 h.ethernet.dst_address[3] = 0xCC;
459 h.ethernet.dst_address[4] = 0xCC;
460 h.ethernet.dst_address[5] = 0xCC;
462 /* leave src address blank (fill in at send time) */
464 /* The srp header is filled in at xmt */
467 h.srp.mode = SRP_MODE_data;
468 srp_header_compute_parity (&h.srp);
470 /* Inner ring and parity will be set at send time */
472 h.ethernet.type = htons (0x2000); /* CDP = 0x2000 */
478 vlib_packet_template_init
480 &cm->packet_templates[CDP_PACKET_TEMPLATE_SRP],
483 /* alloc chunk size */ 8,
491 VLIB_INIT_FUNCTION (cdp_periodic_init);