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