summaryrefslogtreecommitdiffstats
path: root/pimd/pim_msdp.h
blob: 4e1114aff2870d064dbca5183203e84ee6760cea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
 * IP MSDP for Quagga
 * Copyright (C) 2016 Cumulus Networks, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; see the file COPYING; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */
#ifndef PIM_MSDP_H
#define PIM_MSDP_H

#include "lib/openbsd-queue.h"

enum pim_msdp_peer_state {
	PIM_MSDP_DISABLED,
	PIM_MSDP_INACTIVE,
	PIM_MSDP_LISTEN,
	PIM_MSDP_CONNECTING,
	PIM_MSDP_ESTABLISHED
};

/* SA and KA TLVs are processed; rest ignored */
enum pim_msdp_tlv {
	PIM_MSDP_V4_SOURCE_ACTIVE = 1,
	PIM_MSDP_V4_SOURCE_ACTIVE_REQUEST,
	PIM_MSDP_V4_SOURCE_ACTIVE_RESPONSE,
	PIM_MSDP_KEEPALIVE,
	PIM_MSDP_RESERVED,
	PIM_MSDP_TRACEROUTE_PROGRESS,
	PIM_MSDP_TRACEROUTE_REPLY,
};

/* MSDP error codes */
enum pim_msdp_err {
	PIM_MSDP_ERR_NONE = 0,
	PIM_MSDP_ERR_OOM = -1,
	PIM_MSDP_ERR_PEER_EXISTS = -2,
	PIM_MSDP_ERR_MAX_MESH_GROUPS = -3,
	PIM_MSDP_ERR_NO_PEER = -4,
	PIM_MSDP_ERR_MG_MBR_EXISTS = -5,
	PIM_MSDP_ERR_NO_MG = -6,
	PIM_MSDP_ERR_NO_MG_MBR = -7,
	PIM_MSDP_ERR_SIP_EQ_DIP = -8,
};

#define PIM_MSDP_STATE_STRLEN 16
#define PIM_MSDP_UPTIME_STRLEN 80
#define PIM_MSDP_TIMER_STRLEN 12
#define PIM_MSDP_TCP_PORT 639
#define PIM_MSDP_SOCKET_SNDBUF_SIZE 65536

enum pim_msdp_sa_flags {
	PIM_MSDP_SAF_NONE = 0,
	/* There are two cases where we can pickup an active source locally -
	 * 1. We are RP and got a source-register from the FHR
	 * 2. We are RP and FHR and learnt a new directly connected source on a
	 * DR interface */
	PIM_MSDP_SAF_LOCAL = (1 << 0),
	/* We got this in the MSDP SA TLV from a peer (and this passed peer-RPF
	 * checks) */
	PIM_MSDP_SAF_PEER = (1 << 1),
	PIM_MSDP_SAF_REF = (PIM_MSDP_SAF_LOCAL | PIM_MSDP_SAF_PEER),
	PIM_MSDP_SAF_STALE = (1 << 2), /* local entries can get kicked out on
					* misc pim events such as RP change */
	PIM_MSDP_SAF_UP_DEL_IN_PROG = (1 << 3)
};

struct pim_msdp_sa {
	struct pim_instance *pim;

	pim_sgaddr sg;
	char sg_str[PIM_SG_LEN];
	struct in_addr rp;   /* Last RP address associated with this SA */
	struct in_addr peer; /* last peer from who we heard this SA */
	enum pim_msdp_sa_flags flags;

/* rfc-3618 is missing default value for SA-hold-down-Period. pulled
 * this number from industry-standards */
#define PIM_MSDP_SA_HOLD_TIME ((3*60)+30)
	struct thread *sa_state_timer; // 5.6
	int64_t uptime;

	struct pim_upstream *up;
};

enum pim_msdp_peer_flags {
	PIM_MSDP_PEERF_NONE = 0,
	PIM_MSDP_PEERF_LISTENER = (1 << 0),
#define PIM_MSDP_PEER_IS_LISTENER(mp) (mp->flags & PIM_MSDP_PEERF_LISTENER)
	PIM_MSDP_PEERF_SA_JUST_SENT = (1 << 1),
	/** Flag to signalize that peer belongs to a group. */
	PIM_MSDP_PEERF_IN_GROUP = (1 << 2),
};

struct pim_msdp_peer {
	struct pim_instance *pim;

	/* configuration */
	struct in_addr local;
	struct in_addr peer;
	char *mesh_group_name;
	char key_str[INET_ADDRSTRLEN];

