ip6_tunnel.c 46 KB

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