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