dnsd: properly set _src_ IP:port on outgoing UDP packets

function                                             old     new   delta
send_to_from                                           -     258    +258
dnsd_main                                           1500    1568     +68
interrupt                                             17      19      +2
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 2/0 up/down: 328/0)             Total: 328 bytes
   text    data     bss     dec     hex filename
 797101     658    7428  805187   c4943 busybox_old
 797429     658    7428  805515   c4a8b busybox_unstripped
This commit is contained in:
Denis Vlasenko
2008-03-17 09:02:21 +00:00
parent 68404f13d4
commit 081eb71ebd
3 changed files with 65 additions and 63 deletions

View File

@ -476,10 +476,12 @@ struct hostent *xgethostbyname(const char *name);
void socket_want_pktinfo(int fd); void socket_want_pktinfo(int fd);
ssize_t send_to_from(int fd, void *buf, size_t len, int flags, ssize_t send_to_from(int fd, void *buf, size_t len, int flags,
const struct sockaddr *from, const struct sockaddr *to, const struct sockaddr *to,
const struct sockaddr *from,
socklen_t tolen); socklen_t tolen);
ssize_t recv_from_to(int fd, void *buf, size_t len, int flags, ssize_t recv_from_to(int fd, void *buf, size_t len, int flags,
struct sockaddr *from, struct sockaddr *to, struct sockaddr *from,
struct sockaddr *to,
socklen_t sa_size); socklen_t sa_size);
char *xstrdup(const char *s); char *xstrdup(const char *s);

View File

