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

include <linux/ip.h>
include <linux/ipv6.h>
include <linux/route.h>
include <uapi/linux/if_arp.h>
include <uapi/linux/netfilter_ipv6/ip6_tables.h>

# IP sockets

resource sock_in[sock]

sock_in_pair {
	f0	sock_in
	f1	sock_in
}

sockaddr_in {
	family	const[AF_INET, int16]
	port	sock_port
	addr	ipv4_addr
	pad	array[const[0, int8], 8]
}

sockaddr_storage_in {
	addr	sockaddr_in
} [size[SOCKADDR_STORAGE_SIZE], align_ptr]

socket$inet(domain const[AF_INET], type flags[socket_type], proto int8) sock_in
socketpair$inet(domain const[AF_INET], type flags[socket_type], proto int8, fds ptr[out, sock_in_pair])
accept$inet(fd sock_in, peer ptr[out, sockaddr_in, opt], peerlen ptr[inout, len[peer, int32]]) sock_in
accept4$inet(fd sock_in, peer ptr[out, sockaddr_in, opt], peerlen ptr[inout, len[peer, int32]], flags flags[accept_flags]) sock_in
bind$inet(fd sock_in, addr ptr[in, sockaddr_in], addrlen len[addr])
connect$inet(fd sock_in, addr ptr[in, sockaddr_in], addrlen len[addr])
sendto$inet(fd sock_in, buf buffer[in], len len[buf], f flags[send_flags], addr ptr[in, sockaddr_in, opt], addrlen len[addr])
recvfrom$inet(fd sock_in, buf buffer[out], len len[buf], f flags[recv_flags], addr ptr[in, sockaddr_in, opt], addrlen len[addr])
getsockname$inet(fd sock_in, addr ptr[out, sockaddr_in], addrlen ptr[inout, len[addr, int32]])
getpeername$inet(fd sock_in, peer ptr[out, sockaddr_in], peerlen ptr[inout, len[peer, int32]])

# Generic IP options

inet_option_types_int = IP_TOS, IP_TTL, IP_HDRINCL, IP_ROUTER_ALERT, IP_RECVOPTS, IP_RETOPTS, IP_PKTINFO, IP_MTU_DISCOVER, IP_RECVERR, IP_RECVTTL, IP_RECVTOS, IP_MTU, IP_FREEBIND, IP_PASSSEC, IP_TRANSPARENT, IP_RECVORIGDSTADDR, IP_MINTTL, IP_NODEFRAG, IP_CHECKSUM, IP_BIND_ADDRESS_NO_PORT, IP_MULTICAST_TTL, IP_MULTICAST_LOOP, IP_MULTICAST_ALL, IP_UNICAST_IF

inet_option_types_buf = IP_OPTIONS, IP_PKTOPTIONS, IP_IPSEC_POLICY, IP_XFRM_POLICY, IP_MULTICAST_IF, IP_ADD_MEMBERSHIP, IP_DROP_MEMBERSHIP, IP_UNBLOCK_SOURCE, IP_BLOCK_SOURCE, IP_ADD_SOURCE_MEMBERSHIP, IP_DROP_SOURCE_MEMBERSHIP, IP_MSFILTER, MCAST_JOIN_GROUP, MCAST_BLOCK_SOURCE, MCAST_UNBLOCK_SOURCE, MCAST_LEAVE_GROUP, MCAST_JOIN_SOURCE_GROUP, MCAST_LEAVE_SOURCE_GROUP, MCAST_MSFILTER

getsockopt$inet_int(fd sock_in, level const[IPPROTO_IP], optname flags[inet_option_types_int], optval ptr[out, int32], optlen ptr[inout, len[optval, int32]])
setsockopt$inet_int(fd sock_in, level const[IPPROTO_IP], optname flags[inet_option_types_int], optval ptr[in, int32], optlen len[optval])
getsockopt$inet_buf(fd sock_in, level const[IPPROTO_IP], optname flags[inet_option_types_buf], optval buffer[out], optlen ptr[inout, len[optval, int32]])
setsockopt$inet_buf(fd sock_in, level const[IPPROTO_IP], optname flags[inet_option_types_buf], optval buffer[in], optlen len[optval])

