2 * Copyright (C) 2003 Yasuhiro Ohara
4 * This file is part of GNU Zebra.
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
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.
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
25 #include "sockunion.h"
28 #include "lib_errors.h"
32 #include "ospf6_proto.h"
33 #include "ospf6_top.h"
34 #include "ospf6_network.h"
37 struct in6_addr allspfrouters6
;
38 struct in6_addr alldrouters6
;
40 /* setsockopt MulticastLoop to off */
41 static void ospf6_reset_mcastloop(int ospf6_sock
)
44 if (setsockopt(ospf6_sock
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
, &off
,
47 zlog_warn("Network: reset IPV6_MULTICAST_LOOP failed: %s",
48 safe_strerror(errno
));
51 static void ospf6_set_pktinfo(int ospf6_sock
)
53 setsockopt_ipv6_pktinfo(ospf6_sock
, 1);
56 static void ospf6_set_transport_class(int ospf6_sock
)
58 #ifdef IPTOS_PREC_INTERNETCONTROL
59 setsockopt_ipv6_tclass(ospf6_sock
, IPTOS_PREC_INTERNETCONTROL
);
63 static void ospf6_set_checksum(int ospf6_sock
)
66 #ifndef DISABLE_IPV6_CHECKSUM
67 if (setsockopt(ospf6_sock
, IPPROTO_IPV6
, IPV6_CHECKSUM
, &offset
,
70 zlog_warn("Network: set IPV6_CHECKSUM failed: %s",
71 safe_strerror(errno
));
73 zlog_warn("Network: Don't set IPV6_CHECKSUM");
74 #endif /* DISABLE_IPV6_CHECKSUM */
77 void ospf6_serv_close(int *ospf6_sock
)
79 if (*ospf6_sock
!= -1) {
86 /* Make ospf6d's server socket. */
87 int ospf6_serv_sock(struct ospf6
*ospf6
)
94 if (ospf6
->vrf_id
== VRF_UNKNOWN
)
97 frr_with_privs(&ospf6d_privs
) {
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.");
107 /* set socket options */
109 sockopt_reuseaddr(ospf6_sock
);
111 ospf6_set_reuseaddr();
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
);
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
);
126 /* ospf6 set socket option */
127 int ospf6_sso(ifindex_t ifindex
, struct in6_addr
*group
, int option
, int sockfd
)
129 struct ipv6_mreq mreq6
;
131 int bufsize
= (8 * 1024 * 1024);
137 mreq6
.ipv6mr_interface
= ifindex
;
138 memcpy(&mreq6
.ipv6mr_multiaddr
, group
, sizeof(struct in6_addr
));
140 ret
= setsockopt(sockfd
, IPPROTO_IPV6
, option
, &mreq6
, sizeof(mreq6
));
144 "Network: setsockopt (%d) on ifindex %d failed: %s",
145 option
, ifindex
, safe_strerror(errno
));
149 setsockopt_so_sendbuf(sockfd
, bufsize
);
150 setsockopt_so_recvbuf(sockfd
, bufsize
);
155 static int iov_count(struct iovec
*iov
)
158 for (i
= 0; iov
[i
].iov_base
; i
++)
163 static int iov_totallen(struct iovec
*iov
)
167 for (i
= 0; iov
[i
].iov_base
; i
++)
168 totallen
+= iov
[i
].iov_len
;
172 int ospf6_sendmsg(struct in6_addr
*src
, struct in6_addr
*dst
,
173 ifindex_t ifindex
, struct iovec
*message
, int ospf6_sock
)
176 struct msghdr smsghdr
;
177 struct cmsghdr
*scmsgp
;
180 uint8_t buf
[CMSG_SPACE(sizeof(struct in6_pktinfo
))];
182 struct in6_pktinfo
*pktinfo
;
183 struct sockaddr_in6 dst_sin6
;
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
));
193 pktinfo
->ipi6_ifindex
= ifindex
;
195 memcpy(&pktinfo
->ipi6_addr
, src
, sizeof(struct in6_addr
));
197 memset(&pktinfo
->ipi6_addr
, 0, sizeof(struct in6_addr
));
199 /* destination address */
200 dst_sin6
.sin6_family
= AF_INET6
;
202 dst_sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
204 memcpy(&dst_sin6
.sin6_addr
, dst
, sizeof(struct in6_addr
));
205 dst_sin6
.sin6_scope_id
= ifindex
;
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); */
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
);
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)",
226 safe_strerror(errno
), errno
);
231 int ospf6_recvmsg(struct in6_addr
*src
, struct in6_addr
*dst
,
232 ifindex_t
*ifindex
, struct iovec
*message
, int ospf6_sock
)
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
;
241 rcmsgp
= (struct cmsghdr
*)cmsgbuf
;
242 pktinfo
= (struct in6_pktinfo
*)(CMSG_DATA(rcmsgp
));
243 memset(&src_sin6
, 0, sizeof(struct sockaddr_in6
));
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); */
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
);
260 retval
= recvmsg(ospf6_sock
, &rmsghdr
, 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
);
268 memcpy(src
, &src_sin6
.sin6_addr
, sizeof(struct in6_addr
));
270 /* destination address */
272 *ifindex
= pktinfo
->ipi6_ifindex
;
274 memcpy(dst
, &pktinfo
->ipi6_addr
, sizeof(struct in6_addr
));