Skip to content

Commit 117aef1

Browse files
alobakindavem330
authored andcommitted
ip_tunnel: use a separate struct to store tunnel params in the kernel
Unlike IPv6 tunnels which use purely-kernel __ip6_tnl_parm structure to store params inside the kernel, IPv4 tunnel code uses the same ip_tunnel_parm which is being used to talk with the userspace. This makes it difficult to alter or add any fields or use a different format for whatever data. Define struct ip_tunnel_parm_kern, a 1:1 copy of ip_tunnel_parm for now, and use it throughout the code. Define the pieces, where the copy user <-> kernel happens, as standalone functions, and copy the data there field-by-field, so that the kernel-side structure could be easily modified later on and the users wouldn't have to care about this. Reviewed-by: Simon Horman <horms@kernel.org> Signed-off-by: Alexander Lobakin <aleksander.lobakin@intel.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 7adaf37 commit 117aef1

File tree

13 files changed

+137
-75
lines changed

13 files changed

+137
-75
lines changed

drivers/net/ethernet/mellanox/mlxsw/spectrum_ipip.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "spectrum_ipip.h"
99
#include "reg.h"
1010

11-
struct ip_tunnel_parm
11+
struct ip_tunnel_parm_kern
1212
mlxsw_sp_ipip_netdev_parms4(const struct net_device *ol_dev)
1313
{
1414
struct ip_tunnel *tun = netdev_priv(ol_dev);
@@ -24,7 +24,8 @@ mlxsw_sp_ipip_netdev_parms6(const struct net_device *ol_dev)
2424
return tun->parms;
2525
}
2626

27-
static bool mlxsw_sp_ipip_parms4_has_ikey(const struct ip_tunnel_parm *parms)
27+
static bool
28+
mlxsw_sp_ipip_parms4_has_ikey(const struct ip_tunnel_parm_kern *parms)
2829
{
2930
return !!(parms->i_flags & TUNNEL_KEY);
3031
}
@@ -34,7 +35,8 @@ static bool mlxsw_sp_ipip_parms6_has_ikey(const struct __ip6_tnl_parm *parms)
3435
return !!(parms->i_flags & TUNNEL_KEY);
3536
}
3637

37-
static bool mlxsw_sp_ipip_parms4_has_okey(const struct ip_tunnel_parm *parms)
38+
static bool
39+
mlxsw_sp_ipip_parms4_has_okey(const struct ip_tunnel_parm_kern *parms)
3840
{
3941
return !!(parms->o_flags & TUNNEL_KEY);
4042
}
@@ -44,7 +46,7 @@ static bool mlxsw_sp_ipip_parms6_has_okey(const struct __ip6_tnl_parm *parms)
4446
return !!(parms->o_flags & TUNNEL_KEY);
4547
}
4648

47-
static u32 mlxsw_sp_ipip_parms4_ikey(const struct ip_tunnel_parm *parms)
49+
static u32 mlxsw_sp_ipip_parms4_ikey(const struct ip_tunnel_parm_kern *parms)
4850
{
4951
return mlxsw_sp_ipip_parms4_has_ikey(parms) ?
5052
be32_to_cpu(parms->i_key) : 0;
@@ -56,7 +58,7 @@ static u32 mlxsw_sp_ipip_parms6_ikey(const struct __ip6_tnl_parm *parms)
5658
be32_to_cpu(parms->i_key) : 0;
5759
}
5860

59-
static u32 mlxsw_sp_ipip_parms4_okey(const struct ip_tunnel_parm *parms)
61+
static u32 mlxsw_sp_ipip_parms4_okey(const struct ip_tunnel_parm_kern *parms)
6062
{
6163
return mlxsw_sp_ipip_parms4_has_okey(parms) ?
6264
be32_to_cpu(parms->o_key) : 0;
@@ -69,7 +71,7 @@ static u32 mlxsw_sp_ipip_parms6_okey(const struct __ip6_tnl_parm *parms)
6971
}
7072

7173
static union mlxsw_sp_l3addr
72-
mlxsw_sp_ipip_parms4_saddr(const struct ip_tunnel_parm *parms)
74+
mlxsw_sp_ipip_parms4_saddr(const struct ip_tunnel_parm_kern *parms)
7375
{
7476
return (union mlxsw_sp_l3addr) { .addr4 = parms->iph.saddr };
7577
}
@@ -81,7 +83,7 @@ mlxsw_sp_ipip_parms6_saddr(const struct __ip6_tnl_parm *parms)
8183
}
8284