# Specific IP options

sockopt_opt_ip_opts = IP_OPTIONS, IP_PKTOPTIONS

getsockopt$inet_opts(fd sock_in, level const[IPPROTO_IP], optname flags[sockopt_opt_ip_opts], optval buffer[out], optlen ptr[inout, len[optval, int32]])
setsockopt$inet_opts(fd sock_in, level const[IPPROTO_IP], optname flags[sockopt_opt_ip_opts], optval buffer[in], optlen len[optval])

getsockopt$inet_IP_IPSEC_POLICY(fd sock_in, level const[IPPROTO_IP], optname const[IP_IPSEC_POLICY], optval ptr[out, xfrm_filter], optlen ptr[inout, len[optval, int32]])
setsockopt$inet_IP_IPSEC_POLICY(fd sock_in, level const[IPPROTO_IP], optname const[IP_IPSEC_POLICY], optval ptr[in, xfrm_filter], optlen len[optval])
getsockopt$inet_IP_XFRM_POLICY(fd sock_in, level const[IPPROTO_IP], optname const[IP_XFRM_POLICY], optval ptr[out, xfrm_filter], optlen ptr[inout, len[optval, int32]])
setsockopt$inet_IP_XFRM_POLICY(fd sock_in, level const[IPPROTO_IP], optname const[IP_XFRM_POLICY], optval ptr[in, xfrm_filter], optlen len[optval])

sockopt_opt_ip_mreq = IP_ADD_MEMBERSHIP, IP_DROP_MEMBERSHIP, IP_MULTICAST_IF

getsockopt$inet_mreq(fd sock_in, level const[IPPROTO_IP], optname flags[sockopt_opt_ip_mreq], optval ptr[out, ip_mreq], optlen ptr[inout, len[optval, int32]])
setsockopt$inet_mreq(fd sock_in, level const[IPPROTO_IP], optname flags[sockopt_opt_ip_mreq], optval ptr[in, ip_mreq], optlen len[optval])
getsockopt$inet_mreqn(fd sock_in, level const[IPPROTO_IP], optname flags[sockopt_opt_ip_mreq], optval ptr[out, ip_mreqn], optlen ptr[inout, len[optval, int32]])
setsockopt$inet_mreqn(fd sock_in, level const[IPPROTO_IP], optname flags[sockopt_opt_ip_mreq], optval ptr[in, ip_mreqn], optlen len[optval])

sockopt_opt_ip_mreqsrc = IP_ADD_SOURCE_MEMBERSHIP, IP_BLOCK_SOURCE, IP_DROP_SOURCE_MEMBERSHIP, IP_UNBLOCK_SOURCE

getsockopt$inet_mreqsrc(fd sock_in, level const[IPPROTO_IP], optname flags[sockopt_opt_ip_mreqsrc], optval ptr[out, ip_mreq_source], optlen ptr[inout, len[optval, int32]])
setsockopt$inet_mreqsrc(fd sock_in, level const[IPPROTO_IP], optname flags[sockopt_opt_ip_mreqsrc], optval ptr[in, ip_mreq_source], optlen len[optval])

setsockopt$inet_msfilter(fd sock_in, level const[IPPROTO_IP], optname const[IP_MSFILTER], optval ptr[in, ip_msfilter], optlen len[optval])

setsockopt$inet_MCAST_JOIN_GROUP(fd sock_in, level const[IPPROTO_IP], optname const[MCAST_JOIN_GROUP], optval ptr[in, group_req_in], optlen len[optval])
setsockopt$inet_MCAST_LEAVE_GROUP(fd sock_in, level const[IPPROTO_IP], optname const[MCAST_LEAVE_GROUP], optval ptr[in, group_req_in], optlen len[optval])

