ip6_tunnel.c 35 KB

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