]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd-network/dhcp-network.c
sd-dhcp-client: support non-Ethernet hardware addresses
[thirdparty/systemd.git] / src / libsystemd-network / dhcp-network.c
CommitLineData
8b4a9693
PF
1/***
2 This file is part of systemd.
3
4 Copyright (C) 2013 Intel Corporation. All rights reserved.
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
20#include <errno.h>
21#include <sys/types.h>
22#include <sys/socket.h>
23#include <string.h>
24#include <linux/if_packet.h>
76253e73 25#include <linux/if_infiniband.h>
8b4a9693 26#include <net/ethernet.h>
7429b07f 27#include <net/if_arp.h>
8b4a9693
PF
28#include <stdio.h>
29#include <unistd.h>
bc29e507 30#include <linux/filter.h>
8b4a9693
PF
31
32#include "socket-util.h"
33
34#include "dhcp-internal.h"
35
76253e73
DW
36static int _bind_raw_socket(int ifindex, union sockaddr_union *link,
37 uint32_t xid, const uint8_t *mac_addr,
38 size_t mac_addr_len,
39 const uint8_t *bcast_addr,
40 const struct ether_addr *eth_mac,
41 uint16_t arp_type, uint8_t dhcp_hlen) {
bc29e507 42 struct sock_filter filter[] = {
088b6ba2
LP
43 BPF_STMT(BPF_LD + BPF_W + BPF_LEN, 0), /* A <- packet length */
44 BPF_JUMP(BPF_JMP + BPF_JGE + BPF_K, sizeof(DHCPPacket), 1, 0), /* packet >= DHCPPacket ? */
45 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
46 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, offsetof(DHCPPacket, ip.protocol)), /* A <- IP protocol */
47 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 1, 0), /* IP protocol == UDP ? */
48 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
49 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, offsetof(DHCPPacket, ip.frag_off)), /* A <- Flags */
50 BPF_STMT(BPF_ALU + BPF_AND + BPF_K, 0x20), /* A <- A & 0x20 (More Fragments bit) */
51 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0, 1, 0), /* A == 0 ? */
52 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
53 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(DHCPPacket, ip.frag_off)), /* A <- Flags + Fragment offset */
54 BPF_STMT(BPF_ALU + BPF_AND + BPF_K, 0x1fff), /* A <- A & 0x1fff (Fragment offset) */
55 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0, 1, 0), /* A == 0 ? */
56 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
57 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(DHCPPacket, udp.dest)), /* A <- UDP destination port */
58 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, DHCP_PORT_CLIENT, 1, 0), /* UDP destination port == DHCP client port ? */
59 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
60 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, offsetof(DHCPPacket, dhcp.op)), /* A <- DHCP op */
61 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, BOOTREPLY, 1, 0), /* op == BOOTREPLY ? */
62 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
63 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, offsetof(DHCPPacket, dhcp.htype)), /* A <- DHCP header type */
76253e73 64 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, arp_type, 1, 0), /* header type == arp_type ? */
088b6ba2
LP
65 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
66 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, offsetof(DHCPPacket, dhcp.hlen)), /* A <- mac address length */
76253e73 67 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, dhcp_hlen, 1, 0), /* address length == dhcp_hlen ? */
088b6ba2
LP
68 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
69 BPF_STMT(BPF_LD + BPF_W + BPF_ABS, offsetof(DHCPPacket, dhcp.xid)), /* A <- client identifier */
70 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, xid, 1, 0), /* client identifier == xid ? */
71 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
76253e73 72 BPF_STMT(BPF_LD + BPF_IMM, htobe32(*((unsigned int *) eth_mac))), /* A <- 4 bytes of client's MAC */
088b6ba2
LP
73 BPF_STMT(BPF_MISC + BPF_TAX, 0), /* X <- A */
74 BPF_STMT(BPF_LD + BPF_W + BPF_ABS, offsetof(DHCPPacket, dhcp.chaddr)), /* A <- 4 bytes of MAC from dhcp.chaddr */
75 BPF_STMT(BPF_ALU + BPF_XOR + BPF_X, 0), /* A xor X */
76 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0, 1, 0), /* A == 0 ? */
77 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
76253e73 78 BPF_STMT(BPF_LD + BPF_IMM, htobe16(*((unsigned short *) (((char *) eth_mac) + 4)))), /* A <- remainder of client's MAC */
088b6ba2
LP
79 BPF_STMT(BPF_MISC + BPF_TAX, 0), /* X <- A */
80 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(DHCPPacket, dhcp.chaddr) + 4), /* A <- remainder of MAC from dhcp.chaddr */
81 BPF_STMT(BPF_ALU + BPF_XOR + BPF_X, 0), /* A xor X */
82 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0, 1, 0), /* A == 0 ? */
83 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
84 BPF_STMT(BPF_LD + BPF_W + BPF_ABS, offsetof(DHCPPacket, dhcp.magic)), /* A <- DHCP magic cookie */
85 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, DHCP_MAGIC_COOKIE, 1, 0), /* cookie == DHCP magic cookie ? */
86 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
87 BPF_STMT(BPF_RET + BPF_K, 65535), /* return all */
bc29e507
TG
88 };
89 struct sock_fprog fprog = {
088b6ba2
LP
90 .len = ELEMENTSOF(filter),
91 .filter = filter
bc29e507 92 };
c3d2994b 93 _cleanup_close_ int s = -1;
fef0e0f3 94 int r, on = 1;
8b4a9693 95
088b6ba2 96 assert(ifindex > 0);
23f30ed3
TG
97 assert(link);
98
66a67eff 99 s = socket(AF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
8b4a9693
PF
100 if (s < 0)
101 return -errno;
102
088b6ba2 103 r = setsockopt(s, SOL_PACKET, PACKET_AUXDATA, &on, sizeof(on));
c3d2994b
TG
104 if (r < 0)
105 return -errno;
106
107 r = setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog));
108 if (r < 0)
109 return -errno;
110
8c00042c
PF
111 link->ll.sll_family = AF_PACKET;
112 link->ll.sll_protocol = htons(ETH_P_IP);
088b6ba2 113 link->ll.sll_ifindex = ifindex;
76253e73
DW
114 link->ll.sll_hatype = htons(arp_type);
115 link->ll.sll_halen = mac_addr_len;
116 memcpy(link->ll.sll_addr, bcast_addr, mac_addr_len);
8b4a9693 117
c3d2994b
TG
118 r = bind(s, &link->sa, sizeof(link->ll));
119 if (r < 0)
bc29e507 120 return -errno;
bc29e507 121
c3d2994b
TG
122 r = s;
123 s = -1;
8b4a9693 124
c3d2994b 125 return r;
8c00042c
PF
126}
127
76253e73
DW
128int dhcp_network_bind_raw_socket(int ifindex, union sockaddr_union *link,
129 uint32_t xid, const uint8_t *mac_addr,
130 size_t mac_addr_len, uint16_t arp_type) {
131 static const uint8_t eth_bcast[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
132 /* Default broadcast address for IPoIB */
133 static const uint8_t ib_bcast[] = {
134 0x00, 0xff, 0xff, 0xff, 0xff, 0x12, 0x40, 0x1b,
135 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
136 0xff, 0xff, 0xff, 0xff
137 };
138 struct ether_addr eth_mac = { { 0, 0, 0, 0, 0, 0 } };
139 const uint8_t *bcast_addr = NULL;
140 uint8_t dhcp_hlen = 0;
141
142 assert_return(mac_addr_len > 0, -EINVAL);
143
144 if (arp_type == ARPHRD_ETHER) {
145 assert_return(mac_addr_len == ETH_ALEN, -EINVAL);
146 memcpy(&eth_mac, mac_addr, ETH_ALEN);
147 bcast_addr = eth_bcast;
148 dhcp_hlen = ETH_ALEN;
149 } else if (arp_type == ARPHRD_INFINIBAND) {
150 assert_return(mac_addr_len == INFINIBAND_ALEN, -EINVAL);
151 bcast_addr = ib_bcast;
152 } else
153 return -EINVAL;
154
155 return _bind_raw_socket(ifindex, link, xid, mac_addr, mac_addr_len,
156 bcast_addr, &eth_mac, arp_type, dhcp_hlen);
157}
158
c3d2994b 159int dhcp_network_bind_udp_socket(be32_t address, uint16_t port) {
234fc2df
PF
160 union sockaddr_union src = {
161 .in.sin_family = AF_INET,
080ab276
TG
162 .in.sin_port = htobe16(port),
163 .in.sin_addr.s_addr = address,
234fc2df 164 };
c3d2994b 165 _cleanup_close_ int s = -1;
076adf01 166 int r, on = 1, tos = IPTOS_CLASS_CS6;
234fc2df
PF
167
168 s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
169 if (s < 0)
170 return -errno;
171
c3d2994b
TG
172 r = setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
173 if (r < 0)
85923f79 174 return -errno;
b44cd882 175
076adf01
TG
176 r = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
177 if (r < 0)
178 return -errno;
fef0e0f3 179
076adf01 180 if (address == INADDR_ANY) {
fef0e0f3
TG
181 r = setsockopt(s, IPPROTO_IP, IP_PKTINFO, &on, sizeof(on));
182 if (r < 0)
183 return -errno;
d6bd972d
TG
184
185 r = setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
186 if (r < 0)
187 return -errno;
076adf01
TG
188 } else {
189 r = setsockopt(s, IPPROTO_IP, IP_FREEBIND, &on, sizeof(on));
190 if (r < 0)
191 return -errno;
fef0e0f3 192 }
85923f79 193
c3d2994b
TG
194 r = bind(s, &src.sa, sizeof(src.in));
195 if (r < 0)
234fc2df 196 return -errno;
234fc2df 197
c3d2994b
TG
198 r = s;
199 s = -1;
200
201 return r;
234fc2df
PF
202}
203
8c00042c 204int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
c3d2994b
TG
205 const void *packet, size_t len) {
206 int r;
207
23f30ed3
TG
208 assert(link);
209 assert(packet);
210 assert(len);
211
c3d2994b
TG
212 r = sendto(s, packet, len, 0, &link->sa, sizeof(link->ll));
213 if (r < 0)
1c8035a8 214 return -errno;
8b4a9693 215
1c8035a8 216 return 0;
8b4a9693 217}
234fc2df 218
080ab276 219int dhcp_network_send_udp_socket(int s, be32_t address, uint16_t port,
c3d2994b 220 const void *packet, size_t len) {
234fc2df
PF
221 union sockaddr_union dest = {
222 .in.sin_family = AF_INET,
080ab276
TG
223 .in.sin_port = htobe16(port),
224 .in.sin_addr.s_addr = address,
234fc2df 225 };
c3d2994b
TG
226 int r;
227
228 assert(s >= 0);
229 assert(packet);
230 assert(len);
234fc2df 231
c3d2994b
TG
232 r = sendto(s, packet, len, 0, &dest.sa, sizeof(dest.in));
233 if (r < 0)
234fc2df
PF
234 return -errno;
235
236 return 0;
237}