]> git.proxmox.com Git - mirror_frr.git/blame - ospfd/ospf_network.c
ospfd: Do not always debug joining AllDRouters Multicast group
[mirror_frr.git] / ospfd / ospf_network.c
CommitLineData
718e3744 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 *
896014f4
DL
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
718e3744 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"
edd7c245 31#include "privs.h"
313d7993 32#include "lib_errors.h"
edd7c245 33
718e3744 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"
05ba78e4 42#include "ospfd/ospf_dump.h"
edd7c245 43
718e3744 44/* Join to the OSPF ALL SPF ROUTERS multicast group. */
d62a17ae 45int ospf_if_add_allspfrouters(struct ospf *top, struct prefix *p,
46 ifindex_t ifindex)
718e3744 47{
d62a17ae 48 int ret;
49
50 ret = setsockopt_ipv4_multicast(top->fd, IP_ADD_MEMBERSHIP,
51 p->u.prefix4, htonl(OSPF_ALLSPFROUTERS),
52 ifindex);
53 if (ret < 0)
ade6974d 54 flog_err(
450971aa 55 EC_LIB_SOCKET,
96b663a3
MS
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,
ade6974d 58 safe_strerror(errno));
05ba78e4
CS
59 else {
60 if (IS_DEBUG_OSPF_EVENT)
996c9314 61 zlog_debug(
96b663a3
MS
62 "interface %pI4 [%u] join AllSPFRouters Multicast group.",
63 &p->u.prefix4, ifindex);
05ba78e4 64 }
d62a17ae 65
66 return ret;
718e3744 67}
68
d62a17ae 69int ospf_if_drop_allspfrouters(struct ospf *top, struct prefix *p,
70 ifindex_t ifindex)
718e3744 71{
d62a17ae 72 int ret;
73
74 ret = setsockopt_ipv4_multicast(top->fd, IP_DROP_MEMBERSHIP,
75 p->u.prefix4, htonl(OSPF_ALLSPFROUTERS),
76 ifindex);
77 if (ret < 0)
450971aa 78 flog_err(EC_LIB_SOCKET,
96b663a3
MS
79 "can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %pI4, ifindex %u, AllSPFRouters): %s",
80 top->fd, &p->u.prefix4, ifindex,
abcc171c 81 safe_strerror(errno));
05ba78e4
CS
82 else {
83 if (IS_DEBUG_OSPF_EVENT)
996c9314 84 zlog_debug(
96b663a3
MS
85 "interface %pI4 [%u] leave AllSPFRouters Multicast group.",
86 &p->u.prefix4, ifindex);
05ba78e4 87 }
d62a17ae 88
89 return ret;
718e3744 90}
91
92/* Join to the OSPF ALL Designated ROUTERS multicast group. */
d62a17ae 93int ospf_if_add_alldrouters(struct ospf *top, struct prefix *p,
94 ifindex_t ifindex)
718e3744 95{
d62a17ae 96 int ret;
97
98 ret = setsockopt_ipv4_multicast(top->fd, IP_ADD_MEMBERSHIP,
99 p->u.prefix4, htonl(OSPF_ALLDROUTERS),
100 ifindex);
101 if (ret < 0)
ade6974d 102 flog_err(
450971aa 103 EC_LIB_SOCKET,
96b663a3
MS
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,
ade6974d 106 safe_strerror(errno));
15e78e64
DS
107 else {
108 if (IS_DEBUG_OSPF_EVENT)
109 zlog_debug(
110 "interface %pI4 [%u] join AllDRouters Multicast group.",
111 &p->u.prefix4, ifindex);
112 }
d62a17ae 113 return ret;
718e3744 114}
115
d62a17ae 116int ospf_if_drop_alldrouters(struct ospf *top, struct prefix *p,
117 ifindex_t ifindex)
718e3744 118{
d62a17ae 119 int ret;
120
121 ret = setsockopt_ipv4_multicast(top->fd, IP_DROP_MEMBERSHIP,
122 p->u.prefix4, htonl(OSPF_ALLDROUTERS),
123 ifindex);
124 if (ret < 0)
450971aa 125 flog_err(EC_LIB_SOCKET,
96b663a3
MS
126 "can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %pI4, ifindex %u, AllDRouters): %s",
127 top->fd, &p->u.prefix4, ifindex,
abcc171c 128 safe_strerror(errno));
d62a17ae 129 else
130 zlog_debug(
96b663a3
MS
131 "interface %pI4 [%u] leave AllDRouters Multicast group.",
132 &p->u.prefix4, ifindex);
d62a17ae 133
134 return ret;
718e3744 135}
136
d62a17ae 137int ospf_if_ipmulticast(struct ospf *top, struct prefix *p, ifindex_t ifindex)
718e3744 138{
d7c0a89a 139 uint8_t val;
d62a17ae 140 int ret, len;
141
142 /* Prevent receiving self-origined multicast packets. */
143 ret = setsockopt_ipv4_multicast_loop(top->fd, 0);
144 if (ret < 0)
450971aa 145 flog_err(EC_LIB_SOCKET,
abcc171c
DS
146 "can't setsockopt IP_MULTICAST_LOOP(0) for fd %d: %s",
147 top->fd, safe_strerror(errno));
d62a17ae 148
149 /* Explicitly set multicast ttl to 1 -- endo. */
150 val = 1;
151 len = sizeof(val);
152 ret = setsockopt(top->fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&val,
153 len);
154 if (ret < 0)
450971aa 155 flog_err(EC_LIB_SOCKET,
abcc171c
DS
156 "can't setsockopt IP_MULTICAST_TTL(1) for fd %d: %s",
157 top->fd, safe_strerror(errno));
e1b18df1
CS
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)
450971aa 164 flog_err(EC_LIB_SOCKET,
96b663a3
MS
165 "can't setsockopt IP_MULTICAST_IF(fd %d, addr %pI4, ifindex %u): %s",
166 top->fd, &p->u.prefix4, ifindex,
abcc171c 167 safe_strerror(errno));
e1b18df1 168#endif
d62a17ae 169
e7503eab
CS
170 return ret;
171}
172
e7503eab 173int ospf_sock_init(struct ospf *ospf)
718e3744 174{
d62a17ae 175 int ospf_sock;
176 int ret, hincl = 1;
177 int bufsize = (8 * 1024 * 1024);
178
3c0eb8fa
PG
179 /* silently ignore. already done */
180 if (ospf->fd > 0)
181 return -1;
182
183 if (ospf->vrf_id == VRF_UNKNOWN) {
184 /* silently return since VRF is not ready */
185 return -1;
186 }
0cf6db21 187 frr_with_privs(&ospfd_privs) {
6bb30c2c
DL
188 ospf_sock = vrf_socket(AF_INET, SOCK_RAW, IPPROTO_OSPFIGP,
189 ospf->vrf_id, ospf->name);
190 if (ospf_sock < 0) {
450971aa 191 flog_err(EC_LIB_SOCKET,
abcc171c 192 "ospf_read_sock_init: socket: %s",
6bb30c2c 193 safe_strerror(errno));
95d7a42a 194 return -1;
6bb30c2c 195 }
d62a17ae 196
5bd4189c 197#ifdef IP_HDRINCL
6bb30c2c
DL
198 /* we will include IP header with packet */
199 ret = setsockopt(ospf_sock, IPPROTO_IP, IP_HDRINCL, &hincl,
200 sizeof(hincl));
201 if (ret < 0) {
450971aa 202 flog_err(EC_LIB_SOCKET,
abcc171c
DS
203 "Can't set IP_HDRINCL option for fd %d: %s",
204 ospf_sock, safe_strerror(errno));
6bb30c2c
DL
205 break;
206 }
d62a17ae 207#elif defined(IPTOS_PREC_INTERNETCONTROL)
5bd4189c 208#warning "IP_HDRINCL not available on this system"
209#warning "using IPTOS_PREC_INTERNETCONTROL"
6bb30c2c
DL
210 ret = setsockopt_ipv4_tos(ospf_sock,
211 IPTOS_PREC_INTERNETCONTROL);
212 if (ret < 0) {
450971aa 213 flog_err(EC_LIB_SOCKET,
abcc171c
DS
214 "can't set sockopt IP_TOS %d to socket %d: %s",
215 tos, ospf_sock, safe_strerror(errno));
6bb30c2c
DL
216 break;
217 }
5bd4189c 218#else /* !IPTOS_PREC_INTERNETCONTROL */
219#warning "IP_HDRINCL not available, nor is IPTOS_PREC_INTERNETCONTROL"
1c50c1c0 220 flog_err(EC_LIB_UNAVAILABLE, "IP_HDRINCL option not available");
5bd4189c 221#endif /* IP_HDRINCL */
718e3744 222
6bb30c2c 223 ret = setsockopt_ifindex(AF_INET, ospf_sock, 1);
ac191232 224
6bb30c2c 225 if (ret < 0)
450971aa 226 flog_err(EC_LIB_SOCKET,
abcc171c
DS
227 "Can't set pktinfo option for fd %d",
228 ospf_sock);
6bb30c2c 229 }
e7503eab 230
338b8e91
RW
231 setsockopt_so_sendbuf(ospf_sock, bufsize);
232 setsockopt_so_recvbuf(ospf_sock, bufsize);
233
e7503eab 234 ospf->fd = ospf_sock;
e7503eab 235 return ret;
718e3744 236}