rtnetlink.c (22b3b41c709b63b1b9dffe6c4d56a5161d972e51) rtnetlink.c (420d031822737ef570df6834c0eae26f33988f20)
1/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * Routing netlink socket interface: protocol independent part.
7 *
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>

--- 45 unchanged lines hidden (view full) ---

54#include <net/udp.h>
55#include <net/tcp.h>
56#include <net/sock.h>
57#include <net/pkt_sched.h>
58#include <net/fib_rules.h>
59#include <net/rtnetlink.h>
60#include <net/net_namespace.h>
61
1/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * Routing netlink socket interface: protocol independent part.
7 *
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>

--- 45 unchanged lines hidden (view full) ---

54#include <net/udp.h>
55#include <net/tcp.h>
56#include <net/sock.h>
57#include <net/pkt_sched.h>
58#include <net/fib_rules.h>
59#include <net/rtnetlink.h>
60#include <net/net_namespace.h>
61
62#define RTNL_MAX_TYPE 49
62#define RTNL_MAX_TYPE 50
63#define RTNL_SLAVE_MAX_TYPE 36
64
65struct rtnl_link {
66 rtnl_doit_func doit;
67 rtnl_dumpit_func dumpit;
68 struct module *owner;
69 unsigned int flags;
70 struct rcu_head rcu;

--- 2809 unchanged lines hidden (view full) ---

2880 } else {
2881 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
2882 __dev_notify_flags(dev, old_flags, ~0U);
2883 }
2884 return 0;
2885}
2886EXPORT_SYMBOL(rtnl_configure_link);
2887
63#define RTNL_SLAVE_MAX_TYPE 36
64
65struct rtnl_link {
66 rtnl_doit_func doit;
67 rtnl_dumpit_func dumpit;
68 struct module *owner;
69 unsigned int flags;
70 struct rcu_head rcu;

--- 2809 unchanged lines hidden (view full) ---

2880 } else {
2881 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
2882 __dev_notify_flags(dev, old_flags, ~0U);
2883 }
2884 return 0;
2885}
2886EXPORT_SYMBOL(rtnl_configure_link);
2887
2888struct net_device *rtnl_create_link(struct net *net,
2889 const char *ifname, unsigned char name_assign_type,
2890 const struct rtnl_link_ops *ops, struct nlattr *tb[])
2888struct net_device *rtnl_create_link(struct net *net, const char *ifname,
2889 unsigned char name_assign_type,
2890 const struct rtnl_link_ops *ops,
2891 struct nlattr *tb[],
2892 struct netlink_ext_ack *extack)
2891{
2892 struct net_device *dev;
2893 unsigned int num_tx_queues = 1;
2894 unsigned int num_rx_queues = 1;
2895
2896 if (tb[IFLA_NUM_TX_QUEUES])
2897 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2898 else if (ops->get_num_tx_queues)
2899 num_tx_queues = ops->get_num_tx_queues();
2900
2901 if (tb[IFLA_NUM_RX_QUEUES])
2902 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2903 else if (ops->get_num_rx_queues)
2904 num_rx_queues = ops->get_num_rx_queues();
2905
2893{
2894 struct net_device *dev;
2895 unsigned int num_tx_queues = 1;
2896 unsigned int num_rx_queues = 1;
2897
2898 if (tb[IFLA_NUM_TX_QUEUES])
2899 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2900 else if (ops->get_num_tx_queues)
2901 num_tx_queues = ops->get_num_tx_queues();
2902
2903 if (tb[IFLA_NUM_RX_QUEUES])
2904 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2905 else if (ops->get_num_rx_queues)
2906 num_rx_queues = ops->get_num_rx_queues();
2907
2906 if (num_tx_queues < 1 || num_tx_queues > 4096)
2908 if (num_tx_queues < 1 || num_tx_queues > 4096) {
2909 NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
2907 return ERR_PTR(-EINVAL);
2910 return ERR_PTR(-EINVAL);
2911 }
2908
2912
2909 if (num_rx_queues < 1 || num_rx_queues > 4096)
2913 if (num_rx_queues < 1 || num_rx_queues > 4096) {
2914 NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
2910 return ERR_PTR(-EINVAL);
2915 return ERR_PTR(-EINVAL);
2916 }
2911
2912 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
2913 ops->setup, num_tx_queues, num_rx_queues);
2914 if (!dev)
2915 return ERR_PTR(-ENOMEM);
2916
2917 dev_net_set(dev, net);
2918 dev->rtnl_link_ops = ops;

--- 44 unchanged lines hidden (view full) ---

2963 }
2964
2965 return 0;
2966}
2967
2968static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2969 struct netlink_ext_ack *extack)
2970{
2917
2918 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
2919 ops->setup, num_tx_queues, num_rx_queues);
2920 if (!dev)
2921 return ERR_PTR(-ENOMEM);
2922
2923 dev_net_set(dev, net);
2924 dev->rtnl_link_ops = ops;

