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 | #endif /* HAVE_IPV6 */ |
| 183 | |
| 184 | |
gdt | cc49eb5 | 2004-12-30 13:50:32 +0000 | [diff] [blame] | 185 | /* |
| 186 | * Process multicast socket options for IPv4 in an OS-dependent manner. |
| 187 | * Supported options are IP_MULTICAST_IF and IP_{ADD,DROP}_MEMBERSHIP. |
| 188 | * |
| 189 | * Many operating systems have a limit on the number of groups that |
| 190 | * can be joined per socket (where each group and local address |
| 191 | * counts). This impacts OSPF, which joins groups on each interface |
| 192 | * using a single socket. The limit is typically 20, derived from the |
| 193 | * original BSD multicast implementation. Some systems have |
| 194 | * mechanisms for increasing this limit. |
ajs | c188c37 | 2005-10-21 02:57:41 +0000 | [diff] [blame] | 195 | * |
| 196 | * In many 4.4BSD-derived systems, multicast group operations are not |
| 197 | * allowed on interfaces that are not UP. Thus, a previous attempt to |
| 198 | * leave the group may have failed, leaving it still joined, and we |
| 199 | * drop/join quietly to recover. This may not be necessary, but aims to |
| 200 | * defend against unknown behavior in that we will still return an error |
| 201 | * if the second join fails. It is not clear how other systems |
| 202 | * (e.g. Linux, Solaris) behave when leaving groups on down interfaces, |
| 203 | * but this behavior should not be harmful if they behave the same way, |
| 204 | * allow leaves, or implicitly leave all groups joined to down interfaces. |
gdt | cc49eb5 | 2004-12-30 13:50:32 +0000 | [diff] [blame] | 205 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 206 | int |
| 207 | setsockopt_multicast_ipv4(int sock, |
| 208 | int optname, |
Andrew J. Schorr | 97cabcf | 2007-07-06 17:00:24 +0000 | [diff] [blame] | 209 | struct in_addr if_addr /* required */, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 210 | unsigned int mcast_addr, |
Andrew J. Schorr | 97cabcf | 2007-07-06 17:00:24 +0000 | [diff] [blame] | 211 | unsigned int ifindex /* optional: if non-zero, may be |
| 212 | used instead of if_addr */) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 213 | { |
| 214 | |
paul | 42c9819 | 2005-05-07 02:22:51 +0000 | [diff] [blame] | 215 | #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 216 | /* This is better because it uses ifindex directly */ |
| 217 | struct ip_mreqn mreqn; |
ajs | c188c37 | 2005-10-21 02:57:41 +0000 | [diff] [blame] | 218 | int ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 219 | |
| 220 | switch (optname) |
| 221 | { |
| 222 | case IP_MULTICAST_IF: |
| 223 | case IP_ADD_MEMBERSHIP: |
| 224 | case IP_DROP_MEMBERSHIP: |
| 225 | memset (&mreqn, 0, sizeof(mreqn)); |
| 226 | |
| 227 | if (mcast_addr) |
| 228 | mreqn.imr_multiaddr.s_addr = mcast_addr; |
| 229 | |
| 230 | if (ifindex) |
| 231 | mreqn.imr_ifindex = ifindex; |
| 232 | else |
| 233 | mreqn.imr_address = if_addr; |
| 234 | |
ajs | c188c37 | 2005-10-21 02:57:41 +0000 | [diff] [blame] | 235 | ret = setsockopt(sock, IPPROTO_IP, optname, |
| 236 | (void *)&mreqn, sizeof(mreqn)); |
| 237 | if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP) && (errno == EADDRINUSE)) |
| 238 | { |
| 239 | /* see above: handle possible problem when interface comes back up */ |
| 240 | char buf[2][INET_ADDRSTRLEN]; |
| 241 | zlog_info("setsockopt_multicast_ipv4 attempting to drop and " |
| 242 | "re-add (fd %d, ifaddr %s, mcast %s, ifindex %u)", |
| 243 | sock, |
| 244 | inet_ntop(AF_INET, &if_addr, buf[0], sizeof(buf[0])), |
| 245 | inet_ntop(AF_INET, &mreqn.imr_multiaddr, |
| 246 | buf[1], sizeof(buf[1])), ifindex); |
| 247 | setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, |
| 248 | (void *)&mreqn, sizeof(mreqn)); |
| 249 | ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
| 250 | (void *)&mreqn, sizeof(mreqn)); |
| 251 | } |
| 252 | return ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 253 | break; |
| 254 | |
| 255 | default: |
| 256 | /* Can out and give an understandable error */ |
| 257 | errno = EINVAL; |
| 258 | return -1; |
| 259 | break; |
| 260 | } |
| 261 | |
| 262 | /* Example defines for another OS, boilerplate off other code in this |
| 263 | function, AND handle optname as per other sections for consistency !! */ |
| 264 | /* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */ |
| 265 | /* Add your favourite OS here! */ |
| 266 | |
| 267 | #else /* #if OS_TYPE */ |
gdt | cc49eb5 | 2004-12-30 13:50:32 +0000 | [diff] [blame] | 268 | /* standard BSD API */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 269 | |
| 270 | struct in_addr m; |
| 271 | struct ip_mreq mreq; |
ajs | c188c37 | 2005-10-21 02:57:41 +0000 | [diff] [blame] | 272 | int ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 273 | |
paul | 42c9819 | 2005-05-07 02:22:51 +0000 | [diff] [blame] | 274 | #ifdef HAVE_BSD_STRUCT_IP_MREQ_HACK |
| 275 | if (ifindex) |
| 276 | m.s_addr = htonl(ifindex); |
| 277 | else |
| 278 | #endif |
| 279 | m = if_addr; |
| 280 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 281 | switch (optname) |
| 282 | { |
| 283 | case IP_MULTICAST_IF: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 284 | return setsockopt (sock, IPPROTO_IP, optname, (void *)&m, sizeof(m)); |
| 285 | break; |
| 286 | |
| 287 | case IP_ADD_MEMBERSHIP: |
| 288 | case IP_DROP_MEMBERSHIP: |
| 289 | memset (&mreq, 0, sizeof(mreq)); |
| 290 | mreq.imr_multiaddr.s_addr = mcast_addr; |
paul | 42c9819 | 2005-05-07 02:22:51 +0000 | [diff] [blame] | 291 | mreq.imr_interface = m; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 292 | |
ajs | c188c37 | 2005-10-21 02:57:41 +0000 | [diff] [blame] | 293 | ret = setsockopt (sock, IPPROTO_IP, optname, (void *)&mreq, sizeof(mreq)); |
| 294 | if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP) && (errno == EADDRINUSE)) |
| 295 | { |
| 296 | /* see above: handle possible problem when interface comes back up */ |
| 297 | char buf[2][INET_ADDRSTRLEN]; |
| 298 | zlog_info("setsockopt_multicast_ipv4 attempting to drop and " |
| 299 | "re-add (fd %d, ifaddr %s, mcast %s, ifindex %u)", |
| 300 | sock, |
| 301 | inet_ntop(AF_INET, &if_addr, buf[0], sizeof(buf[0])), |
| 302 | inet_ntop(AF_INET, &mreq.imr_multiaddr, |
| 303 | buf[1], sizeof(buf[1])), 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)); |
| 308 | } |
| 309 | return ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 310 | break; |
| 311 | |
| 312 | default: |
| 313 | /* Can out and give an understandable error */ |
| 314 | errno = EINVAL; |
| 315 | return -1; |
| 316 | break; |
| 317 | } |
| 318 | #endif /* #if OS_TYPE */ |
| 319 | |
| 320 | } |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 321 | |
| 322 | static int |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 323 | setsockopt_ipv4_ifindex (int sock, int val) |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 324 | { |
| 325 | int ret; |
| 326 | |
| 327 | #if defined (IP_PKTINFO) |
ajs | 1d75c8c | 2004-12-28 21:43:17 +0000 | [diff] [blame] | 328 | if ((ret = setsockopt (sock, IPPROTO_IP, IP_PKTINFO, &val, sizeof (val))) < 0) |
| 329 | zlog_warn ("Can't set IP_PKTINFO option for fd %d to %d: %s", |
| 330 | sock,val,safe_strerror(errno)); |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 331 | #elif defined (IP_RECVIF) |
ajs | 1d75c8c | 2004-12-28 21:43:17 +0000 | [diff] [blame] | 332 | if ((ret = setsockopt (sock, IPPROTO_IP, IP_RECVIF, &val, sizeof (val))) < 0) |
| 333 | zlog_warn ("Can't set IP_RECVIF option for fd %d to %d: %s", |
| 334 | sock,val,safe_strerror(errno)); |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 335 | #else |
| 336 | #warning "Neither IP_PKTINFO nor IP_RECVIF is available." |
| 337 | #warning "Will not be able to receive link info." |
| 338 | #warning "Things might be seriously broken.." |
ajs | 1d75c8c | 2004-12-28 21:43:17 +0000 | [diff] [blame] | 339 | /* XXX Does this ever happen? Should there be a zlog_warn message here? */ |
| 340 | ret = -1; |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 341 | #endif |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 342 | return ret; |
| 343 | } |
| 344 | |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 345 | int |
| 346 | setsockopt_ifindex (int af, int sock, int val) |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 347 | { |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 348 | int ret = -1; |
| 349 | |
| 350 | switch (af) |
| 351 | { |
| 352 | case AF_INET: |
| 353 | ret = setsockopt_ipv4_ifindex (sock, val); |
| 354 | break; |
| 355 | #ifdef HAVE_IPV6 |
| 356 | case AF_INET6: |
| 357 | ret = setsockopt_ipv6_pktinfo (sock, val); |
| 358 | break; |
| 359 | #endif |
| 360 | default: |
hasso | e473b03 | 2004-09-26 16:08:11 +0000 | [diff] [blame] | 361 | zlog_warn ("setsockopt_ifindex: unknown address family %d", af); |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 362 | } |
| 363 | return ret; |
| 364 | } |
| 365 | |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 366 | /* |
| 367 | * Requires: msgh is not NULL and points to a valid struct msghdr, which |
| 368 | * may or may not have control data about the incoming interface. |
| 369 | * |
| 370 | * Returns the interface index (small integer >= 1) if it can be |
| 371 | * determined, or else 0. |
| 372 | */ |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 373 | static int |
| 374 | getsockopt_ipv4_ifindex (struct msghdr *msgh) |
| 375 | { |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 376 | /* XXX: initialize to zero? (Always overwritten, so just cosmetic.) */ |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 377 | int ifindex = -1; |
gdt | 1d69fdf | 2004-12-29 18:53:30 +0000 | [diff] [blame] | 378 | |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 379 | #if defined(IP_PKTINFO) |
| 380 | /* Linux pktinfo based ifindex retrieval */ |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 381 | struct in_pktinfo *pktinfo; |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 382 | |
| 383 | pktinfo = |
| 384 | (struct in_pktinfo *)getsockopt_cmsg_data (msgh, IPPROTO_IP, IP_PKTINFO); |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 385 | /* XXX Can pktinfo be NULL? Clean up post 0.98. */ |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 386 | ifindex = pktinfo->ipi_ifindex; |
| 387 | |
| 388 | #elif defined(IP_RECVIF) |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 389 | |
| 390 | /* retrieval based on IP_RECVIF */ |
| 391 | |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 392 | #ifndef SUNOS_5 |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 393 | /* BSD systems use a sockaddr_dl as the control message payload. */ |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 394 | struct sockaddr_dl *sdl; |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 395 | #else |
| 396 | /* SUNOS_5 uses an integer with the index. */ |
| 397 | int *ifindex_p; |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 398 | #endif /* SUNOS_5 */ |
gdt | 1d69fdf | 2004-12-29 18:53:30 +0000 | [diff] [blame] | 399 | |
gdt | 33f9232 | 2004-07-23 16:14:32 +0000 | [diff] [blame] | 400 | #ifndef SUNOS_5 |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 401 | /* BSD */ |
gdt | 33f9232 | 2004-07-23 16:14:32 +0000 | [diff] [blame] | 402 | sdl = |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 403 | (struct sockaddr_dl *)getsockopt_cmsg_data (msgh, IPPROTO_IP, IP_RECVIF); |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 404 | if (sdl != NULL) |
| 405 | ifindex = sdl->sdl_index; |
| 406 | else |
| 407 | ifindex = 0; |
| 408 | #else |
| 409 | /* |
| 410 | * Solaris. On Solaris 8, IP_RECVIF is defined, but the call to |
| 411 | * enable it fails with errno=99, and the struct msghdr has |
| 412 | * controllen 0. |
| 413 | */ |
| 414 | ifindex_p = (uint_t *)getsockopt_cmsg_data (msgh, IPPROTO_IP, IP_RECVIF); |
| 415 | if (ifindex_p != NULL) |
| 416 | ifindex = *ifindex_p; |
| 417 | else |
| 418 | ifindex = 0; |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 419 | #endif /* SUNOS_5 */ |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 420 | |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 421 | #else |
| 422 | /* |
| 423 | * Neither IP_PKTINFO nor IP_RECVIF defined - warn at compile time. |
| 424 | * XXX Decide if this is a core service, or if daemons have to cope. |
| 425 | * Since Solaris 8 and OpenBSD seem not to provide it, it seems that |
| 426 | * daemons have to cope. |
| 427 | */ |
| 428 | #warning "getsockopt_ipv4_ifindex: Neither IP_PKTINFO nor IP_RECVIF defined." |
| 429 | #warning "Some daemons may fail to operate correctly!" |
paul | 7d9c6e5 | 2004-10-22 10:54:39 +0000 | [diff] [blame] | 430 | ifindex = 0; |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 431 | |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 432 | #endif /* IP_PKTINFO */ |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 433 | |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 434 | return ifindex; |
| 435 | } |
| 436 | |
| 437 | /* return ifindex, 0 if none found */ |
| 438 | int |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 439 | getsockopt_ifindex (int af, struct msghdr *msgh) |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 440 | { |
| 441 | int ifindex = 0; |
| 442 | |
| 443 | switch (af) |
| 444 | { |
| 445 | case AF_INET: |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 446 | return (getsockopt_ipv4_ifindex (msgh)); |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 447 | break; |
| 448 | #ifdef HAVE_IPV6 |
| 449 | case AF_INET6: |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 450 | return (getsockopt_ipv6_ifindex (msgh)); |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 451 | break; |
| 452 | #endif |
| 453 | default: |
hasso | e473b03 | 2004-09-26 16:08:11 +0000 | [diff] [blame] | 454 | zlog_warn ("getsockopt_ifindex: unknown address family %d", af); |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 455 | return (ifindex = 0); |
| 456 | } |
| 457 | } |
paul | 96e27c9 | 2004-10-05 14:33:43 +0000 | [diff] [blame] | 458 | |
| 459 | /* swab iph between order system uses for IP_HDRINCL and host order */ |
| 460 | void |
| 461 | sockopt_iphdrincl_swab_htosys (struct ip *iph) |
| 462 | { |
| 463 | /* BSD and derived take iph in network order, except for |
| 464 | * ip_len and ip_off |
| 465 | */ |
| 466 | #ifndef HAVE_IP_HDRINCL_BSD_ORDER |
| 467 | iph->ip_len = htons(iph->ip_len); |
| 468 | iph->ip_off = htons(iph->ip_off); |
| 469 | #endif /* HAVE_IP_HDRINCL_BSD_ORDER */ |
| 470 | |
| 471 | iph->ip_id = htons(iph->ip_id); |
| 472 | } |
| 473 | |
| 474 | void |
| 475 | sockopt_iphdrincl_swab_systoh (struct ip *iph) |
| 476 | { |
| 477 | #ifndef HAVE_IP_HDRINCL_BSD_ORDER |
| 478 | iph->ip_len = ntohs(iph->ip_len); |
| 479 | iph->ip_off = ntohs(iph->ip_off); |
| 480 | #endif /* HAVE_IP_HDRINCL_BSD_ORDER */ |
| 481 | |
| 482 | iph->ip_id = ntohs(iph->ip_id); |
| 483 | } |
Paul Jakma | 0df7c91 | 2008-07-21 21:02:49 +0000 | [diff] [blame^] | 484 | |
| 485 | int |
| 486 | sockopt_tcp_signature (int sock, union sockunion *su, const char *password) |
| 487 | { |
| 488 | #if HAVE_DECL_TCP_MD5SIG |
| 489 | #ifndef GNU_LINUX |
| 490 | /* |
| 491 | * XXX Need to do PF_KEY operation here to add/remove an SA entry, |
| 492 | * and add/remove an SP entry for this peer's packet flows also. |
| 493 | */ |
| 494 | int md5sig = password && *password ? 1 : 0; |
| 495 | #else |
| 496 | int keylen = password ? strlen (password) : 0; |
| 497 | struct tcp_md5sig md5sig; |
| 498 | union sockunion *su2, *susock; |
| 499 | int ret; |
| 500 | |
| 501 | /* Figure out whether the socket and the sockunion are the same family.. |
| 502 | * adding AF_INET to AF_INET6 needs to be v4 mapped, you'd think.. |
| 503 | */ |
| 504 | if (!(susock = sockunion_getsockname (sock))) |
| 505 | return -1; |
| 506 | |
| 507 | if (susock->sa.sa_family == su->sa.sa_family) |
| 508 | su2 = su; |
| 509 | else |
| 510 | { |
| 511 | /* oops.. */ |
| 512 | su2 = susock; |
| 513 | |
| 514 | if (su2->sa.sa_family == AF_INET) |
| 515 | { |
| 516 | sockunion_free (susock); |
| 517 | return -1; |
| 518 | }; |
| 519 | |
| 520 | /* If this does not work, then all users of this sockopt will need to |
| 521 | * differentiate between IPv4 and IPv6, and keep seperate sockets for |
| 522 | * each. |
| 523 | * |
| 524 | * Sadly, it doesn't seem to work at present. It's unknown whether |
| 525 | * this is a bug or not. |
| 526 | */ |
| 527 | if (su2->sa.sa_family == AF_INET6 |
| 528 | && su->sa.sa_family == AF_INET) |
| 529 | { |
| 530 | su2->sin6.sin6_family = AF_INET6; |
| 531 | /* V4Map the address */ |
| 532 | memset (&su2->sin6.sin6_addr, 0, sizeof (struct in6_addr)); |
| 533 | su2->sin6.sin6_addr.s6_addr32[2] = htonl(0xffff); |
| 534 | memcpy (&su2->sin6.sin6_addr.s6_addr32[3], &su->sin.sin_addr, 4); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | memset (&md5sig, 0, sizeof (md5sig)); |
| 539 | memcpy (&md5sig.tcpm_addr, su2, sizeof (*su2)); |
| 540 | md5sig.tcpm_keylen = keylen; |
| 541 | if (keylen) |
| 542 | memcpy (md5sig.tcpm_key, password, keylen); |
| 543 | #endif /* GNU_LINUX */ |
| 544 | ret = setsockopt (sock, IPPROTO_TCP, TCP_MD5SIG, &md5sig, sizeof md5sig); |
| 545 | sockunion_free (susock); |
| 546 | return ret; |
| 547 | #else /* HAVE_TCP_MD5SIG */ |
| 548 | return -2; |
| 549 | #endif /* HAVE_TCP_MD5SIG */ |
| 550 | } |