Patrick McHardy | 07b5b17 | 2007-06-13 12:07:54 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * VLAN netlink control interface |
| 3 | * |
| 4 | * Copyright (c) 2007 Patrick McHardy <kaber@trash.net> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * version 2 as published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/netdevice.h> |
| 13 | #include <linux/if_vlan.h> |
| 14 | #include <net/netlink.h> |
| 15 | #include <net/rtnetlink.h> |
| 16 | #include "vlan.h" |
| 17 | |
| 18 | |
| 19 | static const struct nla_policy vlan_policy[IFLA_VLAN_MAX + 1] = { |
| 20 | [IFLA_VLAN_ID] = { .type = NLA_U16 }, |
| 21 | [IFLA_VLAN_FLAGS] = { .len = sizeof(struct ifla_vlan_flags) }, |
| 22 | [IFLA_VLAN_EGRESS_QOS] = { .type = NLA_NESTED }, |
| 23 | [IFLA_VLAN_INGRESS_QOS] = { .type = NLA_NESTED }, |
| 24 | }; |
| 25 | |
| 26 | static const struct nla_policy vlan_map_policy[IFLA_VLAN_QOS_MAX + 1] = { |
| 27 | [IFLA_VLAN_QOS_MAPPING] = { .len = sizeof(struct ifla_vlan_qos_mapping) }, |
| 28 | }; |
| 29 | |
| 30 | |
| 31 | static inline int vlan_validate_qos_map(struct nlattr *attr) |
| 32 | { |
| 33 | if (!attr) |
| 34 | return 0; |
| 35 | return nla_validate_nested(attr, IFLA_VLAN_QOS_MAX, vlan_map_policy); |
| 36 | } |
| 37 | |
| 38 | static int vlan_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 39 | { |
| 40 | struct ifla_vlan_flags *flags; |
| 41 | u16 id; |
| 42 | int err; |
| 43 | |
| 44 | if (!data) |
| 45 | return -EINVAL; |
| 46 | |
| 47 | if (data[IFLA_VLAN_ID]) { |
| 48 | id = nla_get_u16(data[IFLA_VLAN_ID]); |
| 49 | if (id >= VLAN_VID_MASK) |
| 50 | return -ERANGE; |
| 51 | } |
| 52 | if (data[IFLA_VLAN_FLAGS]) { |
| 53 | flags = nla_data(data[IFLA_VLAN_FLAGS]); |
| 54 | if ((flags->flags & flags->mask) & ~VLAN_FLAG_REORDER_HDR) |
| 55 | return -EINVAL; |
| 56 | } |
| 57 | |
| 58 | err = vlan_validate_qos_map(data[IFLA_VLAN_INGRESS_QOS]); |
| 59 | if (err < 0) |
| 60 | return err; |
| 61 | err = vlan_validate_qos_map(data[IFLA_VLAN_EGRESS_QOS]); |
| 62 | if (err < 0) |
| 63 | return err; |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static int vlan_changelink(struct net_device *dev, |
| 68 | struct nlattr *tb[], struct nlattr *data[]) |
| 69 | { |
| 70 | struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev); |
| 71 | struct ifla_vlan_flags *flags; |
| 72 | struct ifla_vlan_qos_mapping *m; |
| 73 | struct nlattr *attr; |
| 74 | int rem; |
| 75 | |
| 76 | if (data[IFLA_VLAN_FLAGS]) { |
| 77 | flags = nla_data(data[IFLA_VLAN_FLAGS]); |
| 78 | vlan->flags = (vlan->flags & ~flags->mask) | |
| 79 | (flags->flags & flags->mask); |
| 80 | } |
| 81 | if (data[IFLA_VLAN_INGRESS_QOS]) { |
| 82 | nla_for_each_nested(attr, data[IFLA_VLAN_INGRESS_QOS], rem) { |
| 83 | m = nla_data(attr); |
| 84 | vlan_dev_set_ingress_priority(dev, m->to, m->from); |
| 85 | } |
| 86 | } |
| 87 | if (data[IFLA_VLAN_EGRESS_QOS]) { |
| 88 | nla_for_each_nested(attr, data[IFLA_VLAN_EGRESS_QOS], rem) { |
| 89 | m = nla_data(attr); |
| 90 | vlan_dev_set_egress_priority(dev, m->from, m->to); |
| 91 | } |
| 92 | } |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | static int vlan_newlink(struct net_device *dev, |
| 97 | struct nlattr *tb[], struct nlattr *data[]) |
| 98 | { |
| 99 | struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev); |
| 100 | struct net_device *real_dev; |
| 101 | int err; |
| 102 | |
| 103 | if (!data[IFLA_VLAN_ID]) |
| 104 | return -EINVAL; |
| 105 | |
| 106 | if (!tb[IFLA_LINK]) |
| 107 | return -EINVAL; |
| 108 | real_dev = __dev_get_by_index(nla_get_u32(tb[IFLA_LINK])); |
| 109 | if (!real_dev) |
| 110 | return -ENODEV; |
| 111 | |
| 112 | vlan->vlan_id = nla_get_u16(data[IFLA_VLAN_ID]); |
| 113 | vlan->real_dev = real_dev; |
| 114 | vlan->flags = VLAN_FLAG_REORDER_HDR; |
| 115 | |
| 116 | err = vlan_check_real_dev(real_dev, vlan->vlan_id); |
| 117 | if (err < 0) |
| 118 | return err; |
| 119 | |
| 120 | if (!tb[IFLA_MTU]) |
| 121 | dev->mtu = real_dev->mtu; |
| 122 | else if (dev->mtu > real_dev->mtu) |
| 123 | return -EINVAL; |
| 124 | |
| 125 | err = vlan_changelink(dev, tb, data); |
| 126 | if (err < 0) |
| 127 | return err; |
| 128 | |
| 129 | return register_vlan_dev(dev); |
| 130 | } |
| 131 | |
| 132 | static void vlan_dellink(struct net_device *dev) |
| 133 | { |
| 134 | unregister_vlan_device(dev); |
| 135 | } |
| 136 | |
| 137 | static inline size_t vlan_qos_map_size(unsigned int n) |
| 138 | { |
| 139 | if (n == 0) |
| 140 | return 0; |
| 141 | /* IFLA_VLAN_{EGRESS,INGRESS}_QOS + n * IFLA_VLAN_QOS_MAPPING */ |
| 142 | return nla_total_size(sizeof(struct nlattr)) + |
| 143 | nla_total_size(sizeof(struct ifla_vlan_qos_mapping)) * n; |
| 144 | } |
| 145 | |
| 146 | static size_t vlan_get_size(const struct net_device *dev) |
| 147 | { |
| 148 | struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev); |
| 149 | |
| 150 | return nla_total_size(2) + /* IFLA_VLAN_ID */ |
| 151 | vlan_qos_map_size(vlan->nr_ingress_mappings) + |
| 152 | vlan_qos_map_size(vlan->nr_egress_mappings); |
| 153 | } |
| 154 | |
| 155 | static int vlan_fill_info(struct sk_buff *skb, const struct net_device *dev) |
| 156 | { |
| 157 | struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev); |
| 158 | struct vlan_priority_tci_mapping *pm; |
| 159 | struct ifla_vlan_flags f; |
| 160 | struct ifla_vlan_qos_mapping m; |
| 161 | struct nlattr *nest; |
| 162 | unsigned int i; |
| 163 | |
| 164 | NLA_PUT_U16(skb, IFLA_VLAN_ID, VLAN_DEV_INFO(dev)->vlan_id); |
| 165 | if (vlan->flags) { |
| 166 | f.flags = vlan->flags; |
| 167 | f.mask = ~0; |
| 168 | NLA_PUT(skb, IFLA_VLAN_FLAGS, sizeof(f), &f); |
| 169 | } |
| 170 | if (vlan->nr_ingress_mappings) { |
| 171 | nest = nla_nest_start(skb, IFLA_VLAN_INGRESS_QOS); |
| 172 | if (nest == NULL) |
| 173 | goto nla_put_failure; |
| 174 | |
| 175 | for (i = 0; i < ARRAY_SIZE(vlan->ingress_priority_map); i++) { |
| 176 | if (!vlan->ingress_priority_map[i]) |
| 177 | continue; |
| 178 | |
| 179 | m.from = i; |
| 180 | m.to = vlan->ingress_priority_map[i]; |
| 181 | NLA_PUT(skb, IFLA_VLAN_QOS_MAPPING, |
| 182 | sizeof(m), &m); |
| 183 | } |
| 184 | nla_nest_end(skb, nest); |
| 185 | } |
| 186 | |
| 187 | if (vlan->nr_egress_mappings) { |
| 188 | nest = nla_nest_start(skb, IFLA_VLAN_EGRESS_QOS); |
| 189 | if (nest == NULL) |
| 190 | goto nla_put_failure; |
| 191 | |
| 192 | for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) { |
| 193 | for (pm = vlan->egress_priority_map[i]; pm; |
| 194 | pm = pm->next) { |
| 195 | if (!pm->vlan_qos) |
| 196 | continue; |
| 197 | |
| 198 | m.from = pm->priority; |
| 199 | m.to = (pm->vlan_qos >> 13) & 0x7; |
| 200 | NLA_PUT(skb, IFLA_VLAN_QOS_MAPPING, |
| 201 | sizeof(m), &m); |
| 202 | } |
| 203 | } |
| 204 | nla_nest_end(skb, nest); |
| 205 | } |
| 206 | return 0; |
| 207 | |
| 208 | nla_put_failure: |
| 209 | return -EMSGSIZE; |
| 210 | } |
| 211 | |
| 212 | struct rtnl_link_ops vlan_link_ops __read_mostly = { |
| 213 | .kind = "vlan", |
| 214 | .maxtype = IFLA_VLAN_MAX, |
| 215 | .policy = vlan_policy, |
| 216 | .priv_size = sizeof(struct vlan_dev_info), |
| 217 | .setup = vlan_setup, |
| 218 | .validate = vlan_validate, |
| 219 | .newlink = vlan_newlink, |
| 220 | .changelink = vlan_changelink, |
| 221 | .dellink = vlan_dellink, |
| 222 | .get_size = vlan_get_size, |
| 223 | .fill_info = vlan_fill_info, |
| 224 | }; |
| 225 | |
| 226 | int __init vlan_netlink_init(void) |
| 227 | { |
| 228 | return rtnl_link_register(&vlan_link_ops); |
| 229 | } |
| 230 | |
| 231 | void __exit vlan_netlink_fini(void) |
| 232 | { |
| 233 | rtnl_link_unregister(&vlan_link_ops); |
| 234 | } |
| 235 | |
| 236 | MODULE_ALIAS_RTNL_LINK("vlan"); |