2 * Copyright (c) 1997-2004 Erez Zadok
3 * Copyright (c) 1989 Jan-Simon Pendry
4 * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1989 The Regents of the University of California.
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgment:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * $Id: rpc_fwd.c,v 1.3.2.5 2004/01/06 03:15:16 ezk Exp $
46 * RPC packet forwarding
51 #endif /* HAVE_CONFIG_H */
56 * Note that the ID field in the external packet is only
57 * ever treated as a 32 bit opaque data object, so there
58 * is no need to convert to and from network byte ordering.
61 #define XID_ALLOC(struct ) (xid++)
62 #define MAX_PACKET_SIZE 8192 /* Maximum UDP packet size */
65 * Each pending reply has an rpc_forward structure
66 * associated with it. These have a 15 second lifespan.
67 * If a new structure is required, then an expired
68 * one will be re-allocated if available, otherwise a fresh
69 * one is allocated. Whenever a reply is received the
70 * structure is discarded.
72 typedef struct rpc_forward rpc_forward;
74 qelem rf_q; /* Linked list */
75 time_t rf_ttl; /* Time to live */
76 u_int rf_xid; /* Packet id */
77 u_int rf_oldid; /* Original packet id */
78 fwd_fun rf_fwd; /* Forwarding function */
80 struct sockaddr_in rf_sin;
84 * Head of list of pending replies
86 qelem rpc_head = {&rpc_head, &rpc_head};
92 * Allocate a rely structure
97 time_t now = clocktime();
98 rpc_forward *p = 0, *p2;
101 * First search for an existing expired one.
103 ITER(p2, rpc_forward, &rpc_head) {
104 if (p2->rf_ttl <= now) {
111 * If one couldn't be found then allocate
112 * a new structure and link it at the
117 * Call forwarding function to say that
118 * this message was junked.
121 dlog("Re-using packet forwarding slot - id %#x", p->rf_xid);
124 (*p->rf_fwd) (0, 0, 0, &p->rf_sin, p->rf_ptr, FALSE);
127 p = ALLOC(struct rpc_forward);
129 ins_que(&p->rf_q, &rpc_head);
132 * Set the time to live field
133 * Timeout in 43 seconds
135 p->rf_ttl = now + 43;
142 * Free an allocated reply structure.
143 * First unlink it from the list, then
147 fwd_free(rpc_forward *p)
155 * Initialize the RPC forwarder
164 #ifdef HAVE_TRANSPORT_TYPE_TLI
166 * Create ping TLI socket (/dev/tcp and /dev/ticlts did not work)
167 * (HPUX-11 does not like using O_NDELAY in flags)
169 fwd_sock = t_open("/dev/udp", O_RDWR|O_NONBLOCK, 0);
171 plog(XLOG_ERROR, "unable to create RPC forwarding TLI socket: %s",
175 #else /* not HAVE_TRANSPORT_TYPE_TLI */
179 fwd_sock = socket(AF_INET, SOCK_DGRAM, 0);
181 plog(XLOG_ERROR, "unable to create RPC forwarding socket: %m");
184 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
187 * Some things we talk to require a priv port - so make one here
189 if (bind_resv_port(fwd_sock, (u_short *) 0) < 0)
190 plog(XLOG_ERROR, "can't bind privileged port (rpc_fwd)");
192 if (fcntl(fwd_sock, F_SETFL, FNDELAY) < 0
194 && ioctl(fwd_sock, FIONBIO, &on) < 0
197 plog(XLOG_ERROR, "Can't set non-block on forwarding socket: %m");
206 * Locate a packet in the forwarding list
213 ITER(p, rpc_forward, &rpc_head) {
223 * This is called to forward a packet to another
224 * RPC server. The message id is changed and noted
225 * so that when a reply appears we can tie it up
226 * correctly. Just matching the reply's source address
227 * would not work because it might come from a
231 fwd_packet(int type_id, voidp pkt, int len, struct sockaddr_in *fwdto, struct sockaddr_in *replyto, voidp i, fwd_fun cb)
236 #ifdef HAVE_TRANSPORT_TYPE_TLI
237 struct t_unitdata ud;
238 #endif /* HAVE_TRANSPORT_TYPE_TLI */
240 if ((int) amd_state >= (int) Finishing)
244 * See if the type_id is fully specified.
245 * If so, then discard any old entries
247 * Otherwise make sure the type_id is
248 * fully qualified by allocating an id here.
251 switch (type_id & RPC_XID_MASK) {
252 case RPC_XID_PORTMAP:
253 dlog("Sending PORTMAP request");
256 dlog("Sending MOUNTD request %#x", type_id);
258 case RPC_XID_NFSPING:
259 dlog("Sending NFS ping");
262 dlog("UNKNOWN RPC XID");
267 if (type_id & ~RPC_XID_MASK) {
268 p = fwd_locate(type_id);
271 dlog("Discarding earlier rpc fwd handle");
277 dlog("Allocating a new xid...");
279 type_id = MK_RPC_XID(type_id, XID_ALLOC(struct ));
288 pkt_int = (u_int *) pkt;
291 * Get the original packet id
293 p->rf_oldid = *pkt_int;
296 * Replace with newly allocated id
298 p->rf_xid = *pkt_int = type_id;
301 * The sendto may fail if, for example, the route
302 * to a remote host is lost because an intermediate
303 * gateway has gone down. Important to fill in the
304 * rest of "p" otherwise nasty things happen later...
310 dlog("Sending packet id %#x to %s.%d",
312 inet_dquad(dq, fwdto->sin_addr.s_addr),
313 ntohs(fwdto->sin_port));
317 /* if NULL, remote server probably down */
319 error = AM_ERRNO_HOST_DOWN;
323 #ifdef HAVE_TRANSPORT_TYPE_TLI
324 ud.addr.buf = (char *) fwdto;
325 if (fwdto) /* if NULL, set sizes to zero */
326 ud.addr.maxlen = ud.addr.len = sizeof(struct sockaddr_in);
328 ud.addr.maxlen = ud.addr.len = 0;
329 ud.opt.buf = (char *) NULL;
330 ud.opt.maxlen = ud.opt.len = 0;
332 ud.udata.maxlen = ud.udata.len = len;
333 if (t_sndudata(fwd_sock, &ud) < 0) {
334 plog(XLOG_ERROR,"fwd_packet failed: t_errno=%d, errno=%d",t_errno,errno);
337 #else /* not HAVE_TRANSPORT_TYPE_TLI */
338 if (sendto(fwd_sock, (char *) pkt, len, 0,
339 (struct sockaddr *) fwdto, sizeof(*fwdto)) < 0)
341 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
344 * Save callback function and return address
349 p->rf_sin = *replyto;
351 memset((voidp) &p->rf_sin, 0, sizeof(p->rf_sin));
359 * Called when some data arrives on the forwarding socket
365 u_int pkt[MAX_PACKET_SIZE / sizeof(u_int) + 1];
369 struct sockaddr_in src_addr;
370 RECVFROM_FROMLEN_TYPE src_addr_len;
371 #ifdef HAVE_TRANSPORT_TYPE_TLI
372 struct t_unitdata ud;
374 #endif /* HAVE_TRANSPORT_TYPE_TLI */
377 * Determine the length of the packet
379 len = MAX_PACKET_SIZE;
382 * Read the packet and check for validity
385 src_addr_len = sizeof(src_addr);
386 #ifdef HAVE_TRANSPORT_TYPE_TLI
387 ud.addr.buf = (char *) &src_addr;
388 ud.addr.maxlen = ud.addr.len = src_addr_len;
389 ud.opt.buf = (char *) NULL;
390 ud.opt.maxlen = ud.opt.len = 0;
391 ud.udata.buf = (char *) pkt;
392 ud.udata.maxlen = ud.udata.len = len;
393 /* XXX: use flags accordingly such as if T_MORE set */
394 rc = t_rcvudata(fwd_sock, &ud, &flags);
395 if (rc == 0) /* success, reset rc to length */
398 plog(XLOG_ERROR,"fwd_reply failed: t_errno=%d, errno=%d, flags=%d",t_errno,errno, flags);
400 #else /* not HAVE_TRANSPORT_TYPE_TLI */
401 rc = recvfrom(fwd_sock,
405 (struct sockaddr *) &src_addr,
407 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
410 * XXX: in svr4, if the T_MORE bit of flags is set, what do
413 if (rc < 0 || src_addr_len != sizeof(src_addr) ||
414 src_addr.sin_family != AF_INET) {
415 if (rc < 0 && errno == EINTR)
417 plog(XLOG_ERROR, "Error reading RPC reply: %m");
422 * Do no more work if finishing soon
424 if ((int) amd_state >= (int) Finishing)
428 * Find packet reference
430 pkt_int = (u_int *) pkt;
433 switch (*pkt_int & RPC_XID_MASK) {
434 case RPC_XID_PORTMAP:
435 dlog("Receiving PORTMAP reply");
438 dlog("Receiving MOUNTD reply %#x", *pkt_int);
440 case RPC_XID_NFSPING:
441 dlog("Receiving NFS ping %#x", *pkt_int);
444 dlog("UNKNOWN RPC XID");
449 p = fwd_locate(*pkt_int);
452 dlog("Can't forward reply id %#x", *pkt_int);
459 * Put the original message id back
462 *pkt_int = p->rf_oldid;
465 * Call forwarding function
467 (*p->rf_fwd) ((voidp) pkt, rc, &src_addr, &p->rf_sin, p->rf_ptr, TRUE);
471 * Free forwarding info