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