/* * Copyright 2015 Futurewei Inc. * * l2fwd is free software: you can redistribute it and/or modify * it under the terms of version 2 the GNU General Public License as published * by the Free Software Foundation only * l2fwd is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this code. If not, see * https://www.gnu.org/licenses/gpl-2.0.html * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static char *net1 = "eth1"; module_param(net1, charp, 0); MODULE_PARM_DESC(net1, "The first net device name and optional DNAT parameters (default is eth1)"); static char *net2 = "eth2"; module_param(net2, charp, 0); MODULE_PARM_DESC(net2, "The second net device name and optional DNAT parameters (default is eth2)"); static bool print = false; module_param(print, bool, 0); MODULE_PARM_DESC(print, "Log forwarding statistics (default is false)"); static int stats_interval = 10; module_param(stats_interval, int, 0); MODULE_PARM_DESC(print, "Forwarding statistics packet interval (default is 10000)"); static bool terminate = false; module_param(terminate, bool, 0); MODULE_PARM_DESC(terminate, "Free skb instead of forwarding"); #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,18,0) #define BURST_MODE static short burst = 1; module_param(burst, short, 0); MODULE_PARM_DESC(burst, "Send burst-many packets to output device at once (default is 1)"); short burst_count; #endif static struct net_device *dev1, *dev2; int count; static struct { uint eth1 : 1; uint eth2 : 1; } dnat_enabled; static uint octet0[4]; static uint mac0[6]; static u8 s_octet0[4]; static u8 s_mac0[6]; static uint octet1[4]; static uint mac1[6]; static u8 s_octet1[4]; static u8 s_mac1[6]; static rx_handler_result_t netdev_frame_hook(struct sk_buff **pskb) { struct sk_buff *skb = *pskb; struct net_device *dev; char *data = skb->data; struct ethhdr *eh = (struct ethhdr *)skb_mac_header(skb); struct iphdr *ih = (struct iphdr *)skb_network_header(skb); struct icmphdr *icmph = icmp_hdr(skb); rx_handler_result_t retval = RX_HANDLER_CONSUMED; if (unlikely(skb->pkt_type == PACKET_LOOPBACK)) return RX_HANDLER_PASS; dev = (struct net_device*) rcu_dereference_rtnl(skb->dev->rx_handler_data); count++; if ( ((count % stats_interval) == 0) && print ) printk("l2fwd count %d\n", count); if (terminate) { kfree_skb(skb); } else { u32 *daddr = &(ih->daddr); u32 *saddr = &(ih->saddr); unsigned short proto = ntohs(eh->h_proto); uint use_dnat = dev == dev1 ? dnat_enabled.eth1:dnat_enabled.eth2; if (unlikely(proto != ETH_P_IP )) return RX_HANDLER_PASS; #ifdef DEBUG printk("use_dnat %d proto %x ETH_P_IP %x Dest MAC - IP %d.%d.%d.%d MAC %x:%x:%x:%x:%x:%x\n",use_dnat,proto,ETH_P_IP,(u8)daddr[0],(u8)daddr[1],(u8)daddr[2],(u8)daddr[3],eh->h_dest[0],eh->h_dest[1],eh->h_dest[2],eh->h_dest[3],eh->h_dest[4],eh->h_dest[5]); #endif if ( (use_dnat == 1) && (proto == ETH_P_IP) ) { unsigned int *t_addr = dev == dev1 ? &octet0[0]:&octet1[0]; u8 *s_addr = dev == dev1 ? &s_octet0[0]:&s_octet1[0]; ((u8 *)daddr)[0] = (u8)t_addr[0]; ((u8 *)daddr)[1] = (u8)t_addr[1]; ((u8 *)daddr)[2] = (u8)t_addr[2]; ((u8 *)daddr)[3] = (u8)t_addr[3]; t_addr = dev == dev1 ? &mac0[0]:&mac1[
# Copyright (c) 2017 Intel Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.