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