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