ip6_tunnel.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334
  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 <linux/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. #include <net/dst_metadata.h>
  57. MODULE_AUTHOR("Ville Nuorvala");
  58. MODULE_DESCRIPTION("IPv6 tunneling device");
  59. MODULE_LICENSE("GPL");
  60. MODULE_ALIAS_RTNL_LINK("ip6tnl");
  61. MODULE_ALIAS_NETDEV("ip6tnl0");
  62. #define IP6_TUNNEL_HASH_SIZE_SHIFT 5
  63. #define IP6_TUNNEL_HASH_SIZE (1 << IP6_TUNNEL_HASH_SIZE_SHIFT)
  64. static bool log_ecn_error = true;
  65. module_param(log_ecn_error, bool, 0644);
  66. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  67. static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
  68. {
  69. u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
  70. return hash_32(hash, IP6_TUNNEL_HASH_SIZE_SHIFT);
  71. }
  72. static int ip6_tnl_dev_init(struct net_device *dev);
  73. static void ip6_tnl_dev_setup(struct net_device *dev);
  74. static struct rtnl_link_ops ip6_link_ops __read_mostly;
  75. static unsigned int ip6_tnl_net_id __read_mostly;
  76. struct ip6_tnl_net {
  77. /* the IPv6 tunnel fallback device */
  78. struct net_device *fb_tnl_dev;
  79. /* lists for storing tunnels in use */
  80. struct ip6_tnl __rcu *tnls_r_l[IP6_TUNNEL_HASH_SIZE];
  81. struct ip6_tnl __rcu *tnls_wc[1];
  82. struct ip6_tnl __rcu **tnls[2];
  83. struct ip6_tnl __rcu *collect_md_tun;
  84. };
  85. static struct net_device_stats *ip6_get_stats(struct net_device *dev)
  86. {
  87. struct pcpu_sw_netstats tmp, sum = { 0 };
  88. int i;
  89. for_each_possible_cpu(i) {
  90. unsigned int start;
  91. const struct pcpu_sw_netstats *tstats =
  92. per_cpu_ptr(dev->tstats, i);
  93. do {
  94. start = u64_stats_fetch_begin_irq(&tstats->syncp);
  95. tmp.rx_packets = tstats->rx_packets;
  96. tmp.rx_bytes = tstats->rx_bytes;
  97. tmp.tx_packets = tstats->tx_packets;
  98. tmp.tx_bytes = tstats->tx_bytes;
  99. } while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
  100. sum.rx_packets += tmp.rx_packets;
  101. sum.rx_bytes += tmp.rx_bytes;
  102. sum.tx_packets += tmp.tx_packets;
  103. sum.tx_bytes += tmp.tx_bytes;
  104. }
  105. dev->stats.rx_packets = sum.rx_packets;
  106. dev->stats.rx_bytes = sum.rx_bytes;
  107. dev->stats.tx_packets = sum.tx_packets;
  108. dev->stats.tx_bytes = sum.tx_bytes;
  109. return &dev->stats;
  110. }
  111. /**
  112. * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
  113. * @remote: the address of the tunnel exit-point
  114. * @local: the address of the tunnel entry-point
  115. *
  116. * Return:
  117. * tunnel matching given end-points if found,
  118. * else fallback tunnel if its device is up,
  119. * else %NULL
  120. **/
  121. #define for_each_ip6_tunnel_rcu(start) \
  122. for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
  123. static struct ip6_tnl *
  124. ip6_tnl_lookup(struct net *net, const struct in6_addr *remote, const struct in6_addr *local)
  125. {
  126. unsigned int hash = HASH(remote, local);
  127. struct ip6_tnl *t;
  128. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  129. struct in6_addr any;
  130. for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  131. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  132. ipv6_addr_equal(remote, &t->parms.raddr) &&
  133. (t->dev->flags & IFF_UP))
  134. return t;
  135. }
  136. memset(&any, 0, sizeof(any));
  137. hash = HASH(&any, local);
  138. for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  139. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  140. ipv6_addr_any(&t->parms.raddr) &&
  141. (t->dev->flags & IFF_UP))
  142. return t;
  143. }
  144. hash = HASH(remote, &any);
  145. for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  146. if (ipv6_addr_equal(remote, &t->parms.raddr) &&
  147. ipv6_addr_any(&t->parms.laddr) &&
  148. (t->dev->flags & IFF_UP))
  149. return t;
  150. }
  151. t = rcu_dereference(ip6n->collect_md_tun);
  152. if (t && t->dev->flags & IFF_UP)
  153. return t;
  154. t = rcu_dereference(ip6n->tnls_wc[0]);
  155. if (t && (t->dev->flags & IFF_UP))
  156. return t;
  157. return NULL;
  158. }
  159. /**
  160. * ip6_tnl_bucket - get head of list matching given tunnel parameters
  161. * @p: parameters containing tunnel end-points
  162. *
  163. * Description:
  164. * ip6_tnl_bucket() returns the head of the list matching the
  165. * &struct in6_addr entries laddr and raddr in @p.
  166. *
  167. * Return: head of IPv6 tunnel list
  168. **/
  169. static struct ip6_tnl __rcu **
  170. ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p)
  171. {
  172. const struct in6_addr *remote = &p->raddr;
  173. const struct in6_addr *local = &p->laddr;
  174. unsigned int h = 0;
  175. int prio = 0;
  176. if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
  177. prio = 1;
  178. h = HASH(remote, local);
  179. }
  180. return &ip6n->tnls[prio][h];
  181. }
  182. /**
  183. * ip6_tnl_link - add tunnel to hash table
  184. * @t: tunnel to be added
  185. **/
  186. static void
  187. ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
  188. {
  189. struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms);
  190. if (t->parms.collect_md)
  191. rcu_assign_pointer(ip6n->collect_md_tun, t);
  192. rcu_assign_pointer(t->next , rtnl_dereference(*tp));
  193. rcu_assign_pointer(*tp, t);
  194. }
  195. /**
  196. * ip6_tnl_unlink - remove tunnel from hash table
  197. * @t: tunnel to be removed
  198. **/
  199. static void
  200. ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
  201. {
  202. struct ip6_tnl __rcu **tp;
  203. struct ip6_tnl *iter;
  204. if (t->parms.collect_md)
  205. rcu_assign_pointer(ip6n->collect_md_tun, NULL);
  206. for (tp = ip6_tnl_bucket(ip6n, &t->parms);
  207. (iter = rtnl_dereference(*tp)) != NULL;
  208. tp = &iter->next) {
  209. if (t == iter) {
  210. rcu_assign_pointer(*tp, t->next);
  211. break;
  212. }
  213. }
  214. }
  215. static void ip6_dev_free(struct net_device *dev)
  216. {
  217. struct ip6_tnl *t = netdev_priv(dev);
  218. gro_cells_destroy(&t->gro_cells);
  219. dst_cache_destroy(&t->dst_cache);
  220. free_percpu(dev->tstats);
  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. dev->rtnl_link_ops = &ip6_link_ops;
  230. err = register_netdevice(dev);
  231. if (err < 0)
  232. goto out;
  233. strcpy(t->parms.name, dev->name);
  234. dev_hold(dev);
  235. ip6_tnl_link(ip6n, t);
  236. return 0;
  237. out:
  238. return err;
  239. }
  240. /**
  241. * ip6_tnl_create - create a new tunnel
  242. * @p: tunnel parameters
  243. * @pt: pointer to new tunnel
  244. *
  245. * Description:
  246. * Create tunnel matching given parameters.
  247. *
  248. * Return:
  249. * created tunnel or error pointer
  250. **/
  251. static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
  252. {
  253. struct net_device *dev;
  254. struct ip6_tnl *t;
  255. char name[IFNAMSIZ];
  256. int err = -E2BIG;
  257. if (p->name[0]) {
  258. if (!dev_valid_name(p->name))
  259. goto failed;
  260. strlcpy(name, p->name, IFNAMSIZ);
  261. } else {
  262. sprintf(name, "ip6tnl%%d");
  263. }
  264. err = -ENOMEM;
  265. dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
  266. ip6_tnl_dev_setup);
  267. if (!dev)
  268. goto failed;
  269. dev_net_set(dev, net);
  270. t = netdev_priv(dev);
  271. t->parms = *p;
  272. t->net = dev_net(dev);
  273. err = ip6_tnl_create2(dev);
  274. if (err < 0)
  275. goto failed_free;
  276. return t;
  277. failed_free:
  278. free_netdev(dev);
  279. failed:
  280. return ERR_PTR(err);
  281. }
  282. /**
  283. * ip6_tnl_locate - find or create tunnel matching given parameters
  284. * @p: tunnel parameters
  285. * @create: != 0 if allowed to create new tunnel if no match found
  286. *
  287. * Description:
  288. * ip6_tnl_locate() first tries to locate an existing tunnel
  289. * based on @parms. If this is unsuccessful, but @create is set a new
  290. * tunnel device is created and registered for use.
  291. *
  292. * Return:
  293. * matching tunnel or error pointer
  294. **/
  295. static struct ip6_tnl *ip6_tnl_locate(struct net *net,
  296. struct __ip6_tnl_parm *p, int create)
  297. {
  298. const struct in6_addr *remote = &p->raddr;
  299. const struct in6_addr *local = &p->laddr;
  300. struct ip6_tnl __rcu **tp;
  301. struct ip6_tnl *t;
  302. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  303. for (tp = ip6_tnl_bucket(ip6n, p);
  304. (t = rtnl_dereference(*tp)) != NULL;
  305. tp = &t->next) {
  306. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  307. ipv6_addr_equal(remote, &t->parms.raddr)) {
  308. if (create)
  309. return ERR_PTR(-EEXIST);
  310. return t;
  311. }
  312. }
  313. if (!create)
  314. return ERR_PTR(-ENODEV);
  315. return ip6_tnl_create(net, p);
  316. }
  317. /**
  318. * ip6_tnl_dev_uninit - tunnel device uninitializer
  319. * @dev: the device to be destroyed
  320. *
  321. * Description:
  322. * ip6_tnl_dev_uninit() removes tunnel from its list
  323. **/
  324. static void
  325. ip6_tnl_dev_uninit(struct net_device *dev)
  326. {
  327. struct ip6_tnl *t = netdev_priv(dev);
  328. struct net *net = t->net;
  329. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  330. if (dev == ip6n->fb_tnl_dev)
  331. RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
  332. else
  333. ip6_tnl_unlink(ip6n, t);
  334. dst_cache_reset(&t->dst_cache);
  335. dev_put(dev);
  336. }
  337. /**
  338. * parse_tvl_tnl_enc_lim - handle encapsulation limit option
  339. * @skb: received socket buffer
  340. *
  341. * Return:
  342. * 0 if none was found,
  343. * else index to encapsulation limit
  344. **/
  345. __u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw)
  346. {
  347. const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)raw;
  348. unsigned int nhoff = raw - skb->data;
  349. unsigned int off = nhoff + sizeof(*ipv6h);
  350. u8 next, nexthdr = ipv6h->nexthdr;
  351. while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
  352. struct ipv6_opt_hdr *hdr;
  353. u16 optlen;
  354. if (!pskb_may_pull(skb, off + sizeof(*hdr)))
  355. break;
  356. hdr = (struct ipv6_opt_hdr *)(skb->data + off);
  357. if (nexthdr == NEXTHDR_FRAGMENT) {
  358. struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
  359. if (frag_hdr->frag_off)
  360. break;
  361. optlen = 8;
  362. } else if (nexthdr == NEXTHDR_AUTH) {
  363. optlen = (hdr->hdrlen + 2) << 2;
  364. } else {
  365. optlen = ipv6_optlen(hdr);
  366. }
  367. /* cache hdr->nexthdr, since pskb_may_pull() might
  368. * invalidate hdr
  369. */
  370. next = hdr->nexthdr;
  371. if (nexthdr == NEXTHDR_DEST) {
  372. u16 i = 2;
  373. /* Remember : hdr is no longer valid at this point. */
  374. if (!pskb_may_pull(skb, off + optlen))
  375. break;
  376. while (1) {
  377. struct ipv6_tlv_tnl_enc_lim *tel;
  378. /* No more room for encapsulation limit */
  379. if (i + sizeof(*tel) > optlen)
  380. break;
  381. tel = (struct ipv6_tlv_tnl_enc_lim *)(skb->data + off + i);
  382. /* return index of option if found and valid */
  383. if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
  384. tel->length == 1)
  385. return i + off - nhoff;
  386. /* else jump to next option */
  387. if (tel->type)
  388. i += tel->length + 2;
  389. else
  390. i++;
  391. }
  392. }
  393. nexthdr = next;
  394. off += optlen;
  395. }
  396. return 0;
  397. }
  398. EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim);
  399. /**
  400. * ip6_tnl_err - tunnel error handler
  401. *
  402. * Description:
  403. * ip6_tnl_err() should handle errors in the tunnel according
  404. * to the specifications in RFC 2473.
  405. **/
  406. static int
  407. ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
  408. u8 *type, u8 *code, int *msg, __u32 *info, int offset)
  409. {
  410. const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)skb->data;
  411. struct net *net = dev_net(skb->dev);
  412. u8 rel_type = ICMPV6_DEST_UNREACH;
  413. u8 rel_code = ICMPV6_ADDR_UNREACH;
  414. __u32 rel_info = 0;
  415. struct ip6_tnl *t;
  416. int err = -ENOENT;
  417. int rel_msg = 0;
  418. u8 tproto;
  419. __u16 len;
  420. /* If the packet doesn't contain the original IPv6 header we are
  421. in trouble since we might need the source address for further
  422. processing of the error. */
  423. rcu_read_lock();
  424. t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr, &ipv6h->saddr);
  425. if (!t)
  426. goto out;
  427. tproto = READ_ONCE(t->parms.proto);
  428. if (tproto != ipproto && tproto != 0)
  429. goto out;
  430. err = 0;
  431. switch (*type) {
  432. struct ipv6_tlv_tnl_enc_lim *tel;
  433. __u32 mtu, teli;
  434. case ICMPV6_DEST_UNREACH:
  435. net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
  436. t->parms.name);
  437. rel_msg = 1;
  438. break;
  439. case ICMPV6_TIME_EXCEED:
  440. if ((*code) == ICMPV6_EXC_HOPLIMIT) {
  441. net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
  442. t->parms.name);
  443. rel_msg = 1;
  444. }
  445. break;
  446. case ICMPV6_PARAMPROB:
  447. teli = 0;
  448. if ((*code) == ICMPV6_HDR_FIELD)
  449. teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
  450. if (teli && teli == *info - 2) {
  451. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
  452. if (tel->encap_limit == 0) {
  453. net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
  454. t->parms.name);
  455. rel_msg = 1;
  456. }
  457. } else {
  458. net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
  459. t->parms.name);
  460. }
  461. break;
  462. case ICMPV6_PKT_TOOBIG:
  463. ip6_update_pmtu(skb, net, htonl(*info), 0, 0,
  464. sock_net_uid(net, NULL));
  465. mtu = *info - offset;
  466. if (mtu < IPV6_MIN_MTU)
  467. mtu = IPV6_MIN_MTU;
  468. len = sizeof(*ipv6h) + ntohs(ipv6h->payload_len);
  469. if (len > mtu) {
  470. rel_type = ICMPV6_PKT_TOOBIG;
  471. rel_code = 0;
  472. rel_info = mtu;
  473. rel_msg = 1;
  474. }
  475. break;
  476. case NDISC_REDIRECT:
  477. ip6_redirect(skb, net, skb->dev->ifindex, 0,
  478. sock_net_uid(net, NULL));
  479. break;
  480. }
  481. *type = rel_type;
  482. *code = rel_code;
  483. *info = rel_info;
  484. *msg = rel_msg;
  485. out:
  486. rcu_read_unlock();
  487. return err;
  488. }
  489. static int
  490. ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  491. u8 type, u8 code, int offset, __be32 info)
  492. {
  493. __u32 rel_info = ntohl(info);
  494. const struct iphdr *eiph;
  495. struct sk_buff *skb2;
  496. int err, rel_msg = 0;
  497. u8 rel_type = type;
  498. u8 rel_code = code;
  499. struct rtable *rt;
  500. struct flowi4 fl4;
  501. err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
  502. &rel_msg, &rel_info, offset);
  503. if (err < 0)
  504. return err;
  505. if (rel_msg == 0)
  506. return 0;
  507. switch (rel_type) {
  508. case ICMPV6_DEST_UNREACH:
  509. if (rel_code != ICMPV6_ADDR_UNREACH)
  510. return 0;
  511. rel_type = ICMP_DEST_UNREACH;
  512. rel_code = ICMP_HOST_UNREACH;
  513. break;
  514. case ICMPV6_PKT_TOOBIG:
  515. if (rel_code != 0)
  516. return 0;
  517. rel_type = ICMP_DEST_UNREACH;
  518. rel_code = ICMP_FRAG_NEEDED;
  519. break;
  520. default:
  521. return 0;
  522. }
  523. if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
  524. return 0;
  525. skb2 = skb_clone(skb, GFP_ATOMIC);
  526. if (!skb2)
  527. return 0;
  528. skb_dst_drop(skb2);
  529. skb_pull(skb2, offset);
  530. skb_reset_network_header(skb2);
  531. eiph = ip_hdr(skb2);
  532. /* Try to guess incoming interface */
  533. rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL, eiph->saddr,
  534. 0, 0, 0, IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
  535. if (IS_ERR(rt))
  536. goto out;
  537. skb2->dev = rt->dst.dev;
  538. ip_rt_put(rt);
  539. /* route "incoming" packet */
  540. if (rt->rt_flags & RTCF_LOCAL) {
  541. rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
  542. eiph->daddr, eiph->saddr, 0, 0,
  543. IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
  544. if (IS_ERR(rt) || rt->dst.dev->type != ARPHRD_TUNNEL) {
  545. if (!IS_ERR(rt))
  546. ip_rt_put(rt);
  547. goto out;
  548. }
  549. skb_dst_set(skb2, &rt->dst);
  550. } else {
  551. if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
  552. skb2->dev) ||
  553. skb_dst(skb2)->dev->type != ARPHRD_TUNNEL)
  554. goto out;
  555. }
  556. /* change mtu on this route */
  557. if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
  558. if (rel_info > dst_mtu(skb_dst(skb2)))
  559. goto out;
  560. skb_dst_update_pmtu(skb2, rel_info);
  561. }
  562. icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
  563. out:
  564. kfree_skb(skb2);
  565. return 0;
  566. }
  567. static int
  568. ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  569. u8 type, u8 code, int offset, __be32 info)
  570. {
  571. __u32 rel_info = ntohl(info);
  572. int err, rel_msg = 0;
  573. u8 rel_type = type;
  574. u8 rel_code = code;
  575. err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
  576. &rel_msg, &rel_info, offset);
  577. if (err < 0)
  578. return err;
  579. if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
  580. struct rt6_info *rt;
  581. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  582. if (!skb2)
  583. return 0;
  584. skb_dst_drop(skb2);
  585. skb_pull(skb2, offset);
  586. skb_reset_network_header(skb2);
  587. /* Try to guess incoming interface */
  588. rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr,
  589. NULL, 0, skb2, 0);
  590. if (rt && rt->dst.dev)
  591. skb2->dev = rt->dst.dev;
  592. icmpv6_send(skb2, rel_type, rel_code, rel_info);
  593. ip6_rt_put(rt);
  594. kfree_skb(skb2);
  595. }
  596. return 0;
  597. }
  598. static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
  599. const struct ipv6hdr *ipv6h,
  600. struct sk_buff *skb)
  601. {
  602. __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
  603. if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
  604. ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
  605. return IP6_ECN_decapsulate(ipv6h, skb);
  606. }
  607. static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
  608. const struct ipv6hdr *ipv6h,
  609. struct sk_buff *skb)
  610. {
  611. if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
  612. ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
  613. return IP6_ECN_decapsulate(ipv6h, skb);
  614. }
  615. __u32 ip6_tnl_get_cap(struct ip6_tnl *t,
  616. const struct in6_addr *laddr,
  617. const struct in6_addr *raddr)
  618. {
  619. struct __ip6_tnl_parm *p = &t->parms;
  620. int ltype = ipv6_addr_type(laddr);
  621. int rtype = ipv6_addr_type(raddr);
  622. __u32 flags = 0;
  623. if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) {
  624. flags = IP6_TNL_F_CAP_PER_PACKET;
  625. } else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
  626. rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
  627. !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
  628. (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
  629. if (ltype&IPV6_ADDR_UNICAST)
  630. flags |= IP6_TNL_F_CAP_XMIT;
  631. if (rtype&IPV6_ADDR_UNICAST)
  632. flags |= IP6_TNL_F_CAP_RCV;
  633. }
  634. return flags;
  635. }
  636. EXPORT_SYMBOL(ip6_tnl_get_cap);
  637. /* called with rcu_read_lock() */
  638. int ip6_tnl_rcv_ctl(struct ip6_tnl *t,
  639. const struct in6_addr *laddr,
  640. const struct in6_addr *raddr)
  641. {
  642. struct __ip6_tnl_parm *p = &t->parms;
  643. int ret = 0;
  644. struct net *net = t->net;
  645. if ((p->flags & IP6_TNL_F_CAP_RCV) ||
  646. ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
  647. (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) {
  648. struct net_device *ldev = NULL;
  649. if (p->link)
  650. ldev = dev_get_by_index_rcu(net, p->link);
  651. if ((ipv6_addr_is_multicast(laddr) ||
  652. likely(ipv6_chk_addr_and_flags(net, laddr, ldev, false,
  653. 0, IFA_F_TENTATIVE))) &&
  654. ((p->flags & IP6_TNL_F_ALLOW_LOCAL_REMOTE) ||
  655. likely(!ipv6_chk_addr_and_flags(net, raddr, ldev, true,
  656. 0, IFA_F_TENTATIVE))))
  657. ret = 1;
  658. }
  659. return ret;
  660. }
  661. EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl);
  662. static int __ip6_tnl_rcv(struct ip6_tnl *tunnel, struct sk_buff *skb,
  663. const struct tnl_ptk_info *tpi,
  664. struct metadata_dst *tun_dst,
  665. int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
  666. const struct ipv6hdr *ipv6h,
  667. struct sk_buff *skb),
  668. bool log_ecn_err)
  669. {
  670. struct pcpu_sw_netstats *tstats;
  671. const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  672. int err;
  673. if ((!(tpi->flags & TUNNEL_CSUM) &&
  674. (tunnel->parms.i_flags & TUNNEL_CSUM)) ||
  675. ((tpi->flags & TUNNEL_CSUM) &&
  676. !(tunnel->parms.i_flags & TUNNEL_CSUM))) {
  677. tunnel->dev->stats.rx_crc_errors++;
  678. tunnel->dev->stats.rx_errors++;
  679. goto drop;
  680. }
  681. if (tunnel->parms.i_flags & TUNNEL_SEQ) {
  682. if (!(tpi->flags & TUNNEL_SEQ) ||
  683. (tunnel->i_seqno &&
  684. (s32)(ntohl(tpi->seq) - tunnel->i_seqno) < 0)) {
  685. tunnel->dev->stats.rx_fifo_errors++;
  686. tunnel->dev->stats.rx_errors++;
  687. goto drop;
  688. }
  689. tunnel->i_seqno = ntohl(tpi->seq) + 1;
  690. }
  691. skb->protocol = tpi->proto;
  692. /* Warning: All skb pointers will be invalidated! */
  693. if (tunnel->dev->type == ARPHRD_ETHER) {
  694. if (!pskb_may_pull(skb, ETH_HLEN)) {
  695. tunnel->dev->stats.rx_length_errors++;
  696. tunnel->dev->stats.rx_errors++;
  697. goto drop;
  698. }
  699. ipv6h = ipv6_hdr(skb);
  700. skb->protocol = eth_type_trans(skb, tunnel->dev);
  701. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
  702. } else {
  703. skb->dev = tunnel->dev;
  704. }
  705. skb_reset_network_header(skb);
  706. memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
  707. __skb_tunnel_rx(skb, tunnel->dev, tunnel->net);
  708. err = dscp_ecn_decapsulate(tunnel, ipv6h, skb);
  709. if (unlikely(err)) {
  710. if (log_ecn_err)
  711. net_info_ratelimited("non-ECT from %pI6 with DS=%#x\n",
  712. &ipv6h->saddr,
  713. ipv6_get_dsfield(ipv6h));
  714. if (err > 1) {
  715. ++tunnel->dev->stats.rx_frame_errors;
  716. ++tunnel->dev->stats.rx_errors;
  717. goto drop;
  718. }
  719. }
  720. tstats = this_cpu_ptr(tunnel->dev->tstats);
  721. u64_stats_update_begin(&tstats->syncp);
  722. tstats->rx_packets++;
  723. tstats->rx_bytes += skb->len;
  724. u64_stats_update_end(&tstats->syncp);
  725. skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(tunnel->dev)));
  726. if (tun_dst)
  727. skb_dst_set(skb, (struct dst_entry *)tun_dst);
  728. gro_cells_receive(&tunnel->gro_cells, skb);
  729. return 0;
  730. drop:
  731. if (tun_dst)
  732. dst_release((struct dst_entry *)tun_dst);
  733. kfree_skb(skb);
  734. return 0;
  735. }
  736. int ip6_tnl_rcv(struct ip6_tnl *t, struct sk_buff *skb,
  737. const struct tnl_ptk_info *tpi,
  738. struct metadata_dst *tun_dst,
  739. bool log_ecn_err)
  740. {
  741. return __ip6_tnl_rcv(t, skb, tpi, tun_dst, ip6ip6_dscp_ecn_decapsulate,
  742. log_ecn_err);
  743. }
  744. EXPORT_SYMBOL(ip6_tnl_rcv);
  745. static const struct tnl_ptk_info tpi_v6 = {
  746. /* no tunnel info required for ipxip6. */
  747. .proto = htons(ETH_P_IPV6),
  748. };
  749. static const struct tnl_ptk_info tpi_v4 = {
  750. /* no tunnel info required for ipxip6. */
  751. .proto = htons(ETH_P_IP),
  752. };
  753. static int ipxip6_rcv(struct sk_buff *skb, u8 ipproto,
  754. const struct tnl_ptk_info *tpi,
  755. int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
  756. const struct ipv6hdr *ipv6h,
  757. struct sk_buff *skb))
  758. {
  759. struct ip6_tnl *t;
  760. const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  761. struct metadata_dst *tun_dst = NULL;
  762. int ret = -1;
  763. rcu_read_lock();
  764. t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr);
  765. if (t) {
  766. u8 tproto = READ_ONCE(t->parms.proto);
  767. if (tproto != ipproto && tproto != 0)
  768. goto drop;
  769. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  770. goto drop;
  771. if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr))
  772. goto drop;
  773. if (iptunnel_pull_header(skb, 0, tpi->proto, false))
  774. goto drop;
  775. if (t->parms.collect_md) {
  776. tun_dst = ipv6_tun_rx_dst(skb, 0, 0, 0);
  777. if (!tun_dst)
  778. goto drop;
  779. }
  780. ret = __ip6_tnl_rcv(t, skb, tpi, tun_dst, dscp_ecn_decapsulate,
  781. log_ecn_error);
  782. }
  783. rcu_read_unlock();
  784. return ret;
  785. drop:
  786. rcu_read_unlock();
  787. kfree_skb(skb);
  788. return 0;
  789. }
  790. static int ip4ip6_rcv(struct sk_buff *skb)
  791. {
  792. return ipxip6_rcv(skb, IPPROTO_IPIP, &tpi_v4,
  793. ip4ip6_dscp_ecn_decapsulate);
  794. }
  795. static int ip6ip6_rcv(struct sk_buff *skb)
  796. {
  797. return ipxip6_rcv(skb, IPPROTO_IPV6, &tpi_v6,
  798. ip6ip6_dscp_ecn_decapsulate);
  799. }
  800. struct ipv6_tel_txoption {
  801. struct ipv6_txoptions ops;
  802. __u8 dst_opt[8];
  803. };
  804. static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
  805. {
  806. memset(opt, 0, sizeof(struct ipv6_tel_txoption));
  807. opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
  808. opt->dst_opt[3] = 1;
  809. opt->dst_opt[4] = encap_limit;
  810. opt->dst_opt[5] = IPV6_TLV_PADN;
  811. opt->dst_opt[6] = 1;
  812. opt->ops.dst1opt = (struct ipv6_opt_hdr *) opt->dst_opt;
  813. opt->ops.opt_nflen = 8;
  814. }
  815. /**
  816. * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  817. * @t: the outgoing tunnel device
  818. * @hdr: IPv6 header from the incoming packet
  819. *
  820. * Description:
  821. * Avoid trivial tunneling loop by checking that tunnel exit-point
  822. * doesn't match source of incoming packet.
  823. *
  824. * Return:
  825. * 1 if conflict,
  826. * 0 else
  827. **/
  828. static inline bool
  829. ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
  830. {
  831. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  832. }
  833. int ip6_tnl_xmit_ctl(struct ip6_tnl *t,
  834. const struct in6_addr *laddr,
  835. const struct in6_addr *raddr)
  836. {
  837. struct __ip6_tnl_parm *p = &t->parms;
  838. int ret = 0;
  839. struct net *net = t->net;
  840. if (t->parms.collect_md)
  841. return 1;
  842. if ((p->flags & IP6_TNL_F_CAP_XMIT) ||
  843. ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
  844. (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_XMIT))) {
  845. struct net_device *ldev = NULL;
  846. rcu_read_lock();
  847. if (p->link)
  848. ldev = dev_get_by_index_rcu(net, p->link);
  849. if (unlikely(!ipv6_chk_addr_and_flags(net, laddr, ldev, false,
  850. 0, IFA_F_TENTATIVE)))
  851. pr_warn("%s xmit: Local address not yet configured!\n",
  852. p->name);
  853. else if (!(p->flags & IP6_TNL_F_ALLOW_LOCAL_REMOTE) &&
  854. !ipv6_addr_is_multicast(raddr) &&
  855. unlikely(ipv6_chk_addr_and_flags(net, raddr, ldev,
  856. true, 0, IFA_F_TENTATIVE)))
  857. pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
  858. p->name);
  859. else
  860. ret = 1;
  861. rcu_read_unlock();
  862. }
  863. return ret;
  864. }
  865. EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);
  866. /**
  867. * ip6_tnl_xmit - encapsulate packet and send
  868. * @skb: the outgoing socket buffer
  869. * @dev: the outgoing tunnel device
  870. * @dsfield: dscp code for outer header
  871. * @fl6: flow of tunneled packet
  872. * @encap_limit: encapsulation limit
  873. * @pmtu: Path MTU is stored if packet is too big
  874. * @proto: next header value
  875. *
  876. * Description:
  877. * Build new header and do some sanity checks on the packet before sending
  878. * it.
  879. *
  880. * Return:
  881. * 0 on success
  882. * -1 fail
  883. * %-EMSGSIZE message too big. return mtu in this case.
  884. **/
  885. int ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, __u8 dsfield,
  886. struct flowi6 *fl6, int encap_limit, __u32 *pmtu,
  887. __u8 proto)
  888. {
  889. struct ip6_tnl *t = netdev_priv(dev);
  890. struct net *net = t->net;
  891. struct net_device_stats *stats = &t->dev->stats;
  892. struct ipv6hdr *ipv6h;
  893. struct ipv6_tel_txoption opt;
  894. struct dst_entry *dst = NULL, *ndst = NULL;
  895. struct net_device *tdev;
  896. int mtu;
  897. unsigned int eth_hlen = t->dev->type == ARPHRD_ETHER ? ETH_HLEN : 0;
  898. unsigned int psh_hlen = sizeof(struct ipv6hdr) + t->encap_hlen;
  899. unsigned int max_headroom = psh_hlen;
  900. bool use_cache = false;
  901. u8 hop_limit;
  902. int err = -1;
  903. if (t->parms.collect_md) {
  904. hop_limit = skb_tunnel_info(skb)->key.ttl;
  905. goto route_lookup;
  906. } else {
  907. hop_limit = t->parms.hop_limit;
  908. }
  909. /* NBMA tunnel */
  910. if (ipv6_addr_any(&t->parms.raddr)) {
  911. if (skb->protocol == htons(ETH_P_IPV6)) {
  912. struct in6_addr *addr6;
  913. struct neighbour *neigh;
  914. int addr_type;
  915. if (!skb_dst(skb))
  916. goto tx_err_link_failure;
  917. neigh = dst_neigh_lookup(skb_dst(skb),
  918. &ipv6_hdr(skb)->daddr);
  919. if (!neigh)
  920. goto tx_err_link_failure;
  921. addr6 = (struct in6_addr *)&neigh->primary_key;
  922. addr_type = ipv6_addr_type(addr6);
  923. if (addr_type == IPV6_ADDR_ANY)
  924. addr6 = &ipv6_hdr(skb)->daddr;
  925. memcpy(&fl6->daddr, addr6, sizeof(fl6->daddr));
  926. neigh_release(neigh);
  927. }
  928. } else if (t->parms.proto != 0 && !(t->parms.flags &
  929. (IP6_TNL_F_USE_ORIG_TCLASS |
  930. IP6_TNL_F_USE_ORIG_FWMARK))) {
  931. /* enable the cache only if neither the outer protocol nor the
  932. * routing decision depends on the current inner header value
  933. */
  934. use_cache = true;
  935. }
  936. if (use_cache)
  937. dst = dst_cache_get(&t->dst_cache);
  938. if (!ip6_tnl_xmit_ctl(t, &fl6->saddr, &fl6->daddr))
  939. goto tx_err_link_failure;
  940. if (!dst) {
  941. route_lookup:
  942. /* add dsfield to flowlabel for route lookup */
  943. fl6->flowlabel = ip6_make_flowinfo(dsfield, fl6->flowlabel);
  944. dst = ip6_route_output(net, NULL, fl6);
  945. if (dst->error)
  946. goto tx_err_link_failure;
  947. dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), NULL, 0);
  948. if (IS_ERR(dst)) {
  949. err = PTR_ERR(dst);
  950. dst = NULL;
  951. goto tx_err_link_failure;
  952. }
  953. if (t->parms.collect_md &&
  954. ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
  955. &fl6->daddr, 0, &fl6->saddr))
  956. goto tx_err_link_failure;
  957. ndst = dst;
  958. }
  959. tdev = dst->dev;
  960. if (tdev == dev) {
  961. stats->collisions++;
  962. net_warn_ratelimited("%s: Local routing loop detected!\n",
  963. t->parms.name);
  964. goto tx_err_dst_release;
  965. }
  966. mtu = dst_mtu(dst) - eth_hlen - psh_hlen - t->tun_hlen;
  967. if (encap_limit >= 0) {
  968. max_headroom += 8;
  969. mtu -= 8;
  970. }
  971. if (skb->protocol == htons(ETH_P_IPV6)) {
  972. if (mtu < IPV6_MIN_MTU)
  973. mtu = IPV6_MIN_MTU;
  974. } else if (mtu < 576) {
  975. mtu = 576;
  976. }
  977. skb_dst_update_pmtu(skb, mtu);
  978. if (skb->len - t->tun_hlen - eth_hlen > mtu && !skb_is_gso(skb)) {
  979. *pmtu = mtu;
  980. err = -EMSGSIZE;
  981. goto tx_err_dst_release;
  982. }
  983. if (t->err_count > 0) {
  984. if (time_before(jiffies,
  985. t->err_time + IP6TUNNEL_ERR_TIMEO)) {
  986. t->err_count--;
  987. dst_link_failure(skb);
  988. } else {
  989. t->err_count = 0;
  990. }
  991. }
  992. skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
  993. /*
  994. * Okay, now see if we can stuff it in the buffer as-is.
  995. */
  996. max_headroom += LL_RESERVED_SPACE(tdev);
  997. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  998. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  999. struct sk_buff *new_skb;
  1000. new_skb = skb_realloc_headroom(skb, max_headroom);
  1001. if (!new_skb)
  1002. goto tx_err_dst_release;
  1003. if (skb->sk)
  1004. skb_set_owner_w(new_skb, skb->sk);
  1005. consume_skb(skb);
  1006. skb = new_skb;
  1007. }
  1008. if (t->parms.collect_md) {
  1009. if (t->encap.type != TUNNEL_ENCAP_NONE)
  1010. goto tx_err_dst_release;
  1011. } else {
  1012. if (use_cache && ndst)
  1013. dst_cache_set_ip6(&t->dst_cache, ndst, &fl6->saddr);
  1014. }
  1015. skb_dst_set(skb, dst);
  1016. if (encap_limit >= 0) {
  1017. init_tel_txopt(&opt, encap_limit);
  1018. ipv6_push_frag_opts(skb, &opt.ops, &proto);
  1019. }
  1020. hop_limit = hop_limit ? : ip6_dst_hoplimit(dst);
  1021. /* Calculate max headroom for all the headers and adjust
  1022. * needed_headroom if necessary.
  1023. */
  1024. max_headroom = LL_RESERVED_SPACE(dst->dev) + sizeof(struct ipv6hdr)
  1025. + dst->header_len + t->hlen;
  1026. if (max_headroom > dev->needed_headroom)
  1027. dev->needed_headroom = max_headroom;
  1028. err = ip6_tnl_encap(skb, t, &proto, fl6);
  1029. if (err)
  1030. return err;
  1031. skb_push(skb, sizeof(struct ipv6hdr));
  1032. skb_reset_network_header(skb);
  1033. ipv6h = ipv6_hdr(skb);
  1034. ip6_flow_hdr(ipv6h, dsfield,
  1035. ip6_make_flowlabel(net, skb, fl6->flowlabel, true, fl6));
  1036. ipv6h->hop_limit = hop_limit;
  1037. ipv6h->nexthdr = proto;
  1038. ipv6h->saddr = fl6->saddr;
  1039. ipv6h->daddr = fl6->daddr;
  1040. ip6tunnel_xmit(NULL, skb, dev);
  1041. return 0;
  1042. tx_err_link_failure:
  1043. stats->tx_carrier_errors++;
  1044. dst_link_failure(skb);
  1045. tx_err_dst_release:
  1046. dst_release(dst);
  1047. return err;
  1048. }
  1049. EXPORT_SYMBOL(ip6_tnl_xmit);
  1050. static inline int
  1051. ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  1052. {
  1053. struct ip6_tnl *t = netdev_priv(dev);
  1054. const struct iphdr *iph = ip_hdr(skb);
  1055. int encap_limit = -1;
  1056. struct flowi6 fl6;
  1057. __u8 dsfield;
  1058. __u32 mtu;
  1059. u8 tproto;
  1060. int err;
  1061. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  1062. tproto = READ_ONCE(t->parms.proto);
  1063. if (tproto != IPPROTO_IPIP && tproto != 0)
  1064. return -1;
  1065. if (t->parms.collect_md) {
  1066. struct ip_tunnel_info *tun_info;
  1067. const struct ip_tunnel_key *key;
  1068. tun_info = skb_tunnel_info(skb);
  1069. if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
  1070. ip_tunnel_info_af(tun_info) != AF_INET6))
  1071. return -1;
  1072. key = &tun_info->key;
  1073. memset(&fl6, 0, sizeof(fl6));
  1074. fl6.flowi6_proto = IPPROTO_IPIP;
  1075. fl6.daddr = key->u.ipv6.dst;
  1076. fl6.flowlabel = key->label;
  1077. dsfield = key->tos;
  1078. } else {
  1079. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1080. encap_limit = t->parms.encap_limit;
  1081. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  1082. fl6.flowi6_proto = IPPROTO_IPIP;
  1083. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  1084. dsfield = ipv4_get_dsfield(iph);
  1085. else
  1086. dsfield = ip6_tclass(t->parms.flowinfo);
  1087. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  1088. fl6.flowi6_mark = skb->mark;
  1089. else
  1090. fl6.flowi6_mark = t->parms.fwmark;
  1091. }
  1092. fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
  1093. if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6))
  1094. return -1;
  1095. dsfield = INET_ECN_encapsulate(dsfield, ipv4_get_dsfield(iph));
  1096. skb_set_inner_ipproto(skb, IPPROTO_IPIP);
  1097. err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
  1098. IPPROTO_IPIP);
  1099. if (err != 0) {
  1100. /* XXX: send ICMP error even if DF is not set. */
  1101. if (err == -EMSGSIZE)
  1102. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  1103. htonl(mtu));
  1104. return -1;
  1105. }
  1106. return 0;
  1107. }
  1108. static inline int
  1109. ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  1110. {
  1111. struct ip6_tnl *t = netdev_priv(dev);
  1112. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  1113. int encap_limit = -1;
  1114. __u16 offset;
  1115. struct flowi6 fl6;
  1116. __u8 dsfield;
  1117. __u32 mtu;
  1118. u8 tproto;
  1119. int err;
  1120. tproto = READ_ONCE(t->parms.proto);
  1121. if ((tproto != IPPROTO_IPV6 && tproto != 0) ||
  1122. ip6_tnl_addr_conflict(t, ipv6h))
  1123. return -1;
  1124. if (t->parms.collect_md) {
  1125. struct ip_tunnel_info *tun_info;
  1126. const struct ip_tunnel_key *key;
  1127. tun_info = skb_tunnel_info(skb);
  1128. if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
  1129. ip_tunnel_info_af(tun_info) != AF_INET6))
  1130. return -1;
  1131. key = &tun_info->key;
  1132. memset(&fl6, 0, sizeof(fl6));
  1133. fl6.flowi6_proto = IPPROTO_IPV6;
  1134. fl6.daddr = key->u.ipv6.dst;
  1135. fl6.flowlabel = key->label;
  1136. dsfield = key->tos;
  1137. } else {
  1138. offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
  1139. /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
  1140. ipv6h = ipv6_hdr(skb);
  1141. if (offset > 0) {
  1142. struct ipv6_tlv_tnl_enc_lim *tel;
  1143. tel = (void *)&skb_network_header(skb)[offset];
  1144. if (tel->encap_limit == 0) {
  1145. icmpv6_send(skb, ICMPV6_PARAMPROB,
  1146. ICMPV6_HDR_FIELD, offset + 2);
  1147. return -1;
  1148. }
  1149. encap_limit = tel->encap_limit - 1;
  1150. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
  1151. encap_limit = t->parms.encap_limit;
  1152. }
  1153. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  1154. fl6.flowi6_proto = IPPROTO_IPV6;
  1155. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  1156. dsfield = ipv6_get_dsfield(ipv6h);
  1157. else
  1158. dsfield = ip6_tclass(t->parms.flowinfo);
  1159. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
  1160. fl6.flowlabel |= ip6_flowlabel(ipv6h);
  1161. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  1162. fl6.flowi6_mark = skb->mark;
  1163. else
  1164. fl6.flowi6_mark = t->parms.fwmark;
  1165. }
  1166. fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
  1167. if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6))
  1168. return -1;
  1169. dsfield = INET_ECN_encapsulate(dsfield, ipv6_get_dsfield(ipv6h));
  1170. skb_set_inner_ipproto(skb, IPPROTO_IPV6);
  1171. err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
  1172. IPPROTO_IPV6);
  1173. if (err != 0) {
  1174. if (err == -EMSGSIZE)
  1175. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  1176. return -1;
  1177. }
  1178. return 0;
  1179. }
  1180. static netdev_tx_t
  1181. ip6_tnl_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1182. {
  1183. struct ip6_tnl *t = netdev_priv(dev);
  1184. struct net_device_stats *stats = &t->dev->stats;
  1185. int ret;
  1186. switch (skb->protocol) {
  1187. case htons(ETH_P_IP):
  1188. ret = ip4ip6_tnl_xmit(skb, dev);
  1189. break;
  1190. case htons(ETH_P_IPV6):
  1191. ret = ip6ip6_tnl_xmit(skb, dev);
  1192. break;
  1193. default:
  1194. goto tx_err;
  1195. }
  1196. if (ret < 0)
  1197. goto tx_err;
  1198. return NETDEV_TX_OK;
  1199. tx_err:
  1200. stats->tx_errors++;
  1201. stats->tx_dropped++;
  1202. kfree_skb(skb);
  1203. return NETDEV_TX_OK;
  1204. }
  1205. static void ip6_tnl_link_config(struct ip6_tnl *t)
  1206. {
  1207. struct net_device *dev = t->dev;
  1208. struct __ip6_tnl_parm *p = &t->parms;
  1209. struct flowi6 *fl6 = &t->fl.u.ip6;
  1210. int t_hlen;
  1211. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  1212. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  1213. /* Set up flowi template */
  1214. fl6->saddr = p->laddr;
  1215. fl6->daddr = p->raddr;
  1216. fl6->flowi6_oif = p->link;
  1217. fl6->flowlabel = 0;
  1218. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  1219. fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  1220. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  1221. fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  1222. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
  1223. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  1224. if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
  1225. dev->flags |= IFF_POINTOPOINT;
  1226. else
  1227. dev->flags &= ~IFF_POINTOPOINT;
  1228. t->tun_hlen = 0;
  1229. t->hlen = t->encap_hlen + t->tun_hlen;
  1230. t_hlen = t->hlen + sizeof(struct ipv6hdr);
  1231. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  1232. int strict = (ipv6_addr_type(&p->raddr) &
  1233. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  1234. struct rt6_info *rt = rt6_lookup(t->net,
  1235. &p->raddr, &p->laddr,
  1236. p->link, NULL, strict);
  1237. if (!rt)
  1238. return;
  1239. if (rt->dst.dev) {
  1240. dev->hard_header_len = rt->dst.dev->hard_header_len +
  1241. t_hlen;
  1242. dev->mtu = rt->dst.dev->mtu - t_hlen;
  1243. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1244. dev->mtu -= 8;
  1245. if (dev->mtu < IPV6_MIN_MTU)
  1246. dev->mtu = IPV6_MIN_MTU;
  1247. }
  1248. ip6_rt_put(rt);
  1249. }
  1250. }
  1251. /**
  1252. * ip6_tnl_change - update the tunnel parameters
  1253. * @t: tunnel to be changed
  1254. * @p: tunnel configuration parameters
  1255. *
  1256. * Description:
  1257. * ip6_tnl_change() updates the tunnel parameters
  1258. **/
  1259. static int
  1260. ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
  1261. {
  1262. t->parms.laddr = p->laddr;
  1263. t->parms.raddr = p->raddr;
  1264. t->parms.flags = p->flags;
  1265. t->parms.hop_limit = p->hop_limit;
  1266. t->parms.encap_limit = p->encap_limit;
  1267. t->parms.flowinfo = p->flowinfo;
  1268. t->parms.link = p->link;
  1269. t->parms.proto = p->proto;
  1270. t->parms.fwmark = p->fwmark;
  1271. dst_cache_reset(&t->dst_cache);
  1272. ip6_tnl_link_config(t);
  1273. return 0;
  1274. }
  1275. static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
  1276. {
  1277. struct net *net = t->net;
  1278. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1279. int err;
  1280. ip6_tnl_unlink(ip6n, t);
  1281. synchronize_net();
  1282. err = ip6_tnl_change(t, p);
  1283. ip6_tnl_link(ip6n, t);
  1284. netdev_state_change(t->dev);
  1285. return err;
  1286. }
  1287. static int ip6_tnl0_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
  1288. {
  1289. /* for default tnl0 device allow to change only the proto */
  1290. t->parms.proto = p->proto;
  1291. netdev_state_change(t->dev);
  1292. return 0;
  1293. }
  1294. static void
  1295. ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u)
  1296. {
  1297. p->laddr = u->laddr;
  1298. p->raddr = u->raddr;
  1299. p->flags = u->flags;
  1300. p->hop_limit = u->hop_limit;
  1301. p->encap_limit = u->encap_limit;
  1302. p->flowinfo = u->flowinfo;
  1303. p->link = u->link;
  1304. p->proto = u->proto;
  1305. memcpy(p->name, u->name, sizeof(u->name));
  1306. }
  1307. static void
  1308. ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p)
  1309. {
  1310. u->laddr = p->laddr;
  1311. u->raddr = p->raddr;
  1312. u->flags = p->flags;
  1313. u->hop_limit = p->hop_limit;
  1314. u->encap_limit = p->encap_limit;
  1315. u->flowinfo = p->flowinfo;
  1316. u->link = p->link;
  1317. u->proto = p->proto;
  1318. memcpy(u->name, p->name, sizeof(u->name));
  1319. }
  1320. /**
  1321. * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
  1322. * @dev: virtual device associated with tunnel
  1323. * @ifr: parameters passed from userspace
  1324. * @cmd: command to be performed
  1325. *
  1326. * Description:
  1327. * ip6_tnl_ioctl() is used for managing IPv6 tunnels
  1328. * from userspace.
  1329. *
  1330. * The possible commands are the following:
  1331. * %SIOCGETTUNNEL: get tunnel parameters for device
  1332. * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
  1333. * %SIOCCHGTUNNEL: change tunnel parameters to those given
  1334. * %SIOCDELTUNNEL: delete tunnel
  1335. *
  1336. * The fallback device "ip6tnl0", created during module
  1337. * initialization, can be used for creating other tunnel devices.
  1338. *
  1339. * Return:
  1340. * 0 on success,
  1341. * %-EFAULT if unable to copy data to or from userspace,
  1342. * %-EPERM if current process hasn't %CAP_NET_ADMIN set
  1343. * %-EINVAL if passed tunnel parameters are invalid,
  1344. * %-EEXIST if changing a tunnel's parameters would cause a conflict
  1345. * %-ENODEV if attempting to change or delete a nonexisting device
  1346. **/
  1347. static int
  1348. ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1349. {
  1350. int err = 0;
  1351. struct ip6_tnl_parm p;
  1352. struct __ip6_tnl_parm p1;
  1353. struct ip6_tnl *t = netdev_priv(dev);
  1354. struct net *net = t->net;
  1355. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1356. memset(&p1, 0, sizeof(p1));
  1357. switch (cmd) {
  1358. case SIOCGETTUNNEL:
  1359. if (dev == ip6n->fb_tnl_dev) {
  1360. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  1361. err = -EFAULT;
  1362. break;
  1363. }
  1364. ip6_tnl_parm_from_user(&p1, &p);
  1365. t = ip6_tnl_locate(net, &p1, 0);
  1366. if (IS_ERR(t))
  1367. t = netdev_priv(dev);
  1368. } else {
  1369. memset(&p, 0, sizeof(p));
  1370. }
  1371. ip6_tnl_parm_to_user(&p, &t->parms);
  1372. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) {
  1373. err = -EFAULT;
  1374. }
  1375. break;
  1376. case SIOCADDTUNNEL:
  1377. case SIOCCHGTUNNEL:
  1378. err = -EPERM;
  1379. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1380. break;
  1381. err = -EFAULT;
  1382. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1383. break;
  1384. err = -EINVAL;
  1385. if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
  1386. p.proto != 0)
  1387. break;
  1388. ip6_tnl_parm_from_user(&p1, &p);
  1389. t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL);
  1390. if (cmd == SIOCCHGTUNNEL) {
  1391. if (!IS_ERR(t)) {
  1392. if (t->dev != dev) {
  1393. err = -EEXIST;
  1394. break;
  1395. }
  1396. } else
  1397. t = netdev_priv(dev);
  1398. if (dev == ip6n->fb_tnl_dev)
  1399. err = ip6_tnl0_update(t, &p1);
  1400. else
  1401. err = ip6_tnl_update(t, &p1);
  1402. }
  1403. if (!IS_ERR(t)) {
  1404. err = 0;
  1405. ip6_tnl_parm_to_user(&p, &t->parms);
  1406. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  1407. err = -EFAULT;
  1408. } else {
  1409. err = PTR_ERR(t);
  1410. }
  1411. break;
  1412. case SIOCDELTUNNEL:
  1413. err = -EPERM;
  1414. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1415. break;
  1416. if (dev == ip6n->fb_tnl_dev) {
  1417. err = -EFAULT;
  1418. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1419. break;
  1420. err = -ENOENT;
  1421. ip6_tnl_parm_from_user(&p1, &p);
  1422. t = ip6_tnl_locate(net, &p1, 0);
  1423. if (IS_ERR(t))
  1424. break;
  1425. err = -EPERM;
  1426. if (t->dev == ip6n->fb_tnl_dev)
  1427. break;
  1428. dev = t->dev;
  1429. }
  1430. err = 0;
  1431. unregister_netdevice(dev);
  1432. break;
  1433. default:
  1434. err = -EINVAL;
  1435. }
  1436. return err;
  1437. }
  1438. /**
  1439. * ip6_tnl_change_mtu - change mtu manually for tunnel device
  1440. * @dev: virtual device associated with tunnel
  1441. * @new_mtu: the new mtu
  1442. *
  1443. * Return:
  1444. * 0 on success,
  1445. * %-EINVAL if mtu too small
  1446. **/
  1447. int ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
  1448. {
  1449. struct ip6_tnl *tnl = netdev_priv(dev);
  1450. if (tnl->parms.proto == IPPROTO_IPV6) {
  1451. if (new_mtu < IPV6_MIN_MTU)
  1452. return -EINVAL;
  1453. } else {
  1454. if (new_mtu < ETH_MIN_MTU)
  1455. return -EINVAL;
  1456. }
  1457. if (tnl->parms.proto == IPPROTO_IPV6 || tnl->parms.proto == 0) {
  1458. if (new_mtu > IP6_MAX_MTU - dev->hard_header_len)
  1459. return -EINVAL;
  1460. } else {
  1461. if (new_mtu > IP_MAX_MTU - dev->hard_header_len)
  1462. return -EINVAL;
  1463. }
  1464. dev->mtu = new_mtu;
  1465. return 0;
  1466. }
  1467. EXPORT_SYMBOL(ip6_tnl_change_mtu);
  1468. int ip6_tnl_get_iflink(const struct net_device *dev)
  1469. {
  1470. struct ip6_tnl *t = netdev_priv(dev);
  1471. return t->parms.link;
  1472. }
  1473. EXPORT_SYMBOL(ip6_tnl_get_iflink);
  1474. int ip6_tnl_encap_add_ops(const struct ip6_tnl_encap_ops *ops,
  1475. unsigned int num)
  1476. {
  1477. if (num >= MAX_IPTUN_ENCAP_OPS)
  1478. return -ERANGE;
  1479. return !cmpxchg((const struct ip6_tnl_encap_ops **)
  1480. &ip6tun_encaps[num],
  1481. NULL, ops) ? 0 : -1;
  1482. }
  1483. EXPORT_SYMBOL(ip6_tnl_encap_add_ops);
  1484. int ip6_tnl_encap_del_ops(const struct ip6_tnl_encap_ops *ops,
  1485. unsigned int num)
  1486. {
  1487. int ret;
  1488. if (num >= MAX_IPTUN_ENCAP_OPS)
  1489. return -ERANGE;
  1490. ret = (cmpxchg((const struct ip6_tnl_encap_ops **)
  1491. &ip6tun_encaps[num],
  1492. ops, NULL) == ops) ? 0 : -1;
  1493. synchronize_net();
  1494. return ret;
  1495. }
  1496. EXPORT_SYMBOL(ip6_tnl_encap_del_ops);
  1497. int ip6_tnl_encap_setup(struct ip6_tnl *t,
  1498. struct ip_tunnel_encap *ipencap)
  1499. {
  1500. int hlen;
  1501. memset(&t->encap, 0, sizeof(t->encap));
  1502. hlen = ip6_encap_hlen(ipencap);
  1503. if (hlen < 0)
  1504. return hlen;
  1505. t->encap.type = ipencap->type;
  1506. t->encap.sport = ipencap->sport;
  1507. t->encap.dport = ipencap->dport;
  1508. t->encap.flags = ipencap->flags;
  1509. t->encap_hlen = hlen;
  1510. t->hlen = t->encap_hlen + t->tun_hlen;
  1511. return 0;
  1512. }
  1513. EXPORT_SYMBOL_GPL(ip6_tnl_encap_setup);
  1514. static const struct net_device_ops ip6_tnl_netdev_ops = {
  1515. .ndo_init = ip6_tnl_dev_init,
  1516. .ndo_uninit = ip6_tnl_dev_uninit,
  1517. .ndo_start_xmit = ip6_tnl_start_xmit,
  1518. .ndo_do_ioctl = ip6_tnl_ioctl,
  1519. .ndo_change_mtu = ip6_tnl_change_mtu,
  1520. .ndo_get_stats = ip6_get_stats,
  1521. .ndo_get_iflink = ip6_tnl_get_iflink,
  1522. };
  1523. #define IPXIPX_FEATURES (NETIF_F_SG | \
  1524. NETIF_F_FRAGLIST | \
  1525. NETIF_F_HIGHDMA | \
  1526. NETIF_F_GSO_SOFTWARE | \
  1527. NETIF_F_HW_CSUM)
  1528. /**
  1529. * ip6_tnl_dev_setup - setup virtual tunnel device
  1530. * @dev: virtual device associated with tunnel
  1531. *
  1532. * Description:
  1533. * Initialize function pointers and device parameters
  1534. **/
  1535. static void ip6_tnl_dev_setup(struct net_device *dev)
  1536. {
  1537. dev->netdev_ops = &ip6_tnl_netdev_ops;
  1538. dev->needs_free_netdev = true;
  1539. dev->priv_destructor = ip6_dev_free;
  1540. dev->type = ARPHRD_TUNNEL6;
  1541. dev->flags |= IFF_NOARP;
  1542. dev->addr_len = sizeof(struct in6_addr);
  1543. dev->features |= NETIF_F_LLTX;
  1544. netif_keep_dst(dev);
  1545. dev->features |= IPXIPX_FEATURES;
  1546. dev->hw_features |= IPXIPX_FEATURES;
  1547. /* This perm addr will be used as interface identifier by IPv6 */
  1548. dev->addr_assign_type = NET_ADDR_RANDOM;
  1549. eth_random_addr(dev->perm_addr);
  1550. }
  1551. /**
  1552. * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
  1553. * @dev: virtual device associated with tunnel
  1554. **/
  1555. static inline int
  1556. ip6_tnl_dev_init_gen(struct net_device *dev)
  1557. {
  1558. struct ip6_tnl *t = netdev_priv(dev);
  1559. int ret;
  1560. int t_hlen;
  1561. t->dev = dev;
  1562. t->net = dev_net(dev);
  1563. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1564. if (!dev->tstats)
  1565. return -ENOMEM;
  1566. ret = dst_cache_init(&t->dst_cache, GFP_KERNEL);
  1567. if (ret)
  1568. goto free_stats;
  1569. ret = gro_cells_init(&t->gro_cells, dev);
  1570. if (ret)
  1571. goto destroy_dst;
  1572. t->tun_hlen = 0;
  1573. t->hlen = t->encap_hlen + t->tun_hlen;
  1574. t_hlen = t->hlen + sizeof(struct ipv6hdr);
  1575. dev->type = ARPHRD_TUNNEL6;
  1576. dev->hard_header_len = LL_MAX_HEADER + t_hlen;
  1577. dev->mtu = ETH_DATA_LEN - t_hlen;
  1578. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1579. dev->mtu -= 8;
  1580. dev->min_mtu = ETH_MIN_MTU;
  1581. dev->max_mtu = IP6_MAX_MTU - dev->hard_header_len;
  1582. return 0;
  1583. destroy_dst:
  1584. dst_cache_destroy(&t->dst_cache);
  1585. free_stats:
  1586. free_percpu(dev->tstats);
  1587. dev->tstats = NULL;
  1588. return ret;
  1589. }
  1590. /**
  1591. * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
  1592. * @dev: virtual device associated with tunnel
  1593. **/
  1594. static int ip6_tnl_dev_init(struct net_device *dev)
  1595. {
  1596. struct ip6_tnl *t = netdev_priv(dev);
  1597. int err = ip6_tnl_dev_init_gen(dev);
  1598. if (err)
  1599. return err;
  1600. ip6_tnl_link_config(t);
  1601. if (t->parms.collect_md) {
  1602. dev->features |= NETIF_F_NETNS_LOCAL;
  1603. netif_keep_dst(dev);
  1604. }
  1605. return 0;
  1606. }
  1607. /**
  1608. * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
  1609. * @dev: fallback device
  1610. *
  1611. * Return: 0
  1612. **/
  1613. static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
  1614. {
  1615. struct ip6_tnl *t = netdev_priv(dev);
  1616. struct net *net = dev_net(dev);
  1617. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1618. t->parms.proto = IPPROTO_IPV6;
  1619. dev_hold(dev);
  1620. rcu_assign_pointer(ip6n->tnls_wc[0], t);
  1621. return 0;
  1622. }
  1623. static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[],
  1624. struct netlink_ext_ack *extack)
  1625. {
  1626. u8 proto;
  1627. if (!data || !data[IFLA_IPTUN_PROTO])
  1628. return 0;
  1629. proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1630. if (proto != IPPROTO_IPV6 &&
  1631. proto != IPPROTO_IPIP &&
  1632. proto != 0)
  1633. return -EINVAL;
  1634. return 0;
  1635. }
  1636. static void ip6_tnl_netlink_parms(struct nlattr *data[],
  1637. struct __ip6_tnl_parm *parms)
  1638. {
  1639. memset(parms, 0, sizeof(*parms));
  1640. if (!data)
  1641. return;
  1642. if (data[IFLA_IPTUN_LINK])
  1643. parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
  1644. if (data[IFLA_IPTUN_LOCAL])
  1645. parms->laddr = nla_get_in6_addr(data[IFLA_IPTUN_LOCAL]);
  1646. if (data[IFLA_IPTUN_REMOTE])
  1647. parms->raddr = nla_get_in6_addr(data[IFLA_IPTUN_REMOTE]);
  1648. if (data[IFLA_IPTUN_TTL])
  1649. parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]);
  1650. if (data[IFLA_IPTUN_ENCAP_LIMIT])
  1651. parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]);
  1652. if (data[IFLA_IPTUN_FLOWINFO])
  1653. parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]);
  1654. if (data[IFLA_IPTUN_FLAGS])
  1655. parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]);
  1656. if (data[IFLA_IPTUN_PROTO])
  1657. parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1658. if (data[IFLA_IPTUN_COLLECT_METADATA])
  1659. parms->collect_md = true;
  1660. if (data[IFLA_IPTUN_FWMARK])
  1661. parms->fwmark = nla_get_u32(data[IFLA_IPTUN_FWMARK]);
  1662. }
  1663. static bool ip6_tnl_netlink_encap_parms(struct nlattr *data[],
  1664. struct ip_tunnel_encap *ipencap)
  1665. {
  1666. bool ret = false;
  1667. memset(ipencap, 0, sizeof(*ipencap));
  1668. if (!data)
  1669. return ret;
  1670. if (data[IFLA_IPTUN_ENCAP_TYPE]) {
  1671. ret = true;
  1672. ipencap->type = nla_get_u16(data[IFLA_IPTUN_ENCAP_TYPE]);
  1673. }
  1674. if (data[IFLA_IPTUN_ENCAP_FLAGS]) {
  1675. ret = true;
  1676. ipencap->flags = nla_get_u16(data[IFLA_IPTUN_ENCAP_FLAGS]);
  1677. }
  1678. if (data[IFLA_IPTUN_ENCAP_SPORT]) {
  1679. ret = true;
  1680. ipencap->sport = nla_get_be16(data[IFLA_IPTUN_ENCAP_SPORT]);
  1681. }
  1682. if (data[IFLA_IPTUN_ENCAP_DPORT]) {
  1683. ret = true;
  1684. ipencap->dport = nla_get_be16(data[IFLA_IPTUN_ENCAP_DPORT]);
  1685. }
  1686. return ret;
  1687. }
  1688. static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
  1689. struct nlattr *tb[], struct nlattr *data[],
  1690. struct netlink_ext_ack *extack)
  1691. {
  1692. struct net *net = dev_net(dev);
  1693. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1694. struct ip_tunnel_encap ipencap;
  1695. struct ip6_tnl *nt, *t;
  1696. int err;
  1697. nt = netdev_priv(dev);
  1698. if (ip6_tnl_netlink_encap_parms(data, &ipencap)) {
  1699. err = ip6_tnl_encap_setup(nt, &ipencap);
  1700. if (err < 0)
  1701. return err;
  1702. }
  1703. ip6_tnl_netlink_parms(data, &nt->parms);
  1704. if (nt->parms.collect_md) {
  1705. if (rtnl_dereference(ip6n->collect_md_tun))
  1706. return -EEXIST;
  1707. } else {
  1708. t = ip6_tnl_locate(net, &nt->parms, 0);
  1709. if (!IS_ERR(t))
  1710. return -EEXIST;
  1711. }
  1712. err = ip6_tnl_create2(dev);
  1713. if (!err && tb[IFLA_MTU])
  1714. ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
  1715. return err;
  1716. }
  1717. static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],
  1718. struct nlattr *data[],
  1719. struct netlink_ext_ack *extack)
  1720. {
  1721. struct ip6_tnl *t = netdev_priv(dev);
  1722. struct __ip6_tnl_parm p;
  1723. struct net *net = t->net;
  1724. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1725. struct ip_tunnel_encap ipencap;
  1726. if (dev == ip6n->fb_tnl_dev)
  1727. return -EINVAL;
  1728. if (ip6_tnl_netlink_encap_parms(data, &ipencap)) {
  1729. int err = ip6_tnl_encap_setup(t, &ipencap);
  1730. if (err < 0)
  1731. return err;
  1732. }
  1733. ip6_tnl_netlink_parms(data, &p);
  1734. if (p.collect_md)
  1735. return -EINVAL;
  1736. t = ip6_tnl_locate(net, &p, 0);
  1737. if (!IS_ERR(t)) {
  1738. if (t->dev != dev)
  1739. return -EEXIST;
  1740. } else
  1741. t = netdev_priv(dev);
  1742. return ip6_tnl_update(t, &p);
  1743. }
  1744. static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head)
  1745. {
  1746. struct net *net = dev_net(dev);
  1747. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1748. if (dev != ip6n->fb_tnl_dev)
  1749. unregister_netdevice_queue(dev, head);
  1750. }
  1751. static size_t ip6_tnl_get_size(const struct net_device *dev)
  1752. {
  1753. return
  1754. /* IFLA_IPTUN_LINK */
  1755. nla_total_size(4) +
  1756. /* IFLA_IPTUN_LOCAL */
  1757. nla_total_size(sizeof(struct in6_addr)) +
  1758. /* IFLA_IPTUN_REMOTE */
  1759. nla_total_size(sizeof(struct in6_addr)) +
  1760. /* IFLA_IPTUN_TTL */
  1761. nla_total_size(1) +
  1762. /* IFLA_IPTUN_ENCAP_LIMIT */
  1763. nla_total_size(1) +
  1764. /* IFLA_IPTUN_FLOWINFO */
  1765. nla_total_size(4) +
  1766. /* IFLA_IPTUN_FLAGS */
  1767. nla_total_size(4) +
  1768. /* IFLA_IPTUN_PROTO */
  1769. nla_total_size(1) +
  1770. /* IFLA_IPTUN_ENCAP_TYPE */
  1771. nla_total_size(2) +
  1772. /* IFLA_IPTUN_ENCAP_FLAGS */
  1773. nla_total_size(2) +
  1774. /* IFLA_IPTUN_ENCAP_SPORT */
  1775. nla_total_size(2) +
  1776. /* IFLA_IPTUN_ENCAP_DPORT */
  1777. nla_total_size(2) +
  1778. /* IFLA_IPTUN_COLLECT_METADATA */
  1779. nla_total_size(0) +
  1780. /* IFLA_IPTUN_FWMARK */
  1781. nla_total_size(4) +
  1782. 0;
  1783. }
  1784. static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1785. {
  1786. struct ip6_tnl *tunnel = netdev_priv(dev);
  1787. struct __ip6_tnl_parm *parm = &tunnel->parms;
  1788. if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
  1789. nla_put_in6_addr(skb, IFLA_IPTUN_LOCAL, &parm->laddr) ||
  1790. nla_put_in6_addr(skb, IFLA_IPTUN_REMOTE, &parm->raddr) ||
  1791. nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) ||
  1792. nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) ||
  1793. nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) ||
  1794. nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) ||
  1795. nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto) ||
  1796. nla_put_u32(skb, IFLA_IPTUN_FWMARK, parm->fwmark))
  1797. goto nla_put_failure;
  1798. if (nla_put_u16(skb, IFLA_IPTUN_ENCAP_TYPE, tunnel->encap.type) ||
  1799. nla_put_be16(skb, IFLA_IPTUN_ENCAP_SPORT, tunnel->encap.sport) ||
  1800. nla_put_be16(skb, IFLA_IPTUN_ENCAP_DPORT, tunnel->encap.dport) ||
  1801. nla_put_u16(skb, IFLA_IPTUN_ENCAP_FLAGS, tunnel->encap.flags))
  1802. goto nla_put_failure;
  1803. if (parm->collect_md)
  1804. if (nla_put_flag(skb, IFLA_IPTUN_COLLECT_METADATA))
  1805. goto nla_put_failure;
  1806. return 0;
  1807. nla_put_failure:
  1808. return -EMSGSIZE;
  1809. }
  1810. struct net *ip6_tnl_get_link_net(const struct net_device *dev)
  1811. {
  1812. struct ip6_tnl *tunnel = netdev_priv(dev);
  1813. return tunnel->net;
  1814. }
  1815. EXPORT_SYMBOL(ip6_tnl_get_link_net);
  1816. static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = {
  1817. [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
  1818. [IFLA_IPTUN_LOCAL] = { .len = sizeof(struct in6_addr) },
  1819. [IFLA_IPTUN_REMOTE] = { .len = sizeof(struct in6_addr) },
  1820. [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
  1821. [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NLA_U8 },
  1822. [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32 },
  1823. [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 },
  1824. [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
  1825. [IFLA_IPTUN_ENCAP_TYPE] = { .type = NLA_U16 },
  1826. [IFLA_IPTUN_ENCAP_FLAGS] = { .type = NLA_U16 },
  1827. [IFLA_IPTUN_ENCAP_SPORT] = { .type = NLA_U16 },
  1828. [IFLA_IPTUN_ENCAP_DPORT] = { .type = NLA_U16 },
  1829. [IFLA_IPTUN_COLLECT_METADATA] = { .type = NLA_FLAG },
  1830. [IFLA_IPTUN_FWMARK] = { .type = NLA_U32 },
  1831. };
  1832. static struct rtnl_link_ops ip6_link_ops __read_mostly = {
  1833. .kind = "ip6tnl",
  1834. .maxtype = IFLA_IPTUN_MAX,
  1835. .policy = ip6_tnl_policy,
  1836. .priv_size = sizeof(struct ip6_tnl),
  1837. .setup = ip6_tnl_dev_setup,
  1838. .validate = ip6_tnl_validate,
  1839. .newlink = ip6_tnl_newlink,
  1840. .changelink = ip6_tnl_changelink,
  1841. .dellink = ip6_tnl_dellink,
  1842. .get_size = ip6_tnl_get_size,
  1843. .fill_info = ip6_tnl_fill_info,
  1844. .get_link_net = ip6_tnl_get_link_net,
  1845. };
  1846. static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
  1847. .handler = ip4ip6_rcv,
  1848. .err_handler = ip4ip6_err,
  1849. .priority = 1,
  1850. };
  1851. static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
  1852. .handler = ip6ip6_rcv,
  1853. .err_handler = ip6ip6_err,
  1854. .priority = 1,
  1855. };
  1856. static void __net_exit ip6_tnl_destroy_tunnels(struct net *net, struct list_head *list)
  1857. {
  1858. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1859. struct net_device *dev, *aux;
  1860. int h;
  1861. struct ip6_tnl *t;
  1862. for_each_netdev_safe(net, dev, aux)
  1863. if (dev->rtnl_link_ops == &ip6_link_ops)
  1864. unregister_netdevice_queue(dev, list);
  1865. for (h = 0; h < IP6_TUNNEL_HASH_SIZE; h++) {
  1866. t = rtnl_dereference(ip6n->tnls_r_l[h]);
  1867. while (t) {
  1868. /* If dev is in the same netns, it has already
  1869. * been added to the list by the previous loop.
  1870. */
  1871. if (!net_eq(dev_net(t->dev), net))
  1872. unregister_netdevice_queue(t->dev, list);
  1873. t = rtnl_dereference(t->next);
  1874. }
  1875. }
  1876. }
  1877. static int __net_init ip6_tnl_init_net(struct net *net)
  1878. {
  1879. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1880. struct ip6_tnl *t = NULL;
  1881. int err;
  1882. ip6n->tnls[0] = ip6n->tnls_wc;
  1883. ip6n->tnls[1] = ip6n->tnls_r_l;
  1884. if (!net_has_fallback_tunnels(net))
  1885. return 0;
  1886. err = -ENOMEM;
  1887. ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
  1888. NET_NAME_UNKNOWN, ip6_tnl_dev_setup);
  1889. if (!ip6n->fb_tnl_dev)
  1890. goto err_alloc_dev;
  1891. dev_net_set(ip6n->fb_tnl_dev, net);
  1892. ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_link_ops;
  1893. /* FB netdevice is special: we have one, and only one per netns.
  1894. * Allowing to move it to another netns is clearly unsafe.
  1895. */
  1896. ip6n->fb_tnl_dev->features |= NETIF_F_NETNS_LOCAL;
  1897. err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
  1898. if (err < 0)
  1899. goto err_register;
  1900. err = register_netdev(ip6n->fb_tnl_dev);
  1901. if (err < 0)
  1902. goto err_register;
  1903. t = netdev_priv(ip6n->fb_tnl_dev);
  1904. strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
  1905. return 0;
  1906. err_register:
  1907. free_netdev(ip6n->fb_tnl_dev);
  1908. err_alloc_dev:
  1909. return err;
  1910. }
  1911. static void __net_exit ip6_tnl_exit_batch_net(struct list_head *net_list)
  1912. {
  1913. struct net *net;
  1914. LIST_HEAD(list);
  1915. rtnl_lock();
  1916. list_for_each_entry(net, net_list, exit_list)
  1917. ip6_tnl_destroy_tunnels(net, &list);
  1918. unregister_netdevice_many(&list);
  1919. rtnl_unlock();
  1920. }
  1921. static struct pernet_operations ip6_tnl_net_ops = {
  1922. .init = ip6_tnl_init_net,
  1923. .exit_batch = ip6_tnl_exit_batch_net,
  1924. .id = &ip6_tnl_net_id,
  1925. .size = sizeof(struct ip6_tnl_net),
  1926. };
  1927. /**
  1928. * ip6_tunnel_init - register protocol and reserve needed resources
  1929. *
  1930. * Return: 0 on success
  1931. **/
  1932. static int __init ip6_tunnel_init(void)
  1933. {
  1934. int err;
  1935. if (!ipv6_mod_enabled())
  1936. return -EOPNOTSUPP;
  1937. err = register_pernet_device(&ip6_tnl_net_ops);
  1938. if (err < 0)
  1939. goto out_pernet;
  1940. err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
  1941. if (err < 0) {
  1942. pr_err("%s: can't register ip4ip6\n", __func__);
  1943. goto out_ip4ip6;
  1944. }
  1945. err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
  1946. if (err < 0) {
  1947. pr_err("%s: can't register ip6ip6\n", __func__);
  1948. goto out_ip6ip6;
  1949. }
  1950. err = rtnl_link_register(&ip6_link_ops);
  1951. if (err < 0)
  1952. goto rtnl_link_failed;
  1953. return 0;
  1954. rtnl_link_failed:
  1955. xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
  1956. out_ip6ip6:
  1957. xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
  1958. out_ip4ip6:
  1959. unregister_pernet_device(&ip6_tnl_net_ops);
  1960. out_pernet:
  1961. return err;
  1962. }
  1963. /**
  1964. * ip6_tunnel_cleanup - free resources and unregister protocol
  1965. **/
  1966. static void __exit ip6_tunnel_cleanup(void)
  1967. {
  1968. rtnl_link_unregister(&ip6_link_ops);
  1969. if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
  1970. pr_info("%s: can't deregister ip4ip6\n", __func__);
  1971. if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
  1972. pr_info("%s: can't deregister ip6ip6\n", __func__);
  1973. unregister_pernet_device(&ip6_tnl_net_ops);
  1974. }
  1975. module_init(ip6_tunnel_init);
  1976. module_exit(ip6_tunnel_cleanup);