]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/dhcp-network.c
Merge pull request #7388 from keszybz/doc-tweak
[thirdparty/systemd.git] / src / libsystemd-network / dhcp-network.c
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 <net/ethernet.h>
22 #include <net/if.h>
23 #include <net/if_arp.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <sys/socket.h>
27 #include <linux/filter.h>
28 #include <linux/if_infiniband.h>
29 #include <linux/if_packet.h>
30
31 #include "dhcp-internal.h"
32 #include "fd-util.h"
33 #include "socket-util.h"
34
35 static int _bind_raw_socket(int ifindex, union sockaddr_union *link,
36 uint32_t xid, const uint8_t *mac_addr,
37 size_t mac_addr_len,
38 const uint8_t *bcast_addr,
39 const struct ether_addr *eth_mac,
40 uint16_t arp_type, uint8_t dhcp_hlen,
41 uint16_t port) {
42 struct sock_filter filter[] = {
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, port, 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 */
64 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, arp_type, 1, 0), /* header type == arp_type ? */
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 */
67 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, dhcp_hlen, 1, 0), /* address length == dhcp_hlen ? */
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 */
72 BPF_STMT(BPF_LD + BPF_IMM, htobe32(*((unsigned int *) eth_mac))), /* A <- 4 bytes of client's MAC */
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 */
78 BPF_STMT(BPF_LD + BPF_IMM, htobe16(*((unsigned short *) (((char *) eth_mac) + 4)))), /* A <- remainder of client's MAC */
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 */
88 };
89 struct sock_fprog fprog = {
90 .len = ELEMENTSOF(filter),
91 .filter = filter
92 };
93 _cleanup_close_ int s = -1;
94 int r, on = 1;
95
96 assert(ifindex > 0);
97 assert(link);
98
99 s = socket(AF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
100 if (s < 0)
101 return -errno;
102
103 r = setsockopt(s, SOL_PACKET, PACKET_AUXDATA, &on, sizeof(on));
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
111 link->ll = (struct sockaddr_ll) {
112 .sll_family = AF_PACKET,
113 .sll_protocol = htobe16(ETH_P_IP),
114 .sll_ifindex = ifindex,
115 .sll_hatype = htobe16(arp_type),
116 .sll_halen = mac_addr_len,
117 };
118 memcpy(link->ll.sll_addr, bcast_addr, mac_addr_len);
119
120 r = bind(s, &link->sa, SOCKADDR_LL_LEN(link->ll));
121 if (r < 0)
122 return -errno;
123
124 r = s;
125 s = -1;
126
127 return r;
128 }
129
130 int dhcp_network_bind_raw_socket(int ifindex, union sockaddr_union *link,
131 uint32_t xid, const uint8_t *mac_addr,
132 size_t mac_addr_len, uint16_t arp_type,
133 uint16_t port) {
134 static const uint8_t eth_bcast[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
135 /* Default broadcast address for IPoIB */
136 static const uint8_t ib_bcast[] = {
137 0x00, 0xff, 0xff, 0xff, 0xff, 0x12, 0x40, 0x1b,
138 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
139 0xff, 0xff, 0xff, 0xff
140 };
141 struct ether_addr eth_mac = { { 0, 0, 0, 0, 0, 0 } };
142 const uint8_t *bcast_addr = NULL;
143 uint8_t dhcp_hlen = 0;
144
145 assert_return(mac_addr_len > 0, -EINVAL);
146
147 if (arp_type == ARPHRD_ETHER) {
148 assert_return(mac_addr_len == ETH_ALEN, -EINVAL);
149 memcpy(&eth_mac, mac_addr, ETH_ALEN);
150 bcast_addr = eth_bcast;
151 dhcp_hlen = ETH_ALEN;
152 } else if (arp_type == ARPHRD_INFINIBAND) {
153 assert_return(mac_addr_len == INFINIBAND_ALEN, -EINVAL);
154 bcast_addr = ib_bcast;
155 } else
156 return -EINVAL;
157
158 return _bind_raw_socket(ifindex, link, xid, mac_addr, mac_addr_len,
159 bcast_addr, &eth_mac, arp_type, dhcp_hlen, port);
160 }
161
162 int dhcp_network_bind_udp_socket(int ifindex, be32_t address, uint16_t port) {
163 union sockaddr_union src = {
164 .in.sin_family = AF_INET,
165 .in.sin_port = htobe16(port),
166 .in.sin_addr.s_addr = address,
167 };
168 _cleanup_close_ int s = -1;
169 char ifname[IF_NAMESIZE] = "";
170 int r, on = 1, tos = IPTOS_CLASS_CS6;
171
172 s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
173 if (s < 0)
174 return -errno;
175
176 r = setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
177 if (r < 0)
178 return -errno;
179
180 r = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
181 if (r < 0)
182 return -errno;
183
184 if (ifindex > 0) {
185 if (if_indextoname(ifindex, ifname) == 0)
186 return -errno;
187
188 r = setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname));
189 if (r < 0)
190 return -errno;
191 }
192
193 if (address == INADDR_ANY) {
194 r = setsockopt(s, IPPROTO_IP, IP_PKTINFO, &on, sizeof(on));
195 if (r < 0)
196 return -errno;
197
198 r = setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
199 if (r < 0)
200 return -errno;
201
202 } else {
203 r = setsockopt(s, IPPROTO_IP, IP_FREEBIND, &on, sizeof(on));
204 if (r < 0)
205 return -errno;
206 }
207
208 r = bind(s, &src.sa, sizeof(src.in));
209 if (r < 0)
210 return -errno;
211
212 r = s;
213 s = -1;
214
215 return r;
216 }
217
218 int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
219 const void *packet, size_t len) {
220 int r;
221
222 assert(link);
223 assert(packet);
224 assert(len);
225
226 r = sendto(s, packet, len, 0, &link->sa, SOCKADDR_LL_LEN(link->ll));
227 if (r < 0)
228 return -errno;
229
230 return 0;
231 }
232
233 int dhcp_network_send_udp_socket(int s, be32_t address, uint16_t port,
234 const void *packet, size_t len) {
235 union sockaddr_union dest = {
236 .in.sin_family = AF_INET,
237 .in.sin_port = htobe16(port),
238 .in.sin_addr.s_addr = address,
239 };
240 int r;
241
242 assert(s >= 0);
243 assert(packet);
244 assert(len);
245
246 r = sendto(s, packet, len, 0, &dest.sa, sizeof(dest.in));
247 if (r < 0)
248 return -errno;
249
250 return 0;
251 }