]> git.proxmox.com Git - mirror_frr.git/blob - ospf6d/ospf6_network.c
Increase SO_SNDBUF and SO_RCVBUF to 8MB to avoid drops in large networks.
[mirror_frr.git] / ospf6d / ospf6_network.c
1 /*
2 * Copyright (C) 2003 Yasuhiro Ohara
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
22 #include <zebra.h>
23
24 #include "log.h"
25 #include "memory.h"
26 #include "sockunion.h"
27 #include "sockopt.h"
28 #include "privs.h"
29
30 #include "libospf.h"
31 #include "ospf6_proto.h"
32 #include "ospf6_network.h"
33
34 extern struct zebra_privs_t ospf6d_privs;
35
36 int ospf6_sock;
37 struct in6_addr allspfrouters6;
38 struct in6_addr alldrouters6;
39
40 /* setsockopt ReUseAddr to on */
41 void
42 ospf6_set_reuseaddr (void)
43 {
44 u_int on = 0;
45 if (setsockopt (ospf6_sock, SOL_SOCKET, SO_REUSEADDR, &on,
46 sizeof (u_int)) < 0)
47 zlog_warn ("Network: set SO_REUSEADDR failed: %s", safe_strerror (errno));
48 }
49
50 /* setsockopt MulticastLoop to off */
51 void
52 ospf6_reset_mcastloop (void)
53 {
54 u_int off = 0;
55 if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
56 &off, sizeof (u_int)) < 0)
57 zlog_warn ("Network: reset IPV6_MULTICAST_LOOP failed: %s",
58 safe_strerror (errno));
59 }
60
61 void
62 ospf6_set_pktinfo (void)
63 {
64 setsockopt_ipv6_pktinfo (ospf6_sock, 1);
65 }
66
67 void
68 ospf6_set_transport_class (void)
69 {
70 #ifdef IPTOS_PREC_INTERNETCONTROL
71 setsockopt_ipv6_tclass (ospf6_sock, IPTOS_PREC_INTERNETCONTROL);
72 #endif
73 }
74
75 void
76 ospf6_set_checksum (void)
77 {
78 int offset = 12;
79 #ifndef DISABLE_IPV6_CHECKSUM
80 if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_CHECKSUM,
81 &offset, sizeof (offset)) < 0)
82 zlog_warn ("Network: set IPV6_CHECKSUM failed: %s", safe_strerror (errno));
83 #else
84 zlog_warn ("Network: Don't set IPV6_CHECKSUM");
85 #endif /* DISABLE_IPV6_CHECKSUM */
86 }
87
88 /* Make ospf6d's server socket. */
89 int
90 ospf6_serv_sock (void)
91 {
92 if (ospf6d_privs.change (ZPRIVS_RAISE))
93 zlog_err ("ospf6_serv_sock: could not raise privs");
94
95 ospf6_sock = socket (AF_INET6, SOCK_RAW, IPPROTO_OSPFIGP);
96 if (ospf6_sock < 0)
97 {
98 zlog_warn ("Network: can't create OSPF6 socket.");
99 if (ospf6d_privs.change (ZPRIVS_LOWER))
100 zlog_err ("ospf_sock_init: could not lower privs");
101 return -1;
102 }
103 if (ospf6d_privs.change (ZPRIVS_LOWER))
104 zlog_err ("ospf_sock_init: could not lower privs");
105
106 /* set socket options */
107 #if 1
108 sockopt_reuseaddr (ospf6_sock);
109 #else
110 ospf6_set_reuseaddr ();
111 #endif /*1*/
112 ospf6_reset_mcastloop ();
113 ospf6_set_pktinfo ();
114 ospf6_set_transport_class ();
115 ospf6_set_checksum ();
116
117 /* setup global in6_addr, allspf6 and alldr6 for later use */
118 inet_pton (AF_INET6, ALLSPFROUTERS6, &allspfrouters6);
119 inet_pton (AF_INET6, ALLDROUTERS6, &alldrouters6);
120
121 return 0;
122 }
123
124 /* ospf6 set socket option */
125 void
126 ospf6_sso (u_int ifindex, struct in6_addr *group, int option)
127 {
128 struct ipv6_mreq mreq6;
129 int ret;
130 int bufsize = (8 * 1024 * 1024);
131 int optval;
132 socklen_t optlen = sizeof(optval);
133
134 assert (ifindex);
135 mreq6.ipv6mr_interface = ifindex;
136 memcpy (&mreq6.ipv6mr_multiaddr, group, sizeof (struct in6_addr));
137
138 ret = setsockopt (ospf6_sock, IPPROTO_IPV6, option,
139 &mreq6, sizeof (mreq6));
140 if (ret < 0)
141 zlog_err ("Network: setsockopt (%d) on ifindex %d failed: %s",
142 option, ifindex, safe_strerror (errno));
143
144 if ((ret = setsockopt (ospf6_sock, SOL_SOCKET, SO_SNDBUF,
145 &bufsize, sizeof (bufsize))) < 0)
146 {
147 zlog_err ("Couldn't increase raw wbuf size: %s\n", safe_strerror(errno));
148 }
149
150 if ((ret = getsockopt (ospf6_sock, SOL_SOCKET, SO_SNDBUF,
151 &optval, &optlen)) < 0)
152 {
153 zlog_err ("getsockopt of SO_SNDBUF failed with error %s\n", safe_strerror(errno));
154 }
155 else if (optval < bufsize)
156 {
157 zlog_err ("Unable to SO_SNDBUF to %d, set to %d\n", bufsize, optval);
158 }
159
160 if ((ret = setsockopt (ospf6_sock, SOL_SOCKET, SO_RCVBUF,
161 &bufsize, sizeof (bufsize))) < 0)
162 {
163 zlog_err ("Couldn't increase raw rbuf size: %s\n", safe_strerror(errno));
164 }
165
166 if ((ret = getsockopt (ospf6_sock, SOL_SOCKET, SO_RCVBUF,
167 &optval, &optlen)) < 0)
168 {
169 zlog_err ("getsockopt of SO_RCVBUF failed with error %s\n", safe_strerror(errno));
170 }
171 else if (optval < bufsize)
172 {
173 zlog_err ("Unable to SO_RCVBUF to %d, set to %d\n", bufsize, optval);
174 }
175 }
176
177 static int
178 iov_count (struct iovec *iov)
179 {
180 int i;
181 for (i = 0; iov[i].iov_base; i++)
182 ;
183 return i;
184 }
185
186 static int
187 iov_totallen (struct iovec *iov)
188 {
189 int i;
190 int totallen = 0;
191 for (i = 0; iov[i].iov_base; i++)
192 totallen += iov[i].iov_len;
193 return totallen;
194 }
195
196 int
197 ospf6_sendmsg (struct in6_addr *src, struct in6_addr *dst,
198 unsigned int *ifindex, struct iovec *message)
199 {
200 int retval;
201 struct msghdr smsghdr;
202 struct cmsghdr *scmsgp;
203 u_char cmsgbuf[CMSG_SPACE(sizeof (struct in6_pktinfo))];
204 struct in6_pktinfo *pktinfo;
205 struct sockaddr_in6 dst_sin6;
206
207 assert (dst);
208 assert (*ifindex);
209
210 scmsgp = (struct cmsghdr *)cmsgbuf;
211 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
212 memset (&dst_sin6, 0, sizeof (struct sockaddr_in6));
213
214 /* source address */
215 pktinfo->ipi6_ifindex = *ifindex;
216 if (src)
217 memcpy (&pktinfo->ipi6_addr, src, sizeof (struct in6_addr));
218 else
219 memset (&pktinfo->ipi6_addr, 0, sizeof (struct in6_addr));
220
221 /* destination address */
222 dst_sin6.sin6_family = AF_INET6;
223 #ifdef SIN6_LEN
224 dst_sin6.sin6_len = sizeof (struct sockaddr_in6);
225 #endif /*SIN6_LEN*/
226 memcpy (&dst_sin6.sin6_addr, dst, sizeof (struct in6_addr));
227 #ifdef HAVE_SIN6_SCOPE_ID
228 dst_sin6.sin6_scope_id = *ifindex;
229 #endif
230
231 /* send control msg */
232 scmsgp->cmsg_level = IPPROTO_IPV6;
233 scmsgp->cmsg_type = IPV6_PKTINFO;
234 scmsgp->cmsg_len = CMSG_LEN (sizeof (struct in6_pktinfo));
235 /* scmsgp = CMSG_NXTHDR (&smsghdr, scmsgp); */
236
237 /* send msg hdr */
238 memset (&smsghdr, 0, sizeof (smsghdr));
239 smsghdr.msg_iov = message;
240 smsghdr.msg_iovlen = iov_count (message);
241 smsghdr.msg_name = (caddr_t) &dst_sin6;
242 smsghdr.msg_namelen = sizeof (struct sockaddr_in6);
243 smsghdr.msg_control = (caddr_t) cmsgbuf;
244 smsghdr.msg_controllen = scmsgp->cmsg_len;
245
246 retval = sendmsg (ospf6_sock, &smsghdr, 0);
247 if (retval != iov_totallen (message))
248 zlog_warn ("sendmsg failed: ifindex: %d: %s (%d)",
249 *ifindex, safe_strerror (errno), errno);
250
251 return retval;
252 }
253
254 int
255 ospf6_recvmsg (struct in6_addr *src, struct in6_addr *dst,
256 unsigned int *ifindex, struct iovec *message)
257 {
258 int retval;
259 struct msghdr rmsghdr;
260 struct cmsghdr *rcmsgp;
261 u_char cmsgbuf[CMSG_SPACE(sizeof (struct in6_pktinfo))];
262 struct in6_pktinfo *pktinfo;
263 struct sockaddr_in6 src_sin6;
264
265 rcmsgp = (struct cmsghdr *)cmsgbuf;
266 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(rcmsgp));
267 memset (&src_sin6, 0, sizeof (struct sockaddr_in6));
268
269 /* receive control msg */
270 rcmsgp->cmsg_level = IPPROTO_IPV6;
271 rcmsgp->cmsg_type = IPV6_PKTINFO;
272 rcmsgp->cmsg_len = CMSG_LEN (sizeof (struct in6_pktinfo));
273 /* rcmsgp = CMSG_NXTHDR (&rmsghdr, rcmsgp); */
274
275 /* receive msg hdr */
276 memset (&rmsghdr, 0, sizeof (rmsghdr));
277 rmsghdr.msg_iov = message;
278 rmsghdr.msg_iovlen = iov_count (message);
279 rmsghdr.msg_name = (caddr_t) &src_sin6;
280 rmsghdr.msg_namelen = sizeof (struct sockaddr_in6);
281 rmsghdr.msg_control = (caddr_t) cmsgbuf;
282 rmsghdr.msg_controllen = sizeof (cmsgbuf);
283
284 retval = recvmsg (ospf6_sock, &rmsghdr, 0);
285 if (retval < 0)
286 zlog_warn ("recvmsg failed: %s", safe_strerror (errno));
287 else if (retval == iov_totallen (message))
288 zlog_warn ("recvmsg read full buffer size: %d", retval);
289
290 /* source address */
291 assert (src);
292 memcpy (src, &src_sin6.sin6_addr, sizeof (struct in6_addr));
293
294 /* destination address */
295 if (ifindex)
296 *ifindex = pktinfo->ipi6_ifindex;
297 if (dst)
298 memcpy (dst, &pktinfo->ipi6_addr, sizeof (struct in6_addr));
299
300 return retval;
301 }
302
303