	/* state */
	enum pim_msdp_peer_state state;
	enum pim_msdp_peer_flags flags;

	/* TCP socket info */
	union sockunion su_local;
	union sockunion su_peer;
	int fd;

/* protocol timers */
#define PIM_MSDP_PEER_HOLD_TIME 75
	struct thread *hold_timer; // 5.4
#define PIM_MSDP_PEER_KA_TIME 60
	struct thread *ka_timer; // 5.5
#define PIM_MSDP_PEER_CONNECT_RETRY_TIME 30
	struct thread *cr_timer; // 5.6

	/* packet thread and buffers */
	uint32_t packet_size;
	struct stream *ibuf;
	struct stream_fifo *obuf;
	struct thread *t_read;
	struct thread *t_write;

	/* stats */
	uint32_t conn_attempts;
	uint32_t est_flaps;
	uint32_t sa_cnt; /* number of SAs attributed to this peer */
#define PIM_MSDP_PEER_LAST_RESET_STR 20
	char last_reset[PIM_MSDP_PEER_LAST_RESET_STR];

	/* packet stats */
	uint32_t ka_tx_cnt;
	uint32_t sa_tx_cnt;
	uint32_t ka_rx_cnt;
	uint32_t sa_rx_cnt;
	uint32_t unk_rx_cnt;

	/* timestamps */
	int64_t uptime;
};

struct pim_msdp_mg_mbr {
	struct in_addr mbr_ip;
	struct pim_msdp_peer *mp;
};

/* PIM MSDP mesh-group */
struct pim_msdp_mg {
	char *mesh_group_name;
	struct in_addr src_ip;
	uint32_t mbr_cnt;
	struct list *mbr_list;

	/** Belongs to PIM instance list. */
	SLIST_ENTRY(pim_msdp_mg) mg_entry;
};

SLIST_HEAD(pim_mesh_group_list, pim_msdp_mg);

enum pim_msdp_flags {
	PIM_MSDPF_NONE = 0,
	PIM_MSDPF_ENABLE = (1 << 0),
	PIM_MSDPF_LISTENER = (1 << 1)
};

struct pim_msdp_listener {
	int fd;
	union sockunion su;
	struct thread *thread;
};

struct pim_msdp {
	enum pim_msdp_flags flags;
	struct thread_master *master;
	struct pim_msdp_listener listener;
	uint32_t rejected_accepts;

	/* MSDP peer info */
	struct hash *peer_hash;
	struct list *peer_list;

/* MSDP active-source info */
#define PIM_MSDP_SA_ADVERTISMENT_TIME 60
	struct thread *sa_adv_timer; // 5.6
	struct hash *sa_hash;
	struct list *sa_list;
	uint32_t local_cnt;

	/* keep a scratch pad for building SA TLVs */
	struct stream *work_obuf;

	struct in_addr originator_id;

	/** List of mesh groups. */
	struct pim_mesh_group_list mglist;

	/** MSDP global hold time period. */
	uint32_t hold_time;
	/** MSDP global keep alive period. */
	uint32_t keep_alive;
	/** MSDP global connection retry period. */
	uint32_t connection_retry;
};

#define PIM_MSDP_PEER_READ_ON(mp)                                              \
	thread_add_read(mp->pim->msdp.master, pim_msdp_read, mp, mp->fd,       \
			&mp->t_read)

#define PIM_MSDP_PEER_WRITE_ON(mp)                                             \
	thread_add_write(mp->pim->msdp.master, pim_msdp_write, mp, mp->fd,     \
			 &mp->t_write)

#define PIM_MSDP_PEER_READ_OFF(mp) thread_cancel(&mp->t_read)
#define PIM_MSDP_PEER_WRITE_OFF(mp) thread_cancel(&mp->t_write)

#if PIM_IPV != 6
// struct pim_msdp *msdp;
struct pim_instance;
void pim_msdp_init(struct pim_instance *pim, struct thread_master *master);
void pim_msdp_exit(struct pim_instance *pim);
char *pim_msdp_state_dump(enum pim_msdp_peer_state state, char *buf,
			  int buf_size);
struct pim_msdp_peer *pim_msdp_peer_find(struct pim_instance *pim,
					 struct in_addr peer_addr);
