Esempio n. 1
0
int nl_dect_cell_build_msg(struct nl_msg *msg, struct nl_dect_cell *cell)
{
	struct dectmsg dm = {
		.dm_index	= cell->c_index,
	};

	if (nlmsg_append(msg, &dm, sizeof(dm), NLMSG_ALIGNTO) < 0)
		goto nla_put_failure;
	if (cell->ce_mask & CELL_ATTR_NAME)
		NLA_PUT_STRING(msg, DECTA_CELL_NAME, cell->c_name);
	if (cell->ce_mask & CELL_ATTR_FLAGS)
		NLA_PUT_U32(msg, DECTA_CELL_FLAGS, cell->c_flags);
	if (cell->ce_mask & CELL_ATTR_LINK)
		NLA_PUT_U8(msg, DECTA_CELL_CLUSTER, cell->c_link);
	return 0;

nla_put_failure:
	return -NLE_MSGSIZE;
}

static struct trans_tbl cell_flags[] = {
	__ADD(DECT_CELL_CCP,		ccp)
	__ADD(DECT_CELL_SLAVE,		slave)
	__ADD(DECT_CELL_MONITOR,	monitor)
};

char *nl_dect_cell_flags2str(uint32_t flags, char *buf, size_t len)
{
	return __flags2str(flags, buf, len, cell_flags, ARRAY_SIZE(cell_flags));
}

uint32_t nl_dect_cell_str2flags(const char *str)
{
	return __str2flags(str, cell_flags, ARRAY_SIZE(cell_flags));
}

