suricata
util-lua-dnp3.c
Go to the documentation of this file.
1 /* Copyright (C) 2015 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 #include "suricata-common.h"
19 
20 #include "app-layer-dnp3.h"
21 #include "app-layer-dnp3-objects.h"
22 
23 #ifdef HAVE_LUA
24 
25 #include <lua.h>
26 #include <lualib.h>
27 #include <lauxlib.h>
28 
29 #include "util-lua.h"
30 #include "util-lua-common.h"
31 #include "util-lua-dnp3.h"
32 #include "util-lua-dnp3-objects.h"
33 
34 /**
35  * \brief Helper macro to push key and integer value onto a table.
36  */
37 #define LUA_PUSHT_INT(l, k, v) do { \
38  lua_pushliteral(luastate, k); \
39  lua_pushinteger(luastate, v); \
40  lua_settable(luastate, -3); \
41  } while (0);
42 
43 static void DNP3PushPoints(lua_State *luastate, DNP3Object *object)
44 {
45  DNP3Point *point;
46  int i = 1;
47 
48  TAILQ_FOREACH(point, object->points, next) {
49  lua_pushinteger(luastate, i++);
50  lua_newtable(luastate);
51 
52  lua_pushliteral(luastate, "index");
53  lua_pushinteger(luastate, point->index);
54  lua_settable(luastate, -3);
55 
56  DNP3PushPoint(luastate, object, point);
57 
58  lua_settable(luastate, -3);
59  }
60 }
61 
62 static void DNP3PushObjects(lua_State *luastate, DNP3ObjectList *objects)
63 {
64  DNP3Object *object = NULL;
65  int i = 1;
66 
67  TAILQ_FOREACH(object, objects, next) {
68  lua_pushinteger(luastate, i++);
69  lua_newtable(luastate);
70 
71  lua_pushliteral(luastate, "group");
72  lua_pushinteger(luastate, object->group);
73  lua_settable(luastate, -3);
74 
75  lua_pushliteral(luastate, "variation");
76  lua_pushinteger(luastate, object->variation);
77  lua_settable(luastate, -3);
78 
79  lua_pushliteral(luastate, "points");
80  lua_newtable(luastate);
81  DNP3PushPoints(luastate, object);
82  lua_settable(luastate, -3);
83 
84  lua_settable(luastate, -3);
85  }
86 }
87 
88 static void DNP3PushLinkHeader(lua_State *luastate, DNP3LinkHeader *header)
89 {
90  LUA_PUSHT_INT(luastate, "len", header->len);
91  LUA_PUSHT_INT(luastate, "control", header->control);
92  LUA_PUSHT_INT(luastate, "dst", header->dst);
93  LUA_PUSHT_INT(luastate, "src", header->src);
94  LUA_PUSHT_INT(luastate, "crc", header->crc);
95 }
96 
97 static void DNP3PushApplicationHeader(lua_State *luastate,
98  DNP3ApplicationHeader *header)
99 {
100  LUA_PUSHT_INT(luastate, "control", header->control);
101  LUA_PUSHT_INT(luastate, "function_code", header->function_code);
102 }
103 
104 static void DNP3PushRequest(lua_State *luastate, DNP3Transaction *tx)
105 {
106  /* Link header. */
107  lua_pushliteral(luastate, "link_header");
108  lua_newtable(luastate);
109  DNP3PushLinkHeader(luastate, &tx->request_lh);
110  lua_settable(luastate, -3);
111 
112  /* Transport header. */
113  LUA_PUSHT_INT(luastate, "transport_header", tx->request_th);
114 
115  /* Application header. */
116  lua_pushliteral(luastate, "application_header");
117  lua_newtable(luastate);
118  DNP3PushApplicationHeader(luastate, &tx->request_ah);
119  lua_settable(luastate, -3);
120 
121  lua_pushliteral(luastate, "objects");
122  lua_newtable(luastate);
123  DNP3PushObjects(luastate, &tx->request_objects);
124  lua_settable(luastate, -3);
125 }
126 
127 static void DNP3PushResponse(lua_State *luastate, DNP3Transaction *tx)
128 {
129  /* Link header. */
130  lua_pushliteral(luastate, "link_header");
131  lua_newtable(luastate);
132  DNP3PushLinkHeader(luastate, &tx->response_lh);
133  lua_settable(luastate, -3);
134 
135  /* Transport header. */
136  LUA_PUSHT_INT(luastate, "transport_header", tx->response_th);
137 
138  /* Application header. */
139  lua_pushliteral(luastate, "application_header");
140  lua_newtable(luastate);
141  DNP3PushApplicationHeader(luastate, &tx->response_ah);
142  lua_settable(luastate, -3);
143 
144  /* Internal indicators. */
145  LUA_PUSHT_INT(luastate, "indicators",
146  tx->response_iin.iin1 << 8 | tx->response_iin.iin2);
147 
148  lua_pushliteral(luastate, "objects");
149  lua_newtable(luastate);
150  DNP3PushObjects(luastate, &tx->response_objects);
151  lua_settable(luastate, -3);
152 }
153 
154 static int DNP3GetTx(lua_State *luastate)
155 {
156  if (!LuaStateNeedProto(luastate, ALPROTO_DNP3)) {
157  return LuaCallbackError(luastate, "error: protocol not dnp3");
158  }
159 
160  DNP3Transaction *tx = LuaStateGetTX(luastate);
161  if (tx == NULL) {
162  return LuaCallbackError(luastate, "error: no tx");
163  }
164 
165  lua_newtable(luastate);
166 
167  lua_pushliteral(luastate, "tx_num");
168  lua_pushinteger(luastate, tx->tx_num);
169  lua_settable(luastate, -3);
170 
171  LUA_PUSHT_INT(luastate, "has_request", tx->has_request);
172  if (tx->has_request) {
173  lua_pushliteral(luastate, "request");
174  lua_newtable(luastate);
175  LUA_PUSHT_INT(luastate, "done", tx->request_done);
176  LUA_PUSHT_INT(luastate, "complete", tx->request_complete);
177  DNP3PushRequest(luastate, tx);
178  lua_settable(luastate, -3);
179  }
180 
181  LUA_PUSHT_INT(luastate, "has_response", tx->has_response);
182  if (tx->has_response) {
183  lua_pushliteral(luastate, "response");
184  lua_newtable(luastate);
185  LUA_PUSHT_INT(luastate, "done", tx->response_done);
186  LUA_PUSHT_INT(luastate, "complete", tx->response_complete);
187  DNP3PushResponse(luastate, tx);
188  lua_settable(luastate, -3);
189  }
190 
191  return 1;
192 }
193 
194 int LuaRegisterDNP3Functions(lua_State *luastate)
195 {
196  lua_pushcfunction(luastate, DNP3GetTx);
197  lua_setglobal(luastate, "DNP3GetTx");
198 
199  return 0;
200 }
201 
202 #endif /* HAVE_LUA */
DNP3ApplicationHeader response_ah
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:350
struct HtpBodyChunk_ * next
DNP3ObjectList request_objects
DNP3 transaction.
DNP3TransportHeader response_th
DNP3TransportHeader request_th
Struct to hold the list of decoded objects.
uint8_t request_complete
DNP3LinkHeader request_lh
DNP3ApplicationHeader request_ah
DNP3 object point.
uint8_t variation
uint8_t response_complete
DNP3LinkHeader response_lh
DNP3InternalInd response_iin
void DNP3PushPoint(lua_State *luastate, DNP3Object *object, DNP3Point *item)
DNP3PointList * points
uint32_t index
DNP3ObjectList response_objects