use symbol_get to create the magic ipv4/ipv6 tunnels
Subsystems (3)
Hi Dave,
both the ipv4 and ipv6 code have an ioctl each that can be used to create a tunnel using code that doesn't live in the core kernel or ipv6 module. Currently they call ioctls on the tunnel devices to create these, for which the code needs to override the address limit, which is a "feature" I plan to get rid of.
Instead this patchset makes the ipip and sit modules export a function that can be used to create the tunnels, and then uses symbol_get in the core ipv4/ipv6 code to reference that function at runtime.
PATCH 1/4 ipv4: streamline ipmr_new_tunnel
Reduce a few level of indentation to simplify the function.
--- net/ipv4/ipmr.c | 73 ++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 37 deletions(-)
net/ipv4/ipmr.c +36 -37
@@ -469,50 +469,49 @@ static bool ipmr_init_vif_indev(const struct net_device *dev)static struct net_device *ipmr_new_tunnel (struct net *net,struct vifctl *v) { -struct net_device *dev; +mm_segment_t oldfs =get_fs (); +struct net_device *tunnel_dev, *new_dev; +struct ip_tunnel_parm p = { }; +struct ifreq ifr; +int err; - dev =__dev_get_by_name (net,"tunl0" ); + tunnel_dev =__dev_get_by_name (net,"tunl0" ); +if (!tunnel_dev) + goto out; -if (dev) { -const struct net_device_ops *ops = dev->netdev_ops; -int err; -struct ifreq ifr; -struct ip_tunnel_parm p; + p.iph.daddr = v->vifc_rmt_addr.s_addr; + p.iph.saddr = v->vifc_lcl_addr.s_addr; + p.iph.version =4 ; + p.iph.ihl =5 ; + p.iph.protocol =IPPROTO_IPIP ; +sprintf (p.name,"dvmrp%d" , v->vifc_vifi); + ifr.ifr_ifru.ifru_data = (__force void __user *)&p; -memset (&p,0 ,sizeof (p)); - p.iph.daddr = v->vifc_rmt_addr.s_addr; - p.iph.saddr = v->vifc_lcl_addr.s_addr; - p.iph.version =4 ; - p.iph.ihl =5 ; - p.iph.protocol =IPPROTO_IPIP ; -sprintf (p.name,"dvmrp%d" , v->vifc_vifi); - ifr.ifr_ifru.ifru_data = (__force void __user *)&p; +if (!tunnel_dev->netdev_ops->ndo_do_ioctl) + goto out; -if (ops->ndo_do_ioctl) { -mm_segment_t oldfs =get_fs (); +set_fs (KERNEL_DS ); + err = tunnel_dev->netdev_ops->ndo_do_ioctl (tunnel_dev, &ifr, +SIOCADDTUNNEL ); +set_fs (oldfs); +if (err) + goto out; -set_fs (KERNEL_DS ); - err = ops->ndo_do_ioctl (dev, &ifr,SIOCADDTUNNEL ); -set_fs (oldfs); - }else { - err = -EOPNOTSUPP ; - } - dev =NULL ; + new_dev =__dev_get_by_name (net, p.name); +if (!new_dev) + goto out; -if (err ==0 && - (dev =__dev_get_by_name (net, p.name)) !=NULL ) { - dev->flags |=IFF_MULTICAST ; -if (!ipmr_init_vif_indev (dev)) - goto failure; -if (dev_open (dev,NULL )) - goto failure; -dev_hold (dev); - } - } -return dev; + new_dev->flags |=IFF_MULTICAST ; +if (!ipmr_init_vif_indev (new_dev)) + goto out_unregister; +if (dev_open (new_dev,NULL )) + goto out_unregister; +dev_hold (new_dev); +return new_dev; -failure: -unregister_netdevice (dev); +out_unregister: +unregister_netdevice (new_dev); +out:return NULL ; }
Signature · 1 line
2.26.2
PATCH 2/4 ipv4: consolidate the VIFF_TUNNEL handling in ipmr_new_tunnel
In reply to Christoph HellwigAlso move the dev_set_allmulti call and the error handling into the ioctl helper. This allows reusing already looked up tunnel_dev pointer and the set up argument structure for the deletion in the error handler.
--- net/ipv4/ipmr.c | 53 ++++++++++++------------------------------------- 1 file changed, 13 insertions(+), 40 deletions(-)
net/ipv4/ipmr.c +13 -40
@@ -419,37 +419,6 @@ static void ipmr_free_table(struct mr_table *mrt)/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */ -static void ipmr_del_tunnel (struct net_device *dev,struct vifctl *v) -{ -struct net *net =dev_net (dev); - -dev_close (dev); - - dev =__dev_get_by_name (net,"tunl0" ); -if (dev) { -const struct net_device_ops *ops = dev->netdev_ops; -struct ifreq ifr; -struct ip_tunnel_parm p; - -memset (&p,0 ,sizeof (p)); - p.iph.daddr = v->vifc_rmt_addr.s_addr; - p.iph.saddr = v->vifc_lcl_addr.s_addr; - p.iph.version =4 ; - p.iph.ihl =5 ; - p.iph.protocol =IPPROTO_IPIP ; -sprintf (p.name,"dvmrp%d" , v->vifc_vifi); - ifr.ifr_ifru.ifru_data = (__force void __user *)&p; - -if (ops->ndo_do_ioctl) { -mm_segment_t oldfs =get_fs (); - -set_fs (KERNEL_DS ); - ops->ndo_do_ioctl (dev, &ifr,SIOCDELTUNNEL ); -set_fs (oldfs); - } - } -} -/* Initialize ipmr pimreg/tunnel in_device */ static bool ipmr_init_vif_indev (const struct net_device *dev) {
@@ -507,12 +476,22 @@ static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)if (dev_open (new_dev,NULL )) goto out_unregister;dev_hold (new_dev); + err =dev_set_allmulti (new_dev,1 ); +if (err) { +dev_close (new_dev); +set_fs (KERNEL_DS ); + tunnel_dev->netdev_ops->ndo_do_ioctl (tunnel_dev, &ifr, +SIOCDELTUNNEL ); +set_fs (oldfs); +dev_put (new_dev); + new_dev =ERR_PTR (err); + }return new_dev; out_unregister:unregister_netdevice (new_dev); out: -return NULL ; +return ERR_PTR (-ENOBUFS ); }#if defined(CONFIG_IP_PIMSM_V1 ) || defined(CONFIG_IP_PIMSM_V2 )
@@ -864,14 +843,8 @@ static int vif_add(struct net *net, struct mr_table *mrt,break ;case VIFF_TUNNEL : dev =ipmr_new_tunnel (net, vifc); -if (!dev) -return -ENOBUFS ; - err =dev_set_allmulti (dev,1 ); -if (err) { -ipmr_del_tunnel (dev, vifc); -dev_put (dev); -return err; - } +if (IS_ERR (dev)) +return PTR_ERR (dev);break ;case VIFF_USE_IFINDEX :case 0 :
Signature · 1 line
2.26.2
PATCH 3/4 ipv4: use symbol_get to access ipip symbols
In reply to Christoph HellwigInstead of going through the ioctl handler from kernel space, use symbol_get to access the ip_tunnel_ioctl directly.
--- net/ipv4/ipmr.c | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-)
net/ipv4/ipmr.c +12 -16
@@ -438,10 +438,9 @@ static bool ipmr_init_vif_indev(const struct net_device *dev)static struct net_device *ipmr_new_tunnel (struct net *net,struct vifctl *v) { -mm_segment_t oldfs =get_fs (); +int (*tunnel_ctl)(struct net_device *,struct ip_tunnel_parm *,int );struct net_device *tunnel_dev, *new_dev;struct ip_tunnel_parm p = { }; -struct ifreq ifr;int err; tunnel_dev =__dev_get_by_name (net,"tunl0" );
@@ -454,21 +453,17 @@ static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v) p.iph.ihl =5 ; p.iph.protocol =IPPROTO_IPIP ;sprintf (p.name,"dvmrp%d" , v->vifc_vifi); - ifr.ifr_ifru.ifru_data = (__force void __user *)&p; -if (!tunnel_dev->netdev_ops->ndo_do_ioctl) - goto out; + tunnel_ctl =symbol_get (ip_tunnel_ioctl); +if (!tunnel_ctl) +return ERR_PTR (-ENOBUFS ); -set_fs (KERNEL_DS ); - err = tunnel_dev->netdev_ops->ndo_do_ioctl (tunnel_dev, &ifr, -SIOCADDTUNNEL ); -set_fs (oldfs); -if (err) - goto out; +if (tunnel_ctl (tunnel_dev, &p,SIOCADDTUNNEL )) + goto out_symbol_put; new_dev =__dev_get_by_name (net, p.name);if (!new_dev) - goto out; + goto out_symbol_put; new_dev->flags |=IFF_MULTICAST ;if (!ipmr_init_vif_indev (new_dev))
@@ -479,17 +474,18 @@ static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v) err =dev_set_allmulti (new_dev,1 );if (err) {dev_close (new_dev); -set_fs (KERNEL_DS ); - tunnel_dev->netdev_ops->ndo_do_ioctl (tunnel_dev, &ifr, -SIOCDELTUNNEL ); -set_fs (oldfs); +tunnel_ctl (tunnel_dev, &p,SIOCDELTUNNEL );dev_put (new_dev); new_dev =ERR_PTR (err); } + +symbol_put (ip_tunnel_ioctl);return new_dev; out_unregister:unregister_netdevice (new_dev); +out_symbol_put: +symbol_put (ipmr_new_tunnel); out:return ERR_PTR (-ENOBUFS ); }
Signature · 1 line
2.26.2
PATCH 4/4 ipv6: symbol_get to access a sit symbol
In reply to Christoph HellwigInstead of going through the ioctl handler from kernel space, use symbol_get to the newly factored out ipip6_set_dstaddr helper, bypassing addrconf.c entirely.
--- include/net/addrconf.h | 1 - include/net/ipv6.h | 2 ++ net/ipv6/addrconf.c | 66 ------------------------------------------ net/ipv6/af_inet6.c | 20 ++++++++++++- net/ipv6/sit.c | 41 ++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 68 deletions(-)
include/net/addrconf.h +0 -1
@@ -76,7 +76,6 @@ void addrconf_cleanup(void);int addrconf_add_ifaddr (struct net *net,void __user *arg);int addrconf_del_ifaddr (struct net *net,void __user *arg); -int addrconf_set_dstaddr (struct net *net,void __user *arg);int ipv6_chk_addr (struct net *net,const struct in6_addr *addr,const struct net_device *dev,int strict);
include/net/ipv6.h +2 -0
@@ -1080,6 +1080,8 @@ struct in6_addr *fl6_update_dst(struct flowi6 *fl6,const struct ipv6_txoptions *opt, struct in6_addr *orig); +int ipip6_set_dstaddr (struct net *net,void __user *arg); +/* * socket options (ipv6_sockglue.c) */
net/ipv6/addrconf.c +0 -66
72 lines not on this page · raw message
net/ipv6/af_inet6.c +19 -1
@@ -538,6 +538,19 @@ int inet6_getname(struct socket *sock, struct sockaddr *uaddr, }EXPORT_SYMBOL (inet6_getname); +static int inet6_ioctl_set_dstaddr (struct net *net,void __user *arg) +{ +int (*set_dstaddr)(struct net *,void __user *); +int err; + + set_dstaddr =symbol_get (ipip6_set_dstaddr); +if (!set_dstaddr) +return -EOPNOTSUPP ; + err =set_dstaddr (net, arg); +symbol_put (ipip6_set_dstaddr); +return err; +} +int inet6_ioctl (struct socket *sock,unsigned int cmd,unsigned long arg) {struct sock *sk = sock->sk;
@@ -554,7 +567,12 @@ int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)case SIOCDIFADDR :return addrconf_del_ifaddr (net, (void __user *) arg);case SIOCSIFDSTADDR : -return addrconf_set_dstaddr (net, (void __user *) arg); +/* Special case for SIT interfaces where we create a new +* "virtual" device. +*/ +if (!IS_ENABLED (CONFIG_IPV6_SIT )) +return -ENODEV ; +return inet6_ioctl_set_dstaddr (net, (void __user *) arg);default :if (!sk->sk_prot->ioctl)return -ENOIOCTLCMD ;
net/ipv6/sit.c +41 -0
47 lines not on this page · raw message
Signature · 1 line
2.26.2
David Miller
maintainerRe: [PATCH 4/4] ipv6: symbol_get to access a sit symbol
From: Christoph Hellwig <[email protected]>
Date: Thu, 14 May 2020 16:51:01 +0200
Quoted 5 lines
Instead of going through the ioctl handler from kernel space, use
symbol_getto the newly factored outipip6_set_dstaddrhelper, bypassingaddrconf.centirely.Signed-off-by Christoph Hellwig <[email protected]>
...
Quoted 17 lines
net/ipv6/addrconf.c +0
-17-memset (&p,0 ,sizeof (p)); - p.iph.daddr = ireq.ifr6_addr.s6_addr32[3 ]; - p.iph.saddr =0 ; - p.iph.version =4 ; - p.iph.ihl =5 ; - p.iph.protocol =IPPROTO_IPV6 ; - p.iph.ttl =64 ; - ifr.ifr_ifru.ifru_data = (__force void __user *)&p; - -if (ops->ndo_do_ioctl) { -mm_segment_t oldfs =get_fs (); - -set_fs (KERNEL_DS ); - err = ops->ndo_do_ioctl (dev, &ifr,SIOCADDTUNNEL ); -set_fs (oldfs); - }else - err = -EOPNOTSUPP ;
...
net/ipv6/sit.c static struct ip_tunnel *ipip6_tunnel_locate(struct net *net, · line 307
Quoted 10 lines
net/ipv6/sit.c +10
-0+ p.iph.daddr = ireq.ifr6_addr.s6_addr32[3 ]; + p.iph.version =4 ; + p.iph.ihl =5 ; + p.iph.protocol =IPPROTO_IPV6 ; + p.iph.ttl =64 ; + p.iph.frag_off |=htons (IP_DF ); + + err = -ENOBUFS ; +if (!ipip6_tunnel_locate (dev_net (tunnel_dev), &p, true)) + goto out_unlock;
You're not undoing one, but two levels of abstraction here.
Is this "ipip6_tunnel_locate()" call part of the SIT ioctl implementation?
Where did it come from? Why are ->ndo_do_ioctl() implementations no longer
allowed from here?
Honestly, this feels like a bit much.
Re: [PATCH 4/4] ipv6: symbol_get to access a sit symbol
Quoted 3 lines · David Miller
You're not undoing one, but two levels of abstraction here.
Is this "
ipip6_tunnel_locate()" call part of the SIT ioctl implementation?
Yes. Take a look at the convoluted case handling the SIOCADDTUNNEL and SIOCCHGTUNNEL commands in ipip6_tunnel_ioctl.
Quoted 2 lines
Where did it come from? Why are ->ndo_do_ioctl() implementations no longer
allowed from here?
The problem is that we feed kernel pointers to it, which requires set_fs address space overrides that I plan to kill off entirely.
Quoted 1 line
Honestly, this feels like a bit much.
My initial plan was to add a ->tunnel_ctl method to the net_device_ops, and lift the copy_{to,from}_user for SIOCADDTUNNEL, SIOCCHGTUNNEL, SIOCDELTUNNEL and maybe SIOCGETTUNNEL to net/socket.c. But that turned out to have two problems:
- first these ioctls names use SIOCDEVPRIVATE range, that can also be implemented by other drivers
- the ip_tunnel_parm struture is only used by the ipv4 tunneling drivers (including sit), the "real" ipv6 tunnels use a ip6_tnl_parm or ip6_tnl_parm structure instead
But if you don't like the symbol_get approach, I could do the tunnel_ctl operation, just for the іpv4-ish tunnels, and only for the kernel callers.
---end quoted text---
David Miller
maintainerRe: [PATCH 4/4] ipv6: symbol_get to access a sit symbol
From: Christoph Hellwig <[email protected]>
Date: Fri, 15 May 2020 08:33:24 +0200
Quoted 10 lines
My initial plan was to add a ->tunnel_ctl method to the
net_device_ops, and lift the copy_{to,from}_user for SIOCADDTUNNEL, SIOCCHGTUNNEL, SIOCDELTUNNEL and maybe SIOCGETTUNNEL tonet/socket.c. But that turned out to have two problems:- first these ioctls names use SIOCDEVPRIVATE range, that can also be implemented by other drivers
- theip_tunnel_parmstruture is only used by the ipv4 tunneling drivers (including sit), the "real" ipv6 tunnels use aip6_tnl_parmorip6_tnl_parmstructure instead
Yes, this is the core of the problem, the user provided data's type is unknown until we are very deep in the call chains.
I wonder if there is some clever way to propagate this size value "up"?
Re: [PATCH 4/4] ipv6: symbol_get to access a sit symbol
Quoted 14 lines · David Miller
From: Christoph Hellwig <[email protected]>
Date: Fri, 15 May 2020 08:33:24 +0200Quoted 10 lines
My initial plan was to add a ->tunnel_ctl method to the
net_device_ops, and lift the copy_{to,from}_user for SIOCADDTUNNEL, SIOCCHGTUNNEL, SIOCDELTUNNEL and maybe SIOCGETTUNNEL tonet/socket.c. But that turned out to have two problems:- first these ioctls names use SIOCDEVPRIVATE range, that can also be implemented by other drivers
- theip_tunnel_parmstruture is only used by the ipv4 tunneling drivers (including sit), the "real" ipv6 tunnels use aip6_tnl_parmorip6_tnl_parmstructure instead
Quoted 5 lines
Yes, this is the core of the problem, the user provided data's type is unknown until we are very deep in the call chains.
I wonder if there is some clever way to propagate this size value "up"?
As far as I can tell the only information vectors is the net_device structure or its op vector. But even then we have the problem that other devices use the SIOCDEVPRIVATE range for something else.
I'll look into implenenting the tunnel_ctl method just for kernel callers (plus maybe a generic helper for the ioctl), and we'll see if you like that better.
David Miller
maintainerRe: [PATCH 4/4] ipv6: symbol_get to access a sit symbol
From: Christoph Hellwig <[email protected]>
Date: Mon, 18 May 2020 08:30:43 +0200
Quoted 3 lines
I'll look into implenenting the
tunnel_ctlmethod just for kernel callers (plus maybe a generic helper for the ioctl), and we'll see if you like that better.
Ok, thank you.