@ -25,10 +25,10 @@ socket_want_pktinfo(int fd)
} }
#ifdef UNUSED
ssize_t ssize_t
send_to_from(int fd, void *buf, size_t len, int flags, send_to_from(int fd, void *buf, size_t len, int flags,
const struct sockaddr *from, const struct sockaddr *to, const struct sockaddr *to,
const struct sockaddr *from,
socklen_t tolen) socklen_t tolen)
{ {
#ifndef IP_PKTINFO #ifndef IP_PKTINFO
@ -92,7 +92,6 @@ send_to_from(int fd, void *buf, size_t len, int flags,
return sendmsg(fd, &msg, flags); return sendmsg(fd, &msg, flags);
#endif #endif
} }
#endif /* UNUSED */
/* NB: this will never set port# in 'to'! /* NB: this will never set port# in 'to'!
* _Only_ IP/IPv6 address part of 'to' is _maybe_ modified. * _Only_ IP/IPv6 address part of 'to' is _maybe_ modified.

View File

@ -36,7 +36,7 @@ enum {
ttl(4B) + rlen(2B) + r (MAX_NAME_LEN =21B) + ttl(4B) + rlen(2B) + r (MAX_NAME_LEN =21B) +
2*querystring (2 MAX_NAME_LEN= 42B), all together 90 Byte 2*querystring (2 MAX_NAME_LEN= 42B), all together 90 Byte
*/ */
MAX_PACK_LEN = 512 + 1, MAX_PACK_LEN = 512,
DEFAULT_TTL = 30, // increase this when not testing? DEFAULT_TTL = 30, // increase this when not testing?
@ -44,12 +44,6 @@ enum {
REQ_PTR = 12 REQ_PTR = 12
}; };
struct dns_repl { // resource record, add 0 or 1 to accepted dns_msg in resp
uint16_t rlen;
uint8_t *r; // resource
uint16_t flags;
};
struct dns_head { // the message from client and first part of response mag struct dns_head { // the message from client and first part of response mag
uint16_t id; uint16_t id;
uint16_t flags; uint16_t flags;
@ -218,20 +212,20 @@ static int table_lookup(uint16_t type, uint8_t * as, uint8_t * qs)
return -1; return -1;
} }
/* /*
* Decode message and generate answer * Decode message and generate answer
*/ */
static int process_packet(uint8_t *buf) static int process_packet(uint8_t *buf)
{ {
uint8_t answstr[MAX_NAME_LEN + 1];
struct dns_head *head; struct dns_head *head;
struct dns_prop *qprop; struct dns_prop *qprop;
struct dns_repl outr; uint8_t *from, *answb;
void *next, *from, *answb; uint16_t outr_rlen;
uint16_t outr_flags;
uint8_t answstr[MAX_NAME_LEN + 1];
int lookup_result, type, len, packet_len;
uint16_t flags; uint16_t flags;
int lookup_result, type, packet_len;
int querystr_len;
answstr[0] = '\0'; answstr[0] = '\0';
@ -247,11 +241,12 @@ static int process_packet(uint8_t * buf)
} }
from = (void *)&head[1]; // start of query string from = (void *)&head[1]; // start of query string
next = answb = from + strlen((char *)from) + 1 + sizeof(struct dns_prop); // where to append answer block //FIXME: strlen of untrusted data??!
querystr_len = strlen((char *)from) + 1 + sizeof(struct dns_prop);
answb = from + querystr_len; // where to append answer block
outr.rlen = 0; // may change later outr_rlen = 0;
outr.r = NULL; outr_flags = 0;
outr.flags = 0;
qprop = (struct dns_prop *)(answb - 4); qprop = (struct dns_prop *)(answb - 4);
type = ntohs(qprop->type); type = ntohs(qprop->type);
@ -262,7 +257,7 @@ static int process_packet(uint8_t * buf)
} }
if (ntohs(qprop->class) != 1 /* class INET */ ) { if (ntohs(qprop->class) != 1 /* class INET */ ) {
outr.flags = 4; /* not supported */ outr_flags = 4; /* not supported */
goto empty_packet; goto empty_packet;
} }
/* we only support standard queries */ /* we only support standard queries */
@ -272,49 +267,50 @@ static int process_packet(uint8_t * buf)
// We have a standard query // We have a standard query
bb_info_msg("%s", (char *)from); bb_info_msg("%s", (char *)from);
lookup_result = table_lookup(type, answstr, (uint8_t*)from); lookup_result = table_lookup(type, answstr, from);
if (lookup_result != 0) { if (lookup_result != 0) {
outr.flags = 3 | 0x0400; //name do not exist and auth outr_flags = 3 | 0x0400; // name do not exist and auth
goto empty_packet; goto empty_packet;
} }
if (type == REQ_A) { // return an address if (type == REQ_A) { // return an address
struct in_addr a; struct in_addr a; // NB! its "struct { unsigned __long__ s_addr; }"
uint32_t v32;
if (!inet_aton((char*)answstr, &a)) { //dotted dec to long conv if (!inet_aton((char*)answstr, &a)) { //dotted dec to long conv
outr.flags = 1; /* Frmt err */ outr_flags = 1; /* Frmt err */
goto empty_packet; goto empty_packet;
} }
memcpy(answstr, &a.s_addr, 4); // save before a disappears v32 = a.s_addr; /* in case long != int */
outr.rlen = 4; // uint32_t IP memcpy(answstr, &v32, 4);
outr_rlen = 4; // uint32_t IP
} else } else
outr.rlen = strlen((char *)answstr) + 1; // a host name outr_rlen = strlen((char *)answstr) + 1; // a host name
outr.r = answstr; // 32 bit ip or a host name outr_flags |= 0x0400; /* authority-bit */
outr.flags |= 0x0400; /* authority-bit */
// we have an answer // we have an answer
head->nansw = htons(1); head->nansw = htons(1);
// copy query block to answer block // copy query block to answer block
len = answb - from; memcpy(answb, from, querystr_len);
memcpy(answb, from, len); answb += querystr_len;
next += len;
// and append answer rr // and append answer rr
*(uint32_t *) next = htonl(ttl); // FIXME: unaligned accesses??
next += 4; *(uint32_t *) answb = htonl(ttl);
*(uint16_t *) next = htons(outr.rlen); answb += 4;
next += 2; *(uint16_t *) answb = htons(outr_rlen);
memcpy(next, (void *)answstr, outr.rlen); answb += 2;
next += outr.rlen; memcpy(answb, answstr, outr_rlen);
answb += outr_rlen;
empty_packet: empty_packet:
flags = ntohs(head->flags); flags = ntohs(head->flags);
// clear rcode and RA, set responsebit and our new flags // clear rcode and RA, set responsebit and our new flags
flags |= (outr.flags & 0xff80) | 0x8000; flags |= (outr_flags & 0xff80) | 0x8000;
head->flags = htons(flags); head->flags = htons(flags);
head->nauth = head->nadd = htons(0); head->nauth = head->nadd = 0;
head->nquer = htons(1); head->nquer = htons(1);
packet_len = (uint8_t *)next - buf; packet_len = answb - buf;
return packet_len; return packet_len;
} }
@ -333,10 +329,13 @@ int dnsd_main(int argc ATTRIBUTE_UNUSED, char **argv)
{ {
const char *listen_interface = "0.0.0.0"; const char *listen_interface = "0.0.0.0";
char *sttl, *sport; char *sttl, *sport;
len_and_sockaddr *lsa; len_and_sockaddr *lsa, *from, *to;
unsigned lsa_size;
int udps; int udps;
uint16_t port = 53; uint16_t port = 53;
uint8_t buf[MAX_PACK_LEN]; /* Paranoid sizing: querystring x2 + ttl + outr_rlen + answstr */
/* I'd rather see process_packet() fixed instead... */
uint8_t buf[MAX_PACK_LEN * 2 + 4 + 2 + (MAX_NAME_LEN+1)];
getopt32(argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport); getopt32(argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport);
//if (option_mask32 & 0x1) // -i //if (option_mask32 & 0x1) // -i
@ -375,32 +374,34 @@ int dnsd_main(int argc ATTRIBUTE_UNUSED, char **argv)
lsa = xdotted2sockaddr(listen_interface, port); lsa = xdotted2sockaddr(listen_interface, port);
udps = xsocket(lsa->u.sa.sa_family, SOCK_DGRAM, 0); udps = xsocket(lsa->u.sa.sa_family, SOCK_DGRAM, 0);
xbind(udps, &lsa->u.sa, lsa->len); xbind(udps, &lsa->u.sa, lsa->len);
/* xlisten(udps, 50); - ?!! DGRAM sockets are never listened on I think? */ socket_want_pktinfo(udps); /* needed for recv_from_to to work */
lsa_size = LSA_LEN_SIZE + lsa->len;
from = xzalloc(lsa_size);
to = xzalloc(lsa_size);
bb_info_msg("Accepting UDP packets on %s", bb_info_msg("Accepting UDP packets on %s",
xmalloc_sockaddr2dotted(&lsa->u.sa)); xmalloc_sockaddr2dotted(&lsa->u.sa));
while (1) { while (1) {
int r; int r;
socklen_t fromlen = lsa->len; /* Try to get *DEST* address (to which of our addresses
// FIXME: need to get *DEST* address (to which of our addresses * this query was directed), and reply from the same address.
// this query was directed), and reply from the same address. * Or else we can exhibit usual UDP ugliness:
// Or else we can exhibit usual UDP ugliness: * [ip1.multihomed.ip2] <= query to ip1 <= peer
// [ip1.multihomed.ip2] <= query to ip1 <= peer * [ip1.multihomed.ip2] => reply from ip2 => peer (confused) */
// [ip1.multihomed.ip2] => reply from ip2 => peer (confused) memcpy(to, lsa, lsa_size);
r = recv_from_to(udps, buf, MAX_PACK_LEN + 1, 0, &from->u.sa, &to->u.sa, lsa->len);
// TODO: recv_from_to if (r < 12 || r > MAX_PACK_LEN) {
r = recvfrom(udps, buf, sizeof(buf), 0, &lsa->u.sa, &fromlen);
if (OPT_verbose)
bb_info_msg("Got UDP packet");
if (r < 12 || r > 512) {
bb_error_msg("invalid packet size"); bb_error_msg("invalid packet size");
continue; continue;
} }
if (OPT_verbose)
bb_info_msg("Got UDP packet");
buf[r] = '\0'; /* paranoia */
r = process_packet(buf); r = process_packet(buf);
if (r <= 0) if (r <= 0)
continue; continue;
sendto(udps, buf, r, 0, &lsa->u.sa, fromlen); send_to_from(udps, buf, r, 0, &to->u.sa, &from->u.sa, lsa->len);
} }
return 0; return 0;
} }