/* Test to crash netlink with large vmalloc'ed skbuff */
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include <net/if.h>

#include <libmnl/libmnl.h>
#include <linux/if_link.h>
#include <linux/rtnetlink.h>

static int test(int family)
{
	struct mnl_socket *nl;
	char buf[MNL_SOCKET_BUFFER_SIZE*4];
	struct nlmsghdr *nlh;
	struct rtmsg *rtm;
	uint32_t prefix, seq, portid;
	int ret;

	nlh = mnl_nlmsg_put_header(buf);
	nlh->nlmsg_type	= 20; /* don't care */
	nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK;
	nlh->nlmsg_seq = seq = time(NULL);
	nlh->nlmsg_len += sizeof(buf) - sizeof(struct nlmsghdr) - 100;

	nl = mnl_socket_open(family);
	if (nl == NULL) {
		perror("mnl_socket_open");
		return -1;
	}

	if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
		perror("mnl_socket_bind");
		return -1;
	}
	portid = mnl_socket_get_portid(nl);

	/* Send garbage, don't care */
	if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
		perror("mnl_socket_send");
		return -1;
	}

	ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
	if (ret < 0) {
		perror("mnl_cb_run");
		return -1;
	}

	mnl_socket_close(nl);
	return 0;
}

int main(int argc, char *argv[])
{
	int i=0;

	for (i=0; i<20; i++) {
		printf("testing %d\n", i);
		test(i);
	}
}
