blob: 8682e1422fb4df2a6261e2fc7ca8abd21345fd1e [file] [log] [blame]
Max372868b2017-03-02 12:12:00 +01001/* OsmoHLR Control Interface implementation */
2
3/* (C) 2017 sysmocom s.f.m.c. GmbH <info@sysmocom.de>
4 * All Rights Reserved
5 *
6 * Author: Max Suraev <msuraev@sysmocom.de>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#include <stdbool.h>
24
25#include <osmocom/ctrl/control_cmd.h>
26#include <osmocom/ctrl/control_if.h>
27#include <osmocom/ctrl/ports.h>
28
29#include "gsup_server.h"
30#include "logging.h"
31#include "db.h"
32#include "hlr.h"
33#include "luop.h"
34#include "ctrl.h"
35
Max9cacb6f2017-02-20 17:22:56 +010036static int handle_cmd_ps(struct hlr *ctx, struct ctrl_cmd *cmd, bool enable)
37{
38 struct lu_operation *luop = NULL;
39 struct osmo_gsup_conn *co;
40
Neels Hofmeyr518335e2017-10-06 03:20:14 +020041 if (db_subscr_get_by_imsi(ctx->dbc, cmd->value, NULL) < 0) {
Max9cacb6f2017-02-20 17:22:56 +010042 cmd->reply = "Subscriber Unknown in HLR";
43 return CTRL_CMD_ERROR;
44 }
45
Neels Hofmeyre8ccd502017-10-06 04:10:06 +020046 if (db_subscr_nam(ctx->dbc, cmd->value, enable, true) < 0) {
Max9cacb6f2017-02-20 17:22:56 +010047 cmd->reply = "Error updating DB";
48 return CTRL_CMD_ERROR;
49 }
50
51 /* FIXME: only send to single SGSN where latest update for IMSI came from */
52 if (!enable) {
53 llist_for_each_entry(co, &ctx->gs->clients, list) {
54 luop = lu_op_alloc_conn(co);
55 lu_op_fill_subscr(luop, ctx->dbc, cmd->value);
56 lu_op_tx_del_subscr_data(luop);
57 }
58 }
59
60 cmd->reply = "OK";
61
62 return CTRL_CMD_REPLY;
63}
64
65CTRL_CMD_DEFINE_WO_NOVRF(enable_ps, "enable-ps");
66static int set_enable_ps(struct ctrl_cmd *cmd, void *data)
67{
68 return handle_cmd_ps(data, cmd, true);
69}
70
71CTRL_CMD_DEFINE_WO_NOVRF(disable_ps, "disable-ps");
72static int set_disable_ps(struct ctrl_cmd *cmd, void *data)
73{
74 return handle_cmd_ps(data, cmd, false);
75}
76
Max372868b2017-03-02 12:12:00 +010077CTRL_CMD_DEFINE_WO_NOVRF(status_ps, "status-ps");
78static int set_status_ps(struct ctrl_cmd *cmd, void *data)
79{
80 struct hlr *ctx = data;
81 struct lu_operation *luop = lu_op_alloc(ctx->gs);
82 if (!luop) {
83 cmd->reply = "Internal HLR error";
84 return CTRL_CMD_ERROR;
85 }
86
87 if (!lu_op_fill_subscr(luop, ctx->dbc, cmd->value)) {
88 cmd->reply = "Subscriber Unknown in HLR";
89 return CTRL_CMD_ERROR;
90 }
91
92 cmd->reply = luop->subscr.nam_ps ? "1" : "0";
93
94 return CTRL_CMD_REPLY;
95}
96
97int hlr_ctrl_cmds_install()
98{
99 int rc = 0;
100
Max9cacb6f2017-02-20 17:22:56 +0100101 rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_enable_ps);
102 rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_disable_ps);
Max372868b2017-03-02 12:12:00 +0100103 rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_status_ps);
104
105 return rc;
106}
107
108struct ctrl_handle *hlr_controlif_setup(struct hlr *ctx,
109 struct osmo_gsup_server *gs)
110{
111 int rc;
112 struct ctrl_handle *hdl = ctrl_interface_setup_dynip(ctx,
113 ctx->ctrl_bind_addr,
114 OSMO_CTRL_PORT_HLR,
115 NULL);
116 if (!hdl)
117 return NULL;
118
119 rc = hlr_ctrl_cmds_install();
120 if (rc) /* FIXME: close control interface? */
121 return NULL;
122
123 return hdl;
124}