dev_addr_lists.c 24 KB

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