]> git.proxmox.com Git - mirror_frr.git/blame - ospfd/ospf_network.c
Merge pull request #5793 from ton31337/fix/formatting_show_bgp_summary_failed
[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,
ade6974d
QY
56 "can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, "
57 "ifindex %u, AllSPFRouters): %s; perhaps a kernel limit "
58 "on # of multicast group memberships has been exceeded?",
59 top->fd, inet_ntoa(p->u.prefix4), ifindex,
60 safe_strerror(errno));
05ba78e4
CS
61 else {
62 if (IS_DEBUG_OSPF_EVENT)
996c9314
LB
63 zlog_debug(
64 "interface %s [%u] join AllSPFRouters Multicast group.",
65 inet_ntoa(p->u.prefix4), ifindex);
05ba78e4 66 }
d62a17ae 67
68 return ret;
718e3744 69}
70
d62a17ae 71int ospf_if_drop_allspfrouters(struct ospf *top, struct prefix *p,
72 ifindex_t ifindex)
718e3744 73{
d62a17ae 74 int ret;
75
76 ret = setsockopt_ipv4_multicast(top->fd, IP_DROP_MEMBERSHIP,
77 p->u.prefix4, htonl(OSPF_ALLSPFROUTERS),
78 ifindex);
79 if (ret < 0)
450971aa 80 flog_err(EC_LIB_SOCKET,
abcc171c
DS
81 "can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, "
82 "ifindex %u, AllSPFRouters): %s",
83 top->fd, inet_ntoa(p->u.prefix4), ifindex,
84 safe_strerror(errno));
05ba78e4
CS
85 else {
86 if (IS_DEBUG_OSPF_EVENT)
996c9314
LB
87 zlog_debug(
88 "interface %s [%u] leave AllSPFRouters Multicast group.",
89 inet_ntoa(p->u.prefix4), ifindex);
05ba78e4 90 }
d62a17ae 91
92 return ret;
718e3744 93}
94
95/* Join to the OSPF ALL Designated ROUTERS multicast group. */
d62a17ae 96int ospf_if_add_alldrouters(struct ospf *top, struct prefix *p,
97 ifindex_t ifindex)
718e3744 98{
d62a17ae 99 int ret;
100
101 ret = setsockopt_ipv4_multicast(top->fd, IP_ADD_MEMBERSHIP,
102 p->u.prefix4, htonl(OSPF_ALLDROUTERS),
103 ifindex);
104 if (ret < 0)
ade6974d 105 flog_err(
450971aa 106 EC_LIB_SOCKET,
ade6974d
QY
107 "can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, "
108 "ifindex %u, AllDRouters): %s; perhaps a kernel limit "
109 "on # of multicast group memberships has been exceeded?",
110 top->fd, inet_ntoa(p->u.prefix4), ifindex,
111 safe_strerror(errno));
d62a17ae 112 else
113 zlog_debug(
114 "interface %s [%u] join AllDRouters Multicast group.",
115 inet_ntoa(p->u.prefix4), ifindex);
116
117 return ret;
718e3744 118}
119
d62a17ae 120int ospf_if_drop_alldrouters(struct ospf *top, struct prefix *p,
121 ifindex_t ifindex)
718e3744 122{
d62a17ae 123 int ret;
124
125 ret = setsockopt_ipv4_multicast(top->fd, IP_DROP_MEMBERSHIP,
126 p->u.prefix4, htonl(OSPF_ALLDROUTERS),
127 ifindex);
128 if (ret < 0)
450971aa 129 flog_err(EC_LIB_SOCKET,
abcc171c
DS
130 "can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, "
131 "ifindex %u, AllDRouters): %s",
132 top->fd, inet_ntoa(p->u.prefix4), ifindex,
133 safe_strerror(errno));
d62a17ae 134 else
135 zlog_debug(
136 "interface %s [%u] leave AllDRouters Multicast group.",
137 inet_ntoa(p->u.prefix4), ifindex);
138
139 return ret;
718e3744 140}
141
d62a17ae 142int ospf_if_ipmulticast(struct ospf *top, struct prefix *p, ifindex_t ifindex)
718e3744 143{
d7c0a89a 144 uint8_t val;
d62a17ae 145 int ret, len;
146
147 /* Prevent receiving self-origined multicast packets. */
148 ret = setsockopt_ipv4_multicast_loop(top->fd, 0);
149 if (ret < 0)
450971aa 150 flog_err(EC_LIB_SOCKET,
abcc171c
DS
151 "can't setsockopt IP_MULTICAST_LOOP(0) for fd %d: %s",
152 top->fd, safe_strerror(errno));
d62a17ae 153
154 /* Explicitly set multicast ttl to 1 -- endo. */
155 val = 1;
156 len = sizeof(val);
157 ret = setsockopt(top->fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&val,
158 len);
159 if (ret < 0)
450971aa 160 flog_err(EC_LIB_SOCKET,
abcc171c
DS
161 "can't setsockopt IP_MULTICAST_TTL(1) for fd %d: %s",
162 top->fd, safe_strerror(errno));
e1b18df1
CS
163#ifndef GNU_LINUX
164 /* For GNU LINUX ospf_write uses IP_PKTINFO, in_pktinfo to send
165 * packet out of ifindex. Below would be used Non Linux system.
166 */
167 ret = setsockopt_ipv4_multicast_if(top->fd, p->u.prefix4, ifindex);
168 if (ret < 0)
450971aa 169 flog_err(EC_LIB_SOCKET,
abcc171c
DS
170 "can't setsockopt IP_MULTICAST_IF(fd %d, addr %s, "
171 "ifindex %u): %s",
172 top->fd, inet_ntoa(p->u.prefix4), ifindex,
173 safe_strerror(errno));
e1b18df1 174#endif
d62a17ae 175
e7503eab
CS
176 return ret;
177}
178
e7503eab 179int ospf_sock_init(struct ospf *ospf)
718e3744 180{
d62a17ae 181 int ospf_sock;
182 int ret, hincl = 1;
183 int bufsize = (8 * 1024 * 1024);
184
3c0eb8fa
PG
185 /* silently ignore. already done */
186 if (ospf->fd > 0)
187 return -1;
188
189 if (ospf->vrf_id == VRF_UNKNOWN) {
190 /* silently return since VRF is not ready */
191 return -1;
192 }
0cf6db21 193 frr_with_privs(&ospfd_privs) {
6bb30c2c
DL
194 ospf_sock = vrf_socket(AF_INET, SOCK_RAW, IPPROTO_OSPFIGP,
195 ospf->vrf_id, ospf->name);
196 if (ospf_sock < 0) {
450971aa 197 flog_err(EC_LIB_SOCKET,
abcc171c 198 "ospf_read_sock_init: socket: %s",
6bb30c2c
DL
199 safe_strerror(errno));
200 exit(1);
201 }
d62a17ae 202
5bd4189c 203#ifdef IP_HDRINCL
6bb30c2c
DL
204 /* we will include IP header with packet */
205 ret = setsockopt(ospf_sock, IPPROTO_IP, IP_HDRINCL, &hincl,
206 sizeof(hincl));
207 if (ret < 0) {
450971aa 208 flog_err(EC_LIB_SOCKET,
abcc171c
DS
209 "Can't set IP_HDRINCL option for fd %d: %s",
210 ospf_sock, safe_strerror(errno));
6bb30c2c
DL
211 close(ospf_sock);
212 break;
213 }
d62a17ae 214#elif defined(IPTOS_PREC_INTERNETCONTROL)
5bd4189c 215#warning "IP_HDRINCL not available on this system"
216#warning "using IPTOS_PREC_INTERNETCONTROL"
6bb30c2c
DL
217 ret = setsockopt_ipv4_tos(ospf_sock,
218 IPTOS_PREC_INTERNETCONTROL);
219 if (ret < 0) {
450971aa 220 flog_err(EC_LIB_SOCKET,
abcc171c
DS
221 "can't set sockopt IP_TOS %d to socket %d: %s",
222 tos, ospf_sock, safe_strerror(errno));
6bb30c2c
DL
223 close(ospf_sock); /* Prevent sd leak. */
224 break;
225 }
5bd4189c 226#else /* !IPTOS_PREC_INTERNETCONTROL */
227#warning "IP_HDRINCL not available, nor is IPTOS_PREC_INTERNETCONTROL"
1c50c1c0 228 flog_err(EC_LIB_UNAVAILABLE, "IP_HDRINCL option not available");
5bd4189c 229#endif /* IP_HDRINCL */
718e3744 230
6bb30c2c 231 ret = setsockopt_ifindex(AF_INET, ospf_sock, 1);
ac191232 232
6bb30c2c 233 if (ret < 0)
450971aa 234 flog_err(EC_LIB_SOCKET,
abcc171c
DS
235 "Can't set pktinfo option for fd %d",
236 ospf_sock);
6bb30c2c 237 }
e7503eab 238
338b8e91
RW
239 setsockopt_so_sendbuf(ospf_sock, bufsize);
240 setsockopt_so_recvbuf(ospf_sock, bufsize);
241
e7503eab 242 ospf->fd = ospf_sock;
e7503eab 243 return ret;
718e3744 244}