Large code cleanups.
[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",
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         memcpy(tlv, head, sizeof(*tlv));
442         head->next = NULL;
443         pairfree(&head);
444         return 0;
445
446 make_tlv:
447         tlv->vp_tlv = malloc(data_len);
448         if (!tlv->vp_tlv) {
449                 fr_strerror_printf("No memory");
450                 return -1;
451         }
452         memcpy(tlv->vp_tlv, data, data_len);
453         tlv->length = data_len;
454         
455         return 0;
456 }
457
458
459 /*
460  *      Decode ONE value into a VP
461  */
462 static int fr_dhcp_attr2vp(VALUE_PAIR *vp, const uint8_t *p, size_t alen)
463 {
464         switch (vp->type) {
465         case PW_TYPE_BYTE:
466                 if (alen != 1) goto raw;
467                 vp->vp_integer = p[0];
468                 break;
469                 
470         case PW_TYPE_SHORT:
471                 if (alen != 2) goto raw;
472                 vp->vp_integer = (p[0] << 8) | p[1];
473                 break;
474                 
475         case PW_TYPE_INTEGER:
476                 if (alen != 4) goto raw;
477                 memcpy(&vp->vp_integer, p, 4);
478                 vp->vp_integer = ntohl(vp->vp_integer);
479                 break;
480                 
481         case PW_TYPE_IPADDR:
482                 if (alen != 4) goto raw;
483                 memcpy(&vp->vp_ipaddr, p , 4);
484                 vp->length = 4;
485                 break;
486                 
487         case PW_TYPE_STRING:
488                 if (alen > 253) return -1;
489                 memcpy(vp->vp_strvalue, p , alen);
490                 vp->vp_strvalue[alen] = '\0';
491                 break;
492                 
493         raw:
494                 vp->type = PW_TYPE_OCTETS;
495
496         case PW_TYPE_OCTETS:
497                 if (alen > 253) return -1;
498                 memcpy(vp->vp_octets, p, alen);
499                 break;
500                 
501         case PW_TYPE_TLV:
502                 return decode_tlv(vp, p, alen);
503                 
504         default:
505                 fr_strerror_printf("Internal sanity check %d %d", vp->type, __LINE__);
506                 break;
507         } /* switch over type */
508
509         vp->length = alen;
510         return 0;
511 }
512
513 int fr_dhcp_decode(RADIUS_PACKET *packet)
514 {
515         int i;
516         uint8_t *p, *next;
517         uint32_t giaddr;
518         VALUE_PAIR *head, *vp, **tail;
519         VALUE_PAIR *maxms, *mtu;
520         char buffer[2048];
521
522         head = NULL;
523         tail = &head;
524         p = packet->data;
525         
526         if ((fr_debug_flag > 2) && fr_log_fp) {
527                 for (i = 0; i < packet->data_len; i++) {
528                         if ((i & 0x0f) == 0x00) fr_strerror_printf("%d: ", i);
529                         fprintf(fr_log_fp, "%02x ", packet->data[i]);
530                         if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
531                 }
532                 fprintf(fr_log_fp, "\n");
533         }
534
535         if (packet->data[1] != 1) {
536                 fr_strerror_printf("Packet is not Ethernet: %u",
537                       packet->data[1]);
538                 return -1;
539         }
540
541         /*
542          *      Decode the header.
543          */
544         for (i = 0; i < 14; i++) {
545                 vp = pairmake(dhcp_header_names[i], NULL, T_OP_EQ);
546                 if (!vp) {
547                         fr_strerror_printf("Parse error %s", fr_strerror());
548                         pairfree(&head);
549                         return -1;
550                 }
551
552                 if ((i == 11) && 
553                     (packet->data[1] == 1) &&
554                     (packet->data[2] == 6)) {
555                         vp->type = PW_TYPE_ETHERNET;
556                 }
557
558                 switch (vp->type) {
559                 case PW_TYPE_BYTE:
560                         vp->vp_integer = p[0];
561                         vp->length = 1;
562                         break;
563                         
564                 case PW_TYPE_SHORT:
565                         vp->vp_integer = (p[0] << 8) | p[1];
566                         vp->length = 2;
567                         break;
568                         
569                 case PW_TYPE_INTEGER:
570                         memcpy(&vp->vp_integer, p, 4);
571                         vp->vp_integer = ntohl(vp->vp_integer);
572                         vp->length = 4;
573                         break;
574                         
575                 case PW_TYPE_IPADDR:
576                         memcpy(&vp->vp_ipaddr, p, 4);
577                         vp->length = 4;
578                         break;
579                         
580                 case PW_TYPE_STRING:
581                         memcpy(vp->vp_strvalue, p, dhcp_header_sizes[i]);
582                         vp->vp_strvalue[dhcp_header_sizes[i]] = '\0';
583                         vp->length = strlen(vp->vp_strvalue);
584                         if (vp->length == 0) {
585                                 pairfree(&vp);
586                         }
587                         break;
588                         
589                 case PW_TYPE_OCTETS:
590                         memcpy(vp->vp_octets, p, packet->data[2]);
591                         vp->length = packet->data[2];
592                         break;
593                         
594                 case PW_TYPE_ETHERNET:
595                         memcpy(vp->vp_ether, p, sizeof(vp->vp_ether));
596                         vp->length = sizeof(vp->vp_ether);
597                         break;
598                         
599                 default:
600                         fr_strerror_printf("BAD TYPE %d", vp->type);
601                         pairfree(&vp);
602                         break;
603                 }
604                 p += dhcp_header_sizes[i];
605
606                 if (!vp) continue;
607                 
608                 if (fr_debug_flag > 1) {
609                         vp_prints(buffer, sizeof(buffer), vp);
610                         fr_strerror_printf("\t%s", buffer);
611                 }
612                 *tail = vp;
613                 tail = &vp->next;
614         }
615         
616         /*
617          *      Loop over the options.
618          */
619         next = packet->data + 240;
620
621         /*
622          *      FIXME: This should also check sname && file fields.
623          *      See the dhcp_get_option() function above.
624          */
625         while (next < (packet->data + packet->data_len)) {
626                 int num_entries, alen;
627                 DICT_ATTR *da;
628                 
629                 p = next;
630
631                 if (*p == 0) break;
632                 if (*p == 255) break; /* end of options signifier */
633                 if ((p + 2) > (packet->data + packet->data_len)) break;
634
635                 next = p + 2 + p[1];
636
637                 if (p[1] >= 253) {
638                         fr_strerror_printf("Attribute too long %u %u",
639                               p[0], p[1]);
640                         continue;
641                 }
642                                 
643                 da = dict_attrbyvalue(DHCP2ATTR(p[0]));
644                 if (!da) {
645                         fr_strerror_printf("Attribute not in our dictionary: %u",
646                               p[0]);
647                         continue;
648                 }
649
650                 vp = NULL;
651                 num_entries = 1;
652                 alen = p[1];
653                 p += 2;
654
655                 /*
656                  *      Could be an array of bytes, integers, etc.
657                  */
658                 if (da->flags.array) {
659                         switch (da->type) {
660                         case PW_TYPE_BYTE:
661                                 num_entries = alen;
662                                 alen = 1;
663                                 break;
664
665                         case PW_TYPE_SHORT: /* ignore any trailing data */
666                                 num_entries = alen >> 1;
667                                 alen = 2;
668                                 break;
669
670                         case PW_TYPE_IPADDR:
671                         case PW_TYPE_INTEGER:
672                         case PW_TYPE_DATE: /* ignore any trailing data */
673                                 num_entries = alen >> 2;
674                                 alen = 4;
675                                 break;
676
677                         default:
678
679                                 break; /* really an internal sanity failure */
680                         }
681                 }
682
683                 /*
684                  *      Loop over all of the entries, building VPs
685                  */
686                 for (i = 0; i < num_entries; i++) {
687                         vp = pairmake(da->name, NULL, T_OP_EQ);
688                         if (!vp) {
689                                 fr_strerror_printf("Cannot build attribute %s",
690                                         fr_strerror());
691                                 pairfree(&head);
692                                 return -1;
693                         }
694
695                         /*
696                          *      Hack for ease of use.
697                          */
698                         if ((da->attr == DHCP2ATTR(0x3d)) &&
699                             !da->flags.array &&
700                             (alen == 7) && (*p == 1) && (num_entries == 1)) {
701                                 vp->type = PW_TYPE_ETHERNET;
702                                 memcpy(vp->vp_octets, p + 1, 6);
703                                 vp->length = alen;
704
705                         } else if (fr_dhcp_attr2vp(vp, p, alen) < 0) {
706                                         pairfree(&vp);
707                                         pairfree(&head);
708                                         return -1;
709                         }
710
711                         if (fr_debug_flag > 1) {
712                                 vp_prints(buffer, sizeof(buffer), vp);
713                                 fr_strerror_printf("\t%s", buffer);
714                         }
715
716                         *tail = vp;
717                         while (*tail) tail = &vp->next;
718                         p += alen;
719                 } /* loop over array entries */
720         } /* loop over the entire packet */
721
722         /*
723          *      If DHCP request, set ciaddr to zero.
724          */
725
726         /*
727          *      Set broadcast flag for broken vendors, but only if
728          *      giaddr isn't set.
729          */
730         memcpy(&giaddr, packet->data + 24, sizeof(giaddr));
731         if (giaddr == htonl(INADDR_ANY)) {
732                 /*
733                  *      DHCP Opcode is request
734                  */
735                 vp = pairfind(head, DHCP2ATTR(256));
736                 if (vp && vp->lvalue == 3) {
737                         /*
738                          *      Vendor is "MSFT 98"
739                          */
740                         vp = pairfind(head, DHCP2ATTR(63));
741                         if (vp && (strcmp(vp->vp_strvalue, "MSFT 98") == 0)) {
742                                 vp = pairfind(head, DHCP2ATTR(262));
743
744                                 /*
745                                  *      Reply should be broadcast.
746                                  */
747                                 if (vp) vp->lvalue |= 0x8000;
748                                 packet->data[10] |= 0x80;                       
749                         }
750                 }
751         }
752
753         /*
754          *      FIXME: Nuke attributes that aren't used in the normal
755          *      header for discover/requests.
756          */
757         packet->vps = head;
758
759         /*
760          *      Client can request a LARGER size, but not a smaller
761          *      one.  They also cannot request a size larger than MTU.
762          */
763         maxms = pairfind(packet->vps, DHCP2ATTR(57));
764         mtu = pairfind(packet->vps, DHCP2ATTR(26));
765
766         if (mtu && (mtu->vp_integer < DEFAULT_PACKET_SIZE)) {
767                 fr_strerror_printf("DHCP Fatal: Client says MTU is smaller than minimum permitted by the specification.");
768                 return -1;
769         }
770
771         if (maxms && (maxms->vp_integer < DEFAULT_PACKET_SIZE)) {
772                 fr_strerror_printf("DHCP WARNING: Client says maximum message size is smaller than minimum permitted by the specification: fixing it");
773                 maxms->vp_integer = DEFAULT_PACKET_SIZE;
774         }
775
776         if (maxms && mtu && (maxms->vp_integer > mtu->vp_integer)) {
777                 fr_strerror_printf("DHCP WARNING: Client says MTU is smaller than maximum message size: fixing it");
778                 maxms->vp_integer = mtu->vp_integer;
779         }
780
781         if (fr_debug_flag) fflush(stdout);
782
783         return 0;
784 }
785
786
787 static int attr_cmp(const void *one, const void *two)
788 {
789         const VALUE_PAIR * const *a = one;
790         const VALUE_PAIR * const *b = two;
791
792         /*
793          *      DHCP-Message-Type is first, for simplicity.
794          */
795         if (((*a)->attribute == DHCP2ATTR(53)) &&
796             (*b)->attribute != DHCP2ATTR(53)) return -1;
797
798         /*
799          *      Relay-Agent is last
800          */
801         if (((*a)->attribute == DHCP2ATTR(82)) &&
802             (*b)->attribute != DHCP2ATTR(82)) return +1;
803
804         return ((*a)->attribute - (*b)->attribute);
805 }
806
807
808 static size_t fr_dhcp_vp2attr(VALUE_PAIR *vp, uint8_t *p, size_t room)
809 {
810         size_t length;
811         uint32_t lvalue;
812
813         /*
814          *      FIXME: Check room!
815          */
816         room = room;            /* -Wunused */
817
818         /*
819          *      Search for all attributes of the same
820          *      type, and pack them into the same
821          *      attribute.
822          */
823         switch (vp->type) {
824         case PW_TYPE_BYTE:
825                 length = 1;
826                 *p = vp->vp_integer & 0xff;
827                 break;
828                 
829         case PW_TYPE_SHORT:
830                 length = 2;
831                 p[0] = (vp->vp_integer >> 8) & 0xff;
832                 p[1] = vp->vp_integer & 0xff;
833                 break;
834                 
835         case PW_TYPE_INTEGER:
836                 length = 4;
837                 lvalue = htonl(vp->vp_integer);
838                 memcpy(p, &lvalue, 4);
839                 break;
840                 
841         case PW_TYPE_IPADDR:
842                 length = 4;
843                 memcpy(p, &vp->vp_ipaddr, 4);
844                 break;
845                 
846         case PW_TYPE_ETHERNET:
847                 length = 6;
848                 memcpy(p, &vp->vp_ether, 6);
849                 break;
850                 
851         case PW_TYPE_STRING:
852                 memcpy(p, vp->vp_strvalue, vp->length);
853                 length = vp->length;
854                 break;
855                 
856         case PW_TYPE_TLV:       /* FIXME: split it on 255? */
857                 memcpy(p, vp->vp_tlv, vp->length);
858                 length = vp->length;
859                 break;
860
861         case PW_TYPE_OCTETS:
862                 memcpy(p, vp->vp_octets, vp->length);
863                 length = vp->length;
864                 break;
865                 
866         default:
867                 fr_strerror_printf("BAD TYPE2 %d", vp->type);
868                 length = 0;
869                 break;
870         }
871
872         return length;
873 }
874
875 static VALUE_PAIR *fr_dhcp_vp2suboption(VALUE_PAIR *vps)
876 {
877         int length;
878         unsigned int attribute;
879         uint8_t *ptr;
880         VALUE_PAIR *vp, *tlv;
881
882         attribute = vps->attribute & 0xffff00ff;
883
884         tlv = paircreate(attribute, PW_TYPE_TLV);
885         if (!tlv) return NULL;
886
887         tlv->length = 0;
888         for (vp = vps; vp != NULL; vp = vp->next) {
889                 /*
890                  *      Group the attributes ONLY until we see a
891                  *      non-TLV attribute.
892                  */
893                 if (!vp->flags.is_tlv ||
894                     vp->flags.encoded ||
895                     ((vp->attribute & 0xffff00ff) != attribute)) {
896                         break;
897                 }
898
899                 tlv->length += vp->length + 2;
900         }
901
902         if (!tlv->length) {
903                 pairfree(&tlv);
904                 return NULL;
905         }
906
907         tlv->vp_tlv = malloc(tlv->length);
908         if (!tlv->vp_tlv) {
909                 pairfree(&tlv);
910                 return NULL;
911         }
912
913         ptr = tlv->vp_tlv;
914         for (vp = vps; vp != NULL; vp = vp->next) {
915                 if (!vp->flags.is_tlv ||
916                     vp->flags.encoded ||
917                     ((vp->attribute & 0xffff00ff) != attribute)) {
918                         break;
919                 }
920
921                 length = fr_dhcp_vp2attr(vp, ptr + 2,
922                                          tlv->vp_tlv + tlv->length - ptr);
923                 if (length > 255) return NULL;
924
925                 /*
926                  *      Pack the attribute.
927                  */
928                 ptr[0] = (vp->attribute & 0xff00) >> 8;
929                 ptr[1] = length;
930
931                 ptr += length + 2;
932                 vp->flags.encoded = 1;
933         }
934
935         return tlv;
936 }
937
938
939 int fr_dhcp_encode(RADIUS_PACKET *packet, RADIUS_PACKET *original)
940 {
941         int i, num_vps;
942         uint8_t *p;
943         VALUE_PAIR *vp;
944         uint32_t lvalue, mms;
945         size_t dhcp_size, length;
946         dhcp_packet_t *dhcp;
947         char buffer[1024];
948
949         if (packet->data) return 0;
950
951         packet->data = malloc(MAX_PACKET_SIZE);
952         if (!packet->data) return -1;
953
954         packet->data_len = MAX_PACKET_SIZE;
955
956         if (packet->code == 0) packet->code = PW_DHCP_NAK;
957
958         /*
959          *      If there's a request, use it as a template.
960          *      Otherwise, assume that the caller has set up
961          *      everything appropriately.
962          */
963         if (original) {
964                 packet->dst_ipaddr.af = AF_INET;
965                 packet->src_ipaddr.af = AF_INET;
966
967                 packet->dst_port = original->src_port;
968                 packet->src_port = original->dst_port;
969
970                 /*
971                  *      Note that for DHCP, we NEVER send the response
972                  *      to the source IP address of the request.  It
973                  *      may have traversed multiple relays, and we
974                  *      need to send the request to the relay closest
975                  *      to the client.
976                  *
977                  *      if giaddr, send to giaddr.
978                  *      if NAK, send broadcast packet
979                  *      if ciaddr, unicast to ciaddr
980                  *      if flags & 0x8000, broadcast (client request)
981                  *      if sent from 0.0.0.0, broadcast response
982                  *      unicast to client yiaddr
983                  */
984                 
985                 /*
986                  *      FIXME: alignment issues.  We likely don't want to
987                  *      de-reference the packet structure directly..
988                  */
989                 dhcp = (dhcp_packet_t *) original->data;
990                 
991                 if (dhcp->giaddr != htonl(INADDR_ANY)) {
992                         packet->dst_ipaddr.ipaddr.ip4addr.s_addr = dhcp->giaddr;
993                         
994                         if (dhcp->giaddr != htonl(INADDR_LOOPBACK)) {
995                                 packet->dst_port = original->dst_port;
996                         } else {
997                                 packet->dst_port = original->src_port; /* debugging */
998                         }
999                         
1000                 } else if (packet->code == PW_DHCP_NAK) {
1001                         packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_BROADCAST);
1002                         
1003                 } else if (dhcp->ciaddr != htonl(INADDR_ANY)) {
1004                         packet->dst_ipaddr.ipaddr.ip4addr.s_addr = dhcp->ciaddr;
1005                         
1006                 } else if ((dhcp->flags & 0x8000) != 0) {
1007                         packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_BROADCAST);
1008                         
1009                 } else if (packet->dst_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY)) {
1010                         packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_BROADCAST);
1011                         
1012                 } else if (dhcp->yiaddr != htonl(INADDR_ANY)) {
1013                         packet->dst_ipaddr.ipaddr.ip4addr.s_addr = dhcp->yiaddr;
1014                         
1015                 } else {
1016                         /* leave destination IP alone. */
1017                 }
1018
1019                 /*
1020                  *      Rewrite the source IP to be our own, if we know it.
1021                  */
1022                 if (packet->src_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_BROADCAST)) {
1023                         packet->src_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1024                 }
1025         } else {
1026                 memset(packet->data, 0, packet->data_len);
1027         }
1028
1029         if (fr_debug_flag > 1) {
1030                 char type_buf[64];
1031                 const char *name = type_buf;
1032                 char src_ip_buf[256], dst_ip_buf[256];
1033                 
1034                 if ((packet->code >= PW_DHCP_DISCOVER) &&
1035                     (packet->code <= PW_DHCP_INFORM)) {
1036                         name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
1037                 } else {
1038                         snprintf(type_buf, sizeof(type_buf), "%d",
1039                                  packet->code - PW_DHCP_OFFSET);
1040                 }
1041
1042                 DEBUG("Sending %s of id %08x from %s:%d to %s:%d",
1043                        name, (unsigned int) packet->id,
1044                        inet_ntop(packet->src_ipaddr.af,
1045                                  &packet->src_ipaddr.ipaddr,
1046                                  src_ip_buf, sizeof(src_ip_buf)),
1047                        packet->src_port,
1048                        inet_ntop(packet->dst_ipaddr.af,
1049                                  &packet->dst_ipaddr.ipaddr,
1050                                  dst_ip_buf, sizeof(dst_ip_buf)),
1051                        packet->dst_port);
1052                 fflush(stdout);
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         memset(p, 0, 192);      /* bootp legacy */
1205         p += 192;
1206
1207         lvalue = htonl(DHCP_OPTION_MAGIC_NUMBER); /* DHCP magic number */
1208         memcpy(p, &lvalue, 4);
1209         p += 4;
1210
1211         /*
1212          *      Print the header.
1213          */
1214         if (fr_debug_flag > 1) {
1215                 uint8_t *pp = p;
1216
1217                 p = packet->data;
1218
1219                 for (i = 0; i < 14; i++) {
1220                         vp = pairmake(dhcp_header_names[i], NULL, T_OP_EQ);
1221                         if (!vp) {
1222                                 fr_strerror_printf("Parse error %s", fr_strerror());
1223                                 return -1;
1224                         }
1225                         
1226                         switch (vp->type) {
1227                         case PW_TYPE_BYTE:
1228                                 vp->vp_integer = p[0];
1229                                 vp->length = 1;
1230                                 break;
1231                                 
1232                         case PW_TYPE_SHORT:
1233                                 vp->vp_integer = (p[0] << 8) | p[1];
1234                                 vp->length = 2;
1235                                 break;
1236                                 
1237                         case PW_TYPE_INTEGER:
1238                                 memcpy(&vp->vp_integer, p, 4);
1239                                 vp->vp_integer = ntohl(vp->vp_integer);
1240                                 vp->length = 4;
1241                                 break;
1242                                 
1243                         case PW_TYPE_IPADDR:
1244                                 memcpy(&vp->vp_ipaddr, p, 4);
1245                                 vp->length = 4;
1246                                 break;
1247                                 
1248                         case PW_TYPE_STRING:
1249                                 memcpy(vp->vp_strvalue, p, dhcp_header_sizes[i]);
1250                                 vp->vp_strvalue[dhcp_header_sizes[i]] = '\0';
1251                                 vp->length = strlen(vp->vp_strvalue);
1252                                 break;
1253                                 
1254                         case PW_TYPE_OCTETS: /* only for Client HW Address */
1255                                 memcpy(vp->vp_octets, p, packet->data[2]);
1256                                 vp->length = packet->data[2];
1257                                 break;
1258                                 
1259                         case PW_TYPE_ETHERNET: /* only for Client HW Address */
1260                                 memcpy(vp->vp_ether, p, sizeof(vp->vp_ether));
1261                                 vp->length = sizeof(vp->vp_ether);
1262                                 break;
1263                                 
1264                         default:
1265                                 fr_strerror_printf("Internal sanity check failed %d %d", vp->type, __LINE__);
1266                                 pairfree(&vp);
1267                                 break;
1268                         }
1269                         
1270                         p += dhcp_header_sizes[i];
1271                         
1272                         vp_prints(buffer, sizeof(buffer), vp);
1273                         fr_strerror_printf("\t%s", buffer);
1274                         pairfree(&vp);
1275                 }
1276
1277                 /*
1278                  *      Jump over DHCP magic number, response, etc.
1279                  */
1280                 p = pp;
1281         }
1282
1283         /*
1284          *      Before packing the attributes, re-order them so that
1285          *      the array ones are all contiguous.  This simplifies
1286          *      the later code.
1287          */
1288         num_vps = 0;
1289         for (vp = packet->vps; vp != NULL; vp = vp->next) {
1290                 num_vps++;
1291         }
1292         if (num_vps > 1) {
1293                 VALUE_PAIR **array, **last;
1294
1295                 array = malloc(num_vps * sizeof(VALUE_PAIR *));
1296                 
1297                 i = 0;
1298                 for (vp = packet->vps; vp != NULL; vp = vp->next) {
1299                         array[i++] = vp;
1300                 }
1301                 
1302                 /*
1303                  *      Sort the attributes.
1304                  */
1305                 qsort(array, (size_t) num_vps, sizeof(VALUE_PAIR *),
1306                       attr_cmp);
1307                 
1308                 last = &packet->vps;
1309                 for (i = 0; i < num_vps; i++) {
1310                         *last = array[i];
1311                         array[i]->next = NULL;
1312                         last = &(array[i]->next);
1313                 }
1314                 free(array);
1315         }
1316
1317         p[0] = 0x35;            /* DHCP-Message-Type */
1318         p[1] = 1;
1319         p[2] = packet->code - PW_DHCP_OFFSET;
1320         p += 3;
1321
1322         /*
1323          *      Pack in the attributes.
1324          */
1325         vp = packet->vps;
1326         while (vp) {
1327                 int num_entries = 1;
1328                 
1329                 VALUE_PAIR *same;
1330                 uint8_t *plength, *pattr;
1331
1332                 if (!IS_DHCP_ATTR(vp)) goto next;
1333                 if (vp->attribute == DHCP2ATTR(53)) goto next; /* already done */
1334                 if (((vp->attribute & 0xffff) > 255) &&
1335                     (DHCP_BASE_ATTR(vp->attribute) != PW_DHCP_OPTION_82)) goto next;
1336
1337                 length = vp->length;
1338
1339                 for (same = vp->next; same != NULL; same = same->next) {
1340                         if (same->attribute != vp->attribute) break;
1341                         num_entries++;
1342                 }
1343
1344                 /*
1345                  *      For client-identifier
1346                  */
1347                 if ((vp->type == PW_TYPE_ETHERNET) &&
1348                     (vp->length == 6) &&
1349                     (num_entries == 1)) {
1350                         vp->type = PW_TYPE_OCTETS;
1351                         memmove(vp->vp_octets + 1, vp->vp_octets, 6);
1352                         vp->vp_octets[0] = 1;
1353                 }
1354
1355                 pattr = p;
1356                 *(p++) = vp->attribute & 0xff;
1357                 plength = p;
1358                 *(p++) = 0;     /* header isn't included in attr length */
1359
1360                 for (i = 0; i < num_entries; i++) {
1361                         if (fr_debug_flag > 1) {
1362                                 vp_prints(buffer, sizeof(buffer), vp);
1363                                 fr_strerror_printf("\t%s", buffer);
1364                         }
1365
1366                         if (vp->flags.is_tlv) {
1367                                 VALUE_PAIR *tlv = fr_dhcp_vp2suboption(vp);
1368                                 if (vp) {
1369                                         tlv->next = vp->next;
1370                                         vp->next = tlv;
1371                                 }
1372                                 
1373                                 /*
1374                                  *      The encoded flag MUST be set in the vp!
1375                                  */
1376                                 vp = vp->next;
1377                         }
1378
1379                         length = fr_dhcp_vp2attr(vp, p, 0);
1380
1381                         /*
1382                          *      This will never happen due to FreeRADIUS
1383                          *      limitations: sizeof(vp->vp_octets) < 255
1384                          */
1385                         if (length > 255) {
1386                                 fr_strerror_printf("WARNING Ignoring too long attribute %s!", vp->name);
1387                                 break;
1388                         }
1389
1390                         /*
1391                          *      More than one attribute of the same type
1392                          *      in a row: they are packed together
1393                          *      into the same TLV.  If we overflow,
1394                          *      go bananas!
1395                          */
1396                         if ((*plength + length) > 255) {
1397                                 fr_strerror_printf("WARNING Ignoring too long attribute %s!", vp->name);
1398                                 break;
1399                         }
1400                         
1401                         *plength += length;
1402                         p += length;
1403
1404                         if (vp->next &&
1405                             (vp->next->attribute == vp->attribute))
1406                                 vp = vp->next;
1407                 } /* loop over num_entries */
1408
1409         next:
1410                 vp = vp->next;
1411         }
1412
1413         p[0] = 0xff;            /* end of option option */
1414         p[1] = 0x00;
1415         p += 2;
1416         dhcp_size = p - packet->data;
1417
1418         /*
1419          *      FIXME: if (dhcp_size > mms),
1420          *        then we put the extra options into the "sname" and "file"
1421          *        fields, AND set the "end option option" in the "options"
1422          *        field.  We also set the "overload option",
1423          *        and put options into the "file" field, followed by
1424          *        the "sname" field.  Where each option is completely
1425          *        enclosed in the "file" and/or "sname" field, AND
1426          *        followed by the "end of option", and MUST be followed
1427          *        by padding option.
1428          *
1429          *      Yuck.  That sucks...
1430          */
1431         packet->data_len = dhcp_size;
1432
1433         if (original) {
1434                 /*
1435                  *      FIXME: This may set it to broadcast, which we don't
1436                  *      want.  Instead, set it to the real address of the
1437                  *      socket.
1438                  */
1439                 packet->src_ipaddr = original->dst_ipaddr;
1440         
1441                 packet->sockfd = original->sockfd;
1442         }
1443
1444         if (packet->data_len < DEFAULT_PACKET_SIZE) {
1445                 memset(packet->data + packet->data_len, 0,
1446                        DEFAULT_PACKET_SIZE - packet->data_len);
1447                 packet->data_len = DEFAULT_PACKET_SIZE;
1448         }
1449
1450         if ((fr_debug_flag > 2) && fr_log_fp) {
1451                 for (i = 0; i < packet->data_len; i++) {
1452                         if ((i & 0x0f) == 0x00) fprintf(fr_log_fp, "%d: ", i);
1453                         fprintf(fr_log_fp, "%02x ", packet->data[i]);
1454                         if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
1455                 }
1456                 fprintf(fr_log_fp, "\n");
1457         }
1458
1459         return 0;
1460 }
1461 #endif /* WITH_DHCP */