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