14 #include <libubox/utils.h>
15 #include <libubox/avl.h>
16 #include <libubox/avl-cmp.h>
17 #include <libubox/list.h>
18 #include <libubox/vlist.h>
22 static struct blob_buf b
;
23 static int reload_pipe
[2] = { -1, -1 };
25 static int lease_cmp(const void *k1
, const void *k2
, void *ptr
);
26 static void lease_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
27 struct vlist_node
*node_old
);
29 struct vlist_tree leases
= VLIST_TREE_INIT(leases
, lease_cmp
, lease_update
, true, false);
30 AVL_TREE(interfaces
, avl_strcmp
, false, NULL
);
31 struct config config
= {.legacy
= false, .main_dhcpv4
= false,
32 .dhcp_cb
= NULL
, .dhcp_statefile
= NULL
,
33 .log_level
= LOG_WARNING
};
35 #define START_DEFAULT 100
36 #define LIMIT_DEFAULT 150
38 #define HOSTID_LEN_MIN 12
39 #define HOSTID_LEN_MAX 64
40 #define HOSTID_LEN_DEFAULT HOSTID_LEN_MIN
42 #define OAF_DHCPV6 (OAF_DHCPV6_NA | OAF_DHCPV6_PD)
48 IFACE_ATTR_DYNAMICDHCP
,
60 IFACE_ATTR_DNS_SERVICE
,
62 IFACE_ATTR_FILTER_CLASS
,
63 IFACE_ATTR_DHCPV4_FORCERECONF
,
64 IFACE_ATTR_DHCPV6_RAW
,
65 IFACE_ATTR_DHCPV6_ASSIGNALL
,
68 IFACE_ATTR_DHCPV6_HOSTID_LEN
,
69 IFACE_ATTR_RA_DEFAULT
,
70 IFACE_ATTR_RA_MANAGEMENT
,
73 IFACE_ATTR_RA_OFFLINK
,
74 IFACE_ATTR_RA_PREFERENCE
,
75 IFACE_ATTR_RA_ADVROUTER
,
76 IFACE_ATTR_RA_MININTERVAL
,
77 IFACE_ATTR_RA_MAXINTERVAL
,
78 IFACE_ATTR_RA_LIFETIME
,
79 IFACE_ATTR_RA_USELEASETIME
,
80 IFACE_ATTR_RA_REACHABLETIME
,
81 IFACE_ATTR_RA_RETRANSTIME
,
82 IFACE_ATTR_RA_HOPLIMIT
,
85 IFACE_ATTR_PD_MANAGER
,
87 IFACE_ATTR_NDPROXY_ROUTING
,
88 IFACE_ATTR_NDPROXY_SLAVE
,
89 IFACE_ATTR_PREFIX_FILTER
,
90 IFACE_ATTR_PREFERRED_LIFETIME
,
95 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
96 [IFACE_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
97 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
98 [IFACE_ATTR_NETWORKID
] = { .name
= "networkid", .type
= BLOBMSG_TYPE_STRING
},
99 [IFACE_ATTR_DYNAMICDHCP
] = { .name
= "dynamicdhcp", .type
= BLOBMSG_TYPE_BOOL
},
100 [IFACE_ATTR_LEASETIME
] = { .name
= "leasetime", .type
= BLOBMSG_TYPE_STRING
},
101 [IFACE_ATTR_START
] = { .name
= "start", .type
= BLOBMSG_TYPE_INT32
},
102 [IFACE_ATTR_LIMIT
] = { .name
= "limit", .type
= BLOBMSG_TYPE_INT32
},
103 [IFACE_ATTR_MASTER
] = { .name
= "master", .type
= BLOBMSG_TYPE_BOOL
},
104 [IFACE_ATTR_UPSTREAM
] = { .name
= "upstream", .type
= BLOBMSG_TYPE_ARRAY
},
105 [IFACE_ATTR_RA
] = { .name
= "ra", .type
= BLOBMSG_TYPE_STRING
},
106 [IFACE_ATTR_DHCPV4
] = { .name
= "dhcpv4", .type
= BLOBMSG_TYPE_STRING
},
107 [IFACE_ATTR_DHCPV6
] = { .name
= "dhcpv6", .type
= BLOBMSG_TYPE_STRING
},
108 [IFACE_ATTR_NDP
] = { .name
= "ndp", .type
= BLOBMSG_TYPE_STRING
},
109 [IFACE_ATTR_ROUTER
] = { .name
= "router", .type
= BLOBMSG_TYPE_ARRAY
},
110 [IFACE_ATTR_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
111 [IFACE_ATTR_DNS_SERVICE
] = { .name
= "dns_service", .type
= BLOBMSG_TYPE_BOOL
},
112 [IFACE_ATTR_DOMAIN
] = { .name
= "domain", .type
= BLOBMSG_TYPE_ARRAY
},
113 [IFACE_ATTR_FILTER_CLASS
] = { .name
= "filter_class", .type
= BLOBMSG_TYPE_STRING
},
114 [IFACE_ATTR_DHCPV4_FORCERECONF
] = { .name
= "dhcpv4_forcereconf", .type
= BLOBMSG_TYPE_BOOL
},
115 [IFACE_ATTR_DHCPV6_RAW
] = { .name
= "dhcpv6_raw", .type
= BLOBMSG_TYPE_STRING
},
116 [IFACE_ATTR_DHCPV6_ASSIGNALL
] = { .name
="dhcpv6_assignall", .type
= BLOBMSG_TYPE_BOOL
},
117 [IFACE_ATTR_DHCPV6_PD
] = { .name
= "dhcpv6_pd", .type
= BLOBMSG_TYPE_BOOL
},
118 [IFACE_ATTR_DHCPV6_NA
] = { .name
= "dhcpv6_na", .type
= BLOBMSG_TYPE_BOOL
},
119 [IFACE_ATTR_DHCPV6_HOSTID_LEN
] = { .name
= "dhcpv6_hostidlength", .type
= BLOBMSG_TYPE_INT32
},
120 [IFACE_ATTR_PD_MANAGER
] = { .name
= "pd_manager", .type
= BLOBMSG_TYPE_STRING
},
121 [IFACE_ATTR_PD_CER
] = { .name
= "pd_cer", .type
= BLOBMSG_TYPE_STRING
},
122 [IFACE_ATTR_RA_DEFAULT
] = { .name
= "ra_default", .type
= BLOBMSG_TYPE_INT32
},
123 [IFACE_ATTR_RA_MANAGEMENT
] = { .name
= "ra_management", .type
= BLOBMSG_TYPE_INT32
},
124 [IFACE_ATTR_RA_FLAGS
] = { .name
= "ra_flags", . type
= BLOBMSG_TYPE_ARRAY
},
125 [IFACE_ATTR_RA_SLAAC
] = { .name
= "ra_slaac", .type
= BLOBMSG_TYPE_BOOL
},
126 [IFACE_ATTR_RA_OFFLINK
] = { .name
= "ra_offlink", .type
= BLOBMSG_TYPE_BOOL
},
127 [IFACE_ATTR_RA_PREFERENCE
] = { .name
= "ra_preference", .type
= BLOBMSG_TYPE_STRING
},
128 [IFACE_ATTR_RA_ADVROUTER
] = { .name
= "ra_advrouter", .type
= BLOBMSG_TYPE_BOOL
},
129 [IFACE_ATTR_RA_MININTERVAL
] = { .name
= "ra_mininterval", .type
= BLOBMSG_TYPE_INT32
},
130 [IFACE_ATTR_RA_MAXINTERVAL
] = { .name
= "ra_maxinterval", .type
= BLOBMSG_TYPE_INT32
},
131 [IFACE_ATTR_RA_LIFETIME
] = { .name
= "ra_lifetime", .type
= BLOBMSG_TYPE_INT32
},
132 [IFACE_ATTR_RA_USELEASETIME
] = { .name
= "ra_useleasetime", .type
= BLOBMSG_TYPE_BOOL
},
133 [IFACE_ATTR_RA_REACHABLETIME
] = { .name
= "ra_reachabletime", .type
= BLOBMSG_TYPE_INT32
},
134 [IFACE_ATTR_RA_RETRANSTIME
] = { .name
= "ra_retranstime", .type
= BLOBMSG_TYPE_INT32
},
135 [IFACE_ATTR_RA_HOPLIMIT
] = { .name
= "ra_hoplimit", .type
= BLOBMSG_TYPE_INT32
},
136 [IFACE_ATTR_RA_MTU
] = { .name
= "ra_mtu", .type
= BLOBMSG_TYPE_INT32
},
137 [IFACE_ATTR_RA_DNS
] = { .name
= "ra_dns", .type
= BLOBMSG_TYPE_BOOL
},
138 [IFACE_ATTR_NDPROXY_ROUTING
] = { .name
= "ndproxy_routing", .type
= BLOBMSG_TYPE_BOOL
},
139 [IFACE_ATTR_NDPROXY_SLAVE
] = { .name
= "ndproxy_slave", .type
= BLOBMSG_TYPE_BOOL
},
140 [IFACE_ATTR_PREFIX_FILTER
] = { .name
= "prefix_filter", .type
= BLOBMSG_TYPE_STRING
},
141 [IFACE_ATTR_PREFERRED_LIFETIME
] = { .name
= "preferred_lifetime", .type
= BLOBMSG_TYPE_STRING
},
142 [IFACE_ATTR_NTP
] = { .name
= "ntp", .type
= BLOBMSG_TYPE_ARRAY
},
145 static const struct uci_blob_param_info iface_attr_info
[IFACE_ATTR_MAX
] = {
146 [IFACE_ATTR_UPSTREAM
] = { .type
= BLOBMSG_TYPE_STRING
},
147 [IFACE_ATTR_DNS
] = { .type
= BLOBMSG_TYPE_STRING
},
148 [IFACE_ATTR_DOMAIN
] = { .type
= BLOBMSG_TYPE_STRING
},
151 const struct uci_blob_param_list interface_attr_list
= {
152 .n_params
= IFACE_ATTR_MAX
,
153 .params
= iface_attrs
,
154 .info
= iface_attr_info
,
157 const struct blobmsg_policy lease_attrs
[LEASE_ATTR_MAX
] = {
158 [LEASE_ATTR_IP
] = { .name
= "ip", .type
= BLOBMSG_TYPE_STRING
},
159 [LEASE_ATTR_MAC
] = { .name
= "mac", .type
= BLOBMSG_TYPE_STRING
},
160 [LEASE_ATTR_DUID
] = { .name
= "duid", .type
= BLOBMSG_TYPE_STRING
},
161 [LEASE_ATTR_HOSTID
] = { .name
= "hostid", .type
= BLOBMSG_TYPE_STRING
},
162 [LEASE_ATTR_LEASETIME
] = { .name
= "leasetime", .type
= BLOBMSG_TYPE_STRING
},
163 [LEASE_ATTR_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
166 const struct uci_blob_param_list lease_attr_list
= {
167 .n_params
= LEASE_ATTR_MAX
,
168 .params
= lease_attrs
,
173 ODHCPD_ATTR_MAINDHCP
,
174 ODHCPD_ATTR_LEASEFILE
,
175 ODHCPD_ATTR_LEASETRIGGER
,
176 ODHCPD_ATTR_LOGLEVEL
,
180 static const struct blobmsg_policy odhcpd_attrs
[ODHCPD_ATTR_MAX
] = {
181 [ODHCPD_ATTR_LEGACY
] = { .name
= "legacy", .type
= BLOBMSG_TYPE_BOOL
},
182 [ODHCPD_ATTR_MAINDHCP
] = { .name
= "maindhcp", .type
= BLOBMSG_TYPE_BOOL
},
183 [ODHCPD_ATTR_LEASEFILE
] = { .name
= "leasefile", .type
= BLOBMSG_TYPE_STRING
},
184 [ODHCPD_ATTR_LEASETRIGGER
] = { .name
= "leasetrigger", .type
= BLOBMSG_TYPE_STRING
},
185 [ODHCPD_ATTR_LOGLEVEL
] = { .name
= "loglevel", .type
= BLOBMSG_TYPE_INT32
},
188 const struct uci_blob_param_list odhcpd_attr_list
= {
189 .n_params
= ODHCPD_ATTR_MAX
,
190 .params
= odhcpd_attrs
,
193 static const struct { const char *name
; uint8_t flag
; } ra_flags
[] = {
194 { .name
= "managed-config", .flag
= ND_RA_FLAG_MANAGED
},
195 { .name
= "other-config", .flag
= ND_RA_FLAG_OTHER
},
196 { .name
= "home-agent", .flag
= ND_RA_FLAG_HOME_AGENT
},
197 { .name
= "none", . flag
= 0 },
201 static void set_interface_defaults(struct interface
*iface
)
203 iface
->ignore
= true;
204 iface
->dhcpv4
= MODE_DISABLED
;
205 iface
->dhcpv6
= MODE_DISABLED
;
206 iface
->ra
= MODE_DISABLED
;
207 iface
->ndp
= MODE_DISABLED
;
208 iface
->learn_routes
= 1;
209 iface
->dhcp_leasetime
= 43200;
210 iface
->preferred_lifetime
= 43200;
211 iface
->dhcpv4_start
.s_addr
= htonl(START_DEFAULT
);
212 iface
->dhcpv4_end
.s_addr
= htonl(START_DEFAULT
+ LIMIT_DEFAULT
- 1);
213 iface
->dhcpv6_assignall
= true;
214 iface
->dhcpv6_pd
= true;
215 iface
->dhcpv6_na
= true;
216 iface
->dhcpv6_hostid_len
= HOSTID_LEN_DEFAULT
;
217 iface
->dns_service
= true;
218 iface
->ra_flags
= ND_RA_FLAG_OTHER
;
219 iface
->ra_slaac
= true;
220 iface
->ra_maxinterval
= 600;
221 iface
->ra_mininterval
= iface
->ra_maxinterval
/3;
222 iface
->ra_lifetime
= -1;
223 iface
->ra_dns
= true;
226 static void clean_interface(struct interface
*iface
)
230 free(iface
->upstream
);
231 free(iface
->dhcpv4_router
);
232 free(iface
->dhcpv4_dns
);
233 free(iface
->dhcpv6_raw
);
234 free(iface
->filter_class
);
235 free(iface
->dhcpv4_ntp
);
236 free(iface
->dhcpv6_ntp
);
237 free(iface
->dhcpv6_sntp
);
238 memset(&iface
->ra
, 0, sizeof(*iface
) - offsetof(struct interface
, ra
));
239 set_interface_defaults(iface
);
242 static void close_interface(struct interface
*iface
)
244 avl_delete(&interfaces
, &iface
->avl
);
246 router_setup_interface(iface
, false);
247 dhcpv6_setup_interface(iface
, false);
248 ndp_setup_interface(iface
, false);
249 #ifdef DHCPV4_SUPPORT
250 dhcpv4_setup_interface(iface
, false);
253 clean_interface(iface
);
256 free(iface
->invalid_addr6
);
261 static int parse_mode(const char *mode
)
263 if (!strcmp(mode
, "disabled"))
264 return MODE_DISABLED
;
265 else if (!strcmp(mode
, "server"))
267 else if (!strcmp(mode
, "relay"))
269 else if (!strcmp(mode
, "hybrid"))
275 static int parse_ra_flags(uint8_t *flags
, struct blob_attr
*attr
)
277 struct blob_attr
*cur
;
280 blobmsg_for_each_attr(cur
, attr
, rem
) {
283 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
286 if (!blobmsg_check_attr(cur
, false))
289 for (i
= 0; ra_flags
[i
].name
; i
++) {
290 if (!strcmp(ra_flags
[i
].name
, blobmsg_get_string(cur
))) {
291 *flags
|= ra_flags
[i
].flag
;
296 if (!ra_flags
[i
].name
)
303 static void set_config(struct uci_section
*s
)
305 struct blob_attr
*tb
[ODHCPD_ATTR_MAX
], *c
;
307 blob_buf_init(&b
, 0);
308 uci_to_blob(&b
, s
, &odhcpd_attr_list
);
309 blobmsg_parse(odhcpd_attrs
, ODHCPD_ATTR_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
311 if ((c
= tb
[ODHCPD_ATTR_LEGACY
]))
312 config
.legacy
= blobmsg_get_bool(c
);
314 if ((c
= tb
[ODHCPD_ATTR_MAINDHCP
]))
315 config
.main_dhcpv4
= blobmsg_get_bool(c
);
317 if ((c
= tb
[ODHCPD_ATTR_LEASEFILE
])) {
318 free(config
.dhcp_statefile
);
319 config
.dhcp_statefile
= strdup(blobmsg_get_string(c
));
322 if ((c
= tb
[ODHCPD_ATTR_LEASETRIGGER
])) {
323 free(config
.dhcp_cb
);
324 config
.dhcp_cb
= strdup(blobmsg_get_string(c
));
327 if ((c
= tb
[ODHCPD_ATTR_LOGLEVEL
])) {
328 int log_level
= (blobmsg_get_u32(c
) & LOG_PRIMASK
);
330 if (config
.log_level
!= log_level
) {
331 config
.log_level
= log_level
;
332 setlogmask(LOG_UPTO(config
.log_level
));
337 static double parse_leasetime(struct blob_attr
*c
) {
338 char *val
= blobmsg_get_string(c
), *endptr
= NULL
;
339 double time
= strcmp(val
, "infinite") ? strtod(val
, &endptr
) : UINT32_MAX
;
341 if (time
&& endptr
&& endptr
[0]) {
342 if (endptr
[0] == 's')
344 else if (endptr
[0] == 'm')
346 else if (endptr
[0] == 'h')
348 else if (endptr
[0] == 'd')
350 else if (endptr
[0] == 'w')
351 time
*= 7 * 24 * 3600;
365 static void free_lease(struct lease
*l
)
372 int set_lease_from_blobmsg(struct blob_attr
*ba
)
374 struct blob_attr
*tb
[LEASE_ATTR_MAX
], *c
;
379 blobmsg_parse(lease_attrs
, LEASE_ATTR_MAX
, tb
, blob_data(ba
), blob_len(ba
));
381 if ((c
= tb
[LEASE_ATTR_DUID
]))
382 duidlen
= (blobmsg_data_len(c
) - 1) / 2;
384 l
= calloc_a(sizeof(*l
), &duid
, duidlen
);
388 if ((c
= tb
[LEASE_ATTR_MAC
]))
389 if (!ether_aton_r(blobmsg_get_string(c
), &l
->mac
))
392 if ((c
= tb
[LEASE_ATTR_DUID
])) {
396 len
= odhcpd_unhexlify(l
->duid
, duidlen
, blobmsg_get_string(c
));
404 if ((c
= tb
[LEASE_ATTR_NAME
])) {
405 l
->hostname
= strdup(blobmsg_get_string(c
));
406 if (!l
->hostname
|| !odhcpd_valid_hostname(l
->hostname
))
410 if ((c
= tb
[LEASE_ATTR_IP
]))
411 if (inet_pton(AF_INET
, blobmsg_get_string(c
), &l
->ipaddr
) < 0)
414 if ((c
= tb
[LEASE_ATTR_HOSTID
])) {
416 l
->hostid
= strtoull(blobmsg_get_string(c
), NULL
, 16);
420 uint32_t i4a
= ntohl(l
->ipaddr
) & 0xff;
421 l
->hostid
= ((i4a
/ 100) << 8) | (((i4a
% 100) / 10) << 4) | (i4a
% 10);
424 if ((c
= tb
[LEASE_ATTR_LEASETIME
])) {
425 double time
= parse_leasetime(c
);
432 INIT_LIST_HEAD(&l
->assignments
);
433 vlist_add(&leases
, &l
->node
, l
);
443 static int set_lease_from_uci(struct uci_section
*s
)
445 blob_buf_init(&b
, 0);
446 uci_to_blob(&b
, s
, &lease_attr_list
);
448 return set_lease_from_blobmsg(b
.head
);
451 /* Parse NTP Options for DHCPv6 Address */
452 static int parse_ntp_options(uint16_t *dhcpv6_ntp_len
, struct in6_addr addr6
, uint8_t **dhcpv6_ntp
)
454 uint16_t sub_opt
= 0, sub_len
= htons(IPV6_ADDR_LEN
);
455 uint16_t ntp_len
= IPV6_ADDR_LEN
+ 4;
456 uint8_t *ntp
= *dhcpv6_ntp
;
457 size_t pos
= *dhcpv6_ntp_len
;
459 ntp
= realloc(ntp
, pos
+ ntp_len
);
465 if (IN6_IS_ADDR_MULTICAST(&addr6
))
466 sub_opt
= htons(NTP_SUBOPTION_MC_ADDR
);
468 sub_opt
= htons(NTP_SUBOPTION_SRV_ADDR
);
470 memcpy(ntp
+ pos
, &sub_opt
, sizeof(sub_opt
));
471 pos
+= sizeof(sub_opt
);
472 memcpy(ntp
+ pos
, &sub_len
, sizeof(sub_len
));
473 pos
+= sizeof(sub_len
);
474 memcpy(ntp
+ pos
, &addr6
, IPV6_ADDR_LEN
);
476 *dhcpv6_ntp_len
+= ntp_len
;
481 /* Parse NTP Options for FQDN */
482 static int parse_ntp_fqdn(uint16_t *dhcpv6_ntp_len
, char *fqdn
, uint8_t **dhcpv6_ntp
)
484 size_t fqdn_len
= strlen(fqdn
);
485 uint16_t sub_opt
= 0, sub_len
= 0, ntp_len
= 0;
486 uint8_t *ntp
= *dhcpv6_ntp
;
487 size_t pos
= *dhcpv6_ntp_len
;
488 uint8_t buf
[256] = {0};
490 if (fqdn_len
> 0 && fqdn
[fqdn_len
- 1] == '.')
491 fqdn
[fqdn_len
- 1] = 0;
493 int len
= dn_comp(fqdn
, buf
, sizeof(buf
), NULL
, NULL
);
499 ntp
= realloc(ntp
, pos
+ ntp_len
);
505 sub_opt
= htons(NTP_SUBOPTION_SRV_FQDN
);
506 sub_len
= htons(len
);
508 memcpy(ntp
+ pos
, &sub_opt
, sizeof(sub_opt
));
509 pos
+= sizeof(sub_opt
);
510 memcpy(ntp
+ pos
, &sub_len
, sizeof(sub_len
));
511 pos
+= sizeof(sub_len
);
512 memcpy(ntp
+ pos
, buf
, len
);
514 *dhcpv6_ntp_len
+= ntp_len
;
519 int config_parse_interface(void *data
, size_t len
, const char *name
, bool overwrite
)
521 struct interface
*iface
;
522 struct blob_attr
*tb
[IFACE_ATTR_MAX
], *c
;
523 bool get_addrs
= false;
525 const char *ifname
= NULL
;
527 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
, data
, len
);
529 if (tb
[IFACE_ATTR_INTERFACE
])
530 name
= blobmsg_get_string(tb
[IFACE_ATTR_INTERFACE
]);
535 iface
= avl_find_element(&interfaces
, name
, iface
, avl
);
539 iface
= calloc_a(sizeof(*iface
), &new_name
, strlen(name
) + 1);
543 iface
->name
= strcpy(new_name
, name
);
544 iface
->avl
.key
= iface
->name
;
545 iface
->router_event
.uloop
.fd
= -1;
546 iface
->dhcpv6_event
.uloop
.fd
= -1;
547 iface
->ndp_event
.uloop
.fd
= -1;
548 iface
->ndp_ping_fd
= -1;
549 iface
->dhcpv4_event
.uloop
.fd
= -1;
550 INIT_LIST_HEAD(&iface
->ia_assignments
);
551 INIT_LIST_HEAD(&iface
->dhcpv4_assignments
);
552 INIT_LIST_HEAD(&iface
->dhcpv4_fr_ips
);
554 set_interface_defaults(iface
);
556 avl_insert(&interfaces
, &iface
->avl
);
557 get_addrs
= overwrite
= true;
561 if ((c
= tb
[IFACE_ATTR_IFNAME
]))
562 ifname
= blobmsg_get_string(c
);
563 else if ((c
= tb
[IFACE_ATTR_NETWORKID
]))
564 ifname
= blobmsg_get_string(c
);
568 if (overwrite
|| !iface
->ifname
)
569 ifname
= ubus_get_ifname(name
);
572 if (!iface
->ifname
&& !ifname
)
577 iface
->ifname
= strdup(ifname
);
582 if (!iface
->ifindex
&&
583 (iface
->ifindex
= if_nametoindex(iface
->ifname
)) <= 0)
586 if ((iface
->ifflags
= odhcpd_get_flags(iface
)) < 0)
591 ssize_t len
= netlink_get_interface_addrs(iface
->ifindex
,
592 true, &iface
->addr6
);
595 iface
->addr6_len
= len
;
597 len
= netlink_get_interface_addrs(iface
->ifindex
,
598 false, &iface
->addr4
);
600 iface
->addr4_len
= len
;
603 for (size_t i
= 0; i
< iface
->addr6_len
; i
++) {
604 struct odhcpd_ipaddr
*addr
= &iface
->addr6
[i
];
606 if (!addr
->tentative
) {
607 iface
->have_link_local
= true;
614 if ((c
= tb
[IFACE_ATTR_DYNAMICDHCP
]))
615 iface
->no_dynamic_dhcp
= !blobmsg_get_bool(c
);
617 if ((c
= tb
[IFACE_ATTR_LEASETIME
])) {
618 double time
= parse_leasetime(c
);
621 iface
->dhcp_leasetime
= time
;
623 syslog(LOG_ERR
, "Invalid %s value configured for interface '%s'",
624 iface_attrs
[IFACE_ATTR_LEASETIME
].name
, iface
->name
);
628 if ((c
= tb
[IFACE_ATTR_PREFERRED_LIFETIME
])) {
629 double time
= parse_leasetime(c
);
632 iface
->preferred_lifetime
= time
;
634 syslog(LOG_ERR
, "Invalid %s value configured for interface '%s'",
635 iface_attrs
[IFACE_ATTR_PREFERRED_LIFETIME
].name
, iface
->name
);
639 if ((c
= tb
[IFACE_ATTR_START
])) {
640 iface
->dhcpv4_start
.s_addr
= htonl(blobmsg_get_u32(c
));
641 iface
->dhcpv4_end
.s_addr
= htonl(ntohl(iface
->dhcpv4_start
.s_addr
) +
644 if (config
.main_dhcpv4
&& config
.legacy
)
645 iface
->dhcpv4
= MODE_SERVER
;
648 if ((c
= tb
[IFACE_ATTR_LIMIT
]))
649 iface
->dhcpv4_end
.s_addr
= htonl(ntohl(iface
->dhcpv4_start
.s_addr
) +
650 blobmsg_get_u32(c
) - 1);
652 if ((c
= tb
[IFACE_ATTR_MASTER
]))
653 iface
->master
= blobmsg_get_bool(c
);
655 if (overwrite
&& (c
= tb
[IFACE_ATTR_UPSTREAM
])) {
656 struct blob_attr
*cur
;
659 blobmsg_for_each_attr(cur
, c
, rem
) {
660 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
|| !blobmsg_check_attr(cur
, false))
663 iface
->upstream
= realloc(iface
->upstream
,
664 iface
->upstream_len
+ blobmsg_data_len(cur
));
665 if (!iface
->upstream
)
668 memcpy(iface
->upstream
+ iface
->upstream_len
, blobmsg_get_string(cur
), blobmsg_data_len(cur
));
669 iface
->upstream_len
+= blobmsg_data_len(cur
);
673 if ((c
= tb
[IFACE_ATTR_RA
])) {
674 if ((mode
= parse_mode(blobmsg_get_string(c
))) >= 0) {
677 if (iface
->ra
!= MODE_DISABLED
)
678 iface
->ignore
= false;
680 syslog(LOG_ERR
, "Invalid %s mode configured for interface '%s'",
681 iface_attrs
[IFACE_ATTR_RA
].name
, iface
->name
);
684 if ((c
= tb
[IFACE_ATTR_DHCPV4
])) {
685 if ((mode
= parse_mode(blobmsg_get_string(c
))) >= 0) {
686 if (config
.main_dhcpv4
) {
687 iface
->dhcpv4
= mode
;
689 if (iface
->dhcpv4
!= MODE_DISABLED
)
690 iface
->ignore
= false;
693 syslog(LOG_ERR
, "Invalid %s mode configured for interface %s",
694 iface_attrs
[IFACE_ATTR_DHCPV4
].name
, iface
->name
);
697 if ((c
= tb
[IFACE_ATTR_DHCPV6
])) {
698 if ((mode
= parse_mode(blobmsg_get_string(c
))) >= 0) {
699 iface
->dhcpv6
= mode
;
701 if (iface
->dhcpv6
!= MODE_DISABLED
)
702 iface
->ignore
= false;
704 syslog(LOG_ERR
, "Invalid %s mode configured for interface '%s'",
705 iface_attrs
[IFACE_ATTR_DHCPV6
].name
, iface
->name
);
708 if ((c
= tb
[IFACE_ATTR_NDP
])) {
709 if ((mode
= parse_mode(blobmsg_get_string(c
))) >= 0) {
712 if (iface
->ndp
!= MODE_DISABLED
)
713 iface
->ignore
= false;
715 syslog(LOG_ERR
, "Invalid %s mode configured for interface '%s'",
716 iface_attrs
[IFACE_ATTR_NDP
].name
, iface
->name
);
719 if ((c
= tb
[IFACE_ATTR_ROUTER
])) {
720 struct blob_attr
*cur
;
723 blobmsg_for_each_attr(cur
, c
, rem
) {
724 struct in_addr addr4
;
726 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
|| !blobmsg_check_attr(cur
, false))
729 if (inet_pton(AF_INET
, blobmsg_get_string(cur
), &addr4
) == 1) {
730 iface
->dhcpv4_router
= realloc(iface
->dhcpv4_router
,
731 (++iface
->dhcpv4_router_cnt
) * sizeof(*iface
->dhcpv4_router
));
732 if (!iface
->dhcpv4_router
)
735 iface
->dhcpv4_router
[iface
->dhcpv4_router_cnt
- 1] = addr4
;
737 syslog(LOG_ERR
, "Invalid %s value configured for interface '%s'",
738 iface_attrs
[IFACE_ATTR_ROUTER
].name
, iface
->name
);
742 if ((c
= tb
[IFACE_ATTR_DNS
])) {
743 struct blob_attr
*cur
;
746 iface
->always_rewrite_dns
= true;
747 blobmsg_for_each_attr(cur
, c
, rem
) {
748 struct in_addr addr4
;
749 struct in6_addr addr6
;
751 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
|| !blobmsg_check_attr(cur
, false))
754 if (inet_pton(AF_INET
, blobmsg_get_string(cur
), &addr4
) == 1) {
755 if (addr4
.s_addr
== INADDR_ANY
) {
756 syslog(LOG_ERR
, "Invalid %s value configured for interface '%s'",
757 iface_attrs
[IFACE_ATTR_DNS
].name
, iface
->name
);
762 iface
->dhcpv4_dns
= realloc(iface
->dhcpv4_dns
,
763 (++iface
->dhcpv4_dns_cnt
) * sizeof(*iface
->dhcpv4_dns
));
764 if (!iface
->dhcpv4_dns
)
767 iface
->dhcpv4_dns
[iface
->dhcpv4_dns_cnt
- 1] = addr4
;
768 } else if (inet_pton(AF_INET6
, blobmsg_get_string(cur
), &addr6
) == 1) {
769 if (IN6_IS_ADDR_UNSPECIFIED(&addr6
)) {
770 syslog(LOG_ERR
, "Invalid %s value configured for interface '%s'",
771 iface_attrs
[IFACE_ATTR_DNS
].name
, iface
->name
);
776 iface
->dns
= realloc(iface
->dns
,
777 (++iface
->dns_cnt
) * sizeof(*iface
->dns
));
781 iface
->dns
[iface
->dns_cnt
- 1] = addr6
;
783 syslog(LOG_ERR
, "Invalid %s value configured for interface '%s'",
784 iface_attrs
[IFACE_ATTR_DNS
].name
, iface
->name
);
788 if ((c
= tb
[IFACE_ATTR_DNS_SERVICE
]))
789 iface
->dns_service
= blobmsg_get_bool(c
);
791 if ((c
= tb
[IFACE_ATTR_DOMAIN
])) {
792 struct blob_attr
*cur
;
795 blobmsg_for_each_attr(cur
, c
, rem
) {
797 char *domain
= blobmsg_get_string(cur
);
798 size_t domainlen
= strlen(domain
);
801 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
|| !blobmsg_check_attr(cur
, false))
804 domain
= blobmsg_get_string(cur
);
805 domainlen
= strlen(domain
);
807 if (domainlen
> 0 && domain
[domainlen
- 1] == '.')
808 domain
[domainlen
- 1] = 0;
810 len
= dn_comp(domain
, buf
, sizeof(buf
), NULL
, NULL
);
812 syslog(LOG_ERR
, "Invalid %s value configured for interface '%s'",
813 iface_attrs
[IFACE_ATTR_DOMAIN
].name
, iface
->name
);
818 iface
->search
= realloc(iface
->search
, iface
->search_len
+ len
);
822 memcpy(&iface
->search
[iface
->search_len
], buf
, len
);
823 iface
->search_len
+= len
;
827 if ((c
= tb
[IFACE_ATTR_FILTER_CLASS
])) {
828 iface
->filter_class
= realloc(iface
->filter_class
, blobmsg_data_len(c
) + 1);
829 memcpy(iface
->filter_class
, blobmsg_get_string(c
), blobmsg_data_len(c
) + 1);
832 if ((c
= tb
[IFACE_ATTR_DHCPV4_FORCERECONF
]))
833 iface
->dhcpv4_forcereconf
= blobmsg_get_bool(c
);
835 if ((c
= tb
[IFACE_ATTR_DHCPV6_RAW
])) {
836 iface
->dhcpv6_raw_len
= blobmsg_data_len(c
) / 2;
837 iface
->dhcpv6_raw
= realloc(iface
->dhcpv6_raw
, iface
->dhcpv6_raw_len
);
838 odhcpd_unhexlify(iface
->dhcpv6_raw
, iface
->dhcpv6_raw_len
, blobmsg_get_string(c
));
841 if ((c
= tb
[IFACE_ATTR_DHCPV6_ASSIGNALL
]))
842 iface
->dhcpv6_assignall
= blobmsg_get_bool(c
);
844 if ((c
= tb
[IFACE_ATTR_DHCPV6_PD
]))
845 iface
->dhcpv6_pd
= blobmsg_get_bool(c
);
847 if ((c
= tb
[IFACE_ATTR_DHCPV6_NA
]))
848 iface
->dhcpv6_na
= blobmsg_get_bool(c
);
850 if ((c
= tb
[IFACE_ATTR_DHCPV6_HOSTID_LEN
])) {
851 uint32_t hostid_len
= blobmsg_get_u32(c
);
853 if (hostid_len
>= HOSTID_LEN_MIN
&& hostid_len
<= HOSTID_LEN_MAX
)
854 iface
->dhcpv6_hostid_len
= hostid_len
;
856 syslog(LOG_ERR
, "Invalid %s value configured for interface '%s'",
857 iface_attrs
[IFACE_ATTR_DHCPV6_HOSTID_LEN
].name
, iface
->name
);
861 if ((c
= tb
[IFACE_ATTR_RA_DEFAULT
]))
862 iface
->default_router
= blobmsg_get_u32(c
);
864 if (!tb
[IFACE_ATTR_RA_FLAGS
] && !tb
[IFACE_ATTR_RA_SLAAC
] &&
865 (c
= tb
[IFACE_ATTR_RA_MANAGEMENT
])) {
866 switch (blobmsg_get_u32(c
)) {
868 iface
->ra_flags
= ND_RA_FLAG_OTHER
;
869 iface
->ra_slaac
= true;
872 iface
->ra_flags
= ND_RA_FLAG_OTHER
|ND_RA_FLAG_MANAGED
;
873 iface
->ra_slaac
= true;
876 iface
->ra_flags
= ND_RA_FLAG_OTHER
|ND_RA_FLAG_MANAGED
;
877 iface
->ra_slaac
= false;
884 if ((c
= tb
[IFACE_ATTR_RA_FLAGS
])) {
887 if (parse_ra_flags(&iface
->ra_flags
, c
) < 0)
888 syslog(LOG_ERR
, "Invalid %s value configured for interface '%s'",
889 iface_attrs
[IFACE_ATTR_RA_FLAGS
].name
, iface
->name
);
892 if ((c
= tb
[IFACE_ATTR_RA_REACHABLETIME
])) {
893 uint32_t ra_reachabletime
= blobmsg_get_u32(c
);
895 if (ra_reachabletime
<= 3600000)
896 iface
->ra_reachabletime
= ra_reachabletime
;
898 syslog(LOG_ERR
, "Invalid %s value configured for interface '%s'",
899 iface_attrs
[IFACE_ATTR_RA_REACHABLETIME
].name
, iface
->name
);
902 if ((c
= tb
[IFACE_ATTR_RA_RETRANSTIME
])) {
903 uint32_t ra_retranstime
= blobmsg_get_u32(c
);
905 if (ra_retranstime
<= 60000)
906 iface
->ra_retranstime
= ra_retranstime
;
908 syslog(LOG_ERR
, "Invalid %s value configured for interface '%s'",
909 iface_attrs
[IFACE_ATTR_RA_RETRANSTIME
].name
, iface
->name
);
912 if ((c
= tb
[IFACE_ATTR_RA_HOPLIMIT
])) {
913 uint32_t ra_hoplimit
= blobmsg_get_u32(c
);
915 if (ra_hoplimit
<= 255)
916 iface
->ra_hoplimit
= ra_hoplimit
;
918 syslog(LOG_ERR
, "Invalid %s value configured for interface '%s'",
919 iface_attrs
[IFACE_ATTR_RA_HOPLIMIT
].name
, iface
->name
);
922 if ((c
= tb
[IFACE_ATTR_RA_MTU
])) {
923 uint32_t ra_mtu
= blobmsg_get_u32(c
);
925 if (ra_mtu
>= 1280 || ra_mtu
<= 65535)
926 iface
->ra_mtu
= ra_mtu
;
928 syslog(LOG_ERR
, "Invalid %s value configured for interface '%s'",
929 iface_attrs
[IFACE_ATTR_RA_MTU
].name
, iface
->name
);
932 if ((c
= tb
[IFACE_ATTR_RA_SLAAC
]))
933 iface
->ra_slaac
= blobmsg_get_bool(c
);
935 if ((c
= tb
[IFACE_ATTR_RA_OFFLINK
]))
936 iface
->ra_not_onlink
= blobmsg_get_bool(c
);
938 if ((c
= tb
[IFACE_ATTR_RA_ADVROUTER
]))
939 iface
->ra_advrouter
= blobmsg_get_bool(c
);
941 if ((c
= tb
[IFACE_ATTR_RA_MININTERVAL
]))
942 iface
->ra_mininterval
= blobmsg_get_u32(c
);
944 if ((c
= tb
[IFACE_ATTR_RA_MAXINTERVAL
]))
945 iface
->ra_maxinterval
= blobmsg_get_u32(c
);
947 if ((c
= tb
[IFACE_ATTR_RA_LIFETIME
]))
948 iface
->ra_lifetime
= blobmsg_get_u32(c
);
950 if ((c
= tb
[IFACE_ATTR_RA_USELEASETIME
]))
951 iface
->ra_useleasetime
= blobmsg_get_bool(c
);
953 if ((c
= tb
[IFACE_ATTR_RA_DNS
]))
954 iface
->ra_dns
= blobmsg_get_bool(c
);
956 if ((c
= tb
[IFACE_ATTR_RA_PREFERENCE
])) {
957 const char *prio
= blobmsg_get_string(c
);
959 if (!strcmp(prio
, "high"))
960 iface
->route_preference
= 1;
961 else if (!strcmp(prio
, "low"))
962 iface
->route_preference
= -1;
963 else if (!strcmp(prio
, "medium") || !strcmp(prio
, "default"))
964 iface
->route_preference
= 0;
966 syslog(LOG_ERR
, "Invalid %s mode configured for interface '%s'",
967 iface_attrs
[IFACE_ATTR_RA_PREFERENCE
].name
, iface
->name
);
970 if ((c
= tb
[IFACE_ATTR_PD_MANAGER
]))
971 strncpy(iface
->dhcpv6_pd_manager
, blobmsg_get_string(c
),
972 sizeof(iface
->dhcpv6_pd_manager
) - 1);
974 if ((c
= tb
[IFACE_ATTR_PD_CER
]) &&
975 inet_pton(AF_INET6
, blobmsg_get_string(c
), &iface
->dhcpv6_pd_cer
) < 1)
976 syslog(LOG_ERR
, "Invalid %s value configured for interface '%s'",
977 iface_attrs
[IFACE_ATTR_PD_CER
].name
, iface
->name
);
979 if ((c
= tb
[IFACE_ATTR_NDPROXY_ROUTING
]))
980 iface
->learn_routes
= blobmsg_get_bool(c
);
982 if ((c
= tb
[IFACE_ATTR_NDPROXY_SLAVE
]))
983 iface
->external
= blobmsg_get_bool(c
);
985 if ((c
= tb
[IFACE_ATTR_PREFIX_FILTER
])) {
986 const char *str
= blobmsg_get_string(c
);
987 char *astr
= malloc(strlen(str
) + 1);
991 if (!astr
|| !strcpy(astr
, str
) ||
992 (delim
= strchr(astr
, '/')) == NULL
|| (*(delim
++) = 0) ||
993 sscanf(delim
, "%i", &l
) == 0 || l
> 128 ||
994 inet_pton(AF_INET6
, astr
, &iface
->pio_filter_addr
) == 0)
995 iface
->pio_filter_length
= 0;
997 iface
->pio_filter_length
= l
;
1003 if (overwrite
&& (c
= tb
[IFACE_ATTR_NTP
])) {
1004 struct blob_attr
*cur
;
1007 blobmsg_for_each_attr(cur
, c
, rem
) {
1008 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
|| !blobmsg_check_attr(cur
, false))
1011 char *str
= blobmsg_get_string(cur
);
1012 struct in_addr addr4
;
1013 struct in6_addr addr6
;
1015 if (inet_pton(AF_INET
, str
, &addr4
) == 1) {
1016 if (addr4
.s_addr
== INADDR_ANY
)
1019 iface
->dhcpv4_ntp
= realloc(iface
->dhcpv4_ntp
,
1020 (++iface
->dhcpv4_ntp_cnt
) * sizeof(*iface
->dhcpv4_ntp
));
1021 if (!iface
->dhcpv4_ntp
)
1024 iface
->dhcpv4_ntp
[iface
->dhcpv4_ntp_cnt
- 1] = addr4
;
1025 } else if (inet_pton(AF_INET6
, str
, &addr6
) == 1) {
1026 if (IN6_IS_ADDR_UNSPECIFIED(&addr6
))
1029 iface
->dhcpv6_sntp
= realloc(iface
->dhcpv6_sntp
,
1030 (++iface
->dhcpv6_sntp_cnt
) * sizeof(*iface
->dhcpv6_sntp
));
1031 if (!iface
->dhcpv6_sntp
)
1034 iface
->dhcpv6_sntp
[iface
->dhcpv6_sntp_cnt
- 1] = addr6
;
1036 if (!parse_ntp_options(&iface
->dhcpv6_ntp_len
, addr6
, &iface
->dhcpv6_ntp
))
1037 iface
->dhcpv6_ntp_cnt
++;
1039 if (!parse_ntp_fqdn(&iface
->dhcpv6_ntp_len
, str
, &iface
->dhcpv6_ntp
))
1040 iface
->dhcpv6_ntp_cnt
++;
1048 close_interface(iface
);
1052 static int set_interface(struct uci_section
*s
)
1054 blob_buf_init(&b
, 0);
1055 uci_to_blob(&b
, s
, &interface_attr_list
);
1057 return config_parse_interface(blob_data(b
.head
), blob_len(b
.head
), s
->e
.name
, true);
1060 static void lease_delete_assignments(struct lease
*l
, bool v6
)
1062 struct dhcp_assignment
*a
, *tmp
;
1063 unsigned int flag
= v6
? OAF_DHCPV6
: OAF_DHCPV4
;
1065 list_for_each_entry_safe(a
, tmp
, &l
->assignments
, lease_list
) {
1066 if (a
->flags
& flag
)
1071 static void lease_update_assignments(struct lease
*l
)
1073 struct dhcp_assignment
*a
;
1075 list_for_each_entry(a
, &l
->assignments
, lease_list
) {
1081 a
->hostname
= strdup(l
->hostname
);
1083 a
->leasetime
= l
->leasetime
;
1087 static int lease_cmp(const void *k1
, const void *k2
, _unused
void *ptr
)
1089 const struct lease
*l1
= k1
, *l2
= k2
;
1092 if (l1
->duid_len
!= l2
->duid_len
)
1093 return l1
->duid_len
- l2
->duid_len
;
1095 if (l1
->duid_len
&& l2
->duid_len
)
1096 cmp
= memcmp(l1
->duid
, l2
->duid
, l1
->duid_len
);
1101 return memcmp(l1
->mac
.ether_addr_octet
, l2
->mac
.ether_addr_octet
,
1102 sizeof(l1
->mac
.ether_addr_octet
));
1105 static void lease_change_config(struct lease
*l_old
, struct lease
*l_new
)
1107 bool update
= false;
1109 if ((!!l_new
->hostname
!= !!l_old
->hostname
) ||
1110 (l_new
->hostname
&& strcmp(l_new
->hostname
, l_old
->hostname
))) {
1111 free(l_old
->hostname
);
1112 l_old
->hostname
= NULL
;
1114 if (l_new
->hostname
)
1115 l_old
->hostname
= strdup(l_new
->hostname
);
1120 if (l_old
->leasetime
!= l_new
->leasetime
) {
1121 l_old
->leasetime
= l_new
->leasetime
;
1125 if (l_old
->ipaddr
!= l_new
->ipaddr
) {
1126 l_old
->ipaddr
= l_new
->ipaddr
;
1127 lease_delete_assignments(l_old
, false);
1130 if (l_old
->hostid
!= l_new
->hostid
) {
1131 l_old
->hostid
= l_new
->hostid
;
1132 lease_delete_assignments(l_old
, true);
1136 lease_update_assignments(l_old
);
1141 static void lease_delete(struct lease
*l
)
1143 struct dhcp_assignment
*a
, *tmp
;
1145 list_for_each_entry_safe(a
, tmp
, &l
->assignments
, lease_list
)
1151 static void lease_update(_unused
struct vlist_tree
*tree
, struct vlist_node
*node_new
,
1152 struct vlist_node
*node_old
)
1154 struct lease
*lease_new
= container_of(node_new
, struct lease
, node
);
1155 struct lease
*lease_old
= container_of(node_old
, struct lease
, node
);
1157 if (node_old
&& node_new
)
1158 lease_change_config(lease_old
, lease_new
);
1160 lease_delete(lease_old
);
1163 struct lease
*config_find_lease_by_duid(const uint8_t *duid
, const uint16_t len
)
1167 vlist_for_each_element(&leases
, l
, node
) {
1168 if (l
->duid_len
== len
&& !memcmp(l
->duid
, duid
, len
))
1175 struct lease
*config_find_lease_by_mac(const uint8_t *mac
)
1179 vlist_for_each_element(&leases
, l
, node
) {
1180 if (!memcmp(l
->mac
.ether_addr_octet
, mac
,
1181 sizeof(l
->mac
.ether_addr_octet
)))
1188 struct lease
*config_find_lease_by_hostid(const uint64_t hostid
)
1192 vlist_for_each_element(&leases
, l
, node
) {
1193 if (l
->hostid
== hostid
)
1200 struct lease
*config_find_lease_by_ipaddr(const uint32_t ipaddr
)
1204 vlist_for_each_element(&leases
, l
, node
) {
1205 if (l
->ipaddr
== ipaddr
)
1212 void odhcpd_reload(void)
1214 struct uci_context
*uci
= uci_alloc_context();
1215 struct interface
*master
= NULL
, *i
, *tmp
;
1220 vlist_update(&leases
);
1221 avl_for_each_element(&interfaces
, i
, avl
)
1224 struct uci_package
*dhcp
= NULL
;
1225 if (!uci_load(uci
, "dhcp", &dhcp
)) {
1226 struct uci_element
*e
;
1228 /* 1. Global settings */
1229 uci_foreach_element(&dhcp
->sections
, e
) {
1230 struct uci_section
*s
= uci_to_section(e
);
1231 if (!strcmp(s
->type
, "odhcpd"))
1236 uci_foreach_element(&dhcp
->sections
, e
) {
1237 struct uci_section
*s
= uci_to_section(e
);
1238 if (!strcmp(s
->type
, "dhcp"))
1242 /* 3. Static leases */
1243 uci_foreach_element(&dhcp
->sections
, e
) {
1244 struct uci_section
* s
= uci_to_section(e
);
1245 if (!strcmp(s
->type
, "host"))
1246 set_lease_from_uci(s
);
1250 if (config
.dhcp_statefile
) {
1251 char *path
= strdup(config
.dhcp_statefile
);
1253 mkdir_p(dirname(path
), 0755);
1257 vlist_flush(&leases
);
1260 ubus_apply_network();
1263 bool any_dhcpv6_slave
= false, any_ra_slave
= false, any_ndp_slave
= false;
1266 avl_for_each_element(&interfaces
, i
, avl
) {
1270 if (i
->dhcpv6
== MODE_HYBRID
|| i
->dhcpv6
== MODE_RELAY
)
1271 any_dhcpv6_slave
= true;
1273 if (i
->ra
== MODE_HYBRID
|| i
->ra
== MODE_RELAY
)
1274 any_ra_slave
= true;
1276 if (i
->ndp
== MODE_HYBRID
|| i
->ndp
== MODE_RELAY
)
1277 any_ndp_slave
= true;
1280 /* Evaluate hybrid mode for master */
1281 avl_for_each_element(&interfaces
, i
, avl
) {
1285 enum odhcpd_mode hybrid_mode
= MODE_DISABLED
;
1287 if (!ubus_has_prefix(i
->name
, i
->ifname
))
1288 hybrid_mode
= MODE_RELAY
;
1291 if (i
->dhcpv6
== MODE_HYBRID
)
1292 i
->dhcpv6
= hybrid_mode
;
1294 if (i
->dhcpv6
== MODE_RELAY
&& !any_dhcpv6_slave
)
1295 i
->dhcpv6
= MODE_DISABLED
;
1297 if (i
->ra
== MODE_HYBRID
)
1298 i
->ra
= hybrid_mode
;
1300 if (i
->ra
== MODE_RELAY
&& !any_ra_slave
)
1301 i
->ra
= MODE_DISABLED
;
1303 if (i
->ndp
== MODE_HYBRID
)
1304 i
->ndp
= hybrid_mode
;
1306 if (i
->ndp
== MODE_RELAY
&& !any_ndp_slave
)
1307 i
->ndp
= MODE_DISABLED
;
1309 if (i
->dhcpv6
== MODE_RELAY
|| i
->ra
== MODE_RELAY
|| i
->ndp
== MODE_RELAY
)
1314 avl_for_each_element_safe(&interfaces
, i
, avl
, tmp
) {
1315 if (i
->inuse
&& i
->ifflags
& IFF_RUNNING
) {
1316 /* Resolve hybrid mode */
1317 if (i
->dhcpv6
== MODE_HYBRID
)
1318 i
->dhcpv6
= (master
&& master
->dhcpv6
== MODE_RELAY
) ?
1319 MODE_RELAY
: MODE_SERVER
;
1321 if (i
->ra
== MODE_HYBRID
)
1322 i
->ra
= (master
&& master
->ra
== MODE_RELAY
) ?
1323 MODE_RELAY
: MODE_SERVER
;
1325 if (i
->ndp
== MODE_HYBRID
)
1326 i
->ndp
= (master
&& master
->ndp
== MODE_RELAY
) ?
1327 MODE_RELAY
: MODE_DISABLED
;
1329 router_setup_interface(i
, i
->ra
!= MODE_DISABLED
);
1330 dhcpv6_setup_interface(i
, i
->dhcpv6
!= MODE_DISABLED
);
1331 ndp_setup_interface(i
, i
->ndp
!= MODE_DISABLED
);
1332 #ifdef DHCPV4_SUPPORT
1333 dhcpv4_setup_interface(i
, i
->dhcpv4
!= MODE_DISABLED
);
1339 uci_unload(uci
, dhcp
);
1340 uci_free_context(uci
);
1343 static void handle_signal(int signal
)
1347 if (signal
== SIGHUP
) {
1348 if (write(reload_pipe
[1], b
, sizeof(b
)) < 0) {}
1353 static void reload_cb(struct uloop_fd
*u
, _unused
unsigned int events
)
1356 if (read(u
->fd
, b
, sizeof(b
)) < 0) {}
1361 static struct uloop_fd reload_fd
= { .fd
= -1, .cb
= reload_cb
};
1363 void odhcpd_run(void)
1365 if (pipe2(reload_pipe
, O_NONBLOCK
| O_CLOEXEC
) < 0) {}
1367 reload_fd
.fd
= reload_pipe
[0];
1368 uloop_fd_add(&reload_fd
, ULOOP_READ
);
1370 signal(SIGTERM
, handle_signal
);
1371 signal(SIGINT
, handle_signal
);
1372 signal(SIGHUP
, handle_signal
);