/** @cond SKIP */
struct nl_object_ops nl_dect_cell_obj_ops = {
	.oo_name	= "nl_dect/cell",
	.oo_size	= sizeof(struct nl_dect_cell),
	.oo_free_data	= cell_free_data,
	.oo_dump	= {
		[NL_DUMP_LINE]	= cell_dump,
	},
	.oo_id_attrs	= CELL_ATTR_NAME,
Esempio n. 2
0
File: nl.c Progetto: ebichu/dd-wrt
/**
 * Send a netlink message.
 * @arg handle		netlink handle
 * @arg nmsg		netlink message
 * @return see sendmsg(2)
 */
int nl_send(struct nl_handle *handle, struct nlmsghdr *nmsg)
{
	struct nl_cb *cb;

	struct iovec iov = {
		.iov_base = (void *) nmsg,
		.iov_len = nmsg->nlmsg_len,
	};

	struct msghdr msg = {
		.msg_name = (void *) &handle->h_peer,
		.msg_namelen = sizeof(struct sockaddr_nl),
		.msg_iov = &iov,
		.msg_iovlen = 1,
	};

	cb = &handle->h_cb;
	if (cb->cb_msg_out)
		if (cb->cb_msg_out(nmsg, cb->cb_msg_out_arg) != NL_PROCEED)
			return 0;

	return sendmsg(handle->h_fd, &msg, 0);
}

/**
 * Send a netlink message and check & extend needed header values
 * @arg handle		netlink handle
 * @arg nmsg		netlink message
 *
 * Checks the netlink message \c nmsg for completness and extends it
 * as required before sending it out. Checked fields include pid,
 * sequence nr, and flags.
 *
 * @return see sendmsg(2)
 */
int nl_send_auto_complete(struct nl_handle *handle, struct nlmsghdr *nmsg)
{
	if (nmsg->nlmsg_pid == 0)
		nmsg->nlmsg_pid = handle->h_local.nl_pid;

	if (nmsg->nlmsg_seq == 0)
		nmsg->nlmsg_seq = handle->h_seq_next++;
	
	nmsg->nlmsg_flags |= (NLM_F_REQUEST | NLM_F_ACK);

	if (handle->h_cb.cb_send_ow)
		return handle->h_cb.cb_send_ow(handle, nmsg);
	else
		return nl_send(handle, nmsg);
}

/**
 * Send a netlink request message
 * @arg handle		netlink handle
 * @arg type		message type
 * @arg flags		message flags
 *
 * Fills out a netlink request message and sends it out using
 * nl_send_auto_complete()
 *
 * @return See sendmsg(2)
 */
int nl_request(struct nl_handle *handle, int type, int flags)
{
	struct nlmsghdr n = {
		.nlmsg_len = NLMSG_LENGTH(0),
		.nlmsg_type = type,
		.nlmsg_flags = flags,
	};

	return nl_send_auto_complete(handle, &n);
}

/**
 * Send a netlink request message with data.
 * @arg handle		netlink handle
 * @arg type		message type
 * @arg flags		message flags
 * @arg buf		data buffer
 * @arg len		length of data
 *
 * Fills out a netlink request message, appends the data to the tail
 * and sends it out using nl_send_auto_complete().
 *
 * @return See sendmsg(2)
 */
int nl_request_with_data(struct nl_handle *handle, int type, int flags,
			 unsigned char *buf, size_t len)
{
	int err = 0;
	struct nl_msg *m;
	struct nlmsghdr n = {
		.nlmsg_len = NLMSG_LENGTH(0),
		.nlmsg_type = type,
		.nlmsg_flags = flags,
	};

	m = nl_msg_build(&n);
	nl_msg_append_raw(m, buf, len);

	err = nl_send_auto_complete(handle, m->nmsg);
	nl_msg_free(m);
	return err;
}

/** @} */

/**
 * @name Receive
 * @{
 */

/**
 * Receive a netlink message from netlink socket.
 * @arg handle		netlink handle
 * @arg nla		target pointer for peer's netlink address
 * @arg buf		target pointer for message content.
 *
 * Receives a netlink message, allocates a buffer in \c *buf and
 * stores the message content. The peer's netlink address is stored
 * in \c *nla. The caller is responsible for freeing the buffer allocated
 * in \c *buf if a positive value is returned.  Interruped system calls
 * are handled by repeating the read. The input buffer size is determined
 * by peeking before the actual read is done.
 *
 * A non-blocking sockets causes the function to return immediately if
 * no data is available.
 *
 * @return Number of octets read, 0 on EOF or a negative error code.
 */
int nl_recv(struct nl_handle *handle, struct sockaddr_nl *nla, unsigned char **buf)
{
	int n;
	int flags = MSG_PEEK;

	struct iovec iov = {
		.iov_len = 4096,
	};

	struct msghdr msg = {
		.msg_name = (void *) nla,
		.msg_namelen = sizeof(sizeof(struct sockaddr_nl)),
		.msg_iov = &iov,
		.msg_iovlen = 1,
		.msg_control = NULL,
		.msg_controllen = 0,
		.msg_flags = 0,
	};

	iov.iov_base = *buf = calloc(1, iov.iov_len);

retry:

	if ((n = recvmsg(handle->h_fd, &msg, flags)) <= 0) {
		if (!n)
			goto abort;
		else if (n < 0) {
			if (errno == EINTR)
				goto retry;
			else if (errno == EAGAIN)
				goto abort;
			else {
				free(*buf);
				return nl_error(errno, "recvmsg failed");
			}
		}
	}
	
	if (iov.iov_len < n) {
		/* Provided buffer is not long enough, enlarge it
		 * and try again. */
		iov.iov_len *= 2;
		iov.iov_base = *buf = realloc(*buf, iov.iov_len);
		goto retry;
	} else if (flags != 0) {
		/* Buffer is big enough, do the actual reading */
		flags = 0;
		goto retry;
	}

	if (msg.msg_namelen != sizeof(struct sockaddr_nl)) {
		free(*buf);
		return nl_error(EADDRNOTAVAIL, "socket address size mismatch");
	}

	return n;

abort:
	free(*buf);
	return 0;
}


/**
 * Receive a set of messages from a netlink socket.
 * @arg handle		netlink handle
 * @arg cb		set of callbacks to control the behaviour.
 *
 * Repeatedly calls nl_recv() and parses the messages as netlink
 * messages. Stops reading if one of the callbacks returns
 * NL_EXIT or nl_recv returns either 0 or a negative error code.
 *
 * A non-blocking sockets causes the function to return immediately if
 * no data is available.
 *
 * @return 0 on success or a negative error code from nl_recv().
 * @see \ref Handlers
 */
int nl_recvmsgs(struct nl_handle *handle, struct nl_cb *cb)
{
	int n, err = 0;
	unsigned char *buf = NULL;
	struct nlmsghdr *hdr;
	struct sockaddr_nl nla = {0};

continue_reading:
	if (cb->cb_recv_ow)
		n = cb->cb_recv_ow(handle, &nla, &buf);
	else
		n = nl_recv(handle, &nla, &buf);

	if (n <= 0)
		return n;

	hdr = (struct nlmsghdr *) buf;
	while (NLMSG_OK(hdr, n)) {

		/* Raw callback is the first, it gives the most control
		 * to the user and he can do his very own parsing. */
		if (cb->cb_msg_in) {
			err = cb->cb_msg_in(&nla, hdr, cb->cb_msg_in_arg);
			if (err == NL_SKIP)
				goto skip;
			else if (err == NL_EXIT || err < 0)
				goto out;
		}

		/* Sequence number checking. The check may be done by
		 * the user, otherwise a very simple check is applied
		 * enforcing strict ordering */
		if (cb->cb_seq_check) {
			err = cb->cb_seq_check(&nla, hdr, cb->cb_seq_check_arg);
			if (err == NL_SKIP)
				goto skip;
			else if (err == NL_EXIT || err < 0)
				goto out;
		} else if (hdr->nlmsg_seq != handle->h_seq_expect) {
			if (cb->cb_invalid) {
				err = cb->cb_invalid(&nla, hdr,
						     cb->cb_invalid_arg);
				if (err == NL_SKIP)
					goto skip;
				else if (err == NL_EXIT || err < 0)
					goto out;
			} else
				goto out;
		}

		if (hdr->nlmsg_type == NLMSG_DONE ||
		    hdr->nlmsg_type == NLMSG_ERROR ||
		    hdr->nlmsg_type == NLMSG_NOOP ||
		    hdr->nlmsg_type == NLMSG_OVERRUN) {
			/* We can't check for !NLM_F_MULTI since some netlink
			 * users in the kernel are broken. */
			handle->h_seq_expect++;
		}
	
		/* Other side wishes to see an ack for this message */
		if (hdr->nlmsg_flags & NLM_F_ACK) {
			if (cb->cb_send_ack) {
				err = cb->cb_send_ack(&nla, hdr,
						      cb->cb_send_ack_arg);
				if (err == NL_SKIP)
					goto skip;
				else if (err == NL_EXIT || err < 0)
					goto out;
			} else {
				/* FIXME: implement */
			}
		}

		/* messages terminates a multpart message, this is
		 * usually the end of a message and therefore we slip
		 * out of the loop by default. the user may overrule
		 * this action by skipping this packet. */
		if (hdr->nlmsg_type == NLMSG_DONE) {
			if (cb->cb_finish) {
				err = cb->cb_finish(&nla, hdr,
						    cb->cb_finish_arg);
				if (err == NL_SKIP)
					goto skip;
				else if (err == NL_EXIT || err < 0)
					goto out;
			}
			goto out;
		}

		/* Message to be ignored, the default action is to
		 * skip this message if no callback is specified. The
		 * user may overrule this action by returning
		 * NL_PROCEED. */
		else if (hdr->nlmsg_type == NLMSG_NOOP) {
			if (cb->cb_skipped) {
				err = cb->cb_skipped(&nla, hdr,
						     cb->cb_skipped_arg);
				if (err == NL_SKIP)
					goto skip;
				else if (err == NL_EXIT || err < 0)
					goto out;
			} else
				goto skip;
		}

		/* Data got lost, report back to user. The default action is to
		 * quit parsing. The user may overrule this action by retuning
		 * NL_SKIP or NL_PROCEED (dangerous) */
		else if (hdr->nlmsg_type == NLMSG_OVERRUN) {
			if (cb->cb_overrun) {
				err = cb->cb_overrun(&nla, hdr,
						     cb->cb_overrun_arg);
				if (err == NL_SKIP)
					goto skip;
				else if (err == NL_EXIT || err < 0)
					goto out;
			} else
				goto out;
		}

		/* Message carries a nlmsgerr */
		else if (hdr->nlmsg_type == NLMSG_ERROR) {
			struct nlmsgerr *e = (struct nlmsgerr*) NLMSG_DATA(hdr);

			if (hdr->nlmsg_len < NLMSG_LENGTH(sizeof(*e))) {
				/* Truncated error message, the default action
				 * is to stop parsing. The user may overrule
				 * this action by returning NL_SKIP or
				 * NL_PROCEED (dangerous) */
				if (cb->cb_invalid) {
					err = cb->cb_invalid(&nla, hdr,
							cb->cb_invalid_arg);
					if (err == NL_SKIP)
						goto skip;
					else if (err == NL_EXIT || err < 0)
						goto out;
				} else
					goto out;
			} else if (e->error) {
				/* Error message reported back from kernel. */
				if (cb->cb_error) {
					err = cb->cb_error(&nla, e,
							   cb->cb_error_arg);
					if (err == NL_SKIP)
						goto skip;
					else if (err == NL_EXIT || err < 0)
						goto out;
				} else
					goto out;
			} else if (cb->cb_ack) {
				/* ACK */
				err = cb->cb_ack(&nla, hdr, cb->cb_ack_arg);
				if (err == NL_SKIP)
					goto skip;
				else if (err == NL_EXIT || err < 0)
					goto out;
			}
		} else {
			/* Valid message (not checking for MULTIPART bit to
			 * get along with broken kernels. NL_SKIP has no
			 * effect on this.  */
			if (cb->cb_valid) {
				err = cb->cb_valid(&nla, hdr, cb->cb_valid_arg);
				if (err == NL_SKIP)
					goto skip;
				else if (err == NL_EXIT || err < 0)
					goto out;
			}
		}
skip:
		hdr = NLMSG_NEXT(hdr, n);
	}
	
	if (buf) {
		free(buf);
		buf = NULL;
	}

	/* Multipart message not yet complete, continue reading */
	goto continue_reading;

out:
	if (buf)
		free(buf);

	return err;
}

/**
 * Receive a set of message from a netlink socket using handlers in nl_handle.
 * @arg handle		netlink handle
 *
 * Calls nl_recvmsgs() with the handlers configured in the netlink handle.
 * 
 * @see \ref Handlers
 */
int nl_recvmsgs_def(struct nl_handle *handle)
{
	if (handle->h_cb.cb_recvmsgs_ow)
		return handle->h_cb.cb_recvmsgs_ow(handle, &handle->h_cb);
	else
		return nl_recvmsgs(handle, &handle->h_cb);
}

static int ack_wait_handler(struct sockaddr_nl *who, struct nlmsghdr *n,
			    void *arg)
{
	return NL_EXIT;
}

/**
 * Wait for ACK.
 * @arg handle		netlink handle
 * @pre The netlink socket must be in blocking state.
 *
 * Waits until an ACK is received for the latest not yet acknoledged
 * netlink message.
 */
int nl_wait_for_ack(struct nl_handle *handle)
{
	struct nl_cb cb;

	memcpy(&cb, &handle->h_cb, sizeof(cb));
	cb.cb_ack = ack_wait_handler;

	return nl_recvmsgs(handle, &cb);
}


/** @} */

/**
 * @name Netlink Family Translations
 * @{
 */

static struct trans_tbl nlfamilies[] = {
	__ADD(NETLINK_ROUTE,route)
	__ADD(NETLINK_SKIP,skip)
	__ADD(NETLINK_USERSOCK,usersock)
	__ADD(NETLINK_FIREWALL,firewall)
	__ADD(NETLINK_TCPDIAG,tcpdiag)
	__ADD(NETLINK_NFLOG,nflog)
	__ADD(NETLINK_XFRM,xfrm)
	__ADD(NETLINK_SELINUX,selinux)
	__ADD(NETLINK_ARPD,arpd)
	__ADD(NETLINK_AUDIT,audit)
	__ADD(NETLINK_ROUTE6,route6)
	__ADD(NETLINK_IP6_FW,ip6_fw)
	__ADD(NETLINK_DNRTMSG,dnrtmsg)
	__ADD(NETLINK_KOBJECT_UEVENT,kobject_uevent)
	__ADD(NETLINK_TAPBASE,tapbase)
};

/**
 * Convert a netlink family to a character string (Reentrant).
 * @arg family		netlink family
 * @arg buf		destination buffer
 * @arg len		buffer length
 *
 * Converts a netlink family to a character string and stores it in
 * the specified destination buffer.
 *
 * @return The destination buffer or the family encoded in hexidecimal
 *         form if no match was found.
 */
char * nl_nlfamily2str_r(int family, char *buf, size_t len)
{
	return __type2str_r(family, buf, len, nlfamilies,
	    ARRAY_SIZE(nlfamilies));
}

/**
 * Convert a netlink family to a character string.
 * @arg family		netlink family
 *
 * Converts a netlink family to a character string and stores it in a
 * static buffer.
 *
 * @return A static buffer or the family encoded in hexidecimal
 *         form if no match was found.
 * @attention This funnction is NOT thread safe.
 */
char * nl_nlfamily2str(int family)
{
	static char buf[32];
	memset(buf, 0, sizeof(buf));
	return __type2str_r(family, buf, sizeof(buf), nlfamilies,
	    ARRAY_SIZE(nlfamilies));
}

/**
 * Convert a character string to a netlink family
 * @arg name		name of netlink family
 *
 * Converts the provided character string specifying a netlink
 * family to the corresponding numeric value.
 *
 * @return Netlink family negative value if none was found.
 */
int nl_str2nlfamily(const char *name)
{
	return __str2type(name, nlfamilies, ARRAY_SIZE(nlfamilies));
}
Esempio n. 3
0
	if (queue->ce_mask & QUEUE_ATTR_MAXLEN)
		nl_dump(p, "maxlen=%u ", queue->queue_maxlen);

