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