Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  VLAN netlink control interface
0004  *
0005  *  Copyright (c) 2007 Patrick McHardy <kaber@trash.net>
0006  */
0007 
0008 #include <linux/kernel.h>
0009 #include <linux/netdevice.h>
0010 #include <linux/if_vlan.h>
0011 #include <linux/module.h>
0012 #include <net/net_namespace.h>
0013 #include <net/netlink.h>
0014 #include <net/rtnetlink.h>
0015 #include "vlan.h"
0016 
0017 
0018 static const struct nla_policy vlan_policy[IFLA_VLAN_MAX + 1] = {
0019     [IFLA_VLAN_ID]      = { .type = NLA_U16 },
0020     [IFLA_VLAN_FLAGS]   = { .len = sizeof(struct ifla_vlan_flags) },
0021     [IFLA_VLAN_EGRESS_QOS]  = { .type = NLA_NESTED },
0022     [IFLA_VLAN_INGRESS_QOS] = { .type = NLA_NESTED },
0023     [IFLA_VLAN_PROTOCOL]    = { .type = NLA_U16 },
0024 };
0025 
0026 static const struct nla_policy vlan_map_policy[IFLA_VLAN_QOS_MAX + 1] = {
0027     [IFLA_VLAN_QOS_MAPPING] = { .len = sizeof(struct ifla_vlan_qos_mapping) },
0028 };
0029 
0030 
0031 static inline int vlan_validate_qos_map(struct nlattr *attr)
0032 {
0033     if (!attr)
0034         return 0;
0035     return nla_validate_nested_deprecated(attr, IFLA_VLAN_QOS_MAX,
0036                           vlan_map_policy, NULL);
0037 }
0038 
0039 static int vlan_validate(struct nlattr *tb[], struct nlattr *data[],
0040              struct netlink_ext_ack *extack)
0041 {
0042     struct ifla_vlan_flags *flags;
0043     u16 id;
0044     int err;
0045 
0046     if (tb[IFLA_ADDRESS]) {
0047         if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
0048             NL_SET_ERR_MSG_MOD(extack, "Invalid link address");
0049             return -EINVAL;
0050         }
0051         if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
0052             NL_SET_ERR_MSG_MOD(extack, "Invalid link address");
0053             return -EADDRNOTAVAIL;
0054         }
0055     }
0056 
0057     if (!data) {
0058         NL_SET_ERR_MSG_MOD(extack, "VLAN properties not specified");
0059         return -EINVAL;
0060     }
0061 
0062     if (data[IFLA_VLAN_PROTOCOL]) {
0063         switch (nla_get_be16(data[IFLA_VLAN_PROTOCOL])) {
0064         case htons(ETH_P_8021Q):
0065         case htons(ETH_P_8021AD):
0066             break;
0067         default:
0068             NL_SET_ERR_MSG_MOD(extack, "Invalid VLAN protocol");
0069             return -EPROTONOSUPPORT;
0070         }
0071     }
0072 
0073     if (data[IFLA_VLAN_ID]) {
0074         id = nla_get_u16(data[IFLA_VLAN_ID]);
0075         if (id >= VLAN_VID_MASK) {
0076             NL_SET_ERR_MSG_MOD(extack, "Invalid VLAN id");
0077             return -ERANGE;
0078         }
0079     }
0080     if (data[IFLA_VLAN_FLAGS]) {
0081         flags = nla_data(data[IFLA_VLAN_FLAGS]);
0082         if ((flags->flags & flags->mask) &
0083             ~(VLAN_FLAG_REORDER_HDR | VLAN_FLAG_GVRP |
0084               VLAN_FLAG_LOOSE_BINDING | VLAN_FLAG_MVRP |
0085               VLAN_FLAG_BRIDGE_BINDING)) {
0086             NL_SET_ERR_MSG_MOD(extack, "Invalid VLAN flags");
0087             return -EINVAL;
0088         }
0089     }
0090 
0091     err = vlan_validate_qos_map(data[IFLA_VLAN_INGRESS_QOS]);
0092     if (err < 0) {
0093         NL_SET_ERR_MSG_MOD(extack, "Invalid ingress QOS map");
0094         return err;
0095     }
0096     err = vlan_validate_qos_map(data[IFLA_VLAN_EGRESS_QOS]);
0097     if (err < 0) {
0098         NL_SET_ERR_MSG_MOD(extack, "Invalid egress QOS map");
0099         return err;
0100     }
0101     return 0;
0102 }
0103 
0104 static int vlan_changelink(struct net_device *dev, struct nlattr *tb[],
0105                struct nlattr *data[],
0106                struct netlink_ext_ack *extack)
0107 {
0108     struct ifla_vlan_flags *flags;
0109     struct ifla_vlan_qos_mapping *m;
0110     struct nlattr *attr;
0111     int rem, err;
0112 
0113     if (data[IFLA_VLAN_FLAGS]) {
0114         flags = nla_data(data[IFLA_VLAN_FLAGS]);
0115         err = vlan_dev_change_flags(dev, flags->flags, flags->mask);
0116         if (err)
0117             return err;
0118     }
0119     if (data[IFLA_VLAN_INGRESS_QOS]) {
0120         nla_for_each_nested(attr, data[IFLA_VLAN_INGRESS_QOS], rem) {
0121             m = nla_data(attr);
0122             vlan_dev_set_ingress_priority(dev, m->to, m->from);
0123         }
0124     }
0125     if (data[IFLA_VLAN_EGRESS_QOS]) {
0126         nla_for_each_nested(attr, data[IFLA_VLAN_EGRESS_QOS], rem) {
0127             m = nla_data(attr);
0128             err = vlan_dev_set_egress_priority(dev, m->from, m->to);
0129             if (err)
0130                 return err;
0131         }
0132     }
0133     return 0;
0134 }
0135 
0136 static int vlan_newlink(struct net *src_net, struct net_device *dev,
0137             struct nlattr *tb[], struct nlattr *data[],
0138             struct netlink_ext_ack *extack)
0139 {
0140     struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
0141     struct net_device *real_dev;
0142     unsigned int max_mtu;
0143     __be16 proto;
0144     int err;
0145 
0146     if (!data[IFLA_VLAN_ID]) {
0147         NL_SET_ERR_MSG_MOD(extack, "VLAN id not specified");
0148         return -EINVAL;
0149     }
0150 
0151     if (!tb[IFLA_LINK]) {
0152         NL_SET_ERR_MSG_MOD(extack, "link not specified");
0153         return -EINVAL;
0154     }
0155 
0156     real_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
0157     if (!real_dev) {
0158         NL_SET_ERR_MSG_MOD(extack, "link does not exist");
0159         return -ENODEV;
0160     }
0161 
0162     if (data[IFLA_VLAN_PROTOCOL])
0163         proto = nla_get_be16(data[IFLA_VLAN_PROTOCOL]);
0164     else
0165         proto = htons(ETH_P_8021Q);
0166 
0167     vlan->vlan_proto = proto;
0168     vlan->vlan_id    = nla_get_u16(data[IFLA_VLAN_ID]);
0169     vlan->real_dev   = real_dev;
0170     dev->priv_flags |= (real_dev->priv_flags & IFF_XMIT_DST_RELEASE);
0171     vlan->flags  = VLAN_FLAG_REORDER_HDR;
0172 
0173     err = vlan_check_real_dev(real_dev, vlan->vlan_proto, vlan->vlan_id,
0174                   extack);
0175     if (err < 0)
0176         return err;
0177 
0178     max_mtu = netif_reduces_vlan_mtu(real_dev) ? real_dev->mtu - VLAN_HLEN :
0179                              real_dev->mtu;
0180     if (!tb[IFLA_MTU])
0181         dev->mtu = max_mtu;
0182     else if (dev->mtu > max_mtu)
0183         return -EINVAL;
0184 
0185     /* Note: If this initial vlan_changelink() fails, we need
0186      * to call vlan_dev_free_egress_priority() to free memory.
0187      */
0188     err = vlan_changelink(dev, tb, data, extack);
0189 
0190     if (!err)
0191         err = register_vlan_dev(dev, extack);
0192 
0193     if (err)
0194         vlan_dev_free_egress_priority(dev);
0195     return err;
0196 }
0197 
0198 static inline size_t vlan_qos_map_size(unsigned int n)
0199 {
0200     if (n == 0)
0201         return 0;
0202     /* IFLA_VLAN_{EGRESS,INGRESS}_QOS + n * IFLA_VLAN_QOS_MAPPING */
0203     return nla_total_size(sizeof(struct nlattr)) +
0204            nla_total_size(sizeof(struct ifla_vlan_qos_mapping)) * n;
0205 }
0206 
0207 static size_t vlan_get_size(const struct net_device *dev)
0208 {
0209     struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
0210 
0211     return nla_total_size(2) +  /* IFLA_VLAN_PROTOCOL */
0212            nla_total_size(2) +  /* IFLA_VLAN_ID */
0213            nla_total_size(sizeof(struct ifla_vlan_flags)) + /* IFLA_VLAN_FLAGS */
0214            vlan_qos_map_size(vlan->nr_ingress_mappings) +
0215            vlan_qos_map_size(vlan->nr_egress_mappings);
0216 }
0217 
0218 static int vlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
0219 {
0220     struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
0221     struct vlan_priority_tci_mapping *pm;
0222     struct ifla_vlan_flags f;
0223     struct ifla_vlan_qos_mapping m;
0224     struct nlattr *nest;
0225     unsigned int i;
0226 
0227     if (nla_put_be16(skb, IFLA_VLAN_PROTOCOL, vlan->vlan_proto) ||
0228         nla_put_u16(skb, IFLA_VLAN_ID, vlan->vlan_id))
0229         goto nla_put_failure;
0230     if (vlan->flags) {
0231         f.flags = vlan->flags;
0232         f.mask  = ~0;
0233         if (nla_put(skb, IFLA_VLAN_FLAGS, sizeof(f), &f))
0234             goto nla_put_failure;
0235     }
0236     if (vlan->nr_ingress_mappings) {
0237         nest = nla_nest_start_noflag(skb, IFLA_VLAN_INGRESS_QOS);
0238         if (nest == NULL)
0239             goto nla_put_failure;
0240 
0241         for (i = 0; i < ARRAY_SIZE(vlan->ingress_priority_map); i++) {
0242             if (!vlan->ingress_priority_map[i])
0243                 continue;
0244 
0245             m.from = i;
0246             m.to   = vlan->ingress_priority_map[i];
0247             if (nla_put(skb, IFLA_VLAN_QOS_MAPPING,
0248                     sizeof(m), &m))
0249                 goto nla_put_failure;
0250         }
0251         nla_nest_end(skb, nest);
0252     }
0253 
0254     if (vlan->nr_egress_mappings) {
0255         nest = nla_nest_start_noflag(skb, IFLA_VLAN_EGRESS_QOS);
0256         if (nest == NULL)
0257             goto nla_put_failure;
0258 
0259         for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) {
0260             for (pm = vlan->egress_priority_map[i]; pm;
0261                  pm = pm->next) {
0262                 if (!pm->vlan_qos)
0263                     continue;
0264 
0265                 m.from = pm->priority;
0266                 m.to   = (pm->vlan_qos >> 13) & 0x7;
0267                 if (nla_put(skb, IFLA_VLAN_QOS_MAPPING,
0268                         sizeof(m), &m))
0269                     goto nla_put_failure;
0270             }
0271         }
0272         nla_nest_end(skb, nest);
0273     }
0274     return 0;
0275 
0276 nla_put_failure:
0277     return -EMSGSIZE;
0278 }
0279 
0280 static struct net *vlan_get_link_net(const struct net_device *dev)
0281 {
0282     struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
0283 
0284     return dev_net(real_dev);
0285 }
0286 
0287 struct rtnl_link_ops vlan_link_ops __read_mostly = {
0288     .kind       = "vlan",
0289     .maxtype    = IFLA_VLAN_MAX,
0290     .policy     = vlan_policy,
0291     .priv_size  = sizeof(struct vlan_dev_priv),
0292     .setup      = vlan_setup,
0293     .validate   = vlan_validate,
0294     .newlink    = vlan_newlink,
0295     .changelink = vlan_changelink,
0296     .dellink    = unregister_vlan_dev,
0297     .get_size   = vlan_get_size,
0298     .fill_info  = vlan_fill_info,
0299     .get_link_net   = vlan_get_link_net,
0300 };
0301 
0302 int __init vlan_netlink_init(void)
0303 {
0304     return rtnl_link_register(&vlan_link_ops);
0305 }
0306 
0307 void __exit vlan_netlink_fini(void)
0308 {
0309     rtnl_link_unregister(&vlan_link_ops);
0310 }
0311 
0312 MODULE_ALIAS_RTNL_LINK("vlan");