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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
|
/* $OpenBSD: krpc_subr.c,v 1.11 2001/06/27 05:45:00 nate Exp $ */
/* $NetBSD: krpc_subr.c,v 1.12.4.1 1996/06/07 00:52:26 cgd Exp $ */
/*
* Copyright (c) 1995 Gordon Ross, Adam Glass
* Copyright (c) 1992 Regents of the University of California.
* All rights reserved.
*
* This software was developed by the Computer Systems Engineering group
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
* contributed to Berkeley.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Lawrence Berkeley Laboratory and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* partially based on:
* libnetboot/rpc.c
* @(#) Header: rpc.c,v 1.12 93/09/28 08:31:56 leres Exp (LBL)
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/ioctl.h>
#include <sys/proc.h>
#include <sys/mount.h>
#include <sys/mbuf.h>
#include <sys/reboot.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <net/if.h>
#include <netinet/in.h>
#include <nfs/rpcv2.h>
#include <nfs/krpc.h>
#include <nfs/xdr_subs.h>
#include <dev/rndvar.h>
/*
* Kernel support for Sun RPC
*
* Used currently for bootstrapping in nfs diskless configurations.
*/
/*
* Generic RPC headers
*/
struct auth_info {
u_int32_t authtype; /* auth type */
u_int32_t authlen; /* auth length */
};
struct auth_unix {
int32_t ua_time;
int32_t ua_hostname; /* null */
int32_t ua_uid;
int32_t ua_gid;
int32_t ua_gidlist; /* null */
};
struct rpc_call {
u_int32_t rp_xid; /* request transaction id */
int32_t rp_direction; /* call direction (0) */
u_int32_t rp_rpcvers; /* rpc version (2) */
u_int32_t rp_prog; /* program */
u_int32_t rp_vers; /* version */
u_int32_t rp_proc; /* procedure */
struct auth_info rpc_auth;
struct auth_unix rpc_unix;
struct auth_info rpc_verf;
};
struct rpc_reply {
u_int32_t rp_xid; /* request transaction id */
int32_t rp_direction; /* call direction (1) */
int32_t rp_astatus; /* accept status (0: accepted) */
union {
u_int32_t rpu_errno;
struct {
struct auth_info rok_auth;
u_int32_t rok_status;
} rpu_rok;
} rp_u;
};
#define rp_errno rp_u.rpu_errno
#define rp_auth rp_u.rpu_rok.rok_auth
#define rp_status rp_u.rpu_rok.rok_status
#define MIN_REPLY_HDR 16 /* xid, dir, astat, errno */
/*
* What is the longest we will wait before re-sending a request?
* Note this is also the frequency of "RPC timeout" messages.
* The re-send loop count sup linearly to this maximum, so the
* first complaint will happen after (1+2+3+4+5)=15 seconds.
*/
#define MAX_RESEND_DELAY 5 /* seconds */
/*
* Call portmap to lookup a port number for a particular rpc program
* Returns non-zero error on failure.
*/
int
krpc_portmap(sin, prog, vers, portp)
struct sockaddr_in *sin; /* server address */
u_int prog, vers; /* host order */
u_int16_t *portp; /* network order */
{
struct sdata {
u_int32_t prog; /* call program */
u_int32_t vers; /* call version */
u_int32_t proto; /* call protocol */
u_int32_t port; /* call port (unused) */
} *sdata;
struct rdata {
u_int16_t pad;
u_int16_t port;
} *rdata;
struct mbuf *m;
int error;
/* The portmapper port is fixed. */
if (prog == PMAPPROG) {
*portp = htons(PMAPPORT);
return 0;
}
m = m_get(M_WAIT, MT_DATA);
sdata = mtod(m, struct sdata *);
m->m_len = sizeof(*sdata);
/* Do the RPC to get it. */
sdata->prog = txdr_unsigned(prog);
sdata->vers = txdr_unsigned(vers);
sdata->proto = txdr_unsigned(IPPROTO_UDP);
sdata->port = 0;
sin->sin_port = htons(PMAPPORT);
error = krpc_call(sin, PMAPPROG, PMAPVERS,
PMAPPROC_GETPORT, &m, NULL);
if (error)
return error;
if (m->m_len < sizeof(*rdata)) {
m = m_pullup(m, sizeof(*rdata));
if (m == NULL)
return ENOBUFS;
}
rdata = mtod(m, struct rdata *);
*portp = rdata->port;
m_freem(m);
return 0;
}
/*
* Do a remote procedure call (RPC) and wait for its reply.
* If from_p is non-null, then we are doing broadcast, and
* the address from whence the response came is saved there.
*/
int
krpc_call(sa, prog, vers, func, data, from_p)
struct sockaddr_in *sa;
u_int prog, vers, func;
struct mbuf **data; /* input/output */
struct mbuf **from_p; /* output */
{
struct socket *so;
struct sockaddr_in *sin;
struct mbuf *m, *nam, *mhead, *from, *mopt;
struct rpc_call *call;
struct rpc_reply *reply;
struct uio auio;
int error, rcvflg, timo, secs, len;
static u_int32_t xid = 0;
u_int32_t newxid;
int *ip;
struct timeval *tv;
/*
* Validate address family.
* Sorry, this is INET specific...
*/
if (sa->sin_family != AF_INET)
return (EAFNOSUPPORT);
/* Free at end if not null. */
nam = mhead = NULL;
from = NULL;
/*
* Create socket and set its receive timeout.
*/
if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0)))
goto out;
m = m_get(M_WAIT, MT_SOOPTS);
tv = mtod(m, struct timeval *);
m->m_len = sizeof(*tv);
tv->tv_sec = 1;
tv->tv_usec = 0;
if ((error = sosetopt(so, SOL_SOCKET, SO_RCVTIMEO, m)))
goto out;
/*
* Enable broadcast if necessary.
*/
if (from_p) {
int32_t *on;
m = m_get(M_WAIT, MT_SOOPTS);
on = mtod(m, int32_t *);
m->m_len = sizeof(*on);
*on = 1;
if ((error = sosetopt(so, SOL_SOCKET, SO_BROADCAST, m)))
goto out;
}
/*
* Bind the local endpoint to a reserved port,
* because some NFS servers refuse requests from
* non-reserved (non-privileged) ports.
*/
m = m_getclr(M_WAIT, MT_SONAME);
sin = mtod(m, struct sockaddr_in *);
sin->sin_len = m->m_len = sizeof(*sin);
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = INADDR_ANY;
MGET(mopt, M_WAIT, MT_SOOPTS);
mopt->m_len = sizeof(int);
ip = mtod(mopt, int *);
*ip = IP_PORTRANGE_LOW;
error = sosetopt(so, IPPROTO_IP, IP_PORTRANGE, mopt);
if (error)
goto out;
MGET(m, M_WAIT, MT_SONAME);
sin = mtod(m, struct sockaddr_in *);
sin->sin_len = m->m_len = sizeof (struct sockaddr_in);
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = INADDR_ANY;
sin->sin_port = htons(0);
error = sobind(so, m);
m_freem(m);
if (error) {
printf("bind failed\n");
goto out;
}
MGET(mopt, M_WAIT, MT_SOOPTS);
mopt->m_len = sizeof(int);
ip = mtod(mopt, int *);
*ip = IP_PORTRANGE_DEFAULT;
error = sosetopt(so, IPPROTO_IP, IP_PORTRANGE, mopt);
if (error)
goto out;
/*
* Setup socket address for the server.
*/
nam = m_get(M_WAIT, MT_SONAME);
sin = mtod(nam, struct sockaddr_in *);
bcopy((caddr_t)sa, (caddr_t)sin, (nam->m_len = sa->sin_len));
/*
* Prepend RPC message header.
*/
mhead = m_gethdr(M_WAIT, MT_DATA);
mhead->m_next = *data;
call = mtod(mhead, struct rpc_call *);
mhead->m_len = sizeof(*call);
bzero((caddr_t)call, sizeof(*call));
/* rpc_call part */
while ((newxid = arc4random()) == xid);
xid = newxid;
call->rp_xid = txdr_unsigned(xid);
/* call->rp_direction = 0; */
call->rp_rpcvers = txdr_unsigned(2);
call->rp_prog = txdr_unsigned(prog);
call->rp_vers = txdr_unsigned(vers);
call->rp_proc = txdr_unsigned(func);
/* rpc_auth part (auth_unix as root) */
call->rpc_auth.authtype = txdr_unsigned(RPCAUTH_UNIX);
call->rpc_auth.authlen = txdr_unsigned(sizeof(struct auth_unix));
/* rpc_verf part (auth_null) */
call->rpc_verf.authtype = 0;
call->rpc_verf.authlen = 0;
/*
* Setup packet header
*/
len = 0;
m = mhead;
while (m) {
len += m->m_len;
m = m->m_next;
}
mhead->m_pkthdr.len = len;
mhead->m_pkthdr.rcvif = NULL;
/*
* Send it, repeatedly, until a reply is received,
* but delay each re-send by an increasing amount.
* If the delay hits the maximum, start complaining.
*/
timo = 0;
for (;;) {
/* Send RPC request (or re-send). */
m = m_copym(mhead, 0, M_COPYALL, M_WAIT);
if (m == NULL) {
error = ENOBUFS;
goto out;
}
error = sosend(so, nam, NULL, m, NULL, 0);
if (error) {
printf("krpc_call: sosend: %d\n", error);
goto out;
}
m = NULL;
/* Determine new timeout. */
if (timo < MAX_RESEND_DELAY)
timo++;
else
printf("RPC timeout for server 0x%x\n",
ntohl(sin->sin_addr.s_addr));
/*
* Wait for up to timo seconds for a reply.
* The socket receive timeout was set to 1 second.
*/
secs = timo;
while (secs > 0) {
if (from) {
m_freem(from);
from = NULL;
}
if (m) {
m_freem(m);
m = NULL;
}
auio.uio_resid = len = 1<<16;
rcvflg = 0;
error = soreceive(so, &from, &auio, &m, NULL, &rcvflg);
if (error == EWOULDBLOCK) {
secs--;
continue;
}
if (error)
goto out;
len -= auio.uio_resid;
/* Does the reply contain at least a header? */
if (len < MIN_REPLY_HDR)
continue;
if (m->m_len < MIN_REPLY_HDR)
continue;
reply = mtod(m, struct rpc_reply *);
/* Is it the right reply? */
if (reply->rp_direction != txdr_unsigned(RPC_REPLY))
continue;
if (reply->rp_xid != txdr_unsigned(xid))
continue;
/* Was RPC accepted? (authorization OK) */
if (reply->rp_astatus != 0) {
error = fxdr_unsigned(u_int32_t, reply->rp_errno);
printf("rpc denied, error=%d\n", error);
continue;
}
/* Did the call succeed? */
if (reply->rp_status != 0) {
error = fxdr_unsigned(u_int32_t, reply->rp_status);
printf("rpc denied, status=%d\n", error);
continue;
}
goto gotreply; /* break two levels */
} /* while secs */
} /* forever send/receive */
error = ETIMEDOUT;
goto out;
gotreply:
/*
* Get RPC reply header into first mbuf,
* get its length, then strip it off.
*/
len = sizeof(*reply);
if (m->m_len < len) {
m = m_pullup(m, len);
if (m == NULL) {
error = ENOBUFS;
goto out;
}
}
reply = mtod(m, struct rpc_reply *);
if (reply->rp_auth.authtype != 0) {
len += fxdr_unsigned(u_int32_t, reply->rp_auth.authlen);
len = (len + 3) & ~3; /* XXX? */
}
m_adj(m, len);
/* result */
*data = m;
if (from_p) {
*from_p = from;
from = NULL;
}
out:
if (nam) m_freem(nam);
if (mhead) m_freem(mhead);
if (from) m_freem(from);
soclose(so);
return error;
}
/*
* eXternal Data Representation routines.
* (but with non-standard args...)
*/
/*
* String representation for RPC.
*/
struct xdr_string {
u_int32_t len; /* length without null or padding */
char data[4]; /* data (longer, of course) */
/* data is padded to a long-word boundary */
};
struct mbuf *
xdr_string_encode(str, len)
char *str;
int len;
{
struct mbuf *m;
struct xdr_string *xs;
int dlen; /* padded string length */
int mlen; /* message length */
dlen = (len + 3) & ~3;
mlen = dlen + 4;
if (mlen > MCLBYTES) /* If too big, we just can't do it. */
return (NULL);
m = m_get(M_WAIT, MT_DATA);
if (mlen > MLEN) {
MCLGET(m, M_WAIT);
if ((m->m_flags & M_EXT) == 0) {
(void) m_free(m); /* There can be only one. */
return (NULL);
}
}
xs = mtod(m, struct xdr_string *);
m->m_len = mlen;
xs->len = txdr_unsigned(len);
bcopy(str, xs->data, len);
return (m);
}
struct mbuf *
xdr_string_decode(m, str, len_p)
struct mbuf *m;
char *str;
int *len_p; /* bufsize - 1 */
{
struct xdr_string *xs;
int mlen; /* message length */
int slen; /* string length */
if (m->m_len < 4) {
m = m_pullup(m, 4);
if (m == NULL)
return (NULL);
}
xs = mtod(m, struct xdr_string *);
slen = fxdr_unsigned(u_int32_t, xs->len);
mlen = 4 + ((slen + 3) & ~3);
if (slen > *len_p)
slen = *len_p;
if (slen > m->m_pkthdr.len) {
m_freem(m);
return (NULL);
}
m_copydata(m, 4, slen, str);
m_adj(m, mlen);
str[slen] = '\0';
*len_p = slen;
return (m);
}
/*
* Inet address in RPC messages
* (Note, really four ints, NOT chars. Blech.)
*/
struct xdr_inaddr {
u_int32_t atype;
u_int32_t addr[4];
};
struct mbuf *
xdr_inaddr_encode(ia)
struct in_addr *ia; /* already in network order */
{
struct mbuf *m;
struct xdr_inaddr *xi;
u_int8_t *cp;
u_int32_t *ip;
m = m_get(M_WAIT, MT_DATA);
xi = mtod(m, struct xdr_inaddr *);
m->m_len = sizeof(*xi);
xi->atype = txdr_unsigned(1);
ip = xi->addr;
cp = (u_int8_t *)&ia->s_addr;
*ip++ = txdr_unsigned(*cp++);
*ip++ = txdr_unsigned(*cp++);
*ip++ = txdr_unsigned(*cp++);
*ip++ = txdr_unsigned(*cp++);
return (m);
}
struct mbuf *
xdr_inaddr_decode(m, ia)
struct mbuf *m;
struct in_addr *ia; /* already in network order */
{
struct xdr_inaddr *xi;
u_int8_t *cp;
u_int32_t *ip;
if (m->m_len < sizeof(*xi)) {
m = m_pullup(m, sizeof(*xi));
if (m == NULL)
return (NULL);
}
xi = mtod(m, struct xdr_inaddr *);
if (xi->atype != txdr_unsigned(1)) {
ia->s_addr = INADDR_ANY;
goto out;
}
ip = xi->addr;
cp = (u_int8_t *)&ia->s_addr;
*cp++ = fxdr_unsigned(u_int8_t, *ip++);
*cp++ = fxdr_unsigned(u_int8_t, *ip++);
*cp++ = fxdr_unsigned(u_int8_t, *ip++);
*cp++ = fxdr_unsigned(u_int8_t, *ip++);
out:
m_adj(m, sizeof(*xi));
return (m);
}
|