@@ -256,6 +256,9 @@ struct tun_struct {
256256 struct tun_prog __rcu * steering_prog ;
257257 struct tun_prog __rcu * filter_prog ;
258258 struct ethtool_link_ksettings link_ksettings ;
259+ /* init args */
260+ struct file * file ;
261+ struct ifreq * ifr ;
259262};
260263
261264struct veth {
@@ -281,6 +284,9 @@ void *tun_ptr_to_xdp(void *ptr)
281284}
282285EXPORT_SYMBOL (tun_ptr_to_xdp );
283286
287+ static void tun_flow_init (struct tun_struct * tun );
288+ static void tun_flow_uninit (struct tun_struct * tun );
289+
284290static int tun_napi_receive (struct napi_struct * napi , int budget )
285291{
286292 struct tun_file * tfile = container_of (napi , struct tun_file , napi );
@@ -1038,6 +1044,49 @@ static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
10381044
10391045static const struct ethtool_ops tun_ethtool_ops ;
10401046
1047+ static int tun_net_init (struct net_device * dev )
1048+ {
1049+ struct tun_struct * tun = netdev_priv (dev );
1050+ struct ifreq * ifr = tun -> ifr ;
1051+ int err ;
1052+
1053+ tun -> pcpu_stats = netdev_alloc_pcpu_stats (struct tun_pcpu_stats );
1054+ if (!tun -> pcpu_stats )
1055+ return - ENOMEM ;
1056+
1057+ spin_lock_init (& tun -> lock );
1058+
1059+ err = security_tun_dev_alloc_security (& tun -> security );
1060+ if (err < 0 ) {
1061+ free_percpu (tun -> pcpu_stats );
1062+ return err ;
1063+ }
1064+
1065+ tun_flow_init (tun );
1066+
1067+ dev -> hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
1068+ TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
1069+ NETIF_F_HW_VLAN_STAG_TX ;
1070+ dev -> features = dev -> hw_features | NETIF_F_LLTX ;
1071+ dev -> vlan_features = dev -> features &
1072+ ~(NETIF_F_HW_VLAN_CTAG_TX |
1073+ NETIF_F_HW_VLAN_STAG_TX );
1074+
1075+ tun -> flags = (tun -> flags & ~TUN_FEATURES ) |
1076+ (ifr -> ifr_flags & TUN_FEATURES );
1077+
1078+ INIT_LIST_HEAD (& tun -> disabled );
1079+ err = tun_attach (tun , tun -> file , false, ifr -> ifr_flags & IFF_NAPI ,
1080+ ifr -> ifr_flags & IFF_NAPI_FRAGS , false);
1081+ if (err < 0 ) {
1082+ tun_flow_uninit (tun );
1083+ security_tun_dev_free_security (tun -> security );
1084+ free_percpu (tun -> pcpu_stats );
1085+ return err ;
1086+ }
1087+ return 0 ;
1088+ }
1089+
10411090/* Net device detach from fd. */
10421091static void tun_net_uninit (struct net_device * dev )
10431092{
@@ -1268,6 +1317,7 @@ static int tun_xdp(struct net_device *dev, struct netdev_bpf *xdp)
12681317}
12691318
12701319static const struct net_device_ops tun_netdev_ops = {
1320+ .ndo_init = tun_net_init ,
12711321 .ndo_uninit = tun_net_uninit ,
12721322 .ndo_open = tun_net_open ,
12731323 .ndo_stop = tun_net_close ,
@@ -1347,6 +1397,7 @@ static int tun_xdp_tx(struct net_device *dev, struct xdp_buff *xdp)
13471397}
13481398
13491399static const struct net_device_ops tap_netdev_ops = {
1400+ .ndo_init = tun_net_init ,
13501401 .ndo_uninit = tun_net_uninit ,
13511402 .ndo_open = tun_net_open ,
13521403 .ndo_stop = tun_net_close ,
@@ -1386,7 +1437,7 @@ static void tun_flow_uninit(struct tun_struct *tun)
13861437#define MAX_MTU 65535
13871438
13881439/* Initialize net device. */
1389- static void tun_net_init (struct net_device * dev )
1440+ static void tun_net_initialize (struct net_device * dev )
13901441{
13911442 struct tun_struct * tun = netdev_priv (dev );
13921443
@@ -2658,9 +2709,6 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
26582709
26592710 if (!dev )
26602711 return - ENOMEM ;
2661- err = dev_get_valid_name (net , dev , name );
2662- if (err < 0 )
2663- goto err_free_dev ;
26642712
26652713 dev_net_set (dev , net );
26662714 dev -> rtnl_link_ops = & tun_link_ops ;
@@ -2679,41 +2727,16 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
26792727 tun -> rx_batched = 0 ;
26802728 RCU_INIT_POINTER (tun -> steering_prog , NULL );
26812729
2682- tun -> pcpu_stats = netdev_alloc_pcpu_stats (struct tun_pcpu_stats );
2683- if (!tun -> pcpu_stats ) {
2684- err = - ENOMEM ;
2685- goto err_free_dev ;
2686- }
2687-
2688- spin_lock_init (& tun -> lock );
2689-
2690- err = security_tun_dev_alloc_security (& tun -> security );
2691- if (err < 0 )
2692- goto err_free_stat ;
2693-
2694- tun_net_init (dev );
2695- tun_flow_init (tun );
2696-
2697- dev -> hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
2698- TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
2699- NETIF_F_HW_VLAN_STAG_TX ;
2700- dev -> features = dev -> hw_features | NETIF_F_LLTX ;
2701- dev -> vlan_features = dev -> features &
2702- ~(NETIF_F_HW_VLAN_CTAG_TX |
2703- NETIF_F_HW_VLAN_STAG_TX );
2730+ tun -> ifr = ifr ;
2731+ tun -> file = file ;
27042732
2705- tun -> flags = (tun -> flags & ~TUN_FEATURES ) |
2706- (ifr -> ifr_flags & TUN_FEATURES );
2707-
2708- INIT_LIST_HEAD (& tun -> disabled );
2709- err = tun_attach (tun , file , false, ifr -> ifr_flags & IFF_NAPI ,
2710- ifr -> ifr_flags & IFF_NAPI_FRAGS , false);
2711- if (err < 0 )
2712- goto err_free_flow ;
2733+ tun_net_initialize (dev );
27132734
27142735 err = register_netdevice (tun -> dev );
2715- if (err < 0 )
2716- goto err_detach ;
2736+ if (err < 0 ) {
2737+ free_netdev (dev );
2738+ return err ;
2739+ }
27172740 /* free_netdev() won't check refcnt, to aovid race
27182741 * with dev_put() we need publish tun after registration.
27192742 */
@@ -2732,20 +2755,6 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
27322755
27332756 strcpy (ifr -> ifr_name , tun -> dev -> name );
27342757 return 0 ;
2735-
2736- err_detach :
2737- tun_detach_all (dev );
2738- /* register_netdevice() already called tun_free_netdev() */
2739- goto err_free_dev ;
2740-
2741- err_free_flow :
2742- tun_flow_uninit (tun );
2743- security_tun_dev_free_security (tun -> security );
2744- err_free_stat :
2745- free_percpu (tun -> pcpu_stats );
2746- err_free_dev :
2747- free_netdev (dev );
2748- return err ;
27492758}
27502759
27512760static void tun_get_iff (struct net * net , struct tun_struct * tun ,
0 commit comments