ip6_tunnel.c 35 KB

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