	if (queue->ce_mask & QUEUE_ATTR_COPY_MODE)
		nl_dump(p, "copy_mode=%s ",
			nfnl_queue_copy_mode2str(queue->queue_copy_mode,
						 buf, sizeof(buf)));

	if (queue->ce_mask & QUEUE_ATTR_COPY_RANGE)
		nl_dump(p, "copy_range=%u ", queue->queue_copy_range);

	nl_dump(p, "\n");
}

static const struct trans_tbl copy_modes[] = {
	__ADD(NFNL_QUEUE_COPY_NONE,	none),
	__ADD(NFNL_QUEUE_COPY_META,	meta),
	__ADD(NFNL_QUEUE_COPY_PACKET,	packet),
};

char *nfnl_queue_copy_mode2str(enum nfnl_queue_copy_mode copy_mode, char *buf,
			       size_t len)
{
	return __type2str(copy_mode, buf, len, copy_modes,
			   ARRAY_SIZE(copy_modes));
}

int nfnl_queue_str2copy_mode(const char *name)
{
	return __str2type(name, copy_modes, ARRAY_SIZE(copy_modes));
}
	diff |= RULE_DIFF(TYPE,		a->r_type != b->r_type);
	diff |= RULE_DIFF(PRIO,		a->r_prio != b->r_prio);
	diff |= RULE_DIFF(MARK,		a->r_mark != b->r_mark);
	diff |= RULE_DIFF(SRC_LEN,	a->r_src_len != b->r_src_len);
	diff |= RULE_DIFF(DST_LEN,	a->r_dst_len != b->r_dst_len);
	diff |= RULE_DIFF(SRC,		nl_addr_cmp(a->r_src, b->r_src));
	diff |= RULE_DIFF(DST,		nl_addr_cmp(a->r_dst, b->r_dst));
	diff |= RULE_DIFF(IIF,		strcmp(a->r_iif, b->r_iif));
	
#undef RULE_DIFF

	return diff;
}

static struct trans_tbl rule_attrs[] = {
	__ADD(RULE_ATTR_FAMILY, family)
	__ADD(RULE_ATTR_PRIO, prio)
	__ADD(RULE_ATTR_MARK, mark)
	__ADD(RULE_ATTR_IIF, iif)
	__ADD(RULE_ATTR_REALMS, realms)
	__ADD(RULE_ATTR_SRC, src)
	__ADD(RULE_ATTR_DST, dst)
	__ADD(RULE_ATTR_DSFIELD, dsfield)
	__ADD(RULE_ATTR_TABLE, table)
	__ADD(RULE_ATTR_TYPE, type)
	__ADD(RULE_ATTR_SRC_LEN, src_len)
	__ADD(RULE_ATTR_DST_LEN, dst_len)
	__ADD(RULE_ATTR_SRCMAP, srcmap)
};

static char *rule_attrs2str(int attrs, char *buf, size_t len)
#include <netlink-local.h>
#include <netlink-tc.h>
#include <netlink/netlink.h>
#include <netlink/utils.h>
#include <netlink/route/tc.h>
#include <netlink/route/classifier.h>
#include <netlink/route/classifier-modules.h>
#include <netlink/route/cls/police.h>

/**
 * @name Policer Type
 * @{
 */

static struct trans_tbl police_types[] = {
	__ADD(TC_POLICE_UNSPEC,unspec)
	__ADD(TC_POLICE_OK,ok)
	__ADD(TC_POLICE_RECLASSIFY,reclassify)
	__ADD(TC_POLICE_SHOT,shot)
#ifdef TC_POLICE_PIPE
	__ADD(TC_POLICE_PIPE,pipe)
#endif
};

/**
 * Transform a policer type number into a character string (Reentrant).
 * @arg type		policer type
 * @arg buf		destination buffer
 * @arg len		buffer length
 *
 * Transforms a policer type number into a character string and stores
Esempio n. 6
0
	if (flags & LOOSE_COMPARISON)
		diff |= CT_DIFF(STATUS, (a->ct_status ^ b->ct_status) &
					b->ct_status_mask);
	else
		diff |= CT_DIFF(STATUS, a->ct_status != b->ct_status);

#undef CT_DIFF
#undef CT_DIFF_VAL
#undef CT_DIFF_ADDR

	return diff;
}

static const struct trans_tbl ct_attrs[] = {
	__ADD(CT_ATTR_FAMILY,		family),
	__ADD(CT_ATTR_PROTO,		proto),
	__ADD(CT_ATTR_TCP_STATE,	tcpstate),
	__ADD(CT_ATTR_STATUS,		status),
	__ADD(CT_ATTR_TIMEOUT,		timeout),
	__ADD(CT_ATTR_MARK,		mark),
	__ADD(CT_ATTR_USE,		use),
	__ADD(CT_ATTR_ID,		id),
	__ADD(CT_ATTR_ORIG_SRC,		origsrc),
	__ADD(CT_ATTR_ORIG_DST,		origdst),
	__ADD(CT_ATTR_ORIG_SRC_PORT,	origsrcport),
	__ADD(CT_ATTR_ORIG_DST_PORT,	origdstport),
	__ADD(CT_ATTR_ORIG_ICMP_ID,	origicmpid),
	__ADD(CT_ATTR_ORIG_ICMP_TYPE,	origicmptype),
	__ADD(CT_ATTR_ORIG_ICMP_CODE,	origicmpcode),
	__ADD(CT_ATTR_ORIG_PACKETS,	origpackets),
Esempio n. 7
0
	if ((err = nl_send_auto_complete(handle, nl_msg_get(m))) < 0)
		return err;

	nl_msg_free(m);
	return nl_wait_for_ack(handle);
}

/** @} */

/**
 * @name Neighbour States Translations
 * @{
 */

static struct trans_tbl neigh_states[] = {
	__ADD(NUD_INCOMPLETE, incomplete)
	__ADD(NUD_REACHABLE, reachable)
	__ADD(NUD_STALE, stale)
	__ADD(NUD_DELAY, delay)
	__ADD(NUD_PROBE, probe)
	__ADD(NUD_FAILED, failed)
	__ADD(NUD_NOARP, norarp)
	__ADD(NUD_PERMANENT, permanent)
};

/**
 * Convert neighbour states to a character string.
 * @arg state		neighbour state
 *
 * Converts a neighbour state to a character string separated by
 * commas and stores it in a static buffer.
Esempio n. 8
0
 * lib/netfilter/netfilter.c    Netfilter Generic Functions
 *
 *	This library is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU Lesser General Public
 *	License as published by the Free Software Foundation version 2.1
 *	of the License.
 *
 * Copyright (c) 2008 Patrick McHardy <*****@*****.**>
 */

#include <netlink-local.h>
#include <netlink/netfilter/netfilter.h>
#include <linux/netfilter.h>

static const struct trans_tbl nfnl_verdicts[] = {
    __ADD(NF_DROP,		NF_DROP)
    __ADD(NF_ACCEPT,	NF_ACCEPT)
    __ADD(NF_STOLEN,	NF_STOLEN)
    __ADD(NF_QUEUE,		NF_QUEUE)
    __ADD(NF_REPEAT,	NF_REPEAT)
    __ADD(NF_STOP,		NF_STOP)
};

char *nfnl_verdict2str(unsigned int verdict, char *buf, size_t len)
{
    return __type2str(verdict, buf, len, nfnl_verdicts,
                      ARRAY_SIZE(nfnl_verdicts));
}

unsigned int nfnl_str2verdict(const char *name)
{
Esempio n. 9
0
	v = meta_alloc(TCF_META_TYPE(hdr->right.kind),
		       TCF_META_ID(hdr->right.kind),
		       hdr->right.shift, vdata, vlen);
	if (!v) {
		rtnl_meta_value_put(m->left);
		return -NLE_NOMEM;
	}

	m->right = v;
	m->opnd = hdr->left.op;

	return 0;
}

