• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6.36/net/core/
1/*
2 * net/core/dev_addr_lists.c - Functions for handling net device lists
3 * Copyright (c) 2010 Jiri Pirko <jpirko@redhat.com>
4 *
5 * This file contains functions for working with unicast, multicast and device
6 * addresses lists.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/netdevice.h>
15#include <linux/rtnetlink.h>
16#include <linux/list.h>
17#include <linux/proc_fs.h>
18
19/*
20 * General list handling functions
21 */
22
23static int __hw_addr_add_ex(struct netdev_hw_addr_list *list,
24			    unsigned char *addr, int addr_len,
25			    unsigned char addr_type, bool global)
26{
27	struct netdev_hw_addr *ha;
28	int alloc_size;
29
30	if (addr_len > MAX_ADDR_LEN)
31		return -EINVAL;
32
33	list_for_each_entry(ha, &list->list, list) {
34		if (!memcmp(ha->addr, addr, addr_len) &&
35		    ha->type == addr_type) {
36			if (global) {
37				/* check if addr is already used as global */
38				if (ha->global_use)
39					return 0;
40				else
41					ha->global_use = true;
42			}
43			ha->refcount++;
44			return 0;
45		}
46	}
47
48
49	alloc_size = sizeof(*ha);
50	if (alloc_size < L1_CACHE_BYTES)
51		alloc_size = L1_CACHE_BYTES;
52	ha = kmalloc(alloc_size, GFP_ATOMIC);
53	if (!ha)
54		return -ENOMEM;
55	memcpy(ha->addr, addr, addr_len);
56	ha->type = addr_type;
57	ha->refcount = 1;
58	ha->global_use = global;
59	ha->synced = false;
60	list_add_tail_rcu(&ha->list, &list->list);
61	list->count++;
62	return 0;
63}
64
65static int __hw_addr_add(struct netdev_hw_addr_list *list, unsigned char *addr,
66			 int addr_len, unsigned char addr_type)
67{
68	return __hw_addr_add_ex(list, addr, addr_len, addr_type, false);
69}
70
71static void ha_rcu_free(struct rcu_head *head)
72{
73	struct netdev_hw_addr *ha;
74
75	ha = container_of(head, struct netdev_hw_addr, rcu_head);
76	kfree(ha);
77}
78
79static int __hw_addr_del_ex(struct netdev_hw_addr_list *list,
80			    unsigned char *addr, int addr_len,
81			    unsigned char addr_type, bool global)
82{
83	struct netdev_hw_addr *ha;
84
85	list_for_each_entry(ha, &list->list, list) {
86		if (!memcmp(ha->addr, addr, addr_len) &&
87		    (ha->type == addr_type || !addr_type)) {
88			if (global) {
89				if (!ha->global_use)
90					break;
91				else
92					ha->global_use = false;
93			}
94			if (--ha->refcount)
95				return 0;
96			list_del_rcu(&ha->list);
97			call_rcu(&ha->rcu_head, ha_rcu_free);
98			list->count--;
99			return 0;
100		}
101	}
102	return -ENOENT;
103}
104
105static int __hw_addr_del(struct netdev_hw_addr_list *list, unsigned char *addr,
106			 int addr_len, unsigned char addr_type)
107{
108	return __hw_addr_del_ex(list, addr, addr_len, addr_type, false);
109}
110
111int __hw_addr_add_multiple(struct netdev_hw_addr_list *to_list,
112			   struct netdev_hw_addr_list *from_list,
113			   int addr_len, unsigned char addr_type)
114{
115	int err;
116	struct netdev_hw_addr *ha, *ha2;
117	unsigned char type;
118
119	list_for_each_entry(ha, &from_list->list, list) {
120		type = addr_type ? addr_type : ha->type;
121		err = __hw_addr_add(to_list, ha->addr, addr_len, type);
122		if (err)
123			goto unroll;
124	}
125	return 0;
126
127unroll:
128	list_for_each_entry(ha2, &from_list->list, list) {
129		if (ha2 == ha)
130			break;
131		type = addr_type ? addr_type : ha2->type;
132		__hw_addr_del(to_list, ha2->addr, addr_len, type);
133	}
134	return err;
135}
136EXPORT_SYMBOL(__hw_addr_add_multiple);
137
138void __hw_addr_del_multiple(struct netdev_hw_addr_list *to_list,
139			    struct netdev_hw_addr_list *from_list,
140			    int addr_len, unsigned char addr_type)
141{
142	struct netdev_hw_addr *ha;
143	unsigned char type;
144
145	list_for_each_entry(ha, &from_list->list, list) {
146		type = addr_type ? addr_type : ha->type;
147		__hw_addr_del(to_list, ha->addr, addr_len, addr_type);
148	}
149}
150EXPORT_SYMBOL(__hw_addr_del_multiple);
151
152int __hw_addr_sync(struct netdev_hw_addr_list *to_list,
153		   struct netdev_hw_addr_list *from_list,
154		   int addr_len)
155{
156	int err = 0;
157	struct netdev_hw_addr *ha, *tmp;
158
159	list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
160		if (!ha->synced) {
161			err = __hw_addr_add(to_list, ha->addr,
162					    addr_len, ha->type);
163			if (err)
164				break;
165			ha->synced = true;
166			ha->refcount++;
167		} else if (ha->refcount == 1) {
168			__hw_addr_del(to_list, ha->addr, addr_len, ha->type);
169			__hw_addr_del(from_list, ha->addr, addr_len, ha->type);
170		}
171	}
172	return err;
173}
174EXPORT_SYMBOL(__hw_addr_sync);
175
176void __hw_addr_unsync(struct netdev_hw_addr_list *to_list,
177		      struct netdev_hw_addr_list *from_list,
178		      int addr_len)
179{
180	struct netdev_hw_addr *ha, *tmp;
181
182	list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
183		if (ha->synced) {
184			__hw_addr_del(to_list, ha->addr,
185				      addr_len, ha->type);
186			ha->synced = false;
187			__hw_addr_del(from_list, ha->addr,
188				      addr_len, ha->type);
189		}
190	}
191}
192EXPORT_SYMBOL(__hw_addr_unsync);
193
194void __hw_addr_flush(struct netdev_hw_addr_list *list)
195{
196	struct netdev_hw_addr *ha, *tmp;
197
198	list_for_each_entry_safe(ha, tmp, &list->list, list) {
199		list_del_rcu(&ha->list);
200		call_rcu(&ha->rcu_head, ha_rcu_free);
201	}
202	list->count = 0;
203}
204EXPORT_SYMBOL(__hw_addr_flush);
205
206void __hw_addr_init(struct netdev_hw_addr_list *list)
207{
208	INIT_LIST_HEAD(&list->list);
209	list->count = 0;
210}
211EXPORT_SYMBOL(__hw_addr_init);
212
213/*
214 * Device addresses handling functions
215 */
216
217/**
218 *	dev_addr_flush - Flush device address list
219 *	@dev: device
220 *
221 *	Flush device address list and reset ->dev_addr.
222 *
223 *	The caller must hold the rtnl_mutex.
224 */
225void dev_addr_flush(struct net_device *dev)
226{
227	/* rtnl_mutex must be held here */
228
229	__hw_addr_flush(&dev->dev_addrs);
230	dev->dev_addr = NULL;
231}
232EXPORT_SYMBOL(dev_addr_flush);
233
234/**
235 *	dev_addr_init - Init device address list
236 *	@dev: device
237 *
238 *	Init device address list and create the first element,
239 *	used by ->dev_addr.
240 *
241 *	The caller must hold the rtnl_mutex.
242 */
243int dev_addr_init(struct net_device *dev)
244{
245	unsigned char addr[MAX_ADDR_LEN];
246	struct netdev_hw_addr *ha;
247	int err;
248
249	/* rtnl_mutex must be held here */
250
251	__hw_addr_init(&dev->dev_addrs);
252	memset(addr, 0, sizeof(addr));
253	err = __hw_addr_add(&dev->dev_addrs, addr, sizeof(addr),
254			    NETDEV_HW_ADDR_T_LAN);
255	if (!err) {
256		/*
257		 * Get the first (previously created) address from the list
258		 * and set dev_addr pointer to this location.
259		 */
260		ha = list_first_entry(&dev->dev_addrs.list,
261				      struct netdev_hw_addr, list);
262		dev->dev_addr = ha->addr;
263	}
264	return err;
265}
266EXPORT_SYMBOL(dev_addr_init);
267
268/**
269 *	dev_addr_add - Add a device address
270 *	@dev: device
271 *	@addr: address to add
272 *	@addr_type: address type
273 *
274 *	Add a device address to the device or increase the reference count if
275 *	it already exists.
276 *
277 *	The caller must hold the rtnl_mutex.
278 */
279int dev_addr_add(struct net_device *dev, unsigned char *addr,
280		 unsigned char addr_type)
281{
282	int err;
283
284	ASSERT_RTNL();
285
286	err = __hw_addr_add(&dev->dev_addrs, addr, dev->addr_len, addr_type);
287	if (!err)
288		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
289	return err;
290}
291EXPORT_SYMBOL(dev_addr_add);
292
293/**
294 *	dev_addr_del - Release a device address.
295 *	@dev: device
296 *	@addr: address to delete
297 *	@addr_type: address type
298 *
299 *	Release reference to a device address and remove it from the device
300 *	if the reference count drops to zero.
301 *
302 *	The caller must hold the rtnl_mutex.
303 */
304int dev_addr_del(struct net_device *dev, unsigned char *addr,
305		 unsigned char addr_type)
306{
307	int err;
308	struct netdev_hw_addr *ha;
309
310	ASSERT_RTNL();
311
312	/*
313	 * We can not remove the first address from the list because
314	 * dev->dev_addr points to that.
315	 */
316	ha = list_first_entry(&dev->dev_addrs.list,
317			      struct netdev_hw_addr, list);
318	if (ha->addr == dev->dev_addr && ha->refcount == 1)
319		return -ENOENT;
320
321	err = __hw_addr_del(&dev->dev_addrs, addr, dev->addr_len,
322			    addr_type);
323	if (!err)
324		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
325	return err;
326}
327EXPORT_SYMBOL(dev_addr_del);
328
329/**
330 *	dev_addr_add_multiple - Add device addresses from another device
331 *	@to_dev: device to which addresses will be added
332 *	@from_dev: device from which addresses will be added
333 *	@addr_type: address type - 0 means type will be used from from_dev
334 *
335 *	Add device addresses of the one device to another.
336 **
337 *	The caller must hold the rtnl_mutex.
338 */
339int dev_addr_add_multiple(struct net_device *to_dev,
340			  struct net_device *from_dev,
341			  unsigned char addr_type)
342{
343	int err;
344
345	ASSERT_RTNL();
346
347	if (from_dev->addr_len != to_dev->addr_len)
348		return -EINVAL;
349	err = __hw_addr_add_multiple(&to_dev->dev_addrs, &from_dev->dev_addrs,
350				     to_dev->addr_len, addr_type);
351	if (!err)
352		call_netdevice_notifiers(NETDEV_CHANGEADDR, to_dev);
353	return err;
354}
355EXPORT_SYMBOL(dev_addr_add_multiple);
356
357/**
358 *	dev_addr_del_multiple - Delete device addresses by another device
359 *	@to_dev: device where the addresses will be deleted
360 *	@from_dev: device by which addresses the addresses will be deleted
361 *	@addr_type: address type - 0 means type will used from from_dev
362 *
363 *	Deletes addresses in to device by the list of addresses in from device.
364 *
365 *	The caller must hold the rtnl_mutex.
366 */
367int dev_addr_del_multiple(struct net_device *to_dev,
368			  struct net_device *from_dev,
369			  unsigned char addr_type)
370{
371	ASSERT_RTNL();
372
373	if (from_dev->addr_len != to_dev->addr_len)
374		return -EINVAL;
375	__hw_addr_del_multiple(&to_dev->dev_addrs, &from_dev->dev_addrs,
376			       to_dev->addr_len, addr_type);
377	call_netdevice_notifiers(NETDEV_CHANGEADDR, to_dev);
378	return 0;
379}
380EXPORT_SYMBOL(dev_addr_del_multiple);
381
382/*
383 * Unicast list handling functions
384 */
385
386/**
387 *	dev_uc_add - Add a secondary unicast address
388 *	@dev: device
389 *	@addr: address to add
390 *
391 *	Add a secondary unicast address to the device or increase
392 *	the reference count if it already exists.
393 */
394int dev_uc_add(struct net_device *dev, unsigned char *addr)
395{
396	int err;
397
398	netif_addr_lock_bh(dev);
399	err = __hw_addr_add(&dev->uc, addr, dev->addr_len,
400			    NETDEV_HW_ADDR_T_UNICAST);
401	if (!err)
402		__dev_set_rx_mode(dev);
403	netif_addr_unlock_bh(dev);
404	return err;
405}
406EXPORT_SYMBOL(dev_uc_add);
407
408/**
409 *	dev_uc_del - Release secondary unicast address.
410 *	@dev: device
411 *	@addr: address to delete
412 *
413 *	Release reference to a secondary unicast address and remove it
414 *	from the device if the reference count drops to zero.
415 */
416int dev_uc_del(struct net_device *dev, unsigned char *addr)
417{
418	int err;
419
420	netif_addr_lock_bh(dev);
421	err = __hw_addr_del(&dev->uc, addr, dev->addr_len,
422			    NETDEV_HW_ADDR_T_UNICAST);
423	if (!err)
424		__dev_set_rx_mode(dev);
425	netif_addr_unlock_bh(dev);
426	return err;
427}
428EXPORT_SYMBOL(dev_uc_del);
429
430/**
431 *	dev_uc_sync - Synchronize device's unicast list to another device
432 *	@to: destination device
433 *	@from: source device
434 *
435 *	Add newly added addresses to the destination device and release
436 *	addresses that have no users left. The source device must be
437 *	locked by netif_tx_lock_bh.
438 *
439 *	This function is intended to be called from the dev->set_rx_mode
440 *	function of layered software devices.
441 */
442int dev_uc_sync(struct net_device *to, struct net_device *from)
443{
444	int err = 0;
445
446	if (to->addr_len != from->addr_len)
447		return -EINVAL;
448
449	netif_addr_lock_bh(to);
450	err = __hw_addr_sync(&to->uc, &from->uc, to->addr_len);
451	if (!err)
452		__dev_set_rx_mode(to);
453	netif_addr_unlock_bh(to);
454	return err;
455}
456EXPORT_SYMBOL(dev_uc_sync);
457
458/**
459 *	dev_uc_unsync - Remove synchronized addresses from the destination device
460 *	@to: destination device
461 *	@from: source device
462 *
463 *	Remove all addresses that were added to the destination device by
464 *	dev_uc_sync(). This function is intended to be called from the
465 *	dev->stop function of layered software devices.
466 */
467void dev_uc_unsync(struct net_device *to, struct net_device *from)
468{
469	if (to->addr_len != from->addr_len)
470		return;
471
472	netif_addr_lock_bh(from);
473	netif_addr_lock(to);
474	__hw_addr_unsync(&to->uc, &from->uc, to->addr_len);
475	__dev_set_rx_mode(to);
476	netif_addr_unlock(to);
477	netif_addr_unlock_bh(from);
478}
479EXPORT_SYMBOL(dev_uc_unsync);
480
481/**
482 *	dev_uc_flush - Flush unicast addresses
483 *	@dev: device
484 *
485 *	Flush unicast addresses.
486 */
487void dev_uc_flush(struct net_device *dev)
488{
489	netif_addr_lock_bh(dev);
490	__hw_addr_flush(&dev->uc);
491	netif_addr_unlock_bh(dev);
492}
493EXPORT_SYMBOL(dev_uc_flush);
494
495/**
496 *	dev_uc_flush - Init unicast address list
497 *	@dev: device
498 *
499 *	Init unicast address list.
500 */
501void dev_uc_init(struct net_device *dev)
502{
503	__hw_addr_init(&dev->uc);
504}
505EXPORT_SYMBOL(dev_uc_init);
506
507/*
508 * Multicast list handling functions
509 */
510
511static int __dev_mc_add(struct net_device *dev, unsigned char *addr,
512			bool global)
513{
514	int err;
515
516	netif_addr_lock_bh(dev);
517	err = __hw_addr_add_ex(&dev->mc, addr, dev->addr_len,
518			       NETDEV_HW_ADDR_T_MULTICAST, global);
519	if (!err)
520		__dev_set_rx_mode(dev);
521	netif_addr_unlock_bh(dev);
522	return err;
523}
524/**
525 *	dev_mc_add - Add a multicast address
526 *	@dev: device
527 *	@addr: address to add
528 *
529 *	Add a multicast address to the device or increase
530 *	the reference count if it already exists.
531 */
532int dev_mc_add(struct net_device *dev, unsigned char *addr)
533{
534	return __dev_mc_add(dev, addr, false);
535}
536EXPORT_SYMBOL(dev_mc_add);
537
538/**
539 *	dev_mc_add_global - Add a global multicast address
540 *	@dev: device
541 *	@addr: address to add
542 *
543 *	Add a global multicast address to the device.
544 */
545int dev_mc_add_global(struct net_device *dev, unsigned char *addr)
546{
547	return __dev_mc_add(dev, addr, true);
548}
549EXPORT_SYMBOL(dev_mc_add_global);
550
551static int __dev_mc_del(struct net_device *dev, unsigned char *addr,
552			bool global)
553{
554	int err;
555
556	netif_addr_lock_bh(dev);
557	err = __hw_addr_del_ex(&dev->mc, addr, dev->addr_len,
558			       NETDEV_HW_ADDR_T_MULTICAST, global);
559	if (!err)
560		__dev_set_rx_mode(dev);
561	netif_addr_unlock_bh(dev);
562	return err;
563}
564
565/**
566 *	dev_mc_del - Delete a multicast address.
567 *	@dev: device
568 *	@addr: address to delete
569 *
570 *	Release reference to a multicast address and remove it
571 *	from the device if the reference count drops to zero.
572 */
573int dev_mc_del(struct net_device *dev, unsigned char *addr)
574{
575	return __dev_mc_del(dev, addr, false);
576}
577EXPORT_SYMBOL(dev_mc_del);
578
579/**
580 *	dev_mc_del_global - Delete a global multicast address.
581 *	@dev: device
582 *	@addr: address to delete
583 *
584 *	Release reference to a multicast address and remove it
585 *	from the device if the reference count drops to zero.
586 */
587int dev_mc_del_global(struct net_device *dev, unsigned char *addr)
588{
589	return __dev_mc_del(dev, addr, true);
590}
591EXPORT_SYMBOL(dev_mc_del_global);
592
593/**
594 *	dev_mc_sync - Synchronize device's unicast list to another device
595 *	@to: destination device
596 *	@from: source device
597 *
598 *	Add newly added addresses to the destination device and release
599 *	addresses that have no users left. The source device must be
600 *	locked by netif_tx_lock_bh.
601 *
602 *	This function is intended to be called from the dev->set_multicast_list
603 *	or dev->set_rx_mode function of layered software devices.
604 */
605int dev_mc_sync(struct net_device *to, struct net_device *from)
606{
607	int err = 0;
608
609	if (to->addr_len != from->addr_len)
610		return -EINVAL;
611
612	netif_addr_lock_bh(to);
613	err = __hw_addr_sync(&to->mc, &from->mc, to->addr_len);
614	if (!err)
615		__dev_set_rx_mode(to);
616	netif_addr_unlock_bh(to);
617	return err;
618}
619EXPORT_SYMBOL(dev_mc_sync);
620
621/**
622 *	dev_mc_unsync - Remove synchronized addresses from the destination device
623 *	@to: destination device
624 *	@from: source device
625 *
626 *	Remove all addresses that were added to the destination device by
627 *	dev_mc_sync(). This function is intended to be called from the
628 *	dev->stop function of layered software devices.
629 */
630void dev_mc_unsync(struct net_device *to, struct net_device *from)
631{
632	if (to->addr_len != from->addr_len)
633		return;
634
635	netif_addr_lock_bh(from);
636	netif_addr_lock(to);
637	__hw_addr_unsync(&to->mc, &from->mc, to->addr_len);
638	__dev_set_rx_mode(to);
639	netif_addr_unlock(to);
640	netif_addr_unlock_bh(from);
641}
642EXPORT_SYMBOL(dev_mc_unsync);
643
644/**
645 *	dev_mc_flush - Flush multicast addresses
646 *	@dev: device
647 *
648 *	Flush multicast addresses.
649 */
650void dev_mc_flush(struct net_device *dev)
651{
652	netif_addr_lock_bh(dev);
653	__hw_addr_flush(&dev->mc);
654	netif_addr_unlock_bh(dev);
655}
656EXPORT_SYMBOL(dev_mc_flush);
657
658/**
659 *	dev_mc_flush - Init multicast address list
660 *	@dev: device
661 *
662 *	Init multicast address list.
663 */
664void dev_mc_init(struct net_device *dev)
665{
666	__hw_addr_init(&dev->mc);
667}
668EXPORT_SYMBOL(dev_mc_init);
669
670#ifdef CONFIG_PROC_FS
671#include <linux/seq_file.h>
672
673static int dev_mc_seq_show(struct seq_file *seq, void *v)
674{
675	struct netdev_hw_addr *ha;
676	struct net_device *dev = v;
677
678	if (v == SEQ_START_TOKEN)
679		return 0;
680
681	netif_addr_lock_bh(dev);
682	netdev_for_each_mc_addr(ha, dev) {
683		int i;
684
685		seq_printf(seq, "%-4d %-15s %-5d %-5d ", dev->ifindex,
686			   dev->name, ha->refcount, ha->global_use);
687
688		for (i = 0; i < dev->addr_len; i++)
689			seq_printf(seq, "%02x", ha->addr[i]);
690
691		seq_putc(seq, '\n');
692	}
693	netif_addr_unlock_bh(dev);
694	return 0;
695}
696
697static const struct seq_operations dev_mc_seq_ops = {
698	.start = dev_seq_start,
699	.next  = dev_seq_next,
700	.stop  = dev_seq_stop,
701	.show  = dev_mc_seq_show,
702};
703
704static int dev_mc_seq_open(struct inode *inode, struct file *file)
705{
706	return seq_open_net(inode, file, &dev_mc_seq_ops,
707			    sizeof(struct seq_net_private));
708}
709
710static const struct file_operations dev_mc_seq_fops = {
711	.owner	 = THIS_MODULE,
712	.open    = dev_mc_seq_open,
713	.read    = seq_read,
714	.llseek  = seq_lseek,
715	.release = seq_release_net,
716};
717
718#endif
719
720static int __net_init dev_mc_net_init(struct net *net)
721{
722	if (!proc_net_fops_create(net, "dev_mcast", 0, &dev_mc_seq_fops))
723		return -ENOMEM;
724	return 0;
725}
726
727static void __net_exit dev_mc_net_exit(struct net *net)
728{
729	proc_net_remove(net, "dev_mcast");
730}
731
732static struct pernet_operations __net_initdata dev_mc_net_ops = {
733	.init = dev_mc_net_init,
734	.exit = dev_mc_net_exit,
735};
736
737void __init dev_mcast_init(void)
738{
739	register_pernet_subsys(&dev_mc_net_ops);
740}
741