]> git.proxmox.com Git - mirror_frr.git/blob - ospf6d/ospf6_network.c
Merge remote-tracking branch 'origin/cmaster' into cmaster-next
[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 MulticastLoop to off */
41 static void
42 ospf6_reset_mcastloop (void)
43 {
44 u_int off = 0;
45 if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
46 &off, sizeof (u_int)) < 0)
47 zlog_warn ("Network: reset IPV6_MULTICAST_LOOP failed: %s",
48 safe_strerror (errno));
49 }
50
51 static void
52 ospf6_set_pktinfo (void)
53 {
54 setsockopt_ipv6_pktinfo (ospf6_sock, 1);
55 }
56
57 static void
58 ospf6_set_transport_class (void)
59 {
60 #ifdef IPTOS_PREC_INTERNETCONTROL
61 setsockopt_ipv6_tclass (ospf6_sock, IPTOS_PREC_INTERNETCONTROL);
62 #endif
63 }
64
65 static void
66 ospf6_set_checksum (void)
67 {
68 int offset = 12;
69 #ifndef DISABLE_IPV6_CHECKSUM
70 if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_CHECKSUM,
71 &offset, sizeof (offset)) < 0)
72 zlog_warn ("Network: set IPV6_CHECKSUM failed: %s", safe_strerror (errno));
73 #else
74 zlog_warn ("Network: Don't set IPV6_CHECKSUM");
75 #endif /* DISABLE_IPV6_CHECKSUM */
76 }
77
78 /* Make ospf6d's server socket. */
79 int
80 ospf6_serv_sock (void)
81 {
82 if (ospf6d_privs.change (ZPRIVS_RAISE))
83 zlog_err ("ospf6_serv_sock: could not raise privs");
84
85 ospf6_sock = socket (AF_INET6, SOCK_RAW, IPPROTO_OSPFIGP);
86 if (ospf6_sock < 0)
87 {
88 zlog_warn ("Network: can't create OSPF6 socket.");
89 if (ospf6d_privs.change (ZPRIVS_LOWER))
90 zlog_err ("ospf_sock_init: could not lower privs");
91 return -1;
92 }
93 if (ospf6d_privs.change (ZPRIVS_LOWER))
94 zlog_err ("ospf_sock_init: could not lower privs");
95
96 /* set socket options */
97 #if 1
98 sockopt_reuseaddr (ospf6_sock);
99 #else
100 ospf6_set_reuseaddr ();
101 #endif /*1*/
102 ospf6_reset_mcastloop ();
103 ospf6_set_pktinfo ();
104 ospf6_set_transport_class ();
105 ospf6_set_checksum ();
106
107 /* setup global in6_addr, allspf6 and alldr6 for later use */
108 inet_pton (AF_INET6, ALLSPFROUTERS6, &allspfrouters6);
109 inet_pton (AF_INET6, ALLDROUTERS6, &alldrouters6);
110
111 return 0;
112 }
113
114 /* ospf6 set socket option */
115 int
116 ospf6_sso (ifindex_t ifindex, struct in6_addr *group, int option)
117 {
118 struct ipv6_mreq mreq6;
119 int ret;
120 int bufsize = (8 * 1024 * 1024);
121 int optval;
122 socklen_t optlen = sizeof(optval);
123
124 assert (ifindex);
125 mreq6.ipv6mr_interface = ifindex;
126 memcpy (&mreq6.ipv6mr_multiaddr, group, sizeof (struct in6_addr));
127
128 ret = setsockopt (ospf6_sock, IPPROTO_IPV6, option,
129 &mreq6, sizeof (mreq6));
130 if (ret < 0)
131 {
132 zlog_err ("Network: setsockopt (%d) on ifindex %d failed: %s",
133 option, ifindex, safe_strerror (errno));
134 return ret;
135 }
136
137 if ((ret = setsockopt (ospf6_sock, SOL_SOCKET, SO_SNDBUF,
138 &bufsize, sizeof (bufsize))) < 0)
139 {
140 zlog_err ("Couldn't increase raw wbuf size: %s\n", safe_strerror(errno));
141 return ret;
142 }
143
144 if ((ret = getsockopt (ospf6_sock, SOL_SOCKET, SO_SNDBUF,
145 &optval, &optlen)) < 0)
146 {
147 zlog_err ("getsockopt of SO_SNDBUF failed with error %s\n", safe_strerror(errno));
148 return ret;
149 }
150 else if (optval < bufsize)
151 {
152 zlog_err ("Unable to SO_SNDBUF to %d, set to %d\n", bufsize, optval);
153 }
154
155 if ((ret = setsockopt (ospf6_sock, SOL_SOCKET, SO_RCVBUF,
156 &bufsize, sizeof (bufsize))) < 0)
157 {
158 zlog_err ("Couldn't increase raw rbuf size: %s\n", safe_strerror(errno));
159 }
160
161 if ((ret = getsockopt (ospf6_sock, SOL_SOCKET, SO_RCVBUF,
162 &optval, &optlen)) < 0)
163 {
164 zlog_err ("getsockopt of SO_RCVBUF failed with error %s\n", safe_strerror(errno));
165 return ret;
166 }
167 else if (optval < bufsize)
168 {
169 zlog_err ("Unable to SO_RCVBUF to %d, set to %d\n", bufsize, optval);
170 }
171
172 return 0;
173 }
174
175 static int
176 iov_count (struct iovec *iov)
177 {
178 int i;
179 for (i = 0; iov[i].iov_base; i++)
180 ;
181 return i;
182 }
183
184 static int
185 iov_totallen (struct iovec *iov)
186 {
187 int i;
188 int totallen = 0;
189 for (i = 0; iov[i].iov_base; i++)
190 totallen += iov[i].iov_len;
191 return totallen;
192 }
193
194 int
195 ospf6_sendmsg (struct in6_addr *src, struct in6_addr *dst,
196 ifindex_t *ifindex, struct iovec *message)
197 {
198 int retval;
199 struct msghdr smsghdr;
200 struct cmsghdr *scmsgp;
201 union
202 {
203 struct cmsghdr hdr;
204 u_char buf[CMSG_SPACE (sizeof (struct in6_pktinfo))];
205 } cmsgbuf;
206 struct in6_pktinfo *pktinfo;
207 struct sockaddr_in6 dst_sin6;
208
209 assert (dst);
210 assert (*ifindex);
211
212 scmsgp = (struct cmsghdr *)&cmsgbuf;
213 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
214 memset (&dst_sin6, 0, sizeof (struct sockaddr_in6));
215
216 /* source address */
217 pktinfo->ipi6_ifindex = *ifindex;
218 if (src)
219 memcpy (&pktinfo->ipi6_addr, src, sizeof (struct in6_addr));
220 else
221 memset (&pktinfo->ipi6_addr, 0, sizeof (struct in6_addr));
222
223 /* destination address */
224 dst_sin6.sin6_family = AF_INET6;
225 #ifdef SIN6_LEN
226 dst_sin6.sin6_len = sizeof (struct sockaddr_in6);
227 #endif /*SIN6_LEN*/
228 memcpy (&dst_sin6.sin6_addr, dst, sizeof (struct in6_addr));
229 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
230 dst_sin6.sin6_scope_id = *ifindex;
231 #endif
232
233 /* send control msg */
234 scmsgp->cmsg_level = IPPROTO_IPV6;
235 scmsgp->cmsg_type = IPV6_PKTINFO;
236 scmsgp->cmsg_len = CMSG_LEN (sizeof (struct in6_pktinfo));
237 /* scmsgp = CMSG_NXTHDR (&smsghdr, scmsgp); */
238
239 /* send msg hdr */
240 memset (&smsghdr, 0, sizeof (smsghdr));
241 smsghdr.msg_iov = message;
242 smsghdr.msg_iovlen = iov_count (message);
243 smsghdr.msg_name = (caddr_t) &dst_sin6;
244 smsghdr.msg_namelen = sizeof (struct sockaddr_in6);
245 smsghdr.msg_control = (caddr_t) &cmsgbuf.buf;
246 smsghdr.msg_controllen = sizeof(cmsgbuf.buf);
247
248 retval = sendmsg (ospf6_sock, &smsghdr, 0);
249 if (retval != iov_totallen (message))
250 zlog_warn ("sendmsg failed: ifindex: %d: %s (%d)",
251 *ifindex, safe_strerror (errno), errno);
252
253 return retval;
254 }
255
256 int
257 ospf6_recvmsg (struct in6_addr *src, struct in6_addr *dst,
258 ifindex_t *ifindex, struct iovec *message)
259 {
260 int retval;
261 struct msghdr rmsghdr;
262 struct cmsghdr *rcmsgp;
263 u_char cmsgbuf[CMSG_SPACE(sizeof (struct in6_pktinfo))];
264 struct in6_pktinfo *pktinfo;
265 struct sockaddr_in6 src_sin6;
266
267 rcmsgp = (struct cmsghdr *)cmsgbuf;
268 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(rcmsgp));
269 memset (&src_sin6, 0, sizeof (struct sockaddr_in6));
270
271 /* receive control msg */
272 rcmsgp->cmsg_level = IPPROTO_IPV6;
273 rcmsgp->cmsg_type = IPV6_PKTINFO;
274 rcmsgp->cmsg_len = CMSG_LEN (sizeof (struct in6_pktinfo));
275 /* rcmsgp = CMSG_NXTHDR (&rmsghdr, rcmsgp); */
276
277 /* receive msg hdr */
278 memset (&rmsghdr, 0, sizeof (rmsghdr));
279 rmsghdr.msg_iov = message;
280 rmsghdr.msg_iovlen = iov_count (message);
281 rmsghdr.msg_name = (caddr_t) &src_sin6;
282 rmsghdr.msg_namelen = sizeof (struct sockaddr_in6);
283 rmsghdr.msg_control = (caddr_t) cmsgbuf;
284 rmsghdr.msg_controllen = sizeof (cmsgbuf);
285
286 retval = recvmsg (ospf6_sock, &rmsghdr, 0);
287 if (retval < 0)
288 zlog_warn ("recvmsg failed: %s", safe_strerror (errno));
289 else if (retval == iov_totallen (message))
290 zlog_warn ("recvmsg read full buffer size: %d", retval);
291
292 /* source address */
293 assert (src);
294 memcpy (src, &src_sin6.sin6_addr, sizeof (struct in6_addr));
295
296 /* destination address */
297 if (ifindex)
298 *ifindex = pktinfo->ipi6_ifindex;
299 if (dst)
300 memcpy (dst, &pktinfo->ipi6_addr, sizeof (struct in6_addr));
301
302 return retval;
303 }
304
305