^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Generic internet FLOW.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #ifndef _NET_FLOW_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #define _NET_FLOW_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/in6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <net/flow_dissector.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/uidgid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * ifindex generation is per-net namespace, and loopback is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * always the 1st device in ns (see net_dev_init), thus any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * loopback device should get ifindex 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define LOOPBACK_IFINDEX 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct flowi_tunnel {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) __be64 tun_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct flowi_common {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) int flowic_oif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) int flowic_iif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) __u32 flowic_mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) __u8 flowic_tos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) __u8 flowic_scope;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) __u8 flowic_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) __u8 flowic_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define FLOWI_FLAG_ANYSRC 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define FLOWI_FLAG_KNOWN_NH 0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define FLOWI_FLAG_SKIP_NH_OIF 0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) __u32 flowic_secid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) kuid_t flowic_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct flowi_tunnel flowic_tun_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) __u32 flowic_multipath_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) union flowi_uli {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) __be16 dport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) __be16 sport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) } ports;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) __u8 type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) __u8 code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) } icmpt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) __le16 dport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) __le16 sport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) } dnports;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) __be32 spi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) __be32 gre_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) __u8 type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) } mht;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct flowi4 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct flowi_common __fl_common;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #define flowi4_oif __fl_common.flowic_oif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #define flowi4_iif __fl_common.flowic_iif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #define flowi4_mark __fl_common.flowic_mark
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #define flowi4_tos __fl_common.flowic_tos
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #define flowi4_scope __fl_common.flowic_scope
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #define flowi4_proto __fl_common.flowic_proto
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #define flowi4_flags __fl_common.flowic_flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #define flowi4_secid __fl_common.flowic_secid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #define flowi4_tun_key __fl_common.flowic_tun_key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #define flowi4_uid __fl_common.flowic_uid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) #define flowi4_multipath_hash __fl_common.flowic_multipath_hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /* (saddr,daddr) must be grouped, same order as in IP header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) __be32 saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) __be32 daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) union flowi_uli uli;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) #define fl4_sport uli.ports.sport
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) #define fl4_dport uli.ports.dport
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) #define fl4_icmp_type uli.icmpt.type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) #define fl4_icmp_code uli.icmpt.code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) #define fl4_ipsec_spi uli.spi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) #define fl4_mh_type uli.mht.type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #define fl4_gre_key uli.gre_key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) } __attribute__((__aligned__(BITS_PER_LONG/8)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static inline void flowi4_init_output(struct flowi4 *fl4, int oif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) __u32 mark, __u8 tos, __u8 scope,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) __u8 proto, __u8 flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) __be32 daddr, __be32 saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) __be16 dport, __be16 sport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) kuid_t uid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) fl4->flowi4_oif = oif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) fl4->flowi4_iif = LOOPBACK_IFINDEX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) fl4->flowi4_mark = mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) fl4->flowi4_tos = tos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) fl4->flowi4_scope = scope;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) fl4->flowi4_proto = proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) fl4->flowi4_flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) fl4->flowi4_secid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) fl4->flowi4_tun_key.tun_id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) fl4->flowi4_uid = uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) fl4->daddr = daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) fl4->saddr = saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) fl4->fl4_dport = dport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) fl4->fl4_sport = sport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) fl4->flowi4_multipath_hash = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) /* Reset some input parameters after previous lookup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static inline void flowi4_update_output(struct flowi4 *fl4, int oif, __u8 tos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) __be32 daddr, __be32 saddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) fl4->flowi4_oif = oif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) fl4->flowi4_tos = tos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) fl4->daddr = daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) fl4->saddr = saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct flowi6 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct flowi_common __fl_common;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) #define flowi6_oif __fl_common.flowic_oif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) #define flowi6_iif __fl_common.flowic_iif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) #define flowi6_mark __fl_common.flowic_mark
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) #define flowi6_scope __fl_common.flowic_scope
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) #define flowi6_proto __fl_common.flowic_proto
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) #define flowi6_flags __fl_common.flowic_flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) #define flowi6_secid __fl_common.flowic_secid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) #define flowi6_tun_key __fl_common.flowic_tun_key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) #define flowi6_uid __fl_common.flowic_uid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) struct in6_addr daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) struct in6_addr saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) /* Note: flowi6_tos is encoded in flowlabel, too. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) __be32 flowlabel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) union flowi_uli uli;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) #define fl6_sport uli.ports.sport
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) #define fl6_dport uli.ports.dport
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) #define fl6_icmp_type uli.icmpt.type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) #define fl6_icmp_code uli.icmpt.code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) #define fl6_ipsec_spi uli.spi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) #define fl6_mh_type uli.mht.type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) #define fl6_gre_key uli.gre_key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) __u32 mp_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) } __attribute__((__aligned__(BITS_PER_LONG/8)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) struct flowidn {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) struct flowi_common __fl_common;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) #define flowidn_oif __fl_common.flowic_oif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) #define flowidn_iif __fl_common.flowic_iif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) #define flowidn_mark __fl_common.flowic_mark
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) #define flowidn_scope __fl_common.flowic_scope
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) #define flowidn_proto __fl_common.flowic_proto
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) #define flowidn_flags __fl_common.flowic_flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) __le16 daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) __le16 saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) union flowi_uli uli;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) #define fld_sport uli.ports.sport
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) #define fld_dport uli.ports.dport
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) } __attribute__((__aligned__(BITS_PER_LONG/8)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct flowi {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) struct flowi_common __fl_common;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) struct flowi4 ip4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) struct flowi6 ip6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) struct flowidn dn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) } u;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) #define flowi_oif u.__fl_common.flowic_oif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) #define flowi_iif u.__fl_common.flowic_iif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) #define flowi_mark u.__fl_common.flowic_mark
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) #define flowi_tos u.__fl_common.flowic_tos
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) #define flowi_scope u.__fl_common.flowic_scope
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) #define flowi_proto u.__fl_common.flowic_proto
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) #define flowi_flags u.__fl_common.flowic_flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) #define flowi_secid u.__fl_common.flowic_secid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) #define flowi_tun_key u.__fl_common.flowic_tun_key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) #define flowi_uid u.__fl_common.flowic_uid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) } __attribute__((__aligned__(BITS_PER_LONG/8)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static inline struct flowi *flowi4_to_flowi(struct flowi4 *fl4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return container_of(fl4, struct flowi, u.ip4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static inline struct flowi *flowi6_to_flowi(struct flowi6 *fl6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return container_of(fl6, struct flowi, u.ip6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static inline struct flowi *flowidn_to_flowi(struct flowidn *fldn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) return container_of(fldn, struct flowi, u.dn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) #endif