@@ -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 );
@@ -953,6 +959,49 @@ static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
953
959
954
960
static const struct ethtool_ops tun_ethtool_ops ;
955
961
962
+ static int tun_net_init (struct net_device * dev )
963
+ {
964
+ struct tun_struct * tun = netdev_priv (dev );
965
+ struct ifreq * ifr = tun -> ifr ;
966
+ int err ;
967
+
968
+ dev -> tstats = netdev_alloc_pcpu_stats (struct pcpu_sw_netstats );
969
+ if (!dev -> tstats )
970
+ return - ENOMEM ;
971
+
972
+ spin_lock_init (& tun -> lock );
973
+
974
+ err = security_tun_dev_alloc_security (& tun -> security );
975
+ if (err < 0 ) {
976
+ free_percpu (dev -> tstats );
977
+ return err ;
978
+ }
979
+
980
+ tun_flow_init (tun );
981
+
982
+ dev -> hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
983
+ TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
984
+ NETIF_F_HW_VLAN_STAG_TX ;
985
+ dev -> features = dev -> hw_features | NETIF_F_LLTX ;
986
+ dev -> vlan_features = dev -> features &
987
+ ~(NETIF_F_HW_VLAN_CTAG_TX |
988
+ NETIF_F_HW_VLAN_STAG_TX );
989
+
990
+ tun -> flags = (tun -> flags & ~TUN_FEATURES ) |
991
+ (ifr -> ifr_flags & TUN_FEATURES );
992
+
993
+ INIT_LIST_HEAD (& tun -> disabled );
994
+ err = tun_attach (tun , tun -> file , false, ifr -> ifr_flags & IFF_NAPI ,
995
+ ifr -> ifr_flags & IFF_NAPI_FRAGS , false);
996
+ if (err < 0 ) {
997
+ tun_flow_uninit (tun );
998
+ security_tun_dev_free_security (tun -> security );
999
+ free_percpu (dev -> tstats );
1000
+ return err ;
1001
+ }
1002
+ return 0 ;
1003
+ }
1004
+
956
1005
/* Net device detach from fd. */
957
1006
static void tun_net_uninit (struct net_device * dev )
958
1007
{
@@ -1169,6 +1218,7 @@ static int tun_net_change_carrier(struct net_device *dev, bool new_carrier)
1169
1218
}
1170
1219
1171
1220
static const struct net_device_ops tun_netdev_ops = {
1221
+ .ndo_init = tun_net_init ,
1172
1222
.ndo_uninit = tun_net_uninit ,
1173
1223
.ndo_open = tun_net_open ,
1174
1224
.ndo_stop = tun_net_close ,
@@ -1252,6 +1302,7 @@ static int tun_xdp_tx(struct net_device *dev, struct xdp_buff *xdp)
1252
1302
}
1253
1303
1254
1304
static const struct net_device_ops tap_netdev_ops = {
1305
+ .ndo_init = tun_net_init ,
1255
1306
.ndo_uninit = tun_net_uninit ,
1256
1307
.ndo_open = tun_net_open ,
1257
1308
.ndo_stop = tun_net_close ,
@@ -1292,7 +1343,7 @@ static void tun_flow_uninit(struct tun_struct *tun)
1292
1343
#define MAX_MTU 65535
1293
1344
1294
1345
/* Initialize net device. */
1295
- static void tun_net_init (struct net_device * dev )
1346
+ static void tun_net_initialize (struct net_device * dev )
1296
1347
{
1297
1348
struct tun_struct * tun = netdev_priv (dev );
1298
1349
@@ -2206,11 +2257,6 @@ static void tun_free_netdev(struct net_device *dev)
2206
2257
BUG_ON (!(list_empty (& tun -> disabled )));
2207
2258
2208
2259
free_percpu (dev -> tstats );
2209
- /* We clear tstats so that tun_set_iff() can tell if
2210
- * tun_free_netdev() has been called from register_netdevice().
2211
- */
2212
- dev -> tstats = NULL ;
2213
-
2214
2260
tun_flow_uninit (tun );
2215
2261
security_tun_dev_free_security (tun -> security );
2216
2262
__tun_set_ebpf (tun , & tun -> steering_prog , NULL );
@@ -2716,41 +2762,16 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
2716
2762
tun -> rx_batched = 0 ;
2717
2763
RCU_INIT_POINTER (tun -> steering_prog , NULL );
2718
2764
2719
- dev -> tstats = netdev_alloc_pcpu_stats (struct pcpu_sw_netstats );
2720
- if (!dev -> tstats ) {
2721
- err = - ENOMEM ;
2722
- goto err_free_dev ;
2723
- }
2724
-
2725
- spin_lock_init (& tun -> lock );
2726
-
2727
- err = security_tun_dev_alloc_security (& tun -> security );
2728
- if (err < 0 )
2729
- goto err_free_stat ;
2730
-
2731
- tun_net_init (dev );
2732
- tun_flow_init (tun );
2765
+ tun -> ifr = ifr ;
2766
+ tun -> file = file ;
2733
2767
2734
- dev -> hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
2735
- TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
2736
- NETIF_F_HW_VLAN_STAG_TX ;
2737
- dev -> features = dev -> hw_features | NETIF_F_LLTX ;
2738
- dev -> vlan_features = dev -> features &
2739
- ~(NETIF_F_HW_VLAN_CTAG_TX |
2740
- NETIF_F_HW_VLAN_STAG_TX );
2741
-
2742
- tun -> flags = (tun -> flags & ~TUN_FEATURES ) |
2743
- (ifr -> ifr_flags & TUN_FEATURES );
2744
-
2745
- INIT_LIST_HEAD (& tun -> disabled );
2746
- err = tun_attach (tun , file , false, ifr -> ifr_flags & IFF_NAPI ,
2747
- ifr -> ifr_flags & IFF_NAPI_FRAGS , false);
2748
- if (err < 0 )
2749
- goto err_free_flow ;
2768
+ tun_net_initialize (dev );
2750
2769
2751
2770
err = register_netdevice (tun -> dev );
2752
- if (err < 0 )
2753
- goto err_detach ;
2771
+ if (err < 0 ) {
2772
+ free_netdev (dev );
2773
+ return err ;
2774
+ }
2754
2775
/* free_netdev() won't check refcnt, to avoid race
2755
2776
* with dev_put() we need publish tun after registration.
2756
2777
*/
@@ -2767,24 +2788,6 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
2767
2788
2768
2789
strcpy (ifr -> ifr_name , tun -> dev -> name );
2769
2790
return 0 ;
2770
-
2771
- err_detach :
2772
- tun_detach_all (dev );
2773
- /* We are here because register_netdevice() has failed.
2774
- * If register_netdevice() already called tun_free_netdev()
2775
- * while dealing with the error, dev->stats has been cleared.
2776
- */
2777
- if (!dev -> tstats )
2778
- goto err_free_dev ;
2779
-
2780
- err_free_flow :
2781
- tun_flow_uninit (tun );
2782
- security_tun_dev_free_security (tun -> security );
2783
- err_free_stat :
2784
- free_percpu (dev -> tstats );
2785
- err_free_dev :
2786
- free_netdev (dev );
2787
- return err ;
2788
2791
}
2789
2792
2790
2793
static void tun_get_iff (struct tun_struct * tun , struct ifreq * ifr )
0 commit comments