back to topotato report
topotato coverage report
Current view: top level - ospfd - ospf_network.c (source / functions) Hit Total Coverage
Test: test_ospf_topo1.py::OSPFTopo1Test Lines: 55 66 83.3 %
Date: 2023-02-24 18:38:32 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /*
       2             :  * OSPF network related functions
       3             :  *   Copyright (C) 1999 Toshiaki Takada
       4             :  *
       5             :  * This file is part of GNU Zebra.
       6             :  *
       7             :  * GNU Zebra is free software; you can redistribute it and/or modify it
       8             :  * under the terms of the GNU General Public License as published by the
       9             :  * Free Software Foundation; either version 2, or (at your option) any
      10             :  * later version.
      11             :  *
      12             :  * GNU Zebra is distributed in the hope that it will be useful, but
      13             :  * WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU General Public License along
      18             :  * with this program; see the file COPYING; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : #include <zebra.h>
      23             : 
      24             : #include "thread.h"
      25             : #include "linklist.h"
      26             : #include "prefix.h"
      27             : #include "if.h"
      28             : #include "sockunion.h"
      29             : #include "log.h"
      30             : #include "sockopt.h"
      31             : #include "privs.h"
      32             : #include "lib_errors.h"
      33             : 
      34             : #include "ospfd/ospfd.h"
      35             : #include "ospfd/ospf_network.h"
      36             : #include "ospfd/ospf_interface.h"
      37             : #include "ospfd/ospf_asbr.h"
      38             : #include "ospfd/ospf_lsa.h"
      39             : #include "ospfd/ospf_lsdb.h"
      40             : #include "ospfd/ospf_neighbor.h"
      41             : #include "ospfd/ospf_packet.h"
      42             : #include "ospfd/ospf_dump.h"
      43             : 
      44             : /* Join to the OSPF ALL SPF ROUTERS multicast group. */
      45          18 : int ospf_if_add_allspfrouters(struct ospf *top, struct prefix *p,
      46             :                               ifindex_t ifindex)
      47             : {
      48          18 :         int ret;
      49             : 
      50          18 :         ret = setsockopt_ipv4_multicast(top->fd, IP_ADD_MEMBERSHIP,
      51             :                                         p->u.prefix4, htonl(OSPF_ALLSPFROUTERS),
      52             :                                         ifindex);
      53          18 :         if (ret < 0)
      54           0 :                 flog_err(
      55             :                         EC_LIB_SOCKET,
      56             :                         "can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %pI4, ifindex %u, AllSPFRouters): %s; perhaps a kernel limit on # of multicast group memberships has been exceeded?",
      57             :                         top->fd, &p->u.prefix4, ifindex,
      58             :                         safe_strerror(errno));
      59             :         else {
      60          18 :                 if (IS_DEBUG_OSPF_EVENT)
      61          18 :                         zlog_debug(
      62             :                                 "interface %pI4 [%u] join AllSPFRouters Multicast group.",
      63             :                                 &p->u.prefix4, ifindex);
      64             :         }
      65             : 
      66          18 :         return ret;
      67             : }
      68             : 
      69          18 : int ospf_if_drop_allspfrouters(struct ospf *top, struct prefix *p,
      70             :                                ifindex_t ifindex)
      71             : {
      72          18 :         int ret;
      73             : 
      74          18 :         ret = setsockopt_ipv4_multicast(top->fd, IP_DROP_MEMBERSHIP,
      75             :                                         p->u.prefix4, htonl(OSPF_ALLSPFROUTERS),
      76             :                                         ifindex);
      77          18 :         if (ret < 0)
      78           0 :                 flog_err(EC_LIB_SOCKET,
      79             :                          "can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %pI4, ifindex %u, AllSPFRouters): %s",
      80             :                          top->fd, &p->u.prefix4, ifindex,
      81             :                          safe_strerror(errno));
      82             :         else {
      83          18 :                 if (IS_DEBUG_OSPF_EVENT)
      84          18 :                         zlog_debug(
      85             :                                 "interface %pI4 [%u] leave AllSPFRouters Multicast group.",
      86             :                                 &p->u.prefix4, ifindex);
      87             :         }
      88             : 
      89          18 :         return ret;
      90             : }
      91             : 
      92             : /* Join to the OSPF ALL Designated ROUTERS multicast group. */
      93           9 : int ospf_if_add_alldrouters(struct ospf *top, struct prefix *p,
      94             :                             ifindex_t ifindex)
      95             : {
      96           9 :         int ret;
      97             : 
      98           9 :         ret = setsockopt_ipv4_multicast(top->fd, IP_ADD_MEMBERSHIP,
      99             :                                         p->u.prefix4, htonl(OSPF_ALLDROUTERS),
     100             :                                         ifindex);
     101           9 :         if (ret < 0)
     102           0 :                 flog_err(
     103             :                         EC_LIB_SOCKET,
     104             :                         "can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %pI4, ifindex %u, AllDRouters): %s; perhaps a kernel limit on # of multicast group memberships has been exceeded?",
     105             :                         top->fd, &p->u.prefix4, ifindex,
     106             :                         safe_strerror(errno));
     107             :         else {
     108           9 :                 if (IS_DEBUG_OSPF_EVENT)
     109           9 :                         zlog_debug(
     110             :                                 "interface %pI4 [%u] join AllDRouters Multicast group.",
     111             :                                 &p->u.prefix4, ifindex);
     112             :         }
     113           9 :         return ret;
     114             : }
     115             : 
     116           9 : int ospf_if_drop_alldrouters(struct ospf *top, struct prefix *p,
     117             :                              ifindex_t ifindex)
     118             : {
     119           9 :         int ret;
     120             : 
     121           9 :         ret = setsockopt_ipv4_multicast(top->fd, IP_DROP_MEMBERSHIP,
     122             :                                         p->u.prefix4, htonl(OSPF_ALLDROUTERS),
     123             :                                         ifindex);
     124           9 :         if (ret < 0)
     125           0 :                 flog_err(EC_LIB_SOCKET,
     126             :                          "can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %pI4, ifindex %u, AllDRouters): %s",
     127             :                          top->fd, &p->u.prefix4, ifindex,
     128             :                          safe_strerror(errno));
     129             :         else
     130           9 :                 zlog_debug(
     131             :                         "interface %pI4 [%u] leave AllDRouters Multicast group.",
     132             :                         &p->u.prefix4, ifindex);
     133             : 
     134           9 :         return ret;
     135             : }
     136             : 
     137         193 : int ospf_if_ipmulticast(struct ospf *top, struct prefix *p, ifindex_t ifindex)
     138             : {
     139         193 :         uint8_t val;
     140         193 :         int ret, len;
     141             : 
     142             :         /* Prevent receiving self-origined multicast packets. */
     143         193 :         ret = setsockopt_ipv4_multicast_loop(top->fd, 0);
     144         193 :         if (ret < 0)
     145           0 :                 flog_err(EC_LIB_SOCKET,
     146             :                          "can't setsockopt IP_MULTICAST_LOOP(0) for fd %d: %s",
     147             :                          top->fd, safe_strerror(errno));
     148             : 
     149             :         /* Explicitly set multicast ttl to 1 -- endo. */
     150         193 :         val = 1;
     151         193 :         len = sizeof(val);
     152         193 :         ret = setsockopt(top->fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&val,
     153             :                          len);
     154         193 :         if (ret < 0)
     155           0 :                 flog_err(EC_LIB_SOCKET,
     156             :                          "can't setsockopt IP_MULTICAST_TTL(1) for fd %d: %s",
     157             :                          top->fd, safe_strerror(errno));
     158             : #ifndef GNU_LINUX
     159             :         /* For GNU LINUX ospf_write uses IP_PKTINFO, in_pktinfo to send
     160             :          * packet out of ifindex. Below would be used Non Linux system.
     161             :          */
     162             :         ret = setsockopt_ipv4_multicast_if(top->fd, p->u.prefix4, ifindex);
     163             :         if (ret < 0)
     164             :                 flog_err(EC_LIB_SOCKET,
     165             :                          "can't setsockopt IP_MULTICAST_IF(fd %d, addr %pI4, ifindex %u): %s",
     166             :                          top->fd, &p->u.prefix4, ifindex,
     167             :                          safe_strerror(errno));
     168             : #endif
     169             : 
     170         193 :         return ret;
     171             : }
     172             : 
     173           4 : int ospf_sock_init(struct ospf *ospf)
     174             : {
     175           4 :         int ospf_sock;
     176           4 :         int ret, hincl = 1;
     177           4 :         int bufsize = (8 * 1024 * 1024);
     178             : 
     179             :         /* silently ignore. already done */
     180           4 :         if (ospf->fd > 0)
     181             :                 return -1;
     182             : 
     183           4 :         if (ospf->vrf_id == VRF_UNKNOWN) {
     184             :                 /* silently return since VRF is not ready */
     185             :                 return -1;
     186             :         }
     187           8 :         frr_with_privs(&ospfd_privs) {
     188           8 :                 ospf_sock = vrf_socket(AF_INET, SOCK_RAW, IPPROTO_OSPFIGP,
     189           4 :                                        ospf->vrf_id, ospf->name);
     190           4 :                 if (ospf_sock < 0) {
     191           0 :                         flog_err(EC_LIB_SOCKET,
     192             :                                  "ospf_read_sock_init: socket: %s",
     193             :                                  safe_strerror(errno));
     194           0 :                         return -1;
     195             :                 }
     196             : 
     197             : #ifdef IP_HDRINCL
     198             :                 /* we will include IP header with packet */
     199           4 :                 ret = setsockopt(ospf_sock, IPPROTO_IP, IP_HDRINCL, &hincl,
     200             :                                  sizeof(hincl));
     201           4 :                 if (ret < 0) {
     202           0 :                         flog_err(EC_LIB_SOCKET,
     203             :                                  "Can't set IP_HDRINCL option for fd %d: %s",
     204             :                                  ospf_sock, safe_strerror(errno));
     205           0 :                         break;
     206             :                 }
     207             : #elif defined(IPTOS_PREC_INTERNETCONTROL)
     208             : #warning "IP_HDRINCL not available on this system"
     209             : #warning "using IPTOS_PREC_INTERNETCONTROL"
     210             :                 ret = setsockopt_ipv4_tos(ospf_sock,
     211             :                                           IPTOS_PREC_INTERNETCONTROL);
     212             :                 if (ret < 0) {
     213             :                         flog_err(EC_LIB_SOCKET,
     214             :                                  "can't set sockopt IP_TOS %d to socket %d: %s",
     215             :                                  tos, ospf_sock, safe_strerror(errno));
     216             :                         break;
     217             :                 }
     218             : #else /* !IPTOS_PREC_INTERNETCONTROL */
     219             : #warning "IP_HDRINCL not available, nor is IPTOS_PREC_INTERNETCONTROL"
     220             :                 flog_err(EC_LIB_UNAVAILABLE, "IP_HDRINCL option not available");
     221             : #endif /* IP_HDRINCL */
     222             : 
     223           4 :                 ret = setsockopt_ifindex(AF_INET, ospf_sock, 1);
     224             : 
     225           4 :                 if (ret < 0)
     226           0 :                         flog_err(EC_LIB_SOCKET,
     227             :                                  "Can't set pktinfo option for fd %d",
     228             :                                  ospf_sock);
     229             :         }
     230             : 
     231           4 :         setsockopt_so_sendbuf(ospf_sock, bufsize);
     232           4 :         setsockopt_so_recvbuf(ospf_sock, bufsize);
     233             : 
     234           4 :         ospf->fd = ospf_sock;
     235           4 :         return ret;
     236             : }

Generated by: LCOV version v1.16-topotato