sit.c 39 KB

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