Decouple from Ethernet, introduce "l2"

This commit is contained in:
Sergio R. Caprile committed 2025-12-12 13:46:20 -03:00
1 parent c1298973e1
commit 66d4069644
11 files changed
+1717 -774

No files matched your search

+765 -349
View File
File diff suppressed because it is too large. Load diff
+103 -42
View File
@@ -1242,6 +1242,7 @@ size_t mg_print_ip_port(void (*out)(char, void *), void *arg, va_list *ap);
size_t mg_print_ip4(void (*out)(char, void *), void *arg, va_list *ap);
size_t mg_print_ip6(void (*out)(char, void *), void *arg, va_list *ap);
size_t mg_print_mac(void (*out)(char, void *), void *arg, va_list *ap);
size_t mg_print_l2addr(void (*out)(char, void *), void *arg, va_list *ap);
// Various output functions
void mg_pfn_iobuf(char ch, void *param); // param: struct mg_iobuf *
@@ -3148,6 +3149,62 @@ bool mg_wifi_ap_stop(void);
#if MG_ENABLE_TCPIP
// no config defaults to 0 => Ethernet
enum mg_l2type { MG_TCPIP_L2_ETH = 0, MG_TCPIP_L2_PPP }; // MG_TCPIP_L2_PPPoE
#if defined(__DCC__)
#pragma pack(1)
#else
#pragma pack(push, 1)
#endif
struct mg_l2addr {
union {
uint8_t mac[6];
} addr;
};
#if defined(__DCC__)
#pragma pack(0)
#else
#pragma pack(pop)
#endif
#if 0
TODO(): ?
struct eth_opts {
bool enable_crc32_check; // Do a CRC check on RX frames and strip it
bool enable_mac_check; // Do a MAC check on RX frames
};
struct mg_l2opts {
union {
struct eth_opts eth;
};
};
#endif
enum mg_l2proto {
MG_TCPIP_L2PROTO_IPV4 = 0,
MG_TCPIP_L2PROTO_IPV6,
MG_TCPIP_L2PROTO_ARP,
MG_TCPIP_L2PROTO_PPPoE_DISC,
MG_TCPIP_L2PROTO_PPPoE_SESS
};
enum mg_l2addrtype {
MG_TCPIP_L2ADDR_BCAST,
MG_TCPIP_L2ADDR_MCAST,
MG_TCPIP_L2ADDR_MCAST6
};
#endif
#if MG_ENABLE_TCPIP
@@ -3165,29 +3222,32 @@ typedef void (*mg_tcpip_event_handler_t)(struct mg_tcpip_if *ifp, int ev,
void *ev_data);
enum {
MG_TCPIP_EV_ST_CHG, // state change uint8_t * (&ifp->state)
MG_TCPIP_EV_DHCP_DNS, // DHCP DNS assignment uint32_t *ipaddr
MG_TCPIP_EV_DHCP_SNTP, // DHCP SNTP assignment uint32_t *ipaddr
MG_TCPIP_EV_ARP, // Got ARP packet struct mg_str *
MG_TCPIP_EV_TIMER_1S, // 1 second timer NULL
MG_TCPIP_EV_WIFI_SCAN_RESULT, // Wi-Fi scan results struct mg_wifi_scan_bss_data *
MG_TCPIP_EV_WIFI_SCAN_END, // Wi-Fi scan has finished NULL
MG_TCPIP_EV_WIFI_CONNECT_ERR, // Wi-Fi connect has failed driver and chip specific
MG_TCPIP_EV_DRIVER, // Driver event driver specific
MG_TCPIP_EV_ST6_CHG, // state6 change uint8_t * (&ifp->state6)
MG_TCPIP_EV_USER // Starting ID for user events
MG_TCPIP_EV_ST_CHG, // state change uint8_t * (&ifp->state)
MG_TCPIP_EV_DHCP_DNS, // DHCP DNS assignment uint32_t *ipaddr
MG_TCPIP_EV_DHCP_SNTP, // DHCP SNTP assignment uint32_t *ipaddr
MG_TCPIP_EV_ARP, // Got ARP packet struct mg_str *
MG_TCPIP_EV_TIMER_1S, // 1 second timer NULL
MG_TCPIP_EV_WIFI_SCAN_RESULT, // Wi-Fi scan results struct
// mg_wifi_scan_bss_data *
MG_TCPIP_EV_WIFI_SCAN_END, // Wi-Fi scan has finished NULL
MG_TCPIP_EV_WIFI_CONNECT_ERR, // Wi-Fi connect has failed driver and
// chip specific
MG_TCPIP_EV_DRIVER, // Driver event driver specific
MG_TCPIP_EV_ST6_CHG, // state6 change uint8_t *
// (&ifp->state6)
MG_TCPIP_EV_USER // Starting ID for user events
};
// Network interface
struct mg_tcpip_if {
uint8_t mac[6]; // MAC address. Must be set to a valid MAC
uint32_t ip, mask, gw; // IP address, mask, default gateway
struct mg_str tx; // Output (TX) buffer
bool enable_dhcp_client; // Enable DCHP client
bool enable_dhcp_server; // Enable DCHP server
bool enable_get_gateway; // DCHP server sets client as gateway
bool enable_req_dns; // DCHP client requests DNS server
bool enable_req_sntp; // DCHP client requests SNTP server
uint8_t mac[sizeof(struct mg_l2addr)]; // hw address. Set to a valid addr
uint32_t ip, mask, gw; // IP address, mask, default gateway
struct mg_str tx; // Output (TX) buffer
bool enable_dhcp_client; // Enable DCHP client
bool enable_dhcp_server; // Enable DCHP server
bool enable_get_gateway; // DCHP server sets client as gateway
bool enable_req_dns; // DCHP client requests DNS server
bool enable_req_sntp; // DCHP client requests SNTP server
bool enable_crc32_check; // Do a CRC check on RX frames and strip it
bool enable_mac_check; // Do a MAC check on RX frames
bool update_mac_hash_table; // Signal drivers to update MAC controller
@@ -3198,21 +3258,22 @@ struct mg_tcpip_if {
struct mg_mgr *mgr; // Mongoose event manager
struct mg_queue recv_queue; // Receive queue
char dhcp_name[MG_TCPIP_DHCPNAME_SIZE]; // Name for DHCP, "mip" if unset
uint16_t mtu; // Interface MTU
#define MG_TCPIP_MTU_DEFAULT 1500
uint16_t mtu; // Interface link payload
uint16_t framesize; // Interface frame max length
#if MG_ENABLE_IPV6
uint64_t ip6ll[2], ip6[2]; // IPv6 link-local and global addresses
uint8_t prefix[8]; // IPv6 global address prefix
uint8_t prefix_len; // Prefix length
uint64_t gw6[2]; // Default gateway
bool enable_slaac; // Enable IPv6 address autoconfiguration
bool enable_dhcp6_client; // Enable DCHPv6 client
uint64_t ip6ll[2], ip6[2]; // IPv6 link-local and global addresses,
uint8_t prefix[8]; // prefix,
uint8_t prefix_len; // prefix length,
uint64_t gw6[2]; // default gateway.
bool enable_slaac; // Enable IPv6 address autoconfiguration
bool enable_dhcp6_client; // Enable DCHPv6 client TODO()
#endif
// Internal state, user can use it but should not change it
uint8_t gwmac[6]; // Router's MAC
char *dns4_url; // DNS server URL
uint64_t now; // Current time
uint8_t gwmac[sizeof(struct mg_l2addr)]; // Router's hw address
enum mg_l2type l2type; // Ethernet, PPP, etc.
char *dns4_url; // DNS server URL
uint64_t now; // Current time
uint64_t timer_1000ms; // 1000 ms timer: for DHCP and link state
uint64_t lease_expire; // Lease expiration time, in ms
uint16_t eport; // Next ephemeral port
@@ -3228,11 +3289,12 @@ struct mg_tcpip_if {
#define MG_TCPIP_STATE_READY 4 // Interface has fully come up, ready to work
bool gw_ready; // We've got a hw address for the router
#if MG_ENABLE_IPV6
uint8_t gw6mac[6]; // IPv6 Router's MAC
uint8_t state6; // Current IPv6 state
bool gw6_ready; // We've got a hw address for the IPv6 router
uint8_t gw6mac[sizeof(struct mg_l2addr)]; // IPV6 Router's hw address
uint8_t state6; // Current IPv6 state
bool gw6_ready; // We've got a hw address for the IPv6 router
#endif
};
void mg_tcpip_init(struct mg_mgr *, struct mg_tcpip_if *);
void mg_tcpip_free(struct mg_tcpip_if *);
void mg_tcpip_qwrite(void *buf, size_t len, struct mg_tcpip_if *ifp);
@@ -3264,39 +3326,38 @@ struct mg_tcpip_spi {
uint8_t (*txn)(void *, uint8_t); // SPI transaction: write 1 byte, read reply
};
// Alignment and memory section requirements
#ifndef MG_8BYTE_ALIGNED
#if defined(__GNUC__)
#define MG_8BYTE_ALIGNED __attribute__((aligned((8U))))
#else
#define MG_8BYTE_ALIGNED
#endif // compiler
#endif // 8BYTE_ALIGNED
#endif // compiler
#endif // 8BYTE_ALIGNED
#ifndef MG_16BYTE_ALIGNED
#if defined(__GNUC__)
#define MG_16BYTE_ALIGNED __attribute__((aligned((16U))))
#else
#define MG_16BYTE_ALIGNED
#endif // compiler
#endif // 16BYTE_ALIGNED
#endif // compiler
#endif // 16BYTE_ALIGNED
#ifndef MG_32BYTE_ALIGNED
#if defined(__GNUC__)
#define MG_32BYTE_ALIGNED __attribute__((aligned((32U))))
#else
#define MG_32BYTE_ALIGNED
#endif // compiler
#endif // 32BYTE_ALIGNED
#endif // compiler
#endif // 32BYTE_ALIGNED
#ifndef MG_64BYTE_ALIGNED
#if defined(__GNUC__)
#define MG_64BYTE_ALIGNED __attribute__((aligned((64U))))
#else
#define MG_64BYTE_ALIGNED
#endif // compiler
#endif // 64BYTE_ALIGNED
#endif // compiler
#endif // 64BYTE_ALIGNED
#ifndef MG_ETH_RAM
#define MG_ETH_RAM
+121
View File
@@ -0,0 +1,121 @@
#include "l2.h"
#include "net.h"
#include "net_builtin.h"
#if MG_ENABLE_TCPIP
// L2 API
void mg_l2_init(enum mg_l2type type, uint8_t *addr, uint16_t *mtu,
uint16_t *framesize);
uint8_t *mg_l2_header(enum mg_l2type type, enum mg_l2proto proto, uint8_t *src,
uint8_t *dst, uint8_t *frame);
size_t mg_l2_footer(enum mg_l2type type, size_t len, uint8_t *frame);
bool mg_l2_rx(struct mg_tcpip_if *ifp, enum mg_l2proto *proto,
struct mg_str *pay, struct mg_str *raw);
// TODO(): ? bool mg_l2_rx(enum mg_l2type type, struct mg_l2opts *opts, uint8_t
// *addr, enum mg_l2proto *proto, struct mg_str *pay, struct mg_str *raw);
uint8_t *mg_l2_getaddr(enum mg_l2type type, uint8_t *frame);
uint8_t *mg_l2_mapip(enum mg_l2type type, enum mg_l2addrtype addrtype,
struct mg_addr *ip);
bool mg_l2_genip6(enum mg_l2type type, uint64_t *ip6, uint8_t prefix_len,
uint8_t *addr);
bool mg_l2_ip6get(enum mg_l2type type, uint8_t *addr, uint8_t *opts,
uint8_t len);
uint8_t mg_l2_ip6put(enum mg_l2type type, uint8_t *addr, uint8_t *opts);
// clang-format off
extern void mg_l2_eth_init(struct mg_l2addr *, uint16_t *, uint16_t *);
extern uint8_t *mg_l2_eth_header(enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *);
extern size_t mg_l2_eth_footer(size_t, uint8_t *);
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(uint8_t *);
extern struct mg_l2addr *mg_l2_eth_mapip(enum mg_l2addrtype, struct mg_addr *);
extern bool mg_l2_eth_genip6(uint64_t *, uint8_t, struct mg_l2addr *);
extern bool mg_l2_eth_ip6get(struct mg_l2addr *, uint8_t *, uint8_t);
extern uint8_t mg_l2_eth_ip6put(struct mg_l2addr *, uint8_t *);
extern void mg_l2_ppp_init(struct mg_l2addr *, uint16_t *, uint16_t *);
extern uint8_t *mg_l2_ppp_header(enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *);
extern size_t mg_l2_ppp_footer(size_t, uint8_t *);
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(uint8_t *);
extern struct mg_l2addr *mg_l2_ppp_mapip(enum mg_l2addrtype, struct mg_addr *);
#if MG_ENABLE_IPV6
extern bool mg_l2_ppp_genip6(uint64_t *, uint8_t, struct mg_l2addr *);
extern bool mg_l2_ppp_ip6get(struct mg_l2addr *, uint8_t *, uint8_t);
extern uint8_t mg_l2_ppp_ip6put(struct mg_l2addr *, uint8_t *);
#endif
typedef void (*l2_init_fn)(struct mg_l2addr *, uint16_t *, uint16_t *);
typedef uint8_t *((*l2_header_fn)(enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *));
typedef size_t (*l2_footer_fn)(size_t, uint8_t *);
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)(uint8_t *));
typedef struct mg_l2addr (*(*l2_mapip_fn)(enum mg_l2addrtype, struct mg_addr *));
#if MG_ENABLE_IPV6
typedef bool (*l2_genip6_fn)(uint64_t *, uint8_t, struct mg_l2addr *);
typedef bool (*l2_ip6get_fn)(struct mg_l2addr *, uint8_t *, uint8_t);
typedef uint8_t (*l2_ip6put_fn)(struct mg_l2addr *, uint8_t *);
#endif
// clang-format on
static const l2_init_fn l2_init[] = {mg_l2_eth_init, mg_l2_ppp_init};
static const l2_header_fn l2_header[] = {mg_l2_eth_header, mg_l2_ppp_header};
static const l2_footer_fn l2_footer[] = {mg_l2_eth_footer, mg_l2_ppp_footer};
static const l2_rx_fn l2_rx[] = {mg_l2_eth_rx, mg_l2_ppp_rx};
static const l2_getaddr_fn l2_getaddr[] = {mg_l2_eth_getaddr,
mg_l2_ppp_getaddr};
static const l2_mapip_fn l2_mapip[] = {mg_l2_eth_mapip, mg_l2_ppp_mapip};
#if MG_ENABLE_IPV6
static const l2_genip6_fn l2_genip6[] = {mg_l2_eth_genip6, mg_l2_ppp_genip6};
static const l2_ip6get_fn l2_ip6get[] = {mg_l2_eth_ip6get, mg_l2_ppp_ip6get};
static const l2_ip6put_fn l2_ip6put[] = {mg_l2_eth_ip6put, mg_l2_ppp_ip6put};
#endif
void mg_l2_init(enum mg_l2type type, uint8_t *addr, uint16_t *mtu,
uint16_t *framesize) {
l2_init[type]((struct mg_l2addr *) addr, mtu, framesize);
}
uint8_t *mg_l2_header(enum mg_l2type type, enum mg_l2proto proto, uint8_t *src,
uint8_t *dst, uint8_t *frame) {
return l2_header[type](proto, (struct mg_l2addr *) src,
(struct mg_l2addr *) dst, frame);
}
size_t mg_l2_footer(enum mg_l2type type, size_t len, uint8_t *frame) {
return l2_footer[type](len, frame);
}
bool mg_l2_rx(struct mg_tcpip_if *ifp, enum mg_l2proto *proto,
struct mg_str *pay, struct mg_str *raw) {
return l2_rx[ifp->l2type](ifp, proto, pay, raw);
}
uint8_t *mg_l2_getaddr(enum mg_l2type type, uint8_t *frame) {
return (uint8_t *) l2_getaddr[type](frame);
}
struct mg_l2addr s_mapip;
uint8_t *mg_l2_mapip(enum mg_l2type type, enum mg_l2addrtype addrtype,
struct mg_addr *ip) {
return (uint8_t *) l2_mapip[type](addrtype, ip);
}
#if MG_ENABLE_IPV6
bool mg_l2_genip6(enum mg_l2type type, uint64_t *ip6, uint8_t prefix_len,
uint8_t *addr) {
return l2_genip6[type](ip6, prefix_len, (struct mg_l2addr *) addr);
}
bool mg_l2_ip6get(enum mg_l2type type, uint8_t *addr, uint8_t *opts,
uint8_t len) {
return l2_ip6get[type]((struct mg_l2addr *) addr, opts, len);
}
uint8_t mg_l2_ip6put(enum mg_l2type type, uint8_t *addr, uint8_t *opts) {
return l2_ip6put[type]((struct mg_l2addr *) addr, opts);
}
#endif
#endif
+55
View File
@@ -0,0 +1,55 @@
#pragma once
#include "arch.h"
#include "config.h"
#if MG_ENABLE_TCPIP
// no config defaults to 0 => Ethernet
enum mg_l2type { MG_TCPIP_L2_ETH = 0, MG_TCPIP_L2_PPP }; // MG_TCPIP_L2_PPPoE
#if defined(__DCC__)
#pragma pack(1)
#else
#pragma pack(push, 1)
#endif
struct mg_l2addr {
union {
uint8_t mac[6];
} addr;
};
#if defined(__DCC__)
#pragma pack(0)
#else
#pragma pack(pop)
#endif
#if 0
TODO(): ?
struct eth_opts {
bool enable_crc32_check; // Do a CRC check on RX frames and strip it
bool enable_mac_check; // Do a MAC check on RX frames
};
struct mg_l2opts {
union {
struct eth_opts eth;
};
};
#endif
enum mg_l2proto {
MG_TCPIP_L2PROTO_IPV4 = 0,
MG_TCPIP_L2PROTO_IPV6,
MG_TCPIP_L2PROTO_ARP,
MG_TCPIP_L2PROTO_PPPoE_DISC,
MG_TCPIP_L2PROTO_PPPoE_SESS
};
enum mg_l2addrtype {
MG_TCPIP_L2ADDR_BCAST,
MG_TCPIP_L2ADDR_MCAST,
MG_TCPIP_L2ADDR_MCAST6
};
#endif
+171
View File
@@ -0,0 +1,171 @@
#include "l2.h"
#include "log.h"
#include "net.h"
#include "net_builtin.h"
#include "printf.h"
#include "util.h"
#if MG_ENABLE_TCPIP
#if defined(__DCC__)
#pragma pack(1)
#else
#pragma pack(push, 1)
#endif
struct eth {
uint8_t dst[6]; // Destination MAC address
uint8_t src[6]; // Source MAC address
uint16_t type; // Ethernet type
};
#if defined(__DCC__)
#pragma pack(0)
#else
#pragma pack(pop)
#endif
static const uint16_t eth_types[] = {
// order is vital, see l2.h
0x800, // IPv4
0x86dd, // IPv6
0x806, // ARP
0x8863, // PPPoE Discovery Stage
0x8864 // PPPoE Session Stage
};
void mg_l2_eth_init(struct mg_l2addr *l2addr, uint16_t *mtu,
uint16_t *framesize) {
// If MAC is not set, make a random one
if (l2addr->addr.mac[0] == 0 && l2addr->addr.mac[1] == 0 &&
l2addr->addr.mac[2] == 0 && l2addr->addr.mac[3] == 0 &&
l2addr->addr.mac[4] == 0 && l2addr->addr.mac[5] == 0) {
l2addr->addr.mac[0] = 0x02; // Locally administered, unicast
mg_random(&l2addr->addr.mac[1], sizeof(l2addr->addr.mac) - 1);
MG_INFO(
("MAC not set. Generated random: %M", mg_print_mac, l2addr->addr.mac));
}
*mtu = 1500;
*framesize = 1540;
}
uint8_t *mg_l2_eth_header(enum mg_l2proto proto, struct mg_l2addr *src,
struct mg_l2addr *dst, uint8_t *frame) {
struct eth *eth = (struct eth *) frame;
eth->type = mg_htons(eth_types[(unsigned int) proto]);
memcpy(eth->src, src->addr.mac, sizeof(eth->dst));
memcpy(eth->dst, dst->addr.mac, sizeof(eth->dst));
return (uint8_t *) (eth + 1);
}
size_t mg_l2_eth_footer(size_t len, uint8_t *frame) {
struct eth *eth = (struct eth *) frame;
// nothing to do; there is no len field in Ethernet, CRC is hw-calculated
return len + sizeof(*eth);
}
struct mg_l2addr *mg_l2_eth_mapip(enum mg_l2addrtype addrtype,
struct mg_addr *addr);
bool mg_l2_eth_rx(struct mg_tcpip_if *ifp, enum mg_l2proto *proto,
struct mg_str *pay, struct mg_str *raw) {
struct eth *eth = (struct eth *) raw->buf;
uint16_t type, len;
unsigned int i;
if (raw->len < sizeof(*eth)) return false; // Truncated - runt?
len = (uint16_t) raw->len;
if (ifp->enable_mac_check &&
memcmp(eth->dst, ifp->mac, sizeof(eth->dst)) != 0 &&
memcmp(eth->dst, mg_l2_eth_mapip(MG_TCPIP_L2ADDR_BCAST, NULL),
sizeof(eth->dst)) != 0)
return false; // TODO(): add multicast addresses
if (ifp->enable_crc32_check && len > sizeof(*eth) + 4) {
uint32_t crc;
len -= 4; // TODO(scaprile): check on bigendian
crc = mg_crc32(0, (const char *) raw->buf, len);
if (memcmp((void *) ((size_t) raw->buf + len), &crc, sizeof(crc)))
return false;
}
pay->buf = (char *) (eth + 1);
pay->len = len;
type = mg_htons(eth->type);
for (i = 0; i < sizeof(eth_types) / sizeof(uint16_t); i++) {
if (type == eth_types[i]) break;
}
if (i == sizeof(eth_types)) {
MG_DEBUG(("Unknown eth type %x", type));
if (mg_log_level >= MG_LL_VERBOSE)
mg_hexdump(raw->buf, raw->len >= 32 ? 32 : raw->len);
return false;
}
*proto = (enum mg_l2proto) i;
return true;
}
struct mg_l2addr *mg_l2_eth_getaddr(uint8_t *frame) {
struct eth *eth = (struct eth *) frame;
return (struct mg_l2addr *) &eth->src;
}
extern struct mg_l2addr s_mapip;
struct mg_l2addr *mg_l2_eth_mapip(enum mg_l2addrtype addrtype,
struct mg_addr *addr) {
switch (addrtype) {
case MG_TCPIP_L2ADDR_BCAST:
memset(s_mapip.addr.mac, 0xff, sizeof(s_mapip.addr.mac));
break;
case MG_TCPIP_L2ADDR_MCAST: {
uint8_t *ip = (uint8_t *) &addr->addr.ip4;
// IP multicast group MAC, RFC-1112 6.4
s_mapip.addr.mac[0] = 0x01, s_mapip.addr.mac[1] = 0x00,
s_mapip.addr.mac[2] = 0x5E;
s_mapip.addr.mac[3] = ip[1] & 0x7F; // 23 LSb
s_mapip.addr.mac[4] = ip[2];
s_mapip.addr.mac[5] = ip[3];
break;
}
case MG_TCPIP_L2ADDR_MCAST6: {
// IPv6 multicast address mapping, RFC-2464 7
uint8_t *ip = (uint8_t *) &addr->addr.ip6;
s_mapip.addr.mac[0] = 0x33, s_mapip.addr.mac[1] = 0x33;
s_mapip.addr.mac[2] = ip[12], s_mapip.addr.mac[3] = ip[13],
s_mapip.addr.mac[4] = ip[14], s_mapip.addr.mac[5] = ip[15];
break;
}
}
return &s_mapip;
}
#if MG_ENABLE_IPV6
static void meui64(uint8_t *addr, uint8_t *mac) {
*addr++ = *mac++ ^ (uint8_t) 0x02, *addr++ = *mac++, *addr++ = *mac++;
*addr++ = 0xff, *addr++ = 0xfe;
*addr++ = *mac++, *addr++ = *mac++, *addr = *mac;
}
bool mg_l2_eth_genip6(uint64_t *ip6, uint8_t prefix_len,
struct mg_l2addr *l2addr) {
if (prefix_len > 64) {
MG_ERROR(("Prefix length > 64, UNSUPPORTED"));
return false;
}
ip6[0] = 0;
meui64(((uint8_t *) &ip6[1]), l2addr->addr.mac); // RFC-4291 2.5.4, 2.5.1
return true;
}
bool mg_l2_eth_ip6get(struct mg_l2addr *l2addr, uint8_t *opts, uint8_t len) {
if (len != 1) return false;
memcpy(l2addr->addr.mac, opts, 6);
return true;
}
uint8_t mg_l2_eth_ip6put(struct mg_l2addr *l2addr, uint8_t *opts) {
memcpy(opts, l2addr->addr.mac, 6);
return 1;
}
#endif
#endif
+115
View File
@@ -0,0 +1,115 @@
#include "l2.h"
#include "log.h"
#include "net.h"
#include "net_builtin.h"
#include "printf.h"
#include "util.h"
#if MG_ENABLE_TCPIP
#if defined(__DCC__)
#pragma pack(1)
#else
#pragma pack(push, 1)
#endif
struct ppp { // RFC-1662
uint8_t addr, ctrl;
uint16_t proto;
};
struct lcp { // RFC-1661
uint8_t code, id, len[2];
};
struct ipcp { // RFC-1332
uint8_t code;
};
struct ipv6cp { // RFC-5072
uint8_t code;
};
#if defined(__DCC__)
#pragma pack(0)
#else
#pragma pack(pop)
#endif
void mg_l2_ppp_init(struct mg_l2addr *addr, uint16_t *mtu,
uint16_t *framesize) {
(void) addr;
*mtu = 1500; // 1492 for PPPoE
*framesize = 1540; // *** TODO(scaprile): actual value, check for PPPoE too
}
uint8_t *mg_l2_ppp_header(enum mg_l2proto proto, struct mg_l2addr *src,
struct mg_l2addr *dst, uint8_t *frame) {
(void) src;
(void) dst;
(void) proto;
return frame;
}
size_t mg_l2_ppp_footer(size_t len, uint8_t *frame) {
(void) frame;
return len;
}
bool mg_l2_ppp_rx(struct mg_tcpip_if *ifp, enum mg_l2proto *proto,
struct mg_str *pay, struct mg_str *raw) {
#if 0
if (ppp->addr == MG_PPP_ADDR && ppp->ctrl == MG_PPP_CTRL) {
code = ntohs(ppp->proto);
payload = (uint8_t *) (ppp + 1);
} else { // Address-and-Control-Field-Compressed PPP header
uint16_t *cppp = (uint16_t *) ppp;
code = ntohs(*cppp);
payload = (uint8_t *) (cppp + 1);
}
#endif
*pay = *raw;
*proto = MG_TCPIP_L2PROTO_IPV4;
(void) ifp;
return true;
}
struct mg_l2addr *mg_l2_ppp_getaddr(uint8_t *frame) {
(void) frame;
return &s_mapip; // bogus
}
extern struct mg_l2addr s_mapip;
struct mg_l2addr *mg_l2_ppp_mapip(enum mg_l2addrtype addrtype,
struct mg_addr *addr) {
(void) addrtype;
(void) addr;
return &s_mapip; // bogus
}
#if MG_ENABLE_IPV6
bool mg_l2_ppp_genip6(uint64_t *ip6, uint8_t prefix_len,
struct mg_l2addr *addr) {
(void) ip6;
(void) prefix_len;
(void) addr;
return false;
}
bool mg_l2_ppp_ip6get(struct mg_l2addr *addr, uint8_t *opts, uint8_t len) {
(void) addr;
(void) opts;
(void) len;
return false;
}
uint8_t mg_l2_ppp_ip6put(struct mg_l2addr *addr, uint8_t *opts) {
(void) addr;
(void) opts;
return 0;
}
#endif
#endif
+321 -338
View File
File diff suppressed because it is too large. Load diff
+47 -42
View File
@@ -1,6 +1,7 @@
#pragma once
#include "arch.h"
#include "l2.h"
#include "net.h"
#include "queue.h"
#include "str.h"
@@ -20,29 +21,32 @@ typedef void (*mg_tcpip_event_handler_t)(struct mg_tcpip_if *ifp, int ev,
void *ev_data);
enum {
MG_TCPIP_EV_ST_CHG, // state change uint8_t * (&ifp->state)
MG_TCPIP_EV_DHCP_DNS, // DHCP DNS assignment uint32_t *ipaddr
MG_TCPIP_EV_DHCP_SNTP, // DHCP SNTP assignment uint32_t *ipaddr
MG_TCPIP_EV_ARP, // Got ARP packet struct mg_str *
MG_TCPIP_EV_TIMER_1S, // 1 second timer NULL
MG_TCPIP_EV_WIFI_SCAN_RESULT, // Wi-Fi scan results struct mg_wifi_scan_bss_data *
MG_TCPIP_EV_WIFI_SCAN_END, // Wi-Fi scan has finished NULL
MG_TCPIP_EV_WIFI_CONNECT_ERR, // Wi-Fi connect has failed driver and chip specific
MG_TCPIP_EV_DRIVER, // Driver event driver specific
MG_TCPIP_EV_ST6_CHG, // state6 change uint8_t * (&ifp->state6)
MG_TCPIP_EV_USER // Starting ID for user events
MG_TCPIP_EV_ST_CHG, // state change uint8_t * (&ifp->state)
MG_TCPIP_EV_DHCP_DNS, // DHCP DNS assignment uint32_t *ipaddr
MG_TCPIP_EV_DHCP_SNTP, // DHCP SNTP assignment uint32_t *ipaddr
MG_TCPIP_EV_ARP, // Got ARP packet struct mg_str *
MG_TCPIP_EV_TIMER_1S, // 1 second timer NULL
MG_TCPIP_EV_WIFI_SCAN_RESULT, // Wi-Fi scan results struct
// mg_wifi_scan_bss_data *
MG_TCPIP_EV_WIFI_SCAN_END, // Wi-Fi scan has finished NULL
MG_TCPIP_EV_WIFI_CONNECT_ERR, // Wi-Fi connect has failed driver and
// chip specific
MG_TCPIP_EV_DRIVER, // Driver event driver specific
MG_TCPIP_EV_ST6_CHG, // state6 change uint8_t *
// (&ifp->state6)
MG_TCPIP_EV_USER // Starting ID for user events
};
// Network interface
struct mg_tcpip_if {
uint8_t mac[6]; // MAC address. Must be set to a valid MAC
uint32_t ip, mask, gw; // IP address, mask, default gateway
struct mg_str tx; // Output (TX) buffer
bool enable_dhcp_client; // Enable DCHP client
bool enable_dhcp_server; // Enable DCHP server
bool enable_get_gateway; // DCHP server sets client as gateway
bool enable_req_dns; // DCHP client requests DNS server
bool enable_req_sntp; // DCHP client requests SNTP server
uint8_t mac[sizeof(struct mg_l2addr)]; // hw address. Set to a valid addr
uint32_t ip, mask, gw; // IP address, mask, default gateway
struct mg_str tx; // Output (TX) buffer
bool enable_dhcp_client; // Enable DCHP client
bool enable_dhcp_server; // Enable DCHP server
bool enable_get_gateway; // DCHP server sets client as gateway
bool enable_req_dns; // DCHP client requests DNS server
bool enable_req_sntp; // DCHP client requests SNTP server
bool enable_crc32_check; // Do a CRC check on RX frames and strip it
bool enable_mac_check; // Do a MAC check on RX frames
bool update_mac_hash_table; // Signal drivers to update MAC controller
@@ -53,21 +57,22 @@ struct mg_tcpip_if {
struct mg_mgr *mgr; // Mongoose event manager
struct mg_queue recv_queue; // Receive queue
char dhcp_name[MG_TCPIP_DHCPNAME_SIZE]; // Name for DHCP, "mip" if unset
uint16_t mtu; // Interface MTU
#define MG_TCPIP_MTU_DEFAULT 1500
uint16_t mtu; // Interface link payload
uint16_t framesize; // Interface frame max length
#if MG_ENABLE_IPV6
uint64_t ip6ll[2], ip6[2]; // IPv6 link-local and global addresses
uint8_t prefix[8]; // IPv6 global address prefix
uint8_t prefix_len; // Prefix length
uint64_t gw6[2]; // Default gateway
bool enable_slaac; // Enable IPv6 address autoconfiguration
bool enable_dhcp6_client; // Enable DCHPv6 client
uint64_t ip6ll[2], ip6[2]; // IPv6 link-local and global addresses,
uint8_t prefix[8]; // prefix,
uint8_t prefix_len; // prefix length,
uint64_t gw6[2]; // default gateway.
bool enable_slaac; // Enable IPv6 address autoconfiguration
bool enable_dhcp6_client; // Enable DCHPv6 client TODO()
#endif
// Internal state, user can use it but should not change it
uint8_t gwmac[6]; // Router's MAC
char *dns4_url; // DNS server URL
uint64_t now; // Current time
uint8_t gwmac[sizeof(struct mg_l2addr)]; // Router's hw address
enum mg_l2type l2type; // Ethernet, PPP, etc.
char *dns4_url; // DNS server URL
uint64_t now; // Current time
uint64_t timer_1000ms; // 1000 ms timer: for DHCP and link state
uint64_t lease_expire; // Lease expiration time, in ms
uint16_t eport; // Next ephemeral port
@@ -83,11 +88,12 @@ struct mg_tcpip_if {
#define MG_TCPIP_STATE_READY 4 // Interface has fully come up, ready to work
bool gw_ready; // We've got a hw address for the router
#if MG_ENABLE_IPV6
uint8_t gw6mac[6]; // IPv6 Router's MAC
uint8_t state6; // Current IPv6 state
bool gw6_ready; // We've got a hw address for the IPv6 router
uint8_t gw6mac[sizeof(struct mg_l2addr)]; // IPV6 Router's hw address
uint8_t state6; // Current IPv6 state
bool gw6_ready; // We've got a hw address for the IPv6 router
#endif
};
void mg_tcpip_init(struct mg_mgr *, struct mg_tcpip_if *);
void mg_tcpip_free(struct mg_tcpip_if *);
void mg_tcpip_qwrite(void *buf, size_t len, struct mg_tcpip_if *ifp);
@@ -119,39 +125,38 @@ struct mg_tcpip_spi {
uint8_t (*txn)(void *, uint8_t); // SPI transaction: write 1 byte, read reply
};
// Alignment and memory section requirements
#ifndef MG_8BYTE_ALIGNED
#if defined(__GNUC__)
#define MG_8BYTE_ALIGNED __attribute__((aligned((8U))))
#else
#define MG_8BYTE_ALIGNED
#endif // compiler
#endif // 8BYTE_ALIGNED
#endif // compiler
#endif // 8BYTE_ALIGNED
#ifndef MG_16BYTE_ALIGNED
#if defined(__GNUC__)
#define MG_16BYTE_ALIGNED __attribute__((aligned((16U))))
#else
#define MG_16BYTE_ALIGNED
#endif // compiler
#endif // 16BYTE_ALIGNED
#endif // compiler
#endif // 16BYTE_ALIGNED
#ifndef MG_32BYTE_ALIGNED
#if defined(__GNUC__)
#define MG_32BYTE_ALIGNED __attribute__((aligned((32U))))
#else
#define MG_32BYTE_ALIGNED
#endif // compiler
#endif // 32BYTE_ALIGNED
#endif // compiler
#endif // 32BYTE_ALIGNED
#ifndef MG_64BYTE_ALIGNED
#if defined(__GNUC__)
#define MG_64BYTE_ALIGNED __attribute__((aligned((64U))))
#else
#define MG_64BYTE_ALIGNED
#endif // compiler
#endif // 64BYTE_ALIGNED
#endif // compiler
#endif // 64BYTE_ALIGNED
#ifndef MG_ETH_RAM
#define MG_ETH_RAM
+17 -2
View File
@@ -107,12 +107,27 @@ size_t mg_print_ip_port(void (*out)(char, void *), void *arg, va_list *ap) {
return mg_xprintf(out, arg, "%M:%hu", mg_print_ip, a, mg_ntohs(a->port));
}
size_t mg_print_mac(void (*out)(char, void *), void *arg, va_list *ap) {
uint8_t *p = va_arg(*ap, uint8_t *);
static size_t print_mac(void (*out)(char, void *), void *arg, uint8_t *p) {
return mg_xprintf(out, arg, "%02x:%02x:%02x:%02x:%02x:%02x", p[0], p[1], p[2],
p[3], p[4], p[5]);
}
size_t mg_print_mac(void (*out)(char, void *), void *arg, va_list *ap) {
uint8_t *p = va_arg(*ap, uint8_t *);
return print_mac(out, arg, p);
}
#if MG_ENABLE_TCPIP
size_t mg_print_l2addr(void (*out)(char, void *), void *arg, va_list *ap) {
enum mg_l2type type = (enum mg_l2type) va_arg(*ap, int);
if (type == MG_TCPIP_L2_ETH) {
uint8_t *p = va_arg(*ap, uint8_t *);
return print_mac(out, arg, p);
}
return 0;
}
#endif
static char mg_esc(int c, bool esc) {
const char *p, *esc1 = "\b\f\n\r\t\\\"", *esc2 = "bfnrt\\\"";
for (p = esc ? esc1 : esc2; *p != '\0'; p++) {
+1
View File
@@ -20,6 +20,7 @@ size_t mg_print_ip_port(void (*out)(char, void *), void *arg, va_list *ap);
size_t mg_print_ip4(void (*out)(char, void *), void *arg, va_list *ap);
size_t mg_print_ip6(void (*out)(char, void *), void *arg, va_list *ap);
size_t mg_print_mac(void (*out)(char, void *), void *arg, va_list *ap);
size_t mg_print_l2addr(void (*out)(char, void *), void *arg, va_list *ap);
// Various output functions
void mg_pfn_iobuf(char ch, void *param); // param: struct mg_iobuf *
+1 -1
View File
@@ -230,7 +230,7 @@ mongoose.c: Makefile $(wildcard ../src/*.c) $(wildcard ../src/drivers/*.c)
cd .. && (export LC_ALL=C ; cat src/license.h; echo; echo '#include "mongoose.h"' ; (for F in src/*.c src/drivers/*.c ; do echo; echo '#ifdef MG_ENABLE_LINES'; echo "#line 1 \"$$F\""; echo '#endif'; cat $$F | sed -e 's,#include ".*,,'; done))> $@
mongoose.h: $(HDRS) Makefile
cd .. && (cat src/license.h; echo; echo '#ifndef MONGOOSE_H'; echo '#define MONGOOSE_H'; echo; cat src/version.h ; echo; echo '#ifdef __cplusplus'; echo 'extern "C" {'; echo '#endif'; cat src/arch.h src/arch_*.h src/net_ft.h src/net_lwip.h src/net_rl.h src/config.h src/profile.h src/str.h src/queue.h src/fmt.h src/printf.h src/log.h src/timer.h src/fs.h src/util.h src/url.h src/iobuf.h src/base64.h src/md5.h src/sha1.h src/sha256.h src/event.h src/net.h src/http.h src/ssi.h src/tls.h src/tls_x25519.h src/tls_aes128.h src/tls_uecc.h src/tls_chacha20.h src/tls_rsa.h src/tls_mbed.h src/tls_openssl.h src/ws.h src/sntp.h src/mqtt.h src/dns.h src/json.h src/rpc.h src/ota.h src/flash.h src/wifi.h src/net_builtin.h src/drivers/*.h | sed -e '/keep/! s,#include ".*,,' -e 's,^#pragma once,,'; echo; echo '#ifdef __cplusplus'; echo '}'; echo '#endif'; echo '#endif // MONGOOSE_H')> $@
cd .. && (cat src/license.h; echo; echo '#ifndef MONGOOSE_H'; echo '#define MONGOOSE_H'; echo; cat src/version.h ; echo; echo '#ifdef __cplusplus'; echo 'extern "C" {'; echo '#endif'; cat src/arch.h src/arch_*.h src/net_ft.h src/net_lwip.h src/net_rl.h src/config.h src/profile.h src/str.h src/queue.h src/fmt.h src/printf.h src/log.h src/timer.h src/fs.h src/util.h src/url.h src/iobuf.h src/base64.h src/md5.h src/sha1.h src/sha256.h src/event.h src/net.h src/http.h src/ssi.h src/tls.h src/tls_x25519.h src/tls_aes128.h src/tls_uecc.h src/tls_chacha20.h src/tls_rsa.h src/tls_mbed.h src/tls_openssl.h src/ws.h src/sntp.h src/mqtt.h src/dns.h src/json.h src/rpc.h src/ota.h src/flash.h src/wifi.h src/l2.h src/net_builtin.h src/drivers/*.h | sed -e '/keep/! s,#include ".*,,' -e 's,^#pragma once,,'; echo; echo '#ifdef __cplusplus'; echo '}'; echo '#endif'; echo '#endif // MONGOOSE_H')> $@
clean: clean_tutorials clean_tutorials_embedded