sockopt_opt_ip_group_source_req = MCAST_JOIN_SOURCE_GROUP, MCAST_LEAVE_SOURCE_GROUP, MCAST_BLOCK_SOURCE, MCAST_UNBLOCK_SOURCE

setsockopt$inet_group_source_req(fd sock_in, level const[IPPROTO_IP], optname flags[sockopt_opt_ip_group_source_req], optval ptr[in, group_source_req_in], optlen len[optval])

setsockopt$inet_MCAST_MSFILTER(fd sock_in, level const[IPPROTO_IP], optname const[MCAST_MSFILTER], optval ptr[in, group_filter_in], optlen len[optval])

getsockopt$inet_pktinfo(fd sock_in, level const[IPPROTO_IP], optname const[IP_PKTINFO], optval ptr[out, in_pktinfo], optlen ptr[inout, len[optval, int32]])
setsockopt$inet_pktinfo(fd sock_in, level const[IPPROTO_IP], optname const[IP_PKTINFO], optval ptr[in, in_pktinfo], optlen len[optval])

getsockopt$inet_mtu(fd sock_in, level const[IPPROTO_IP], optname const[IP_MTU_DISCOVER], optval ptr[out, flags[ip_mtu_discover, int32]], optlen ptr[inout, len[optval, int32]])
setsockopt$inet_mtu(fd sock_in, level const[IPPROTO_IP], optname const[IP_MTU_DISCOVER], optval ptr[in, flags[ip_mtu_discover, int32]], optlen len[optval])

ip_mreq {
	imr_multiaddr	ipv4_addr
	imr_interface	ipv4_addr
}

ip_mreqn {
	imr_multiaddr	ipv4_addr
	imr_address	ipv4_addr
	imr_ifindex	ifindex
}

ip_mreq_source {
	imr_multiaddr	ipv4_addr
	imr_interface	ipv4_addr
	imr_sourceaddr	ipv4_addr
}

ip_msfilter {
	imsf_multiaddr	ipv4_addr
	imsf_interface	ipv4_addr
	imsf_fmode	flags[ip_msfilter_mode, int32]
	imsf_numsrc	len[imsf_slist, int32]
	imsf_slist	array[ipv4_addr]
}

ip_msfilter_mode = MCAST_INCLUDE, MCAST_EXCLUDE

in_pktinfo {
	ipi_ifindex	ifindex
	ipi_spec_dst	ipv4_addr
	ipi_addr	ipv4_addr
}

group_req_in {
	gr_interface	int32
	gr_group	sockaddr_storage_in
}

group_source_req_in {
	gsr_interface	int32
	gsr_group	sockaddr_storage_in
	gsr_source	sockaddr_storage_in
}

group_filter_in {
	gf_interface	int32
	gf_group	sockaddr_storage_in
	gf_fmode	flags[ip_msfilter_mode, int32]
	gf_numsrc	len[gf_slist, int32]
	gf_slist	array[sockaddr_storage_in]
}

ip_mtu_discover = IP_PMTUDISC_DONT, IP_PMTUDISC_WANT, IP_PMTUDISC_DO, IP_PMTUDISC_PROBE, IP_PMTUDISC_INTERFACE, IP_PMTUDISC_OMIT

# IP ioctls

# http://lxr.free-electrons.com/ident?i=inet_ioctl

ioctl$sock_inet_SIOCADDRT(fd sock_in, cmd const[SIOCADDRT], arg ptr[in, rtentry_in])
ioctl$sock_inet_SIOCDELRT(fd sock_in, cmd const[SIOCDELRT], arg ptr[in, rtentry_in])
ioctl$sock_inet_SIOCRTMSG(fd sock_in, cmd const[SIOCRTMSG], arg ptr[in, rtentry_in])

