krpc_subr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /* $OpenBSD: krpc_subr.c,v 1.28 2015/07/15 22:16:42 deraadt Exp $ */
  2. /* $NetBSD: krpc_subr.c,v 1.12.4.1 1996/06/07 00:52:26 cgd Exp $ */
  3. /*
  4. * Copyright (c) 1995 Gordon Ross, Adam Glass
  5. * Copyright (c) 1992 Regents of the University of California.
  6. * All rights reserved.
  7. *
  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.
  11. *
  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.
  27. *
  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.
  39. *
  40. * partially based on:
  41. * libnetboot/rpc.c
  42. * @(#) Header: rpc.c,v 1.12 93/09/28 08:31:56 leres Exp (LBL)
  43. */
  44. #include <sys/param.h>
  45. #include <sys/systm.h>
  46. #include <sys/conf.h>
  47. #include <sys/ioctl.h>
  48. #include <sys/proc.h>
  49. #include <sys/mount.h>
  50. #include <sys/mbuf.h>
  51. #include <sys/reboot.h>
  52. #include <sys/socket.h>
  53. #include <sys/socketvar.h>
  54. #include <netinet/in.h>
  55. #include <nfs/rpcv2.h>
  56. #include <nfs/krpc.h>
  57. #include <nfs/xdr_subs.h>
  58. #include <crypto/idgen.h>
  59. /*
  60. * Kernel support for Sun RPC
  61. *
  62. * Used currently for bootstrapping in nfs diskless configurations.
  63. */
  64. /*
  65. * Generic RPC headers
  66. */
  67. struct auth_info {
  68. u_int32_t authtype; /* auth type */
  69. u_int32_t authlen; /* auth length */
  70. };
  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 */
  77. };
  78. struct rpc_call {
  79. u_int32_t rp_xid; /* request transaction id */
  80. int32_t rp_direction; /* call direction (0) */
  81. u_int32_t rp_rpcvers; /* rpc version (2) */
  82. u_int32_t rp_prog; /* program */
  83. u_int32_t rp_vers; /* version */
  84. u_int32_t rp_proc; /* procedure */
  85. struct auth_info rpc_auth;
  86. struct auth_unix rpc_unix;
  87. struct auth_info rpc_verf;
  88. };
  89. struct rpc_reply {
  90. u_int32_t rp_xid; /* request transaction id */
  91. int32_t rp_direction; /* call direction (1) */
  92. int32_t rp_astatus; /* accept status (0: accepted) */
  93. union {
  94. u_int32_t rpu_errno;
  95. struct {
  96. struct auth_info rok_auth;
  97. u_int32_t rok_status;
  98. } rpu_rok;
  99. } rp_u;
  100. };
  101. #define rp_errno rp_u.rpu_errno
  102. #define rp_auth rp_u.rpu_rok.rok_auth
  103. #define rp_status rp_u.rpu_rok.rok_status
  104. #define MIN_REPLY_HDR 16 /* xid, dir, astat, errno */
  105. u_int32_t krpc_get_xid(void);
  106. /*
  107. * Return an unpredictable XID.
  108. */
  109. u_int32_t
  110. krpc_get_xid(void)
  111. {
  112. static struct idgen32_ctx krpc_xid_ctx;
  113. static int called = 0;
  114. if (!called) {
  115. called = 1;
  116. idgen32_init(&krpc_xid_ctx);
  117. }
  118. return idgen32(&krpc_xid_ctx);
  119. }
  120. /*
  121. * What is the longest we will wait before re-sending a request?
  122. * Note this is also the frequency of "RPC timeout" messages.
  123. * The re-send loop count sup linearly to this maximum, so the
  124. * first complaint will happen after (1+2+3+4+5)=15 seconds.
  125. */
  126. #define MAX_RESEND_DELAY 5 /* seconds */
  127. /*
  128. * Call portmap to lookup a port number for a particular rpc program
  129. * Returns non-zero error on failure.
  130. */
  131. int
  132. krpc_portmap(struct sockaddr_in *sin, u_int prog, u_int vers, u_int16_t *portp)
  133. {
  134. struct sdata {
  135. u_int32_t prog; /* call program */
  136. u_int32_t vers; /* call version */
  137. u_int32_t proto; /* call protocol */
  138. u_int32_t port; /* call port (unused) */
  139. } *sdata;
  140. struct rdata {
  141. u_int16_t pad;
  142. u_int16_t port;
  143. } *rdata;
  144. struct mbuf *m;
  145. int error;
  146. /* The portmapper port is fixed. */
  147. if (prog == PMAPPROG) {
  148. *portp = htons(PMAPPORT);
  149. return 0;
  150. }
  151. m = m_get(M_WAIT, MT_DATA);
  152. sdata = mtod(m, struct sdata *);
  153. m->m_len = sizeof(*sdata);
  154. /* Do the RPC to get it. */
  155. sdata->prog = txdr_unsigned(prog);
  156. sdata->vers = txdr_unsigned(vers);
  157. sdata->proto = txdr_unsigned(IPPROTO_UDP);
  158. sdata->port = 0;
  159. sin->sin_port = htons(PMAPPORT);
  160. error = krpc_call(sin, PMAPPROG, PMAPVERS,
  161. PMAPPROC_GETPORT, &m, NULL, -1);
  162. if (error)
  163. return error;
  164. if (m->m_len < sizeof(*rdata)) {
  165. m = m_pullup(m, sizeof(*rdata));
  166. if (m == NULL)
  167. return ENOBUFS;
  168. }
  169. rdata = mtod(m, struct rdata *);
  170. *portp = rdata->port;
  171. m_freem(m);
  172. return 0;
  173. }
  174. /*
  175. * Do a remote procedure call (RPC) and wait for its reply.
  176. * If from_p is non-null, then we are doing broadcast, and
  177. * the address from whence the response came is saved there.
  178. * data: input/output
  179. * from_p: output
  180. */
  181. int
  182. krpc_call(struct sockaddr_in *sa, u_int prog, u_int vers, u_int func,
  183. struct mbuf **data, struct mbuf **from_p, int retries)
  184. {
  185. struct socket *so;
  186. struct sockaddr_in *sin;
  187. struct mbuf *m, *nam, *mhead, *from, *mopt;
  188. struct rpc_call *call;
  189. struct rpc_reply *reply;
  190. struct uio auio;
  191. int error, rcvflg, timo, secs, len;
  192. static u_int32_t xid = 0;
  193. char addr[INET_ADDRSTRLEN];
  194. int *ip;
  195. struct timeval tv;
  196. /*
  197. * Validate address family.
  198. * Sorry, this is INET specific...
  199. */
  200. if (sa->sin_family != AF_INET)
  201. return (EAFNOSUPPORT);
  202. /* Free at end if not null. */
  203. nam = mhead = NULL;
  204. from = NULL;
  205. /*
  206. * Create socket and set its receive timeout.
  207. */
  208. if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0)))
  209. goto out;
  210. m = m_get(M_WAIT, MT_SOOPTS);
  211. tv.tv_sec = 1;
  212. tv.tv_usec = 0;
  213. memcpy(mtod(m, struct timeval *), &tv, sizeof tv);
  214. m->m_len = sizeof(tv);
  215. if ((error = sosetopt(so, SOL_SOCKET, SO_RCVTIMEO, m)))
  216. goto out;
  217. /*
  218. * Enable broadcast if necessary.
  219. */
  220. if (from_p) {
  221. int32_t *on;
  222. m = m_get(M_WAIT, MT_SOOPTS);
  223. on = mtod(m, int32_t *);
  224. m->m_len = sizeof(*on);
  225. *on = 1;
  226. if ((error = sosetopt(so, SOL_SOCKET, SO_BROADCAST, m)))
  227. goto out;
  228. }
  229. /*
  230. * Bind the local endpoint to a reserved port,
  231. * because some NFS servers refuse requests from
  232. * non-reserved (non-privileged) ports.
  233. */
  234. MGET(mopt, M_WAIT, MT_SOOPTS);
  235. mopt->m_len = sizeof(int);
  236. ip = mtod(mopt, int *);
  237. *ip = IP_PORTRANGE_LOW;
  238. error = sosetopt(so, IPPROTO_IP, IP_PORTRANGE, mopt);
  239. if (error)
  240. goto out;
  241. MGET(m, M_WAIT, MT_SONAME);
  242. sin = mtod(m, struct sockaddr_in *);
  243. sin->sin_len = m->m_len = sizeof (struct sockaddr_in);
  244. sin->sin_family = AF_INET;
  245. sin->sin_addr.s_addr = INADDR_ANY;
  246. sin->sin_port = htons(0);
  247. error = sobind(so, m, &proc0);
  248. m_freem(m);
  249. if (error) {
  250. printf("bind failed\n");
  251. goto out;
  252. }
  253. MGET(mopt, M_WAIT, MT_SOOPTS);
  254. mopt->m_len = sizeof(int);
  255. ip = mtod(mopt, int *);
  256. *ip = IP_PORTRANGE_DEFAULT;
  257. error = sosetopt(so, IPPROTO_IP, IP_PORTRANGE, mopt);
  258. if (error)
  259. goto out;
  260. /*
  261. * Setup socket address for the server.
  262. */
  263. nam = m_get(M_WAIT, MT_SONAME);
  264. sin = mtod(nam, struct sockaddr_in *);
  265. bcopy(sa, sin, (nam->m_len = sa->sin_len));
  266. /*
  267. * Prepend RPC message header.
  268. */
  269. mhead = m_gethdr(M_WAIT, MT_DATA);
  270. mhead->m_next = *data;
  271. call = mtod(mhead, struct rpc_call *);
  272. mhead->m_len = sizeof(*call);
  273. memset(call, 0, sizeof(*call));
  274. /* rpc_call part */
  275. xid = krpc_get_xid();
  276. call->rp_xid = txdr_unsigned(xid);
  277. /* call->rp_direction = 0; */
  278. call->rp_rpcvers = txdr_unsigned(2);
  279. call->rp_prog = txdr_unsigned(prog);
  280. call->rp_vers = txdr_unsigned(vers);
  281. call->rp_proc = txdr_unsigned(func);
  282. /* rpc_auth part (auth_unix as root) */
  283. call->rpc_auth.authtype = txdr_unsigned(RPCAUTH_UNIX);
  284. call->rpc_auth.authlen = txdr_unsigned(sizeof(struct auth_unix));
  285. /* rpc_verf part (auth_null) */
  286. call->rpc_verf.authtype = 0;
  287. call->rpc_verf.authlen = 0;
  288. /*
  289. * Setup packet header
  290. */
  291. len = 0;
  292. m = mhead;
  293. while (m) {
  294. len += m->m_len;
  295. m = m->m_next;
  296. }
  297. mhead->m_pkthdr.len = len;
  298. mhead->m_pkthdr.ph_ifidx = 0;
  299. /*
  300. * Send it, repeatedly, until a reply is received,
  301. * but delay each re-send by an increasing amount.
  302. * If the delay hits the maximum, start complaining.
  303. */
  304. for (timo = 0; retries; retries--) {
  305. /* Send RPC request (or re-send). */
  306. m = m_copym(mhead, 0, M_COPYALL, M_WAIT);
  307. if (m == NULL) {
  308. error = ENOBUFS;
  309. goto out;
  310. }
  311. error = sosend(so, nam, NULL, m, NULL, 0);
  312. if (error) {
  313. printf("krpc_call: sosend: %d\n", error);
  314. goto out;
  315. }
  316. m = NULL;
  317. /* Determine new timeout. */
  318. if (timo < MAX_RESEND_DELAY)
  319. timo++;
  320. else
  321. printf("RPC timeout for server %s (0x%x) prog %u\n",
  322. inet_ntop(AF_INET, &sin->sin_addr,
  323. addr, sizeof(addr)),
  324. ntohl(sin->sin_addr.s_addr), prog);
  325. /*
  326. * Wait for up to timo seconds for a reply.
  327. * The socket receive timeout was set to 1 second.
  328. */
  329. secs = timo;
  330. while (secs > 0) {
  331. m_freem(from);
  332. from = NULL;
  333. m_freem(m);
  334. m = NULL;
  335. auio.uio_resid = len = 1<<16;
  336. auio.uio_procp = NULL;
  337. rcvflg = 0;
  338. error = soreceive(so, &from, &auio, &m, NULL, &rcvflg,
  339. 0);
  340. if (error == EWOULDBLOCK) {
  341. secs--;
  342. continue;
  343. }
  344. if (error)
  345. goto out;
  346. len -= auio.uio_resid;
  347. /* Does the reply contain at least a header? */
  348. if (len < MIN_REPLY_HDR)
  349. continue;
  350. if (m->m_len < MIN_REPLY_HDR)
  351. continue;
  352. reply = mtod(m, struct rpc_reply *);
  353. /* Is it the right reply? */
  354. if (reply->rp_direction != txdr_unsigned(RPC_REPLY))
  355. continue;
  356. if (reply->rp_xid != txdr_unsigned(xid))
  357. continue;
  358. /* Was RPC accepted? (authorization OK) */
  359. if (reply->rp_astatus != 0) {
  360. error = fxdr_unsigned(u_int32_t, reply->rp_errno);
  361. printf("rpc denied, error=%d\n", error);
  362. continue;
  363. }
  364. /* Did the call succeed? */
  365. if (reply->rp_status != 0) {
  366. error = fxdr_unsigned(u_int32_t, reply->rp_status);
  367. printf("rpc denied, status=%d\n", error);
  368. continue;
  369. }
  370. goto gotreply; /* break two levels */
  371. } /* while secs */
  372. } /* forever send/receive */
  373. error = ETIMEDOUT;
  374. goto out;
  375. gotreply:
  376. /*
  377. * Get RPC reply header into first mbuf,
  378. * get its length, then strip it off.
  379. */
  380. len = sizeof(*reply);
  381. if (m->m_len < len) {
  382. m = m_pullup(m, len);
  383. if (m == NULL) {
  384. error = ENOBUFS;
  385. goto out;
  386. }
  387. }
  388. reply = mtod(m, struct rpc_reply *);
  389. if (reply->rp_auth.authtype != 0) {
  390. len += fxdr_unsigned(u_int32_t, reply->rp_auth.authlen);
  391. len = (len + 3) & ~3; /* XXX? */
  392. }
  393. m_adj(m, len);
  394. /* result */
  395. *data = m;
  396. if (from_p && error == 0) {
  397. *from_p = from;
  398. from = NULL;
  399. }
  400. out:
  401. m_freem(nam);
  402. m_freem(mhead);
  403. m_freem(from);
  404. soclose(so);
  405. return error;
  406. }
  407. /*
  408. * eXternal Data Representation routines.
  409. * (but with non-standard args...)
  410. */
  411. /*
  412. * String representation for RPC.
  413. */
  414. struct xdr_string {
  415. u_int32_t len; /* length without null or padding */
  416. char data[4]; /* data (longer, of course) */
  417. /* data is padded to a long-word boundary */
  418. };
  419. struct mbuf *
  420. xdr_string_encode(char *str, int len)
  421. {
  422. struct mbuf *m;
  423. struct xdr_string *xs;
  424. int dlen; /* padded string length */
  425. int mlen; /* message length */
  426. dlen = (len + 3) & ~3;
  427. mlen = dlen + 4;
  428. if (mlen > MCLBYTES) /* If too big, we just can't do it. */
  429. return (NULL);
  430. m = m_get(M_WAIT, MT_DATA);
  431. if (mlen > MLEN) {
  432. MCLGET(m, M_WAIT);
  433. if ((m->m_flags & M_EXT) == 0) {
  434. (void) m_free(m); /* There can be only one. */
  435. return (NULL);
  436. }
  437. }
  438. xs = mtod(m, struct xdr_string *);
  439. m->m_len = mlen;
  440. xs->len = txdr_unsigned(len);
  441. bcopy(str, xs->data, len);
  442. return (m);
  443. }
  444. struct mbuf *
  445. xdr_string_decode(struct mbuf *m, char *str, int *len_p)
  446. {
  447. struct xdr_string *xs;
  448. int mlen; /* message length */
  449. int slen; /* string length */
  450. if (m->m_len < 4) {
  451. m = m_pullup(m, 4);
  452. if (m == NULL)
  453. return (NULL);
  454. }
  455. xs = mtod(m, struct xdr_string *);
  456. slen = fxdr_unsigned(u_int32_t, xs->len);
  457. mlen = 4 + ((slen + 3) & ~3);
  458. if (slen > *len_p)
  459. slen = *len_p;
  460. if (slen > m->m_pkthdr.len) {
  461. m_freem(m);
  462. return (NULL);
  463. }
  464. m_copydata(m, 4, slen, str);
  465. m_adj(m, mlen);
  466. str[slen] = '\0';
  467. *len_p = slen;
  468. return (m);
  469. }
  470. /*
  471. * Inet address in RPC messages
  472. * (Note, really four ints, NOT chars. Blech.)
  473. */
  474. struct xdr_inaddr {
  475. u_int32_t atype;
  476. u_int32_t addr[4];
  477. };
  478. struct mbuf *
  479. xdr_inaddr_encode(struct in_addr *ia)
  480. {
  481. struct mbuf *m;
  482. struct xdr_inaddr *xi;
  483. u_int8_t *cp;
  484. u_int32_t *ip;
  485. m = m_get(M_WAIT, MT_DATA);
  486. xi = mtod(m, struct xdr_inaddr *);
  487. m->m_len = sizeof(*xi);
  488. xi->atype = txdr_unsigned(1);
  489. ip = xi->addr;
  490. cp = (u_int8_t *)&ia->s_addr;
  491. *ip++ = txdr_unsigned(*cp++);
  492. *ip++ = txdr_unsigned(*cp++);
  493. *ip++ = txdr_unsigned(*cp++);
  494. *ip++ = txdr_unsigned(*cp++);
  495. return (m);
  496. }
  497. struct mbuf *
  498. xdr_inaddr_decode(struct mbuf *m, struct in_addr *ia)
  499. {
  500. struct xdr_inaddr *xi;
  501. u_int8_t *cp;
  502. u_int32_t *ip;
  503. if (m->m_len < sizeof(*xi)) {
  504. m = m_pullup(m, sizeof(*xi));
  505. if (m == NULL)
  506. return (NULL);
  507. }
  508. xi = mtod(m, struct xdr_inaddr *);
  509. if (xi->atype != txdr_unsigned(1)) {
  510. ia->s_addr = INADDR_ANY;
  511. goto out;
  512. }
  513. ip = xi->addr;
  514. cp = (u_int8_t *)&ia->s_addr;
  515. *cp++ = fxdr_unsigned(u_int8_t, *ip++);
  516. *cp++ = fxdr_unsigned(u_int8_t, *ip++);
  517. *cp++ = fxdr_unsigned(u_int8_t, *ip++);
  518. *cp++ = fxdr_unsigned(u_int8_t, *ip++);
  519. out:
  520. m_adj(m, sizeof(*xi));
  521. return (m);
  522. }