Cisco Discovery Protocol, initial working attempt
[vpp.git] / vnet / vnet / cdp / cdp_periodic.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 <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>
22
23 /*
24  * Generate a set of specific CDP TLVs.
25  *
26  * $$$ eventually these need to fish better data from
27  * other data structures; e.g. the hostname, software version info
28  * etc.
29  */
30
31 static void add_device_name_tlv (vnet_hw_interface_t *hw, u8 **t0p)
32 {
33     cdp_tlv_t *t = (cdp_tlv_t *) *t0p;
34
35     t->t = htons(CDP_TLV_device_name);
36     t->l = htons(3 + sizeof (*t));
37     memcpy (&t->v, "VPP", 3);
38     
39     *t0p += ntohs(t->l);
40 }
41
42 static void add_port_id_tlv (vnet_hw_interface_t *hw, u8 **t0p)
43 {
44     cdp_tlv_t *t = (cdp_tlv_t *) *t0p;
45
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));
49     *t0p += ntohs(t->l);
50 }
51
52 static void add_version_tlv (vnet_hw_interface_t *hw, u8 **t0p)
53 {
54     cdp_tlv_t *t = (cdp_tlv_t *) *t0p;
55
56     t->t = htons(CDP_TLV_version);
57     t->l = htons(12 + sizeof (*t));
58     memcpy (&t->v, "VPP Software", 12);
59     *t0p += ntohs(t->l);
60 }
61
62 static void add_platform_tlv (vnet_hw_interface_t *hw, u8 **t0p)
63 {
64     cdp_tlv_t *t = (cdp_tlv_t *) *t0p;
65
66     t->t = htons(CDP_TLV_platform);
67     t->l = htons(2 + sizeof (*t));
68     memcpy (&t->v, "SW", 2);
69     *t0p += ntohs(t->l);
70 }
71
72 static void add_capability_tlv (vnet_hw_interface_t *hw, u8 **t0p)
73 {
74     cdp_tlv_t *t = (cdp_tlv_t *) *t0p;
75     u32 capabilities;
76
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));
82     *t0p += ntohs(t->l);
83 }
84
85 static void add_tlvs (cdp_main_t *cm, vnet_hw_interface_t *hw, u8 **t0p)
86 {
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);
92 }
93
94 /*
95  * send a cdp pkt on an ethernet interface
96  */
97 static void 
98 send_ethernet_hello (cdp_main_t *cm, cdp_neighbor_t *n, int count)
99 {
100     u32 * to_next;
101     ethernet_llc_snap_and_cdp_header_t *h0;
102     vnet_hw_interface_t *hw;
103     u32 bi0;
104     vlib_buffer_t *b0;
105     u8 *t0;
106     u16 checksum;
107     int nbytes_to_checksum;
108     int i;
109     vlib_frame_t * f;
110     vlib_main_t * vm = cm->vlib_main;
111     vnet_main_t * vnm = cm->vnet_main;
112
113     for (i = 0; i < count; i++) {
114         /* 
115          * see cdp_periodic_init() to understand what's already painted
116          * into the buffer by the packet template mechanism  
117          */
118         h0 = vlib_packet_template_get_packet 
119             (vm, 
120              &cm->packet_templates[n->packet_template_index], &bi0);
121         
122         /* Add the interface's ethernet source address */
123         hw = vnet_get_sup_hw_interface (vnm, n->sw_if_index);
124         
125         memcpy (h0->ethernet.src_address, hw->hw_address, 
126                 vec_len (hw->hw_address));
127         
128         t0 = (u8 *) &h0->cdp.data;
129
130         /* add TLVs */
131         add_tlvs (cm, hw, &t0);
132         
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);
137         
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) ;
142
143         /* Set the 802.3 ethernet length */
144         h0->ethernet.len = htons (b0->current_length
145                                   - sizeof (ethernet_802_3_header_t));
146
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);
150         to_next[0] = bi0;
151         f->n_vectors = 1;
152
153         vlib_put_frame_to_node (vm, hw->output_node_index, f);
154         n->last_sent = vlib_time_now (vm);
155     }
156 }
157
158 /*
159  * send a cdp pkt on an hdlc interface
160  */
161 static void 
162 send_hdlc_hello (cdp_main_t *cm, cdp_neighbor_t *n, int count)
163 {
164     u32 * to_next;
165     hdlc_and_cdp_header_t *h0;
166     vnet_hw_interface_t *hw;
167     u32 bi0;
168     vlib_buffer_t *b0;
169     u8 *t0;
170     u16 checksum;
171     int nbytes_to_checksum;
172     int i;
173     vlib_frame_t * f;
174     vlib_main_t * vm = cm->vlib_main;
175     vnet_main_t * vnm = cm->vnet_main;
176
177     for (i = 0; i < count; i++) {
178         /* 
179          * see cdp_periodic_init() to understand what's already painted
180          * into the buffer by the packet template mechanism  
181          */
182         h0 = vlib_packet_template_get_packet 
183             (vm, 
184              &cm->packet_templates[n->packet_template_index], &bi0);
185         
186         hw = vnet_get_sup_hw_interface (vnm, n->sw_if_index);
187
188         t0 = (u8 *) &h0->cdp.data;
189
190         /* add TLVs */
191         add_tlvs (cm, hw, &t0);
192         
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);
197         
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) ;
202
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);
206         to_next[0] = bi0;
207         f->n_vectors = 1;
208
209         vlib_put_frame_to_node (vm, hw->output_node_index, f);
210         n->last_sent = vlib_time_now (vm);
211     }
212 }
213
214 /*
215  * send a cdp pkt on an srp interface
216  */
217 static void 
218 send_srp_hello (cdp_main_t *cm, cdp_neighbor_t *n, int count)
219 {
220     u32 * to_next;
221     srp_and_cdp_header_t *h0;
222     vnet_hw_interface_t *hw;
223     u32 bi0;
224     vlib_buffer_t *b0;
225     u8 *t0;
226     u16 checksum;
227     int nbytes_to_checksum;
228     int i;
229     vlib_frame_t * f;
230     vlib_main_t * vm = cm->vlib_main;
231     vnet_main_t * vnm = cm->vnet_main;
232
233     for (i = 0; i < count; i++) {
234         /* 
235          * see cdp_periodic_init() to understand what's already painted
236          * into the buffer by the packet template mechanism  
237          */
238         h0 = vlib_packet_template_get_packet 
239             (vm, 
240              &cm->packet_templates[n->packet_template_index], &bi0);
241         
242         hw = vnet_get_sup_hw_interface (vnm, n->sw_if_index);
243
244         t0 = (u8 *) &h0->cdp.data;
245
246         /* add TLVs */
247         add_tlvs (cm, hw, &t0);
248         
249         /* Add the interface's ethernet source address */
250         memcpy (h0->ethernet.src_address, hw->hw_address, 
251                 vec_len (hw->hw_address));
252
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);
257         
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) ;
262
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);
266         to_next[0] = bi0;
267         f->n_vectors = 1;
268
269         vlib_put_frame_to_node (vm, hw->output_node_index, f);
270         n->last_sent = vlib_time_now (vm);
271     }
272 }
273
274 /*
275  * Decide which cdp packet template to use
276  */
277 static int pick_packet_template (cdp_main_t *cm, cdp_neighbor_t *n)
278 {
279     n->packet_template_index = CDP_PACKET_TEMPLATE_ETHERNET;
280
281     return 0;
282 }
283
284 /* Send a cdp neighbor announcement */
285 static void send_hello (cdp_main_t *cm, cdp_neighbor_t *n, int count)
286 {
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)) {
290             n->last_sent = 1e70;
291             return;
292         }
293     }
294
295     switch (n->packet_template_index)
296     {
297     case CDP_PACKET_TEMPLATE_ETHERNET:
298         send_ethernet_hello (cm, n, count);
299         break;
300
301     case CDP_PACKET_TEMPLATE_HDLC:
302         send_hdlc_hello (cm, n, count);
303         break;
304
305     case CDP_PACKET_TEMPLATE_SRP:
306         send_srp_hello (cm, n, count);
307         break;
308
309     default:
310         ASSERT(0);
311     }
312     n->last_sent = vlib_time_now (cm->vlib_main);
313 }
314
315 static void delete_neighbor (cdp_main_t *cm, cdp_neighbor_t *n,
316                              int want_broadcast)
317 {
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);
325 }
326
327 void cdp_periodic (vlib_main_t * vm)
328 {
329     cdp_main_t *cm = &cdp_main;
330     cdp_neighbor_t *n;
331     f64 now = vlib_time_now (vm);
332     vnet_sw_interface_t *sw;
333     static u32 * delete_list = 0;
334     int i;
335     static cdp_neighbor_t **n_list = 0;
336
337     pool_foreach (n, cm->neighbors, 
338     ({
339       vec_add1 (n_list, n);
340     }));
341
342     /* Across all cdp neighbors known to the system */
343     for (i = 0; i < vec_len (n_list); i++) {
344         n = n_list[i];
345
346         /* "no cdp run" provisioned on the interface? */
347         if (n->disabled == 1)
348             continue;
349
350         sw = vnet_get_sw_interface (cm->vnet_main, n->sw_if_index);
351
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 */);
363     }
364         
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);
368     }
369     if (delete_list)
370         _vec_len (delete_list) = 0;
371     if (n_list)
372         _vec_len (n_list) = 0;
373 }
374
375 static clib_error_t *cdp_periodic_init (vlib_main_t * vm)
376 {
377     cdp_main_t * cm = &cdp_main;
378
379     /* Create the ethernet cdp hello packet template */
380     {
381         ethernet_llc_snap_and_cdp_header_t h;
382
383         memset (&h, 0, sizeof (h));
384         
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;
392         
393         /* leave src address blank (fill in at send time) */
394         
395         /* leave length blank (fill in at send time) */
396         
397         /* LLC */
398         h.llc.dst_sap = h.llc.src_sap = 0xAA; /* SNAP */
399         h.llc.control = 0x03;       /* UI (no extended control bytes) */
400         
401         /* SNAP */
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 */
406         
407         /* CDP */
408         h.cdp.version = 2;
409         h.cdp.ttl = 180;
410         
411         vlib_packet_template_init 
412             (vm,
413              &cm->packet_templates[CDP_PACKET_TEMPLATE_ETHERNET],
414              /* data */ &h,
415              sizeof (h),
416              /* alloc chunk size */ 8,
417              "cdp-ethernet");
418     }
419
420 #if 0 /* retain for reference */
421
422     /* Create the hdlc cdp hello packet template */
423     {
424         hdlc_and_cdp_header_t h;
425
426         memset (&h, 0, sizeof (h));
427
428         h.hdlc.address = 0x0f;
429         /* h.hdlc.control = 0; (memset) */
430         h.hdlc.protocol = htons (0x2000); /* CDP = 0x2000 */
431
432         /* CDP */
433         h.cdp.version = 2;
434         h.cdp.ttl = 180;
435
436         vlib_packet_template_init 
437             (vm,
438              &cm->packet_templates[CDP_PACKET_TEMPLATE_HDLC],
439              /* data */ &h,
440              sizeof (h),
441              /* alloc chunk size */ 8,
442              "cdp-hdlc");
443     }
444
445     /* Create the srp cdp hello packet template */
446     {
447         srp_and_cdp_header_t h;
448
449         memset (&h, 0, sizeof (h));
450         
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;
458         
459         /* leave src address blank (fill in at send time) */
460         
461         /* The srp header is filled in at xmt */
462         h.srp.ttl = 1;
463         h.srp.priority = 7;
464         h.srp.mode = SRP_MODE_data;
465         srp_header_compute_parity (&h.srp);
466
467         /* Inner ring and parity will be set at send time */
468
469         h.ethernet.type = htons (0x2000); /* CDP = 0x2000 */
470
471         /* CDP */
472         h.cdp.version = 2;
473         h.cdp.ttl = 180;
474         
475         vlib_packet_template_init 
476             (vm,
477              &cm->packet_templates[CDP_PACKET_TEMPLATE_SRP],
478              /* data */ &h,
479              sizeof (h),
480              /* alloc chunk size */ 8,
481              "cdp-srp");
482     }
483 #endif
484
485     return 0;
486 }
487
488 VLIB_INIT_FUNCTION (cdp_periodic_init);