blob: ba21d072be31c95827833f9e5014f20cfa43bb8b [file] [log] [blame]
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001/*
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 Emelyanove314dbd2007-09-25 16:14:46 -070011#include <linux/netdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070013#include <linux/ethtool.h>
14#include <linux/etherdevice.h>
Eric Dumazetcf05c702011-06-19 22:48:34 -070015#include <linux/u64_stats_sync.h>
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070016
Jiri Pirkof7b12602014-02-18 20:53:18 +010017#include <net/rtnetlink.h>
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070018#include <net/dst.h>
19#include <net/xfrm.h>
Stephen Hemmingerecef9692007-12-25 17:23:59 -080020#include <linux/veth.h>
Paul Gortmaker9d9779e2011-07-03 15:21:01 -040021#include <linux/module.h>
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070022
23#define DRV_NAME "veth"
24#define DRV_VERSION "1.0"
25
Eric Biederman38d40812009-03-03 23:36:04 -080026#define MIN_MTU 68 /* Min L3 MTU */
27#define MAX_MTU 65535 /* Max L3 MTU (arbitrary) */
Eric Biederman38d40812009-03-03 23:36:04 -080028
Eric Dumazet26811282012-12-29 16:02:43 +000029struct pcpu_vstats {
30 u64 packets;
31 u64 bytes;
Eric Dumazetcf05c702011-06-19 22:48:34 -070032 struct u64_stats_sync syncp;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070033};
34
35struct veth_priv {
Eric Dumazetd0e2c552013-01-04 15:42:40 +000036 struct net_device __rcu *peer;
Eric Dumazet26811282012-12-29 16:02:43 +000037 atomic64_t dropped;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070038};
39
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070040/*
41 * ethtool interface
42 */
43
44static struct {
45 const char string[ETH_GSTRING_LEN];
46} ethtool_stats_keys[] = {
47 { "peer_ifindex" },
48};
49
50static int veth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
51{
52 cmd->supported = 0;
53 cmd->advertising = 0;
David Decotigny70739492011-04-27 18:32:40 +000054 ethtool_cmd_speed_set(cmd, SPEED_10000);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070055 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
65static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
66{
Rick Jones33a5ba12011-11-15 14:59:53 +000067 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
68 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070069}
70
71static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
72{
73 switch(stringset) {
74 case ETH_SS_STATS:
75 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
76 break;
77 }
78}
79
Jeff Garzikb9f2c042007-10-03 18:07:32 -070080static int veth_get_sset_count(struct net_device *dev, int sset)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070081{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070082 switch (sset) {
83 case ETH_SS_STATS:
84 return ARRAY_SIZE(ethtool_stats_keys);
85 default:
86 return -EOPNOTSUPP;
87 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070088}
89
90static void veth_get_ethtool_stats(struct net_device *dev,
91 struct ethtool_stats *stats, u64 *data)
92{
Eric Dumazetd0e2c552013-01-04 15:42:40 +000093 struct veth_priv *priv = netdev_priv(dev);
94 struct net_device *peer = rtnl_dereference(priv->peer);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070095
Eric Dumazetd0e2c552013-01-04 15:42:40 +000096 data[0] = peer ? peer->ifindex : 0;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070097}
98
Stephen Hemminger0fc0b732009-09-02 01:03:33 -070099static const struct ethtool_ops veth_ethtool_ops = {
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700100 .get_settings = veth_get_settings,
101 .get_drvinfo = veth_get_drvinfo,
102 .get_link = ethtool_op_get_link,
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700103 .get_strings = veth_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700104 .get_sset_count = veth_get_sset_count,
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700105 .get_ethtool_stats = veth_get_ethtool_stats,
106};
107
Stephen Hemminger424efe92009-08-31 19:50:51 +0000108static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700109{
Eric Dumazet26811282012-12-29 16:02:43 +0000110 struct veth_priv *priv = netdev_priv(dev);
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000111 struct net_device *rcv;
Eric Dumazet26811282012-12-29 16:02:43 +0000112 int length = skb->len;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700113
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000114 rcu_read_lock();
115 rcv = rcu_dereference(priv->peer);
116 if (unlikely(!rcv)) {
117 kfree_skb(skb);
118 goto drop;
119 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700120
Eric Dumazet26811282012-12-29 16:02:43 +0000121 if (likely(dev_forward_skb(rcv, skb) == NET_RX_SUCCESS)) {
122 struct pcpu_vstats *stats = this_cpu_ptr(dev->vstats);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700123
Eric Dumazet26811282012-12-29 16:02:43 +0000124 u64_stats_update_begin(&stats->syncp);
125 stats->bytes += length;
126 stats->packets++;
127 u64_stats_update_end(&stats->syncp);
128 } else {
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000129drop:
Eric Dumazet26811282012-12-29 16:02:43 +0000130 atomic64_inc(&priv->dropped);
131 }
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000132 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +0000133 return NETDEV_TX_OK;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700134}
135
136/*
137 * general routines
138 */
139
Eric Dumazet26811282012-12-29 16:02:43 +0000140static u64 veth_stats_one(struct pcpu_vstats *result, struct net_device *dev)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700141{
Eric Dumazetcf05c702011-06-19 22:48:34 -0700142 struct veth_priv *priv = netdev_priv(dev);
David S. Miller11687a12009-06-25 02:45:42 -0700143 int cpu;
David S. Miller11687a12009-06-25 02:45:42 -0700144
Eric Dumazet26811282012-12-29 16:02:43 +0000145 result->packets = 0;
146 result->bytes = 0;
Eric Dumazet2b1c8b02009-11-18 07:09:39 +0000147 for_each_possible_cpu(cpu) {
Eric Dumazet26811282012-12-29 16:02:43 +0000148 struct pcpu_vstats *stats = per_cpu_ptr(dev->vstats, cpu);
149 u64 packets, bytes;
Eric Dumazetcf05c702011-06-19 22:48:34 -0700150 unsigned int start;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700151
Eric Dumazetcf05c702011-06-19 22:48:34 -0700152 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700153 start = u64_stats_fetch_begin_irq(&stats->syncp);
Eric Dumazet26811282012-12-29 16:02:43 +0000154 packets = stats->packets;
155 bytes = stats->bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700156 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
Eric Dumazet26811282012-12-29 16:02:43 +0000157 result->packets += packets;
158 result->bytes += bytes;
David S. Miller11687a12009-06-25 02:45:42 -0700159 }
Eric Dumazet26811282012-12-29 16:02:43 +0000160 return atomic64_read(&priv->dropped);
161}
162
163static struct rtnl_link_stats64 *veth_get_stats64(struct net_device *dev,
164 struct rtnl_link_stats64 *tot)
165{
166 struct veth_priv *priv = netdev_priv(dev);
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000167 struct net_device *peer;
Eric Dumazet26811282012-12-29 16:02:43 +0000168 struct pcpu_vstats one;
169
170 tot->tx_dropped = veth_stats_one(&one, dev);
171 tot->tx_bytes = one.bytes;
172 tot->tx_packets = one.packets;
173
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000174 rcu_read_lock();
175 peer = rcu_dereference(priv->peer);
176 if (peer) {
177 tot->rx_dropped = veth_stats_one(&one, peer);
178 tot->rx_bytes = one.bytes;
179 tot->rx_packets = one.packets;
180 }
181 rcu_read_unlock();
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700182
stephen hemminger6311cc42011-06-08 14:53:59 +0000183 return tot;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700184}
185
Gao feng5c70ef82013-10-04 16:52:24 +0800186/* fake multicast ability */
187static void veth_set_multicast_list(struct net_device *dev)
188{
189}
190
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700191static int veth_open(struct net_device *dev)
192{
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000193 struct veth_priv *priv = netdev_priv(dev);
194 struct net_device *peer = rtnl_dereference(priv->peer);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700195
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000196 if (!peer)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700197 return -ENOTCONN;
198
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000199 if (peer->flags & IFF_UP) {
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700200 netif_carrier_on(dev);
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000201 netif_carrier_on(peer);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700202 }
203 return 0;
204}
205
Eric W. Biederman2cf48a12009-02-25 19:47:29 +0000206static int veth_close(struct net_device *dev)
207{
208 struct veth_priv *priv = netdev_priv(dev);
Eric Dumazet2efd32e2013-01-10 08:32:45 +0000209 struct net_device *peer = rtnl_dereference(priv->peer);
Eric W. Biederman2cf48a12009-02-25 19:47:29 +0000210
211 netif_carrier_off(dev);
Eric Dumazet2efd32e2013-01-10 08:32:45 +0000212 if (peer)
213 netif_carrier_off(peer);
Eric W. Biederman2cf48a12009-02-25 19:47:29 +0000214
215 return 0;
216}
217
Eric Biederman38d40812009-03-03 23:36:04 -0800218static int is_valid_veth_mtu(int new_mtu)
219{
Eric Dumazet807540b2010-09-23 05:40:09 +0000220 return new_mtu >= MIN_MTU && new_mtu <= MAX_MTU;
Eric Biederman38d40812009-03-03 23:36:04 -0800221}
222
223static int veth_change_mtu(struct net_device *dev, int new_mtu)
224{
225 if (!is_valid_veth_mtu(new_mtu))
226 return -EINVAL;
227 dev->mtu = new_mtu;
228 return 0;
229}
230
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700231static int veth_dev_init(struct net_device *dev)
232{
WANG Cong1c213bd2014-02-13 11:46:28 -0800233 dev->vstats = netdev_alloc_pcpu_stats(struct pcpu_vstats);
Eric Dumazet26811282012-12-29 16:02:43 +0000234 if (!dev->vstats)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700235 return -ENOMEM;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700236 return 0;
237}
238
David S. Miller11687a12009-06-25 02:45:42 -0700239static void veth_dev_free(struct net_device *dev)
240{
Eric Dumazet26811282012-12-29 16:02:43 +0000241 free_percpu(dev->vstats);
David S. Miller11687a12009-06-25 02:45:42 -0700242 free_netdev(dev);
243}
244
WANG Congbb446c12014-06-23 15:36:02 -0700245#ifdef CONFIG_NET_POLL_CONTROLLER
246static void veth_poll_controller(struct net_device *dev)
247{
248 /* veth only receives frames when its peer sends one
249 * Since it's a synchronous operation, we are guaranteed
250 * never to have pending data when we poll for it so
251 * there is nothing to do here.
252 *
253 * We need this though so netpoll recognizes us as an interface that
254 * supports polling, which enables bridge devices in virt setups to
255 * still use netconsole
256 */
257}
258#endif /* CONFIG_NET_POLL_CONTROLLER */
259
Nicolas Dichtela45253b2015-04-02 17:07:11 +0200260static int veth_get_iflink(const struct net_device *dev)
261{
262 struct veth_priv *priv = netdev_priv(dev);
263 struct net_device *peer;
264 int iflink;
265
266 rcu_read_lock();
267 peer = rcu_dereference(priv->peer);
268 iflink = peer ? peer->ifindex : 0;
269 rcu_read_unlock();
270
271 return iflink;
272}
273
Stephen Hemminger4456e7b2008-11-19 21:50:10 -0800274static const struct net_device_ops veth_netdev_ops = {
Daniel Lezcanoee923622009-02-22 00:04:45 -0800275 .ndo_init = veth_dev_init,
276 .ndo_open = veth_open,
Eric W. Biederman2cf48a12009-02-25 19:47:29 +0000277 .ndo_stop = veth_close,
Daniel Lezcanoee923622009-02-22 00:04:45 -0800278 .ndo_start_xmit = veth_xmit,
Eric Biederman38d40812009-03-03 23:36:04 -0800279 .ndo_change_mtu = veth_change_mtu,
stephen hemminger6311cc42011-06-08 14:53:59 +0000280 .ndo_get_stats64 = veth_get_stats64,
Gao feng5c70ef82013-10-04 16:52:24 +0800281 .ndo_set_rx_mode = veth_set_multicast_list,
Daniel Lezcanoee923622009-02-22 00:04:45 -0800282 .ndo_set_mac_address = eth_mac_addr,
WANG Congbb446c12014-06-23 15:36:02 -0700283#ifdef CONFIG_NET_POLL_CONTROLLER
284 .ndo_poll_controller = veth_poll_controller,
285#endif
Nicolas Dichtela45253b2015-04-02 17:07:11 +0200286 .ndo_get_iflink = veth_get_iflink,
Toshiaki Makita1a04a822015-07-31 15:03:25 +0900287 .ndo_features_check = passthru_features_check,
Stephen Hemminger4456e7b2008-11-19 21:50:10 -0800288};
289
Eric Dumazet80933152012-12-29 16:26:10 +0000290#define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
291 NETIF_F_HW_CSUM | NETIF_F_RXCSUM | NETIF_F_HIGHDMA | \
Eric Dumazet82d81892013-10-25 18:25:03 -0700292 NETIF_F_GSO_GRE | NETIF_F_GSO_UDP_TUNNEL | \
293 NETIF_F_GSO_IPIP | NETIF_F_GSO_SIT | NETIF_F_UFO | \
Patrick McHardy28d2b132013-04-19 02:04:32 +0000294 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | \
295 NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_STAG_RX )
Eric Dumazet80933152012-12-29 16:26:10 +0000296
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700297static void veth_setup(struct net_device *dev)
298{
299 ether_setup(dev);
300
Neil Horman550fd082011-07-26 06:05:38 +0000301 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Hannes Frederic Sowa23ea5a92012-10-30 16:22:01 +0000302 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Phil Sutter02f01ec2015-08-18 10:30:29 +0200303 dev->priv_flags |= IFF_NO_QUEUE;
Neil Horman550fd082011-07-26 06:05:38 +0000304
Stephen Hemminger4456e7b2008-11-19 21:50:10 -0800305 dev->netdev_ops = &veth_netdev_ops;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700306 dev->ethtool_ops = &veth_ethtool_ops;
307 dev->features |= NETIF_F_LLTX;
Eric Dumazet80933152012-12-29 16:26:10 +0000308 dev->features |= VETH_FEATURES;
Toshiaki Makita8d0d21f2014-02-18 21:20:08 +0900309 dev->vlan_features = dev->features &
Vlad Yasevich3f8c7072014-03-27 22:14:48 -0400310 ~(NETIF_F_HW_VLAN_CTAG_TX |
311 NETIF_F_HW_VLAN_STAG_TX |
312 NETIF_F_HW_VLAN_CTAG_RX |
313 NETIF_F_HW_VLAN_STAG_RX);
David S. Miller11687a12009-06-25 02:45:42 -0700314 dev->destructor = veth_dev_free;
Michał Mirosława2c725f2011-03-31 01:01:35 +0000315
Eric Dumazet80933152012-12-29 16:26:10 +0000316 dev->hw_features = VETH_FEATURES;
Eric Dumazet82d81892013-10-25 18:25:03 -0700317 dev->hw_enc_features = VETH_FEATURES;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700318}
319
320/*
321 * netlink interface
322 */
323
324static int veth_validate(struct nlattr *tb[], struct nlattr *data[])
325{
326 if (tb[IFLA_ADDRESS]) {
327 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
328 return -EINVAL;
329 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
330 return -EADDRNOTAVAIL;
331 }
Eric Biederman38d40812009-03-03 23:36:04 -0800332 if (tb[IFLA_MTU]) {
333 if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
334 return -EINVAL;
335 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700336 return 0;
337}
338
339static struct rtnl_link_ops veth_link_ops;
340
Eric W. Biederman81adee42009-11-08 00:53:51 -0800341static int veth_newlink(struct net *src_net, struct net_device *dev,
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700342 struct nlattr *tb[], struct nlattr *data[])
343{
344 int err;
345 struct net_device *peer;
346 struct veth_priv *priv;
347 char ifname[IFNAMSIZ];
348 struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
Tom Gundersen55177502014-07-14 16:37:25 +0200349 unsigned char name_assign_type;
Patrick McHardy3729d502010-02-26 06:34:54 +0000350 struct ifinfomsg *ifmp;
Eric W. Biederman81adee42009-11-08 00:53:51 -0800351 struct net *net;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700352
353 /*
354 * create and register peer first
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700355 */
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700356 if (data != NULL && data[VETH_INFO_PEER] != NULL) {
357 struct nlattr *nla_peer;
358
359 nla_peer = data[VETH_INFO_PEER];
Patrick McHardy3729d502010-02-26 06:34:54 +0000360 ifmp = nla_data(nla_peer);
Jiri Pirkof7b12602014-02-18 20:53:18 +0100361 err = rtnl_nla_parse_ifla(peer_tb,
362 nla_data(nla_peer) + sizeof(struct ifinfomsg),
363 nla_len(nla_peer) - sizeof(struct ifinfomsg));
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700364 if (err < 0)
365 return err;
366
367 err = veth_validate(peer_tb, NULL);
368 if (err < 0)
369 return err;
370
371 tbp = peer_tb;
Patrick McHardy3729d502010-02-26 06:34:54 +0000372 } else {
373 ifmp = NULL;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700374 tbp = tb;
Patrick McHardy3729d502010-02-26 06:34:54 +0000375 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700376
Tom Gundersen55177502014-07-14 16:37:25 +0200377 if (tbp[IFLA_IFNAME]) {
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700378 nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
Tom Gundersen55177502014-07-14 16:37:25 +0200379 name_assign_type = NET_NAME_USER;
380 } else {
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700381 snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
Tom Gundersen55177502014-07-14 16:37:25 +0200382 name_assign_type = NET_NAME_ENUM;
383 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700384
Eric W. Biederman81adee42009-11-08 00:53:51 -0800385 net = rtnl_link_get_net(src_net, tbp);
386 if (IS_ERR(net))
387 return PTR_ERR(net);
388
Tom Gundersen55177502014-07-14 16:37:25 +0200389 peer = rtnl_create_link(net, ifname, name_assign_type,
390 &veth_link_ops, tbp);
Eric W. Biederman81adee42009-11-08 00:53:51 -0800391 if (IS_ERR(peer)) {
392 put_net(net);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700393 return PTR_ERR(peer);
Eric W. Biederman81adee42009-11-08 00:53:51 -0800394 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700395
396 if (tbp[IFLA_ADDRESS] == NULL)
Danny Kukawkaf2cedb62012-02-15 06:45:39 +0000397 eth_hw_addr_random(peer);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700398
Pavel Emelyanove6f8f1a2012-08-08 21:53:03 +0000399 if (ifmp && (dev->ifindex != 0))
400 peer->ifindex = ifmp->ifi_index;
401
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700402 err = register_netdevice(peer);
Eric W. Biederman81adee42009-11-08 00:53:51 -0800403 put_net(net);
404 net = NULL;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700405 if (err < 0)
406 goto err_register_peer;
407
408 netif_carrier_off(peer);
409
Patrick McHardy3729d502010-02-26 06:34:54 +0000410 err = rtnl_configure_link(peer, ifmp);
411 if (err < 0)
412 goto err_configure_peer;
413
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700414 /*
415 * register dev last
416 *
417 * note, that since we've registered new device the dev's name
418 * should be re-allocated
419 */
420
421 if (tb[IFLA_ADDRESS] == NULL)
Danny Kukawkaf2cedb62012-02-15 06:45:39 +0000422 eth_hw_addr_random(dev);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700423
Jiri Pirko6c8c4442011-04-30 01:28:17 +0000424 if (tb[IFLA_IFNAME])
425 nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
426 else
427 snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
428
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700429 err = register_netdevice(dev);
430 if (err < 0)
431 goto err_register_dev;
432
433 netif_carrier_off(dev);
434
435 /*
436 * tie the deviced together
437 */
438
439 priv = netdev_priv(dev);
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000440 rcu_assign_pointer(priv->peer, peer);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700441
442 priv = netdev_priv(peer);
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000443 rcu_assign_pointer(priv->peer, dev);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700444 return 0;
445
446err_register_dev:
447 /* nothing to do */
Patrick McHardy3729d502010-02-26 06:34:54 +0000448err_configure_peer:
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700449 unregister_netdevice(peer);
450 return err;
451
452err_register_peer:
453 free_netdev(peer);
454 return err;
455}
456
Eric Dumazet23289a32009-10-27 07:06:36 +0000457static void veth_dellink(struct net_device *dev, struct list_head *head)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700458{
459 struct veth_priv *priv;
460 struct net_device *peer;
461
462 priv = netdev_priv(dev);
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000463 peer = rtnl_dereference(priv->peer);
464
465 /* Note : dellink() is called from default_device_exit_batch(),
466 * before a rcu_synchronize() point. The devices are guaranteed
467 * not being freed before one RCU grace period.
468 */
469 RCU_INIT_POINTER(priv->peer, NULL);
Eric Dumazet24540532009-10-30 01:00:27 -0700470 unregister_netdevice_queue(dev, head);
Eric Dumazetf45a5c22013-02-08 20:10:49 +0000471
472 if (peer) {
473 priv = netdev_priv(peer);
474 RCU_INIT_POINTER(priv->peer, NULL);
475 unregister_netdevice_queue(peer, head);
476 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700477}
478
Thomas Graf23711432012-02-15 04:09:46 +0000479static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
480 [VETH_INFO_PEER] = { .len = sizeof(struct ifinfomsg) },
481};
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700482
Nicolas Dichtele5f4e7b2015-01-20 15:15:46 +0100483static struct net *veth_get_link_net(const struct net_device *dev)
484{
485 struct veth_priv *priv = netdev_priv(dev);
486 struct net_device *peer = rtnl_dereference(priv->peer);
487
488 return peer ? dev_net(peer) : dev_net(dev);
489}
490
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700491static struct rtnl_link_ops veth_link_ops = {
492 .kind = DRV_NAME,
493 .priv_size = sizeof(struct veth_priv),
494 .setup = veth_setup,
495 .validate = veth_validate,
496 .newlink = veth_newlink,
497 .dellink = veth_dellink,
498 .policy = veth_policy,
499 .maxtype = VETH_INFO_MAX,
Nicolas Dichtele5f4e7b2015-01-20 15:15:46 +0100500 .get_link_net = veth_get_link_net,
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700501};
502
503/*
504 * init/fini
505 */
506
507static __init int veth_init(void)
508{
509 return rtnl_link_register(&veth_link_ops);
510}
511
512static __exit void veth_exit(void)
513{
Patrick McHardy68365452008-01-20 17:25:14 -0800514 rtnl_link_unregister(&veth_link_ops);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700515}
516
517module_init(veth_init);
518module_exit(veth_exit);
519
520MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
521MODULE_LICENSE("GPL v2");
522MODULE_ALIAS_RTNL_LINK(DRV_NAME);