aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/interceptor/linux_ipm.c
blob: a9cb079dca11f3bebb47d439086b639ce4214ec5 (plain)
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
/* Netfilter Driver for IPSec VPN Client
 *
 * Copyright(c)   2012 Samsung Electronics
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

/*
 * linux_ipm.c
 *
 * Linux interceptor kernel to user space messaging.
 *
 */

#include "linux_internal.h"

extern SshInterceptor ssh_interceptor_context;

/************************ Internal utility functions ************************/

/* Use printk instead of SSH_DEBUG macros. */
#ifdef DEBUG_LIGHT
#define SSH_LINUX_IPM_DEBUG(x...) if (net_ratelimit()) printk(KERN_INFO x)
#define SSH_LINUX_IPM_WARN(x...) panic(x)
#endif /* DEBUG_LIGHT */

#ifndef SSH_LINUX_IPM_DEBUG
#define SSH_LINUX_IPM_DEBUG(x...)
#define SSH_LINUX_IPM_WARN(x...) printk(KERN_CRIT x)
#endif /* SSH_LINUX_IPM_DEBUG */

/************************* Ipm message alloc / free *************************/

static void interceptor_ipm_message_free_internal(SshInterceptor interceptor,
						  SshInterceptorIpmMsg msg)
{
  SSH_ASSERT(interceptor != NULL);
  SSH_ASSERT(msg != NULL);

  if (msg->buf)
    ssh_free(msg->buf);
  msg->buf = NULL;

  SSH_LINUX_STATISTICS(interceptor,
  {
    interceptor->stats.ipm_send_queue_len--;
    interceptor->stats.ipm_send_queue_bytes -= (SshUInt64) msg->len;
  });

  if (msg->emergency_mallocated)
    {
      ssh_free(msg);
    }
  else
    {
      msg->next = interceptor->ipm.msg_freelist;
      interceptor->ipm.msg_freelist = msg;
      msg->prev = NULL;
    }
}

void interceptor_ipm_message_free(SshInterceptor interceptor,
				  SshInterceptorIpmMsg msg)
{
  local_bh_disable();
  write_lock(&interceptor->ipm.lock);
  interceptor_ipm_message_free_internal(interceptor, msg);
  write_unlock(&interceptor->ipm.lock);
  local_bh_enable();
}

static SshInterceptorIpmMsg
interceptor_ipm_message_alloc(SshInterceptor interceptor,
			      Boolean reliable,
			      size_t len)
{
  SshInterceptorIpmMsg msg = NULL;

  SSH_ASSERT(interceptor != NULL);

  /* Try to take a message from freelist. */
  if (interceptor->ipm.msg_freelist)
    {
      msg = interceptor->ipm.msg_freelist;
      interceptor->ipm.msg_freelist = msg->next;
    }

#if SSH_LINUX_MAX_IPM_MESSAGES < 2000
#error "SSH_LINUX_MAX_IPM_MESSAGES is too low"
#endif /* SSH_LINUX_MAX_IPM_MESSAGES */

  /* Try to allocate a new message. */
  else if (interceptor->ipm.msg_allocated < SSH_LINUX_MAX_IPM_MESSAGES)
    {
      msg = ssh_calloc(1, sizeof(*msg));
      if (msg != NULL)
	{
	  interceptor->ipm.msg_allocated++;
	}
    }

  /* Try to reuse last unreliable message in send queue. */
  if (msg == NULL && reliable == TRUE)
    {
      /* This is a reliable message, reuse last unreliable message. */
      if (interceptor->ipm.send_queue_num_unreliable > 0)
        {
          for (msg = interceptor->ipm.send_queue_tail;
               msg != NULL;
               msg = msg->prev)
            {
              if (msg->reliable == 0)
                {
                  if (msg->next != NULL)
                    msg->next->prev = msg->prev;

                  if (msg->prev != NULL)
                    msg->prev->next = msg->next;

                  if (msg == interceptor->ipm.send_queue)
                    interceptor->ipm.send_queue = msg->next;

                  if (msg == interceptor->ipm.send_queue_tail)
                    interceptor->ipm.send_queue_tail = msg->prev;

                  SSH_ASSERT(interceptor->ipm.send_queue_num_unreliable > 0);
                  interceptor->ipm.send_queue_num_unreliable--;

                  SSH_LINUX_STATISTICS(interceptor,
	          {
                    interceptor->stats.ipm_send_queue_len--;
                    interceptor->stats.ipm_send_queue_bytes
                      -= (SshUInt64) msg->len;
                  });

                  ssh_free(msg->buf);
                  break;
                }
            }
        }

      /* Last resort, malloc message. */
      if (msg == NULL)
	{
	  msg = ssh_calloc(1, sizeof(*msg));
	  if (msg != NULL)
	    msg->emergency_mallocated = 1;
	}
    }

  if (msg)
    SSH_LINUX_STATISTICS(interceptor,
    {
      interceptor->stats.ipm_send_queue_len++;
      interceptor->stats.ipm_send_queue_bytes += (SshUInt64) len;
    });

  return msg;
}

void interceptor_ipm_message_freelist_uninit(SshInterceptor interceptor)
{
  SshInterceptorIpmMsg msg;
  int freelist_len;

  local_bh_disable();
  write_lock(&interceptor->ipm.lock);

  SSH_ASSERT(atomic_read(&interceptor->ipm.open) == 0);

  while (interceptor->ipm.msg_freelist != NULL)
    {
      msg = interceptor->ipm.msg_freelist;
      interceptor->ipm.msg_freelist = msg->next;
      SSH_ASSERT(msg->buf == NULL);
      ssh_free(msg);
      interceptor->ipm.msg_allocated--;
    }

  freelist_len = interceptor->ipm.msg_allocated;

  write_unlock(&interceptor->ipm.lock);
  local_bh_enable();

  if (freelist_len)
    SSH_LINUX_IPM_WARN("Memory leak detected: %d ipm messages leaked!\n",
		       freelist_len);
}


