dev.c 209 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9
/*
 * 	NET3	Protocol independent device support routines.
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 *
 *	Derived from the non IP parts of dev.c 1.0.19
10
 * 		Authors:	Ross Biro
Linus Torvalds's avatar
Linus Torvalds committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
 *				Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
 *				Mark Evans, <evansmp@uhura.aston.ac.uk>
 *
 *	Additional Authors:
 *		Florian la Roche <rzsfl@rz.uni-sb.de>
 *		Alan Cox <gw4pts@gw4pts.ampr.org>
 *		David Hinds <dahinds@users.sourceforge.net>
 *		Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
 *		Adam Sulmicki <adam@cfar.umd.edu>
 *              Pekka Riikonen <priikone@poesidon.pspt.fi>
 *
 *	Changes:
 *              D.J. Barrow     :       Fixed bug where dev->refcnt gets set
 *              			to 2 if register_netdev gets called
 *              			before net_dev_init & also removed a
 *              			few lines of code in the process.
 *		Alan Cox	:	device private ioctl copies fields back.
 *		Alan Cox	:	Transmit queue code does relevant
 *					stunts to keep the queue safe.
 *		Alan Cox	:	Fixed double lock.
 *		Alan Cox	:	Fixed promisc NULL pointer trap
 *		????????	:	Support the full private ioctl range
 *		Alan Cox	:	Moved ioctl permission check into
 *					drivers
 *		Tim Kordas	:	SIOCADDMULTI/SIOCDELMULTI
 *		Alan Cox	:	100 backlog just doesn't cut it when
 *					you start doing multicast video 8)
 *		Alan Cox	:	Rewrote net_bh and list manager.
 *		Alan Cox	: 	Fix ETH_P_ALL echoback lengths.
 *		Alan Cox	:	Took out transmit every packet pass
 *					Saved a few bytes in the ioctl handler
 *		Alan Cox	:	Network driver sets packet type before
 *					calling netif_rx. Saves a function
 *					call a packet.
 *		Alan Cox	:	Hashed net_bh()
 *		Richard Kooijman:	Timestamp fixes.
 *		Alan Cox	:	Wrong field in SIOCGIFDSTADDR
 *		Alan Cox	:	Device lock protection.
 *		Alan Cox	: 	Fixed nasty side effect of device close
 *					changes.
 *		Rudi Cilibrasi	:	Pass the right thing to
 *					set_mac_address()
 *		Dave Miller	:	32bit quantity for the device lock to
 *					make it work out on a Sparc.
 *		Bjorn Ekwall	:	Added KERNELD hack.
 *		Alan Cox	:	Cleaned up the backlog initialise.
 *		Craig Metz	:	SIOCGIFCONF fix if space for under
 *					1 device.
 *	    Thomas Bogendoerfer :	Return ENODEV for dev_open, if there
 *					is no device open function.
 *		Andi Kleen	:	Fix error reporting for SIOCGIFCONF
 *	    Michael Chastain	:	Fix signed/unsigned for SIOCGIFCONF
 *		Cyrus Durgin	:	Cleaned for KMOD
 *		Adam Sulmicki   :	Bug Fix : Network Device Unload
 *					A network device unload needs to purge
 *					the backlog queue.
 *	Paul Rusty Russell	:	SIOCSIFNAME
 *              Pekka Riikonen  :	Netdev boot-time settings code
 *              Andrew Morton   :       Make unregister_netdevice wait
 *              			indefinitely on dev->refcnt
 * 		J Hadi Salim	:	- Backlog queue sampling
 *				        - netif_rx() feedback
 */

#include <asm/uaccess.h>
#include <linux/bitops.h>
77
#include <linux/capability.h>
Linus Torvalds's avatar
Linus Torvalds committed
78 79 80
#include <linux/cpu.h>
#include <linux/types.h>
#include <linux/kernel.h>
81
#include <linux/hash.h>
82
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
83
#include <linux/sched.h>
Arjan van de Ven's avatar
Arjan van de Ven committed
84
#include <linux/mutex.h>
Linus Torvalds's avatar
Linus Torvalds committed
85 86 87 88 89 90 91 92 93
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/socket.h>
#include <linux/sockios.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/if_ether.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
94
#include <linux/ethtool.h>
Linus Torvalds's avatar
Linus Torvalds committed
95 96
#include <linux/notifier.h>
#include <linux/skbuff.h>
97
#include <linux/bpf.h>
98
#include <net/net_namespace.h>
Linus Torvalds's avatar
Linus Torvalds committed
99
#include <net/sock.h>
Eric Dumazet's avatar
Eric Dumazet committed
100
#include <net/busy_poll.h>
Linus Torvalds's avatar
Linus Torvalds committed
101 102 103
#include <linux/rtnetlink.h>
#include <linux/stat.h>
#include <net/dst.h>
104
#include <net/dst_metadata.h>
Linus Torvalds's avatar
Linus Torvalds committed
105 106
#include <net/pkt_sched.h>
#include <net/checksum.h>
107
#include <net/xfrm.h>
Linus Torvalds's avatar
Linus Torvalds committed
108 109 110 111 112 113 114 115
#include <linux/highmem.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/netpoll.h>
#include <linux/rcupdate.h>
#include <linux/delay.h>
#include <net/iw_handler.h>
#include <asm/current.h>
Steve Grubb's avatar
Steve Grubb committed
116
#include <linux/audit.h>
117
#include <linux/dmaengine.h>
118
#include <linux/err.h>
119
#include <linux/ctype.h>
120
#include <linux/if_arp.h>
121
#include <linux/if_vlan.h>
122
#include <linux/ip.h>
123
#include <net/ip.h>
124
#include <net/mpls.h>
125 126
#include <linux/ipv6.h>
#include <linux/in.h>
127 128
#include <linux/jhash.h>
#include <linux/random.h>
129
#include <trace/events/napi.h>
130
#include <trace/events/net.h>
131
#include <trace/events/skb.h>
132
#include <linux/pci.h>
133
#include <linux/inetdevice.h>
134
#include <linux/cpu_rmap.h>
135
#include <linux/static_key.h>
Eliezer Tamir's avatar
Eliezer Tamir committed
136
#include <linux/hashtable.h>
137
#include <linux/vmalloc.h>
138
#include <linux/if_macvlan.h>
139
#include <linux/errqueue.h>
140
#include <linux/hrtimer.h>
141
#include <linux/netfilter_ingress.h>
142
#include <linux/sctp.h>
143
#include <linux/crash_dump.h>
Linus Torvalds's avatar
Linus Torvalds committed
144

