Coverage Report

Created: 2026-10-31 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libiec61850/hal/ethernet/linux/ethernet_linux.c
Line
Count
Source
1
/*
2
 *  ethernet_linux.c
3
 *
4
 *  Copyright 2013-2026 Michael Zillgith
5
 *
6
 *  This file is part of libIEC61850.
7
 *
8
 *  libIEC61850 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 3 of the License, or
11
 *  (at your option) any later version.
12
 *
13
 *  libIEC61850 is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with libIEC61850.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 *  See COPYING file for the complete license text.
22
 */
23
24
#include <arpa/inet.h>
25
#include <net/if.h>
26
#include <linux/filter.h>
27
#include <linux/if_arp.h>
28
#include <linux/if_ether.h>
29
#include <linux/if_packet.h>
30
#include <poll.h>
31
#include <stdio.h>
32
#include <sys/ioctl.h>
33
#include <sys/socket.h>
34
#include <unistd.h>
35
36
#include <string.h>
37
38
#include "hal_ethernet.h"
39
#include "lib_memory.h"
40
41
#ifndef DEBUG_SOCKET
42
0
#define DEBUG_SOCKET 0
43
#endif
44
45
struct sEthernetSocket
46
{
47
    int rawSocket;
48
    bool isBind;
49
    struct sockaddr_ll socketAddress;
50
};
51
52
struct sEthernetHandleSet
53
{
54
    struct pollfd* handles;
55
    int nhandles;
56
};
57
58
EthernetHandleSet
59
EthernetHandleSet_new(void)
60
0
{
61
0
    EthernetHandleSet self = (EthernetHandleSet)GLOBAL_MALLOC(sizeof(struct sEthernetHandleSet));
62
63
0
    if (self != NULL)
64
0
    {
65
0
        self->handles = NULL;
66
0
        self->nhandles = 0;
67
0
    }
68
69
0
    return self;
70
0
}
71
72
void
73
EthernetHandleSet_addSocket(EthernetHandleSet self, const EthernetSocket sock)
74
0
{
75
0
    if (self != NULL && sock != NULL)
76
0
    {
77
0
        int i = self->nhandles;
78
0
        struct pollfd* newHandles = realloc(self->handles, (i + 1) * sizeof(struct pollfd));
79
80
0
        if (newHandles == NULL)
81
0
            return;
82
83
0
        self->handles = newHandles;
84
0
        self->nhandles++;
85
86
0
        self->handles[i].fd = sock->rawSocket;
87
0
        self->handles[i].events = POLLIN;
88
0
    }
89
0
}
90
91
void
92
EthernetHandleSet_removeSocket(EthernetHandleSet self, const EthernetSocket sock)
93
0
{
94
0
    if ((self != NULL) && (sock != NULL))
95
0
    {
96
0
        int i;
97
98
0
        for (i = 0; i < self->nhandles; i++)
99
0
        {
100
0
            if (self->handles[i].fd == sock->rawSocket)
101
0
            {
102
0
                memmove(&self->handles[i], &self->handles[i + 1], sizeof(struct pollfd) * (self->nhandles - i - 1));
103
0
                self->nhandles--;
104
0
                return;
105
0
            }
106
0
        }
107
0
    }
108
0
}
109
110
int
111
EthernetHandleSet_waitReady(EthernetHandleSet self, unsigned int timeoutMs)
112
0
{
113
0
    int result;
114
115
0
    if ((self != NULL) && (self->nhandles >= 0))
116
0
    {
117
0
        result = poll(self->handles, self->nhandles, timeoutMs);
118
0
    }
119
0
    else
120
0
    {
121
0
        result = -1;
122
0
    }
123
124
0
    return result;
125
0
}
126
127
void
128
EthernetHandleSet_destroy(EthernetHandleSet self)
129
0
{
130
0
    if (self != NULL)
131
0
    {
132
0
        if (self->nhandles)
133
0
            free(self->handles);
134
135
0
        GLOBAL_FREEMEM(self);
136
0
    }
137
0
}
138
139
static int
140
getInterfaceIndex(int sock, const char* deviceName)
141
314
{
142
314
    struct ifreq ifr;
143
314
    memset(&ifr, 0, sizeof(struct ifreq));
144
145
314
    strncpy(ifr.ifr_name, deviceName, IFNAMSIZ - 1);
146
147
314
    if (ioctl(sock, SIOCGIFINDEX, &ifr) == -1)
148
0
    {
149
0
        if (DEBUG_SOCKET)
150
0
            printf("ETHERNET_LINUX: Failed to get interface index\n");
151
0
        return -1;
152
0
    }
153
154
314
    int interfaceIndex = ifr.ifr_ifindex;
155
156
314
    return interfaceIndex;
157
314
}
158
159
void
160
Ethernet_getInterfaceMACAddress(const char* interfaceId, uint8_t* addr)
161
0
{
162
0
    struct ifreq buffer;
163
164
0
    int sock = socket(PF_INET, SOCK_DGRAM, 0);
165
166
0
    memset(&buffer, 0x00, sizeof(buffer));
167
168
0
    strncpy(buffer.ifr_name, interfaceId, IFNAMSIZ - 1);
169
170
0
    ioctl(sock, SIOCGIFHWADDR, &buffer);
171
172
0
    close(sock);
173
174
0
    int i;
175
176
0
    for (i = 0; i < 6; i++)
177
0
    {
178
0
        addr[i] = (unsigned char)buffer.ifr_hwaddr.sa_data[i];
179
0
    }
180
0
}
181
182
EthernetSocket
183
Ethernet_createSocket(const char* interfaceId, uint8_t* destAddress)
184
314
{
185
314
    EthernetSocket self = GLOBAL_CALLOC(1, sizeof(struct sEthernetSocket));
186
187
314
    if (self)
188
314
    {
189
314
        self->rawSocket = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
190
191
314
        if (self->rawSocket == -1)
192
0
        {
193
0
            if (DEBUG_SOCKET)
194
0
                printf("Error creating raw socket!\n");
195
0
            GLOBAL_FREEMEM(self);
196
0
            return NULL;
197
0
        }
198
199
314
        self->socketAddress.sll_family = PF_PACKET;
200
314
        self->socketAddress.sll_protocol = htons(ETH_P_ALL);
201
202
314
        int ifcIdx = getInterfaceIndex(self->rawSocket, interfaceId);
203
204
314
        if (ifcIdx == -1)
205
0
        {
206
0
            Ethernet_destroySocket(self);
207
0
            return NULL;
208
0
        }
209
210
314
        self->socketAddress.sll_ifindex = ifcIdx;
211
212
314
        self->socketAddress.sll_hatype = ARPHRD_ETHER;
213
314
        self->socketAddress.sll_pkttype = PACKET_HOST | PACKET_MULTICAST;
214
215
314
        self->socketAddress.sll_halen = ETH_ALEN;
216
217
314
        memset(self->socketAddress.sll_addr, 0, 8);
218
219
314
        if (destAddress != NULL)
220
0
            memcpy(self->socketAddress.sll_addr, destAddress, 6);
221
222
314
        self->isBind = false;
223
224
314
        Ethernet_setMode(self, ETHERNET_SOCKET_MODE_PROMISC);
225
314
    }
226
227
314
    return self;
228
314
}
229
230
void
231
Ethernet_setMode(EthernetSocket self, EthernetSocketMode mode)
232
628
{
233
628
    if (self)
234
628
    {
235
628
        if (mode == ETHERNET_SOCKET_MODE_PROMISC)
236
314
        {
237
314
            struct ifreq ifr;
238
314
            memset(&ifr, 0, sizeof(struct ifreq));
239
240
314
            if (if_indextoname(self->socketAddress.sll_ifindex, ifr.ifr_name) == NULL)
241
0
            {
242
0
                if (DEBUG_SOCKET)
243
0
                    printf("ETHERNET_LINUX: Problem getting device name\n");
244
0
                return;
245
0
            }
246
247
314
            if (ioctl(self->rawSocket, SIOCGIFFLAGS, &ifr) == -1)
248
0
            {
249
0
                if (DEBUG_SOCKET)
250
0
                    printf("ETHERNET_LINUX: Problem getting device flags\n");
251
0
                return;
252
0
            }
253
254
314
            ifr.ifr_flags |= IFF_PROMISC;
255
314
            if (ioctl(self->rawSocket, SIOCSIFFLAGS, &ifr) == -1)
256
0
            {
257
0
                if (DEBUG_SOCKET)
258
0
                    printf("ETHERNET_LINUX: Setting device to promiscuous mode failed\n");
259
0
                return;
260
0
            }
261
314
        }
262
314
        else if (mode == ETHERNET_SOCKET_MODE_ALL_MULTICAST)
263
0
        {
264
0
            struct ifreq ifr;
265
0
            memset(&ifr, 0, sizeof(struct ifreq));
266
267
0
            if (if_indextoname(self->socketAddress.sll_ifindex, ifr.ifr_name) == NULL)
268
0
            {
269
0
                if (DEBUG_SOCKET)
270
0
                    printf("ETHERNET_LINUX: Problem getting device name\n");
271
0
                return;
272
0
            }
273
274
0
            if (ioctl(self->rawSocket, SIOCGIFFLAGS, &ifr) == -1)
275
0
            {
276
0
                if (DEBUG_SOCKET)
277
0
                    printf("ETHERNET_LINUX: Problem getting device flags\n");
278
0
                return;
279
0
            }
280
281
0
            ifr.ifr_flags |= IFF_ALLMULTI;
282
0
            if (ioctl(self->rawSocket, SIOCSIFFLAGS, &ifr) == -1)
283
0
            {
284
0
                if (DEBUG_SOCKET)
285
0
                    printf("ETHERNET_LINUX: Setting device to promiscuous mode failed\n");
286
0
                return;
287
0
            }
288
0
        }
289
314
        else if (mode == ETHERNET_SOCKET_MODE_HOST_ONLY)
290
0
        {
291
0
            self->socketAddress.sll_pkttype = PACKET_HOST;
292
0
        }
293
314
        else if (mode == ETHERNET_SOCKET_MODE_MULTICAST)
294
314
        {
295
314
            self->socketAddress.sll_pkttype = PACKET_HOST | PACKET_MULTICAST;
296
314
        }
297
628
    }
298
628
}
299
300
void
301
Ethernet_addMulticastAddress(EthernetSocket self, const uint8_t* multicastAddress)
302
314
{
303
314
    struct packet_mreq mreq;
304
314
    memset(&mreq, 0, sizeof(struct packet_mreq));
305
306
314
    mreq.mr_ifindex = self->socketAddress.sll_ifindex;
307
314
    mreq.mr_alen = ETH_ALEN;
308
314
    mreq.mr_type = PACKET_MR_MULTICAST;
309
314
    mreq.mr_address[0] = multicastAddress[0];
310
314
    mreq.mr_address[1] = multicastAddress[1];
311
314
    mreq.mr_address[2] = multicastAddress[2];
312
314
    mreq.mr_address[3] = multicastAddress[3];
313
314
    mreq.mr_address[4] = multicastAddress[4];
314
314
    mreq.mr_address[5] = multicastAddress[5];
315
316
314
    int res = setsockopt(self->rawSocket, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
317
318
314
    if (res != 0)
319
0
    {
320
0
        if (DEBUG_SOCKET)
321
0
            printf("ETHERNET_LINUX: Setting multicast address failed\n");
322
0
    }
323
314
}
324
325
void
326
Ethernet_setProtocolFilter(EthernetSocket self, uint16_t etherType)
327
314
{
328
314
    if (etherType == 0x88b8)
329
314
    {
330
        /* enable linux kernel filtering for GOOSE */
331
332
314
        struct sock_fprog fprog;
333
314
        struct sock_filter filter[] = {/* sudo tcpdump -i enx88d7f6377223 '(ether proto 0x88b8)' -dd */
334
314
                                       {0x28, 0, 0, 0x0000000c},
335
314
                                       {0x15, 0, 1, 0x000088b8},
336
314
                                       {0x6, 0, 0, 0x00040000},
337
314
                                       {0x6, 0, 0, 0x00000000}};
338
339
314
        fprog.len = sizeof(filter) / sizeof(*filter);
340
314
        fprog.filter = filter;
341
342
314
        if (setsockopt(self->rawSocket, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog)) == -1)
343
0
            if (DEBUG_SOCKET)
344
0
                printf("ETHERNET_LINUX: Applying filter failed\n");
345
314
    }
346
0
    else
347
0
    {
348
0
        self->socketAddress.sll_protocol = htons(etherType);
349
0
    }
350
314
}
351
352
/* non-blocking receive */
353
int
354
Ethernet_receivePacket(EthernetSocket self, uint8_t* buffer, int bufferSize)
355
0
{
356
0
    if (self->isBind == false)
357
0
    {
358
0
        if (bind(self->rawSocket, (struct sockaddr*)&self->socketAddress, sizeof(self->socketAddress)) == 0)
359
0
            self->isBind = true;
360
0
        else
361
0
            return 0;
362
0
    }
363
364
0
    return recvfrom(self->rawSocket, buffer, bufferSize, MSG_DONTWAIT, 0, 0);
365
0
}
366
367
void
368
Ethernet_sendPacket(EthernetSocket self, uint8_t* buffer, int packetSize)
369
0
{
370
0
    sendto(self->rawSocket, buffer, packetSize, 0, (struct sockaddr*)&(self->socketAddress),
371
0
           sizeof(self->socketAddress));
372
0
}
373
374
void
375
Ethernet_destroySocket(EthernetSocket self)
376
0
{
377
0
    if (self)
378
0
    {
379
0
        close(self->rawSocket);
380
0
        GLOBAL_FREEMEM(self);
381
0
    }
382
0
}
383
384
bool
385
Ethernet_isSupported()
386
0
{
387
    return true;
388
0
}