ip6_gre.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742
  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/init.h>
  26. #include <linux/in6.h>
  27. #include <linux/inetdevice.h>
  28. #include <linux/igmp.h>
  29. #include <linux/netfilter_ipv4.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/if_ether.h>
  32. #include <linux/hash.h>
  33. #include <linux/if_tunnel.h>
  34. #include <linux/ip6_tunnel.h>
  35. #include <net/sock.h>
  36. #include <net/ip.h>
  37. #include <net/ip_tunnels.h>
  38. #include <net/icmp.h>
  39. #include <net/protocol.h>
  40. #include <net/addrconf.h>
  41. #include <net/arp.h>
  42. #include <net/checksum.h>
  43. #include <net/dsfield.h>
  44. #include <net/inet_ecn.h>
  45. #include <net/xfrm.h>
  46. #include <net/net_namespace.h>
  47. #include <net/netns/generic.h>
  48. #include <net/rtnetlink.h>
  49. #include <net/ipv6.h>
  50. #include <net/ip6_fib.h>
  51. #include <net/ip6_route.h>
  52. #include <net/ip6_tunnel.h>
  53. static bool log_ecn_error = true;
  54. module_param(log_ecn_error, bool, 0644);
  55. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  56. #define HASH_SIZE_SHIFT 5
  57. #define HASH_SIZE (1 << HASH_SIZE_SHIFT)
  58. static int ip6gre_net_id __read_mostly;
  59. struct ip6gre_net {
  60. struct ip6_tnl __rcu *tunnels[4][HASH_SIZE];
  61. struct net_device *fb_tunnel_dev;
  62. };
  63. static struct rtnl_link_ops ip6gre_link_ops __read_mostly;
  64. static struct rtnl_link_ops ip6gre_tap_ops __read_mostly;
  65. static int ip6gre_tunnel_init(struct net_device *dev);
  66. static void ip6gre_tunnel_setup(struct net_device *dev);
  67. static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t);
  68. static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu);
  69. /* Tunnel hash table */
  70. /*
  71. 4 hash tables:
  72. 3: (remote,local)
  73. 2: (remote,*)
  74. 1: (*,local)
  75. 0: (*,*)
  76. We require exact key match i.e. if a key is present in packet
  77. it will match only tunnel with the same key; if it is not present,
  78. it will match only keyless tunnel.
  79. All keysless packets, if not matched configured keyless tunnels
  80. will match fallback tunnel.
  81. */
  82. #define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(HASH_SIZE - 1))
  83. static u32 HASH_ADDR(const struct in6_addr *addr)
  84. {
  85. u32 hash = ipv6_addr_hash(addr);
  86. return hash_32(hash, HASH_SIZE_SHIFT);
  87. }
  88. #define tunnels_r_l tunnels[3]
  89. #define tunnels_r tunnels[2]
  90. #define tunnels_l tunnels[1]
  91. #define tunnels_wc tunnels[0]
  92. /* Given src, dst and key, find appropriate for input tunnel. */
  93. static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev,
  94. const struct in6_addr *remote, const struct in6_addr *local,
  95. __be32 key, __be16 gre_proto)
  96. {
  97. struct net *net = dev_net(dev);
  98. int link = dev->ifindex;
  99. unsigned int h0 = HASH_ADDR(remote);
  100. unsigned int h1 = HASH_KEY(key);
  101. struct ip6_tnl *t, *cand = NULL;
  102. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  103. int dev_type = (gre_proto == htons(ETH_P_TEB)) ?
  104. ARPHRD_ETHER : ARPHRD_IP6GRE;
  105. int score, cand_score = 4;
  106. for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) {
  107. if (!ipv6_addr_equal(local, &t->parms.laddr) ||
  108. !ipv6_addr_equal(remote, &t->parms.raddr) ||
  109. key != t->parms.i_key ||
  110. !(t->dev->flags & IFF_UP))
  111. continue;
  112. if (t->dev->type != ARPHRD_IP6GRE &&
  113. t->dev->type != dev_type)
  114. continue;
  115. score = 0;
  116. if (t->parms.link != link)
  117. score |= 1;
  118. if (t->dev->type != dev_type)
  119. score |= 2;
  120. if (score == 0)
  121. return t;
  122. if (score < cand_score) {
  123. cand = t;
  124. cand_score = score;
  125. }
  126. }
  127. for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) {
  128. if (!ipv6_addr_equal(remote, &t->parms.raddr) ||
  129. key != t->parms.i_key ||
  130. !(t->dev->flags & IFF_UP))
  131. continue;
  132. if (t->dev->type != ARPHRD_IP6GRE &&
  133. t->dev->type != dev_type)
  134. continue;
  135. score = 0;
  136. if (t->parms.link != link)
  137. score |= 1;
  138. if (t->dev->type != dev_type)
  139. score |= 2;
  140. if (score == 0)
  141. return t;
  142. if (score < cand_score) {
  143. cand = t;
  144. cand_score = score;
  145. }
  146. }
  147. for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) {
  148. if ((!ipv6_addr_equal(local, &t->parms.laddr) &&
  149. (!ipv6_addr_equal(local, &t->parms.raddr) ||
  150. !ipv6_addr_is_multicast(local))) ||
  151. key != t->parms.i_key ||
  152. !(t->dev->flags & IFF_UP))
  153. continue;
  154. if (t->dev->type != ARPHRD_IP6GRE &&
  155. t->dev->type != dev_type)
  156. continue;
  157. score = 0;
  158. if (t->parms.link != link)
  159. score |= 1;
  160. if (t->dev->type != dev_type)
  161. score |= 2;
  162. if (score == 0)
  163. return t;
  164. if (score < cand_score) {
  165. cand = t;
  166. cand_score = score;
  167. }
  168. }
  169. for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) {
  170. if (t->parms.i_key != key ||
  171. !(t->dev->flags & IFF_UP))
  172. continue;
  173. if (t->dev->type != ARPHRD_IP6GRE &&
  174. t->dev->type != dev_type)
  175. continue;
  176. score = 0;
  177. if (t->parms.link != link)
  178. score |= 1;
  179. if (t->dev->type != dev_type)
  180. score |= 2;
  181. if (score == 0)
  182. return t;
  183. if (score < cand_score) {
  184. cand = t;
  185. cand_score = score;
  186. }
  187. }
  188. if (cand)
  189. return cand;
  190. dev = ign->fb_tunnel_dev;
  191. if (dev->flags & IFF_UP)
  192. return netdev_priv(dev);
  193. return NULL;
  194. }
  195. static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign,
  196. const struct __ip6_tnl_parm *p)
  197. {
  198. const struct in6_addr *remote = &p->raddr;
  199. const struct in6_addr *local = &p->laddr;
  200. unsigned int h = HASH_KEY(p->i_key);
  201. int prio = 0;
  202. if (!ipv6_addr_any(local))
  203. prio |= 1;
  204. if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) {
  205. prio |= 2;
  206. h ^= HASH_ADDR(remote);
  207. }
  208. return &ign->tunnels[prio][h];
  209. }
  210. static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign,
  211. const struct ip6_tnl *t)
  212. {
  213. return __ip6gre_bucket(ign, &t->parms);
  214. }
  215. static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t)
  216. {
  217. struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t);
  218. rcu_assign_pointer(t->next, rtnl_dereference(*tp));
  219. rcu_assign_pointer(*tp, t);
  220. }
  221. static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t)
  222. {
  223. struct ip6_tnl __rcu **tp;
  224. struct ip6_tnl *iter;
  225. for (tp = ip6gre_bucket(ign, t);
  226. (iter = rtnl_dereference(*tp)) != NULL;
  227. tp = &iter->next) {
  228. if (t == iter) {
  229. rcu_assign_pointer(*tp, t->next);
  230. break;
  231. }
  232. }
  233. }
  234. static struct ip6_tnl *ip6gre_tunnel_find(struct net *net,
  235. const struct __ip6_tnl_parm *parms,
  236. int type)
  237. {
  238. const struct in6_addr *remote = &parms->raddr;
  239. const struct in6_addr *local = &parms->laddr;
  240. __be32 key = parms->i_key;
  241. int link = parms->link;
  242. struct ip6_tnl *t;
  243. struct ip6_tnl __rcu **tp;
  244. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  245. for (tp = __ip6gre_bucket(ign, parms);
  246. (t = rtnl_dereference(*tp)) != NULL;
  247. tp = &t->next)
  248. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  249. ipv6_addr_equal(remote, &t->parms.raddr) &&
  250. key == t->parms.i_key &&
  251. link == t->parms.link &&
  252. type == t->dev->type)
  253. break;
  254. return t;
  255. }
  256. static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net,
  257. const struct __ip6_tnl_parm *parms, int create)
  258. {
  259. struct ip6_tnl *t, *nt;
  260. struct net_device *dev;
  261. char name[IFNAMSIZ];
  262. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  263. t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE);
  264. if (t && create)
  265. return NULL;
  266. if (t || !create)
  267. return t;
  268. if (parms->name[0])
  269. strlcpy(name, parms->name, IFNAMSIZ);
  270. else
  271. strcpy(name, "ip6gre%d");
  272. dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
  273. ip6gre_tunnel_setup);
  274. if (!dev)
  275. return NULL;
  276. dev_net_set(dev, net);
  277. nt = netdev_priv(dev);
  278. nt->parms = *parms;
  279. dev->rtnl_link_ops = &ip6gre_link_ops;
  280. nt->dev = dev;
  281. nt->net = dev_net(dev);
  282. ip6gre_tnl_link_config(nt, 1);
  283. if (register_netdevice(dev) < 0)
  284. goto failed_free;
  285. /* Can use a lockless transmit, unless we generate output sequences */
  286. if (!(nt->parms.o_flags & GRE_SEQ))
  287. dev->features |= NETIF_F_LLTX;
  288. dev_hold(dev);
  289. ip6gre_tunnel_link(ign, nt);
  290. return nt;
  291. failed_free:
  292. free_netdev(dev);
  293. return NULL;
  294. }
  295. static void ip6gre_tunnel_uninit(struct net_device *dev)
  296. {
  297. struct ip6_tnl *t = netdev_priv(dev);
  298. struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
  299. ip6gre_tunnel_unlink(ign, t);
  300. ip6_tnl_dst_reset(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_dbg_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_dbg_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_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
  354. t->parms.name);
  355. }
  356. } else {
  357. net_dbg_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_get(tunnel);
  537. if (!dst) {
  538. dst = ip6_route_output(net, NULL, fl6);
  539. if (dst->error)
  540. goto tx_err_link_failure;
  541. dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), NULL, 0);
  542. if (IS_ERR(dst)) {
  543. err = PTR_ERR(dst);
  544. dst = NULL;
  545. goto tx_err_link_failure;
  546. }
  547. ndst = dst;
  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 && ndst)
  593. ip6_tnl_dst_set(tunnel, ndst);
  594. skb_dst_set(skb, dst);
  595. proto = NEXTHDR_GRE;
  596. if (encap_limit >= 0) {
  597. init_tel_txopt(&opt, encap_limit);
  598. ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
  599. }
  600. if (likely(!skb->encapsulation)) {
  601. skb_reset_inner_headers(skb);
  602. skb->encapsulation = 1;
  603. }
  604. skb_push(skb, gre_hlen);
  605. skb_reset_network_header(skb);
  606. skb_set_transport_header(skb, sizeof(*ipv6h));
  607. /*
  608. * Push down and install the IP header.
  609. */
  610. ipv6h = ipv6_hdr(skb);
  611. ip6_flow_hdr(ipv6h, INET_ECN_encapsulate(0, dsfield),
  612. ip6_make_flowlabel(net, skb, fl6->flowlabel, true, fl6));
  613. ipv6h->hop_limit = tunnel->parms.hop_limit;
  614. ipv6h->nexthdr = proto;
  615. ipv6h->saddr = fl6->saddr;
  616. ipv6h->daddr = fl6->daddr;
  617. ((__be16 *)(ipv6h + 1))[0] = tunnel->parms.o_flags;
  618. protocol = (dev->type == ARPHRD_ETHER) ?
  619. htons(ETH_P_TEB) : skb->protocol;
  620. ((__be16 *)(ipv6h + 1))[1] = protocol;
  621. if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) {
  622. __be32 *ptr = (__be32 *)(((u8 *)ipv6h) + tunnel->hlen - 4);
  623. if (tunnel->parms.o_flags&GRE_SEQ) {
  624. ++tunnel->o_seqno;
  625. *ptr = htonl(tunnel->o_seqno);
  626. ptr--;
  627. }
  628. if (tunnel->parms.o_flags&GRE_KEY) {
  629. *ptr = tunnel->parms.o_key;
  630. ptr--;
  631. }
  632. if (tunnel->parms.o_flags&GRE_CSUM) {
  633. *ptr = 0;
  634. *(__sum16 *)ptr = ip_compute_csum((void *)(ipv6h+1),
  635. skb->len - sizeof(struct ipv6hdr));
  636. }
  637. }
  638. skb_set_inner_protocol(skb, protocol);
  639. ip6tunnel_xmit(NULL, skb, dev);
  640. return 0;
  641. tx_err_link_failure:
  642. stats->tx_carrier_errors++;
  643. dst_link_failure(skb);
  644. tx_err_dst_release:
  645. dst_release(dst);
  646. return err;
  647. }
  648. static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
  649. {
  650. struct ip6_tnl *t = netdev_priv(dev);
  651. const struct iphdr *iph = ip_hdr(skb);
  652. int encap_limit = -1;
  653. struct flowi6 fl6;
  654. __u8 dsfield;
  655. __u32 mtu;
  656. int err;
  657. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  658. encap_limit = t->parms.encap_limit;
  659. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  660. fl6.flowi6_proto = IPPROTO_GRE;
  661. dsfield = ipv4_get_dsfield(iph);
  662. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  663. fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
  664. & IPV6_TCLASS_MASK;
  665. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  666. fl6.flowi6_mark = skb->mark;
  667. err = ip6gre_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
  668. if (err != 0) {
  669. /* XXX: send ICMP error even if DF is not set. */
  670. if (err == -EMSGSIZE)
  671. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  672. htonl(mtu));
  673. return -1;
  674. }
  675. return 0;
  676. }
  677. static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
  678. {
  679. struct ip6_tnl *t = netdev_priv(dev);
  680. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  681. int encap_limit = -1;
  682. __u16 offset;
  683. struct flowi6 fl6;
  684. __u8 dsfield;
  685. __u32 mtu;
  686. int err;
  687. if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
  688. return -1;
  689. offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
  690. if (offset > 0) {
  691. struct ipv6_tlv_tnl_enc_lim *tel;
  692. tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
  693. if (tel->encap_limit == 0) {
  694. icmpv6_send(skb, ICMPV6_PARAMPROB,
  695. ICMPV6_HDR_FIELD, offset + 2);
  696. return -1;
  697. }
  698. encap_limit = tel->encap_limit - 1;
  699. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  700. encap_limit = t->parms.encap_limit;
  701. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  702. fl6.flowi6_proto = IPPROTO_GRE;
  703. dsfield = ipv6_get_dsfield(ipv6h);
  704. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  705. fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
  706. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
  707. fl6.flowlabel |= ip6_flowlabel(ipv6h);
  708. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  709. fl6.flowi6_mark = skb->mark;
  710. err = ip6gre_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
  711. if (err != 0) {
  712. if (err == -EMSGSIZE)
  713. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  714. return -1;
  715. }
  716. return 0;
  717. }
  718. /**
  719. * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  720. * @t: the outgoing tunnel device
  721. * @hdr: IPv6 header from the incoming packet
  722. *
  723. * Description:
  724. * Avoid trivial tunneling loop by checking that tunnel exit-point
  725. * doesn't match source of incoming packet.
  726. *
  727. * Return:
  728. * 1 if conflict,
  729. * 0 else
  730. **/
  731. static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
  732. const struct ipv6hdr *hdr)
  733. {
  734. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  735. }
  736. static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
  737. {
  738. struct ip6_tnl *t = netdev_priv(dev);
  739. int encap_limit = -1;
  740. struct flowi6 fl6;
  741. __u32 mtu;
  742. int err;
  743. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  744. encap_limit = t->parms.encap_limit;
  745. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  746. fl6.flowi6_proto = skb->protocol;
  747. err = ip6gre_xmit2(skb, dev, 0, &fl6, encap_limit, &mtu);
  748. return err;
  749. }
  750. static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
  751. struct net_device *dev)
  752. {
  753. struct ip6_tnl *t = netdev_priv(dev);
  754. struct net_device_stats *stats = &t->dev->stats;
  755. int ret;
  756. if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
  757. goto tx_err;
  758. switch (skb->protocol) {
  759. case htons(ETH_P_IP):
  760. ret = ip6gre_xmit_ipv4(skb, dev);
  761. break;
  762. case htons(ETH_P_IPV6):
  763. ret = ip6gre_xmit_ipv6(skb, dev);
  764. break;
  765. default:
  766. ret = ip6gre_xmit_other(skb, dev);
  767. break;
  768. }
  769. if (ret < 0)
  770. goto tx_err;
  771. return NETDEV_TX_OK;
  772. tx_err:
  773. stats->tx_errors++;
  774. stats->tx_dropped++;
  775. kfree_skb(skb);
  776. return NETDEV_TX_OK;
  777. }
  778. static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
  779. {
  780. struct net_device *dev = t->dev;
  781. struct __ip6_tnl_parm *p = &t->parms;
  782. struct flowi6 *fl6 = &t->fl.u.ip6;
  783. int addend = sizeof(struct ipv6hdr) + 4;
  784. if (dev->type != ARPHRD_ETHER) {
  785. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  786. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  787. }
  788. /* Set up flowi template */
  789. fl6->saddr = p->laddr;
  790. fl6->daddr = p->raddr;
  791. fl6->flowi6_oif = p->link;
  792. fl6->flowlabel = 0;
  793. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  794. fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  795. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  796. fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  797. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
  798. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  799. if (p->flags&IP6_TNL_F_CAP_XMIT &&
  800. p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
  801. dev->flags |= IFF_POINTOPOINT;
  802. else
  803. dev->flags &= ~IFF_POINTOPOINT;
  804. /* Precalculate GRE options length */
  805. if (t->parms.o_flags&(GRE_CSUM|GRE_KEY|GRE_SEQ)) {
  806. if (t->parms.o_flags&GRE_CSUM)
  807. addend += 4;
  808. if (t->parms.o_flags&GRE_KEY)
  809. addend += 4;
  810. if (t->parms.o_flags&GRE_SEQ)
  811. addend += 4;
  812. }
  813. t->hlen = addend;
  814. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  815. int strict = (ipv6_addr_type(&p->raddr) &
  816. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  817. struct rt6_info *rt = rt6_lookup(t->net,
  818. &p->raddr, &p->laddr,
  819. p->link, strict);
  820. if (!rt)
  821. return;
  822. if (rt->dst.dev) {
  823. dev->hard_header_len = rt->dst.dev->hard_header_len + addend;
  824. if (set_mtu) {
  825. dev->mtu = rt->dst.dev->mtu - addend;
  826. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  827. dev->mtu -= 8;
  828. if (dev->mtu < IPV6_MIN_MTU)
  829. dev->mtu = IPV6_MIN_MTU;
  830. }
  831. }
  832. ip6_rt_put(rt);
  833. }
  834. }
  835. static int ip6gre_tnl_change(struct ip6_tnl *t,
  836. const struct __ip6_tnl_parm *p, int set_mtu)
  837. {
  838. t->parms.laddr = p->laddr;
  839. t->parms.raddr = p->raddr;
  840. t->parms.flags = p->flags;
  841. t->parms.hop_limit = p->hop_limit;
  842. t->parms.encap_limit = p->encap_limit;
  843. t->parms.flowinfo = p->flowinfo;
  844. t->parms.link = p->link;
  845. t->parms.proto = p->proto;
  846. t->parms.i_key = p->i_key;
  847. t->parms.o_key = p->o_key;
  848. t->parms.i_flags = p->i_flags;
  849. t->parms.o_flags = p->o_flags;
  850. ip6_tnl_dst_reset(t);
  851. ip6gre_tnl_link_config(t, set_mtu);
  852. return 0;
  853. }
  854. static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
  855. const struct ip6_tnl_parm2 *u)
  856. {
  857. p->laddr = u->laddr;
  858. p->raddr = u->raddr;
  859. p->flags = u->flags;
  860. p->hop_limit = u->hop_limit;
  861. p->encap_limit = u->encap_limit;
  862. p->flowinfo = u->flowinfo;
  863. p->link = u->link;
  864. p->i_key = u->i_key;
  865. p->o_key = u->o_key;
  866. p->i_flags = u->i_flags;
  867. p->o_flags = u->o_flags;
  868. memcpy(p->name, u->name, sizeof(u->name));
  869. }
  870. static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
  871. const struct __ip6_tnl_parm *p)
  872. {
  873. u->proto = IPPROTO_GRE;
  874. u->laddr = p->laddr;
  875. u->raddr = p->raddr;
  876. u->flags = p->flags;
  877. u->hop_limit = p->hop_limit;
  878. u->encap_limit = p->encap_limit;
  879. u->flowinfo = p->flowinfo;
  880. u->link = p->link;
  881. u->i_key = p->i_key;
  882. u->o_key = p->o_key;
  883. u->i_flags = p->i_flags;
  884. u->o_flags = p->o_flags;
  885. memcpy(u->name, p->name, sizeof(u->name));
  886. }
  887. static int ip6gre_tunnel_ioctl(struct net_device *dev,
  888. struct ifreq *ifr, int cmd)
  889. {
  890. int err = 0;
  891. struct ip6_tnl_parm2 p;
  892. struct __ip6_tnl_parm p1;
  893. struct ip6_tnl *t = netdev_priv(dev);
  894. struct net *net = t->net;
  895. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  896. switch (cmd) {
  897. case SIOCGETTUNNEL:
  898. if (dev == ign->fb_tunnel_dev) {
  899. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  900. err = -EFAULT;
  901. break;
  902. }
  903. ip6gre_tnl_parm_from_user(&p1, &p);
  904. t = ip6gre_tunnel_locate(net, &p1, 0);
  905. if (!t)
  906. t = netdev_priv(dev);
  907. }
  908. memset(&p, 0, sizeof(p));
  909. ip6gre_tnl_parm_to_user(&p, &t->parms);
  910. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  911. err = -EFAULT;
  912. break;
  913. case SIOCADDTUNNEL:
  914. case SIOCCHGTUNNEL:
  915. err = -EPERM;
  916. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  917. goto done;
  918. err = -EFAULT;
  919. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  920. goto done;
  921. err = -EINVAL;
  922. if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
  923. goto done;
  924. if (!(p.i_flags&GRE_KEY))
  925. p.i_key = 0;
  926. if (!(p.o_flags&GRE_KEY))
  927. p.o_key = 0;
  928. ip6gre_tnl_parm_from_user(&p1, &p);
  929. t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
  930. if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  931. if (t) {
  932. if (t->dev != dev) {
  933. err = -EEXIST;
  934. break;
  935. }
  936. } else {
  937. t = netdev_priv(dev);
  938. ip6gre_tunnel_unlink(ign, t);
  939. synchronize_net();
  940. ip6gre_tnl_change(t, &p1, 1);
  941. ip6gre_tunnel_link(ign, t);
  942. netdev_state_change(dev);
  943. }
  944. }
  945. if (t) {
  946. err = 0;
  947. memset(&p, 0, sizeof(p));
  948. ip6gre_tnl_parm_to_user(&p, &t->parms);
  949. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  950. err = -EFAULT;
  951. } else
  952. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  953. break;
  954. case SIOCDELTUNNEL:
  955. err = -EPERM;
  956. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  957. goto done;
  958. if (dev == ign->fb_tunnel_dev) {
  959. err = -EFAULT;
  960. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  961. goto done;
  962. err = -ENOENT;
  963. ip6gre_tnl_parm_from_user(&p1, &p);
  964. t = ip6gre_tunnel_locate(net, &p1, 0);
  965. if (!t)
  966. goto done;
  967. err = -EPERM;
  968. if (t == netdev_priv(ign->fb_tunnel_dev))
  969. goto done;
  970. dev = t->dev;
  971. }
  972. unregister_netdevice(dev);
  973. err = 0;
  974. break;
  975. default:
  976. err = -EINVAL;
  977. }
  978. done:
  979. return err;
  980. }
  981. static int ip6gre_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  982. {
  983. if (new_mtu < 68 ||
  984. new_mtu > 0xFFF8 - dev->hard_header_len)
  985. return -EINVAL;
  986. dev->mtu = new_mtu;
  987. return 0;
  988. }
  989. static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
  990. unsigned short type,
  991. const void *daddr, const void *saddr, unsigned int len)
  992. {
  993. struct ip6_tnl *t = netdev_priv(dev);
  994. struct ipv6hdr *ipv6h = (struct ipv6hdr *)skb_push(skb, t->hlen);
  995. __be16 *p = (__be16 *)(ipv6h+1);
  996. ip6_flow_hdr(ipv6h, 0,
  997. ip6_make_flowlabel(dev_net(dev), skb,
  998. t->fl.u.ip6.flowlabel, true,
  999. &t->fl.u.ip6));
  1000. ipv6h->hop_limit = t->parms.hop_limit;
  1001. ipv6h->nexthdr = NEXTHDR_GRE;
  1002. ipv6h->saddr = t->parms.laddr;
  1003. ipv6h->daddr = t->parms.raddr;
  1004. p[0] = t->parms.o_flags;
  1005. p[1] = htons(type);
  1006. /*
  1007. * Set the source hardware address.
  1008. */
  1009. if (saddr)
  1010. memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
  1011. if (daddr)
  1012. memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
  1013. if (!ipv6_addr_any(&ipv6h->daddr))
  1014. return t->hlen;
  1015. return -t->hlen;
  1016. }
  1017. static const struct header_ops ip6gre_header_ops = {
  1018. .create = ip6gre_header,
  1019. };
  1020. static const struct net_device_ops ip6gre_netdev_ops = {
  1021. .ndo_init = ip6gre_tunnel_init,
  1022. .ndo_uninit = ip6gre_tunnel_uninit,
  1023. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1024. .ndo_do_ioctl = ip6gre_tunnel_ioctl,
  1025. .ndo_change_mtu = ip6gre_tunnel_change_mtu,
  1026. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1027. .ndo_get_iflink = ip6_tnl_get_iflink,
  1028. };
  1029. static void ip6gre_dev_free(struct net_device *dev)
  1030. {
  1031. struct ip6_tnl *t = netdev_priv(dev);
  1032. ip6_tnl_dst_destroy(t);
  1033. free_percpu(dev->tstats);
  1034. free_netdev(dev);
  1035. }
  1036. static void ip6gre_tunnel_setup(struct net_device *dev)
  1037. {
  1038. struct ip6_tnl *t;
  1039. dev->netdev_ops = &ip6gre_netdev_ops;
  1040. dev->destructor = ip6gre_dev_free;
  1041. dev->type = ARPHRD_IP6GRE;
  1042. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr) + 4;
  1043. dev->mtu = ETH_DATA_LEN - sizeof(struct ipv6hdr) - 4;
  1044. t = netdev_priv(dev);
  1045. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1046. dev->mtu -= 8;
  1047. dev->flags |= IFF_NOARP;
  1048. dev->addr_len = sizeof(struct in6_addr);
  1049. netif_keep_dst(dev);
  1050. }
  1051. static int ip6gre_tunnel_init_common(struct net_device *dev)
  1052. {
  1053. struct ip6_tnl *tunnel;
  1054. int ret;
  1055. tunnel = netdev_priv(dev);
  1056. tunnel->dev = dev;
  1057. tunnel->net = dev_net(dev);
  1058. strcpy(tunnel->parms.name, dev->name);
  1059. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1060. if (!dev->tstats)
  1061. return -ENOMEM;
  1062. ret = ip6_tnl_dst_init(tunnel);
  1063. if (ret) {
  1064. free_percpu(dev->tstats);
  1065. dev->tstats = NULL;
  1066. return ret;
  1067. }
  1068. return 0;
  1069. }
  1070. static int ip6gre_tunnel_init(struct net_device *dev)
  1071. {
  1072. struct ip6_tnl *tunnel;
  1073. int ret;
  1074. ret = ip6gre_tunnel_init_common(dev);
  1075. if (ret)
  1076. return ret;
  1077. tunnel = netdev_priv(dev);
  1078. memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
  1079. memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
  1080. if (ipv6_addr_any(&tunnel->parms.raddr))
  1081. dev->header_ops = &ip6gre_header_ops;
  1082. return 0;
  1083. }
  1084. static void ip6gre_fb_tunnel_init(struct net_device *dev)
  1085. {
  1086. struct ip6_tnl *tunnel = netdev_priv(dev);
  1087. tunnel->dev = dev;
  1088. tunnel->net = dev_net(dev);
  1089. strcpy(tunnel->parms.name, dev->name);
  1090. tunnel->hlen = sizeof(struct ipv6hdr) + 4;
  1091. dev_hold(dev);
  1092. }
  1093. static struct inet6_protocol ip6gre_protocol __read_mostly = {
  1094. .handler = ip6gre_rcv,
  1095. .err_handler = ip6gre_err,
  1096. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  1097. };
  1098. static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)
  1099. {
  1100. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1101. struct net_device *dev, *aux;
  1102. int prio;
  1103. for_each_netdev_safe(net, dev, aux)
  1104. if (dev->rtnl_link_ops == &ip6gre_link_ops ||
  1105. dev->rtnl_link_ops == &ip6gre_tap_ops)
  1106. unregister_netdevice_queue(dev, head);
  1107. for (prio = 0; prio < 4; prio++) {
  1108. int h;
  1109. for (h = 0; h < HASH_SIZE; h++) {
  1110. struct ip6_tnl *t;
  1111. t = rtnl_dereference(ign->tunnels[prio][h]);
  1112. while (t) {
  1113. /* If dev is in the same netns, it has already
  1114. * been added to the list by the previous loop.
  1115. */
  1116. if (!net_eq(dev_net(t->dev), net))
  1117. unregister_netdevice_queue(t->dev,
  1118. head);
  1119. t = rtnl_dereference(t->next);
  1120. }
  1121. }
  1122. }
  1123. }
  1124. static int __net_init ip6gre_init_net(struct net *net)
  1125. {
  1126. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1127. int err;
  1128. ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
  1129. NET_NAME_UNKNOWN,
  1130. ip6gre_tunnel_setup);
  1131. if (!ign->fb_tunnel_dev) {
  1132. err = -ENOMEM;
  1133. goto err_alloc_dev;
  1134. }
  1135. dev_net_set(ign->fb_tunnel_dev, net);
  1136. /* FB netdevice is special: we have one, and only one per netns.
  1137. * Allowing to move it to another netns is clearly unsafe.
  1138. */
  1139. ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
  1140. ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
  1141. ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
  1142. err = register_netdev(ign->fb_tunnel_dev);
  1143. if (err)
  1144. goto err_reg_dev;
  1145. rcu_assign_pointer(ign->tunnels_wc[0],
  1146. netdev_priv(ign->fb_tunnel_dev));
  1147. return 0;
  1148. err_reg_dev:
  1149. ip6gre_dev_free(ign->fb_tunnel_dev);
  1150. err_alloc_dev:
  1151. return err;
  1152. }
  1153. static void __net_exit ip6gre_exit_net(struct net *net)
  1154. {
  1155. LIST_HEAD(list);
  1156. rtnl_lock();
  1157. ip6gre_destroy_tunnels(net, &list);
  1158. unregister_netdevice_many(&list);
  1159. rtnl_unlock();
  1160. }
  1161. static struct pernet_operations ip6gre_net_ops = {
  1162. .init = ip6gre_init_net,
  1163. .exit = ip6gre_exit_net,
  1164. .id = &ip6gre_net_id,
  1165. .size = sizeof(struct ip6gre_net),
  1166. };
  1167. static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
  1168. {
  1169. __be16 flags;
  1170. if (!data)
  1171. return 0;
  1172. flags = 0;
  1173. if (data[IFLA_GRE_IFLAGS])
  1174. flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
  1175. if (data[IFLA_GRE_OFLAGS])
  1176. flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
  1177. if (flags & (GRE_VERSION|GRE_ROUTING))
  1178. return -EINVAL;
  1179. return 0;
  1180. }
  1181. static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[])
  1182. {
  1183. struct in6_addr daddr;
  1184. if (tb[IFLA_ADDRESS]) {
  1185. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  1186. return -EINVAL;
  1187. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  1188. return -EADDRNOTAVAIL;
  1189. }
  1190. if (!data)
  1191. goto out;
  1192. if (data[IFLA_GRE_REMOTE]) {
  1193. daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
  1194. if (ipv6_addr_any(&daddr))
  1195. return -EINVAL;
  1196. }
  1197. out:
  1198. return ip6gre_tunnel_validate(tb, data);
  1199. }
  1200. static void ip6gre_netlink_parms(struct nlattr *data[],
  1201. struct __ip6_tnl_parm *parms)
  1202. {
  1203. memset(parms, 0, sizeof(*parms));
  1204. if (!data)
  1205. return;
  1206. if (data[IFLA_GRE_LINK])
  1207. parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
  1208. if (data[IFLA_GRE_IFLAGS])
  1209. parms->i_flags = nla_get_be16(data[IFLA_GRE_IFLAGS]);
  1210. if (data[IFLA_GRE_OFLAGS])
  1211. parms->o_flags = nla_get_be16(data[IFLA_GRE_OFLAGS]);
  1212. if (data[IFLA_GRE_IKEY])
  1213. parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
  1214. if (data[IFLA_GRE_OKEY])
  1215. parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
  1216. if (data[IFLA_GRE_LOCAL])
  1217. parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
  1218. if (data[IFLA_GRE_REMOTE])
  1219. parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
  1220. if (data[IFLA_GRE_TTL])
  1221. parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
  1222. if (data[IFLA_GRE_ENCAP_LIMIT])
  1223. parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
  1224. if (data[IFLA_GRE_FLOWINFO])
  1225. parms->flowinfo = nla_get_u32(data[IFLA_GRE_FLOWINFO]);
  1226. if (data[IFLA_GRE_FLAGS])
  1227. parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
  1228. }
  1229. static int ip6gre_tap_init(struct net_device *dev)
  1230. {
  1231. struct ip6_tnl *tunnel;
  1232. int ret;
  1233. ret = ip6gre_tunnel_init_common(dev);
  1234. if (ret)
  1235. return ret;
  1236. tunnel = netdev_priv(dev);
  1237. ip6gre_tnl_link_config(tunnel, 1);
  1238. return 0;
  1239. }
  1240. static const struct net_device_ops ip6gre_tap_netdev_ops = {
  1241. .ndo_init = ip6gre_tap_init,
  1242. .ndo_uninit = ip6gre_tunnel_uninit,
  1243. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1244. .ndo_set_mac_address = eth_mac_addr,
  1245. .ndo_validate_addr = eth_validate_addr,
  1246. .ndo_change_mtu = ip6gre_tunnel_change_mtu,
  1247. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1248. .ndo_get_iflink = ip6_tnl_get_iflink,
  1249. };
  1250. static void ip6gre_tap_setup(struct net_device *dev)
  1251. {
  1252. ether_setup(dev);
  1253. dev->netdev_ops = &ip6gre_tap_netdev_ops;
  1254. dev->destructor = ip6gre_dev_free;
  1255. dev->features |= NETIF_F_NETNS_LOCAL;
  1256. }
  1257. static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
  1258. struct nlattr *tb[], struct nlattr *data[])
  1259. {
  1260. struct ip6_tnl *nt;
  1261. struct net *net = dev_net(dev);
  1262. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1263. int err;
  1264. nt = netdev_priv(dev);
  1265. ip6gre_netlink_parms(data, &nt->parms);
  1266. if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
  1267. return -EEXIST;
  1268. if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
  1269. eth_hw_addr_random(dev);
  1270. nt->dev = dev;
  1271. nt->net = dev_net(dev);
  1272. ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
  1273. /* Can use a lockless transmit, unless we generate output sequences */
  1274. if (!(nt->parms.o_flags & GRE_SEQ))
  1275. dev->features |= NETIF_F_LLTX;
  1276. err = register_netdevice(dev);
  1277. if (err)
  1278. goto out;
  1279. dev_hold(dev);
  1280. ip6gre_tunnel_link(ign, nt);
  1281. out:
  1282. return err;
  1283. }
  1284. static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
  1285. struct nlattr *data[])
  1286. {
  1287. struct ip6_tnl *t, *nt = netdev_priv(dev);
  1288. struct net *net = nt->net;
  1289. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1290. struct __ip6_tnl_parm p;
  1291. if (dev == ign->fb_tunnel_dev)
  1292. return -EINVAL;
  1293. ip6gre_netlink_parms(data, &p);
  1294. t = ip6gre_tunnel_locate(net, &p, 0);
  1295. if (t) {
  1296. if (t->dev != dev)
  1297. return -EEXIST;
  1298. } else {
  1299. t = nt;
  1300. }
  1301. ip6gre_tunnel_unlink(ign, t);
  1302. ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
  1303. ip6gre_tunnel_link(ign, t);
  1304. return 0;
  1305. }
  1306. static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
  1307. {
  1308. struct net *net = dev_net(dev);
  1309. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1310. if (dev != ign->fb_tunnel_dev)
  1311. unregister_netdevice_queue(dev, head);
  1312. }
  1313. static size_t ip6gre_get_size(const struct net_device *dev)
  1314. {
  1315. return
  1316. /* IFLA_GRE_LINK */
  1317. nla_total_size(4) +
  1318. /* IFLA_GRE_IFLAGS */
  1319. nla_total_size(2) +
  1320. /* IFLA_GRE_OFLAGS */
  1321. nla_total_size(2) +
  1322. /* IFLA_GRE_IKEY */
  1323. nla_total_size(4) +
  1324. /* IFLA_GRE_OKEY */
  1325. nla_total_size(4) +
  1326. /* IFLA_GRE_LOCAL */
  1327. nla_total_size(sizeof(struct in6_addr)) +
  1328. /* IFLA_GRE_REMOTE */
  1329. nla_total_size(sizeof(struct in6_addr)) +
  1330. /* IFLA_GRE_TTL */
  1331. nla_total_size(1) +
  1332. /* IFLA_GRE_TOS */
  1333. nla_total_size(1) +
  1334. /* IFLA_GRE_ENCAP_LIMIT */
  1335. nla_total_size(1) +
  1336. /* IFLA_GRE_FLOWINFO */
  1337. nla_total_size(4) +
  1338. /* IFLA_GRE_FLAGS */
  1339. nla_total_size(4) +
  1340. 0;
  1341. }
  1342. static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1343. {
  1344. struct ip6_tnl *t = netdev_priv(dev);
  1345. struct __ip6_tnl_parm *p = &t->parms;
  1346. if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
  1347. nla_put_be16(skb, IFLA_GRE_IFLAGS, p->i_flags) ||
  1348. nla_put_be16(skb, IFLA_GRE_OFLAGS, p->o_flags) ||
  1349. nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
  1350. nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
  1351. nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
  1352. nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
  1353. nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
  1354. /*nla_put_u8(skb, IFLA_GRE_TOS, t->priority) ||*/
  1355. nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
  1356. nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
  1357. nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags))
  1358. goto nla_put_failure;
  1359. return 0;
  1360. nla_put_failure:
  1361. return -EMSGSIZE;
  1362. }
  1363. static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
  1364. [IFLA_GRE_LINK] = { .type = NLA_U32 },
  1365. [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
  1366. [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
  1367. [IFLA_GRE_IKEY] = { .type = NLA_U32 },
  1368. [IFLA_GRE_OKEY] = { .type = NLA_U32 },
  1369. [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
  1370. [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
  1371. [IFLA_GRE_TTL] = { .type = NLA_U8 },
  1372. [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
  1373. [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 },
  1374. [IFLA_GRE_FLAGS] = { .type = NLA_U32 },
  1375. };
  1376. static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
  1377. .kind = "ip6gre",
  1378. .maxtype = IFLA_GRE_MAX,
  1379. .policy = ip6gre_policy,
  1380. .priv_size = sizeof(struct ip6_tnl),
  1381. .setup = ip6gre_tunnel_setup,
  1382. .validate = ip6gre_tunnel_validate,
  1383. .newlink = ip6gre_newlink,
  1384. .changelink = ip6gre_changelink,
  1385. .dellink = ip6gre_dellink,
  1386. .get_size = ip6gre_get_size,
  1387. .fill_info = ip6gre_fill_info,
  1388. .get_link_net = ip6_tnl_get_link_net,
  1389. };
  1390. static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
  1391. .kind = "ip6gretap",
  1392. .maxtype = IFLA_GRE_MAX,
  1393. .policy = ip6gre_policy,
  1394. .priv_size = sizeof(struct ip6_tnl),
  1395. .setup = ip6gre_tap_setup,
  1396. .validate = ip6gre_tap_validate,
  1397. .newlink = ip6gre_newlink,
  1398. .changelink = ip6gre_changelink,
  1399. .get_size = ip6gre_get_size,
  1400. .fill_info = ip6gre_fill_info,
  1401. .get_link_net = ip6_tnl_get_link_net,
  1402. };
  1403. /*
  1404. * And now the modules code and kernel interface.
  1405. */
  1406. static int __init ip6gre_init(void)
  1407. {
  1408. int err;
  1409. pr_info("GRE over IPv6 tunneling driver\n");
  1410. err = register_pernet_device(&ip6gre_net_ops);
  1411. if (err < 0)
  1412. return err;
  1413. err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1414. if (err < 0) {
  1415. pr_info("%s: can't add protocol\n", __func__);
  1416. goto add_proto_failed;
  1417. }
  1418. err = rtnl_link_register(&ip6gre_link_ops);
  1419. if (err < 0)
  1420. goto rtnl_link_failed;
  1421. err = rtnl_link_register(&ip6gre_tap_ops);
  1422. if (err < 0)
  1423. goto tap_ops_failed;
  1424. out:
  1425. return err;
  1426. tap_ops_failed:
  1427. rtnl_link_unregister(&ip6gre_link_ops);
  1428. rtnl_link_failed:
  1429. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1430. add_proto_failed:
  1431. unregister_pernet_device(&ip6gre_net_ops);
  1432. goto out;
  1433. }
  1434. static void __exit ip6gre_fini(void)
  1435. {
  1436. rtnl_link_unregister(&ip6gre_tap_ops);
  1437. rtnl_link_unregister(&ip6gre_link_ops);
  1438. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1439. unregister_pernet_device(&ip6gre_net_ops);
  1440. }
  1441. module_init(ip6gre_init);
  1442. module_exit(ip6gre_fini);
  1443. MODULE_LICENSE("GPL");
  1444. MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
  1445. MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
  1446. MODULE_ALIAS_RTNL_LINK("ip6gre");
  1447. MODULE_ALIAS_RTNL_LINK("ip6gretap");
  1448. MODULE_ALIAS_NETDEV("ip6gre0");