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