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