145 146
#include "net-sysfs.h"

147 148 149
/* Instead of increasing this, you should create a hash table. */
#define MAX_GRO_SKBS 8

Herbert Xu's avatar
Herbert Xu committed
150 151 152
/* This should be increased if a protocol with a bigger head is added. */
#define GRO_MAX_HEAD (MAX_HEADER + 128)

Linus Torvalds's avatar
Linus Torvalds committed
153
static DEFINE_SPINLOCK(ptype_lock);
154
static DEFINE_SPINLOCK(offload_lock);
155 156
struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
struct list_head ptype_all __read_mostly;	/* Taps */
157
static struct list_head offload_base __read_mostly;
Linus Torvalds's avatar
Linus Torvalds committed
158

159
static int netif_rx_internal(struct sk_buff *skb);
160 161 162
static int call_netdevice_notifiers_info(unsigned long val,
					 struct net_device *dev,
					 struct netdev_notifier_info *info);
163

Linus Torvalds's avatar
Linus Torvalds committed
164
/*
165
 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
Linus Torvalds's avatar
Linus Torvalds committed
166 167
 * semaphore.
 *
168
 * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
Linus Torvalds's avatar
Linus Torvalds committed
169 170
 *
 * Writers must hold the rtnl semaphore while they loop through the
171
 * dev_base_head list, and hold dev_base_lock for writing when they do the
Linus Torvalds's avatar
Linus Torvalds committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185
 * actual updates.  This allows pure readers to access the list even
 * while a writer is preparing to update it.
 *
 * To put it another way, dev_base_lock is held for writing only to
 * protect against pure readers; the rtnl semaphore provides the
 * protection against other writers.
 *
 * See, for example usages, register_netdevice() and
 * unregister_netdevice(), which must be called with the rtnl
 * semaphore held.
 */
DEFINE_RWLOCK(dev_base_lock);
EXPORT_SYMBOL(dev_base_lock);

Eliezer Tamir's avatar
Eliezer Tamir committed
186 187 188
/* protects napi_hash addition/deletion and napi_gen_id */
static DEFINE_SPINLOCK(napi_hash_lock);

189
static unsigned int napi_gen_id = NR_CPUS;
190
static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8);
Eliezer Tamir's avatar
Eliezer Tamir committed
191

192
static seqcount_t devnet_rename_seq;
193

194 195 196 197 198
static inline void dev_base_seq_inc(struct net *net)
{
	while (++net->dev_base_seq == 0);
}

199
static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
Linus Torvalds's avatar
Linus Torvalds committed
200
{
201
	unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ));
202

203
	return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
Linus Torvalds's avatar
Linus Torvalds committed
204 205
}

206
static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
Linus Torvalds's avatar
Linus Torvalds committed
207
{
208
	return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
Linus Torvalds's avatar
Linus Torvalds committed
209 210
}

Eric Dumazet's avatar
Eric Dumazet committed
211
static inline void rps_lock(struct softnet_data *sd)
212 213
{
#ifdef CONFIG_RPS
Eric Dumazet's avatar
Eric Dumazet committed
214
	spin_lock(&sd->input_pkt_queue.lock);
215 216 217
#endif
}

Eric Dumazet's avatar
Eric Dumazet committed
218
static inline void rps_unlock(struct softnet_data *sd)
219 220
{
#ifdef CONFIG_RPS
Eric Dumazet's avatar
Eric Dumazet committed
221
	spin_unlock(&sd->input_pkt_queue.lock);
222 223 224
#endif
}

225
/* Device list insertion */
226
static void list_netdevice(struct net_device *dev)
227
{
228
	struct net *net = dev_net(dev);
229 230 231 232

	ASSERT_RTNL();

	write_lock_bh(&dev_base_lock);
233
	list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
234
	hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
235 236
	hlist_add_head_rcu(&dev->index_hlist,
			   dev_index_hash(net, dev->ifindex));
237
	write_unlock_bh(&dev_base_lock);
238 239

	dev_base_seq_inc(net);
240 241
}

242 243 244
/* Device list removal
 * caller must respect a RCU grace period before freeing/reusing dev
 */
245 246 247 248 249 250
static void unlist_netdevice(struct net_device *dev)
{
	ASSERT_RTNL();

	/* Unlink dev from the device chain */
	write_lock_bh(&dev_base_lock);
251
	list_del_rcu(&dev->dev_list);
252
	hlist_del_rcu(&dev->name_hlist);
253
	hlist_del_rcu(&dev->index_hlist);
254
	write_unlock_bh(&dev_base_lock);
255 256

	dev_base_seq_inc(dev_net(dev));
257 258
}

Linus Torvalds's avatar
Linus Torvalds committed
259 260 261 262
/*
 *	Our notifier list
 */

263
static RAW_NOTIFIER_HEAD(netdev_chain);
Linus Torvalds's avatar
Linus Torvalds committed
264 265 266 267 268

/*
 *	Device drivers call our routines to queue packets here. We empty the
 *	queue in the local softnet handler.
 */
269

270
DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
Eric Dumazet's avatar
Eric Dumazet committed
271
EXPORT_PER_CPU_SYMBOL(softnet_data);
Linus Torvalds's avatar
Linus Torvalds committed
272

273
#ifdef CONFIG_LOCKDEP
274
/*
275
 * register_netdevice() inits txq->_xmit_lock and sets lockdep class
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
 * according to dev->type
 */
static const unsigned short netdev_lock_type[] =
	{ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
	 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
	 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
	 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
	 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
	 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
	 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
	 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
	 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
	 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
	 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
	 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
291 292 293
	 ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM,
	 ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE,
	 ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE};
294

295
static const char *const netdev_lock_name[] =
296 297 298 299 300 301 302 303 304 305 306 307
	{"_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
	 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
	 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
	 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
	 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
	 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
	 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
	 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
	 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
	 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
	 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
	 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
308 309 310
	 "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM",
	 "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE",
	 "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"};
311 312

static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
313
static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
314 315 316 317 318 319 320 321 322 323 324 325

static inline unsigned short netdev_lock_pos(unsigned short dev_type)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
		if (netdev_lock_type[i] == dev_type)
			return i;
	/* the last key is used by default */
	return ARRAY_SIZE(netdev_lock_type) - 1;
}

