blob: 1717dda82eb1d3fad2b6e7f95cc86dda5bc87397 [file] [log] [blame]
Marek BehĂșnc2d19d02024-04-04 09:50:54 +02001// 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
11static 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
22static 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
31void 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}