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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
|
/*
* DHCP specific low-level socket helpers
*/
#include <c-stdaux.h>
#include <errno.h>
#include <linux/filter.h>
#include <sys/socket.h> /* needed by linux/if.h */
#include <linux/if.h>
#include <linux/if_packet.h>
#include <linux/netdevice.h>
#include <linux/udp.h>
#include <netinet/ip.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include "n-dhcp4-private.h"
#include "util/packet.h"
#include "util/socket.h"
/**
* n_dhcp4_c_socket_packet_new() - create a new DHCP4 client packet socket
* @sockfdp: return argumnet for the new socket
* @ifindex: interface index to bind to
*
* Create a new AF_PACKET/SOCK_DGRAM socket usable to listen to and send DHCP client
* packets before an IP address has been configured.
*
* Only unfragmented DHCP packets from a server to a client destined for the given
* ifindex is returned.
*
* Return: 0 on success, or a negative error code on failure.
*/
int n_dhcp4_c_socket_packet_new(int *sockfdp, int ifindex) {
_c_cleanup_(c_closep) int sockfd = -1;
struct sock_filter filter[] = {
/*
* IP
*
* Check
* - UDP
* - Unfragmented
* - Large enough to fit the DHCP header
*
* Leave X the size of the IP header, for future indirect reads.
*/
BPF_STMT(BPF_LD + BPF_B + BPF_ABS, offsetof(struct iphdr, protocol)), /* A <- IP protocol */
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 1, 0), /* IP protocol == UDP ? */
BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
BPF_STMT(BPF_LD + BPF_B + BPF_ABS, offsetof(struct iphdr, frag_off)), /* A <- Flags */
BPF_STMT(BPF_ALU + BPF_AND + BPF_K, ntohs(IP_MF | IP_OFFMASK)), /* A <- A & (IP_MF | IP_OFFMASK) */
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0, 1, 0), /* fragmented packet ? */
BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 0), /* X <- IP header length */
BPF_STMT(BPF_LD + BPF_W + BPF_LEN, 0), /* A <- packet length */
BPF_STMT(BPF_ALU + BPF_SUB + BPF_X, 0), /* A -= X */
BPF_JUMP(BPF_JMP + BPF_JGE + BPF_K, sizeof(struct udphdr) + sizeof(NDhcp4Message), 1, 0), /* packet >= DHCPPacket ? */
BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
/*
* UDP
*
* Check
* - DHCP client port
*
* Leave X the size of IP and UDP headers, for future indirect reads.
*/
BPF_STMT(BPF_LD + BPF_H + BPF_IND, offsetof(struct udphdr, dest)), /* A <- UDP destination port */
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, N_DHCP4_NETWORK_CLIENT_PORT, 1, 0), /* UDP destination port == DHCP client port ? */
BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
BPF_STMT(BPF_LD + BPF_W + BPF_K, sizeof(struct udphdr)), /* A <- size of UDP header */
BPF_STMT(BPF_ALU + BPF_ADD + BPF_X, 0), /* A += X */
BPF_STMT(BPF_MISC + BPF_TAX, 0), /* X <- A */
/*
* DHCP
*
* Check
* - BOOTREPLY (from server to client)
* - DHCP magic cookie
*/
BPF_STMT(BPF_LD + BPF_B + BPF_IND, offsetof(NDhcp4Header, op)), /* A <- DHCP op */
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, N_DHCP4_OP_BOOTREPLY, 1, 0), /* op == BOOTREPLY ? */
BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
BPF_STMT(BPF_LD + BPF_W + BPF_IND, offsetof(NDhcp4Message, magic)), /* A <- DHCP magic cookie */
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, N_DHCP4_MESSAGE_MAGIC, 1, 0), /* cookie == DHCP magic cookie ? */
BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
BPF_STMT(BPF_RET + BPF_K, 65535), /* return all */
};
struct sock_fprog fprog = {
.filter = filter,
.len = sizeof(filter) / sizeof(filter[0]),
};
struct sockaddr_ll addr = {
.sll_family = AF_PACKET,
.sll_protocol = htons(ETH_P_IP),
.sll_ifindex = ifindex,
};
int r, on = 1;
sockfd = socket(AF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
if (sockfd < 0)
return -errno;
r = setsockopt(sockfd, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog));
if (r < 0)
return -errno;
/* We need the flag that tells us if the checksum is correct. */
r = setsockopt(sockfd, SOL_PACKET, PACKET_AUXDATA, &on, sizeof(on));
if (r < 0)
return -errno;
r = bind(sockfd, (struct sockaddr*)&addr, sizeof(addr));
if (r < 0)
return -errno;
*sockfdp = sockfd;
sockfd = -1;
return 0;
}
/**
* n_dhcp4_c_socket_udp_new() - create a new DHCP4 client UDP socket
* @sockfdp: return argumnet for the new socket
* @ifindex: interface index to bind to
* @client_addr: client address to bind to
* @server_addr: server address to connect to
*
* Create a new AF_INET/SOCK_DGRAM socket usable to listen to and send DHCP client
* packets.
*
* The client address given in @addr must be configured on the interface @ifindex
* before the socket is created.
*
* Return: 0 on success, or a negative error code on failure.
*/
int n_dhcp4_c_socket_udp_new(int *sockfdp,
int ifindex,
const struct in_addr *client_addr,
const struct in_addr *server_addr) {
_c_cleanup_(c_closep) int sockfd = -1;
struct sock_filter filter[] = {
/*
* IP/UDP
*
* Set X to the size of IP and UDP headers, for future indirect reads.
*/
BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 0), /* X <- IP header length */
BPF_STMT(BPF_LD + BPF_W + BPF_K, sizeof(struct udphdr)), /* A <- size of UDP header */
BPF_STMT(BPF_ALU + BPF_ADD + BPF_X, 0), /* A += X */
BPF_STMT(BPF_MISC + BPF_TAX, 0), /* X <- A */
/*
* DHCP
*
* Check
* - BOOTREPLY (from server to client)
* - DHCP magic cookie
*/
BPF_STMT(BPF_LD + BPF_B + BPF_IND, offsetof(NDhcp4Header, op)), /* A <- DHCP op */
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, N_DHCP4_OP_BOOTREPLY, 1, 0), /* op == BOOTREPLY ? */
BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
BPF_STMT(BPF_LD + BPF_W + BPF_IND, offsetof(NDhcp4Message, magic)), /* A <- DHCP magic cookie */
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, N_DHCP4_MESSAGE_MAGIC, 1, 0), /* cookie == DHCP magic cookie ? */
BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
BPF_STMT(BPF_RET + BPF_K, 65535), /* return all */
};
struct sock_fprog fprog = {
.filter = filter,
.len = sizeof(filter) / sizeof(filter[0]),
};
struct sockaddr_in saddr = {
.sin_family = AF_INET,
.sin_addr = *client_addr,
.sin_port = htons(N_DHCP4_NETWORK_CLIENT_PORT),
};
struct sockaddr_in daddr = {
.sin_family = AF_INET,
.sin_addr = *server_addr,
.sin_port = htons(N_DHCP4_NETWORK_SERVER_PORT),
};
int r, tos = IPTOS_CLASS_CS6, on = 1;
sockfd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
if (sockfd < 0)
return -errno;
r = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if (r < 0)
return -errno;
r = setsockopt(sockfd, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog));
if (r < 0)
return -errno;
r = socket_bind_if(sockfd, ifindex);
if (r)
return r;
r = setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
if (r < 0)
return -errno;
r = setsockopt(sockfd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
if (r < 0)
return -errno;
r = bind(sockfd, (struct sockaddr*)&saddr, sizeof(saddr));
if (r < 0)
return -errno;
r = connect(sockfd, (struct sockaddr*)&daddr, sizeof(daddr));
if (r < 0)
return -errno;
*sockfdp = sockfd;
sockfd = -1;
return 0;
}
/**
* n_dhcp4_s_socket_packet_new() - create a new DHCP4 server packet socket
* @sockfdp: return argumnet for the new socket
*
* Create a new AF_PACKET/SOCK_DGRAM socket usable to send DHCP packets to clients
* before they have an IP address configured, on the given interface.
*
* Return: 0 on success, or a negative error code on failure.
*/
int n_dhcp4_s_socket_packet_new(int *sockfdp) {
_c_cleanup_(c_closep) int sockfd = -1;
sockfd = socket(AF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
if (sockfd < 0)
return -errno;
*sockfdp = sockfd;
sockfd = -1;
return 0;
}
/**
* n_dhcp4_s_socket_udp_new() - create a new DHCP4 server UDP socket
* @sockfdp: return argumnet for the new socket
* @ifindex: intercafe index to bind to
*
* Create a new AF_INET/SOCK_DGRAM socket usable to listen to DHCP server packets,
* on the given interface.
*
* Return: 0 on success, or a negative error code on failure.
*/
int n_dhcp4_s_socket_udp_new(int *sockfdp, int ifindex) {
_c_cleanup_(c_closep) int sockfd = -1;
struct sock_filter filter[] = {
/*
* IP/UDP
*
* Set X to the size of IP and UDP headers, for future indirect reads.
*/
BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 0), /* X <- IP header length */
BPF_STMT(BPF_LD + BPF_W + BPF_K, sizeof(struct udphdr)), /* A <- size of UDP header */
BPF_STMT(BPF_ALU + BPF_ADD + BPF_X, 0), /* A += X */
BPF_STMT(BPF_MISC + BPF_TAX, 0), /* X <- A */
/*
* DHCP
*
* Check
* - BOOTREQUEST (from client to server)
* - DHCP magic cookie
*/
BPF_STMT(BPF_LD + BPF_B + BPF_IND, offsetof(NDhcp4Header, op)), /* A <- DHCP op */
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, N_DHCP4_OP_BOOTREQUEST, 1, 0), /* op == BOOTREQUEST ? */
BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
BPF_STMT(BPF_LD + BPF_W + BPF_IND, offsetof(NDhcp4Message, magic)), /* A <- DHCP magic cookie */
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, N_DHCP4_MESSAGE_MAGIC, 1, 0), /* cookie == DHCP magic cookie ? */
BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
BPF_STMT(BPF_RET + BPF_K, 65535), /* return all */
};
struct sock_fprog fprog = {
.filter = filter,
.len = sizeof(filter) / sizeof(filter[0]),
};
struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_addr = { INADDR_ANY },
.sin_port = htons(N_DHCP4_NETWORK_SERVER_PORT),
};
int r, tos = IPTOS_CLASS_CS6, on = 1;
sockfd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
if (sockfd < 0)
return -errno;
r = setsockopt(sockfd, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog));
if (r < 0)
return -errno;
r = socket_bind_if(sockfd, ifindex);
if (r)
return r;
r = setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
if (r < 0)
return -errno;
r = setsockopt(sockfd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
if (r < 0)
return -errno;
r = setsockopt(sockfd, IPPROTO_IP, IP_PKTINFO, &on, sizeof(on));
if (r < 0)
return -errno;
r = bind(sockfd, (struct sockaddr*)&addr, sizeof(addr));
if (r < 0)
return -errno;
*sockfdp = sockfd;
sockfd = -1;
return 0;
}
static int n_dhcp4_socket_packet_send(int sockfd,
int ifindex,
const struct sockaddr_in *src_paddr,
const unsigned char *dest_haddr,
unsigned char halen,
const struct sockaddr_in *dest_paddr,
NDhcp4Outgoing *message) {
struct packet_sockaddr_ll haddr = {
.sll_family = AF_PACKET,
.sll_protocol = htons(ETH_P_IP),
.sll_ifindex = ifindex,
.sll_halen = halen,
};
const void *buf;
size_t n_buf, len;
int r;
c_assert(halen <= sizeof(haddr.sll_addr));
memcpy(haddr.sll_addr, dest_haddr, halen);
n_buf = n_dhcp4_outgoing_get_raw(message, &buf);
r = packet_sendto_udp(sockfd, buf, n_buf, &len, src_paddr, &haddr, dest_paddr);
if (r < 0) {
if (r == -EAGAIN || r == -ENOBUFS)
return N_DHCP4_E_DROPPED;
else if (r == -ENETDOWN || r == -ENXIO)
return N_DHCP4_E_DOWN;
else
return r;
} else if (len != n_buf) {
return N_DHCP4_E_DROPPED;
}
return 0;
}
/**
* n_dhcp4_c_socket_packet_send() - XXX
*/
int n_dhcp4_c_socket_packet_send(int sockfd,
int ifindex,
const unsigned char *dest_haddr,
unsigned char halen,
NDhcp4Outgoing *message) {
struct sockaddr_in src_paddr = {
.sin_family = AF_INET,
.sin_port = htons(N_DHCP4_NETWORK_CLIENT_PORT),
.sin_addr = { INADDR_ANY },
};
struct sockaddr_in dest_paddr = {
.sin_family = AF_INET,
.sin_port = htons(N_DHCP4_NETWORK_SERVER_PORT),
.sin_addr = { INADDR_BROADCAST }
};
return n_dhcp4_socket_packet_send(sockfd,
ifindex,
&src_paddr,
dest_haddr,
halen,
&dest_paddr,
message);
}
/**
* n_dhcp4_c_socket_udp_send() - XXX
*/
int n_dhcp4_c_socket_udp_send(int sockfd,
NDhcp4Outgoing *message) {
const void *buf;
size_t n_buf;
ssize_t len;
n_buf = n_dhcp4_outgoing_get_raw(message, &buf);
len = send(sockfd, buf, n_buf, 0);
if (len < 0) {
if (errno == EAGAIN || errno == ENOBUFS)
return N_DHCP4_E_DROPPED;
else if (errno == ENETDOWN || errno == ENXIO)
return N_DHCP4_E_DOWN;
else
return -errno;
} else if ((size_t)len != n_buf)
return N_DHCP4_E_DROPPED;
return 0;
}
/**
* n_dhcp4_c_socket_udp_broadcast() - XXX
*/
int n_dhcp4_c_socket_udp_broadcast(int sockfd, NDhcp4Outgoing *message) {
struct sockaddr_in sockaddr_dest = {
.sin_family = AF_INET,
.sin_port = htons(N_DHCP4_NETWORK_SERVER_PORT),
.sin_addr = { INADDR_BROADCAST },
};
const void *buf;
size_t n_buf;
ssize_t len;
n_buf = n_dhcp4_outgoing_get_raw(message, &buf);
len = sendto(sockfd,
buf,
n_buf,
0,
(struct sockaddr*)&sockaddr_dest,
sizeof(sockaddr_dest));
if (len < 0) {
if (errno == EAGAIN || errno == ENOBUFS)
return N_DHCP4_E_DROPPED;
else if (errno == ENETDOWN || errno == ENXIO)
return N_DHCP4_E_DOWN;
else
return -errno;
} else if ((size_t)len != n_buf)
return N_DHCP4_E_DROPPED;
return 0;
}
/**
* n_dhcp4_s_socket_packet_send() - XXX
*/
int n_dhcp4_s_socket_packet_send(int sockfd,
int ifindex,
const struct in_addr *src_inaddr,
const unsigned char *dest_haddr,
unsigned char halen,
const struct in_addr *dest_inaddr,
NDhcp4Outgoing *message) {
struct sockaddr_in src_paddr = {
.sin_family = AF_INET,
.sin_port = htons(N_DHCP4_NETWORK_SERVER_PORT),
.sin_addr = *src_inaddr,
};
struct sockaddr_in dest_paddr = {
.sin_family = AF_INET,
.sin_port = htons(N_DHCP4_NETWORK_CLIENT_PORT),
.sin_addr = *dest_inaddr,
};
return n_dhcp4_socket_packet_send(sockfd,
ifindex,
&src_paddr,
dest_haddr,
halen,
&dest_paddr,
message);
}
/**
* n_dhcp4_s_socket_udp_send() - XXX
*/
int n_dhcp4_s_socket_udp_send(int sockfd,
const struct in_addr *inaddr_src,
const struct in_addr *inaddr_dest,
NDhcp4Outgoing *message) {
struct sockaddr_in sockaddr_dest = {
.sin_family = AF_INET,
.sin_port = htons(N_DHCP4_NETWORK_CLIENT_PORT),
.sin_addr = *inaddr_dest,
};
struct iovec iov = {};
union {
struct cmsghdr align; /* ensure correct stack alignment */
char buf[CMSG_SPACE(sizeof(struct in_pktinfo))];
} control = {};
struct in_pktinfo pktinfo = {
.ipi_spec_dst = *inaddr_src,
};
struct msghdr msg = {
.msg_name = (void*)&sockaddr_dest,
.msg_namelen = sizeof(sockaddr_dest),
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_control = &control.buf,
.msg_controllen = sizeof(control.buf),
};
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
ssize_t len;
cmsg->cmsg_level = IPPROTO_IP;
cmsg->cmsg_type = IP_PKTINFO;
cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
memcpy(CMSG_DATA(cmsg), &pktinfo, sizeof(pktinfo));
iov.iov_len = n_dhcp4_outgoing_get_raw(message, (const void **)&iov.iov_base);
len = sendmsg(sockfd, &msg, 0);
if (len < 0) {
if (errno == EAGAIN || errno == ENOBUFS)
return N_DHCP4_E_DROPPED;
else if (errno == ENETDOWN || errno == ENXIO)
return N_DHCP4_E_DOWN;
else
return -errno;
} else if ((size_t)len != iov.iov_len)
return N_DHCP4_E_DROPPED;
return 0;
}
int n_dhcp4_s_socket_udp_broadcast(int sockfd,
const struct in_addr *inaddr_src,
NDhcp4Outgoing *message) {
return n_dhcp4_s_socket_udp_send(sockfd,
inaddr_src,
&(const struct in_addr){INADDR_BROADCAST},
message);
}
int n_dhcp4_c_socket_packet_recv(int sockfd,
uint8_t *buf,
size_t n_buf,
NDhcp4Incoming **messagep) {
_c_cleanup_(n_dhcp4_incoming_freep) NDhcp4Incoming *message = NULL;
size_t len;
int r;
r = packet_recv_udp(sockfd, buf, n_buf, &len);
if (r < 0) {
if (r == -ENETDOWN)
return N_DHCP4_E_DOWN;
else if (r == -EAGAIN)
return N_DHCP4_E_AGAIN;
else
return -errno;
} else if (len == 0) {
return N_DHCP4_E_MALFORMED;
}
r = n_dhcp4_incoming_new(&message, buf, len);
if (r)
return r;
*messagep = message;
message = NULL;
return 0;
}
static int n_dhcp4_socket_udp_recv(int sockfd,
uint8_t *buf,
size_t n_buf,
NDhcp4Incoming **messagep,
struct in_pktinfo *pktinfo) {
_c_cleanup_(n_dhcp4_incoming_freep) NDhcp4Incoming *message = NULL;
struct iovec iov = {
.iov_base = buf,
.iov_len = n_buf,
};
uint8_t cmsgbuf[CMSG_LEN(sizeof(struct in_pktinfo))];
struct msghdr msg = {
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_control = cmsgbuf,
.msg_controllen = sizeof(cmsgbuf),
};
ssize_t len;
int r;
len = recvmsg(sockfd, &msg, MSG_TRUNC);
if (len < 0) {
if (errno == ENETDOWN)
return N_DHCP4_E_DOWN;
else if (errno == EAGAIN)
return N_DHCP4_E_AGAIN;
else
return -errno;
} else if (len == 0 || (size_t)len > n_buf) {
return N_DHCP4_E_MALFORMED;
}
r = n_dhcp4_incoming_new(&message, buf, len);
if (r)
return r;
if (pktinfo) {
struct cmsghdr *cmsg;
cmsg = CMSG_FIRSTHDR(&msg);
c_assert(cmsg);
c_assert(cmsg->cmsg_level == IPPROTO_IP);
c_assert(cmsg->cmsg_type == IP_PKTINFO);
c_assert(cmsg->cmsg_len == CMSG_LEN(sizeof(struct in_pktinfo)));
memcpy(pktinfo, (void*)CMSG_DATA(cmsg), sizeof(struct in_pktinfo));
}
*messagep = message;
message = NULL;
return 0;
}
int n_dhcp4_c_socket_udp_recv(int sockfd,
uint8_t *buf,
size_t n_buf,
NDhcp4Incoming **messagep) {
return n_dhcp4_socket_udp_recv(sockfd, buf, n_buf, messagep, NULL);
}
int n_dhcp4_s_socket_udp_recv(int sockfd,
uint8_t *buf,
size_t n_buf,
NDhcp4Incoming **messagep,
struct sockaddr_in *dest) {
struct in_pktinfo pktinfo = {};
int r;
r = n_dhcp4_socket_udp_recv(sockfd, buf, n_buf, messagep, &pktinfo);
if (r)
return r;
if (dest) {
dest->sin_family = AF_INET;
dest->sin_port = htons(N_DHCP4_NETWORK_SERVER_PORT);
dest->sin_addr.s_addr = pktinfo.ipi_addr.s_addr;
}
return 0;
}
|