1d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes/*
277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes * Copyright (c) 2017 The strace developers.
4d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * All rights reserved.
5d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes *
6d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * Redistribution and use in source and binary forms, with or without
7d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * modification, are permitted provided that the following conditions
8d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * are met:
9d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * 1. Redistributions of source code must retain the above copyright
10d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes *    notice, this list of conditions and the following disclaimer.
11d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * 2. Redistributions in binary form must reproduce the above copyright
12d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes *    notice, this list of conditions and the following disclaimer in the
13d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes *    documentation and/or other materials provided with the distribution.
14d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * 3. The name of the author may not be used to endorse or promote products
15d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes *    derived from this software without specific prior written permission.
16d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes *
17d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes */
28d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
2977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#include "tests.h"
30d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
3177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#include <stdio.h>
3277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#include "netlink.h"
3377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#include <linux/rtnetlink.h>
3477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
3577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#ifdef RTM_GETNSID
3677c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
3777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes# include "test_nlattr.h"
3877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
3977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughesstatic void
4077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughesinit_rtgenmsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
4177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes{
4277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	SET_STRUCT(struct nlmsghdr, nlh,
4377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		.nlmsg_len = msg_len,
4477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		.nlmsg_type = RTM_GETNSID,
4577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		.nlmsg_flags = NLM_F_DUMP
4677c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	);
4777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
4877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	struct rtgenmsg *const msg = NLMSG_DATA(nlh);
4977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	SET_STRUCT(struct rtgenmsg, msg,
5077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		.rtgen_family = AF_UNIX
5177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	);
5277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes}
5377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
5477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughesstatic void
5577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughesprint_rtgenmsg(const unsigned int msg_len)
5677c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes{
5777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	printf("{len=%u, type=RTM_GETNSID, flags=NLM_F_DUMP"
5877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	       ", seq=0, pid=0}, {rtgen_family=AF_UNIX}",
5977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	       msg_len);
6077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes}
61d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
6277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughesint
6377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughesmain(void)
6477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes{
6577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	skip_if_unavailable("/proc/self/fd/");
66d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
6777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	const int fd = create_nl_socket(NETLINK_ROUTE);
6877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	const unsigned int hdrlen = sizeof(struct rtgenmsg);
6977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
7077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
7177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	static char pattern[4096];
7277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
7377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
7477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	const unsigned int nla_type = 0xffff & NLA_TYPE_MASK;
7577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	char nla_type_str[256];
7677c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	sprintf(nla_type_str, "%#x /* NETNSA_??? */", nla_type);
7777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	TEST_NLATTR_(fd, nlh0, hdrlen,
7877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		     init_rtgenmsg, print_rtgenmsg,
7977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		     nla_type, nla_type_str,
8077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		     4, pattern, 4,
8177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		     print_quoted_hex(pattern, 4));
8277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
8377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	puts("+++ exited with 0 +++");
8477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	return 0;
8577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes}
8677c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
8777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#else
8877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
8977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott HughesSKIP_MAIN_UNDEFINED("RTM_GETNSID")
9077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
9177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#endif
92