void pim_msdp_peer_established(struct pim_msdp_peer *mp);
void pim_msdp_peer_pkt_rxed(struct pim_msdp_peer *mp);
void pim_msdp_peer_stop_tcp_conn(struct pim_msdp_peer *mp, bool chg_state);
void pim_msdp_peer_reset_tcp_conn(struct pim_msdp_peer *mp, const char *rc_str);
void pim_msdp_write(struct thread *thread);
int pim_msdp_config_write(struct pim_instance *pim, struct vty *vty,
			  const char *spaces);
bool pim_msdp_peer_config_write(struct vty *vty, struct pim_instance *pim,
				const char *spaces);
void pim_msdp_peer_pkt_txed(struct pim_msdp_peer *mp);
void pim_msdp_sa_ref(struct pim_instance *pim, struct pim_msdp_peer *mp,
		     pim_sgaddr *sg, struct in_addr rp);
void pim_msdp_sa_local_update(struct pim_upstream *up);
void pim_msdp_sa_local_del(struct pim_instance *pim, pim_sgaddr *sg);
void pim_msdp_i_am_rp_changed(struct pim_instance *pim);
bool pim_msdp_peer_rpf_check(struct pim_msdp_peer *mp, struct in_addr rp);
void pim_msdp_up_join_state_changed(struct pim_instance *pim,
				    struct pim_upstream *xg_up);
void pim_msdp_up_del(struct pim_instance *pim, pim_sgaddr *sg);
enum pim_msdp_err pim_msdp_mg_del(struct pim_instance *pim,
				  const char *mesh_group_name);

/**
 * Allocates a new mesh group data structure under PIM instance.
 */
struct pim_msdp_mg *pim_msdp_mg_new(struct pim_instance *pim,
				    const char *mesh_group_name);
/**
 * Deallocates mesh group data structure under PIM instance.
 */
void pim_msdp_mg_free(struct pim_instance *pim, struct pim_msdp_mg **mgp);

/**
 * Change the source address of a mesh group peers. It will do the following:
 * - Close all peers TCP connections
 * - Recreate peers data structure
 * - Start TCP connections with new local address.
 */
void pim_msdp_mg_src_add(struct pim_instance *pim, struct pim_msdp_mg *mg,
			 struct in_addr *ai);

/**
 * Add new peer to mesh group and starts the connection if source address is
 * configured.
 */
struct pim_msdp_mg_mbr *pim_msdp_mg_mbr_add(struct pim_instance *pim,
					    struct pim_msdp_mg *mg,
					    struct in_addr *ia);

/**
 * Stops the connection and removes the peer data structures.
 */
void pim_msdp_mg_mbr_del(struct pim_msdp_mg *mg, struct pim_msdp_mg_mbr *mbr);

/**
 * Allocates MSDP peer data structure, adds peer to group name
 * `mesh_group_name` and starts state machine. If no group name is provided then
 * the peer will work standalone.
 *
 * \param pim PIM instance
 * \param peer_addr peer address
 * \param local_addr local listening address
 * \param mesh_group_name mesh group name (or `NULL` for peers without group).
 */
struct pim_msdp_peer *pim_msdp_peer_add(struct pim_instance *pim,
					const struct in_addr *peer_addr,
					const struct in_addr *local_addr,
					const char *mesh_group_name);

/**
 * Stops peer state machine and free memory.
 */
void pim_msdp_peer_del(struct pim_msdp_peer **mp);

/**
 * Changes peer source address.
 *
 * NOTE:
 * This will cause the connection to drop and start again.
 */
void pim_msdp_peer_change_source(struct pim_msdp_peer *mp,
				 const struct in_addr *addr);

#else /* PIM_IPV == 6 */
static inline void pim_msdp_init(struct pim_instance *pim,
				 struct thread_master *master)
{
}

static inline void pim_msdp_exit(struct pim_instance *pim)
{
}

static inline void pim_msdp_i_am_rp_changed(struct pim_instance *pim)
{
}

static inline void pim_msdp_up_join_state_changed(struct pim_instance *pim,
						  struct pim_upstream *xg_up)
{
}

static inline void pim_msdp_up_del(struct pim_instance *pim, pim_sgaddr *sg)
{
}

static inline void pim_msdp_sa_local_update(struct pim_upstream *up)
{
}

static inline void pim_msdp_sa_local_del(struct pim_instance *pim,
					 pim_sgaddr *sg)
{
}

static inline int pim_msdp_config_write(struct pim_instance *pim,
					struct vty *vty, const char *spaces)
{
	return 0;
}

static inline bool pim_msdp_peer_config_write(struct vty *vty,
					      struct pim_instance *pim,
					      const char *spaces)
{
	return false;
}
#endif /* PIM_IPV == 6 */

#endif