ip6_tunnel.c 39 KB

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