]> git.proxmox.com Git - mirror_frr.git/blob - bfdd/bfd_packet.c
Merge pull request #12626 from opensourcerouting/fix/bgpd_neighbor_password_unnumbered
[mirror_frr.git] / bfdd / bfd_packet.c
1 /*********************************************************************
2 * Copyright 2017 Cumulus Networks, Inc. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; see the file COPYING; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * bfd_packet.c: implements the BFD protocol packet handling.
19 *
20 * Authors
21 * -------
22 * Shrijeet Mukherjee [shm@cumulusnetworks.com]
23 * Kanna Rajagopal [kanna@cumulusnetworks.com]
24 * Radhika Mahankali [Radhika@cumulusnetworks.com]
25 */
26
27 #include <zebra.h>
28
29 #ifdef BFD_LINUX
30 #include <linux/if_packet.h>
31 #endif /* BFD_LINUX */
32
33 #include <netinet/if_ether.h>
34 #include <netinet/udp.h>
35
36 #include "lib/sockopt.h"
37 #include "lib/checksum.h"
38 #include "lib/network.h"
39
40 #include "bfd.h"
41
42 /*
43 * Prototypes
44 */
45 static int ptm_bfd_process_echo_pkt(struct bfd_vrf_global *bvrf, int s);
46 int _ptm_bfd_send(struct bfd_session *bs, uint16_t *port, const void *data,
47 size_t datalen);
48
49 static void bfd_sd_reschedule(struct bfd_vrf_global *bvrf, int sd);
50 ssize_t bfd_recv_ipv4(int sd, uint8_t *msgbuf, size_t msgbuflen, uint8_t *ttl,
51 ifindex_t *ifindex, struct sockaddr_any *local,
52 struct sockaddr_any *peer);
53 ssize_t bfd_recv_ipv6(int sd, uint8_t *msgbuf, size_t msgbuflen, uint8_t *ttl,
54 ifindex_t *ifindex, struct sockaddr_any *local,
55 struct sockaddr_any *peer);
56 int bp_udp_send(int sd, uint8_t ttl, uint8_t *data, size_t datalen,
57 struct sockaddr *to, socklen_t tolen);
58 int bp_bfd_echo_in(struct bfd_vrf_global *bvrf, int sd, uint8_t *ttl,
59 uint32_t *my_discr, uint64_t *my_rtt);
60 #ifdef BFD_LINUX
61 ssize_t bfd_recv_ipv4_fp(int sd, uint8_t *msgbuf, size_t msgbuflen,
62 uint8_t *ttl, ifindex_t *ifindex,
63 struct sockaddr_any *local, struct sockaddr_any *peer);
64 void bfd_peer_mac_set(int sd, struct bfd_session *bfd,
65 struct sockaddr_any *peer, struct interface *ifp);
66 int bp_udp_send_fp(int sd, uint8_t *data, size_t datalen,
67 struct bfd_session *bfd);
68 ssize_t bfd_recv_fp_echo(int sd, uint8_t *msgbuf, size_t msgbuflen,
69 uint8_t *ttl, ifindex_t *ifindex,
70 struct sockaddr_any *local, struct sockaddr_any *peer);
71 #endif
72
73 /* socket related prototypes */
74 static void bp_set_ipopts(int sd);
75 static void bp_bind_ip(int sd, uint16_t port);
76 static void bp_set_ipv6opts(int sd);
77 static void bp_bind_ipv6(int sd, uint16_t port);
78
79
80 /*
81 * Functions
82 */
83 int _ptm_bfd_send(struct bfd_session *bs, uint16_t *port, const void *data,
84 size_t datalen)
85 {
86 struct sockaddr *sa;
87 struct sockaddr_in sin;
88 struct sockaddr_in6 sin6;
89 socklen_t slen;
90 ssize_t rv;
91 int sd = -1;
92
93 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_IPV6)) {
94 memset(&sin6, 0, sizeof(sin6));
95 sin6.sin6_family = AF_INET6;
96 memcpy(&sin6.sin6_addr, &bs->key.peer, sizeof(sin6.sin6_addr));
97 if (bs->ifp && IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr))
98 sin6.sin6_scope_id = bs->ifp->ifindex;
99
100 sin6.sin6_port =
101 (port) ? *port
102 : (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH))
103 ? htons(BFD_DEF_MHOP_DEST_PORT)
104 : htons(BFD_DEFDESTPORT);
105
106 sd = bs->sock;
107 sa = (struct sockaddr *)&sin6;
108 slen = sizeof(sin6);
109 } else {
110 memset(&sin, 0, sizeof(sin));
111 sin.sin_family = AF_INET;
112 memcpy(&sin.sin_addr, &bs->key.peer, sizeof(sin.sin_addr));
113 sin.sin_port =
114 (port) ? *port
115 : (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH))
116 ? htons(BFD_DEF_MHOP_DEST_PORT)
117 : htons(BFD_DEFDESTPORT);
118
119 sd = bs->sock;
120 sa = (struct sockaddr *)&sin;
121 slen = sizeof(sin);
122 }
123
124 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
125 sa->sa_len = slen;
126 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
127 rv = sendto(sd, data, datalen, 0, sa, slen);
128 if (rv <= 0) {
129 if (bglobal.debug_network)
130 zlog_debug("packet-send: send failure: %s",
131 strerror(errno));
132 return -1;
133 }
134 if (rv < (ssize_t)datalen) {
135 if (bglobal.debug_network)
136 zlog_debug("packet-send: send partial: %s",
137 strerror(errno));
138 }
139
140 return 0;
141 }
142
143 #ifdef BFD_LINUX
144 /*
145 * Compute the UDP checksum.
146 *
147 * Checksum is not set in the packet, just computed.
148 *
149 * pkt
150 * Packet, fully filled out except for checksum field.
151 *
152 * pktsize
153 * sizeof(*pkt)
154 *
155 * ip
156 * IP address that pkt will be transmitted from and too.
157 *
158 * Returns:
159 * Checksum in network byte order.
160 */
161 static uint16_t bfd_pkt_checksum(struct udphdr *pkt, size_t pktsize,
162 struct in6_addr *ip, sa_family_t family)
163 {
164 uint16_t chksum;
165
166 pkt->check = 0;
167
168 if (family == AF_INET6) {
169 struct ipv6_ph ph = {};
170
171 memcpy(&ph.src, ip, sizeof(ph.src));
172 memcpy(&ph.dst, ip, sizeof(ph.dst));
173 ph.ulpl = htons(pktsize);
174 ph.next_hdr = IPPROTO_UDP;
175 chksum = in_cksum_with_ph6(&ph, pkt, pktsize);
176 } else {
177 struct ipv4_ph ph = {};
178
179 memcpy(&ph.src, ip, sizeof(ph.src));
180 memcpy(&ph.dst, ip, sizeof(ph.dst));
181 ph.proto = IPPROTO_UDP;
182 ph.len = htons(pktsize);
183 chksum = in_cksum_with_ph4(&ph, pkt, pktsize);
184 }
185
186 return chksum;
187 }
188
189 /*
190 * This routine creates the entire ECHO packet so that it will be looped
191 * in the forwarding plane of the peer router instead of going up the
192 * stack in BFD to be looped. If we haven't learned the peers MAC yet
193 * no echo is sent.
194 *
195 * echo packet with src/dst IP equal to local IP
196 * dest MAC as peer's MAC
197 *
198 * currently support ipv4
199 */
200 void ptm_bfd_echo_fp_snd(struct bfd_session *bfd)
201 {
202 int sd;
203 struct bfd_vrf_global *bvrf = bfd_vrf_look_by_session(bfd);
204 int total_len = 0;
205 struct ethhdr *eth;
206 struct udphdr *uh;
207 struct iphdr *iph;
208 struct bfd_echo_pkt *beph;
209 static char sendbuff[100];
210 struct timeval time_sent;
211
212 if (!bvrf)
213 return;
214 if (!CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_MAC_SET))
215 return;
216 if (!CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE))
217 SET_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE);
218
219 memset(sendbuff, 0, sizeof(sendbuff));
220
221 /* add eth hdr */
222 eth = (struct ethhdr *)(sendbuff);
223 memcpy(eth->h_source, bfd->ifp->hw_addr, sizeof(eth->h_source));
224 memcpy(eth->h_dest, bfd->peer_hw_addr, sizeof(eth->h_dest));
225
226 total_len += sizeof(struct ethhdr);
227
228 sd = bvrf->bg_echo;
229 eth->h_proto = htons(ETH_P_IP);
230
231 /* add ip hdr */
232 iph = (struct iphdr *)(sendbuff + sizeof(struct ethhdr));
233
234 iph->ihl = sizeof(struct ip) >> 2;
235 iph->version = IPVERSION;
236 iph->tos = IPTOS_PREC_INTERNETCONTROL;
237 iph->id = (uint16_t)frr_weak_random();
238 iph->ttl = BFD_TTL_VAL;
239 iph->protocol = IPPROTO_UDP;
240 memcpy(&iph->saddr, &bfd->local_address.sa_sin.sin_addr,
241 sizeof(bfd->local_address.sa_sin.sin_addr));
242 memcpy(&iph->daddr, &bfd->local_address.sa_sin.sin_addr,
243 sizeof(bfd->local_address.sa_sin.sin_addr));
244 total_len += sizeof(struct iphdr);
245
246 /* add udp hdr */
247 uh = (struct udphdr *)(sendbuff + sizeof(struct iphdr) +
248 sizeof(struct ethhdr));
249 uh->source = htons(BFD_DEF_ECHO_PORT);
250 uh->dest = htons(BFD_DEF_ECHO_PORT);
251
252 total_len += sizeof(struct udphdr);
253
254 /* add bfd echo */
255 beph = (struct bfd_echo_pkt *)(sendbuff + sizeof(struct udphdr) +
256 sizeof(struct iphdr) +
257 sizeof(struct ethhdr));
258
259 beph->ver = BFD_ECHO_VERSION;
260 beph->len = BFD_ECHO_PKT_LEN;
261 beph->my_discr = htonl(bfd->discrs.my_discr);
262
263 /* RTT calculation: add starting time in packet */
264 monotime(&time_sent);
265 beph->time_sent_sec = htobe64(time_sent.tv_sec);
266 beph->time_sent_usec = htobe64(time_sent.tv_usec);
267
268 total_len += sizeof(struct bfd_echo_pkt);
269 uh->len =
270 htons(total_len - sizeof(struct iphdr) - sizeof(struct ethhdr));
271 uh->check = bfd_pkt_checksum(
272 uh, (total_len - sizeof(struct iphdr) - sizeof(struct ethhdr)),
273 (struct in6_addr *)&iph->saddr, AF_INET);
274
275 iph->tot_len = htons(total_len - sizeof(struct ethhdr));
276 iph->check = in_cksum((const void *)iph, sizeof(struct iphdr));
277
278 if (bp_udp_send_fp(sd, (uint8_t *)&sendbuff, total_len, bfd) == -1)
279 return;
280
281 bfd->stats.tx_echo_pkt++;
282 }
283 #endif
284
285 void ptm_bfd_echo_snd(struct bfd_session *bfd)
286 {
287 struct sockaddr *sa;
288 socklen_t salen;
289 int sd;
290 struct bfd_echo_pkt bep;
291 struct sockaddr_in sin;
292 struct sockaddr_in6 sin6;
293 struct bfd_vrf_global *bvrf = bfd_vrf_look_by_session(bfd);
294
295 if (!bvrf)
296 return;
297 if (!CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE))
298 SET_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE);
299
300 memset(&bep, 0, sizeof(bep));
301 bep.ver = BFD_ECHO_VERSION;
302 bep.len = BFD_ECHO_PKT_LEN;
303 bep.my_discr = htonl(bfd->discrs.my_discr);
304
305 if (CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_IPV6)) {
306 if (bvrf->bg_echov6 == -1)
307 return;
308 sd = bvrf->bg_echov6;
309 memset(&sin6, 0, sizeof(sin6));
310 sin6.sin6_family = AF_INET6;
311 memcpy(&sin6.sin6_addr, &bfd->key.peer, sizeof(sin6.sin6_addr));
312 if (bfd->ifp && IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr))
313 sin6.sin6_scope_id = bfd->ifp->ifindex;
314
315 sin6.sin6_port = htons(BFD_DEF_ECHO_PORT);
316 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
317 sin6.sin6_len = sizeof(sin6);
318 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
319
320 sa = (struct sockaddr *)&sin6;
321 salen = sizeof(sin6);
322 } else {
323 sd = bvrf->bg_echo;
324 memset(&sin, 0, sizeof(sin));
325 sin.sin_family = AF_INET;
326 memcpy(&sin.sin_addr, &bfd->key.peer, sizeof(sin.sin_addr));
327 sin.sin_port = htons(BFD_DEF_ECHO_PORT);
328 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
329 sin.sin_len = sizeof(sin);
330 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
331
332 sa = (struct sockaddr *)&sin;
333 salen = sizeof(sin);
334 }
335 if (bp_udp_send(sd, BFD_TTL_VAL, (uint8_t *)&bep, sizeof(bep), sa,
336 salen)
337 == -1)
338 return;
339
340 bfd->stats.tx_echo_pkt++;
341 }
342
343 static int ptm_bfd_process_echo_pkt(struct bfd_vrf_global *bvrf, int s)
344 {
345 struct bfd_session *bfd;
346 uint32_t my_discr = 0;
347 uint64_t my_rtt = 0;
348 uint8_t ttl = 0;
349
350 /* Receive and parse echo packet. */
351 if (bp_bfd_echo_in(bvrf, s, &ttl, &my_discr, &my_rtt) == -1)
352 return 0;
353
354 /* Your discriminator not zero - use it to find session */
355 bfd = bfd_id_lookup(my_discr);
356 if (bfd == NULL) {
357 if (bglobal.debug_network)
358 zlog_debug("echo-packet: no matching session (id:%u)",
359 my_discr);
360 return -1;
361 }
362
363 if (!CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE)) {
364 if (bglobal.debug_network)
365 zlog_debug("echo-packet: echo disabled [%s] (id:%u)",
366 bs_to_string(bfd), my_discr);
367 return -1;
368 }
369
370 /* RTT Calculation: add current RTT to samples */
371 if (my_rtt != 0) {
372 bfd->rtt[bfd->rtt_index] = my_rtt;
373 bfd->rtt_index++;
374 if (bfd->rtt_index >= BFD_RTT_SAMPLE)
375 bfd->rtt_index = 0;
376 if (bfd->rtt_valid < BFD_RTT_SAMPLE)
377 bfd->rtt_valid++;
378 }
379
380 bfd->stats.rx_echo_pkt++;
381
382 /* Compute detect time */
383 bfd->echo_detect_TO = bfd->remote_detect_mult * bfd->echo_xmt_TO;
384
385 /* Update echo receive timeout. */
386 if (bfd->echo_detect_TO > 0)
387 bfd_echo_recvtimer_update(bfd);
388
389 return 0;
390 }
391
392 void ptm_bfd_snd(struct bfd_session *bfd, int fbit)
393 {
394 struct bfd_pkt cp = {};
395
396 /* Set fields according to section 6.5.7 */
397 cp.diag = bfd->local_diag;
398 BFD_SETVER(cp.diag, BFD_VERSION);
399 cp.flags = 0;
400 BFD_SETSTATE(cp.flags, bfd->ses_state);
401
402 if (CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_CBIT))
403 BFD_SETCBIT(cp.flags, BFD_CBIT);
404
405 BFD_SETDEMANDBIT(cp.flags, BFD_DEF_DEMAND);
406
407 /*
408 * Polling and Final can't be set at the same time.
409 *
410 * RFC 5880, Section 6.5.
411 */
412 BFD_SETFBIT(cp.flags, fbit);
413 if (fbit == 0)
414 BFD_SETPBIT(cp.flags, bfd->polling);
415
416 cp.detect_mult = bfd->detect_mult;
417 cp.len = BFD_PKT_LEN;
418 cp.discrs.my_discr = htonl(bfd->discrs.my_discr);
419 cp.discrs.remote_discr = htonl(bfd->discrs.remote_discr);
420 if (bfd->polling) {
421 cp.timers.desired_min_tx =
422 htonl(bfd->timers.desired_min_tx);
423 cp.timers.required_min_rx =
424 htonl(bfd->timers.required_min_rx);
425 } else {
426 /*
427 * We can only announce current setting on poll, this
428 * avoids timing mismatch with our peer and give it
429 * the oportunity to learn. See `bs_final_handler` for
430 * more information.
431 */
432 cp.timers.desired_min_tx =
433 htonl(bfd->cur_timers.desired_min_tx);
434 cp.timers.required_min_rx =
435 htonl(bfd->cur_timers.required_min_rx);
436 }
437 cp.timers.required_min_echo = htonl(bfd->timers.required_min_echo_rx);
438
439 if (_ptm_bfd_send(bfd, NULL, &cp, BFD_PKT_LEN) != 0)
440 return;
441
442 bfd->stats.tx_ctrl_pkt++;
443 }
444
445 #ifdef BFD_LINUX
446 /*
447 * receive the ipv4 echo packet that was loopback in the peers forwarding plane
448 */
449 ssize_t bfd_recv_ipv4_fp(int sd, uint8_t *msgbuf, size_t msgbuflen,
450 uint8_t *ttl, ifindex_t *ifindex,
451 struct sockaddr_any *local, struct sockaddr_any *peer)
452 {
453 ssize_t mlen;
454 struct sockaddr_ll msgaddr;
455 struct msghdr msghdr;
456 struct iovec iov[1];
457 uint16_t recv_checksum;
458 uint16_t checksum;
459 struct iphdr *ip;
460 struct udphdr *uh;
461
462 /* Prepare the recvmsg params. */
463 iov[0].iov_base = msgbuf;
464 iov[0].iov_len = msgbuflen;
465
466 memset(&msghdr, 0, sizeof(msghdr));
467 msghdr.msg_name = &msgaddr;
468 msghdr.msg_namelen = sizeof(msgaddr);
469 msghdr.msg_iov = iov;
470 msghdr.msg_iovlen = 1;
471
472 mlen = recvmsg(sd, &msghdr, MSG_DONTWAIT);
473 if (mlen == -1) {
474 if (errno != EAGAIN || errno != EWOULDBLOCK || errno != EINTR)
475 zlog_err("%s: recv failed: %s", __func__,
476 strerror(errno));
477
478 return -1;
479 }
480
481 ip = (struct iphdr *)(msgbuf + sizeof(struct ethhdr));
482
483 /* verify ip checksum */
484 recv_checksum = ip->check;
485 ip->check = 0;
486 checksum = in_cksum((const void *)ip, sizeof(struct iphdr));
487 if (recv_checksum != checksum) {
488 if (bglobal.debug_network)
489 zlog_debug(
490 "%s: invalid iphdr checksum expected 0x%x rcvd 0x%x",
491 __func__, checksum, recv_checksum);
492 return -1;
493 }
494
495 *ttl = ip->ttl;
496 if (*ttl != 254) {
497 /* Echo should be looped in peer's forwarding plane, but it also
498 * comes up to BFD so silently drop it
499 */
500 if (ip->daddr == ip->saddr)
501 return -1;
502
503 if (bglobal.debug_network)
504 zlog_debug("%s: invalid TTL: %u", __func__, *ttl);
505 return -1;
506 }
507
508 local->sa_sin.sin_family = AF_INET;
509 memcpy(&local->sa_sin.sin_addr, &ip->saddr, sizeof(ip->saddr));
510 peer->sa_sin.sin_family = AF_INET;
511 memcpy(&peer->sa_sin.sin_addr, &ip->daddr, sizeof(ip->daddr));
512
513 *ifindex = msgaddr.sll_ifindex;
514
515 /* verify udp checksum */
516 uh = (struct udphdr *)(msgbuf + sizeof(struct iphdr) +
517 sizeof(struct ethhdr));
518 recv_checksum = uh->check;
519 uh->check = 0;
520 checksum = bfd_pkt_checksum(uh, ntohs(uh->len),
521 (struct in6_addr *)&ip->saddr, AF_INET);
522 if (recv_checksum != checksum) {
523 if (bglobal.debug_network)
524 zlog_debug(
525 "%s: invalid udphdr checksum expected 0x%x rcvd 0x%x",
526 __func__, checksum, recv_checksum);
527 return -1;
528 }
529 return mlen;
530 }
531 #endif
532
533 ssize_t bfd_recv_ipv4(int sd, uint8_t *msgbuf, size_t msgbuflen, uint8_t *ttl,
534 ifindex_t *ifindex, struct sockaddr_any *local,
535 struct sockaddr_any *peer)
536 {
537 struct cmsghdr *cm;
538 ssize_t mlen;
539 struct sockaddr_in msgaddr;
540 struct msghdr msghdr;
541 struct iovec iov[1];
542 uint8_t cmsgbuf[255];
543
544 /* Prepare the recvmsg params. */
545 iov[0].iov_base = msgbuf;
546 iov[0].iov_len = msgbuflen;
547
548 memset(&msghdr, 0, sizeof(msghdr));
549 msghdr.msg_name = &msgaddr;
550 msghdr.msg_namelen = sizeof(msgaddr);
551 msghdr.msg_iov = iov;
552 msghdr.msg_iovlen = 1;
553 msghdr.msg_control = cmsgbuf;
554 msghdr.msg_controllen = sizeof(cmsgbuf);
555
556 mlen = recvmsg(sd, &msghdr, MSG_DONTWAIT);
557 if (mlen == -1) {
558 if (errno != EAGAIN)
559 zlog_err("ipv4-recv: recv failed: %s", strerror(errno));
560
561 return -1;
562 }
563
564 /* Get source address */
565 peer->sa_sin = *((struct sockaddr_in *)(msghdr.msg_name));
566
567 /* Get and check TTL */
568 for (cm = CMSG_FIRSTHDR(&msghdr); cm != NULL;
569 cm = CMSG_NXTHDR(&msghdr, cm)) {
570 if (cm->cmsg_level != IPPROTO_IP)
571 continue;
572
573 switch (cm->cmsg_type) {
574 #ifdef BFD_LINUX
575 case IP_TTL: {
576 uint32_t ttlval;
577
578 memcpy(&ttlval, CMSG_DATA(cm), sizeof(ttlval));
579 if (ttlval > 255) {
580 if (bglobal.debug_network)
581 zlog_debug("%s: invalid TTL: %u",
582 __func__, ttlval);
583 return -1;
584 }
585 *ttl = ttlval;
586 break;
587 }
588
589 case IP_PKTINFO: {
590 struct in_pktinfo *pi =
591 (struct in_pktinfo *)CMSG_DATA(cm);
592
593 if (pi == NULL)
594 break;
595
596 local->sa_sin.sin_family = AF_INET;
597 local->sa_sin.sin_addr = pi->ipi_addr;
598 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
599 local->sa_sin.sin_len = sizeof(local->sa_sin);
600 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
601
602 *ifindex = pi->ipi_ifindex;
603 break;
604 }
605 #endif /* BFD_LINUX */
606 #ifdef BFD_BSD
607 case IP_RECVTTL: {
608 memcpy(ttl, CMSG_DATA(cm), sizeof(*ttl));
609 break;
610 }
611
612 case IP_RECVDSTADDR: {
613 struct in_addr ia;
614
615 memcpy(&ia, CMSG_DATA(cm), sizeof(ia));
616 local->sa_sin.sin_family = AF_INET;
617 local->sa_sin.sin_addr = ia;
618 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
619 local->sa_sin.sin_len = sizeof(local->sa_sin);
620 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
621 break;
622 }
623 #endif /* BFD_BSD */
624
625 default:
626 /*
627 * On *BSDs we expect to land here when skipping
628 * the IP_RECVIF header. It will be handled by
629 * getsockopt_ifindex() below.
630 */
631 /* NOTHING */
632 break;
633 }
634 }
635
636 /* OS agnostic way of getting interface name. */
637 if (*ifindex == IFINDEX_INTERNAL)
638 *ifindex = getsockopt_ifindex(AF_INET, &msghdr);
639
640 return mlen;
641 }
642
643 ssize_t bfd_recv_ipv6(int sd, uint8_t *msgbuf, size_t msgbuflen, uint8_t *ttl,
644 ifindex_t *ifindex, struct sockaddr_any *local,
645 struct sockaddr_any *peer)
646 {
647 struct cmsghdr *cm;
648 struct in6_pktinfo *pi6 = NULL;
649 ssize_t mlen;
650 uint32_t ttlval;
651 struct sockaddr_in6 msgaddr6;
652 struct msghdr msghdr6;
653 struct iovec iov[1];
654 uint8_t cmsgbuf6[255];
655
656 /* Prepare the recvmsg params. */
657 iov[0].iov_base = msgbuf;
658 iov[0].iov_len = msgbuflen;
659
660 memset(&msghdr6, 0, sizeof(msghdr6));
661 msghdr6.msg_name = &msgaddr6;
662 msghdr6.msg_namelen = sizeof(msgaddr6);
663 msghdr6.msg_iov = iov;
664 msghdr6.msg_iovlen = 1;
665 msghdr6.msg_control = cmsgbuf6;
666 msghdr6.msg_controllen = sizeof(cmsgbuf6);
667
668 mlen = recvmsg(sd, &msghdr6, MSG_DONTWAIT);
669 if (mlen == -1) {
670 if (errno != EAGAIN)
671 zlog_err("ipv6-recv: recv failed: %s", strerror(errno));
672
673 return -1;
674 }
675
676 /* Get source address */
677 peer->sa_sin6 = *((struct sockaddr_in6 *)(msghdr6.msg_name));
678
679 /* Get and check TTL */
680 for (cm = CMSG_FIRSTHDR(&msghdr6); cm != NULL;
681 cm = CMSG_NXTHDR(&msghdr6, cm)) {
682 if (cm->cmsg_level != IPPROTO_IPV6)
683 continue;
684
685 if (cm->cmsg_type == IPV6_HOPLIMIT) {
686 memcpy(&ttlval, CMSG_DATA(cm), sizeof(ttlval));
687 if (ttlval > 255) {
688 if (bglobal.debug_network)
689 zlog_debug("%s: invalid TTL: %u",
690 __func__, ttlval);
691 return -1;
692 }
693
694 *ttl = ttlval;
695 } else if (cm->cmsg_type == IPV6_PKTINFO) {
696 pi6 = (struct in6_pktinfo *)CMSG_DATA(cm);
697 if (pi6) {
698 local->sa_sin6.sin6_family = AF_INET6;
699 local->sa_sin6.sin6_addr = pi6->ipi6_addr;
700 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
701 local->sa_sin6.sin6_len = sizeof(local->sa_sin6);
702 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
703
704 *ifindex = pi6->ipi6_ifindex;
705
706 /* Set scope ID for link local addresses. */
707 if (IN6_IS_ADDR_LINKLOCAL(
708 &peer->sa_sin6.sin6_addr))
709 peer->sa_sin6.sin6_scope_id = *ifindex;
710 if (IN6_IS_ADDR_LINKLOCAL(
711 &local->sa_sin6.sin6_addr))
712 local->sa_sin6.sin6_scope_id = *ifindex;
713 }
714 }
715 }
716
717 return mlen;
718 }
719
720 static void bfd_sd_reschedule(struct bfd_vrf_global *bvrf, int sd)
721 {
722 if (sd == bvrf->bg_shop) {
723 THREAD_OFF(bvrf->bg_ev[0]);
724 thread_add_read(master, bfd_recv_cb, bvrf, bvrf->bg_shop,
725 &bvrf->bg_ev[0]);
726 } else if (sd == bvrf->bg_mhop) {
727 THREAD_OFF(bvrf->bg_ev[1]);
728 thread_add_read(master, bfd_recv_cb, bvrf, bvrf->bg_mhop,
729 &bvrf->bg_ev[1]);
730 } else if (sd == bvrf->bg_shop6) {
731 THREAD_OFF(bvrf->bg_ev[2]);
732 thread_add_read(master, bfd_recv_cb, bvrf, bvrf->bg_shop6,
733 &bvrf->bg_ev[2]);
734 } else if (sd == bvrf->bg_mhop6) {
735 THREAD_OFF(bvrf->bg_ev[3]);
736 thread_add_read(master, bfd_recv_cb, bvrf, bvrf->bg_mhop6,
737 &bvrf->bg_ev[3]);
738 } else if (sd == bvrf->bg_echo) {
739 THREAD_OFF(bvrf->bg_ev[4]);
740 thread_add_read(master, bfd_recv_cb, bvrf, bvrf->bg_echo,
741 &bvrf->bg_ev[4]);
742 } else if (sd == bvrf->bg_echov6) {
743 THREAD_OFF(bvrf->bg_ev[5]);
744 thread_add_read(master, bfd_recv_cb, bvrf, bvrf->bg_echov6,
745 &bvrf->bg_ev[5]);
746 }
747 }
748
749 static void cp_debug(bool mhop, struct sockaddr_any *peer,
750 struct sockaddr_any *local, ifindex_t ifindex,
751 vrf_id_t vrfid, const char *fmt, ...)
752 {
753 char buf[512], peerstr[128], localstr[128], portstr[64], vrfstr[64];
754 va_list vl;
755
756 /* Don't to any processing if debug is disabled. */
757 if (bglobal.debug_network == false)
758 return;
759
760 if (peer->sa_sin.sin_family)
761 snprintf(peerstr, sizeof(peerstr), " peer:%s", satostr(peer));
762 else
763 peerstr[0] = 0;
764
765 if (local->sa_sin.sin_family)
766 snprintf(localstr, sizeof(localstr), " local:%s",
767 satostr(local));
768 else
769 localstr[0] = 0;
770
771 if (ifindex != IFINDEX_INTERNAL)
772 snprintf(portstr, sizeof(portstr), " port:%u", ifindex);
773 else
774 portstr[0] = 0;
775
776 if (vrfid != VRF_DEFAULT)
777 snprintf(vrfstr, sizeof(vrfstr), " vrf:%u", vrfid);
778 else
779 vrfstr[0] = 0;
780
781 va_start(vl, fmt);
782 vsnprintf(buf, sizeof(buf), fmt, vl);
783 va_end(vl);
784
785 zlog_debug("control-packet: %s [mhop:%s%s%s%s%s]", buf,
786 mhop ? "yes" : "no", peerstr, localstr, portstr, vrfstr);
787 }
788
789 void bfd_recv_cb(struct thread *t)
790 {
791 int sd = THREAD_FD(t);
792 struct bfd_session *bfd;
793 struct bfd_pkt *cp;
794 bool is_mhop;
795 ssize_t mlen = 0;
796 uint8_t ttl = 0;
797 vrf_id_t vrfid;
798 ifindex_t ifindex = IFINDEX_INTERNAL;
799 struct sockaddr_any local, peer;
800 uint8_t msgbuf[1516];
801 struct interface *ifp = NULL;
802 struct bfd_vrf_global *bvrf = THREAD_ARG(t);
803
804 /* Schedule next read. */
805 bfd_sd_reschedule(bvrf, sd);
806
807 /* Handle echo packets. */
808 if (sd == bvrf->bg_echo || sd == bvrf->bg_echov6) {
809 ptm_bfd_process_echo_pkt(bvrf, sd);
810 return;
811 }
812
813 /* Sanitize input/output. */
814 memset(&local, 0, sizeof(local));
815 memset(&peer, 0, sizeof(peer));
816
817 /* Handle control packets. */
818 is_mhop = false;
819 if (sd == bvrf->bg_shop || sd == bvrf->bg_mhop) {
820 is_mhop = sd == bvrf->bg_mhop;
821 mlen = bfd_recv_ipv4(sd, msgbuf, sizeof(msgbuf), &ttl, &ifindex,
822 &local, &peer);
823 } else if (sd == bvrf->bg_shop6 || sd == bvrf->bg_mhop6) {
824 is_mhop = sd == bvrf->bg_mhop6;
825 mlen = bfd_recv_ipv6(sd, msgbuf, sizeof(msgbuf), &ttl, &ifindex,
826 &local, &peer);
827 }
828
829 /*
830 * With netns backend, we have a separate socket in each VRF. It means
831 * that bvrf here is correct and we believe the bvrf->vrf->vrf_id.
832 * With VRF-lite backend, we have a single socket in the default VRF.
833 * It means that we can't believe the bvrf->vrf->vrf_id. But in
834 * VRF-lite, the ifindex is globally unique, so we can retrieve the
835 * correct vrf_id from the interface.
836 */
837 vrfid = bvrf->vrf->vrf_id;
838 if (ifindex) {
839 ifp = if_lookup_by_index(ifindex, vrfid);
840 if (ifp)
841 vrfid = ifp->vrf->vrf_id;
842 }
843
844 /* Implement RFC 5880 6.8.6 */
845 if (mlen < BFD_PKT_LEN) {
846 cp_debug(is_mhop, &peer, &local, ifindex, vrfid,
847 "too small (%ld bytes)", mlen);
848 return;
849 }
850
851 /* Validate single hop packet TTL. */
852 if ((!is_mhop) && (ttl != BFD_TTL_VAL)) {
853 cp_debug(is_mhop, &peer, &local, ifindex, vrfid,
854 "invalid TTL: %d expected %d", ttl, BFD_TTL_VAL);
855 return;
856 }
857
858 /*
859 * Parse the control header for inconsistencies:
860 * - Invalid version;
861 * - Bad multiplier configuration;
862 * - Short packets;
863 * - Invalid discriminator;
864 */
865 cp = (struct bfd_pkt *)(msgbuf);
866 if (BFD_GETVER(cp->diag) != BFD_VERSION) {
867 cp_debug(is_mhop, &peer, &local, ifindex, vrfid,
868 "bad version %d", BFD_GETVER(cp->diag));
869 return;
870 }
871
872 if (cp->detect_mult == 0) {
873 cp_debug(is_mhop, &peer, &local, ifindex, vrfid,
874 "detect multiplier set to zero");
875 return;
876 }
877
878 if ((cp->len < BFD_PKT_LEN) || (cp->len > mlen)) {
879 cp_debug(is_mhop, &peer, &local, ifindex, vrfid, "too small");
880 return;
881 }
882
883 if (cp->discrs.my_discr == 0) {
884 cp_debug(is_mhop, &peer, &local, ifindex, vrfid,
885 "'my discriminator' is zero");
886 return;
887 }
888
889 /* Find the session that this packet belongs. */
890 bfd = ptm_bfd_sess_find(cp, &peer, &local, ifp, vrfid, is_mhop);
891 if (bfd == NULL) {
892 cp_debug(is_mhop, &peer, &local, ifindex, vrfid,
893 "no session found");
894 return;
895 }
896 /*
897 * We may have a situation where received packet is on wrong vrf
898 */
899 if (bfd && bfd->vrf && bfd->vrf != bvrf->vrf) {
900 cp_debug(is_mhop, &peer, &local, ifindex, vrfid,
901 "wrong vrfid.");
902 return;
903 }
904
905 /* Ensure that existing good sessions are not overridden. */
906 if (!cp->discrs.remote_discr && bfd->ses_state != PTM_BFD_DOWN &&
907 bfd->ses_state != PTM_BFD_ADM_DOWN) {
908 cp_debug(is_mhop, &peer, &local, ifindex, vrfid,
909 "'remote discriminator' is zero, not overridden");
910 return;
911 }
912
913 /*
914 * Multi hop: validate packet TTL.
915 * Single hop: set local address that received the packet.
916 * set peers mac address for echo packets
917 */
918 if (is_mhop) {
919 if (ttl < bfd->mh_ttl) {
920 cp_debug(is_mhop, &peer, &local, ifindex, vrfid,
921 "exceeded max hop count (expected %d, got %d)",
922 bfd->mh_ttl, ttl);
923 return;
924 }
925 } else {
926
927 if (bfd->local_address.sa_sin.sin_family == AF_UNSPEC)
928 bfd->local_address = local;
929 #ifdef BFD_LINUX
930 if (ifp)
931 bfd_peer_mac_set(sd, bfd, &peer, ifp);
932 #endif
933 }
934
935 bfd->stats.rx_ctrl_pkt++;
936
937 /*
938 * If no interface was detected, save the interface where the
939 * packet came in.
940 */
941 if (!is_mhop && bfd->ifp == NULL)
942 bfd->ifp = ifp;
943
944 /* Log remote discriminator changes. */
945 if ((bfd->discrs.remote_discr != 0)
946 && (bfd->discrs.remote_discr != ntohl(cp->discrs.my_discr)))
947 cp_debug(is_mhop, &peer, &local, ifindex, vrfid,
948 "remote discriminator mismatch (expected %u, got %u)",
949 bfd->discrs.remote_discr, ntohl(cp->discrs.my_discr));
950
951 bfd->discrs.remote_discr = ntohl(cp->discrs.my_discr);
952
953 /* Save remote diagnostics before state switch. */
954 bfd->remote_diag = cp->diag & BFD_DIAGMASK;
955
956 /* Update remote timers settings. */
957 bfd->remote_timers.desired_min_tx = ntohl(cp->timers.desired_min_tx);
958 bfd->remote_timers.required_min_rx = ntohl(cp->timers.required_min_rx);
959 bfd->remote_timers.required_min_echo =
960 ntohl(cp->timers.required_min_echo);
961 bfd->remote_detect_mult = cp->detect_mult;
962
963 if (BFD_GETCBIT(cp->flags))
964 bfd->remote_cbit = 1;
965 else
966 bfd->remote_cbit = 0;
967
968 /* State switch from section 6.2. */
969 bs_state_handler(bfd, BFD_GETSTATE(cp->flags));
970
971 /* RFC 5880, Section 6.5: handle POLL/FINAL negotiation sequence. */
972 if (bfd->polling && BFD_GETFBIT(cp->flags)) {
973 /* Disable polling. */
974 bfd->polling = 0;
975
976 /* Handle poll finalization. */
977 bs_final_handler(bfd);
978 }
979
980 /*
981 * Detection timeout calculation:
982 * The minimum detection timeout is the remote detection
983 * multipler (number of packets to be missed) times the agreed
984 * transmission interval.
985 *
986 * RFC 5880, Section 6.8.4.
987 */
988 if (bfd->cur_timers.required_min_rx > bfd->remote_timers.desired_min_tx)
989 bfd->detect_TO = bfd->remote_detect_mult
990 * bfd->cur_timers.required_min_rx;
991 else
992 bfd->detect_TO = bfd->remote_detect_mult
993 * bfd->remote_timers.desired_min_tx;
994
995 /* Apply new receive timer immediately. */
996 bfd_recvtimer_update(bfd);
997
998 /* Handle echo timers changes. */
999 bs_echo_timer_handler(bfd);
1000
1001 /*
1002 * We've received a packet with the POLL bit set, we must send
1003 * a control packet back with the FINAL bit set.
1004 *
1005 * RFC 5880, Section 6.5.
1006 */
1007 if (BFD_GETPBIT(cp->flags)) {
1008 /* We are finalizing a poll negotiation. */
1009 bs_final_handler(bfd);
1010
1011 /* Send the control packet with the final bit immediately. */
1012 ptm_bfd_snd(bfd, 1);
1013 }
1014 }
1015
1016 /*
1017 * bp_bfd_echo_in: proccesses an BFD echo packet. On TTL == BFD_TTL_VAL
1018 * the packet is looped back or returns the my discriminator ID along
1019 * with the TTL.
1020 *
1021 * Returns -1 on error or loopback or 0 on success.
1022 */
1023 int bp_bfd_echo_in(struct bfd_vrf_global *bvrf, int sd, uint8_t *ttl,
1024 uint32_t *my_discr, uint64_t *my_rtt)
1025 {
1026 struct bfd_echo_pkt *bep;
1027 ssize_t rlen;
1028 struct sockaddr_any local, peer;
1029 ifindex_t ifindex = IFINDEX_INTERNAL;
1030 vrf_id_t vrfid = VRF_DEFAULT;
1031 uint8_t msgbuf[1516];
1032 size_t bfd_offset = 0;
1033
1034 if (sd == bvrf->bg_echo) {
1035 #ifdef BFD_LINUX
1036 rlen = bfd_recv_ipv4_fp(sd, msgbuf, sizeof(msgbuf), ttl,
1037 &ifindex, &local, &peer);
1038
1039 /* silently drop echo packet that is looped in fastpath but
1040 * still comes up to BFD
1041 */
1042 if (rlen == -1)
1043 return -1;
1044 bfd_offset = sizeof(struct udphdr) + sizeof(struct iphdr) +
1045 sizeof(struct ethhdr);
1046 #else
1047 rlen = bfd_recv_ipv4(sd, msgbuf, sizeof(msgbuf), ttl, &ifindex,
1048 &local, &peer);
1049 bfd_offset = 0;
1050 #endif
1051 } else {
1052 rlen = bfd_recv_ipv6(sd, msgbuf, sizeof(msgbuf), ttl, &ifindex,
1053 &local, &peer);
1054 bfd_offset = 0;
1055 }
1056
1057 /* Short packet, better not risk reading it. */
1058 if (rlen < (ssize_t)sizeof(*bep)) {
1059 cp_debug(false, &peer, &local, ifindex, vrfid,
1060 "small echo packet");
1061 return -1;
1062 }
1063
1064 /* Test for loopback for ipv6, ipv4 is looped in forwarding plane */
1065 if ((*ttl == BFD_TTL_VAL) && (sd == bvrf->bg_echov6)) {
1066 bp_udp_send(sd, *ttl - 1, msgbuf, rlen,
1067 (struct sockaddr *)&peer,
1068 (sd == bvrf->bg_echo) ? sizeof(peer.sa_sin)
1069 : sizeof(peer.sa_sin6));
1070 return -1;
1071 }
1072
1073 /* Read my discriminator from BFD Echo packet. */
1074 bep = (struct bfd_echo_pkt *)(msgbuf + bfd_offset);
1075 *my_discr = ntohl(bep->my_discr);
1076 if (*my_discr == 0) {
1077 cp_debug(false, &peer, &local, ifindex, vrfid,
1078 "invalid echo packet discriminator (zero)");
1079 return -1;
1080 }
1081
1082 #ifdef BFD_LINUX
1083 /* RTT Calculation: determine RTT time of IPv4 echo pkt */
1084 if (sd == bvrf->bg_echo) {
1085 struct timeval time_sent = {0, 0};
1086
1087 time_sent.tv_sec = be64toh(bep->time_sent_sec);
1088 time_sent.tv_usec = be64toh(bep->time_sent_usec);
1089 *my_rtt = monotime_since(&time_sent, NULL);
1090 }
1091 #endif
1092
1093 return 0;
1094 }
1095
1096 #ifdef BFD_LINUX
1097 /*
1098 * send a bfd packet with src/dst same IP so that the peer will receive
1099 * the packet and forward it back to sender in the forwarding plane
1100 */
1101 int bp_udp_send_fp(int sd, uint8_t *data, size_t datalen,
1102 struct bfd_session *bfd)
1103 {
1104 ssize_t wlen;
1105 struct msghdr msg = {0};
1106 struct iovec iov[1];
1107 uint8_t msgctl[255];
1108 struct sockaddr_ll sadr_ll = {0};
1109
1110 sadr_ll.sll_ifindex = bfd->ifp->ifindex;
1111 sadr_ll.sll_halen = ETH_ALEN;
1112 memcpy(sadr_ll.sll_addr, bfd->peer_hw_addr, sizeof(bfd->peer_hw_addr));
1113 sadr_ll.sll_protocol = htons(ETH_P_IP);
1114
1115 /* Prepare message data. */
1116 iov[0].iov_base = data;
1117 iov[0].iov_len = datalen;
1118
1119 memset(msgctl, 0, sizeof(msgctl));
1120 msg.msg_name = &sadr_ll;
1121 msg.msg_namelen = sizeof(sadr_ll);
1122 msg.msg_iov = iov;
1123 msg.msg_iovlen = 1;
1124
1125 /* Send echo to peer */
1126 wlen = sendmsg(sd, &msg, 0);
1127
1128 if (wlen <= 0) {
1129 if (bglobal.debug_network)
1130 zlog_debug("%s: loopback failure: (%d) %s", __func__,
1131 errno, strerror(errno));
1132 return -1;
1133 } else if (wlen < (ssize_t)datalen) {
1134 if (bglobal.debug_network)
1135 zlog_debug("%s: partial send: %zd expected %zu",
1136 __func__, wlen, datalen);
1137 return -1;
1138 }
1139
1140 return 0;
1141 }
1142 #endif
1143
1144 int bp_udp_send(int sd, uint8_t ttl, uint8_t *data, size_t datalen,
1145 struct sockaddr *to, socklen_t tolen)
1146 {
1147 struct cmsghdr *cmsg;
1148 ssize_t wlen;
1149 int ttlval = ttl;
1150 bool is_ipv6 = to->sa_family == AF_INET6;
1151 struct msghdr msg;
1152 struct iovec iov[1];
1153 uint8_t msgctl[255];
1154
1155 /* Prepare message data. */
1156 iov[0].iov_base = data;
1157 iov[0].iov_len = datalen;
1158
1159 memset(&msg, 0, sizeof(msg));
1160 memset(msgctl, 0, sizeof(msgctl));
1161 msg.msg_name = to;
1162 msg.msg_namelen = tolen;
1163 msg.msg_iov = iov;
1164 msg.msg_iovlen = 1;
1165
1166 /* Prepare the packet TTL information. */
1167 if (ttl > 0) {
1168 /* Use ancillary data. */
1169 msg.msg_control = msgctl;
1170 msg.msg_controllen = CMSG_LEN(sizeof(ttlval));
1171
1172 /* Configure the ancillary data. */
1173 cmsg = CMSG_FIRSTHDR(&msg);
1174 cmsg->cmsg_len = CMSG_LEN(sizeof(ttlval));
1175 if (is_ipv6) {
1176 cmsg->cmsg_level = IPPROTO_IPV6;
1177 cmsg->cmsg_type = IPV6_HOPLIMIT;
1178 } else {
1179 #ifdef BFD_LINUX
1180 cmsg->cmsg_level = IPPROTO_IP;
1181 cmsg->cmsg_type = IP_TTL;
1182 #else
1183 /* FreeBSD does not support TTL in ancillary data. */
1184 msg.msg_control = NULL;
1185 msg.msg_controllen = 0;
1186
1187 bp_set_ttl(sd, ttl);
1188 #endif /* BFD_BSD */
1189 }
1190 memcpy(CMSG_DATA(cmsg), &ttlval, sizeof(ttlval));
1191 }
1192
1193 /* Send echo back. */
1194 wlen = sendmsg(sd, &msg, 0);
1195 if (wlen <= 0) {
1196 if (bglobal.debug_network)
1197 zlog_debug("%s: loopback failure: (%d) %s", __func__,
1198 errno, strerror(errno));
1199 return -1;
1200 } else if (wlen < (ssize_t)datalen) {
1201 if (bglobal.debug_network)
1202 zlog_debug("%s: partial send: %zd expected %zu",
1203 __func__, wlen, datalen);
1204 return -1;
1205 }
1206
1207 return 0;
1208 }
1209
1210
1211 /*
1212 * Sockets creation.
1213 */
1214
1215
1216 /*
1217 * IPv4 sockets
1218 */
1219 int bp_set_ttl(int sd, uint8_t value)
1220 {
1221 int ttl = value;
1222
1223 if (setsockopt(sd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)) == -1) {
1224 zlog_warn("%s: setsockopt(IP_TTL, %d): %s", __func__, value,
1225 strerror(errno));
1226 return -1;
1227 }
1228
1229 return 0;
1230 }
1231
1232 int bp_set_tos(int sd, uint8_t value)
1233 {
1234 int tos = value;
1235
1236 if (setsockopt(sd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) == -1) {
1237 zlog_warn("%s: setsockopt(IP_TOS, %d): %s", __func__, value,
1238 strerror(errno));
1239 return -1;
1240 }
1241
1242 return 0;
1243 }
1244
1245 static bool bp_set_reuse_addr(int sd)
1246 {
1247 int one = 1;
1248
1249 if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
1250 zlog_warn("%s: setsockopt(SO_REUSEADDR, %d): %s", __func__, one,
1251 strerror(errno));
1252 return false;
1253 }
1254 return true;
1255 }
1256
1257 static bool bp_set_reuse_port(int sd)
1258 {
1259 int one = 1;
1260
1261 if (setsockopt(sd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)) == -1) {
1262 zlog_warn("%s: setsockopt(SO_REUSEPORT, %d): %s", __func__, one,
1263 strerror(errno));
1264 return false;
1265 }
1266 return true;
1267 }
1268
1269
1270 static void bp_set_ipopts(int sd)
1271 {
1272 int rcvttl = BFD_RCV_TTL_VAL;
1273
1274 if (!bp_set_reuse_addr(sd))
1275 zlog_fatal("set-reuse-addr: failed");
1276
1277 if (!bp_set_reuse_port(sd))
1278 zlog_fatal("set-reuse-port: failed");
1279
1280 if (bp_set_ttl(sd, BFD_TTL_VAL) != 0)
1281 zlog_fatal("set-ipopts: TTL configuration failed");
1282
1283 if (setsockopt(sd, IPPROTO_IP, IP_RECVTTL, &rcvttl, sizeof(rcvttl))
1284 == -1)
1285 zlog_fatal("set-ipopts: setsockopt(IP_RECVTTL, %d): %s", rcvttl,
1286 strerror(errno));
1287
1288 #ifdef BFD_LINUX
1289 int pktinfo = BFD_PKT_INFO_VAL;
1290
1291 /* Figure out address and interface to do the peer matching. */
1292 if (setsockopt(sd, IPPROTO_IP, IP_PKTINFO, &pktinfo, sizeof(pktinfo))
1293 == -1)
1294 zlog_fatal("set-ipopts: setsockopt(IP_PKTINFO, %d): %s",
1295 pktinfo, strerror(errno));
1296 #endif /* BFD_LINUX */
1297 #ifdef BFD_BSD
1298 int yes = 1;
1299
1300 /* Find out our address for peer matching. */
1301 if (setsockopt(sd, IPPROTO_IP, IP_RECVDSTADDR, &yes, sizeof(yes)) == -1)
1302 zlog_fatal("set-ipopts: setsockopt(IP_RECVDSTADDR, %d): %s",
1303 yes, strerror(errno));
1304
1305 /* Find out interface where the packet came in. */
1306 if (setsockopt_ifindex(AF_INET, sd, yes) == -1)
1307 zlog_fatal("set-ipopts: setsockopt_ipv4_ifindex(%d): %s", yes,
1308 strerror(errno));
1309 #endif /* BFD_BSD */
1310 }
1311
1312 static void bp_bind_ip(int sd, uint16_t port)
1313 {
1314 struct sockaddr_in sin;
1315
1316 memset(&sin, 0, sizeof(sin));
1317 sin.sin_family = AF_INET;
1318 sin.sin_addr.s_addr = htonl(INADDR_ANY);
1319 sin.sin_port = htons(port);
1320 if (bind(sd, (struct sockaddr *)&sin, sizeof(sin)) == -1)
1321 zlog_fatal("bind-ip: bind: %s", strerror(errno));
1322 }
1323
1324 int bp_udp_shop(const struct vrf *vrf)
1325 {
1326 int sd;
1327
1328 frr_with_privs(&bglobal.bfdd_privs) {
1329 sd = vrf_socket(AF_INET, SOCK_DGRAM, PF_UNSPEC, vrf->vrf_id,
1330 vrf->name);
1331 }
1332 if (sd == -1)
1333 zlog_fatal("udp-shop: socket: %s", strerror(errno));
1334
1335 bp_set_ipopts(sd);
1336 bp_bind_ip(sd, BFD_DEFDESTPORT);
1337 return sd;
1338 }
1339
1340 int bp_udp_mhop(const struct vrf *vrf)
1341 {
1342 int sd;
1343
1344 frr_with_privs(&bglobal.bfdd_privs) {
1345 sd = vrf_socket(AF_INET, SOCK_DGRAM, PF_UNSPEC, vrf->vrf_id,
1346 vrf->name);
1347 }
1348 if (sd == -1)
1349 zlog_fatal("udp-mhop: socket: %s", strerror(errno));
1350
1351 bp_set_ipopts(sd);
1352 bp_bind_ip(sd, BFD_DEF_MHOP_DEST_PORT);
1353
1354 return sd;
1355 }
1356
1357 int bp_peer_socket(const struct bfd_session *bs)
1358 {
1359 int sd, pcount;
1360 struct sockaddr_in sin;
1361 static int srcPort = BFD_SRCPORTINIT;
1362 const char *device_to_bind = NULL;
1363
1364 if (bs->key.ifname[0])
1365 device_to_bind = (const char *)bs->key.ifname;
1366 else if ((!vrf_is_backend_netns() && bs->vrf->vrf_id != VRF_DEFAULT)
1367 || ((CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH)
1368 && bs->key.vrfname[0])))
1369 device_to_bind = (const char *)bs->key.vrfname;
1370
1371 frr_with_privs(&bglobal.bfdd_privs) {
1372 sd = vrf_socket(AF_INET, SOCK_DGRAM, PF_UNSPEC,
1373 bs->vrf->vrf_id, device_to_bind);
1374 }
1375 if (sd == -1) {
1376 zlog_err("ipv4-new: failed to create socket: %s",
1377 strerror(errno));
1378 return -1;
1379 }
1380
1381 /* Set TTL to 255 for all transmitted packets */
1382 if (bp_set_ttl(sd, BFD_TTL_VAL) != 0) {
1383 close(sd);
1384 return -1;
1385 }
1386
1387 /* Set TOS to CS6 for all transmitted packets */
1388 if (bp_set_tos(sd, BFD_TOS_VAL) != 0) {
1389 close(sd);
1390 return -1;
1391 }
1392
1393 /* Find an available source port in the proper range */
1394 memset(&sin, 0, sizeof(sin));
1395 sin.sin_family = AF_INET;
1396 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1397 sin.sin_len = sizeof(sin);
1398 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
1399 memcpy(&sin.sin_addr, &bs->key.local, sizeof(sin.sin_addr));
1400
1401 pcount = 0;
1402 do {
1403 if ((++pcount) > (BFD_SRCPORTMAX - BFD_SRCPORTINIT)) {
1404 /* Searched all ports, none available */
1405 zlog_err("ipv4-new: failed to bind port: %s",
1406 strerror(errno));
1407 close(sd);
1408 return -1;
1409 }
1410 if (srcPort >= BFD_SRCPORTMAX)
1411 srcPort = BFD_SRCPORTINIT;
1412 sin.sin_port = htons(srcPort++);
1413 } while (bind(sd, (struct sockaddr *)&sin, sizeof(sin)) < 0);
1414
1415 return sd;
1416 }
1417
1418
1419 /*
1420 * IPv6 sockets
1421 */
1422
1423 int bp_peer_socketv6(const struct bfd_session *bs)
1424 {
1425 int sd, pcount;
1426 struct sockaddr_in6 sin6;
1427 static int srcPort = BFD_SRCPORTINIT;
1428 const char *device_to_bind = NULL;
1429
1430 if (bs->key.ifname[0])
1431 device_to_bind = (const char *)bs->key.ifname;
1432 else if ((!vrf_is_backend_netns() && bs->vrf->vrf_id != VRF_DEFAULT)
1433 || ((CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH)
1434 && bs->key.vrfname[0])))
1435 device_to_bind = (const char *)bs->key.vrfname;
1436
1437 frr_with_privs(&bglobal.bfdd_privs) {
1438 sd = vrf_socket(AF_INET6, SOCK_DGRAM, PF_UNSPEC,
1439 bs->vrf->vrf_id, device_to_bind);
1440 }
1441 if (sd == -1) {
1442 zlog_err("ipv6-new: failed to create socket: %s",
1443 strerror(errno));
1444 return -1;
1445 }
1446
1447 /* Set TTL to 255 for all transmitted packets */
1448 if (bp_set_ttlv6(sd, BFD_TTL_VAL) != 0) {
1449 close(sd);
1450 return -1;
1451 }
1452
1453 /* Set TOS to CS6 for all transmitted packets */
1454 if (bp_set_tosv6(sd, BFD_TOS_VAL) != 0) {
1455 close(sd);
1456 return -1;
1457 }
1458
1459 /* Find an available source port in the proper range */
1460 memset(&sin6, 0, sizeof(sin6));
1461 sin6.sin6_family = AF_INET6;
1462 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1463 sin6.sin6_len = sizeof(sin6);
1464 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
1465 memcpy(&sin6.sin6_addr, &bs->key.local, sizeof(sin6.sin6_addr));
1466 if (bs->ifp && IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr))
1467 sin6.sin6_scope_id = bs->ifp->ifindex;
1468
1469 pcount = 0;
1470 do {
1471 if ((++pcount) > (BFD_SRCPORTMAX - BFD_SRCPORTINIT)) {
1472 /* Searched all ports, none available */
1473 zlog_err("ipv6-new: failed to bind port: %s",
1474 strerror(errno));
1475 close(sd);
1476 return -1;
1477 }
1478 if (srcPort >= BFD_SRCPORTMAX)
1479 srcPort = BFD_SRCPORTINIT;
1480 sin6.sin6_port = htons(srcPort++);
1481 } while (bind(sd, (struct sockaddr *)&sin6, sizeof(sin6)) < 0);
1482
1483 return sd;
1484 }
1485
1486 int bp_set_ttlv6(int sd, uint8_t value)
1487 {
1488 int ttl = value;
1489
1490 if (setsockopt(sd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl))
1491 == -1) {
1492 zlog_warn("set-ttlv6: setsockopt(IPV6_UNICAST_HOPS, %d): %s",
1493 value, strerror(errno));
1494 return -1;
1495 }
1496
1497 return 0;
1498 }
1499
1500 int bp_set_tosv6(int sd, uint8_t value)
1501 {
1502 int tos = value;
1503
1504 if (setsockopt(sd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos))
1505 == -1) {
1506 zlog_warn("set-tosv6: setsockopt(IPV6_TCLASS, %d): %s", value,
1507 strerror(errno));
1508 return -1;
1509 }
1510
1511 return 0;
1512 }
1513
1514 static void bp_set_ipv6opts(int sd)
1515 {
1516 int ipv6_pktinfo = BFD_IPV6_PKT_INFO_VAL;
1517 int ipv6_only = BFD_IPV6_ONLY_VAL;
1518
1519 if (!bp_set_reuse_addr(sd))
1520 zlog_fatal("set-reuse-addr: failed");
1521
1522 if (!bp_set_reuse_port(sd))
1523 zlog_fatal("set-reuse-port: failed");
1524
1525 if (bp_set_ttlv6(sd, BFD_TTL_VAL) == -1)
1526 zlog_fatal(
1527 "set-ipv6opts: setsockopt(IPV6_UNICAST_HOPS, %d): %s",
1528 BFD_TTL_VAL, strerror(errno));
1529
1530 if (setsockopt_ipv6_hoplimit(sd, BFD_RCV_TTL_VAL) == -1)
1531 zlog_fatal("set-ipv6opts: setsockopt(IPV6_HOPLIMIT, %d): %s",
1532 BFD_RCV_TTL_VAL, strerror(errno));
1533
1534 if (setsockopt_ipv6_pktinfo(sd, ipv6_pktinfo) == -1)
1535 zlog_fatal("set-ipv6opts: setsockopt(IPV6_PKTINFO, %d): %s",
1536 ipv6_pktinfo, strerror(errno));
1537
1538 if (setsockopt(sd, IPPROTO_IPV6, IPV6_V6ONLY, &ipv6_only,
1539 sizeof(ipv6_only))
1540 == -1)
1541 zlog_fatal("set-ipv6opts: setsockopt(IPV6_V6ONLY, %d): %s",
1542 ipv6_only, strerror(errno));
1543 }
1544
1545 static void bp_bind_ipv6(int sd, uint16_t port)
1546 {
1547 struct sockaddr_in6 sin6;
1548
1549 memset(&sin6, 0, sizeof(sin6));
1550 sin6.sin6_family = AF_INET6;
1551 sin6.sin6_addr = in6addr_any;
1552 sin6.sin6_port = htons(port);
1553 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1554 sin6.sin6_len = sizeof(sin6);
1555 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
1556 if (bind(sd, (struct sockaddr *)&sin6, sizeof(sin6)) == -1)
1557 zlog_fatal("bind-ipv6: bind: %s", strerror(errno));
1558 }
1559
1560 int bp_udp6_shop(const struct vrf *vrf)
1561 {
1562 int sd;
1563
1564 frr_with_privs(&bglobal.bfdd_privs) {
1565 sd = vrf_socket(AF_INET6, SOCK_DGRAM, PF_UNSPEC, vrf->vrf_id,
1566 vrf->name);
1567 }
1568 if (sd == -1) {
1569 if (errno != EAFNOSUPPORT)
1570 zlog_fatal("udp6-shop: socket: %s", strerror(errno));
1571 else
1572 zlog_warn("udp6-shop: V6 is not supported, continuing");
1573
1574 return -1;
1575 }
1576
1577 bp_set_ipv6opts(sd);
1578 bp_bind_ipv6(sd, BFD_DEFDESTPORT);
1579
1580 return sd;
1581 }
1582
1583 int bp_udp6_mhop(const struct vrf *vrf)
1584 {
1585 int sd;
1586
1587 frr_with_privs(&bglobal.bfdd_privs) {
1588 sd = vrf_socket(AF_INET6, SOCK_DGRAM, PF_UNSPEC, vrf->vrf_id,
1589 vrf->name);
1590 }
1591 if (sd == -1) {
1592 if (errno != EAFNOSUPPORT)
1593 zlog_fatal("udp6-mhop: socket: %s", strerror(errno));
1594 else
1595 zlog_warn("udp6-mhop: V6 is not supported, continuing");
1596
1597 return -1;
1598 }
1599
1600 bp_set_ipv6opts(sd);
1601 bp_bind_ipv6(sd, BFD_DEF_MHOP_DEST_PORT);
1602
1603 return sd;
1604 }
1605
1606 #ifdef BFD_LINUX
1607 /* tcpdump -dd udp dst port 3785 */
1608 struct sock_filter my_filterudp[] = {
1609 {0x28, 0, 0, 0x0000000c}, {0x15, 0, 8, 0x00000800},
1610 {0x30, 0, 0, 0x00000017}, {0x15, 0, 6, 0x00000011},
1611 {0x28, 0, 0, 0x00000014}, {0x45, 4, 0, 0x00001fff},
1612 {0xb1, 0, 0, 0x0000000e}, {0x48, 0, 0, 0x00000010},
1613 {0x15, 0, 1, 0x00000ec9}, {0x6, 0, 0, 0x00040000},
1614 {0x6, 0, 0, 0x00000000},
1615 };
1616
1617 #define MY_FILTER_LENGTH 11
1618
1619 int bp_echo_socket(const struct vrf *vrf)
1620 {
1621 int s;
1622
1623 frr_with_privs (&bglobal.bfdd_privs) {
1624 s = vrf_socket(AF_PACKET, SOCK_RAW, ETH_P_IP, vrf->vrf_id,
1625 vrf->name);
1626 }
1627
1628 if (s == -1)
1629 zlog_fatal("echo-socket: socket: %s", strerror(errno));
1630
1631 struct sock_fprog pf;
1632 struct sockaddr_ll sll = {0};
1633
1634 /* adjust filter for socket to only receive ECHO packets */
1635 pf.filter = my_filterudp;
1636 pf.len = MY_FILTER_LENGTH;
1637 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, &pf, sizeof(pf)) ==
1638 -1) {
1639 zlog_warn("%s: setsockopt(SO_ATTACH_FILTER): %s", __func__,
1640 strerror(errno));
1641 close(s);
1642 return -1;
1643 }
1644
1645 memset(&sll, 0, sizeof(sll));
1646 sll.sll_family = AF_PACKET;
1647 sll.sll_protocol = htons(ETH_P_IP);
1648 sll.sll_ifindex = 0;
1649 if (bind(s, (struct sockaddr *)&sll, sizeof(sll)) < 0) {
1650 zlog_warn("Failed to bind echo socket: %s",
1651 safe_strerror(errno));
1652 close(s);
1653 return -1;
1654 }
1655
1656 return s;
1657 }
1658 #else
1659 int bp_echo_socket(const struct vrf *vrf)
1660 {
1661 int s;
1662
1663 frr_with_privs(&bglobal.bfdd_privs) {
1664 s = vrf_socket(AF_INET, SOCK_DGRAM, 0, vrf->vrf_id, vrf->name);
1665 }
1666 if (s == -1)
1667 zlog_fatal("echo-socket: socket: %s", strerror(errno));
1668
1669 bp_set_ipopts(s);
1670 bp_bind_ip(s, BFD_DEF_ECHO_PORT);
1671
1672 return s;
1673 }
1674 #endif
1675
1676 int bp_echov6_socket(const struct vrf *vrf)
1677 {
1678 int s;
1679
1680 frr_with_privs(&bglobal.bfdd_privs) {
1681 s = vrf_socket(AF_INET6, SOCK_DGRAM, 0, vrf->vrf_id, vrf->name);
1682 }
1683 if (s == -1) {
1684 if (errno != EAFNOSUPPORT)
1685 zlog_fatal("echov6-socket: socket: %s",
1686 strerror(errno));
1687 else
1688 zlog_warn("echov6-socket: V6 is not supported, continuing");
1689
1690 return -1;
1691 }
1692
1693 bp_set_ipv6opts(s);
1694 bp_bind_ipv6(s, BFD_DEF_ECHO_PORT);
1695
1696 return s;
1697 }
1698
1699 #ifdef BFD_LINUX
1700 /* get peer's mac address to be used with Echo packets when they are looped in
1701 * peers forwarding plane
1702 */
1703 void bfd_peer_mac_set(int sd, struct bfd_session *bfd,
1704 struct sockaddr_any *peer, struct interface *ifp)
1705 {
1706 struct arpreq arpreq_;
1707
1708 if (CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_MAC_SET))
1709 return;
1710 if (ifp->flags & IFF_NOARP)
1711 return;
1712
1713 if (peer->sa_sin.sin_family == AF_INET) {
1714 /* IPV4 */
1715 struct sockaddr_in *addr =
1716 (struct sockaddr_in *)&arpreq_.arp_pa;
1717
1718 memset(&arpreq_, 0, sizeof(struct arpreq));
1719 addr->sin_family = AF_INET;
1720 memcpy(&addr->sin_addr.s_addr, &peer->sa_sin.sin_addr,
1721 sizeof(addr->sin_addr));
1722 strlcpy(arpreq_.arp_dev, ifp->name, sizeof(arpreq_.arp_dev));
1723
1724 if (ioctl(sd, SIOCGARP, &arpreq_) < 0) {
1725 zlog_warn(
1726 "BFD: getting peer's mac on %s failed error %s",
1727 ifp->name, strerror(errno));
1728 UNSET_FLAG(bfd->flags, BFD_SESS_FLAG_MAC_SET);
1729 memset(bfd->peer_hw_addr, 0, sizeof(bfd->peer_hw_addr));
1730
1731 } else {
1732 memcpy(bfd->peer_hw_addr, arpreq_.arp_ha.sa_data,
1733 sizeof(bfd->peer_hw_addr));
1734 SET_FLAG(bfd->flags, BFD_SESS_FLAG_MAC_SET);
1735 }
1736 }
1737 }
1738 #endif