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