ip6_tunnel.c 45 KB

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