ip_gre.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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/init.h>
  27. #include <linux/in6.h>
  28. #include <linux/inetdevice.h>
  29. #include <linux/igmp.h>
  30. #include <linux/netfilter_ipv4.h>
  31. #include <linux/etherdevice.h>
  32. #include <linux/if_ether.h>
  33. #include <net/sock.h>
  34. #include <net/ip.h>
  35. #include <net/icmp.h>
  36. #include <net/protocol.h>
  37. #include <net/ip_tunnels.h>
  38. #include <net/arp.h>
  39. #include <net/checksum.h>
  40. #include <net/dsfield.h>
  41. #include <net/inet_ecn.h>
  42. #include <net/xfrm.h>
  43. #include <net/net_namespace.h>
  44. #include <net/netns/generic.h>
  45. #include <net/rtnetlink.h>
  46. #include <net/gre.h>
  47. #if IS_ENABLED(CONFIG_IPV6)
  48. #include <net/ipv6.h>
  49. #include <net/ip6_fib.h>
  50. #include <net/ip6_route.h>
  51. #endif
  52. /*
  53. Problems & solutions
  54. --------------------
  55. 1. The most important issue is detecting local dead loops.
  56. They would cause complete host lockup in transmit, which
  57. would be "resolved" by stack overflow or, if queueing is enabled,
  58. with infinite looping in net_bh.
  59. We cannot track such dead loops during route installation,
  60. it is infeasible task. The most general solutions would be
  61. to keep skb->encapsulation counter (sort of local ttl),
  62. and silently drop packet when it expires. It is a good
  63. solution, but it supposes maintaining new variable in ALL
  64. skb, even if no tunneling is used.
  65. Current solution: xmit_recursion breaks dead loops. This is a percpu
  66. counter, since when we enter the first ndo_xmit(), cpu migration is
  67. forbidden. We force an exit if this counter reaches RECURSION_LIMIT
  68. 2. Networking dead loops would not kill routers, but would really
  69. kill network. IP hop limit plays role of "t->recursion" in this case,
  70. if we copy it from packet being encapsulated to upper header.
  71. It is very good solution, but it introduces two problems:
  72. - Routing protocols, using packets with ttl=1 (OSPF, RIP2),
  73. do not work over tunnels.
  74. - traceroute does not work. I planned to relay ICMP from tunnel,
  75. so that this problem would be solved and traceroute output
  76. would even more informative. This idea appeared to be wrong:
  77. only Linux complies to rfc1812 now (yes, guys, Linux is the only
  78. true router now :-)), all routers (at least, in neighbourhood of mine)
  79. return only 8 bytes of payload. It is the end.
  80. Hence, if we want that OSPF worked or traceroute said something reasonable,
  81. we should search for another solution.
  82. One of them is to parse packet trying to detect inner encapsulation
  83. made by our node. It is difficult or even impossible, especially,
  84. taking into account fragmentation. TO be short, ttl is not solution at all.
  85. Current solution: The solution was UNEXPECTEDLY SIMPLE.
  86. We force DF flag on tunnels with preconfigured hop limit,
  87. that is ALL. :-) Well, it does not remove the problem completely,
  88. but exponential growth of network traffic is changed to linear
  89. (branches, that exceed pmtu are pruned) and tunnel mtu
  90. rapidly degrades to value <68, where looping stops.
  91. Yes, it is not good if there exists a router in the loop,
  92. which does not force DF, even when encapsulating packets have DF set.
  93. But it is not our problem! Nobody could accuse us, we made
  94. all that we could make. Even if it is your gated who injected
  95. fatal route to network, even if it were you who configured
  96. fatal static route: you are innocent. :-)
  97. Alexey Kuznetsov.
  98. */
  99. static bool log_ecn_error = true;
  100. module_param(log_ecn_error, bool, 0644);
  101. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  102. static struct rtnl_link_ops ipgre_link_ops __read_mostly;
  103. static int ipgre_tunnel_init(struct net_device *dev);
  104. static int ipgre_net_id __read_mostly;
  105. static int gre_tap_net_id __read_mostly;
  106. static int ipgre_err(struct sk_buff *skb, u32 info,
  107. const struct tnl_ptk_info *tpi)
  108. {
  109. /* All the routers (except for Linux) return only
  110. 8 bytes of packet payload. It means, that precise relaying of
  111. ICMP in the real Internet is absolutely infeasible.
  112. Moreover, Cisco "wise men" put GRE key to the third word
  113. in GRE header. It makes impossible maintaining even soft
  114. state for keyed GRE tunnels with enabled checksum. Tell
  115. them "thank you".
  116. Well, I wonder, rfc1812 was written by Cisco employee,
  117. what the hell these idiots break standards established
  118. by themselves???
  119. */
  120. struct net *net = dev_net(skb->dev);
  121. struct ip_tunnel_net *itn;
  122. const struct iphdr *iph;
  123. const int type = icmp_hdr(skb)->type;
  124. const int code = icmp_hdr(skb)->code;
  125. struct ip_tunnel *t;
  126. switch (type) {
  127. default:
  128. case ICMP_PARAMETERPROB:
  129. return PACKET_RCVD;
  130. case ICMP_DEST_UNREACH:
  131. switch (code) {
  132. case ICMP_SR_FAILED:
  133. case ICMP_PORT_UNREACH:
  134. /* Impossible event. */
  135. return PACKET_RCVD;
  136. default:
  137. /* All others are translated to HOST_UNREACH.
  138. rfc2003 contains "deep thoughts" about NET_UNREACH,
  139. I believe they are just ether pollution. --ANK
  140. */
  141. break;
  142. }
  143. break;
  144. case ICMP_TIME_EXCEEDED:
  145. if (code != ICMP_EXC_TTL)
  146. return PACKET_RCVD;
  147. break;
  148. case ICMP_REDIRECT:
  149. break;
  150. }
  151. if (tpi->proto == htons(ETH_P_TEB))
  152. itn = net_generic(net, gre_tap_net_id);
  153. else
  154. itn = net_generic(net, ipgre_net_id);
  155. iph = (const struct iphdr *)skb->data;
  156. t = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags,
  157. iph->daddr, iph->saddr, tpi->key);
  158. if (t == NULL)
  159. return PACKET_REJECT;
  160. if (t->parms.iph.daddr == 0 ||
  161. ipv4_is_multicast(t->parms.iph.daddr))
  162. return PACKET_RCVD;
  163. if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
  164. return PACKET_RCVD;
  165. if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
  166. t->err_count++;
  167. else
  168. t->err_count = 1;
  169. t->err_time = jiffies;
  170. return PACKET_RCVD;
  171. }
  172. static int ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
  173. {
  174. struct net *net = dev_net(skb->dev);
  175. struct ip_tunnel_net *itn;
  176. const struct iphdr *iph;
  177. struct ip_tunnel *tunnel;
  178. if (tpi->proto == htons(ETH_P_TEB))
  179. itn = net_generic(net, gre_tap_net_id);
  180. else
  181. itn = net_generic(net, ipgre_net_id);
  182. iph = ip_hdr(skb);
  183. tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags,
  184. iph->saddr, iph->daddr, tpi->key);
  185. if (tunnel) {
  186. ip_tunnel_rcv(tunnel, skb, tpi, log_ecn_error);
  187. return PACKET_RCVD;
  188. }
  189. return PACKET_REJECT;
  190. }
  191. static void __gre_xmit(struct sk_buff *skb, struct net_device *dev,
  192. const struct iphdr *tnl_params,
  193. __be16 proto)
  194. {
  195. struct ip_tunnel *tunnel = netdev_priv(dev);
  196. struct tnl_ptk_info tpi;
  197. tpi.flags = tunnel->parms.o_flags;
  198. tpi.proto = proto;
  199. tpi.key = tunnel->parms.o_key;
  200. if (tunnel->parms.o_flags & TUNNEL_SEQ)
  201. tunnel->o_seqno++;
  202. tpi.seq = htonl(tunnel->o_seqno);
  203. /* Push GRE header. */
  204. gre_build_header(skb, &tpi, tunnel->hlen);
  205. ip_tunnel_xmit(skb, dev, tnl_params, tnl_params->protocol);
  206. }
  207. static netdev_tx_t ipgre_xmit(struct sk_buff *skb,
  208. struct net_device *dev)
  209. {
  210. struct ip_tunnel *tunnel = netdev_priv(dev);
  211. const struct iphdr *tnl_params;
  212. skb = gre_handle_offloads(skb, !!(tunnel->parms.o_flags&TUNNEL_CSUM));
  213. if (IS_ERR(skb))
  214. goto out;
  215. if (dev->header_ops) {
  216. /* Need space for new headers */
  217. if (skb_cow_head(skb, dev->needed_headroom -
  218. (tunnel->hlen + sizeof(struct iphdr))))
  219. goto free_skb;
  220. tnl_params = (const struct iphdr *)skb->data;
  221. /* Pull skb since ip_tunnel_xmit() needs skb->data pointing
  222. * to gre header.
  223. */
  224. skb_pull(skb, tunnel->hlen + sizeof(struct iphdr));
  225. } else {
  226. if (skb_cow_head(skb, dev->needed_headroom))
  227. goto free_skb;
  228. tnl_params = &tunnel->parms.iph;
  229. }
  230. __gre_xmit(skb, dev, tnl_params, skb->protocol);
  231. return NETDEV_TX_OK;
  232. free_skb:
  233. dev_kfree_skb(skb);
  234. out:
  235. dev->stats.tx_dropped++;
  236. return NETDEV_TX_OK;
  237. }
  238. static netdev_tx_t gre_tap_xmit(struct sk_buff *skb,
  239. struct net_device *dev)
  240. {
  241. struct ip_tunnel *tunnel = netdev_priv(dev);
  242. skb = gre_handle_offloads(skb, !!(tunnel->parms.o_flags&TUNNEL_CSUM));
  243. if (IS_ERR(skb))
  244. goto out;
  245. if (skb_cow_head(skb, dev->needed_headroom))
  246. goto free_skb;
  247. __gre_xmit(skb, dev, &tunnel->parms.iph, htons(ETH_P_TEB));
  248. return NETDEV_TX_OK;
  249. free_skb:
  250. dev_kfree_skb(skb);
  251. out:
  252. dev->stats.tx_dropped++;
  253. return NETDEV_TX_OK;
  254. }
  255. static int ipgre_tunnel_ioctl(struct net_device *dev,
  256. struct ifreq *ifr, int cmd)
  257. {
  258. int err = 0;
  259. struct ip_tunnel_parm p;
  260. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  261. return -EFAULT;
  262. if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
  263. if (p.iph.version != 4 || p.iph.protocol != IPPROTO_GRE ||
  264. p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)) ||
  265. ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING)))
  266. return -EINVAL;
  267. }
  268. p.i_flags = gre_flags_to_tnl_flags(p.i_flags);
  269. p.o_flags = gre_flags_to_tnl_flags(p.o_flags);
  270. err = ip_tunnel_ioctl(dev, &p, cmd);
  271. if (err)
  272. return err;
  273. p.i_flags = tnl_flags_to_gre_flags(p.i_flags);
  274. p.o_flags = tnl_flags_to_gre_flags(p.o_flags);
  275. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  276. return -EFAULT;
  277. return 0;
  278. }
  279. /* Nice toy. Unfortunately, useless in real life :-)
  280. It allows to construct virtual multiprotocol broadcast "LAN"
  281. over the Internet, provided multicast routing is tuned.
  282. I have no idea was this bicycle invented before me,
  283. so that I had to set ARPHRD_IPGRE to a random value.
  284. I have an impression, that Cisco could make something similar,
  285. but this feature is apparently missing in IOS<=11.2(8).
  286. I set up 10.66.66/24 and fec0:6666:6666::0/96 as virtual networks
  287. with broadcast 224.66.66.66. If you have access to mbone, play with me :-)
  288. ping -t 255 224.66.66.66
  289. If nobody answers, mbone does not work.
  290. ip tunnel add Universe mode gre remote 224.66.66.66 local <Your_real_addr> ttl 255
  291. ip addr add 10.66.66.<somewhat>/24 dev Universe
  292. ifconfig Universe up
  293. ifconfig Universe add fe80::<Your_real_addr>/10
  294. ifconfig Universe add fec0:6666:6666::<Your_real_addr>/96
  295. ftp 10.66.66.66
  296. ...
  297. ftp fec0:6666:6666::193.233.7.65
  298. ...
  299. */
  300. static int ipgre_header(struct sk_buff *skb, struct net_device *dev,
  301. unsigned short type,
  302. const void *daddr, const void *saddr, unsigned int len)
  303. {
  304. struct ip_tunnel *t = netdev_priv(dev);
  305. struct iphdr *iph;
  306. struct gre_base_hdr *greh;
  307. iph = (struct iphdr *)skb_push(skb, t->hlen + sizeof(*iph));
  308. greh = (struct gre_base_hdr *)(iph+1);
  309. greh->flags = tnl_flags_to_gre_flags(t->parms.o_flags);
  310. greh->protocol = htons(type);
  311. memcpy(iph, &t->parms.iph, sizeof(struct iphdr));
  312. /* Set the source hardware address. */
  313. if (saddr)
  314. memcpy(&iph->saddr, saddr, 4);
  315. if (daddr)
  316. memcpy(&iph->daddr, daddr, 4);
  317. if (iph->daddr)
  318. return t->hlen;
  319. return -(t->hlen + sizeof(*iph));
  320. }
  321. static int ipgre_header_parse(const struct sk_buff *skb, unsigned char *haddr)
  322. {
  323. const struct iphdr *iph = (const struct iphdr *) skb_mac_header(skb);
  324. memcpy(haddr, &iph->saddr, 4);
  325. return 4;
  326. }
  327. static const struct header_ops ipgre_header_ops = {
  328. .create = ipgre_header,
  329. .parse = ipgre_header_parse,
  330. };
  331. #ifdef CONFIG_NET_IPGRE_BROADCAST
  332. static int ipgre_open(struct net_device *dev)
  333. {
  334. struct ip_tunnel *t = netdev_priv(dev);
  335. if (ipv4_is_multicast(t->parms.iph.daddr)) {
  336. struct flowi4 fl4;
  337. struct rtable *rt;
  338. rt = ip_route_output_gre(dev_net(dev), &fl4,
  339. t->parms.iph.daddr,
  340. t->parms.iph.saddr,
  341. t->parms.o_key,
  342. RT_TOS(t->parms.iph.tos),
  343. t->parms.link);
  344. if (IS_ERR(rt))
  345. return -EADDRNOTAVAIL;
  346. dev = rt->dst.dev;
  347. ip_rt_put(rt);
  348. if (__in_dev_get_rtnl(dev) == NULL)
  349. return -EADDRNOTAVAIL;
  350. t->mlink = dev->ifindex;
  351. ip_mc_inc_group(__in_dev_get_rtnl(dev), t->parms.iph.daddr);
  352. }
  353. return 0;
  354. }
  355. static int ipgre_close(struct net_device *dev)
  356. {
  357. struct ip_tunnel *t = netdev_priv(dev);
  358. if (ipv4_is_multicast(t->parms.iph.daddr) && t->mlink) {
  359. struct in_device *in_dev;
  360. in_dev = inetdev_by_index(dev_net(dev), t->mlink);
  361. if (in_dev)
  362. ip_mc_dec_group(in_dev, t->parms.iph.daddr);
  363. }
  364. return 0;
  365. }
  366. #endif
  367. static const struct net_device_ops ipgre_netdev_ops = {
  368. .ndo_init = ipgre_tunnel_init,
  369. .ndo_uninit = ip_tunnel_uninit,
  370. #ifdef CONFIG_NET_IPGRE_BROADCAST
  371. .ndo_open = ipgre_open,
  372. .ndo_stop = ipgre_close,
  373. #endif
  374. .ndo_start_xmit = ipgre_xmit,
  375. .ndo_do_ioctl = ipgre_tunnel_ioctl,
  376. .ndo_change_mtu = ip_tunnel_change_mtu,
  377. .ndo_get_stats64 = ip_tunnel_get_stats64,
  378. };
  379. #define GRE_FEATURES (NETIF_F_SG | \
  380. NETIF_F_FRAGLIST | \
  381. NETIF_F_HIGHDMA | \
  382. NETIF_F_HW_CSUM)
  383. static void ipgre_tunnel_setup(struct net_device *dev)
  384. {
  385. dev->netdev_ops = &ipgre_netdev_ops;
  386. ip_tunnel_setup(dev, ipgre_net_id);
  387. }
  388. static void __gre_tunnel_init(struct net_device *dev)
  389. {
  390. struct ip_tunnel *tunnel;
  391. tunnel = netdev_priv(dev);
  392. tunnel->hlen = ip_gre_calc_hlen(tunnel->parms.o_flags);
  393. tunnel->parms.iph.protocol = IPPROTO_GRE;
  394. dev->needed_headroom = LL_MAX_HEADER + sizeof(struct iphdr) + 4;
  395. dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 4;
  396. dev->features |= NETIF_F_NETNS_LOCAL | GRE_FEATURES;
  397. dev->hw_features |= GRE_FEATURES;
  398. if (!(tunnel->parms.o_flags & TUNNEL_SEQ)) {
  399. /* TCP offload with GRE SEQ is not supported. */
  400. dev->features |= NETIF_F_GSO_SOFTWARE;
  401. dev->hw_features |= NETIF_F_GSO_SOFTWARE;
  402. /* Can use a lockless transmit, unless we generate
  403. * output sequences
  404. */
  405. dev->features |= NETIF_F_LLTX;
  406. }
  407. }
  408. static int ipgre_tunnel_init(struct net_device *dev)
  409. {
  410. struct ip_tunnel *tunnel = netdev_priv(dev);
  411. struct iphdr *iph = &tunnel->parms.iph;
  412. __gre_tunnel_init(dev);
  413. memcpy(dev->dev_addr, &iph->saddr, 4);
  414. memcpy(dev->broadcast, &iph->daddr, 4);
  415. dev->type = ARPHRD_IPGRE;
  416. dev->flags = IFF_NOARP;
  417. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  418. dev->addr_len = 4;
  419. if (iph->daddr) {
  420. #ifdef CONFIG_NET_IPGRE_BROADCAST
  421. if (ipv4_is_multicast(iph->daddr)) {
  422. if (!iph->saddr)
  423. return -EINVAL;
  424. dev->flags = IFF_BROADCAST;
  425. dev->header_ops = &ipgre_header_ops;
  426. }
  427. #endif
  428. } else
  429. dev->header_ops = &ipgre_header_ops;
  430. return ip_tunnel_init(dev);
  431. }
  432. static struct gre_cisco_protocol ipgre_protocol = {
  433. .handler = ipgre_rcv,
  434. .err_handler = ipgre_err,
  435. .priority = 0,
  436. };
  437. static int __net_init ipgre_init_net(struct net *net)
  438. {
  439. return ip_tunnel_init_net(net, ipgre_net_id, &ipgre_link_ops, NULL);
  440. }
  441. static void __net_exit ipgre_exit_net(struct net *net)
  442. {
  443. struct ip_tunnel_net *itn = net_generic(net, ipgre_net_id);
  444. ip_tunnel_delete_net(itn);
  445. }
  446. static struct pernet_operations ipgre_net_ops = {
  447. .init = ipgre_init_net,
  448. .exit = ipgre_exit_net,
  449. .id = &ipgre_net_id,
  450. .size = sizeof(struct ip_tunnel_net),
  451. };
  452. static int ipgre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
  453. {
  454. __be16 flags;
  455. if (!data)
  456. return 0;
  457. flags = 0;
  458. if (data[IFLA_GRE_IFLAGS])
  459. flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
  460. if (data[IFLA_GRE_OFLAGS])
  461. flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
  462. if (flags & (GRE_VERSION|GRE_ROUTING))
  463. return -EINVAL;
  464. return 0;
  465. }
  466. static int ipgre_tap_validate(struct nlattr *tb[], struct nlattr *data[])
  467. {
  468. __be32 daddr;
  469. if (tb[IFLA_ADDRESS]) {
  470. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  471. return -EINVAL;
  472. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  473. return -EADDRNOTAVAIL;
  474. }
  475. if (!data)
  476. goto out;
  477. if (data[IFLA_GRE_REMOTE]) {
  478. memcpy(&daddr, nla_data(data[IFLA_GRE_REMOTE]), 4);
  479. if (!daddr)
  480. return -EINVAL;
  481. }
  482. out:
  483. return ipgre_tunnel_validate(tb, data);
  484. }
  485. static void ipgre_netlink_parms(struct nlattr *data[], struct nlattr *tb[],
  486. struct ip_tunnel_parm *parms)
  487. {
  488. memset(parms, 0, sizeof(*parms));
  489. parms->iph.protocol = IPPROTO_GRE;
  490. if (!data)
  491. return;
  492. if (data[IFLA_GRE_LINK])
  493. parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
  494. if (data[IFLA_GRE_IFLAGS])
  495. parms->i_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_IFLAGS]));
  496. if (data[IFLA_GRE_OFLAGS])
  497. parms->o_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_OFLAGS]));
  498. if (data[IFLA_GRE_IKEY])
  499. parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
  500. if (data[IFLA_GRE_OKEY])
  501. parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
  502. if (data[IFLA_GRE_LOCAL])
  503. parms->iph.saddr = nla_get_be32(data[IFLA_GRE_LOCAL]);
  504. if (data[IFLA_GRE_REMOTE])
  505. parms->iph.daddr = nla_get_be32(data[IFLA_GRE_REMOTE]);
  506. if (data[IFLA_GRE_TTL])
  507. parms->iph.ttl = nla_get_u8(data[IFLA_GRE_TTL]);
  508. if (data[IFLA_GRE_TOS])
  509. parms->iph.tos = nla_get_u8(data[IFLA_GRE_TOS]);
  510. if (!data[IFLA_GRE_PMTUDISC] || nla_get_u8(data[IFLA_GRE_PMTUDISC]))
  511. parms->iph.frag_off = htons(IP_DF);
  512. }
  513. static int gre_tap_init(struct net_device *dev)
  514. {
  515. __gre_tunnel_init(dev);
  516. return ip_tunnel_init(dev);
  517. }
  518. static const struct net_device_ops gre_tap_netdev_ops = {
  519. .ndo_init = gre_tap_init,
  520. .ndo_uninit = ip_tunnel_uninit,
  521. .ndo_start_xmit = gre_tap_xmit,
  522. .ndo_set_mac_address = eth_mac_addr,
  523. .ndo_validate_addr = eth_validate_addr,
  524. .ndo_change_mtu = ip_tunnel_change_mtu,
  525. .ndo_get_stats64 = ip_tunnel_get_stats64,
  526. };
  527. static void ipgre_tap_setup(struct net_device *dev)
  528. {
  529. ether_setup(dev);
  530. dev->netdev_ops = &gre_tap_netdev_ops;
  531. ip_tunnel_setup(dev, gre_tap_net_id);
  532. }
  533. static int ipgre_newlink(struct net *src_net, struct net_device *dev,
  534. struct nlattr *tb[], struct nlattr *data[])
  535. {
  536. struct ip_tunnel_parm p;
  537. ipgre_netlink_parms(data, tb, &p);
  538. return ip_tunnel_newlink(dev, tb, &p);
  539. }
  540. static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[],
  541. struct nlattr *data[])
  542. {
  543. struct ip_tunnel_parm p;
  544. ipgre_netlink_parms(data, tb, &p);
  545. return ip_tunnel_changelink(dev, tb, &p);
  546. }
  547. static size_t ipgre_get_size(const struct net_device *dev)
  548. {
  549. return
  550. /* IFLA_GRE_LINK */
  551. nla_total_size(4) +
  552. /* IFLA_GRE_IFLAGS */
  553. nla_total_size(2) +
  554. /* IFLA_GRE_OFLAGS */
  555. nla_total_size(2) +
  556. /* IFLA_GRE_IKEY */
  557. nla_total_size(4) +
  558. /* IFLA_GRE_OKEY */
  559. nla_total_size(4) +
  560. /* IFLA_GRE_LOCAL */
  561. nla_total_size(4) +
  562. /* IFLA_GRE_REMOTE */
  563. nla_total_size(4) +
  564. /* IFLA_GRE_TTL */
  565. nla_total_size(1) +
  566. /* IFLA_GRE_TOS */
  567. nla_total_size(1) +
  568. /* IFLA_GRE_PMTUDISC */
  569. nla_total_size(1) +
  570. 0;
  571. }
  572. static int ipgre_fill_info(struct sk_buff *skb, const struct net_device *dev)
  573. {
  574. struct ip_tunnel *t = netdev_priv(dev);
  575. struct ip_tunnel_parm *p = &t->parms;
  576. if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
  577. nla_put_be16(skb, IFLA_GRE_IFLAGS, tnl_flags_to_gre_flags(p->i_flags)) ||
  578. nla_put_be16(skb, IFLA_GRE_OFLAGS, tnl_flags_to_gre_flags(p->o_flags)) ||
  579. nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
  580. nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
  581. nla_put_be32(skb, IFLA_GRE_LOCAL, p->iph.saddr) ||
  582. nla_put_be32(skb, IFLA_GRE_REMOTE, p->iph.daddr) ||
  583. nla_put_u8(skb, IFLA_GRE_TTL, p->iph.ttl) ||
  584. nla_put_u8(skb, IFLA_GRE_TOS, p->iph.tos) ||
  585. nla_put_u8(skb, IFLA_GRE_PMTUDISC,
  586. !!(p->iph.frag_off & htons(IP_DF))))
  587. goto nla_put_failure;
  588. return 0;
  589. nla_put_failure:
  590. return -EMSGSIZE;
  591. }
  592. static const struct nla_policy ipgre_policy[IFLA_GRE_MAX + 1] = {
  593. [IFLA_GRE_LINK] = { .type = NLA_U32 },
  594. [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
  595. [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
  596. [IFLA_GRE_IKEY] = { .type = NLA_U32 },
  597. [IFLA_GRE_OKEY] = { .type = NLA_U32 },
  598. [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
  599. [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
  600. [IFLA_GRE_TTL] = { .type = NLA_U8 },
  601. [IFLA_GRE_TOS] = { .type = NLA_U8 },
  602. [IFLA_GRE_PMTUDISC] = { .type = NLA_U8 },
  603. };
  604. static struct rtnl_link_ops ipgre_link_ops __read_mostly = {
  605. .kind = "gre",
  606. .maxtype = IFLA_GRE_MAX,
  607. .policy = ipgre_policy,
  608. .priv_size = sizeof(struct ip_tunnel),
  609. .setup = ipgre_tunnel_setup,
  610. .validate = ipgre_tunnel_validate,
  611. .newlink = ipgre_newlink,
  612. .changelink = ipgre_changelink,
  613. .dellink = ip_tunnel_dellink,
  614. .get_size = ipgre_get_size,
  615. .fill_info = ipgre_fill_info,
  616. };
  617. static struct rtnl_link_ops ipgre_tap_ops __read_mostly = {
  618. .kind = "gretap",
  619. .maxtype = IFLA_GRE_MAX,
  620. .policy = ipgre_policy,
  621. .priv_size = sizeof(struct ip_tunnel),
  622. .setup = ipgre_tap_setup,
  623. .validate = ipgre_tap_validate,
  624. .newlink = ipgre_newlink,
  625. .changelink = ipgre_changelink,
  626. .dellink = ip_tunnel_dellink,
  627. .get_size = ipgre_get_size,
  628. .fill_info = ipgre_fill_info,
  629. };
  630. static int __net_init ipgre_tap_init_net(struct net *net)
  631. {
  632. return ip_tunnel_init_net(net, gre_tap_net_id, &ipgre_tap_ops, NULL);
  633. }
  634. static void __net_exit ipgre_tap_exit_net(struct net *net)
  635. {
  636. struct ip_tunnel_net *itn = net_generic(net, gre_tap_net_id);
  637. ip_tunnel_delete_net(itn);
  638. }
  639. static struct pernet_operations ipgre_tap_net_ops = {
  640. .init = ipgre_tap_init_net,
  641. .exit = ipgre_tap_exit_net,
  642. .id = &gre_tap_net_id,
  643. .size = sizeof(struct ip_tunnel_net),
  644. };
  645. static int __init ipgre_init(void)
  646. {
  647. int err;
  648. pr_info("GRE over IPv4 tunneling driver\n");
  649. err = register_pernet_device(&ipgre_net_ops);
  650. if (err < 0)
  651. return err;
  652. err = register_pernet_device(&ipgre_tap_net_ops);
  653. if (err < 0)
  654. goto pnet_tap_faied;
  655. err = gre_cisco_register(&ipgre_protocol);
  656. if (err < 0) {
  657. pr_info("%s: can't add protocol\n", __func__);
  658. goto add_proto_failed;
  659. }
  660. err = rtnl_link_register(&ipgre_link_ops);
  661. if (err < 0)
  662. goto rtnl_link_failed;
  663. err = rtnl_link_register(&ipgre_tap_ops);
  664. if (err < 0)
  665. goto tap_ops_failed;
  666. return 0;
  667. tap_ops_failed:
  668. rtnl_link_unregister(&ipgre_link_ops);
  669. rtnl_link_failed:
  670. gre_cisco_unregister(&ipgre_protocol);
  671. add_proto_failed:
  672. unregister_pernet_device(&ipgre_tap_net_ops);
  673. pnet_tap_faied:
  674. unregister_pernet_device(&ipgre_net_ops);
  675. return err;
  676. }
  677. static void __exit ipgre_fini(void)
  678. {
  679. rtnl_link_unregister(&ipgre_tap_ops);
  680. rtnl_link_unregister(&ipgre_link_ops);
  681. gre_cisco_unregister(&ipgre_protocol);
  682. unregister_pernet_device(&ipgre_tap_net_ops);
  683. unregister_pernet_device(&ipgre_net_ops);
  684. }
  685. module_init(ipgre_init);
  686. module_exit(ipgre_fini);
  687. MODULE_LICENSE("GPL");
  688. MODULE_ALIAS_RTNL_LINK("gre");
  689. MODULE_ALIAS_RTNL_LINK("gretap");
  690. MODULE_ALIAS_NETDEV("gre0");
  691. MODULE_ALIAS_NETDEV("gretap0");