Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/net/veth.c |
| 3 | * |
| 4 | * Copyright (C) 2007 OpenVZ http://openvz.org, SWsoft Inc |
| 5 | * |
| 6 | * Author: Pavel Emelianov <xemul@openvz.org> |
| 7 | * Ethtool interface from: Eric W. Biederman <ebiederm@xmission.com> |
| 8 | * |
| 9 | */ |
| 10 | |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 11 | #include <linux/netdevice.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 13 | #include <linux/ethtool.h> |
| 14 | #include <linux/etherdevice.h> |
| 15 | |
| 16 | #include <net/dst.h> |
| 17 | #include <net/xfrm.h> |
Stephen Hemminger | ecef969 | 2007-12-25 17:23:59 -0800 | [diff] [blame] | 18 | #include <linux/veth.h> |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 19 | |
| 20 | #define DRV_NAME "veth" |
| 21 | #define DRV_VERSION "1.0" |
| 22 | |
Eric Biederman | 38d4081 | 2009-03-03 23:36:04 -0800 | [diff] [blame] | 23 | #define MIN_MTU 68 /* Min L3 MTU */ |
| 24 | #define MAX_MTU 65535 /* Max L3 MTU (arbitrary) */ |
Eric Biederman | 38d4081 | 2009-03-03 23:36:04 -0800 | [diff] [blame] | 25 | |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 26 | struct veth_net_stats { |
| 27 | unsigned long rx_packets; |
| 28 | unsigned long tx_packets; |
| 29 | unsigned long rx_bytes; |
| 30 | unsigned long tx_bytes; |
| 31 | unsigned long tx_dropped; |
Eric Biederman | 38d4081 | 2009-03-03 23:36:04 -0800 | [diff] [blame] | 32 | unsigned long rx_dropped; |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | struct veth_priv { |
| 36 | struct net_device *peer; |
Tejun Heo | 47d7427 | 2010-02-16 15:21:08 +0000 | [diff] [blame] | 37 | struct veth_net_stats __percpu *stats; |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 40 | /* |
| 41 | * ethtool interface |
| 42 | */ |
| 43 | |
| 44 | static struct { |
| 45 | const char string[ETH_GSTRING_LEN]; |
| 46 | } ethtool_stats_keys[] = { |
| 47 | { "peer_ifindex" }, |
| 48 | }; |
| 49 | |
| 50 | static int veth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 51 | { |
| 52 | cmd->supported = 0; |
| 53 | cmd->advertising = 0; |
David Decotigny | 7073949 | 2011-04-27 18:32:40 +0000 | [diff] [blame] | 54 | ethtool_cmd_speed_set(cmd, SPEED_10000); |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 55 | cmd->duplex = DUPLEX_FULL; |
| 56 | cmd->port = PORT_TP; |
| 57 | cmd->phy_address = 0; |
| 58 | cmd->transceiver = XCVR_INTERNAL; |
| 59 | cmd->autoneg = AUTONEG_DISABLE; |
| 60 | cmd->maxtxpkt = 0; |
| 61 | cmd->maxrxpkt = 0; |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) |
| 66 | { |
| 67 | strcpy(info->driver, DRV_NAME); |
| 68 | strcpy(info->version, DRV_VERSION); |
| 69 | strcpy(info->fw_version, "N/A"); |
| 70 | } |
| 71 | |
| 72 | static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf) |
| 73 | { |
| 74 | switch(stringset) { |
| 75 | case ETH_SS_STATS: |
| 76 | memcpy(buf, ðtool_stats_keys, sizeof(ethtool_stats_keys)); |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | |
Jeff Garzik | b9f2c04 | 2007-10-03 18:07:32 -0700 | [diff] [blame] | 81 | static int veth_get_sset_count(struct net_device *dev, int sset) |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 82 | { |
Jeff Garzik | b9f2c04 | 2007-10-03 18:07:32 -0700 | [diff] [blame] | 83 | switch (sset) { |
| 84 | case ETH_SS_STATS: |
| 85 | return ARRAY_SIZE(ethtool_stats_keys); |
| 86 | default: |
| 87 | return -EOPNOTSUPP; |
| 88 | } |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static void veth_get_ethtool_stats(struct net_device *dev, |
| 92 | struct ethtool_stats *stats, u64 *data) |
| 93 | { |
| 94 | struct veth_priv *priv; |
| 95 | |
| 96 | priv = netdev_priv(dev); |
| 97 | data[0] = priv->peer->ifindex; |
| 98 | } |
| 99 | |
Stephen Hemminger | 0fc0b73 | 2009-09-02 01:03:33 -0700 | [diff] [blame] | 100 | static const struct ethtool_ops veth_ethtool_ops = { |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 101 | .get_settings = veth_get_settings, |
| 102 | .get_drvinfo = veth_get_drvinfo, |
| 103 | .get_link = ethtool_op_get_link, |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 104 | .get_strings = veth_get_strings, |
Jeff Garzik | b9f2c04 | 2007-10-03 18:07:32 -0700 | [diff] [blame] | 105 | .get_sset_count = veth_get_sset_count, |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 106 | .get_ethtool_stats = veth_get_ethtool_stats, |
| 107 | }; |
| 108 | |
| 109 | /* |
| 110 | * xmit |
| 111 | */ |
| 112 | |
Stephen Hemminger | 424efe9 | 2009-08-31 19:50:51 +0000 | [diff] [blame] | 113 | static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev) |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 114 | { |
| 115 | struct net_device *rcv = NULL; |
| 116 | struct veth_priv *priv, *rcv_priv; |
Eric Biederman | 38d4081 | 2009-03-03 23:36:04 -0800 | [diff] [blame] | 117 | struct veth_net_stats *stats, *rcv_stats; |
Christoph Lameter | e7dcaa4 | 2009-10-03 19:48:23 +0900 | [diff] [blame] | 118 | int length; |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 119 | |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 120 | priv = netdev_priv(dev); |
| 121 | rcv = priv->peer; |
| 122 | rcv_priv = netdev_priv(rcv); |
| 123 | |
Christoph Lameter | e7dcaa4 | 2009-10-03 19:48:23 +0900 | [diff] [blame] | 124 | stats = this_cpu_ptr(priv->stats); |
| 125 | rcv_stats = this_cpu_ptr(rcv_priv->stats); |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 126 | |
| 127 | if (!(rcv->flags & IFF_UP)) |
Eric Biederman | 38d4081 | 2009-03-03 23:36:04 -0800 | [diff] [blame] | 128 | goto tx_drop; |
| 129 | |
Michał Mirosław | 0b79675 | 2010-12-14 12:35:13 +0000 | [diff] [blame] | 130 | /* don't change ip_summed == CHECKSUM_PARTIAL, as that |
| 131 | will cause bad checksum on forwarded packets */ |
Michał Mirosław | a2c725f | 2011-03-31 01:01:35 +0000 | [diff] [blame] | 132 | if (skb->ip_summed == CHECKSUM_NONE && |
| 133 | rcv->features & NETIF_F_RXCSUM) |
| 134 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 135 | |
Eric W. Biederman | 675071a | 2011-03-21 18:24:53 -0700 | [diff] [blame] | 136 | length = skb->len; |
Arnd Bergmann | 4454096 | 2009-11-26 06:07:08 +0000 | [diff] [blame] | 137 | if (dev_forward_skb(rcv, skb) != NET_RX_SUCCESS) |
| 138 | goto rx_drop; |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 139 | |
| 140 | stats->tx_bytes += length; |
| 141 | stats->tx_packets++; |
| 142 | |
Eric Biederman | 38d4081 | 2009-03-03 23:36:04 -0800 | [diff] [blame] | 143 | rcv_stats->rx_bytes += length; |
| 144 | rcv_stats->rx_packets++; |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 145 | |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 146 | return NETDEV_TX_OK; |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 147 | |
Eric Biederman | 38d4081 | 2009-03-03 23:36:04 -0800 | [diff] [blame] | 148 | tx_drop: |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 149 | kfree_skb(skb); |
| 150 | stats->tx_dropped++; |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 151 | return NETDEV_TX_OK; |
Eric Biederman | 38d4081 | 2009-03-03 23:36:04 -0800 | [diff] [blame] | 152 | |
| 153 | rx_drop: |
Eric Biederman | 38d4081 | 2009-03-03 23:36:04 -0800 | [diff] [blame] | 154 | rcv_stats->rx_dropped++; |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 155 | return NETDEV_TX_OK; |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /* |
| 159 | * general routines |
| 160 | */ |
| 161 | |
| 162 | static struct net_device_stats *veth_get_stats(struct net_device *dev) |
| 163 | { |
David S. Miller | 11687a1 | 2009-06-25 02:45:42 -0700 | [diff] [blame] | 164 | struct veth_priv *priv; |
David S. Miller | 11687a1 | 2009-06-25 02:45:42 -0700 | [diff] [blame] | 165 | int cpu; |
Eric Dumazet | 2b1c8b0 | 2009-11-18 07:09:39 +0000 | [diff] [blame] | 166 | struct veth_net_stats *stats, total = {0}; |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 167 | |
David S. Miller | 11687a1 | 2009-06-25 02:45:42 -0700 | [diff] [blame] | 168 | priv = netdev_priv(dev); |
David S. Miller | 11687a1 | 2009-06-25 02:45:42 -0700 | [diff] [blame] | 169 | |
Eric Dumazet | 2b1c8b0 | 2009-11-18 07:09:39 +0000 | [diff] [blame] | 170 | for_each_possible_cpu(cpu) { |
David S. Miller | 11687a1 | 2009-06-25 02:45:42 -0700 | [diff] [blame] | 171 | stats = per_cpu_ptr(priv->stats, cpu); |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 172 | |
Eric Dumazet | 2b1c8b0 | 2009-11-18 07:09:39 +0000 | [diff] [blame] | 173 | total.rx_packets += stats->rx_packets; |
| 174 | total.tx_packets += stats->tx_packets; |
| 175 | total.rx_bytes += stats->rx_bytes; |
| 176 | total.tx_bytes += stats->tx_bytes; |
| 177 | total.tx_dropped += stats->tx_dropped; |
| 178 | total.rx_dropped += stats->rx_dropped; |
David S. Miller | 11687a1 | 2009-06-25 02:45:42 -0700 | [diff] [blame] | 179 | } |
Eric Dumazet | 2b1c8b0 | 2009-11-18 07:09:39 +0000 | [diff] [blame] | 180 | dev->stats.rx_packets = total.rx_packets; |
| 181 | dev->stats.tx_packets = total.tx_packets; |
| 182 | dev->stats.rx_bytes = total.rx_bytes; |
| 183 | dev->stats.tx_bytes = total.tx_bytes; |
| 184 | dev->stats.tx_dropped = total.tx_dropped; |
| 185 | dev->stats.rx_dropped = total.rx_dropped; |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 186 | |
Eric Dumazet | 2b1c8b0 | 2009-11-18 07:09:39 +0000 | [diff] [blame] | 187 | return &dev->stats; |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | static int veth_open(struct net_device *dev) |
| 191 | { |
| 192 | struct veth_priv *priv; |
| 193 | |
| 194 | priv = netdev_priv(dev); |
| 195 | if (priv->peer == NULL) |
| 196 | return -ENOTCONN; |
| 197 | |
| 198 | if (priv->peer->flags & IFF_UP) { |
| 199 | netif_carrier_on(dev); |
| 200 | netif_carrier_on(priv->peer); |
| 201 | } |
| 202 | return 0; |
| 203 | } |
| 204 | |
Eric W. Biederman | 2cf48a1 | 2009-02-25 19:47:29 +0000 | [diff] [blame] | 205 | static int veth_close(struct net_device *dev) |
| 206 | { |
| 207 | struct veth_priv *priv = netdev_priv(dev); |
| 208 | |
| 209 | netif_carrier_off(dev); |
| 210 | netif_carrier_off(priv->peer); |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
Eric Biederman | 38d4081 | 2009-03-03 23:36:04 -0800 | [diff] [blame] | 215 | static int is_valid_veth_mtu(int new_mtu) |
| 216 | { |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 217 | return new_mtu >= MIN_MTU && new_mtu <= MAX_MTU; |
Eric Biederman | 38d4081 | 2009-03-03 23:36:04 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | static int veth_change_mtu(struct net_device *dev, int new_mtu) |
| 221 | { |
| 222 | if (!is_valid_veth_mtu(new_mtu)) |
| 223 | return -EINVAL; |
| 224 | dev->mtu = new_mtu; |
| 225 | return 0; |
| 226 | } |
| 227 | |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 228 | static int veth_dev_init(struct net_device *dev) |
| 229 | { |
Tejun Heo | 47d7427 | 2010-02-16 15:21:08 +0000 | [diff] [blame] | 230 | struct veth_net_stats __percpu *stats; |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 231 | struct veth_priv *priv; |
| 232 | |
| 233 | stats = alloc_percpu(struct veth_net_stats); |
| 234 | if (stats == NULL) |
| 235 | return -ENOMEM; |
| 236 | |
| 237 | priv = netdev_priv(dev); |
| 238 | priv->stats = stats; |
| 239 | return 0; |
| 240 | } |
| 241 | |
David S. Miller | 11687a1 | 2009-06-25 02:45:42 -0700 | [diff] [blame] | 242 | static void veth_dev_free(struct net_device *dev) |
| 243 | { |
| 244 | struct veth_priv *priv; |
| 245 | |
| 246 | priv = netdev_priv(dev); |
| 247 | free_percpu(priv->stats); |
| 248 | free_netdev(dev); |
| 249 | } |
| 250 | |
Stephen Hemminger | 4456e7b | 2008-11-19 21:50:10 -0800 | [diff] [blame] | 251 | static const struct net_device_ops veth_netdev_ops = { |
Daniel Lezcano | ee92362 | 2009-02-22 00:04:45 -0800 | [diff] [blame] | 252 | .ndo_init = veth_dev_init, |
| 253 | .ndo_open = veth_open, |
Eric W. Biederman | 2cf48a1 | 2009-02-25 19:47:29 +0000 | [diff] [blame] | 254 | .ndo_stop = veth_close, |
Daniel Lezcano | ee92362 | 2009-02-22 00:04:45 -0800 | [diff] [blame] | 255 | .ndo_start_xmit = veth_xmit, |
Eric Biederman | 38d4081 | 2009-03-03 23:36:04 -0800 | [diff] [blame] | 256 | .ndo_change_mtu = veth_change_mtu, |
Daniel Lezcano | ee92362 | 2009-02-22 00:04:45 -0800 | [diff] [blame] | 257 | .ndo_get_stats = veth_get_stats, |
| 258 | .ndo_set_mac_address = eth_mac_addr, |
Stephen Hemminger | 4456e7b | 2008-11-19 21:50:10 -0800 | [diff] [blame] | 259 | }; |
| 260 | |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 261 | static void veth_setup(struct net_device *dev) |
| 262 | { |
| 263 | ether_setup(dev); |
| 264 | |
Neil Horman | 9cf81e7 | 2011-07-26 06:05:38 +0000 | [diff] [blame] | 265 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
| 266 | |
Stephen Hemminger | 4456e7b | 2008-11-19 21:50:10 -0800 | [diff] [blame] | 267 | dev->netdev_ops = &veth_netdev_ops; |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 268 | dev->ethtool_ops = &veth_ethtool_ops; |
| 269 | dev->features |= NETIF_F_LLTX; |
David S. Miller | 11687a1 | 2009-06-25 02:45:42 -0700 | [diff] [blame] | 270 | dev->destructor = veth_dev_free; |
Michał Mirosław | a2c725f | 2011-03-31 01:01:35 +0000 | [diff] [blame] | 271 | |
| 272 | dev->hw_features = NETIF_F_NO_CSUM | NETIF_F_SG | NETIF_F_RXCSUM; |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* |
| 276 | * netlink interface |
| 277 | */ |
| 278 | |
| 279 | static int veth_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 280 | { |
| 281 | if (tb[IFLA_ADDRESS]) { |
| 282 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) |
| 283 | return -EINVAL; |
| 284 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) |
| 285 | return -EADDRNOTAVAIL; |
| 286 | } |
Eric Biederman | 38d4081 | 2009-03-03 23:36:04 -0800 | [diff] [blame] | 287 | if (tb[IFLA_MTU]) { |
| 288 | if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU]))) |
| 289 | return -EINVAL; |
| 290 | } |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | static struct rtnl_link_ops veth_link_ops; |
| 295 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 296 | static int veth_newlink(struct net *src_net, struct net_device *dev, |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 297 | struct nlattr *tb[], struct nlattr *data[]) |
| 298 | { |
| 299 | int err; |
| 300 | struct net_device *peer; |
| 301 | struct veth_priv *priv; |
| 302 | char ifname[IFNAMSIZ]; |
| 303 | struct nlattr *peer_tb[IFLA_MAX + 1], **tbp; |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 304 | struct ifinfomsg *ifmp; |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 305 | struct net *net; |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 306 | |
| 307 | /* |
| 308 | * create and register peer first |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 309 | */ |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 310 | if (data != NULL && data[VETH_INFO_PEER] != NULL) { |
| 311 | struct nlattr *nla_peer; |
| 312 | |
| 313 | nla_peer = data[VETH_INFO_PEER]; |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 314 | ifmp = nla_data(nla_peer); |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 315 | err = nla_parse(peer_tb, IFLA_MAX, |
| 316 | nla_data(nla_peer) + sizeof(struct ifinfomsg), |
| 317 | nla_len(nla_peer) - sizeof(struct ifinfomsg), |
| 318 | ifla_policy); |
| 319 | if (err < 0) |
| 320 | return err; |
| 321 | |
| 322 | err = veth_validate(peer_tb, NULL); |
| 323 | if (err < 0) |
| 324 | return err; |
| 325 | |
| 326 | tbp = peer_tb; |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 327 | } else { |
| 328 | ifmp = NULL; |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 329 | tbp = tb; |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 330 | } |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 331 | |
| 332 | if (tbp[IFLA_IFNAME]) |
| 333 | nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ); |
| 334 | else |
| 335 | snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d"); |
| 336 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 337 | net = rtnl_link_get_net(src_net, tbp); |
| 338 | if (IS_ERR(net)) |
| 339 | return PTR_ERR(net); |
| 340 | |
| 341 | peer = rtnl_create_link(src_net, net, ifname, &veth_link_ops, tbp); |
| 342 | if (IS_ERR(peer)) { |
| 343 | put_net(net); |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 344 | return PTR_ERR(peer); |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 345 | } |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 346 | |
| 347 | if (tbp[IFLA_ADDRESS] == NULL) |
| 348 | random_ether_addr(peer->dev_addr); |
| 349 | |
| 350 | err = register_netdevice(peer); |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 351 | put_net(net); |
| 352 | net = NULL; |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 353 | if (err < 0) |
| 354 | goto err_register_peer; |
| 355 | |
| 356 | netif_carrier_off(peer); |
| 357 | |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 358 | err = rtnl_configure_link(peer, ifmp); |
| 359 | if (err < 0) |
| 360 | goto err_configure_peer; |
| 361 | |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 362 | /* |
| 363 | * register dev last |
| 364 | * |
| 365 | * note, that since we've registered new device the dev's name |
| 366 | * should be re-allocated |
| 367 | */ |
| 368 | |
| 369 | if (tb[IFLA_ADDRESS] == NULL) |
| 370 | random_ether_addr(dev->dev_addr); |
| 371 | |
Jiri Pirko | 6c8c444 | 2011-04-30 01:28:17 +0000 | [diff] [blame] | 372 | if (tb[IFLA_IFNAME]) |
| 373 | nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ); |
| 374 | else |
| 375 | snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d"); |
| 376 | |
| 377 | if (strchr(dev->name, '%')) { |
| 378 | err = dev_alloc_name(dev, dev->name); |
| 379 | if (err < 0) |
| 380 | goto err_alloc_name; |
| 381 | } |
| 382 | |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 383 | err = register_netdevice(dev); |
| 384 | if (err < 0) |
| 385 | goto err_register_dev; |
| 386 | |
| 387 | netif_carrier_off(dev); |
| 388 | |
| 389 | /* |
| 390 | * tie the deviced together |
| 391 | */ |
| 392 | |
| 393 | priv = netdev_priv(dev); |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 394 | priv->peer = peer; |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 395 | |
| 396 | priv = netdev_priv(peer); |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 397 | priv->peer = dev; |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 398 | return 0; |
| 399 | |
| 400 | err_register_dev: |
| 401 | /* nothing to do */ |
Jiri Pirko | 6c8c444 | 2011-04-30 01:28:17 +0000 | [diff] [blame] | 402 | err_alloc_name: |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 403 | err_configure_peer: |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 404 | unregister_netdevice(peer); |
| 405 | return err; |
| 406 | |
| 407 | err_register_peer: |
| 408 | free_netdev(peer); |
| 409 | return err; |
| 410 | } |
| 411 | |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 412 | static void veth_dellink(struct net_device *dev, struct list_head *head) |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 413 | { |
| 414 | struct veth_priv *priv; |
| 415 | struct net_device *peer; |
| 416 | |
| 417 | priv = netdev_priv(dev); |
| 418 | peer = priv->peer; |
| 419 | |
Eric Dumazet | 2454053 | 2009-10-30 01:00:27 -0700 | [diff] [blame] | 420 | unregister_netdevice_queue(dev, head); |
| 421 | unregister_netdevice_queue(peer, head); |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | static const struct nla_policy veth_policy[VETH_INFO_MAX + 1]; |
| 425 | |
| 426 | static struct rtnl_link_ops veth_link_ops = { |
| 427 | .kind = DRV_NAME, |
| 428 | .priv_size = sizeof(struct veth_priv), |
| 429 | .setup = veth_setup, |
| 430 | .validate = veth_validate, |
| 431 | .newlink = veth_newlink, |
| 432 | .dellink = veth_dellink, |
| 433 | .policy = veth_policy, |
| 434 | .maxtype = VETH_INFO_MAX, |
| 435 | }; |
| 436 | |
| 437 | /* |
| 438 | * init/fini |
| 439 | */ |
| 440 | |
| 441 | static __init int veth_init(void) |
| 442 | { |
| 443 | return rtnl_link_register(&veth_link_ops); |
| 444 | } |
| 445 | |
| 446 | static __exit void veth_exit(void) |
| 447 | { |
Patrick McHardy | 6836545 | 2008-01-20 17:25:14 -0800 | [diff] [blame] | 448 | rtnl_link_unregister(&veth_link_ops); |
Pavel Emelyanov | e314dbd | 2007-09-25 16:14:46 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | module_init(veth_init); |
| 452 | module_exit(veth_exit); |
| 453 | |
| 454 | MODULE_DESCRIPTION("Virtual Ethernet Tunnel"); |
| 455 | MODULE_LICENSE("GPL v2"); |
| 456 | MODULE_ALIAS_RTNL_LINK(DRV_NAME); |