Lines Matching refs:tun
132 struct tun_struct __rcu *tun; member
158 struct tun_struct *tun; member
259 static void tun_napi_init(struct tun_struct *tun, struct tun_file *tfile, in tun_napi_init() argument
265 netif_tx_napi_add(tun->dev, &tfile->napi, tun_napi_poll, in tun_napi_init()
289 static inline bool tun_legacy_is_little_endian(struct tun_struct *tun) in tun_legacy_is_little_endian() argument
291 return tun->flags & TUN_VNET_BE ? false : in tun_legacy_is_little_endian()
295 static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp) in tun_get_vnet_be() argument
297 int be = !!(tun->flags & TUN_VNET_BE); in tun_get_vnet_be()
305 static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp) in tun_set_vnet_be() argument
313 tun->flags |= TUN_VNET_BE; in tun_set_vnet_be()
315 tun->flags &= ~TUN_VNET_BE; in tun_set_vnet_be()
320 static inline bool tun_legacy_is_little_endian(struct tun_struct *tun) in tun_legacy_is_little_endian() argument
325 static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp) in tun_get_vnet_be() argument
330 static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp) in tun_set_vnet_be() argument
336 static inline bool tun_is_little_endian(struct tun_struct *tun) in tun_is_little_endian() argument
338 return tun->flags & TUN_VNET_LE || in tun_is_little_endian()
339 tun_legacy_is_little_endian(tun); in tun_is_little_endian()
342 static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val) in tun16_to_cpu() argument
344 return __virtio16_to_cpu(tun_is_little_endian(tun), val); in tun16_to_cpu()
347 static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val) in cpu_to_tun16() argument
349 return __cpu_to_virtio16(tun_is_little_endian(tun), val); in cpu_to_tun16()
368 static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun, in tun_flow_create() argument
375 netif_info(tun, tx_queued, tun->dev, in tun_flow_create()
382 e->tun = tun; in tun_flow_create()
384 ++tun->flow_count; in tun_flow_create()
389 static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e) in tun_flow_delete() argument
391 netif_info(tun, tx_queued, tun->dev, "delete flow: hash %u index %u\n", in tun_flow_delete()
395 --tun->flow_count; in tun_flow_delete()
398 static void tun_flow_flush(struct tun_struct *tun) in tun_flow_flush() argument
402 spin_lock_bh(&tun->lock); in tun_flow_flush()
407 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) in tun_flow_flush()
408 tun_flow_delete(tun, e); in tun_flow_flush()
410 spin_unlock_bh(&tun->lock); in tun_flow_flush()
413 static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index) in tun_flow_delete_by_queue() argument
417 spin_lock_bh(&tun->lock); in tun_flow_delete_by_queue()
422 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { in tun_flow_delete_by_queue()
424 tun_flow_delete(tun, e); in tun_flow_delete_by_queue()
427 spin_unlock_bh(&tun->lock); in tun_flow_delete_by_queue()
432 struct tun_struct *tun = from_timer(tun, t, flow_gc_timer); in tun_flow_cleanup() local
433 unsigned long delay = tun->ageing_time; in tun_flow_cleanup()
438 spin_lock(&tun->lock); in tun_flow_cleanup()
443 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { in tun_flow_cleanup()
448 tun_flow_delete(tun, e); in tun_flow_cleanup()
458 mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer)); in tun_flow_cleanup()
459 spin_unlock(&tun->lock); in tun_flow_cleanup()
462 static void tun_flow_update(struct tun_struct *tun, u32 rxhash, in tun_flow_update() argument
467 unsigned long delay = tun->ageing_time; in tun_flow_update()
470 head = &tun->flows[tun_hashfn(rxhash)]; in tun_flow_update()
483 spin_lock_bh(&tun->lock); in tun_flow_update()
485 tun->flow_count < MAX_TAP_FLOWS) in tun_flow_update()
486 tun_flow_create(tun, head, rxhash, queue_index); in tun_flow_update()
488 if (!timer_pending(&tun->flow_gc_timer)) in tun_flow_update()
489 mod_timer(&tun->flow_gc_timer, in tun_flow_update()
491 spin_unlock_bh(&tun->lock); in tun_flow_update()
512 static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb) in tun_automq_select_queue() argument
518 numqueues = READ_ONCE(tun->numqueues); in tun_automq_select_queue()
521 e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq); in tun_automq_select_queue()
533 static u16 tun_ebpf_select_queue(struct tun_struct *tun, struct sk_buff *skb) in tun_ebpf_select_queue() argument
539 numqueues = READ_ONCE(tun->numqueues); in tun_ebpf_select_queue()
543 prog = rcu_dereference(tun->steering_prog); in tun_ebpf_select_queue()
553 struct tun_struct *tun = netdev_priv(dev); in tun_select_queue() local
557 if (rcu_dereference(tun->steering_prog)) in tun_select_queue()
558 ret = tun_ebpf_select_queue(tun, skb); in tun_select_queue()
560 ret = tun_automq_select_queue(tun, skb); in tun_select_queue()
566 static inline bool tun_not_capable(struct tun_struct *tun) in tun_not_capable() argument
569 struct net *net = dev_net(tun->dev); in tun_not_capable()
571 return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) || in tun_not_capable()
572 (gid_valid(tun->group) && !in_egroup_p(tun->group))) && in tun_not_capable()
576 static void tun_set_real_num_queues(struct tun_struct *tun) in tun_set_real_num_queues() argument
578 netif_set_real_num_tx_queues(tun->dev, tun->numqueues); in tun_set_real_num_queues()
579 netif_set_real_num_rx_queues(tun->dev, tun->numqueues); in tun_set_real_num_queues()
582 static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile) in tun_disable_queue() argument
584 tfile->detached = tun; in tun_disable_queue()
585 list_add_tail(&tfile->next, &tun->disabled); in tun_disable_queue()
586 ++tun->numdisabled; in tun_disable_queue()
591 struct tun_struct *tun = tfile->detached; in tun_enable_queue() local
595 --tun->numdisabled; in tun_enable_queue()
596 return tun; in tun_enable_queue()
627 struct tun_struct *tun; in __tun_detach() local
629 tun = rtnl_dereference(tfile->tun); in __tun_detach()
631 if (tun && clean) { in __tun_detach()
636 if (tun && !tfile->detached) { in __tun_detach()
638 BUG_ON(index >= tun->numqueues); in __tun_detach()
640 rcu_assign_pointer(tun->tfiles[index], in __tun_detach()
641 tun->tfiles[tun->numqueues - 1]); in __tun_detach()
642 ntfile = rtnl_dereference(tun->tfiles[index]); in __tun_detach()
644 rcu_assign_pointer(tun->tfiles[tun->numqueues - 1], in __tun_detach()
647 --tun->numqueues; in __tun_detach()
649 RCU_INIT_POINTER(tfile->tun, NULL); in __tun_detach()
652 tun_disable_queue(tun, tfile); in __tun_detach()
655 tun_flow_delete_by_queue(tun, tun->numqueues + 1); in __tun_detach()
658 tun_set_real_num_queues(tun); in __tun_detach()
660 tun = tun_enable_queue(tfile); in __tun_detach()
665 if (tun && tun->numqueues == 0 && tun->numdisabled == 0) { in __tun_detach()
666 netif_carrier_off(tun->dev); in __tun_detach()
668 if (!(tun->flags & IFF_PERSIST) && in __tun_detach()
669 tun->dev->reg_state == NETREG_REGISTERED) in __tun_detach()
670 unregister_netdevice(tun->dev); in __tun_detach()
672 if (tun) in __tun_detach()
681 struct tun_struct *tun; in tun_detach() local
685 tun = rtnl_dereference(tfile->tun); in tun_detach()
686 dev = tun ? tun->dev : NULL; in tun_detach()
695 struct tun_struct *tun = netdev_priv(dev); in tun_detach_all() local
697 int i, n = tun->numqueues; in tun_detach_all()
700 tfile = rtnl_dereference(tun->tfiles[i]); in tun_detach_all()
705 RCU_INIT_POINTER(tfile->tun, NULL); in tun_detach_all()
706 --tun->numqueues; in tun_detach_all()
708 list_for_each_entry(tfile, &tun->disabled, next) { in tun_detach_all()
711 RCU_INIT_POINTER(tfile->tun, NULL); in tun_detach_all()
713 BUG_ON(tun->numqueues != 0); in tun_detach_all()
717 tfile = rtnl_dereference(tun->tfiles[i]); in tun_detach_all()
724 list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) { in tun_detach_all()
730 BUG_ON(tun->numdisabled != 0); in tun_detach_all()
732 if (tun->flags & IFF_PERSIST) in tun_detach_all()
736 static int tun_attach(struct tun_struct *tun, struct file *file, in tun_attach() argument
741 struct net_device *dev = tun->dev; in tun_attach()
744 err = security_tun_dev_attach(tfile->socket.sk, tun->security); in tun_attach()
749 if (rtnl_dereference(tfile->tun) && !tfile->detached) in tun_attach()
753 if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1) in tun_attach()
758 tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES) in tun_attach()
764 if (!skip_filter && (tun->filter_attached == true)) { in tun_attach()
766 err = sk_attach_filter(&tun->fprog, tfile->socket.sk); in tun_attach()
779 tfile->queue_index = tun->numqueues; in tun_attach()
791 tun->dev, tfile->queue_index, 0); in tun_attach()
807 tun_napi_init(tun, tfile, napi, napi_frags); in tun_attach()
810 if (rtnl_dereference(tun->xdp_prog)) in tun_attach()
822 rcu_assign_pointer(tfile->tun, tun); in tun_attach()
823 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile); in tun_attach()
824 tun->numqueues++; in tun_attach()
825 tun_set_real_num_queues(tun); in tun_attach()
832 struct tun_struct *tun; in tun_get() local
835 tun = rcu_dereference(tfile->tun); in tun_get()
836 if (tun) in tun_get()
837 dev_hold(tun->dev); in tun_get()
840 return tun; in tun_get()
843 static void tun_put(struct tun_struct *tun) in tun_put() argument
845 dev_put(tun->dev); in tun_put()
978 static void tun_automq_xmit(struct tun_struct *tun, struct sk_buff *skb) in tun_automq_xmit() argument
981 if (tun->numqueues == 1 && static_branch_unlikely(&rps_needed)) { in tun_automq_xmit()
989 e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)], rxhash); in tun_automq_xmit()
996 static unsigned int run_ebpf_filter(struct tun_struct *tun, in run_ebpf_filter() argument
1000 struct tun_prog *prog = rcu_dereference(tun->filter_prog); in run_ebpf_filter()
1011 struct tun_struct *tun = netdev_priv(dev); in tun_net_xmit() local
1017 tfile = rcu_dereference(tun->tfiles[txq]); in tun_net_xmit()
1023 if (!rcu_dereference(tun->steering_prog)) in tun_net_xmit()
1024 tun_automq_xmit(tun, skb); in tun_net_xmit()
1026 netif_info(tun, tx_queued, tun->dev, "%s %d\n", __func__, skb->len); in tun_net_xmit()
1031 if (!check_filter(&tun->txflt, skb)) in tun_net_xmit()
1038 len = run_ebpf_filter(tun, skb, len); in tun_net_xmit()
1085 struct tun_struct *tun = netdev_priv(dev); in tun_net_fix_features() local
1087 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES); in tun_net_fix_features()
1092 struct tun_struct *tun = netdev_priv(dev); in tun_set_headroom() local
1097 tun->align = new_hr; in tun_set_headroom()
1103 struct tun_struct *tun = netdev_priv(dev); in tun_net_get_stats64() local
1108 (unsigned long)atomic_long_read(&tun->rx_frame_errors); in tun_net_get_stats64()
1114 struct tun_struct *tun = netdev_priv(dev); in tun_xdp_set() local
1119 old_prog = rtnl_dereference(tun->xdp_prog); in tun_xdp_set()
1120 rcu_assign_pointer(tun->xdp_prog, prog); in tun_xdp_set()
1124 for (i = 0; i < tun->numqueues; i++) { in tun_xdp_set()
1125 tfile = rtnl_dereference(tun->tfiles[i]); in tun_xdp_set()
1131 list_for_each_entry(tfile, &tun->disabled, next) { in tun_xdp_set()
1154 struct tun_struct *tun = netdev_priv(dev); in tun_net_change_carrier() local
1156 if (!tun->numqueues) in tun_net_change_carrier()
1189 struct tun_struct *tun = netdev_priv(dev); in tun_xdp_xmit() local
1201 numqueues = READ_ONCE(tun->numqueues); in tun_xdp_xmit()
1207 tfile = rcu_dereference(tun->tfiles[smp_processor_id() % in tun_xdp_xmit()
1267 static void tun_flow_init(struct tun_struct *tun) in tun_flow_init() argument
1272 INIT_HLIST_HEAD(&tun->flows[i]); in tun_flow_init()
1274 tun->ageing_time = TUN_FLOW_EXPIRE; in tun_flow_init()
1275 timer_setup(&tun->flow_gc_timer, tun_flow_cleanup, 0); in tun_flow_init()
1276 mod_timer(&tun->flow_gc_timer, in tun_flow_init()
1277 round_jiffies_up(jiffies + tun->ageing_time)); in tun_flow_init()
1280 static void tun_flow_uninit(struct tun_struct *tun) in tun_flow_uninit() argument
1282 del_timer_sync(&tun->flow_gc_timer); in tun_flow_uninit()
1283 tun_flow_flush(tun); in tun_flow_uninit()
1292 struct tun_struct *tun = netdev_priv(dev); in tun_net_init() local
1294 switch (tun->flags & TUN_TYPE_MASK) { in tun_net_init()
1325 static bool tun_sock_writeable(struct tun_struct *tun, struct tun_file *tfile) in tun_sock_writeable() argument
1329 return (tun->dev->flags & IFF_UP) && sock_writeable(sk); in tun_sock_writeable()
1338 struct tun_struct *tun = tun_get(tfile); in tun_chr_poll() local
1342 if (!tun) in tun_chr_poll()
1357 if (tun_sock_writeable(tun, tfile) || in tun_chr_poll()
1359 tun_sock_writeable(tun, tfile))) in tun_chr_poll()
1362 if (tun->dev->reg_state != NETREG_REGISTERED) in tun_chr_poll()
1365 tun_put(tun); in tun_chr_poll()
1449 static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile, in tun_rx_batched() argument
1454 u32 rx_batched = tun->rx_batched; in tun_rx_batched()
1489 static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile, in tun_can_build_skb() argument
1492 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) in tun_can_build_skb()
1530 static int tun_xdp_act(struct tun_struct *tun, struct bpf_prog *xdp_prog, in tun_xdp_act() argument
1537 err = xdp_do_redirect(tun->dev, xdp, xdp_prog); in tun_xdp_act()
1542 err = tun_xdp_tx(tun->dev, xdp); in tun_xdp_act()
1552 trace_xdp_exception(tun->dev, xdp_prog, act); in tun_xdp_act()
1555 atomic_long_inc(&tun->dev->rx_dropped); in tun_xdp_act()
1562 static struct sk_buff *tun_build_skb(struct tun_struct *tun, in tun_build_skb() argument
1577 xdp_prog = rcu_dereference(tun->xdp_prog); in tun_build_skb()
1608 xdp_prog = rcu_dereference(tun->xdp_prog); in tun_build_skb()
1621 err = tun_xdp_act(tun, xdp_prog, &xdp, act); in tun_build_skb()
1648 static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, in tun_get_user() argument
1655 size_t len = total_len, align = tun->align, linear; in tun_get_user()
1665 if (!(tun->flags & IFF_NO_PI)) { in tun_get_user()
1674 if (tun->flags & IFF_VNET_HDR) { in tun_get_user()
1675 int vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz); in tun_get_user()
1685 …tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso… in tun_get_user()
1686 …gso.hdr_len = cpu_to_tun16(tun, tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_off… in tun_get_user()
1688 if (tun16_to_cpu(tun, gso.hdr_len) > len) in tun_get_user()
1693 if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) { in tun_get_user()
1696 (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN))) in tun_get_user()
1709 copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN; in tun_get_user()
1718 if (!frags && tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) { in tun_get_user()
1723 skb = tun_build_skb(tun, tfile, from, &gso, len, &skb_xdp); in tun_get_user()
1725 atomic_long_inc(&tun->dev->rx_dropped); in tun_get_user()
1733 if (tun16_to_cpu(tun, gso.hdr_len) > good_linear) in tun_get_user()
1736 linear = tun16_to_cpu(tun, gso.hdr_len); in tun_get_user()
1754 atomic_long_inc(&tun->dev->rx_dropped); in tun_get_user()
1768 atomic_long_inc(&tun->dev->rx_dropped); in tun_get_user()
1779 if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) { in tun_get_user()
1780 atomic_long_inc(&tun->rx_frame_errors); in tun_get_user()
1790 switch (tun->flags & TUN_TYPE_MASK) { in tun_get_user()
1792 if (tun->flags & IFF_NO_PI) { in tun_get_user()
1803 atomic_long_inc(&tun->dev->rx_dropped); in tun_get_user()
1811 skb->dev = tun->dev; in tun_get_user()
1818 skb->protocol = eth_type_trans(skb, tun->dev); in tun_get_user()
1840 xdp_prog = rcu_dereference(tun->xdp_prog); in tun_get_user()
1861 if (!rcu_access_pointer(tun->steering_prog) && tun->numqueues > 1 && in tun_get_user()
1866 if (unlikely(!(tun->dev->flags & IFF_UP))) { in tun_get_user()
1877 headlen = eth_get_headlen(tun->dev, skb->data, in tun_get_user()
1881 atomic_long_inc(&tun->dev->rx_dropped); in tun_get_user()
1907 tun_rx_batched(tun, tfile, skb, more); in tun_get_user()
1914 dev_sw_netstats_rx_add(tun->dev, len); in tun_get_user()
1918 tun_flow_update(tun, rxhash, tfile); in tun_get_user()
1927 struct tun_struct *tun = tun_get(tfile); in tun_chr_write_iter() local
1931 if (!tun) in tun_chr_write_iter()
1937 result = tun_get_user(tun, tfile, NULL, from, noblock, false); in tun_chr_write_iter()
1939 tun_put(tun); in tun_chr_write_iter()
1943 static ssize_t tun_put_user_xdp(struct tun_struct *tun, in tun_put_user_xdp() argument
1952 if (tun->flags & IFF_VNET_HDR) { in tun_put_user_xdp()
1955 vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz); in tun_put_user_xdp()
1967 dev_sw_netstats_tx_add(tun->dev, 1, ret); in tun_put_user_xdp()
1974 static ssize_t tun_put_user(struct tun_struct *tun, in tun_put_user() argument
1988 if (tun->flags & IFF_VNET_HDR) in tun_put_user()
1989 vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz); in tun_put_user()
1993 if (!(tun->flags & IFF_NO_PI)) { in tun_put_user()
2014 tun_is_little_endian(tun), true, in tun_put_user()
2019 sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size), in tun_put_user()
2020 tun16_to_cpu(tun, gso.hdr_len)); in tun_put_user()
2024 min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true); in tun_put_user()
2058 dev_sw_netstats_tx_add(tun->dev, 1, skb->len + vlan_hlen); in tun_put_user()
2105 static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile, in tun_do_read() argument
2127 ret = tun_put_user_xdp(tun, tfile, xdpf, to); in tun_do_read()
2132 ret = tun_put_user(tun, tfile, skb, to); in tun_do_read()
2146 struct tun_struct *tun = tun_get(tfile); in tun_chr_read_iter() local
2150 if (!tun) in tun_chr_read_iter()
2156 ret = tun_do_read(tun, tfile, to, noblock, NULL); in tun_chr_read_iter()
2160 tun_put(tun); in tun_chr_read_iter()
2172 static int __tun_set_ebpf(struct tun_struct *tun, in __tun_set_ebpf() argument
2185 spin_lock_bh(&tun->lock); in __tun_set_ebpf()
2187 lockdep_is_held(&tun->lock)); in __tun_set_ebpf()
2189 spin_unlock_bh(&tun->lock); in __tun_set_ebpf()
2199 struct tun_struct *tun = netdev_priv(dev); in tun_free_netdev() local
2201 BUG_ON(!(list_empty(&tun->disabled))); in tun_free_netdev()
2209 tun_flow_uninit(tun); in tun_free_netdev()
2210 security_tun_dev_free_security(tun->security); in tun_free_netdev()
2211 __tun_set_ebpf(tun, &tun->steering_prog, NULL); in tun_free_netdev()
2212 __tun_set_ebpf(tun, &tun->filter_prog, NULL); in tun_free_netdev()
2217 struct tun_struct *tun = netdev_priv(dev); in tun_setup() local
2219 tun->owner = INVALID_UID; in tun_setup()
2220 tun->group = INVALID_GID; in tun_setup()
2221 tun_default_link_ksettings(dev, &tun->link_ksettings); in tun_setup()
2260 struct tun_struct *tun = netdev_priv(dev); in tun_fill_info() local
2262 if (nla_put_u8(skb, IFLA_TUN_TYPE, tun->flags & TUN_TYPE_MASK)) in tun_fill_info()
2264 if (uid_valid(tun->owner) && in tun_fill_info()
2266 from_kuid_munged(current_user_ns(), tun->owner))) in tun_fill_info()
2268 if (gid_valid(tun->group) && in tun_fill_info()
2270 from_kgid_munged(current_user_ns(), tun->group))) in tun_fill_info()
2272 if (nla_put_u8(skb, IFLA_TUN_PI, !(tun->flags & IFF_NO_PI))) in tun_fill_info()
2274 if (nla_put_u8(skb, IFLA_TUN_VNET_HDR, !!(tun->flags & IFF_VNET_HDR))) in tun_fill_info()
2276 if (nla_put_u8(skb, IFLA_TUN_PERSIST, !!(tun->flags & IFF_PERSIST))) in tun_fill_info()
2279 !!(tun->flags & IFF_MULTI_QUEUE))) in tun_fill_info()
2281 if (tun->flags & IFF_MULTI_QUEUE) { in tun_fill_info()
2282 if (nla_put_u32(skb, IFLA_TUN_NUM_QUEUES, tun->numqueues)) in tun_fill_info()
2285 tun->numdisabled)) in tun_fill_info()
2330 static int tun_xdp_one(struct tun_struct *tun, in tun_xdp_one() argument
2346 xdp_prog = rcu_dereference(tun->xdp_prog); in tun_xdp_one()
2357 err = tun_xdp_act(tun, xdp_prog, xdp, act); in tun_xdp_one()
2394 if (virtio_net_hdr_to_skb(skb, gso, tun_is_little_endian(tun))) { in tun_xdp_one()
2395 atomic_long_inc(&tun->rx_frame_errors); in tun_xdp_one()
2401 skb->protocol = eth_type_trans(skb, tun->dev); in tun_xdp_one()
2412 if (!rcu_dereference(tun->steering_prog) && tun->numqueues > 1 && in tun_xdp_one()
2421 dev_sw_netstats_rx_add(tun->dev, datasize); in tun_xdp_one()
2424 tun_flow_update(tun, rxhash, tfile); in tun_xdp_one()
2434 struct tun_struct *tun = tun_get(tfile); in tun_sendmsg() local
2438 if (!tun) in tun_sendmsg()
2453 tun_xdp_one(tun, tfile, xdp, &flush, &tpage); in tun_sendmsg()
2468 ret = tun_get_user(tun, tfile, ctl ? ctl->ptr : NULL, &m->msg_iter, in tun_sendmsg()
2472 tun_put(tun); in tun_sendmsg()
2480 struct tun_struct *tun = tun_get(tfile); in tun_recvmsg() local
2484 if (!tun) { in tun_recvmsg()
2498 ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT, ptr); in tun_recvmsg()
2504 tun_put(tun); in tun_recvmsg()
2508 tun_put(tun); in tun_recvmsg()
2531 struct tun_struct *tun; in tun_peek_len() local
2534 tun = tun_get(tfile); in tun_peek_len()
2535 if (!tun) in tun_peek_len()
2539 tun_put(tun); in tun_peek_len()
2557 static int tun_flags(struct tun_struct *tun) in tun_flags() argument
2559 return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP); in tun_flags()
2565 struct tun_struct *tun = netdev_priv(to_net_dev(dev)); in tun_flags_show() local
2566 return sprintf(buf, "0x%x\n", tun_flags(tun)); in tun_flags_show()
2572 struct tun_struct *tun = netdev_priv(to_net_dev(dev)); in owner_show() local
2573 return uid_valid(tun->owner)? in owner_show()
2575 from_kuid_munged(current_user_ns(), tun->owner)): in owner_show()
2582 struct tun_struct *tun = netdev_priv(to_net_dev(dev)); in group_show() local
2583 return gid_valid(tun->group) ? in group_show()
2585 from_kgid_munged(current_user_ns(), tun->group)): in group_show()
2606 struct tun_struct *tun; in tun_set_iff() local
2628 tun = netdev_priv(dev); in tun_set_iff()
2630 tun = netdev_priv(dev); in tun_set_iff()
2635 !!(tun->flags & IFF_MULTI_QUEUE)) in tun_set_iff()
2638 if (tun_not_capable(tun)) in tun_set_iff()
2640 err = security_tun_dev_open(tun->security); in tun_set_iff()
2644 err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER, in tun_set_iff()
2650 if (tun->flags & IFF_MULTI_QUEUE && in tun_set_iff()
2651 (tun->numqueues + tun->numdisabled > 1)) { in tun_set_iff()
2659 tun->flags = (tun->flags & ~TUN_FEATURES) | in tun_set_iff()
2702 tun = netdev_priv(dev); in tun_set_iff()
2703 tun->dev = dev; in tun_set_iff()
2704 tun->flags = flags; in tun_set_iff()
2705 tun->txflt.count = 0; in tun_set_iff()
2706 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr); in tun_set_iff()
2708 tun->align = NET_SKB_PAD; in tun_set_iff()
2709 tun->filter_attached = false; in tun_set_iff()
2710 tun->sndbuf = tfile->socket.sk->sk_sndbuf; in tun_set_iff()
2711 tun->rx_batched = 0; in tun_set_iff()
2712 RCU_INIT_POINTER(tun->steering_prog, NULL); in tun_set_iff()
2720 spin_lock_init(&tun->lock); in tun_set_iff()
2722 err = security_tun_dev_alloc_security(&tun->security); in tun_set_iff()
2727 tun_flow_init(tun); in tun_set_iff()
2737 tun->flags = (tun->flags & ~TUN_FEATURES) | in tun_set_iff()
2740 INIT_LIST_HEAD(&tun->disabled); in tun_set_iff()
2741 err = tun_attach(tun, file, false, ifr->ifr_flags & IFF_NAPI, in tun_set_iff()
2746 err = register_netdevice(tun->dev); in tun_set_iff()
2752 rcu_assign_pointer(tfile->tun, tun); in tun_set_iff()
2755 netif_carrier_on(tun->dev); in tun_set_iff()
2760 if (netif_running(tun->dev)) in tun_set_iff()
2761 netif_tx_wake_all_queues(tun->dev); in tun_set_iff()
2763 strcpy(ifr->ifr_name, tun->dev->name); in tun_set_iff()
2776 tun_flow_uninit(tun); in tun_set_iff()
2777 security_tun_dev_free_security(tun->security); in tun_set_iff()
2785 static void tun_get_iff(struct tun_struct *tun, struct ifreq *ifr) in tun_get_iff() argument
2787 strcpy(ifr->ifr_name, tun->dev->name); in tun_get_iff()
2789 ifr->ifr_flags = tun_flags(tun); in tun_get_iff()
2795 static int set_offload(struct tun_struct *tun, unsigned long arg) in set_offload() argument
2823 tun->set_features = features; in set_offload()
2824 tun->dev->wanted_features &= ~TUN_USER_FEATURES; in set_offload()
2825 tun->dev->wanted_features |= features; in set_offload()
2826 netdev_update_features(tun->dev); in set_offload()
2831 static void tun_detach_filter(struct tun_struct *tun, int n) in tun_detach_filter() argument
2837 tfile = rtnl_dereference(tun->tfiles[i]); in tun_detach_filter()
2843 tun->filter_attached = false; in tun_detach_filter()
2846 static int tun_attach_filter(struct tun_struct *tun) in tun_attach_filter() argument
2851 for (i = 0; i < tun->numqueues; i++) { in tun_attach_filter()
2852 tfile = rtnl_dereference(tun->tfiles[i]); in tun_attach_filter()
2854 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk); in tun_attach_filter()
2857 tun_detach_filter(tun, i); in tun_attach_filter()
2862 tun->filter_attached = true; in tun_attach_filter()
2866 static void tun_set_sndbuf(struct tun_struct *tun) in tun_set_sndbuf() argument
2871 for (i = 0; i < tun->numqueues; i++) { in tun_set_sndbuf()
2872 tfile = rtnl_dereference(tun->tfiles[i]); in tun_set_sndbuf()
2873 tfile->socket.sk->sk_sndbuf = tun->sndbuf; in tun_set_sndbuf()
2880 struct tun_struct *tun; in tun_set_queue() local
2886 tun = tfile->detached; in tun_set_queue()
2887 if (!tun) { in tun_set_queue()
2891 ret = security_tun_dev_attach_queue(tun->security); in tun_set_queue()
2894 ret = tun_attach(tun, file, false, tun->flags & IFF_NAPI, in tun_set_queue()
2895 tun->flags & IFF_NAPI_FRAGS, true); in tun_set_queue()
2897 tun = rtnl_dereference(tfile->tun); in tun_set_queue()
2898 if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached) in tun_set_queue()
2906 netdev_state_change(tun->dev); in tun_set_queue()
2913 static int tun_set_ebpf(struct tun_struct *tun, struct tun_prog __rcu **prog_p, in tun_set_ebpf() argument
2930 return __tun_set_ebpf(tun, prog_p, prog); in tun_set_ebpf()
2977 struct tun_struct *tun; in __tun_chr_ioctl() local
3013 tun = tun_get(tfile); in __tun_chr_ioctl()
3016 if (tun) in __tun_chr_ioctl()
3032 if (tun) in __tun_chr_ioctl()
3045 if (!tun) in __tun_chr_ioctl()
3048 netif_info(tun, drv, tun->dev, "tun_chr_ioctl cmd %u\n", cmd); in __tun_chr_ioctl()
3050 net = dev_net(tun->dev); in __tun_chr_ioctl()
3054 tun_get_iff(tun, &ifr); in __tun_chr_ioctl()
3069 netif_info(tun, drv, tun->dev, "ignored: set checksum %s\n", in __tun_chr_ioctl()
3077 if (arg && !(tun->flags & IFF_PERSIST)) { in __tun_chr_ioctl()
3078 tun->flags |= IFF_PERSIST; in __tun_chr_ioctl()
3082 if (!arg && (tun->flags & IFF_PERSIST)) { in __tun_chr_ioctl()
3083 tun->flags &= ~IFF_PERSIST; in __tun_chr_ioctl()
3088 netif_info(tun, drv, tun->dev, "persist %s\n", in __tun_chr_ioctl()
3099 tun->owner = owner; in __tun_chr_ioctl()
3101 netif_info(tun, drv, tun->dev, "owner set to %u\n", in __tun_chr_ioctl()
3102 from_kuid(&init_user_ns, tun->owner)); in __tun_chr_ioctl()
3112 tun->group = group; in __tun_chr_ioctl()
3114 netif_info(tun, drv, tun->dev, "group set to %u\n", in __tun_chr_ioctl()
3115 from_kgid(&init_user_ns, tun->group)); in __tun_chr_ioctl()
3120 if (tun->dev->flags & IFF_UP) { in __tun_chr_ioctl()
3121 netif_info(tun, drv, tun->dev, in __tun_chr_ioctl()
3126 tun->dev); in __tun_chr_ioctl()
3129 netif_info(tun, drv, tun->dev, in __tun_chr_ioctl()
3133 tun->dev->type = (int) arg; in __tun_chr_ioctl()
3134 tun->dev->addr_len = tun_get_addr_len(tun->dev->type); in __tun_chr_ioctl()
3135 netif_info(tun, drv, tun->dev, "linktype set to %d\n", in __tun_chr_ioctl()
3136 tun->dev->type); in __tun_chr_ioctl()
3138 tun->dev); in __tun_chr_ioctl()
3143 tun->msg_enable = (u32)arg; in __tun_chr_ioctl()
3147 ret = set_offload(tun, arg); in __tun_chr_ioctl()
3153 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) in __tun_chr_ioctl()
3155 ret = update_filter(&tun->txflt, (void __user *)arg); in __tun_chr_ioctl()
3160 dev_get_mac_address(&ifr.ifr_hwaddr, net, tun->dev->name); in __tun_chr_ioctl()
3167 ret = dev_set_mac_address_user(tun->dev, &ifr.ifr_hwaddr, NULL); in __tun_chr_ioctl()
3186 tun->sndbuf = sndbuf; in __tun_chr_ioctl()
3187 tun_set_sndbuf(tun); in __tun_chr_ioctl()
3191 vnet_hdr_sz = tun->vnet_hdr_sz; in __tun_chr_ioctl()
3206 tun->vnet_hdr_sz = vnet_hdr_sz; in __tun_chr_ioctl()
3210 le = !!(tun->flags & TUN_VNET_LE); in __tun_chr_ioctl()
3221 tun->flags |= TUN_VNET_LE; in __tun_chr_ioctl()
3223 tun->flags &= ~TUN_VNET_LE; in __tun_chr_ioctl()
3227 ret = tun_get_vnet_be(tun, argp); in __tun_chr_ioctl()
3231 ret = tun_set_vnet_be(tun, argp); in __tun_chr_ioctl()
3237 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) in __tun_chr_ioctl()
3240 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog))) in __tun_chr_ioctl()
3243 ret = tun_attach_filter(tun); in __tun_chr_ioctl()
3249 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) in __tun_chr_ioctl()
3252 tun_detach_filter(tun, tun->numqueues); in __tun_chr_ioctl()
3257 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) in __tun_chr_ioctl()
3260 if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog))) in __tun_chr_ioctl()
3266 ret = tun_set_ebpf(tun, &tun->steering_prog, argp); in __tun_chr_ioctl()
3270 ret = tun_set_ebpf(tun, &tun->filter_prog, argp); in __tun_chr_ioctl()
3278 ret = tun_net_change_carrier(tun->dev, (bool)carrier); in __tun_chr_ioctl()
3294 netdev_state_change(tun->dev); in __tun_chr_ioctl()
3298 if (tun) in __tun_chr_ioctl()
3299 tun_put(tun); in __tun_chr_ioctl()
3371 RCU_INIT_POINTER(tfile->tun, NULL); in tun_chr_open()
3406 struct tun_struct *tun; in tun_chr_show_fdinfo() local
3412 tun = tun_get(tfile); in tun_chr_show_fdinfo()
3413 if (tun) in tun_chr_show_fdinfo()
3414 tun_get_iff(tun, &ifr); in tun_chr_show_fdinfo()
3417 if (tun) in tun_chr_show_fdinfo()
3418 tun_put(tun); in tun_chr_show_fdinfo()
3466 struct tun_struct *tun = netdev_priv(dev); in tun_get_link_ksettings() local
3468 memcpy(cmd, &tun->link_ksettings, sizeof(*cmd)); in tun_get_link_ksettings()
3475 struct tun_struct *tun = netdev_priv(dev); in tun_set_link_ksettings() local
3477 memcpy(&tun->link_ksettings, cmd, sizeof(*cmd)); in tun_set_link_ksettings()
3483 struct tun_struct *tun = netdev_priv(dev); in tun_get_drvinfo() local
3488 switch (tun->flags & TUN_TYPE_MASK) { in tun_get_drvinfo()
3500 struct tun_struct *tun = netdev_priv(dev); in tun_get_msglevel() local
3502 return tun->msg_enable; in tun_get_msglevel()
3507 struct tun_struct *tun = netdev_priv(dev); in tun_set_msglevel() local
3509 tun->msg_enable = value; in tun_set_msglevel()
3517 struct tun_struct *tun = netdev_priv(dev); in tun_get_coalesce() local
3519 ec->rx_max_coalesced_frames = tun->rx_batched; in tun_get_coalesce()
3529 struct tun_struct *tun = netdev_priv(dev); in tun_set_coalesce() local
3532 tun->rx_batched = NAPI_POLL_WEIGHT; in tun_set_coalesce()
3534 tun->rx_batched = ec->rx_max_coalesced_frames; in tun_set_coalesce()
3552 static int tun_queue_resize(struct tun_struct *tun) in tun_queue_resize() argument
3554 struct net_device *dev = tun->dev; in tun_queue_resize()
3557 int n = tun->numqueues + tun->numdisabled; in tun_queue_resize()
3564 for (i = 0; i < tun->numqueues; i++) { in tun_queue_resize()
3565 tfile = rtnl_dereference(tun->tfiles[i]); in tun_queue_resize()
3568 list_for_each_entry(tfile, &tun->disabled, next) in tun_queue_resize()
3583 struct tun_struct *tun = netdev_priv(dev); in tun_device_event() local
3591 if (tun_queue_resize(tun)) in tun_device_event()
3595 for (i = 0; i < tun->numqueues; i++) { in tun_device_event()
3598 tfile = rtnl_dereference(tun->tfiles[i]); in tun_device_event()