2 * Copyright (c) 1997-2014 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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * File: am-utils/amd/rpc_fwd.c
41 * RPC packet forwarding
46 #endif /* HAVE_CONFIG_H */
51 * Note that the ID field in the external packet is only
52 * ever treated as a 32 bit opaque data object, so there
53 * is no need to convert to and from network byte ordering.
56 #define XID_ALLOC() (xid++)
57 #define MAX_PACKET_SIZE 8192 /* Maximum UDP packet size */
60 * Each pending reply has an rpc_forward structure
61 * associated with it. These have a 15 second lifespan.
62 * If a new structure is required, then an expired
63 * one will be re-allocated if available, otherwise a fresh
64 * one is allocated. Whenever a reply is received the
65 * structure is discarded.
67 typedef struct rpc_forward rpc_forward;
69 qelem rf_q; /* Linked list */
70 time_t rf_ttl; /* Time to live */
71 u_int rf_xid; /* Packet id */
72 u_int rf_oldid; /* Original packet id */
73 fwd_fun *rf_fwd; /* Forwarding function */
75 struct sockaddr_in rf_sin;
79 * Head of list of pending replies
81 qelem rpc_head = {&rpc_head, &rpc_head};
87 * Allocate a rely structure
92 time_t now = clocktime(NULL);
93 rpc_forward *p = NULL, *p2;
96 * First search for an existing expired one.
98 ITER(p2, rpc_forward, &rpc_head) {
99 if (p2->rf_ttl <= now) {
106 * If one couldn't be found then allocate
107 * a new structure and link it at the
112 * Call forwarding function to say that
113 * this message was junked.
115 dlog("Re-using packet forwarding slot - id %#x", p->rf_xid);
117 (*p->rf_fwd) (0, 0, 0, &p->rf_sin, p->rf_ptr, FALSE);
120 p = ALLOC(struct rpc_forward);
122 ins_que(&p->rf_q, &rpc_head);
125 * Set the time to live field
126 * Timeout in 43 seconds
128 p->rf_ttl = now + 43;
135 * Free an allocated reply structure.
136 * First unlink it from the list, then
140 fwd_free(rpc_forward *p)
148 * Initialize the RPC forwarder
157 #ifdef HAVE_TRANSPORT_TYPE_TLI
159 * Create ping TLI socket (/dev/tcp and /dev/ticlts did not work)
160 * (HPUX-11 does not like using O_NDELAY in flags)
162 fwd_sock = t_open("/dev/udp", O_RDWR|O_NONBLOCK, 0);
164 plog(XLOG_ERROR, "unable to create RPC forwarding TLI socket: %s",
168 #else /* not HAVE_TRANSPORT_TYPE_TLI */
172 fwd_sock = socket(AF_INET, SOCK_DGRAM, 0);
174 plog(XLOG_ERROR, "unable to create RPC forwarding socket: %m");
177 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
180 * Some things we talk to require a priv port - so make one here
182 if (bind_resv_port(fwd_sock, (u_short *) NULL) < 0)
183 plog(XLOG_ERROR, "can't bind privileged port (rpc_fwd)");
185 if (fcntl(fwd_sock, F_SETFL, FNDELAY) < 0
187 && ioctl(fwd_sock, FIONBIO, &on) < 0
190 plog(XLOG_ERROR, "Can't set non-block on forwarding socket: %m");
199 * Locate a packet in the forwarding list
206 ITER(p, rpc_forward, &rpc_head) {
216 * This is called to forward a packet to another
217 * RPC server. The message id is changed and noted
218 * so that when a reply appears we can tie it up
219 * correctly. Just matching the reply's source address
220 * would not work because it might come from a
224 fwd_packet(int type_id, char *pkt, int len, struct sockaddr_in *fwdto, struct sockaddr_in *replyto, opaque_t cb_arg, fwd_fun cb)
229 #ifdef HAVE_TRANSPORT_TYPE_TLI
230 struct t_unitdata ud;
231 #endif /* HAVE_TRANSPORT_TYPE_TLI */
233 if ((int) amd_state >= (int) Finishing)
237 * See if the type_id is fully specified.
238 * If so, then discard any old entries
240 * Otherwise make sure the type_id is
241 * fully qualified by allocating an id here.
243 switch (type_id & RPC_XID_MASK) {
244 case RPC_XID_PORTMAP:
245 dlog("Sending PORTMAP request %#x", type_id);
248 dlog("Sending MOUNTD request %#x", type_id);
250 case RPC_XID_NFSPING:
251 dlog("Sending NFS ping %#x", type_id);
254 dlog("Sending WebNFS lookup %#x", type_id);
257 dlog("UNKNOWN RPC XID %#x", type_id);
261 if (type_id & ~RPC_XID_MASK) {
262 p = fwd_locate(type_id);
264 dlog("Discarding earlier rpc fwd handle");
268 dlog("Allocating a new xid...");
269 type_id = MK_RPC_XID(type_id, XID_ALLOC());
278 pkt_int = (u_int *) pkt;
281 * Get the original packet id
283 p->rf_oldid = ntohl(*pkt_int);
286 * Replace with newly allocated id
289 *pkt_int = htonl(type_id);
292 * The sendto may fail if, for example, the route
293 * to a remote host is lost because an intermediate
294 * gateway has gone down. Important to fill in the
295 * rest of "p" otherwise nasty things happen later...
301 dlog("Sending packet id %#x to %s:%d",
303 inet_dquad(dq, sizeof(dq), fwdto->sin_addr.s_addr),
304 ntohs(fwdto->sin_port));
308 /* if NULL, remote server probably down */
310 error = AM_ERRNO_HOST_DOWN;
314 #ifdef HAVE_TRANSPORT_TYPE_TLI
315 ud.addr.buf = (char *) fwdto;
316 if (fwdto) /* if NULL, set sizes to zero */
317 ud.addr.maxlen = ud.addr.len = sizeof(struct sockaddr_in);
319 ud.addr.maxlen = ud.addr.len = 0;
320 ud.opt.buf = (char *) NULL;
321 ud.opt.maxlen = ud.opt.len = 0;
323 ud.udata.maxlen = ud.udata.len = len;
324 if (t_sndudata(fwd_sock, &ud) < 0) {
325 plog(XLOG_ERROR,"fwd_packet failed: t_errno=%d, errno=%d",t_errno,errno);
328 #else /* not HAVE_TRANSPORT_TYPE_TLI */
329 if (sendto(fwd_sock, (char *) pkt, len, 0,
330 (struct sockaddr *) fwdto, sizeof(*fwdto)) < 0)
332 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
335 * Save callback function and return address
340 p->rf_sin = *replyto;
342 memset((voidp) &p->rf_sin, 0, sizeof(p->rf_sin));
350 * Called when some data arrives on the forwarding socket
356 u_int pkt[MAX_PACKET_SIZE / sizeof(u_int) + 1];
361 struct sockaddr_in src_addr;
362 RECVFROM_FROMLEN_TYPE src_addr_len;
363 #ifdef HAVE_TRANSPORT_TYPE_TLI
364 struct t_unitdata ud;
366 #endif /* HAVE_TRANSPORT_TYPE_TLI */
369 * Determine the length of the packet
371 len = MAX_PACKET_SIZE;
374 * Read the packet and check for validity
377 src_addr_len = sizeof(src_addr);
378 #ifdef HAVE_TRANSPORT_TYPE_TLI
379 ud.addr.buf = (char *) &src_addr;
380 ud.addr.maxlen = ud.addr.len = src_addr_len;
381 ud.opt.buf = (char *) NULL;
382 ud.opt.maxlen = ud.opt.len = 0;
383 ud.udata.buf = (char *) pkt;
384 ud.udata.maxlen = ud.udata.len = len;
385 /* XXX: use flags accordingly such as if T_MORE set */
386 rc = t_rcvudata(fwd_sock, &ud, &flags);
387 if (rc == 0) /* success, reset rc to length */
390 plog(XLOG_ERROR,"fwd_reply failed: t_errno=%d, errno=%d, flags=%d",t_errno,errno, flags);
392 * Clear error indication, otherwise the error condition persists and
393 * amd gets into an infinite loop.
395 if (t_errno == TLOOK)
396 t_rcvuderr(fwd_sock, NULL);
398 #else /* not HAVE_TRANSPORT_TYPE_TLI */
399 rc = recvfrom(fwd_sock,
403 (struct sockaddr *) &src_addr,
405 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
408 * XXX: in svr4, if the T_MORE bit of flags is set, what do
411 if (rc < 0 || src_addr_len != sizeof(src_addr) ||
412 src_addr.sin_family != AF_INET) {
413 if (rc < 0 && errno == EINTR)
415 plog(XLOG_ERROR, "Error reading RPC reply: %m");
420 * Do no more work if finishing soon
422 if ((int) amd_state >= (int) Finishing)
426 * Find packet reference
428 pkt_int = (u_int *) pkt;
429 pkt_xid = ntohl(*pkt_int);
431 switch (pkt_xid & RPC_XID_MASK) {
432 case RPC_XID_PORTMAP:
433 dlog("Receiving PORTMAP reply %#x", pkt_xid);
436 dlog("Receiving MOUNTD reply %#x", pkt_xid);
438 case RPC_XID_NFSPING:
439 dlog("Receiving NFS ping %#x", pkt_xid);
442 dlog("Receiving WebNFS lookup %#x", pkt_xid);
445 dlog("UNKNOWN RPC XID %#x", pkt_xid);
449 p = fwd_locate(pkt_xid);
451 dlog("Can't forward reply id %#x", pkt_xid);
457 * Put the original message id back
460 *pkt_int = htonl(p->rf_oldid);
463 * Call forwarding function
465 (*p->rf_fwd) ((voidp) pkt, rc, &src_addr, &p->rf_sin, p->rf_ptr, TRUE);
469 * Free forwarding info