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>
31 #include "pim_msdp_packet.h"
32 #include "pim_msdp_socket.h"
34 static char *pim_msdp_pkt_type_dump(enum pim_msdp_tlv type
, char *buf
,
38 case PIM_MSDP_V4_SOURCE_ACTIVE
:
39 snprintf(buf
, buf_size
, "%s", "SA");
41 case PIM_MSDP_V4_SOURCE_ACTIVE_REQUEST
:
42 snprintf(buf
, buf_size
, "%s", "SA_REQ");
44 case PIM_MSDP_V4_SOURCE_ACTIVE_RESPONSE
:
45 snprintf(buf
, buf_size
, "%s", "SA_RESP");
47 case PIM_MSDP_KEEPALIVE
:
48 snprintf(buf
, buf_size
, "%s", "KA");
50 case PIM_MSDP_RESERVED
:
51 snprintf(buf
, buf_size
, "%s", "RSVD");
53 case PIM_MSDP_TRACEROUTE_PROGRESS
:
54 snprintf(buf
, buf_size
, "%s", "TRACE_PROG");
56 case PIM_MSDP_TRACEROUTE_REPLY
:
57 snprintf(buf
, buf_size
, "%s", "TRACE_REPLY");
60 snprintf(buf
, buf_size
, "UNK-%d", type
);
65 static void pim_msdp_pkt_sa_dump_one(struct stream
*s
)
69 /* just throw away the three reserved bytes */
71 /* throw away the prefix length also */
74 memset(&sg
, 0, sizeof(struct prefix_sg
));
75 sg
.grp
.s_addr
= stream_get_ipv4(s
);
76 sg
.src
.s_addr
= stream_get_ipv4(s
);
78 zlog_debug(" sg %s", pim_str_sg_dump(&sg
));
81 static void pim_msdp_pkt_sa_dump(struct stream
*s
)
85 struct in_addr rp
; /* Last RP address associated with this SA */
87 entry_cnt
= stream_getc(s
);
88 rp
.s_addr
= stream_get_ipv4(s
);
90 if (PIM_DEBUG_MSDP_PACKETS
) {
91 char rp_str
[INET_ADDRSTRLEN
];
92 pim_inet4_dump("<rp?>", rp
, rp_str
, sizeof(rp_str
));
93 zlog_debug(" entry_cnt %d rp %s", entry_cnt
, rp_str
);
97 for (i
= 0; i
< entry_cnt
; ++i
) {
98 pim_msdp_pkt_sa_dump_one(s
);
102 static void pim_msdp_pkt_dump(struct pim_msdp_peer
*mp
, int type
, int len
,
103 bool rx
, struct stream
*s
)
105 char type_str
[PIM_MSDP_PKT_TYPE_STRLEN
];
107 pim_msdp_pkt_type_dump(type
, type_str
, sizeof(type_str
));
109 zlog_debug("MSDP peer %s pkt %s type %s len %d", mp
->key_str
,
110 rx
? "rx" : "tx", type_str
, len
);
117 case PIM_MSDP_V4_SOURCE_ACTIVE
:
118 pim_msdp_pkt_sa_dump(s
);
124 /* Check file descriptor whether connect is established. */
125 static void pim_msdp_connect_check(struct pim_msdp_peer
*mp
)
131 if (mp
->state
!= PIM_MSDP_CONNECTING
) {
132 /* if we are here it means we are not in a connecting or
134 * for now treat this as a fatal error */
135 pim_msdp_peer_reset_tcp_conn(mp
, "invalid-state");
139 PIM_MSDP_PEER_READ_OFF(mp
);
140 PIM_MSDP_PEER_WRITE_OFF(mp
);
142 /* Check file descriptor. */
143 slen
= sizeof(status
);
144 ret
= getsockopt(mp
->fd
, SOL_SOCKET
, SO_ERROR
, (void *)&status
, &slen
);
146 /* If getsockopt is fail, this is fatal error. */
148 zlog_err("can't get sockopt for nonblocking connect");
149 pim_msdp_peer_reset_tcp_conn(mp
, "connect-failed");
153 /* When status is 0 then TCP connection is established. */
154 if (PIM_DEBUG_MSDP_INTERNAL
) {
155 zlog_debug("MSDP peer %s pim_connect_check %s", mp
->key_str
,
156 status
? "fail" : "success");
159 pim_msdp_peer_established(mp
);
161 pim_msdp_peer_reset_tcp_conn(mp
, "connect-failed");
165 static void pim_msdp_pkt_delete(struct pim_msdp_peer
*mp
)
167 stream_free(stream_fifo_pop(mp
->obuf
));
170 static void pim_msdp_pkt_add(struct pim_msdp_peer
*mp
, struct stream
*s
)
172 stream_fifo_push(mp
->obuf
, s
);
175 static void pim_msdp_write_proceed_actions(struct pim_msdp_peer
*mp
)
177 if (stream_fifo_head(mp
->obuf
)) {
178 PIM_MSDP_PEER_WRITE_ON(mp
);
182 int pim_msdp_write(struct thread
*thread
)
184 struct pim_msdp_peer
*mp
;
187 enum pim_msdp_tlv type
;
190 int work_max_cnt
= 100;
192 mp
= THREAD_ARG(thread
);
195 if (PIM_DEBUG_MSDP_INTERNAL
) {
196 zlog_debug("MSDP peer %s pim_msdp_write", mp
->key_str
);
202 /* check if TCP connection is established */
203 if (mp
->state
!= PIM_MSDP_ESTABLISHED
) {
204 pim_msdp_connect_check(mp
);
208 s
= stream_fifo_head(mp
->obuf
);
210 pim_msdp_write_proceed_actions(mp
);
214 sockopt_cork(mp
->fd
, 1);
216 /* Nonblocking write until TCP output buffer is full */
220 /* Number of bytes to be sent */
221 writenum
= stream_get_endp(s
) - stream_get_getp(s
);
223 /* Call write() system call */
224 num
= write(mp
->fd
, STREAM_PNT(s
), writenum
);
226 /* write failed either retry needed or error */
227 if (ERRNO_IO_RETRY(errno
)) {
228 if (PIM_DEBUG_MSDP_INTERNAL
) {
230 "MSDP peer %s pim_msdp_write io retry",
236 pim_msdp_peer_reset_tcp_conn(mp
, "pkt-tx-failed");
240 if (num
!= writenum
) {
242 stream_forward_getp(s
, num
);
243 if (PIM_DEBUG_MSDP_INTERNAL
) {
245 "MSDP peer %s pim_msdp_partial_write",
251 /* Retrieve msdp packet type. */
252 stream_set_getp(s
, 0);
253 type
= stream_getc(s
);
254 len
= stream_getw(s
);
256 case PIM_MSDP_KEEPALIVE
:
259 case PIM_MSDP_V4_SOURCE_ACTIVE
:
264 if (PIM_DEBUG_MSDP_PACKETS
) {
265 pim_msdp_pkt_dump(mp
, type
, len
, false /*rx*/, s
);
268 /* packet sent delete it. */
269 pim_msdp_pkt_delete(mp
);
272 /* may need to pause if we have done too much work in this
274 if (work_cnt
>= work_max_cnt
) {
277 } while ((s
= stream_fifo_head(mp
->obuf
)) != NULL
);
278 pim_msdp_write_proceed_actions(mp
);
280 sockopt_cork(mp
->fd
, 0);
282 if (PIM_DEBUG_MSDP_INTERNAL
) {
283 zlog_debug("MSDP peer %s pim_msdp_write wrote %d packets",
284 mp
->key_str
, work_cnt
);
290 static void pim_msdp_pkt_send(struct pim_msdp_peer
*mp
, struct stream
*s
)
292 /* Add packet to the end of list. */
293 pim_msdp_pkt_add(mp
, s
);
295 PIM_MSDP_PEER_WRITE_ON(mp
);
298 void pim_msdp_pkt_ka_tx(struct pim_msdp_peer
*mp
)
302 if (mp
->state
!= PIM_MSDP_ESTABLISHED
) {
303 /* don't tx anything unless a session is established */
306 s
= stream_new(PIM_MSDP_KA_TLV_MAX_SIZE
);
307 stream_putc(s
, PIM_MSDP_KEEPALIVE
);
308 stream_putw(s
, PIM_MSDP_KA_TLV_MAX_SIZE
);
310 pim_msdp_pkt_send(mp
, s
);
313 static void pim_msdp_pkt_sa_push_to_one_peer(struct pim_instance
*pim
,
314 struct pim_msdp_peer
*mp
)
318 if (mp
->state
!= PIM_MSDP_ESTABLISHED
) {
319 /* don't tx anything unless a session is established */
322 s
= stream_dup(pim
->msdp
.work_obuf
);
324 pim_msdp_pkt_send(mp
, s
);
325 mp
->flags
|= PIM_MSDP_PEERF_SA_JUST_SENT
;
329 /* push the stream into the obuf fifo of all the peers */
330 static void pim_msdp_pkt_sa_push(struct pim_instance
*pim
,
331 struct pim_msdp_peer
*mp
)
333 struct listnode
*mpnode
;
336 pim_msdp_pkt_sa_push_to_one_peer(pim
, mp
);
338 for (ALL_LIST_ELEMENTS_RO(pim
->msdp
.peer_list
, mpnode
, mp
)) {
339 if (PIM_DEBUG_MSDP_INTERNAL
) {
340 zlog_debug("MSDP peer %s pim_msdp_pkt_sa_push",
343 pim_msdp_pkt_sa_push_to_one_peer(pim
, mp
);
348 static int pim_msdp_pkt_sa_fill_hdr(struct pim_instance
*pim
, int local_cnt
)
352 stream_reset(pim
->msdp
.work_obuf
);
353 curr_tlv_ecnt
= local_cnt
> PIM_MSDP_SA_MAX_ENTRY_CNT
354 ? PIM_MSDP_SA_MAX_ENTRY_CNT
356 local_cnt
-= curr_tlv_ecnt
;
357 stream_putc(pim
->msdp
.work_obuf
, PIM_MSDP_V4_SOURCE_ACTIVE
);
358 stream_putw(pim
->msdp
.work_obuf
,
359 PIM_MSDP_SA_ENTRY_CNT2SIZE(curr_tlv_ecnt
));
360 stream_putc(pim
->msdp
.work_obuf
, curr_tlv_ecnt
);
361 stream_put_ipv4(pim
->msdp
.work_obuf
, pim
->msdp
.originator_id
.s_addr
);
366 static void pim_msdp_pkt_sa_fill_one(struct pim_msdp_sa
*sa
)
368 stream_put3(sa
->pim
->msdp
.work_obuf
, 0 /* reserved */);
369 stream_putc(sa
->pim
->msdp
.work_obuf
, 32 /* sprefix len */);
370 stream_put_ipv4(sa
->pim
->msdp
.work_obuf
, sa
->sg
.grp
.s_addr
);
371 stream_put_ipv4(sa
->pim
->msdp
.work_obuf
, sa
->sg
.src
.s_addr
);
374 static void pim_msdp_pkt_sa_gen(struct pim_instance
*pim
,
375 struct pim_msdp_peer
*mp
)
377 struct listnode
*sanode
;
378 struct pim_msdp_sa
*sa
;
380 int local_cnt
= pim
->msdp
.local_cnt
;
383 if (PIM_DEBUG_MSDP_INTERNAL
) {
384 zlog_debug(" sa gen %d", local_cnt
);
387 local_cnt
= pim_msdp_pkt_sa_fill_hdr(pim
, local_cnt
);
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(pim
, local_cnt
);
413 pim_msdp_pkt_sa_push(pim
, mp
);
418 static void pim_msdp_pkt_sa_tx_done(struct pim_instance
*pim
)
420 struct listnode
*mpnode
;
421 struct pim_msdp_peer
*mp
;
423 /* if SA were sent to the peers we restart ka timer and avoid
424 * unnecessary ka noise */
425 for (ALL_LIST_ELEMENTS_RO(pim
->msdp
.peer_list
, mpnode
, mp
)) {
426 if (mp
->flags
& PIM_MSDP_PEERF_SA_JUST_SENT
) {
427 mp
->flags
&= ~PIM_MSDP_PEERF_SA_JUST_SENT
;
428 pim_msdp_peer_pkt_txed(mp
);
433 void pim_msdp_pkt_sa_tx(struct pim_instance
*pim
)
435 pim_msdp_pkt_sa_gen(pim
, NULL
/* mp */);
436 pim_msdp_pkt_sa_tx_done(pim
);
439 void pim_msdp_pkt_sa_tx_one(struct pim_msdp_sa
*sa
)
441 pim_msdp_pkt_sa_fill_hdr(sa
->pim
, 1 /* cnt */);
442 pim_msdp_pkt_sa_fill_one(sa
);
443 pim_msdp_pkt_sa_push(sa
->pim
, NULL
);
444 pim_msdp_pkt_sa_tx_done(sa
->pim
);
447 /* when a connection is first established we push all SAs immediately */
448 void pim_msdp_pkt_sa_tx_to_one_peer(struct pim_msdp_peer
*mp
)
450 pim_msdp_pkt_sa_gen(mp
->pim
, mp
);
451 pim_msdp_pkt_sa_tx_done(mp
->pim
);
454 static void pim_msdp_pkt_rxed_with_fatal_error(struct pim_msdp_peer
*mp
)
456 pim_msdp_peer_reset_tcp_conn(mp
, "invalid-pkt-rx");
459 static void pim_msdp_pkt_ka_rx(struct pim_msdp_peer
*mp
, int len
)
462 if (len
!= PIM_MSDP_KA_TLV_MAX_SIZE
) {
463 pim_msdp_pkt_rxed_with_fatal_error(mp
);
466 pim_msdp_peer_pkt_rxed(mp
);
469 static void pim_msdp_pkt_sa_rx_one(struct pim_msdp_peer
*mp
, struct in_addr rp
)
474 /* just throw away the three reserved bytes */
475 stream_get3(mp
->ibuf
);
476 prefix_len
= stream_getc(mp
->ibuf
);
478 memset(&sg
, 0, sizeof(struct prefix_sg
));
479 sg
.grp
.s_addr
= stream_get_ipv4(mp
->ibuf
);
480 sg
.src
.s_addr
= stream_get_ipv4(mp
->ibuf
);
482 if (prefix_len
!= 32) {
483 /* ignore SA update if the prefix length is not 32 */
484 zlog_err("rxed sa update with invalid prefix length %d",
488 if (PIM_DEBUG_MSDP_PACKETS
) {
489 zlog_debug(" sg %s", pim_str_sg_dump(&sg
));
491 pim_msdp_sa_ref(mp
->pim
, mp
, &sg
, rp
);
494 static void pim_msdp_pkt_sa_rx(struct pim_msdp_peer
*mp
, int len
)
498 struct in_addr rp
; /* Last RP address associated with this SA */
502 if (len
< PIM_MSDP_SA_TLV_MIN_SIZE
) {
503 pim_msdp_pkt_rxed_with_fatal_error(mp
);
507 entry_cnt
= stream_getc(mp
->ibuf
);
508 /* some vendors include the actual multicast data in the tlv (at the
510 * we will ignore such data. in the future we may consider pushing it
513 if (len
< PIM_MSDP_SA_ENTRY_CNT2SIZE(entry_cnt
)) {
514 pim_msdp_pkt_rxed_with_fatal_error(mp
);
517 rp
.s_addr
= stream_get_ipv4(mp
->ibuf
);
519 if (PIM_DEBUG_MSDP_PACKETS
) {
520 char rp_str
[INET_ADDRSTRLEN
];
521 pim_inet4_dump("<rp?>", rp
, rp_str
, sizeof(rp_str
));
522 zlog_debug(" entry_cnt %d rp %s", entry_cnt
, rp_str
);
525 if (!pim_msdp_peer_rpf_check(mp
, rp
)) {
526 /* if peer-RPF check fails don't process the packet any further
528 if (PIM_DEBUG_MSDP_PACKETS
) {
529 zlog_debug(" peer RPF check failed");
534 pim_msdp_peer_pkt_rxed(mp
);
536 /* update SA cache */
537 for (i
= 0; i
< entry_cnt
; ++i
) {
538 pim_msdp_pkt_sa_rx_one(mp
, rp
);
542 static void pim_msdp_pkt_rx(struct pim_msdp_peer
*mp
)
544 enum pim_msdp_tlv type
;
547 /* re-read type and len */
548 type
= stream_getc_from(mp
->ibuf
, 0);
549 len
= stream_getw_from(mp
->ibuf
, 1);
550 if (len
< PIM_MSDP_HEADER_SIZE
) {
551 pim_msdp_pkt_rxed_with_fatal_error(mp
);
555 if (len
> PIM_MSDP_SA_TLV_MAX_SIZE
) {
556 /* if tlv size if greater than max just ignore the tlv */
560 if (PIM_DEBUG_MSDP_PACKETS
) {
561 pim_msdp_pkt_dump(mp
, type
, len
, true /*rx*/, NULL
/*s*/);
565 case PIM_MSDP_KEEPALIVE
:
566 pim_msdp_pkt_ka_rx(mp
, len
);
568 case PIM_MSDP_V4_SOURCE_ACTIVE
:
570 pim_msdp_pkt_sa_rx(mp
, len
);
577 /* pim msdp read utility function. */
578 static int pim_msdp_read_packet(struct pim_msdp_peer
*mp
)
585 old_endp
= stream_get_endp(mp
->ibuf
);
586 readsize
= mp
->packet_size
- old_endp
;
591 /* Read packet from fd */
592 nbytes
= stream_read_try(mp
->ibuf
, mp
->fd
, readsize
);
593 new_endp
= stream_get_endp(mp
->ibuf
);
595 if (PIM_DEBUG_MSDP_INTERNAL
) {
596 zlog_debug("MSDP peer %s read failed %d", mp
->key_str
,
600 if (PIM_DEBUG_MSDP_INTERNAL
) {
602 "MSDP peer %s pim_msdp_read io retry old_end: %d new_end: %d",
603 mp
->key_str
, old_endp
, new_endp
);
605 /* transient error retry */
608 pim_msdp_pkt_rxed_with_fatal_error(mp
);
613 if (PIM_DEBUG_MSDP_INTERNAL
) {
614 zlog_debug("MSDP peer %s read failed %d", mp
->key_str
,
617 pim_msdp_peer_reset_tcp_conn(mp
, "peer-down");
621 /* We read partial packet. */
622 if (stream_get_endp(mp
->ibuf
) != mp
->packet_size
) {
623 if (PIM_DEBUG_MSDP_INTERNAL
) {
625 "MSDP peer %s read partial len %d old_endp %d new_endp %d",
626 mp
->key_str
, mp
->packet_size
, old_endp
,
635 int pim_msdp_read(struct thread
*thread
)
637 struct pim_msdp_peer
*mp
;
641 mp
= THREAD_ARG(thread
);
644 if (PIM_DEBUG_MSDP_INTERNAL
) {
645 zlog_debug("MSDP peer %s pim_msdp_read", mp
->key_str
);
652 /* check if TCP connection is established */
653 if (mp
->state
!= PIM_MSDP_ESTABLISHED
) {
654 pim_msdp_connect_check(mp
);
658 PIM_MSDP_PEER_READ_ON(mp
);
660 if (!mp
->packet_size
) {
661 mp
->packet_size
= PIM_MSDP_HEADER_SIZE
;
664 if (stream_get_endp(mp
->ibuf
) < PIM_MSDP_HEADER_SIZE
) {
665 /* start by reading the TLV header */
666 rc
= pim_msdp_read_packet(mp
);
668 goto pim_msdp_read_end
;
671 /* Find TLV type and len */
672 stream_getc(mp
->ibuf
);
673 len
= stream_getw(mp
->ibuf
);
674 if (len
< PIM_MSDP_HEADER_SIZE
) {
675 pim_msdp_pkt_rxed_with_fatal_error(mp
);
676 goto pim_msdp_read_end
;
678 /* read complete TLV */
679 mp
->packet_size
= len
;
682 rc
= pim_msdp_read_packet(mp
);
684 goto pim_msdp_read_end
;
689 /* reset input buffers and get ready for the next packet */
691 stream_reset(mp
->ibuf
);