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