2 * IP MSDP packet helper
3 * Copyright (C) 2016 Cumulus Networks, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <lib/network.h>
23 #include <lib/stream.h>
24 #include <lib/thread.h>
26 #include <lib/lib_errors.h>
32 #include "pim_msdp_packet.h"
33 #include "pim_msdp_socket.h"
35 static char *pim_msdp_pkt_type_dump(enum pim_msdp_tlv type
, char *buf
,
39 case PIM_MSDP_V4_SOURCE_ACTIVE
:
40 snprintf(buf
, buf_size
, "%s", "SA");
42 case PIM_MSDP_V4_SOURCE_ACTIVE_REQUEST
:
43 snprintf(buf
, buf_size
, "%s", "SA_REQ");
45 case PIM_MSDP_V4_SOURCE_ACTIVE_RESPONSE
:
46 snprintf(buf
, buf_size
, "%s", "SA_RESP");
48 case PIM_MSDP_KEEPALIVE
:
49 snprintf(buf
, buf_size
, "%s", "KA");
51 case PIM_MSDP_RESERVED
:
52 snprintf(buf
, buf_size
, "%s", "RSVD");
54 case PIM_MSDP_TRACEROUTE_PROGRESS
:
55 snprintf(buf
, buf_size
, "%s", "TRACE_PROG");
57 case PIM_MSDP_TRACEROUTE_REPLY
:
58 snprintf(buf
, buf_size
, "%s", "TRACE_REPLY");
61 snprintf(buf
, buf_size
, "UNK-%d", type
);
66 static void pim_msdp_pkt_sa_dump_one(struct stream
*s
)
70 /* just throw away the three reserved bytes */
72 /* throw away the prefix length also */
75 memset(&sg
, 0, sizeof(struct prefix_sg
));
76 sg
.grp
.s_addr
= stream_get_ipv4(s
);
77 sg
.src
.s_addr
= stream_get_ipv4(s
);
79 zlog_debug(" sg %s", pim_str_sg_dump(&sg
));
82 static void pim_msdp_pkt_sa_dump(struct stream
*s
)
86 struct in_addr rp
; /* Last RP address associated with this SA */
88 entry_cnt
= stream_getc(s
);
89 rp
.s_addr
= stream_get_ipv4(s
);
91 if (PIM_DEBUG_MSDP_PACKETS
) {
92 char rp_str
[INET_ADDRSTRLEN
];
93 pim_inet4_dump("<rp?>", rp
, rp_str
, sizeof(rp_str
));
94 zlog_debug(" entry_cnt %d rp %s", entry_cnt
, rp_str
);
98 for (i
= 0; i
< entry_cnt
; ++i
) {
99 pim_msdp_pkt_sa_dump_one(s
);
103 static void pim_msdp_pkt_dump(struct pim_msdp_peer
*mp
, int type
, int len
,
104 bool rx
, struct stream
*s
)
106 char type_str
[PIM_MSDP_PKT_TYPE_STRLEN
];
108 pim_msdp_pkt_type_dump(type
, type_str
, sizeof(type_str
));
110 zlog_debug("MSDP peer %s pkt %s type %s len %d", mp
->key_str
,
111 rx
? "rx" : "tx", type_str
, len
);
118 case PIM_MSDP_V4_SOURCE_ACTIVE
:
119 pim_msdp_pkt_sa_dump(s
);
125 /* Check file descriptor whether connect is established. */
126 static void pim_msdp_connect_check(struct pim_msdp_peer
*mp
)
132 if (mp
->state
!= PIM_MSDP_CONNECTING
) {
133 /* if we are here it means we are not in a connecting or
135 * for now treat this as a fatal error */
136 pim_msdp_peer_reset_tcp_conn(mp
, "invalid-state");
140 PIM_MSDP_PEER_READ_OFF(mp
);
141 PIM_MSDP_PEER_WRITE_OFF(mp
);
143 /* Check file descriptor. */
144 slen
= sizeof(status
);
145 ret
= getsockopt(mp
->fd
, SOL_SOCKET
, SO_ERROR
, (void *)&status
, &slen
);
147 /* If getsockopt is fail, this is fatal error. */
149 zlog_ferr(LIB_ERR_SOCKET
,
150 "can't get sockopt for nonblocking connect");
151 pim_msdp_peer_reset_tcp_conn(mp
, "connect-failed");
155 /* When status is 0 then TCP connection is established. */
156 if (PIM_DEBUG_MSDP_INTERNAL
) {
157 zlog_debug("MSDP peer %s pim_connect_check %s", mp
->key_str
,
158 status
? "fail" : "success");
161 pim_msdp_peer_established(mp
);
163 pim_msdp_peer_reset_tcp_conn(mp
, "connect-failed");
167 static void pim_msdp_pkt_delete(struct pim_msdp_peer
*mp
)
169 stream_free(stream_fifo_pop(mp
->obuf
));
172 static void pim_msdp_pkt_add(struct pim_msdp_peer
*mp
, struct stream
*s
)
174 stream_fifo_push(mp
->obuf
, s
);
177 static void pim_msdp_write_proceed_actions(struct pim_msdp_peer
*mp
)
179 if (stream_fifo_head(mp
->obuf
)) {
180 PIM_MSDP_PEER_WRITE_ON(mp
);
184 int pim_msdp_write(struct thread
*thread
)
186 struct pim_msdp_peer
*mp
;
189 enum pim_msdp_tlv type
;
192 int work_max_cnt
= 100;
194 mp
= THREAD_ARG(thread
);
197 if (PIM_DEBUG_MSDP_INTERNAL
) {
198 zlog_debug("MSDP peer %s pim_msdp_write", mp
->key_str
);
204 /* check if TCP connection is established */
205 if (mp
->state
!= PIM_MSDP_ESTABLISHED
) {
206 pim_msdp_connect_check(mp
);
210 s
= stream_fifo_head(mp
->obuf
);
212 pim_msdp_write_proceed_actions(mp
);
216 sockopt_cork(mp
->fd
, 1);
218 /* Nonblocking write until TCP output buffer is full */
222 /* Number of bytes to be sent */
223 writenum
= stream_get_endp(s
) - stream_get_getp(s
);
225 /* Call write() system call */
226 num
= write(mp
->fd
, stream_pnt(s
), writenum
);
228 /* write failed either retry needed or error */
229 if (ERRNO_IO_RETRY(errno
)) {
230 if (PIM_DEBUG_MSDP_INTERNAL
) {
232 "MSDP peer %s pim_msdp_write io retry",
238 pim_msdp_peer_reset_tcp_conn(mp
, "pkt-tx-failed");
242 if (num
!= writenum
) {
244 stream_forward_getp(s
, num
);
245 if (PIM_DEBUG_MSDP_INTERNAL
) {
247 "MSDP peer %s pim_msdp_partial_write",
253 /* Retrieve msdp packet type. */
254 stream_set_getp(s
, 0);
255 type
= stream_getc(s
);
256 len
= stream_getw(s
);
258 case PIM_MSDP_KEEPALIVE
:
261 case PIM_MSDP_V4_SOURCE_ACTIVE
:
266 if (PIM_DEBUG_MSDP_PACKETS
) {
267 pim_msdp_pkt_dump(mp
, type
, len
, false /*rx*/, s
);
270 /* packet sent delete it. */
271 pim_msdp_pkt_delete(mp
);
274 /* may need to pause if we have done too much work in this
276 if (work_cnt
>= work_max_cnt
) {
279 } while ((s
= stream_fifo_head(mp
->obuf
)) != NULL
);
280 pim_msdp_write_proceed_actions(mp
);
282 sockopt_cork(mp
->fd
, 0);
284 if (PIM_DEBUG_MSDP_INTERNAL
) {
285 zlog_debug("MSDP peer %s pim_msdp_write wrote %d packets",
286 mp
->key_str
, work_cnt
);
292 static void pim_msdp_pkt_send(struct pim_msdp_peer
*mp
, struct stream
*s
)
294 /* Add packet to the end of list. */
295 pim_msdp_pkt_add(mp
, s
);
297 PIM_MSDP_PEER_WRITE_ON(mp
);
300 void pim_msdp_pkt_ka_tx(struct pim_msdp_peer
*mp
)
304 if (mp
->state
!= PIM_MSDP_ESTABLISHED
) {
305 /* don't tx anything unless a session is established */
308 s
= stream_new(PIM_MSDP_KA_TLV_MAX_SIZE
);
309 stream_putc(s
, PIM_MSDP_KEEPALIVE
);
310 stream_putw(s
, PIM_MSDP_KA_TLV_MAX_SIZE
);
312 pim_msdp_pkt_send(mp
, s
);
315 static void pim_msdp_pkt_sa_push_to_one_peer(struct pim_instance
*pim
,
316 struct pim_msdp_peer
*mp
)
320 if (mp
->state
!= PIM_MSDP_ESTABLISHED
) {
321 /* don't tx anything unless a session is established */
324 s
= stream_dup(pim
->msdp
.work_obuf
);
326 pim_msdp_pkt_send(mp
, s
);
327 mp
->flags
|= PIM_MSDP_PEERF_SA_JUST_SENT
;
331 /* push the stream into the obuf fifo of all the peers */
332 static void pim_msdp_pkt_sa_push(struct pim_instance
*pim
,
333 struct pim_msdp_peer
*mp
)
335 struct listnode
*mpnode
;
338 pim_msdp_pkt_sa_push_to_one_peer(pim
, mp
);
340 for (ALL_LIST_ELEMENTS_RO(pim
->msdp
.peer_list
, mpnode
, mp
)) {
341 if (PIM_DEBUG_MSDP_INTERNAL
) {
342 zlog_debug("MSDP peer %s pim_msdp_pkt_sa_push",
345 pim_msdp_pkt_sa_push_to_one_peer(pim
, mp
);
350 static int pim_msdp_pkt_sa_fill_hdr(struct pim_instance
*pim
, int local_cnt
)
354 stream_reset(pim
->msdp
.work_obuf
);
355 curr_tlv_ecnt
= local_cnt
> PIM_MSDP_SA_MAX_ENTRY_CNT
356 ? PIM_MSDP_SA_MAX_ENTRY_CNT
358 local_cnt
-= curr_tlv_ecnt
;
359 stream_putc(pim
->msdp
.work_obuf
, PIM_MSDP_V4_SOURCE_ACTIVE
);
360 stream_putw(pim
->msdp
.work_obuf
,
361 PIM_MSDP_SA_ENTRY_CNT2SIZE(curr_tlv_ecnt
));
362 stream_putc(pim
->msdp
.work_obuf
, curr_tlv_ecnt
);
363 stream_put_ipv4(pim
->msdp
.work_obuf
, pim
->msdp
.originator_id
.s_addr
);
368 static void pim_msdp_pkt_sa_fill_one(struct pim_msdp_sa
*sa
)
370 stream_put3(sa
->pim
->msdp
.work_obuf
, 0 /* reserved */);
371 stream_putc(sa
->pim
->msdp
.work_obuf
, 32 /* sprefix len */);
372 stream_put_ipv4(sa
->pim
->msdp
.work_obuf
, sa
->sg
.grp
.s_addr
);
373 stream_put_ipv4(sa
->pim
->msdp
.work_obuf
, sa
->sg
.src
.s_addr
);
376 static void pim_msdp_pkt_sa_gen(struct pim_instance
*pim
,
377 struct pim_msdp_peer
*mp
)
379 struct listnode
*sanode
;
380 struct pim_msdp_sa
*sa
;
382 int local_cnt
= pim
->msdp
.local_cnt
;
385 if (PIM_DEBUG_MSDP_INTERNAL
) {
386 zlog_debug(" sa gen %d", local_cnt
);
389 local_cnt
= pim_msdp_pkt_sa_fill_hdr(pim
, local_cnt
);
391 for (ALL_LIST_ELEMENTS_RO(pim
->msdp
.sa_list
, sanode
, sa
)) {
392 if (!(sa
->flags
& PIM_MSDP_SAF_LOCAL
)) {
393 /* current implementation of MSDP is for anycast i.e.
395 * no re-forwarding of SAs that we learnt from other
399 /* add sa into scratch pad */
400 pim_msdp_pkt_sa_fill_one(sa
);
402 if (sa_count
>= PIM_MSDP_SA_MAX_ENTRY_CNT
) {
403 pim_msdp_pkt_sa_push(pim
, mp
);
406 if (PIM_DEBUG_MSDP_INTERNAL
) {
407 zlog_debug(" sa gen for remainder %d",
410 local_cnt
= pim_msdp_pkt_sa_fill_hdr(pim
, local_cnt
);
415 pim_msdp_pkt_sa_push(pim
, mp
);
420 static void pim_msdp_pkt_sa_tx_done(struct pim_instance
*pim
)
422 struct listnode
*mpnode
;
423 struct pim_msdp_peer
*mp
;
425 /* if SA were sent to the peers we restart ka timer and avoid
426 * unnecessary ka noise */
427 for (ALL_LIST_ELEMENTS_RO(pim
->msdp
.peer_list
, mpnode
, mp
)) {
428 if (mp
->flags
& PIM_MSDP_PEERF_SA_JUST_SENT
) {
429 mp
->flags
&= ~PIM_MSDP_PEERF_SA_JUST_SENT
;
430 pim_msdp_peer_pkt_txed(mp
);
435 void pim_msdp_pkt_sa_tx(struct pim_instance
*pim
)
437 pim_msdp_pkt_sa_gen(pim
, NULL
/* mp */);
438 pim_msdp_pkt_sa_tx_done(pim
);
441 void pim_msdp_pkt_sa_tx_one(struct pim_msdp_sa
*sa
)
443 pim_msdp_pkt_sa_fill_hdr(sa
->pim
, 1 /* cnt */);
444 pim_msdp_pkt_sa_fill_one(sa
);
445 pim_msdp_pkt_sa_push(sa
->pim
, NULL
);
446 pim_msdp_pkt_sa_tx_done(sa
->pim
);
449 /* when a connection is first established we push all SAs immediately */
450 void pim_msdp_pkt_sa_tx_to_one_peer(struct pim_msdp_peer
*mp
)
452 pim_msdp_pkt_sa_gen(mp
->pim
, mp
);
453 pim_msdp_pkt_sa_tx_done(mp
->pim
);
456 static void pim_msdp_pkt_rxed_with_fatal_error(struct pim_msdp_peer
*mp
)
458 pim_msdp_peer_reset_tcp_conn(mp
, "invalid-pkt-rx");
461 static void pim_msdp_pkt_ka_rx(struct pim_msdp_peer
*mp
, int len
)
464 if (len
!= PIM_MSDP_KA_TLV_MAX_SIZE
) {
465 pim_msdp_pkt_rxed_with_fatal_error(mp
);
468 pim_msdp_peer_pkt_rxed(mp
);
471 static void pim_msdp_pkt_sa_rx_one(struct pim_msdp_peer
*mp
, struct in_addr rp
)
476 /* just throw away the three reserved bytes */
477 stream_get3(mp
->ibuf
);
478 prefix_len
= stream_getc(mp
->ibuf
);
480 memset(&sg
, 0, sizeof(struct prefix_sg
));
481 sg
.grp
.s_addr
= stream_get_ipv4(mp
->ibuf
);
482 sg
.src
.s_addr
= stream_get_ipv4(mp
->ibuf
);
484 if (prefix_len
!= 32) {
485 /* ignore SA update if the prefix length is not 32 */
486 zlog_err("rxed sa update with invalid prefix length %d",
490 if (PIM_DEBUG_MSDP_PACKETS
) {
491 zlog_debug(" sg %s", pim_str_sg_dump(&sg
));
493 pim_msdp_sa_ref(mp
->pim
, mp
, &sg
, rp
);
496 static void pim_msdp_pkt_sa_rx(struct pim_msdp_peer
*mp
, int len
)
500 struct in_addr rp
; /* Last RP address associated with this SA */
504 if (len
< PIM_MSDP_SA_TLV_MIN_SIZE
) {
505 pim_msdp_pkt_rxed_with_fatal_error(mp
);
509 entry_cnt
= stream_getc(mp
->ibuf
);
510 /* some vendors include the actual multicast data in the tlv (at the
512 * we will ignore such data. in the future we may consider pushing it
515 if (len
< PIM_MSDP_SA_ENTRY_CNT2SIZE(entry_cnt
)) {
516 pim_msdp_pkt_rxed_with_fatal_error(mp
);
519 rp
.s_addr
= stream_get_ipv4(mp
->ibuf
);
521 if (PIM_DEBUG_MSDP_PACKETS
) {
522 char rp_str
[INET_ADDRSTRLEN
];
523 pim_inet4_dump("<rp?>", rp
, rp_str
, sizeof(rp_str
));
524 zlog_debug(" entry_cnt %d rp %s", entry_cnt
, rp_str
);
527 if (!pim_msdp_peer_rpf_check(mp
, rp
)) {
528 /* if peer-RPF check fails don't process the packet any further
530 if (PIM_DEBUG_MSDP_PACKETS
) {
531 zlog_debug(" peer RPF check failed");
536 pim_msdp_peer_pkt_rxed(mp
);
538 /* update SA cache */
539 for (i
= 0; i
< entry_cnt
; ++i
) {
540 pim_msdp_pkt_sa_rx_one(mp
, rp
);
544 static void pim_msdp_pkt_rx(struct pim_msdp_peer
*mp
)
546 enum pim_msdp_tlv type
;
549 /* re-read type and len */
550 type
= stream_getc_from(mp
->ibuf
, 0);
551 len
= stream_getw_from(mp
->ibuf
, 1);
552 if (len
< PIM_MSDP_HEADER_SIZE
) {
553 pim_msdp_pkt_rxed_with_fatal_error(mp
);
557 if (len
> PIM_MSDP_SA_TLV_MAX_SIZE
) {
558 /* if tlv size if greater than max just ignore the tlv */
562 if (PIM_DEBUG_MSDP_PACKETS
) {
563 pim_msdp_pkt_dump(mp
, type
, len
, true /*rx*/, NULL
/*s*/);
567 case PIM_MSDP_KEEPALIVE
:
568 pim_msdp_pkt_ka_rx(mp
, len
);
570 case PIM_MSDP_V4_SOURCE_ACTIVE
:
572 pim_msdp_pkt_sa_rx(mp
, len
);
579 /* pim msdp read utility function. */
580 static int pim_msdp_read_packet(struct pim_msdp_peer
*mp
)
587 old_endp
= stream_get_endp(mp
->ibuf
);
588 readsize
= mp
->packet_size
- old_endp
;
593 /* Read packet from fd */
594 nbytes
= stream_read_try(mp
->ibuf
, mp
->fd
, readsize
);
595 new_endp
= stream_get_endp(mp
->ibuf
);
597 if (PIM_DEBUG_MSDP_INTERNAL
) {
598 zlog_debug("MSDP peer %s read failed %d", mp
->key_str
,
602 if (PIM_DEBUG_MSDP_INTERNAL
) {
604 "MSDP peer %s pim_msdp_read io retry old_end: %d new_end: %d",
605 mp
->key_str
, old_endp
, new_endp
);
607 /* transient error retry */
610 pim_msdp_pkt_rxed_with_fatal_error(mp
);
615 if (PIM_DEBUG_MSDP_INTERNAL
) {
616 zlog_debug("MSDP peer %s read failed %d", mp
->key_str
,
619 pim_msdp_peer_reset_tcp_conn(mp
, "peer-down");
623 /* We read partial packet. */
624 if (stream_get_endp(mp
->ibuf
) != mp
->packet_size
) {
625 if (PIM_DEBUG_MSDP_INTERNAL
) {
627 "MSDP peer %s read partial len %d old_endp %d new_endp %d",
628 mp
->key_str
, mp
->packet_size
, old_endp
,
637 int pim_msdp_read(struct thread
*thread
)
639 struct pim_msdp_peer
*mp
;
643 mp
= THREAD_ARG(thread
);
646 if (PIM_DEBUG_MSDP_INTERNAL
) {
647 zlog_debug("MSDP peer %s pim_msdp_read", mp
->key_str
);
654 /* check if TCP connection is established */
655 if (mp
->state
!= PIM_MSDP_ESTABLISHED
) {
656 pim_msdp_connect_check(mp
);
660 PIM_MSDP_PEER_READ_ON(mp
);
662 if (!mp
->packet_size
) {
663 mp
->packet_size
= PIM_MSDP_HEADER_SIZE
;
666 if (stream_get_endp(mp
->ibuf
) < PIM_MSDP_HEADER_SIZE
) {
667 /* start by reading the TLV header */
668 rc
= pim_msdp_read_packet(mp
);
670 goto pim_msdp_read_end
;
673 /* Find TLV type and len */
674 stream_getc(mp
->ibuf
);
675 len
= stream_getw(mp
->ibuf
);
676 if (len
< PIM_MSDP_HEADER_SIZE
) {
677 pim_msdp_pkt_rxed_with_fatal_error(mp
);
678 goto pim_msdp_read_end
;
680 /* read complete TLV */
681 mp
->packet_size
= len
;
684 rc
= pim_msdp_read_packet(mp
);
686 goto pim_msdp_read_end
;
691 /* reset input buffers and get ready for the next packet */
693 stream_reset(mp
->ibuf
);