]>
Commit | Line | Data |
---|---|---|
0e6fbc5b PS |
1 | /* |
2 | * Copyright (c) 2013 Nicira, Inc. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of version 2 of the GNU General Public | |
6 | * License as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but | |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this program; if not, write to the Free Software | |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
16 | * 02110-1301, USA | |
17 | */ | |
18 | ||
19 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
20 | ||
21 | #include <linux/types.h> | |
22 | #include <linux/kernel.h> | |
23 | #include <linux/skbuff.h> | |
24 | #include <linux/netdevice.h> | |
25 | #include <linux/in.h> | |
26 | #include <linux/if_arp.h> | |
27 | #include <linux/mroute.h> | |
28 | #include <linux/init.h> | |
29 | #include <linux/in6.h> | |
30 | #include <linux/inetdevice.h> | |
31 | #include <linux/netfilter_ipv4.h> | |
32 | #include <linux/etherdevice.h> | |
33 | #include <linux/if_ether.h> | |
34 | #include <linux/if_vlan.h> | |
e7030878 | 35 | #include <linux/static_key.h> |
0e6fbc5b PS |
36 | |
37 | #include <net/ip.h> | |
38 | #include <net/icmp.h> | |
39 | #include <net/protocol.h> | |
40 | #include <net/ip_tunnels.h> | |
41 | #include <net/arp.h> | |
42 | #include <net/checksum.h> | |
43 | #include <net/dsfield.h> | |
44 | #include <net/inet_ecn.h> | |
45 | #include <net/xfrm.h> | |
46 | #include <net/net_namespace.h> | |
47 | #include <net/netns/generic.h> | |
48 | #include <net/rtnetlink.h> | |
49 | ||
aad88724 | 50 | int iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb, |
0e6fbc5b | 51 | __be32 src, __be32 dst, __u8 proto, |
963a88b3 | 52 | __u8 tos, __u8 ttl, __be16 df, bool xnet) |
0e6fbc5b PS |
53 | { |
54 | int pkt_len = skb->len; | |
55 | struct iphdr *iph; | |
56 | int err; | |
57 | ||
963a88b3 ND |
58 | skb_scrub_packet(skb, xnet); |
59 | ||
7539fadc | 60 | skb_clear_hash(skb); |
0e6fbc5b PS |
61 | skb_dst_set(skb, &rt->dst); |
62 | memset(IPCB(skb), 0, sizeof(*IPCB(skb))); | |
63 | ||
64 | /* Push down and install the IP header. */ | |
78a3694d | 65 | skb_push(skb, sizeof(struct iphdr)); |
0e6fbc5b PS |
66 | skb_reset_network_header(skb); |
67 | ||
68 | iph = ip_hdr(skb); | |
69 | ||
70 | iph->version = 4; | |
71 | iph->ihl = sizeof(struct iphdr) >> 2; | |
72 | iph->frag_off = df; | |
73 | iph->protocol = proto; | |
74 | iph->tos = tos; | |
75 | iph->daddr = dst; | |
76 | iph->saddr = src; | |
77 | iph->ttl = ttl; | |
926a882f HFS |
78 | __ip_select_ident(dev_net(rt->dst.dev), iph, |
79 | skb_shinfo(skb)->gso_segs ?: 1); | |
0e6fbc5b | 80 | |
aad88724 | 81 | err = ip_local_out_sk(sk, skb); |
0e6fbc5b PS |
82 | if (unlikely(net_xmit_eval(err))) |
83 | pkt_len = 0; | |
84 | return pkt_len; | |
85 | } | |
86 | EXPORT_SYMBOL_GPL(iptunnel_xmit); | |
3d7b46cd PS |
87 | |
88 | int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto) | |
89 | { | |
90 | if (unlikely(!pskb_may_pull(skb, hdr_len))) | |
91 | return -ENOMEM; | |
92 | ||
93 | skb_pull_rcsum(skb, hdr_len); | |
94 | ||
95 | if (inner_proto == htons(ETH_P_TEB)) { | |
1245dfc8 | 96 | struct ethhdr *eh; |
3d7b46cd PS |
97 | |
98 | if (unlikely(!pskb_may_pull(skb, ETH_HLEN))) | |
99 | return -ENOMEM; | |
100 | ||
1245dfc8 | 101 | eh = (struct ethhdr *)skb->data; |
d181ddca | 102 | if (likely(eth_proto_is_802_3(eh->h_proto))) |
3d7b46cd PS |
103 | skb->protocol = eh->h_proto; |
104 | else | |
105 | skb->protocol = htons(ETH_P_802_2); | |
106 | ||
107 | } else { | |
108 | skb->protocol = inner_proto; | |
109 | } | |
110 | ||
111 | nf_reset(skb); | |
112 | secpath_reset(skb); | |
7539fadc | 113 | skb_clear_hash_if_not_l4(skb); |
fbd02dd4 | 114 | skb_dst_drop(skb); |
3d7b46cd PS |
115 | skb->vlan_tci = 0; |
116 | skb_set_queue_mapping(skb, 0); | |
117 | skb->pkt_type = PACKET_HOST; | |
118 | return 0; | |
119 | } | |
120 | EXPORT_SYMBOL_GPL(iptunnel_pull_header); | |
2d26f0a3 ED |
121 | |
122 | struct sk_buff *iptunnel_handle_offloads(struct sk_buff *skb, | |
123 | bool csum_help, | |
124 | int gso_type_mask) | |
125 | { | |
126 | int err; | |
127 | ||
128 | if (likely(!skb->encapsulation)) { | |
129 | skb_reset_inner_headers(skb); | |
130 | skb->encapsulation = 1; | |
131 | } | |
132 | ||
133 | if (skb_is_gso(skb)) { | |
134 | err = skb_unclone(skb, GFP_ATOMIC); | |
135 | if (unlikely(err)) | |
136 | goto error; | |
137 | skb_shinfo(skb)->gso_type |= gso_type_mask; | |
138 | return skb; | |
139 | } | |
140 | ||
7e2b10c1 TH |
141 | /* If packet is not gso and we are resolving any partial checksum, |
142 | * clear encapsulation flag. This allows setting CHECKSUM_PARTIAL | |
143 | * on the outer header without confusing devices that implement | |
144 | * NETIF_F_IP_CSUM with encapsulation. | |
145 | */ | |
146 | if (csum_help) | |
147 | skb->encapsulation = 0; | |
148 | ||
2d26f0a3 ED |
149 | if (skb->ip_summed == CHECKSUM_PARTIAL && csum_help) { |
150 | err = skb_checksum_help(skb); | |
151 | if (unlikely(err)) | |
152 | goto error; | |
153 | } else if (skb->ip_summed != CHECKSUM_PARTIAL) | |
154 | skb->ip_summed = CHECKSUM_NONE; | |
155 | ||
156 | return skb; | |
157 | error: | |
158 | kfree_skb(skb); | |
159 | return ERR_PTR(err); | |
160 | } | |
161 | EXPORT_SYMBOL_GPL(iptunnel_handle_offloads); | |
ebe44f35 DM |
162 | |
163 | /* Often modified stats are per cpu, other are shared (netdev->stats) */ | |
164 | struct rtnl_link_stats64 *ip_tunnel_get_stats64(struct net_device *dev, | |
165 | struct rtnl_link_stats64 *tot) | |
166 | { | |
167 | int i; | |
168 | ||
c24a5964 AD |
169 | netdev_stats_to_stats64(tot, &dev->stats); |
170 | ||
ebe44f35 DM |
171 | for_each_possible_cpu(i) { |
172 | const struct pcpu_sw_netstats *tstats = | |
173 | per_cpu_ptr(dev->tstats, i); | |
174 | u64 rx_packets, rx_bytes, tx_packets, tx_bytes; | |
175 | unsigned int start; | |
176 | ||
177 | do { | |
57a7744e | 178 | start = u64_stats_fetch_begin_irq(&tstats->syncp); |
ebe44f35 DM |
179 | rx_packets = tstats->rx_packets; |
180 | tx_packets = tstats->tx_packets; | |
181 | rx_bytes = tstats->rx_bytes; | |
182 | tx_bytes = tstats->tx_bytes; | |
57a7744e | 183 | } while (u64_stats_fetch_retry_irq(&tstats->syncp, start)); |
ebe44f35 DM |
184 | |
185 | tot->rx_packets += rx_packets; | |
186 | tot->tx_packets += tx_packets; | |
187 | tot->rx_bytes += rx_bytes; | |
188 | tot->tx_bytes += tx_bytes; | |
189 | } | |
190 | ||
ebe44f35 DM |
191 | return tot; |
192 | } | |
193 | EXPORT_SYMBOL_GPL(ip_tunnel_get_stats64); | |
3093fbe7 | 194 | |
a1c234f9 JB |
195 | static const struct nla_policy ip_tun_policy[LWTUNNEL_IP_MAX + 1] = { |
196 | [LWTUNNEL_IP_ID] = { .type = NLA_U64 }, | |
197 | [LWTUNNEL_IP_DST] = { .type = NLA_U32 }, | |
198 | [LWTUNNEL_IP_SRC] = { .type = NLA_U32 }, | |
199 | [LWTUNNEL_IP_TTL] = { .type = NLA_U8 }, | |
200 | [LWTUNNEL_IP_TOS] = { .type = NLA_U8 }, | |
201 | [LWTUNNEL_IP_SPORT] = { .type = NLA_U16 }, | |
202 | [LWTUNNEL_IP_DPORT] = { .type = NLA_U16 }, | |
203 | [LWTUNNEL_IP_FLAGS] = { .type = NLA_U16 }, | |
3093fbe7 TG |
204 | }; |
205 | ||
206 | static int ip_tun_build_state(struct net_device *dev, struct nlattr *attr, | |
127eb7cd | 207 | unsigned int family, const void *cfg, |
3093fbe7 TG |
208 | struct lwtunnel_state **ts) |
209 | { | |
210 | struct ip_tunnel_info *tun_info; | |
211 | struct lwtunnel_state *new_state; | |
a1c234f9 | 212 | struct nlattr *tb[LWTUNNEL_IP_MAX + 1]; |
3093fbe7 TG |
213 | int err; |
214 | ||
a1c234f9 | 215 | err = nla_parse_nested(tb, LWTUNNEL_IP_MAX, attr, ip_tun_policy); |
3093fbe7 TG |
216 | if (err < 0) |
217 | return err; | |
218 | ||
219 | new_state = lwtunnel_state_alloc(sizeof(*tun_info)); | |
220 | if (!new_state) | |
221 | return -ENOMEM; | |
222 | ||
223 | new_state->type = LWTUNNEL_ENCAP_IP; | |
224 | ||
225 | tun_info = lwt_tun_info(new_state); | |
226 | ||
a1c234f9 JB |
227 | if (tb[LWTUNNEL_IP_ID]) |
228 | tun_info->key.tun_id = nla_get_u64(tb[LWTUNNEL_IP_ID]); | |
3093fbe7 | 229 | |
a1c234f9 | 230 | if (tb[LWTUNNEL_IP_DST]) |
c1ea5d67 | 231 | tun_info->key.u.ipv4.dst = nla_get_be32(tb[LWTUNNEL_IP_DST]); |
3093fbe7 | 232 | |
a1c234f9 | 233 | if (tb[LWTUNNEL_IP_SRC]) |
c1ea5d67 | 234 | tun_info->key.u.ipv4.src = nla_get_be32(tb[LWTUNNEL_IP_SRC]); |
3093fbe7 | 235 | |
a1c234f9 | 236 | if (tb[LWTUNNEL_IP_TTL]) |
7c383fb2 | 237 | tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP_TTL]); |
3093fbe7 | 238 | |
a1c234f9 | 239 | if (tb[LWTUNNEL_IP_TOS]) |
7c383fb2 | 240 | tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP_TOS]); |
3093fbe7 | 241 | |
a1c234f9 JB |
242 | if (tb[LWTUNNEL_IP_SPORT]) |
243 | tun_info->key.tp_src = nla_get_be16(tb[LWTUNNEL_IP_SPORT]); | |
3093fbe7 | 244 | |
a1c234f9 JB |
245 | if (tb[LWTUNNEL_IP_DPORT]) |
246 | tun_info->key.tp_dst = nla_get_be16(tb[LWTUNNEL_IP_DPORT]); | |
3093fbe7 | 247 | |
a1c234f9 JB |
248 | if (tb[LWTUNNEL_IP_FLAGS]) |
249 | tun_info->key.tun_flags = nla_get_u16(tb[LWTUNNEL_IP_FLAGS]); | |
3093fbe7 TG |
250 | |
251 | tun_info->mode = IP_TUNNEL_INFO_TX; | |
252 | tun_info->options = NULL; | |
253 | tun_info->options_len = 0; | |
254 | ||
255 | *ts = new_state; | |
256 | ||
257 | return 0; | |
258 | } | |
259 | ||
260 | static int ip_tun_fill_encap_info(struct sk_buff *skb, | |
261 | struct lwtunnel_state *lwtstate) | |
262 | { | |
263 | struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate); | |
264 | ||
a1c234f9 | 265 | if (nla_put_u64(skb, LWTUNNEL_IP_ID, tun_info->key.tun_id) || |
c1ea5d67 JB |
266 | nla_put_be32(skb, LWTUNNEL_IP_DST, tun_info->key.u.ipv4.dst) || |
267 | nla_put_be32(skb, LWTUNNEL_IP_SRC, tun_info->key.u.ipv4.src) || | |
7c383fb2 JB |
268 | nla_put_u8(skb, LWTUNNEL_IP_TOS, tun_info->key.tos) || |
269 | nla_put_u8(skb, LWTUNNEL_IP_TTL, tun_info->key.ttl) || | |
a1c234f9 JB |
270 | nla_put_u16(skb, LWTUNNEL_IP_SPORT, tun_info->key.tp_src) || |
271 | nla_put_u16(skb, LWTUNNEL_IP_DPORT, tun_info->key.tp_dst) || | |
272 | nla_put_u16(skb, LWTUNNEL_IP_FLAGS, tun_info->key.tun_flags)) | |
3093fbe7 TG |
273 | return -ENOMEM; |
274 | ||
275 | return 0; | |
276 | } | |
277 | ||
278 | static int ip_tun_encap_nlsize(struct lwtunnel_state *lwtstate) | |
279 | { | |
a1c234f9 JB |
280 | return nla_total_size(8) /* LWTUNNEL_IP_ID */ |
281 | + nla_total_size(4) /* LWTUNNEL_IP_DST */ | |
282 | + nla_total_size(4) /* LWTUNNEL_IP_SRC */ | |
283 | + nla_total_size(1) /* LWTUNNEL_IP_TOS */ | |
284 | + nla_total_size(1) /* LWTUNNEL_IP_TTL */ | |
285 | + nla_total_size(2) /* LWTUNNEL_IP_SPORT */ | |
286 | + nla_total_size(2) /* LWTUNNEL_IP_DPORT */ | |
287 | + nla_total_size(2); /* LWTUNNEL_IP_FLAGS */ | |
3093fbe7 TG |
288 | } |
289 | ||
2d798499 JB |
290 | static int ip_tun_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b) |
291 | { | |
292 | return memcmp(lwt_tun_info(a), lwt_tun_info(b), | |
293 | sizeof(struct ip_tunnel_info)); | |
294 | } | |
295 | ||
3093fbe7 TG |
296 | static const struct lwtunnel_encap_ops ip_tun_lwt_ops = { |
297 | .build_state = ip_tun_build_state, | |
298 | .fill_encap = ip_tun_fill_encap_info, | |
299 | .get_encap_size = ip_tun_encap_nlsize, | |
2d798499 | 300 | .cmp_encap = ip_tun_cmp_encap, |
3093fbe7 TG |
301 | }; |
302 | ||
32a2b002 JB |
303 | static const struct nla_policy ip6_tun_policy[LWTUNNEL_IP6_MAX + 1] = { |
304 | [LWTUNNEL_IP6_ID] = { .type = NLA_U64 }, | |
305 | [LWTUNNEL_IP6_DST] = { .len = sizeof(struct in6_addr) }, | |
306 | [LWTUNNEL_IP6_SRC] = { .len = sizeof(struct in6_addr) }, | |
307 | [LWTUNNEL_IP6_HOPLIMIT] = { .type = NLA_U8 }, | |
308 | [LWTUNNEL_IP6_TC] = { .type = NLA_U8 }, | |
309 | [LWTUNNEL_IP6_SPORT] = { .type = NLA_U16 }, | |
310 | [LWTUNNEL_IP6_DPORT] = { .type = NLA_U16 }, | |
311 | [LWTUNNEL_IP6_FLAGS] = { .type = NLA_U16 }, | |
312 | }; | |
313 | ||
314 | static int ip6_tun_build_state(struct net_device *dev, struct nlattr *attr, | |
127eb7cd | 315 | unsigned int family, const void *cfg, |
32a2b002 JB |
316 | struct lwtunnel_state **ts) |
317 | { | |
318 | struct ip_tunnel_info *tun_info; | |
319 | struct lwtunnel_state *new_state; | |
320 | struct nlattr *tb[LWTUNNEL_IP6_MAX + 1]; | |
321 | int err; | |
322 | ||
323 | err = nla_parse_nested(tb, LWTUNNEL_IP6_MAX, attr, ip6_tun_policy); | |
324 | if (err < 0) | |
325 | return err; | |
326 | ||
327 | new_state = lwtunnel_state_alloc(sizeof(*tun_info)); | |
328 | if (!new_state) | |
329 | return -ENOMEM; | |
330 | ||
331 | new_state->type = LWTUNNEL_ENCAP_IP6; | |
332 | ||
333 | tun_info = lwt_tun_info(new_state); | |
334 | ||
335 | if (tb[LWTUNNEL_IP6_ID]) | |
336 | tun_info->key.tun_id = nla_get_u64(tb[LWTUNNEL_IP6_ID]); | |
337 | ||
338 | if (tb[LWTUNNEL_IP6_DST]) | |
339 | tun_info->key.u.ipv6.dst = nla_get_in6_addr(tb[LWTUNNEL_IP6_DST]); | |
340 | ||
341 | if (tb[LWTUNNEL_IP6_SRC]) | |
342 | tun_info->key.u.ipv6.src = nla_get_in6_addr(tb[LWTUNNEL_IP6_SRC]); | |
343 | ||
344 | if (tb[LWTUNNEL_IP6_HOPLIMIT]) | |
345 | tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP6_HOPLIMIT]); | |
346 | ||
347 | if (tb[LWTUNNEL_IP6_TC]) | |
348 | tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP6_TC]); | |
349 | ||
350 | if (tb[LWTUNNEL_IP6_SPORT]) | |
351 | tun_info->key.tp_src = nla_get_be16(tb[LWTUNNEL_IP6_SPORT]); | |
352 | ||
353 | if (tb[LWTUNNEL_IP6_DPORT]) | |
354 | tun_info->key.tp_dst = nla_get_be16(tb[LWTUNNEL_IP6_DPORT]); | |
355 | ||
356 | if (tb[LWTUNNEL_IP6_FLAGS]) | |
357 | tun_info->key.tun_flags = nla_get_u16(tb[LWTUNNEL_IP6_FLAGS]); | |
358 | ||
359 | tun_info->mode = IP_TUNNEL_INFO_TX; | |
360 | tun_info->options = NULL; | |
361 | tun_info->options_len = 0; | |
362 | ||
363 | *ts = new_state; | |
364 | ||
365 | return 0; | |
366 | } | |
367 | ||
368 | static int ip6_tun_fill_encap_info(struct sk_buff *skb, | |
369 | struct lwtunnel_state *lwtstate) | |
370 | { | |
371 | struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate); | |
372 | ||
373 | if (nla_put_u64(skb, LWTUNNEL_IP6_ID, tun_info->key.tun_id) || | |
374 | nla_put_in6_addr(skb, LWTUNNEL_IP6_DST, &tun_info->key.u.ipv6.dst) || | |
375 | nla_put_in6_addr(skb, LWTUNNEL_IP6_SRC, &tun_info->key.u.ipv6.src) || | |
376 | nla_put_u8(skb, LWTUNNEL_IP6_HOPLIMIT, tun_info->key.tos) || | |
377 | nla_put_u8(skb, LWTUNNEL_IP6_TC, tun_info->key.ttl) || | |
378 | nla_put_u16(skb, LWTUNNEL_IP6_SPORT, tun_info->key.tp_src) || | |
379 | nla_put_u16(skb, LWTUNNEL_IP6_DPORT, tun_info->key.tp_dst) || | |
380 | nla_put_u16(skb, LWTUNNEL_IP6_FLAGS, tun_info->key.tun_flags)) | |
381 | return -ENOMEM; | |
382 | ||
383 | return 0; | |
384 | } | |
385 | ||
386 | static int ip6_tun_encap_nlsize(struct lwtunnel_state *lwtstate) | |
387 | { | |
388 | return nla_total_size(8) /* LWTUNNEL_IP6_ID */ | |
389 | + nla_total_size(16) /* LWTUNNEL_IP6_DST */ | |
390 | + nla_total_size(16) /* LWTUNNEL_IP6_SRC */ | |
391 | + nla_total_size(1) /* LWTUNNEL_IP6_HOPLIMIT */ | |
392 | + nla_total_size(1) /* LWTUNNEL_IP6_TC */ | |
393 | + nla_total_size(2) /* LWTUNNEL_IP6_SPORT */ | |
394 | + nla_total_size(2) /* LWTUNNEL_IP6_DPORT */ | |
395 | + nla_total_size(2); /* LWTUNNEL_IP6_FLAGS */ | |
396 | } | |
397 | ||
398 | static const struct lwtunnel_encap_ops ip6_tun_lwt_ops = { | |
399 | .build_state = ip6_tun_build_state, | |
400 | .fill_encap = ip6_tun_fill_encap_info, | |
401 | .get_encap_size = ip6_tun_encap_nlsize, | |
402 | .cmp_encap = ip_tun_cmp_encap, | |
403 | }; | |
404 | ||
045a0fa0 | 405 | void __init ip_tunnel_core_init(void) |
3093fbe7 TG |
406 | { |
407 | lwtunnel_encap_add_ops(&ip_tun_lwt_ops, LWTUNNEL_ENCAP_IP); | |
32a2b002 | 408 | lwtunnel_encap_add_ops(&ip6_tun_lwt_ops, LWTUNNEL_ENCAP_IP6); |
3093fbe7 | 409 | } |
e7030878 TG |
410 | |
411 | struct static_key ip_tunnel_metadata_cnt = STATIC_KEY_INIT_FALSE; | |
412 | EXPORT_SYMBOL(ip_tunnel_metadata_cnt); | |
413 | ||
414 | void ip_tunnel_need_metadata(void) | |
415 | { | |
416 | static_key_slow_inc(&ip_tunnel_metadata_cnt); | |
417 | } | |
418 | EXPORT_SYMBOL_GPL(ip_tunnel_need_metadata); | |
419 | ||
420 | void ip_tunnel_unneed_metadata(void) | |
421 | { | |
422 | static_key_slow_dec(&ip_tunnel_metadata_cnt); | |
423 | } | |
424 | EXPORT_SYMBOL_GPL(ip_tunnel_unneed_metadata); |