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