static const struct trans_tbl meta_int[] = {
	__ADD(TCF_META_ID_RANDOM, random)
	__ADD(TCF_META_ID_LOADAVG_0, loadavg_0)
	__ADD(TCF_META_ID_LOADAVG_1, loadavg_1)
	__ADD(TCF_META_ID_LOADAVG_2, loadavg_2)
	__ADD(TCF_META_ID_DEV, dev)
	__ADD(TCF_META_ID_PRIORITY, prio)
	__ADD(TCF_META_ID_PROTOCOL, proto)
	__ADD(TCF_META_ID_PKTTYPE, pkttype)
	__ADD(TCF_META_ID_PKTLEN, pktlen)
	__ADD(TCF_META_ID_DATALEN, datalen)
	__ADD(TCF_META_ID_MACLEN, maclen)
	__ADD(TCF_META_ID_NFMARK, mark)
	__ADD(TCF_META_ID_TCINDEX, tcindex)
	__ADD(TCF_META_ID_RTCLASSID, rtclassid)
	__ADD(TCF_META_ID_RTIIF, rtiif)
	__ADD(TCF_META_ID_SK_FAMILY, sk_family)
Esempio n. 10
0
		BUG();

	if (rtnl_route_ops.co_dump[type])
		rtnl_route_ops.co_dump[type](NULL, (struct nl_common *) route,
		    fd, params);
}

/** @} */

/**
 * \name Scope Translations
 * @{
 */

static struct trans_tbl scopes[] = {
	__ADD(255,nowhere)
	__ADD(254,host)
	__ADD(253,link)
	__ADD(200,site)
	__ADD(0,global)
};

/**
 * Convert a scope ID to a character string.
 * @arg scope		scope id
 *
 * Converts a scope ID o a character string and stores it in a
 * static buffer.
 *
 * \return A static buffer or the type encoded in hexidecimal
 *         form if no match was found.
Esempio n. 11
0
File: ae.c Progetto: Domikk/libnl
					diff |= 1;
			}
		}
	}
#undef XFRM_AE_DIFF

	return diff;
}

/**
 * @name XFRM AE Attribute Translations
 * @{
 */
static const struct trans_tbl ae_attrs[] =
{
	__ADD(XFRM_AE_ATTR_DADDR, daddr),
	__ADD(XFRM_AE_ATTR_SPI, spi),
	__ADD(XFRM_AE_ATTR_PROTO, protocol),
	__ADD(XFRM_AE_ATTR_SADDR, saddr),
	__ADD(XFRM_AE_ATTR_FLAGS, flags),
	__ADD(XFRM_AE_ATTR_REQID, reqid),
	__ADD(XFRM_AE_ATTR_MARK, mark),
	__ADD(XFRM_AE_ATTR_LIFETIME, cur_lifetime),
	__ADD(XFRM_AE_ATTR_REPLAY_MAXAGE, replay_maxage),
	__ADD(XFRM_AE_ATTR_REPLAY_MAXDIFF, replay_maxdiff),
	__ADD(XFRM_AE_ATTR_REPLAY_STATE, replay_state),
};

static char* xfrm_ae_attrs2str (int attrs, char *buf, size_t len)
{
	return __flags2str(attrs, buf, len, ae_attrs, ARRAY_SIZE(ae_attrs));
Esempio n. 12
0
		diff |= EXP_DIFF_L4PROTO_PORTS(NAT_L4PROTO_PORTS,	exp_nat.proto.l4protodata);
		diff |= EXP_DIFF_L4PROTO_ICMP(NAT_L4PROTO_ICMP,		exp_nat.proto.l4protodata);

#undef EXP_DIFF
#undef EXP_DIFF_VAL
#undef EXP_DIFF_STRING
#undef EXP_DIFF_ADDR
#undef EXP_DIFF_L4PROTO_PORTS
#undef EXP_DIFF_L4PROTO_ICMP

	return diff;
}

// CLI arguments?
static const struct trans_tbl exp_attrs[] = {
	__ADD(EXP_ATTR_FAMILY,				family),
	__ADD(EXP_ATTR_TIMEOUT,				timeout),
	__ADD(EXP_ATTR_ID,				id),
	__ADD(EXP_ATTR_HELPER_NAME,			helpername),
	__ADD(EXP_ATTR_ZONE,				zone),
	__ADD(EXP_ATTR_CLASS,				class),
	__ADD(EXP_ATTR_FLAGS,				flags),
	__ADD(EXP_ATTR_FN,				function),
	__ADD(EXP_ATTR_EXPECT_IP_SRC,			expectipsrc),
	__ADD(EXP_ATTR_EXPECT_IP_DST,			expectipdst),
	__ADD(EXP_ATTR_EXPECT_L4PROTO_NUM,		expectprotonum),
	__ADD(EXP_ATTR_EXPECT_L4PROTO_PORTS,		expectports),
	__ADD(EXP_ATTR_EXPECT_L4PROTO_ICMP,		expecticmp),
	__ADD(EXP_ATTR_MASTER_IP_SRC,			masteripsrc),
	__ADD(EXP_ATTR_MASTER_IP_DST,			masteripdst),
	__ADD(EXP_ATTR_MASTER_L4PROTO_NUM,		masterprotonum),
Esempio n. 13
0
File: vlan.c Progetto: Domikk/libnl
	if (negress == NULL)
		return NULL;

	if (vi->vi_mask & VLAN_HAS_EGRESS_QOS) {
		*negress = vi->vi_negress;
		return vi->vi_egress_qos;
	} else {
		*negress = 0;
		return NULL;
	}
}

/** @} */

static const struct trans_tbl vlan_flags[] = {
	__ADD(VLAN_FLAG_REORDER_HDR, reorder_hdr),
	__ADD(VLAN_FLAG_GVRP, gvrp),
	__ADD(VLAN_FLAG_LOOSE_BINDING, loose_binding),
	__ADD(VLAN_FLAG_MVRP, mvrp),
};

/**
 * @name Flag Translation
 * @{
 */

char *rtnl_link_vlan_flags2str(int flags, char *buf, size_t len)
{
	return __flags2str(flags, buf, len, vlan_flags, ARRAY_SIZE(vlan_flags));
}
Esempio n. 14
0
/*
 * __env_struct_sig --
 *	Compute signature of structures.
 *
 * PUBLIC: u_int32_t __env_struct_sig __P((void));
 */