--- 44 unchanged lines hidden (view full) ---

2969 }
2970
2971 return 0;
2972}
2973
2974static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2975 struct netlink_ext_ack *extack)
2976{
2977 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
2978 unsigned char name_assign_type = NET_NAME_USER;
2979 struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
2980 const struct rtnl_link_ops *m_ops = NULL;
2981 struct nlattr *attr[RTNL_MAX_TYPE + 1];
2982 struct net_device *master_dev = NULL;
2971 struct net *net = sock_net(skb->sk);
2972 const struct rtnl_link_ops *ops;
2983 struct net *net = sock_net(skb->sk);
2984 const struct rtnl_link_ops *ops;
2973 const struct rtnl_link_ops *m_ops = NULL;
2985 struct nlattr *tb[IFLA_MAX + 1];
2986 struct net *dest_net, *link_net;
2987 struct nlattr **slave_data;
2988 char kind[MODULE_NAME_LEN];
2974 struct net_device *dev;
2989 struct net_device *dev;
2975 struct net_device *master_dev = NULL;
2976 struct ifinfomsg *ifm;
2990 struct ifinfomsg *ifm;
2977 char kind[MODULE_NAME_LEN];
2978 char ifname[IFNAMSIZ];
2991 char ifname[IFNAMSIZ];
2979 struct nlattr *tb[IFLA_MAX+1];
2980 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
2981 unsigned char name_assign_type = NET_NAME_USER;
2992 struct nlattr **data;
2982 int err;
2983
2984#ifdef CONFIG_MODULES
2985replay:
2986#endif
2987 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
2988 if (err < 0)
2989 return err;

--- 39 unchanged lines hidden (view full) ---

3029 if (linkinfo[IFLA_INFO_KIND]) {
3030 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
3031 ops = rtnl_link_ops_get(kind);
3032 } else {
3033 kind[0] = '\0';
3034 ops = NULL;
3035 }
3036
2993 int err;
2994
2995#ifdef CONFIG_MODULES
2996replay:
2997#endif
2998 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
2999 if (err < 0)
3000 return err;

--- 39 unchanged lines hidden (view full) ---

3040 if (linkinfo[IFLA_INFO_KIND]) {
3041 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
3042 ops = rtnl_link_ops_get(kind);
3043 } else {
3044 kind[0] = '\0';
3045 ops = NULL;
3046 }
3047
3037 if (1) {
3038 struct nlattr *attr[RTNL_MAX_TYPE + 1];
3039 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3040 struct nlattr **data = NULL;
3041 struct nlattr **slave_data = NULL;
3042 struct net *dest_net, *link_net = NULL;
3048 data = NULL;
3049 if (ops) {
3050 if (ops->maxtype > RTNL_MAX_TYPE)
3051 return -EINVAL;
3043
3052
3044 if (ops) {
3045 if (ops->maxtype > RTNL_MAX_TYPE)
3046 return -EINVAL;
3047
3048 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3049 err = nla_parse_nested(attr, ops->maxtype,
3050 linkinfo[IFLA_INFO_DATA],
3051 ops->policy, NULL);
3052 if (err < 0)
3053 return err;
3054 data = attr;
3055 }
3056 if (ops->validate) {
3057 err = ops->validate(tb, data, extack);
3058 if (err < 0)
3059 return err;
3060 }
3053 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3054 err = nla_parse_nested(attr, ops->maxtype,
3055 linkinfo[IFLA_INFO_DATA],
3056 ops->policy, extack);
3057 if (err < 0)
3058 return err;
3059 data = attr;
3061 }
3060 }
3061 if (ops->validate) {
3062 err = ops->validate(tb, data, extack);
3063 if (err < 0)
3064 return err;
3065 }
3066 }
3062
3067
3063 if (m_ops) {
3064 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3065 return -EINVAL;
3068 slave_data = NULL;
3069 if (m_ops) {
3070 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3071 return -EINVAL;
3066
3072
3067 if (m_ops->slave_maxtype &&
3068 linkinfo[IFLA_INFO_SLAVE_DATA]) {
3069 err = nla_parse_nested(slave_attr,
3070 m_ops->slave_maxtype,
3071 linkinfo[IFLA_INFO_SLAVE_DATA],
3072 m_ops->slave_policy,
3073 NULL);
3074 if (err < 0)
3075 return err;
3076 slave_data = slave_attr;
3077 }
3073 if (m_ops->slave_maxtype &&
3074 linkinfo[IFLA_INFO_SLAVE_DATA]) {
3075 err = nla_parse_nested(slave_attr, m_ops->slave_maxtype,
3076 linkinfo[IFLA_INFO_SLAVE_DATA],
3077 m_ops->slave_policy, extack);
3078 if (err < 0)
3079 return err;
3080 slave_data = slave_attr;
3078 }
3081 }
3082 }
3079
3083
3080 if (dev) {
3081 int status = 0;
3084 if (dev) {
3085 int status = 0;
3082
3086
3083 if (nlh->nlmsg_flags & NLM_F_EXCL)
3084 return -EEXIST;
3085 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3087 if (nlh->nlmsg_flags & NLM_F_EXCL)
3088 return -EEXIST;
3089 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3090 return -EOPNOTSUPP;
3091
3092 if (linkinfo[IFLA_INFO_DATA]) {
3093 if (!ops || ops != dev->rtnl_link_ops ||
3094 !ops->changelink)
3086 return -EOPNOTSUPP;
3087
3095 return -EOPNOTSUPP;
3096
3088 if (linkinfo[IFLA_INFO_DATA]) {
3089 if (!ops || ops != dev->rtnl_link_ops ||
3090 !ops->changelink)
3091 return -EOPNOTSUPP;
3097 err = ops->changelink(dev, tb, data, extack);
3098 if (err < 0)
3099 return err;
3100 status |= DO_SETLINK_NOTIFY;
3101 }
3092
3102
3093 err = ops->changelink(dev, tb, data, extack);
3094 if (err < 0)
3095 return err;
3096 status |= DO_SETLINK_NOTIFY;
3097 }
3103 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3104 if (!m_ops || !m_ops->slave_changelink)
3105 return -EOPNOTSUPP;
3098
3106
3099 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3100 if (!m_ops || !m_ops->slave_changelink)
3101 return -EOPNOTSUPP;
3102
3103 err = m_ops->slave_changelink(master_dev, dev,
3104 tb, slave_data,
3105 extack);
3106 if (err < 0)
3107 return err;
3108 status |= DO_SETLINK_NOTIFY;
3109 }
3110
3111 return do_setlink(skb, dev, ifm, extack, tb, ifname,
3112 status);
3107 err = m_ops->slave_changelink(master_dev, dev, tb,
3108 slave_data, extack);
3109 if (err < 0)
3110 return err;
3111 status |= DO_SETLINK_NOTIFY;
3113 }
3114
3112 }
3113
3115 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3116 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
3117 return rtnl_group_changelink(skb, net,
3114 return do_setlink(skb, dev, ifm, extack, tb, ifname, status);
3115 }
3116
3117 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3118 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
3119 return rtnl_group_changelink(skb, net,
3118 nla_get_u32(tb[IFLA_GROUP]),
3119 ifm, extack, tb);
3120 nla_get_u32(tb[IFLA_GROUP]),
3121 ifm, extack, tb);
3120 return -ENODEV;
3121 }
3122 return -ENODEV;
3123 }
3122
3124
3123 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3124 return -EOPNOTSUPP;
3125 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3126 return -EOPNOTSUPP;
3125
3127
3126 if (!ops) {
3128 if (!ops) {
3127#ifdef CONFIG_MODULES
3129#ifdef CONFIG_MODULES
3128 if (kind[0]) {
3129 __rtnl_unlock();
3130 request_module("rtnl-link-%s", kind);
3131 rtnl_lock();
3132 ops = rtnl_link_ops_get(kind);
3133 if (ops)
3134 goto replay;
3135 }
3136#endif
3137 return -EOPNOTSUPP;
3130 if (kind[0]) {
3131 __rtnl_unlock();
3132 request_module("rtnl-link-%s", kind);
3133 rtnl_lock();
3134 ops = rtnl_link_ops_get(kind);
3135 if (ops)
3136 goto replay;
3138 }
3137 }
3138#endif
3139 NL_SET_ERR_MSG(extack, "Unknown device type");
3140 return -EOPNOTSUPP;
3141 }
3139
3142
3140 if (!ops->setup)
3141 return -EOPNOTSUPP;
3143 if (!ops->setup)
3144 return -EOPNOTSUPP;
3142
3145
3143 if (!ifname[0]) {
3144 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3145 name_assign_type = NET_NAME_ENUM;
3146 }
3146 if (!ifname[0]) {
3147 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3148 name_assign_type = NET_NAME_ENUM;
3149 }
3147
3150
3148 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3149 if (IS_ERR(dest_net))
3150 return PTR_ERR(dest_net);
3151 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3152 if (IS_ERR(dest_net))
3153 return PTR_ERR(dest_net);
3151
3154
3152 if (tb[IFLA_LINK_NETNSID]) {
3153 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3155 if (tb[IFLA_LINK_NETNSID]) {
3156 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3154
3157
3155 link_net = get_net_ns_by_id(dest_net, id);
3156 if (!link_net) {
3157 err = -EINVAL;
3158 goto out;
3159 }
3160 err = -EPERM;
3161 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3162 goto out;
3163 }
3164
3165 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3166 name_assign_type, ops, tb);
3167 if (IS_ERR(dev)) {
3168 err = PTR_ERR(dev);
3158 link_net = get_net_ns_by_id(dest_net, id);
3159 if (!link_net) {
3160 NL_SET_ERR_MSG(extack, "Unknown network namespace id");
3161 err = -EINVAL;
3169 goto out;
3170 }
3162 goto out;
3163 }
3164 err = -EPERM;
3165 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3166 goto out;
3167 } else {
3168 link_net = NULL;
3169 }
3171
3170
3172 dev->ifindex = ifm->ifi_index;
3171 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3172 name_assign_type, ops, tb, extack);
3173 if (IS_ERR(dev)) {
3174 err = PTR_ERR(dev);
3175 goto out;
3176 }
3173
3177
3174 if (ops->newlink) {
3175 err = ops->newlink(link_net ? : net, dev, tb, data,
3176 extack);
3177 /* Drivers should call free_netdev() in ->destructor
3178 * and unregister it on failure after registration
3179 * so that device could be finally freed in rtnl_unlock.
3180 */
3181 if (err < 0) {
3182 /* If device is not registered at all, free it now */
3183 if (dev->reg_state == NETREG_UNINITIALIZED)
3184 free_netdev(dev);
3185 goto out;
3186 }
3187 } else {
3188 err = register_netdevice(dev);
3189 if (err < 0) {
3178 dev->ifindex = ifm->ifi_index;
3179
3180 if (ops->newlink) {
3181 err = ops->newlink(link_net ? : net, dev, tb, data, extack);
3182 /* Drivers should call free_netdev() in ->destructor
3183 * and unregister it on failure after registration
3184 * so that device could be finally freed in rtnl_unlock.
3185 */
3186 if (err < 0) {
3187 /* If device is not registered at all, free it now */
3188 if (dev->reg_state == NETREG_UNINITIALIZED)
3190 free_netdev(dev);
3189 free_netdev(dev);
3191 goto out;
3192 }
3190 goto out;
3193 }
3191 }
3194 err = rtnl_configure_link(dev, ifm);
3192 } else {
3193 err = register_netdevice(dev);
3194 if (err < 0) {
3195 free_netdev(dev);
3196 goto out;
3197 }
3198 }
3199 err = rtnl_configure_link(dev, ifm);
3200 if (err < 0)
3201 goto out_unregister;
3202 if (link_net) {
3203 err = dev_change_net_namespace(dev, dest_net, ifname);
3195 if (err < 0)
3196 goto out_unregister;
3204 if (err < 0)
3205 goto out_unregister;
3197 if (link_net) {
3198 err = dev_change_net_namespace(dev, dest_net, ifname);
3199 if (err < 0)
3200 goto out_unregister;
3201 }
3202 if (tb[IFLA_MASTER]) {
3203 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]),
3204 extack);
3205 if (err)
3206 goto out_unregister;
3207 }
3206 }
3207 if (tb[IFLA_MASTER]) {
3208 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
3209 if (err)
3210 goto out_unregister;
3211 }
3208out:
3212out:
3209 if (link_net)
3210 put_net(link_net);
3211 put_net(dest_net);
3212 return err;
3213 if (link_net)
3214 put_net(link_net);
3215 put_net(dest_net);
3216 return err;
3213out_unregister:
3217out_unregister:
3214 if (ops->newlink) {
3215 LIST_HEAD(list_kill);
3218 if (ops->newlink) {
3219 LIST_HEAD(list_kill);
3216
3220
3217 ops->dellink(dev, &list_kill);
3218 unregister_netdevice_many(&list_kill);
3219 } else {
3220 unregister_netdevice(dev);
3221 }
3222 goto out;
3221 ops->dellink(dev, &list_kill);
3222 unregister_netdevice_many(&list_kill);
3223 } else {
3224 unregister_netdevice(dev);
3223 }
3225 }
3226 goto out;
3224}
3225
3226static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3227 struct netlink_ext_ack *extack)
3228{
3229 struct net *net = sock_net(skb->sk);
3230 struct net *tgt_net = net;
3231 struct ifinfomsg *ifm;

--- 1837 unchanged lines hidden ---
3227}
3228
3229static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3230 struct netlink_ext_ack *extack)
3231{
3232 struct net *net = sock_net(skb->sk);
3233 struct net *tgt_net = net;
3234 struct ifinfomsg *ifm;

--- 1837 unchanged lines hidden ---