ip6_gre.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611
  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 ipv6hdr *ipv6h = (const struct ipv6hdr *)skb->data;
  308. __be16 *p = (__be16 *)(skb->data + offset);
  309. int grehlen = offset + 4;
  310. struct ip6_tnl *t;
  311. __be16 flags;
  312. flags = p[0];
  313. if (flags&(GRE_CSUM|GRE_KEY|GRE_SEQ|GRE_ROUTING|GRE_VERSION)) {
  314. if (flags&(GRE_VERSION|GRE_ROUTING))
  315. return;
  316. if (flags&GRE_KEY) {
  317. grehlen += 4;
  318. if (flags&GRE_CSUM)
  319. grehlen += 4;
  320. }
  321. }
  322. /* If only 8 bytes returned, keyed message will be dropped here */
  323. if (!pskb_may_pull(skb, grehlen))
  324. return;
  325. ipv6h = (const struct ipv6hdr *)skb->data;
  326. p = (__be16 *)(skb->data + offset);
  327. t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
  328. flags & GRE_KEY ?
  329. *(((__be32 *)p) + (grehlen / 4) - 1) : 0,
  330. p[1]);
  331. if (!t)
  332. return;
  333. switch (type) {
  334. __u32 teli;
  335. struct ipv6_tlv_tnl_enc_lim *tel;
  336. __u32 mtu;
  337. case ICMPV6_DEST_UNREACH:
  338. net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
  339. t->parms.name);
  340. break;
  341. case ICMPV6_TIME_EXCEED:
  342. if (code == ICMPV6_EXC_HOPLIMIT) {
  343. net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
  344. t->parms.name);
  345. }
  346. break;
  347. case ICMPV6_PARAMPROB:
  348. teli = 0;
  349. if (code == ICMPV6_HDR_FIELD)
  350. teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
  351. if (teli && teli == be32_to_cpu(info) - 2) {
  352. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
  353. if (tel->encap_limit == 0) {
  354. net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
  355. t->parms.name);
  356. }
  357. } else {
  358. net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
  359. t->parms.name);
  360. }
  361. break;
  362. case ICMPV6_PKT_TOOBIG:
  363. mtu = be32_to_cpu(info) - offset;
  364. if (mtu < IPV6_MIN_MTU)
  365. mtu = IPV6_MIN_MTU;
  366. t->dev->mtu = mtu;
  367. break;
  368. }
  369. if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO))
  370. t->err_count++;
  371. else
  372. t->err_count = 1;
  373. t->err_time = jiffies;
  374. }
  375. static int ip6gre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
  376. {
  377. const struct ipv6hdr *ipv6h;
  378. struct ip6_tnl *tunnel;
  379. ipv6h = ipv6_hdr(skb);
  380. tunnel = ip6gre_tunnel_lookup(skb->dev,
  381. &ipv6h->saddr, &ipv6h->daddr, tpi->key,
  382. tpi->proto);
  383. if (tunnel) {
  384. ip6_tnl_rcv(tunnel, skb, tpi, NULL, false);
  385. return PACKET_RCVD;
  386. }
  387. return PACKET_REJECT;
  388. }
  389. static int gre_rcv(struct sk_buff *skb)
  390. {
  391. struct tnl_ptk_info tpi;
  392. bool csum_err = false;
  393. int hdr_len;
  394. hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IPV6), 0);
  395. if (hdr_len < 0)
  396. goto drop;
  397. if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false))
  398. goto drop;
  399. if (ip6gre_rcv(skb, &tpi) == PACKET_RCVD)
  400. return 0;
  401. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  402. drop:
  403. kfree_skb(skb);
  404. return 0;
  405. }
  406. struct ipv6_tel_txoption {
  407. struct ipv6_txoptions ops;
  408. __u8 dst_opt[8];
  409. };
  410. static int gre_handle_offloads(struct sk_buff *skb, bool csum)
  411. {
  412. return iptunnel_handle_offloads(skb,
  413. csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
  414. }
  415. static netdev_tx_t __gre6_xmit(struct sk_buff *skb,
  416. struct net_device *dev, __u8 dsfield,
  417. struct flowi6 *fl6, int encap_limit,
  418. __u32 *pmtu, __be16 proto)
  419. {
  420. struct ip6_tnl *tunnel = netdev_priv(dev);
  421. __be16 protocol = (dev->type == ARPHRD_ETHER) ?
  422. htons(ETH_P_TEB) : proto;
  423. if (dev->type == ARPHRD_ETHER)
  424. IPCB(skb)->flags = 0;
  425. if (dev->header_ops && dev->type == ARPHRD_IP6GRE)
  426. fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr;
  427. else
  428. fl6->daddr = tunnel->parms.raddr;
  429. if (tunnel->parms.o_flags & TUNNEL_SEQ)
  430. tunnel->o_seqno++;
  431. /* Push GRE header. */
  432. gre_build_header(skb, tunnel->tun_hlen, tunnel->parms.o_flags,
  433. protocol, tunnel->parms.o_key, htonl(tunnel->o_seqno));
  434. return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu,
  435. NEXTHDR_GRE);
  436. }
  437. static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
  438. {
  439. struct ip6_tnl *t = netdev_priv(dev);
  440. const struct iphdr *iph = ip_hdr(skb);
  441. int encap_limit = -1;
  442. struct flowi6 fl6;
  443. __u8 dsfield;
  444. __u32 mtu;
  445. int err;
  446. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  447. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  448. encap_limit = t->parms.encap_limit;
  449. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  450. dsfield = ipv4_get_dsfield(iph);
  451. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  452. fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
  453. & IPV6_TCLASS_MASK;
  454. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  455. fl6.flowi6_mark = skb->mark;
  456. fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
  457. err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
  458. if (err)
  459. return -1;
  460. err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
  461. skb->protocol);
  462. if (err != 0) {
  463. /* XXX: send ICMP error even if DF is not set. */
  464. if (err == -EMSGSIZE)
  465. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  466. htonl(mtu));
  467. return -1;
  468. }
  469. return 0;
  470. }
  471. static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
  472. {
  473. struct ip6_tnl *t = netdev_priv(dev);
  474. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  475. int encap_limit = -1;
  476. __u16 offset;
  477. struct flowi6 fl6;
  478. __u8 dsfield;
  479. __u32 mtu;
  480. int err;
  481. if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
  482. return -1;
  483. offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
  484. if (offset > 0) {
  485. struct ipv6_tlv_tnl_enc_lim *tel;
  486. tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
  487. if (tel->encap_limit == 0) {
  488. icmpv6_send(skb, ICMPV6_PARAMPROB,
  489. ICMPV6_HDR_FIELD, offset + 2);
  490. return -1;
  491. }
  492. encap_limit = tel->encap_limit - 1;
  493. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  494. encap_limit = t->parms.encap_limit;
  495. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  496. dsfield = ipv6_get_dsfield(ipv6h);
  497. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  498. fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
  499. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
  500. fl6.flowlabel |= ip6_flowlabel(ipv6h);
  501. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  502. fl6.flowi6_mark = skb->mark;
  503. fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
  504. if (gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)))
  505. return -1;
  506. err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit,
  507. &mtu, skb->protocol);
  508. if (err != 0) {
  509. if (err == -EMSGSIZE)
  510. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  511. return -1;
  512. }
  513. return 0;
  514. }
  515. /**
  516. * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  517. * @t: the outgoing tunnel device
  518. * @hdr: IPv6 header from the incoming packet
  519. *
  520. * Description:
  521. * Avoid trivial tunneling loop by checking that tunnel exit-point
  522. * doesn't match source of incoming packet.
  523. *
  524. * Return:
  525. * 1 if conflict,
  526. * 0 else
  527. **/
  528. static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
  529. const struct ipv6hdr *hdr)
  530. {
  531. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  532. }
  533. static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
  534. {
  535. struct ip6_tnl *t = netdev_priv(dev);
  536. int encap_limit = -1;
  537. struct flowi6 fl6;
  538. __u32 mtu;
  539. int err;
  540. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  541. encap_limit = t->parms.encap_limit;
  542. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  543. err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
  544. if (err)
  545. return err;
  546. err = __gre6_xmit(skb, dev, 0, &fl6, encap_limit, &mtu, skb->protocol);
  547. return err;
  548. }
  549. static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
  550. struct net_device *dev)
  551. {
  552. struct ip6_tnl *t = netdev_priv(dev);
  553. struct net_device_stats *stats = &t->dev->stats;
  554. int ret;
  555. if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
  556. goto tx_err;
  557. switch (skb->protocol) {
  558. case htons(ETH_P_IP):
  559. ret = ip6gre_xmit_ipv4(skb, dev);
  560. break;
  561. case htons(ETH_P_IPV6):
  562. ret = ip6gre_xmit_ipv6(skb, dev);
  563. break;
  564. default:
  565. ret = ip6gre_xmit_other(skb, dev);
  566. break;
  567. }
  568. if (ret < 0)
  569. goto tx_err;
  570. return NETDEV_TX_OK;
  571. tx_err:
  572. stats->tx_errors++;
  573. stats->tx_dropped++;
  574. kfree_skb(skb);
  575. return NETDEV_TX_OK;
  576. }
  577. static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
  578. {
  579. struct net_device *dev = t->dev;
  580. struct __ip6_tnl_parm *p = &t->parms;
  581. struct flowi6 *fl6 = &t->fl.u.ip6;
  582. int t_hlen;
  583. if (dev->type != ARPHRD_ETHER) {
  584. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  585. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  586. }
  587. /* Set up flowi template */
  588. fl6->saddr = p->laddr;
  589. fl6->daddr = p->raddr;
  590. fl6->flowi6_oif = p->link;
  591. fl6->flowlabel = 0;
  592. fl6->flowi6_proto = IPPROTO_GRE;
  593. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  594. fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  595. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  596. fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  597. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
  598. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  599. if (p->flags&IP6_TNL_F_CAP_XMIT &&
  600. p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
  601. dev->flags |= IFF_POINTOPOINT;
  602. else
  603. dev->flags &= ~IFF_POINTOPOINT;
  604. t->tun_hlen = gre_calc_hlen(t->parms.o_flags);
  605. t->hlen = t->encap_hlen + t->tun_hlen;
  606. t_hlen = t->hlen + sizeof(struct ipv6hdr);
  607. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  608. int strict = (ipv6_addr_type(&p->raddr) &
  609. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  610. struct rt6_info *rt = rt6_lookup(t->net,
  611. &p->raddr, &p->laddr,
  612. p->link, strict);
  613. if (!rt)
  614. return;
  615. if (rt->dst.dev) {
  616. dev->hard_header_len = rt->dst.dev->hard_header_len +
  617. t_hlen;
  618. if (set_mtu) {
  619. dev->mtu = rt->dst.dev->mtu - t_hlen;
  620. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  621. dev->mtu -= 8;
  622. if (dev->type == ARPHRD_ETHER)
  623. dev->mtu -= ETH_HLEN;
  624. if (dev->mtu < IPV6_MIN_MTU)
  625. dev->mtu = IPV6_MIN_MTU;
  626. }
  627. }
  628. ip6_rt_put(rt);
  629. }
  630. }
  631. static int ip6gre_tnl_change(struct ip6_tnl *t,
  632. const struct __ip6_tnl_parm *p, int set_mtu)
  633. {
  634. t->parms.laddr = p->laddr;
  635. t->parms.raddr = p->raddr;
  636. t->parms.flags = p->flags;
  637. t->parms.hop_limit = p->hop_limit;
  638. t->parms.encap_limit = p->encap_limit;
  639. t->parms.flowinfo = p->flowinfo;
  640. t->parms.link = p->link;
  641. t->parms.proto = p->proto;
  642. t->parms.i_key = p->i_key;
  643. t->parms.o_key = p->o_key;
  644. t->parms.i_flags = p->i_flags;
  645. t->parms.o_flags = p->o_flags;
  646. dst_cache_reset(&t->dst_cache);
  647. ip6gre_tnl_link_config(t, set_mtu);
  648. return 0;
  649. }
  650. static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
  651. const struct ip6_tnl_parm2 *u)
  652. {
  653. p->laddr = u->laddr;
  654. p->raddr = u->raddr;
  655. p->flags = u->flags;
  656. p->hop_limit = u->hop_limit;
  657. p->encap_limit = u->encap_limit;
  658. p->flowinfo = u->flowinfo;
  659. p->link = u->link;
  660. p->i_key = u->i_key;
  661. p->o_key = u->o_key;
  662. p->i_flags = gre_flags_to_tnl_flags(u->i_flags);
  663. p->o_flags = gre_flags_to_tnl_flags(u->o_flags);
  664. memcpy(p->name, u->name, sizeof(u->name));
  665. }
  666. static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
  667. const struct __ip6_tnl_parm *p)
  668. {
  669. u->proto = IPPROTO_GRE;
  670. u->laddr = p->laddr;
  671. u->raddr = p->raddr;
  672. u->flags = p->flags;
  673. u->hop_limit = p->hop_limit;
  674. u->encap_limit = p->encap_limit;
  675. u->flowinfo = p->flowinfo;
  676. u->link = p->link;
  677. u->i_key = p->i_key;
  678. u->o_key = p->o_key;
  679. u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
  680. u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
  681. memcpy(u->name, p->name, sizeof(u->name));
  682. }
  683. static int ip6gre_tunnel_ioctl(struct net_device *dev,
  684. struct ifreq *ifr, int cmd)
  685. {
  686. int err = 0;
  687. struct ip6_tnl_parm2 p;
  688. struct __ip6_tnl_parm p1;
  689. struct ip6_tnl *t = netdev_priv(dev);
  690. struct net *net = t->net;
  691. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  692. memset(&p1, 0, sizeof(p1));
  693. switch (cmd) {
  694. case SIOCGETTUNNEL:
  695. if (dev == ign->fb_tunnel_dev) {
  696. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  697. err = -EFAULT;
  698. break;
  699. }
  700. ip6gre_tnl_parm_from_user(&p1, &p);
  701. t = ip6gre_tunnel_locate(net, &p1, 0);
  702. if (!t)
  703. t = netdev_priv(dev);
  704. }
  705. memset(&p, 0, sizeof(p));
  706. ip6gre_tnl_parm_to_user(&p, &t->parms);
  707. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  708. err = -EFAULT;
  709. break;
  710. case SIOCADDTUNNEL:
  711. case SIOCCHGTUNNEL:
  712. err = -EPERM;
  713. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  714. goto done;
  715. err = -EFAULT;
  716. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  717. goto done;
  718. err = -EINVAL;
  719. if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
  720. goto done;
  721. if (!(p.i_flags&GRE_KEY))
  722. p.i_key = 0;
  723. if (!(p.o_flags&GRE_KEY))
  724. p.o_key = 0;
  725. ip6gre_tnl_parm_from_user(&p1, &p);
  726. t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
  727. if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  728. if (t) {
  729. if (t->dev != dev) {
  730. err = -EEXIST;
  731. break;
  732. }
  733. } else {
  734. t = netdev_priv(dev);
  735. ip6gre_tunnel_unlink(ign, t);
  736. synchronize_net();
  737. ip6gre_tnl_change(t, &p1, 1);
  738. ip6gre_tunnel_link(ign, t);
  739. netdev_state_change(dev);
  740. }
  741. }
  742. if (t) {
  743. err = 0;
  744. memset(&p, 0, sizeof(p));
  745. ip6gre_tnl_parm_to_user(&p, &t->parms);
  746. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  747. err = -EFAULT;
  748. } else
  749. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  750. break;
  751. case SIOCDELTUNNEL:
  752. err = -EPERM;
  753. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  754. goto done;
  755. if (dev == ign->fb_tunnel_dev) {
  756. err = -EFAULT;
  757. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  758. goto done;
  759. err = -ENOENT;
  760. ip6gre_tnl_parm_from_user(&p1, &p);
  761. t = ip6gre_tunnel_locate(net, &p1, 0);
  762. if (!t)
  763. goto done;
  764. err = -EPERM;
  765. if (t == netdev_priv(ign->fb_tunnel_dev))
  766. goto done;
  767. dev = t->dev;
  768. }
  769. unregister_netdevice(dev);
  770. err = 0;
  771. break;
  772. default:
  773. err = -EINVAL;
  774. }
  775. done:
  776. return err;
  777. }
  778. static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
  779. unsigned short type,
  780. const void *daddr, const void *saddr, unsigned int len)
  781. {
  782. struct ip6_tnl *t = netdev_priv(dev);
  783. struct ipv6hdr *ipv6h = (struct ipv6hdr *)skb_push(skb, t->hlen);
  784. __be16 *p = (__be16 *)(ipv6h+1);
  785. ip6_flow_hdr(ipv6h, 0,
  786. ip6_make_flowlabel(dev_net(dev), skb,
  787. t->fl.u.ip6.flowlabel, true,
  788. &t->fl.u.ip6));
  789. ipv6h->hop_limit = t->parms.hop_limit;
  790. ipv6h->nexthdr = NEXTHDR_GRE;
  791. ipv6h->saddr = t->parms.laddr;
  792. ipv6h->daddr = t->parms.raddr;
  793. p[0] = t->parms.o_flags;
  794. p[1] = htons(type);
  795. /*
  796. * Set the source hardware address.
  797. */
  798. if (saddr)
  799. memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
  800. if (daddr)
  801. memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
  802. if (!ipv6_addr_any(&ipv6h->daddr))
  803. return t->hlen;
  804. return -t->hlen;
  805. }
  806. static const struct header_ops ip6gre_header_ops = {
  807. .create = ip6gre_header,
  808. };
  809. static const struct net_device_ops ip6gre_netdev_ops = {
  810. .ndo_init = ip6gre_tunnel_init,
  811. .ndo_uninit = ip6gre_tunnel_uninit,
  812. .ndo_start_xmit = ip6gre_tunnel_xmit,
  813. .ndo_do_ioctl = ip6gre_tunnel_ioctl,
  814. .ndo_change_mtu = ip6_tnl_change_mtu,
  815. .ndo_get_stats64 = ip_tunnel_get_stats64,
  816. .ndo_get_iflink = ip6_tnl_get_iflink,
  817. };
  818. static void ip6gre_dev_free(struct net_device *dev)
  819. {
  820. struct ip6_tnl *t = netdev_priv(dev);
  821. dst_cache_destroy(&t->dst_cache);
  822. free_percpu(dev->tstats);
  823. free_netdev(dev);
  824. }
  825. static void ip6gre_tunnel_setup(struct net_device *dev)
  826. {
  827. dev->netdev_ops = &ip6gre_netdev_ops;
  828. dev->destructor = ip6gre_dev_free;
  829. dev->type = ARPHRD_IP6GRE;
  830. dev->flags |= IFF_NOARP;
  831. dev->addr_len = sizeof(struct in6_addr);
  832. netif_keep_dst(dev);
  833. }
  834. static int ip6gre_tunnel_init_common(struct net_device *dev)
  835. {
  836. struct ip6_tnl *tunnel;
  837. int ret;
  838. int t_hlen;
  839. tunnel = netdev_priv(dev);
  840. tunnel->dev = dev;
  841. tunnel->net = dev_net(dev);
  842. strcpy(tunnel->parms.name, dev->name);
  843. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  844. if (!dev->tstats)
  845. return -ENOMEM;
  846. ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
  847. if (ret) {
  848. free_percpu(dev->tstats);
  849. dev->tstats = NULL;
  850. return ret;
  851. }
  852. tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
  853. tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
  854. t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
  855. dev->hard_header_len = LL_MAX_HEADER + t_hlen;
  856. dev->mtu = ETH_DATA_LEN - t_hlen;
  857. if (dev->type == ARPHRD_ETHER)
  858. dev->mtu -= ETH_HLEN;
  859. if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  860. dev->mtu -= 8;
  861. return 0;
  862. }
  863. static int ip6gre_tunnel_init(struct net_device *dev)
  864. {
  865. struct ip6_tnl *tunnel;
  866. int ret;
  867. ret = ip6gre_tunnel_init_common(dev);
  868. if (ret)
  869. return ret;
  870. tunnel = netdev_priv(dev);
  871. memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
  872. memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
  873. if (ipv6_addr_any(&tunnel->parms.raddr))
  874. dev->header_ops = &ip6gre_header_ops;
  875. return 0;
  876. }
  877. static void ip6gre_fb_tunnel_init(struct net_device *dev)
  878. {
  879. struct ip6_tnl *tunnel = netdev_priv(dev);
  880. tunnel->dev = dev;
  881. tunnel->net = dev_net(dev);
  882. strcpy(tunnel->parms.name, dev->name);
  883. tunnel->hlen = sizeof(struct ipv6hdr) + 4;
  884. dev_hold(dev);
  885. }
  886. static struct inet6_protocol ip6gre_protocol __read_mostly = {
  887. .handler = gre_rcv,
  888. .err_handler = ip6gre_err,
  889. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  890. };
  891. static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)
  892. {
  893. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  894. struct net_device *dev, *aux;
  895. int prio;
  896. for_each_netdev_safe(net, dev, aux)
  897. if (dev->rtnl_link_ops == &ip6gre_link_ops ||
  898. dev->rtnl_link_ops == &ip6gre_tap_ops)
  899. unregister_netdevice_queue(dev, head);
  900. for (prio = 0; prio < 4; prio++) {
  901. int h;
  902. for (h = 0; h < IP6_GRE_HASH_SIZE; h++) {
  903. struct ip6_tnl *t;
  904. t = rtnl_dereference(ign->tunnels[prio][h]);
  905. while (t) {
  906. /* If dev is in the same netns, it has already
  907. * been added to the list by the previous loop.
  908. */
  909. if (!net_eq(dev_net(t->dev), net))
  910. unregister_netdevice_queue(t->dev,
  911. head);
  912. t = rtnl_dereference(t->next);
  913. }
  914. }
  915. }
  916. }
  917. static int __net_init ip6gre_init_net(struct net *net)
  918. {
  919. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  920. int err;
  921. ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
  922. NET_NAME_UNKNOWN,
  923. ip6gre_tunnel_setup);
  924. if (!ign->fb_tunnel_dev) {
  925. err = -ENOMEM;
  926. goto err_alloc_dev;
  927. }
  928. dev_net_set(ign->fb_tunnel_dev, net);
  929. /* FB netdevice is special: we have one, and only one per netns.
  930. * Allowing to move it to another netns is clearly unsafe.
  931. */
  932. ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
  933. ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
  934. ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
  935. err = register_netdev(ign->fb_tunnel_dev);
  936. if (err)
  937. goto err_reg_dev;
  938. rcu_assign_pointer(ign->tunnels_wc[0],
  939. netdev_priv(ign->fb_tunnel_dev));
  940. return 0;
  941. err_reg_dev:
  942. ip6gre_dev_free(ign->fb_tunnel_dev);
  943. err_alloc_dev:
  944. return err;
  945. }
  946. static void __net_exit ip6gre_exit_net(struct net *net)
  947. {
  948. LIST_HEAD(list);
  949. rtnl_lock();
  950. ip6gre_destroy_tunnels(net, &list);
  951. unregister_netdevice_many(&list);
  952. rtnl_unlock();
  953. }
  954. static struct pernet_operations ip6gre_net_ops = {
  955. .init = ip6gre_init_net,
  956. .exit = ip6gre_exit_net,
  957. .id = &ip6gre_net_id,
  958. .size = sizeof(struct ip6gre_net),
  959. };
  960. static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
  961. {
  962. __be16 flags;
  963. if (!data)
  964. return 0;
  965. flags = 0;
  966. if (data[IFLA_GRE_IFLAGS])
  967. flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
  968. if (data[IFLA_GRE_OFLAGS])
  969. flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
  970. if (flags & (GRE_VERSION|GRE_ROUTING))
  971. return -EINVAL;
  972. return 0;
  973. }
  974. static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[])
  975. {
  976. struct in6_addr daddr;
  977. if (tb[IFLA_ADDRESS]) {
  978. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  979. return -EINVAL;
  980. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  981. return -EADDRNOTAVAIL;
  982. }
  983. if (!data)
  984. goto out;
  985. if (data[IFLA_GRE_REMOTE]) {
  986. daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
  987. if (ipv6_addr_any(&daddr))
  988. return -EINVAL;
  989. }
  990. out:
  991. return ip6gre_tunnel_validate(tb, data);
  992. }
  993. static void ip6gre_netlink_parms(struct nlattr *data[],
  994. struct __ip6_tnl_parm *parms)
  995. {
  996. memset(parms, 0, sizeof(*parms));
  997. if (!data)
  998. return;
  999. if (data[IFLA_GRE_LINK])
  1000. parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
  1001. if (data[IFLA_GRE_IFLAGS])
  1002. parms->i_flags = gre_flags_to_tnl_flags(
  1003. nla_get_be16(data[IFLA_GRE_IFLAGS]));
  1004. if (data[IFLA_GRE_OFLAGS])
  1005. parms->o_flags = gre_flags_to_tnl_flags(
  1006. nla_get_be16(data[IFLA_GRE_OFLAGS]));
  1007. if (data[IFLA_GRE_IKEY])
  1008. parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
  1009. if (data[IFLA_GRE_OKEY])
  1010. parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
  1011. if (data[IFLA_GRE_LOCAL])
  1012. parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
  1013. if (data[IFLA_GRE_REMOTE])
  1014. parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
  1015. if (data[IFLA_GRE_TTL])
  1016. parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
  1017. if (data[IFLA_GRE_ENCAP_LIMIT])
  1018. parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
  1019. if (data[IFLA_GRE_FLOWINFO])
  1020. parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]);
  1021. if (data[IFLA_GRE_FLAGS])
  1022. parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
  1023. }
  1024. static int ip6gre_tap_init(struct net_device *dev)
  1025. {
  1026. struct ip6_tnl *tunnel;
  1027. int ret;
  1028. ret = ip6gre_tunnel_init_common(dev);
  1029. if (ret)
  1030. return ret;
  1031. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1032. tunnel = netdev_priv(dev);
  1033. ip6gre_tnl_link_config(tunnel, 1);
  1034. return 0;
  1035. }
  1036. static const struct net_device_ops ip6gre_tap_netdev_ops = {
  1037. .ndo_init = ip6gre_tap_init,
  1038. .ndo_uninit = ip6gre_tunnel_uninit,
  1039. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1040. .ndo_set_mac_address = eth_mac_addr,
  1041. .ndo_validate_addr = eth_validate_addr,
  1042. .ndo_change_mtu = ip6_tnl_change_mtu,
  1043. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1044. .ndo_get_iflink = ip6_tnl_get_iflink,
  1045. };
  1046. #define GRE6_FEATURES (NETIF_F_SG | \
  1047. NETIF_F_FRAGLIST | \
  1048. NETIF_F_HIGHDMA | \
  1049. NETIF_F_HW_CSUM)
  1050. static void ip6gre_tap_setup(struct net_device *dev)
  1051. {
  1052. ether_setup(dev);
  1053. dev->netdev_ops = &ip6gre_tap_netdev_ops;
  1054. dev->destructor = ip6gre_dev_free;
  1055. dev->features |= NETIF_F_NETNS_LOCAL;
  1056. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  1057. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1058. }
  1059. static bool ip6gre_netlink_encap_parms(struct nlattr *data[],
  1060. struct ip_tunnel_encap *ipencap)
  1061. {
  1062. bool ret = false;
  1063. memset(ipencap, 0, sizeof(*ipencap));
  1064. if (!data)
  1065. return ret;
  1066. if (data[IFLA_GRE_ENCAP_TYPE]) {
  1067. ret = true;
  1068. ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
  1069. }
  1070. if (data[IFLA_GRE_ENCAP_FLAGS]) {
  1071. ret = true;
  1072. ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
  1073. }
  1074. if (data[IFLA_GRE_ENCAP_SPORT]) {
  1075. ret = true;
  1076. ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
  1077. }
  1078. if (data[IFLA_GRE_ENCAP_DPORT]) {
  1079. ret = true;
  1080. ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
  1081. }
  1082. return ret;
  1083. }
  1084. static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
  1085. struct nlattr *tb[], struct nlattr *data[])
  1086. {
  1087. struct ip6_tnl *nt;
  1088. struct net *net = dev_net(dev);
  1089. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1090. struct ip_tunnel_encap ipencap;
  1091. int err;
  1092. nt = netdev_priv(dev);
  1093. if (ip6gre_netlink_encap_parms(data, &ipencap)) {
  1094. int err = ip6_tnl_encap_setup(nt, &ipencap);
  1095. if (err < 0)
  1096. return err;
  1097. }
  1098. ip6gre_netlink_parms(data, &nt->parms);
  1099. if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
  1100. return -EEXIST;
  1101. if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
  1102. eth_hw_addr_random(dev);
  1103. nt->dev = dev;
  1104. nt->net = dev_net(dev);
  1105. ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
  1106. dev->features |= GRE6_FEATURES;
  1107. dev->hw_features |= GRE6_FEATURES;
  1108. if (!(nt->parms.o_flags & TUNNEL_SEQ)) {
  1109. /* TCP offload with GRE SEQ is not supported, nor
  1110. * can we support 2 levels of outer headers requiring
  1111. * an update.
  1112. */
  1113. if (!(nt->parms.o_flags & TUNNEL_CSUM) ||
  1114. (nt->encap.type == TUNNEL_ENCAP_NONE)) {
  1115. dev->features |= NETIF_F_GSO_SOFTWARE;
  1116. dev->hw_features |= NETIF_F_GSO_SOFTWARE;
  1117. }
  1118. /* Can use a lockless transmit, unless we generate
  1119. * output sequences
  1120. */
  1121. dev->features |= NETIF_F_LLTX;
  1122. }
  1123. err = register_netdevice(dev);
  1124. if (err)
  1125. goto out;
  1126. dev_hold(dev);
  1127. ip6gre_tunnel_link(ign, nt);
  1128. out:
  1129. return err;
  1130. }
  1131. static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
  1132. struct nlattr *data[])
  1133. {
  1134. struct ip6_tnl *t, *nt = netdev_priv(dev);
  1135. struct net *net = nt->net;
  1136. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1137. struct __ip6_tnl_parm p;
  1138. struct ip_tunnel_encap ipencap;
  1139. if (dev == ign->fb_tunnel_dev)
  1140. return -EINVAL;
  1141. if (ip6gre_netlink_encap_parms(data, &ipencap)) {
  1142. int err = ip6_tnl_encap_setup(nt, &ipencap);
  1143. if (err < 0)
  1144. return err;
  1145. }
  1146. ip6gre_netlink_parms(data, &p);
  1147. t = ip6gre_tunnel_locate(net, &p, 0);
  1148. if (t) {
  1149. if (t->dev != dev)
  1150. return -EEXIST;
  1151. } else {
  1152. t = nt;
  1153. }
  1154. ip6gre_tunnel_unlink(ign, t);
  1155. ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
  1156. ip6gre_tunnel_link(ign, t);
  1157. return 0;
  1158. }
  1159. static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
  1160. {
  1161. struct net *net = dev_net(dev);
  1162. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1163. if (dev != ign->fb_tunnel_dev)
  1164. unregister_netdevice_queue(dev, head);
  1165. }
  1166. static size_t ip6gre_get_size(const struct net_device *dev)
  1167. {
  1168. return
  1169. /* IFLA_GRE_LINK */
  1170. nla_total_size(4) +
  1171. /* IFLA_GRE_IFLAGS */
  1172. nla_total_size(2) +
  1173. /* IFLA_GRE_OFLAGS */
  1174. nla_total_size(2) +
  1175. /* IFLA_GRE_IKEY */
  1176. nla_total_size(4) +
  1177. /* IFLA_GRE_OKEY */
  1178. nla_total_size(4) +
  1179. /* IFLA_GRE_LOCAL */
  1180. nla_total_size(sizeof(struct in6_addr)) +
  1181. /* IFLA_GRE_REMOTE */
  1182. nla_total_size(sizeof(struct in6_addr)) +
  1183. /* IFLA_GRE_TTL */
  1184. nla_total_size(1) +
  1185. /* IFLA_GRE_ENCAP_LIMIT */
  1186. nla_total_size(1) +
  1187. /* IFLA_GRE_FLOWINFO */
  1188. nla_total_size(4) +
  1189. /* IFLA_GRE_FLAGS */
  1190. nla_total_size(4) +
  1191. /* IFLA_GRE_ENCAP_TYPE */
  1192. nla_total_size(2) +
  1193. /* IFLA_GRE_ENCAP_FLAGS */
  1194. nla_total_size(2) +
  1195. /* IFLA_GRE_ENCAP_SPORT */
  1196. nla_total_size(2) +
  1197. /* IFLA_GRE_ENCAP_DPORT */
  1198. nla_total_size(2) +
  1199. 0;
  1200. }
  1201. static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1202. {
  1203. struct ip6_tnl *t = netdev_priv(dev);
  1204. struct __ip6_tnl_parm *p = &t->parms;
  1205. if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
  1206. nla_put_be16(skb, IFLA_GRE_IFLAGS,
  1207. gre_tnl_flags_to_gre_flags(p->i_flags)) ||
  1208. nla_put_be16(skb, IFLA_GRE_OFLAGS,
  1209. gre_tnl_flags_to_gre_flags(p->o_flags)) ||
  1210. nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
  1211. nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
  1212. nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
  1213. nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
  1214. nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
  1215. nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
  1216. nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
  1217. nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags))
  1218. goto nla_put_failure;
  1219. if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
  1220. t->encap.type) ||
  1221. nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
  1222. t->encap.sport) ||
  1223. nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
  1224. t->encap.dport) ||
  1225. nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
  1226. t->encap.flags))
  1227. goto nla_put_failure;
  1228. return 0;
  1229. nla_put_failure:
  1230. return -EMSGSIZE;
  1231. }
  1232. static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
  1233. [IFLA_GRE_LINK] = { .type = NLA_U32 },
  1234. [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
  1235. [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
  1236. [IFLA_GRE_IKEY] = { .type = NLA_U32 },
  1237. [IFLA_GRE_OKEY] = { .type = NLA_U32 },
  1238. [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
  1239. [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
  1240. [IFLA_GRE_TTL] = { .type = NLA_U8 },
  1241. [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
  1242. [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 },
  1243. [IFLA_GRE_FLAGS] = { .type = NLA_U32 },
  1244. [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 },
  1245. [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 },
  1246. [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 },
  1247. [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 },
  1248. };
  1249. static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
  1250. .kind = "ip6gre",
  1251. .maxtype = IFLA_GRE_MAX,
  1252. .policy = ip6gre_policy,
  1253. .priv_size = sizeof(struct ip6_tnl),
  1254. .setup = ip6gre_tunnel_setup,
  1255. .validate = ip6gre_tunnel_validate,
  1256. .newlink = ip6gre_newlink,
  1257. .changelink = ip6gre_changelink,
  1258. .dellink = ip6gre_dellink,
  1259. .get_size = ip6gre_get_size,
  1260. .fill_info = ip6gre_fill_info,
  1261. .get_link_net = ip6_tnl_get_link_net,
  1262. };
  1263. static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
  1264. .kind = "ip6gretap",
  1265. .maxtype = IFLA_GRE_MAX,
  1266. .policy = ip6gre_policy,
  1267. .priv_size = sizeof(struct ip6_tnl),
  1268. .setup = ip6gre_tap_setup,
  1269. .validate = ip6gre_tap_validate,
  1270. .newlink = ip6gre_newlink,
  1271. .changelink = ip6gre_changelink,
  1272. .get_size = ip6gre_get_size,
  1273. .fill_info = ip6gre_fill_info,
  1274. .get_link_net = ip6_tnl_get_link_net,
  1275. };
  1276. /*
  1277. * And now the modules code and kernel interface.
  1278. */
  1279. static int __init ip6gre_init(void)
  1280. {
  1281. int err;
  1282. pr_info("GRE over IPv6 tunneling driver\n");
  1283. err = register_pernet_device(&ip6gre_net_ops);
  1284. if (err < 0)
  1285. return err;
  1286. err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1287. if (err < 0) {
  1288. pr_info("%s: can't add protocol\n", __func__);
  1289. goto add_proto_failed;
  1290. }
  1291. err = rtnl_link_register(&ip6gre_link_ops);
  1292. if (err < 0)
  1293. goto rtnl_link_failed;
  1294. err = rtnl_link_register(&ip6gre_tap_ops);
  1295. if (err < 0)
  1296. goto tap_ops_failed;
  1297. out:
  1298. return err;
  1299. tap_ops_failed:
  1300. rtnl_link_unregister(&ip6gre_link_ops);
  1301. rtnl_link_failed:
  1302. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1303. add_proto_failed:
  1304. unregister_pernet_device(&ip6gre_net_ops);
  1305. goto out;
  1306. }
  1307. static void __exit ip6gre_fini(void)
  1308. {
  1309. rtnl_link_unregister(&ip6gre_tap_ops);
  1310. rtnl_link_unregister(&ip6gre_link_ops);
  1311. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1312. unregister_pernet_device(&ip6gre_net_ops);
  1313. }
  1314. module_init(ip6gre_init);
  1315. module_exit(ip6gre_fini);
  1316. MODULE_LICENSE("GPL");
  1317. MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
  1318. MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
  1319. MODULE_ALIAS_RTNL_LINK("ip6gre");
  1320. MODULE_ALIAS_RTNL_LINK("ip6gretap");
  1321. MODULE_ALIAS_NETDEV("ip6gre0");