Add grouping session functionality for load balancing and failover
[l2tpns.git] / autosnoop.c
1 #include <string.h>
2 #include <sys/socket.h>
3 #include <linux/rtnetlink.h>
4
5 #include "l2tpns.h"
6 #include "plugin.h"
7
8 /* set up intercept based on RADIUS reply */
9
10 int plugin_api_version = PLUGIN_API_VERSION;
11 static struct pluginfuncs *f = 0;
12
13 int plugin_radius_response(struct param_radius_response *data)
14 {
15 if (!strcmp(data->key, "intercept"))
16 {
17 char *p;
18 data->s->snoop_ip = 0;
19 data->s->snoop_port = 0;
20 if ((p = strchr(data->value, ':')))
21 {
22 *p++ = 0;
23 if (*data->value)
24 data->s->snoop_ip = inet_addr(data->value);
25
26 if (data->s->snoop_ip == INADDR_NONE)
27 data->s->snoop_ip = 0;
28
29 if (*p)
30 data->s->snoop_port = atoi(p);
31
32 f->log(3, f->get_id_by_session(data->s), data->s->tunnel,
33 " Intercepting user to %s:%d\n",
34 f->fmtaddr(data->s->snoop_ip, 0), data->s->snoop_port);
35 }
36 else
37 {
38 f->log(3, f->get_id_by_session(data->s), data->s->tunnel,
39 " Not Intercepting user (reply string should"
40 " be intercept=ip:port)\n");
41 }
42 }
43
44 return PLUGIN_RET_OK;
45 }
46
47 int plugin_radius_reset(struct param_radius_reset *data)
48 {
49 data->s->snoop_ip = 0;
50 data->s->snoop_port = 0;
51 return PLUGIN_RET_OK;
52 }
53
54 int plugin_radius_account(struct param_radius_account *data)
55 {
56 if (data->s->snoop_ip && data->s->snoop_port)
57 {
58 uint8_t *p = *data->packet;
59
60 *p = 26; // vendor-specific
61 *(uint32_t *) (p + 2) = htonl(9); // Cisco
62 p[6] = 1; // Cisco-AVPair
63 p[7] = 2 + sprintf((char *) p + 8, "intercept=%s:%d",
64 f->fmtaddr(data->s->snoop_ip, 0), data->s->snoop_port);
65
66 p[1] = p[7] + 6;
67 *data->packet += p[1];
68 }
69
70 return PLUGIN_RET_OK;
71 }
72
73 int plugin_init(struct pluginfuncs *funcs)
74 {
75 return ((f = funcs)) ? 1 : 0;
76 }