xref: /linux-6.15/include/net/flow.h (revision 2d300ce0)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *	Generic internet FLOW.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #ifndef _NET_FLOW_H
91da177e4SLinus Torvalds #define _NET_FLOW_H
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/in6.h>
1260063497SArun Sharma #include <linux/atomic.h>
1321bf7315SJakub Kicinski #include <linux/container_of.h>
14622ec2c9SLorenzo Colitti #include <linux/uidgid.h>
151da177e4SLinus Torvalds 
1621bf7315SJakub Kicinski struct flow_keys;
1721bf7315SJakub Kicinski 
186a662719SCong Wang /*
196a662719SCong Wang  * ifindex generation is per-net namespace, and loopback is
206a662719SCong Wang  * always the 1st device in ns (see net_dev_init), thus any
216a662719SCong Wang  * loopback device should get ifindex 1
226a662719SCong Wang  */
236a662719SCong Wang 
246a662719SCong Wang #define LOOPBACK_IFINDEX	1
256a662719SCong Wang 
261b7179d3SThomas Graf struct flowi_tunnel {
271b7179d3SThomas Graf 	__be64			tun_id;
281b7179d3SThomas Graf };
291b7179d3SThomas Graf 
30806566ccSDavid S. Miller struct flowi_common {
31806566ccSDavid S. Miller 	int	flowic_oif;
32806566ccSDavid S. Miller 	int	flowic_iif;
3340867d74SDavid Ahern 	int     flowic_l3mdev;
34806566ccSDavid S. Miller 	__u32	flowic_mark;
35806566ccSDavid S. Miller 	__u8	flowic_tos;
36806566ccSDavid S. Miller 	__u8	flowic_scope;
37806566ccSDavid S. Miller 	__u8	flowic_proto;
38806566ccSDavid S. Miller 	__u8	flowic_flags;
39fbef0a40SDavid S. Miller #define FLOWI_FLAG_ANYSRC		0x01
400e0d44abSSteffen Klassert #define FLOWI_FLAG_KNOWN_NH		0x02
41*2d300ce0SIdo Schimmel #define FLOWI_FLAG_L3MDEV_OIF		0x04
42806566ccSDavid S. Miller 	__u32	flowic_secid;
43622ec2c9SLorenzo Colitti 	kuid_t  flowic_uid;
4424ba1440Swenxu 	__u32		flowic_multipath_hash;
451726483bSEric Dumazet 	struct flowi_tunnel flowic_tun_key;
46806566ccSDavid S. Miller };
47806566ccSDavid S. Miller 
4808704bcbSDavid S. Miller union flowi_uli {
4908704bcbSDavid S. Miller 	struct {
5008704bcbSDavid S. Miller 		__be16	dport;
519b12c75bSDavid S. Miller 		__be16	sport;
5208704bcbSDavid S. Miller 	} ports;
5308704bcbSDavid S. Miller 
5408704bcbSDavid S. Miller 	struct {
5508704bcbSDavid S. Miller 		__u8	type;
5608704bcbSDavid S. Miller 		__u8	code;
5708704bcbSDavid S. Miller 	} icmpt;
5808704bcbSDavid S. Miller 
5908704bcbSDavid S. Miller 	__be32		gre_key;
6008704bcbSDavid S. Miller 
6108704bcbSDavid S. Miller 	struct {
6208704bcbSDavid S. Miller 		__u8	type;
6308704bcbSDavid S. Miller 	} mht;
6408704bcbSDavid S. Miller };
6508704bcbSDavid S. Miller 
6656bb8059SDavid S. Miller struct flowi4 {
67806566ccSDavid S. Miller 	struct flowi_common	__fl_common;
6822bd5b9bSDavid S. Miller #define flowi4_oif		__fl_common.flowic_oif
6922bd5b9bSDavid S. Miller #define flowi4_iif		__fl_common.flowic_iif
7040867d74SDavid Ahern #define flowi4_l3mdev		__fl_common.flowic_l3mdev
7122bd5b9bSDavid S. Miller #define flowi4_mark		__fl_common.flowic_mark
7222bd5b9bSDavid S. Miller #define flowi4_tos		__fl_common.flowic_tos
7322bd5b9bSDavid S. Miller #define flowi4_scope		__fl_common.flowic_scope
7422bd5b9bSDavid S. Miller #define flowi4_proto		__fl_common.flowic_proto
7522bd5b9bSDavid S. Miller #define flowi4_flags		__fl_common.flowic_flags
7622bd5b9bSDavid S. Miller #define flowi4_secid		__fl_common.flowic_secid
771b7179d3SThomas Graf #define flowi4_tun_key		__fl_common.flowic_tun_key
78622ec2c9SLorenzo Colitti #define flowi4_uid		__fl_common.flowic_uid
7924ba1440Swenxu #define flowi4_multipath_hash	__fl_common.flowic_multipath_hash
8084f9307cSEric Dumazet 
8184f9307cSEric Dumazet 	/* (saddr,daddr) must be grouped, same order as in IP header */
82f2c3fe24SAl Viro 	__be32			saddr;
8384f9307cSEric Dumazet 	__be32			daddr;
8484f9307cSEric Dumazet 
8556bb8059SDavid S. Miller 	union flowi_uli		uli;
869cce96dfSDavid S. Miller #define fl4_sport		uli.ports.sport
879cce96dfSDavid S. Miller #define fl4_dport		uli.ports.dport
889cce96dfSDavid S. Miller #define fl4_icmp_type		uli.icmpt.type
899cce96dfSDavid S. Miller #define fl4_icmp_code		uli.icmpt.code
909cce96dfSDavid S. Miller #define fl4_mh_type		uli.mht.type
919cce96dfSDavid S. Miller #define fl4_gre_key		uli.gre_key
92728871bcSDavid Ward } __attribute__((__aligned__(BITS_PER_LONG/8)));
931da177e4SLinus Torvalds 
flowi4_init_output(struct flowi4 * fl4,int oif,__u32 mark,__u8 tos,__u8 scope,__u8 proto,__u8 flags,__be32 daddr,__be32 saddr,__be16 dport,__be16 sport,kuid_t uid)9483229aa5SDavid S. Miller static inline void flowi4_init_output(struct flowi4 *fl4, int oif,
9583229aa5SDavid S. Miller 				      __u32 mark, __u8 tos, __u8 scope,
9683229aa5SDavid S. Miller 				      __u8 proto, __u8 flags,
9783229aa5SDavid S. Miller 				      __be32 daddr, __be32 saddr,
98e2d118a1SLorenzo Colitti 				      __be16 dport, __be16 sport,
99e2d118a1SLorenzo Colitti 				      kuid_t uid)
10083229aa5SDavid S. Miller {
10183229aa5SDavid S. Miller 	fl4->flowi4_oif = oif;
1026a662719SCong Wang 	fl4->flowi4_iif = LOOPBACK_IFINDEX;
10340867d74SDavid Ahern 	fl4->flowi4_l3mdev = 0;
10483229aa5SDavid S. Miller 	fl4->flowi4_mark = mark;
10583229aa5SDavid S. Miller 	fl4->flowi4_tos = tos;
10683229aa5SDavid S. Miller 	fl4->flowi4_scope = scope;
10783229aa5SDavid S. Miller 	fl4->flowi4_proto = proto;
10883229aa5SDavid S. Miller 	fl4->flowi4_flags = flags;
10983229aa5SDavid S. Miller 	fl4->flowi4_secid = 0;
1101b7179d3SThomas Graf 	fl4->flowi4_tun_key.tun_id = 0;
111e2d118a1SLorenzo Colitti 	fl4->flowi4_uid = uid;
11283229aa5SDavid S. Miller 	fl4->daddr = daddr;
11383229aa5SDavid S. Miller 	fl4->saddr = saddr;
11483229aa5SDavid S. Miller 	fl4->fl4_dport = dport;
1159b12c75bSDavid S. Miller 	fl4->fl4_sport = sport;
1161869e226SDavid Ahern 	fl4->flowi4_multipath_hash = 0;
11783229aa5SDavid S. Miller }
118e6b45241SJulian Anastasov 
119e6b45241SJulian Anastasov /* Reset some input parameters after previous lookup */
flowi4_update_output(struct flowi4 * fl4,int oif,__be32 daddr,__be32 saddr)1203f06760cSGuillaume Nault static inline void flowi4_update_output(struct flowi4 *fl4, int oif,
121e6b45241SJulian Anastasov 					__be32 daddr, __be32 saddr)
122e6b45241SJulian Anastasov {
123e6b45241SJulian Anastasov 	fl4->flowi4_oif = oif;
124e6b45241SJulian Anastasov 	fl4->daddr = daddr;
125e6b45241SJulian Anastasov 	fl4->saddr = saddr;
126e6b45241SJulian Anastasov }
12783229aa5SDavid S. Miller 
12883229aa5SDavid S. Miller 
12956bb8059SDavid S. Miller struct flowi6 {
13056bb8059SDavid S. Miller 	struct flowi_common	__fl_common;
1312032656eSDavid S. Miller #define flowi6_oif		__fl_common.flowic_oif
1322032656eSDavid S. Miller #define flowi6_iif		__fl_common.flowic_iif
13340867d74SDavid Ahern #define flowi6_l3mdev		__fl_common.flowic_l3mdev
1342032656eSDavid S. Miller #define flowi6_mark		__fl_common.flowic_mark
1352032656eSDavid S. Miller #define flowi6_scope		__fl_common.flowic_scope
1362032656eSDavid S. Miller #define flowi6_proto		__fl_common.flowic_proto
1372032656eSDavid S. Miller #define flowi6_flags		__fl_common.flowic_flags
1382032656eSDavid S. Miller #define flowi6_secid		__fl_common.flowic_secid
139904af04dSJiri Benc #define flowi6_tun_key		__fl_common.flowic_tun_key
140622ec2c9SLorenzo Colitti #define flowi6_uid		__fl_common.flowic_uid
1411da177e4SLinus Torvalds 	struct in6_addr		daddr;
1421da177e4SLinus Torvalds 	struct in6_addr		saddr;
14369716a2bSDaniel Borkmann 	/* Note: flowi6_tos is encoded in flowlabel, too. */
14490bcaf7bSAl Viro 	__be32			flowlabel;
14556bb8059SDavid S. Miller 	union flowi_uli		uli;
1461958b856SDavid S. Miller #define fl6_sport		uli.ports.sport
1471958b856SDavid S. Miller #define fl6_dport		uli.ports.dport
1481958b856SDavid S. Miller #define fl6_icmp_type		uli.icmpt.type
1491958b856SDavid S. Miller #define fl6_icmp_code		uli.icmpt.code
1501958b856SDavid S. Miller #define fl6_mh_type		uli.mht.type
1511958b856SDavid S. Miller #define fl6_gre_key		uli.gre_key
15229825717SJakub Sitnicki 	__u32			mp_hash;
153728871bcSDavid Ward } __attribute__((__aligned__(BITS_PER_LONG/8)));
1541da177e4SLinus Torvalds 
15556bb8059SDavid S. Miller struct flowi {
15656bb8059SDavid S. Miller 	union {
15756bb8059SDavid S. Miller 		struct flowi_common	__fl_common;
15856bb8059SDavid S. Miller 		struct flowi4		ip4;
15956bb8059SDavid S. Miller 		struct flowi6		ip6;
16056bb8059SDavid S. Miller 	} u;
16156bb8059SDavid S. Miller #define flowi_oif	u.__fl_common.flowic_oif
16256bb8059SDavid S. Miller #define flowi_iif	u.__fl_common.flowic_iif
16340867d74SDavid Ahern #define flowi_l3mdev	u.__fl_common.flowic_l3mdev
16456bb8059SDavid S. Miller #define flowi_mark	u.__fl_common.flowic_mark
16556bb8059SDavid S. Miller #define flowi_tos	u.__fl_common.flowic_tos
16656bb8059SDavid S. Miller #define flowi_scope	u.__fl_common.flowic_scope
16756bb8059SDavid S. Miller #define flowi_proto	u.__fl_common.flowic_proto
16856bb8059SDavid S. Miller #define flowi_flags	u.__fl_common.flowic_flags
16956bb8059SDavid S. Miller #define flowi_secid	u.__fl_common.flowic_secid
1701b7179d3SThomas Graf #define flowi_tun_key	u.__fl_common.flowic_tun_key
171622ec2c9SLorenzo Colitti #define flowi_uid	u.__fl_common.flowic_uid
1721da177e4SLinus Torvalds } __attribute__((__aligned__(BITS_PER_LONG/8)));
1731da177e4SLinus Torvalds 
flowi4_to_flowi(struct flowi4 * fl4)17459b1a94cSDavid S. Miller static inline struct flowi *flowi4_to_flowi(struct flowi4 *fl4)
17559b1a94cSDavid S. Miller {
17659b1a94cSDavid S. Miller 	return container_of(fl4, struct flowi, u.ip4);
17759b1a94cSDavid S. Miller }
17859b1a94cSDavid S. Miller 
flowi4_to_flowi_common(struct flowi4 * fl4)1793df98d79SPaul Moore static inline struct flowi_common *flowi4_to_flowi_common(struct flowi4 *fl4)
1803df98d79SPaul Moore {
181b9edbfe1STetsuo Handa 	return &(fl4->__fl_common);
1823df98d79SPaul Moore }
1833df98d79SPaul Moore 
flowi6_to_flowi(struct flowi6 * fl6)18459b1a94cSDavid S. Miller static inline struct flowi *flowi6_to_flowi(struct flowi6 *fl6)
18559b1a94cSDavid S. Miller {
18659b1a94cSDavid S. Miller 	return container_of(fl6, struct flowi, u.ip6);
18759b1a94cSDavid S. Miller }
18859b1a94cSDavid S. Miller 
flowi6_to_flowi_common(struct flowi6 * fl6)1893df98d79SPaul Moore static inline struct flowi_common *flowi6_to_flowi_common(struct flowi6 *fl6)
1903df98d79SPaul Moore {
191b9edbfe1STetsuo Handa 	return &(fl6->__fl_common);
1923df98d79SPaul Moore }
1933df98d79SPaul Moore 
19420a17bf6SDavid S. Miller __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys);
195c6cc1ca7STom Herbert 
1961da177e4SLinus Torvalds #endif
197