summaryrefslogtreecommitdiff
path: root/shared/n-dhcp4/src/n-dhcp4-s-connection.c
blob: 71ae0be826cd9186d9a0309a1ff02ebab63bb0b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 * DHCPv4 Server Connection
 *
 * XXX
 */

#include <assert.h>
#include <c-stdaux.h>
#include <errno.h>
#include <net/if_arp.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include "n-dhcp4-private.h"
#include "util/packet.h"

int n_dhcp4_s_connection_init(NDhcp4SConnection *connection, int ifindex) {
        int r;

        *connection = (NDhcp4SConnection)N_DHCP4_S_CONNECTION_NULL(*connection);

        r = n_dhcp4_s_socket_packet_new(&connection->fd_packet);
        if (r)
                return r;

        r = n_dhcp4_s_socket_udp_new(&connection->fd_udp, ifindex);
        if (r)
                return r;

        connection->ifindex = ifindex;

        return 0;
}

void n_dhcp4_s_connection_deinit(NDhcp4SConnection *connection) {
        c_assert(!connection->ip);

        if (connection->fd_udp >= 0) {
                close(connection->fd_udp);
        }

        if (connection->fd_packet >= 0) {
                close(connection->fd_packet);
        }

        *connection = (NDhcp4SConnection)N_DHCP4_S_CONNECTION_NULL(*connection);
}

void n_dhcp4_s_connection_get_fd(NDhcp4SConnection *connection, int *fdp) {
        *fdp = connection->fd_udp;
}

static bool n_dhcp4_s_connection_owns_ip(NDhcp4SConnection *connection, struct in_addr addr) {
        if (!connection->ip)
                return false;
        return (connection->ip->ip.s_addr == addr.s_addr);
}

static int n_dhcp4_s_connection_verify_incoming(NDhcp4SConnection *connection,
                                                NDhcp4Incoming *message,
                                                bool broadcast) {
        uint8_t type;
        int r;

        r = n_dhcp4_incoming_query_message_type(message, &type);
        if (r) {
                if (r == N_DHCP4_E_UNSET)
                        return N_DHCP4_E_MALFORMED;
                else
                        return r;
        }

        switch (type) {
        case N_DHCP4_MESSAGE_DISCOVER:
                message->userdata.type = N_DHCP4_C_MESSAGE_DISCOVER;
                break;
        case N_DHCP4_MESSAGE_REQUEST: {
                struct in_addr server_identifier = {};
                struct in_addr requested_ip = {};

                r = n_dhcp4_incoming_query_server_identifier(message, &server_identifier);
                if (r) {
                        if (r == N_DHCP4_E_UNSET) {
                                r = n_dhcp4_incoming_query_requested_ip(message, &requested_ip);
                                if (r) {
                                        if (r == N_DHCP4_E_UNSET) {
                                                if (broadcast) {
                                                        message->userdata.type = N_DHCP4_C_MESSAGE_REBIND;
                                                } else {
                                                        message->userdata.type = N_DHCP4_C_MESSAGE_RENEW;
                                                }
                                        } else {
                                                return r;
                                        }
                                } else {
                                        message->userdata.type = N_DHCP4_C_MESSAGE_REBOOT;
                                }
                        } else {
                                return r;
                        }
                } else {
                        if (n_dhcp4_s_connection_owns_ip(connection, server_identifier)) {
                                message->userdata.type = N_DHCP4_C_MESSAGE_SELECT;
                        } else {
                                message->userdata.type = N_DHCP4_C_MESSAGE_IGNORE;
                        }
                }
        }
                break;
        case N_DHCP4_MESSAGE_DECLINE:
                message->userdata.type = N_DHCP4_C_MESSAGE_DECLINE;
                break;
        case N_DHCP4_MESSAGE_RELEASE:
                message->userdata.type = N_DHCP4_C_MESSAGE_RELEASE;
                break;
        default:
                return N_DHCP4_E_UNEXPECTED;
        }

        return 0;
}

int n_dhcp4_s_connection_dispatch_io(NDhcp4SConnection *connection, NDhcp4Incoming **messagep) {
        _c_cleanup_(n_dhcp4_incoming_freep) NDhcp4Incoming *message = NULL;
        struct sockaddr_in dest = {};
        int r;

        r = n_dhcp4_s_socket_udp_recv(connection->fd_udp,
                                          connection->buf,
                                          sizeof(connection->buf),
                                          &message,
                                          &dest);
        if (r)
                return r;

        r = n_dhcp4_s_connection_verify_incoming(connection,
                                                 message,
                                                 dest.sin_addr.s_addr == INADDR_BROADCAST);
        if (r) {
                if (r == N_DHCP4_E_MALFORMED || r == N_DHCP4_E_UNEXPECTED) {
                        *messagep = NULL;
                        return 0;
                }

                return -ENOTRECOVERABLE;
        }

        *messagep = message;
        message = NULL;
        return 0;
}

