3 #include <libubox/uloop.h>
4 #include <netinet/in.h>
7 #include <libubox/utils.h>
13 static struct ubus_context
*ubus
= NULL
;
14 static struct ubus_subscriber netifd
;
15 static struct blob_buf b
;
16 static struct blob_attr
*dump
= NULL
;
17 static uint32_t objid
= 0;
18 static struct ubus_request req_dump
= { .list
= LIST_HEAD_INIT(req_dump
.list
) };
20 static int handle_dhcpv4_leases(struct ubus_context
*ctx
, _unused
struct ubus_object
*obj
,
21 struct ubus_request_data
*req
, _unused
const char *method
,
22 _unused
struct blob_attr
*msg
)
24 struct interface
*iface
;
25 time_t now
= odhcpd_time();
29 a
= blobmsg_open_table(&b
, "device");
31 avl_for_each_element(&interfaces
, iface
, avl
) {
32 if (iface
->dhcpv4
!= MODE_SERVER
|| iface
->dhcpv4_assignments
.next
== NULL
)
35 void *i
= blobmsg_open_table(&b
, iface
->ifname
);
36 void *j
= blobmsg_open_array(&b
, "leases");
38 struct dhcp_assignment
*c
;
39 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
40 if (!INFINITE_VALID(c
->valid_until
) && c
->valid_until
< now
)
43 void *m
, *l
= blobmsg_open_table(&b
, NULL
);
44 char *buf
= blobmsg_alloc_string_buffer(&b
, "mac", 13);
46 odhcpd_hexlify(buf
, c
->hwaddr
, sizeof(c
->hwaddr
));
47 blobmsg_add_string_buffer(&b
);
49 blobmsg_add_string(&b
, "hostname", (c
->hostname
) ? c
->hostname
: "");
50 blobmsg_add_u8(&b
, "accept-reconf-nonce", c
->accept_fr_nonce
);
52 m
= blobmsg_open_array(&b
, "flags");
53 if (c
->flags
& OAF_BOUND
)
54 blobmsg_add_string(&b
, NULL
, "bound");
56 if (c
->flags
& OAF_STATIC
)
57 blobmsg_add_string(&b
, NULL
, "static");
59 if (c
->flags
& OAF_BROKEN_HOSTNAME
)
60 blobmsg_add_string(&b
, NULL
, "broken-hostname");
61 blobmsg_close_array(&b
, m
);
63 buf
= blobmsg_alloc_string_buffer(&b
, "address", INET_ADDRSTRLEN
);
64 struct in_addr addr
= {.s_addr
= c
->addr
};
65 inet_ntop(AF_INET
, &addr
, buf
, INET_ADDRSTRLEN
);
66 blobmsg_add_string_buffer(&b
);
68 blobmsg_add_u32(&b
, "valid", INFINITE_VALID(c
->valid_until
) ?
69 (uint32_t)-1 : (uint32_t)(c
->valid_until
- now
));
71 blobmsg_close_table(&b
, l
);
74 blobmsg_close_array(&b
, j
);
75 blobmsg_close_table(&b
, i
);
78 blobmsg_close_table(&b
, a
);
79 ubus_send_reply(ctx
, req
, b
.head
);
84 static void dhcpv6_blobmsg_ia_addr(struct in6_addr
*addr
, int prefix
, uint32_t pref
,
85 uint32_t valid
, _unused
void *arg
)
87 void *a
= blobmsg_open_table(&b
, NULL
);
88 char *buf
= blobmsg_alloc_string_buffer(&b
, "address", INET6_ADDRSTRLEN
);
90 inet_ntop(AF_INET6
, addr
, buf
, INET6_ADDRSTRLEN
);
91 blobmsg_add_string_buffer(&b
);
92 blobmsg_add_u32(&b
, "preferred-lifetime",
93 pref
== UINT32_MAX
? (uint32_t)-1 : pref
);
94 blobmsg_add_u32(&b
, "valid-lifetime",
95 valid
== UINT32_MAX
? (uint32_t)-1 : valid
);
98 blobmsg_add_u32(&b
, "prefix-length", prefix
);
100 blobmsg_close_table(&b
, a
);
103 static int handle_dhcpv6_leases(_unused
struct ubus_context
*ctx
, _unused
struct ubus_object
*obj
,
104 _unused
struct ubus_request_data
*req
, _unused
const char *method
,
105 _unused
struct blob_attr
*msg
)
107 struct interface
*iface
;
108 time_t now
= odhcpd_time();
111 blob_buf_init(&b
, 0);
112 a
= blobmsg_open_table(&b
, "device");
114 avl_for_each_element(&interfaces
, iface
, avl
) {
115 if (iface
->dhcpv6
!= MODE_SERVER
|| iface
->ia_assignments
.next
== NULL
)
118 void *i
= blobmsg_open_table(&b
, iface
->ifname
);
119 void *j
= blobmsg_open_array(&b
, "leases");
121 struct dhcp_assignment
*a
, *border
= list_last_entry(
122 &iface
->ia_assignments
, struct dhcp_assignment
, head
);
124 list_for_each_entry(a
, &iface
->ia_assignments
, head
) {
125 if (a
== border
|| (!INFINITE_VALID(a
->valid_until
) &&
126 a
->valid_until
< now
))
129 void *m
, *l
= blobmsg_open_table(&b
, NULL
);
130 char *buf
= blobmsg_alloc_string_buffer(&b
, "duid", 264);
132 odhcpd_hexlify(buf
, a
->clid_data
, a
->clid_len
);
133 blobmsg_add_string_buffer(&b
);
135 blobmsg_add_u32(&b
, "iaid", ntohl(a
->iaid
));
136 blobmsg_add_string(&b
, "hostname", (a
->hostname
) ? a
->hostname
: "");
137 blobmsg_add_u8(&b
, "accept-reconf", a
->accept_reconf
);
138 blobmsg_add_u32(&b
, "assigned", a
->assigned
);
140 m
= blobmsg_open_array(&b
, "flags");
141 if (a
->flags
& OAF_BOUND
)
142 blobmsg_add_string(&b
, NULL
, "bound");
144 if (a
->flags
& OAF_STATIC
)
145 blobmsg_add_string(&b
, NULL
, "static");
146 blobmsg_close_array(&b
, m
);
148 m
= blobmsg_open_array(&b
, a
->length
== 128 ? "ipv6-addr": "ipv6-prefix");
149 dhcpv6_enum_ia_addrs(iface
, a
, now
, dhcpv6_blobmsg_ia_addr
, NULL
);
150 blobmsg_close_table(&b
, m
);
152 blobmsg_add_u32(&b
, "valid", INFINITE_VALID(a
->valid_until
) ?
153 (uint32_t)-1 : (uint32_t)(a
->valid_until
- now
));
155 blobmsg_close_table(&b
, l
);
158 blobmsg_close_array(&b
, j
);
159 blobmsg_close_table(&b
, i
);
162 blobmsg_close_table(&b
, a
);
163 ubus_send_reply(ctx
, req
, b
.head
);
168 static struct ubus_method main_object_methods
[] = {
169 {.name
= "ipv4leases", .handler
= handle_dhcpv4_leases
},
170 {.name
= "ipv6leases", .handler
= handle_dhcpv6_leases
},
173 static struct ubus_object_type main_object_type
=
174 UBUS_OBJECT_TYPE("dhcp", main_object_methods
);
176 static struct ubus_object main_object
= {
178 .type
= &main_object_type
,
179 .methods
= main_object_methods
,
180 .n_methods
= ARRAY_SIZE(main_object_methods
),
189 static const struct blobmsg_policy dump_attrs
[DUMP_ATTR_MAX
] = {
190 [DUMP_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_ARRAY
},
195 IFACE_ATTR_INTERFACE
,
204 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
205 [IFACE_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
206 [IFACE_ATTR_IFNAME
] = { .name
= "l3_device", .type
= BLOBMSG_TYPE_STRING
},
207 [IFACE_ATTR_UP
] = { .name
= "up", .type
= BLOBMSG_TYPE_BOOL
},
208 [IFACE_ATTR_DATA
] = { .name
= "data", .type
= BLOBMSG_TYPE_TABLE
},
209 [IFACE_ATTR_PREFIX
] = { .name
= "ipv6-prefix", .type
= BLOBMSG_TYPE_ARRAY
},
210 [IFACE_ATTR_ADDRESS
] = { .name
= "ipv6-address", .type
= BLOBMSG_TYPE_ARRAY
},
213 static void handle_dump(_unused
struct ubus_request
*req
, _unused
int type
, struct blob_attr
*msg
)
215 struct blob_attr
*tb
[DUMP_ATTR_MAX
];
216 blobmsg_parse(dump_attrs
, DUMP_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
218 if (!tb
[DUMP_ATTR_INTERFACE
])
222 dump
= blob_memdup(tb
[DUMP_ATTR_INTERFACE
]);
227 static void update_netifd(bool subscribe
)
230 ubus_subscribe(ubus
, &netifd
, objid
);
232 ubus_abort_request(ubus
, &req_dump
);
233 blob_buf_init(&b
, 0);
235 if (!ubus_invoke_async(ubus
, objid
, "dump", b
.head
, &req_dump
)) {
236 req_dump
.data_cb
= handle_dump
;
237 ubus_complete_request_async(ubus
, &req_dump
);
242 static int handle_update(_unused
struct ubus_context
*ctx
, _unused
struct ubus_object
*obj
,
243 _unused
struct ubus_request_data
*req
, _unused
const char *method
,
244 struct blob_attr
*msg
)
246 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
250 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
251 const char *interface
= (tb
[IFACE_ATTR_INTERFACE
]) ?
252 blobmsg_get_string(tb
[IFACE_ATTR_INTERFACE
]) : "";
254 avl_for_each_element(&interfaces
, c
, avl
) {
255 if (!strcmp(interface
, c
->name
) && !c
->ignore
) {
262 update_netifd(false);
268 void ubus_apply_network(void)
276 blobmsg_for_each_attr(a
, dump
, rem
) {
277 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
278 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
, blobmsg_data(a
), blobmsg_data_len(a
));
280 if (!tb
[IFACE_ATTR_INTERFACE
] || !tb
[IFACE_ATTR_DATA
])
283 const char *interface
= (tb
[IFACE_ATTR_INTERFACE
]) ?
284 blobmsg_get_string(tb
[IFACE_ATTR_INTERFACE
]) : "";
286 bool matched
= false;
287 struct interface
*c
, *tmp
;
288 avl_for_each_element_safe(&interfaces
, c
, avl
, tmp
) {
289 char *f
= memmem(c
->upstream
, c
->upstream_len
,
290 interface
, strlen(interface
) + 1);
291 bool cmatched
= !strcmp(interface
, c
->name
);
294 if (!cmatched
&& (!c
->upstream_len
|| !f
|| (f
!= c
->upstream
&& f
[-1] != 0)))
298 config_parse_interface(blobmsg_data(tb
[IFACE_ATTR_DATA
]),
299 blobmsg_data_len(tb
[IFACE_ATTR_DATA
]), c
->name
, false);
303 config_parse_interface(blobmsg_data(tb
[IFACE_ATTR_DATA
]),
304 blobmsg_data_len(tb
[IFACE_ATTR_DATA
]), interface
, false);
315 static const struct blobmsg_policy obj_attrs
[OBJ_ATTR_MAX
] = {
316 [OBJ_ATTR_ID
] = { .name
= "id", .type
= BLOBMSG_TYPE_INT32
},
317 [OBJ_ATTR_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
320 void ubus_bcast_dhcp_event(const char *type
, const uint8_t *mac
,
321 const size_t mlen
, const struct in_addr
*addr
, const char *name
,
322 const char *interface
)
324 if (!ubus
|| !main_object
.has_subscribers
)
327 blob_buf_init(&b
, 0);
329 blobmsg_add_string(&b
, "mac", odhcpd_print_mac(mac
, mlen
));
331 blobmsg_add_string(&b
, "ip", inet_ntoa(*addr
));
333 blobmsg_add_string(&b
, "name", name
);
335 blobmsg_add_string(&b
, "interface", interface
);
337 ubus_notify(ubus
, &main_object
, type
, b
.head
, -1);
340 static void handle_event(_unused
struct ubus_context
*ctx
, _unused
struct ubus_event_handler
*ev
,
341 _unused
const char *type
, struct blob_attr
*msg
)
343 struct blob_attr
*tb
[OBJ_ATTR_MAX
];
344 blobmsg_parse(obj_attrs
, OBJ_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
346 if (!tb
[OBJ_ATTR_ID
] || !tb
[OBJ_ATTR_PATH
])
349 if (strcmp(blobmsg_get_string(tb
[OBJ_ATTR_PATH
]), "network.interface"))
352 objid
= blobmsg_get_u32(tb
[OBJ_ATTR_ID
]);
356 static struct ubus_event_handler event_handler
= { .cb
= handle_event
};
359 const char* ubus_get_ifname(const char *name
)
367 blobmsg_for_each_attr(c
, dump
, rem
) {
368 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
369 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
, blobmsg_data(c
), blobmsg_data_len(c
));
371 if (!tb
[IFACE_ATTR_INTERFACE
] || strcmp(name
,
372 blobmsg_get_string(tb
[IFACE_ATTR_INTERFACE
])))
375 if (tb
[IFACE_ATTR_IFNAME
])
376 return blobmsg_get_string(tb
[IFACE_ATTR_IFNAME
]);
383 bool ubus_has_prefix(const char *name
, const char *ifname
)
385 struct blob_attr
*c
, *cur
;
391 blobmsg_for_each_attr(c
, dump
, rem
) {
392 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
393 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
, blobmsg_data(c
), blobmsg_data_len(c
));
395 if (!tb
[IFACE_ATTR_INTERFACE
] || !tb
[IFACE_ATTR_IFNAME
])
398 if (strcmp(name
, blobmsg_get_string(tb
[IFACE_ATTR_INTERFACE
])) ||
399 strcmp(ifname
, blobmsg_get_string(tb
[IFACE_ATTR_IFNAME
])))
402 if ((cur
= tb
[IFACE_ATTR_PREFIX
])) {
403 if (blobmsg_type(cur
) != BLOBMSG_TYPE_ARRAY
|| !blobmsg_check_attr(cur
, false))
408 blobmsg_for_each_attr(d
, cur
, drem
) {
420 if (!(ubus
= ubus_connect(NULL
))) {
421 syslog(LOG_ERR
, "Unable to connect to ubus: %m");
425 netifd
.cb
= handle_update
;
426 ubus_register_subscriber(ubus
, &netifd
);
428 ubus_add_uloop(ubus
);
429 ubus_add_object(ubus
, &main_object
);
430 ubus_register_event_handler(ubus
, &event_handler
, "ubus.object.add");
431 if (!ubus_lookup_id(ubus
, "network.interface", &objid
))