18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci *	Generic internet FLOW.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#ifndef _NET_FLOW_H
98c2ecf20Sopenharmony_ci#define _NET_FLOW_H
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/socket.h>
128c2ecf20Sopenharmony_ci#include <linux/in6.h>
138c2ecf20Sopenharmony_ci#include <linux/atomic.h>
148c2ecf20Sopenharmony_ci#include <net/flow_dissector.h>
158c2ecf20Sopenharmony_ci#include <linux/uidgid.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci/*
188c2ecf20Sopenharmony_ci * ifindex generation is per-net namespace, and loopback is
198c2ecf20Sopenharmony_ci * always the 1st device in ns (see net_dev_init), thus any
208c2ecf20Sopenharmony_ci * loopback device should get ifindex 1
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define LOOPBACK_IFINDEX	1
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistruct flowi_tunnel {
268c2ecf20Sopenharmony_ci	__be64			tun_id;
278c2ecf20Sopenharmony_ci};
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistruct flowi_common {
308c2ecf20Sopenharmony_ci	int	flowic_oif;
318c2ecf20Sopenharmony_ci	int	flowic_iif;
328c2ecf20Sopenharmony_ci	__u32	flowic_mark;
338c2ecf20Sopenharmony_ci	__u8	flowic_tos;
348c2ecf20Sopenharmony_ci	__u8	flowic_scope;
358c2ecf20Sopenharmony_ci	__u8	flowic_proto;
368c2ecf20Sopenharmony_ci	__u8	flowic_flags;
378c2ecf20Sopenharmony_ci#define FLOWI_FLAG_ANYSRC		0x01
388c2ecf20Sopenharmony_ci#define FLOWI_FLAG_KNOWN_NH		0x02
398c2ecf20Sopenharmony_ci#define FLOWI_FLAG_SKIP_NH_OIF		0x04
408c2ecf20Sopenharmony_ci	__u32	flowic_secid;
418c2ecf20Sopenharmony_ci	kuid_t  flowic_uid;
428c2ecf20Sopenharmony_ci	__u32		flowic_multipath_hash;
438c2ecf20Sopenharmony_ci	struct flowi_tunnel flowic_tun_key;
448c2ecf20Sopenharmony_ci};
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ciunion flowi_uli {
478c2ecf20Sopenharmony_ci	struct {
488c2ecf20Sopenharmony_ci		__be16	dport;
498c2ecf20Sopenharmony_ci		__be16	sport;
508c2ecf20Sopenharmony_ci	} ports;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	struct {
538c2ecf20Sopenharmony_ci		__u8	type;
548c2ecf20Sopenharmony_ci		__u8	code;
558c2ecf20Sopenharmony_ci	} icmpt;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	__be32		spi;
588c2ecf20Sopenharmony_ci	__be32		gre_key;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	struct {
618c2ecf20Sopenharmony_ci		__u8	type;
628c2ecf20Sopenharmony_ci	} mht;
638c2ecf20Sopenharmony_ci};
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistruct flowi4 {
668c2ecf20Sopenharmony_ci	struct flowi_common	__fl_common;
678c2ecf20Sopenharmony_ci#define flowi4_oif		__fl_common.flowic_oif
688c2ecf20Sopenharmony_ci#define flowi4_iif		__fl_common.flowic_iif
698c2ecf20Sopenharmony_ci#define flowi4_mark		__fl_common.flowic_mark
708c2ecf20Sopenharmony_ci#define flowi4_tos		__fl_common.flowic_tos
718c2ecf20Sopenharmony_ci#define flowi4_scope		__fl_common.flowic_scope
728c2ecf20Sopenharmony_ci#define flowi4_proto		__fl_common.flowic_proto
738c2ecf20Sopenharmony_ci#define flowi4_flags		__fl_common.flowic_flags
748c2ecf20Sopenharmony_ci#define flowi4_secid		__fl_common.flowic_secid
758c2ecf20Sopenharmony_ci#define flowi4_tun_key		__fl_common.flowic_tun_key
768c2ecf20Sopenharmony_ci#define flowi4_uid		__fl_common.flowic_uid
778c2ecf20Sopenharmony_ci#define flowi4_multipath_hash	__fl_common.flowic_multipath_hash
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	/* (saddr,daddr) must be grouped, same order as in IP header */
808c2ecf20Sopenharmony_ci	__be32			saddr;
818c2ecf20Sopenharmony_ci	__be32			daddr;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	union flowi_uli		uli;
848c2ecf20Sopenharmony_ci#define fl4_sport		uli.ports.sport
858c2ecf20Sopenharmony_ci#define fl4_dport		uli.ports.dport
868c2ecf20Sopenharmony_ci#define fl4_icmp_type		uli.icmpt.type
878c2ecf20Sopenharmony_ci#define fl4_icmp_code		uli.icmpt.code
888c2ecf20Sopenharmony_ci#define fl4_ipsec_spi		uli.spi
898c2ecf20Sopenharmony_ci#define fl4_mh_type		uli.mht.type
908c2ecf20Sopenharmony_ci#define fl4_gre_key		uli.gre_key
918c2ecf20Sopenharmony_ci} __attribute__((__aligned__(BITS_PER_LONG/8)));
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic inline void flowi4_init_output(struct flowi4 *fl4, int oif,
948c2ecf20Sopenharmony_ci				      __u32 mark, __u8 tos, __u8 scope,
958c2ecf20Sopenharmony_ci				      __u8 proto, __u8 flags,
968c2ecf20Sopenharmony_ci				      __be32 daddr, __be32 saddr,
978c2ecf20Sopenharmony_ci				      __be16 dport, __be16 sport,
988c2ecf20Sopenharmony_ci				      kuid_t uid)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	fl4->flowi4_oif = oif;
1018c2ecf20Sopenharmony_ci	fl4->flowi4_iif = LOOPBACK_IFINDEX;
1028c2ecf20Sopenharmony_ci	fl4->flowi4_mark = mark;
1038c2ecf20Sopenharmony_ci	fl4->flowi4_tos = tos;
1048c2ecf20Sopenharmony_ci	fl4->flowi4_scope = scope;
1058c2ecf20Sopenharmony_ci	fl4->flowi4_proto = proto;
1068c2ecf20Sopenharmony_ci	fl4->flowi4_flags = flags;
1078c2ecf20Sopenharmony_ci	fl4->flowi4_secid = 0;
1088c2ecf20Sopenharmony_ci	fl4->flowi4_tun_key.tun_id = 0;
1098c2ecf20Sopenharmony_ci	fl4->flowi4_uid = uid;
1108c2ecf20Sopenharmony_ci	fl4->daddr = daddr;
1118c2ecf20Sopenharmony_ci	fl4->saddr = saddr;
1128c2ecf20Sopenharmony_ci	fl4->fl4_dport = dport;
1138c2ecf20Sopenharmony_ci	fl4->fl4_sport = sport;
1148c2ecf20Sopenharmony_ci	fl4->flowi4_multipath_hash = 0;
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci/* Reset some input parameters after previous lookup */
1188c2ecf20Sopenharmony_cistatic inline void flowi4_update_output(struct flowi4 *fl4, int oif, __u8 tos,
1198c2ecf20Sopenharmony_ci					__be32 daddr, __be32 saddr)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	fl4->flowi4_oif = oif;
1228c2ecf20Sopenharmony_ci	fl4->flowi4_tos = tos;
1238c2ecf20Sopenharmony_ci	fl4->daddr = daddr;
1248c2ecf20Sopenharmony_ci	fl4->saddr = saddr;
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cistruct flowi6 {
1298c2ecf20Sopenharmony_ci	struct flowi_common	__fl_common;
1308c2ecf20Sopenharmony_ci#define flowi6_oif		__fl_common.flowic_oif
1318c2ecf20Sopenharmony_ci#define flowi6_iif		__fl_common.flowic_iif
1328c2ecf20Sopenharmony_ci#define flowi6_mark		__fl_common.flowic_mark
1338c2ecf20Sopenharmony_ci#define flowi6_scope		__fl_common.flowic_scope
1348c2ecf20Sopenharmony_ci#define flowi6_proto		__fl_common.flowic_proto
1358c2ecf20Sopenharmony_ci#define flowi6_flags		__fl_common.flowic_flags
1368c2ecf20Sopenharmony_ci#define flowi6_secid		__fl_common.flowic_secid
1378c2ecf20Sopenharmony_ci#define flowi6_tun_key		__fl_common.flowic_tun_key
1388c2ecf20Sopenharmony_ci#define flowi6_uid		__fl_common.flowic_uid
1398c2ecf20Sopenharmony_ci	struct in6_addr		daddr;
1408c2ecf20Sopenharmony_ci	struct in6_addr		saddr;
1418c2ecf20Sopenharmony_ci	/* Note: flowi6_tos is encoded in flowlabel, too. */
1428c2ecf20Sopenharmony_ci	__be32			flowlabel;
1438c2ecf20Sopenharmony_ci	union flowi_uli		uli;
1448c2ecf20Sopenharmony_ci#define fl6_sport		uli.ports.sport
1458c2ecf20Sopenharmony_ci#define fl6_dport		uli.ports.dport
1468c2ecf20Sopenharmony_ci#define fl6_icmp_type		uli.icmpt.type
1478c2ecf20Sopenharmony_ci#define fl6_icmp_code		uli.icmpt.code
1488c2ecf20Sopenharmony_ci#define fl6_ipsec_spi		uli.spi
1498c2ecf20Sopenharmony_ci#define fl6_mh_type		uli.mht.type
1508c2ecf20Sopenharmony_ci#define fl6_gre_key		uli.gre_key
1518c2ecf20Sopenharmony_ci	__u32			mp_hash;
1528c2ecf20Sopenharmony_ci} __attribute__((__aligned__(BITS_PER_LONG/8)));
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistruct flowi {
1558c2ecf20Sopenharmony_ci	union {
1568c2ecf20Sopenharmony_ci		struct flowi_common	__fl_common;
1578c2ecf20Sopenharmony_ci		struct flowi4		ip4;
1588c2ecf20Sopenharmony_ci		struct flowi6		ip6;
1598c2ecf20Sopenharmony_ci	} u;
1608c2ecf20Sopenharmony_ci#define flowi_oif	u.__fl_common.flowic_oif
1618c2ecf20Sopenharmony_ci#define flowi_iif	u.__fl_common.flowic_iif
1628c2ecf20Sopenharmony_ci#define flowi_mark	u.__fl_common.flowic_mark
1638c2ecf20Sopenharmony_ci#define flowi_tos	u.__fl_common.flowic_tos
1648c2ecf20Sopenharmony_ci#define flowi_scope	u.__fl_common.flowic_scope
1658c2ecf20Sopenharmony_ci#define flowi_proto	u.__fl_common.flowic_proto
1668c2ecf20Sopenharmony_ci#define flowi_flags	u.__fl_common.flowic_flags
1678c2ecf20Sopenharmony_ci#define flowi_secid	u.__fl_common.flowic_secid
1688c2ecf20Sopenharmony_ci#define flowi_tun_key	u.__fl_common.flowic_tun_key
1698c2ecf20Sopenharmony_ci#define flowi_uid	u.__fl_common.flowic_uid
1708c2ecf20Sopenharmony_ci} __attribute__((__aligned__(BITS_PER_LONG/8)));
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic inline struct flowi *flowi4_to_flowi(struct flowi4 *fl4)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	return container_of(fl4, struct flowi, u.ip4);
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistatic inline struct flowi_common *flowi4_to_flowi_common(struct flowi4 *fl4)
1788c2ecf20Sopenharmony_ci{
1798c2ecf20Sopenharmony_ci	return &(flowi4_to_flowi(fl4)->u.__fl_common);
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_cistatic inline struct flowi *flowi6_to_flowi(struct flowi6 *fl6)
1838c2ecf20Sopenharmony_ci{
1848c2ecf20Sopenharmony_ci	return container_of(fl6, struct flowi, u.ip6);
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic inline struct flowi_common *flowi6_to_flowi_common(struct flowi6 *fl6)
1888c2ecf20Sopenharmony_ci{
1898c2ecf20Sopenharmony_ci	return &(flowi6_to_flowi(fl6)->u.__fl_common);
1908c2ecf20Sopenharmony_ci}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci__u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys);
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci#endif
195