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