u_int32_t
__env_struct_sig()
{
	u_short t[__STRUCTURE_COUNT + 5];
	u_int i;

	i = 0;
#define	__ADD(s)	(t[i++] = sizeof(struct s))

#ifdef	HAVE_MUTEX_SUPPORT
	__ADD(__db_mutex_stat);
#endif
	__ADD(__db_lock_stat);
	__ADD(__db_lock_hstat);
	__ADD(__db_lock_pstat);
	__ADD(__db_ilock);
	__ADD(__db_lock_u);
	__ADD(__db_lsn);
	__ADD(__db_log_stat);
	__ADD(__db_mpool_stat);
	__ADD(__db_rep_stat);
	__ADD(__db_repmgr_stat);
	__ADD(__db_seq_stat);
	__ADD(__db_bt_stat);
	__ADD(__db_h_stat);
	__ADD(__db_heap_stat);
	__ADD(__db_qam_stat);
#ifdef	HAVE_MUTEX_SUPPORT
	__ADD(__mutex_state);
#endif
	__ADD(__db_thread_info);
	__ADD(__env_thread_info);
	__ADD(__db_lockregion);
	__ADD(__sh_dbt);
	__ADD(__db_lockobj);
	__ADD(__db_locker);
	__ADD(__db_lockpart);
	__ADD(__db_lock);
	__ADD(__log);
	__ADD(__mpool);
	__ADD(__db_mpool_fstat_int);
	__ADD(__mpoolfile);
	__ADD(__bh);
#ifdef	HAVE_MUTEX_SUPPORT
	__ADD(__db_mutexregion);
#endif
#ifdef	HAVE_MUTEX_SUPPORT
	__ADD(__mutex_history);
#endif
#ifdef	HAVE_MUTEX_SUPPORT
	__ADD(__db_mutex_t);
#endif
	__ADD(__db_reg_env);
	__ADD(__db_region);
	__ADD(__rep);
	__ADD(__db_txn_stat_int);
	__ADD(__db_txnregion);

#ifndef HAVE_MIXED_SIZE_ADDRESSING
	__ADD(__db_dbt);
#ifdef	HAVE_MUTEX_SUPPORT
	__ADD(__db_event_mutex_died_info);
#endif
	__ADD(__db_event_failchk_info);
	__ADD(__db_lockreq);
	__ADD(__db_log_cursor);
	__ADD(__log_rec_spec);
	__ADD(__db_mpoolfile);
	__ADD(__db_mpool_fstat);
	__ADD(__db_txn);
	__ADD(__kids);
	__ADD(__my_cursors);
	__ADD(__femfs);
	__ADD(__db_preplist);
	__ADD(__db_txn_active);
	__ADD(__db_txn_stat);
	__ADD(__db_txn_token);
	__ADD(__db_repmgr_site);
	__ADD(__db_repmgr_conn_err);
	__ADD(__db_seq_record);
	__ADD(__db_sequence);
	__ADD(__db);
	__ADD(__cq_fq);
	__ADD(__cq_aq);
	__ADD(__cq_jq);
	__ADD(__db_stream);
	__ADD(__db_heap_rid);
	__ADD(__dbc);
	__ADD(__key_range);
	__ADD(__db_compact);
	__ADD(__db_env);
	__ADD(__db_distab);
	__ADD(__db_logvrfy_config);
	__ADD(__db_channel);
	__ADD(__db_site);
	__ADD(__fn);
	__ADD(__db_msgbuf);
	__ADD(__pin_list);
	__ADD(__flag_map);
	__ADD(__db_backup_handle);
	__ADD(__env);
	__ADD(__dbc_internal);
	__ADD(__dbpginfo);
	__ADD(__epg);
	__ADD(__cursor);
	__ADD(__btree);
	__ADD(__db_cipher);
	__ADD(__db_foreign_info);
	__ADD(__db_txnhead);
	__ADD(__db_txnlist);
	__ADD(__join_cursor);
	__ADD(__pg_chksum);
	__ADD(__pg_crypto);
	__ADD(__heaphdr);
	__ADD(__heaphdrsplt);
	__ADD(__pglist);
	__ADD(__vrfy_dbinfo);
	__ADD(__vrfy_pageinfo);
	__ADD(__vrfy_childinfo);
	__ADD(__db_globals);
	__ADD(__envq);
	__ADD(__heap);
	__ADD(__heap_cursor);
	__ADD(__db_locktab);
	__ADD(__db_entry);
	__ADD(__fname);
	__ADD(__db_log);
	__ADD(__hdr);
	__ADD(__log_persist);
	__ADD(__db_commit);
	__ADD(__db_filestart);
	__ADD(__log_rec_hdr);
	__ADD(__db_log_verify_info);
	__ADD(__txn_verify_info);
	__ADD(__lv_filereg_info);
	__ADD(__lv_filelife);
	__ADD(__lv_ckp_info);
	__ADD(__lv_timestamp_info);
	__ADD(__lv_txnrange);
	__ADD(__add_recycle_params);
	__ADD(__ckp_verify_params);
	__ADD(__db_mpool);
	__ADD(__db_mpreg);
	__ADD(__db_mpool_hash);
	__ADD(__bh_frozen_p);
	__ADD(__bh_frozen_a);
#ifdef	HAVE_MUTEX_SUPPORT
	__ADD(__db_mutexmgr);
#endif
	__ADD(__fh_t);
	__ADD(__db_partition);
	__ADD(__part_internal);
	__ADD(__qcursor);
	__ADD(__mpfarray);
	__ADD(__qmpf);
	__ADD(__queue);
	__ADD(__qam_filelist);
	__ADD(__db_reg_env_ref);
	__ADD(__db_region_mem_t);
	__ADD(__db_reginfo_t);
	__ADD(__rep_waiter);
	__ADD(__db_rep);
	__ADD(__rep_lease_entry);
	__ADD(__txn_detail);
	__ADD(__db_txnmgr);
	__ADD(__db_commit_info);
	__ADD(__txn_logrec);
#endif

	return (__ham_func5(NULL, t, i * sizeof(t[0])));
}
Esempio n. 15
0
		buf += l;
		len -= l;
	}

	return buf_orig;
}

/** @} */

/**
 * @name Netlink Family Translations
 * @{
 */

static const struct trans_tbl nlfamilies[] = {
	__ADD(NETLINK_ROUTE,route),
	__ADD(NETLINK_USERSOCK,usersock),
	__ADD(NETLINK_FIREWALL,firewall),
	__ADD(NETLINK_INET_DIAG,inetdiag),
	__ADD(NETLINK_NFLOG,nflog),
	__ADD(NETLINK_XFRM,xfrm),
	__ADD(NETLINK_SELINUX,selinux),
	__ADD(NETLINK_ISCSI,iscsi),
	__ADD(NETLINK_AUDIT,audit),
	__ADD(NETLINK_FIB_LOOKUP,fib_lookup),
	__ADD(NETLINK_CONNECTOR,connector),
	__ADD(NETLINK_NETFILTER,netfilter),
	__ADD(NETLINK_IP6_FW,ip6_fw),
	__ADD(NETLINK_DNRTMSG,dnrtmsg),
	__ADD(NETLINK_KOBJECT_UEVENT,kobject_uevent),
	__ADD(NETLINK_GENERIC,generic),
Esempio n. 16
0
	};

	err = nl_send_simple(sk, type, flags, &gmsg, sizeof(gmsg));

	return err >= 0 ? 0 : err;
}

/** @} */

/**
 * @name Routing Type Translations
 * @{
 */

static const struct trans_tbl rtntypes[] = {
	__ADD(RTN_UNSPEC,unspec)
	__ADD(RTN_UNICAST,unicast)
	__ADD(RTN_LOCAL,local)
	__ADD(RTN_BROADCAST,broadcast)
	__ADD(RTN_ANYCAST,anycast)
	__ADD(RTN_MULTICAST,multicast)
	__ADD(RTN_BLACKHOLE,blackhole)
	__ADD(RTN_UNREACHABLE,unreachable)
	__ADD(RTN_PROHIBIT,prohibit)
	__ADD(RTN_THROW,throw)
	__ADD(RTN_NAT,nat)
	__ADD(RTN_XRESOLVE,xresolve)
};

char *nl_rtntype2str(int type, char *buf, size_t size)
{
Esempio n. 17
0
}

uint32_t rtnl_route_nh_get_realms(struct rtnl_nexthop *nh)
{
	return nh->rtnh_realms;
}

/** @} */

/**
 * @name Nexthop Flags Translations
 * @{
 */

static const struct trans_tbl nh_flags[] = {
	__ADD(RTNH_F_DEAD, dead)
	__ADD(RTNH_F_PERVASIVE, pervasive)
	__ADD(RTNH_F_ONLINK, onlink)
};

char *rtnl_route_nh_flags2str(int flags, char *buf, size_t len)
{
	return __flags2str(flags, buf, len, nh_flags, ARRAY_SIZE(nh_flags));
}

int rtnl_route_nh_str2flags(const char *name)
{
	return __str2flags(name, nh_flags, ARRAY_SIZE(nh_flags));
}

/** @} */
Esempio n. 18
0
					   stat);
		}
	}

	return 0;
}

/* These live in include/net/if_inet6.h and should be moved to include/linux */
#define IF_RA_OTHERCONF	0x80
#define IF_RA_MANAGED	0x40
#define IF_RA_RCVD	0x20
#define IF_RS_SENT	0x10
#define IF_READY	0x80000000

static const struct trans_tbl inet6_flags[] = {
	__ADD(IF_RA_OTHERCONF, ra_otherconf)
	__ADD(IF_RA_MANAGED, ra_managed)
	__ADD(IF_RA_RCVD, ra_rcvd)
	__ADD(IF_RS_SENT, rs_sent)
	__ADD(IF_READY, ready)
};

static char *inet6_flags2str(int flags, char *buf, size_t len)
{
	return __flags2str(flags, buf, len, inet6_flags,
			   ARRAY_SIZE(inet6_flags));
}

