]> git.proxmox.com Git - mirror_frr.git/blame - ospfd/ospf_network.c
ospfd: don't exit when socket is not created
[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));
d62a17ae 107 else
108 zlog_debug(
96b663a3
MS
109 "interface %pI4 [%u] join AllDRouters Multicast group.",
110 &p->u.prefix4, ifindex);
d62a17ae 111
112 return ret;
718e3744 113}
114
d62a17ae 115int ospf_if_drop_alldrouters(struct ospf *top, struct prefix *p,
116 ifindex_t ifindex)
718e3744 117{
d62a17ae 118 int ret;
119
120 ret = setsockopt_ipv4_multicast(top->fd, IP_DROP_MEMBERSHIP,
121 p->u.prefix4, htonl(OSPF_ALLDROUTERS),
122 ifindex);
123 if (ret < 0)
450971aa 124 flog_err(EC_LIB_SOCKET,
96b663a3
MS
125 "can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %pI4, ifindex %u, AllDRouters): %s",
126 top->fd, &p->u.prefix4, ifindex,
abcc171c 127 safe_strerror(errno));
d62a17ae 128 else
129 zlog_debug(
96b663a3
MS
130 "interface %pI4 [%u] leave AllDRouters Multicast group.",
131 &p->u.prefix4, ifindex);
d62a17ae 132
133 return ret;
718e3744 134}
135
d62a17ae 136int ospf_if_ipmulticast(struct ospf *top, struct prefix *p, ifindex_t ifindex)
718e3744 137{
d7c0a89a 138 uint8_t val;
d62a17ae 139 int ret, len;
140
141 /* Prevent receiving self-origined multicast packets. */
142 ret = setsockopt_ipv4_multicast_loop(top->fd, 0);
143 if (ret < 0)
450971aa 144 flog_err(EC_LIB_SOCKET,
abcc171c
DS
145 "can't setsockopt IP_MULTICAST_LOOP(0) for fd %d: %s",
146 top->fd, safe_strerror(errno));
d62a17ae 147
148 /* Explicitly set multicast ttl to 1 -- endo. */
149 val = 1;
150 len = sizeof(val);
151 ret = setsockopt(top->fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&val,
152 len);
153 if (ret < 0)
450971aa 154 flog_err(EC_LIB_SOCKET,
abcc171c
DS
155 "can't setsockopt IP_MULTICAST_TTL(1) for fd %d: %s",
156 top->fd, safe_strerror(errno));
e1b18df1
CS
157#ifndef GNU_LINUX
158 /* For GNU LINUX ospf_write uses IP_PKTINFO, in_pktinfo to send
159 * packet out of ifindex. Below would be used Non Linux system.
160 */
161 ret = setsockopt_ipv4_multicast_if(top->fd, p->u.prefix4, ifindex);
162 if (ret < 0)
450971aa 163 flog_err(EC_LIB_SOCKET,
96b663a3
MS
164 "can't setsockopt IP_MULTICAST_IF(fd %d, addr %pI4, ifindex %u): %s",
165 top->fd, &p->u.prefix4, ifindex,
abcc171c 166 safe_strerror(errno));
e1b18df1 167#endif
d62a17ae 168
e7503eab
CS
169 return ret;
170}
171
e7503eab 172int ospf_sock_init(struct ospf *ospf)
718e3744 173{
d62a17ae 174 int ospf_sock;
175 int ret, hincl = 1;
176 int bufsize = (8 * 1024 * 1024);
177
3c0eb8fa
PG
178 /* silently ignore. already done */
179 if (ospf->fd > 0)
180 return -1;
181
182 if (ospf->vrf_id == VRF_UNKNOWN) {
183 /* silently return since VRF is not ready */
184 return -1;
185 }
0cf6db21 186 frr_with_privs(&ospfd_privs) {
6bb30c2c
DL
187 ospf_sock = vrf_socket(AF_INET, SOCK_RAW, IPPROTO_OSPFIGP,
188 ospf->vrf_id, ospf->name);
189 if (ospf_sock < 0) {
450971aa 190 flog_err(EC_LIB_SOCKET,
abcc171c 191 "ospf_read_sock_init: socket: %s",
6bb30c2c 192 safe_strerror(errno));
95d7a42a 193 return -1;
6bb30c2c 194 }
d62a17ae 195
5bd4189c 196#ifdef IP_HDRINCL
6bb30c2c
DL
197 /* we will include IP header with packet */
198 ret = setsockopt(ospf_sock, IPPROTO_IP, IP_HDRINCL, &hincl,
199 sizeof(hincl));
200 if (ret < 0) {
450971aa 201 flog_err(EC_LIB_SOCKET,
abcc171c
DS
202 "Can't set IP_HDRINCL option for fd %d: %s",
203 ospf_sock, safe_strerror(errno));
6bb30c2c
DL
204 break;
205 }
d62a17ae 206#elif defined(IPTOS_PREC_INTERNETCONTROL)
5bd4189c 207#warning "IP_HDRINCL not available on this system"
208#warning "using IPTOS_PREC_INTERNETCONTROL"
6bb30c2c
DL
209 ret = setsockopt_ipv4_tos(ospf_sock,
210 IPTOS_PREC_INTERNETCONTROL);
211 if (ret < 0) {
450971aa 212 flog_err(EC_LIB_SOCKET,
abcc171c
DS
213 "can't set sockopt IP_TOS %d to socket %d: %s",
214 tos, ospf_sock, safe_strerror(errno));
6bb30c2c
DL
215 break;
216 }
5bd4189c 217#else /* !IPTOS_PREC_INTERNETCONTROL */
218#warning "IP_HDRINCL not available, nor is IPTOS_PREC_INTERNETCONTROL"
1c50c1c0 219 flog_err(EC_LIB_UNAVAILABLE, "IP_HDRINCL option not available");
5bd4189c 220#endif /* IP_HDRINCL */
718e3744 221
6bb30c2c 222 ret = setsockopt_ifindex(AF_INET, ospf_sock, 1);
ac191232 223
6bb30c2c 224 if (ret < 0)
450971aa 225 flog_err(EC_LIB_SOCKET,
abcc171c
DS
226 "Can't set pktinfo option for fd %d",
227 ospf_sock);
6bb30c2c 228 }
e7503eab 229
338b8e91
RW
230 setsockopt_so_sendbuf(ospf_sock, bufsize);
231 setsockopt_so_recvbuf(ospf_sock, bufsize);
232
e7503eab 233 ospf->fd = ospf_sock;
e7503eab 234 return ret;
718e3744 235}