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 | |
| 45 | for (cmsg = CMSG_FIRSTHDR(msgh); |
| 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 | |
| 155 | /* Set up a multicast socket options for IPv4 |
| 156 | This is here so that people only have to do their OS multicast mess |
| 157 | in one place rather than all through zebra, ospfd, and ripd |
| 158 | NB: This is a hookpoint for specific OS functionality */ |
| 159 | int |
| 160 | setsockopt_multicast_ipv4(int sock, |
| 161 | int optname, |
| 162 | struct in_addr if_addr, |
| 163 | unsigned int mcast_addr, |
| 164 | unsigned int ifindex) |
| 165 | { |
| 166 | |
| 167 | /* Linux 2.2.0 and up */ |
| 168 | #if defined(GNU_LINUX) && LINUX_VERSION_CODE > 131584 |
| 169 | /* This is better because it uses ifindex directly */ |
| 170 | struct ip_mreqn mreqn; |
| 171 | |
| 172 | switch (optname) |
| 173 | { |
| 174 | case IP_MULTICAST_IF: |
| 175 | case IP_ADD_MEMBERSHIP: |
| 176 | case IP_DROP_MEMBERSHIP: |
| 177 | memset (&mreqn, 0, sizeof(mreqn)); |
| 178 | |
| 179 | if (mcast_addr) |
| 180 | mreqn.imr_multiaddr.s_addr = mcast_addr; |
| 181 | |
| 182 | if (ifindex) |
| 183 | mreqn.imr_ifindex = ifindex; |
| 184 | else |
| 185 | mreqn.imr_address = if_addr; |
| 186 | |
| 187 | return setsockopt(sock, IPPROTO_IP, optname, (void *)&mreqn, sizeof(mreqn)); |
| 188 | break; |
| 189 | |
| 190 | default: |
| 191 | /* Can out and give an understandable error */ |
| 192 | errno = EINVAL; |
| 193 | return -1; |
| 194 | break; |
| 195 | } |
| 196 | |
| 197 | /* Example defines for another OS, boilerplate off other code in this |
| 198 | function, AND handle optname as per other sections for consistency !! */ |
| 199 | /* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */ |
| 200 | /* Add your favourite OS here! */ |
| 201 | |
| 202 | #else /* #if OS_TYPE */ |
| 203 | /* default OS support */ |
| 204 | |
| 205 | struct in_addr m; |
| 206 | struct ip_mreq mreq; |
| 207 | |
| 208 | switch (optname) |
| 209 | { |
| 210 | case IP_MULTICAST_IF: |
| 211 | m = if_addr; |
| 212 | |
| 213 | return setsockopt (sock, IPPROTO_IP, optname, (void *)&m, sizeof(m)); |
| 214 | break; |
| 215 | |
| 216 | case IP_ADD_MEMBERSHIP: |
| 217 | case IP_DROP_MEMBERSHIP: |
| 218 | memset (&mreq, 0, sizeof(mreq)); |
| 219 | mreq.imr_multiaddr.s_addr = mcast_addr; |
| 220 | mreq.imr_interface = if_addr; |
| 221 | |
| 222 | return setsockopt (sock, |
| 223 | IPPROTO_IP, |
| 224 | optname, |
| 225 | (void *)&mreq, |
| 226 | sizeof(mreq)); |
| 227 | break; |
| 228 | |
| 229 | default: |
| 230 | /* Can out and give an understandable error */ |
| 231 | errno = EINVAL; |
| 232 | return -1; |
| 233 | break; |
| 234 | } |
| 235 | #endif /* #if OS_TYPE */ |
| 236 | |
| 237 | } |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 238 | |
| 239 | static int |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 240 | setsockopt_ipv4_ifindex (int sock, int val) |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 241 | { |
| 242 | int ret; |
| 243 | |
| 244 | #if defined (IP_PKTINFO) |
ajs | 1d75c8c | 2004-12-28 21:43:17 +0000 | [diff] [blame] | 245 | if ((ret = setsockopt (sock, IPPROTO_IP, IP_PKTINFO, &val, sizeof (val))) < 0) |
| 246 | zlog_warn ("Can't set IP_PKTINFO option for fd %d to %d: %s", |
| 247 | sock,val,safe_strerror(errno)); |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 248 | #elif defined (IP_RECVIF) |
ajs | 1d75c8c | 2004-12-28 21:43:17 +0000 | [diff] [blame] | 249 | if ((ret = setsockopt (sock, IPPROTO_IP, IP_RECVIF, &val, sizeof (val))) < 0) |
| 250 | zlog_warn ("Can't set IP_RECVIF option for fd %d to %d: %s", |
| 251 | sock,val,safe_strerror(errno)); |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 252 | #else |
| 253 | #warning "Neither IP_PKTINFO nor IP_RECVIF is available." |
| 254 | #warning "Will not be able to receive link info." |
| 255 | #warning "Things might be seriously broken.." |
ajs | 1d75c8c | 2004-12-28 21:43:17 +0000 | [diff] [blame] | 256 | /* XXX Does this ever happen? Should there be a zlog_warn message here? */ |
| 257 | ret = -1; |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 258 | #endif |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 259 | return ret; |
| 260 | } |
| 261 | |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 262 | int |
| 263 | setsockopt_ifindex (int af, int sock, int val) |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 264 | { |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 265 | int ret = -1; |
| 266 | |
| 267 | switch (af) |
| 268 | { |
| 269 | case AF_INET: |
| 270 | ret = setsockopt_ipv4_ifindex (sock, val); |
| 271 | break; |
| 272 | #ifdef HAVE_IPV6 |
| 273 | case AF_INET6: |
| 274 | ret = setsockopt_ipv6_pktinfo (sock, val); |
| 275 | break; |
| 276 | #endif |
| 277 | default: |
hasso | e473b03 | 2004-09-26 16:08:11 +0000 | [diff] [blame] | 278 | zlog_warn ("setsockopt_ifindex: unknown address family %d", af); |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 279 | } |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | static int |
| 284 | getsockopt_ipv4_ifindex (struct msghdr *msgh) |
| 285 | { |
gdt | 1d69fdf | 2004-12-29 18:53:30 +0000 | [diff] [blame^] | 286 | /* |
| 287 | * XXX: This routine's semantics are ill-defined, in particular how |
| 288 | * the results "can't determine interface due to runtime behavior" |
| 289 | * and "OS has no support for how to determine behavior" are |
| 290 | * encoded. For now, return 0 for either case; caller must handle |
| 291 | * as "don't know". |
| 292 | */ |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 293 | int ifindex = -1; |
gdt | 1d69fdf | 2004-12-29 18:53:30 +0000 | [diff] [blame^] | 294 | |
| 295 | /* |
| 296 | * If autoconf found a method to get ifindex, but it didn't work for |
| 297 | * this packet, or on this OS, this routine can be entered with a |
| 298 | * NULL cmsghdr pointer. Check msgh before using in each case |
| 299 | * below, rather than here, to avoid having to ifdef twice, once for |
| 300 | * declarations and once for code. |
| 301 | */ |
| 302 | |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 303 | #if defined(IP_PKTINFO) |
| 304 | /* Linux pktinfo based ifindex retrieval */ |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 305 | struct in_pktinfo *pktinfo; |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 306 | |
gdt | 1d69fdf | 2004-12-29 18:53:30 +0000 | [diff] [blame^] | 307 | if (msgh == NULL) |
| 308 | return 0; |
| 309 | |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 310 | pktinfo = |
| 311 | (struct in_pktinfo *)getsockopt_cmsg_data (msgh, IPPROTO_IP, IP_PKTINFO); |
| 312 | ifindex = pktinfo->ipi_ifindex; |
| 313 | |
| 314 | #elif defined(IP_RECVIF) |
| 315 | /* BSD/other IP_RECVIF based ifindex retrieval */ |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 316 | #ifndef SUNOS_5 |
gdt | 33f9232 | 2004-07-23 16:14:32 +0000 | [diff] [blame] | 317 | /* RECVIF, but not SUNOS, so BSD */ |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 318 | struct sockaddr_dl *sdl; |
| 319 | #endif /* SUNOS_5 */ |
gdt | 33f9232 | 2004-07-23 16:14:32 +0000 | [diff] [blame] | 320 | /* SUNOS_5 doesn't need a structure to extract ifindex */ |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 321 | |
gdt | 1d69fdf | 2004-12-29 18:53:30 +0000 | [diff] [blame^] | 322 | if (msgh == NULL) |
| 323 | return 0; |
| 324 | |
gdt | 33f9232 | 2004-07-23 16:14:32 +0000 | [diff] [blame] | 325 | #ifndef SUNOS_5 |
| 326 | sdl = |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 327 | (struct sockaddr_dl *)getsockopt_cmsg_data (msgh, IPPROTO_IP, IP_RECVIF); |
gdt | 33f9232 | 2004-07-23 16:14:32 +0000 | [diff] [blame] | 328 | ifindex = sdl->sdl_index; |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 329 | #else /* !SUNOS_5 */ |
gdt | 33f9232 | 2004-07-23 16:14:32 +0000 | [diff] [blame] | 330 | ifindex = *(uint_t *)getsockopt_cmsg_data (msgh, IPPROTO_IP, IP_RECVIF); |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 331 | #endif /* SUNOS_5 */ |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 332 | |
| 333 | #else /* neither IP_PKTINFO nor IP_RECVIF, broken */ |
| 334 | |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 335 | #warning "getsockopt_ipv4_pktinfo_ifindex: dont have PKTINFO or RECVIF" |
paul | 7d9c6e5 | 2004-10-22 10:54:39 +0000 | [diff] [blame] | 336 | #warning "things probably will be broken on this platform!" |
gdt | 33f9232 | 2004-07-23 16:14:32 +0000 | [diff] [blame] | 337 | /* XXX why not -1 - this is a failure condition. */ |
paul | 7d9c6e5 | 2004-10-22 10:54:39 +0000 | [diff] [blame] | 338 | ifindex = 0; |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 339 | #endif /* IP_PKTINFO */ |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 340 | return ifindex; |
| 341 | } |
| 342 | |
| 343 | /* return ifindex, 0 if none found */ |
| 344 | int |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 345 | getsockopt_ifindex (int af, struct msghdr *msgh) |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 346 | { |
| 347 | int ifindex = 0; |
| 348 | |
| 349 | switch (af) |
| 350 | { |
| 351 | case AF_INET: |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 352 | return (getsockopt_ipv4_ifindex (msgh)); |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 353 | break; |
| 354 | #ifdef HAVE_IPV6 |
| 355 | case AF_INET6: |
paul | e682276 | 2004-08-19 04:13:29 +0000 | [diff] [blame] | 356 | return (getsockopt_ipv6_ifindex (msgh)); |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 357 | break; |
| 358 | #endif |
| 359 | default: |
hasso | e473b03 | 2004-09-26 16:08:11 +0000 | [diff] [blame] | 360 | zlog_warn ("getsockopt_ifindex: unknown address family %d", af); |
paul | 4f7baa0 | 2004-07-23 15:11:07 +0000 | [diff] [blame] | 361 | return (ifindex = 0); |
| 362 | } |
| 363 | } |
paul | 96e27c9 | 2004-10-05 14:33:43 +0000 | [diff] [blame] | 364 | |
| 365 | /* swab iph between order system uses for IP_HDRINCL and host order */ |
| 366 | void |
| 367 | sockopt_iphdrincl_swab_htosys (struct ip *iph) |
| 368 | { |
| 369 | /* BSD and derived take iph in network order, except for |
| 370 | * ip_len and ip_off |
| 371 | */ |
| 372 | #ifndef HAVE_IP_HDRINCL_BSD_ORDER |
| 373 | iph->ip_len = htons(iph->ip_len); |
| 374 | iph->ip_off = htons(iph->ip_off); |
| 375 | #endif /* HAVE_IP_HDRINCL_BSD_ORDER */ |
| 376 | |
| 377 | iph->ip_id = htons(iph->ip_id); |
| 378 | } |
| 379 | |
| 380 | void |
| 381 | sockopt_iphdrincl_swab_systoh (struct ip *iph) |
| 382 | { |
| 383 | #ifndef HAVE_IP_HDRINCL_BSD_ORDER |
| 384 | iph->ip_len = ntohs(iph->ip_len); |
| 385 | iph->ip_off = ntohs(iph->ip_off); |
| 386 | #endif /* HAVE_IP_HDRINCL_BSD_ORDER */ |
| 387 | |
| 388 | iph->ip_id = ntohs(iph->ip_id); |
| 389 | } |