1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
/*
* lan.c
*
* Copyright 2015-present Facebook. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#define _GNU_SOURCE /* To get defns of NI_MAXSERV and NI_MAXHOST */
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <net/if.h>
#include <linux/if_link.h>
#include <sys/ioctl.h>
#include <stdbool.h>
#include <openbmc/ipmi.h>
#define ETH_INTF_NAME "eth0"
#define IPV6_LINK_LOCAL_BYTE1 0xFE
#define IPV6_LINK_LOCAL_BYTE2 0x80
#define BYTE_MASK 0xFF
#define BYTE1_OFFSET 8
#define BYTE2_OFFSET 16
#define BYTE3_OFFSET 24
void plat_lan_init(lan_config_t *lan)
{
struct ifaddrs *ifaddr, *ifa;
struct sockaddr_in *addr;
struct sockaddr_in6 *addr6;
int family, s, n, i;
unsigned long ip;
unsigned char *ip6;
int sd;
struct ifreq ifr;
uint8_t mac_addr[6];
uint8_t eui_64_addr[8] = {0x0};
bool slaac_flag = false;
char test[64];
if (getifaddrs(&ifaddr) == -1) {
return;
}
for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
if (ifa->ifa_addr == NULL) {
continue;
}
if (strcmp(ifa->ifa_name, ETH_INTF_NAME)) {
continue;
}
family = ifa->ifa_addr->sa_family;
if (family == AF_INET) {
addr = (struct sockaddr_in*) ifa->ifa_addr;
ip = addr->sin_addr.s_addr;
// copy the ip address from long to byte array with MSB first
lan->ip_addr[3] = (ip >> BYTE3_OFFSET) & BYTE_MASK;
lan->ip_addr[2] = (ip >> BYTE2_OFFSET) & BYTE_MASK;
lan->ip_addr[1] = (ip >> BYTE1_OFFSET) & BYTE_MASK;
lan->ip_addr[0] = ip & BYTE_MASK;
} else if (family == AF_INET6) {
addr6 = (struct sockaddr_in6*) ifa->ifa_addr;
ip6 = addr6->sin6_addr.s6_addr;
// If the address is Link Local, Ignore it
if ((ip6[0] == IPV6_LINK_LOCAL_BYTE1) && (ip6[1] == IPV6_LINK_LOCAL_BYTE2)) {
continue;
}
// Get the MAC address
sd = socket(PF_INET, SOCK_DGRAM, 0);
strcpy(ifr.ifr_name, ifa->ifa_name);
if(ioctl(sd, SIOCGIFHWADDR, &ifr) != -1) {
uint8_t* mac_addr = (uint8_t*)ifr.ifr_hwaddr.sa_data;
/*
* SLAAC address has lower 8B as follows:
* 3B == First 24b MAC address
* 2B == FFFE
* 3B == Last 24b MAC address
*/
memcpy((void *)&eui_64_addr[0], (void *)&mac_addr[0], 3);
eui_64_addr[3] = 0xFF;
eui_64_addr[4] = 0xFE;
memcpy((void *)&eui_64_addr[5], (void *)&mac_addr[3], 3);
eui_64_addr[0] += 2;
// Check if the address is SLAAC address. If yes, skip it.
if (!memcmp((void *)&ip6[8], (void *)eui_64_addr, 8)) {
slaac_flag = true;
}
}
if (slaac_flag)
continue;
// copy the ip address from array with MSB first
memcpy(lan->ip6_addr, ip6, SIZE_IP6_ADDR);
}
}
freeifaddrs(ifaddr);
}
|