back to topotato report
topotato coverage report
Current view: top level - pimd - pim_msg.c (source / functions) Hit Total Coverage
Test: test_pim6_basic.py::PIM6Basic Lines: 81 137 59.1 %
Date: 2023-02-24 18:38:38 Functions: 4 12 33.3 %

          Line data    Source code
       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 "if.h"
      23             : #include "log.h"
      24             : #include "prefix.h"
      25             : #include "vty.h"
      26             : #include "plist.h"
      27             : 
      28             : #include "pimd.h"
      29             : #include "pim_instance.h"
      30             : #include "pim_vty.h"
      31             : #include "pim_pim.h"
      32             : #include "pim_msg.h"
      33             : #include "pim_util.h"
      34             : #include "pim_str.h"
      35             : #include "pim_iface.h"
      36             : #include "pim_rp.h"
      37             : #include "pim_rpf.h"
      38             : #include "pim_register.h"
      39             : #include "pim_jp_agg.h"
      40             : #include "pim_oil.h"
      41             : 
      42          29 : void pim_msg_build_header(pim_addr src, pim_addr dst, uint8_t *pim_msg,
      43             :                           size_t pim_msg_size, uint8_t pim_msg_type,
      44             :                           bool no_fwd)
      45             : {
      46          29 :         struct pim_msg_header *header = (struct pim_msg_header *)pim_msg;
      47          29 :         struct iovec iov[2], *iovp = iov;
      48             : 
      49             :         /*
      50             :          * The checksum for Registers is done only on the first 8 bytes of the
      51             :          * packet, including the PIM header and the next 4 bytes, excluding the
      52             :          * data packet portion
      53             :          *
      54             :          * for IPv6, the pseudoheader upper-level protocol length is also
      55             :          * truncated, so let's just set it here before everything else.
      56             :          */
      57          29 :         if (pim_msg_type == PIM_MSG_TYPE_REGISTER)
      58           0 :                 pim_msg_size = PIM_MSG_REGISTER_LEN;
      59             : 
      60             : #if PIM_IPV == 6
      61          29 :         struct ipv6_ph phdr = {
      62             :                 .src = src,
      63             :                 .dst = dst,
      64          29 :                 .ulpl = htonl(pim_msg_size),
      65             :                 .next_hdr = IPPROTO_PIM,
      66             :         };
      67             : 
      68          29 :         iovp->iov_base = &phdr;
      69          29 :         iovp->iov_len = sizeof(phdr);
      70          29 :         iovp++;
      71             : #endif
      72             : 
      73             :         /*
      74             :          * Write header
      75             :          */
      76          29 :         header->ver = PIM_PROTO_VERSION;
      77          29 :         header->type = pim_msg_type;
      78          29 :         header->Nbit = no_fwd;
      79          29 :         header->reserved = 0;
      80             : 
      81          29 :         header->checksum = 0;
      82          29 :         iovp->iov_base = header;
      83          29 :         iovp->iov_len = pim_msg_size;
      84          29 :         iovp++;
      85             : 
      86          29 :         header->checksum = in_cksumv(iov, iovp - iov);
      87          29 : }
      88             : 
      89           0 : uint8_t *pim_msg_addr_encode_ipv4_ucast(uint8_t *buf, struct in_addr addr)
      90             : {
      91           0 :         buf[0] = PIM_MSG_ADDRESS_FAMILY_IPV4; /* addr family */
      92           0 :         buf[1] = '\0';                        /* native encoding */
      93           0 :         memcpy(buf + 2, &addr, sizeof(struct in_addr));
      94             : 
      95           0 :         return buf + PIM_ENCODED_IPV4_UCAST_SIZE;
      96             : }
      97             : 
      98           0 : uint8_t *pim_msg_addr_encode_ipv4_group(uint8_t *buf, struct in_addr addr)
      99             : {
     100           0 :         buf[0] = PIM_MSG_ADDRESS_FAMILY_IPV4; /* addr family */
     101           0 :         buf[1] = '\0';                        /* native encoding */
     102           0 :         buf[2] = '\0';                        /* reserved */
     103           0 :         buf[3] = 32;                          /* mask len */
     104           0 :         memcpy(buf + 4, &addr, sizeof(struct in_addr));
     105             : 
     106           0 :         return buf + PIM_ENCODED_IPV4_GROUP_SIZE;
     107             : }
     108             : 
     109           0 : uint8_t *pim_msg_addr_encode_ipv4_source(uint8_t *buf, struct in_addr addr,
     110             :                                          uint8_t bits)
     111             : {
     112           0 :         buf[0] = PIM_MSG_ADDRESS_FAMILY_IPV4; /* addr family */
     113           0 :         buf[1] = '\0';                        /* native encoding */
     114           0 :         buf[2] = bits;
     115           0 :         buf[3] = 32; /* mask len */
     116           0 :         memcpy(buf + 4, &addr, sizeof(struct in_addr));
     117             : 
     118           0 :         return buf + PIM_ENCODED_IPV4_SOURCE_SIZE;
     119             : }
     120             : 
     121           2 : uint8_t *pim_msg_addr_encode_ipv6_source(uint8_t *buf, struct in6_addr addr,
     122             :                                          uint8_t bits)
     123             : {
     124           2 :         buf[0] = PIM_MSG_ADDRESS_FAMILY_IPV6; /* addr family */
     125           2 :         buf[1] = '\0';                        /* native encoding */
     126           2 :         buf[2] = bits;
     127           2 :         buf[3] = 128; /* mask len */
     128           2 :         buf += 4;
     129             : 
     130           2 :         memcpy(buf, &addr, sizeof(addr));
     131           2 :         buf += sizeof(addr);
     132             : 
     133           2 :         return buf;
     134             : }
     135             : 
     136           2 : uint8_t *pim_msg_addr_encode_ipv6_ucast(uint8_t *buf, struct in6_addr addr)
     137             : {
     138           2 :         buf[0] = PIM_MSG_ADDRESS_FAMILY_IPV6; /* addr family */
     139           2 :         buf[1] = '\0';                        /* native encoding */
     140           2 :         buf += 2;
     141             : 
     142           2 :         memcpy(buf, &addr, sizeof(addr));
     143           2 :         buf += sizeof(addr);
     144             : 
     145           2 :         return buf;
     146             : }
     147             : 
     148           2 : uint8_t *pim_msg_addr_encode_ipv6_group(uint8_t *buf, struct in6_addr addr)
     149             : {
     150           2 :         buf[0] = PIM_MSG_ADDRESS_FAMILY_IPV6; /* addr family */
     151           2 :         buf[1] = '\0';                        /* native encoding */
     152           2 :         buf[2] = '\0';                        /* reserved */
     153           2 :         buf[3] = 128;                         /* mask len */
     154           2 :         buf += 4;
     155             : 
     156           2 :         memcpy(buf, &addr, sizeof(addr));
     157           2 :         buf += sizeof(addr);
     158             : 
     159           2 :         return buf;
     160             : }
     161             : 
     162             : #if PIM_IPV == 4
     163             : #define pim_msg_addr_encode(what) pim_msg_addr_encode_ipv4_##what
     164             : #else
     165             : #define pim_msg_addr_encode(what) pim_msg_addr_encode_ipv6_##what
     166             : #endif
     167             : 
     168           2 : uint8_t *pim_msg_addr_encode_ucast(uint8_t *buf, pim_addr addr)
     169             : {
     170           2 :         return pim_msg_addr_encode(ucast)(buf, addr);
     171             : }
     172             : 
     173           2 : uint8_t *pim_msg_addr_encode_group(uint8_t *buf, pim_addr addr)
     174             : {
     175           2 :         return pim_msg_addr_encode(group)(buf, addr);
     176             : }
     177             : 
     178           2 : uint8_t *pim_msg_addr_encode_source(uint8_t *buf, pim_addr addr, uint8_t bits)
     179             : {
     180           2 :         return pim_msg_addr_encode(source)(buf, addr, bits);
     181             : }
     182             : 
     183             : /*
     184             :  * For the given 'struct pim_jp_sources' list
     185             :  * determine the size_t it would take up.
     186             :  */
     187           2 : size_t pim_msg_get_jp_group_size(struct list *sources)
     188             : {
     189           2 :         struct pim_jp_sources *js;
     190           2 :         size_t size = 0;
     191             : 
     192           2 :         if (!sources)
     193             :                 return 0;
     194             : 
     195           2 :         size += sizeof(pim_encoded_group);
     196           2 :         size += 4; // Joined sources (2) + Pruned Sources (2)
     197             : 
     198           2 :         size += sizeof(pim_encoded_source) * sources->count;
     199             : 
     200           2 :         js = listgetdata(listhead(sources));
     201           2 :         if (js && pim_addr_is_any(js->up->sg.src) && js->is_join) {
     202           0 :                 struct pim_upstream *child, *up;
     203           0 :                 struct listnode *up_node;
     204             : 
     205           0 :                 up = js->up;
     206           0 :                 if (PIM_DEBUG_PIM_PACKETS)
     207           0 :                         zlog_debug(
     208             :                                 "%s: Considering (%s) children for (S,G,rpt) prune",
     209             :                                 __func__, up->sg_str);
     210             : 
     211           0 :                 for (ALL_LIST_ELEMENTS_RO(up->sources, up_node, child)) {
     212           0 :                         if (!PIM_UPSTREAM_FLAG_TEST_USE_RPT(child->flags)) {
     213             :                                 /* If we are using SPT and the SPT and RPT IIFs
     214             :                                  * are different we can prune the source off
     215             :                                  * of the RPT.
     216             :                                  * If RPF_interface(S) is not resolved hold
     217             :                                  * decision to prune as SPT may end up on the
     218             :                                  * same IIF as RPF_interface(RP).
     219             :                                  */
     220           0 :                                 if (child->rpf.source_nexthop.interface &&
     221           0 :                                         !pim_rpf_is_same(&up->rpf,
     222             :                                                 &child->rpf)) {
     223           0 :                                         size += sizeof(pim_encoded_source);
     224           0 :                                         PIM_UPSTREAM_FLAG_SET_SEND_SG_RPT_PRUNE(
     225             :                                                 child->flags);
     226           0 :                                         if (PIM_DEBUG_PIM_PACKETS)
     227           0 :                                                 zlog_debug(
     228             :                                                         "%s: SPT Bit and RPF'(%s) != RPF'(S,G): Add Prune (%s,rpt) to compound message",
     229             :                                                         __func__, up->sg_str,
     230             :                                                         child->sg_str);
     231           0 :                                 } else if (PIM_DEBUG_PIM_PACKETS)
     232           0 :                                         zlog_debug(
     233             :                                                 "%s: SPT Bit and RPF'(%s) == RPF'(S,G): Not adding Prune for (%s,rpt)",
     234             :                                                 __func__, up->sg_str,
     235             :                                                 child->sg_str);
     236           0 :                         } else if (pim_upstream_empty_inherited_olist(child)) {
     237             :                                 /* S is supposed to be forwarded along the RPT
     238             :                                  * but it's inherited OIL is empty. So just
     239             :                                  * prune it off.
     240             :                                  */
     241           0 :                                 size += sizeof(pim_encoded_source);
     242           0 :                                 PIM_UPSTREAM_FLAG_SET_SEND_SG_RPT_PRUNE(
     243             :                                                 child->flags);
     244           0 :                                 if (PIM_DEBUG_PIM_PACKETS)
     245           0 :                                         zlog_debug(
     246             :                                                 "%s: inherited_olist(%s,rpt) is NULL, Add Prune to compound message",
     247             :                                                 __func__, child->sg_str);
     248           0 :                         } else if (PIM_DEBUG_PIM_PACKETS)
     249           0 :                                 zlog_debug(
     250             :                                         "%s: Do not add Prune %s to compound message %s",
     251             :                                         __func__, child->sg_str, up->sg_str);
     252             :                 }
     253             :         }
     254             :         return size;
     255             : }
     256             : 
     257           2 : size_t pim_msg_build_jp_groups(struct pim_jp_groups *grp,
     258             :                                struct pim_jp_agg_group *sgs, size_t size)
     259             : {
     260           2 :         struct listnode *node, *nnode;
     261           2 :         struct pim_jp_sources *source;
     262           2 :         struct pim_upstream *up = NULL;
     263           2 :         pim_addr stosend;
     264           2 :         uint8_t bits;
     265           2 :         uint8_t tgroups = 0;
     266             : 
     267           2 :         memset(grp, 0, size);
     268           2 :         pim_msg_addr_encode_group((uint8_t *)&grp->g, sgs->group);
     269             : 
     270           6 :         for (ALL_LIST_ELEMENTS(sgs->sources, node, nnode, source)) {
     271             :                 /* number of joined/pruned sources */
     272           2 :                 if (source->is_join)
     273           1 :                         grp->joins++;
     274             :                 else
     275           1 :                         grp->prunes++;
     276             : 
     277           2 :                 if (pim_addr_is_any(source->up->sg.src)) {
     278           0 :                         struct pim_instance *pim = source->up->channel_oil->pim;
     279           0 :                         struct pim_rpf *rpf = pim_rp_g(pim, source->up->sg.grp);
     280           0 :                         bits = PIM_ENCODE_SPARSE_BIT | PIM_ENCODE_WC_BIT
     281             :                                | PIM_ENCODE_RPT_BIT;
     282           0 :                         stosend = rpf->rpf_addr;
     283             :                         /* Only Send SGRpt in case of *,G Join */
     284           0 :                         if (source->is_join)
     285           0 :                                 up = source->up;
     286             :                 } else {
     287           2 :                         bits = PIM_ENCODE_SPARSE_BIT;
     288           2 :                         stosend = source->up->sg.src;
     289             :                 }
     290             : 
     291           2 :                 pim_msg_addr_encode_source((uint8_t *)&grp->s[tgroups], stosend,
     292             :                                            bits);
     293           2 :                 tgroups++;
     294             :         }
     295             : 
     296           2 :         if (up) {
     297           0 :                 struct pim_upstream *child;
     298             : 
     299           0 :                 for (ALL_LIST_ELEMENTS(up->sources, node, nnode, child)) {
     300           0 :                         if (PIM_UPSTREAM_FLAG_TEST_SEND_SG_RPT_PRUNE(
     301             :                                     child->flags)) {
     302           0 :                                 pim_msg_addr_encode_source(
     303           0 :                                         (uint8_t *)&grp->s[tgroups],
     304             :                                         child->sg.src,
     305             :                                         PIM_ENCODE_SPARSE_BIT |
     306             :                                                 PIM_ENCODE_RPT_BIT);
     307           0 :                                 tgroups++;
     308           0 :                                 PIM_UPSTREAM_FLAG_UNSET_SEND_SG_RPT_PRUNE(
     309             :                                         child->flags);
     310           0 :                                 grp->prunes++;
     311             :                         }
     312             :                 }
     313             :         }
     314             : 
     315           2 :         grp->joins = htons(grp->joins);
     316           2 :         grp->prunes = htons(grp->prunes);
     317             : 
     318           2 :         return size;
     319             : }

Generated by: LCOV version v1.16-topotato