]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
e905a9ed | 2 | * Linux NET3: GRE over IP protocol decoder. |
1da177e4 LT |
3 | * |
4 | * Authors: Alexey Kuznetsov (kuznet@ms2.inr.ac.ru) | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version | |
9 | * 2 of the License, or (at your option) any later version. | |
10 | * | |
11 | */ | |
12 | ||
afd46503 JP |
13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
14 | ||
4fc268d2 | 15 | #include <linux/capability.h> |
1da177e4 LT |
16 | #include <linux/module.h> |
17 | #include <linux/types.h> | |
1da177e4 | 18 | #include <linux/kernel.h> |
5a0e3ad6 | 19 | #include <linux/slab.h> |
1da177e4 LT |
20 | #include <asm/uaccess.h> |
21 | #include <linux/skbuff.h> | |
22 | #include <linux/netdevice.h> | |
23 | #include <linux/in.h> | |
24 | #include <linux/tcp.h> | |
25 | #include <linux/udp.h> | |
26 | #include <linux/if_arp.h> | |
27 | #include <linux/mroute.h> | |
2e15ea39 | 28 | #include <linux/if_vlan.h> |
1da177e4 LT |
29 | #include <linux/init.h> |
30 | #include <linux/in6.h> | |
31 | #include <linux/inetdevice.h> | |
32 | #include <linux/igmp.h> | |
33 | #include <linux/netfilter_ipv4.h> | |
e1a80002 | 34 | #include <linux/etherdevice.h> |
46f25dff | 35 | #include <linux/if_ether.h> |
1da177e4 LT |
36 | |
37 | #include <net/sock.h> | |
38 | #include <net/ip.h> | |
39 | #include <net/icmp.h> | |
40 | #include <net/protocol.h> | |
c5441932 | 41 | #include <net/ip_tunnels.h> |
1da177e4 LT |
42 | #include <net/arp.h> |
43 | #include <net/checksum.h> | |
44 | #include <net/dsfield.h> | |
45 | #include <net/inet_ecn.h> | |
46 | #include <net/xfrm.h> | |
59a4c759 PE |
47 | #include <net/net_namespace.h> |
48 | #include <net/netns/generic.h> | |
c19e654d | 49 | #include <net/rtnetlink.h> |
00959ade | 50 | #include <net/gre.h> |
2e15ea39 | 51 | #include <net/dst_metadata.h> |
1da177e4 | 52 | |
dfd56b8b | 53 | #if IS_ENABLED(CONFIG_IPV6) |
1da177e4 LT |
54 | #include <net/ipv6.h> |
55 | #include <net/ip6_fib.h> | |
56 | #include <net/ip6_route.h> | |
57 | #endif | |
58 | ||
59 | /* | |
60 | Problems & solutions | |
61 | -------------------- | |
62 | ||
63 | 1. The most important issue is detecting local dead loops. | |
64 | They would cause complete host lockup in transmit, which | |
65 | would be "resolved" by stack overflow or, if queueing is enabled, | |
66 | with infinite looping in net_bh. | |
67 | ||
68 | We cannot track such dead loops during route installation, | |
69 | it is infeasible task. The most general solutions would be | |
70 | to keep skb->encapsulation counter (sort of local ttl), | |
6d0722a2 | 71 | and silently drop packet when it expires. It is a good |
bff52857 | 72 | solution, but it supposes maintaining new variable in ALL |
1da177e4 LT |
73 | skb, even if no tunneling is used. |
74 | ||
6d0722a2 ED |
75 | Current solution: xmit_recursion breaks dead loops. This is a percpu |
76 | counter, since when we enter the first ndo_xmit(), cpu migration is | |
77 | forbidden. We force an exit if this counter reaches RECURSION_LIMIT | |
1da177e4 LT |
78 | |
79 | 2. Networking dead loops would not kill routers, but would really | |
80 | kill network. IP hop limit plays role of "t->recursion" in this case, | |
81 | if we copy it from packet being encapsulated to upper header. | |
82 | It is very good solution, but it introduces two problems: | |
83 | ||
84 | - Routing protocols, using packets with ttl=1 (OSPF, RIP2), | |
85 | do not work over tunnels. | |
86 | - traceroute does not work. I planned to relay ICMP from tunnel, | |
87 | so that this problem would be solved and traceroute output | |
88 | would even more informative. This idea appeared to be wrong: | |
89 | only Linux complies to rfc1812 now (yes, guys, Linux is the only | |
90 | true router now :-)), all routers (at least, in neighbourhood of mine) | |
91 | return only 8 bytes of payload. It is the end. | |
92 | ||
93 | Hence, if we want that OSPF worked or traceroute said something reasonable, | |
94 | we should search for another solution. | |
95 | ||
96 | One of them is to parse packet trying to detect inner encapsulation | |
97 | made by our node. It is difficult or even impossible, especially, | |
bff52857 | 98 | taking into account fragmentation. TO be short, ttl is not solution at all. |
1da177e4 LT |
99 | |
100 | Current solution: The solution was UNEXPECTEDLY SIMPLE. | |
101 | We force DF flag on tunnels with preconfigured hop limit, | |
102 | that is ALL. :-) Well, it does not remove the problem completely, | |
103 | but exponential growth of network traffic is changed to linear | |
104 | (branches, that exceed pmtu are pruned) and tunnel mtu | |
bff52857 | 105 | rapidly degrades to value <68, where looping stops. |
1da177e4 LT |
106 | Yes, it is not good if there exists a router in the loop, |
107 | which does not force DF, even when encapsulating packets have DF set. | |
108 | But it is not our problem! Nobody could accuse us, we made | |
109 | all that we could make. Even if it is your gated who injected | |
110 | fatal route to network, even if it were you who configured | |
111 | fatal static route: you are innocent. :-) | |
112 | ||
1da177e4 LT |
113 | Alexey Kuznetsov. |
114 | */ | |
115 | ||
eccc1bb8 | 116 | static bool log_ecn_error = true; |
117 | module_param(log_ecn_error, bool, 0644); | |
118 | MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); | |
119 | ||
c19e654d | 120 | static struct rtnl_link_ops ipgre_link_ops __read_mostly; |
1da177e4 | 121 | static int ipgre_tunnel_init(struct net_device *dev); |
eb8ce741 | 122 | |
f99189b1 | 123 | static int ipgre_net_id __read_mostly; |
c5441932 | 124 | static int gre_tap_net_id __read_mostly; |
1da177e4 | 125 | |
bda7bb46 PS |
126 | static int ipgre_err(struct sk_buff *skb, u32 info, |
127 | const struct tnl_ptk_info *tpi) | |
1da177e4 | 128 | { |
1da177e4 | 129 | |
c5441932 PS |
130 | /* All the routers (except for Linux) return only |
131 | 8 bytes of packet payload. It means, that precise relaying of | |
132 | ICMP in the real Internet is absolutely infeasible. | |
1da177e4 | 133 | |
c5441932 PS |
134 | Moreover, Cisco "wise men" put GRE key to the third word |
135 | in GRE header. It makes impossible maintaining even soft | |
136 | state for keyed GRE tunnels with enabled checksum. Tell | |
137 | them "thank you". | |
1da177e4 | 138 | |
c5441932 PS |
139 | Well, I wonder, rfc1812 was written by Cisco employee, |
140 | what the hell these idiots break standards established | |
141 | by themselves??? | |
142 | */ | |
143 | struct net *net = dev_net(skb->dev); | |
144 | struct ip_tunnel_net *itn; | |
96f5a846 | 145 | const struct iphdr *iph; |
88c7664f ACM |
146 | const int type = icmp_hdr(skb)->type; |
147 | const int code = icmp_hdr(skb)->code; | |
1da177e4 | 148 | struct ip_tunnel *t; |
1da177e4 | 149 | |
1da177e4 LT |
150 | switch (type) { |
151 | default: | |
152 | case ICMP_PARAMETERPROB: | |
bda7bb46 | 153 | return PACKET_RCVD; |
1da177e4 LT |
154 | |
155 | case ICMP_DEST_UNREACH: | |
156 | switch (code) { | |
157 | case ICMP_SR_FAILED: | |
158 | case ICMP_PORT_UNREACH: | |
159 | /* Impossible event. */ | |
bda7bb46 | 160 | return PACKET_RCVD; |
1da177e4 LT |
161 | default: |
162 | /* All others are translated to HOST_UNREACH. | |
163 | rfc2003 contains "deep thoughts" about NET_UNREACH, | |
164 | I believe they are just ether pollution. --ANK | |
165 | */ | |
166 | break; | |
167 | } | |
168 | break; | |
169 | case ICMP_TIME_EXCEEDED: | |
170 | if (code != ICMP_EXC_TTL) | |
bda7bb46 | 171 | return PACKET_RCVD; |
1da177e4 | 172 | break; |
55be7a9c DM |
173 | |
174 | case ICMP_REDIRECT: | |
175 | break; | |
1da177e4 LT |
176 | } |
177 | ||
bda7bb46 | 178 | if (tpi->proto == htons(ETH_P_TEB)) |
c5441932 PS |
179 | itn = net_generic(net, gre_tap_net_id); |
180 | else | |
181 | itn = net_generic(net, ipgre_net_id); | |
182 | ||
c0c0c50f | 183 | iph = (const struct iphdr *)(icmp_hdr(skb) + 1); |
bda7bb46 PS |
184 | t = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags, |
185 | iph->daddr, iph->saddr, tpi->key); | |
d2083287 | 186 | |
51456b29 | 187 | if (!t) |
bda7bb46 | 188 | return PACKET_REJECT; |
36393395 | 189 | |
36393395 | 190 | if (t->parms.iph.daddr == 0 || |
f97c1e0c | 191 | ipv4_is_multicast(t->parms.iph.daddr)) |
bda7bb46 | 192 | return PACKET_RCVD; |
1da177e4 LT |
193 | |
194 | if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED) | |
bda7bb46 | 195 | return PACKET_RCVD; |
1da177e4 | 196 | |
da6185d8 | 197 | if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO)) |
1da177e4 LT |
198 | t->err_count++; |
199 | else | |
200 | t->err_count = 1; | |
201 | t->err_time = jiffies; | |
bda7bb46 | 202 | return PACKET_RCVD; |
1da177e4 LT |
203 | } |
204 | ||
2e15ea39 PS |
205 | static __be64 key_to_tunnel_id(__be32 key) |
206 | { | |
207 | #ifdef __BIG_ENDIAN | |
208 | return (__force __be64)((__force u32)key); | |
209 | #else | |
210 | return (__force __be64)((__force u64)key << 32); | |
211 | #endif | |
212 | } | |
213 | ||
214 | /* Returns the least-significant 32 bits of a __be64. */ | |
215 | static __be32 tunnel_id_to_key(__be64 x) | |
216 | { | |
217 | #ifdef __BIG_ENDIAN | |
218 | return (__force __be32)x; | |
219 | #else | |
220 | return (__force __be32)((__force u64)x >> 32); | |
221 | #endif | |
222 | } | |
223 | ||
bda7bb46 | 224 | static int ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi) |
1da177e4 | 225 | { |
c5441932 | 226 | struct net *net = dev_net(skb->dev); |
2e15ea39 | 227 | struct metadata_dst *tun_dst = NULL; |
c5441932 | 228 | struct ip_tunnel_net *itn; |
b71d1d42 | 229 | const struct iphdr *iph; |
1da177e4 | 230 | struct ip_tunnel *tunnel; |
1da177e4 | 231 | |
bda7bb46 | 232 | if (tpi->proto == htons(ETH_P_TEB)) |
c5441932 PS |
233 | itn = net_generic(net, gre_tap_net_id); |
234 | else | |
235 | itn = net_generic(net, ipgre_net_id); | |
1da177e4 | 236 | |
c5441932 | 237 | iph = ip_hdr(skb); |
bda7bb46 PS |
238 | tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags, |
239 | iph->saddr, iph->daddr, tpi->key); | |
e1a80002 | 240 | |
d2083287 | 241 | if (tunnel) { |
0e3da5bb | 242 | skb_pop_mac_header(skb); |
2e15ea39 PS |
243 | if (tunnel->collect_md) { |
244 | struct ip_tunnel_info *info; | |
245 | ||
246 | tun_dst = metadata_dst_alloc(0, GFP_ATOMIC); | |
247 | if (!tun_dst) | |
248 | return PACKET_REJECT; | |
249 | ||
250 | info = &tun_dst->u.tun_info; | |
251 | info->key.ipv4_src = iph->saddr; | |
252 | info->key.ipv4_dst = iph->daddr; | |
253 | info->key.ipv4_tos = iph->tos; | |
254 | info->key.ipv4_ttl = iph->ttl; | |
255 | ||
256 | info->mode = IP_TUNNEL_INFO_RX; | |
257 | info->key.tun_flags = tpi->flags & | |
258 | (TUNNEL_CSUM | TUNNEL_KEY); | |
259 | info->key.tun_id = key_to_tunnel_id(tpi->key); | |
260 | ||
261 | info->key.tp_src = 0; | |
262 | info->key.tp_dst = 0; | |
263 | } | |
264 | ||
265 | ip_tunnel_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error); | |
bda7bb46 | 266 | return PACKET_RCVD; |
1da177e4 | 267 | } |
bda7bb46 | 268 | return PACKET_REJECT; |
1da177e4 LT |
269 | } |
270 | ||
2e15ea39 PS |
271 | static void build_header(struct sk_buff *skb, int hdr_len, __be16 flags, |
272 | __be16 proto, __be32 key, __be32 seq) | |
273 | { | |
274 | struct gre_base_hdr *greh; | |
275 | ||
276 | skb_push(skb, hdr_len); | |
277 | ||
278 | skb_reset_transport_header(skb); | |
279 | greh = (struct gre_base_hdr *)skb->data; | |
280 | greh->flags = tnl_flags_to_gre_flags(flags); | |
281 | greh->protocol = proto; | |
282 | ||
283 | if (flags & (TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_SEQ)) { | |
284 | __be32 *ptr = (__be32 *)(((u8 *)greh) + hdr_len - 4); | |
285 | ||
286 | if (flags & TUNNEL_SEQ) { | |
287 | *ptr = seq; | |
288 | ptr--; | |
289 | } | |
290 | if (flags & TUNNEL_KEY) { | |
291 | *ptr = key; | |
292 | ptr--; | |
293 | } | |
294 | if (flags & TUNNEL_CSUM && | |
295 | !(skb_shinfo(skb)->gso_type & | |
296 | (SKB_GSO_GRE | SKB_GSO_GRE_CSUM))) { | |
297 | *ptr = 0; | |
298 | *(__sum16 *)ptr = csum_fold(skb_checksum(skb, 0, | |
299 | skb->len, 0)); | |
300 | } | |
301 | } | |
302 | } | |
303 | ||
c5441932 PS |
304 | static void __gre_xmit(struct sk_buff *skb, struct net_device *dev, |
305 | const struct iphdr *tnl_params, | |
306 | __be16 proto) | |
307 | { | |
308 | struct ip_tunnel *tunnel = netdev_priv(dev); | |
1da177e4 | 309 | |
c5441932 PS |
310 | if (tunnel->parms.o_flags & TUNNEL_SEQ) |
311 | tunnel->o_seqno++; | |
1da177e4 | 312 | |
c5441932 | 313 | /* Push GRE header. */ |
2e15ea39 PS |
314 | build_header(skb, tunnel->tun_hlen, tunnel->parms.o_flags, |
315 | proto, tunnel->parms.o_key, htonl(tunnel->o_seqno)); | |
54bc9bac | 316 | |
2e15ea39 | 317 | skb_set_inner_protocol(skb, proto); |
bf3d6a8f | 318 | ip_tunnel_xmit(skb, dev, tnl_params, tnl_params->protocol); |
c5441932 | 319 | } |
1da177e4 | 320 | |
b2acd1dc PS |
321 | static struct sk_buff *gre_handle_offloads(struct sk_buff *skb, |
322 | bool csum) | |
323 | { | |
324 | return iptunnel_handle_offloads(skb, csum, | |
325 | csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE); | |
326 | } | |
327 | ||
2e15ea39 PS |
328 | static void gre_fb_xmit(struct sk_buff *skb, struct net_device *dev) |
329 | { | |
330 | struct ip_tunnel_info *tun_info; | |
331 | struct net *net = dev_net(dev); | |
332 | const struct ip_tunnel_key *key; | |
333 | struct flowi4 fl; | |
334 | struct rtable *rt; | |
335 | int min_headroom; | |
336 | int tunnel_hlen; | |
337 | __be16 df, flags; | |
338 | int err; | |
339 | ||
340 | tun_info = skb_tunnel_info(skb, AF_INET); | |
341 | if (unlikely(!tun_info || tun_info->mode != IP_TUNNEL_INFO_TX)) | |
342 | goto err_free_skb; | |
343 | ||
344 | key = &tun_info->key; | |
345 | memset(&fl, 0, sizeof(fl)); | |
346 | fl.daddr = key->ipv4_dst; | |
347 | fl.saddr = key->ipv4_src; | |
348 | fl.flowi4_tos = RT_TOS(key->ipv4_tos); | |
349 | fl.flowi4_mark = skb->mark; | |
350 | fl.flowi4_proto = IPPROTO_GRE; | |
351 | ||
352 | rt = ip_route_output_key(net, &fl); | |
353 | if (IS_ERR(rt)) | |
354 | goto err_free_skb; | |
355 | ||
356 | tunnel_hlen = ip_gre_calc_hlen(key->tun_flags); | |
357 | ||
358 | min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len | |
359 | + tunnel_hlen + sizeof(struct iphdr); | |
360 | if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) { | |
361 | int head_delta = SKB_DATA_ALIGN(min_headroom - | |
362 | skb_headroom(skb) + | |
363 | 16); | |
364 | err = pskb_expand_head(skb, max_t(int, head_delta, 0), | |
365 | 0, GFP_ATOMIC); | |
366 | if (unlikely(err)) | |
367 | goto err_free_rt; | |
368 | } | |
369 | ||
370 | /* Push Tunnel header. */ | |
371 | skb = gre_handle_offloads(skb, !!(tun_info->key.tun_flags & TUNNEL_CSUM)); | |
372 | if (IS_ERR(skb)) { | |
373 | skb = NULL; | |
374 | goto err_free_rt; | |
375 | } | |
376 | ||
377 | flags = tun_info->key.tun_flags & (TUNNEL_CSUM | TUNNEL_KEY); | |
378 | build_header(skb, tunnel_hlen, flags, htons(ETH_P_TEB), | |
379 | tunnel_id_to_key(tun_info->key.tun_id), 0); | |
380 | ||
381 | df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0; | |
382 | err = iptunnel_xmit(skb->sk, rt, skb, fl.saddr, | |
383 | key->ipv4_dst, IPPROTO_GRE, | |
384 | key->ipv4_tos, key->ipv4_ttl, df, false); | |
385 | iptunnel_xmit_stats(err, &dev->stats, dev->tstats); | |
386 | return; | |
387 | ||
388 | err_free_rt: | |
389 | ip_rt_put(rt); | |
390 | err_free_skb: | |
391 | kfree_skb(skb); | |
392 | dev->stats.tx_dropped++; | |
393 | } | |
394 | ||
c5441932 PS |
395 | static netdev_tx_t ipgre_xmit(struct sk_buff *skb, |
396 | struct net_device *dev) | |
397 | { | |
398 | struct ip_tunnel *tunnel = netdev_priv(dev); | |
399 | const struct iphdr *tnl_params; | |
1da177e4 | 400 | |
2e15ea39 PS |
401 | if (tunnel->collect_md) { |
402 | gre_fb_xmit(skb, dev); | |
403 | return NETDEV_TX_OK; | |
404 | } | |
405 | ||
c5441932 PS |
406 | if (dev->header_ops) { |
407 | /* Need space for new headers */ | |
408 | if (skb_cow_head(skb, dev->needed_headroom - | |
2bac7cb3 | 409 | (tunnel->hlen + sizeof(struct iphdr)))) |
c5441932 | 410 | goto free_skb; |
1da177e4 | 411 | |
c5441932 | 412 | tnl_params = (const struct iphdr *)skb->data; |
1da177e4 | 413 | |
c5441932 PS |
414 | /* Pull skb since ip_tunnel_xmit() needs skb->data pointing |
415 | * to gre header. | |
416 | */ | |
417 | skb_pull(skb, tunnel->hlen + sizeof(struct iphdr)); | |
8a0033a9 | 418 | skb_reset_mac_header(skb); |
c5441932 PS |
419 | } else { |
420 | if (skb_cow_head(skb, dev->needed_headroom)) | |
421 | goto free_skb; | |
1da177e4 | 422 | |
c5441932 | 423 | tnl_params = &tunnel->parms.iph; |
1da177e4 LT |
424 | } |
425 | ||
8a0033a9 TT |
426 | skb = gre_handle_offloads(skb, !!(tunnel->parms.o_flags&TUNNEL_CSUM)); |
427 | if (IS_ERR(skb)) | |
428 | goto out; | |
429 | ||
c5441932 | 430 | __gre_xmit(skb, dev, tnl_params, skb->protocol); |
6ed10654 | 431 | return NETDEV_TX_OK; |
1da177e4 | 432 | |
c5441932 | 433 | free_skb: |
3acfa1e7 | 434 | kfree_skb(skb); |
c5441932 PS |
435 | out: |
436 | dev->stats.tx_dropped++; | |
6ed10654 | 437 | return NETDEV_TX_OK; |
1da177e4 LT |
438 | } |
439 | ||
c5441932 PS |
440 | static netdev_tx_t gre_tap_xmit(struct sk_buff *skb, |
441 | struct net_device *dev) | |
ee34c1eb | 442 | { |
c5441932 | 443 | struct ip_tunnel *tunnel = netdev_priv(dev); |
ee34c1eb | 444 | |
2e15ea39 PS |
445 | if (tunnel->collect_md) { |
446 | gre_fb_xmit(skb, dev); | |
447 | return NETDEV_TX_OK; | |
448 | } | |
449 | ||
45f2e997 | 450 | skb = gre_handle_offloads(skb, !!(tunnel->parms.o_flags&TUNNEL_CSUM)); |
c5441932 PS |
451 | if (IS_ERR(skb)) |
452 | goto out; | |
ee34c1eb | 453 | |
c5441932 PS |
454 | if (skb_cow_head(skb, dev->needed_headroom)) |
455 | goto free_skb; | |
42aa9162 | 456 | |
c5441932 | 457 | __gre_xmit(skb, dev, &tunnel->parms.iph, htons(ETH_P_TEB)); |
c5441932 | 458 | return NETDEV_TX_OK; |
ee34c1eb | 459 | |
c5441932 | 460 | free_skb: |
3acfa1e7 | 461 | kfree_skb(skb); |
c5441932 PS |
462 | out: |
463 | dev->stats.tx_dropped++; | |
464 | return NETDEV_TX_OK; | |
ee34c1eb MS |
465 | } |
466 | ||
c5441932 PS |
467 | static int ipgre_tunnel_ioctl(struct net_device *dev, |
468 | struct ifreq *ifr, int cmd) | |
1da177e4 | 469 | { |
4565e991 | 470 | int err; |
1da177e4 | 471 | struct ip_tunnel_parm p; |
1da177e4 | 472 | |
c5441932 PS |
473 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) |
474 | return -EFAULT; | |
6c734fb8 CW |
475 | if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) { |
476 | if (p.iph.version != 4 || p.iph.protocol != IPPROTO_GRE || | |
477 | p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)) || | |
478 | ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))) | |
479 | return -EINVAL; | |
1da177e4 | 480 | } |
c5441932 PS |
481 | p.i_flags = gre_flags_to_tnl_flags(p.i_flags); |
482 | p.o_flags = gre_flags_to_tnl_flags(p.o_flags); | |
1da177e4 | 483 | |
c5441932 PS |
484 | err = ip_tunnel_ioctl(dev, &p, cmd); |
485 | if (err) | |
486 | return err; | |
1da177e4 | 487 | |
c5441932 PS |
488 | p.i_flags = tnl_flags_to_gre_flags(p.i_flags); |
489 | p.o_flags = tnl_flags_to_gre_flags(p.o_flags); | |
490 | ||
491 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) | |
492 | return -EFAULT; | |
1da177e4 LT |
493 | return 0; |
494 | } | |
495 | ||
1da177e4 LT |
496 | /* Nice toy. Unfortunately, useless in real life :-) |
497 | It allows to construct virtual multiprotocol broadcast "LAN" | |
498 | over the Internet, provided multicast routing is tuned. | |
499 | ||
500 | ||
501 | I have no idea was this bicycle invented before me, | |
502 | so that I had to set ARPHRD_IPGRE to a random value. | |
503 | I have an impression, that Cisco could make something similar, | |
504 | but this feature is apparently missing in IOS<=11.2(8). | |
e905a9ed | 505 | |
1da177e4 LT |
506 | I set up 10.66.66/24 and fec0:6666:6666::0/96 as virtual networks |
507 | with broadcast 224.66.66.66. If you have access to mbone, play with me :-) | |
508 | ||
509 | ping -t 255 224.66.66.66 | |
510 | ||
511 | If nobody answers, mbone does not work. | |
512 | ||
513 | ip tunnel add Universe mode gre remote 224.66.66.66 local <Your_real_addr> ttl 255 | |
514 | ip addr add 10.66.66.<somewhat>/24 dev Universe | |
515 | ifconfig Universe up | |
516 | ifconfig Universe add fe80::<Your_real_addr>/10 | |
517 | ifconfig Universe add fec0:6666:6666::<Your_real_addr>/96 | |
518 | ftp 10.66.66.66 | |
519 | ... | |
520 | ftp fec0:6666:6666::193.233.7.65 | |
521 | ... | |
1da177e4 | 522 | */ |
3b04ddde SH |
523 | static int ipgre_header(struct sk_buff *skb, struct net_device *dev, |
524 | unsigned short type, | |
1507850b | 525 | const void *daddr, const void *saddr, unsigned int len) |
1da177e4 | 526 | { |
2941a486 | 527 | struct ip_tunnel *t = netdev_priv(dev); |
c5441932 PS |
528 | struct iphdr *iph; |
529 | struct gre_base_hdr *greh; | |
1da177e4 | 530 | |
c5441932 PS |
531 | iph = (struct iphdr *)skb_push(skb, t->hlen + sizeof(*iph)); |
532 | greh = (struct gre_base_hdr *)(iph+1); | |
533 | greh->flags = tnl_flags_to_gre_flags(t->parms.o_flags); | |
534 | greh->protocol = htons(type); | |
1da177e4 | 535 | |
c5441932 | 536 | memcpy(iph, &t->parms.iph, sizeof(struct iphdr)); |
e905a9ed | 537 | |
c5441932 | 538 | /* Set the source hardware address. */ |
1da177e4 LT |
539 | if (saddr) |
540 | memcpy(&iph->saddr, saddr, 4); | |
6d55cb91 | 541 | if (daddr) |
1da177e4 | 542 | memcpy(&iph->daddr, daddr, 4); |
6d55cb91 | 543 | if (iph->daddr) |
77a482bd | 544 | return t->hlen + sizeof(*iph); |
e905a9ed | 545 | |
c5441932 | 546 | return -(t->hlen + sizeof(*iph)); |
1da177e4 LT |
547 | } |
548 | ||
6a5f44d7 TT |
549 | static int ipgre_header_parse(const struct sk_buff *skb, unsigned char *haddr) |
550 | { | |
b71d1d42 | 551 | const struct iphdr *iph = (const struct iphdr *) skb_mac_header(skb); |
6a5f44d7 TT |
552 | memcpy(haddr, &iph->saddr, 4); |
553 | return 4; | |
554 | } | |
555 | ||
3b04ddde SH |
556 | static const struct header_ops ipgre_header_ops = { |
557 | .create = ipgre_header, | |
6a5f44d7 | 558 | .parse = ipgre_header_parse, |
3b04ddde SH |
559 | }; |
560 | ||
6a5f44d7 | 561 | #ifdef CONFIG_NET_IPGRE_BROADCAST |
1da177e4 LT |
562 | static int ipgre_open(struct net_device *dev) |
563 | { | |
2941a486 | 564 | struct ip_tunnel *t = netdev_priv(dev); |
1da177e4 | 565 | |
f97c1e0c | 566 | if (ipv4_is_multicast(t->parms.iph.daddr)) { |
cbb1e85f DM |
567 | struct flowi4 fl4; |
568 | struct rtable *rt; | |
569 | ||
b57708ad | 570 | rt = ip_route_output_gre(t->net, &fl4, |
cbb1e85f DM |
571 | t->parms.iph.daddr, |
572 | t->parms.iph.saddr, | |
573 | t->parms.o_key, | |
574 | RT_TOS(t->parms.iph.tos), | |
575 | t->parms.link); | |
b23dd4fe | 576 | if (IS_ERR(rt)) |
1da177e4 | 577 | return -EADDRNOTAVAIL; |
d8d1f30b | 578 | dev = rt->dst.dev; |
1da177e4 | 579 | ip_rt_put(rt); |
51456b29 | 580 | if (!__in_dev_get_rtnl(dev)) |
1da177e4 LT |
581 | return -EADDRNOTAVAIL; |
582 | t->mlink = dev->ifindex; | |
e5ed6399 | 583 | ip_mc_inc_group(__in_dev_get_rtnl(dev), t->parms.iph.daddr); |
1da177e4 LT |
584 | } |
585 | return 0; | |
586 | } | |
587 | ||
588 | static int ipgre_close(struct net_device *dev) | |
589 | { | |
2941a486 | 590 | struct ip_tunnel *t = netdev_priv(dev); |
b8c26a33 | 591 | |
f97c1e0c | 592 | if (ipv4_is_multicast(t->parms.iph.daddr) && t->mlink) { |
7fee0ca2 | 593 | struct in_device *in_dev; |
b57708ad | 594 | in_dev = inetdev_by_index(t->net, t->mlink); |
8723e1b4 | 595 | if (in_dev) |
1da177e4 | 596 | ip_mc_dec_group(in_dev, t->parms.iph.daddr); |
1da177e4 LT |
597 | } |
598 | return 0; | |
599 | } | |
1da177e4 LT |
600 | #endif |
601 | ||
b8c26a33 SH |
602 | static const struct net_device_ops ipgre_netdev_ops = { |
603 | .ndo_init = ipgre_tunnel_init, | |
c5441932 | 604 | .ndo_uninit = ip_tunnel_uninit, |
b8c26a33 SH |
605 | #ifdef CONFIG_NET_IPGRE_BROADCAST |
606 | .ndo_open = ipgre_open, | |
607 | .ndo_stop = ipgre_close, | |
608 | #endif | |
c5441932 | 609 | .ndo_start_xmit = ipgre_xmit, |
b8c26a33 | 610 | .ndo_do_ioctl = ipgre_tunnel_ioctl, |
c5441932 PS |
611 | .ndo_change_mtu = ip_tunnel_change_mtu, |
612 | .ndo_get_stats64 = ip_tunnel_get_stats64, | |
1e99584b | 613 | .ndo_get_iflink = ip_tunnel_get_iflink, |
b8c26a33 SH |
614 | }; |
615 | ||
6b78f16e ED |
616 | #define GRE_FEATURES (NETIF_F_SG | \ |
617 | NETIF_F_FRAGLIST | \ | |
618 | NETIF_F_HIGHDMA | \ | |
619 | NETIF_F_HW_CSUM) | |
620 | ||
1da177e4 LT |
621 | static void ipgre_tunnel_setup(struct net_device *dev) |
622 | { | |
b8c26a33 | 623 | dev->netdev_ops = &ipgre_netdev_ops; |
5a455275 | 624 | dev->type = ARPHRD_IPGRE; |
c5441932 PS |
625 | ip_tunnel_setup(dev, ipgre_net_id); |
626 | } | |
1da177e4 | 627 | |
c5441932 PS |
628 | static void __gre_tunnel_init(struct net_device *dev) |
629 | { | |
630 | struct ip_tunnel *tunnel; | |
4565e991 | 631 | int t_hlen; |
c5441932 PS |
632 | |
633 | tunnel = netdev_priv(dev); | |
4565e991 | 634 | tunnel->tun_hlen = ip_gre_calc_hlen(tunnel->parms.o_flags); |
c5441932 PS |
635 | tunnel->parms.iph.protocol = IPPROTO_GRE; |
636 | ||
4565e991 TH |
637 | tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen; |
638 | ||
639 | t_hlen = tunnel->hlen + sizeof(struct iphdr); | |
640 | ||
641 | dev->needed_headroom = LL_MAX_HEADER + t_hlen + 4; | |
642 | dev->mtu = ETH_DATA_LEN - t_hlen - 4; | |
6b78f16e | 643 | |
b57708ad | 644 | dev->features |= GRE_FEATURES; |
6b78f16e | 645 | dev->hw_features |= GRE_FEATURES; |
c5441932 PS |
646 | |
647 | if (!(tunnel->parms.o_flags & TUNNEL_SEQ)) { | |
648 | /* TCP offload with GRE SEQ is not supported. */ | |
649 | dev->features |= NETIF_F_GSO_SOFTWARE; | |
650 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; | |
651 | /* Can use a lockless transmit, unless we generate | |
652 | * output sequences | |
653 | */ | |
654 | dev->features |= NETIF_F_LLTX; | |
655 | } | |
1da177e4 LT |
656 | } |
657 | ||
658 | static int ipgre_tunnel_init(struct net_device *dev) | |
659 | { | |
c5441932 PS |
660 | struct ip_tunnel *tunnel = netdev_priv(dev); |
661 | struct iphdr *iph = &tunnel->parms.iph; | |
1da177e4 | 662 | |
c5441932 | 663 | __gre_tunnel_init(dev); |
1da177e4 | 664 | |
c5441932 PS |
665 | memcpy(dev->dev_addr, &iph->saddr, 4); |
666 | memcpy(dev->broadcast, &iph->daddr, 4); | |
1da177e4 | 667 | |
c5441932 | 668 | dev->flags = IFF_NOARP; |
02875878 | 669 | netif_keep_dst(dev); |
c5441932 | 670 | dev->addr_len = 4; |
1da177e4 | 671 | |
1da177e4 | 672 | if (iph->daddr) { |
1da177e4 | 673 | #ifdef CONFIG_NET_IPGRE_BROADCAST |
f97c1e0c | 674 | if (ipv4_is_multicast(iph->daddr)) { |
1da177e4 LT |
675 | if (!iph->saddr) |
676 | return -EINVAL; | |
677 | dev->flags = IFF_BROADCAST; | |
3b04ddde | 678 | dev->header_ops = &ipgre_header_ops; |
1da177e4 LT |
679 | } |
680 | #endif | |
ee34c1eb | 681 | } else |
6a5f44d7 | 682 | dev->header_ops = &ipgre_header_ops; |
1da177e4 | 683 | |
c5441932 | 684 | return ip_tunnel_init(dev); |
1da177e4 LT |
685 | } |
686 | ||
bda7bb46 PS |
687 | static struct gre_cisco_protocol ipgre_protocol = { |
688 | .handler = ipgre_rcv, | |
689 | .err_handler = ipgre_err, | |
690 | .priority = 0, | |
1da177e4 LT |
691 | }; |
692 | ||
2c8c1e72 | 693 | static int __net_init ipgre_init_net(struct net *net) |
59a4c759 | 694 | { |
c5441932 | 695 | return ip_tunnel_init_net(net, ipgre_net_id, &ipgre_link_ops, NULL); |
59a4c759 PE |
696 | } |
697 | ||
2c8c1e72 | 698 | static void __net_exit ipgre_exit_net(struct net *net) |
59a4c759 | 699 | { |
c5441932 | 700 | struct ip_tunnel_net *itn = net_generic(net, ipgre_net_id); |
6c742e71 | 701 | ip_tunnel_delete_net(itn, &ipgre_link_ops); |
59a4c759 PE |
702 | } |
703 | ||
704 | static struct pernet_operations ipgre_net_ops = { | |
705 | .init = ipgre_init_net, | |
706 | .exit = ipgre_exit_net, | |
cfb8fbf2 | 707 | .id = &ipgre_net_id, |
c5441932 | 708 | .size = sizeof(struct ip_tunnel_net), |
59a4c759 | 709 | }; |
1da177e4 | 710 | |
c19e654d HX |
711 | static int ipgre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[]) |
712 | { | |
713 | __be16 flags; | |
714 | ||
715 | if (!data) | |
716 | return 0; | |
717 | ||
718 | flags = 0; | |
719 | if (data[IFLA_GRE_IFLAGS]) | |
720 | flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]); | |
721 | if (data[IFLA_GRE_OFLAGS]) | |
722 | flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]); | |
723 | if (flags & (GRE_VERSION|GRE_ROUTING)) | |
724 | return -EINVAL; | |
725 | ||
726 | return 0; | |
727 | } | |
728 | ||
e1a80002 HX |
729 | static int ipgre_tap_validate(struct nlattr *tb[], struct nlattr *data[]) |
730 | { | |
731 | __be32 daddr; | |
732 | ||
733 | if (tb[IFLA_ADDRESS]) { | |
734 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) | |
735 | return -EINVAL; | |
736 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) | |
737 | return -EADDRNOTAVAIL; | |
738 | } | |
739 | ||
740 | if (!data) | |
741 | goto out; | |
742 | ||
743 | if (data[IFLA_GRE_REMOTE]) { | |
744 | memcpy(&daddr, nla_data(data[IFLA_GRE_REMOTE]), 4); | |
745 | if (!daddr) | |
746 | return -EINVAL; | |
747 | } | |
748 | ||
749 | out: | |
750 | return ipgre_tunnel_validate(tb, data); | |
751 | } | |
752 | ||
2e15ea39 PS |
753 | static void ipgre_netlink_parms(struct net_device *dev, |
754 | struct nlattr *data[], | |
755 | struct nlattr *tb[], | |
756 | struct ip_tunnel_parm *parms) | |
c19e654d | 757 | { |
7bb82d92 | 758 | memset(parms, 0, sizeof(*parms)); |
c19e654d HX |
759 | |
760 | parms->iph.protocol = IPPROTO_GRE; | |
761 | ||
762 | if (!data) | |
763 | return; | |
764 | ||
765 | if (data[IFLA_GRE_LINK]) | |
766 | parms->link = nla_get_u32(data[IFLA_GRE_LINK]); | |
767 | ||
768 | if (data[IFLA_GRE_IFLAGS]) | |
c5441932 | 769 | parms->i_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_IFLAGS])); |
c19e654d HX |
770 | |
771 | if (data[IFLA_GRE_OFLAGS]) | |
c5441932 | 772 | parms->o_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_OFLAGS])); |
c19e654d HX |
773 | |
774 | if (data[IFLA_GRE_IKEY]) | |
775 | parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]); | |
776 | ||
777 | if (data[IFLA_GRE_OKEY]) | |
778 | parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]); | |
779 | ||
780 | if (data[IFLA_GRE_LOCAL]) | |
67b61f6c | 781 | parms->iph.saddr = nla_get_in_addr(data[IFLA_GRE_LOCAL]); |
c19e654d HX |
782 | |
783 | if (data[IFLA_GRE_REMOTE]) | |
67b61f6c | 784 | parms->iph.daddr = nla_get_in_addr(data[IFLA_GRE_REMOTE]); |
c19e654d HX |
785 | |
786 | if (data[IFLA_GRE_TTL]) | |
787 | parms->iph.ttl = nla_get_u8(data[IFLA_GRE_TTL]); | |
788 | ||
789 | if (data[IFLA_GRE_TOS]) | |
790 | parms->iph.tos = nla_get_u8(data[IFLA_GRE_TOS]); | |
791 | ||
792 | if (!data[IFLA_GRE_PMTUDISC] || nla_get_u8(data[IFLA_GRE_PMTUDISC])) | |
793 | parms->iph.frag_off = htons(IP_DF); | |
2e15ea39 PS |
794 | |
795 | if (data[IFLA_GRE_COLLECT_METADATA]) { | |
796 | struct ip_tunnel *t = netdev_priv(dev); | |
797 | ||
798 | t->collect_md = true; | |
799 | } | |
c19e654d HX |
800 | } |
801 | ||
4565e991 TH |
802 | /* This function returns true when ENCAP attributes are present in the nl msg */ |
803 | static bool ipgre_netlink_encap_parms(struct nlattr *data[], | |
804 | struct ip_tunnel_encap *ipencap) | |
805 | { | |
806 | bool ret = false; | |
807 | ||
808 | memset(ipencap, 0, sizeof(*ipencap)); | |
809 | ||
810 | if (!data) | |
811 | return ret; | |
812 | ||
813 | if (data[IFLA_GRE_ENCAP_TYPE]) { | |
814 | ret = true; | |
815 | ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]); | |
816 | } | |
817 | ||
818 | if (data[IFLA_GRE_ENCAP_FLAGS]) { | |
819 | ret = true; | |
820 | ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]); | |
821 | } | |
822 | ||
823 | if (data[IFLA_GRE_ENCAP_SPORT]) { | |
824 | ret = true; | |
3e97fa70 | 825 | ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]); |
4565e991 TH |
826 | } |
827 | ||
828 | if (data[IFLA_GRE_ENCAP_DPORT]) { | |
829 | ret = true; | |
3e97fa70 | 830 | ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]); |
4565e991 TH |
831 | } |
832 | ||
833 | return ret; | |
834 | } | |
835 | ||
c5441932 | 836 | static int gre_tap_init(struct net_device *dev) |
e1a80002 | 837 | { |
c5441932 | 838 | __gre_tunnel_init(dev); |
bec94d43 | 839 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
e1a80002 | 840 | |
c5441932 | 841 | return ip_tunnel_init(dev); |
e1a80002 HX |
842 | } |
843 | ||
c5441932 PS |
844 | static const struct net_device_ops gre_tap_netdev_ops = { |
845 | .ndo_init = gre_tap_init, | |
846 | .ndo_uninit = ip_tunnel_uninit, | |
847 | .ndo_start_xmit = gre_tap_xmit, | |
b8c26a33 SH |
848 | .ndo_set_mac_address = eth_mac_addr, |
849 | .ndo_validate_addr = eth_validate_addr, | |
c5441932 PS |
850 | .ndo_change_mtu = ip_tunnel_change_mtu, |
851 | .ndo_get_stats64 = ip_tunnel_get_stats64, | |
1e99584b | 852 | .ndo_get_iflink = ip_tunnel_get_iflink, |
b8c26a33 SH |
853 | }; |
854 | ||
e1a80002 HX |
855 | static void ipgre_tap_setup(struct net_device *dev) |
856 | { | |
e1a80002 | 857 | ether_setup(dev); |
c5441932 | 858 | dev->netdev_ops = &gre_tap_netdev_ops; |
f8c1b7ce | 859 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
c5441932 | 860 | ip_tunnel_setup(dev, gre_tap_net_id); |
e1a80002 HX |
861 | } |
862 | ||
c5441932 PS |
863 | static int ipgre_newlink(struct net *src_net, struct net_device *dev, |
864 | struct nlattr *tb[], struct nlattr *data[]) | |
c19e654d | 865 | { |
c5441932 | 866 | struct ip_tunnel_parm p; |
4565e991 TH |
867 | struct ip_tunnel_encap ipencap; |
868 | ||
869 | if (ipgre_netlink_encap_parms(data, &ipencap)) { | |
870 | struct ip_tunnel *t = netdev_priv(dev); | |
871 | int err = ip_tunnel_encap_setup(t, &ipencap); | |
872 | ||
873 | if (err < 0) | |
874 | return err; | |
875 | } | |
c19e654d | 876 | |
2e15ea39 | 877 | ipgre_netlink_parms(dev, data, tb, &p); |
c5441932 | 878 | return ip_tunnel_newlink(dev, tb, &p); |
c19e654d HX |
879 | } |
880 | ||
881 | static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[], | |
882 | struct nlattr *data[]) | |
883 | { | |
c19e654d | 884 | struct ip_tunnel_parm p; |
4565e991 TH |
885 | struct ip_tunnel_encap ipencap; |
886 | ||
887 | if (ipgre_netlink_encap_parms(data, &ipencap)) { | |
888 | struct ip_tunnel *t = netdev_priv(dev); | |
889 | int err = ip_tunnel_encap_setup(t, &ipencap); | |
890 | ||
891 | if (err < 0) | |
892 | return err; | |
893 | } | |
c19e654d | 894 | |
2e15ea39 | 895 | ipgre_netlink_parms(dev, data, tb, &p); |
c5441932 | 896 | return ip_tunnel_changelink(dev, tb, &p); |
c19e654d HX |
897 | } |
898 | ||
899 | static size_t ipgre_get_size(const struct net_device *dev) | |
900 | { | |
901 | return | |
902 | /* IFLA_GRE_LINK */ | |
903 | nla_total_size(4) + | |
904 | /* IFLA_GRE_IFLAGS */ | |
905 | nla_total_size(2) + | |
906 | /* IFLA_GRE_OFLAGS */ | |
907 | nla_total_size(2) + | |
908 | /* IFLA_GRE_IKEY */ | |
909 | nla_total_size(4) + | |
910 | /* IFLA_GRE_OKEY */ | |
911 | nla_total_size(4) + | |
912 | /* IFLA_GRE_LOCAL */ | |
913 | nla_total_size(4) + | |
914 | /* IFLA_GRE_REMOTE */ | |
915 | nla_total_size(4) + | |
916 | /* IFLA_GRE_TTL */ | |
917 | nla_total_size(1) + | |
918 | /* IFLA_GRE_TOS */ | |
919 | nla_total_size(1) + | |
920 | /* IFLA_GRE_PMTUDISC */ | |
921 | nla_total_size(1) + | |
4565e991 TH |
922 | /* IFLA_GRE_ENCAP_TYPE */ |
923 | nla_total_size(2) + | |
924 | /* IFLA_GRE_ENCAP_FLAGS */ | |
925 | nla_total_size(2) + | |
926 | /* IFLA_GRE_ENCAP_SPORT */ | |
927 | nla_total_size(2) + | |
928 | /* IFLA_GRE_ENCAP_DPORT */ | |
929 | nla_total_size(2) + | |
2e15ea39 PS |
930 | /* IFLA_GRE_COLLECT_METADATA */ |
931 | nla_total_size(0) + | |
c19e654d HX |
932 | 0; |
933 | } | |
934 | ||
935 | static int ipgre_fill_info(struct sk_buff *skb, const struct net_device *dev) | |
936 | { | |
937 | struct ip_tunnel *t = netdev_priv(dev); | |
938 | struct ip_tunnel_parm *p = &t->parms; | |
939 | ||
f3756b79 | 940 | if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) || |
c5441932 PS |
941 | nla_put_be16(skb, IFLA_GRE_IFLAGS, tnl_flags_to_gre_flags(p->i_flags)) || |
942 | nla_put_be16(skb, IFLA_GRE_OFLAGS, tnl_flags_to_gre_flags(p->o_flags)) || | |
f3756b79 DM |
943 | nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) || |
944 | nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) || | |
930345ea JB |
945 | nla_put_in_addr(skb, IFLA_GRE_LOCAL, p->iph.saddr) || |
946 | nla_put_in_addr(skb, IFLA_GRE_REMOTE, p->iph.daddr) || | |
f3756b79 DM |
947 | nla_put_u8(skb, IFLA_GRE_TTL, p->iph.ttl) || |
948 | nla_put_u8(skb, IFLA_GRE_TOS, p->iph.tos) || | |
949 | nla_put_u8(skb, IFLA_GRE_PMTUDISC, | |
950 | !!(p->iph.frag_off & htons(IP_DF)))) | |
951 | goto nla_put_failure; | |
4565e991 TH |
952 | |
953 | if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE, | |
954 | t->encap.type) || | |
3e97fa70 SD |
955 | nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT, |
956 | t->encap.sport) || | |
957 | nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT, | |
958 | t->encap.dport) || | |
4565e991 | 959 | nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS, |
e1b2cb65 | 960 | t->encap.flags)) |
4565e991 TH |
961 | goto nla_put_failure; |
962 | ||
2e15ea39 PS |
963 | if (t->collect_md) { |
964 | if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA)) | |
965 | goto nla_put_failure; | |
966 | } | |
967 | ||
c19e654d HX |
968 | return 0; |
969 | ||
970 | nla_put_failure: | |
971 | return -EMSGSIZE; | |
972 | } | |
973 | ||
974 | static const struct nla_policy ipgre_policy[IFLA_GRE_MAX + 1] = { | |
975 | [IFLA_GRE_LINK] = { .type = NLA_U32 }, | |
976 | [IFLA_GRE_IFLAGS] = { .type = NLA_U16 }, | |
977 | [IFLA_GRE_OFLAGS] = { .type = NLA_U16 }, | |
978 | [IFLA_GRE_IKEY] = { .type = NLA_U32 }, | |
979 | [IFLA_GRE_OKEY] = { .type = NLA_U32 }, | |
4d74f8ba PM |
980 | [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) }, |
981 | [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) }, | |
c19e654d HX |
982 | [IFLA_GRE_TTL] = { .type = NLA_U8 }, |
983 | [IFLA_GRE_TOS] = { .type = NLA_U8 }, | |
984 | [IFLA_GRE_PMTUDISC] = { .type = NLA_U8 }, | |
4565e991 TH |
985 | [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 }, |
986 | [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 }, | |
987 | [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 }, | |
988 | [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 }, | |
2e15ea39 | 989 | [IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG }, |
c19e654d HX |
990 | }; |
991 | ||
992 | static struct rtnl_link_ops ipgre_link_ops __read_mostly = { | |
993 | .kind = "gre", | |
994 | .maxtype = IFLA_GRE_MAX, | |
995 | .policy = ipgre_policy, | |
996 | .priv_size = sizeof(struct ip_tunnel), | |
997 | .setup = ipgre_tunnel_setup, | |
998 | .validate = ipgre_tunnel_validate, | |
999 | .newlink = ipgre_newlink, | |
1000 | .changelink = ipgre_changelink, | |
c5441932 | 1001 | .dellink = ip_tunnel_dellink, |
c19e654d HX |
1002 | .get_size = ipgre_get_size, |
1003 | .fill_info = ipgre_fill_info, | |
1728d4fa | 1004 | .get_link_net = ip_tunnel_get_link_net, |
c19e654d HX |
1005 | }; |
1006 | ||
e1a80002 HX |
1007 | static struct rtnl_link_ops ipgre_tap_ops __read_mostly = { |
1008 | .kind = "gretap", | |
1009 | .maxtype = IFLA_GRE_MAX, | |
1010 | .policy = ipgre_policy, | |
1011 | .priv_size = sizeof(struct ip_tunnel), | |
1012 | .setup = ipgre_tap_setup, | |
1013 | .validate = ipgre_tap_validate, | |
1014 | .newlink = ipgre_newlink, | |
1015 | .changelink = ipgre_changelink, | |
c5441932 | 1016 | .dellink = ip_tunnel_dellink, |
e1a80002 HX |
1017 | .get_size = ipgre_get_size, |
1018 | .fill_info = ipgre_fill_info, | |
1728d4fa | 1019 | .get_link_net = ip_tunnel_get_link_net, |
e1a80002 HX |
1020 | }; |
1021 | ||
b2acd1dc PS |
1022 | struct net_device *gretap_fb_dev_create(struct net *net, const char *name, |
1023 | u8 name_assign_type) | |
1024 | { | |
1025 | struct nlattr *tb[IFLA_MAX + 1]; | |
1026 | struct net_device *dev; | |
1027 | struct ip_tunnel *t; | |
1028 | int err; | |
1029 | ||
1030 | memset(&tb, 0, sizeof(tb)); | |
1031 | ||
1032 | dev = rtnl_create_link(net, name, name_assign_type, | |
1033 | &ipgre_tap_ops, tb); | |
1034 | if (IS_ERR(dev)) | |
1035 | return dev; | |
1036 | ||
1037 | /* Configure flow based GRE device. */ | |
1038 | t = netdev_priv(dev); | |
1039 | t->collect_md = true; | |
1040 | ||
1041 | err = ipgre_newlink(net, dev, tb, NULL); | |
1042 | if (err < 0) | |
1043 | goto out; | |
1044 | return dev; | |
1045 | out: | |
1046 | free_netdev(dev); | |
1047 | return ERR_PTR(err); | |
1048 | } | |
1049 | EXPORT_SYMBOL_GPL(gretap_fb_dev_create); | |
1050 | ||
c5441932 PS |
1051 | static int __net_init ipgre_tap_init_net(struct net *net) |
1052 | { | |
2e15ea39 | 1053 | return ip_tunnel_init_net(net, gre_tap_net_id, &ipgre_tap_ops, "gretap0"); |
c5441932 PS |
1054 | } |
1055 | ||
1056 | static void __net_exit ipgre_tap_exit_net(struct net *net) | |
1057 | { | |
1058 | struct ip_tunnel_net *itn = net_generic(net, gre_tap_net_id); | |
6c742e71 | 1059 | ip_tunnel_delete_net(itn, &ipgre_tap_ops); |
c5441932 PS |
1060 | } |
1061 | ||
1062 | static struct pernet_operations ipgre_tap_net_ops = { | |
1063 | .init = ipgre_tap_init_net, | |
1064 | .exit = ipgre_tap_exit_net, | |
1065 | .id = &gre_tap_net_id, | |
1066 | .size = sizeof(struct ip_tunnel_net), | |
1067 | }; | |
1da177e4 LT |
1068 | |
1069 | static int __init ipgre_init(void) | |
1070 | { | |
1071 | int err; | |
1072 | ||
058bd4d2 | 1073 | pr_info("GRE over IPv4 tunneling driver\n"); |
1da177e4 | 1074 | |
cfb8fbf2 | 1075 | err = register_pernet_device(&ipgre_net_ops); |
59a4c759 | 1076 | if (err < 0) |
c2892f02 AD |
1077 | return err; |
1078 | ||
c5441932 PS |
1079 | err = register_pernet_device(&ipgre_tap_net_ops); |
1080 | if (err < 0) | |
1081 | goto pnet_tap_faied; | |
1082 | ||
bda7bb46 | 1083 | err = gre_cisco_register(&ipgre_protocol); |
c2892f02 | 1084 | if (err < 0) { |
058bd4d2 | 1085 | pr_info("%s: can't add protocol\n", __func__); |
c2892f02 AD |
1086 | goto add_proto_failed; |
1087 | } | |
7daa0004 | 1088 | |
c19e654d HX |
1089 | err = rtnl_link_register(&ipgre_link_ops); |
1090 | if (err < 0) | |
1091 | goto rtnl_link_failed; | |
1092 | ||
e1a80002 HX |
1093 | err = rtnl_link_register(&ipgre_tap_ops); |
1094 | if (err < 0) | |
1095 | goto tap_ops_failed; | |
1096 | ||
c5441932 | 1097 | return 0; |
c19e654d | 1098 | |
e1a80002 HX |
1099 | tap_ops_failed: |
1100 | rtnl_link_unregister(&ipgre_link_ops); | |
c19e654d | 1101 | rtnl_link_failed: |
bda7bb46 | 1102 | gre_cisco_unregister(&ipgre_protocol); |
c2892f02 | 1103 | add_proto_failed: |
c5441932 PS |
1104 | unregister_pernet_device(&ipgre_tap_net_ops); |
1105 | pnet_tap_faied: | |
c2892f02 | 1106 | unregister_pernet_device(&ipgre_net_ops); |
c5441932 | 1107 | return err; |
1da177e4 LT |
1108 | } |
1109 | ||
db44575f | 1110 | static void __exit ipgre_fini(void) |
1da177e4 | 1111 | { |
e1a80002 | 1112 | rtnl_link_unregister(&ipgre_tap_ops); |
c19e654d | 1113 | rtnl_link_unregister(&ipgre_link_ops); |
bda7bb46 | 1114 | gre_cisco_unregister(&ipgre_protocol); |
c5441932 | 1115 | unregister_pernet_device(&ipgre_tap_net_ops); |
c2892f02 | 1116 | unregister_pernet_device(&ipgre_net_ops); |
1da177e4 LT |
1117 | } |
1118 | ||
1119 | module_init(ipgre_init); | |
1120 | module_exit(ipgre_fini); | |
1121 | MODULE_LICENSE("GPL"); | |
4d74f8ba PM |
1122 | MODULE_ALIAS_RTNL_LINK("gre"); |
1123 | MODULE_ALIAS_RTNL_LINK("gretap"); | |
8909c9ad | 1124 | MODULE_ALIAS_NETDEV("gre0"); |
c5441932 | 1125 | MODULE_ALIAS_NETDEV("gretap0"); |