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 <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. 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. };
  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 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. const struct in6_addr *remote = &p->raddr;
  278. const 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. const struct ipv6hdr *ipv6h = (const 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. const struct ipv6hdr *ipv6h = (const 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. const struct iphdr *eiph;
  470. struct rtable *rt;
  471. struct flowi4 fl4;
  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. rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
  505. eiph->saddr, 0,
  506. 0, 0,
  507. IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
  508. if (IS_ERR(rt))
  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. rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
  516. eiph->daddr, eiph->saddr,
  517. 0, 0,
  518. IPPROTO_IPIP,
  519. RT_TOS(eiph->tos), 0);
  520. if (IS_ERR(rt) ||
  521. rt->dst.dev->type != ARPHRD_TUNNEL) {
  522. if (!IS_ERR(rt))
  523. ip_rt_put(rt);
  524. goto out;
  525. }
  526. skb_dst_set(skb2, &rt->dst);
  527. } else {
  528. ip_rt_put(rt);
  529. if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
  530. skb2->dev) ||
  531. skb_dst(skb2)->dev->type != ARPHRD_TUNNEL)
  532. goto out;
  533. }
  534. /* change mtu on this route */
  535. if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
  536. if (rel_info > dst_mtu(skb_dst(skb2)))
  537. goto out;
  538. skb_dst(skb2)->ops->update_pmtu(skb_dst(skb2), rel_info);
  539. }
  540. icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
  541. out:
  542. kfree_skb(skb2);
  543. return 0;
  544. }
  545. static int
  546. ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  547. u8 type, u8 code, int offset, __be32 info)
  548. {
  549. int rel_msg = 0;
  550. u8 rel_type = type;
  551. u8 rel_code = code;
  552. __u32 rel_info = ntohl(info);
  553. int err;
  554. err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
  555. &rel_msg, &rel_info, offset);
  556. if (err < 0)
  557. return err;
  558. if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
  559. struct rt6_info *rt;
  560. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  561. if (!skb2)
  562. return 0;
  563. skb_dst_drop(skb2);
  564. skb_pull(skb2, offset);
  565. skb_reset_network_header(skb2);
  566. /* Try to guess incoming interface */
  567. rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr,
  568. NULL, 0, 0);
  569. if (rt && rt->rt6i_dev)
  570. skb2->dev = rt->rt6i_dev;
  571. icmpv6_send(skb2, rel_type, rel_code, rel_info);
  572. if (rt)
  573. dst_release(&rt->dst);
  574. kfree_skb(skb2);
  575. }
  576. return 0;
  577. }
  578. static void ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
  579. const struct ipv6hdr *ipv6h,
  580. struct sk_buff *skb)
  581. {
  582. __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
  583. if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
  584. ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
  585. if (INET_ECN_is_ce(dsfield))
  586. IP_ECN_set_ce(ip_hdr(skb));
  587. }
  588. static void ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
  589. const struct ipv6hdr *ipv6h,
  590. struct sk_buff *skb)
  591. {
  592. if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
  593. ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
  594. if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6h)))
  595. IP6_ECN_set_ce(ipv6_hdr(skb));
  596. }
  597. /* called with rcu_read_lock() */
  598. static inline int ip6_tnl_rcv_ctl(struct ip6_tnl *t)
  599. {
  600. struct ip6_tnl_parm *p = &t->parms;
  601. int ret = 0;
  602. struct net *net = dev_net(t->dev);
  603. if (p->flags & IP6_TNL_F_CAP_RCV) {
  604. struct net_device *ldev = NULL;
  605. if (p->link)
  606. ldev = dev_get_by_index_rcu(net, p->link);
  607. if ((ipv6_addr_is_multicast(&p->laddr) ||
  608. likely(ipv6_chk_addr(net, &p->laddr, ldev, 0))) &&
  609. likely(!ipv6_chk_addr(net, &p->raddr, NULL, 0)))
  610. ret = 1;
  611. }
  612. return ret;
  613. }
  614. /**
  615. * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
  616. * @skb: received socket buffer
  617. * @protocol: ethernet protocol ID
  618. * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
  619. *
  620. * Return: 0
  621. **/
  622. static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
  623. __u8 ipproto,
  624. void (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
  625. const struct ipv6hdr *ipv6h,
  626. struct sk_buff *skb))
  627. {
  628. struct ip6_tnl *t;
  629. const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  630. rcu_read_lock();
  631. if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr,
  632. &ipv6h->daddr)) != NULL) {
  633. struct pcpu_tstats *tstats;
  634. if (t->parms.proto != ipproto && t->parms.proto != 0) {
  635. rcu_read_unlock();
  636. goto discard;
  637. }
  638. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  639. rcu_read_unlock();
  640. goto discard;
  641. }
  642. if (!ip6_tnl_rcv_ctl(t)) {
  643. t->dev->stats.rx_dropped++;
  644. rcu_read_unlock();
  645. goto discard;
  646. }
  647. secpath_reset(skb);
  648. skb->mac_header = skb->network_header;
  649. skb_reset_network_header(skb);
  650. skb->protocol = htons(protocol);
  651. skb->pkt_type = PACKET_HOST;
  652. memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
  653. tstats = this_cpu_ptr(t->dev->tstats);
  654. tstats->rx_packets++;
  655. tstats->rx_bytes += skb->len;
  656. __skb_tunnel_rx(skb, t->dev);
  657. dscp_ecn_decapsulate(t, ipv6h, skb);
  658. netif_rx(skb);
  659. rcu_read_unlock();
  660. return 0;
  661. }
  662. rcu_read_unlock();
  663. return 1;
  664. discard:
  665. kfree_skb(skb);
  666. return 0;
  667. }
  668. static int ip4ip6_rcv(struct sk_buff *skb)
  669. {
  670. return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
  671. ip4ip6_dscp_ecn_decapsulate);
  672. }
  673. static int ip6ip6_rcv(struct sk_buff *skb)
  674. {
  675. return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
  676. ip6ip6_dscp_ecn_decapsulate);
  677. }
  678. struct ipv6_tel_txoption {
  679. struct ipv6_txoptions ops;
  680. __u8 dst_opt[8];
  681. };
  682. static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
  683. {
  684. memset(opt, 0, sizeof(struct ipv6_tel_txoption));
  685. opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
  686. opt->dst_opt[3] = 1;
  687. opt->dst_opt[4] = encap_limit;
  688. opt->dst_opt[5] = IPV6_TLV_PADN;
  689. opt->dst_opt[6] = 1;
  690. opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
  691. opt->ops.opt_nflen = 8;
  692. }
  693. /**
  694. * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  695. * @t: the outgoing tunnel device
  696. * @hdr: IPv6 header from the incoming packet
  697. *
  698. * Description:
  699. * Avoid trivial tunneling loop by checking that tunnel exit-point
  700. * doesn't match source of incoming packet.
  701. *
  702. * Return:
  703. * 1 if conflict,
  704. * 0 else
  705. **/
  706. static inline int
  707. ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
  708. {
  709. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  710. }
  711. static inline int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
  712. {
  713. struct ip6_tnl_parm *p = &t->parms;
  714. int ret = 0;
  715. struct net *net = dev_net(t->dev);
  716. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  717. struct net_device *ldev = NULL;
  718. rcu_read_lock();
  719. if (p->link)
  720. ldev = dev_get_by_index_rcu(net, p->link);
  721. if (unlikely(!ipv6_chk_addr(net, &p->laddr, ldev, 0)))
  722. printk(KERN_WARNING
  723. "%s xmit: Local address not yet configured!\n",
  724. p->name);
  725. else if (!ipv6_addr_is_multicast(&p->raddr) &&
  726. unlikely(ipv6_chk_addr(net, &p->raddr, NULL, 0)))
  727. printk(KERN_WARNING
  728. "%s xmit: Routing loop! "
  729. "Remote address found on this node!\n",
  730. p->name);
  731. else
  732. ret = 1;
  733. rcu_read_unlock();
  734. }
  735. return ret;
  736. }
  737. /**
  738. * ip6_tnl_xmit2 - encapsulate packet and send
  739. * @skb: the outgoing socket buffer
  740. * @dev: the outgoing tunnel device
  741. * @dsfield: dscp code for outer header
  742. * @fl: flow of tunneled packet
  743. * @encap_limit: encapsulation limit
  744. * @pmtu: Path MTU is stored if packet is too big
  745. *
  746. * Description:
  747. * Build new header and do some sanity checks on the packet before sending
  748. * it.
  749. *
  750. * Return:
  751. * 0 on success
  752. * -1 fail
  753. * %-EMSGSIZE message too big. return mtu in this case.
  754. **/
  755. static int ip6_tnl_xmit2(struct sk_buff *skb,
  756. struct net_device *dev,
  757. __u8 dsfield,
  758. struct flowi6 *fl6,
  759. int encap_limit,
  760. __u32 *pmtu)
  761. {
  762. struct net *net = dev_net(dev);
  763. struct ip6_tnl *t = netdev_priv(dev);
  764. struct net_device_stats *stats = &t->dev->stats;
  765. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  766. struct ipv6_tel_txoption opt;
  767. struct dst_entry *dst;
  768. struct net_device *tdev;
  769. int mtu;
  770. unsigned int max_headroom = sizeof(struct ipv6hdr);
  771. u8 proto;
  772. int err = -1;
  773. int pkt_len;
  774. if ((dst = ip6_tnl_dst_check(t)) != NULL)
  775. dst_hold(dst);
  776. else {
  777. dst = ip6_route_output(net, NULL, fl6);
  778. if (dst->error)
  779. goto tx_err_link_failure;
  780. dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), NULL, 0);
  781. if (IS_ERR(dst)) {
  782. err = PTR_ERR(dst);
  783. dst = NULL;
  784. goto tx_err_link_failure;
  785. }
  786. }
  787. tdev = dst->dev;
  788. if (tdev == dev) {
  789. stats->collisions++;
  790. if (net_ratelimit())
  791. printk(KERN_WARNING
  792. "%s: Local routing loop detected!\n",
  793. t->parms.name);
  794. goto tx_err_dst_release;
  795. }
  796. mtu = dst_mtu(dst) - sizeof (*ipv6h);
  797. if (encap_limit >= 0) {
  798. max_headroom += 8;
  799. mtu -= 8;
  800. }
  801. if (mtu < IPV6_MIN_MTU)
  802. mtu = IPV6_MIN_MTU;
  803. if (skb_dst(skb))
  804. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
  805. if (skb->len > mtu) {
  806. *pmtu = mtu;
  807. err = -EMSGSIZE;
  808. goto tx_err_dst_release;
  809. }
  810. /*
  811. * Okay, now see if we can stuff it in the buffer as-is.
  812. */
  813. max_headroom += LL_RESERVED_SPACE(tdev);
  814. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  815. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  816. struct sk_buff *new_skb;
  817. if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
  818. goto tx_err_dst_release;
  819. if (skb->sk)
  820. skb_set_owner_w(new_skb, skb->sk);
  821. kfree_skb(skb);
  822. skb = new_skb;
  823. }
  824. skb_dst_drop(skb);
  825. skb_dst_set(skb, dst_clone(dst));
  826. skb->transport_header = skb->network_header;
  827. proto = fl6->flowi6_proto;
  828. if (encap_limit >= 0) {
  829. init_tel_txopt(&opt, encap_limit);
  830. ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
  831. }
  832. skb_push(skb, sizeof(struct ipv6hdr));
  833. skb_reset_network_header(skb);
  834. ipv6h = ipv6_hdr(skb);
  835. *(__be32*)ipv6h = fl6->flowlabel | htonl(0x60000000);
  836. dsfield = INET_ECN_encapsulate(0, dsfield);
  837. ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
  838. ipv6h->hop_limit = t->parms.hop_limit;
  839. ipv6h->nexthdr = proto;
  840. ipv6_addr_copy(&ipv6h->saddr, &fl6->saddr);
  841. ipv6_addr_copy(&ipv6h->daddr, &fl6->daddr);
  842. nf_reset(skb);
  843. pkt_len = skb->len;
  844. err = ip6_local_out(skb);
  845. if (net_xmit_eval(err) == 0) {
  846. struct pcpu_tstats *tstats = this_cpu_ptr(t->dev->tstats);
  847. tstats->tx_bytes += pkt_len;
  848. tstats->tx_packets++;
  849. } else {
  850. stats->tx_errors++;
  851. stats->tx_aborted_errors++;
  852. }
  853. ip6_tnl_dst_store(t, dst);
  854. return 0;
  855. tx_err_link_failure:
  856. stats->tx_carrier_errors++;
  857. dst_link_failure(skb);
  858. tx_err_dst_release:
  859. dst_release(dst);
  860. return err;
  861. }
  862. static inline int
  863. ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  864. {
  865. struct ip6_tnl *t = netdev_priv(dev);
  866. const struct iphdr *iph = ip_hdr(skb);
  867. int encap_limit = -1;
  868. struct flowi6 fl6;
  869. __u8 dsfield;
  870. __u32 mtu;
  871. int err;
  872. if ((t->parms.proto != IPPROTO_IPIP && t->parms.proto != 0) ||
  873. !ip6_tnl_xmit_ctl(t))
  874. return -1;
  875. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  876. encap_limit = t->parms.encap_limit;
  877. memcpy(&fl6, &t->fl.u.ip6, sizeof (fl6));
  878. fl6.flowi6_proto = IPPROTO_IPIP;
  879. dsfield = ipv4_get_dsfield(iph);
  880. if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
  881. fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
  882. & IPV6_TCLASS_MASK;
  883. err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
  884. if (err != 0) {
  885. /* XXX: send ICMP error even if DF is not set. */
  886. if (err == -EMSGSIZE)
  887. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  888. htonl(mtu));
  889. return -1;
  890. }
  891. return 0;
  892. }
  893. static inline int
  894. ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  895. {
  896. struct ip6_tnl *t = netdev_priv(dev);
  897. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  898. int encap_limit = -1;
  899. __u16 offset;
  900. struct flowi6 fl6;
  901. __u8 dsfield;
  902. __u32 mtu;
  903. int err;
  904. if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
  905. !ip6_tnl_xmit_ctl(t) || ip6_tnl_addr_conflict(t, ipv6h))
  906. return -1;
  907. offset = parse_tlv_tnl_enc_lim(skb, skb_network_header(skb));
  908. if (offset > 0) {
  909. struct ipv6_tlv_tnl_enc_lim *tel;
  910. tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
  911. if (tel->encap_limit == 0) {
  912. icmpv6_send(skb, ICMPV6_PARAMPROB,
  913. ICMPV6_HDR_FIELD, offset + 2);
  914. return -1;
  915. }
  916. encap_limit = tel->encap_limit - 1;
  917. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  918. encap_limit = t->parms.encap_limit;
  919. memcpy(&fl6, &t->fl.u.ip6, sizeof (fl6));
  920. fl6.flowi6_proto = IPPROTO_IPV6;
  921. dsfield = ipv6_get_dsfield(ipv6h);
  922. if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
  923. fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
  924. if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
  925. fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
  926. err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
  927. if (err != 0) {
  928. if (err == -EMSGSIZE)
  929. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  930. return -1;
  931. }
  932. return 0;
  933. }
  934. static netdev_tx_t
  935. ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  936. {
  937. struct ip6_tnl *t = netdev_priv(dev);
  938. struct net_device_stats *stats = &t->dev->stats;
  939. int ret;
  940. switch (skb->protocol) {
  941. case htons(ETH_P_IP):
  942. ret = ip4ip6_tnl_xmit(skb, dev);
  943. break;
  944. case htons(ETH_P_IPV6):
  945. ret = ip6ip6_tnl_xmit(skb, dev);
  946. break;
  947. default:
  948. goto tx_err;
  949. }
  950. if (ret < 0)
  951. goto tx_err;
  952. return NETDEV_TX_OK;
  953. tx_err:
  954. stats->tx_errors++;
  955. stats->tx_dropped++;
  956. kfree_skb(skb);
  957. return NETDEV_TX_OK;
  958. }
  959. static void ip6_tnl_set_cap(struct ip6_tnl *t)
  960. {
  961. struct ip6_tnl_parm *p = &t->parms;
  962. int ltype = ipv6_addr_type(&p->laddr);
  963. int rtype = ipv6_addr_type(&p->raddr);
  964. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV);
  965. if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
  966. rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
  967. !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
  968. (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
  969. if (ltype&IPV6_ADDR_UNICAST)
  970. p->flags |= IP6_TNL_F_CAP_XMIT;
  971. if (rtype&IPV6_ADDR_UNICAST)
  972. p->flags |= IP6_TNL_F_CAP_RCV;
  973. }
  974. }
  975. static void ip6_tnl_link_config(struct ip6_tnl *t)
  976. {
  977. struct net_device *dev = t->dev;
  978. struct ip6_tnl_parm *p = &t->parms;
  979. struct flowi6 *fl6 = &t->fl.u.ip6;
  980. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  981. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  982. /* Set up flowi template */
  983. ipv6_addr_copy(&fl6->saddr, &p->laddr);
  984. ipv6_addr_copy(&fl6->daddr, &p->raddr);
  985. fl6->flowi6_oif = p->link;
  986. fl6->flowlabel = 0;
  987. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  988. fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  989. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  990. fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  991. ip6_tnl_set_cap(t);
  992. if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
  993. dev->flags |= IFF_POINTOPOINT;
  994. else
  995. dev->flags &= ~IFF_POINTOPOINT;
  996. dev->iflink = p->link;
  997. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  998. int strict = (ipv6_addr_type(&p->raddr) &
  999. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  1000. struct rt6_info *rt = rt6_lookup(dev_net(dev),
  1001. &p->raddr, &p->laddr,
  1002. p->link, strict);
  1003. if (rt == NULL)
  1004. return;
  1005. if (rt->rt6i_dev) {
  1006. dev->hard_header_len = rt->rt6i_dev->hard_header_len +
  1007. sizeof (struct ipv6hdr);
  1008. dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr);
  1009. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1010. dev->mtu-=8;
  1011. if (dev->mtu < IPV6_MIN_MTU)
  1012. dev->mtu = IPV6_MIN_MTU;
  1013. }
  1014. dst_release(&rt->dst);
  1015. }
  1016. }
  1017. /**
  1018. * ip6_tnl_change - update the tunnel parameters
  1019. * @t: tunnel to be changed
  1020. * @p: tunnel configuration parameters
  1021. *
  1022. * Description:
  1023. * ip6_tnl_change() updates the tunnel parameters
  1024. **/
  1025. static int
  1026. ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
  1027. {
  1028. ipv6_addr_copy(&t->parms.laddr, &p->laddr);
  1029. ipv6_addr_copy(&t->parms.raddr, &p->raddr);
  1030. t->parms.flags = p->flags;
  1031. t->parms.hop_limit = p->hop_limit;
  1032. t->parms.encap_limit = p->encap_limit;
  1033. t->parms.flowinfo = p->flowinfo;
  1034. t->parms.link = p->link;
  1035. t->parms.proto = p->proto;
  1036. ip6_tnl_dst_reset(t);
  1037. ip6_tnl_link_config(t);
  1038. return 0;
  1039. }
  1040. /**
  1041. * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
  1042. * @dev: virtual device associated with tunnel
  1043. * @ifr: parameters passed from userspace
  1044. * @cmd: command to be performed
  1045. *
  1046. * Description:
  1047. * ip6_tnl_ioctl() is used for managing IPv6 tunnels
  1048. * from userspace.
  1049. *
  1050. * The possible commands are the following:
  1051. * %SIOCGETTUNNEL: get tunnel parameters for device
  1052. * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
  1053. * %SIOCCHGTUNNEL: change tunnel parameters to those given
  1054. * %SIOCDELTUNNEL: delete tunnel
  1055. *
  1056. * The fallback device "ip6tnl0", created during module
  1057. * initialization, can be used for creating other tunnel devices.
  1058. *
  1059. * Return:
  1060. * 0 on success,
  1061. * %-EFAULT if unable to copy data to or from userspace,
  1062. * %-EPERM if current process hasn't %CAP_NET_ADMIN set
  1063. * %-EINVAL if passed tunnel parameters are invalid,
  1064. * %-EEXIST if changing a tunnel's parameters would cause a conflict
  1065. * %-ENODEV if attempting to change or delete a nonexisting device
  1066. **/
  1067. static int
  1068. ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1069. {
  1070. int err = 0;
  1071. struct ip6_tnl_parm p;
  1072. struct ip6_tnl *t = NULL;
  1073. struct net *net = dev_net(dev);
  1074. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1075. switch (cmd) {
  1076. case SIOCGETTUNNEL:
  1077. if (dev == ip6n->fb_tnl_dev) {
  1078. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
  1079. err = -EFAULT;
  1080. break;
  1081. }
  1082. t = ip6_tnl_locate(net, &p, 0);
  1083. }
  1084. if (t == NULL)
  1085. t = netdev_priv(dev);
  1086. memcpy(&p, &t->parms, sizeof (p));
  1087. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
  1088. err = -EFAULT;
  1089. }
  1090. break;
  1091. case SIOCADDTUNNEL:
  1092. case SIOCCHGTUNNEL:
  1093. err = -EPERM;
  1094. if (!capable(CAP_NET_ADMIN))
  1095. break;
  1096. err = -EFAULT;
  1097. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
  1098. break;
  1099. err = -EINVAL;
  1100. if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
  1101. p.proto != 0)
  1102. break;
  1103. t = ip6_tnl_locate(net, &p, cmd == SIOCADDTUNNEL);
  1104. if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
  1105. if (t != NULL) {
  1106. if (t->dev != dev) {
  1107. err = -EEXIST;
  1108. break;
  1109. }
  1110. } else
  1111. t = netdev_priv(dev);
  1112. ip6_tnl_unlink(ip6n, t);
  1113. synchronize_net();
  1114. err = ip6_tnl_change(t, &p);
  1115. ip6_tnl_link(ip6n, t);
  1116. netdev_state_change(dev);
  1117. }
  1118. if (t) {
  1119. err = 0;
  1120. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof (p)))
  1121. err = -EFAULT;
  1122. } else
  1123. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  1124. break;
  1125. case SIOCDELTUNNEL:
  1126. err = -EPERM;
  1127. if (!capable(CAP_NET_ADMIN))
  1128. break;
  1129. if (dev == ip6n->fb_tnl_dev) {
  1130. err = -EFAULT;
  1131. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
  1132. break;
  1133. err = -ENOENT;
  1134. if ((t = ip6_tnl_locate(net, &p, 0)) == NULL)
  1135. break;
  1136. err = -EPERM;
  1137. if (t->dev == ip6n->fb_tnl_dev)
  1138. break;
  1139. dev = t->dev;
  1140. }
  1141. err = 0;
  1142. unregister_netdevice(dev);
  1143. break;
  1144. default:
  1145. err = -EINVAL;
  1146. }
  1147. return err;
  1148. }
  1149. /**
  1150. * ip6_tnl_change_mtu - change mtu manually for tunnel device
  1151. * @dev: virtual device associated with tunnel
  1152. * @new_mtu: the new mtu
  1153. *
  1154. * Return:
  1155. * 0 on success,
  1156. * %-EINVAL if mtu too small
  1157. **/
  1158. static int
  1159. ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
  1160. {
  1161. if (new_mtu < IPV6_MIN_MTU) {
  1162. return -EINVAL;
  1163. }
  1164. dev->mtu = new_mtu;
  1165. return 0;
  1166. }
  1167. static const struct net_device_ops ip6_tnl_netdev_ops = {
  1168. .ndo_uninit = ip6_tnl_dev_uninit,
  1169. .ndo_start_xmit = ip6_tnl_xmit,
  1170. .ndo_do_ioctl = ip6_tnl_ioctl,
  1171. .ndo_change_mtu = ip6_tnl_change_mtu,
  1172. .ndo_get_stats = ip6_get_stats,
  1173. };
  1174. /**
  1175. * ip6_tnl_dev_setup - setup virtual tunnel device
  1176. * @dev: virtual device associated with tunnel
  1177. *
  1178. * Description:
  1179. * Initialize function pointers and device parameters
  1180. **/
  1181. static void ip6_tnl_dev_setup(struct net_device *dev)
  1182. {
  1183. struct ip6_tnl *t;
  1184. dev->netdev_ops = &ip6_tnl_netdev_ops;
  1185. dev->destructor = ip6_dev_free;
  1186. dev->type = ARPHRD_TUNNEL6;
  1187. dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
  1188. dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
  1189. t = netdev_priv(dev);
  1190. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1191. dev->mtu-=8;
  1192. dev->flags |= IFF_NOARP;
  1193. dev->addr_len = sizeof(struct in6_addr);
  1194. dev->features |= NETIF_F_NETNS_LOCAL;
  1195. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1196. }
  1197. /**
  1198. * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
  1199. * @dev: virtual device associated with tunnel
  1200. **/
  1201. static inline int
  1202. ip6_tnl_dev_init_gen(struct net_device *dev)
  1203. {
  1204. struct ip6_tnl *t = netdev_priv(dev);
  1205. t->dev = dev;
  1206. strcpy(t->parms.name, dev->name);
  1207. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1208. if (!dev->tstats)
  1209. return -ENOMEM;
  1210. return 0;
  1211. }
  1212. /**
  1213. * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
  1214. * @dev: virtual device associated with tunnel
  1215. **/
  1216. static int ip6_tnl_dev_init(struct net_device *dev)
  1217. {
  1218. struct ip6_tnl *t = netdev_priv(dev);
  1219. int err = ip6_tnl_dev_init_gen(dev);
  1220. if (err)
  1221. return err;
  1222. ip6_tnl_link_config(t);
  1223. return 0;
  1224. }
  1225. /**
  1226. * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
  1227. * @dev: fallback device
  1228. *
  1229. * Return: 0
  1230. **/
  1231. static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
  1232. {
  1233. struct ip6_tnl *t = netdev_priv(dev);
  1234. struct net *net = dev_net(dev);
  1235. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1236. int err = ip6_tnl_dev_init_gen(dev);
  1237. if (err)
  1238. return err;
  1239. t->parms.proto = IPPROTO_IPV6;
  1240. dev_hold(dev);
  1241. rcu_assign_pointer(ip6n->tnls_wc[0], t);
  1242. return 0;
  1243. }
  1244. static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
  1245. .handler = ip4ip6_rcv,
  1246. .err_handler = ip4ip6_err,
  1247. .priority = 1,
  1248. };
  1249. static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
  1250. .handler = ip6ip6_rcv,
  1251. .err_handler = ip6ip6_err,
  1252. .priority = 1,
  1253. };
  1254. static void __net_exit ip6_tnl_destroy_tunnels(struct ip6_tnl_net *ip6n)
  1255. {
  1256. int h;
  1257. struct ip6_tnl *t;
  1258. LIST_HEAD(list);
  1259. for (h = 0; h < HASH_SIZE; h++) {
  1260. t = rtnl_dereference(ip6n->tnls_r_l[h]);
  1261. while (t != NULL) {
  1262. unregister_netdevice_queue(t->dev, &list);
  1263. t = rtnl_dereference(t->next);
  1264. }
  1265. }
  1266. t = rtnl_dereference(ip6n->tnls_wc[0]);
  1267. unregister_netdevice_queue(t->dev, &list);
  1268. unregister_netdevice_many(&list);
  1269. }
  1270. static int __net_init ip6_tnl_init_net(struct net *net)
  1271. {
  1272. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1273. int err;
  1274. ip6n->tnls[0] = ip6n->tnls_wc;
  1275. ip6n->tnls[1] = ip6n->tnls_r_l;
  1276. err = -ENOMEM;
  1277. ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
  1278. ip6_tnl_dev_setup);
  1279. if (!ip6n->fb_tnl_dev)
  1280. goto err_alloc_dev;
  1281. dev_net_set(ip6n->fb_tnl_dev, net);
  1282. err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
  1283. if (err < 0)
  1284. goto err_register;
  1285. err = register_netdev(ip6n->fb_tnl_dev);
  1286. if (err < 0)
  1287. goto err_register;
  1288. return 0;
  1289. err_register:
  1290. ip6_dev_free(ip6n->fb_tnl_dev);
  1291. err_alloc_dev:
  1292. return err;
  1293. }
  1294. static void __net_exit ip6_tnl_exit_net(struct net *net)
  1295. {
  1296. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1297. rtnl_lock();
  1298. ip6_tnl_destroy_tunnels(ip6n);
  1299. rtnl_unlock();
  1300. }
  1301. static struct pernet_operations ip6_tnl_net_ops = {
  1302. .init = ip6_tnl_init_net,
  1303. .exit = ip6_tnl_exit_net,
  1304. .id = &ip6_tnl_net_id,
  1305. .size = sizeof(struct ip6_tnl_net),
  1306. };
  1307. /**
  1308. * ip6_tunnel_init - register protocol and reserve needed resources
  1309. *
  1310. * Return: 0 on success
  1311. **/
  1312. static int __init ip6_tunnel_init(void)
  1313. {
  1314. int err;
  1315. err = register_pernet_device(&ip6_tnl_net_ops);
  1316. if (err < 0)
  1317. goto out_pernet;
  1318. err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
  1319. if (err < 0) {
  1320. printk(KERN_ERR "ip6_tunnel init: can't register ip4ip6\n");
  1321. goto out_ip4ip6;
  1322. }
  1323. err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
  1324. if (err < 0) {
  1325. printk(KERN_ERR "ip6_tunnel init: can't register ip6ip6\n");
  1326. goto out_ip6ip6;
  1327. }
  1328. return 0;
  1329. out_ip6ip6:
  1330. xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
  1331. out_ip4ip6:
  1332. unregister_pernet_device(&ip6_tnl_net_ops);
  1333. out_pernet:
  1334. return err;
  1335. }
  1336. /**
  1337. * ip6_tunnel_cleanup - free resources and unregister protocol
  1338. **/
  1339. static void __exit ip6_tunnel_cleanup(void)
  1340. {
  1341. if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
  1342. printk(KERN_INFO "ip6_tunnel close: can't deregister ip4ip6\n");
  1343. if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
  1344. printk(KERN_INFO "ip6_tunnel close: can't deregister ip6ip6\n");
  1345. unregister_pernet_device(&ip6_tnl_net_ops);
  1346. }
  1347. module_init(ip6_tunnel_init);
  1348. module_exit(ip6_tunnel_cleanup);