2013-12-04 21:33:17 +00:00
|
|
|
#include <config.h>
|
|
|
|
#include "networking.h"
|
2014-12-20 22:52:39 +00:00
|
|
|
#include "ntp_debug.h"
|
2013-12-04 21:33:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Send a packet */
|
2014-12-20 22:52:39 +00:00
|
|
|
int
|
2013-12-04 21:33:17 +00:00
|
|
|
sendpkt (
|
|
|
|
SOCKET rsock,
|
|
|
|
sockaddr_u *dest,
|
|
|
|
struct pkt *pkt,
|
|
|
|
int len
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int cc;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-12-20 22:52:39 +00:00
|
|
|
if (debug > 2) {
|
|
|
|
printf("sntp sendpkt: Packet data:\n");
|
|
|
|
pkt_output(pkt, len, stdout);
|
2013-12-04 21:33:17 +00:00
|
|
|
}
|
2014-12-20 22:52:39 +00:00
|
|
|
#endif
|
|
|
|
TRACE(1, ("sntp sendpkt: Sending packet to %s ...\n",
|
|
|
|
sptoa(dest)));
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
cc = sendto(rsock, (void *)pkt, len, 0, &dest->sa,
|
|
|
|
SOCKLEN(dest));
|
2013-12-04 21:33:17 +00:00
|
|
|
if (cc == SOCKET_ERROR) {
|
2020-03-04 13:59:29 +00:00
|
|
|
msyslog(LOG_ERR, "sendpkt: sendto(%s) failed: %m",
|
2014-12-20 22:52:39 +00:00
|
|
|
sptoa(dest));
|
|
|
|
return FALSE;
|
2013-12-04 21:33:17 +00:00
|
|
|
}
|
2014-12-20 22:52:39 +00:00
|
|
|
TRACE(1, ("Packet sent.\n"));
|
|
|
|
|
|
|
|
return TRUE;
|
2013-12-04 21:33:17 +00:00
|
|
|
}
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/* Receive raw data */
|
|
|
|
int
|
|
|
|
recvdata(
|
2014-12-20 22:52:39 +00:00
|
|
|
SOCKET rsock,
|
|
|
|
sockaddr_u * sender,
|
|
|
|
void * rdata,
|
|
|
|
int rdata_length
|
2013-12-04 21:33:17 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
GETSOCKNAME_SOCKLEN_TYPE slen;
|
|
|
|
int recvc;
|
|
|
|
|
|
|
|
slen = sizeof(*sender);
|
2014-12-20 22:52:39 +00:00
|
|
|
recvc = recvfrom(rsock, rdata, rdata_length, 0,
|
2013-12-04 21:33:17 +00:00
|
|
|
&sender->sa, &slen);
|
2014-12-20 22:52:39 +00:00
|
|
|
if (recvc < 0)
|
|
|
|
return recvc;
|
2013-12-04 21:33:17 +00:00
|
|
|
#ifdef DEBUG
|
2014-12-20 22:52:39 +00:00
|
|
|
if (debug > 2) {
|
|
|
|
printf("Received %d bytes from %s:\n", recvc, sptoa(sender));
|
|
|
|
pkt_output((struct pkt *)rdata, recvc, stdout);
|
2013-12-04 21:33:17 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return recvc;
|
|
|
|
}
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
/* Parsing from a short 'struct pkt' directly is bound to create
|
|
|
|
* coverity warnings. These are hard to avoid, as the formal declaration
|
|
|
|
* does not reflect the true layout in the presence of autokey extension
|
|
|
|
* fields. Parsing and skipping the extension fields of a received packet
|
|
|
|
* until there's only the MAC left is better done in this separate
|
|
|
|
* function.
|
|
|
|
*/
|
|
|
|
static void*
|
|
|
|
skip_efields(
|
|
|
|
u_int32 *head, /* head of extension chain */
|
|
|
|
u_int32 *tail /* tail/end of extension chain */
|
2013-12-04 21:33:17 +00:00
|
|
|
)
|
|
|
|
{
|
2014-12-20 22:52:39 +00:00
|
|
|
|
|
|
|
u_int nlen; /* next extension length */
|
|
|
|
while ((tail - head) > 6) {
|
2019-03-07 13:01:16 +00:00
|
|
|
nlen = ntohl(*head) & 0xffff;
|
|
|
|
++head;
|
2014-12-20 22:52:39 +00:00
|
|
|
nlen = (nlen + 3) >> 2;
|
2015-02-05 20:53:33 +00:00
|
|
|
if (nlen > (u_int)(tail - head) || nlen < 4)
|
2014-12-20 22:52:39 +00:00
|
|
|
return NULL; /* Blooper! Inconsistent! */
|
|
|
|
head += nlen;
|
2013-12-04 21:33:17 +00:00
|
|
|
}
|
2014-12-20 22:52:39 +00:00
|
|
|
return head;
|
2013-12-04 21:33:17 +00:00
|
|
|
}
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
/*
|
|
|
|
** Check if it's data for us and whether it's useable or not.
|
|
|
|
**
|
|
|
|
** If not, return a failure code so we can delete this server from our list
|
|
|
|
** and continue with another one.
|
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
int
|
|
|
|
process_pkt (
|
|
|
|
struct pkt *rpkt,
|
2014-12-20 22:52:39 +00:00
|
|
|
sockaddr_u *sender,
|
2013-12-04 21:33:17 +00:00
|
|
|
int pkt_len,
|
|
|
|
int mode,
|
|
|
|
struct pkt *spkt,
|
2014-12-20 22:52:39 +00:00
|
|
|
const char * func_name
|
2013-12-04 21:33:17 +00:00
|
|
|
)
|
|
|
|
{
|
2014-12-20 22:52:39 +00:00
|
|
|
u_int key_id;
|
|
|
|
struct key * pkt_key;
|
|
|
|
int is_authentic;
|
|
|
|
int mac_size;
|
|
|
|
u_int exten_len;
|
|
|
|
u_int32 * exten_end;
|
|
|
|
u_int32 * packet_end;
|
|
|
|
l_fp sent_xmt;
|
|
|
|
l_fp resp_org;
|
|
|
|
|
2015-10-21 19:16:13 +00:00
|
|
|
// key_id = 0;
|
2014-12-20 22:52:39 +00:00
|
|
|
pkt_key = NULL;
|
|
|
|
is_authentic = (HAVE_OPT(AUTHENTICATION)) ? 0 : -1;
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
|
|
|
* Parse the extension field if present. We figure out whether
|
|
|
|
* an extension field is present by measuring the MAC size. If
|
|
|
|
* the number of words following the packet header is 0, no MAC
|
|
|
|
* is present and the packet is not authenticated. If 1, the
|
|
|
|
* packet is a crypto-NAK; if 3, the packet is authenticated
|
|
|
|
* with DES; if 5, the packet is authenticated with MD5; if 6,
|
|
|
|
* the packet is authenticated with SHA. If 2 or 4, the packet
|
|
|
|
* is a runt and discarded forthwith. If greater than 6, an
|
|
|
|
* extension field is present, so we subtract the length of the
|
|
|
|
* field and go around again.
|
|
|
|
*/
|
2015-02-05 20:53:33 +00:00
|
|
|
if (pkt_len < (int)LEN_PKT_NOMAC || (pkt_len & 3) != 0) {
|
2014-12-20 22:52:39 +00:00
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"%s: Incredible packet length: %d. Discarding.",
|
|
|
|
func_name, pkt_len);
|
2013-12-04 21:33:17 +00:00
|
|
|
return PACKET_UNUSEABLE;
|
|
|
|
}
|
2018-02-28 06:23:12 +00:00
|
|
|
|
|
|
|
/* HMS: the following needs a bit of work */
|
2014-12-20 22:52:39 +00:00
|
|
|
/* Note: pkt_len must be a multiple of 4 at this point! */
|
2016-01-08 08:06:14 +00:00
|
|
|
packet_end = (void*)((char*)rpkt + pkt_len);
|
2014-12-20 22:52:39 +00:00
|
|
|
exten_end = skip_efields(rpkt->exten, packet_end);
|
2015-02-05 20:53:33 +00:00
|
|
|
if (NULL == exten_end) {
|
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"%s: Missing extension field. Discarding.",
|
|
|
|
func_name);
|
|
|
|
return PACKET_UNUSEABLE;
|
|
|
|
}
|
2018-02-28 06:23:12 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
/* get size of MAC in cells; can be zero */
|
|
|
|
exten_len = (u_int)(packet_end - exten_end);
|
|
|
|
|
|
|
|
/* deduce action required from remaining length */
|
|
|
|
switch (exten_len) {
|
|
|
|
|
2018-02-28 06:23:12 +00:00
|
|
|
case 0: /* no Legacy MAC */
|
2014-12-20 22:52:39 +00:00
|
|
|
break;
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
case 1: /* crypto NAK */
|
2018-02-28 06:23:12 +00:00
|
|
|
/* Only if the keyID is 0 and there were no EFs */
|
2014-12-20 22:52:39 +00:00
|
|
|
key_id = ntohl(*exten_end);
|
2018-02-28 06:23:12 +00:00
|
|
|
printf("Crypto NAK = 0x%08x from %s\n", key_id, stoa(sender));
|
2013-12-04 21:33:17 +00:00
|
|
|
break;
|
2014-12-20 22:52:39 +00:00
|
|
|
|
|
|
|
case 3: /* key ID + 3DES MAC -- unsupported! */
|
2015-02-05 20:53:33 +00:00
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"%s: Key ID + 3DES MAC is unsupported. Discarding.",
|
|
|
|
func_name);
|
|
|
|
return PACKET_UNUSEABLE;
|
2014-12-20 22:52:39 +00:00
|
|
|
|
|
|
|
case 5: /* key ID + MD5 MAC */
|
|
|
|
case 6: /* key ID + SHA MAC */
|
|
|
|
/*
|
|
|
|
** Look for the key used by the server in the specified
|
|
|
|
** keyfile and if existent, fetch it or else leave the
|
|
|
|
** pointer untouched
|
|
|
|
*/
|
|
|
|
key_id = ntohl(*exten_end);
|
2013-12-04 21:33:17 +00:00
|
|
|
get_key(key_id, &pkt_key);
|
|
|
|
if (!pkt_key) {
|
|
|
|
printf("unrecognized key ID = 0x%08x\n", key_id);
|
|
|
|
break;
|
|
|
|
}
|
2014-12-20 22:52:39 +00:00
|
|
|
/*
|
|
|
|
** Seems like we've got a key with matching keyid.
|
|
|
|
**
|
|
|
|
** Generate a md5sum of the packet with the key from our
|
|
|
|
** keyfile and compare those md5sums.
|
|
|
|
*/
|
|
|
|
mac_size = exten_len << 2;
|
2016-01-21 08:50:56 +00:00
|
|
|
if (!auth_md5(rpkt, pkt_len - mac_size,
|
2014-12-20 22:52:39 +00:00
|
|
|
mac_size - 4, pkt_key)) {
|
|
|
|
is_authentic = FALSE;
|
2013-12-04 21:33:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Yay! Things worked out! */
|
2014-12-20 22:52:39 +00:00
|
|
|
is_authentic = TRUE;
|
|
|
|
TRACE(1, ("sntp %s: packet from %s authenticated using key id %d.\n",
|
|
|
|
func_name, stoa(sender), key_id));
|
2013-12-04 21:33:17 +00:00
|
|
|
break;
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
default:
|
2015-02-05 20:53:33 +00:00
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"%s: Unexpected extension length: %d. Discarding.",
|
|
|
|
func_name, exten_len);
|
|
|
|
return PACKET_UNUSEABLE;
|
2013-12-04 21:33:17 +00:00
|
|
|
}
|
2014-12-20 22:52:39 +00:00
|
|
|
|
|
|
|
switch (is_authentic) {
|
|
|
|
|
|
|
|
case -1: /* unknown */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0: /* not authentic */
|
|
|
|
return SERVER_AUTH_FAIL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: /* authentic */
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* error */
|
|
|
|
break;
|
2013-12-04 21:33:17 +00:00
|
|
|
}
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/* Check for server's ntp version */
|
|
|
|
if (PKT_VERSION(rpkt->li_vn_mode) < NTP_OLDVERSION ||
|
|
|
|
PKT_VERSION(rpkt->li_vn_mode) > NTP_VERSION) {
|
2014-12-20 22:52:39 +00:00
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"%s: Packet shows wrong version (%d)",
|
|
|
|
func_name, PKT_VERSION(rpkt->li_vn_mode));
|
2013-12-04 21:33:17 +00:00
|
|
|
return SERVER_UNUSEABLE;
|
|
|
|
}
|
|
|
|
/* We want a server to sync with */
|
|
|
|
if (PKT_MODE(rpkt->li_vn_mode) != mode &&
|
|
|
|
PKT_MODE(rpkt->li_vn_mode) != MODE_PASSIVE) {
|
2014-12-20 22:52:39 +00:00
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"%s: mode %d stratum %d", func_name,
|
|
|
|
PKT_MODE(rpkt->li_vn_mode), rpkt->stratum);
|
2013-12-04 21:33:17 +00:00
|
|
|
return SERVER_UNUSEABLE;
|
|
|
|
}
|
|
|
|
/* Stratum is unspecified (0) check what's going on */
|
|
|
|
if (STRATUM_PKT_UNSPEC == rpkt->stratum) {
|
|
|
|
char *ref_char;
|
2014-12-20 22:52:39 +00:00
|
|
|
|
|
|
|
TRACE(1, ("%s: Stratum unspecified, going to check for KOD (stratum: %d)\n",
|
|
|
|
func_name, rpkt->stratum));
|
2013-12-04 21:33:17 +00:00
|
|
|
ref_char = (char *) &rpkt->refid;
|
2014-12-20 22:52:39 +00:00
|
|
|
TRACE(1, ("%s: Packet refid: %c%c%c%c\n", func_name,
|
|
|
|
ref_char[0], ref_char[1], ref_char[2], ref_char[3]));
|
2013-12-04 21:33:17 +00:00
|
|
|
/* If it's a KOD packet we'll just use the KOD information */
|
|
|
|
if (ref_char[0] != 'X') {
|
|
|
|
if (strncmp(ref_char, "DENY", 4) == 0)
|
|
|
|
return KOD_DEMOBILIZE;
|
|
|
|
if (strncmp(ref_char, "RSTR", 4) == 0)
|
|
|
|
return KOD_DEMOBILIZE;
|
|
|
|
if (strncmp(ref_char, "RATE", 4) == 0)
|
|
|
|
return KOD_RATE;
|
2014-12-20 22:52:39 +00:00
|
|
|
/*
|
|
|
|
** There are other interesting kiss codes which
|
|
|
|
** might be interesting for authentication.
|
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* If the server is not synced it's not really useable for us */
|
|
|
|
if (LEAP_NOTINSYNC == PKT_LEAP(rpkt->li_vn_mode)) {
|
2014-12-20 22:52:39 +00:00
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"%s: %s not in sync, skipping this server",
|
|
|
|
func_name, stoa(sender));
|
2013-12-04 21:33:17 +00:00
|
|
|
return SERVER_UNUSEABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Decode the org timestamp and make sure we're getting a response
|
|
|
|
* to our last request, but only if we're not in broadcast mode.
|
|
|
|
*/
|
2014-12-20 22:52:39 +00:00
|
|
|
if (MODE_BROADCAST == mode)
|
|
|
|
return pkt_len;
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
if (!L_ISEQU(&rpkt->org, &spkt->xmt)) {
|
|
|
|
NTOHL_FP(&rpkt->org, &resp_org);
|
|
|
|
NTOHL_FP(&spkt->xmt, &sent_xmt);
|
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"%s response org expected to match sent xmt",
|
|
|
|
stoa(sender));
|
|
|
|
msyslog(LOG_ERR, "resp org: %s", prettydate(&resp_org));
|
|
|
|
msyslog(LOG_ERR, "sent xmt: %s", prettydate(&sent_xmt));
|
2013-12-04 21:33:17 +00:00
|
|
|
return PACKET_UNUSEABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pkt_len;
|
|
|
|
}
|