-
Notifications
You must be signed in to change notification settings - Fork 5
/
bfd_packet.c
1406 lines (1196 loc) · 37.1 KB
/
bfd_packet.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************************
* Copyright 2017 Cumulus Networks, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; see the file COPYING; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* bfd_packet.c: implements the BFD protocol packet handling.
*
* Authors
* -------
* Shrijeet Mukherjee [[email protected]]
* Kanna Rajagopal [[email protected]]
* Radhika Mahankali [[email protected]]
*/
/* XXX: fix compilation error on Ubuntu 16.04 or older. */
#ifndef _UAPI_IPV6_H
#define _UAPI_IPV6_H
#endif /* _UAPI_IPV6_H */
#include <linux/filter.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <linux/udp.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <err.h>
#include <stdint.h>
#include <unistd.h>
#include "bfd.h"
/*
* Definitions
*/
/* iov for BFD control frames */
#define CMSG_HDR_LEN sizeof(struct cmsghdr)
#define CMSG_TTL_LEN (CMSG_HDR_LEN + sizeof(uint32_t))
#define CMSG_IN_PKT_INFO_LEN (CMSG_HDR_LEN + sizeof(struct in_pktinfo) + 4)
#define CMSG_IN6_PKT_INFO_LEN \
(CMSG_HDR_LEN + sizeof(struct in6_addr) + sizeof(int) + 4)
typedef struct bfd_raw_echo_pkt_s {
struct iphdr ip;
struct udphdr udp;
bfd_echo_pkt_t data;
} bfd_raw_echo_pkt_t;
typedef struct bfd_raw_ctrl_pkt_s {
struct iphdr ip;
struct udphdr udp;
bfd_pkt_t data;
} bfd_raw_ctrl_pkt_t;
typedef struct vxlan_hdr_s {
uint32_t flags;
uint32_t vnid;
} vxlan_hdr_t;
#define IP_ECHO_PKT_LEN (IP_HDR_LEN + UDP_HDR_LEN + BFD_ECHO_PKT_LEN)
#define UDP_ECHO_PKT_LEN (UDP_HDR_LEN + BFD_ECHO_PKT_LEN)
#define IP_CTRL_PKT_LEN (IP_HDR_LEN + UDP_HDR_LEN + BFD_PKT_LEN)
#define UDP_CTRL_PKT_LEN (UDP_HDR_LEN + BFD_PKT_LEN)
static uint8_t msgbuf[BFD_PKT_LEN];
/* Berkeley Packet filter code to filter out BFD Echo packets.
* tcpdump -dd "(udp dst port 3785)"
*/
static struct sock_filter bfd_echo_filter[] = {
{0x28, 0, 0, 0x0000000c}, {0x15, 0, 4, 0x000086dd},
{0x30, 0, 0, 0x00000014}, {0x15, 0, 11, 0x00000011},
{0x28, 0, 0, 0x00000038}, {0x15, 8, 9, 0x00000ec9},
{0x15, 0, 8, 0x00000800}, {0x30, 0, 0, 0x00000017},
{0x15, 0, 6, 0x00000011}, {0x28, 0, 0, 0x00000014},
{0x45, 4, 0, 0x00001fff}, {0xb1, 0, 0, 0x0000000e},
{0x48, 0, 0, 0x00000010}, {0x15, 0, 1, 0x00000ec9},
{0x6, 0, 0, 0x0000ffff}, {0x6, 0, 0, 0x00000000},
};
/* Berkeley Packet filter code to filter out BFD vxlan packets.
* tcpdump -dd "(udp dst port 4789)"
*/
static struct sock_filter bfd_vxlan_filter[] = {
{0x28, 0, 0, 0x0000000c}, {0x15, 0, 4, 0x000086dd},
{0x30, 0, 0, 0x00000014}, {0x15, 0, 11, 0x00000011},
{0x28, 0, 0, 0x00000038}, {0x15, 8, 9, 0x000012b5},
{0x15, 0, 8, 0x00000800}, {0x30, 0, 0, 0x00000017},
{0x15, 0, 6, 0x00000011}, {0x28, 0, 0, 0x00000014},
{0x45, 4, 0, 0x00001fff}, {0xb1, 0, 0, 0x0000000e},
{0x48, 0, 0, 0x00000010}, {0x15, 0, 1, 0x000012b5},
{0x6, 0, 0, 0x0000ffff}, {0x6, 0, 0, 0x00000000},
};
static int ttlval = BFD_TTL_VAL;
static int tosval = BFD_TOS_VAL;
static int rcvttl = BFD_RCV_TTL_VAL;
static int pktinfo = BFD_PKT_INFO_VAL;
typedef struct udp_psuedo_header_s {
uint32_t saddr;
uint32_t daddr;
uint8_t reserved;
uint8_t protocol;
uint16_t len;
} udp_psuedo_header_t;
#define UDP_PSUEDO_HDR_LEN sizeof(udp_psuedo_header_t)
/*
* Prototypes
*/
uint16_t checksum(uint16_t *buf, int len);
uint16_t udp4_checksum(struct iphdr *iph, uint8_t *buf, int len);
uint16_t ptm_bfd_gen_IP_ID(bfd_session *bfd);
void ptm_bfd_echo_pkt_create(bfd_session *bfd);
int ptm_bfd_echo_loopback(uint8_t *pkt, int pkt_len, struct sockaddr_ll *sll);
void ptm_bfd_vxlan_pkt_snd(bfd_session *bfd, int fbit);
int ptm_bfd_process_echo_pkt(int s);
bool ptm_bfd_validate_vxlan_pkt(bfd_session *bfd,
bfd_session_vxlan_info_t *vxlan_info);
ssize_t bfd_recv_ipv4(int sd, bool is_mhop, char *port, size_t portlen,
char *vrfname, size_t vrfnamelen,
struct sockaddr_any *local, struct sockaddr_any *peer);
ssize_t bfd_recv_ipv6(int sd, bool is_mhop, char *port, size_t portlen,
char *vrfname, size_t vrfnamelen,
struct sockaddr_any *local, struct sockaddr_any *peer);
/* socket related prototypes */
void bp_set_ipopts(int sd);
void bp_bind_ip(int sd, uint16_t port);
void bp_set_ipv6opts(int sd);
void bp_bind_ipv6(int sd, uint16_t port);
/*
* Functions
*/
uint16_t checksum(uint16_t *buf, int len)
{
int nbytes = len;
int sum = 0;
uint16_t csum = 0;
int size = sizeof(uint16_t);
while (nbytes > 1) {
sum += *buf++;
nbytes -= size;
}
if (nbytes == 1) {
*(uint8_t *)(&csum) = *(uint8_t *)buf;
sum += csum;
}
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
csum = ~sum;
return (csum);
}
uint16_t udp4_checksum(struct iphdr *iph, uint8_t *buf, int len)
{
char *ptr;
udp_psuedo_header_t pudp_hdr;
uint16_t csum;
pudp_hdr.saddr = iph->saddr;
pudp_hdr.daddr = iph->daddr;
pudp_hdr.reserved = 0;
pudp_hdr.protocol = iph->protocol;
pudp_hdr.len = htons(len);
ptr = malloc(UDP_PSUEDO_HDR_LEN + len);
memcpy(ptr, &pudp_hdr, UDP_PSUEDO_HDR_LEN);
memcpy(ptr + UDP_PSUEDO_HDR_LEN, buf, len);
csum = checksum((uint16_t *)ptr, UDP_PSUEDO_HDR_LEN + len);
free(ptr);
return csum;
}
uint16_t ptm_bfd_gen_IP_ID(bfd_session *bfd)
{
return (++bfd->ip_id);
}
static int _ptm_bfd_send(bfd_session *bs, bool use_layer2, uint16_t *port,
const void *data, size_t datalen)
{
struct sockaddr *sa;
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
struct sockaddr_ll dll;
socklen_t slen;
ssize_t rv;
int sd = -1;
if (use_layer2) {
memset(&dll, 0, sizeof(dll));
dll.sll_family = AF_PACKET;
dll.sll_protocol = htons(ETH_P_IP);
memcpy(dll.sll_addr, bs->peer_mac, ETHERNET_ADDRESS_LENGTH);
dll.sll_halen = htons(ETHERNET_ADDRESS_LENGTH);
dll.sll_ifindex = bs->ifindex;
sd = bglobal.bg_echo;
sa = (struct sockaddr *)&dll;
slen = sizeof(dll);
} else if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_IPV6)) {
memset(&sin6, 0, sizeof(sin6));
sin6.sin6_family = AF_INET6;
sin6.sin6_addr = bs->shop.peer.sa_sin6.sin6_addr;
sin6.sin6_port =
(port) ? *port
: (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH))
? htons(BFD_DEF_MHOP_DEST_PORT)
: htons(BFD_DEFDESTPORT);
sd = bs->sock;
sa = (struct sockaddr *)&sin6;
slen = sizeof(sin6);
} else {
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr = bs->shop.peer.sa_sin.sin_addr;
sin.sin_port =
(port) ? *port
: (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH))
? htons(BFD_DEF_MHOP_DEST_PORT)
: htons(BFD_DEFDESTPORT);
sd = bs->sock;
sa = (struct sockaddr *)&sin;
slen = sizeof(sin);
}
rv = sendto(sd, data, datalen, 0, sa, slen);
if (rv <= 0) {
log_debug("%s:%d: sendto: (%d) %s\n", __FUNCTION__, __LINE__,
errno, strerror(errno));
return -1;
}
if (rv < (ssize_t)datalen) {
log_debug("%s:%d: sendto: sent partial data\n", __FUNCTION__,
__LINE__);
}
return 0;
}
void ptm_bfd_echo_pkt_create(bfd_session *bfd)
{
bfd_raw_echo_pkt_t ep;
uint8_t *pkt = bfd->echo_pkt;
memset(&ep, 0, sizeof(bfd_raw_echo_pkt_t));
memset(bfd->echo_pkt, 0, BFD_ECHO_PKT_TOT_LEN);
/* Construct ethernet header information */
memcpy(pkt, bfd->peer_mac, ETHERNET_ADDRESS_LENGTH);
pkt = pkt + ETHERNET_ADDRESS_LENGTH;
memcpy(pkt, bfd->local_mac, ETHERNET_ADDRESS_LENGTH);
pkt = pkt + ETHERNET_ADDRESS_LENGTH;
pkt[0] = ETH_P_IP / 256;
pkt[1] = ETH_P_IP % 256;
pkt += 2;
/* Construct IP header information */
ep.ip.version = 4;
ep.ip.ihl = 5;
ep.ip.tos = 0;
ep.ip.tot_len = htons(IP_ECHO_PKT_LEN);
ep.ip.id = htons(ptm_bfd_gen_IP_ID(bfd));
ep.ip.frag_off = 0;
ep.ip.ttl = BFD_TTL_VAL;
ep.ip.protocol = IPPROTO_UDP;
ep.ip.saddr = bfd->local_ip.sa_sin.sin_addr.s_addr;
ep.ip.daddr = bfd->shop.peer.sa_sin.sin_addr.s_addr;
ep.ip.check = checksum((uint16_t *)&ep.ip, IP_HDR_LEN);
/* Construct UDP header information */
ep.udp.source = htons(BFD_DEF_ECHO_PORT);
ep.udp.dest = htons(BFD_DEF_ECHO_PORT);
ep.udp.len = htons(UDP_ECHO_PKT_LEN);
/* Construct Echo packet information */
ep.data.ver = BFD_ECHO_VERSION;
ep.data.len = BFD_ECHO_PKT_LEN;
ep.data.my_discr = htonl(bfd->discrs.my_discr);
ep.udp.check =
udp4_checksum(&ep.ip, (uint8_t *)&ep.udp, UDP_ECHO_PKT_LEN);
memcpy(pkt, &ep, sizeof(bfd_raw_echo_pkt_t));
}
void ptm_bfd_echo_snd(bfd_session *bfd)
{
bfd_raw_echo_pkt_t *ep;
bool use_layer2 = false;
const void *pkt;
size_t pktlen;
uint16_t port = htons(BFD_DEF_ECHO_PORT);
if (!BFD_CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE)) {
ptm_bfd_echo_pkt_create(bfd);
BFD_SET_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE);
} else {
/* just update the checksum and ip Id */
ep = (bfd_raw_echo_pkt_t *)(bfd->echo_pkt + ETH_HDR_LEN);
ep->ip.id = htons(ptm_bfd_gen_IP_ID(bfd));
ep->ip.check = 0;
ep->ip.check = checksum((uint16_t *)&ep->ip, IP_HDR_LEN);
}
if (use_layer2) {
pkt = bfd->echo_pkt;
pktlen = BFD_ECHO_PKT_TOT_LEN;
} else {
pkt = &bfd->echo_pkt[ETH_HDR_LEN + IP_HDR_LEN + UDP_HDR_LEN];
pktlen = BFD_ECHO_PKT_TOT_LEN
- (ETH_HDR_LEN + IP_HDR_LEN + UDP_HDR_LEN);
}
if (_ptm_bfd_send(bfd, use_layer2, &port, pkt, pktlen) != 0) {
ERRLOG("Error sending echo pkt: %s", strerror(errno));
return;
}
bfd->stats.tx_echo_pkt++;
}
int ptm_bfd_echo_loopback(uint8_t *pkt, int pkt_len, struct sockaddr_ll *sll)
{
bfd_raw_echo_pkt_t *ep = (bfd_raw_echo_pkt_t *)(pkt + ETH_HDR_LEN);
uint32_t temp_ip;
uint8_t temp_mac[ETHERNET_ADDRESS_LENGTH];
struct ethhdr *eth = (struct ethhdr *)pkt;
/* swap the mac addresses */
memcpy(temp_mac, eth->h_source, ETHERNET_ADDRESS_LENGTH);
memcpy(eth->h_source, eth->h_dest, ETHERNET_ADDRESS_LENGTH);
memcpy(eth->h_dest, temp_mac, ETHERNET_ADDRESS_LENGTH);
/* swap ip addresses */
temp_ip = ep->ip.saddr;
ep->ip.saddr = ep->ip.daddr;
ep->ip.daddr = temp_ip;
ep->ip.ttl = ep->ip.ttl - 1;
ep->ip.check = 0;
ep->ip.check = checksum((uint16_t *)ep, IP_HDR_LEN);
if (sendto(bglobal.bg_echo, pkt, pkt_len, 0, (struct sockaddr *)sll,
sizeof(struct sockaddr_ll))
< 0) {
ERRLOG("Error sending echo pkt: %s", strerror(errno));
return -1;
}
return 0;
}
void ptm_bfd_vxlan_pkt_snd(bfd_session *bfd, int fbit)
{
bfd_raw_ctrl_pkt_t cp;
uint8_t vxlan_pkt[BFD_VXLAN_PKT_TOT_LEN];
uint8_t *pkt = vxlan_pkt;
struct sockaddr_in sin;
vxlan_hdr_t *vhdr;
memset(pkt, 0, BFD_VXLAN_PKT_TOT_LEN);
memset(&cp, 0, sizeof(bfd_raw_ctrl_pkt_t));
/* Construct VxLAN header information */
vhdr = (vxlan_hdr_t *)pkt;
vhdr->flags = htonl(0x08000000);
vhdr->vnid = htonl(bfd->vxlan_info.vnid << 8);
pkt += VXLAN_HDR_LEN;
/* Construct ethernet header information */
memcpy(pkt, bfd->vxlan_info.peer_dst_mac, ETHERNET_ADDRESS_LENGTH);
pkt = pkt + ETHERNET_ADDRESS_LENGTH;
memcpy(pkt, bfd->vxlan_info.local_dst_mac, ETHERNET_ADDRESS_LENGTH);
pkt = pkt + ETHERNET_ADDRESS_LENGTH;
pkt[0] = ETH_P_IP / 256;
pkt[1] = ETH_P_IP % 256;
pkt += 2;
/* Construct IP header information */
cp.ip.version = 4;
cp.ip.ihl = 5;
cp.ip.tos = 0;
cp.ip.tot_len = htons(IP_CTRL_PKT_LEN);
cp.ip.id = ptm_bfd_gen_IP_ID(bfd);
cp.ip.frag_off = 0;
cp.ip.ttl = BFD_TTL_VAL;
cp.ip.protocol = IPPROTO_UDP;
cp.ip.daddr = bfd->vxlan_info.peer_dst_ip.s_addr;
cp.ip.saddr = bfd->vxlan_info.local_dst_ip.s_addr;
cp.ip.check = checksum((uint16_t *)&cp.ip, IP_HDR_LEN);
/* Construct UDP header information */
cp.udp.source = htons(BFD_DEFDESTPORT);
cp.udp.dest = htons(BFD_DEFDESTPORT);
cp.udp.len = htons(UDP_CTRL_PKT_LEN);
/* Construct BFD control packet information */
cp.data.diag = bfd->local_diag;
BFD_SETVER(cp.data.diag, BFD_VERSION);
BFD_SETSTATE(cp.data.flags, bfd->ses_state);
BFD_SETDEMANDBIT(cp.data.flags, BFD_DEF_DEMAND);
BFD_SETPBIT(cp.data.flags, bfd->polling);
BFD_SETFBIT(cp.data.flags, fbit);
cp.data.detect_mult = bfd->detect_mult;
cp.data.len = BFD_PKT_LEN;
cp.data.discrs.my_discr = htonl(bfd->discrs.my_discr);
cp.data.discrs.remote_discr = htonl(bfd->discrs.remote_discr);
cp.data.timers.desired_min_tx = htonl(bfd->timers.desired_min_tx);
cp.data.timers.required_min_rx = htonl(bfd->timers.required_min_rx);
cp.data.timers.required_min_echo = htonl(bfd->timers.required_min_echo);
cp.udp.check =
udp4_checksum(&cp.ip, (uint8_t *)&cp.udp, UDP_CTRL_PKT_LEN);
memcpy(pkt, &cp, sizeof(bfd_raw_ctrl_pkt_t));
sin.sin_family = AF_INET;
sin.sin_addr = bfd->shop.peer.sa_sin.sin_addr;
sin.sin_port = htons(4789);
if (sendto(bfd->sock, vxlan_pkt, BFD_VXLAN_PKT_TOT_LEN, 0,
(struct sockaddr *)&sin, sizeof(struct sockaddr_in))
< 0) {
ERRLOG("Error sending vxlan bfd pkt: %s", strerror(errno));
} else {
bfd->stats.tx_ctrl_pkt++;
}
}
int ptm_bfd_process_echo_pkt(int s)
{
ssize_t pkt_len;
struct sockaddr_ll sll;
uint32_t from_len = sizeof(struct sockaddr_ll);
bfd_raw_echo_pkt_t *ep;
char rx_pkt[BFD_RX_BUF_LEN];
bfd_session *bfd;
uint32_t my_discr = 0;
pkt_len = recvfrom(s, rx_pkt, BFD_RX_BUF_LEN, MSG_DONTWAIT,
(struct sockaddr *)&sll, &from_len);
if (pkt_len <= 0) {
if (errno != EAGAIN)
ERRLOG("Error receiving from BFD Echo socket: %s",
strerror(errno));
return -1;
}
/* Check if we have at least the basic headers to send back. */
if (pkt_len < HEADERS_MIN_LEN) {
INFOLOG("Received short echo packet");
return -1;
}
ep = (bfd_raw_echo_pkt_t *)(rx_pkt + ETH_HDR_LEN);
/* if TTL = 255, assume that the received echo packet has
* to be looped back */
if (ep->ip.ttl == BFD_TTL_VAL) {
return ptm_bfd_echo_loopback((void *)rx_pkt, pkt_len, &sll);
}
/* Packet is too small for us to process */
if (pkt_len < BFD_ECHO_PKT_TOT_LEN) {
INFOLOG("Received short echo packet");
return -1;
}
if (ep->data.my_discr == 0) {
INFOLOG("My discriminator is zero in echo pkt from 0x%x",
ntohl(ep->ip.saddr));
return -1;
}
/* Your discriminator not zero - use it to find session */
my_discr = ntohl(ep->data.my_discr);
bfd = bs_session_find(my_discr);
if (bfd == NULL) {
INFOLOG("Failed to extract session from echo packet");
return -1;
}
if (!BFD_CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE)) {
INFOLOG("BFD echo not active - ignore echo packet");
return -1;
}
bfd->stats.rx_echo_pkt++;
/* Compute detect time */
bfd->echo_detect_TO = bfd->remote_detect_mult * bfd->echo_xmt_TO;
/* Update echo receive timeout. */
bfd_echo_recvtimer_update(bfd);
return 0;
}
void ptm_bfd_snd(bfd_session *bfd, int fbit)
{
bfd_pkt_t cp;
/* if the BFD session is for VxLAN tunnel, then construct and
* send bfd raw packet */
if (BFD_CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_VXLAN)) {
ptm_bfd_vxlan_pkt_snd(bfd, fbit);
return;
}
/* Set fields according to section 6.5.7 */
cp.diag = bfd->local_diag;
BFD_SETVER(cp.diag, BFD_VERSION);
cp.flags = 0;
BFD_SETSTATE(cp.flags, bfd->ses_state);
BFD_SETDEMANDBIT(cp.flags, BFD_DEF_DEMAND);
BFD_SETPBIT(cp.flags, bfd->polling);
BFD_SETFBIT(cp.flags, fbit);
cp.detect_mult = bfd->detect_mult;
cp.len = BFD_PKT_LEN;
cp.discrs.my_discr = htonl(bfd->discrs.my_discr);
cp.discrs.remote_discr = htonl(bfd->discrs.remote_discr);
if (bfd->polling) {
cp.timers.desired_min_tx =
htonl(bfd->new_timers.desired_min_tx);
cp.timers.required_min_rx =
htonl(bfd->new_timers.required_min_rx);
} else {
cp.timers.desired_min_tx = htonl(bfd->timers.desired_min_tx);
cp.timers.required_min_rx = htonl(bfd->timers.required_min_rx);
}
cp.timers.required_min_echo = htonl(bfd->timers.required_min_echo);
if (_ptm_bfd_send(bfd, false, NULL, &cp, BFD_PKT_LEN) != 0) {
ERRLOG("Error sending control pkt: %s", strerror(errno));
return;
}
bfd->stats.tx_ctrl_pkt++;
}
#if 0 /* TODO VxLAN Support */
static bfd_pkt_t *
ptm_bfd_process_vxlan_pkt(int s,
ptm_sockevent_e se,
void *udata,
int *ifindex,
struct sockaddr_in *sin,
bfd_session_vxlan_info_t *vxlan_info,
uint8_t *rx_pkt,
int *mlen)
{
struct sockaddr_ll sll;
uint32_t from_len = sizeof(struct sockaddr_ll);
bfd_raw_ctrl_pkt_t *cp;
uint8_t *pkt = rx_pkt;
struct iphdr *iph;
struct ethhdr *inner_ethh;
*mlen = recvfrom(s, rx_pkt, BFD_RX_BUF_LEN, MSG_DONTWAIT,
(struct sockaddr *)&sll, &from_len);
if (*mlen < 0) {
if (errno != EAGAIN) {
ERRLOG("Error receiving from BFD Vxlan socket %d: %m\n", s);
}
return NULL;
}
iph = (struct iphdr *)(pkt + ETH_HDR_LEN);
pkt = pkt + ETH_HDR_LEN + IP_HDR_LEN + UDP_HDR_LEN;
vxlan_info->vnid = ntohl(*((int *)(pkt + 4)));
vxlan_info->vnid = vxlan_info->vnid >> 8;
pkt = pkt + VXLAN_HDR_LEN;
inner_ethh = (struct ethhdr *)pkt;
cp = (bfd_raw_ctrl_pkt_t *)(pkt + ETH_HDR_LEN);
/* Discard the non BFD packets */
if (ntohs(cp->udp.dest) != BFD_DEFDESTPORT)
return NULL;
*ifindex = sll.sll_ifindex;
sin->sin_addr.s_addr = iph->saddr;
sin->sin_port = ntohs(cp->udp.dest);
vxlan_info->local_dst_ip.s_addr = cp->ip.daddr;
memcpy(vxlan_info->local_dst_mac, inner_ethh->h_dest,
ETHERNET_ADDRESS_LENGTH);
return (&cp->data);
}
#endif /* VxLAN */
bool ptm_bfd_validate_vxlan_pkt(bfd_session *bfd,
bfd_session_vxlan_info_t *vxlan_info)
{
if (bfd->vxlan_info.check_tnl_key && (vxlan_info->vnid != 0)) {
ERRLOG("Error Rx BFD Vxlan pkt with non-zero vnid %d",
vxlan_info->vnid);
return false;
}
if (bfd->vxlan_info.local_dst_ip.s_addr
!= vxlan_info->local_dst_ip.s_addr) {
ERRLOG("Error Rx BFD Vxlan pkt with wrong inner dst IP %s",
inet_ntoa(vxlan_info->local_dst_ip));
return false;
}
if (memcmp(bfd->vxlan_info.local_dst_mac, vxlan_info->local_dst_mac,
ETHERNET_ADDRESS_LENGTH)) {
ERRLOG("Error Rx BFD Vxlan pkt with wrong inner dst"
" MAC %02x:%02x:%02x:%02x:%02x:%02x",
vxlan_info->local_dst_mac[0],
vxlan_info->local_dst_mac[1],
vxlan_info->local_dst_mac[2],
vxlan_info->local_dst_mac[3],
vxlan_info->local_dst_mac[4],
vxlan_info->local_dst_mac[5]);
return false;
}
return true;
}
ssize_t bfd_recv_ipv4(int sd, bool is_mhop, char *port, size_t portlen,
char *vrfname, size_t vrfnamelen,
struct sockaddr_any *local, struct sockaddr_any *peer)
{
struct cmsghdr *cm;
struct in_pktinfo *pi = NULL;
ssize_t mlen;
uint32_t ttl;
struct sockaddr_in msgaddr;
struct msghdr msghdr;
struct iovec iov[1];
uint8_t cmsgbuf[255];
/* Prepare the recvmsg params. */
iov[0].iov_base = msgbuf;
iov[0].iov_len = sizeof(msgbuf);
memset(&msghdr, 0, sizeof(msghdr));
msghdr.msg_name = &msgaddr;
msghdr.msg_namelen = sizeof(msgaddr);
msghdr.msg_iov = iov;
msghdr.msg_iovlen = 1;
msghdr.msg_control = cmsgbuf;
msghdr.msg_controllen = sizeof(cmsgbuf);
/* Sanitize input/output. */
memset(port, 0, portlen);
memset(vrfname, 0, vrfnamelen);
memset(local, 0, sizeof(*local));
memset(peer, 0, sizeof(*peer));
if ((mlen = recvmsg(sd, &msghdr, MSG_DONTWAIT)) == -1) {
if (errno != EAGAIN) {
ERRLOG("Error receiving from BFD socket: %s",
strerror(errno));
}
return -1;
}
/* Get source address */
peer->sa_sin = *((struct sockaddr_in *)(msghdr.msg_name));
/* Get and check TTL */
for (cm = CMSG_FIRSTHDR(&msghdr); cm != NULL;
cm = CMSG_NXTHDR(&msghdr, cm)) {
if (cm->cmsg_level != SOL_IP)
continue;
if (cm->cmsg_type == IP_TTL) {
memcpy(&ttl, CMSG_DATA(cm), sizeof(ttl));
if ((is_mhop == false) && (ttl != BFD_TTL_VAL)) {
INFOLOG("Received pkt with invalid TTL %u from %s flags: %d",
ttl, satostr(peer), msghdr.msg_flags);
return -1;
}
} else if (cm->cmsg_type == IP_PKTINFO) {
pi = (struct in_pktinfo *)CMSG_DATA(cm);
if (pi) {
local->sa_sin.sin_family = AF_INET;
local->sa_sin.sin_addr = pi->ipi_addr;
fetch_portname_from_ifindex(pi->ipi_ifindex,
port, portlen);
}
}
}
return mlen;
}
ssize_t bfd_recv_ipv6(int sd, bool is_mhop, char *port, size_t portlen,
char *vrfname, size_t vrfnamelen,
struct sockaddr_any *local, struct sockaddr_any *peer)
{
struct cmsghdr *cm;
struct in6_pktinfo *pi6 = NULL;
ssize_t mlen;
struct sockaddr_in6 msgaddr6;
struct msghdr msghdr6;
struct iovec iov[1];
uint8_t cmsgbuf6[255];
/* Prepare the recvmsg params. */
iov[0].iov_base = msgbuf;
iov[0].iov_len = sizeof(msgbuf);
memset(&msghdr6, 0, sizeof(msghdr6));
msghdr6.msg_name = &msgaddr6;
msghdr6.msg_namelen = sizeof(msgaddr6);
msghdr6.msg_iov = iov;
msghdr6.msg_iovlen = 1;
msghdr6.msg_control = cmsgbuf6;
msghdr6.msg_controllen = sizeof(cmsgbuf6);
/* Sanitize input/output. */
memset(port, 0, portlen);
memset(vrfname, 0, vrfnamelen);
memset(local, 0, sizeof(*local));
memset(peer, 0, sizeof(*peer));
if ((mlen = recvmsg(sd, &msghdr6, MSG_DONTWAIT)) == -1) {
if (errno != EAGAIN) {
ERRLOG("Error receiving from BFD socket: %s",
strerror(errno));
}
return -1;
}
/* Get source address */
peer->sa_sin6 = *((struct sockaddr_in6 *)(msghdr6.msg_name));
/* Get and check TTL */
for (cm = CMSG_FIRSTHDR(&msghdr6); cm != NULL;
cm = CMSG_NXTHDR(&msghdr6, cm)) {
if (cm->cmsg_level != IPPROTO_IPV6)
continue;
if (cm->cmsg_type == IPV6_2292HOPLIMIT) {
memcpy(&ttlval, CMSG_DATA(cm), 4);
if ((is_mhop == false) && (ttlval != BFD_TTL_VAL)) {
INFOLOG("Received pkt with invalid TTL %u from %s flags: %d\n",
ttlval, satostr(peer),
msghdr6.msg_flags);
return -1;
}
} else if (cm->cmsg_type == IPV6_2292PKTINFO) {
pi6 = (struct in6_pktinfo *)CMSG_DATA(cm);
if (pi6) {
local->sa_sin.sin_family = AF_INET6;
local->sa_sin6.sin6_addr = pi6->ipi6_addr;
fetch_portname_from_ifindex(pi6->ipi6_ifindex,
port, portlen);
}
}
}
return mlen;
}
void bfd_recv_cb(evutil_socket_t sd, short events __attribute__((unused)),
void *arg __attribute__((unused)))
{
bfd_session *bfd;
bfd_pkt_t *cp;
bool is_mhop, is_vxlan;
ssize_t mlen = 0;
uint8_t old_state;
uint32_t oldEchoXmt_TO, oldXmtTime;
struct sockaddr_any local, peer;
char port[MAXNAMELEN + 1], vrfname[MAXNAMELEN + 1];
bfd_session_vxlan_info_t vxlan_info;
if (sd == bglobal.bg_echo) {
ptm_bfd_process_echo_pkt(sd);
return;
}
is_mhop = is_vxlan = false;
if (sd == bglobal.bg_shop || sd == bglobal.bg_mhop) {
is_mhop = sd == bglobal.bg_mhop;
mlen = bfd_recv_ipv4(sd, is_mhop, port, sizeof(port), vrfname,
sizeof(vrfname), &local, &peer);
} else if (sd == bglobal.bg_shop6 || sd == bglobal.bg_mhop6) {
is_mhop = sd == bglobal.bg_mhop6;
mlen = bfd_recv_ipv6(sd, is_mhop, port, sizeof(port), vrfname,
sizeof(vrfname), &local, &peer);
}
#if 0 /* TODO vxlan handling */
cp = ptm_bfd_process_vxlan_pkt(s, se, udata, &local_ifindex,
&sin, &vxlan_info, rx_pkt, &mlen);
if (!cp) {
return -1;
}
is_vxlan = true;
/* keep in network-byte order */
peer.ip4_addr.s_addr = sin.sin_addr.s_addr;
peer.family = AF_INET;
strcpy(peer_addr, inet_ntoa(sin.sin_addr));
#endif
/* Implement RFC 5880 6.8.6 */
if (mlen < BFD_PKT_LEN) {
INFOLOG("Received short packet from %s", satostr(&peer));
return;
}
cp = (bfd_pkt_t *)(msgbuf);
if (BFD_GETVER(cp->diag) != BFD_VERSION) {
INFOLOG("Received bad version %d from %s", BFD_GETVER(cp->diag),
satostr(&peer));
return;
}
if (cp->detect_mult == 0) {
INFOLOG("Detect Mult is zero in pkt from %s", satostr(&peer));
return;
}
if ((cp->len < BFD_PKT_LEN) || (cp->len > mlen)) {
INFOLOG("Invalid length %d in control pkt from %s", cp->len,
satostr(&peer));
return;
}
if (cp->discrs.my_discr == 0) {
INFOLOG("My discriminator is zero in pkt from %s",
satostr(&peer));
return;
}
if ((bfd = ptm_bfd_sess_find(cp, port, &peer, &local, vrfname, is_mhop))
== NULL) {
DLOG("Failed to generate session from remote packet");
return;
}
if (is_vxlan && !ptm_bfd_validate_vxlan_pkt(bfd, &vxlan_info)) {
return;
}
bfd->stats.rx_ctrl_pkt++;
if (is_mhop) {
if ((BFD_TTL_VAL - bfd->mh_ttl) > ttlval) {
DLOG("Exceeded max hop count of %d, dropped pkt from"
" %s with TTL %d",
bfd->mh_ttl, satostr(&peer), ttlval);
return;
}
} else if (bfd->local_ip.sa_sin.sin_family == AF_UNSPEC) {
bfd->local_ip = local;
}
if ((bfd->discrs.remote_discr != 0)
&& (bfd->discrs.remote_discr != ntohl(cp->discrs.my_discr))) {
DLOG("My Discriminator mismatch in pkt"
"from %s, Expected %d Got %d",
satostr(&peer), bfd->discrs.remote_discr,
ntohl(cp->discrs.my_discr));
}
bfd->discrs.remote_discr = ntohl(cp->discrs.my_discr);
/* If received the Final bit, the new values should take effect */
if (bfd->polling && BFD_GETFBIT(cp->flags)) {
bfd->timers.desired_min_tx = bfd->new_timers.desired_min_tx;
bfd->timers.required_min_rx = bfd->new_timers.required_min_rx;
bfd->new_timers.desired_min_tx = 0;
bfd->new_timers.required_min_rx = 0;
bfd->polling = 0;
}
if (!bfd->demand_mode) {
/* Compute detect time */
bfd->detect_TO = cp->detect_mult
* ((bfd->timers.required_min_rx
> ntohl(cp->timers.desired_min_tx))
? bfd->timers.required_min_rx
: ntohl(cp->timers.desired_min_tx));
bfd->remote_detect_mult = cp->detect_mult;
} else {
ERRLOG("Unsupport BFD mode detected");
}
/* Save remote diagnostics before state switch. */
bfd->remote_diag = cp->diag & BFD_DIAGMASK;
/* State switch from section 6.8.6 */
old_state = bfd->ses_state;
if (BFD_GETSTATE(cp->flags) == PTM_BFD_ADM_DOWN) {
if (bfd->ses_state != PTM_BFD_DOWN) {
ptm_bfd_ses_dn(bfd, BFD_DIAGNEIGHDOWN);
}
} else {
switch (bfd->ses_state) {
case (PTM_BFD_DOWN):
if (BFD_GETSTATE(cp->flags) == PTM_BFD_INIT) {
ptm_bfd_ses_up(bfd);
} else if (BFD_GETSTATE(cp->flags) == PTM_BFD_DOWN) {
bfd->ses_state = PTM_BFD_INIT;
} /* UP stays in DOWN state */
break;
case (PTM_BFD_INIT):
if (BFD_GETSTATE(cp->flags) == PTM_BFD_INIT
|| BFD_GETSTATE(cp->flags) == PTM_BFD_UP) {
ptm_bfd_ses_up(bfd);
} /* DOWN stays in INIT state */
break;
case (PTM_BFD_UP):
if (BFD_GETSTATE(cp->flags) == PTM_BFD_DOWN) {
ptm_bfd_ses_dn(bfd, BFD_DIAGNEIGHDOWN);
} /* INIT and UP stays in UP state */
break;
}
}
if (old_state != bfd->ses_state) {
DLOG("BFD Sess %d [%s] Old State [%s] : New State [%s]",
bfd->discrs.my_discr, satostr(&peer),
state_list[old_state].str, state_list[bfd->ses_state].str);
}
if (BFD_CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO)) {
if (BFD_CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE)) {
if (!ntohl(cp->timers.required_min_echo)) {
ptm_bfd_echo_stop(bfd, 1);
} else {
oldEchoXmt_TO = bfd->echo_xmt_TO;
bfd->echo_xmt_TO =
bfd->timers.required_min_echo;
if (ntohl(cp->timers.required_min_echo)
> bfd->echo_xmt_TO)
bfd->echo_xmt_TO = ntohl(
cp->timers.required_min_echo);
if (oldEchoXmt_TO != bfd->echo_xmt_TO)
ptm_bfd_echo_start(bfd);
}
} else if (ntohl(cp->timers.required_min_echo)) {
bfd->echo_xmt_TO = bfd->timers.required_min_echo;
if (ntohl(cp->timers.required_min_echo)
> bfd->echo_xmt_TO)
bfd->echo_xmt_TO =
ntohl(cp->timers.required_min_echo);
ptm_bfd_echo_start(bfd);
}
}
if (BFD_CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE)) {
if (!ntohl(cp->timers.required_min_echo)) {
}
bfd->echo_xmt_TO = bfd->timers.required_min_echo;
if (ntohl(cp->timers.required_min_echo) > bfd->echo_xmt_TO)
bfd->echo_xmt_TO = ntohl(cp->timers.required_min_echo);
}
/* Calculate new transmit time */
oldXmtTime = bfd->xmt_TO;
bfd->xmt_TO =
(bfd->timers.desired_min_tx > ntohl(cp->timers.required_min_rx))
? bfd->timers.desired_min_tx
: ntohl(cp->timers.required_min_rx);
/* If transmit time has changed, and too much time until next xmt,
* restart
*/
if (BFD_GETPBIT(cp->flags)) {