-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathubus.c
367 lines (316 loc) · 10.9 KB
/
ubus.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Copyright (C) 2013-2019 iopsys Software Solutions AB
* Author Mohamed Kallel <[email protected]>
* Author Ahmed Zribi <[email protected]>
* Copyright (C) 2012 Luka Perkov <[email protected]>
*/
#include <unistd.h>
#include <libubus.h>
#include <sys/file.h>
#include "cwmp.h"
#include "ubus.h"
#include "external.h"
#include "xml.h"
#include "log.h"
#ifdef TR098
#include <libtr098/wepkey.h>
#include <libtr098/dmentry.h>
#else
#include <libbbfdm/wepkey.h>
#include <libbbfdm/dmentry.h>
#endif
#include "netlink.h"
static struct ubus_context *ctx = NULL;
static struct blob_buf b;
static const char *arr_session_status[] = {
[SESSION_WAITING] = "waiting",
[SESSION_RUNNING] = "running",
[SESSION_FAILURE] = "failure",
[SESSION_SUCCESS] = "success",
};
static int
cwmp_handle_notify(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
bool send_signal = false;
CWMP_LOG(INFO, "triggered ubus notification");
blob_buf_init(&b, 0);
pthread_mutex_lock(&(cwmp_main.mutex_handle_notify));
if (!cwmp_main.count_handle_notify)
send_signal = true;
cwmp_main.count_handle_notify++;
pthread_mutex_unlock(&(cwmp_main.mutex_handle_notify));
if (send_signal)
pthread_cond_signal(&(cwmp_main.threshold_handle_notify));
blobmsg_add_u32(&b, "status", 1);
ubus_send_reply(ctx, req, b.head);
blob_buf_free(&b);
return 0;
}
enum command {
COMMAND_NAME,
__COMMAND_MAX
};
static const struct blobmsg_policy command_policy[] = {
[COMMAND_NAME] = { .name = "command", .type = BLOBMSG_TYPE_STRING },
};
static int
cwmp_handle_command(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_attr *tb[__COMMAND_MAX];
blobmsg_parse(command_policy, ARRAYSIZEOF(command_policy), tb,
blob_data(msg), blob_len(msg));
if (!tb[COMMAND_NAME])
return UBUS_STATUS_INVALID_ARGUMENT;
blob_buf_init(&b, 0);
char *cmd = blobmsg_data(tb[COMMAND_NAME]);
char *info;
if (!strcmp("reload_end_session", cmd)) {
CWMP_LOG(INFO, "triggered ubus reload_end_session");
#ifndef TR098
bbf_cwmp_set_end_session(END_SESSION_RELOAD);
#else
cwmp_set_end_session(END_SESSION_RELOAD);
#endif
blobmsg_add_u32(&b, "status", 0);
if (asprintf(&info, "icwmpd config will reload at the end of the session") == -1)
return -1;
} else if (!strcmp("reload", cmd)) {
CWMP_LOG(INFO, "triggered ubus reload");
if (cwmp_main.session_status.last_status == SESSION_RUNNING) {
#ifndef TR098
bbf_cwmp_set_end_session(END_SESSION_RELOAD);
#else
cwmp_set_end_session(END_SESSION_RELOAD);
#endif
blobmsg_add_u32(&b, "status", 0);
blobmsg_add_string(&b, "info", "Session running, reload at the end of the session");
}
else {
pthread_mutex_lock (&(cwmp_main.mutex_session_queue));
cwmp_apply_acs_changes();
pthread_mutex_unlock (&(cwmp_main.mutex_session_queue));
blobmsg_add_u32(&b, "status", 0);
if (asprintf(&info, "icwmpd config reloaded") == -1)
return -1;
}
} else if (!strcmp("reboot_end_session", cmd)) {
CWMP_LOG(INFO, "triggered ubus reboot_end_session");
#ifndef TR098
bbf_cwmp_set_end_session(END_SESSION_REBOOT);
#else
cwmp_set_end_session(END_SESSION_REBOOT);
#endif
blobmsg_add_u32(&b, "status", 0);
if (asprintf(&info, "icwmpd will reboot at the end of the session") == -1)
return -1;
} else if (!strcmp("action_end_session", cmd)) {
CWMP_LOG(INFO, "triggered ubus action_end_session");
#ifndef TR098
bbf_cwmp_set_end_session(END_SESSION_EXTERNAL_ACTION);
#else
cwmp_set_end_session(END_SESSION_EXTERNAL_ACTION);
#endif
blobmsg_add_u32(&b, "status", 0);
if (asprintf(&info, "icwmpd will execute the scheduled action commands at the end of the session") == -1)
return -1;
} else if (!strcmp("exit", cmd)) {
pthread_t exit_thread;
int error;
CWMP_LOG(INFO, "triggered ubus exit");
int rc = flock(cwmp_main.pid_file, LOCK_UN | LOCK_NB);
close(cwmp_main.pid_file);
if(rc) {
char *piderr = "PID file unlock failed!";
fprintf(stderr, "%s\n", piderr);
CWMP_LOG(ERROR, "%s", piderr);
}
blobmsg_add_u32(&b, "status", 0);
if (asprintf(&info, "icwmpd daemon stopped") == -1)
return -1;
blobmsg_add_string(&b, "info", info);
free(info);
ubus_send_reply(ctx, req, b.head);
blob_buf_free(&b);
close(cwmp_main.cr_socket_desc);
CWMP_LOG(INFO,"Close connection request server socket");
error = pthread_create(&exit_thread, NULL, &thread_exit_program, NULL);
if (error<0)
{
CWMP_LOG(ERROR,"Error when creating the exit thread!");
return -1;
}
return 0;
} else {
blobmsg_add_u32(&b, "status", -1);
if (asprintf(&info, "%s command is not supported", cmd) == -1)
return -1;
}
blobmsg_add_string(&b, "info", info);
free(info);
ubus_send_reply(ctx, req, b.head);
blob_buf_free(&b);
return 0;
}
static inline time_t get_session_status_next_time() {
time_t ntime = 0;
if(list_schedule_inform.next!=&(list_schedule_inform)) {
struct schedule_inform *schedule_inform;
schedule_inform = list_entry(list_schedule_inform.next,struct schedule_inform, list);
ntime = schedule_inform->scheduled_time;
}
if (!ntime || (cwmp_main.session_status.next_retry && ntime > cwmp_main.session_status.next_retry)) {
ntime = cwmp_main.session_status.next_retry;
}
if (!ntime || (cwmp_main.session_status.next_periodic && ntime > cwmp_main.session_status.next_periodic)) {
ntime = cwmp_main.session_status.next_periodic;
}
return ntime;
}
static int
cwmp_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
void *c;
time_t ntime = 0;
blob_buf_init(&b, 0);
c = blobmsg_open_table(&b, "cwmp");
blobmsg_add_string(&b, "status", "up");
blobmsg_add_string(&b, "start_time", mix_get_time_of(cwmp_main.start_time));
blobmsg_add_string(&b, "acs_url", cwmp_main.conf.acsurl);
blobmsg_close_table(&b, c);
c = blobmsg_open_table(&b, "last_session");
blobmsg_add_string(&b, "status", cwmp_main.session_status.last_start_time ? arr_session_status[cwmp_main.session_status.last_status] : "N/A");
blobmsg_add_string(&b, "start_time", cwmp_main.session_status.last_start_time ? mix_get_time_of(cwmp_main.session_status.last_start_time) : "N/A");
blobmsg_add_string(&b, "end_time", cwmp_main.session_status.last_end_time ? mix_get_time_of(cwmp_main.session_status.last_end_time) : "N/A");
blobmsg_close_table(&b, c);
c = blobmsg_open_table(&b, "next_session");
blobmsg_add_string(&b, "status", arr_session_status[SESSION_WAITING]);
ntime = get_session_status_next_time();
blobmsg_add_string(&b, "start_time", ntime ? mix_get_time_of(ntime) : "N/A");
blobmsg_add_string(&b, "end_time", "N/A");
blobmsg_close_table(&b, c);
c = blobmsg_open_table(&b, "statistics");
blobmsg_add_u32(&b, "success_sessions", cwmp_main.session_status.success_session);
blobmsg_add_u32(&b, "failure_sessions", cwmp_main.session_status.failure_session);
blobmsg_add_u32(&b, "total_sessions", cwmp_main.session_status.success_session + cwmp_main.session_status.failure_session);
blobmsg_close_table(&b, c);
ubus_send_reply(ctx, req, b.head);
blob_buf_free(&b);
return 0;
}
enum enum_inform {
INFORM_GET_RPC_METHODS,
INFORM_EVENT,
__INFORM_MAX
};
static const struct blobmsg_policy inform_policy[] = {
[INFORM_GET_RPC_METHODS] = { .name = "GetRPCMethods", .type = BLOBMSG_TYPE_BOOL },
[INFORM_EVENT] = { .name = "event", .type = BLOBMSG_TYPE_STRING },
};
static int
cwmp_handle_inform(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_attr *tb[__INFORM_MAX];
bool grm = false;
char *event = "";
struct event_container *event_container;
struct session *session;
blob_buf_init(&b, 0);
blobmsg_parse(inform_policy, ARRAYSIZEOF(inform_policy), tb,
blob_data(msg), blob_len(msg));
if (tb[INFORM_GET_RPC_METHODS]) {
grm = blobmsg_data(tb[INFORM_GET_RPC_METHODS]);
}
if (tb[INFORM_EVENT]) {
event = blobmsg_data(tb[INFORM_EVENT]);
}
if (grm) {
pthread_mutex_lock (&(cwmp_main.mutex_session_queue));
event_container = cwmp_add_event_container (&cwmp_main, EVENT_IDX_2PERIODIC, "");
if (event_container == NULL)
{
pthread_mutex_unlock (&(cwmp_main.mutex_session_queue));
return 0;
}
cwmp_save_event_container (&cwmp_main,event_container);
session = list_entry (cwmp_main.head_event_container, struct session,head_event_container);
if(cwmp_add_session_rpc_acs(session, RPC_ACS_GET_RPC_METHODS) == NULL)
{
pthread_mutex_unlock (&(cwmp_main.mutex_session_queue));
return 0;
}
pthread_mutex_unlock (&(cwmp_main.mutex_session_queue));
pthread_cond_signal(&(cwmp_main.threshold_session_send));
blobmsg_add_u32(&b, "status", 1);
blobmsg_add_string(&b, "info", "Session with GetRPCMethods will start");
}
else {
int event_code = cwmp_get_int_event_code(event);
pthread_mutex_lock (&(cwmp_main.mutex_session_queue));
cwmp_add_event_container (&cwmp_main, event_code, "");
pthread_mutex_unlock (&(cwmp_main.mutex_session_queue));
pthread_cond_signal(&(cwmp_main.threshold_session_send));
if (cwmp_main.session_status.last_status == SESSION_RUNNING) {
blobmsg_add_u32(&b, "status", -1);
blobmsg_add_string(&b, "info", "Session already running, event will be sent at the end of the session");
}
else
{
blobmsg_add_u32(&b, "status", 1);
blobmsg_add_string(&b, "info", "Session started");
}
}
ubus_send_reply(ctx, req, b.head);
blob_buf_free(&b);
return 0;
}
static const struct ubus_method freecwmp_methods[] = {
UBUS_METHOD_NOARG("notify", cwmp_handle_notify),
UBUS_METHOD("command", cwmp_handle_command, command_policy),
UBUS_METHOD_NOARG("status", cwmp_handle_status),
UBUS_METHOD("inform", cwmp_handle_inform, inform_policy),
};
static struct ubus_object_type main_object_type =
UBUS_OBJECT_TYPE("freecwmpd", freecwmp_methods);
static struct ubus_object main_object = {
.name = "tr069",
.type = &main_object_type,
.methods = freecwmp_methods,
.n_methods = ARRAYSIZEOF(freecwmp_methods),
};
int
ubus_init(struct cwmp *cwmp)
{
uloop_init();
if (netlink_init()) {
CWMP_LOG(ERROR,"netlink initialization failed");
}
if (cwmp->conf.ipv6_enable) {
if (netlink_init_v6()) {
CWMP_LOG(ERROR,"netlink initialization failed");
}
}
ctx = ubus_connect(cwmp->conf.ubus_socket);
if (!ctx) return -1;
ubus_add_uloop(ctx);
if (ubus_add_object(ctx, &main_object)) return -1;
uloop_run();
return 0;
}
void
ubus_exit(void)
{
if (ctx) ubus_free(ctx);
}