summaryrefslogtreecommitdiffstats
path: root/src/net/tcp/oncrpc.c
blob: cb66aeb85163f4afa33ed2b83a50b990b236d8ee (plain) (blame)
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
/*
 * Copyright (C) 2013 Marin Hannache <ipxe@mareo.fr>.
 *
 * 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 any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <byteswap.h>
#include <ipxe/socket.h>
#include <ipxe/tcpip.h>
#include <ipxe/in.h>
#include <ipxe/iobuf.h>
#include <ipxe/dhcp.h>
#include <ipxe/xfer.h>
#include <ipxe/open.h>
#include <ipxe/uri.h>
#include <ipxe/features.h>
#include <ipxe/oncrpc.h>
#include <ipxe/oncrpc_iob.h>
#include <ipxe/init.h>
#include <ipxe/settings.h>
#include <ipxe/version.h>

/** @file
 *
 * SUN ONC RPC protocol
 *
 */

/** Set most significant bit to 1. */
#define SET_LAST_FRAME( x ) ( (x) | 1 << 31 )
#define GET_FRAME_SIZE( x ) ( (x) & ~( 1 << 31 ) )

#define ONCRPC_CALL     0
#define ONCRPC_REPLY    1

/** AUTH NONE authentication flavor */
struct oncrpc_cred oncrpc_auth_none = {
	.flavor = ONCRPC_AUTH_NONE,
	.length = 0
};

const struct setting uid_setting __setting ( SETTING_AUTH, uid ) = {
	.name        = "uid",
	.description = "User ID",
	.tag         = DHCP_EB_UID,
	.type        = &setting_type_uint32
};

const struct setting gid_setting __setting ( SETTING_AUTH, gid ) = {
	.name        = "gid",
	.description = "Group ID",
	.tag         = DHCP_EB_GID,
	.type        = &setting_type_uint32
};

/**
 * Initialize an ONC RPC AUTH SYS credential structure
 *
 * @v auth_sys          The structure to initialize
 *
 * The hostname field is filled with the value of the hostname setting, if the
 * hostname setting is empty, PRODUCT_SHORT_NAME (usually "iPXE") is used
 * instead.
 */
int oncrpc_init_cred_sys ( struct oncrpc_cred_sys *auth_sys ) {
	if ( ! auth_sys )
		return -EINVAL;

	fetch_string_setting_copy ( NULL, &hostname_setting,
	                            &auth_sys->hostname );
	if ( ! auth_sys->hostname )
		if ( ! ( auth_sys->hostname = strdup ( product_short_name ) ) )
			return -ENOMEM;

	auth_sys->uid         = fetch_uintz_setting ( NULL, &uid_setting );
	auth_sys->gid         = fetch_uintz_setting ( NULL, &uid_setting );
	auth_sys->aux_gid_len = 0;
	auth_sys->stamp       = 0;

	auth_sys->credential.flavor = ONCRPC_AUTH_SYS;
	auth_sys->credential.length = 16 +
	                              oncrpc_strlen ( auth_sys->hostname );

	return 0;
}

/**
 * Prepare an ONC RPC session structure to be used by the ONC RPC layer
 *
 * @v session           ONC RPC session
 * @v credential        Credential structure pointer
 * @v verifier          Verifier structure pointer
 * @v prog_name         ONC RPC program number
 * @v prog_vers         ONC RPC program version number
 */
void oncrpc_init_session ( struct oncrpc_session *session,
                           struct oncrpc_cred *credential,
                           struct oncrpc_cred *verifier, uint32_t prog_name,
                           uint32_t prog_vers ) {
	if ( ! session )
		return;

	session->rpc_id     = rand();
	session->credential = credential;
	session->verifier   = verifier;
	session->prog_name  = prog_name;
	session->prog_vers  = prog_vers;
}

int oncrpc_call ( struct interface *intf, struct oncrpc_session *session,
                  uint32_t proc_name, const struct oncrpc_field fields[] ) {
	size_t           frame_size;
	struct io_buffer *io_buf;

	if ( ! session )
		return -EINVAL;

	struct oncrpc_field header[] = {
		ONCRPC_FIELD ( int32, 0 ),
		ONCRPC_FIELD ( int32, ++session->rpc_id ),
		ONCRPC_FIELD ( int32, ONCRPC_CALL ),
		ONCRPC_FIELD ( int32, ONCRPC_VERS ),
		ONCRPC_FIELD ( int32, session->prog_name ),
		ONCRPC_FIELD ( int32, session->prog_vers ),
		ONCRPC_FIELD ( int32, proc_name ),
		ONCRPC_FIELD ( cred, session->credential ),
		ONCRPC_FIELD ( cred, session->verifier ),
		ONCRPC_FIELD_END,
	};

	frame_size  = oncrpc_compute_size ( header );
	frame_size += oncrpc_compute_size ( fields );

	io_buf = alloc_iob ( frame_size );
	if ( ! io_buf )
		return -ENOBUFS;

	header[0].value.int32 = SET_LAST_FRAME ( frame_size -
	                                         sizeof ( uint32_t ) );

	oncrpc_iob_add_fields ( io_buf, header );
	oncrpc_iob_add_fields ( io_buf, fields );

	return xfer_deliver_iob ( intf, iob_disown ( io_buf ) );
}

size_t oncrpc_compute_size ( const struct oncrpc_field fields[] ) {

	size_t i;
	size_t size = 0;

	for ( i = 0; fields[i].type != oncrpc_none; i++ ) {
		switch ( fields[i].type ) {
		case oncrpc_int32:
			size += sizeof ( uint32_t );
			break;

		case oncrpc_int64:
			size += sizeof ( uint64_t );
			break;

		case oncrpc_str:
			size += oncrpc_strlen ( fields[i].value.str );
			break;

		case oncrpc_array:
			size += oncrpc_align ( fields[i].value.array.length );
			size += sizeof ( uint32_t );
			break;

		case oncrpc_intarray:
			size += sizeof ( uint32_t ) *
				fields[i].value.intarray.length;
			size += sizeof ( uint32_t );
			break;

		case oncrpc_cred:
			size += fields[i].value.cred->length;
			size += 2 * sizeof ( uint32_t );
			break;

		default:
			return size;
		}
	}

	return size;
}

/**
 * Parse an I/O buffer to extract a ONC RPC REPLY
 * @v session	        ONC RPC session
 * @v reply             Reply structure where data will be saved
 * @v io_buf            I/O buffer
 */
int oncrpc_get_reply ( struct oncrpc_session *session __unused,
                       struct oncrpc_reply *reply, struct io_buffer *io_buf ) {
	if ( ! reply || ! io_buf )
		return -EINVAL;

	reply->frame_size = GET_FRAME_SIZE ( oncrpc_iob_get_int ( io_buf ) );
	reply->rpc_id     = oncrpc_iob_get_int ( io_buf );

	/* iPXE has no support for handling ONC RPC call */
	if ( oncrpc_iob_get_int ( io_buf ) != ONCRPC_REPLY )
		return -ENOSYS;

	reply->reply_state = oncrpc_iob_get_int ( io_buf );

	if ( reply->reply_state == 0 )
	{
		/* verifier.flavor */
		oncrpc_iob_get_int ( io_buf );
		/* verifier.length */
		iob_pull ( io_buf, oncrpc_iob_get_int ( io_buf ));

		/* We don't use the verifier in iPXE, let it be an empty
		   verifier. */
		reply->verifier = &oncrpc_auth_none;
	}

	reply->accept_state = oncrpc_iob_get_int ( io_buf );
	reply->data         = io_buf;

	return 0;
}