]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/lldp-network.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <linux/filter.h>
4 #include <netinet/if_ether.h>
7 #include "lldp-network.h"
8 #include "missing_network.h"
9 #include "socket-util.h"
11 int lldp_network_bind_raw_socket(int ifindex
) {
13 static const struct sock_filter filter
[] = {
14 BPF_STMT(BPF_LD
+ BPF_W
+ BPF_ABS
, offsetof(struct ethhdr
, h_dest
)), /* A <- 4 bytes of destination MAC */
15 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, 0x0180c200, 1, 0), /* A != 01:80:c2:00 */
16 BPF_STMT(BPF_RET
+ BPF_K
, 0), /* drop packet */
17 BPF_STMT(BPF_LD
+ BPF_H
+ BPF_ABS
, offsetof(struct ethhdr
, h_dest
) + 4), /* A <- remaining 2 bytes of destination MAC */
18 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, 0x0000, 3, 0), /* A != 00:00 */
19 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, 0x0003, 2, 0), /* A != 00:03 */
20 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, 0x000e, 1, 0), /* A != 00:0e */
21 BPF_STMT(BPF_RET
+ BPF_K
, 0), /* drop packet */
22 BPF_STMT(BPF_LD
+ BPF_H
+ BPF_ABS
, offsetof(struct ethhdr
, h_proto
)), /* A <- protocol */
23 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, ETHERTYPE_LLDP
, 1, 0), /* A != ETHERTYPE_LLDP */
24 BPF_STMT(BPF_RET
+ BPF_K
, 0), /* drop packet */
25 BPF_STMT(BPF_RET
+ BPF_K
, (uint32_t) -1), /* accept packet */
28 static const struct sock_fprog fprog
= {
29 .len
= ELEMENTSOF(filter
),
30 .filter
= (struct sock_filter
*) filter
,
33 struct packet_mreq mreq
= {
34 .mr_ifindex
= ifindex
,
35 .mr_type
= PACKET_MR_MULTICAST
,
37 .mr_address
= { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x00 }
40 union sockaddr_union saddrll
= {
41 .ll
.sll_family
= AF_PACKET
,
42 .ll
.sll_ifindex
= ifindex
,
45 _cleanup_close_
int fd
= -1;
50 fd
= socket(AF_PACKET
, SOCK_RAW
|SOCK_CLOEXEC
|SOCK_NONBLOCK
,
51 htobe16(ETHERTYPE_LLDP
));
55 r
= setsockopt(fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &fprog
, sizeof(fprog
));
59 r
= setsockopt(fd
, SOL_PACKET
, PACKET_ADD_MEMBERSHIP
, &mreq
, sizeof(mreq
));
63 mreq
.mr_address
[ETH_ALEN
- 1] = 0x03;
64 r
= setsockopt(fd
, SOL_PACKET
, PACKET_ADD_MEMBERSHIP
, &mreq
, sizeof(mreq
));
68 mreq
.mr_address
[ETH_ALEN
- 1] = 0x0E;
69 r
= setsockopt(fd
, SOL_PACKET
, PACKET_ADD_MEMBERSHIP
, &mreq
, sizeof(mreq
));
73 r
= bind(fd
, &saddrll
.sa
, sizeof(saddrll
.ll
));