summaryrefslogtreecommitdiff
path: root/accel-pppd/ctrl/ipoe/backup.c
blob: 9633bf71de2d8d63e2c704bffcecc8ea48b99e23 (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
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <net/ethernet.h>

#include "triton.h"
#include "events.h"
#include "log.h"
#include "memdebug.h"

#include "ipoe.h"
#include "backup.h"
#include "ap_session_backup.h"

#define IPOE_TAG_HWADDR              1
#define IPOE_TAG_CLIENT_ID           2
#define IPOE_TAG_RELAY_AGENT         3
#define IPOE_TAG_XID                 5
#define IPOE_TAG_GIADDR              6
#define IPOE_TAG_CALLING_SID         7
#define IPOE_TAG_CALLED_SID          8
#define IPOE_TAG_IFNAME              9
#define IPOE_TAG_FLAGS              10
#define IPOE_TAG_YIADDR             11
#define IPOE_TAG_SIADDR             12
#define IPOE_TAG_MASK               13
#define IPOE_TAG_RELAY_SERVER_ID    14
#define IPOE_TAG_LEASE_TIME         15

#define IPOE_TAG_IFINDEX           100

#define IPOE_FLAG_IFCFG          0x01
#define IPOE_FLAG_DHCP_ADDR      0x02
#define IPOE_FLAG_RELAY_ADDR     0x04
#define IPOE_FLAG_L4_REDIR       0x08
#define IPOE_FLAG_L4_REDIR_SET   0x10

#define add_tag(id, data, size) if (!backup_add_tag(m, id, 0, data, size)) return -1;
#define add_tag_i(id, data, size) if (!backup_add_tag(m, id, 1, data, size)) return -1;

static LIST_HEAD(ds_list);

static void restore_complete(void);

#ifdef USE_BACKUP
static int session_save(struct ap_session *ses, struct backup_mod *m)
{
	struct ipoe_session *conn = container_of(ses, typeof(*conn), ses);
	int flags = 0;

	if (conn->ifcfg)
		flags |= IPOE_FLAG_IFCFG;
	
	if (conn->dhcp_addr)
		flags |= IPOE_FLAG_DHCP_ADDR;
	
	if (conn->relay_addr)
		flags |= IPOE_FLAG_RELAY_ADDR;
	
	if (conn->l4_redirect)
		flags |= IPOE_FLAG_L4_REDIR;
	
	if (conn->l4_redirect_set)
		flags |= IPOE_FLAG_L4_REDIR_SET;

	add_tag(IPOE_TAG_HWADDR, conn->hwaddr, 6);
	add_tag(IPOE_TAG_CALLING_SID, ses->ctrl->calling_station_id, strlen(ses->ctrl->calling_station_id));
	add_tag(IPOE_TAG_CALLED_SID, ses->ctrl->called_station_id, strlen(ses->ctrl->called_station_id));
	add_tag(IPOE_TAG_XID, &conn->xid, 4);
	add_tag(IPOE_TAG_GIADDR, &conn->giaddr, 4);
	add_tag(IPOE_TAG_YIADDR, &conn->yiaddr, 4);
	add_tag(IPOE_TAG_SIADDR, &conn->siaddr, 4);
	add_tag(IPOE_TAG_MASK, &conn->mask, 1);
	add_tag(IPOE_TAG_FLAGS, &flags, 4);
	add_tag(IPOE_TAG_RELAY_SERVER_ID, &conn->relay_server_id, 4);
	add_tag(IPOE_TAG_LEASE_TIME, &conn->lease_time, 4);
	add_tag(IPOE_TAG_IFNAME, conn->serv->ifname, strlen(conn->serv->ifname) + 1);

	if (conn->client_id)
		add_tag(IPOE_TAG_CLIENT_ID, conn->client_id->data, conn->client_id->len);

	if (conn->relay_agent)
		add_tag(IPOE_TAG_RELAY_AGENT, conn->relay_agent->data, conn->relay_agent->len);
	
	add_tag_i(IPOE_TAG_IFINDEX, &conn->ifindex, 4);

	return 0;
}

static int session_restore(struct ap_session *ses, struct backup_mod *m)
{
	struct ipoe_session *conn = container_of(ses, typeof(*conn), ses);


	return 0;
}

static void set_dhcpv4_opt(struct dhcpv4_option **opt, struct backup_tag *t, uint8_t **ptr)
{
	*opt = (struct dhcpv4_option *)(*ptr); 
	(*opt)->len = t->size;
	memcpy((*opt)->data, t->data, t->size);
	(*ptr) += sizeof(**opt) + t->size;
}

static struct ap_session *ctrl_restore(struct backup_mod *m)
{
	struct backup_tag *t;
	struct ipoe_session *ses;
	struct ipoe_serv *serv;
	struct backup_tag *ifname = NULL;
	int dlen = 0;
	uint8_t *ptr;
	struct ipoe_session_info *info;
	int flags = 0;

	//if (!m->data->internal)
	//	return NULL;

	list_for_each_entry(t, &m->tag_list, entry) {
		switch(t->id) {
			case IPOE_TAG_CLIENT_ID:
			case IPOE_TAG_RELAY_AGENT:
				dlen += sizeof(struct dhcpv4_option) + t->size;
				break;
			case IPOE_TAG_IFNAME:
				ifname = t;
				break;
		}
	}

	if (!ifname)
		return NULL;

	serv = ipoe_find_serv((char *)ifname->data);
	if (!serv)
		return NULL;

	ses = ipoe_session_alloc();
	if (!ses)
		return NULL;

	if (dlen)
		ses->data = _malloc(dlen);

	ptr = ses->data;

	list_for_each_entry(t, &m->tag_list, entry) {
		switch(t->id) {
			case IPOE_TAG_HWADDR:
				memcpy(ses->hwaddr, t->data, 6);
				break;
			case IPOE_TAG_CALLING_SID:
				ses->ctrl.calling_station_id = _malloc(t->size + 1);
				memcpy(ses->ctrl.calling_station_id, t->data, t->size);
				ses->ctrl.calling_station_id[t->size] = 0;
				break;
			case IPOE_TAG_CALLED_SID:
				ses->ctrl.called_station_id = _malloc(t->size + 1);
				memcpy(ses->ctrl.called_station_id, t->data, t->size);
				ses->ctrl.called_station_id[t->size] = 0;
				break;
			case IPOE_TAG_XID:
				ses->xid = *(uint32_t *)t->data;
				break;
			case IPOE_TAG_GIADDR:
				ses->giaddr = *(uint32_t *)t->data;
				break;
			case IPOE_TAG_CLIENT_ID:
				set_dhcpv4_opt(&ses->client_id, t, &ptr);
				break;
			case IPOE_TAG_RELAY_AGENT:
				set_dhcpv4_opt(&ses->relay_agent, t, &ptr);
				dhcpv4_parse_opt82(ses->relay_agent, &ses->agent_circuit_id, &ses->agent_remote_id);
				break;
			case IPOE_TAG_IFINDEX:
				ses->ifindex = *(uint32_t *)t->data;
				break;
			case IPOE_TAG_YIADDR:
				ses->yiaddr = *(uint32_t *)t->data;
				break;
			case IPOE_TAG_SIADDR:
				ses->siaddr = *(uint32_t *)t->data;
				break;
			case IPOE_TAG_MASK:
				ses->mask = *(uint8_t *)t->data;
				break;
			case IPOE_TAG_FLAGS:
				flags = *(uint32_t *)t->data;
				break;
			case IPOE_TAG_RELAY_SERVER_ID:
				ses->relay_server_id = *(uint32_t *)t->data;
				break;
			case IPOE_TAG_LEASE_TIME:
				ses->lease_time = *(uint32_t *)t->data;
				break;
		}
	}

	if (flags & IPOE_FLAG_IFCFG)
		ses->ifcfg = 1;
	
	if (flags & IPOE_FLAG_DHCP_ADDR) {
		dhcpv4_reserve_ip(ses->serv->dhcpv4, ses->yiaddr);
		ses->dhcp_addr = 1;
	}
	
	if (flags & IPOE_FLAG_RELAY_ADDR)
		ses->relay_addr = 1;
	
	if (flags & IPOE_FLAG_L4_REDIR)
		ses->l4_redirect = 1;
	
	if (flags & IPOE_FLAG_L4_REDIR_SET && m->data->internal)
		ses->l4_redirect = 1;

	ses->serv = serv;
	
	triton_context_register(&ses->ctx, &ses->ses);
	triton_context_wakeup(&ses->ctx);

	pthread_mutex_lock(&serv->lock);
	list_add_tail(&ses->entry, &serv->sessions);
	pthread_mutex_unlock(&serv->lock);

	if (ses->ifindex != -1) {
		list_for_each_entry(info, &ds_list, entry) {
			if (info->ifindex == ses->ifindex) {
				list_del(&info->entry);
				_free(info);
				break;
			}
		}
	}

	return &ses->ses;
}

static struct backup_module mod = {
	.id = MODID_IPOE,
	.save = session_save,
	.restore = session_restore,
	.ctrl_restore = ctrl_restore,
	.restore_complete = restore_complete,
};
#endif

static void dump_sessions(void)
{
	ipoe_nl_get_sessions(&ds_list);

#ifndef USE_BACKUP
	restore_complete();
#endif
}

static void restore_complete(void)
{
	struct ipoe_session_info *info;

	while (!list_empty(&ds_list)) {
		info = list_entry(ds_list.next, typeof(*info), entry);
		ipoe_nl_delete(info->ifindex);
		list_del(&info->entry);
		_free(info);
	}
}

static void init(void)
{
	dump_sessions();

#ifdef USE_BACKUP
	backup_register_module(&mod);
#endif
}

DEFINE_INIT(100, init);