3 #include <libubox/uloop.h>
4 #include <netinet/in.h>
11 static struct ubus_context
*ubus
= NULL
;
12 static struct ubus_subscriber netifd
;
13 static struct blob_buf b
;
14 static struct blob_attr
*dump
= NULL
;
15 static uint32_t objid
= 0;
16 static struct ubus_request req_dump
= { .list
= LIST_HEAD_INIT(req_dump
.list
) };
18 static int handle_dhcpv4_leases(struct ubus_context
*ctx
, _unused
struct ubus_object
*obj
,
19 struct ubus_request_data
*req
, _unused
const char *method
,
20 _unused
struct blob_attr
*msg
)
22 struct interface
*iface
;
23 time_t now
= odhcpd_time();
27 a
= blobmsg_open_table(&b
, "device");
29 list_for_each_entry(iface
, &interfaces
, head
) {
30 if (iface
->dhcpv4
!= MODE_SERVER
|| iface
->dhcpv4_assignments
.next
== NULL
)
33 void *i
= blobmsg_open_table(&b
, iface
->ifname
);
34 void *j
= blobmsg_open_array(&b
, "leases");
36 struct dhcpv4_assignment
*c
;
37 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
38 if (!INFINITE_VALID(c
->valid_until
) && c
->valid_until
< now
)
41 void *m
, *l
= blobmsg_open_table(&b
, NULL
);
42 char *buf
= blobmsg_alloc_string_buffer(&b
, "mac", 13);
44 odhcpd_hexlify(buf
, c
->hwaddr
, sizeof(c
->hwaddr
));
45 blobmsg_add_string_buffer(&b
);
47 blobmsg_add_string(&b
, "hostname", (c
->hostname
) ? c
->hostname
: "");
49 m
= blobmsg_open_array(&b
, "flags");
50 if (c
->flags
& OAF_BOUND
)
51 blobmsg_add_string(&b
, NULL
, "bound");
53 if (c
->flags
& OAF_STATIC
)
54 blobmsg_add_string(&b
, NULL
, "static");
55 blobmsg_close_array(&b
, m
);
57 buf
= blobmsg_alloc_string_buffer(&b
, "ip", INET_ADDRSTRLEN
);
58 struct in_addr addr
= {htonl(c
->addr
)};
59 inet_ntop(AF_INET
, &addr
, buf
, INET_ADDRSTRLEN
);
60 blobmsg_add_string_buffer(&b
);
62 blobmsg_add_u32(&b
, "valid", INFINITE_VALID(c
->valid_until
) ?
63 (uint32_t)-1 : (uint32_t)(c
->valid_until
- now
));
65 blobmsg_close_table(&b
, l
);
68 blobmsg_close_array(&b
, j
);
69 blobmsg_close_table(&b
, i
);
72 blobmsg_close_table(&b
, a
);
73 ubus_send_reply(ctx
, req
, b
.head
);
78 static void dhcpv6_blobmsg_ia_addr(struct in6_addr
*addr
, int prefix
, uint32_t pref
,
79 uint32_t valid
, _unused
void *arg
)
81 void *a
= blobmsg_open_table(&b
, NULL
);
82 char *buf
= blobmsg_alloc_string_buffer(&b
, NULL
, INET6_ADDRSTRLEN
);
84 inet_ntop(AF_INET6
, addr
, buf
, INET6_ADDRSTRLEN
);
85 blobmsg_add_string_buffer(&b
);
86 blobmsg_add_u32(&b
, "preferred-lifetime",
87 pref
== UINT32_MAX
? (uint32_t)-1 : pref
);
88 blobmsg_add_u32(&b
, "valid-lifetime",
89 valid
== UINT32_MAX
? (uint32_t)-1 : valid
);
92 blobmsg_add_u32(&b
, "prefix-length", prefix
);
94 blobmsg_close_table(&b
, a
);
97 static int handle_dhcpv6_leases(_unused
struct ubus_context
*ctx
, _unused
struct ubus_object
*obj
,
98 _unused
struct ubus_request_data
*req
, _unused
const char *method
,
99 _unused
struct blob_attr
*msg
)
101 struct interface
*iface
;
102 time_t now
= odhcpd_time();
105 blob_buf_init(&b
, 0);
106 a
= blobmsg_open_table(&b
, "device");
108 list_for_each_entry(iface
, &interfaces
, head
) {
109 if (iface
->dhcpv6
!= MODE_SERVER
|| iface
->ia_assignments
.next
== NULL
)
112 void *i
= blobmsg_open_table(&b
, iface
->ifname
);
113 void *j
= blobmsg_open_array(&b
, "leases");
115 struct dhcpv6_assignment
*a
, *border
= list_last_entry(
116 &iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
118 list_for_each_entry(a
, &iface
->ia_assignments
, head
) {
119 if (a
== border
|| (!INFINITE_VALID(a
->valid_until
) &&
120 a
->valid_until
< now
))
123 void *m
, *l
= blobmsg_open_table(&b
, NULL
);
124 char *buf
= blobmsg_alloc_string_buffer(&b
, "duid", 264);
126 odhcpd_hexlify(buf
, a
->clid_data
, a
->clid_len
);
127 blobmsg_add_string_buffer(&b
);
129 blobmsg_add_u32(&b
, "iaid", ntohl(a
->iaid
));
130 blobmsg_add_string(&b
, "hostname", (a
->hostname
) ? a
->hostname
: "");
131 blobmsg_add_u8(&b
, "accept-reconf", a
->accept_reconf
);
132 blobmsg_add_u32(&b
, "assigned", a
->assigned
);
134 m
= blobmsg_open_array(&b
, "flags");
135 if (a
->flags
& OAF_BOUND
)
136 blobmsg_add_string(&b
, NULL
, "bound");
138 if (a
->flags
& OAF_STATIC
)
139 blobmsg_add_string(&b
, NULL
, "static");
140 blobmsg_close_array(&b
, m
);
142 m
= blobmsg_open_array(&b
, a
->length
== 128 ? "ipv6-addr": "ipv6-prefix");
143 dhcpv6_enum_ia_addrs(iface
, a
, now
, dhcpv6_blobmsg_ia_addr
, NULL
);
144 blobmsg_close_table(&b
, m
);
146 blobmsg_add_u32(&b
, "valid", INFINITE_VALID(a
->valid_until
) ?
147 (uint32_t)-1 : (uint32_t)(a
->valid_until
- now
));
149 blobmsg_close_table(&b
, l
);
152 blobmsg_close_array(&b
, j
);
153 blobmsg_close_table(&b
, i
);
156 blobmsg_close_table(&b
, a
);
157 ubus_send_reply(ctx
, req
, b
.head
);
162 static struct ubus_method main_object_methods
[] = {
163 {.name
= "ipv4leases", .handler
= handle_dhcpv4_leases
},
164 {.name
= "ipv6leases", .handler
= handle_dhcpv6_leases
},
167 static struct ubus_object_type main_object_type
=
168 UBUS_OBJECT_TYPE("dhcp", main_object_methods
);
170 static struct ubus_object main_object
= {
172 .type
= &main_object_type
,
173 .methods
= main_object_methods
,
174 .n_methods
= ARRAY_SIZE(main_object_methods
),
183 static const struct blobmsg_policy dump_attrs
[DUMP_ATTR_MAX
] = {
184 [DUMP_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_ARRAY
},
189 IFACE_ATTR_INTERFACE
,
198 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
199 [IFACE_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
200 [IFACE_ATTR_IFNAME
] = { .name
= "l3_device", .type
= BLOBMSG_TYPE_STRING
},
201 [IFACE_ATTR_UP
] = { .name
= "up", .type
= BLOBMSG_TYPE_BOOL
},
202 [IFACE_ATTR_DATA
] = { .name
= "data", .type
= BLOBMSG_TYPE_TABLE
},
203 [IFACE_ATTR_PREFIX
] = { .name
= "ipv6-prefix", .type
= BLOBMSG_TYPE_ARRAY
},
204 [IFACE_ATTR_ADDRESS
] = { .name
= "ipv6-address", .type
= BLOBMSG_TYPE_ARRAY
},
207 static void handle_dump(_unused
struct ubus_request
*req
, _unused
int type
, struct blob_attr
*msg
)
209 struct blob_attr
*tb
[DUMP_ATTR_MAX
];
210 blobmsg_parse(dump_attrs
, DUMP_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
212 if (!tb
[DUMP_ATTR_INTERFACE
])
216 dump
= blob_memdup(tb
[DUMP_ATTR_INTERFACE
]);
221 static void update_netifd(bool subscribe
)
224 ubus_subscribe(ubus
, &netifd
, objid
);
226 ubus_abort_request(ubus
, &req_dump
);
227 if (!ubus_invoke_async(ubus
, objid
, "dump", NULL
, &req_dump
)) {
228 req_dump
.data_cb
= handle_dump
;
229 ubus_complete_request_async(ubus
, &req_dump
);
234 static int handle_update(_unused
struct ubus_context
*ctx
, _unused
struct ubus_object
*obj
,
235 _unused
struct ubus_request_data
*req
, _unused
const char *method
,
236 struct blob_attr
*msg
)
238 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
239 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
241 const char *interface
= (tb
[IFACE_ATTR_INTERFACE
]) ?
242 blobmsg_get_string(tb
[IFACE_ATTR_INTERFACE
]) : "";
243 const char *ifname
= (tb
[IFACE_ATTR_IFNAME
]) ?
244 blobmsg_get_string(tb
[IFACE_ATTR_IFNAME
]) : "";
246 struct interface
*c
, *iface
= NULL
;
247 list_for_each_entry(c
, &interfaces
, head
)
248 if (!strcmp(interface
, c
->name
) || !strcmp(ifname
, c
->ifname
))
251 if (iface
&& iface
->ignore
)
254 update_netifd(false);
259 void ubus_apply_network(void)
267 blobmsg_for_each_attr(a
, dump
, rem
) {
268 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
269 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
, blobmsg_data(a
), blobmsg_data_len(a
));
271 if (!tb
[IFACE_ATTR_INTERFACE
] || !tb
[IFACE_ATTR_DATA
])
274 const char *interface
= (tb
[IFACE_ATTR_INTERFACE
]) ?
275 blobmsg_get_string(tb
[IFACE_ATTR_INTERFACE
]) : "";
276 const char *ifname
= (tb
[IFACE_ATTR_IFNAME
]) ?
277 blobmsg_get_string(tb
[IFACE_ATTR_IFNAME
]) : "";
279 bool matched
= false;
280 struct interface
*c
, *n
;
281 list_for_each_entry_safe(c
, n
, &interfaces
, head
) {
282 char *f
= memmem(c
->upstream
, c
->upstream_len
,
283 interface
, strlen(interface
) + 1);
284 bool cmatched
= !strcmp(interface
, c
->name
) || !strcmp(ifname
, c
->ifname
);
287 if (!cmatched
&& (!c
->upstream_len
|| !f
|| (f
!= c
->upstream
&& f
[-1] != 0)))
291 config_parse_interface(blobmsg_data(tb
[IFACE_ATTR_DATA
]),
292 blobmsg_data_len(tb
[IFACE_ATTR_DATA
]), c
->name
, false);
296 config_parse_interface(blobmsg_data(tb
[IFACE_ATTR_DATA
]),
297 blobmsg_data_len(tb
[IFACE_ATTR_DATA
]), interface
, false);
308 static const struct blobmsg_policy obj_attrs
[OBJ_ATTR_MAX
] = {
309 [OBJ_ATTR_ID
] = { .name
= "id", .type
= BLOBMSG_TYPE_INT32
},
310 [OBJ_ATTR_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
314 static void handle_event(_unused
struct ubus_context
*ctx
, _unused
struct ubus_event_handler
*ev
,
315 _unused
const char *type
, struct blob_attr
*msg
)
317 struct blob_attr
*tb
[OBJ_ATTR_MAX
];
318 blobmsg_parse(obj_attrs
, OBJ_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
320 if (!tb
[OBJ_ATTR_ID
] || !tb
[OBJ_ATTR_PATH
])
323 if (strcmp(blobmsg_get_string(tb
[OBJ_ATTR_PATH
]), "network.interface"))
326 objid
= blobmsg_get_u32(tb
[OBJ_ATTR_ID
]);
330 static struct ubus_event_handler event_handler
= { .cb
= handle_event
};
333 const char* ubus_get_ifname(const char *name
)
341 blobmsg_for_each_attr(c
, dump
, rem
) {
342 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
343 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
, blobmsg_data(c
), blobmsg_data_len(c
));
345 if (!tb
[IFACE_ATTR_INTERFACE
] || strcmp(name
,
346 blobmsg_get_string(tb
[IFACE_ATTR_INTERFACE
])))
349 if (tb
[IFACE_ATTR_IFNAME
])
350 return blobmsg_get_string(tb
[IFACE_ATTR_IFNAME
]);
357 bool ubus_has_prefix(const char *name
, const char *ifname
)
359 struct blob_attr
*c
, *cur
;
365 blobmsg_for_each_attr(c
, dump
, rem
) {
366 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
367 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
, blobmsg_data(c
), blobmsg_data_len(c
));
369 if (!tb
[IFACE_ATTR_INTERFACE
] || !tb
[IFACE_ATTR_IFNAME
])
372 if (strcmp(name
, blobmsg_get_string(tb
[IFACE_ATTR_INTERFACE
])) ||
373 strcmp(ifname
, blobmsg_get_string(tb
[IFACE_ATTR_IFNAME
])))
376 if ((cur
= tb
[IFACE_ATTR_PREFIX
])) {
377 if (blobmsg_type(cur
) != BLOBMSG_TYPE_ARRAY
|| !blobmsg_check_attr(cur
, false))
382 blobmsg_for_each_attr(d
, cur
, drem
) {
394 if (!(ubus
= ubus_connect(NULL
))) {
395 syslog(LOG_ERR
, "Unable to connect to ubus: %s", strerror(errno
));
399 netifd
.cb
= handle_update
;
400 ubus_register_subscriber(ubus
, &netifd
);
402 ubus_add_uloop(ubus
);
403 ubus_add_object(ubus
, &main_object
);
404 ubus_register_event_handler(ubus
, &event_handler
, "ubus.object.add");
405 if (!ubus_lookup_id(ubus
, "network.interface", &objid
))