xref: /mOS-networking-stack/core/src/eth_in.c (revision d8823779)
1 #include <string.h>
2 
3 #include "ip_in.h"
4 #include "eth_in.h"
5 #include "eth_out.h"
6 #include "arp.h"
7 #include "debug.h"
8 #include "ip_out.h"
9 #include "config.h"
10 
11 /*----------------------------------------------------------------------------*/
12 inline void
13 FillInPacketEthContext (struct pkt_ctx *pctx, uint32_t cur_ts, int in_ifidx,
14 		        int index, struct ethhdr *ethh, int eth_len)
15 {
16 	pctx->p.cur_ts = cur_ts;
17 	pctx->p.in_ifidx = in_ifidx;
18 	pctx->out_ifidx = -1;
19 	pctx->p.ethh = ethh;
20 	pctx->p.eth_len = eth_len;
21 	pctx->batch_index = index;
22 	pctx->forward = g_config.mos->forward;
23 
24 	return;
25 }
26 /*----------------------------------------------------------------------------*/
27 int
28 ProcessPacket(mtcp_manager_t mtcp, const int ifidx, const int index,
29 		uint32_t cur_ts, unsigned char *pkt_data, int len)
30 {
31 	struct pkt_ctx pctx;
32 	struct mon_listener *walk;
33 	struct ethhdr *ethh = (struct ethhdr *)pkt_data;
34 	int ret = -1;
35 	u_short h_proto = ntohs(ethh->h_proto);
36 
37 	memset(&pctx, 0, sizeof(pctx));
38 
39 #ifdef PKTDUMP
40 	DumpPacket(mtcp, (char *)pkt_data, len, "IN", ifidx);
41 #endif
42 
43 #ifdef NETSTAT
44 	mtcp->nstat.rx_packets[ifidx]++;
45 	mtcp->nstat.rx_bytes[ifidx] += len + ETHER_OVR;
46 #endif /* NETSTAT */
47 
48 	/**
49 	 * To Do: System level configurations or callback can enable each functionality
50 	 * - Check PROMISCUOUS MODE
51 	 * - ARP
52 	 * - SLOWPATH
53 	 */
54 
55 	FillInPacketEthContext(&pctx, cur_ts, ifidx, index, ethh, len);
56 
57 	if (h_proto == ETH_P_IP) {
58 		/* process ipv4 packet */
59 		ret = ProcessInIPv4Packet(mtcp, &pctx);
60 	} else {
61 		/* callback for monitor raw socket */
62 		TAILQ_FOREACH(walk, &mtcp->monitors, link)
63 			if (walk->socket->socktype == MOS_SOCK_MONITOR_RAW)
64 				HandleCallback(mtcp, MOS_NULL, walk->socket, MOS_SIDE_BOTH,
65 					       &pctx, MOS_ON_PKT_IN);
66 
67 		/* drop the packet if forwarding is off */
68 		if (!mtcp->num_msp || !pctx.forward) {
69 #ifdef RUN_ARP
70 			if (h_proto == ETH_P_ARP) {
71 				ret = ProcessARPPacket(mtcp, cur_ts, ifidx, pkt_data, len);
72 				return TRUE;
73 			} else
74 #endif
75 				{
76 					DumpPacket(mtcp, (char *)pkt_data, len, "??", ifidx);
77 					if (mtcp->iom->release_pkt)
78 						mtcp->iom->release_pkt(mtcp->ctx, ifidx, pkt_data, len);
79 				}
80 		} else { /* else forward */
81 			ForwardEthernetFrame(mtcp, &pctx);
82 			return TRUE;
83 		}
84 	}
85 
86 #ifdef NETSTAT
87 	if (ret < 0) {
88 		mtcp->nstat.rx_errors[ifidx]++;
89 	}
90 #endif
91 
92 	return ret;
93 }
94 /*----------------------------------------------------------------------------*/
95