Marek BehĂșn | c2d19d0 | 2024-04-04 09:50:54 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2017 Marek BehĂșn <kabel@kernel.org> |
| 4 | */ |
| 5 | |
| 6 | #include <env.h> |
| 7 | #include <net.h> |
| 8 | |
| 9 | #include "turris_common.h" |
| 10 | |
| 11 | static void increment_mac(u8 *mac) |
| 12 | { |
| 13 | int i; |
| 14 | |
| 15 | for (i = 5; i >= 3; i--) { |
| 16 | mac[i] += 1; |
| 17 | if (mac[i]) |
| 18 | break; |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | static void set_mac_if_invalid(int i, u8 *mac) |
| 23 | { |
| 24 | u8 oldmac[6]; |
| 25 | |
| 26 | if (is_valid_ethaddr(mac) && |
| 27 | !eth_env_get_enetaddr_by_index("eth", i, oldmac)) |
| 28 | eth_env_set_enetaddr_by_index("eth", i, mac); |
| 29 | } |
| 30 | |
| 31 | void turris_init_mac_addresses(int first_idx, const u8 *first_mac) |
| 32 | { |
| 33 | u8 mac[6]; |
| 34 | |
| 35 | memcpy(mac, first_mac, sizeof(mac)); |
| 36 | |
| 37 | set_mac_if_invalid((first_idx + 0) % 3, mac); |
| 38 | increment_mac(mac); |
| 39 | set_mac_if_invalid((first_idx + 1) % 3, mac); |
| 40 | increment_mac(mac); |
| 41 | set_mac_if_invalid((first_idx + 2) % 3, mac); |
| 42 | } |