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"
31 #include "ospf6_proto.h"
32 #include "ospf6_network.h"
36 struct in6_addr allspfrouters6
;
37 struct in6_addr alldrouters6
;
39 /* setsockopt MulticastLoop to off */
40 static void ospf6_reset_mcastloop(void)
43 if (setsockopt(ospf6_sock
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
, &off
,
46 zlog_warn("Network: reset IPV6_MULTICAST_LOOP failed: %s",
47 safe_strerror(errno
));
50 static void ospf6_set_pktinfo(void)
52 setsockopt_ipv6_pktinfo(ospf6_sock
, 1);
55 static void ospf6_set_transport_class(void)
57 #ifdef IPTOS_PREC_INTERNETCONTROL
58 setsockopt_ipv6_tclass(ospf6_sock
, IPTOS_PREC_INTERNETCONTROL
);
62 static void ospf6_set_checksum(void)
65 #ifndef DISABLE_IPV6_CHECKSUM
66 if (setsockopt(ospf6_sock
, IPPROTO_IPV6
, IPV6_CHECKSUM
, &offset
,
69 zlog_warn("Network: set IPV6_CHECKSUM failed: %s",
70 safe_strerror(errno
));
72 zlog_warn("Network: Don't set IPV6_CHECKSUM");
73 #endif /* DISABLE_IPV6_CHECKSUM */
76 /* Make ospf6d's server socket. */
77 int ospf6_serv_sock(void)
79 if (ospf6d_privs
.change(ZPRIVS_RAISE
))
80 zlog_ferr(LIB_ERR_PRIVILEGES
,
81 "ospf6_serv_sock: could not raise privs");
83 ospf6_sock
= socket(AF_INET6
, SOCK_RAW
, IPPROTO_OSPFIGP
);
85 zlog_warn("Network: can't create OSPF6 socket.");
86 if (ospf6d_privs
.change(ZPRIVS_LOWER
))
87 zlog_ferr(LIB_ERR_PRIVILEGES
,
88 "ospf_sock_init: could not lower privs");
91 if (ospf6d_privs
.change(ZPRIVS_LOWER
))
92 zlog_ferr(LIB_ERR_PRIVILEGES
,
93 "ospf_sock_init: could not lower privs");
95 /* set socket options */
97 sockopt_reuseaddr(ospf6_sock
);
99 ospf6_set_reuseaddr();
101 ospf6_reset_mcastloop();
103 ospf6_set_transport_class();
104 ospf6_set_checksum();
106 /* setup global in6_addr, allspf6 and alldr6 for later use */
107 inet_pton(AF_INET6
, ALLSPFROUTERS6
, &allspfrouters6
);
108 inet_pton(AF_INET6
, ALLDROUTERS6
, &alldrouters6
);
113 /* ospf6 set socket option */
114 int ospf6_sso(ifindex_t ifindex
, struct in6_addr
*group
, int option
)
116 struct ipv6_mreq mreq6
;
118 int bufsize
= (8 * 1024 * 1024);
121 mreq6
.ipv6mr_interface
= ifindex
;
122 memcpy(&mreq6
.ipv6mr_multiaddr
, group
, sizeof(struct in6_addr
));
124 ret
= setsockopt(ospf6_sock
, IPPROTO_IPV6
, option
, &mreq6
,
127 zlog_ferr(LIB_ERR_SOCKET
,
128 "Network: setsockopt (%d) on ifindex %d failed: %s",
129 option
, ifindex
, safe_strerror(errno
));
133 setsockopt_so_sendbuf(ospf6_sock
, bufsize
);
134 setsockopt_so_recvbuf(ospf6_sock
, bufsize
);
139 static int iov_count(struct iovec
*iov
)
142 for (i
= 0; iov
[i
].iov_base
; i
++)
147 static int iov_totallen(struct iovec
*iov
)
151 for (i
= 0; iov
[i
].iov_base
; i
++)
152 totallen
+= iov
[i
].iov_len
;
156 int ospf6_sendmsg(struct in6_addr
*src
, struct in6_addr
*dst
,
157 ifindex_t
*ifindex
, struct iovec
*message
)
160 struct msghdr smsghdr
;
161 struct cmsghdr
*scmsgp
;
164 uint8_t buf
[CMSG_SPACE(sizeof(struct in6_pktinfo
))];
166 struct in6_pktinfo
*pktinfo
;
167 struct sockaddr_in6 dst_sin6
;
172 scmsgp
= (struct cmsghdr
*)&cmsgbuf
;
173 pktinfo
= (struct in6_pktinfo
*)(CMSG_DATA(scmsgp
));
174 memset(&dst_sin6
, 0, sizeof(struct sockaddr_in6
));
177 pktinfo
->ipi6_ifindex
= *ifindex
;
179 memcpy(&pktinfo
->ipi6_addr
, src
, sizeof(struct in6_addr
));
181 memset(&pktinfo
->ipi6_addr
, 0, sizeof(struct in6_addr
));
183 /* destination address */
184 dst_sin6
.sin6_family
= AF_INET6
;
186 dst_sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
188 memcpy(&dst_sin6
.sin6_addr
, dst
, sizeof(struct in6_addr
));
189 dst_sin6
.sin6_scope_id
= *ifindex
;
191 /* send control msg */
192 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
193 scmsgp
->cmsg_type
= IPV6_PKTINFO
;
194 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
195 /* scmsgp = CMSG_NXTHDR (&smsghdr, scmsgp); */
198 memset(&smsghdr
, 0, sizeof(smsghdr
));
199 smsghdr
.msg_iov
= message
;
200 smsghdr
.msg_iovlen
= iov_count(message
);
201 smsghdr
.msg_name
= (caddr_t
)&dst_sin6
;
202 smsghdr
.msg_namelen
= sizeof(struct sockaddr_in6
);
203 smsghdr
.msg_control
= (caddr_t
)&cmsgbuf
.buf
;
204 smsghdr
.msg_controllen
= sizeof(cmsgbuf
.buf
);
206 retval
= sendmsg(ospf6_sock
, &smsghdr
, 0);
207 if (retval
!= iov_totallen(message
))
208 zlog_warn("sendmsg failed: ifindex: %d: %s (%d)", *ifindex
,
209 safe_strerror(errno
), errno
);
214 int ospf6_recvmsg(struct in6_addr
*src
, struct in6_addr
*dst
,
215 ifindex_t
*ifindex
, struct iovec
*message
)
218 struct msghdr rmsghdr
;
219 struct cmsghdr
*rcmsgp
;
220 uint8_t cmsgbuf
[CMSG_SPACE(sizeof(struct in6_pktinfo
))];
221 struct in6_pktinfo
*pktinfo
;
222 struct sockaddr_in6 src_sin6
;
224 rcmsgp
= (struct cmsghdr
*)cmsgbuf
;
225 pktinfo
= (struct in6_pktinfo
*)(CMSG_DATA(rcmsgp
));
226 memset(&src_sin6
, 0, sizeof(struct sockaddr_in6
));
228 /* receive control msg */
229 rcmsgp
->cmsg_level
= IPPROTO_IPV6
;
230 rcmsgp
->cmsg_type
= IPV6_PKTINFO
;
231 rcmsgp
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
232 /* rcmsgp = CMSG_NXTHDR (&rmsghdr, rcmsgp); */
234 /* receive msg hdr */
235 memset(&rmsghdr
, 0, sizeof(rmsghdr
));
236 rmsghdr
.msg_iov
= message
;
237 rmsghdr
.msg_iovlen
= iov_count(message
);
238 rmsghdr
.msg_name
= (caddr_t
)&src_sin6
;
239 rmsghdr
.msg_namelen
= sizeof(struct sockaddr_in6
);
240 rmsghdr
.msg_control
= (caddr_t
)cmsgbuf
;
241 rmsghdr
.msg_controllen
= sizeof(cmsgbuf
);
243 retval
= recvmsg(ospf6_sock
, &rmsghdr
, 0);
245 zlog_warn("recvmsg failed: %s", safe_strerror(errno
));
246 else if (retval
== iov_totallen(message
))
247 zlog_warn("recvmsg read full buffer size: %d", retval
);
251 memcpy(src
, &src_sin6
.sin6_addr
, sizeof(struct in6_addr
));
253 /* destination address */
255 *ifindex
= pktinfo
->ipi6_ifindex
;
257 memcpy(dst
, &pktinfo
->ipi6_addr
, sizeof(struct in6_addr
));