]> git.proxmox.com Git - mirror_frr.git/blob - ospf6d/ospf6_network.c
Merge pull request #7466 from idryzhov/fix-bfd-null-deref
[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 along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include "log.h"
24 #include "memory.h"
25 #include "sockunion.h"
26 #include "sockopt.h"
27 #include "privs.h"
28 #include "lib_errors.h"
29 #include "vrf.h"
30
31 #include "libospf.h"
32 #include "ospf6_proto.h"
33 #include "ospf6_top.h"
34 #include "ospf6_network.h"
35 #include "ospf6d.h"
36
37 struct in6_addr allspfrouters6;
38 struct in6_addr alldrouters6;
39
40 /* setsockopt MulticastLoop to off */
41 static void ospf6_reset_mcastloop(int ospf6_sock)
42 {
43 unsigned int off = 0;
44 if (setsockopt(ospf6_sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &off,
45 sizeof(unsigned int))
46 < 0)
47 zlog_warn("Network: reset IPV6_MULTICAST_LOOP failed: %s",
48 safe_strerror(errno));
49 }
50
51 static void ospf6_set_pktinfo(int ospf6_sock)
52 {
53 setsockopt_ipv6_pktinfo(ospf6_sock, 1);
54 }
55
56 static void ospf6_set_transport_class(int ospf6_sock)
57 {
58 #ifdef IPTOS_PREC_INTERNETCONTROL
59 setsockopt_ipv6_tclass(ospf6_sock, IPTOS_PREC_INTERNETCONTROL);
60 #endif
61 }
62
63 static void ospf6_set_checksum(int ospf6_sock)
64 {
65 int offset = 12;
66 #ifndef DISABLE_IPV6_CHECKSUM
67 if (setsockopt(ospf6_sock, IPPROTO_IPV6, IPV6_CHECKSUM, &offset,
68 sizeof(offset))
69 < 0)
70 zlog_warn("Network: set IPV6_CHECKSUM failed: %s",
71 safe_strerror(errno));
72 #else
73 zlog_warn("Network: Don't set IPV6_CHECKSUM");
74 #endif /* DISABLE_IPV6_CHECKSUM */
75 }
76
77 void ospf6_serv_close(int *ospf6_sock)
78 {
79 if (*ospf6_sock != -1) {
80 close(*ospf6_sock);
81 *ospf6_sock = -1;
82 return;
83 }
84 }
85
86 /* Make ospf6d's server socket. */
87 int ospf6_serv_sock(struct ospf6 *ospf6)
88 {
89 int ospf6_sock;
90
91 if (ospf6->fd != -1)
92 return -1;
93
94 if (ospf6->vrf_id == VRF_UNKNOWN)
95 return -1;
96
97 frr_with_privs(&ospf6d_privs) {
98
99 ospf6_sock = vrf_socket(AF_INET6, SOCK_RAW, IPPROTO_OSPFIGP,
100 ospf6->vrf_id, ospf6->name);
101 if (ospf6_sock < 0) {
102 zlog_warn("Network: can't create OSPF6 socket.");
103 return -1;
104 }
105 }
106
107 /* set socket options */
108 #if 1
109 sockopt_reuseaddr(ospf6_sock);
110 #else
111 ospf6_set_reuseaddr();
112 #endif /*1*/
113 ospf6_reset_mcastloop(ospf6_sock);
114 ospf6_set_pktinfo(ospf6_sock);
115 ospf6_set_transport_class(ospf6_sock);
116 ospf6_set_checksum(ospf6_sock);
117
118 ospf6->fd = ospf6_sock;
119 /* setup global in6_addr, allspf6 and alldr6 for later use */
120 inet_pton(AF_INET6, ALLSPFROUTERS6, &allspfrouters6);
121 inet_pton(AF_INET6, ALLDROUTERS6, &alldrouters6);
122
123 return 0;
124 }
125
126 /* ospf6 set socket option */
127 int ospf6_sso(ifindex_t ifindex, struct in6_addr *group, int option, int sockfd)
128 {
129 struct ipv6_mreq mreq6;
130 int ret;
131 int bufsize = (8 * 1024 * 1024);
132
133 if (sockfd == -1)
134 return -1;
135
136 assert(ifindex);
137 mreq6.ipv6mr_interface = ifindex;
138 memcpy(&mreq6.ipv6mr_multiaddr, group, sizeof(struct in6_addr));
139
140 ret = setsockopt(sockfd, IPPROTO_IPV6, option, &mreq6, sizeof(mreq6));
141 if (ret < 0) {
142 flog_err_sys(
143 EC_LIB_SOCKET,
144 "Network: setsockopt (%d) on ifindex %d failed: %s",
145 option, ifindex, safe_strerror(errno));
146 return ret;
147 }
148
149 setsockopt_so_sendbuf(sockfd, bufsize);
150 setsockopt_so_recvbuf(sockfd, bufsize);
151
152 return 0;
153 }
154
155 static int iov_count(struct iovec *iov)
156 {
157 int i;
158 for (i = 0; iov[i].iov_base; i++)
159 ;
160 return i;
161 }
162
163 static int iov_totallen(struct iovec *iov)
164 {
165 int i;
166 int totallen = 0;
167 for (i = 0; iov[i].iov_base; i++)
168 totallen += iov[i].iov_len;
169 return totallen;
170 }
171
172 int ospf6_sendmsg(struct in6_addr *src, struct in6_addr *dst,
173 ifindex_t ifindex, struct iovec *message, int ospf6_sock)
174 {
175 int retval;
176 struct msghdr smsghdr;
177 struct cmsghdr *scmsgp;
178 union {
179 struct cmsghdr hdr;
180 uint8_t buf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
181 } cmsgbuf;
182 struct in6_pktinfo *pktinfo;
183 struct sockaddr_in6 dst_sin6;
184
185 assert(dst);
186
187 memset(&cmsgbuf, 0, sizeof(cmsgbuf));
188 scmsgp = (struct cmsghdr *)&cmsgbuf;
189 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
190 memset(&dst_sin6, 0, sizeof(struct sockaddr_in6));
191
192 /* source address */
193 pktinfo->ipi6_ifindex = ifindex;
194 if (src)
195 memcpy(&pktinfo->ipi6_addr, src, sizeof(struct in6_addr));
196 else
197 memset(&pktinfo->ipi6_addr, 0, sizeof(struct in6_addr));
198
199 /* destination address */
200 dst_sin6.sin6_family = AF_INET6;
201 #ifdef SIN6_LEN
202 dst_sin6.sin6_len = sizeof(struct sockaddr_in6);
203 #endif /*SIN6_LEN*/
204 memcpy(&dst_sin6.sin6_addr, dst, sizeof(struct in6_addr));
205 dst_sin6.sin6_scope_id = ifindex;
206
207 /* send control msg */
208 scmsgp->cmsg_level = IPPROTO_IPV6;
209 scmsgp->cmsg_type = IPV6_PKTINFO;
210 scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
211 /* scmsgp = CMSG_NXTHDR (&smsghdr, scmsgp); */
212
213 /* send msg hdr */
214 memset(&smsghdr, 0, sizeof(smsghdr));
215 smsghdr.msg_iov = message;
216 smsghdr.msg_iovlen = iov_count(message);
217 smsghdr.msg_name = (caddr_t)&dst_sin6;
218 smsghdr.msg_namelen = sizeof(struct sockaddr_in6);
219 smsghdr.msg_control = (caddr_t)&cmsgbuf.buf;
220 smsghdr.msg_controllen = sizeof(cmsgbuf.buf);
221
222 retval = sendmsg(ospf6_sock, &smsghdr, 0);
223 if (retval != iov_totallen(message))
224 zlog_warn("sendmsg failed: source: %pI6 Dest: %pI6 ifindex: %d: %s (%d)",
225 src, dst, ifindex,
226 safe_strerror(errno), errno);
227
228 return retval;
229 }
230
231 int ospf6_recvmsg(struct in6_addr *src, struct in6_addr *dst,
232 ifindex_t *ifindex, struct iovec *message, int ospf6_sock)
233 {
234 int retval;
235 struct msghdr rmsghdr;
236 struct cmsghdr *rcmsgp;
237 uint8_t cmsgbuf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
238 struct in6_pktinfo *pktinfo;
239 struct sockaddr_in6 src_sin6;
240
241 rcmsgp = (struct cmsghdr *)cmsgbuf;
242 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(rcmsgp));
243 memset(&src_sin6, 0, sizeof(struct sockaddr_in6));
244
245 /* receive control msg */
246 rcmsgp->cmsg_level = IPPROTO_IPV6;
247 rcmsgp->cmsg_type = IPV6_PKTINFO;
248 rcmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
249 /* rcmsgp = CMSG_NXTHDR (&rmsghdr, rcmsgp); */
250
251 /* receive msg hdr */
252 memset(&rmsghdr, 0, sizeof(rmsghdr));
253 rmsghdr.msg_iov = message;
254 rmsghdr.msg_iovlen = iov_count(message);
255 rmsghdr.msg_name = (caddr_t)&src_sin6;
256 rmsghdr.msg_namelen = sizeof(struct sockaddr_in6);
257 rmsghdr.msg_control = (caddr_t)cmsgbuf;
258 rmsghdr.msg_controllen = sizeof(cmsgbuf);
259
260 retval = recvmsg(ospf6_sock, &rmsghdr, 0);
261 if (retval < 0)
262 zlog_warn("recvmsg failed: %s", safe_strerror(errno));
263 else if (retval == iov_totallen(message))
264 zlog_warn("recvmsg read full buffer size: %d", retval);
265
266 /* source address */
267 assert(src);
268 memcpy(src, &src_sin6.sin6_addr, sizeof(struct in6_addr));
269
270 /* destination address */
271 if (ifindex)
272 *ifindex = pktinfo->ipi6_ifindex;
273 if (dst)
274 memcpy(dst, &pktinfo->ipi6_addr, sizeof(struct in6_addr));
275
276 return retval;
277 }