]> git.proxmox.com Git - mirror_frr.git/blob - ospf6d/ospf6_network.c
Merge pull request #7414 from donaldsharp/32bitflags
[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)
128 {
129 struct ipv6_mreq mreq6;
130 int ret;
131 int bufsize = (8 * 1024 * 1024);
132
133 if (ospf6->fd == -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(ospf6->fd, IPPROTO_IPV6, option, &mreq6,
141 sizeof(mreq6));
142 if (ret < 0) {
143 flog_err_sys(
144 EC_LIB_SOCKET,
145 "Network: setsockopt (%d) on ifindex %d failed: %s",
146 option, ifindex, safe_strerror(errno));
147 return ret;
148 }
149
150 setsockopt_so_sendbuf(ospf6->fd, bufsize);
151 setsockopt_so_recvbuf(ospf6->fd, bufsize);
152
153 return 0;
154 }
155
156 static int iov_count(struct iovec *iov)
157 {
158 int i;
159 for (i = 0; iov[i].iov_base; i++)
160 ;
161 return i;
162 }
163
164 static int iov_totallen(struct iovec *iov)
165 {
166 int i;
167 int totallen = 0;
168 for (i = 0; iov[i].iov_base; i++)
169 totallen += iov[i].iov_len;
170 return totallen;
171 }
172
173 int ospf6_sendmsg(struct in6_addr *src, struct in6_addr *dst,
174 ifindex_t ifindex, struct iovec *message, int ospf6_sock)
175 {
176 int retval;
177 struct msghdr smsghdr;
178 struct cmsghdr *scmsgp;
179 union {
180 struct cmsghdr hdr;
181 uint8_t buf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
182 } cmsgbuf;
183 struct in6_pktinfo *pktinfo;
184 struct sockaddr_in6 dst_sin6;
185
186 assert(dst);
187
188 memset(&cmsgbuf, 0, sizeof(cmsgbuf));
189 scmsgp = (struct cmsghdr *)&cmsgbuf;
190 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
191 memset(&dst_sin6, 0, sizeof(struct sockaddr_in6));
192
193 /* source address */
194 pktinfo->ipi6_ifindex = ifindex;
195 if (src)
196 memcpy(&pktinfo->ipi6_addr, src, sizeof(struct in6_addr));
197 else
198 memset(&pktinfo->ipi6_addr, 0, sizeof(struct in6_addr));
199
200 /* destination address */
201 dst_sin6.sin6_family = AF_INET6;
202 #ifdef SIN6_LEN
203 dst_sin6.sin6_len = sizeof(struct sockaddr_in6);
204 #endif /*SIN6_LEN*/
205 memcpy(&dst_sin6.sin6_addr, dst, sizeof(struct in6_addr));
206 dst_sin6.sin6_scope_id = ifindex;
207
208 /* send control msg */
209 scmsgp->cmsg_level = IPPROTO_IPV6;
210 scmsgp->cmsg_type = IPV6_PKTINFO;
211 scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
212 /* scmsgp = CMSG_NXTHDR (&smsghdr, scmsgp); */
213
214 /* send msg hdr */
215 memset(&smsghdr, 0, sizeof(smsghdr));
216 smsghdr.msg_iov = message;
217 smsghdr.msg_iovlen = iov_count(message);
218 smsghdr.msg_name = (caddr_t)&dst_sin6;
219 smsghdr.msg_namelen = sizeof(struct sockaddr_in6);
220 smsghdr.msg_control = (caddr_t)&cmsgbuf.buf;
221 smsghdr.msg_controllen = sizeof(cmsgbuf.buf);
222
223 retval = sendmsg(ospf6_sock, &smsghdr, 0);
224 if (retval != iov_totallen(message))
225 zlog_warn("sendmsg failed: source: %pI6 Dest: %pI6 ifindex: %d: %s (%d)",
226 src, dst, ifindex,
227 safe_strerror(errno), errno);
228
229 return retval;
230 }
231
232 int ospf6_recvmsg(struct in6_addr *src, struct in6_addr *dst,
233 ifindex_t *ifindex, struct iovec *message, int ospf6_sock)
234 {
235 int retval;
236 struct msghdr rmsghdr;
237 struct cmsghdr *rcmsgp;
238 uint8_t cmsgbuf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
239 struct in6_pktinfo *pktinfo;
240 struct sockaddr_in6 src_sin6;
241
242 rcmsgp = (struct cmsghdr *)cmsgbuf;
243 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(rcmsgp));
244 memset(&src_sin6, 0, sizeof(struct sockaddr_in6));
245
246 /* receive control msg */
247 rcmsgp->cmsg_level = IPPROTO_IPV6;
248 rcmsgp->cmsg_type = IPV6_PKTINFO;
249 rcmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
250 /* rcmsgp = CMSG_NXTHDR (&rmsghdr, rcmsgp); */
251
252 /* receive msg hdr */
253 memset(&rmsghdr, 0, sizeof(rmsghdr));
254 rmsghdr.msg_iov = message;
255 rmsghdr.msg_iovlen = iov_count(message);
256 rmsghdr.msg_name = (caddr_t)&src_sin6;
257 rmsghdr.msg_namelen = sizeof(struct sockaddr_in6);
258 rmsghdr.msg_control = (caddr_t)cmsgbuf;
259 rmsghdr.msg_controllen = sizeof(cmsgbuf);
260
261 retval = recvmsg(ospf6_sock, &rmsghdr, 0);
262 if (retval < 0)
263 zlog_warn("recvmsg failed: %s", safe_strerror(errno));
264 else if (retval == iov_totallen(message))
265 zlog_warn("recvmsg read full buffer size: %d", retval);
266
267 /* source address */
268 assert(src);
269 memcpy(src, &src_sin6.sin6_addr, sizeof(struct in6_addr));
270
271 /* destination address */
272 if (ifindex)
273 *ifindex = pktinfo->ipi6_ifindex;
274 if (dst)
275 memcpy(dst, &pktinfo->ipi6_addr, sizeof(struct in6_addr));
276
277 return retval;
278 }