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