1 /* vi: set sw=4 ts=4: */
2 /*
3 * Utility routines.
4 *
5 * Copyright (C) 2007 Denys Vlasenko
6 *
7 * Licensed under GPLv2, see file LICENSE in this source tree.
8 */
9 #include "libbb.h"
10
11 #if defined(IPV6_PKTINFO) && !defined(IPV6_RECVPKTINFO)
12 # define IPV6_RECVPKTINFO IPV6_PKTINFO
13 #endif
14
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 */
19 void FAST_FUNC
socket_want_pktinfo(int fd UNUSED_PARAM)20 socket_want_pktinfo(int fd UNUSED_PARAM)
21 {
22 #ifdef IP_PKTINFO
23 setsockopt_1(fd, IPPROTO_IP, IP_PKTINFO);
24 #endif
25 #if ENABLE_FEATURE_IPV6 && defined(IPV6_RECVPKTINFO)
26 setsockopt_1(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO);
27 #endif
28 }
29
30
31 ssize_t FAST_FUNC
send_to_from(int fd,void * buf,size_t len,int flags,const struct sockaddr * to,const struct sockaddr * from,socklen_t tolen)32 send_to_from(int fd, void *buf, size_t len, int flags,
33 const struct sockaddr *to,
34 const struct sockaddr *from,
35 socklen_t tolen)
36 {
37 #ifndef IP_PKTINFO
38 (void)from; /* suppress "unused from" warning */
39 return sendto(fd, buf, len, flags, to, tolen);
40 #else
41 struct iovec iov[1];
42 struct msghdr msg;
43 union {
44 char cmsg[CMSG_SPACE(sizeof(struct in_pktinfo))];
45 # if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
46 char cmsg6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
47 # endif
48 } u;
49 struct cmsghdr* cmsgptr;
50
51 if (from->sa_family != AF_INET
52 # if ENABLE_FEATURE_IPV6
53 && from->sa_family != AF_INET6
54 # endif
55 ) {
56 /* ANY local address */
57 return sendto(fd, buf, len, flags, to, tolen);
58 }
59
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
65 memset(&u, 0, sizeof(u));
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;
72 msg.msg_control = &u;
73 msg.msg_controllen = sizeof(u);
74 msg.msg_flags = flags;
75
76 cmsgptr = CMSG_FIRSTHDR(&msg);
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) {
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));
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 */
96 pktptr->ipi_spec_dst = ((struct sockaddr_in*)from)->sin_addr;
97 }
98 # if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
99 else if (/* to->sa_family == AF_INET6 && */ from->sa_family == AF_INET6) {
100 struct in6_pktinfo *pktptr;
101 cmsgptr->cmsg_level = IPPROTO_IPV6;
102 cmsgptr->cmsg_type = IPV6_PKTINFO;
103 cmsgptr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
104 pktptr = (struct in6_pktinfo *)(CMSG_DATA(cmsgptr));
105 /* pktptr->ipi6_ifindex = 0; -- already done by memset(u...) */
106 pktptr->ipi6_addr = ((struct sockaddr_in6*)from)->sin6_addr;
107 }
108 # endif
109 msg.msg_controllen = cmsgptr->cmsg_len;
110
111 return sendmsg(fd, &msg, flags);
112 #endif
113 }
114
115 /* NB: this will never set port# in 'to'!
116 * _Only_ IP/IPv6 address part of 'to' is _maybe_ modified.
117 * Typical usage is to preinit 'to' with "default" value
118 * before calling recv_from_to(). */
119 ssize_t FAST_FUNC
recv_from_to(int fd,void * buf,size_t len,int flags,struct sockaddr * from,struct sockaddr * to,socklen_t sa_size)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
125 (void)to; /* suppress "unused to" warning */
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))];
132 # if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
133 char cmsg6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
134 # endif
135 } u;
136 struct cmsghdr *cmsgptr;
137 struct msghdr msg;
138 ssize_t recv_length;
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
155 # define to4 ((struct sockaddr_in*)to)
156 # define to6 ((struct sockaddr_in6*)to)
157 /* Here we try to retrieve destination IP and memorize it */
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 ) {
165 const int IPI_ADDR_OFF = offsetof(struct in_pktinfo, ipi_addr);
166 to->sa_family = AF_INET;
167 /*# define pktinfo(cmsgptr) ( (struct in_pktinfo*)(CMSG_DATA(cmsgptr)) )*/
168 /*to4->sin_addr = pktinfo(cmsgptr)->ipi_addr; - may be unaligned */
169 memcpy(&to4->sin_addr, (char*)(CMSG_DATA(cmsgptr)) + IPI_ADDR_OFF, sizeof(to4->sin_addr));
170 /*to4->sin_port = 123; - this data is not supplied by kernel */
171 break;
172 }
173 # if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
174 if (cmsgptr->cmsg_level == IPPROTO_IPV6
175 && cmsgptr->cmsg_type == IPV6_PKTINFO
176 ) {
177 const int IPI6_ADDR_OFF = offsetof(struct in6_pktinfo, ipi6_addr);
178 to->sa_family = AF_INET6;
179 /*# define pktinfo(cmsgptr) ( (struct in6_pktinfo*)(CMSG_DATA(cmsgptr)) )*/
180 /*to6->sin6_addr = pktinfo(cmsgptr)->ipi6_addr; - may be unaligned */
181 memcpy(&to6->sin6_addr, (char*)(CMSG_DATA(cmsgptr)) + IPI6_ADDR_OFF, sizeof(to6->sin6_addr));
182 /*to6->sin6_port = 123; */
183 break;
184 }
185 # endif
186 }
187 return recv_length;
188 #endif
189 }
190