^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * (C) 2020 Alexander Aring <alex.aring@gmail.com>
^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) #include <linux/rpl_iptunnel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <net/dst_cache.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <net/ip6_route.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <net/lwtunnel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <net/ipv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <net/rpl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) struct rpl_iptunnel_encap {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct ipv6_rpl_sr_hdr srh[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct rpl_lwt {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct dst_cache cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct rpl_iptunnel_encap tuninfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static inline struct rpl_lwt *rpl_lwt_lwtunnel(struct lwtunnel_state *lwt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) return (struct rpl_lwt *)lwt->data;
^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) static inline struct rpl_iptunnel_encap *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) rpl_encap_lwtunnel(struct lwtunnel_state *lwt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) return &rpl_lwt_lwtunnel(lwt)->tuninfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static const struct nla_policy rpl_iptunnel_policy[RPL_IPTUNNEL_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) [RPL_IPTUNNEL_SRH] = { .type = NLA_BINARY },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static bool rpl_validate_srh(struct net *net, struct ipv6_rpl_sr_hdr *srh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) size_t seglen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if ((srh->hdrlen << 3) != seglen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* check at least one segment and seglen fit with segments_left */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (!srh->segments_left ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) (srh->segments_left * sizeof(struct in6_addr)) != seglen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (srh->cmpri || srh->cmpre)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) err = ipv6_chk_rpl_srh_loop(net, srh->rpl_segaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) srh->segments_left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (ipv6_addr_type(&srh->rpl_segaddr[srh->segments_left - 1]) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) IPV6_ADDR_MULTICAST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return true;
^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 rpl_build_state(struct net *net, struct nlattr *nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) unsigned int family, const void *cfg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct lwtunnel_state **ts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct nlattr *tb[RPL_IPTUNNEL_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct lwtunnel_state *newts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct ipv6_rpl_sr_hdr *srh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct rpl_lwt *rlwt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) int err, srh_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (family != AF_INET6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) err = nla_parse_nested(tb, RPL_IPTUNNEL_MAX, nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) rpl_iptunnel_policy, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (!tb[RPL_IPTUNNEL_SRH])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) srh = nla_data(tb[RPL_IPTUNNEL_SRH]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) srh_len = nla_len(tb[RPL_IPTUNNEL_SRH]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (srh_len < sizeof(*srh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /* verify that SRH is consistent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (!rpl_validate_srh(net, srh, srh_len - sizeof(*srh)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) newts = lwtunnel_state_alloc(srh_len + sizeof(*rlwt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (!newts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) rlwt = rpl_lwt_lwtunnel(newts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) err = dst_cache_init(&rlwt->cache, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) kfree(newts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) memcpy(&rlwt->tuninfo.srh, srh, srh_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) newts->type = LWTUNNEL_ENCAP_RPL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) newts->flags |= LWTUNNEL_STATE_INPUT_REDIRECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) *ts = newts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return 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) static void rpl_destroy_state(struct lwtunnel_state *lwt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) dst_cache_destroy(&rpl_lwt_lwtunnel(lwt)->cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static int rpl_do_srh_inline(struct sk_buff *skb, const struct rpl_lwt *rlwt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) const struct ipv6_rpl_sr_hdr *srh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct ipv6_rpl_sr_hdr *isrh, *csrh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) const struct ipv6hdr *oldhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) struct ipv6hdr *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) unsigned char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) size_t hdrlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) oldhdr = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) buf = kcalloc(struct_size(srh, segments.addr, srh->segments_left), 2, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) isrh = (struct ipv6_rpl_sr_hdr *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) csrh = (struct ipv6_rpl_sr_hdr *)(buf + ((srh->hdrlen + 1) << 3));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) memcpy(isrh, srh, sizeof(*isrh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) memcpy(isrh->rpl_segaddr, &srh->rpl_segaddr[1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) (srh->segments_left - 1) * 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) isrh->rpl_segaddr[srh->segments_left - 1] = oldhdr->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) ipv6_rpl_srh_compress(csrh, isrh, &srh->rpl_segaddr[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) isrh->segments_left - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) hdrlen = ((csrh->hdrlen + 1) << 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) err = skb_cow_head(skb, hdrlen + skb->mac_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) skb_pull(skb, sizeof(struct ipv6hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) skb_postpull_rcsum(skb, skb_network_header(skb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) sizeof(struct ipv6hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) skb_push(skb, sizeof(struct ipv6hdr) + hdrlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) skb_mac_header_rebuild(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) hdr = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) memmove(hdr, oldhdr, sizeof(*hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) isrh = (void *)hdr + sizeof(*hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) memcpy(isrh, csrh, hdrlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) isrh->nexthdr = hdr->nexthdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) hdr->nexthdr = NEXTHDR_ROUTING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) hdr->daddr = srh->rpl_segaddr[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) skb_set_transport_header(skb, sizeof(struct ipv6hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) skb_postpush_rcsum(skb, hdr, sizeof(struct ipv6hdr) + hdrlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static int rpl_do_srh(struct sk_buff *skb, const struct rpl_lwt *rlwt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) struct dst_entry *dst = skb_dst(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) struct rpl_iptunnel_encap *tinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (skb->protocol != htons(ETH_P_IPV6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) tinfo = rpl_encap_lwtunnel(dst->lwtstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) err = rpl_do_srh_inline(skb, rlwt, tinfo->srh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) static int rpl_output(struct net *net, struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct dst_entry *orig_dst = skb_dst(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct dst_entry *dst = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) struct rpl_lwt *rlwt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) rlwt = rpl_lwt_lwtunnel(orig_dst->lwtstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) err = rpl_do_srh(skb, rlwt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) dst = dst_cache_get(&rlwt->cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (unlikely(!dst)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) struct ipv6hdr *hdr = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) struct flowi6 fl6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) memset(&fl6, 0, sizeof(fl6));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) fl6.daddr = hdr->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) fl6.saddr = hdr->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) fl6.flowlabel = ip6_flowinfo(hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) fl6.flowi6_mark = skb->mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) fl6.flowi6_proto = hdr->nexthdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) dst = ip6_route_output(net, NULL, &fl6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (dst->error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) err = dst->error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) dst_release(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) dst_cache_set_ip6(&rlwt->cache, dst, &fl6.saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) skb_dst_drop(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) skb_dst_set(skb, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) err = skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return dst_output(net, sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) static int rpl_input(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) struct dst_entry *orig_dst = skb_dst(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct dst_entry *dst = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct rpl_lwt *rlwt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) rlwt = rpl_lwt_lwtunnel(orig_dst->lwtstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) err = rpl_do_srh(skb, rlwt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) dst = dst_cache_get(&rlwt->cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) skb_dst_drop(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (!dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) ip6_route_input(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) dst = skb_dst(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) if (!dst->error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) dst_cache_set_ip6(&rlwt->cache, dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) &ipv6_hdr(skb)->saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) skb_dst_set(skb, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) err = skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) return dst_input(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static int nla_put_rpl_srh(struct sk_buff *skb, int attrtype,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) struct rpl_iptunnel_encap *tuninfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) struct rpl_iptunnel_encap *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) struct nlattr *nla;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) len = RPL_IPTUNNEL_SRH_SIZE(tuninfo->srh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) nla = nla_reserve(skb, attrtype, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (!nla)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) data = nla_data(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) memcpy(data, tuninfo->srh, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) static int rpl_fill_encap_info(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) struct lwtunnel_state *lwtstate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) struct rpl_iptunnel_encap *tuninfo = rpl_encap_lwtunnel(lwtstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (nla_put_rpl_srh(skb, RPL_IPTUNNEL_SRH, tuninfo))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static int rpl_encap_nlsize(struct lwtunnel_state *lwtstate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) struct rpl_iptunnel_encap *tuninfo = rpl_encap_lwtunnel(lwtstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return nla_total_size(RPL_IPTUNNEL_SRH_SIZE(tuninfo->srh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) static int rpl_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) struct rpl_iptunnel_encap *a_hdr = rpl_encap_lwtunnel(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) struct rpl_iptunnel_encap *b_hdr = rpl_encap_lwtunnel(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) int len = RPL_IPTUNNEL_SRH_SIZE(a_hdr->srh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (len != RPL_IPTUNNEL_SRH_SIZE(b_hdr->srh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) return memcmp(a_hdr, b_hdr, len);
^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 const struct lwtunnel_encap_ops rpl_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) .build_state = rpl_build_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) .destroy_state = rpl_destroy_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) .output = rpl_output,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) .input = rpl_input,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) .fill_encap = rpl_fill_encap_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) .get_encap_size = rpl_encap_nlsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) .cmp_encap = rpl_encap_cmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) int __init rpl_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) err = lwtunnel_encap_add_ops(&rpl_ops, LWTUNNEL_ENCAP_RPL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) pr_info("RPL Segment Routing with IPv6\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) void rpl_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) lwtunnel_encap_del_ops(&rpl_ops, LWTUNNEL_ENCAP_RPL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }