sit.c 24 KB

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