Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 2 | #include <linux/module.h> |
| 3 | #include <linux/errno.h> |
| 4 | #include <linux/socket.h> |
| 5 | #include <linux/skbuff.h> |
| 6 | #include <linux/ip.h> |
Stefano Brivio | b8a51b3 | 2018-11-08 12:19:23 +0100 | [diff] [blame] | 7 | #include <linux/icmp.h> |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 8 | #include <linux/udp.h> |
| 9 | #include <linux/types.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <net/genetlink.h> |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 12 | #include <net/gue.h> |
stephen hemminger | 9dc621a | 2017-05-19 09:55:54 -0700 | [diff] [blame] | 13 | #include <net/fou.h> |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 14 | #include <net/ip.h> |
Tom Herbert | afe9332 | 2014-09-17 12:25:57 -0700 | [diff] [blame] | 15 | #include <net/protocol.h> |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 16 | #include <net/udp.h> |
| 17 | #include <net/udp_tunnel.h> |
| 18 | #include <net/xfrm.h> |
| 19 | #include <uapi/linux/fou.h> |
| 20 | #include <uapi/linux/genetlink.h> |
| 21 | |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 22 | struct fou { |
| 23 | struct socket *sock; |
| 24 | u8 protocol; |
Tom Herbert | fe881ef | 2015-02-10 16:30:33 -0800 | [diff] [blame] | 25 | u8 flags; |
WANG Cong | 4cbcdf2 | 2015-04-10 12:00:28 -0700 | [diff] [blame] | 26 | __be16 port; |
Tom Herbert | 5f914b6 | 2016-05-18 09:06:16 -0700 | [diff] [blame] | 27 | u8 family; |
WANG Cong | 7a6c8c3 | 2015-04-10 12:00:30 -0700 | [diff] [blame] | 28 | u16 type; |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 29 | struct list_head list; |
Hannes Frederic Sowa | 3036fac | 2015-12-15 21:01:53 +0100 | [diff] [blame] | 30 | struct rcu_head rcu; |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
Tom Herbert | fe881ef | 2015-02-10 16:30:33 -0800 | [diff] [blame] | 33 | #define FOU_F_REMCSUM_NOPARTIAL BIT(0) |
| 34 | |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 35 | struct fou_cfg { |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 36 | u16 type; |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 37 | u8 protocol; |
Tom Herbert | fe881ef | 2015-02-10 16:30:33 -0800 | [diff] [blame] | 38 | u8 flags; |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 39 | struct udp_port_cfg udp_config; |
| 40 | }; |
| 41 | |
WANG Cong | 02d793c | 2015-04-10 12:00:29 -0700 | [diff] [blame] | 42 | static unsigned int fou_net_id; |
| 43 | |
| 44 | struct fou_net { |
| 45 | struct list_head fou_list; |
| 46 | struct mutex fou_lock; |
| 47 | }; |
| 48 | |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 49 | static inline struct fou *fou_from_sock(struct sock *sk) |
| 50 | { |
| 51 | return sk->sk_user_data; |
| 52 | } |
| 53 | |
Tom Herbert | 5f914b6 | 2016-05-18 09:06:16 -0700 | [diff] [blame] | 54 | static int fou_recv_pull(struct sk_buff *skb, struct fou *fou, size_t len) |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 55 | { |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 56 | /* Remove 'len' bytes from the packet (UDP header and |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 57 | * FOU header if present). |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 58 | */ |
Tom Herbert | 5f914b6 | 2016-05-18 09:06:16 -0700 | [diff] [blame] | 59 | if (fou->family == AF_INET) |
| 60 | ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len); |
| 61 | else |
| 62 | ipv6_hdr(skb)->payload_len = |
| 63 | htons(ntohs(ipv6_hdr(skb)->payload_len) - len); |
| 64 | |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 65 | __skb_pull(skb, len); |
| 66 | skb_postpull_rcsum(skb, udp_hdr(skb), len); |
| 67 | skb_reset_transport_header(skb); |
Jesse Gross | a09a4c8 | 2016-03-19 09:32:02 -0700 | [diff] [blame] | 68 | return iptunnel_pull_offloads(skb); |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static int fou_udp_recv(struct sock *sk, struct sk_buff *skb) |
| 72 | { |
| 73 | struct fou *fou = fou_from_sock(sk); |
| 74 | |
| 75 | if (!fou) |
| 76 | return 1; |
| 77 | |
Tom Herbert | 5f914b6 | 2016-05-18 09:06:16 -0700 | [diff] [blame] | 78 | if (fou_recv_pull(skb, fou, sizeof(struct udphdr))) |
Jesse Gross | a09a4c8 | 2016-03-19 09:32:02 -0700 | [diff] [blame] | 79 | goto drop; |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 80 | |
| 81 | return -fou->protocol; |
Jesse Gross | a09a4c8 | 2016-03-19 09:32:02 -0700 | [diff] [blame] | 82 | |
| 83 | drop: |
| 84 | kfree_skb(skb); |
| 85 | return 0; |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 88 | static struct guehdr *gue_remcsum(struct sk_buff *skb, struct guehdr *guehdr, |
Tom Herbert | fe881ef | 2015-02-10 16:30:33 -0800 | [diff] [blame] | 89 | void *data, size_t hdrlen, u8 ipproto, |
| 90 | bool nopartial) |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 91 | { |
| 92 | __be16 *pd = data; |
Tom Herbert | 4fd671d | 2014-11-25 11:21:20 -0800 | [diff] [blame] | 93 | size_t start = ntohs(pd[0]); |
| 94 | size_t offset = ntohs(pd[1]); |
Tom Herbert | b7fe10e | 2015-08-19 17:07:32 -0700 | [diff] [blame] | 95 | size_t plen = sizeof(struct udphdr) + hdrlen + |
| 96 | max_t(size_t, offset + sizeof(u16), start); |
| 97 | |
| 98 | if (skb->remcsum_offload) |
| 99 | return guehdr; |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 100 | |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 101 | if (!pskb_may_pull(skb, plen)) |
| 102 | return NULL; |
| 103 | guehdr = (struct guehdr *)&udp_hdr(skb)[1]; |
| 104 | |
Tom Herbert | fe881ef | 2015-02-10 16:30:33 -0800 | [diff] [blame] | 105 | skb_remcsum_process(skb, (void *)guehdr + hdrlen, |
| 106 | start, offset, nopartial); |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 107 | |
| 108 | return guehdr; |
| 109 | } |
| 110 | |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 111 | static int gue_control_message(struct sk_buff *skb, struct guehdr *guehdr) |
| 112 | { |
| 113 | /* No support yet */ |
| 114 | kfree_skb(skb); |
| 115 | return 0; |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 118 | static int gue_udp_recv(struct sock *sk, struct sk_buff *skb) |
| 119 | { |
| 120 | struct fou *fou = fou_from_sock(sk); |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 121 | size_t len, optlen, hdrlen; |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 122 | struct guehdr *guehdr; |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 123 | void *data; |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 124 | u16 doffset = 0; |
Lorenzo Bianconi | 988dc4a | 2019-04-09 11:47:20 +0200 | [diff] [blame] | 125 | u8 proto_ctype; |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 126 | |
| 127 | if (!fou) |
| 128 | return 1; |
| 129 | |
| 130 | len = sizeof(struct udphdr) + sizeof(struct guehdr); |
| 131 | if (!pskb_may_pull(skb, len)) |
| 132 | goto drop; |
| 133 | |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 134 | guehdr = (struct guehdr *)&udp_hdr(skb)[1]; |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 135 | |
Tom Herbert | c1e48af | 2016-06-06 16:06:02 -0700 | [diff] [blame] | 136 | switch (guehdr->version) { |
| 137 | case 0: /* Full GUE header present */ |
| 138 | break; |
| 139 | |
| 140 | case 1: { |
Simon Horman | c9d52f2 | 2019-04-09 09:59:07 +0200 | [diff] [blame] | 141 | /* Direct encapsulation of IPv4 or IPv6 */ |
Tom Herbert | c1e48af | 2016-06-06 16:06:02 -0700 | [diff] [blame] | 142 | |
| 143 | int prot; |
| 144 | |
| 145 | switch (((struct iphdr *)guehdr)->version) { |
| 146 | case 4: |
| 147 | prot = IPPROTO_IPIP; |
| 148 | break; |
| 149 | case 6: |
| 150 | prot = IPPROTO_IPV6; |
| 151 | break; |
| 152 | default: |
| 153 | goto drop; |
| 154 | } |
| 155 | |
| 156 | if (fou_recv_pull(skb, fou, sizeof(struct udphdr))) |
| 157 | goto drop; |
| 158 | |
| 159 | return -prot; |
| 160 | } |
| 161 | |
| 162 | default: /* Undefined version */ |
| 163 | goto drop; |
| 164 | } |
| 165 | |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 166 | optlen = guehdr->hlen << 2; |
| 167 | len += optlen; |
| 168 | |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 169 | if (!pskb_may_pull(skb, len)) |
| 170 | goto drop; |
| 171 | |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 172 | /* guehdr may change after pull */ |
| 173 | guehdr = (struct guehdr *)&udp_hdr(skb)[1]; |
Li RongQing | d8f00d2 | 2014-10-17 16:53:47 +0800 | [diff] [blame] | 174 | |
Lorenzo Bianconi | 526bb57 | 2019-04-09 12:03:07 +0200 | [diff] [blame] | 175 | if (validate_gue_flags(guehdr, optlen)) |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 176 | goto drop; |
| 177 | |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 178 | hdrlen = sizeof(struct guehdr) + optlen; |
| 179 | |
Tom Herbert | 5f914b6 | 2016-05-18 09:06:16 -0700 | [diff] [blame] | 180 | if (fou->family == AF_INET) |
| 181 | ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len); |
| 182 | else |
| 183 | ipv6_hdr(skb)->payload_len = |
| 184 | htons(ntohs(ipv6_hdr(skb)->payload_len) - len); |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 185 | |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 186 | /* Pull csum through the guehdr now . This can be used if |
| 187 | * there is a remote checksum offload. |
| 188 | */ |
| 189 | skb_postpull_rcsum(skb, udp_hdr(skb), len); |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 190 | |
| 191 | data = &guehdr[1]; |
| 192 | |
| 193 | if (guehdr->flags & GUE_FLAG_PRIV) { |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 194 | __be32 flags = *(__be32 *)(data + doffset); |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 195 | |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 196 | doffset += GUE_LEN_PRIV; |
| 197 | |
| 198 | if (flags & GUE_PFLAG_REMCSUM) { |
| 199 | guehdr = gue_remcsum(skb, guehdr, data + doffset, |
Tom Herbert | fe881ef | 2015-02-10 16:30:33 -0800 | [diff] [blame] | 200 | hdrlen, guehdr->proto_ctype, |
| 201 | !!(fou->flags & |
| 202 | FOU_F_REMCSUM_NOPARTIAL)); |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 203 | if (!guehdr) |
| 204 | goto drop; |
| 205 | |
| 206 | data = &guehdr[1]; |
| 207 | |
| 208 | doffset += GUE_PLEN_REMCSUM; |
| 209 | } |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 212 | if (unlikely(guehdr->control)) |
| 213 | return gue_control_message(skb, guehdr); |
| 214 | |
Lorenzo Bianconi | 988dc4a | 2019-04-09 11:47:20 +0200 | [diff] [blame] | 215 | proto_ctype = guehdr->proto_ctype; |
Tom Herbert | 4fd671d | 2014-11-25 11:21:20 -0800 | [diff] [blame] | 216 | __skb_pull(skb, sizeof(struct udphdr) + hdrlen); |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 217 | skb_reset_transport_header(skb); |
| 218 | |
Jesse Gross | a09a4c8 | 2016-03-19 09:32:02 -0700 | [diff] [blame] | 219 | if (iptunnel_pull_offloads(skb)) |
| 220 | goto drop; |
| 221 | |
Lorenzo Bianconi | 988dc4a | 2019-04-09 11:47:20 +0200 | [diff] [blame] | 222 | return -proto_ctype; |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 223 | |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 224 | drop: |
| 225 | kfree_skb(skb); |
| 226 | return 0; |
| 227 | } |
| 228 | |
David Miller | d4546c2 | 2018-06-24 14:13:49 +0900 | [diff] [blame] | 229 | static struct sk_buff *fou_gro_receive(struct sock *sk, |
| 230 | struct list_head *head, |
| 231 | struct sk_buff *skb) |
Tom Herbert | afe9332 | 2014-09-17 12:25:57 -0700 | [diff] [blame] | 232 | { |
Tom Herbert | d92283e | 2016-04-05 08:22:54 -0700 | [diff] [blame] | 233 | u8 proto = fou_from_sock(sk)->protocol; |
Tom Herbert | efc98d0 | 2014-10-03 15:48:08 -0700 | [diff] [blame] | 234 | const struct net_offload **offloads; |
David Miller | d4546c2 | 2018-06-24 14:13:49 +0900 | [diff] [blame] | 235 | const struct net_offload *ops; |
| 236 | struct sk_buff *pp = NULL; |
Tom Herbert | afe9332 | 2014-09-17 12:25:57 -0700 | [diff] [blame] | 237 | |
Alexander Duyck | c348338 | 2016-03-29 14:55:22 -0700 | [diff] [blame] | 238 | /* We can clear the encap_mark for FOU as we are essentially doing |
| 239 | * one of two possible things. We are either adding an L4 tunnel |
Randy Dunlap | 2bdcc73 | 2020-08-22 16:31:41 -0700 | [diff] [blame] | 240 | * header to the outer L3 tunnel header, or we are simply |
Alexander Duyck | c348338 | 2016-03-29 14:55:22 -0700 | [diff] [blame] | 241 | * treating the GRE tunnel header as though it is a UDP protocol |
| 242 | * specific header such as VXLAN or GENEVE. |
| 243 | */ |
| 244 | NAPI_GRO_CB(skb)->encap_mark = 0; |
| 245 | |
Alexander Duyck | a0ca153 | 2016-04-05 09:13:39 -0700 | [diff] [blame] | 246 | /* Flag this frame as already having an outer encap header */ |
| 247 | NAPI_GRO_CB(skb)->is_fou = 1; |
| 248 | |
Tom Herbert | afe9332 | 2014-09-17 12:25:57 -0700 | [diff] [blame] | 249 | rcu_read_lock(); |
Tom Herbert | efc98d0 | 2014-10-03 15:48:08 -0700 | [diff] [blame] | 250 | offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads; |
Tom Herbert | afe9332 | 2014-09-17 12:25:57 -0700 | [diff] [blame] | 251 | ops = rcu_dereference(offloads[proto]); |
| 252 | if (!ops || !ops->callbacks.gro_receive) |
| 253 | goto out_unlock; |
| 254 | |
Sabrina Dubroca | fcd91dd | 2016-10-20 15:58:02 +0200 | [diff] [blame] | 255 | pp = call_gro_receive(ops->callbacks.gro_receive, head, skb); |
Tom Herbert | afe9332 | 2014-09-17 12:25:57 -0700 | [diff] [blame] | 256 | |
| 257 | out_unlock: |
| 258 | rcu_read_unlock(); |
| 259 | |
| 260 | return pp; |
| 261 | } |
| 262 | |
Tom Herbert | d92283e | 2016-04-05 08:22:54 -0700 | [diff] [blame] | 263 | static int fou_gro_complete(struct sock *sk, struct sk_buff *skb, |
| 264 | int nhoff) |
Tom Herbert | afe9332 | 2014-09-17 12:25:57 -0700 | [diff] [blame] | 265 | { |
| 266 | const struct net_offload *ops; |
Tom Herbert | d92283e | 2016-04-05 08:22:54 -0700 | [diff] [blame] | 267 | u8 proto = fou_from_sock(sk)->protocol; |
Tom Herbert | afe9332 | 2014-09-17 12:25:57 -0700 | [diff] [blame] | 268 | int err = -ENOSYS; |
Tom Herbert | efc98d0 | 2014-10-03 15:48:08 -0700 | [diff] [blame] | 269 | const struct net_offload **offloads; |
Tom Herbert | afe9332 | 2014-09-17 12:25:57 -0700 | [diff] [blame] | 270 | |
| 271 | rcu_read_lock(); |
Tom Herbert | efc98d0 | 2014-10-03 15:48:08 -0700 | [diff] [blame] | 272 | offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads; |
Tom Herbert | afe9332 | 2014-09-17 12:25:57 -0700 | [diff] [blame] | 273 | ops = rcu_dereference(offloads[proto]); |
| 274 | if (WARN_ON(!ops || !ops->callbacks.gro_complete)) |
| 275 | goto out_unlock; |
| 276 | |
| 277 | err = ops->callbacks.gro_complete(skb, nhoff); |
| 278 | |
Jarno Rajahalme | 229740c | 2016-05-03 16:10:21 -0700 | [diff] [blame] | 279 | skb_set_inner_mac_header(skb, nhoff); |
| 280 | |
Tom Herbert | afe9332 | 2014-09-17 12:25:57 -0700 | [diff] [blame] | 281 | out_unlock: |
| 282 | rcu_read_unlock(); |
| 283 | |
| 284 | return err; |
| 285 | } |
| 286 | |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 287 | static struct guehdr *gue_gro_remcsum(struct sk_buff *skb, unsigned int off, |
| 288 | struct guehdr *guehdr, void *data, |
Tom Herbert | b7fe10e | 2015-08-19 17:07:32 -0700 | [diff] [blame] | 289 | size_t hdrlen, struct gro_remcsum *grc, |
| 290 | bool nopartial) |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 291 | { |
| 292 | __be16 *pd = data; |
Tom Herbert | 4fd671d | 2014-11-25 11:21:20 -0800 | [diff] [blame] | 293 | size_t start = ntohs(pd[0]); |
| 294 | size_t offset = ntohs(pd[1]); |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 295 | |
| 296 | if (skb->remcsum_offload) |
Tom Herbert | b7fe10e | 2015-08-19 17:07:32 -0700 | [diff] [blame] | 297 | return guehdr; |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 298 | |
Tom Herbert | 4fd671d | 2014-11-25 11:21:20 -0800 | [diff] [blame] | 299 | if (!NAPI_GRO_CB(skb)->csum_valid) |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 300 | return NULL; |
| 301 | |
Tom Herbert | b7fe10e | 2015-08-19 17:07:32 -0700 | [diff] [blame] | 302 | guehdr = skb_gro_remcsum_process(skb, (void *)guehdr, off, hdrlen, |
| 303 | start, offset, grc, nopartial); |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 304 | |
| 305 | skb->remcsum_offload = 1; |
| 306 | |
| 307 | return guehdr; |
| 308 | } |
| 309 | |
David Miller | d4546c2 | 2018-06-24 14:13:49 +0900 | [diff] [blame] | 310 | static struct sk_buff *gue_gro_receive(struct sock *sk, |
| 311 | struct list_head *head, |
| 312 | struct sk_buff *skb) |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 313 | { |
| 314 | const struct net_offload **offloads; |
| 315 | const struct net_offload *ops; |
David Miller | d4546c2 | 2018-06-24 14:13:49 +0900 | [diff] [blame] | 316 | struct sk_buff *pp = NULL; |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 317 | struct sk_buff *p; |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 318 | struct guehdr *guehdr; |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 319 | size_t len, optlen, hdrlen, off; |
| 320 | void *data; |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 321 | u16 doffset = 0; |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 322 | int flush = 1; |
Tom Herbert | d92283e | 2016-04-05 08:22:54 -0700 | [diff] [blame] | 323 | struct fou *fou = fou_from_sock(sk); |
Tom Herbert | 26c4f7d | 2015-02-10 16:30:27 -0800 | [diff] [blame] | 324 | struct gro_remcsum grc; |
Tom Herbert | c1e48af | 2016-06-06 16:06:02 -0700 | [diff] [blame] | 325 | u8 proto; |
Tom Herbert | 26c4f7d | 2015-02-10 16:30:27 -0800 | [diff] [blame] | 326 | |
| 327 | skb_gro_remcsum_init(&grc); |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 328 | |
| 329 | off = skb_gro_offset(skb); |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 330 | len = off + sizeof(*guehdr); |
| 331 | |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 332 | guehdr = skb_gro_header_fast(skb, off); |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 333 | if (skb_gro_header_hard(skb, len)) { |
| 334 | guehdr = skb_gro_header_slow(skb, len, off); |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 335 | if (unlikely(!guehdr)) |
| 336 | goto out; |
| 337 | } |
| 338 | |
Tom Herbert | c1e48af | 2016-06-06 16:06:02 -0700 | [diff] [blame] | 339 | switch (guehdr->version) { |
| 340 | case 0: |
| 341 | break; |
| 342 | case 1: |
| 343 | switch (((struct iphdr *)guehdr)->version) { |
| 344 | case 4: |
| 345 | proto = IPPROTO_IPIP; |
| 346 | break; |
| 347 | case 6: |
| 348 | proto = IPPROTO_IPV6; |
| 349 | break; |
| 350 | default: |
| 351 | goto out; |
| 352 | } |
| 353 | goto next_proto; |
| 354 | default: |
| 355 | goto out; |
| 356 | } |
| 357 | |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 358 | optlen = guehdr->hlen << 2; |
| 359 | len += optlen; |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 360 | |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 361 | if (skb_gro_header_hard(skb, len)) { |
| 362 | guehdr = skb_gro_header_slow(skb, len, off); |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 363 | if (unlikely(!guehdr)) |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 364 | goto out; |
| 365 | } |
| 366 | |
| 367 | if (unlikely(guehdr->control) || guehdr->version != 0 || |
| 368 | validate_gue_flags(guehdr, optlen)) |
| 369 | goto out; |
| 370 | |
| 371 | hdrlen = sizeof(*guehdr) + optlen; |
| 372 | |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 373 | /* Adjust NAPI_GRO_CB(skb)->csum to account for guehdr, |
| 374 | * this is needed if there is a remote checkcsum offload. |
| 375 | */ |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 376 | skb_gro_postpull_rcsum(skb, guehdr, hdrlen); |
| 377 | |
| 378 | data = &guehdr[1]; |
| 379 | |
| 380 | if (guehdr->flags & GUE_FLAG_PRIV) { |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 381 | __be32 flags = *(__be32 *)(data + doffset); |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 382 | |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 383 | doffset += GUE_LEN_PRIV; |
| 384 | |
| 385 | if (flags & GUE_PFLAG_REMCSUM) { |
| 386 | guehdr = gue_gro_remcsum(skb, off, guehdr, |
Tom Herbert | b7fe10e | 2015-08-19 17:07:32 -0700 | [diff] [blame] | 387 | data + doffset, hdrlen, &grc, |
Tom Herbert | fe881ef | 2015-02-10 16:30:33 -0800 | [diff] [blame] | 388 | !!(fou->flags & |
| 389 | FOU_F_REMCSUM_NOPARTIAL)); |
Tom Herbert | b7fe10e | 2015-08-19 17:07:32 -0700 | [diff] [blame] | 390 | |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 391 | if (!guehdr) |
| 392 | goto out; |
| 393 | |
| 394 | data = &guehdr[1]; |
| 395 | |
| 396 | doffset += GUE_PLEN_REMCSUM; |
| 397 | } |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 400 | skb_gro_pull(skb, hdrlen); |
| 401 | |
David Miller | d4546c2 | 2018-06-24 14:13:49 +0900 | [diff] [blame] | 402 | list_for_each_entry(p, head, list) { |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 403 | const struct guehdr *guehdr2; |
| 404 | |
| 405 | if (!NAPI_GRO_CB(p)->same_flow) |
| 406 | continue; |
| 407 | |
| 408 | guehdr2 = (struct guehdr *)(p->data + off); |
| 409 | |
| 410 | /* Compare base GUE header to be equal (covers |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 411 | * hlen, version, proto_ctype, and flags. |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 412 | */ |
| 413 | if (guehdr->word != guehdr2->word) { |
| 414 | NAPI_GRO_CB(p)->same_flow = 0; |
| 415 | continue; |
| 416 | } |
| 417 | |
| 418 | /* Compare optional fields are the same. */ |
| 419 | if (guehdr->hlen && memcmp(&guehdr[1], &guehdr2[1], |
| 420 | guehdr->hlen << 2)) { |
| 421 | NAPI_GRO_CB(p)->same_flow = 0; |
| 422 | continue; |
| 423 | } |
| 424 | } |
| 425 | |
Tom Herbert | c1e48af | 2016-06-06 16:06:02 -0700 | [diff] [blame] | 426 | proto = guehdr->proto_ctype; |
| 427 | |
| 428 | next_proto: |
| 429 | |
Alexander Duyck | c348338 | 2016-03-29 14:55:22 -0700 | [diff] [blame] | 430 | /* We can clear the encap_mark for GUE as we are essentially doing |
| 431 | * one of two possible things. We are either adding an L4 tunnel |
Randy Dunlap | 2bdcc73 | 2020-08-22 16:31:41 -0700 | [diff] [blame] | 432 | * header to the outer L3 tunnel header, or we are simply |
Alexander Duyck | c348338 | 2016-03-29 14:55:22 -0700 | [diff] [blame] | 433 | * treating the GRE tunnel header as though it is a UDP protocol |
| 434 | * specific header such as VXLAN or GENEVE. |
| 435 | */ |
| 436 | NAPI_GRO_CB(skb)->encap_mark = 0; |
| 437 | |
Alexander Duyck | a0ca153 | 2016-04-05 09:13:39 -0700 | [diff] [blame] | 438 | /* Flag this frame as already having an outer encap header */ |
| 439 | NAPI_GRO_CB(skb)->is_fou = 1; |
| 440 | |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 441 | rcu_read_lock(); |
| 442 | offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads; |
Tom Herbert | c1e48af | 2016-06-06 16:06:02 -0700 | [diff] [blame] | 443 | ops = rcu_dereference(offloads[proto]); |
Tom Herbert | 2701366 | 2015-08-19 17:07:34 -0700 | [diff] [blame] | 444 | if (WARN_ON_ONCE(!ops || !ops->callbacks.gro_receive)) |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 445 | goto out_unlock; |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 446 | |
Sabrina Dubroca | fcd91dd | 2016-10-20 15:58:02 +0200 | [diff] [blame] | 447 | pp = call_gro_receive(ops->callbacks.gro_receive, head, skb); |
Alexander Duyck | c194cf9 | 2016-03-09 09:24:23 -0800 | [diff] [blame] | 448 | flush = 0; |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 449 | |
| 450 | out_unlock: |
| 451 | rcu_read_unlock(); |
| 452 | out: |
Sabrina Dubroca | 603d4cf | 2018-06-30 17:38:55 +0200 | [diff] [blame] | 453 | skb_gro_flush_final_remcsum(skb, pp, flush, &grc); |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 454 | |
| 455 | return pp; |
| 456 | } |
| 457 | |
Tom Herbert | d92283e | 2016-04-05 08:22:54 -0700 | [diff] [blame] | 458 | static int gue_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff) |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 459 | { |
| 460 | const struct net_offload **offloads; |
| 461 | struct guehdr *guehdr = (struct guehdr *)(skb->data + nhoff); |
| 462 | const struct net_offload *ops; |
Tom Herbert | c1e48af | 2016-06-06 16:06:02 -0700 | [diff] [blame] | 463 | unsigned int guehlen = 0; |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 464 | u8 proto; |
| 465 | int err = -ENOENT; |
| 466 | |
Tom Herbert | c1e48af | 2016-06-06 16:06:02 -0700 | [diff] [blame] | 467 | switch (guehdr->version) { |
| 468 | case 0: |
| 469 | proto = guehdr->proto_ctype; |
| 470 | guehlen = sizeof(*guehdr) + (guehdr->hlen << 2); |
| 471 | break; |
| 472 | case 1: |
| 473 | switch (((struct iphdr *)guehdr)->version) { |
| 474 | case 4: |
| 475 | proto = IPPROTO_IPIP; |
| 476 | break; |
| 477 | case 6: |
| 478 | proto = IPPROTO_IPV6; |
| 479 | break; |
| 480 | default: |
| 481 | return err; |
| 482 | } |
| 483 | break; |
| 484 | default: |
| 485 | return err; |
| 486 | } |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 487 | |
| 488 | rcu_read_lock(); |
| 489 | offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads; |
| 490 | ops = rcu_dereference(offloads[proto]); |
| 491 | if (WARN_ON(!ops || !ops->callbacks.gro_complete)) |
| 492 | goto out_unlock; |
| 493 | |
| 494 | err = ops->callbacks.gro_complete(skb, nhoff + guehlen); |
| 495 | |
Jarno Rajahalme | 229740c | 2016-05-03 16:10:21 -0700 | [diff] [blame] | 496 | skb_set_inner_mac_header(skb, nhoff + guehlen); |
| 497 | |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 498 | out_unlock: |
| 499 | rcu_read_unlock(); |
| 500 | return err; |
| 501 | } |
| 502 | |
Kristian Evensen | 1713cb3 | 2019-03-27 11:16:03 +0100 | [diff] [blame] | 503 | static bool fou_cfg_cmp(struct fou *fou, struct fou_cfg *cfg) |
| 504 | { |
| 505 | struct sock *sk = fou->sock->sk; |
| 506 | struct udp_port_cfg *udp_cfg = &cfg->udp_config; |
| 507 | |
| 508 | if (fou->family != udp_cfg->family || |
| 509 | fou->port != udp_cfg->local_udp_port || |
| 510 | sk->sk_dport != udp_cfg->peer_udp_port || |
| 511 | sk->sk_bound_dev_if != udp_cfg->bind_ifindex) |
| 512 | return false; |
| 513 | |
| 514 | if (fou->family == AF_INET) { |
| 515 | if (sk->sk_rcv_saddr != udp_cfg->local_ip.s_addr || |
| 516 | sk->sk_daddr != udp_cfg->peer_ip.s_addr) |
| 517 | return false; |
| 518 | else |
| 519 | return true; |
| 520 | #if IS_ENABLED(CONFIG_IPV6) |
| 521 | } else { |
| 522 | if (ipv6_addr_cmp(&sk->sk_v6_rcv_saddr, &udp_cfg->local_ip6) || |
| 523 | ipv6_addr_cmp(&sk->sk_v6_daddr, &udp_cfg->peer_ip6)) |
| 524 | return false; |
| 525 | else |
| 526 | return true; |
| 527 | #endif |
| 528 | } |
| 529 | |
| 530 | return false; |
| 531 | } |
| 532 | |
| 533 | static int fou_add_to_port_list(struct net *net, struct fou *fou, |
| 534 | struct fou_cfg *cfg) |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 535 | { |
WANG Cong | 02d793c | 2015-04-10 12:00:29 -0700 | [diff] [blame] | 536 | struct fou_net *fn = net_generic(net, fou_net_id); |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 537 | struct fou *fout; |
| 538 | |
WANG Cong | 02d793c | 2015-04-10 12:00:29 -0700 | [diff] [blame] | 539 | mutex_lock(&fn->fou_lock); |
| 540 | list_for_each_entry(fout, &fn->fou_list, list) { |
Kristian Evensen | 1713cb3 | 2019-03-27 11:16:03 +0100 | [diff] [blame] | 541 | if (fou_cfg_cmp(fout, cfg)) { |
WANG Cong | 02d793c | 2015-04-10 12:00:29 -0700 | [diff] [blame] | 542 | mutex_unlock(&fn->fou_lock); |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 543 | return -EALREADY; |
| 544 | } |
| 545 | } |
| 546 | |
WANG Cong | 02d793c | 2015-04-10 12:00:29 -0700 | [diff] [blame] | 547 | list_add(&fou->list, &fn->fou_list); |
| 548 | mutex_unlock(&fn->fou_lock); |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 549 | |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | static void fou_release(struct fou *fou) |
| 554 | { |
| 555 | struct socket *sock = fou->sock; |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 556 | |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 557 | list_del(&fou->list); |
WANG Cong | 02d793c | 2015-04-10 12:00:29 -0700 | [diff] [blame] | 558 | udp_tunnel_sock_release(sock); |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 559 | |
Hannes Frederic Sowa | 3036fac | 2015-12-15 21:01:53 +0100 | [diff] [blame] | 560 | kfree_rcu(fou, rcu); |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | static int fou_create(struct net *net, struct fou_cfg *cfg, |
| 564 | struct socket **sockp) |
| 565 | { |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 566 | struct socket *sock = NULL; |
WANG Cong | 02d793c | 2015-04-10 12:00:29 -0700 | [diff] [blame] | 567 | struct fou *fou = NULL; |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 568 | struct sock *sk; |
Tom Herbert | 440924b | 2016-05-18 09:06:14 -0700 | [diff] [blame] | 569 | struct udp_tunnel_sock_cfg tunnel_cfg; |
WANG Cong | 02d793c | 2015-04-10 12:00:29 -0700 | [diff] [blame] | 570 | int err; |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 571 | |
| 572 | /* Open UDP socket */ |
| 573 | err = udp_sock_create(net, &cfg->udp_config, &sock); |
| 574 | if (err < 0) |
| 575 | goto error; |
| 576 | |
| 577 | /* Allocate FOU port structure */ |
| 578 | fou = kzalloc(sizeof(*fou), GFP_KERNEL); |
| 579 | if (!fou) { |
| 580 | err = -ENOMEM; |
| 581 | goto error; |
| 582 | } |
| 583 | |
| 584 | sk = sock->sk; |
| 585 | |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 586 | fou->port = cfg->udp_config.local_udp_port; |
Tom Herbert | 5f914b6 | 2016-05-18 09:06:16 -0700 | [diff] [blame] | 587 | fou->family = cfg->udp_config.family; |
| 588 | fou->flags = cfg->flags; |
Tom Herbert | 440924b | 2016-05-18 09:06:14 -0700 | [diff] [blame] | 589 | fou->type = cfg->type; |
| 590 | fou->sock = sock; |
| 591 | |
| 592 | memset(&tunnel_cfg, 0, sizeof(tunnel_cfg)); |
| 593 | tunnel_cfg.encap_type = 1; |
| 594 | tunnel_cfg.sk_user_data = fou; |
| 595 | tunnel_cfg.encap_destroy = NULL; |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 596 | |
| 597 | /* Initial for fou type */ |
| 598 | switch (cfg->type) { |
| 599 | case FOU_ENCAP_DIRECT: |
Tom Herbert | 440924b | 2016-05-18 09:06:14 -0700 | [diff] [blame] | 600 | tunnel_cfg.encap_rcv = fou_udp_recv; |
| 601 | tunnel_cfg.gro_receive = fou_gro_receive; |
| 602 | tunnel_cfg.gro_complete = fou_gro_complete; |
| 603 | fou->protocol = cfg->protocol; |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 604 | break; |
| 605 | case FOU_ENCAP_GUE: |
Tom Herbert | 440924b | 2016-05-18 09:06:14 -0700 | [diff] [blame] | 606 | tunnel_cfg.encap_rcv = gue_udp_recv; |
| 607 | tunnel_cfg.gro_receive = gue_gro_receive; |
| 608 | tunnel_cfg.gro_complete = gue_gro_complete; |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 609 | break; |
| 610 | default: |
| 611 | err = -EINVAL; |
| 612 | goto error; |
| 613 | } |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 614 | |
Tom Herbert | 440924b | 2016-05-18 09:06:14 -0700 | [diff] [blame] | 615 | setup_udp_tunnel_sock(net, sock, &tunnel_cfg); |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 616 | |
| 617 | sk->sk_allocation = GFP_ATOMIC; |
| 618 | |
Kristian Evensen | 1713cb3 | 2019-03-27 11:16:03 +0100 | [diff] [blame] | 619 | err = fou_add_to_port_list(net, fou, cfg); |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 620 | if (err) |
| 621 | goto error; |
| 622 | |
| 623 | if (sockp) |
| 624 | *sockp = sock; |
| 625 | |
| 626 | return 0; |
| 627 | |
| 628 | error: |
| 629 | kfree(fou); |
| 630 | if (sock) |
WANG Cong | 02d793c | 2015-04-10 12:00:29 -0700 | [diff] [blame] | 631 | udp_tunnel_sock_release(sock); |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 632 | |
| 633 | return err; |
| 634 | } |
| 635 | |
| 636 | static int fou_destroy(struct net *net, struct fou_cfg *cfg) |
| 637 | { |
WANG Cong | 02d793c | 2015-04-10 12:00:29 -0700 | [diff] [blame] | 638 | struct fou_net *fn = net_generic(net, fou_net_id); |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 639 | int err = -EINVAL; |
WANG Cong | 02d793c | 2015-04-10 12:00:29 -0700 | [diff] [blame] | 640 | struct fou *fou; |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 641 | |
WANG Cong | 02d793c | 2015-04-10 12:00:29 -0700 | [diff] [blame] | 642 | mutex_lock(&fn->fou_lock); |
| 643 | list_for_each_entry(fou, &fn->fou_list, list) { |
Kristian Evensen | 1713cb3 | 2019-03-27 11:16:03 +0100 | [diff] [blame] | 644 | if (fou_cfg_cmp(fou, cfg)) { |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 645 | fou_release(fou); |
| 646 | err = 0; |
| 647 | break; |
| 648 | } |
| 649 | } |
WANG Cong | 02d793c | 2015-04-10 12:00:29 -0700 | [diff] [blame] | 650 | mutex_unlock(&fn->fou_lock); |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 651 | |
| 652 | return err; |
| 653 | } |
| 654 | |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 655 | static struct genl_family fou_nl_family; |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 656 | |
stephen hemminger | 3f18ff2 | 2016-08-31 15:19:37 -0700 | [diff] [blame] | 657 | static const struct nla_policy fou_nl_policy[FOU_ATTR_MAX + 1] = { |
Kristian Evensen | 1713cb3 | 2019-03-27 11:16:03 +0100 | [diff] [blame] | 658 | [FOU_ATTR_PORT] = { .type = NLA_U16, }, |
| 659 | [FOU_ATTR_AF] = { .type = NLA_U8, }, |
| 660 | [FOU_ATTR_IPPROTO] = { .type = NLA_U8, }, |
| 661 | [FOU_ATTR_TYPE] = { .type = NLA_U8, }, |
| 662 | [FOU_ATTR_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG, }, |
| 663 | [FOU_ATTR_LOCAL_V4] = { .type = NLA_U32, }, |
| 664 | [FOU_ATTR_PEER_V4] = { .type = NLA_U32, }, |
Kristian Evensen | bb48eb9 | 2020-01-23 13:20:18 +0100 | [diff] [blame] | 665 | [FOU_ATTR_LOCAL_V6] = { .len = sizeof(struct in6_addr), }, |
| 666 | [FOU_ATTR_PEER_V6] = { .len = sizeof(struct in6_addr), }, |
Kristian Evensen | 1713cb3 | 2019-03-27 11:16:03 +0100 | [diff] [blame] | 667 | [FOU_ATTR_PEER_PORT] = { .type = NLA_U16, }, |
| 668 | [FOU_ATTR_IFINDEX] = { .type = NLA_S32, }, |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 669 | }; |
| 670 | |
| 671 | static int parse_nl_config(struct genl_info *info, |
| 672 | struct fou_cfg *cfg) |
| 673 | { |
Kristian Evensen | 1713cb3 | 2019-03-27 11:16:03 +0100 | [diff] [blame] | 674 | bool has_local = false, has_peer = false; |
| 675 | struct nlattr *attr; |
| 676 | int ifindex; |
| 677 | __be16 port; |
| 678 | |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 679 | memset(cfg, 0, sizeof(*cfg)); |
| 680 | |
| 681 | cfg->udp_config.family = AF_INET; |
| 682 | |
| 683 | if (info->attrs[FOU_ATTR_AF]) { |
| 684 | u8 family = nla_get_u8(info->attrs[FOU_ATTR_AF]); |
| 685 | |
Tom Herbert | 5f914b6 | 2016-05-18 09:06:16 -0700 | [diff] [blame] | 686 | switch (family) { |
| 687 | case AF_INET: |
| 688 | break; |
| 689 | case AF_INET6: |
| 690 | cfg->udp_config.ipv6_v6only = 1; |
| 691 | break; |
| 692 | default: |
| 693 | return -EAFNOSUPPORT; |
| 694 | } |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 695 | |
| 696 | cfg->udp_config.family = family; |
| 697 | } |
| 698 | |
| 699 | if (info->attrs[FOU_ATTR_PORT]) { |
Kristian Evensen | 1713cb3 | 2019-03-27 11:16:03 +0100 | [diff] [blame] | 700 | port = nla_get_be16(info->attrs[FOU_ATTR_PORT]); |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 701 | cfg->udp_config.local_udp_port = port; |
| 702 | } |
| 703 | |
| 704 | if (info->attrs[FOU_ATTR_IPPROTO]) |
| 705 | cfg->protocol = nla_get_u8(info->attrs[FOU_ATTR_IPPROTO]); |
| 706 | |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 707 | if (info->attrs[FOU_ATTR_TYPE]) |
| 708 | cfg->type = nla_get_u8(info->attrs[FOU_ATTR_TYPE]); |
| 709 | |
Tom Herbert | fe881ef | 2015-02-10 16:30:33 -0800 | [diff] [blame] | 710 | if (info->attrs[FOU_ATTR_REMCSUM_NOPARTIAL]) |
| 711 | cfg->flags |= FOU_F_REMCSUM_NOPARTIAL; |
| 712 | |
Kristian Evensen | 1713cb3 | 2019-03-27 11:16:03 +0100 | [diff] [blame] | 713 | if (cfg->udp_config.family == AF_INET) { |
| 714 | if (info->attrs[FOU_ATTR_LOCAL_V4]) { |
| 715 | attr = info->attrs[FOU_ATTR_LOCAL_V4]; |
| 716 | cfg->udp_config.local_ip.s_addr = nla_get_in_addr(attr); |
| 717 | has_local = true; |
| 718 | } |
| 719 | |
| 720 | if (info->attrs[FOU_ATTR_PEER_V4]) { |
| 721 | attr = info->attrs[FOU_ATTR_PEER_V4]; |
| 722 | cfg->udp_config.peer_ip.s_addr = nla_get_in_addr(attr); |
| 723 | has_peer = true; |
| 724 | } |
| 725 | #if IS_ENABLED(CONFIG_IPV6) |
| 726 | } else { |
| 727 | if (info->attrs[FOU_ATTR_LOCAL_V6]) { |
| 728 | attr = info->attrs[FOU_ATTR_LOCAL_V6]; |
| 729 | cfg->udp_config.local_ip6 = nla_get_in6_addr(attr); |
| 730 | has_local = true; |
| 731 | } |
| 732 | |
| 733 | if (info->attrs[FOU_ATTR_PEER_V6]) { |
| 734 | attr = info->attrs[FOU_ATTR_PEER_V6]; |
| 735 | cfg->udp_config.peer_ip6 = nla_get_in6_addr(attr); |
| 736 | has_peer = true; |
| 737 | } |
| 738 | #endif |
| 739 | } |
| 740 | |
| 741 | if (has_peer) { |
| 742 | if (info->attrs[FOU_ATTR_PEER_PORT]) { |
| 743 | port = nla_get_be16(info->attrs[FOU_ATTR_PEER_PORT]); |
| 744 | cfg->udp_config.peer_udp_port = port; |
| 745 | } else { |
| 746 | return -EINVAL; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | if (info->attrs[FOU_ATTR_IFINDEX]) { |
| 751 | if (!has_local) |
| 752 | return -EINVAL; |
| 753 | |
| 754 | ifindex = nla_get_s32(info->attrs[FOU_ATTR_IFINDEX]); |
| 755 | |
| 756 | cfg->udp_config.bind_ifindex = ifindex; |
| 757 | } |
| 758 | |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 759 | return 0; |
| 760 | } |
| 761 | |
| 762 | static int fou_nl_cmd_add_port(struct sk_buff *skb, struct genl_info *info) |
| 763 | { |
WANG Cong | 02d793c | 2015-04-10 12:00:29 -0700 | [diff] [blame] | 764 | struct net *net = genl_info_net(info); |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 765 | struct fou_cfg cfg; |
| 766 | int err; |
| 767 | |
| 768 | err = parse_nl_config(info, &cfg); |
| 769 | if (err) |
| 770 | return err; |
| 771 | |
WANG Cong | 02d793c | 2015-04-10 12:00:29 -0700 | [diff] [blame] | 772 | return fou_create(net, &cfg, NULL); |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | static int fou_nl_cmd_rm_port(struct sk_buff *skb, struct genl_info *info) |
| 776 | { |
WANG Cong | 02d793c | 2015-04-10 12:00:29 -0700 | [diff] [blame] | 777 | struct net *net = genl_info_net(info); |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 778 | struct fou_cfg cfg; |
WANG Cong | 6727063 | 2015-04-10 12:00:27 -0700 | [diff] [blame] | 779 | int err; |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 780 | |
WANG Cong | 6727063 | 2015-04-10 12:00:27 -0700 | [diff] [blame] | 781 | err = parse_nl_config(info, &cfg); |
| 782 | if (err) |
| 783 | return err; |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 784 | |
WANG Cong | 02d793c | 2015-04-10 12:00:29 -0700 | [diff] [blame] | 785 | return fou_destroy(net, &cfg); |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 786 | } |
| 787 | |
WANG Cong | 7a6c8c3 | 2015-04-10 12:00:30 -0700 | [diff] [blame] | 788 | static int fou_fill_info(struct fou *fou, struct sk_buff *msg) |
| 789 | { |
Kristian Evensen | 1713cb3 | 2019-03-27 11:16:03 +0100 | [diff] [blame] | 790 | struct sock *sk = fou->sock->sk; |
| 791 | |
WANG Cong | 7a6c8c3 | 2015-04-10 12:00:30 -0700 | [diff] [blame] | 792 | if (nla_put_u8(msg, FOU_ATTR_AF, fou->sock->sk->sk_family) || |
| 793 | nla_put_be16(msg, FOU_ATTR_PORT, fou->port) || |
Kristian Evensen | 1713cb3 | 2019-03-27 11:16:03 +0100 | [diff] [blame] | 794 | nla_put_be16(msg, FOU_ATTR_PEER_PORT, sk->sk_dport) || |
WANG Cong | 7a6c8c3 | 2015-04-10 12:00:30 -0700 | [diff] [blame] | 795 | nla_put_u8(msg, FOU_ATTR_IPPROTO, fou->protocol) || |
Kristian Evensen | 1713cb3 | 2019-03-27 11:16:03 +0100 | [diff] [blame] | 796 | nla_put_u8(msg, FOU_ATTR_TYPE, fou->type) || |
| 797 | nla_put_s32(msg, FOU_ATTR_IFINDEX, sk->sk_bound_dev_if)) |
WANG Cong | 7a6c8c3 | 2015-04-10 12:00:30 -0700 | [diff] [blame] | 798 | return -1; |
| 799 | |
| 800 | if (fou->flags & FOU_F_REMCSUM_NOPARTIAL) |
| 801 | if (nla_put_flag(msg, FOU_ATTR_REMCSUM_NOPARTIAL)) |
| 802 | return -1; |
Kristian Evensen | 1713cb3 | 2019-03-27 11:16:03 +0100 | [diff] [blame] | 803 | |
| 804 | if (fou->sock->sk->sk_family == AF_INET) { |
| 805 | if (nla_put_in_addr(msg, FOU_ATTR_LOCAL_V4, sk->sk_rcv_saddr)) |
| 806 | return -1; |
| 807 | |
| 808 | if (nla_put_in_addr(msg, FOU_ATTR_PEER_V4, sk->sk_daddr)) |
| 809 | return -1; |
| 810 | #if IS_ENABLED(CONFIG_IPV6) |
| 811 | } else { |
| 812 | if (nla_put_in6_addr(msg, FOU_ATTR_LOCAL_V6, |
| 813 | &sk->sk_v6_rcv_saddr)) |
| 814 | return -1; |
| 815 | |
| 816 | if (nla_put_in6_addr(msg, FOU_ATTR_PEER_V6, &sk->sk_v6_daddr)) |
| 817 | return -1; |
| 818 | #endif |
| 819 | } |
| 820 | |
WANG Cong | 7a6c8c3 | 2015-04-10 12:00:30 -0700 | [diff] [blame] | 821 | return 0; |
| 822 | } |
| 823 | |
| 824 | static int fou_dump_info(struct fou *fou, u32 portid, u32 seq, |
| 825 | u32 flags, struct sk_buff *skb, u8 cmd) |
| 826 | { |
| 827 | void *hdr; |
| 828 | |
| 829 | hdr = genlmsg_put(skb, portid, seq, &fou_nl_family, flags, cmd); |
| 830 | if (!hdr) |
| 831 | return -ENOMEM; |
| 832 | |
| 833 | if (fou_fill_info(fou, skb) < 0) |
| 834 | goto nla_put_failure; |
| 835 | |
| 836 | genlmsg_end(skb, hdr); |
| 837 | return 0; |
| 838 | |
| 839 | nla_put_failure: |
| 840 | genlmsg_cancel(skb, hdr); |
| 841 | return -EMSGSIZE; |
| 842 | } |
| 843 | |
| 844 | static int fou_nl_cmd_get_port(struct sk_buff *skb, struct genl_info *info) |
| 845 | { |
| 846 | struct net *net = genl_info_net(info); |
| 847 | struct fou_net *fn = net_generic(net, fou_net_id); |
| 848 | struct sk_buff *msg; |
| 849 | struct fou_cfg cfg; |
| 850 | struct fou *fout; |
| 851 | __be16 port; |
Tom Herbert | 5f914b6 | 2016-05-18 09:06:16 -0700 | [diff] [blame] | 852 | u8 family; |
WANG Cong | 7a6c8c3 | 2015-04-10 12:00:30 -0700 | [diff] [blame] | 853 | int ret; |
| 854 | |
| 855 | ret = parse_nl_config(info, &cfg); |
| 856 | if (ret) |
| 857 | return ret; |
| 858 | port = cfg.udp_config.local_udp_port; |
| 859 | if (port == 0) |
| 860 | return -EINVAL; |
| 861 | |
Tom Herbert | 5f914b6 | 2016-05-18 09:06:16 -0700 | [diff] [blame] | 862 | family = cfg.udp_config.family; |
| 863 | if (family != AF_INET && family != AF_INET6) |
| 864 | return -EINVAL; |
| 865 | |
WANG Cong | 7a6c8c3 | 2015-04-10 12:00:30 -0700 | [diff] [blame] | 866 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 867 | if (!msg) |
| 868 | return -ENOMEM; |
| 869 | |
| 870 | ret = -ESRCH; |
| 871 | mutex_lock(&fn->fou_lock); |
| 872 | list_for_each_entry(fout, &fn->fou_list, list) { |
Kristian Evensen | 1713cb3 | 2019-03-27 11:16:03 +0100 | [diff] [blame] | 873 | if (fou_cfg_cmp(fout, &cfg)) { |
WANG Cong | 7a6c8c3 | 2015-04-10 12:00:30 -0700 | [diff] [blame] | 874 | ret = fou_dump_info(fout, info->snd_portid, |
| 875 | info->snd_seq, 0, msg, |
| 876 | info->genlhdr->cmd); |
| 877 | break; |
| 878 | } |
| 879 | } |
| 880 | mutex_unlock(&fn->fou_lock); |
| 881 | if (ret < 0) |
| 882 | goto out_free; |
| 883 | |
| 884 | return genlmsg_reply(msg, info); |
| 885 | |
| 886 | out_free: |
| 887 | nlmsg_free(msg); |
| 888 | return ret; |
| 889 | } |
| 890 | |
| 891 | static int fou_nl_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 892 | { |
| 893 | struct net *net = sock_net(skb->sk); |
| 894 | struct fou_net *fn = net_generic(net, fou_net_id); |
| 895 | struct fou *fout; |
| 896 | int idx = 0, ret; |
| 897 | |
| 898 | mutex_lock(&fn->fou_lock); |
| 899 | list_for_each_entry(fout, &fn->fou_list, list) { |
| 900 | if (idx++ < cb->args[0]) |
| 901 | continue; |
| 902 | ret = fou_dump_info(fout, NETLINK_CB(cb->skb).portid, |
| 903 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 904 | skb, FOU_CMD_GET); |
| 905 | if (ret) |
WANG Cong | 540207a | 2015-04-15 11:48:49 -0700 | [diff] [blame] | 906 | break; |
WANG Cong | 7a6c8c3 | 2015-04-10 12:00:30 -0700 | [diff] [blame] | 907 | } |
| 908 | mutex_unlock(&fn->fou_lock); |
| 909 | |
WANG Cong | 7a6c8c3 | 2015-04-10 12:00:30 -0700 | [diff] [blame] | 910 | cb->args[0] = idx; |
| 911 | return skb->len; |
| 912 | } |
| 913 | |
Jakub Kicinski | 66a9b92 | 2020-10-02 14:49:54 -0700 | [diff] [blame] | 914 | static const struct genl_small_ops fou_nl_ops[] = { |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 915 | { |
| 916 | .cmd = FOU_CMD_ADD, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 917 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 918 | .doit = fou_nl_cmd_add_port, |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 919 | .flags = GENL_ADMIN_PERM, |
| 920 | }, |
| 921 | { |
| 922 | .cmd = FOU_CMD_DEL, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 923 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 924 | .doit = fou_nl_cmd_rm_port, |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 925 | .flags = GENL_ADMIN_PERM, |
| 926 | }, |
WANG Cong | 7a6c8c3 | 2015-04-10 12:00:30 -0700 | [diff] [blame] | 927 | { |
| 928 | .cmd = FOU_CMD_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 929 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
WANG Cong | 7a6c8c3 | 2015-04-10 12:00:30 -0700 | [diff] [blame] | 930 | .doit = fou_nl_cmd_get_port, |
| 931 | .dumpit = fou_nl_dump, |
WANG Cong | 7a6c8c3 | 2015-04-10 12:00:30 -0700 | [diff] [blame] | 932 | }, |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 933 | }; |
| 934 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 935 | static struct genl_family fou_nl_family __ro_after_init = { |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 936 | .hdrsize = 0, |
| 937 | .name = FOU_GENL_NAME, |
| 938 | .version = FOU_GENL_VERSION, |
| 939 | .maxattr = FOU_ATTR_MAX, |
Johannes Berg | 3b0f31f | 2019-03-21 22:51:02 +0100 | [diff] [blame] | 940 | .policy = fou_nl_policy, |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 941 | .netnsok = true, |
| 942 | .module = THIS_MODULE, |
Jakub Kicinski | 66a9b92 | 2020-10-02 14:49:54 -0700 | [diff] [blame] | 943 | .small_ops = fou_nl_ops, |
| 944 | .n_small_ops = ARRAY_SIZE(fou_nl_ops), |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 945 | }; |
| 946 | |
Tom Herbert | a8c5f90 | 2014-11-12 11:54:09 -0800 | [diff] [blame] | 947 | size_t fou_encap_hlen(struct ip_tunnel_encap *e) |
| 948 | { |
| 949 | return sizeof(struct udphdr); |
| 950 | } |
| 951 | EXPORT_SYMBOL(fou_encap_hlen); |
| 952 | |
| 953 | size_t gue_encap_hlen(struct ip_tunnel_encap *e) |
| 954 | { |
| 955 | size_t len; |
| 956 | bool need_priv = false; |
| 957 | |
| 958 | len = sizeof(struct udphdr) + sizeof(struct guehdr); |
| 959 | |
| 960 | if (e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) { |
| 961 | len += GUE_PLEN_REMCSUM; |
| 962 | need_priv = true; |
| 963 | } |
| 964 | |
| 965 | len += need_priv ? GUE_LEN_PRIV : 0; |
| 966 | |
| 967 | return len; |
| 968 | } |
| 969 | EXPORT_SYMBOL(gue_encap_hlen); |
| 970 | |
Tom Herbert | dc969b8 | 2016-05-18 09:06:15 -0700 | [diff] [blame] | 971 | int __fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e, |
| 972 | u8 *protocol, __be16 *sport, int type) |
| 973 | { |
| 974 | int err; |
| 975 | |
| 976 | err = iptunnel_handle_offloads(skb, type); |
| 977 | if (err) |
| 978 | return err; |
| 979 | |
| 980 | *sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev), |
| 981 | skb, 0, 0, false); |
| 982 | |
| 983 | return 0; |
| 984 | } |
| 985 | EXPORT_SYMBOL(__fou_build_header); |
| 986 | |
Tom Herbert | dc969b8 | 2016-05-18 09:06:15 -0700 | [diff] [blame] | 987 | int __gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e, |
| 988 | u8 *protocol, __be16 *sport, int type) |
Tom Herbert | 63487ba | 2014-11-04 09:06:51 -0800 | [diff] [blame] | 989 | { |
Tom Herbert | 63487ba | 2014-11-04 09:06:51 -0800 | [diff] [blame] | 990 | struct guehdr *guehdr; |
Tom Herbert | b17f709 | 2014-11-04 09:06:56 -0800 | [diff] [blame] | 991 | size_t hdrlen, optlen = 0; |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 992 | void *data; |
| 993 | bool need_priv = false; |
Alexander Duyck | aed069d | 2016-04-14 15:33:37 -0400 | [diff] [blame] | 994 | int err; |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 995 | |
Tom Herbert | b17f709 | 2014-11-04 09:06:56 -0800 | [diff] [blame] | 996 | if ((e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) && |
| 997 | skb->ip_summed == CHECKSUM_PARTIAL) { |
Tom Herbert | b17f709 | 2014-11-04 09:06:56 -0800 | [diff] [blame] | 998 | optlen += GUE_PLEN_REMCSUM; |
| 999 | type |= SKB_GSO_TUNNEL_REMCSUM; |
| 1000 | need_priv = true; |
| 1001 | } |
| 1002 | |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 1003 | optlen += need_priv ? GUE_LEN_PRIV : 0; |
Tom Herbert | 63487ba | 2014-11-04 09:06:51 -0800 | [diff] [blame] | 1004 | |
Alexander Duyck | aed069d | 2016-04-14 15:33:37 -0400 | [diff] [blame] | 1005 | err = iptunnel_handle_offloads(skb, type); |
| 1006 | if (err) |
| 1007 | return err; |
Tom Herbert | 63487ba | 2014-11-04 09:06:51 -0800 | [diff] [blame] | 1008 | |
| 1009 | /* Get source port (based on flow hash) before skb_push */ |
Tom Herbert | dc969b8 | 2016-05-18 09:06:15 -0700 | [diff] [blame] | 1010 | *sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev), |
| 1011 | skb, 0, 0, false); |
Tom Herbert | 63487ba | 2014-11-04 09:06:51 -0800 | [diff] [blame] | 1012 | |
Tom Herbert | b17f709 | 2014-11-04 09:06:56 -0800 | [diff] [blame] | 1013 | hdrlen = sizeof(struct guehdr) + optlen; |
| 1014 | |
| 1015 | skb_push(skb, hdrlen); |
Tom Herbert | 63487ba | 2014-11-04 09:06:51 -0800 | [diff] [blame] | 1016 | |
| 1017 | guehdr = (struct guehdr *)skb->data; |
| 1018 | |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 1019 | guehdr->control = 0; |
Tom Herbert | 63487ba | 2014-11-04 09:06:51 -0800 | [diff] [blame] | 1020 | guehdr->version = 0; |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 1021 | guehdr->hlen = optlen >> 2; |
Tom Herbert | 63487ba | 2014-11-04 09:06:51 -0800 | [diff] [blame] | 1022 | guehdr->flags = 0; |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 1023 | guehdr->proto_ctype = *protocol; |
| 1024 | |
| 1025 | data = &guehdr[1]; |
| 1026 | |
| 1027 | if (need_priv) { |
| 1028 | __be32 *flags = data; |
| 1029 | |
| 1030 | guehdr->flags |= GUE_FLAG_PRIV; |
| 1031 | *flags = 0; |
| 1032 | data += GUE_LEN_PRIV; |
| 1033 | |
Tom Herbert | b17f709 | 2014-11-04 09:06:56 -0800 | [diff] [blame] | 1034 | if (type & SKB_GSO_TUNNEL_REMCSUM) { |
| 1035 | u16 csum_start = skb_checksum_start_offset(skb); |
| 1036 | __be16 *pd = data; |
| 1037 | |
| 1038 | if (csum_start < hdrlen) |
| 1039 | return -EINVAL; |
| 1040 | |
| 1041 | csum_start -= hdrlen; |
| 1042 | pd[0] = htons(csum_start); |
| 1043 | pd[1] = htons(csum_start + skb->csum_offset); |
| 1044 | |
| 1045 | if (!skb_is_gso(skb)) { |
| 1046 | skb->ip_summed = CHECKSUM_NONE; |
| 1047 | skb->encapsulation = 0; |
| 1048 | } |
| 1049 | |
| 1050 | *flags |= GUE_PFLAG_REMCSUM; |
| 1051 | data += GUE_PLEN_REMCSUM; |
| 1052 | } |
| 1053 | |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 1054 | } |
Tom Herbert | 63487ba | 2014-11-04 09:06:51 -0800 | [diff] [blame] | 1055 | |
Tom Herbert | dc969b8 | 2016-05-18 09:06:15 -0700 | [diff] [blame] | 1056 | return 0; |
| 1057 | } |
| 1058 | EXPORT_SYMBOL(__gue_build_header); |
| 1059 | |
stephen hemminger | 9dc621a | 2017-05-19 09:55:54 -0700 | [diff] [blame] | 1060 | #ifdef CONFIG_NET_FOU_IP_TUNNELS |
| 1061 | |
| 1062 | static void fou_build_udp(struct sk_buff *skb, struct ip_tunnel_encap *e, |
| 1063 | struct flowi4 *fl4, u8 *protocol, __be16 sport) |
| 1064 | { |
| 1065 | struct udphdr *uh; |
| 1066 | |
| 1067 | skb_push(skb, sizeof(struct udphdr)); |
| 1068 | skb_reset_transport_header(skb); |
| 1069 | |
| 1070 | uh = udp_hdr(skb); |
| 1071 | |
| 1072 | uh->dest = e->dport; |
| 1073 | uh->source = sport; |
| 1074 | uh->len = htons(skb->len); |
| 1075 | udp_set_csum(!(e->flags & TUNNEL_ENCAP_FLAG_CSUM), skb, |
| 1076 | fl4->saddr, fl4->daddr, skb->len); |
| 1077 | |
| 1078 | *protocol = IPPROTO_UDP; |
| 1079 | } |
| 1080 | |
| 1081 | static int fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e, |
| 1082 | u8 *protocol, struct flowi4 *fl4) |
| 1083 | { |
| 1084 | int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM : |
| 1085 | SKB_GSO_UDP_TUNNEL; |
| 1086 | __be16 sport; |
| 1087 | int err; |
| 1088 | |
| 1089 | err = __fou_build_header(skb, e, protocol, &sport, type); |
| 1090 | if (err) |
| 1091 | return err; |
| 1092 | |
| 1093 | fou_build_udp(skb, e, fl4, protocol, sport); |
| 1094 | |
| 1095 | return 0; |
| 1096 | } |
| 1097 | |
| 1098 | static int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e, |
| 1099 | u8 *protocol, struct flowi4 *fl4) |
Tom Herbert | dc969b8 | 2016-05-18 09:06:15 -0700 | [diff] [blame] | 1100 | { |
| 1101 | int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM : |
| 1102 | SKB_GSO_UDP_TUNNEL; |
| 1103 | __be16 sport; |
| 1104 | int err; |
| 1105 | |
| 1106 | err = __gue_build_header(skb, e, protocol, &sport, type); |
| 1107 | if (err) |
| 1108 | return err; |
| 1109 | |
Tom Herbert | 63487ba | 2014-11-04 09:06:51 -0800 | [diff] [blame] | 1110 | fou_build_udp(skb, e, fl4, protocol, sport); |
| 1111 | |
| 1112 | return 0; |
| 1113 | } |
Tom Herbert | 63487ba | 2014-11-04 09:06:51 -0800 | [diff] [blame] | 1114 | |
Stefano Brivio | b8a51b3 | 2018-11-08 12:19:23 +0100 | [diff] [blame] | 1115 | static int gue_err_proto_handler(int proto, struct sk_buff *skb, u32 info) |
| 1116 | { |
| 1117 | const struct net_protocol *ipprot = rcu_dereference(inet_protos[proto]); |
| 1118 | |
| 1119 | if (ipprot && ipprot->err_handler) { |
| 1120 | if (!ipprot->err_handler(skb, info)) |
| 1121 | return 0; |
| 1122 | } |
| 1123 | |
| 1124 | return -ENOENT; |
| 1125 | } |
| 1126 | |
| 1127 | static int gue_err(struct sk_buff *skb, u32 info) |
| 1128 | { |
| 1129 | int transport_offset = skb_transport_offset(skb); |
| 1130 | struct guehdr *guehdr; |
Eric Dumazet | 26fc181 | 2019-01-11 06:27:35 -0800 | [diff] [blame] | 1131 | size_t len, optlen; |
Stefano Brivio | b8a51b3 | 2018-11-08 12:19:23 +0100 | [diff] [blame] | 1132 | int ret; |
| 1133 | |
Eric Dumazet | 26fc181 | 2019-01-11 06:27:35 -0800 | [diff] [blame] | 1134 | len = sizeof(struct udphdr) + sizeof(struct guehdr); |
Eric Dumazet | 5355ed6 | 2019-03-06 10:41:00 -0800 | [diff] [blame] | 1135 | if (!pskb_may_pull(skb, transport_offset + len)) |
Stefano Brivio | b8a51b3 | 2018-11-08 12:19:23 +0100 | [diff] [blame] | 1136 | return -EINVAL; |
| 1137 | |
| 1138 | guehdr = (struct guehdr *)&udp_hdr(skb)[1]; |
| 1139 | |
| 1140 | switch (guehdr->version) { |
| 1141 | case 0: /* Full GUE header present */ |
| 1142 | break; |
| 1143 | case 1: { |
Simon Horman | c9d52f2 | 2019-04-09 09:59:07 +0200 | [diff] [blame] | 1144 | /* Direct encapsulation of IPv4 or IPv6 */ |
Stefano Brivio | b8a51b3 | 2018-11-08 12:19:23 +0100 | [diff] [blame] | 1145 | skb_set_transport_header(skb, -(int)sizeof(struct icmphdr)); |
| 1146 | |
| 1147 | switch (((struct iphdr *)guehdr)->version) { |
| 1148 | case 4: |
| 1149 | ret = gue_err_proto_handler(IPPROTO_IPIP, skb, info); |
| 1150 | goto out; |
| 1151 | #if IS_ENABLED(CONFIG_IPV6) |
| 1152 | case 6: |
| 1153 | ret = gue_err_proto_handler(IPPROTO_IPV6, skb, info); |
| 1154 | goto out; |
| 1155 | #endif |
| 1156 | default: |
| 1157 | ret = -EOPNOTSUPP; |
| 1158 | goto out; |
| 1159 | } |
| 1160 | } |
| 1161 | default: /* Undefined version */ |
| 1162 | return -EOPNOTSUPP; |
| 1163 | } |
| 1164 | |
| 1165 | if (guehdr->control) |
| 1166 | return -ENOENT; |
| 1167 | |
| 1168 | optlen = guehdr->hlen << 2; |
| 1169 | |
Eric Dumazet | 5355ed6 | 2019-03-06 10:41:00 -0800 | [diff] [blame] | 1170 | if (!pskb_may_pull(skb, transport_offset + len + optlen)) |
Eric Dumazet | 26fc181 | 2019-01-11 06:27:35 -0800 | [diff] [blame] | 1171 | return -EINVAL; |
| 1172 | |
| 1173 | guehdr = (struct guehdr *)&udp_hdr(skb)[1]; |
Stefano Brivio | b8a51b3 | 2018-11-08 12:19:23 +0100 | [diff] [blame] | 1174 | if (validate_gue_flags(guehdr, optlen)) |
| 1175 | return -EINVAL; |
| 1176 | |
Stefano Brivio | 1178903 | 2018-12-18 00:13:17 +0100 | [diff] [blame] | 1177 | /* Handling exceptions for direct UDP encapsulation in GUE would lead to |
| 1178 | * recursion. Besides, this kind of encapsulation can't even be |
| 1179 | * configured currently. Discard this. |
| 1180 | */ |
Stefano Brivio | bc6e019 | 2019-01-03 21:43:34 +0100 | [diff] [blame] | 1181 | if (guehdr->proto_ctype == IPPROTO_UDP || |
| 1182 | guehdr->proto_ctype == IPPROTO_UDPLITE) |
Stefano Brivio | 1178903 | 2018-12-18 00:13:17 +0100 | [diff] [blame] | 1183 | return -EOPNOTSUPP; |
| 1184 | |
Stefano Brivio | b8a51b3 | 2018-11-08 12:19:23 +0100 | [diff] [blame] | 1185 | skb_set_transport_header(skb, -(int)sizeof(struct icmphdr)); |
| 1186 | ret = gue_err_proto_handler(guehdr->proto_ctype, skb, info); |
| 1187 | |
| 1188 | out: |
| 1189 | skb_set_transport_header(skb, transport_offset); |
| 1190 | return ret; |
| 1191 | } |
| 1192 | |
Tom Herbert | a8c5f90 | 2014-11-12 11:54:09 -0800 | [diff] [blame] | 1193 | |
Andi Kleen | 5eeb292 | 2015-04-08 06:04:31 -0700 | [diff] [blame] | 1194 | static const struct ip_tunnel_encap_ops fou_iptun_ops = { |
Tom Herbert | a8c5f90 | 2014-11-12 11:54:09 -0800 | [diff] [blame] | 1195 | .encap_hlen = fou_encap_hlen, |
| 1196 | .build_header = fou_build_header, |
Stefano Brivio | b8a51b3 | 2018-11-08 12:19:23 +0100 | [diff] [blame] | 1197 | .err_handler = gue_err, |
Tom Herbert | a8c5f90 | 2014-11-12 11:54:09 -0800 | [diff] [blame] | 1198 | }; |
| 1199 | |
Andi Kleen | 5eeb292 | 2015-04-08 06:04:31 -0700 | [diff] [blame] | 1200 | static const struct ip_tunnel_encap_ops gue_iptun_ops = { |
Tom Herbert | a8c5f90 | 2014-11-12 11:54:09 -0800 | [diff] [blame] | 1201 | .encap_hlen = gue_encap_hlen, |
| 1202 | .build_header = gue_build_header, |
Stefano Brivio | b8a51b3 | 2018-11-08 12:19:23 +0100 | [diff] [blame] | 1203 | .err_handler = gue_err, |
Tom Herbert | a8c5f90 | 2014-11-12 11:54:09 -0800 | [diff] [blame] | 1204 | }; |
| 1205 | |
| 1206 | static int ip_tunnel_encap_add_fou_ops(void) |
| 1207 | { |
| 1208 | int ret; |
| 1209 | |
| 1210 | ret = ip_tunnel_encap_add_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU); |
| 1211 | if (ret < 0) { |
| 1212 | pr_err("can't add fou ops\n"); |
| 1213 | return ret; |
| 1214 | } |
| 1215 | |
| 1216 | ret = ip_tunnel_encap_add_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE); |
| 1217 | if (ret < 0) { |
| 1218 | pr_err("can't add gue ops\n"); |
| 1219 | ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU); |
| 1220 | return ret; |
| 1221 | } |
| 1222 | |
| 1223 | return 0; |
| 1224 | } |
| 1225 | |
| 1226 | static void ip_tunnel_encap_del_fou_ops(void) |
| 1227 | { |
| 1228 | ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU); |
| 1229 | ip_tunnel_encap_del_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE); |
| 1230 | } |
| 1231 | |
| 1232 | #else |
| 1233 | |
| 1234 | static int ip_tunnel_encap_add_fou_ops(void) |
| 1235 | { |
| 1236 | return 0; |
| 1237 | } |
| 1238 | |
Thomas Graf | 882288c | 2014-11-13 12:48:21 +0100 | [diff] [blame] | 1239 | static void ip_tunnel_encap_del_fou_ops(void) |
Tom Herbert | a8c5f90 | 2014-11-12 11:54:09 -0800 | [diff] [blame] | 1240 | { |
| 1241 | } |
| 1242 | |
| 1243 | #endif |
| 1244 | |
WANG Cong | 02d793c | 2015-04-10 12:00:29 -0700 | [diff] [blame] | 1245 | static __net_init int fou_init_net(struct net *net) |
| 1246 | { |
| 1247 | struct fou_net *fn = net_generic(net, fou_net_id); |
| 1248 | |
| 1249 | INIT_LIST_HEAD(&fn->fou_list); |
| 1250 | mutex_init(&fn->fou_lock); |
| 1251 | return 0; |
| 1252 | } |
| 1253 | |
| 1254 | static __net_exit void fou_exit_net(struct net *net) |
| 1255 | { |
| 1256 | struct fou_net *fn = net_generic(net, fou_net_id); |
| 1257 | struct fou *fou, *next; |
| 1258 | |
| 1259 | /* Close all the FOU sockets */ |
| 1260 | mutex_lock(&fn->fou_lock); |
| 1261 | list_for_each_entry_safe(fou, next, &fn->fou_list, list) |
| 1262 | fou_release(fou); |
| 1263 | mutex_unlock(&fn->fou_lock); |
| 1264 | } |
| 1265 | |
| 1266 | static struct pernet_operations fou_net_ops = { |
| 1267 | .init = fou_init_net, |
| 1268 | .exit = fou_exit_net, |
| 1269 | .id = &fou_net_id, |
| 1270 | .size = sizeof(struct fou_net), |
| 1271 | }; |
| 1272 | |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 1273 | static int __init fou_init(void) |
| 1274 | { |
| 1275 | int ret; |
| 1276 | |
WANG Cong | 02d793c | 2015-04-10 12:00:29 -0700 | [diff] [blame] | 1277 | ret = register_pernet_device(&fou_net_ops); |
| 1278 | if (ret) |
Tom Herbert | a8c5f90 | 2014-11-12 11:54:09 -0800 | [diff] [blame] | 1279 | goto exit; |
| 1280 | |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 1281 | ret = genl_register_family(&fou_nl_family); |
Tom Herbert | a8c5f90 | 2014-11-12 11:54:09 -0800 | [diff] [blame] | 1282 | if (ret < 0) |
WANG Cong | 02d793c | 2015-04-10 12:00:29 -0700 | [diff] [blame] | 1283 | goto unregister; |
Tom Herbert | a8c5f90 | 2014-11-12 11:54:09 -0800 | [diff] [blame] | 1284 | |
WANG Cong | 02d793c | 2015-04-10 12:00:29 -0700 | [diff] [blame] | 1285 | ret = ip_tunnel_encap_add_fou_ops(); |
| 1286 | if (ret == 0) |
| 1287 | return 0; |
| 1288 | |
| 1289 | genl_unregister_family(&fou_nl_family); |
| 1290 | unregister: |
| 1291 | unregister_pernet_device(&fou_net_ops); |
Tom Herbert | a8c5f90 | 2014-11-12 11:54:09 -0800 | [diff] [blame] | 1292 | exit: |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 1293 | return ret; |
| 1294 | } |
| 1295 | |
| 1296 | static void __exit fou_fini(void) |
| 1297 | { |
Tom Herbert | a8c5f90 | 2014-11-12 11:54:09 -0800 | [diff] [blame] | 1298 | ip_tunnel_encap_del_fou_ops(); |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 1299 | genl_unregister_family(&fou_nl_family); |
WANG Cong | 02d793c | 2015-04-10 12:00:29 -0700 | [diff] [blame] | 1300 | unregister_pernet_device(&fou_net_ops); |
Tom Herbert | 23461551 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | module_init(fou_init); |
| 1304 | module_exit(fou_fini); |
| 1305 | MODULE_AUTHOR("Tom Herbert <therbert@google.com>"); |
| 1306 | MODULE_LICENSE("GPL"); |
Rob Gill | 67c20de | 2020-06-20 02:08:25 +0000 | [diff] [blame] | 1307 | MODULE_DESCRIPTION("Foo over UDP"); |