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
42 IFACE_ATTR_DYNAMICDHCP
,
56 IFACE_ATTR_FILTER_CLASS
,
57 IFACE_ATTR_DHCPV4_FORCERECONF
,
58 IFACE_ATTR_DHCPV6_RAW
,
59 IFACE_ATTR_DHCPV6_ASSIGNALL
,
62 IFACE_ATTR_RA_DEFAULT
,
63 IFACE_ATTR_RA_MANAGEMENT
,
64 IFACE_ATTR_RA_OFFLINK
,
65 IFACE_ATTR_RA_PREFERENCE
,
66 IFACE_ATTR_RA_ADVROUTER
,
67 IFACE_ATTR_RA_MININTERVAL
,
68 IFACE_ATTR_RA_MAXINTERVAL
,
69 IFACE_ATTR_RA_LIFETIME
,
70 IFACE_ATTR_RA_USELEASETIME
,
71 IFACE_ATTR_RA_REACHABLETIME
,
72 IFACE_ATTR_RA_RETRANSTIME
,
73 IFACE_ATTR_RA_HOPLIMIT
,
76 IFACE_ATTR_PD_MANAGER
,
78 IFACE_ATTR_NDPROXY_ROUTING
,
79 IFACE_ATTR_NDPROXY_SLAVE
,
80 IFACE_ATTR_PREFIX_FILTER
,
84 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
85 [IFACE_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
86 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
87 [IFACE_ATTR_NETWORKID
] = { .name
= "networkid", .type
= BLOBMSG_TYPE_STRING
},
88 [IFACE_ATTR_DYNAMICDHCP
] = { .name
= "dynamicdhcp", .type
= BLOBMSG_TYPE_BOOL
},
89 [IFACE_ATTR_IGNORE
] = { .name
= "ignore", .type
= BLOBMSG_TYPE_BOOL
},
90 [IFACE_ATTR_LEASETIME
] = { .name
= "leasetime", .type
= BLOBMSG_TYPE_STRING
},
91 [IFACE_ATTR_START
] = { .name
= "start", .type
= BLOBMSG_TYPE_INT32
},
92 [IFACE_ATTR_LIMIT
] = { .name
= "limit", .type
= BLOBMSG_TYPE_INT32
},
93 [IFACE_ATTR_MASTER
] = { .name
= "master", .type
= BLOBMSG_TYPE_BOOL
},
94 [IFACE_ATTR_UPSTREAM
] = { .name
= "upstream", .type
= BLOBMSG_TYPE_ARRAY
},
95 [IFACE_ATTR_RA
] = { .name
= "ra", .type
= BLOBMSG_TYPE_STRING
},
96 [IFACE_ATTR_DHCPV4
] = { .name
= "dhcpv4", .type
= BLOBMSG_TYPE_STRING
},
97 [IFACE_ATTR_DHCPV6
] = { .name
= "dhcpv6", .type
= BLOBMSG_TYPE_STRING
},
98 [IFACE_ATTR_NDP
] = { .name
= "ndp", .type
= BLOBMSG_TYPE_STRING
},
99 [IFACE_ATTR_ROUTER
] = { .name
= "router", .type
= BLOBMSG_TYPE_ARRAY
},
100 [IFACE_ATTR_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
101 [IFACE_ATTR_DOMAIN
] = { .name
= "domain", .type
= BLOBMSG_TYPE_ARRAY
},
102 [IFACE_ATTR_FILTER_CLASS
] = { .name
= "filter_class", .type
= BLOBMSG_TYPE_STRING
},
103 [IFACE_ATTR_DHCPV4_FORCERECONF
] = { .name
= "dhcpv4_forcereconf", .type
= BLOBMSG_TYPE_BOOL
},
104 [IFACE_ATTR_DHCPV6_RAW
] = { .name
= "dhcpv6_raw", .type
= BLOBMSG_TYPE_STRING
},
105 [IFACE_ATTR_DHCPV6_ASSIGNALL
] = { .name
="dhcpv6_assignall", .type
= BLOBMSG_TYPE_BOOL
},
106 [IFACE_ATTR_DHCPV6_PD
] = { .name
= "dhcpv6_pd", .type
= BLOBMSG_TYPE_BOOL
},
107 [IFACE_ATTR_DHCPV6_NA
] = { .name
= "dhcpv6_na", .type
= BLOBMSG_TYPE_BOOL
},
108 [IFACE_ATTR_PD_MANAGER
] = { .name
= "pd_manager", .type
= BLOBMSG_TYPE_STRING
},
109 [IFACE_ATTR_PD_CER
] = { .name
= "pd_cer", .type
= BLOBMSG_TYPE_STRING
},
110 [IFACE_ATTR_RA_DEFAULT
] = { .name
= "ra_default", .type
= BLOBMSG_TYPE_INT32
},
111 [IFACE_ATTR_RA_MANAGEMENT
] = { .name
= "ra_management", .type
= BLOBMSG_TYPE_INT32
},
112 [IFACE_ATTR_RA_OFFLINK
] = { .name
= "ra_offlink", .type
= BLOBMSG_TYPE_BOOL
},
113 [IFACE_ATTR_RA_PREFERENCE
] = { .name
= "ra_preference", .type
= BLOBMSG_TYPE_STRING
},
114 [IFACE_ATTR_RA_ADVROUTER
] = { .name
= "ra_advrouter", .type
= BLOBMSG_TYPE_BOOL
},
115 [IFACE_ATTR_RA_MININTERVAL
] = { .name
= "ra_mininterval", .type
= BLOBMSG_TYPE_INT32
},
116 [IFACE_ATTR_RA_MAXINTERVAL
] = { .name
= "ra_maxinterval", .type
= BLOBMSG_TYPE_INT32
},
117 [IFACE_ATTR_RA_LIFETIME
] = { .name
= "ra_lifetime", .type
= BLOBMSG_TYPE_INT32
},
118 [IFACE_ATTR_RA_USELEASETIME
] = { .name
= "ra_useleasetime", .type
= BLOBMSG_TYPE_BOOL
},
119 [IFACE_ATTR_RA_REACHABLETIME
] = { .name
= "ra_reachabletime", .type
= BLOBMSG_TYPE_INT32
},
120 [IFACE_ATTR_RA_RETRANSTIME
] = { .name
= "ra_retranstime", .type
= BLOBMSG_TYPE_INT32
},
121 [IFACE_ATTR_RA_HOPLIMIT
] = { .name
= "ra_hoplimit", .type
= BLOBMSG_TYPE_INT32
},
122 [IFACE_ATTR_RA_MTU
] = { .name
= "ra_mtu", .type
= BLOBMSG_TYPE_INT32
},
123 [IFACE_ATTR_RA_DNS
] = { .name
= "ra_dns", .type
= BLOBMSG_TYPE_BOOL
},
124 [IFACE_ATTR_NDPROXY_ROUTING
] = { .name
= "ndproxy_routing", .type
= BLOBMSG_TYPE_BOOL
},
125 [IFACE_ATTR_NDPROXY_SLAVE
] = { .name
= "ndproxy_slave", .type
= BLOBMSG_TYPE_BOOL
},
126 [IFACE_ATTR_PREFIX_FILTER
] = { .name
= "prefix_filter", .type
= BLOBMSG_TYPE_STRING
},
129 static const struct uci_blob_param_info iface_attr_info
[IFACE_ATTR_MAX
] = {
130 [IFACE_ATTR_UPSTREAM
] = { .type
= BLOBMSG_TYPE_STRING
},
131 [IFACE_ATTR_DNS
] = { .type
= BLOBMSG_TYPE_STRING
},
132 [IFACE_ATTR_DOMAIN
] = { .type
= BLOBMSG_TYPE_STRING
},
135 const struct uci_blob_param_list interface_attr_list
= {
136 .n_params
= IFACE_ATTR_MAX
,
137 .params
= iface_attrs
,
138 .info
= iface_attr_info
,
146 LEASE_ATTR_LEASETIME
,
151 static const struct blobmsg_policy lease_attrs
[LEASE_ATTR_MAX
] = {
152 [LEASE_ATTR_IP
] = { .name
= "ip", .type
= BLOBMSG_TYPE_STRING
},
153 [LEASE_ATTR_MAC
] = { .name
= "mac", .type
= BLOBMSG_TYPE_STRING
},
154 [LEASE_ATTR_DUID
] = { .name
= "duid", .type
= BLOBMSG_TYPE_STRING
},
155 [LEASE_ATTR_HOSTID
] = { .name
= "hostid", .type
= BLOBMSG_TYPE_STRING
},
156 [LEASE_ATTR_LEASETIME
] = { .name
= "leasetime", .type
= BLOBMSG_TYPE_STRING
},
157 [LEASE_ATTR_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
160 const struct uci_blob_param_list lease_attr_list
= {
161 .n_params
= LEASE_ATTR_MAX
,
162 .params
= lease_attrs
,
167 ODHCPD_ATTR_MAINDHCP
,
168 ODHCPD_ATTR_LEASEFILE
,
169 ODHCPD_ATTR_LEASETRIGGER
,
170 ODHCPD_ATTR_LOGLEVEL
,
174 static const struct blobmsg_policy odhcpd_attrs
[ODHCPD_ATTR_MAX
] = {
175 [ODHCPD_ATTR_LEGACY
] = { .name
= "legacy", .type
= BLOBMSG_TYPE_BOOL
},
176 [ODHCPD_ATTR_MAINDHCP
] = { .name
= "maindhcp", .type
= BLOBMSG_TYPE_BOOL
},
177 [ODHCPD_ATTR_LEASEFILE
] = { .name
= "leasefile", .type
= BLOBMSG_TYPE_STRING
},
178 [ODHCPD_ATTR_LEASETRIGGER
] = { .name
= "leasetrigger", .type
= BLOBMSG_TYPE_STRING
},
179 [ODHCPD_ATTR_LOGLEVEL
] = { .name
= "loglevel", .type
= BLOBMSG_TYPE_INT32
},
182 const struct uci_blob_param_list odhcpd_attr_list
= {
183 .n_params
= ODHCPD_ATTR_MAX
,
184 .params
= odhcpd_attrs
,
187 static int mkdir_p(char *dir
, mode_t mask
)
189 char *l
= strrchr(dir
, '/');
197 if (mkdir_p(dir
, mask
))
202 ret
= mkdir(dir
, mask
);
203 if (ret
&& errno
== EEXIST
)
207 syslog(LOG_ERR
, "mkdir(%s, %d) failed: %m\n", dir
, mask
);
212 static void set_interface_defaults(struct interface
*iface
)
214 iface
->learn_routes
= 1;
215 iface
->dhcpv4_leasetime
= 43200;
216 iface
->dhcpv4_start
.s_addr
= htonl(START_DEFAULT
);
217 iface
->dhcpv4_end
.s_addr
= htonl(START_DEFAULT
+ LIMIT_DEFAULT
- 1);
218 iface
->dhcpv6_assignall
= true;
219 iface
->dhcpv6_pd
= true;
220 iface
->dhcpv6_na
= true;
221 iface
->ra_managed
= RA_MANAGED_MFLAG
;
222 iface
->ra_maxinterval
= 600;
223 iface
->ra_mininterval
= iface
->ra_maxinterval
/3;
224 iface
->ra_lifetime
= -1;
225 iface
->ra_dns
= true;
228 static void clean_interface(struct interface
*iface
)
232 free(iface
->upstream
);
233 free(iface
->dhcpv4_router
);
234 free(iface
->dhcpv4_dns
);
235 free(iface
->dhcpv6_raw
);
236 free(iface
->filter_class
);
237 memset(&iface
->ra
, 0, sizeof(*iface
) - offsetof(struct interface
, ra
));
238 set_interface_defaults(iface
);
241 static void close_interface(struct interface
*iface
)
243 avl_delete(&interfaces
, &iface
->avl
);
245 router_setup_interface(iface
, false);
246 dhcpv6_setup_interface(iface
, false);
247 ndp_setup_interface(iface
, false);
248 #ifdef DHCPV4_SUPPORT
249 dhcpv4_setup_interface(iface
, false);
252 clean_interface(iface
);
259 static int parse_mode(const char *mode
)
261 if (!strcmp(mode
, "disabled"))
262 return MODE_DISABLED
;
263 else if (!strcmp(mode
, "server"))
265 else if (!strcmp(mode
, "relay"))
267 else if (!strcmp(mode
, "hybrid"))
273 static void set_config(struct uci_section
*s
)
275 struct blob_attr
*tb
[ODHCPD_ATTR_MAX
], *c
;
277 blob_buf_init(&b
, 0);
278 uci_to_blob(&b
, s
, &odhcpd_attr_list
);
279 blobmsg_parse(odhcpd_attrs
, ODHCPD_ATTR_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
281 if ((c
= tb
[ODHCPD_ATTR_LEGACY
]))
282 config
.legacy
= blobmsg_get_bool(c
);
284 if ((c
= tb
[ODHCPD_ATTR_MAINDHCP
]))
285 config
.main_dhcpv4
= blobmsg_get_bool(c
);
287 if ((c
= tb
[ODHCPD_ATTR_LEASEFILE
])) {
288 free(config
.dhcp_statefile
);
289 config
.dhcp_statefile
= strdup(blobmsg_get_string(c
));
292 if ((c
= tb
[ODHCPD_ATTR_LEASETRIGGER
])) {
293 free(config
.dhcp_cb
);
294 config
.dhcp_cb
= strdup(blobmsg_get_string(c
));
297 if ((c
= tb
[ODHCPD_ATTR_LOGLEVEL
])) {
298 int log_level
= (blobmsg_get_u32(c
) & LOG_PRIMASK
);
300 if (config
.log_level
!= log_level
) {
301 config
.log_level
= log_level
;
302 setlogmask(LOG_UPTO(config
.log_level
));
307 static double parse_leasetime(struct blob_attr
*c
) {
308 char *val
= blobmsg_get_string(c
), *endptr
= NULL
;
309 double time
= strcmp(val
, "infinite") ? strtod(val
, &endptr
) : UINT32_MAX
;
311 if (time
&& endptr
&& endptr
[0]) {
312 if (endptr
[0] == 's')
314 else if (endptr
[0] == 'm')
316 else if (endptr
[0] == 'h')
318 else if (endptr
[0] == 'd')
320 else if (endptr
[0] == 'w')
321 time
*= 7 * 24 * 3600;
335 static void free_lease(struct lease
*l
)
341 static int set_lease(struct uci_section
*s
)
343 struct blob_attr
*tb
[LEASE_ATTR_MAX
], *c
;
348 blob_buf_init(&b
, 0);
349 uci_to_blob(&b
, s
, &lease_attr_list
);
350 blobmsg_parse(lease_attrs
, LEASE_ATTR_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
352 if ((c
= tb
[LEASE_ATTR_DUID
]))
353 duidlen
= (blobmsg_data_len(c
) - 1) / 2;
355 l
= calloc_a(sizeof(*l
), &duid
, duidlen
);
359 if ((c
= tb
[LEASE_ATTR_MAC
]))
360 if (!ether_aton_r(blobmsg_get_string(c
), &l
->mac
))
363 if ((c
= tb
[LEASE_ATTR_DUID
])) {
367 len
= odhcpd_unhexlify(l
->duid
, duidlen
, blobmsg_get_string(c
));
375 if ((c
= tb
[LEASE_ATTR_NAME
])) {
376 l
->hostname
= strdup(blobmsg_get_string(c
));
377 if (!l
->hostname
|| !odhcpd_valid_hostname(l
->hostname
))
381 if ((c
= tb
[LEASE_ATTR_IP
]))
382 if (inet_pton(AF_INET
, blobmsg_get_string(c
), &l
->ipaddr
) < 0)
385 if ((c
= tb
[LEASE_ATTR_HOSTID
])) {
387 l
->hostid
= strtoul(blobmsg_get_string(c
), NULL
, 16);
391 uint32_t i4a
= ntohl(l
->ipaddr
) & 0xff;
392 l
->hostid
= ((i4a
/ 100) << 8) | (((i4a
% 100) / 10) << 4) | (i4a
% 10);
395 if ((c
= tb
[LEASE_ATTR_LEASETIME
])) {
396 double time
= parse_leasetime(c
);
403 INIT_LIST_HEAD(&l
->assignments
);
404 vlist_add(&leases
, &l
->node
, l
);
414 int config_parse_interface(void *data
, size_t len
, const char *name
, bool overwrite
)
416 struct interface
*iface
;
417 struct blob_attr
*tb
[IFACE_ATTR_MAX
], *c
;
418 bool get_addrs
= false;
420 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
, data
, len
);
422 if (tb
[IFACE_ATTR_INTERFACE
])
423 name
= blobmsg_get_string(tb
[IFACE_ATTR_INTERFACE
]);
428 iface
= avl_find_element(&interfaces
, name
, iface
, avl
);
432 iface
= calloc_a(sizeof(*iface
), &new_name
, strlen(name
) + 1);
436 iface
->name
= strcpy(new_name
, name
);
437 iface
->avl
.key
= iface
->name
;
438 iface
->router_event
.uloop
.fd
= -1;
439 iface
->dhcpv6_event
.uloop
.fd
= -1;
440 iface
->ndp_event
.uloop
.fd
= -1;
441 iface
->ndp_ping_fd
= -1;
442 iface
->dhcpv4_event
.uloop
.fd
= -1;
443 set_interface_defaults(iface
);
445 avl_insert(&interfaces
, &iface
->avl
);
446 get_addrs
= overwrite
= true;
449 const char *ifname
= NULL
;
451 if ((c
= tb
[IFACE_ATTR_IFNAME
]))
452 ifname
= blobmsg_get_string(c
);
453 else if ((c
= tb
[IFACE_ATTR_NETWORKID
]))
454 ifname
= blobmsg_get_string(c
);
458 if (overwrite
|| !iface
->ifname
)
459 ifname
= ubus_get_ifname(name
);
462 if (!iface
->ifname
&& !ifname
)
467 iface
->ifname
= strdup(ifname
);
472 if (!iface
->ifindex
&&
473 (iface
->ifindex
= if_nametoindex(iface
->ifname
)) <= 0)
478 ssize_t len
= netlink_get_interface_addrs(iface
->ifindex
,
479 true, &iface
->addr6
);
482 iface
->addr6_len
= len
;
484 len
= netlink_get_interface_addrs(iface
->ifindex
,
485 false, &iface
->addr4
);
487 iface
->addr4_len
= len
;
492 if ((c
= tb
[IFACE_ATTR_DYNAMICDHCP
]))
493 iface
->no_dynamic_dhcp
= !blobmsg_get_bool(c
);
495 if (overwrite
&& (c
= tb
[IFACE_ATTR_IGNORE
]))
496 iface
->ignore
= blobmsg_get_bool(c
);
498 if ((c
= tb
[IFACE_ATTR_LEASETIME
])) {
499 double time
= parse_leasetime(c
);
503 iface
->dhcpv4_leasetime
= time
;
506 if ((c
= tb
[IFACE_ATTR_START
])) {
507 iface
->dhcpv4_start
.s_addr
= htonl(blobmsg_get_u32(c
));
508 iface
->dhcpv4_end
.s_addr
= htonl(ntohl(iface
->dhcpv4_start
.s_addr
) +
511 if (config
.main_dhcpv4
&& config
.legacy
)
512 iface
->dhcpv4
= MODE_SERVER
;
515 if ((c
= tb
[IFACE_ATTR_LIMIT
]))
516 iface
->dhcpv4_end
.s_addr
= htonl(ntohl(iface
->dhcpv4_start
.s_addr
) +
517 blobmsg_get_u32(c
) - 1);
519 if ((c
= tb
[IFACE_ATTR_MASTER
]))
520 iface
->master
= blobmsg_get_bool(c
);
522 if (overwrite
&& (c
= tb
[IFACE_ATTR_UPSTREAM
])) {
523 struct blob_attr
*cur
;
526 blobmsg_for_each_attr(cur
, c
, rem
) {
527 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
|| !blobmsg_check_attr(cur
, false))
530 iface
->upstream
= realloc(iface
->upstream
,
531 iface
->upstream_len
+ blobmsg_data_len(cur
));
532 if (!iface
->upstream
)
535 memcpy(iface
->upstream
+ iface
->upstream_len
, blobmsg_get_string(cur
), blobmsg_data_len(cur
));
536 iface
->upstream_len
+= blobmsg_data_len(cur
);
541 if ((c
= tb
[IFACE_ATTR_RA
])) {
542 if ((mode
= parse_mode(blobmsg_get_string(c
))) >= 0)
548 if ((c
= tb
[IFACE_ATTR_DHCPV4
])) {
549 if ((mode
= parse_mode(blobmsg_get_string(c
))) >= 0) {
550 if (config
.main_dhcpv4
)
551 iface
->dhcpv4
= mode
;
557 if ((c
= tb
[IFACE_ATTR_DHCPV6
])) {
558 if ((mode
= parse_mode(blobmsg_get_string(c
))) >= 0)
559 iface
->dhcpv6
= mode
;
564 if ((c
= tb
[IFACE_ATTR_NDP
])) {
565 if ((mode
= parse_mode(blobmsg_get_string(c
))) >= 0)
571 if ((c
= tb
[IFACE_ATTR_ROUTER
])) {
572 struct blob_attr
*cur
;
575 blobmsg_for_each_attr(cur
, c
, rem
) {
576 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
|| !blobmsg_check_attr(cur
, false))
579 struct in_addr addr4
;
580 if (inet_pton(AF_INET
, blobmsg_get_string(cur
), &addr4
) == 1) {
581 iface
->dhcpv4_router
= realloc(iface
->dhcpv4_router
,
582 (++iface
->dhcpv4_router_cnt
) * sizeof(*iface
->dhcpv4_router
));
583 if (!iface
->dhcpv4_router
)
586 iface
->dhcpv4_router
[iface
->dhcpv4_router_cnt
- 1] = addr4
;
592 if ((c
= tb
[IFACE_ATTR_DNS
])) {
593 struct blob_attr
*cur
;
596 iface
->always_rewrite_dns
= true;
597 blobmsg_for_each_attr(cur
, c
, rem
) {
598 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
|| !blobmsg_check_attr(cur
, false))
601 struct in_addr addr4
;
602 struct in6_addr addr6
;
603 if (inet_pton(AF_INET
, blobmsg_get_string(cur
), &addr4
) == 1) {
604 if (addr4
.s_addr
== INADDR_ANY
)
607 iface
->dhcpv4_dns
= realloc(iface
->dhcpv4_dns
,
608 (++iface
->dhcpv4_dns_cnt
) * sizeof(*iface
->dhcpv4_dns
));
609 if (!iface
->dhcpv4_dns
)
612 iface
->dhcpv4_dns
[iface
->dhcpv4_dns_cnt
- 1] = addr4
;
613 } else if (inet_pton(AF_INET6
, blobmsg_get_string(cur
), &addr6
) == 1) {
614 if (IN6_IS_ADDR_UNSPECIFIED(&addr6
))
617 iface
->dns
= realloc(iface
->dns
,
618 (++iface
->dns_cnt
) * sizeof(*iface
->dns
));
622 iface
->dns
[iface
->dns_cnt
- 1] = addr6
;
628 if ((c
= tb
[IFACE_ATTR_DOMAIN
])) {
629 struct blob_attr
*cur
;
632 blobmsg_for_each_attr(cur
, c
, rem
) {
633 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
|| !blobmsg_check_attr(cur
, false))
637 char *domain
= blobmsg_get_string(cur
);
638 size_t domainlen
= strlen(domain
);
639 if (domainlen
> 0 && domain
[domainlen
- 1] == '.')
640 domain
[domainlen
- 1] = 0;
642 int len
= dn_comp(domain
, buf
, sizeof(buf
), NULL
, NULL
);
646 iface
->search
= realloc(iface
->search
, iface
->search_len
+ len
);
650 memcpy(&iface
->search
[iface
->search_len
], buf
, len
);
651 iface
->search_len
+= len
;
655 if ((c
= tb
[IFACE_ATTR_FILTER_CLASS
])) {
656 iface
->filter_class
= realloc(iface
->filter_class
, blobmsg_data_len(c
) + 1);
657 memcpy(iface
->filter_class
, blobmsg_get_string(c
), blobmsg_data_len(c
) + 1);
660 if ((c
= tb
[IFACE_ATTR_DHCPV4_FORCERECONF
]))
661 iface
->dhcpv4_forcereconf
= blobmsg_get_bool(c
);
663 if ((c
= tb
[IFACE_ATTR_DHCPV6_RAW
])) {
664 iface
->dhcpv6_raw_len
= blobmsg_data_len(c
) / 2;
665 iface
->dhcpv6_raw
= realloc(iface
->dhcpv6_raw
, iface
->dhcpv6_raw_len
);
666 odhcpd_unhexlify(iface
->dhcpv6_raw
, iface
->dhcpv6_raw_len
, blobmsg_get_string(c
));
669 if ((c
= tb
[IFACE_ATTR_DHCPV6_ASSIGNALL
]))
670 iface
->dhcpv6_assignall
= blobmsg_get_bool(c
);
672 if ((c
= tb
[IFACE_ATTR_DHCPV6_PD
]))
673 iface
->dhcpv6_pd
= blobmsg_get_bool(c
);
675 if ((c
= tb
[IFACE_ATTR_DHCPV6_NA
]))
676 iface
->dhcpv6_na
= blobmsg_get_bool(c
);
678 if ((c
= tb
[IFACE_ATTR_RA_DEFAULT
]))
679 iface
->default_router
= blobmsg_get_u32(c
);
681 if ((c
= tb
[IFACE_ATTR_RA_MANAGEMENT
]))
682 iface
->ra_managed
= blobmsg_get_u32(c
);
684 if ((c
= tb
[IFACE_ATTR_RA_REACHABLETIME
])) {
685 uint32_t ra_reachabletime
= blobmsg_get_u32(c
);
686 if (ra_reachabletime
> 3600000)
689 iface
->ra_reachabletime
= ra_reachabletime
;
692 if ((c
= tb
[IFACE_ATTR_RA_RETRANSTIME
])) {
693 uint32_t ra_retranstime
= blobmsg_get_u32(c
);
694 if (ra_retranstime
> 60000)
697 iface
->ra_retranstime
= ra_retranstime
;
700 if ((c
= tb
[IFACE_ATTR_RA_HOPLIMIT
])) {
701 uint32_t ra_hoplimit
= blobmsg_get_u32(c
);
702 if (ra_hoplimit
> 255)
705 iface
->ra_hoplimit
= ra_hoplimit
;
708 if ((c
= tb
[IFACE_ATTR_RA_MTU
])) {
709 uint32_t ra_mtu
= blobmsg_get_u32(c
);
710 if (ra_mtu
< 1280 || ra_mtu
> 65535)
713 iface
->ra_mtu
= ra_mtu
;
716 if ((c
= tb
[IFACE_ATTR_RA_OFFLINK
]))
717 iface
->ra_not_onlink
= blobmsg_get_bool(c
);
719 if ((c
= tb
[IFACE_ATTR_RA_ADVROUTER
]))
720 iface
->ra_advrouter
= blobmsg_get_bool(c
);
722 if ((c
= tb
[IFACE_ATTR_RA_MININTERVAL
]))
723 iface
->ra_mininterval
= blobmsg_get_u32(c
);
725 if ((c
= tb
[IFACE_ATTR_RA_MAXINTERVAL
]))
726 iface
->ra_maxinterval
= blobmsg_get_u32(c
);
728 if ((c
= tb
[IFACE_ATTR_RA_LIFETIME
]))
729 iface
->ra_lifetime
= blobmsg_get_u32(c
);
731 if ((c
= tb
[IFACE_ATTR_RA_USELEASETIME
]))
732 iface
->ra_useleasetime
= blobmsg_get_bool(c
);
734 if ((c
= tb
[IFACE_ATTR_RA_DNS
]))
735 iface
->ra_dns
= blobmsg_get_bool(c
);
737 if ((c
= tb
[IFACE_ATTR_RA_PREFERENCE
])) {
738 const char *prio
= blobmsg_get_string(c
);
740 if (!strcmp(prio
, "high"))
741 iface
->route_preference
= 1;
742 else if (!strcmp(prio
, "low"))
743 iface
->route_preference
= -1;
744 else if (!strcmp(prio
, "medium") || !strcmp(prio
, "default"))
745 iface
->route_preference
= 0;
750 if ((c
= tb
[IFACE_ATTR_PD_MANAGER
]))
751 strncpy(iface
->dhcpv6_pd_manager
, blobmsg_get_string(c
),
752 sizeof(iface
->dhcpv6_pd_manager
) - 1);
754 if ((c
= tb
[IFACE_ATTR_PD_CER
]) &&
755 inet_pton(AF_INET6
, blobmsg_get_string(c
), &iface
->dhcpv6_pd_cer
) < 1)
758 if ((c
= tb
[IFACE_ATTR_NDPROXY_ROUTING
]))
759 iface
->learn_routes
= blobmsg_get_bool(c
);
761 if ((c
= tb
[IFACE_ATTR_NDPROXY_SLAVE
]))
762 iface
->external
= blobmsg_get_bool(c
);
764 if ((c
= tb
[IFACE_ATTR_PREFIX_FILTER
])) {
765 const char *str
= blobmsg_get_string(c
);
766 char *astr
= malloc(strlen(str
) + 1);
770 if (!astr
|| !strcpy(astr
, str
) ||
771 (delim
= strchr(astr
, '/')) == NULL
|| (*(delim
++) = 0) ||
772 sscanf(delim
, "%i", &l
) == 0 || l
> 128 ||
773 inet_pton(AF_INET6
, astr
, &iface
->pio_filter_addr
) == 0)
774 iface
->pio_filter_length
= 0;
776 iface
->pio_filter_length
= l
;
785 close_interface(iface
);
789 static int set_interface(struct uci_section
*s
)
791 blob_buf_init(&b
, 0);
792 uci_to_blob(&b
, s
, &interface_attr_list
);
794 return config_parse_interface(blob_data(b
.head
), blob_len(b
.head
), s
->e
.name
, true);
797 static void lease_delete_assignments(struct lease
*l
, bool v6
)
799 struct dhcp_assignment
*a
, *tmp
;
800 unsigned int flag
= v6
? OAF_DHCPV6
: OAF_DHCPV4
;
802 list_for_each_entry_safe(a
, tmp
, &l
->assignments
, lease_list
) {
808 static void lease_update_assignments(struct lease
*l
)
810 struct dhcp_assignment
*a
;
812 list_for_each_entry(a
, &l
->assignments
, lease_list
) {
818 a
->hostname
= strdup(l
->hostname
);
820 a
->leasetime
= l
->leasetime
;
824 static int lease_cmp(const void *k1
, const void *k2
, _unused
void *ptr
)
826 const struct lease
*l1
= k1
, *l2
= k2
;
829 if (l1
->duid_len
!= l2
->duid_len
)
830 return l1
->duid_len
- l2
->duid_len
;
832 if (l1
->duid_len
&& l2
->duid_len
)
833 cmp
= memcmp(l1
->duid
, l2
->duid
, l1
->duid_len
);
838 return memcmp(l1
->mac
.ether_addr_octet
, l2
->mac
.ether_addr_octet
,
839 sizeof(l1
->mac
.ether_addr_octet
));
842 static void lease_change_config(struct lease
*l_old
, struct lease
*l_new
)
846 if ((!!l_new
->hostname
!= !!l_old
->hostname
) ||
847 (l_new
->hostname
&& strcmp(l_new
->hostname
, l_old
->hostname
))) {
848 free(l_old
->hostname
);
849 l_old
->hostname
= NULL
;
852 l_old
->hostname
= strdup(l_new
->hostname
);
857 if (l_old
->leasetime
!= l_new
->leasetime
) {
858 l_old
->leasetime
= l_new
->leasetime
;
862 if (l_old
->ipaddr
!= l_new
->ipaddr
) {
863 l_old
->ipaddr
= l_new
->ipaddr
;
864 lease_delete_assignments(l_old
, false);
867 if (l_old
->hostid
!= l_new
->hostid
) {
868 l_old
->hostid
= l_new
->hostid
;
869 lease_delete_assignments(l_old
, true);
873 lease_update_assignments(l_old
);
878 static void lease_delete(struct lease
*l
)
880 struct dhcp_assignment
*a
;
882 list_for_each_entry(a
, &l
->assignments
, lease_list
)
888 static void lease_update(_unused
struct vlist_tree
*tree
, struct vlist_node
*node_new
,
889 struct vlist_node
*node_old
)
891 struct lease
*lease_new
= container_of(node_new
, struct lease
, node
);
892 struct lease
*lease_old
= container_of(node_old
, struct lease
, node
);
894 if (node_old
&& node_new
)
895 lease_change_config(lease_old
, lease_new
);
897 lease_delete(lease_old
);
900 struct lease
*config_find_lease_by_duid(const uint8_t *duid
, const uint16_t len
)
904 vlist_for_each_element(&leases
, l
, node
) {
905 if (l
->duid_len
== len
&& !memcmp(l
->duid
, duid
, len
))
912 struct lease
*config_find_lease_by_mac(const uint8_t *mac
)
916 vlist_for_each_element(&leases
, l
, node
) {
917 if (!memcmp(l
->mac
.ether_addr_octet
, mac
,
918 sizeof(l
->mac
.ether_addr_octet
)))
925 struct lease
*config_find_lease_by_hostid(const uint32_t hostid
)
929 vlist_for_each_element(&leases
, l
, node
) {
930 if (l
->hostid
== hostid
)
937 struct lease
*config_find_lease_by_ipaddr(const uint32_t ipaddr
)
941 vlist_for_each_element(&leases
, l
, node
) {
942 if (l
->ipaddr
== ipaddr
)
949 void odhcpd_reload(void)
951 struct uci_context
*uci
= uci_alloc_context();
952 struct interface
*master
= NULL
, *i
, *tmp
;
957 vlist_update(&leases
);
958 avl_for_each_element(&interfaces
, i
, avl
)
961 struct uci_package
*dhcp
= NULL
;
962 if (!uci_load(uci
, "dhcp", &dhcp
)) {
963 struct uci_element
*e
;
964 uci_foreach_element(&dhcp
->sections
, e
) {
965 struct uci_section
*s
= uci_to_section(e
);
966 if (!strcmp(s
->type
, "host"))
968 else if (!strcmp(s
->type
, "odhcpd"))
972 uci_foreach_element(&dhcp
->sections
, e
) {
973 struct uci_section
*s
= uci_to_section(e
);
974 if (!strcmp(s
->type
, "dhcp"))
979 if (config
.dhcp_statefile
) {
980 char *path
= strdup(config
.dhcp_statefile
);
982 mkdir_p(dirname(path
), 0755);
986 vlist_flush(&leases
);
989 ubus_apply_network();
992 bool any_dhcpv6_slave
= false, any_ra_slave
= false, any_ndp_slave
= false;
995 avl_for_each_element(&interfaces
, i
, avl
) {
999 if (i
->dhcpv6
== MODE_HYBRID
|| i
->dhcpv6
== MODE_RELAY
)
1000 any_dhcpv6_slave
= true;
1002 if (i
->ra
== MODE_HYBRID
|| i
->ra
== MODE_RELAY
)
1003 any_ra_slave
= true;
1005 if (i
->ndp
== MODE_HYBRID
|| i
->ndp
== MODE_RELAY
)
1006 any_ndp_slave
= true;
1009 /* Evaluate hybrid mode for master */
1010 avl_for_each_element(&interfaces
, i
, avl
) {
1014 enum odhcpd_mode hybrid_mode
= MODE_DISABLED
;
1016 if (!ubus_has_prefix(i
->name
, i
->ifname
))
1017 hybrid_mode
= MODE_RELAY
;
1020 if (i
->dhcpv6
== MODE_HYBRID
)
1021 i
->dhcpv6
= hybrid_mode
;
1023 if (i
->dhcpv6
== MODE_RELAY
&& !any_dhcpv6_slave
)
1024 i
->dhcpv6
= MODE_DISABLED
;
1026 if (i
->ra
== MODE_HYBRID
)
1027 i
->ra
= hybrid_mode
;
1029 if (i
->ra
== MODE_RELAY
&& !any_ra_slave
)
1030 i
->ra
= MODE_DISABLED
;
1032 if (i
->ndp
== MODE_HYBRID
)
1033 i
->ndp
= hybrid_mode
;
1035 if (i
->ndp
== MODE_RELAY
&& !any_ndp_slave
)
1036 i
->ndp
= MODE_DISABLED
;
1038 if (i
->dhcpv6
== MODE_RELAY
|| i
->ra
== MODE_RELAY
|| i
->ndp
== MODE_RELAY
)
1043 avl_for_each_element_safe(&interfaces
, i
, avl
, tmp
) {
1045 /* Resolve hybrid mode */
1046 if (i
->dhcpv6
== MODE_HYBRID
)
1047 i
->dhcpv6
= (master
&& master
->dhcpv6
== MODE_RELAY
) ?
1048 MODE_RELAY
: MODE_SERVER
;
1050 if (i
->ra
== MODE_HYBRID
)
1051 i
->ra
= (master
&& master
->ra
== MODE_RELAY
) ?
1052 MODE_RELAY
: MODE_SERVER
;
1054 if (i
->ndp
== MODE_HYBRID
)
1055 i
->ndp
= (master
&& master
->ndp
== MODE_RELAY
) ?
1056 MODE_RELAY
: MODE_DISABLED
;
1058 router_setup_interface(i
, !i
->ignore
|| i
->ra
!= MODE_DISABLED
);
1059 dhcpv6_setup_interface(i
, !i
->ignore
|| i
->dhcpv6
!= MODE_DISABLED
);
1060 ndp_setup_interface(i
, !i
->ignore
|| i
->ndp
!= MODE_DISABLED
);
1061 #ifdef DHCPV4_SUPPORT
1062 dhcpv4_setup_interface(i
, !i
->ignore
|| i
->dhcpv4
!= MODE_DISABLED
);
1068 uci_unload(uci
, dhcp
);
1069 uci_free_context(uci
);
1072 static void handle_signal(int signal
)
1076 if (signal
== SIGHUP
) {
1077 if (write(reload_pipe
[1], b
, sizeof(b
)) < 0) {}
1082 static void reload_cb(struct uloop_fd
*u
, _unused
unsigned int events
)
1085 if (read(u
->fd
, b
, sizeof(b
)) < 0) {}
1090 static struct uloop_fd reload_fd
= { .fd
= -1, .cb
= reload_cb
};
1092 void odhcpd_run(void)
1094 if (pipe2(reload_pipe
, O_NONBLOCK
| O_CLOEXEC
) < 0) {}
1096 reload_fd
.fd
= reload_pipe
[0];
1097 uloop_fd_add(&reload_fd
, ULOOP_READ
);
1099 signal(SIGTERM
, handle_signal
);
1100 signal(SIGINT
, handle_signal
);
1101 signal(SIGHUP
, handle_signal
);