krpc_subr.c revision 1.38 1 1.38 maxv /* $NetBSD: krpc_subr.c,v 1.38 2015/03/06 19:03:30 maxv Exp $ */
2 1.3 cgd
3 1.1 glass /*
4 1.8 gwr * Copyright (c) 1995 Gordon Ross, Adam Glass
5 1.1 glass * Copyright (c) 1992 Regents of the University of California.
6 1.1 glass * All rights reserved.
7 1.1 glass *
8 1.1 glass * This software was developed by the Computer Systems Engineering group
9 1.1 glass * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10 1.1 glass * contributed to Berkeley.
11 1.1 glass *
12 1.1 glass * Redistribution and use in source and binary forms, with or without
13 1.1 glass * modification, are permitted provided that the following conditions
14 1.1 glass * are met:
15 1.1 glass * 1. Redistributions of source code must retain the above copyright
16 1.1 glass * notice, this list of conditions and the following disclaimer.
17 1.1 glass * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 glass * notice, this list of conditions and the following disclaimer in the
19 1.1 glass * documentation and/or other materials provided with the distribution.
20 1.1 glass * 3. All advertising materials mentioning features or use of this software
21 1.1 glass * must display the following acknowledgement:
22 1.1 glass * This product includes software developed by the University of
23 1.1 glass * California, Lawrence Berkeley Laboratory and its contributors.
24 1.1 glass * 4. Neither the name of the University nor the names of its contributors
25 1.1 glass * may be used to endorse or promote products derived from this software
26 1.1 glass * without specific prior written permission.
27 1.1 glass *
28 1.1 glass * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.1 glass * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.1 glass * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.1 glass * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.1 glass * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.1 glass * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.1 glass * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.1 glass * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.1 glass * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.1 glass * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.1 glass * SUCH DAMAGE.
39 1.1 glass *
40 1.1 glass * partially based on:
41 1.1 glass * libnetboot/rpc.c
42 1.1 glass * @(#) Header: rpc.c,v 1.12 93/09/28 08:31:56 leres Exp (LBL)
43 1.1 glass */
44 1.25 lukem
45 1.25 lukem #include <sys/cdefs.h>
46 1.38 maxv __KERNEL_RCSID(0, "$NetBSD: krpc_subr.c,v 1.38 2015/03/06 19:03:30 maxv Exp $");
47 1.1 glass
48 1.1 glass #include <sys/param.h>
49 1.8 gwr #include <sys/systm.h>
50 1.1 glass #include <sys/ioctl.h>
51 1.1 glass #include <sys/proc.h>
52 1.1 glass #include <sys/mount.h>
53 1.1 glass #include <sys/mbuf.h>
54 1.8 gwr #include <sys/reboot.h>
55 1.1 glass #include <sys/socket.h>
56 1.8 gwr #include <sys/socketvar.h>
57 1.1 glass
58 1.1 glass #include <netinet/in.h>
59 1.1 glass
60 1.1 glass #include <nfs/rpcv2.h>
61 1.7 gwr #include <nfs/krpc.h>
62 1.9 mycroft #include <nfs/xdr_subs.h>
63 1.21 drochner #include <nfs/nfsproto.h> /* XXX NFSX_V3FHMAX for next */
64 1.22 fvdl #include <nfs/nfs.h>
65 1.22 fvdl #include <nfs/nfsmount.h>
66 1.21 drochner #include <nfs/nfsdiskless.h> /* XXX decl nfs_boot_sendrecv */
67 1.1 glass
68 1.1 glass /*
69 1.1 glass * Kernel support for Sun RPC
70 1.1 glass *
71 1.1 glass * Used currently for bootstrapping in nfs diskless configurations.
72 1.1 glass */
73 1.1 glass
74 1.1 glass /*
75 1.1 glass * Generic RPC headers
76 1.1 glass */
77 1.1 glass
78 1.1 glass struct auth_info {
79 1.10 gwr u_int32_t authtype; /* auth type */
80 1.10 gwr u_int32_t authlen; /* auth length */
81 1.10 gwr };
82 1.10 gwr
83 1.10 gwr struct auth_unix {
84 1.10 gwr int32_t ua_time;
85 1.10 gwr int32_t ua_hostname; /* null */
86 1.10 gwr int32_t ua_uid;
87 1.10 gwr int32_t ua_gid;
88 1.10 gwr int32_t ua_gidlist; /* null */
89 1.1 glass };
90 1.1 glass
91 1.1 glass struct rpc_call {
92 1.8 gwr u_int32_t rp_xid; /* request transaction id */
93 1.8 gwr int32_t rp_direction; /* call direction (0) */
94 1.8 gwr u_int32_t rp_rpcvers; /* rpc version (2) */
95 1.8 gwr u_int32_t rp_prog; /* program */
96 1.8 gwr u_int32_t rp_vers; /* version */
97 1.8 gwr u_int32_t rp_proc; /* procedure */
98 1.10 gwr struct auth_info rpc_auth;
99 1.10 gwr struct auth_unix rpc_unix;
100 1.10 gwr struct auth_info rpc_verf;
101 1.1 glass };
102 1.1 glass
103 1.1 glass struct rpc_reply {
104 1.11 cgd u_int32_t rp_xid; /* request transaction id */
105 1.12 fvdl int32_t rp_direction; /* call direction (1) */
106 1.12 fvdl int32_t rp_astatus; /* accept status (0: accepted) */
107 1.1 glass union {
108 1.20 gwr /* rejected */
109 1.20 gwr struct {
110 1.20 gwr u_int32_t rej_stat;
111 1.20 gwr u_int32_t rej_val1;
112 1.20 gwr u_int32_t rej_val2;
113 1.20 gwr } rpu_rej;
114 1.20 gwr /* accepted */
115 1.1 glass struct {
116 1.10 gwr struct auth_info rok_auth;
117 1.10 gwr u_int32_t rok_status;
118 1.10 gwr } rpu_rok;
119 1.1 glass } rp_u;
120 1.1 glass };
121 1.20 gwr #define rp_rstat rp_u.rpu_rej.rej_stat
122 1.10 gwr #define rp_auth rp_u.rpu_rok.rok_auth
123 1.10 gwr #define rp_status rp_u.rpu_rok.rok_status
124 1.1 glass
125 1.1 glass #define MIN_REPLY_HDR 16 /* xid, dir, astat, errno */
126 1.1 glass
127 1.34 dsl static int krpccheck(struct mbuf*, void*);
128 1.2 gwr
129 1.2 gwr /*
130 1.1 glass * Call portmap to lookup a port number for a particular rpc program
131 1.2 gwr * Returns non-zero error on failure.
132 1.1 glass */
133 1.1 glass int
134 1.36 dsl krpc_portmap(struct sockaddr_in *sin, u_int prog, u_int vers, u_int proto, u_int16_t *portp, struct lwp *l)
135 1.36 dsl /* sin: server address */
136 1.36 dsl /* prog, vers, proto: host order */
137 1.36 dsl /* portp: network order */
138 1.1 glass {
139 1.1 glass struct sdata {
140 1.11 cgd u_int32_t prog; /* call program */
141 1.11 cgd u_int32_t vers; /* call version */
142 1.11 cgd u_int32_t proto; /* call protocol */
143 1.11 cgd u_int32_t port; /* call port (unused) */
144 1.1 glass } *sdata;
145 1.1 glass struct rdata {
146 1.8 gwr u_int16_t pad;
147 1.8 gwr u_int16_t port;
148 1.1 glass } *rdata;
149 1.1 glass struct mbuf *m;
150 1.1 glass int error;
151 1.1 glass
152 1.1 glass /* The portmapper port is fixed. */
153 1.1 glass if (prog == PMAPPROG) {
154 1.1 glass *portp = htons(PMAPPORT);
155 1.1 glass return 0;
156 1.1 glass }
157 1.1 glass
158 1.8 gwr m = m_get(M_WAIT, MT_DATA);
159 1.8 gwr sdata = mtod(m, struct sdata *);
160 1.1 glass m->m_len = sizeof(*sdata);
161 1.1 glass
162 1.1 glass /* Do the RPC to get it. */
163 1.9 mycroft sdata->prog = txdr_unsigned(prog);
164 1.9 mycroft sdata->vers = txdr_unsigned(vers);
165 1.19 fvdl sdata->proto = txdr_unsigned(proto);
166 1.1 glass sdata->port = 0;
167 1.1 glass
168 1.7 gwr sin->sin_port = htons(PMAPPORT);
169 1.7 gwr error = krpc_call(sin, PMAPPROG, PMAPVERS,
170 1.30 christos PMAPPROC_GETPORT, &m, NULL, l);
171 1.29 perry if (error)
172 1.1 glass return error;
173 1.1 glass
174 1.8 gwr if (m->m_len < sizeof(*rdata)) {
175 1.8 gwr m = m_pullup(m, sizeof(*rdata));
176 1.8 gwr if (m == NULL)
177 1.8 gwr return ENOBUFS;
178 1.8 gwr }
179 1.1 glass rdata = mtod(m, struct rdata *);
180 1.1 glass *portp = rdata->port;
181 1.1 glass
182 1.1 glass m_freem(m);
183 1.1 glass return 0;
184 1.1 glass }
185 1.1 glass
186 1.37 cegger static int krpccheck(struct mbuf *m, void *context)
187 1.21 drochner {
188 1.21 drochner struct rpc_reply *reply;
189 1.21 drochner
190 1.21 drochner /* Does the reply contain at least a header? */
191 1.21 drochner if (m->m_pkthdr.len < MIN_REPLY_HDR)
192 1.21 drochner return(-1);
193 1.21 drochner if (m->m_len < sizeof(struct rpc_reply)) {
194 1.21 drochner m = m_pullup(m, sizeof(struct rpc_reply));
195 1.21 drochner if (m == NULL)
196 1.21 drochner return(-1);
197 1.21 drochner }
198 1.21 drochner reply = mtod(m, struct rpc_reply *);
199 1.21 drochner
200 1.21 drochner /* Is it the right reply? */
201 1.21 drochner if (reply->rp_direction != txdr_unsigned(RPC_REPLY))
202 1.21 drochner return(-1);
203 1.21 drochner
204 1.21 drochner if (reply->rp_xid != txdr_unsigned(*(u_int32_t*)context))
205 1.21 drochner return(-1);
206 1.21 drochner
207 1.21 drochner return(0);
208 1.21 drochner }
209 1.21 drochner
210 1.1 glass /*
211 1.1 glass * Do a remote procedure call (RPC) and wait for its reply.
212 1.7 gwr * If from_p is non-null, then we are doing broadcast, and
213 1.7 gwr * the address from whence the response came is saved there.
214 1.1 glass */
215 1.1 glass int
216 1.36 dsl krpc_call(struct sockaddr_in *sa, u_int prog, u_int vers, u_int func, struct mbuf **data, struct mbuf **from_p, struct lwp *l)
217 1.36 dsl /* data: input/output */
218 1.36 dsl /* from_p: output */
219 1.1 glass {
220 1.1 glass struct socket *so;
221 1.1 glass struct sockaddr_in *sin;
222 1.7 gwr struct mbuf *m, *nam, *mhead, *from;
223 1.1 glass struct rpc_call *call;
224 1.1 glass struct rpc_reply *reply;
225 1.21 drochner int error, len;
226 1.8 gwr static u_int32_t xid = ~0xFF;
227 1.11 cgd u_int16_t tport;
228 1.1 glass
229 1.1 glass /*
230 1.1 glass * Validate address family.
231 1.1 glass * Sorry, this is INET specific...
232 1.1 glass */
233 1.7 gwr if (sa->sin_family != AF_INET)
234 1.1 glass return (EAFNOSUPPORT);
235 1.1 glass
236 1.1 glass /* Free at end if not null. */
237 1.1 glass nam = mhead = NULL;
238 1.7 gwr from = NULL;
239 1.1 glass
240 1.1 glass /*
241 1.24 wiz * Create socket and set its receive timeout.
242 1.1 glass */
243 1.33 ad if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0, l, NULL)))
244 1.38 maxv return error;
245 1.1 glass
246 1.21 drochner if ((error = nfs_boot_setrecvtimo(so)))
247 1.1 glass goto out;
248 1.7 gwr
249 1.7 gwr /*
250 1.7 gwr * Enable broadcast if necessary.
251 1.7 gwr */
252 1.7 gwr if (from_p) {
253 1.21 drochner if ((error = nfs_boot_enbroadcast(so)))
254 1.7 gwr goto out;
255 1.1 glass }
256 1.1 glass
257 1.1 glass /*
258 1.5 gwr * Bind the local endpoint to a reserved port,
259 1.5 gwr * because some NFS servers refuse requests from
260 1.5 gwr * non-reserved (non-privileged) ports.
261 1.5 gwr */
262 1.5 gwr tport = IPPORT_RESERVED;
263 1.5 gwr do {
264 1.5 gwr tport--;
265 1.30 christos error = nfs_boot_sobind_ipport(so, tport, l);
266 1.5 gwr } while (error == EADDRINUSE &&
267 1.5 gwr tport > IPPORT_RESERVED / 2);
268 1.5 gwr if (error) {
269 1.18 christos printf("bind failed\n");
270 1.5 gwr goto out;
271 1.5 gwr }
272 1.5 gwr
273 1.5 gwr /*
274 1.1 glass * Setup socket address for the server.
275 1.1 glass */
276 1.1 glass nam = m_get(M_WAIT, MT_SONAME);
277 1.1 glass sin = mtod(nam, struct sockaddr_in *);
278 1.32 christos memcpy((void *)sin, (void *)sa,
279 1.8 gwr (nam->m_len = sa->sin_len));
280 1.1 glass
281 1.1 glass /*
282 1.2 gwr * Prepend RPC message header.
283 1.1 glass */
284 1.8 gwr mhead = m_gethdr(M_WAIT, MT_DATA);
285 1.8 gwr mhead->m_next = *data;
286 1.1 glass call = mtod(mhead, struct rpc_call *);
287 1.8 gwr mhead->m_len = sizeof(*call);
288 1.32 christos memset((void *)call, 0, sizeof(*call));
289 1.10 gwr /* rpc_call part */
290 1.7 gwr xid++;
291 1.9 mycroft call->rp_xid = txdr_unsigned(xid);
292 1.1 glass /* call->rp_direction = 0; */
293 1.9 mycroft call->rp_rpcvers = txdr_unsigned(2);
294 1.9 mycroft call->rp_prog = txdr_unsigned(prog);
295 1.9 mycroft call->rp_vers = txdr_unsigned(vers);
296 1.9 mycroft call->rp_proc = txdr_unsigned(func);
297 1.10 gwr /* rpc_auth part (auth_unix as root) */
298 1.10 gwr call->rpc_auth.authtype = txdr_unsigned(RPCAUTH_UNIX);
299 1.10 gwr call->rpc_auth.authlen = txdr_unsigned(sizeof(struct auth_unix));
300 1.10 gwr /* rpc_verf part (auth_null) */
301 1.10 gwr call->rpc_verf.authtype = 0;
302 1.10 gwr call->rpc_verf.authlen = 0;
303 1.1 glass
304 1.1 glass /*
305 1.8 gwr * Setup packet header
306 1.8 gwr */
307 1.8 gwr len = 0;
308 1.8 gwr m = mhead;
309 1.8 gwr while (m) {
310 1.8 gwr len += m->m_len;
311 1.8 gwr m = m->m_next;
312 1.8 gwr }
313 1.8 gwr mhead->m_pkthdr.len = len;
314 1.8 gwr mhead->m_pkthdr.rcvif = NULL;
315 1.8 gwr
316 1.28 jonathan error = nfs_boot_sendrecv(so, nam, 0, mhead, krpccheck, &m, &from,
317 1.30 christos &xid, l);
318 1.21 drochner if (error)
319 1.20 gwr goto out;
320 1.20 gwr
321 1.21 drochner /* m_pullup() was done in krpccheck() */
322 1.21 drochner reply = mtod(m, struct rpc_reply *);
323 1.1 glass
324 1.21 drochner /* Was RPC accepted? (authorization OK) */
325 1.21 drochner if (reply->rp_astatus != 0) {
326 1.21 drochner /* Note: This is NOT an error code! */
327 1.21 drochner error = fxdr_unsigned(u_int32_t, reply->rp_rstat);
328 1.21 drochner switch (error) {
329 1.21 drochner case RPC_MISMATCH:
330 1.21 drochner /* .re_status = RPC_VERSMISMATCH; */
331 1.21 drochner error = ERPCMISMATCH;
332 1.21 drochner break;
333 1.21 drochner case RPC_AUTHERR:
334 1.21 drochner /* .re_status = RPC_AUTHERROR; */
335 1.21 drochner error = EAUTH;
336 1.21 drochner break;
337 1.21 drochner default:
338 1.21 drochner /* unexpected */
339 1.21 drochner error = EBADRPC;
340 1.21 drochner break;
341 1.20 gwr }
342 1.21 drochner goto out;
343 1.21 drochner }
344 1.7 gwr
345 1.21 drochner /* Did the call succeed? */
346 1.21 drochner if (reply->rp_status != 0) {
347 1.21 drochner /* Note: This is NOT an error code! */
348 1.21 drochner error = fxdr_unsigned(u_int32_t, reply->rp_status);
349 1.21 drochner switch (error) {
350 1.21 drochner case RPC_PROGUNAVAIL:
351 1.21 drochner error = EPROGUNAVAIL;
352 1.21 drochner break;
353 1.21 drochner case RPC_PROGMISMATCH:
354 1.21 drochner error = EPROGMISMATCH;
355 1.21 drochner break;
356 1.21 drochner case RPC_PROCUNAVAIL:
357 1.21 drochner error = EPROCUNAVAIL;
358 1.21 drochner break;
359 1.21 drochner case RPC_GARBAGE:
360 1.21 drochner default:
361 1.21 drochner error = EBADRPC;
362 1.20 gwr }
363 1.21 drochner goto out;
364 1.21 drochner }
365 1.1 glass
366 1.1 glass /*
367 1.20 gwr * OK, we have received a good reply!
368 1.21 drochner * Get its length, then strip it off.
369 1.2 gwr */
370 1.8 gwr len = sizeof(*reply);
371 1.10 gwr if (reply->rp_auth.authtype != 0) {
372 1.10 gwr len += fxdr_unsigned(u_int32_t, reply->rp_auth.authlen);
373 1.1 glass len = (len + 3) & ~3; /* XXX? */
374 1.1 glass }
375 1.1 glass m_adj(m, len);
376 1.2 gwr
377 1.1 glass /* result */
378 1.1 glass *data = m;
379 1.31 christos if (from_p && error == 0) {
380 1.7 gwr *from_p = from;
381 1.7 gwr from = NULL;
382 1.7 gwr }
383 1.1 glass
384 1.1 glass out:
385 1.1 glass if (nam) m_freem(nam);
386 1.1 glass if (mhead) m_freem(mhead);
387 1.7 gwr if (from) m_freem(from);
388 1.1 glass soclose(so);
389 1.1 glass return error;
390 1.8 gwr }
391 1.8 gwr
392 1.8 gwr /*
393 1.8 gwr * eXternal Data Representation routines.
394 1.8 gwr * (but with non-standard args...)
395 1.8 gwr */
396 1.8 gwr
397 1.8 gwr /*
398 1.8 gwr * String representation for RPC.
399 1.8 gwr */
400 1.8 gwr struct xdr_string {
401 1.8 gwr u_int32_t len; /* length without null or padding */
402 1.8 gwr char data[4]; /* data (longer, of course) */
403 1.8 gwr /* data is padded to a long-word boundary */
404 1.8 gwr };
405 1.8 gwr
406 1.8 gwr struct mbuf *
407 1.35 dsl xdr_string_encode(char *str, int len)
408 1.8 gwr {
409 1.8 gwr struct mbuf *m;
410 1.8 gwr struct xdr_string *xs;
411 1.8 gwr int dlen; /* padded string length */
412 1.8 gwr int mlen; /* message length */
413 1.8 gwr
414 1.8 gwr dlen = (len + 3) & ~3;
415 1.8 gwr mlen = dlen + 4;
416 1.8 gwr
417 1.13 cgd if (mlen > MCLBYTES) /* If too big, we just can't do it. */
418 1.13 cgd return (NULL);
419 1.13 cgd
420 1.8 gwr m = m_get(M_WAIT, MT_DATA);
421 1.8 gwr if (mlen > MLEN) {
422 1.27 matt m_clget(m, M_WAIT);
423 1.13 cgd if ((m->m_flags & M_EXT) == 0) {
424 1.13 cgd (void) m_free(m); /* There can be only one. */
425 1.13 cgd return (NULL);
426 1.13 cgd }
427 1.8 gwr }
428 1.8 gwr xs = mtod(m, struct xdr_string *);
429 1.8 gwr m->m_len = mlen;
430 1.9 mycroft xs->len = txdr_unsigned(len);
431 1.23 perry memcpy(xs->data, str, len);
432 1.8 gwr return (m);
433 1.8 gwr }
434 1.8 gwr
435 1.8 gwr struct mbuf *
436 1.36 dsl xdr_string_decode(struct mbuf *m, char *str, int *len_p)
437 1.36 dsl /* len_p: bufsize - 1 */
438 1.8 gwr {
439 1.8 gwr struct xdr_string *xs;
440 1.8 gwr int mlen; /* message length */
441 1.8 gwr int slen; /* string length */
442 1.8 gwr
443 1.8 gwr if (m->m_len < 4) {
444 1.8 gwr m = m_pullup(m, 4);
445 1.8 gwr if (m == NULL)
446 1.8 gwr return (NULL);
447 1.8 gwr }
448 1.8 gwr xs = mtod(m, struct xdr_string *);
449 1.9 mycroft slen = fxdr_unsigned(u_int32_t, xs->len);
450 1.8 gwr mlen = 4 + ((slen + 3) & ~3);
451 1.8 gwr
452 1.8 gwr if (slen > *len_p)
453 1.8 gwr slen = *len_p;
454 1.8 gwr m_copydata(m, 4, slen, str);
455 1.8 gwr m_adj(m, mlen);
456 1.8 gwr
457 1.8 gwr str[slen] = '\0';
458 1.8 gwr *len_p = slen;
459 1.8 gwr
460 1.8 gwr return (m);
461 1.8 gwr }
462 1.8 gwr
463 1.8 gwr
464 1.8 gwr /*
465 1.8 gwr * Inet address in RPC messages
466 1.8 gwr * (Note, really four ints, NOT chars. Blech.)
467 1.8 gwr */
468 1.8 gwr struct xdr_inaddr {
469 1.9 mycroft u_int32_t atype;
470 1.9 mycroft u_int32_t addr[4];
471 1.8 gwr };
472 1.8 gwr
473 1.8 gwr struct mbuf *
474 1.36 dsl xdr_inaddr_encode(struct in_addr *ia)
475 1.36 dsl /* ia: already in network order */
476 1.8 gwr {
477 1.8 gwr struct mbuf *m;
478 1.8 gwr struct xdr_inaddr *xi;
479 1.9 mycroft u_int8_t *cp;
480 1.9 mycroft u_int32_t *ip;
481 1.8 gwr
482 1.8 gwr m = m_get(M_WAIT, MT_DATA);
483 1.8 gwr xi = mtod(m, struct xdr_inaddr *);
484 1.8 gwr m->m_len = sizeof(*xi);
485 1.9 mycroft xi->atype = txdr_unsigned(1);
486 1.8 gwr ip = xi->addr;
487 1.9 mycroft cp = (u_int8_t *)&ia->s_addr;
488 1.9 mycroft *ip++ = txdr_unsigned(*cp++);
489 1.9 mycroft *ip++ = txdr_unsigned(*cp++);
490 1.9 mycroft *ip++ = txdr_unsigned(*cp++);
491 1.9 mycroft *ip++ = txdr_unsigned(*cp++);
492 1.8 gwr
493 1.8 gwr return (m);
494 1.8 gwr }
495 1.8 gwr
496 1.8 gwr struct mbuf *
497 1.36 dsl xdr_inaddr_decode(struct mbuf *m, struct in_addr *ia)
498 1.36 dsl /* ia: already in network order */
499 1.8 gwr {
500 1.8 gwr struct xdr_inaddr *xi;
501 1.9 mycroft u_int8_t *cp;
502 1.9 mycroft u_int32_t *ip;
503 1.8 gwr
504 1.8 gwr if (m->m_len < sizeof(*xi)) {
505 1.8 gwr m = m_pullup(m, sizeof(*xi));
506 1.8 gwr if (m == NULL)
507 1.8 gwr return (NULL);
508 1.8 gwr }
509 1.8 gwr xi = mtod(m, struct xdr_inaddr *);
510 1.9 mycroft if (xi->atype != txdr_unsigned(1)) {
511 1.8 gwr ia->s_addr = INADDR_ANY;
512 1.8 gwr goto out;
513 1.8 gwr }
514 1.8 gwr ip = xi->addr;
515 1.9 mycroft cp = (u_int8_t *)&ia->s_addr;
516 1.9 mycroft *cp++ = fxdr_unsigned(u_int8_t, *ip++);
517 1.9 mycroft *cp++ = fxdr_unsigned(u_int8_t, *ip++);
518 1.9 mycroft *cp++ = fxdr_unsigned(u_int8_t, *ip++);
519 1.9 mycroft *cp++ = fxdr_unsigned(u_int8_t, *ip++);
520 1.8 gwr
521 1.8 gwr out:
522 1.8 gwr m_adj(m, sizeof(*xi));
523 1.8 gwr return (m);
524 1.1 glass }
525