sit.c 24 KB

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