ip6_tunnel.c 36 KB

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