326 327
static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
						 unsigned short dev_type)
328 329 330 331 332 333 334
{
	int i;

	i = netdev_lock_pos(dev_type);
	lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
				   netdev_lock_name[i]);
}
335 336 337 338 339 340 341 342 343 344

static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
{
	int i;

	i = netdev_lock_pos(dev->type);
	lockdep_set_class_and_name(&dev->addr_list_lock,
				   &netdev_addr_lock_key[i],
				   netdev_lock_name[i]);
}
345
#else
346 347 348 349 350
static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
						 unsigned short dev_type)
{
}
static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
351 352 353
{
}
#endif
Linus Torvalds's avatar
Linus Torvalds committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376

/*******************************************************************************

		Protocol management and registration routines

*******************************************************************************/

/*
 *	Add a protocol ID to the list. Now that the input handler is
 *	smarter we can dispense with all the messy stuff that used to be
 *	here.
 *
 *	BEWARE!!! Protocol handlers, mangling input packets,
 *	MUST BE last in hash buckets and checking protocol handlers
 *	MUST start from promiscuous ptype_all chain in net_bh.
 *	It is true now, do not change it.
 *	Explanation follows: if protocol handler, mangling packet, will
 *	be the first on list, it is not able to sense, that packet
 *	is cloned and should be copied-on-write, so that it will
 *	change it and subsequent readers will get broken packet.
 *							--ANK (980803)
 */

377 378 379
static inline struct list_head *ptype_head(const struct packet_type *pt)
{
	if (pt->type == htons(ETH_P_ALL))
380
		return pt->dev ? &pt->dev->ptype_all : &ptype_all;
381
	else
382 383
		return pt->dev ? &pt->dev->ptype_specific :
				 &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
384 385
}

Linus Torvalds's avatar
Linus Torvalds committed
386 387 388 389 390 391 392 393
/**
 *	dev_add_pack - add packet handler
 *	@pt: packet type declaration
 *
 *	Add a protocol handler to the networking stack. The passed &packet_type
 *	is linked into kernel lists and may not be freed until it has been
 *	removed from the kernel lists.
 *
394
 *	This call does not sleep therefore it can not
Linus Torvalds's avatar
Linus Torvalds committed
395 396 397 398 399 400
 *	guarantee all CPU's that are in middle of receiving packets
 *	will see the new packet type (until the next received packet).
 */

void dev_add_pack(struct packet_type *pt)
{
401
	struct list_head *head = ptype_head(pt);
Linus Torvalds's avatar
Linus Torvalds committed
402

403 404 405
	spin_lock(&ptype_lock);
	list_add_rcu(&pt->list, head);
	spin_unlock(&ptype_lock);
Linus Torvalds's avatar
Linus Torvalds committed
406
}
Eric Dumazet's avatar
Eric Dumazet committed
407
EXPORT_SYMBOL(dev_add_pack);
Linus Torvalds's avatar
Linus Torvalds committed
408 409 410 411 412 413 414 415

/**
 *	__dev_remove_pack	 - remove packet handler
 *	@pt: packet type declaration
 *
 *	Remove a protocol handler that was previously added to the kernel
 *	protocol handlers by dev_add_pack(). The passed &packet_type is removed
 *	from the kernel lists and can be freed or reused once this function
416
 *	returns.
Linus Torvalds's avatar
Linus Torvalds committed
417 418 419 420 421 422 423
 *
 *      The packet type might still be in use by receivers
 *	and must not be freed until after all the CPU's have gone
 *	through a quiescent state.
 */
void __dev_remove_pack(struct packet_type *pt)
{
424
	struct list_head *head = ptype_head(pt);
Linus Torvalds's avatar
Linus Torvalds committed
425 426
	struct packet_type *pt1;

427
	spin_lock(&ptype_lock);
Linus Torvalds's avatar
Linus Torvalds committed
428 429 430 431 432 433 434 435

	list_for_each_entry(pt1, head, list) {
		if (pt == pt1) {
			list_del_rcu(&pt->list);
			goto out;
		}
	}

436
	pr_warn("dev_remove_pack: %p not found\n", pt);
Linus Torvalds's avatar
Linus Torvalds committed
437
out:
438
	spin_unlock(&ptype_lock);
Linus Torvalds's avatar
Linus Torvalds committed
439
}
Eric Dumazet's avatar
Eric Dumazet committed
440 441
EXPORT_SYMBOL(__dev_remove_pack);

Linus Torvalds's avatar
Linus Torvalds committed
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
/**
 *	dev_remove_pack	 - remove packet handler
 *	@pt: packet type declaration
 *
 *	Remove a protocol handler that was previously added to the kernel
 *	protocol handlers by dev_add_pack(). The passed &packet_type is removed
 *	from the kernel lists and can be freed or reused once this function
 *	returns.
 *
 *	This call sleeps to guarantee that no CPU is looking at the packet
 *	type after return.
 */
void dev_remove_pack(struct packet_type *pt)
{
	__dev_remove_pack(pt);
457

Linus Torvalds's avatar
Linus Torvalds committed
458 459
	synchronize_net();
}
Eric Dumazet's avatar
Eric Dumazet committed
460
EXPORT_SYMBOL(dev_remove_pack);
Linus Torvalds's avatar
Linus Torvalds committed
461

462 463 464 465 466 467 468 469 470 471 472 473 474 475 476

/**
 *	dev_add_offload - register offload handlers
 *	@po: protocol offload declaration
 *
 *	Add protocol offload handlers to the networking stack. The passed
 *	&proto_offload is linked into kernel lists and may not be freed until
 *	it has been removed from the kernel lists.
 *
 *	This call does not sleep therefore it can not
 *	guarantee all CPU's that are in middle of receiving packets
 *	will see the new offload handlers (until the next received packet).
 */
void dev_add_offload(struct packet_offload *po)
{
477
	struct packet_offload *elem;
478 479

	spin_lock(&offload_lock);
480 481 482 483 484
	list_for_each_entry(elem, &offload_base, list) {
		if (po->priority < elem->priority)
			break;
	}
	list_add_rcu(&po->list, elem->list.prev);
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
	spin_unlock(&offload_lock);
}
EXPORT_SYMBOL(dev_add_offload);

