net: Receive IPv4 packet
Check if we have received IPv4 packet and call a handler function to process it. Change-Id: I9f9e5f0888d2c3b91401c98f4925647ddce09962 Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
This commit is contained in:
parent
262a02fe67
commit
d24f673248
4 changed files with 71 additions and 20 deletions
|
@ -192,11 +192,6 @@ static void process_msg(struct slip_context *slip)
|
|||
return;
|
||||
}
|
||||
|
||||
#if defined(CONFIG_NET_IPV4)
|
||||
#error "FIXME - TBDL"
|
||||
#endif /* IPv4 */
|
||||
|
||||
#if defined(CONFIG_NET_IPV6)
|
||||
if (buf->frags) {
|
||||
if (net_recv(net_if_get_by_link_addr(&slip->ll_addr),
|
||||
buf) < 0) {
|
||||
|
@ -204,7 +199,6 @@ static void process_msg(struct slip_context *slip)
|
|||
}
|
||||
slip->rx = slip->last = NULL;
|
||||
}
|
||||
#endif /* CONFIG_NET_IPV6 */
|
||||
}
|
||||
|
||||
static inline int slip_input_byte(struct slip_context *slip,
|
||||
|
|
|
@ -168,6 +168,19 @@ struct net_ipv6_hdr {
|
|||
struct in6_addr dst;
|
||||
} __attribute__((__packed__));
|
||||
|
||||
struct net_ipv4_hdr {
|
||||
uint8_t vhl;
|
||||
uint8_t tos;
|
||||
uint8_t len[2];
|
||||
uint8_t id[2];
|
||||
uint8_t offset[2];
|
||||
uint8_t ttl;
|
||||
uint8_t proto;
|
||||
uint16_t chksum;
|
||||
struct in_addr src;
|
||||
struct in_addr dst;
|
||||
} __attribute__((__packed__));
|
||||
|
||||
#define NET_UDPH_LEN 8 /* Size of UDP header */
|
||||
#define NET_TCPH_LEN 20 /* Size of TCP header */
|
||||
#define NET_ICMPH_LEN 4 /* Size of ICMP header */
|
||||
|
|
|
@ -162,6 +162,44 @@ drop:
|
|||
}
|
||||
#endif /* CONFIG_NET_IPV6 */
|
||||
|
||||
#if defined(CONFIG_NET_IPV4)
|
||||
static inline enum net_verdict process_ipv4_pkt(struct net_buf *buf)
|
||||
{
|
||||
struct net_ipv4_hdr *hdr = NET_IPV4_BUF(buf);
|
||||
int real_len = net_buf_frags_len(buf);
|
||||
int pkt_len = (hdr->len[0] << 8) + hdr->len[1];
|
||||
|
||||
if (real_len > pkt_len) {
|
||||
NET_DBG("IPv4 adjust pkt len to %d (was %d)",
|
||||
pkt_len, real_len);
|
||||
net_buf_frag_last(buf)->len -= real_len - pkt_len;
|
||||
real_len -= pkt_len;
|
||||
} else if (real_len < pkt_len) {
|
||||
NET_DBG("IPv4 packet size %d buf len %d", pkt_len, real_len);
|
||||
NET_STATS(++net_stats.ipv4.drop);
|
||||
return NET_DROP;
|
||||
}
|
||||
|
||||
#if NET_DEBUG > 0
|
||||
do {
|
||||
char out[sizeof("xxx.xxx.xxx.xxx")];
|
||||
snprintf(out, sizeof(out), net_sprint_ipv4_addr(&hdr->dst));
|
||||
NET_DBG("IPv4 packet received from %s to %s",
|
||||
net_sprint_ipv4_addr(&hdr->src), out);
|
||||
} while (0);
|
||||
#endif /* NET_DEBUG > 0 */
|
||||
|
||||
if (!net_is_my_ipv4_addr(&hdr->dst)) {
|
||||
NET_DBG("IPv4 packet in buf %p not for me", buf);
|
||||
NET_STATS(++net_stats.ipv4.drop);
|
||||
goto drop;
|
||||
}
|
||||
|
||||
drop:
|
||||
return NET_DROP;
|
||||
}
|
||||
#endif /* CONFIG_NET_IPV4 */
|
||||
|
||||
static inline enum net_verdict process_data(struct net_buf *buf)
|
||||
{
|
||||
/* If there is no data, then drop the packet. Also if
|
||||
|
@ -182,10 +220,15 @@ static inline enum net_verdict process_data(struct net_buf *buf)
|
|||
NET_STATS(++net_stats.ipv6.recv);
|
||||
net_nbuf_family(buf) = PF_INET6;
|
||||
return process_ipv6_pkt(buf);
|
||||
#endif
|
||||
#if defined(CONFIG_NET_IPV4)
|
||||
case 0x40:
|
||||
NET_STATS(++net_stats.ipv4.recv);
|
||||
net_nbuf_family(buf) = PF_INET;
|
||||
return process_ipv4_pkt(buf);
|
||||
#endif
|
||||
}
|
||||
|
||||
drop:
|
||||
NET_STATS(++net_stats.ip_errors.protoerr);
|
||||
NET_STATS(++net_stats.ip_errors.vhlerr);
|
||||
return NET_DROP;
|
||||
|
|
|
@ -42,12 +42,12 @@
|
|||
|
||||
#if defined(CONFIG_NET_IPV6)
|
||||
/* admin-local, dynamically allocated multicast address */
|
||||
#define MCAST_IPADDR { { { 0xff, 0x84, 0, 0, 0, 0, 0, 0, \
|
||||
#define MCAST_IP6ADDR { { { 0xff, 0x84, 0, 0, 0, 0, 0, 0, \
|
||||
0, 0, 0, 0, 0, 0, 0, 0x2 } } }
|
||||
|
||||
/* Define my IP address where to expect messages */
|
||||
#if !defined(CONFIG_NET_TESTING)
|
||||
#define MY_IPADDR { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, \
|
||||
#define MY_IP6ADDR { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, \
|
||||
0, 0, 0, 0, 0, 0, 0, 0x1 } } }
|
||||
#define MY_PREFIX_LEN 64
|
||||
#endif
|
||||
|
@ -55,16 +55,17 @@
|
|||
#if defined(CONFIG_NET_TESTING)
|
||||
static const struct in6_addr in6addr_my = IN6ADDR_ANY_INIT;
|
||||
#else
|
||||
static const struct in6_addr in6addr_my = MY_IPADDR;
|
||||
static const struct in6_addr in6addr_my = MY_IP6ADDR;
|
||||
#endif
|
||||
|
||||
#else /* IPv6 */
|
||||
|
||||
/* Organization-local 239.192.0.0/14 */
|
||||
#define MCAST_IPADDR { { { 239, 192, 0, 2 } } }
|
||||
|
||||
#endif /* IPv6 */
|
||||
|
||||
#if defined(CONFIG_NET_IPV4)
|
||||
/* Organization-local 239.192.0.0/14 */
|
||||
#define MCAST_IP4ADDR { { { 239, 192, 0, 2 } } }
|
||||
#define MY_IP4ADDR { { { 192, 168, 0, 1 } } }
|
||||
#endif /* IPv4 */
|
||||
|
||||
#define MY_PORT 4242
|
||||
|
||||
static inline void init_app(void)
|
||||
|
@ -97,7 +98,7 @@ static inline bool get_context(struct net_context **udp_recv4,
|
|||
static struct net_addr any_addr6;
|
||||
static struct net_addr my_addr6;
|
||||
static const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
|
||||
static const struct in6_addr in6addr_mcast = MCAST_IPADDR;
|
||||
static const struct in6_addr in6addr_mcast = MCAST_IP6ADDR;
|
||||
#endif
|
||||
|
||||
#if defined(CONFIG_NET_IPV4)
|
||||
|
@ -105,8 +106,8 @@ static inline bool get_context(struct net_context **udp_recv4,
|
|||
static struct net_addr any_addr4;
|
||||
static struct net_addr my_addr4;
|
||||
static const struct in_addr in4addr_any = { { { 0 } } };
|
||||
static struct in_addr in4addr_my = MY_IPADDR;
|
||||
static struct in_addr in4addr_mcast = MCAST_IPADDR;
|
||||
static struct in_addr in4addr_my = MY_IP4ADDR;
|
||||
static struct in_addr in4addr_mcast = MCAST_IP4ADDR;
|
||||
#endif
|
||||
|
||||
#if defined(CONFIG_NET_IPV6)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue