usb4bsd: Next attempt at fixing BIOS handoff for XHCI
[dragonfly.git] / lib / libstand / rpc.c
bloba5447e824555a6bbbb5b795b8c1fa9a403f41210
1 /* $NetBSD: rpc.c,v 1.18 1998/01/23 19:27:45 thorpej Exp $ */
2 /* $DragonFly: src/lib/libstand/rpc.c,v 1.3 2005/12/11 02:27:26 swildner Exp $ */
4 /*
5 * Copyright (c) 1992 Regents of the University of California.
6 * All rights reserved.
8 * This software was developed by the Computer Systems Engineering group
9 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10 * contributed to Berkeley.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Lawrence Berkeley Laboratory and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
40 * @(#) Header: rpc.c,v 1.12 93/09/28 08:31:56 leres Exp (LBL)
44 * RPC functions used by NFS and bootparams.
45 * Note that bootparams requires the ability to find out the
46 * address of the server from which its response has come.
47 * This is supported by keeping the IP/UDP headers in the
48 * buffer space provided by the caller. (See rpc_fromaddr)
51 #include <sys/param.h>
52 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
57 #include <string.h>
59 #include "rpcv2.h"
61 #include "stand.h"
62 #include "net.h"
63 #include "netif.h"
64 #include "rpc.h"
66 struct auth_info {
67 int32_t authtype; /* auth type */
68 u_int32_t authlen; /* auth length */
71 struct auth_unix {
72 int32_t ua_time;
73 int32_t ua_hostname; /* null */
74 int32_t ua_uid;
75 int32_t ua_gid;
76 int32_t ua_gidlist; /* null */
79 struct rpc_call {
80 u_int32_t rp_xid; /* request transaction id */
81 int32_t rp_direction; /* call direction (0) */
82 u_int32_t rp_rpcvers; /* rpc version (2) */
83 u_int32_t rp_prog; /* program */
84 u_int32_t rp_vers; /* version */
85 u_int32_t rp_proc; /* procedure */
88 struct rpc_reply {
89 u_int32_t rp_xid; /* request transaction id */
90 int32_t rp_direction; /* call direction (1) */
91 int32_t rp_astatus; /* accept status (0: accepted) */
92 union {
93 u_int32_t rpu_errno;
94 struct {
95 struct auth_info rok_auth;
96 u_int32_t rok_status;
97 } rpu_rok;
98 } rp_u;
101 /* Local forwards */
102 static ssize_t recvrpc(struct iodesc *, void *, size_t, time_t);
103 static int rpc_getport(struct iodesc *, n_long, n_long);
105 int rpc_xid;
106 int rpc_port = 0x400; /* predecrement */
109 * Make a rpc call; return length of answer
110 * Note: Caller must leave room for headers.
112 ssize_t
113 rpc_call(struct iodesc *d, n_long prog, n_long vers, n_long proc, void *sdata,
114 size_t slen, void *rdata, size_t rlen)
116 ssize_t cc;
117 struct auth_info *auth;
118 struct rpc_call *call;
119 struct rpc_reply *reply;
120 char *send_head, *send_tail;
121 char *recv_head, *recv_tail;
122 n_long x;
123 int port; /* host order */
125 #ifdef RPC_DEBUG
126 if (debug)
127 printf("rpc_call: prog=0x%x vers=%d proc=%d\n",
128 prog, vers, proc);
129 #endif
131 port = rpc_getport(d, prog, vers);
132 if (port == -1)
133 return (-1);
135 d->destport = htons(port);
138 * Prepend authorization stuff and headers.
139 * Note, must prepend things in reverse order.
141 send_head = sdata;
142 send_tail = (char *)sdata + slen;
144 /* Auth verifier is always auth_null */
145 send_head -= sizeof(*auth);
146 auth = (struct auth_info *)send_head;
147 auth->authtype = htonl(RPCAUTH_NULL);
148 auth->authlen = 0;
150 #if 1
151 /* Auth credentials: always auth unix (as root) */
152 send_head -= sizeof(struct auth_unix);
153 bzero(send_head, sizeof(struct auth_unix));
154 send_head -= sizeof(*auth);
155 auth = (struct auth_info *)send_head;
156 auth->authtype = htonl(RPCAUTH_UNIX);
157 auth->authlen = htonl(sizeof(struct auth_unix));
158 #else
159 /* Auth credentials: always auth_null (XXX OK?) */
160 send_head -= sizeof(*auth);
161 auth = send_head;
162 auth->authtype = htonl(RPCAUTH_NULL);
163 auth->authlen = 0;
164 #endif
166 /* RPC call structure. */
167 send_head -= sizeof(*call);
168 call = (struct rpc_call *)send_head;
169 rpc_xid++;
170 call->rp_xid = htonl(rpc_xid);
171 call->rp_direction = htonl(RPC_CALL);
172 call->rp_rpcvers = htonl(RPC_VER2);
173 call->rp_prog = htonl(prog);
174 call->rp_vers = htonl(vers);
175 call->rp_proc = htonl(proc);
177 /* Make room for the rpc_reply header. */
178 recv_head = rdata;
179 recv_tail = (char *)rdata + rlen;
180 recv_head -= sizeof(*reply);
182 cc = sendrecv(d,
183 sendudp, send_head, send_tail - send_head,
184 recvrpc, recv_head, recv_tail - recv_head);
186 #ifdef RPC_DEBUG
187 if (debug)
188 printf("callrpc: cc=%ld rlen=%lu\n", (long)cc, (u_long)rlen);
189 #endif
190 if (cc == -1)
191 return (-1);
193 if (cc <= sizeof(*reply)) {
194 errno = EBADRPC;
195 return (-1);
198 recv_tail = recv_head + cc;
201 * Check the RPC reply status.
202 * The xid, dir, astatus were already checked.
204 reply = (struct rpc_reply *)recv_head;
205 auth = &reply->rp_u.rpu_rok.rok_auth;
206 x = ntohl(auth->authlen);
207 if (x != 0) {
208 #ifdef RPC_DEBUG
209 if (debug)
210 printf("callrpc: reply auth != NULL\n");
211 #endif
212 errno = EBADRPC;
213 return(-1);
215 x = ntohl(reply->rp_u.rpu_rok.rok_status);
216 if (x != 0) {
217 printf("callrpc: error = %ld\n", (long)x);
218 errno = EBADRPC;
219 return(-1);
221 recv_head += sizeof(*reply);
223 return (ssize_t)(recv_tail - recv_head);
227 * Returns true if packet is the one we're waiting for.
228 * This just checks the XID, direction, acceptance.
229 * Remaining checks are done by callrpc
231 static ssize_t
232 recvrpc(struct iodesc *d, void *pkt, size_t len, time_t tleft)
234 struct rpc_reply *reply;
235 ssize_t n;
236 int x;
238 errno = 0;
239 #ifdef RPC_DEBUG
240 if (debug)
241 printf("recvrpc: called len=%lu\n", (u_long)len);
242 #endif
244 n = readudp(d, pkt, len, tleft);
245 if (n <= (4 * 4))
246 return -1;
248 reply = (struct rpc_reply *)pkt;
250 x = ntohl(reply->rp_xid);
251 if (x != rpc_xid) {
252 #ifdef RPC_DEBUG
253 if (debug)
254 printf("recvrpc: rp_xid %d != xid %d\n", x, rpc_xid);
255 #endif
256 return -1;
259 x = ntohl(reply->rp_direction);
260 if (x != RPC_REPLY) {
261 #ifdef RPC_DEBUG
262 if (debug)
263 printf("recvrpc: rp_direction %d != REPLY\n", x);
264 #endif
265 return -1;
268 x = ntohl(reply->rp_astatus);
269 if (x != RPC_MSGACCEPTED) {
270 errno = ntohl(reply->rp_u.rpu_errno);
271 printf("recvrpc: reject, astat=%d, errno=%d\n", x, errno);
272 return -1;
275 /* Return data count (thus indicating success) */
276 return (n);
280 * Given a pointer to a reply just received,
281 * dig out the IP address/port from the headers.
283 void
284 rpc_fromaddr(void *pkt, struct in_addr *addr, u_short *port)
286 struct hackhdr {
287 /* Tail of IP header: just IP addresses */
288 n_long ip_src;
289 n_long ip_dst;
290 /* UDP header: */
291 u_int16_t uh_sport; /* source port */
292 u_int16_t uh_dport; /* destination port */
293 int16_t uh_ulen; /* udp length */
294 u_int16_t uh_sum; /* udp checksum */
295 /* RPC reply header: */
296 struct rpc_reply rpc;
297 } *hhdr;
299 hhdr = ((struct hackhdr *)pkt) - 1;
300 addr->s_addr = hhdr->ip_src;
301 *port = hhdr->uh_sport;
305 * RPC Portmapper cache
307 #define PMAP_NUM 8 /* need at most 5 pmap entries */
309 int rpc_pmap_num;
310 struct pmap_list {
311 struct in_addr addr; /* server, net order */
312 u_int prog; /* host order */
313 u_int vers; /* host order */
314 int port; /* host order */
315 } rpc_pmap_list[PMAP_NUM];
318 * return port number in host order, or -1
320 * Parameters:
321 * addr: server, net order
322 * prog: host order
323 * vers: host order
326 rpc_pmap_getcache(struct in_addr addr, u_int prog, u_int vers)
328 struct pmap_list *pl;
330 for (pl = rpc_pmap_list; pl < &rpc_pmap_list[rpc_pmap_num]; pl++) {
331 if (pl->addr.s_addr == addr.s_addr &&
332 pl->prog == prog && pl->vers == vers )
334 return (pl->port);
337 return (-1);
341 * Parameters:
342 * addr: server, net order
343 * prog: host order
344 * vers: host order
345 * port: host order
347 void
348 rpc_pmap_putcache(struct in_addr addr, u_int prog, u_int vers, int port)
350 struct pmap_list *pl;
352 /* Don't overflow cache... */
353 if (rpc_pmap_num >= PMAP_NUM) {
354 /* ... just re-use the last entry. */
355 rpc_pmap_num = PMAP_NUM - 1;
356 #ifdef RPC_DEBUG
357 printf("rpc_pmap_putcache: cache overflow\n");
358 #endif
361 pl = &rpc_pmap_list[rpc_pmap_num];
362 rpc_pmap_num++;
364 /* Cache answer */
365 pl->addr = addr;
366 pl->prog = prog;
367 pl->vers = vers;
368 pl->port = port;
373 * Request a port number from the port mapper.
374 * Returns the port in host order.
376 * Parameters:
377 * prog: host order
378 * vers: host order
381 rpc_getport(struct iodesc *d, n_long prog, n_long vers)
383 struct args {
384 n_long prog; /* call program */
385 n_long vers; /* call version */
386 n_long proto; /* call protocol */
387 n_long port; /* call port (unused) */
388 } *args;
389 struct res {
390 n_long port;
391 } *res;
392 struct {
393 n_long h[RPC_HEADER_WORDS];
394 struct args d;
395 } sdata;
396 struct {
397 n_long h[RPC_HEADER_WORDS];
398 struct res d;
399 n_long pad;
400 } rdata;
401 ssize_t cc;
402 int port;
404 #ifdef RPC_DEBUG
405 if (debug)
406 printf("getport: prog=0x%x vers=%d\n", prog, vers);
407 #endif
409 /* This one is fixed forever. */
410 if (prog == PMAPPROG)
411 return (PMAPPORT);
413 /* Try for cached answer first */
414 port = rpc_pmap_getcache(d->destip, prog, vers);
415 if (port != -1)
416 return (port);
418 args = &sdata.d;
419 args->prog = htonl(prog);
420 args->vers = htonl(vers);
421 args->proto = htonl(IPPROTO_UDP);
422 args->port = 0;
423 res = &rdata.d;
425 cc = rpc_call(d, PMAPPROG, PMAPVERS, PMAPPROC_GETPORT,
426 args, sizeof(*args), res, sizeof(*res));
427 if (cc < sizeof(*res)) {
428 printf("getport: %s", strerror(errno));
429 errno = EBADRPC;
430 return (-1);
432 port = (int)ntohl(res->port);
434 rpc_pmap_putcache(d->destip, prog, vers, port);
436 return (port);