static const struct trans_tbl inet6_devconf[] = {
	__ADD(DEVCONF_FORWARDING, forwarding)
	__ADD(DEVCONF_HOPLIMIT, hoplimit)
Esempio n. 19
0
File: nl.c Progetto: artisdom/mipv6
/**
 * Send netlink message with control over sendmsg() message header.
 * @arg handle		Netlink handle.
 * @arg msg		Netlink message to be sent.
 * @arg hdr		Sendmsg() message header.
 * @return Number of characters sent on sucess or a negative error code.
 */
int nl_sendmsg(struct nl_handle *handle, struct nl_msg *msg, struct msghdr *hdr)
{
	struct nl_cb *cb;
	int ret;

	struct iovec iov = {
		.iov_base = (void *) nlmsg_hdr(msg),
		.iov_len = nlmsg_hdr(msg)->nlmsg_len,
	};

	hdr->msg_iov = &iov;
	hdr->msg_iovlen = 1;

	nlmsg_set_src(msg, &handle->h_local);

	cb = nl_handle_get_cb(handle);
	if (cb->cb_set[NL_CB_MSG_OUT])
		if (nl_cb_call(cb, NL_CB_MSG_OUT, msg) != NL_PROCEED)
			return 0;

	ret = sendmsg(handle->h_fd, hdr, 0);
	if (ret < 0)
		return nl_errno(errno);

	return ret;
}


/**
 * Send netlink message.
 * @arg handle		Netlink handle
 * @arg msg		Netlink message to be sent.
 * @see nl_sendmsg()
 * @return Number of characters sent on success or a negative error code.
 */
int nl_send(struct nl_handle *handle, struct nl_msg *msg)
{
	struct sockaddr_nl *dst;
	struct ucred *creds;
	
	struct msghdr hdr = {
		.msg_name = (void *) &handle->h_peer,
		.msg_namelen = sizeof(struct sockaddr_nl),
	};

	/* Overwrite destination if specified in the message itself, defaults
	 * to the peer address of the handle.
	 */
	dst = nlmsg_get_dst(msg);
	if (dst->nl_family == AF_NETLINK)
		hdr.msg_name = dst;

	/* Add credentials if present. */
	creds = nlmsg_get_creds(msg);
	if (creds != NULL) {
		char buf[CMSG_SPACE(sizeof(struct ucred))];
		struct cmsghdr *cmsg;

		hdr.msg_control = buf;
		hdr.msg_controllen = sizeof(buf);

		cmsg = CMSG_FIRSTHDR(&hdr);
		cmsg->cmsg_level = SOL_SOCKET;
		cmsg->cmsg_type = SCM_CREDENTIALS;
		cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
		memcpy(CMSG_DATA(cmsg), creds, sizeof(struct ucred));
	}

	return nl_sendmsg(handle, msg, &hdr);
}

/**
 * Send netlink message and check & extend header values as needed.
 * @arg handle		Netlink handle.
 * @arg msg		Netlink message to be sent.
 *
 * Checks the netlink message \c nlh for completness and extends it
 * as required before sending it out. Checked fields include pid,
 * sequence nr, and flags.
 *
 * @see nl_send()
 * @return Number of characters sent or a negative error code.
 */
int nl_send_auto_complete(struct nl_handle *handle, struct nl_msg *msg)
{
	struct nlmsghdr *nlh;

	nlh = nlmsg_hdr(msg);
	if (nlh->nlmsg_pid == 0)
		nlh->nlmsg_pid = handle->h_local.nl_pid;

	if (nlh->nlmsg_seq == 0)
		nlh->nlmsg_seq = handle->h_seq_next++;
	
	nlh->nlmsg_flags |= (NLM_F_REQUEST | NLM_F_ACK);

	if (handle->h_cb->cb_send_ow)
		return handle->h_cb->cb_send_ow(handle, msg);
	else
		return nl_send(handle, msg);
}

/**
 * Send simple netlink message using nl_send_auto_complete()
 * @arg handle		Netlink handle.
 * @arg type		Netlink message type.
 * @arg flags		Netlink message flags.
 * @arg buf		Data buffer.
 * @arg size		Size of data buffer.
 *
 * Builds a netlink message with the specified type and flags and
 * appends the specified data as payload to the message.
 *
 * @see nl_send_auto_complete()
 * @return Number of characters sent on success or a negative error code.
 */
int nl_send_simple(struct nl_handle *handle, int type, int flags, void *buf,
		   size_t size)
{
	int err;
	struct nl_msg *msg;
	struct nlmsghdr nlh = {
		.nlmsg_len = nlmsg_msg_size(0),
		.nlmsg_type = type,
		.nlmsg_flags = flags,
	};

	msg = nlmsg_build(&nlh);
	if (!msg)
		return nl_errno(ENOMEM);

	if (buf && size)
		nlmsg_append(msg, buf, size, 1);

	err = nl_send_auto_complete(handle, msg);
	nlmsg_free(msg);

	return err;
}

/** @} */

/**
 * @name Receive
 * @{
 */

/**
 * Receive netlink message from netlink socket.
 * @arg handle		Netlink handle.
 * @arg nla		Destination pointer for peer's netlink address.
 * @arg buf		Destination pointer for message content.
 * @arg creds		Destination pointer for credentials.
 *
 * Receives a netlink message, allocates a buffer in \c *buf and
 * stores the message content. The peer's netlink address is stored
 * in \c *nla. The caller is responsible for freeing the buffer allocated
 * in \c *buf if a positive value is returned.  Interruped system calls
 * are handled by repeating the read. The input buffer size is determined
 * by peeking before the actual read is done.
 *
 * A non-blocking sockets causes the function to return immediately if
 * no data is available.
 *
 * @return Number of octets read, 0 on EOF or a negative error code.
 */
int nl_recv(struct nl_handle *handle, struct sockaddr_nl *nla,
	    unsigned char **buf, struct ucred **creds)
{
	int n;
	int flags = MSG_PEEK;

	struct iovec iov = {
		.iov_len = 4096,
	};

	struct msghdr msg = {
		.msg_name = (void *) nla,
		.msg_namelen = sizeof(sizeof(struct sockaddr_nl)),
		.msg_iov = &iov,
		.msg_iovlen = 1,
		.msg_control = NULL,
		.msg_controllen = 0,
		.msg_flags = 0,
	};
	struct cmsghdr *cmsg;

	iov.iov_base = *buf = calloc(1, iov.iov_len);

	if (handle->h_flags & NL_SOCK_PASSCRED) {
		msg.msg_controllen = CMSG_SPACE(sizeof(struct ucred));
		msg.msg_control = calloc(1, msg.msg_controllen);
	}
retry:

	if ((n = recvmsg(handle->h_fd, &msg, flags)) <= 0) {
		if (!n)
			goto abort;
		else if (n < 0) {
			if (errno == EINTR)
				goto retry;
			else if (errno == EAGAIN)
				goto abort;
			else {
				free(msg.msg_control);
				free(*buf);
				return nl_error(errno, "recvmsg failed");
			}
		}
	}
	
	if (iov.iov_len < n) {
		/* Provided buffer is not long enough, enlarge it
		 * and try again. */
		iov.iov_len *= 2;
		iov.iov_base = *buf = realloc(*buf, iov.iov_len);
		goto retry;
	} else if (msg.msg_flags & MSG_CTRUNC) {
		msg.msg_controllen *= 2;
		msg.msg_control = realloc(msg.msg_control, msg.msg_controllen);
		goto retry;
	} else if (flags != 0) {
		/* Buffer is big enough, do the actual reading */
		flags = 0;
		goto retry;
	}

	if (msg.msg_namelen != sizeof(struct sockaddr_nl)) {
		free(msg.msg_control);
		free(*buf);
		return nl_error(EADDRNOTAVAIL, "socket address size mismatch");
	}

	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
		if (cmsg->cmsg_level == SOL_SOCKET &&
		    cmsg->cmsg_type == SCM_CREDENTIALS) {
			*creds = calloc(1, sizeof(struct ucred));
			memcpy(*creds, CMSG_DATA(cmsg), sizeof(struct ucred));
			break;
		}
	}

	free(msg.msg_control);
	return n;

abort:
	free(msg.msg_control);
	free(*buf);
	return 0;
}


/**
 * Receive a set of messages from a netlink socket.
 * @arg handle		netlink handle
 * @arg cb		set of callbacks to control the behaviour.
 *
 * Repeatedly calls nl_recv() and parses the messages as netlink
 * messages. Stops reading if one of the callbacks returns
 * NL_EXIT or nl_recv returns either 0 or a negative error code.
 *
 * A non-blocking sockets causes the function to return immediately if
 * no data is available.
 *
 * @return 0 on success or a negative error code from nl_recv().
 */
