]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* setsockopt functions |
2 | * Copyright (C) 1999 Kunihiro Ishiguro | |
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> | |
e6f8d095 | 22 | |
718e3744 | 23 | #include "log.h" |
e6822768 | 24 | #include "sockopt.h" |
0df7c91f | 25 | #include "sockunion.h" |
481bc15f | 26 | #include "lib_errors.h" |
718e3744 | 27 | |
1b636c0a DL |
28 | #if (defined(__FreeBSD__) \ |
29 | && ((__FreeBSD_version >= 500022 && __FreeBSD_version < 700000) \ | |
30 | || (__FreeBSD_version < 500000 && __FreeBSD_version >= 440000))) \ | |
31 | || (defined(__NetBSD__) && defined(__NetBSD_Version__) \ | |
32 | && __NetBSD_Version__ >= 106010000) \ | |
33 | || defined(__OpenBSD__) || defined(__APPLE__) \ | |
34 | || defined(__DragonFly__) || defined(__sun) | |
35 | #define HAVE_BSD_STRUCT_IP_MREQ_HACK | |
36 | #endif | |
37 | ||
d62a17ae | 38 | void setsockopt_so_recvbuf(int sock, int size) |
0b3acf4f | 39 | { |
d62a17ae | 40 | int orig_req = size; |
6228a3b8 | 41 | |
d62a17ae | 42 | while (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size)) |
43 | == -1) | |
44 | size /= 2; | |
0b3acf4f | 45 | |
d62a17ae | 46 | if (size != orig_req) |
450971aa | 47 | flog_err(EC_LIB_SOCKET, |
4496fbb3 DS |
48 | "%s: fd %d: SO_RCVBUF set to %d (requested %d)", |
49 | __func__, sock, size, orig_req); | |
0b3acf4f | 50 | } |
51 | ||
d62a17ae | 52 | void setsockopt_so_sendbuf(const int sock, int size) |
b7fe4141 | 53 | { |
d62a17ae | 54 | int orig_req = size; |
6228a3b8 | 55 | |
d62a17ae | 56 | while (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size)) |
57 | == -1) | |
58 | size /= 2; | |
b7fe4141 | 59 | |
d62a17ae | 60 | if (size != orig_req) |
450971aa | 61 | flog_err(EC_LIB_SOCKET, |
4496fbb3 DS |
62 | "%s: fd %d: SO_SNDBUF set to %d (requested %d)", |
63 | __func__, sock, size, orig_req); | |
b7fe4141 DO |
64 | } |
65 | ||
d62a17ae | 66 | int getsockopt_so_sendbuf(const int sock) |
b7fe4141 | 67 | { |
d7c0a89a | 68 | uint32_t optval; |
d62a17ae | 69 | socklen_t optlen = sizeof(optval); |
70 | int ret = getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&optval, | |
71 | &optlen); | |
72 | if (ret < 0) { | |
450971aa | 73 | flog_err_sys(EC_LIB_SYSTEM_CALL, |
09c866e3 QY |
74 | "fd %d: can't getsockopt SO_SNDBUF: %d (%s)", sock, |
75 | errno, safe_strerror(errno)); | |
d62a17ae | 76 | return ret; |
77 | } | |
78 | return optval; | |
b7fe4141 DO |
79 | } |
80 | ||
772aae8b DS |
81 | int getsockopt_so_recvbuf(const int sock) |
82 | { | |
83 | uint32_t optval; | |
84 | socklen_t optlen = sizeof(optval); | |
85 | int ret = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&optval, | |
86 | &optlen); | |
87 | if (ret < 0) { | |
88 | flog_err_sys(EC_LIB_SYSTEM_CALL, | |
89 | "fd %d: can't getsockopt SO_RCVBUF: %d (%s)", sock, | |
90 | errno, safe_strerror(errno)); | |
91 | return ret; | |
92 | } | |
93 | return optval; | |
94 | } | |
95 | ||
d62a17ae | 96 | static void *getsockopt_cmsg_data(struct msghdr *msgh, int level, int type) |
4f7baa0e | 97 | { |
d62a17ae | 98 | struct cmsghdr *cmsg; |
d62a17ae | 99 | |
adf0e7c6 | 100 | for (cmsg = CMSG_FIRSTHDR(msgh); cmsg != NULL; |
d62a17ae | 101 | cmsg = CMSG_NXTHDR(msgh, cmsg)) |
dc094865 | 102 | if (cmsg->cmsg_level == level && cmsg->cmsg_type == type) |
5037cc3e | 103 | return CMSG_DATA(cmsg); |
d62a17ae | 104 | |
105 | return NULL; | |
4f7baa0e | 106 | } |
107 | ||
718e3744 | 108 | /* Set IPv6 packet info to the socket. */ |
d62a17ae | 109 | int setsockopt_ipv6_pktinfo(int sock, int val) |
718e3744 | 110 | { |
d62a17ae | 111 | int ret; |
112 | ||
113 | #ifdef IPV6_RECVPKTINFO /*2292bis-01*/ | |
114 | ret = setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &val, | |
115 | sizeof(val)); | |
116 | if (ret < 0) | |
450971aa | 117 | flog_err(EC_LIB_SOCKET, |
4496fbb3 DS |
118 | "can't setsockopt IPV6_RECVPKTINFO : %s", |
119 | safe_strerror(errno)); | |
d62a17ae | 120 | #else /*RFC2292*/ |
121 | ret = setsockopt(sock, IPPROTO_IPV6, IPV6_PKTINFO, &val, sizeof(val)); | |
122 | if (ret < 0) | |
450971aa | 123 | flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_PKTINFO : %s", |
4496fbb3 | 124 | safe_strerror(errno)); |
03bad95a | 125 | #endif /* IANA_IPV6 */ |
d62a17ae | 126 | return ret; |
718e3744 | 127 | } |
128 | ||
718e3744 | 129 | /* Set multicast hops val to the socket. */ |
d62a17ae | 130 | int setsockopt_ipv6_multicast_hops(int sock, int val) |
718e3744 | 131 | { |
d62a17ae | 132 | int ret; |
718e3744 | 133 | |
d62a17ae | 134 | ret = setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, |
135 | sizeof(val)); | |
136 | if (ret < 0) | |
1c50c1c0 | 137 | flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_MULTICAST_HOPS"); |
d62a17ae | 138 | return ret; |
718e3744 | 139 | } |
140 | ||
141 | /* Set multicast hops val to the socket. */ | |
d62a17ae | 142 | int setsockopt_ipv6_unicast_hops(int sock, int val) |
718e3744 | 143 | { |
d62a17ae | 144 | int ret; |
718e3744 | 145 | |
d62a17ae | 146 | ret = setsockopt(sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &val, |
147 | sizeof(val)); | |
148 | if (ret < 0) | |
450971aa | 149 | flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_UNICAST_HOPS"); |
d62a17ae | 150 | return ret; |
718e3744 | 151 | } |
152 | ||
d62a17ae | 153 | int setsockopt_ipv6_hoplimit(int sock, int val) |
718e3744 | 154 | { |
d62a17ae | 155 | int ret; |
156 | ||
157 | #ifdef IPV6_RECVHOPLIMIT /*2292bis-01*/ | |
158 | ret = setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &val, | |
159 | sizeof(val)); | |
160 | if (ret < 0) | |
450971aa | 161 | flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_RECVHOPLIMIT"); |
d62a17ae | 162 | #else /*RFC2292*/ |
163 | ret = setsockopt(sock, IPPROTO_IPV6, IPV6_HOPLIMIT, &val, sizeof(val)); | |
164 | if (ret < 0) | |
450971aa | 165 | flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_HOPLIMIT"); |
718e3744 | 166 | #endif |
d62a17ae | 167 | return ret; |
718e3744 | 168 | } |
169 | ||
170 | /* Set multicast loop zero to the socket. */ | |
d62a17ae | 171 | int setsockopt_ipv6_multicast_loop(int sock, int val) |
718e3744 | 172 | { |
d62a17ae | 173 | int ret; |
174 | ||
175 | ret = setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val, | |
176 | sizeof(val)); | |
177 | if (ret < 0) | |
1c50c1c0 | 178 | flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_MULTICAST_LOOP"); |
d62a17ae | 179 | return ret; |
718e3744 | 180 | } |
181 | ||
d62a17ae | 182 | static int getsockopt_ipv6_ifindex(struct msghdr *msgh) |
4f7baa0e | 183 | { |
d62a17ae | 184 | struct in6_pktinfo *pktinfo; |
185 | ||
186 | pktinfo = getsockopt_cmsg_data(msgh, IPPROTO_IPV6, IPV6_PKTINFO); | |
187 | ||
188 | return pktinfo->ipi6_ifindex; | |
4f7baa0e | 189 | } |
718e3744 | 190 | |
d62a17ae | 191 | int setsockopt_ipv6_tclass(int sock, int tclass) |
6d0732c8 | 192 | { |
d62a17ae | 193 | int ret = 0; |
6d0732c8 | 194 | |
ad61af67 | 195 | #ifdef IPV6_TCLASS /* RFC3542 */ |
d62a17ae | 196 | ret = setsockopt(sock, IPPROTO_IPV6, IPV6_TCLASS, &tclass, |
197 | sizeof(tclass)); | |
198 | if (ret < 0) | |
450971aa | 199 | flog_err(EC_LIB_SOCKET, |
4496fbb3 DS |
200 | "Can't set IPV6_TCLASS option for fd %d to %#x: %s", |
201 | sock, tclass, safe_strerror(errno)); | |
ad61af67 | 202 | #endif |
d62a17ae | 203 | return ret; |
6d0732c8 | 204 | } |
718e3744 | 205 | |
cc49eb5a | 206 | /* |
207 | * Process multicast socket options for IPv4 in an OS-dependent manner. | |
69bf3a39 | 208 | * Supported options are IP_{ADD,DROP}_MEMBERSHIP. |
cc49eb5a | 209 | * |
210 | * Many operating systems have a limit on the number of groups that | |
211 | * can be joined per socket (where each group and local address | |
212 | * counts). This impacts OSPF, which joins groups on each interface | |
213 | * using a single socket. The limit is typically 20, derived from the | |
214 | * original BSD multicast implementation. Some systems have | |
215 | * mechanisms for increasing this limit. | |
c188c37c | 216 | * |
217 | * In many 4.4BSD-derived systems, multicast group operations are not | |
218 | * allowed on interfaces that are not UP. Thus, a previous attempt to | |
219 | * leave the group may have failed, leaving it still joined, and we | |
220 | * drop/join quietly to recover. This may not be necessary, but aims to | |
221 | * defend against unknown behavior in that we will still return an error | |
222 | * if the second join fails. It is not clear how other systems | |
223 | * (e.g. Linux, Solaris) behave when leaving groups on down interfaces, | |
224 | * but this behavior should not be harmful if they behave the same way, | |
225 | * allow leaves, or implicitly leave all groups joined to down interfaces. | |
cc49eb5a | 226 | */ |
d62a17ae | 227 | int setsockopt_ipv4_multicast(int sock, int optname, struct in_addr if_addr, |
228 | unsigned int mcast_addr, ifindex_t ifindex) | |
718e3744 | 229 | { |
10d04cdb | 230 | #ifdef HAVE_RFC3678 |
d62a17ae | 231 | struct group_req gr; |
232 | struct sockaddr_in *si; | |
233 | int ret; | |
234 | memset(&gr, 0, sizeof(gr)); | |
235 | si = (struct sockaddr_in *)&gr.gr_group; | |
236 | gr.gr_interface = ifindex; | |
237 | si->sin_family = AF_INET; | |
10d04cdb | 238 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
d62a17ae | 239 | si->sin_len = sizeof(struct sockaddr_in); |
10d04cdb | 240 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
d62a17ae | 241 | si->sin_addr.s_addr = mcast_addr; |
242 | ret = setsockopt(sock, IPPROTO_IP, | |
243 | (optname == IP_ADD_MEMBERSHIP) ? MCAST_JOIN_GROUP | |
244 | : MCAST_LEAVE_GROUP, | |
245 | (void *)&gr, sizeof(gr)); | |
246 | if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP) | |
247 | && (errno == EADDRINUSE)) { | |
248 | setsockopt(sock, IPPROTO_IP, MCAST_LEAVE_GROUP, (void *)&gr, | |
249 | sizeof(gr)); | |
250 | ret = setsockopt(sock, IPPROTO_IP, MCAST_JOIN_GROUP, | |
251 | (void *)&gr, sizeof(gr)); | |
252 | } | |
253 | return ret; | |
718e3744 | 254 | |
10d04cdb | 255 | #elif defined(HAVE_STRUCT_IP_MREQN_IMR_IFINDEX) && !defined(__FreeBSD__) |
d62a17ae | 256 | struct ip_mreqn mreqn; |
257 | int ret; | |
258 | ||
259 | assert(optname == IP_ADD_MEMBERSHIP || optname == IP_DROP_MEMBERSHIP); | |
260 | memset(&mreqn, 0, sizeof(mreqn)); | |
261 | ||
262 | mreqn.imr_multiaddr.s_addr = mcast_addr; | |
263 | mreqn.imr_ifindex = ifindex; | |
264 | ||
265 | ret = setsockopt(sock, IPPROTO_IP, optname, (void *)&mreqn, | |
266 | sizeof(mreqn)); | |
267 | if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP) | |
268 | && (errno == EADDRINUSE)) { | |
269 | /* see above: handle possible problem when interface comes back | |
270 | * up */ | |
271 | char buf[1][INET_ADDRSTRLEN]; | |
272 | zlog_info( | |
3efd0893 | 273 | "setsockopt_ipv4_multicast attempting to drop and re-add (fd %d, mcast %s, ifindex %u)", |
9d303b37 DL |
274 | sock, inet_ntop(AF_INET, &mreqn.imr_multiaddr, buf[0], |
275 | sizeof(buf[0])), | |
d62a17ae | 276 | ifindex); |
277 | setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (void *)&mreqn, | |
278 | sizeof(mreqn)); | |
279 | ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, | |
280 | (void *)&mreqn, sizeof(mreqn)); | |
281 | } | |
282 | return ret; | |
283 | ||
284 | /* Example defines for another OS, boilerplate off other code in this | |
285 | function, AND handle optname as per other sections for consistency !! */ | |
286 | /* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */ | |
287 | /* Add your favourite OS here! */ | |
288 | ||
289 | #elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK) /* #if OS_TYPE */ | |
290 | /* standard BSD API */ | |
291 | ||
292 | struct ip_mreq mreq; | |
293 | int ret; | |
294 | ||
295 | assert(optname == IP_ADD_MEMBERSHIP || optname == IP_DROP_MEMBERSHIP); | |
296 | ||
297 | ||
298 | memset(&mreq, 0, sizeof(mreq)); | |
299 | mreq.imr_multiaddr.s_addr = mcast_addr; | |
4a3867d0 | 300 | #if !defined __OpenBSD__ |
d62a17ae | 301 | mreq.imr_interface.s_addr = htonl(ifindex); |
4a3867d0 | 302 | #else |
d62a17ae | 303 | mreq.imr_interface.s_addr = if_addr.s_addr; |
4a3867d0 RW |
304 | #endif |
305 | ||
d62a17ae | 306 | ret = setsockopt(sock, IPPROTO_IP, optname, (void *)&mreq, |
307 | sizeof(mreq)); | |
308 | if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP) | |
309 | && (errno == EADDRINUSE)) { | |
310 | /* see above: handle possible problem when interface comes back | |
311 | * up */ | |
312 | char buf[1][INET_ADDRSTRLEN]; | |
313 | zlog_info( | |
3efd0893 | 314 | "setsockopt_ipv4_multicast attempting to drop and re-add (fd %d, mcast %s, ifindex %u)", |
9d303b37 DL |
315 | sock, inet_ntop(AF_INET, &mreq.imr_multiaddr, buf[0], |
316 | sizeof(buf[0])), | |
d62a17ae | 317 | ifindex); |
318 | setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (void *)&mreq, | |
319 | sizeof(mreq)); | |
320 | ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, | |
321 | (void *)&mreq, sizeof(mreq)); | |
322 | } | |
323 | return ret; | |
ee7e75d3 | 324 | |
69bf3a39 | 325 | #else |
d62a17ae | 326 | #error "Unsupported multicast API" |
718e3744 | 327 | #endif /* #if OS_TYPE */ |
718e3744 | 328 | } |
4f7baa0e | 329 | |
69bf3a39 DT |
330 | /* |
331 | * Set IP_MULTICAST_IF socket option in an OS-dependent manner. | |
332 | */ | |
d62a17ae | 333 | int setsockopt_ipv4_multicast_if(int sock, struct in_addr if_addr, |
334 | ifindex_t ifindex) | |
69bf3a39 DT |
335 | { |
336 | ||
337 | #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX | |
d62a17ae | 338 | struct ip_mreqn mreqn; |
339 | memset(&mreqn, 0, sizeof(mreqn)); | |
69bf3a39 | 340 | |
d62a17ae | 341 | mreqn.imr_ifindex = ifindex; |
342 | return setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, (void *)&mreqn, | |
343 | sizeof(mreqn)); | |
69bf3a39 | 344 | |
d62a17ae | 345 | /* Example defines for another OS, boilerplate off other code in this |
346 | function */ | |
347 | /* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */ | |
348 | /* Add your favourite OS here! */ | |
69bf3a39 | 349 | #elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK) |
d62a17ae | 350 | struct in_addr m; |
69bf3a39 | 351 | |
4a3867d0 | 352 | #if !defined __OpenBSD__ |
d62a17ae | 353 | m.s_addr = htonl(ifindex); |
4a3867d0 | 354 | #else |
d62a17ae | 355 | m.s_addr = if_addr.s_addr; |
4a3867d0 | 356 | #endif |
69bf3a39 | 357 | |
d62a17ae | 358 | return setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, (void *)&m, |
359 | sizeof(m)); | |
69bf3a39 | 360 | #else |
d62a17ae | 361 | #error "Unsupported multicast API" |
69bf3a39 DT |
362 | #endif |
363 | } | |
c5bdb09f | 364 | |
d7c0a89a | 365 | int setsockopt_ipv4_multicast_loop(int sock, uint8_t val) |
c5bdb09f | 366 | { |
d62a17ae | 367 | int ret; |
c5bdb09f | 368 | |
d62a17ae | 369 | ret = setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP, (void *)&val, |
370 | sizeof(val)); | |
371 | if (ret < 0) | |
450971aa | 372 | flog_err(EC_LIB_SOCKET, "can't setsockopt IP_MULTICAST_LOOP"); |
c5bdb09f | 373 | |
d62a17ae | 374 | return ret; |
c5bdb09f RW |
375 | } |
376 | ||
d62a17ae | 377 | static int setsockopt_ipv4_ifindex(int sock, ifindex_t val) |
4f7baa0e | 378 | { |
d62a17ae | 379 | int ret; |
380 | ||
381 | #if defined(IP_PKTINFO) | |
0e2d7076 DA |
382 | ret = setsockopt(sock, IPPROTO_IP, IP_PKTINFO, &val, sizeof(val)); |
383 | if (ret < 0) | |
450971aa | 384 | flog_err(EC_LIB_SOCKET, |
4496fbb3 DS |
385 | "Can't set IP_PKTINFO option for fd %d to %d: %s", |
386 | sock, val, safe_strerror(errno)); | |
d62a17ae | 387 | #elif defined(IP_RECVIF) |
0e2d7076 DA |
388 | ret = setsockopt(sock, IPPROTO_IP, IP_RECVIF, &val, sizeof(val)); |
389 | if (ret < 0) | |
450971aa | 390 | flog_err(EC_LIB_SOCKET, |
4496fbb3 DS |
391 | "Can't set IP_RECVIF option for fd %d to %d: %s", sock, |
392 | val, safe_strerror(errno)); | |
4f7baa0e | 393 | #else |
394 | #warning "Neither IP_PKTINFO nor IP_RECVIF is available." | |
395 | #warning "Will not be able to receive link info." | |
396 | #warning "Things might be seriously broken.." | |
d62a17ae | 397 | /* XXX Does this ever happen? Should there be a zlog_warn message here? |
398 | */ | |
399 | ret = -1; | |
4f7baa0e | 400 | #endif |
d62a17ae | 401 | return ret; |
4f7baa0e | 402 | } |
403 | ||
d62a17ae | 404 | int setsockopt_ipv4_tos(int sock, int tos) |
1423c809 | 405 | { |
d62a17ae | 406 | int ret; |
1423c809 | 407 | |
d62a17ae | 408 | ret = setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
409 | if (ret < 0) | |
450971aa | 410 | flog_err(EC_LIB_SOCKET, |
4496fbb3 DS |
411 | "Can't set IP_TOS option for fd %d to %#x: %s", sock, |
412 | tos, safe_strerror(errno)); | |
d62a17ae | 413 | return ret; |
1423c809 SH |
414 | } |
415 | ||
416 | ||
d62a17ae | 417 | int setsockopt_ifindex(int af, int sock, ifindex_t val) |
e6822768 | 418 | { |
d62a17ae | 419 | int ret = -1; |
420 | ||
421 | switch (af) { | |
422 | case AF_INET: | |
423 | ret = setsockopt_ipv4_ifindex(sock, val); | |
424 | break; | |
425 | case AF_INET6: | |
426 | ret = setsockopt_ipv6_pktinfo(sock, val); | |
427 | break; | |
428 | default: | |
450971aa | 429 | flog_err(EC_LIB_DEVELOPMENT, |
4496fbb3 | 430 | "setsockopt_ifindex: unknown address family %d", af); |
d62a17ae | 431 | } |
432 | return ret; | |
e6822768 | 433 | } |
d62a17ae | 434 | |
d44debed | 435 | /* |
436 | * Requires: msgh is not NULL and points to a valid struct msghdr, which | |
437 | * may or may not have control data about the incoming interface. | |
438 | * | |
439 | * Returns the interface index (small integer >= 1) if it can be | |
440 | * determined, or else 0. | |
441 | */ | |
d62a17ae | 442 | static ifindex_t getsockopt_ipv4_ifindex(struct msghdr *msgh) |
4f7baa0e | 443 | { |
a2b6e694 | 444 | ifindex_t ifindex; |
1d69fdf6 | 445 | |
e6822768 | 446 | #if defined(IP_PKTINFO) |
d62a17ae | 447 | /* Linux pktinfo based ifindex retrieval */ |
448 | struct in_pktinfo *pktinfo; | |
449 | ||
450 | pktinfo = (struct in_pktinfo *)getsockopt_cmsg_data(msgh, IPPROTO_IP, | |
451 | IP_PKTINFO); | |
a2b6e694 | 452 | |
453 | /* getsockopt_ifindex() will forward this, being 0 "not found" */ | |
454 | if (pktinfo == NULL) | |
455 | return 0; | |
456 | ||
d62a17ae | 457 | ifindex = pktinfo->ipi_ifindex; |
458 | ||
e6822768 | 459 | #elif defined(IP_RECVIF) |
d44debed | 460 | |
d62a17ae | 461 | /* retrieval based on IP_RECVIF */ |
d44debed | 462 | |
d62a17ae | 463 | /* BSD systems use a sockaddr_dl as the control message payload. */ |
464 | struct sockaddr_dl *sdl; | |
1d69fdf6 | 465 | |
d62a17ae | 466 | /* BSD */ |
467 | sdl = (struct sockaddr_dl *)getsockopt_cmsg_data(msgh, IPPROTO_IP, | |
468 | IP_RECVIF); | |
469 | if (sdl != NULL) | |
470 | ifindex = sdl->sdl_index; | |
471 | else | |
472 | ifindex = 0; | |
e6822768 | 473 | |
d44debed | 474 | #else |
d62a17ae | 475 | /* |
476 | * Neither IP_PKTINFO nor IP_RECVIF defined - warn at compile time. | |
477 | * XXX Decide if this is a core service, or if daemons have to cope. | |
478 | * Since Solaris 8 and OpenBSD seem not to provide it, it seems that | |
479 | * daemons have to cope. | |
480 | */ | |
d44debed | 481 | #warning "getsockopt_ipv4_ifindex: Neither IP_PKTINFO nor IP_RECVIF defined." |
482 | #warning "Some daemons may fail to operate correctly!" | |
d62a17ae | 483 | ifindex = 0; |
d44debed | 484 | |
d62a17ae | 485 | #endif /* IP_PKTINFO */ |
d44debed | 486 | |
d62a17ae | 487 | return ifindex; |
4f7baa0e | 488 | } |
489 | ||
490 | /* return ifindex, 0 if none found */ | |
d62a17ae | 491 | ifindex_t getsockopt_ifindex(int af, struct msghdr *msgh) |
4f7baa0e | 492 | { |
d62a17ae | 493 | switch (af) { |
494 | case AF_INET: | |
495 | return (getsockopt_ipv4_ifindex(msgh)); | |
d62a17ae | 496 | case AF_INET6: |
497 | return (getsockopt_ipv6_ifindex(msgh)); | |
d62a17ae | 498 | default: |
450971aa | 499 | flog_err(EC_LIB_DEVELOPMENT, |
4496fbb3 | 500 | "getsockopt_ifindex: unknown address family %d", af); |
d62a17ae | 501 | return 0; |
502 | } | |
4f7baa0e | 503 | } |
96e27c99 | 504 | |
505 | /* swab iph between order system uses for IP_HDRINCL and host order */ | |
d62a17ae | 506 | void sockopt_iphdrincl_swab_htosys(struct ip *iph) |
96e27c99 | 507 | { |
d62a17ae | 508 | /* BSD and derived take iph in network order, except for |
509 | * ip_len and ip_off | |
510 | */ | |
96e27c99 | 511 | #ifndef HAVE_IP_HDRINCL_BSD_ORDER |
d62a17ae | 512 | iph->ip_len = htons(iph->ip_len); |
513 | iph->ip_off = htons(iph->ip_off); | |
96e27c99 | 514 | #endif /* HAVE_IP_HDRINCL_BSD_ORDER */ |
515 | ||
d62a17ae | 516 | iph->ip_id = htons(iph->ip_id); |
96e27c99 | 517 | } |
518 | ||
d62a17ae | 519 | void sockopt_iphdrincl_swab_systoh(struct ip *iph) |
96e27c99 | 520 | { |
521 | #ifndef HAVE_IP_HDRINCL_BSD_ORDER | |
d62a17ae | 522 | iph->ip_len = ntohs(iph->ip_len); |
523 | iph->ip_off = ntohs(iph->ip_off); | |
96e27c99 | 524 | #endif /* HAVE_IP_HDRINCL_BSD_ORDER */ |
525 | ||
d62a17ae | 526 | iph->ip_id = ntohs(iph->ip_id); |
96e27c99 | 527 | } |
0df7c91f | 528 | |
d62a17ae | 529 | int sockopt_tcp_rtt(int sock) |
cf279b3a TT |
530 | { |
531 | #ifdef TCP_INFO | |
d62a17ae | 532 | struct tcp_info ti; |
533 | socklen_t len = sizeof(ti); | |
cf279b3a | 534 | |
d62a17ae | 535 | if (getsockopt(sock, IPPROTO_TCP, TCP_INFO, &ti, &len) != 0) |
536 | return 0; | |
cf279b3a | 537 | |
d62a17ae | 538 | return ti.tcpi_rtt / 1000; |
cf279b3a | 539 | #else |
d62a17ae | 540 | return 0; |
cf279b3a TT |
541 | #endif |
542 | } | |
543 | ||
b33e4666 QY |
544 | int sockopt_tcp_signature_ext(int sock, union sockunion *su, uint16_t prefixlen, |
545 | const char *password) | |
0df7c91f | 546 | { |
b33e4666 QY |
547 | #ifndef HAVE_DECL_TCP_MD5SIG |
548 | /* | |
549 | * We have been asked to enable MD5 auth for an address, but our | |
550 | * platform doesn't support that | |
551 | */ | |
552 | return -2; | |
553 | #endif | |
554 | ||
555 | #ifndef TCP_MD5SIG_EXT | |
556 | /* | |
557 | * We have been asked to enable MD5 auth for a prefix, but our platform | |
558 | * doesn't support that | |
559 | */ | |
560 | if (prefixlen > 0) | |
561 | return -2; | |
562 | #endif | |
563 | ||
3535a785 | 564 | #if HAVE_DECL_TCP_MD5SIG |
d62a17ae | 565 | int ret; |
b33e4666 QY |
566 | |
567 | int optname = TCP_MD5SIG; | |
0df7c91f | 568 | #ifndef GNU_LINUX |
d62a17ae | 569 | /* |
570 | * XXX Need to do PF_KEY operation here to add/remove an SA entry, | |
571 | * and add/remove an SP entry for this peer's packet flows also. | |
572 | */ | |
573 | int md5sig = password && *password ? 1 : 0; | |
0df7c91f | 574 | #else |
d62a17ae | 575 | int keylen = password ? strlen(password) : 0; |
576 | struct tcp_md5sig md5sig; | |
577 | union sockunion *su2, *susock; | |
578 | ||
579 | /* Figure out whether the socket and the sockunion are the same family.. | |
580 | * adding AF_INET to AF_INET6 needs to be v4 mapped, you'd think.. | |
581 | */ | |
582 | if (!(susock = sockunion_getsockname(sock))) | |
583 | return -1; | |
584 | ||
585 | if (susock->sa.sa_family == su->sa.sa_family) | |
586 | su2 = su; | |
587 | else { | |
588 | /* oops.. */ | |
589 | su2 = susock; | |
590 | ||
591 | if (su2->sa.sa_family == AF_INET) { | |
592 | sockunion_free(susock); | |
593 | return 0; | |
594 | } | |
595 | ||
596 | /* If this does not work, then all users of this sockopt will | |
597 | * need to | |
03bad95a | 598 | * differentiate between IPv4 and IPv6, and keep separate |
d62a17ae | 599 | * sockets for |
600 | * each. | |
601 | * | |
602 | * Sadly, it doesn't seem to work at present. It's unknown | |
603 | * whether | |
604 | * this is a bug or not. | |
605 | */ | |
606 | if (su2->sa.sa_family == AF_INET6 | |
607 | && su->sa.sa_family == AF_INET) { | |
608 | su2->sin6.sin6_family = AF_INET6; | |
609 | /* V4Map the address */ | |
610 | memset(&su2->sin6.sin6_addr, 0, | |
611 | sizeof(struct in6_addr)); | |
612 | su2->sin6.sin6_addr.s6_addr32[2] = htonl(0xffff); | |
613 | memcpy(&su2->sin6.sin6_addr.s6_addr32[3], | |
614 | &su->sin.sin_addr, 4); | |
615 | } | |
616 | } | |
617 | ||
618 | memset(&md5sig, 0, sizeof(md5sig)); | |
619 | memcpy(&md5sig.tcpm_addr, su2, sizeof(*su2)); | |
b33e4666 | 620 | |
d62a17ae | 621 | md5sig.tcpm_keylen = keylen; |
622 | if (keylen) | |
623 | memcpy(md5sig.tcpm_key, password, keylen); | |
624 | sockunion_free(susock); | |
b33e4666 QY |
625 | |
626 | /* | |
627 | * Handle support for MD5 signatures on prefixes, if available and | |
628 | * requested. Technically the #ifdef check below is not needed because | |
629 | * if prefixlen > 0 and we don't have support for this feature we would | |
630 | * have already returned by now, but leaving it there to be explicit. | |
631 | */ | |
632 | #ifdef TCP_MD5SIG_EXT | |
633 | if (prefixlen > 0) { | |
634 | md5sig.tcpm_prefixlen = prefixlen; | |
635 | md5sig.tcpm_flags = TCP_MD5SIG_FLAG_PREFIX; | |
636 | optname = TCP_MD5SIG_EXT; | |
637 | } | |
638 | #endif /* TCP_MD5SIG_EXT */ | |
639 | ||
0df7c91f | 640 | #endif /* GNU_LINUX */ |
b33e4666 | 641 | |
0e2d7076 DA |
642 | ret = setsockopt(sock, IPPROTO_TCP, optname, &md5sig, sizeof(md5sig)); |
643 | if (ret < 0) { | |
d62a17ae | 644 | if (ENOENT == errno) |
645 | ret = 0; | |
646 | else | |
09c866e3 | 647 | flog_err_sys( |
450971aa | 648 | EC_LIB_SYSTEM_CALL, |
09c866e3 QY |
649 | "sockopt_tcp_signature: setsockopt(%d): %s", |
650 | sock, safe_strerror(errno)); | |
d62a17ae | 651 | } |
652 | return ret; | |
b33e4666 | 653 | #endif /* HAVE_TCP_MD5SIG */ |
adc109b5 DS |
654 | |
655 | /* | |
656 | * Making compiler happy. If we get to this point we probably | |
657 | * have done something really really wrong. | |
658 | */ | |
659 | return -2; | |
b33e4666 QY |
660 | } |
661 | ||
662 | int sockopt_tcp_signature(int sock, union sockunion *su, const char *password) | |
663 | { | |
664 | return sockopt_tcp_signature_ext(sock, su, 0, password); | |
0df7c91f | 665 | } |
4ab46701 AR |
666 | |
667 | /* set TCP mss value to socket */ | |
668 | int sockopt_tcp_mss_set(int sock, int tcp_maxseg) | |
669 | { | |
670 | int ret = 0; | |
671 | socklen_t tcp_maxseg_len = sizeof(tcp_maxseg); | |
672 | ||
673 | ret = setsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, &tcp_maxseg, | |
674 | tcp_maxseg_len); | |
675 | if (ret != 0) { | |
676 | flog_err_sys(EC_LIB_SYSTEM_CALL, | |
677 | "%s failed: setsockopt(%d): %s", __func__, sock, | |
678 | safe_strerror(errno)); | |
679 | } | |
680 | ||
681 | return ret; | |
682 | } | |
683 | ||
684 | /* get TCP mss value synced by socket */ | |
685 | int sockopt_tcp_mss_get(int sock) | |
686 | { | |
687 | int ret = 0; | |
688 | int tcp_maxseg = 0; | |
689 | socklen_t tcp_maxseg_len = sizeof(tcp_maxseg); | |
690 | ||
691 | ret = getsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, &tcp_maxseg, | |
692 | &tcp_maxseg_len); | |
693 | if (ret != 0) { | |
694 | flog_err_sys(EC_LIB_SYSTEM_CALL, | |
695 | "%s failed: getsockopt(%d): %s", __func__, sock, | |
696 | safe_strerror(errno)); | |
697 | return 0; | |
698 | } | |
699 | ||
700 | return tcp_maxseg; | |
701 | } |