ip6_tunnel.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508
  1. /*
  2. * IPv6 tunneling device
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Ville Nuorvala <vnuorval@tcs.hut.fi>
  7. * Yasuyuki Kozakai <kozakai@linux-ipv6.org>
  8. *
  9. * Based on:
  10. * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
  11. *
  12. * RFC 2473
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. *
  19. */
  20. #include <linux/module.h>
  21. #include <linux/capability.h>
  22. #include <linux/errno.h>
  23. #include <linux/types.h>
  24. #include <linux/sockios.h>
  25. #include <linux/icmp.h>
  26. #include <linux/if.h>
  27. #include <linux/in.h>
  28. #include <linux/ip.h>
  29. #include <linux/if_tunnel.h>
  30. #include <linux/net.h>
  31. #include <linux/in6.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/if_arp.h>
  34. #include <linux/icmpv6.h>
  35. #include <linux/init.h>
  36. #include <linux/route.h>
  37. #include <linux/rtnetlink.h>
  38. #include <linux/netfilter_ipv6.h>
  39. #include <asm/uaccess.h>
  40. #include <asm/atomic.h>
  41. #include <net/icmp.h>
  42. #include <net/ip.h>
  43. #include <net/ipv6.h>
  44. #include <net/ip6_route.h>
  45. #include <net/addrconf.h>
  46. #include <net/ip6_tunnel.h>
  47. #include <net/xfrm.h>
  48. #include <net/dsfield.h>
  49. #include <net/inet_ecn.h>
  50. #include <net/net_namespace.h>
  51. #include <net/netns/generic.h>
  52. MODULE_AUTHOR("Ville Nuorvala");
  53. MODULE_DESCRIPTION("IPv6 tunneling device");
  54. MODULE_LICENSE("GPL");
  55. #define IPV6_TLV_TEL_DST_SIZE 8
  56. #ifdef IP6_TNL_DEBUG
  57. #define IP6_TNL_TRACE(x...) printk(KERN_DEBUG "%s:" x "\n", __func__)
  58. #else
  59. #define IP6_TNL_TRACE(x...) do {;} while(0)
  60. #endif
  61. #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
  62. #define IPV6_TCLASS_SHIFT 20
  63. #define HASH_SIZE 32
  64. #define HASH(addr) ((__force u32)((addr)->s6_addr32[0] ^ (addr)->s6_addr32[1] ^ \
  65. (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \
  66. (HASH_SIZE - 1))
  67. static void ip6_fb_tnl_dev_init(struct net_device *dev);
  68. static void 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. t->parms = *p;
  216. ip6_tnl_dev_init(dev);
  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. u8 *type, u8 *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. u8 rel_type = ICMPV6_DEST_UNREACH;
  349. u8 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. u8 type, u8 code, int offset, __be32 info)
  428. {
  429. int rel_msg = 0;
  430. u8 rel_type = type;
  431. u8 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. skb_dst_drop(skb2);
  466. skb_pull(skb2, offset);
  467. skb_reset_network_header(skb2);
  468. eiph = ip_hdr(skb2);
  469. /* Try to guess incoming interface */
  470. memset(&fl, 0, sizeof(fl));
  471. fl.fl4_dst = eiph->saddr;
  472. fl.fl4_tos = RT_TOS(eiph->tos);
  473. fl.proto = IPPROTO_IPIP;
  474. if (ip_route_output_key(dev_net(skb->dev), &rt, &fl))
  475. goto out;
  476. skb2->dev = rt->u.dst.dev;
  477. /* route "incoming" packet */
  478. if (rt->rt_flags & RTCF_LOCAL) {
  479. ip_rt_put(rt);
  480. rt = NULL;
  481. fl.fl4_dst = eiph->daddr;
  482. fl.fl4_src = eiph->saddr;
  483. fl.fl4_tos = eiph->tos;
  484. if (ip_route_output_key(dev_net(skb->dev), &rt, &fl) ||
  485. rt->u.dst.dev->type != ARPHRD_TUNNEL) {
  486. ip_rt_put(rt);
  487. goto out;
  488. }
  489. skb_dst_set(skb2, (struct dst_entry *)rt);
  490. } else {
  491. ip_rt_put(rt);
  492. if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
  493. skb2->dev) ||
  494. skb_dst(skb2)->dev->type != ARPHRD_TUNNEL)
  495. goto out;
  496. }
  497. /* change mtu on this route */
  498. if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
  499. if (rel_info > dst_mtu(skb_dst(skb2)))
  500. goto out;
  501. skb_dst(skb2)->ops->update_pmtu(skb_dst(skb2), rel_info);
  502. }
  503. icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
  504. out:
  505. kfree_skb(skb2);
  506. return 0;
  507. }
  508. static int
  509. ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  510. u8 type, u8 code, int offset, __be32 info)
  511. {
  512. int rel_msg = 0;
  513. u8 rel_type = type;
  514. u8 rel_code = code;
  515. __u32 rel_info = ntohl(info);
  516. int err;
  517. err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
  518. &rel_msg, &rel_info, offset);
  519. if (err < 0)
  520. return err;
  521. if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
  522. struct rt6_info *rt;
  523. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  524. if (!skb2)
  525. return 0;
  526. skb_dst_drop(skb2);
  527. skb_pull(skb2, offset);
  528. skb_reset_network_header(skb2);
  529. /* Try to guess incoming interface */
  530. rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr,
  531. NULL, 0, 0);
  532. if (rt && rt->rt6i_dev)
  533. skb2->dev = rt->rt6i_dev;
  534. icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
  535. if (rt)
  536. dst_release(&rt->u.dst);
  537. kfree_skb(skb2);
  538. }
  539. return 0;
  540. }
  541. static void ip4ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
  542. struct ipv6hdr *ipv6h,
  543. struct sk_buff *skb)
  544. {
  545. __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
  546. if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
  547. ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
  548. if (INET_ECN_is_ce(dsfield))
  549. IP_ECN_set_ce(ip_hdr(skb));
  550. }
  551. static void ip6ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
  552. struct ipv6hdr *ipv6h,
  553. struct sk_buff *skb)
  554. {
  555. if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
  556. ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
  557. if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6h)))
  558. IP6_ECN_set_ce(ipv6_hdr(skb));
  559. }
  560. static inline int ip6_tnl_rcv_ctl(struct ip6_tnl *t)
  561. {
  562. struct ip6_tnl_parm *p = &t->parms;
  563. int ret = 0;
  564. struct net *net = dev_net(t->dev);
  565. if (p->flags & IP6_TNL_F_CAP_RCV) {
  566. struct net_device *ldev = NULL;
  567. if (p->link)
  568. ldev = dev_get_by_index(net, p->link);
  569. if ((ipv6_addr_is_multicast(&p->laddr) ||
  570. likely(ipv6_chk_addr(net, &p->laddr, ldev, 0))) &&
  571. likely(!ipv6_chk_addr(net, &p->raddr, NULL, 0)))
  572. ret = 1;
  573. if (ldev)
  574. dev_put(ldev);
  575. }
  576. return ret;
  577. }
  578. /**
  579. * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
  580. * @skb: received socket buffer
  581. * @protocol: ethernet protocol ID
  582. * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
  583. *
  584. * Return: 0
  585. **/
  586. static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
  587. __u8 ipproto,
  588. void (*dscp_ecn_decapsulate)(struct ip6_tnl *t,
  589. struct ipv6hdr *ipv6h,
  590. struct sk_buff *skb))
  591. {
  592. struct ip6_tnl *t;
  593. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  594. read_lock(&ip6_tnl_lock);
  595. if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr,
  596. &ipv6h->daddr)) != NULL) {
  597. if (t->parms.proto != ipproto && t->parms.proto != 0) {
  598. read_unlock(&ip6_tnl_lock);
  599. goto discard;
  600. }
  601. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  602. read_unlock(&ip6_tnl_lock);
  603. goto discard;
  604. }
  605. if (!ip6_tnl_rcv_ctl(t)) {
  606. t->dev->stats.rx_dropped++;
  607. read_unlock(&ip6_tnl_lock);
  608. goto discard;
  609. }
  610. secpath_reset(skb);
  611. skb->mac_header = skb->network_header;
  612. skb_reset_network_header(skb);
  613. skb->protocol = htons(protocol);
  614. skb->pkt_type = PACKET_HOST;
  615. memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
  616. skb->dev = t->dev;
  617. skb_dst_drop(skb);
  618. nf_reset(skb);
  619. dscp_ecn_decapsulate(t, ipv6h, skb);
  620. t->dev->stats.rx_packets++;
  621. t->dev->stats.rx_bytes += skb->len;
  622. netif_rx(skb);
  623. read_unlock(&ip6_tnl_lock);
  624. return 0;
  625. }
  626. read_unlock(&ip6_tnl_lock);
  627. return 1;
  628. discard:
  629. kfree_skb(skb);
  630. return 0;
  631. }
  632. static int ip4ip6_rcv(struct sk_buff *skb)
  633. {
  634. return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
  635. ip4ip6_dscp_ecn_decapsulate);
  636. }
  637. static int ip6ip6_rcv(struct sk_buff *skb)
  638. {
  639. return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
  640. ip6ip6_dscp_ecn_decapsulate);
  641. }
  642. struct ipv6_tel_txoption {
  643. struct ipv6_txoptions ops;
  644. __u8 dst_opt[8];
  645. };
  646. static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
  647. {
  648. memset(opt, 0, sizeof(struct ipv6_tel_txoption));
  649. opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
  650. opt->dst_opt[3] = 1;
  651. opt->dst_opt[4] = encap_limit;
  652. opt->dst_opt[5] = IPV6_TLV_PADN;
  653. opt->dst_opt[6] = 1;
  654. opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
  655. opt->ops.opt_nflen = 8;
  656. }
  657. /**
  658. * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  659. * @t: the outgoing tunnel device
  660. * @hdr: IPv6 header from the incoming packet
  661. *
  662. * Description:
  663. * Avoid trivial tunneling loop by checking that tunnel exit-point
  664. * doesn't match source of incoming packet.
  665. *
  666. * Return:
  667. * 1 if conflict,
  668. * 0 else
  669. **/
  670. static inline int
  671. ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
  672. {
  673. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  674. }
  675. static inline int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
  676. {
  677. struct ip6_tnl_parm *p = &t->parms;
  678. int ret = 0;
  679. struct net *net = dev_net(t->dev);
  680. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  681. struct net_device *ldev = NULL;
  682. if (p->link)
  683. ldev = dev_get_by_index(net, p->link);
  684. if (unlikely(!ipv6_chk_addr(net, &p->laddr, ldev, 0)))
  685. printk(KERN_WARNING
  686. "%s xmit: Local address not yet configured!\n",
  687. p->name);
  688. else if (!ipv6_addr_is_multicast(&p->raddr) &&
  689. unlikely(ipv6_chk_addr(net, &p->raddr, NULL, 0)))
  690. printk(KERN_WARNING
  691. "%s xmit: Routing loop! "
  692. "Remote address found on this node!\n",
  693. p->name);
  694. else
  695. ret = 1;
  696. if (ldev)
  697. dev_put(ldev);
  698. }
  699. return ret;
  700. }
  701. /**
  702. * ip6_tnl_xmit2 - encapsulate packet and send
  703. * @skb: the outgoing socket buffer
  704. * @dev: the outgoing tunnel device
  705. * @dsfield: dscp code for outer header
  706. * @fl: flow of tunneled packet
  707. * @encap_limit: encapsulation limit
  708. * @pmtu: Path MTU is stored if packet is too big
  709. *
  710. * Description:
  711. * Build new header and do some sanity checks on the packet before sending
  712. * it.
  713. *
  714. * Return:
  715. * 0 on success
  716. * -1 fail
  717. * %-EMSGSIZE message too big. return mtu in this case.
  718. **/
  719. static int ip6_tnl_xmit2(struct sk_buff *skb,
  720. struct net_device *dev,
  721. __u8 dsfield,
  722. struct flowi *fl,
  723. int encap_limit,
  724. __u32 *pmtu)
  725. {
  726. struct net *net = dev_net(dev);
  727. struct ip6_tnl *t = netdev_priv(dev);
  728. struct net_device_stats *stats = &t->dev->stats;
  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(net, NULL, fl);
  742. if (dst->error || xfrm_lookup(net, &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(skb))
  762. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), 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. skb_dst_drop(skb);
  783. skb_dst_set(skb, 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 netdev_tx_t
  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->dev->stats;
  896. int ret;
  897. switch (skb->protocol) {
  898. case htons(ETH_P_IP):
  899. ret = ip4ip6_tnl_xmit(skb, dev);
  900. break;
  901. case htons(ETH_P_IPV6):
  902. ret = ip6ip6_tnl_xmit(skb, dev);
  903. break;
  904. default:
  905. goto tx_err;
  906. }
  907. if (ret < 0)
  908. goto tx_err;
  909. return NETDEV_TX_OK;
  910. tx_err:
  911. stats->tx_errors++;
  912. stats->tx_dropped++;
  913. kfree_skb(skb);
  914. return NETDEV_TX_OK;
  915. }
  916. static void ip6_tnl_set_cap(struct ip6_tnl *t)
  917. {
  918. struct ip6_tnl_parm *p = &t->parms;
  919. int ltype = ipv6_addr_type(&p->laddr);
  920. int rtype = ipv6_addr_type(&p->raddr);
  921. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV);
  922. if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
  923. rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
  924. !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
  925. (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
  926. if (ltype&IPV6_ADDR_UNICAST)
  927. p->flags |= IP6_TNL_F_CAP_XMIT;
  928. if (rtype&IPV6_ADDR_UNICAST)
  929. p->flags |= IP6_TNL_F_CAP_RCV;
  930. }
  931. }
  932. static void ip6_tnl_link_config(struct ip6_tnl *t)
  933. {
  934. struct net_device *dev = t->dev;
  935. struct ip6_tnl_parm *p = &t->parms;
  936. struct flowi *fl = &t->fl;
  937. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  938. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  939. /* Set up flowi template */
  940. ipv6_addr_copy(&fl->fl6_src, &p->laddr);
  941. ipv6_addr_copy(&fl->fl6_dst, &p->raddr);
  942. fl->oif = p->link;
  943. fl->fl6_flowlabel = 0;
  944. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  945. fl->fl6_flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  946. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  947. fl->fl6_flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  948. ip6_tnl_set_cap(t);
  949. if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
  950. dev->flags |= IFF_POINTOPOINT;
  951. else
  952. dev->flags &= ~IFF_POINTOPOINT;
  953. dev->iflink = p->link;
  954. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  955. int strict = (ipv6_addr_type(&p->raddr) &
  956. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  957. struct rt6_info *rt = rt6_lookup(dev_net(dev),
  958. &p->raddr, &p->laddr,
  959. p->link, strict);
  960. if (rt == NULL)
  961. return;
  962. if (rt->rt6i_dev) {
  963. dev->hard_header_len = rt->rt6i_dev->hard_header_len +
  964. sizeof (struct ipv6hdr);
  965. dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr);
  966. if (dev->mtu < IPV6_MIN_MTU)
  967. dev->mtu = IPV6_MIN_MTU;
  968. }
  969. dst_release(&rt->u.dst);
  970. }
  971. }
  972. /**
  973. * ip6_tnl_change - update the tunnel parameters
  974. * @t: tunnel to be changed
  975. * @p: tunnel configuration parameters
  976. *
  977. * Description:
  978. * ip6_tnl_change() updates the tunnel parameters
  979. **/
  980. static int
  981. ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
  982. {
  983. ipv6_addr_copy(&t->parms.laddr, &p->laddr);
  984. ipv6_addr_copy(&t->parms.raddr, &p->raddr);
  985. t->parms.flags = p->flags;
  986. t->parms.hop_limit = p->hop_limit;
  987. t->parms.encap_limit = p->encap_limit;
  988. t->parms.flowinfo = p->flowinfo;
  989. t->parms.link = p->link;
  990. t->parms.proto = p->proto;
  991. ip6_tnl_dst_reset(t);
  992. ip6_tnl_link_config(t);
  993. return 0;
  994. }
  995. /**
  996. * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
  997. * @dev: virtual device associated with tunnel
  998. * @ifr: parameters passed from userspace
  999. * @cmd: command to be performed
  1000. *
  1001. * Description:
  1002. * ip6_tnl_ioctl() is used for managing IPv6 tunnels
  1003. * from userspace.
  1004. *
  1005. * The possible commands are the following:
  1006. * %SIOCGETTUNNEL: get tunnel parameters for device
  1007. * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
  1008. * %SIOCCHGTUNNEL: change tunnel parameters to those given
  1009. * %SIOCDELTUNNEL: delete tunnel
  1010. *
  1011. * The fallback device "ip6tnl0", created during module
  1012. * initialization, can be used for creating other tunnel devices.
  1013. *
  1014. * Return:
  1015. * 0 on success,
  1016. * %-EFAULT if unable to copy data to or from userspace,
  1017. * %-EPERM if current process hasn't %CAP_NET_ADMIN set
  1018. * %-EINVAL if passed tunnel parameters are invalid,
  1019. * %-EEXIST if changing a tunnel's parameters would cause a conflict
  1020. * %-ENODEV if attempting to change or delete a nonexisting device
  1021. **/
  1022. static int
  1023. ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1024. {
  1025. int err = 0;
  1026. struct ip6_tnl_parm p;
  1027. struct ip6_tnl *t = NULL;
  1028. struct net *net = dev_net(dev);
  1029. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1030. switch (cmd) {
  1031. case SIOCGETTUNNEL:
  1032. if (dev == ip6n->fb_tnl_dev) {
  1033. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
  1034. err = -EFAULT;
  1035. break;
  1036. }
  1037. t = ip6_tnl_locate(net, &p, 0);
  1038. }
  1039. if (t == NULL)
  1040. t = netdev_priv(dev);
  1041. memcpy(&p, &t->parms, sizeof (p));
  1042. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
  1043. err = -EFAULT;
  1044. }
  1045. break;
  1046. case SIOCADDTUNNEL:
  1047. case SIOCCHGTUNNEL:
  1048. err = -EPERM;
  1049. if (!capable(CAP_NET_ADMIN))
  1050. break;
  1051. err = -EFAULT;
  1052. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
  1053. break;
  1054. err = -EINVAL;
  1055. if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
  1056. p.proto != 0)
  1057. break;
  1058. t = ip6_tnl_locate(net, &p, cmd == SIOCADDTUNNEL);
  1059. if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
  1060. if (t != NULL) {
  1061. if (t->dev != dev) {
  1062. err = -EEXIST;
  1063. break;
  1064. }
  1065. } else
  1066. t = netdev_priv(dev);
  1067. ip6_tnl_unlink(ip6n, t);
  1068. err = ip6_tnl_change(t, &p);
  1069. ip6_tnl_link(ip6n, t);
  1070. netdev_state_change(dev);
  1071. }
  1072. if (t) {
  1073. err = 0;
  1074. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof (p)))
  1075. err = -EFAULT;
  1076. } else
  1077. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  1078. break;
  1079. case SIOCDELTUNNEL:
  1080. err = -EPERM;
  1081. if (!capable(CAP_NET_ADMIN))
  1082. break;
  1083. if (dev == ip6n->fb_tnl_dev) {
  1084. err = -EFAULT;
  1085. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
  1086. break;
  1087. err = -ENOENT;
  1088. if ((t = ip6_tnl_locate(net, &p, 0)) == NULL)
  1089. break;
  1090. err = -EPERM;
  1091. if (t->dev == ip6n->fb_tnl_dev)
  1092. break;
  1093. dev = t->dev;
  1094. }
  1095. err = 0;
  1096. unregister_netdevice(dev);
  1097. break;
  1098. default:
  1099. err = -EINVAL;
  1100. }
  1101. return err;
  1102. }
  1103. /**
  1104. * ip6_tnl_change_mtu - change mtu manually for tunnel device
  1105. * @dev: virtual device associated with tunnel
  1106. * @new_mtu: the new mtu
  1107. *
  1108. * Return:
  1109. * 0 on success,
  1110. * %-EINVAL if mtu too small
  1111. **/
  1112. static int
  1113. ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
  1114. {
  1115. if (new_mtu < IPV6_MIN_MTU) {
  1116. return -EINVAL;
  1117. }
  1118. dev->mtu = new_mtu;
  1119. return 0;
  1120. }
  1121. static const struct net_device_ops ip6_tnl_netdev_ops = {
  1122. .ndo_uninit = ip6_tnl_dev_uninit,
  1123. .ndo_start_xmit = ip6_tnl_xmit,
  1124. .ndo_do_ioctl = ip6_tnl_ioctl,
  1125. .ndo_change_mtu = ip6_tnl_change_mtu,
  1126. };
  1127. /**
  1128. * ip6_tnl_dev_setup - setup virtual tunnel device
  1129. * @dev: virtual device associated with tunnel
  1130. *
  1131. * Description:
  1132. * Initialize function pointers and device parameters
  1133. **/
  1134. static void ip6_tnl_dev_setup(struct net_device *dev)
  1135. {
  1136. dev->netdev_ops = &ip6_tnl_netdev_ops;
  1137. dev->destructor = free_netdev;
  1138. dev->type = ARPHRD_TUNNEL6;
  1139. dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
  1140. dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
  1141. dev->flags |= IFF_NOARP;
  1142. dev->addr_len = sizeof(struct in6_addr);
  1143. dev->features |= NETIF_F_NETNS_LOCAL;
  1144. }
  1145. /**
  1146. * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
  1147. * @dev: virtual device associated with tunnel
  1148. **/
  1149. static inline void
  1150. ip6_tnl_dev_init_gen(struct net_device *dev)
  1151. {
  1152. struct ip6_tnl *t = netdev_priv(dev);
  1153. t->dev = dev;
  1154. strcpy(t->parms.name, dev->name);
  1155. }
  1156. /**
  1157. * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
  1158. * @dev: virtual device associated with tunnel
  1159. **/
  1160. static void ip6_tnl_dev_init(struct net_device *dev)
  1161. {
  1162. struct ip6_tnl *t = netdev_priv(dev);
  1163. ip6_tnl_dev_init_gen(dev);
  1164. ip6_tnl_link_config(t);
  1165. }
  1166. /**
  1167. * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
  1168. * @dev: fallback device
  1169. *
  1170. * Return: 0
  1171. **/
  1172. static void ip6_fb_tnl_dev_init(struct net_device *dev)
  1173. {
  1174. struct ip6_tnl *t = netdev_priv(dev);
  1175. struct net *net = dev_net(dev);
  1176. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1177. ip6_tnl_dev_init_gen(dev);
  1178. t->parms.proto = IPPROTO_IPV6;
  1179. dev_hold(dev);
  1180. ip6n->tnls_wc[0] = t;
  1181. }
  1182. static struct xfrm6_tunnel ip4ip6_handler = {
  1183. .handler = ip4ip6_rcv,
  1184. .err_handler = ip4ip6_err,
  1185. .priority = 1,
  1186. };
  1187. static struct xfrm6_tunnel ip6ip6_handler = {
  1188. .handler = ip6ip6_rcv,
  1189. .err_handler = ip6ip6_err,
  1190. .priority = 1,
  1191. };
  1192. static void ip6_tnl_destroy_tunnels(struct ip6_tnl_net *ip6n)
  1193. {
  1194. int h;
  1195. struct ip6_tnl *t;
  1196. for (h = 0; h < HASH_SIZE; h++) {
  1197. while ((t = ip6n->tnls_r_l[h]) != NULL)
  1198. unregister_netdevice(t->dev);
  1199. }
  1200. t = ip6n->tnls_wc[0];
  1201. unregister_netdevice(t->dev);
  1202. }
  1203. static int ip6_tnl_init_net(struct net *net)
  1204. {
  1205. int err;
  1206. struct ip6_tnl_net *ip6n;
  1207. err = -ENOMEM;
  1208. ip6n = kzalloc(sizeof(struct ip6_tnl_net), GFP_KERNEL);
  1209. if (ip6n == NULL)
  1210. goto err_alloc;
  1211. err = net_assign_generic(net, ip6_tnl_net_id, ip6n);
  1212. if (err < 0)
  1213. goto err_assign;
  1214. ip6n->tnls[0] = ip6n->tnls_wc;
  1215. ip6n->tnls[1] = ip6n->tnls_r_l;
  1216. err = -ENOMEM;
  1217. ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
  1218. ip6_tnl_dev_setup);
  1219. if (!ip6n->fb_tnl_dev)
  1220. goto err_alloc_dev;
  1221. dev_net_set(ip6n->fb_tnl_dev, net);
  1222. ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
  1223. err = register_netdev(ip6n->fb_tnl_dev);
  1224. if (err < 0)
  1225. goto err_register;
  1226. return 0;
  1227. err_register:
  1228. free_netdev(ip6n->fb_tnl_dev);
  1229. err_alloc_dev:
  1230. /* nothing */
  1231. err_assign:
  1232. kfree(ip6n);
  1233. err_alloc:
  1234. return err;
  1235. }
  1236. static void ip6_tnl_exit_net(struct net *net)
  1237. {
  1238. struct ip6_tnl_net *ip6n;
  1239. ip6n = net_generic(net, ip6_tnl_net_id);
  1240. rtnl_lock();
  1241. ip6_tnl_destroy_tunnels(ip6n);
  1242. rtnl_unlock();
  1243. kfree(ip6n);
  1244. }
  1245. static struct pernet_operations ip6_tnl_net_ops = {
  1246. .init = ip6_tnl_init_net,
  1247. .exit = ip6_tnl_exit_net,
  1248. };
  1249. /**
  1250. * ip6_tunnel_init - register protocol and reserve needed resources
  1251. *
  1252. * Return: 0 on success
  1253. **/
  1254. static int __init ip6_tunnel_init(void)
  1255. {
  1256. int err;
  1257. if (xfrm6_tunnel_register(&ip4ip6_handler, AF_INET)) {
  1258. printk(KERN_ERR "ip6_tunnel init: can't register ip4ip6\n");
  1259. err = -EAGAIN;
  1260. goto out;
  1261. }
  1262. if (xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6)) {
  1263. printk(KERN_ERR "ip6_tunnel init: can't register ip6ip6\n");
  1264. err = -EAGAIN;
  1265. goto unreg_ip4ip6;
  1266. }
  1267. err = register_pernet_gen_device(&ip6_tnl_net_id, &ip6_tnl_net_ops);
  1268. if (err < 0)
  1269. goto err_pernet;
  1270. return 0;
  1271. err_pernet:
  1272. xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
  1273. unreg_ip4ip6:
  1274. xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
  1275. out:
  1276. return err;
  1277. }
  1278. /**
  1279. * ip6_tunnel_cleanup - free resources and unregister protocol
  1280. **/
  1281. static void __exit ip6_tunnel_cleanup(void)
  1282. {
  1283. if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
  1284. printk(KERN_INFO "ip6_tunnel close: can't deregister ip4ip6\n");
  1285. if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
  1286. printk(KERN_INFO "ip6_tunnel close: can't deregister ip6ip6\n");
  1287. unregister_pernet_gen_device(ip6_tnl_net_id, &ip6_tnl_net_ops);
  1288. }
  1289. module_init(ip6_tunnel_init);
  1290. module_exit(ip6_tunnel_cleanup);