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