#include <sys/types.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <libintl.h>
#include <unistd.h>
#include <sys/sysmacros.h>
#include <netconfig.h>
#include <errno.h>
#include <sys/sockio.h>
#include "inetd_impl.h"
uu_list_pool_t *conn_ind_pool = NULL;
boolean_t
rpc_info_equal(const rpc_info_t *ri, const rpc_info_t *ri2)
{
return ((ri->prognum == ri2->prognum) &&
(ri->lowver == ri2->lowver) &&
(ri->highver == ri2->highver) &&
(strcmp(ri->netid, ri2->netid) == 0));
}
static int
can_use_af(sa_family_t af)
{
struct lifnum lifn;
int fd;
if ((fd = open("/dev/udp", O_RDONLY)) < 0) {
return (0);
}
lifn.lifn_family = af;
lifn.lifn_flags = IFF_UP & !(IFF_NOXMIT | IFF_DEPRECATED);
if (ioctl(fd, SIOCGLIFNUM, &lifn, sizeof (lifn)) < 0) {
lifn.lifn_count = 0;
}
(void) close(fd);
return (lifn.lifn_count);
}
static boolean_t
is_v6_netid(const char *netid)
{
return ((strcmp(netid, SOCKET_PROTO_TCP6) == 0) ||
(strcmp(netid, SOCKET_PROTO_UDP6) == 0));
}
int
register_rpc_service(const char *fmri, const rpc_info_t *rpc)
{
struct netconfig *nconf;
int ver;
if ((nconf = getnetconfigent(rpc->netid)) == NULL) {
if (is_v6_netid(rpc->netid) && !can_use_af(AF_INET6)) {
warn_msg(gettext(
"Couldn't register netid %s for RPC instance %s "
"because no IPv6 interfaces are plumbed"),
rpc->netid, fmri);
} else {
error_msg(gettext(
"Failed to lookup netid '%s' for instance %s: %s"),
rpc->netid, fmri, nc_sperror());
}
return (-1);
}
for (ver = rpc->lowver; ver <= rpc->highver; ver++) {
if (!rpcb_set(rpc->prognum, ver, nconf, &(rpc->netbuf))) {
error_msg(gettext("Failed to register version %d "
"of RPC service instance %s, netid %s"), ver,
fmri, rpc->netid);
for (ver--; ver >= rpc->lowver; ver--)
(void) rpcb_unset(rpc->prognum, ver, nconf);
freenetconfigent(nconf);
return (-1);
}
}
freenetconfigent(nconf);
return (0);
}
void
unregister_rpc_service(const char *fmri, const rpc_info_t *rpc)
{
int ver;
struct netconfig *nconf;
if ((nconf = getnetconfigent(rpc->netid)) == NULL) {
if (!(is_v6_netid(rpc->netid) && !can_use_af(AF_INET6))) {
error_msg(gettext(
"Failed to lookup netid '%s' for instance %s: %s"),
rpc->netid, fmri, nc_sperror());
}
return;
}
for (ver = rpc->lowver; ver <= rpc->highver; ver++)
(void) rpcb_unset(rpc->prognum, ver, nconf);
freenetconfigent(nconf);
}
int
tlx_init(void)
{
if ((conn_ind_pool = uu_list_pool_create("conn_ind_pool",
sizeof (tlx_conn_ind_t), offsetof(tlx_conn_ind_t, link),
NULL, UU_LIST_POOL_DEBUG)) == NULL) {
error_msg("%s: %s", gettext("Failed to create uu pool"),
uu_strerror(uu_error()));
return (-1);
}
return (0);
}
void
tlx_fini(void)
{
if (conn_ind_pool != NULL) {
uu_list_pool_destroy(conn_ind_pool);
conn_ind_pool = NULL;
}
}
boolean_t
tlx_info_equal(const tlx_info_t *ti, const tlx_info_t *ti2, boolean_t isrpc)
{
return ((isrpc || (memcmp(ti->local_addr.buf, ti2->local_addr.buf,
sizeof (struct sockaddr_storage)) == 0)) &&
(strcmp(ti->dev_name, ti2->dev_name) == 0));
}
static int
tlx_bind(int fd, const struct netbuf *reqaddr, struct netbuf *retaddr, int qlen)
{
struct t_bind breq;
struct t_bind bret;
if (retaddr != NULL) {
bret.addr.buf = retaddr->buf;
bret.addr.maxlen = retaddr->maxlen;
}
if (reqaddr != NULL) {
breq.addr.buf = reqaddr->buf;
breq.addr.len = reqaddr->len;
} else {
breq.addr.len = 0;
}
breq.qlen = qlen;
if (t_bind(fd, &breq, retaddr != NULL ? &bret : NULL) < 0)
return (-1);
if (retaddr != NULL)
retaddr->len = bret.addr.len;
return (0);
}
static int
tlx_setsockopt(int fd, int level, int optname, const void *optval,
socklen_t optlen)
{
struct t_optmgmt request, reply;
struct {
struct opthdr sockopt;
char data[256];
} optbuf;
if (optlen > sizeof (optbuf.data)) {
error_msg(gettext("t_optmgmt request too long"));
return (-1);
}
optbuf.sockopt.level = level;
optbuf.sockopt.name = optname;
optbuf.sockopt.len = optlen;
(void) memcpy(optbuf.data, optval, optlen);
request.opt.len = sizeof (struct opthdr) + optlen;
request.opt.buf = (char *)&optbuf;
request.flags = T_NEGOTIATE;
reply.opt.maxlen = sizeof (struct opthdr) + optlen;
reply.opt.buf = (char *)&optbuf;
reply.flags = 0;
if ((t_optmgmt(fd, &request, &reply) == -1) ||
(reply.flags != T_SUCCESS)) {
error_msg("t_optmgmt: %s", t_strerror(t_errno));
return (-1);
}
return (0);
}
static boolean_t
netbufs_equal(struct netbuf *n1, struct netbuf *n2)
{
return ((n1->len == n2->len) &&
(memcmp(n1->buf, n2->buf, (size_t)n1->len) == 0));
}
int
create_bound_endpoint(const instance_t *inst, tlx_info_t *tlx_info)
{
int fd;
int qlen;
const char *fmri = inst->fmri;
struct netbuf *reqaddr;
struct netbuf *retaddr;
struct netbuf netbuf;
struct sockaddr_storage ss;
rpc_info_t *rpc = tlx_info->pr_info.ri;
if ((fd = t_open(tlx_info->dev_name, O_RDWR, NULL)) == -1) {
error_msg(gettext("Failed to open transport %s for "
"instance %s, proto %s: %s"), tlx_info->dev_name,
fmri, tlx_info->pr_info.proto, t_strerror(t_errno));
return (-1);
}
if (tlx_info->pr_info.v6only) {
int on = 1;
if (tlx_setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on,
sizeof (on)) == -1) {
(void) t_close(fd);
return (-1);
}
}
if ((rpc != NULL) && (rpc->is_loopback))
svc_fd_negotiate_ucred(fd);
if (rpc != NULL) {
reqaddr = NULL;
retaddr = &(rpc->netbuf);
} else {
reqaddr = &(tlx_info->local_addr);
netbuf.buf = (char *)&ss;
netbuf.maxlen = sizeof (ss);
retaddr = &netbuf;
}
qlen = inst->config->basic->conn_backlog;
if ((tlx_bind(fd, reqaddr, retaddr, qlen) == -1) ||
((reqaddr != NULL) && !netbufs_equal(reqaddr, retaddr))) {
error_msg(gettext("Failed to bind to the requested address "
"for instance %s, proto %s"), fmri,
tlx_info->pr_info.proto);
(void) t_close(fd);
return (-1);
}
return (fd);
}
static struct t_call *
get_new_conind(int fd)
{
struct t_call *call;
if ((call = (struct t_call *)t_alloc(fd, T_CALL, T_ALL)) == NULL) {
error_msg("t_alloc: %s", t_strerror(t_errno));
return (NULL);
}
if (t_listen(fd, call) < 0) {
error_msg("t_listen: %s", t_strerror(t_errno));
(void) t_free((char *)call, T_CALL);
return (NULL);
}
return (call);
}
int
queue_conind(uu_list_t *queue, struct t_call *call)
{
tlx_conn_ind_t *ci;
if ((ci = malloc(sizeof (tlx_conn_ind_t))) == NULL) {
error_msg(strerror(errno));
return (-1);
}
ci->call = call;
uu_list_node_init(ci, &ci->link, conn_ind_pool);
(void) uu_list_insert_after(queue, NULL, ci);
return (0);
}
struct t_call *
dequeue_conind(uu_list_t *queue)
{
struct t_call *ret;
tlx_conn_ind_t *ci = uu_list_first(queue);
if (ci == NULL)
return (NULL);
ret = ci->call;
uu_list_remove(queue, ci);
free(ci);
return (ret);
}
static int
process_tlook(const char *fmri, tlx_info_t *tlx_info)
{
int event;
int fd = tlx_info->pr_info.listen_fd;
switch (event = t_look(fd)) {
case T_LISTEN: {
struct t_call *call;
debug_msg("process_tlook: T_LISTEN event");
if ((call = get_new_conind(fd)) == NULL)
return (-1);
if (queue_conind(tlx_info->conn_ind_queue, call) == -1) {
error_msg(gettext("Failed to queue connection "
"indication for instance %s"), fmri);
(void) t_free((char *)call, T_CALL);
return (-1);
}
break;
}
case T_DISCONNECT: {
tlx_conn_ind_t *cip;
struct t_discon *discon;
debug_msg("process_tlook: T_DISCONNECT event");
if ((discon = (struct t_discon *)
t_alloc(fd, T_DIS, T_ALL)) == NULL) {
error_msg("t_alloc: %s", t_strerror(t_errno));
return (-1);
}
if (t_rcvdis(fd, discon) < 0) {
error_msg("t_rcvdis: %s", t_strerror(t_errno));
(void) t_free((char *)discon, T_DIS);
return (-1);
}
cip = uu_list_first(tlx_info->conn_ind_queue);
while ((cip != NULL) &&
(cip->call->sequence != discon->sequence)) {
cip = uu_list_next(tlx_info->conn_ind_queue, cip);
}
if (cip != NULL) {
uu_list_remove(tlx_info->conn_ind_queue, cip);
(void) t_free((char *)cip->call, T_CALL);
free(cip);
}
(void) t_free((char *)discon, T_DIS);
break;
}
case -1:
error_msg("t_look: %s", t_strerror(t_errno));
return (-1);
default:
error_msg(gettext("do_tlook: unexpected t_look event: %d"),
event);
return (-1);
}
return (0);
}
int
tlx_accept(const char *fmri, tlx_info_t *tlx_info,
struct sockaddr_storage *remote_addr)
{
tlx_conn_ind_t *conind;
struct t_call *call;
int fd;
int listen_fd = tlx_info->pr_info.listen_fd;
if ((fd = t_open(tlx_info->dev_name, O_RDWR, NULL)) == -1) {
error_msg("t_open: %s", t_strerror(t_errno));
return (-1);
}
if (tlx_info->pr_info.v6only) {
int on = 1;
if (tlx_setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on,
sizeof (on)) == -1) {
(void) t_close(fd);
return (-1);
}
}
if (t_bind(fd, NULL, NULL) == -1) {
error_msg("t_bind: %s", t_strerror(t_errno));
(void) t_close(fd);
return (-1);
}
if ((conind = uu_list_first(tlx_info->conn_ind_queue)) != NULL) {
debug_msg("taking con off queue");
call = conind->call;
} else if ((call = get_new_conind(listen_fd)) == NULL) {
(void) t_close(fd);
return (-1);
}
if (t_accept(listen_fd, fd, call) == -1) {
if (t_errno == TLOOK) {
if (uu_list_first(tlx_info->conn_ind_queue) == NULL) {
if (queue_conind(tlx_info->conn_ind_queue,
call) == -1) {
error_msg(gettext(
"Failed to queue connection "
"indication for instance %s"),
fmri);
(void) t_free((char *)call, T_CALL);
return (-1);
}
}
(void) process_tlook(fmri, tlx_info);
} else {
error_msg("%s: %s", "t_accept failed",
t_strerror(t_errno));
if (uu_list_first(tlx_info->conn_ind_queue) != NULL)
(void) dequeue_conind(tlx_info->conn_ind_queue);
(void) t_free((char *)call, T_CALL);
}
(void) t_close(fd);
return (-1);
}
(void) memcpy(remote_addr, call->addr.buf,
MIN(call->addr.len, sizeof (*remote_addr)));
if (uu_list_first(tlx_info->conn_ind_queue) != NULL)
(void) dequeue_conind(tlx_info->conn_ind_queue);
(void) t_free((char *)call, T_CALL);
return (fd);
}
void
close_net_fd(instance_t *inst, int fd)
{
if (inst->config->basic->istlx) {
(void) t_close(fd);
} else {
(void) close(fd);
}
}
void
consume_wait_data(instance_t *inst, int fd)
{
int flag;
char buf[50];
if (inst->config->basic->istlx) {
(void) t_rcv(fd, buf, sizeof (buf), &flag);
} else {
(void) recv(fd, buf, sizeof (buf), 0);
}
}