sit.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  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. static DEFINE_SPINLOCK(ipip6_prl_lock);
  217. #define for_each_prl_rcu(start) \
  218. for (prl = rcu_dereference(start); \
  219. prl; \
  220. prl = rcu_dereference(prl->next))
  221. static struct ip_tunnel_prl_entry *
  222. __ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr)
  223. {
  224. struct ip_tunnel_prl_entry *prl;
  225. for_each_prl_rcu(t->prl)
  226. if (prl->addr == addr)
  227. break;
  228. return prl;
  229. }
  230. static int ipip6_tunnel_get_prl(struct ip_tunnel *t,
  231. struct ip_tunnel_prl __user *a)
  232. {
  233. struct ip_tunnel_prl kprl, *kp;
  234. struct ip_tunnel_prl_entry *prl;
  235. unsigned int cmax, c = 0, ca, len;
  236. int ret = 0;
  237. if (copy_from_user(&kprl, a, sizeof(kprl)))
  238. return -EFAULT;
  239. cmax = kprl.datalen / sizeof(kprl);
  240. if (cmax > 1 && kprl.addr != htonl(INADDR_ANY))
  241. cmax = 1;
  242. /* For simple GET or for root users,
  243. * we try harder to allocate.
  244. */
  245. kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
  246. kcalloc(cmax, sizeof(*kp), GFP_KERNEL) :
  247. NULL;
  248. rcu_read_lock();
  249. ca = t->prl_count < cmax ? t->prl_count : cmax;
  250. if (!kp) {
  251. /* We don't try hard to allocate much memory for
  252. * non-root users.
  253. * For root users, retry allocating enough memory for
  254. * the answer.
  255. */
  256. kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
  257. if (!kp) {
  258. ret = -ENOMEM;
  259. goto out;
  260. }
  261. }
  262. c = 0;
  263. for_each_prl_rcu(t->prl) {
  264. if (c >= cmax)
  265. break;
  266. if (kprl.addr != htonl(INADDR_ANY) && prl->addr != kprl.addr)
  267. continue;
  268. kp[c].addr = prl->addr;
  269. kp[c].flags = prl->flags;
  270. c++;
  271. if (kprl.addr != htonl(INADDR_ANY))
  272. break;
  273. }
  274. out:
  275. rcu_read_unlock();
  276. len = sizeof(*kp) * c;
  277. ret = 0;
  278. if ((len && copy_to_user(a + 1, kp, len)) || put_user(len, &a->datalen))
  279. ret = -EFAULT;
  280. kfree(kp);
  281. return ret;
  282. }
  283. static int
  284. ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg)
  285. {
  286. struct ip_tunnel_prl_entry *p;
  287. int err = 0;
  288. if (a->addr == htonl(INADDR_ANY))
  289. return -EINVAL;
  290. spin_lock(&ipip6_prl_lock);
  291. for (p = t->prl; p; p = p->next) {
  292. if (p->addr == a->addr) {
  293. if (chg) {
  294. p->flags = a->flags;
  295. goto out;
  296. }
  297. err = -EEXIST;
  298. goto out;
  299. }
  300. }
  301. if (chg) {
  302. err = -ENXIO;
  303. goto out;
  304. }
  305. p = kzalloc(sizeof(struct ip_tunnel_prl_entry), GFP_KERNEL);
  306. if (!p) {
  307. err = -ENOBUFS;
  308. goto out;
  309. }
  310. p->next = t->prl;
  311. p->addr = a->addr;
  312. p->flags = a->flags;
  313. t->prl_count++;
  314. rcu_assign_pointer(t->prl, p);
  315. out:
  316. spin_unlock(&ipip6_prl_lock);
  317. return err;
  318. }
  319. static void prl_entry_destroy_rcu(struct rcu_head *head)
  320. {
  321. kfree(container_of(head, struct ip_tunnel_prl_entry, rcu_head));
  322. }
  323. static void prl_list_destroy_rcu(struct rcu_head *head)
  324. {
  325. struct ip_tunnel_prl_entry *p, *n;
  326. p = container_of(head, struct ip_tunnel_prl_entry, rcu_head);
  327. do {
  328. n = p->next;
  329. kfree(p);
  330. p = n;
  331. } while (p);
  332. }
  333. static int
  334. ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
  335. {
  336. struct ip_tunnel_prl_entry *x, **p;
  337. int err = 0;
  338. spin_lock(&ipip6_prl_lock);
  339. if (a && a->addr != htonl(INADDR_ANY)) {
  340. for (p = &t->prl; *p; p = &(*p)->next) {
  341. if ((*p)->addr == a->addr) {
  342. x = *p;
  343. *p = x->next;
  344. call_rcu(&x->rcu_head, prl_entry_destroy_rcu);
  345. t->prl_count--;
  346. goto out;
  347. }
  348. }
  349. err = -ENXIO;
  350. } else {
  351. if (t->prl) {
  352. t->prl_count = 0;
  353. x = t->prl;
  354. call_rcu(&x->rcu_head, prl_list_destroy_rcu);
  355. t->prl = NULL;
  356. }
  357. }
  358. out:
  359. spin_unlock(&ipip6_prl_lock);
  360. return err;
  361. }
  362. static int
  363. isatap_chksrc(struct sk_buff *skb, struct iphdr *iph, struct ip_tunnel *t)
  364. {
  365. struct ip_tunnel_prl_entry *p;
  366. int ok = 1;
  367. rcu_read_lock();
  368. p = __ipip6_tunnel_locate_prl(t, iph->saddr);
  369. if (p) {
  370. if (p->flags & PRL_DEFAULT)
  371. skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT;
  372. else
  373. skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT;
  374. } else {
  375. struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr;
  376. if (ipv6_addr_is_isatap(addr6) &&
  377. (addr6->s6_addr32[3] == iph->saddr) &&
  378. ipv6_chk_prefix(addr6, t->dev))
  379. skb->ndisc_nodetype = NDISC_NODETYPE_HOST;
  380. else
  381. ok = 0;
  382. }
  383. rcu_read_unlock();
  384. return ok;
  385. }
  386. static void ipip6_tunnel_uninit(struct net_device *dev)
  387. {
  388. struct net *net = dev_net(dev);
  389. struct sit_net *sitn = net_generic(net, sit_net_id);
  390. if (dev == sitn->fb_tunnel_dev) {
  391. spin_lock_bh(&ipip6_lock);
  392. sitn->tunnels_wc[0] = NULL;
  393. spin_unlock_bh(&ipip6_lock);
  394. dev_put(dev);
  395. } else {
  396. ipip6_tunnel_unlink(sitn, netdev_priv(dev));
  397. ipip6_tunnel_del_prl(netdev_priv(dev), NULL);
  398. dev_put(dev);
  399. }
  400. }
  401. static int ipip6_err(struct sk_buff *skb, u32 info)
  402. {
  403. /* All the routers (except for Linux) return only
  404. 8 bytes of packet payload. It means, that precise relaying of
  405. ICMP in the real Internet is absolutely infeasible.
  406. */
  407. struct iphdr *iph = (struct iphdr*)skb->data;
  408. const int type = icmp_hdr(skb)->type;
  409. const int code = icmp_hdr(skb)->code;
  410. struct ip_tunnel *t;
  411. int err;
  412. switch (type) {
  413. default:
  414. case ICMP_PARAMETERPROB:
  415. return 0;
  416. case ICMP_DEST_UNREACH:
  417. switch (code) {
  418. case ICMP_SR_FAILED:
  419. case ICMP_PORT_UNREACH:
  420. /* Impossible event. */
  421. return 0;
  422. case ICMP_FRAG_NEEDED:
  423. /* Soft state for pmtu is maintained by IP core. */
  424. return 0;
  425. default:
  426. /* All others are translated to HOST_UNREACH.
  427. rfc2003 contains "deep thoughts" about NET_UNREACH,
  428. I believe they are just ether pollution. --ANK
  429. */
  430. break;
  431. }
  432. break;
  433. case ICMP_TIME_EXCEEDED:
  434. if (code != ICMP_EXC_TTL)
  435. return 0;
  436. break;
  437. }
  438. err = -ENOENT;
  439. rcu_read_lock();
  440. t = ipip6_tunnel_lookup(dev_net(skb->dev),
  441. skb->dev,
  442. iph->daddr,
  443. iph->saddr);
  444. if (t == NULL || t->parms.iph.daddr == 0)
  445. goto out;
  446. err = 0;
  447. if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
  448. goto out;
  449. if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
  450. t->err_count++;
  451. else
  452. t->err_count = 1;
  453. t->err_time = jiffies;
  454. out:
  455. rcu_read_unlock();
  456. return err;
  457. }
  458. static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
  459. {
  460. if (INET_ECN_is_ce(iph->tos))
  461. IP6_ECN_set_ce(ipv6_hdr(skb));
  462. }
  463. static int ipip6_rcv(struct sk_buff *skb)
  464. {
  465. struct iphdr *iph;
  466. struct ip_tunnel *tunnel;
  467. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  468. goto out;
  469. iph = ip_hdr(skb);
  470. rcu_read_lock();
  471. tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
  472. iph->saddr, iph->daddr);
  473. if (tunnel != NULL) {
  474. secpath_reset(skb);
  475. skb->mac_header = skb->network_header;
  476. skb_reset_network_header(skb);
  477. IPCB(skb)->flags = 0;
  478. skb->protocol = htons(ETH_P_IPV6);
  479. skb->pkt_type = PACKET_HOST;
  480. if ((tunnel->dev->priv_flags & IFF_ISATAP) &&
  481. !isatap_chksrc(skb, iph, tunnel)) {
  482. tunnel->dev->stats.rx_errors++;
  483. rcu_read_unlock();
  484. kfree_skb(skb);
  485. return 0;
  486. }
  487. tunnel->dev->stats.rx_packets++;
  488. tunnel->dev->stats.rx_bytes += skb->len;
  489. skb->dev = tunnel->dev;
  490. skb_dst_drop(skb);
  491. nf_reset(skb);
  492. ipip6_ecn_decapsulate(iph, skb);
  493. netif_rx(skb);
  494. rcu_read_unlock();
  495. return 0;
  496. }
  497. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  498. rcu_read_unlock();
  499. out:
  500. kfree_skb(skb);
  501. return 0;
  502. }
  503. /*
  504. * Returns the embedded IPv4 address if the IPv6 address
  505. * comes from 6rd / 6to4 (RFC 3056) addr space.
  506. */
  507. static inline
  508. __be32 try_6rd(struct in6_addr *v6dst, struct ip_tunnel *tunnel)
  509. {
  510. __be32 dst = 0;
  511. #ifdef CONFIG_IPV6_SIT_6RD
  512. if (ipv6_prefix_equal(v6dst, &tunnel->ip6rd.prefix,
  513. tunnel->ip6rd.prefixlen)) {
  514. unsigned pbw0, pbi0;
  515. int pbi1;
  516. u32 d;
  517. pbw0 = tunnel->ip6rd.prefixlen >> 5;
  518. pbi0 = tunnel->ip6rd.prefixlen & 0x1f;
  519. d = (ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >>
  520. tunnel->ip6rd.relay_prefixlen;
  521. pbi1 = pbi0 - tunnel->ip6rd.relay_prefixlen;
  522. if (pbi1 > 0)
  523. d |= ntohl(v6dst->s6_addr32[pbw0 + 1]) >>
  524. (32 - pbi1);
  525. dst = tunnel->ip6rd.relay_prefix | htonl(d);
  526. }
  527. #else
  528. if (v6dst->s6_addr16[0] == htons(0x2002)) {
  529. /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
  530. memcpy(&dst, &v6dst->s6_addr16[1], 4);
  531. }
  532. #endif
  533. return dst;
  534. }
  535. /*
  536. * This function assumes it is being called from dev_queue_xmit()
  537. * and that skb is filled properly by that function.
  538. */
  539. static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
  540. struct net_device *dev)
  541. {
  542. struct ip_tunnel *tunnel = netdev_priv(dev);
  543. struct net_device_stats *stats = &dev->stats;
  544. struct netdev_queue *txq = netdev_get_tx_queue(dev, 0);
  545. struct iphdr *tiph = &tunnel->parms.iph;
  546. struct ipv6hdr *iph6 = ipv6_hdr(skb);
  547. u8 tos = tunnel->parms.iph.tos;
  548. __be16 df = tiph->frag_off;
  549. struct rtable *rt; /* Route to the other host */
  550. struct net_device *tdev; /* Device to other host */
  551. struct iphdr *iph; /* Our new IP header */
  552. unsigned int max_headroom; /* The extra header space needed */
  553. __be32 dst = tiph->daddr;
  554. int mtu;
  555. struct in6_addr *addr6;
  556. int addr_type;
  557. if (skb->protocol != htons(ETH_P_IPV6))
  558. goto tx_error;
  559. /* ISATAP (RFC4214) - must come before 6to4 */
  560. if (dev->priv_flags & IFF_ISATAP) {
  561. struct neighbour *neigh = NULL;
  562. if (skb_dst(skb))
  563. neigh = skb_dst(skb)->neighbour;
  564. if (neigh == NULL) {
  565. if (net_ratelimit())
  566. printk(KERN_DEBUG "sit: nexthop == NULL\n");
  567. goto tx_error;
  568. }
  569. addr6 = (struct in6_addr*)&neigh->primary_key;
  570. addr_type = ipv6_addr_type(addr6);
  571. if ((addr_type & IPV6_ADDR_UNICAST) &&
  572. ipv6_addr_is_isatap(addr6))
  573. dst = addr6->s6_addr32[3];
  574. else
  575. goto tx_error;
  576. }
  577. if (!dst)
  578. dst = try_6rd(&iph6->daddr, tunnel);
  579. if (!dst) {
  580. struct neighbour *neigh = NULL;
  581. if (skb_dst(skb))
  582. neigh = skb_dst(skb)->neighbour;
  583. if (neigh == NULL) {
  584. if (net_ratelimit())
  585. printk(KERN_DEBUG "sit: nexthop == NULL\n");
  586. goto tx_error;
  587. }
  588. addr6 = (struct in6_addr*)&neigh->primary_key;
  589. addr_type = ipv6_addr_type(addr6);
  590. if (addr_type == IPV6_ADDR_ANY) {
  591. addr6 = &ipv6_hdr(skb)->daddr;
  592. addr_type = ipv6_addr_type(addr6);
  593. }
  594. if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
  595. goto tx_error_icmp;
  596. dst = addr6->s6_addr32[3];
  597. }
  598. {
  599. struct flowi fl = { .nl_u = { .ip4_u =
  600. { .daddr = dst,
  601. .saddr = tiph->saddr,
  602. .tos = RT_TOS(tos) } },
  603. .oif = tunnel->parms.link,
  604. .proto = IPPROTO_IPV6 };
  605. if (ip_route_output_key(dev_net(dev), &rt, &fl)) {
  606. stats->tx_carrier_errors++;
  607. goto tx_error_icmp;
  608. }
  609. }
  610. if (rt->rt_type != RTN_UNICAST) {
  611. ip_rt_put(rt);
  612. stats->tx_carrier_errors++;
  613. goto tx_error_icmp;
  614. }
  615. tdev = rt->u.dst.dev;
  616. if (tdev == dev) {
  617. ip_rt_put(rt);
  618. stats->collisions++;
  619. goto tx_error;
  620. }
  621. if (df) {
  622. mtu = dst_mtu(&rt->u.dst) - sizeof(struct iphdr);
  623. if (mtu < 68) {
  624. stats->collisions++;
  625. ip_rt_put(rt);
  626. goto tx_error;
  627. }
  628. if (mtu < IPV6_MIN_MTU) {
  629. mtu = IPV6_MIN_MTU;
  630. df = 0;
  631. }
  632. if (tunnel->parms.iph.daddr && skb_dst(skb))
  633. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
  634. if (skb->len > mtu) {
  635. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  636. ip_rt_put(rt);
  637. goto tx_error;
  638. }
  639. }
  640. if (tunnel->err_count > 0) {
  641. if (time_before(jiffies,
  642. tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
  643. tunnel->err_count--;
  644. dst_link_failure(skb);
  645. } else
  646. tunnel->err_count = 0;
  647. }
  648. /*
  649. * Okay, now see if we can stuff it in the buffer as-is.
  650. */
  651. max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
  652. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  653. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  654. struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
  655. if (!new_skb) {
  656. ip_rt_put(rt);
  657. txq->tx_dropped++;
  658. dev_kfree_skb(skb);
  659. return NETDEV_TX_OK;
  660. }
  661. if (skb->sk)
  662. skb_set_owner_w(new_skb, skb->sk);
  663. dev_kfree_skb(skb);
  664. skb = new_skb;
  665. iph6 = ipv6_hdr(skb);
  666. }
  667. skb->transport_header = skb->network_header;
  668. skb_push(skb, sizeof(struct iphdr));
  669. skb_reset_network_header(skb);
  670. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  671. IPCB(skb)->flags = 0;
  672. skb_dst_drop(skb);
  673. skb_dst_set(skb, &rt->u.dst);
  674. /*
  675. * Push down and install the IPIP header.
  676. */
  677. iph = ip_hdr(skb);
  678. iph->version = 4;
  679. iph->ihl = sizeof(struct iphdr)>>2;
  680. iph->frag_off = df;
  681. iph->protocol = IPPROTO_IPV6;
  682. iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
  683. iph->daddr = rt->rt_dst;
  684. iph->saddr = rt->rt_src;
  685. if ((iph->ttl = tiph->ttl) == 0)
  686. iph->ttl = iph6->hop_limit;
  687. nf_reset(skb);
  688. IPTUNNEL_XMIT();
  689. return NETDEV_TX_OK;
  690. tx_error_icmp:
  691. dst_link_failure(skb);
  692. tx_error:
  693. stats->tx_errors++;
  694. dev_kfree_skb(skb);
  695. return NETDEV_TX_OK;
  696. }
  697. static void ipip6_tunnel_bind_dev(struct net_device *dev)
  698. {
  699. struct net_device *tdev = NULL;
  700. struct ip_tunnel *tunnel;
  701. struct iphdr *iph;
  702. tunnel = netdev_priv(dev);
  703. iph = &tunnel->parms.iph;
  704. if (iph->daddr) {
  705. struct flowi fl = { .nl_u = { .ip4_u =
  706. { .daddr = iph->daddr,
  707. .saddr = iph->saddr,
  708. .tos = RT_TOS(iph->tos) } },
  709. .oif = tunnel->parms.link,
  710. .proto = IPPROTO_IPV6 };
  711. struct rtable *rt;
  712. if (!ip_route_output_key(dev_net(dev), &rt, &fl)) {
  713. tdev = rt->u.dst.dev;
  714. ip_rt_put(rt);
  715. }
  716. dev->flags |= IFF_POINTOPOINT;
  717. }
  718. if (!tdev && tunnel->parms.link)
  719. tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
  720. if (tdev) {
  721. dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
  722. dev->mtu = tdev->mtu - sizeof(struct iphdr);
  723. if (dev->mtu < IPV6_MIN_MTU)
  724. dev->mtu = IPV6_MIN_MTU;
  725. }
  726. dev->iflink = tunnel->parms.link;
  727. }
  728. static int
  729. ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
  730. {
  731. int err = 0;
  732. struct ip_tunnel_parm p;
  733. struct ip_tunnel_prl prl;
  734. struct ip_tunnel *t;
  735. struct net *net = dev_net(dev);
  736. struct sit_net *sitn = net_generic(net, sit_net_id);
  737. #ifdef CONFIG_IPV6_SIT_6RD
  738. struct ip_tunnel_6rd ip6rd;
  739. #endif
  740. switch (cmd) {
  741. case SIOCGETTUNNEL:
  742. #ifdef CONFIG_IPV6_SIT_6RD
  743. case SIOCGET6RD:
  744. #endif
  745. t = NULL;
  746. if (dev == sitn->fb_tunnel_dev) {
  747. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  748. err = -EFAULT;
  749. break;
  750. }
  751. t = ipip6_tunnel_locate(net, &p, 0);
  752. }
  753. if (t == NULL)
  754. t = netdev_priv(dev);
  755. err = -EFAULT;
  756. if (cmd == SIOCGETTUNNEL) {
  757. memcpy(&p, &t->parms, sizeof(p));
  758. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p,
  759. sizeof(p)))
  760. goto done;
  761. #ifdef CONFIG_IPV6_SIT_6RD
  762. } else {
  763. ipv6_addr_copy(&ip6rd.prefix, &t->ip6rd.prefix);
  764. ip6rd.relay_prefix = t->ip6rd.relay_prefix;
  765. ip6rd.prefixlen = t->ip6rd.prefixlen;
  766. ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen;
  767. if (copy_to_user(ifr->ifr_ifru.ifru_data, &ip6rd,
  768. sizeof(ip6rd)))
  769. goto done;
  770. #endif
  771. }
  772. err = 0;
  773. break;
  774. case SIOCADDTUNNEL:
  775. case SIOCCHGTUNNEL:
  776. err = -EPERM;
  777. if (!capable(CAP_NET_ADMIN))
  778. goto done;
  779. err = -EFAULT;
  780. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  781. goto done;
  782. err = -EINVAL;
  783. if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
  784. p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
  785. goto done;
  786. if (p.iph.ttl)
  787. p.iph.frag_off |= htons(IP_DF);
  788. t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
  789. if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  790. if (t != NULL) {
  791. if (t->dev != dev) {
  792. err = -EEXIST;
  793. break;
  794. }
  795. } else {
  796. if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
  797. (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
  798. err = -EINVAL;
  799. break;
  800. }
  801. t = netdev_priv(dev);
  802. ipip6_tunnel_unlink(sitn, t);
  803. t->parms.iph.saddr = p.iph.saddr;
  804. t->parms.iph.daddr = p.iph.daddr;
  805. memcpy(dev->dev_addr, &p.iph.saddr, 4);
  806. memcpy(dev->broadcast, &p.iph.daddr, 4);
  807. ipip6_tunnel_link(sitn, t);
  808. netdev_state_change(dev);
  809. }
  810. }
  811. if (t) {
  812. err = 0;
  813. if (cmd == SIOCCHGTUNNEL) {
  814. t->parms.iph.ttl = p.iph.ttl;
  815. t->parms.iph.tos = p.iph.tos;
  816. if (t->parms.link != p.link) {
  817. t->parms.link = p.link;
  818. ipip6_tunnel_bind_dev(dev);
  819. netdev_state_change(dev);
  820. }
  821. }
  822. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
  823. err = -EFAULT;
  824. } else
  825. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  826. break;
  827. case SIOCDELTUNNEL:
  828. err = -EPERM;
  829. if (!capable(CAP_NET_ADMIN))
  830. goto done;
  831. if (dev == sitn->fb_tunnel_dev) {
  832. err = -EFAULT;
  833. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  834. goto done;
  835. err = -ENOENT;
  836. if ((t = ipip6_tunnel_locate(net, &p, 0)) == NULL)
  837. goto done;
  838. err = -EPERM;
  839. if (t == netdev_priv(sitn->fb_tunnel_dev))
  840. goto done;
  841. dev = t->dev;
  842. }
  843. unregister_netdevice(dev);
  844. err = 0;
  845. break;
  846. case SIOCGETPRL:
  847. err = -EINVAL;
  848. if (dev == sitn->fb_tunnel_dev)
  849. goto done;
  850. err = -ENOENT;
  851. if (!(t = netdev_priv(dev)))
  852. goto done;
  853. err = ipip6_tunnel_get_prl(t, ifr->ifr_ifru.ifru_data);
  854. break;
  855. case SIOCADDPRL:
  856. case SIOCDELPRL:
  857. case SIOCCHGPRL:
  858. err = -EPERM;
  859. if (!capable(CAP_NET_ADMIN))
  860. goto done;
  861. err = -EINVAL;
  862. if (dev == sitn->fb_tunnel_dev)
  863. goto done;
  864. err = -EFAULT;
  865. if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
  866. goto done;
  867. err = -ENOENT;
  868. if (!(t = netdev_priv(dev)))
  869. goto done;
  870. switch (cmd) {
  871. case SIOCDELPRL:
  872. err = ipip6_tunnel_del_prl(t, &prl);
  873. break;
  874. case SIOCADDPRL:
  875. case SIOCCHGPRL:
  876. err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
  877. break;
  878. }
  879. netdev_state_change(dev);
  880. break;
  881. #ifdef CONFIG_IPV6_SIT_6RD
  882. case SIOCADD6RD:
  883. case SIOCCHG6RD:
  884. case SIOCDEL6RD:
  885. err = -EPERM;
  886. if (!capable(CAP_NET_ADMIN))
  887. goto done;
  888. err = -EFAULT;
  889. if (copy_from_user(&ip6rd, ifr->ifr_ifru.ifru_data,
  890. sizeof(ip6rd)))
  891. goto done;
  892. t = netdev_priv(dev);
  893. if (cmd != SIOCDEL6RD) {
  894. struct in6_addr prefix;
  895. __be32 relay_prefix;
  896. err = -EINVAL;
  897. if (ip6rd.relay_prefixlen > 32 ||
  898. ip6rd.prefixlen + (32 - ip6rd.relay_prefixlen) > 64)
  899. goto done;
  900. ipv6_addr_prefix(&prefix, &ip6rd.prefix,
  901. ip6rd.prefixlen);
  902. if (!ipv6_addr_equal(&prefix, &ip6rd.prefix))
  903. goto done;
  904. if (ip6rd.relay_prefixlen)
  905. relay_prefix = ip6rd.relay_prefix &
  906. htonl(0xffffffffUL <<
  907. (32 - ip6rd.relay_prefixlen));
  908. else
  909. relay_prefix = 0;
  910. if (relay_prefix != ip6rd.relay_prefix)
  911. goto done;
  912. ipv6_addr_copy(&t->ip6rd.prefix, &prefix);
  913. t->ip6rd.relay_prefix = relay_prefix;
  914. t->ip6rd.prefixlen = ip6rd.prefixlen;
  915. t->ip6rd.relay_prefixlen = ip6rd.relay_prefixlen;
  916. } else
  917. ipip6_tunnel_clone_6rd(dev, sitn);
  918. err = 0;
  919. break;
  920. #endif
  921. default:
  922. err = -EINVAL;
  923. }
  924. done:
  925. return err;
  926. }
  927. static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  928. {
  929. if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
  930. return -EINVAL;
  931. dev->mtu = new_mtu;
  932. return 0;
  933. }
  934. static const struct net_device_ops ipip6_netdev_ops = {
  935. .ndo_uninit = ipip6_tunnel_uninit,
  936. .ndo_start_xmit = ipip6_tunnel_xmit,
  937. .ndo_do_ioctl = ipip6_tunnel_ioctl,
  938. .ndo_change_mtu = ipip6_tunnel_change_mtu,
  939. };
  940. static void ipip6_tunnel_setup(struct net_device *dev)
  941. {
  942. dev->netdev_ops = &ipip6_netdev_ops;
  943. dev->destructor = free_netdev;
  944. dev->type = ARPHRD_SIT;
  945. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
  946. dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
  947. dev->flags = IFF_NOARP;
  948. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  949. dev->iflink = 0;
  950. dev->addr_len = 4;
  951. dev->features |= NETIF_F_NETNS_LOCAL;
  952. }
  953. static void ipip6_tunnel_init(struct net_device *dev)
  954. {
  955. struct ip_tunnel *tunnel = netdev_priv(dev);
  956. tunnel->dev = dev;
  957. strcpy(tunnel->parms.name, dev->name);
  958. memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
  959. memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
  960. ipip6_tunnel_bind_dev(dev);
  961. }
  962. static void __net_init ipip6_fb_tunnel_init(struct net_device *dev)
  963. {
  964. struct ip_tunnel *tunnel = netdev_priv(dev);
  965. struct iphdr *iph = &tunnel->parms.iph;
  966. struct net *net = dev_net(dev);
  967. struct sit_net *sitn = net_generic(net, sit_net_id);
  968. tunnel->dev = dev;
  969. strcpy(tunnel->parms.name, dev->name);
  970. iph->version = 4;
  971. iph->protocol = IPPROTO_IPV6;
  972. iph->ihl = 5;
  973. iph->ttl = 64;
  974. dev_hold(dev);
  975. sitn->tunnels_wc[0] = tunnel;
  976. }
  977. static struct xfrm_tunnel sit_handler = {
  978. .handler = ipip6_rcv,
  979. .err_handler = ipip6_err,
  980. .priority = 1,
  981. };
  982. static void __net_exit sit_destroy_tunnels(struct sit_net *sitn, struct list_head *head)
  983. {
  984. int prio;
  985. for (prio = 1; prio < 4; prio++) {
  986. int h;
  987. for (h = 0; h < HASH_SIZE; h++) {
  988. struct ip_tunnel *t = sitn->tunnels[prio][h];
  989. while (t != NULL) {
  990. unregister_netdevice_queue(t->dev, head);
  991. t = t->next;
  992. }
  993. }
  994. }
  995. }
  996. static int __net_init sit_init_net(struct net *net)
  997. {
  998. struct sit_net *sitn = net_generic(net, sit_net_id);
  999. int err;
  1000. sitn->tunnels[0] = sitn->tunnels_wc;
  1001. sitn->tunnels[1] = sitn->tunnels_l;
  1002. sitn->tunnels[2] = sitn->tunnels_r;
  1003. sitn->tunnels[3] = sitn->tunnels_r_l;
  1004. sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
  1005. ipip6_tunnel_setup);
  1006. if (!sitn->fb_tunnel_dev) {
  1007. err = -ENOMEM;
  1008. goto err_alloc_dev;
  1009. }
  1010. dev_net_set(sitn->fb_tunnel_dev, net);
  1011. ipip6_fb_tunnel_init(sitn->fb_tunnel_dev);
  1012. ipip6_tunnel_clone_6rd(sitn->fb_tunnel_dev, sitn);
  1013. if ((err = register_netdev(sitn->fb_tunnel_dev)))
  1014. goto err_reg_dev;
  1015. return 0;
  1016. err_reg_dev:
  1017. dev_put(sitn->fb_tunnel_dev);
  1018. free_netdev(sitn->fb_tunnel_dev);
  1019. err_alloc_dev:
  1020. return err;
  1021. }
  1022. static void __net_exit sit_exit_net(struct net *net)
  1023. {
  1024. struct sit_net *sitn = net_generic(net, sit_net_id);
  1025. LIST_HEAD(list);
  1026. rtnl_lock();
  1027. sit_destroy_tunnels(sitn, &list);
  1028. unregister_netdevice_queue(sitn->fb_tunnel_dev, &list);
  1029. unregister_netdevice_many(&list);
  1030. rtnl_unlock();
  1031. }
  1032. static struct pernet_operations sit_net_ops = {
  1033. .init = sit_init_net,
  1034. .exit = sit_exit_net,
  1035. .id = &sit_net_id,
  1036. .size = sizeof(struct sit_net),
  1037. };
  1038. static void __exit sit_cleanup(void)
  1039. {
  1040. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  1041. unregister_pernet_device(&sit_net_ops);
  1042. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  1043. }
  1044. static int __init sit_init(void)
  1045. {
  1046. int err;
  1047. printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
  1048. err = register_pernet_device(&sit_net_ops);
  1049. if (err < 0)
  1050. return err;
  1051. err = xfrm4_tunnel_register(&sit_handler, AF_INET6);
  1052. if (err < 0) {
  1053. unregister_pernet_device(&sit_net_ops);
  1054. printk(KERN_INFO "sit init: Can't add protocol\n");
  1055. }
  1056. return err;
  1057. }
  1058. module_init(sit_init);
  1059. module_exit(sit_cleanup);
  1060. MODULE_LICENSE("GPL");
  1061. MODULE_ALIAS("sit0");