4 * Copyright (c) 1997-2009 Erez Zadok
5 * Copyright (c) 1989 Jan-Simon Pendry
6 * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
7 * Copyright (c) 1989 The Regents of the University of California.
10 * This code is derived from software contributed to Berkeley by
11 * Jan-Simon Pendry at Imperial College, London.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgment:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * File: am-utils/amd/rpc_fwd.c
47 * RPC packet forwarding
52 #endif /* HAVE_CONFIG_H */
57 * Note that the ID field in the external packet is only
58 * ever treated as a 32 bit opaque data object, so there
59 * is no need to convert to and from network byte ordering.
62 #define XID_ALLOC() (xid++)
63 #define MAX_PACKET_SIZE 8192 /* Maximum UDP packet size */
66 * Each pending reply has an rpc_forward structure
67 * associated with it. These have a 15 second lifespan.
68 * If a new structure is required, then an expired
69 * one will be re-allocated if available, otherwise a fresh
70 * one is allocated. Whenever a reply is received the
71 * structure is discarded.
73 typedef struct rpc_forward rpc_forward
;
75 qelem rf_q
; /* Linked list */
76 time_t rf_ttl
; /* Time to live */
77 u_int rf_xid
; /* Packet id */
78 u_int rf_oldid
; /* Original packet id */
79 fwd_fun
*rf_fwd
; /* Forwarding function */
81 struct sockaddr_in rf_sin
;
85 * Head of list of pending replies
87 qelem rpc_head
= {&rpc_head
, &rpc_head
};
93 * Allocate a rely structure
98 time_t now
= clocktime(NULL
);
99 rpc_forward
*p
= NULL
, *p2
;
102 * First search for an existing expired one.
104 ITER(p2
, rpc_forward
, &rpc_head
) {
105 if (p2
->rf_ttl
<= now
) {
112 * If one couldn't be found then allocate
113 * a new structure and link it at the
118 * Call forwarding function to say that
119 * this message was junked.
121 dlog("Re-using packet forwarding slot - id %#x", p
->rf_xid
);
123 (*p
->rf_fwd
) (0, 0, 0, &p
->rf_sin
, p
->rf_ptr
, FALSE
);
126 p
= ALLOC(struct rpc_forward
);
128 ins_que(&p
->rf_q
, &rpc_head
);
131 * Set the time to live field
132 * Timeout in 43 seconds
134 p
->rf_ttl
= now
+ 43;
141 * Free an allocated reply structure.
142 * First unlink it from the list, then
146 fwd_free(rpc_forward
*p
)
154 * Initialize the RPC forwarder
163 #ifdef HAVE_TRANSPORT_TYPE_TLI
165 * Create ping TLI socket (/dev/tcp and /dev/ticlts did not work)
166 * (HPUX-11 does not like using O_NDELAY in flags)
168 fwd_sock
= t_open("/dev/udp", O_RDWR
|O_NONBLOCK
, 0);
170 plog(XLOG_ERROR
, "unable to create RPC forwarding TLI socket: %s",
174 #else /* not HAVE_TRANSPORT_TYPE_TLI */
178 fwd_sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
180 plog(XLOG_ERROR
, "unable to create RPC forwarding socket: %m");
183 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
186 * Some things we talk to require a priv port - so make one here
188 if (bind_resv_port(fwd_sock
, (u_short
*) NULL
) < 0)
189 plog(XLOG_ERROR
, "can't bind privileged port (rpc_fwd)");
191 if (fcntl(fwd_sock
, F_SETFL
, FNDELAY
) < 0
193 && ioctl(fwd_sock
, FIONBIO
, &on
) < 0
196 plog(XLOG_ERROR
, "Can't set non-block on forwarding socket: %m");
205 * Locate a packet in the forwarding list
212 ITER(p
, rpc_forward
, &rpc_head
) {
222 * This is called to forward a packet to another
223 * RPC server. The message id is changed and noted
224 * so that when a reply appears we can tie it up
225 * correctly. Just matching the reply's source address
226 * would not work because it might come from a
230 fwd_packet(int type_id
, char *pkt
, int len
, struct sockaddr_in
*fwdto
, struct sockaddr_in
*replyto
, opaque_t cb_arg
, fwd_fun cb
)
235 #ifdef HAVE_TRANSPORT_TYPE_TLI
236 struct t_unitdata ud
;
237 #endif /* HAVE_TRANSPORT_TYPE_TLI */
239 if ((int) amd_state
>= (int) Finishing
)
243 * See if the type_id is fully specified.
244 * If so, then discard any old entries
246 * Otherwise make sure the type_id is
247 * fully qualified by allocating an id here.
249 switch (type_id
& RPC_XID_MASK
) {
250 case RPC_XID_PORTMAP
:
251 dlog("Sending PORTMAP request %#x", type_id
);
254 dlog("Sending MOUNTD request %#x", type_id
);
256 case RPC_XID_NFSPING
:
257 dlog("Sending NFS ping %#x", type_id
);
260 dlog("Sending WebNFS lookup %#x", type_id
);
263 dlog("UNKNOWN RPC XID %#x", type_id
);
267 if (type_id
& ~RPC_XID_MASK
) {
268 p
= fwd_locate(type_id
);
270 dlog("Discarding earlier rpc fwd handle");
274 dlog("Allocating a new xid...");
275 type_id
= MK_RPC_XID(type_id
, XID_ALLOC());
284 pkt_int
= (u_int
*) pkt
;
287 * Get the original packet id
289 p
->rf_oldid
= ntohl(*pkt_int
);
292 * Replace with newly allocated id
295 *pkt_int
= htonl(type_id
);
298 * The sendto may fail if, for example, the route
299 * to a remote host is lost because an intermediate
300 * gateway has gone down. Important to fill in the
301 * rest of "p" otherwise nasty things happen later...
307 dlog("Sending packet id %#x to %s:%d",
309 inet_dquad(dq
, sizeof(dq
), fwdto
->sin_addr
.s_addr
),
310 ntohs(fwdto
->sin_port
));
314 /* if NULL, remote server probably down */
316 error
= AM_ERRNO_HOST_DOWN
;
320 #ifdef HAVE_TRANSPORT_TYPE_TLI
321 ud
.addr
.buf
= (char *) fwdto
;
322 if (fwdto
) /* if NULL, set sizes to zero */
323 ud
.addr
.maxlen
= ud
.addr
.len
= sizeof(struct sockaddr_in
);
325 ud
.addr
.maxlen
= ud
.addr
.len
= 0;
326 ud
.opt
.buf
= (char *) NULL
;
327 ud
.opt
.maxlen
= ud
.opt
.len
= 0;
329 ud
.udata
.maxlen
= ud
.udata
.len
= len
;
330 if (t_sndudata(fwd_sock
, &ud
) < 0) {
331 plog(XLOG_ERROR
,"fwd_packet failed: t_errno=%d, errno=%d",t_errno
,errno
);
334 #else /* not HAVE_TRANSPORT_TYPE_TLI */
335 if (sendto(fwd_sock
, (char *) pkt
, len
, 0,
336 (struct sockaddr
*) fwdto
, sizeof(*fwdto
)) < 0)
338 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
341 * Save callback function and return address
346 p
->rf_sin
= *replyto
;
348 memset((voidp
) &p
->rf_sin
, 0, sizeof(p
->rf_sin
));
356 * Called when some data arrives on the forwarding socket
362 u_int pkt
[MAX_PACKET_SIZE
/ sizeof(u_int
) + 1];
367 struct sockaddr_in src_addr
;
368 RECVFROM_FROMLEN_TYPE src_addr_len
;
369 #ifdef HAVE_TRANSPORT_TYPE_TLI
370 struct t_unitdata ud
;
372 #endif /* HAVE_TRANSPORT_TYPE_TLI */
375 * Determine the length of the packet
377 len
= MAX_PACKET_SIZE
;
380 * Read the packet and check for validity
383 src_addr_len
= sizeof(src_addr
);
384 #ifdef HAVE_TRANSPORT_TYPE_TLI
385 ud
.addr
.buf
= (char *) &src_addr
;
386 ud
.addr
.maxlen
= ud
.addr
.len
= src_addr_len
;
387 ud
.opt
.buf
= (char *) NULL
;
388 ud
.opt
.maxlen
= ud
.opt
.len
= 0;
389 ud
.udata
.buf
= (char *) pkt
;
390 ud
.udata
.maxlen
= ud
.udata
.len
= len
;
391 /* XXX: use flags accordingly such as if T_MORE set */
392 rc
= t_rcvudata(fwd_sock
, &ud
, &flags
);
393 if (rc
== 0) /* success, reset rc to length */
396 plog(XLOG_ERROR
,"fwd_reply failed: t_errno=%d, errno=%d, flags=%d",t_errno
,errno
, flags
);
398 * Clear error indication, otherwise the error condition persists and
399 * amd gets into an infinite loop.
401 if (t_errno
== TLOOK
)
402 t_rcvuderr(fwd_sock
, NULL
);
404 #else /* not HAVE_TRANSPORT_TYPE_TLI */
405 rc
= recvfrom(fwd_sock
,
409 (struct sockaddr
*) &src_addr
,
411 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
414 * XXX: in svr4, if the T_MORE bit of flags is set, what do
417 if (rc
< 0 || src_addr_len
!= sizeof(src_addr
) ||
418 src_addr
.sin_family
!= AF_INET
) {
419 if (rc
< 0 && errno
== EINTR
)
421 plog(XLOG_ERROR
, "Error reading RPC reply: %m");
426 * Do no more work if finishing soon
428 if ((int) amd_state
>= (int) Finishing
)
432 * Find packet reference
434 pkt_int
= (u_int
*) pkt
;
435 pkt_xid
= ntohl(*pkt_int
);
437 switch (pkt_xid
& RPC_XID_MASK
) {
438 case RPC_XID_PORTMAP
:
439 dlog("Receiving PORTMAP reply %#x", pkt_xid
);
442 dlog("Receiving MOUNTD reply %#x", pkt_xid
);
444 case RPC_XID_NFSPING
:
445 dlog("Receiving NFS ping %#x", pkt_xid
);
448 dlog("Receiving WebNFS lookup %#x", pkt_xid
);
451 dlog("UNKNOWN RPC XID %#x", pkt_xid
);
455 p
= fwd_locate(pkt_xid
);
457 dlog("Can't forward reply id %#x", pkt_xid
);
463 * Put the original message id back
466 *pkt_int
= htonl(p
->rf_oldid
);
469 * Call forwarding function
471 (*p
->rf_fwd
) ((voidp
) pkt
, rc
, &src_addr
, &p
->rf_sin
, p
->rf_ptr
, TRUE
);
475 * Free forwarding info