Minor formatting in dhcp.c
[freeradius.git] / src / modules / proto_dhcp / dhcp.c
1 /*
2  * dhcp.c       Functions to send/receive dhcp packets.
3  *
4  * Version:     $Id$
5  *
6  *   This library is free software; you can redistribute it and/or
7  *   modify it under the terms of the GNU Lesser General Public
8  *   License as published by the Free Software Foundation; either
9  *   version 2.1 of the License, or (at your option) any later version.
10  *
11  *   This library is distributed in the hope that it will be useful,
12  *   but 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
17  *   License along with this library; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 2008 The FreeRADIUS server project
21  * Copyright 2008 Alan DeKok <aland@deployingradius.com>
22  */
23
24 RCSID("$Id$")
25
26 #include <freeradius-devel/libradius.h>
27 #include <freeradius-devel/udpfromto.h>
28 #include <freeradius-devel/dhcp.h>
29
30 #ifndef __MINGW32__
31 #include <sys/ioctl.h>
32 #endif
33
34 #ifdef HAVE_SYS_SOCKET_H
35 #include <sys/socket.h>
36 #endif
37 #ifdef HAVE_SYS_TYPES_H
38 #include <sys/types.h>
39 #endif
40
41 #ifndef __MINGW32__
42 #include <net/if_arp.h>
43 #endif
44
45 #define DHCP_CHADDR_LEN (16)
46 #define DHCP_SNAME_LEN  (64)
47 #define DHCP_FILE_LEN   (128)
48 #define DHCP_VEND_LEN   (308)
49 #define DHCP_OPTION_MAGIC_NUMBER (0x63825363)
50
51 #ifndef INADDR_BROADCAST
52 #define INADDR_BROADCAST INADDR_NONE
53 #endif
54
55 /* @todo: this is a hack */
56 #  define DEBUG                 if (fr_debug_flag && fr_log_fp) fr_printf_log
57 void fr_strerror_printf(char const *fmt, ...);
58 #  define debug_pair(vp)        do { if (fr_debug_flag && fr_log_fp) { \
59                                         vp_print(fr_log_fp, vp); \
60                                      } \
61                                 } while(0)
62
63 typedef struct dhcp_packet_t {
64         uint8_t         opcode;
65         uint8_t         htype;
66         uint8_t         hlen;
67         uint8_t         hops;
68         uint32_t        xid;    /* 4 */
69         uint16_t        secs;   /* 8 */
70         uint16_t        flags;
71         uint32_t        ciaddr; /* 12 */
72         uint32_t        yiaddr; /* 16 */
73         uint32_t        siaddr; /* 20 */
74         uint32_t        giaddr; /* 24 */
75         uint8_t         chaddr[DHCP_CHADDR_LEN]; /* 28 */
76         uint8_t         sname[DHCP_SNAME_LEN]; /* 44 */
77         uint8_t         file[DHCP_FILE_LEN]; /* 108 */
78         uint32_t        option_format; /* 236 */
79         uint8_t         options[DHCP_VEND_LEN];
80 } dhcp_packet_t;
81
82 typedef struct dhcp_option_t {
83         uint8_t         code;
84         uint8_t         length;
85 } dhcp_option_t;
86
87 /*
88  *      INADDR_ANY : 68 -> INADDR_BROADCAST : 67        DISCOVER
89  *      INADDR_BROADCAST : 68 <- SERVER_IP : 67         OFFER
90  *      INADDR_ANY : 68 -> INADDR_BROADCAST : 67        REQUEST
91  *      INADDR_BROADCAST : 68 <- SERVER_IP : 67         ACK
92  */
93 static char const *dhcp_header_names[] = {
94         "DHCP-Opcode",
95         "DHCP-Hardware-Type",
96         "DHCP-Hardware-Address-Length",
97         "DHCP-Hop-Count",
98         "DHCP-Transaction-Id",
99         "DHCP-Number-of-Seconds",
100         "DHCP-Flags",
101         "DHCP-Client-IP-Address",
102         "DHCP-Your-IP-Address",
103         "DHCP-Server-IP-Address",
104         "DHCP-Gateway-IP-Address",
105         "DHCP-Client-Hardware-Address",
106         "DHCP-Server-Host-Name",
107         "DHCP-Boot-Filename",
108
109         NULL
110 };
111
112 static char const *dhcp_message_types[] = {
113         "invalid",
114         "DHCP-Discover",
115         "DHCP-Offer",
116         "DHCP-Request",
117         "DHCP-Decline",
118         "DHCP-Ack",
119         "DHCP-NAK",
120         "DHCP-Release",
121         "DHCP-Inform",
122         "DHCP-Force-Renew",
123 };
124
125 static int dhcp_header_sizes[] = {
126         1, 1, 1, 1,
127         4, 2, 2, 4,
128         4, 4, 4,
129         DHCP_CHADDR_LEN,
130         DHCP_SNAME_LEN,
131         DHCP_FILE_LEN
132 };
133
134
135 /*
136  *      Some clients silently ignore responses less than 300 bytes.
137  */
138 #define MIN_PACKET_SIZE (244)
139 #define DEFAULT_PACKET_SIZE (300)
140 #define MAX_PACKET_SIZE (1500 - 40)
141
142 #define DHCP_OPTION_FIELD (0)
143 #define DHCP_FILE_FIELD   (1)
144 #define DHCP_SNAME_FIELD  (2)
145
146 static uint8_t *dhcp_get_option(dhcp_packet_t *packet, size_t packet_size,
147                                 unsigned int option)
148 {
149         int overload = 0;
150         int field = DHCP_OPTION_FIELD;
151         size_t where, size;
152         uint8_t *data;
153
154         where = 0;
155         size = packet_size - offsetof(dhcp_packet_t, options);
156         data = &packet->options[where];
157
158         while (where < size) {
159                 if (data[0] == 0) { /* padding */
160                         where++;
161                         continue;
162                 }
163
164                 if (data[0] == 255) { /* end of options */
165                         if ((field == DHCP_OPTION_FIELD) &&
166                             (overload & DHCP_FILE_FIELD)) {
167                                 data = packet->file;
168                                 where = 0;
169                                 size = sizeof(packet->file);
170                                 field = DHCP_FILE_FIELD;
171                                 continue;
172
173                         } else if ((field == DHCP_FILE_FIELD) &&
174                                    (overload & DHCP_SNAME_FIELD)) {
175                                 data = packet->sname;
176                                 where = 0;
177                                 size = sizeof(packet->sname);
178                                 field = DHCP_SNAME_FIELD;
179                                 continue;
180                         }
181
182                         return NULL;
183                 }
184
185                 /*
186                  *      We MUST have a real option here.
187                  */
188                 if ((where + 2) > size) {
189                         fr_strerror_printf("Options overflow field at %u",
190                                            (unsigned int) (data - (uint8_t *) packet));
191                         return NULL;
192                 }
193
194                 if ((where + 2 + data[1]) > size) {
195                         fr_strerror_printf("Option length overflows field at %u",
196                                            (unsigned int) (data - (uint8_t *) packet));
197                         return NULL;
198                 }
199
200                 if (data[0] == option) return data;
201
202                 if (data[0] == 52) { /* overload sname and/or file */
203                         overload = data[3];
204                 }
205
206                 where += data[1] + 2;
207                 data += data[1] + 2;
208         }
209
210         return NULL;
211 }
212
213 /*
214  *      DHCPv4 is only for IPv4.  Broadcast only works if udpfromto is
215  *      defined.
216  */
217 RADIUS_PACKET *fr_dhcp_recv(int sockfd)
218 {
219         uint32_t                magic;
220         struct sockaddr_storage src;
221         struct sockaddr_storage dst;
222         socklen_t               sizeof_src;
223         socklen_t               sizeof_dst;
224         RADIUS_PACKET           *packet;
225         int                     port;
226         uint8_t                 *code;
227         ssize_t                 data_len;
228
229         packet = rad_alloc(NULL, 0);
230         if (!packet) {
231                 fr_strerror_printf("Failed allocating packet");
232                 return NULL;
233         }
234
235         packet->data = talloc_zero_array(packet, uint8_t, MAX_PACKET_SIZE);
236         if (!packet->data) {
237                 fr_strerror_printf("Out of memory");
238                 rad_free(&packet);
239                 return NULL;
240         }
241
242         packet->sockfd = sockfd;
243         sizeof_src = sizeof(src);
244 #ifdef WITH_UDPFROMTO
245         sizeof_dst = sizeof(dst);
246         data_len = recvfromto(sockfd, packet->data, MAX_PACKET_SIZE, 0,
247                               (struct sockaddr *)&src, &sizeof_src,
248                               (struct sockaddr *)&dst, &sizeof_dst);
249 #else
250         data_len = recvfrom(sockfd, packet->data, MAX_PACKET_SIZE, 0,
251                             (struct sockaddr *)&src, &sizeof_src);
252 #endif
253
254         if (data_len <= 0) {
255                 fr_strerror_printf("Failed reading DHCP socket: %s", fr_syserror(errno));
256                 rad_free(&packet);
257                 return NULL;
258         }
259
260         packet->data_len = data_len;
261         if (packet->data_len < MIN_PACKET_SIZE) {
262                 fr_strerror_printf("DHCP packet is too small (%zu < %d)",
263                                    packet->data_len, MIN_PACKET_SIZE);
264                 rad_free(&packet);
265                 return NULL;
266         }
267
268         if (packet->data_len > MAX_PACKET_SIZE) {
269                 fr_strerror_printf("DHCP packet is too large (%zx > %d)",
270                                    packet->data_len, MAX_PACKET_SIZE);
271                 rad_free(&packet);
272                 return NULL;
273         }
274
275         if (packet->data[1] != 1) {
276                 fr_strerror_printf("DHCP can only receive ethernet requests, not type %02x",
277                       packet->data[1]);
278                 rad_free(&packet);
279                 return NULL;
280         }
281
282         if (packet->data[2] != 6) {
283                 fr_strerror_printf("Ethernet HW length is wrong length %d",
284                         packet->data[2]);
285                 rad_free(&packet);
286                 return NULL;
287         }
288
289         memcpy(&magic, packet->data + 236, 4);
290         magic = ntohl(magic);
291         if (magic != DHCP_OPTION_MAGIC_NUMBER) {
292                 fr_strerror_printf("Cannot do BOOTP");
293                 rad_free(&packet);
294                 return NULL;
295         }
296
297         /*
298          *      Create unique keys for the packet.
299          */
300         memcpy(&magic, packet->data + 4, 4);
301         packet->id = ntohl(magic);
302
303         code = dhcp_get_option((dhcp_packet_t *) packet->data,
304                                packet->data_len, 53);
305         if (!code) {
306                 fr_strerror_printf("No message-type option was found in the packet");
307                 rad_free(&packet);
308                 return NULL;
309         }
310
311         if ((code[1] < 1) || (code[2] == 0) || (code[2] > 8)) {
312                 fr_strerror_printf("Unknown value for message-type option");
313                 rad_free(&packet);
314                 return NULL;
315         }
316
317         packet->code = code[2] | PW_DHCP_OFFSET;
318
319         /*
320          *      Create a unique vector from the MAC address and the
321          *      DHCP opcode.  This is a hack for the RADIUS
322          *      infrastructure in the rest of the server.
323          *
324          *      Note: packet->data[2] == 6, which is smaller than
325          *      sizeof(packet->vector)
326          *
327          *      FIXME:  Look for client-identifier in packet,
328          *      and use that, too?
329          */
330         memset(packet->vector, 0, sizeof(packet->vector));
331         memcpy(packet->vector, packet->data + 28, packet->data[2]);
332         packet->vector[packet->data[2]] = packet->code & 0xff;
333
334         /*
335          *      FIXME: for DISCOVER / REQUEST: src_port == dst_port + 1
336          *      FIXME: for OFFER / ACK       : src_port = dst_port - 1
337          */
338
339         /*
340          *      Unique keys are xid, client mac, and client ID?
341          */
342
343         /*
344          *      FIXME: More checks, like DHCP packet type?
345          */
346
347         sizeof_dst = sizeof(dst);
348
349 #ifndef WITH_UDPFROMTO
350         /*
351          *      This should never fail...
352          */
353         if (getsockname(sockfd, (struct sockaddr *) &dst, &sizeof_dst) < 0) {
354                 fr_strerror_printf("getsockname failed: %s", fr_syserror(errno));
355                 rad_free(&packet);
356                 return NULL;
357         }
358 #endif
359
360         fr_sockaddr2ipaddr(&dst, sizeof_dst, &packet->dst_ipaddr, &port);
361         packet->dst_port = port;
362
363         fr_sockaddr2ipaddr(&src, sizeof_src, &packet->src_ipaddr, &port);
364         packet->src_port = port;
365
366         if (fr_debug_flag > 1) {
367                 char type_buf[64];
368                 char const *name = type_buf;
369                 char src_ip_buf[256], dst_ip_buf[256];
370
371                 if ((packet->code >= PW_DHCP_DISCOVER) &&
372                     (packet->code <= PW_DHCP_INFORM)) {
373                         name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
374                 } else {
375                         snprintf(type_buf, sizeof(type_buf), "%d",
376                                  packet->code - PW_DHCP_OFFSET);
377                 }
378
379                 DEBUG("Received %s of id %08x from %s:%d to %s:%d\n",
380                        name, (unsigned int) packet->id,
381                        inet_ntop(packet->src_ipaddr.af,
382                                  &packet->src_ipaddr.ipaddr,
383                                  src_ip_buf, sizeof(src_ip_buf)),
384                        packet->src_port,
385                        inet_ntop(packet->dst_ipaddr.af,
386                                  &packet->dst_ipaddr.ipaddr,
387                                  dst_ip_buf, sizeof(dst_ip_buf)),
388                        packet->dst_port);
389         }
390
391         return packet;
392 }
393
394
395 /*
396  *      Send a DHCP packet.
397  */
398 int fr_dhcp_send(RADIUS_PACKET *packet)
399 {
400         struct sockaddr_storage dst;
401         socklen_t               sizeof_dst;
402 #ifdef WITH_UDPFROMTO
403         struct sockaddr_storage src;
404         socklen_t               sizeof_src;
405
406         fr_ipaddr2sockaddr(&packet->src_ipaddr, packet->src_port,
407             &src, &sizeof_src);
408 #endif
409
410         fr_ipaddr2sockaddr(&packet->dst_ipaddr, packet->dst_port,
411                            &dst, &sizeof_dst);
412
413         if (packet->data_len == 0) {
414                 fr_strerror_printf("No data to send");
415                 return -1;
416         }
417
418         if (fr_debug_flag > 1) {
419                 char type_buf[64];
420                 char const *name = type_buf;
421 #ifdef WITH_UDPFROMTO
422                 char src_ip_buf[INET6_ADDRSTRLEN];
423 #endif
424                 char dst_ip_buf[INET6_ADDRSTRLEN];
425
426                 if ((packet->code >= PW_DHCP_DISCOVER) &&
427                     (packet->code <= PW_DHCP_INFORM)) {
428                         name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
429                 } else {
430                         snprintf(type_buf, sizeof(type_buf), "%d",
431                             packet->code - PW_DHCP_OFFSET);
432                 }
433
434                 DEBUG(
435 #ifdef WITH_UDPFROMTO
436                 "Sending %s of id %08x from %s:%d to %s:%d\n",
437 #else
438                 "Sending %s of id %08x to %s:%d\n",
439 #endif
440                    name, (unsigned int) packet->id,
441 #ifdef WITH_UDPFROMTO
442                    inet_ntop(packet->src_ipaddr.af, &packet->src_ipaddr.ipaddr, src_ip_buf, sizeof(src_ip_buf)),
443                    packet->src_port,
444 #endif
445                    inet_ntop(packet->dst_ipaddr.af, &packet->dst_ipaddr.ipaddr, dst_ip_buf, sizeof(dst_ip_buf)),
446                    packet->dst_port);
447         }
448
449 #ifndef WITH_UDPFROMTO
450         /*
451          *      Assume that the packet is encoded before sending it.
452          */
453         return sendto(packet->sockfd, packet->data, packet->data_len, 0,
454                       (struct sockaddr *)&dst, sizeof_dst);
455 #else
456
457         return sendfromto(packet->sockfd, packet->data, packet->data_len, 0,
458                           (struct sockaddr *)&src, sizeof_src,
459                           (struct sockaddr *)&dst, sizeof_dst);
460 #endif
461 }
462
463 static int fr_dhcp_attr2vp(RADIUS_PACKET *packet, VALUE_PAIR *vp, uint8_t const *p, size_t alen);
464
465 static int decode_tlv(RADIUS_PACKET *packet, VALUE_PAIR *tlv, uint8_t const *data, size_t data_len)
466 {
467         uint8_t const *p;
468         VALUE_PAIR *head, *vp;
469         vp_cursor_t cursor;
470
471         /*
472          *      Take a pass at parsing it.
473          */
474         p = data;
475         while (p < (data + data_len)) {
476                 if ((p + 2) > (data + data_len)) goto make_tlv;
477
478                 if ((p + p[1] + 2) > (data + data_len)) goto make_tlv;
479                 p += 2 + p[1];
480         }
481
482         /*
483          *      Got here... must be well formed.
484          */
485         head = NULL;
486         paircursor(&cursor, &head);
487
488         p = data;
489         while (p < (data + data_len)) {
490                 vp = paircreate(packet, tlv->da->attr | (p[0] << 8), DHCP_MAGIC_VENDOR);
491                 if (!vp) {
492                         pairfree(&head);
493                         goto make_tlv;
494                 }
495
496                 if (fr_dhcp_attr2vp(packet, vp, p + 2, p[1]) < 0) {
497                         pairfree(&head);
498                         goto make_tlv;
499                 }
500
501                 pairinsert(&cursor, vp);
502                 p += 2 + p[1];
503         }
504
505         /*
506          *      The caller allocated TLV, so we need to copy the FIRST
507          *      attribute over top of that.
508          *
509          *      This is a pretty awful hack, but we should be able to
510          *      clean it up when we get nested VPs so lets leave it for
511          *      now.
512          */
513         if (head) {
514                 /* Cleanup any old TLV data */
515                 talloc_free(tlv->vp_tlv);
516
517                 /* @fixme fragile */
518                 memcpy(tlv, head, sizeof(*tlv));
519
520                 /* If the VP has a talloced value we need to reparent it to the original TLV attribute */
521                 switch (head->da->type) {
522                         case PW_TYPE_STRING:
523                         case PW_TYPE_OCTETS:
524                         case PW_TYPE_TLV:
525                                 talloc_steal(tlv, head->data.ptr);
526                         default:
527                                 break;
528                 }
529                 tlv->next = head->next;
530                 talloc_free(head);
531         }
532
533         return 0;
534
535 make_tlv:
536         tlv->vp_tlv = talloc_array(tlv, uint8_t, data_len);
537         if (!tlv->vp_tlv) {
538                 fr_strerror_printf("No memory");
539                 return -1;
540         }
541         memcpy(tlv->vp_tlv, data, data_len);
542         tlv->length = data_len;
543
544         return 0;
545 }
546
547
548 /*
549  *      Decode ONE value into a VP
550  */
551 static int fr_dhcp_attr2vp(RADIUS_PACKET *packet, VALUE_PAIR *vp, uint8_t const *p, size_t alen)
552 {
553         char *q;
554
555         switch (vp->da->type) {
556         case PW_TYPE_BYTE:
557                 if (alen != 1) goto raw;
558                 vp->vp_integer = p[0];
559                 break;
560
561         case PW_TYPE_SHORT:
562                 if (alen != 2) goto raw;
563                 memcpy(&vp->vp_integer, p, 2);
564                 vp->vp_integer = ntohs(vp->vp_integer);
565                 break;
566
567         case PW_TYPE_INTEGER:
568                 if (alen != 4) goto raw;
569                 memcpy(&vp->vp_integer, p, 4);
570                 vp->vp_integer = ntohl(vp->vp_integer);
571                 break;
572
573         case PW_TYPE_IPADDR:
574                 if (alen != 4) goto raw;
575                 /*
576                  *      Keep value in Network Order!
577                  */
578                 memcpy(&vp->vp_ipaddr, p , 4);
579                 vp->length = 4;
580                 break;
581
582         case PW_TYPE_STRING:
583                 vp->vp_strvalue = q = talloc_array(vp, char, alen + 1);
584                 vp->type = VT_DATA;
585                 memcpy(q, p , alen);
586                 q[alen] = '\0';
587                 break;
588
589         case PW_TYPE_ETHERNET:
590                 memcpy(vp->vp_ether, p, sizeof(vp->vp_ether));
591                 vp->length = sizeof(vp->vp_ether);
592                 break;
593
594         /*
595          *      Value doesn't match up with attribute type, overwrite the
596          *      vp's original DICT_ATTR with an unknown one.
597          */
598         raw:
599                 if (pair2unknown(vp) < 0) return -1;
600
601         case PW_TYPE_OCTETS:
602                 if (alen > 253) return -1;
603                 pairmemcpy(vp, p, alen);
604                 break;
605
606         case PW_TYPE_TLV:
607                 return decode_tlv(packet, vp, p, alen);
608
609         default:
610                 fr_strerror_printf("Internal sanity check %d %d", vp->da->type, __LINE__);
611                 return -1;
612         } /* switch over type */
613
614         vp->length = alen;
615         return 0;
616 }
617
618 ssize_t fr_dhcp_decode_options(RADIUS_PACKET *packet,
619                                uint8_t const *data, size_t len, VALUE_PAIR **head)
620 {
621         int i;
622         VALUE_PAIR *vp;
623         vp_cursor_t cursor;
624         uint8_t const *p, *next;
625         next = data;
626
627         *head = NULL;
628         paircursor(&cursor, head);
629
630         /*
631          *      FIXME: This should also check sname && file fields.
632          *      See the dhcp_get_option() function above.
633          */
634         while (next < (data + len)) {
635                 int num_entries, alen;
636                 DICT_ATTR const *da;
637
638                 p = next;
639
640                 if (*p == 0) break;
641                 if (*p == 255) break; /* end of options signifier */
642                 if ((p + 2) > (data + len)) break;
643
644                 next = p + 2 + p[1];
645
646                 if (p[1] >= 253) {
647                         fr_strerror_printf("Attribute too long %u %u",
648                                            p[0], p[1]);
649                         continue;
650                 }
651
652                 da = dict_attrbyvalue(p[0], DHCP_MAGIC_VENDOR);
653                 if (!da) {
654                         fr_strerror_printf("Attribute not in our dictionary: %u",
655                                            p[0]);
656                         continue;
657                 }
658
659                 vp = NULL;
660                 num_entries = 1;
661                 alen = p[1];
662                 p += 2;
663
664                 /*
665                  *      Could be an array of bytes, integers, etc.
666                  */
667                 if (da->flags.array) {
668                         switch (da->type) {
669                         case PW_TYPE_BYTE:
670                                 num_entries = alen;
671                                 alen = 1;
672                                 break;
673
674                         case PW_TYPE_SHORT: /* ignore any trailing data */
675                                 num_entries = alen >> 1;
676                                 alen = 2;
677                                 break;
678
679                         case PW_TYPE_IPADDR:
680                         case PW_TYPE_INTEGER:
681                         case PW_TYPE_DATE: /* ignore any trailing data */
682                                 num_entries = alen >> 2;
683                                 alen = 4;
684                                 break;
685
686                         default:
687
688                                 break; /* really an internal sanity failure */
689                         }
690                 }
691
692                 /*
693                  *      Loop over all of the entries, building VPs
694                  */
695                 for (i = 0; i < num_entries; i++) {
696                         vp = pairmake(packet, NULL, da->name, NULL, T_OP_ADD);
697                         if (!vp) {
698                                 fr_strerror_printf("Cannot build attribute %s",
699                                         fr_strerror());
700                                 pairfree(head);
701                                 return -1;
702                         }
703
704                         /*
705                          *      Hack for ease of use.
706                          */
707                         if ((da->vendor == DHCP_MAGIC_VENDOR) &&
708                             (da->attr == 61) && !da->flags.array &&
709                             (alen == 7) && (*p == 1) && (num_entries == 1)) {
710                                 pairmemcpy(vp, p + 1, 6);
711
712                         } else if (fr_dhcp_attr2vp(packet, vp, p, alen) < 0) {
713                                 pairfree(&vp);
714                                 pairfree(head);
715                                 return -1;
716                         }
717
718                         pairinsert(&cursor, vp);
719
720                         for (vp = paircurrent(&cursor);
721                              vp;
722                              vp = pairnext(&cursor)) {
723                                 debug_pair(vp);
724                         }
725                         p += alen;
726                 } /* loop over array entries */
727         } /* loop over the entire packet */
728
729         return next - data;
730 }
731
732 int fr_dhcp_decode(RADIUS_PACKET *packet)
733 {
734         size_t i;
735         uint8_t *p;
736         uint32_t giaddr;
737         vp_cursor_t cursor;
738         VALUE_PAIR *head = NULL, *vp;
739         VALUE_PAIR *maxms, *mtu;
740
741         paircursor(&cursor, &head);
742         p = packet->data;
743
744         if ((fr_debug_flag > 2) && fr_log_fp) {
745                 for (i = 0; i < packet->data_len; i++) {
746                         if ((i & 0x0f) == 0x00) fprintf(fr_log_fp, "%d: ", (int) i);
747                         fprintf(fr_log_fp, "%02x ", packet->data[i]);
748                         if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
749                 }
750                 fprintf(fr_log_fp, "\n");
751         }
752
753         if (packet->data[1] != 1) {
754                 fr_strerror_printf("Packet is not Ethernet: %u",
755                       packet->data[1]);
756                 return -1;
757         }
758
759         /*
760          *      Decode the header.
761          */
762         for (i = 0; i < 14; i++) {
763                 char *q;
764
765                 vp = pairmake(packet, NULL, dhcp_header_names[i], NULL, T_OP_EQ);
766                 if (!vp) {
767                         char buffer[256];
768                         strlcpy(buffer, fr_strerror(), sizeof(buffer));
769                         fr_strerror_printf("Cannot decode packet due to internal error: %s", buffer);
770                         pairfree(&head);
771                         return -1;
772                 }
773
774                 /*
775                  *      If chaddr does != 6 bytes it's probably not ethernet, and we should store
776                  *      it as an opaque type (octets).
777                  */
778                 if ((i == 11) && (packet->data[1] == 1) && (packet->data[2] != sizeof(vp->vp_ether))) {
779                         DICT_ATTR const *da = dict_attrunknown(vp->da->attr, vp->da->vendor, true);
780                         if (!da) {
781                                 return -1;
782                         }
783                         vp->da = da;
784                 }
785
786                 switch (vp->da->type) {
787                 case PW_TYPE_BYTE:
788                         vp->vp_integer = p[0];
789                         vp->length = 1;
790                         break;
791
792                 case PW_TYPE_SHORT:
793                         vp->vp_integer = (p[0] << 8) | p[1];
794                         vp->length = 2;
795                         break;
796
797                 case PW_TYPE_INTEGER:
798                         memcpy(&vp->vp_integer, p, 4);
799                         vp->vp_integer = ntohl(vp->vp_integer);
800                         vp->length = 4;
801                         break;
802
803                 case PW_TYPE_IPADDR:
804                         memcpy(&vp->vp_ipaddr, p, 4);
805                         vp->length = 4;
806                         break;
807
808                 case PW_TYPE_STRING:
809                         vp->vp_strvalue = q = talloc_array(vp, char, dhcp_header_sizes[i] + 1);
810                         vp->type = VT_DATA;
811                         memcpy(q, p, dhcp_header_sizes[i]);
812                         q[dhcp_header_sizes[i]] = '\0';
813                         vp->length = strlen(vp->vp_strvalue);
814                         if (vp->length == 0) {
815                                 pairfree(&vp);
816                         }
817                         break;
818
819                 case PW_TYPE_OCTETS:
820                         pairmemcpy(vp, p, packet->data[2]);
821                         break;
822
823                 case PW_TYPE_ETHERNET:
824                         memcpy(vp->vp_ether, p, sizeof(vp->vp_ether));
825                         vp->length = sizeof(vp->vp_ether);
826                         break;
827
828                 default:
829                         fr_strerror_printf("BAD TYPE %d", vp->da->type);
830                         pairfree(&vp);
831                         break;
832                 }
833                 p += dhcp_header_sizes[i];
834
835                 if (!vp) continue;
836
837                 debug_pair(vp);
838                 pairinsert(&cursor, vp);
839         }
840
841         /*
842          *      Loop over the options.
843          */
844
845         /*
846          *      Nothing uses tail after this call, if it does in the future
847          *      it'll need to find the new tail...
848          */
849         {
850                 VALUE_PAIR *options = NULL;
851
852                 if (fr_dhcp_decode_options(packet,
853                                            packet->data + 240, packet->data_len - 240,
854                                            &options) < 0) {
855                         return -1;
856                 }
857
858                 if (options) {
859                         pairinsert(&cursor, options);
860                 }
861         }
862
863         /*
864          *      If DHCP request, set ciaddr to zero.
865          */
866
867         /*
868          *      Set broadcast flag for broken vendors, but only if
869          *      giaddr isn't set.
870          */
871         memcpy(&giaddr, packet->data + 24, sizeof(giaddr));
872         if (giaddr == htonl(INADDR_ANY)) {
873                 /*
874                  *      DHCP Opcode is request
875                  */
876                 vp = pairfind(head, 256, DHCP_MAGIC_VENDOR, TAG_ANY);
877                 if (vp && vp->vp_integer == 3) {
878                         /*
879                          *      Vendor is "MSFT 98"
880                          */
881                         vp = pairfind(head, 63, DHCP_MAGIC_VENDOR, TAG_ANY);
882                         if (vp && (strcmp(vp->vp_strvalue, "MSFT 98") == 0)) {
883                                 vp = pairfind(head, 262, DHCP_MAGIC_VENDOR, TAG_ANY);
884
885                                 /*
886                                  *      Reply should be broadcast.
887                                  */
888                                 if (vp) vp->vp_integer |= 0x8000;
889                                 packet->data[10] |= 0x80;
890                         }
891                 }
892         }
893
894         /*
895          *      FIXME: Nuke attributes that aren't used in the normal
896          *      header for discover/requests.
897          */
898         packet->vps = head;
899
900         /*
901          *      Client can request a LARGER size, but not a smaller
902          *      one.  They also cannot request a size larger than MTU.
903          */
904         maxms = pairfind(packet->vps, 57, DHCP_MAGIC_VENDOR, TAG_ANY);
905         mtu = pairfind(packet->vps, 26, DHCP_MAGIC_VENDOR, TAG_ANY);
906
907         if (mtu && (mtu->vp_integer < DEFAULT_PACKET_SIZE)) {
908                 fr_strerror_printf("DHCP Fatal: Client says MTU is smaller than minimum permitted by the specification.");
909                 return -1;
910         }
911
912         if (maxms && (maxms->vp_integer < DEFAULT_PACKET_SIZE)) {
913                 fr_strerror_printf("DHCP WARNING: Client says maximum message size is smaller than minimum permitted by the specification: fixing it");
914                 maxms->vp_integer = DEFAULT_PACKET_SIZE;
915         }
916
917         if (maxms && mtu && (maxms->vp_integer > mtu->vp_integer)) {
918                 fr_strerror_printf("DHCP WARNING: Client says MTU is smaller than maximum message size: fixing it");
919                 maxms->vp_integer = mtu->vp_integer;
920         }
921
922         if (fr_debug_flag) fflush(stdout);
923
924         return 0;
925 }
926
927
928 static int attr_cmp(void const *one, void const *two)
929 {
930         VALUE_PAIR const * const *a = one;
931         VALUE_PAIR const * const *b = two;
932
933         /*
934          *      DHCP-Message-Type is first, for simplicity.
935          */
936         if (((*a)->da->attr == 53) &&
937             (*b)->da->attr != 53) return -1;
938
939         /*
940          *      Relay-Agent is last
941          */
942         if (((*a)->da->attr == 82) &&
943             (*b)->da->attr != 82) return 1;
944
945         return ((*a)->da->attr - (*b)->da->attr);
946 }
947
948 /*
949  * @todo Check room!
950  */
951 static size_t fr_dhcp_vp2attr(VALUE_PAIR *vp, uint8_t *p, UNUSED size_t room)
952 {
953         size_t length;
954         uint32_t lvalue;
955
956         /*
957          *      Search for all attributes of the same
958          *      type, and pack them into the same
959          *      attribute.
960          */
961         switch (vp->da->type) {
962         case PW_TYPE_BYTE:
963                 length = 1;
964                 *p = vp->vp_integer & 0xff;
965                 break;
966
967         case PW_TYPE_SHORT:
968                 length = 2;
969                 p[0] = (vp->vp_integer >> 8) & 0xff;
970                 p[1] = vp->vp_integer & 0xff;
971                 break;
972
973         case PW_TYPE_INTEGER:
974                 length = 4;
975                 lvalue = htonl(vp->vp_integer);
976                 memcpy(p, &lvalue, 4);
977                 break;
978
979         case PW_TYPE_IPADDR:
980                 length = 4;
981                 memcpy(p, &vp->vp_ipaddr, 4);
982                 break;
983
984         case PW_TYPE_ETHERNET:
985                 length = 6;
986                 memcpy(p, &vp->vp_ether, 6);
987                 break;
988
989         case PW_TYPE_STRING:
990                 memcpy(p, vp->vp_strvalue, vp->length);
991                 length = vp->length;
992                 break;
993
994         case PW_TYPE_TLV:       /* FIXME: split it on 255? */
995                 memcpy(p, vp->vp_tlv, vp->length);
996                 length = vp->length;
997                 break;
998
999         case PW_TYPE_OCTETS:
1000                 memcpy(p, vp->vp_octets, vp->length);
1001                 length = vp->length;
1002                 break;
1003
1004         default:
1005                 fr_strerror_printf("BAD TYPE2 %d", vp->da->type);
1006                 length = 0;
1007                 break;
1008         }
1009
1010         return length;
1011 }
1012
1013 static VALUE_PAIR *fr_dhcp_vp2suboption(RADIUS_PACKET *packet, VALUE_PAIR *vps)
1014 {
1015         int length;
1016         unsigned int attribute;
1017         uint8_t *ptr;
1018         vp_cursor_t cursor;
1019         VALUE_PAIR *vp, *tlv;
1020
1021         attribute = vps->da->attr & 0xffff00ff;
1022
1023         tlv = paircreate(packet, attribute, DHCP_MAGIC_VENDOR);
1024         if (!tlv) return NULL;
1025
1026         tlv->length = 0;
1027         for (vp = paircursor(&cursor, &vps);
1028              vp;
1029              vp = pairnext(&cursor)) {
1030                 /*
1031                  *      Group the attributes ONLY until we see a
1032                  *      non-TLV attribute.
1033                  */
1034                 if (!vp->da->flags.is_tlv ||
1035                     vp->da->flags.extended ||
1036                     ((vp->da->attr & 0xffff00ff) != attribute)) {
1037                         break;
1038                 }
1039
1040                 tlv->length += vp->length + 2;
1041         }
1042
1043         if (!tlv->length) {
1044                 pairfree(&tlv);
1045                 return NULL;
1046         }
1047
1048         tlv->vp_tlv = talloc_array(tlv, uint8_t, tlv->length);
1049         if (!tlv->vp_tlv) {
1050                 pairfree(&tlv);
1051                 return NULL;
1052         }
1053
1054         ptr = tlv->vp_tlv;
1055         for (vp = paircursor(&cursor, &vps);
1056              vp;
1057              vp = pairnext(&cursor)) {
1058                 if (!vp->da->flags.is_tlv ||
1059                     vp->da->flags.extended ||
1060                     ((vp->da->attr & 0xffff00ff) != attribute)) {
1061                         break;
1062                 }
1063
1064                 length = fr_dhcp_vp2attr(vp, ptr + 2,
1065                                          tlv->vp_tlv + tlv->length - ptr);
1066                 if (length > 255) {
1067                         pairfree(&tlv);
1068                         return NULL;
1069                 }
1070
1071                 /*
1072                  *      Pack the attribute.
1073                  */
1074                 ptr[0] = (vp->da->attr & 0xff00) >> 8;
1075                 ptr[1] = length;
1076
1077                 ptr += length + 2;
1078         }
1079
1080         return tlv;
1081 }
1082
1083
1084 int fr_dhcp_encode(RADIUS_PACKET *packet)
1085 {
1086         unsigned int i, num_vps;
1087         uint8_t *p;
1088         vp_cursor_t cursor;
1089         VALUE_PAIR *vp;
1090         uint32_t lvalue;
1091         size_t dhcp_size, length;
1092 #ifndef NDEBUG
1093         char const *name;
1094 #  ifdef WITH_UDPFROMTO
1095         char src_ip_buf[256];
1096 #  endif
1097         char dst_ip_buf[256];
1098 #endif
1099
1100         if (packet->data) return 0;
1101
1102         packet->data_len = MAX_PACKET_SIZE;
1103         packet->data = talloc_zero_array(packet, uint8_t, packet->data_len);
1104
1105         /* XXX Ugly ... should be set by the caller */
1106         if (packet->code == 0) packet->code = PW_DHCP_NAK;
1107
1108 #ifndef NDEBUG
1109         if ((packet->code >= PW_DHCP_DISCOVER) &&
1110             (packet->code <= PW_DHCP_INFORM)) {
1111                 name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
1112         } else {
1113                 name = "?Unknown?";
1114         }
1115
1116         DEBUG(
1117 #  ifdef WITH_UDPFROMTO
1118               "Encoding %s of id %08x from %s:%d to %s:%d\n",
1119 #  else
1120               "Encoding %s of id %08x to %s:%d\n",
1121 #  endif
1122               name, (unsigned int) packet->id,
1123 #  ifdef WITH_UDPFROMTO
1124               inet_ntop(packet->src_ipaddr.af,
1125                         &packet->src_ipaddr.ipaddr,
1126                         src_ip_buf, sizeof(src_ip_buf)),
1127               packet->src_port,
1128 #  endif
1129               inet_ntop(packet->dst_ipaddr.af,
1130                         &packet->dst_ipaddr.ipaddr,
1131                      dst_ip_buf, sizeof(dst_ip_buf)),
1132               packet->dst_port);
1133 #endif
1134
1135         p = packet->data;
1136
1137         /*
1138          *      @todo: Make this work again.
1139          */
1140 #if 0
1141         mms = DEFAULT_PACKET_SIZE; /* maximum message size */
1142
1143         /*
1144          *      Clients can request a LARGER size, but not a
1145          *      smaller one.  They also cannot request a size
1146          *      larger than MTU.
1147          */
1148
1149         /* DHCP-DHCP-Maximum-Msg-Size */
1150         vp = pairfind(packet->vps, 57, DHCP_MAGIC_VENDOR, TAG_ANY);
1151         if (vp && (vp->vp_integer > mms)) {
1152                 mms = vp->vp_integer;
1153
1154                 if (mms > MAX_PACKET_SIZE) mms = MAX_PACKET_SIZE;
1155         }
1156 #endif
1157
1158         vp = pairfind(packet->vps, 256, DHCP_MAGIC_VENDOR, TAG_ANY);
1159         if (vp) {
1160                 *p++ = vp->vp_integer & 0xff;
1161         } else {
1162                 *p++ = 1;       /* client message */
1163         }
1164
1165         /* DHCP-Hardware-Type */
1166         if ((vp = pairfind(packet->vps, 257, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1167                 *p++ = vp->vp_integer & 0xFF;
1168         } else {
1169                 *p++ = 1;               /* hardware type = ethernet */
1170         }
1171
1172         /* DHCP-Hardware-Address-Length */
1173         if ((vp = pairfind(packet->vps, 258, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1174                 *p++ = vp->vp_integer & 0xFF;
1175         } else {
1176                 *p++ = 6;               /* 6 bytes of ethernet */
1177         }
1178
1179         /* DHCP-Hop-Count */
1180         if ((vp = pairfind(packet->vps, 259, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1181                 *p = vp->vp_integer & 0xff;
1182         }
1183         p++;
1184
1185         /* DHCP-Transaction-Id */
1186         if ((vp = pairfind(packet->vps, 260, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1187                 lvalue = htonl(vp->vp_integer);
1188         } else {
1189                 lvalue = fr_rand();
1190         }
1191         memcpy(p, &lvalue, 4);
1192         p += 4;
1193
1194         /* DHCP-Number-of-Seconds */
1195         if ((vp = pairfind(packet->vps, 261, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1196                 lvalue = htonl(vp->vp_integer);
1197                 memcpy(p, &lvalue, 2);
1198         }
1199         p += 2;
1200
1201         /* DHCP-Flags */
1202         if ((vp = pairfind(packet->vps, 262, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1203                 lvalue = htons(vp->vp_integer);
1204                 memcpy(p, &lvalue, 2);
1205         }
1206         p += 2;
1207
1208         /* DHCP-Client-IP-Address */
1209         if ((vp = pairfind(packet->vps, 263, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1210                 memcpy(p, &vp->vp_ipaddr, 4);
1211         }
1212         p += 4;
1213
1214         /* DHCP-Your-IP-address */
1215         if ((vp = pairfind(packet->vps, 264, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1216                 lvalue = vp->vp_ipaddr;
1217         } else {
1218                 lvalue = htonl(INADDR_ANY);
1219         }
1220         memcpy(p, &lvalue, 4);
1221         p += 4;
1222
1223         /* DHCP-Server-IP-Address */
1224         vp = pairfind(packet->vps, 265, DHCP_MAGIC_VENDOR, TAG_ANY);
1225
1226         /* DHCP-DHCP-Server-Identifier */
1227         if (!vp && (vp = pairfind(packet->vps, 54, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1228                 lvalue = vp->vp_ipaddr;
1229         } else {
1230                 lvalue = htonl(INADDR_ANY);
1231         }
1232         memcpy(p, &lvalue, 4);
1233         p += 4;
1234
1235         /*
1236          *      DHCP-Gateway-IP-Address
1237          */
1238         if ((vp = pairfind(packet->vps, 266, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1239                 lvalue = vp->vp_ipaddr;
1240         } else {
1241                 lvalue = htonl(INADDR_ANY);
1242         }
1243         memcpy(p, &lvalue, 4);
1244         p += 4;
1245
1246         /* DHCP-Client-Hardware-Address */
1247         if ((vp = pairfind(packet->vps, 267, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1248                 if (vp->length == sizeof(vp->vp_ether)) {
1249                         memcpy(p, vp->vp_ether, vp->length);
1250                 } /* else ignore it */
1251         }
1252         p += DHCP_CHADDR_LEN;
1253
1254         /* DHCP-Server-Host-Name */
1255         if ((vp = pairfind(packet->vps, 268, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1256                 if (vp->length > DHCP_SNAME_LEN) {
1257                         memcpy(p, vp->vp_strvalue, DHCP_SNAME_LEN);
1258                 } else {
1259                         memcpy(p, vp->vp_strvalue, vp->length);
1260                 }
1261         }
1262         p += DHCP_SNAME_LEN;
1263
1264         /*
1265          *      Copy over DHCP-Boot-Filename.
1266          *
1267          *      FIXME: This copy should be delayed until AFTER the options
1268          *      have been processed.  If there are too many options for
1269          *      the packet, then they go into the sname && filename fields.
1270          *      When that happens, the boot filename is passed as an option,
1271          *      instead of being placed verbatim in the filename field.
1272          */
1273
1274         /* DHCP-Boot-Filename */
1275         if ((vp = pairfind(packet->vps, 269, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1276                 if (vp->length > DHCP_FILE_LEN) {
1277                         memcpy(p, vp->vp_strvalue, DHCP_FILE_LEN);
1278                 } else {
1279                         memcpy(p, vp->vp_strvalue, vp->length);
1280                 }
1281         }
1282         p += DHCP_FILE_LEN;
1283
1284         /* DHCP magic number */
1285         lvalue = htonl(DHCP_OPTION_MAGIC_NUMBER);
1286         memcpy(p, &lvalue, 4);
1287         p += 4;
1288
1289         /*
1290          *      Print the header.
1291          */
1292         if (fr_debug_flag > 1) {
1293                 uint8_t *pp = p;
1294
1295                 p = packet->data;
1296
1297                 for (i = 0; i < 14; i++) {
1298                         char *q;
1299
1300                         vp = pairmake(packet, NULL,
1301                                       dhcp_header_names[i], NULL, T_OP_EQ);
1302                         if (!vp) {
1303                                 char buffer[256];
1304                                 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1305                                 fr_strerror_printf("Cannot decode packet due to internal error: %s", buffer);
1306                                 return -1;
1307                         }
1308
1309                         switch (vp->da->type) {
1310                         case PW_TYPE_BYTE:
1311                                 vp->vp_integer = p[0];
1312                                 vp->length = 1;
1313                                 break;
1314
1315                         case PW_TYPE_SHORT:
1316                                 vp->vp_integer = (p[0] << 8) | p[1];
1317                                 vp->length = 2;
1318                                 break;
1319
1320                         case PW_TYPE_INTEGER:
1321                                 memcpy(&vp->vp_integer, p, 4);
1322                                 vp->vp_integer = ntohl(vp->vp_integer);
1323                                 vp->length = 4;
1324                                 break;
1325
1326                         case PW_TYPE_IPADDR:
1327                                 memcpy(&vp->vp_ipaddr, p, 4);
1328                                 vp->length = 4;
1329                                 break;
1330
1331                         case PW_TYPE_STRING:
1332                                 vp->vp_strvalue = q = talloc_array(vp, char, dhcp_header_sizes[i] + 1);
1333                                 vp->type = VT_DATA;
1334                                 memcpy(q, p, dhcp_header_sizes[i]);
1335                                 q[dhcp_header_sizes[i]] = '\0';
1336                                 vp->length = strlen(vp->vp_strvalue);
1337                                 break;
1338
1339                         case PW_TYPE_OCTETS: /* only for Client HW Address */
1340                                 pairmemcpy(vp, p, packet->data[2]);
1341                                 break;
1342
1343                         case PW_TYPE_ETHERNET: /* only for Client HW Address */
1344                                 memcpy(vp->vp_ether, p, sizeof(vp->vp_ether));
1345                                 vp->length = sizeof(vp->vp_ether);
1346                                 break;
1347
1348                         default:
1349                                 fr_strerror_printf("Internal sanity check failed %d %d", vp->da->type, __LINE__);
1350                                 pairfree(&vp);
1351                                 break;
1352                         }
1353
1354                         p += dhcp_header_sizes[i];
1355
1356                         debug_pair(vp);
1357                         pairfree(&vp);
1358                 }
1359
1360                 /*
1361                  *      Jump over DHCP magic number, response, etc.
1362                  */
1363                 p = pp;
1364         }
1365
1366         /*
1367          *      Before packing the attributes, re-order them so that
1368          *      the array ones are all contiguous.  This simplifies
1369          *      the later code.
1370          */
1371         num_vps = 0;
1372         for (vp = paircursor(&cursor, &packet->vps);
1373              vp;
1374              vp = pairnext(&cursor)) {
1375                 num_vps++;
1376         }
1377         if (num_vps > 1) {
1378                 VALUE_PAIR **array;
1379
1380                 array = talloc_array(packet, VALUE_PAIR*, num_vps);
1381
1382                 i = 0;
1383                 for (vp = paircursor(&cursor, &packet->vps);
1384                      vp;
1385                      vp = pairnext(&cursor)) {
1386                         array[i++] = vp;
1387                 }
1388
1389                 /*
1390                  *      Sort the attributes.
1391                  */
1392                 qsort(array, (size_t) num_vps, sizeof(VALUE_PAIR *), attr_cmp);
1393
1394                 packet->vps = NULL;
1395                 paircursor(&cursor, &packet->vps);
1396                 for (i = 0; i < num_vps; i++) {
1397                         array[i]->next = NULL;
1398                         pairinsert(&cursor, array[i]);
1399                 }
1400                 talloc_free(array);
1401         }
1402
1403         p[0] = 0x35;            /* DHCP-Message-Type */
1404         p[1] = 1;
1405         p[2] = packet->code - PW_DHCP_OFFSET;
1406         p += 3;
1407
1408         /*
1409          *      Pack in the attributes.
1410          */
1411         vp = packet->vps;
1412         while (vp) {
1413                 unsigned int num_entries = 1;
1414                 VALUE_PAIR *same;
1415                 uint8_t *plength;
1416
1417                 if (vp->da->vendor != DHCP_MAGIC_VENDOR) goto next;
1418                 if (vp->da->attr == 53) goto next; /* already done */
1419                 if ((vp->da->attr > 255) &&
1420                     (DHCP_BASE_ATTR(vp->da->attr) != PW_DHCP_OPTION_82)) goto next;
1421
1422                 debug_pair(vp);
1423                 if (vp->da->flags.extended) goto next;
1424
1425                 for (same = paircursor(&cursor, &vp->next);
1426                      same;
1427                      same = pairnext(&cursor)) {
1428                         if (same->da->attr != vp->da->attr) break;
1429                         num_entries++;
1430                 }
1431
1432                 /*
1433                  *      For client-identifier
1434                  * @todo What's this meant to be doing?!
1435                  */
1436 #if 0
1437                 if ((vp->da->type == PW_TYPE_ETHERNET) &&
1438                     (vp->length == 6) &&
1439                     (num_entries == 1)) {
1440                         vp->da->type = PW_TYPE_OCTETS;
1441                         memmove(vp->vp_octets + 1, vp->vp_octets, 6);
1442                         vp->vp_octets[0] = 1;
1443                 }
1444 #endif
1445                 *(p++) = vp->da->attr & 0xff;
1446                 plength = p;
1447                 *(p++) = 0;     /* header isn't included in attr length */
1448
1449                 for (i = 0; i < num_entries; i++) {
1450                         if (i != 0) debug_pair(vp);
1451
1452                         if (vp->da->flags.is_tlv) {
1453                                 VALUE_PAIR *tlv;
1454
1455                                 /*
1456                                  *      Should NOT have been encoded yet!
1457                                  */
1458                                 tlv = fr_dhcp_vp2suboption(packet, vp);
1459
1460                                 /*
1461                                  *      Ignore it if there's an issue
1462                                  *      encoding it.
1463                                  */
1464                                 if (!tlv) goto next;
1465
1466                                 tlv->next = vp->next;
1467                                 vp->next = tlv;
1468                                 vp = tlv;
1469                         }
1470
1471                         length = fr_dhcp_vp2attr(vp, p, 0);
1472
1473                         /*
1474                          *      This will never happen due to FreeRADIUS
1475                          *      limitations: sizeof(vp->vp_octets) < 255
1476                          */
1477                         if (length > 255) {
1478                                 fr_strerror_printf("WARNING Ignoring too long attribute %s!", vp->da->name);
1479                                 break;
1480                         }
1481
1482                         /*
1483                          *      More than one attribute of the same type
1484                          *      in a row: they are packed together
1485                          *      into the same TLV.  If we overflow,
1486                          *      go bananas!
1487                          */
1488                         if ((*plength + length) > 255) {
1489                                 fr_strerror_printf("WARNING Ignoring too long attribute %s!", vp->da->name);
1490                                 break;
1491                         }
1492
1493                         *plength += length;
1494                         p += length;
1495
1496                         if (vp->next && (vp->next->da->attr == vp->da->attr)) {
1497                                 vp = vp->next;
1498                         }
1499                 } /* loop over num_entries */
1500
1501         next:
1502                 vp = vp->next;
1503         }
1504
1505         p[0] = 0xff;            /* end of option option */
1506         p[1] = 0x00;
1507         p += 2;
1508         dhcp_size = p - packet->data;
1509
1510         /*
1511          *      FIXME: if (dhcp_size > mms),
1512          *        then we put the extra options into the "sname" and "file"
1513          *        fields, AND set the "end option option" in the "options"
1514          *        field.  We also set the "overload option",
1515          *        and put options into the "file" field, followed by
1516          *        the "sname" field.  Where each option is completely
1517          *        enclosed in the "file" and/or "sname" field, AND
1518          *        followed by the "end of option", and MUST be followed
1519          *        by padding option.
1520          *
1521          *      Yuck.  That sucks...
1522          */
1523         packet->data_len = dhcp_size;
1524
1525         if (packet->data_len < DEFAULT_PACKET_SIZE) {
1526                 memset(packet->data + packet->data_len, 0,
1527                        DEFAULT_PACKET_SIZE - packet->data_len);
1528                 packet->data_len = DEFAULT_PACKET_SIZE;
1529         }
1530
1531         if ((fr_debug_flag > 2) && fr_log_fp) {
1532                 fprintf(fr_log_fp, "DHCP Sending %zu bytes\n", packet->data_len);
1533                 for (i = 0; i < packet->data_len; i++) {
1534                         if ((i & 0x0f) == 0x00) fprintf(fr_log_fp, "%d: ", i);
1535                         fprintf(fr_log_fp, "%02x ", packet->data[i]);
1536                         if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
1537                 }
1538                 fprintf(fr_log_fp, "\n");
1539         }
1540
1541         return 0;
1542 }
1543
1544 #ifdef SIOCSARP
1545 int fr_dhcp_add_arp_entry(int fd, char const *interface,
1546                           VALUE_PAIR *macaddr, VALUE_PAIR *ip)
1547 {
1548         struct sockaddr_in *sin;
1549         struct arpreq req;
1550
1551         if (!interface) {
1552                 fr_strerror_printf("No interface specified.  Cannot update ARP table");
1553                 return -1;
1554         }
1555
1556         if (!fr_assert(macaddr) ||
1557             !fr_assert((macaddr->da->type == PW_TYPE_ETHERNET) || (macaddr->da->type == PW_TYPE_OCTETS))) {
1558                 fr_strerror_printf("Wrong VP type (%s) for chaddr",
1559                                    fr_int2str(dict_attr_types, macaddr->da->type, "<invalid>"));
1560                 return -1;
1561         }
1562
1563         if (macaddr->length > sizeof(req.arp_ha.sa_data)) {
1564                 fr_strerror_printf("arp sa_data field too small (%zu octets) to contain chaddr (%zu octets)",
1565                                    sizeof(req.arp_ha.sa_data), macaddr->length);
1566                 return -1;
1567         }
1568
1569         memset(&req, 0, sizeof(req));
1570         sin = (struct sockaddr_in *) &req.arp_pa;
1571         sin->sin_family = AF_INET;
1572         sin->sin_addr.s_addr = ip->vp_ipaddr;
1573
1574         strlcpy(req.arp_dev, interface, sizeof(req.arp_dev));
1575
1576         if (macaddr->da->type == PW_TYPE_ETHERNET) {
1577                 memcpy(&req.arp_ha.sa_data, &macaddr->vp_ether, sizeof(macaddr->vp_ether));
1578         } else {
1579                 memcpy(&req.arp_ha.sa_data, macaddr->vp_octets, macaddr->length);
1580         }
1581
1582         req.arp_flags = ATF_COM;
1583         if (ioctl(fd, SIOCSARP, &req) < 0) {
1584                 fr_strerror_printf("Failed to add entry in ARP cache: %s (%d)", fr_syserror(errno), errno);
1585                 return -1;
1586         }
1587
1588         return 0;
1589 }
1590 #else
1591 int fr_dhcp_add_arp_entry(UNUSED int fd, UNUSED char const *interface,
1592                           UNUSED VALUE_PAIR *macaddr, UNUSED VALUE_PAIR *ip)
1593 {
1594         fr_strerror_printf("Adding ARP entry is unsupported on this system");
1595         return -1;
1596 }
1597 #endif