]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_pim.c
Merge pull request #5058 from mjstapp/fix_dplane_config_handler
[mirror_frr.git] / pimd / pim_pim.c
1 /*
2 * PIM for Quagga
3 * Copyright (C) 2008 Everton da Silva Marques
4 *
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.
9 *
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.
14 *
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
18 */
19
20 #include <zebra.h>
21
22 #include "log.h"
23 #include "thread.h"
24 #include "memory.h"
25 #include "if.h"
26
27 #include "pimd.h"
28 #include "pim_pim.h"
29 #include "pim_time.h"
30 #include "pim_iface.h"
31 #include "pim_sock.h"
32 #include "pim_str.h"
33 #include "pim_util.h"
34 #include "pim_tlv.h"
35 #include "pim_neighbor.h"
36 #include "pim_hello.h"
37 #include "pim_join.h"
38 #include "pim_assert.h"
39 #include "pim_msg.h"
40 #include "pim_register.h"
41 #include "pim_errors.h"
42 #include "pim_bsm.h"
43
44 static int on_pim_hello_send(struct thread *t);
45 static int pim_hello_send(struct interface *ifp, uint16_t holdtime);
46
47 static const char *pim_pim_msgtype2str(enum pim_msg_type type)
48 {
49 switch (type) {
50 case PIM_MSG_TYPE_HELLO:
51 return "HELLO";
52 case PIM_MSG_TYPE_REGISTER:
53 return "REGISTER";
54 case PIM_MSG_TYPE_REG_STOP:
55 return "REGSTOP";
56 case PIM_MSG_TYPE_JOIN_PRUNE:
57 return "JOINPRUNE";
58 case PIM_MSG_TYPE_BOOTSTRAP:
59 return "BOOT";
60 case PIM_MSG_TYPE_ASSERT:
61 return "ASSERT";
62 case PIM_MSG_TYPE_GRAFT:
63 return "GRAFT";
64 case PIM_MSG_TYPE_GRAFT_ACK:
65 return "GACK";
66 case PIM_MSG_TYPE_CANDIDATE:
67 return "CANDIDATE";
68 }
69
70 return "UNKNOWN";
71 }
72
73 static void sock_close(struct interface *ifp)
74 {
75 struct pim_interface *pim_ifp = ifp->info;
76
77 if (PIM_DEBUG_PIM_TRACE) {
78 if (pim_ifp->t_pim_sock_read) {
79 zlog_debug(
80 "Cancelling READ event for PIM socket fd=%d on interface %s",
81 pim_ifp->pim_sock_fd, ifp->name);
82 }
83 }
84 THREAD_OFF(pim_ifp->t_pim_sock_read);
85
86 if (PIM_DEBUG_PIM_TRACE) {
87 if (pim_ifp->t_pim_hello_timer) {
88 zlog_debug(
89 "Cancelling PIM hello timer for interface %s",
90 ifp->name);
91 }
92 }
93 THREAD_OFF(pim_ifp->t_pim_hello_timer);
94
95 if (PIM_DEBUG_PIM_TRACE) {
96 zlog_debug("Deleting PIM socket fd=%d on interface %s",
97 pim_ifp->pim_sock_fd, ifp->name);
98 }
99
100 /*
101 * If the fd is already deleted no need to do anything here
102 */
103 if (pim_ifp->pim_sock_fd > 0 && close(pim_ifp->pim_sock_fd)) {
104 zlog_warn(
105 "Failure closing PIM socket fd=%d on interface %s: errno=%d: %s",
106 pim_ifp->pim_sock_fd, ifp->name, errno,
107 safe_strerror(errno));
108 }
109
110 pim_ifp->pim_sock_fd = -1;
111 pim_ifp->pim_sock_creation = 0;
112 }
113
114 void pim_sock_delete(struct interface *ifp, const char *delete_message)
115 {
116 zlog_info("PIM INTERFACE DOWN: on interface %s: %s", ifp->name,
117 delete_message);
118
119 if (!ifp->info) {
120 flog_err(EC_PIM_CONFIG,
121 "%s: %s: but PIM not enabled on interface %s (!)",
122 __PRETTY_FUNCTION__, delete_message, ifp->name);
123 return;
124 }
125
126 /*
127 RFC 4601: 4.3.1. Sending Hello Messages
128
129 Before an interface goes down or changes primary IP address, a Hello
130 message with a zero HoldTime should be sent immediately (with the
131 old IP address if the IP address changed).
132 */
133 pim_hello_send(ifp, 0 /* zero-sec holdtime */);
134
135 pim_neighbor_delete_all(ifp, delete_message);
136
137 sock_close(ifp);
138 }
139
140 int pim_pim_packet(struct interface *ifp, uint8_t *buf, size_t len)
141 {
142 struct ip *ip_hdr;
143 size_t ip_hlen; /* ip header length in bytes */
144 char src_str[INET_ADDRSTRLEN];
145 char dst_str[INET_ADDRSTRLEN];
146 uint8_t *pim_msg;
147 int pim_msg_len;
148 uint16_t pim_checksum; /* received checksum */
149 uint16_t checksum; /* computed checksum */
150 struct pim_neighbor *neigh;
151 struct pim_msg_header *header;
152 bool no_fwd;
153
154 if (len < sizeof(*ip_hdr)) {
155 if (PIM_DEBUG_PIM_PACKETS)
156 zlog_debug(
157 "PIM packet size=%zu shorter than minimum=%zu",
158 len, sizeof(*ip_hdr));
159 return -1;
160 }
161
162 ip_hdr = (struct ip *)buf;
163 ip_hlen = ip_hdr->ip_hl << 2; /* ip_hl gives length in 4-byte words */
164
165 pim_msg = buf + ip_hlen;
166 pim_msg_len = len - ip_hlen;
167
168 header = (struct pim_msg_header *)pim_msg;
169 if (pim_msg_len < PIM_PIM_MIN_LEN) {
170 if (PIM_DEBUG_PIM_PACKETS)
171 zlog_debug(
172 "PIM message size=%d shorter than minimum=%d",
173 pim_msg_len, PIM_PIM_MIN_LEN);
174 return -1;
175 }
176
177 if (header->ver != PIM_PROTO_VERSION) {
178 if (PIM_DEBUG_PIM_PACKETS)
179 zlog_debug(
180 "Ignoring PIM pkt from %s with unsupported version: %d",
181 ifp->name, header->ver);
182 return -1;
183 }
184
185 /* save received checksum */
186 pim_checksum = header->checksum;
187
188 /* for computing checksum */
189 header->checksum = 0;
190 no_fwd = header->Nbit;
191
192 if (header->type == PIM_MSG_TYPE_REGISTER) {
193 /* First 8 byte header checksum */
194 checksum = in_cksum(pim_msg, PIM_MSG_REGISTER_LEN);
195 if (checksum != pim_checksum) {
196 checksum = in_cksum(pim_msg, pim_msg_len);
197 if (checksum != pim_checksum) {
198 if (PIM_DEBUG_PIM_PACKETS)
199 zlog_debug(
200 "Ignoring PIM pkt from %s with invalid checksum: received=%x calculated=%x",
201 ifp->name, pim_checksum,
202 checksum);
203
204 return -1;
205 }
206 }
207 } else {
208 checksum = in_cksum(pim_msg, pim_msg_len);
209 if (checksum != pim_checksum) {
210 if (PIM_DEBUG_PIM_PACKETS)
211 zlog_debug(
212 "Ignoring PIM pkt from %s with invalid checksum: received=%x calculated=%x",
213 ifp->name, pim_checksum, checksum);
214
215 return -1;
216 }
217 }
218
219 if (PIM_DEBUG_PIM_PACKETS) {
220 pim_inet4_dump("<src?>", ip_hdr->ip_src, src_str,
221 sizeof(src_str));
222 pim_inet4_dump("<dst?>", ip_hdr->ip_dst, dst_str,
223 sizeof(dst_str));
224 zlog_debug(
225 "Recv PIM %s packet from %s to %s on %s: ttl=%d pim_version=%d pim_msg_size=%d checksum=%x",
226 pim_pim_msgtype2str(header->type), src_str, dst_str,
227 ifp->name, ip_hdr->ip_ttl, header->ver, pim_msg_len,
228 checksum);
229 if (PIM_DEBUG_PIM_PACKETDUMP_RECV) {
230 pim_pkt_dump(__PRETTY_FUNCTION__, pim_msg, pim_msg_len);
231 }
232 }
233
234 switch (header->type) {
235 case PIM_MSG_TYPE_HELLO:
236 return pim_hello_recv(ifp, ip_hdr->ip_src,
237 pim_msg + PIM_MSG_HEADER_LEN,
238 pim_msg_len - PIM_MSG_HEADER_LEN);
239 break;
240 case PIM_MSG_TYPE_REGISTER:
241 return pim_register_recv(ifp, ip_hdr->ip_dst, ip_hdr->ip_src,
242 pim_msg + PIM_MSG_HEADER_LEN,
243 pim_msg_len - PIM_MSG_HEADER_LEN);
244 break;
245 case PIM_MSG_TYPE_REG_STOP:
246 return pim_register_stop_recv(ifp, pim_msg + PIM_MSG_HEADER_LEN,
247 pim_msg_len - PIM_MSG_HEADER_LEN);
248 break;
249 case PIM_MSG_TYPE_JOIN_PRUNE:
250 neigh = pim_neighbor_find(ifp, ip_hdr->ip_src);
251 if (!neigh) {
252 if (PIM_DEBUG_PIM_PACKETS)
253 zlog_debug(
254 "%s %s: non-hello PIM message type=%d from non-neighbor %s on %s",
255 __FILE__, __PRETTY_FUNCTION__,
256 header->type, src_str, ifp->name);
257 return -1;
258 }
259 pim_neighbor_timer_reset(neigh, neigh->holdtime);
260 return pim_joinprune_recv(ifp, neigh, ip_hdr->ip_src,
261 pim_msg + PIM_MSG_HEADER_LEN,
262 pim_msg_len - PIM_MSG_HEADER_LEN);
263 break;
264 case PIM_MSG_TYPE_ASSERT:
265 neigh = pim_neighbor_find(ifp, ip_hdr->ip_src);
266 if (!neigh) {
267 if (PIM_DEBUG_PIM_PACKETS)
268 zlog_debug(
269 "%s %s: non-hello PIM message type=%d from non-neighbor %s on %s",
270 __FILE__, __PRETTY_FUNCTION__,
271 header->type, src_str, ifp->name);
272 return -1;
273 }
274 pim_neighbor_timer_reset(neigh, neigh->holdtime);
275 return pim_assert_recv(ifp, neigh, ip_hdr->ip_src,
276 pim_msg + PIM_MSG_HEADER_LEN,
277 pim_msg_len - PIM_MSG_HEADER_LEN);
278 break;
279 case PIM_MSG_TYPE_BOOTSTRAP:
280 return pim_bsm_process(ifp, ip_hdr, pim_msg, pim_msg_len,
281 no_fwd);
282 break;
283
284 default:
285 if (PIM_DEBUG_PIM_PACKETS) {
286 zlog_debug(
287 "Recv PIM packet type %d which is not currently understood",
288 header->type);
289 }
290 return -1;
291 }
292 return -1;
293 }
294
295 static void pim_sock_read_on(struct interface *ifp);
296
297 static int pim_sock_read(struct thread *t)
298 {
299 struct interface *ifp, *orig_ifp;
300 struct pim_interface *pim_ifp;
301 int fd;
302 struct sockaddr_in from;
303 struct sockaddr_in to;
304 socklen_t fromlen = sizeof(from);
305 socklen_t tolen = sizeof(to);
306 uint8_t buf[PIM_PIM_BUFSIZE_READ];
307 int len;
308 ifindex_t ifindex = -1;
309 int result = -1; /* defaults to bad */
310 static long long count = 0;
311 int cont = 1;
312
313 orig_ifp = ifp = THREAD_ARG(t);
314 fd = THREAD_FD(t);
315
316 pim_ifp = ifp->info;
317
318 while (cont) {
319 len = pim_socket_recvfromto(fd, buf, sizeof(buf), &from,
320 &fromlen, &to, &tolen, &ifindex);
321 if (len < 0) {
322 if (errno == EINTR)
323 continue;
324 if (errno == EWOULDBLOCK || errno == EAGAIN)
325 break;
326
327 if (PIM_DEBUG_PIM_PACKETS)
328 zlog_debug("Received errno: %d %s", errno,
329 safe_strerror(errno));
330 goto done;
331 }
332
333 /*
334 * What? So with vrf's the incoming packet is received
335 * on the vrf interface but recvfromto above returns
336 * the right ifindex, so just use it. We know
337 * it's the right interface because we bind to it
338 */
339 ifp = if_lookup_by_index(ifindex, pim_ifp->pim->vrf_id);
340 if (!ifp || !ifp->info) {
341 if (PIM_DEBUG_PIM_PACKETS)
342 zlog_debug(
343 "%s: Received incoming pim packet on interface(%s:%d) not yet configured for pim",
344 __PRETTY_FUNCTION__,
345 ifp ? ifp->name : "Unknown", ifindex);
346 goto done;
347 }
348 int fail = pim_pim_packet(ifp, buf, len);
349 if (fail) {
350 if (PIM_DEBUG_PIM_PACKETS)
351 zlog_debug("%s: pim_pim_packet() return=%d",
352 __PRETTY_FUNCTION__, fail);
353 goto done;
354 }
355
356 count++;
357 if (count % router->packet_process == 0)
358 cont = 0;
359 }
360
361 result = 0; /* good */
362
363 done:
364 pim_sock_read_on(orig_ifp);
365
366 if (result) {
367 ++pim_ifp->pim_ifstat_hello_recvfail;
368 }
369
370 return result;
371 }
372
373 static void pim_sock_read_on(struct interface *ifp)
374 {
375 struct pim_interface *pim_ifp;
376
377 zassert(ifp);
378 zassert(ifp->info);
379
380 pim_ifp = ifp->info;
381
382 if (PIM_DEBUG_PIM_TRACE_DETAIL) {
383 zlog_debug("Scheduling READ event on PIM socket fd=%d",
384 pim_ifp->pim_sock_fd);
385 }
386 pim_ifp->t_pim_sock_read = NULL;
387 thread_add_read(router->master, pim_sock_read, ifp,
388 pim_ifp->pim_sock_fd, &pim_ifp->t_pim_sock_read);
389 }
390
391 static int pim_sock_open(struct interface *ifp)
392 {
393 int fd;
394 struct pim_interface *pim_ifp = ifp->info;
395
396 fd = pim_socket_mcast(IPPROTO_PIM, pim_ifp->primary_address, ifp,
397 0 /* loop=false */);
398 if (fd < 0)
399 return -1;
400
401 if (pim_socket_join(fd, qpim_all_pim_routers_addr,
402 pim_ifp->primary_address, ifp->ifindex)) {
403 close(fd);
404 return -2;
405 }
406
407 return fd;
408 }
409
410 void pim_ifstat_reset(struct interface *ifp)
411 {
412 struct pim_interface *pim_ifp;
413
414 zassert(ifp);
415
416 pim_ifp = ifp->info;
417 if (!pim_ifp) {
418 return;
419 }
420
421 pim_ifp->pim_ifstat_start = pim_time_monotonic_sec();
422 pim_ifp->pim_ifstat_hello_sent = 0;
423 pim_ifp->pim_ifstat_hello_sendfail = 0;
424 pim_ifp->pim_ifstat_hello_recv = 0;
425 pim_ifp->pim_ifstat_hello_recvfail = 0;
426 }
427
428 void pim_sock_reset(struct interface *ifp)
429 {
430 struct pim_interface *pim_ifp;
431
432 zassert(ifp);
433 zassert(ifp->info);
434
435 pim_ifp = ifp->info;
436
437 pim_ifp->primary_address = pim_find_primary_addr(ifp);
438
439 pim_ifp->pim_sock_fd = -1;
440 pim_ifp->pim_sock_creation = 0;
441 pim_ifp->t_pim_sock_read = NULL;
442
443 pim_ifp->t_pim_hello_timer = NULL;
444 pim_ifp->pim_hello_period = PIM_DEFAULT_HELLO_PERIOD;
445 pim_ifp->pim_default_holdtime =
446 -1; /* unset: means 3.5 * pim_hello_period */
447 pim_ifp->pim_triggered_hello_delay = PIM_DEFAULT_TRIGGERED_HELLO_DELAY;
448 pim_ifp->pim_dr_priority = PIM_DEFAULT_DR_PRIORITY;
449 pim_ifp->pim_propagation_delay_msec =
450 PIM_DEFAULT_PROPAGATION_DELAY_MSEC;
451 pim_ifp->pim_override_interval_msec =
452 PIM_DEFAULT_OVERRIDE_INTERVAL_MSEC;
453 if (PIM_DEFAULT_CAN_DISABLE_JOIN_SUPPRESSION) {
454 PIM_IF_DO_PIM_CAN_DISABLE_JOIN_SUPRESSION(pim_ifp->options);
455 } else {
456 PIM_IF_DONT_PIM_CAN_DISABLE_JOIN_SUPRESSION(pim_ifp->options);
457 }
458
459 /* neighbors without lan_delay */
460 pim_ifp->pim_number_of_nonlandelay_neighbors = 0;
461 pim_ifp->pim_neighbors_highest_propagation_delay_msec = 0;
462 pim_ifp->pim_neighbors_highest_override_interval_msec = 0;
463
464 /* DR Election */
465 pim_ifp->pim_dr_election_last = 0; /* timestamp */
466 pim_ifp->pim_dr_election_count = 0;
467 pim_ifp->pim_dr_election_changes = 0;
468 pim_ifp->pim_dr_num_nondrpri_neighbors =
469 0; /* neighbors without dr_pri */
470 pim_ifp->pim_dr_addr = pim_ifp->primary_address;
471
472 pim_ifstat_reset(ifp);
473 }
474
475 static uint16_t ip_id = 0;
476
477
478 static int pim_msg_send_frame(int fd, char *buf, size_t len,
479 struct sockaddr *dst, size_t salen)
480 {
481 struct ip *ip = (struct ip *)buf;
482
483 while (sendto(fd, buf, len, MSG_DONTWAIT, dst, salen) < 0) {
484 char dst_str[INET_ADDRSTRLEN];
485
486 switch (errno) {
487 case EMSGSIZE: {
488 size_t hdrsize = sizeof(struct ip);
489 size_t newlen1 = ((len - hdrsize) / 2) & 0xFFF8;
490 size_t sendlen = newlen1 + hdrsize;
491 size_t offset = ntohs(ip->ip_off);
492
493 ip->ip_len = htons(sendlen);
494 ip->ip_off = htons(offset | IP_MF);
495 if (pim_msg_send_frame(fd, buf, sendlen, dst, salen)
496 == 0) {
497 struct ip *ip2 = (struct ip *)(buf + newlen1);
498 size_t newlen2 = len - sendlen;
499 sendlen = newlen2 + hdrsize;
500
501 memcpy(ip2, ip, hdrsize);
502 ip2->ip_len = htons(sendlen);
503 ip2->ip_off = htons(offset + (newlen1 >> 3));
504 return pim_msg_send_frame(fd, (char *)ip2,
505 sendlen, dst, salen);
506 }
507 }
508
509 return -1;
510 break;
511 default:
512 if (PIM_DEBUG_PIM_PACKETS) {
513 pim_inet4_dump("<dst?>", ip->ip_dst, dst_str,
514 sizeof(dst_str));
515 zlog_warn(
516 "%s: sendto() failure to %s: fd=%d msg_size=%zd: errno=%d: %s",
517 __PRETTY_FUNCTION__, dst_str, fd, len,
518 errno, safe_strerror(errno));
519 }
520 return -1;
521 break;
522 }
523 }
524
525 return 0;
526 }
527
528 int pim_msg_send(int fd, struct in_addr src, struct in_addr dst,
529 uint8_t *pim_msg, int pim_msg_size, const char *ifname)
530 {
531 struct sockaddr_in to;
532 socklen_t tolen;
533 unsigned char buffer[10000];
534 unsigned char *msg_start;
535 uint8_t ttl;
536 struct pim_msg_header *header;
537 struct ip *ip;
538
539 memset(buffer, 0, 10000);
540 int sendlen = sizeof(struct ip) + pim_msg_size;
541
542 msg_start = buffer + sizeof(struct ip);
543 memcpy(msg_start, pim_msg, pim_msg_size);
544
545 header = (struct pim_msg_header *)pim_msg;
546 /*
547 * Omnios apparently doesn't have a #define for IP default
548 * ttl that is the same as all other platforms.
549 */
550 #ifndef IPDEFTTL
551 #define IPDEFTTL 64
552 #endif
553 /* TTL for packets destine to ALL-PIM-ROUTERS is 1 */
554 switch (header->type) {
555 case PIM_MSG_TYPE_HELLO:
556 case PIM_MSG_TYPE_JOIN_PRUNE:
557 case PIM_MSG_TYPE_BOOTSTRAP:
558 case PIM_MSG_TYPE_ASSERT:
559 ttl = 1;
560 break;
561 case PIM_MSG_TYPE_REGISTER:
562 case PIM_MSG_TYPE_REG_STOP:
563 case PIM_MSG_TYPE_GRAFT:
564 case PIM_MSG_TYPE_GRAFT_ACK:
565 case PIM_MSG_TYPE_CANDIDATE:
566 ttl = IPDEFTTL;
567 break;
568 default:
569 ttl = MAXTTL;
570 break;
571 }
572
573 ip = (struct ip *)buffer;
574 ip->ip_id = htons(++ip_id);
575 ip->ip_hl = 5;
576 ip->ip_v = 4;
577 ip->ip_tos = IPTOS_PREC_INTERNETCONTROL;
578 ip->ip_p = PIM_IP_PROTO_PIM;
579 ip->ip_src = src;
580 ip->ip_dst = dst;
581 ip->ip_ttl = ttl;
582 ip->ip_len = htons(sendlen);
583
584 if (PIM_DEBUG_PIM_PACKETS) {
585 char dst_str[INET_ADDRSTRLEN];
586 pim_inet4_dump("<dst?>", dst, dst_str, sizeof(dst_str));
587 zlog_debug("%s: to %s on %s: msg_size=%d checksum=%x",
588 __PRETTY_FUNCTION__, dst_str, ifname, pim_msg_size,
589 header->checksum);
590 }
591
592 memset(&to, 0, sizeof(to));
593 to.sin_family = AF_INET;
594 to.sin_addr = dst;
595 tolen = sizeof(to);
596
597 if (PIM_DEBUG_PIM_PACKETDUMP_SEND) {
598 pim_pkt_dump(__PRETTY_FUNCTION__, pim_msg, pim_msg_size);
599 }
600
601 pim_msg_send_frame(fd, (char *)buffer, sendlen, (struct sockaddr *)&to,
602 tolen);
603 return 0;
604 }
605
606 static int hello_send(struct interface *ifp, uint16_t holdtime)
607 {
608 uint8_t pim_msg[PIM_PIM_BUFSIZE_WRITE];
609 struct pim_interface *pim_ifp;
610 int pim_tlv_size;
611 int pim_msg_size;
612
613 pim_ifp = ifp->info;
614
615 if (PIM_DEBUG_PIM_HELLO) {
616 char dst_str[INET_ADDRSTRLEN];
617 pim_inet4_dump("<dst?>", qpim_all_pim_routers_addr, dst_str,
618 sizeof(dst_str));
619 zlog_debug(
620 "%s: to %s on %s: holdt=%u prop_d=%u overr_i=%u dis_join_supp=%d dr_prio=%u gen_id=%08x addrs=%d",
621 __PRETTY_FUNCTION__, dst_str, ifp->name, holdtime,
622 pim_ifp->pim_propagation_delay_msec,
623 pim_ifp->pim_override_interval_msec,
624 PIM_IF_TEST_PIM_CAN_DISABLE_JOIN_SUPRESSION(
625 pim_ifp->options),
626 pim_ifp->pim_dr_priority, pim_ifp->pim_generation_id,
627 listcount(ifp->connected));
628 }
629
630 pim_tlv_size = pim_hello_build_tlv(
631 ifp, pim_msg + PIM_PIM_MIN_LEN,
632 sizeof(pim_msg) - PIM_PIM_MIN_LEN, holdtime,
633 pim_ifp->pim_dr_priority, pim_ifp->pim_generation_id,
634 pim_ifp->pim_propagation_delay_msec,
635 pim_ifp->pim_override_interval_msec,
636 PIM_IF_TEST_PIM_CAN_DISABLE_JOIN_SUPRESSION(pim_ifp->options));
637 if (pim_tlv_size < 0) {
638 return -1;
639 }
640
641 pim_msg_size = pim_tlv_size + PIM_PIM_MIN_LEN;
642
643 zassert(pim_msg_size >= PIM_PIM_MIN_LEN);
644 zassert(pim_msg_size <= PIM_PIM_BUFSIZE_WRITE);
645
646 pim_msg_build_header(pim_msg, pim_msg_size, PIM_MSG_TYPE_HELLO, false);
647
648 if (pim_msg_send(pim_ifp->pim_sock_fd, pim_ifp->primary_address,
649 qpim_all_pim_routers_addr, pim_msg, pim_msg_size,
650 ifp->name)) {
651 if (PIM_DEBUG_PIM_HELLO) {
652 zlog_debug(
653 "%s: could not send PIM message on interface %s",
654 __PRETTY_FUNCTION__, ifp->name);
655 }
656 return -2;
657 }
658
659 return 0;
660 }
661
662 static int pim_hello_send(struct interface *ifp, uint16_t holdtime)
663 {
664 struct pim_interface *pim_ifp = ifp->info;
665
666 if (if_is_loopback_or_vrf(ifp))
667 return 0;
668
669 if (hello_send(ifp, holdtime)) {
670 ++pim_ifp->pim_ifstat_hello_sendfail;
671
672 if (PIM_DEBUG_PIM_HELLO) {
673 zlog_warn("Could not send PIM hello on interface %s",
674 ifp->name);
675 }
676 return -1;
677 }
678
679 ++pim_ifp->pim_ifstat_hello_sent;
680
681 return 0;
682 }
683
684 static void hello_resched(struct interface *ifp)
685 {
686 struct pim_interface *pim_ifp;
687
688 pim_ifp = ifp->info;
689
690 if (PIM_DEBUG_PIM_HELLO) {
691 zlog_debug("Rescheduling %d sec hello on interface %s",
692 pim_ifp->pim_hello_period, ifp->name);
693 }
694 THREAD_OFF(pim_ifp->t_pim_hello_timer);
695 thread_add_timer(router->master, on_pim_hello_send, ifp,
696 pim_ifp->pim_hello_period,
697 &pim_ifp->t_pim_hello_timer);
698 }
699
700 /*
701 Periodic hello timer
702 */
703 static int on_pim_hello_send(struct thread *t)
704 {
705 struct pim_interface *pim_ifp;
706 struct interface *ifp;
707
708 ifp = THREAD_ARG(t);
709 pim_ifp = ifp->info;
710
711 /*
712 * Schedule next hello
713 */
714 hello_resched(ifp);
715
716 /*
717 * Send hello
718 */
719 return pim_hello_send(ifp, PIM_IF_DEFAULT_HOLDTIME(pim_ifp));
720 }
721
722 /*
723 RFC 4601: 4.3.1. Sending Hello Messages
724
725 Thus, if a router needs to send a Join/Prune or Assert message on an
726 interface on which it has not yet sent a Hello message with the
727 currently configured IP address, then it MUST immediately send the
728 relevant Hello message without waiting for the Hello Timer to
729 expire, followed by the Join/Prune or Assert message.
730 */
731 void pim_hello_restart_now(struct interface *ifp)
732 {
733 struct pim_interface *pim_ifp;
734
735 pim_ifp = ifp->info;
736
737 /*
738 * Reset next hello timer
739 */
740 hello_resched(ifp);
741
742 /*
743 * Immediately send hello
744 */
745 pim_hello_send(ifp, PIM_IF_DEFAULT_HOLDTIME(pim_ifp));
746 }
747
748 /*
749 RFC 4601: 4.3.1. Sending Hello Messages
750
751 To allow new or rebooting routers to learn of PIM neighbors quickly,
752 when a Hello message is received from a new neighbor, or a Hello
753 message with a new GenID is received from an existing neighbor, a
754 new Hello message should be sent on this interface after a
755 randomized delay between 0 and Triggered_Hello_Delay.
756 */
757 void pim_hello_restart_triggered(struct interface *ifp)
758 {
759 struct pim_interface *pim_ifp;
760 int triggered_hello_delay_msec;
761 int random_msec;
762
763 pim_ifp = ifp->info;
764
765 /*
766 * No need to ever start loopback or vrf device hello's
767 */
768 if (if_is_loopback_or_vrf(ifp))
769 return;
770
771 /*
772 * There exists situations where we have the a RPF out this
773 * interface, but we haven't formed a neighbor yet. This
774 * happens especially during interface flaps. While
775 * we would like to handle this more gracefully in other
776 * parts of the code. In order to get us up and running
777 * let's just send the hello immediate'ish
778 * This should be revisited when we get nexthop tracking
779 * in and when we have a better handle on safely
780 * handling the rpf information for upstreams that
781 * we cannot legally reach yet.
782 */
783 triggered_hello_delay_msec = 1;
784 // triggered_hello_delay_msec = 1000 *
785 // pim_ifp->pim_triggered_hello_delay;
786
787 if (pim_ifp->t_pim_hello_timer) {
788 long remain_msec =
789 pim_time_timer_remain_msec(pim_ifp->t_pim_hello_timer);
790 if (remain_msec <= triggered_hello_delay_msec) {
791 /* Rescheduling hello would increase the delay, then
792 it's faster
793 to just wait for the scheduled periodic hello. */
794 return;
795 }
796
797 THREAD_OFF(pim_ifp->t_pim_hello_timer);
798 }
799
800 random_msec = triggered_hello_delay_msec;
801 // random_msec = random() % (triggered_hello_delay_msec + 1);
802
803 if (PIM_DEBUG_PIM_HELLO) {
804 zlog_debug("Scheduling %d msec triggered hello on interface %s",
805 random_msec, ifp->name);
806 }
807
808 thread_add_timer_msec(router->master, on_pim_hello_send, ifp,
809 random_msec, &pim_ifp->t_pim_hello_timer);
810 }
811
812 int pim_sock_add(struct interface *ifp)
813 {
814 struct pim_interface *pim_ifp;
815 uint32_t old_genid;
816
817 pim_ifp = ifp->info;
818 zassert(pim_ifp);
819
820 if (pim_ifp->pim_sock_fd >= 0) {
821 if (PIM_DEBUG_PIM_PACKETS)
822 zlog_debug(
823 "Can't recreate existing PIM socket fd=%d for interface %s",
824 pim_ifp->pim_sock_fd, ifp->name);
825 return -1;
826 }
827
828 pim_ifp->pim_sock_fd = pim_sock_open(ifp);
829 if (pim_ifp->pim_sock_fd < 0) {
830 if (PIM_DEBUG_PIM_PACKETS)
831 zlog_debug("Could not open PIM socket on interface %s",
832 ifp->name);
833 return -2;
834 }
835
836 pim_socket_ip_hdr(pim_ifp->pim_sock_fd);
837
838 pim_ifp->t_pim_sock_read = NULL;
839 pim_ifp->pim_sock_creation = pim_time_monotonic_sec();
840
841 /*
842 * Just ensure that the new generation id
843 * actually chooses something different.
844 * Actually ran across a case where this
845 * happened, pre-switch to random().
846 * While this is unlikely to happen now
847 * let's make sure it doesn't.
848 */
849 old_genid = pim_ifp->pim_generation_id;
850
851 while (old_genid == pim_ifp->pim_generation_id)
852 pim_ifp->pim_generation_id = random();
853
854 zlog_info("PIM INTERFACE UP: on interface %s ifindex=%d", ifp->name,
855 ifp->ifindex);
856
857 /*
858 * Start receiving PIM messages
859 */
860 pim_sock_read_on(ifp);
861
862 /*
863 * Start sending PIM hello's
864 */
865 pim_hello_restart_triggered(ifp);
866
867 return 0;
868 }