mirror of
https://github.com/cesanta/mongoose.git
synced 2026-12-04 21:58:22 +00:00
Compare commits
1
Commits
link-local
..
priol2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
63ba221588 |
No files matched your search
+63
-62
@@ -6134,7 +6134,8 @@ size_t mg_jwt_verify_es256(struct mg_str jwt, const struct mg_jwt_opts *opts,
|
|||||||
void mg_l2_init(struct mg_tcpip_if *ifp);
|
void mg_l2_init(struct mg_tcpip_if *ifp);
|
||||||
bool mg_l2_poll(struct mg_tcpip_if *ifp, bool expired_1000ms);
|
bool mg_l2_poll(struct mg_tcpip_if *ifp, bool expired_1000ms);
|
||||||
uint8_t *mg_l2_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
uint8_t *mg_l2_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
||||||
uint8_t *src, uint8_t *dst, uint8_t *frame);
|
uint8_t *src, uint8_t *dst, uint8_t *frame,
|
||||||
|
uint8_t dscp);
|
||||||
size_t mg_l2_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *cur);
|
size_t mg_l2_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *cur);
|
||||||
bool mg_l2_rx(struct mg_tcpip_if *ifp, enum mg_l2proto *proto,
|
bool mg_l2_rx(struct mg_tcpip_if *ifp, enum mg_l2proto *proto,
|
||||||
struct mg_str *pay, struct mg_str *raw);
|
struct mg_str *pay, struct mg_str *raw);
|
||||||
@@ -6153,7 +6154,7 @@ size_t mg_l2_driver_output(struct mg_tcpip_if *ifp, size_t len);
|
|||||||
// clang-format off
|
// clang-format off
|
||||||
extern void mg_l2_eth_init(struct mg_tcpip_if *);
|
extern void mg_l2_eth_init(struct mg_tcpip_if *);
|
||||||
extern bool mg_l2_eth_poll(struct mg_tcpip_if *, bool);
|
extern bool mg_l2_eth_poll(struct mg_tcpip_if *, bool);
|
||||||
extern uint8_t *mg_l2_eth_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *);
|
extern uint8_t *mg_l2_eth_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *, uint8_t);
|
||||||
extern size_t mg_l2_eth_trailer(struct mg_tcpip_if *, size_t, uint8_t *);
|
extern size_t mg_l2_eth_trailer(struct mg_tcpip_if *, size_t, uint8_t *);
|
||||||
extern bool mg_l2_eth_rx(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
|
extern bool mg_l2_eth_rx(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
|
||||||
extern struct mg_l2addr *mg_l2_eth_getaddr(struct mg_tcpip_if *, uint8_t *);
|
extern struct mg_l2addr *mg_l2_eth_getaddr(struct mg_tcpip_if *, uint8_t *);
|
||||||
@@ -6166,7 +6167,7 @@ extern uint8_t mg_l2_eth_ip6put(struct mg_l2addr *, uint8_t *);
|
|||||||
|
|
||||||
extern void mg_l2_ppp_init(struct mg_tcpip_if *);
|
extern void mg_l2_ppp_init(struct mg_tcpip_if *);
|
||||||
extern bool mg_l2_ppp_poll(struct mg_tcpip_if *, bool);
|
extern bool mg_l2_ppp_poll(struct mg_tcpip_if *, bool);
|
||||||
extern uint8_t *mg_l2_ppp_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *);
|
extern uint8_t *mg_l2_ppp_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *, uint8_t);
|
||||||
extern size_t mg_l2_ppp_trailer(struct mg_tcpip_if *, size_t, uint8_t *);
|
extern size_t mg_l2_ppp_trailer(struct mg_tcpip_if *, size_t, uint8_t *);
|
||||||
extern bool mg_l2_ppp_rx(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
|
extern bool mg_l2_ppp_rx(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
|
||||||
extern struct mg_l2addr *mg_l2_ppp_getaddr(struct mg_tcpip_if *, uint8_t *);
|
extern struct mg_l2addr *mg_l2_ppp_getaddr(struct mg_tcpip_if *, uint8_t *);
|
||||||
@@ -6178,13 +6179,13 @@ extern uint8_t mg_l2_ppp_ip6put(struct mg_l2addr *, uint8_t *);
|
|||||||
#endif
|
#endif
|
||||||
extern void mg_l2_pppoe_init(struct mg_tcpip_if *);
|
extern void mg_l2_pppoe_init(struct mg_tcpip_if *);
|
||||||
extern bool mg_l2_pppoe_poll(struct mg_tcpip_if *, bool);
|
extern bool mg_l2_pppoe_poll(struct mg_tcpip_if *, bool);
|
||||||
extern uint8_t *mg_l2_pppoe_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *);
|
extern uint8_t *mg_l2_pppoe_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *, uint8_t);
|
||||||
extern size_t mg_l2_pppoe_trailer(struct mg_tcpip_if *, size_t, uint8_t *);
|
extern size_t mg_l2_pppoe_trailer(struct mg_tcpip_if *, size_t, uint8_t *);
|
||||||
extern bool mg_l2_pppoe_rx(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
|
extern bool mg_l2_pppoe_rx(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
|
||||||
|
|
||||||
typedef void (*l2_init_fn)(struct mg_tcpip_if *);
|
typedef void (*l2_init_fn)(struct mg_tcpip_if *);
|
||||||
typedef bool (*l2_poll_fn)(struct mg_tcpip_if *, bool);
|
typedef bool (*l2_poll_fn)(struct mg_tcpip_if *, bool);
|
||||||
typedef uint8_t *((*l2_header_fn)(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *));
|
typedef uint8_t *((*l2_header_fn)(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *, uint8_t));
|
||||||
typedef size_t (*l2_trailer_fn)(struct mg_tcpip_if *, size_t, uint8_t *);
|
typedef size_t (*l2_trailer_fn)(struct mg_tcpip_if *, size_t, uint8_t *);
|
||||||
typedef bool (*l2_rx_fn)(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
|
typedef bool (*l2_rx_fn)(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
|
||||||
typedef struct mg_l2addr (*(*l2_getaddr_fn)(struct mg_tcpip_if *, uint8_t *));
|
typedef struct mg_l2addr (*(*l2_getaddr_fn)(struct mg_tcpip_if *, uint8_t *));
|
||||||
@@ -6227,9 +6228,10 @@ bool mg_l2_poll(struct mg_tcpip_if *ifp, bool expired_1000ms) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
uint8_t *mg_l2_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
uint8_t *mg_l2_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
||||||
uint8_t *src, uint8_t *dst, uint8_t *frame) {
|
uint8_t *src, uint8_t *dst, uint8_t *frame,
|
||||||
|
uint8_t dscp) {
|
||||||
return l2_header[ifp->l2type](ifp, proto, (struct mg_l2addr *) src,
|
return l2_header[ifp->l2type](ifp, proto, (struct mg_l2addr *) src,
|
||||||
(struct mg_l2addr *) dst, frame);
|
(struct mg_l2addr *) dst, frame, dscp);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t mg_l2_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *frame) {
|
size_t mg_l2_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *frame) {
|
||||||
@@ -6343,7 +6345,7 @@ bool mg_l2_eth_poll(struct mg_tcpip_if *ifp, bool expired_1000ms) {
|
|||||||
|
|
||||||
uint8_t *mg_l2_eth_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
uint8_t *mg_l2_eth_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
||||||
struct mg_l2addr *src, struct mg_l2addr *dst,
|
struct mg_l2addr *src, struct mg_l2addr *dst,
|
||||||
uint8_t *frame) {
|
uint8_t *frame, uint8_t dscp) {
|
||||||
struct eth_data *d = &ifp->l2data.eth;
|
struct eth_data *d = &ifp->l2data.eth;
|
||||||
struct eth *eth = (struct eth *) frame;
|
struct eth *eth = (struct eth *) frame;
|
||||||
uint8_t *hlp;
|
uint8_t *hlp;
|
||||||
@@ -6355,7 +6357,8 @@ uint8_t *mg_l2_eth_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
|||||||
} else { // Add 802.1Q tag
|
} else { // Add 802.1Q tag
|
||||||
struct qtag *qtag = (struct qtag *) ð->type;
|
struct qtag *qtag = (struct qtag *) ð->type;
|
||||||
qtag->tpid = mg_htons(0x8100);
|
qtag->tpid = mg_htons(0x8100);
|
||||||
qtag->tci = mg_htons(d->vlan_id); // PCP = default (best-effort)
|
qtag->tci = mg_htons((uint16_t) ((d->vlan_id & 0x1fff) | // VID + DEI
|
||||||
|
(dscp >> 3) << 13)); // RFC-9435 5.1.1
|
||||||
hlp += sizeof(*qtag);
|
hlp += sizeof(*qtag);
|
||||||
MG_STORE_BE16(qtag + 1, eth_types[(unsigned int) proto]);
|
MG_STORE_BE16(qtag + 1, eth_types[(unsigned int) proto]);
|
||||||
}
|
}
|
||||||
@@ -6634,22 +6637,24 @@ static uint8_t *l2_ppp_header(enum mg_l2proto proto, uint8_t *p) {
|
|||||||
|
|
||||||
uint8_t *mg_l2_ppp_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
uint8_t *mg_l2_ppp_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
||||||
struct mg_l2addr *src, struct mg_l2addr *dst,
|
struct mg_l2addr *src, struct mg_l2addr *dst,
|
||||||
uint8_t *frame) {
|
uint8_t *frame, uint8_t dscp) {
|
||||||
(void) ifp;
|
(void) ifp;
|
||||||
(void) src;
|
(void) src;
|
||||||
(void) dst;
|
(void) dst;
|
||||||
|
(void) dscp;
|
||||||
return l2_ppp_header(proto, hdlc_header(frame));
|
return l2_ppp_header(proto, hdlc_header(frame));
|
||||||
}
|
}
|
||||||
|
|
||||||
extern uint8_t *mg_l2_eth_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
extern uint8_t *mg_l2_eth_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
||||||
struct mg_l2addr *src, struct mg_l2addr *dst,
|
struct mg_l2addr *src, struct mg_l2addr *dst,
|
||||||
uint8_t *frame);
|
uint8_t *frame, uint8_t dscp);
|
||||||
|
|
||||||
static uint8_t *pppoe_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
static uint8_t *pppoe_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
||||||
uint8_t code, uint16_t id, struct mg_l2addr *src,
|
uint8_t code, uint16_t id, struct mg_l2addr *src,
|
||||||
struct mg_l2addr *dst, uint8_t *frame) {
|
struct mg_l2addr *dst, uint8_t *frame,
|
||||||
|
uint8_t dscp) {
|
||||||
struct pppoe *pppoe =
|
struct pppoe *pppoe =
|
||||||
(struct pppoe *) mg_l2_eth_header(ifp, proto, src, dst, frame);
|
(struct pppoe *) mg_l2_eth_header(ifp, proto, src, dst, frame, dscp);
|
||||||
pppoe->vertype = 0x11;
|
pppoe->vertype = 0x11;
|
||||||
pppoe->code = code;
|
pppoe->code = code;
|
||||||
pppoe->id = id;
|
pppoe->id = id;
|
||||||
@@ -6658,10 +6663,10 @@ static uint8_t *pppoe_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
|||||||
|
|
||||||
uint8_t *mg_l2_pppoe_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
uint8_t *mg_l2_pppoe_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
||||||
struct mg_l2addr *src, struct mg_l2addr *dst,
|
struct mg_l2addr *src, struct mg_l2addr *dst,
|
||||||
uint8_t *frame) {
|
uint8_t *frame, uint8_t dscp) {
|
||||||
(void) dst;
|
(void) dst;
|
||||||
return l2_ppp_header(proto, pppoe_header(ifp, MG_TCPIP_L2PROTO_PPPoE_SESS, 0,
|
return l2_ppp_header(proto, pppoe_header(ifp, MG_TCPIP_L2PROTO_PPPoE_SESS, 0,
|
||||||
s_id, src, dst, frame));
|
s_id, src, dst, frame, dscp));
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t mg_l2_ppp_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *cur) {
|
size_t mg_l2_ppp_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *cur) {
|
||||||
@@ -6697,7 +6702,8 @@ static uint8_t *ppp_tx_frame_header(struct mg_tcpip_if *ifp, uint16_t proto) {
|
|||||||
return ppp_header(proto, hdlc_header(l2p));
|
return ppp_header(proto, hdlc_header(l2p));
|
||||||
return ppp_header(proto, pppoe_header(ifp, MG_TCPIP_L2PROTO_PPPoE_SESS, 0,
|
return ppp_header(proto, pppoe_header(ifp, MG_TCPIP_L2PROTO_PPPoE_SESS, 0,
|
||||||
s_id, (struct mg_l2addr *) ifp->mac,
|
s_id, (struct mg_l2addr *) ifp->mac,
|
||||||
(struct mg_l2addr *) ifp->gwmac, l2p));
|
(struct mg_l2addr *) ifp->gwmac, l2p,
|
||||||
|
0));
|
||||||
}
|
}
|
||||||
static size_t ppp_tx_frame_trailer(struct mg_tcpip_if *ifp, size_t len,
|
static size_t ppp_tx_frame_trailer(struct mg_tcpip_if *ifp, size_t len,
|
||||||
uint8_t *cur) {
|
uint8_t *cur) {
|
||||||
@@ -7012,7 +7018,7 @@ static size_t pppoe_tx_frame(struct mg_tcpip_if *ifp, uint8_t code, uint16_t id,
|
|||||||
struct mg_l2addr *dst) {
|
struct mg_l2addr *dst) {
|
||||||
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
|
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
|
||||||
uint8_t *p = pppoe_header(ifp, MG_TCPIP_L2PROTO_PPPoE_DISC, code, id,
|
uint8_t *p = pppoe_header(ifp, MG_TCPIP_L2PROTO_PPPoE_DISC, code, id,
|
||||||
(struct mg_l2addr *) ifp->mac, dst, l2p);
|
(struct mg_l2addr *) ifp->mac, dst, l2p, 0);
|
||||||
memmove(p, data, datasz);
|
memmove(p, data, datasz);
|
||||||
return mg_l2_driver_output(ifp, pppoe_trailer(ifp, datasz, p + datasz));
|
return mg_l2_driver_output(ifp, pppoe_trailer(ifp, datasz, p + datasz));
|
||||||
}
|
}
|
||||||
@@ -9374,7 +9380,7 @@ struct pkt {
|
|||||||
// L2 API
|
// L2 API
|
||||||
void mg_l2_init(struct mg_tcpip_if *ifp);
|
void mg_l2_init(struct mg_tcpip_if *ifp);
|
||||||
uint8_t *mg_l2_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
uint8_t *mg_l2_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
||||||
uint8_t *src, uint8_t *dst, uint8_t *frame);
|
uint8_t *src, uint8_t *dst, uint8_t *frame, uint8_t dscp);
|
||||||
size_t mg_l2_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *cur);
|
size_t mg_l2_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *cur);
|
||||||
bool mg_l2_rx(struct mg_tcpip_if *ifp, enum mg_l2proto *proto,
|
bool mg_l2_rx(struct mg_tcpip_if *ifp, enum mg_l2proto *proto,
|
||||||
struct mg_str *pay, struct mg_str *raw);
|
struct mg_str *pay, struct mg_str *raw);
|
||||||
@@ -9527,7 +9533,6 @@ static const struct mg_addr ip6_allnodes = {
|
|||||||
static const uint8_t locpx[8] = {0xfe, 0x80, 0, 0, 0, 0, 0, 0};
|
static const uint8_t locpx[8] = {0xfe, 0x80, 0, 0, 0, 0, 0, 0};
|
||||||
|
|
||||||
#define MG_IP6MATCH(a, b) (a[0] == b[0] && a[1] == b[1])
|
#define MG_IP6MATCH(a, b) (a[0] == b[0] && a[1] == b[1])
|
||||||
#define MG_IP6UNSPEC(a) (a[0] == 0 && a[1] == 0)
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static void settmout(struct mg_connection *c, uint8_t type) {
|
static void settmout(struct mg_connection *c, uint8_t type) {
|
||||||
@@ -9555,7 +9560,7 @@ void mg_tcpip_arp_request(struct mg_tcpip_if *ifp, uint32_t ip, uint8_t *mac) {
|
|||||||
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
|
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
|
||||||
struct arp *arp = (struct arp *) mg_l2_header(
|
struct arp *arp = (struct arp *) mg_l2_header(
|
||||||
ifp, MG_TCPIP_L2PROTO_ARP, ifp->mac,
|
ifp, MG_TCPIP_L2PROTO_ARP, ifp->mac,
|
||||||
mg_l2_mapip(ifp->l2type, MG_TCPIP_L2ADDR_BCAST, NULL), l2p);
|
mg_l2_mapip(ifp->l2type, MG_TCPIP_L2ADDR_BCAST, NULL), l2p, 0);
|
||||||
memset(arp, 0, sizeof(*arp));
|
memset(arp, 0, sizeof(*arp));
|
||||||
arp->fmt = mg_htons(1), arp->pro = mg_htons(0x800), arp->hlen = 6,
|
arp->fmt = mg_htons(1), arp->pro = mg_htons(0x800), arp->hlen = 6,
|
||||||
arp->plen = 4;
|
arp->plen = 4;
|
||||||
@@ -9602,7 +9607,7 @@ static struct ip *tx_ip(struct mg_tcpip_if *ifp, uint8_t *l2_dst, uint8_t proto,
|
|||||||
// ifp->tx.buf is 8-bit aligned, keep other headers as pointers, see pkt
|
// ifp->tx.buf is 8-bit aligned, keep other headers as pointers, see pkt
|
||||||
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
|
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
|
||||||
struct ip *ip = (struct ip *) mg_l2_header(ifp, MG_TCPIP_L2PROTO_IPV4,
|
struct ip *ip = (struct ip *) mg_l2_header(ifp, MG_TCPIP_L2PROTO_IPV4,
|
||||||
ifp->mac, l2_dst, l2p);
|
ifp->mac, l2_dst, l2p, dscp);
|
||||||
memset(ip, 0, sizeof(*ip));
|
memset(ip, 0, sizeof(*ip));
|
||||||
ip->ver = 0x45; // Version 4, header length 5 words
|
ip->ver = 0x45; // Version 4, header length 5 words
|
||||||
ip->tos = (uint8_t) (dscp << 2);
|
ip->tos = (uint8_t) (dscp << 2);
|
||||||
@@ -9778,8 +9783,9 @@ static void rx_arp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
|
|||||||
// ARP request. Make a response, then send
|
// ARP request. Make a response, then send
|
||||||
// MG_VERBOSE(("ARP req from %M", mg_print_ip4, &pkt->arp->spa));
|
// MG_VERBOSE(("ARP req from %M", mg_print_ip4, &pkt->arp->spa));
|
||||||
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
|
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
|
||||||
struct arp *arp = (struct arp *) mg_l2_header(
|
struct arp *arp =
|
||||||
ifp, MG_TCPIP_L2PROTO_ARP, ifp->mac, mg_l2_getaddr(ifp, pkt->l2), l2p);
|
(struct arp *) mg_l2_header(ifp, MG_TCPIP_L2PROTO_ARP, ifp->mac,
|
||||||
|
mg_l2_getaddr(ifp, pkt->l2), l2p, 0);
|
||||||
*arp = *pkt->arp;
|
*arp = *pkt->arp;
|
||||||
arp->op = mg_htons(2);
|
arp->op = mg_htons(2);
|
||||||
memcpy(arp->tha, pkt->arp->sha, sizeof(pkt->arp->tha));
|
memcpy(arp->tha, pkt->arp->sha, sizeof(pkt->arp->tha));
|
||||||
@@ -9977,7 +9983,7 @@ static struct ip6 *tx_ip6(struct mg_tcpip_if *ifp, uint8_t *l2_dst,
|
|||||||
// ifp->tx.buf is 8-bit aligned, keep other headers as pointers, see pkt
|
// ifp->tx.buf is 8-bit aligned, keep other headers as pointers, see pkt
|
||||||
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
|
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
|
||||||
struct ip6 *ip6 = (struct ip6 *) mg_l2_header(ifp, MG_TCPIP_L2PROTO_IPV6,
|
struct ip6 *ip6 = (struct ip6 *) mg_l2_header(ifp, MG_TCPIP_L2PROTO_IPV6,
|
||||||
ifp->mac, l2_dst, l2p);
|
ifp->mac, l2_dst, l2p, dscp);
|
||||||
memset(ip6, 0, sizeof(*ip6));
|
memset(ip6, 0, sizeof(*ip6));
|
||||||
ip6->ver = (uint8_t) (0x60 | (dscp >> 2)); // Version 6, traffic class
|
ip6->ver = (uint8_t) (0x60 | (dscp >> 2)); // Version 6, traffic class
|
||||||
ip6->label[0] = (uint8_t) (dscp << 6);
|
ip6->label[0] = (uint8_t) (dscp << 6);
|
||||||
@@ -10082,9 +10088,9 @@ static void rx_ndp_ns(struct mg_tcpip_if *ifp, struct pkt *pkt) {
|
|||||||
// - use solicited node multicast to resolve a l2 address (l2_addr = NULL)
|
// - use solicited node multicast to resolve a l2 address (l2_addr = NULL)
|
||||||
// - use unicast to verify presence (l2_addr = neighbor l2 address)
|
// - use unicast to verify presence (l2_addr = neighbor l2 address)
|
||||||
static void tx_ndp_ns(struct mg_tcpip_if *ifp, uint64_t *ip_dst,
|
static void tx_ndp_ns(struct mg_tcpip_if *ifp, uint64_t *ip_dst,
|
||||||
uint8_t *l2_addr, bool ll) {
|
uint8_t *l2_addr) {
|
||||||
uint64_t *ip_src = ll ? ifp->ip6ll : ifp->ip6;
|
|
||||||
uint8_t payload[4 + 16 + 16]; // NOTE(): 16 --> optional len upto 2 hw addr
|
uint8_t payload[4 + 16 + 16]; // NOTE(): 16 --> optional len upto 2 hw addr
|
||||||
|
uint64_t ip_unspec[2] = {0, 0};
|
||||||
size_t payload_len = 20;
|
size_t payload_len = 20;
|
||||||
bool mcast = (l2_addr == NULL);
|
bool mcast = (l2_addr == NULL);
|
||||||
uint64_t ip_mcast[2] = {0, 0};
|
uint64_t ip_mcast[2] = {0, 0};
|
||||||
@@ -10104,12 +10110,14 @@ static void tx_ndp_ns(struct mg_tcpip_if *ifp, uint64_t *ip_dst,
|
|||||||
l2 = mg_l2_mapip(ifp->l2type, MG_TCPIP_L2ADDR_MCAST6, &ipd);
|
l2 = mg_l2_mapip(ifp->l2type, MG_TCPIP_L2ADDR_MCAST6, &ipd);
|
||||||
}
|
}
|
||||||
payload_len = 20;
|
payload_len = 20;
|
||||||
if (!MG_IP6UNSPEC(ip_src)) {
|
// TODO(robertc2000): using only link-local IP addr for now
|
||||||
|
// We might consider to add an option to use either link-local or global IP
|
||||||
|
if (!MG_IP6MATCH(ifp->ip6ll, ip_unspec)) {
|
||||||
payload[20] = 1; // 4.6.1, source hwaddr; option length in 8-byte units
|
payload[20] = 1; // 4.6.1, source hwaddr; option length in 8-byte units
|
||||||
payload[21] = mg_l2_ip6put(ifp->l2type, ifp->mac, payload + 22);
|
payload[21] = mg_l2_ip6put(ifp->l2type, ifp->mac, payload + 22);
|
||||||
payload_len += 8 * payload[21];
|
payload_len += 8 * payload[21];
|
||||||
}
|
}
|
||||||
tx_icmp6(ifp, l2, ip_src, mcast ? ip_mcast : ip_dst, 135, 0, payload,
|
tx_icmp6(ifp, l2, ifp->ip6ll, mcast ? ip_mcast : ip_dst, 135, 0, payload,
|
||||||
payload_len);
|
payload_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -10117,10 +10125,11 @@ static void tx_ndp_ns(struct mg_tcpip_if *ifp, uint64_t *ip_dst,
|
|||||||
static void tx_ndp_rs(struct mg_tcpip_if *ifp) {
|
static void tx_ndp_rs(struct mg_tcpip_if *ifp) {
|
||||||
uint8_t payload[4 + 16]; // reserved + optional len upto 2 hw addr NOTE()
|
uint8_t payload[4 + 16]; // reserved + optional len upto 2 hw addr NOTE()
|
||||||
size_t payload_len = 4;
|
size_t payload_len = 4;
|
||||||
|
uint64_t ip_unspec[2] = {0, 0};
|
||||||
|
|
||||||
memset(payload, 0, sizeof(payload));
|
memset(payload, 0, sizeof(payload));
|
||||||
|
|
||||||
if (!MG_IP6UNSPEC(ifp->ip6ll)) {
|
if (!MG_IP6MATCH(ifp->ip6ll, ip_unspec)) {
|
||||||
payload[4] = 1; // 4.6.1, source hwaddr; option length in 8-byte units
|
payload[4] = 1; // 4.6.1, source hwaddr; option length in 8-byte units
|
||||||
payload[5] = mg_l2_ip6put(ifp->l2type, ifp->mac, payload + 6);
|
payload[5] = mg_l2_ip6put(ifp->l2type, ifp->mac, payload + 6);
|
||||||
payload_len += 8 * payload[5];
|
payload_len += 8 * payload[5];
|
||||||
@@ -10289,7 +10298,7 @@ static void onstate6change(struct mg_tcpip_if *ifp) {
|
|||||||
(struct mg_addr *) &ip6_allnodes),
|
(struct mg_addr *) &ip6_allnodes),
|
||||||
ifp->ip6ll, (uint64_t *) ip6_allnodes.addr.ip6, false,
|
ifp->ip6ll, (uint64_t *) ip6_allnodes.addr.ip6, false,
|
||||||
ifp->mac);
|
ifp->mac);
|
||||||
if (!MG_IP6UNSPEC(ifp->ip6))
|
if (ifp->ip6[0] != 0 && ifp->ip6[1] != 0)
|
||||||
tx_ndp_na(ifp, // RFC-9131 4.1
|
tx_ndp_na(ifp, // RFC-9131 4.1
|
||||||
mg_l2_mapip(ifp->l2type, MG_TCPIP_L2ADDR_MCAST6,
|
mg_l2_mapip(ifp->l2type, MG_TCPIP_L2ADDR_MCAST6,
|
||||||
(struct mg_addr *) &ip6_allrouters),
|
(struct mg_addr *) &ip6_allrouters),
|
||||||
@@ -10304,9 +10313,9 @@ static void onstate6change(struct mg_tcpip_if *ifp) {
|
|||||||
ifp->is_ip6_changed = false;
|
ifp->is_ip6_changed = false;
|
||||||
}
|
}
|
||||||
} else if (ifp->state6 == MG_TCPIP_STATE_IP) {
|
} else if (ifp->state6 == MG_TCPIP_STATE_IP) {
|
||||||
if (!MG_IP6UNSPEC(ifp->gw6) &&
|
if ((ifp->gw6[0] != 0 || ifp->gw6[1] != 0) &&
|
||||||
(ifp->l2type != MG_TCPIP_L2_PPP && ifp->l2type != MG_TCPIP_L2_PPPoE))
|
(ifp->l2type != MG_TCPIP_L2_PPP && ifp->l2type != MG_TCPIP_L2_PPPoE))
|
||||||
tx_ndp_ns(ifp, ifp->gw6, NULL, true); // unsolicited GW hwaddr resolution
|
tx_ndp_ns(ifp, ifp->gw6, NULL); // unsolicited GW hwaddr resolution
|
||||||
} else if (ifp->state6 == MG_TCPIP_STATE_UP) {
|
} else if (ifp->state6 == MG_TCPIP_STATE_UP) {
|
||||||
MG_INFO(("IP: %M", mg_print_ip6, &ifp->ip6ll));
|
MG_INFO(("IP: %M", mg_print_ip6, &ifp->ip6ll));
|
||||||
}
|
}
|
||||||
@@ -10519,14 +10528,13 @@ static struct mg_connection *accept_conn(struct mg_connection *lsn,
|
|||||||
struct pkt *pkt, uint16_t mss) {
|
struct pkt *pkt, uint16_t mss) {
|
||||||
struct connstate *s;
|
struct connstate *s;
|
||||||
uint8_t *l2addr;
|
uint8_t *l2addr;
|
||||||
struct mg_tcpip_if *ifp = lsn->mgr->ifp;
|
|
||||||
struct mg_connection *c = mg_alloc_conn(lsn->mgr);
|
struct mg_connection *c = mg_alloc_conn(lsn->mgr);
|
||||||
if (c == NULL) {
|
if (c == NULL) {
|
||||||
MG_ERROR(("OOM"));
|
MG_ERROR(("OOM"));
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
s = (struct connstate *) (c + 1);
|
s = (struct connstate *) (c + 1);
|
||||||
s->retransmit = ifp->enable_tcp_retransmit;
|
s->retransmit = lsn->mgr->ifp->enable_tcp_retransmit;
|
||||||
s->txq.align = MG_TCPIP_TXQ_ALIGN;
|
s->txq.align = MG_TCPIP_TXQ_ALIGN;
|
||||||
s->dmss = mss; // from options in client SYN
|
s->dmss = mss; // from options in client SYN
|
||||||
s->seq = mg_ntohl(pkt->tcp->ack), s->ack = mg_ntohl(pkt->tcp->seq);
|
s->seq = mg_ntohl(pkt->tcp->ack), s->ack = mg_ntohl(pkt->tcp->seq);
|
||||||
@@ -10535,21 +10543,19 @@ static struct mg_connection *accept_conn(struct mg_connection *lsn,
|
|||||||
if (lsn->loc.is_ip6) {
|
if (lsn->loc.is_ip6) {
|
||||||
c->rem.addr.ip6[0] = pkt->ip6->src[0],
|
c->rem.addr.ip6[0] = pkt->ip6->src[0],
|
||||||
c->rem.addr.ip6[1] = pkt->ip6->src[1], c->rem.is_ip6 = true;
|
c->rem.addr.ip6[1] = pkt->ip6->src[1], c->rem.is_ip6 = true;
|
||||||
if (pkt->ip6->dst[0] == ifp->ip6ll[0]) { // link-local prefix
|
c->loc.addr.ip6[0] = c->mgr->ifp->ip6[0],
|
||||||
c->loc.addr.ip6[0] = ifp->ip6ll[0], c->loc.addr.ip6[1] = ifp->ip6ll[1];
|
c->loc.addr.ip6[1] = c->mgr->ifp->ip6[1], c->loc.is_ip6 = true;
|
||||||
} else {
|
// TODO(): compare lsn to link-local, or rem as link-local: use ll instead
|
||||||
c->loc.addr.ip6[0] = ifp->ip6[0], c->loc.addr.ip6[1] = ifp->ip6[1];
|
|
||||||
}
|
|
||||||
c->loc.is_ip6 = true;
|
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
c->rem.addr.ip4 = pkt->ip->src;
|
c->rem.addr.ip4 = pkt->ip->src;
|
||||||
c->loc.addr.ip4 = ifp->ip;
|
c->loc.addr.ip4 = c->mgr->ifp->ip;
|
||||||
}
|
}
|
||||||
c->rem.port = pkt->tcp->sport;
|
c->rem.port = pkt->tcp->sport;
|
||||||
c->loc.port = lsn->loc.port;
|
c->loc.port = lsn->loc.port;
|
||||||
if ((l2addr = get_return_l2addr(ifp, &c->rem, false, pkt)) == NULL) {
|
if ((l2addr = get_return_l2addr(lsn->mgr->ifp, &c->rem, false, pkt)) ==
|
||||||
|
NULL) {
|
||||||
mg_free(c); // safety net for lousy networks, not actually needed
|
mg_free(c); // safety net for lousy networks, not actually needed
|
||||||
return NULL; // as path has already been checked at SYN (sending SYN+ACK)
|
return NULL; // as path has already been checked at SYN (sending SYN+ACK)
|
||||||
}
|
}
|
||||||
@@ -10607,14 +10613,10 @@ static bool udp_send(struct mg_connection *c, const void *buf, size_t len) {
|
|||||||
struct mg_addr ips;
|
struct mg_addr ips;
|
||||||
memset(&ips, 0, sizeof(ips));
|
memset(&ips, 0, sizeof(ips));
|
||||||
#if MG_ENABLE_IPV6
|
#if MG_ENABLE_IPV6
|
||||||
if (c->loc.is_ip6) { // At [::] infer link-local from peer
|
if (c->loc.is_ip6) {
|
||||||
if (MG_IP6UNSPEC(c->loc.addr.ip6) ? c->rem.addr.ip6[0] == ifp->ip6ll[0]
|
ips.addr.ip6[0] = ifp->ip6[0], ips.addr.ip6[1] = ifp->ip6[1],
|
||||||
: c->loc.addr.ip6[0] == ifp->ip6ll[0]) {
|
|
||||||
ips.addr.ip6[0] = ifp->ip6ll[0], ips.addr.ip6[1] = ifp->ip6ll[1];
|
|
||||||
} else {
|
|
||||||
ips.addr.ip6[0] = ifp->ip6[0], ips.addr.ip6[1] = ifp->ip6[1];
|
|
||||||
}
|
|
||||||
ips.is_ip6 = true;
|
ips.is_ip6 = true;
|
||||||
|
// TODO(): detect link-local (c->rem) and use it
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
@@ -11275,14 +11277,14 @@ static void mg_ip6_link(struct mg_tcpip_if *ifp, bool drv_up, bool l2_up) {
|
|||||||
bool cur_drv = (ifp->state6 != MG_TCPIP_STATE_DOWN);
|
bool cur_drv = (ifp->state6 != MG_TCPIP_STATE_DOWN);
|
||||||
bool cur_l2 = (ifp->state6 >= MG_TCPIP_STATE_UP);
|
bool cur_l2 = (ifp->state6 >= MG_TCPIP_STATE_UP);
|
||||||
if (drv_up != cur_drv || l2_up != cur_l2) { // link/L2 state has changed
|
if (drv_up != cur_drv || l2_up != cur_l2) { // link/L2 state has changed
|
||||||
if (ifp->l2type == MG_TCPIP_L2_ETH && l2_up &&
|
if (ifp->l2type == MG_TCPIP_L2_ETH && l2_up && ifp->ip6ll[0] == 0 &&
|
||||||
MG_IP6UNSPEC(ifp->ip6ll)) { // gen ll address
|
ifp->ip6ll[1] == 0) { // gen ll address
|
||||||
mg_l2_genip6(ifp->l2type, ifp->ip6ll, 64, ifp->mac);
|
mg_l2_genip6(ifp->l2type, ifp->ip6ll, 64, ifp->mac);
|
||||||
memcpy(ifp->ip6ll, locpx, 8); // RFC-4291 2.5.6; RFC-2464 5
|
memcpy(ifp->ip6ll, locpx, 8); // RFC-4291 2.5.6; RFC-2464 5
|
||||||
} // just got our link local address if we didn't have one.
|
} // just got our link local address if we didn't have one.
|
||||||
// If static configuration is used, global addresses,
|
// If static configuration is used, global addresses,
|
||||||
// prefix length, and gw are already filled at this point.
|
// prefix length, and gw are already filled at this point.
|
||||||
if (MG_IP6UNSPEC(ifp->ip6)) ifp->enable_slaac = true;
|
if (ifp->ip6[0] == 0 && ifp->ip6[1] == 0) ifp->enable_slaac = true;
|
||||||
if (!l2_up && ifp->enable_slaac) ifp->ip6[0] = ifp->ip6[1] = 0;
|
if (!l2_up && ifp->enable_slaac) ifp->ip6[0] = ifp->ip6[1] = 0;
|
||||||
ifp->state6 = !drv_up ? MG_TCPIP_STATE_DOWN
|
ifp->state6 = !drv_up ? MG_TCPIP_STATE_DOWN
|
||||||
: !l2_up ? MG_TCPIP_STATE_LINK_UP
|
: !l2_up ? MG_TCPIP_STATE_LINK_UP
|
||||||
@@ -11343,8 +11345,8 @@ static void mg_tcpip_poll(struct mg_tcpip_if *ifp, uint64_t now) {
|
|||||||
onstate6change(ifp);
|
onstate6change(ifp);
|
||||||
}
|
}
|
||||||
if (expired_1000ms && ifp->state6 == MG_TCPIP_STATE_READY && !ifp->gw6_ready &&
|
if (expired_1000ms && ifp->state6 == MG_TCPIP_STATE_READY && !ifp->gw6_ready &&
|
||||||
!MG_IP6UNSPEC(ifp->gw6))
|
(ifp->gw6[0] != 0 || ifp->gw6[1] != 0))
|
||||||
tx_ndp_ns(ifp, ifp->gw6, NULL, true); // retry GW hwaddr resolution
|
tx_ndp_ns(ifp, ifp->gw6, NULL); // retry GW hwaddr resolution
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
{ // poll driver and let L2 do its work, if any
|
{ // poll driver and let L2 do its work, if any
|
||||||
@@ -11523,12 +11525,13 @@ void mg_connect_resolved(struct mg_connection *c) {
|
|||||||
} // otherwise retain an existing bound port
|
} // otherwise retain an existing bound port
|
||||||
#if MG_ENABLE_IPV6
|
#if MG_ENABLE_IPV6
|
||||||
if (c->rem.is_ip6) {
|
if (c->rem.is_ip6) {
|
||||||
bool any = MG_IP6UNSPEC(c->loc.addr.ip6);
|
if (c->loc.addr.ip6[0] == 0 && c->loc.addr.ip6[1] == 0 &&
|
||||||
if (any && c->rem.addr.ip6[0] == ifp->ip6ll[0]) { // same local LAN, use ll
|
c->rem.addr.ip6[0] == ifp->ip6ll[0]) { // same local LAN, use ll
|
||||||
c->loc.addr.ip6[0] = ifp->ip6ll[0], c->loc.addr.ip6[1] = ifp->ip6ll[1];
|
c->loc.addr.ip6[0] = ifp->ip6ll[0], c->loc.addr.ip6[1] = ifp->ip6ll[1];
|
||||||
} else if (any) { // Use global address
|
} else if (c->loc.addr.ip6[0] == 0 && c->loc.addr.ip6[1] == 0) {
|
||||||
|
// Use global address
|
||||||
c->loc.addr.ip6[0] = ifp->ip6[0], c->loc.addr.ip6[1] = ifp->ip6[1];
|
c->loc.addr.ip6[0] = ifp->ip6[0], c->loc.addr.ip6[1] = ifp->ip6[1];
|
||||||
} // otherwise retain an existing bound address
|
} // otherwise retain an existing bound address
|
||||||
c->loc.is_ip6 = true;
|
c->loc.is_ip6 = true;
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
@@ -11550,8 +11553,7 @@ void mg_connect_resolved(struct mg_connection *c) {
|
|||||||
&& !MG_IP6MATCH(c->rem.addr.ip6, ifp->gw6))) { // and not gw
|
&& !MG_IP6MATCH(c->rem.addr.ip6, ifp->gw6))) { // and not gw
|
||||||
// If we're in the same LAN, fire a Neighbor Solicitation
|
// If we're in the same LAN, fire a Neighbor Solicitation
|
||||||
MG_DEBUG(("%lu NS lookup...", c->id));
|
MG_DEBUG(("%lu NS lookup...", c->id));
|
||||||
tx_ndp_ns(ifp, c->rem.addr.ip6, NULL,
|
tx_ndp_ns(ifp, c->rem.addr.ip6, NULL); // RFC-4861 4.3, requesting
|
||||||
c->loc.addr.ip6[0] == ifp->ip6ll[0]); // RFC-4861 4.3, request
|
|
||||||
settmout(c, MIP_TTYPE_ARP);
|
settmout(c, MIP_TTYPE_ARP);
|
||||||
c->is_arplooking = 1;
|
c->is_arplooking = 1;
|
||||||
} else if (ifp->gw6_ready) {
|
} else if (ifp->gw6_ready) {
|
||||||
@@ -18515,7 +18517,6 @@ static void mg_tls_derive_secret(const char *label, uint8_t *key, size_t keysz,
|
|||||||
size_t labelsz = strlen(label);
|
size_t labelsz = strlen(label);
|
||||||
uint8_t secret[32];
|
uint8_t secret[32];
|
||||||
uint8_t packed[256] = {0, (uint8_t) hashsz, (uint8_t) labelsz};
|
uint8_t packed[256] = {0, (uint8_t) hashsz, (uint8_t) labelsz};
|
||||||
// TODO: assert lengths of label, key, data and hash
|
|
||||||
if (labelsz > 0) memmove(packed + 3, label, labelsz);
|
if (labelsz > 0) memmove(packed + 3, label, labelsz);
|
||||||
packed[3 + labelsz] = (uint8_t) datasz;
|
packed[3 + labelsz] = (uint8_t) datasz;
|
||||||
if (datasz > 0) memmove(packed + labelsz + 4, data, datasz);
|
if (datasz > 0) memmove(packed + labelsz + 4, data, datasz);
|
||||||
@@ -31089,7 +31090,7 @@ static size_t mg_cyw_tx(unsigned int ifc, void *data, size_t len) {
|
|||||||
uint16_t txlen = (uint16_t) (len + sizeof(*hdr));
|
uint16_t txlen = (uint16_t) (len + sizeof(*hdr));
|
||||||
memset(txdata, 0, sizeof(*hdr));
|
memset(txdata, 0, sizeof(*hdr));
|
||||||
memcpy((uint8_t *) txdata + sizeof(*hdr), data, len);
|
memcpy((uint8_t *) txdata + sizeof(*hdr), data, len);
|
||||||
// TODO(): hdr->bdc.priority = map IP to TOS if supporting QoS/ToS
|
// TODO(): hdr->bdc.priority = dscp >> 3; // RFC-9435 5.1.1
|
||||||
hdr->bdc.flags = 2 << 4; // BDC version 2
|
hdr->bdc.flags = 2 << 4; // BDC version 2
|
||||||
hdr->bdc.flags2 = (uint8_t) ifc; // 0 -> STA, 1 -> AP
|
hdr->bdc.flags2 = (uint8_t) ifc; // 0 -> STA, 1 -> AP
|
||||||
// hdr->bdc.data_offset = 0; // actually zeroed above
|
// hdr->bdc.data_offset = 0; // actually zeroed above
|
||||||
|
|||||||
+1
-1
@@ -225,7 +225,7 @@ static size_t mg_cyw_tx(unsigned int ifc, void *data, size_t len) {
|
|||||||
uint16_t txlen = (uint16_t) (len + sizeof(*hdr));
|
uint16_t txlen = (uint16_t) (len + sizeof(*hdr));
|
||||||
memset(txdata, 0, sizeof(*hdr));
|
memset(txdata, 0, sizeof(*hdr));
|
||||||
memcpy((uint8_t *) txdata + sizeof(*hdr), data, len);
|
memcpy((uint8_t *) txdata + sizeof(*hdr), data, len);
|
||||||
// TODO(): hdr->bdc.priority = map IP to TOS if supporting QoS/ToS
|
// TODO(): hdr->bdc.priority = dscp >> 3; // RFC-9435 5.1.1
|
||||||
hdr->bdc.flags = 2 << 4; // BDC version 2
|
hdr->bdc.flags = 2 << 4; // BDC version 2
|
||||||
hdr->bdc.flags2 = (uint8_t) ifc; // 0 -> STA, 1 -> AP
|
hdr->bdc.flags2 = (uint8_t) ifc; // 0 -> STA, 1 -> AP
|
||||||
// hdr->bdc.data_offset = 0; // actually zeroed above
|
// hdr->bdc.data_offset = 0; // actually zeroed above
|
||||||
|
|||||||
@@ -8,7 +8,8 @@
|
|||||||
void mg_l2_init(struct mg_tcpip_if *ifp);
|
void mg_l2_init(struct mg_tcpip_if *ifp);
|
||||||
bool mg_l2_poll(struct mg_tcpip_if *ifp, bool expired_1000ms);
|
bool mg_l2_poll(struct mg_tcpip_if *ifp, bool expired_1000ms);
|
||||||
uint8_t *mg_l2_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
uint8_t *mg_l2_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
||||||
uint8_t *src, uint8_t *dst, uint8_t *frame);
|
uint8_t *src, uint8_t *dst, uint8_t *frame,
|
||||||
|
uint8_t dscp);
|
||||||
size_t mg_l2_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *cur);
|
size_t mg_l2_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *cur);
|
||||||
bool mg_l2_rx(struct mg_tcpip_if *ifp, enum mg_l2proto *proto,
|
bool mg_l2_rx(struct mg_tcpip_if *ifp, enum mg_l2proto *proto,
|
||||||
struct mg_str *pay, struct mg_str *raw);
|
struct mg_str *pay, struct mg_str *raw);
|
||||||
@@ -27,7 +28,7 @@ size_t mg_l2_driver_output(struct mg_tcpip_if *ifp, size_t len);
|
|||||||
// clang-format off
|
// clang-format off
|
||||||
extern void mg_l2_eth_init(struct mg_tcpip_if *);
|
extern void mg_l2_eth_init(struct mg_tcpip_if *);
|
||||||
extern bool mg_l2_eth_poll(struct mg_tcpip_if *, bool);
|
extern bool mg_l2_eth_poll(struct mg_tcpip_if *, bool);
|
||||||
extern uint8_t *mg_l2_eth_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *);
|
extern uint8_t *mg_l2_eth_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *, uint8_t);
|
||||||
extern size_t mg_l2_eth_trailer(struct mg_tcpip_if *, size_t, uint8_t *);
|
extern size_t mg_l2_eth_trailer(struct mg_tcpip_if *, size_t, uint8_t *);
|
||||||
extern bool mg_l2_eth_rx(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
|
extern bool mg_l2_eth_rx(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
|
||||||
extern struct mg_l2addr *mg_l2_eth_getaddr(struct mg_tcpip_if *, uint8_t *);
|
extern struct mg_l2addr *mg_l2_eth_getaddr(struct mg_tcpip_if *, uint8_t *);
|
||||||
@@ -40,7 +41,7 @@ extern uint8_t mg_l2_eth_ip6put(struct mg_l2addr *, uint8_t *);
|
|||||||
|
|
||||||
extern void mg_l2_ppp_init(struct mg_tcpip_if *);
|
extern void mg_l2_ppp_init(struct mg_tcpip_if *);
|
||||||
extern bool mg_l2_ppp_poll(struct mg_tcpip_if *, bool);
|
extern bool mg_l2_ppp_poll(struct mg_tcpip_if *, bool);
|
||||||
extern uint8_t *mg_l2_ppp_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *);
|
extern uint8_t *mg_l2_ppp_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *, uint8_t);
|
||||||
extern size_t mg_l2_ppp_trailer(struct mg_tcpip_if *, size_t, uint8_t *);
|
extern size_t mg_l2_ppp_trailer(struct mg_tcpip_if *, size_t, uint8_t *);
|
||||||
extern bool mg_l2_ppp_rx(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
|
extern bool mg_l2_ppp_rx(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
|
||||||
extern struct mg_l2addr *mg_l2_ppp_getaddr(struct mg_tcpip_if *, uint8_t *);
|
extern struct mg_l2addr *mg_l2_ppp_getaddr(struct mg_tcpip_if *, uint8_t *);
|
||||||
@@ -52,13 +53,13 @@ extern uint8_t mg_l2_ppp_ip6put(struct mg_l2addr *, uint8_t *);
|
|||||||
#endif
|
#endif
|
||||||
extern void mg_l2_pppoe_init(struct mg_tcpip_if *);
|
extern void mg_l2_pppoe_init(struct mg_tcpip_if *);
|
||||||
extern bool mg_l2_pppoe_poll(struct mg_tcpip_if *, bool);
|
extern bool mg_l2_pppoe_poll(struct mg_tcpip_if *, bool);
|
||||||
extern uint8_t *mg_l2_pppoe_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *);
|
extern uint8_t *mg_l2_pppoe_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *, uint8_t);
|
||||||
extern size_t mg_l2_pppoe_trailer(struct mg_tcpip_if *, size_t, uint8_t *);
|
extern size_t mg_l2_pppoe_trailer(struct mg_tcpip_if *, size_t, uint8_t *);
|
||||||
extern bool mg_l2_pppoe_rx(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
|
extern bool mg_l2_pppoe_rx(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
|
||||||
|
|
||||||
typedef void (*l2_init_fn)(struct mg_tcpip_if *);
|
typedef void (*l2_init_fn)(struct mg_tcpip_if *);
|
||||||
typedef bool (*l2_poll_fn)(struct mg_tcpip_if *, bool);
|
typedef bool (*l2_poll_fn)(struct mg_tcpip_if *, bool);
|
||||||
typedef uint8_t *((*l2_header_fn)(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *));
|
typedef uint8_t *((*l2_header_fn)(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *, uint8_t));
|
||||||
typedef size_t (*l2_trailer_fn)(struct mg_tcpip_if *, size_t, uint8_t *);
|
typedef size_t (*l2_trailer_fn)(struct mg_tcpip_if *, size_t, uint8_t *);
|
||||||
typedef bool (*l2_rx_fn)(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
|
typedef bool (*l2_rx_fn)(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
|
||||||
typedef struct mg_l2addr (*(*l2_getaddr_fn)(struct mg_tcpip_if *, uint8_t *));
|
typedef struct mg_l2addr (*(*l2_getaddr_fn)(struct mg_tcpip_if *, uint8_t *));
|
||||||
@@ -101,9 +102,10 @@ bool mg_l2_poll(struct mg_tcpip_if *ifp, bool expired_1000ms) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
uint8_t *mg_l2_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
uint8_t *mg_l2_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
||||||
uint8_t *src, uint8_t *dst, uint8_t *frame) {
|
uint8_t *src, uint8_t *dst, uint8_t *frame,
|
||||||
|
uint8_t dscp) {
|
||||||
return l2_header[ifp->l2type](ifp, proto, (struct mg_l2addr *) src,
|
return l2_header[ifp->l2type](ifp, proto, (struct mg_l2addr *) src,
|
||||||
(struct mg_l2addr *) dst, frame);
|
(struct mg_l2addr *) dst, frame, dscp);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t mg_l2_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *frame) {
|
size_t mg_l2_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *frame) {
|
||||||
|
|||||||
+3
-2
@@ -63,7 +63,7 @@ bool mg_l2_eth_poll(struct mg_tcpip_if *ifp, bool expired_1000ms) {
|
|||||||
|
|
||||||
uint8_t *mg_l2_eth_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
uint8_t *mg_l2_eth_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
||||||
struct mg_l2addr *src, struct mg_l2addr *dst,
|
struct mg_l2addr *src, struct mg_l2addr *dst,
|
||||||
uint8_t *frame) {
|
uint8_t *frame, uint8_t dscp) {
|
||||||
struct eth_data *d = &ifp->l2data.eth;
|
struct eth_data *d = &ifp->l2data.eth;
|
||||||
struct eth *eth = (struct eth *) frame;
|
struct eth *eth = (struct eth *) frame;
|
||||||
uint8_t *hlp;
|
uint8_t *hlp;
|
||||||
@@ -75,7 +75,8 @@ uint8_t *mg_l2_eth_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
|||||||
} else { // Add 802.1Q tag
|
} else { // Add 802.1Q tag
|
||||||
struct qtag *qtag = (struct qtag *) ð->type;
|
struct qtag *qtag = (struct qtag *) ð->type;
|
||||||
qtag->tpid = mg_htons(0x8100);
|
qtag->tpid = mg_htons(0x8100);
|
||||||
qtag->tci = mg_htons(d->vlan_id); // PCP = default (best-effort)
|
qtag->tci = mg_htons((uint16_t) ((d->vlan_id & 0x1fff) | // VID + DEI
|
||||||
|
(dscp >> 3) << 13)); // RFC-9435 5.1.1
|
||||||
hlp += sizeof(*qtag);
|
hlp += sizeof(*qtag);
|
||||||
MG_STORE_BE16(qtag + 1, eth_types[(unsigned int) proto]);
|
MG_STORE_BE16(qtag + 1, eth_types[(unsigned int) proto]);
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-8
@@ -139,22 +139,24 @@ static uint8_t *l2_ppp_header(enum mg_l2proto proto, uint8_t *p) {
|
|||||||
|
|
||||||
uint8_t *mg_l2_ppp_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
uint8_t *mg_l2_ppp_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
||||||
struct mg_l2addr *src, struct mg_l2addr *dst,
|
struct mg_l2addr *src, struct mg_l2addr *dst,
|
||||||
uint8_t *frame) {
|
uint8_t *frame, uint8_t dscp) {
|
||||||
(void) ifp;
|
(void) ifp;
|
||||||
(void) src;
|
(void) src;
|
||||||
(void) dst;
|
(void) dst;
|
||||||
|
(void) dscp;
|
||||||
return l2_ppp_header(proto, hdlc_header(frame));
|
return l2_ppp_header(proto, hdlc_header(frame));
|
||||||
}
|
}
|
||||||
|
|
||||||
extern uint8_t *mg_l2_eth_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
extern uint8_t *mg_l2_eth_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
||||||
struct mg_l2addr *src, struct mg_l2addr *dst,
|
struct mg_l2addr *src, struct mg_l2addr *dst,
|
||||||
uint8_t *frame);
|
uint8_t *frame, uint8_t dscp);
|
||||||
|
|
||||||
static uint8_t *pppoe_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
static uint8_t *pppoe_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
||||||
uint8_t code, uint16_t id, struct mg_l2addr *src,
|
uint8_t code, uint16_t id, struct mg_l2addr *src,
|
||||||
struct mg_l2addr *dst, uint8_t *frame) {
|
struct mg_l2addr *dst, uint8_t *frame,
|
||||||
|
uint8_t dscp) {
|
||||||
struct pppoe *pppoe =
|
struct pppoe *pppoe =
|
||||||
(struct pppoe *) mg_l2_eth_header(ifp, proto, src, dst, frame);
|
(struct pppoe *) mg_l2_eth_header(ifp, proto, src, dst, frame, dscp);
|
||||||
pppoe->vertype = 0x11;
|
pppoe->vertype = 0x11;
|
||||||
pppoe->code = code;
|
pppoe->code = code;
|
||||||
pppoe->id = id;
|
pppoe->id = id;
|
||||||
@@ -163,10 +165,10 @@ static uint8_t *pppoe_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
|||||||
|
|
||||||
uint8_t *mg_l2_pppoe_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
uint8_t *mg_l2_pppoe_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
||||||
struct mg_l2addr *src, struct mg_l2addr *dst,
|
struct mg_l2addr *src, struct mg_l2addr *dst,
|
||||||
uint8_t *frame) {
|
uint8_t *frame, uint8_t dscp) {
|
||||||
(void) dst;
|
(void) dst;
|
||||||
return l2_ppp_header(proto, pppoe_header(ifp, MG_TCPIP_L2PROTO_PPPoE_SESS, 0,
|
return l2_ppp_header(proto, pppoe_header(ifp, MG_TCPIP_L2PROTO_PPPoE_SESS, 0,
|
||||||
s_id, src, dst, frame));
|
s_id, src, dst, frame, dscp));
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t mg_l2_ppp_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *cur) {
|
size_t mg_l2_ppp_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *cur) {
|
||||||
@@ -202,7 +204,8 @@ static uint8_t *ppp_tx_frame_header(struct mg_tcpip_if *ifp, uint16_t proto) {
|
|||||||
return ppp_header(proto, hdlc_header(l2p));
|
return ppp_header(proto, hdlc_header(l2p));
|
||||||
return ppp_header(proto, pppoe_header(ifp, MG_TCPIP_L2PROTO_PPPoE_SESS, 0,
|
return ppp_header(proto, pppoe_header(ifp, MG_TCPIP_L2PROTO_PPPoE_SESS, 0,
|
||||||
s_id, (struct mg_l2addr *) ifp->mac,
|
s_id, (struct mg_l2addr *) ifp->mac,
|
||||||
(struct mg_l2addr *) ifp->gwmac, l2p));
|
(struct mg_l2addr *) ifp->gwmac, l2p,
|
||||||
|
0));
|
||||||
}
|
}
|
||||||
static size_t ppp_tx_frame_trailer(struct mg_tcpip_if *ifp, size_t len,
|
static size_t ppp_tx_frame_trailer(struct mg_tcpip_if *ifp, size_t len,
|
||||||
uint8_t *cur) {
|
uint8_t *cur) {
|
||||||
@@ -517,7 +520,7 @@ static size_t pppoe_tx_frame(struct mg_tcpip_if *ifp, uint8_t code, uint16_t id,
|
|||||||
struct mg_l2addr *dst) {
|
struct mg_l2addr *dst) {
|
||||||
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
|
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
|
||||||
uint8_t *p = pppoe_header(ifp, MG_TCPIP_L2PROTO_PPPoE_DISC, code, id,
|
uint8_t *p = pppoe_header(ifp, MG_TCPIP_L2PROTO_PPPoE_DISC, code, id,
|
||||||
(struct mg_l2addr *) ifp->mac, dst, l2p);
|
(struct mg_l2addr *) ifp->mac, dst, l2p, 0);
|
||||||
memmove(p, data, datasz);
|
memmove(p, data, datasz);
|
||||||
return mg_l2_driver_output(ifp, pppoe_trailer(ifp, datasz, p + datasz));
|
return mg_l2_driver_output(ifp, pppoe_trailer(ifp, datasz, p + datasz));
|
||||||
}
|
}
|
||||||
|
|||||||
+39
-43
@@ -227,7 +227,7 @@ struct pkt {
|
|||||||
// L2 API
|
// L2 API
|
||||||
void mg_l2_init(struct mg_tcpip_if *ifp);
|
void mg_l2_init(struct mg_tcpip_if *ifp);
|
||||||
uint8_t *mg_l2_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
uint8_t *mg_l2_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
|
||||||
uint8_t *src, uint8_t *dst, uint8_t *frame);
|
uint8_t *src, uint8_t *dst, uint8_t *frame, uint8_t dscp);
|
||||||
size_t mg_l2_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *cur);
|
size_t mg_l2_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *cur);
|
||||||
bool mg_l2_rx(struct mg_tcpip_if *ifp, enum mg_l2proto *proto,
|
bool mg_l2_rx(struct mg_tcpip_if *ifp, enum mg_l2proto *proto,
|
||||||
struct mg_str *pay, struct mg_str *raw);
|
struct mg_str *pay, struct mg_str *raw);
|
||||||
@@ -380,7 +380,6 @@ static const struct mg_addr ip6_allnodes = {
|
|||||||
static const uint8_t locpx[8] = {0xfe, 0x80, 0, 0, 0, 0, 0, 0};
|
static const uint8_t locpx[8] = {0xfe, 0x80, 0, 0, 0, 0, 0, 0};
|
||||||
|
|
||||||
#define MG_IP6MATCH(a, b) (a[0] == b[0] && a[1] == b[1])
|
#define MG_IP6MATCH(a, b) (a[0] == b[0] && a[1] == b[1])
|
||||||
#define MG_IP6UNSPEC(a) (a[0] == 0 && a[1] == 0)
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static void settmout(struct mg_connection *c, uint8_t type) {
|
static void settmout(struct mg_connection *c, uint8_t type) {
|
||||||
@@ -408,7 +407,7 @@ void mg_tcpip_arp_request(struct mg_tcpip_if *ifp, uint32_t ip, uint8_t *mac) {
|
|||||||
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
|
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
|
||||||
struct arp *arp = (struct arp *) mg_l2_header(
|
struct arp *arp = (struct arp *) mg_l2_header(
|
||||||
ifp, MG_TCPIP_L2PROTO_ARP, ifp->mac,
|
ifp, MG_TCPIP_L2PROTO_ARP, ifp->mac,
|
||||||
mg_l2_mapip(ifp->l2type, MG_TCPIP_L2ADDR_BCAST, NULL), l2p);
|
mg_l2_mapip(ifp->l2type, MG_TCPIP_L2ADDR_BCAST, NULL), l2p, 0);
|
||||||
memset(arp, 0, sizeof(*arp));
|
memset(arp, 0, sizeof(*arp));
|
||||||
arp->fmt = mg_htons(1), arp->pro = mg_htons(0x800), arp->hlen = 6,
|
arp->fmt = mg_htons(1), arp->pro = mg_htons(0x800), arp->hlen = 6,
|
||||||
arp->plen = 4;
|
arp->plen = 4;
|
||||||
@@ -455,7 +454,7 @@ static struct ip *tx_ip(struct mg_tcpip_if *ifp, uint8_t *l2_dst, uint8_t proto,
|
|||||||
// ifp->tx.buf is 8-bit aligned, keep other headers as pointers, see pkt
|
// ifp->tx.buf is 8-bit aligned, keep other headers as pointers, see pkt
|
||||||
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
|
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
|
||||||
struct ip *ip = (struct ip *) mg_l2_header(ifp, MG_TCPIP_L2PROTO_IPV4,
|
struct ip *ip = (struct ip *) mg_l2_header(ifp, MG_TCPIP_L2PROTO_IPV4,
|
||||||
ifp->mac, l2_dst, l2p);
|
ifp->mac, l2_dst, l2p, dscp);
|
||||||
memset(ip, 0, sizeof(*ip));
|
memset(ip, 0, sizeof(*ip));
|
||||||
ip->ver = 0x45; // Version 4, header length 5 words
|
ip->ver = 0x45; // Version 4, header length 5 words
|
||||||
ip->tos = (uint8_t) (dscp << 2);
|
ip->tos = (uint8_t) (dscp << 2);
|
||||||
@@ -631,8 +630,9 @@ static void rx_arp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
|
|||||||
// ARP request. Make a response, then send
|
// ARP request. Make a response, then send
|
||||||
// MG_VERBOSE(("ARP req from %M", mg_print_ip4, &pkt->arp->spa));
|
// MG_VERBOSE(("ARP req from %M", mg_print_ip4, &pkt->arp->spa));
|
||||||
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
|
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
|
||||||
struct arp *arp = (struct arp *) mg_l2_header(
|
struct arp *arp =
|
||||||
ifp, MG_TCPIP_L2PROTO_ARP, ifp->mac, mg_l2_getaddr(ifp, pkt->l2), l2p);
|
(struct arp *) mg_l2_header(ifp, MG_TCPIP_L2PROTO_ARP, ifp->mac,
|
||||||
|
mg_l2_getaddr(ifp, pkt->l2), l2p, 0);
|
||||||
*arp = *pkt->arp;
|
*arp = *pkt->arp;
|
||||||
arp->op = mg_htons(2);
|
arp->op = mg_htons(2);
|
||||||
memcpy(arp->tha, pkt->arp->sha, sizeof(pkt->arp->tha));
|
memcpy(arp->tha, pkt->arp->sha, sizeof(pkt->arp->tha));
|
||||||
@@ -830,7 +830,7 @@ static struct ip6 *tx_ip6(struct mg_tcpip_if *ifp, uint8_t *l2_dst,
|
|||||||
// ifp->tx.buf is 8-bit aligned, keep other headers as pointers, see pkt
|
// ifp->tx.buf is 8-bit aligned, keep other headers as pointers, see pkt
|
||||||
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
|
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
|
||||||
struct ip6 *ip6 = (struct ip6 *) mg_l2_header(ifp, MG_TCPIP_L2PROTO_IPV6,
|
struct ip6 *ip6 = (struct ip6 *) mg_l2_header(ifp, MG_TCPIP_L2PROTO_IPV6,
|
||||||
ifp->mac, l2_dst, l2p);
|
ifp->mac, l2_dst, l2p, dscp);
|
||||||
memset(ip6, 0, sizeof(*ip6));
|
memset(ip6, 0, sizeof(*ip6));
|
||||||
ip6->ver = (uint8_t) (0x60 | (dscp >> 2)); // Version 6, traffic class
|
ip6->ver = (uint8_t) (0x60 | (dscp >> 2)); // Version 6, traffic class
|
||||||
ip6->label[0] = (uint8_t) (dscp << 6);
|
ip6->label[0] = (uint8_t) (dscp << 6);
|
||||||
@@ -935,9 +935,9 @@ static void rx_ndp_ns(struct mg_tcpip_if *ifp, struct pkt *pkt) {
|
|||||||
// - use solicited node multicast to resolve a l2 address (l2_addr = NULL)
|
// - use solicited node multicast to resolve a l2 address (l2_addr = NULL)
|
||||||
// - use unicast to verify presence (l2_addr = neighbor l2 address)
|
// - use unicast to verify presence (l2_addr = neighbor l2 address)
|
||||||
static void tx_ndp_ns(struct mg_tcpip_if *ifp, uint64_t *ip_dst,
|
static void tx_ndp_ns(struct mg_tcpip_if *ifp, uint64_t *ip_dst,
|
||||||
uint8_t *l2_addr, bool ll) {
|
uint8_t *l2_addr) {
|
||||||
uint64_t *ip_src = ll ? ifp->ip6ll : ifp->ip6;
|
|
||||||
uint8_t payload[4 + 16 + 16]; // NOTE(): 16 --> optional len upto 2 hw addr
|
uint8_t payload[4 + 16 + 16]; // NOTE(): 16 --> optional len upto 2 hw addr
|
||||||
|
uint64_t ip_unspec[2] = {0, 0};
|
||||||
size_t payload_len = 20;
|
size_t payload_len = 20;
|
||||||
bool mcast = (l2_addr == NULL);
|
bool mcast = (l2_addr == NULL);
|
||||||
uint64_t ip_mcast[2] = {0, 0};
|
uint64_t ip_mcast[2] = {0, 0};
|
||||||
@@ -957,12 +957,14 @@ static void tx_ndp_ns(struct mg_tcpip_if *ifp, uint64_t *ip_dst,
|
|||||||
l2 = mg_l2_mapip(ifp->l2type, MG_TCPIP_L2ADDR_MCAST6, &ipd);
|
l2 = mg_l2_mapip(ifp->l2type, MG_TCPIP_L2ADDR_MCAST6, &ipd);
|
||||||
}
|
}
|
||||||
payload_len = 20;
|
payload_len = 20;
|
||||||
if (!MG_IP6UNSPEC(ip_src)) {
|
// TODO(robertc2000): using only link-local IP addr for now
|
||||||
|
// We might consider to add an option to use either link-local or global IP
|
||||||
|
if (!MG_IP6MATCH(ifp->ip6ll, ip_unspec)) {
|
||||||
payload[20] = 1; // 4.6.1, source hwaddr; option length in 8-byte units
|
payload[20] = 1; // 4.6.1, source hwaddr; option length in 8-byte units
|
||||||
payload[21] = mg_l2_ip6put(ifp->l2type, ifp->mac, payload + 22);
|
payload[21] = mg_l2_ip6put(ifp->l2type, ifp->mac, payload + 22);
|
||||||
payload_len += 8 * payload[21];
|
payload_len += 8 * payload[21];
|
||||||
}
|
}
|
||||||
tx_icmp6(ifp, l2, ip_src, mcast ? ip_mcast : ip_dst, 135, 0, payload,
|
tx_icmp6(ifp, l2, ifp->ip6ll, mcast ? ip_mcast : ip_dst, 135, 0, payload,
|
||||||
payload_len);
|
payload_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -970,10 +972,11 @@ static void tx_ndp_ns(struct mg_tcpip_if *ifp, uint64_t *ip_dst,
|
|||||||
static void tx_ndp_rs(struct mg_tcpip_if *ifp) {
|
static void tx_ndp_rs(struct mg_tcpip_if *ifp) {
|
||||||
uint8_t payload[4 + 16]; // reserved + optional len upto 2 hw addr NOTE()
|
uint8_t payload[4 + 16]; // reserved + optional len upto 2 hw addr NOTE()
|
||||||
size_t payload_len = 4;
|
size_t payload_len = 4;
|
||||||
|
uint64_t ip_unspec[2] = {0, 0};
|
||||||
|
|
||||||
memset(payload, 0, sizeof(payload));
|
memset(payload, 0, sizeof(payload));
|
||||||
|
|
||||||
if (!MG_IP6UNSPEC(ifp->ip6ll)) {
|
if (!MG_IP6MATCH(ifp->ip6ll, ip_unspec)) {
|
||||||
payload[4] = 1; // 4.6.1, source hwaddr; option length in 8-byte units
|
payload[4] = 1; // 4.6.1, source hwaddr; option length in 8-byte units
|
||||||
payload[5] = mg_l2_ip6put(ifp->l2type, ifp->mac, payload + 6);
|
payload[5] = mg_l2_ip6put(ifp->l2type, ifp->mac, payload + 6);
|
||||||
payload_len += 8 * payload[5];
|
payload_len += 8 * payload[5];
|
||||||
@@ -1142,7 +1145,7 @@ static void onstate6change(struct mg_tcpip_if *ifp) {
|
|||||||
(struct mg_addr *) &ip6_allnodes),
|
(struct mg_addr *) &ip6_allnodes),
|
||||||
ifp->ip6ll, (uint64_t *) ip6_allnodes.addr.ip6, false,
|
ifp->ip6ll, (uint64_t *) ip6_allnodes.addr.ip6, false,
|
||||||
ifp->mac);
|
ifp->mac);
|
||||||
if (!MG_IP6UNSPEC(ifp->ip6))
|
if (ifp->ip6[0] != 0 && ifp->ip6[1] != 0)
|
||||||
tx_ndp_na(ifp, // RFC-9131 4.1
|
tx_ndp_na(ifp, // RFC-9131 4.1
|
||||||
mg_l2_mapip(ifp->l2type, MG_TCPIP_L2ADDR_MCAST6,
|
mg_l2_mapip(ifp->l2type, MG_TCPIP_L2ADDR_MCAST6,
|
||||||
(struct mg_addr *) &ip6_allrouters),
|
(struct mg_addr *) &ip6_allrouters),
|
||||||
@@ -1157,9 +1160,9 @@ static void onstate6change(struct mg_tcpip_if *ifp) {
|
|||||||
ifp->is_ip6_changed = false;
|
ifp->is_ip6_changed = false;
|
||||||
}
|
}
|
||||||
} else if (ifp->state6 == MG_TCPIP_STATE_IP) {
|
} else if (ifp->state6 == MG_TCPIP_STATE_IP) {
|
||||||
if (!MG_IP6UNSPEC(ifp->gw6) &&
|
if ((ifp->gw6[0] != 0 || ifp->gw6[1] != 0) &&
|
||||||
(ifp->l2type != MG_TCPIP_L2_PPP && ifp->l2type != MG_TCPIP_L2_PPPoE))
|
(ifp->l2type != MG_TCPIP_L2_PPP && ifp->l2type != MG_TCPIP_L2_PPPoE))
|
||||||
tx_ndp_ns(ifp, ifp->gw6, NULL, true); // unsolicited GW hwaddr resolution
|
tx_ndp_ns(ifp, ifp->gw6, NULL); // unsolicited GW hwaddr resolution
|
||||||
} else if (ifp->state6 == MG_TCPIP_STATE_UP) {
|
} else if (ifp->state6 == MG_TCPIP_STATE_UP) {
|
||||||
MG_INFO(("IP: %M", mg_print_ip6, &ifp->ip6ll));
|
MG_INFO(("IP: %M", mg_print_ip6, &ifp->ip6ll));
|
||||||
}
|
}
|
||||||
@@ -1372,14 +1375,13 @@ static struct mg_connection *accept_conn(struct mg_connection *lsn,
|
|||||||
struct pkt *pkt, uint16_t mss) {
|
struct pkt *pkt, uint16_t mss) {
|
||||||
struct connstate *s;
|
struct connstate *s;
|
||||||
uint8_t *l2addr;
|
uint8_t *l2addr;
|
||||||
struct mg_tcpip_if *ifp = lsn->mgr->ifp;
|
|
||||||
struct mg_connection *c = mg_alloc_conn(lsn->mgr);
|
struct mg_connection *c = mg_alloc_conn(lsn->mgr);
|
||||||
if (c == NULL) {
|
if (c == NULL) {
|
||||||
MG_ERROR(("OOM"));
|
MG_ERROR(("OOM"));
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
s = (struct connstate *) (c + 1);
|
s = (struct connstate *) (c + 1);
|
||||||
s->retransmit = ifp->enable_tcp_retransmit;
|
s->retransmit = lsn->mgr->ifp->enable_tcp_retransmit;
|
||||||
s->txq.align = MG_TCPIP_TXQ_ALIGN;
|
s->txq.align = MG_TCPIP_TXQ_ALIGN;
|
||||||
s->dmss = mss; // from options in client SYN
|
s->dmss = mss; // from options in client SYN
|
||||||
s->seq = mg_ntohl(pkt->tcp->ack), s->ack = mg_ntohl(pkt->tcp->seq);
|
s->seq = mg_ntohl(pkt->tcp->ack), s->ack = mg_ntohl(pkt->tcp->seq);
|
||||||
@@ -1388,21 +1390,19 @@ static struct mg_connection *accept_conn(struct mg_connection *lsn,
|
|||||||
if (lsn->loc.is_ip6) {
|
if (lsn->loc.is_ip6) {
|
||||||
c->rem.addr.ip6[0] = pkt->ip6->src[0],
|
c->rem.addr.ip6[0] = pkt->ip6->src[0],
|
||||||
c->rem.addr.ip6[1] = pkt->ip6->src[1], c->rem.is_ip6 = true;
|
c->rem.addr.ip6[1] = pkt->ip6->src[1], c->rem.is_ip6 = true;
|
||||||
if (pkt->ip6->dst[0] == ifp->ip6ll[0]) { // link-local prefix
|
c->loc.addr.ip6[0] = c->mgr->ifp->ip6[0],
|
||||||
c->loc.addr.ip6[0] = ifp->ip6ll[0], c->loc.addr.ip6[1] = ifp->ip6ll[1];
|
c->loc.addr.ip6[1] = c->mgr->ifp->ip6[1], c->loc.is_ip6 = true;
|
||||||
} else {
|
// TODO(): compare lsn to link-local, or rem as link-local: use ll instead
|
||||||
c->loc.addr.ip6[0] = ifp->ip6[0], c->loc.addr.ip6[1] = ifp->ip6[1];
|
|
||||||
}
|
|
||||||
c->loc.is_ip6 = true;
|
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
c->rem.addr.ip4 = pkt->ip->src;
|
c->rem.addr.ip4 = pkt->ip->src;
|
||||||
c->loc.addr.ip4 = ifp->ip;
|
c->loc.addr.ip4 = c->mgr->ifp->ip;
|
||||||
}
|
}
|
||||||
c->rem.port = pkt->tcp->sport;
|
c->rem.port = pkt->tcp->sport;
|
||||||
c->loc.port = lsn->loc.port;
|
c->loc.port = lsn->loc.port;
|
||||||
if ((l2addr = get_return_l2addr(ifp, &c->rem, false, pkt)) == NULL) {
|
if ((l2addr = get_return_l2addr(lsn->mgr->ifp, &c->rem, false, pkt)) ==
|
||||||
|
NULL) {
|
||||||
mg_free(c); // safety net for lousy networks, not actually needed
|
mg_free(c); // safety net for lousy networks, not actually needed
|
||||||
return NULL; // as path has already been checked at SYN (sending SYN+ACK)
|
return NULL; // as path has already been checked at SYN (sending SYN+ACK)
|
||||||
}
|
}
|
||||||
@@ -1460,14 +1460,10 @@ static bool udp_send(struct mg_connection *c, const void *buf, size_t len) {
|
|||||||
struct mg_addr ips;
|
struct mg_addr ips;
|
||||||
memset(&ips, 0, sizeof(ips));
|
memset(&ips, 0, sizeof(ips));
|
||||||
#if MG_ENABLE_IPV6
|
#if MG_ENABLE_IPV6
|
||||||
if (c->loc.is_ip6) { // At [::] infer link-local from peer
|
if (c->loc.is_ip6) {
|
||||||
if (MG_IP6UNSPEC(c->loc.addr.ip6) ? c->rem.addr.ip6[0] == ifp->ip6ll[0]
|
ips.addr.ip6[0] = ifp->ip6[0], ips.addr.ip6[1] = ifp->ip6[1],
|
||||||
: c->loc.addr.ip6[0] == ifp->ip6ll[0]) {
|
|
||||||
ips.addr.ip6[0] = ifp->ip6ll[0], ips.addr.ip6[1] = ifp->ip6ll[1];
|
|
||||||
} else {
|
|
||||||
ips.addr.ip6[0] = ifp->ip6[0], ips.addr.ip6[1] = ifp->ip6[1];
|
|
||||||
}
|
|
||||||
ips.is_ip6 = true;
|
ips.is_ip6 = true;
|
||||||
|
// TODO(): detect link-local (c->rem) and use it
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
@@ -2128,14 +2124,14 @@ static void mg_ip6_link(struct mg_tcpip_if *ifp, bool drv_up, bool l2_up) {
|
|||||||
bool cur_drv = (ifp->state6 != MG_TCPIP_STATE_DOWN);
|
bool cur_drv = (ifp->state6 != MG_TCPIP_STATE_DOWN);
|
||||||
bool cur_l2 = (ifp->state6 >= MG_TCPIP_STATE_UP);
|
bool cur_l2 = (ifp->state6 >= MG_TCPIP_STATE_UP);
|
||||||
if (drv_up != cur_drv || l2_up != cur_l2) { // link/L2 state has changed
|
if (drv_up != cur_drv || l2_up != cur_l2) { // link/L2 state has changed
|
||||||
if (ifp->l2type == MG_TCPIP_L2_ETH && l2_up &&
|
if (ifp->l2type == MG_TCPIP_L2_ETH && l2_up && ifp->ip6ll[0] == 0 &&
|
||||||
MG_IP6UNSPEC(ifp->ip6ll)) { // gen ll address
|
ifp->ip6ll[1] == 0) { // gen ll address
|
||||||
mg_l2_genip6(ifp->l2type, ifp->ip6ll, 64, ifp->mac);
|
mg_l2_genip6(ifp->l2type, ifp->ip6ll, 64, ifp->mac);
|
||||||
memcpy(ifp->ip6ll, locpx, 8); // RFC-4291 2.5.6; RFC-2464 5
|
memcpy(ifp->ip6ll, locpx, 8); // RFC-4291 2.5.6; RFC-2464 5
|
||||||
} // just got our link local address if we didn't have one.
|
} // just got our link local address if we didn't have one.
|
||||||
// If static configuration is used, global addresses,
|
// If static configuration is used, global addresses,
|
||||||
// prefix length, and gw are already filled at this point.
|
// prefix length, and gw are already filled at this point.
|
||||||
if (MG_IP6UNSPEC(ifp->ip6)) ifp->enable_slaac = true;
|
if (ifp->ip6[0] == 0 && ifp->ip6[1] == 0) ifp->enable_slaac = true;
|
||||||
if (!l2_up && ifp->enable_slaac) ifp->ip6[0] = ifp->ip6[1] = 0;
|
if (!l2_up && ifp->enable_slaac) ifp->ip6[0] = ifp->ip6[1] = 0;
|
||||||
ifp->state6 = !drv_up ? MG_TCPIP_STATE_DOWN
|
ifp->state6 = !drv_up ? MG_TCPIP_STATE_DOWN
|
||||||
: !l2_up ? MG_TCPIP_STATE_LINK_UP
|
: !l2_up ? MG_TCPIP_STATE_LINK_UP
|
||||||
@@ -2196,8 +2192,8 @@ static void mg_tcpip_poll(struct mg_tcpip_if *ifp, uint64_t now) {
|
|||||||
onstate6change(ifp);
|
onstate6change(ifp);
|
||||||
}
|
}
|
||||||
if (expired_1000ms && ifp->state6 == MG_TCPIP_STATE_READY && !ifp->gw6_ready &&
|
if (expired_1000ms && ifp->state6 == MG_TCPIP_STATE_READY && !ifp->gw6_ready &&
|
||||||
!MG_IP6UNSPEC(ifp->gw6))
|
(ifp->gw6[0] != 0 || ifp->gw6[1] != 0))
|
||||||
tx_ndp_ns(ifp, ifp->gw6, NULL, true); // retry GW hwaddr resolution
|
tx_ndp_ns(ifp, ifp->gw6, NULL); // retry GW hwaddr resolution
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
{ // poll driver and let L2 do its work, if any
|
{ // poll driver and let L2 do its work, if any
|
||||||
@@ -2376,12 +2372,13 @@ void mg_connect_resolved(struct mg_connection *c) {
|
|||||||
} // otherwise retain an existing bound port
|
} // otherwise retain an existing bound port
|
||||||
#if MG_ENABLE_IPV6
|
#if MG_ENABLE_IPV6
|
||||||
if (c->rem.is_ip6) {
|
if (c->rem.is_ip6) {
|
||||||
bool any = MG_IP6UNSPEC(c->loc.addr.ip6);
|
if (c->loc.addr.ip6[0] == 0 && c->loc.addr.ip6[1] == 0 &&
|
||||||
if (any && c->rem.addr.ip6[0] == ifp->ip6ll[0]) { // same local LAN, use ll
|
c->rem.addr.ip6[0] == ifp->ip6ll[0]) { // same local LAN, use ll
|
||||||
c->loc.addr.ip6[0] = ifp->ip6ll[0], c->loc.addr.ip6[1] = ifp->ip6ll[1];
|
c->loc.addr.ip6[0] = ifp->ip6ll[0], c->loc.addr.ip6[1] = ifp->ip6ll[1];
|
||||||
} else if (any) { // Use global address
|
} else if (c->loc.addr.ip6[0] == 0 && c->loc.addr.ip6[1] == 0) {
|
||||||
|
// Use global address
|
||||||
c->loc.addr.ip6[0] = ifp->ip6[0], c->loc.addr.ip6[1] = ifp->ip6[1];
|
c->loc.addr.ip6[0] = ifp->ip6[0], c->loc.addr.ip6[1] = ifp->ip6[1];
|
||||||
} // otherwise retain an existing bound address
|
} // otherwise retain an existing bound address
|
||||||
c->loc.is_ip6 = true;
|
c->loc.is_ip6 = true;
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
@@ -2403,8 +2400,7 @@ void mg_connect_resolved(struct mg_connection *c) {
|
|||||||
&& !MG_IP6MATCH(c->rem.addr.ip6, ifp->gw6))) { // and not gw
|
&& !MG_IP6MATCH(c->rem.addr.ip6, ifp->gw6))) { // and not gw
|
||||||
// If we're in the same LAN, fire a Neighbor Solicitation
|
// If we're in the same LAN, fire a Neighbor Solicitation
|
||||||
MG_DEBUG(("%lu NS lookup...", c->id));
|
MG_DEBUG(("%lu NS lookup...", c->id));
|
||||||
tx_ndp_ns(ifp, c->rem.addr.ip6, NULL,
|
tx_ndp_ns(ifp, c->rem.addr.ip6, NULL); // RFC-4861 4.3, requesting
|
||||||
c->loc.addr.ip6[0] == ifp->ip6ll[0]); // RFC-4861 4.3, request
|
|
||||||
settmout(c, MIP_TTYPE_ARP);
|
settmout(c, MIP_TTYPE_ARP);
|
||||||
c->is_arplooking = 1;
|
c->is_arplooking = 1;
|
||||||
} else if (ifp->gw6_ready) {
|
} else if (ifp->gw6_ready) {
|
||||||
|
|||||||
@@ -317,7 +317,6 @@ static void mg_tls_derive_secret(const char *label, uint8_t *key, size_t keysz,
|
|||||||
size_t labelsz = strlen(label);
|
size_t labelsz = strlen(label);
|
||||||
uint8_t secret[32];
|
uint8_t secret[32];
|
||||||
uint8_t packed[256] = {0, (uint8_t) hashsz, (uint8_t) labelsz};
|
uint8_t packed[256] = {0, (uint8_t) hashsz, (uint8_t) labelsz};
|
||||||
// TODO: assert lengths of label, key, data and hash
|
|
||||||
if (labelsz > 0) memmove(packed + 3, label, labelsz);
|
if (labelsz > 0) memmove(packed + 3, label, labelsz);
|
||||||
packed[3 + labelsz] = (uint8_t) datasz;
|
packed[3 + labelsz] = (uint8_t) datasz;
|
||||||
if (datasz > 0) memmove(packed + labelsz + 4, data, datasz);
|
if (datasz > 0) memmove(packed + labelsz + 4, data, datasz);
|
||||||
|
|||||||
@@ -328,8 +328,6 @@ static void init_tests(struct mg_mgr *mgr, struct eth *e, struct ipp *ipp,
|
|||||||
mif->driver_data = &s_driver_data;
|
mif->driver_data = &s_driver_data;
|
||||||
#if MG_ENABLE_IPV6
|
#if MG_ENABLE_IPV6
|
||||||
if (ipp->ip6 != NULL) {
|
if (ipp->ip6 != NULL) {
|
||||||
mif->ip6ll[0] = mg_htonll(0xfe80000000000000);
|
|
||||||
mif->ip6ll[1] = mg_htonll(1);
|
|
||||||
mif->ip6[0] = mg_htonll(0x100000000000000);
|
mif->ip6[0] = mg_htonll(0x100000000000000);
|
||||||
mif->prefix[0] = 1;
|
mif->prefix[0] = 1;
|
||||||
mif->prefix_len = 64;
|
mif->prefix_len = 64;
|
||||||
@@ -666,16 +664,6 @@ static void test_tcp_basics(bool ipv6) {
|
|||||||
ASSERT((t->ack == mg_htonl(1002)));
|
ASSERT((t->ack == mg_htonl(1002)));
|
||||||
// make sure it is closed
|
// make sure it is closed
|
||||||
ASSERT(mgr.conns->next == NULL); // only one connection: the listener
|
ASSERT(mgr.conns->next == NULL); // only one connection: the listener
|
||||||
#if MG_ENABLE_IPV6
|
|
||||||
// Test we use link-local address for link-local peers
|
|
||||||
if (ipv6) {
|
|
||||||
ip6.src[0] = mif.ip6ll[0], ip6.src[1] = mg_htonll(2);
|
|
||||||
ip6.dst[0] = mif.ip6ll[0], ip6.dst[1] = mif.ip6ll[1];
|
|
||||||
init_tcp_handshake(&e, &ipp, &mgr);
|
|
||||||
ASSERT(mgr.conns->loc.addr.ip6[0] == mif.ip6ll[0] &&
|
|
||||||
mgr.conns->loc.addr.ip6[1] == mif.ip6ll[1]);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
s_driver_data.len = 0;
|
s_driver_data.len = 0;
|
||||||
mg_mgr_free(&mgr);
|
mg_mgr_free(&mgr);
|
||||||
@@ -1525,18 +1513,6 @@ static void test_udp(bool ipv6) {
|
|||||||
ASSERT((uint8_t) (((i6->ver & 15) << 2) | (i6->label[0] >> 6)) ==
|
ASSERT((uint8_t) (((i6->ver & 15) << 2) | (i6->label[0] >> 6)) ==
|
||||||
MG_DSCP_AF(4, 1));
|
MG_DSCP_AF(4, 1));
|
||||||
ASSERT(udp6csum_ok(i6, u));
|
ASSERT(udp6csum_ok(i6, u));
|
||||||
#if MG_ENABLE_IPV6
|
|
||||||
// Test we use link-local address for link-local peers
|
|
||||||
mgr.conns->recv.len = 0;
|
|
||||||
ip6.src[0] = mif.ip6ll[0], ip6.src[1] = mg_htonll(2);
|
|
||||||
ip6.dst[0] = mif.ip6ll[0], ip6.dst[1] = mif.ip6ll[1];
|
|
||||||
s_driver_data.len = 0;
|
|
||||||
create_udp_dat(&e, &ipp, 1, 888, 2);
|
|
||||||
mg_mgr_poll(&mgr, 0);
|
|
||||||
ASSERT(received_response(&s_driver_data));
|
|
||||||
ASSERT(i6->src[0] == mif.ip6ll[0] && i6->src[1] == mif.ip6ll[1] &&
|
|
||||||
i6->dst[0] == ip6.src[0] && i6->dst[1] == ip6.src[1]);
|
|
||||||
#endif
|
|
||||||
} else {
|
} else {
|
||||||
ASSERT(i->src == mg_htonl(0x1000000) && i->dst == mg_htonl(0x2000000));
|
ASSERT(i->src == mg_htonl(0x1000000) && i->dst == mg_htonl(0x2000000));
|
||||||
ASSERT(i->tos == (MG_DSCP_AF(4, 1) << 2));
|
ASSERT(i->tos == (MG_DSCP_AF(4, 1) << 2));
|
||||||
|
|||||||
Reference in new issue
Block a user