ip6_gre.c 40 KB

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