177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes/*
277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes * Copyright (c) 2017 The strace developers.
477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes * All rights reserved.
577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes *
677c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes * Redistribution and use in source and binary forms, with or without
777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes * modification, are permitted provided that the following conditions
877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes * are met:
977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes * 1. Redistributions of source code must retain the above copyright
1077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes *    notice, this list of conditions and the following disclaimer.
1177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes * 2. Redistributions in binary form must reproduce the above copyright
1277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes *    notice, this list of conditions and the following disclaimer in the
1377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes *    documentation and/or other materials provided with the distribution.
1477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes * 3. The name of the author may not be used to endorse or promote products
1577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes *    derived from this software without specific prior written permission.
1677c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes *
1777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2677c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes */
2877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
2977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#include "tests.h"
3077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
3177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#include <stdio.h>
3277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#include <inttypes.h>
3377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#include "test_nlattr.h"
3477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#include <linux/if.h>
3577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#include <linux/if_arp.h>
3677c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#ifdef HAVE_LINUX_IF_LINK_H
3777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes# include <linux/if_link.h>
3877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#endif
3977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#include <linux/rtnetlink.h>
4077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
4177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#define IFLA_BRPORT_PRIORITY 2
4277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#define IFLA_BRPORT_MESSAGE_AGE_TIMER 21
4377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
4477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughesconst unsigned int hdrlen = sizeof(struct ifinfomsg);
4577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
4677c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughesstatic void
4777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughesinit_ifinfomsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
4877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes{
4977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	SET_STRUCT(struct nlmsghdr, nlh,
5077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		.nlmsg_len = msg_len,
5177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		.nlmsg_type = RTM_GETLINK,
5277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		.nlmsg_flags = NLM_F_DUMP
5377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	);
5477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
5577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	struct ifinfomsg *const msg = NLMSG_DATA(nlh);
5677c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	SET_STRUCT(struct ifinfomsg, msg,
5777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		.ifi_family = AF_UNIX,
5877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		.ifi_type = ARPHRD_LOOPBACK,
5977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		.ifi_index = ifindex_lo(),
6077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		.ifi_flags = IFF_UP,
6177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	);
6277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
6377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	struct nlattr *const nla = NLMSG_ATTR(nlh, sizeof(*msg));
6477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	SET_STRUCT(struct nlattr, nla,
6577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		.nla_len = msg_len - NLMSG_SPACE(hdrlen),
6677c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		.nla_type = IFLA_PROTINFO
6777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	);
6877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes}
6977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
7077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughesstatic void
7177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughesprint_ifinfomsg(const unsigned int msg_len)
7277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes{
7377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	printf("{len=%u, type=RTM_GETLINK, flags=NLM_F_DUMP"
7477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	       ", seq=0, pid=0}, {ifi_family=AF_UNIX"
7577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	       ", ifi_type=ARPHRD_LOOPBACK"
7677c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	       ", ifi_index=" IFINDEX_LO_STR
7777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	       ", ifi_flags=IFF_UP, ifi_change=0}"
7877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	       ", {{nla_len=%u, nla_type=IFLA_PROTINFO}",
7977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	       msg_len, msg_len - NLMSG_SPACE(hdrlen));
8077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes}
8177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
8277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughesint
8377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughesmain(void)
8477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes{
8577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	skip_if_unavailable("/proc/self/fd/");
8677c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
8777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	const int fd = create_nl_socket(NETLINK_ROUTE);
8877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
8977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
9077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	static char pattern[4096];
9177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
9277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
9377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	const uint16_t u16 = 0xabcd;
9477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen,
9577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes				  init_ifinfomsg, print_ifinfomsg,
9677c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes				  IFLA_BRPORT_PRIORITY, pattern, u16,
9777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes				  printf("%u", u16));
9877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
9977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	const uint64_t u64 = 0xabcdedeeefeafeab;
10077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen,
10177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes				  init_ifinfomsg, print_ifinfomsg,
10277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes				  IFLA_BRPORT_MESSAGE_AGE_TIMER, pattern, u64,
10377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes				  printf("%" PRIu64, u64));
10477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
10577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#ifdef HAVE_STRUCT_IFLA_BRIDGE_ID
10677c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	static const struct ifla_bridge_id id = {
10777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		.prio = { 0xab, 0xcd },
10877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		.addr = { 0xab, 0xcd, 0xef, 0xac, 0xbc, 0xcd }
10977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	};
11077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen,
11177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes				  init_ifinfomsg, print_ifinfomsg,
11277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes				  IFLA_BRPORT_ROOT_ID, pattern, id,
11377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes				  printf("{prio=[%u, %u]"
11477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes					 ", addr=%02x:%02x:%02x:%02x:%02x:%02x}",
11577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes					 id.prio[0], id.prio[1],
11677c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes					 id.addr[0], id.addr[1], id.addr[2],
11777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes					 id.addr[3], id.addr[4], id.addr[5]));
11877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#endif
11977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
12077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	puts("+++ exited with 0 +++");
12177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	return 0;
12277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes}
123