mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-26 20:38:25 +00:00
Compare commits
4
Commits
6d32a7c238
...
60840d0d89
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
60840d0d89 | ||
|
|
298ee549da | ||
|
|
5e4ae64e1b | ||
|
|
c5778aa608 |
No files matched your search
+63
-38
@@ -2061,14 +2061,14 @@ static bool mg_dns_send(struct mg_connection *c, const struct mg_str *name,
|
||||
bool mg_dnsc_init(struct mg_mgr *mgr, struct mg_dns *dnsc);
|
||||
bool mg_dnsc_init(struct mg_mgr *mgr, struct mg_dns *dnsc) {
|
||||
if (dnsc->url == NULL) {
|
||||
mg_error(0, "DNS server URL is NULL. Call mg_mgr_init()");
|
||||
MG_ERROR(("DNS server URL is NULL. Call mg_mgr_init()"));
|
||||
return false;
|
||||
}
|
||||
if (dnsc->c == NULL) {
|
||||
dnsc->c = mg_connect(mgr, dnsc->url, NULL, NULL);
|
||||
if (dnsc->c == NULL) return false;
|
||||
dnsc->c->pfn = dns_cb;
|
||||
}
|
||||
dnsc->c->pfn = dns_cb; // do it always, DHCP-assigned DNS bootstrap needs it
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -8919,6 +8919,8 @@ static bool tcpcsum_ok(void *d, void *t) {
|
||||
0);
|
||||
}
|
||||
|
||||
#define MG_IPISLOCAL(i, a) ((a & i->mask) == (i->ip & i->mask))
|
||||
|
||||
#if MG_ENABLE_IPV6
|
||||
static uint16_t p6csum(void *d, void *p, uint8_t proto, size_t plen) {
|
||||
uint32_t sum;
|
||||
@@ -9027,8 +9029,11 @@ static void onstatechange(struct mg_tcpip_if *ifp) {
|
||||
ifp->is_ip_changed = false;
|
||||
}
|
||||
} else if (ifp->state == MG_TCPIP_STATE_IP) {
|
||||
if (ifp->gw != 0 && ifp->l2type == MG_TCPIP_L2_ETH)
|
||||
mg_tcpip_arp_request(ifp, ifp->gw, NULL); // unsolicited GW ARP request
|
||||
if (ifp->l2type == MG_TCPIP_L2_ETH) {
|
||||
ifp->gw_ready = false; // remove any prior state
|
||||
if (ifp->gw != 0) // send an unsolicited GW ARP request
|
||||
mg_tcpip_arp_request(ifp, ifp->gw, NULL);
|
||||
}
|
||||
} else if (ifp->state == MG_TCPIP_STATE_UP) {
|
||||
srand((unsigned int) mg_millis());
|
||||
}
|
||||
@@ -9279,6 +9284,7 @@ static void rx_icmp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
|
||||
}
|
||||
|
||||
static void setdns4(struct mg_tcpip_if *ifp, uint32_t *ip);
|
||||
static void conndns4(struct mg_tcpip_if *ifp, uint32_t ip);
|
||||
|
||||
static bool dhcp_opt_len_ok(uint8_t len, uint8_t *p, uint8_t *end) {
|
||||
return (len >= 4 && (len & 3) == 0 && p + 6 < end);
|
||||
@@ -9339,16 +9345,15 @@ static void rx_dhcp_client(struct mg_tcpip_if *ifp, struct pkt *pkt) {
|
||||
uint64_t rand;
|
||||
ifp->lease_expire = ifp->now + (uint64_t) lease * 1000;
|
||||
MG_INFO(("Lease: %u sec (%lld)", lease, ifp->lease_expire / 1000));
|
||||
// assume DHCP server = router until ARP resolves
|
||||
memcpy(ifp->gwmac, mg_l2_getaddr(ifp, pkt->l2), sizeof(ifp->gwmac));
|
||||
ifp->gw_ready = true; // NOTE(): actual gw ARP won't retry now
|
||||
if (ifp->ip != ip) ifp->is_ip_changed = true;
|
||||
ifp->ip = ip, ifp->gw = gw, ifp->mask = mask;
|
||||
ifp->state = MG_TCPIP_STATE_IP; // BOUND state
|
||||
mg_random(&rand, sizeof(rand));
|
||||
srand((unsigned int) (rand + mg_millis()));
|
||||
if (ifp->enable_req_dns && dns != 0) {
|
||||
setdns4(ifp, &dns);
|
||||
setdns4(ifp, &dns); // configure the provided DNS
|
||||
if (MG_IPISLOCAL(ifp, dns)) // connect to a local DNS at this point
|
||||
conndns4(ifp, dns);
|
||||
mg_tcpip_call(ifp, MG_TCPIP_EV_DHCP_DNS, &dns);
|
||||
}
|
||||
if (ifp->enable_req_sntp && sntp != 0)
|
||||
@@ -9399,7 +9404,7 @@ static void rx_dhcp_server(struct mg_tcpip_if *ifp, struct pkt *pkt) {
|
||||
res.xid = pkt->dhcp->xid;
|
||||
mac = mg_l2_getaddr(ifp, pkt->l2);
|
||||
if (ifp->enable_get_gateway) {
|
||||
ifp->gw = res.yiaddr; // set gw IP, best-effort gwmac as DHCP server's
|
||||
ifp->gw = res.yiaddr; // set gw IP, and MAC
|
||||
memcpy(ifp->gwmac, mac, sizeof(ifp->gwmac));
|
||||
}
|
||||
tx_udp4(ifp, mac, ifp->ip, mg_htons(67), op == 1 ? ~0U : res.yiaddr,
|
||||
@@ -9656,6 +9661,7 @@ static void rx_ndp_ra(struct mg_tcpip_if *ifp, struct pkt *pkt) {
|
||||
if (gotprefix && !fill_global(ifp, prefix, prefix_len)) return;
|
||||
if (changed) ifp->is_ip6_changed = true;
|
||||
ifp->gw6[0] = pkt->ip6->src[0], ifp->gw6[1] = pkt->ip6->src[1];
|
||||
ifp->gw6_ready = false; // Remove any prior state
|
||||
if (gotl2addr) memcpy(ifp->gw6mac, l2, sizeof(ifp->gw6mac));
|
||||
if (gotl2addr || ifp->l2type == MG_TCPIP_L2_PPP ||
|
||||
ifp->l2type == MG_TCPIP_L2_PPPoE) {
|
||||
@@ -9670,10 +9676,8 @@ static void rx_ndp_ra(struct mg_tcpip_if *ifp, struct pkt *pkt) {
|
||||
mtu));
|
||||
ifp->mtu = (uint16_t) mtu;
|
||||
}
|
||||
if (ifp->state6 != MG_TCPIP_STATE_READY) {
|
||||
tx_ndp_ns(ifp, ifp->gw6, NULL); // unsolicited GW hwaddr resolution
|
||||
if (ifp->state6 != MG_TCPIP_STATE_READY)
|
||||
ifp->state6 = MG_TCPIP_STATE_IP;
|
||||
}
|
||||
onstate6change(ifp);
|
||||
}
|
||||
}
|
||||
@@ -9789,7 +9793,7 @@ static uint8_t *get_return_l2addr(struct mg_tcpip_if *ifp, struct mg_addr *rem,
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
if (ifp->ip != 0 && ((rem->addr.ip4 & ifp->mask) == (ifp->ip & ifp->mask)))
|
||||
if (ifp->ip != 0 && MG_IPISLOCAL(ifp, rem->addr.ip4))
|
||||
return mg_l2_getaddr(ifp, pkt->l2); // same LAN, get from frame
|
||||
if (ifp->gw_ready) // use the router
|
||||
return ifp->gwmac; // ignore source address in frame
|
||||
@@ -10741,7 +10745,7 @@ static void mg_tcpip_poll(struct mg_tcpip_if *ifp, uint64_t now) {
|
||||
}
|
||||
// Handle gw ARP request timeout, order is important
|
||||
if (expired_1000ms && ifp->state == MG_TCPIP_STATE_IP) {
|
||||
ifp->state = MG_TCPIP_STATE_READY; // keep best-effort MAC or poison mark
|
||||
ifp->state = MG_TCPIP_STATE_READY; // gw MAC not resolved, we'll retry
|
||||
onstatechange(ifp);
|
||||
}
|
||||
if (expired_1000ms && ifp->state == MG_TCPIP_STATE_READY && !ifp->gw_ready &&
|
||||
@@ -10750,10 +10754,10 @@ static void mg_tcpip_poll(struct mg_tcpip_if *ifp, uint64_t now) {
|
||||
#if MG_ENABLE_IPV6
|
||||
// Handle gw NS/NA req/resp timeout, order is important
|
||||
if (expired_1000ms && ifp->state6 == MG_TCPIP_STATE_IP) {
|
||||
ifp->state6 = MG_TCPIP_STATE_READY; // keep best-effort MAC or poison mark
|
||||
ifp->state6 = MG_TCPIP_STATE_READY; // gw6 MAC not resolved, we'll retry
|
||||
onstate6change(ifp);
|
||||
}
|
||||
if (expired_1000ms && ifp->state == MG_TCPIP_STATE_READY && !ifp->gw6_ready &&
|
||||
if (expired_1000ms && ifp->state6 == MG_TCPIP_STATE_READY && !ifp->gw6_ready &&
|
||||
(ifp->gw6[0] != 0 || ifp->gw6[1] != 0))
|
||||
tx_ndp_ns(ifp, ifp->gw6, NULL); // retry GW hwaddr resolution
|
||||
#endif
|
||||
@@ -10969,7 +10973,7 @@ void mg_connect_resolved(struct mg_connection *c) {
|
||||
#endif
|
||||
} else {
|
||||
uint32_t rem_ip = c->rem.addr.ip4;
|
||||
if (ifp->ip && ((rem_ip & ifp->mask) == (ifp->ip & ifp->mask)) &&
|
||||
if (ifp->ip && MG_IPISLOCAL(ifp, rem_ip) &&
|
||||
rem_ip != ifp->gw) { // skip if gw (onstatechange -> ARP)
|
||||
// If we're in the same LAN, fire an ARP lookup.
|
||||
MG_DEBUG(("%lu ARP lookup...", c->id));
|
||||
@@ -11114,10 +11118,31 @@ static void setdns4(struct mg_tcpip_if *ifp, uint32_t *ip) {
|
||||
MG_DEBUG(("Set DNS URL to %s", dnsc->url));
|
||||
if (ifp->mgr->use_dns6) return;
|
||||
if (dnsc->c != NULL) mg_close_conn(dnsc->c);
|
||||
if (!mg_dnsc_init(ifp->mgr, dnsc)) // create DNS connection
|
||||
MG_ERROR(("DNS connection creation failed"));
|
||||
}
|
||||
|
||||
static void conndns4(struct mg_tcpip_if *ifp, uint32_t ip) {
|
||||
struct mg_connection *c;
|
||||
struct mg_dns *dnsc = &ifp->mgr->dns4;
|
||||
if (ifp->mgr->use_dns6) return;
|
||||
if ((c = mg_alloc_conn(ifp->mgr)) == NULL) {
|
||||
MG_ERROR(("OOM, DNS connection creation failed"));
|
||||
return;
|
||||
} // mg_connect_svc() would block us, so we bypass it here
|
||||
LIST_ADD_HEAD(struct mg_connection, &ifp->mgr->conns, c);
|
||||
c->is_udp = true;
|
||||
c->fd = (void *) (size_t) MG_INVALID_SOCKET;
|
||||
c->is_client = true;
|
||||
dnsc->c = c;
|
||||
mg_call(c, MG_EV_OPEN, (void *) dnsc->url);
|
||||
MG_DEBUG(("%lu %ld %s", c->id, c->fd, dnsc->url));
|
||||
c->rem.addr.ip4 = ip;
|
||||
c->rem.port = mg_htons(53);
|
||||
mg_connect_resolved(c);
|
||||
mg_dnsc_init(ifp->mgr, dnsc); // will fill c->pfn = dns_cb
|
||||
}
|
||||
|
||||
|
||||
|
||||
void mg_tcpip_mapip(struct mg_connection *c, struct mg_addr *ip) {
|
||||
struct connstate *s = (struct connstate *) (c + 1);
|
||||
uint8_t *l2addr = tcpip_mapip(c->mgr->ifp, ip);
|
||||
@@ -17564,7 +17589,7 @@ static bool mg_tls_encrypt(struct mg_connection *c, const uint8_t *msg,
|
||||
c->is_client ? tls->enc.client_write_iv : tls->enc.server_write_iv;
|
||||
|
||||
if (msgsz > 16384) {
|
||||
MG_ERROR(("msg longer than recordsz"));
|
||||
mg_error(c, "msg longer than recordsz");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -18225,19 +18250,19 @@ static bool mg_tls_send_cert_verify(struct mg_connection *c, bool is_client) {
|
||||
|
||||
// Check bounds
|
||||
if (emlen > sizeof(em) || verifysz > sizeof(verify)) {
|
||||
MG_ERROR(("RSA key too large for static buffers"));
|
||||
mg_error(c, "RSA key too large for static buffers");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!mg_tls_pss_encode(hash, sizeof(hash), &rn, em)) {
|
||||
MG_ERROR(("Failed PSS encode"));
|
||||
mg_error(c, "Failed PSS encode");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Validate PSS encoded message format
|
||||
if (em[emlen - 1] != 0xbc) {
|
||||
MG_ERROR(("Invalid PSS encoding: last byte is 0x%02x, expected 0xbc",
|
||||
em[emlen - 1]));
|
||||
mg_error(c, "Invalid PSS encoding: last byte is 0x%02x, expected 0xbc",
|
||||
em[emlen - 1]);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -18250,7 +18275,7 @@ static bool mg_tls_send_cert_verify(struct mg_connection *c, bool is_client) {
|
||||
// Sign directly into the verify buffer (verify + 8 = signature location)
|
||||
memset(verify + 8, 0, emlen); // Initialize signature area
|
||||
if (!mg_tls_rsa_sign(tls, em, emlen, verify + 8)) {
|
||||
MG_ERROR(("Failed RSA sign"));
|
||||
mg_error(c, "Failed RSA sign");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -18333,8 +18358,6 @@ static bool mg_tls_client_send_hello(struct mg_connection *c) {
|
||||
struct tls_data *tls = (struct tls_data *) c->tls;
|
||||
struct mg_iobuf *wio = &tls->send;
|
||||
|
||||
uint8_t p256_pub[64]; // X || Y, sent as an uncompressed point 0x04 || X || Y
|
||||
|
||||
// - "signature algorithms we actually support", see above
|
||||
// uint8_t secp256r1_sig_algs[]
|
||||
// - all popular signature algorithms (if we don't care about verification)
|
||||
@@ -18400,18 +18423,20 @@ static bool mg_tls_client_send_hello(struct mg_connection *c) {
|
||||
MG_STORE_BE16(server_name_ext + 7, hostnamesz);
|
||||
}
|
||||
|
||||
// calculate keyshare
|
||||
if (!mg_p256_make_key(p256_pub, tls->p256_cli)) {
|
||||
// calc+fill keyshare; X || Y, sent as an uncompressed point 0x04 || X || Y
|
||||
if (!mg_p256_make_key(msg_client_hello + 95, tls->p256_cli)) {
|
||||
mg_error(c, "P-256 key");
|
||||
return false; // Do not send uninitialised p256_pub
|
||||
return false;
|
||||
}
|
||||
|
||||
// fill in the gaps: random + session ID + keyshare
|
||||
if (!mg_random(tls->session_id, sizeof(tls->session_id))) mg_error(c, "RNG");
|
||||
if (!mg_random(tls->random, sizeof(tls->random))) mg_error(c, "RNG");
|
||||
// fill in the gaps: random + session ID
|
||||
if (!mg_random(tls->session_id, sizeof(tls->session_id)) ||
|
||||
!mg_random(tls->random, sizeof(tls->random))) {
|
||||
mg_error(c, "RNG");
|
||||
return false;
|
||||
}
|
||||
memmove(msg_client_hello + 11, tls->random, sizeof(tls->random));
|
||||
memmove(msg_client_hello + 44, tls->session_id, sizeof(tls->session_id));
|
||||
memmove(msg_client_hello + 95, p256_pub, sizeof(p256_pub));
|
||||
|
||||
// client hello message
|
||||
if (mg_iobuf_add(wio, wio->len, msg_client_hello, sizeof(msg_client_hello)) ==
|
||||
@@ -19192,8 +19217,8 @@ static int mg_tls_recv_cert_verify(struct mg_connection *c) {
|
||||
// 0809 = rsa_pss_pss_sha256
|
||||
// 080A = rsa_pss_pss_sha384
|
||||
// 080B = rsa_pss_pss_sha512
|
||||
MG_ERROR(("unsupported certverify signature scheme: %x of %d bytes",
|
||||
sigalg, siglen));
|
||||
mg_error(c, "unsupported certverify signature scheme: %x of %d bytes",
|
||||
sigalg, siglen);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -19355,8 +19380,8 @@ void mg_tls_handshake(struct mg_connection *c) {
|
||||
MG_DEBUG(("%lu TLS handshake done in %lu ms: %lu ms CPU, %lu ms peer wait",
|
||||
c->id, total, cpu, total > cpu ? total - cpu : 0));
|
||||
}
|
||||
if (!res) {
|
||||
mg_error(c, "TLS OOM");
|
||||
if (!res) { // already mg_error()ed at handshake helper ?
|
||||
if (!c->is_closing) mg_error(c, "TLS OOM"); // No, do it
|
||||
return;
|
||||
}
|
||||
while (tls->send.len > 0 &&
|
||||
|
||||
@@ -263,14 +263,14 @@ static bool mg_dns_send(struct mg_connection *c, const struct mg_str *name,
|
||||
bool mg_dnsc_init(struct mg_mgr *mgr, struct mg_dns *dnsc);
|
||||
bool mg_dnsc_init(struct mg_mgr *mgr, struct mg_dns *dnsc) {
|
||||
if (dnsc->url == NULL) {
|
||||
mg_error(0, "DNS server URL is NULL. Call mg_mgr_init()");
|
||||
MG_ERROR(("DNS server URL is NULL. Call mg_mgr_init()"));
|
||||
return false;
|
||||
}
|
||||
if (dnsc->c == NULL) {
|
||||
dnsc->c = mg_connect(mgr, dnsc->url, NULL, NULL);
|
||||
if (dnsc->c == NULL) return false;
|
||||
dnsc->c->pfn = dns_cb;
|
||||
}
|
||||
dnsc->c->pfn = dns_cb; // do it always, DHCP-assigned DNS bootstrap needs it
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
+42
-17
@@ -313,6 +313,8 @@ static bool tcpcsum_ok(void *d, void *t) {
|
||||
0);
|
||||
}
|
||||
|
||||
#define MG_IPISLOCAL(i, a) ((a & i->mask) == (i->ip & i->mask))
|
||||
|
||||
#if MG_ENABLE_IPV6
|
||||
static uint16_t p6csum(void *d, void *p, uint8_t proto, size_t plen) {
|
||||
uint32_t sum;
|
||||
@@ -421,8 +423,11 @@ static void onstatechange(struct mg_tcpip_if *ifp) {
|
||||
ifp->is_ip_changed = false;
|
||||
}
|
||||
} else if (ifp->state == MG_TCPIP_STATE_IP) {
|
||||
if (ifp->gw != 0 && ifp->l2type == MG_TCPIP_L2_ETH)
|
||||
mg_tcpip_arp_request(ifp, ifp->gw, NULL); // unsolicited GW ARP request
|
||||
if (ifp->l2type == MG_TCPIP_L2_ETH) {
|
||||
ifp->gw_ready = false; // remove any prior state
|
||||
if (ifp->gw != 0) // send an unsolicited GW ARP request
|
||||
mg_tcpip_arp_request(ifp, ifp->gw, NULL);
|
||||
}
|
||||
} else if (ifp->state == MG_TCPIP_STATE_UP) {
|
||||
srand((unsigned int) mg_millis());
|
||||
}
|
||||
@@ -673,6 +678,7 @@ static void rx_icmp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
|
||||
}
|
||||
|
||||
static void setdns4(struct mg_tcpip_if *ifp, uint32_t *ip);
|
||||
static void conndns4(struct mg_tcpip_if *ifp, uint32_t ip);
|
||||
|
||||
static bool dhcp_opt_len_ok(uint8_t len, uint8_t *p, uint8_t *end) {
|
||||
return (len >= 4 && (len & 3) == 0 && p + 6 < end);
|
||||
@@ -733,16 +739,15 @@ static void rx_dhcp_client(struct mg_tcpip_if *ifp, struct pkt *pkt) {
|
||||
uint64_t rand;
|
||||
ifp->lease_expire = ifp->now + (uint64_t) lease * 1000;
|
||||
MG_INFO(("Lease: %u sec (%lld)", lease, ifp->lease_expire / 1000));
|
||||
// assume DHCP server = router until ARP resolves
|
||||
memcpy(ifp->gwmac, mg_l2_getaddr(ifp, pkt->l2), sizeof(ifp->gwmac));
|
||||
ifp->gw_ready = true; // NOTE(): actual gw ARP won't retry now
|
||||
if (ifp->ip != ip) ifp->is_ip_changed = true;
|
||||
ifp->ip = ip, ifp->gw = gw, ifp->mask = mask;
|
||||
ifp->state = MG_TCPIP_STATE_IP; // BOUND state
|
||||
mg_random(&rand, sizeof(rand));
|
||||
srand((unsigned int) (rand + mg_millis()));
|
||||
if (ifp->enable_req_dns && dns != 0) {
|
||||
setdns4(ifp, &dns);
|
||||
setdns4(ifp, &dns); // configure the provided DNS
|
||||
if (MG_IPISLOCAL(ifp, dns)) // connect to a local DNS at this point
|
||||
conndns4(ifp, dns);
|
||||
mg_tcpip_call(ifp, MG_TCPIP_EV_DHCP_DNS, &dns);
|
||||
}
|
||||
if (ifp->enable_req_sntp && sntp != 0)
|
||||
@@ -793,7 +798,7 @@ static void rx_dhcp_server(struct mg_tcpip_if *ifp, struct pkt *pkt) {
|
||||
res.xid = pkt->dhcp->xid;
|
||||
mac = mg_l2_getaddr(ifp, pkt->l2);
|
||||
if (ifp->enable_get_gateway) {
|
||||
ifp->gw = res.yiaddr; // set gw IP, best-effort gwmac as DHCP server's
|
||||
ifp->gw = res.yiaddr; // set gw IP, and MAC
|
||||
memcpy(ifp->gwmac, mac, sizeof(ifp->gwmac));
|
||||
}
|
||||
tx_udp4(ifp, mac, ifp->ip, mg_htons(67), op == 1 ? ~0U : res.yiaddr,
|
||||
@@ -1050,6 +1055,7 @@ static void rx_ndp_ra(struct mg_tcpip_if *ifp, struct pkt *pkt) {
|
||||
if (gotprefix && !fill_global(ifp, prefix, prefix_len)) return;
|
||||
if (changed) ifp->is_ip6_changed = true;
|
||||
ifp->gw6[0] = pkt->ip6->src[0], ifp->gw6[1] = pkt->ip6->src[1];
|
||||
ifp->gw6_ready = false; // Remove any prior state
|
||||
if (gotl2addr) memcpy(ifp->gw6mac, l2, sizeof(ifp->gw6mac));
|
||||
if (gotl2addr || ifp->l2type == MG_TCPIP_L2_PPP ||
|
||||
ifp->l2type == MG_TCPIP_L2_PPPoE) {
|
||||
@@ -1064,10 +1070,8 @@ static void rx_ndp_ra(struct mg_tcpip_if *ifp, struct pkt *pkt) {
|
||||
mtu));
|
||||
ifp->mtu = (uint16_t) mtu;
|
||||
}
|
||||
if (ifp->state6 != MG_TCPIP_STATE_READY) {
|
||||
tx_ndp_ns(ifp, ifp->gw6, NULL); // unsolicited GW hwaddr resolution
|
||||
if (ifp->state6 != MG_TCPIP_STATE_READY)
|
||||
ifp->state6 = MG_TCPIP_STATE_IP;
|
||||
}
|
||||
onstate6change(ifp);
|
||||
}
|
||||
}
|
||||
@@ -1183,7 +1187,7 @@ static uint8_t *get_return_l2addr(struct mg_tcpip_if *ifp, struct mg_addr *rem,
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
if (ifp->ip != 0 && ((rem->addr.ip4 & ifp->mask) == (ifp->ip & ifp->mask)))
|
||||
if (ifp->ip != 0 && MG_IPISLOCAL(ifp, rem->addr.ip4))
|
||||
return mg_l2_getaddr(ifp, pkt->l2); // same LAN, get from frame
|
||||
if (ifp->gw_ready) // use the router
|
||||
return ifp->gwmac; // ignore source address in frame
|
||||
@@ -2135,7 +2139,7 @@ static void mg_tcpip_poll(struct mg_tcpip_if *ifp, uint64_t now) {
|
||||
}
|
||||
// Handle gw ARP request timeout, order is important
|
||||
if (expired_1000ms && ifp->state == MG_TCPIP_STATE_IP) {
|
||||
ifp->state = MG_TCPIP_STATE_READY; // keep best-effort MAC or poison mark
|
||||
ifp->state = MG_TCPIP_STATE_READY; // gw MAC not resolved, we'll retry
|
||||
onstatechange(ifp);
|
||||
}
|
||||
if (expired_1000ms && ifp->state == MG_TCPIP_STATE_READY && !ifp->gw_ready &&
|
||||
@@ -2144,10 +2148,10 @@ static void mg_tcpip_poll(struct mg_tcpip_if *ifp, uint64_t now) {
|
||||
#if MG_ENABLE_IPV6
|
||||
// Handle gw NS/NA req/resp timeout, order is important
|
||||
if (expired_1000ms && ifp->state6 == MG_TCPIP_STATE_IP) {
|
||||
ifp->state6 = MG_TCPIP_STATE_READY; // keep best-effort MAC or poison mark
|
||||
ifp->state6 = MG_TCPIP_STATE_READY; // gw6 MAC not resolved, we'll retry
|
||||
onstate6change(ifp);
|
||||
}
|
||||
if (expired_1000ms && ifp->state == MG_TCPIP_STATE_READY && !ifp->gw6_ready &&
|
||||
if (expired_1000ms && ifp->state6 == MG_TCPIP_STATE_READY && !ifp->gw6_ready &&
|
||||
(ifp->gw6[0] != 0 || ifp->gw6[1] != 0))
|
||||
tx_ndp_ns(ifp, ifp->gw6, NULL); // retry GW hwaddr resolution
|
||||
#endif
|
||||
@@ -2363,7 +2367,7 @@ void mg_connect_resolved(struct mg_connection *c) {
|
||||
#endif
|
||||
} else {
|
||||
uint32_t rem_ip = c->rem.addr.ip4;
|
||||
if (ifp->ip && ((rem_ip & ifp->mask) == (ifp->ip & ifp->mask)) &&
|
||||
if (ifp->ip && MG_IPISLOCAL(ifp, rem_ip) &&
|
||||
rem_ip != ifp->gw) { // skip if gw (onstatechange -> ARP)
|
||||
// If we're in the same LAN, fire an ARP lookup.
|
||||
MG_DEBUG(("%lu ARP lookup...", c->id));
|
||||
@@ -2508,10 +2512,31 @@ static void setdns4(struct mg_tcpip_if *ifp, uint32_t *ip) {
|
||||
MG_DEBUG(("Set DNS URL to %s", dnsc->url));
|
||||
if (ifp->mgr->use_dns6) return;
|
||||
if (dnsc->c != NULL) mg_close_conn(dnsc->c);
|
||||
if (!mg_dnsc_init(ifp->mgr, dnsc)) // create DNS connection
|
||||
MG_ERROR(("DNS connection creation failed"));
|
||||
}
|
||||
|
||||
static void conndns4(struct mg_tcpip_if *ifp, uint32_t ip) {
|
||||
struct mg_connection *c;
|
||||
struct mg_dns *dnsc = &ifp->mgr->dns4;
|
||||
if (ifp->mgr->use_dns6) return;
|
||||
if ((c = mg_alloc_conn(ifp->mgr)) == NULL) {
|
||||
MG_ERROR(("OOM, DNS connection creation failed"));
|
||||
return;
|
||||
} // mg_connect_svc() would block us, so we bypass it here
|
||||
LIST_ADD_HEAD(struct mg_connection, &ifp->mgr->conns, c);
|
||||
c->is_udp = true;
|
||||
c->fd = (void *) (size_t) MG_INVALID_SOCKET;
|
||||
c->is_client = true;
|
||||
dnsc->c = c;
|
||||
mg_call(c, MG_EV_OPEN, (void *) dnsc->url);
|
||||
MG_DEBUG(("%lu %ld %s", c->id, c->fd, dnsc->url));
|
||||
c->rem.addr.ip4 = ip;
|
||||
c->rem.port = mg_htons(53);
|
||||
mg_connect_resolved(c);
|
||||
mg_dnsc_init(ifp->mgr, dnsc); // will fill c->pfn = dns_cb
|
||||
}
|
||||
|
||||
|
||||
|
||||
void mg_tcpip_mapip(struct mg_connection *c, struct mg_addr *ip) {
|
||||
struct connstate *s = (struct connstate *) (c + 1);
|
||||
uint8_t *l2addr = tcpip_mapip(c->mgr->ifp, ip);
|
||||
|
||||
+19
-19
@@ -469,7 +469,7 @@ static bool mg_tls_encrypt(struct mg_connection *c, const uint8_t *msg,
|
||||
c->is_client ? tls->enc.client_write_iv : tls->enc.server_write_iv;
|
||||
|
||||
if (msgsz > 16384) {
|
||||
MG_ERROR(("msg longer than recordsz"));
|
||||
mg_error(c, "msg longer than recordsz");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1130,19 +1130,19 @@ static bool mg_tls_send_cert_verify(struct mg_connection *c, bool is_client) {
|
||||
|
||||
// Check bounds
|
||||
if (emlen > sizeof(em) || verifysz > sizeof(verify)) {
|
||||
MG_ERROR(("RSA key too large for static buffers"));
|
||||
mg_error(c, "RSA key too large for static buffers");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!mg_tls_pss_encode(hash, sizeof(hash), &rn, em)) {
|
||||
MG_ERROR(("Failed PSS encode"));
|
||||
mg_error(c, "Failed PSS encode");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Validate PSS encoded message format
|
||||
if (em[emlen - 1] != 0xbc) {
|
||||
MG_ERROR(("Invalid PSS encoding: last byte is 0x%02x, expected 0xbc",
|
||||
em[emlen - 1]));
|
||||
mg_error(c, "Invalid PSS encoding: last byte is 0x%02x, expected 0xbc",
|
||||
em[emlen - 1]);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1155,7 +1155,7 @@ static bool mg_tls_send_cert_verify(struct mg_connection *c, bool is_client) {
|
||||
// Sign directly into the verify buffer (verify + 8 = signature location)
|
||||
memset(verify + 8, 0, emlen); // Initialize signature area
|
||||
if (!mg_tls_rsa_sign(tls, em, emlen, verify + 8)) {
|
||||
MG_ERROR(("Failed RSA sign"));
|
||||
mg_error(c, "Failed RSA sign");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1238,8 +1238,6 @@ static bool mg_tls_client_send_hello(struct mg_connection *c) {
|
||||
struct tls_data *tls = (struct tls_data *) c->tls;
|
||||
struct mg_iobuf *wio = &tls->send;
|
||||
|
||||
uint8_t p256_pub[64]; // X || Y, sent as an uncompressed point 0x04 || X || Y
|
||||
|
||||
// - "signature algorithms we actually support", see above
|
||||
// uint8_t secp256r1_sig_algs[]
|
||||
// - all popular signature algorithms (if we don't care about verification)
|
||||
@@ -1305,18 +1303,20 @@ static bool mg_tls_client_send_hello(struct mg_connection *c) {
|
||||
MG_STORE_BE16(server_name_ext + 7, hostnamesz);
|
||||
}
|
||||
|
||||
// calculate keyshare
|
||||
if (!mg_p256_make_key(p256_pub, tls->p256_cli)) {
|
||||
// calc+fill keyshare; X || Y, sent as an uncompressed point 0x04 || X || Y
|
||||
if (!mg_p256_make_key(msg_client_hello + 95, tls->p256_cli)) {
|
||||
mg_error(c, "P-256 key");
|
||||
return false; // Do not send uninitialised p256_pub
|
||||
return false;
|
||||
}
|
||||
|
||||
// fill in the gaps: random + session ID + keyshare
|
||||
if (!mg_random(tls->session_id, sizeof(tls->session_id))) mg_error(c, "RNG");
|
||||
if (!mg_random(tls->random, sizeof(tls->random))) mg_error(c, "RNG");
|
||||
// fill in the gaps: random + session ID
|
||||
if (!mg_random(tls->session_id, sizeof(tls->session_id)) ||
|
||||
!mg_random(tls->random, sizeof(tls->random))) {
|
||||
mg_error(c, "RNG");
|
||||
return false;
|
||||
}
|
||||
memmove(msg_client_hello + 11, tls->random, sizeof(tls->random));
|
||||
memmove(msg_client_hello + 44, tls->session_id, sizeof(tls->session_id));
|
||||
memmove(msg_client_hello + 95, p256_pub, sizeof(p256_pub));
|
||||
|
||||
// client hello message
|
||||
if (mg_iobuf_add(wio, wio->len, msg_client_hello, sizeof(msg_client_hello)) ==
|
||||
@@ -2097,8 +2097,8 @@ static int mg_tls_recv_cert_verify(struct mg_connection *c) {
|
||||
// 0809 = rsa_pss_pss_sha256
|
||||
// 080A = rsa_pss_pss_sha384
|
||||
// 080B = rsa_pss_pss_sha512
|
||||
MG_ERROR(("unsupported certverify signature scheme: %x of %d bytes",
|
||||
sigalg, siglen));
|
||||
mg_error(c, "unsupported certverify signature scheme: %x of %d bytes",
|
||||
sigalg, siglen);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -2260,8 +2260,8 @@ void mg_tls_handshake(struct mg_connection *c) {
|
||||
MG_DEBUG(("%lu TLS handshake done in %lu ms: %lu ms CPU, %lu ms peer wait",
|
||||
c->id, total, cpu, total > cpu ? total - cpu : 0));
|
||||
}
|
||||
if (!res) {
|
||||
mg_error(c, "TLS OOM");
|
||||
if (!res) { // already mg_error()ed at handshake helper ?
|
||||
if (!c->is_closing) mg_error(c, "TLS OOM"); // No, do it
|
||||
return;
|
||||
}
|
||||
while (tls->send.len > 0 &&
|
||||
|
||||
Reference in new issue
Block a user