xref: /openbmc/linux/include/net/flow.h (revision b95e6830)
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
41806566ccSDavid S. Miller 	__u32	flowic_secid;
42622ec2c9SLorenzo Colitti 	kuid_t  flowic_uid;
4324ba1440Swenxu 	__u32		flowic_multipath_hash;
44*b95e6830SEric Dumazet 	struct flowi_tunnel flowic_tun_key;
45806566ccSDavid S. Miller };
46806566ccSDavid S. Miller 
4708704bcbSDavid S. Miller union flowi_uli {
4808704bcbSDavid S. Miller 	struct {
4908704bcbSDavid S. Miller 		__be16	dport;
509b12c75bSDavid S. Miller 		__be16	sport;
5108704bcbSDavid S. Miller 	} ports;
5208704bcbSDavid S. Miller 
5308704bcbSDavid S. Miller 	struct {
5408704bcbSDavid S. Miller 		__u8	type;
5508704bcbSDavid S. Miller 		__u8	code;
5608704bcbSDavid S. Miller 	} icmpt;
5708704bcbSDavid S. Miller 
5808704bcbSDavid S. Miller 	__be32		gre_key;
5908704bcbSDavid S. Miller 
6008704bcbSDavid S. Miller 	struct {
6108704bcbSDavid S. Miller 		__u8	type;
6208704bcbSDavid S. Miller 	} mht;
6308704bcbSDavid S. Miller };
6408704bcbSDavid S. Miller 
6556bb8059SDavid S. Miller struct flowi4 {
66806566ccSDavid S. Miller 	struct flowi_common	__fl_common;
6722bd5b9bSDavid S. Miller #define flowi4_oif		__fl_common.flowic_oif
6822bd5b9bSDavid S. Miller #define flowi4_iif		__fl_common.flowic_iif
6940867d74SDavid Ahern #define flowi4_l3mdev		__fl_common.flowic_l3mdev
7022bd5b9bSDavid S. Miller #define flowi4_mark		__fl_common.flowic_mark
7122bd5b9bSDavid S. Miller #define flowi4_tos		__fl_common.flowic_tos
7222bd5b9bSDavid S. Miller #define flowi4_scope		__fl_common.flowic_scope
7322bd5b9bSDavid S. Miller #define flowi4_proto		__fl_common.flowic_proto
7422bd5b9bSDavid S. Miller #define flowi4_flags		__fl_common.flowic_flags
7522bd5b9bSDavid S. Miller #define flowi4_secid		__fl_common.flowic_secid
761b7179d3SThomas Graf #define flowi4_tun_key		__fl_common.flowic_tun_key
77622ec2c9SLorenzo Colitti #define flowi4_uid		__fl_common.flowic_uid
7824ba1440Swenxu #define flowi4_multipath_hash	__fl_common.flowic_multipath_hash
7984f9307cSEric Dumazet 
8084f9307cSEric Dumazet 	/* (saddr,daddr) must be grouped, same order as in IP header */
81f2c3fe24SAl Viro 	__be32			saddr;
8284f9307cSEric Dumazet 	__be32			daddr;
8384f9307cSEric Dumazet 
8456bb8059SDavid S. Miller 	union flowi_uli		uli;
859cce96dfSDavid S. Miller #define fl4_sport		uli.ports.sport
869cce96dfSDavid S. Miller #define fl4_dport		uli.ports.dport
879cce96dfSDavid S. Miller #define fl4_icmp_type		uli.icmpt.type
889cce96dfSDavid S. Miller #define fl4_icmp_code		uli.icmpt.code
899cce96dfSDavid S. Miller #define fl4_mh_type		uli.mht.type
909cce96dfSDavid S. Miller #define fl4_gre_key		uli.gre_key
91728871bcSDavid Ward } __attribute__((__aligned__(BITS_PER_LONG/8)));
921da177e4SLinus 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)9383229aa5SDavid S. Miller static inline void flowi4_init_output(struct flowi4 *fl4, int oif,
9483229aa5SDavid S. Miller 				      __u32 mark, __u8 tos, __u8 scope,
9583229aa5SDavid S. Miller 				      __u8 proto, __u8 flags,
9683229aa5SDavid S. Miller 				      __be32 daddr, __be32 saddr,
97e2d118a1SLorenzo Colitti 				      __be16 dport, __be16 sport,
98e2d118a1SLorenzo Colitti 				      kuid_t uid)
9983229aa5SDavid S. Miller {
10083229aa5SDavid S. Miller 	fl4->flowi4_oif = oif;
1016a662719SCong Wang 	fl4->flowi4_iif = LOOPBACK_IFINDEX;
10240867d74SDavid Ahern 	fl4->flowi4_l3mdev = 0;
10383229aa5SDavid S. Miller 	fl4->flowi4_mark = mark;
10483229aa5SDavid S. Miller 	fl4->flowi4_tos = tos;
10583229aa5SDavid S. Miller 	fl4->flowi4_scope = scope;
10683229aa5SDavid S. Miller 	fl4->flowi4_proto = proto;
10783229aa5SDavid S. Miller 	fl4->flowi4_flags = flags;
10883229aa5SDavid S. Miller 	fl4->flowi4_secid = 0;
1091b7179d3SThomas Graf 	fl4->flowi4_tun_key.tun_id = 0;
110e2d118a1SLorenzo Colitti 	fl4->flowi4_uid = uid;
11183229aa5SDavid S. Miller 	fl4->daddr = daddr;
11283229aa5SDavid S. Miller 	fl4->saddr = saddr;
11383229aa5SDavid S. Miller 	fl4->fl4_dport = dport;
1149b12c75bSDavid S. Miller 	fl4->fl4_sport = sport;
1151869e226SDavid Ahern 	fl4->flowi4_multipath_hash = 0;
11683229aa5SDavid S. Miller }
117e6b45241SJulian Anastasov 
118e6b45241SJulian Anastasov /* Reset some input parameters after previous lookup */
flowi4_update_output(struct flowi4 * fl4,int oif,__be32 daddr,__be32 saddr)1193f06760cSGuillaume Nault static inline void flowi4_update_output(struct flowi4 *fl4, int oif,
120e6b45241SJulian Anastasov 					__be32 daddr, __be32 saddr)
121e6b45241SJulian Anastasov {
122e6b45241SJulian Anastasov 	fl4->flowi4_oif = oif;
123e6b45241SJulian Anastasov 	fl4->daddr = daddr;
124e6b45241SJulian Anastasov 	fl4->saddr = saddr;
125e6b45241SJulian Anastasov }
12683229aa5SDavid S. Miller 
12783229aa5SDavid S. Miller 
12856bb8059SDavid S. Miller struct flowi6 {
12956bb8059SDavid S. Miller 	struct flowi_common	__fl_common;
1302032656eSDavid S. Miller #define flowi6_oif		__fl_common.flowic_oif
1312032656eSDavid S. Miller #define flowi6_iif		__fl_common.flowic_iif
13240867d74SDavid Ahern #define flowi6_l3mdev		__fl_common.flowic_l3mdev
1332032656eSDavid S. Miller #define flowi6_mark		__fl_common.flowic_mark
1342032656eSDavid S. Miller #define flowi6_scope		__fl_common.flowic_scope
1352032656eSDavid S. Miller #define flowi6_proto		__fl_common.flowic_proto
1362032656eSDavid S. Miller #define flowi6_flags		__fl_common.flowic_flags
1372032656eSDavid S. Miller #define flowi6_secid		__fl_common.flowic_secid
138904af04dSJiri Benc #define flowi6_tun_key		__fl_common.flowic_tun_key
139622ec2c9SLorenzo Colitti #define flowi6_uid		__fl_common.flowic_uid
1401da177e4SLinus Torvalds 	struct in6_addr		daddr;
1411da177e4SLinus Torvalds 	struct in6_addr		saddr;
14269716a2bSDaniel Borkmann 	/* Note: flowi6_tos is encoded in flowlabel, too. */
14390bcaf7bSAl Viro 	__be32			flowlabel;
14456bb8059SDavid S. Miller 	union flowi_uli		uli;
1451958b856SDavid S. Miller #define fl6_sport		uli.ports.sport
1461958b856SDavid S. Miller #define fl6_dport		uli.ports.dport
1471958b856SDavid S. Miller #define fl6_icmp_type		uli.icmpt.type
1481958b856SDavid S. Miller #define fl6_icmp_code		uli.icmpt.code
1491958b856SDavid S. Miller #define fl6_mh_type		uli.mht.type
1501958b856SDavid S. Miller #define fl6_gre_key		uli.gre_key
15129825717SJakub Sitnicki 	__u32			mp_hash;
152728871bcSDavid Ward } __attribute__((__aligned__(BITS_PER_LONG/8)));
1531da177e4SLinus Torvalds 
15456bb8059SDavid S. Miller struct flowi {
15556bb8059SDavid S. Miller 	union {
15656bb8059SDavid S. Miller 		struct flowi_common	__fl_common;
15756bb8059SDavid S. Miller 		struct flowi4		ip4;
15856bb8059SDavid S. Miller 		struct flowi6		ip6;
15956bb8059SDavid S. Miller 	} u;
16056bb8059SDavid S. Miller #define flowi_oif	u.__fl_common.flowic_oif
16156bb8059SDavid S. Miller #define flowi_iif	u.__fl_common.flowic_iif
16240867d74SDavid Ahern #define flowi_l3mdev	u.__fl_common.flowic_l3mdev
16356bb8059SDavid S. Miller #define flowi_mark	u.__fl_common.flowic_mark
16456bb8059SDavid S. Miller #define flowi_tos	u.__fl_common.flowic_tos
16556bb8059SDavid S. Miller #define flowi_scope	u.__fl_common.flowic_scope
16656bb8059SDavid S. Miller #define flowi_proto	u.__fl_common.flowic_proto
16756bb8059SDavid S. Miller #define flowi_flags	u.__fl_common.flowic_flags
16856bb8059SDavid S. Miller #define flowi_secid	u.__fl_common.flowic_secid
1691b7179d3SThomas Graf #define flowi_tun_key	u.__fl_common.flowic_tun_key
170622ec2c9SLorenzo Colitti #define flowi_uid	u.__fl_common.flowic_uid
1711da177e4SLinus Torvalds } __attribute__((__aligned__(BITS_PER_LONG/8)));
1721da177e4SLinus Torvalds 
flowi4_to_flowi(struct flowi4 * fl4)17359b1a94cSDavid S. Miller static inline struct flowi *flowi4_to_flowi(struct flowi4 *fl4)
17459b1a94cSDavid S. Miller {
17559b1a94cSDavid S. Miller 	return container_of(fl4, struct flowi, u.ip4);
17659b1a94cSDavid S. Miller }
17759b1a94cSDavid S. Miller 
flowi4_to_flowi_common(struct flowi4 * fl4)1783df98d79SPaul Moore static inline struct flowi_common *flowi4_to_flowi_common(struct flowi4 *fl4)
1793df98d79SPaul Moore {
180b9edbfe1STetsuo Handa 	return &(fl4->__fl_common);
1813df98d79SPaul Moore }
1823df98d79SPaul Moore 
flowi6_to_flowi(struct flowi6 * fl6)18359b1a94cSDavid S. Miller static inline struct flowi *flowi6_to_flowi(struct flowi6 *fl6)
18459b1a94cSDavid S. Miller {
18559b1a94cSDavid S. Miller 	return container_of(fl6, struct flowi, u.ip6);
18659b1a94cSDavid S. Miller }
18759b1a94cSDavid S. Miller 
flowi6_to_flowi_common(struct flowi6 * fl6)1883df98d79SPaul Moore static inline struct flowi_common *flowi6_to_flowi_common(struct flowi6 *fl6)
1893df98d79SPaul Moore {
190b9edbfe1STetsuo Handa 	return &(fl6->__fl_common);
1913df98d79SPaul Moore }
1923df98d79SPaul Moore 
19320a17bf6SDavid S. Miller __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys);
194c6cc1ca7STom Herbert 
1951da177e4SLinus Torvalds #endif
196