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>
29 #include "pim_instance.h"
31 #include "pim_errors.h"
34 #include "pim_msdp_packet.h"
35 #include "pim_msdp_socket.h"
37 static char *pim_msdp_pkt_type_dump(enum pim_msdp_tlv type
, char *buf
,
41 case PIM_MSDP_V4_SOURCE_ACTIVE
:
42 snprintf(buf
, buf_size
, "%s", "SA");
44 case PIM_MSDP_V4_SOURCE_ACTIVE_REQUEST
:
45 snprintf(buf
, buf_size
, "%s", "SA_REQ");
47 case PIM_MSDP_V4_SOURCE_ACTIVE_RESPONSE
:
48 snprintf(buf
, buf_size
, "%s", "SA_RESP");
50 case PIM_MSDP_KEEPALIVE
:
51 snprintf(buf
, buf_size
, "%s", "KA");
53 case PIM_MSDP_RESERVED
:
54 snprintf(buf
, buf_size
, "%s", "RSVD");
56 case PIM_MSDP_TRACEROUTE_PROGRESS
:
57 snprintf(buf
, buf_size
, "%s", "TRACE_PROG");
59 case PIM_MSDP_TRACEROUTE_REPLY
:
60 snprintf(buf
, buf_size
, "%s", "TRACE_REPLY");
63 snprintf(buf
, buf_size
, "UNK-%d", type
);
68 static void pim_msdp_pkt_sa_dump_one(struct stream
*s
)
72 /* just throw away the three reserved bytes */
74 /* throw away the prefix length also */
77 memset(&sg
, 0, sizeof(sg
));
78 sg
.grp
.s_addr
= stream_get_ipv4(s
);
79 sg
.src
.s_addr
= stream_get_ipv4(s
);
81 zlog_debug(" sg %pSG", &sg
);
84 static void pim_msdp_pkt_sa_dump(struct stream
*s
)
88 struct in_addr rp
; /* Last RP address associated with this SA */
90 entry_cnt
= stream_getc(s
);
91 rp
.s_addr
= stream_get_ipv4(s
);
93 if (PIM_DEBUG_MSDP_PACKETS
) {
94 char rp_str
[INET_ADDRSTRLEN
];
95 pim_inet4_dump("<rp?>", rp
, rp_str
, sizeof(rp_str
));
96 zlog_debug(" entry_cnt %d rp %s", entry_cnt
, rp_str
);
100 for (i
= 0; i
< entry_cnt
; ++i
) {
101 pim_msdp_pkt_sa_dump_one(s
);
105 static void pim_msdp_pkt_dump(struct pim_msdp_peer
*mp
, int type
, int len
,
106 bool rx
, struct stream
*s
)
108 char type_str
[PIM_MSDP_PKT_TYPE_STRLEN
];
110 pim_msdp_pkt_type_dump(type
, type_str
, sizeof(type_str
));
112 zlog_debug("MSDP peer %s pkt %s type %s len %d", mp
->key_str
,
113 rx
? "rx" : "tx", type_str
, len
);
120 case PIM_MSDP_V4_SOURCE_ACTIVE
:
121 pim_msdp_pkt_sa_dump(s
);
127 /* Check file descriptor whether connect is established. */
128 static void pim_msdp_connect_check(struct pim_msdp_peer
*mp
)
134 if (mp
->state
!= PIM_MSDP_CONNECTING
) {
135 /* if we are here it means we are not in a connecting or
137 * for now treat this as a fatal error */
138 pim_msdp_peer_reset_tcp_conn(mp
, "invalid-state");
142 PIM_MSDP_PEER_READ_OFF(mp
);
143 PIM_MSDP_PEER_WRITE_OFF(mp
);
145 /* Check file descriptor. */
146 slen
= sizeof(status
);
147 ret
= getsockopt(mp
->fd
, SOL_SOCKET
, SO_ERROR
, (void *)&status
, &slen
);
149 /* If getsockopt is fail, this is fatal error. */
151 flog_err_sys(EC_LIB_SOCKET
,
152 "can't get sockopt for nonblocking connect");
153 pim_msdp_peer_reset_tcp_conn(mp
, "connect-failed");
157 /* When status is 0 then TCP connection is established. */
158 if (PIM_DEBUG_MSDP_INTERNAL
) {
159 zlog_debug("MSDP peer %s pim_connect_check %s", mp
->key_str
,
160 status
? "fail" : "success");
163 pim_msdp_peer_established(mp
);
165 pim_msdp_peer_reset_tcp_conn(mp
, "connect-failed");
169 static void pim_msdp_pkt_delete(struct pim_msdp_peer
*mp
)
171 stream_free(stream_fifo_pop(mp
->obuf
));
174 static void pim_msdp_pkt_add(struct pim_msdp_peer
*mp
, struct stream
*s
)
176 stream_fifo_push(mp
->obuf
, s
);
179 static void pim_msdp_write_proceed_actions(struct pim_msdp_peer
*mp
)
181 if (stream_fifo_head(mp
->obuf
)) {
182 PIM_MSDP_PEER_WRITE_ON(mp
);
186 void pim_msdp_write(struct thread
*thread
)
188 struct pim_msdp_peer
*mp
;
191 enum pim_msdp_tlv type
;
194 int work_max_cnt
= 100;
196 mp
= THREAD_ARG(thread
);
199 if (PIM_DEBUG_MSDP_INTERNAL
) {
200 zlog_debug("MSDP peer %s pim_msdp_write", mp
->key_str
);
206 /* check if TCP connection is established */
207 if (mp
->state
!= PIM_MSDP_ESTABLISHED
) {
208 pim_msdp_connect_check(mp
);
212 s
= stream_fifo_head(mp
->obuf
);
214 pim_msdp_write_proceed_actions(mp
);
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 if (PIM_DEBUG_MSDP_INTERNAL
) {
283 zlog_debug("MSDP peer %s pim_msdp_write wrote %d packets",
284 mp
->key_str
, work_cnt
);
288 static void pim_msdp_pkt_send(struct pim_msdp_peer
*mp
, struct stream
*s
)
290 /* Add packet to the end of list. */
291 pim_msdp_pkt_add(mp
, s
);
293 PIM_MSDP_PEER_WRITE_ON(mp
);
296 void pim_msdp_pkt_ka_tx(struct pim_msdp_peer
*mp
)
300 if (mp
->state
!= PIM_MSDP_ESTABLISHED
) {
301 /* don't tx anything unless a session is established */
304 s
= stream_new(PIM_MSDP_KA_TLV_MAX_SIZE
);
305 stream_putc(s
, PIM_MSDP_KEEPALIVE
);
306 stream_putw(s
, PIM_MSDP_KA_TLV_MAX_SIZE
);
308 pim_msdp_pkt_send(mp
, s
);
311 static void pim_msdp_pkt_sa_push_to_one_peer(struct pim_instance
*pim
,
312 struct pim_msdp_peer
*mp
)
316 if (mp
->state
!= PIM_MSDP_ESTABLISHED
) {
317 /* don't tx anything unless a session is established */
320 s
= stream_dup(pim
->msdp
.work_obuf
);
322 pim_msdp_pkt_send(mp
, s
);
323 mp
->flags
|= PIM_MSDP_PEERF_SA_JUST_SENT
;
327 /* push the stream into the obuf fifo of all the peers */
328 static void pim_msdp_pkt_sa_push(struct pim_instance
*pim
,
329 struct pim_msdp_peer
*mp
)
331 struct listnode
*mpnode
;
334 pim_msdp_pkt_sa_push_to_one_peer(pim
, mp
);
336 for (ALL_LIST_ELEMENTS_RO(pim
->msdp
.peer_list
, mpnode
, mp
)) {
337 if (PIM_DEBUG_MSDP_INTERNAL
) {
338 zlog_debug("MSDP peer %s pim_msdp_pkt_sa_push",
341 pim_msdp_pkt_sa_push_to_one_peer(pim
, mp
);
346 static int pim_msdp_pkt_sa_fill_hdr(struct pim_instance
*pim
, int local_cnt
,
351 stream_reset(pim
->msdp
.work_obuf
);
352 curr_tlv_ecnt
= local_cnt
> PIM_MSDP_SA_MAX_ENTRY_CNT
353 ? PIM_MSDP_SA_MAX_ENTRY_CNT
355 local_cnt
-= curr_tlv_ecnt
;
356 stream_putc(pim
->msdp
.work_obuf
, PIM_MSDP_V4_SOURCE_ACTIVE
);
357 stream_putw(pim
->msdp
.work_obuf
,
358 PIM_MSDP_SA_ENTRY_CNT2SIZE(curr_tlv_ecnt
));
359 stream_putc(pim
->msdp
.work_obuf
, curr_tlv_ecnt
);
360 stream_put_ipv4(pim
->msdp
.work_obuf
, rp
.s_addr
);
365 static void pim_msdp_pkt_sa_fill_one(struct pim_msdp_sa
*sa
)
367 stream_put3(sa
->pim
->msdp
.work_obuf
, 0 /* reserved */);
368 stream_putc(sa
->pim
->msdp
.work_obuf
, 32 /* sprefix len */);
369 stream_put_ipv4(sa
->pim
->msdp
.work_obuf
, sa
->sg
.grp
.s_addr
);
370 stream_put_ipv4(sa
->pim
->msdp
.work_obuf
, sa
->sg
.src
.s_addr
);
373 static void pim_msdp_pkt_sa_gen(struct pim_instance
*pim
,
374 struct pim_msdp_peer
*mp
)
376 struct listnode
*sanode
;
377 struct pim_msdp_sa
*sa
;
379 int local_cnt
= pim
->msdp
.local_cnt
;
382 if (PIM_DEBUG_MSDP_INTERNAL
) {
383 zlog_debug(" sa gen %d", local_cnt
);
386 local_cnt
= pim_msdp_pkt_sa_fill_hdr(pim
, local_cnt
,
387 pim
->msdp
.originator_id
);
389 for (ALL_LIST_ELEMENTS_RO(pim
->msdp
.sa_list
, sanode
, sa
)) {
390 if (!(sa
->flags
& PIM_MSDP_SAF_LOCAL
)) {
391 /* current implementation of MSDP is for anycast i.e.
393 * no re-forwarding of SAs that we learnt from other
397 /* add sa into scratch pad */
398 pim_msdp_pkt_sa_fill_one(sa
);
400 if (sa_count
>= PIM_MSDP_SA_MAX_ENTRY_CNT
) {
401 pim_msdp_pkt_sa_push(pim
, mp
);
404 if (PIM_DEBUG_MSDP_INTERNAL
) {
405 zlog_debug(" sa gen for remainder %d",
408 local_cnt
= pim_msdp_pkt_sa_fill_hdr(
409 pim
, local_cnt
, pim
->msdp
.originator_id
);
414 pim_msdp_pkt_sa_push(pim
, mp
);
419 static void pim_msdp_pkt_sa_tx_done(struct pim_instance
*pim
)
421 struct listnode
*mpnode
;
422 struct pim_msdp_peer
*mp
;
424 /* if SA were sent to the peers we restart ka timer and avoid
425 * unnecessary ka noise */
426 for (ALL_LIST_ELEMENTS_RO(pim
->msdp
.peer_list
, mpnode
, mp
)) {
427 if (mp
->flags
& PIM_MSDP_PEERF_SA_JUST_SENT
) {
428 mp
->flags
&= ~PIM_MSDP_PEERF_SA_JUST_SENT
;
429 pim_msdp_peer_pkt_txed(mp
);
434 void pim_msdp_pkt_sa_tx(struct pim_instance
*pim
)
436 pim_msdp_pkt_sa_gen(pim
, NULL
/* mp */);
437 pim_msdp_pkt_sa_tx_done(pim
);
440 void pim_msdp_pkt_sa_tx_one(struct pim_msdp_sa
*sa
)
442 pim_msdp_pkt_sa_fill_hdr(sa
->pim
, 1 /* cnt */, sa
->rp
);
443 pim_msdp_pkt_sa_fill_one(sa
);
444 pim_msdp_pkt_sa_push(sa
->pim
, NULL
);
445 pim_msdp_pkt_sa_tx_done(sa
->pim
);
448 /* when a connection is first established we push all SAs immediately */
449 void pim_msdp_pkt_sa_tx_to_one_peer(struct pim_msdp_peer
*mp
)
451 pim_msdp_pkt_sa_gen(mp
->pim
, mp
);
452 pim_msdp_pkt_sa_tx_done(mp
->pim
);
455 void pim_msdp_pkt_sa_tx_one_to_one_peer(struct pim_msdp_peer
*mp
,
456 struct in_addr rp
, pim_sgaddr sg
)
458 struct pim_msdp_sa sa
;
460 /* Fills the SA header. */
461 pim_msdp_pkt_sa_fill_hdr(mp
->pim
, 1, rp
);
463 /* Fills the message contents. */
466 pim_msdp_pkt_sa_fill_one(&sa
);
468 /* Pushes the message. */
469 pim_msdp_pkt_sa_push(sa
.pim
, mp
);
470 pim_msdp_pkt_sa_tx_done(sa
.pim
);
473 static void pim_msdp_pkt_rxed_with_fatal_error(struct pim_msdp_peer
*mp
)
475 pim_msdp_peer_reset_tcp_conn(mp
, "invalid-pkt-rx");
478 static void pim_msdp_pkt_ka_rx(struct pim_msdp_peer
*mp
, int len
)
481 if (len
!= PIM_MSDP_KA_TLV_MAX_SIZE
) {
482 pim_msdp_pkt_rxed_with_fatal_error(mp
);
485 pim_msdp_peer_pkt_rxed(mp
);
488 static void pim_msdp_pkt_sa_rx_one(struct pim_msdp_peer
*mp
, struct in_addr rp
)
492 struct listnode
*peer_node
;
493 struct pim_msdp_peer
*peer
;
495 /* just throw away the three reserved bytes */
496 stream_get3(mp
->ibuf
);
497 prefix_len
= stream_getc(mp
->ibuf
);
499 memset(&sg
, 0, sizeof(sg
));
500 sg
.grp
.s_addr
= stream_get_ipv4(mp
->ibuf
);
501 sg
.src
.s_addr
= stream_get_ipv4(mp
->ibuf
);
503 if (prefix_len
!= IPV4_MAX_BITLEN
) {
504 /* ignore SA update if the prefix length is not 32 */
505 flog_err(EC_PIM_MSDP_PACKET
,
506 "rxed sa update with invalid prefix length %d",
510 if (PIM_DEBUG_MSDP_PACKETS
) {
511 zlog_debug(" sg %pSG", &sg
);
513 pim_msdp_sa_ref(mp
->pim
, mp
, &sg
, rp
);
515 /* Forwards the SA to the peers that are not in the RPF to the RP nor in
516 * the same mesh group as the peer from which we received the message.
517 * If the message group is not set, i.e. "default", then we assume that
518 * the message must be forwarded.*/
519 for (ALL_LIST_ELEMENTS_RO(mp
->pim
->msdp
.peer_list
, peer_node
, peer
)) {
520 /* Not a RPF peer, so skip it. */
521 if (pim_msdp_peer_rpf_check(peer
, rp
))
523 /* Don't forward inside the meshed group. */
524 if ((mp
->flags
& PIM_MSDP_PEERF_IN_GROUP
)
525 && strcmp(mp
->mesh_group_name
, peer
->mesh_group_name
) == 0)
528 pim_msdp_pkt_sa_tx_one_to_one_peer(peer
, rp
, sg
);
532 static void pim_msdp_pkt_sa_rx(struct pim_msdp_peer
*mp
, int len
)
536 struct in_addr rp
; /* Last RP address associated with this SA */
540 if (len
< PIM_MSDP_SA_TLV_MIN_SIZE
) {
541 pim_msdp_pkt_rxed_with_fatal_error(mp
);
545 entry_cnt
= stream_getc(mp
->ibuf
);
546 /* some vendors include the actual multicast data in the tlv (at the
547 * end). we will ignore such data. in the future we may consider pushing
550 if (len
< PIM_MSDP_SA_ENTRY_CNT2SIZE(entry_cnt
)) {
551 pim_msdp_pkt_rxed_with_fatal_error(mp
);
554 rp
.s_addr
= stream_get_ipv4(mp
->ibuf
);
556 if (PIM_DEBUG_MSDP_PACKETS
) {
557 char rp_str
[INET_ADDRSTRLEN
];
558 pim_inet4_dump("<rp?>", rp
, rp_str
, sizeof(rp_str
));
559 zlog_debug(" entry_cnt %d rp %s", entry_cnt
, rp_str
);
562 pim_msdp_peer_pkt_rxed(mp
);
564 if (!pim_msdp_peer_rpf_check(mp
, rp
)) {
565 /* if peer-RPF check fails don't process the packet any further
567 if (PIM_DEBUG_MSDP_PACKETS
) {
568 zlog_debug(" peer RPF check failed");
573 /* update SA cache */
574 for (i
= 0; i
< entry_cnt
; ++i
) {
575 pim_msdp_pkt_sa_rx_one(mp
, rp
);
579 static void pim_msdp_pkt_rx(struct pim_msdp_peer
*mp
)
581 enum pim_msdp_tlv type
;
584 /* re-read type and len */
585 type
= stream_getc_from(mp
->ibuf
, 0);
586 len
= stream_getw_from(mp
->ibuf
, 1);
587 if (len
< PIM_MSDP_HEADER_SIZE
) {
588 pim_msdp_pkt_rxed_with_fatal_error(mp
);
592 if (len
> PIM_MSDP_SA_TLV_MAX_SIZE
) {
593 /* if tlv size if greater than max just ignore the tlv */
597 if (PIM_DEBUG_MSDP_PACKETS
) {
598 pim_msdp_pkt_dump(mp
, type
, len
, true /*rx*/, NULL
/*s*/);
602 case PIM_MSDP_KEEPALIVE
:
603 pim_msdp_pkt_ka_rx(mp
, len
);
605 case PIM_MSDP_V4_SOURCE_ACTIVE
:
607 pim_msdp_pkt_sa_rx(mp
, len
);
614 /* pim msdp read utility function. */
615 static int pim_msdp_read_packet(struct pim_msdp_peer
*mp
)
622 old_endp
= stream_get_endp(mp
->ibuf
);
623 readsize
= mp
->packet_size
- old_endp
;
628 /* Read packet from fd */
629 nbytes
= stream_read_try(mp
->ibuf
, mp
->fd
, readsize
);
630 new_endp
= stream_get_endp(mp
->ibuf
);
632 if (PIM_DEBUG_MSDP_INTERNAL
) {
633 zlog_debug("MSDP peer %s read failed %d", mp
->key_str
,
637 if (PIM_DEBUG_MSDP_INTERNAL
) {
639 "MSDP peer %s pim_msdp_read io retry old_end: %d new_end: %d",
640 mp
->key_str
, old_endp
, new_endp
);
642 /* transient error retry */
645 pim_msdp_pkt_rxed_with_fatal_error(mp
);
650 if (PIM_DEBUG_MSDP_INTERNAL
) {
651 zlog_debug("MSDP peer %s read failed %d", mp
->key_str
,
654 pim_msdp_peer_reset_tcp_conn(mp
, "peer-down");
658 /* We read partial packet. */
659 if (stream_get_endp(mp
->ibuf
) != mp
->packet_size
) {
660 if (PIM_DEBUG_MSDP_INTERNAL
) {
662 "MSDP peer %s read partial len %d old_endp %d new_endp %d",
663 mp
->key_str
, mp
->packet_size
, old_endp
,
672 void pim_msdp_read(struct thread
*thread
)
674 struct pim_msdp_peer
*mp
;
678 mp
= THREAD_ARG(thread
);
681 if (PIM_DEBUG_MSDP_INTERNAL
) {
682 zlog_debug("MSDP peer %s pim_msdp_read", mp
->key_str
);
689 /* check if TCP connection is established */
690 if (mp
->state
!= PIM_MSDP_ESTABLISHED
) {
691 pim_msdp_connect_check(mp
);
695 PIM_MSDP_PEER_READ_ON(mp
);
697 if (!mp
->packet_size
) {
698 mp
->packet_size
= PIM_MSDP_HEADER_SIZE
;
701 if (stream_get_endp(mp
->ibuf
) < PIM_MSDP_HEADER_SIZE
) {
702 /* start by reading the TLV header */
703 rc
= pim_msdp_read_packet(mp
);
707 /* Find TLV type and len */
708 stream_getc(mp
->ibuf
);
709 len
= stream_getw(mp
->ibuf
);
710 if (len
< PIM_MSDP_HEADER_SIZE
) {
711 pim_msdp_pkt_rxed_with_fatal_error(mp
);
714 /* read complete TLV */
715 mp
->packet_size
= len
;
718 rc
= pim_msdp_read_packet(mp
);
724 /* reset input buffers and get ready for the next packet */
726 stream_reset(mp
->ibuf
);