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 (!INFINITE_VALID(lease
->valid_until
) && 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", INFINITE_VALID(lease
->valid_until
) ?
54 (uint32_t)-1 : (uint32_t)(lease
->valid_until
- now
));
56 blobmsg_close_table(&b
, l
);
59 blobmsg_close_array(&b
, j
);
60 blobmsg_close_table(&b
, i
);
63 blobmsg_close_table(&b
, a
);
64 ubus_send_reply(ctx
, req
, b
.head
);
69 static int handle_dhcpv6_leases(_unused
struct ubus_context
*ctx
, _unused
struct ubus_object
*obj
,
70 _unused
struct ubus_request_data
*req
, _unused
const char *method
,
71 _unused
struct blob_attr
*msg
)
74 void *a
= blobmsg_open_table(&b
, "device");
75 time_t now
= odhcpd_time();
77 struct interface
*iface
;
78 list_for_each_entry(iface
, &interfaces
, head
) {
79 if (iface
->dhcpv6
!= RELAYD_SERVER
)
82 void *i
= blobmsg_open_table(&b
, iface
->ifname
);
83 void *j
= blobmsg_open_array(&b
, "leases");
85 struct dhcpv6_assignment
*lease
;
86 list_for_each_entry(lease
, &iface
->ia_assignments
, head
) {
87 if (!INFINITE_VALID(lease
->valid_until
) && lease
->valid_until
< now
)
90 void *l
= blobmsg_open_table(&b
, NULL
);
92 char *buf
= blobmsg_alloc_string_buffer(&b
, "duid", 264);
93 odhcpd_hexlify(buf
, lease
->clid_data
, lease
->clid_len
);
94 blobmsg_add_string_buffer(&b
);
96 blobmsg_add_u32(&b
, "iaid", ntohl(lease
->iaid
));
97 blobmsg_add_string(&b
, "hostname", (lease
->hostname
) ? lease
->hostname
: "");
98 blobmsg_add_u32(&b
, "assigned", lease
->assigned
);
99 blobmsg_add_u32(&b
, "length", lease
->length
);
101 void *m
= blobmsg_open_array(&b
, "ipv6");
102 struct in6_addr addr
;
103 for (size_t i
= 0; i
< iface
->ia_addr_len
; ++i
) {
104 if (iface
->ia_addr
[i
].prefix
> 64)
107 addr
= iface
->ia_addr
[i
].addr
;
108 if (lease
->length
== 128)
109 addr
.s6_addr32
[3] = htonl(lease
->assigned
);
111 addr
.s6_addr32
[1] |= htonl(lease
->assigned
);
112 addr
.s6_addr32
[2] = addr
.s6_addr32
[3] = 0;
115 char *c
= blobmsg_alloc_string_buffer(&b
, NULL
, INET6_ADDRSTRLEN
);
116 inet_ntop(AF_INET6
, &addr
, c
, INET6_ADDRSTRLEN
);
117 blobmsg_add_string_buffer(&b
);
119 blobmsg_close_table(&b
, m
);
121 blobmsg_add_u32(&b
, "valid", INFINITE_VALID(lease
->valid_until
) ?
122 (uint32_t)-1 : (uint32_t)(lease
->valid_until
- now
));
124 blobmsg_close_table(&b
, l
);
127 blobmsg_close_array(&b
, j
);
128 blobmsg_close_table(&b
, i
);
131 blobmsg_close_table(&b
, a
);
132 ubus_send_reply(ctx
, req
, b
.head
);
137 static struct ubus_method main_object_methods
[] = {
138 {.name
= "ipv4leases", .handler
= handle_dhcpv4_leases
},
139 {.name
= "ipv6leases", .handler
= handle_dhcpv6_leases
},
142 static struct ubus_object_type main_object_type
=
143 UBUS_OBJECT_TYPE("dhcp", main_object_methods
);
145 static struct ubus_object main_object
= {
147 .type
= &main_object_type
,
148 .methods
= main_object_methods
,
149 .n_methods
= ARRAY_SIZE(main_object_methods
),
158 static const struct blobmsg_policy dump_attrs
[DUMP_ATTR_MAX
] = {
159 [DUMP_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_ARRAY
},
164 IFACE_ATTR_INTERFACE
,
173 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
174 [IFACE_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
175 [IFACE_ATTR_IFNAME
] = { .name
= "l3_device", .type
= BLOBMSG_TYPE_STRING
},
176 [IFACE_ATTR_UP
] = { .name
= "up", .type
= BLOBMSG_TYPE_BOOL
},
177 [IFACE_ATTR_DATA
] = { .name
= "data", .type
= BLOBMSG_TYPE_TABLE
},
178 [IFACE_ATTR_PREFIX
] = { .name
= "ipv6-prefix", .type
= BLOBMSG_TYPE_ARRAY
},
179 [IFACE_ATTR_ADDRESS
] = { .name
= "ipv6-address", .type
= BLOBMSG_TYPE_ARRAY
},
182 static void handle_dump(_unused
struct ubus_request
*req
, _unused
int type
, struct blob_attr
*msg
)
184 struct blob_attr
*tb
[DUMP_ATTR_MAX
];
185 blobmsg_parse(dump_attrs
, DUMP_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
187 if (!tb
[DUMP_ATTR_INTERFACE
])
191 dump
= blob_memdup(tb
[DUMP_ATTR_INTERFACE
]);
196 static void update_netifd(bool subscribe
)
199 ubus_subscribe(ubus
, &netifd
, objid
);
201 ubus_abort_request(ubus
, &req_dump
);
202 if (!ubus_invoke_async(ubus
, objid
, "dump", NULL
, &req_dump
)) {
203 req_dump
.data_cb
= handle_dump
;
204 ubus_complete_request_async(ubus
, &req_dump
);
209 static int handle_update(_unused
struct ubus_context
*ctx
, _unused
struct ubus_object
*obj
,
210 _unused
struct ubus_request_data
*req
, _unused
const char *method
,
211 struct blob_attr
*msg
)
213 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
214 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
216 const char *interface
= (tb
[IFACE_ATTR_INTERFACE
]) ?
217 blobmsg_get_string(tb
[IFACE_ATTR_INTERFACE
]) : "";
218 const char *ifname
= (tb
[IFACE_ATTR_IFNAME
]) ?
219 blobmsg_get_string(tb
[IFACE_ATTR_IFNAME
]) : "";
221 struct interface
*c
, *iface
= NULL
;
222 list_for_each_entry(c
, &interfaces
, head
)
223 if (!strcmp(interface
, c
->name
) || !strcmp(ifname
, c
->ifname
))
226 if (iface
&& iface
->ignore
)
229 update_netifd(false);
234 void ubus_apply_network(void)
242 blobmsg_for_each_attr(a
, dump
, rem
) {
243 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
244 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
, blobmsg_data(a
), blobmsg_data_len(a
));
246 if (!tb
[IFACE_ATTR_INTERFACE
] || !tb
[IFACE_ATTR_DATA
])
249 const char *interface
= (tb
[IFACE_ATTR_INTERFACE
]) ?
250 blobmsg_get_string(tb
[IFACE_ATTR_INTERFACE
]) : "";
251 const char *ifname
= (tb
[IFACE_ATTR_IFNAME
]) ?
252 blobmsg_get_string(tb
[IFACE_ATTR_IFNAME
]) : "";
254 bool matched
= false;
255 struct interface
*c
, *n
;
256 list_for_each_entry_safe(c
, n
, &interfaces
, head
) {
257 char *f
= memmem(c
->upstream
, c
->upstream_len
,
258 interface
, strlen(interface
) + 1);
259 bool cmatched
= !strcmp(interface
, c
->name
) || !strcmp(ifname
, c
->ifname
);
262 if (!cmatched
&& (!c
->upstream_len
|| !f
|| (f
!= c
->upstream
&& f
[-1] != 0)))
266 config_parse_interface(blobmsg_data(tb
[IFACE_ATTR_DATA
]),
267 blobmsg_data_len(tb
[IFACE_ATTR_DATA
]), c
->name
, false);
271 config_parse_interface(blobmsg_data(tb
[IFACE_ATTR_DATA
]),
272 blobmsg_data_len(tb
[IFACE_ATTR_DATA
]), interface
, false);
283 static const struct blobmsg_policy obj_attrs
[OBJ_ATTR_MAX
] = {
284 [OBJ_ATTR_ID
] = { .name
= "id", .type
= BLOBMSG_TYPE_INT32
},
285 [OBJ_ATTR_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
289 static void handle_event(_unused
struct ubus_context
*ctx
, _unused
struct ubus_event_handler
*ev
,
290 _unused
const char *type
, struct blob_attr
*msg
)
292 struct blob_attr
*tb
[OBJ_ATTR_MAX
];
293 blobmsg_parse(obj_attrs
, OBJ_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
295 if (!tb
[OBJ_ATTR_ID
] || !tb
[OBJ_ATTR_PATH
])
298 if (strcmp(blobmsg_get_string(tb
[OBJ_ATTR_PATH
]), "network.interface"))
301 objid
= blobmsg_get_u32(tb
[OBJ_ATTR_ID
]);
305 static struct ubus_event_handler event_handler
= { .cb
= handle_event
};
308 const char* ubus_get_ifname(const char *name
)
316 blobmsg_for_each_attr(c
, dump
, rem
) {
317 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
318 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
, blobmsg_data(c
), blobmsg_data_len(c
));
320 if (!tb
[IFACE_ATTR_INTERFACE
] || strcmp(name
,
321 blobmsg_get_string(tb
[IFACE_ATTR_INTERFACE
])))
324 if (tb
[IFACE_ATTR_IFNAME
])
325 return blobmsg_get_string(tb
[IFACE_ATTR_IFNAME
]);
332 bool ubus_has_prefix(const char *name
, const char *ifname
)
334 struct blob_attr
*c
, *cur
;
340 blobmsg_for_each_attr(c
, dump
, rem
) {
341 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
342 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
, blobmsg_data(c
), blobmsg_data_len(c
));
344 if (!tb
[IFACE_ATTR_INTERFACE
] || !tb
[IFACE_ATTR_IFNAME
])
347 if (strcmp(name
, blobmsg_get_string(tb
[IFACE_ATTR_INTERFACE
])) ||
348 strcmp(ifname
, blobmsg_get_string(tb
[IFACE_ATTR_IFNAME
])))
351 if ((cur
= tb
[IFACE_ATTR_PREFIX
])) {
352 if (blobmsg_type(cur
) != BLOBMSG_TYPE_ARRAY
|| !blobmsg_check_attr(cur
, false))
357 blobmsg_for_each_attr(d
, cur
, drem
) {
369 if (!(ubus
= ubus_connect(NULL
))) {
370 syslog(LOG_ERR
, "Unable to connect to ubus: %s", strerror(errno
));
374 netifd
.cb
= handle_update
;
375 ubus_register_subscriber(ubus
, &netifd
);
377 ubus_add_uloop(ubus
);
378 ubus_add_object(ubus
, &main_object
);
379 ubus_register_event_handler(ubus
, &event_handler
, "ubus.object.add");
380 if (!ubus_lookup_id(ubus
, "network.interface", &objid
))