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
,
88 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
89 [IFACE_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
90 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
91 [IFACE_ATTR_NETWORKID
] = { .name
= "networkid", .type
= BLOBMSG_TYPE_STRING
},
92 [IFACE_ATTR_DYNAMICDHCP
] = { .name
= "dynamicdhcp", .type
= BLOBMSG_TYPE_BOOL
},
93 [IFACE_ATTR_LEASETIME
] = { .name
= "leasetime", .type
= BLOBMSG_TYPE_STRING
},
94 [IFACE_ATTR_START
] = { .name
= "start", .type
= BLOBMSG_TYPE_INT32
},
95 [IFACE_ATTR_LIMIT
] = { .name
= "limit", .type
= BLOBMSG_TYPE_INT32
},
96 [IFACE_ATTR_MASTER
] = { .name
= "master", .type
= BLOBMSG_TYPE_BOOL
},
97 [IFACE_ATTR_UPSTREAM
] = { .name
= "upstream", .type
= BLOBMSG_TYPE_ARRAY
},
98 [IFACE_ATTR_RA
] = { .name
= "ra", .type
= BLOBMSG_TYPE_STRING
},
99 [IFACE_ATTR_DHCPV4
] = { .name
= "dhcpv4", .type
= BLOBMSG_TYPE_STRING
},
100 [IFACE_ATTR_DHCPV6
] = { .name
= "dhcpv6", .type
= BLOBMSG_TYPE_STRING
},
101 [IFACE_ATTR_NDP
] = { .name
= "ndp", .type
= BLOBMSG_TYPE_STRING
},
102 [IFACE_ATTR_ROUTER
] = { .name
= "router", .type
= BLOBMSG_TYPE_ARRAY
},
103 [IFACE_ATTR_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
104 [IFACE_ATTR_DNS_SERVICE
] = { .name
= "dns_service", .type
= BLOBMSG_TYPE_BOOL
},
105 [IFACE_ATTR_DOMAIN
] = { .name
= "domain", .type
= BLOBMSG_TYPE_ARRAY
},
106 [IFACE_ATTR_FILTER_CLASS
] = { .name
= "filter_class", .type
= BLOBMSG_TYPE_STRING
},
107 [IFACE_ATTR_DHCPV4_FORCERECONF
] = { .name
= "dhcpv4_forcereconf", .type
= BLOBMSG_TYPE_BOOL
},
108 [IFACE_ATTR_DHCPV6_RAW
] = { .name
= "dhcpv6_raw", .type
= BLOBMSG_TYPE_STRING
},
109 [IFACE_ATTR_DHCPV6_ASSIGNALL
] = { .name
="dhcpv6_assignall", .type
= BLOBMSG_TYPE_BOOL
},
110 [IFACE_ATTR_DHCPV6_PD
] = { .name
= "dhcpv6_pd", .type
= BLOBMSG_TYPE_BOOL
},
111 [IFACE_ATTR_DHCPV6_NA
] = { .name
= "dhcpv6_na", .type
= BLOBMSG_TYPE_BOOL
},
112 [IFACE_ATTR_PD_MANAGER
] = { .name
= "pd_manager", .type
= BLOBMSG_TYPE_STRING
},
113 [IFACE_ATTR_PD_CER
] = { .name
= "pd_cer", .type
= BLOBMSG_TYPE_STRING
},
114 [IFACE_ATTR_RA_DEFAULT
] = { .name
= "ra_default", .type
= BLOBMSG_TYPE_INT32
},
115 [IFACE_ATTR_RA_MANAGEMENT
] = { .name
= "ra_management", .type
= BLOBMSG_TYPE_INT32
},
116 [IFACE_ATTR_RA_FLAGS
] = { .name
= "ra_flags", . type
= BLOBMSG_TYPE_ARRAY
},
117 [IFACE_ATTR_RA_SLAAC
] = { .name
= "ra_slaac", .type
= BLOBMSG_TYPE_BOOL
},
118 [IFACE_ATTR_RA_OFFLINK
] = { .name
= "ra_offlink", .type
= BLOBMSG_TYPE_BOOL
},
119 [IFACE_ATTR_RA_PREFERENCE
] = { .name
= "ra_preference", .type
= BLOBMSG_TYPE_STRING
},
120 [IFACE_ATTR_RA_ADVROUTER
] = { .name
= "ra_advrouter", .type
= BLOBMSG_TYPE_BOOL
},
121 [IFACE_ATTR_RA_MININTERVAL
] = { .name
= "ra_mininterval", .type
= BLOBMSG_TYPE_INT32
},
122 [IFACE_ATTR_RA_MAXINTERVAL
] = { .name
= "ra_maxinterval", .type
= BLOBMSG_TYPE_INT32
},
123 [IFACE_ATTR_RA_LIFETIME
] = { .name
= "ra_lifetime", .type
= BLOBMSG_TYPE_INT32
},
124 [IFACE_ATTR_RA_USELEASETIME
] = { .name
= "ra_useleasetime", .type
= BLOBMSG_TYPE_BOOL
},
125 [IFACE_ATTR_RA_REACHABLETIME
] = { .name
= "ra_reachabletime", .type
= BLOBMSG_TYPE_INT32
},
126 [IFACE_ATTR_RA_RETRANSTIME
] = { .name
= "ra_retranstime", .type
= BLOBMSG_TYPE_INT32
},
127 [IFACE_ATTR_RA_HOPLIMIT
] = { .name
= "ra_hoplimit", .type
= BLOBMSG_TYPE_INT32
},
128 [IFACE_ATTR_RA_MTU
] = { .name
= "ra_mtu", .type
= BLOBMSG_TYPE_INT32
},
129 [IFACE_ATTR_RA_DNS
] = { .name
= "ra_dns", .type
= BLOBMSG_TYPE_BOOL
},
130 [IFACE_ATTR_NDPROXY_ROUTING
] = { .name
= "ndproxy_routing", .type
= BLOBMSG_TYPE_BOOL
},
131 [IFACE_ATTR_NDPROXY_SLAVE
] = { .name
= "ndproxy_slave", .type
= BLOBMSG_TYPE_BOOL
},
132 [IFACE_ATTR_PREFIX_FILTER
] = { .name
= "prefix_filter", .type
= BLOBMSG_TYPE_STRING
},
135 static const struct uci_blob_param_info iface_attr_info
[IFACE_ATTR_MAX
] = {
136 [IFACE_ATTR_UPSTREAM
] = { .type
= BLOBMSG_TYPE_STRING
},
137 [IFACE_ATTR_DNS
] = { .type
= BLOBMSG_TYPE_STRING
},
138 [IFACE_ATTR_DOMAIN
] = { .type
= BLOBMSG_TYPE_STRING
},
141 const struct uci_blob_param_list interface_attr_list
= {
142 .n_params
= IFACE_ATTR_MAX
,
143 .params
= iface_attrs
,
144 .info
= iface_attr_info
,
147 const struct blobmsg_policy lease_attrs
[LEASE_ATTR_MAX
] = {
148 [LEASE_ATTR_IP
] = { .name
= "ip", .type
= BLOBMSG_TYPE_STRING
},
149 [LEASE_ATTR_MAC
] = { .name
= "mac", .type
= BLOBMSG_TYPE_STRING
},
150 [LEASE_ATTR_DUID
] = { .name
= "duid", .type
= BLOBMSG_TYPE_STRING
},
151 [LEASE_ATTR_HOSTID
] = { .name
= "hostid", .type
= BLOBMSG_TYPE_STRING
},
152 [LEASE_ATTR_LEASETIME
] = { .name
= "leasetime", .type
= BLOBMSG_TYPE_STRING
},
153 [LEASE_ATTR_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
156 const struct uci_blob_param_list lease_attr_list
= {
157 .n_params
= LEASE_ATTR_MAX
,
158 .params
= lease_attrs
,
163 ODHCPD_ATTR_MAINDHCP
,
164 ODHCPD_ATTR_LEASEFILE
,
165 ODHCPD_ATTR_LEASETRIGGER
,
166 ODHCPD_ATTR_LOGLEVEL
,
170 static const struct blobmsg_policy odhcpd_attrs
[ODHCPD_ATTR_MAX
] = {
171 [ODHCPD_ATTR_LEGACY
] = { .name
= "legacy", .type
= BLOBMSG_TYPE_BOOL
},
172 [ODHCPD_ATTR_MAINDHCP
] = { .name
= "maindhcp", .type
= BLOBMSG_TYPE_BOOL
},
173 [ODHCPD_ATTR_LEASEFILE
] = { .name
= "leasefile", .type
= BLOBMSG_TYPE_STRING
},
174 [ODHCPD_ATTR_LEASETRIGGER
] = { .name
= "leasetrigger", .type
= BLOBMSG_TYPE_STRING
},
175 [ODHCPD_ATTR_LOGLEVEL
] = { .name
= "loglevel", .type
= BLOBMSG_TYPE_INT32
},
178 const struct uci_blob_param_list odhcpd_attr_list
= {
179 .n_params
= ODHCPD_ATTR_MAX
,
180 .params
= odhcpd_attrs
,
183 static const struct { const char *name
; uint8_t flag
; } ra_flags
[] = {
184 { .name
= "managed-config", .flag
= ND_RA_FLAG_MANAGED
},
185 { .name
= "other-config", .flag
= ND_RA_FLAG_OTHER
},
186 { .name
= "home-agent", .flag
= ND_RA_FLAG_HOME_AGENT
},
187 { .name
= "none", . flag
= 0 },
191 static void set_interface_defaults(struct interface
*iface
)
193 iface
->ignore
= true;
194 iface
->dhcpv4
= MODE_DISABLED
;
195 iface
->dhcpv6
= MODE_DISABLED
;
196 iface
->ra
= MODE_DISABLED
;
197 iface
->ndp
= MODE_DISABLED
;
198 iface
->learn_routes
= 1;
199 iface
->dhcp_leasetime
= 43200;
200 iface
->dhcpv4_start
.s_addr
= htonl(START_DEFAULT
);
201 iface
->dhcpv4_end
.s_addr
= htonl(START_DEFAULT
+ LIMIT_DEFAULT
- 1);
202 iface
->dhcpv6_assignall
= true;
203 iface
->dhcpv6_pd
= true;
204 iface
->dhcpv6_na
= true;
205 iface
->dns_service
= true;
206 iface
->ra_flags
= ND_RA_FLAG_OTHER
;
207 iface
->ra_slaac
= true;
208 iface
->ra_maxinterval
= 600;
209 iface
->ra_mininterval
= iface
->ra_maxinterval
/3;
210 iface
->ra_lifetime
= -1;
211 iface
->ra_dns
= true;
214 static void clean_interface(struct interface
*iface
)
218 free(iface
->upstream
);
219 free(iface
->dhcpv4_router
);
220 free(iface
->dhcpv4_dns
);
221 free(iface
->dhcpv6_raw
);
222 free(iface
->filter_class
);
223 memset(&iface
->ra
, 0, sizeof(*iface
) - offsetof(struct interface
, ra
));
224 set_interface_defaults(iface
);
227 static void close_interface(struct interface
*iface
)
229 avl_delete(&interfaces
, &iface
->avl
);
231 router_setup_interface(iface
, false);
232 dhcpv6_setup_interface(iface
, false);
233 ndp_setup_interface(iface
, false);
234 #ifdef DHCPV4_SUPPORT
235 dhcpv4_setup_interface(iface
, false);
238 clean_interface(iface
);
245 static int parse_mode(const char *mode
)
247 if (!strcmp(mode
, "disabled"))
248 return MODE_DISABLED
;
249 else if (!strcmp(mode
, "server"))
251 else if (!strcmp(mode
, "relay"))
253 else if (!strcmp(mode
, "hybrid"))
259 static int parse_ra_flags(uint8_t *flags
, struct blob_attr
*attr
)
261 struct blob_attr
*cur
;
264 blobmsg_for_each_attr(cur
, attr
, rem
) {
267 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
270 if (!blobmsg_check_attr(cur
, false))
273 for (i
= 0; ra_flags
[i
].name
; i
++) {
274 if (!strcmp(ra_flags
[i
].name
, blobmsg_get_string(cur
))) {
275 *flags
|= ra_flags
[i
].flag
;
280 if (!ra_flags
[i
].name
)
287 static void set_config(struct uci_section
*s
)
289 struct blob_attr
*tb
[ODHCPD_ATTR_MAX
], *c
;
291 blob_buf_init(&b
, 0);
292 uci_to_blob(&b
, s
, &odhcpd_attr_list
);
293 blobmsg_parse(odhcpd_attrs
, ODHCPD_ATTR_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
295 if ((c
= tb
[ODHCPD_ATTR_LEGACY
]))
296 config
.legacy
= blobmsg_get_bool(c
);
298 if ((c
= tb
[ODHCPD_ATTR_MAINDHCP
]))
299 config
.main_dhcpv4
= blobmsg_get_bool(c
);
301 if ((c
= tb
[ODHCPD_ATTR_LEASEFILE
])) {
302 free(config
.dhcp_statefile
);
303 config
.dhcp_statefile
= strdup(blobmsg_get_string(c
));
306 if ((c
= tb
[ODHCPD_ATTR_LEASETRIGGER
])) {
307 free(config
.dhcp_cb
);
308 config
.dhcp_cb
= strdup(blobmsg_get_string(c
));
311 if ((c
= tb
[ODHCPD_ATTR_LOGLEVEL
])) {
312 int log_level
= (blobmsg_get_u32(c
) & LOG_PRIMASK
);
314 if (config
.log_level
!= log_level
) {
315 config
.log_level
= log_level
;
316 setlogmask(LOG_UPTO(config
.log_level
));
321 static double parse_leasetime(struct blob_attr
*c
) {
322 char *val
= blobmsg_get_string(c
), *endptr
= NULL
;
323 double time
= strcmp(val
, "infinite") ? strtod(val
, &endptr
) : UINT32_MAX
;
325 if (time
&& endptr
&& endptr
[0]) {
326 if (endptr
[0] == 's')
328 else if (endptr
[0] == 'm')
330 else if (endptr
[0] == 'h')
332 else if (endptr
[0] == 'd')
334 else if (endptr
[0] == 'w')
335 time
*= 7 * 24 * 3600;
349 static void free_lease(struct lease
*l
)
356 int set_lease_from_blobmsg(struct blob_attr
*ba
)
358 struct blob_attr
*tb
[LEASE_ATTR_MAX
], *c
;
363 blobmsg_parse(lease_attrs
, LEASE_ATTR_MAX
, tb
, blob_data(ba
), blob_len(ba
));
365 if ((c
= tb
[LEASE_ATTR_DUID
]))
366 duidlen
= (blobmsg_data_len(c
) - 1) / 2;
368 l
= calloc_a(sizeof(*l
), &duid
, duidlen
);
372 if ((c
= tb
[LEASE_ATTR_MAC
]))
373 if (!ether_aton_r(blobmsg_get_string(c
), &l
->mac
))
376 if ((c
= tb
[LEASE_ATTR_DUID
])) {
380 len
= odhcpd_unhexlify(l
->duid
, duidlen
, blobmsg_get_string(c
));
388 if ((c
= tb
[LEASE_ATTR_NAME
])) {
389 l
->hostname
= strdup(blobmsg_get_string(c
));
390 if (!l
->hostname
|| !odhcpd_valid_hostname(l
->hostname
))
394 if ((c
= tb
[LEASE_ATTR_IP
]))
395 if (inet_pton(AF_INET
, blobmsg_get_string(c
), &l
->ipaddr
) < 0)
398 if ((c
= tb
[LEASE_ATTR_HOSTID
])) {
400 l
->hostid
= strtoul(blobmsg_get_string(c
), NULL
, 16);
404 uint32_t i4a
= ntohl(l
->ipaddr
) & 0xff;
405 l
->hostid
= ((i4a
/ 100) << 8) | (((i4a
% 100) / 10) << 4) | (i4a
% 10);
408 if ((c
= tb
[LEASE_ATTR_LEASETIME
])) {
409 double time
= parse_leasetime(c
);
416 INIT_LIST_HEAD(&l
->assignments
);
417 vlist_add(&leases
, &l
->node
, l
);
427 static int set_lease_from_uci(struct uci_section
*s
)
429 blob_buf_init(&b
, 0);
430 uci_to_blob(&b
, s
, &lease_attr_list
);
432 return set_lease_from_blobmsg(b
.head
);
435 int config_parse_interface(void *data
, size_t len
, const char *name
, bool overwrite
)
437 struct interface
*iface
;
438 struct blob_attr
*tb
[IFACE_ATTR_MAX
], *c
;
439 bool get_addrs
= false;
441 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
, data
, len
);
443 if (tb
[IFACE_ATTR_INTERFACE
])
444 name
= blobmsg_get_string(tb
[IFACE_ATTR_INTERFACE
]);
449 iface
= avl_find_element(&interfaces
, name
, iface
, avl
);
453 iface
= calloc_a(sizeof(*iface
), &new_name
, strlen(name
) + 1);
457 iface
->name
= strcpy(new_name
, name
);
458 iface
->avl
.key
= iface
->name
;
459 iface
->router_event
.uloop
.fd
= -1;
460 iface
->dhcpv6_event
.uloop
.fd
= -1;
461 iface
->ndp_event
.uloop
.fd
= -1;
462 iface
->ndp_ping_fd
= -1;
463 iface
->dhcpv4_event
.uloop
.fd
= -1;
464 INIT_LIST_HEAD(&iface
->ia_assignments
);
465 INIT_LIST_HEAD(&iface
->dhcpv4_assignments
);
466 INIT_LIST_HEAD(&iface
->dhcpv4_fr_ips
);
468 set_interface_defaults(iface
);
470 avl_insert(&interfaces
, &iface
->avl
);
471 get_addrs
= overwrite
= true;
474 const char *ifname
= NULL
;
476 if ((c
= tb
[IFACE_ATTR_IFNAME
]))
477 ifname
= blobmsg_get_string(c
);
478 else if ((c
= tb
[IFACE_ATTR_NETWORKID
]))
479 ifname
= blobmsg_get_string(c
);
483 if (overwrite
|| !iface
->ifname
)
484 ifname
= ubus_get_ifname(name
);
487 if (!iface
->ifname
&& !ifname
)
492 iface
->ifname
= strdup(ifname
);
497 if (!iface
->ifindex
&&
498 (iface
->ifindex
= if_nametoindex(iface
->ifname
)) <= 0)
503 ssize_t len
= netlink_get_interface_addrs(iface
->ifindex
,
504 true, &iface
->addr6
);
507 iface
->addr6_len
= len
;
509 len
= netlink_get_interface_addrs(iface
->ifindex
,
510 false, &iface
->addr4
);
512 iface
->addr4_len
= len
;
517 if ((c
= tb
[IFACE_ATTR_DYNAMICDHCP
]))
518 iface
->no_dynamic_dhcp
= !blobmsg_get_bool(c
);
520 if ((c
= tb
[IFACE_ATTR_LEASETIME
])) {
521 double time
= parse_leasetime(c
);
525 iface
->dhcp_leasetime
= time
;
528 if ((c
= tb
[IFACE_ATTR_START
])) {
529 iface
->dhcpv4_start
.s_addr
= htonl(blobmsg_get_u32(c
));
530 iface
->dhcpv4_end
.s_addr
= htonl(ntohl(iface
->dhcpv4_start
.s_addr
) +
533 if (config
.main_dhcpv4
&& config
.legacy
)
534 iface
->dhcpv4
= MODE_SERVER
;
537 if ((c
= tb
[IFACE_ATTR_LIMIT
]))
538 iface
->dhcpv4_end
.s_addr
= htonl(ntohl(iface
->dhcpv4_start
.s_addr
) +
539 blobmsg_get_u32(c
) - 1);
541 if ((c
= tb
[IFACE_ATTR_MASTER
]))
542 iface
->master
= blobmsg_get_bool(c
);
544 if (overwrite
&& (c
= tb
[IFACE_ATTR_UPSTREAM
])) {
545 struct blob_attr
*cur
;
548 blobmsg_for_each_attr(cur
, c
, rem
) {
549 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
|| !blobmsg_check_attr(cur
, false))
552 iface
->upstream
= realloc(iface
->upstream
,
553 iface
->upstream_len
+ blobmsg_data_len(cur
));
554 if (!iface
->upstream
)
557 memcpy(iface
->upstream
+ iface
->upstream_len
, blobmsg_get_string(cur
), blobmsg_data_len(cur
));
558 iface
->upstream_len
+= blobmsg_data_len(cur
);
563 if ((c
= tb
[IFACE_ATTR_RA
])) {
564 if ((mode
= parse_mode(blobmsg_get_string(c
))) >= 0) {
567 if (iface
->ra
!= MODE_DISABLED
)
568 iface
->ignore
= false;
573 if ((c
= tb
[IFACE_ATTR_DHCPV4
])) {
574 if ((mode
= parse_mode(blobmsg_get_string(c
))) >= 0) {
575 if (config
.main_dhcpv4
) {
576 iface
->dhcpv4
= mode
;
578 if (iface
->dhcpv4
!= MODE_DISABLED
)
579 iface
->ignore
= false;
586 if ((c
= tb
[IFACE_ATTR_DHCPV6
])) {
587 if ((mode
= parse_mode(blobmsg_get_string(c
))) >= 0) {
588 iface
->dhcpv6
= mode
;
590 if (iface
->dhcpv6
!= MODE_DISABLED
)
591 iface
->ignore
= false;
596 if ((c
= tb
[IFACE_ATTR_NDP
])) {
597 if ((mode
= parse_mode(blobmsg_get_string(c
))) >= 0) {
600 if (iface
->ndp
!= MODE_DISABLED
)
601 iface
->ignore
= false;
606 if ((c
= tb
[IFACE_ATTR_ROUTER
])) {
607 struct blob_attr
*cur
;
610 blobmsg_for_each_attr(cur
, c
, rem
) {
611 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
|| !blobmsg_check_attr(cur
, false))
614 struct in_addr addr4
;
615 if (inet_pton(AF_INET
, blobmsg_get_string(cur
), &addr4
) == 1) {
616 iface
->dhcpv4_router
= realloc(iface
->dhcpv4_router
,
617 (++iface
->dhcpv4_router_cnt
) * sizeof(*iface
->dhcpv4_router
));
618 if (!iface
->dhcpv4_router
)
621 iface
->dhcpv4_router
[iface
->dhcpv4_router_cnt
- 1] = addr4
;
627 if ((c
= tb
[IFACE_ATTR_DNS
])) {
628 struct blob_attr
*cur
;
631 iface
->always_rewrite_dns
= true;
632 blobmsg_for_each_attr(cur
, c
, rem
) {
633 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
|| !blobmsg_check_attr(cur
, false))
636 struct in_addr addr4
;
637 struct in6_addr addr6
;
638 if (inet_pton(AF_INET
, blobmsg_get_string(cur
), &addr4
) == 1) {
639 if (addr4
.s_addr
== INADDR_ANY
)
642 iface
->dhcpv4_dns
= realloc(iface
->dhcpv4_dns
,
643 (++iface
->dhcpv4_dns_cnt
) * sizeof(*iface
->dhcpv4_dns
));
644 if (!iface
->dhcpv4_dns
)
647 iface
->dhcpv4_dns
[iface
->dhcpv4_dns_cnt
- 1] = addr4
;
648 } else if (inet_pton(AF_INET6
, blobmsg_get_string(cur
), &addr6
) == 1) {
649 if (IN6_IS_ADDR_UNSPECIFIED(&addr6
))
652 iface
->dns
= realloc(iface
->dns
,
653 (++iface
->dns_cnt
) * sizeof(*iface
->dns
));
657 iface
->dns
[iface
->dns_cnt
- 1] = addr6
;
663 if ((c
= tb
[IFACE_ATTR_DNS_SERVICE
]))
664 iface
->dns_service
= blobmsg_get_bool(c
);
666 if ((c
= tb
[IFACE_ATTR_DOMAIN
])) {
667 struct blob_attr
*cur
;
670 blobmsg_for_each_attr(cur
, c
, rem
) {
671 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
|| !blobmsg_check_attr(cur
, false))
675 char *domain
= blobmsg_get_string(cur
);
676 size_t domainlen
= strlen(domain
);
677 if (domainlen
> 0 && domain
[domainlen
- 1] == '.')
678 domain
[domainlen
- 1] = 0;
680 int len
= dn_comp(domain
, buf
, sizeof(buf
), NULL
, NULL
);
684 iface
->search
= realloc(iface
->search
, iface
->search_len
+ len
);
688 memcpy(&iface
->search
[iface
->search_len
], buf
, len
);
689 iface
->search_len
+= len
;
693 if ((c
= tb
[IFACE_ATTR_FILTER_CLASS
])) {
694 iface
->filter_class
= realloc(iface
->filter_class
, blobmsg_data_len(c
) + 1);
695 memcpy(iface
->filter_class
, blobmsg_get_string(c
), blobmsg_data_len(c
) + 1);
698 if ((c
= tb
[IFACE_ATTR_DHCPV4_FORCERECONF
]))
699 iface
->dhcpv4_forcereconf
= blobmsg_get_bool(c
);
701 if ((c
= tb
[IFACE_ATTR_DHCPV6_RAW
])) {
702 iface
->dhcpv6_raw_len
= blobmsg_data_len(c
) / 2;
703 iface
->dhcpv6_raw
= realloc(iface
->dhcpv6_raw
, iface
->dhcpv6_raw_len
);
704 odhcpd_unhexlify(iface
->dhcpv6_raw
, iface
->dhcpv6_raw_len
, blobmsg_get_string(c
));
707 if ((c
= tb
[IFACE_ATTR_DHCPV6_ASSIGNALL
]))
708 iface
->dhcpv6_assignall
= blobmsg_get_bool(c
);
710 if ((c
= tb
[IFACE_ATTR_DHCPV6_PD
]))
711 iface
->dhcpv6_pd
= blobmsg_get_bool(c
);
713 if ((c
= tb
[IFACE_ATTR_DHCPV6_NA
]))
714 iface
->dhcpv6_na
= blobmsg_get_bool(c
);
716 if ((c
= tb
[IFACE_ATTR_RA_DEFAULT
]))
717 iface
->default_router
= blobmsg_get_u32(c
);
719 if (!tb
[IFACE_ATTR_RA_FLAGS
] && !tb
[IFACE_ATTR_RA_SLAAC
] &&
720 (c
= tb
[IFACE_ATTR_RA_MANAGEMENT
])) {
721 switch (blobmsg_get_u32(c
)) {
723 iface
->ra_flags
= ND_RA_FLAG_OTHER
;
724 iface
->ra_slaac
= true;
727 iface
->ra_flags
= ND_RA_FLAG_OTHER
|ND_RA_FLAG_MANAGED
;
728 iface
->ra_slaac
= true;
731 iface
->ra_flags
= ND_RA_FLAG_OTHER
|ND_RA_FLAG_MANAGED
;
732 iface
->ra_slaac
= false;
739 if ((c
= tb
[IFACE_ATTR_RA_FLAGS
])) {
741 if (parse_ra_flags(&iface
->ra_flags
, c
) < 0)
745 if ((c
= tb
[IFACE_ATTR_RA_REACHABLETIME
])) {
746 uint32_t ra_reachabletime
= blobmsg_get_u32(c
);
747 if (ra_reachabletime
> 3600000)
750 iface
->ra_reachabletime
= ra_reachabletime
;
753 if ((c
= tb
[IFACE_ATTR_RA_RETRANSTIME
])) {
754 uint32_t ra_retranstime
= blobmsg_get_u32(c
);
755 if (ra_retranstime
> 60000)
758 iface
->ra_retranstime
= ra_retranstime
;
761 if ((c
= tb
[IFACE_ATTR_RA_HOPLIMIT
])) {
762 uint32_t ra_hoplimit
= blobmsg_get_u32(c
);
763 if (ra_hoplimit
> 255)
766 iface
->ra_hoplimit
= ra_hoplimit
;
769 if ((c
= tb
[IFACE_ATTR_RA_MTU
])) {
770 uint32_t ra_mtu
= blobmsg_get_u32(c
);
771 if (ra_mtu
< 1280 || ra_mtu
> 65535)
774 iface
->ra_mtu
= ra_mtu
;
777 if ((c
= tb
[IFACE_ATTR_RA_SLAAC
]))
778 iface
->ra_slaac
= blobmsg_get_bool(c
);
780 if ((c
= tb
[IFACE_ATTR_RA_OFFLINK
]))
781 iface
->ra_not_onlink
= blobmsg_get_bool(c
);
783 if ((c
= tb
[IFACE_ATTR_RA_ADVROUTER
]))
784 iface
->ra_advrouter
= blobmsg_get_bool(c
);
786 if ((c
= tb
[IFACE_ATTR_RA_MININTERVAL
]))
787 iface
->ra_mininterval
= blobmsg_get_u32(c
);
789 if ((c
= tb
[IFACE_ATTR_RA_MAXINTERVAL
]))
790 iface
->ra_maxinterval
= blobmsg_get_u32(c
);
792 if ((c
= tb
[IFACE_ATTR_RA_LIFETIME
]))
793 iface
->ra_lifetime
= blobmsg_get_u32(c
);
795 if ((c
= tb
[IFACE_ATTR_RA_USELEASETIME
]))
796 iface
->ra_useleasetime
= blobmsg_get_bool(c
);
798 if ((c
= tb
[IFACE_ATTR_RA_DNS
]))
799 iface
->ra_dns
= blobmsg_get_bool(c
);
801 if ((c
= tb
[IFACE_ATTR_RA_PREFERENCE
])) {
802 const char *prio
= blobmsg_get_string(c
);
804 if (!strcmp(prio
, "high"))
805 iface
->route_preference
= 1;
806 else if (!strcmp(prio
, "low"))
807 iface
->route_preference
= -1;
808 else if (!strcmp(prio
, "medium") || !strcmp(prio
, "default"))
809 iface
->route_preference
= 0;
814 if ((c
= tb
[IFACE_ATTR_PD_MANAGER
]))
815 strncpy(iface
->dhcpv6_pd_manager
, blobmsg_get_string(c
),
816 sizeof(iface
->dhcpv6_pd_manager
) - 1);
818 if ((c
= tb
[IFACE_ATTR_PD_CER
]) &&
819 inet_pton(AF_INET6
, blobmsg_get_string(c
), &iface
->dhcpv6_pd_cer
) < 1)
822 if ((c
= tb
[IFACE_ATTR_NDPROXY_ROUTING
]))
823 iface
->learn_routes
= blobmsg_get_bool(c
);
825 if ((c
= tb
[IFACE_ATTR_NDPROXY_SLAVE
]))
826 iface
->external
= blobmsg_get_bool(c
);
828 if ((c
= tb
[IFACE_ATTR_PREFIX_FILTER
])) {
829 const char *str
= blobmsg_get_string(c
);
830 char *astr
= malloc(strlen(str
) + 1);
834 if (!astr
|| !strcpy(astr
, str
) ||
835 (delim
= strchr(astr
, '/')) == NULL
|| (*(delim
++) = 0) ||
836 sscanf(delim
, "%i", &l
) == 0 || l
> 128 ||
837 inet_pton(AF_INET6
, astr
, &iface
->pio_filter_addr
) == 0)
838 iface
->pio_filter_length
= 0;
840 iface
->pio_filter_length
= l
;
849 close_interface(iface
);
853 static int set_interface(struct uci_section
*s
)
855 blob_buf_init(&b
, 0);
856 uci_to_blob(&b
, s
, &interface_attr_list
);
858 return config_parse_interface(blob_data(b
.head
), blob_len(b
.head
), s
->e
.name
, true);
861 static void lease_delete_assignments(struct lease
*l
, bool v6
)
863 struct dhcp_assignment
*a
, *tmp
;
864 unsigned int flag
= v6
? OAF_DHCPV6
: OAF_DHCPV4
;
866 list_for_each_entry_safe(a
, tmp
, &l
->assignments
, lease_list
) {
872 static void lease_update_assignments(struct lease
*l
)
874 struct dhcp_assignment
*a
;
876 list_for_each_entry(a
, &l
->assignments
, lease_list
) {
882 a
->hostname
= strdup(l
->hostname
);
884 a
->leasetime
= l
->leasetime
;
888 static int lease_cmp(const void *k1
, const void *k2
, _unused
void *ptr
)
890 const struct lease
*l1
= k1
, *l2
= k2
;
893 if (l1
->duid_len
!= l2
->duid_len
)
894 return l1
->duid_len
- l2
->duid_len
;
896 if (l1
->duid_len
&& l2
->duid_len
)
897 cmp
= memcmp(l1
->duid
, l2
->duid
, l1
->duid_len
);
902 return memcmp(l1
->mac
.ether_addr_octet
, l2
->mac
.ether_addr_octet
,
903 sizeof(l1
->mac
.ether_addr_octet
));
906 static void lease_change_config(struct lease
*l_old
, struct lease
*l_new
)
910 if ((!!l_new
->hostname
!= !!l_old
->hostname
) ||
911 (l_new
->hostname
&& strcmp(l_new
->hostname
, l_old
->hostname
))) {
912 free(l_old
->hostname
);
913 l_old
->hostname
= NULL
;
916 l_old
->hostname
= strdup(l_new
->hostname
);
921 if (l_old
->leasetime
!= l_new
->leasetime
) {
922 l_old
->leasetime
= l_new
->leasetime
;
926 if (l_old
->ipaddr
!= l_new
->ipaddr
) {
927 l_old
->ipaddr
= l_new
->ipaddr
;
928 lease_delete_assignments(l_old
, false);
931 if (l_old
->hostid
!= l_new
->hostid
) {
932 l_old
->hostid
= l_new
->hostid
;
933 lease_delete_assignments(l_old
, true);
937 lease_update_assignments(l_old
);
942 static void lease_delete(struct lease
*l
)
944 struct dhcp_assignment
*a
, *tmp
;
946 list_for_each_entry_safe(a
, tmp
, &l
->assignments
, lease_list
)
952 static void lease_update(_unused
struct vlist_tree
*tree
, struct vlist_node
*node_new
,
953 struct vlist_node
*node_old
)
955 struct lease
*lease_new
= container_of(node_new
, struct lease
, node
);
956 struct lease
*lease_old
= container_of(node_old
, struct lease
, node
);
958 if (node_old
&& node_new
)
959 lease_change_config(lease_old
, lease_new
);
961 lease_delete(lease_old
);
964 struct lease
*config_find_lease_by_duid(const uint8_t *duid
, const uint16_t len
)
968 vlist_for_each_element(&leases
, l
, node
) {
969 if (l
->duid_len
== len
&& !memcmp(l
->duid
, duid
, len
))
976 struct lease
*config_find_lease_by_mac(const uint8_t *mac
)
980 vlist_for_each_element(&leases
, l
, node
) {
981 if (!memcmp(l
->mac
.ether_addr_octet
, mac
,
982 sizeof(l
->mac
.ether_addr_octet
)))
989 struct lease
*config_find_lease_by_hostid(const uint32_t hostid
)
993 vlist_for_each_element(&leases
, l
, node
) {
994 if (l
->hostid
== hostid
)
1001 struct lease
*config_find_lease_by_ipaddr(const uint32_t ipaddr
)
1005 vlist_for_each_element(&leases
, l
, node
) {
1006 if (l
->ipaddr
== ipaddr
)
1013 void odhcpd_reload(void)
1015 struct uci_context
*uci
= uci_alloc_context();
1016 struct interface
*master
= NULL
, *i
, *tmp
;
1021 vlist_update(&leases
);
1022 avl_for_each_element(&interfaces
, i
, avl
)
1025 struct uci_package
*dhcp
= NULL
;
1026 if (!uci_load(uci
, "dhcp", &dhcp
)) {
1027 struct uci_element
*e
;
1029 /* 1. Global settings */
1030 uci_foreach_element(&dhcp
->sections
, e
) {
1031 struct uci_section
*s
= uci_to_section(e
);
1032 if (!strcmp(s
->type
, "odhcpd"))
1037 uci_foreach_element(&dhcp
->sections
, e
) {
1038 struct uci_section
*s
= uci_to_section(e
);
1039 if (!strcmp(s
->type
, "dhcp"))
1043 /* 3. Static leases */
1044 uci_foreach_element(&dhcp
->sections
, e
) {
1045 struct uci_section
* s
= uci_to_section(e
);
1046 if (!strcmp(s
->type
, "host"))
1047 set_lease_from_uci(s
);
1051 if (config
.dhcp_statefile
) {
1052 char *path
= strdup(config
.dhcp_statefile
);
1054 mkdir_p(dirname(path
), 0755);
1058 vlist_flush(&leases
);
1061 ubus_apply_network();
1064 bool any_dhcpv6_slave
= false, any_ra_slave
= false, any_ndp_slave
= false;
1067 avl_for_each_element(&interfaces
, i
, avl
) {
1071 if (i
->dhcpv6
== MODE_HYBRID
|| i
->dhcpv6
== MODE_RELAY
)
1072 any_dhcpv6_slave
= true;
1074 if (i
->ra
== MODE_HYBRID
|| i
->ra
== MODE_RELAY
)
1075 any_ra_slave
= true;
1077 if (i
->ndp
== MODE_HYBRID
|| i
->ndp
== MODE_RELAY
)
1078 any_ndp_slave
= true;
1081 /* Evaluate hybrid mode for master */
1082 avl_for_each_element(&interfaces
, i
, avl
) {
1086 enum odhcpd_mode hybrid_mode
= MODE_DISABLED
;
1088 if (!ubus_has_prefix(i
->name
, i
->ifname
))
1089 hybrid_mode
= MODE_RELAY
;
1092 if (i
->dhcpv6
== MODE_HYBRID
)
1093 i
->dhcpv6
= hybrid_mode
;
1095 if (i
->dhcpv6
== MODE_RELAY
&& !any_dhcpv6_slave
)
1096 i
->dhcpv6
= MODE_DISABLED
;
1098 if (i
->ra
== MODE_HYBRID
)
1099 i
->ra
= hybrid_mode
;
1101 if (i
->ra
== MODE_RELAY
&& !any_ra_slave
)
1102 i
->ra
= MODE_DISABLED
;
1104 if (i
->ndp
== MODE_HYBRID
)
1105 i
->ndp
= hybrid_mode
;
1107 if (i
->ndp
== MODE_RELAY
&& !any_ndp_slave
)
1108 i
->ndp
= MODE_DISABLED
;
1110 if (i
->dhcpv6
== MODE_RELAY
|| i
->ra
== MODE_RELAY
|| i
->ndp
== MODE_RELAY
)
1115 avl_for_each_element_safe(&interfaces
, i
, avl
, tmp
) {
1117 /* Resolve hybrid mode */
1118 if (i
->dhcpv6
== MODE_HYBRID
)
1119 i
->dhcpv6
= (master
&& master
->dhcpv6
== MODE_RELAY
) ?
1120 MODE_RELAY
: MODE_SERVER
;
1122 if (i
->ra
== MODE_HYBRID
)
1123 i
->ra
= (master
&& master
->ra
== MODE_RELAY
) ?
1124 MODE_RELAY
: MODE_SERVER
;
1126 if (i
->ndp
== MODE_HYBRID
)
1127 i
->ndp
= (master
&& master
->ndp
== MODE_RELAY
) ?
1128 MODE_RELAY
: MODE_DISABLED
;
1130 router_setup_interface(i
, i
->ra
!= MODE_DISABLED
);
1131 dhcpv6_setup_interface(i
, i
->dhcpv6
!= MODE_DISABLED
);
1132 ndp_setup_interface(i
, i
->ndp
!= MODE_DISABLED
);
1133 #ifdef DHCPV4_SUPPORT
1134 dhcpv4_setup_interface(i
, i
->dhcpv4
!= MODE_DISABLED
);
1140 uci_unload(uci
, dhcp
);
1141 uci_free_context(uci
);
1144 static void handle_signal(int signal
)
1148 if (signal
== SIGHUP
) {
1149 if (write(reload_pipe
[1], b
, sizeof(b
)) < 0) {}
1154 static void reload_cb(struct uloop_fd
*u
, _unused
unsigned int events
)
1157 if (read(u
->fd
, b
, sizeof(b
)) < 0) {}
1162 static struct uloop_fd reload_fd
= { .fd
= -1, .cb
= reload_cb
};
1164 void odhcpd_run(void)
1166 if (pipe2(reload_pipe
, O_NONBLOCK
| O_CLOEXEC
) < 0) {}
1168 reload_fd
.fd
= reload_pipe
[0];
1169 uloop_fd_add(&reload_fd
, ULOOP_READ
);
1171 signal(SIGTERM
, handle_signal
);
1172 signal(SIGINT
, handle_signal
);
1173 signal(SIGHUP
, handle_signal
);