ip_rpcb_pxy.c revision 1.2 1 1.2 christos /* $NetBSD: ip_rpcb_pxy.c,v 1.2 2012/03/23 20:39:50 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (C) 2002-2003 by Ryan Beasley <ryanb (at) goddamnbastard.org>
5 1.1 christos *
6 1.1 christos * See the IPFILTER.LICENCE file for details on licencing.
7 1.1 christos */
8 1.1 christos /*
9 1.1 christos * Overview:
10 1.1 christos * This is an in-kernel application proxy for Sun's RPCBIND (nee portmap)
11 1.1 christos * protocol as defined in RFC1833. It is far from complete, mostly
12 1.1 christos * lacking in less-likely corner cases, but it's definitely functional.
13 1.1 christos *
14 1.1 christos * Invocation:
15 1.1 christos * rdr <int> <e_ip>/32 port <e_p> -> <i_ip> port <i_p> udp proxy rpcbu
16 1.1 christos *
17 1.1 christos * If the host running IP Filter is the same as the RPC server, it's
18 1.1 christos * perfectly legal for both the internal and external addresses and ports
19 1.1 christos * to match.
20 1.1 christos *
21 1.1 christos * When triggered by appropriate IP NAT rules, this proxy works by
22 1.1 christos * examining data contained in received packets. Requests and replies are
23 1.1 christos * modified, NAT and state table entries created, etc., as necessary.
24 1.1 christos */
25 1.1 christos /*
26 1.1 christos * TODO / NOTES
27 1.1 christos *
28 1.1 christos * o Must implement locking to protect proxy session data.
29 1.1 christos * o Fragmentation isn't supported.
30 1.1 christos * o Only supports UDP.
31 1.1 christos * o Doesn't support multiple RPC records in a single request.
32 1.1 christos * o Errors should be more fine-grained. (e.g., malloc failure vs.
33 1.1 christos * illegal RPCB request / reply)
34 1.1 christos * o Even with the limit on the total amount of recorded transactions,
35 1.1 christos * should there be a timeout on transaction removal?
36 1.1 christos * o There is a potential collision between cloning, wildcard NAT and
37 1.1 christos * state entries. There should be an appr_getport routine for
38 1.1 christos * to avoid this.
39 1.1 christos * o The enclosed hack of STREAMS support is pretty sick and most likely
40 1.1 christos * broken.
41 1.1 christos *
42 1.2 christos * Id: ip_rpcb_pxy.c,v 2.47 2009/03/01 12:41:26 darrenr Exp
43 1.1 christos */
44 1.2 christos
45 1.2 christos #include <sys/cdefs.h>
46 1.2 christos __KERNEL_RCSID(1, "$NetBSD: ip_rpcb_pxy.c,v 1.2 2012/03/23 20:39:50 christos Exp $");
47 1.2 christos
48 1.1 christos #define IPF_RPCB_PROXY
49 1.1 christos
50 1.1 christos /*
51 1.1 christos * Function prototypes
52 1.1 christos */
53 1.2 christos void ipf_p_rpcb_main_load(void);
54 1.2 christos void ipf_p_rpcb_main_unload(void);
55 1.2 christos int ipf_p_rpcb_new(void *, fr_info_t *, ap_session_t *, nat_t *);
56 1.2 christos void ipf_p_rpcb_del(ipf_main_softc_t *, ap_session_t *);
57 1.2 christos int ipf_p_rpcb_in(void *, fr_info_t *, ap_session_t *, nat_t *);
58 1.2 christos int ipf_p_rpcb_out(void *, fr_info_t *, ap_session_t *, nat_t *);
59 1.2 christos
60 1.2 christos static void ipf_p_rpcb_flush(rpcb_session_t *);
61 1.2 christos static int ipf_p_rpcb_decodereq(fr_info_t *, nat_t *,
62 1.2 christos rpcb_session_t *, rpc_msg_t *);
63 1.2 christos static int ipf_p_rpcb_skipauth(rpc_msg_t *, xdr_auth_t *, u_32_t **);
64 1.2 christos static int ipf_p_rpcb_insert(rpcb_session_t *, rpcb_xact_t *);
65 1.2 christos static int ipf_p_rpcb_xdrrpcb(rpc_msg_t *, u_32_t *, rpcb_args_t *);
66 1.2 christos static int ipf_p_rpcb_getuaddr(rpc_msg_t *, xdr_uaddr_t *,
67 1.2 christos u_32_t **);
68 1.2 christos static u_int ipf_p_rpcb_atoi(char *);
69 1.2 christos static int ipf_p_rpcb_modreq(fr_info_t *, nat_t *, rpc_msg_t *,
70 1.2 christos mb_t *, u_int);
71 1.2 christos static int ipf_p_rpcb_decoderep(fr_info_t *, nat_t *,
72 1.2 christos rpcb_session_t *, rpc_msg_t *, rpcb_xact_t **);
73 1.2 christos static rpcb_xact_t * ipf_p_rpcb_lookup(rpcb_session_t *, u_32_t);
74 1.2 christos static void ipf_p_rpcb_deref(rpcb_session_t *, rpcb_xact_t *);
75 1.2 christos static int ipf_p_rpcb_getproto(rpc_msg_t *, xdr_proto_t *,
76 1.2 christos u_32_t **);
77 1.2 christos static int ipf_p_rpcb_getnat(fr_info_t *, nat_t *, u_int, u_int);
78 1.2 christos static int ipf_p_rpcb_modv3(fr_info_t *, nat_t *, rpc_msg_t *,
79 1.2 christos mb_t *, u_int);
80 1.2 christos static int ipf_p_rpcb_modv4(fr_info_t *, nat_t *, rpc_msg_t *,
81 1.2 christos mb_t *, u_int);
82 1.2 christos static void ipf_p_rpcb_fixlen(fr_info_t *, int);
83 1.1 christos
84 1.1 christos /*
85 1.1 christos * Global variables
86 1.1 christos */
87 1.1 christos static frentry_t rpcbfr; /* Skeleton rule for reference by entities
88 1.1 christos this proxy creates. */
89 1.1 christos static int rpcbcnt; /* Upper bound of allocated RPCB sessions. */
90 1.1 christos /* XXX rpcbcnt still requires locking. */
91 1.1 christos
92 1.1 christos static int rpcb_proxy_init = 0;
93 1.1 christos
94 1.1 christos
95 1.1 christos /*
96 1.1 christos * Since rpc_msg contains only pointers, one should use this macro as a
97 1.1 christos * handy way to get to the goods. (In case you're wondering about the name,
98 1.1 christos * this started as BYTEREF -> BREF -> B.)
99 1.1 christos */
100 1.1 christos #define B(r) (u_32_t)ntohl(*(r))
101 1.1 christos
102 1.1 christos /*
103 1.1 christos * Public subroutines
104 1.1 christos */
105 1.1 christos
106 1.1 christos /* -------------------------------------------------------------------- */
107 1.1 christos /* Function: ipf_p_rpcb_main_load */
108 1.1 christos /* Returns: void */
109 1.1 christos /* Parameters: (void) */
110 1.1 christos /* */
111 1.1 christos /* Initialize the filter rule entry and session limiter. */
112 1.1 christos /* -------------------------------------------------------------------- */
113 1.1 christos void
114 1.2 christos ipf_p_rpcb_main_load(void)
115 1.1 christos {
116 1.1 christos rpcbcnt = 0;
117 1.1 christos
118 1.1 christos bzero((char *)&rpcbfr, sizeof(rpcbfr));
119 1.1 christos rpcbfr.fr_ref = 1;
120 1.1 christos rpcbfr.fr_flags = FR_PASS|FR_QUICK|FR_KEEPSTATE;
121 1.1 christos MUTEX_INIT(&rpcbfr.fr_lock, "ipf Sun RPCB proxy rule lock");
122 1.1 christos rpcb_proxy_init = 1;
123 1.1 christos }
124 1.1 christos
125 1.1 christos /* -------------------------------------------------------------------- */
126 1.1 christos /* Function: ipf_p_rpcb_main_unload */
127 1.1 christos /* Returns: void */
128 1.1 christos /* Parameters: (void) */
129 1.1 christos /* */
130 1.1 christos /* Destroy rpcbfr's mutex to avoid a lock leak. */
131 1.1 christos /* -------------------------------------------------------------------- */
132 1.1 christos void
133 1.2 christos ipf_p_rpcb_main_unload(void)
134 1.1 christos {
135 1.1 christos if (rpcb_proxy_init == 1) {
136 1.1 christos MUTEX_DESTROY(&rpcbfr.fr_lock);
137 1.1 christos rpcb_proxy_init = 0;
138 1.1 christos }
139 1.1 christos }
140 1.1 christos
141 1.1 christos /* -------------------------------------------------------------------- */
142 1.1 christos /* Function: ipf_p_rpcb_new */
143 1.1 christos /* Returns: int - -1 == failure, 0 == success */
144 1.1 christos /* Parameters: fin(I) - pointer to packet information */
145 1.1 christos /* aps(I) - pointer to proxy session structure */
146 1.1 christos /* nat(I) - pointer to NAT session structure */
147 1.1 christos /* */
148 1.1 christos /* Allocate resources for per-session proxy structures. */
149 1.1 christos /* -------------------------------------------------------------------- */
150 1.1 christos int
151 1.2 christos ipf_p_rpcb_new(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat)
152 1.1 christos {
153 1.1 christos rpcb_session_t *rs;
154 1.1 christos
155 1.1 christos fin = fin; /* LINT */
156 1.1 christos nat = nat; /* LINT */
157 1.1 christos
158 1.1 christos KMALLOC(rs, rpcb_session_t *);
159 1.1 christos if (rs == NULL)
160 1.1 christos return(-1);
161 1.1 christos
162 1.1 christos bzero((char *)rs, sizeof(*rs));
163 1.1 christos MUTEX_INIT(&rs->rs_rxlock, "ipf Sun RPCB proxy session lock");
164 1.1 christos
165 1.1 christos aps->aps_data = rs;
166 1.1 christos
167 1.1 christos return(0);
168 1.1 christos }
169 1.1 christos
170 1.1 christos /* -------------------------------------------------------------------- */
171 1.1 christos /* Function: ipf_p_rpcb_del */
172 1.1 christos /* Returns: void */
173 1.1 christos /* Parameters: aps(I) - pointer to proxy session structure */
174 1.1 christos /* */
175 1.1 christos /* Free up a session's list of RPCB requests. */
176 1.1 christos /* -------------------------------------------------------------------- */
177 1.1 christos void
178 1.2 christos ipf_p_rpcb_del(ipf_main_softc_t *softc, ap_session_t *aps)
179 1.1 christos {
180 1.1 christos rpcb_session_t *rs;
181 1.1 christos rs = (rpcb_session_t *)aps->aps_data;
182 1.1 christos
183 1.1 christos MUTEX_ENTER(&rs->rs_rxlock);
184 1.1 christos ipf_p_rpcb_flush(rs);
185 1.1 christos MUTEX_EXIT(&rs->rs_rxlock);
186 1.1 christos MUTEX_DESTROY(&rs->rs_rxlock);
187 1.1 christos }
188 1.1 christos
189 1.1 christos /* -------------------------------------------------------------------- */
190 1.1 christos /* Function: ipf_p_rpcb_in */
191 1.1 christos /* Returns: int - APR_ERR(1) == drop the packet, */
192 1.1 christos /* APR_ERR(2) == kill the proxy session, */
193 1.1 christos /* else change in packet length (in bytes) */
194 1.1 christos /* Parameters: fin(I) - pointer to packet information */
195 1.1 christos /* ip(I) - pointer to packet header */
196 1.1 christos /* aps(I) - pointer to proxy session structure */
197 1.1 christos /* nat(I) - pointer to NAT session structure */
198 1.1 christos /* */
199 1.1 christos /* Given a presumed RPCB request, perform some minor tests and pass off */
200 1.1 christos /* for decoding. Also pass packet off for a rewrite if necessary. */
201 1.1 christos /* -------------------------------------------------------------------- */
202 1.1 christos int
203 1.2 christos ipf_p_rpcb_in(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat)
204 1.1 christos {
205 1.1 christos rpc_msg_t rpcmsg, *rm;
206 1.1 christos rpcb_session_t *rs;
207 1.1 christos u_int off, dlen;
208 1.1 christos mb_t *m;
209 1.1 christos int rv;
210 1.1 christos
211 1.1 christos /* Disallow fragmented or illegally short packets. */
212 1.1 christos if ((fin->fin_flx & (FI_FRAG|FI_SHORT)) != 0)
213 1.1 christos return(APR_ERR(1));
214 1.1 christos
215 1.1 christos /* Perform basic variable initialization. */
216 1.1 christos rs = (rpcb_session_t *)aps->aps_data;
217 1.1 christos
218 1.1 christos m = fin->fin_m;
219 1.1 christos off = (char *)fin->fin_dp - (char *)fin->fin_ip;
220 1.1 christos off += sizeof(udphdr_t) + fin->fin_ipoff;
221 1.1 christos dlen = fin->fin_dlen - sizeof(udphdr_t);
222 1.1 christos
223 1.1 christos /* Disallow packets outside legal range for supported requests. */
224 1.1 christos if ((dlen < RPCB_REQMIN) || (dlen > RPCB_REQMAX))
225 1.1 christos return(APR_ERR(1));
226 1.1 christos
227 1.1 christos /* Copy packet over to convenience buffer. */
228 1.1 christos rm = &rpcmsg;
229 1.1 christos bzero((char *)rm, sizeof(*rm));
230 1.2 christos COPYDATA(m, off, dlen, (void *)&rm->rm_msgbuf);
231 1.1 christos rm->rm_buflen = dlen;
232 1.1 christos
233 1.1 christos /* Send off to decode request. */
234 1.1 christos rv = ipf_p_rpcb_decodereq(fin, nat, rs, rm);
235 1.1 christos
236 1.1 christos switch(rv)
237 1.1 christos {
238 1.1 christos case -1:
239 1.1 christos return(APR_ERR(1));
240 1.1 christos /*NOTREACHED*/
241 1.1 christos break;
242 1.1 christos case 0:
243 1.1 christos break;
244 1.1 christos case 1:
245 1.1 christos rv = ipf_p_rpcb_modreq(fin, nat, rm, m, off);
246 1.1 christos break;
247 1.1 christos default:
248 1.1 christos /*CONSTANTCONDITION*/
249 1.1 christos IPF_PANIC(1, ("illegal rv %d (ipf_p_rpcb_req)", rv));
250 1.1 christos }
251 1.1 christos
252 1.1 christos return(rv);
253 1.1 christos }
254 1.1 christos
255 1.1 christos /* -------------------------------------------------------------------- */
256 1.1 christos /* Function: ipf_p_rpcb_out */
257 1.1 christos /* Returns: int - APR_ERR(1) == drop the packet, */
258 1.1 christos /* APR_ERR(2) == kill the proxy session, */
259 1.1 christos /* else change in packet length (in bytes) */
260 1.1 christos /* Parameters: fin(I) - pointer to packet information */
261 1.1 christos /* ip(I) - pointer to packet header */
262 1.1 christos /* aps(I) - pointer to proxy session structure */
263 1.1 christos /* nat(I) - pointer to NAT session structure */
264 1.1 christos /* */
265 1.1 christos /* Given a presumed RPCB reply, perform some minor tests and pass off */
266 1.1 christos /* for decoding. If the message indicates a successful request with */
267 1.1 christos /* valid addressing information, create NAT and state structures to */
268 1.1 christos /* allow direct communication between RPC client and server. */
269 1.1 christos /* -------------------------------------------------------------------- */
270 1.1 christos int
271 1.2 christos ipf_p_rpcb_out(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat)
272 1.1 christos {
273 1.1 christos rpc_msg_t rpcmsg, *rm;
274 1.1 christos rpcb_session_t *rs;
275 1.1 christos rpcb_xact_t *rx;
276 1.1 christos u_int off, dlen;
277 1.1 christos int rv, diff;
278 1.1 christos mb_t *m;
279 1.1 christos
280 1.2 christos rx = NULL; /* XXX gcc */
281 1.2 christos
282 1.1 christos /* Disallow fragmented or illegally short packets. */
283 1.1 christos if ((fin->fin_flx & (FI_FRAG|FI_SHORT)) != 0)
284 1.1 christos return(APR_ERR(1));
285 1.1 christos
286 1.1 christos /* Perform basic variable initialization. */
287 1.1 christos rs = (rpcb_session_t *)aps->aps_data;
288 1.1 christos rx = NULL;
289 1.1 christos
290 1.1 christos m = fin->fin_m;
291 1.1 christos off = (char *)fin->fin_dp - (char *)fin->fin_ip;
292 1.1 christos off += sizeof(udphdr_t) + fin->fin_ipoff;
293 1.1 christos dlen = fin->fin_dlen - sizeof(udphdr_t);
294 1.1 christos diff = 0;
295 1.1 christos
296 1.1 christos /* Disallow packets outside legal range for supported requests. */
297 1.1 christos if ((dlen < RPCB_REPMIN) || (dlen > RPCB_REPMAX))
298 1.1 christos return(APR_ERR(1));
299 1.1 christos
300 1.1 christos /* Copy packet over to convenience buffer. */
301 1.1 christos rm = &rpcmsg;
302 1.1 christos bzero((char *)rm, sizeof(*rm));
303 1.2 christos COPYDATA(m, off, dlen, (void *)&rm->rm_msgbuf);
304 1.1 christos rm->rm_buflen = dlen;
305 1.1 christos
306 1.1 christos rx = NULL; /* XXX gcc */
307 1.1 christos
308 1.1 christos /* Send off to decode reply. */
309 1.1 christos rv = ipf_p_rpcb_decoderep(fin, nat, rs, rm, &rx);
310 1.1 christos
311 1.1 christos switch(rv)
312 1.1 christos {
313 1.1 christos case -1: /* Bad packet */
314 1.1 christos if (rx != NULL) {
315 1.1 christos MUTEX_ENTER(&rs->rs_rxlock);
316 1.1 christos ipf_p_rpcb_deref(rs, rx);
317 1.1 christos MUTEX_EXIT(&rs->rs_rxlock);
318 1.1 christos }
319 1.1 christos return(APR_ERR(1));
320 1.1 christos /*NOTREACHED*/
321 1.1 christos break;
322 1.1 christos case 0: /* Negative reply / request rejected */
323 1.1 christos break;
324 1.1 christos case 1: /* Positive reply */
325 1.1 christos /*
326 1.1 christos * With the IP address embedded in a GETADDR(LIST) reply,
327 1.1 christos * we'll need to rewrite the packet in the very possible
328 1.1 christos * event that the internal & external addresses aren't the
329 1.1 christos * same. (i.e., this box is either a router or rpcbind
330 1.1 christos * only listens on loopback.)
331 1.1 christos */
332 1.1 christos if (nat->nat_odstaddr != nat->nat_ndstaddr) {
333 1.1 christos if (rx->rx_type == RPCB_RES_STRING)
334 1.1 christos diff = ipf_p_rpcb_modv3(fin, nat, rm, m, off);
335 1.1 christos else if (rx->rx_type == RPCB_RES_LIST)
336 1.1 christos diff = ipf_p_rpcb_modv4(fin, nat, rm, m, off);
337 1.1 christos }
338 1.1 christos break;
339 1.1 christos default:
340 1.1 christos /*CONSTANTCONDITION*/
341 1.1 christos IPF_PANIC(1, ("illegal rv %d (ipf_p_rpcb_decoderep)", rv));
342 1.1 christos }
343 1.1 christos
344 1.1 christos if (rx != NULL) {
345 1.1 christos MUTEX_ENTER(&rs->rs_rxlock);
346 1.1 christos /* XXX Gross hack - I'm overloading the reference
347 1.1 christos * counter to deal with both threads and retransmitted
348 1.1 christos * requests. One deref signals that this thread is
349 1.1 christos * finished with rx, and the other signals that we've
350 1.1 christos * processed its reply.
351 1.1 christos */
352 1.1 christos ipf_p_rpcb_deref(rs, rx);
353 1.1 christos ipf_p_rpcb_deref(rs, rx);
354 1.1 christos MUTEX_EXIT(&rs->rs_rxlock);
355 1.1 christos }
356 1.1 christos
357 1.1 christos return(diff);
358 1.1 christos }
359 1.1 christos
360 1.1 christos /*
361 1.1 christos * Private support subroutines
362 1.1 christos */
363 1.1 christos
364 1.1 christos /* -------------------------------------------------------------------- */
365 1.1 christos /* Function: ipf_p_rpcb_flush */
366 1.1 christos /* Returns: void */
367 1.1 christos /* Parameters: rs(I) - pointer to RPCB session structure */
368 1.1 christos /* */
369 1.1 christos /* Simply flushes the list of outstanding transactions, if any. */
370 1.1 christos /* -------------------------------------------------------------------- */
371 1.1 christos static void
372 1.2 christos ipf_p_rpcb_flush(rpcb_session_t *rs)
373 1.1 christos {
374 1.1 christos rpcb_xact_t *r1, *r2;
375 1.1 christos
376 1.1 christos r1 = rs->rs_rxlist;
377 1.1 christos if (r1 == NULL)
378 1.1 christos return;
379 1.1 christos
380 1.1 christos while (r1 != NULL) {
381 1.1 christos r2 = r1;
382 1.1 christos r1 = r1->rx_next;
383 1.1 christos KFREE(r2);
384 1.1 christos }
385 1.1 christos }
386 1.1 christos
387 1.1 christos /* -------------------------------------------------------------------- */
388 1.1 christos /* Function: ipf_p_rpcb_decodereq */
389 1.1 christos /* Returns: int - -1 == bad request or critical failure, */
390 1.1 christos /* 0 == request successfully decoded, */
391 1.1 christos /* 1 == request successfully decoded; requires */
392 1.1 christos /* address rewrite/modification */
393 1.1 christos /* Parameters: fin(I) - pointer to packet information */
394 1.1 christos /* nat(I) - pointer to NAT session structure */
395 1.1 christos /* rs(I) - pointer to RPCB session structure */
396 1.1 christos /* rm(I) - pointer to RPC message structure */
397 1.1 christos /* */
398 1.1 christos /* Take a presumed RPCB request, decode it, and store the results in */
399 1.1 christos /* the transaction list. If the internal target address needs to be */
400 1.1 christos /* modified, store its location in ptr. */
401 1.1 christos /* WARNING: It's the responsibility of the caller to make sure there */
402 1.1 christos /* is enough room in rs_buf for the basic RPC message "preamble". */
403 1.1 christos /* -------------------------------------------------------------------- */
404 1.1 christos static int
405 1.2 christos ipf_p_rpcb_decodereq(fr_info_t *fin, nat_t *nat, rpcb_session_t *rs,
406 1.2 christos rpc_msg_t *rm)
407 1.1 christos {
408 1.1 christos rpcb_args_t *ra;
409 1.1 christos u_32_t xdr, *p;
410 1.1 christos rpc_call_t *rc;
411 1.1 christos rpcb_xact_t rx;
412 1.1 christos int mod;
413 1.1 christos
414 1.1 christos p = (u_32_t *)rm->rm_msgbuf;
415 1.1 christos mod = 0;
416 1.1 christos
417 1.1 christos bzero((char *)&rx, sizeof(rx));
418 1.1 christos rc = &rm->rm_call;
419 1.1 christos
420 1.1 christos rm->rm_xid = p;
421 1.1 christos rx.rx_xid = B(p++); /* Record this message's XID. */
422 1.1 christos
423 1.1 christos /* Parse out and test the RPC header. */
424 1.1 christos if ((B(p++) != RPCB_CALL) ||
425 1.1 christos (B(p++) != RPCB_MSG_VERSION) ||
426 1.1 christos (B(p++) != RPCB_PROG))
427 1.1 christos return(-1);
428 1.1 christos
429 1.1 christos /* Record the RPCB version and procedure. */
430 1.1 christos rc->rc_vers = p++;
431 1.1 christos rc->rc_proc = p++;
432 1.1 christos
433 1.1 christos /* Bypass RPC authentication stuff. */
434 1.1 christos if (ipf_p_rpcb_skipauth(rm, &rc->rc_authcred, &p) != 0)
435 1.1 christos return(-1);
436 1.1 christos if (ipf_p_rpcb_skipauth(rm, &rc->rc_authverf, &p) != 0)
437 1.1 christos return(-1);
438 1.1 christos
439 1.1 christos /* Compare RPCB version and procedure numbers. */
440 1.1 christos switch(B(rc->rc_vers))
441 1.1 christos {
442 1.1 christos case 2:
443 1.1 christos /* This proxy only supports PMAP_GETPORT. */
444 1.1 christos if (B(rc->rc_proc) != RPCB_GETPORT)
445 1.1 christos return(-1);
446 1.1 christos
447 1.1 christos /* Portmap requests contain four 4 byte parameters. */
448 1.1 christos if (RPCB_BUF_EQ(rm, p, 16) == 0)
449 1.1 christos return(-1);
450 1.1 christos
451 1.1 christos p += 2; /* Skip requested program and version numbers. */
452 1.1 christos
453 1.1 christos /* Sanity check the requested protocol. */
454 1.1 christos xdr = B(p);
455 1.1 christos if (!(xdr == IPPROTO_UDP || xdr == IPPROTO_TCP))
456 1.1 christos return(-1);
457 1.1 christos
458 1.1 christos rx.rx_type = RPCB_RES_PMAP;
459 1.1 christos rx.rx_proto = xdr;
460 1.1 christos break;
461 1.1 christos case 3:
462 1.1 christos case 4:
463 1.1 christos /* GETADDRLIST is exclusive to v4; GETADDR for v3 & v4 */
464 1.1 christos switch(B(rc->rc_proc))
465 1.1 christos {
466 1.1 christos case RPCB_GETADDR:
467 1.1 christos rx.rx_type = RPCB_RES_STRING;
468 1.1 christos rx.rx_proto = (u_int)fin->fin_p;
469 1.1 christos break;
470 1.1 christos case RPCB_GETADDRLIST:
471 1.1 christos if (B(rc->rc_vers) != 4)
472 1.1 christos return(-1);
473 1.1 christos rx.rx_type = RPCB_RES_LIST;
474 1.1 christos break;
475 1.1 christos default:
476 1.1 christos return(-1);
477 1.1 christos }
478 1.1 christos
479 1.1 christos ra = &rc->rc_rpcbargs;
480 1.1 christos
481 1.1 christos /* Decode the 'struct rpcb' request. */
482 1.1 christos if (ipf_p_rpcb_xdrrpcb(rm, p, ra) != 0)
483 1.1 christos return(-1);
484 1.1 christos
485 1.1 christos /* Are the target address & port valid? */
486 1.1 christos if ((ra->ra_maddr.xu_ip != nat->nat_ndstaddr) ||
487 1.1 christos (ra->ra_maddr.xu_port != nat->nat_ndport))
488 1.1 christos return(-1);
489 1.1 christos
490 1.1 christos /* Do we need to rewrite this packet? */
491 1.1 christos if ((nat->nat_ndstaddr != nat->nat_odstaddr) ||
492 1.1 christos (nat->nat_ndport != nat->nat_odport))
493 1.1 christos mod = 1;
494 1.1 christos break;
495 1.1 christos default:
496 1.1 christos return(-1);
497 1.1 christos }
498 1.1 christos
499 1.1 christos MUTEX_ENTER(&rs->rs_rxlock);
500 1.1 christos if (ipf_p_rpcb_insert(rs, &rx) != 0) {
501 1.1 christos MUTEX_EXIT(&rs->rs_rxlock);
502 1.1 christos return(-1);
503 1.1 christos }
504 1.1 christos MUTEX_EXIT(&rs->rs_rxlock);
505 1.1 christos
506 1.1 christos return(mod);
507 1.1 christos }
508 1.1 christos
509 1.1 christos /* -------------------------------------------------------------------- */
510 1.1 christos /* Function: ipf_p_rpcb_skipauth */
511 1.1 christos /* Returns: int -- -1 == illegal auth parameters (lengths) */
512 1.1 christos /* 0 == valid parameters, pointer advanced */
513 1.1 christos /* Parameters: rm(I) - pointer to RPC message structure */
514 1.1 christos /* auth(I) - pointer to RPC auth structure */
515 1.1 christos /* buf(IO) - pointer to location within convenience buffer */
516 1.1 christos /* */
517 1.1 christos /* Record auth data length & location of auth data, then advance past */
518 1.1 christos /* it. */
519 1.1 christos /* -------------------------------------------------------------------- */
520 1.1 christos static int
521 1.2 christos ipf_p_rpcb_skipauth(rpc_msg_t *rm, xdr_auth_t *auth, u_32_t **buf)
522 1.1 christos {
523 1.1 christos u_32_t *p, xdr;
524 1.1 christos
525 1.1 christos p = *buf;
526 1.1 christos
527 1.1 christos /* Make sure we have enough space for expected fixed auth parms. */
528 1.1 christos if (RPCB_BUF_GEQ(rm, p, 8) == 0)
529 1.1 christos return(-1);
530 1.1 christos
531 1.1 christos p++; /* We don't care about auth_flavor. */
532 1.1 christos
533 1.1 christos auth->xa_string.xs_len = p;
534 1.1 christos xdr = B(p++); /* Length of auth_data */
535 1.1 christos
536 1.1 christos /* Test for absurdity / illegality of auth_data length. */
537 1.1 christos if ((XDRALIGN(xdr) < xdr) || (RPCB_BUF_GEQ(rm, p, XDRALIGN(xdr)) == 0))
538 1.1 christos return(-1);
539 1.1 christos
540 1.1 christos auth->xa_string.xs_str = (char *)p;
541 1.1 christos
542 1.1 christos p += XDRALIGN(xdr); /* Advance our location. */
543 1.1 christos
544 1.1 christos *buf = (u_32_t *)p;
545 1.1 christos
546 1.1 christos return(0);
547 1.1 christos }
548 1.1 christos
549 1.1 christos /* -------------------------------------------------------------------- */
550 1.1 christos /* Function: ipf_p_rpcb_insert */
551 1.1 christos /* Returns: int -- -1 == list insertion failed, */
552 1.1 christos /* 0 == item successfully added */
553 1.1 christos /* Parameters: rs(I) - pointer to RPCB session structure */
554 1.1 christos /* rx(I) - pointer to RPCB transaction structure */
555 1.1 christos /* -------------------------------------------------------------------- */
556 1.1 christos static int
557 1.2 christos ipf_p_rpcb_insert(rpcb_session_t *rs, rpcb_xact_t *rx)
558 1.1 christos {
559 1.1 christos rpcb_xact_t *rxp;
560 1.1 christos
561 1.1 christos rxp = ipf_p_rpcb_lookup(rs, rx->rx_xid);
562 1.1 christos if (rxp != NULL) {
563 1.1 christos ++rxp->rx_ref;
564 1.1 christos return(0);
565 1.1 christos }
566 1.1 christos
567 1.1 christos if (rpcbcnt == RPCB_MAXREQS)
568 1.1 christos return(-1);
569 1.1 christos
570 1.1 christos KMALLOC(rxp, rpcb_xact_t *);
571 1.1 christos if (rxp == NULL)
572 1.1 christos return(-1);
573 1.1 christos
574 1.1 christos bcopy((char *)rx, (char *)rxp, sizeof(*rx));
575 1.1 christos
576 1.1 christos if (rs->rs_rxlist != NULL)
577 1.1 christos rs->rs_rxlist->rx_pnext = &rxp->rx_next;
578 1.1 christos
579 1.1 christos rxp->rx_pnext = &rs->rs_rxlist;
580 1.1 christos rxp->rx_next = rs->rs_rxlist;
581 1.1 christos rs->rs_rxlist = rxp;
582 1.1 christos
583 1.1 christos rxp->rx_ref = 1;
584 1.1 christos
585 1.1 christos ++rpcbcnt;
586 1.1 christos
587 1.1 christos return(0);
588 1.1 christos }
589 1.1 christos
590 1.1 christos /* -------------------------------------------------------------------- */
591 1.1 christos /* Function: ipf_p_rpcb_xdrrpcb */
592 1.1 christos /* Returns: int -- -1 == failure to properly decode the request */
593 1.1 christos /* 0 == rpcb successfully decoded */
594 1.1 christos /* Parameters: rs(I) - pointer to RPCB session structure */
595 1.1 christos /* p(I) - pointer to location within session buffer */
596 1.1 christos /* rpcb(O) - pointer to rpcb (xdr type) structure */
597 1.1 christos /* */
598 1.1 christos /* Decode a XDR encoded rpcb structure and record its contents in rpcb */
599 1.1 christos /* within only the context of TCP/UDP over IP networks. */
600 1.1 christos /* -------------------------------------------------------------------- */
601 1.1 christos static int
602 1.2 christos ipf_p_rpcb_xdrrpcb(rpc_msg_t *rm, u_32_t *p, rpcb_args_t *ra)
603 1.1 christos {
604 1.1 christos if (!RPCB_BUF_GEQ(rm, p, 20))
605 1.1 christos return(-1);
606 1.1 christos
607 1.1 christos /* Bypass target program & version. */
608 1.1 christos p += 2;
609 1.1 christos
610 1.1 christos /* Decode r_netid. Must be "tcp" or "udp". */
611 1.1 christos if (ipf_p_rpcb_getproto(rm, &ra->ra_netid, &p) != 0)
612 1.1 christos return(-1);
613 1.1 christos
614 1.1 christos /* Decode r_maddr. */
615 1.1 christos if (ipf_p_rpcb_getuaddr(rm, &ra->ra_maddr, &p) != 0)
616 1.1 christos return(-1);
617 1.1 christos
618 1.1 christos /* Advance to r_owner and make sure it's empty. */
619 1.1 christos if (!RPCB_BUF_EQ(rm, p, 4) || (B(p) != 0))
620 1.1 christos return(-1);
621 1.1 christos
622 1.1 christos return(0);
623 1.1 christos }
624 1.1 christos
625 1.1 christos /* -------------------------------------------------------------------- */
626 1.1 christos /* Function: ipf_p_rpcb_getuaddr */
627 1.1 christos /* Returns: int -- -1 == illegal string, */
628 1.1 christos /* 0 == string parsed; contents recorded */
629 1.1 christos /* Parameters: rm(I) - pointer to RPC message structure */
630 1.1 christos /* xu(I) - pointer to universal address structure */
631 1.1 christos /* p(IO) - pointer to location within message buffer */
632 1.1 christos /* */
633 1.1 christos /* Decode the IP address / port at p and record them in xu. */
634 1.1 christos /* -------------------------------------------------------------------- */
635 1.1 christos static int
636 1.2 christos ipf_p_rpcb_getuaddr(rpc_msg_t *rm, xdr_uaddr_t *xu, u_32_t **p)
637 1.1 christos {
638 1.1 christos char *c, *i, *b, *pp;
639 1.1 christos u_int d, dd, l, t;
640 1.1 christos char uastr[24];
641 1.1 christos
642 1.1 christos /* Test for string length. */
643 1.1 christos if (!RPCB_BUF_GEQ(rm, *p, 4))
644 1.1 christos return(-1);
645 1.1 christos
646 1.1 christos xu->xu_xslen = (*p)++;
647 1.1 christos xu->xu_xsstr = (char *)*p;
648 1.1 christos
649 1.1 christos /* Length check */
650 1.1 christos l = B(xu->xu_xslen);
651 1.1 christos if (l < 11 || l > 23 || !RPCB_BUF_GEQ(rm, *p, XDRALIGN(l)))
652 1.1 christos return(-1);
653 1.1 christos
654 1.1 christos /* Advance p */
655 1.1 christos *(char **)p += XDRALIGN(l);
656 1.1 christos
657 1.1 christos /* Copy string to local buffer & terminate C style */
658 1.1 christos bcopy(xu->xu_xsstr, uastr, l);
659 1.1 christos uastr[l] = '\0';
660 1.1 christos
661 1.1 christos i = (char *)&xu->xu_ip;
662 1.1 christos pp = (char *)&xu->xu_port;
663 1.1 christos
664 1.1 christos /*
665 1.1 christos * Expected format: a.b.c.d.e.f where [a-d] correspond to bytes of
666 1.1 christos * an IP address and [ef] are the bytes of a L4 port.
667 1.1 christos */
668 1.1 christos if (!(ISDIGIT(uastr[0]) && ISDIGIT(uastr[l-1])))
669 1.1 christos return(-1);
670 1.1 christos b = uastr;
671 1.1 christos for (c = &uastr[1], d = 0, dd = 0; c < &uastr[l-1]; c++) {
672 1.1 christos if (ISDIGIT(*c)) {
673 1.1 christos dd = 0;
674 1.1 christos continue;
675 1.1 christos }
676 1.1 christos if (*c == '.') {
677 1.1 christos if (dd != 0)
678 1.1 christos return(-1);
679 1.1 christos
680 1.1 christos /* Check for ASCII byte. */
681 1.1 christos *c = '\0';
682 1.1 christos t = ipf_p_rpcb_atoi(b);
683 1.1 christos if (t > 255)
684 1.1 christos return(-1);
685 1.1 christos
686 1.1 christos /* Aim b at beginning of the next byte. */
687 1.1 christos b = c + 1;
688 1.1 christos
689 1.1 christos /* Switch off IP addr vs port parsing. */
690 1.1 christos if (d < 4)
691 1.1 christos i[d++] = t & 0xff;
692 1.1 christos else
693 1.1 christos pp[d++ - 4] = t & 0xff;
694 1.1 christos
695 1.1 christos dd = 1;
696 1.1 christos continue;
697 1.1 christos }
698 1.1 christos return(-1);
699 1.1 christos }
700 1.1 christos if (d != 5) /* String must contain exactly 5 periods. */
701 1.1 christos return(-1);
702 1.1 christos
703 1.1 christos /* Handle the last byte (port low byte) */
704 1.1 christos t = ipf_p_rpcb_atoi(b);
705 1.1 christos if (t > 255)
706 1.1 christos return(-1);
707 1.1 christos pp[d - 4] = t & 0xff;
708 1.1 christos
709 1.1 christos return(0);
710 1.1 christos }
711 1.1 christos
712 1.1 christos /* -------------------------------------------------------------------- */
713 1.1 christos /* Function: ipf_p_rpcb_atoi (XXX should be generic for all proxies) */
714 1.1 christos /* Returns: int -- integer representation of supplied string */
715 1.1 christos /* Parameters: ptr(I) - input string */
716 1.1 christos /* */
717 1.1 christos /* Simple version of atoi(3) ripped from ip_rcmd_pxy.c. */
718 1.1 christos /* -------------------------------------------------------------------- */
719 1.1 christos static u_int
720 1.2 christos ipf_p_rpcb_atoi(char *ptr)
721 1.1 christos {
722 1.2 christos char *s = ptr, c;
723 1.2 christos u_int i = 0;
724 1.1 christos
725 1.1 christos while (((c = *s++) != '\0') && ISDIGIT(c)) {
726 1.1 christos i *= 10;
727 1.1 christos i += c - '0';
728 1.1 christos }
729 1.1 christos return i;
730 1.1 christos }
731 1.1 christos
732 1.1 christos /* -------------------------------------------------------------------- */
733 1.1 christos /* Function: ipf_p_rpcb_modreq */
734 1.1 christos /* Returns: int -- change in datagram length */
735 1.1 christos /* APR_ERR(2) - critical failure */
736 1.1 christos /* Parameters: fin(I) - pointer to packet information */
737 1.1 christos /* nat(I) - pointer to NAT session */
738 1.1 christos /* rm(I) - pointer to RPC message structure */
739 1.1 christos /* m(I) - pointer to mbuf chain */
740 1.1 christos /* off(I) - current offset within mbuf chain */
741 1.1 christos /* */
742 1.1 christos /* When external and internal addresses differ, we rewrite the former */
743 1.1 christos /* with the latter. (This is exclusive to protocol versions 3 & 4). */
744 1.1 christos /* -------------------------------------------------------------------- */
745 1.1 christos static int
746 1.2 christos ipf_p_rpcb_modreq(fr_info_t *fin, nat_t *nat, rpc_msg_t *rm, mb_t *m, u_int off)
747 1.1 christos {
748 1.1 christos u_int len, xlen, pos, bogo;
749 1.1 christos rpcb_args_t *ra;
750 1.1 christos char uaddr[24];
751 1.1 christos udphdr_t *udp;
752 1.1 christos char *i, *p;
753 1.1 christos int diff;
754 1.1 christos
755 1.1 christos ra = &rm->rm_call.rc_rpcbargs;
756 1.1 christos i = (char *)&nat->nat_odstaddr;
757 1.1 christos p = (char *)&nat->nat_odport;
758 1.1 christos
759 1.1 christos /* Form new string. */
760 1.1 christos bzero(uaddr, sizeof(uaddr)); /* Just in case we need padding. */
761 1.1 christos #if defined(SNPRINTF) && defined(_KERNEL)
762 1.1 christos SNPRINTF(uaddr, sizeof(uaddr),
763 1.2 christos "%u.%u.%u.%u.%u.%u", i[0] & 0xff, i[1] & 0xff,
764 1.2 christos i[2] & 0xff, i[3] & 0xff, p[0] & 0xff, p[1] & 0xff);
765 1.1 christos #else
766 1.1 christos (void) sprintf(uaddr,
767 1.1 christos "%u.%u.%u.%u.%u.%u", i[0] & 0xff, i[1] & 0xff,
768 1.1 christos i[2] & 0xff, i[3] & 0xff, p[0] & 0xff, p[1] & 0xff);
769 1.2 christos #endif
770 1.1 christos len = strlen(uaddr);
771 1.1 christos xlen = XDRALIGN(len);
772 1.1 christos
773 1.1 christos /* Determine mbuf offset to start writing to. */
774 1.1 christos pos = (char *)ra->ra_maddr.xu_xslen - rm->rm_msgbuf;
775 1.1 christos off += pos;
776 1.1 christos
777 1.1 christos /* Write new string length. */
778 1.1 christos bogo = htonl(len);
779 1.2 christos COPYBACK(m, off, 4, (void *)&bogo);
780 1.1 christos off += 4;
781 1.1 christos
782 1.1 christos /* Write new string. */
783 1.1 christos COPYBACK(m, off, xlen, uaddr);
784 1.1 christos off += xlen;
785 1.1 christos
786 1.1 christos /* Write in zero r_owner. */
787 1.1 christos bogo = 0;
788 1.2 christos COPYBACK(m, off, 4, (void *)&bogo);
789 1.1 christos
790 1.1 christos /* Determine difference in data lengths. */
791 1.1 christos diff = xlen - XDRALIGN(B(ra->ra_maddr.xu_xslen));
792 1.1 christos
793 1.1 christos /*
794 1.1 christos * If our new string has a different length, make necessary
795 1.1 christos * adjustments.
796 1.1 christos */
797 1.1 christos if (diff != 0) {
798 1.1 christos udp = fin->fin_dp;
799 1.1 christos udp->uh_ulen = htons(ntohs(udp->uh_ulen) + diff);
800 1.1 christos fin->fin_plen += diff;
801 1.1 christos fin->fin_ip->ip_len = htons(fin->fin_plen);
802 1.1 christos fin->fin_dlen += diff;
803 1.1 christos /* XXX Storage lengths. */
804 1.1 christos }
805 1.1 christos
806 1.1 christos return(diff);
807 1.1 christos }
808 1.1 christos
809 1.1 christos /* -------------------------------------------------------------------- */
810 1.1 christos /* Function: ipf_p_rpcb_decoderep */
811 1.1 christos /* Returns: int - -1 == bad request or critical failure, */
812 1.1 christos /* 0 == valid, negative reply */
813 1.1 christos /* 1 == vaddlid, positive reply; needs no changes */
814 1.1 christos /* Parameters: fin(I) - pointer to packet information */
815 1.1 christos /* nat(I) - pointer to NAT session structure */
816 1.1 christos /* rs(I) - pointer to RPCB session structure */
817 1.1 christos /* rm(I) - pointer to RPC message structure */
818 1.1 christos /* rxp(O) - pointer to RPCB transaction structure */
819 1.1 christos /* */
820 1.1 christos /* Take a presumed RPCB reply, extract the XID, search for the original */
821 1.1 christos /* request information, and determine whether the request was accepted */
822 1.1 christos /* or rejected. With a valid accepted reply, go ahead and create NAT */
823 1.1 christos /* and state entries, and finish up by rewriting the packet as */
824 1.1 christos /* required. */
825 1.1 christos /* */
826 1.1 christos /* WARNING: It's the responsibility of the caller to make sure there */
827 1.1 christos /* is enough room in rs_buf for the basic RPC message "preamble". */
828 1.1 christos /* -------------------------------------------------------------------- */
829 1.1 christos static int
830 1.2 christos ipf_p_rpcb_decoderep(fr_info_t *fin, nat_t *nat, rpcb_session_t *rs,
831 1.2 christos rpc_msg_t *rm, rpcb_xact_t **rxp)
832 1.1 christos {
833 1.1 christos rpcb_listp_t *rl;
834 1.1 christos rpcb_entry_t *re;
835 1.1 christos rpcb_xact_t *rx;
836 1.1 christos u_32_t xdr, *p;
837 1.1 christos rpc_resp_t *rr;
838 1.1 christos int rv, cnt;
839 1.1 christos
840 1.1 christos p = (u_32_t *)rm->rm_msgbuf;
841 1.1 christos
842 1.1 christos bzero((char *)&rx, sizeof(rx));
843 1.1 christos rr = &rm->rm_resp;
844 1.1 christos
845 1.1 christos rm->rm_xid = p;
846 1.1 christos xdr = B(p++); /* Record this message's XID. */
847 1.1 christos
848 1.1 christos /* Lookup XID */
849 1.1 christos MUTEX_ENTER(&rs->rs_rxlock);
850 1.1 christos if ((rx = ipf_p_rpcb_lookup(rs, xdr)) == NULL) {
851 1.1 christos MUTEX_EXIT(&rs->rs_rxlock);
852 1.1 christos return(-1);
853 1.1 christos }
854 1.1 christos ++rx->rx_ref; /* per thread reference */
855 1.1 christos MUTEX_EXIT(&rs->rs_rxlock);
856 1.1 christos
857 1.1 christos *rxp = rx;
858 1.1 christos
859 1.1 christos /* Test call vs reply */
860 1.1 christos if (B(p++) != RPCB_REPLY)
861 1.1 christos return(-1);
862 1.1 christos
863 1.1 christos /* Test reply_stat */
864 1.1 christos switch(B(p++))
865 1.1 christos {
866 1.1 christos case RPCB_MSG_DENIED:
867 1.1 christos return(0);
868 1.1 christos case RPCB_MSG_ACCEPTED:
869 1.1 christos break;
870 1.1 christos default:
871 1.1 christos return(-1);
872 1.1 christos }
873 1.1 christos
874 1.1 christos /* Bypass RPC authentication stuff. */
875 1.1 christos if (ipf_p_rpcb_skipauth(rm, &rr->rr_authverf, &p) != 0)
876 1.1 christos return(-1);
877 1.1 christos
878 1.1 christos /* Test accept status */
879 1.1 christos if (!RPCB_BUF_GEQ(rm, p, 4))
880 1.1 christos return(-1);
881 1.1 christos if (B(p++) != 0)
882 1.1 christos return(0);
883 1.1 christos
884 1.1 christos /* Parse out the expected reply */
885 1.1 christos switch(rx->rx_type)
886 1.1 christos {
887 1.1 christos case RPCB_RES_PMAP:
888 1.1 christos /* There must be only one 4 byte argument. */
889 1.1 christos if (!RPCB_BUF_EQ(rm, p, 4))
890 1.1 christos return(-1);
891 1.1 christos
892 1.1 christos rr->rr_v2 = p;
893 1.1 christos xdr = B(rr->rr_v2);
894 1.1 christos
895 1.1 christos /* Reply w/ a 0 port indicates service isn't registered */
896 1.1 christos if (xdr == 0)
897 1.1 christos return(0);
898 1.1 christos
899 1.1 christos /* Is the value sane? */
900 1.1 christos if (xdr > 65535)
901 1.1 christos return(-1);
902 1.1 christos
903 1.1 christos /* Create NAT & state table entries. */
904 1.1 christos if (ipf_p_rpcb_getnat(fin, nat, rx->rx_proto, (u_int)xdr) != 0)
905 1.1 christos return(-1);
906 1.1 christos break;
907 1.1 christos case RPCB_RES_STRING:
908 1.1 christos /* Expecting a XDR string; need 4 bytes for length */
909 1.1 christos if (!RPCB_BUF_GEQ(rm, p, 4))
910 1.1 christos return(-1);
911 1.1 christos
912 1.1 christos rr->rr_v3.xu_str.xs_len = p++;
913 1.1 christos rr->rr_v3.xu_str.xs_str = (char *)p;
914 1.1 christos
915 1.1 christos xdr = B(rr->rr_v3.xu_xslen);
916 1.1 christos
917 1.1 christos /* A null string indicates an unregistered service */
918 1.1 christos if ((xdr == 0) && RPCB_BUF_EQ(rm, p, 0))
919 1.1 christos return(0);
920 1.1 christos
921 1.1 christos /* Decode the target IP address / port. */
922 1.1 christos if (ipf_p_rpcb_getuaddr(rm, &rr->rr_v3, &p) != 0)
923 1.1 christos return(-1);
924 1.1 christos
925 1.1 christos /* Validate the IP address and port contained. */
926 1.1 christos if (nat->nat_odstaddr != rr->rr_v3.xu_ip)
927 1.1 christos return(-1);
928 1.1 christos
929 1.1 christos /* Create NAT & state table entries. */
930 1.1 christos if (ipf_p_rpcb_getnat(fin, nat, rx->rx_proto,
931 1.1 christos (u_int)rr->rr_v3.xu_port) != 0)
932 1.1 christos return(-1);
933 1.1 christos break;
934 1.1 christos case RPCB_RES_LIST:
935 1.1 christos if (!RPCB_BUF_GEQ(rm, p, 4))
936 1.1 christos return(-1);
937 1.1 christos /* rpcb_entry_list_ptr */
938 1.1 christos switch(B(p))
939 1.1 christos {
940 1.1 christos case 0:
941 1.1 christos return(0);
942 1.1 christos /*NOTREACHED*/
943 1.1 christos break;
944 1.1 christos case 1:
945 1.1 christos break;
946 1.1 christos default:
947 1.1 christos return(-1);
948 1.1 christos }
949 1.1 christos rl = &rr->rr_v4;
950 1.1 christos rl->rl_list = p++;
951 1.1 christos cnt = 0;
952 1.1 christos
953 1.1 christos for(;;) {
954 1.1 christos re = &rl->rl_entries[rl->rl_cnt];
955 1.1 christos if (ipf_p_rpcb_getuaddr(rm, &re->re_maddr, &p) != 0)
956 1.1 christos return(-1);
957 1.1 christos if (ipf_p_rpcb_getproto(rm, &re->re_netid, &p) != 0)
958 1.1 christos return(-1);
959 1.1 christos /* re_semantics & re_pfamily length */
960 1.1 christos if (!RPCB_BUF_GEQ(rm, p, 12))
961 1.1 christos return(-1);
962 1.1 christos p++; /* Skipping re_semantics. */
963 1.1 christos xdr = B(p++);
964 1.1 christos if ((xdr != 4) || strncmp((char *)p, "inet", 4))
965 1.1 christos return(-1);
966 1.1 christos p++;
967 1.1 christos if (ipf_p_rpcb_getproto(rm, &re->re_proto, &p) != 0)
968 1.1 christos return(-1);
969 1.1 christos if (!RPCB_BUF_GEQ(rm, p, 4))
970 1.1 christos return(-1);
971 1.1 christos re->re_more = p;
972 1.1 christos if (B(re->re_more) > 1) /* 0,1 only legal values */
973 1.1 christos return(-1);
974 1.1 christos ++rl->rl_cnt;
975 1.1 christos ++cnt;
976 1.1 christos if (B(re->re_more) == 0)
977 1.1 christos break;
978 1.1 christos /* Replies in max out at 2; TCP and/or UDP */
979 1.1 christos if (cnt > 2)
980 1.1 christos return(-1);
981 1.1 christos p++;
982 1.1 christos }
983 1.1 christos
984 1.1 christos for(rl->rl_cnt = 0; rl->rl_cnt < cnt; rl->rl_cnt++) {
985 1.1 christos re = &rl->rl_entries[rl->rl_cnt];
986 1.1 christos rv = ipf_p_rpcb_getnat(fin, nat,
987 1.1 christos re->re_proto.xp_proto,
988 1.1 christos (u_int)re->re_maddr.xu_port);
989 1.1 christos if (rv != 0)
990 1.1 christos return(-1);
991 1.1 christos }
992 1.1 christos break;
993 1.1 christos default:
994 1.1 christos /*CONSTANTCONDITION*/
995 1.1 christos IPF_PANIC(1, ("illegal rx_type %d", rx->rx_type));
996 1.1 christos }
997 1.1 christos
998 1.1 christos return(1);
999 1.1 christos }
1000 1.1 christos
1001 1.1 christos /* -------------------------------------------------------------------- */
1002 1.1 christos /* Function: ipf_p_rpcb_lookup */
1003 1.1 christos /* Returns: rpcb_xact_t * - NULL == no matching record, */
1004 1.1 christos /* else pointer to relevant entry */
1005 1.1 christos /* Parameters: rs(I) - pointer to RPCB session */
1006 1.1 christos /* xid(I) - XID to look for */
1007 1.1 christos /* -------------------------------------------------------------------- */
1008 1.1 christos static rpcb_xact_t *
1009 1.2 christos ipf_p_rpcb_lookup(rpcb_session_t *rs, u_32_t xid)
1010 1.1 christos {
1011 1.1 christos rpcb_xact_t *rx;
1012 1.1 christos
1013 1.1 christos if (rs->rs_rxlist == NULL)
1014 1.1 christos return(NULL);
1015 1.1 christos
1016 1.1 christos for (rx = rs->rs_rxlist; rx != NULL; rx = rx->rx_next)
1017 1.1 christos if (rx->rx_xid == xid)
1018 1.1 christos break;
1019 1.1 christos
1020 1.1 christos return(rx);
1021 1.1 christos }
1022 1.1 christos
1023 1.1 christos /* -------------------------------------------------------------------- */
1024 1.1 christos /* Function: ipf_p_rpcb_deref */
1025 1.1 christos /* Returns: (void) */
1026 1.1 christos /* Parameters: rs(I) - pointer to RPCB session */
1027 1.1 christos /* rx(I) - pointer to RPC transaction struct to remove */
1028 1.1 christos /* force(I) - indicates to delete entry regardless of */
1029 1.1 christos /* reference count */
1030 1.1 christos /* Locking: rs->rs_rxlock must be held write only */
1031 1.1 christos /* */
1032 1.1 christos /* Free the RPCB transaction record rx from the chain of entries. */
1033 1.1 christos /* -------------------------------------------------------------------- */
1034 1.1 christos static void
1035 1.2 christos ipf_p_rpcb_deref(rpcb_session_t *rs, rpcb_xact_t *rx)
1036 1.1 christos {
1037 1.1 christos rs = rs; /* LINT */
1038 1.1 christos
1039 1.1 christos if (rx == NULL)
1040 1.1 christos return;
1041 1.1 christos
1042 1.1 christos if (--rx->rx_ref != 0)
1043 1.1 christos return;
1044 1.1 christos
1045 1.1 christos if (rx->rx_next != NULL)
1046 1.1 christos rx->rx_next->rx_pnext = rx->rx_pnext;
1047 1.1 christos
1048 1.1 christos *rx->rx_pnext = rx->rx_next;
1049 1.1 christos
1050 1.1 christos KFREE(rx);
1051 1.1 christos
1052 1.1 christos --rpcbcnt;
1053 1.1 christos }
1054 1.1 christos
1055 1.1 christos /* -------------------------------------------------------------------- */
1056 1.1 christos /* Function: ipf_p_rpcb_getproto */
1057 1.1 christos /* Returns: int - -1 == illegal protocol/netid, */
1058 1.1 christos /* 0 == legal protocol/netid */
1059 1.1 christos /* Parameters: rm(I) - pointer to RPC message structure */
1060 1.1 christos /* xp(I) - pointer to netid structure */
1061 1.1 christos /* p(IO) - pointer to location within packet buffer */
1062 1.1 christos /* */
1063 1.1 christos /* Decode netid/proto stored at p and record its numeric value. */
1064 1.1 christos /* -------------------------------------------------------------------- */
1065 1.1 christos static int
1066 1.2 christos ipf_p_rpcb_getproto(rpc_msg_t *rm, xdr_proto_t *xp, u_32_t **p)
1067 1.1 christos {
1068 1.1 christos u_int len;
1069 1.1 christos
1070 1.1 christos /* Must have 4 bytes for length & 4 bytes for "tcp" or "udp". */
1071 1.1 christos if (!RPCB_BUF_GEQ(rm, p, 8))
1072 1.1 christos return(-1);
1073 1.1 christos
1074 1.1 christos xp->xp_xslen = (*p)++;
1075 1.1 christos xp->xp_xsstr = (char *)*p;
1076 1.1 christos
1077 1.1 christos /* Test the string length. */
1078 1.1 christos len = B(xp->xp_xslen);
1079 1.1 christos if (len != 3)
1080 1.1 christos return(-1);
1081 1.1 christos
1082 1.1 christos /* Test the actual string & record the protocol accordingly. */
1083 1.1 christos if (!strncmp((char *)xp->xp_xsstr, "tcp\0", 4))
1084 1.1 christos xp->xp_proto = IPPROTO_TCP;
1085 1.1 christos else if (!strncmp((char *)xp->xp_xsstr, "udp\0", 4))
1086 1.1 christos xp->xp_proto = IPPROTO_UDP;
1087 1.1 christos else {
1088 1.1 christos return(-1);
1089 1.1 christos }
1090 1.1 christos
1091 1.1 christos /* Advance past the string. */
1092 1.1 christos (*p)++;
1093 1.1 christos
1094 1.1 christos return(0);
1095 1.1 christos }
1096 1.1 christos
1097 1.1 christos /* -------------------------------------------------------------------- */
1098 1.1 christos /* Function: ipf_p_rpcb_getnat */
1099 1.1 christos /* Returns: int -- -1 == failed to create table entries, */
1100 1.1 christos /* 0 == success */
1101 1.1 christos /* Parameters: fin(I) - pointer to packet information */
1102 1.1 christos /* nat(I) - pointer to NAT table entry */
1103 1.1 christos /* proto(I) - transport protocol for new entries */
1104 1.1 christos /* port(I) - new port to use w/ wildcard table entries */
1105 1.1 christos /* */
1106 1.1 christos /* Create state and NAT entries to handle an anticipated connection */
1107 1.1 christos /* attempt between RPC client and server. */
1108 1.1 christos /* -------------------------------------------------------------------- */
1109 1.1 christos static int
1110 1.2 christos ipf_p_rpcb_getnat(fr_info_t *fin, nat_t *nat, u_int proto, u_int port)
1111 1.1 christos {
1112 1.1 christos ipf_main_softc_t *softc = fin->fin_main_soft;
1113 1.1 christos ipnat_t *ipn, ipnat;
1114 1.1 christos tcphdr_t tcp;
1115 1.1 christos ipstate_t *is;
1116 1.1 christos fr_info_t fi;
1117 1.1 christos nat_t *natl;
1118 1.1 christos int nflags;
1119 1.1 christos
1120 1.1 christos ipn = nat->nat_ptr;
1121 1.1 christos
1122 1.1 christos /* Generate dummy fr_info */
1123 1.1 christos bcopy((char *)fin, (char *)&fi, sizeof(fi));
1124 1.1 christos fi.fin_out = 0;
1125 1.1 christos fi.fin_p = proto;
1126 1.1 christos fi.fin_sport = 0;
1127 1.1 christos fi.fin_dport = port & 0xffff;
1128 1.1 christos fi.fin_flx |= FI_IGNORE;
1129 1.1 christos fi.fin_saddr = nat->nat_osrcaddr;
1130 1.1 christos fi.fin_daddr = nat->nat_odstaddr;
1131 1.1 christos
1132 1.1 christos bzero((char *)&tcp, sizeof(tcp));
1133 1.1 christos tcp.th_dport = htons(port);
1134 1.1 christos
1135 1.1 christos if (proto == IPPROTO_TCP) {
1136 1.1 christos tcp.th_win = htons(8192);
1137 1.1 christos TCP_OFF_A(&tcp, sizeof(tcphdr_t) >> 2);
1138 1.1 christos fi.fin_dlen = sizeof(tcphdr_t);
1139 1.1 christos tcp.th_flags = TH_SYN;
1140 1.1 christos nflags = NAT_TCP;
1141 1.1 christos } else {
1142 1.1 christos fi.fin_dlen = sizeof(udphdr_t);
1143 1.1 christos nflags = NAT_UDP;
1144 1.1 christos }
1145 1.1 christos
1146 1.1 christos nflags |= SI_W_SPORT|NAT_SEARCH;
1147 1.1 christos fi.fin_dp = &tcp;
1148 1.1 christos fi.fin_plen = fi.fin_hlen + fi.fin_dlen;
1149 1.1 christos
1150 1.1 christos /*
1151 1.1 christos * Search for existing NAT & state entries. Pay close attention to
1152 1.1 christos * mutexes / locks grabbed from lookup routines, as not doing so could
1153 1.1 christos * lead to bad things.
1154 1.1 christos *
1155 1.1 christos * If successful, fr_stlookup returns with ipf_state locked. We have
1156 1.1 christos * no use for this lock, so simply unlock it if necessary.
1157 1.1 christos */
1158 1.1 christos is = ipf_state_lookup(&fi, &tcp, NULL);
1159 1.1 christos if (is != NULL) {
1160 1.1 christos RWLOCK_EXIT(&softc->ipf_state);
1161 1.1 christos }
1162 1.1 christos
1163 1.1 christos RWLOCK_EXIT(&softc->ipf_nat);
1164 1.1 christos
1165 1.1 christos WRITE_ENTER(&softc->ipf_nat);
1166 1.1 christos natl = ipf_nat_inlookup(&fi, nflags, proto, fi.fin_src, fi.fin_dst);
1167 1.1 christos
1168 1.1 christos if ((natl != NULL) && (is != NULL)) {
1169 1.1 christos MUTEX_DOWNGRADE(&softc->ipf_nat);
1170 1.1 christos return(0);
1171 1.1 christos }
1172 1.1 christos
1173 1.1 christos /* Slightly modify the following structures for actual use in creating
1174 1.1 christos * NAT and/or state entries. We're primarily concerned with stripping
1175 1.1 christos * flags that may be detrimental to the creation process or simply
1176 1.1 christos * shouldn't be associated with a table entry.
1177 1.1 christos */
1178 1.1 christos fi.fin_fr = &rpcbfr;
1179 1.1 christos fi.fin_flx &= ~FI_IGNORE;
1180 1.1 christos nflags &= ~NAT_SEARCH;
1181 1.1 christos
1182 1.1 christos if (natl == NULL) {
1183 1.1 christos #ifdef USE_MUTEXES
1184 1.1 christos ipf_nat_softc_t *softn = softc->ipf_nat_soft;
1185 1.1 christos #endif
1186 1.1 christos
1187 1.1 christos /* XXX Since we're just copying the original ipn contents
1188 1.1 christos * back, would we be better off just sending a pointer to
1189 1.1 christos * the 'temp' copy off to nat_new instead?
1190 1.1 christos */
1191 1.1 christos /* Generate template/bogus NAT rule. */
1192 1.1 christos bcopy((char *)ipn, (char *)&ipnat, sizeof(ipnat));
1193 1.1 christos ipn->in_flags = nflags & IPN_TCPUDP;
1194 1.1 christos ipn->in_apr = NULL;
1195 1.1 christos ipn->in_pr[0] = proto;
1196 1.1 christos ipn->in_pr[1] = proto;
1197 1.1 christos ipn->in_dpmin = fi.fin_dport;
1198 1.1 christos ipn->in_dpmax = fi.fin_dport;
1199 1.1 christos ipn->in_dpnext = fi.fin_dport;
1200 1.1 christos ipn->in_space = 1;
1201 1.1 christos ipn->in_ippip = 1;
1202 1.1 christos if (ipn->in_flags & IPN_FILTER) {
1203 1.1 christos ipn->in_scmp = 0;
1204 1.1 christos ipn->in_dcmp = 0;
1205 1.1 christos }
1206 1.1 christos ipn->in_plabel = -1;
1207 1.1 christos
1208 1.1 christos /* Create NAT entry. return NULL if this fails. */
1209 1.1 christos MUTEX_ENTER(&softn->ipf_nat_new);
1210 1.1 christos natl = ipf_nat_add(&fi, ipn, NULL, nflags|SI_CLONE|NAT_SLAVE,
1211 1.1 christos NAT_INBOUND);
1212 1.1 christos MUTEX_EXIT(&softn->ipf_nat_new);
1213 1.1 christos
1214 1.1 christos bcopy((char *)&ipnat, (char *)ipn, sizeof(ipnat));
1215 1.1 christos
1216 1.1 christos if (natl == NULL) {
1217 1.1 christos MUTEX_DOWNGRADE(&softc->ipf_nat);
1218 1.1 christos return(-1);
1219 1.1 christos }
1220 1.1 christos
1221 1.1 christos fi.fin_saddr = natl->nat_nsrcaddr;
1222 1.1 christos fi.fin_daddr = natl->nat_ndstaddr;
1223 1.1 christos ipn->in_use++;
1224 1.1 christos (void) ipf_nat_proto(&fi, natl, nflags);
1225 1.1 christos MUTEX_ENTER(&natl->nat_lock);
1226 1.1 christos ipf_nat_update(&fi, natl);
1227 1.1 christos MUTEX_EXIT(&natl->nat_lock);
1228 1.1 christos }
1229 1.1 christos MUTEX_DOWNGRADE(&softc->ipf_nat);
1230 1.1 christos
1231 1.1 christos if (is == NULL) {
1232 1.1 christos /* Create state entry. Return NULL if this fails. */
1233 1.1 christos fi.fin_flx |= FI_NATED;
1234 1.1 christos fi.fin_flx &= ~FI_STATE;
1235 1.1 christos nflags &= NAT_TCPUDP;
1236 1.1 christos nflags |= SI_W_SPORT|SI_CLONE;
1237 1.1 christos
1238 1.1 christos if (ipf_state_add(softc, &fi, NULL, nflags) != 0) {
1239 1.1 christos /*
1240 1.1 christos * XXX nat_delete is private to ip_nat.c. Should
1241 1.1 christos * check w/ Darren about this one.
1242 1.1 christos *
1243 1.1 christos * nat_delete(natl, NL_EXPIRE);
1244 1.1 christos */
1245 1.1 christos return(-1);
1246 1.1 christos }
1247 1.1 christos }
1248 1.1 christos
1249 1.1 christos return(0);
1250 1.1 christos }
1251 1.1 christos
1252 1.1 christos /* -------------------------------------------------------------------- */
1253 1.1 christos /* Function: ipf_p_rpcb_modv3 */
1254 1.1 christos /* Returns: int -- change in packet length */
1255 1.1 christos /* Parameters: fin(I) - pointer to packet information */
1256 1.1 christos /* nat(I) - pointer to NAT session */
1257 1.1 christos /* rm(I) - pointer to RPC message structure */
1258 1.1 christos /* m(I) - pointer to mbuf chain */
1259 1.1 christos /* off(I) - offset within mbuf chain */
1260 1.1 christos /* */
1261 1.1 christos /* Write a new universal address string to this packet, adjusting */
1262 1.1 christos /* lengths as necessary. */
1263 1.1 christos /* -------------------------------------------------------------------- */
1264 1.1 christos static int
1265 1.2 christos ipf_p_rpcb_modv3(fr_info_t *fin, nat_t *nat, rpc_msg_t *rm, mb_t *m, u_int off)
1266 1.1 christos {
1267 1.1 christos u_int len, xlen, pos, bogo;
1268 1.1 christos rpc_resp_t *rr;
1269 1.1 christos char uaddr[24];
1270 1.1 christos char *i, *p;
1271 1.1 christos int diff;
1272 1.1 christos
1273 1.1 christos rr = &rm->rm_resp;
1274 1.1 christos i = (char *)&nat->nat_ndstaddr;
1275 1.1 christos p = (char *)&rr->rr_v3.xu_port;
1276 1.1 christos
1277 1.1 christos /* Form new string. */
1278 1.1 christos bzero(uaddr, sizeof(uaddr)); /* Just in case we need padding. */
1279 1.1 christos #if defined(SNPRINTF) && defined(_KERNEL)
1280 1.1 christos SNPRINTF(uaddr, sizeof(uaddr),
1281 1.2 christos "%u.%u.%u.%u.%u.%u", i[0] & 0xff, i[1] & 0xff,
1282 1.2 christos i[2] & 0xff, i[3] & 0xff, p[0] & 0xff, p[1] & 0xff);
1283 1.1 christos #else
1284 1.1 christos (void) sprintf(uaddr,
1285 1.1 christos "%u.%u.%u.%u.%u.%u", i[0] & 0xff, i[1] & 0xff,
1286 1.1 christos i[2] & 0xff, i[3] & 0xff, p[0] & 0xff, p[1] & 0xff);
1287 1.2 christos #endif
1288 1.1 christos len = strlen(uaddr);
1289 1.1 christos xlen = XDRALIGN(len);
1290 1.1 christos
1291 1.1 christos /* Determine mbuf offset to write to. */
1292 1.1 christos pos = (char *)rr->rr_v3.xu_xslen - rm->rm_msgbuf;
1293 1.1 christos off += pos;
1294 1.1 christos
1295 1.1 christos /* Write new string length. */
1296 1.1 christos bogo = htonl(len);
1297 1.2 christos COPYBACK(m, off, 4, (void *)&bogo);
1298 1.1 christos off += 4;
1299 1.1 christos
1300 1.1 christos /* Write new string. */
1301 1.1 christos COPYBACK(m, off, xlen, uaddr);
1302 1.1 christos
1303 1.1 christos /* Determine difference in data lengths. */
1304 1.1 christos diff = xlen - XDRALIGN(B(rr->rr_v3.xu_xslen));
1305 1.1 christos
1306 1.1 christos /*
1307 1.1 christos * If our new string has a different length, make necessary
1308 1.1 christos * adjustments.
1309 1.1 christos */
1310 1.1 christos if (diff != 0)
1311 1.1 christos ipf_p_rpcb_fixlen(fin, diff);
1312 1.1 christos
1313 1.1 christos return(diff);
1314 1.1 christos }
1315 1.1 christos
1316 1.1 christos /* -------------------------------------------------------------------- */
1317 1.1 christos /* Function: ipf_p_rpcb_modv4 */
1318 1.1 christos /* Returns: int -- change in packet length */
1319 1.1 christos /* Parameters: fin(I) - pointer to packet information */
1320 1.1 christos /* nat(I) - pointer to NAT session */
1321 1.1 christos /* rm(I) - pointer to RPC message structure */
1322 1.1 christos /* m(I) - pointer to mbuf chain */
1323 1.1 christos /* off(I) - offset within mbuf chain */
1324 1.1 christos /* */
1325 1.1 christos /* Write new rpcb_entry list, adjusting lengths as necessary. */
1326 1.1 christos /* -------------------------------------------------------------------- */
1327 1.1 christos static int
1328 1.2 christos ipf_p_rpcb_modv4(fr_info_t *fin, nat_t *nat, rpc_msg_t *rm, mb_t *m, u_int off)
1329 1.1 christos {
1330 1.1 christos u_int len, xlen, pos, bogo;
1331 1.1 christos rpcb_listp_t *rl;
1332 1.1 christos rpcb_entry_t *re;
1333 1.1 christos rpc_resp_t *rr;
1334 1.1 christos char uaddr[24];
1335 1.1 christos int diff, cnt;
1336 1.1 christos char *i, *p;
1337 1.1 christos
1338 1.1 christos diff = 0;
1339 1.1 christos rr = &rm->rm_resp;
1340 1.1 christos rl = &rr->rr_v4;
1341 1.1 christos
1342 1.1 christos i = (char *)&nat->nat_ndstaddr;
1343 1.1 christos
1344 1.1 christos /* Determine mbuf offset to write to. */
1345 1.1 christos re = &rl->rl_entries[0];
1346 1.1 christos pos = (char *)re->re_maddr.xu_xslen - rm->rm_msgbuf;
1347 1.1 christos off += pos;
1348 1.1 christos
1349 1.1 christos for (cnt = 0; cnt < rl->rl_cnt; cnt++) {
1350 1.1 christos re = &rl->rl_entries[cnt];
1351 1.1 christos p = (char *)&re->re_maddr.xu_port;
1352 1.1 christos
1353 1.1 christos /* Form new string. */
1354 1.1 christos bzero(uaddr, sizeof(uaddr)); /* Just in case we need
1355 1.1 christos padding. */
1356 1.1 christos #if defined(SNPRINTF) && defined(_KERNEL)
1357 1.1 christos SNPRINTF(uaddr, sizeof(uaddr),
1358 1.2 christos "%u.%u.%u.%u.%u.%u", i[0] & 0xff,
1359 1.2 christos i[1] & 0xff, i[2] & 0xff, i[3] & 0xff,
1360 1.2 christos p[0] & 0xff, p[1] & 0xff);
1361 1.1 christos #else
1362 1.1 christos (void) sprintf(uaddr,
1363 1.1 christos "%u.%u.%u.%u.%u.%u", i[0] & 0xff,
1364 1.1 christos i[1] & 0xff, i[2] & 0xff, i[3] & 0xff,
1365 1.1 christos p[0] & 0xff, p[1] & 0xff);
1366 1.2 christos #endif
1367 1.1 christos len = strlen(uaddr);
1368 1.1 christos xlen = XDRALIGN(len);
1369 1.1 christos
1370 1.1 christos /* Write new string length. */
1371 1.1 christos bogo = htonl(len);
1372 1.2 christos COPYBACK(m, off, 4, (void *)&bogo);
1373 1.1 christos off += 4;
1374 1.1 christos
1375 1.1 christos /* Write new string. */
1376 1.1 christos COPYBACK(m, off, xlen, uaddr);
1377 1.1 christos off += xlen;
1378 1.1 christos
1379 1.1 christos /* Record any change in length. */
1380 1.1 christos diff += xlen - XDRALIGN(B(re->re_maddr.xu_xslen));
1381 1.1 christos
1382 1.1 christos /* If the length changed, copy back the rest of this entry. */
1383 1.1 christos len = ((char *)re->re_more + 4) -
1384 1.1 christos (char *)re->re_netid.xp_xslen;
1385 1.1 christos if (diff != 0) {
1386 1.2 christos COPYBACK(m, off, len, (void *)re->re_netid.xp_xslen);
1387 1.1 christos }
1388 1.1 christos off += len;
1389 1.1 christos }
1390 1.1 christos
1391 1.1 christos /*
1392 1.1 christos * If our new string has a different length, make necessary
1393 1.1 christos * adjustments.
1394 1.1 christos */
1395 1.1 christos if (diff != 0)
1396 1.1 christos ipf_p_rpcb_fixlen(fin, diff);
1397 1.1 christos
1398 1.1 christos return(diff);
1399 1.1 christos }
1400 1.1 christos
1401 1.1 christos
1402 1.1 christos /* -------------------------------------------------------------------- */
1403 1.1 christos /* Function: ipf_p_rpcb_fixlen */
1404 1.1 christos /* Returns: (void) */
1405 1.1 christos /* Parameters: fin(I) - pointer to packet information */
1406 1.1 christos /* len(I) - change in packet length */
1407 1.1 christos /* */
1408 1.1 christos /* Adjust various packet related lengths held in structure and packet */
1409 1.1 christos /* header fields. */
1410 1.1 christos /* -------------------------------------------------------------------- */
1411 1.1 christos static void
1412 1.2 christos ipf_p_rpcb_fixlen(fr_info_t *fin, int len)
1413 1.1 christos {
1414 1.1 christos udphdr_t *udp;
1415 1.1 christos
1416 1.1 christos udp = fin->fin_dp;
1417 1.1 christos udp->uh_ulen = htons(ntohs(udp->uh_ulen) + len);
1418 1.1 christos fin->fin_plen += len;
1419 1.1 christos fin->fin_ip->ip_len = htons(fin->fin_plen);
1420 1.1 christos fin->fin_dlen += len;
1421 1.1 christos }
1422 1.1 christos
1423 1.1 christos #undef B
1424