]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_network.c
Merge remote-tracking branch 'origin/cmaster' into cmaster-next
[mirror_frr.git] / ospfd / ospf_network.c
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 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23 #include <zebra.h>
24
25 #include "thread.h"
26 #include "linklist.h"
27 #include "prefix.h"
28 #include "if.h"
29 #include "sockunion.h"
30 #include "log.h"
31 #include "sockopt.h"
32 #include "privs.h"
33
34 extern struct zebra_privs_t ospfd_privs;
35
36 #include "ospfd/ospfd.h"
37 #include "ospfd/ospf_network.h"
38 #include "ospfd/ospf_interface.h"
39 #include "ospfd/ospf_asbr.h"
40 #include "ospfd/ospf_lsa.h"
41 #include "ospfd/ospf_lsdb.h"
42 #include "ospfd/ospf_neighbor.h"
43 #include "ospfd/ospf_packet.h"
44 #include "ospfd/ospf_dump.h"
45
46
47
48 /* Join to the OSPF ALL SPF ROUTERS multicast group. */
49 int
50 ospf_if_add_allspfrouters (struct ospf *top, struct prefix *p,
51 ifindex_t ifindex)
52 {
53 int ret;
54
55 ret = setsockopt_ipv4_multicast (top->fd, IP_ADD_MEMBERSHIP,
56 p->u.prefix4, htonl (OSPF_ALLSPFROUTERS),
57 ifindex);
58 if (ret < 0)
59 zlog_warn ("can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, "
60 "ifindex %u, AllSPFRouters): %s; perhaps a kernel limit "
61 "on # of multicast group memberships has been exceeded?",
62 top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno));
63 else
64 zlog_debug ("interface %s [%u] join AllSPFRouters Multicast group.",
65 inet_ntoa (p->u.prefix4), ifindex);
66
67 return ret;
68 }
69
70 int
71 ospf_if_drop_allspfrouters (struct ospf *top, struct prefix *p,
72 ifindex_t ifindex)
73 {
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)
80 zlog_warn ("can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, "
81 "ifindex %u, AllSPFRouters): %s",
82 top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno));
83 else
84 zlog_debug ("interface %s [%u] leave AllSPFRouters Multicast group.",
85 inet_ntoa (p->u.prefix4), ifindex);
86
87 return ret;
88 }
89
90 /* Join to the OSPF ALL Designated ROUTERS multicast group. */
91 int
92 ospf_if_add_alldrouters (struct ospf *top, struct prefix *p, ifindex_t ifindex)
93 {
94 int ret;
95
96 ret = setsockopt_ipv4_multicast (top->fd, IP_ADD_MEMBERSHIP,
97 p->u.prefix4, htonl (OSPF_ALLDROUTERS),
98 ifindex);
99 if (ret < 0)
100 zlog_warn ("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, safe_strerror(errno));
104 else
105 zlog_debug ("interface %s [%u] join AllDRouters Multicast group.",
106 inet_ntoa (p->u.prefix4), ifindex);
107
108 return ret;
109 }
110
111 int
112 ospf_if_drop_alldrouters (struct ospf *top, struct prefix *p, ifindex_t ifindex)
113 {
114 int ret;
115
116 ret = setsockopt_ipv4_multicast (top->fd, IP_DROP_MEMBERSHIP,
117 p->u.prefix4, htonl (OSPF_ALLDROUTERS),
118 ifindex);
119 if (ret < 0)
120 zlog_warn ("can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, "
121 "ifindex %u, AllDRouters): %s",
122 top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno));
123 else
124 zlog_debug ("interface %s [%u] leave AllDRouters Multicast group.",
125 inet_ntoa (p->u.prefix4), ifindex);
126
127 return ret;
128 }
129
130 int
131 ospf_if_ipmulticast (struct ospf *top, struct prefix *p, ifindex_t ifindex)
132 {
133 u_char val;
134 int ret, len;
135
136 val = 0;
137 len = sizeof (val);
138
139 /* Prevent receiving self-origined multicast packets. */
140 ret = setsockopt (top->fd, IPPROTO_IP, IP_MULTICAST_LOOP, (void *)&val, len);
141 if (ret < 0)
142 zlog_warn ("can't setsockopt IP_MULTICAST_LOOP(0) for fd %d: %s",
143 top->fd, safe_strerror(errno));
144
145 /* Explicitly set multicast ttl to 1 -- endo. */
146 val = 1;
147 ret = setsockopt (top->fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&val, len);
148 if (ret < 0)
149 zlog_warn ("can't setsockopt IP_MULTICAST_TTL(1) for fd %d: %s",
150 top->fd, safe_strerror (errno));
151
152 ret = setsockopt_ipv4_multicast_if (top->fd, p->u.prefix4, ifindex);
153 if (ret < 0)
154 zlog_warn("can't setsockopt IP_MULTICAST_IF(fd %d, addr %s, "
155 "ifindex %u): %s",
156 top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno));
157
158 return ret;
159 }
160
161 int
162 ospf_sock_init (void)
163 {
164 int ospf_sock;
165 int ret, hincl = 1;
166 int bufsize = (8 * 1024 * 1024);
167 int optval;
168 socklen_t optlen = sizeof(optval);
169
170 if ( ospfd_privs.change (ZPRIVS_RAISE) )
171 zlog_err ("ospf_sock_init: could not raise privs, %s",
172 safe_strerror (errno) );
173
174 ospf_sock = socket (AF_INET, SOCK_RAW, IPPROTO_OSPFIGP);
175 if (ospf_sock < 0)
176 {
177 int save_errno = errno;
178 if ( ospfd_privs.change (ZPRIVS_LOWER) )
179 zlog_err ("ospf_sock_init: could not lower privs, %s",
180 safe_strerror (errno) );
181 zlog_err ("ospf_read_sock_init: socket: %s", safe_strerror (save_errno));
182 exit(1);
183 }
184
185 #ifdef IP_HDRINCL
186 /* we will include IP header with packet */
187 ret = setsockopt (ospf_sock, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof (hincl));
188 if (ret < 0)
189 {
190 int save_errno = errno;
191 if ( ospfd_privs.change (ZPRIVS_LOWER) )
192 zlog_err ("ospf_sock_init: could not lower privs, %s",
193 safe_strerror (errno) );
194 zlog_warn ("Can't set IP_HDRINCL option for fd %d: %s",
195 ospf_sock, safe_strerror(save_errno));
196 }
197 #elif defined (IPTOS_PREC_INTERNETCONTROL)
198 #warning "IP_HDRINCL not available on this system"
199 #warning "using IPTOS_PREC_INTERNETCONTROL"
200 ret = setsockopt_ipv4_tos(ospf_sock, IPTOS_PREC_INTERNETCONTROL);
201 if (ret < 0)
202 {
203 int save_errno = errno;
204 if ( ospfd_privs.change (ZPRIVS_LOWER) )
205 zlog_err ("ospf_sock_init: could not lower privs, %s",
206 safe_strerror (errno) );
207 zlog_warn ("can't set sockopt IP_TOS %d to socket %d: %s",
208 tos, ospf_sock, safe_strerror(save_errno));
209 close (ospf_sock); /* Prevent sd leak. */
210 return ret;
211 }
212 #else /* !IPTOS_PREC_INTERNETCONTROL */
213 #warning "IP_HDRINCL not available, nor is IPTOS_PREC_INTERNETCONTROL"
214 zlog_warn ("IP_HDRINCL option not available");
215 #endif /* IP_HDRINCL */
216
217 ret = setsockopt_ifindex (AF_INET, ospf_sock, 1);
218
219 if (ret < 0)
220 zlog_warn ("Can't set pktinfo option for fd %d", ospf_sock);
221
222 if (ospfd_privs.change (ZPRIVS_LOWER))
223 {
224 zlog_err ("ospf_sock_init: could not lower privs, %s",
225 safe_strerror (errno) );
226 }
227
228 if ((ret = setsockopt (ospf_sock, SOL_SOCKET, SO_RCVBUF,
229 &bufsize, sizeof (bufsize))) < 0)
230 {
231 zlog_err ("Couldn't increase raw rbuf size: %s\n", safe_strerror(errno));
232 }
233
234 if ((ret = getsockopt (ospf_sock, SOL_SOCKET, SO_RCVBUF,
235 &optval, &optlen)) < 0)
236 {
237 zlog_err("getsockopt of SO_RCVBUF failed with error %s\n", safe_strerror(errno));
238 }
239 if (optval < bufsize)
240 {
241 zlog_err("Unable to SO_RCVBUF to %d, set to %d\n", bufsize, optval);
242 }
243
244
245 if ((ret = setsockopt (ospf_sock, SOL_SOCKET, SO_SNDBUF,
246 &bufsize, sizeof (bufsize))) < 0)
247 {
248 zlog_err ("Couldn't increase raw wbuf size: %s\n", safe_strerror(errno));
249 }
250
251 if ((ret = getsockopt (ospf_sock, SOL_SOCKET, SO_SNDBUF,
252 &optval, &optlen)) < 0)
253 {
254 zlog_err ("getsockopt of SO_SNDBUF failed with error %s\n", safe_strerror(errno));
255 }
256 if (optval < bufsize)
257 {
258 zlog_err ("Unable to SO_SNDBUF to %d, set to %d\n", bufsize, optval);
259 }
260
261 return ospf_sock;
262 }
263
264 void
265 ospf_adjust_sndbuflen (struct ospf * ospf, unsigned int buflen)
266 {
267 int ret, newbuflen;
268 /* Check if any work has to be done at all. */
269 if (ospf->maxsndbuflen >= buflen)
270 return;
271 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
272 zlog_debug ("%s: adjusting OSPF send buffer size to %d",
273 __func__, buflen);
274 if (ospfd_privs.change (ZPRIVS_RAISE))
275 zlog_err ("%s: could not raise privs, %s", __func__,
276 safe_strerror (errno));
277 /* Now we try to set SO_SNDBUF to what our caller has requested
278 * (the MTU of a newly added interface). However, if the OS has
279 * truncated the actual buffer size to somewhat less size, try
280 * to detect it and update our records appropriately. The OS
281 * may allocate more buffer space, than requested, this isn't
282 * a error.
283 */
284 ret = setsockopt_so_sendbuf (ospf->fd, buflen);
285 newbuflen = getsockopt_so_sendbuf (ospf->fd);
286 if (ret < 0 || newbuflen < 0 || newbuflen < (int) buflen)
287 zlog_warn ("%s: tried to set SO_SNDBUF to %u, but got %d",
288 __func__, buflen, newbuflen);
289 if (newbuflen >= 0)
290 ospf->maxsndbuflen = (unsigned int)newbuflen;
291 else
292 zlog_warn ("%s: failed to get SO_SNDBUF", __func__);
293 if (ospfd_privs.change (ZPRIVS_LOWER))
294 zlog_err ("%s: could not lower privs, %s", __func__,
295 safe_strerror (errno));
296 }