176404edcSAsim Jamshed #include <string.h>
276404edcSAsim Jamshed
376404edcSAsim Jamshed #include "ip_in.h"
476404edcSAsim Jamshed #include "eth_in.h"
576404edcSAsim Jamshed #include "eth_out.h"
676404edcSAsim Jamshed #include "arp.h"
776404edcSAsim Jamshed #include "debug.h"
876404edcSAsim Jamshed #include "ip_out.h"
976404edcSAsim Jamshed #include "config.h"
1076404edcSAsim Jamshed
1176404edcSAsim Jamshed /*----------------------------------------------------------------------------*/
1276404edcSAsim Jamshed inline void
FillInPacketEthContext(struct pkt_ctx * pctx,uint32_t cur_ts,int in_ifidx,int index,struct ethhdr * ethh,int eth_len)1376404edcSAsim Jamshed FillInPacketEthContext (struct pkt_ctx *pctx, uint32_t cur_ts, int in_ifidx,
1476404edcSAsim Jamshed int index, struct ethhdr *ethh, int eth_len)
1576404edcSAsim Jamshed {
1676404edcSAsim Jamshed pctx->p.cur_ts = cur_ts;
17*a834ea89SAsim Jamshed pctx->p.in_ifidx = in_ifidx;
1876404edcSAsim Jamshed pctx->out_ifidx = -1;
1976404edcSAsim Jamshed pctx->p.ethh = ethh;
2076404edcSAsim Jamshed pctx->p.eth_len = eth_len;
2176404edcSAsim Jamshed pctx->batch_index = index;
2276404edcSAsim Jamshed pctx->forward = g_config.mos->forward;
2376404edcSAsim Jamshed
2476404edcSAsim Jamshed return;
2576404edcSAsim Jamshed }
2676404edcSAsim Jamshed /*----------------------------------------------------------------------------*/
2776404edcSAsim Jamshed int
ProcessPacket(mtcp_manager_t mtcp,const int ifidx,const int index,uint32_t cur_ts,unsigned char * pkt_data,int len)2876404edcSAsim Jamshed ProcessPacket(mtcp_manager_t mtcp, const int ifidx, const int index,
2976404edcSAsim Jamshed uint32_t cur_ts, unsigned char *pkt_data, int len)
3076404edcSAsim Jamshed {
3176404edcSAsim Jamshed struct pkt_ctx pctx;
3276404edcSAsim Jamshed struct ethhdr *ethh = (struct ethhdr *)pkt_data;
3376404edcSAsim Jamshed int ret = -1;
3476404edcSAsim Jamshed u_short h_proto = ntohs(ethh->h_proto);
3576404edcSAsim Jamshed
3676404edcSAsim Jamshed memset(&pctx, 0, sizeof(pctx));
3776404edcSAsim Jamshed
3876404edcSAsim Jamshed #ifdef PKTDUMP
3976404edcSAsim Jamshed DumpPacket(mtcp, (char *)pkt_data, len, "IN", ifidx);
4076404edcSAsim Jamshed #endif
4176404edcSAsim Jamshed
4276404edcSAsim Jamshed #ifdef NETSTAT
4376404edcSAsim Jamshed mtcp->nstat.rx_packets[ifidx]++;
44*a834ea89SAsim Jamshed mtcp->nstat.rx_bytes[ifidx] += len + ETHER_OVR;
4576404edcSAsim Jamshed #endif /* NETSTAT */
4676404edcSAsim Jamshed
4776404edcSAsim Jamshed /**
4876404edcSAsim Jamshed * To Do: System level configurations or callback can enable each functionality
4976404edcSAsim Jamshed * - Check PROMISCUOUS MODE
5076404edcSAsim Jamshed * - ARP
5176404edcSAsim Jamshed * - SLOWPATH
5276404edcSAsim Jamshed */
5376404edcSAsim Jamshed
5476404edcSAsim Jamshed FillInPacketEthContext(&pctx, cur_ts, ifidx, index, ethh, len);
5576404edcSAsim Jamshed
5676404edcSAsim Jamshed if (h_proto == ETH_P_IP) {
5776404edcSAsim Jamshed /* process ipv4 packet */
5876404edcSAsim Jamshed ret = ProcessInIPv4Packet(mtcp, &pctx);
5976404edcSAsim Jamshed } else {
6076404edcSAsim Jamshed
6176404edcSAsim Jamshed /* drop the packet if forwarding is off */
6276404edcSAsim Jamshed if (!mtcp->num_msp || !pctx.forward) {
6376404edcSAsim Jamshed #ifdef RUN_ARP
6476404edcSAsim Jamshed if (h_proto == ETH_P_ARP) {
6576404edcSAsim Jamshed ret = ProcessARPPacket(mtcp, cur_ts, ifidx, pkt_data, len);
6676404edcSAsim Jamshed return TRUE;
6776404edcSAsim Jamshed } else
6876404edcSAsim Jamshed #endif
6976404edcSAsim Jamshed {
7076404edcSAsim Jamshed DumpPacket(mtcp, (char *)pkt_data, len, "??", ifidx);
7176404edcSAsim Jamshed if (mtcp->iom->release_pkt)
7276404edcSAsim Jamshed mtcp->iom->release_pkt(mtcp->ctx, ifidx, pkt_data, len);
7376404edcSAsim Jamshed }
7476404edcSAsim Jamshed } else { /* else forward */
7576404edcSAsim Jamshed ForwardEthernetFrame(mtcp, &pctx);
7676404edcSAsim Jamshed return TRUE;
7776404edcSAsim Jamshed }
7876404edcSAsim Jamshed }
7976404edcSAsim Jamshed
8076404edcSAsim Jamshed #ifdef NETSTAT
8176404edcSAsim Jamshed if (ret < 0) {
8276404edcSAsim Jamshed mtcp->nstat.rx_errors[ifidx]++;
8376404edcSAsim Jamshed }
8476404edcSAsim Jamshed #endif
8576404edcSAsim Jamshed
8676404edcSAsim Jamshed return ret;
8776404edcSAsim Jamshed }
8876404edcSAsim Jamshed /*----------------------------------------------------------------------------*/
89