ip6_gre.c 41 KB

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