source: rtems-libbsd/dhcpcd/duid.c @ 338f300

55-freebsd-126-freebsd-12
Last change on this file since 338f300 was f2ed769, checked in by Sebastian Huber <sebastian.huber@…>, on 01/30/14 at 12:29:46

DHCPCD(8): Import

Import DHCPCD(8) from:

http://roy.marples.name/projects/dhcpcd/

The upstream sources can be obtained via:

fossil clone http://roy.marples.name/projects/dhcpcd

The imported version is 2014-01-29 19:46:44 [6b209507bb].

  • Property mode set to 100644
File size: 4.2 KB
Line 
1/*
2 * dhcpcd - DHCP client daemon
3 * Copyright (c) 2006-2013 Roy Marples <roy@marples.name>
4 * All rights reserved
5
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#define DUID_TIME_EPOCH 946684800
29#define DUID_LLT        1
30#define DUID_LL         3
31
32#include <sys/socket.h>
33#include <sys/types.h>
34
35#include <net/if.h>
36#include <net/if_arp.h>
37
38#include <errno.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <syslog.h>
43#include <time.h>
44#include <unistd.h>
45
46#ifndef ARPHRD_NETROM
47#  define ARPHRD_NETROM 0
48#endif
49
50#include "common.h"
51#include "duid.h"
52#include "net.h"
53
54unsigned char *duid = NULL;
55size_t duid_len = 0;
56
57static size_t
58duid_make(unsigned char *d, const struct interface *ifp, uint16_t type)
59{
60        unsigned char *p;
61        uint16_t u16;
62        time_t t;
63        uint32_t u32;
64
65        p = d;
66        u16 = htons(type);
67        memcpy(p, &u16, 2);
68        p += 2;
69        u16 = htons(ifp->family);
70        memcpy(p, &u16, 2);
71        p += 2;
72        if (type == DUID_LLT) {
73                /* time returns seconds from jan 1 1970, but DUID-LLT is
74                 * seconds from jan 1 2000 modulo 2^32 */
75                t = time(NULL) - DUID_TIME_EPOCH;
76                u32 = htonl(t & 0xffffffff);
77                memcpy(p, &u32, 4);
78                p += 4;
79        }
80        /* Finally, add the MAC address of the interface */
81        memcpy(p, ifp->hwaddr, ifp->hwlen);
82        p += ifp->hwlen;
83        return p - d;
84}
85
86static size_t
87duid_get(unsigned char *d, const struct interface *ifp)
88{
89        FILE *f;
90        int x = 0;
91        size_t len = 0;
92        char *line;
93        const struct interface *ifp2;
94
95        /* If we already have a DUID then use it as it's never supposed
96         * to change once we have one even if the interfaces do */
97        if ((f = fopen(DUID, "r"))) {
98                while ((line = get_line(f))) {
99                        len = hwaddr_aton(NULL, line);
100                        if (len && len <= DUID_LEN) {
101                                hwaddr_aton(d, line);
102                                break;
103                        }
104                        len = 0;
105                }
106                fclose(f);
107                if (len)
108                        return len;
109        } else {
110                if (errno != ENOENT)
111                        syslog(LOG_ERR, "error reading DUID: %s: %m", DUID);
112        }
113
114        /* No file? OK, lets make one based on our interface */
115        if (ifp->family == ARPHRD_NETROM) {
116                syslog(LOG_WARNING, "%s: is a NET/ROM psuedo interface",
117                    ifp->name);
118                TAILQ_FOREACH(ifp2, ifaces, next) {
119                        if (ifp2->family != ARPHRD_NETROM)
120                                break;
121                }
122                if (ifp2) {
123                        ifp = ifp2;
124                        syslog(LOG_WARNING,
125                            "picked interface %s to generate a DUID",
126                            ifp->name);
127                } else {
128                        syslog(LOG_WARNING,
129                            "no interfaces have a fixed hardware address");
130                        return duid_make(d, ifp, DUID_LL);
131                }
132        }
133
134        if (!(f = fopen(DUID, "w"))) {
135                syslog(LOG_ERR, "error writing DUID: %s: %m", DUID);
136                return duid_make(d, ifp, DUID_LL);
137        }
138        len = duid_make(d, ifp, DUID_LLT);
139        x = fprintf(f, "%s\n", hwaddr_ntoa(d, len));
140        fclose(f);
141        /* Failed to write the duid? scrub it, we cannot use it */
142        if (x < 1) {
143                syslog(LOG_ERR, "error writing DUID: %s: %m", DUID);
144                unlink(DUID);
145                return duid_make(d, ifp, DUID_LL);
146        }
147        return len;
148}
149
150size_t duid_init(const struct interface *ifp)
151{
152
153        if (duid == NULL) {
154                duid = malloc(DUID_LEN);
155                if (duid == NULL) {
156                        syslog(LOG_ERR, "%s: %m", __func__);
157                        return 0;
158                }
159                duid_len = duid_get(duid, ifp);
160        }
161        return duid_len;
162}
Note: See TracBrowser for help on using the repository browser.