If there are substantial number of slaves created as simulated by Syzbot, the backlog processing could take much longer and result into the issue found in the Syzbot report. INFO: rcu_sched detected stalls on CPUs/tasks: (detected by 1, t=10502 jiffies, g=5049, c=5048, q=752) All QSes seen, last rcu_sched kthread activity 10502 (4294965563-4294955061), jiffies_till_next_fqs=1, root ->qsmask 0x0 syz-executor.1 R running task on cpu 1 10984 11210 3866 0x30020008 179034491270 Call Trace: <IRQ> [<ffffffff81497163>] _sched_show_task kernel/sched/core.c:8063 [inline] [<ffffffff81497163>] _sched_show_task.cold+0x2fd/0x392 kernel/sched/core.c:8030 [<ffffffff8146a91b>] sched_show_task+0xb/0x10 kernel/sched/core.c:8073 [<ffffffff815c931b>] print_other_cpu_stall kernel/rcu/tree.c:1577 [inline] [<ffffffff815c931b>] check_cpu_stall kernel/rcu/tree.c:1695 [inline] [<ffffffff815c931b>] __rcu_pending kernel/rcu/tree.c:3478 [inline] [<ffffffff815c931b>] rcu_pending kernel/rcu/tree.c:3540 [inline] [<ffffffff815c931b>] rcu_check_callbacks.cold+0xbb4/0xc29 kernel/rcu/tree.c:2876 [<ffffffff815e3962>] update_process_times+0x32/0x80 kernel/time/timer.c:1635 [<ffffffff816164f0>] tick_sched_handle+0xa0/0x180 kernel/time/tick-sched.c:161 [<ffffffff81616ae4>] tick_sched_timer+0x44/0x130 kernel/time/tick-sched.c:1193 [<ffffffff815e75f7>] __run_hrtimer kernel/time/hrtimer.c:1393 [inline] [<ffffffff815e75f7>] __hrtimer_run_queues+0x307/0xd90 kernel/time/hrtimer.c:1455 [<ffffffff815e90ea>] hrtimer_interrupt+0x2ea/0x730 kernel/time/hrtimer.c:1513 [<ffffffff844050f4>] local_apic_timer_interrupt arch/x86/kernel/apic/apic.c:1031 [inline] [<ffffffff844050f4>] smp_apic_timer_interrupt+0x144/0x5e0 arch/x86/kernel/apic/apic.c:1056 [<ffffffff84401cbe>] apic_timer_interrupt+0x8e/0xa0 arch/x86/entry/entry_64.S:778 RIP: 0010:do_raw_read_lock+0x22/0x80 kernel/locking/spinlock_debug.c:153 RSP: 0018:ffff8801dad07ab8 EFLAGS: 00000a02 ORIG_RAX: ffffffffffffff12 RAX: 0000000000000000 RBX: ffff8801c4135680 RCX: 0000000000000000 RDX: 1ffff10038826afe RSI: ffff88019d816bb8 RDI: ffff8801c41357f0 RBP: ffff8801dad07ac0 R08: 0000000000004b15 R09: 0000000000310273 R10: ffff88019d816bb8 R11: 0000000000000001 R12: ffff8801c41357e8 R13: 0000000000000000 R14: ffff8801cfb19850 R15: ffff8801cfb198b0 [<ffffffff8101460e>] __raw_read_lock_bh include/linux/rwlock_api_smp.h:177 [inline] [<ffffffff8101460e>] _raw_read_lock_bh+0x3e/0x50 kernel/locking/spinlock.c:240 [<ffffffff840d78ca>] ipv6_chk_mcast_addr+0x11a/0x6f0 net/ipv6/mcast.c:1006 [<ffffffff84023439>] ip6_mc_input+0x319/0x8e0 net/ipv6/ip6_input.c:482 [<ffffffff840211c8>] dst_input include/net/dst.h:449 [inline] [<ffffffff840211c8>] ip6_rcv_finish+0x408/0x610 net/ipv6/ip6_input.c:78 [<ffffffff840214de>] NF_HOOK include/linux/netfilter.h:292 [inline] [<ffffffff840214de>] NF_HOOK include/linux/netfilter.h:286 [inline] [<ffffffff840214de>] ipv6_rcv+0x10e/0x420 net/ipv6/ip6_input.c:278 [<ffffffff83a29efa>] __netif_receive_skb_one_core+0x12a/0x1f0 net/core/dev.c:5303 [<ffffffff83a2a15c>] __netif_receive_skb+0x2c/0x1b0 net/core/dev.c:5417 [<ffffffff83a2f536>] process_backlog+0x216/0x6c0 net/core/dev.c:6243 [<ffffffff83a30d1b>] napi_poll net/core/dev.c:6680 [inline] [<ffffffff83a30d1b>] net_rx_action+0x47b/0xfb0 net/core/dev.c:6748 [<ffffffff846002c8>] __do_softirq+0x2c8/0x99a kernel/softirq.c:317 [<ffffffff813e656a>] invoke_softirq kernel/softirq.c:399 [inline] [<ffffffff813e656a>] irq_exit+0x16a/0x1a0 kernel/softirq.c:439 [<ffffffff84405115>] exiting_irq arch/x86/include/asm/apic.h:561 [inline] [<ffffffff84405115>] smp_apic_timer_interrupt+0x165/0x5e0 arch/x86/kernel/apic/apic.c:1058 [<ffffffff84401cbe>] apic_timer_interrupt+0x8e/0xa0 arch/x86/entry/entry_64.S:778 </IRQ> RIP: 0010:__sanitizer_cov_trace_pc+0x26/0x50 kernel/kcov.c:102 RSP: 0018:ffff880196033bd8 EFLAGS: 00000246 ORIG_RAX: ffffffffffffff12 RAX: ffff88019d8161c0 RBX: 00000000ffffffff RCX: ffffc90003501000 RDX: 0000000000000002 RSI: ffffffff816236d1 RDI: 0000000000000005 RBP: ffff880196033bd8 R08: ffff88019d8161c0 R09: 0000000000000000 R10: 1ffff10032c067f0 R11: 0000000000000000 R12: 0000000000000000 R13: 0000000000000080 R14: 0000000000000000 R15: 0000000000000000 [<ffffffff816236d1>] do_futex+0x151/0x1d50 kernel/futex.c:3548 [<ffffffff816260f0>] C_SYSC_futex kernel/futex_compat.c:201 [inline] [<ffffffff816260f0>] compat_SyS_futex+0x270/0x3b0 kernel/futex_compat.c:175 [<ffffffff8101da17>] do_syscall_32_irqs_on arch/x86/entry/common.c:353 [inline] [<ffffffff8101da17>] do_fast_syscall_32+0x357/0xe1c arch/x86/entry/common.c:415 [<ffffffff84401a9b>] entry_SYSENTER_compat+0x8b/0x9d arch/x86/entry/entry_64_compat.S:139 RIP: 0023:0xf7f23c69 RSP: 002b:00000000f5d1f12c EFLAGS: 00000282 ORIG_RAX: 00000000000000f0 RAX: ffffffffffffffda RBX: 000000000816af88 RCX: 0000000000000080 RDX: 0000000000000000 RSI: 0000000000000000 RDI: 000000000816af8c RBP: 00000000f5d1f228 R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000000 R12: 0000000000000000 R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000000 rcu_sched kthread starved for 10502 jiffies! g5049 c5048 f0x2 RCU_GP_WAIT_FQS(3) ->state=0x0 ->cpu=1 rcu_sched R running task on cpu 1 13048 8 2 0x90000000 179099587640 Call Trace: [<ffffffff8147321f>] context_switch+0x60f/0xa60 kernel/sched/core.c:3209 [<ffffffff8100095a>] __schedule+0x5aa/0x1da0 kernel/sched/core.c:3934 [<ffffffff810021df>] schedule+0x8f/0x1b0 kernel/sched/core.c:4011 [<ffffffff8101116d>] schedule_timeout+0x50d/0xee0 kernel/time/timer.c:1803 [<ffffffff815c13f1>] rcu_gp_kthread+0xda1/0x3b50 kernel/rcu/tree.c:2327 [<ffffffff8144b318>] kthread+0x348/0x420 kernel/kthread.c:246 [<ffffffff84400266>] ret_from_fork+0x56/0x70 arch/x86/entry/entry_64.S:393 Fixes: ba35f8588f47 (“ipvlan: Defer multicast / broadcast processing to a work-queue”) Signed-off-by: Mahesh Bandewar <maheshb@google.com> Reported-by: syzbot <syzkaller@googlegroups.com> Signed-off-by: David S. Miller <davem@davemloft.net>
757 lines
17 KiB
C
757 lines
17 KiB
C
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
/* Copyright (c) 2014 Mahesh Bandewar <maheshb@google.com>
|
|
*/
|
|
|
|
#include "ipvlan.h"
|
|
|
|
static u32 ipvlan_jhash_secret __read_mostly;
|
|
|
|
void ipvlan_init_secret(void)
|
|
{
|
|
net_get_random_once(&ipvlan_jhash_secret, sizeof(ipvlan_jhash_secret));
|
|
}
|
|
|
|
void ipvlan_count_rx(const struct ipvl_dev *ipvlan,
|
|
unsigned int len, bool success, bool mcast)
|
|
{
|
|
if (likely(success)) {
|
|
struct ipvl_pcpu_stats *pcptr;
|
|
|
|
pcptr = this_cpu_ptr(ipvlan->pcpu_stats);
|
|
u64_stats_update_begin(&pcptr->syncp);
|
|
pcptr->rx_pkts++;
|
|
pcptr->rx_bytes += len;
|
|
if (mcast)
|
|
pcptr->rx_mcast++;
|
|
u64_stats_update_end(&pcptr->syncp);
|
|
} else {
|
|
this_cpu_inc(ipvlan->pcpu_stats->rx_errs);
|
|
}
|
|
}
|
|
EXPORT_SYMBOL_GPL(ipvlan_count_rx);
|
|
|
|
#if IS_ENABLED(CONFIG_IPV6)
|
|
static u8 ipvlan_get_v6_hash(const void *iaddr)
|
|
{
|
|
const struct in6_addr *ip6_addr = iaddr;
|
|
|
|
return __ipv6_addr_jhash(ip6_addr, ipvlan_jhash_secret) &
|
|
IPVLAN_HASH_MASK;
|
|
}
|
|
#else
|
|
static u8 ipvlan_get_v6_hash(const void *iaddr)
|
|
{
|
|
return 0;
|
|
}
|
|
#endif
|
|
|
|
static u8 ipvlan_get_v4_hash(const void *iaddr)
|
|
{
|
|
const struct in_addr *ip4_addr = iaddr;
|
|
|
|
return jhash_1word(ip4_addr->s_addr, ipvlan_jhash_secret) &
|
|
IPVLAN_HASH_MASK;
|
|
}
|
|
|
|
static bool addr_equal(bool is_v6, struct ipvl_addr *addr, const void *iaddr)
|
|
{
|
|
if (!is_v6 && addr->atype == IPVL_IPV4) {
|
|
struct in_addr *i4addr = (struct in_addr *)iaddr;
|
|
|
|
return addr->ip4addr.s_addr == i4addr->s_addr;
|
|
#if IS_ENABLED(CONFIG_IPV6)
|
|
} else if (is_v6 && addr->atype == IPVL_IPV6) {
|
|
struct in6_addr *i6addr = (struct in6_addr *)iaddr;
|
|
|
|
return ipv6_addr_equal(&addr->ip6addr, i6addr);
|
|
#endif
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static struct ipvl_addr *ipvlan_ht_addr_lookup(const struct ipvl_port *port,
|
|
const void *iaddr, bool is_v6)
|
|
{
|
|
struct ipvl_addr *addr;
|
|
u8 hash;
|
|
|
|
hash = is_v6 ? ipvlan_get_v6_hash(iaddr) :
|
|
ipvlan_get_v4_hash(iaddr);
|
|
hlist_for_each_entry_rcu(addr, &port->hlhead[hash], hlnode)
|
|
if (addr_equal(is_v6, addr, iaddr))
|
|
return addr;
|
|
return NULL;
|
|
}
|
|
|
|
void ipvlan_ht_addr_add(struct ipvl_dev *ipvlan, struct ipvl_addr *addr)
|
|
{
|
|
struct ipvl_port *port = ipvlan->port;
|
|
u8 hash;
|
|
|
|
hash = (addr->atype == IPVL_IPV6) ?
|
|
ipvlan_get_v6_hash(&addr->ip6addr) :
|
|
ipvlan_get_v4_hash(&addr->ip4addr);
|
|
if (hlist_unhashed(&addr->hlnode))
|
|
hlist_add_head_rcu(&addr->hlnode, &port->hlhead[hash]);
|
|
}
|
|
|
|
void ipvlan_ht_addr_del(struct ipvl_addr *addr)
|
|
{
|
|
hlist_del_init_rcu(&addr->hlnode);
|
|
}
|
|
|
|
struct ipvl_addr *ipvlan_find_addr(const struct ipvl_dev *ipvlan,
|
|
const void *iaddr, bool is_v6)
|
|
{
|
|
struct ipvl_addr *addr, *ret = NULL;
|
|
|
|
rcu_read_lock();
|
|
list_for_each_entry_rcu(addr, &ipvlan->addrs, anode) {
|
|
if (addr_equal(is_v6, addr, iaddr)) {
|
|
ret = addr;
|
|
break;
|
|
}
|
|
}
|
|
rcu_read_unlock();
|
|
return ret;
|
|
}
|
|
|
|
bool ipvlan_addr_busy(struct ipvl_port *port, void *iaddr, bool is_v6)
|
|
{
|
|
struct ipvl_dev *ipvlan;
|
|
bool ret = false;
|
|
|
|
rcu_read_lock();
|
|
list_for_each_entry_rcu(ipvlan, &port->ipvlans, pnode) {
|
|
if (ipvlan_find_addr(ipvlan, iaddr, is_v6)) {
|
|
ret = true;
|
|
break;
|
|
}
|
|
}
|
|
rcu_read_unlock();
|
|
return ret;
|
|
}
|
|
|
|
void *ipvlan_get_L3_hdr(struct ipvl_port *port, struct sk_buff *skb, int *type)
|
|
{
|
|
void *lyr3h = NULL;
|
|
|
|
switch (skb->protocol) {
|
|
case htons(ETH_P_ARP): {
|
|
struct arphdr *arph;
|
|
|
|
if (unlikely(!pskb_may_pull(skb, arp_hdr_len(port->dev))))
|
|
return NULL;
|
|
|
|
arph = arp_hdr(skb);
|
|
*type = IPVL_ARP;
|
|
lyr3h = arph;
|
|
break;
|
|
}
|
|
case htons(ETH_P_IP): {
|
|
u32 pktlen;
|
|
struct iphdr *ip4h;
|
|
|
|
if (unlikely(!pskb_may_pull(skb, sizeof(*ip4h))))
|
|
return NULL;
|
|
|
|
ip4h = ip_hdr(skb);
|
|
pktlen = ntohs(ip4h->tot_len);
|
|
if (ip4h->ihl < 5 || ip4h->version != 4)
|
|
return NULL;
|
|
if (skb->len < pktlen || pktlen < (ip4h->ihl * 4))
|
|
return NULL;
|
|
|
|
*type = IPVL_IPV4;
|
|
lyr3h = ip4h;
|
|
break;
|
|
}
|
|
#if IS_ENABLED(CONFIG_IPV6)
|
|
case htons(ETH_P_IPV6): {
|
|
struct ipv6hdr *ip6h;
|
|
|
|
if (unlikely(!pskb_may_pull(skb, sizeof(*ip6h))))
|
|
return NULL;
|
|
|
|
ip6h = ipv6_hdr(skb);
|
|
if (ip6h->version != 6)
|
|
return NULL;
|
|
|
|
*type = IPVL_IPV6;
|
|
lyr3h = ip6h;
|
|
/* Only Neighbour Solicitation pkts need different treatment */
|
|
if (ipv6_addr_any(&ip6h->saddr) &&
|
|
ip6h->nexthdr == NEXTHDR_ICMP) {
|
|
struct icmp6hdr *icmph;
|
|
|
|
if (unlikely(!pskb_may_pull(skb, sizeof(*ip6h) + sizeof(*icmph))))
|
|
return NULL;
|
|
|
|
ip6h = ipv6_hdr(skb);
|
|
icmph = (struct icmp6hdr *)(ip6h + 1);
|
|
|
|
if (icmph->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) {
|
|
/* Need to access the ipv6 address in body */
|
|
if (unlikely(!pskb_may_pull(skb, sizeof(*ip6h) + sizeof(*icmph)
|
|
+ sizeof(struct in6_addr))))
|
|
return NULL;
|
|
|
|
ip6h = ipv6_hdr(skb);
|
|
icmph = (struct icmp6hdr *)(ip6h + 1);
|
|
}
|
|
|
|
*type = IPVL_ICMPV6;
|
|
lyr3h = icmph;
|
|
}
|
|
break;
|
|
}
|
|
#endif
|
|
default:
|
|
return NULL;
|
|
}
|
|
|
|
return lyr3h;
|
|
}
|
|
|
|
unsigned int ipvlan_mac_hash(const unsigned char *addr)
|
|
{
|
|
u32 hash = jhash_1word(__get_unaligned_cpu32(addr+2),
|
|
ipvlan_jhash_secret);
|
|
|
|
return hash & IPVLAN_MAC_FILTER_MASK;
|
|
}
|
|
|
|
void ipvlan_process_multicast(struct work_struct *work)
|
|
{
|
|
struct ipvl_port *port = container_of(work, struct ipvl_port, wq);
|
|
struct ethhdr *ethh;
|
|
struct ipvl_dev *ipvlan;
|
|
struct sk_buff *skb, *nskb;
|
|
struct sk_buff_head list;
|
|
unsigned int len;
|
|
unsigned int mac_hash;
|
|
int ret;
|
|
u8 pkt_type;
|
|
bool tx_pkt;
|
|
|
|
__skb_queue_head_init(&list);
|
|
|
|
spin_lock_bh(&port->backlog.lock);
|
|
skb_queue_splice_tail_init(&port->backlog, &list);
|
|
spin_unlock_bh(&port->backlog.lock);
|
|
|
|
while ((skb = __skb_dequeue(&list)) != NULL) {
|
|
struct net_device *dev = skb->dev;
|
|
bool consumed = false;
|
|
|
|
ethh = eth_hdr(skb);
|
|
tx_pkt = IPVL_SKB_CB(skb)->tx_pkt;
|
|
mac_hash = ipvlan_mac_hash(ethh->h_dest);
|
|
|
|
if (ether_addr_equal(ethh->h_dest, port->dev->broadcast))
|
|
pkt_type = PACKET_BROADCAST;
|
|
else
|
|
pkt_type = PACKET_MULTICAST;
|
|
|
|
rcu_read_lock();
|
|
list_for_each_entry_rcu(ipvlan, &port->ipvlans, pnode) {
|
|
if (tx_pkt && (ipvlan->dev == skb->dev))
|
|
continue;
|
|
if (!test_bit(mac_hash, ipvlan->mac_filters))
|
|
continue;
|
|
if (!(ipvlan->dev->flags & IFF_UP))
|
|
continue;
|
|
ret = NET_RX_DROP;
|
|
len = skb->len + ETH_HLEN;
|
|
nskb = skb_clone(skb, GFP_ATOMIC);
|
|
local_bh_disable();
|
|
if (nskb) {
|
|
consumed = true;
|
|
nskb->pkt_type = pkt_type;
|
|
nskb->dev = ipvlan->dev;
|
|
if (tx_pkt)
|
|
ret = dev_forward_skb(ipvlan->dev, nskb);
|
|
else
|
|
ret = netif_rx(nskb);
|
|
}
|
|
ipvlan_count_rx(ipvlan, len, ret == NET_RX_SUCCESS, true);
|
|
local_bh_enable();
|
|
cond_resched_rcu();
|
|
}
|
|
rcu_read_unlock();
|
|
|
|
if (tx_pkt) {
|
|
/* If the packet originated here, send it out. */
|
|
skb->dev = port->dev;
|
|
skb->pkt_type = pkt_type;
|
|
dev_queue_xmit(skb);
|
|
} else {
|
|
if (consumed)
|
|
consume_skb(skb);
|
|
else
|
|
kfree_skb(skb);
|
|
}
|
|
if (dev)
|
|
dev_put(dev);
|
|
}
|
|
}
|
|
|
|
static void ipvlan_skb_crossing_ns(struct sk_buff *skb, struct net_device *dev)
|
|
{
|
|
bool xnet = true;
|
|
|
|
if (dev)
|
|
xnet = !net_eq(dev_net(skb->dev), dev_net(dev));
|
|
|
|
skb_scrub_packet(skb, xnet);
|
|
if (dev)
|
|
skb->dev = dev;
|
|
}
|
|
|
|
static int ipvlan_rcv_frame(struct ipvl_addr *addr, struct sk_buff **pskb,
|
|
bool local)
|
|
{
|
|
struct ipvl_dev *ipvlan = addr->master;
|
|
struct net_device *dev = ipvlan->dev;
|
|
unsigned int len;
|
|
rx_handler_result_t ret = RX_HANDLER_CONSUMED;
|
|
bool success = false;
|
|
struct sk_buff *skb = *pskb;
|
|
|
|
len = skb->len + ETH_HLEN;
|
|
/* Only packets exchanged between two local slaves need to have
|
|
* device-up check as well as skb-share check.
|
|
*/
|
|
if (local) {
|
|
if (unlikely(!(dev->flags & IFF_UP))) {
|
|
kfree_skb(skb);
|
|
goto out;
|
|
}
|
|
|
|
skb = skb_share_check(skb, GFP_ATOMIC);
|
|
if (!skb)
|
|
goto out;
|
|
|
|
*pskb = skb;
|
|
}
|
|
|
|
if (local) {
|
|
skb->pkt_type = PACKET_HOST;
|
|
if (dev_forward_skb(ipvlan->dev, skb) == NET_RX_SUCCESS)
|
|
success = true;
|
|
} else {
|
|
skb->dev = dev;
|
|
ret = RX_HANDLER_ANOTHER;
|
|
success = true;
|
|
}
|
|
|
|
out:
|
|
ipvlan_count_rx(ipvlan, len, success, false);
|
|
return ret;
|
|
}
|
|
|
|
struct ipvl_addr *ipvlan_addr_lookup(struct ipvl_port *port, void *lyr3h,
|
|
int addr_type, bool use_dest)
|
|
{
|
|
struct ipvl_addr *addr = NULL;
|
|
|
|
switch (addr_type) {
|
|
#if IS_ENABLED(CONFIG_IPV6)
|
|
case IPVL_IPV6: {
|
|
struct ipv6hdr *ip6h;
|
|
struct in6_addr *i6addr;
|
|
|
|
ip6h = (struct ipv6hdr *)lyr3h;
|
|
i6addr = use_dest ? &ip6h->daddr : &ip6h->saddr;
|
|
addr = ipvlan_ht_addr_lookup(port, i6addr, true);
|
|
break;
|
|
}
|
|
case IPVL_ICMPV6: {
|
|
struct nd_msg *ndmh;
|
|
struct in6_addr *i6addr;
|
|
|
|
/* Make sure that the NeighborSolicitation ICMPv6 packets
|
|
* are handled to avoid DAD issue.
|
|
*/
|
|
ndmh = (struct nd_msg *)lyr3h;
|
|
if (ndmh->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) {
|
|
i6addr = &ndmh->target;
|
|
addr = ipvlan_ht_addr_lookup(port, i6addr, true);
|
|
}
|
|
break;
|
|
}
|
|
#endif
|
|
case IPVL_IPV4: {
|
|
struct iphdr *ip4h;
|
|
__be32 *i4addr;
|
|
|
|
ip4h = (struct iphdr *)lyr3h;
|
|
i4addr = use_dest ? &ip4h->daddr : &ip4h->saddr;
|
|
addr = ipvlan_ht_addr_lookup(port, i4addr, false);
|
|
break;
|
|
}
|
|
case IPVL_ARP: {
|
|
struct arphdr *arph;
|
|
unsigned char *arp_ptr;
|
|
__be32 dip;
|
|
|
|
arph = (struct arphdr *)lyr3h;
|
|
arp_ptr = (unsigned char *)(arph + 1);
|
|
if (use_dest)
|
|
arp_ptr += (2 * port->dev->addr_len) + 4;
|
|
else
|
|
arp_ptr += port->dev->addr_len;
|
|
|
|
memcpy(&dip, arp_ptr, 4);
|
|
addr = ipvlan_ht_addr_lookup(port, &dip, false);
|
|
break;
|
|
}
|
|
}
|
|
|
|
return addr;
|
|
}
|
|
|
|
static int ipvlan_process_v4_outbound(struct sk_buff *skb)
|
|
{
|
|
const struct iphdr *ip4h = ip_hdr(skb);
|
|
struct net_device *dev = skb->dev;
|
|
struct net *net = dev_net(dev);
|
|
struct rtable *rt;
|
|
int err, ret = NET_XMIT_DROP;
|
|
struct flowi4 fl4 = {
|
|
.flowi4_oif = dev->ifindex,
|
|
.flowi4_tos = RT_TOS(ip4h->tos),
|
|
.flowi4_flags = FLOWI_FLAG_ANYSRC,
|
|
.flowi4_mark = skb->mark,
|
|
.daddr = ip4h->daddr,
|
|
.saddr = ip4h->saddr,
|
|
};
|
|
|
|
rt = ip_route_output_flow(net, &fl4, NULL);
|
|
if (IS_ERR(rt))
|
|
goto err;
|
|
|
|
if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
|
|
ip_rt_put(rt);
|
|
goto err;
|
|
}
|
|
skb_dst_set(skb, &rt->dst);
|
|
err = ip_local_out(net, skb->sk, skb);
|
|
if (unlikely(net_xmit_eval(err)))
|
|
dev->stats.tx_errors++;
|
|
else
|
|
ret = NET_XMIT_SUCCESS;
|
|
goto out;
|
|
err:
|
|
dev->stats.tx_errors++;
|
|
kfree_skb(skb);
|
|
out:
|
|
return ret;
|
|
}
|
|
|
|
#if IS_ENABLED(CONFIG_IPV6)
|
|
static int ipvlan_process_v6_outbound(struct sk_buff *skb)
|
|
{
|
|
const struct ipv6hdr *ip6h = ipv6_hdr(skb);
|
|
struct net_device *dev = skb->dev;
|
|
struct net *net = dev_net(dev);
|
|
struct dst_entry *dst;
|
|
int err, ret = NET_XMIT_DROP;
|
|
struct flowi6 fl6 = {
|
|
.flowi6_oif = dev->ifindex,
|
|
.daddr = ip6h->daddr,
|
|
.saddr = ip6h->saddr,
|
|
.flowi6_flags = FLOWI_FLAG_ANYSRC,
|
|
.flowlabel = ip6_flowinfo(ip6h),
|
|
.flowi6_mark = skb->mark,
|
|
.flowi6_proto = ip6h->nexthdr,
|
|
};
|
|
|
|
dst = ip6_route_output(net, NULL, &fl6);
|
|
if (dst->error) {
|
|
ret = dst->error;
|
|
dst_release(dst);
|
|
goto err;
|
|
}
|
|
skb_dst_set(skb, dst);
|
|
err = ip6_local_out(net, skb->sk, skb);
|
|
if (unlikely(net_xmit_eval(err)))
|
|
dev->stats.tx_errors++;
|
|
else
|
|
ret = NET_XMIT_SUCCESS;
|
|
goto out;
|
|
err:
|
|
dev->stats.tx_errors++;
|
|
kfree_skb(skb);
|
|
out:
|
|
return ret;
|
|
}
|
|
#else
|
|
static int ipvlan_process_v6_outbound(struct sk_buff *skb)
|
|
{
|
|
return NET_XMIT_DROP;
|
|
}
|
|
#endif
|
|
|
|
static int ipvlan_process_outbound(struct sk_buff *skb)
|
|
{
|
|
struct ethhdr *ethh = eth_hdr(skb);
|
|
int ret = NET_XMIT_DROP;
|
|
|
|
/* The ipvlan is a pseudo-L2 device, so the packets that we receive
|
|
* will have L2; which need to discarded and processed further
|
|
* in the net-ns of the main-device.
|
|
*/
|
|
if (skb_mac_header_was_set(skb)) {
|
|
/* In this mode we dont care about
|
|
* multicast and broadcast traffic */
|
|
if (is_multicast_ether_addr(ethh->h_dest)) {
|
|
pr_debug_ratelimited(
|
|
"Dropped {multi|broad}cast of type=[%x]\n",
|
|
ntohs(skb->protocol));
|
|
kfree_skb(skb);
|
|
goto out;
|
|
}
|
|
|
|
skb_pull(skb, sizeof(*ethh));
|
|
skb->mac_header = (typeof(skb->mac_header))~0U;
|
|
skb_reset_network_header(skb);
|
|
}
|
|
|
|
if (skb->protocol == htons(ETH_P_IPV6))
|
|
ret = ipvlan_process_v6_outbound(skb);
|
|
else if (skb->protocol == htons(ETH_P_IP))
|
|
ret = ipvlan_process_v4_outbound(skb);
|
|
else {
|
|
pr_warn_ratelimited("Dropped outbound packet type=%x\n",
|
|
ntohs(skb->protocol));
|
|
kfree_skb(skb);
|
|
}
|
|
out:
|
|
return ret;
|
|
}
|
|
|
|
static void ipvlan_multicast_enqueue(struct ipvl_port *port,
|
|
struct sk_buff *skb, bool tx_pkt)
|
|
{
|
|
if (skb->protocol == htons(ETH_P_PAUSE)) {
|
|
kfree_skb(skb);
|
|
return;
|
|
}
|
|
|
|
/* Record that the deferred packet is from TX or RX path. By
|
|
* looking at mac-addresses on packet will lead to erronus decisions.
|
|
* (This would be true for a loopback-mode on master device or a
|
|
* hair-pin mode of the switch.)
|
|
*/
|
|
IPVL_SKB_CB(skb)->tx_pkt = tx_pkt;
|
|
|
|
spin_lock(&port->backlog.lock);
|
|
if (skb_queue_len(&port->backlog) < IPVLAN_QBACKLOG_LIMIT) {
|
|
if (skb->dev)
|
|
dev_hold(skb->dev);
|
|
__skb_queue_tail(&port->backlog, skb);
|
|
spin_unlock(&port->backlog.lock);
|
|
schedule_work(&port->wq);
|
|
} else {
|
|
spin_unlock(&port->backlog.lock);
|
|
atomic_long_inc(&skb->dev->rx_dropped);
|
|
kfree_skb(skb);
|
|
}
|
|
}
|
|
|
|
static int ipvlan_xmit_mode_l3(struct sk_buff *skb, struct net_device *dev)
|
|
{
|
|
const struct ipvl_dev *ipvlan = netdev_priv(dev);
|
|
void *lyr3h;
|
|
struct ipvl_addr *addr;
|
|
int addr_type;
|
|
|
|
lyr3h = ipvlan_get_L3_hdr(ipvlan->port, skb, &addr_type);
|
|
if (!lyr3h)
|
|
goto out;
|
|
|
|
if (!ipvlan_is_vepa(ipvlan->port)) {
|
|
addr = ipvlan_addr_lookup(ipvlan->port, lyr3h, addr_type, true);
|
|
if (addr) {
|
|
if (ipvlan_is_private(ipvlan->port)) {
|
|
consume_skb(skb);
|
|
return NET_XMIT_DROP;
|
|
}
|
|
return ipvlan_rcv_frame(addr, &skb, true);
|
|
}
|
|
}
|
|
out:
|
|
ipvlan_skb_crossing_ns(skb, ipvlan->phy_dev);
|
|
return ipvlan_process_outbound(skb);
|
|
}
|
|
|
|
static int ipvlan_xmit_mode_l2(struct sk_buff *skb, struct net_device *dev)
|
|
{
|
|
const struct ipvl_dev *ipvlan = netdev_priv(dev);
|
|
struct ethhdr *eth = eth_hdr(skb);
|
|
struct ipvl_addr *addr;
|
|
void *lyr3h;
|
|
int addr_type;
|
|
|
|
if (!ipvlan_is_vepa(ipvlan->port) &&
|
|
ether_addr_equal(eth->h_dest, eth->h_source)) {
|
|
lyr3h = ipvlan_get_L3_hdr(ipvlan->port, skb, &addr_type);
|
|
if (lyr3h) {
|
|
addr = ipvlan_addr_lookup(ipvlan->port, lyr3h, addr_type, true);
|
|
if (addr) {
|
|
if (ipvlan_is_private(ipvlan->port)) {
|
|
consume_skb(skb);
|
|
return NET_XMIT_DROP;
|
|
}
|
|
return ipvlan_rcv_frame(addr, &skb, true);
|
|
}
|
|
}
|
|
skb = skb_share_check(skb, GFP_ATOMIC);
|
|
if (!skb)
|
|
return NET_XMIT_DROP;
|
|
|
|
/* Packet definitely does not belong to any of the
|
|
* virtual devices, but the dest is local. So forward
|
|
* the skb for the main-dev. At the RX side we just return
|
|
* RX_PASS for it to be processed further on the stack.
|
|
*/
|
|
return dev_forward_skb(ipvlan->phy_dev, skb);
|
|
|
|
} else if (is_multicast_ether_addr(eth->h_dest)) {
|
|
ipvlan_skb_crossing_ns(skb, NULL);
|
|
ipvlan_multicast_enqueue(ipvlan->port, skb, true);
|
|
return NET_XMIT_SUCCESS;
|
|
}
|
|
|
|
skb->dev = ipvlan->phy_dev;
|
|
return dev_queue_xmit(skb);
|
|
}
|
|
|
|
int ipvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
|
|
{
|
|
struct ipvl_dev *ipvlan = netdev_priv(dev);
|
|
struct ipvl_port *port = ipvlan_port_get_rcu_bh(ipvlan->phy_dev);
|
|
|
|
if (!port)
|
|
goto out;
|
|
|
|
if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
|
|
goto out;
|
|
|
|
switch(port->mode) {
|
|
case IPVLAN_MODE_L2:
|
|
return ipvlan_xmit_mode_l2(skb, dev);
|
|
case IPVLAN_MODE_L3:
|
|
#ifdef CONFIG_IPVLAN_L3S
|
|
case IPVLAN_MODE_L3S:
|
|
#endif
|
|
return ipvlan_xmit_mode_l3(skb, dev);
|
|
}
|
|
|
|
/* Should not reach here */
|
|
WARN_ONCE(true, "ipvlan_queue_xmit() called for mode = [%hx]\n",
|
|
port->mode);
|
|
out:
|
|
kfree_skb(skb);
|
|
return NET_XMIT_DROP;
|
|
}
|
|
|
|
static bool ipvlan_external_frame(struct sk_buff *skb, struct ipvl_port *port)
|
|
{
|
|
struct ethhdr *eth = eth_hdr(skb);
|
|
struct ipvl_addr *addr;
|
|
void *lyr3h;
|
|
int addr_type;
|
|
|
|
if (ether_addr_equal(eth->h_source, skb->dev->dev_addr)) {
|
|
lyr3h = ipvlan_get_L3_hdr(port, skb, &addr_type);
|
|
if (!lyr3h)
|
|
return true;
|
|
|
|
addr = ipvlan_addr_lookup(port, lyr3h, addr_type, false);
|
|
if (addr)
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static rx_handler_result_t ipvlan_handle_mode_l3(struct sk_buff **pskb,
|
|
struct ipvl_port *port)
|
|
{
|
|
void *lyr3h;
|
|
int addr_type;
|
|
struct ipvl_addr *addr;
|
|
struct sk_buff *skb = *pskb;
|
|
rx_handler_result_t ret = RX_HANDLER_PASS;
|
|
|
|
lyr3h = ipvlan_get_L3_hdr(port, skb, &addr_type);
|
|
if (!lyr3h)
|
|
goto out;
|
|
|
|
addr = ipvlan_addr_lookup(port, lyr3h, addr_type, true);
|
|
if (addr)
|
|
ret = ipvlan_rcv_frame(addr, pskb, false);
|
|
|
|
out:
|
|
return ret;
|
|
}
|
|
|
|
static rx_handler_result_t ipvlan_handle_mode_l2(struct sk_buff **pskb,
|
|
struct ipvl_port *port)
|
|
{
|
|
struct sk_buff *skb = *pskb;
|
|
struct ethhdr *eth = eth_hdr(skb);
|
|
rx_handler_result_t ret = RX_HANDLER_PASS;
|
|
|
|
if (is_multicast_ether_addr(eth->h_dest)) {
|
|
if (ipvlan_external_frame(skb, port)) {
|
|
struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
|
|
|
|
/* External frames are queued for device local
|
|
* distribution, but a copy is given to master
|
|
* straight away to avoid sending duplicates later
|
|
* when work-queue processes this frame. This is
|
|
* achieved by returning RX_HANDLER_PASS.
|
|
*/
|
|
if (nskb) {
|
|
ipvlan_skb_crossing_ns(nskb, NULL);
|
|
ipvlan_multicast_enqueue(port, nskb, false);
|
|
}
|
|
}
|
|
} else {
|
|
/* Perform like l3 mode for non-multicast packet */
|
|
ret = ipvlan_handle_mode_l3(pskb, port);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
rx_handler_result_t ipvlan_handle_frame(struct sk_buff **pskb)
|
|
{
|
|
struct sk_buff *skb = *pskb;
|
|
struct ipvl_port *port = ipvlan_port_get_rcu(skb->dev);
|
|
|
|
if (!port)
|
|
return RX_HANDLER_PASS;
|
|
|
|
switch (port->mode) {
|
|
case IPVLAN_MODE_L2:
|
|
return ipvlan_handle_mode_l2(pskb, port);
|
|
case IPVLAN_MODE_L3:
|
|
return ipvlan_handle_mode_l3(pskb, port);
|
|
#ifdef CONFIG_IPVLAN_L3S
|
|
case IPVLAN_MODE_L3S:
|
|
return RX_HANDLER_PASS;
|
|
#endif
|
|
}
|
|
|
|
/* Should not reach here */
|
|
WARN_ONCE(true, "ipvlan_handle_frame() called for mode = [%hx]\n",
|
|
port->mode);
|
|
kfree_skb(skb);
|
|
return RX_HANDLER_CONSUMED;
|
|
}
|