/
protocol.c
1367 lines (1181 loc) · 34.6 KB
/
protocol.c
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Build and parse DHCP4 packets
*
* Copyright (C) 2010-2012, Olaf Kirch <okir@suse.de>
*
* Heavily inspired by dhcp4cd, which was written by Roy Marples <roy@marples.name>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <time.h>
#include <assert.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <net/if_arp.h>
#include <net/ethernet.h>
#include <arpa/inet.h>
#include <errno.h>
#include <limits.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <wicked/netinfo.h>
#include <wicked/route.h>
#include <wicked/logging.h>
#include <wicked/socket.h>
#include <wicked/resolver.h>
#include <wicked/nis.h>
#include "dhcp4/dhcp.h"
#include "dhcp4/protocol.h"
#include "buffer.h"
#include "socket_priv.h"
static void ni_dhcp4_socket_recv(ni_socket_t *);
/*
* Open a DHCP4 socket for send and receive
*/
int
ni_dhcp4_socket_open(ni_dhcp4_device_t *dev)
{
ni_capture_protinfo_t prot_info;
ni_capture_t *capture;
/* We need to bind to a port, otherwise Linux will generate
* ICMP_UNREACHABLE messages telling the server that there's
* no DHCP4 client listening at all.
*
* We don't actually use this fd at all, instead using our packet
* filter socket.
*
* (It would be nice if we did, at least in BOUND/RENEWING state
* where good manners would dictate unicast requests anyway).
*/
if (dev->listen_fd == -1) {
struct sockaddr_in sin;
struct ifreq ifr;
int on = 1;
int fd;
if ((fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
ni_error("socket: %m");
return -1;
}
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
ni_error("SO_REUSEADDR: %m");
if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &on, sizeof(on)) == -1)
ni_error("SO_RCVBUF: %m");
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name));
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)) == -1)
ni_error("SO_SOBINDTODEVICE: %m");
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(DHCP4_CLIENT_PORT);
if (bind(fd, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
ni_error("bind: %m");
close(fd);
} else {
dev->listen_fd = fd;
fcntl(fd, F_SETFD, FD_CLOEXEC);
}
}
memset(&prot_info, 0, sizeof(prot_info));
prot_info.eth_protocol = ETHERTYPE_IP;
prot_info.ip_protocol = IPPROTO_UDP;
prot_info.ip_port = DHCP4_CLIENT_PORT;
if ((capture = dev->capture) != NULL) {
if (ni_capture_is_valid(capture, ETHERTYPE_IP))
return 0;
ni_capture_free(dev->capture);
dev->capture = NULL;
}
dev->capture = ni_capture_open(&dev->system, &prot_info, ni_dhcp4_socket_recv);
if (!dev->capture)
return -1;
ni_capture_set_user_data(dev->capture, dev);
return 0;
}
/*
* This callback is invoked from the socket code when we
* detect an incoming DHCP4 packet on the raw socket.
*/
static void
ni_dhcp4_socket_recv(ni_socket_t *sock)
{
ni_capture_t *capture = sock->user_data;
ni_buffer_t buf;
if (ni_capture_recv(capture, &buf) >= 0) {
ni_dhcp4_device_t *dev = ni_capture_get_user_data(capture);
ni_dhcp4_fsm_process_dhcp4_packet(dev, &buf);
}
}
/*
* Inline functions for setting/retrieving options from a buffer
*/
static inline void
ni_dhcp4_option_put(ni_buffer_t *bp, int code, const void *data, size_t len)
{
ni_buffer_putc(bp, code);
ni_buffer_putc(bp, len);
ni_buffer_put(bp, data, len);
}
static inline void
ni_dhcp4_option_put_empty(ni_buffer_t *bp, int code)
{
ni_dhcp4_option_put(bp, code, NULL, 0);
}
static inline void
ni_dhcp4_option_put8(ni_buffer_t *bp, int code, unsigned char value)
{
ni_dhcp4_option_put(bp, code, &value, 1);
}
static inline void
ni_dhcp4_option_put16(ni_buffer_t *bp, int code, uint16_t value)
{
value = htons(value);
ni_dhcp4_option_put(bp, code, &value, 2);
}
static inline void
ni_dhcp4_option_put32(ni_buffer_t *bp, int code, uint32_t value)
{
value = htonl(value);
ni_dhcp4_option_put(bp, code, &value, 4);
}
static inline void
ni_dhcp4_option_put_ipv4(ni_buffer_t *bp, int code, struct in_addr addr)
{
ni_dhcp4_option_put(bp, code, &addr, 4);
}
static inline void
ni_dhcp4_option_puts(ni_buffer_t *bp, int code, const char *string)
{
ni_dhcp4_option_put(bp, code, string, strlen(string));
}
static inline unsigned int
ni_dhcp4_option_begin(ni_buffer_t *bp, int code)
{
ni_buffer_putc(bp, code);
ni_buffer_putc(bp, 0);
return bp->tail;
}
static inline void
ni_dhcp4_option_end(ni_buffer_t *bp, unsigned int pos)
{
if (pos == 0 || pos > bp->size) {
ni_error("ni_dhcp4_option_end: bad offset!");
} else {
bp->base[pos-1] = bp->tail - pos;
}
}
static int
ni_dhcp4_option_next(ni_buffer_t *bp, ni_buffer_t *optbuf)
{
unsigned char code, count;
if (bp->underflow)
return -1;
if (bp->head == bp->tail)
return DHCP4_END;
if (bp->tail - bp->head < 2)
goto underflow;
code = bp->base[bp->head++];
if (code != DHCP4_PAD && code != DHCP4_END) {
count = bp->base[bp->head++];
if (bp->tail - bp->head < count)
goto underflow;
ni_buffer_init_reader(optbuf, bp->base + bp->head, count);
bp->head += count;
} else {
memset(optbuf, 0, sizeof(*optbuf));
}
return code;
underflow:
bp->underflow = 1;
return -1;
}
static int
ni_dhcp4_option_get_sockaddr(ni_buffer_t *bp, ni_sockaddr_t *addr)
{
struct sockaddr_in *sin = &addr->sin;
memset(sin, 0, sizeof(*addr));
sin->sin_family = AF_INET;
return ni_buffer_get(bp, &sin->sin_addr, 4);
}
static int
ni_dhcp4_option_get_ipv4(ni_buffer_t *bp, struct in_addr *addr)
{
return ni_buffer_get(bp, addr, 4);
}
static int
ni_dhcp4_option_get16(ni_buffer_t *bp, uint16_t *var)
{
if (ni_buffer_get(bp, var, 2) < 0)
return -1;
*var = ntohs(*var);
return 0;
}
static int
ni_dhcp4_option_get32(ni_buffer_t *bp, uint32_t *var)
{
if (ni_buffer_get(bp, var, 4) < 0)
return -1;
*var = ntohl(*var);
return 0;
}
static int
ni_dhcp4_option_get_string(ni_buffer_t *bp, char **var, unsigned int *lenp)
{
unsigned int len = ni_buffer_count(bp);
if (len == 0)
return -1;
if (lenp)
*lenp = len;
if (*var)
free(*var);
*var = malloc(len + 1);
ni_buffer_get(bp, *var, len);
(*var)[len] = '\0';
return 0;
}
int
ni_dhcp4_build_message(const ni_dhcp4_device_t *dev,
unsigned int msg_code,
const ni_addrconf_lease_t *lease,
ni_buffer_t *msgbuf)
{
const ni_dhcp4_config_t *options = dev->config;
struct in_addr src_addr, dst_addr;
ni_dhcp4_message_t *message = NULL;
if (!options || !lease)
return -1;
if (IN_LINKLOCAL(ntohl(lease->dhcp4.address.s_addr))) {
ni_error("cannot request a link local address");
goto failed;
}
src_addr.s_addr = dst_addr.s_addr = 0;
switch (msg_code) {
case DHCP4_DISCOVER:
if (lease->dhcp4.server_id.s_addr != 0)
return -1;
break;
case DHCP4_REQUEST:
case DHCP4_RELEASE:
case DHCP4_INFORM:
if (lease->dhcp4.address.s_addr == 0 || lease->dhcp4.server_id.s_addr == 0)
return -1;
if (dev->fsm.state != NI_DHCP4_STATE_REQUESTING
&& dev->fsm.state != NI_DHCP4_STATE_REBOOT) {
src_addr = lease->dhcp4.address;
dst_addr = lease->dhcp4.server_id;
}
break;
}
/* Reserve some room for the IP and UDP header */
ni_buffer_reserve_head(msgbuf, sizeof(struct ip) + sizeof(struct udphdr));
/* Build the message */
message = ni_buffer_push_tail(msgbuf, sizeof(*message));
message->op = DHCP4_BOOTREQUEST;
message->hwtype = dev->system.hwaddr.type;
message->xid = dev->dhcp4.xid;
message->cookie = htonl(MAGIC_COOKIE);
message->secs = htons(ni_dhcp4_device_uptime(dev, 0xFFFF));
if (dev->fsm.state == NI_DHCP4_STATE_BOUND
|| dev->fsm.state == NI_DHCP4_STATE_REBINDING
|| dev->fsm.state == NI_DHCP4_STATE_RENEWING)
message->ciaddr = lease->dhcp4.address.s_addr;
switch (dev->system.hwaddr.type) {
case ARPHRD_ETHER:
case ARPHRD_IEEE802:
if (dev->system.hwaddr.len > sizeof(message->chaddr)) {
ni_error("dhcp4 cannot handle hwaddress length %u",
dev->system.hwaddr.len);
goto failed;
}
message->hwlen = dev->system.hwaddr.len;
memcpy(&message->chaddr, dev->system.hwaddr.data, dev->system.hwaddr.len);
break;
case ARPHRD_IEEE1394:
case ARPHRD_INFINIBAND:
message->hwlen = 0;
if (message->ciaddr == 0)
message->flags = htons(BROADCAST_FLAG);
break;
default:
ni_error("dhcp4: unknown hardware type 0x%x", dev->system.hwaddr.type);
}
ni_dhcp4_option_put8(msgbuf, DHCP4_MESSAGETYPE, msg_code);
if (msg_code == DHCP4_REQUEST)
ni_dhcp4_option_put16(msgbuf, DHCP4_MAXMESSAGESIZE, dev->system.mtu);
ni_dhcp4_option_put(msgbuf, DHCP4_CLIENTID,
options->client_id.data,
options->client_id.len);
if (msg_code != DHCP4_DECLINE && msg_code != DHCP4_RELEASE) {
if (options->userclass.len > 0)
ni_dhcp4_option_put(msgbuf, DHCP4_USERCLASS,
options->userclass.data,
options->userclass.len);
if (options->classid && options->classid[0])
ni_dhcp4_option_puts(msgbuf, DHCP4_CLASSID, options->classid);
}
if (msg_code == DHCP4_DISCOVER || msg_code == DHCP4_REQUEST) {
if (lease->dhcp4.address.s_addr)
ni_dhcp4_option_put_ipv4(msgbuf, DHCP4_ADDRESS, lease->dhcp4.address);
if (lease->dhcp4.lease_time != 0)
ni_dhcp4_option_put32(msgbuf, DHCP4_LEASETIME, lease->dhcp4.lease_time);
}
if (msg_code == DHCP4_REQUEST) {
if (lease->dhcp4.server_id.s_addr)
ni_dhcp4_option_put_ipv4(msgbuf, DHCP4_SERVERIDENTIFIER, lease->dhcp4.server_id);
}
if (msg_code == DHCP4_DISCOVER || msg_code == DHCP4_INFORM || msg_code == DHCP4_REQUEST) {
unsigned int params_begin;
if (options->hostname && options->hostname[0]) {
if (options->fqdn == FQDN_DISABLE) {
ni_dhcp4_option_puts(msgbuf, DHCP4_HOSTNAME,
options->hostname);
} else {
/* IETF DHC-FQDN option(81)
* http://tools.ietf.org/html/rfc4702#section-2.1
*
* Flags: 0000NEOS
* S: 1 => Client requests Server to update
* a RR in DNS as well as PTR
* O: 1 => Server indicates to client that
* DNS has been updated
* E: 1 => Name data is DNS format
* N: 1 => Client requests Server to not
* update DNS
*/
ni_buffer_putc(msgbuf, DHCP4_FQDN);
ni_buffer_putc(msgbuf, strlen(options->hostname) + 3);
ni_buffer_putc(msgbuf, options->fqdn & 0x9);
ni_buffer_putc(msgbuf, 0); /* from server for PTR RR */
ni_buffer_putc(msgbuf, 0); /* from server for A RR if S=1 */
ni_buffer_put(msgbuf, options->hostname, strlen(options->hostname));
}
}
params_begin = ni_dhcp4_option_begin(msgbuf, DHCP4_PARAMETERREQUESTLIST);
if (msg_code != DHCP4_INFORM) {
ni_buffer_putc(msgbuf, DHCP4_RENEWALTIME);
ni_buffer_putc(msgbuf, DHCP4_REBINDTIME);
}
ni_buffer_putc(msgbuf, DHCP4_NETMASK);
ni_buffer_putc(msgbuf, DHCP4_BROADCAST);
ni_buffer_putc(msgbuf, DHCP4_MTU);
if (options->flags & DHCP4_DO_CSR) {
ni_buffer_putc(msgbuf, DHCP4_CSR);
}
if (options->flags & DHCP4_DO_MSCSR) {
ni_buffer_putc(msgbuf, DHCP4_MSCSR);
}
/* RFC 3442 states classless static routes should be
* before routers and static routes as classless static
* routes override them both */
if (options->flags & DHCP4_DO_GATEWAY) {
ni_buffer_putc(msgbuf, DHCP4_STATICROUTE);
ni_buffer_putc(msgbuf, DHCP4_ROUTERS);
}
if (options->flags & DHCP4_DO_HOSTNAME) {
if (options->fqdn == FQDN_DISABLE)
ni_buffer_putc(msgbuf, DHCP4_HOSTNAME);
else
ni_buffer_putc(msgbuf, DHCP4_FQDN);
}
if (options->flags & DHCP4_DO_RESOLVER) {
ni_buffer_putc(msgbuf, DHCP4_DNSSEARCH);
ni_buffer_putc(msgbuf, DHCP4_DNSDOMAIN);
ni_buffer_putc(msgbuf, DHCP4_DNSSERVER);
}
if (options->flags & DHCP4_DO_NIS) {
ni_buffer_putc(msgbuf, DHCP4_NISDOMAIN);
ni_buffer_putc(msgbuf, DHCP4_NISSERVER);
}
if (options->flags & DHCP4_DO_NTP) {
ni_buffer_putc(msgbuf, DHCP4_NTPSERVER);
}
if (options->flags & DHCP4_DO_ROOT) {
ni_buffer_putc(msgbuf, DHCP4_ROOTPATH);
}
if (options->flags & DHCP4_DO_LPR) {
ni_buffer_putc(msgbuf, DHCP4_LPRSERVER);
}
if (options->flags & DHCP4_DO_LOG) {
ni_buffer_putc(msgbuf, DHCP4_LOGSERVER);
}
if (options->flags & DHCP4_DO_NDS) {
ni_buffer_putc(msgbuf, DHCP4_NDS_SERVER);
ni_buffer_putc(msgbuf, DHCP4_NDS_TREE);
ni_buffer_putc(msgbuf, DHCP4_NDS_CTX);
}
if (options->flags & DHCP4_DO_SIP) {
ni_buffer_putc(msgbuf, DHCP4_SIPSERVER);
}
if (options->flags & DHCP4_DO_SMB) {
ni_buffer_putc(msgbuf, DHCP4_NETBIOSNAMESERVER);
ni_buffer_putc(msgbuf, DHCP4_NETBIOSDDSERVER);
ni_buffer_putc(msgbuf, DHCP4_NETBIOSNODETYPE);
ni_buffer_putc(msgbuf, DHCP4_NETBIOSSCOPE);
}
if (options->flags & DHCP4_DO_POSIX_TZ) {
ni_buffer_putc(msgbuf, DHCP4_POSIX_TZ_STRING);
ni_buffer_putc(msgbuf, DHCP4_POSIX_TZ_DBNAME);
}
ni_dhcp4_option_end(msgbuf, params_begin);
}
ni_buffer_putc(msgbuf, DHCP4_END);
#ifdef BOOTP_MESSAGE_LENGTH_MIN
ni_buffer_pad(msgbuf, BOOTP_MESSAGE_LENGTH_MIN, DHCP4_PAD);
#endif
if (ni_capture_build_udp_header(msgbuf, src_addr, DHCP4_CLIENT_PORT, dst_addr, DHCP4_SERVER_PORT) < 0) {
ni_error("unable to build packet header");
goto failed;
}
return 0;
failed:
return -1;
}
/*
* Decode an RFC3397 DNS search order option.
*/
static int
ni_dhcp4_decode_dnssearch(ni_buffer_t *optbuf, ni_string_array_t *list, const char *what)
{
ni_stringbuf_t namebuf = NI_STRINGBUF_INIT_DYNAMIC;
unsigned char *base = ni_buffer_head(optbuf);
unsigned int base_offset = optbuf->head;
size_t len;
ni_string_array_destroy(list);
while (ni_buffer_count(optbuf) && !optbuf->underflow) {
ni_buffer_t *bp = optbuf;
ni_buffer_t jumpbuf;
while (1) {
unsigned int pos = bp->head - base_offset;
unsigned int pointer;
char label[64];
int length;
if ((length = ni_buffer_getc(bp)) < 0)
goto failure; /* unexpected EOF */
if (length == 0)
break; /* end of this name */
switch (length & 0xC0) {
case 0:
/* Plain name component */
if (ni_buffer_get(bp, label, length) < 0)
goto failure;
label[length] = '\0';
if (!ni_stringbuf_empty(&namebuf))
ni_stringbuf_putc(&namebuf, '.');
ni_stringbuf_puts(&namebuf, label);
break;
case 0xC0:
/* Pointer */
pointer = (length & 0x3F) << 8;
if ((length = ni_buffer_getc(bp)) < 0)
goto failure;
pointer |= length;
if (pointer >= pos)
goto failure;
ni_buffer_init_reader(&jumpbuf, base, pos);
jumpbuf.head = pointer;
bp = &jumpbuf;
break;
default:
goto failure;
}
}
if (!ni_stringbuf_empty(&namebuf)) {
len = ni_string_len(namebuf.string);
if (ni_check_domain_name(namebuf.string, len, 0)) {
ni_string_array_append(list, namebuf.string);
} else {
ni_warn("Discarded suspect %s: '%s'", what,
ni_print_suspect(namebuf.string, len));
}
}
ni_stringbuf_destroy(&namebuf);
}
return 0;
failure:
ni_stringbuf_destroy(&namebuf);
ni_string_array_destroy(list);
return -1;
}
/*
* Decode a CIDR list option.
*/
static int
ni_dhcp4_decode_csr(ni_buffer_t *bp, ni_route_array_t *routes)
{
while (ni_buffer_count(bp) && !bp->underflow) {
ni_sockaddr_t destination, gateway;
struct in_addr prefix = { 0 };
unsigned int prefix_len;
ni_route_t *rp;
prefix_len = ni_buffer_getc(bp);
if (prefix_len > 32) {
ni_error("invalid prefix len of %u in classless static route", prefix_len);
return -1;
}
if (prefix_len)
ni_buffer_get(bp, &prefix, (prefix_len + 7) / 8);
ni_sockaddr_set_ipv4(&destination, prefix, 0);
if (ni_dhcp4_option_get_sockaddr(bp, &gateway) < 0)
return -1;
rp = ni_route_create(prefix_len, &destination, &gateway, 0, NULL);
ni_route_array_append(routes, rp);
}
if (bp->underflow)
return -1;
return 0;
}
static int
ni_dhcp4_decode_address_list(ni_buffer_t *bp, ni_string_array_t *list)
{
while (ni_buffer_count(bp) && !bp->underflow) {
struct in_addr addr;
if (ni_dhcp4_option_get_ipv4(bp, &addr) < 0)
return -1;
ni_string_array_append(list, inet_ntoa(addr));
}
if (bp->underflow)
return -1;
return 0;
}
static int
ni_dhcp4_decode_sipservers(ni_buffer_t *bp, ni_string_array_t *list)
{
int encoding;
encoding = ni_buffer_getc(bp);
switch (encoding) {
case -1:
ni_debug_dhcp("%s: missing data", __FUNCTION__);
return -1;
case 0:
return ni_dhcp4_decode_dnssearch(bp, list, "sip-server name");
case 1:
return ni_dhcp4_decode_address_list(bp, list);
default:
ni_error("unknown sip encoding %d", encoding);
return -1;
}
return 0;
}
/*
* Given an IPv4 address, guess the netmask.
*/
static inline unsigned int
__count_net_bits(uint32_t prefix)
{
unsigned int len = 0;
while (prefix) {
prefix <<= 1;
len++;
}
return len;
}
static unsigned int
guess_prefix_len(struct in_addr addr)
{
uint32_t prefix = ntohl(addr.s_addr);
unsigned int len;
/* At a minimum, use the prefix len for this IPv4 address class. */
if (IN_CLASSA(prefix))
len = 8;
else if (IN_CLASSB(prefix))
len = 16;
else if (IN_CLASSC(prefix))
len = 24;
else
len = 0;
/* If the address has bits beyond the default class,
* extend the prefix until we've covered all of them. */
return len + __count_net_bits(prefix << len);
}
static unsigned int
guess_prefix_len_sockaddr(const ni_sockaddr_t *ap)
{
return guess_prefix_len(ap->sin.sin_addr);
}
/*
* DHCP4_STATICROUTE
* List of network/gateway pairs.
*/
static int
ni_dhcp4_decode_static_routes(ni_buffer_t *bp, ni_route_array_t *routes)
{
while (ni_buffer_count(bp) && !bp->underflow) {
ni_sockaddr_t destination, gateway;
ni_route_t *rp;
if (ni_dhcp4_option_get_sockaddr(bp, &destination) < 0
|| ni_dhcp4_option_get_sockaddr(bp, &gateway) < 0)
return -1;
rp = ni_route_create(guess_prefix_len_sockaddr(&destination),
&destination,
&gateway,
0, NULL);
ni_route_array_append(routes, rp);
}
return 0;
}
/*
* DHCP4_ROUTERS (3)
* List of gateways for default route
*/
static int
ni_dhcp4_decode_routers(ni_buffer_t *bp, ni_route_array_t *routes)
{
ni_sockaddr_t gateway;
while (ni_buffer_count(bp) && !bp->underflow) {
ni_route_t *rp;
if (ni_dhcp4_option_get_sockaddr(bp, &gateway) < 0)
return -1;
rp = ni_route_create(0, NULL, &gateway, 0, NULL);
ni_route_array_append(routes, rp);
}
return 0;
}
static int
ni_dhcp4_option_get_domain(ni_buffer_t *bp, char **var, const char *what)
{
unsigned int len;
char *tmp = NULL;
if (ni_dhcp4_option_get_string(bp, &tmp, &len) < 0)
return -1;
if (!ni_check_domain_name(tmp, len, 0)) {
ni_warn("Discarded suspect %s: '%s'", what,
ni_print_suspect(tmp, len));
free(tmp);
return -1;
}
if (*var)
free(*var);
*var = tmp;
return 0;
}
static int
ni_dhcp4_option_get_domain_list(ni_buffer_t *bp, ni_string_array_t *var,
const char *what)
{
ni_string_array_t list = NI_STRING_ARRAY_INIT;
unsigned int len, i;
char *tmp = NULL;
if (ni_dhcp4_option_get_string(bp, &tmp, &len) < 0)
return -1;
/*
* Hack to accept "compatibility abuse" of dns domain name
* option containing multiple domains instead to send them
* using a dns-search option...
*/
if (!ni_string_split(&list, tmp, " ", 0)) {
ni_warn("Discarded suspect %s: '%s'", what,
ni_print_suspect(tmp, len));
free(tmp);
return -1;
}
for (i = 0; i < list.count; ++i) {
const char *dom = list.data[i];
if (!ni_check_domain_name(dom, ni_string_len(dom), 0)) {
ni_warn("Discarded suspect %s: '%s'", what,
ni_print_suspect(tmp, len));
ni_string_array_destroy(&list);
free(tmp);
return -1;
}
}
if (list.count != 1) {
ni_warn("Abuse of %s option to provide a list: '%s'",
what, tmp);
}
free(tmp);
ni_string_array_move(var, &list);
return 0;
}
static int
ni_dhcp4_option_get_pathname(ni_buffer_t *bp, char **var, const char *what)
{
unsigned int len;
char *tmp = NULL;
if (ni_dhcp4_option_get_string(bp, &tmp, &len) < 0)
return -1;
if (!ni_check_pathname(tmp, len)) {
ni_warn("Discarded suspect %s: '%s'", what,
ni_print_suspect(tmp, len));
free(tmp);
return -1;
}
if (*var)
free(*var);
*var = tmp;
return 0;
}
static int
ni_dhcp4_option_get_printable(ni_buffer_t *bp, char **var, const char *what)
{
unsigned int len;
char *tmp = NULL;
if (ni_dhcp4_option_get_string(bp, &tmp, &len) < 0)
return -1;
if (!ni_check_printable(tmp, len)) {
ni_warn("Discarded non-printable %s: '%s'", what,
ni_print_suspect(tmp, len));
free(tmp);
return -1;
}
if (*var)
free(*var);
*var = tmp;
return 0;
}
static int
ni_dhcp4_option_get_netbios_type(ni_buffer_t *bp, unsigned int *type)
{
unsigned int len = ni_buffer_count(bp);
if (len != 1)
return -1;
*type = (unsigned int)ni_buffer_getc(bp);
switch (*type) {
case 0x1: /* B-node */
case 0x2: /* P-node */
case 0x4: /* M-node */
case 0x8: /* H-node */
return 0;
default:
break;
}
*type = 0;
return -1;
}
/*
* Parse a DHCP4 response.
* FIXME: RFC2131 states that the server is allowed to split a DHCP4 option into
* several (partial) options if the total length exceeds 255 octets. We don't
* handle this yet.
*/
int
ni_dhcp4_parse_response(const ni_dhcp4_message_t *message, ni_buffer_t *options, ni_addrconf_lease_t **leasep)
{
ni_buffer_t overload_buf;
ni_addrconf_lease_t *lease;
ni_route_array_t default_routes = NI_ROUTE_ARRAY_INIT;
ni_route_array_t static_routes = NI_ROUTE_ARRAY_INIT;
ni_route_array_t classless_routes = NI_ROUTE_ARRAY_INIT;
ni_string_array_t dns_servers = NI_STRING_ARRAY_INIT;
ni_string_array_t dns_search = NI_STRING_ARRAY_INIT;
ni_string_array_t dns_domain = NI_STRING_ARRAY_INIT;
ni_string_array_t nis_servers = NI_STRING_ARRAY_INIT;
char *nisdomain = NULL;
char *tmp = NULL;
int opt_overload = 0;
int msg_type = -1;
int use_bootserver = 1;
int use_bootfile = 1;
lease = ni_addrconf_lease_new(NI_ADDRCONF_DHCP, AF_INET);
lease->state = NI_ADDRCONF_STATE_GRANTED;
lease->type = NI_ADDRCONF_DHCP;
lease->family = AF_INET;
lease->time_acquired = time(NULL);
lease->dhcp4.address.s_addr = message->yiaddr;
lease->dhcp4.boot_saddr.s_addr = message->siaddr;
lease->dhcp4.relay_addr.s_addr = message->giaddr;
parse_more:
/* Loop as long as we still have data in the buffer. */
while (ni_buffer_count(options) && !options->underflow) {
ni_buffer_t buf;
int option;
option = ni_dhcp4_option_next(options, &buf);
//ni_debug_dhcp("handle option %s (%d)", ni_dhcp4_option_name(option), option);
if (option == DHCP4_PAD)
continue;
if (option == DHCP4_END)
break;
if (option < 0)
goto error;
if (ni_buffer_count(&buf) == 0) {
ni_error("option %d has zero length", option);
goto error;
}
switch (option) {
case DHCP4_MESSAGETYPE:
msg_type = ni_buffer_getc(&buf);
if (msg_type < 0)
goto error;
continue;
case DHCP4_ADDRESS:
ni_dhcp4_option_get_ipv4(&buf, &lease->dhcp4.address);
break;
case DHCP4_NETMASK:
ni_dhcp4_option_get_ipv4(&buf, &lease->dhcp4.netmask);
break;
case DHCP4_BROADCAST:
ni_dhcp4_option_get_ipv4(&buf, &lease->dhcp4.broadcast);
break;
case DHCP4_SERVERIDENTIFIER:
ni_dhcp4_option_get_ipv4(&buf, &lease->dhcp4.server_id);
break;
case DHCP4_LEASETIME:
ni_dhcp4_option_get32(&buf, &lease->dhcp4.lease_time);
break;
case DHCP4_RENEWALTIME:
ni_dhcp4_option_get32(&buf, &lease->dhcp4.renewal_time);
break;
case DHCP4_REBINDTIME:
ni_dhcp4_option_get32(&buf, &lease->dhcp4.rebind_time);
break;
case DHCP4_MTU:
ni_dhcp4_option_get16(&buf, &lease->dhcp4.mtu);
/* Minimum legal mtu is 68 accoridng to
* RFC 2132. In practise it's 576 which is the
* minimum maximum message size. */
if (lease->dhcp4.mtu < MTU_MIN) {
ni_debug_dhcp("MTU %u is too low, minimum is %d; ignoring",
lease->dhcp4.mtu, MTU_MIN);
lease->dhcp4.mtu = 0;
}
break;
case DHCP4_HOSTNAME:
ni_dhcp4_option_get_domain(&buf, &lease->hostname,
"hostname");
break;
case DHCP4_DNSDOMAIN:
ni_dhcp4_option_get_domain_list(&buf, &dns_domain,
"dns-domain");
break;
case DHCP4_MESSAGE:
ni_dhcp4_option_get_printable(&buf, &lease->dhcp4.message,
"dhcp4-message");
break;
case DHCP4_ROOTPATH:
ni_dhcp4_option_get_pathname(&buf, &lease->dhcp4.root_path,
"root-path");