ip6_tunnel.c 44 KB

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