@@ -209,13 +209,19 @@ struct tun_struct {
209
209
struct tun_prog __rcu * steering_prog ;
210
210
struct tun_prog __rcu * filter_prog ;
211
211
struct ethtool_link_ksettings link_ksettings ;
212
+ /* init args */
213
+ struct file * file ;
214
+ struct ifreq * ifr ;
212
215
};
213
216
214
217
struct veth {
215
218
__be16 h_vlan_proto ;
216
219
__be16 h_vlan_TCI ;
217
220
};
218
221
222
+ static void tun_flow_init (struct tun_struct * tun );
223
+ static void tun_flow_uninit (struct tun_struct * tun );
224
+
219
225
static int tun_napi_receive (struct napi_struct * napi , int budget )
220
226
{
221
227
struct tun_file * tfile = container_of (napi , struct tun_file , napi );
@@ -952,6 +958,49 @@ static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
952
958
953
959
static const struct ethtool_ops tun_ethtool_ops ;
954
960
961
+ static int tun_net_init (struct net_device * dev )
962
+ {
963
+ struct tun_struct * tun = netdev_priv (dev );
964
+ struct ifreq * ifr = tun -> ifr ;
965
+ int err ;
966
+
967
+ dev -> tstats = netdev_alloc_pcpu_stats (struct pcpu_sw_netstats );
968
+ if (!dev -> tstats )
969
+ return - ENOMEM ;
970
+
971
+ spin_lock_init (& tun -> lock );
972
+
973
+ err = security_tun_dev_alloc_security (& tun -> security );
974
+ if (err < 0 ) {
975
+ free_percpu (dev -> tstats );
976
+ return err ;
977
+ }
978
+
979
+ tun_flow_init (tun );
980
+
981
+ dev -> hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
982
+ TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
983
+ NETIF_F_HW_VLAN_STAG_TX ;
984
+ dev -> features = dev -> hw_features | NETIF_F_LLTX ;
985
+ dev -> vlan_features = dev -> features &
986
+ ~(NETIF_F_HW_VLAN_CTAG_TX |
987
+ NETIF_F_HW_VLAN_STAG_TX );
988
+
989
+ tun -> flags = (tun -> flags & ~TUN_FEATURES ) |
990
+ (ifr -> ifr_flags & TUN_FEATURES );
991
+
992
+ INIT_LIST_HEAD (& tun -> disabled );
993
+ err = tun_attach (tun , tun -> file , false, ifr -> ifr_flags & IFF_NAPI ,
994
+ ifr -> ifr_flags & IFF_NAPI_FRAGS , false);
995
+ if (err < 0 ) {
996
+ tun_flow_uninit (tun );
997
+ security_tun_dev_free_security (tun -> security );
998
+ free_percpu (dev -> tstats );
999
+ return err ;
1000
+ }
1001
+ return 0 ;
1002
+ }
1003
+
955
1004
/* Net device detach from fd. */
956
1005
static void tun_net_uninit (struct net_device * dev )
957
1006
{
@@ -1186,6 +1235,7 @@ static int tun_net_change_carrier(struct net_device *dev, bool new_carrier)
1186
1235
}
1187
1236
1188
1237
static const struct net_device_ops tun_netdev_ops = {
1238
+ .ndo_init = tun_net_init ,
1189
1239
.ndo_uninit = tun_net_uninit ,
1190
1240
.ndo_open = tun_net_open ,
1191
1241
.ndo_stop = tun_net_close ,
@@ -1269,6 +1319,7 @@ static int tun_xdp_tx(struct net_device *dev, struct xdp_buff *xdp)
1269
1319
}
1270
1320
1271
1321
static const struct net_device_ops tap_netdev_ops = {
1322
+ .ndo_init = tun_net_init ,
1272
1323
.ndo_uninit = tun_net_uninit ,
1273
1324
.ndo_open = tun_net_open ,
1274
1325
.ndo_stop = tun_net_close ,
@@ -1309,7 +1360,7 @@ static void tun_flow_uninit(struct tun_struct *tun)
1309
1360
#define MAX_MTU 65535
1310
1361
1311
1362
/* Initialize net device. */
1312
- static void tun_net_init (struct net_device * dev )
1363
+ static void tun_net_initialize (struct net_device * dev )
1313
1364
{
1314
1365
struct tun_struct * tun = netdev_priv (dev );
1315
1366
@@ -2227,11 +2278,6 @@ static void tun_free_netdev(struct net_device *dev)
2227
2278
BUG_ON (!(list_empty (& tun -> disabled )));
2228
2279
2229
2280
free_percpu (dev -> tstats );
2230
- /* We clear tstats so that tun_set_iff() can tell if
2231
- * tun_free_netdev() has been called from register_netdevice().
2232
- */
2233
- dev -> tstats = NULL ;
2234
-
2235
2281
tun_flow_uninit (tun );
2236
2282
security_tun_dev_free_security (tun -> security );
2237
2283
__tun_set_ebpf (tun , & tun -> steering_prog , NULL );
@@ -2737,41 +2783,16 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
2737
2783
tun -> rx_batched = 0 ;
2738
2784
RCU_INIT_POINTER (tun -> steering_prog , NULL );
2739
2785
2740
- dev -> tstats = netdev_alloc_pcpu_stats (struct pcpu_sw_netstats );
2741
- if (!dev -> tstats ) {
2742
- err = - ENOMEM ;
2743
- goto err_free_dev ;
2744
- }
2745
-
2746
- spin_lock_init (& tun -> lock );
2747
-
2748
- err = security_tun_dev_alloc_security (& tun -> security );
2749
- if (err < 0 )
2750
- goto err_free_stat ;
2751
-
2752
- tun_net_init (dev );
2753
- tun_flow_init (tun );
2786
+ tun -> ifr = ifr ;
2787
+ tun -> file = file ;
2754
2788
2755
- dev -> hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
2756
- TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
2757
- NETIF_F_HW_VLAN_STAG_TX ;
2758
- dev -> features = dev -> hw_features | NETIF_F_LLTX ;
2759
- dev -> vlan_features = dev -> features &
2760
- ~(NETIF_F_HW_VLAN_CTAG_TX |
2761
- NETIF_F_HW_VLAN_STAG_TX );
2762
-
2763
- tun -> flags = (tun -> flags & ~TUN_FEATURES ) |
2764
- (ifr -> ifr_flags & TUN_FEATURES );
2765
-
2766
- INIT_LIST_HEAD (& tun -> disabled );
2767
- err = tun_attach (tun , file , false, ifr -> ifr_flags & IFF_NAPI ,
2768
- ifr -> ifr_flags & IFF_NAPI_FRAGS , false);
2769
- if (err < 0 )
2770
- goto err_free_flow ;
2789
+ tun_net_initialize (dev );
2771
2790
2772
2791
err = register_netdevice (tun -> dev );
2773
- if (err < 0 )
2774
- goto err_detach ;
2792
+ if (err < 0 ) {
2793
+ free_netdev (dev );
2794
+ return err ;
2795
+ }
2775
2796
/* free_netdev() won't check refcnt, to avoid race
2776
2797
* with dev_put() we need publish tun after registration.
2777
2798
*/
@@ -2788,24 +2809,6 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
2788
2809
2789
2810
strcpy (ifr -> ifr_name , tun -> dev -> name );
2790
2811
return 0 ;
2791
-
2792
- err_detach :
2793
- tun_detach_all (dev );
2794
- /* We are here because register_netdevice() has failed.
2795
- * If register_netdevice() already called tun_free_netdev()
2796
- * while dealing with the error, dev->stats has been cleared.
2797
- */
2798
- if (!dev -> tstats )
2799
- goto err_free_dev ;
2800
-
2801
- err_free_flow :
2802
- tun_flow_uninit (tun );
2803
- security_tun_dev_free_security (tun -> security );
2804
- err_free_stat :
2805
- free_percpu (dev -> tstats );
2806
- err_free_dev :
2807
- free_netdev (dev );
2808
- return err ;
2809
2812
}
2810
2813
2811
2814
static void tun_get_iff (struct tun_struct * tun , struct ifreq * ifr )
0 commit comments