/**
 *	__dev_remove_offload	 - remove offload handler
 *	@po: packet offload declaration
 *
 *	Remove a protocol offload handler that was previously added to the
 *	kernel offload handlers by dev_add_offload(). The passed &offload_type
 *	is removed from the kernel lists and can be freed or reused once this
 *	function returns.
 *
 *      The packet type might still be in use by receivers
 *	and must not be freed until after all the CPU's have gone
 *	through a quiescent state.
 */
502
static void __dev_remove_offload(struct packet_offload *po)
503 504 505 506
{
	struct list_head *head = &offload_base;
	struct packet_offload *po1;

507
	spin_lock(&offload_lock);
508 509 510 511 512 513 514 515 516 517

	list_for_each_entry(po1, head, list) {
		if (po == po1) {
			list_del_rcu(&po->list);
			goto out;
		}
	}

	pr_warn("dev_remove_offload: %p not found\n", po);
out:
518
	spin_unlock(&offload_lock);
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
}

/**
 *	dev_remove_offload	 - remove packet offload handler
 *	@po: packet offload declaration
 *
 *	Remove a packet offload handler that was previously added to the kernel
 *	offload handlers by dev_add_offload(). The passed &offload_type is
 *	removed from the kernel lists and can be freed or reused once this
 *	function returns.
 *
 *	This call sleeps to guarantee that no CPU is looking at the packet
 *	type after return.
 */
void dev_remove_offload(struct packet_offload *po)
{
	__dev_remove_offload(po);

	synchronize_net();
}
EXPORT_SYMBOL(dev_remove_offload);

Linus Torvalds's avatar
Linus Torvalds committed
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
/******************************************************************************

		      Device Boot-time Settings Routines

*******************************************************************************/

/* Boot time configuration table */
static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];

/**
 *	netdev_boot_setup_add	- add new setup entry
 *	@name: name of the device
 *	@map: configured settings for the device
 *
 *	Adds new setup entry to the dev_boot_setup list.  The function
 *	returns 0 on error and 1 on success.  This is a generic routine to
 *	all netdevices.
 */
static int netdev_boot_setup_add(char *name, struct ifmap *map)
{
	struct netdev_boot_setup *s;
	int i;

	s = dev_boot_setup;
	for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
		if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
			memset(s[i].name, 0, sizeof(s[i].name));
568
			strlcpy(s[i].name, name, IFNAMSIZ);
Linus Torvalds's avatar
Linus Torvalds committed
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
			memcpy(&s[i].map, map, sizeof(s[i].map));
			break;
		}
	}

	return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
}

/**
 *	netdev_boot_setup_check	- check boot time settings
 *	@dev: the netdevice
 *
 * 	Check boot time settings for the device.
 *	The found settings are set for the device to be used
 *	later in the device probing.
 *	Returns 0 if no settings found, 1 if they are.
 */
int netdev_boot_setup_check(struct net_device *dev)
{
	struct netdev_boot_setup *s = dev_boot_setup;
	int i;

	for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
		if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
593
		    !strcmp(dev->name, s[i].name)) {
Linus Torvalds's avatar
Linus Torvalds committed
594 595 596 597 598 599 600 601 602
			dev->irq 	= s[i].map.irq;
			dev->base_addr 	= s[i].map.base_addr;
			dev->mem_start 	= s[i].map.mem_start;
			dev->mem_end 	= s[i].map.mem_end;
			return 1;
		}
	}
	return 0;
}
Eric Dumazet's avatar
Eric Dumazet committed
603
EXPORT_SYMBOL(netdev_boot_setup_check);
Linus Torvalds's avatar
Linus Torvalds committed
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627


/**
 *	netdev_boot_base	- get address from boot time settings
 *	@prefix: prefix for network device
 *	@unit: id for network device
 *
 * 	Check boot time settings for the base address of device.
 *	The found settings are set for the device to be used
 *	later in the device probing.
 *	Returns 0 if no settings found.
 */
unsigned long netdev_boot_base(const char *prefix, int unit)
{
	const struct netdev_boot_setup *s = dev_boot_setup;
	char name[IFNAMSIZ];
	int i;

	sprintf(name, "%s%d", prefix, unit);

	/*
	 * If device already registered then return base of 1
	 * to indicate not to probe for this interface
	 */
628
	if (__dev_get_by_name(&init_net, name))
Linus Torvalds's avatar
Linus Torvalds committed
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
		return 1;

	for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
		if (!strcmp(name, s[i].name))
			return s[i].map.base_addr;
	return 0;
}

/*
 * Saves at boot time configured settings for any netdevice.
 */
int __init netdev_boot_setup(char *str)
{
	int ints[5];
	struct ifmap map;

	str = get_options(str, ARRAY_SIZE(ints), ints);
	if (!str || !*str)
		return 0;

	/* Save settings */
	memset(&map, 0, sizeof(map));
	if (ints[0] > 0)
		map.irq = ints[1];
	if (ints[0] > 1)
		map.base_addr = ints[2];
	if (ints[0] > 2)
		map.mem_start = ints[3];
	if (ints[0] > 3)
		map.mem_end = ints[4];

	/* Add new entry to the list */
	return netdev_boot_setup_add(str, &map);
}

__setup("netdev=", netdev_boot_setup);

/*******************************************************************************

			    Device Interface Subroutines

*******************************************************************************/

672 673 674 675 676 677 678 679 680 681 682 683 684
/**
 *	dev_get_iflink	- get 'iflink' value of a interface
 *	@dev: targeted interface
 *
 *	Indicates the ifindex the interface is linked to.
 *	Physical interfaces have the same 'ifindex' and 'iflink' values.
 */

int dev_get_iflink(const struct net_device *dev)
{
	if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink)
		return dev->netdev_ops->ndo_get_iflink(dev);

685
	return dev->ifindex;
686 687 688
}
EXPORT_SYMBOL(dev_get_iflink);

689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
/**
 *	dev_fill_metadata_dst - Retrieve tunnel egress information.
 *	@dev: targeted interface
 *	@skb: The packet.
 *
 *	For better visibility of tunnel traffic OVS needs to retrieve
 *	egress tunnel information for a packet. Following API allows
 *	user to get this info.
 */
int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
{
	struct ip_tunnel_info *info;

	if (!dev->netdev_ops  || !dev->netdev_ops->ndo_fill_metadata_dst)
		return -EINVAL;

	info = skb_tunnel_info_unclone(skb);
	if (!info)
		return -ENOMEM;
	if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX)))
		return -EINVAL;

	return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb);
}
EXPORT_SYMBOL_GPL(dev_fill_metadata_dst);

