Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Denis Vlasenko | d18f52b | 2008-03-02 12:53:15 +0000 | [diff] [blame] | 5 | * Copyright (C) 2007 Denys Vlasenko |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 8 | */ |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 10 | |
James Clarke | 518fb3b | 2017-10-07 18:53:23 +0100 | [diff] [blame] | 11 | #if defined(IPV6_PKTINFO) && !defined(IPV6_RECVPKTINFO) |
| 12 | # define IPV6_RECVPKTINFO IPV6_PKTINFO |
| 13 | #endif |
| 14 | |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 15 | /* |
| 16 | * This asks kernel to let us know dst addr/port of incoming packets |
| 17 | * We don't check for errors here. Not supported == won't be used |
| 18 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 19 | void FAST_FUNC |
Denys Vlasenko | 2491511 | 2011-08-28 05:31:49 +0200 | [diff] [blame] | 20 | socket_want_pktinfo(int fd UNUSED_PARAM) |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 21 | { |
| 22 | #ifdef IP_PKTINFO |
Denys Vlasenko | c52cbea | 2015-08-24 19:48:03 +0200 | [diff] [blame] | 23 | setsockopt_1(fd, IPPROTO_IP, IP_PKTINFO); |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 24 | #endif |
James Clarke | 518fb3b | 2017-10-07 18:53:23 +0100 | [diff] [blame] | 25 | #if ENABLE_FEATURE_IPV6 && defined(IPV6_RECVPKTINFO) |
| 26 | setsockopt_1(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO); |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 27 | #endif |
| 28 | } |
| 29 | |
| 30 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 31 | ssize_t FAST_FUNC |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 32 | send_to_from(int fd, void *buf, size_t len, int flags, |
Denis Vlasenko | 081eb71 | 2008-03-17 09:02:21 +0000 | [diff] [blame] | 33 | const struct sockaddr *to, |
| 34 | const struct sockaddr *from, |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 35 | socklen_t tolen) |
| 36 | { |
| 37 | #ifndef IP_PKTINFO |
Denys Vlasenko | 1361aa2 | 2009-11-26 16:52:25 +0100 | [diff] [blame] | 38 | (void)from; /* suppress "unused from" warning */ |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 39 | return sendto(fd, buf, len, flags, to, tolen); |
| 40 | #else |
| 41 | struct iovec iov[1]; |
| 42 | struct msghdr msg; |
Denis Vlasenko | e9b76e1 | 2008-05-22 17:41:01 +0000 | [diff] [blame] | 43 | union { |
| 44 | char cmsg[CMSG_SPACE(sizeof(struct in_pktinfo))]; |
Denys Vlasenko | 1361aa2 | 2009-11-26 16:52:25 +0100 | [diff] [blame] | 45 | # if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO) |
Denis Vlasenko | e9b76e1 | 2008-05-22 17:41:01 +0000 | [diff] [blame] | 46 | char cmsg6[CMSG_SPACE(sizeof(struct in6_pktinfo))]; |
Denys Vlasenko | 1361aa2 | 2009-11-26 16:52:25 +0100 | [diff] [blame] | 47 | # endif |
Denis Vlasenko | e9b76e1 | 2008-05-22 17:41:01 +0000 | [diff] [blame] | 48 | } u; |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 49 | struct cmsghdr* cmsgptr; |
| 50 | |
| 51 | if (from->sa_family != AF_INET |
Denys Vlasenko | 1361aa2 | 2009-11-26 16:52:25 +0100 | [diff] [blame] | 52 | # if ENABLE_FEATURE_IPV6 |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 53 | && from->sa_family != AF_INET6 |
Denys Vlasenko | 1361aa2 | 2009-11-26 16:52:25 +0100 | [diff] [blame] | 54 | # endif |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 55 | ) { |
| 56 | /* ANY local address */ |
| 57 | return sendto(fd, buf, len, flags, to, tolen); |
| 58 | } |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 59 | |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 60 | /* man recvmsg and man cmsg is needed to make sense of code below */ |
| 61 | |
| 62 | iov[0].iov_base = buf; |
| 63 | iov[0].iov_len = len; |
| 64 | |
Denis Vlasenko | e9b76e1 | 2008-05-22 17:41:01 +0000 | [diff] [blame] | 65 | memset(&u, 0, sizeof(u)); |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 66 | |
| 67 | memset(&msg, 0, sizeof(msg)); |
| 68 | msg.msg_name = (void *)(struct sockaddr *)to; /* or compiler will annoy us */ |
| 69 | msg.msg_namelen = tolen; |
| 70 | msg.msg_iov = iov; |
| 71 | msg.msg_iovlen = 1; |
Denis Vlasenko | e9b76e1 | 2008-05-22 17:41:01 +0000 | [diff] [blame] | 72 | msg.msg_control = &u; |
| 73 | msg.msg_controllen = sizeof(u); |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 74 | msg.msg_flags = flags; |
| 75 | |
| 76 | cmsgptr = CMSG_FIRSTHDR(&msg); |
Denys Vlasenko | 7cef481 | 2016-09-15 13:20:51 +0200 | [diff] [blame] | 77 | /* |
| 78 | * Users report that to->sa_family can be AF_INET6 too, |
| 79 | * if "to" was acquired by recv_from_to(). IOW: recv_from_to() |
| 80 | * was seen showing IPv6 "from" even when the destination |
| 81 | * of received packet (our local address) was IPv4. |
| 82 | */ |
| 83 | if (/* to->sa_family == AF_INET && */ from->sa_family == AF_INET) { |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 84 | struct in_pktinfo *pktptr; |
| 85 | cmsgptr->cmsg_level = IPPROTO_IP; |
| 86 | cmsgptr->cmsg_type = IP_PKTINFO; |
| 87 | cmsgptr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo)); |
| 88 | pktptr = (struct in_pktinfo *)(CMSG_DATA(cmsgptr)); |
Denys Vlasenko | 1361aa2 | 2009-11-26 16:52:25 +0100 | [diff] [blame] | 89 | /*pktptr->ipi_ifindex = 0; -- already done by memset(u...) */ |
| 90 | /* In general, CMSG_DATA() can be unaligned, but in this case |
| 91 | * we know for sure it is sufficiently aligned: |
| 92 | * CMSG_FIRSTHDR simply returns &u above, |
| 93 | * and CMSG_DATA returns &u + size_t + int + int. |
| 94 | * Thus direct assignment is ok: |
| 95 | */ |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 96 | pktptr->ipi_spec_dst = ((struct sockaddr_in*)from)->sin_addr; |
| 97 | } |
Denys Vlasenko | 1361aa2 | 2009-11-26 16:52:25 +0100 | [diff] [blame] | 98 | # if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO) |
Denys Vlasenko | 7cef481 | 2016-09-15 13:20:51 +0200 | [diff] [blame] | 99 | else if (/* to->sa_family == AF_INET6 && */ from->sa_family == AF_INET6) { |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 100 | struct in6_pktinfo *pktptr; |
| 101 | cmsgptr->cmsg_level = IPPROTO_IPV6; |
Denis Vlasenko | 64a1512 | 2007-04-04 10:16:15 +0000 | [diff] [blame] | 102 | cmsgptr->cmsg_type = IPV6_PKTINFO; |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 103 | cmsgptr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); |
| 104 | pktptr = (struct in6_pktinfo *)(CMSG_DATA(cmsgptr)); |
Denys Vlasenko | 1361aa2 | 2009-11-26 16:52:25 +0100 | [diff] [blame] | 105 | /* pktptr->ipi6_ifindex = 0; -- already done by memset(u...) */ |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 106 | pktptr->ipi6_addr = ((struct sockaddr_in6*)from)->sin6_addr; |
| 107 | } |
Denys Vlasenko | 1361aa2 | 2009-11-26 16:52:25 +0100 | [diff] [blame] | 108 | # endif |
Denis Vlasenko | e9b76e1 | 2008-05-22 17:41:01 +0000 | [diff] [blame] | 109 | msg.msg_controllen = cmsgptr->cmsg_len; |
| 110 | |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 111 | return sendmsg(fd, &msg, flags); |
| 112 | #endif |
| 113 | } |
| 114 | |
Denis Vlasenko | 64a1512 | 2007-04-04 10:16:15 +0000 | [diff] [blame] | 115 | /* NB: this will never set port# in 'to'! |
| 116 | * _Only_ IP/IPv6 address part of 'to' is _maybe_ modified. |
Denis Vlasenko | 7946879 | 2007-04-04 11:02:55 +0000 | [diff] [blame] | 117 | * Typical usage is to preinit 'to' with "default" value |
Denis Vlasenko | 64a1512 | 2007-04-04 10:16:15 +0000 | [diff] [blame] | 118 | * before calling recv_from_to(). */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 119 | ssize_t FAST_FUNC |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 120 | recv_from_to(int fd, void *buf, size_t len, int flags, |
| 121 | struct sockaddr *from, struct sockaddr *to, |
| 122 | socklen_t sa_size) |
| 123 | { |
| 124 | #ifndef IP_PKTINFO |
Denys Vlasenko | 1361aa2 | 2009-11-26 16:52:25 +0100 | [diff] [blame] | 125 | (void)to; /* suppress "unused to" warning */ |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 126 | return recvfrom(fd, buf, len, flags, from, &sa_size); |
| 127 | #else |
| 128 | /* man recvmsg and man cmsg is needed to make sense of code below */ |
| 129 | struct iovec iov[1]; |
| 130 | union { |
| 131 | char cmsg[CMSG_SPACE(sizeof(struct in_pktinfo))]; |
Denys Vlasenko | 1361aa2 | 2009-11-26 16:52:25 +0100 | [diff] [blame] | 132 | # if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO) |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 133 | char cmsg6[CMSG_SPACE(sizeof(struct in6_pktinfo))]; |
Denys Vlasenko | 1361aa2 | 2009-11-26 16:52:25 +0100 | [diff] [blame] | 134 | # endif |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 135 | } u; |
| 136 | struct cmsghdr *cmsgptr; |
| 137 | struct msghdr msg; |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 138 | ssize_t recv_length; |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 139 | |
| 140 | iov[0].iov_base = buf; |
| 141 | iov[0].iov_len = len; |
| 142 | |
| 143 | memset(&msg, 0, sizeof(msg)); |
| 144 | msg.msg_name = (struct sockaddr *)from; |
| 145 | msg.msg_namelen = sa_size; |
| 146 | msg.msg_iov = iov; |
| 147 | msg.msg_iovlen = 1; |
| 148 | msg.msg_control = &u; |
| 149 | msg.msg_controllen = sizeof(u); |
| 150 | |
| 151 | recv_length = recvmsg(fd, &msg, flags); |
| 152 | if (recv_length < 0) |
| 153 | return recv_length; |
| 154 | |
Denys Vlasenko | 1361aa2 | 2009-11-26 16:52:25 +0100 | [diff] [blame] | 155 | # define to4 ((struct sockaddr_in*)to) |
| 156 | # define to6 ((struct sockaddr_in6*)to) |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 157 | /* Here we try to retrieve destination IP and memorize it */ |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 158 | for (cmsgptr = CMSG_FIRSTHDR(&msg); |
| 159 | cmsgptr != NULL; |
| 160 | cmsgptr = CMSG_NXTHDR(&msg, cmsgptr) |
| 161 | ) { |
| 162 | if (cmsgptr->cmsg_level == IPPROTO_IP |
| 163 | && cmsgptr->cmsg_type == IP_PKTINFO |
| 164 | ) { |
Denys Vlasenko | 12ca080 | 2010-02-04 18:41:18 +0100 | [diff] [blame] | 165 | const int IPI_ADDR_OFF = offsetof(struct in_pktinfo, ipi_addr); |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 166 | to->sa_family = AF_INET; |
Denys Vlasenko | 12ca080 | 2010-02-04 18:41:18 +0100 | [diff] [blame] | 167 | /*# define pktinfo(cmsgptr) ( (struct in_pktinfo*)(CMSG_DATA(cmsgptr)) )*/ |
Denys Vlasenko | 1361aa2 | 2009-11-26 16:52:25 +0100 | [diff] [blame] | 168 | /*to4->sin_addr = pktinfo(cmsgptr)->ipi_addr; - may be unaligned */ |
Denys Vlasenko | 12ca080 | 2010-02-04 18:41:18 +0100 | [diff] [blame] | 169 | memcpy(&to4->sin_addr, (char*)(CMSG_DATA(cmsgptr)) + IPI_ADDR_OFF, sizeof(to4->sin_addr)); |
Denys Vlasenko | 1361aa2 | 2009-11-26 16:52:25 +0100 | [diff] [blame] | 170 | /*to4->sin_port = 123; - this data is not supplied by kernel */ |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 171 | break; |
| 172 | } |
Denys Vlasenko | 1361aa2 | 2009-11-26 16:52:25 +0100 | [diff] [blame] | 173 | # if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO) |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 174 | if (cmsgptr->cmsg_level == IPPROTO_IPV6 |
Denis Vlasenko | 64a1512 | 2007-04-04 10:16:15 +0000 | [diff] [blame] | 175 | && cmsgptr->cmsg_type == IPV6_PKTINFO |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 176 | ) { |
Denys Vlasenko | 12ca080 | 2010-02-04 18:41:18 +0100 | [diff] [blame] | 177 | const int IPI6_ADDR_OFF = offsetof(struct in6_pktinfo, ipi6_addr); |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 178 | to->sa_family = AF_INET6; |
Denys Vlasenko | 12ca080 | 2010-02-04 18:41:18 +0100 | [diff] [blame] | 179 | /*# define pktinfo(cmsgptr) ( (struct in6_pktinfo*)(CMSG_DATA(cmsgptr)) )*/ |
Denys Vlasenko | 1361aa2 | 2009-11-26 16:52:25 +0100 | [diff] [blame] | 180 | /*to6->sin6_addr = pktinfo(cmsgptr)->ipi6_addr; - may be unaligned */ |
Denys Vlasenko | 12ca080 | 2010-02-04 18:41:18 +0100 | [diff] [blame] | 181 | memcpy(&to6->sin6_addr, (char*)(CMSG_DATA(cmsgptr)) + IPI6_ADDR_OFF, sizeof(to6->sin6_addr)); |
Denys Vlasenko | 1361aa2 | 2009-11-26 16:52:25 +0100 | [diff] [blame] | 182 | /*to6->sin6_port = 123; */ |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 183 | break; |
| 184 | } |
Denys Vlasenko | 1361aa2 | 2009-11-26 16:52:25 +0100 | [diff] [blame] | 185 | # endif |
Denis Vlasenko | 992e05b | 2007-04-03 01:13:04 +0000 | [diff] [blame] | 186 | } |
| 187 | return recv_length; |
| 188 | #endif |
| 189 | } |