summaryrefslogtreecommitdiff
path: root/src/libcharon/sa/tasks/ike_vendor.c
blob: 7c435b6d1f1f9f65a1c62c52f7fb372a7b390a77 (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
/*
 * Copyright (C) 2009 Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "ike_vendor.h"

#include <daemon.h>
#include <encoding/payloads/vendor_id_payload.h>

typedef struct private_ike_vendor_t private_ike_vendor_t;

/**
 * Private data of an ike_vendor_t object.
 */
struct private_ike_vendor_t {

	/**
	 * Public ike_vendor_t interface.
	 */
	ike_vendor_t public;

	/**
	 * Associated IKE_SA
	 */
	ike_sa_t *ike_sa;

	/**
	 * Are we the inititator of this task
	 */
	bool initiator;
};

/**
 * strongSwan specific vendor ID without version, MD5("strongSwan")
 */
static chunk_t strongswan_vid = chunk_from_chars(
	0x88,0x2f,0xe5,0x6d,0x6f,0xd2,0x0d,0xbc,
	0x22,0x51,0x61,0x3b,0x2e,0xbe,0x5b,0xeb
);

METHOD(task_t, build, status_t,
	private_ike_vendor_t *this, message_t *message)
{
	if (lib->settings->get_bool(lib->settings,
								"charon.send_vendor_id", FALSE))
	{
		vendor_id_payload_t *vid;

		vid = vendor_id_payload_create_data(chunk_clone(strongswan_vid));
		message->add_payload(message, &vid->payload_interface);
	}

	return this->initiator ? NEED_MORE : SUCCESS;
}

METHOD(task_t, process, status_t,
	private_ike_vendor_t *this, message_t *message)
{
	enumerator_t *enumerator;
	payload_t *payload;

	enumerator = message->create_payload_enumerator(message);
	while (enumerator->enumerate(enumerator, &payload))
	{
		if (payload->get_type(payload) == VENDOR_ID)
		{
			vendor_id_payload_t *vid;
			chunk_t data;

			vid = (vendor_id_payload_t*)payload;
			data = vid->get_data(vid);

			if (chunk_equals(data, strongswan_vid))
			{
				DBG1(DBG_IKE, "received strongSwan vendor id");
				this->ike_sa->enable_extension(this->ike_sa, EXT_STRONGSWAN);
			}
			else
			{
				DBG1(DBG_ENC, "received unknown vendor id: %#B", &data);
			}
		}
	}
	enumerator->destroy(enumerator);

	return this->initiator ? SUCCESS : NEED_MORE;
}

METHOD(task_t, migrate, void,
	private_ike_vendor_t *this, ike_sa_t *ike_sa)
{
	this->ike_sa = ike_sa;
}

METHOD(task_t, get_type, task_type_t,
	private_ike_vendor_t *this)
{
	return IKE_VENDOR;
}

METHOD(task_t, destroy, void,
	private_ike_vendor_t *this)
{
	free(this);
}

/**
 * See header
 */
ike_vendor_t *ike_vendor_create(ike_sa_t *ike_sa, bool initiator)
{
	private_ike_vendor_t *this;

	INIT(this,
		.public.task = {
			.build = _build,
			.process = _process,
			.migrate = _migrate,
			.get_type = _get_type,
			.destroy = _destroy,
		},
		.initiator = initiator,
		.ike_sa = ike_sa,
	);

	return &this->public;
}