1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 Copyright © 2013 Intel Corporation. All rights reserved.
7 #include <net/ethernet.h>
9 #include <net/if_arp.h>
12 #include <linux/filter.h>
13 #include <linux/if_infiniband.h>
14 #include <linux/if_packet.h>
16 #include "dhcp-internal.h"
18 #include "socket-util.h"
19 #include "unaligned.h"
21 static int _bind_raw_socket(
23 union sockaddr_union
*link
,
25 const struct hw_addr_data
*hw_addr
,
26 const struct hw_addr_data
*bcast_addr
,
34 assert(IN_SET(arp_type
, ARPHRD_ETHER
, ARPHRD_INFINIBAND
));
38 assert(hw_addr
->length
== ETH_ALEN
);
39 assert(bcast_addr
->length
== ETH_ALEN
);
41 case ARPHRD_INFINIBAND
:
42 assert(hw_addr
->length
== 0);
43 assert(bcast_addr
->length
== INFINIBAND_ALEN
);
49 struct sock_filter filter
[] = {
50 BPF_STMT(BPF_LD
+ BPF_W
+ BPF_LEN
, 0), /* A <- packet length */
51 BPF_JUMP(BPF_JMP
+ BPF_JGE
+ BPF_K
, sizeof(DHCPPacket
), 1, 0), /* packet >= DHCPPacket ? */
52 BPF_STMT(BPF_RET
+ BPF_K
, 0), /* ignore */
53 BPF_STMT(BPF_LD
+ BPF_B
+ BPF_ABS
, offsetof(DHCPPacket
, ip
.protocol
)), /* A <- IP protocol */
54 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, IPPROTO_UDP
, 1, 0), /* IP protocol == UDP ? */
55 BPF_STMT(BPF_RET
+ BPF_K
, 0), /* ignore */
56 BPF_STMT(BPF_LD
+ BPF_B
+ BPF_ABS
, offsetof(DHCPPacket
, ip
.frag_off
)), /* A <- Flags */
57 BPF_STMT(BPF_ALU
+ BPF_AND
+ BPF_K
, 0x20), /* A <- A & 0x20 (More Fragments bit) */
58 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, 0, 1, 0), /* A == 0 ? */
59 BPF_STMT(BPF_RET
+ BPF_K
, 0), /* ignore */
60 BPF_STMT(BPF_LD
+ BPF_H
+ BPF_ABS
, offsetof(DHCPPacket
, ip
.frag_off
)), /* A <- Flags + Fragment offset */
61 BPF_STMT(BPF_ALU
+ BPF_AND
+ BPF_K
, 0x1fff), /* A <- A & 0x1fff (Fragment offset) */
62 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, 0, 1, 0), /* A == 0 ? */
63 BPF_STMT(BPF_RET
+ BPF_K
, 0), /* ignore */
64 BPF_STMT(BPF_LD
+ BPF_H
+ BPF_ABS
, offsetof(DHCPPacket
, udp
.dest
)), /* A <- UDP destination port */
65 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, port
, 1, 0), /* UDP destination port == DHCP client port ? */
66 BPF_STMT(BPF_RET
+ BPF_K
, 0), /* ignore */
67 BPF_STMT(BPF_LD
+ BPF_B
+ BPF_ABS
, offsetof(DHCPPacket
, dhcp
.op
)), /* A <- DHCP op */
68 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, BOOTREPLY
, 1, 0), /* op == BOOTREPLY ? */
69 BPF_STMT(BPF_RET
+ BPF_K
, 0), /* ignore */
70 BPF_STMT(BPF_LD
+ BPF_B
+ BPF_ABS
, offsetof(DHCPPacket
, dhcp
.htype
)), /* A <- DHCP header type */
71 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, arp_type
, 1, 0), /* header type == arp_type ? */
72 BPF_STMT(BPF_RET
+ BPF_K
, 0), /* ignore */
73 BPF_STMT(BPF_LD
+ BPF_W
+ BPF_ABS
, offsetof(DHCPPacket
, dhcp
.xid
)), /* A <- client identifier */
74 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, xid
, 1, 0), /* client identifier == xid ? */
75 BPF_STMT(BPF_RET
+ BPF_K
, 0), /* ignore */
76 BPF_STMT(BPF_LD
+ BPF_B
+ BPF_ABS
, offsetof(DHCPPacket
, dhcp
.hlen
)), /* A <- MAC address length */
77 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, (uint8_t) hw_addr
->length
, 1, 0), /* address length == hw_addr->length ? */
78 BPF_STMT(BPF_RET
+ BPF_K
, 0), /* ignore */
80 /* We only support MAC address length to be either 0 or 6 (ETH_ALEN). Optionally
81 * compare chaddr for ETH_ALEN bytes. */
82 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, ETH_ALEN
, 0, 8), /* A (the MAC address length) == ETH_ALEN ? */
83 BPF_STMT(BPF_LDX
+ BPF_IMM
, unaligned_read_be32(hw_addr
->bytes
)), /* X <- 4 bytes of client's MAC */
84 BPF_STMT(BPF_LD
+ BPF_W
+ BPF_ABS
, offsetof(DHCPPacket
, dhcp
.chaddr
)), /* A <- 4 bytes of MAC from dhcp.chaddr */
85 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_X
, 0, 1, 0), /* A == X ? */
86 BPF_STMT(BPF_RET
+ BPF_K
, 0), /* ignore */
87 BPF_STMT(BPF_LDX
+ BPF_IMM
, unaligned_read_be16(hw_addr
->bytes
+ 4)), /* X <- remainder of client's MAC */
88 BPF_STMT(BPF_LD
+ BPF_H
+ BPF_ABS
, offsetof(DHCPPacket
, dhcp
.chaddr
) + 4), /* A <- remainder of MAC from dhcp.chaddr */
89 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_X
, 0, 1, 0), /* A == X ? */
90 BPF_STMT(BPF_RET
+ BPF_K
, 0), /* ignore */
92 BPF_STMT(BPF_LD
+ BPF_W
+ BPF_ABS
, offsetof(DHCPPacket
, dhcp
.magic
)), /* A <- DHCP magic cookie */
93 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, DHCP_MAGIC_COOKIE
, 1, 0), /* cookie == DHCP magic cookie ? */
94 BPF_STMT(BPF_RET
+ BPF_K
, 0), /* ignore */
95 BPF_STMT(BPF_RET
+ BPF_K
, UINT32_MAX
), /* accept */
97 struct sock_fprog fprog
= {
98 .len
= ELEMENTSOF(filter
),
101 _cleanup_close_
int s
= -1;
104 s
= socket(AF_PACKET
, SOCK_DGRAM
| SOCK_CLOEXEC
| SOCK_NONBLOCK
, 0);
108 r
= setsockopt_int(s
, SOL_PACKET
, PACKET_AUXDATA
, true);
112 r
= setsockopt(s
, SOL_SOCKET
, SO_ATTACH_FILTER
, &fprog
, sizeof(fprog
));
116 link
->ll
= (struct sockaddr_ll
) {
117 .sll_family
= AF_PACKET
,
118 .sll_protocol
= htobe16(ETH_P_IP
),
119 .sll_ifindex
= ifindex
,
120 .sll_hatype
= htobe16(arp_type
),
121 .sll_halen
= bcast_addr
->length
,
123 /* We may overflow link->ll. link->ll_buffer ensures we have enough space. */
124 memcpy(link
->ll
.sll_addr
, bcast_addr
->bytes
, bcast_addr
->length
);
126 r
= bind(s
, &link
->sa
, SOCKADDR_LL_LEN(link
->ll
));
133 int dhcp_network_bind_raw_socket(
135 union sockaddr_union
*link
,
137 const struct hw_addr_data
*hw_addr
,
138 const struct hw_addr_data
*bcast_addr
,
142 static struct hw_addr_data default_eth_bcast
= {
144 .ether
= {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }},
145 }, default_ib_bcast
= {
146 .length
= INFINIBAND_ALEN
,
148 0x00, 0xff, 0xff, 0xff, 0xff, 0x12, 0x40, 0x1b,
149 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
150 0xff, 0xff, 0xff, 0xff
160 return _bind_raw_socket(ifindex
, link
, xid
,
162 (bcast_addr
&& !hw_addr_is_null(bcast_addr
)) ? bcast_addr
: &default_eth_bcast
,
165 case ARPHRD_INFINIBAND
:
166 return _bind_raw_socket(ifindex
, link
, xid
,
168 (bcast_addr
&& !hw_addr_is_null(bcast_addr
)) ? bcast_addr
: &default_ib_bcast
,
175 int dhcp_network_bind_udp_socket(int ifindex
, be32_t address
, uint16_t port
, int ip_service_type
) {
176 union sockaddr_union src
= {
177 .in
.sin_family
= AF_INET
,
178 .in
.sin_port
= htobe16(port
),
179 .in
.sin_addr
.s_addr
= address
,
181 _cleanup_close_
int s
= -1;
184 s
= socket(AF_INET
, SOCK_DGRAM
| SOCK_CLOEXEC
| SOCK_NONBLOCK
, 0);
188 if (ip_service_type
>= 0)
189 r
= setsockopt_int(s
, IPPROTO_IP
, IP_TOS
, ip_service_type
);
191 r
= setsockopt_int(s
, IPPROTO_IP
, IP_TOS
, IPTOS_CLASS_CS6
);
195 r
= setsockopt_int(s
, SOL_SOCKET
, SO_REUSEADDR
, true);
200 r
= socket_bind_to_ifindex(s
, ifindex
);
205 if (port
== DHCP_PORT_SERVER
) {
206 r
= setsockopt_int(s
, SOL_SOCKET
, SO_BROADCAST
, true);
209 if (address
== INADDR_ANY
) {
210 /* IP_PKTINFO filter should not be applied when packets are
211 allowed to enter/leave through the interface other than
212 DHCP server sits on(BindToInterface option). */
213 r
= setsockopt_int(s
, IPPROTO_IP
, IP_PKTINFO
, true);
218 r
= setsockopt_int(s
, IPPROTO_IP
, IP_FREEBIND
, true);
223 if (bind(s
, &src
.sa
, sizeof(src
.in
)) < 0)
229 int dhcp_network_send_raw_socket(
231 const union sockaddr_union
*link
,
235 /* Do not add assert(s >= 0) here, as this is called in fuzz-dhcp-server, and in that case this
236 * function should fail with negative errno. */
242 if (sendto(s
, packet
, len
, 0, &link
->sa
, SOCKADDR_LL_LEN(link
->ll
)) < 0)
248 int dhcp_network_send_udp_socket(
255 union sockaddr_union dest
= {
256 .in
.sin_family
= AF_INET
,
257 .in
.sin_port
= htobe16(port
),
258 .in
.sin_addr
.s_addr
= address
,
265 if (sendto(s
, packet
, len
, 0, &dest
.sa
, sizeof(dest
.in
)) < 0)