^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) #ifndef MPLS_INTERNAL_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define MPLS_INTERNAL_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <net/mpls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) /* put a reasonable limit on the number of labels
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * we will accept from userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #define MAX_NEW_LABELS 30
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) struct mpls_entry_decoded {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) u32 label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) u8 ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) u8 tc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) u8 bos;
^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) struct mpls_pcpu_stats {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct mpls_link_stats stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct u64_stats_sync syncp;
^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) struct mpls_dev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) int input_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct mpls_pcpu_stats __percpu *stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct ctl_table_header *sysctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct rcu_head rcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #if BITS_PER_LONG == 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define MPLS_INC_STATS_LEN(mdev, len, pkts_field, bytes_field) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) __typeof__(*(mdev)->stats) *ptr = \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) raw_cpu_ptr((mdev)->stats); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) local_bh_disable(); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) u64_stats_update_begin(&ptr->syncp); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) ptr->stats.pkts_field++; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) ptr->stats.bytes_field += (len); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) u64_stats_update_end(&ptr->syncp); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) local_bh_enable(); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #define MPLS_INC_STATS(mdev, field) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) __typeof__(*(mdev)->stats) *ptr = \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) raw_cpu_ptr((mdev)->stats); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) local_bh_disable(); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) u64_stats_update_begin(&ptr->syncp); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) ptr->stats.field++; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) u64_stats_update_end(&ptr->syncp); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) local_bh_enable(); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #define MPLS_INC_STATS_LEN(mdev, len, pkts_field, bytes_field) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) this_cpu_inc((mdev)->stats->stats.pkts_field); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) this_cpu_add((mdev)->stats->stats.bytes_field, (len)); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #define MPLS_INC_STATS(mdev, field) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) this_cpu_inc((mdev)->stats->stats.field)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct sk_buff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #define LABEL_NOT_SPECIFIED (1 << 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /* This maximum ha length copied from the definition of struct neighbour */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #define VIA_ALEN_ALIGN sizeof(unsigned long)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #define MAX_VIA_ALEN (ALIGN(MAX_ADDR_LEN, VIA_ALEN_ALIGN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) enum mpls_payload_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) MPT_UNSPEC, /* IPv4 or IPv6 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) MPT_IPV4 = 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) MPT_IPV6 = 6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /* Other types not implemented:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * - Pseudo-wire with or without control word (RFC4385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * - GAL (RFC5586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct mpls_nh { /* next hop label forwarding entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct net_device __rcu *nh_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) /* nh_flags is accessed under RCU in the packet path; it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * modified handling netdev events with rtnl lock held
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) unsigned int nh_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) u8 nh_labels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) u8 nh_via_alen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) u8 nh_via_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) u8 nh_reserved1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) u32 nh_label[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /* offset of via from beginning of mpls_nh */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #define MPLS_NH_VIA_OFF(num_labels) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) ALIGN(sizeof(struct mpls_nh) + (num_labels) * sizeof(u32), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) VIA_ALEN_ALIGN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /* all nexthops within a route have the same size based on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * max number of labels and max via length across all nexthops
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) #define MPLS_NH_SIZE(num_labels, max_via_alen) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) (MPLS_NH_VIA_OFF((num_labels)) + \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) ALIGN((max_via_alen), VIA_ALEN_ALIGN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) enum mpls_ttl_propagation {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) MPLS_TTL_PROP_DEFAULT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) MPLS_TTL_PROP_ENABLED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) MPLS_TTL_PROP_DISABLED,
^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) /* The route, nexthops and vias are stored together in the same memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * block:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * +----------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * | mpls_route |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * +----------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * | mpls_nh 0 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * +----------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * | alignment padding | 4 bytes for odd number of labels
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * +----------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * | via[rt_max_alen] 0 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * +----------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * | alignment padding | via's aligned on sizeof(unsigned long)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * +----------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * | ... |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * +----------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * | mpls_nh n-1 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * +----------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * | via[rt_max_alen] n-1 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * +----------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) struct mpls_route { /* next hop label forwarding entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) struct rcu_head rt_rcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) u8 rt_protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) u8 rt_payload_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) u8 rt_max_alen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) u8 rt_ttl_propagate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) u8 rt_nhn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /* rt_nhn_alive is accessed under RCU in the packet path; it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * is modified handling netdev events with rtnl lock held
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) u8 rt_nhn_alive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) u8 rt_nh_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) u8 rt_via_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) u8 rt_reserved1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct mpls_nh rt_nh[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) #define for_nexthops(rt) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) int nhsel; struct mpls_nh *nh; u8 *__nh; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) for (nhsel = 0, nh = (rt)->rt_nh, __nh = (u8 *)((rt)->rt_nh); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) nhsel < (rt)->rt_nhn; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) __nh += rt->rt_nh_size, nh = (struct mpls_nh *)__nh, nhsel++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) #define change_nexthops(rt) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) int nhsel; struct mpls_nh *nh; u8 *__nh; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) for (nhsel = 0, nh = (struct mpls_nh *)((rt)->rt_nh), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) __nh = (u8 *)((rt)->rt_nh); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) nhsel < (rt)->rt_nhn; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) __nh += rt->rt_nh_size, nh = (struct mpls_nh *)__nh, nhsel++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) #define endfor_nexthops(rt) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static inline struct mpls_entry_decoded mpls_entry_decode(struct mpls_shim_hdr *hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) struct mpls_entry_decoded result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) unsigned entry = be32_to_cpu(hdr->label_stack_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) result.label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) result.ttl = (entry & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) result.tc = (entry & MPLS_LS_TC_MASK) >> MPLS_LS_TC_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) result.bos = (entry & MPLS_LS_S_MASK) >> MPLS_LS_S_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return rcu_dereference_rtnl(dev->mpls_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) int nla_put_labels(struct sk_buff *skb, int attrtype, u8 labels,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) const u32 label[]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) int nla_get_labels(const struct nlattr *nla, u8 max_labels, u8 *labels,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) u32 label[], struct netlink_ext_ack *extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) bool mpls_output_possible(const struct net_device *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) unsigned int mpls_dev_mtu(const struct net_device *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) void mpls_stats_inc_outucastpkts(struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) const struct sk_buff *skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) #endif /* MPLS_INTERNAL_H */