ip6_tunnel.c 38 KB

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