diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c
index 6da137f1a764..a33b60d1de2d 100644
--- a/net/core/rtnetlink.c
+++ b/net/core/rtnetlink.c
@@ -2675,7 +2675,7 @@ static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
 	return err;
 }
 
-static int do_set_master(struct net_device *dev, int ifindex,
+static int do_set_master(struct net_device *dev, struct net_device *master,
 			 struct netlink_ext_ack *extack)
 {
 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
@@ -2683,7 +2683,7 @@ static int do_set_master(struct net_device *dev, int ifindex,
 	int err;
 
 	if (upper_dev) {
-		if (upper_dev->ifindex == ifindex)
+		if (upper_dev == master)
 			return 0;
 		ops = upper_dev->netdev_ops;
 		if (ops->ndo_del_slave) {
@@ -2695,10 +2695,8 @@ static int do_set_master(struct net_device *dev, int ifindex,
 		}
 	}
 
-	if (ifindex) {
-		upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
-		if (!upper_dev)
-			return -EINVAL;
+	if (master) {
+		upper_dev = master;
 		ops = upper_dev->netdev_ops;
 		if (ops->ndo_add_slave) {
 			err = ops->ndo_add_slave(upper_dev, dev, extack);
@@ -2775,7 +2773,8 @@ static int do_set_proto_down(struct net_device *dev,
 /* notify flag means notify + modified. */
 #define DO_SETLINK_NOTIFY	0x03
 static int do_setlink(struct net *net, const struct sk_buff *skb,
-		      struct net_device *dev, struct ifinfomsg *ifm,
+		      struct net_device *dev, struct net_device *master,
+		      struct ifinfomsg *ifm,
 		      struct netlink_ext_ack *extack,
 		      struct nlattr **tb, int status)
 {
@@ -2897,8 +2896,8 @@ static int do_setlink(struct net *net, const struct sk_buff *skb,
 			goto errout;
 	}
 
-	if (tb[IFLA_MASTER]) {
-		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
+	if (master) {
+		err = do_set_master(dev, master, extack);
 		if (err)
 			goto errout;
 		status |= DO_SETLINK_MODIFIED;
@@ -3156,12 +3155,24 @@ static struct net_device *rtnl_dev_get(struct net *net,
 	return __dev_get_by_name(net, ifname);
 }
 
+static struct net_device *rtnl_master_get(struct net *net, struct nlattr *tb[])
+{
+	struct net_device *master;
+
+	if (!tb[IFLA_MASTER])
+		return NULL;
+	master = __dev_get_by_index(net, nla_get_u32(tb[IFLA_MASTER]));
+	if (!master)
+		return ERR_PTR(-EINVAL);
+	return master;
+}
+
 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
 			struct netlink_ext_ack *extack)
 {
 	struct net *net = sock_net(skb->sk);
 	struct ifinfomsg *ifm;
-	struct net_device *dev;
+	struct net_device *dev, *master = NULL;
 	struct net *target_net = NULL;
 	int err;
 	struct nlattr *tb[IFLA_MAX+1];
@@ -3196,11 +3207,17 @@ static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
 		goto errout;
 	}
 
+	master = rtnl_master_get(target_net ? : net, tb);
+	if (IS_ERR(master)) {
+		err = -EINVAL;
+		goto errout;
+	}
+
 	err = validate_linkmsg(dev, tb, extack);
 	if (err < 0)
 		goto errout;
 
-	err = do_setlink(target_net, skb, dev, ifm, extack, tb, 0);
+	err = do_setlink(target_net, skb, dev, master, ifm, extack, tb, 0);
 errout:
 	if (target_net)
 		put_net(target_net);
@@ -3440,7 +3457,7 @@ static int rtnl_group_changelink(const struct sk_buff *skb,
 		struct netlink_ext_ack *extack,
 		struct nlattr **tb)
 {
-	struct net_device *dev, *aux;
+	struct net_device *dev, *aux, *master = NULL;
 	struct net *target_net;
 	int err;
 
@@ -3451,12 +3468,18 @@ static int rtnl_group_changelink(const struct sk_buff *skb,
 		goto out;
 	}
 
+	master = rtnl_master_get(target_net ? : net, tb);
+	if (IS_ERR(master)) {
+		err = -EINVAL;
+		goto out;
+	}
+
 	for_each_netdev_safe(net, dev, aux) {
 		if (dev->group == group) {
 			err = validate_linkmsg(dev, tb, extack);
 			if (err < 0)
 				break;
-			err = do_setlink(target_net, skb, dev, ifm, extack, tb, 0);
+			err = do_setlink(target_net, skb, dev, master, ifm, extack, tb, 0);
 			if (err < 0)
 				break;
 		}
@@ -3478,7 +3501,7 @@ static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm,
 	struct net *net = sock_net(skb->sk);
 	u32 portid = NETLINK_CB(skb).portid;
 	struct net *link_net;
-	struct net_device *dev;
+	struct net_device *dev, *master = NULL;
 	char ifname[IFNAMSIZ];
 	int err;
 
@@ -3519,6 +3542,12 @@ static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm,
 
 	dev->ifindex = ifm->ifi_index;
 
+	master = rtnl_master_get(link_net ? : dest_net, tb);
+	if (IS_ERR(master)) {
+		err = -EINVAL;
+		goto out;
+	}
+
 	if (ops->newlink)
 		err = ops->newlink(link_net ? : net, dev, tb, data, extack);
 	else
@@ -3536,8 +3565,8 @@ static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm,
 		if (err < 0)
 			goto out_unregister;
 	}
-	if (tb[IFLA_MASTER]) {
-		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
+	if (master) {
+		err = do_set_master(dev, master, extack);
 		if (err)
 			goto out_unregister;
 	}
@@ -3567,6 +3596,7 @@ static int __rtnl_newlink_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
 				  struct rtnl_newlink_tbs *tbs,
 				  struct netlink_ext_ack *extack,
 				  struct net *target_net, struct net_device *dev,
+				  struct net_device *new_master,
 				  const struct rtnl_link_ops *ops,
 				  struct nlattr **linkinfo, struct nlattr **data)
 {
@@ -3632,7 +3662,7 @@ static int __rtnl_newlink_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
 		status |= DO_SETLINK_NOTIFY;
 	}
 
-	err = do_setlink(target_net, skb, dev, ifm, extack, tb, status);
+	err = do_setlink(target_net, skb, dev, new_master, ifm, extack, tb, status);
 out:
 	return err;
 }
@@ -3647,7 +3677,7 @@ static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
 	struct net *net = sock_net(skb->sk);
 	const struct rtnl_link_ops *ops;
 	char kind[MODULE_NAME_LEN];
-	struct net_device *dev;
+	struct net_device *dev, *new_master = NULL;
 	struct ifinfomsg *ifm;
 	struct nlattr **data;
 	bool link_specified;
@@ -3709,8 +3739,12 @@ static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
 	}
 
 	if (dev) {
+		new_master = rtnl_master_get(target_net ? : net, tb);
+		if (IS_ERR(new_master))
+			return -EINVAL;
+
 		err = __rtnl_newlink_setlink(skb, nlh, tbs, extack,
-					     target_net, dev,
+					     target_net, dev, new_master,
 					     ops, linkinfo, data);
 		return err;
 	}
