ip_gre.c 34 KB

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