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