ip6_gre.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727
  1. /*
  2. * GRE over IPv6 protocol decoder.
  3. *
  4. * Authors: Dmitry Kozlov (xeb@mail.ru)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/capability.h>
  14. #include <linux/module.h>
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/slab.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/in.h>
  22. #include <linux/tcp.h>
  23. #include <linux/udp.h>
  24. #include <linux/if_arp.h>
  25. #include <linux/mroute.h>
  26. #include <linux/init.h>
  27. #include <linux/in6.h>
  28. #include <linux/inetdevice.h>
  29. #include <linux/igmp.h>
  30. #include <linux/netfilter_ipv4.h>
  31. #include <linux/etherdevice.h>
  32. #include <linux/if_ether.h>
  33. #include <linux/hash.h>
  34. #include <linux/if_tunnel.h>
  35. #include <linux/ip6_tunnel.h>
  36. #include <net/sock.h>
  37. #include <net/ip.h>
  38. #include <net/ip_tunnels.h>
  39. #include <net/icmp.h>
  40. #include <net/protocol.h>
  41. #include <net/addrconf.h>
  42. #include <net/arp.h>
  43. #include <net/checksum.h>
  44. #include <net/dsfield.h>
  45. #include <net/inet_ecn.h>
  46. #include <net/xfrm.h>
  47. #include <net/net_namespace.h>
  48. #include <net/netns/generic.h>
  49. #include <net/rtnetlink.h>
  50. #include <net/ipv6.h>
  51. #include <net/ip6_fib.h>
  52. #include <net/ip6_route.h>
  53. #include <net/ip6_tunnel.h>
  54. static bool log_ecn_error = true;
  55. module_param(log_ecn_error, bool, 0644);
  56. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  57. #define HASH_SIZE_SHIFT 5
  58. #define HASH_SIZE (1 << HASH_SIZE_SHIFT)
  59. static int ip6gre_net_id __read_mostly;
  60. struct ip6gre_net {
  61. struct ip6_tnl __rcu *tunnels[4][HASH_SIZE];
  62. struct net_device *fb_tunnel_dev;
  63. };
  64. static struct rtnl_link_ops ip6gre_link_ops __read_mostly;
  65. static struct rtnl_link_ops ip6gre_tap_ops __read_mostly;
  66. static int ip6gre_tunnel_init(struct net_device *dev);
  67. static void ip6gre_tunnel_setup(struct net_device *dev);
  68. static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t);
  69. static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu);
  70. /* Tunnel hash table */
  71. /*
  72. 4 hash tables:
  73. 3: (remote,local)
  74. 2: (remote,*)
  75. 1: (*,local)
  76. 0: (*,*)
  77. We require exact key match i.e. if a key is present in packet
  78. it will match only tunnel with the same key; if it is not present,
  79. it will match only keyless tunnel.
  80. All keysless packets, if not matched configured keyless tunnels
  81. will match fallback tunnel.
  82. */
  83. #define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(HASH_SIZE - 1))
  84. static u32 HASH_ADDR(const struct in6_addr *addr)
  85. {
  86. u32 hash = ipv6_addr_hash(addr);
  87. return hash_32(hash, HASH_SIZE_SHIFT);
  88. }
  89. #define tunnels_r_l tunnels[3]
  90. #define tunnels_r tunnels[2]
  91. #define tunnels_l tunnels[1]
  92. #define tunnels_wc tunnels[0]
  93. /* Given src, dst and key, find appropriate for input tunnel. */
  94. static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev,
  95. const struct in6_addr *remote, const struct in6_addr *local,
  96. __be32 key, __be16 gre_proto)
  97. {
  98. struct net *net = dev_net(dev);
  99. int link = dev->ifindex;
  100. unsigned int h0 = HASH_ADDR(remote);
  101. unsigned int h1 = HASH_KEY(key);
  102. struct ip6_tnl *t, *cand = NULL;
  103. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  104. int dev_type = (gre_proto == htons(ETH_P_TEB)) ?
  105. ARPHRD_ETHER : ARPHRD_IP6GRE;
  106. int score, cand_score = 4;
  107. for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) {
  108. if (!ipv6_addr_equal(local, &t->parms.laddr) ||
  109. !ipv6_addr_equal(remote, &t->parms.raddr) ||
  110. key != t->parms.i_key ||
  111. !(t->dev->flags & IFF_UP))
  112. continue;
  113. if (t->dev->type != ARPHRD_IP6GRE &&
  114. t->dev->type != dev_type)
  115. continue;
  116. score = 0;
  117. if (t->parms.link != link)
  118. score |= 1;
  119. if (t->dev->type != dev_type)
  120. score |= 2;
  121. if (score == 0)
  122. return t;
  123. if (score < cand_score) {
  124. cand = t;
  125. cand_score = score;
  126. }
  127. }
  128. for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) {
  129. if (!ipv6_addr_equal(remote, &t->parms.raddr) ||
  130. key != t->parms.i_key ||
  131. !(t->dev->flags & IFF_UP))
  132. continue;
  133. if (t->dev->type != ARPHRD_IP6GRE &&
  134. t->dev->type != dev_type)
  135. continue;
  136. score = 0;
  137. if (t->parms.link != link)
  138. score |= 1;
  139. if (t->dev->type != dev_type)
  140. score |= 2;
  141. if (score == 0)
  142. return t;
  143. if (score < cand_score) {
  144. cand = t;
  145. cand_score = score;
  146. }
  147. }
  148. for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) {
  149. if ((!ipv6_addr_equal(local, &t->parms.laddr) &&
  150. (!ipv6_addr_equal(local, &t->parms.raddr) ||
  151. !ipv6_addr_is_multicast(local))) ||
  152. key != t->parms.i_key ||
  153. !(t->dev->flags & IFF_UP))
  154. continue;
  155. if (t->dev->type != ARPHRD_IP6GRE &&
  156. t->dev->type != dev_type)
  157. continue;
  158. score = 0;
  159. if (t->parms.link != link)
  160. score |= 1;
  161. if (t->dev->type != dev_type)
  162. score |= 2;
  163. if (score == 0)
  164. return t;
  165. if (score < cand_score) {
  166. cand = t;
  167. cand_score = score;
  168. }
  169. }
  170. for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) {
  171. if (t->parms.i_key != key ||
  172. !(t->dev->flags & IFF_UP))
  173. continue;
  174. if (t->dev->type != ARPHRD_IP6GRE &&
  175. t->dev->type != dev_type)
  176. continue;
  177. score = 0;
  178. if (t->parms.link != link)
  179. score |= 1;
  180. if (t->dev->type != dev_type)
  181. score |= 2;
  182. if (score == 0)
  183. return t;
  184. if (score < cand_score) {
  185. cand = t;
  186. cand_score = score;
  187. }
  188. }
  189. if (cand)
  190. return cand;
  191. dev = ign->fb_tunnel_dev;
  192. if (dev->flags & IFF_UP)
  193. return netdev_priv(dev);
  194. return NULL;
  195. }
  196. static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign,
  197. const struct __ip6_tnl_parm *p)
  198. {
  199. const struct in6_addr *remote = &p->raddr;
  200. const struct in6_addr *local = &p->laddr;
  201. unsigned int h = HASH_KEY(p->i_key);
  202. int prio = 0;
  203. if (!ipv6_addr_any(local))
  204. prio |= 1;
  205. if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) {
  206. prio |= 2;
  207. h ^= HASH_ADDR(remote);
  208. }
  209. return &ign->tunnels[prio][h];
  210. }
  211. static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign,
  212. const struct ip6_tnl *t)
  213. {
  214. return __ip6gre_bucket(ign, &t->parms);
  215. }
  216. static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t)
  217. {
  218. struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t);
  219. rcu_assign_pointer(t->next, rtnl_dereference(*tp));
  220. rcu_assign_pointer(*tp, t);
  221. }
  222. static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t)
  223. {
  224. struct ip6_tnl __rcu **tp;
  225. struct ip6_tnl *iter;
  226. for (tp = ip6gre_bucket(ign, t);
  227. (iter = rtnl_dereference(*tp)) != NULL;
  228. tp = &iter->next) {
  229. if (t == iter) {
  230. rcu_assign_pointer(*tp, t->next);
  231. break;
  232. }
  233. }
  234. }
  235. static struct ip6_tnl *ip6gre_tunnel_find(struct net *net,
  236. const struct __ip6_tnl_parm *parms,
  237. int type)
  238. {
  239. const struct in6_addr *remote = &parms->raddr;
  240. const struct in6_addr *local = &parms->laddr;
  241. __be32 key = parms->i_key;
  242. int link = parms->link;
  243. struct ip6_tnl *t;
  244. struct ip6_tnl __rcu **tp;
  245. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  246. for (tp = __ip6gre_bucket(ign, parms);
  247. (t = rtnl_dereference(*tp)) != NULL;
  248. tp = &t->next)
  249. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  250. ipv6_addr_equal(remote, &t->parms.raddr) &&
  251. key == t->parms.i_key &&
  252. link == t->parms.link &&
  253. type == t->dev->type)
  254. break;
  255. return t;
  256. }
  257. static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net,
  258. const struct __ip6_tnl_parm *parms, int create)
  259. {
  260. struct ip6_tnl *t, *nt;
  261. struct net_device *dev;
  262. char name[IFNAMSIZ];
  263. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  264. t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE);
  265. if (t && create)
  266. return NULL;
  267. if (t || !create)
  268. return t;
  269. if (parms->name[0])
  270. strlcpy(name, parms->name, IFNAMSIZ);
  271. else
  272. strcpy(name, "ip6gre%d");
  273. dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
  274. ip6gre_tunnel_setup);
  275. if (!dev)
  276. return NULL;
  277. dev_net_set(dev, net);
  278. nt = netdev_priv(dev);
  279. nt->parms = *parms;
  280. dev->rtnl_link_ops = &ip6gre_link_ops;
  281. nt->dev = dev;
  282. nt->net = dev_net(dev);
  283. ip6gre_tnl_link_config(nt, 1);
  284. if (register_netdevice(dev) < 0)
  285. goto failed_free;
  286. /* Can use a lockless transmit, unless we generate output sequences */
  287. if (!(nt->parms.o_flags & GRE_SEQ))
  288. dev->features |= NETIF_F_LLTX;
  289. dev_hold(dev);
  290. ip6gre_tunnel_link(ign, nt);
  291. return nt;
  292. failed_free:
  293. free_netdev(dev);
  294. return NULL;
  295. }
  296. static void ip6gre_tunnel_uninit(struct net_device *dev)
  297. {
  298. struct ip6_tnl *t = netdev_priv(dev);
  299. struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
  300. ip6gre_tunnel_unlink(ign, t);
  301. dev_put(dev);
  302. }
  303. static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  304. u8 type, u8 code, int offset, __be32 info)
  305. {
  306. const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)skb->data;
  307. __be16 *p = (__be16 *)(skb->data + offset);
  308. int grehlen = offset + 4;
  309. struct ip6_tnl *t;
  310. __be16 flags;
  311. flags = p[0];
  312. if (flags&(GRE_CSUM|GRE_KEY|GRE_SEQ|GRE_ROUTING|GRE_VERSION)) {
  313. if (flags&(GRE_VERSION|GRE_ROUTING))
  314. return;
  315. if (flags&GRE_KEY) {
  316. grehlen += 4;
  317. if (flags&GRE_CSUM)
  318. grehlen += 4;
  319. }
  320. }
  321. /* If only 8 bytes returned, keyed message will be dropped here */
  322. if (!pskb_may_pull(skb, grehlen))
  323. return;
  324. ipv6h = (const struct ipv6hdr *)skb->data;
  325. p = (__be16 *)(skb->data + offset);
  326. t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
  327. flags & GRE_KEY ?
  328. *(((__be32 *)p) + (grehlen / 4) - 1) : 0,
  329. p[1]);
  330. if (!t)
  331. return;
  332. switch (type) {
  333. __u32 teli;
  334. struct ipv6_tlv_tnl_enc_lim *tel;
  335. __u32 mtu;
  336. case ICMPV6_DEST_UNREACH:
  337. net_warn_ratelimited("%s: Path to destination invalid or inactive!\n",
  338. t->parms.name);
  339. break;
  340. case ICMPV6_TIME_EXCEED:
  341. if (code == ICMPV6_EXC_HOPLIMIT) {
  342. net_warn_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
  343. t->parms.name);
  344. }
  345. break;
  346. case ICMPV6_PARAMPROB:
  347. teli = 0;
  348. if (code == ICMPV6_HDR_FIELD)
  349. teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
  350. if (teli && teli == be32_to_cpu(info) - 2) {
  351. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
  352. if (tel->encap_limit == 0) {
  353. net_warn_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
  354. t->parms.name);
  355. }
  356. } else {
  357. net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
  358. t->parms.name);
  359. }
  360. break;
  361. case ICMPV6_PKT_TOOBIG:
  362. mtu = be32_to_cpu(info) - offset;
  363. if (mtu < IPV6_MIN_MTU)
  364. mtu = IPV6_MIN_MTU;
  365. t->dev->mtu = mtu;
  366. break;
  367. }
  368. if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO))
  369. t->err_count++;
  370. else
  371. t->err_count = 1;
  372. t->err_time = jiffies;
  373. }
  374. static int ip6gre_rcv(struct sk_buff *skb)
  375. {
  376. const struct ipv6hdr *ipv6h;
  377. u8 *h;
  378. __be16 flags;
  379. __sum16 csum = 0;
  380. __be32 key = 0;
  381. u32 seqno = 0;
  382. struct ip6_tnl *tunnel;
  383. int offset = 4;
  384. __be16 gre_proto;
  385. int err;
  386. if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
  387. goto drop;
  388. ipv6h = ipv6_hdr(skb);
  389. h = skb->data;
  390. flags = *(__be16 *)h;
  391. if (flags&(GRE_CSUM|GRE_KEY|GRE_ROUTING|GRE_SEQ|GRE_VERSION)) {
  392. /* - Version must be 0.
  393. - We do not support routing headers.
  394. */
  395. if (flags&(GRE_VERSION|GRE_ROUTING))
  396. goto drop;
  397. if (flags&GRE_CSUM) {
  398. csum = skb_checksum_simple_validate(skb);
  399. offset += 4;
  400. }
  401. if (flags&GRE_KEY) {
  402. key = *(__be32 *)(h + offset);
  403. offset += 4;
  404. }
  405. if (flags&GRE_SEQ) {
  406. seqno = ntohl(*(__be32 *)(h + offset));
  407. offset += 4;
  408. }
  409. }
  410. gre_proto = *(__be16 *)(h + 2);
  411. tunnel = ip6gre_tunnel_lookup(skb->dev,
  412. &ipv6h->saddr, &ipv6h->daddr, key,
  413. gre_proto);
  414. if (tunnel) {
  415. struct pcpu_sw_netstats *tstats;
  416. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  417. goto drop;
  418. if (!ip6_tnl_rcv_ctl(tunnel, &ipv6h->daddr, &ipv6h->saddr)) {
  419. tunnel->dev->stats.rx_dropped++;
  420. goto drop;
  421. }
  422. skb->protocol = gre_proto;
  423. /* WCCP version 1 and 2 protocol decoding.
  424. * - Change protocol to IPv6
  425. * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
  426. */
  427. if (flags == 0 && gre_proto == htons(ETH_P_WCCP)) {
  428. skb->protocol = htons(ETH_P_IPV6);
  429. if ((*(h + offset) & 0xF0) != 0x40)
  430. offset += 4;
  431. }
  432. skb->mac_header = skb->network_header;
  433. __pskb_pull(skb, offset);
  434. skb_postpull_rcsum(skb, skb_transport_header(skb), offset);
  435. if (((flags&GRE_CSUM) && csum) ||
  436. (!(flags&GRE_CSUM) && tunnel->parms.i_flags&GRE_CSUM)) {
  437. tunnel->dev->stats.rx_crc_errors++;
  438. tunnel->dev->stats.rx_errors++;
  439. goto drop;
  440. }
  441. if (tunnel->parms.i_flags&GRE_SEQ) {
  442. if (!(flags&GRE_SEQ) ||
  443. (tunnel->i_seqno &&
  444. (s32)(seqno - tunnel->i_seqno) < 0)) {
  445. tunnel->dev->stats.rx_fifo_errors++;
  446. tunnel->dev->stats.rx_errors++;
  447. goto drop;
  448. }
  449. tunnel->i_seqno = seqno + 1;
  450. }
  451. /* Warning: All skb pointers will be invalidated! */
  452. if (tunnel->dev->type == ARPHRD_ETHER) {
  453. if (!pskb_may_pull(skb, ETH_HLEN)) {
  454. tunnel->dev->stats.rx_length_errors++;
  455. tunnel->dev->stats.rx_errors++;
  456. goto drop;
  457. }
  458. ipv6h = ipv6_hdr(skb);
  459. skb->protocol = eth_type_trans(skb, tunnel->dev);
  460. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
  461. }
  462. __skb_tunnel_rx(skb, tunnel->dev, tunnel->net);
  463. skb_reset_network_header(skb);
  464. err = IP6_ECN_decapsulate(ipv6h, skb);
  465. if (unlikely(err)) {
  466. if (log_ecn_error)
  467. net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n",
  468. &ipv6h->saddr,
  469. ipv6_get_dsfield(ipv6h));
  470. if (err > 1) {
  471. ++tunnel->dev->stats.rx_frame_errors;
  472. ++tunnel->dev->stats.rx_errors;
  473. goto drop;
  474. }
  475. }
  476. tstats = this_cpu_ptr(tunnel->dev->tstats);
  477. u64_stats_update_begin(&tstats->syncp);
  478. tstats->rx_packets++;
  479. tstats->rx_bytes += skb->len;
  480. u64_stats_update_end(&tstats->syncp);
  481. netif_rx(skb);
  482. return 0;
  483. }
  484. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  485. drop:
  486. kfree_skb(skb);
  487. return 0;
  488. }
  489. struct ipv6_tel_txoption {
  490. struct ipv6_txoptions ops;
  491. __u8 dst_opt[8];
  492. };
  493. static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
  494. {
  495. memset(opt, 0, sizeof(struct ipv6_tel_txoption));
  496. opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
  497. opt->dst_opt[3] = 1;
  498. opt->dst_opt[4] = encap_limit;
  499. opt->dst_opt[5] = IPV6_TLV_PADN;
  500. opt->dst_opt[6] = 1;
  501. opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
  502. opt->ops.opt_nflen = 8;
  503. }
  504. static netdev_tx_t ip6gre_xmit2(struct sk_buff *skb,
  505. struct net_device *dev,
  506. __u8 dsfield,
  507. struct flowi6 *fl6,
  508. int encap_limit,
  509. __u32 *pmtu)
  510. {
  511. struct ip6_tnl *tunnel = netdev_priv(dev);
  512. struct net *net = tunnel->net;
  513. struct net_device *tdev; /* Device to other host */
  514. struct ipv6hdr *ipv6h; /* Our new IP header */
  515. unsigned int max_headroom = 0; /* The extra header space needed */
  516. int gre_hlen;
  517. struct ipv6_tel_txoption opt;
  518. int mtu;
  519. struct dst_entry *dst = NULL, *ndst = NULL;
  520. struct net_device_stats *stats = &tunnel->dev->stats;
  521. int err = -1;
  522. u8 proto;
  523. struct sk_buff *new_skb;
  524. __be16 protocol;
  525. if (dev->type == ARPHRD_ETHER)
  526. IPCB(skb)->flags = 0;
  527. if (dev->header_ops && dev->type == ARPHRD_IP6GRE) {
  528. gre_hlen = 0;
  529. ipv6h = (struct ipv6hdr *)skb->data;
  530. fl6->daddr = ipv6h->daddr;
  531. } else {
  532. gre_hlen = tunnel->hlen;
  533. fl6->daddr = tunnel->parms.raddr;
  534. }
  535. if (!fl6->flowi6_mark)
  536. dst = ip6_tnl_dst_check(tunnel);
  537. if (!dst) {
  538. ndst = ip6_route_output(net, NULL, fl6);
  539. if (ndst->error)
  540. goto tx_err_link_failure;
  541. ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(fl6), NULL, 0);
  542. if (IS_ERR(ndst)) {
  543. err = PTR_ERR(ndst);
  544. ndst = NULL;
  545. goto tx_err_link_failure;
  546. }
  547. dst = ndst;
  548. }
  549. tdev = dst->dev;
  550. if (tdev == dev) {
  551. stats->collisions++;
  552. net_warn_ratelimited("%s: Local routing loop detected!\n",
  553. tunnel->parms.name);
  554. goto tx_err_dst_release;
  555. }
  556. mtu = dst_mtu(dst) - sizeof(*ipv6h);
  557. if (encap_limit >= 0) {
  558. max_headroom += 8;
  559. mtu -= 8;
  560. }
  561. if (mtu < IPV6_MIN_MTU)
  562. mtu = IPV6_MIN_MTU;
  563. if (skb_dst(skb))
  564. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
  565. if (skb->len > mtu) {
  566. *pmtu = mtu;
  567. err = -EMSGSIZE;
  568. goto tx_err_dst_release;
  569. }
  570. if (tunnel->err_count > 0) {
  571. if (time_before(jiffies,
  572. tunnel->err_time + IP6TUNNEL_ERR_TIMEO)) {
  573. tunnel->err_count--;
  574. dst_link_failure(skb);
  575. } else
  576. tunnel->err_count = 0;
  577. }
  578. skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(dev)));
  579. max_headroom += LL_RESERVED_SPACE(tdev) + gre_hlen + dst->header_len;
  580. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  581. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  582. new_skb = skb_realloc_headroom(skb, max_headroom);
  583. if (max_headroom > dev->needed_headroom)
  584. dev->needed_headroom = max_headroom;
  585. if (!new_skb)
  586. goto tx_err_dst_release;
  587. if (skb->sk)
  588. skb_set_owner_w(new_skb, skb->sk);
  589. consume_skb(skb);
  590. skb = new_skb;
  591. }
  592. if (fl6->flowi6_mark) {
  593. skb_dst_set(skb, dst);
  594. ndst = NULL;
  595. } else {
  596. skb_dst_set_noref(skb, dst);
  597. }
  598. proto = NEXTHDR_GRE;
  599. if (encap_limit >= 0) {
  600. init_tel_txopt(&opt, encap_limit);
  601. ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
  602. }
  603. if (likely(!skb->encapsulation)) {
  604. skb_reset_inner_headers(skb);
  605. skb->encapsulation = 1;
  606. }
  607. skb_push(skb, gre_hlen);
  608. skb_reset_network_header(skb);
  609. skb_set_transport_header(skb, sizeof(*ipv6h));
  610. /*
  611. * Push down and install the IP header.
  612. */
  613. ipv6h = ipv6_hdr(skb);
  614. ip6_flow_hdr(ipv6h, INET_ECN_encapsulate(0, dsfield),
  615. ip6_make_flowlabel(net, skb, fl6->flowlabel, false));
  616. ipv6h->hop_limit = tunnel->parms.hop_limit;
  617. ipv6h->nexthdr = proto;
  618. ipv6h->saddr = fl6->saddr;
  619. ipv6h->daddr = fl6->daddr;
  620. ((__be16 *)(ipv6h + 1))[0] = tunnel->parms.o_flags;
  621. protocol = (dev->type == ARPHRD_ETHER) ?
  622. htons(ETH_P_TEB) : skb->protocol;
  623. ((__be16 *)(ipv6h + 1))[1] = protocol;
  624. if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) {
  625. __be32 *ptr = (__be32 *)(((u8 *)ipv6h) + tunnel->hlen - 4);
  626. if (tunnel->parms.o_flags&GRE_SEQ) {
  627. ++tunnel->o_seqno;
  628. *ptr = htonl(tunnel->o_seqno);
  629. ptr--;
  630. }
  631. if (tunnel->parms.o_flags&GRE_KEY) {
  632. *ptr = tunnel->parms.o_key;
  633. ptr--;
  634. }
  635. if (tunnel->parms.o_flags&GRE_CSUM) {
  636. *ptr = 0;
  637. *(__sum16 *)ptr = ip_compute_csum((void *)(ipv6h+1),
  638. skb->len - sizeof(struct ipv6hdr));
  639. }
  640. }
  641. skb_set_inner_protocol(skb, protocol);
  642. ip6tunnel_xmit(NULL, skb, dev);
  643. if (ndst)
  644. ip6_tnl_dst_store(tunnel, ndst);
  645. return 0;
  646. tx_err_link_failure:
  647. stats->tx_carrier_errors++;
  648. dst_link_failure(skb);
  649. tx_err_dst_release:
  650. dst_release(ndst);
  651. return err;
  652. }
  653. static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
  654. {
  655. struct ip6_tnl *t = netdev_priv(dev);
  656. const struct iphdr *iph = ip_hdr(skb);
  657. int encap_limit = -1;
  658. struct flowi6 fl6;
  659. __u8 dsfield;
  660. __u32 mtu;
  661. int err;
  662. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  663. encap_limit = t->parms.encap_limit;
  664. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  665. fl6.flowi6_proto = IPPROTO_GRE;
  666. dsfield = ipv4_get_dsfield(iph);
  667. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  668. fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
  669. & IPV6_TCLASS_MASK;
  670. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  671. fl6.flowi6_mark = skb->mark;
  672. err = ip6gre_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
  673. if (err != 0) {
  674. /* XXX: send ICMP error even if DF is not set. */
  675. if (err == -EMSGSIZE)
  676. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  677. htonl(mtu));
  678. return -1;
  679. }
  680. return 0;
  681. }
  682. static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
  683. {
  684. struct ip6_tnl *t = netdev_priv(dev);
  685. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  686. int encap_limit = -1;
  687. __u16 offset;
  688. struct flowi6 fl6;
  689. __u8 dsfield;
  690. __u32 mtu;
  691. int err;
  692. if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
  693. return -1;
  694. offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
  695. if (offset > 0) {
  696. struct ipv6_tlv_tnl_enc_lim *tel;
  697. tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
  698. if (tel->encap_limit == 0) {
  699. icmpv6_send(skb, ICMPV6_PARAMPROB,
  700. ICMPV6_HDR_FIELD, offset + 2);
  701. return -1;
  702. }
  703. encap_limit = tel->encap_limit - 1;
  704. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  705. encap_limit = t->parms.encap_limit;
  706. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  707. fl6.flowi6_proto = IPPROTO_GRE;
  708. dsfield = ipv6_get_dsfield(ipv6h);
  709. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  710. fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
  711. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
  712. fl6.flowlabel |= ip6_flowlabel(ipv6h);
  713. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  714. fl6.flowi6_mark = skb->mark;
  715. err = ip6gre_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
  716. if (err != 0) {
  717. if (err == -EMSGSIZE)
  718. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  719. return -1;
  720. }
  721. return 0;
  722. }
  723. /**
  724. * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  725. * @t: the outgoing tunnel device
  726. * @hdr: IPv6 header from the incoming packet
  727. *
  728. * Description:
  729. * Avoid trivial tunneling loop by checking that tunnel exit-point
  730. * doesn't match source of incoming packet.
  731. *
  732. * Return:
  733. * 1 if conflict,
  734. * 0 else
  735. **/
  736. static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
  737. const struct ipv6hdr *hdr)
  738. {
  739. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  740. }
  741. static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
  742. {
  743. struct ip6_tnl *t = netdev_priv(dev);
  744. int encap_limit = -1;
  745. struct flowi6 fl6;
  746. __u32 mtu;
  747. int err;
  748. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  749. encap_limit = t->parms.encap_limit;
  750. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  751. fl6.flowi6_proto = skb->protocol;
  752. err = ip6gre_xmit2(skb, dev, 0, &fl6, encap_limit, &mtu);
  753. return err;
  754. }
  755. static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
  756. struct net_device *dev)
  757. {
  758. struct ip6_tnl *t = netdev_priv(dev);
  759. struct net_device_stats *stats = &t->dev->stats;
  760. int ret;
  761. if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
  762. goto tx_err;
  763. switch (skb->protocol) {
  764. case htons(ETH_P_IP):
  765. ret = ip6gre_xmit_ipv4(skb, dev);
  766. break;
  767. case htons(ETH_P_IPV6):
  768. ret = ip6gre_xmit_ipv6(skb, dev);
  769. break;
  770. default:
  771. ret = ip6gre_xmit_other(skb, dev);
  772. break;
  773. }
  774. if (ret < 0)
  775. goto tx_err;
  776. return NETDEV_TX_OK;
  777. tx_err:
  778. stats->tx_errors++;
  779. stats->tx_dropped++;
  780. kfree_skb(skb);
  781. return NETDEV_TX_OK;
  782. }
  783. static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
  784. {
  785. struct net_device *dev = t->dev;
  786. struct __ip6_tnl_parm *p = &t->parms;
  787. struct flowi6 *fl6 = &t->fl.u.ip6;
  788. int addend = sizeof(struct ipv6hdr) + 4;
  789. if (dev->type != ARPHRD_ETHER) {
  790. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  791. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  792. }
  793. /* Set up flowi template */
  794. fl6->saddr = p->laddr;
  795. fl6->daddr = p->raddr;
  796. fl6->flowi6_oif = p->link;
  797. fl6->flowlabel = 0;
  798. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  799. fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  800. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  801. fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  802. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
  803. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  804. if (p->flags&IP6_TNL_F_CAP_XMIT &&
  805. p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
  806. dev->flags |= IFF_POINTOPOINT;
  807. else
  808. dev->flags &= ~IFF_POINTOPOINT;
  809. /* Precalculate GRE options length */
  810. if (t->parms.o_flags&(GRE_CSUM|GRE_KEY|GRE_SEQ)) {
  811. if (t->parms.o_flags&GRE_CSUM)
  812. addend += 4;
  813. if (t->parms.o_flags&GRE_KEY)
  814. addend += 4;
  815. if (t->parms.o_flags&GRE_SEQ)
  816. addend += 4;
  817. }
  818. t->hlen = addend;
  819. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  820. int strict = (ipv6_addr_type(&p->raddr) &
  821. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  822. struct rt6_info *rt = rt6_lookup(t->net,
  823. &p->raddr, &p->laddr,
  824. p->link, strict);
  825. if (!rt)
  826. return;
  827. if (rt->dst.dev) {
  828. dev->hard_header_len = rt->dst.dev->hard_header_len + addend;
  829. if (set_mtu) {
  830. dev->mtu = rt->dst.dev->mtu - addend;
  831. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  832. dev->mtu -= 8;
  833. if (dev->mtu < IPV6_MIN_MTU)
  834. dev->mtu = IPV6_MIN_MTU;
  835. }
  836. }
  837. ip6_rt_put(rt);
  838. }
  839. }
  840. static int ip6gre_tnl_change(struct ip6_tnl *t,
  841. const struct __ip6_tnl_parm *p, int set_mtu)
  842. {
  843. t->parms.laddr = p->laddr;
  844. t->parms.raddr = p->raddr;
  845. t->parms.flags = p->flags;
  846. t->parms.hop_limit = p->hop_limit;
  847. t->parms.encap_limit = p->encap_limit;
  848. t->parms.flowinfo = p->flowinfo;
  849. t->parms.link = p->link;
  850. t->parms.proto = p->proto;
  851. t->parms.i_key = p->i_key;
  852. t->parms.o_key = p->o_key;
  853. t->parms.i_flags = p->i_flags;
  854. t->parms.o_flags = p->o_flags;
  855. ip6_tnl_dst_reset(t);
  856. ip6gre_tnl_link_config(t, set_mtu);
  857. return 0;
  858. }
  859. static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
  860. const struct ip6_tnl_parm2 *u)
  861. {
  862. p->laddr = u->laddr;
  863. p->raddr = u->raddr;
  864. p->flags = u->flags;
  865. p->hop_limit = u->hop_limit;
  866. p->encap_limit = u->encap_limit;
  867. p->flowinfo = u->flowinfo;
  868. p->link = u->link;
  869. p->i_key = u->i_key;
  870. p->o_key = u->o_key;
  871. p->i_flags = u->i_flags;
  872. p->o_flags = u->o_flags;
  873. memcpy(p->name, u->name, sizeof(u->name));
  874. }
  875. static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
  876. const struct __ip6_tnl_parm *p)
  877. {
  878. u->proto = IPPROTO_GRE;
  879. u->laddr = p->laddr;
  880. u->raddr = p->raddr;
  881. u->flags = p->flags;
  882. u->hop_limit = p->hop_limit;
  883. u->encap_limit = p->encap_limit;
  884. u->flowinfo = p->flowinfo;
  885. u->link = p->link;
  886. u->i_key = p->i_key;
  887. u->o_key = p->o_key;
  888. u->i_flags = p->i_flags;
  889. u->o_flags = p->o_flags;
  890. memcpy(u->name, p->name, sizeof(u->name));
  891. }
  892. static int ip6gre_tunnel_ioctl(struct net_device *dev,
  893. struct ifreq *ifr, int cmd)
  894. {
  895. int err = 0;
  896. struct ip6_tnl_parm2 p;
  897. struct __ip6_tnl_parm p1;
  898. struct ip6_tnl *t = netdev_priv(dev);
  899. struct net *net = t->net;
  900. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  901. switch (cmd) {
  902. case SIOCGETTUNNEL:
  903. if (dev == ign->fb_tunnel_dev) {
  904. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  905. err = -EFAULT;
  906. break;
  907. }
  908. ip6gre_tnl_parm_from_user(&p1, &p);
  909. t = ip6gre_tunnel_locate(net, &p1, 0);
  910. if (!t)
  911. t = netdev_priv(dev);
  912. }
  913. memset(&p, 0, sizeof(p));
  914. ip6gre_tnl_parm_to_user(&p, &t->parms);
  915. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  916. err = -EFAULT;
  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.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
  928. goto done;
  929. if (!(p.i_flags&GRE_KEY))
  930. p.i_key = 0;
  931. if (!(p.o_flags&GRE_KEY))
  932. p.o_key = 0;
  933. ip6gre_tnl_parm_from_user(&p1, &p);
  934. t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
  935. if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  936. if (t) {
  937. if (t->dev != dev) {
  938. err = -EEXIST;
  939. break;
  940. }
  941. } else {
  942. t = netdev_priv(dev);
  943. ip6gre_tunnel_unlink(ign, t);
  944. synchronize_net();
  945. ip6gre_tnl_change(t, &p1, 1);
  946. ip6gre_tunnel_link(ign, t);
  947. netdev_state_change(dev);
  948. }
  949. }
  950. if (t) {
  951. err = 0;
  952. memset(&p, 0, sizeof(p));
  953. ip6gre_tnl_parm_to_user(&p, &t->parms);
  954. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  955. err = -EFAULT;
  956. } else
  957. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  958. break;
  959. case SIOCDELTUNNEL:
  960. err = -EPERM;
  961. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  962. goto done;
  963. if (dev == ign->fb_tunnel_dev) {
  964. err = -EFAULT;
  965. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  966. goto done;
  967. err = -ENOENT;
  968. ip6gre_tnl_parm_from_user(&p1, &p);
  969. t = ip6gre_tunnel_locate(net, &p1, 0);
  970. if (!t)
  971. goto done;
  972. err = -EPERM;
  973. if (t == netdev_priv(ign->fb_tunnel_dev))
  974. goto done;
  975. dev = t->dev;
  976. }
  977. unregister_netdevice(dev);
  978. err = 0;
  979. break;
  980. default:
  981. err = -EINVAL;
  982. }
  983. done:
  984. return err;
  985. }
  986. static int ip6gre_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  987. {
  988. if (new_mtu < 68 ||
  989. new_mtu > 0xFFF8 - dev->hard_header_len)
  990. return -EINVAL;
  991. dev->mtu = new_mtu;
  992. return 0;
  993. }
  994. static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
  995. unsigned short type,
  996. const void *daddr, const void *saddr, unsigned int len)
  997. {
  998. struct ip6_tnl *t = netdev_priv(dev);
  999. struct ipv6hdr *ipv6h = (struct ipv6hdr *)skb_push(skb, t->hlen);
  1000. __be16 *p = (__be16 *)(ipv6h+1);
  1001. ip6_flow_hdr(ipv6h, 0,
  1002. ip6_make_flowlabel(dev_net(dev), skb,
  1003. t->fl.u.ip6.flowlabel, false));
  1004. ipv6h->hop_limit = t->parms.hop_limit;
  1005. ipv6h->nexthdr = NEXTHDR_GRE;
  1006. ipv6h->saddr = t->parms.laddr;
  1007. ipv6h->daddr = t->parms.raddr;
  1008. p[0] = t->parms.o_flags;
  1009. p[1] = htons(type);
  1010. /*
  1011. * Set the source hardware address.
  1012. */
  1013. if (saddr)
  1014. memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
  1015. if (daddr)
  1016. memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
  1017. if (!ipv6_addr_any(&ipv6h->daddr))
  1018. return t->hlen;
  1019. return -t->hlen;
  1020. }
  1021. static const struct header_ops ip6gre_header_ops = {
  1022. .create = ip6gre_header,
  1023. };
  1024. static const struct net_device_ops ip6gre_netdev_ops = {
  1025. .ndo_init = ip6gre_tunnel_init,
  1026. .ndo_uninit = ip6gre_tunnel_uninit,
  1027. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1028. .ndo_do_ioctl = ip6gre_tunnel_ioctl,
  1029. .ndo_change_mtu = ip6gre_tunnel_change_mtu,
  1030. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1031. .ndo_get_iflink = ip6_tnl_get_iflink,
  1032. };
  1033. static void ip6gre_dev_free(struct net_device *dev)
  1034. {
  1035. free_percpu(dev->tstats);
  1036. free_netdev(dev);
  1037. }
  1038. static void ip6gre_tunnel_setup(struct net_device *dev)
  1039. {
  1040. struct ip6_tnl *t;
  1041. dev->netdev_ops = &ip6gre_netdev_ops;
  1042. dev->destructor = ip6gre_dev_free;
  1043. dev->type = ARPHRD_IP6GRE;
  1044. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr) + 4;
  1045. dev->mtu = ETH_DATA_LEN - sizeof(struct ipv6hdr) - 4;
  1046. t = netdev_priv(dev);
  1047. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1048. dev->mtu -= 8;
  1049. dev->flags |= IFF_NOARP;
  1050. dev->addr_len = sizeof(struct in6_addr);
  1051. netif_keep_dst(dev);
  1052. }
  1053. static int ip6gre_tunnel_init(struct net_device *dev)
  1054. {
  1055. struct ip6_tnl *tunnel;
  1056. tunnel = netdev_priv(dev);
  1057. tunnel->dev = dev;
  1058. tunnel->net = dev_net(dev);
  1059. strcpy(tunnel->parms.name, dev->name);
  1060. memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
  1061. memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
  1062. if (ipv6_addr_any(&tunnel->parms.raddr))
  1063. dev->header_ops = &ip6gre_header_ops;
  1064. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1065. if (!dev->tstats)
  1066. return -ENOMEM;
  1067. return 0;
  1068. }
  1069. static void ip6gre_fb_tunnel_init(struct net_device *dev)
  1070. {
  1071. struct ip6_tnl *tunnel = netdev_priv(dev);
  1072. tunnel->dev = dev;
  1073. tunnel->net = dev_net(dev);
  1074. strcpy(tunnel->parms.name, dev->name);
  1075. tunnel->hlen = sizeof(struct ipv6hdr) + 4;
  1076. dev_hold(dev);
  1077. }
  1078. static struct inet6_protocol ip6gre_protocol __read_mostly = {
  1079. .handler = ip6gre_rcv,
  1080. .err_handler = ip6gre_err,
  1081. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  1082. };
  1083. static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)
  1084. {
  1085. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1086. struct net_device *dev, *aux;
  1087. int prio;
  1088. for_each_netdev_safe(net, dev, aux)
  1089. if (dev->rtnl_link_ops == &ip6gre_link_ops ||
  1090. dev->rtnl_link_ops == &ip6gre_tap_ops)
  1091. unregister_netdevice_queue(dev, head);
  1092. for (prio = 0; prio < 4; prio++) {
  1093. int h;
  1094. for (h = 0; h < HASH_SIZE; h++) {
  1095. struct ip6_tnl *t;
  1096. t = rtnl_dereference(ign->tunnels[prio][h]);
  1097. while (t) {
  1098. /* If dev is in the same netns, it has already
  1099. * been added to the list by the previous loop.
  1100. */
  1101. if (!net_eq(dev_net(t->dev), net))
  1102. unregister_netdevice_queue(t->dev,
  1103. head);
  1104. t = rtnl_dereference(t->next);
  1105. }
  1106. }
  1107. }
  1108. }
  1109. static int __net_init ip6gre_init_net(struct net *net)
  1110. {
  1111. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1112. int err;
  1113. ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
  1114. NET_NAME_UNKNOWN,
  1115. ip6gre_tunnel_setup);
  1116. if (!ign->fb_tunnel_dev) {
  1117. err = -ENOMEM;
  1118. goto err_alloc_dev;
  1119. }
  1120. dev_net_set(ign->fb_tunnel_dev, net);
  1121. /* FB netdevice is special: we have one, and only one per netns.
  1122. * Allowing to move it to another netns is clearly unsafe.
  1123. */
  1124. ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
  1125. ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
  1126. ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
  1127. err = register_netdev(ign->fb_tunnel_dev);
  1128. if (err)
  1129. goto err_reg_dev;
  1130. rcu_assign_pointer(ign->tunnels_wc[0],
  1131. netdev_priv(ign->fb_tunnel_dev));
  1132. return 0;
  1133. err_reg_dev:
  1134. ip6gre_dev_free(ign->fb_tunnel_dev);
  1135. err_alloc_dev:
  1136. return err;
  1137. }
  1138. static void __net_exit ip6gre_exit_net(struct net *net)
  1139. {
  1140. LIST_HEAD(list);
  1141. rtnl_lock();
  1142. ip6gre_destroy_tunnels(net, &list);
  1143. unregister_netdevice_many(&list);
  1144. rtnl_unlock();
  1145. }
  1146. static struct pernet_operations ip6gre_net_ops = {
  1147. .init = ip6gre_init_net,
  1148. .exit = ip6gre_exit_net,
  1149. .id = &ip6gre_net_id,
  1150. .size = sizeof(struct ip6gre_net),
  1151. };
  1152. static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
  1153. {
  1154. __be16 flags;
  1155. if (!data)
  1156. return 0;
  1157. flags = 0;
  1158. if (data[IFLA_GRE_IFLAGS])
  1159. flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
  1160. if (data[IFLA_GRE_OFLAGS])
  1161. flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
  1162. if (flags & (GRE_VERSION|GRE_ROUTING))
  1163. return -EINVAL;
  1164. return 0;
  1165. }
  1166. static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[])
  1167. {
  1168. struct in6_addr daddr;
  1169. if (tb[IFLA_ADDRESS]) {
  1170. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  1171. return -EINVAL;
  1172. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  1173. return -EADDRNOTAVAIL;
  1174. }
  1175. if (!data)
  1176. goto out;
  1177. if (data[IFLA_GRE_REMOTE]) {
  1178. daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
  1179. if (ipv6_addr_any(&daddr))
  1180. return -EINVAL;
  1181. }
  1182. out:
  1183. return ip6gre_tunnel_validate(tb, data);
  1184. }
  1185. static void ip6gre_netlink_parms(struct nlattr *data[],
  1186. struct __ip6_tnl_parm *parms)
  1187. {
  1188. memset(parms, 0, sizeof(*parms));
  1189. if (!data)
  1190. return;
  1191. if (data[IFLA_GRE_LINK])
  1192. parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
  1193. if (data[IFLA_GRE_IFLAGS])
  1194. parms->i_flags = nla_get_be16(data[IFLA_GRE_IFLAGS]);
  1195. if (data[IFLA_GRE_OFLAGS])
  1196. parms->o_flags = nla_get_be16(data[IFLA_GRE_OFLAGS]);
  1197. if (data[IFLA_GRE_IKEY])
  1198. parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
  1199. if (data[IFLA_GRE_OKEY])
  1200. parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
  1201. if (data[IFLA_GRE_LOCAL])
  1202. parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
  1203. if (data[IFLA_GRE_REMOTE])
  1204. parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
  1205. if (data[IFLA_GRE_TTL])
  1206. parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
  1207. if (data[IFLA_GRE_ENCAP_LIMIT])
  1208. parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
  1209. if (data[IFLA_GRE_FLOWINFO])
  1210. parms->flowinfo = nla_get_u32(data[IFLA_GRE_FLOWINFO]);
  1211. if (data[IFLA_GRE_FLAGS])
  1212. parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
  1213. }
  1214. static int ip6gre_tap_init(struct net_device *dev)
  1215. {
  1216. struct ip6_tnl *tunnel;
  1217. tunnel = netdev_priv(dev);
  1218. tunnel->dev = dev;
  1219. tunnel->net = dev_net(dev);
  1220. strcpy(tunnel->parms.name, dev->name);
  1221. ip6gre_tnl_link_config(tunnel, 1);
  1222. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1223. if (!dev->tstats)
  1224. return -ENOMEM;
  1225. return 0;
  1226. }
  1227. static const struct net_device_ops ip6gre_tap_netdev_ops = {
  1228. .ndo_init = ip6gre_tap_init,
  1229. .ndo_uninit = ip6gre_tunnel_uninit,
  1230. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1231. .ndo_set_mac_address = eth_mac_addr,
  1232. .ndo_validate_addr = eth_validate_addr,
  1233. .ndo_change_mtu = ip6gre_tunnel_change_mtu,
  1234. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1235. .ndo_get_iflink = ip6_tnl_get_iflink,
  1236. };
  1237. static void ip6gre_tap_setup(struct net_device *dev)
  1238. {
  1239. ether_setup(dev);
  1240. dev->netdev_ops = &ip6gre_tap_netdev_ops;
  1241. dev->destructor = ip6gre_dev_free;
  1242. dev->features |= NETIF_F_NETNS_LOCAL;
  1243. }
  1244. static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
  1245. struct nlattr *tb[], struct nlattr *data[])
  1246. {
  1247. struct ip6_tnl *nt;
  1248. struct net *net = dev_net(dev);
  1249. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1250. int err;
  1251. nt = netdev_priv(dev);
  1252. ip6gre_netlink_parms(data, &nt->parms);
  1253. if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
  1254. return -EEXIST;
  1255. if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
  1256. eth_hw_addr_random(dev);
  1257. nt->dev = dev;
  1258. nt->net = dev_net(dev);
  1259. ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
  1260. /* Can use a lockless transmit, unless we generate output sequences */
  1261. if (!(nt->parms.o_flags & GRE_SEQ))
  1262. dev->features |= NETIF_F_LLTX;
  1263. err = register_netdevice(dev);
  1264. if (err)
  1265. goto out;
  1266. dev_hold(dev);
  1267. ip6gre_tunnel_link(ign, nt);
  1268. out:
  1269. return err;
  1270. }
  1271. static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
  1272. struct nlattr *data[])
  1273. {
  1274. struct ip6_tnl *t, *nt = netdev_priv(dev);
  1275. struct net *net = nt->net;
  1276. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1277. struct __ip6_tnl_parm p;
  1278. if (dev == ign->fb_tunnel_dev)
  1279. return -EINVAL;
  1280. ip6gre_netlink_parms(data, &p);
  1281. t = ip6gre_tunnel_locate(net, &p, 0);
  1282. if (t) {
  1283. if (t->dev != dev)
  1284. return -EEXIST;
  1285. } else {
  1286. t = nt;
  1287. ip6gre_tunnel_unlink(ign, t);
  1288. ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
  1289. ip6gre_tunnel_link(ign, t);
  1290. netdev_state_change(dev);
  1291. }
  1292. return 0;
  1293. }
  1294. static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
  1295. {
  1296. struct net *net = dev_net(dev);
  1297. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1298. if (dev != ign->fb_tunnel_dev)
  1299. unregister_netdevice_queue(dev, head);
  1300. }
  1301. static size_t ip6gre_get_size(const struct net_device *dev)
  1302. {
  1303. return
  1304. /* IFLA_GRE_LINK */
  1305. nla_total_size(4) +
  1306. /* IFLA_GRE_IFLAGS */
  1307. nla_total_size(2) +
  1308. /* IFLA_GRE_OFLAGS */
  1309. nla_total_size(2) +
  1310. /* IFLA_GRE_IKEY */
  1311. nla_total_size(4) +
  1312. /* IFLA_GRE_OKEY */
  1313. nla_total_size(4) +
  1314. /* IFLA_GRE_LOCAL */
  1315. nla_total_size(sizeof(struct in6_addr)) +
  1316. /* IFLA_GRE_REMOTE */
  1317. nla_total_size(sizeof(struct in6_addr)) +
  1318. /* IFLA_GRE_TTL */
  1319. nla_total_size(1) +
  1320. /* IFLA_GRE_TOS */
  1321. nla_total_size(1) +
  1322. /* IFLA_GRE_ENCAP_LIMIT */
  1323. nla_total_size(1) +
  1324. /* IFLA_GRE_FLOWINFO */
  1325. nla_total_size(4) +
  1326. /* IFLA_GRE_FLAGS */
  1327. nla_total_size(4) +
  1328. 0;
  1329. }
  1330. static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1331. {
  1332. struct ip6_tnl *t = netdev_priv(dev);
  1333. struct __ip6_tnl_parm *p = &t->parms;
  1334. if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
  1335. nla_put_be16(skb, IFLA_GRE_IFLAGS, p->i_flags) ||
  1336. nla_put_be16(skb, IFLA_GRE_OFLAGS, p->o_flags) ||
  1337. nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
  1338. nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
  1339. nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
  1340. nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
  1341. nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
  1342. /*nla_put_u8(skb, IFLA_GRE_TOS, t->priority) ||*/
  1343. nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
  1344. nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
  1345. nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags))
  1346. goto nla_put_failure;
  1347. return 0;
  1348. nla_put_failure:
  1349. return -EMSGSIZE;
  1350. }
  1351. static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
  1352. [IFLA_GRE_LINK] = { .type = NLA_U32 },
  1353. [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
  1354. [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
  1355. [IFLA_GRE_IKEY] = { .type = NLA_U32 },
  1356. [IFLA_GRE_OKEY] = { .type = NLA_U32 },
  1357. [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
  1358. [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
  1359. [IFLA_GRE_TTL] = { .type = NLA_U8 },
  1360. [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
  1361. [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 },
  1362. [IFLA_GRE_FLAGS] = { .type = NLA_U32 },
  1363. };
  1364. static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
  1365. .kind = "ip6gre",
  1366. .maxtype = IFLA_GRE_MAX,
  1367. .policy = ip6gre_policy,
  1368. .priv_size = sizeof(struct ip6_tnl),
  1369. .setup = ip6gre_tunnel_setup,
  1370. .validate = ip6gre_tunnel_validate,
  1371. .newlink = ip6gre_newlink,
  1372. .changelink = ip6gre_changelink,
  1373. .dellink = ip6gre_dellink,
  1374. .get_size = ip6gre_get_size,
  1375. .fill_info = ip6gre_fill_info,
  1376. .get_link_net = ip6_tnl_get_link_net,
  1377. };
  1378. static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
  1379. .kind = "ip6gretap",
  1380. .maxtype = IFLA_GRE_MAX,
  1381. .policy = ip6gre_policy,
  1382. .priv_size = sizeof(struct ip6_tnl),
  1383. .setup = ip6gre_tap_setup,
  1384. .validate = ip6gre_tap_validate,
  1385. .newlink = ip6gre_newlink,
  1386. .changelink = ip6gre_changelink,
  1387. .get_size = ip6gre_get_size,
  1388. .fill_info = ip6gre_fill_info,
  1389. .get_link_net = ip6_tnl_get_link_net,
  1390. };
  1391. /*
  1392. * And now the modules code and kernel interface.
  1393. */
  1394. static int __init ip6gre_init(void)
  1395. {
  1396. int err;
  1397. pr_info("GRE over IPv6 tunneling driver\n");
  1398. err = register_pernet_device(&ip6gre_net_ops);
  1399. if (err < 0)
  1400. return err;
  1401. err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1402. if (err < 0) {
  1403. pr_info("%s: can't add protocol\n", __func__);
  1404. goto add_proto_failed;
  1405. }
  1406. err = rtnl_link_register(&ip6gre_link_ops);
  1407. if (err < 0)
  1408. goto rtnl_link_failed;
  1409. err = rtnl_link_register(&ip6gre_tap_ops);
  1410. if (err < 0)
  1411. goto tap_ops_failed;
  1412. out:
  1413. return err;
  1414. tap_ops_failed:
  1415. rtnl_link_unregister(&ip6gre_link_ops);
  1416. rtnl_link_failed:
  1417. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1418. add_proto_failed:
  1419. unregister_pernet_device(&ip6gre_net_ops);
  1420. goto out;
  1421. }
  1422. static void __exit ip6gre_fini(void)
  1423. {
  1424. rtnl_link_unregister(&ip6gre_tap_ops);
  1425. rtnl_link_unregister(&ip6gre_link_ops);
  1426. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1427. unregister_pernet_device(&ip6gre_net_ops);
  1428. }
  1429. module_init(ip6gre_init);
  1430. module_exit(ip6gre_fini);
  1431. MODULE_LICENSE("GPL");
  1432. MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
  1433. MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
  1434. MODULE_ALIAS_RTNL_LINK("ip6gre");
  1435. MODULE_ALIAS_RTNL_LINK("ip6gretap");
  1436. MODULE_ALIAS_NETDEV("ip6gre0");