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 | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 25 | |
paul | 0b3acf4 | 2004-09-17 08:39:08 +0000 | [diff] [blame] | 26 | int |
| 27 | setsockopt_so_recvbuf (int sock, int size) |
| 28 | { |
| 29 | int ret; |
| 30 | |
paul | b89e60c | 2004-09-21 15:43:13 +0000 | [diff] [blame] | 31 | if ( (ret = setsockopt (sock, SOL_SOCKET, SO_RCVBUF, (char *) |
ajs | ff29bb3 | 2004-11-19 18:29:22 +0000 | [diff] [blame] | 32 | &size, sizeof (int))) < 0) |
| 33 | zlog_err ("fd %d: can't setsockopt SO_RCVBUF to %d: %s", |
ajs | ae5e24d | 2004-11-19 23:43:10 +0000 | [diff] [blame] | 34 | sock,size,safe_strerror(errno)); |
paul | 0b3acf4 | 2004-09-17 08:39:08 +0000 | [diff] [blame] | 35 | |
| 36 | return ret; |
| 37 | } |
| 38 | |
Denis Ovsienko | b7fe414 | 2007-08-21 16:32:56 +0000 | [diff] [blame] | 39 | int |
| 40 | setsockopt_so_sendbuf (const int sock, int size) |
| 41 | { |
| 42 | int ret = setsockopt (sock, SOL_SOCKET, SO_SNDBUF, |
| 43 | (char *)&size, sizeof (int)); |
| 44 | |
| 45 | if (ret < 0) |
| 46 | zlog_err ("fd %d: can't setsockopt SO_SNDBUF to %d: %s", |
| 47 | sock, size, safe_strerror (errno)); |
| 48 | |
| 49 | return ret; |
| 50 | } |
| 51 | |
| 52 | int |
| 53 | getsockopt_so_sendbuf (const int sock) |
| 54 | { |
| 55 | u_int32_t optval; |
| 56 | socklen_t optlen = sizeof (optval); |
| 57 | int ret = getsockopt (sock, SOL_SOCKET, SO_SNDBUF, |
| 58 | (char *)&optval, &optlen); |
| 59 | if (ret < 0) |
| 60 | { |
| 61 | zlog_err ("fd %d: can't getsockopt SO_SNDBUF: %d (%s)", |
| 62 | sock, errno, safe_strerror (errno)); |
| 63 | return ret; |
| 64 | } |
| 65 | return optval; |
| 66 | } |
| 67 | |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 68 | static void * |
| 69 | getsockopt_cmsg_data (struct msghdr *msgh, int level, int type) |
| 70 | { |
| 71 | struct cmsghdr *cmsg; |
| 72 | void *ptr = NULL; |
| 73 | |
ajs | b99760a | 2005-01-04 16:24:43 +0000 | [diff] [blame] | 74 | for (cmsg = ZCMSG_FIRSTHDR(msgh); |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 75 | cmsg != NULL; |
| 76 | cmsg = CMSG_NXTHDR(msgh, cmsg)) |
| 77 | if (cmsg->cmsg_level == level && cmsg->cmsg_type) |
| 78 | return (ptr = CMSG_DATA(cmsg)); |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 79 | |
| 80 | return NULL; |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 81 | } |
| 82 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 83 | #ifdef HAVE_IPV6 |
| 84 | /* Set IPv6 packet info to the socket. */ |
| 85 | int |
| 86 | setsockopt_ipv6_pktinfo (int sock, int val) |
| 87 | { |
| 88 | int ret; |
| 89 | |
| 90 | #ifdef IPV6_RECVPKTINFO /*2292bis-01*/ |
| 91 | ret = setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &val, sizeof(val)); |
| 92 | if (ret < 0) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 93 | zlog_warn ("can't setsockopt IPV6_RECVPKTINFO : %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 94 | #else /*RFC2292*/ |
| 95 | ret = setsockopt(sock, IPPROTO_IPV6, IPV6_PKTINFO, &val, sizeof(val)); |
| 96 | if (ret < 0) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 97 | zlog_warn ("can't setsockopt IPV6_PKTINFO : %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 98 | #endif /* INIA_IPV6 */ |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | /* Set multicast hops val to the socket. */ |
| 103 | int |
| 104 | setsockopt_ipv6_checksum (int sock, int val) |
| 105 | { |
| 106 | int ret; |
| 107 | |
| 108 | #ifdef GNU_LINUX |
| 109 | ret = setsockopt(sock, IPPROTO_RAW, IPV6_CHECKSUM, &val, sizeof(val)); |
| 110 | #else |
| 111 | ret = setsockopt(sock, IPPROTO_IPV6, IPV6_CHECKSUM, &val, sizeof(val)); |
| 112 | #endif /* GNU_LINUX */ |
| 113 | if (ret < 0) |
| 114 | zlog_warn ("can't setsockopt IPV6_CHECKSUM"); |
| 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | /* Set multicast hops val to the socket. */ |
| 119 | int |
| 120 | setsockopt_ipv6_multicast_hops (int sock, int val) |
| 121 | { |
| 122 | int ret; |
| 123 | |
| 124 | ret = setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val)); |
| 125 | if (ret < 0) |
| 126 | zlog_warn ("can't setsockopt IPV6_MULTICAST_HOPS"); |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | /* Set multicast hops val to the socket. */ |
| 131 | int |
| 132 | setsockopt_ipv6_unicast_hops (int sock, int val) |
| 133 | { |
| 134 | int ret; |
| 135 | |
| 136 | ret = setsockopt(sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &val, sizeof(val)); |
| 137 | if (ret < 0) |
| 138 | zlog_warn ("can't setsockopt IPV6_UNICAST_HOPS"); |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | int |
| 143 | setsockopt_ipv6_hoplimit (int sock, int val) |
| 144 | { |
| 145 | int ret; |
| 146 | |
| 147 | #ifdef IPV6_RECVHOPLIMIT /*2292bis-01*/ |
| 148 | ret = setsockopt (sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &val, sizeof(val)); |
| 149 | if (ret < 0) |
| 150 | zlog_warn ("can't setsockopt IPV6_RECVHOPLIMIT"); |
| 151 | #else /*RFC2292*/ |
| 152 | ret = setsockopt (sock, IPPROTO_IPV6, IPV6_HOPLIMIT, &val, sizeof(val)); |
| 153 | if (ret < 0) |
| 154 | zlog_warn ("can't setsockopt IPV6_HOPLIMIT"); |
| 155 | #endif |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | /* Set multicast loop zero to the socket. */ |
| 160 | int |
| 161 | setsockopt_ipv6_multicast_loop (int sock, int val) |
| 162 | { |
| 163 | int ret; |
| 164 | |
| 165 | ret = setsockopt (sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val, |
| 166 | sizeof (val)); |
| 167 | if (ret < 0) |
| 168 | zlog_warn ("can't setsockopt IPV6_MULTICAST_LOOP"); |
| 169 | return ret; |
| 170 | } |
| 171 | |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 172 | static int |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 173 | getsockopt_ipv6_ifindex (struct msghdr *msgh) |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 174 | { |
| 175 | struct in6_pktinfo *pktinfo; |
| 176 | |
| 177 | pktinfo = getsockopt_cmsg_data (msgh, IPPROTO_IPV6, IPV6_PKTINFO); |
| 178 | |
| 179 | return pktinfo->ipi6_ifindex; |
| 180 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 181 | #endif /* HAVE_IPV6 */ |
| 182 | |
| 183 | |
gdt | cc49eb5 | 2004-12-30 13:50:32 +0000 | [diff] [blame] | 184 | /* |
| 185 | * Process multicast socket options for IPv4 in an OS-dependent manner. |
| 186 | * Supported options are IP_MULTICAST_IF and IP_{ADD,DROP}_MEMBERSHIP. |
| 187 | * |
| 188 | * Many operating systems have a limit on the number of groups that |
| 189 | * can be joined per socket (where each group and local address |
| 190 | * counts). This impacts OSPF, which joins groups on each interface |
| 191 | * using a single socket. The limit is typically 20, derived from the |
| 192 | * original BSD multicast implementation. Some systems have |
| 193 | * mechanisms for increasing this limit. |
ajs | c188c37 | 2005-10-21 02:57:41 +0000 | [diff] [blame] | 194 | * |
| 195 | * In many 4.4BSD-derived systems, multicast group operations are not |
| 196 | * allowed on interfaces that are not UP. Thus, a previous attempt to |
| 197 | * leave the group may have failed, leaving it still joined, and we |
| 198 | * drop/join quietly to recover. This may not be necessary, but aims to |
| 199 | * defend against unknown behavior in that we will still return an error |
| 200 | * if the second join fails. It is not clear how other systems |
| 201 | * (e.g. Linux, Solaris) behave when leaving groups on down interfaces, |
| 202 | * but this behavior should not be harmful if they behave the same way, |
| 203 | * allow leaves, or implicitly leave all groups joined to down interfaces. |
gdt | cc49eb5 | 2004-12-30 13:50:32 +0000 | [diff] [blame] | 204 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 205 | int |
| 206 | setsockopt_multicast_ipv4(int sock, |
| 207 | int optname, |
Andrew J. Schorr | 97cabcf | 2007-07-06 17:00:24 +0000 | [diff] [blame] | 208 | struct in_addr if_addr /* required */, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 209 | unsigned int mcast_addr, |
Andrew J. Schorr | 97cabcf | 2007-07-06 17:00:24 +0000 | [diff] [blame] | 210 | unsigned int ifindex /* optional: if non-zero, may be |
| 211 | used instead of if_addr */) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 212 | { |
| 213 | |
paul | 42c9819 | 2005-05-07 02:22:51 +0000 | [diff] [blame] | 214 | #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 215 | /* This is better because it uses ifindex directly */ |
| 216 | struct ip_mreqn mreqn; |
ajs | c188c37 | 2005-10-21 02:57:41 +0000 | [diff] [blame] | 217 | int ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 218 | |
| 219 | switch (optname) |
| 220 | { |
| 221 | case IP_MULTICAST_IF: |
| 222 | case IP_ADD_MEMBERSHIP: |
| 223 | case IP_DROP_MEMBERSHIP: |
| 224 | memset (&mreqn, 0, sizeof(mreqn)); |
| 225 | |
| 226 | if (mcast_addr) |
| 227 | mreqn.imr_multiaddr.s_addr = mcast_addr; |
| 228 | |
| 229 | if (ifindex) |
| 230 | mreqn.imr_ifindex = ifindex; |
| 231 | else |
| 232 | mreqn.imr_address = if_addr; |
| 233 | |
ajs | c188c37 | 2005-10-21 02:57:41 +0000 | [diff] [blame] | 234 | ret = setsockopt(sock, IPPROTO_IP, optname, |
| 235 | (void *)&mreqn, sizeof(mreqn)); |
| 236 | if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP) && (errno == EADDRINUSE)) |
| 237 | { |
| 238 | /* see above: handle possible problem when interface comes back up */ |
| 239 | char buf[2][INET_ADDRSTRLEN]; |
| 240 | zlog_info("setsockopt_multicast_ipv4 attempting to drop and " |
| 241 | "re-add (fd %d, ifaddr %s, mcast %s, ifindex %u)", |
| 242 | sock, |
| 243 | inet_ntop(AF_INET, &if_addr, buf[0], sizeof(buf[0])), |
| 244 | inet_ntop(AF_INET, &mreqn.imr_multiaddr, |
| 245 | buf[1], sizeof(buf[1])), ifindex); |
| 246 | setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, |
| 247 | (void *)&mreqn, sizeof(mreqn)); |
| 248 | ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
| 249 | (void *)&mreqn, sizeof(mreqn)); |
| 250 | } |
| 251 | return ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 252 | break; |
| 253 | |
| 254 | default: |
| 255 | /* Can out and give an understandable error */ |
| 256 | errno = EINVAL; |
| 257 | return -1; |
| 258 | break; |
| 259 | } |
| 260 | |
| 261 | /* Example defines for another OS, boilerplate off other code in this |
| 262 | function, AND handle optname as per other sections for consistency !! */ |
| 263 | /* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */ |
| 264 | /* Add your favourite OS here! */ |
| 265 | |
| 266 | #else /* #if OS_TYPE */ |
gdt | cc49eb5 | 2004-12-30 13:50:32 +0000 | [diff] [blame] | 267 | /* standard BSD API */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 268 | |
| 269 | struct in_addr m; |
| 270 | struct ip_mreq mreq; |
ajs | c188c37 | 2005-10-21 02:57:41 +0000 | [diff] [blame] | 271 | int ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 272 | |
paul | 42c9819 | 2005-05-07 02:22:51 +0000 | [diff] [blame] | 273 | #ifdef HAVE_BSD_STRUCT_IP_MREQ_HACK |
| 274 | if (ifindex) |
| 275 | m.s_addr = htonl(ifindex); |
| 276 | else |
| 277 | #endif |
| 278 | m = if_addr; |
| 279 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 280 | switch (optname) |
| 281 | { |
| 282 | case IP_MULTICAST_IF: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 283 | return setsockopt (sock, IPPROTO_IP, optname, (void *)&m, sizeof(m)); |
| 284 | break; |
| 285 | |
| 286 | case IP_ADD_MEMBERSHIP: |
| 287 | case IP_DROP_MEMBERSHIP: |
| 288 | memset (&mreq, 0, sizeof(mreq)); |
| 289 | mreq.imr_multiaddr.s_addr = mcast_addr; |
paul | 42c9819 | 2005-05-07 02:22:51 +0000 | [diff] [blame] | 290 | mreq.imr_interface = m; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 291 | |
ajs | c188c37 | 2005-10-21 02:57:41 +0000 | [diff] [blame] | 292 | ret = setsockopt (sock, IPPROTO_IP, optname, (void *)&mreq, sizeof(mreq)); |
| 293 | if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP) && (errno == EADDRINUSE)) |
| 294 | { |
| 295 | /* see above: handle possible problem when interface comes back up */ |
| 296 | char buf[2][INET_ADDRSTRLEN]; |
| 297 | zlog_info("setsockopt_multicast_ipv4 attempting to drop and " |
| 298 | "re-add (fd %d, ifaddr %s, mcast %s, ifindex %u)", |
| 299 | sock, |
| 300 | inet_ntop(AF_INET, &if_addr, buf[0], sizeof(buf[0])), |
| 301 | inet_ntop(AF_INET, &mreq.imr_multiaddr, |
| 302 | buf[1], sizeof(buf[1])), ifindex); |
| 303 | setsockopt (sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, |
| 304 | (void *)&mreq, sizeof(mreq)); |
| 305 | ret = setsockopt (sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
| 306 | (void *)&mreq, sizeof(mreq)); |
| 307 | } |
| 308 | return ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 309 | break; |
| 310 | |
| 311 | default: |
| 312 | /* Can out and give an understandable error */ |
| 313 | errno = EINVAL; |
| 314 | return -1; |
| 315 | break; |
| 316 | } |
| 317 | #endif /* #if OS_TYPE */ |
| 318 | |
| 319 | } |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 320 | |
| 321 | static int |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 322 | setsockopt_ipv4_ifindex (int sock, int val) |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 323 | { |
| 324 | int ret; |
| 325 | |
| 326 | #if defined (IP_PKTINFO) |
ajs | 1d75c8c | 2004-12-28 21:43:17 +0000 | [diff] [blame] | 327 | if ((ret = setsockopt (sock, IPPROTO_IP, IP_PKTINFO, &val, sizeof (val))) < 0) |
| 328 | zlog_warn ("Can't set IP_PKTINFO option for fd %d to %d: %s", |
| 329 | sock,val,safe_strerror(errno)); |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 330 | #elif defined (IP_RECVIF) |
ajs | 1d75c8c | 2004-12-28 21:43:17 +0000 | [diff] [blame] | 331 | if ((ret = setsockopt (sock, IPPROTO_IP, IP_RECVIF, &val, sizeof (val))) < 0) |
| 332 | zlog_warn ("Can't set IP_RECVIF option for fd %d to %d: %s", |
| 333 | sock,val,safe_strerror(errno)); |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 334 | #else |
| 335 | #warning "Neither IP_PKTINFO nor IP_RECVIF is available." |
| 336 | #warning "Will not be able to receive link info." |
| 337 | #warning "Things might be seriously broken.." |
ajs | 1d75c8c | 2004-12-28 21:43:17 +0000 | [diff] [blame] | 338 | /* XXX Does this ever happen? Should there be a zlog_warn message here? */ |
| 339 | ret = -1; |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 340 | #endif |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 341 | return ret; |
| 342 | } |
| 343 | |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 344 | int |
| 345 | setsockopt_ifindex (int af, int sock, int val) |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 346 | { |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 347 | int ret = -1; |
| 348 | |
| 349 | switch (af) |
| 350 | { |
| 351 | case AF_INET: |
| 352 | ret = setsockopt_ipv4_ifindex (sock, val); |
| 353 | break; |
| 354 | #ifdef HAVE_IPV6 |
| 355 | case AF_INET6: |
| 356 | ret = setsockopt_ipv6_pktinfo (sock, val); |
| 357 | break; |
| 358 | #endif |
| 359 | default: |
hasso | e473b03 | 2004-09-26 16:08:11 +0000 | [diff] [blame] | 360 | zlog_warn ("setsockopt_ifindex: unknown address family %d", af); |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 361 | } |
| 362 | return ret; |
| 363 | } |
| 364 | |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 365 | /* |
| 366 | * Requires: msgh is not NULL and points to a valid struct msghdr, which |
| 367 | * may or may not have control data about the incoming interface. |
| 368 | * |
| 369 | * Returns the interface index (small integer >= 1) if it can be |
| 370 | * determined, or else 0. |
| 371 | */ |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 372 | static int |
| 373 | getsockopt_ipv4_ifindex (struct msghdr *msgh) |
| 374 | { |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 375 | /* XXX: initialize to zero? (Always overwritten, so just cosmetic.) */ |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 376 | int ifindex = -1; |
gdt | 1d69fdf | 2004-12-29 18:53:30 +0000 | [diff] [blame] | 377 | |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 378 | #if defined(IP_PKTINFO) |
| 379 | /* Linux pktinfo based ifindex retrieval */ |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 380 | struct in_pktinfo *pktinfo; |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 381 | |
| 382 | pktinfo = |
| 383 | (struct in_pktinfo *)getsockopt_cmsg_data (msgh, IPPROTO_IP, IP_PKTINFO); |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 384 | /* XXX Can pktinfo be NULL? Clean up post 0.98. */ |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 385 | ifindex = pktinfo->ipi_ifindex; |
| 386 | |
| 387 | #elif defined(IP_RECVIF) |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 388 | |
| 389 | /* retrieval based on IP_RECVIF */ |
| 390 | |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 391 | #ifndef SUNOS_5 |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 392 | /* BSD systems use a sockaddr_dl as the control message payload. */ |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 393 | struct sockaddr_dl *sdl; |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 394 | #else |
| 395 | /* SUNOS_5 uses an integer with the index. */ |
| 396 | int *ifindex_p; |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 397 | #endif /* SUNOS_5 */ |
gdt | 1d69fdf | 2004-12-29 18:53:30 +0000 | [diff] [blame] | 398 | |
gdt | 33f9232 | 2004-07-23 16:14:32 +0000 | [diff] [blame] | 399 | #ifndef SUNOS_5 |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 400 | /* BSD */ |
gdt | 33f9232 | 2004-07-23 16:14:32 +0000 | [diff] [blame] | 401 | sdl = |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 402 | (struct sockaddr_dl *)getsockopt_cmsg_data (msgh, IPPROTO_IP, IP_RECVIF); |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 403 | if (sdl != NULL) |
| 404 | ifindex = sdl->sdl_index; |
| 405 | else |
| 406 | ifindex = 0; |
| 407 | #else |
| 408 | /* |
| 409 | * Solaris. On Solaris 8, IP_RECVIF is defined, but the call to |
| 410 | * enable it fails with errno=99, and the struct msghdr has |
| 411 | * controllen 0. |
| 412 | */ |
| 413 | ifindex_p = (uint_t *)getsockopt_cmsg_data (msgh, IPPROTO_IP, IP_RECVIF); |
| 414 | if (ifindex_p != NULL) |
| 415 | ifindex = *ifindex_p; |
| 416 | else |
| 417 | ifindex = 0; |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 418 | #endif /* SUNOS_5 */ |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 419 | |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 420 | #else |
| 421 | /* |
| 422 | * Neither IP_PKTINFO nor IP_RECVIF defined - warn at compile time. |
| 423 | * XXX Decide if this is a core service, or if daemons have to cope. |
| 424 | * Since Solaris 8 and OpenBSD seem not to provide it, it seems that |
| 425 | * daemons have to cope. |
| 426 | */ |
| 427 | #warning "getsockopt_ipv4_ifindex: Neither IP_PKTINFO nor IP_RECVIF defined." |
| 428 | #warning "Some daemons may fail to operate correctly!" |
paul | 7d9c6e5 | 2004-10-22 10:54:39 +0000 | [diff] [blame] | 429 | ifindex = 0; |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 430 | |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 431 | #endif /* IP_PKTINFO */ |
gdt | d44debe | 2004-12-29 20:06:23 +0000 | [diff] [blame] | 432 | |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 433 | return ifindex; |
| 434 | } |
| 435 | |
| 436 | /* return ifindex, 0 if none found */ |
| 437 | int |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 438 | getsockopt_ifindex (int af, struct msghdr *msgh) |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 439 | { |
| 440 | int ifindex = 0; |
| 441 | |
| 442 | switch (af) |
| 443 | { |
| 444 | case AF_INET: |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 445 | return (getsockopt_ipv4_ifindex (msgh)); |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 446 | break; |
| 447 | #ifdef HAVE_IPV6 |
| 448 | case AF_INET6: |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 449 | return (getsockopt_ipv6_ifindex (msgh)); |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 450 | break; |
| 451 | #endif |
| 452 | default: |
hasso | e473b03 | 2004-09-26 16:08:11 +0000 | [diff] [blame] | 453 | zlog_warn ("getsockopt_ifindex: unknown address family %d", af); |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 454 | return (ifindex = 0); |
| 455 | } |
| 456 | } |
paul | 96e27c9 | 2004-10-05 14:33:43 +0000 | [diff] [blame] | 457 | |
| 458 | /* swab iph between order system uses for IP_HDRINCL and host order */ |
| 459 | void |
| 460 | sockopt_iphdrincl_swab_htosys (struct ip *iph) |
| 461 | { |
| 462 | /* BSD and derived take iph in network order, except for |
| 463 | * ip_len and ip_off |
| 464 | */ |
| 465 | #ifndef HAVE_IP_HDRINCL_BSD_ORDER |
| 466 | iph->ip_len = htons(iph->ip_len); |
| 467 | iph->ip_off = htons(iph->ip_off); |
| 468 | #endif /* HAVE_IP_HDRINCL_BSD_ORDER */ |
| 469 | |
| 470 | iph->ip_id = htons(iph->ip_id); |
| 471 | } |
| 472 | |
| 473 | void |
| 474 | sockopt_iphdrincl_swab_systoh (struct ip *iph) |
| 475 | { |
| 476 | #ifndef HAVE_IP_HDRINCL_BSD_ORDER |
| 477 | iph->ip_len = ntohs(iph->ip_len); |
| 478 | iph->ip_off = ntohs(iph->ip_off); |
| 479 | #endif /* HAVE_IP_HDRINCL_BSD_ORDER */ |
| 480 | |
| 481 | iph->ip_id = ntohs(iph->ip_id); |
| 482 | } |