# Copyright 2018 syzkaller project authors. All rights reserved.
# Use of this source code is governed by Apache 2 LICENSE that can be teamnd in the LICENSE file.

# AF_NETLINK/NETLINK_GENERIC/team support.

include <linux/net.h>
include <net/netlink.h>
include <uapi/linux/netlink.h>
include <uapi/linux/genetlink.h>
include <uapi/linux/if_team.h>

resource genl_team_family_id[int16]
resource ifindex_team[ifindex]
type msghdr_nl_team[CMD] msghdr_netlink[netlink_msg_t[genl_team_family_id, genlmsghdr_t[CMD], team_nl_policy]]

syz_genetlink_get_family_id$team(name ptr[in, string["team"]]) genl_team_family_id
ioctl$ifreq_SIOCGIFINDEX_team(fd sock, cmd const[SIOCGIFINDEX], arg ptr[inout, ifreq_dev_t["team0", ifindex_team[opt]]])

sendmsg$TEAM_CMD_NOOP(fd sock_nl_generic, msg ptr[in, msghdr_nl_team[TEAM_CMD_NOOP]], f flags[send_flags])
sendmsg$TEAM_CMD_OPTIONS_SET(fd sock_nl_generic, msg ptr[in, msghdr_nl_team[TEAM_CMD_OPTIONS_SET]], f flags[send_flags])
sendmsg$TEAM_CMD_OPTIONS_GET(fd sock_nl_generic, msg ptr[in, msghdr_nl_team[TEAM_CMD_OPTIONS_GET]], f flags[send_flags])
sendmsg$TEAM_CMD_PORT_LIST_GET(fd sock_nl_generic, msg ptr[in, msghdr_nl_team[TEAM_CMD_PORT_LIST_GET]], f flags[send_flags])

team_nl_policy {
	TEAM_ATTR_TEAM_IFINDEX	nlattr[TEAM_ATTR_TEAM_IFINDEX, ifindex_team]
	TEAM_ATTR_LIST_OPTION	nlattr[TEAM_ATTR_LIST_OPTION, array[nlattr[TEAM_ATTR_ITEM_OPTION, team_attr_option]]]
} [packed, align_4]

type team_nl_option_policy[NAME, TYPE, DATA] {
	TEAM_ATTR_OPTION_NAME	nlattr[TEAM_ATTR_OPTION_NAME, string[NAME, TEAM_STRING_MAX_LEN]]
	TEAM_ATTR_OPTION_TYPE	nlattr[TEAM_ATTR_OPTION_TYPE, const[TYPE, int8]]
	TEAM_ATTR_OPTION_DATA	nlattr[TEAM_ATTR_OPTION_DATA, DATA]
} [packed, align_4]

type team_nl_option_policy_per_port[NAME, TYPE, DATA] {
	opt				team_nl_option_policy[NAME, TYPE, DATA]
	TEAM_ATTR_OPTION_PORT_IFINDEX	nlattr[TEAM_ATTR_OPTION_PORT_IFINDEX, ifindex]
} [packed, align_4]

type team_nl_option_policy_array[NAME, TYPE, DATA, SIZE] {
	opt				team_nl_option_policy[NAME, TYPE, DATA]
	TEAM_ATTR_OPTION_ARRAY_INDEX	nlattr[TEAM_ATTR_OPTION_ARRAY_INDEX, int32[0:SIZE]]
} [packed, align_4]

team_attr_option [
	name				team_nl_option_policy["mode", NLA_STRING, string[team_attr_option_mode]]
	notify_peers_count		team_nl_option_policy["notify_peers_count", NLA_U32, int32]
	notify_peers_interval		team_nl_option_policy["notify_peers_interval", NLA_U32, int32]
	mcast_rejoin_count		team_nl_option_policy["mcast_rejoin_count", NLA_U32, int32]
	mcast_rejoin_interval		team_nl_option_policy["mcast_rejoin_interval", NLA_U32, int32]
# TODO: NLA_FLAG value is determined by presence of TEAM_ATTR_OPTION_DATA attr, but we always pass it.
	enabled				team_nl_option_policy_per_port["enabled", NLA_FLAG, void]
	user_linkup			team_nl_option_policy_per_port["user_linkup", NLA_FLAG, void]
	user_linkup_enabled		team_nl_option_policy_per_port["user_linkup_enabled", NLA_FLAG, void]
	priority			team_nl_option_policy_per_port["priority", NLA_S32, int32]
	queue_id			team_nl_option_policy_per_port["queue_id", NLA_U32, int32]
	activeport			team_nl_option_policy["activeport", NLA_U32, ifindex]
	bpf_hash_func			team_nl_option_policy["bpf_hash_func", NLA_BINARY, array[sock_filter]]
	lb_tx_method			team_nl_option_policy["lb_tx_method", NLA_STRING, string[team_attr_option_lb_tx_method]]
	lb_tx_hash_to_port_mapping	team_nl_option_policy_array["lb_tx_hash_to_port_mapping", NLA_U32, ifindex, 256]
	lb_hash_stats			team_nl_option_policy_array["lb_hash_stats", NLA_BINARY, int32, 256]
	lb_port_stats			team_nl_option_policy_per_port["lb_port_stats", NLA_BINARY, int32]
	lb_stats_refresh_interval	team_nl_option_policy["lb_stats_refresh_interval", NLA_U32, int32]
] [varlen]

team_attr_option_mode = "activebackup", "broadcast", "loadbalance", "random", "roundrobin"
team_attr_option_lb_tx_method = "hash", "hash_to_port_mapping"