int nl_recvmsgs(struct nl_handle *handle, struct nl_cb *cb)
{
	int n, err = 0;
	unsigned char *buf = NULL;
	struct nlmsghdr *hdr;
	struct sockaddr_nl nla = {0};
	struct nl_msg *msg = NULL;
	struct ucred *creds = NULL;

continue_reading:
	if (cb->cb_recv_ow)
		n = cb->cb_recv_ow(handle, &nla, &buf, &creds);
	else
		n = nl_recv(handle, &nla, &buf, &creds);

	if (n <= 0)
		return n;

	NL_DBG(3, "recvmsgs(%p): Read %d bytes\n", handle, n);

	hdr = (struct nlmsghdr *) buf;
	while (nlmsg_ok(hdr, n)) {
		NL_DBG(3, "recgmsgs(%p): Processing valid message...\n",
		       handle);

		nlmsg_free(msg);
		msg = nlmsg_convert(hdr);
		if (!msg) {
			err = nl_errno(ENOMEM);
			goto out;
		}

		nlmsg_set_proto(msg, handle->h_proto);
		nlmsg_set_src(msg, &nla);
		if (creds)
			nlmsg_set_creds(msg, creds);

		/* Raw callback is the first, it gives the most control
		 * to the user and he can do his very own parsing. */
		if (cb->cb_set[NL_CB_MSG_IN]) {
			err = nl_cb_call(cb, NL_CB_MSG_IN, msg);
			if (err == NL_SKIP)
				goto skip;
			else if (err == NL_EXIT || err < 0)
				goto out;
		}

		/* Sequence number checking. The check may be done by
		 * the user, otherwise a very simple check is applied
		 * enforcing strict ordering */
		if (cb->cb_set[NL_CB_SEQ_CHECK]) {
			err = nl_cb_call(cb, NL_CB_SEQ_CHECK, msg);
			if (err == NL_SKIP)
				goto skip;
			else if (err == NL_EXIT || err < 0)
				goto out;
		} else if (hdr->nlmsg_seq != handle->h_seq_expect) {
			if (cb->cb_set[NL_CB_INVALID]) {
				err = nl_cb_call(cb, NL_CB_INVALID, msg);
				if (err == NL_SKIP)
					goto skip;
				else if (err == NL_EXIT || err < 0)
					goto out;
			} else
				goto out;
		}

		if (hdr->nlmsg_type == NLMSG_DONE ||
		    hdr->nlmsg_type == NLMSG_ERROR ||
		    hdr->nlmsg_type == NLMSG_NOOP ||
		    hdr->nlmsg_type == NLMSG_OVERRUN) {
			/* We can't check for !NLM_F_MULTI since some netlink
			 * users in the kernel are broken. */
			handle->h_seq_expect++;
			NL_DBG(3, "recvmsgs(%p): Increased expected " \
			       "sequence number to %d\n",
			       handle, handle->h_seq_expect);
		}
	
		/* Other side wishes to see an ack for this message */
		if (hdr->nlmsg_flags & NLM_F_ACK) {
			if (cb->cb_set[NL_CB_SEND_ACK]) {
				err = nl_cb_call(cb, NL_CB_SEND_ACK, msg);
				if (err == NL_SKIP)
					goto skip;
				else if (err == NL_EXIT || err < 0)
					goto out;
			} else {
				/* FIXME: implement */
			}
		}

		/* messages terminates a multpart message, this is
		 * usually the end of a message and therefore we slip
		 * out of the loop by default. the user may overrule
		 * this action by skipping this packet. */
		if (hdr->nlmsg_type == NLMSG_DONE) {
			if (cb->cb_set[NL_CB_FINISH]) {
				err = nl_cb_call(cb, NL_CB_FINISH, msg);
				if (err == NL_SKIP)
					goto skip;
				else if (err == NL_EXIT || err < 0)
					goto out;
			}
			err = 0;
			goto out;
		}

		/* Message to be ignored, the default action is to
		 * skip this message if no callback is specified. The
		 * user may overrule this action by returning
		 * NL_PROCEED. */
		else if (hdr->nlmsg_type == NLMSG_NOOP) {
			if (cb->cb_set[NL_CB_SKIPPED]) {
				err = nl_cb_call(cb, NL_CB_SKIPPED, msg);
				if (err == NL_SKIP)
					goto skip;
				else if (err == NL_EXIT || err < 0)
					goto out;
			} else
				goto skip;
		}

		/* Data got lost, report back to user. The default action is to
		 * quit parsing. The user may overrule this action by retuning
		 * NL_SKIP or NL_PROCEED (dangerous) */
		else if (hdr->nlmsg_type == NLMSG_OVERRUN) {
			if (cb->cb_set[NL_CB_OVERRUN]) {
				err = nl_cb_call(cb, NL_CB_OVERRUN, msg);
				if (err == NL_SKIP)
					goto skip;
				else if (err == NL_EXIT || err < 0)
					goto out;
			} else
				goto out;
		}

		/* Message carries a nlmsgerr */
		else if (hdr->nlmsg_type == NLMSG_ERROR) {
			struct nlmsgerr *e = nlmsg_data(hdr);

			if (hdr->nlmsg_len < nlmsg_msg_size(sizeof(*e))) {
				/* Truncated error message, the default action
				 * is to stop parsing. The user may overrule
				 * this action by returning NL_SKIP or
				 * NL_PROCEED (dangerous) */
				if (cb->cb_set[NL_CB_INVALID]) {
					err = nl_cb_call(cb, NL_CB_INVALID,
							 msg);
					if (err == NL_SKIP)
						goto skip;
					else if (err == NL_EXIT || err < 0)
						goto out;
				} else
					goto out;
			} else if (e->error) {
				/* Error message reported back from kernel. */
				if (cb->cb_err) {
					err = cb->cb_err(&nla, e,
							   cb->cb_err_arg);
					if (err == NL_SKIP)
						goto skip;
					else if (err == NL_EXIT || err < 0) {
						nl_error(-e->error,
							 "Netlink Error");
						err = e->error;
						goto out;
					}
				} else {
					nl_error(-e->error, "Netlink Error");
					err = e->error;
					goto out;
				}
			} else if (cb->cb_set[NL_CB_ACK]) {
				/* ACK */
				err = nl_cb_call(cb, NL_CB_ACK, msg);
				if (err == NL_SKIP)
					goto skip;
				else if (err == NL_EXIT || err < 0)
					goto out;
			}
		} else {
			/* Valid message (not checking for MULTIPART bit to
			 * get along with broken kernels. NL_SKIP has no
			 * effect on this.  */
			if (cb->cb_set[NL_CB_VALID]) {
				err = nl_cb_call(cb, NL_CB_VALID, msg);
				if (err == NL_SKIP)
					goto skip;
				else if (err == NL_EXIT || err < 0)
					goto out;
			}
		}
skip:
		hdr = nlmsg_next(hdr, &n);
	}
	
	nlmsg_free(msg);
	free(buf);
	free(creds);
	buf = NULL;
	msg = NULL;
	creds = NULL;

	/* Multipart message not yet complete, continue reading */
	goto continue_reading;

out:
	nlmsg_free(msg);
	free(buf);
	free(creds);

	return err;
}

/**
 * Receive a set of message from a netlink socket using handlers in nl_handle.
 * @arg handle		netlink handle
 *
 * Calls nl_recvmsgs() with the handlers configured in the netlink handle.
 */
int nl_recvmsgs_def(struct nl_handle *handle)
{
	if (handle->h_cb->cb_recvmsgs_ow)
		return handle->h_cb->cb_recvmsgs_ow(handle, handle->h_cb);
	else
		return nl_recvmsgs(handle, handle->h_cb);
}

static int ack_wait_handler(struct nl_msg *msg, void *arg)
{
	return NL_EXIT;
}

/**
 * Wait for ACK.
 * @arg handle		netlink handle
 * @pre The netlink socket must be in blocking state.
 *
 * Waits until an ACK is received for the latest not yet acknowledged
 * netlink message.
 */
int nl_wait_for_ack(struct nl_handle *handle)
{
	int err;
	struct nl_cb *cb = nl_cb_clone(nl_handle_get_cb(handle));

	nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, NULL);

	err = nl_recvmsgs(handle, cb);
	nl_cb_destroy(cb);

	return err;
}

/** @} */

/**
 * @name Netlink Family Translations
 * @{
 */

static struct trans_tbl nlfamilies[] = {
	__ADD(NETLINK_ROUTE,route)
	__ADD(NETLINK_W1,w1)
	__ADD(NETLINK_USERSOCK,usersock)
	__ADD(NETLINK_FIREWALL,firewall)
	__ADD(NETLINK_INET_DIAG,inetdiag)
	__ADD(NETLINK_NFLOG,nflog)
	__ADD(NETLINK_XFRM,xfrm)
	__ADD(NETLINK_SELINUX,selinux)
	__ADD(NETLINK_ISCSI,iscsi)
	__ADD(NETLINK_AUDIT,audit)
	__ADD(NETLINK_FIB_LOOKUP,fib_lookup)
	__ADD(NETLINK_CONNECTOR,connector)
	__ADD(NETLINK_NETFILTER,netfilter)
	__ADD(NETLINK_IP6_FW,ip6_fw)
	__ADD(NETLINK_DNRTMSG,dnrtmsg)
	__ADD(NETLINK_KOBJECT_UEVENT,kobject_uevent)
	__ADD(NETLINK_GENERIC,generic)
};

/**
 * Convert netlink family to character string.
 * @arg family		Netlink family.
 * @arg buf		Destination buffer.
 * @arg size		Size of destination buffer.
 *
 * Converts a netlink family to a character string and stores it in
 * the specified destination buffer.
 *
 * @return The destination buffer or the family encoded in hexidecimal
 *         form if no match was found.
 */
char * nl_nlfamily2str(int family, char *buf, size_t size)
{
	return __type2str(family, buf, size, nlfamilies,
			  ARRAY_SIZE(nlfamilies));
}

/**
 * Convert character string to netlink family.
 * @arg name		Name of netlink family.
 *
 * Converts the provided character string specifying a netlink
 * family to the corresponding numeric value.
 *
 * @return Numeric netlink family or a negative value if no match was found.
 */
