]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_igmp_mtrace.c
Merge pull request #1756 from qlyoung/stylechecker
[mirror_frr.git] / pimd / pim_igmp_mtrace.c
1 /*
2 * Multicast traceroute for FRRouting
3 * Copyright (C) 2017 Mladen Sablic
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 "pimd.h"
23 #include "pim_util.h"
24 #include "pim_sock.h"
25 #include "pim_rp.h"
26 #include "pim_oil.h"
27 #include "pim_ifchannel.h"
28 #include "pim_macro.h"
29 #include "pim_igmp_mtrace.h"
30
31 static void mtrace_rsp_init(struct igmp_mtrace_rsp *mtrace_rspp)
32 {
33 mtrace_rspp->arrival = 0;
34 mtrace_rspp->incoming.s_addr = 0;
35 mtrace_rspp->outgoing.s_addr = 0;
36 mtrace_rspp->prev_hop.s_addr = 0;
37 mtrace_rspp->in_count = MTRACE_UNKNOWN_COUNT;
38 mtrace_rspp->out_count = MTRACE_UNKNOWN_COUNT;
39 mtrace_rspp->total = MTRACE_UNKNOWN_COUNT;
40 mtrace_rspp->rtg_proto = 0;
41 mtrace_rspp->fwd_ttl = 0;
42 mtrace_rspp->mbz = 0;
43 mtrace_rspp->s = 0;
44 mtrace_rspp->src_mask = 0;
45 mtrace_rspp->fwd_code = MTRACE_FWD_CODE_NO_ERROR;
46 }
47
48 static void mtrace_rsp_debug(uint32_t qry_id, int rsp,
49 struct igmp_mtrace_rsp *mrspp)
50 {
51 char inc_str[INET_ADDRSTRLEN];
52 char out_str[INET_ADDRSTRLEN];
53 char prv_str[INET_ADDRSTRLEN];
54
55 zlog_debug(
56 "Rx mt(%d) qid=%ud arr=%x in=%s out=%s prev=%s proto=%d fwd=%d",
57 rsp, ntohl(qry_id), mrspp->arrival,
58 inet_ntop(AF_INET, &(mrspp->incoming), inc_str,
59 sizeof(inc_str)),
60 inet_ntop(AF_INET, &(mrspp->outgoing), out_str,
61 sizeof(out_str)),
62 inet_ntop(AF_INET, &(mrspp->prev_hop), prv_str,
63 sizeof(prv_str)),
64 mrspp->rtg_proto, mrspp->fwd_code);
65 }
66
67 static void mtrace_debug(struct pim_interface *pim_ifp,
68 struct igmp_mtrace *mtracep, int mtrace_len)
69 {
70 char inc_str[INET_ADDRSTRLEN];
71 char grp_str[INET_ADDRSTRLEN];
72 char src_str[INET_ADDRSTRLEN];
73 char dst_str[INET_ADDRSTRLEN];
74 char rsp_str[INET_ADDRSTRLEN];
75
76 zlog_debug(
77 "Rx mtrace packet incoming on %s: "
78 "hops=%d type=%d size=%d, grp=%s, src=%s,"
79 " dst=%s rsp=%s ttl=%d qid=%ud",
80 inet_ntop(AF_INET, &(pim_ifp->primary_address), inc_str,
81 sizeof(inc_str)),
82 mtracep->hops, mtracep->type, mtrace_len,
83 inet_ntop(AF_INET, &(mtracep->grp_addr), grp_str,
84 sizeof(grp_str)),
85 inet_ntop(AF_INET, &(mtracep->src_addr), src_str,
86 sizeof(src_str)),
87 inet_ntop(AF_INET, &(mtracep->dst_addr), dst_str,
88 sizeof(dst_str)),
89 inet_ntop(AF_INET, &(mtracep->rsp_addr), rsp_str,
90 sizeof(rsp_str)),
91 mtracep->rsp_ttl, ntohl(mtracep->qry_id));
92 if (mtrace_len > (int)sizeof(struct igmp_mtrace)) {
93
94 int i;
95
96 int responses = mtrace_len - sizeof(struct igmp_mtrace);
97
98 if ((responses % sizeof(struct igmp_mtrace_rsp)) != 0)
99 if (PIM_DEBUG_MTRACE)
100 zlog_debug(
101 "Mtrace response block of wrong"
102 " length");
103
104 responses = responses / sizeof(struct igmp_mtrace_rsp);
105
106 for (i = 0; i < responses; i++)
107 mtrace_rsp_debug(mtracep->qry_id, i, &mtracep->rsp[i]);
108 }
109 }
110
111 /* 5.1 Query Arrival Time */
112 static uint32_t query_arrival_time(void)
113 {
114 struct timeval tv;
115 uint32_t qat;
116
117 char m_qat[] = "Query arrival time lookup failed: errno=%d: %s";
118
119 if (gettimeofday(&tv, NULL) < 0) {
120 if (PIM_DEBUG_MTRACE)
121 zlog_warn(m_qat, errno, safe_strerror(errno));
122 return 0;
123 }
124 /* not sure second offset correct, as I get different value */
125 qat = ((tv.tv_sec + 32384) << 16) + ((tv.tv_usec << 10) / 15625);
126
127 return qat;
128 }
129
130 static int mtrace_send_packet(struct interface *ifp,
131 struct igmp_mtrace *mtracep,
132 size_t mtrace_buf_len, struct in_addr dst_addr,
133 struct in_addr group_addr)
134 {
135 struct sockaddr_in to;
136 struct pim_interface *pim_ifp;
137 socklen_t tolen;
138 ssize_t sent;
139 int ret;
140 int fd;
141 char pim_str[INET_ADDRSTRLEN];
142 char rsp_str[INET_ADDRSTRLEN];
143 u_char ttl;
144
145 pim_ifp = ifp->info;
146
147 memset(&to, 0, sizeof(to));
148 to.sin_family = AF_INET;
149 to.sin_addr = dst_addr;
150 tolen = sizeof(to);
151
152 if (PIM_DEBUG_MTRACE)
153 zlog_debug("Sending mtrace packet to %s on %s",
154 inet_ntop(AF_INET, &mtracep->rsp_addr, rsp_str,
155 sizeof(rsp_str)),
156 inet_ntop(AF_INET, &pim_ifp->primary_address,
157 pim_str, sizeof(pim_str)));
158
159 fd = pim_socket_raw(IPPROTO_IGMP);
160
161 if (fd < 0)
162 return -1;
163
164 ret = pim_socket_bind(fd, ifp);
165
166 if (ret < 0) {
167 ret = -1;
168 goto close_fd;
169 }
170
171 if (IPV4_CLASS_DE(ntohl(dst_addr.s_addr))) {
172 if (IPV4_MC_LINKLOCAL(ntohl(dst_addr.s_addr))) {
173 ttl = 1;
174 } else {
175 if (mtracep->type == PIM_IGMP_MTRACE_RESPONSE)
176 ttl = mtracep->rsp_ttl;
177 else
178 ttl = 64;
179 }
180 ret = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl,
181 sizeof(ttl));
182
183 if (ret < 0) {
184 if (PIM_DEBUG_MTRACE)
185 zlog_warn("Failed to set socket multicast TTL");
186 ret = -1;
187 goto close_fd;
188 }
189 }
190
191 sent = sendto(fd, (char *)mtracep, mtrace_buf_len, MSG_DONTWAIT,
192 (struct sockaddr *)&to, tolen);
193
194 if (sent != (ssize_t)mtrace_buf_len) {
195 char dst_str[INET_ADDRSTRLEN];
196 char group_str[INET_ADDRSTRLEN];
197
198 pim_inet4_dump("<dst?>", dst_addr, dst_str, sizeof(dst_str));
199 pim_inet4_dump("<group?>", group_addr, group_str,
200 sizeof(group_str));
201 if (sent < 0) {
202 if (PIM_DEBUG_MTRACE)
203 zlog_warn(
204 "Send mtrace request failed for %s on"
205 "%s: group=%s msg_size=%zd: errno=%d: "
206 " %s",
207 dst_str, ifp->name, group_str,
208 mtrace_buf_len, errno,
209 safe_strerror(errno));
210 } else {
211 if (PIM_DEBUG_MTRACE)
212 zlog_warn(
213 "Send mtrace request failed for %s on"
214 " %s: group=%s msg_size=%zd: sent=%zd",
215 dst_str, ifp->name, group_str,
216 mtrace_buf_len, sent);
217 }
218 ret = -1;
219 goto close_fd;
220 }
221 ret = 0;
222 close_fd:
223 close(fd);
224 return ret;
225 }
226
227 static int mtrace_un_forward_packet(struct pim_instance *pim, struct ip *ip_hdr,
228 struct interface *interface)
229 {
230 struct pim_nexthop nexthop;
231 struct sockaddr_in to;
232 struct interface *if_out;
233 socklen_t tolen;
234 int ret;
235 int fd;
236 int sent;
237 uint16_t checksum;
238
239 checksum = ip_hdr->ip_sum;
240
241 ip_hdr->ip_sum = 0;
242
243 if (checksum != in_cksum(ip_hdr, ip_hdr->ip_hl * 4))
244 return -1;
245
246 if (ip_hdr->ip_ttl-- <= 1)
247 return -1;
248
249 ip_hdr->ip_sum = in_cksum(ip_hdr, ip_hdr->ip_hl * 4);
250
251 fd = pim_socket_raw(IPPROTO_RAW);
252
253 if (fd < 0)
254 return -1;
255
256 pim_socket_ip_hdr(fd);
257
258 if (interface == NULL) {
259 ret = pim_nexthop_lookup(pim, &nexthop, ip_hdr->ip_dst, 0);
260
261 if (ret != 0) {
262 if (PIM_DEBUG_MTRACE)
263 zlog_warn(
264 "Dropping mtrace packet, "
265 "no route to destination");
266 return -1;
267 }
268
269 if_out = nexthop.interface;
270 } else {
271 if_out = interface;
272 }
273
274 ret = pim_socket_bind(fd, if_out);
275
276 if (ret < 0) {
277 close(fd);
278 return -1;
279 }
280
281 memset(&to, 0, sizeof(to));
282 to.sin_family = AF_INET;
283 to.sin_addr = ip_hdr->ip_dst;
284 tolen = sizeof(to);
285
286 sent = sendto(fd, ip_hdr, ntohs(ip_hdr->ip_len), 0,
287 (struct sockaddr *)&to, tolen);
288
289 close(fd);
290
291 if (sent < 0) {
292 if (PIM_DEBUG_MTRACE)
293 zlog_warn(
294 "Failed to forward mtrace packet:"
295 " sendto errno=%d, %s",
296 errno, safe_strerror(errno));
297 return -1;
298 }
299
300 if (PIM_DEBUG_MTRACE) {
301 zlog_debug("Fwd mtrace packet len=%u to %s ttl=%u",
302 ntohs(ip_hdr->ip_len), inet_ntoa(ip_hdr->ip_dst),
303 ip_hdr->ip_ttl);
304 }
305
306 return 0;
307 }
308
309 static int mtrace_mc_forward_packet(struct pim_instance *pim, struct ip *ip_hdr)
310 {
311 struct prefix_sg sg;
312 struct channel_oil *c_oil;
313 struct listnode *chnode;
314 struct listnode *chnextnode;
315 struct pim_ifchannel *ch = NULL;
316 int ret = -1;
317
318 memset(&sg, 0, sizeof(struct prefix_sg));
319 sg.grp = ip_hdr->ip_dst;
320
321 c_oil = pim_find_channel_oil(pim, &sg);
322
323 if (c_oil == NULL) {
324 if (PIM_DEBUG_MTRACE) {
325 zlog_debug(
326 "Dropping mtrace multicast packet "
327 "len=%u to %s ttl=%u",
328 ntohs(ip_hdr->ip_len),
329 inet_ntoa(ip_hdr->ip_dst), ip_hdr->ip_ttl);
330 }
331 return -1;
332 }
333 if (c_oil->up == NULL)
334 return -1;
335 if (c_oil->up->ifchannels == NULL)
336 return -1;
337 for (ALL_LIST_ELEMENTS(c_oil->up->ifchannels, chnode, chnextnode, ch)) {
338 if (pim_macro_chisin_oiflist(ch)) {
339 int r;
340
341 r = mtrace_un_forward_packet(pim, ip_hdr,
342 ch->interface);
343 if (r == 0)
344 ret = 0;
345 }
346 }
347 return ret;
348 }
349
350
351 static int mtrace_forward_packet(struct pim_instance *pim, struct ip *ip_hdr)
352 {
353 if (IPV4_CLASS_DE(ntohl(ip_hdr->ip_dst.s_addr)))
354 return mtrace_mc_forward_packet(pim, ip_hdr);
355 else
356 return mtrace_un_forward_packet(pim, ip_hdr, NULL);
357 }
358
359 /* 6.5 Sending Traceroute Responses */
360 static int mtrace_send_mc_response(struct pim_instance *pim,
361 struct igmp_mtrace *mtracep,
362 size_t mtrace_len)
363 {
364 struct prefix_sg sg;
365 struct channel_oil *c_oil;
366 struct listnode *chnode;
367 struct listnode *chnextnode;
368 struct pim_ifchannel *ch = NULL;
369 int ret = -1;
370
371 memset(&sg, 0, sizeof(struct prefix_sg));
372 sg.grp = mtracep->rsp_addr;
373
374 c_oil = pim_find_channel_oil(pim, &sg);
375
376 if (c_oil == NULL) {
377 if (PIM_DEBUG_MTRACE) {
378 zlog_debug(
379 "Dropping mtrace multicast response packet "
380 "len=%u to %s",
381 (unsigned int)mtrace_len,
382 inet_ntoa(mtracep->rsp_addr));
383 }
384 return -1;
385 }
386 if (c_oil->up == NULL)
387 return -1;
388 if (c_oil->up->ifchannels == NULL)
389 return -1;
390 for (ALL_LIST_ELEMENTS(c_oil->up->ifchannels, chnode, chnextnode, ch)) {
391 if (pim_macro_chisin_oiflist(ch)) {
392 int r;
393
394 r = mtrace_send_packet(ch->interface, mtracep,
395 mtrace_len, mtracep->rsp_addr,
396 mtracep->grp_addr);
397 if (r == 0)
398 ret = 0;
399 }
400 }
401 return ret;
402 }
403
404 static int mtrace_send_response(struct pim_instance *pim,
405 struct igmp_mtrace *mtracep, size_t mtrace_len)
406 {
407 struct pim_nexthop nexthop;
408 int ret;
409
410 mtracep->type = PIM_IGMP_MTRACE_RESPONSE;
411
412 mtracep->checksum = 0;
413 mtracep->checksum = in_cksum((char *)mtracep, mtrace_len);
414
415 if (IPV4_CLASS_DE(ntohl(mtracep->rsp_addr.s_addr))) {
416 struct pim_rpf *p_rpf;
417 char grp_str[INET_ADDRSTRLEN];
418
419 if (pim_rp_i_am_rp(pim, mtracep->rsp_addr))
420 return mtrace_send_mc_response(pim, mtracep,
421 mtrace_len);
422
423 p_rpf = pim_rp_g(pim, mtracep->rsp_addr);
424
425 if (p_rpf == NULL) {
426 if (PIM_DEBUG_MTRACE)
427 zlog_warn("mtrace no RP for %s",
428 inet_ntop(AF_INET,
429 &(mtracep->rsp_addr),
430 grp_str, sizeof(grp_str)));
431 return -1;
432 }
433 nexthop = p_rpf->source_nexthop;
434 if (PIM_DEBUG_MTRACE)
435 zlog_debug("mtrace response to RP");
436 } else {
437 /* TODO: should use unicast rib lookup */
438 ret = pim_nexthop_lookup(pim, &nexthop, mtracep->rsp_addr, 1);
439
440 if (ret != 0) {
441 if (PIM_DEBUG_MTRACE)
442 zlog_warn(
443 "Dropped response qid=%ud, no route to "
444 "response address",
445 mtracep->qry_id);
446 return -1;
447 }
448 }
449
450 return mtrace_send_packet(nexthop.interface, mtracep, mtrace_len,
451 mtracep->rsp_addr, mtracep->grp_addr);
452 }
453
454 int igmp_mtrace_recv_qry_req(struct igmp_sock *igmp, struct ip *ip_hdr,
455 struct in_addr from, const char *from_str,
456 char *igmp_msg, int igmp_msg_len)
457 {
458 static uint32_t qry_id, qry_src;
459 char mtrace_buf[MTRACE_HDR_SIZE + MTRACE_MAX_HOPS * MTRACE_RSP_SIZE];
460 struct pim_nexthop nexthop;
461 struct interface *ifp;
462 struct interface *out_ifp;
463 struct pim_interface *pim_ifp;
464 struct pim_interface *pim_out_ifp;
465 struct pim_instance *pim;
466 struct igmp_mtrace *mtracep;
467 struct igmp_mtrace_rsp *rspp;
468 struct in_addr nh_addr;
469 enum mtrace_fwd_code fwd_code = MTRACE_FWD_CODE_NO_ERROR;
470 int ret;
471 size_t r_len;
472 int last_rsp_ind = 0;
473 size_t mtrace_len;
474 uint16_t recv_checksum;
475 uint16_t checksum;
476
477 ifp = igmp->interface;
478 pim_ifp = ifp->info;
479 pim = pim_ifp->pim;
480
481 /*
482 * 6. Router Behaviour
483 * Check if mtrace packet is addressed elsewhere and forward,
484 * if applicable
485 */
486 if (!IPV4_CLASS_DE(ntohl(ip_hdr->ip_dst.s_addr)))
487 if (!if_lookup_exact_address(&ip_hdr->ip_dst, AF_INET,
488 pim->vrf_id))
489 return mtrace_forward_packet(pim, ip_hdr);
490
491 if (igmp_msg_len < (int)sizeof(struct igmp_mtrace)) {
492 if (PIM_DEBUG_MTRACE)
493 zlog_warn(
494 "Recv mtrace packet from %s on %s: too short,"
495 " len=%d, min=%lu",
496 from_str, ifp->name, igmp_msg_len,
497 sizeof(struct igmp_mtrace));
498 return -1;
499 }
500
501 mtracep = (struct igmp_mtrace *)igmp_msg;
502
503 recv_checksum = mtracep->checksum;
504
505 mtracep->checksum = 0;
506
507 checksum = in_cksum(igmp_msg, igmp_msg_len);
508
509 if (recv_checksum != checksum) {
510 if (PIM_DEBUG_MTRACE)
511 zlog_warn(
512 "Recv mtrace packet from %s on %s: checksum"
513 " mismatch: received=%x computed=%x",
514 from_str, ifp->name, recv_checksum, checksum);
515 return -1;
516 }
517
518 if (PIM_DEBUG_MTRACE)
519 mtrace_debug(pim_ifp, mtracep, igmp_msg_len);
520
521 /* subtract header from message length */
522 r_len = igmp_msg_len - sizeof(struct igmp_mtrace);
523
524 /* Classify mtrace packet, check if it is a query */
525 if (!r_len) {
526 if (PIM_DEBUG_MTRACE)
527 zlog_debug("Received IGMP multicast traceroute query");
528
529 /* 6.1.1 Packet verification */
530 if (!pim_if_connected_to_source(ifp, mtracep->dst_addr)) {
531 if (IPV4_CLASS_DE(ntohl(ip_hdr->ip_dst.s_addr))) {
532 if (PIM_DEBUG_MTRACE)
533 zlog_debug(
534 "Dropping multicast query "
535 "on wrong interface");
536 return -1;
537 }
538 /* Unicast query on wrong interface */
539 fwd_code = MTRACE_FWD_CODE_WRONG_IF;
540 }
541 if (qry_id == mtracep->qry_id && qry_src == from.s_addr) {
542 if (PIM_DEBUG_MTRACE)
543 zlog_debug(
544 "Dropping multicast query with "
545 "duplicate source and id");
546 return -1;
547 }
548 qry_id = mtracep->qry_id;
549 qry_src = from.s_addr;
550 }
551 /* if response fields length is equal to a whole number of responses */
552 else if ((r_len % sizeof(struct igmp_mtrace_rsp)) == 0) {
553 r_len = igmp_msg_len - sizeof(struct igmp_mtrace);
554
555 if (r_len != 0)
556 last_rsp_ind = r_len / sizeof(struct igmp_mtrace_rsp);
557 if (last_rsp_ind > MTRACE_MAX_HOPS) {
558 if (PIM_DEBUG_MTRACE)
559 zlog_warn("Mtrace request of excessive size");
560 return -1;
561 }
562 } else {
563 if (PIM_DEBUG_MTRACE)
564 zlog_warn(
565 "Recv mtrace packet from %s on %s: "
566 "invalid length %d",
567 from_str, ifp->name, igmp_msg_len);
568 return -1;
569 }
570
571 /* 6.2.1 Packet Verification - drop not link-local multicast */
572 if (IPV4_CLASS_DE(ntohl(ip_hdr->ip_dst.s_addr))
573 && !IPV4_MC_LINKLOCAL(ntohl(ip_hdr->ip_dst.s_addr))) {
574 if (PIM_DEBUG_MTRACE)
575 zlog_warn(
576 "Recv mtrace packet from %s on %s:"
577 " not link-local multicast %s",
578 from_str, ifp->name, inet_ntoa(ip_hdr->ip_dst));
579 return -1;
580 }
581
582 /* 6.2.2. Normal Processing */
583
584 /* 6.2.2. 1. */
585
586 if (last_rsp_ind == MTRACE_MAX_HOPS) {
587 mtracep->rsp[MTRACE_MAX_HOPS - 1].fwd_code =
588 MTRACE_FWD_CODE_NO_SPACE;
589 return mtrace_send_response(pim_ifp->pim, mtracep,
590 igmp_msg_len);
591 }
592
593 /* calculate new mtrace mtrace lenght with extra response */
594 mtrace_len = igmp_msg_len + sizeof(struct igmp_mtrace_rsp);
595
596 /* copy received query/request */
597 memcpy(mtrace_buf, igmp_msg, igmp_msg_len);
598
599 /* repoint mtracep pointer to copy */
600 mtracep = (struct igmp_mtrace *)mtrace_buf;
601
602 /* pointer for extra response field to be filled in */
603 rspp = &mtracep->rsp[last_rsp_ind];
604
605 /* initialize extra response field */
606 mtrace_rsp_init(rspp);
607
608 rspp->arrival = htonl(query_arrival_time());
609 rspp->outgoing = pim_ifp->primary_address;
610 rspp->out_count = htonl(MTRACE_UNKNOWN_COUNT);
611
612 /* 6.2.2. 2. Attempt to determine forwarding information */
613
614 nh_addr.s_addr = 0;
615
616 ret = pim_nexthop_lookup(pim, &nexthop, mtracep->src_addr, 1);
617
618 if (ret == 0) {
619 char nexthop_str[INET_ADDRSTRLEN];
620
621 if (PIM_DEBUG_MTRACE)
622 zlog_debug("mtrace pim_nexthop_lookup OK");
623
624 if (PIM_DEBUG_MTRACE)
625 zlog_warn("mtrace next_hop=%s",
626 inet_ntop(nexthop.mrib_nexthop_addr.family,
627 &nexthop.mrib_nexthop_addr.u.prefix,
628 nexthop_str, sizeof(nexthop_str)));
629
630 if (nexthop.mrib_nexthop_addr.family == AF_INET)
631 nh_addr = nexthop.mrib_nexthop_addr.u.prefix4;
632 }
633 /* 6.4 Forwarding Traceroute Requests: ... Otherwise, ... */
634 else {
635 if (PIM_DEBUG_MTRACE)
636 zlog_debug("mtrace not found neighbor");
637 if (!fwd_code)
638 rspp->fwd_code = MTRACE_FWD_CODE_NO_ROUTE;
639 else
640 rspp->fwd_code = fwd_code;
641 /* 6.5 Sending Traceroute Responses */
642 return mtrace_send_response(pim, mtracep, mtrace_len);
643 }
644
645 out_ifp = nexthop.interface;
646 pim_out_ifp = out_ifp->info;
647
648 rspp->incoming = pim_out_ifp->primary_address;
649 rspp->prev_hop = nh_addr;
650 rspp->in_count = htonl(MTRACE_UNKNOWN_COUNT);
651 rspp->total = htonl(MTRACE_UNKNOWN_COUNT);
652 rspp->rtg_proto = MTRACE_RTG_PROTO_PIM;
653 rspp->s = 1;
654 rspp->src_mask = 32;
655
656 if (nh_addr.s_addr == 0) {
657 /* reached source? */
658 if (pim_if_connected_to_source(out_ifp, mtracep->src_addr))
659 return mtrace_send_response(pim, mtracep, mtrace_len);
660 /*
661 * 6.4 Forwarding Traceroute Requests:
662 * Previous-hop router not known
663 */
664 inet_aton(MCAST_ALL_ROUTERS, &nh_addr);
665 }
666
667 if (mtracep->hops <= (last_rsp_ind + 1))
668 return mtrace_send_response(pim, mtracep, mtrace_len);
669
670 mtracep->checksum = 0;
671
672 mtracep->checksum = in_cksum(mtrace_buf, mtrace_len);
673
674 return mtrace_send_packet(out_ifp, mtracep, mtrace_len, nh_addr,
675 mtracep->grp_addr);
676 }
677
678 int igmp_mtrace_recv_response(struct igmp_sock *igmp, struct ip *ip_hdr,
679 struct in_addr from, const char *from_str,
680 char *igmp_msg, int igmp_msg_len)
681 {
682 static uint32_t qry_id, rsp_dst;
683 struct interface *ifp;
684 struct pim_interface *pim_ifp;
685 struct pim_instance *pim;
686 struct igmp_mtrace *mtracep;
687 uint16_t recv_checksum;
688 uint16_t checksum;
689
690 ifp = igmp->interface;
691 pim_ifp = ifp->info;
692 pim = pim_ifp->pim;
693
694 mtracep = (struct igmp_mtrace *)igmp_msg;
695
696 recv_checksum = mtracep->checksum;
697
698 mtracep->checksum = 0;
699
700 checksum = in_cksum(igmp_msg, igmp_msg_len);
701
702 if (recv_checksum != checksum) {
703 if (PIM_DEBUG_MTRACE)
704 zlog_warn(
705 "Recv mtrace response from %s on %s: checksum"
706 " mismatch: received=%x computed=%x",
707 from_str, ifp->name, recv_checksum, checksum);
708 return -1;
709 }
710
711 mtracep->checksum = checksum;
712
713 if (PIM_DEBUG_MTRACE)
714 mtrace_debug(pim_ifp, mtracep, igmp_msg_len);
715
716 /* Drop duplicate packets */
717 if (qry_id == mtracep->qry_id && rsp_dst == ip_hdr->ip_dst.s_addr) {
718 if (PIM_DEBUG_MTRACE)
719 zlog_debug("duplicate mtrace response packet dropped");
720 return -1;
721 }
722
723 qry_id = mtracep->qry_id;
724 rsp_dst = ip_hdr->ip_dst.s_addr;
725
726 return mtrace_forward_packet(pim, ip_hdr);
727 }