ip6_tunnel.c 57 KB

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