blob: 68355e6c4002ad3b1b027832e9facdd4864b9cb3 [file] [log] [blame]
Denis Vlasenko29fe7262007-04-05 20:26:28 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Denis Vlasenkod18f52b2008-03-02 12:53:15 +00005 * Copyright (C) 2007 Denys Vlasenko
Denis Vlasenko29fe7262007-04-05 20:26:28 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko29fe7262007-04-05 20:26:28 +00008 */
Denis Vlasenko29fe7262007-04-05 20:26:28 +00009#include "libbb.h"
Denis Vlasenko992e05b2007-04-03 01:13:04 +000010
James Clarke518fb3b2017-10-07 18:53:23 +010011#if defined(IPV6_PKTINFO) && !defined(IPV6_RECVPKTINFO)
12# define IPV6_RECVPKTINFO IPV6_PKTINFO
13#endif
14
Denis Vlasenko992e05b2007-04-03 01:13:04 +000015/*
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 Vlasenkodefc1ea2008-06-27 02:52:20 +000019void FAST_FUNC
Denys Vlasenko24915112011-08-28 05:31:49 +020020socket_want_pktinfo(int fd UNUSED_PARAM)
Denis Vlasenko992e05b2007-04-03 01:13:04 +000021{
22#ifdef IP_PKTINFO
Denys Vlasenkoc52cbea2015-08-24 19:48:03 +020023 setsockopt_1(fd, IPPROTO_IP, IP_PKTINFO);
Denis Vlasenko992e05b2007-04-03 01:13:04 +000024#endif
James Clarke518fb3b2017-10-07 18:53:23 +010025#if ENABLE_FEATURE_IPV6 && defined(IPV6_RECVPKTINFO)
26 setsockopt_1(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO);
Denis Vlasenko992e05b2007-04-03 01:13:04 +000027#endif
28}
29
30
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000031ssize_t FAST_FUNC
Denis Vlasenko992e05b2007-04-03 01:13:04 +000032send_to_from(int fd, void *buf, size_t len, int flags,
Denis Vlasenko081eb712008-03-17 09:02:21 +000033 const struct sockaddr *to,
34 const struct sockaddr *from,
Denis Vlasenko992e05b2007-04-03 01:13:04 +000035 socklen_t tolen)
36{
37#ifndef IP_PKTINFO
Denys Vlasenko1361aa22009-11-26 16:52:25 +010038 (void)from; /* suppress "unused from" warning */
Denis Vlasenko992e05b2007-04-03 01:13:04 +000039 return sendto(fd, buf, len, flags, to, tolen);
40#else
41 struct iovec iov[1];
42 struct msghdr msg;
Denis Vlasenkoe9b76e12008-05-22 17:41:01 +000043 union {
44 char cmsg[CMSG_SPACE(sizeof(struct in_pktinfo))];
Denys Vlasenko1361aa22009-11-26 16:52:25 +010045# if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
Denis Vlasenkoe9b76e12008-05-22 17:41:01 +000046 char cmsg6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
Denys Vlasenko1361aa22009-11-26 16:52:25 +010047# endif
Denis Vlasenkoe9b76e12008-05-22 17:41:01 +000048 } u;
Denis Vlasenko992e05b2007-04-03 01:13:04 +000049 struct cmsghdr* cmsgptr;
50
51 if (from->sa_family != AF_INET
Denys Vlasenko1361aa22009-11-26 16:52:25 +010052# if ENABLE_FEATURE_IPV6
Denis Vlasenko992e05b2007-04-03 01:13:04 +000053 && from->sa_family != AF_INET6
Denys Vlasenko1361aa22009-11-26 16:52:25 +010054# endif
Denis Vlasenko992e05b2007-04-03 01:13:04 +000055 ) {
56 /* ANY local address */
57 return sendto(fd, buf, len, flags, to, tolen);
58 }
Denis Vlasenko4b924f32007-05-30 00:29:55 +000059
Denis Vlasenko992e05b2007-04-03 01:13:04 +000060 /* 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 Vlasenkoe9b76e12008-05-22 17:41:01 +000065 memset(&u, 0, sizeof(u));
Denis Vlasenko992e05b2007-04-03 01:13:04 +000066
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 Vlasenkoe9b76e12008-05-22 17:41:01 +000072 msg.msg_control = &u;
73 msg.msg_controllen = sizeof(u);
Denis Vlasenko992e05b2007-04-03 01:13:04 +000074 msg.msg_flags = flags;
75
76 cmsgptr = CMSG_FIRSTHDR(&msg);
Denys Vlasenko7cef4812016-09-15 13:20:51 +020077 /*
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 Vlasenko992e05b2007-04-03 01:13:04 +000084 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 Vlasenko1361aa22009-11-26 16:52:25 +010089 /*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 Vlasenko992e05b2007-04-03 01:13:04 +000096 pktptr->ipi_spec_dst = ((struct sockaddr_in*)from)->sin_addr;
97 }
Denys Vlasenko1361aa22009-11-26 16:52:25 +010098# if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
Denys Vlasenko7cef4812016-09-15 13:20:51 +020099 else if (/* to->sa_family == AF_INET6 && */ from->sa_family == AF_INET6) {
Denis Vlasenko992e05b2007-04-03 01:13:04 +0000100 struct in6_pktinfo *pktptr;
101 cmsgptr->cmsg_level = IPPROTO_IPV6;
Denis Vlasenko64a15122007-04-04 10:16:15 +0000102 cmsgptr->cmsg_type = IPV6_PKTINFO;
Denis Vlasenko992e05b2007-04-03 01:13:04 +0000103 cmsgptr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
104 pktptr = (struct in6_pktinfo *)(CMSG_DATA(cmsgptr));
Denys Vlasenko1361aa22009-11-26 16:52:25 +0100105 /* pktptr->ipi6_ifindex = 0; -- already done by memset(u...) */
Denis Vlasenko992e05b2007-04-03 01:13:04 +0000106 pktptr->ipi6_addr = ((struct sockaddr_in6*)from)->sin6_addr;
107 }
Denys Vlasenko1361aa22009-11-26 16:52:25 +0100108# endif
Denis Vlasenkoe9b76e12008-05-22 17:41:01 +0000109 msg.msg_controllen = cmsgptr->cmsg_len;
110
Denis Vlasenko992e05b2007-04-03 01:13:04 +0000111 return sendmsg(fd, &msg, flags);
112#endif
113}
114
Denis Vlasenko64a15122007-04-04 10:16:15 +0000115/* NB: this will never set port# in 'to'!
116 * _Only_ IP/IPv6 address part of 'to' is _maybe_ modified.
Denis Vlasenko79468792007-04-04 11:02:55 +0000117 * Typical usage is to preinit 'to' with "default" value
Denis Vlasenko64a15122007-04-04 10:16:15 +0000118 * before calling recv_from_to(). */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000119ssize_t FAST_FUNC
Denis Vlasenko992e05b2007-04-03 01:13:04 +0000120recv_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 Vlasenko1361aa22009-11-26 16:52:25 +0100125 (void)to; /* suppress "unused to" warning */
Denis Vlasenko992e05b2007-04-03 01:13:04 +0000126 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 Vlasenko1361aa22009-11-26 16:52:25 +0100132# if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
Denis Vlasenko992e05b2007-04-03 01:13:04 +0000133 char cmsg6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
Denys Vlasenko1361aa22009-11-26 16:52:25 +0100134# endif
Denis Vlasenko992e05b2007-04-03 01:13:04 +0000135 } u;
136 struct cmsghdr *cmsgptr;
137 struct msghdr msg;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000138 ssize_t recv_length;
Denis Vlasenko992e05b2007-04-03 01:13:04 +0000139
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 Vlasenko1361aa22009-11-26 16:52:25 +0100155# define to4 ((struct sockaddr_in*)to)
156# define to6 ((struct sockaddr_in6*)to)
Denis Vlasenko992e05b2007-04-03 01:13:04 +0000157 /* Here we try to retrieve destination IP and memorize it */
Denis Vlasenko992e05b2007-04-03 01:13:04 +0000158 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 Vlasenko12ca0802010-02-04 18:41:18 +0100165 const int IPI_ADDR_OFF = offsetof(struct in_pktinfo, ipi_addr);
Denis Vlasenko992e05b2007-04-03 01:13:04 +0000166 to->sa_family = AF_INET;
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100167 /*# define pktinfo(cmsgptr) ( (struct in_pktinfo*)(CMSG_DATA(cmsgptr)) )*/
Denys Vlasenko1361aa22009-11-26 16:52:25 +0100168 /*to4->sin_addr = pktinfo(cmsgptr)->ipi_addr; - may be unaligned */
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100169 memcpy(&to4->sin_addr, (char*)(CMSG_DATA(cmsgptr)) + IPI_ADDR_OFF, sizeof(to4->sin_addr));
Denys Vlasenko1361aa22009-11-26 16:52:25 +0100170 /*to4->sin_port = 123; - this data is not supplied by kernel */
Denis Vlasenko992e05b2007-04-03 01:13:04 +0000171 break;
172 }
Denys Vlasenko1361aa22009-11-26 16:52:25 +0100173# if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
Denis Vlasenko992e05b2007-04-03 01:13:04 +0000174 if (cmsgptr->cmsg_level == IPPROTO_IPV6
Denis Vlasenko64a15122007-04-04 10:16:15 +0000175 && cmsgptr->cmsg_type == IPV6_PKTINFO
Denis Vlasenko992e05b2007-04-03 01:13:04 +0000176 ) {
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100177 const int IPI6_ADDR_OFF = offsetof(struct in6_pktinfo, ipi6_addr);
Denis Vlasenko992e05b2007-04-03 01:13:04 +0000178 to->sa_family = AF_INET6;
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100179 /*# define pktinfo(cmsgptr) ( (struct in6_pktinfo*)(CMSG_DATA(cmsgptr)) )*/
Denys Vlasenko1361aa22009-11-26 16:52:25 +0100180 /*to6->sin6_addr = pktinfo(cmsgptr)->ipi6_addr; - may be unaligned */
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100181 memcpy(&to6->sin6_addr, (char*)(CMSG_DATA(cmsgptr)) + IPI6_ADDR_OFF, sizeof(to6->sin6_addr));
Denys Vlasenko1361aa22009-11-26 16:52:25 +0100182 /*to6->sin6_port = 123; */
Denis Vlasenko992e05b2007-04-03 01:13:04 +0000183 break;
184 }
Denys Vlasenko1361aa22009-11-26 16:52:25 +0100185# endif
Denis Vlasenko992e05b2007-04-03 01:13:04 +0000186 }
187 return recv_length;
188#endif
189}