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