]> git.proxmox.com Git - mirror_frr.git/blame - ospfd/ospf_network.c
*: reindent
[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
33extern struct zebra_privs_t ospfd_privs;
718e3744 34
35#include "ospfd/ospfd.h"
36#include "ospfd/ospf_network.h"
37#include "ospfd/ospf_interface.h"
38#include "ospfd/ospf_asbr.h"
39#include "ospfd/ospf_lsa.h"
40#include "ospfd/ospf_lsdb.h"
41#include "ospfd/ospf_neighbor.h"
42#include "ospfd/ospf_packet.h"
43
edd7c245 44
718e3744 45/* Join to the OSPF ALL SPF ROUTERS multicast group. */
d62a17ae 46int ospf_if_add_allspfrouters(struct ospf *top, struct prefix *p,
47 ifindex_t ifindex)
718e3744 48{
d62a17ae 49 int ret;
50
51 ret = setsockopt_ipv4_multicast(top->fd, IP_ADD_MEMBERSHIP,
52 p->u.prefix4, htonl(OSPF_ALLSPFROUTERS),
53 ifindex);
54 if (ret < 0)
55 zlog_warn(
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));
61 else
62 zlog_debug(
63 "interface %s [%u] join AllSPFRouters Multicast group.",
64 inet_ntoa(p->u.prefix4), ifindex);
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)
78 zlog_warn(
79 "can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, "
80 "ifindex %u, AllSPFRouters): %s",
81 top->fd, inet_ntoa(p->u.prefix4), ifindex,
82 safe_strerror(errno));
83 else
84 zlog_debug(
85 "interface %s [%u] leave AllSPFRouters Multicast group.",
86 inet_ntoa(p->u.prefix4), ifindex);
87
88 return ret;
718e3744 89}
90
91/* Join to the OSPF ALL Designated ROUTERS multicast group. */
d62a17ae 92int ospf_if_add_alldrouters(struct ospf *top, struct prefix *p,
93 ifindex_t ifindex)
718e3744 94{
d62a17ae 95 int ret;
96
97 ret = setsockopt_ipv4_multicast(top->fd, IP_ADD_MEMBERSHIP,
98 p->u.prefix4, htonl(OSPF_ALLDROUTERS),
99 ifindex);
100 if (ret < 0)
101 zlog_warn(
102 "can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, "
103 "ifindex %u, AllDRouters): %s; perhaps a kernel limit "
104 "on # of multicast group memberships has been exceeded?",
105 top->fd, inet_ntoa(p->u.prefix4), ifindex,
106 safe_strerror(errno));
107 else
108 zlog_debug(
109 "interface %s [%u] join AllDRouters Multicast group.",
110 inet_ntoa(p->u.prefix4), ifindex);
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)
124 zlog_warn(
125 "can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, "
126 "ifindex %u, AllDRouters): %s",
127 top->fd, inet_ntoa(p->u.prefix4), ifindex,
128 safe_strerror(errno));
129 else
130 zlog_debug(
131 "interface %s [%u] leave AllDRouters Multicast group.",
132 inet_ntoa(p->u.prefix4), ifindex);
133
134 return ret;
718e3744 135}
136
d62a17ae 137int ospf_if_ipmulticast(struct ospf *top, struct prefix *p, ifindex_t ifindex)
718e3744 138{
d62a17ae 139 u_char val;
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)
145 zlog_warn("can't setsockopt IP_MULTICAST_LOOP(0) for fd %d: %s",
146 top->fd, safe_strerror(errno));
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)
154 zlog_warn("can't setsockopt IP_MULTICAST_TTL(1) for fd %d: %s",
155 top->fd, safe_strerror(errno));
156
157 ret = setsockopt_ipv4_multicast_if(top->fd, p->u.prefix4, ifindex);
158 if (ret < 0)
159 zlog_warn(
160 "can't setsockopt IP_MULTICAST_IF(fd %d, addr %s, "
161 "ifindex %u): %s",
162 top->fd, inet_ntoa(p->u.prefix4), ifindex,
163 safe_strerror(errno));
164
165 return ret;
718e3744 166}
167
d62a17ae 168int ospf_sock_init(void)
718e3744 169{
d62a17ae 170 int ospf_sock;
171 int ret, hincl = 1;
172 int bufsize = (8 * 1024 * 1024);
173
174 if (ospfd_privs.change(ZPRIVS_RAISE))
175 zlog_err("ospf_sock_init: could not raise privs, %s",
176 safe_strerror(errno));
177
178 ospf_sock = socket(AF_INET, SOCK_RAW, IPPROTO_OSPFIGP);
179 if (ospf_sock < 0) {
180 int save_errno = errno;
181 if (ospfd_privs.change(ZPRIVS_LOWER))
182 zlog_err("ospf_sock_init: could not lower privs, %s",
183 safe_strerror(errno));
184 zlog_err("ospf_read_sock_init: socket: %s",
185 safe_strerror(save_errno));
186 exit(1);
187 }
188
5bd4189c 189#ifdef IP_HDRINCL
d62a17ae 190 /* we will include IP header with packet */
191 ret = setsockopt(ospf_sock, IPPROTO_IP, IP_HDRINCL, &hincl,
192 sizeof(hincl));
193 if (ret < 0) {
194 int save_errno = errno;
195 if (ospfd_privs.change(ZPRIVS_LOWER))
196 zlog_err("ospf_sock_init: could not lower privs, %s",
197 safe_strerror(errno));
198 zlog_warn("Can't set IP_HDRINCL option for fd %d: %s",
199 ospf_sock, safe_strerror(save_errno));
200 }
201#elif defined(IPTOS_PREC_INTERNETCONTROL)
5bd4189c 202#warning "IP_HDRINCL not available on this system"
203#warning "using IPTOS_PREC_INTERNETCONTROL"
d62a17ae 204 ret = setsockopt_ipv4_tos(ospf_sock, IPTOS_PREC_INTERNETCONTROL);
205 if (ret < 0) {
206 int save_errno = errno;
207 if (ospfd_privs.change(ZPRIVS_LOWER))
208 zlog_err("ospf_sock_init: could not lower privs, %s",
209 safe_strerror(errno));
210 zlog_warn("can't set sockopt IP_TOS %d to socket %d: %s", tos,
211 ospf_sock, safe_strerror(save_errno));
212 close(ospf_sock); /* Prevent sd leak. */
213 return ret;
214 }
5bd4189c 215#else /* !IPTOS_PREC_INTERNETCONTROL */
216#warning "IP_HDRINCL not available, nor is IPTOS_PREC_INTERNETCONTROL"
d62a17ae 217 zlog_warn("IP_HDRINCL option not available");
5bd4189c 218#endif /* IP_HDRINCL */
718e3744 219
d62a17ae 220 ret = setsockopt_ifindex(AF_INET, ospf_sock, 1);
ac191232 221
d62a17ae 222 if (ret < 0)
223 zlog_warn("Can't set pktinfo option for fd %d", ospf_sock);
edd7c245 224
d62a17ae 225 if (ospfd_privs.change(ZPRIVS_LOWER)) {
226 zlog_err("ospf_sock_init: could not lower privs, %s",
227 safe_strerror(errno));
228 }
a78d75b0 229
d62a17ae 230 setsockopt_so_sendbuf(ospf_sock, bufsize);
231 setsockopt_so_recvbuf(ospf_sock, bufsize);
a78d75b0 232
d62a17ae 233 return ospf_sock;
718e3744 234}