ip6_gre.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628
  1. /*
  2. * GRE over IPv6 protocol decoder.
  3. *
  4. * Authors: Dmitry Kozlov (xeb@mail.ru)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/capability.h>
  14. #include <linux/module.h>
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/slab.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/in.h>
  22. #include <linux/tcp.h>
  23. #include <linux/udp.h>
  24. #include <linux/if_arp.h>
  25. #include <linux/init.h>
  26. #include <linux/in6.h>
  27. #include <linux/inetdevice.h>
  28. #include <linux/igmp.h>
  29. #include <linux/netfilter_ipv4.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/if_ether.h>
  32. #include <linux/hash.h>
  33. #include <linux/if_tunnel.h>
  34. #include <linux/ip6_tunnel.h>
  35. #include <net/sock.h>
  36. #include <net/ip.h>
  37. #include <net/ip_tunnels.h>
  38. #include <net/icmp.h>
  39. #include <net/protocol.h>
  40. #include <net/addrconf.h>
  41. #include <net/arp.h>
  42. #include <net/checksum.h>
  43. #include <net/dsfield.h>
  44. #include <net/inet_ecn.h>
  45. #include <net/xfrm.h>
  46. #include <net/net_namespace.h>
  47. #include <net/netns/generic.h>
  48. #include <net/rtnetlink.h>
  49. #include <net/ipv6.h>
  50. #include <net/ip6_fib.h>
  51. #include <net/ip6_route.h>
  52. #include <net/ip6_tunnel.h>
  53. #include <net/gre.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 IP6_GRE_HASH_SIZE_SHIFT 5
  58. #define IP6_GRE_HASH_SIZE (1 << IP6_GRE_HASH_SIZE_SHIFT)
  59. static int ip6gre_net_id __read_mostly;
  60. struct ip6gre_net {
  61. struct ip6_tnl __rcu *tunnels[4][IP6_GRE_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))&(IP6_GRE_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, IP6_GRE_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. if (!dev_valid_name(parms->name))
  271. return NULL;
  272. strlcpy(name, parms->name, IFNAMSIZ);
  273. } else {
  274. strcpy(name, "ip6gre%d");
  275. }
  276. dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
  277. ip6gre_tunnel_setup);
  278. if (!dev)
  279. return NULL;
  280. dev_net_set(dev, net);
  281. nt = netdev_priv(dev);
  282. nt->parms = *parms;
  283. dev->rtnl_link_ops = &ip6gre_link_ops;
  284. nt->dev = dev;
  285. nt->net = dev_net(dev);
  286. if (register_netdevice(dev) < 0)
  287. goto failed_free;
  288. ip6gre_tnl_link_config(nt, 1);
  289. /* Can use a lockless transmit, unless we generate output sequences */
  290. if (!(nt->parms.o_flags & TUNNEL_SEQ))
  291. dev->features |= NETIF_F_LLTX;
  292. dev_hold(dev);
  293. ip6gre_tunnel_link(ign, nt);
  294. return nt;
  295. failed_free:
  296. free_netdev(dev);
  297. return NULL;
  298. }
  299. static void ip6gre_tunnel_uninit(struct net_device *dev)
  300. {
  301. struct ip6_tnl *t = netdev_priv(dev);
  302. struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
  303. ip6gre_tunnel_unlink(ign, t);
  304. dst_cache_reset(&t->dst_cache);
  305. dev_put(dev);
  306. }
  307. static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  308. u8 type, u8 code, int offset, __be32 info)
  309. {
  310. const struct gre_base_hdr *greh;
  311. const struct ipv6hdr *ipv6h;
  312. int grehlen = sizeof(*greh);
  313. struct ip6_tnl *t;
  314. int key_off = 0;
  315. __be16 flags;
  316. __be32 key;
  317. if (!pskb_may_pull(skb, offset + grehlen))
  318. return;
  319. greh = (const struct gre_base_hdr *)(skb->data + offset);
  320. flags = greh->flags;
  321. if (flags & (GRE_VERSION | GRE_ROUTING))
  322. return;
  323. if (flags & GRE_CSUM)
  324. grehlen += 4;
  325. if (flags & GRE_KEY) {
  326. key_off = grehlen + offset;
  327. grehlen += 4;
  328. }
  329. if (!pskb_may_pull(skb, offset + grehlen))
  330. return;
  331. ipv6h = (const struct ipv6hdr *)skb->data;
  332. greh = (const struct gre_base_hdr *)(skb->data + offset);
  333. key = key_off ? *(__be32 *)(skb->data + key_off) : 0;
  334. t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
  335. key, greh->protocol);
  336. if (!t)
  337. return;
  338. switch (type) {
  339. __u32 teli;
  340. struct ipv6_tlv_tnl_enc_lim *tel;
  341. __u32 mtu;
  342. case ICMPV6_DEST_UNREACH:
  343. net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
  344. t->parms.name);
  345. if (code != ICMPV6_PORT_UNREACH)
  346. break;
  347. return;
  348. case ICMPV6_TIME_EXCEED:
  349. if (code == ICMPV6_EXC_HOPLIMIT) {
  350. net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
  351. t->parms.name);
  352. break;
  353. }
  354. return;
  355. case ICMPV6_PARAMPROB:
  356. teli = 0;
  357. if (code == ICMPV6_HDR_FIELD)
  358. teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
  359. if (teli && teli == be32_to_cpu(info) - 2) {
  360. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
  361. if (tel->encap_limit == 0) {
  362. net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
  363. t->parms.name);
  364. }
  365. } else {
  366. net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
  367. t->parms.name);
  368. }
  369. return;
  370. case ICMPV6_PKT_TOOBIG:
  371. mtu = be32_to_cpu(info) - offset - t->tun_hlen;
  372. if (t->dev->type == ARPHRD_ETHER)
  373. mtu -= ETH_HLEN;
  374. if (mtu < IPV6_MIN_MTU)
  375. mtu = IPV6_MIN_MTU;
  376. t->dev->mtu = mtu;
  377. return;
  378. }
  379. if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO))
  380. t->err_count++;
  381. else
  382. t->err_count = 1;
  383. t->err_time = jiffies;
  384. }
  385. static int ip6gre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
  386. {
  387. const struct ipv6hdr *ipv6h;
  388. struct ip6_tnl *tunnel;
  389. ipv6h = ipv6_hdr(skb);
  390. tunnel = ip6gre_tunnel_lookup(skb->dev,
  391. &ipv6h->saddr, &ipv6h->daddr, tpi->key,
  392. tpi->proto);
  393. if (tunnel) {
  394. ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
  395. return PACKET_RCVD;
  396. }
  397. return PACKET_REJECT;
  398. }
  399. static int gre_rcv(struct sk_buff *skb)
  400. {
  401. struct tnl_ptk_info tpi;
  402. bool csum_err = false;
  403. int hdr_len;
  404. hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IPV6), 0);
  405. if (hdr_len < 0)
  406. goto drop;
  407. if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false))
  408. goto drop;
  409. if (ip6gre_rcv(skb, &tpi) == PACKET_RCVD)
  410. return 0;
  411. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  412. drop:
  413. kfree_skb(skb);
  414. return 0;
  415. }
  416. struct ipv6_tel_txoption {
  417. struct ipv6_txoptions ops;
  418. __u8 dst_opt[8];
  419. };
  420. static int gre_handle_offloads(struct sk_buff *skb, bool csum)
  421. {
  422. return iptunnel_handle_offloads(skb,
  423. csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
  424. }
  425. static netdev_tx_t __gre6_xmit(struct sk_buff *skb,
  426. struct net_device *dev, __u8 dsfield,
  427. struct flowi6 *fl6, int encap_limit,
  428. __u32 *pmtu, __be16 proto)
  429. {
  430. struct ip6_tnl *tunnel = netdev_priv(dev);
  431. struct dst_entry *dst = skb_dst(skb);
  432. __be16 protocol;
  433. if (dev->type == ARPHRD_ETHER)
  434. IPCB(skb)->flags = 0;
  435. if (dev->header_ops && dev->type == ARPHRD_IP6GRE)
  436. fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr;
  437. else
  438. fl6->daddr = tunnel->parms.raddr;
  439. if (tunnel->parms.o_flags & TUNNEL_SEQ)
  440. tunnel->o_seqno++;
  441. /* Push GRE header. */
  442. protocol = (dev->type == ARPHRD_ETHER) ? htons(ETH_P_TEB) : proto;
  443. gre_build_header(skb, tunnel->tun_hlen, tunnel->parms.o_flags,
  444. protocol, tunnel->parms.o_key, htonl(tunnel->o_seqno));
  445. /* TooBig packet may have updated dst->dev's mtu */
  446. if (dst && dst_mtu(dst) > dst->dev->mtu)
  447. dst->ops->update_pmtu(dst, NULL, skb, dst->dev->mtu);
  448. return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu,
  449. NEXTHDR_GRE);
  450. }
  451. static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
  452. {
  453. struct ip6_tnl *t = netdev_priv(dev);
  454. const struct iphdr *iph = ip_hdr(skb);
  455. int encap_limit = -1;
  456. struct flowi6 fl6;
  457. __u8 dsfield;
  458. __u32 mtu;
  459. int err;
  460. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  461. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  462. encap_limit = t->parms.encap_limit;
  463. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  464. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  465. dsfield = ipv4_get_dsfield(iph);
  466. else
  467. dsfield = ip6_tclass(t->parms.flowinfo);
  468. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  469. fl6.flowi6_mark = skb->mark;
  470. err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
  471. if (err)
  472. return -1;
  473. err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
  474. skb->protocol);
  475. if (err != 0) {
  476. /* XXX: send ICMP error even if DF is not set. */
  477. if (err == -EMSGSIZE)
  478. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  479. htonl(mtu));
  480. return -1;
  481. }
  482. return 0;
  483. }
  484. static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
  485. {
  486. struct ip6_tnl *t = netdev_priv(dev);
  487. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  488. int encap_limit = -1;
  489. __u16 offset;
  490. struct flowi6 fl6;
  491. __u8 dsfield;
  492. __u32 mtu;
  493. int err;
  494. if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
  495. return -1;
  496. offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
  497. /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
  498. ipv6h = ipv6_hdr(skb);
  499. if (offset > 0) {
  500. struct ipv6_tlv_tnl_enc_lim *tel;
  501. tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
  502. if (tel->encap_limit == 0) {
  503. icmpv6_send(skb, ICMPV6_PARAMPROB,
  504. ICMPV6_HDR_FIELD, offset + 2);
  505. return -1;
  506. }
  507. encap_limit = tel->encap_limit - 1;
  508. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  509. encap_limit = t->parms.encap_limit;
  510. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  511. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  512. dsfield = ipv6_get_dsfield(ipv6h);
  513. else
  514. dsfield = ip6_tclass(t->parms.flowinfo);
  515. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
  516. fl6.flowlabel |= ip6_flowlabel(ipv6h);
  517. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  518. fl6.flowi6_mark = skb->mark;
  519. if (gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)))
  520. return -1;
  521. err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit,
  522. &mtu, skb->protocol);
  523. if (err != 0) {
  524. if (err == -EMSGSIZE)
  525. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  526. return -1;
  527. }
  528. return 0;
  529. }
  530. /**
  531. * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  532. * @t: the outgoing tunnel device
  533. * @hdr: IPv6 header from the incoming packet
  534. *
  535. * Description:
  536. * Avoid trivial tunneling loop by checking that tunnel exit-point
  537. * doesn't match source of incoming packet.
  538. *
  539. * Return:
  540. * 1 if conflict,
  541. * 0 else
  542. **/
  543. static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
  544. const struct ipv6hdr *hdr)
  545. {
  546. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  547. }
  548. static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
  549. {
  550. struct ip6_tnl *t = netdev_priv(dev);
  551. int encap_limit = -1;
  552. struct flowi6 fl6;
  553. __u32 mtu;
  554. int err;
  555. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  556. encap_limit = t->parms.encap_limit;
  557. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  558. err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
  559. if (err)
  560. return err;
  561. err = __gre6_xmit(skb, dev, 0, &fl6, encap_limit, &mtu, skb->protocol);
  562. return err;
  563. }
  564. static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
  565. struct net_device *dev)
  566. {
  567. struct ip6_tnl *t = netdev_priv(dev);
  568. struct net_device_stats *stats = &t->dev->stats;
  569. int ret;
  570. if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
  571. goto tx_err;
  572. switch (skb->protocol) {
  573. case htons(ETH_P_IP):
  574. ret = ip6gre_xmit_ipv4(skb, dev);
  575. break;
  576. case htons(ETH_P_IPV6):
  577. ret = ip6gre_xmit_ipv6(skb, dev);
  578. break;
  579. default:
  580. ret = ip6gre_xmit_other(skb, dev);
  581. break;
  582. }
  583. if (ret < 0)
  584. goto tx_err;
  585. return NETDEV_TX_OK;
  586. tx_err:
  587. stats->tx_errors++;
  588. stats->tx_dropped++;
  589. kfree_skb(skb);
  590. return NETDEV_TX_OK;
  591. }
  592. static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
  593. {
  594. struct net_device *dev = t->dev;
  595. struct __ip6_tnl_parm *p = &t->parms;
  596. struct flowi6 *fl6 = &t->fl.u.ip6;
  597. int t_hlen;
  598. if (dev->type != ARPHRD_ETHER) {
  599. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  600. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  601. }
  602. /* Set up flowi template */
  603. fl6->saddr = p->laddr;
  604. fl6->daddr = p->raddr;
  605. fl6->flowi6_oif = p->link;
  606. fl6->flowlabel = 0;
  607. fl6->flowi6_proto = IPPROTO_GRE;
  608. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  609. fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  610. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  611. fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  612. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
  613. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  614. if (p->flags&IP6_TNL_F_CAP_XMIT &&
  615. p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
  616. dev->flags |= IFF_POINTOPOINT;
  617. else
  618. dev->flags &= ~IFF_POINTOPOINT;
  619. t->tun_hlen = gre_calc_hlen(t->parms.o_flags);
  620. t->hlen = t->encap_hlen + t->tun_hlen;
  621. t_hlen = t->hlen + sizeof(struct ipv6hdr);
  622. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  623. int strict = (ipv6_addr_type(&p->raddr) &
  624. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  625. struct rt6_info *rt = rt6_lookup(t->net,
  626. &p->raddr, &p->laddr,
  627. p->link, strict);
  628. if (!rt)
  629. return;
  630. if (rt->dst.dev) {
  631. dev->hard_header_len = rt->dst.dev->hard_header_len +
  632. t_hlen;
  633. if (set_mtu) {
  634. dev->mtu = rt->dst.dev->mtu - t_hlen;
  635. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  636. dev->mtu -= 8;
  637. if (dev->type == ARPHRD_ETHER)
  638. dev->mtu -= ETH_HLEN;
  639. if (dev->mtu < IPV6_MIN_MTU)
  640. dev->mtu = IPV6_MIN_MTU;
  641. }
  642. }
  643. ip6_rt_put(rt);
  644. }
  645. }
  646. static int ip6gre_tnl_change(struct ip6_tnl *t,
  647. const struct __ip6_tnl_parm *p, int set_mtu)
  648. {
  649. t->parms.laddr = p->laddr;
  650. t->parms.raddr = p->raddr;
  651. t->parms.flags = p->flags;
  652. t->parms.hop_limit = p->hop_limit;
  653. t->parms.encap_limit = p->encap_limit;
  654. t->parms.flowinfo = p->flowinfo;
  655. t->parms.link = p->link;
  656. t->parms.proto = p->proto;
  657. t->parms.i_key = p->i_key;
  658. t->parms.o_key = p->o_key;
  659. t->parms.i_flags = p->i_flags;
  660. t->parms.o_flags = p->o_flags;
  661. dst_cache_reset(&t->dst_cache);
  662. ip6gre_tnl_link_config(t, set_mtu);
  663. return 0;
  664. }
  665. static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
  666. const struct ip6_tnl_parm2 *u)
  667. {
  668. p->laddr = u->laddr;
  669. p->raddr = u->raddr;
  670. p->flags = u->flags;
  671. p->hop_limit = u->hop_limit;
  672. p->encap_limit = u->encap_limit;
  673. p->flowinfo = u->flowinfo;
  674. p->link = u->link;
  675. p->i_key = u->i_key;
  676. p->o_key = u->o_key;
  677. p->i_flags = gre_flags_to_tnl_flags(u->i_flags);
  678. p->o_flags = gre_flags_to_tnl_flags(u->o_flags);
  679. memcpy(p->name, u->name, sizeof(u->name));
  680. }
  681. static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
  682. const struct __ip6_tnl_parm *p)
  683. {
  684. u->proto = IPPROTO_GRE;
  685. u->laddr = p->laddr;
  686. u->raddr = p->raddr;
  687. u->flags = p->flags;
  688. u->hop_limit = p->hop_limit;
  689. u->encap_limit = p->encap_limit;
  690. u->flowinfo = p->flowinfo;
  691. u->link = p->link;
  692. u->i_key = p->i_key;
  693. u->o_key = p->o_key;
  694. u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
  695. u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
  696. memcpy(u->name, p->name, sizeof(u->name));
  697. }
  698. static int ip6gre_tunnel_ioctl(struct net_device *dev,
  699. struct ifreq *ifr, int cmd)
  700. {
  701. int err = 0;
  702. struct ip6_tnl_parm2 p;
  703. struct __ip6_tnl_parm p1;
  704. struct ip6_tnl *t = netdev_priv(dev);
  705. struct net *net = t->net;
  706. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  707. memset(&p1, 0, sizeof(p1));
  708. switch (cmd) {
  709. case SIOCGETTUNNEL:
  710. if (dev == ign->fb_tunnel_dev) {
  711. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  712. err = -EFAULT;
  713. break;
  714. }
  715. ip6gre_tnl_parm_from_user(&p1, &p);
  716. t = ip6gre_tunnel_locate(net, &p1, 0);
  717. if (!t)
  718. t = netdev_priv(dev);
  719. }
  720. memset(&p, 0, sizeof(p));
  721. ip6gre_tnl_parm_to_user(&p, &t->parms);
  722. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  723. err = -EFAULT;
  724. break;
  725. case SIOCADDTUNNEL:
  726. case SIOCCHGTUNNEL:
  727. err = -EPERM;
  728. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  729. goto done;
  730. err = -EFAULT;
  731. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  732. goto done;
  733. err = -EINVAL;
  734. if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
  735. goto done;
  736. if (!(p.i_flags&GRE_KEY))
  737. p.i_key = 0;
  738. if (!(p.o_flags&GRE_KEY))
  739. p.o_key = 0;
  740. ip6gre_tnl_parm_from_user(&p1, &p);
  741. t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
  742. if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  743. if (t) {
  744. if (t->dev != dev) {
  745. err = -EEXIST;
  746. break;
  747. }
  748. } else {
  749. t = netdev_priv(dev);
  750. ip6gre_tunnel_unlink(ign, t);
  751. synchronize_net();
  752. ip6gre_tnl_change(t, &p1, 1);
  753. ip6gre_tunnel_link(ign, t);
  754. netdev_state_change(dev);
  755. }
  756. }
  757. if (t) {
  758. err = 0;
  759. memset(&p, 0, sizeof(p));
  760. ip6gre_tnl_parm_to_user(&p, &t->parms);
  761. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  762. err = -EFAULT;
  763. } else
  764. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  765. break;
  766. case SIOCDELTUNNEL:
  767. err = -EPERM;
  768. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  769. goto done;
  770. if (dev == ign->fb_tunnel_dev) {
  771. err = -EFAULT;
  772. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  773. goto done;
  774. err = -ENOENT;
  775. ip6gre_tnl_parm_from_user(&p1, &p);
  776. t = ip6gre_tunnel_locate(net, &p1, 0);
  777. if (!t)
  778. goto done;
  779. err = -EPERM;
  780. if (t == netdev_priv(ign->fb_tunnel_dev))
  781. goto done;
  782. dev = t->dev;
  783. }
  784. unregister_netdevice(dev);
  785. err = 0;
  786. break;
  787. default:
  788. err = -EINVAL;
  789. }
  790. done:
  791. return err;
  792. }
  793. static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
  794. unsigned short type, const void *daddr,
  795. const void *saddr, unsigned int len)
  796. {
  797. struct ip6_tnl *t = netdev_priv(dev);
  798. struct ipv6hdr *ipv6h;
  799. __be16 *p;
  800. ipv6h = (struct ipv6hdr *)skb_push(skb, t->hlen + sizeof(*ipv6h));
  801. ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
  802. t->fl.u.ip6.flowlabel,
  803. true, &t->fl.u.ip6));
  804. ipv6h->hop_limit = t->parms.hop_limit;
  805. ipv6h->nexthdr = NEXTHDR_GRE;
  806. ipv6h->saddr = t->parms.laddr;
  807. ipv6h->daddr = t->parms.raddr;
  808. p = (__be16 *)(ipv6h + 1);
  809. p[0] = t->parms.o_flags;
  810. p[1] = htons(type);
  811. /*
  812. * Set the source hardware address.
  813. */
  814. if (saddr)
  815. memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
  816. if (daddr)
  817. memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
  818. if (!ipv6_addr_any(&ipv6h->daddr))
  819. return t->hlen;
  820. return -t->hlen;
  821. }
  822. static const struct header_ops ip6gre_header_ops = {
  823. .create = ip6gre_header,
  824. };
  825. static const struct net_device_ops ip6gre_netdev_ops = {
  826. .ndo_init = ip6gre_tunnel_init,
  827. .ndo_uninit = ip6gre_tunnel_uninit,
  828. .ndo_start_xmit = ip6gre_tunnel_xmit,
  829. .ndo_do_ioctl = ip6gre_tunnel_ioctl,
  830. .ndo_change_mtu = ip6_tnl_change_mtu,
  831. .ndo_get_stats64 = ip_tunnel_get_stats64,
  832. .ndo_get_iflink = ip6_tnl_get_iflink,
  833. };
  834. static void ip6gre_dev_free(struct net_device *dev)
  835. {
  836. struct ip6_tnl *t = netdev_priv(dev);
  837. dst_cache_destroy(&t->dst_cache);
  838. free_percpu(dev->tstats);
  839. free_netdev(dev);
  840. }
  841. static void ip6gre_tunnel_setup(struct net_device *dev)
  842. {
  843. dev->netdev_ops = &ip6gre_netdev_ops;
  844. dev->destructor = ip6gre_dev_free;
  845. dev->type = ARPHRD_IP6GRE;
  846. dev->flags |= IFF_NOARP;
  847. dev->addr_len = sizeof(struct in6_addr);
  848. netif_keep_dst(dev);
  849. }
  850. static int ip6gre_tunnel_init_common(struct net_device *dev)
  851. {
  852. struct ip6_tnl *tunnel;
  853. int ret;
  854. int t_hlen;
  855. tunnel = netdev_priv(dev);
  856. tunnel->dev = dev;
  857. tunnel->net = dev_net(dev);
  858. strcpy(tunnel->parms.name, dev->name);
  859. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  860. if (!dev->tstats)
  861. return -ENOMEM;
  862. ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
  863. if (ret) {
  864. free_percpu(dev->tstats);
  865. dev->tstats = NULL;
  866. return ret;
  867. }
  868. tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
  869. tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
  870. t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
  871. dev->hard_header_len = LL_MAX_HEADER + t_hlen;
  872. dev->mtu = ETH_DATA_LEN - t_hlen;
  873. if (dev->type == ARPHRD_ETHER)
  874. dev->mtu -= ETH_HLEN;
  875. if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  876. dev->mtu -= 8;
  877. return 0;
  878. }
  879. static int ip6gre_tunnel_init(struct net_device *dev)
  880. {
  881. struct ip6_tnl *tunnel;
  882. int ret;
  883. ret = ip6gre_tunnel_init_common(dev);
  884. if (ret)
  885. return ret;
  886. tunnel = netdev_priv(dev);
  887. memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
  888. memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
  889. if (ipv6_addr_any(&tunnel->parms.raddr))
  890. dev->header_ops = &ip6gre_header_ops;
  891. return 0;
  892. }
  893. static void ip6gre_fb_tunnel_init(struct net_device *dev)
  894. {
  895. struct ip6_tnl *tunnel = netdev_priv(dev);
  896. tunnel->dev = dev;
  897. tunnel->net = dev_net(dev);
  898. strcpy(tunnel->parms.name, dev->name);
  899. tunnel->hlen = sizeof(struct ipv6hdr) + 4;
  900. dev_hold(dev);
  901. }
  902. static struct inet6_protocol ip6gre_protocol __read_mostly = {
  903. .handler = gre_rcv,
  904. .err_handler = ip6gre_err,
  905. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  906. };
  907. static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)
  908. {
  909. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  910. struct net_device *dev, *aux;
  911. int prio;
  912. for_each_netdev_safe(net, dev, aux)
  913. if (dev->rtnl_link_ops == &ip6gre_link_ops ||
  914. dev->rtnl_link_ops == &ip6gre_tap_ops)
  915. unregister_netdevice_queue(dev, head);
  916. for (prio = 0; prio < 4; prio++) {
  917. int h;
  918. for (h = 0; h < IP6_GRE_HASH_SIZE; h++) {
  919. struct ip6_tnl *t;
  920. t = rtnl_dereference(ign->tunnels[prio][h]);
  921. while (t) {
  922. /* If dev is in the same netns, it has already
  923. * been added to the list by the previous loop.
  924. */
  925. if (!net_eq(dev_net(t->dev), net))
  926. unregister_netdevice_queue(t->dev,
  927. head);
  928. t = rtnl_dereference(t->next);
  929. }
  930. }
  931. }
  932. }
  933. static int __net_init ip6gre_init_net(struct net *net)
  934. {
  935. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  936. int err;
  937. ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
  938. NET_NAME_UNKNOWN,
  939. ip6gre_tunnel_setup);
  940. if (!ign->fb_tunnel_dev) {
  941. err = -ENOMEM;
  942. goto err_alloc_dev;
  943. }
  944. dev_net_set(ign->fb_tunnel_dev, net);
  945. /* FB netdevice is special: we have one, and only one per netns.
  946. * Allowing to move it to another netns is clearly unsafe.
  947. */
  948. ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
  949. ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
  950. ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
  951. err = register_netdev(ign->fb_tunnel_dev);
  952. if (err)
  953. goto err_reg_dev;
  954. rcu_assign_pointer(ign->tunnels_wc[0],
  955. netdev_priv(ign->fb_tunnel_dev));
  956. return 0;
  957. err_reg_dev:
  958. ip6gre_dev_free(ign->fb_tunnel_dev);
  959. err_alloc_dev:
  960. return err;
  961. }
  962. static void __net_exit ip6gre_exit_net(struct net *net)
  963. {
  964. LIST_HEAD(list);
  965. rtnl_lock();
  966. ip6gre_destroy_tunnels(net, &list);
  967. unregister_netdevice_many(&list);
  968. rtnl_unlock();
  969. }
  970. static struct pernet_operations ip6gre_net_ops = {
  971. .init = ip6gre_init_net,
  972. .exit = ip6gre_exit_net,
  973. .id = &ip6gre_net_id,
  974. .size = sizeof(struct ip6gre_net),
  975. };
  976. static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
  977. {
  978. __be16 flags;
  979. if (!data)
  980. return 0;
  981. flags = 0;
  982. if (data[IFLA_GRE_IFLAGS])
  983. flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
  984. if (data[IFLA_GRE_OFLAGS])
  985. flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
  986. if (flags & (GRE_VERSION|GRE_ROUTING))
  987. return -EINVAL;
  988. return 0;
  989. }
  990. static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[])
  991. {
  992. struct in6_addr daddr;
  993. if (tb[IFLA_ADDRESS]) {
  994. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  995. return -EINVAL;
  996. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  997. return -EADDRNOTAVAIL;
  998. }
  999. if (!data)
  1000. goto out;
  1001. if (data[IFLA_GRE_REMOTE]) {
  1002. daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
  1003. if (ipv6_addr_any(&daddr))
  1004. return -EINVAL;
  1005. }
  1006. out:
  1007. return ip6gre_tunnel_validate(tb, data);
  1008. }
  1009. static void ip6gre_netlink_parms(struct nlattr *data[],
  1010. struct __ip6_tnl_parm *parms)
  1011. {
  1012. memset(parms, 0, sizeof(*parms));
  1013. if (!data)
  1014. return;
  1015. if (data[IFLA_GRE_LINK])
  1016. parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
  1017. if (data[IFLA_GRE_IFLAGS])
  1018. parms->i_flags = gre_flags_to_tnl_flags(
  1019. nla_get_be16(data[IFLA_GRE_IFLAGS]));
  1020. if (data[IFLA_GRE_OFLAGS])
  1021. parms->o_flags = gre_flags_to_tnl_flags(
  1022. nla_get_be16(data[IFLA_GRE_OFLAGS]));
  1023. if (data[IFLA_GRE_IKEY])
  1024. parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
  1025. if (data[IFLA_GRE_OKEY])
  1026. parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
  1027. if (data[IFLA_GRE_LOCAL])
  1028. parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
  1029. if (data[IFLA_GRE_REMOTE])
  1030. parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
  1031. if (data[IFLA_GRE_TTL])
  1032. parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
  1033. if (data[IFLA_GRE_ENCAP_LIMIT])
  1034. parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
  1035. if (data[IFLA_GRE_FLOWINFO])
  1036. parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]);
  1037. if (data[IFLA_GRE_FLAGS])
  1038. parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
  1039. }
  1040. static int ip6gre_tap_init(struct net_device *dev)
  1041. {
  1042. int ret;
  1043. ret = ip6gre_tunnel_init_common(dev);
  1044. if (ret)
  1045. return ret;
  1046. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1047. return 0;
  1048. }
  1049. static const struct net_device_ops ip6gre_tap_netdev_ops = {
  1050. .ndo_init = ip6gre_tap_init,
  1051. .ndo_uninit = ip6gre_tunnel_uninit,
  1052. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1053. .ndo_set_mac_address = eth_mac_addr,
  1054. .ndo_validate_addr = eth_validate_addr,
  1055. .ndo_change_mtu = ip6_tnl_change_mtu,
  1056. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1057. .ndo_get_iflink = ip6_tnl_get_iflink,
  1058. };
  1059. #define GRE6_FEATURES (NETIF_F_SG | \
  1060. NETIF_F_FRAGLIST | \
  1061. NETIF_F_HIGHDMA | \
  1062. NETIF_F_HW_CSUM)
  1063. static void ip6gre_tap_setup(struct net_device *dev)
  1064. {
  1065. ether_setup(dev);
  1066. dev->netdev_ops = &ip6gre_tap_netdev_ops;
  1067. dev->destructor = ip6gre_dev_free;
  1068. dev->features |= NETIF_F_NETNS_LOCAL;
  1069. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  1070. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1071. netif_keep_dst(dev);
  1072. }
  1073. static bool ip6gre_netlink_encap_parms(struct nlattr *data[],
  1074. struct ip_tunnel_encap *ipencap)
  1075. {
  1076. bool ret = false;
  1077. memset(ipencap, 0, sizeof(*ipencap));
  1078. if (!data)
  1079. return ret;
  1080. if (data[IFLA_GRE_ENCAP_TYPE]) {
  1081. ret = true;
  1082. ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
  1083. }
  1084. if (data[IFLA_GRE_ENCAP_FLAGS]) {
  1085. ret = true;
  1086. ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
  1087. }
  1088. if (data[IFLA_GRE_ENCAP_SPORT]) {
  1089. ret = true;
  1090. ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
  1091. }
  1092. if (data[IFLA_GRE_ENCAP_DPORT]) {
  1093. ret = true;
  1094. ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
  1095. }
  1096. return ret;
  1097. }
  1098. static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
  1099. struct nlattr *tb[], struct nlattr *data[])
  1100. {
  1101. struct ip6_tnl *nt;
  1102. struct net *net = dev_net(dev);
  1103. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1104. struct ip_tunnel_encap ipencap;
  1105. int err;
  1106. nt = netdev_priv(dev);
  1107. if (ip6gre_netlink_encap_parms(data, &ipencap)) {
  1108. int err = ip6_tnl_encap_setup(nt, &ipencap);
  1109. if (err < 0)
  1110. return err;
  1111. }
  1112. ip6gre_netlink_parms(data, &nt->parms);
  1113. if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
  1114. return -EEXIST;
  1115. if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
  1116. eth_hw_addr_random(dev);
  1117. nt->dev = dev;
  1118. nt->net = dev_net(dev);
  1119. dev->features |= GRE6_FEATURES;
  1120. dev->hw_features |= GRE6_FEATURES;
  1121. if (!(nt->parms.o_flags & TUNNEL_SEQ)) {
  1122. /* TCP offload with GRE SEQ is not supported, nor
  1123. * can we support 2 levels of outer headers requiring
  1124. * an update.
  1125. */
  1126. if (!(nt->parms.o_flags & TUNNEL_CSUM) ||
  1127. (nt->encap.type == TUNNEL_ENCAP_NONE)) {
  1128. dev->features |= NETIF_F_GSO_SOFTWARE;
  1129. dev->hw_features |= NETIF_F_GSO_SOFTWARE;
  1130. }
  1131. /* Can use a lockless transmit, unless we generate
  1132. * output sequences
  1133. */
  1134. dev->features |= NETIF_F_LLTX;
  1135. }
  1136. err = register_netdevice(dev);
  1137. if (err)
  1138. goto out;
  1139. ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
  1140. if (tb[IFLA_MTU])
  1141. ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
  1142. dev_hold(dev);
  1143. ip6gre_tunnel_link(ign, nt);
  1144. out:
  1145. return err;
  1146. }
  1147. static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
  1148. struct nlattr *data[])
  1149. {
  1150. struct ip6_tnl *t, *nt = netdev_priv(dev);
  1151. struct net *net = nt->net;
  1152. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1153. struct __ip6_tnl_parm p;
  1154. struct ip_tunnel_encap ipencap;
  1155. if (dev == ign->fb_tunnel_dev)
  1156. return -EINVAL;
  1157. if (ip6gre_netlink_encap_parms(data, &ipencap)) {
  1158. int err = ip6_tnl_encap_setup(nt, &ipencap);
  1159. if (err < 0)
  1160. return err;
  1161. }
  1162. ip6gre_netlink_parms(data, &p);
  1163. t = ip6gre_tunnel_locate(net, &p, 0);
  1164. if (t) {
  1165. if (t->dev != dev)
  1166. return -EEXIST;
  1167. } else {
  1168. t = nt;
  1169. }
  1170. ip6gre_tunnel_unlink(ign, t);
  1171. ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
  1172. ip6gre_tunnel_link(ign, t);
  1173. return 0;
  1174. }
  1175. static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
  1176. {
  1177. struct net *net = dev_net(dev);
  1178. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1179. if (dev != ign->fb_tunnel_dev)
  1180. unregister_netdevice_queue(dev, head);
  1181. }
  1182. static size_t ip6gre_get_size(const struct net_device *dev)
  1183. {
  1184. return
  1185. /* IFLA_GRE_LINK */
  1186. nla_total_size(4) +
  1187. /* IFLA_GRE_IFLAGS */
  1188. nla_total_size(2) +
  1189. /* IFLA_GRE_OFLAGS */
  1190. nla_total_size(2) +
  1191. /* IFLA_GRE_IKEY */
  1192. nla_total_size(4) +
  1193. /* IFLA_GRE_OKEY */
  1194. nla_total_size(4) +
  1195. /* IFLA_GRE_LOCAL */
  1196. nla_total_size(sizeof(struct in6_addr)) +
  1197. /* IFLA_GRE_REMOTE */
  1198. nla_total_size(sizeof(struct in6_addr)) +
  1199. /* IFLA_GRE_TTL */
  1200. nla_total_size(1) +
  1201. /* IFLA_GRE_ENCAP_LIMIT */
  1202. nla_total_size(1) +
  1203. /* IFLA_GRE_FLOWINFO */
  1204. nla_total_size(4) +
  1205. /* IFLA_GRE_FLAGS */
  1206. nla_total_size(4) +
  1207. /* IFLA_GRE_ENCAP_TYPE */
  1208. nla_total_size(2) +
  1209. /* IFLA_GRE_ENCAP_FLAGS */
  1210. nla_total_size(2) +
  1211. /* IFLA_GRE_ENCAP_SPORT */
  1212. nla_total_size(2) +
  1213. /* IFLA_GRE_ENCAP_DPORT */
  1214. nla_total_size(2) +
  1215. 0;
  1216. }
  1217. static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1218. {
  1219. struct ip6_tnl *t = netdev_priv(dev);
  1220. struct __ip6_tnl_parm *p = &t->parms;
  1221. if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
  1222. nla_put_be16(skb, IFLA_GRE_IFLAGS,
  1223. gre_tnl_flags_to_gre_flags(p->i_flags)) ||
  1224. nla_put_be16(skb, IFLA_GRE_OFLAGS,
  1225. gre_tnl_flags_to_gre_flags(p->o_flags)) ||
  1226. nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
  1227. nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
  1228. nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
  1229. nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
  1230. nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
  1231. nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
  1232. nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
  1233. nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags))
  1234. goto nla_put_failure;
  1235. if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
  1236. t->encap.type) ||
  1237. nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
  1238. t->encap.sport) ||
  1239. nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
  1240. t->encap.dport) ||
  1241. nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
  1242. t->encap.flags))
  1243. goto nla_put_failure;
  1244. return 0;
  1245. nla_put_failure:
  1246. return -EMSGSIZE;
  1247. }
  1248. static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
  1249. [IFLA_GRE_LINK] = { .type = NLA_U32 },
  1250. [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
  1251. [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
  1252. [IFLA_GRE_IKEY] = { .type = NLA_U32 },
  1253. [IFLA_GRE_OKEY] = { .type = NLA_U32 },
  1254. [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
  1255. [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
  1256. [IFLA_GRE_TTL] = { .type = NLA_U8 },
  1257. [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
  1258. [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 },
  1259. [IFLA_GRE_FLAGS] = { .type = NLA_U32 },
  1260. [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 },
  1261. [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 },
  1262. [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 },
  1263. [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 },
  1264. };
  1265. static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
  1266. .kind = "ip6gre",
  1267. .maxtype = IFLA_GRE_MAX,
  1268. .policy = ip6gre_policy,
  1269. .priv_size = sizeof(struct ip6_tnl),
  1270. .setup = ip6gre_tunnel_setup,
  1271. .validate = ip6gre_tunnel_validate,
  1272. .newlink = ip6gre_newlink,
  1273. .changelink = ip6gre_changelink,
  1274. .dellink = ip6gre_dellink,
  1275. .get_size = ip6gre_get_size,
  1276. .fill_info = ip6gre_fill_info,
  1277. .get_link_net = ip6_tnl_get_link_net,
  1278. };
  1279. static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
  1280. .kind = "ip6gretap",
  1281. .maxtype = IFLA_GRE_MAX,
  1282. .policy = ip6gre_policy,
  1283. .priv_size = sizeof(struct ip6_tnl),
  1284. .setup = ip6gre_tap_setup,
  1285. .validate = ip6gre_tap_validate,
  1286. .newlink = ip6gre_newlink,
  1287. .changelink = ip6gre_changelink,
  1288. .get_size = ip6gre_get_size,
  1289. .fill_info = ip6gre_fill_info,
  1290. .get_link_net = ip6_tnl_get_link_net,
  1291. };
  1292. /*
  1293. * And now the modules code and kernel interface.
  1294. */
  1295. static int __init ip6gre_init(void)
  1296. {
  1297. int err;
  1298. pr_info("GRE over IPv6 tunneling driver\n");
  1299. err = register_pernet_device(&ip6gre_net_ops);
  1300. if (err < 0)
  1301. return err;
  1302. err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1303. if (err < 0) {
  1304. pr_info("%s: can't add protocol\n", __func__);
  1305. goto add_proto_failed;
  1306. }
  1307. err = rtnl_link_register(&ip6gre_link_ops);
  1308. if (err < 0)
  1309. goto rtnl_link_failed;
  1310. err = rtnl_link_register(&ip6gre_tap_ops);
  1311. if (err < 0)
  1312. goto tap_ops_failed;
  1313. out:
  1314. return err;
  1315. tap_ops_failed:
  1316. rtnl_link_unregister(&ip6gre_link_ops);
  1317. rtnl_link_failed:
  1318. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1319. add_proto_failed:
  1320. unregister_pernet_device(&ip6gre_net_ops);
  1321. goto out;
  1322. }
  1323. static void __exit ip6gre_fini(void)
  1324. {
  1325. rtnl_link_unregister(&ip6gre_tap_ops);
  1326. rtnl_link_unregister(&ip6gre_link_ops);
  1327. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1328. unregister_pernet_device(&ip6gre_net_ops);
  1329. }
  1330. module_init(ip6gre_init);
  1331. module_exit(ip6gre_fini);
  1332. MODULE_LICENSE("GPL");
  1333. MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
  1334. MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
  1335. MODULE_ALIAS_RTNL_LINK("ip6gre");
  1336. MODULE_ALIAS_RTNL_LINK("ip6gretap");
  1337. MODULE_ALIAS_NETDEV("ip6gre0");