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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright (c) 2016, Chris Fraire <cfraire@me.com>.
*/
#ifndef _DHCP_IMPL_H
#define _DHCP_IMPL_H
/*
* Common definitions used by Sun DHCP implementations
*/
#ifdef __cplusplus
extern "C" {
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/udp.h>
#include <netinet/dhcp.h>
#include <netinet/dhcp6.h>
#include <dhcp_symbol_common.h>
#include <sys/sunos_dhcp_class.h>
/* Packet fields */
#define CD_PACKET_START 0
#define CD_POPCODE 0 /* packet opcode */
#define CD_PHTYPE 1 /* packet header type */
#define CD_PHLEN 2 /* packet header len */
#define CD_PHOPS 3 /* packet header len */
#define CD_PXID 4 /* packet hops */
#define CD_PSECS 8 /* packet xid */
#define CD_PFLAGS 10 /* packet secs */
#define CD_PCIADDR 12 /* packet flags */
#define CD_YIADDR 16 /* client's ip address */
#define CD_SIADDR 20 /* Bootserver's ip address */
#define CD_GIADDR 24 /* BOOTP relay agent address */
#define CD_PCHADDR 28 /* BOOTP relay agent address */
#define CD_SNAME 44 /* Hostname of Bootserver, or opts */
#define CD_BOOTFILE 108 /* File to boot or opts */
#define CD_PCOOKIE 236 /* packet cookie */
#define CD_POPTIONS 240 /* packet options */
#define CD_PACKET_END CD_POPTIONS
/* Internal server options */
#define CD_INTRNL_START 1024
#define CD_BOOL_HOSTNAME 1024 /* Entry wants hostname (Nameserv) */
#define CD_BOOL_LEASENEG 1025 /* Entry's lease is negotiable */
#define CD_BOOL_ECHO_VCLASS 1026 /* Echo Vendor class back to Entry */
#define CD_BOOTPATH 1027 /* prefix path to File to boot */
#define CD_INTRNL_END 1027
/* Error codes that could be generated while parsing packets */
#define DHCP_ERR_OFFSET 512
#define DHCP_GARBLED_MSG_TYPE (DHCP_ERR_OFFSET+0)
#define DHCP_WRONG_MSG_TYPE (DHCP_ERR_OFFSET+1)
#define DHCP_BAD_OPT_OVLD (DHCP_ERR_OFFSET+2)
/*
* Arbitrary "maximum" client ID length (in bytes), used by various bits
* of the standalone code. This needs to go away someday.
*/
#define DHCP_MAX_CID_LEN 64
/*
* Generic DHCP option structure.
*/
typedef struct {
uint8_t code;
uint8_t len;
uint8_t value[1];
} DHCP_OPT;
/*
* Defines the size of DHCP_OPT code + len
*/
#define DHCP_OPT_META_LEN 2
typedef union sockaddr46_s {
struct sockaddr_in v4;
struct sockaddr_in6 v6;
} sockaddr46_t;
/*
* Generic DHCP packet list. Ensure that _REENTRANT bracketed code stays at
* bottom of this definition - the client doesn't include it. Scan.c in
* libdhcp isn't aware of it either...
*
* The PKT * pointer here actually points to a dhcpv6_message_t if the packet
* is DHCPv6. We assume that PKT * the same or stricter alignment
* requirements, and that the unused elements are not a significant burden.
*/
#define MAX_PKT_LIST 5 /* maximum list size */
typedef struct dhcp_list {
struct dhcp_list *next; /* keep first and in this */
struct dhcp_list *prev; /* order for insque/remque */
PKT *pkt; /* client packet */
uint_t len; /* packet len */
int rfc1048; /* RFC1048 options - boolean */
uint8_t offset; /* BOOTP packet offset */
uint8_t isv6; /* DHCPv6 packet - boolean */
/*
* standard/site options
*/
DHCP_OPT *opts[DHCP_LAST_OPT + 1];
/*
* Vendor specific options (client only)
*/
DHCP_OPT *vs[VS_OPTION_END - VS_OPTION_START + 1];
struct in_addr off_ip; /* Address OFFERed */
uint_t ifindex; /* received ifindex (if any) */
sockaddr46_t pktfrom; /* source (peer) address on input */
sockaddr46_t pktto; /* destination (local) address */
} PKT_LIST;
extern int dhcp_options_scan(PKT_LIST *, boolean_t);
extern boolean_t dhcp_getinfo_pl(PKT_LIST *, uchar_t, uint16_t, uint16_t,
void *, size_t *);
extern dhcpv6_option_t *dhcpv6_find_option(const void *, size_t,
const dhcpv6_option_t *, uint16_t, uint_t *);
extern dhcpv6_option_t *dhcpv6_pkt_option(const PKT_LIST *,
const dhcpv6_option_t *, uint16_t, uint_t *);
#ifdef __cplusplus
}
#endif
#endif /* _DHCP_IMPL_H */
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#ifndef _DHCP_SYMBOL_COMMON_H
#define _DHCP_SYMBOL_COMMON_H
/*
* This file contains DHCP symbol definitions that are shared between userland
* (libdhcputil) and standalone. The sharing is just for convenience; there's
* no inherent relationship between the two implementations.
*
* NOTE: This file should never be included directly, but rather through
* either <dhcp_symbol.h> or <dhcp_impl.h>.
*/
#ifdef __cplusplus
extern "C" {
#endif
/*
* Symbol category ids and strings
*/
typedef enum {
DSYM_BAD_CAT = -1, /* An invalid category */
DSYM_STANDARD = 0, /* Standard option */
DSYM_EXTEND = 1, /* Extended Standard option */
DSYM_VENDOR = 2, /* Vendor-specific option */
DSYM_SITE = 3, /* Site-specific option */
DSYM_FIELD = 4, /* DHCP packet fixed field option */
DSYM_INTERNAL = 5 /* Solaris DHCP internal option */
} dsym_category_t;
/*
* Symbol type ids and strings
*/
typedef enum {
DSYM_BAD_TYPE = -1, /* An invalid type */
DSYM_ASCII = 0, /* A printable character string */
DSYM_OCTET = 1, /* An array of bytes */
DSYM_IP = 2, /* An IP address */
DSYM_NUMBER = 3, /* A signed number */
DSYM_BOOL = 4, /* No associated value */
DSYM_INCLUDE = 5, /* Include macro (internal only) */
DSYM_UNUMBER8 = 6, /* An 8-bit unsigned integer */
DSYM_UNUMBER16 = 7, /* A 16-bit unsigned integer */
DSYM_UNUMBER32 = 8, /* A 32-bit unsigned integer */
DSYM_UNUMBER64 = 9, /* A 64-bit unsigned integer */
DSYM_SNUMBER8 = 10, /* An 8-bit signed integer */
DSYM_SNUMBER16 = 11, /* A 16-bit signed integer */
DSYM_SNUMBER32 = 12, /* A 32-bit signed integer */
DSYM_SNUMBER64 = 13, /* A 64-bit signed integer */
DSYM_UNUMBER24 = 14, /* A 24-bit unsigned integer */
DSYM_IPV6 = 15, /* An IPv6 address */
DSYM_DUID = 16, /* A DHCP Unique Identifier */
DSYM_DOMAIN = 17 /* An RFC 1035-encoded domain name */
} dsym_cdtype_t;
#ifdef __cplusplus
}
#endif
#endif /* _DHCP_SYMBOL_COMMON_H */
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/types.h>
#include <stddef.h>
#include <stdlib.h>
#include <strings.h>
#include <netinet/in.h>
#include <netinet/dhcp.h>
#include "dhcp_impl.h"
/*
* Fetch a copy of the DHCP-supplied value of the parameter requested
* by code in value, and the parameter value length in *vallenp.
*
* Return values:
*
* B_FALSE If invalid code, or no parameter value.
*
* B_TRUE Valid code which has a parameter value.
* *vallenp is set to the parameter value length.
* If the parameter value length is less than or
* equal to *vallenp, value is set to the parameter
* value.
*/
boolean_t
dhcp_getinfo_pl(PKT_LIST *pl, uchar_t optcat, uint16_t code, uint16_t optsize,
void *value, size_t *vallenp)
{
if (pl == NULL)
return (B_FALSE);
if (optcat == DSYM_STANDARD) {
if (code > DHCP_LAST_OPT)
return (B_FALSE);
if (pl->opts[code] == NULL)
return (B_FALSE);
if (*vallenp < pl->opts[code]->len) {
*vallenp = pl->opts[code]->len;
return (B_TRUE);
}
bcopy(pl->opts[code]->value, value, pl->opts[code]->len);
*vallenp = pl->opts[code]->len;
} else if (optcat == DSYM_VENDOR) {
if (code > VS_OPTION_END)
return (B_FALSE);
if (pl->vs[code] == NULL)
return (B_FALSE);
if (*vallenp < pl->vs[code]->len) {
*vallenp = pl->vs[code]->len;
return (B_TRUE);
}
bcopy(pl->vs[code]->value, value, pl->vs[code]->len);
*vallenp = pl->vs[code]->len;
} else if (optcat == DSYM_FIELD) {
if (code + optsize > sizeof (PKT))
return (B_FALSE);
if (*vallenp < optsize) {
*vallenp = optsize;
return (B_TRUE);
}
*vallenp = optsize;
bcopy((caddr_t)pl->pkt + code, value, optsize);
} else
return (B_FALSE);
return (B_TRUE);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1996-2001 by Sun Microsystems, Inc.
* All rights reserved.
*/
#include <sys/types.h>
#include "v4_sum_impl.h"
/*
* Compute one's complement checksum for IP packet headers.
*/
uint16_t
ipv4cksum(uint16_t *cp, uint16_t count)
{
uint_t sum = 0;
uint_t oneword = BIT_WRAP;
if (count == 0)
return (0);
count >>= 1;
while (count--) {
sum += (uint_t)*cp++;
if (sum >= oneword) { /* Wrap carries into low bit */
sum -= oneword;
sum++;
}
}
return ((uint16_t)~sum);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 1996-2002 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* LINTLIBRARY */
#if !defined(_BOOT) && !defined(_KERNEL)
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#endif /* _BOOT && _KERNEL */
#include <sys/types.h>
#include <sys/null.h>
#include <sys/errno.h>
#if defined(_BOOT) || defined(_KERNEL)
#define isdigit(c) ((c) >= '0' && c <= '9')
#define isxdigit(c) (isdigit(c) || (((c) >= 'a') && ((c) <= 'f')) || \
(((c) >= 'A') && ((c) <= 'F')))
#endif /* _BOOT || _KERNEL */
/*
* Converts an octet string into an ASCII string. The string returned is
* NULL-terminated, and the length recorded in blen does *not* include the
* null terminator (in other words, octet_to_hexascii() returns the length a'la
* strlen()).
*
* Returns 0 for Success, errno otherwise.
*/
int
octet_to_hexascii(const void *nump, uint_t nlen, char *bufp, uint_t *blen)
{
int i;
char *bp;
const uchar_t *np;
static char ascii_conv[] = "0123456789ABCDEF";
if (nump == NULL || bufp == NULL || blen == NULL)
return (EINVAL);
if ((nlen * 2) >= *blen) {
*blen = 0;
return (E2BIG);
}
for (i = 0, bp = bufp, np = (const uchar_t *)nump; i < nlen; i++) {
*bp++ = ascii_conv[(np[i] >> 4) & 0x0f];
*bp++ = ascii_conv[np[i] & 0x0f];
}
*bp = '\0';
*blen = i * 2;
return (0);
}
/*
* Converts an ASCII string into an octet string.
*
* Returns 0 for success, errno otherwise.
*/
int
hexascii_to_octet(const char *asp, uint_t alen, void *bufp, uint_t *blen)
{
int i, j, k;
const char *tp;
uchar_t *u_tp;
if (asp == NULL || bufp == NULL || blen == NULL)
return (EINVAL);
if (alen > (*blen * 2))
return (E2BIG);
k = ((alen % 2) == 0) ? alen / 2 : (alen / 2) + 1;
for (tp = asp, u_tp = (uchar_t *)bufp, i = 0; i < k; i++, u_tp++) {
/* one nibble at a time */
for (*u_tp = 0, j = 0; j < 2; j++, tp++) {
if (isdigit(*tp))
*u_tp |= *tp - '0';
else if (isxdigit(*tp))
*u_tp |= (*tp & ~0x20) + 10 - 'A';
else
return (EINVAL);
if ((j % 2) == 0)
*u_tp <<= 4;
}
}
*blen = k;
return (0);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Routines used to extract/insert DHCP options. Must be kept MT SAFE,
* as they are called from different threads.
*/
#include <sys/types.h>
#include "dhcp_impl.h"
#if defined(_KERNEL) && !defined(_BOOT)
#include <sys/sunddi.h>
#else
#include <strings.h>
#endif /* _KERNEL && !_BOOT */
static uint8_t bootmagic[] = BOOTMAGIC;
/*
* Scan field for options.
*/
static void
field_scan(uint8_t *start, uint8_t *end, DHCP_OPT **options,
uint8_t last_option)
{
uint8_t *current;
while (start < end) {
if (*start == CD_PAD) {
start++;
continue;
}
if (*start == CD_END)
break; /* done */
if (*start > last_option) {
if (++start < end)
start += *start + 1;
continue; /* unrecognized option */
}
current = start;
if (++start < end)
start += *start + 1; /* advance to next option */
/* all options besides CD_END and CD_PAD should have a len */
if ((current + 1) >= end)
continue;
/* Ignores duplicate options. */
if (options[*current] == NULL) {
options[*current] = (DHCP_OPT *)current;
/* verify that len won't go beyond end */
if ((current + options[*current]->len + 1) >= end) {
options[*current] = NULL;
continue;
}
}
}
}
/*
* Scan Vendor field for options.
*/
static void
vendor_scan(PKT_LIST *pl)
{
uint8_t *start, *end, len;
if (pl->opts[CD_VENDOR_SPEC] == NULL)
return;
len = pl->opts[CD_VENDOR_SPEC]->len;
start = pl->opts[CD_VENDOR_SPEC]->value;
/* verify that len won't go beyond the end of the packet */
if (((start - (uint8_t *)pl->pkt) + len) > pl->len)
return;
end = start + len;
field_scan(start, end, pl->vs, VS_OPTION_END);
}
/*
* Load opts table in PKT_LIST entry with PKT's options.
* Returns 0 if no fatal errors occur, otherwise...
*/
int
dhcp_options_scan(PKT_LIST *pl, boolean_t scan_vendor)
{
PKT *pkt = pl->pkt;
uint_t opt_size = pl->len - BASE_PKT_SIZE;
/*
* bcmp() is used here instead of memcmp() since kernel/standalone
* doesn't have a memcmp().
*/
if (pl->len < BASE_PKT_SIZE ||
bcmp(pl->pkt->cookie, bootmagic, sizeof (pl->pkt->cookie)) != 0) {
pl->rfc1048 = 0;
return (0);
}
pl->rfc1048 = 1;
/* check the options field */
field_scan(pkt->options, &pkt->options[opt_size], pl->opts,
DHCP_LAST_OPT);
/*
* process vendor specific options. We look at the vendor options
* here, simply because a BOOTP server could fake DHCP vendor
* options. This increases our interoperability with BOOTP.
*/
if (scan_vendor && (pl->opts[CD_VENDOR_SPEC] != NULL))
vendor_scan(pl);
if (pl->opts[CD_DHCP_TYPE] == NULL)
return (0);
if (pl->opts[CD_DHCP_TYPE]->len != 1)
return (DHCP_GARBLED_MSG_TYPE);
if (*pl->opts[CD_DHCP_TYPE]->value < DISCOVER ||
*pl->opts[CD_DHCP_TYPE]->value > INFORM)
return (DHCP_WRONG_MSG_TYPE);
if (pl->opts[CD_OPTION_OVERLOAD]) {
if (pl->opts[CD_OPTION_OVERLOAD]->len != 1) {
pl->opts[CD_OPTION_OVERLOAD] = NULL;
return (DHCP_BAD_OPT_OVLD);
}
switch (*pl->opts[CD_OPTION_OVERLOAD]->value) {
case 1:
field_scan(pkt->file, &pkt->cookie[0], pl->opts,
DHCP_LAST_OPT);
break;
case 2:
field_scan(pkt->sname, &pkt->file[0], pl->opts,
DHCP_LAST_OPT);
break;
case 3:
field_scan(pkt->file, &pkt->cookie[0], pl->opts,
DHCP_LAST_OPT);
field_scan(pkt->sname, &pkt->file[0], pl->opts,
DHCP_LAST_OPT);
break;
default:
pl->opts[CD_OPTION_OVERLOAD] = NULL;
return (DHCP_BAD_OPT_OVLD);
}
}
return (0);
}
/*
* Locate a DHCPv6 option or suboption within a buffer. DHCPv6 uses nested
* options within options, and this function is designed to work with both
* primary options and the suboptions contained within.
*
* The 'oldopt' is a previous option pointer, and is typically used to iterate
* over options of the same code number. The 'codenum' is in host byte order
* for simplicity. 'retlenp' may be NULL, and if present gets the _entire_
* option length (including header).
*
* Warning: the returned pointer has no particular alignment because DHCPv6
* defines options without alignment. The caller must deal with unaligned
* pointers carefully.
*/
dhcpv6_option_t *
dhcpv6_find_option(const void *buffer, size_t buflen,
const dhcpv6_option_t *oldopt, uint16_t codenum, uint_t *retlenp)
{
const uchar_t *bp;
dhcpv6_option_t d6o;
uint_t olen;
codenum = htons(codenum);
bp = buffer;
while (buflen >= sizeof (dhcpv6_option_t)) {
(void) memcpy(&d6o, bp, sizeof (d6o));
olen = ntohs(d6o.d6o_len) + sizeof (d6o);
if (olen > buflen)
break;
if (d6o.d6o_code != codenum ||
(oldopt != NULL && bp <= (const uchar_t *)oldopt)) {
bp += olen;
buflen -= olen;
continue;
}
if (retlenp != NULL)
*retlenp = olen;
/* LINTED: alignment */
return ((dhcpv6_option_t *)bp);
}
return (NULL);
}
/*
* Locate a DHCPv6 option within the top level of a PKT_LIST entry. DHCPv6
* uses nested options within options, and this function returns only the
* primary options. Use dhcpv6_find_option to traverse suboptions.
*
* See dhcpv6_find_option for usage details and warnings.
*/
dhcpv6_option_t *
dhcpv6_pkt_option(const PKT_LIST *plp, const dhcpv6_option_t *oldopt,
uint16_t codenum, uint_t *retlenp)
{
const dhcpv6_message_t *d6m;
if (plp == NULL || plp->pkt == NULL || plp->len < sizeof (*d6m))
return (NULL);
d6m = (const dhcpv6_message_t *)plp->pkt;
return (dhcpv6_find_option(d6m + 1, plp->len - sizeof (*d6m), oldopt,
codenum, retlenp));
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1996-2001 by Sun Microsystems, Inc.
* All rights reserved.
*/
#include <sys/types.h>
#include <strings.h>
#include <netinet/in.h>
#include "v4_sum_impl.h"
/*
* RFC 768 pseudo header. Used in calculating UDP checksums.
*/
struct pseudo_udp {
struct in_addr src;
struct in_addr dst;
uint8_t notused; /* always zero */
uint8_t proto; /* protocol used */
uint16_t len; /* UDP len */
};
/*
* One's complement checksum of pseudo header, udp header, and data.
* Must be MT SAFE.
*/
uint16_t
udp_chksum(struct udphdr *udph, const struct in_addr *src,
const struct in_addr *dst, uint8_t proto)
{
struct pseudo_udp ck;
uint16_t *end_pseudo_hdr = (uint16_t *)(&ck + 1);
uint16_t *sp = (uint16_t *)&ck;
uint_t sum = 0;
uint16_t cnt;
/*
* Start on the pseudo header. Note that pseudo_udp already takes
* acount for the udphdr...
*/
bzero(&ck, sizeof (ck));
/* struct copies */
ck.src = *src;
ck.dst = *dst;
ck.len = udph->uh_ulen;
ck.proto = proto;
/*
* If the packet is an odd length, zero the pad byte for checksum
* purposes [doesn't hurt data]
*/
cnt = ntohs(ck.len) + sizeof (ck);
if (cnt & 1) {
((caddr_t)udph)[ntohs(udph->uh_ulen)] = '\0';
cnt++; /* make even */
}
for (cnt >>= 1; cnt != 0; cnt--) {
sum += *sp++;
if (sum >= BIT_WRAP) {
/* Wrap carries into low bit */
sum -= BIT_WRAP;
sum++;
}
/*
* If we've finished checking the pseudo-header, move
* onto the udp header and data.
*/
if (sp == end_pseudo_hdr)
sp = (uint16_t *)udph;
}
return (~sum == 0 ? ~0 : ~sum);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1999-2001 by Sun Microsystems, Inc.
* All rights reserved.
*/
#ifndef _V4_SUM_IMPL_H
#define _V4_SUM_IMPL_H
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/udp.h>
/*
* Common functions for various IP/UDP module checksum implementations.
*/
#ifdef __cplusplus
extern "C" {
#endif
#define BIT_WRAP (uint_t)0x10000 /* checksum wrap */
extern uint16_t ipv4cksum(uint16_t *, uint16_t);
extern uint16_t udp_chksum(struct udphdr *, const struct in_addr *,
const struct in_addr *, uint8_t);
#ifdef __cplusplus
}
#endif
#endif /* _V4_SUM_IMPL_H */
|