ilb_nat.c revision dbed73cbda2229fd1aa6dc5743993cae7f0a7ee9
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota/*
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * CDDL HEADER START
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * The contents of this file are subject to the terms of the
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * Common Development and Distribution License (the "License").
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * You may not use this file except in compliance with the License.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * or http://www.opensolaris.org/os/licensing.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * See the License for the specific language governing permissions
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * and limitations under the License.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * When distributing Covered Code, include this CDDL HEADER in each
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * If applicable, add the following below this CDDL HEADER, with the
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * fields enclosed by brackets "[]" replaced with your own identifying
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * information: Portions Copyright [yyyy] [name of copyright owner]
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * CDDL HEADER END
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota */
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota/*
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * Use is subject to license terms.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota */
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota#include <stdlib.h>
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota#include <strings.h>
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota#include <unistd.h>
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota#include <stddef.h>
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota#include <sys/types.h>
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota#include <sys/socket.h>
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota#include "libilb.h"
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota#include "libilb_impl.h"
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otaenum which_tbl {
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota show_nat = 1,
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota show_persist
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota};
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota/* The common function to show kernel info. */
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otastatic ilb_status_t ilb_show_info(ilb_handle_t, char *, size_t *, boolean_t *,
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota enum which_tbl);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota/*
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * To get the ILB NAT table.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * buf: The buffer to return the NAT table entries.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * num: The caller sets it to the number of ilb_nat_info_t entries buf can
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * hold. On return, it contains the actual number of entries put in buf.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * end: The caller sets it to B_TRUE if it only wants at most num entries to
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * be returned. The transaction to ilbd will be termianted when this
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * call returns.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * The caller sets it to B_FALSE if it intends to get the whole table.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * If the whole table has more than num entries, the caller can call
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * this function again to retrieve the rest of the table.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * On return, end is set to B_TRUE if end of table is reached; B_FALSE
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * if there are still remaining entries.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota */
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otailb_status_t
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otailb_show_nat(ilb_handle_t h, ilb_nat_info_t buf[], size_t *num,
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota boolean_t *end)
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota{
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota return (ilb_show_info(h, (char *)buf, num, end, show_nat));
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota}
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota/*
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * To get the ILB persistent entry table.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * buf: The buffer to return the persistent table entries.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * num: The caller sets it to the number of ilb_persist_info_t entries buf can
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * hold. On return, it contains the actual number of entries put in buf.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * end: The caller sets it to B_TRUE if it only wants at most num entries to
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * be returned. The transaction to ilbd will be termianted when this
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * call returns.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * The caller sets it to B_FALSE if it intends to get the whole table.
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota * If the whole table has more than num entries, the caller can call
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota * this function again to retrieve the rest of the table.
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota * On return, end is set to B_TRUE if end of table is reached; B_FALSE
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota * if there are still remaining entries.
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota */
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Otailb_status_t
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Otailb_show_persist(ilb_handle_t h, ilb_persist_info_t buf[], size_t *num,
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota boolean_t *end)
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota{
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota return (ilb_show_info(h, (char *)buf, num, end, show_persist));
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota}
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota/*
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * The function doing the work... The tbl parameter determines whith table
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * to show.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota */
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otastatic ilb_status_t
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otailb_show_info(ilb_handle_t h, char *buf, size_t *num, boolean_t *end,
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota enum which_tbl tbl)
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota{
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota ilb_comm_t *req, *rbuf;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota ilb_show_info_t *req_si, *tmp_si;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota size_t reqsz, rbufsz, tmp_rbufsz, cur_num;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota size_t entry_sz;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota ilb_status_t rc;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota if (*num == 0)
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota return (ILB_STATUS_EINVAL);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota reqsz = sizeof (ilb_comm_t) + sizeof (ilb_show_info_t);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota if ((req = malloc(reqsz)) == NULL)
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota return (ILB_STATUS_ENOMEM);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota req_si = (ilb_show_info_t *)&req->ic_data;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota /*
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * Need to allocate a receive buffer and then copy the buffer
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * content to the passed in buf. The reason is that the
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota * communication to ilbd is message based and the protocol
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * includes a header in the reply. We need to remove this header
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * from the message, hence the copying...
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota */
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota if (tbl == show_nat)
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota entry_sz = sizeof (ilb_nat_info_t);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota else
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota entry_sz = sizeof (ilb_persist_info_t);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota rbufsz = *num * entry_sz + sizeof (ilb_comm_t) +
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota sizeof (ilb_show_info_t);
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota if ((rbuf = malloc(rbufsz)) == NULL) {
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota free(req);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota return (ILB_STATUS_ENOMEM);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota }
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota if (tbl == show_nat)
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota req->ic_cmd = ILBD_SHOW_NAT;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota else
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota req->ic_cmd = ILBD_SHOW_PERSIST;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota req->ic_flags = 0;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota req_si->sn_num = *num;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota cur_num = 0;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota do {
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota tmp_rbufsz = rbufsz;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota rc = i_ilb_do_comm(h, req, reqsz, rbuf, &tmp_rbufsz);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota if (rc != ILB_STATUS_OK)
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota goto out;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota if (rbuf->ic_cmd != ILBD_CMD_OK) {
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota rc = *(ilb_status_t *)&rbuf->ic_data;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota goto out;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota }
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota tmp_si = (ilb_show_info_t *)&rbuf->ic_data;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota cur_num += tmp_si->sn_num;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota bcopy(&tmp_si->sn_data, buf, tmp_si->sn_num * entry_sz);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota buf += tmp_si->sn_num * entry_sz;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
/*
* Buffer is filled, regardless of this is the end of table or
* not, we need to stop.
*/
if (cur_num == *num)
break;
/* Try to fill in the rest. */
req_si->sn_num = *num - cur_num;
} while (!(rbuf->ic_flags & ILB_COMM_END));
*num = cur_num;
/* End of transaction, let the caller know. */
if (rbuf->ic_flags & ILB_COMM_END) {
*end = B_TRUE;
} else {
/* The user wants to terminate the transaction */
if (*end) {
req->ic_flags = ILB_COMM_END;
tmp_rbufsz = rbufsz;
rc = i_ilb_do_comm(h, req, reqsz, rbuf, &tmp_rbufsz);
}
}
out:
free(req);
free(rbuf);
return (rc);
}