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
FillInPacketEthContext(struct pkt_ctx * pctx,uint32_t cur_ts,int in_ifidx,int index,struct ethhdr * ethh,int eth_len)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
ProcessPacket(mtcp_manager_t mtcp,const int ifidx,const int index,uint32_t cur_ts,unsigned char * pkt_data,int len)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 ethhdr *ethh = (struct ethhdr *)pkt_data;
33 int ret = -1;
34 u_short h_proto = ntohs(ethh->h_proto);
35
36 memset(&pctx, 0, sizeof(pctx));
37
38 #ifdef PKTDUMP
39 DumpPacket(mtcp, (char *)pkt_data, len, "IN", ifidx);
40 #endif
41
42 #ifdef NETSTAT
43 mtcp->nstat.rx_packets[ifidx]++;
44 mtcp->nstat.rx_bytes[ifidx] += len + ETHER_OVR;
45 #endif /* NETSTAT */
46
47 /**
48 * To Do: System level configurations or callback can enable each functionality
49 * - Check PROMISCUOUS MODE
50 * - ARP
51 * - SLOWPATH
52 */
53
54 FillInPacketEthContext(&pctx, cur_ts, ifidx, index, ethh, len);
55
56 if (h_proto == ETH_P_IP) {
57 /* process ipv4 packet */
58 ret = ProcessInIPv4Packet(mtcp, &pctx);
59 } else {
60
61 /* drop the packet if forwarding is off */
62 if (!mtcp->num_msp || !pctx.forward) {
63 #ifdef RUN_ARP
64 if (h_proto == ETH_P_ARP) {
65 ret = ProcessARPPacket(mtcp, cur_ts, ifidx, pkt_data, len);
66 return TRUE;
67 } else
68 #endif
69 {
70 DumpPacket(mtcp, (char *)pkt_data, len, "??", ifidx);
71 if (mtcp->iom->release_pkt)
72 mtcp->iom->release_pkt(mtcp->ctx, ifidx, pkt_data, len);
73 }
74 } else { /* else forward */
75 ForwardEthernetFrame(mtcp, &pctx);
76 return TRUE;
77 }
78 }
79
80 #ifdef NETSTAT
81 if (ret < 0) {
82 mtcp->nstat.rx_errors[ifidx]++;
83 }
84 #endif
85
86 return ret;
87 }
88 /*----------------------------------------------------------------------------*/
89