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 OAF_DHCPV6 (OAF_DHCPV6_NA | OAF_DHCPV6_PD)
44 IFACE_ATTR_DYNAMICDHCP
,
56 IFACE_ATTR_DNS_SERVICE
,
58 IFACE_ATTR_FILTER_CLASS
,
59 IFACE_ATTR_DHCPV4_FORCERECONF
,
60 IFACE_ATTR_DHCPV6_RAW
,
61 IFACE_ATTR_DHCPV6_ASSIGNALL
,
64 IFACE_ATTR_RA_DEFAULT
,
65 IFACE_ATTR_RA_MANAGEMENT
,
68 IFACE_ATTR_RA_OFFLINK
,
69 IFACE_ATTR_RA_PREFERENCE
,
70 IFACE_ATTR_RA_ADVROUTER
,
71 IFACE_ATTR_RA_MININTERVAL
,
72 IFACE_ATTR_RA_MAXINTERVAL
,
73 IFACE_ATTR_RA_LIFETIME
,
74 IFACE_ATTR_RA_USELEASETIME
,
75 IFACE_ATTR_RA_REACHABLETIME
,
76 IFACE_ATTR_RA_RETRANSTIME
,
77 IFACE_ATTR_RA_HOPLIMIT
,
80 IFACE_ATTR_PD_MANAGER
,
82 IFACE_ATTR_NDPROXY_ROUTING
,
83 IFACE_ATTR_NDPROXY_SLAVE
,
84 IFACE_ATTR_PREFIX_FILTER
,
85 IFACE_ATTR_PREFERRED_LIFETIME
,
89 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
90 [IFACE_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
91 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
92 [IFACE_ATTR_NETWORKID
] = { .name
= "networkid", .type
= BLOBMSG_TYPE_STRING
},
93 [IFACE_ATTR_DYNAMICDHCP
] = { .name
= "dynamicdhcp", .type
= BLOBMSG_TYPE_BOOL
},
94 [IFACE_ATTR_LEASETIME
] = { .name
= "leasetime", .type
= BLOBMSG_TYPE_STRING
},
95 [IFACE_ATTR_START
] = { .name
= "start", .type
= BLOBMSG_TYPE_INT32
},
96 [IFACE_ATTR_LIMIT
] = { .name
= "limit", .type
= BLOBMSG_TYPE_INT32
},
97 [IFACE_ATTR_MASTER
] = { .name
= "master", .type
= BLOBMSG_TYPE_BOOL
},
98 [IFACE_ATTR_UPSTREAM
] = { .name
= "upstream", .type
= BLOBMSG_TYPE_ARRAY
},
99 [IFACE_ATTR_RA
] = { .name
= "ra", .type
= BLOBMSG_TYPE_STRING
},
100 [IFACE_ATTR_DHCPV4
] = { .name
= "dhcpv4", .type
= BLOBMSG_TYPE_STRING
},
101 [IFACE_ATTR_DHCPV6
] = { .name
= "dhcpv6", .type
= BLOBMSG_TYPE_STRING
},
102 [IFACE_ATTR_NDP
] = { .name
= "ndp", .type
= BLOBMSG_TYPE_STRING
},
103 [IFACE_ATTR_ROUTER
] = { .name
= "router", .type
= BLOBMSG_TYPE_ARRAY
},
104 [IFACE_ATTR_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
105 [IFACE_ATTR_DNS_SERVICE
] = { .name
= "dns_service", .type
= BLOBMSG_TYPE_BOOL
},
106 [IFACE_ATTR_DOMAIN
] = { .name
= "domain", .type
= BLOBMSG_TYPE_ARRAY
},
107 [IFACE_ATTR_FILTER_CLASS
] = { .name
= "filter_class", .type
= BLOBMSG_TYPE_STRING
},
108 [IFACE_ATTR_DHCPV4_FORCERECONF
] = { .name
= "dhcpv4_forcereconf", .type
= BLOBMSG_TYPE_BOOL
},
109 [IFACE_ATTR_DHCPV6_RAW
] = { .name
= "dhcpv6_raw", .type
= BLOBMSG_TYPE_STRING
},
110 [IFACE_ATTR_DHCPV6_ASSIGNALL
] = { .name
="dhcpv6_assignall", .type
= BLOBMSG_TYPE_BOOL
},
111 [IFACE_ATTR_DHCPV6_PD
] = { .name
= "dhcpv6_pd", .type
= BLOBMSG_TYPE_BOOL
},
112 [IFACE_ATTR_DHCPV6_NA
] = { .name
= "dhcpv6_na", .type
= BLOBMSG_TYPE_BOOL
},
113 [IFACE_ATTR_PD_MANAGER
] = { .name
= "pd_manager", .type
= BLOBMSG_TYPE_STRING
},
114 [IFACE_ATTR_PD_CER
] = { .name
= "pd_cer", .type
= BLOBMSG_TYPE_STRING
},
115 [IFACE_ATTR_RA_DEFAULT
] = { .name
= "ra_default", .type
= BLOBMSG_TYPE_INT32
},
116 [IFACE_ATTR_RA_MANAGEMENT
] = { .name
= "ra_management", .type
= BLOBMSG_TYPE_INT32
},
117 [IFACE_ATTR_RA_FLAGS
] = { .name
= "ra_flags", . type
= BLOBMSG_TYPE_ARRAY
},
118 [IFACE_ATTR_RA_SLAAC
] = { .name
= "ra_slaac", .type
= BLOBMSG_TYPE_BOOL
},
119 [IFACE_ATTR_RA_OFFLINK
] = { .name
= "ra_offlink", .type
= BLOBMSG_TYPE_BOOL
},
120 [IFACE_ATTR_RA_PREFERENCE
] = { .name
= "ra_preference", .type
= BLOBMSG_TYPE_STRING
},
121 [IFACE_ATTR_RA_ADVROUTER
] = { .name
= "ra_advrouter", .type
= BLOBMSG_TYPE_BOOL
},
122 [IFACE_ATTR_RA_MININTERVAL
] = { .name
= "ra_mininterval", .type
= BLOBMSG_TYPE_INT32
},
123 [IFACE_ATTR_RA_MAXINTERVAL
] = { .name
= "ra_maxinterval", .type
= BLOBMSG_TYPE_INT32
},
124 [IFACE_ATTR_RA_LIFETIME
] = { .name
= "ra_lifetime", .type
= BLOBMSG_TYPE_INT32
},
125 [IFACE_ATTR_RA_USELEASETIME
] = { .name
= "ra_useleasetime", .type
= BLOBMSG_TYPE_BOOL
},
126 [IFACE_ATTR_RA_REACHABLETIME
] = { .name
= "ra_reachabletime", .type
= BLOBMSG_TYPE_INT32
},
127 [IFACE_ATTR_RA_RETRANSTIME
] = { .name
= "ra_retranstime", .type
= BLOBMSG_TYPE_INT32
},
128 [IFACE_ATTR_RA_HOPLIMIT
] = { .name
= "ra_hoplimit", .type
= BLOBMSG_TYPE_INT32
},
129 [IFACE_ATTR_RA_MTU
] = { .name
= "ra_mtu", .type
= BLOBMSG_TYPE_INT32
},
130 [IFACE_ATTR_RA_DNS
] = { .name
= "ra_dns", .type
= BLOBMSG_TYPE_BOOL
},
131 [IFACE_ATTR_NDPROXY_ROUTING
] = { .name
= "ndproxy_routing", .type
= BLOBMSG_TYPE_BOOL
},
132 [IFACE_ATTR_NDPROXY_SLAVE
] = { .name
= "ndproxy_slave", .type
= BLOBMSG_TYPE_BOOL
},
133 [IFACE_ATTR_PREFIX_FILTER
] = { .name
= "prefix_filter", .type
= BLOBMSG_TYPE_STRING
},
134 [IFACE_ATTR_PREFERRED_LIFETIME
] = { .name
= "preferred_lifetime", .type
= BLOBMSG_TYPE_STRING
},
137 static const struct uci_blob_param_info iface_attr_info
[IFACE_ATTR_MAX
] = {
138 [IFACE_ATTR_UPSTREAM
] = { .type
= BLOBMSG_TYPE_STRING
},
139 [IFACE_ATTR_DNS
] = { .type
= BLOBMSG_TYPE_STRING
},
140 [IFACE_ATTR_DOMAIN
] = { .type
= BLOBMSG_TYPE_STRING
},
143 const struct uci_blob_param_list interface_attr_list
= {
144 .n_params
= IFACE_ATTR_MAX
,
145 .params
= iface_attrs
,
146 .info
= iface_attr_info
,
149 const struct blobmsg_policy lease_attrs
[LEASE_ATTR_MAX
] = {
150 [LEASE_ATTR_IP
] = { .name
= "ip", .type
= BLOBMSG_TYPE_STRING
},
151 [LEASE_ATTR_MAC
] = { .name
= "mac", .type
= BLOBMSG_TYPE_STRING
},
152 [LEASE_ATTR_DUID
] = { .name
= "duid", .type
= BLOBMSG_TYPE_STRING
},
153 [LEASE_ATTR_HOSTID
] = { .name
= "hostid", .type
= BLOBMSG_TYPE_STRING
},
154 [LEASE_ATTR_LEASETIME
] = { .name
= "leasetime", .type
= BLOBMSG_TYPE_STRING
},
155 [LEASE_ATTR_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
158 const struct uci_blob_param_list lease_attr_list
= {
159 .n_params
= LEASE_ATTR_MAX
,
160 .params
= lease_attrs
,
165 ODHCPD_ATTR_MAINDHCP
,
166 ODHCPD_ATTR_LEASEFILE
,
167 ODHCPD_ATTR_LEASETRIGGER
,
168 ODHCPD_ATTR_LOGLEVEL
,
172 static const struct blobmsg_policy odhcpd_attrs
[ODHCPD_ATTR_MAX
] = {
173 [ODHCPD_ATTR_LEGACY
] = { .name
= "legacy", .type
= BLOBMSG_TYPE_BOOL
},
174 [ODHCPD_ATTR_MAINDHCP
] = { .name
= "maindhcp", .type
= BLOBMSG_TYPE_BOOL
},
175 [ODHCPD_ATTR_LEASEFILE
] = { .name
= "leasefile", .type
= BLOBMSG_TYPE_STRING
},
176 [ODHCPD_ATTR_LEASETRIGGER
] = { .name
= "leasetrigger", .type
= BLOBMSG_TYPE_STRING
},
177 [ODHCPD_ATTR_LOGLEVEL
] = { .name
= "loglevel", .type
= BLOBMSG_TYPE_INT32
},
180 const struct uci_blob_param_list odhcpd_attr_list
= {
181 .n_params
= ODHCPD_ATTR_MAX
,
182 .params
= odhcpd_attrs
,
185 static const struct { const char *name
; uint8_t flag
; } ra_flags
[] = {
186 { .name
= "managed-config", .flag
= ND_RA_FLAG_MANAGED
},
187 { .name
= "other-config", .flag
= ND_RA_FLAG_OTHER
},
188 { .name
= "home-agent", .flag
= ND_RA_FLAG_HOME_AGENT
},
189 { .name
= "none", . flag
= 0 },
193 static void set_interface_defaults(struct interface
*iface
)
195 iface
->ignore
= true;
196 iface
->dhcpv4
= MODE_DISABLED
;
197 iface
->dhcpv6
= MODE_DISABLED
;
198 iface
->ra
= MODE_DISABLED
;
199 iface
->ndp
= MODE_DISABLED
;
200 iface
->learn_routes
= 1;
201 iface
->dhcp_leasetime
= 43200;
202 iface
->preferred_lifetime
= 43200;
203 iface
->dhcpv4_start
.s_addr
= htonl(START_DEFAULT
);
204 iface
->dhcpv4_end
.s_addr
= htonl(START_DEFAULT
+ LIMIT_DEFAULT
- 1);
205 iface
->dhcpv6_assignall
= true;
206 iface
->dhcpv6_pd
= true;
207 iface
->dhcpv6_na
= true;
208 iface
->dns_service
= true;
209 iface
->ra_flags
= ND_RA_FLAG_OTHER
;
210 iface
->ra_slaac
= true;
211 iface
->ra_maxinterval
= 600;
212 iface
->ra_mininterval
= iface
->ra_maxinterval
/3;
213 iface
->ra_lifetime
= -1;
214 iface
->ra_dns
= true;
217 static void clean_interface(struct interface
*iface
)
221 free(iface
->upstream
);
222 free(iface
->dhcpv4_router
);
223 free(iface
->dhcpv4_dns
);
224 free(iface
->dhcpv6_raw
);
225 free(iface
->filter_class
);
226 memset(&iface
->ra
, 0, sizeof(*iface
) - offsetof(struct interface
, ra
));
227 set_interface_defaults(iface
);
230 static void close_interface(struct interface
*iface
)
232 avl_delete(&interfaces
, &iface
->avl
);
234 router_setup_interface(iface
, false);
235 dhcpv6_setup_interface(iface
, false);
236 ndp_setup_interface(iface
, false);
237 #ifdef DHCPV4_SUPPORT
238 dhcpv4_setup_interface(iface
, false);
241 clean_interface(iface
);
248 static int parse_mode(const char *mode
)
250 if (!strcmp(mode
, "disabled"))
251 return MODE_DISABLED
;
252 else if (!strcmp(mode
, "server"))
254 else if (!strcmp(mode
, "relay"))
256 else if (!strcmp(mode
, "hybrid"))
262 static int parse_ra_flags(uint8_t *flags
, struct blob_attr
*attr
)
264 struct blob_attr
*cur
;
267 blobmsg_for_each_attr(cur
, attr
, rem
) {
270 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
273 if (!blobmsg_check_attr(cur
, false))
276 for (i
= 0; ra_flags
[i
].name
; i
++) {
277 if (!strcmp(ra_flags
[i
].name
, blobmsg_get_string(cur
))) {
278 *flags
|= ra_flags
[i
].flag
;
283 if (!ra_flags
[i
].name
)
290 static void set_config(struct uci_section
*s
)
292 struct blob_attr
*tb
[ODHCPD_ATTR_MAX
], *c
;
294 blob_buf_init(&b
, 0);
295 uci_to_blob(&b
, s
, &odhcpd_attr_list
);
296 blobmsg_parse(odhcpd_attrs
, ODHCPD_ATTR_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
298 if ((c
= tb
[ODHCPD_ATTR_LEGACY
]))
299 config
.legacy
= blobmsg_get_bool(c
);
301 if ((c
= tb
[ODHCPD_ATTR_MAINDHCP
]))
302 config
.main_dhcpv4
= blobmsg_get_bool(c
);
304 if ((c
= tb
[ODHCPD_ATTR_LEASEFILE
])) {
305 free(config
.dhcp_statefile
);
306 config
.dhcp_statefile
= strdup(blobmsg_get_string(c
));
309 if ((c
= tb
[ODHCPD_ATTR_LEASETRIGGER
])) {
310 free(config
.dhcp_cb
);
311 config
.dhcp_cb
= strdup(blobmsg_get_string(c
));
314 if ((c
= tb
[ODHCPD_ATTR_LOGLEVEL
])) {
315 int log_level
= (blobmsg_get_u32(c
) & LOG_PRIMASK
);
317 if (config
.log_level
!= log_level
) {
318 config
.log_level
= log_level
;
319 setlogmask(LOG_UPTO(config
.log_level
));
324 static double parse_leasetime(struct blob_attr
*c
) {
325 char *val
= blobmsg_get_string(c
), *endptr
= NULL
;
326 double time
= strcmp(val
, "infinite") ? strtod(val
, &endptr
) : UINT32_MAX
;
328 if (time
&& endptr
&& endptr
[0]) {
329 if (endptr
[0] == 's')
331 else if (endptr
[0] == 'm')
333 else if (endptr
[0] == 'h')
335 else if (endptr
[0] == 'd')
337 else if (endptr
[0] == 'w')
338 time
*= 7 * 24 * 3600;
352 static void free_lease(struct lease
*l
)
359 int set_lease_from_blobmsg(struct blob_attr
*ba
)
361 struct blob_attr
*tb
[LEASE_ATTR_MAX
], *c
;
366 blobmsg_parse(lease_attrs
, LEASE_ATTR_MAX
, tb
, blob_data(ba
), blob_len(ba
));
368 if ((c
= tb
[LEASE_ATTR_DUID
]))
369 duidlen
= (blobmsg_data_len(c
) - 1) / 2;
371 l
= calloc_a(sizeof(*l
), &duid
, duidlen
);
375 if ((c
= tb
[LEASE_ATTR_MAC
]))
376 if (!ether_aton_r(blobmsg_get_string(c
), &l
->mac
))
379 if ((c
= tb
[LEASE_ATTR_DUID
])) {
383 len
= odhcpd_unhexlify(l
->duid
, duidlen
, blobmsg_get_string(c
));
391 if ((c
= tb
[LEASE_ATTR_NAME
])) {
392 l
->hostname
= strdup(blobmsg_get_string(c
));
393 if (!l
->hostname
|| !odhcpd_valid_hostname(l
->hostname
))
397 if ((c
= tb
[LEASE_ATTR_IP
]))
398 if (inet_pton(AF_INET
, blobmsg_get_string(c
), &l
->ipaddr
) < 0)
401 if ((c
= tb
[LEASE_ATTR_HOSTID
])) {
403 l
->hostid
= strtoul(blobmsg_get_string(c
), NULL
, 16);
407 uint32_t i4a
= ntohl(l
->ipaddr
) & 0xff;
408 l
->hostid
= ((i4a
/ 100) << 8) | (((i4a
% 100) / 10) << 4) | (i4a
% 10);
411 if ((c
= tb
[LEASE_ATTR_LEASETIME
])) {
412 double time
= parse_leasetime(c
);
419 INIT_LIST_HEAD(&l
->assignments
);
420 vlist_add(&leases
, &l
->node
, l
);
430 static int set_lease_from_uci(struct uci_section
*s
)
432 blob_buf_init(&b
, 0);
433 uci_to_blob(&b
, s
, &lease_attr_list
);
435 return set_lease_from_blobmsg(b
.head
);
438 int config_parse_interface(void *data
, size_t len
, const char *name
, bool overwrite
)
440 struct interface
*iface
;
441 struct blob_attr
*tb
[IFACE_ATTR_MAX
], *c
;
442 bool get_addrs
= false;
444 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
, data
, len
);
446 if (tb
[IFACE_ATTR_INTERFACE
])
447 name
= blobmsg_get_string(tb
[IFACE_ATTR_INTERFACE
]);
452 iface
= avl_find_element(&interfaces
, name
, iface
, avl
);
456 iface
= calloc_a(sizeof(*iface
), &new_name
, strlen(name
) + 1);
460 iface
->name
= strcpy(new_name
, name
);
461 iface
->avl
.key
= iface
->name
;
462 iface
->router_event
.uloop
.fd
= -1;
463 iface
->dhcpv6_event
.uloop
.fd
= -1;
464 iface
->ndp_event
.uloop
.fd
= -1;
465 iface
->ndp_ping_fd
= -1;
466 iface
->dhcpv4_event
.uloop
.fd
= -1;
467 INIT_LIST_HEAD(&iface
->ia_assignments
);
468 INIT_LIST_HEAD(&iface
->dhcpv4_assignments
);
469 INIT_LIST_HEAD(&iface
->dhcpv4_fr_ips
);
471 set_interface_defaults(iface
);
473 avl_insert(&interfaces
, &iface
->avl
);
474 get_addrs
= overwrite
= true;
477 const char *ifname
= NULL
;
479 if ((c
= tb
[IFACE_ATTR_IFNAME
]))
480 ifname
= blobmsg_get_string(c
);
481 else if ((c
= tb
[IFACE_ATTR_NETWORKID
]))
482 ifname
= blobmsg_get_string(c
);
486 if (overwrite
|| !iface
->ifname
)
487 ifname
= ubus_get_ifname(name
);
490 if (!iface
->ifname
&& !ifname
)
495 iface
->ifname
= strdup(ifname
);
500 if (!iface
->ifindex
&&
501 (iface
->ifindex
= if_nametoindex(iface
->ifname
)) <= 0)
506 ssize_t len
= netlink_get_interface_addrs(iface
->ifindex
,
507 true, &iface
->addr6
);
510 iface
->addr6_len
= len
;
512 len
= netlink_get_interface_addrs(iface
->ifindex
,
513 false, &iface
->addr4
);
515 iface
->addr4_len
= len
;
520 if ((c
= tb
[IFACE_ATTR_DYNAMICDHCP
]))
521 iface
->no_dynamic_dhcp
= !blobmsg_get_bool(c
);
523 if ((c
= tb
[IFACE_ATTR_LEASETIME
])) {
524 double time
= parse_leasetime(c
);
528 iface
->dhcp_leasetime
= time
;
531 if ((c
= tb
[IFACE_ATTR_PREFERRED_LIFETIME
])) {
532 double time
= parse_leasetime(c
);
536 iface
->preferred_lifetime
= time
;
539 if ((c
= tb
[IFACE_ATTR_START
])) {
540 iface
->dhcpv4_start
.s_addr
= htonl(blobmsg_get_u32(c
));
541 iface
->dhcpv4_end
.s_addr
= htonl(ntohl(iface
->dhcpv4_start
.s_addr
) +
544 if (config
.main_dhcpv4
&& config
.legacy
)
545 iface
->dhcpv4
= MODE_SERVER
;
548 if ((c
= tb
[IFACE_ATTR_LIMIT
]))
549 iface
->dhcpv4_end
.s_addr
= htonl(ntohl(iface
->dhcpv4_start
.s_addr
) +
550 blobmsg_get_u32(c
) - 1);
552 if ((c
= tb
[IFACE_ATTR_MASTER
]))
553 iface
->master
= blobmsg_get_bool(c
);
555 if (overwrite
&& (c
= tb
[IFACE_ATTR_UPSTREAM
])) {
556 struct blob_attr
*cur
;
559 blobmsg_for_each_attr(cur
, c
, rem
) {
560 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
|| !blobmsg_check_attr(cur
, false))
563 iface
->upstream
= realloc(iface
->upstream
,
564 iface
->upstream_len
+ blobmsg_data_len(cur
));
565 if (!iface
->upstream
)
568 memcpy(iface
->upstream
+ iface
->upstream_len
, blobmsg_get_string(cur
), blobmsg_data_len(cur
));
569 iface
->upstream_len
+= blobmsg_data_len(cur
);
574 if ((c
= tb
[IFACE_ATTR_RA
])) {
575 if ((mode
= parse_mode(blobmsg_get_string(c
))) >= 0) {
578 if (iface
->ra
!= MODE_DISABLED
)
579 iface
->ignore
= false;
584 if ((c
= tb
[IFACE_ATTR_DHCPV4
])) {
585 if ((mode
= parse_mode(blobmsg_get_string(c
))) >= 0) {
586 if (config
.main_dhcpv4
) {
587 iface
->dhcpv4
= mode
;
589 if (iface
->dhcpv4
!= MODE_DISABLED
)
590 iface
->ignore
= false;
597 if ((c
= tb
[IFACE_ATTR_DHCPV6
])) {
598 if ((mode
= parse_mode(blobmsg_get_string(c
))) >= 0) {
599 iface
->dhcpv6
= mode
;
601 if (iface
->dhcpv6
!= MODE_DISABLED
)
602 iface
->ignore
= false;
607 if ((c
= tb
[IFACE_ATTR_NDP
])) {
608 if ((mode
= parse_mode(blobmsg_get_string(c
))) >= 0) {
611 if (iface
->ndp
!= MODE_DISABLED
)
612 iface
->ignore
= false;
617 if ((c
= tb
[IFACE_ATTR_ROUTER
])) {
618 struct blob_attr
*cur
;
621 blobmsg_for_each_attr(cur
, c
, rem
) {
622 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
|| !blobmsg_check_attr(cur
, false))
625 struct in_addr addr4
;
626 if (inet_pton(AF_INET
, blobmsg_get_string(cur
), &addr4
) == 1) {
627 iface
->dhcpv4_router
= realloc(iface
->dhcpv4_router
,
628 (++iface
->dhcpv4_router_cnt
) * sizeof(*iface
->dhcpv4_router
));
629 if (!iface
->dhcpv4_router
)
632 iface
->dhcpv4_router
[iface
->dhcpv4_router_cnt
- 1] = addr4
;
638 if ((c
= tb
[IFACE_ATTR_DNS
])) {
639 struct blob_attr
*cur
;
642 iface
->always_rewrite_dns
= true;
643 blobmsg_for_each_attr(cur
, c
, rem
) {
644 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
|| !blobmsg_check_attr(cur
, false))
647 struct in_addr addr4
;
648 struct in6_addr addr6
;
649 if (inet_pton(AF_INET
, blobmsg_get_string(cur
), &addr4
) == 1) {
650 if (addr4
.s_addr
== INADDR_ANY
)
653 iface
->dhcpv4_dns
= realloc(iface
->dhcpv4_dns
,
654 (++iface
->dhcpv4_dns_cnt
) * sizeof(*iface
->dhcpv4_dns
));
655 if (!iface
->dhcpv4_dns
)
658 iface
->dhcpv4_dns
[iface
->dhcpv4_dns_cnt
- 1] = addr4
;
659 } else if (inet_pton(AF_INET6
, blobmsg_get_string(cur
), &addr6
) == 1) {
660 if (IN6_IS_ADDR_UNSPECIFIED(&addr6
))
663 iface
->dns
= realloc(iface
->dns
,
664 (++iface
->dns_cnt
) * sizeof(*iface
->dns
));
668 iface
->dns
[iface
->dns_cnt
- 1] = addr6
;
674 if ((c
= tb
[IFACE_ATTR_DNS_SERVICE
]))
675 iface
->dns_service
= blobmsg_get_bool(c
);
677 if ((c
= tb
[IFACE_ATTR_DOMAIN
])) {
678 struct blob_attr
*cur
;
681 blobmsg_for_each_attr(cur
, c
, rem
) {
682 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
|| !blobmsg_check_attr(cur
, false))
686 char *domain
= blobmsg_get_string(cur
);
687 size_t domainlen
= strlen(domain
);
688 if (domainlen
> 0 && domain
[domainlen
- 1] == '.')
689 domain
[domainlen
- 1] = 0;
691 int len
= dn_comp(domain
, buf
, sizeof(buf
), NULL
, NULL
);
695 iface
->search
= realloc(iface
->search
, iface
->search_len
+ len
);
699 memcpy(&iface
->search
[iface
->search_len
], buf
, len
);
700 iface
->search_len
+= len
;
704 if ((c
= tb
[IFACE_ATTR_FILTER_CLASS
])) {
705 iface
->filter_class
= realloc(iface
->filter_class
, blobmsg_data_len(c
) + 1);
706 memcpy(iface
->filter_class
, blobmsg_get_string(c
), blobmsg_data_len(c
) + 1);
709 if ((c
= tb
[IFACE_ATTR_DHCPV4_FORCERECONF
]))
710 iface
->dhcpv4_forcereconf
= blobmsg_get_bool(c
);
712 if ((c
= tb
[IFACE_ATTR_DHCPV6_RAW
])) {
713 iface
->dhcpv6_raw_len
= blobmsg_data_len(c
) / 2;
714 iface
->dhcpv6_raw
= realloc(iface
->dhcpv6_raw
, iface
->dhcpv6_raw_len
);
715 odhcpd_unhexlify(iface
->dhcpv6_raw
, iface
->dhcpv6_raw_len
, blobmsg_get_string(c
));
718 if ((c
= tb
[IFACE_ATTR_DHCPV6_ASSIGNALL
]))
719 iface
->dhcpv6_assignall
= blobmsg_get_bool(c
);
721 if ((c
= tb
[IFACE_ATTR_DHCPV6_PD
]))
722 iface
->dhcpv6_pd
= blobmsg_get_bool(c
);
724 if ((c
= tb
[IFACE_ATTR_DHCPV6_NA
]))
725 iface
->dhcpv6_na
= blobmsg_get_bool(c
);
727 if ((c
= tb
[IFACE_ATTR_RA_DEFAULT
]))
728 iface
->default_router
= blobmsg_get_u32(c
);
730 if (!tb
[IFACE_ATTR_RA_FLAGS
] && !tb
[IFACE_ATTR_RA_SLAAC
] &&
731 (c
= tb
[IFACE_ATTR_RA_MANAGEMENT
])) {
732 switch (blobmsg_get_u32(c
)) {
734 iface
->ra_flags
= ND_RA_FLAG_OTHER
;
735 iface
->ra_slaac
= true;
738 iface
->ra_flags
= ND_RA_FLAG_OTHER
|ND_RA_FLAG_MANAGED
;
739 iface
->ra_slaac
= true;
742 iface
->ra_flags
= ND_RA_FLAG_OTHER
|ND_RA_FLAG_MANAGED
;
743 iface
->ra_slaac
= false;
750 if ((c
= tb
[IFACE_ATTR_RA_FLAGS
])) {
752 if (parse_ra_flags(&iface
->ra_flags
, c
) < 0)
756 if ((c
= tb
[IFACE_ATTR_RA_REACHABLETIME
])) {
757 uint32_t ra_reachabletime
= blobmsg_get_u32(c
);
758 if (ra_reachabletime
> 3600000)
761 iface
->ra_reachabletime
= ra_reachabletime
;
764 if ((c
= tb
[IFACE_ATTR_RA_RETRANSTIME
])) {
765 uint32_t ra_retranstime
= blobmsg_get_u32(c
);
766 if (ra_retranstime
> 60000)
769 iface
->ra_retranstime
= ra_retranstime
;
772 if ((c
= tb
[IFACE_ATTR_RA_HOPLIMIT
])) {
773 uint32_t ra_hoplimit
= blobmsg_get_u32(c
);
774 if (ra_hoplimit
> 255)
777 iface
->ra_hoplimit
= ra_hoplimit
;
780 if ((c
= tb
[IFACE_ATTR_RA_MTU
])) {
781 uint32_t ra_mtu
= blobmsg_get_u32(c
);
782 if (ra_mtu
< 1280 || ra_mtu
> 65535)
785 iface
->ra_mtu
= ra_mtu
;
788 if ((c
= tb
[IFACE_ATTR_RA_SLAAC
]))
789 iface
->ra_slaac
= blobmsg_get_bool(c
);
791 if ((c
= tb
[IFACE_ATTR_RA_OFFLINK
]))
792 iface
->ra_not_onlink
= blobmsg_get_bool(c
);
794 if ((c
= tb
[IFACE_ATTR_RA_ADVROUTER
]))
795 iface
->ra_advrouter
= blobmsg_get_bool(c
);
797 if ((c
= tb
[IFACE_ATTR_RA_MININTERVAL
]))
798 iface
->ra_mininterval
= blobmsg_get_u32(c
);
800 if ((c
= tb
[IFACE_ATTR_RA_MAXINTERVAL
]))
801 iface
->ra_maxinterval
= blobmsg_get_u32(c
);
803 if ((c
= tb
[IFACE_ATTR_RA_LIFETIME
]))
804 iface
->ra_lifetime
= blobmsg_get_u32(c
);
806 if ((c
= tb
[IFACE_ATTR_RA_USELEASETIME
]))
807 iface
->ra_useleasetime
= blobmsg_get_bool(c
);
809 if ((c
= tb
[IFACE_ATTR_RA_DNS
]))
810 iface
->ra_dns
= blobmsg_get_bool(c
);
812 if ((c
= tb
[IFACE_ATTR_RA_PREFERENCE
])) {
813 const char *prio
= blobmsg_get_string(c
);
815 if (!strcmp(prio
, "high"))
816 iface
->route_preference
= 1;
817 else if (!strcmp(prio
, "low"))
818 iface
->route_preference
= -1;
819 else if (!strcmp(prio
, "medium") || !strcmp(prio
, "default"))
820 iface
->route_preference
= 0;
825 if ((c
= tb
[IFACE_ATTR_PD_MANAGER
]))
826 strncpy(iface
->dhcpv6_pd_manager
, blobmsg_get_string(c
),
827 sizeof(iface
->dhcpv6_pd_manager
) - 1);
829 if ((c
= tb
[IFACE_ATTR_PD_CER
]) &&
830 inet_pton(AF_INET6
, blobmsg_get_string(c
), &iface
->dhcpv6_pd_cer
) < 1)
833 if ((c
= tb
[IFACE_ATTR_NDPROXY_ROUTING
]))
834 iface
->learn_routes
= blobmsg_get_bool(c
);
836 if ((c
= tb
[IFACE_ATTR_NDPROXY_SLAVE
]))
837 iface
->external
= blobmsg_get_bool(c
);
839 if ((c
= tb
[IFACE_ATTR_PREFIX_FILTER
])) {
840 const char *str
= blobmsg_get_string(c
);
841 char *astr
= malloc(strlen(str
) + 1);
845 if (!astr
|| !strcpy(astr
, str
) ||
846 (delim
= strchr(astr
, '/')) == NULL
|| (*(delim
++) = 0) ||
847 sscanf(delim
, "%i", &l
) == 0 || l
> 128 ||
848 inet_pton(AF_INET6
, astr
, &iface
->pio_filter_addr
) == 0)
849 iface
->pio_filter_length
= 0;
851 iface
->pio_filter_length
= l
;
860 close_interface(iface
);
864 static int set_interface(struct uci_section
*s
)
866 blob_buf_init(&b
, 0);
867 uci_to_blob(&b
, s
, &interface_attr_list
);
869 return config_parse_interface(blob_data(b
.head
), blob_len(b
.head
), s
->e
.name
, true);
872 static void lease_delete_assignments(struct lease
*l
, bool v6
)
874 struct dhcp_assignment
*a
, *tmp
;
875 unsigned int flag
= v6
? OAF_DHCPV6
: OAF_DHCPV4
;
877 list_for_each_entry_safe(a
, tmp
, &l
->assignments
, lease_list
) {
883 static void lease_update_assignments(struct lease
*l
)
885 struct dhcp_assignment
*a
;
887 list_for_each_entry(a
, &l
->assignments
, lease_list
) {
893 a
->hostname
= strdup(l
->hostname
);
895 a
->leasetime
= l
->leasetime
;
899 static int lease_cmp(const void *k1
, const void *k2
, _unused
void *ptr
)
901 const struct lease
*l1
= k1
, *l2
= k2
;
904 if (l1
->duid_len
!= l2
->duid_len
)
905 return l1
->duid_len
- l2
->duid_len
;
907 if (l1
->duid_len
&& l2
->duid_len
)
908 cmp
= memcmp(l1
->duid
, l2
->duid
, l1
->duid_len
);
913 return memcmp(l1
->mac
.ether_addr_octet
, l2
->mac
.ether_addr_octet
,
914 sizeof(l1
->mac
.ether_addr_octet
));
917 static void lease_change_config(struct lease
*l_old
, struct lease
*l_new
)
921 if ((!!l_new
->hostname
!= !!l_old
->hostname
) ||
922 (l_new
->hostname
&& strcmp(l_new
->hostname
, l_old
->hostname
))) {
923 free(l_old
->hostname
);
924 l_old
->hostname
= NULL
;
927 l_old
->hostname
= strdup(l_new
->hostname
);
932 if (l_old
->leasetime
!= l_new
->leasetime
) {
933 l_old
->leasetime
= l_new
->leasetime
;
937 if (l_old
->ipaddr
!= l_new
->ipaddr
) {
938 l_old
->ipaddr
= l_new
->ipaddr
;
939 lease_delete_assignments(l_old
, false);
942 if (l_old
->hostid
!= l_new
->hostid
) {
943 l_old
->hostid
= l_new
->hostid
;
944 lease_delete_assignments(l_old
, true);
948 lease_update_assignments(l_old
);
953 static void lease_delete(struct lease
*l
)
955 struct dhcp_assignment
*a
, *tmp
;
957 list_for_each_entry_safe(a
, tmp
, &l
->assignments
, lease_list
)
963 static void lease_update(_unused
struct vlist_tree
*tree
, struct vlist_node
*node_new
,
964 struct vlist_node
*node_old
)
966 struct lease
*lease_new
= container_of(node_new
, struct lease
, node
);
967 struct lease
*lease_old
= container_of(node_old
, struct lease
, node
);
969 if (node_old
&& node_new
)
970 lease_change_config(lease_old
, lease_new
);
972 lease_delete(lease_old
);
975 struct lease
*config_find_lease_by_duid(const uint8_t *duid
, const uint16_t len
)
979 vlist_for_each_element(&leases
, l
, node
) {
980 if (l
->duid_len
== len
&& !memcmp(l
->duid
, duid
, len
))
987 struct lease
*config_find_lease_by_mac(const uint8_t *mac
)
991 vlist_for_each_element(&leases
, l
, node
) {
992 if (!memcmp(l
->mac
.ether_addr_octet
, mac
,
993 sizeof(l
->mac
.ether_addr_octet
)))
1000 struct lease
*config_find_lease_by_hostid(const uint32_t hostid
)
1004 vlist_for_each_element(&leases
, l
, node
) {
1005 if (l
->hostid
== hostid
)
1012 struct lease
*config_find_lease_by_ipaddr(const uint32_t ipaddr
)
1016 vlist_for_each_element(&leases
, l
, node
) {
1017 if (l
->ipaddr
== ipaddr
)
1024 void odhcpd_reload(void)
1026 struct uci_context
*uci
= uci_alloc_context();
1027 struct interface
*master
= NULL
, *i
, *tmp
;
1032 vlist_update(&leases
);
1033 avl_for_each_element(&interfaces
, i
, avl
)
1036 struct uci_package
*dhcp
= NULL
;
1037 if (!uci_load(uci
, "dhcp", &dhcp
)) {
1038 struct uci_element
*e
;
1040 /* 1. Global settings */
1041 uci_foreach_element(&dhcp
->sections
, e
) {
1042 struct uci_section
*s
= uci_to_section(e
);
1043 if (!strcmp(s
->type
, "odhcpd"))
1048 uci_foreach_element(&dhcp
->sections
, e
) {
1049 struct uci_section
*s
= uci_to_section(e
);
1050 if (!strcmp(s
->type
, "dhcp"))
1054 /* 3. Static leases */
1055 uci_foreach_element(&dhcp
->sections
, e
) {
1056 struct uci_section
* s
= uci_to_section(e
);
1057 if (!strcmp(s
->type
, "host"))
1058 set_lease_from_uci(s
);
1062 if (config
.dhcp_statefile
) {
1063 char *path
= strdup(config
.dhcp_statefile
);
1065 mkdir_p(dirname(path
), 0755);
1069 vlist_flush(&leases
);
1072 ubus_apply_network();
1075 bool any_dhcpv6_slave
= false, any_ra_slave
= false, any_ndp_slave
= false;
1078 avl_for_each_element(&interfaces
, i
, avl
) {
1082 if (i
->dhcpv6
== MODE_HYBRID
|| i
->dhcpv6
== MODE_RELAY
)
1083 any_dhcpv6_slave
= true;
1085 if (i
->ra
== MODE_HYBRID
|| i
->ra
== MODE_RELAY
)
1086 any_ra_slave
= true;
1088 if (i
->ndp
== MODE_HYBRID
|| i
->ndp
== MODE_RELAY
)
1089 any_ndp_slave
= true;
1092 /* Evaluate hybrid mode for master */
1093 avl_for_each_element(&interfaces
, i
, avl
) {
1097 enum odhcpd_mode hybrid_mode
= MODE_DISABLED
;
1099 if (!ubus_has_prefix(i
->name
, i
->ifname
))
1100 hybrid_mode
= MODE_RELAY
;
1103 if (i
->dhcpv6
== MODE_HYBRID
)
1104 i
->dhcpv6
= hybrid_mode
;
1106 if (i
->dhcpv6
== MODE_RELAY
&& !any_dhcpv6_slave
)
1107 i
->dhcpv6
= MODE_DISABLED
;
1109 if (i
->ra
== MODE_HYBRID
)
1110 i
->ra
= hybrid_mode
;
1112 if (i
->ra
== MODE_RELAY
&& !any_ra_slave
)
1113 i
->ra
= MODE_DISABLED
;
1115 if (i
->ndp
== MODE_HYBRID
)
1116 i
->ndp
= hybrid_mode
;
1118 if (i
->ndp
== MODE_RELAY
&& !any_ndp_slave
)
1119 i
->ndp
= MODE_DISABLED
;
1121 if (i
->dhcpv6
== MODE_RELAY
|| i
->ra
== MODE_RELAY
|| i
->ndp
== MODE_RELAY
)
1126 avl_for_each_element_safe(&interfaces
, i
, avl
, tmp
) {
1128 /* Resolve hybrid mode */
1129 if (i
->dhcpv6
== MODE_HYBRID
)
1130 i
->dhcpv6
= (master
&& master
->dhcpv6
== MODE_RELAY
) ?
1131 MODE_RELAY
: MODE_SERVER
;
1133 if (i
->ra
== MODE_HYBRID
)
1134 i
->ra
= (master
&& master
->ra
== MODE_RELAY
) ?
1135 MODE_RELAY
: MODE_SERVER
;
1137 if (i
->ndp
== MODE_HYBRID
)
1138 i
->ndp
= (master
&& master
->ndp
== MODE_RELAY
) ?
1139 MODE_RELAY
: MODE_DISABLED
;
1141 router_setup_interface(i
, i
->ra
!= MODE_DISABLED
);
1142 dhcpv6_setup_interface(i
, i
->dhcpv6
!= MODE_DISABLED
);
1143 ndp_setup_interface(i
, i
->ndp
!= MODE_DISABLED
);
1144 #ifdef DHCPV4_SUPPORT
1145 dhcpv4_setup_interface(i
, i
->dhcpv4
!= MODE_DISABLED
);
1151 uci_unload(uci
, dhcp
);
1152 uci_free_context(uci
);
1155 static void handle_signal(int signal
)
1159 if (signal
== SIGHUP
) {
1160 if (write(reload_pipe
[1], b
, sizeof(b
)) < 0) {}
1165 static void reload_cb(struct uloop_fd
*u
, _unused
unsigned int events
)
1168 if (read(u
->fd
, b
, sizeof(b
)) < 0) {}
1173 static struct uloop_fd reload_fd
= { .fd
= -1, .cb
= reload_cb
};
1175 void odhcpd_run(void)
1177 if (pipe2(reload_pipe
, O_NONBLOCK
| O_CLOEXEC
) < 0) {}
1179 reload_fd
.fd
= reload_pipe
[0];
1180 uloop_fd_add(&reload_fd
, ULOOP_READ
);
1182 signal(SIGTERM
, handle_signal
);
1183 signal(SIGINT
, handle_signal
);
1184 signal(SIGHUP
, handle_signal
);