xref: /openbmc/linux/net/ipv4/ip_tunnel_core.c (revision 3093fbe7)
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>
35 
36 #include <net/ip.h>
37 #include <net/icmp.h>
38 #include <net/protocol.h>
39 #include <net/ip_tunnels.h>
40 #include <net/arp.h>
41 #include <net/checksum.h>
42 #include <net/dsfield.h>
43 #include <net/inet_ecn.h>
44 #include <net/xfrm.h>
45 #include <net/net_namespace.h>
46 #include <net/netns/generic.h>
47 #include <net/rtnetlink.h>
48 
49 int iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
50 		  __be32 src, __be32 dst, __u8 proto,
51 		  __u8 tos, __u8 ttl, __be16 df, bool xnet)
52 {
53 	int pkt_len = skb->len;
54 	struct iphdr *iph;
55 	int err;
56 
57 	skb_scrub_packet(skb, xnet);
58 
59 	skb_clear_hash(skb);
60 	skb_dst_set(skb, &rt->dst);
61 	memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
62 
63 	/* Push down and install the IP header. */
64 	skb_push(skb, sizeof(struct iphdr));
65 	skb_reset_network_header(skb);
66 
67 	iph = ip_hdr(skb);
68 
69 	iph->version	=	4;
70 	iph->ihl	=	sizeof(struct iphdr) >> 2;
71 	iph->frag_off	=	df;
72 	iph->protocol	=	proto;
73 	iph->tos	=	tos;
74 	iph->daddr	=	dst;
75 	iph->saddr	=	src;
76 	iph->ttl	=	ttl;
77 	__ip_select_ident(dev_net(rt->dst.dev), iph,
78 			  skb_shinfo(skb)->gso_segs ?: 1);
79 
80 	err = ip_local_out_sk(sk, skb);
81 	if (unlikely(net_xmit_eval(err)))
82 		pkt_len = 0;
83 	return pkt_len;
84 }
85 EXPORT_SYMBOL_GPL(iptunnel_xmit);
86 
87 int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto)
88 {
89 	if (unlikely(!pskb_may_pull(skb, hdr_len)))
90 		return -ENOMEM;
91 
92 	skb_pull_rcsum(skb, hdr_len);
93 
94 	if (inner_proto == htons(ETH_P_TEB)) {
95 		struct ethhdr *eh;
96 
97 		if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
98 			return -ENOMEM;
99 
100 		eh = (struct ethhdr *)skb->data;
101 		if (likely(eth_proto_is_802_3(eh->h_proto)))
102 			skb->protocol = eh->h_proto;
103 		else
104 			skb->protocol = htons(ETH_P_802_2);
105 
106 	} else {
107 		skb->protocol = inner_proto;
108 	}
109 
110 	nf_reset(skb);
111 	secpath_reset(skb);
112 	skb_clear_hash_if_not_l4(skb);
113 	skb_dst_drop(skb);
114 	skb->vlan_tci = 0;
115 	skb_set_queue_mapping(skb, 0);
116 	skb->pkt_type = PACKET_HOST;
117 	return 0;
118 }
119 EXPORT_SYMBOL_GPL(iptunnel_pull_header);
120 
121 struct sk_buff *iptunnel_handle_offloads(struct sk_buff *skb,
122 					 bool csum_help,
123 					 int gso_type_mask)
124 {
125 	int err;
126 
127 	if (likely(!skb->encapsulation)) {
128 		skb_reset_inner_headers(skb);
129 		skb->encapsulation = 1;
130 	}
131 
132 	if (skb_is_gso(skb)) {
133 		err = skb_unclone(skb, GFP_ATOMIC);
134 		if (unlikely(err))
135 			goto error;
136 		skb_shinfo(skb)->gso_type |= gso_type_mask;
137 		return skb;
138 	}
139 
140 	/* If packet is not gso and we are resolving any partial checksum,
141 	 * clear encapsulation flag. This allows setting CHECKSUM_PARTIAL
142 	 * on the outer header without confusing devices that implement
143 	 * NETIF_F_IP_CSUM with encapsulation.
144 	 */
145 	if (csum_help)
146 		skb->encapsulation = 0;
147 
148 	if (skb->ip_summed == CHECKSUM_PARTIAL && csum_help) {
149 		err = skb_checksum_help(skb);
150 		if (unlikely(err))
151 			goto error;
152 	} else if (skb->ip_summed != CHECKSUM_PARTIAL)
153 		skb->ip_summed = CHECKSUM_NONE;
154 
155 	return skb;
156 error:
157 	kfree_skb(skb);
158 	return ERR_PTR(err);
159 }
160 EXPORT_SYMBOL_GPL(iptunnel_handle_offloads);
161 
162 /* Often modified stats are per cpu, other are shared (netdev->stats) */
163 struct rtnl_link_stats64 *ip_tunnel_get_stats64(struct net_device *dev,
164 						struct rtnl_link_stats64 *tot)
165 {
166 	int i;
167 
168 	netdev_stats_to_stats64(tot, &dev->stats);
169 
170 	for_each_possible_cpu(i) {
171 		const struct pcpu_sw_netstats *tstats =
172 						   per_cpu_ptr(dev->tstats, i);
173 		u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
174 		unsigned int start;
175 
176 		do {
177 			start = u64_stats_fetch_begin_irq(&tstats->syncp);
178 			rx_packets = tstats->rx_packets;
179 			tx_packets = tstats->tx_packets;
180 			rx_bytes = tstats->rx_bytes;
181 			tx_bytes = tstats->tx_bytes;
182 		} while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
183 
184 		tot->rx_packets += rx_packets;
185 		tot->tx_packets += tx_packets;
186 		tot->rx_bytes   += rx_bytes;
187 		tot->tx_bytes   += tx_bytes;
188 	}
189 
190 	return tot;
191 }
192 EXPORT_SYMBOL_GPL(ip_tunnel_get_stats64);
193 
194 static const struct nla_policy ip_tun_policy[IP_TUN_MAX + 1] = {
195 	[IP_TUN_ID]		= { .type = NLA_U64 },
196 	[IP_TUN_DST]		= { .type = NLA_U32 },
197 	[IP_TUN_SRC]		= { .type = NLA_U32 },
198 	[IP_TUN_TTL]		= { .type = NLA_U8 },
199 	[IP_TUN_TOS]		= { .type = NLA_U8 },
200 	[IP_TUN_SPORT]		= { .type = NLA_U16 },
201 	[IP_TUN_DPORT]		= { .type = NLA_U16 },
202 	[IP_TUN_FLAGS]		= { .type = NLA_U16 },
203 };
204 
205 static int ip_tun_build_state(struct net_device *dev, struct nlattr *attr,
206 			      struct lwtunnel_state **ts)
207 {
208 	struct ip_tunnel_info *tun_info;
209 	struct lwtunnel_state *new_state;
210 	struct nlattr *tb[IP_TUN_MAX + 1];
211 	int err;
212 
213 	err = nla_parse_nested(tb, IP_TUN_MAX, attr, ip_tun_policy);
214 	if (err < 0)
215 		return err;
216 
217 	new_state = lwtunnel_state_alloc(sizeof(*tun_info));
218 	if (!new_state)
219 		return -ENOMEM;
220 
221 	new_state->type = LWTUNNEL_ENCAP_IP;
222 
223 	tun_info = lwt_tun_info(new_state);
224 
225 	if (tb[IP_TUN_ID])
226 		tun_info->key.tun_id = nla_get_u64(tb[IP_TUN_ID]);
227 
228 	if (tb[IP_TUN_DST])
229 		tun_info->key.ipv4_dst = nla_get_be32(tb[IP_TUN_DST]);
230 
231 	if (tb[IP_TUN_SRC])
232 		tun_info->key.ipv4_src = nla_get_be32(tb[IP_TUN_SRC]);
233 
234 	if (tb[IP_TUN_TTL])
235 		tun_info->key.ipv4_ttl = nla_get_u8(tb[IP_TUN_TTL]);
236 
237 	if (tb[IP_TUN_TOS])
238 		tun_info->key.ipv4_tos = nla_get_u8(tb[IP_TUN_TOS]);
239 
240 	if (tb[IP_TUN_SPORT])
241 		tun_info->key.tp_src = nla_get_be16(tb[IP_TUN_SPORT]);
242 
243 	if (tb[IP_TUN_DPORT])
244 		tun_info->key.tp_dst = nla_get_be16(tb[IP_TUN_DPORT]);
245 
246 	if (tb[IP_TUN_FLAGS])
247 		tun_info->key.tun_flags = nla_get_u16(tb[IP_TUN_FLAGS]);
248 
249 	tun_info->mode = IP_TUNNEL_INFO_TX;
250 	tun_info->options = NULL;
251 	tun_info->options_len = 0;
252 
253 	*ts = new_state;
254 
255 	return 0;
256 }
257 
258 static int ip_tun_fill_encap_info(struct sk_buff *skb,
259 				  struct lwtunnel_state *lwtstate)
260 {
261 	struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
262 
263 	if (nla_put_u64(skb, IP_TUN_ID, tun_info->key.tun_id) ||
264 	    nla_put_be32(skb, IP_TUN_DST, tun_info->key.ipv4_dst) ||
265 	    nla_put_be32(skb, IP_TUN_SRC, tun_info->key.ipv4_src) ||
266 	    nla_put_u8(skb, IP_TUN_TOS, tun_info->key.ipv4_tos) ||
267 	    nla_put_u8(skb, IP_TUN_TTL, tun_info->key.ipv4_ttl) ||
268 	    nla_put_u16(skb, IP_TUN_SPORT, tun_info->key.tp_src) ||
269 	    nla_put_u16(skb, IP_TUN_DPORT, tun_info->key.tp_dst) ||
270 	    nla_put_u16(skb, IP_TUN_FLAGS, tun_info->key.tun_flags))
271 		return -ENOMEM;
272 
273 	return 0;
274 }
275 
276 static int ip_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
277 {
278 	return nla_total_size(8)	/* IP_TUN_ID */
279 		+ nla_total_size(4)	/* IP_TUN_DST */
280 		+ nla_total_size(4)	/* IP_TUN_SRC */
281 		+ nla_total_size(1)	/* IP_TUN_TOS */
282 		+ nla_total_size(1)	/* IP_TUN_TTL */
283 		+ nla_total_size(2)	/* IP_TUN_SPORT */
284 		+ nla_total_size(2)	/* IP_TUN_DPORT */
285 		+ nla_total_size(2);	/* IP_TUN_FLAGS */
286 }
287 
288 static const struct lwtunnel_encap_ops ip_tun_lwt_ops = {
289 	.build_state = ip_tun_build_state,
290 	.fill_encap = ip_tun_fill_encap_info,
291 	.get_encap_size = ip_tun_encap_nlsize,
292 };
293 
294 static int __init ip_tunnel_core_init(void)
295 {
296 	lwtunnel_encap_add_ops(&ip_tun_lwt_ops, LWTUNNEL_ENCAP_IP);
297 
298 	return 0;
299 }
300 module_init(ip_tunnel_core_init);
301 
302 static void __exit ip_tunnel_core_exit(void)
303 {
304 	lwtunnel_encap_del_ops(&ip_tun_lwt_ops, LWTUNNEL_ENCAP_IP);
305 }
306 module_exit(ip_tunnel_core_exit);
307