ip6_gre.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644
  1. /*
  2. * GRE over IPv6 protocol decoder.
  3. *
  4. * Authors: Dmitry Kozlov (xeb@mail.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 <linux/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/init.h>
  26. #include <linux/in6.h>
  27. #include <linux/inetdevice.h>
  28. #include <linux/igmp.h>
  29. #include <linux/netfilter_ipv4.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/if_ether.h>
  32. #include <linux/hash.h>
  33. #include <linux/if_tunnel.h>
  34. #include <linux/ip6_tunnel.h>
  35. #include <net/sock.h>
  36. #include <net/ip.h>
  37. #include <net/ip_tunnels.h>
  38. #include <net/icmp.h>
  39. #include <net/protocol.h>
  40. #include <net/addrconf.h>
  41. #include <net/arp.h>
  42. #include <net/checksum.h>
  43. #include <net/dsfield.h>
  44. #include <net/inet_ecn.h>
  45. #include <net/xfrm.h>
  46. #include <net/net_namespace.h>
  47. #include <net/netns/generic.h>
  48. #include <net/rtnetlink.h>
  49. #include <net/ipv6.h>
  50. #include <net/ip6_fib.h>
  51. #include <net/ip6_route.h>
  52. #include <net/ip6_tunnel.h>
  53. #include <net/gre.h>
  54. static bool log_ecn_error = true;
  55. module_param(log_ecn_error, bool, 0644);
  56. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  57. #define IP6_GRE_HASH_SIZE_SHIFT 5
  58. #define IP6_GRE_HASH_SIZE (1 << IP6_GRE_HASH_SIZE_SHIFT)
  59. static unsigned int ip6gre_net_id __read_mostly;
  60. struct ip6gre_net {
  61. struct ip6_tnl __rcu *tunnels[4][IP6_GRE_HASH_SIZE];
  62. struct net_device *fb_tunnel_dev;
  63. };
  64. static struct rtnl_link_ops ip6gre_link_ops __read_mostly;
  65. static struct rtnl_link_ops ip6gre_tap_ops __read_mostly;
  66. static int ip6gre_tunnel_init(struct net_device *dev);
  67. static void ip6gre_tunnel_setup(struct net_device *dev);
  68. static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t);
  69. static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu);
  70. /* Tunnel hash table */
  71. /*
  72. 4 hash tables:
  73. 3: (remote,local)
  74. 2: (remote,*)
  75. 1: (*,local)
  76. 0: (*,*)
  77. We require exact key match i.e. if a key is present in packet
  78. it will match only tunnel with the same key; if it is not present,
  79. it will match only keyless tunnel.
  80. All keysless packets, if not matched configured keyless tunnels
  81. will match fallback tunnel.
  82. */
  83. #define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(IP6_GRE_HASH_SIZE - 1))
  84. static u32 HASH_ADDR(const struct in6_addr *addr)
  85. {
  86. u32 hash = ipv6_addr_hash(addr);
  87. return hash_32(hash, IP6_GRE_HASH_SIZE_SHIFT);
  88. }
  89. #define tunnels_r_l tunnels[3]
  90. #define tunnels_r tunnels[2]
  91. #define tunnels_l tunnels[1]
  92. #define tunnels_wc tunnels[0]
  93. /* Given src, dst and key, find appropriate for input tunnel. */
  94. static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev,
  95. const struct in6_addr *remote, const struct in6_addr *local,
  96. __be32 key, __be16 gre_proto)
  97. {
  98. struct net *net = dev_net(dev);
  99. int link = dev->ifindex;
  100. unsigned int h0 = HASH_ADDR(remote);
  101. unsigned int h1 = HASH_KEY(key);
  102. struct ip6_tnl *t, *cand = NULL;
  103. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  104. int dev_type = (gre_proto == htons(ETH_P_TEB)) ?
  105. ARPHRD_ETHER : ARPHRD_IP6GRE;
  106. int score, cand_score = 4;
  107. for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) {
  108. if (!ipv6_addr_equal(local, &t->parms.laddr) ||
  109. !ipv6_addr_equal(remote, &t->parms.raddr) ||
  110. key != t->parms.i_key ||
  111. !(t->dev->flags & IFF_UP))
  112. continue;
  113. if (t->dev->type != ARPHRD_IP6GRE &&
  114. t->dev->type != dev_type)
  115. continue;
  116. score = 0;
  117. if (t->parms.link != link)
  118. score |= 1;
  119. if (t->dev->type != dev_type)
  120. score |= 2;
  121. if (score == 0)
  122. return t;
  123. if (score < cand_score) {
  124. cand = t;
  125. cand_score = score;
  126. }
  127. }
  128. for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) {
  129. if (!ipv6_addr_equal(remote, &t->parms.raddr) ||
  130. key != t->parms.i_key ||
  131. !(t->dev->flags & IFF_UP))
  132. continue;
  133. if (t->dev->type != ARPHRD_IP6GRE &&
  134. t->dev->type != dev_type)
  135. continue;
  136. score = 0;
  137. if (t->parms.link != link)
  138. score |= 1;
  139. if (t->dev->type != dev_type)
  140. score |= 2;
  141. if (score == 0)
  142. return t;
  143. if (score < cand_score) {
  144. cand = t;
  145. cand_score = score;
  146. }
  147. }
  148. for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) {
  149. if ((!ipv6_addr_equal(local, &t->parms.laddr) &&
  150. (!ipv6_addr_equal(local, &t->parms.raddr) ||
  151. !ipv6_addr_is_multicast(local))) ||
  152. key != t->parms.i_key ||
  153. !(t->dev->flags & IFF_UP))
  154. continue;
  155. if (t->dev->type != ARPHRD_IP6GRE &&
  156. t->dev->type != dev_type)
  157. continue;
  158. score = 0;
  159. if (t->parms.link != link)
  160. score |= 1;
  161. if (t->dev->type != dev_type)
  162. score |= 2;
  163. if (score == 0)
  164. return t;
  165. if (score < cand_score) {
  166. cand = t;
  167. cand_score = score;
  168. }
  169. }
  170. for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) {
  171. if (t->parms.i_key != key ||
  172. !(t->dev->flags & IFF_UP))
  173. continue;
  174. if (t->dev->type != ARPHRD_IP6GRE &&
  175. t->dev->type != dev_type)
  176. continue;
  177. score = 0;
  178. if (t->parms.link != link)
  179. score |= 1;
  180. if (t->dev->type != dev_type)
  181. score |= 2;
  182. if (score == 0)
  183. return t;
  184. if (score < cand_score) {
  185. cand = t;
  186. cand_score = score;
  187. }
  188. }
  189. if (cand)
  190. return cand;
  191. dev = ign->fb_tunnel_dev;
  192. if (dev->flags & IFF_UP)
  193. return netdev_priv(dev);
  194. return NULL;
  195. }
  196. static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign,
  197. const struct __ip6_tnl_parm *p)
  198. {
  199. const struct in6_addr *remote = &p->raddr;
  200. const struct in6_addr *local = &p->laddr;
  201. unsigned int h = HASH_KEY(p->i_key);
  202. int prio = 0;
  203. if (!ipv6_addr_any(local))
  204. prio |= 1;
  205. if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) {
  206. prio |= 2;
  207. h ^= HASH_ADDR(remote);
  208. }
  209. return &ign->tunnels[prio][h];
  210. }
  211. static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign,
  212. const struct ip6_tnl *t)
  213. {
  214. return __ip6gre_bucket(ign, &t->parms);
  215. }
  216. static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t)
  217. {
  218. struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t);
  219. rcu_assign_pointer(t->next, rtnl_dereference(*tp));
  220. rcu_assign_pointer(*tp, t);
  221. }
  222. static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t)
  223. {
  224. struct ip6_tnl __rcu **tp;
  225. struct ip6_tnl *iter;
  226. for (tp = ip6gre_bucket(ign, t);
  227. (iter = rtnl_dereference(*tp)) != NULL;
  228. tp = &iter->next) {
  229. if (t == iter) {
  230. rcu_assign_pointer(*tp, t->next);
  231. break;
  232. }
  233. }
  234. }
  235. static struct ip6_tnl *ip6gre_tunnel_find(struct net *net,
  236. const struct __ip6_tnl_parm *parms,
  237. int type)
  238. {
  239. const struct in6_addr *remote = &parms->raddr;
  240. const struct in6_addr *local = &parms->laddr;
  241. __be32 key = parms->i_key;
  242. int link = parms->link;
  243. struct ip6_tnl *t;
  244. struct ip6_tnl __rcu **tp;
  245. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  246. for (tp = __ip6gre_bucket(ign, parms);
  247. (t = rtnl_dereference(*tp)) != NULL;
  248. tp = &t->next)
  249. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  250. ipv6_addr_equal(remote, &t->parms.raddr) &&
  251. key == t->parms.i_key &&
  252. link == t->parms.link &&
  253. type == t->dev->type)
  254. break;
  255. return t;
  256. }
  257. static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net,
  258. const struct __ip6_tnl_parm *parms, int create)
  259. {
  260. struct ip6_tnl *t, *nt;
  261. struct net_device *dev;
  262. char name[IFNAMSIZ];
  263. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  264. t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE);
  265. if (t && create)
  266. return NULL;
  267. if (t || !create)
  268. return t;
  269. if (parms->name[0])
  270. strlcpy(name, parms->name, IFNAMSIZ);
  271. else
  272. strcpy(name, "ip6gre%d");
  273. dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
  274. ip6gre_tunnel_setup);
  275. if (!dev)
  276. return NULL;
  277. dev_net_set(dev, net);
  278. nt = netdev_priv(dev);
  279. nt->parms = *parms;
  280. dev->rtnl_link_ops = &ip6gre_link_ops;
  281. nt->dev = dev;
  282. nt->net = dev_net(dev);
  283. ip6gre_tnl_link_config(nt, 1);
  284. if (register_netdevice(dev) < 0)
  285. goto failed_free;
  286. /* Can use a lockless transmit, unless we generate output sequences */
  287. if (!(nt->parms.o_flags & TUNNEL_SEQ))
  288. dev->features |= NETIF_F_LLTX;
  289. dev_hold(dev);
  290. ip6gre_tunnel_link(ign, nt);
  291. return nt;
  292. failed_free:
  293. free_netdev(dev);
  294. return NULL;
  295. }
  296. static void ip6gre_tunnel_uninit(struct net_device *dev)
  297. {
  298. struct ip6_tnl *t = netdev_priv(dev);
  299. struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
  300. ip6gre_tunnel_unlink(ign, t);
  301. dst_cache_reset(&t->dst_cache);
  302. dev_put(dev);
  303. }
  304. static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  305. u8 type, u8 code, int offset, __be32 info)
  306. {
  307. const struct gre_base_hdr *greh;
  308. const struct ipv6hdr *ipv6h;
  309. int grehlen = sizeof(*greh);
  310. struct ip6_tnl *t;
  311. int key_off = 0;
  312. __be16 flags;
  313. __be32 key;
  314. if (!pskb_may_pull(skb, offset + grehlen))
  315. return;
  316. greh = (const struct gre_base_hdr *)(skb->data + offset);
  317. flags = greh->flags;
  318. if (flags & (GRE_VERSION | GRE_ROUTING))
  319. return;
  320. if (flags & GRE_CSUM)
  321. grehlen += 4;
  322. if (flags & GRE_KEY) {
  323. key_off = grehlen + offset;
  324. grehlen += 4;
  325. }
  326. if (!pskb_may_pull(skb, offset + grehlen))
  327. return;
  328. ipv6h = (const struct ipv6hdr *)skb->data;
  329. greh = (const struct gre_base_hdr *)(skb->data + offset);
  330. key = key_off ? *(__be32 *)(skb->data + key_off) : 0;
  331. t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
  332. key, greh->protocol);
  333. if (!t)
  334. return;
  335. switch (type) {
  336. __u32 teli;
  337. struct ipv6_tlv_tnl_enc_lim *tel;
  338. __u32 mtu;
  339. case ICMPV6_DEST_UNREACH:
  340. net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
  341. t->parms.name);
  342. if (code != ICMPV6_PORT_UNREACH)
  343. break;
  344. return;
  345. case ICMPV6_TIME_EXCEED:
  346. if (code == ICMPV6_EXC_HOPLIMIT) {
  347. net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
  348. t->parms.name);
  349. break;
  350. }
  351. return;
  352. case ICMPV6_PARAMPROB:
  353. teli = 0;
  354. if (code == ICMPV6_HDR_FIELD)
  355. teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
  356. if (teli && teli == be32_to_cpu(info) - 2) {
  357. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
  358. if (tel->encap_limit == 0) {
  359. net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
  360. t->parms.name);
  361. }
  362. } else {
  363. net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
  364. t->parms.name);
  365. }
  366. return;
  367. case ICMPV6_PKT_TOOBIG:
  368. mtu = be32_to_cpu(info) - offset - t->tun_hlen;
  369. if (t->dev->type == ARPHRD_ETHER)
  370. mtu -= ETH_HLEN;
  371. if (mtu < IPV6_MIN_MTU)
  372. mtu = IPV6_MIN_MTU;
  373. t->dev->mtu = mtu;
  374. return;
  375. }
  376. if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO))
  377. t->err_count++;
  378. else
  379. t->err_count = 1;
  380. t->err_time = jiffies;
  381. }
  382. static int ip6gre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
  383. {
  384. const struct ipv6hdr *ipv6h;
  385. struct ip6_tnl *tunnel;
  386. ipv6h = ipv6_hdr(skb);
  387. tunnel = ip6gre_tunnel_lookup(skb->dev,
  388. &ipv6h->saddr, &ipv6h->daddr, tpi->key,
  389. tpi->proto);
  390. if (tunnel) {
  391. ip6_tnl_rcv(tunnel, skb, tpi, NULL, false);
  392. return PACKET_RCVD;
  393. }
  394. return PACKET_REJECT;
  395. }
  396. static int gre_rcv(struct sk_buff *skb)
  397. {
  398. struct tnl_ptk_info tpi;
  399. bool csum_err = false;
  400. int hdr_len;
  401. hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IPV6), 0);
  402. if (hdr_len < 0)
  403. goto drop;
  404. if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false))
  405. goto drop;
  406. if (ip6gre_rcv(skb, &tpi) == PACKET_RCVD)
  407. return 0;
  408. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  409. drop:
  410. kfree_skb(skb);
  411. return 0;
  412. }
  413. static int gre_handle_offloads(struct sk_buff *skb, bool csum)
  414. {
  415. return iptunnel_handle_offloads(skb,
  416. csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
  417. }
  418. static netdev_tx_t __gre6_xmit(struct sk_buff *skb,
  419. struct net_device *dev, __u8 dsfield,
  420. struct flowi6 *fl6, int encap_limit,
  421. __u32 *pmtu, __be16 proto)
  422. {
  423. struct ip6_tnl *tunnel = netdev_priv(dev);
  424. struct dst_entry *dst = skb_dst(skb);
  425. __be16 protocol;
  426. if (dev->type == ARPHRD_ETHER)
  427. IPCB(skb)->flags = 0;
  428. if (dev->header_ops && dev->type == ARPHRD_IP6GRE)
  429. fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr;
  430. else
  431. fl6->daddr = tunnel->parms.raddr;
  432. if (tunnel->parms.o_flags & TUNNEL_SEQ)
  433. tunnel->o_seqno++;
  434. /* Push GRE header. */
  435. protocol = (dev->type == ARPHRD_ETHER) ? htons(ETH_P_TEB) : proto;
  436. gre_build_header(skb, tunnel->tun_hlen, tunnel->parms.o_flags,
  437. protocol, tunnel->parms.o_key, htonl(tunnel->o_seqno));
  438. /* TooBig packet may have updated dst->dev's mtu */
  439. if (dst && dst_mtu(dst) > dst->dev->mtu)
  440. dst->ops->update_pmtu(dst, NULL, skb, dst->dev->mtu);
  441. return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu,
  442. NEXTHDR_GRE);
  443. }
  444. static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
  445. {
  446. struct ip6_tnl *t = netdev_priv(dev);
  447. const struct iphdr *iph = ip_hdr(skb);
  448. int encap_limit = -1;
  449. struct flowi6 fl6;
  450. __u8 dsfield;
  451. __u32 mtu;
  452. int err;
  453. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  454. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  455. encap_limit = t->parms.encap_limit;
  456. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  457. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  458. dsfield = ipv4_get_dsfield(iph);
  459. else
  460. dsfield = ip6_tclass(t->parms.flowinfo);
  461. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  462. fl6.flowi6_mark = skb->mark;
  463. else
  464. fl6.flowi6_mark = t->parms.fwmark;
  465. fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
  466. err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
  467. if (err)
  468. return -1;
  469. err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
  470. skb->protocol);
  471. if (err != 0) {
  472. /* XXX: send ICMP error even if DF is not set. */
  473. if (err == -EMSGSIZE)
  474. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  475. htonl(mtu));
  476. return -1;
  477. }
  478. return 0;
  479. }
  480. static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
  481. {
  482. struct ip6_tnl *t = netdev_priv(dev);
  483. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  484. int encap_limit = -1;
  485. __u16 offset;
  486. struct flowi6 fl6;
  487. __u8 dsfield;
  488. __u32 mtu;
  489. int err;
  490. if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
  491. return -1;
  492. offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
  493. /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
  494. ipv6h = ipv6_hdr(skb);
  495. if (offset > 0) {
  496. struct ipv6_tlv_tnl_enc_lim *tel;
  497. tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
  498. if (tel->encap_limit == 0) {
  499. icmpv6_send(skb, ICMPV6_PARAMPROB,
  500. ICMPV6_HDR_FIELD, offset + 2);
  501. return -1;
  502. }
  503. encap_limit = tel->encap_limit - 1;
  504. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  505. encap_limit = t->parms.encap_limit;
  506. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  507. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  508. dsfield = ipv6_get_dsfield(ipv6h);
  509. else
  510. dsfield = ip6_tclass(t->parms.flowinfo);
  511. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
  512. fl6.flowlabel |= ip6_flowlabel(ipv6h);
  513. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  514. fl6.flowi6_mark = skb->mark;
  515. else
  516. fl6.flowi6_mark = t->parms.fwmark;
  517. fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
  518. if (gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)))
  519. return -1;
  520. err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit,
  521. &mtu, skb->protocol);
  522. if (err != 0) {
  523. if (err == -EMSGSIZE)
  524. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  525. return -1;
  526. }
  527. return 0;
  528. }
  529. /**
  530. * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  531. * @t: the outgoing tunnel device
  532. * @hdr: IPv6 header from the incoming packet
  533. *
  534. * Description:
  535. * Avoid trivial tunneling loop by checking that tunnel exit-point
  536. * doesn't match source of incoming packet.
  537. *
  538. * Return:
  539. * 1 if conflict,
  540. * 0 else
  541. **/
  542. static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
  543. const struct ipv6hdr *hdr)
  544. {
  545. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  546. }
  547. static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
  548. {
  549. struct ip6_tnl *t = netdev_priv(dev);
  550. int encap_limit = -1;
  551. struct flowi6 fl6;
  552. __u32 mtu;
  553. int err;
  554. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  555. encap_limit = t->parms.encap_limit;
  556. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  557. err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
  558. if (err)
  559. return err;
  560. err = __gre6_xmit(skb, dev, 0, &fl6, encap_limit, &mtu, skb->protocol);
  561. return err;
  562. }
  563. static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
  564. struct net_device *dev)
  565. {
  566. struct ip6_tnl *t = netdev_priv(dev);
  567. struct net_device_stats *stats = &t->dev->stats;
  568. int ret;
  569. if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
  570. goto tx_err;
  571. switch (skb->protocol) {
  572. case htons(ETH_P_IP):
  573. ret = ip6gre_xmit_ipv4(skb, dev);
  574. break;
  575. case htons(ETH_P_IPV6):
  576. ret = ip6gre_xmit_ipv6(skb, dev);
  577. break;
  578. default:
  579. ret = ip6gre_xmit_other(skb, dev);
  580. break;
  581. }
  582. if (ret < 0)
  583. goto tx_err;
  584. return NETDEV_TX_OK;
  585. tx_err:
  586. stats->tx_errors++;
  587. stats->tx_dropped++;
  588. kfree_skb(skb);
  589. return NETDEV_TX_OK;
  590. }
  591. static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
  592. {
  593. struct net_device *dev = t->dev;
  594. struct __ip6_tnl_parm *p = &t->parms;
  595. struct flowi6 *fl6 = &t->fl.u.ip6;
  596. int t_hlen;
  597. if (dev->type != ARPHRD_ETHER) {
  598. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  599. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  600. }
  601. /* Set up flowi template */
  602. fl6->saddr = p->laddr;
  603. fl6->daddr = p->raddr;
  604. fl6->flowi6_oif = p->link;
  605. fl6->flowlabel = 0;
  606. fl6->flowi6_proto = IPPROTO_GRE;
  607. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  608. fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  609. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  610. fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  611. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
  612. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  613. if (p->flags&IP6_TNL_F_CAP_XMIT &&
  614. p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
  615. dev->flags |= IFF_POINTOPOINT;
  616. else
  617. dev->flags &= ~IFF_POINTOPOINT;
  618. t->tun_hlen = gre_calc_hlen(t->parms.o_flags);
  619. t->hlen = t->encap_hlen + t->tun_hlen;
  620. t_hlen = t->hlen + sizeof(struct ipv6hdr);
  621. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  622. int strict = (ipv6_addr_type(&p->raddr) &
  623. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  624. struct rt6_info *rt = rt6_lookup(t->net,
  625. &p->raddr, &p->laddr,
  626. p->link, strict);
  627. if (!rt)
  628. return;
  629. if (rt->dst.dev) {
  630. dev->hard_header_len = rt->dst.dev->hard_header_len +
  631. t_hlen;
  632. if (set_mtu) {
  633. dev->mtu = rt->dst.dev->mtu - t_hlen;
  634. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  635. dev->mtu -= 8;
  636. if (dev->type == ARPHRD_ETHER)
  637. dev->mtu -= ETH_HLEN;
  638. if (dev->mtu < IPV6_MIN_MTU)
  639. dev->mtu = IPV6_MIN_MTU;
  640. }
  641. }
  642. ip6_rt_put(rt);
  643. }
  644. }
  645. static int ip6gre_tnl_change(struct ip6_tnl *t,
  646. const struct __ip6_tnl_parm *p, int set_mtu)
  647. {
  648. t->parms.laddr = p->laddr;
  649. t->parms.raddr = p->raddr;
  650. t->parms.flags = p->flags;
  651. t->parms.hop_limit = p->hop_limit;
  652. t->parms.encap_limit = p->encap_limit;
  653. t->parms.flowinfo = p->flowinfo;
  654. t->parms.link = p->link;
  655. t->parms.proto = p->proto;
  656. t->parms.i_key = p->i_key;
  657. t->parms.o_key = p->o_key;
  658. t->parms.i_flags = p->i_flags;
  659. t->parms.o_flags = p->o_flags;
  660. t->parms.fwmark = p->fwmark;
  661. dst_cache_reset(&t->dst_cache);
  662. ip6gre_tnl_link_config(t, set_mtu);
  663. return 0;
  664. }
  665. static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
  666. const struct ip6_tnl_parm2 *u)
  667. {
  668. p->laddr = u->laddr;
  669. p->raddr = u->raddr;
  670. p->flags = u->flags;
  671. p->hop_limit = u->hop_limit;
  672. p->encap_limit = u->encap_limit;
  673. p->flowinfo = u->flowinfo;
  674. p->link = u->link;
  675. p->i_key = u->i_key;
  676. p->o_key = u->o_key;
  677. p->i_flags = gre_flags_to_tnl_flags(u->i_flags);
  678. p->o_flags = gre_flags_to_tnl_flags(u->o_flags);
  679. memcpy(p->name, u->name, sizeof(u->name));
  680. }
  681. static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
  682. const struct __ip6_tnl_parm *p)
  683. {
  684. u->proto = IPPROTO_GRE;
  685. u->laddr = p->laddr;
  686. u->raddr = p->raddr;
  687. u->flags = p->flags;
  688. u->hop_limit = p->hop_limit;
  689. u->encap_limit = p->encap_limit;
  690. u->flowinfo = p->flowinfo;
  691. u->link = p->link;
  692. u->i_key = p->i_key;
  693. u->o_key = p->o_key;
  694. u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
  695. u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
  696. memcpy(u->name, p->name, sizeof(u->name));
  697. }
  698. static int ip6gre_tunnel_ioctl(struct net_device *dev,
  699. struct ifreq *ifr, int cmd)
  700. {
  701. int err = 0;
  702. struct ip6_tnl_parm2 p;
  703. struct __ip6_tnl_parm p1;
  704. struct ip6_tnl *t = netdev_priv(dev);
  705. struct net *net = t->net;
  706. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  707. memset(&p1, 0, sizeof(p1));
  708. switch (cmd) {
  709. case SIOCGETTUNNEL:
  710. if (dev == ign->fb_tunnel_dev) {
  711. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  712. err = -EFAULT;
  713. break;
  714. }
  715. ip6gre_tnl_parm_from_user(&p1, &p);
  716. t = ip6gre_tunnel_locate(net, &p1, 0);
  717. if (!t)
  718. t = netdev_priv(dev);
  719. }
  720. memset(&p, 0, sizeof(p));
  721. ip6gre_tnl_parm_to_user(&p, &t->parms);
  722. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  723. err = -EFAULT;
  724. break;
  725. case SIOCADDTUNNEL:
  726. case SIOCCHGTUNNEL:
  727. err = -EPERM;
  728. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  729. goto done;
  730. err = -EFAULT;
  731. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  732. goto done;
  733. err = -EINVAL;
  734. if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
  735. goto done;
  736. if (!(p.i_flags&GRE_KEY))
  737. p.i_key = 0;
  738. if (!(p.o_flags&GRE_KEY))
  739. p.o_key = 0;
  740. ip6gre_tnl_parm_from_user(&p1, &p);
  741. t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
  742. if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  743. if (t) {
  744. if (t->dev != dev) {
  745. err = -EEXIST;
  746. break;
  747. }
  748. } else {
  749. t = netdev_priv(dev);
  750. ip6gre_tunnel_unlink(ign, t);
  751. synchronize_net();
  752. ip6gre_tnl_change(t, &p1, 1);
  753. ip6gre_tunnel_link(ign, t);
  754. netdev_state_change(dev);
  755. }
  756. }
  757. if (t) {
  758. err = 0;
  759. memset(&p, 0, sizeof(p));
  760. ip6gre_tnl_parm_to_user(&p, &t->parms);
  761. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  762. err = -EFAULT;
  763. } else
  764. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  765. break;
  766. case SIOCDELTUNNEL:
  767. err = -EPERM;
  768. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  769. goto done;
  770. if (dev == ign->fb_tunnel_dev) {
  771. err = -EFAULT;
  772. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  773. goto done;
  774. err = -ENOENT;
  775. ip6gre_tnl_parm_from_user(&p1, &p);
  776. t = ip6gre_tunnel_locate(net, &p1, 0);
  777. if (!t)
  778. goto done;
  779. err = -EPERM;
  780. if (t == netdev_priv(ign->fb_tunnel_dev))
  781. goto done;
  782. dev = t->dev;
  783. }
  784. unregister_netdevice(dev);
  785. err = 0;
  786. break;
  787. default:
  788. err = -EINVAL;
  789. }
  790. done:
  791. return err;
  792. }
  793. static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
  794. unsigned short type, const void *daddr,
  795. const void *saddr, unsigned int len)
  796. {
  797. struct ip6_tnl *t = netdev_priv(dev);
  798. struct ipv6hdr *ipv6h;
  799. __be16 *p;
  800. ipv6h = skb_push(skb, t->hlen + sizeof(*ipv6h));
  801. ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
  802. t->fl.u.ip6.flowlabel,
  803. true, &t->fl.u.ip6));
  804. ipv6h->hop_limit = t->parms.hop_limit;
  805. ipv6h->nexthdr = NEXTHDR_GRE;
  806. ipv6h->saddr = t->parms.laddr;
  807. ipv6h->daddr = t->parms.raddr;
  808. p = (__be16 *)(ipv6h + 1);
  809. p[0] = t->parms.o_flags;
  810. p[1] = htons(type);
  811. /*
  812. * Set the source hardware address.
  813. */
  814. if (saddr)
  815. memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
  816. if (daddr)
  817. memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
  818. if (!ipv6_addr_any(&ipv6h->daddr))
  819. return t->hlen;
  820. return -t->hlen;
  821. }
  822. static const struct header_ops ip6gre_header_ops = {
  823. .create = ip6gre_header,
  824. };
  825. static const struct net_device_ops ip6gre_netdev_ops = {
  826. .ndo_init = ip6gre_tunnel_init,
  827. .ndo_uninit = ip6gre_tunnel_uninit,
  828. .ndo_start_xmit = ip6gre_tunnel_xmit,
  829. .ndo_do_ioctl = ip6gre_tunnel_ioctl,
  830. .ndo_change_mtu = ip6_tnl_change_mtu,
  831. .ndo_get_stats64 = ip_tunnel_get_stats64,
  832. .ndo_get_iflink = ip6_tnl_get_iflink,
  833. };
  834. static void ip6gre_dev_free(struct net_device *dev)
  835. {
  836. struct ip6_tnl *t = netdev_priv(dev);
  837. dst_cache_destroy(&t->dst_cache);
  838. free_percpu(dev->tstats);
  839. }
  840. static void ip6gre_tunnel_setup(struct net_device *dev)
  841. {
  842. dev->netdev_ops = &ip6gre_netdev_ops;
  843. dev->needs_free_netdev = true;
  844. dev->priv_destructor = ip6gre_dev_free;
  845. dev->type = ARPHRD_IP6GRE;
  846. dev->flags |= IFF_NOARP;
  847. dev->addr_len = sizeof(struct in6_addr);
  848. netif_keep_dst(dev);
  849. /* This perm addr will be used as interface identifier by IPv6 */
  850. dev->addr_assign_type = NET_ADDR_RANDOM;
  851. eth_random_addr(dev->perm_addr);
  852. }
  853. static int ip6gre_tunnel_init_common(struct net_device *dev)
  854. {
  855. struct ip6_tnl *tunnel;
  856. int ret;
  857. int t_hlen;
  858. tunnel = netdev_priv(dev);
  859. tunnel->dev = dev;
  860. tunnel->net = dev_net(dev);
  861. strcpy(tunnel->parms.name, dev->name);
  862. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  863. if (!dev->tstats)
  864. return -ENOMEM;
  865. ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
  866. if (ret) {
  867. free_percpu(dev->tstats);
  868. dev->tstats = NULL;
  869. return ret;
  870. }
  871. tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
  872. tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
  873. t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
  874. dev->hard_header_len = LL_MAX_HEADER + t_hlen;
  875. dev->mtu = ETH_DATA_LEN - t_hlen;
  876. if (dev->type == ARPHRD_ETHER)
  877. dev->mtu -= ETH_HLEN;
  878. if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  879. dev->mtu -= 8;
  880. return 0;
  881. }
  882. static int ip6gre_tunnel_init(struct net_device *dev)
  883. {
  884. struct ip6_tnl *tunnel;
  885. int ret;
  886. ret = ip6gre_tunnel_init_common(dev);
  887. if (ret)
  888. return ret;
  889. tunnel = netdev_priv(dev);
  890. memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
  891. memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
  892. if (ipv6_addr_any(&tunnel->parms.raddr))
  893. dev->header_ops = &ip6gre_header_ops;
  894. return 0;
  895. }
  896. static void ip6gre_fb_tunnel_init(struct net_device *dev)
  897. {
  898. struct ip6_tnl *tunnel = netdev_priv(dev);
  899. tunnel->dev = dev;
  900. tunnel->net = dev_net(dev);
  901. strcpy(tunnel->parms.name, dev->name);
  902. tunnel->hlen = sizeof(struct ipv6hdr) + 4;
  903. dev_hold(dev);
  904. }
  905. static struct inet6_protocol ip6gre_protocol __read_mostly = {
  906. .handler = gre_rcv,
  907. .err_handler = ip6gre_err,
  908. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  909. };
  910. static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)
  911. {
  912. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  913. struct net_device *dev, *aux;
  914. int prio;
  915. for_each_netdev_safe(net, dev, aux)
  916. if (dev->rtnl_link_ops == &ip6gre_link_ops ||
  917. dev->rtnl_link_ops == &ip6gre_tap_ops)
  918. unregister_netdevice_queue(dev, head);
  919. for (prio = 0; prio < 4; prio++) {
  920. int h;
  921. for (h = 0; h < IP6_GRE_HASH_SIZE; h++) {
  922. struct ip6_tnl *t;
  923. t = rtnl_dereference(ign->tunnels[prio][h]);
  924. while (t) {
  925. /* If dev is in the same netns, it has already
  926. * been added to the list by the previous loop.
  927. */
  928. if (!net_eq(dev_net(t->dev), net))
  929. unregister_netdevice_queue(t->dev,
  930. head);
  931. t = rtnl_dereference(t->next);
  932. }
  933. }
  934. }
  935. }
  936. static int __net_init ip6gre_init_net(struct net *net)
  937. {
  938. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  939. int err;
  940. ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
  941. NET_NAME_UNKNOWN,
  942. ip6gre_tunnel_setup);
  943. if (!ign->fb_tunnel_dev) {
  944. err = -ENOMEM;
  945. goto err_alloc_dev;
  946. }
  947. dev_net_set(ign->fb_tunnel_dev, net);
  948. /* FB netdevice is special: we have one, and only one per netns.
  949. * Allowing to move it to another netns is clearly unsafe.
  950. */
  951. ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
  952. ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
  953. ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
  954. err = register_netdev(ign->fb_tunnel_dev);
  955. if (err)
  956. goto err_reg_dev;
  957. rcu_assign_pointer(ign->tunnels_wc[0],
  958. netdev_priv(ign->fb_tunnel_dev));
  959. return 0;
  960. err_reg_dev:
  961. free_netdev(ign->fb_tunnel_dev);
  962. err_alloc_dev:
  963. return err;
  964. }
  965. static void __net_exit ip6gre_exit_net(struct net *net)
  966. {
  967. LIST_HEAD(list);
  968. rtnl_lock();
  969. ip6gre_destroy_tunnels(net, &list);
  970. unregister_netdevice_many(&list);
  971. rtnl_unlock();
  972. }
  973. static struct pernet_operations ip6gre_net_ops = {
  974. .init = ip6gre_init_net,
  975. .exit = ip6gre_exit_net,
  976. .id = &ip6gre_net_id,
  977. .size = sizeof(struct ip6gre_net),
  978. };
  979. static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
  980. struct netlink_ext_ack *extack)
  981. {
  982. __be16 flags;
  983. if (!data)
  984. return 0;
  985. flags = 0;
  986. if (data[IFLA_GRE_IFLAGS])
  987. flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
  988. if (data[IFLA_GRE_OFLAGS])
  989. flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
  990. if (flags & (GRE_VERSION|GRE_ROUTING))
  991. return -EINVAL;
  992. return 0;
  993. }
  994. static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
  995. struct netlink_ext_ack *extack)
  996. {
  997. struct in6_addr daddr;
  998. if (tb[IFLA_ADDRESS]) {
  999. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  1000. return -EINVAL;
  1001. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  1002. return -EADDRNOTAVAIL;
  1003. }
  1004. if (!data)
  1005. goto out;
  1006. if (data[IFLA_GRE_REMOTE]) {
  1007. daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
  1008. if (ipv6_addr_any(&daddr))
  1009. return -EINVAL;
  1010. }
  1011. out:
  1012. return ip6gre_tunnel_validate(tb, data, extack);
  1013. }
  1014. static void ip6gre_netlink_parms(struct nlattr *data[],
  1015. struct __ip6_tnl_parm *parms)
  1016. {
  1017. memset(parms, 0, sizeof(*parms));
  1018. if (!data)
  1019. return;
  1020. if (data[IFLA_GRE_LINK])
  1021. parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
  1022. if (data[IFLA_GRE_IFLAGS])
  1023. parms->i_flags = gre_flags_to_tnl_flags(
  1024. nla_get_be16(data[IFLA_GRE_IFLAGS]));
  1025. if (data[IFLA_GRE_OFLAGS])
  1026. parms->o_flags = gre_flags_to_tnl_flags(
  1027. nla_get_be16(data[IFLA_GRE_OFLAGS]));
  1028. if (data[IFLA_GRE_IKEY])
  1029. parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
  1030. if (data[IFLA_GRE_OKEY])
  1031. parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
  1032. if (data[IFLA_GRE_LOCAL])
  1033. parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
  1034. if (data[IFLA_GRE_REMOTE])
  1035. parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
  1036. if (data[IFLA_GRE_TTL])
  1037. parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
  1038. if (data[IFLA_GRE_ENCAP_LIMIT])
  1039. parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
  1040. if (data[IFLA_GRE_FLOWINFO])
  1041. parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]);
  1042. if (data[IFLA_GRE_FLAGS])
  1043. parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
  1044. if (data[IFLA_GRE_FWMARK])
  1045. parms->fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
  1046. }
  1047. static int ip6gre_tap_init(struct net_device *dev)
  1048. {
  1049. struct ip6_tnl *tunnel;
  1050. int ret;
  1051. ret = ip6gre_tunnel_init_common(dev);
  1052. if (ret)
  1053. return ret;
  1054. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1055. tunnel = netdev_priv(dev);
  1056. ip6gre_tnl_link_config(tunnel, 1);
  1057. return 0;
  1058. }
  1059. static const struct net_device_ops ip6gre_tap_netdev_ops = {
  1060. .ndo_init = ip6gre_tap_init,
  1061. .ndo_uninit = ip6gre_tunnel_uninit,
  1062. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1063. .ndo_set_mac_address = eth_mac_addr,
  1064. .ndo_validate_addr = eth_validate_addr,
  1065. .ndo_change_mtu = ip6_tnl_change_mtu,
  1066. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1067. .ndo_get_iflink = ip6_tnl_get_iflink,
  1068. };
  1069. #define GRE6_FEATURES (NETIF_F_SG | \
  1070. NETIF_F_FRAGLIST | \
  1071. NETIF_F_HIGHDMA | \
  1072. NETIF_F_HW_CSUM)
  1073. static void ip6gre_tap_setup(struct net_device *dev)
  1074. {
  1075. ether_setup(dev);
  1076. dev->netdev_ops = &ip6gre_tap_netdev_ops;
  1077. dev->needs_free_netdev = true;
  1078. dev->priv_destructor = ip6gre_dev_free;
  1079. dev->features |= NETIF_F_NETNS_LOCAL;
  1080. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  1081. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1082. netif_keep_dst(dev);
  1083. }
  1084. static bool ip6gre_netlink_encap_parms(struct nlattr *data[],
  1085. struct ip_tunnel_encap *ipencap)
  1086. {
  1087. bool ret = false;
  1088. memset(ipencap, 0, sizeof(*ipencap));
  1089. if (!data)
  1090. return ret;
  1091. if (data[IFLA_GRE_ENCAP_TYPE]) {
  1092. ret = true;
  1093. ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
  1094. }
  1095. if (data[IFLA_GRE_ENCAP_FLAGS]) {
  1096. ret = true;
  1097. ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
  1098. }
  1099. if (data[IFLA_GRE_ENCAP_SPORT]) {
  1100. ret = true;
  1101. ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
  1102. }
  1103. if (data[IFLA_GRE_ENCAP_DPORT]) {
  1104. ret = true;
  1105. ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
  1106. }
  1107. return ret;
  1108. }
  1109. static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
  1110. struct nlattr *tb[], struct nlattr *data[],
  1111. struct netlink_ext_ack *extack)
  1112. {
  1113. struct ip6_tnl *nt;
  1114. struct net *net = dev_net(dev);
  1115. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1116. struct ip_tunnel_encap ipencap;
  1117. int err;
  1118. nt = netdev_priv(dev);
  1119. if (ip6gre_netlink_encap_parms(data, &ipencap)) {
  1120. int err = ip6_tnl_encap_setup(nt, &ipencap);
  1121. if (err < 0)
  1122. return err;
  1123. }
  1124. ip6gre_netlink_parms(data, &nt->parms);
  1125. if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
  1126. return -EEXIST;
  1127. if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
  1128. eth_hw_addr_random(dev);
  1129. nt->dev = dev;
  1130. nt->net = dev_net(dev);
  1131. ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
  1132. dev->features |= GRE6_FEATURES;
  1133. dev->hw_features |= GRE6_FEATURES;
  1134. if (!(nt->parms.o_flags & TUNNEL_SEQ)) {
  1135. /* TCP offload with GRE SEQ is not supported, nor
  1136. * can we support 2 levels of outer headers requiring
  1137. * an update.
  1138. */
  1139. if (!(nt->parms.o_flags & TUNNEL_CSUM) ||
  1140. (nt->encap.type == TUNNEL_ENCAP_NONE)) {
  1141. dev->features |= NETIF_F_GSO_SOFTWARE;
  1142. dev->hw_features |= NETIF_F_GSO_SOFTWARE;
  1143. }
  1144. /* Can use a lockless transmit, unless we generate
  1145. * output sequences
  1146. */
  1147. dev->features |= NETIF_F_LLTX;
  1148. }
  1149. err = register_netdevice(dev);
  1150. if (err)
  1151. goto out;
  1152. dev_hold(dev);
  1153. ip6gre_tunnel_link(ign, nt);
  1154. out:
  1155. return err;
  1156. }
  1157. static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
  1158. struct nlattr *data[],
  1159. struct netlink_ext_ack *extack)
  1160. {
  1161. struct ip6_tnl *t, *nt = netdev_priv(dev);
  1162. struct net *net = nt->net;
  1163. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1164. struct __ip6_tnl_parm p;
  1165. struct ip_tunnel_encap ipencap;
  1166. if (dev == ign->fb_tunnel_dev)
  1167. return -EINVAL;
  1168. if (ip6gre_netlink_encap_parms(data, &ipencap)) {
  1169. int err = ip6_tnl_encap_setup(nt, &ipencap);
  1170. if (err < 0)
  1171. return err;
  1172. }
  1173. ip6gre_netlink_parms(data, &p);
  1174. t = ip6gre_tunnel_locate(net, &p, 0);
  1175. if (t) {
  1176. if (t->dev != dev)
  1177. return -EEXIST;
  1178. } else {
  1179. t = nt;
  1180. }
  1181. ip6gre_tunnel_unlink(ign, t);
  1182. ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
  1183. ip6gre_tunnel_link(ign, t);
  1184. return 0;
  1185. }
  1186. static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
  1187. {
  1188. struct net *net = dev_net(dev);
  1189. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1190. if (dev != ign->fb_tunnel_dev)
  1191. unregister_netdevice_queue(dev, head);
  1192. }
  1193. static size_t ip6gre_get_size(const struct net_device *dev)
  1194. {
  1195. return
  1196. /* IFLA_GRE_LINK */
  1197. nla_total_size(4) +
  1198. /* IFLA_GRE_IFLAGS */
  1199. nla_total_size(2) +
  1200. /* IFLA_GRE_OFLAGS */
  1201. nla_total_size(2) +
  1202. /* IFLA_GRE_IKEY */
  1203. nla_total_size(4) +
  1204. /* IFLA_GRE_OKEY */
  1205. nla_total_size(4) +
  1206. /* IFLA_GRE_LOCAL */
  1207. nla_total_size(sizeof(struct in6_addr)) +
  1208. /* IFLA_GRE_REMOTE */
  1209. nla_total_size(sizeof(struct in6_addr)) +
  1210. /* IFLA_GRE_TTL */
  1211. nla_total_size(1) +
  1212. /* IFLA_GRE_ENCAP_LIMIT */
  1213. nla_total_size(1) +
  1214. /* IFLA_GRE_FLOWINFO */
  1215. nla_total_size(4) +
  1216. /* IFLA_GRE_FLAGS */
  1217. nla_total_size(4) +
  1218. /* IFLA_GRE_ENCAP_TYPE */
  1219. nla_total_size(2) +
  1220. /* IFLA_GRE_ENCAP_FLAGS */
  1221. nla_total_size(2) +
  1222. /* IFLA_GRE_ENCAP_SPORT */
  1223. nla_total_size(2) +
  1224. /* IFLA_GRE_ENCAP_DPORT */
  1225. nla_total_size(2) +
  1226. /* IFLA_GRE_FWMARK */
  1227. nla_total_size(4) +
  1228. 0;
  1229. }
  1230. static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1231. {
  1232. struct ip6_tnl *t = netdev_priv(dev);
  1233. struct __ip6_tnl_parm *p = &t->parms;
  1234. if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
  1235. nla_put_be16(skb, IFLA_GRE_IFLAGS,
  1236. gre_tnl_flags_to_gre_flags(p->i_flags)) ||
  1237. nla_put_be16(skb, IFLA_GRE_OFLAGS,
  1238. gre_tnl_flags_to_gre_flags(p->o_flags)) ||
  1239. nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
  1240. nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
  1241. nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
  1242. nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
  1243. nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
  1244. nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
  1245. nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
  1246. nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags) ||
  1247. nla_put_u32(skb, IFLA_GRE_FWMARK, p->fwmark))
  1248. goto nla_put_failure;
  1249. if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
  1250. t->encap.type) ||
  1251. nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
  1252. t->encap.sport) ||
  1253. nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
  1254. t->encap.dport) ||
  1255. nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
  1256. t->encap.flags))
  1257. goto nla_put_failure;
  1258. return 0;
  1259. nla_put_failure:
  1260. return -EMSGSIZE;
  1261. }
  1262. static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
  1263. [IFLA_GRE_LINK] = { .type = NLA_U32 },
  1264. [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
  1265. [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
  1266. [IFLA_GRE_IKEY] = { .type = NLA_U32 },
  1267. [IFLA_GRE_OKEY] = { .type = NLA_U32 },
  1268. [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
  1269. [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
  1270. [IFLA_GRE_TTL] = { .type = NLA_U8 },
  1271. [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
  1272. [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 },
  1273. [IFLA_GRE_FLAGS] = { .type = NLA_U32 },
  1274. [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 },
  1275. [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 },
  1276. [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 },
  1277. [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 },
  1278. [IFLA_GRE_FWMARK] = { .type = NLA_U32 },
  1279. };
  1280. static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
  1281. .kind = "ip6gre",
  1282. .maxtype = IFLA_GRE_MAX,
  1283. .policy = ip6gre_policy,
  1284. .priv_size = sizeof(struct ip6_tnl),
  1285. .setup = ip6gre_tunnel_setup,
  1286. .validate = ip6gre_tunnel_validate,
  1287. .newlink = ip6gre_newlink,
  1288. .changelink = ip6gre_changelink,
  1289. .dellink = ip6gre_dellink,
  1290. .get_size = ip6gre_get_size,
  1291. .fill_info = ip6gre_fill_info,
  1292. .get_link_net = ip6_tnl_get_link_net,
  1293. };
  1294. static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
  1295. .kind = "ip6gretap",
  1296. .maxtype = IFLA_GRE_MAX,
  1297. .policy = ip6gre_policy,
  1298. .priv_size = sizeof(struct ip6_tnl),
  1299. .setup = ip6gre_tap_setup,
  1300. .validate = ip6gre_tap_validate,
  1301. .newlink = ip6gre_newlink,
  1302. .changelink = ip6gre_changelink,
  1303. .get_size = ip6gre_get_size,
  1304. .fill_info = ip6gre_fill_info,
  1305. .get_link_net = ip6_tnl_get_link_net,
  1306. };
  1307. /*
  1308. * And now the modules code and kernel interface.
  1309. */
  1310. static int __init ip6gre_init(void)
  1311. {
  1312. int err;
  1313. pr_info("GRE over IPv6 tunneling driver\n");
  1314. err = register_pernet_device(&ip6gre_net_ops);
  1315. if (err < 0)
  1316. return err;
  1317. err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1318. if (err < 0) {
  1319. pr_info("%s: can't add protocol\n", __func__);
  1320. goto add_proto_failed;
  1321. }
  1322. err = rtnl_link_register(&ip6gre_link_ops);
  1323. if (err < 0)
  1324. goto rtnl_link_failed;
  1325. err = rtnl_link_register(&ip6gre_tap_ops);
  1326. if (err < 0)
  1327. goto tap_ops_failed;
  1328. out:
  1329. return err;
  1330. tap_ops_failed:
  1331. rtnl_link_unregister(&ip6gre_link_ops);
  1332. rtnl_link_failed:
  1333. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1334. add_proto_failed:
  1335. unregister_pernet_device(&ip6gre_net_ops);
  1336. goto out;
  1337. }
  1338. static void __exit ip6gre_fini(void)
  1339. {
  1340. rtnl_link_unregister(&ip6gre_tap_ops);
  1341. rtnl_link_unregister(&ip6gre_link_ops);
  1342. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1343. unregister_pernet_device(&ip6gre_net_ops);
  1344. }
  1345. module_init(ip6gre_init);
  1346. module_exit(ip6gre_fini);
  1347. MODULE_LICENSE("GPL");
  1348. MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
  1349. MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
  1350. MODULE_ALIAS_RTNL_LINK("ip6gre");
  1351. MODULE_ALIAS_RTNL_LINK("ip6gretap");
  1352. MODULE_ALIAS_NETDEV("ip6gre0");