ip6_tunnel.c 37 KB

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