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
)
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(ospf6
->fd
, IPPROTO_IPV6
, option
, &mreq6
,
145 "Network: setsockopt (%d) on ifindex %d failed: %s",
146 option
, ifindex
, safe_strerror(errno
));
150 setsockopt_so_sendbuf(ospf6
->fd
, bufsize
);
151 setsockopt_so_recvbuf(ospf6
->fd
, bufsize
);
156 static int iov_count(struct iovec
*iov
)
159 for (i
= 0; iov
[i
].iov_base
; i
++)
164 static int iov_totallen(struct iovec
*iov
)
168 for (i
= 0; iov
[i
].iov_base
; i
++)
169 totallen
+= iov
[i
].iov_len
;
173 int ospf6_sendmsg(struct in6_addr
*src
, struct in6_addr
*dst
,
174 ifindex_t ifindex
, struct iovec
*message
, int ospf6_sock
)
177 struct msghdr smsghdr
;
178 struct cmsghdr
*scmsgp
;
181 uint8_t buf
[CMSG_SPACE(sizeof(struct in6_pktinfo
))];
183 struct in6_pktinfo
*pktinfo
;
184 struct sockaddr_in6 dst_sin6
;
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
));
194 pktinfo
->ipi6_ifindex
= ifindex
;
196 memcpy(&pktinfo
->ipi6_addr
, src
, sizeof(struct in6_addr
));
198 memset(&pktinfo
->ipi6_addr
, 0, sizeof(struct in6_addr
));
200 /* destination address */
201 dst_sin6
.sin6_family
= AF_INET6
;
203 dst_sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
205 memcpy(&dst_sin6
.sin6_addr
, dst
, sizeof(struct in6_addr
));
206 dst_sin6
.sin6_scope_id
= ifindex
;
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); */
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
);
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)",
227 safe_strerror(errno
), errno
);
232 int ospf6_recvmsg(struct in6_addr
*src
, struct in6_addr
*dst
,
233 ifindex_t
*ifindex
, struct iovec
*message
, int ospf6_sock
)
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
;
242 rcmsgp
= (struct cmsghdr
*)cmsgbuf
;
243 pktinfo
= (struct in6_pktinfo
*)(CMSG_DATA(rcmsgp
));
244 memset(&src_sin6
, 0, sizeof(struct sockaddr_in6
));
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); */
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
);
261 retval
= recvmsg(ospf6_sock
, &rmsghdr
, 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
);
269 memcpy(src
, &src_sin6
.sin6_addr
, sizeof(struct in6_addr
));
271 /* destination address */
273 *ifindex
= pktinfo
->ipi6_ifindex
;
275 memcpy(dst
, &pktinfo
->ipi6_addr
, sizeof(struct in6_addr
));