1 #ifndef _IPXE_ONCRPC_H
2 #define _IPXE_ONCRPC_H
3 
4 #include <stdint.h>
5 #include <ipxe/interface.h>
6 #include <ipxe/iobuf.h>
7 
8 /** @file
9  *
10  * SUN ONC RPC protocol.
11  *
12  */
13 
14 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
15 
16 /** ONC RCP Version */
17 #define ONCRPC_VERS 2
18 
19 /** ONC RPC Null Authentication */
20 #define ONCRPC_AUTH_NONE 0
21 
22 /** ONC RPC System Authentication (also called UNIX Authentication) */
23 #define ONCRPC_AUTH_SYS  1
24 
25 /** Size of an ONC RPC header */
26 #define ONCRPC_HEADER_SIZE ( 11 * sizeof ( uint32_t ) )
27 
28 #define ONCRPC_FIELD( type, value ) { oncrpc_ ## type, { .type = value } }
29 #define ONCRPC_SUBFIELD( type, args... ) \
30 	{ oncrpc_ ## type, { .type = { args } } }
31 
32 #define ONCRPC_FIELD_END { oncrpc_none, { } }
33 
34 /** Enusure that size is a multiple of four */
35 #define oncrpc_align( size ) ( ( (size) + 3 ) & ~3 )
36 
37 /**
38  * Calculate the length of a string, including padding bytes.
39  *
40  * @v str               String
41  * @ret size            Length of the padded string
42  */
43 #define oncrpc_strlen( str ) ( oncrpc_align ( strlen ( str ) ) + \
44                                sizeof ( uint32_t ) )
45 
46 struct oncrpc_cred {
47 	uint32_t               flavor;
48 	uint32_t               length;
49 };
50 
51 struct oncrpc_cred_sys {
52 	struct oncrpc_cred     credential;
53 	uint32_t               stamp;
54 	char                   *hostname;
55 	uint32_t               uid;
56 	uint32_t               gid;
57 	uint32_t               aux_gid_len;
58 	uint32_t               aux_gid[16];
59 };
60 
61 struct oncrpc_reply
62 {
63 	struct oncrpc_cred      *verifier;
64 	uint32_t                rpc_id;
65 	uint32_t                reply_state;
66 	uint32_t                accept_state;
67 	uint32_t                frame_size;
68 	struct io_buffer        *data;
69 };
70 
71 struct oncrpc_session {
72 	struct oncrpc_reply     pending_reply;
73 	struct oncrpc_cred      *credential;
74 	struct oncrpc_cred      *verifier;
75 	uint32_t                rpc_id;
76 	uint32_t                prog_name;
77 	uint32_t                prog_vers;
78 };
79 
80 enum oncrpc_field_type {
81 	oncrpc_none = 0,
82 	oncrpc_int32,
83 	oncrpc_int64,
84 	oncrpc_str,
85 	oncrpc_array,
86 	oncrpc_intarray,
87 	oncrpc_cred,
88 };
89 
90 union oncrpc_field_value {
91 	struct {
92 		size_t           length;
93 		const void       *ptr;
94 	}                        array;
95 
96 	struct {
97 		size_t           length;
98 		const uint32_t   *ptr;
99 	}                        intarray;
100 
101 	int64_t                  int64;
102 	int32_t                  int32;
103 	const char               *str;
104 	const struct oncrpc_cred *cred;
105 };
106 
107 struct oncrpc_field {
108 	enum oncrpc_field_type       type;
109 	union oncrpc_field_value     value;
110 };
111 
112 extern struct oncrpc_cred oncrpc_auth_none;
113 
114 int oncrpc_init_cred_sys ( struct oncrpc_cred_sys *auth_sys );
115 void oncrpc_init_session ( struct oncrpc_session *session,
116                            struct oncrpc_cred *credential,
117                            struct oncrpc_cred *verifier, uint32_t prog_name,
118                            uint32_t prog_vers );
119 
120 int oncrpc_call ( struct interface *intf, struct oncrpc_session *session,
121                   uint32_t proc_name, const struct oncrpc_field fields[] );
122 
123 size_t oncrpc_compute_size ( const struct oncrpc_field fields[] );
124 
125 int oncrpc_get_reply ( struct oncrpc_session *session,
126                        struct oncrpc_reply *reply, struct io_buffer *io_buf );
127 
128 #endif /* _IPXE_ONCRPC_H */
129