8385
static union mlxsw_sp_l3addr
84-
mlxsw_sp_ipip_parms4_daddr(const struct ip_tunnel_parm *parms)
86+
mlxsw_sp_ipip_parms4_daddr(const struct ip_tunnel_parm_kern *parms)
8587
{
8688
return (union mlxsw_sp_l3addr) { .addr4 = parms->iph.daddr };
8789
}
@@ -96,7 +98,7 @@ union mlxsw_sp_l3addr
9698
mlxsw_sp_ipip_netdev_saddr(enum mlxsw_sp_l3proto proto,
9799
const struct net_device *ol_dev)
98100
{
99-
struct ip_tunnel_parm parms4;
101+
struct ip_tunnel_parm_kern parms4;
100102
struct __ip6_tnl_parm parms6;
101103

102104
switch (proto) {
@@ -115,7 +117,9 @@ mlxsw_sp_ipip_netdev_saddr(enum mlxsw_sp_l3proto proto,
115117
static __be32 mlxsw_sp_ipip_netdev_daddr4(const struct net_device *ol_dev)
116118
{
117119

118-
struct ip_tunnel_parm parms4 = mlxsw_sp_ipip_netdev_parms4(ol_dev);
120+
struct ip_tunnel_parm_kern parms4;
121+
122+
parms4 = mlxsw_sp_ipip_netdev_parms4(ol_dev);
119123

120124
return mlxsw_sp_ipip_parms4_daddr(&parms4).addr4;
121125
}
@@ -124,7 +128,7 @@ static union mlxsw_sp_l3addr
124128
mlxsw_sp_ipip_netdev_daddr(enum mlxsw_sp_l3proto proto,
125129
const struct net_device *ol_dev)
126130
{
127-
struct ip_tunnel_parm parms4;
131+
struct ip_tunnel_parm_kern parms4;
128132
struct __ip6_tnl_parm parms6;
129133

130134
switch (proto) {
@@ -150,7 +154,7 @@ bool mlxsw_sp_l3addr_is_zero(union mlxsw_sp_l3addr addr)
150154
static struct mlxsw_sp_ipip_parms
151155
mlxsw_sp_ipip_netdev_parms_init_gre4(const struct net_device *ol_dev)
152156
{
153-
struct ip_tunnel_parm parms = mlxsw_sp_ipip_netdev_parms4(ol_dev);
157+
struct ip_tunnel_parm_kern parms = mlxsw_sp_ipip_netdev_parms4(ol_dev);
154158

155159
return (struct mlxsw_sp_ipip_parms) {
156160
.proto = MLXSW_SP_L3_PROTO_IPV4,
@@ -187,8 +191,8 @@ mlxsw_sp_ipip_decap_config_gre4(struct mlxsw_sp *mlxsw_sp,
187191
{
188192
u16 rif_index = mlxsw_sp_ipip_lb_rif_index(ipip_entry->ol_lb);
189193
u16 ul_rif_id = mlxsw_sp_ipip_lb_ul_rif_id(ipip_entry->ol_lb);
194+
struct ip_tunnel_parm_kern parms;
190195
char rtdp_pl[MLXSW_REG_RTDP_LEN];
191-
struct ip_tunnel_parm parms;
192196
unsigned int type_check;
193197
bool has_ikey;
194198
u32 daddr4;
@@ -252,7 +256,7 @@ static struct mlxsw_sp_rif_ipip_lb_config
252256
mlxsw_sp_ipip_ol_loopback_config_gre4(struct mlxsw_sp *mlxsw_sp,
253257
const struct net_device *ol_dev)
254258
{
255-
struct ip_tunnel_parm parms = mlxsw_sp_ipip_netdev_parms4(ol_dev);
259+
struct ip_tunnel_parm_kern parms = mlxsw_sp_ipip_netdev_parms4(ol_dev);
256260
enum mlxsw_reg_ritr_loopback_ipip_type lb_ipipt;
257261

258262
lb_ipipt = mlxsw_sp_ipip_parms4_has_okey(&parms) ?

drivers/net/ethernet/mellanox/mlxsw/spectrum_ipip.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <linux/if_tunnel.h>
1010
#include <net/ip6_tunnel.h>
1111

12-
struct ip_tunnel_parm
12+
struct ip_tunnel_parm_kern
1313
mlxsw_sp_ipip_netdev_parms4(const struct net_device *ol_dev);
1414
struct __ip6_tnl_parm
1515
mlxsw_sp_ipip_netdev_parms6(const struct net_device *ol_dev);

drivers/net/ethernet/mellanox/mlxsw/spectrum_span.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,8 @@ mlxsw_sp_span_gretap4_route(const struct net_device *to_dev,
413413
__be32 *saddrp, __be32 *daddrp)
414414
{
415415
struct ip_tunnel *tun = netdev_priv(to_dev);
416+
struct ip_tunnel_parm_kern parms;
416417
struct net_device *dev = NULL;
417-
struct ip_tunnel_parm parms;
418418
struct rtable *rt = NULL;
419419
struct flowi4 fl4;
420420

@@ -451,7 +451,7 @@ mlxsw_sp_span_entry_gretap4_parms(struct mlxsw_sp *mlxsw_sp,
451451
const struct net_device *to_dev,
452452
struct mlxsw_sp_span_parms *sparmsp)
453453
{
454-
struct ip_tunnel_parm tparm = mlxsw_sp_ipip_netdev_parms4(to_dev);
454+
struct ip_tunnel_parm_kern tparm = mlxsw_sp_ipip_netdev_parms4(to_dev);
455455
union mlxsw_sp_l3addr saddr = { .addr4 = tparm.iph.saddr };
456456
union mlxsw_sp_l3addr daddr = { .addr4 = tparm.iph.daddr };
457457
bool inherit_tos = tparm.iph.tos & 0x1;

include/linux/netdevice.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ struct ethtool_ops;
5959
struct kernel_hwtstamp_config;
6060
struct phy_device;
6161
struct dsa_port;
62-
struct ip_tunnel_parm;
62+
struct ip_tunnel_parm_kern;
6363
struct macsec_context;
6464
struct macsec_ops;
6565
struct netdev_name_node;
@@ -1327,7 +1327,7 @@ struct netdev_net_notifier {
13271327
* queue id bound to an AF_XDP socket. The flags field specifies if
13281328
* only RX, only Tx, or both should be woken up using the flags
13291329
* XDP_WAKEUP_RX and XDP_WAKEUP_TX.
1330-
* int (*ndo_tunnel_ctl)(struct net_device *dev, struct ip_tunnel_parm *p,
1330+
* int (*ndo_tunnel_ctl)(struct net_device *dev, struct ip_tunnel_parm_kern *p,
13311331
* int cmd);
13321332
* Add, change, delete or get information on an IPv4 tunnel.
13331333
* struct net_device *(*ndo_get_peer_dev)(struct net_device *dev);
@@ -1583,7 +1583,8 @@ struct net_device_ops {
15831583
int (*ndo_xsk_wakeup)(struct net_device *dev,
15841584
u32 queue_id, u32 flags);
15851585
int (*ndo_tunnel_ctl)(struct net_device *dev,
1586-
struct ip_tunnel_parm *p, int cmd);
1586+
struct ip_tunnel_parm_kern *p,
1587+
int cmd);
15871588
struct net_device * (*ndo_get_peer_dev)(struct net_device *dev);
15881589
int (*ndo_fill_forward_path)(struct net_device_path_ctx *ctx,
15891590
struct net_device_path *path);

include/net/ip_tunnels.h

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,17 @@ struct ip_tunnel_prl_entry {
110110

111111
struct metadata_dst;
112112

113+
/* Kernel-side copy of ip_tunnel_parm */
114+
struct ip_tunnel_parm_kern {
115+
char name[IFNAMSIZ];
116+
int link;
117+
__be16 i_flags;
118+
__be16 o_flags;
119+
__be32 i_key;
120+
__be32 o_key;
121+
struct iphdr iph;
122+
};
123+
113124
struct ip_tunnel {
114125
struct ip_tunnel __rcu *next;
115126
struct hlist_node hash_node;
@@ -136,7 +147,7 @@ struct ip_tunnel {
136147

137148
struct dst_cache dst_cache;
138149

139-
struct ip_tunnel_parm parms;
150+
struct ip_tunnel_parm_kern parms;
140151

141152
int mlink;
142153
int encap_hlen; /* Encap header length (FOU,GUE) */
@@ -291,7 +302,11 @@ void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
291302
const struct iphdr *tnl_params, const u8 protocol);
292303
void ip_md_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
293304
const u8 proto, int tunnel_hlen);
294-
int ip_tunnel_ctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd);
305+
int ip_tunnel_ctl(struct net_device *dev, struct ip_tunnel_parm_kern *p,
306+
int cmd);
307+
bool ip_tunnel_parm_from_user(struct ip_tunnel_parm_kern *kp,
308+
const void __user *data);
309+
bool ip_tunnel_parm_to_user(void __user *data, struct ip_tunnel_parm_kern *kp);
295310
int ip_tunnel_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
296311
void __user *data, int cmd);
297312
int __ip_tunnel_change_mtu(struct net_device *dev, int new_mtu, bool strict);
@@ -307,16 +322,16 @@ int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb,
307322
const struct tnl_ptk_info *tpi, struct metadata_dst *tun_dst,
308323
bool log_ecn_error);
309324
int ip_tunnel_changelink(struct net_device *dev, struct nlattr *tb[],
310-
struct ip_tunnel_parm *p, __u32 fwmark);
325+
struct ip_tunnel_parm_kern *p, __u32 fwmark);
311326
int ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[],
312-
struct ip_tunnel_parm *p, __u32 fwmark);
327+
struct ip_tunnel_parm_kern *p, __u32 fwmark);
313328
void ip_tunnel_setup(struct net_device *dev, unsigned int net_id);
314329

315330
bool ip_tunnel_netlink_encap_parms(struct nlattr *data[],
316331
struct ip_tunnel_encap *encap);
317332

318333
void ip_tunnel_netlink_parms(struct nlattr *data[],
319-
struct ip_tunnel_parm *parms);
334+
struct ip_tunnel_parm_kern *parms);
320335

321336
extern const struct header_ops ip_tunnel_header_ops;
322337
__be16 ip_tunnel_parse_protocol(const struct sk_buff *skb);

net/ipv4/ip_gre.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,8 @@ static void ipgre_link_update(struct net_device *dev, bool set_mtu)
785785
}
786786
}
787787

788-
static int ipgre_tunnel_ctl(struct net_device *dev, struct ip_tunnel_parm *p,
788+
static int ipgre_tunnel_ctl(struct net_device *dev,
789+
struct ip_tunnel_parm_kern *p,
789790
int cmd)
790791
{
791792
int err;
@@ -1131,7 +1132,7 @@ static int erspan_validate(struct nlattr *tb[], struct nlattr *data[],
11311132
static int ipgre_netlink_parms(struct net_device *dev,
11321133
struct nlattr *data[],
11331134
struct nlattr *tb[],
1134-
struct ip_tunnel_parm *parms,
1135+
struct ip_tunnel_parm_kern *parms,
11351136
__u32 *fwmark)
11361137
{
11371138
struct ip_tunnel *t = netdev_priv(dev);
@@ -1198,7 +1199,7 @@ static int ipgre_netlink_parms(struct net_device *dev,
11981199
static int erspan_netlink_parms(struct net_device *dev,
11991200
struct nlattr *data[],
12001201
struct nlattr *tb[],
1201-
struct ip_tunnel_parm *parms,
1202+
struct ip_tunnel_parm_kern *parms,
12021203
__u32 *fwmark)
12031204
{
12041205
struct ip_tunnel *t = netdev_priv(dev);
@@ -1357,7 +1358,7 @@ static int ipgre_newlink(struct net *src_net, struct net_device *dev,
13571358
struct nlattr *tb[], struct nlattr *data[],
13581359
struct netlink_ext_ack *extack)
13591360
{
1360-
struct ip_tunnel_parm p;
1361+
struct ip_tunnel_parm_kern p;
13611362
__u32 fwmark = 0;
13621363
int err;
13631364

@@ -1375,7 +1376,7 @@ static int erspan_newlink(struct net *src_net, struct net_device *dev,
13751376
struct nlattr *tb[], struct nlattr *data[],
13761377
struct netlink_ext_ack *extack)
13771378
{
1378-
struct ip_tunnel_parm p;
1379+
struct ip_tunnel_parm_kern p;
13791380
__u32 fwmark = 0;
13801381
int err;
13811382

@@ -1394,8 +1395,8 @@ static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[],
13941395
struct netlink_ext_ack *extack)
13951396
{
13961397
struct ip_tunnel *t = netdev_priv(dev);
1398+
struct ip_tunnel_parm_kern p;
13971399
__u32 fwmark = t->fwmark;
1398-
struct ip_tunnel_parm p;
13991400
int err;
14001401

14011402
err = ipgre_newlink_encap_setup(dev, data);
@@ -1423,8 +1424,8 @@ static int erspan_changelink(struct net_device *dev, struct nlattr *tb[],
14231424
struct netlink_ext_ack *extack)
14241425
{
14251426
struct ip_tunnel *t = netdev_priv(dev);
1427+
struct ip_tunnel_parm_kern p;
14261428
__u32 fwmark = t->fwmark;
1427-
struct ip_tunnel_parm p;
14281429
int err;
14291430

14301431
err = ipgre_newlink_encap_setup(dev, data);
@@ -1496,7 +1497,7 @@ static size_t ipgre_get_size(const struct net_device *dev)
14961497
static int ipgre_fill_info(struct sk_buff *skb, const struct net_device *dev)
14971498
{
14981499
struct ip_tunnel *t = netdev_priv(dev);
1499-
struct ip_tunnel_parm *p = &t->parms;
1500+
struct ip_tunnel_parm_kern *p = &t->parms;
15001501
__be16 o_flags = p->o_flags;
15011502

15021503
if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||

0 commit comments

Comments
 (0)