Linus Torvalds's avatar
Linus Torvalds committed
715 716
/**
 *	__dev_get_by_name	- find a device by its name
717
 *	@net: the applicable net namespace
Linus Torvalds's avatar
Linus Torvalds committed
718 719 720 721 722 723 724 725 726
 *	@name: name to find
 *
 *	Find an interface by name. Must be called under RTNL semaphore
 *	or @dev_base_lock. If the name is found a pointer to the device
 *	is returned. If the name is not found then %NULL is returned. The
 *	reference counters are not incremented so the caller must be
 *	careful with locks.
 */

727
struct net_device *__dev_get_by_name(struct net *net, const char *name)
Linus Torvalds's avatar
Linus Torvalds committed
728
{
729 730
	struct net_device *dev;
	struct hlist_head *head = dev_name_hash(net, name);
Linus Torvalds's avatar
Linus Torvalds committed
731

732
	hlist_for_each_entry(dev, head, name_hlist)
Linus Torvalds's avatar
Linus Torvalds committed
733 734
		if (!strncmp(dev->name, name, IFNAMSIZ))
			return dev;
735

Linus Torvalds's avatar
Linus Torvalds committed
736 737
	return NULL;
}
Eric Dumazet's avatar
Eric Dumazet committed
738
EXPORT_SYMBOL(__dev_get_by_name);
Linus Torvalds's avatar
Linus Torvalds committed
739

740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
/**
 *	dev_get_by_name_rcu	- find a device by its name
 *	@net: the applicable net namespace
 *	@name: name to find
 *
 *	Find an interface by name.
 *	If the name is found a pointer to the device is returned.
 * 	If the name is not found then %NULL is returned.
 *	The reference counters are not incremented so the caller must be
 *	careful with locks. The caller must hold RCU lock.
 */

struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
{
	struct net_device *dev;
	struct hlist_head *head = dev_name_hash(net, name);

757
	hlist_for_each_entry_rcu(dev, head, name_hlist)
758 759 760 761 762 763 764
		if (!strncmp(dev->name, name, IFNAMSIZ))
			return dev;

	return NULL;
}
EXPORT_SYMBOL(dev_get_by_name_rcu);

Linus Torvalds's avatar
Linus Torvalds committed
765 766
/**
 *	dev_get_by_name		- find a device by its name
767
 *	@net: the applicable net namespace
Linus Torvalds's avatar
Linus Torvalds committed
768 769 770 771 772 773 774 775 776
 *	@name: name to find
 *
 *	Find an interface by name. This can be called from any
 *	context and does its own locking. The returned handle has
 *	the usage count incremented and the caller must use dev_put() to
 *	release it when it is no longer needed. %NULL is returned if no
 *	matching device is found.
 */

777
struct net_device *dev_get_by_name(struct net *net, const char *name)
Linus Torvalds's avatar
Linus Torvalds committed
778 779 780
{
	struct net_device *dev;

781 782
	rcu_read_lock();
	dev = dev_get_by_name_rcu(net, name);
Linus Torvalds's avatar
Linus Torvalds committed
783 784
	if (dev)
		dev_hold(dev);
785
	rcu_read_unlock();
Linus Torvalds's avatar
Linus Torvalds committed
786 787
	return dev;
}
Eric Dumazet's avatar
Eric Dumazet committed
788
EXPORT_SYMBOL(dev_get_by_name);
Linus Torvalds's avatar
Linus Torvalds committed
789 790 791

/**
 *	__dev_get_by_index - find a device by its ifindex
792
 *	@net: the applicable net namespace
Linus Torvalds's avatar
Linus Torvalds committed
793 794 795 796 797 798 799 800 801
 *	@ifindex: index of device
 *
 *	Search for an interface by index. Returns %NULL if the device
 *	is not found or a pointer to the device. The device has not
 *	had its reference counter increased so the caller must be careful
 *	about locking. The caller must hold either the RTNL semaphore
 *	or @dev_base_lock.
 */

802
struct net_device *__dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds's avatar
Linus Torvalds committed
803
{
804 805
	struct net_device *dev;
	struct hlist_head *head = dev_index_hash(net, ifindex);
Linus Torvalds's avatar
Linus Torvalds committed
806

807
	hlist_for_each_entry(dev, head, index_hlist)
Linus Torvalds's avatar
Linus Torvalds committed
808 809
		if (dev->ifindex == ifindex)
			return dev;
810

Linus Torvalds's avatar
Linus Torvalds committed
811 812
	return NULL;
}
Eric Dumazet's avatar
Eric Dumazet committed
813
EXPORT_SYMBOL(__dev_get_by_index);
Linus Torvalds's avatar
Linus Torvalds committed
814

815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
/**
 *	dev_get_by_index_rcu - find a device by its ifindex
 *	@net: the applicable net namespace
 *	@ifindex: index of device
 *
 *	Search for an interface by index. Returns %NULL if the device
 *	is not found or a pointer to the device. The device has not
 *	had its reference counter increased so the caller must be careful
 *	about locking. The caller must hold RCU lock.
 */

struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
{
	struct net_device *dev;
	struct hlist_head *head = dev_index_hash(net, ifindex);

831
	hlist_for_each_entry_rcu(dev, head, index_hlist)
832 833 834 835 836 837 838
		if (dev->ifindex == ifindex)
			return dev;

	return NULL;
}
EXPORT_SYMBOL(dev_get_by_index_rcu);

Linus Torvalds's avatar
Linus Torvalds committed
839 840 841

/**
 *	dev_get_by_index - find a device by its ifindex
842
 *	@net: the applicable net namespace
Linus Torvalds's avatar
Linus Torvalds committed
843 844 845 846 847 848 849 850
 *	@ifindex: index of device
 *
 *	Search for an interface by index. Returns NULL if the device
 *	is not found or a pointer to the device. The device returned has
 *	had a reference added and the pointer is safe until the user calls
 *	dev_put to indicate they have finished with it.
 */

851
struct net_device *dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds's avatar
Linus Torvalds committed
852 853 854
{
	struct net_device *dev;

855 856
	rcu_read_lock();
	dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds's avatar
Linus Torvalds committed
857 858
	if (dev)
		dev_hold(dev);
859
	rcu_read_unlock();
Linus Torvalds's avatar
Linus Torvalds committed
860 861
	return dev;
}
Eric Dumazet's avatar
Eric Dumazet committed
862
EXPORT_SYMBOL(dev_get_by_index);
Linus Torvalds's avatar
Linus Torvalds committed
863

