public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
blob 87cb8dd21d2e9ad67103b00d38aed152353c9ea0 3004 bytes (raw)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
 
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright (c) 2021 Red Hat GmbH
 * Author: Stefano Brivio <sbrivio@redhat.com>
 */

#ifndef IP_H
#define IP_H

#include <netinet/ip.h>
#include <netinet/ip6.h>

#include "checksum.h"

#define IN4_IS_ADDR_UNSPECIFIED(a) \
	((a)->s_addr == htonl_constant(INADDR_ANY))
#define IN4_IS_ADDR_BROADCAST(a) \
	((a)->s_addr == htonl_constant(INADDR_BROADCAST))
#define IN4_IS_ADDR_LOOPBACK(a) \
	(ntohl((a)->s_addr) >> IN_CLASSA_NSHIFT == IN_LOOPBACKNET)
#define IN4_IS_ADDR_MULTICAST(a) \
	(IN_MULTICAST(ntohl((a)->s_addr)))
#define IN4_ARE_ADDR_EQUAL(a, b) \
	(((struct in_addr *)(a))->s_addr == ((struct in_addr *)b)->s_addr)
#define IN4ADDR_LOOPBACK_INIT \
	{ .s_addr	= htonl_constant(INADDR_LOOPBACK) }
#define IN4ADDR_ANY_INIT \
	{ .s_addr	= htonl_constant(INADDR_ANY) }

#define L2_BUF_IP4_INIT(proto)						\
	{								\
		.version	= 4,					\
		.ihl		= 5,					\
		.tos		= 0,					\
		.tot_len	= 0,					\
		.id		= 0,					\
		.frag_off	= 0,					\
		.ttl		= 0xff,					\
		.protocol	= (proto),				\
		.saddr		= 0,					\
		.daddr		= 0,					\
	}
#define L2_BUF_IP4_PSUM(proto)	((uint32_t)htons_constant(0x4500) +	\
				 (uint32_t)htons_constant(0xff00 | (proto)))

#define L2_BUF_IP6_INIT(proto)						\
	{								\
		.priority	= 0,					\
		.version	= 6,					\
		.flow_lbl	= { 0 },				\
		.payload_len	= 0,					\
		.nexthdr	= (proto),				\
		.hop_limit	= 255,					\
		.saddr		= IN6ADDR_ANY_INIT,			\
		.daddr		= IN6ADDR_ANY_INIT,			\
	}

struct ipv6hdr {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
#if __BYTE_ORDER == __BIG_ENDIAN
	uint8_t			version:4,
				priority:4;
#else
	uint8_t			priority:4,
				version:4;
#endif
#pragma GCC diagnostic pop
	uint8_t			flow_lbl[3];

	uint16_t		payload_len;
	uint8_t			nexthdr;
	uint8_t			hop_limit;

	struct in6_addr		saddr;
	struct in6_addr		daddr;
};

struct ipv6_opt_hdr {
	uint8_t			nexthdr;
	uint8_t			hdrlen;
	/*
	 * TLV encoded option data follows.
	 */
} __attribute__((packed));	/* required for some archs */

char *ipv6_l4hdr(const struct pool *p, int idx, size_t offset, uint8_t *proto,
		 size_t *dlen);
static inline uint16_t ipv4_hdr_checksum(struct iphdr *iph, int proto)
{
	uint32_t sum = L2_BUF_IP4_PSUM(proto);

	sum += iph->tot_len;
	sum += (iph->saddr >> 16) & 0xffff;
	sum += iph->saddr & 0xffff;
	sum += (iph->daddr >> 16) & 0xffff;
	sum += iph->daddr & 0xffff;

	return ~csum_fold(sum);
}

static inline uint32_t proto_ipv4_header_checksum(struct iphdr *iph, int proto)
{
	uint32_t sum = htons(proto);

	sum += (iph->saddr >> 16) & 0xffff;
	sum += iph->saddr & 0xffff;
	sum += (iph->daddr >> 16) & 0xffff;
	sum += iph->daddr & 0xffff;
	sum += htons(ntohs(iph->tot_len) - 20);

	return sum;
}

static inline uint32_t proto_ipv6_header_checksum(struct ipv6hdr *ip6h,
						  int proto)
{
	uint32_t sum = htons(proto) + ip6h->payload_len;

	sum += sum_16b(&ip6h->saddr, sizeof(ip6h->saddr));
	sum += sum_16b(&ip6h->daddr, sizeof(ip6h->daddr));

	return sum;
}
#endif /* IP_H */

debug log:

solving 87cb8dd21d2e ...
found 87cb8dd21d2e in https://archives.passt.top/passt-dev/20240202141151.3762941-8-lvivier@redhat.com/
found ff7902c45a95 in https://archives.passt.top/passt-dev/20240202141151.3762941-7-lvivier@redhat.com/
found b2e08bc049f3 in https://archives.passt.top/passt-dev/20240202141151.3762941-6-lvivier@redhat.com/ ||
	https://archives.passt.top/passt-dev/20240214085628.210783-6-lvivier@redhat.com/ ||
	https://archives.passt.top/passt-dev/20240217150725.661467-6-lvivier@redhat.com/

applying [1/3] https://archives.passt.top/passt-dev/20240202141151.3762941-6-lvivier@redhat.com/
diff --git a/ip.h b/ip.h
new file mode 100644
index 000000000000..b2e08bc049f3

Checking patch ip.h...
Applied patch ip.h cleanly.

skipping https://archives.passt.top/passt-dev/20240214085628.210783-6-lvivier@redhat.com/ for b2e08bc049f3
skipping https://archives.passt.top/passt-dev/20240217150725.661467-6-lvivier@redhat.com/ for b2e08bc049f3
index at:
100644 b2e08bc049f353fc209355e654a3668ba38915c2	ip.h

applying [2/3] https://archives.passt.top/passt-dev/20240202141151.3762941-7-lvivier@redhat.com/
diff --git a/ip.h b/ip.h
index b2e08bc049f3..ff7902c45a95 100644


applying [3/3] https://archives.passt.top/passt-dev/20240202141151.3762941-8-lvivier@redhat.com/
diff --git a/ip.h b/ip.h
index ff7902c45a95..87cb8dd21d2e 100644

Checking patch ip.h...
Applied patch ip.h cleanly.
Checking patch ip.h...
Applied patch ip.h cleanly.

index at:
100644 87cb8dd21d2e9ad67103b00d38aed152353c9ea0	ip.h

Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).