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