paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 18 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 19 | * 02111-1307, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <zebra.h> |
| 23 | #include "log.h" |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 24 | #include "sockopt.h" |
Paul Jakma | 0df7c91 | 2008-07-21 21:02:49 +0000 | [diff] [blame] | 25 | #include "sockunion.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 26 | |
paul | 0b3acf4 | 2004-09-17 08:39:08 +0000 | [diff] [blame] | 27 | int |
| 28 | setsockopt_so_recvbuf (int sock, int size) |
| 29 | { |
| 30 | int ret; |
| 31 | |
paul | b89e60c | 2004-09-21 15:43:13 +0000 | [diff] [blame] | 32 | if ( (ret = setsockopt (sock, SOL_SOCKET, SO_RCVBUF, (char *) |
ajs | ff29bb3 | 2004-11-19 18:29:22 +0000 | [diff] [blame] | 33 | &size, sizeof (int))) < 0) |
| 34 | zlog_err ("fd %d: can't setsockopt SO_RCVBUF to %d: %s", |
ajs | ae5e24d | 2004-11-19 23:43:10 +0000 | [diff] [blame] | 35 | sock,size,safe_strerror(errno)); |
paul | 0b3acf4 | 2004-09-17 08:39:08 +0000 | [diff] [blame] | 36 | |
| 37 | return ret; |
| 38 | } |
| 39 | |
Denis Ovsienko | b7fe414 | 2007-08-21 16:32:56 +0000 | [diff] [blame] | 40 | int |
| 41 | setsockopt_so_sendbuf (const int sock, int size) |
| 42 | { |
| 43 | int ret = setsockopt (sock, SOL_SOCKET, SO_SNDBUF, |
| 44 | (char *)&size, sizeof (int)); |
| 45 | |
| 46 | if (ret < 0) |
| 47 | zlog_err ("fd %d: can't setsockopt SO_SNDBUF to %d: %s", |
| 48 | sock, size, safe_strerror (errno)); |
| 49 | |
| 50 | return ret; |
| 51 | } |
| 52 | |
| 53 | int |
| 54 | getsockopt_so_sendbuf (const int sock) |
| 55 | { |
| 56 | u_int32_t optval; |
| 57 | socklen_t optlen = sizeof (optval); |
| 58 | int ret = getsockopt (sock, SOL_SOCKET, SO_SNDBUF, |
| 59 | (char *)&optval, &optlen); |
| 60 | if (ret < 0) |
| 61 | { |
| 62 | zlog_err ("fd %d: can't getsockopt SO_SNDBUF: %d (%s)", |
| 63 | sock, errno, safe_strerror (errno)); |
| 64 | return ret; |
| 65 | } |
| 66 | return optval; |
| 67 | } |
| 68 | |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 69 | static void * |
| 70 | getsockopt_cmsg_data (struct msghdr *msgh, int level, int type) |
| 71 | { |
| 72 | struct cmsghdr *cmsg; |
| 73 | void *ptr = NULL; |
| 74 | |
ajs | b99760a | 2005-01-04 16:24:43 +0000 | [diff] [blame] | 75 | for (cmsg = ZCMSG_FIRSTHDR(msgh); |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 76 | cmsg != NULL; |
| 77 | cmsg = CMSG_NXTHDR(msgh, cmsg)) |
| 78 | if (cmsg->cmsg_level == level && cmsg->cmsg_type) |
| 79 | return (ptr = CMSG_DATA(cmsg)); |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 80 | |
| 81 | return NULL; |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 82 | } |
| 83 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 84 | #ifdef HAVE_IPV6 |
| 85 | /* Set IPv6 packet info to the socket. */ |
| 86 | int |
| 87 | setsockopt_ipv6_pktinfo (int sock, int val) |
| 88 | { |
| 89 | int ret; |
| 90 | |
| 91 | #ifdef IPV6_RECVPKTINFO /*2292bis-01*/ |
| 92 | ret = setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &val, sizeof(val)); |
| 93 | if (ret < 0) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 94 | zlog_warn ("can't setsockopt IPV6_RECVPKTINFO : %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 95 | #else /*RFC2292*/ |
| 96 | ret = setsockopt(sock, IPPROTO_IPV6, IPV6_PKTINFO, &val, sizeof(val)); |
| 97 | if (ret < 0) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 98 | zlog_warn ("can't setsockopt IPV6_PKTINFO : %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 99 | #endif /* INIA_IPV6 */ |
| 100 | return ret; |
| 101 | } |
| 102 | |
| 103 | /* Set multicast hops val to the socket. */ |
| 104 | int |
| 105 | setsockopt_ipv6_checksum (int sock, int val) |
| 106 | { |
| 107 | int ret; |
| 108 | |
| 109 | #ifdef GNU_LINUX |
| 110 | ret = setsockopt(sock, IPPROTO_RAW, IPV6_CHECKSUM, &val, sizeof(val)); |
| 111 | #else |
| 112 | ret = setsockopt(sock, IPPROTO_IPV6, IPV6_CHECKSUM, &val, sizeof(val)); |
| 113 | #endif /* GNU_LINUX */ |
| 114 | if (ret < 0) |
| 115 | zlog_warn ("can't setsockopt IPV6_CHECKSUM"); |
| 116 | return ret; |
| 117 | } |
| 118 | |
| 119 | /* Set multicast hops val to the socket. */ |
| 120 | int |
| 121 | setsockopt_ipv6_multicast_hops (int sock, int val) |
| 122 | { |
| 123 | int ret; |
| 124 | |
| 125 | ret = setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val)); |
| 126 | if (ret < 0) |
| 127 | zlog_warn ("can't setsockopt IPV6_MULTICAST_HOPS"); |
| 128 | return ret; |
| 129 | } |
| 130 | |
| 131 | /* Set multicast hops val to the socket. */ |
| 132 | int |
| 133 | setsockopt_ipv6_unicast_hops (int sock, int val) |
| 134 | { |
| 135 | int ret; |
| 136 | |
| 137 | ret = setsockopt(sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &val, sizeof(val)); |
| 138 | if (ret < 0) |
| 139 | zlog_warn ("can't setsockopt IPV6_UNICAST_HOPS"); |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | int |
| 144 | setsockopt_ipv6_hoplimit (int sock, int val) |
| 145 | { |
| 146 | int ret; |
| 147 | |
| 148 | #ifdef IPV6_RECVHOPLIMIT /*2292bis-01*/ |
| 149 | ret = setsockopt (sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &val, sizeof(val)); |
| 150 | if (ret < 0) |
| 151 | zlog_warn ("can't setsockopt IPV6_RECVHOPLIMIT"); |
| 152 | #else /*RFC2292*/ |
| 153 | ret = setsockopt (sock, IPPROTO_IPV6, IPV6_HOPLIMIT, &val, sizeof(val)); |
| 154 | if (ret < 0) |
| 155 | zlog_warn ("can't setsockopt IPV6_HOPLIMIT"); |
| 156 | #endif |
| 157 | return ret; |
| 158 | } |
| 159 | |
| 160 | /* Set multicast loop zero to the socket. */ |
| 161 | int |
| 162 | setsockopt_ipv6_multicast_loop (int sock, int val) |
| 163 | { |
| 164 | int ret; |
| 165 | |
| 166 | ret = setsockopt (sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val, |
| 167 | sizeof (val)); |
| 168 | if (ret < 0) |
| 169 | zlog_warn ("can't setsockopt IPV6_MULTICAST_LOOP"); |
| 170 | return ret; |
| 171 | } |
| 172 | |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 173 | static int |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 174 | getsockopt_ipv6_ifindex (struct msghdr *msgh) |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 175 | { |
| 176 | struct in6_pktinfo *pktinfo; |
| 177 | |
| 178 | pktinfo = getsockopt_cmsg_data (msgh, IPPROTO_IPV6, IPV6_PKTINFO); |
| 179 | |
| 180 | return pktinfo->ipi6_ifindex; |
| 181 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 182 | |
Stephen Hemminger | 6d0732c | 2011-09-28 14:23:35 +0400 | [diff] [blame] | 183 | int |
| 184 | setsockopt_ipv6_tclass(int sock, int tclass) |
| 185 | { |
Denis Ovsienko | ad61af6 | 2011-10-12 15:07:34 +0400 | [diff] [blame] | 186 | int ret = 0; |
Stephen Hemminger | 6d0732c | 2011-09-28 14:23:35 +0400 | [diff] [blame] | 187 | |
Denis Ovsienko | ad61af6 | 2011-10-12 15:07:34 +0400 | [diff] [blame] | 188 | #ifdef IPV6_TCLASS /* RFC3542 */ |
Stephen Hemminger | 6d0732c | 2011-09-28 14:23:35 +0400 | [diff] [blame] | 189 | ret = setsockopt (sock, IPPROTO_IPV6, IPV6_TCLASS, &tclass, sizeof (tclass)); |
| 190 | if (ret < 0) |
| 191 | zlog_warn ("Can't set IPV6_TCLASS option for fd %d to %#x: %s", |
| 192 | sock, tclass, safe_strerror(errno)); |
Denis Ovsienko | ad61af6 | 2011-10-12 15:07:34 +0400 | [diff] [blame] | 193 | #endif |
Stephen Hemminger | 6d0732c | 2011-09-28 14:23:35 +0400 | [diff] [blame] | 194 | return ret; |
| 195 | } |
| 196 | #endif /* HAVE_IPV6 */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 197 | |
gdt | cc49eb5 | 2004-12-30 13:50:32 +0000 | [diff] [blame] | 198 | /* |
| 199 | * Process multicast socket options for IPv4 in an OS-dependent manner. |
Dmitrij Tejblum | 69bf3a3 | 2011-08-18 20:22:17 +0400 | [diff] [blame] | 200 | * Supported options are IP_{ADD,DROP}_MEMBERSHIP. |
gdt | cc49eb5 | 2004-12-30 13:50:32 +0000 | [diff] [blame] | 201 | * |
| 202 | * Many operating systems have a limit on the number of groups that |
| 203 | * can be joined per socket (where each group and local address |
| 204 | * counts). This impacts OSPF, which joins groups on each interface |
| 205 | * using a single socket. The limit is typically 20, derived from the |
| 206 | * original BSD multicast implementation. Some systems have |
| 207 | * mechanisms for increasing this limit. |
ajs | c188c37 | 2005-10-21 02:57:41 +0000 | [diff] [blame] | 208 | * |
| 209 | * In many 4.4BSD-derived systems, multicast group operations are not |
| 210 | * allowed on interfaces that are not UP. Thus, a previous attempt to |
| 211 | * leave the group may have failed, leaving it still joined, and we |
| 212 | * drop/join quietly to recover. This may not be necessary, but aims to |
| 213 | * defend against unknown behavior in that we will still return an error |
| 214 | * if the second join fails. It is not clear how other systems |
| 215 | * (e.g. Linux, Solaris) behave when leaving groups on down interfaces, |
| 216 | * but this behavior should not be harmful if they behave the same way, |
| 217 | * allow leaves, or implicitly leave all groups joined to down interfaces. |
gdt | cc49eb5 | 2004-12-30 13:50:32 +0000 | [diff] [blame] | 218 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 219 | int |
Dmitrij Tejblum | 69bf3a3 | 2011-08-18 20:22:17 +0400 | [diff] [blame] | 220 | setsockopt_ipv4_multicast(int sock, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 221 | int optname, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 222 | unsigned int mcast_addr, |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 223 | ifindex_t ifindex) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 224 | { |
Dmitrij Tejblum | 10d04cd | 2011-08-22 11:39:35 +0400 | [diff] [blame] | 225 | #ifdef HAVE_RFC3678 |
| 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; |
| 233 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
| 234 | si->sin_len = sizeof(struct sockaddr_in); |
| 235 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
| 236 | si->sin_addr.s_addr = mcast_addr; |
| 237 | ret = setsockopt(sock, IPPROTO_IP, (optname == IP_ADD_MEMBERSHIP) ? |
| 238 | MCAST_JOIN_GROUP : MCAST_LEAVE_GROUP, (void *)&gr, sizeof(gr)); |
| 239 | if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP) && (errno == EADDRINUSE)) |
| 240 | { |
| 241 | setsockopt(sock, IPPROTO_IP, MCAST_LEAVE_GROUP, (void *)&gr, sizeof(gr)); |
| 242 | ret = setsockopt(sock, IPPROTO_IP, MCAST_JOIN_GROUP, (void *)&gr, sizeof(gr)); |
| 243 | } |
| 244 | return ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 245 | |
Dmitrij Tejblum | 10d04cd | 2011-08-22 11:39:35 +0400 | [diff] [blame] | 246 | #elif defined(HAVE_STRUCT_IP_MREQN_IMR_IFINDEX) && !defined(__FreeBSD__) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 247 | struct ip_mreqn mreqn; |
ajs | c188c37 | 2005-10-21 02:57:41 +0000 | [diff] [blame] | 248 | int ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 249 | |
Dmitrij Tejblum | ee7e75d | 2011-08-19 23:00:30 +0400 | [diff] [blame] | 250 | assert(optname == IP_ADD_MEMBERSHIP || optname == IP_DROP_MEMBERSHIP); |
| 251 | memset (&mreqn, 0, sizeof(mreqn)); |
| 252 | |
Dmitrij Tejblum | 10d04cd | 2011-08-22 11:39:35 +0400 | [diff] [blame] | 253 | mreqn.imr_multiaddr.s_addr = mcast_addr; |
Dmitrij Tejblum | ee7e75d | 2011-08-19 23:00:30 +0400 | [diff] [blame] | 254 | mreqn.imr_ifindex = ifindex; |
| 255 | |
| 256 | ret = setsockopt(sock, IPPROTO_IP, optname, |
| 257 | (void *)&mreqn, sizeof(mreqn)); |
| 258 | if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP) && (errno == EADDRINUSE)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 259 | { |
Dmitrij Tejblum | ee7e75d | 2011-08-19 23:00:30 +0400 | [diff] [blame] | 260 | /* see above: handle possible problem when interface comes back up */ |
| 261 | char buf[1][INET_ADDRSTRLEN]; |
| 262 | zlog_info("setsockopt_ipv4_multicast attempting to drop and " |
| 263 | "re-add (fd %d, mcast %s, ifindex %u)", |
| 264 | sock, |
| 265 | inet_ntop(AF_INET, &mreqn.imr_multiaddr, |
| 266 | buf[0], sizeof(buf[0])), ifindex); |
| 267 | setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, |
| 268 | (void *)&mreqn, sizeof(mreqn)); |
| 269 | ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
| 270 | (void *)&mreqn, sizeof(mreqn)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 271 | } |
Dmitrij Tejblum | ee7e75d | 2011-08-19 23:00:30 +0400 | [diff] [blame] | 272 | return ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 273 | |
| 274 | /* Example defines for another OS, boilerplate off other code in this |
| 275 | function, AND handle optname as per other sections for consistency !! */ |
| 276 | /* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */ |
| 277 | /* Add your favourite OS here! */ |
| 278 | |
Dmitrij Tejblum | 69bf3a3 | 2011-08-18 20:22:17 +0400 | [diff] [blame] | 279 | #elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK) /* #if OS_TYPE */ |
gdt | cc49eb5 | 2004-12-30 13:50:32 +0000 | [diff] [blame] | 280 | /* standard BSD API */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 281 | |
| 282 | struct in_addr m; |
| 283 | struct ip_mreq mreq; |
ajs | c188c37 | 2005-10-21 02:57:41 +0000 | [diff] [blame] | 284 | int ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 285 | |
Dmitrij Tejblum | ee7e75d | 2011-08-19 23:00:30 +0400 | [diff] [blame] | 286 | assert(optname == IP_ADD_MEMBERSHIP || optname == IP_DROP_MEMBERSHIP); |
| 287 | |
Dmitrij Tejblum | 69bf3a3 | 2011-08-18 20:22:17 +0400 | [diff] [blame] | 288 | m.s_addr = htonl(ifindex); |
paul | 42c9819 | 2005-05-07 02:22:51 +0000 | [diff] [blame] | 289 | |
Dmitrij Tejblum | ee7e75d | 2011-08-19 23:00:30 +0400 | [diff] [blame] | 290 | memset (&mreq, 0, sizeof(mreq)); |
| 291 | mreq.imr_multiaddr.s_addr = mcast_addr; |
| 292 | mreq.imr_interface = m; |
| 293 | |
| 294 | ret = setsockopt (sock, IPPROTO_IP, optname, (void *)&mreq, sizeof(mreq)); |
| 295 | if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP) && (errno == EADDRINUSE)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 296 | { |
Dmitrij Tejblum | ee7e75d | 2011-08-19 23:00:30 +0400 | [diff] [blame] | 297 | /* see above: handle possible problem when interface comes back up */ |
| 298 | char buf[1][INET_ADDRSTRLEN]; |
| 299 | zlog_info("setsockopt_ipv4_multicast attempting to drop and " |
| 300 | "re-add (fd %d, mcast %s, ifindex %u)", |
| 301 | sock, |
| 302 | inet_ntop(AF_INET, &mreq.imr_multiaddr, |
| 303 | buf[0], sizeof(buf[0])), ifindex); |
| 304 | setsockopt (sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, |
| 305 | (void *)&mreq, sizeof(mreq)); |
| 306 | ret = setsockopt (sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
| 307 | (void *)&mreq, sizeof(mreq)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 308 | } |
Dmitrij Tejblum | ee7e75d | 2011-08-19 23:00:30 +0400 | [diff] [blame] | 309 | return ret; |
| 310 | |
Dmitrij Tejblum | 69bf3a3 | 2011-08-18 20:22:17 +0400 | [diff] [blame] | 311 | #else |
| 312 | #error "Unsupported multicast API" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 313 | #endif /* #if OS_TYPE */ |
| 314 | |
| 315 | } |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 316 | |
Dmitrij Tejblum | 69bf3a3 | 2011-08-18 20:22:17 +0400 | [diff] [blame] | 317 | /* |
| 318 | * Set IP_MULTICAST_IF socket option in an OS-dependent manner. |
| 319 | */ |
| 320 | int |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 321 | setsockopt_ipv4_multicast_if(int sock, ifindex_t ifindex) |
Dmitrij Tejblum | 69bf3a3 | 2011-08-18 20:22:17 +0400 | [diff] [blame] | 322 | { |
| 323 | |
| 324 | #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX |
| 325 | struct ip_mreqn mreqn; |
Dmitrij Tejblum | e0afa6f | 2011-08-19 22:25:23 +0400 | [diff] [blame] | 326 | memset (&mreqn, 0, sizeof(mreqn)); |
Dmitrij Tejblum | 69bf3a3 | 2011-08-18 20:22:17 +0400 | [diff] [blame] | 327 | |
| 328 | mreqn.imr_ifindex = ifindex; |
| 329 | return setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, (void *)&mreqn, sizeof(mreqn)); |
| 330 | |
| 331 | /* Example defines for another OS, boilerplate off other code in this |
| 332 | function */ |
| 333 | /* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */ |
| 334 | /* Add your favourite OS here! */ |
| 335 | #elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK) |
| 336 | struct in_addr m; |
| 337 | |
| 338 | m.s_addr = htonl(ifindex); |
| 339 | |
| 340 | return setsockopt (sock, IPPROTO_IP, IP_MULTICAST_IF, (void *)&m, sizeof(m)); |
| 341 | #else |
| 342 | #error "Unsupported multicast API" |
| 343 | #endif |
| 344 | } |
| 345 | |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 346 | static int |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 347 | setsockopt_ipv4_ifindex (int sock, ifindex_t val) |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 348 | { |
| 349 | int ret; |
| 350 | |
| 351 | #if defined (IP_PKTINFO) |
ajs | 1d75c8c | 2004-12-28 21:43:17 +0000 | [diff] [blame] | 352 | if ((ret = setsockopt (sock, IPPROTO_IP, IP_PKTINFO, &val, sizeof (val))) < 0) |
| 353 | zlog_warn ("Can't set IP_PKTINFO option for fd %d to %d: %s", |
| 354 | sock,val,safe_strerror(errno)); |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 355 | #elif defined (IP_RECVIF) |
ajs | 1d75c8c | 2004-12-28 21:43:17 +0000 | [diff] [blame] | 356 | if ((ret = setsockopt (sock, IPPROTO_IP, IP_RECVIF, &val, sizeof (val))) < 0) |
| 357 | zlog_warn ("Can't set IP_RECVIF option for fd %d to %d: %s", |
| 358 | sock,val,safe_strerror(errno)); |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 359 | #else |
| 360 | #warning "Neither IP_PKTINFO nor IP_RECVIF is available." |
| 361 | #warning "Will not be able to receive link info." |
| 362 | #warning "Things might be seriously broken.." |
ajs | 1d75c8c | 2004-12-28 21:43:17 +0000 | [diff] [blame] | 363 | /* XXX Does this ever happen? Should there be a zlog_warn message here? */ |
| 364 | ret = -1; |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 365 | #endif |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 366 | return ret; |
| 367 | } |
| 368 | |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 369 | int |
Stephen Hemminger | 1423c80 | 2008-08-14 17:59:25 +0100 | [diff] [blame] | 370 | setsockopt_ipv4_tos(int sock, int tos) |
| 371 | { |
| 372 | int ret; |
| 373 | |
| 374 | ret = setsockopt (sock, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)); |
| 375 | if (ret < 0) |
| 376 | zlog_warn ("Can't set IP_TOS option for fd %d to %#x: %s", |
| 377 | sock, tos, safe_strerror(errno)); |
| 378 | return ret; |
| 379 | } |
| 380 | |
| 381 | |
| 382 | int |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 383 | setsockopt_ifindex (int af, int sock, ifindex_t val) |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 384 | { |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 385 | int ret = -1; |
| 386 | |
| 387 | switch (af) |
| 388 | { |
| 389 | case AF_INET: |
| 390 | ret = setsockopt_ipv4_ifindex (sock, val); |
| 391 | break; |
| 392 | #ifdef HAVE_IPV6 |
| 393 | case AF_INET6: |
| 394 | ret = setsockopt_ipv6_pktinfo (sock, val); |
| 395 | break; |
| 396 | #endif |
| 397 | default: |
hasso | e473b03 | 2004-09-26 16:08:11 +0000 | [diff] [blame] | 398 | zlog_warn ("setsockopt_ifindex: unknown address family %d", af); |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 399 | } |
| 400 | return ret; |
| 401 | } |
| 402 | |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 403 | /* |
| 404 | * Requires: msgh is not NULL and points to a valid struct msghdr, which |
| 405 | * may or may not have control data about the incoming interface. |
| 406 | * |
| 407 | * Returns the interface index (small integer >= 1) if it can be |
| 408 | * determined, or else 0. |
| 409 | */ |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 410 | static ifindex_t |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 411 | getsockopt_ipv4_ifindex (struct msghdr *msgh) |
| 412 | { |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 413 | /* XXX: initialize to zero? (Always overwritten, so just cosmetic.) */ |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 414 | ifindex_t ifindex = -1; |
gdt | 1d69fdf | 2004-12-29 18:53:30 +0000 | [diff] [blame] | 415 | |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 416 | #if defined(IP_PKTINFO) |
| 417 | /* Linux pktinfo based ifindex retrieval */ |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 418 | struct in_pktinfo *pktinfo; |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 419 | |
| 420 | pktinfo = |
| 421 | (struct in_pktinfo *)getsockopt_cmsg_data (msgh, IPPROTO_IP, IP_PKTINFO); |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 422 | /* XXX Can pktinfo be NULL? Clean up post 0.98. */ |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 423 | ifindex = pktinfo->ipi_ifindex; |
| 424 | |
| 425 | #elif defined(IP_RECVIF) |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 426 | |
| 427 | /* retrieval based on IP_RECVIF */ |
| 428 | |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 429 | #ifndef SUNOS_5 |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 430 | /* BSD systems use a sockaddr_dl as the control message payload. */ |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 431 | struct sockaddr_dl *sdl; |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 432 | #else |
| 433 | /* SUNOS_5 uses an integer with the index. */ |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 434 | ifindex_t *ifindex_p; |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 435 | #endif /* SUNOS_5 */ |
gdt | 1d69fdf | 2004-12-29 18:53:30 +0000 | [diff] [blame] | 436 | |
gdt | 33f9232 | 2004-07-23 16:14:32 +0000 | [diff] [blame] | 437 | #ifndef SUNOS_5 |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 438 | /* BSD */ |
gdt | 33f9232 | 2004-07-23 16:14:32 +0000 | [diff] [blame] | 439 | sdl = |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 440 | (struct sockaddr_dl *)getsockopt_cmsg_data (msgh, IPPROTO_IP, IP_RECVIF); |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 441 | if (sdl != NULL) |
| 442 | ifindex = sdl->sdl_index; |
| 443 | else |
| 444 | ifindex = 0; |
| 445 | #else |
| 446 | /* |
| 447 | * Solaris. On Solaris 8, IP_RECVIF is defined, but the call to |
| 448 | * enable it fails with errno=99, and the struct msghdr has |
| 449 | * controllen 0. |
| 450 | */ |
| 451 | ifindex_p = (uint_t *)getsockopt_cmsg_data (msgh, IPPROTO_IP, IP_RECVIF); |
| 452 | if (ifindex_p != NULL) |
| 453 | ifindex = *ifindex_p; |
| 454 | else |
| 455 | ifindex = 0; |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 456 | #endif /* SUNOS_5 */ |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 457 | |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 458 | #else |
| 459 | /* |
| 460 | * Neither IP_PKTINFO nor IP_RECVIF defined - warn at compile time. |
| 461 | * XXX Decide if this is a core service, or if daemons have to cope. |
| 462 | * Since Solaris 8 and OpenBSD seem not to provide it, it seems that |
| 463 | * daemons have to cope. |
| 464 | */ |
| 465 | #warning "getsockopt_ipv4_ifindex: Neither IP_PKTINFO nor IP_RECVIF defined." |
| 466 | #warning "Some daemons may fail to operate correctly!" |
paul | 7d9c6e5 | 2004-10-22 10:54:39 +0000 | [diff] [blame] | 467 | ifindex = 0; |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 468 | |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 469 | #endif /* IP_PKTINFO */ |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 470 | |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 471 | return ifindex; |
| 472 | } |
| 473 | |
| 474 | /* return ifindex, 0 if none found */ |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 475 | ifindex_t |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 476 | getsockopt_ifindex (int af, struct msghdr *msgh) |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 477 | { |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 478 | switch (af) |
| 479 | { |
| 480 | case AF_INET: |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 481 | return (getsockopt_ipv4_ifindex (msgh)); |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 482 | break; |
| 483 | #ifdef HAVE_IPV6 |
| 484 | case AF_INET6: |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 485 | return (getsockopt_ipv6_ifindex (msgh)); |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 486 | break; |
| 487 | #endif |
| 488 | default: |
hasso | e473b03 | 2004-09-26 16:08:11 +0000 | [diff] [blame] | 489 | zlog_warn ("getsockopt_ifindex: unknown address family %d", af); |
Denis Ovsienko | 3a8c3dd | 2011-08-01 21:59:04 +0400 | [diff] [blame] | 490 | return 0; |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 491 | } |
| 492 | } |
paul | 96e27c9 | 2004-10-05 14:33:43 +0000 | [diff] [blame] | 493 | |
| 494 | /* swab iph between order system uses for IP_HDRINCL and host order */ |
| 495 | void |
| 496 | sockopt_iphdrincl_swab_htosys (struct ip *iph) |
| 497 | { |
| 498 | /* BSD and derived take iph in network order, except for |
| 499 | * ip_len and ip_off |
| 500 | */ |
| 501 | #ifndef HAVE_IP_HDRINCL_BSD_ORDER |
| 502 | iph->ip_len = htons(iph->ip_len); |
| 503 | iph->ip_off = htons(iph->ip_off); |
| 504 | #endif /* HAVE_IP_HDRINCL_BSD_ORDER */ |
| 505 | |
| 506 | iph->ip_id = htons(iph->ip_id); |
| 507 | } |
| 508 | |
| 509 | void |
| 510 | sockopt_iphdrincl_swab_systoh (struct ip *iph) |
| 511 | { |
| 512 | #ifndef HAVE_IP_HDRINCL_BSD_ORDER |
| 513 | iph->ip_len = ntohs(iph->ip_len); |
| 514 | iph->ip_off = ntohs(iph->ip_off); |
| 515 | #endif /* HAVE_IP_HDRINCL_BSD_ORDER */ |
| 516 | |
| 517 | iph->ip_id = ntohs(iph->ip_id); |
| 518 | } |
Paul Jakma | 0df7c91 | 2008-07-21 21:02:49 +0000 | [diff] [blame] | 519 | |
| 520 | int |
Timo Teräs | 6b2672f | 2015-04-29 09:43:03 +0300 | [diff] [blame] | 521 | sockopt_tcp_rtt (int sock) |
| 522 | { |
| 523 | #ifdef TCP_INFO |
| 524 | struct tcp_info ti; |
| 525 | socklen_t len = sizeof(ti); |
| 526 | |
| 527 | if (getsockopt (sock, IPPROTO_TCP, TCP_INFO, &ti, &len) != 0) |
| 528 | return 0; |
| 529 | |
| 530 | return ti.tcpi_rtt / 1000; |
| 531 | #else |
| 532 | return 0; |
| 533 | #endif |
| 534 | } |
| 535 | |
| 536 | int |
Paul Jakma | 0df7c91 | 2008-07-21 21:02:49 +0000 | [diff] [blame] | 537 | sockopt_tcp_signature (int sock, union sockunion *su, const char *password) |
| 538 | { |
Paul Jakma | 3453a71 | 2009-06-12 14:21:02 +0100 | [diff] [blame] | 539 | #if defined(HAVE_TCP_MD5_LINUX24) && defined(GNU_LINUX) |
| 540 | /* Support for the old Linux 2.4 TCP-MD5 patch, taken from Hasso Tepper's |
| 541 | * version of the Quagga patch (based on work by Rick Payne, and Bruce |
| 542 | * Simpson) |
| 543 | */ |
| 544 | #define TCP_MD5_AUTH 13 |
| 545 | #define TCP_MD5_AUTH_ADD 1 |
| 546 | #define TCP_MD5_AUTH_DEL 2 |
| 547 | struct tcp_rfc2385_cmd { |
| 548 | u_int8_t command; /* Command - Add/Delete */ |
| 549 | u_int32_t address; /* IPV4 address associated */ |
| 550 | u_int8_t keylen; /* MD5 Key len (do NOT assume 0 terminated ascii) */ |
| 551 | void *key; /* MD5 Key */ |
| 552 | } cmd; |
| 553 | struct in_addr *addr = &su->sin.sin_addr; |
| 554 | |
| 555 | cmd.command = (password != NULL ? TCP_MD5_AUTH_ADD : TCP_MD5_AUTH_DEL); |
| 556 | cmd.address = addr->s_addr; |
| 557 | cmd.keylen = (password != NULL ? strlen (password) : 0); |
| 558 | cmd.key = password; |
| 559 | |
| 560 | return setsockopt (sock, IPPROTO_TCP, TCP_MD5_AUTH, &cmd, sizeof cmd); |
| 561 | |
| 562 | #elif HAVE_DECL_TCP_MD5SIG |
Paul Jakma | f5612dd | 2008-08-15 14:05:08 +0100 | [diff] [blame] | 563 | int ret; |
Paul Jakma | 0df7c91 | 2008-07-21 21:02:49 +0000 | [diff] [blame] | 564 | #ifndef GNU_LINUX |
| 565 | /* |
| 566 | * XXX Need to do PF_KEY operation here to add/remove an SA entry, |
| 567 | * and add/remove an SP entry for this peer's packet flows also. |
| 568 | */ |
| 569 | int md5sig = password && *password ? 1 : 0; |
| 570 | #else |
| 571 | int keylen = password ? strlen (password) : 0; |
| 572 | struct tcp_md5sig md5sig; |
| 573 | union sockunion *su2, *susock; |
Paul Jakma | 0df7c91 | 2008-07-21 21:02:49 +0000 | [diff] [blame] | 574 | |
| 575 | /* Figure out whether the socket and the sockunion are the same family.. |
| 576 | * adding AF_INET to AF_INET6 needs to be v4 mapped, you'd think.. |
| 577 | */ |
| 578 | if (!(susock = sockunion_getsockname (sock))) |
| 579 | return -1; |
| 580 | |
| 581 | if (susock->sa.sa_family == su->sa.sa_family) |
| 582 | su2 = su; |
| 583 | else |
| 584 | { |
| 585 | /* oops.. */ |
| 586 | su2 = susock; |
| 587 | |
| 588 | if (su2->sa.sa_family == AF_INET) |
| 589 | { |
| 590 | sockunion_free (susock); |
Chris Caputo | 2b35ae4 | 2009-06-23 05:34:29 +0000 | [diff] [blame] | 591 | return 0; |
| 592 | } |
Paul Jakma | 0df7c91 | 2008-07-21 21:02:49 +0000 | [diff] [blame] | 593 | |
Stephen Hemminger | e5862a2 | 2009-02-19 23:01:41 -0800 | [diff] [blame] | 594 | #ifdef HAVE_IPV6 |
Paul Jakma | 0df7c91 | 2008-07-21 21:02:49 +0000 | [diff] [blame] | 595 | /* If this does not work, then all users of this sockopt will need to |
| 596 | * differentiate between IPv4 and IPv6, and keep seperate sockets for |
| 597 | * each. |
| 598 | * |
| 599 | * Sadly, it doesn't seem to work at present. It's unknown whether |
| 600 | * this is a bug or not. |
| 601 | */ |
| 602 | if (su2->sa.sa_family == AF_INET6 |
| 603 | && su->sa.sa_family == AF_INET) |
| 604 | { |
| 605 | su2->sin6.sin6_family = AF_INET6; |
| 606 | /* V4Map the address */ |
| 607 | memset (&su2->sin6.sin6_addr, 0, sizeof (struct in6_addr)); |
| 608 | su2->sin6.sin6_addr.s6_addr32[2] = htonl(0xffff); |
| 609 | memcpy (&su2->sin6.sin6_addr.s6_addr32[3], &su->sin.sin_addr, 4); |
| 610 | } |
Stephen Hemminger | e5862a2 | 2009-02-19 23:01:41 -0800 | [diff] [blame] | 611 | #endif |
Paul Jakma | 0df7c91 | 2008-07-21 21:02:49 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | memset (&md5sig, 0, sizeof (md5sig)); |
| 615 | memcpy (&md5sig.tcpm_addr, su2, sizeof (*su2)); |
| 616 | md5sig.tcpm_keylen = keylen; |
| 617 | if (keylen) |
| 618 | memcpy (md5sig.tcpm_key, password, keylen); |
Paul Jakma | f5612dd | 2008-08-15 14:05:08 +0100 | [diff] [blame] | 619 | sockunion_free (susock); |
Paul Jakma | 0df7c91 | 2008-07-21 21:02:49 +0000 | [diff] [blame] | 620 | #endif /* GNU_LINUX */ |
Chris Caputo | 2b35ae4 | 2009-06-23 05:34:29 +0000 | [diff] [blame] | 621 | if ((ret = setsockopt (sock, IPPROTO_TCP, TCP_MD5SIG, &md5sig, sizeof md5sig)) < 0) |
| 622 | { |
| 623 | /* ENOENT is harmless. It is returned when we clear a password for which |
| 624 | one was not previously set. */ |
| 625 | if (ENOENT == errno) |
| 626 | ret = 0; |
| 627 | else |
| 628 | zlog_err ("sockopt_tcp_signature: setsockopt(%d): %s", |
| 629 | sock, safe_strerror(errno)); |
| 630 | } |
Paul Jakma | 0df7c91 | 2008-07-21 21:02:49 +0000 | [diff] [blame] | 631 | return ret; |
| 632 | #else /* HAVE_TCP_MD5SIG */ |
| 633 | return -2; |
Paul Jakma | 3453a71 | 2009-06-12 14:21:02 +0100 | [diff] [blame] | 634 | #endif /* !HAVE_TCP_MD5SIG */ |
Paul Jakma | 0df7c91 | 2008-07-21 21:02:49 +0000 | [diff] [blame] | 635 | } |