int nl_str2nlfamily(const char *name)
{
	return __str2type(name, nlfamilies, ARRAY_SIZE(nlfamilies));
}
						    b->a_multicast));
	diff |= ADDR_DIFF(BROADCAST,	nl_addr_cmp(a->a_bcast, b->a_bcast));

	if (flags & LOOSE_FLAG_COMPARISON)
		diff |= ADDR_DIFF(FLAGS,
				  (a->a_flags ^ b->a_flags) & b->a_flag_mask);
	else
		diff |= ADDR_DIFF(FLAGS, a->a_flags != b->a_flags);

#undef ADDR_DIFF

	return diff;
}

static struct trans_tbl addr_attrs[] = {
	__ADD(ADDR_ATTR_FAMILY, family)
	__ADD(ADDR_ATTR_PREFIXLEN, prefixlen)
	__ADD(ADDR_ATTR_FLAGS, flags)
	__ADD(ADDR_ATTR_SCOPE, scope)
	__ADD(ADDR_ATTR_IFINDEX, ifindex)
	__ADD(ADDR_ATTR_LABEL, label)
	__ADD(ADDR_ATTR_CACHEINFO, cacheinfo)
	__ADD(ADDR_ATTR_PEER, peer)
	__ADD(ADDR_ATTR_LOCAL, local)
	__ADD(ADDR_ATTR_BROADCAST, broadcast)
	__ADD(ADDR_ATTR_ANYCAST, anycast)
	__ADD(ADDR_ATTR_MULTICAST, multicast)
};

static char *addr_attrs2str(int attrs, char *buf, size_t len)
{
Esempio n. 21
0
        nl_dump(p, "copy_range=%u ", log->log_copy_range);

    if (log->ce_mask & LOG_ATTR_FLUSH_TIMEOUT)
        nl_dump(p, "flush_timeout=%u ", log->log_flush_timeout);

    if (log->ce_mask & LOG_ATTR_ALLOC_SIZE)
        nl_dump(p, "alloc_size=%u ", log->log_alloc_size);

    if (log->ce_mask & LOG_ATTR_QUEUE_THRESHOLD)
        nl_dump(p, "queue_threshold=%u ", log->log_queue_threshold);

    nl_dump(p, "\n");
}

static struct trans_tbl copy_modes[] = {
    __ADD(NFNL_LOG_COPY_NONE,	none)
    __ADD(NFNL_LOG_COPY_META,	meta)
    __ADD(NFNL_LOG_COPY_PACKET,	packet)
};

char *nfnl_log_copy_mode2str(enum nfnl_log_copy_mode copy_mode, char *buf,
                             size_t len)
{
    return __type2str(copy_mode, buf, len, copy_modes,
                      ARRAY_SIZE(copy_modes));
}

enum nfnl_log_copy_mode nfnl_log_str2copy_mode(const char *name)
{
    return __str2type(name, copy_modes, ARRAY_SIZE(copy_modes));
}
Esempio n. 22
0
File: cbq.c Progetto: artisdom/mipv6
#include <netlink/route/qdisc-modules.h>
#include <netlink/route/class.h>
#include <netlink/route/class-modules.h>
#include <netlink/route/link.h>
#include <netlink/route/sch/cbq.h>
#include <netlink/route/cls/police.h>

/**
 * @ingroup qdisc
 * @ingroup class
 * @defgroup cbq Class Based Queueing (CBQ)
 * @{
 */

static struct trans_tbl ovl_strategies[] = {
	__ADD(TC_CBQ_OVL_CLASSIC,classic)
	__ADD(TC_CBQ_OVL_DELAY,delay)
	__ADD(TC_CBQ_OVL_LOWPRIO,lowprio)
	__ADD(TC_CBQ_OVL_DROP,drop)
	__ADD(TC_CBQ_OVL_RCLASSIC,rclassic)
};

/**
 * Convert a CBQ OVL strategy to a character string
 * @arg type		CBQ OVL strategy
 * @arg buf		destination buffer
 * @arg len		length of destination buffer
 *
 * Converts a CBQ OVL strategy to a character string and stores in the
 * provided buffer. Returns the destination buffer or the type
 * encoded in hex if no match was found.
Esempio n. 23
0
		buf += l;
		len -= l;
	}

	return buf_orig;
}

/** @} */

/**
 * @name Netlink Family Translations
 * @{
 */

static const struct trans_tbl nlfamilies[] = {
	__ADD(NETLINK_ROUTE,route)
	__ADD(NETLINK_USERSOCK,usersock)
	__ADD(NETLINK_FIREWALL,firewall)
	__ADD(NETLINK_INET_DIAG,inetdiag)
	__ADD(NETLINK_NFLOG,nflog)
	__ADD(NETLINK_XFRM,xfrm)
	__ADD(NETLINK_SELINUX,selinux)
	__ADD(NETLINK_ISCSI,iscsi)
	__ADD(NETLINK_AUDIT,audit)
	__ADD(NETLINK_FIB_LOOKUP,fib_lookup)
	__ADD(NETLINK_CONNECTOR,connector)
	__ADD(NETLINK_NETFILTER,netfilter)
	__ADD(NETLINK_IP6_FW,ip6_fw)
	__ADD(NETLINK_DNRTMSG,dnrtmsg)
	__ADD(NETLINK_KOBJECT_UEVENT,kobject_uevent)
	__ADD(NETLINK_GENERIC,generic)
Esempio n. 24
0
	req.idiag_family = family;
	req.idiag_states = states;
	req.idiag_ext = ext;

	return nl_send_simple(sk, TCPDIAG_GETSOCK, flags, &req, sizeof(req));
}

/** @} */

/**
 * @name Inet Diag flag and attribute conversions
 * @{
 */

static const struct trans_tbl idiag_states[] = {
	__ADD(IDIAG_SS_UNKNOWN, unknown),
	__ADD(IDIAG_SS_ESTABLISHED, established),
	__ADD(IDIAG_SS_SYN_SENT, syn_sent),
	__ADD(IDIAG_SS_SYN_RECV, syn_recv),
	__ADD(IDIAG_SS_FIN_WAIT1, fin_wait),
	__ADD(IDIAG_SS_FIN_WAIT2, fin_wait2),
	__ADD(IDIAG_SS_TIME_WAIT, time_wait),
	__ADD(IDIAG_SS_CLOSE, close),
	__ADD(IDIAG_SS_CLOSE_WAIT, close_wait),
	__ADD(IDIAG_SS_LAST_ACK, last_ack),
	__ADD(IDIAG_SS_LISTEN, listen),
	__ADD(IDIAG_SS_CLOSING, closing),
	__ADD(IDIAG_SS_MAX, max),
	{ ((1<<IDIAG_SS_MAX)-1), "all" }
};
struct vlan_info
{
	uint16_t		vi_vlan_id;
	uint32_t		vi_flags;
	uint32_t		vi_flags_mask;
	uint32_t		vi_ingress_qos[VLAN_PRIO_MAX+1];
	uint32_t		vi_negress;
	uint32_t		vi_egress_size;
	struct vlan_map * 	vi_egress_qos;
	uint32_t		vi_mask;
};
/** @endcond */

static struct trans_tbl vlan_flags[] = {
	__ADD(VLAN_FLAG_REORDER_HDR, reorder_hdr)
};

char *rtnl_link_vlan_flags2str(int flags, char *buf, size_t len)
{
	return __flags2str(flags, buf, len, vlan_flags, ARRAY_SIZE(vlan_flags));
}

int rtnl_link_vlan_str2flags(const char *name)
{
	return __str2flags(name, vlan_flags, ARRAY_SIZE(vlan_flags));
}

static struct nla_policy vlan_policy[IFLA_VLAN_MAX+1] = {
	[IFLA_VLAN_ID]		= { .type = NLA_U16 },
	[IFLA_VLAN_FLAGS]	= { .minlen = sizeof(struct ifla_vlan_flags) },
Esempio n. 26
0
 * @see rtnl_link_bridge_unset_flags()
 *
 * @return Flags or a negative error code.
 * @retval -NLE_OPNOTSUPP Link is not a bridge
 */
int rtnl_link_bridge_get_flags(struct rtnl_link *link)
{
	struct bridge_data *bd = bridge_data(link);

	IS_BRIDGE_LINK_ASSERT(link);

	return bd->b_flags;
}

static const struct trans_tbl bridge_flags[] = {
	__ADD(RTNL_BRIDGE_HAIRPIN_MODE, hairpin_mode)
	__ADD(RTNL_BRIDGE_BPDU_GUARD, 	bpdu_guard)
	__ADD(RTNL_BRIDGE_ROOT_BLOCK,	root_block)
	__ADD(RTNL_BRIDGE_FAST_LEAVE,	fast_leave)
};

/**
 * @name Flag Translation
 * @{
 */

char *rtnl_link_bridge_flags2str(int flags, char *buf, size_t len)
{
	return __flags2str(flags, buf, len, bridge_flags, ARRAY_SIZE(bridge_flags));
}