^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * SR-IPv6 implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Author:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * David Lebrun <david.lebrun@uclouvain.be>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <net/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <net/ip_tunnels.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <net/lwtunnel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <net/netevent.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <net/netns/generic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <net/ip6_fib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <net/route.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <net/seg6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/seg6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/seg6_iptunnel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <net/addrconf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <net/ip6_route.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <net/dst_cache.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #ifdef CONFIG_IPV6_SEG6_HMAC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <net/seg6_hmac.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static size_t seg6_lwt_headroom(struct seg6_iptunnel_encap *tuninfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) int head = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) switch (tuninfo->mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) case SEG6_IPTUN_MODE_INLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) case SEG6_IPTUN_MODE_ENCAP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) head = sizeof(struct ipv6hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) case SEG6_IPTUN_MODE_L2ENCAP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return ((tuninfo->srh->hdrlen + 1) << 3) + head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct seg6_lwt {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct dst_cache cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct seg6_iptunnel_encap tuninfo[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static inline struct seg6_lwt *seg6_lwt_lwtunnel(struct lwtunnel_state *lwt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return (struct seg6_lwt *)lwt->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static inline struct seg6_iptunnel_encap *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) seg6_encap_lwtunnel(struct lwtunnel_state *lwt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return seg6_lwt_lwtunnel(lwt)->tuninfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static const struct nla_policy seg6_iptunnel_policy[SEG6_IPTUNNEL_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) [SEG6_IPTUNNEL_SRH] = { .type = NLA_BINARY },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static int nla_put_srh(struct sk_buff *skb, int attrtype,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct seg6_iptunnel_encap *tuninfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct seg6_iptunnel_encap *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct nlattr *nla;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) len = SEG6_IPTUN_ENCAP_SIZE(tuninfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) nla = nla_reserve(skb, attrtype, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (!nla)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) data = nla_data(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) memcpy(data, tuninfo, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static void set_tun_src(struct net *net, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct in6_addr *daddr, struct in6_addr *saddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct seg6_pernet_data *sdata = seg6_pernet(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct in6_addr *tun_src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) tun_src = rcu_dereference(sdata->tun_src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (!ipv6_addr_any(tun_src)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) memcpy(saddr, tun_src, sizeof(struct in6_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) ipv6_dev_get_saddr(net, dev, daddr, IPV6_PREFER_SRC_PUBLIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /* Compute flowlabel for outer IPv6 header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static __be32 seg6_make_flowlabel(struct net *net, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct ipv6hdr *inner_hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) int do_flowlabel = net->ipv6.sysctl.seg6_flowlabel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) __be32 flowlabel = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) u32 hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (do_flowlabel > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) hash = skb_get_hash(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) hash = rol32(hash, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) flowlabel = (__force __be32)hash & IPV6_FLOWLABEL_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) } else if (!do_flowlabel && skb->protocol == htons(ETH_P_IPV6)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) flowlabel = ip6_flowlabel(inner_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return flowlabel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /* encapsulate an IPv6 packet within an outer IPv6 header with a given SRH */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) int seg6_do_srh_encap(struct sk_buff *skb, struct ipv6_sr_hdr *osrh, int proto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct dst_entry *dst = skb_dst(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct net *net = dev_net(dst->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct ipv6hdr *hdr, *inner_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct ipv6_sr_hdr *isrh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) int hdrlen, tot_len, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) __be32 flowlabel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) hdrlen = (osrh->hdrlen + 1) << 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) tot_len = hdrlen + sizeof(*hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) err = skb_cow_head(skb, tot_len + skb->mac_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) inner_hdr = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) flowlabel = seg6_make_flowlabel(net, skb, inner_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) skb_push(skb, tot_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) skb_mac_header_rebuild(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) hdr = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /* inherit tc, flowlabel and hlim
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * hlim will be decremented in ip6_forward() afterwards and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * decapsulation will overwrite inner hlim with outer hlim
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (skb->protocol == htons(ETH_P_IPV6)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) ip6_flow_hdr(hdr, ip6_tclass(ip6_flowinfo(inner_hdr)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) flowlabel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) hdr->hop_limit = inner_hdr->hop_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) ip6_flow_hdr(hdr, 0, flowlabel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) hdr->hop_limit = ip6_dst_hoplimit(skb_dst(skb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) /* the control block has been erased, so we have to set the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * iif once again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * We read the receiving interface index directly from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * skb->skb_iif as it is done in the IPv4 receiving path (i.e.:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * ip_rcv_core(...)).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) IP6CB(skb)->iif = skb->skb_iif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) hdr->nexthdr = NEXTHDR_ROUTING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) isrh = (void *)hdr + sizeof(*hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) memcpy(isrh, osrh, hdrlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) isrh->nexthdr = proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) hdr->daddr = isrh->segments[isrh->first_segment];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) set_tun_src(net, dst->dev, &hdr->daddr, &hdr->saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) #ifdef CONFIG_IPV6_SEG6_HMAC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (sr_has_hmac(isrh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) err = seg6_push_hmac(net, &hdr->saddr, isrh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) skb_postpush_rcsum(skb, hdr, tot_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) EXPORT_SYMBOL_GPL(seg6_do_srh_encap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) /* insert an SRH within an IPv6 packet, just after the IPv6 header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) int seg6_do_srh_inline(struct sk_buff *skb, struct ipv6_sr_hdr *osrh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) struct ipv6hdr *hdr, *oldhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct ipv6_sr_hdr *isrh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) int hdrlen, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) hdrlen = (osrh->hdrlen + 1) << 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) err = skb_cow_head(skb, hdrlen + skb->mac_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) oldhdr = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) skb_pull(skb, sizeof(struct ipv6hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) skb_postpull_rcsum(skb, skb_network_header(skb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) sizeof(struct ipv6hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) skb_push(skb, sizeof(struct ipv6hdr) + hdrlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) skb_mac_header_rebuild(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) hdr = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) memmove(hdr, oldhdr, sizeof(*hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) isrh = (void *)hdr + sizeof(*hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) memcpy(isrh, osrh, hdrlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) isrh->nexthdr = hdr->nexthdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) hdr->nexthdr = NEXTHDR_ROUTING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) isrh->segments[0] = hdr->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) hdr->daddr = isrh->segments[isrh->first_segment];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) #ifdef CONFIG_IPV6_SEG6_HMAC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (sr_has_hmac(isrh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) struct net *net = dev_net(skb_dst(skb)->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) err = seg6_push_hmac(net, &hdr->saddr, isrh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) skb_postpush_rcsum(skb, hdr, sizeof(struct ipv6hdr) + hdrlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) EXPORT_SYMBOL_GPL(seg6_do_srh_inline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) static int seg6_do_srh(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) struct dst_entry *dst = skb_dst(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) struct seg6_iptunnel_encap *tinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) int proto, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) tinfo = seg6_encap_lwtunnel(dst->lwtstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) switch (tinfo->mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) case SEG6_IPTUN_MODE_INLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (skb->protocol != htons(ETH_P_IPV6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) err = seg6_do_srh_inline(skb, tinfo->srh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) case SEG6_IPTUN_MODE_ENCAP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) err = iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (skb->protocol == htons(ETH_P_IPV6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) proto = IPPROTO_IPV6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) else if (skb->protocol == htons(ETH_P_IP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) proto = IPPROTO_IPIP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) err = seg6_do_srh_encap(skb, tinfo->srh, proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) skb_set_inner_transport_header(skb, skb_transport_offset(skb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) skb_set_inner_protocol(skb, skb->protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) skb->protocol = htons(ETH_P_IPV6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) case SEG6_IPTUN_MODE_L2ENCAP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (!skb_mac_header_was_set(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (pskb_expand_head(skb, skb->mac_len, 0, GFP_ATOMIC) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) skb_mac_header_rebuild(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) skb_push(skb, skb->mac_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) err = seg6_do_srh_encap(skb, tinfo->srh, IPPROTO_ETHERNET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) skb->protocol = htons(ETH_P_IPV6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) skb_set_transport_header(skb, sizeof(struct ipv6hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static int seg6_input(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) struct dst_entry *orig_dst = skb_dst(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) struct dst_entry *dst = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) struct seg6_lwt *slwt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) err = seg6_do_srh(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) slwt = seg6_lwt_lwtunnel(orig_dst->lwtstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) dst = dst_cache_get(&slwt->cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) skb_dst_drop(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (!dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) ip6_route_input(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) dst = skb_dst(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (!dst->error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) dst_cache_set_ip6(&slwt->cache, dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) &ipv6_hdr(skb)->saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) skb_dst_set(skb, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) err = skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) return dst_input(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) static int seg6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) struct dst_entry *orig_dst = skb_dst(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) struct dst_entry *dst = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) struct seg6_lwt *slwt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) err = seg6_do_srh(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) slwt = seg6_lwt_lwtunnel(orig_dst->lwtstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) dst = dst_cache_get(&slwt->cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) if (unlikely(!dst)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) struct ipv6hdr *hdr = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) struct flowi6 fl6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) memset(&fl6, 0, sizeof(fl6));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) fl6.daddr = hdr->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) fl6.saddr = hdr->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) fl6.flowlabel = ip6_flowinfo(hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) fl6.flowi6_mark = skb->mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) fl6.flowi6_proto = hdr->nexthdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) dst = ip6_route_output(net, NULL, &fl6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (dst->error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) err = dst->error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) dst_release(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) dst_cache_set_ip6(&slwt->cache, dst, &fl6.saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) skb_dst_drop(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) skb_dst_set(skb, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) err = skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) return dst_output(net, sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) static int seg6_build_state(struct net *net, struct nlattr *nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) unsigned int family, const void *cfg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) struct lwtunnel_state **ts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) struct nlattr *tb[SEG6_IPTUNNEL_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) struct seg6_iptunnel_encap *tuninfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) struct lwtunnel_state *newts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) int tuninfo_len, min_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) struct seg6_lwt *slwt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) if (family != AF_INET && family != AF_INET6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) err = nla_parse_nested_deprecated(tb, SEG6_IPTUNNEL_MAX, nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) seg6_iptunnel_policy, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) if (!tb[SEG6_IPTUNNEL_SRH])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) tuninfo = nla_data(tb[SEG6_IPTUNNEL_SRH]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) tuninfo_len = nla_len(tb[SEG6_IPTUNNEL_SRH]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) /* tuninfo must contain at least the iptunnel encap structure,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) * the SRH and one segment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) min_size = sizeof(*tuninfo) + sizeof(struct ipv6_sr_hdr) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) sizeof(struct in6_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) if (tuninfo_len < min_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) switch (tuninfo->mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) case SEG6_IPTUN_MODE_INLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) if (family != AF_INET6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) case SEG6_IPTUN_MODE_ENCAP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) case SEG6_IPTUN_MODE_L2ENCAP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) /* verify that SRH is consistent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) if (!seg6_validate_srh(tuninfo->srh, tuninfo_len - sizeof(*tuninfo), false))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) newts = lwtunnel_state_alloc(tuninfo_len + sizeof(*slwt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) if (!newts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) slwt = seg6_lwt_lwtunnel(newts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) err = dst_cache_init(&slwt->cache, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) kfree(newts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) memcpy(&slwt->tuninfo, tuninfo, tuninfo_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) newts->type = LWTUNNEL_ENCAP_SEG6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) newts->flags |= LWTUNNEL_STATE_INPUT_REDIRECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) if (tuninfo->mode != SEG6_IPTUN_MODE_L2ENCAP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) newts->headroom = seg6_lwt_headroom(tuninfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) *ts = newts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) static void seg6_destroy_state(struct lwtunnel_state *lwt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) dst_cache_destroy(&seg6_lwt_lwtunnel(lwt)->cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) static int seg6_fill_encap_info(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) struct lwtunnel_state *lwtstate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) struct seg6_iptunnel_encap *tuninfo = seg6_encap_lwtunnel(lwtstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) if (nla_put_srh(skb, SEG6_IPTUNNEL_SRH, tuninfo))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) static int seg6_encap_nlsize(struct lwtunnel_state *lwtstate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) struct seg6_iptunnel_encap *tuninfo = seg6_encap_lwtunnel(lwtstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) return nla_total_size(SEG6_IPTUN_ENCAP_SIZE(tuninfo));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) static int seg6_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) struct seg6_iptunnel_encap *a_hdr = seg6_encap_lwtunnel(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) struct seg6_iptunnel_encap *b_hdr = seg6_encap_lwtunnel(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) int len = SEG6_IPTUN_ENCAP_SIZE(a_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) if (len != SEG6_IPTUN_ENCAP_SIZE(b_hdr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) return memcmp(a_hdr, b_hdr, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) static const struct lwtunnel_encap_ops seg6_iptun_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) .build_state = seg6_build_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) .destroy_state = seg6_destroy_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) .output = seg6_output,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) .input = seg6_input,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) .fill_encap = seg6_fill_encap_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) .get_encap_size = seg6_encap_nlsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) .cmp_encap = seg6_encap_cmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) int __init seg6_iptunnel_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) return lwtunnel_encap_add_ops(&seg6_iptun_ops, LWTUNNEL_ENCAP_SEG6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) void seg6_iptunnel_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) lwtunnel_encap_del_ops(&seg6_iptun_ops, LWTUNNEL_ENCAP_SEG6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) }