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