ip6_tunnel.c 35 KB

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