tools/testing/selftests/net/icmp_rfc4884.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/net/icmp_rfc4884.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/net/icmp_rfc4884.c
Extension
.c
Size
17244 bytes
Lines
680
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

struct sockaddr_inet {
	union {
		struct sockaddr_in6 v6;
		struct sockaddr_in v4;
		struct sockaddr sa;
	};
	socklen_t len;
};

struct ip_case_info {
	int	domain;
	int	level;
	int	opt1;
	int	opt2;
	int	proto;
	int	(*build_func)(uint8_t *buf, ssize_t buflen, bool with_ext,
			      int payload_len, bool bad_csum, bool bad_len,
			      bool smaller_len);
	int	min_payload;
};

static int bringup_loopback(void)
{
	struct ifreq ifr = {
		.ifr_name = "lo"
	};
	int fd;

	fd = socket(AF_INET, SOCK_DGRAM, 0);
	if (fd < 0)
		return -1;

	if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0)
		goto err;

	ifr.ifr_flags = ifr.ifr_flags | IFF_UP;

	if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0)
		goto err;

	close(fd);
	return 0;

err:
	close(fd);
	return -1;
}

static uint16_t csum(const void *buf, size_t len)
{
	const uint8_t *data = buf;
	uint32_t sum = 0;

	while (len > 1) {
		sum += (data[0] << 8) | data[1];
		data += 2;
		len -= 2;
	}

	if (len == 1)
		sum += data[0] << 8;

	while (sum >> 16)
		sum = (sum & 0xFFFF) + (sum >> 16);

	return ~sum & 0xFFFF;
}

static int poll_err(int fd)
{
	struct pollfd pfd;

	memset(&pfd, 0, sizeof(pfd));
	pfd.fd = fd;

	if (poll(&pfd, 1, 5000) != 1 || pfd.revents != POLLERR)
		return -1;

	return 0;
}

static void set_addr(struct sockaddr_inet *addr, int domain,
		     unsigned short port)
{
	memset(addr, 0, sizeof(*addr));

	switch (domain) {
	case AF_INET:
		addr->v4.sin_family = AF_INET;
		addr->v4.sin_port = htons(port);

Annotation

Implementation Notes