ip6_tunnel.c 37 KB

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