^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <net/checksum.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <net/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <net/ip6_fib.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/protocol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <uapi/linux/ila.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "ila.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) void ila_init_saved_csum(struct ila_params *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) if (!p->locator_match.v64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) p->csum_diff = compute_csum_diff8(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) (__be32 *)&p->locator,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) (__be32 *)&p->locator_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static __wsum get_csum_diff_iaddr(struct ila_addr *iaddr, struct ila_params *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (p->locator_match.v64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) return p->csum_diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return compute_csum_diff8((__be32 *)&p->locator,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) (__be32 *)&iaddr->loc);
^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 __wsum get_csum_diff(struct ipv6hdr *ip6h, struct ila_params *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return get_csum_diff_iaddr(ila_a2i(&ip6h->daddr), p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static void ila_csum_do_neutral_fmt(struct ila_addr *iaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct ila_params *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) __sum16 *adjust = (__force __sum16 *)&iaddr->ident.v16[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) __wsum diff, fval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) diff = get_csum_diff_iaddr(iaddr, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) fval = (__force __wsum)(ila_csum_neutral_set(iaddr->ident) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) CSUM_NEUTRAL_FLAG : ~CSUM_NEUTRAL_FLAG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) diff = csum_add(diff, fval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) *adjust = ~csum_fold(csum_add(diff, csum_unfold(*adjust)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) /* Flip the csum-neutral bit. Either we are doing a SIR->ILA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * translation with ILA_CSUM_NEUTRAL_MAP as the csum_method
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * and the C-bit is not set, or we are doing an ILA-SIR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * tranlsation and the C-bit is set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) iaddr->ident.csum_neutral ^= 1;
^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 void ila_csum_do_neutral_nofmt(struct ila_addr *iaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct ila_params *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) __sum16 *adjust = (__force __sum16 *)&iaddr->ident.v16[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) __wsum diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) diff = get_csum_diff_iaddr(iaddr, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) *adjust = ~csum_fold(csum_add(diff, csum_unfold(*adjust)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static void ila_csum_adjust_transport(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct ila_params *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) size_t nhoff = sizeof(struct ipv6hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct ipv6hdr *ip6h = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) __wsum diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) switch (ip6h->nexthdr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) case NEXTHDR_TCP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (likely(pskb_may_pull(skb, nhoff + sizeof(struct tcphdr)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct tcphdr *th = (struct tcphdr *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) (skb_network_header(skb) + nhoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) diff = get_csum_diff(ip6h, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) inet_proto_csum_replace_by_diff(&th->check, skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) diff, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) case NEXTHDR_UDP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (likely(pskb_may_pull(skb, nhoff + sizeof(struct udphdr)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct udphdr *uh = (struct udphdr *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) (skb_network_header(skb) + nhoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) diff = get_csum_diff(ip6h, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) inet_proto_csum_replace_by_diff(&uh->check, skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) diff, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (!uh->check)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) uh->check = CSUM_MANGLED_0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) case NEXTHDR_ICMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (likely(pskb_may_pull(skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) nhoff + sizeof(struct icmp6hdr)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct icmp6hdr *ih = (struct icmp6hdr *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) (skb_network_header(skb) + nhoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) diff = get_csum_diff(ip6h, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) inet_proto_csum_replace_by_diff(&ih->icmp6_cksum, skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) diff, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) void ila_update_ipv6_locator(struct sk_buff *skb, struct ila_params *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) bool sir2ila)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct ipv6hdr *ip6h = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct ila_addr *iaddr = ila_a2i(&ip6h->daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) switch (p->csum_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) case ILA_CSUM_ADJUST_TRANSPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) ila_csum_adjust_transport(skb, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) case ILA_CSUM_NEUTRAL_MAP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (sir2ila) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (WARN_ON(ila_csum_neutral_set(iaddr->ident))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) /* Checksum flag should never be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * set in a formatted SIR address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) } else if (!ila_csum_neutral_set(iaddr->ident)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /* ILA to SIR translation and C-bit isn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * set so we're good.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) ila_csum_do_neutral_fmt(iaddr, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) case ILA_CSUM_NEUTRAL_MAP_AUTO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) ila_csum_do_neutral_nofmt(iaddr, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) case ILA_CSUM_NO_ACTION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /* Now change destination address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) iaddr->loc = p->locator;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }