sit.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  1. /*
  2. * IPv6 over IPv4 tunnel device - Simple Internet Transition (SIT)
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. *
  14. * Changes:
  15. * Roger Venning <r.venning@telstra.com>: 6to4 support
  16. * Nate Thompson <nate@thebog.net>: 6to4 support
  17. * Fred Templin <fred.l.templin@boeing.com>: isatap support
  18. */
  19. #include <linux/module.h>
  20. #include <linux/capability.h>
  21. #include <linux/errno.h>
  22. #include <linux/types.h>
  23. #include <linux/socket.h>
  24. #include <linux/sockios.h>
  25. #include <linux/net.h>
  26. #include <linux/in6.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/if_arp.h>
  29. #include <linux/icmp.h>
  30. #include <linux/slab.h>
  31. #include <asm/uaccess.h>
  32. #include <linux/init.h>
  33. #include <linux/netfilter_ipv4.h>
  34. #include <linux/if_ether.h>
  35. #include <net/sock.h>
  36. #include <net/snmp.h>
  37. #include <net/ipv6.h>
  38. #include <net/protocol.h>
  39. #include <net/transp_v6.h>
  40. #include <net/ip6_fib.h>
  41. #include <net/ip6_route.h>
  42. #include <net/ndisc.h>
  43. #include <net/addrconf.h>
  44. #include <net/ip.h>
  45. #include <net/udp.h>
  46. #include <net/icmp.h>
  47. #include <net/ipip.h>
  48. #include <net/inet_ecn.h>
  49. #include <net/xfrm.h>
  50. #include <net/dsfield.h>
  51. #include <net/net_namespace.h>
  52. #include <net/netns/generic.h>
  53. /*
  54. This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c
  55. For comments look at net/ipv4/ip_gre.c --ANK
  56. */
  57. #define HASH_SIZE 16
  58. #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
  59. static void ipip6_tunnel_init(struct net_device *dev);
  60. static void ipip6_tunnel_setup(struct net_device *dev);
  61. static int sit_net_id __read_mostly;
  62. struct sit_net {
  63. struct ip_tunnel *tunnels_r_l[HASH_SIZE];
  64. struct ip_tunnel *tunnels_r[HASH_SIZE];
  65. struct ip_tunnel *tunnels_l[HASH_SIZE];
  66. struct ip_tunnel *tunnels_wc[1];
  67. struct ip_tunnel **tunnels[4];
  68. struct net_device *fb_tunnel_dev;
  69. };
  70. /*
  71. * Locking : hash tables are protected by RCU and a spinlock
  72. */
  73. static DEFINE_SPINLOCK(ipip6_lock);
  74. #define for_each_ip_tunnel_rcu(start) \
  75. for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
  76. /*
  77. * Must be invoked with rcu_read_lock
  78. */
  79. static struct ip_tunnel * ipip6_tunnel_lookup(struct net *net,
  80. struct net_device *dev, __be32 remote, __be32 local)
  81. {
  82. unsigned h0 = HASH(remote);
  83. unsigned h1 = HASH(local);
  84. struct ip_tunnel *t;
  85. struct sit_net *sitn = net_generic(net, sit_net_id);
  86. for_each_ip_tunnel_rcu(sitn->tunnels_r_l[h0 ^ h1]) {
  87. if (local == t->parms.iph.saddr &&
  88. remote == t->parms.iph.daddr &&
  89. (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
  90. (t->dev->flags & IFF_UP))
  91. return t;
  92. }
  93. for_each_ip_tunnel_rcu(sitn->tunnels_r[h0]) {
  94. if (remote == t->parms.iph.daddr &&
  95. (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
  96. (t->dev->flags & IFF_UP))
  97. return t;
  98. }
  99. for_each_ip_tunnel_rcu(sitn->tunnels_l[h1]) {
  100. if (local == t->parms.iph.saddr &&
  101. (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
  102. (t->dev->flags & IFF_UP))
  103. return t;
  104. }
  105. t = rcu_dereference(sitn->tunnels_wc[0]);
  106. if ((t != NULL) && (t->dev->flags & IFF_UP))
  107. return t;
  108. return NULL;
  109. }
  110. static struct ip_tunnel **__ipip6_bucket(struct sit_net *sitn,
  111. struct ip_tunnel_parm *parms)
  112. {
  113. __be32 remote = parms->iph.daddr;
  114. __be32 local = parms->iph.saddr;
  115. unsigned h = 0;
  116. int prio = 0;
  117. if (remote) {
  118. prio |= 2;
  119. h ^= HASH(remote);
  120. }
  121. if (local) {
  122. prio |= 1;
  123. h ^= HASH(local);
  124. }
  125. return &sitn->tunnels[prio][h];
  126. }
  127. static inline struct ip_tunnel **ipip6_bucket(struct sit_net *sitn,
  128. struct ip_tunnel *t)
  129. {
  130. return __ipip6_bucket(sitn, &t->parms);
  131. }
  132. static void ipip6_tunnel_unlink(struct sit_net *sitn, struct ip_tunnel *t)
  133. {
  134. struct ip_tunnel **tp;
  135. for (tp = ipip6_bucket(sitn, t); *tp; tp = &(*tp)->next) {
  136. if (t == *tp) {
  137. spin_lock_bh(&ipip6_lock);
  138. *tp = t->next;
  139. spin_unlock_bh(&ipip6_lock);
  140. break;
  141. }
  142. }
  143. }
  144. static void ipip6_tunnel_link(struct sit_net *sitn, struct ip_tunnel *t)
  145. {
  146. struct ip_tunnel **tp = ipip6_bucket(sitn, t);
  147. spin_lock_bh(&ipip6_lock);
  148. t->next = *tp;
  149. rcu_assign_pointer(*tp, t);
  150. spin_unlock_bh(&ipip6_lock);
  151. }
  152. static void ipip6_tunnel_clone_6rd(struct net_device *dev, struct sit_net *sitn)
  153. {
  154. #ifdef CONFIG_IPV6_SIT_6RD
  155. struct ip_tunnel *t = netdev_priv(dev);
  156. if (t->dev == sitn->fb_tunnel_dev) {
  157. ipv6_addr_set(&t->ip6rd.prefix, htonl(0x20020000), 0, 0, 0);
  158. t->ip6rd.relay_prefix = 0;
  159. t->ip6rd.prefixlen = 16;
  160. t->ip6rd.relay_prefixlen = 0;
  161. } else {
  162. struct ip_tunnel *t0 = netdev_priv(sitn->fb_tunnel_dev);
  163. memcpy(&t->ip6rd, &t0->ip6rd, sizeof(t->ip6rd));
  164. }
  165. #endif
  166. }
  167. static struct ip_tunnel * ipip6_tunnel_locate(struct net *net,
  168. struct ip_tunnel_parm *parms, int create)
  169. {
  170. __be32 remote = parms->iph.daddr;
  171. __be32 local = parms->iph.saddr;
  172. struct ip_tunnel *t, **tp, *nt;
  173. struct net_device *dev;
  174. char name[IFNAMSIZ];
  175. struct sit_net *sitn = net_generic(net, sit_net_id);
  176. for (tp = __ipip6_bucket(sitn, parms); (t = *tp) != NULL; tp = &t->next) {
  177. if (local == t->parms.iph.saddr &&
  178. remote == t->parms.iph.daddr &&
  179. parms->link == t->parms.link) {
  180. if (create)
  181. return NULL;
  182. else
  183. return t;
  184. }
  185. }
  186. if (!create)
  187. goto failed;
  188. if (parms->name[0])
  189. strlcpy(name, parms->name, IFNAMSIZ);
  190. else
  191. sprintf(name, "sit%%d");
  192. dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
  193. if (dev == NULL)
  194. return NULL;
  195. dev_net_set(dev, net);
  196. if (strchr(name, '%')) {
  197. if (dev_alloc_name(dev, name) < 0)
  198. goto failed_free;
  199. }
  200. nt = netdev_priv(dev);
  201. nt->parms = *parms;
  202. ipip6_tunnel_init(dev);
  203. ipip6_tunnel_clone_6rd(dev, sitn);
  204. if (parms->i_flags & SIT_ISATAP)
  205. dev->priv_flags |= IFF_ISATAP;
  206. if (register_netdevice(dev) < 0)
  207. goto failed_free;
  208. dev_hold(dev);
  209. ipip6_tunnel_link(sitn, nt);
  210. return nt;
  211. failed_free:
  212. free_netdev(dev);
  213. failed:
  214. return NULL;
  215. }
  216. #define for_each_prl_rcu(start) \
  217. for (prl = rcu_dereference(start); \
  218. prl; \
  219. prl = rcu_dereference(prl->next))
  220. static struct ip_tunnel_prl_entry *
  221. __ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr)
  222. {
  223. struct ip_tunnel_prl_entry *prl;
  224. for_each_prl_rcu(t->prl)
  225. if (prl->addr == addr)
  226. break;
  227. return prl;
  228. }
  229. static int ipip6_tunnel_get_prl(struct ip_tunnel *t,
  230. struct ip_tunnel_prl __user *a)
  231. {
  232. struct ip_tunnel_prl kprl, *kp;
  233. struct ip_tunnel_prl_entry *prl;
  234. unsigned int cmax, c = 0, ca, len;
  235. int ret = 0;
  236. if (copy_from_user(&kprl, a, sizeof(kprl)))
  237. return -EFAULT;
  238. cmax = kprl.datalen / sizeof(kprl);
  239. if (cmax > 1 && kprl.addr != htonl(INADDR_ANY))
  240. cmax = 1;
  241. /* For simple GET or for root users,
  242. * we try harder to allocate.
  243. */
  244. kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
  245. kcalloc(cmax, sizeof(*kp), GFP_KERNEL) :
  246. NULL;
  247. rcu_read_lock();
  248. ca = t->prl_count < cmax ? t->prl_count : cmax;
  249. if (!kp) {
  250. /* We don't try hard to allocate much memory for
  251. * non-root users.
  252. * For root users, retry allocating enough memory for
  253. * the answer.
  254. */
  255. kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
  256. if (!kp) {
  257. ret = -ENOMEM;
  258. goto out;
  259. }
  260. }
  261. c = 0;
  262. for_each_prl_rcu(t->prl) {
  263. if (c >= cmax)
  264. break;
  265. if (kprl.addr != htonl(INADDR_ANY) && prl->addr != kprl.addr)
  266. continue;
  267. kp[c].addr = prl->addr;
  268. kp[c].flags = prl->flags;
  269. c++;
  270. if (kprl.addr != htonl(INADDR_ANY))
  271. break;
  272. }
  273. out:
  274. rcu_read_unlock();
  275. len = sizeof(*kp) * c;
  276. ret = 0;
  277. if ((len && copy_to_user(a + 1, kp, len)) || put_user(len, &a->datalen))
  278. ret = -EFAULT;
  279. kfree(kp);
  280. return ret;
  281. }
  282. static int
  283. ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg)
  284. {
  285. struct ip_tunnel_prl_entry *p;
  286. int err = 0;
  287. if (a->addr == htonl(INADDR_ANY))
  288. return -EINVAL;
  289. ASSERT_RTNL();
  290. for (p = t->prl; p; p = p->next) {
  291. if (p->addr == a->addr) {
  292. if (chg) {
  293. p->flags = a->flags;
  294. goto out;
  295. }
  296. err = -EEXIST;
  297. goto out;
  298. }
  299. }
  300. if (chg) {
  301. err = -ENXIO;
  302. goto out;
  303. }
  304. p = kzalloc(sizeof(struct ip_tunnel_prl_entry), GFP_KERNEL);
  305. if (!p) {
  306. err = -ENOBUFS;
  307. goto out;
  308. }
  309. p->next = t->prl;
  310. p->addr = a->addr;
  311. p->flags = a->flags;
  312. t->prl_count++;
  313. rcu_assign_pointer(t->prl, p);
  314. out:
  315. return err;
  316. }
  317. static void prl_entry_destroy_rcu(struct rcu_head *head)
  318. {
  319. kfree(container_of(head, struct ip_tunnel_prl_entry, rcu_head));
  320. }
  321. static void prl_list_destroy_rcu(struct rcu_head *head)
  322. {
  323. struct ip_tunnel_prl_entry *p, *n;
  324. p = container_of(head, struct ip_tunnel_prl_entry, rcu_head);
  325. do {
  326. n = p->next;
  327. kfree(p);
  328. p = n;
  329. } while (p);
  330. }
  331. static int
  332. ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
  333. {
  334. struct ip_tunnel_prl_entry *x, **p;
  335. int err = 0;
  336. ASSERT_RTNL();
  337. if (a && a->addr != htonl(INADDR_ANY)) {
  338. for (p = &t->prl; *p; p = &(*p)->next) {
  339. if ((*p)->addr == a->addr) {
  340. x = *p;
  341. *p = x->next;
  342. call_rcu(&x->rcu_head, prl_entry_destroy_rcu);
  343. t->prl_count--;
  344. goto out;
  345. }
  346. }
  347. err = -ENXIO;
  348. } else {
  349. if (t->prl) {
  350. t->prl_count = 0;
  351. x = t->prl;
  352. call_rcu(&x->rcu_head, prl_list_destroy_rcu);
  353. t->prl = NULL;
  354. }
  355. }
  356. out:
  357. return err;
  358. }
  359. static int
  360. isatap_chksrc(struct sk_buff *skb, struct iphdr *iph, struct ip_tunnel *t)
  361. {
  362. struct ip_tunnel_prl_entry *p;
  363. int ok = 1;
  364. rcu_read_lock();
  365. p = __ipip6_tunnel_locate_prl(t, iph->saddr);
  366. if (p) {
  367. if (p->flags & PRL_DEFAULT)
  368. skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT;
  369. else
  370. skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT;
  371. } else {
  372. struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr;
  373. if (ipv6_addr_is_isatap(addr6) &&
  374. (addr6->s6_addr32[3] == iph->saddr) &&
  375. ipv6_chk_prefix(addr6, t->dev))
  376. skb->ndisc_nodetype = NDISC_NODETYPE_HOST;
  377. else
  378. ok = 0;
  379. }
  380. rcu_read_unlock();
  381. return ok;
  382. }
  383. static void ipip6_tunnel_uninit(struct net_device *dev)
  384. {
  385. struct net *net = dev_net(dev);
  386. struct sit_net *sitn = net_generic(net, sit_net_id);
  387. if (dev == sitn->fb_tunnel_dev) {
  388. spin_lock_bh(&ipip6_lock);
  389. sitn->tunnels_wc[0] = NULL;
  390. spin_unlock_bh(&ipip6_lock);
  391. dev_put(dev);
  392. } else {
  393. ipip6_tunnel_unlink(sitn, netdev_priv(dev));
  394. ipip6_tunnel_del_prl(netdev_priv(dev), NULL);
  395. dev_put(dev);
  396. }
  397. }
  398. static int ipip6_err(struct sk_buff *skb, u32 info)
  399. {
  400. /* All the routers (except for Linux) return only
  401. 8 bytes of packet payload. It means, that precise relaying of
  402. ICMP in the real Internet is absolutely infeasible.
  403. */
  404. struct iphdr *iph = (struct iphdr*)skb->data;
  405. const int type = icmp_hdr(skb)->type;
  406. const int code = icmp_hdr(skb)->code;
  407. struct ip_tunnel *t;
  408. int err;
  409. switch (type) {
  410. default:
  411. case ICMP_PARAMETERPROB:
  412. return 0;
  413. case ICMP_DEST_UNREACH:
  414. switch (code) {
  415. case ICMP_SR_FAILED:
  416. case ICMP_PORT_UNREACH:
  417. /* Impossible event. */
  418. return 0;
  419. case ICMP_FRAG_NEEDED:
  420. /* Soft state for pmtu is maintained by IP core. */
  421. return 0;
  422. default:
  423. /* All others are translated to HOST_UNREACH.
  424. rfc2003 contains "deep thoughts" about NET_UNREACH,
  425. I believe they are just ether pollution. --ANK
  426. */
  427. break;
  428. }
  429. break;
  430. case ICMP_TIME_EXCEEDED:
  431. if (code != ICMP_EXC_TTL)
  432. return 0;
  433. break;
  434. }
  435. err = -ENOENT;
  436. rcu_read_lock();
  437. t = ipip6_tunnel_lookup(dev_net(skb->dev),
  438. skb->dev,
  439. iph->daddr,
  440. iph->saddr);
  441. if (t == NULL || t->parms.iph.daddr == 0)
  442. goto out;
  443. err = 0;
  444. if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
  445. goto out;
  446. if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
  447. t->err_count++;
  448. else
  449. t->err_count = 1;
  450. t->err_time = jiffies;
  451. out:
  452. rcu_read_unlock();
  453. return err;
  454. }
  455. static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
  456. {
  457. if (INET_ECN_is_ce(iph->tos))
  458. IP6_ECN_set_ce(ipv6_hdr(skb));
  459. }
  460. static int ipip6_rcv(struct sk_buff *skb)
  461. {
  462. struct iphdr *iph;
  463. struct ip_tunnel *tunnel;
  464. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  465. goto out;
  466. iph = ip_hdr(skb);
  467. rcu_read_lock();
  468. tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
  469. iph->saddr, iph->daddr);
  470. if (tunnel != NULL) {
  471. secpath_reset(skb);
  472. skb->mac_header = skb->network_header;
  473. skb_reset_network_header(skb);
  474. IPCB(skb)->flags = 0;
  475. skb->protocol = htons(ETH_P_IPV6);
  476. skb->pkt_type = PACKET_HOST;
  477. if ((tunnel->dev->priv_flags & IFF_ISATAP) &&
  478. !isatap_chksrc(skb, iph, tunnel)) {
  479. tunnel->dev->stats.rx_errors++;
  480. rcu_read_unlock();
  481. kfree_skb(skb);
  482. return 0;
  483. }
  484. skb_tunnel_rx(skb, tunnel->dev);
  485. ipip6_ecn_decapsulate(iph, skb);
  486. netif_rx(skb);
  487. rcu_read_unlock();
  488. return 0;
  489. }
  490. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  491. rcu_read_unlock();
  492. out:
  493. kfree_skb(skb);
  494. return 0;
  495. }
  496. /*
  497. * Returns the embedded IPv4 address if the IPv6 address
  498. * comes from 6rd / 6to4 (RFC 3056) addr space.
  499. */
  500. static inline
  501. __be32 try_6rd(struct in6_addr *v6dst, struct ip_tunnel *tunnel)
  502. {
  503. __be32 dst = 0;
  504. #ifdef CONFIG_IPV6_SIT_6RD
  505. if (ipv6_prefix_equal(v6dst, &tunnel->ip6rd.prefix,
  506. tunnel->ip6rd.prefixlen)) {
  507. unsigned pbw0, pbi0;
  508. int pbi1;
  509. u32 d;
  510. pbw0 = tunnel->ip6rd.prefixlen >> 5;
  511. pbi0 = tunnel->ip6rd.prefixlen & 0x1f;
  512. d = (ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >>
  513. tunnel->ip6rd.relay_prefixlen;
  514. pbi1 = pbi0 - tunnel->ip6rd.relay_prefixlen;
  515. if (pbi1 > 0)
  516. d |= ntohl(v6dst->s6_addr32[pbw0 + 1]) >>
  517. (32 - pbi1);
  518. dst = tunnel->ip6rd.relay_prefix | htonl(d);
  519. }
  520. #else
  521. if (v6dst->s6_addr16[0] == htons(0x2002)) {
  522. /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
  523. memcpy(&dst, &v6dst->s6_addr16[1], 4);
  524. }
  525. #endif
  526. return dst;
  527. }
  528. /*
  529. * This function assumes it is being called from dev_queue_xmit()
  530. * and that skb is filled properly by that function.
  531. */
  532. static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
  533. struct net_device *dev)
  534. {
  535. struct ip_tunnel *tunnel = netdev_priv(dev);
  536. struct net_device_stats *stats = &dev->stats;
  537. struct netdev_queue *txq = netdev_get_tx_queue(dev, 0);
  538. struct iphdr *tiph = &tunnel->parms.iph;
  539. struct ipv6hdr *iph6 = ipv6_hdr(skb);
  540. u8 tos = tunnel->parms.iph.tos;
  541. __be16 df = tiph->frag_off;
  542. struct rtable *rt; /* Route to the other host */
  543. struct net_device *tdev; /* Device to other host */
  544. struct iphdr *iph; /* Our new IP header */
  545. unsigned int max_headroom; /* The extra header space needed */
  546. __be32 dst = tiph->daddr;
  547. int mtu;
  548. struct in6_addr *addr6;
  549. int addr_type;
  550. if (skb->protocol != htons(ETH_P_IPV6))
  551. goto tx_error;
  552. /* ISATAP (RFC4214) - must come before 6to4 */
  553. if (dev->priv_flags & IFF_ISATAP) {
  554. struct neighbour *neigh = NULL;
  555. if (skb_dst(skb))
  556. neigh = skb_dst(skb)->neighbour;
  557. if (neigh == NULL) {
  558. if (net_ratelimit())
  559. printk(KERN_DEBUG "sit: nexthop == NULL\n");
  560. goto tx_error;
  561. }
  562. addr6 = (struct in6_addr*)&neigh->primary_key;
  563. addr_type = ipv6_addr_type(addr6);
  564. if ((addr_type & IPV6_ADDR_UNICAST) &&
  565. ipv6_addr_is_isatap(addr6))
  566. dst = addr6->s6_addr32[3];
  567. else
  568. goto tx_error;
  569. }
  570. if (!dst)
  571. dst = try_6rd(&iph6->daddr, tunnel);
  572. if (!dst) {
  573. struct neighbour *neigh = NULL;
  574. if (skb_dst(skb))
  575. neigh = skb_dst(skb)->neighbour;
  576. if (neigh == NULL) {
  577. if (net_ratelimit())
  578. printk(KERN_DEBUG "sit: nexthop == NULL\n");
  579. goto tx_error;
  580. }
  581. addr6 = (struct in6_addr*)&neigh->primary_key;
  582. addr_type = ipv6_addr_type(addr6);
  583. if (addr_type == IPV6_ADDR_ANY) {
  584. addr6 = &ipv6_hdr(skb)->daddr;
  585. addr_type = ipv6_addr_type(addr6);
  586. }
  587. if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
  588. goto tx_error_icmp;
  589. dst = addr6->s6_addr32[3];
  590. }
  591. {
  592. struct flowi fl = { .nl_u = { .ip4_u =
  593. { .daddr = dst,
  594. .saddr = tiph->saddr,
  595. .tos = RT_TOS(tos) } },
  596. .oif = tunnel->parms.link,
  597. .proto = IPPROTO_IPV6 };
  598. if (ip_route_output_key(dev_net(dev), &rt, &fl)) {
  599. stats->tx_carrier_errors++;
  600. goto tx_error_icmp;
  601. }
  602. }
  603. if (rt->rt_type != RTN_UNICAST) {
  604. ip_rt_put(rt);
  605. stats->tx_carrier_errors++;
  606. goto tx_error_icmp;
  607. }
  608. tdev = rt->dst.dev;
  609. if (tdev == dev) {
  610. ip_rt_put(rt);
  611. stats->collisions++;
  612. goto tx_error;
  613. }
  614. if (df) {
  615. mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
  616. if (mtu < 68) {
  617. stats->collisions++;
  618. ip_rt_put(rt);
  619. goto tx_error;
  620. }
  621. if (mtu < IPV6_MIN_MTU) {
  622. mtu = IPV6_MIN_MTU;
  623. df = 0;
  624. }
  625. if (tunnel->parms.iph.daddr && skb_dst(skb))
  626. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
  627. if (skb->len > mtu) {
  628. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  629. ip_rt_put(rt);
  630. goto tx_error;
  631. }
  632. }
  633. if (tunnel->err_count > 0) {
  634. if (time_before(jiffies,
  635. tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
  636. tunnel->err_count--;
  637. dst_link_failure(skb);
  638. } else
  639. tunnel->err_count = 0;
  640. }
  641. /*
  642. * Okay, now see if we can stuff it in the buffer as-is.
  643. */
  644. max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
  645. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  646. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  647. struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
  648. if (!new_skb) {
  649. ip_rt_put(rt);
  650. txq->tx_dropped++;
  651. dev_kfree_skb(skb);
  652. return NETDEV_TX_OK;
  653. }
  654. if (skb->sk)
  655. skb_set_owner_w(new_skb, skb->sk);
  656. dev_kfree_skb(skb);
  657. skb = new_skb;
  658. iph6 = ipv6_hdr(skb);
  659. }
  660. skb->transport_header = skb->network_header;
  661. skb_push(skb, sizeof(struct iphdr));
  662. skb_reset_network_header(skb);
  663. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  664. IPCB(skb)->flags = 0;
  665. skb_dst_drop(skb);
  666. skb_dst_set(skb, &rt->dst);
  667. /*
  668. * Push down and install the IPIP header.
  669. */
  670. iph = ip_hdr(skb);
  671. iph->version = 4;
  672. iph->ihl = sizeof(struct iphdr)>>2;
  673. iph->frag_off = df;
  674. iph->protocol = IPPROTO_IPV6;
  675. iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
  676. iph->daddr = rt->rt_dst;
  677. iph->saddr = rt->rt_src;
  678. if ((iph->ttl = tiph->ttl) == 0)
  679. iph->ttl = iph6->hop_limit;
  680. nf_reset(skb);
  681. IPTUNNEL_XMIT();
  682. return NETDEV_TX_OK;
  683. tx_error_icmp:
  684. dst_link_failure(skb);
  685. tx_error:
  686. stats->tx_errors++;
  687. dev_kfree_skb(skb);
  688. return NETDEV_TX_OK;
  689. }
  690. static void ipip6_tunnel_bind_dev(struct net_device *dev)
  691. {
  692. struct net_device *tdev = NULL;
  693. struct ip_tunnel *tunnel;
  694. struct iphdr *iph;
  695. tunnel = netdev_priv(dev);
  696. iph = &tunnel->parms.iph;
  697. if (iph->daddr) {
  698. struct flowi fl = { .nl_u = { .ip4_u =
  699. { .daddr = iph->daddr,
  700. .saddr = iph->saddr,
  701. .tos = RT_TOS(iph->tos) } },
  702. .oif = tunnel->parms.link,
  703. .proto = IPPROTO_IPV6 };
  704. struct rtable *rt;
  705. if (!ip_route_output_key(dev_net(dev), &rt, &fl)) {
  706. tdev = rt->dst.dev;
  707. ip_rt_put(rt);
  708. }
  709. dev->flags |= IFF_POINTOPOINT;
  710. }
  711. if (!tdev && tunnel->parms.link)
  712. tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
  713. if (tdev) {
  714. dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
  715. dev->mtu = tdev->mtu - sizeof(struct iphdr);
  716. if (dev->mtu < IPV6_MIN_MTU)
  717. dev->mtu = IPV6_MIN_MTU;
  718. }
  719. dev->iflink = tunnel->parms.link;
  720. }
  721. static int
  722. ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
  723. {
  724. int err = 0;
  725. struct ip_tunnel_parm p;
  726. struct ip_tunnel_prl prl;
  727. struct ip_tunnel *t;
  728. struct net *net = dev_net(dev);
  729. struct sit_net *sitn = net_generic(net, sit_net_id);
  730. #ifdef CONFIG_IPV6_SIT_6RD
  731. struct ip_tunnel_6rd ip6rd;
  732. #endif
  733. switch (cmd) {
  734. case SIOCGETTUNNEL:
  735. #ifdef CONFIG_IPV6_SIT_6RD
  736. case SIOCGET6RD:
  737. #endif
  738. t = NULL;
  739. if (dev == sitn->fb_tunnel_dev) {
  740. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  741. err = -EFAULT;
  742. break;
  743. }
  744. t = ipip6_tunnel_locate(net, &p, 0);
  745. }
  746. if (t == NULL)
  747. t = netdev_priv(dev);
  748. err = -EFAULT;
  749. if (cmd == SIOCGETTUNNEL) {
  750. memcpy(&p, &t->parms, sizeof(p));
  751. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p,
  752. sizeof(p)))
  753. goto done;
  754. #ifdef CONFIG_IPV6_SIT_6RD
  755. } else {
  756. ipv6_addr_copy(&ip6rd.prefix, &t->ip6rd.prefix);
  757. ip6rd.relay_prefix = t->ip6rd.relay_prefix;
  758. ip6rd.prefixlen = t->ip6rd.prefixlen;
  759. ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen;
  760. if (copy_to_user(ifr->ifr_ifru.ifru_data, &ip6rd,
  761. sizeof(ip6rd)))
  762. goto done;
  763. #endif
  764. }
  765. err = 0;
  766. break;
  767. case SIOCADDTUNNEL:
  768. case SIOCCHGTUNNEL:
  769. err = -EPERM;
  770. if (!capable(CAP_NET_ADMIN))
  771. goto done;
  772. err = -EFAULT;
  773. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  774. goto done;
  775. err = -EINVAL;
  776. if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
  777. p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
  778. goto done;
  779. if (p.iph.ttl)
  780. p.iph.frag_off |= htons(IP_DF);
  781. t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
  782. if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  783. if (t != NULL) {
  784. if (t->dev != dev) {
  785. err = -EEXIST;
  786. break;
  787. }
  788. } else {
  789. if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
  790. (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
  791. err = -EINVAL;
  792. break;
  793. }
  794. t = netdev_priv(dev);
  795. ipip6_tunnel_unlink(sitn, t);
  796. t->parms.iph.saddr = p.iph.saddr;
  797. t->parms.iph.daddr = p.iph.daddr;
  798. memcpy(dev->dev_addr, &p.iph.saddr, 4);
  799. memcpy(dev->broadcast, &p.iph.daddr, 4);
  800. ipip6_tunnel_link(sitn, t);
  801. netdev_state_change(dev);
  802. }
  803. }
  804. if (t) {
  805. err = 0;
  806. if (cmd == SIOCCHGTUNNEL) {
  807. t->parms.iph.ttl = p.iph.ttl;
  808. t->parms.iph.tos = p.iph.tos;
  809. if (t->parms.link != p.link) {
  810. t->parms.link = p.link;
  811. ipip6_tunnel_bind_dev(dev);
  812. netdev_state_change(dev);
  813. }
  814. }
  815. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
  816. err = -EFAULT;
  817. } else
  818. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  819. break;
  820. case SIOCDELTUNNEL:
  821. err = -EPERM;
  822. if (!capable(CAP_NET_ADMIN))
  823. goto done;
  824. if (dev == sitn->fb_tunnel_dev) {
  825. err = -EFAULT;
  826. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  827. goto done;
  828. err = -ENOENT;
  829. if ((t = ipip6_tunnel_locate(net, &p, 0)) == NULL)
  830. goto done;
  831. err = -EPERM;
  832. if (t == netdev_priv(sitn->fb_tunnel_dev))
  833. goto done;
  834. dev = t->dev;
  835. }
  836. unregister_netdevice(dev);
  837. err = 0;
  838. break;
  839. case SIOCGETPRL:
  840. err = -EINVAL;
  841. if (dev == sitn->fb_tunnel_dev)
  842. goto done;
  843. err = -ENOENT;
  844. if (!(t = netdev_priv(dev)))
  845. goto done;
  846. err = ipip6_tunnel_get_prl(t, ifr->ifr_ifru.ifru_data);
  847. break;
  848. case SIOCADDPRL:
  849. case SIOCDELPRL:
  850. case SIOCCHGPRL:
  851. err = -EPERM;
  852. if (!capable(CAP_NET_ADMIN))
  853. goto done;
  854. err = -EINVAL;
  855. if (dev == sitn->fb_tunnel_dev)
  856. goto done;
  857. err = -EFAULT;
  858. if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
  859. goto done;
  860. err = -ENOENT;
  861. if (!(t = netdev_priv(dev)))
  862. goto done;
  863. switch (cmd) {
  864. case SIOCDELPRL:
  865. err = ipip6_tunnel_del_prl(t, &prl);
  866. break;
  867. case SIOCADDPRL:
  868. case SIOCCHGPRL:
  869. err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
  870. break;
  871. }
  872. netdev_state_change(dev);
  873. break;
  874. #ifdef CONFIG_IPV6_SIT_6RD
  875. case SIOCADD6RD:
  876. case SIOCCHG6RD:
  877. case SIOCDEL6RD:
  878. err = -EPERM;
  879. if (!capable(CAP_NET_ADMIN))
  880. goto done;
  881. err = -EFAULT;
  882. if (copy_from_user(&ip6rd, ifr->ifr_ifru.ifru_data,
  883. sizeof(ip6rd)))
  884. goto done;
  885. t = netdev_priv(dev);
  886. if (cmd != SIOCDEL6RD) {
  887. struct in6_addr prefix;
  888. __be32 relay_prefix;
  889. err = -EINVAL;
  890. if (ip6rd.relay_prefixlen > 32 ||
  891. ip6rd.prefixlen + (32 - ip6rd.relay_prefixlen) > 64)
  892. goto done;
  893. ipv6_addr_prefix(&prefix, &ip6rd.prefix,
  894. ip6rd.prefixlen);
  895. if (!ipv6_addr_equal(&prefix, &ip6rd.prefix))
  896. goto done;
  897. if (ip6rd.relay_prefixlen)
  898. relay_prefix = ip6rd.relay_prefix &
  899. htonl(0xffffffffUL <<
  900. (32 - ip6rd.relay_prefixlen));
  901. else
  902. relay_prefix = 0;
  903. if (relay_prefix != ip6rd.relay_prefix)
  904. goto done;
  905. ipv6_addr_copy(&t->ip6rd.prefix, &prefix);
  906. t->ip6rd.relay_prefix = relay_prefix;
  907. t->ip6rd.prefixlen = ip6rd.prefixlen;
  908. t->ip6rd.relay_prefixlen = ip6rd.relay_prefixlen;
  909. } else
  910. ipip6_tunnel_clone_6rd(dev, sitn);
  911. err = 0;
  912. break;
  913. #endif
  914. default:
  915. err = -EINVAL;
  916. }
  917. done:
  918. return err;
  919. }
  920. static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  921. {
  922. if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
  923. return -EINVAL;
  924. dev->mtu = new_mtu;
  925. return 0;
  926. }
  927. static const struct net_device_ops ipip6_netdev_ops = {
  928. .ndo_uninit = ipip6_tunnel_uninit,
  929. .ndo_start_xmit = ipip6_tunnel_xmit,
  930. .ndo_do_ioctl = ipip6_tunnel_ioctl,
  931. .ndo_change_mtu = ipip6_tunnel_change_mtu,
  932. };
  933. static void ipip6_tunnel_setup(struct net_device *dev)
  934. {
  935. dev->netdev_ops = &ipip6_netdev_ops;
  936. dev->destructor = free_netdev;
  937. dev->type = ARPHRD_SIT;
  938. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
  939. dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
  940. dev->flags = IFF_NOARP;
  941. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  942. dev->iflink = 0;
  943. dev->addr_len = 4;
  944. dev->features |= NETIF_F_NETNS_LOCAL;
  945. }
  946. static void ipip6_tunnel_init(struct net_device *dev)
  947. {
  948. struct ip_tunnel *tunnel = netdev_priv(dev);
  949. tunnel->dev = dev;
  950. strcpy(tunnel->parms.name, dev->name);
  951. memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
  952. memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
  953. ipip6_tunnel_bind_dev(dev);
  954. }
  955. static void __net_init ipip6_fb_tunnel_init(struct net_device *dev)
  956. {
  957. struct ip_tunnel *tunnel = netdev_priv(dev);
  958. struct iphdr *iph = &tunnel->parms.iph;
  959. struct net *net = dev_net(dev);
  960. struct sit_net *sitn = net_generic(net, sit_net_id);
  961. tunnel->dev = dev;
  962. strcpy(tunnel->parms.name, dev->name);
  963. iph->version = 4;
  964. iph->protocol = IPPROTO_IPV6;
  965. iph->ihl = 5;
  966. iph->ttl = 64;
  967. dev_hold(dev);
  968. sitn->tunnels_wc[0] = tunnel;
  969. }
  970. static struct xfrm_tunnel sit_handler = {
  971. .handler = ipip6_rcv,
  972. .err_handler = ipip6_err,
  973. .priority = 1,
  974. };
  975. static void __net_exit sit_destroy_tunnels(struct sit_net *sitn, struct list_head *head)
  976. {
  977. int prio;
  978. for (prio = 1; prio < 4; prio++) {
  979. int h;
  980. for (h = 0; h < HASH_SIZE; h++) {
  981. struct ip_tunnel *t = sitn->tunnels[prio][h];
  982. while (t != NULL) {
  983. unregister_netdevice_queue(t->dev, head);
  984. t = t->next;
  985. }
  986. }
  987. }
  988. }
  989. static int __net_init sit_init_net(struct net *net)
  990. {
  991. struct sit_net *sitn = net_generic(net, sit_net_id);
  992. int err;
  993. sitn->tunnels[0] = sitn->tunnels_wc;
  994. sitn->tunnels[1] = sitn->tunnels_l;
  995. sitn->tunnels[2] = sitn->tunnels_r;
  996. sitn->tunnels[3] = sitn->tunnels_r_l;
  997. sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
  998. ipip6_tunnel_setup);
  999. if (!sitn->fb_tunnel_dev) {
  1000. err = -ENOMEM;
  1001. goto err_alloc_dev;
  1002. }
  1003. dev_net_set(sitn->fb_tunnel_dev, net);
  1004. ipip6_fb_tunnel_init(sitn->fb_tunnel_dev);
  1005. ipip6_tunnel_clone_6rd(sitn->fb_tunnel_dev, sitn);
  1006. if ((err = register_netdev(sitn->fb_tunnel_dev)))
  1007. goto err_reg_dev;
  1008. return 0;
  1009. err_reg_dev:
  1010. dev_put(sitn->fb_tunnel_dev);
  1011. free_netdev(sitn->fb_tunnel_dev);
  1012. err_alloc_dev:
  1013. return err;
  1014. }
  1015. static void __net_exit sit_exit_net(struct net *net)
  1016. {
  1017. struct sit_net *sitn = net_generic(net, sit_net_id);
  1018. LIST_HEAD(list);
  1019. rtnl_lock();
  1020. sit_destroy_tunnels(sitn, &list);
  1021. unregister_netdevice_queue(sitn->fb_tunnel_dev, &list);
  1022. unregister_netdevice_many(&list);
  1023. rtnl_unlock();
  1024. }
  1025. static struct pernet_operations sit_net_ops = {
  1026. .init = sit_init_net,
  1027. .exit = sit_exit_net,
  1028. .id = &sit_net_id,
  1029. .size = sizeof(struct sit_net),
  1030. };
  1031. static void __exit sit_cleanup(void)
  1032. {
  1033. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  1034. unregister_pernet_device(&sit_net_ops);
  1035. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  1036. }
  1037. static int __init sit_init(void)
  1038. {
  1039. int err;
  1040. printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
  1041. err = register_pernet_device(&sit_net_ops);
  1042. if (err < 0)
  1043. return err;
  1044. err = xfrm4_tunnel_register(&sit_handler, AF_INET6);
  1045. if (err < 0) {
  1046. unregister_pernet_device(&sit_net_ops);
  1047. printk(KERN_INFO "sit init: Can't add protocol\n");
  1048. }
  1049. return err;
  1050. }
  1051. module_init(sit_init);
  1052. module_exit(sit_cleanup);
  1053. MODULE_LICENSE("GPL");
  1054. MODULE_ALIAS("sit0");