ip6_tunnel.c 33 KB

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