3 #include <libubox/uloop.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
) };
19 static int handle_dhcpv4_leases(struct ubus_context
*ctx
, _unused
struct ubus_object
*obj
,
20 struct ubus_request_data
*req
, _unused
const char *method
,
21 _unused
struct blob_attr
*msg
)
24 void *a
= blobmsg_open_table(&b
, "device");
25 time_t now
= odhcpd_time();
27 struct interface
*iface
;
28 list_for_each_entry(iface
, &interfaces
, head
) {
29 if (iface
->dhcpv4
!= RELAYD_SERVER
)
32 void *i
= blobmsg_open_table(&b
, iface
->ifname
);
33 void *j
= blobmsg_open_array(&b
, "leases");
35 struct dhcpv4_assignment
*lease
;
36 list_for_each_entry(lease
, &iface
->dhcpv4_assignments
, head
) {
37 if (lease
->valid_until
< now
)
40 void *l
= blobmsg_open_table(&b
, NULL
);
42 char *buf
= blobmsg_alloc_string_buffer(&b
, "mac", 13);
43 odhcpd_hexlify(buf
, lease
->hwaddr
, sizeof(lease
->hwaddr
));
44 blobmsg_add_string_buffer(&b
);
46 blobmsg_add_string(&b
, "hostname", lease
->hostname
);
48 buf
= blobmsg_alloc_string_buffer(&b
, "ip", INET_ADDRSTRLEN
);
49 struct in_addr addr
= {htonl(lease
->addr
)};
50 inet_ntop(AF_INET
, &addr
, buf
, INET_ADDRSTRLEN
);
51 blobmsg_add_string_buffer(&b
);
53 blobmsg_add_u32(&b
, "valid", now
- lease
->valid_until
);
55 blobmsg_close_table(&b
, l
);
58 blobmsg_close_array(&b
, j
);
59 blobmsg_close_table(&b
, i
);
62 blobmsg_close_table(&b
, a
);
63 ubus_send_reply(ctx
, req
, b
.head
);
68 static int handle_dhcpv6_leases(_unused
struct ubus_context
*ctx
, _unused
struct ubus_object
*obj
,
69 _unused
struct ubus_request_data
*req
, _unused
const char *method
,
70 _unused
struct blob_attr
*msg
)
73 void *a
= blobmsg_open_table(&b
, "device");
74 time_t now
= odhcpd_time();
76 struct interface
*iface
;
77 list_for_each_entry(iface
, &interfaces
, head
) {
78 if (iface
->dhcpv6
!= RELAYD_SERVER
)
81 void *i
= blobmsg_open_table(&b
, iface
->ifname
);
82 void *j
= blobmsg_open_array(&b
, "leases");
84 struct dhcpv6_assignment
*lease
;
85 list_for_each_entry(lease
, &iface
->ia_assignments
, head
) {
86 if (lease
->valid_until
< now
)
89 void *l
= blobmsg_open_table(&b
, NULL
);
91 char *buf
= blobmsg_alloc_string_buffer(&b
, "duid", 264);
92 odhcpd_hexlify(buf
, lease
->clid_data
, lease
->clid_len
);
93 blobmsg_add_string_buffer(&b
);
95 blobmsg_add_u32(&b
, "iaid", ntohl(lease
->iaid
));
96 blobmsg_add_string(&b
, "hostname", (lease
->hostname
) ? lease
->hostname
: "");
97 blobmsg_add_u32(&b
, "assigned", lease
->assigned
);
98 blobmsg_add_u32(&b
, "length", lease
->length
);
100 void *m
= blobmsg_open_array(&b
, "ipv6");
101 struct in6_addr addr
;
102 for (size_t i
= 0; i
< iface
->ia_addr_len
; ++i
) {
103 if (iface
->ia_addr
[i
].prefix
> 64)
106 addr
= iface
->ia_addr
[i
].addr
;
107 if (lease
->length
== 128)
108 addr
.s6_addr32
[3] = htonl(lease
->assigned
);
110 addr
.s6_addr32
[1] |= htonl(lease
->assigned
);
112 char *c
= blobmsg_alloc_string_buffer(&b
, NULL
, INET6_ADDRSTRLEN
);
113 inet_ntop(AF_INET6
, &addr
, c
, INET6_ADDRSTRLEN
);
114 blobmsg_add_string_buffer(&b
);
116 blobmsg_close_table(&b
, m
);
118 blobmsg_add_u32(&b
, "valid", now
- lease
->valid_until
);
120 blobmsg_close_table(&b
, l
);
123 blobmsg_close_array(&b
, j
);
124 blobmsg_close_table(&b
, i
);
127 blobmsg_close_table(&b
, a
);
128 ubus_send_reply(ctx
, req
, b
.head
);
133 static struct ubus_method main_object_methods
[] = {
134 {.name
= "ipv4leases", .handler
= handle_dhcpv4_leases
},
135 {.name
= "ipv6leases", .handler
= handle_dhcpv6_leases
},
138 static struct ubus_object_type main_object_type
=
139 UBUS_OBJECT_TYPE("dhcp", main_object_methods
);
141 static struct ubus_object main_object
= {
143 .type
= &main_object_type
,
144 .methods
= main_object_methods
,
145 .n_methods
= ARRAY_SIZE(main_object_methods
),
154 static const struct blobmsg_policy dump_attrs
[DUMP_ATTR_MAX
] = {
155 [DUMP_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_ARRAY
},
160 IFACE_ATTR_INTERFACE
,
169 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
170 [IFACE_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
171 [IFACE_ATTR_IFNAME
] = { .name
= "l3_device", .type
= BLOBMSG_TYPE_STRING
},
172 [IFACE_ATTR_UP
] = { .name
= "up", .type
= BLOBMSG_TYPE_BOOL
},
173 [IFACE_ATTR_DATA
] = { .name
= "data", .type
= BLOBMSG_TYPE_TABLE
},
174 [IFACE_ATTR_PREFIX
] = { .name
= "ipv6-prefix", .type
= BLOBMSG_TYPE_ARRAY
},
175 [IFACE_ATTR_ADDRESS
] = { .name
= "ipv6-address", .type
= BLOBMSG_TYPE_ARRAY
},
178 static void handle_dump(_unused
struct ubus_request
*req
, _unused
int type
, struct blob_attr
*msg
)
180 struct blob_attr
*tb
[DUMP_ATTR_MAX
];
181 blobmsg_parse(dump_attrs
, DUMP_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
183 if (!tb
[DUMP_ATTR_INTERFACE
])
187 dump
= blob_memdup(tb
[DUMP_ATTR_INTERFACE
]);
192 static void update_netifd(bool subscribe
)
195 ubus_subscribe(ubus
, &netifd
, objid
);
197 ubus_abort_request(ubus
, &req_dump
);
198 if (!ubus_invoke_async(ubus
, objid
, "dump", NULL
, &req_dump
)) {
199 req_dump
.data_cb
= handle_dump
;
200 ubus_complete_request_async(ubus
, &req_dump
);
205 static int handle_update(_unused
struct ubus_context
*ctx
, _unused
struct ubus_object
*obj
,
206 _unused
struct ubus_request_data
*req
, _unused
const char *method
,
207 struct blob_attr
*msg
)
209 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
210 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
212 const char *interface
= (tb
[IFACE_ATTR_INTERFACE
]) ?
213 blobmsg_get_string(tb
[IFACE_ATTR_INTERFACE
]) : "";
214 const char *ifname
= (tb
[IFACE_ATTR_IFNAME
]) ?
215 blobmsg_get_string(tb
[IFACE_ATTR_IFNAME
]) : "";
217 struct interface
*c
, *iface
= NULL
;
218 list_for_each_entry(c
, &interfaces
, head
)
219 if (!strcmp(interface
, c
->name
) || !strcmp(ifname
, c
->ifname
))
222 if (iface
&& iface
->ignore
)
225 update_netifd(false);
230 void ubus_apply_network(void)
238 blobmsg_for_each_attr(a
, dump
, rem
) {
239 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
240 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
, blobmsg_data(a
), blobmsg_data_len(a
));
242 if (!tb
[IFACE_ATTR_INTERFACE
] || !tb
[IFACE_ATTR_DATA
])
245 const char *interface
= (tb
[IFACE_ATTR_INTERFACE
]) ?
246 blobmsg_get_string(tb
[IFACE_ATTR_INTERFACE
]) : "";
247 const char *ifname
= (tb
[IFACE_ATTR_IFNAME
]) ?
248 blobmsg_get_string(tb
[IFACE_ATTR_IFNAME
]) : "";
250 bool matched
= false;
251 struct interface
*c
, *n
;
252 list_for_each_entry_safe(c
, n
, &interfaces
, head
) {
253 char *f
= memmem(c
->upstream
, c
->upstream_len
,
254 interface
, strlen(interface
) + 1);
255 bool cmatched
= !strcmp(interface
, c
->name
) || !strcmp(ifname
, c
->ifname
);
258 if (!cmatched
&& (!c
->upstream_len
|| !f
|| (f
!= c
->upstream
&& f
[-1] != 0)))
262 config_parse_interface(blobmsg_data(tb
[IFACE_ATTR_DATA
]),
263 blobmsg_data_len(tb
[IFACE_ATTR_DATA
]), c
->name
, false);
267 config_parse_interface(blobmsg_data(tb
[IFACE_ATTR_DATA
]),
268 blobmsg_data_len(tb
[IFACE_ATTR_DATA
]), interface
, false);
279 static const struct blobmsg_policy obj_attrs
[OBJ_ATTR_MAX
] = {
280 [OBJ_ATTR_ID
] = { .name
= "id", .type
= BLOBMSG_TYPE_INT32
},
281 [OBJ_ATTR_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
285 static void handle_event(_unused
struct ubus_context
*ctx
, _unused
struct ubus_event_handler
*ev
,
286 _unused
const char *type
, struct blob_attr
*msg
)
288 struct blob_attr
*tb
[OBJ_ATTR_MAX
];
289 blobmsg_parse(obj_attrs
, OBJ_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
291 if (!tb
[OBJ_ATTR_ID
] || !tb
[OBJ_ATTR_PATH
])
294 if (strcmp(blobmsg_get_string(tb
[OBJ_ATTR_PATH
]), "network.interface"))
297 objid
= blobmsg_get_u32(tb
[OBJ_ATTR_ID
]);
301 static struct ubus_event_handler event_handler
= { .cb
= handle_event
};
304 const char* ubus_get_ifname(const char *name
)
312 blobmsg_for_each_attr(c
, dump
, rem
) {
313 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
314 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
, blobmsg_data(c
), blobmsg_data_len(c
));
316 if (!tb
[IFACE_ATTR_INTERFACE
] || strcmp(name
,
317 blobmsg_get_string(tb
[IFACE_ATTR_INTERFACE
])))
320 if (tb
[IFACE_ATTR_IFNAME
])
321 return blobmsg_get_string(tb
[IFACE_ATTR_IFNAME
]);
328 bool ubus_has_prefix(const char *name
, const char *ifname
)
330 struct blob_attr
*c
, *cur
;
336 blobmsg_for_each_attr(c
, dump
, rem
) {
337 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
338 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
, blobmsg_data(c
), blobmsg_data_len(c
));
340 if (!tb
[IFACE_ATTR_INTERFACE
] || !tb
[IFACE_ATTR_IFNAME
])
343 if (strcmp(name
, blobmsg_get_string(tb
[IFACE_ATTR_INTERFACE
])) ||
344 strcmp(ifname
, blobmsg_get_string(tb
[IFACE_ATTR_IFNAME
])))
347 if ((cur
= tb
[IFACE_ATTR_PREFIX
])) {
348 if (blobmsg_type(cur
) != BLOBMSG_TYPE_ARRAY
|| !blobmsg_check_attr(cur
, NULL
))
353 blobmsg_for_each_attr(d
, cur
, drem
) {
365 if (!(ubus
= ubus_connect(NULL
))) {
366 syslog(LOG_ERR
, "Unable to connect to ubus: %s", strerror(errno
));
370 netifd
.cb
= handle_update
;
371 ubus_register_subscriber(ubus
, &netifd
);
373 ubus_add_uloop(ubus
);
374 ubus_add_object(ubus
, &main_object
);
375 ubus_register_event_handler(ubus
, &event_handler
, "ubus.object.add");
376 if (!ubus_lookup_id(ubus
, "network.interface", &objid
))