ip6_tunnel.c 33 KB

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