]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* |
508e53e2 | 2 | * Copyright (C) 2003 Yasuhiro Ohara |
718e3744 | 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 | * | |
896014f4 DL |
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 | |
718e3744 | 19 | */ |
20 | ||
21 | #include <zebra.h> | |
508e53e2 | 22 | |
718e3744 | 23 | #include "log.h" |
508e53e2 | 24 | #include "memory.h" |
718e3744 | 25 | #include "sockunion.h" |
03d52f8d | 26 | #include "sockopt.h" |
edd7c245 | 27 | #include "privs.h" |
4ba03be5 | 28 | #include "lib_errors.h" |
7df1f362 | 29 | #include "vrf.h" |
718e3744 | 30 | |
8551e6da | 31 | #include "libospf.h" |
718e3744 | 32 | #include "ospf6_proto.h" |
c527acf1 | 33 | #include "ospf6_top.h" |
508e53e2 | 34 | #include "ospf6_network.h" |
101dc9bc | 35 | #include "ospf6d.h" |
6cb85350 | 36 | #include "ospf6_message.h" |
718e3744 | 37 | |
508e53e2 | 38 | struct in6_addr allspfrouters6; |
39 | struct in6_addr alldrouters6; | |
718e3744 | 40 | |
508e53e2 | 41 | /* setsockopt MulticastLoop to off */ |
7df1f362 | 42 | static void ospf6_reset_mcastloop(int ospf6_sock) |
718e3744 | 43 | { |
d7c0a89a | 44 | unsigned int off = 0; |
d62a17ae | 45 | if (setsockopt(ospf6_sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &off, |
d7c0a89a | 46 | sizeof(unsigned int)) |
d62a17ae | 47 | < 0) |
48 | zlog_warn("Network: reset IPV6_MULTICAST_LOOP failed: %s", | |
49 | safe_strerror(errno)); | |
718e3744 | 50 | } |
51 | ||
7df1f362 | 52 | static void ospf6_set_pktinfo(int ospf6_sock) |
718e3744 | 53 | { |
d62a17ae | 54 | setsockopt_ipv6_pktinfo(ospf6_sock, 1); |
718e3744 | 55 | } |
56 | ||
7df1f362 | 57 | static void ospf6_set_transport_class(int ospf6_sock) |
6d0732c8 SH |
58 | { |
59 | #ifdef IPTOS_PREC_INTERNETCONTROL | |
d62a17ae | 60 | setsockopt_ipv6_tclass(ospf6_sock, IPTOS_PREC_INTERNETCONTROL); |
6d0732c8 SH |
61 | #endif |
62 | } | |
63 | ||
7df1f362 | 64 | void ospf6_serv_close(int *ospf6_sock) |
d51884e6 | 65 | { |
7df1f362 K |
66 | if (*ospf6_sock != -1) { |
67 | close(*ospf6_sock); | |
68 | *ospf6_sock = -1; | |
d51884e6 PG |
69 | return; |
70 | } | |
71 | } | |
72 | ||
718e3744 | 73 | /* Make ospf6d's server socket. */ |
7df1f362 | 74 | int ospf6_serv_sock(struct ospf6 *ospf6) |
718e3744 | 75 | { |
7df1f362 K |
76 | int ospf6_sock; |
77 | ||
78 | if (ospf6->fd != -1) | |
79 | return -1; | |
80 | ||
81 | if (ospf6->vrf_id == VRF_UNKNOWN) | |
82 | return -1; | |
83 | ||
0cf6db21 | 84 | frr_with_privs(&ospf6d_privs) { |
d62a17ae | 85 | |
7df1f362 K |
86 | ospf6_sock = vrf_socket(AF_INET6, SOCK_RAW, IPPROTO_OSPFIGP, |
87 | ospf6->vrf_id, ospf6->name); | |
01b9e3fd DL |
88 | if (ospf6_sock < 0) { |
89 | zlog_warn("Network: can't create OSPF6 socket."); | |
90 | return -1; | |
91 | } | |
d62a17ae | 92 | } |
d62a17ae | 93 | |
94 | /* set socket options */ | |
508e53e2 | 95 | #if 1 |
d62a17ae | 96 | sockopt_reuseaddr(ospf6_sock); |
508e53e2 | 97 | #else |
d62a17ae | 98 | ospf6_set_reuseaddr(); |
508e53e2 | 99 | #endif /*1*/ |
7df1f362 K |
100 | ospf6_reset_mcastloop(ospf6_sock); |
101 | ospf6_set_pktinfo(ospf6_sock); | |
102 | ospf6_set_transport_class(ospf6_sock); | |
508e53e2 | 103 | |
7df1f362 | 104 | ospf6->fd = ospf6_sock; |
d62a17ae | 105 | /* setup global in6_addr, allspf6 and alldr6 for later use */ |
106 | inet_pton(AF_INET6, ALLSPFROUTERS6, &allspfrouters6); | |
107 | inet_pton(AF_INET6, ALLDROUTERS6, &alldrouters6); | |
718e3744 | 108 | |
d62a17ae | 109 | return 0; |
718e3744 | 110 | } |
111 | ||
9a9446ea | 112 | /* ospf6 set socket option */ |
beadc736 | 113 | int ospf6_sso(ifindex_t ifindex, struct in6_addr *group, int option, int sockfd) |
718e3744 | 114 | { |
d62a17ae | 115 | struct ipv6_mreq mreq6; |
116 | int ret; | |
117 | int bufsize = (8 * 1024 * 1024); | |
118 | ||
beadc736 | 119 | if (sockfd == -1) |
7df1f362 K |
120 | return -1; |
121 | ||
d62a17ae | 122 | assert(ifindex); |
123 | mreq6.ipv6mr_interface = ifindex; | |
124 | memcpy(&mreq6.ipv6mr_multiaddr, group, sizeof(struct in6_addr)); | |
125 | ||
beadc736 | 126 | ret = setsockopt(sockfd, IPPROTO_IPV6, option, &mreq6, sizeof(mreq6)); |
d62a17ae | 127 | if (ret < 0) { |
09c866e3 | 128 | flog_err_sys( |
450971aa | 129 | EC_LIB_SOCKET, |
09c866e3 QY |
130 | "Network: setsockopt (%d) on ifindex %d failed: %s", |
131 | option, ifindex, safe_strerror(errno)); | |
d62a17ae | 132 | return ret; |
133 | } | |
134 | ||
beadc736 | 135 | setsockopt_so_sendbuf(sockfd, bufsize); |
136 | setsockopt_so_recvbuf(sockfd, bufsize); | |
d62a17ae | 137 | |
138 | return 0; | |
718e3744 | 139 | } |
140 | ||
d62a17ae | 141 | static int iov_count(struct iovec *iov) |
718e3744 | 142 | { |
d62a17ae | 143 | int i; |
144 | for (i = 0; iov[i].iov_base; i++) | |
145 | ; | |
146 | return i; | |
718e3744 | 147 | } |
148 | ||
d62a17ae | 149 | static int iov_totallen(struct iovec *iov) |
718e3744 | 150 | { |
d62a17ae | 151 | int i; |
152 | int totallen = 0; | |
153 | for (i = 0; iov[i].iov_base; i++) | |
154 | totallen += iov[i].iov_len; | |
155 | return totallen; | |
718e3744 | 156 | } |
157 | ||
d62a17ae | 158 | int ospf6_sendmsg(struct in6_addr *src, struct in6_addr *dst, |
b66ec22c | 159 | ifindex_t ifindex, struct iovec *message, int ospf6_sock) |
718e3744 | 160 | { |
d62a17ae | 161 | int retval; |
162 | struct msghdr smsghdr; | |
163 | struct cmsghdr *scmsgp; | |
164 | union { | |
165 | struct cmsghdr hdr; | |
d7c0a89a | 166 | uint8_t buf[CMSG_SPACE(sizeof(struct in6_pktinfo))]; |
d62a17ae | 167 | } cmsgbuf; |
168 | struct in6_pktinfo *pktinfo; | |
169 | struct sockaddr_in6 dst_sin6; | |
170 | ||
171 | assert(dst); | |
d62a17ae | 172 | |
c8b9f5fb | 173 | memset(&cmsgbuf, 0, sizeof(cmsgbuf)); |
d62a17ae | 174 | scmsgp = (struct cmsghdr *)&cmsgbuf; |
175 | pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp)); | |
6006b807 | 176 | memset(&dst_sin6, 0, sizeof(dst_sin6)); |
d62a17ae | 177 | |
178 | /* source address */ | |
b66ec22c | 179 | pktinfo->ipi6_ifindex = ifindex; |
d62a17ae | 180 | if (src) |
181 | memcpy(&pktinfo->ipi6_addr, src, sizeof(struct in6_addr)); | |
182 | else | |
183 | memset(&pktinfo->ipi6_addr, 0, sizeof(struct in6_addr)); | |
184 | ||
185 | /* destination address */ | |
186 | dst_sin6.sin6_family = AF_INET6; | |
718e3744 | 187 | #ifdef SIN6_LEN |
d62a17ae | 188 | dst_sin6.sin6_len = sizeof(struct sockaddr_in6); |
718e3744 | 189 | #endif /*SIN6_LEN*/ |
d62a17ae | 190 | memcpy(&dst_sin6.sin6_addr, dst, sizeof(struct in6_addr)); |
b66ec22c | 191 | dst_sin6.sin6_scope_id = ifindex; |
d62a17ae | 192 | |
193 | /* send control msg */ | |
194 | scmsgp->cmsg_level = IPPROTO_IPV6; | |
195 | scmsgp->cmsg_type = IPV6_PKTINFO; | |
196 | scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); | |
197 | /* scmsgp = CMSG_NXTHDR (&smsghdr, scmsgp); */ | |
198 | ||
199 | /* send msg hdr */ | |
200 | memset(&smsghdr, 0, sizeof(smsghdr)); | |
201 | smsghdr.msg_iov = message; | |
202 | smsghdr.msg_iovlen = iov_count(message); | |
203 | smsghdr.msg_name = (caddr_t)&dst_sin6; | |
204 | smsghdr.msg_namelen = sizeof(struct sockaddr_in6); | |
205 | smsghdr.msg_control = (caddr_t)&cmsgbuf.buf; | |
206 | smsghdr.msg_controllen = sizeof(cmsgbuf.buf); | |
207 | ||
208 | retval = sendmsg(ospf6_sock, &smsghdr, 0); | |
209 | if (retval != iov_totallen(message)) | |
13083032 DS |
210 | zlog_warn("sendmsg failed: source: %pI6 Dest: %pI6 ifindex: %d: %s (%d)", |
211 | src, dst, ifindex, | |
d62a17ae | 212 | safe_strerror(errno), errno); |
213 | ||
214 | return retval; | |
718e3744 | 215 | } |
216 | ||
d62a17ae | 217 | int ospf6_recvmsg(struct in6_addr *src, struct in6_addr *dst, |
7df1f362 | 218 | ifindex_t *ifindex, struct iovec *message, int ospf6_sock) |
718e3744 | 219 | { |
d62a17ae | 220 | int retval; |
221 | struct msghdr rmsghdr; | |
222 | struct cmsghdr *rcmsgp; | |
d7c0a89a | 223 | uint8_t cmsgbuf[CMSG_SPACE(sizeof(struct in6_pktinfo))]; |
d62a17ae | 224 | struct in6_pktinfo *pktinfo; |
225 | struct sockaddr_in6 src_sin6; | |
226 | ||
227 | rcmsgp = (struct cmsghdr *)cmsgbuf; | |
228 | pktinfo = (struct in6_pktinfo *)(CMSG_DATA(rcmsgp)); | |
6006b807 | 229 | memset(&src_sin6, 0, sizeof(src_sin6)); |
d62a17ae | 230 | |
231 | /* receive control msg */ | |
232 | rcmsgp->cmsg_level = IPPROTO_IPV6; | |
233 | rcmsgp->cmsg_type = IPV6_PKTINFO; | |
234 | rcmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); | |
235 | /* rcmsgp = CMSG_NXTHDR (&rmsghdr, rcmsgp); */ | |
236 | ||
237 | /* receive msg hdr */ | |
238 | memset(&rmsghdr, 0, sizeof(rmsghdr)); | |
239 | rmsghdr.msg_iov = message; | |
240 | rmsghdr.msg_iovlen = iov_count(message); | |
241 | rmsghdr.msg_name = (caddr_t)&src_sin6; | |
242 | rmsghdr.msg_namelen = sizeof(struct sockaddr_in6); | |
243 | rmsghdr.msg_control = (caddr_t)cmsgbuf; | |
244 | rmsghdr.msg_controllen = sizeof(cmsgbuf); | |
245 | ||
aa6a96ba PR |
246 | retval = recvmsg(ospf6_sock, &rmsghdr, MSG_DONTWAIT); |
247 | if (retval < 0) { | |
248 | if (errno != EAGAIN && errno != EWOULDBLOCK) | |
249 | zlog_warn("stream_recvmsg failed: %s", | |
250 | safe_strerror(errno)); | |
251 | return retval; | |
252 | } else if (retval == iov_totallen(message)) | |
d62a17ae | 253 | zlog_warn("recvmsg read full buffer size: %d", retval); |
254 | ||
255 | /* source address */ | |
256 | assert(src); | |
257 | memcpy(src, &src_sin6.sin6_addr, sizeof(struct in6_addr)); | |
258 | ||
259 | /* destination address */ | |
260 | if (ifindex) | |
261 | *ifindex = pktinfo->ipi6_ifindex; | |
262 | if (dst) | |
263 | memcpy(dst, &pktinfo->ipi6_addr, sizeof(struct in6_addr)); | |
264 | ||
265 | return retval; | |
718e3744 | 266 | } |