1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2016 Cumulus Networks, Inc.
9 #include "lib/openbsd-queue.h"
11 enum pim_msdp_peer_state
{
19 /* SA and KA TLVs are processed; rest ignored */
21 PIM_MSDP_V4_SOURCE_ACTIVE
= 1,
22 PIM_MSDP_V4_SOURCE_ACTIVE_REQUEST
,
23 PIM_MSDP_V4_SOURCE_ACTIVE_RESPONSE
,
26 PIM_MSDP_TRACEROUTE_PROGRESS
,
27 PIM_MSDP_TRACEROUTE_REPLY
,
30 /* MSDP error codes */
32 PIM_MSDP_ERR_NONE
= 0,
33 PIM_MSDP_ERR_OOM
= -1,
34 PIM_MSDP_ERR_PEER_EXISTS
= -2,
35 PIM_MSDP_ERR_MAX_MESH_GROUPS
= -3,
36 PIM_MSDP_ERR_NO_PEER
= -4,
37 PIM_MSDP_ERR_MG_MBR_EXISTS
= -5,
38 PIM_MSDP_ERR_NO_MG
= -6,
39 PIM_MSDP_ERR_NO_MG_MBR
= -7,
40 PIM_MSDP_ERR_SIP_EQ_DIP
= -8,
43 #define PIM_MSDP_STATE_STRLEN 16
44 #define PIM_MSDP_UPTIME_STRLEN 80
45 #define PIM_MSDP_TIMER_STRLEN 12
46 #define PIM_MSDP_TCP_PORT 639
47 #define PIM_MSDP_SOCKET_SNDBUF_SIZE 65536
49 enum pim_msdp_sa_flags
{
50 PIM_MSDP_SAF_NONE
= 0,
51 /* There are two cases where we can pickup an active source locally -
52 * 1. We are RP and got a source-register from the FHR
53 * 2. We are RP and FHR and learnt a new directly connected source on a
55 PIM_MSDP_SAF_LOCAL
= (1 << 0),
56 /* We got this in the MSDP SA TLV from a peer (and this passed peer-RPF
58 PIM_MSDP_SAF_PEER
= (1 << 1),
59 PIM_MSDP_SAF_REF
= (PIM_MSDP_SAF_LOCAL
| PIM_MSDP_SAF_PEER
),
60 PIM_MSDP_SAF_STALE
= (1 << 2), /* local entries can get kicked out on
61 * misc pim events such as RP change */
62 PIM_MSDP_SAF_UP_DEL_IN_PROG
= (1 << 3)
66 struct pim_instance
*pim
;
69 char sg_str
[PIM_SG_LEN
];
70 struct in_addr rp
; /* Last RP address associated with this SA */
71 struct in_addr peer
; /* last peer from who we heard this SA */
72 enum pim_msdp_sa_flags flags
;
74 /* rfc-3618 is missing default value for SA-hold-down-Period. pulled
75 * this number from industry-standards */
76 #define PIM_MSDP_SA_HOLD_TIME ((3*60)+30)
77 struct thread
*sa_state_timer
; // 5.6
80 struct pim_upstream
*up
;
83 enum pim_msdp_peer_flags
{
84 PIM_MSDP_PEERF_NONE
= 0,
85 PIM_MSDP_PEERF_LISTENER
= (1 << 0),
86 #define PIM_MSDP_PEER_IS_LISTENER(mp) (mp->flags & PIM_MSDP_PEERF_LISTENER)
87 PIM_MSDP_PEERF_SA_JUST_SENT
= (1 << 1),
88 /** Flag to signalize that peer belongs to a group. */
89 PIM_MSDP_PEERF_IN_GROUP
= (1 << 2),
92 struct pim_msdp_peer
{
93 struct pim_instance
*pim
;
98 char *mesh_group_name
;
99 char key_str
[INET_ADDRSTRLEN
];
102 enum pim_msdp_peer_state state
;
103 enum pim_msdp_peer_flags flags
;
105 /* TCP socket info */
106 union sockunion su_local
;
107 union sockunion su_peer
;
110 /* protocol timers */
111 #define PIM_MSDP_PEER_HOLD_TIME 75
112 struct thread
*hold_timer
; // 5.4
113 #define PIM_MSDP_PEER_KA_TIME 60
114 struct thread
*ka_timer
; // 5.5
115 #define PIM_MSDP_PEER_CONNECT_RETRY_TIME 30
116 struct thread
*cr_timer
; // 5.6
118 /* packet thread and buffers */
119 uint32_t packet_size
;
121 struct stream_fifo
*obuf
;
122 struct thread
*t_read
;
123 struct thread
*t_write
;
126 uint32_t conn_attempts
;
128 uint32_t sa_cnt
; /* number of SAs attributed to this peer */
129 #define PIM_MSDP_PEER_LAST_RESET_STR 20
130 char last_reset
[PIM_MSDP_PEER_LAST_RESET_STR
];
143 struct pim_msdp_mg_mbr
{
144 struct in_addr mbr_ip
;
145 struct pim_msdp_peer
*mp
;
148 /* PIM MSDP mesh-group */
150 char *mesh_group_name
;
151 struct in_addr src_ip
;
153 struct list
*mbr_list
;
155 /** Belongs to PIM instance list. */
156 SLIST_ENTRY(pim_msdp_mg
) mg_entry
;
159 SLIST_HEAD(pim_mesh_group_list
, pim_msdp_mg
);
161 enum pim_msdp_flags
{
163 PIM_MSDPF_ENABLE
= (1 << 0),
164 PIM_MSDPF_LISTENER
= (1 << 1)
167 struct pim_msdp_listener
{
170 struct thread
*thread
;
174 enum pim_msdp_flags flags
;
175 struct thread_master
*master
;
176 struct pim_msdp_listener listener
;
177 uint32_t rejected_accepts
;
180 struct hash
*peer_hash
;
181 struct list
*peer_list
;
183 /* MSDP active-source info */
184 #define PIM_MSDP_SA_ADVERTISMENT_TIME 60
185 struct thread
*sa_adv_timer
; // 5.6
186 struct hash
*sa_hash
;
187 struct list
*sa_list
;
190 /* keep a scratch pad for building SA TLVs */
191 struct stream
*work_obuf
;
193 struct in_addr originator_id
;
195 /** List of mesh groups. */
196 struct pim_mesh_group_list mglist
;
198 /** MSDP global hold time period. */
200 /** MSDP global keep alive period. */
202 /** MSDP global connection retry period. */
203 uint32_t connection_retry
;
206 #define PIM_MSDP_PEER_READ_ON(mp) \
207 thread_add_read(mp->pim->msdp.master, pim_msdp_read, mp, mp->fd, \
210 #define PIM_MSDP_PEER_WRITE_ON(mp) \
211 thread_add_write(mp->pim->msdp.master, pim_msdp_write, mp, mp->fd, \
214 #define PIM_MSDP_PEER_READ_OFF(mp) thread_cancel(&mp->t_read)
215 #define PIM_MSDP_PEER_WRITE_OFF(mp) thread_cancel(&mp->t_write)
218 // struct pim_msdp *msdp;
220 void pim_msdp_init(struct pim_instance
*pim
, struct thread_master
*master
);
221 void pim_msdp_exit(struct pim_instance
*pim
);
222 char *pim_msdp_state_dump(enum pim_msdp_peer_state state
, char *buf
,
224 struct pim_msdp_peer
*pim_msdp_peer_find(struct pim_instance
*pim
,
225 struct in_addr peer_addr
);
226 void pim_msdp_peer_established(struct pim_msdp_peer
*mp
);
227 void pim_msdp_peer_pkt_rxed(struct pim_msdp_peer
*mp
);
228 void pim_msdp_peer_stop_tcp_conn(struct pim_msdp_peer
*mp
, bool chg_state
);
229 void pim_msdp_peer_reset_tcp_conn(struct pim_msdp_peer
*mp
, const char *rc_str
);
230 void pim_msdp_write(struct thread
*thread
);
231 int pim_msdp_config_write(struct pim_instance
*pim
, struct vty
*vty
,
233 bool pim_msdp_peer_config_write(struct vty
*vty
, struct pim_instance
*pim
,
235 void pim_msdp_peer_pkt_txed(struct pim_msdp_peer
*mp
);
236 void pim_msdp_sa_ref(struct pim_instance
*pim
, struct pim_msdp_peer
*mp
,
237 pim_sgaddr
*sg
, struct in_addr rp
);
238 void pim_msdp_sa_local_update(struct pim_upstream
*up
);
239 void pim_msdp_sa_local_del(struct pim_instance
*pim
, pim_sgaddr
*sg
);
240 void pim_msdp_i_am_rp_changed(struct pim_instance
*pim
);
241 bool pim_msdp_peer_rpf_check(struct pim_msdp_peer
*mp
, struct in_addr rp
);
242 void pim_msdp_up_join_state_changed(struct pim_instance
*pim
,
243 struct pim_upstream
*xg_up
);
244 void pim_msdp_up_del(struct pim_instance
*pim
, pim_sgaddr
*sg
);
245 enum pim_msdp_err
pim_msdp_mg_del(struct pim_instance
*pim
,
246 const char *mesh_group_name
);
249 * Allocates a new mesh group data structure under PIM instance.
251 struct pim_msdp_mg
*pim_msdp_mg_new(struct pim_instance
*pim
,
252 const char *mesh_group_name
);
254 * Deallocates mesh group data structure under PIM instance.
256 void pim_msdp_mg_free(struct pim_instance
*pim
, struct pim_msdp_mg
**mgp
);
259 * Change the source address of a mesh group peers. It will do the following:
260 * - Close all peers TCP connections
261 * - Recreate peers data structure
262 * - Start TCP connections with new local address.
264 void pim_msdp_mg_src_add(struct pim_instance
*pim
, struct pim_msdp_mg
*mg
,
268 * Add new peer to mesh group and starts the connection if source address is
271 struct pim_msdp_mg_mbr
*pim_msdp_mg_mbr_add(struct pim_instance
*pim
,
272 struct pim_msdp_mg
*mg
,
276 * Stops the connection and removes the peer data structures.
278 void pim_msdp_mg_mbr_del(struct pim_msdp_mg
*mg
, struct pim_msdp_mg_mbr
*mbr
);
281 * Allocates MSDP peer data structure, adds peer to group name
282 * `mesh_group_name` and starts state machine. If no group name is provided then
283 * the peer will work standalone.
285 * \param pim PIM instance
286 * \param peer_addr peer address
287 * \param local_addr local listening address
288 * \param mesh_group_name mesh group name (or `NULL` for peers without group).
290 struct pim_msdp_peer
*pim_msdp_peer_add(struct pim_instance
*pim
,
291 const struct in_addr
*peer_addr
,
292 const struct in_addr
*local_addr
,
293 const char *mesh_group_name
);
296 * Stops peer state machine and free memory.
298 void pim_msdp_peer_del(struct pim_msdp_peer
**mp
);
301 * Changes peer source address.
304 * This will cause the connection to drop and start again.
306 void pim_msdp_peer_change_source(struct pim_msdp_peer
*mp
,
307 const struct in_addr
*addr
);
309 #else /* PIM_IPV == 6 */
310 static inline void pim_msdp_init(struct pim_instance
*pim
,
311 struct thread_master
*master
)
315 static inline void pim_msdp_exit(struct pim_instance
*pim
)
319 static inline void pim_msdp_i_am_rp_changed(struct pim_instance
*pim
)
323 static inline void pim_msdp_up_join_state_changed(struct pim_instance
*pim
,
324 struct pim_upstream
*xg_up
)
328 static inline void pim_msdp_up_del(struct pim_instance
*pim
, pim_sgaddr
*sg
)
332 static inline void pim_msdp_sa_local_update(struct pim_upstream
*up
)
336 static inline void pim_msdp_sa_local_del(struct pim_instance
*pim
,
341 static inline int pim_msdp_config_write(struct pim_instance
*pim
,
342 struct vty
*vty
, const char *spaces
)
347 static inline bool pim_msdp_peer_config_write(struct vty
*vty
,
348 struct pim_instance
*pim
,
353 #endif /* PIM_IPV == 6 */