864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
/**
 *	netdev_get_name - get a netdevice name, knowing its ifindex.
 *	@net: network namespace
 *	@name: a pointer to the buffer where the name will be stored.
 *	@ifindex: the ifindex of the interface to get the name from.
 *
 *	The use of raw_seqcount_begin() and cond_resched() before
 *	retrying is required as we want to give the writers a chance
 *	to complete when CONFIG_PREEMPT is not set.
 */
int netdev_get_name(struct net *net, char *name, int ifindex)
{
	struct net_device *dev;
	unsigned int seq;

retry:
	seq = raw_seqcount_begin(&devnet_rename_seq);
	rcu_read_lock();
	dev = dev_get_by_index_rcu(net, ifindex);
	if (!dev) {
		rcu_read_unlock();
		return -ENODEV;
	}

	strcpy(name, dev->name);
	rcu_read_unlock();
	if (read_seqcount_retry(&devnet_rename_seq, seq)) {
		cond_resched();
		goto retry;
	}

	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
898
/**
899
 *	dev_getbyhwaddr_rcu - find a device by its hardware address
900
 *	@net: the applicable net namespace
Linus Torvalds's avatar
Linus Torvalds committed
901 902 903 904
 *	@type: media type of device
 *	@ha: hardware address
 *
 *	Search for an interface by MAC address. Returns NULL if the device
905 906
 *	is not found or a pointer to the device.
 *	The caller must hold RCU or RTNL.
907
 *	The returned device has not had its ref count increased
Linus Torvalds's avatar
Linus Torvalds committed
908 909 910 911
 *	and the caller must therefore be careful about locking
 *
 */

912 913
struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
				       const char *ha)
Linus Torvalds's avatar
Linus Torvalds committed
914 915 916
{
	struct net_device *dev;

917
	for_each_netdev_rcu(net, dev)
Linus Torvalds's avatar
Linus Torvalds committed
918 919
		if (dev->type == type &&
		    !memcmp(dev->dev_addr, ha, dev->addr_len))
920 921 922
			return dev;

	return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
923
}
924
EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
925

926
struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
Linus Torvalds's avatar
Linus Torvalds committed
927 928 929
{
	struct net_device *dev;

930
	ASSERT_RTNL();
931
	for_each_netdev(net, dev)
932
		if (dev->type == type)
933 934 935
			return dev;

	return NULL;
936 937 938
}
EXPORT_SYMBOL(__dev_getfirstbyhwtype);

939
struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
940
{
941
	struct net_device *dev, *ret = NULL;
942

943 944 945 946 947 948 949 950 951
	rcu_read_lock();
	for_each_netdev_rcu(net, dev)
		if (dev->type == type) {
			dev_hold(dev);
			ret = dev;
			break;
		}
	rcu_read_unlock();
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
952 953 954 955
}
EXPORT_SYMBOL(dev_getfirstbyhwtype);

/**
956
 *	__dev_get_by_flags - find any device with given flags
957
 *	@net: the applicable net namespace
Linus Torvalds's avatar
Linus Torvalds committed
958 959 960 961
 *	@if_flags: IFF_* values
 *	@mask: bitmask of bits in if_flags to check
 *
 *	Search for any interface with the given flags. Returns NULL if a device
Eric Dumazet's avatar
Eric Dumazet committed
962
 *	is not found or a pointer to the device. Must be called inside
963
 *	rtnl_lock(), and result refcount is unchanged.
Linus Torvalds's avatar
Linus Torvalds committed
964 965
 */

966 967
struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags,
				      unsigned short mask)
Linus Torvalds's avatar
Linus Torvalds committed
968
{
969
	struct net_device *dev, *ret;
Linus Torvalds's avatar
Linus Torvalds committed
970

971 972
	ASSERT_RTNL();

973
	ret = NULL;
974
	for_each_netdev(net, dev) {
Linus Torvalds's avatar
Linus Torvalds committed
975
		if (((dev->flags ^ if_flags) & mask) == 0) {
976
			ret = dev;
Linus Torvalds's avatar
Linus Torvalds committed
977 978 979
			break;
		}
	}
980
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
981
}
982
EXPORT_SYMBOL(__dev_get_by_flags);
Linus Torvalds's avatar
Linus Torvalds committed
983 984 985 986 987 988

/**
 *	dev_valid_name - check if name is okay for network device
 *	@name: name string
 *
 *	Network device names need to be valid file names to
989 990
 *	to allow sysfs to work.  We also disallow any kind of
 *	whitespace.
Linus Torvalds's avatar
Linus Torvalds committed
991
 */
992
bool dev_valid_name(const char *name)
Linus Torvalds's avatar
Linus Torvalds committed
993
{
994
	if (*name == '\0')
995
		return false;
996
	if (strlen(name) >= IFNAMSIZ)
997
		return false;
998
	if (!strcmp(name, ".") || !strcmp(name, ".."))
999
		return false;
1000 1001

	while (*name) {
1002
		if (*name == '/' || *name == ':' || isspace(*name))
1003
			return false;
1004 1005
		name++;
	}
1006
	return true;
Linus Torvalds's avatar
Linus Torvalds committed
1007
}
Eric Dumazet's avatar
Eric Dumazet committed
1008
EXPORT_SYMBOL(dev_valid_name);
Linus Torvalds's avatar
Linus Torvalds committed
1009 1010

/**
1011 1012
 *	__dev_alloc_name - allocate a name for a device
 *	@net: network namespace to allocate the device name in
Linus Torvalds's avatar
Linus Torvalds committed
1013
 *	@name: name format string
1014
 *	@buf:  scratch buffer and result name string
Linus Torvalds's avatar
Linus Torvalds committed
1015 1016
 *
 *	Passed a format string - eg "lt%d" it will try and find a suitable
1017 1018 1019 1020 1021 1022
 *	id. It scans list of devices to build up a free map, then chooses
 *	the first empty slot. The caller must hold the dev_base or rtnl lock
 *	while allocating the name and adding the device in order to avoid
 *	duplicates.
 *	Limited to bits_per_byte * page size devices (ie 32K on most platforms).
 *	Returns the number of the unit assigned or a negative errno code.
Linus Torvalds's avatar
Linus Torvalds committed
1023 1024
 */