ioctl$sock_inet_SIOCDARP(fd sock_in, cmd const[SIOCDARP], arg ptr[in, arpreq_in])
ioctl$sock_inet_SIOCGARP(fd sock_in, cmd const[SIOCGARP], arg ptr[inout, arpreq_in])
ioctl$sock_inet_SIOCSARP(fd sock_in, cmd const[SIOCSARP], arg ptr[in, arpreq_in])

ioctl$sock_inet_SIOCGIFADDR(fd sock, cmd const[SIOCGIFADDR], arg ptr[inout, ifreq_t[sockaddr_in]])
ioctl$sock_inet_SIOCSIFADDR(fd sock, cmd const[SIOCSIFADDR], arg ptr[inout, ifreq_t[sockaddr_in]])

ioctl$sock_inet_SIOCGIFBRDADDR(fd sock, cmd const[SIOCGIFBRDADDR], arg ptr[inout, ifreq_t[sockaddr_in]])
ioctl$sock_inet_SIOCSIFBRDADDR(fd sock, cmd const[SIOCSIFBRDADDR], arg ptr[inout, ifreq_t[sockaddr_in]])

ioctl$sock_inet_SIOCGIFNETMASK(fd sock, cmd const[SIOCGIFNETMASK], arg ptr[inout, ifreq_t[sockaddr_in]])
ioctl$sock_inet_SIOCSIFNETMASK(fd sock, cmd const[SIOCSIFNETMASK], arg ptr[inout, ifreq_t[sockaddr_in]])

ioctl$sock_inet_SIOCGIFDSTADDR(fd sock, cmd const[SIOCGIFDSTADDR], arg ptr[inout, ifreq_t[sockaddr_in]])
ioctl$sock_inet_SIOCSIFDSTADDR(fd sock, cmd const[SIOCSIFDSTADDR], arg ptr[inout, ifreq_t[sockaddr_in]])

ioctl$sock_inet_SIOCGIFPFLAGS(fd sock, cmd const[SIOCGIFPFLAGS], arg ptr[inout, ifreq_t[int32]])
ioctl$sock_inet_SIOCSIFPFLAGS(fd sock, cmd const[SIOCSIFPFLAGS], arg ptr[inout, ifreq_t[int32]])

ioctl$sock_inet_SIOCSIFFLAGS(fd sock, cmd const[SIOCSIFFLAGS], arg ptr[inout, ifreq_t[flags[ifru_flags, int16]]])

rt_flags = RTF_UP, RTF_GATEWAY, RTF_HOST, RTF_REINSTATE, RTF_DYNAMIC, RTF_MODIFIED, RTF_MTU, RTF_WINDOW, RTF_IRTT, RTF_REJECT

rtentry_in {
	rt_pad1		int64
	rt_dst		sockaddr_in
	rt_gateway	sockaddr_in
	rt_genmask	sockaddr_in
	rt_flags	flags[rt_flags, int16]
	rt_pad2		int16
	rt_pad3		int64
	rt_pad4		intptr
	rt_metric	int16
	rt_dev		ptr[in, devname, opt]
	rt_mtu		int64
	rt_window	int64
	rt_irtt		int16
}

sockaddr_ethernet_family = ARPHRD_ETHER, ARPHRD_FDDI, ARPHRD_IEEE802

sockaddr_ethernet {
	sa_family	flags[sockaddr_ethernet_family, int16]
	sa_data		mac_addr
} [size[SOCKADDR_SIZE]]

arp_flags = ATF_COM, ATF_PERM, ATF_PUBL, ATF_USETRAILERS, ATF_NETMASK, ATF_DONTPUB

arpreq_in {
	arp_pa		sockaddr_in
	arp_ha		sockaddr_ethernet
	arp_flags	flags[arp_flags, int32]
	arp_netmask	sockaddr_in
	arp_dev		devname
}