]> git.proxmox.com Git - mirror_frr.git/blame - ospfd/ospf_network.c
Merge pull request #1551 from LabNConsulting/working/master/minor-perf
[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"
32
718e3744 33#include "ospfd/ospfd.h"
34#include "ospfd/ospf_network.h"
35#include "ospfd/ospf_interface.h"
36#include "ospfd/ospf_asbr.h"
37#include "ospfd/ospf_lsa.h"
38#include "ospfd/ospf_lsdb.h"
39#include "ospfd/ospf_neighbor.h"
40#include "ospfd/ospf_packet.h"
41
edd7c245 42
718e3744 43/* Join to the OSPF ALL SPF ROUTERS multicast group. */
d62a17ae 44int ospf_if_add_allspfrouters(struct ospf *top, struct prefix *p,
45 ifindex_t ifindex)
718e3744 46{
d62a17ae 47 int ret;
48
49 ret = setsockopt_ipv4_multicast(top->fd, IP_ADD_MEMBERSHIP,
50 p->u.prefix4, htonl(OSPF_ALLSPFROUTERS),
51 ifindex);
52 if (ret < 0)
53 zlog_warn(
54 "can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, "
55 "ifindex %u, AllSPFRouters): %s; perhaps a kernel limit "
56 "on # of multicast group memberships has been exceeded?",
57 top->fd, inet_ntoa(p->u.prefix4), ifindex,
58 safe_strerror(errno));
59 else
60 zlog_debug(
61 "interface %s [%u] join AllSPFRouters Multicast group.",
62 inet_ntoa(p->u.prefix4), ifindex);
63
64 return ret;
718e3744 65}
66
d62a17ae 67int ospf_if_drop_allspfrouters(struct ospf *top, struct prefix *p,
68 ifindex_t ifindex)
718e3744 69{
d62a17ae 70 int ret;
71
72 ret = setsockopt_ipv4_multicast(top->fd, IP_DROP_MEMBERSHIP,
73 p->u.prefix4, htonl(OSPF_ALLSPFROUTERS),
74 ifindex);
75 if (ret < 0)
76 zlog_warn(
77 "can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, "
78 "ifindex %u, AllSPFRouters): %s",
79 top->fd, inet_ntoa(p->u.prefix4), ifindex,
80 safe_strerror(errno));
81 else
82 zlog_debug(
83 "interface %s [%u] leave AllSPFRouters Multicast group.",
84 inet_ntoa(p->u.prefix4), ifindex);
85
86 return ret;
718e3744 87}
88
89/* Join to the OSPF ALL Designated ROUTERS multicast group. */
d62a17ae 90int ospf_if_add_alldrouters(struct ospf *top, struct prefix *p,
91 ifindex_t ifindex)
718e3744 92{
d62a17ae 93 int ret;
94
95 ret = setsockopt_ipv4_multicast(top->fd, IP_ADD_MEMBERSHIP,
96 p->u.prefix4, htonl(OSPF_ALLDROUTERS),
97 ifindex);
98 if (ret < 0)
99 zlog_warn(
100 "can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, "
101 "ifindex %u, AllDRouters): %s; perhaps a kernel limit "
102 "on # of multicast group memberships has been exceeded?",
103 top->fd, inet_ntoa(p->u.prefix4), ifindex,
104 safe_strerror(errno));
105 else
106 zlog_debug(
107 "interface %s [%u] join AllDRouters Multicast group.",
108 inet_ntoa(p->u.prefix4), ifindex);
109
110 return ret;
718e3744 111}
112
d62a17ae 113int ospf_if_drop_alldrouters(struct ospf *top, struct prefix *p,
114 ifindex_t ifindex)
718e3744 115{
d62a17ae 116 int ret;
117
118 ret = setsockopt_ipv4_multicast(top->fd, IP_DROP_MEMBERSHIP,
119 p->u.prefix4, htonl(OSPF_ALLDROUTERS),
120 ifindex);
121 if (ret < 0)
122 zlog_warn(
123 "can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, "
124 "ifindex %u, AllDRouters): %s",
125 top->fd, inet_ntoa(p->u.prefix4), ifindex,
126 safe_strerror(errno));
127 else
128 zlog_debug(
129 "interface %s [%u] leave AllDRouters Multicast group.",
130 inet_ntoa(p->u.prefix4), ifindex);
131
132 return ret;
718e3744 133}
134
d62a17ae 135int ospf_if_ipmulticast(struct ospf *top, struct prefix *p, ifindex_t ifindex)
718e3744 136{
d62a17ae 137 u_char val;
138 int ret, len;
139
140 /* Prevent receiving self-origined multicast packets. */
141 ret = setsockopt_ipv4_multicast_loop(top->fd, 0);
142 if (ret < 0)
143 zlog_warn("can't setsockopt IP_MULTICAST_LOOP(0) for fd %d: %s",
144 top->fd, safe_strerror(errno));
145
146 /* Explicitly set multicast ttl to 1 -- endo. */
147 val = 1;
148 len = sizeof(val);
149 ret = setsockopt(top->fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&val,
150 len);
151 if (ret < 0)
152 zlog_warn("can't setsockopt IP_MULTICAST_TTL(1) for fd %d: %s",
153 top->fd, safe_strerror(errno));
e1b18df1
CS
154#ifndef GNU_LINUX
155 /* For GNU LINUX ospf_write uses IP_PKTINFO, in_pktinfo to send
156 * packet out of ifindex. Below would be used Non Linux system.
157 */
158 ret = setsockopt_ipv4_multicast_if(top->fd, p->u.prefix4, ifindex);
159 if (ret < 0)
160 zlog_warn(
161 "can't setsockopt IP_MULTICAST_IF(fd %d, addr %s, "
162 "ifindex %u): %s",
163 top->fd, inet_ntoa(p->u.prefix4), ifindex,
164 safe_strerror(errno));
165#endif
d62a17ae 166
e7503eab
CS
167 return ret;
168}
169
170int ospf_bind_vrfdevice(struct ospf *ospf, int ospf_sock)
171{
172 int ret = 0;
d62a17ae 173
e7503eab
CS
174#ifdef SO_BINDTODEVICE
175
176 if (ospf && ospf->vrf_id != VRF_DEFAULT &&
177 ospf->vrf_id != VRF_UNKNOWN) {
178 ret = setsockopt(ospf_sock, SOL_SOCKET, SO_BINDTODEVICE,
179 ospf->name,
180 strlen(ospf->name));
181 if (ret < 0) {
182 int save_errno = errno;
183
184 zlog_warn("%s: Could not setsockopt SO_BINDTODEVICE %s",
185 __PRETTY_FUNCTION__,
186 safe_strerror(save_errno));
e7503eab 187 }
b1c3ae8c 188
e7503eab
CS
189 }
190#endif
d62a17ae 191 return ret;
718e3744 192}
193
e7503eab 194int ospf_sock_init(struct ospf *ospf)
718e3744 195{
d62a17ae 196 int ospf_sock;
197 int ret, hincl = 1;
198 int bufsize = (8 * 1024 * 1024);
199
e7503eab 200 if (ospfd_privs.change(ZPRIVS_RAISE)) {
d62a17ae 201 zlog_err("ospf_sock_init: could not raise privs, %s",
202 safe_strerror(errno));
e7503eab 203 }
d62a17ae 204
205 ospf_sock = socket(AF_INET, SOCK_RAW, IPPROTO_OSPFIGP);
206 if (ospf_sock < 0) {
207 int save_errno = errno;
e7503eab 208
d62a17ae 209 if (ospfd_privs.change(ZPRIVS_LOWER))
210 zlog_err("ospf_sock_init: could not lower privs, %s",
211 safe_strerror(errno));
212 zlog_err("ospf_read_sock_init: socket: %s",
213 safe_strerror(save_errno));
214 exit(1);
215 }
216
e7503eab 217 ret = ospf_bind_vrfdevice(ospf, ospf_sock);
757fd711
DS
218 if (ret < 0) {
219 close(ospf_sock);
e7503eab 220 goto out;
757fd711 221 }
e7503eab 222
5bd4189c 223#ifdef IP_HDRINCL
d62a17ae 224 /* we will include IP header with packet */
225 ret = setsockopt(ospf_sock, IPPROTO_IP, IP_HDRINCL, &hincl,
226 sizeof(hincl));
227 if (ret < 0) {
228 int save_errno = errno;
e7503eab 229
d62a17ae 230 zlog_warn("Can't set IP_HDRINCL option for fd %d: %s",
231 ospf_sock, safe_strerror(save_errno));
757fd711 232 close(ospf_sock);
e7503eab 233 goto out;
d62a17ae 234 }
235#elif defined(IPTOS_PREC_INTERNETCONTROL)
5bd4189c 236#warning "IP_HDRINCL not available on this system"
237#warning "using IPTOS_PREC_INTERNETCONTROL"
d62a17ae 238 ret = setsockopt_ipv4_tos(ospf_sock, IPTOS_PREC_INTERNETCONTROL);
239 if (ret < 0) {
240 int save_errno = errno;
e7503eab 241
d62a17ae 242 zlog_warn("can't set sockopt IP_TOS %d to socket %d: %s", tos,
243 ospf_sock, safe_strerror(save_errno));
244 close(ospf_sock); /* Prevent sd leak. */
e7503eab 245 goto out;
d62a17ae 246 }
5bd4189c 247#else /* !IPTOS_PREC_INTERNETCONTROL */
248#warning "IP_HDRINCL not available, nor is IPTOS_PREC_INTERNETCONTROL"
d62a17ae 249 zlog_warn("IP_HDRINCL option not available");
5bd4189c 250#endif /* IP_HDRINCL */
718e3744 251
d62a17ae 252 ret = setsockopt_ifindex(AF_INET, ospf_sock, 1);
ac191232 253
d62a17ae 254 if (ret < 0)
255 zlog_warn("Can't set pktinfo option for fd %d", ospf_sock);
edd7c245 256
e7503eab
CS
257 setsockopt_so_sendbuf(ospf_sock, bufsize);
258 setsockopt_so_recvbuf(ospf_sock, bufsize);
259
260 ospf->fd = ospf_sock;
261out:
d62a17ae 262 if (ospfd_privs.change(ZPRIVS_LOWER)) {
263 zlog_err("ospf_sock_init: could not lower privs, %s",
264 safe_strerror(errno));
265 }
e7503eab 266 return ret;
718e3744 267}