/*
 * If the 'giaddr' field in a DHCP message from a client is non-zero,
 * the server sends any return messages to the 'DHCP server' port on the
 * BOOTP relay agent whose address appears in 'giaddr'. If the 'giaddr'
 * field is zero and the 'ciaddr' field is nonzero, then the server
 * unicasts DHCPOFFER and DHCPACK messages to the address in 'ciaddr'.
 * If 'giaddr' is zero and 'ciaddr' is zero, and the broadcast bit is
 * set, then the server broadcasts DHCPOFFER and DHCPACK messages to
 * 0xffffffff. If the broadcast bit is not set and 'giaddr' is zero and
 * 'ciaddr' is zero, then the server unicasts DHCPOFFER and DHCPACK
 * messages to the client's hardware address and 'yiaddr' address.  In
 * all cases, when 'giaddr' is zero, the server broadcasts any DHCPNAK
 * messages to 0xffffffff.
 */
int n_dhcp4_s_connection_send_reply(NDhcp4SConnection *connection,
                                    const struct in_addr *server_addr,
                                    NDhcp4Outgoing *message) {
        NDhcp4Header *header = n_dhcp4_outgoing_get_header(message);
        int r;

        if (header->giaddr) {
                const struct in_addr giaddr = { header->giaddr };

                r = n_dhcp4_s_socket_udp_send(connection->fd_udp,
                                              server_addr,
                                              &giaddr,
                                              message);
                if (r)
                        return r;
        } else if (header->ciaddr) {
                const struct in_addr ciaddr = { header->ciaddr };

                r = n_dhcp4_s_socket_udp_send(connection->fd_udp,
                                              server_addr,
                                              &ciaddr,
                                              message);
                if (r)
                        return r;
        } else if (header->flags & htons(N_DHCP4_MESSAGE_FLAG_BROADCAST)) {
                r = n_dhcp4_s_socket_udp_broadcast(connection->fd_udp,
                                                   server_addr,
                                                   message);
                if (r)
                        return r;
        } else {
                r = n_dhcp4_s_socket_packet_send(connection->fd_packet,
                                                 connection->ifindex,
                                                 server_addr,
                                                 header->chaddr,
                                                 header->hlen,
                                                 &(struct in_addr){header->yiaddr},
                                                 message);
                if (r)
                        return r;
        }

        return 0;
}

static void n_dhcp4_s_connection_init_reply_header(NDhcp4SConnection *connection,
                                                   NDhcp4Header *request,
                                                   NDhcp4Header *reply) {
        reply->op = N_DHCP4_OP_BOOTREPLY;

        reply->htype = request->htype;
        reply->hlen = request->hlen;
        reply->flags = request->flags;
        reply->xid = request->xid;
        reply->ciaddr = request->ciaddr;
        reply->giaddr = request->giaddr;
        memcpy(reply->chaddr, request->chaddr, request->hlen);
}

static int n_dhcp4_s_connection_outgoing_set_yiaddr(NDhcp4Outgoing *message,
                                                     uint32_t yiaddr,
                                                     uint32_t lifetime) {
        uint32_t t1 = lifetime / 2;
        uint32_t t2 = ((uint64_t)lifetime * 7) / 8;
        struct in_addr addr = { .s_addr = yiaddr };
        int r;

        r = n_dhcp4_outgoing_append_lifetime(message, lifetime);
        if (r)
                return r;

        r = n_dhcp4_outgoing_append_t1(message, t1);
        if (r)
                return r;

        r = n_dhcp4_outgoing_append_t2(message, t2);
        if (r)
                return r;

        n_dhcp4_outgoing_set_yiaddr(message, addr);

        return 0;
}

