1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2003 Yasuhiro Ohara
10 #include "sockunion.h"
13 #include "lib_errors.h"
17 #include "ospf6_proto.h"
18 #include "ospf6_top.h"
19 #include "ospf6_network.h"
21 #include "ospf6_message.h"
23 struct in6_addr allspfrouters6
;
24 struct in6_addr alldrouters6
;
26 /* setsockopt MulticastLoop to off */
27 static void ospf6_reset_mcastloop(int ospf6_sock
)
30 if (setsockopt(ospf6_sock
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
, &off
,
33 zlog_warn("Network: reset IPV6_MULTICAST_LOOP failed: %s",
34 safe_strerror(errno
));
37 static void ospf6_set_pktinfo(int ospf6_sock
)
39 setsockopt_ipv6_pktinfo(ospf6_sock
, 1);
42 static void ospf6_set_transport_class(int ospf6_sock
)
44 #ifdef IPTOS_PREC_INTERNETCONTROL
45 setsockopt_ipv6_tclass(ospf6_sock
, IPTOS_PREC_INTERNETCONTROL
);
49 void ospf6_serv_close(int *ospf6_sock
)
51 if (*ospf6_sock
!= -1) {
58 /* Make ospf6d's server socket. */
59 int ospf6_serv_sock(struct ospf6
*ospf6
)
66 if (ospf6
->vrf_id
== VRF_UNKNOWN
)
69 frr_with_privs(&ospf6d_privs
) {
71 ospf6_sock
= vrf_socket(AF_INET6
, SOCK_RAW
, IPPROTO_OSPFIGP
,
72 ospf6
->vrf_id
, ospf6
->name
);
74 zlog_warn("Network: can't create OSPF6 socket.");
79 /* set socket options */
81 sockopt_reuseaddr(ospf6_sock
);
83 ospf6_set_reuseaddr();
85 ospf6_reset_mcastloop(ospf6_sock
);
86 ospf6_set_pktinfo(ospf6_sock
);
87 ospf6_set_transport_class(ospf6_sock
);
89 ospf6
->fd
= ospf6_sock
;
90 /* setup global in6_addr, allspf6 and alldr6 for later use */
91 inet_pton(AF_INET6
, ALLSPFROUTERS6
, &allspfrouters6
);
92 inet_pton(AF_INET6
, ALLDROUTERS6
, &alldrouters6
);
97 /* ospf6 set socket option */
98 int ospf6_sso(ifindex_t ifindex
, struct in6_addr
*group
, int option
, int sockfd
)
100 struct ipv6_mreq mreq6
;
102 int bufsize
= (8 * 1024 * 1024);
108 mreq6
.ipv6mr_interface
= ifindex
;
109 memcpy(&mreq6
.ipv6mr_multiaddr
, group
, sizeof(struct in6_addr
));
111 ret
= setsockopt(sockfd
, IPPROTO_IPV6
, option
, &mreq6
, sizeof(mreq6
));
115 "Network: setsockopt (%d) on ifindex %d failed: %s",
116 option
, ifindex
, safe_strerror(errno
));
120 setsockopt_so_sendbuf(sockfd
, bufsize
);
121 setsockopt_so_recvbuf(sockfd
, bufsize
);
126 static int iov_count(struct iovec
*iov
)
129 for (i
= 0; iov
[i
].iov_base
; i
++)
134 static int iov_totallen(struct iovec
*iov
)
138 for (i
= 0; iov
[i
].iov_base
; i
++)
139 totallen
+= iov
[i
].iov_len
;
143 int ospf6_sendmsg(struct in6_addr
*src
, struct in6_addr
*dst
,
144 ifindex_t ifindex
, struct iovec
*message
, int ospf6_sock
)
147 struct msghdr smsghdr
;
148 struct cmsghdr
*scmsgp
;
151 uint8_t buf
[CMSG_SPACE(sizeof(struct in6_pktinfo
))];
153 struct in6_pktinfo
*pktinfo
;
154 struct sockaddr_in6 dst_sin6
;
158 memset(&cmsgbuf
, 0, sizeof(cmsgbuf
));
159 scmsgp
= (struct cmsghdr
*)&cmsgbuf
;
160 pktinfo
= (struct in6_pktinfo
*)(CMSG_DATA(scmsgp
));
161 memset(&dst_sin6
, 0, sizeof(dst_sin6
));
164 pktinfo
->ipi6_ifindex
= ifindex
;
166 memcpy(&pktinfo
->ipi6_addr
, src
, sizeof(struct in6_addr
));
168 memset(&pktinfo
->ipi6_addr
, 0, sizeof(struct in6_addr
));
170 /* destination address */
171 dst_sin6
.sin6_family
= AF_INET6
;
173 dst_sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
175 memcpy(&dst_sin6
.sin6_addr
, dst
, sizeof(struct in6_addr
));
176 dst_sin6
.sin6_scope_id
= ifindex
;
178 /* send control msg */
179 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
180 scmsgp
->cmsg_type
= IPV6_PKTINFO
;
181 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
182 /* scmsgp = CMSG_NXTHDR (&smsghdr, scmsgp); */
185 memset(&smsghdr
, 0, sizeof(smsghdr
));
186 smsghdr
.msg_iov
= message
;
187 smsghdr
.msg_iovlen
= iov_count(message
);
188 smsghdr
.msg_name
= (caddr_t
)&dst_sin6
;
189 smsghdr
.msg_namelen
= sizeof(struct sockaddr_in6
);
190 smsghdr
.msg_control
= (caddr_t
)&cmsgbuf
.buf
;
191 smsghdr
.msg_controllen
= sizeof(cmsgbuf
.buf
);
193 retval
= sendmsg(ospf6_sock
, &smsghdr
, 0);
194 if (retval
!= iov_totallen(message
))
195 zlog_warn("sendmsg failed: source: %pI6 Dest: %pI6 ifindex: %d: %s (%d)",
197 safe_strerror(errno
), errno
);
202 int ospf6_recvmsg(struct in6_addr
*src
, struct in6_addr
*dst
,
203 ifindex_t
*ifindex
, struct iovec
*message
, int ospf6_sock
)
206 struct msghdr rmsghdr
;
207 struct cmsghdr
*rcmsgp
;
208 uint8_t cmsgbuf
[CMSG_SPACE(sizeof(struct in6_pktinfo
))];
209 struct in6_pktinfo
*pktinfo
;
210 struct sockaddr_in6 src_sin6
;
212 rcmsgp
= (struct cmsghdr
*)cmsgbuf
;
213 pktinfo
= (struct in6_pktinfo
*)(CMSG_DATA(rcmsgp
));
214 memset(&src_sin6
, 0, sizeof(src_sin6
));
216 /* receive control msg */
217 rcmsgp
->cmsg_level
= IPPROTO_IPV6
;
218 rcmsgp
->cmsg_type
= IPV6_PKTINFO
;
219 rcmsgp
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
220 /* rcmsgp = CMSG_NXTHDR (&rmsghdr, rcmsgp); */
222 /* receive msg hdr */
223 memset(&rmsghdr
, 0, sizeof(rmsghdr
));
224 rmsghdr
.msg_iov
= message
;
225 rmsghdr
.msg_iovlen
= iov_count(message
);
226 rmsghdr
.msg_name
= (caddr_t
)&src_sin6
;
227 rmsghdr
.msg_namelen
= sizeof(struct sockaddr_in6
);
228 rmsghdr
.msg_control
= (caddr_t
)cmsgbuf
;
229 rmsghdr
.msg_controllen
= sizeof(cmsgbuf
);
231 retval
= recvmsg(ospf6_sock
, &rmsghdr
, MSG_DONTWAIT
);
233 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
)
234 zlog_warn("stream_recvmsg failed: %s",
235 safe_strerror(errno
));
237 } else if (retval
== iov_totallen(message
))
238 zlog_warn("recvmsg read full buffer size: %d", retval
);
242 memcpy(src
, &src_sin6
.sin6_addr
, sizeof(struct in6_addr
));
244 /* destination address */
246 *ifindex
= pktinfo
->ipi6_ifindex
;
248 memcpy(dst
, &pktinfo
->ipi6_addr
, sizeof(struct in6_addr
));