tun.c (45a15d89fbcd280571eba8e5ca309e14ba6afa8f) | tun.c (4b4f052e2d89c2eb7e13ee28ba9e85f8097aef3d) |
---|---|
1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * TUN - Universal TUN/TAP device driver. 4 * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com> 5 * 6 * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $ 7 */ 8 --- 1044 unchanged lines hidden (view full) --- 1053 1054 return len; 1055} 1056 1057/* Net device start xmit */ 1058static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) 1059{ 1060 struct tun_struct *tun = netdev_priv(dev); | 1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * TUN - Universal TUN/TAP device driver. 4 * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com> 5 * 6 * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $ 7 */ 8 --- 1044 unchanged lines hidden (view full) --- 1053 1054 return len; 1055} 1056 1057/* Net device start xmit */ 1058static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) 1059{ 1060 struct tun_struct *tun = netdev_priv(dev); |
1061 enum skb_drop_reason drop_reason; |
|
1061 int txq = skb->queue_mapping; 1062 struct netdev_queue *queue; 1063 struct tun_file *tfile; 1064 int len = skb->len; 1065 1066 rcu_read_lock(); 1067 tfile = rcu_dereference(tun->tfiles[txq]); 1068 1069 /* Drop packet if interface is not attached */ | 1062 int txq = skb->queue_mapping; 1063 struct netdev_queue *queue; 1064 struct tun_file *tfile; 1065 int len = skb->len; 1066 1067 rcu_read_lock(); 1068 tfile = rcu_dereference(tun->tfiles[txq]); 1069 1070 /* Drop packet if interface is not attached */ |
1070 if (!tfile) | 1071 if (!tfile) { 1072 drop_reason = SKB_DROP_REASON_DEV_READY; |
1071 goto drop; | 1073 goto drop; |
1074 } |
|
1072 1073 if (!rcu_dereference(tun->steering_prog)) 1074 tun_automq_xmit(tun, skb); 1075 1076 netif_info(tun, tx_queued, tun->dev, "%s %d\n", __func__, skb->len); 1077 1078 /* Drop if the filter does not like it. 1079 * This is a noop if the filter is disabled. 1080 * Filter can be enabled only for the TAP devices. */ | 1075 1076 if (!rcu_dereference(tun->steering_prog)) 1077 tun_automq_xmit(tun, skb); 1078 1079 netif_info(tun, tx_queued, tun->dev, "%s %d\n", __func__, skb->len); 1080 1081 /* Drop if the filter does not like it. 1082 * This is a noop if the filter is disabled. 1083 * Filter can be enabled only for the TAP devices. */ |
1081 if (!check_filter(&tun->txflt, skb)) | 1084 if (!check_filter(&tun->txflt, skb)) { 1085 drop_reason = SKB_DROP_REASON_TAP_TXFILTER; |
1082 goto drop; | 1086 goto drop; |
1087 } |
|
1083 1084 if (tfile->socket.sk->sk_filter && | 1088 1089 if (tfile->socket.sk->sk_filter && |
1085 sk_filter(tfile->socket.sk, skb)) | 1090 sk_filter(tfile->socket.sk, skb)) { 1091 drop_reason = SKB_DROP_REASON_SOCKET_FILTER; |
1086 goto drop; | 1092 goto drop; |
1093 } |
|
1087 1088 len = run_ebpf_filter(tun, skb, len); | 1094 1095 len = run_ebpf_filter(tun, skb, len); |
1089 if (len == 0) | 1096 if (len == 0) { 1097 drop_reason = SKB_DROP_REASON_TAP_FILTER; |
1090 goto drop; | 1098 goto drop; |
1099 } |
|
1091 | 1100 |
1092 if (pskb_trim(skb, len)) | 1101 if (pskb_trim(skb, len)) { 1102 drop_reason = SKB_DROP_REASON_NOMEM; |
1093 goto drop; | 1103 goto drop; |
1104 } |
|
1094 | 1105 |
1095 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) | 1106 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) { 1107 drop_reason = SKB_DROP_REASON_SKB_UCOPY_FAULT; |
1096 goto drop; | 1108 goto drop; |
1109 } |
|
1097 1098 skb_tx_timestamp(skb); 1099 1100 /* Orphan the skb - required as we might hang on to it 1101 * for indefinite time. 1102 */ 1103 skb_orphan(skb); 1104 1105 nf_reset_ct(skb); 1106 | 1110 1111 skb_tx_timestamp(skb); 1112 1113 /* Orphan the skb - required as we might hang on to it 1114 * for indefinite time. 1115 */ 1116 skb_orphan(skb); 1117 1118 nf_reset_ct(skb); 1119 |
1107 if (ptr_ring_produce(&tfile->tx_ring, skb)) | 1120 if (ptr_ring_produce(&tfile->tx_ring, skb)) { 1121 drop_reason = SKB_DROP_REASON_FULL_RING; |
1108 goto drop; | 1122 goto drop; |
1123 } |
|
1109 1110 /* NETIF_F_LLTX requires to do our own update of trans_start */ 1111 queue = netdev_get_tx_queue(dev, txq); 1112 queue->trans_start = jiffies; 1113 1114 /* Notify and wake up reader process */ 1115 if (tfile->flags & TUN_FASYNC) 1116 kill_fasync(&tfile->fasync, SIGIO, POLL_IN); 1117 tfile->socket.sk->sk_data_ready(tfile->socket.sk); 1118 1119 rcu_read_unlock(); 1120 return NETDEV_TX_OK; 1121 1122drop: 1123 atomic_long_inc(&dev->tx_dropped); 1124 skb_tx_error(skb); | 1124 1125 /* NETIF_F_LLTX requires to do our own update of trans_start */ 1126 queue = netdev_get_tx_queue(dev, txq); 1127 queue->trans_start = jiffies; 1128 1129 /* Notify and wake up reader process */ 1130 if (tfile->flags & TUN_FASYNC) 1131 kill_fasync(&tfile->fasync, SIGIO, POLL_IN); 1132 tfile->socket.sk->sk_data_ready(tfile->socket.sk); 1133 1134 rcu_read_unlock(); 1135 return NETDEV_TX_OK; 1136 1137drop: 1138 atomic_long_inc(&dev->tx_dropped); 1139 skb_tx_error(skb); |
1125 kfree_skb(skb); | 1140 kfree_skb_reason(skb, drop_reason); |
1126 rcu_read_unlock(); 1127 return NET_XMIT_DROP; 1128} 1129 1130static void tun_net_mclist(struct net_device *dev) 1131{ 1132 /* 1133 * This callback is supposed to deal with mc filter in --- 581 unchanged lines hidden (view full) --- 1715 struct virtio_net_hdr gso = { 0 }; 1716 int good_linear; 1717 int copylen; 1718 bool zerocopy = false; 1719 int err; 1720 u32 rxhash = 0; 1721 int skb_xdp = 1; 1722 bool frags = tun_napi_frags_enabled(tfile); | 1141 rcu_read_unlock(); 1142 return NET_XMIT_DROP; 1143} 1144 1145static void tun_net_mclist(struct net_device *dev) 1146{ 1147 /* 1148 * This callback is supposed to deal with mc filter in --- 581 unchanged lines hidden (view full) --- 1730 struct virtio_net_hdr gso = { 0 }; 1731 int good_linear; 1732 int copylen; 1733 bool zerocopy = false; 1734 int err; 1735 u32 rxhash = 0; 1736 int skb_xdp = 1; 1737 bool frags = tun_napi_frags_enabled(tfile); |
1738 enum skb_drop_reason drop_reason; |
|
1723 1724 if (!(tun->flags & IFF_NO_PI)) { 1725 if (len < sizeof(pi)) 1726 return -EINVAL; 1727 len -= sizeof(pi); 1728 1729 if (!copy_from_iter_full(&pi, sizeof(pi), from)) 1730 return -EFAULT; --- 87 unchanged lines hidden (view full) --- 1818 1819 if (zerocopy) 1820 err = zerocopy_sg_from_iter(skb, from); 1821 else 1822 err = skb_copy_datagram_from_iter(skb, 0, from, len); 1823 1824 if (err) { 1825 err = -EFAULT; | 1739 1740 if (!(tun->flags & IFF_NO_PI)) { 1741 if (len < sizeof(pi)) 1742 return -EINVAL; 1743 len -= sizeof(pi); 1744 1745 if (!copy_from_iter_full(&pi, sizeof(pi), from)) 1746 return -EFAULT; --- 87 unchanged lines hidden (view full) --- 1834 1835 if (zerocopy) 1836 err = zerocopy_sg_from_iter(skb, from); 1837 else 1838 err = skb_copy_datagram_from_iter(skb, 0, from, len); 1839 1840 if (err) { 1841 err = -EFAULT; |
1842 drop_reason = SKB_DROP_REASON_SKB_UCOPY_FAULT; |
|
1826drop: 1827 atomic_long_inc(&tun->dev->rx_dropped); | 1843drop: 1844 atomic_long_inc(&tun->dev->rx_dropped); |
1828 kfree_skb(skb); | 1845 kfree_skb_reason(skb, drop_reason); |
1829 if (frags) { 1830 tfile->napi.skb = NULL; 1831 mutex_unlock(&tfile->napi_mutex); 1832 } 1833 1834 return err; 1835 } 1836 } --- 30 unchanged lines hidden (view full) --- 1867 1868 skb_reset_mac_header(skb); 1869 skb->protocol = pi.proto; 1870 skb->dev = tun->dev; 1871 break; 1872 case IFF_TAP: 1873 if (frags && !pskb_may_pull(skb, ETH_HLEN)) { 1874 err = -ENOMEM; | 1846 if (frags) { 1847 tfile->napi.skb = NULL; 1848 mutex_unlock(&tfile->napi_mutex); 1849 } 1850 1851 return err; 1852 } 1853 } --- 30 unchanged lines hidden (view full) --- 1884 1885 skb_reset_mac_header(skb); 1886 skb->protocol = pi.proto; 1887 skb->dev = tun->dev; 1888 break; 1889 case IFF_TAP: 1890 if (frags && !pskb_may_pull(skb, ETH_HLEN)) { 1891 err = -ENOMEM; |
1892 drop_reason = SKB_DROP_REASON_HDR_TRUNC; |
|
1875 goto drop; 1876 } 1877 skb->protocol = eth_type_trans(skb, tun->dev); 1878 break; 1879 } 1880 1881 /* copy skb_ubuf_info for callback when skb has no error */ 1882 if (zerocopy) { --- 37 unchanged lines hidden (view full) --- 1920 if (!rcu_access_pointer(tun->steering_prog) && tun->numqueues > 1 && 1921 !tfile->detached) 1922 rxhash = __skb_get_hash_symmetric(skb); 1923 1924 rcu_read_lock(); 1925 if (unlikely(!(tun->dev->flags & IFF_UP))) { 1926 err = -EIO; 1927 rcu_read_unlock(); | 1893 goto drop; 1894 } 1895 skb->protocol = eth_type_trans(skb, tun->dev); 1896 break; 1897 } 1898 1899 /* copy skb_ubuf_info for callback when skb has no error */ 1900 if (zerocopy) { --- 37 unchanged lines hidden (view full) --- 1938 if (!rcu_access_pointer(tun->steering_prog) && tun->numqueues > 1 && 1939 !tfile->detached) 1940 rxhash = __skb_get_hash_symmetric(skb); 1941 1942 rcu_read_lock(); 1943 if (unlikely(!(tun->dev->flags & IFF_UP))) { 1944 err = -EIO; 1945 rcu_read_unlock(); |
1946 drop_reason = SKB_DROP_REASON_DEV_READY; |
|
1928 goto drop; 1929 } 1930 1931 if (frags) { 1932 u32 headlen; 1933 1934 /* Exercise flow dissector code path. */ 1935 skb_push(skb, ETH_HLEN); --- 1783 unchanged lines hidden --- | 1947 goto drop; 1948 } 1949 1950 if (frags) { 1951 u32 headlen; 1952 1953 /* Exercise flow dissector code path. */ 1954 skb_push(skb, ETH_HLEN); --- 1783 unchanged lines hidden --- |