sit.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/module.h>
  21. #include <linux/capability.h>
  22. #include <linux/errno.h>
  23. #include <linux/types.h>
  24. #include <linux/socket.h>
  25. #include <linux/sockios.h>
  26. #include <linux/net.h>
  27. #include <linux/in6.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/if_arp.h>
  30. #include <linux/icmp.h>
  31. #include <linux/slab.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 bool log_ecn_error = true;
  61. module_param(log_ecn_error, bool, 0644);
  62. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  63. static int ipip6_tunnel_init(struct net_device *dev);
  64. static void ipip6_tunnel_setup(struct net_device *dev);
  65. static void ipip6_dev_free(struct net_device *dev);
  66. static bool check_6rd(struct ip_tunnel *tunnel, const struct in6_addr *v6dst,
  67. __be32 *v4dst);
  68. static struct rtnl_link_ops sit_link_ops __read_mostly;
  69. static int sit_net_id __read_mostly;
  70. struct sit_net {
  71. struct ip_tunnel __rcu *tunnels_r_l[HASH_SIZE];
  72. struct ip_tunnel __rcu *tunnels_r[HASH_SIZE];
  73. struct ip_tunnel __rcu *tunnels_l[HASH_SIZE];
  74. struct ip_tunnel __rcu *tunnels_wc[1];
  75. struct ip_tunnel __rcu **tunnels[4];
  76. struct net_device *fb_tunnel_dev;
  77. };
  78. static struct rtnl_link_stats64 *ipip6_get_stats64(struct net_device *dev,
  79. struct rtnl_link_stats64 *tot)
  80. {
  81. int i;
  82. for_each_possible_cpu(i) {
  83. const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i);
  84. u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
  85. unsigned int start;
  86. do {
  87. start = u64_stats_fetch_begin_bh(&tstats->syncp);
  88. rx_packets = tstats->rx_packets;
  89. tx_packets = tstats->tx_packets;
  90. rx_bytes = tstats->rx_bytes;
  91. tx_bytes = tstats->tx_bytes;
  92. } while (u64_stats_fetch_retry_bh(&tstats->syncp, start));
  93. tot->rx_packets += rx_packets;
  94. tot->tx_packets += tx_packets;
  95. tot->rx_bytes += rx_bytes;
  96. tot->tx_bytes += tx_bytes;
  97. }
  98. tot->rx_errors = dev->stats.rx_errors;
  99. tot->rx_frame_errors = dev->stats.rx_frame_errors;
  100. tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
  101. tot->tx_carrier_errors = dev->stats.tx_carrier_errors;
  102. tot->tx_dropped = dev->stats.tx_dropped;
  103. tot->tx_aborted_errors = dev->stats.tx_aborted_errors;
  104. tot->tx_errors = dev->stats.tx_errors;
  105. return tot;
  106. }
  107. /*
  108. * Must be invoked with rcu_read_lock
  109. */
  110. static struct ip_tunnel *ipip6_tunnel_lookup(struct net *net,
  111. struct net_device *dev, __be32 remote, __be32 local)
  112. {
  113. unsigned int h0 = HASH(remote);
  114. unsigned int h1 = HASH(local);
  115. struct ip_tunnel *t;
  116. struct sit_net *sitn = net_generic(net, sit_net_id);
  117. for_each_ip_tunnel_rcu(t, sitn->tunnels_r_l[h0 ^ h1]) {
  118. if (local == t->parms.iph.saddr &&
  119. remote == t->parms.iph.daddr &&
  120. (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
  121. (t->dev->flags & IFF_UP))
  122. return t;
  123. }
  124. for_each_ip_tunnel_rcu(t, sitn->tunnels_r[h0]) {
  125. if (remote == t->parms.iph.daddr &&
  126. (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
  127. (t->dev->flags & IFF_UP))
  128. return t;
  129. }
  130. for_each_ip_tunnel_rcu(t, sitn->tunnels_l[h1]) {
  131. if (local == t->parms.iph.saddr &&
  132. (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
  133. (t->dev->flags & IFF_UP))
  134. return t;
  135. }
  136. t = rcu_dereference(sitn->tunnels_wc[0]);
  137. if ((t != NULL) && (t->dev->flags & IFF_UP))
  138. return t;
  139. return NULL;
  140. }
  141. static struct ip_tunnel __rcu **__ipip6_bucket(struct sit_net *sitn,
  142. struct ip_tunnel_parm *parms)
  143. {
  144. __be32 remote = parms->iph.daddr;
  145. __be32 local = parms->iph.saddr;
  146. unsigned int h = 0;
  147. int prio = 0;
  148. if (remote) {
  149. prio |= 2;
  150. h ^= HASH(remote);
  151. }
  152. if (local) {
  153. prio |= 1;
  154. h ^= HASH(local);
  155. }
  156. return &sitn->tunnels[prio][h];
  157. }
  158. static inline struct ip_tunnel __rcu **ipip6_bucket(struct sit_net *sitn,
  159. struct ip_tunnel *t)
  160. {
  161. return __ipip6_bucket(sitn, &t->parms);
  162. }
  163. static void ipip6_tunnel_unlink(struct sit_net *sitn, struct ip_tunnel *t)
  164. {
  165. struct ip_tunnel __rcu **tp;
  166. struct ip_tunnel *iter;
  167. for (tp = ipip6_bucket(sitn, t);
  168. (iter = rtnl_dereference(*tp)) != NULL;
  169. tp = &iter->next) {
  170. if (t == iter) {
  171. rcu_assign_pointer(*tp, t->next);
  172. break;
  173. }
  174. }
  175. }
  176. static void ipip6_tunnel_link(struct sit_net *sitn, struct ip_tunnel *t)
  177. {
  178. struct ip_tunnel __rcu **tp = ipip6_bucket(sitn, t);
  179. rcu_assign_pointer(t->next, rtnl_dereference(*tp));
  180. rcu_assign_pointer(*tp, t);
  181. }
  182. static void ipip6_tunnel_clone_6rd(struct net_device *dev, struct sit_net *sitn)
  183. {
  184. #ifdef CONFIG_IPV6_SIT_6RD
  185. struct ip_tunnel *t = netdev_priv(dev);
  186. if (t->dev == sitn->fb_tunnel_dev) {
  187. ipv6_addr_set(&t->ip6rd.prefix, htonl(0x20020000), 0, 0, 0);
  188. t->ip6rd.relay_prefix = 0;
  189. t->ip6rd.prefixlen = 16;
  190. t->ip6rd.relay_prefixlen = 0;
  191. } else {
  192. struct ip_tunnel *t0 = netdev_priv(sitn->fb_tunnel_dev);
  193. memcpy(&t->ip6rd, &t0->ip6rd, sizeof(t->ip6rd));
  194. }
  195. #endif
  196. }
  197. static int ipip6_tunnel_create(struct net_device *dev)
  198. {
  199. struct ip_tunnel *t = netdev_priv(dev);
  200. struct net *net = dev_net(dev);
  201. struct sit_net *sitn = net_generic(net, sit_net_id);
  202. int err;
  203. err = ipip6_tunnel_init(dev);
  204. if (err < 0)
  205. goto out;
  206. ipip6_tunnel_clone_6rd(dev, sitn);
  207. if ((__force u16)t->parms.i_flags & SIT_ISATAP)
  208. dev->priv_flags |= IFF_ISATAP;
  209. err = register_netdevice(dev);
  210. if (err < 0)
  211. goto out;
  212. strcpy(t->parms.name, dev->name);
  213. dev->rtnl_link_ops = &sit_link_ops;
  214. dev_hold(dev);
  215. ipip6_tunnel_link(sitn, t);
  216. return 0;
  217. out:
  218. return err;
  219. }
  220. static struct ip_tunnel *ipip6_tunnel_locate(struct net *net,
  221. struct ip_tunnel_parm *parms, int create)
  222. {
  223. __be32 remote = parms->iph.daddr;
  224. __be32 local = parms->iph.saddr;
  225. struct ip_tunnel *t, *nt;
  226. struct ip_tunnel __rcu **tp;
  227. struct net_device *dev;
  228. char name[IFNAMSIZ];
  229. struct sit_net *sitn = net_generic(net, sit_net_id);
  230. for (tp = __ipip6_bucket(sitn, parms);
  231. (t = rtnl_dereference(*tp)) != NULL;
  232. tp = &t->next) {
  233. if (local == t->parms.iph.saddr &&
  234. remote == t->parms.iph.daddr &&
  235. parms->link == t->parms.link) {
  236. if (create)
  237. return NULL;
  238. else
  239. return t;
  240. }
  241. }
  242. if (!create)
  243. goto failed;
  244. if (parms->name[0])
  245. strlcpy(name, parms->name, IFNAMSIZ);
  246. else
  247. strcpy(name, "sit%d");
  248. dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
  249. if (dev == NULL)
  250. return NULL;
  251. dev_net_set(dev, net);
  252. nt = netdev_priv(dev);
  253. nt->parms = *parms;
  254. if (ipip6_tunnel_create(dev) < 0)
  255. goto failed_free;
  256. return nt;
  257. failed_free:
  258. ipip6_dev_free(dev);
  259. failed:
  260. return NULL;
  261. }
  262. #define for_each_prl_rcu(start) \
  263. for (prl = rcu_dereference(start); \
  264. prl; \
  265. prl = rcu_dereference(prl->next))
  266. static struct ip_tunnel_prl_entry *
  267. __ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr)
  268. {
  269. struct ip_tunnel_prl_entry *prl;
  270. for_each_prl_rcu(t->prl)
  271. if (prl->addr == addr)
  272. break;
  273. return prl;
  274. }
  275. static int ipip6_tunnel_get_prl(struct ip_tunnel *t,
  276. struct ip_tunnel_prl __user *a)
  277. {
  278. struct ip_tunnel_prl kprl, *kp;
  279. struct ip_tunnel_prl_entry *prl;
  280. unsigned int cmax, c = 0, ca, len;
  281. int ret = 0;
  282. if (copy_from_user(&kprl, a, sizeof(kprl)))
  283. return -EFAULT;
  284. cmax = kprl.datalen / sizeof(kprl);
  285. if (cmax > 1 && kprl.addr != htonl(INADDR_ANY))
  286. cmax = 1;
  287. /* For simple GET or for root users,
  288. * we try harder to allocate.
  289. */
  290. kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
  291. kcalloc(cmax, sizeof(*kp), GFP_KERNEL) :
  292. NULL;
  293. rcu_read_lock();
  294. ca = t->prl_count < cmax ? t->prl_count : cmax;
  295. if (!kp) {
  296. /* We don't try hard to allocate much memory for
  297. * non-root users.
  298. * For root users, retry allocating enough memory for
  299. * the answer.
  300. */
  301. kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
  302. if (!kp) {
  303. ret = -ENOMEM;
  304. goto out;
  305. }
  306. }
  307. c = 0;
  308. for_each_prl_rcu(t->prl) {
  309. if (c >= cmax)
  310. break;
  311. if (kprl.addr != htonl(INADDR_ANY) && prl->addr != kprl.addr)
  312. continue;
  313. kp[c].addr = prl->addr;
  314. kp[c].flags = prl->flags;
  315. c++;
  316. if (kprl.addr != htonl(INADDR_ANY))
  317. break;
  318. }
  319. out:
  320. rcu_read_unlock();
  321. len = sizeof(*kp) * c;
  322. ret = 0;
  323. if ((len && copy_to_user(a + 1, kp, len)) || put_user(len, &a->datalen))
  324. ret = -EFAULT;
  325. kfree(kp);
  326. return ret;
  327. }
  328. static int
  329. ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg)
  330. {
  331. struct ip_tunnel_prl_entry *p;
  332. int err = 0;
  333. if (a->addr == htonl(INADDR_ANY))
  334. return -EINVAL;
  335. ASSERT_RTNL();
  336. for (p = rtnl_dereference(t->prl); p; p = rtnl_dereference(p->next)) {
  337. if (p->addr == a->addr) {
  338. if (chg) {
  339. p->flags = a->flags;
  340. goto out;
  341. }
  342. err = -EEXIST;
  343. goto out;
  344. }
  345. }
  346. if (chg) {
  347. err = -ENXIO;
  348. goto out;
  349. }
  350. p = kzalloc(sizeof(struct ip_tunnel_prl_entry), GFP_KERNEL);
  351. if (!p) {
  352. err = -ENOBUFS;
  353. goto out;
  354. }
  355. p->next = t->prl;
  356. p->addr = a->addr;
  357. p->flags = a->flags;
  358. t->prl_count++;
  359. rcu_assign_pointer(t->prl, p);
  360. out:
  361. return err;
  362. }
  363. static void prl_list_destroy_rcu(struct rcu_head *head)
  364. {
  365. struct ip_tunnel_prl_entry *p, *n;
  366. p = container_of(head, struct ip_tunnel_prl_entry, rcu_head);
  367. do {
  368. n = rcu_dereference_protected(p->next, 1);
  369. kfree(p);
  370. p = n;
  371. } while (p);
  372. }
  373. static int
  374. ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
  375. {
  376. struct ip_tunnel_prl_entry *x;
  377. struct ip_tunnel_prl_entry __rcu **p;
  378. int err = 0;
  379. ASSERT_RTNL();
  380. if (a && a->addr != htonl(INADDR_ANY)) {
  381. for (p = &t->prl;
  382. (x = rtnl_dereference(*p)) != NULL;
  383. p = &x->next) {
  384. if (x->addr == a->addr) {
  385. *p = x->next;
  386. kfree_rcu(x, rcu_head);
  387. t->prl_count--;
  388. goto out;
  389. }
  390. }
  391. err = -ENXIO;
  392. } else {
  393. x = rtnl_dereference(t->prl);
  394. if (x) {
  395. t->prl_count = 0;
  396. call_rcu(&x->rcu_head, prl_list_destroy_rcu);
  397. t->prl = NULL;
  398. }
  399. }
  400. out:
  401. return err;
  402. }
  403. static int
  404. isatap_chksrc(struct sk_buff *skb, const struct iphdr *iph, struct ip_tunnel *t)
  405. {
  406. struct ip_tunnel_prl_entry *p;
  407. int ok = 1;
  408. rcu_read_lock();
  409. p = __ipip6_tunnel_locate_prl(t, iph->saddr);
  410. if (p) {
  411. if (p->flags & PRL_DEFAULT)
  412. skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT;
  413. else
  414. skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT;
  415. } else {
  416. const struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr;
  417. if (ipv6_addr_is_isatap(addr6) &&
  418. (addr6->s6_addr32[3] == iph->saddr) &&
  419. ipv6_chk_prefix(addr6, t->dev))
  420. skb->ndisc_nodetype = NDISC_NODETYPE_HOST;
  421. else
  422. ok = 0;
  423. }
  424. rcu_read_unlock();
  425. return ok;
  426. }
  427. static void ipip6_tunnel_uninit(struct net_device *dev)
  428. {
  429. struct net *net = dev_net(dev);
  430. struct sit_net *sitn = net_generic(net, sit_net_id);
  431. if (dev == sitn->fb_tunnel_dev) {
  432. RCU_INIT_POINTER(sitn->tunnels_wc[0], NULL);
  433. } else {
  434. ipip6_tunnel_unlink(sitn, netdev_priv(dev));
  435. ipip6_tunnel_del_prl(netdev_priv(dev), NULL);
  436. }
  437. dev_put(dev);
  438. }
  439. static int ipip6_err(struct sk_buff *skb, u32 info)
  440. {
  441. /* All the routers (except for Linux) return only
  442. 8 bytes of packet payload. It means, that precise relaying of
  443. ICMP in the real Internet is absolutely infeasible.
  444. */
  445. const struct iphdr *iph = (const struct iphdr *)skb->data;
  446. const int type = icmp_hdr(skb)->type;
  447. const int code = icmp_hdr(skb)->code;
  448. struct ip_tunnel *t;
  449. int err;
  450. switch (type) {
  451. default:
  452. case ICMP_PARAMETERPROB:
  453. return 0;
  454. case ICMP_DEST_UNREACH:
  455. switch (code) {
  456. case ICMP_SR_FAILED:
  457. case ICMP_PORT_UNREACH:
  458. /* Impossible event. */
  459. return 0;
  460. default:
  461. /* All others are translated to HOST_UNREACH.
  462. rfc2003 contains "deep thoughts" about NET_UNREACH,
  463. I believe they are just ether pollution. --ANK
  464. */
  465. break;
  466. }
  467. break;
  468. case ICMP_TIME_EXCEEDED:
  469. if (code != ICMP_EXC_TTL)
  470. return 0;
  471. break;
  472. case ICMP_REDIRECT:
  473. break;
  474. }
  475. err = -ENOENT;
  476. t = ipip6_tunnel_lookup(dev_net(skb->dev),
  477. skb->dev,
  478. iph->daddr,
  479. iph->saddr);
  480. if (t == NULL)
  481. goto out;
  482. if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
  483. ipv4_update_pmtu(skb, dev_net(skb->dev), info,
  484. t->dev->ifindex, 0, IPPROTO_IPV6, 0);
  485. err = 0;
  486. goto out;
  487. }
  488. if (type == ICMP_REDIRECT) {
  489. ipv4_redirect(skb, dev_net(skb->dev), t->dev->ifindex, 0,
  490. IPPROTO_IPV6, 0);
  491. err = 0;
  492. goto out;
  493. }
  494. if (t->parms.iph.daddr == 0)
  495. goto out;
  496. err = 0;
  497. if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
  498. goto out;
  499. if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
  500. t->err_count++;
  501. else
  502. t->err_count = 1;
  503. t->err_time = jiffies;
  504. out:
  505. return err;
  506. }
  507. static inline bool is_spoofed_6rd(struct ip_tunnel *tunnel, const __be32 v4addr,
  508. const struct in6_addr *v6addr)
  509. {
  510. __be32 v4embed = 0;
  511. if (check_6rd(tunnel, v6addr, &v4embed) && v4addr != v4embed)
  512. return true;
  513. return false;
  514. }
  515. static int ipip6_rcv(struct sk_buff *skb)
  516. {
  517. const struct iphdr *iph = ip_hdr(skb);
  518. struct ip_tunnel *tunnel;
  519. int err;
  520. tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
  521. iph->saddr, iph->daddr);
  522. if (tunnel != NULL) {
  523. struct pcpu_tstats *tstats;
  524. secpath_reset(skb);
  525. skb->mac_header = skb->network_header;
  526. skb_reset_network_header(skb);
  527. IPCB(skb)->flags = 0;
  528. skb->protocol = htons(ETH_P_IPV6);
  529. skb->pkt_type = PACKET_HOST;
  530. if (tunnel->dev->priv_flags & IFF_ISATAP) {
  531. if (!isatap_chksrc(skb, iph, tunnel)) {
  532. tunnel->dev->stats.rx_errors++;
  533. goto out;
  534. }
  535. } else {
  536. if (is_spoofed_6rd(tunnel, iph->saddr,
  537. &ipv6_hdr(skb)->saddr) ||
  538. is_spoofed_6rd(tunnel, iph->daddr,
  539. &ipv6_hdr(skb)->daddr)) {
  540. tunnel->dev->stats.rx_errors++;
  541. goto out;
  542. }
  543. }
  544. __skb_tunnel_rx(skb, tunnel->dev);
  545. err = IP_ECN_decapsulate(iph, skb);
  546. if (unlikely(err)) {
  547. if (log_ecn_error)
  548. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  549. &iph->saddr, iph->tos);
  550. if (err > 1) {
  551. ++tunnel->dev->stats.rx_frame_errors;
  552. ++tunnel->dev->stats.rx_errors;
  553. goto out;
  554. }
  555. }
  556. tstats = this_cpu_ptr(tunnel->dev->tstats);
  557. tstats->rx_packets++;
  558. tstats->rx_bytes += skb->len;
  559. netif_rx(skb);
  560. return 0;
  561. }
  562. /* no tunnel matched, let upstream know, ipsec may handle it */
  563. return 1;
  564. out:
  565. kfree_skb(skb);
  566. return 0;
  567. }
  568. /*
  569. * If the IPv6 address comes from 6rd / 6to4 (RFC 3056) addr space this function
  570. * stores the embedded IPv4 address in v4dst and returns true.
  571. */
  572. static bool check_6rd(struct ip_tunnel *tunnel, const struct in6_addr *v6dst,
  573. __be32 *v4dst)
  574. {
  575. #ifdef CONFIG_IPV6_SIT_6RD
  576. if (ipv6_prefix_equal(v6dst, &tunnel->ip6rd.prefix,
  577. tunnel->ip6rd.prefixlen)) {
  578. unsigned int pbw0, pbi0;
  579. int pbi1;
  580. u32 d;
  581. pbw0 = tunnel->ip6rd.prefixlen >> 5;
  582. pbi0 = tunnel->ip6rd.prefixlen & 0x1f;
  583. d = (ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >>
  584. tunnel->ip6rd.relay_prefixlen;
  585. pbi1 = pbi0 - tunnel->ip6rd.relay_prefixlen;
  586. if (pbi1 > 0)
  587. d |= ntohl(v6dst->s6_addr32[pbw0 + 1]) >>
  588. (32 - pbi1);
  589. *v4dst = tunnel->ip6rd.relay_prefix | htonl(d);
  590. return true;
  591. }
  592. #else
  593. if (v6dst->s6_addr16[0] == htons(0x2002)) {
  594. /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
  595. memcpy(v4dst, &v6dst->s6_addr16[1], 4);
  596. return true;
  597. }
  598. #endif
  599. return false;
  600. }
  601. static inline __be32 try_6rd(struct ip_tunnel *tunnel,
  602. const struct in6_addr *v6dst)
  603. {
  604. __be32 dst = 0;
  605. check_6rd(tunnel, v6dst, &dst);
  606. return dst;
  607. }
  608. /*
  609. * This function assumes it is being called from dev_queue_xmit()
  610. * and that skb is filled properly by that function.
  611. */
  612. static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
  613. struct net_device *dev)
  614. {
  615. struct ip_tunnel *tunnel = netdev_priv(dev);
  616. const struct iphdr *tiph = &tunnel->parms.iph;
  617. const struct ipv6hdr *iph6 = ipv6_hdr(skb);
  618. u8 tos = tunnel->parms.iph.tos;
  619. __be16 df = tiph->frag_off;
  620. struct rtable *rt; /* Route to the other host */
  621. struct net_device *tdev; /* Device to other host */
  622. struct iphdr *iph; /* Our new IP header */
  623. unsigned int max_headroom; /* The extra header space needed */
  624. __be32 dst = tiph->daddr;
  625. struct flowi4 fl4;
  626. int mtu;
  627. const struct in6_addr *addr6;
  628. int addr_type;
  629. if (skb->protocol != htons(ETH_P_IPV6))
  630. goto tx_error;
  631. if (tos == 1)
  632. tos = ipv6_get_dsfield(iph6);
  633. /* ISATAP (RFC4214) - must come before 6to4 */
  634. if (dev->priv_flags & IFF_ISATAP) {
  635. struct neighbour *neigh = NULL;
  636. bool do_tx_error = false;
  637. if (skb_dst(skb))
  638. neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
  639. if (neigh == NULL) {
  640. net_dbg_ratelimited("sit: nexthop == NULL\n");
  641. goto tx_error;
  642. }
  643. addr6 = (const struct in6_addr *)&neigh->primary_key;
  644. addr_type = ipv6_addr_type(addr6);
  645. if ((addr_type & IPV6_ADDR_UNICAST) &&
  646. ipv6_addr_is_isatap(addr6))
  647. dst = addr6->s6_addr32[3];
  648. else
  649. do_tx_error = true;
  650. neigh_release(neigh);
  651. if (do_tx_error)
  652. goto tx_error;
  653. }
  654. if (!dst)
  655. dst = try_6rd(tunnel, &iph6->daddr);
  656. if (!dst) {
  657. struct neighbour *neigh = NULL;
  658. bool do_tx_error = false;
  659. if (skb_dst(skb))
  660. neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
  661. if (neigh == NULL) {
  662. net_dbg_ratelimited("sit: nexthop == NULL\n");
  663. goto tx_error;
  664. }
  665. addr6 = (const struct in6_addr *)&neigh->primary_key;
  666. addr_type = ipv6_addr_type(addr6);
  667. if (addr_type == IPV6_ADDR_ANY) {
  668. addr6 = &ipv6_hdr(skb)->daddr;
  669. addr_type = ipv6_addr_type(addr6);
  670. }
  671. if ((addr_type & IPV6_ADDR_COMPATv4) != 0)
  672. dst = addr6->s6_addr32[3];
  673. else
  674. do_tx_error = true;
  675. neigh_release(neigh);
  676. if (do_tx_error)
  677. goto tx_error;
  678. }
  679. rt = ip_route_output_ports(dev_net(dev), &fl4, NULL,
  680. dst, tiph->saddr,
  681. 0, 0,
  682. IPPROTO_IPV6, RT_TOS(tos),
  683. tunnel->parms.link);
  684. if (IS_ERR(rt)) {
  685. dev->stats.tx_carrier_errors++;
  686. goto tx_error_icmp;
  687. }
  688. if (rt->rt_type != RTN_UNICAST) {
  689. ip_rt_put(rt);
  690. dev->stats.tx_carrier_errors++;
  691. goto tx_error_icmp;
  692. }
  693. tdev = rt->dst.dev;
  694. if (tdev == dev) {
  695. ip_rt_put(rt);
  696. dev->stats.collisions++;
  697. goto tx_error;
  698. }
  699. if (df) {
  700. mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
  701. if (mtu < 68) {
  702. dev->stats.collisions++;
  703. ip_rt_put(rt);
  704. goto tx_error;
  705. }
  706. if (mtu < IPV6_MIN_MTU) {
  707. mtu = IPV6_MIN_MTU;
  708. df = 0;
  709. }
  710. if (tunnel->parms.iph.daddr && skb_dst(skb))
  711. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
  712. if (skb->len > mtu) {
  713. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  714. ip_rt_put(rt);
  715. goto tx_error;
  716. }
  717. }
  718. if (tunnel->err_count > 0) {
  719. if (time_before(jiffies,
  720. tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
  721. tunnel->err_count--;
  722. dst_link_failure(skb);
  723. } else
  724. tunnel->err_count = 0;
  725. }
  726. /*
  727. * Okay, now see if we can stuff it in the buffer as-is.
  728. */
  729. max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
  730. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  731. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  732. struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
  733. if (!new_skb) {
  734. ip_rt_put(rt);
  735. dev->stats.tx_dropped++;
  736. dev_kfree_skb(skb);
  737. return NETDEV_TX_OK;
  738. }
  739. if (skb->sk)
  740. skb_set_owner_w(new_skb, skb->sk);
  741. dev_kfree_skb(skb);
  742. skb = new_skb;
  743. iph6 = ipv6_hdr(skb);
  744. }
  745. skb->transport_header = skb->network_header;
  746. skb_push(skb, sizeof(struct iphdr));
  747. skb_reset_network_header(skb);
  748. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  749. IPCB(skb)->flags = 0;
  750. skb_dst_drop(skb);
  751. skb_dst_set(skb, &rt->dst);
  752. /*
  753. * Push down and install the IPIP header.
  754. */
  755. iph = ip_hdr(skb);
  756. iph->version = 4;
  757. iph->ihl = sizeof(struct iphdr)>>2;
  758. iph->frag_off = df;
  759. iph->protocol = IPPROTO_IPV6;
  760. iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
  761. iph->daddr = fl4.daddr;
  762. iph->saddr = fl4.saddr;
  763. if ((iph->ttl = tiph->ttl) == 0)
  764. iph->ttl = iph6->hop_limit;
  765. iptunnel_xmit(skb, dev);
  766. return NETDEV_TX_OK;
  767. tx_error_icmp:
  768. dst_link_failure(skb);
  769. tx_error:
  770. dev->stats.tx_errors++;
  771. dev_kfree_skb(skb);
  772. return NETDEV_TX_OK;
  773. }
  774. static void ipip6_tunnel_bind_dev(struct net_device *dev)
  775. {
  776. struct net_device *tdev = NULL;
  777. struct ip_tunnel *tunnel;
  778. const struct iphdr *iph;
  779. struct flowi4 fl4;
  780. tunnel = netdev_priv(dev);
  781. iph = &tunnel->parms.iph;
  782. if (iph->daddr) {
  783. struct rtable *rt = ip_route_output_ports(dev_net(dev), &fl4, NULL,
  784. iph->daddr, iph->saddr,
  785. 0, 0,
  786. IPPROTO_IPV6,
  787. RT_TOS(iph->tos),
  788. tunnel->parms.link);
  789. if (!IS_ERR(rt)) {
  790. tdev = rt->dst.dev;
  791. ip_rt_put(rt);
  792. }
  793. dev->flags |= IFF_POINTOPOINT;
  794. }
  795. if (!tdev && tunnel->parms.link)
  796. tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
  797. if (tdev) {
  798. dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
  799. dev->mtu = tdev->mtu - sizeof(struct iphdr);
  800. if (dev->mtu < IPV6_MIN_MTU)
  801. dev->mtu = IPV6_MIN_MTU;
  802. }
  803. dev->iflink = tunnel->parms.link;
  804. }
  805. static void ipip6_tunnel_update(struct ip_tunnel *t, struct ip_tunnel_parm *p)
  806. {
  807. struct net *net = dev_net(t->dev);
  808. struct sit_net *sitn = net_generic(net, sit_net_id);
  809. ipip6_tunnel_unlink(sitn, t);
  810. synchronize_net();
  811. t->parms.iph.saddr = p->iph.saddr;
  812. t->parms.iph.daddr = p->iph.daddr;
  813. memcpy(t->dev->dev_addr, &p->iph.saddr, 4);
  814. memcpy(t->dev->broadcast, &p->iph.daddr, 4);
  815. ipip6_tunnel_link(sitn, t);
  816. t->parms.iph.ttl = p->iph.ttl;
  817. t->parms.iph.tos = p->iph.tos;
  818. if (t->parms.link != p->link) {
  819. t->parms.link = p->link;
  820. ipip6_tunnel_bind_dev(t->dev);
  821. }
  822. netdev_state_change(t->dev);
  823. }
  824. #ifdef CONFIG_IPV6_SIT_6RD
  825. static int ipip6_tunnel_update_6rd(struct ip_tunnel *t,
  826. struct ip_tunnel_6rd *ip6rd)
  827. {
  828. struct in6_addr prefix;
  829. __be32 relay_prefix;
  830. if (ip6rd->relay_prefixlen > 32 ||
  831. ip6rd->prefixlen + (32 - ip6rd->relay_prefixlen) > 64)
  832. return -EINVAL;
  833. ipv6_addr_prefix(&prefix, &ip6rd->prefix, ip6rd->prefixlen);
  834. if (!ipv6_addr_equal(&prefix, &ip6rd->prefix))
  835. return -EINVAL;
  836. if (ip6rd->relay_prefixlen)
  837. relay_prefix = ip6rd->relay_prefix &
  838. htonl(0xffffffffUL <<
  839. (32 - ip6rd->relay_prefixlen));
  840. else
  841. relay_prefix = 0;
  842. if (relay_prefix != ip6rd->relay_prefix)
  843. return -EINVAL;
  844. t->ip6rd.prefix = prefix;
  845. t->ip6rd.relay_prefix = relay_prefix;
  846. t->ip6rd.prefixlen = ip6rd->prefixlen;
  847. t->ip6rd.relay_prefixlen = ip6rd->relay_prefixlen;
  848. netdev_state_change(t->dev);
  849. return 0;
  850. }
  851. #endif
  852. static int
  853. ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
  854. {
  855. int err = 0;
  856. struct ip_tunnel_parm p;
  857. struct ip_tunnel_prl prl;
  858. struct ip_tunnel *t;
  859. struct net *net = dev_net(dev);
  860. struct sit_net *sitn = net_generic(net, sit_net_id);
  861. #ifdef CONFIG_IPV6_SIT_6RD
  862. struct ip_tunnel_6rd ip6rd;
  863. #endif
  864. switch (cmd) {
  865. case SIOCGETTUNNEL:
  866. #ifdef CONFIG_IPV6_SIT_6RD
  867. case SIOCGET6RD:
  868. #endif
  869. t = NULL;
  870. if (dev == sitn->fb_tunnel_dev) {
  871. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  872. err = -EFAULT;
  873. break;
  874. }
  875. t = ipip6_tunnel_locate(net, &p, 0);
  876. }
  877. if (t == NULL)
  878. t = netdev_priv(dev);
  879. err = -EFAULT;
  880. if (cmd == SIOCGETTUNNEL) {
  881. memcpy(&p, &t->parms, sizeof(p));
  882. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p,
  883. sizeof(p)))
  884. goto done;
  885. #ifdef CONFIG_IPV6_SIT_6RD
  886. } else {
  887. ip6rd.prefix = t->ip6rd.prefix;
  888. ip6rd.relay_prefix = t->ip6rd.relay_prefix;
  889. ip6rd.prefixlen = t->ip6rd.prefixlen;
  890. ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen;
  891. if (copy_to_user(ifr->ifr_ifru.ifru_data, &ip6rd,
  892. sizeof(ip6rd)))
  893. goto done;
  894. #endif
  895. }
  896. err = 0;
  897. break;
  898. case SIOCADDTUNNEL:
  899. case SIOCCHGTUNNEL:
  900. err = -EPERM;
  901. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  902. goto done;
  903. err = -EFAULT;
  904. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  905. goto done;
  906. err = -EINVAL;
  907. if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
  908. p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
  909. goto done;
  910. if (p.iph.ttl)
  911. p.iph.frag_off |= htons(IP_DF);
  912. t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
  913. if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  914. if (t != NULL) {
  915. if (t->dev != dev) {
  916. err = -EEXIST;
  917. break;
  918. }
  919. } else {
  920. if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
  921. (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
  922. err = -EINVAL;
  923. break;
  924. }
  925. t = netdev_priv(dev);
  926. }
  927. ipip6_tunnel_update(t, &p);
  928. }
  929. if (t) {
  930. err = 0;
  931. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
  932. err = -EFAULT;
  933. } else
  934. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  935. break;
  936. case SIOCDELTUNNEL:
  937. err = -EPERM;
  938. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  939. goto done;
  940. if (dev == sitn->fb_tunnel_dev) {
  941. err = -EFAULT;
  942. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  943. goto done;
  944. err = -ENOENT;
  945. if ((t = ipip6_tunnel_locate(net, &p, 0)) == NULL)
  946. goto done;
  947. err = -EPERM;
  948. if (t == netdev_priv(sitn->fb_tunnel_dev))
  949. goto done;
  950. dev = t->dev;
  951. }
  952. unregister_netdevice(dev);
  953. err = 0;
  954. break;
  955. case SIOCGETPRL:
  956. err = -EINVAL;
  957. if (dev == sitn->fb_tunnel_dev)
  958. goto done;
  959. err = -ENOENT;
  960. if (!(t = netdev_priv(dev)))
  961. goto done;
  962. err = ipip6_tunnel_get_prl(t, ifr->ifr_ifru.ifru_data);
  963. break;
  964. case SIOCADDPRL:
  965. case SIOCDELPRL:
  966. case SIOCCHGPRL:
  967. err = -EPERM;
  968. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  969. goto done;
  970. err = -EINVAL;
  971. if (dev == sitn->fb_tunnel_dev)
  972. goto done;
  973. err = -EFAULT;
  974. if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
  975. goto done;
  976. err = -ENOENT;
  977. if (!(t = netdev_priv(dev)))
  978. goto done;
  979. switch (cmd) {
  980. case SIOCDELPRL:
  981. err = ipip6_tunnel_del_prl(t, &prl);
  982. break;
  983. case SIOCADDPRL:
  984. case SIOCCHGPRL:
  985. err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
  986. break;
  987. }
  988. netdev_state_change(dev);
  989. break;
  990. #ifdef CONFIG_IPV6_SIT_6RD
  991. case SIOCADD6RD:
  992. case SIOCCHG6RD:
  993. case SIOCDEL6RD:
  994. err = -EPERM;
  995. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  996. goto done;
  997. err = -EFAULT;
  998. if (copy_from_user(&ip6rd, ifr->ifr_ifru.ifru_data,
  999. sizeof(ip6rd)))
  1000. goto done;
  1001. t = netdev_priv(dev);
  1002. if (cmd != SIOCDEL6RD) {
  1003. err = ipip6_tunnel_update_6rd(t, &ip6rd);
  1004. if (err < 0)
  1005. goto done;
  1006. } else
  1007. ipip6_tunnel_clone_6rd(dev, sitn);
  1008. err = 0;
  1009. break;
  1010. #endif
  1011. default:
  1012. err = -EINVAL;
  1013. }
  1014. done:
  1015. return err;
  1016. }
  1017. static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  1018. {
  1019. if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
  1020. return -EINVAL;
  1021. dev->mtu = new_mtu;
  1022. return 0;
  1023. }
  1024. static const struct net_device_ops ipip6_netdev_ops = {
  1025. .ndo_uninit = ipip6_tunnel_uninit,
  1026. .ndo_start_xmit = ipip6_tunnel_xmit,
  1027. .ndo_do_ioctl = ipip6_tunnel_ioctl,
  1028. .ndo_change_mtu = ipip6_tunnel_change_mtu,
  1029. .ndo_get_stats64= ipip6_get_stats64,
  1030. };
  1031. static void ipip6_dev_free(struct net_device *dev)
  1032. {
  1033. free_percpu(dev->tstats);
  1034. free_netdev(dev);
  1035. }
  1036. static void ipip6_tunnel_setup(struct net_device *dev)
  1037. {
  1038. dev->netdev_ops = &ipip6_netdev_ops;
  1039. dev->destructor = ipip6_dev_free;
  1040. dev->type = ARPHRD_SIT;
  1041. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
  1042. dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
  1043. dev->flags = IFF_NOARP;
  1044. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1045. dev->iflink = 0;
  1046. dev->addr_len = 4;
  1047. dev->features |= NETIF_F_NETNS_LOCAL;
  1048. dev->features |= NETIF_F_LLTX;
  1049. }
  1050. static int ipip6_tunnel_init(struct net_device *dev)
  1051. {
  1052. struct ip_tunnel *tunnel = netdev_priv(dev);
  1053. tunnel->dev = dev;
  1054. memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
  1055. memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
  1056. ipip6_tunnel_bind_dev(dev);
  1057. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1058. if (!dev->tstats)
  1059. return -ENOMEM;
  1060. return 0;
  1061. }
  1062. static int __net_init ipip6_fb_tunnel_init(struct net_device *dev)
  1063. {
  1064. struct ip_tunnel *tunnel = netdev_priv(dev);
  1065. struct iphdr *iph = &tunnel->parms.iph;
  1066. struct net *net = dev_net(dev);
  1067. struct sit_net *sitn = net_generic(net, sit_net_id);
  1068. tunnel->dev = dev;
  1069. strcpy(tunnel->parms.name, dev->name);
  1070. iph->version = 4;
  1071. iph->protocol = IPPROTO_IPV6;
  1072. iph->ihl = 5;
  1073. iph->ttl = 64;
  1074. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1075. if (!dev->tstats)
  1076. return -ENOMEM;
  1077. dev_hold(dev);
  1078. rcu_assign_pointer(sitn->tunnels_wc[0], tunnel);
  1079. return 0;
  1080. }
  1081. static void ipip6_netlink_parms(struct nlattr *data[],
  1082. struct ip_tunnel_parm *parms)
  1083. {
  1084. memset(parms, 0, sizeof(*parms));
  1085. parms->iph.version = 4;
  1086. parms->iph.protocol = IPPROTO_IPV6;
  1087. parms->iph.ihl = 5;
  1088. parms->iph.ttl = 64;
  1089. if (!data)
  1090. return;
  1091. if (data[IFLA_IPTUN_LINK])
  1092. parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
  1093. if (data[IFLA_IPTUN_LOCAL])
  1094. parms->iph.saddr = nla_get_be32(data[IFLA_IPTUN_LOCAL]);
  1095. if (data[IFLA_IPTUN_REMOTE])
  1096. parms->iph.daddr = nla_get_be32(data[IFLA_IPTUN_REMOTE]);
  1097. if (data[IFLA_IPTUN_TTL]) {
  1098. parms->iph.ttl = nla_get_u8(data[IFLA_IPTUN_TTL]);
  1099. if (parms->iph.ttl)
  1100. parms->iph.frag_off = htons(IP_DF);
  1101. }
  1102. if (data[IFLA_IPTUN_TOS])
  1103. parms->iph.tos = nla_get_u8(data[IFLA_IPTUN_TOS]);
  1104. if (!data[IFLA_IPTUN_PMTUDISC] || nla_get_u8(data[IFLA_IPTUN_PMTUDISC]))
  1105. parms->iph.frag_off = htons(IP_DF);
  1106. if (data[IFLA_IPTUN_FLAGS])
  1107. parms->i_flags = nla_get_be16(data[IFLA_IPTUN_FLAGS]);
  1108. }
  1109. #ifdef CONFIG_IPV6_SIT_6RD
  1110. /* This function returns true when 6RD attributes are present in the nl msg */
  1111. static bool ipip6_netlink_6rd_parms(struct nlattr *data[],
  1112. struct ip_tunnel_6rd *ip6rd)
  1113. {
  1114. bool ret = false;
  1115. memset(ip6rd, 0, sizeof(*ip6rd));
  1116. if (!data)
  1117. return ret;
  1118. if (data[IFLA_IPTUN_6RD_PREFIX]) {
  1119. ret = true;
  1120. nla_memcpy(&ip6rd->prefix, data[IFLA_IPTUN_6RD_PREFIX],
  1121. sizeof(struct in6_addr));
  1122. }
  1123. if (data[IFLA_IPTUN_6RD_RELAY_PREFIX]) {
  1124. ret = true;
  1125. ip6rd->relay_prefix =
  1126. nla_get_be32(data[IFLA_IPTUN_6RD_RELAY_PREFIX]);
  1127. }
  1128. if (data[IFLA_IPTUN_6RD_PREFIXLEN]) {
  1129. ret = true;
  1130. ip6rd->prefixlen = nla_get_u16(data[IFLA_IPTUN_6RD_PREFIXLEN]);
  1131. }
  1132. if (data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]) {
  1133. ret = true;
  1134. ip6rd->relay_prefixlen =
  1135. nla_get_u16(data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]);
  1136. }
  1137. return ret;
  1138. }
  1139. #endif
  1140. static int ipip6_newlink(struct net *src_net, struct net_device *dev,
  1141. struct nlattr *tb[], struct nlattr *data[])
  1142. {
  1143. struct net *net = dev_net(dev);
  1144. struct ip_tunnel *nt;
  1145. #ifdef CONFIG_IPV6_SIT_6RD
  1146. struct ip_tunnel_6rd ip6rd;
  1147. #endif
  1148. int err;
  1149. nt = netdev_priv(dev);
  1150. ipip6_netlink_parms(data, &nt->parms);
  1151. if (ipip6_tunnel_locate(net, &nt->parms, 0))
  1152. return -EEXIST;
  1153. err = ipip6_tunnel_create(dev);
  1154. if (err < 0)
  1155. return err;
  1156. #ifdef CONFIG_IPV6_SIT_6RD
  1157. if (ipip6_netlink_6rd_parms(data, &ip6rd))
  1158. err = ipip6_tunnel_update_6rd(nt, &ip6rd);
  1159. #endif
  1160. return err;
  1161. }
  1162. static int ipip6_changelink(struct net_device *dev, struct nlattr *tb[],
  1163. struct nlattr *data[])
  1164. {
  1165. struct ip_tunnel *t;
  1166. struct ip_tunnel_parm p;
  1167. struct net *net = dev_net(dev);
  1168. struct sit_net *sitn = net_generic(net, sit_net_id);
  1169. #ifdef CONFIG_IPV6_SIT_6RD
  1170. struct ip_tunnel_6rd ip6rd;
  1171. #endif
  1172. if (dev == sitn->fb_tunnel_dev)
  1173. return -EINVAL;
  1174. ipip6_netlink_parms(data, &p);
  1175. if (((dev->flags & IFF_POINTOPOINT) && !p.iph.daddr) ||
  1176. (!(dev->flags & IFF_POINTOPOINT) && p.iph.daddr))
  1177. return -EINVAL;
  1178. t = ipip6_tunnel_locate(net, &p, 0);
  1179. if (t) {
  1180. if (t->dev != dev)
  1181. return -EEXIST;
  1182. } else
  1183. t = netdev_priv(dev);
  1184. ipip6_tunnel_update(t, &p);
  1185. #ifdef CONFIG_IPV6_SIT_6RD
  1186. if (ipip6_netlink_6rd_parms(data, &ip6rd))
  1187. return ipip6_tunnel_update_6rd(t, &ip6rd);
  1188. #endif
  1189. return 0;
  1190. }
  1191. static size_t ipip6_get_size(const struct net_device *dev)
  1192. {
  1193. return
  1194. /* IFLA_IPTUN_LINK */
  1195. nla_total_size(4) +
  1196. /* IFLA_IPTUN_LOCAL */
  1197. nla_total_size(4) +
  1198. /* IFLA_IPTUN_REMOTE */
  1199. nla_total_size(4) +
  1200. /* IFLA_IPTUN_TTL */
  1201. nla_total_size(1) +
  1202. /* IFLA_IPTUN_TOS */
  1203. nla_total_size(1) +
  1204. /* IFLA_IPTUN_PMTUDISC */
  1205. nla_total_size(1) +
  1206. /* IFLA_IPTUN_FLAGS */
  1207. nla_total_size(2) +
  1208. #ifdef CONFIG_IPV6_SIT_6RD
  1209. /* IFLA_IPTUN_6RD_PREFIX */
  1210. nla_total_size(sizeof(struct in6_addr)) +
  1211. /* IFLA_IPTUN_6RD_RELAY_PREFIX */
  1212. nla_total_size(4) +
  1213. /* IFLA_IPTUN_6RD_PREFIXLEN */
  1214. nla_total_size(2) +
  1215. /* IFLA_IPTUN_6RD_RELAY_PREFIXLEN */
  1216. nla_total_size(2) +
  1217. #endif
  1218. 0;
  1219. }
  1220. static int ipip6_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1221. {
  1222. struct ip_tunnel *tunnel = netdev_priv(dev);
  1223. struct ip_tunnel_parm *parm = &tunnel->parms;
  1224. if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
  1225. nla_put_be32(skb, IFLA_IPTUN_LOCAL, parm->iph.saddr) ||
  1226. nla_put_be32(skb, IFLA_IPTUN_REMOTE, parm->iph.daddr) ||
  1227. nla_put_u8(skb, IFLA_IPTUN_TTL, parm->iph.ttl) ||
  1228. nla_put_u8(skb, IFLA_IPTUN_TOS, parm->iph.tos) ||
  1229. nla_put_u8(skb, IFLA_IPTUN_PMTUDISC,
  1230. !!(parm->iph.frag_off & htons(IP_DF))) ||
  1231. nla_put_be16(skb, IFLA_IPTUN_FLAGS, parm->i_flags))
  1232. goto nla_put_failure;
  1233. #ifdef CONFIG_IPV6_SIT_6RD
  1234. if (nla_put(skb, IFLA_IPTUN_6RD_PREFIX, sizeof(struct in6_addr),
  1235. &tunnel->ip6rd.prefix) ||
  1236. nla_put_be32(skb, IFLA_IPTUN_6RD_RELAY_PREFIX,
  1237. tunnel->ip6rd.relay_prefix) ||
  1238. nla_put_u16(skb, IFLA_IPTUN_6RD_PREFIXLEN,
  1239. tunnel->ip6rd.prefixlen) ||
  1240. nla_put_u16(skb, IFLA_IPTUN_6RD_RELAY_PREFIXLEN,
  1241. tunnel->ip6rd.relay_prefixlen))
  1242. goto nla_put_failure;
  1243. #endif
  1244. return 0;
  1245. nla_put_failure:
  1246. return -EMSGSIZE;
  1247. }
  1248. static const struct nla_policy ipip6_policy[IFLA_IPTUN_MAX + 1] = {
  1249. [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
  1250. [IFLA_IPTUN_LOCAL] = { .type = NLA_U32 },
  1251. [IFLA_IPTUN_REMOTE] = { .type = NLA_U32 },
  1252. [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
  1253. [IFLA_IPTUN_TOS] = { .type = NLA_U8 },
  1254. [IFLA_IPTUN_PMTUDISC] = { .type = NLA_U8 },
  1255. [IFLA_IPTUN_FLAGS] = { .type = NLA_U16 },
  1256. #ifdef CONFIG_IPV6_SIT_6RD
  1257. [IFLA_IPTUN_6RD_PREFIX] = { .len = sizeof(struct in6_addr) },
  1258. [IFLA_IPTUN_6RD_RELAY_PREFIX] = { .type = NLA_U32 },
  1259. [IFLA_IPTUN_6RD_PREFIXLEN] = { .type = NLA_U16 },
  1260. [IFLA_IPTUN_6RD_RELAY_PREFIXLEN] = { .type = NLA_U16 },
  1261. #endif
  1262. };
  1263. static struct rtnl_link_ops sit_link_ops __read_mostly = {
  1264. .kind = "sit",
  1265. .maxtype = IFLA_IPTUN_MAX,
  1266. .policy = ipip6_policy,
  1267. .priv_size = sizeof(struct ip_tunnel),
  1268. .setup = ipip6_tunnel_setup,
  1269. .newlink = ipip6_newlink,
  1270. .changelink = ipip6_changelink,
  1271. .get_size = ipip6_get_size,
  1272. .fill_info = ipip6_fill_info,
  1273. };
  1274. static struct xfrm_tunnel sit_handler __read_mostly = {
  1275. .handler = ipip6_rcv,
  1276. .err_handler = ipip6_err,
  1277. .priority = 1,
  1278. };
  1279. static void __net_exit sit_destroy_tunnels(struct sit_net *sitn, struct list_head *head)
  1280. {
  1281. int prio;
  1282. for (prio = 1; prio < 4; prio++) {
  1283. int h;
  1284. for (h = 0; h < HASH_SIZE; h++) {
  1285. struct ip_tunnel *t;
  1286. t = rtnl_dereference(sitn->tunnels[prio][h]);
  1287. while (t != NULL) {
  1288. unregister_netdevice_queue(t->dev, head);
  1289. t = rtnl_dereference(t->next);
  1290. }
  1291. }
  1292. }
  1293. }
  1294. static int __net_init sit_init_net(struct net *net)
  1295. {
  1296. struct sit_net *sitn = net_generic(net, sit_net_id);
  1297. struct ip_tunnel *t;
  1298. int err;
  1299. sitn->tunnels[0] = sitn->tunnels_wc;
  1300. sitn->tunnels[1] = sitn->tunnels_l;
  1301. sitn->tunnels[2] = sitn->tunnels_r;
  1302. sitn->tunnels[3] = sitn->tunnels_r_l;
  1303. sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
  1304. ipip6_tunnel_setup);
  1305. if (!sitn->fb_tunnel_dev) {
  1306. err = -ENOMEM;
  1307. goto err_alloc_dev;
  1308. }
  1309. dev_net_set(sitn->fb_tunnel_dev, net);
  1310. err = ipip6_fb_tunnel_init(sitn->fb_tunnel_dev);
  1311. if (err)
  1312. goto err_dev_free;
  1313. ipip6_tunnel_clone_6rd(sitn->fb_tunnel_dev, sitn);
  1314. if ((err = register_netdev(sitn->fb_tunnel_dev)))
  1315. goto err_reg_dev;
  1316. t = netdev_priv(sitn->fb_tunnel_dev);
  1317. strcpy(t->parms.name, sitn->fb_tunnel_dev->name);
  1318. return 0;
  1319. err_reg_dev:
  1320. dev_put(sitn->fb_tunnel_dev);
  1321. err_dev_free:
  1322. ipip6_dev_free(sitn->fb_tunnel_dev);
  1323. err_alloc_dev:
  1324. return err;
  1325. }
  1326. static void __net_exit sit_exit_net(struct net *net)
  1327. {
  1328. struct sit_net *sitn = net_generic(net, sit_net_id);
  1329. LIST_HEAD(list);
  1330. rtnl_lock();
  1331. sit_destroy_tunnels(sitn, &list);
  1332. unregister_netdevice_queue(sitn->fb_tunnel_dev, &list);
  1333. unregister_netdevice_many(&list);
  1334. rtnl_unlock();
  1335. }
  1336. static struct pernet_operations sit_net_ops = {
  1337. .init = sit_init_net,
  1338. .exit = sit_exit_net,
  1339. .id = &sit_net_id,
  1340. .size = sizeof(struct sit_net),
  1341. };
  1342. static void __exit sit_cleanup(void)
  1343. {
  1344. rtnl_link_unregister(&sit_link_ops);
  1345. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  1346. unregister_pernet_device(&sit_net_ops);
  1347. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  1348. }
  1349. static int __init sit_init(void)
  1350. {
  1351. int err;
  1352. pr_info("IPv6 over IPv4 tunneling driver\n");
  1353. err = register_pernet_device(&sit_net_ops);
  1354. if (err < 0)
  1355. return err;
  1356. err = xfrm4_tunnel_register(&sit_handler, AF_INET6);
  1357. if (err < 0) {
  1358. pr_info("%s: can't add protocol\n", __func__);
  1359. goto xfrm_tunnel_failed;
  1360. }
  1361. err = rtnl_link_register(&sit_link_ops);
  1362. if (err < 0)
  1363. goto rtnl_link_failed;
  1364. out:
  1365. return err;
  1366. rtnl_link_failed:
  1367. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  1368. xfrm_tunnel_failed:
  1369. unregister_pernet_device(&sit_net_ops);
  1370. goto out;
  1371. }
  1372. module_init(sit_init);
  1373. module_exit(sit_cleanup);
  1374. MODULE_LICENSE("GPL");
  1375. MODULE_ALIAS_NETDEV("sit0");