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
: "");
48 blobmsg_add_u8(&b
, "accept-reconf-nonce", c
->accept_fr_nonce
);
50 m
= blobmsg_open_array(&b
, "flags");
51 if (c
->flags
& OAF_BOUND
)
52 blobmsg_add_string(&b
, NULL
, "bound");
54 if (c
->flags
& OAF_STATIC
)
55 blobmsg_add_string(&b
, NULL
, "static");
56 blobmsg_close_array(&b
, m
);
58 buf
= blobmsg_alloc_string_buffer(&b
, "ip", INET_ADDRSTRLEN
);
59 struct in_addr addr
= {.s_addr
= c
->addr
};
60 inet_ntop(AF_INET
, &addr
, buf
, INET_ADDRSTRLEN
);
61 blobmsg_add_string_buffer(&b
);
63 blobmsg_add_u32(&b
, "valid", INFINITE_VALID(c
->valid_until
) ?
64 (uint32_t)-1 : (uint32_t)(c
->valid_until
- now
));
66 blobmsg_close_table(&b
, l
);
69 blobmsg_close_array(&b
, j
);
70 blobmsg_close_table(&b
, i
);
73 blobmsg_close_table(&b
, a
);
74 ubus_send_reply(ctx
, req
, b
.head
);
79 static void dhcpv6_blobmsg_ia_addr(struct in6_addr
*addr
, int prefix
, uint32_t pref
,
80 uint32_t valid
, _unused
void *arg
)
82 void *a
= blobmsg_open_table(&b
, NULL
);
83 char *buf
= blobmsg_alloc_string_buffer(&b
, NULL
, INET6_ADDRSTRLEN
);
85 inet_ntop(AF_INET6
, addr
, buf
, INET6_ADDRSTRLEN
);
86 blobmsg_add_string_buffer(&b
);
87 blobmsg_add_u32(&b
, "preferred-lifetime",
88 pref
== UINT32_MAX
? (uint32_t)-1 : pref
);
89 blobmsg_add_u32(&b
, "valid-lifetime",
90 valid
== UINT32_MAX
? (uint32_t)-1 : valid
);
93 blobmsg_add_u32(&b
, "prefix-length", prefix
);
95 blobmsg_close_table(&b
, a
);
98 static int handle_dhcpv6_leases(_unused
struct ubus_context
*ctx
, _unused
struct ubus_object
*obj
,
99 _unused
struct ubus_request_data
*req
, _unused
const char *method
,
100 _unused
struct blob_attr
*msg
)
102 struct interface
*iface
;
103 time_t now
= odhcpd_time();
106 blob_buf_init(&b
, 0);
107 a
= blobmsg_open_table(&b
, "device");
109 list_for_each_entry(iface
, &interfaces
, head
) {
110 if (iface
->dhcpv6
!= MODE_SERVER
|| iface
->ia_assignments
.next
== NULL
)
113 void *i
= blobmsg_open_table(&b
, iface
->ifname
);
114 void *j
= blobmsg_open_array(&b
, "leases");
116 struct dhcpv6_assignment
*a
, *border
= list_last_entry(
117 &iface
->ia_assignments
, struct dhcpv6_assignment
, head
);
119 list_for_each_entry(a
, &iface
->ia_assignments
, head
) {
120 if (a
== border
|| (!INFINITE_VALID(a
->valid_until
) &&
121 a
->valid_until
< now
))
124 void *m
, *l
= blobmsg_open_table(&b
, NULL
);
125 char *buf
= blobmsg_alloc_string_buffer(&b
, "duid", 264);
127 odhcpd_hexlify(buf
, a
->clid_data
, a
->clid_len
);
128 blobmsg_add_string_buffer(&b
);
130 blobmsg_add_u32(&b
, "iaid", ntohl(a
->iaid
));
131 blobmsg_add_string(&b
, "hostname", (a
->hostname
) ? a
->hostname
: "");
132 blobmsg_add_u8(&b
, "accept-reconf", a
->accept_reconf
);
133 blobmsg_add_u32(&b
, "assigned", a
->assigned
);
135 m
= blobmsg_open_array(&b
, "flags");
136 if (a
->flags
& OAF_BOUND
)
137 blobmsg_add_string(&b
, NULL
, "bound");
139 if (a
->flags
& OAF_STATIC
)
140 blobmsg_add_string(&b
, NULL
, "static");
141 blobmsg_close_array(&b
, m
);
143 m
= blobmsg_open_array(&b
, a
->length
== 128 ? "ipv6-addr": "ipv6-prefix");
144 dhcpv6_enum_ia_addrs(iface
, a
, now
, dhcpv6_blobmsg_ia_addr
, NULL
);
145 blobmsg_close_table(&b
, m
);
147 blobmsg_add_u32(&b
, "valid", INFINITE_VALID(a
->valid_until
) ?
148 (uint32_t)-1 : (uint32_t)(a
->valid_until
- now
));
150 blobmsg_close_table(&b
, l
);
153 blobmsg_close_array(&b
, j
);
154 blobmsg_close_table(&b
, i
);
157 blobmsg_close_table(&b
, a
);
158 ubus_send_reply(ctx
, req
, b
.head
);
163 static struct ubus_method main_object_methods
[] = {
164 {.name
= "ipv4leases", .handler
= handle_dhcpv4_leases
},
165 {.name
= "ipv6leases", .handler
= handle_dhcpv6_leases
},
168 static struct ubus_object_type main_object_type
=
169 UBUS_OBJECT_TYPE("dhcp", main_object_methods
);
171 static struct ubus_object main_object
= {
173 .type
= &main_object_type
,
174 .methods
= main_object_methods
,
175 .n_methods
= ARRAY_SIZE(main_object_methods
),
184 static const struct blobmsg_policy dump_attrs
[DUMP_ATTR_MAX
] = {
185 [DUMP_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_ARRAY
},
190 IFACE_ATTR_INTERFACE
,
199 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
200 [IFACE_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
201 [IFACE_ATTR_IFNAME
] = { .name
= "l3_device", .type
= BLOBMSG_TYPE_STRING
},
202 [IFACE_ATTR_UP
] = { .name
= "up", .type
= BLOBMSG_TYPE_BOOL
},
203 [IFACE_ATTR_DATA
] = { .name
= "data", .type
= BLOBMSG_TYPE_TABLE
},
204 [IFACE_ATTR_PREFIX
] = { .name
= "ipv6-prefix", .type
= BLOBMSG_TYPE_ARRAY
},
205 [IFACE_ATTR_ADDRESS
] = { .name
= "ipv6-address", .type
= BLOBMSG_TYPE_ARRAY
},
208 static void handle_dump(_unused
struct ubus_request
*req
, _unused
int type
, struct blob_attr
*msg
)
210 struct blob_attr
*tb
[DUMP_ATTR_MAX
];
211 blobmsg_parse(dump_attrs
, DUMP_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
213 if (!tb
[DUMP_ATTR_INTERFACE
])
217 dump
= blob_memdup(tb
[DUMP_ATTR_INTERFACE
]);
222 static void update_netifd(bool subscribe
)
225 ubus_subscribe(ubus
, &netifd
, objid
);
227 ubus_abort_request(ubus
, &req_dump
);
228 if (!ubus_invoke_async(ubus
, objid
, "dump", NULL
, &req_dump
)) {
229 req_dump
.data_cb
= handle_dump
;
230 ubus_complete_request_async(ubus
, &req_dump
);
235 static int handle_update(_unused
struct ubus_context
*ctx
, _unused
struct ubus_object
*obj
,
236 _unused
struct ubus_request_data
*req
, _unused
const char *method
,
237 struct blob_attr
*msg
)
239 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
240 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
242 const char *interface
= (tb
[IFACE_ATTR_INTERFACE
]) ?
243 blobmsg_get_string(tb
[IFACE_ATTR_INTERFACE
]) : "";
244 const char *ifname
= (tb
[IFACE_ATTR_IFNAME
]) ?
245 blobmsg_get_string(tb
[IFACE_ATTR_IFNAME
]) : "";
247 struct interface
*c
, *iface
= NULL
;
248 list_for_each_entry(c
, &interfaces
, head
)
249 if (!strcmp(interface
, c
->name
) || !strcmp(ifname
, c
->ifname
))
252 if (iface
&& iface
->ignore
)
255 update_netifd(false);
260 void ubus_apply_network(void)
268 blobmsg_for_each_attr(a
, dump
, rem
) {
269 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
270 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
, blobmsg_data(a
), blobmsg_data_len(a
));
272 if (!tb
[IFACE_ATTR_INTERFACE
] || !tb
[IFACE_ATTR_DATA
])
275 const char *interface
= (tb
[IFACE_ATTR_INTERFACE
]) ?
276 blobmsg_get_string(tb
[IFACE_ATTR_INTERFACE
]) : "";
277 const char *ifname
= (tb
[IFACE_ATTR_IFNAME
]) ?
278 blobmsg_get_string(tb
[IFACE_ATTR_IFNAME
]) : "";
280 bool matched
= false;
281 struct interface
*c
, *n
;
282 list_for_each_entry_safe(c
, n
, &interfaces
, head
) {
283 char *f
= memmem(c
->upstream
, c
->upstream_len
,
284 interface
, strlen(interface
) + 1);
285 bool cmatched
= !strcmp(interface
, c
->name
) || !strcmp(ifname
, c
->ifname
);
288 if (!cmatched
&& (!c
->upstream_len
|| !f
|| (f
!= c
->upstream
&& f
[-1] != 0)))
292 config_parse_interface(blobmsg_data(tb
[IFACE_ATTR_DATA
]),
293 blobmsg_data_len(tb
[IFACE_ATTR_DATA
]), c
->name
, false);
297 config_parse_interface(blobmsg_data(tb
[IFACE_ATTR_DATA
]),
298 blobmsg_data_len(tb
[IFACE_ATTR_DATA
]), interface
, false);
309 static const struct blobmsg_policy obj_attrs
[OBJ_ATTR_MAX
] = {
310 [OBJ_ATTR_ID
] = { .name
= "id", .type
= BLOBMSG_TYPE_INT32
},
311 [OBJ_ATTR_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
314 void ubus_bcast_dhcp_event(const char *type
, const uint8_t *mac
,
315 const size_t mlen
, const struct in_addr
*addr
, const char *name
,
316 const char *interface
)
318 if (!ubus
|| !main_object
.has_subscribers
)
321 blob_buf_init(&b
, 0);
323 blobmsg_add_string(&b
, "mac", odhcpd_print_mac(mac
, mlen
));
325 blobmsg_add_string(&b
, "ip", inet_ntoa(*addr
));
327 blobmsg_add_string(&b
, "name", name
);
329 blobmsg_add_string(&b
, "interface", interface
);
331 ubus_notify(ubus
, &main_object
, type
, b
.head
, -1);
334 static void handle_event(_unused
struct ubus_context
*ctx
, _unused
struct ubus_event_handler
*ev
,
335 _unused
const char *type
, struct blob_attr
*msg
)
337 struct blob_attr
*tb
[OBJ_ATTR_MAX
];
338 blobmsg_parse(obj_attrs
, OBJ_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
340 if (!tb
[OBJ_ATTR_ID
] || !tb
[OBJ_ATTR_PATH
])
343 if (strcmp(blobmsg_get_string(tb
[OBJ_ATTR_PATH
]), "network.interface"))
346 objid
= blobmsg_get_u32(tb
[OBJ_ATTR_ID
]);
350 static struct ubus_event_handler event_handler
= { .cb
= handle_event
};
353 const char* ubus_get_ifname(const char *name
)
361 blobmsg_for_each_attr(c
, dump
, rem
) {
362 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
363 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
, blobmsg_data(c
), blobmsg_data_len(c
));
365 if (!tb
[IFACE_ATTR_INTERFACE
] || strcmp(name
,
366 blobmsg_get_string(tb
[IFACE_ATTR_INTERFACE
])))
369 if (tb
[IFACE_ATTR_IFNAME
])
370 return blobmsg_get_string(tb
[IFACE_ATTR_IFNAME
]);
377 bool ubus_has_prefix(const char *name
, const char *ifname
)
379 struct blob_attr
*c
, *cur
;
385 blobmsg_for_each_attr(c
, dump
, rem
) {
386 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
387 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
, blobmsg_data(c
), blobmsg_data_len(c
));
389 if (!tb
[IFACE_ATTR_INTERFACE
] || !tb
[IFACE_ATTR_IFNAME
])
392 if (strcmp(name
, blobmsg_get_string(tb
[IFACE_ATTR_INTERFACE
])) ||
393 strcmp(ifname
, blobmsg_get_string(tb
[IFACE_ATTR_IFNAME
])))
396 if ((cur
= tb
[IFACE_ATTR_PREFIX
])) {
397 if (blobmsg_type(cur
) != BLOBMSG_TYPE_ARRAY
|| !blobmsg_check_attr(cur
, false))
402 blobmsg_for_each_attr(d
, cur
, drem
) {
414 if (!(ubus
= ubus_connect(NULL
))) {
415 syslog(LOG_ERR
, "Unable to connect to ubus: %s", strerror(errno
));
419 netifd
.cb
= handle_update
;
420 ubus_register_subscriber(ubus
, &netifd
);
422 ubus_add_uloop(ubus
);
423 ubus_add_object(ubus
, &main_object
);
424 ubus_register_event_handler(ubus
, &event_handler
, "ubus.object.add");
425 if (!ubus_lookup_id(ubus
, "network.interface", &objid
))