dev_addr_lists.c 21 KB

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