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 | |
| 19 | static DEFINE_SPINLOCK(fou_lock); |
| 20 | static LIST_HEAD(fou_list); |
| 21 | |
| 22 | struct fou { |
| 23 | struct socket *sock; |
| 24 | u8 protocol; |
Tom Herbert | fe881ef | 2015-02-10 16:30:33 -0800 | [diff] [blame] | 25 | u8 flags; |
Tom Herbert | 2346155 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 26 | u16 port; |
Tom Herbert | afe9332 | 2014-09-17 12:25:57 -0700 | [diff] [blame] | 27 | struct udp_offload udp_offloads; |
Tom Herbert | 2346155 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 28 | struct list_head list; |
| 29 | }; |
| 30 | |
Tom Herbert | fe881ef | 2015-02-10 16:30:33 -0800 | [diff] [blame] | 31 | #define FOU_F_REMCSUM_NOPARTIAL BIT(0) |
| 32 | |
Tom Herbert | 2346155 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 33 | struct fou_cfg { |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 34 | u16 type; |
Tom Herbert | 2346155 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 35 | u8 protocol; |
Tom Herbert | fe881ef | 2015-02-10 16:30:33 -0800 | [diff] [blame] | 36 | u8 flags; |
Tom Herbert | 2346155 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 37 | struct udp_port_cfg udp_config; |
| 38 | }; |
| 39 | |
| 40 | static inline struct fou *fou_from_sock(struct sock *sk) |
| 41 | { |
| 42 | return sk->sk_user_data; |
| 43 | } |
| 44 | |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 45 | static void fou_recv_pull(struct sk_buff *skb, size_t len) |
Tom Herbert | 2346155 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 46 | { |
| 47 | struct iphdr *iph = ip_hdr(skb); |
| 48 | |
| 49 | /* Remove 'len' bytes from the packet (UDP header and |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 50 | * FOU header if present). |
Tom Herbert | 2346155 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 51 | */ |
| 52 | iph->tot_len = htons(ntohs(iph->tot_len) - len); |
| 53 | __skb_pull(skb, len); |
| 54 | skb_postpull_rcsum(skb, udp_hdr(skb), len); |
| 55 | skb_reset_transport_header(skb); |
Tom Herbert | 2346155 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static int fou_udp_recv(struct sock *sk, struct sk_buff *skb) |
| 59 | { |
| 60 | struct fou *fou = fou_from_sock(sk); |
| 61 | |
| 62 | if (!fou) |
| 63 | return 1; |
| 64 | |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 65 | fou_recv_pull(skb, sizeof(struct udphdr)); |
| 66 | |
| 67 | return -fou->protocol; |
| 68 | } |
| 69 | |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 70 | static struct guehdr *gue_remcsum(struct sk_buff *skb, struct guehdr *guehdr, |
Tom Herbert | fe881ef | 2015-02-10 16:30:33 -0800 | [diff] [blame] | 71 | void *data, size_t hdrlen, u8 ipproto, |
| 72 | bool nopartial) |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 73 | { |
| 74 | __be16 *pd = data; |
Tom Herbert | 4fd671d | 2014-11-25 11:21:20 -0800 | [diff] [blame] | 75 | size_t start = ntohs(pd[0]); |
| 76 | size_t offset = ntohs(pd[1]); |
| 77 | size_t plen = hdrlen + max_t(size_t, offset + sizeof(u16), start); |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 78 | |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 79 | if (!pskb_may_pull(skb, plen)) |
| 80 | return NULL; |
| 81 | guehdr = (struct guehdr *)&udp_hdr(skb)[1]; |
| 82 | |
Tom Herbert | fe881ef | 2015-02-10 16:30:33 -0800 | [diff] [blame] | 83 | skb_remcsum_process(skb, (void *)guehdr + hdrlen, |
| 84 | start, offset, nopartial); |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 85 | |
| 86 | return guehdr; |
| 87 | } |
| 88 | |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 89 | static int gue_control_message(struct sk_buff *skb, struct guehdr *guehdr) |
| 90 | { |
| 91 | /* No support yet */ |
| 92 | kfree_skb(skb); |
| 93 | return 0; |
Tom Herbert | 2346155 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 96 | static int gue_udp_recv(struct sock *sk, struct sk_buff *skb) |
| 97 | { |
| 98 | struct fou *fou = fou_from_sock(sk); |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 99 | size_t len, optlen, hdrlen; |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 100 | struct guehdr *guehdr; |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 101 | void *data; |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 102 | u16 doffset = 0; |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 103 | |
| 104 | if (!fou) |
| 105 | return 1; |
| 106 | |
| 107 | len = sizeof(struct udphdr) + sizeof(struct guehdr); |
| 108 | if (!pskb_may_pull(skb, len)) |
| 109 | goto drop; |
| 110 | |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 111 | guehdr = (struct guehdr *)&udp_hdr(skb)[1]; |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 112 | |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 113 | optlen = guehdr->hlen << 2; |
| 114 | len += optlen; |
| 115 | |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 116 | if (!pskb_may_pull(skb, len)) |
| 117 | goto drop; |
| 118 | |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 119 | /* guehdr may change after pull */ |
| 120 | guehdr = (struct guehdr *)&udp_hdr(skb)[1]; |
Li RongQing | d8f00d2 | 2014-10-17 16:53:47 +0800 | [diff] [blame] | 121 | |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 122 | hdrlen = sizeof(struct guehdr) + optlen; |
| 123 | |
| 124 | if (guehdr->version != 0 || validate_gue_flags(guehdr, optlen)) |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 125 | goto drop; |
| 126 | |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 127 | hdrlen = sizeof(struct guehdr) + optlen; |
| 128 | |
| 129 | ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len); |
| 130 | |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 131 | /* Pull csum through the guehdr now . This can be used if |
| 132 | * there is a remote checksum offload. |
| 133 | */ |
| 134 | skb_postpull_rcsum(skb, udp_hdr(skb), len); |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 135 | |
| 136 | data = &guehdr[1]; |
| 137 | |
| 138 | if (guehdr->flags & GUE_FLAG_PRIV) { |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 139 | __be32 flags = *(__be32 *)(data + doffset); |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 140 | |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 141 | doffset += GUE_LEN_PRIV; |
| 142 | |
| 143 | if (flags & GUE_PFLAG_REMCSUM) { |
| 144 | guehdr = gue_remcsum(skb, guehdr, data + doffset, |
Tom Herbert | fe881ef | 2015-02-10 16:30:33 -0800 | [diff] [blame] | 145 | hdrlen, guehdr->proto_ctype, |
| 146 | !!(fou->flags & |
| 147 | FOU_F_REMCSUM_NOPARTIAL)); |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 148 | if (!guehdr) |
| 149 | goto drop; |
| 150 | |
| 151 | data = &guehdr[1]; |
| 152 | |
| 153 | doffset += GUE_PLEN_REMCSUM; |
| 154 | } |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 157 | if (unlikely(guehdr->control)) |
| 158 | return gue_control_message(skb, guehdr); |
| 159 | |
Tom Herbert | 4fd671d | 2014-11-25 11:21:20 -0800 | [diff] [blame] | 160 | __skb_pull(skb, sizeof(struct udphdr) + hdrlen); |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 161 | skb_reset_transport_header(skb); |
| 162 | |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 163 | return -guehdr->proto_ctype; |
| 164 | |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 165 | drop: |
| 166 | kfree_skb(skb); |
| 167 | return 0; |
| 168 | } |
| 169 | |
Tom Herbert | afe9332 | 2014-09-17 12:25:57 -0700 | [diff] [blame] | 170 | static struct sk_buff **fou_gro_receive(struct sk_buff **head, |
Tom Herbert | a2b12f3 | 2015-01-12 17:00:37 -0800 | [diff] [blame] | 171 | struct sk_buff *skb, |
| 172 | struct udp_offload *uoff) |
Tom Herbert | afe9332 | 2014-09-17 12:25:57 -0700 | [diff] [blame] | 173 | { |
| 174 | const struct net_offload *ops; |
| 175 | struct sk_buff **pp = NULL; |
| 176 | u8 proto = NAPI_GRO_CB(skb)->proto; |
Tom Herbert | efc98d0 | 2014-10-03 15:48:08 -0700 | [diff] [blame] | 177 | const struct net_offload **offloads; |
Tom Herbert | afe9332 | 2014-09-17 12:25:57 -0700 | [diff] [blame] | 178 | |
| 179 | rcu_read_lock(); |
Tom Herbert | efc98d0 | 2014-10-03 15:48:08 -0700 | [diff] [blame] | 180 | offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads; |
Tom Herbert | afe9332 | 2014-09-17 12:25:57 -0700 | [diff] [blame] | 181 | ops = rcu_dereference(offloads[proto]); |
| 182 | if (!ops || !ops->callbacks.gro_receive) |
| 183 | goto out_unlock; |
| 184 | |
| 185 | pp = ops->callbacks.gro_receive(head, skb); |
| 186 | |
| 187 | out_unlock: |
| 188 | rcu_read_unlock(); |
| 189 | |
| 190 | return pp; |
| 191 | } |
| 192 | |
Tom Herbert | a2b12f3 | 2015-01-12 17:00:37 -0800 | [diff] [blame] | 193 | static int fou_gro_complete(struct sk_buff *skb, int nhoff, |
| 194 | struct udp_offload *uoff) |
Tom Herbert | afe9332 | 2014-09-17 12:25:57 -0700 | [diff] [blame] | 195 | { |
| 196 | const struct net_offload *ops; |
| 197 | u8 proto = NAPI_GRO_CB(skb)->proto; |
| 198 | int err = -ENOSYS; |
Tom Herbert | efc98d0 | 2014-10-03 15:48:08 -0700 | [diff] [blame] | 199 | const struct net_offload **offloads; |
Tom Herbert | afe9332 | 2014-09-17 12:25:57 -0700 | [diff] [blame] | 200 | |
Jesse Gross | cfdf1e1 | 2014-11-10 11:45:13 -0800 | [diff] [blame] | 201 | udp_tunnel_gro_complete(skb, nhoff); |
| 202 | |
Tom Herbert | afe9332 | 2014-09-17 12:25:57 -0700 | [diff] [blame] | 203 | rcu_read_lock(); |
Tom Herbert | efc98d0 | 2014-10-03 15:48:08 -0700 | [diff] [blame] | 204 | offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads; |
Tom Herbert | afe9332 | 2014-09-17 12:25:57 -0700 | [diff] [blame] | 205 | ops = rcu_dereference(offloads[proto]); |
| 206 | if (WARN_ON(!ops || !ops->callbacks.gro_complete)) |
| 207 | goto out_unlock; |
| 208 | |
| 209 | err = ops->callbacks.gro_complete(skb, nhoff); |
| 210 | |
| 211 | out_unlock: |
| 212 | rcu_read_unlock(); |
| 213 | |
| 214 | return err; |
| 215 | } |
| 216 | |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 217 | static struct guehdr *gue_gro_remcsum(struct sk_buff *skb, unsigned int off, |
| 218 | struct guehdr *guehdr, void *data, |
Tom Herbert | 26c4f7d | 2015-02-10 16:30:27 -0800 | [diff] [blame] | 219 | size_t hdrlen, u8 ipproto, |
Tom Herbert | fe881ef | 2015-02-10 16:30:33 -0800 | [diff] [blame] | 220 | struct gro_remcsum *grc, bool nopartial) |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 221 | { |
| 222 | __be16 *pd = data; |
Tom Herbert | 4fd671d | 2014-11-25 11:21:20 -0800 | [diff] [blame] | 223 | size_t start = ntohs(pd[0]); |
| 224 | size_t offset = ntohs(pd[1]); |
| 225 | size_t plen = hdrlen + max_t(size_t, offset + sizeof(u16), start); |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 226 | |
| 227 | if (skb->remcsum_offload) |
Tom Herbert | 26c4f7d | 2015-02-10 16:30:27 -0800 | [diff] [blame] | 228 | return NULL; |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 229 | |
Tom Herbert | 4fd671d | 2014-11-25 11:21:20 -0800 | [diff] [blame] | 230 | if (!NAPI_GRO_CB(skb)->csum_valid) |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 231 | return NULL; |
| 232 | |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 233 | /* Pull checksum that will be written */ |
| 234 | if (skb_gro_header_hard(skb, off + plen)) { |
| 235 | guehdr = skb_gro_header_slow(skb, off + plen, off); |
| 236 | if (!guehdr) |
| 237 | return NULL; |
| 238 | } |
| 239 | |
Tom Herbert | 26c4f7d | 2015-02-10 16:30:27 -0800 | [diff] [blame] | 240 | skb_gro_remcsum_process(skb, (void *)guehdr + hdrlen, |
Tom Herbert | fe881ef | 2015-02-10 16:30:33 -0800 | [diff] [blame] | 241 | start, offset, grc, nopartial); |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 242 | |
| 243 | skb->remcsum_offload = 1; |
| 244 | |
| 245 | return guehdr; |
| 246 | } |
| 247 | |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 248 | static struct sk_buff **gue_gro_receive(struct sk_buff **head, |
Tom Herbert | a2b12f3 | 2015-01-12 17:00:37 -0800 | [diff] [blame] | 249 | struct sk_buff *skb, |
| 250 | struct udp_offload *uoff) |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 251 | { |
| 252 | const struct net_offload **offloads; |
| 253 | const struct net_offload *ops; |
| 254 | struct sk_buff **pp = NULL; |
| 255 | struct sk_buff *p; |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 256 | struct guehdr *guehdr; |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 257 | size_t len, optlen, hdrlen, off; |
| 258 | void *data; |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 259 | u16 doffset = 0; |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 260 | int flush = 1; |
Tom Herbert | fe881ef | 2015-02-10 16:30:33 -0800 | [diff] [blame] | 261 | struct fou *fou = container_of(uoff, struct fou, udp_offloads); |
Tom Herbert | 26c4f7d | 2015-02-10 16:30:27 -0800 | [diff] [blame] | 262 | struct gro_remcsum grc; |
| 263 | |
| 264 | skb_gro_remcsum_init(&grc); |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 265 | |
| 266 | off = skb_gro_offset(skb); |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 267 | len = off + sizeof(*guehdr); |
| 268 | |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 269 | guehdr = skb_gro_header_fast(skb, off); |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 270 | if (skb_gro_header_hard(skb, len)) { |
| 271 | guehdr = skb_gro_header_slow(skb, len, off); |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 272 | if (unlikely(!guehdr)) |
| 273 | goto out; |
| 274 | } |
| 275 | |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 276 | optlen = guehdr->hlen << 2; |
| 277 | len += optlen; |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 278 | |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 279 | if (skb_gro_header_hard(skb, len)) { |
| 280 | guehdr = skb_gro_header_slow(skb, len, off); |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 281 | if (unlikely(!guehdr)) |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 282 | goto out; |
| 283 | } |
| 284 | |
| 285 | if (unlikely(guehdr->control) || guehdr->version != 0 || |
| 286 | validate_gue_flags(guehdr, optlen)) |
| 287 | goto out; |
| 288 | |
| 289 | hdrlen = sizeof(*guehdr) + optlen; |
| 290 | |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 291 | /* Adjust NAPI_GRO_CB(skb)->csum to account for guehdr, |
| 292 | * this is needed if there is a remote checkcsum offload. |
| 293 | */ |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 294 | skb_gro_postpull_rcsum(skb, guehdr, hdrlen); |
| 295 | |
| 296 | data = &guehdr[1]; |
| 297 | |
| 298 | if (guehdr->flags & GUE_FLAG_PRIV) { |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 299 | __be32 flags = *(__be32 *)(data + doffset); |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 300 | |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 301 | doffset += GUE_LEN_PRIV; |
| 302 | |
| 303 | if (flags & GUE_PFLAG_REMCSUM) { |
| 304 | guehdr = gue_gro_remcsum(skb, off, guehdr, |
| 305 | data + doffset, hdrlen, |
Tom Herbert | fe881ef | 2015-02-10 16:30:33 -0800 | [diff] [blame] | 306 | guehdr->proto_ctype, &grc, |
| 307 | !!(fou->flags & |
| 308 | FOU_F_REMCSUM_NOPARTIAL)); |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 309 | if (!guehdr) |
| 310 | goto out; |
| 311 | |
| 312 | data = &guehdr[1]; |
| 313 | |
| 314 | doffset += GUE_PLEN_REMCSUM; |
| 315 | } |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Tom Herbert | a8d31c1 | 2014-11-04 09:06:57 -0800 | [diff] [blame] | 318 | skb_gro_pull(skb, hdrlen); |
| 319 | |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 320 | flush = 0; |
| 321 | |
| 322 | for (p = *head; p; p = p->next) { |
| 323 | const struct guehdr *guehdr2; |
| 324 | |
| 325 | if (!NAPI_GRO_CB(p)->same_flow) |
| 326 | continue; |
| 327 | |
| 328 | guehdr2 = (struct guehdr *)(p->data + off); |
| 329 | |
| 330 | /* Compare base GUE header to be equal (covers |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 331 | * hlen, version, proto_ctype, and flags. |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 332 | */ |
| 333 | if (guehdr->word != guehdr2->word) { |
| 334 | NAPI_GRO_CB(p)->same_flow = 0; |
| 335 | continue; |
| 336 | } |
| 337 | |
| 338 | /* Compare optional fields are the same. */ |
| 339 | if (guehdr->hlen && memcmp(&guehdr[1], &guehdr2[1], |
| 340 | guehdr->hlen << 2)) { |
| 341 | NAPI_GRO_CB(p)->same_flow = 0; |
| 342 | continue; |
| 343 | } |
| 344 | } |
| 345 | |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 346 | rcu_read_lock(); |
| 347 | offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads; |
| 348 | ops = rcu_dereference(offloads[guehdr->proto_ctype]); |
| 349 | if (WARN_ON(!ops || !ops->callbacks.gro_receive)) |
| 350 | goto out_unlock; |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 351 | |
| 352 | pp = ops->callbacks.gro_receive(head, skb); |
| 353 | |
| 354 | out_unlock: |
| 355 | rcu_read_unlock(); |
| 356 | out: |
| 357 | NAPI_GRO_CB(skb)->flush |= flush; |
Tom Herbert | 26c4f7d | 2015-02-10 16:30:27 -0800 | [diff] [blame] | 358 | skb_gro_remcsum_cleanup(skb, &grc); |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 359 | |
| 360 | return pp; |
| 361 | } |
| 362 | |
Tom Herbert | a2b12f3 | 2015-01-12 17:00:37 -0800 | [diff] [blame] | 363 | static int gue_gro_complete(struct sk_buff *skb, int nhoff, |
| 364 | struct udp_offload *uoff) |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 365 | { |
| 366 | const struct net_offload **offloads; |
| 367 | struct guehdr *guehdr = (struct guehdr *)(skb->data + nhoff); |
| 368 | const struct net_offload *ops; |
| 369 | unsigned int guehlen; |
| 370 | u8 proto; |
| 371 | int err = -ENOENT; |
| 372 | |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 373 | proto = guehdr->proto_ctype; |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 374 | |
| 375 | guehlen = sizeof(*guehdr) + (guehdr->hlen << 2); |
| 376 | |
| 377 | rcu_read_lock(); |
| 378 | offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads; |
| 379 | ops = rcu_dereference(offloads[proto]); |
| 380 | if (WARN_ON(!ops || !ops->callbacks.gro_complete)) |
| 381 | goto out_unlock; |
| 382 | |
| 383 | err = ops->callbacks.gro_complete(skb, nhoff + guehlen); |
| 384 | |
| 385 | out_unlock: |
| 386 | rcu_read_unlock(); |
| 387 | return err; |
| 388 | } |
| 389 | |
Tom Herbert | 2346155 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 390 | static int fou_add_to_port_list(struct fou *fou) |
| 391 | { |
| 392 | struct fou *fout; |
| 393 | |
| 394 | spin_lock(&fou_lock); |
| 395 | list_for_each_entry(fout, &fou_list, list) { |
| 396 | if (fou->port == fout->port) { |
| 397 | spin_unlock(&fou_lock); |
| 398 | return -EALREADY; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | list_add(&fou->list, &fou_list); |
| 403 | spin_unlock(&fou_lock); |
| 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | static void fou_release(struct fou *fou) |
| 409 | { |
| 410 | struct socket *sock = fou->sock; |
| 411 | struct sock *sk = sock->sk; |
| 412 | |
WANG Cong | 9272f04 | 2015-04-10 12:00:26 -0700 | [diff] [blame^] | 413 | if (sk->sk_family == AF_INET) |
| 414 | udp_del_offload(&fou->udp_offloads); |
Tom Herbert | 2346155 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 415 | |
| 416 | list_del(&fou->list); |
| 417 | |
| 418 | /* Remove hooks into tunnel socket */ |
| 419 | sk->sk_user_data = NULL; |
| 420 | |
| 421 | sock_release(sock); |
| 422 | |
| 423 | kfree(fou); |
| 424 | } |
| 425 | |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 426 | static int fou_encap_init(struct sock *sk, struct fou *fou, struct fou_cfg *cfg) |
| 427 | { |
| 428 | udp_sk(sk)->encap_rcv = fou_udp_recv; |
| 429 | fou->protocol = cfg->protocol; |
| 430 | fou->udp_offloads.callbacks.gro_receive = fou_gro_receive; |
| 431 | fou->udp_offloads.callbacks.gro_complete = fou_gro_complete; |
| 432 | fou->udp_offloads.port = cfg->udp_config.local_udp_port; |
| 433 | fou->udp_offloads.ipproto = cfg->protocol; |
| 434 | |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | static int gue_encap_init(struct sock *sk, struct fou *fou, struct fou_cfg *cfg) |
| 439 | { |
| 440 | udp_sk(sk)->encap_rcv = gue_udp_recv; |
| 441 | fou->udp_offloads.callbacks.gro_receive = gue_gro_receive; |
| 442 | fou->udp_offloads.callbacks.gro_complete = gue_gro_complete; |
| 443 | fou->udp_offloads.port = cfg->udp_config.local_udp_port; |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
Tom Herbert | 2346155 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 448 | static int fou_create(struct net *net, struct fou_cfg *cfg, |
| 449 | struct socket **sockp) |
| 450 | { |
| 451 | struct fou *fou = NULL; |
| 452 | int err; |
| 453 | struct socket *sock = NULL; |
| 454 | struct sock *sk; |
| 455 | |
| 456 | /* Open UDP socket */ |
| 457 | err = udp_sock_create(net, &cfg->udp_config, &sock); |
| 458 | if (err < 0) |
| 459 | goto error; |
| 460 | |
| 461 | /* Allocate FOU port structure */ |
| 462 | fou = kzalloc(sizeof(*fou), GFP_KERNEL); |
| 463 | if (!fou) { |
| 464 | err = -ENOMEM; |
| 465 | goto error; |
| 466 | } |
| 467 | |
| 468 | sk = sock->sk; |
| 469 | |
Tom Herbert | fe881ef | 2015-02-10 16:30:33 -0800 | [diff] [blame] | 470 | fou->flags = cfg->flags; |
Tom Herbert | 37dd024 | 2014-10-03 15:48:09 -0700 | [diff] [blame] | 471 | fou->port = cfg->udp_config.local_udp_port; |
| 472 | |
| 473 | /* Initial for fou type */ |
| 474 | switch (cfg->type) { |
| 475 | case FOU_ENCAP_DIRECT: |
| 476 | err = fou_encap_init(sk, fou, cfg); |
| 477 | if (err) |
| 478 | goto error; |
| 479 | break; |
| 480 | case FOU_ENCAP_GUE: |
| 481 | err = gue_encap_init(sk, fou, cfg); |
| 482 | if (err) |
| 483 | goto error; |
| 484 | break; |
| 485 | default: |
| 486 | err = -EINVAL; |
| 487 | goto error; |
| 488 | } |
Tom Herbert | 2346155 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 489 | |
| 490 | udp_sk(sk)->encap_type = 1; |
| 491 | udp_encap_enable(); |
| 492 | |
| 493 | sk->sk_user_data = fou; |
| 494 | fou->sock = sock; |
| 495 | |
Tom Herbert | 224d019 | 2015-01-05 13:56:14 -0800 | [diff] [blame] | 496 | inet_inc_convert_csum(sk); |
Tom Herbert | 2346155 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 497 | |
| 498 | sk->sk_allocation = GFP_ATOMIC; |
| 499 | |
Tom Herbert | afe9332 | 2014-09-17 12:25:57 -0700 | [diff] [blame] | 500 | if (cfg->udp_config.family == AF_INET) { |
| 501 | err = udp_add_offload(&fou->udp_offloads); |
| 502 | if (err) |
| 503 | goto error; |
| 504 | } |
| 505 | |
Tom Herbert | 2346155 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 506 | err = fou_add_to_port_list(fou); |
| 507 | if (err) |
| 508 | goto error; |
| 509 | |
| 510 | if (sockp) |
| 511 | *sockp = sock; |
| 512 | |
| 513 | return 0; |
| 514 | |
| 515 | error: |
| 516 | kfree(fou); |
| 517 | if (sock) |
| 518 | sock_release(sock); |
| 519 | |
| 520 | return err; |
| 521 | } |
| 522 | |
| 523 | static int fou_destroy(struct net *net, struct fou_cfg *cfg) |
| 524 | { |
| 525 | struct fou *fou; |
| 526 | u16 port = cfg->udp_config.local_udp_port; |
| 527 | int err = -EINVAL; |
| 528 | |
| 529 | spin_lock(&fou_lock); |
| 530 | list_for_each_entry(fou, &fou_list, list) { |
| 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 | } |
| 537 | spin_unlock(&fou_lock); |
| 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 | |
| 569 | if (family != AF_INET && family != AF_INET6) |
| 570 | return -EINVAL; |
| 571 | |
| 572 | cfg->udp_config.family = family; |
| 573 | } |
| 574 | |
| 575 | if (info->attrs[FOU_ATTR_PORT]) { |
| 576 | u16 port = nla_get_u16(info->attrs[FOU_ATTR_PORT]); |
| 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 | { |
| 595 | struct fou_cfg cfg; |
| 596 | int err; |
| 597 | |
| 598 | err = parse_nl_config(info, &cfg); |
| 599 | if (err) |
| 600 | return err; |
| 601 | |
| 602 | return fou_create(&init_net, &cfg, NULL); |
| 603 | } |
| 604 | |
| 605 | static int fou_nl_cmd_rm_port(struct sk_buff *skb, struct genl_info *info) |
| 606 | { |
| 607 | struct fou_cfg cfg; |
| 608 | |
| 609 | parse_nl_config(info, &cfg); |
| 610 | |
| 611 | return fou_destroy(&init_net, &cfg); |
| 612 | } |
| 613 | |
| 614 | static const struct genl_ops fou_nl_ops[] = { |
| 615 | { |
| 616 | .cmd = FOU_CMD_ADD, |
| 617 | .doit = fou_nl_cmd_add_port, |
| 618 | .policy = fou_nl_policy, |
| 619 | .flags = GENL_ADMIN_PERM, |
| 620 | }, |
| 621 | { |
| 622 | .cmd = FOU_CMD_DEL, |
| 623 | .doit = fou_nl_cmd_rm_port, |
| 624 | .policy = fou_nl_policy, |
| 625 | .flags = GENL_ADMIN_PERM, |
| 626 | }, |
| 627 | }; |
| 628 | |
Tom Herbert | a8c5f90 | 2014-11-12 11:54:09 -0800 | [diff] [blame] | 629 | size_t fou_encap_hlen(struct ip_tunnel_encap *e) |
| 630 | { |
| 631 | return sizeof(struct udphdr); |
| 632 | } |
| 633 | EXPORT_SYMBOL(fou_encap_hlen); |
| 634 | |
| 635 | size_t gue_encap_hlen(struct ip_tunnel_encap *e) |
| 636 | { |
| 637 | size_t len; |
| 638 | bool need_priv = false; |
| 639 | |
| 640 | len = sizeof(struct udphdr) + sizeof(struct guehdr); |
| 641 | |
| 642 | if (e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) { |
| 643 | len += GUE_PLEN_REMCSUM; |
| 644 | need_priv = true; |
| 645 | } |
| 646 | |
| 647 | len += need_priv ? GUE_LEN_PRIV : 0; |
| 648 | |
| 649 | return len; |
| 650 | } |
| 651 | EXPORT_SYMBOL(gue_encap_hlen); |
| 652 | |
Tom Herbert | 63487ba | 2014-11-04 09:06:51 -0800 | [diff] [blame] | 653 | static void fou_build_udp(struct sk_buff *skb, struct ip_tunnel_encap *e, |
| 654 | struct flowi4 *fl4, u8 *protocol, __be16 sport) |
| 655 | { |
| 656 | struct udphdr *uh; |
| 657 | |
| 658 | skb_push(skb, sizeof(struct udphdr)); |
| 659 | skb_reset_transport_header(skb); |
| 660 | |
| 661 | uh = udp_hdr(skb); |
| 662 | |
| 663 | uh->dest = e->dport; |
| 664 | uh->source = sport; |
| 665 | uh->len = htons(skb->len); |
| 666 | uh->check = 0; |
| 667 | udp_set_csum(!(e->flags & TUNNEL_ENCAP_FLAG_CSUM), skb, |
| 668 | fl4->saddr, fl4->daddr, skb->len); |
| 669 | |
| 670 | *protocol = IPPROTO_UDP; |
| 671 | } |
| 672 | |
| 673 | int fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e, |
| 674 | u8 *protocol, struct flowi4 *fl4) |
| 675 | { |
| 676 | bool csum = !!(e->flags & TUNNEL_ENCAP_FLAG_CSUM); |
| 677 | int type = csum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL; |
| 678 | __be16 sport; |
| 679 | |
| 680 | skb = iptunnel_handle_offloads(skb, csum, type); |
| 681 | |
| 682 | if (IS_ERR(skb)) |
| 683 | return PTR_ERR(skb); |
| 684 | |
| 685 | sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev), |
| 686 | skb, 0, 0, false); |
| 687 | fou_build_udp(skb, e, fl4, protocol, sport); |
| 688 | |
| 689 | return 0; |
| 690 | } |
| 691 | EXPORT_SYMBOL(fou_build_header); |
| 692 | |
| 693 | int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e, |
| 694 | u8 *protocol, struct flowi4 *fl4) |
| 695 | { |
| 696 | bool csum = !!(e->flags & TUNNEL_ENCAP_FLAG_CSUM); |
| 697 | int type = csum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL; |
| 698 | struct guehdr *guehdr; |
Tom Herbert | b17f709 | 2014-11-04 09:06:56 -0800 | [diff] [blame] | 699 | size_t hdrlen, optlen = 0; |
Tom Herbert | 63487ba | 2014-11-04 09:06:51 -0800 | [diff] [blame] | 700 | __be16 sport; |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 701 | void *data; |
| 702 | bool need_priv = false; |
| 703 | |
Tom Herbert | b17f709 | 2014-11-04 09:06:56 -0800 | [diff] [blame] | 704 | if ((e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) && |
| 705 | skb->ip_summed == CHECKSUM_PARTIAL) { |
| 706 | csum = false; |
| 707 | optlen += GUE_PLEN_REMCSUM; |
| 708 | type |= SKB_GSO_TUNNEL_REMCSUM; |
| 709 | need_priv = true; |
| 710 | } |
| 711 | |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 712 | optlen += need_priv ? GUE_LEN_PRIV : 0; |
Tom Herbert | 63487ba | 2014-11-04 09:06:51 -0800 | [diff] [blame] | 713 | |
| 714 | skb = iptunnel_handle_offloads(skb, csum, type); |
| 715 | |
| 716 | if (IS_ERR(skb)) |
| 717 | return PTR_ERR(skb); |
| 718 | |
| 719 | /* Get source port (based on flow hash) before skb_push */ |
| 720 | sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev), |
| 721 | skb, 0, 0, false); |
| 722 | |
Tom Herbert | b17f709 | 2014-11-04 09:06:56 -0800 | [diff] [blame] | 723 | hdrlen = sizeof(struct guehdr) + optlen; |
| 724 | |
| 725 | skb_push(skb, hdrlen); |
Tom Herbert | 63487ba | 2014-11-04 09:06:51 -0800 | [diff] [blame] | 726 | |
| 727 | guehdr = (struct guehdr *)skb->data; |
| 728 | |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 729 | guehdr->control = 0; |
Tom Herbert | 63487ba | 2014-11-04 09:06:51 -0800 | [diff] [blame] | 730 | guehdr->version = 0; |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 731 | guehdr->hlen = optlen >> 2; |
Tom Herbert | 63487ba | 2014-11-04 09:06:51 -0800 | [diff] [blame] | 732 | guehdr->flags = 0; |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 733 | guehdr->proto_ctype = *protocol; |
| 734 | |
| 735 | data = &guehdr[1]; |
| 736 | |
| 737 | if (need_priv) { |
| 738 | __be32 *flags = data; |
| 739 | |
| 740 | guehdr->flags |= GUE_FLAG_PRIV; |
| 741 | *flags = 0; |
| 742 | data += GUE_LEN_PRIV; |
| 743 | |
Tom Herbert | b17f709 | 2014-11-04 09:06:56 -0800 | [diff] [blame] | 744 | if (type & SKB_GSO_TUNNEL_REMCSUM) { |
| 745 | u16 csum_start = skb_checksum_start_offset(skb); |
| 746 | __be16 *pd = data; |
| 747 | |
| 748 | if (csum_start < hdrlen) |
| 749 | return -EINVAL; |
| 750 | |
| 751 | csum_start -= hdrlen; |
| 752 | pd[0] = htons(csum_start); |
| 753 | pd[1] = htons(csum_start + skb->csum_offset); |
| 754 | |
| 755 | if (!skb_is_gso(skb)) { |
| 756 | skb->ip_summed = CHECKSUM_NONE; |
| 757 | skb->encapsulation = 0; |
| 758 | } |
| 759 | |
| 760 | *flags |= GUE_PFLAG_REMCSUM; |
| 761 | data += GUE_PLEN_REMCSUM; |
| 762 | } |
| 763 | |
Tom Herbert | 5024c33 | 2014-11-04 09:06:53 -0800 | [diff] [blame] | 764 | } |
Tom Herbert | 63487ba | 2014-11-04 09:06:51 -0800 | [diff] [blame] | 765 | |
| 766 | fou_build_udp(skb, e, fl4, protocol, sport); |
| 767 | |
| 768 | return 0; |
| 769 | } |
| 770 | EXPORT_SYMBOL(gue_build_header); |
| 771 | |
Tom Herbert | a8c5f90 | 2014-11-12 11:54:09 -0800 | [diff] [blame] | 772 | #ifdef CONFIG_NET_FOU_IP_TUNNELS |
| 773 | |
| 774 | static const struct ip_tunnel_encap_ops __read_mostly fou_iptun_ops = { |
| 775 | .encap_hlen = fou_encap_hlen, |
| 776 | .build_header = fou_build_header, |
| 777 | }; |
| 778 | |
| 779 | static const struct ip_tunnel_encap_ops __read_mostly gue_iptun_ops = { |
| 780 | .encap_hlen = gue_encap_hlen, |
| 781 | .build_header = gue_build_header, |
| 782 | }; |
| 783 | |
| 784 | static int ip_tunnel_encap_add_fou_ops(void) |
| 785 | { |
| 786 | int ret; |
| 787 | |
| 788 | ret = ip_tunnel_encap_add_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU); |
| 789 | if (ret < 0) { |
| 790 | pr_err("can't add fou ops\n"); |
| 791 | return ret; |
| 792 | } |
| 793 | |
| 794 | ret = ip_tunnel_encap_add_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE); |
| 795 | if (ret < 0) { |
| 796 | pr_err("can't add gue ops\n"); |
| 797 | ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU); |
| 798 | return ret; |
| 799 | } |
| 800 | |
| 801 | return 0; |
| 802 | } |
| 803 | |
| 804 | static void ip_tunnel_encap_del_fou_ops(void) |
| 805 | { |
| 806 | ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU); |
| 807 | ip_tunnel_encap_del_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE); |
| 808 | } |
| 809 | |
| 810 | #else |
| 811 | |
| 812 | static int ip_tunnel_encap_add_fou_ops(void) |
| 813 | { |
| 814 | return 0; |
| 815 | } |
| 816 | |
Thomas Graf | 882288c | 2014-11-13 12:48:21 +0100 | [diff] [blame] | 817 | static void ip_tunnel_encap_del_fou_ops(void) |
Tom Herbert | a8c5f90 | 2014-11-12 11:54:09 -0800 | [diff] [blame] | 818 | { |
| 819 | } |
| 820 | |
| 821 | #endif |
| 822 | |
Tom Herbert | 2346155 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 823 | static int __init fou_init(void) |
| 824 | { |
| 825 | int ret; |
| 826 | |
| 827 | ret = genl_register_family_with_ops(&fou_nl_family, |
| 828 | fou_nl_ops); |
| 829 | |
Tom Herbert | a8c5f90 | 2014-11-12 11:54:09 -0800 | [diff] [blame] | 830 | if (ret < 0) |
| 831 | goto exit; |
| 832 | |
| 833 | ret = ip_tunnel_encap_add_fou_ops(); |
| 834 | if (ret < 0) |
| 835 | genl_unregister_family(&fou_nl_family); |
| 836 | |
| 837 | exit: |
Tom Herbert | 2346155 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 838 | return ret; |
| 839 | } |
| 840 | |
| 841 | static void __exit fou_fini(void) |
| 842 | { |
| 843 | struct fou *fou, *next; |
| 844 | |
Tom Herbert | a8c5f90 | 2014-11-12 11:54:09 -0800 | [diff] [blame] | 845 | ip_tunnel_encap_del_fou_ops(); |
| 846 | |
Tom Herbert | 2346155 | 2014-09-17 12:25:56 -0700 | [diff] [blame] | 847 | genl_unregister_family(&fou_nl_family); |
| 848 | |
| 849 | /* Close all the FOU sockets */ |
| 850 | |
| 851 | spin_lock(&fou_lock); |
| 852 | list_for_each_entry_safe(fou, next, &fou_list, list) |
| 853 | fou_release(fou); |
| 854 | spin_unlock(&fou_lock); |
| 855 | } |
| 856 | |
| 857 | module_init(fou_init); |
| 858 | module_exit(fou_fini); |
| 859 | MODULE_AUTHOR("Tom Herbert <therbert@google.com>"); |
| 860 | MODULE_LICENSE("GPL"); |