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