/***************************** Process message from ipm ********************/

ssize_t ssh_interceptor_receive_from_ipm(unsigned char *data, size_t len)
{
  SshUInt32 msg_len;
  SshUInt8 msg_type;

  /* Need a complete header. */
  if (len < 5)
    return 0;

  /* Parse message header. */
  msg_len = SSH_GET_32BIT(data) - 1;
  msg_type = SSH_GET_8BIT(data + 4);

  /* Need a complete message. */
  if (msg_len > (len - 5))
    return 0;

  /* Pass message to engine. */
  local_bh_disable();

  ssh_engine_packet_from_ipm(ssh_interceptor_context->engine,
			     msg_type, data + 5, msg_len);

  local_bh_enable();

  return msg_len + 5;
}


/***************************** Send to ipm *********************************/

Boolean ssh_interceptor_send_to_ipm(unsigned char *data, size_t len,
				    Boolean reliable, void *machine_context)
{
  SshInterceptorIpmMsg msg = NULL;

  local_bh_disable();
  write_lock(&ssh_interceptor_context->ipm.lock);

  /* Check ipm channel status */
  if (atomic_read(&ssh_interceptor_context->ipm.open) == 0)
    {
      write_unlock(&ssh_interceptor_context->ipm.lock);
      local_bh_enable();
      ssh_free(data);
      SSH_LINUX_IPM_DEBUG("ipm channel closed, dropping ipm message len %d\n",
			  (int) len);
      return FALSE;
    }

  /* Allocate a message. */
  msg = interceptor_ipm_message_alloc(ssh_interceptor_context, reliable, len);
  if (msg == NULL)
    {
      write_unlock(&ssh_interceptor_context->ipm.lock);
      local_bh_enable();

      if (reliable)
	SSH_LINUX_IPM_WARN("Dropping reliable ipm message type %d len %d\n",
			   (int) (len < 5 ? -1 : data[4]), (int) len);
      else
	SSH_LINUX_IPM_DEBUG("Dropping unreliable ipm message type %d "
			    "len %d\n",
			    (int) (len < 5 ? -1 : data[4]), (int) len);
      ssh_free(data);
      return FALSE;
    }

  /* Fill message structure. */
  msg->buf = data;
  msg->len = len;
  msg->offset = 0;
  if (reliable)
    msg->reliable = 1;

  /* Append message to send queue tail. */
  msg->prev = ssh_interceptor_context->ipm.send_queue_tail;
  ssh_interceptor_context->ipm.send_queue_tail = msg;
  msg->next = NULL;
  if (msg->prev)
    msg->prev->next = msg;

  if (ssh_interceptor_context->ipm.send_queue == NULL)
    ssh_interceptor_context->ipm.send_queue = msg;

  if (msg->reliable == 0)
    {
      ssh_interceptor_context->ipm.send_queue_num_unreliable++;
      SSH_ASSERT(ssh_interceptor_context->ipm.send_queue_num_unreliable != 0);
    }

  write_unlock(&ssh_interceptor_context->ipm.lock);
  local_bh_enable();

  /* Wake up reader. */
  wake_up_interruptible(&ssh_interceptor_context->ipm_proc_entry.wait_queue);

  return TRUE;
}


/**************************** Ipm channel open / close **********************/

void interceptor_ipm_open(SshInterceptor interceptor)
{

  local_bh_disable();
  write_lock(&interceptor->ipm.lock);

  /* Assert that send queue is empty */
  SSH_ASSERT(interceptor->ipm.send_queue == NULL);

  /* Mark ipm channel open */
  atomic_set(&interceptor->ipm.open, 1);

  write_unlock(&interceptor->ipm.lock);
  local_bh_enable();
}

void interceptor_ipm_close(SshInterceptor interceptor)
{
  SshInterceptorIpmMsg msg, list;

  local_bh_disable();
  write_lock(&interceptor->ipm.lock);

  /* Mark ipm channel closed */
  atomic_set(&interceptor->ipm.open, 0);

  /* Clear send queue */
  list = interceptor->ipm.send_queue;
  interceptor->ipm.send_queue = NULL;
  interceptor->ipm.send_queue_tail = NULL;

  write_unlock(&interceptor->ipm.lock);
  local_bh_enable();

  /* Free all ipm messages from send queue. */
  while (list != NULL)
    {
      msg = list;
      list = msg->next;
      interceptor_ipm_message_free(interceptor, msg);
    }
}


/***************************** Init / uninit ********************************/

Boolean ssh_interceptor_ipm_init(SshInterceptor interceptor)
{
  /* Initialize ipm structure */
  atomic_set(&interceptor->ipm.open, 0);
  rwlock_init(&interceptor->ipm.lock);

  /* Initialize /proc interface */
  return ssh_interceptor_proc_init(interceptor);
}

void ssh_interceptor_ipm_uninit(SshInterceptor interceptor)
{
  /* Uninit /proc interface */
  ssh_interceptor_proc_uninit(interceptor);

  interceptor_ipm_close(interceptor);

  /* Free ipm messages.*/
  interceptor_ipm_message_freelist_uninit(interceptor);
}