1025
static int __dev_alloc_name(struct net *net, const char *name, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
1026 1027 1028 1029
{
	int i = 0;
	const char *p;
	const int max_netdevices = 8*PAGE_SIZE;
1030
	unsigned long *inuse;
Linus Torvalds's avatar
Linus Torvalds committed
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
	struct net_device *d;

	p = strnchr(name, IFNAMSIZ-1, '%');
	if (p) {
		/*
		 * Verify the string as this thing may have come from
		 * the user.  There must be either one "%d" and no other "%"
		 * characters.
		 */
		if (p[1] != 'd' || strchr(p + 2, '%'))
			return -EINVAL;

		/* Use one page as a bit array of possible slots */
1044
		inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
1045 1046 1047
		if (!inuse)
			return -ENOMEM;

1048
		for_each_netdev(net, d) {
Linus Torvalds's avatar
Linus Torvalds committed
1049 1050 1051 1052 1053 1054
			if (!sscanf(d->name, name, &i))
				continue;
			if (i < 0 || i >= max_netdevices)
				continue;

			/*  avoid cases where sscanf is not exact inverse of printf */
1055
			snprintf(buf, IFNAMSIZ, name, i);
Linus Torvalds's avatar
Linus Torvalds committed
1056 1057 1058 1059 1060 1061 1062 1063
			if (!strncmp(buf, d->name, IFNAMSIZ))
				set_bit(i, inuse);
		}

		i = find_first_zero_bit(inuse, max_netdevices);
		free_page((unsigned long) inuse);
	}

1064 1065
	if (buf != name)
		snprintf(buf, IFNAMSIZ, name, i);
1066
	if (!__dev_get_by_name(net, buf))
Linus Torvalds's avatar
Linus Torvalds committed
1067 1068 1069 1070 1071 1072 1073 1074 1075
		return i;

	/* It is possible to run out of possible slots
	 * when the name is long and there isn't enough space left
	 * for the digits, or if all bits are used.
	 */
	return -ENFILE;
}

1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
/**
 *	dev_alloc_name - allocate a name for a device
 *	@dev: device
 *	@name: name format string
 *
 *	Passed a format string - eg "lt%d" it will try and find a suitable
 *	id. It scans list of devices to build up a free map, then chooses
 *	the first empty slot. The caller must hold the dev_base or rtnl lock
 *	while allocating the name and adding the device in order to avoid
 *	duplicates.
 *	Limited to bits_per_byte * page size devices (ie 32K on most platforms).
 *	Returns the number of the unit assigned or a negative errno code.
 */

int dev_alloc_name(struct net_device *dev, const char *name)
{
	char buf[IFNAMSIZ];
	struct net *net;
	int ret;

1096 1097
	BUG_ON(!dev_net(dev));
	net = dev_net(dev);
1098 1099 1100 1101 1102
	ret = __dev_alloc_name(net, name, buf);
	if (ret >= 0)
		strlcpy(dev->name, buf, IFNAMSIZ);
	return ret;
}
Eric Dumazet's avatar
Eric Dumazet committed
1103
EXPORT_SYMBOL(dev_alloc_name);
1104

1105 1106 1107
static int dev_alloc_name_ns(struct net *net,
			     struct net_device *dev,
			     const char *name)
1108
{
1109 1110
	char buf[IFNAMSIZ];
	int ret;
1111

1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
	ret = __dev_alloc_name(net, name, buf);
	if (ret >= 0)
		strlcpy(dev->name, buf, IFNAMSIZ);
	return ret;
}

static int dev_get_valid_name(struct net *net,
			      struct net_device *dev,
			      const char *name)
{
	BUG_ON(!net);
1123

1124 1125 1126
	if (!dev_valid_name(name))
		return -EINVAL;

1127
	if (strchr(name, '%'))
1128
		return dev_alloc_name_ns(net, dev, name);
1129 1130
	else if (__dev_get_by_name(net, name))
		return -EEXIST;
1131 1132
	else if (dev->name != name)
		strlcpy(dev->name, name, IFNAMSIZ);
1133 1134 1135

	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
1136 1137 1138 1139 1140 1141 1142 1143 1144

/**
 *	dev_change_name - change name of a device
 *	@dev: device
 *	@newname: name (or format string) must be at least IFNAMSIZ
 *
 *	Change name of a device, can pass format strings "eth%d".
 *	for wildcarding.
 */
1145
int dev_change_name(struct net_device *dev, const char *newname)
Linus Torvalds's avatar
Linus Torvalds committed
1146
{
1147
	unsigned char old_assign_type;
1148
	char oldname[IFNAMSIZ];
Linus Torvalds's avatar
Linus Torvalds committed
1149
	int err = 0;
1150
	int ret;
1151
	struct net *net;
Linus Torvalds's avatar
Linus Torvalds committed
1152 1153

	ASSERT_RTNL();
1154
	BUG_ON(!dev_net(dev));
Linus Torvalds's avatar
Linus Torvalds committed
1155

1156
	net = dev_net(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1157 1158 1159
	if (dev->flags & IFF_UP)
		return -EBUSY;

1160
	write_seqcount_begin(&devnet_rename_seq);
1161 1162

	if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
1163
		write_seqcount_end(&devnet_rename_seq);
1164
		return 0;
1165
	}
1166

1167 1168
	memcpy(oldname, dev->name, IFNAMSIZ);

1169
	err = dev_get_valid_name(net, dev, newname);
1170
	if (err < 0) {
1171
		write_seqcount_end(&devnet_rename_seq);
1172
		return err;
1173
	}
Linus Torvalds's avatar
Linus Torvalds committed
1174

1175 1176 1177
	if (oldname[0] && !strchr(oldname, '%'))
		netdev_info(dev, "renamed from %s\n", oldname);

1178 1179 1180
	old_assign_type = dev->name_assign_type;
	dev->name_assign_type = NET_NAME_RENAMED;

1181
rollback:
1182 1183 1184
	ret = device_rename(&dev->dev, dev->name);
	if (ret) {
		memcpy(dev->name, oldname, IFNAMSIZ);
1185
		dev->name_assign_type = old_assign_type;
1186
		write_seqcount_end(&devnet_rename_seq);
1187
		return ret;
1188
	}
1189

1190
	write_seqcount_end(&devnet_rename_seq);
1191

1192 1193
	netdev_adjacent_rename_links(dev, oldname);

1194
	write_lock_bh(&dev_base_lock);
1195
	hlist_del_rcu(&dev->name_hlist);
1196 1197 1198 1199 1200 1201
	write_unlock_bh(&dev_base_lock);

	synchronize_rcu();

	write_lock_bh(&dev_base_lock);
	hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
1202 1203
	write_unlock_bh(&dev_base_lock);

1204
	ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
1205 1206 1207
	ret = notifier_to_errno(ret);

	if (ret) {
1208 1209
		/* err >= 0 after dev_alloc_name() or stores the first errno */
		if (err >= 0) {
1210
			err = ret;
1211
			write_seqcount_begin(&devnet_rename_seq);
1212
			memcpy(dev->name, oldname, IFNAMSIZ);
1213
			memcpy(oldname, newname, IFNAMSIZ);
1214 1215
			dev->name_assign_type = old_assign_type;
			old_assign_type = NET_NAME_RENAMED;
1216
			goto rollback;
1217
		} else {
1218
			pr_err("%s: name change rollback failed: %d\n",
1219
			       dev->name, ret);
1220 1221
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
1222 1223 1224 1225

	return err;
}

1226 1227 1228 1229
/**
 *	dev_set_alias - change ifalias of a device
 *	@dev: device
 *	@alias: name up to IFALIASZ
1230
 *	@len: limit of bytes to copy from info
1231 1232 1233 1234 1235
 *
 *	Set ifalias for a device,
 */
int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
{
1236 1237
	char *new_ifalias;

1238 1239 1240 1241 1242
	ASSERT_RTNL();

	if (len >= IFALIASZ)
		return -EINVAL;

1243
	if (!len) {
1244 1245
		kfree(dev->ifalias);
		dev->ifalias = NULL;
1246 1247 1248
		return 0;
	}

1249 1250
	new_ifalias = krealloc(dev->ifalias, len + 1, GFP_KERNEL);
	if (!new_ifalias)
1251
		return -ENOMEM;
1252
	dev->ifalias = new_ifalias;
1253 1254 1255 1256 1257 1258

	strlcpy(dev->ifalias, alias, len+1);
	return len;
}


1259
/**
1260
 *	netdev_features_change - device changes features
1261 1262 1263 1264 1265 1266
 *	@dev: device to cause notification
 *
 *	Called to indicate a device has changed features.
 */
void netdev_features_change(struct net_device *dev)
{
1267
	call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
1268 1269 1270
}
EXPORT_SYMBOL(netdev_features_change);

Linus Torvalds's avatar
Linus Torvalds committed
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
/**
 *	netdev_state_change - device changes state
 *	@dev: device to cause notification
 *
 *	Called to indicate a device has changed state. This function calls
 *	the notifier chains for netdev_chain and sends a NEWLINK message
 *	to the routing socket.
 */
void netdev_state_change(struct net_device *dev)
{
	if (dev->flags & IFF_UP) {
1282 1283 1284 1285 1286
		struct netdev_notifier_change_info change_info;

		change_info.flags_changed = 0;
		call_netdevice_notifiers_info(NETDEV_CHANGE, dev,
					      &change_info.info);
1287
		rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
1288 1289
	}
}
Eric Dumazet's avatar
Eric Dumazet committed
1290
EXPORT_SYMBOL(netdev_state_change);
Linus Torvalds's avatar
Linus Torvalds committed
1291

1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
/**
 * 	netdev_notify_peers - notify network peers about existence of @dev
 * 	@dev: network device
 *
 * Generate traffic such that interested network peers are aware of
 * @dev, such as by generating a gratuitous ARP. This may be used when
 * a device wants to inform the rest of the network about some sort of
 * reconfiguration such as a failover event or virtual machine
 * migration.
 */
void netdev_notify_peers(struct net_device *dev)
1303
{
1304 1305 1306
	rtnl_lock();
	call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
	rtnl_unlock();
1307
}
1308
EXPORT_SYMBOL(netdev_notify_peers);
1309

1310
static int __dev_open(struct net_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
1311
{
1312
	const struct net_device_ops *ops = dev->netdev_ops;
1313
	int ret;
Linus Torvalds's avatar
Linus Torvalds committed
1314

1315 1316
	ASSERT_RTNL();

Linus Torvalds's avatar
Linus Torvalds committed
1317 1318 1319
	if (!netif_device_present(dev))
		return -ENODEV;

1320 1321 1322 1323
	/* Block netpoll from trying to do any rx path servicing.
	 * If we don't do this there is a chance ndo_poll_controller
	 * or ndo_poll may be running while we open the device
	 */
1324
	netpoll_poll_disable(dev);
1325

1326 1327 1328 1329 1330
	ret = call_netdevice_notifiers(NETDEV_PRE_UP, dev);
	ret = notifier_to_errno(ret);
	if (ret)
		return ret;

Linus Torvalds's avatar
Linus Torvalds committed
1331
	set_bit(__LINK_STATE_START, &dev->state);
1332

1333 1334
	if (ops->ndo_validate_addr)
		ret = ops->ndo_validate_addr(dev);
1335

1336 1337
	if (!ret && ops->ndo_open)
		ret = ops->ndo_open(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1338

1339
	netpoll_poll_enable(dev);
1340

1341 1342 1343
	if (ret)
		clear_bit(__LINK_STATE_START, &dev->state);
	else {
Linus Torvalds's avatar
Linus Torvalds committed
1344
		dev->flags |= IFF_UP;
1345
		dev_set_rx_mode(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1346
		dev_activate(dev);
1347
		add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds's avatar
Linus Torvalds committed
1348
	}
1349

Linus Torvalds's avatar
Linus Torvalds committed
1350 1351 1352 1353
	return ret;
}

/**
1354 1355
 *	dev_open	- prepare an interface for use.
 *	@dev:	device to open
Linus Torvalds's avatar
Linus Torvalds committed
1356
 *
1357 1358 1359 1360 1361 1362 1363
 *	Takes a device from down to up state. The device's private open
 *	function is invoked and then the multicast lists are loaded. Finally
 *	the device is moved into the up state and a %NETDEV_UP message is
 *	sent to the netdev notifier chain.
 *
 *	Calling this function on an active interface is a nop. On a failure
 *	a negative errno code is returned.
Linus Torvalds's avatar
Linus Torvalds committed
1364
 */