ip6_tunnel.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947
  1. /*
  2. * IPv6 tunneling device
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Ville Nuorvala <vnuorval@tcs.hut.fi>
  7. * Yasuyuki Kozakai <kozakai@linux-ipv6.org>
  8. *
  9. * Based on:
  10. * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
  11. *
  12. * RFC 2473
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. *
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/module.h>
  22. #include <linux/capability.h>
  23. #include <linux/errno.h>
  24. #include <linux/types.h>
  25. #include <linux/sockios.h>
  26. #include <linux/icmp.h>
  27. #include <linux/if.h>
  28. #include <linux/in.h>
  29. #include <linux/ip.h>
  30. #include <linux/net.h>
  31. #include <linux/in6.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/if_arp.h>
  34. #include <linux/icmpv6.h>
  35. #include <linux/init.h>
  36. #include <linux/route.h>
  37. #include <linux/rtnetlink.h>
  38. #include <linux/netfilter_ipv6.h>
  39. #include <linux/slab.h>
  40. #include <linux/hash.h>
  41. #include <linux/etherdevice.h>
  42. #include <asm/uaccess.h>
  43. #include <linux/atomic.h>
  44. #include <net/icmp.h>
  45. #include <net/ip.h>
  46. #include <net/ip_tunnels.h>
  47. #include <net/ipv6.h>
  48. #include <net/ip6_route.h>
  49. #include <net/addrconf.h>
  50. #include <net/ip6_tunnel.h>
  51. #include <net/xfrm.h>
  52. #include <net/dsfield.h>
  53. #include <net/inet_ecn.h>
  54. #include <net/net_namespace.h>
  55. #include <net/netns/generic.h>
  56. MODULE_AUTHOR("Ville Nuorvala");
  57. MODULE_DESCRIPTION("IPv6 tunneling device");
  58. MODULE_LICENSE("GPL");
  59. MODULE_ALIAS_RTNL_LINK("ip6tnl");
  60. MODULE_ALIAS_NETDEV("ip6tnl0");
  61. #define HASH_SIZE_SHIFT 5
  62. #define HASH_SIZE (1 << HASH_SIZE_SHIFT)
  63. static bool log_ecn_error = true;
  64. module_param(log_ecn_error, bool, 0644);
  65. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  66. static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
  67. {
  68. u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
  69. return hash_32(hash, HASH_SIZE_SHIFT);
  70. }
  71. static int ip6_tnl_dev_init(struct net_device *dev);
  72. static void ip6_tnl_dev_setup(struct net_device *dev);
  73. static struct rtnl_link_ops ip6_link_ops __read_mostly;
  74. static int ip6_tnl_net_id __read_mostly;
  75. struct ip6_tnl_net {
  76. /* the IPv6 tunnel fallback device */
  77. struct net_device *fb_tnl_dev;
  78. /* lists for storing tunnels in use */
  79. struct ip6_tnl __rcu *tnls_r_l[HASH_SIZE];
  80. struct ip6_tnl __rcu *tnls_wc[1];
  81. struct ip6_tnl __rcu **tnls[2];
  82. };
  83. static struct net_device_stats *ip6_get_stats(struct net_device *dev)
  84. {
  85. struct pcpu_sw_netstats tmp, sum = { 0 };
  86. int i;
  87. for_each_possible_cpu(i) {
  88. unsigned int start;
  89. const struct pcpu_sw_netstats *tstats =
  90. per_cpu_ptr(dev->tstats, i);
  91. do {
  92. start = u64_stats_fetch_begin_irq(&tstats->syncp);
  93. tmp.rx_packets = tstats->rx_packets;
  94. tmp.rx_bytes = tstats->rx_bytes;
  95. tmp.tx_packets = tstats->tx_packets;
  96. tmp.tx_bytes = tstats->tx_bytes;
  97. } while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
  98. sum.rx_packets += tmp.rx_packets;
  99. sum.rx_bytes += tmp.rx_bytes;
  100. sum.tx_packets += tmp.tx_packets;
  101. sum.tx_bytes += tmp.tx_bytes;
  102. }
  103. dev->stats.rx_packets = sum.rx_packets;
  104. dev->stats.rx_bytes = sum.rx_bytes;
  105. dev->stats.tx_packets = sum.tx_packets;
  106. dev->stats.tx_bytes = sum.tx_bytes;
  107. return &dev->stats;
  108. }
  109. /*
  110. * Locking : hash tables are protected by RCU and RTNL
  111. */
  112. struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
  113. {
  114. struct dst_entry *dst = t->dst_cache;
  115. if (dst && dst->obsolete &&
  116. !dst->ops->check(dst, t->dst_cookie)) {
  117. t->dst_cache = NULL;
  118. dst_release(dst);
  119. return NULL;
  120. }
  121. return dst;
  122. }
  123. EXPORT_SYMBOL_GPL(ip6_tnl_dst_check);
  124. void ip6_tnl_dst_reset(struct ip6_tnl *t)
  125. {
  126. dst_release(t->dst_cache);
  127. t->dst_cache = NULL;
  128. }
  129. EXPORT_SYMBOL_GPL(ip6_tnl_dst_reset);
  130. void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
  131. {
  132. struct rt6_info *rt = (struct rt6_info *) dst;
  133. t->dst_cookie = rt6_get_cookie(rt);
  134. dst_release(t->dst_cache);
  135. t->dst_cache = dst;
  136. }
  137. EXPORT_SYMBOL_GPL(ip6_tnl_dst_store);
  138. /**
  139. * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
  140. * @remote: the address of the tunnel exit-point
  141. * @local: the address of the tunnel entry-point
  142. *
  143. * Return:
  144. * tunnel matching given end-points if found,
  145. * else fallback tunnel if its device is up,
  146. * else %NULL
  147. **/
  148. #define for_each_ip6_tunnel_rcu(start) \
  149. for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
  150. static struct ip6_tnl *
  151. ip6_tnl_lookup(struct net *net, const struct in6_addr *remote, const struct in6_addr *local)
  152. {
  153. unsigned int hash = HASH(remote, local);
  154. struct ip6_tnl *t;
  155. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  156. struct in6_addr any;
  157. for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  158. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  159. ipv6_addr_equal(remote, &t->parms.raddr) &&
  160. (t->dev->flags & IFF_UP))
  161. return t;
  162. }
  163. memset(&any, 0, sizeof(any));
  164. hash = HASH(&any, local);
  165. for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  166. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  167. (t->dev->flags & IFF_UP))
  168. return t;
  169. }
  170. hash = HASH(remote, &any);
  171. for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  172. if (ipv6_addr_equal(remote, &t->parms.raddr) &&
  173. (t->dev->flags & IFF_UP))
  174. return t;
  175. }
  176. t = rcu_dereference(ip6n->tnls_wc[0]);
  177. if (t && (t->dev->flags & IFF_UP))
  178. return t;
  179. return NULL;
  180. }
  181. /**
  182. * ip6_tnl_bucket - get head of list matching given tunnel parameters
  183. * @p: parameters containing tunnel end-points
  184. *
  185. * Description:
  186. * ip6_tnl_bucket() returns the head of the list matching the
  187. * &struct in6_addr entries laddr and raddr in @p.
  188. *
  189. * Return: head of IPv6 tunnel list
  190. **/
  191. static struct ip6_tnl __rcu **
  192. ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p)
  193. {
  194. const struct in6_addr *remote = &p->raddr;
  195. const struct in6_addr *local = &p->laddr;
  196. unsigned int h = 0;
  197. int prio = 0;
  198. if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
  199. prio = 1;
  200. h = HASH(remote, local);
  201. }
  202. return &ip6n->tnls[prio][h];
  203. }
  204. /**
  205. * ip6_tnl_link - add tunnel to hash table
  206. * @t: tunnel to be added
  207. **/
  208. static void
  209. ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
  210. {
  211. struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms);
  212. rcu_assign_pointer(t->next , rtnl_dereference(*tp));
  213. rcu_assign_pointer(*tp, t);
  214. }
  215. /**
  216. * ip6_tnl_unlink - remove tunnel from hash table
  217. * @t: tunnel to be removed
  218. **/
  219. static void
  220. ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
  221. {
  222. struct ip6_tnl __rcu **tp;
  223. struct ip6_tnl *iter;
  224. for (tp = ip6_tnl_bucket(ip6n, &t->parms);
  225. (iter = rtnl_dereference(*tp)) != NULL;
  226. tp = &iter->next) {
  227. if (t == iter) {
  228. rcu_assign_pointer(*tp, t->next);
  229. break;
  230. }
  231. }
  232. }
  233. static void ip6_dev_free(struct net_device *dev)
  234. {
  235. free_percpu(dev->tstats);
  236. free_netdev(dev);
  237. }
  238. static int ip6_tnl_create2(struct net_device *dev)
  239. {
  240. struct ip6_tnl *t = netdev_priv(dev);
  241. struct net *net = dev_net(dev);
  242. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  243. int err;
  244. t = netdev_priv(dev);
  245. err = register_netdevice(dev);
  246. if (err < 0)
  247. goto out;
  248. strcpy(t->parms.name, dev->name);
  249. dev->rtnl_link_ops = &ip6_link_ops;
  250. dev_hold(dev);
  251. ip6_tnl_link(ip6n, t);
  252. return 0;
  253. out:
  254. return err;
  255. }
  256. /**
  257. * ip6_tnl_create - create a new tunnel
  258. * @p: tunnel parameters
  259. * @pt: pointer to new tunnel
  260. *
  261. * Description:
  262. * Create tunnel matching given parameters.
  263. *
  264. * Return:
  265. * created tunnel or error pointer
  266. **/
  267. static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
  268. {
  269. struct net_device *dev;
  270. struct ip6_tnl *t;
  271. char name[IFNAMSIZ];
  272. int err = -ENOMEM;
  273. if (p->name[0])
  274. strlcpy(name, p->name, IFNAMSIZ);
  275. else
  276. sprintf(name, "ip6tnl%%d");
  277. dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
  278. ip6_tnl_dev_setup);
  279. if (!dev)
  280. goto failed;
  281. dev_net_set(dev, net);
  282. t = netdev_priv(dev);
  283. t->parms = *p;
  284. t->net = dev_net(dev);
  285. err = ip6_tnl_create2(dev);
  286. if (err < 0)
  287. goto failed_free;
  288. return t;
  289. failed_free:
  290. ip6_dev_free(dev);
  291. failed:
  292. return ERR_PTR(err);
  293. }
  294. /**
  295. * ip6_tnl_locate - find or create tunnel matching given parameters
  296. * @p: tunnel parameters
  297. * @create: != 0 if allowed to create new tunnel if no match found
  298. *
  299. * Description:
  300. * ip6_tnl_locate() first tries to locate an existing tunnel
  301. * based on @parms. If this is unsuccessful, but @create is set a new
  302. * tunnel device is created and registered for use.
  303. *
  304. * Return:
  305. * matching tunnel or error pointer
  306. **/
  307. static struct ip6_tnl *ip6_tnl_locate(struct net *net,
  308. struct __ip6_tnl_parm *p, int create)
  309. {
  310. const struct in6_addr *remote = &p->raddr;
  311. const struct in6_addr *local = &p->laddr;
  312. struct ip6_tnl __rcu **tp;
  313. struct ip6_tnl *t;
  314. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  315. for (tp = ip6_tnl_bucket(ip6n, p);
  316. (t = rtnl_dereference(*tp)) != NULL;
  317. tp = &t->next) {
  318. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  319. ipv6_addr_equal(remote, &t->parms.raddr)) {
  320. if (create)
  321. return ERR_PTR(-EEXIST);
  322. return t;
  323. }
  324. }
  325. if (!create)
  326. return ERR_PTR(-ENODEV);
  327. return ip6_tnl_create(net, p);
  328. }
  329. /**
  330. * ip6_tnl_dev_uninit - tunnel device uninitializer
  331. * @dev: the device to be destroyed
  332. *
  333. * Description:
  334. * ip6_tnl_dev_uninit() removes tunnel from its list
  335. **/
  336. static void
  337. ip6_tnl_dev_uninit(struct net_device *dev)
  338. {
  339. struct ip6_tnl *t = netdev_priv(dev);
  340. struct net *net = t->net;
  341. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  342. if (dev == ip6n->fb_tnl_dev)
  343. RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
  344. else
  345. ip6_tnl_unlink(ip6n, t);
  346. ip6_tnl_dst_reset(t);
  347. dev_put(dev);
  348. }
  349. /**
  350. * parse_tvl_tnl_enc_lim - handle encapsulation limit option
  351. * @skb: received socket buffer
  352. *
  353. * Return:
  354. * 0 if none was found,
  355. * else index to encapsulation limit
  356. **/
  357. __u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw)
  358. {
  359. const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) raw;
  360. __u8 nexthdr = ipv6h->nexthdr;
  361. __u16 off = sizeof(*ipv6h);
  362. while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
  363. __u16 optlen = 0;
  364. struct ipv6_opt_hdr *hdr;
  365. if (raw + off + sizeof(*hdr) > skb->data &&
  366. !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
  367. break;
  368. hdr = (struct ipv6_opt_hdr *) (raw + off);
  369. if (nexthdr == NEXTHDR_FRAGMENT) {
  370. struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
  371. if (frag_hdr->frag_off)
  372. break;
  373. optlen = 8;
  374. } else if (nexthdr == NEXTHDR_AUTH) {
  375. optlen = (hdr->hdrlen + 2) << 2;
  376. } else {
  377. optlen = ipv6_optlen(hdr);
  378. }
  379. if (nexthdr == NEXTHDR_DEST) {
  380. __u16 i = off + 2;
  381. while (1) {
  382. struct ipv6_tlv_tnl_enc_lim *tel;
  383. /* No more room for encapsulation limit */
  384. if (i + sizeof (*tel) > off + optlen)
  385. break;
  386. tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
  387. /* return index of option if found and valid */
  388. if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
  389. tel->length == 1)
  390. return i;
  391. /* else jump to next option */
  392. if (tel->type)
  393. i += tel->length + 2;
  394. else
  395. i++;
  396. }
  397. }
  398. nexthdr = hdr->nexthdr;
  399. off += optlen;
  400. }
  401. return 0;
  402. }
  403. EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim);
  404. /**
  405. * ip6_tnl_err - tunnel error handler
  406. *
  407. * Description:
  408. * ip6_tnl_err() should handle errors in the tunnel according
  409. * to the specifications in RFC 2473.
  410. **/
  411. static int
  412. ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
  413. u8 *type, u8 *code, int *msg, __u32 *info, int offset)
  414. {
  415. const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) skb->data;
  416. struct ip6_tnl *t;
  417. int rel_msg = 0;
  418. u8 rel_type = ICMPV6_DEST_UNREACH;
  419. u8 rel_code = ICMPV6_ADDR_UNREACH;
  420. u8 tproto;
  421. __u32 rel_info = 0;
  422. __u16 len;
  423. int err = -ENOENT;
  424. /* If the packet doesn't contain the original IPv6 header we are
  425. in trouble since we might need the source address for further
  426. processing of the error. */
  427. rcu_read_lock();
  428. t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr, &ipv6h->saddr);
  429. if (!t)
  430. goto out;
  431. tproto = ACCESS_ONCE(t->parms.proto);
  432. if (tproto != ipproto && tproto != 0)
  433. goto out;
  434. err = 0;
  435. switch (*type) {
  436. __u32 teli;
  437. struct ipv6_tlv_tnl_enc_lim *tel;
  438. __u32 mtu;
  439. case ICMPV6_DEST_UNREACH:
  440. net_warn_ratelimited("%s: Path to destination invalid or inactive!\n",
  441. t->parms.name);
  442. rel_msg = 1;
  443. break;
  444. case ICMPV6_TIME_EXCEED:
  445. if ((*code) == ICMPV6_EXC_HOPLIMIT) {
  446. net_warn_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
  447. t->parms.name);
  448. rel_msg = 1;
  449. }
  450. break;
  451. case ICMPV6_PARAMPROB:
  452. teli = 0;
  453. if ((*code) == ICMPV6_HDR_FIELD)
  454. teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
  455. if (teli && teli == *info - 2) {
  456. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
  457. if (tel->encap_limit == 0) {
  458. net_warn_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
  459. t->parms.name);
  460. rel_msg = 1;
  461. }
  462. } else {
  463. net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
  464. t->parms.name);
  465. }
  466. break;
  467. case ICMPV6_PKT_TOOBIG:
  468. mtu = *info - offset;
  469. if (mtu < IPV6_MIN_MTU)
  470. mtu = IPV6_MIN_MTU;
  471. t->dev->mtu = mtu;
  472. len = sizeof(*ipv6h) + ntohs(ipv6h->payload_len);
  473. if (len > mtu) {
  474. rel_type = ICMPV6_PKT_TOOBIG;
  475. rel_code = 0;
  476. rel_info = mtu;
  477. rel_msg = 1;
  478. }
  479. break;
  480. }
  481. *type = rel_type;
  482. *code = rel_code;
  483. *info = rel_info;
  484. *msg = rel_msg;
  485. out:
  486. rcu_read_unlock();
  487. return err;
  488. }
  489. static int
  490. ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  491. u8 type, u8 code, int offset, __be32 info)
  492. {
  493. int rel_msg = 0;
  494. u8 rel_type = type;
  495. u8 rel_code = code;
  496. __u32 rel_info = ntohl(info);
  497. int err;
  498. struct sk_buff *skb2;
  499. const struct iphdr *eiph;
  500. struct rtable *rt;
  501. struct flowi4 fl4;
  502. err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
  503. &rel_msg, &rel_info, offset);
  504. if (err < 0)
  505. return err;
  506. if (rel_msg == 0)
  507. return 0;
  508. switch (rel_type) {
  509. case ICMPV6_DEST_UNREACH:
  510. if (rel_code != ICMPV6_ADDR_UNREACH)
  511. return 0;
  512. rel_type = ICMP_DEST_UNREACH;
  513. rel_code = ICMP_HOST_UNREACH;
  514. break;
  515. case ICMPV6_PKT_TOOBIG:
  516. if (rel_code != 0)
  517. return 0;
  518. rel_type = ICMP_DEST_UNREACH;
  519. rel_code = ICMP_FRAG_NEEDED;
  520. break;
  521. case NDISC_REDIRECT:
  522. rel_type = ICMP_REDIRECT;
  523. rel_code = ICMP_REDIR_HOST;
  524. default:
  525. return 0;
  526. }
  527. if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
  528. return 0;
  529. skb2 = skb_clone(skb, GFP_ATOMIC);
  530. if (!skb2)
  531. return 0;
  532. skb_dst_drop(skb2);
  533. skb_pull(skb2, offset);
  534. skb_reset_network_header(skb2);
  535. eiph = ip_hdr(skb2);
  536. /* Try to guess incoming interface */
  537. rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
  538. eiph->saddr, 0,
  539. 0, 0,
  540. IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
  541. if (IS_ERR(rt))
  542. goto out;
  543. skb2->dev = rt->dst.dev;
  544. /* route "incoming" packet */
  545. if (rt->rt_flags & RTCF_LOCAL) {
  546. ip_rt_put(rt);
  547. rt = NULL;
  548. rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
  549. eiph->daddr, eiph->saddr,
  550. 0, 0,
  551. IPPROTO_IPIP,
  552. RT_TOS(eiph->tos), 0);
  553. if (IS_ERR(rt) ||
  554. rt->dst.dev->type != ARPHRD_TUNNEL) {
  555. if (!IS_ERR(rt))
  556. ip_rt_put(rt);
  557. goto out;
  558. }
  559. skb_dst_set(skb2, &rt->dst);
  560. } else {
  561. ip_rt_put(rt);
  562. if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
  563. skb2->dev) ||
  564. skb_dst(skb2)->dev->type != ARPHRD_TUNNEL)
  565. goto out;
  566. }
  567. /* change mtu on this route */
  568. if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
  569. if (rel_info > dst_mtu(skb_dst(skb2)))
  570. goto out;
  571. skb_dst(skb2)->ops->update_pmtu(skb_dst(skb2), NULL, skb2, rel_info);
  572. }
  573. if (rel_type == ICMP_REDIRECT)
  574. skb_dst(skb2)->ops->redirect(skb_dst(skb2), NULL, skb2);
  575. icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
  576. out:
  577. kfree_skb(skb2);
  578. return 0;
  579. }
  580. static int
  581. ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  582. u8 type, u8 code, int offset, __be32 info)
  583. {
  584. int rel_msg = 0;
  585. u8 rel_type = type;
  586. u8 rel_code = code;
  587. __u32 rel_info = ntohl(info);
  588. int err;
  589. err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
  590. &rel_msg, &rel_info, offset);
  591. if (err < 0)
  592. return err;
  593. if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
  594. struct rt6_info *rt;
  595. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  596. if (!skb2)
  597. return 0;
  598. skb_dst_drop(skb2);
  599. skb_pull(skb2, offset);
  600. skb_reset_network_header(skb2);
  601. /* Try to guess incoming interface */
  602. rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr,
  603. NULL, 0, 0);
  604. if (rt && rt->dst.dev)
  605. skb2->dev = rt->dst.dev;
  606. icmpv6_send(skb2, rel_type, rel_code, rel_info);
  607. ip6_rt_put(rt);
  608. kfree_skb(skb2);
  609. }
  610. return 0;
  611. }
  612. static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
  613. const struct ipv6hdr *ipv6h,
  614. struct sk_buff *skb)
  615. {
  616. __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
  617. if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
  618. ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
  619. return IP6_ECN_decapsulate(ipv6h, skb);
  620. }
  621. static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
  622. const struct ipv6hdr *ipv6h,
  623. struct sk_buff *skb)
  624. {
  625. if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
  626. ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
  627. return IP6_ECN_decapsulate(ipv6h, skb);
  628. }
  629. __u32 ip6_tnl_get_cap(struct ip6_tnl *t,
  630. const struct in6_addr *laddr,
  631. const struct in6_addr *raddr)
  632. {
  633. struct __ip6_tnl_parm *p = &t->parms;
  634. int ltype = ipv6_addr_type(laddr);
  635. int rtype = ipv6_addr_type(raddr);
  636. __u32 flags = 0;
  637. if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) {
  638. flags = IP6_TNL_F_CAP_PER_PACKET;
  639. } else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
  640. rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
  641. !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
  642. (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
  643. if (ltype&IPV6_ADDR_UNICAST)
  644. flags |= IP6_TNL_F_CAP_XMIT;
  645. if (rtype&IPV6_ADDR_UNICAST)
  646. flags |= IP6_TNL_F_CAP_RCV;
  647. }
  648. return flags;
  649. }
  650. EXPORT_SYMBOL(ip6_tnl_get_cap);
  651. /* called with rcu_read_lock() */
  652. int ip6_tnl_rcv_ctl(struct ip6_tnl *t,
  653. const struct in6_addr *laddr,
  654. const struct in6_addr *raddr)
  655. {
  656. struct __ip6_tnl_parm *p = &t->parms;
  657. int ret = 0;
  658. struct net *net = t->net;
  659. if ((p->flags & IP6_TNL_F_CAP_RCV) ||
  660. ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
  661. (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) {
  662. struct net_device *ldev = NULL;
  663. if (p->link)
  664. ldev = dev_get_by_index_rcu(net, p->link);
  665. if ((ipv6_addr_is_multicast(laddr) ||
  666. likely(ipv6_chk_addr(net, laddr, ldev, 0))) &&
  667. likely(!ipv6_chk_addr(net, raddr, NULL, 0)))
  668. ret = 1;
  669. }
  670. return ret;
  671. }
  672. EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl);
  673. /**
  674. * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
  675. * @skb: received socket buffer
  676. * @protocol: ethernet protocol ID
  677. * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
  678. *
  679. * Return: 0
  680. **/
  681. static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
  682. __u8 ipproto,
  683. int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
  684. const struct ipv6hdr *ipv6h,
  685. struct sk_buff *skb))
  686. {
  687. struct ip6_tnl *t;
  688. const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  689. u8 tproto;
  690. int err;
  691. rcu_read_lock();
  692. t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr);
  693. if (t) {
  694. struct pcpu_sw_netstats *tstats;
  695. tproto = ACCESS_ONCE(t->parms.proto);
  696. if (tproto != ipproto && tproto != 0) {
  697. rcu_read_unlock();
  698. goto discard;
  699. }
  700. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  701. rcu_read_unlock();
  702. goto discard;
  703. }
  704. if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
  705. t->dev->stats.rx_dropped++;
  706. rcu_read_unlock();
  707. goto discard;
  708. }
  709. skb->mac_header = skb->network_header;
  710. skb_reset_network_header(skb);
  711. skb->protocol = htons(protocol);
  712. memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
  713. __skb_tunnel_rx(skb, t->dev, t->net);
  714. err = dscp_ecn_decapsulate(t, ipv6h, skb);
  715. if (unlikely(err)) {
  716. if (log_ecn_error)
  717. net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n",
  718. &ipv6h->saddr,
  719. ipv6_get_dsfield(ipv6h));
  720. if (err > 1) {
  721. ++t->dev->stats.rx_frame_errors;
  722. ++t->dev->stats.rx_errors;
  723. rcu_read_unlock();
  724. goto discard;
  725. }
  726. }
  727. tstats = this_cpu_ptr(t->dev->tstats);
  728. u64_stats_update_begin(&tstats->syncp);
  729. tstats->rx_packets++;
  730. tstats->rx_bytes += skb->len;
  731. u64_stats_update_end(&tstats->syncp);
  732. netif_rx(skb);
  733. rcu_read_unlock();
  734. return 0;
  735. }
  736. rcu_read_unlock();
  737. return 1;
  738. discard:
  739. kfree_skb(skb);
  740. return 0;
  741. }
  742. static int ip4ip6_rcv(struct sk_buff *skb)
  743. {
  744. return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
  745. ip4ip6_dscp_ecn_decapsulate);
  746. }
  747. static int ip6ip6_rcv(struct sk_buff *skb)
  748. {
  749. return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
  750. ip6ip6_dscp_ecn_decapsulate);
  751. }
  752. struct ipv6_tel_txoption {
  753. struct ipv6_txoptions ops;
  754. __u8 dst_opt[8];
  755. };
  756. static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
  757. {
  758. memset(opt, 0, sizeof(struct ipv6_tel_txoption));
  759. opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
  760. opt->dst_opt[3] = 1;
  761. opt->dst_opt[4] = encap_limit;
  762. opt->dst_opt[5] = IPV6_TLV_PADN;
  763. opt->dst_opt[6] = 1;
  764. opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
  765. opt->ops.opt_nflen = 8;
  766. }
  767. /**
  768. * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  769. * @t: the outgoing tunnel device
  770. * @hdr: IPv6 header from the incoming packet
  771. *
  772. * Description:
  773. * Avoid trivial tunneling loop by checking that tunnel exit-point
  774. * doesn't match source of incoming packet.
  775. *
  776. * Return:
  777. * 1 if conflict,
  778. * 0 else
  779. **/
  780. static inline bool
  781. ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
  782. {
  783. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  784. }
  785. int ip6_tnl_xmit_ctl(struct ip6_tnl *t,
  786. const struct in6_addr *laddr,
  787. const struct in6_addr *raddr)
  788. {
  789. struct __ip6_tnl_parm *p = &t->parms;
  790. int ret = 0;
  791. struct net *net = t->net;
  792. if ((p->flags & IP6_TNL_F_CAP_XMIT) ||
  793. ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
  794. (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_XMIT))) {
  795. struct net_device *ldev = NULL;
  796. rcu_read_lock();
  797. if (p->link)
  798. ldev = dev_get_by_index_rcu(net, p->link);
  799. if (unlikely(!ipv6_chk_addr(net, laddr, ldev, 0)))
  800. pr_warn("%s xmit: Local address not yet configured!\n",
  801. p->name);
  802. else if (!ipv6_addr_is_multicast(raddr) &&
  803. unlikely(ipv6_chk_addr(net, raddr, NULL, 0)))
  804. pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
  805. p->name);
  806. else
  807. ret = 1;
  808. rcu_read_unlock();
  809. }
  810. return ret;
  811. }
  812. EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);
  813. /**
  814. * ip6_tnl_xmit2 - encapsulate packet and send
  815. * @skb: the outgoing socket buffer
  816. * @dev: the outgoing tunnel device
  817. * @dsfield: dscp code for outer header
  818. * @fl: flow of tunneled packet
  819. * @encap_limit: encapsulation limit
  820. * @pmtu: Path MTU is stored if packet is too big
  821. *
  822. * Description:
  823. * Build new header and do some sanity checks on the packet before sending
  824. * it.
  825. *
  826. * Return:
  827. * 0 on success
  828. * -1 fail
  829. * %-EMSGSIZE message too big. return mtu in this case.
  830. **/
  831. static int ip6_tnl_xmit2(struct sk_buff *skb,
  832. struct net_device *dev,
  833. __u8 dsfield,
  834. struct flowi6 *fl6,
  835. int encap_limit,
  836. __u32 *pmtu)
  837. {
  838. struct ip6_tnl *t = netdev_priv(dev);
  839. struct net *net = t->net;
  840. struct net_device_stats *stats = &t->dev->stats;
  841. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  842. struct ipv6_tel_txoption opt;
  843. struct dst_entry *dst = NULL, *ndst = NULL;
  844. struct net_device *tdev;
  845. int mtu;
  846. unsigned int max_headroom = sizeof(struct ipv6hdr);
  847. u8 proto;
  848. int err = -1;
  849. /* NBMA tunnel */
  850. if (ipv6_addr_any(&t->parms.raddr)) {
  851. struct in6_addr *addr6;
  852. struct neighbour *neigh;
  853. int addr_type;
  854. if (!skb_dst(skb))
  855. goto tx_err_link_failure;
  856. neigh = dst_neigh_lookup(skb_dst(skb),
  857. &ipv6_hdr(skb)->daddr);
  858. if (!neigh)
  859. goto tx_err_link_failure;
  860. addr6 = (struct in6_addr *)&neigh->primary_key;
  861. addr_type = ipv6_addr_type(addr6);
  862. if (addr_type == IPV6_ADDR_ANY)
  863. addr6 = &ipv6_hdr(skb)->daddr;
  864. memcpy(&fl6->daddr, addr6, sizeof(fl6->daddr));
  865. neigh_release(neigh);
  866. } else if (!fl6->flowi6_mark)
  867. dst = ip6_tnl_dst_check(t);
  868. if (!ip6_tnl_xmit_ctl(t, &fl6->saddr, &fl6->daddr))
  869. goto tx_err_link_failure;
  870. if (!dst) {
  871. ndst = ip6_route_output(net, NULL, fl6);
  872. if (ndst->error)
  873. goto tx_err_link_failure;
  874. ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(fl6), NULL, 0);
  875. if (IS_ERR(ndst)) {
  876. err = PTR_ERR(ndst);
  877. ndst = NULL;
  878. goto tx_err_link_failure;
  879. }
  880. dst = ndst;
  881. }
  882. tdev = dst->dev;
  883. if (tdev == dev) {
  884. stats->collisions++;
  885. net_warn_ratelimited("%s: Local routing loop detected!\n",
  886. t->parms.name);
  887. goto tx_err_dst_release;
  888. }
  889. mtu = dst_mtu(dst) - sizeof(*ipv6h);
  890. if (encap_limit >= 0) {
  891. max_headroom += 8;
  892. mtu -= 8;
  893. }
  894. if (mtu < IPV6_MIN_MTU)
  895. mtu = IPV6_MIN_MTU;
  896. if (skb_dst(skb))
  897. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
  898. if (skb->len > mtu) {
  899. *pmtu = mtu;
  900. err = -EMSGSIZE;
  901. goto tx_err_dst_release;
  902. }
  903. skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
  904. /*
  905. * Okay, now see if we can stuff it in the buffer as-is.
  906. */
  907. max_headroom += LL_RESERVED_SPACE(tdev);
  908. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  909. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  910. struct sk_buff *new_skb;
  911. new_skb = skb_realloc_headroom(skb, max_headroom);
  912. if (!new_skb)
  913. goto tx_err_dst_release;
  914. if (skb->sk)
  915. skb_set_owner_w(new_skb, skb->sk);
  916. consume_skb(skb);
  917. skb = new_skb;
  918. }
  919. if (fl6->flowi6_mark) {
  920. skb_dst_set(skb, dst);
  921. ndst = NULL;
  922. } else {
  923. skb_dst_set_noref(skb, dst);
  924. }
  925. skb->transport_header = skb->network_header;
  926. proto = fl6->flowi6_proto;
  927. if (encap_limit >= 0) {
  928. init_tel_txopt(&opt, encap_limit);
  929. ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
  930. }
  931. if (likely(!skb->encapsulation)) {
  932. skb_reset_inner_headers(skb);
  933. skb->encapsulation = 1;
  934. }
  935. skb_push(skb, sizeof(struct ipv6hdr));
  936. skb_reset_network_header(skb);
  937. ipv6h = ipv6_hdr(skb);
  938. ip6_flow_hdr(ipv6h, INET_ECN_encapsulate(0, dsfield),
  939. ip6_make_flowlabel(net, skb, fl6->flowlabel, false));
  940. ipv6h->hop_limit = t->parms.hop_limit;
  941. ipv6h->nexthdr = proto;
  942. ipv6h->saddr = fl6->saddr;
  943. ipv6h->daddr = fl6->daddr;
  944. ip6tunnel_xmit(NULL, skb, dev);
  945. if (ndst)
  946. ip6_tnl_dst_store(t, ndst);
  947. return 0;
  948. tx_err_link_failure:
  949. stats->tx_carrier_errors++;
  950. dst_link_failure(skb);
  951. tx_err_dst_release:
  952. dst_release(ndst);
  953. return err;
  954. }
  955. static inline int
  956. ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  957. {
  958. struct ip6_tnl *t = netdev_priv(dev);
  959. const struct iphdr *iph = ip_hdr(skb);
  960. int encap_limit = -1;
  961. struct flowi6 fl6;
  962. __u8 dsfield;
  963. __u32 mtu;
  964. u8 tproto;
  965. int err;
  966. tproto = ACCESS_ONCE(t->parms.proto);
  967. if (tproto != IPPROTO_IPIP && tproto != 0)
  968. return -1;
  969. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  970. encap_limit = t->parms.encap_limit;
  971. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  972. fl6.flowi6_proto = IPPROTO_IPIP;
  973. dsfield = ipv4_get_dsfield(iph);
  974. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  975. fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
  976. & IPV6_TCLASS_MASK;
  977. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  978. fl6.flowi6_mark = skb->mark;
  979. err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
  980. if (err != 0) {
  981. /* XXX: send ICMP error even if DF is not set. */
  982. if (err == -EMSGSIZE)
  983. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  984. htonl(mtu));
  985. return -1;
  986. }
  987. return 0;
  988. }
  989. static inline int
  990. ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  991. {
  992. struct ip6_tnl *t = netdev_priv(dev);
  993. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  994. int encap_limit = -1;
  995. __u16 offset;
  996. struct flowi6 fl6;
  997. __u8 dsfield;
  998. __u32 mtu;
  999. u8 tproto;
  1000. int err;
  1001. tproto = ACCESS_ONCE(t->parms.proto);
  1002. if ((tproto != IPPROTO_IPV6 && tproto != 0) ||
  1003. ip6_tnl_addr_conflict(t, ipv6h))
  1004. return -1;
  1005. offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
  1006. if (offset > 0) {
  1007. struct ipv6_tlv_tnl_enc_lim *tel;
  1008. tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
  1009. if (tel->encap_limit == 0) {
  1010. icmpv6_send(skb, ICMPV6_PARAMPROB,
  1011. ICMPV6_HDR_FIELD, offset + 2);
  1012. return -1;
  1013. }
  1014. encap_limit = tel->encap_limit - 1;
  1015. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1016. encap_limit = t->parms.encap_limit;
  1017. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  1018. fl6.flowi6_proto = IPPROTO_IPV6;
  1019. dsfield = ipv6_get_dsfield(ipv6h);
  1020. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  1021. fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
  1022. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
  1023. fl6.flowlabel |= ip6_flowlabel(ipv6h);
  1024. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  1025. fl6.flowi6_mark = skb->mark;
  1026. err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
  1027. if (err != 0) {
  1028. if (err == -EMSGSIZE)
  1029. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  1030. return -1;
  1031. }
  1032. return 0;
  1033. }
  1034. static netdev_tx_t
  1035. ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  1036. {
  1037. struct ip6_tnl *t = netdev_priv(dev);
  1038. struct net_device_stats *stats = &t->dev->stats;
  1039. int ret;
  1040. switch (skb->protocol) {
  1041. case htons(ETH_P_IP):
  1042. ret = ip4ip6_tnl_xmit(skb, dev);
  1043. break;
  1044. case htons(ETH_P_IPV6):
  1045. ret = ip6ip6_tnl_xmit(skb, dev);
  1046. break;
  1047. default:
  1048. goto tx_err;
  1049. }
  1050. if (ret < 0)
  1051. goto tx_err;
  1052. return NETDEV_TX_OK;
  1053. tx_err:
  1054. stats->tx_errors++;
  1055. stats->tx_dropped++;
  1056. kfree_skb(skb);
  1057. return NETDEV_TX_OK;
  1058. }
  1059. static void ip6_tnl_link_config(struct ip6_tnl *t)
  1060. {
  1061. struct net_device *dev = t->dev;
  1062. struct __ip6_tnl_parm *p = &t->parms;
  1063. struct flowi6 *fl6 = &t->fl.u.ip6;
  1064. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  1065. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  1066. /* Set up flowi template */
  1067. fl6->saddr = p->laddr;
  1068. fl6->daddr = p->raddr;
  1069. fl6->flowi6_oif = p->link;
  1070. fl6->flowlabel = 0;
  1071. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  1072. fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  1073. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  1074. fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  1075. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
  1076. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  1077. if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
  1078. dev->flags |= IFF_POINTOPOINT;
  1079. else
  1080. dev->flags &= ~IFF_POINTOPOINT;
  1081. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  1082. int strict = (ipv6_addr_type(&p->raddr) &
  1083. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  1084. struct rt6_info *rt = rt6_lookup(t->net,
  1085. &p->raddr, &p->laddr,
  1086. p->link, strict);
  1087. if (!rt)
  1088. return;
  1089. if (rt->dst.dev) {
  1090. dev->hard_header_len = rt->dst.dev->hard_header_len +
  1091. sizeof(struct ipv6hdr);
  1092. dev->mtu = rt->dst.dev->mtu - sizeof(struct ipv6hdr);
  1093. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1094. dev->mtu -= 8;
  1095. if (dev->mtu < IPV6_MIN_MTU)
  1096. dev->mtu = IPV6_MIN_MTU;
  1097. }
  1098. ip6_rt_put(rt);
  1099. }
  1100. }
  1101. /**
  1102. * ip6_tnl_change - update the tunnel parameters
  1103. * @t: tunnel to be changed
  1104. * @p: tunnel configuration parameters
  1105. *
  1106. * Description:
  1107. * ip6_tnl_change() updates the tunnel parameters
  1108. **/
  1109. static int
  1110. ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
  1111. {
  1112. t->parms.laddr = p->laddr;
  1113. t->parms.raddr = p->raddr;
  1114. t->parms.flags = p->flags;
  1115. t->parms.hop_limit = p->hop_limit;
  1116. t->parms.encap_limit = p->encap_limit;
  1117. t->parms.flowinfo = p->flowinfo;
  1118. t->parms.link = p->link;
  1119. t->parms.proto = p->proto;
  1120. ip6_tnl_dst_reset(t);
  1121. ip6_tnl_link_config(t);
  1122. return 0;
  1123. }
  1124. static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
  1125. {
  1126. struct net *net = t->net;
  1127. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1128. int err;
  1129. ip6_tnl_unlink(ip6n, t);
  1130. synchronize_net();
  1131. err = ip6_tnl_change(t, p);
  1132. ip6_tnl_link(ip6n, t);
  1133. netdev_state_change(t->dev);
  1134. return err;
  1135. }
  1136. static int ip6_tnl0_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
  1137. {
  1138. /* for default tnl0 device allow to change only the proto */
  1139. t->parms.proto = p->proto;
  1140. netdev_state_change(t->dev);
  1141. return 0;
  1142. }
  1143. static void
  1144. ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u)
  1145. {
  1146. p->laddr = u->laddr;
  1147. p->raddr = u->raddr;
  1148. p->flags = u->flags;
  1149. p->hop_limit = u->hop_limit;
  1150. p->encap_limit = u->encap_limit;
  1151. p->flowinfo = u->flowinfo;
  1152. p->link = u->link;
  1153. p->proto = u->proto;
  1154. memcpy(p->name, u->name, sizeof(u->name));
  1155. }
  1156. static void
  1157. ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p)
  1158. {
  1159. u->laddr = p->laddr;
  1160. u->raddr = p->raddr;
  1161. u->flags = p->flags;
  1162. u->hop_limit = p->hop_limit;
  1163. u->encap_limit = p->encap_limit;
  1164. u->flowinfo = p->flowinfo;
  1165. u->link = p->link;
  1166. u->proto = p->proto;
  1167. memcpy(u->name, p->name, sizeof(u->name));
  1168. }
  1169. /**
  1170. * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
  1171. * @dev: virtual device associated with tunnel
  1172. * @ifr: parameters passed from userspace
  1173. * @cmd: command to be performed
  1174. *
  1175. * Description:
  1176. * ip6_tnl_ioctl() is used for managing IPv6 tunnels
  1177. * from userspace.
  1178. *
  1179. * The possible commands are the following:
  1180. * %SIOCGETTUNNEL: get tunnel parameters for device
  1181. * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
  1182. * %SIOCCHGTUNNEL: change tunnel parameters to those given
  1183. * %SIOCDELTUNNEL: delete tunnel
  1184. *
  1185. * The fallback device "ip6tnl0", created during module
  1186. * initialization, can be used for creating other tunnel devices.
  1187. *
  1188. * Return:
  1189. * 0 on success,
  1190. * %-EFAULT if unable to copy data to or from userspace,
  1191. * %-EPERM if current process hasn't %CAP_NET_ADMIN set
  1192. * %-EINVAL if passed tunnel parameters are invalid,
  1193. * %-EEXIST if changing a tunnel's parameters would cause a conflict
  1194. * %-ENODEV if attempting to change or delete a nonexisting device
  1195. **/
  1196. static int
  1197. ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1198. {
  1199. int err = 0;
  1200. struct ip6_tnl_parm p;
  1201. struct __ip6_tnl_parm p1;
  1202. struct ip6_tnl *t = netdev_priv(dev);
  1203. struct net *net = t->net;
  1204. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1205. switch (cmd) {
  1206. case SIOCGETTUNNEL:
  1207. if (dev == ip6n->fb_tnl_dev) {
  1208. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  1209. err = -EFAULT;
  1210. break;
  1211. }
  1212. ip6_tnl_parm_from_user(&p1, &p);
  1213. t = ip6_tnl_locate(net, &p1, 0);
  1214. if (IS_ERR(t))
  1215. t = netdev_priv(dev);
  1216. } else {
  1217. memset(&p, 0, sizeof(p));
  1218. }
  1219. ip6_tnl_parm_to_user(&p, &t->parms);
  1220. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) {
  1221. err = -EFAULT;
  1222. }
  1223. break;
  1224. case SIOCADDTUNNEL:
  1225. case SIOCCHGTUNNEL:
  1226. err = -EPERM;
  1227. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1228. break;
  1229. err = -EFAULT;
  1230. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1231. break;
  1232. err = -EINVAL;
  1233. if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
  1234. p.proto != 0)
  1235. break;
  1236. ip6_tnl_parm_from_user(&p1, &p);
  1237. t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL);
  1238. if (cmd == SIOCCHGTUNNEL) {
  1239. if (!IS_ERR(t)) {
  1240. if (t->dev != dev) {
  1241. err = -EEXIST;
  1242. break;
  1243. }
  1244. } else
  1245. t = netdev_priv(dev);
  1246. if (dev == ip6n->fb_tnl_dev)
  1247. err = ip6_tnl0_update(t, &p1);
  1248. else
  1249. err = ip6_tnl_update(t, &p1);
  1250. }
  1251. if (!IS_ERR(t)) {
  1252. err = 0;
  1253. ip6_tnl_parm_to_user(&p, &t->parms);
  1254. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  1255. err = -EFAULT;
  1256. } else {
  1257. err = PTR_ERR(t);
  1258. }
  1259. break;
  1260. case SIOCDELTUNNEL:
  1261. err = -EPERM;
  1262. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1263. break;
  1264. if (dev == ip6n->fb_tnl_dev) {
  1265. err = -EFAULT;
  1266. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1267. break;
  1268. err = -ENOENT;
  1269. ip6_tnl_parm_from_user(&p1, &p);
  1270. t = ip6_tnl_locate(net, &p1, 0);
  1271. if (IS_ERR(t))
  1272. break;
  1273. err = -EPERM;
  1274. if (t->dev == ip6n->fb_tnl_dev)
  1275. break;
  1276. dev = t->dev;
  1277. }
  1278. err = 0;
  1279. unregister_netdevice(dev);
  1280. break;
  1281. default:
  1282. err = -EINVAL;
  1283. }
  1284. return err;
  1285. }
  1286. /**
  1287. * ip6_tnl_change_mtu - change mtu manually for tunnel device
  1288. * @dev: virtual device associated with tunnel
  1289. * @new_mtu: the new mtu
  1290. *
  1291. * Return:
  1292. * 0 on success,
  1293. * %-EINVAL if mtu too small
  1294. **/
  1295. static int
  1296. ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
  1297. {
  1298. struct ip6_tnl *tnl = netdev_priv(dev);
  1299. if (tnl->parms.proto == IPPROTO_IPIP) {
  1300. if (new_mtu < 68)
  1301. return -EINVAL;
  1302. } else {
  1303. if (new_mtu < IPV6_MIN_MTU)
  1304. return -EINVAL;
  1305. }
  1306. if (new_mtu > 0xFFF8 - dev->hard_header_len)
  1307. return -EINVAL;
  1308. dev->mtu = new_mtu;
  1309. return 0;
  1310. }
  1311. int ip6_tnl_get_iflink(const struct net_device *dev)
  1312. {
  1313. struct ip6_tnl *t = netdev_priv(dev);
  1314. return t->parms.link;
  1315. }
  1316. EXPORT_SYMBOL(ip6_tnl_get_iflink);
  1317. static const struct net_device_ops ip6_tnl_netdev_ops = {
  1318. .ndo_init = ip6_tnl_dev_init,
  1319. .ndo_uninit = ip6_tnl_dev_uninit,
  1320. .ndo_start_xmit = ip6_tnl_xmit,
  1321. .ndo_do_ioctl = ip6_tnl_ioctl,
  1322. .ndo_change_mtu = ip6_tnl_change_mtu,
  1323. .ndo_get_stats = ip6_get_stats,
  1324. .ndo_get_iflink = ip6_tnl_get_iflink,
  1325. };
  1326. /**
  1327. * ip6_tnl_dev_setup - setup virtual tunnel device
  1328. * @dev: virtual device associated with tunnel
  1329. *
  1330. * Description:
  1331. * Initialize function pointers and device parameters
  1332. **/
  1333. static void ip6_tnl_dev_setup(struct net_device *dev)
  1334. {
  1335. struct ip6_tnl *t;
  1336. dev->netdev_ops = &ip6_tnl_netdev_ops;
  1337. dev->destructor = ip6_dev_free;
  1338. dev->type = ARPHRD_TUNNEL6;
  1339. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr);
  1340. dev->mtu = ETH_DATA_LEN - sizeof(struct ipv6hdr);
  1341. t = netdev_priv(dev);
  1342. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1343. dev->mtu -= 8;
  1344. dev->flags |= IFF_NOARP;
  1345. dev->addr_len = sizeof(struct in6_addr);
  1346. netif_keep_dst(dev);
  1347. /* This perm addr will be used as interface identifier by IPv6 */
  1348. dev->addr_assign_type = NET_ADDR_RANDOM;
  1349. eth_random_addr(dev->perm_addr);
  1350. }
  1351. /**
  1352. * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
  1353. * @dev: virtual device associated with tunnel
  1354. **/
  1355. static inline int
  1356. ip6_tnl_dev_init_gen(struct net_device *dev)
  1357. {
  1358. struct ip6_tnl *t = netdev_priv(dev);
  1359. t->dev = dev;
  1360. t->net = dev_net(dev);
  1361. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1362. if (!dev->tstats)
  1363. return -ENOMEM;
  1364. return 0;
  1365. }
  1366. /**
  1367. * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
  1368. * @dev: virtual device associated with tunnel
  1369. **/
  1370. static int ip6_tnl_dev_init(struct net_device *dev)
  1371. {
  1372. struct ip6_tnl *t = netdev_priv(dev);
  1373. int err = ip6_tnl_dev_init_gen(dev);
  1374. if (err)
  1375. return err;
  1376. ip6_tnl_link_config(t);
  1377. return 0;
  1378. }
  1379. /**
  1380. * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
  1381. * @dev: fallback device
  1382. *
  1383. * Return: 0
  1384. **/
  1385. static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
  1386. {
  1387. struct ip6_tnl *t = netdev_priv(dev);
  1388. struct net *net = dev_net(dev);
  1389. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1390. t->parms.proto = IPPROTO_IPV6;
  1391. dev_hold(dev);
  1392. rcu_assign_pointer(ip6n->tnls_wc[0], t);
  1393. return 0;
  1394. }
  1395. static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[])
  1396. {
  1397. u8 proto;
  1398. if (!data || !data[IFLA_IPTUN_PROTO])
  1399. return 0;
  1400. proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1401. if (proto != IPPROTO_IPV6 &&
  1402. proto != IPPROTO_IPIP &&
  1403. proto != 0)
  1404. return -EINVAL;
  1405. return 0;
  1406. }
  1407. static void ip6_tnl_netlink_parms(struct nlattr *data[],
  1408. struct __ip6_tnl_parm *parms)
  1409. {
  1410. memset(parms, 0, sizeof(*parms));
  1411. if (!data)
  1412. return;
  1413. if (data[IFLA_IPTUN_LINK])
  1414. parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
  1415. if (data[IFLA_IPTUN_LOCAL])
  1416. parms->laddr = nla_get_in6_addr(data[IFLA_IPTUN_LOCAL]);
  1417. if (data[IFLA_IPTUN_REMOTE])
  1418. parms->raddr = nla_get_in6_addr(data[IFLA_IPTUN_REMOTE]);
  1419. if (data[IFLA_IPTUN_TTL])
  1420. parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]);
  1421. if (data[IFLA_IPTUN_ENCAP_LIMIT])
  1422. parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]);
  1423. if (data[IFLA_IPTUN_FLOWINFO])
  1424. parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]);
  1425. if (data[IFLA_IPTUN_FLAGS])
  1426. parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]);
  1427. if (data[IFLA_IPTUN_PROTO])
  1428. parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1429. }
  1430. static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
  1431. struct nlattr *tb[], struct nlattr *data[])
  1432. {
  1433. struct net *net = dev_net(dev);
  1434. struct ip6_tnl *nt, *t;
  1435. nt = netdev_priv(dev);
  1436. ip6_tnl_netlink_parms(data, &nt->parms);
  1437. t = ip6_tnl_locate(net, &nt->parms, 0);
  1438. if (!IS_ERR(t))
  1439. return -EEXIST;
  1440. return ip6_tnl_create2(dev);
  1441. }
  1442. static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],
  1443. struct nlattr *data[])
  1444. {
  1445. struct ip6_tnl *t = netdev_priv(dev);
  1446. struct __ip6_tnl_parm p;
  1447. struct net *net = t->net;
  1448. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1449. if (dev == ip6n->fb_tnl_dev)
  1450. return -EINVAL;
  1451. ip6_tnl_netlink_parms(data, &p);
  1452. t = ip6_tnl_locate(net, &p, 0);
  1453. if (!IS_ERR(t)) {
  1454. if (t->dev != dev)
  1455. return -EEXIST;
  1456. } else
  1457. t = netdev_priv(dev);
  1458. return ip6_tnl_update(t, &p);
  1459. }
  1460. static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head)
  1461. {
  1462. struct net *net = dev_net(dev);
  1463. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1464. if (dev != ip6n->fb_tnl_dev)
  1465. unregister_netdevice_queue(dev, head);
  1466. }
  1467. static size_t ip6_tnl_get_size(const struct net_device *dev)
  1468. {
  1469. return
  1470. /* IFLA_IPTUN_LINK */
  1471. nla_total_size(4) +
  1472. /* IFLA_IPTUN_LOCAL */
  1473. nla_total_size(sizeof(struct in6_addr)) +
  1474. /* IFLA_IPTUN_REMOTE */
  1475. nla_total_size(sizeof(struct in6_addr)) +
  1476. /* IFLA_IPTUN_TTL */
  1477. nla_total_size(1) +
  1478. /* IFLA_IPTUN_ENCAP_LIMIT */
  1479. nla_total_size(1) +
  1480. /* IFLA_IPTUN_FLOWINFO */
  1481. nla_total_size(4) +
  1482. /* IFLA_IPTUN_FLAGS */
  1483. nla_total_size(4) +
  1484. /* IFLA_IPTUN_PROTO */
  1485. nla_total_size(1) +
  1486. 0;
  1487. }
  1488. static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1489. {
  1490. struct ip6_tnl *tunnel = netdev_priv(dev);
  1491. struct __ip6_tnl_parm *parm = &tunnel->parms;
  1492. if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
  1493. nla_put_in6_addr(skb, IFLA_IPTUN_LOCAL, &parm->laddr) ||
  1494. nla_put_in6_addr(skb, IFLA_IPTUN_REMOTE, &parm->raddr) ||
  1495. nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) ||
  1496. nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) ||
  1497. nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) ||
  1498. nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) ||
  1499. nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto))
  1500. goto nla_put_failure;
  1501. return 0;
  1502. nla_put_failure:
  1503. return -EMSGSIZE;
  1504. }
  1505. struct net *ip6_tnl_get_link_net(const struct net_device *dev)
  1506. {
  1507. struct ip6_tnl *tunnel = netdev_priv(dev);
  1508. return tunnel->net;
  1509. }
  1510. EXPORT_SYMBOL(ip6_tnl_get_link_net);
  1511. static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = {
  1512. [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
  1513. [IFLA_IPTUN_LOCAL] = { .len = sizeof(struct in6_addr) },
  1514. [IFLA_IPTUN_REMOTE] = { .len = sizeof(struct in6_addr) },
  1515. [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
  1516. [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NLA_U8 },
  1517. [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32 },
  1518. [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 },
  1519. [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
  1520. };
  1521. static struct rtnl_link_ops ip6_link_ops __read_mostly = {
  1522. .kind = "ip6tnl",
  1523. .maxtype = IFLA_IPTUN_MAX,
  1524. .policy = ip6_tnl_policy,
  1525. .priv_size = sizeof(struct ip6_tnl),
  1526. .setup = ip6_tnl_dev_setup,
  1527. .validate = ip6_tnl_validate,
  1528. .newlink = ip6_tnl_newlink,
  1529. .changelink = ip6_tnl_changelink,
  1530. .dellink = ip6_tnl_dellink,
  1531. .get_size = ip6_tnl_get_size,
  1532. .fill_info = ip6_tnl_fill_info,
  1533. .get_link_net = ip6_tnl_get_link_net,
  1534. };
  1535. static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
  1536. .handler = ip4ip6_rcv,
  1537. .err_handler = ip4ip6_err,
  1538. .priority = 1,
  1539. };
  1540. static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
  1541. .handler = ip6ip6_rcv,
  1542. .err_handler = ip6ip6_err,
  1543. .priority = 1,
  1544. };
  1545. static void __net_exit ip6_tnl_destroy_tunnels(struct net *net)
  1546. {
  1547. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1548. struct net_device *dev, *aux;
  1549. int h;
  1550. struct ip6_tnl *t;
  1551. LIST_HEAD(list);
  1552. for_each_netdev_safe(net, dev, aux)
  1553. if (dev->rtnl_link_ops == &ip6_link_ops)
  1554. unregister_netdevice_queue(dev, &list);
  1555. for (h = 0; h < HASH_SIZE; h++) {
  1556. t = rtnl_dereference(ip6n->tnls_r_l[h]);
  1557. while (t) {
  1558. /* If dev is in the same netns, it has already
  1559. * been added to the list by the previous loop.
  1560. */
  1561. if (!net_eq(dev_net(t->dev), net))
  1562. unregister_netdevice_queue(t->dev, &list);
  1563. t = rtnl_dereference(t->next);
  1564. }
  1565. }
  1566. unregister_netdevice_many(&list);
  1567. }
  1568. static int __net_init ip6_tnl_init_net(struct net *net)
  1569. {
  1570. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1571. struct ip6_tnl *t = NULL;
  1572. int err;
  1573. ip6n->tnls[0] = ip6n->tnls_wc;
  1574. ip6n->tnls[1] = ip6n->tnls_r_l;
  1575. err = -ENOMEM;
  1576. ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
  1577. NET_NAME_UNKNOWN, ip6_tnl_dev_setup);
  1578. if (!ip6n->fb_tnl_dev)
  1579. goto err_alloc_dev;
  1580. dev_net_set(ip6n->fb_tnl_dev, net);
  1581. ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_link_ops;
  1582. /* FB netdevice is special: we have one, and only one per netns.
  1583. * Allowing to move it to another netns is clearly unsafe.
  1584. */
  1585. ip6n->fb_tnl_dev->features |= NETIF_F_NETNS_LOCAL;
  1586. err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
  1587. if (err < 0)
  1588. goto err_register;
  1589. err = register_netdev(ip6n->fb_tnl_dev);
  1590. if (err < 0)
  1591. goto err_register;
  1592. t = netdev_priv(ip6n->fb_tnl_dev);
  1593. strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
  1594. return 0;
  1595. err_register:
  1596. ip6_dev_free(ip6n->fb_tnl_dev);
  1597. err_alloc_dev:
  1598. return err;
  1599. }
  1600. static void __net_exit ip6_tnl_exit_net(struct net *net)
  1601. {
  1602. rtnl_lock();
  1603. ip6_tnl_destroy_tunnels(net);
  1604. rtnl_unlock();
  1605. }
  1606. static struct pernet_operations ip6_tnl_net_ops = {
  1607. .init = ip6_tnl_init_net,
  1608. .exit = ip6_tnl_exit_net,
  1609. .id = &ip6_tnl_net_id,
  1610. .size = sizeof(struct ip6_tnl_net),
  1611. };
  1612. /**
  1613. * ip6_tunnel_init - register protocol and reserve needed resources
  1614. *
  1615. * Return: 0 on success
  1616. **/
  1617. static int __init ip6_tunnel_init(void)
  1618. {
  1619. int err;
  1620. err = register_pernet_device(&ip6_tnl_net_ops);
  1621. if (err < 0)
  1622. goto out_pernet;
  1623. err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
  1624. if (err < 0) {
  1625. pr_err("%s: can't register ip4ip6\n", __func__);
  1626. goto out_ip4ip6;
  1627. }
  1628. err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
  1629. if (err < 0) {
  1630. pr_err("%s: can't register ip6ip6\n", __func__);
  1631. goto out_ip6ip6;
  1632. }
  1633. err = rtnl_link_register(&ip6_link_ops);
  1634. if (err < 0)
  1635. goto rtnl_link_failed;
  1636. return 0;
  1637. rtnl_link_failed:
  1638. xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
  1639. out_ip6ip6:
  1640. xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
  1641. out_ip4ip6:
  1642. unregister_pernet_device(&ip6_tnl_net_ops);
  1643. out_pernet:
  1644. return err;
  1645. }
  1646. /**
  1647. * ip6_tunnel_cleanup - free resources and unregister protocol
  1648. **/
  1649. static void __exit ip6_tunnel_cleanup(void)
  1650. {
  1651. rtnl_link_unregister(&ip6_link_ops);
  1652. if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
  1653. pr_info("%s: can't deregister ip4ip6\n", __func__);
  1654. if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
  1655. pr_info("%s: can't deregister ip6ip6\n", __func__);
  1656. unregister_pernet_device(&ip6_tnl_net_ops);
  1657. }
  1658. module_init(ip6_tunnel_init);
  1659. module_exit(ip6_tunnel_cleanup);