static int n_dhcp4_s_connection_new_reply(NDhcp4SConnection *connection,
                                          NDhcp4Outgoing **messagep,
                                          NDhcp4Incoming *request,
                                          uint8_t type,
                                          const struct in_addr *server_address) {
        _c_cleanup_(n_dhcp4_outgoing_freep) NDhcp4Outgoing *message = NULL;
        uint16_t max_message_size;
        uint8_t *client_identifier;
        size_t n_client_identifier;
        int r;

        r = n_dhcp4_incoming_query_max_message_size(request, &max_message_size);
        if (r)
                return r;

        r = n_dhcp4_outgoing_new(&message,
                                 max_message_size,
                                 N_DHCP4_OVERLOAD_FILE | N_DHCP4_OVERLOAD_SNAME);
        if (r)
                return r;

        n_dhcp4_s_connection_init_reply_header(connection,
                                               n_dhcp4_incoming_get_header(request),
                                               n_dhcp4_outgoing_get_header(message));

        r = n_dhcp4_outgoing_append(message, N_DHCP4_OPTION_MESSAGE_TYPE, &type, sizeof(type));
        if (r)
                return r;

        r = n_dhcp4_outgoing_append_server_identifier(message, *server_address);
        if (r)
                return r;

        r = n_dhcp4_incoming_query(request,
                                   N_DHCP4_OPTION_CLIENT_IDENTIFIER,
                                   &client_identifier,
                                   &n_client_identifier);
        if (!r) {
                r = n_dhcp4_outgoing_append(message,
                                            N_DHCP4_OPTION_CLIENT_IDENTIFIER,
                                            client_identifier,
                                            n_client_identifier);
                if (r)
                        return r;
        } else if (r != N_DHCP4_E_UNSET) {
                return r;
        }

        *messagep = message;
        message = NULL;
        return 0;
}

int n_dhcp4_s_connection_offer_new(NDhcp4SConnection *connection,
                                   NDhcp4Outgoing **replyp,
                                   NDhcp4Incoming *request,
                                   const struct in_addr *server_address,
                                   const struct in_addr *client_address,
                                   uint32_t lifetime) {
        _c_cleanup_(n_dhcp4_outgoing_freep) NDhcp4Outgoing *reply = NULL;
        int r;

        r = n_dhcp4_s_connection_new_reply(connection,
                                           &reply,
                                           request,
                                           N_DHCP4_MESSAGE_OFFER,
                                           server_address);
        if (r)
                return r;

        r = n_dhcp4_s_connection_outgoing_set_yiaddr(reply,
                                                     client_address->s_addr,
                                                     lifetime);
        if (r)
                return r;

        *replyp = reply;
        reply = NULL;
        return 0;
}

int n_dhcp4_s_connection_ack_new(NDhcp4SConnection *connection,
                                 NDhcp4Outgoing **replyp,
                                 NDhcp4Incoming *request,
                                 const struct in_addr *server_address,
                                 const struct in_addr *client_address,
                                 uint32_t lifetime) {
        _c_cleanup_(n_dhcp4_outgoing_freep) NDhcp4Outgoing *reply = NULL;
        int r;

        r = n_dhcp4_s_connection_new_reply(connection,
                                           &reply,
                                           request,
                                           N_DHCP4_MESSAGE_ACK,
                                           server_address);
        if (r)
                return r;

        r = n_dhcp4_s_connection_outgoing_set_yiaddr(reply,
                                                     client_address->s_addr,
                                                     lifetime);
        if (r)
                return r;

        *replyp = reply;
        reply = NULL;
        return 0;
}

int n_dhcp4_s_connection_nak_new(NDhcp4SConnection *connection,
                                 NDhcp4Outgoing **replyp,
                                 NDhcp4Incoming *request,
                                 const struct in_addr *server_address) {
        _c_cleanup_(n_dhcp4_outgoing_freep) NDhcp4Outgoing *reply = NULL;
        int r;

        r = n_dhcp4_s_connection_new_reply(connection,
                                           &reply,
                                           request,
                                           N_DHCP4_MESSAGE_NAK,
                                           server_address);
        if (r)
                return r;

        /*
         * The RFC is a bit unclear on how NAK should be sent, on the
         * one hand it says that they should be unconditinoally broadcast
         * (unless going through a relay agent), on the other, when they
         * do go through a relay agent, they will not be. We treat them
         * as any other reply and only broadcast when the broadcast bit
         * is set.
         */

        *replyp = reply;
        reply = NULL;
        return 0;
}

void n_dhcp4_s_connection_ip_init(NDhcp4SConnectionIp *ip, struct in_addr addr) {
        *ip = (NDhcp4SConnectionIp)N_DHCP4_S_CONNECTION_IP_NULL(*ip);
        ip->ip = addr;
}

void n_dhcp4_s_connection_ip_deinit(NDhcp4SConnectionIp *ip) {
        c_assert(!ip->connection);
        *ip = (NDhcp4SConnectionIp)N_DHCP4_S_CONNECTION_IP_NULL(*ip);
}

void n_dhcp4_s_connection_ip_link(NDhcp4SConnectionIp *ip, NDhcp4SConnection *connection) {
        c_assert(!connection->ip);
        c_assert(!ip->connection);

        connection->ip = ip;
        ip->connection = connection;
}

void n_dhcp4_s_connection_ip_unlink(NDhcp4SConnectionIp *ip) {
        ip->connection->ip = NULL;
        ip->connection = NULL;
}