summaryrefslogtreecommitdiffstats
path: root/pimd/pim_hello.c
blob: a0661ef36baf8a5edcc79f7ab0bea58ef39fd7b4 (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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * PIM for Quagga
 * Copyright (C) 2008  Everton da Silva Marques
 */

#include <zebra.h>

#include "log.h"
#include "if.h"

#include "pimd.h"
#include "pim_instance.h"
#include "pim_pim.h"
#include "pim_str.h"
#include "pim_tlv.h"
#include "pim_util.h"
#include "pim_hello.h"
#include "pim_iface.h"
#include "pim_neighbor.h"
#include "pim_upstream.h"
#include "pim_bsm.h"

static void on_trace(const char *label, struct interface *ifp, pim_addr src)
{
	if (PIM_DEBUG_PIM_TRACE)
		zlog_debug("%s: from %pPAs on %s", label, &src, ifp->name);
}

static void tlv_trace_bool(const char *label, const char *tlv_name,
			   const char *ifname, pim_addr src_addr, int isset,
			   int value)
{
	if (isset)
		zlog_debug(
			"%s: PIM hello option from %pPAs on interface %s: %s=%d",
			label, &src_addr, ifname, tlv_name, value);
}

static void tlv_trace_uint16(const char *label, const char *tlv_name,
			     const char *ifname, pim_addr src_addr, int isset,
			     uint16_t value)
{
	if (isset)
		zlog_debug(
			"%s: PIM hello option from %pPAs on interface %s: %s=%u",
			label, &src_addr, ifname, tlv_name, value);
}

static void tlv_trace_uint32(const char *label, const char *tlv_name,
			     const char *ifname, pim_addr src_addr, int isset,
			     uint32_t value)
{
	if (isset)
		zlog_debug(
			"%s: PIM hello option from %pPAs on interface %s: %s=%u",
			label, &src_addr, ifname, tlv_name, value);
}

static void tlv_trace_uint32_hex(const char *label, const char *tlv_name,
				 const char *ifname, pim_addr src_addr,
				 int isset, uint32_t value)
{
	if (isset)
		zlog_debug(
			"%s: PIM hello option from %pPAs on interface %s: %s=%08x",
			label, &src_addr, ifname, tlv_name, value);
}

static void tlv_trace_list(const char *label, const char *tlv_name,
			   const char *ifname, pim_addr src_addr, int isset,
			   struct list *addr_list)
{
	if (isset)
		zlog_debug(
			"%s: PIM hello option from %pPAs on interface %s: %s size=%d list=%p",
			label, &src_addr, ifname, tlv_name,
			addr_list ? ((int)listcount(addr_list)) : -1,
			(void *)addr_list);
}

#define FREE_ADDR_LIST                                                         \
	if (hello_option_addr_list) {                                          \
		list_delete(&hello_option_addr_list);                          \
	}

#define FREE_ADDR_LIST_THEN_RETURN(code)                                       \
	{                                                                      \
		FREE_ADDR_LIST                                                 \
		return (code);                                                 \
	}

int pim_hello_recv(struct interface *ifp, pim_addr src_addr, uint8_t *tlv_buf,
		   int tlv_buf_size)
{
	struct pim_interface *pim_ifp;
	struct pim_neighbor *neigh;
	uint8_t *tlv_curr;
	uint8_t *tlv_pastend;
	pim_hello_options hello_options =
		0; /* bit array recording options found */
	uint16_t hello_option_holdtime = 0;
	uint16_t hello_option_propagation_delay = 0;
	uint16_t hello_option_override_interval = 0;
	uint32_t hello_option_dr_priority = 0;
	uint32_t hello_option_generation_id = 0;
	struct list *hello_option_addr_list = 0;

	if (PIM_DEBUG_PIM_HELLO)
		on_trace(__func__, ifp, src_addr);

	pim_ifp = ifp->info;
	assert(pim_ifp);

	if (pim_ifp->pim_passive_enable) {
		if (PIM_DEBUG_PIM_PACKETS)
			zlog_debug(
				"skip receiving PIM message on passive interface %s",
				ifp->name);
		return 0;
	}

	++pim_ifp->pim_ifstat_hello_recv;

	/*
	  Parse PIM hello TLVs
	 */
	assert(tlv_buf_size >= 0);
	tlv_curr = tlv_buf;
	tlv_pastend = tlv_buf + tlv_buf_size;

	while (tlv_curr < tlv_pastend) {
		uint16_t option_type;
		uint16_t option_len;
		int remain = tlv_pastend - tlv_curr;

		if (remain < PIM_TLV_MIN_SIZE) {
			if (PIM_DEBUG_PIM_HELLO)
				zlog_debug(
					"%s: short PIM hello TLV size=%d < min=%d from %pPAs on interface %s",
					__func__, remain, PIM_TLV_MIN_SIZE,
					&src_addr, ifp->name);
			FREE_ADDR_LIST_THEN_RETURN(-1);
		}

		option_type = PIM_TLV_GET_TYPE(tlv_curr);
		tlv_curr += PIM_TLV_TYPE_SIZE;
		option_len = PIM_TLV_GET_LENGTH(tlv_curr);
		tlv_curr += PIM_TLV_LENGTH_SIZE;

		if ((tlv_curr + option_len) > tlv_pastend) {
			if (PIM_DEBUG_PIM_HELLO)
				zlog_debug(
					"%s: long PIM hello TLV type=%d length=%d > left=%td from %pPAs on interface %s",
					__func__, option_type, option_len,
					tlv_pastend - tlv_curr, &src_addr,
					ifp->name);
			FREE_ADDR_LIST_THEN_RETURN(-2);
		}

		if (PIM_DEBUG_PIM_HELLO)
			zlog_debug(
				"%s: parse left_size=%d: PIM hello TLV type=%d length=%d from %pPAs on %s",
				__func__, remain, option_type, option_len,
				&src_addr, ifp->name);

		switch (option_type) {
		case PIM_MSG_OPTION_TYPE_HOLDTIME:
			if (pim_tlv_parse_holdtime(ifp->name, src_addr,
						   &hello_options,
						   &hello_option_holdtime,
						   option_len, tlv_curr)) {
				FREE_ADDR_LIST_THEN_RETURN(-3);
			}
			break;
		case PIM_MSG_OPTION_TYPE_LAN_PRUNE_DELAY:
			if (pim_tlv_parse_lan_prune_delay(
				    ifp->name, src_addr, &hello_options,
				    &hello_option_propagation_delay,
				    &hello_option_override_interval, option_len,
				    tlv_curr)) {
				FREE_ADDR_LIST_THEN_RETURN(-4);
			}
			break;
		case PIM_MSG_OPTION_TYPE_DR_PRIORITY:
			if (pim_tlv_parse_dr_priority(ifp->name, src_addr,
						      &hello_options,
						      &hello_option_dr_priority,
						      option_len, tlv_curr)) {
				FREE_ADDR_LIST_THEN_RETURN(-5);
			}
			break;
		case PIM_MSG_OPTION_TYPE_GENERATION_ID:
			if (pim_tlv_parse_generation_id(
				    ifp->name, src_addr, &hello_options,
				    &hello_option_generation_id, option_len,
				    tlv_curr)) {
				FREE_ADDR_LIST_THEN_RETURN(-6);
			}
			break;
		case PIM_MSG_OPTION_TYPE_ADDRESS_LIST:
			if (pim_tlv_parse_addr_list(ifp->name, src_addr,
						    &hello_options,
						    &hello_option_addr_list,
						    option_len, tlv_curr)) {
				return -7;
			}
			break;
		case PIM_MSG_OPTION_TYPE_DM_STATE_REFRESH:
			if (PIM_DEBUG_PIM_HELLO)
				zlog_debug(
					"%s: ignoring PIM hello dense-mode state refresh TLV option type=%d length=%d from %pPAs on interface %s",
					__func__, option_type, option_len,
					&src_addr, ifp->name);
			break;
		default:
			if (PIM_DEBUG_PIM_HELLO)
				zlog_debug(
					"%s: ignoring unknown PIM hello TLV type=%d length=%d from %pPAs on interface %s",
					__func__, option_type, option_len,
					&src_addr, ifp->name);
		}

		tlv_curr += option_len;
	}

	/*
	  Check received PIM hello options
	*/

	if (PIM_DEBUG_PIM_HELLO) {
		tlv_trace_uint16(__func__, "holdtime", ifp->name, src_addr,
				 PIM_OPTION_IS_SET(hello_options,
						   PIM_OPTION_MASK_HOLDTIME),
				 hello_option_holdtime);
		tlv_trace_uint16(
			__func__, "propagation_delay", ifp->name, src_addr,
			PIM_OPTION_IS_SET(hello_options,
					  PIM_OPTION_MASK_LAN_PRUNE_DELAY),
			hello_option_propagation_delay);
		tlv_trace_uint16(
			__func__, "override_interval", ifp->name, src_addr,
			PIM_OPTION_IS_SET(hello_options,
					  PIM_OPTION_MASK_LAN_PRUNE_DELAY),
			hello_option_override_interval);
		tlv_trace_bool(
			__func__, "can_disable_join_suppression", ifp->name,
			src_addr,
			PIM_OPTION_IS_SET(hello_options,
					  PIM_OPTION_MASK_LAN_PRUNE_DELAY),
			PIM_OPTION_IS_SET(
				hello_options,
				PIM_OPTION_MASK_CAN_DISABLE_JOIN_SUPPRESSION));
		tlv_trace_uint32(__func__, "dr_priority", ifp->name, src_addr,
				 PIM_OPTION_IS_SET(hello_options,
						   PIM_OPTION_MASK_DR_PRIORITY),
				 hello_option_dr_priority);
		tlv_trace_uint32_hex(
			__func__, "generation_id", ifp->name, src_addr,
			PIM_OPTION_IS_SET(hello_options,
					  PIM_OPTION_MASK_GENERATION_ID),
			hello_option_generation_id);
		tlv_trace_list(__func__, "address_list", ifp->name, src_addr,
			       PIM_OPTION_IS_SET(hello_options,
						 PIM_OPTION_MASK_ADDRESS_LIST),
			       hello_option_addr_list);
	}

	if (!PIM_OPTION_IS_SET(hello_options, PIM_OPTION_MASK_HOLDTIME)) {
		if (PIM_DEBUG_PIM_HELLO)
			zlog_debug(
				"%s: PIM hello missing holdtime from %pPAs on interface %s",
				__func__, &src_addr, ifp->name);
	}

	/*
	  New neighbor?
	*/

	neigh = pim_neighbor_find(ifp, src_addr, false);
	if (!neigh) {
		/* Add as new neighbor */

		neigh = pim_neighbor_add(
			ifp, src_addr, hello_options, hello_option_holdtime,
			hello_option_propagation_delay,
			hello_option_override_interval,
			hello_option_dr_priority, hello_option_generation_id,
			hello_option_addr_list, PIM_NEIGHBOR_SEND_DELAY);
		if (!neigh) {
			if (PIM_DEBUG_PIM_HELLO)
				zlog_warn(
					"%s: failure creating PIM neighbor %pPAs on interface %s",
					__func__, &src_addr, ifp->name);
			FREE_ADDR_LIST_THEN_RETURN(-8);
		}
		/* Forward BSM if required */
		if (!pim_bsm_new_nbr_fwd(neigh, ifp)) {
			if (PIM_DEBUG_PIM_HELLO)
				zlog_debug(
					"%s: forwarding bsm to new nbr failed",
					__func__);
		}

		/* actual addr list has been saved under neighbor */
		return 0;
	}

	/*
	  Received generation ID ?
	*/

	if (PIM_OPTION_IS_SET(hello_options, PIM_OPTION_MASK_GENERATION_ID)) {
		/* GenID mismatch ? */
		if (!PIM_OPTION_IS_SET(neigh->hello_options,
				       PIM_OPTION_MASK_GENERATION_ID)
		    || (hello_option_generation_id != neigh->generation_id)) {
			/* GenID mismatch, then replace neighbor */

			if (PIM_DEBUG_PIM_HELLO)
				zlog_debug(
					"%s: GenId mismatch new=%08x old=%08x: replacing neighbor %pPAs on %s",
					__func__, hello_option_generation_id,
					neigh->generation_id, &src_addr,
					ifp->name);

			pim_upstream_rpf_genid_changed(pim_ifp->pim,
						       neigh->source_addr);

			pim_neighbor_delete(ifp, neigh, "GenID mismatch");
			neigh = pim_neighbor_add(ifp, src_addr, hello_options,
						 hello_option_holdtime,
						 hello_option_propagation_delay,
						 hello_option_override_interval,
						 hello_option_dr_priority,
						 hello_option_generation_id,
						 hello_option_addr_list,
						 PIM_NEIGHBOR_SEND_NOW);
			if (!neigh) {
				if (PIM_DEBUG_PIM_HELLO)
					zlog_debug(
						"%s: failure re-creating PIM neighbor %pPAs on interface %s",
						__func__, &src_addr, ifp->name);
				FREE_ADDR_LIST_THEN_RETURN(-9);
			}
			/* Forward BSM if required */
			if (!pim_bsm_new_nbr_fwd(neigh, ifp)) {
				if (PIM_DEBUG_PIM_HELLO)
					zlog_debug(
						"%s: forwarding bsm to new nbr failed",
						__func__);
			}
			/* actual addr list is saved under neighbor */
			return 0;

		} /* GenId mismatch: replace neighbor */

	} /* GenId received */

	/*
	  Update existing neighbor
	*/

	pim_neighbor_update(neigh, hello_options, hello_option_holdtime,
			    hello_option_dr_priority, hello_option_addr_list);
	/* actual addr list is saved under neighbor */
	return 0;
}

int pim_hello_build_tlv(struct interface *ifp, uint8_t *tlv_buf,
			int tlv_buf_size, uint16_t holdtime,
			uint32_t dr_priority, uint32_t generation_id,
			uint16_t propagation_delay, uint16_t override_interval,
			int can_disable_join_suppression)
{
	uint8_t *curr = tlv_buf;
	uint8_t *pastend = tlv_buf + tlv_buf_size;
	uint8_t *tmp;
#if PIM_IPV == 4
	struct pim_interface *pim_ifp = ifp->info;
	struct pim_instance *pim = pim_ifp->pim;
#endif

	/*
	 * Append options
	 */

	/* Holdtime */
	curr = pim_tlv_append_uint16(curr, pastend,
				     PIM_MSG_OPTION_TYPE_HOLDTIME, holdtime);
	if (!curr) {
		if (PIM_DEBUG_PIM_HELLO) {
			zlog_debug(
				"%s: could not set PIM hello Holdtime option for interface %s",
				__func__, ifp->name);
		}
		return -1;
	}

	/* LAN Prune Delay */
	tmp = pim_tlv_append_2uint16(curr, pastend,
				     PIM_MSG_OPTION_TYPE_LAN_PRUNE_DELAY,
				     propagation_delay, override_interval);
	if (!tmp) {
		if (PIM_DEBUG_PIM_HELLO) {
			zlog_debug(
				"%s: could not set PIM LAN Prune Delay option for interface %s",
				__func__, ifp->name);
		}
		return -1;
	}
	if (can_disable_join_suppression) {
		*(curr + 4) |= 0x80; /* enable T bit */
	}
	curr = tmp;

	/* DR Priority */
	curr = pim_tlv_append_uint32(
		curr, pastend, PIM_MSG_OPTION_TYPE_DR_PRIORITY, dr_priority);
	if (!curr) {
		if (PIM_DEBUG_PIM_HELLO) {
			zlog_debug(
				"%s: could not set PIM hello DR Priority option for interface %s",
				__func__, ifp->name);
		}
		return -2;
	}

	/* Generation ID */
	curr = pim_tlv_append_uint32(curr, pastend,
				     PIM_MSG_OPTION_TYPE_GENERATION_ID,
				     generation_id);
	if (!curr) {
		if (PIM_DEBUG_PIM_HELLO) {
			zlog_debug(
				"%s: could not set PIM hello Generation ID option for interface %s",
				__func__, ifp->name);
		}
		return -3;
	}

	/* Secondary Address List */
	if (if_connected_count(ifp->connected)) {
		curr = pim_tlv_append_addrlist_ucast(curr, pastend, ifp,
						     PIM_AF);
		if (!curr) {
			if (PIM_DEBUG_PIM_HELLO) {
				zlog_debug(
					"%s: could not set PIM hello %s Secondary Address List option for interface %s",
					__func__, PIM_AF_NAME, ifp->name);
			}
			return -4;
		}
#if PIM_IPV == 4
		if (pim->send_v6_secondary) {
			curr = pim_tlv_append_addrlist_ucast(curr, pastend, ifp,
							     AF_INET6);
			if (!curr) {
				if (PIM_DEBUG_PIM_HELLO) {
					zlog_debug(
						"%s: could not sent PIM hello v6 secondary Address List option for interface %s",
						__func__, ifp->name);
				}
				return -4;
			}
		}
#endif
	}

	return curr - tlv_buf;
}

/*
  RFC 4601: 4.3.1.  Sending Hello Messages

  Thus, if a router needs to send a Join/Prune or Assert message on an
  interface on which it has not yet sent a Hello message with the
  currently configured IP address, then it MUST immediately send the
  relevant Hello message without waiting for the Hello Timer to
  expire, followed by the Join/Prune or Assert message.
*/
void pim_hello_require(struct interface *ifp)
{
	struct pim_interface *pim_ifp;

	assert(ifp);

	pim_ifp = ifp->info;

	assert(pim_ifp);

	if (PIM_IF_FLAG_TEST_HELLO_SENT(pim_ifp->flags))
		return;

	pim_hello_restart_now(ifp); /* Send hello and restart timer */
}