ip6_tunnel.c 35 KB

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