ip6_gre.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402
  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. #include <net/erspan.h>
  55. #include <net/dst_metadata.h>
  56. static bool log_ecn_error = true;
  57. module_param(log_ecn_error, bool, 0644);
  58. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  59. #define IP6_GRE_HASH_SIZE_SHIFT 5
  60. #define IP6_GRE_HASH_SIZE (1 << IP6_GRE_HASH_SIZE_SHIFT)
  61. static unsigned int ip6gre_net_id __read_mostly;
  62. struct ip6gre_net {
  63. struct ip6_tnl __rcu *tunnels[4][IP6_GRE_HASH_SIZE];
  64. struct ip6_tnl __rcu *collect_md_tun;
  65. struct ip6_tnl __rcu *collect_md_tun_erspan;
  66. struct net_device *fb_tunnel_dev;
  67. };
  68. static struct rtnl_link_ops ip6gre_link_ops __read_mostly;
  69. static struct rtnl_link_ops ip6gre_tap_ops __read_mostly;
  70. static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly;
  71. static int ip6gre_tunnel_init(struct net_device *dev);
  72. static void ip6gre_tunnel_setup(struct net_device *dev);
  73. static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t);
  74. static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu);
  75. static void ip6erspan_tnl_link_config(struct ip6_tnl *t, int set_mtu);
  76. /* Tunnel hash table */
  77. /*
  78. 4 hash tables:
  79. 3: (remote,local)
  80. 2: (remote,*)
  81. 1: (*,local)
  82. 0: (*,*)
  83. We require exact key match i.e. if a key is present in packet
  84. it will match only tunnel with the same key; if it is not present,
  85. it will match only keyless tunnel.
  86. All keysless packets, if not matched configured keyless tunnels
  87. will match fallback tunnel.
  88. */
  89. #define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(IP6_GRE_HASH_SIZE - 1))
  90. static u32 HASH_ADDR(const struct in6_addr *addr)
  91. {
  92. u32 hash = ipv6_addr_hash(addr);
  93. return hash_32(hash, IP6_GRE_HASH_SIZE_SHIFT);
  94. }
  95. #define tunnels_r_l tunnels[3]
  96. #define tunnels_r tunnels[2]
  97. #define tunnels_l tunnels[1]
  98. #define tunnels_wc tunnels[0]
  99. /* Given src, dst and key, find appropriate for input tunnel. */
  100. static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev,
  101. const struct in6_addr *remote, const struct in6_addr *local,
  102. __be32 key, __be16 gre_proto)
  103. {
  104. struct net *net = dev_net(dev);
  105. int link = dev->ifindex;
  106. unsigned int h0 = HASH_ADDR(remote);
  107. unsigned int h1 = HASH_KEY(key);
  108. struct ip6_tnl *t, *cand = NULL;
  109. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  110. int dev_type = (gre_proto == htons(ETH_P_TEB) ||
  111. gre_proto == htons(ETH_P_ERSPAN) ||
  112. gre_proto == htons(ETH_P_ERSPAN2)) ?
  113. ARPHRD_ETHER : ARPHRD_IP6GRE;
  114. int score, cand_score = 4;
  115. for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) {
  116. if (!ipv6_addr_equal(local, &t->parms.laddr) ||
  117. !ipv6_addr_equal(remote, &t->parms.raddr) ||
  118. key != t->parms.i_key ||
  119. !(t->dev->flags & IFF_UP))
  120. continue;
  121. if (t->dev->type != ARPHRD_IP6GRE &&
  122. t->dev->type != dev_type)
  123. continue;
  124. score = 0;
  125. if (t->parms.link != link)
  126. score |= 1;
  127. if (t->dev->type != dev_type)
  128. score |= 2;
  129. if (score == 0)
  130. return t;
  131. if (score < cand_score) {
  132. cand = t;
  133. cand_score = score;
  134. }
  135. }
  136. for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) {
  137. if (!ipv6_addr_equal(remote, &t->parms.raddr) ||
  138. key != t->parms.i_key ||
  139. !(t->dev->flags & IFF_UP))
  140. continue;
  141. if (t->dev->type != ARPHRD_IP6GRE &&
  142. t->dev->type != dev_type)
  143. continue;
  144. score = 0;
  145. if (t->parms.link != link)
  146. score |= 1;
  147. if (t->dev->type != dev_type)
  148. score |= 2;
  149. if (score == 0)
  150. return t;
  151. if (score < cand_score) {
  152. cand = t;
  153. cand_score = score;
  154. }
  155. }
  156. for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) {
  157. if ((!ipv6_addr_equal(local, &t->parms.laddr) &&
  158. (!ipv6_addr_equal(local, &t->parms.raddr) ||
  159. !ipv6_addr_is_multicast(local))) ||
  160. key != t->parms.i_key ||
  161. !(t->dev->flags & IFF_UP))
  162. continue;
  163. if (t->dev->type != ARPHRD_IP6GRE &&
  164. t->dev->type != dev_type)
  165. continue;
  166. score = 0;
  167. if (t->parms.link != link)
  168. score |= 1;
  169. if (t->dev->type != dev_type)
  170. score |= 2;
  171. if (score == 0)
  172. return t;
  173. if (score < cand_score) {
  174. cand = t;
  175. cand_score = score;
  176. }
  177. }
  178. for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) {
  179. if (t->parms.i_key != key ||
  180. !(t->dev->flags & IFF_UP))
  181. continue;
  182. if (t->dev->type != ARPHRD_IP6GRE &&
  183. t->dev->type != dev_type)
  184. continue;
  185. score = 0;
  186. if (t->parms.link != link)
  187. score |= 1;
  188. if (t->dev->type != dev_type)
  189. score |= 2;
  190. if (score == 0)
  191. return t;
  192. if (score < cand_score) {
  193. cand = t;
  194. cand_score = score;
  195. }
  196. }
  197. if (cand)
  198. return cand;
  199. if (gre_proto == htons(ETH_P_ERSPAN) ||
  200. gre_proto == htons(ETH_P_ERSPAN2))
  201. t = rcu_dereference(ign->collect_md_tun_erspan);
  202. else
  203. t = rcu_dereference(ign->collect_md_tun);
  204. if (t && t->dev->flags & IFF_UP)
  205. return t;
  206. dev = ign->fb_tunnel_dev;
  207. if (dev && dev->flags & IFF_UP)
  208. return netdev_priv(dev);
  209. return NULL;
  210. }
  211. static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign,
  212. const struct __ip6_tnl_parm *p)
  213. {
  214. const struct in6_addr *remote = &p->raddr;
  215. const struct in6_addr *local = &p->laddr;
  216. unsigned int h = HASH_KEY(p->i_key);
  217. int prio = 0;
  218. if (!ipv6_addr_any(local))
  219. prio |= 1;
  220. if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) {
  221. prio |= 2;
  222. h ^= HASH_ADDR(remote);
  223. }
  224. return &ign->tunnels[prio][h];
  225. }
  226. static void ip6gre_tunnel_link_md(struct ip6gre_net *ign, struct ip6_tnl *t)
  227. {
  228. if (t->parms.collect_md)
  229. rcu_assign_pointer(ign->collect_md_tun, t);
  230. }
  231. static void ip6erspan_tunnel_link_md(struct ip6gre_net *ign, struct ip6_tnl *t)
  232. {
  233. if (t->parms.collect_md)
  234. rcu_assign_pointer(ign->collect_md_tun_erspan, t);
  235. }
  236. static void ip6gre_tunnel_unlink_md(struct ip6gre_net *ign, struct ip6_tnl *t)
  237. {
  238. if (t->parms.collect_md)
  239. rcu_assign_pointer(ign->collect_md_tun, NULL);
  240. }
  241. static void ip6erspan_tunnel_unlink_md(struct ip6gre_net *ign,
  242. struct ip6_tnl *t)
  243. {
  244. if (t->parms.collect_md)
  245. rcu_assign_pointer(ign->collect_md_tun_erspan, NULL);
  246. }
  247. static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign,
  248. const struct ip6_tnl *t)
  249. {
  250. return __ip6gre_bucket(ign, &t->parms);
  251. }
  252. static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t)
  253. {
  254. struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t);
  255. rcu_assign_pointer(t->next, rtnl_dereference(*tp));
  256. rcu_assign_pointer(*tp, t);
  257. }
  258. static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t)
  259. {
  260. struct ip6_tnl __rcu **tp;
  261. struct ip6_tnl *iter;
  262. for (tp = ip6gre_bucket(ign, t);
  263. (iter = rtnl_dereference(*tp)) != NULL;
  264. tp = &iter->next) {
  265. if (t == iter) {
  266. rcu_assign_pointer(*tp, t->next);
  267. break;
  268. }
  269. }
  270. }
  271. static struct ip6_tnl *ip6gre_tunnel_find(struct net *net,
  272. const struct __ip6_tnl_parm *parms,
  273. int type)
  274. {
  275. const struct in6_addr *remote = &parms->raddr;
  276. const struct in6_addr *local = &parms->laddr;
  277. __be32 key = parms->i_key;
  278. int link = parms->link;
  279. struct ip6_tnl *t;
  280. struct ip6_tnl __rcu **tp;
  281. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  282. for (tp = __ip6gre_bucket(ign, parms);
  283. (t = rtnl_dereference(*tp)) != NULL;
  284. tp = &t->next)
  285. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  286. ipv6_addr_equal(remote, &t->parms.raddr) &&
  287. key == t->parms.i_key &&
  288. link == t->parms.link &&
  289. type == t->dev->type)
  290. break;
  291. return t;
  292. }
  293. static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net,
  294. const struct __ip6_tnl_parm *parms, int create)
  295. {
  296. struct ip6_tnl *t, *nt;
  297. struct net_device *dev;
  298. char name[IFNAMSIZ];
  299. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  300. t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE);
  301. if (t && create)
  302. return NULL;
  303. if (t || !create)
  304. return t;
  305. if (parms->name[0]) {
  306. if (!dev_valid_name(parms->name))
  307. return NULL;
  308. strlcpy(name, parms->name, IFNAMSIZ);
  309. } else {
  310. strcpy(name, "ip6gre%d");
  311. }
  312. dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
  313. ip6gre_tunnel_setup);
  314. if (!dev)
  315. return NULL;
  316. dev_net_set(dev, net);
  317. nt = netdev_priv(dev);
  318. nt->parms = *parms;
  319. dev->rtnl_link_ops = &ip6gre_link_ops;
  320. nt->dev = dev;
  321. nt->net = dev_net(dev);
  322. if (register_netdevice(dev) < 0)
  323. goto failed_free;
  324. ip6gre_tnl_link_config(nt, 1);
  325. /* Can use a lockless transmit, unless we generate output sequences */
  326. if (!(nt->parms.o_flags & TUNNEL_SEQ))
  327. dev->features |= NETIF_F_LLTX;
  328. dev_hold(dev);
  329. ip6gre_tunnel_link(ign, nt);
  330. return nt;
  331. failed_free:
  332. free_netdev(dev);
  333. return NULL;
  334. }
  335. static void ip6erspan_tunnel_uninit(struct net_device *dev)
  336. {
  337. struct ip6_tnl *t = netdev_priv(dev);
  338. struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
  339. ip6erspan_tunnel_unlink_md(ign, t);
  340. ip6gre_tunnel_unlink(ign, t);
  341. dst_cache_reset(&t->dst_cache);
  342. dev_put(dev);
  343. }
  344. static void ip6gre_tunnel_uninit(struct net_device *dev)
  345. {
  346. struct ip6_tnl *t = netdev_priv(dev);
  347. struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
  348. ip6gre_tunnel_unlink_md(ign, t);
  349. ip6gre_tunnel_unlink(ign, t);
  350. dst_cache_reset(&t->dst_cache);
  351. dev_put(dev);
  352. }
  353. static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  354. u8 type, u8 code, int offset, __be32 info)
  355. {
  356. struct net *net = dev_net(skb->dev);
  357. const struct gre_base_hdr *greh;
  358. const struct ipv6hdr *ipv6h;
  359. int grehlen = sizeof(*greh);
  360. struct ip6_tnl *t;
  361. int key_off = 0;
  362. __be16 flags;
  363. __be32 key;
  364. if (!pskb_may_pull(skb, offset + grehlen))
  365. return;
  366. greh = (const struct gre_base_hdr *)(skb->data + offset);
  367. flags = greh->flags;
  368. if (flags & (GRE_VERSION | GRE_ROUTING))
  369. return;
  370. if (flags & GRE_CSUM)
  371. grehlen += 4;
  372. if (flags & GRE_KEY) {
  373. key_off = grehlen + offset;
  374. grehlen += 4;
  375. }
  376. if (!pskb_may_pull(skb, offset + grehlen))
  377. return;
  378. ipv6h = (const struct ipv6hdr *)skb->data;
  379. greh = (const struct gre_base_hdr *)(skb->data + offset);
  380. key = key_off ? *(__be32 *)(skb->data + key_off) : 0;
  381. t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
  382. key, greh->protocol);
  383. if (!t)
  384. return;
  385. switch (type) {
  386. struct ipv6_tlv_tnl_enc_lim *tel;
  387. __u32 teli;
  388. case ICMPV6_DEST_UNREACH:
  389. net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
  390. t->parms.name);
  391. if (code != ICMPV6_PORT_UNREACH)
  392. break;
  393. return;
  394. case ICMPV6_TIME_EXCEED:
  395. if (code == ICMPV6_EXC_HOPLIMIT) {
  396. net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
  397. t->parms.name);
  398. break;
  399. }
  400. return;
  401. case ICMPV6_PARAMPROB:
  402. teli = 0;
  403. if (code == ICMPV6_HDR_FIELD)
  404. teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
  405. if (teli && teli == be32_to_cpu(info) - 2) {
  406. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
  407. if (tel->encap_limit == 0) {
  408. net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
  409. t->parms.name);
  410. }
  411. } else {
  412. net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
  413. t->parms.name);
  414. }
  415. return;
  416. case ICMPV6_PKT_TOOBIG:
  417. ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
  418. return;
  419. case NDISC_REDIRECT:
  420. ip6_redirect(skb, net, skb->dev->ifindex, 0,
  421. sock_net_uid(net, NULL));
  422. return;
  423. }
  424. if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO))
  425. t->err_count++;
  426. else
  427. t->err_count = 1;
  428. t->err_time = jiffies;
  429. }
  430. static int ip6gre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
  431. {
  432. const struct ipv6hdr *ipv6h;
  433. struct ip6_tnl *tunnel;
  434. ipv6h = ipv6_hdr(skb);
  435. tunnel = ip6gre_tunnel_lookup(skb->dev,
  436. &ipv6h->saddr, &ipv6h->daddr, tpi->key,
  437. tpi->proto);
  438. if (tunnel) {
  439. if (tunnel->parms.collect_md) {
  440. struct metadata_dst *tun_dst;
  441. __be64 tun_id;
  442. __be16 flags;
  443. flags = tpi->flags;
  444. tun_id = key32_to_tunnel_id(tpi->key);
  445. tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id, 0);
  446. if (!tun_dst)
  447. return PACKET_REJECT;
  448. ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
  449. } else {
  450. ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
  451. }
  452. return PACKET_RCVD;
  453. }
  454. return PACKET_REJECT;
  455. }
  456. static int ip6erspan_rcv(struct sk_buff *skb, struct tnl_ptk_info *tpi,
  457. int gre_hdr_len)
  458. {
  459. struct erspan_base_hdr *ershdr;
  460. const struct ipv6hdr *ipv6h;
  461. struct erspan_md2 *md2;
  462. struct ip6_tnl *tunnel;
  463. u8 ver;
  464. ipv6h = ipv6_hdr(skb);
  465. ershdr = (struct erspan_base_hdr *)skb->data;
  466. ver = ershdr->ver;
  467. tunnel = ip6gre_tunnel_lookup(skb->dev,
  468. &ipv6h->saddr, &ipv6h->daddr, tpi->key,
  469. tpi->proto);
  470. if (tunnel) {
  471. int len = erspan_hdr_len(ver);
  472. if (unlikely(!pskb_may_pull(skb, len)))
  473. return PACKET_REJECT;
  474. if (__iptunnel_pull_header(skb, len,
  475. htons(ETH_P_TEB),
  476. false, false) < 0)
  477. return PACKET_REJECT;
  478. if (tunnel->parms.collect_md) {
  479. struct erspan_metadata *pkt_md, *md;
  480. struct metadata_dst *tun_dst;
  481. struct ip_tunnel_info *info;
  482. unsigned char *gh;
  483. __be64 tun_id;
  484. __be16 flags;
  485. tpi->flags |= TUNNEL_KEY;
  486. flags = tpi->flags;
  487. tun_id = key32_to_tunnel_id(tpi->key);
  488. tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id,
  489. sizeof(*md));
  490. if (!tun_dst)
  491. return PACKET_REJECT;
  492. /* skb can be uncloned in __iptunnel_pull_header, so
  493. * old pkt_md is no longer valid and we need to reset
  494. * it
  495. */
  496. gh = skb_network_header(skb) +
  497. skb_network_header_len(skb);
  498. pkt_md = (struct erspan_metadata *)(gh + gre_hdr_len +
  499. sizeof(*ershdr));
  500. info = &tun_dst->u.tun_info;
  501. md = ip_tunnel_info_opts(info);
  502. md->version = ver;
  503. md2 = &md->u.md2;
  504. memcpy(md2, pkt_md, ver == 1 ? ERSPAN_V1_MDSIZE :
  505. ERSPAN_V2_MDSIZE);
  506. info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
  507. info->options_len = sizeof(*md);
  508. ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
  509. } else {
  510. ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
  511. }
  512. return PACKET_RCVD;
  513. }
  514. return PACKET_REJECT;
  515. }
  516. static int gre_rcv(struct sk_buff *skb)
  517. {
  518. struct tnl_ptk_info tpi;
  519. bool csum_err = false;
  520. int hdr_len;
  521. hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IPV6), 0);
  522. if (hdr_len < 0)
  523. goto drop;
  524. if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false))
  525. goto drop;
  526. if (unlikely(tpi.proto == htons(ETH_P_ERSPAN) ||
  527. tpi.proto == htons(ETH_P_ERSPAN2))) {
  528. if (ip6erspan_rcv(skb, &tpi, hdr_len) == PACKET_RCVD)
  529. return 0;
  530. goto out;
  531. }
  532. if (ip6gre_rcv(skb, &tpi) == PACKET_RCVD)
  533. return 0;
  534. out:
  535. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  536. drop:
  537. kfree_skb(skb);
  538. return 0;
  539. }
  540. static int gre_handle_offloads(struct sk_buff *skb, bool csum)
  541. {
  542. return iptunnel_handle_offloads(skb,
  543. csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
  544. }
  545. static void prepare_ip6gre_xmit_ipv4(struct sk_buff *skb,
  546. struct net_device *dev,
  547. struct flowi6 *fl6, __u8 *dsfield,
  548. int *encap_limit)
  549. {
  550. const struct iphdr *iph = ip_hdr(skb);
  551. struct ip6_tnl *t = netdev_priv(dev);
  552. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  553. *encap_limit = t->parms.encap_limit;
  554. memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
  555. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  556. *dsfield = ipv4_get_dsfield(iph);
  557. else
  558. *dsfield = ip6_tclass(t->parms.flowinfo);
  559. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  560. fl6->flowi6_mark = skb->mark;
  561. else
  562. fl6->flowi6_mark = t->parms.fwmark;
  563. fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
  564. }
  565. static int prepare_ip6gre_xmit_ipv6(struct sk_buff *skb,
  566. struct net_device *dev,
  567. struct flowi6 *fl6, __u8 *dsfield,
  568. int *encap_limit)
  569. {
  570. struct ipv6hdr *ipv6h;
  571. struct ip6_tnl *t = netdev_priv(dev);
  572. __u16 offset;
  573. offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
  574. /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
  575. ipv6h = ipv6_hdr(skb);
  576. if (offset > 0) {
  577. struct ipv6_tlv_tnl_enc_lim *tel;
  578. tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
  579. if (tel->encap_limit == 0) {
  580. icmpv6_send(skb, ICMPV6_PARAMPROB,
  581. ICMPV6_HDR_FIELD, offset + 2);
  582. return -1;
  583. }
  584. *encap_limit = tel->encap_limit - 1;
  585. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
  586. *encap_limit = t->parms.encap_limit;
  587. }
  588. memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
  589. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  590. *dsfield = ipv6_get_dsfield(ipv6h);
  591. else
  592. *dsfield = ip6_tclass(t->parms.flowinfo);
  593. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
  594. fl6->flowlabel |= ip6_flowlabel(ipv6h);
  595. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  596. fl6->flowi6_mark = skb->mark;
  597. else
  598. fl6->flowi6_mark = t->parms.fwmark;
  599. fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
  600. return 0;
  601. }
  602. static netdev_tx_t __gre6_xmit(struct sk_buff *skb,
  603. struct net_device *dev, __u8 dsfield,
  604. struct flowi6 *fl6, int encap_limit,
  605. __u32 *pmtu, __be16 proto)
  606. {
  607. struct ip6_tnl *tunnel = netdev_priv(dev);
  608. __be16 protocol;
  609. if (dev->type == ARPHRD_ETHER)
  610. IPCB(skb)->flags = 0;
  611. if (dev->header_ops && dev->type == ARPHRD_IP6GRE)
  612. fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr;
  613. else
  614. fl6->daddr = tunnel->parms.raddr;
  615. if (skb_cow_head(skb, dev->needed_headroom ?: tunnel->hlen))
  616. return -ENOMEM;
  617. /* Push GRE header. */
  618. protocol = (dev->type == ARPHRD_ETHER) ? htons(ETH_P_TEB) : proto;
  619. if (tunnel->parms.collect_md) {
  620. struct ip_tunnel_info *tun_info;
  621. const struct ip_tunnel_key *key;
  622. __be16 flags;
  623. tun_info = skb_tunnel_info(skb);
  624. if (unlikely(!tun_info ||
  625. !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
  626. ip_tunnel_info_af(tun_info) != AF_INET6))
  627. return -EINVAL;
  628. key = &tun_info->key;
  629. memset(fl6, 0, sizeof(*fl6));
  630. fl6->flowi6_proto = IPPROTO_GRE;
  631. fl6->daddr = key->u.ipv6.dst;
  632. fl6->flowlabel = key->label;
  633. fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
  634. dsfield = key->tos;
  635. flags = key->tun_flags &
  636. (TUNNEL_CSUM | TUNNEL_KEY | TUNNEL_SEQ);
  637. tunnel->tun_hlen = gre_calc_hlen(flags);
  638. gre_build_header(skb, tunnel->tun_hlen,
  639. flags, protocol,
  640. tunnel_id_to_key32(tun_info->key.tun_id),
  641. (flags & TUNNEL_SEQ) ? htonl(tunnel->o_seqno++)
  642. : 0);
  643. } else {
  644. if (tunnel->parms.o_flags & TUNNEL_SEQ)
  645. tunnel->o_seqno++;
  646. gre_build_header(skb, tunnel->tun_hlen, tunnel->parms.o_flags,
  647. protocol, tunnel->parms.o_key,
  648. htonl(tunnel->o_seqno));
  649. }
  650. return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu,
  651. NEXTHDR_GRE);
  652. }
  653. static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
  654. {
  655. struct ip6_tnl *t = netdev_priv(dev);
  656. int encap_limit = -1;
  657. struct flowi6 fl6;
  658. __u8 dsfield = 0;
  659. __u32 mtu;
  660. int err;
  661. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  662. if (!t->parms.collect_md)
  663. prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
  664. &dsfield, &encap_limit);
  665. err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
  666. if (err)
  667. return -1;
  668. err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
  669. skb->protocol);
  670. if (err != 0) {
  671. /* XXX: send ICMP error even if DF is not set. */
  672. if (err == -EMSGSIZE)
  673. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  674. htonl(mtu));
  675. return -1;
  676. }
  677. return 0;
  678. }
  679. static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
  680. {
  681. struct ip6_tnl *t = netdev_priv(dev);
  682. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  683. int encap_limit = -1;
  684. struct flowi6 fl6;
  685. __u8 dsfield = 0;
  686. __u32 mtu;
  687. int err;
  688. if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
  689. return -1;
  690. if (!t->parms.collect_md &&
  691. prepare_ip6gre_xmit_ipv6(skb, dev, &fl6, &dsfield, &encap_limit))
  692. return -1;
  693. if (gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)))
  694. return -1;
  695. err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit,
  696. &mtu, skb->protocol);
  697. if (err != 0) {
  698. if (err == -EMSGSIZE)
  699. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  700. return -1;
  701. }
  702. return 0;
  703. }
  704. /**
  705. * ip6gre_tnl_addr_conflict - compare packet addresses to tunnel's own
  706. * @t: the outgoing tunnel device
  707. * @hdr: IPv6 header from the incoming packet
  708. *
  709. * Description:
  710. * Avoid trivial tunneling loop by checking that tunnel exit-point
  711. * doesn't match source of incoming packet.
  712. *
  713. * Return:
  714. * 1 if conflict,
  715. * 0 else
  716. **/
  717. static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
  718. const struct ipv6hdr *hdr)
  719. {
  720. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  721. }
  722. static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
  723. {
  724. struct ip6_tnl *t = netdev_priv(dev);
  725. int encap_limit = -1;
  726. struct flowi6 fl6;
  727. __u32 mtu;
  728. int err;
  729. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  730. encap_limit = t->parms.encap_limit;
  731. if (!t->parms.collect_md)
  732. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  733. err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
  734. if (err)
  735. return err;
  736. err = __gre6_xmit(skb, dev, 0, &fl6, encap_limit, &mtu, skb->protocol);
  737. return err;
  738. }
  739. static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
  740. struct net_device *dev)
  741. {
  742. struct ip6_tnl *t = netdev_priv(dev);
  743. struct net_device_stats *stats = &t->dev->stats;
  744. int ret;
  745. if (!pskb_inet_may_pull(skb))
  746. goto tx_err;
  747. if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
  748. goto tx_err;
  749. switch (skb->protocol) {
  750. case htons(ETH_P_IP):
  751. ret = ip6gre_xmit_ipv4(skb, dev);
  752. break;
  753. case htons(ETH_P_IPV6):
  754. ret = ip6gre_xmit_ipv6(skb, dev);
  755. break;
  756. default:
  757. ret = ip6gre_xmit_other(skb, dev);
  758. break;
  759. }
  760. if (ret < 0)
  761. goto tx_err;
  762. return NETDEV_TX_OK;
  763. tx_err:
  764. stats->tx_errors++;
  765. stats->tx_dropped++;
  766. kfree_skb(skb);
  767. return NETDEV_TX_OK;
  768. }
  769. static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb,
  770. struct net_device *dev)
  771. {
  772. struct ip6_tnl *t = netdev_priv(dev);
  773. struct dst_entry *dst = skb_dst(skb);
  774. struct net_device_stats *stats;
  775. bool truncate = false;
  776. int encap_limit = -1;
  777. __u8 dsfield = false;
  778. struct flowi6 fl6;
  779. int err = -EINVAL;
  780. __be16 proto;
  781. __u32 mtu;
  782. int nhoff;
  783. int thoff;
  784. if (!pskb_inet_may_pull(skb))
  785. goto tx_err;
  786. if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
  787. goto tx_err;
  788. if (gre_handle_offloads(skb, false))
  789. goto tx_err;
  790. if (skb->len > dev->mtu + dev->hard_header_len) {
  791. pskb_trim(skb, dev->mtu + dev->hard_header_len);
  792. truncate = true;
  793. }
  794. nhoff = skb_network_header(skb) - skb_mac_header(skb);
  795. if (skb->protocol == htons(ETH_P_IP) &&
  796. (ntohs(ip_hdr(skb)->tot_len) > skb->len - nhoff))
  797. truncate = true;
  798. thoff = skb_transport_header(skb) - skb_mac_header(skb);
  799. if (skb->protocol == htons(ETH_P_IPV6) &&
  800. (ntohs(ipv6_hdr(skb)->payload_len) > skb->len - thoff))
  801. truncate = true;
  802. if (skb_cow_head(skb, dev->needed_headroom ?: t->hlen))
  803. goto tx_err;
  804. t->parms.o_flags &= ~TUNNEL_KEY;
  805. IPCB(skb)->flags = 0;
  806. /* For collect_md mode, derive fl6 from the tunnel key,
  807. * for native mode, call prepare_ip6gre_xmit_{ipv4,ipv6}.
  808. */
  809. if (t->parms.collect_md) {
  810. struct ip_tunnel_info *tun_info;
  811. const struct ip_tunnel_key *key;
  812. struct erspan_metadata *md;
  813. __be32 tun_id;
  814. tun_info = skb_tunnel_info(skb);
  815. if (unlikely(!tun_info ||
  816. !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
  817. ip_tunnel_info_af(tun_info) != AF_INET6))
  818. goto tx_err;
  819. key = &tun_info->key;
  820. memset(&fl6, 0, sizeof(fl6));
  821. fl6.flowi6_proto = IPPROTO_GRE;
  822. fl6.daddr = key->u.ipv6.dst;
  823. fl6.flowlabel = key->label;
  824. fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
  825. dsfield = key->tos;
  826. if (!(tun_info->key.tun_flags & TUNNEL_ERSPAN_OPT))
  827. goto tx_err;
  828. md = ip_tunnel_info_opts(tun_info);
  829. if (!md)
  830. goto tx_err;
  831. tun_id = tunnel_id_to_key32(key->tun_id);
  832. if (md->version == 1) {
  833. erspan_build_header(skb,
  834. ntohl(tun_id),
  835. ntohl(md->u.index), truncate,
  836. false);
  837. } else if (md->version == 2) {
  838. erspan_build_header_v2(skb,
  839. ntohl(tun_id),
  840. md->u.md2.dir,
  841. get_hwid(&md->u.md2),
  842. truncate, false);
  843. } else {
  844. goto tx_err;
  845. }
  846. } else {
  847. switch (skb->protocol) {
  848. case htons(ETH_P_IP):
  849. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  850. prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
  851. &dsfield, &encap_limit);
  852. break;
  853. case htons(ETH_P_IPV6):
  854. if (ipv6_addr_equal(&t->parms.raddr, &ipv6_hdr(skb)->saddr))
  855. goto tx_err;
  856. if (prepare_ip6gre_xmit_ipv6(skb, dev, &fl6,
  857. &dsfield, &encap_limit))
  858. goto tx_err;
  859. break;
  860. default:
  861. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  862. break;
  863. }
  864. if (t->parms.erspan_ver == 1)
  865. erspan_build_header(skb, ntohl(t->parms.o_key),
  866. t->parms.index,
  867. truncate, false);
  868. else if (t->parms.erspan_ver == 2)
  869. erspan_build_header_v2(skb, ntohl(t->parms.o_key),
  870. t->parms.dir,
  871. t->parms.hwid,
  872. truncate, false);
  873. else
  874. goto tx_err;
  875. fl6.daddr = t->parms.raddr;
  876. }
  877. /* Push GRE header. */
  878. proto = (t->parms.erspan_ver == 1) ? htons(ETH_P_ERSPAN)
  879. : htons(ETH_P_ERSPAN2);
  880. gre_build_header(skb, 8, TUNNEL_SEQ, proto, 0, htonl(t->o_seqno++));
  881. /* TooBig packet may have updated dst->dev's mtu */
  882. if (!t->parms.collect_md && dst && dst_mtu(dst) > dst->dev->mtu)
  883. dst->ops->update_pmtu(dst, NULL, skb, dst->dev->mtu);
  884. err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
  885. NEXTHDR_GRE);
  886. if (err != 0) {
  887. /* XXX: send ICMP error even if DF is not set. */
  888. if (err == -EMSGSIZE) {
  889. if (skb->protocol == htons(ETH_P_IP))
  890. icmp_send(skb, ICMP_DEST_UNREACH,
  891. ICMP_FRAG_NEEDED, htonl(mtu));
  892. else
  893. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  894. }
  895. goto tx_err;
  896. }
  897. return NETDEV_TX_OK;
  898. tx_err:
  899. stats = &t->dev->stats;
  900. stats->tx_errors++;
  901. stats->tx_dropped++;
  902. kfree_skb(skb);
  903. return NETDEV_TX_OK;
  904. }
  905. static void ip6gre_tnl_link_config_common(struct ip6_tnl *t)
  906. {
  907. struct net_device *dev = t->dev;
  908. struct __ip6_tnl_parm *p = &t->parms;
  909. struct flowi6 *fl6 = &t->fl.u.ip6;
  910. if (dev->type != ARPHRD_ETHER) {
  911. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  912. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  913. }
  914. /* Set up flowi template */
  915. fl6->saddr = p->laddr;
  916. fl6->daddr = p->raddr;
  917. fl6->flowi6_oif = p->link;
  918. fl6->flowlabel = 0;
  919. fl6->flowi6_proto = IPPROTO_GRE;
  920. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  921. fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  922. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  923. fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  924. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
  925. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  926. if (p->flags&IP6_TNL_F_CAP_XMIT &&
  927. p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
  928. dev->flags |= IFF_POINTOPOINT;
  929. else
  930. dev->flags &= ~IFF_POINTOPOINT;
  931. }
  932. static void ip6gre_tnl_link_config_route(struct ip6_tnl *t, int set_mtu,
  933. int t_hlen)
  934. {
  935. const struct __ip6_tnl_parm *p = &t->parms;
  936. struct net_device *dev = t->dev;
  937. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  938. int strict = (ipv6_addr_type(&p->raddr) &
  939. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  940. struct rt6_info *rt = rt6_lookup(t->net,
  941. &p->raddr, &p->laddr,
  942. p->link, NULL, strict);
  943. if (!rt)
  944. return;
  945. if (rt->dst.dev) {
  946. dev->needed_headroom = rt->dst.dev->hard_header_len +
  947. t_hlen;
  948. if (set_mtu) {
  949. dev->mtu = rt->dst.dev->mtu - t_hlen;
  950. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  951. dev->mtu -= 8;
  952. if (dev->type == ARPHRD_ETHER)
  953. dev->mtu -= ETH_HLEN;
  954. if (dev->mtu < IPV6_MIN_MTU)
  955. dev->mtu = IPV6_MIN_MTU;
  956. }
  957. }
  958. ip6_rt_put(rt);
  959. }
  960. }
  961. static int ip6gre_calc_hlen(struct ip6_tnl *tunnel)
  962. {
  963. int t_hlen;
  964. tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
  965. tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
  966. t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
  967. tunnel->dev->needed_headroom = LL_MAX_HEADER + t_hlen;
  968. return t_hlen;
  969. }
  970. static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
  971. {
  972. ip6gre_tnl_link_config_common(t);
  973. ip6gre_tnl_link_config_route(t, set_mtu, ip6gre_calc_hlen(t));
  974. }
  975. static void ip6gre_tnl_copy_tnl_parm(struct ip6_tnl *t,
  976. const struct __ip6_tnl_parm *p)
  977. {
  978. t->parms.laddr = p->laddr;
  979. t->parms.raddr = p->raddr;
  980. t->parms.flags = p->flags;
  981. t->parms.hop_limit = p->hop_limit;
  982. t->parms.encap_limit = p->encap_limit;
  983. t->parms.flowinfo = p->flowinfo;
  984. t->parms.link = p->link;
  985. t->parms.proto = p->proto;
  986. t->parms.i_key = p->i_key;
  987. t->parms.o_key = p->o_key;
  988. t->parms.i_flags = p->i_flags;
  989. t->parms.o_flags = p->o_flags;
  990. t->parms.fwmark = p->fwmark;
  991. t->parms.erspan_ver = p->erspan_ver;
  992. t->parms.index = p->index;
  993. t->parms.dir = p->dir;
  994. t->parms.hwid = p->hwid;
  995. dst_cache_reset(&t->dst_cache);
  996. }
  997. static int ip6gre_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p,
  998. int set_mtu)
  999. {
  1000. ip6gre_tnl_copy_tnl_parm(t, p);
  1001. ip6gre_tnl_link_config(t, set_mtu);
  1002. return 0;
  1003. }
  1004. static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
  1005. const struct ip6_tnl_parm2 *u)
  1006. {
  1007. p->laddr = u->laddr;
  1008. p->raddr = u->raddr;
  1009. p->flags = u->flags;
  1010. p->hop_limit = u->hop_limit;
  1011. p->encap_limit = u->encap_limit;
  1012. p->flowinfo = u->flowinfo;
  1013. p->link = u->link;
  1014. p->i_key = u->i_key;
  1015. p->o_key = u->o_key;
  1016. p->i_flags = gre_flags_to_tnl_flags(u->i_flags);
  1017. p->o_flags = gre_flags_to_tnl_flags(u->o_flags);
  1018. memcpy(p->name, u->name, sizeof(u->name));
  1019. }
  1020. static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
  1021. const struct __ip6_tnl_parm *p)
  1022. {
  1023. u->proto = IPPROTO_GRE;
  1024. u->laddr = p->laddr;
  1025. u->raddr = p->raddr;
  1026. u->flags = p->flags;
  1027. u->hop_limit = p->hop_limit;
  1028. u->encap_limit = p->encap_limit;
  1029. u->flowinfo = p->flowinfo;
  1030. u->link = p->link;
  1031. u->i_key = p->i_key;
  1032. u->o_key = p->o_key;
  1033. u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
  1034. u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
  1035. memcpy(u->name, p->name, sizeof(u->name));
  1036. }
  1037. static int ip6gre_tunnel_ioctl(struct net_device *dev,
  1038. struct ifreq *ifr, int cmd)
  1039. {
  1040. int err = 0;
  1041. struct ip6_tnl_parm2 p;
  1042. struct __ip6_tnl_parm p1;
  1043. struct ip6_tnl *t = netdev_priv(dev);
  1044. struct net *net = t->net;
  1045. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1046. memset(&p1, 0, sizeof(p1));
  1047. switch (cmd) {
  1048. case SIOCGETTUNNEL:
  1049. if (dev == ign->fb_tunnel_dev) {
  1050. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  1051. err = -EFAULT;
  1052. break;
  1053. }
  1054. ip6gre_tnl_parm_from_user(&p1, &p);
  1055. t = ip6gre_tunnel_locate(net, &p1, 0);
  1056. if (!t)
  1057. t = netdev_priv(dev);
  1058. }
  1059. memset(&p, 0, sizeof(p));
  1060. ip6gre_tnl_parm_to_user(&p, &t->parms);
  1061. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  1062. err = -EFAULT;
  1063. break;
  1064. case SIOCADDTUNNEL:
  1065. case SIOCCHGTUNNEL:
  1066. err = -EPERM;
  1067. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1068. goto done;
  1069. err = -EFAULT;
  1070. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1071. goto done;
  1072. err = -EINVAL;
  1073. if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
  1074. goto done;
  1075. if (!(p.i_flags&GRE_KEY))
  1076. p.i_key = 0;
  1077. if (!(p.o_flags&GRE_KEY))
  1078. p.o_key = 0;
  1079. ip6gre_tnl_parm_from_user(&p1, &p);
  1080. t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
  1081. if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  1082. if (t) {
  1083. if (t->dev != dev) {
  1084. err = -EEXIST;
  1085. break;
  1086. }
  1087. } else {
  1088. t = netdev_priv(dev);
  1089. ip6gre_tunnel_unlink(ign, t);
  1090. synchronize_net();
  1091. ip6gre_tnl_change(t, &p1, 1);
  1092. ip6gre_tunnel_link(ign, t);
  1093. netdev_state_change(dev);
  1094. }
  1095. }
  1096. if (t) {
  1097. err = 0;
  1098. memset(&p, 0, sizeof(p));
  1099. ip6gre_tnl_parm_to_user(&p, &t->parms);
  1100. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  1101. err = -EFAULT;
  1102. } else
  1103. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  1104. break;
  1105. case SIOCDELTUNNEL:
  1106. err = -EPERM;
  1107. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1108. goto done;
  1109. if (dev == ign->fb_tunnel_dev) {
  1110. err = -EFAULT;
  1111. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1112. goto done;
  1113. err = -ENOENT;
  1114. ip6gre_tnl_parm_from_user(&p1, &p);
  1115. t = ip6gre_tunnel_locate(net, &p1, 0);
  1116. if (!t)
  1117. goto done;
  1118. err = -EPERM;
  1119. if (t == netdev_priv(ign->fb_tunnel_dev))
  1120. goto done;
  1121. dev = t->dev;
  1122. }
  1123. unregister_netdevice(dev);
  1124. err = 0;
  1125. break;
  1126. default:
  1127. err = -EINVAL;
  1128. }
  1129. done:
  1130. return err;
  1131. }
  1132. static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
  1133. unsigned short type, const void *daddr,
  1134. const void *saddr, unsigned int len)
  1135. {
  1136. struct ip6_tnl *t = netdev_priv(dev);
  1137. struct ipv6hdr *ipv6h;
  1138. __be16 *p;
  1139. ipv6h = skb_push(skb, t->hlen + sizeof(*ipv6h));
  1140. ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
  1141. t->fl.u.ip6.flowlabel,
  1142. true, &t->fl.u.ip6));
  1143. ipv6h->hop_limit = t->parms.hop_limit;
  1144. ipv6h->nexthdr = NEXTHDR_GRE;
  1145. ipv6h->saddr = t->parms.laddr;
  1146. ipv6h->daddr = t->parms.raddr;
  1147. p = (__be16 *)(ipv6h + 1);
  1148. p[0] = t->parms.o_flags;
  1149. p[1] = htons(type);
  1150. /*
  1151. * Set the source hardware address.
  1152. */
  1153. if (saddr)
  1154. memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
  1155. if (daddr)
  1156. memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
  1157. if (!ipv6_addr_any(&ipv6h->daddr))
  1158. return t->hlen;
  1159. return -t->hlen;
  1160. }
  1161. static const struct header_ops ip6gre_header_ops = {
  1162. .create = ip6gre_header,
  1163. };
  1164. static const struct net_device_ops ip6gre_netdev_ops = {
  1165. .ndo_init = ip6gre_tunnel_init,
  1166. .ndo_uninit = ip6gre_tunnel_uninit,
  1167. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1168. .ndo_do_ioctl = ip6gre_tunnel_ioctl,
  1169. .ndo_change_mtu = ip6_tnl_change_mtu,
  1170. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1171. .ndo_get_iflink = ip6_tnl_get_iflink,
  1172. };
  1173. static void ip6gre_dev_free(struct net_device *dev)
  1174. {
  1175. struct ip6_tnl *t = netdev_priv(dev);
  1176. gro_cells_destroy(&t->gro_cells);
  1177. dst_cache_destroy(&t->dst_cache);
  1178. free_percpu(dev->tstats);
  1179. }
  1180. static void ip6gre_tunnel_setup(struct net_device *dev)
  1181. {
  1182. dev->netdev_ops = &ip6gre_netdev_ops;
  1183. dev->needs_free_netdev = true;
  1184. dev->priv_destructor = ip6gre_dev_free;
  1185. dev->type = ARPHRD_IP6GRE;
  1186. dev->flags |= IFF_NOARP;
  1187. dev->addr_len = sizeof(struct in6_addr);
  1188. netif_keep_dst(dev);
  1189. /* This perm addr will be used as interface identifier by IPv6 */
  1190. dev->addr_assign_type = NET_ADDR_RANDOM;
  1191. eth_random_addr(dev->perm_addr);
  1192. }
  1193. #define GRE6_FEATURES (NETIF_F_SG | \
  1194. NETIF_F_FRAGLIST | \
  1195. NETIF_F_HIGHDMA | \
  1196. NETIF_F_HW_CSUM)
  1197. static void ip6gre_tnl_init_features(struct net_device *dev)
  1198. {
  1199. struct ip6_tnl *nt = netdev_priv(dev);
  1200. dev->features |= GRE6_FEATURES;
  1201. dev->hw_features |= GRE6_FEATURES;
  1202. if (!(nt->parms.o_flags & TUNNEL_SEQ)) {
  1203. /* TCP offload with GRE SEQ is not supported, nor
  1204. * can we support 2 levels of outer headers requiring
  1205. * an update.
  1206. */
  1207. if (!(nt->parms.o_flags & TUNNEL_CSUM) ||
  1208. nt->encap.type == TUNNEL_ENCAP_NONE) {
  1209. dev->features |= NETIF_F_GSO_SOFTWARE;
  1210. dev->hw_features |= NETIF_F_GSO_SOFTWARE;
  1211. }
  1212. /* Can use a lockless transmit, unless we generate
  1213. * output sequences
  1214. */
  1215. dev->features |= NETIF_F_LLTX;
  1216. }
  1217. }
  1218. static int ip6gre_tunnel_init_common(struct net_device *dev)
  1219. {
  1220. struct ip6_tnl *tunnel;
  1221. int ret;
  1222. int t_hlen;
  1223. tunnel = netdev_priv(dev);
  1224. tunnel->dev = dev;
  1225. tunnel->net = dev_net(dev);
  1226. strcpy(tunnel->parms.name, dev->name);
  1227. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1228. if (!dev->tstats)
  1229. return -ENOMEM;
  1230. ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
  1231. if (ret)
  1232. goto cleanup_alloc_pcpu_stats;
  1233. ret = gro_cells_init(&tunnel->gro_cells, dev);
  1234. if (ret)
  1235. goto cleanup_dst_cache_init;
  1236. t_hlen = ip6gre_calc_hlen(tunnel);
  1237. dev->mtu = ETH_DATA_LEN - t_hlen;
  1238. if (dev->type == ARPHRD_ETHER)
  1239. dev->mtu -= ETH_HLEN;
  1240. if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1241. dev->mtu -= 8;
  1242. if (tunnel->parms.collect_md) {
  1243. dev->features |= NETIF_F_NETNS_LOCAL;
  1244. netif_keep_dst(dev);
  1245. }
  1246. ip6gre_tnl_init_features(dev);
  1247. return 0;
  1248. cleanup_dst_cache_init:
  1249. dst_cache_destroy(&tunnel->dst_cache);
  1250. cleanup_alloc_pcpu_stats:
  1251. free_percpu(dev->tstats);
  1252. dev->tstats = NULL;
  1253. return ret;
  1254. }
  1255. static int ip6gre_tunnel_init(struct net_device *dev)
  1256. {
  1257. struct ip6_tnl *tunnel;
  1258. int ret;
  1259. ret = ip6gre_tunnel_init_common(dev);
  1260. if (ret)
  1261. return ret;
  1262. tunnel = netdev_priv(dev);
  1263. if (tunnel->parms.collect_md)
  1264. return 0;
  1265. memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
  1266. memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
  1267. if (ipv6_addr_any(&tunnel->parms.raddr))
  1268. dev->header_ops = &ip6gre_header_ops;
  1269. return 0;
  1270. }
  1271. static void ip6gre_fb_tunnel_init(struct net_device *dev)
  1272. {
  1273. struct ip6_tnl *tunnel = netdev_priv(dev);
  1274. tunnel->dev = dev;
  1275. tunnel->net = dev_net(dev);
  1276. strcpy(tunnel->parms.name, dev->name);
  1277. tunnel->hlen = sizeof(struct ipv6hdr) + 4;
  1278. dev_hold(dev);
  1279. }
  1280. static struct inet6_protocol ip6gre_protocol __read_mostly = {
  1281. .handler = gre_rcv,
  1282. .err_handler = ip6gre_err,
  1283. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  1284. };
  1285. static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)
  1286. {
  1287. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1288. struct net_device *dev, *aux;
  1289. int prio;
  1290. for_each_netdev_safe(net, dev, aux)
  1291. if (dev->rtnl_link_ops == &ip6gre_link_ops ||
  1292. dev->rtnl_link_ops == &ip6gre_tap_ops ||
  1293. dev->rtnl_link_ops == &ip6erspan_tap_ops)
  1294. unregister_netdevice_queue(dev, head);
  1295. for (prio = 0; prio < 4; prio++) {
  1296. int h;
  1297. for (h = 0; h < IP6_GRE_HASH_SIZE; h++) {
  1298. struct ip6_tnl *t;
  1299. t = rtnl_dereference(ign->tunnels[prio][h]);
  1300. while (t) {
  1301. /* If dev is in the same netns, it has already
  1302. * been added to the list by the previous loop.
  1303. */
  1304. if (!net_eq(dev_net(t->dev), net))
  1305. unregister_netdevice_queue(t->dev,
  1306. head);
  1307. t = rtnl_dereference(t->next);
  1308. }
  1309. }
  1310. }
  1311. }
  1312. static int __net_init ip6gre_init_net(struct net *net)
  1313. {
  1314. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1315. int err;
  1316. if (!net_has_fallback_tunnels(net))
  1317. return 0;
  1318. ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
  1319. NET_NAME_UNKNOWN,
  1320. ip6gre_tunnel_setup);
  1321. if (!ign->fb_tunnel_dev) {
  1322. err = -ENOMEM;
  1323. goto err_alloc_dev;
  1324. }
  1325. dev_net_set(ign->fb_tunnel_dev, net);
  1326. /* FB netdevice is special: we have one, and only one per netns.
  1327. * Allowing to move it to another netns is clearly unsafe.
  1328. */
  1329. ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
  1330. ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
  1331. ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
  1332. err = register_netdev(ign->fb_tunnel_dev);
  1333. if (err)
  1334. goto err_reg_dev;
  1335. rcu_assign_pointer(ign->tunnels_wc[0],
  1336. netdev_priv(ign->fb_tunnel_dev));
  1337. return 0;
  1338. err_reg_dev:
  1339. free_netdev(ign->fb_tunnel_dev);
  1340. err_alloc_dev:
  1341. return err;
  1342. }
  1343. static void __net_exit ip6gre_exit_batch_net(struct list_head *net_list)
  1344. {
  1345. struct net *net;
  1346. LIST_HEAD(list);
  1347. rtnl_lock();
  1348. list_for_each_entry(net, net_list, exit_list)
  1349. ip6gre_destroy_tunnels(net, &list);
  1350. unregister_netdevice_many(&list);
  1351. rtnl_unlock();
  1352. }
  1353. static struct pernet_operations ip6gre_net_ops = {
  1354. .init = ip6gre_init_net,
  1355. .exit_batch = ip6gre_exit_batch_net,
  1356. .id = &ip6gre_net_id,
  1357. .size = sizeof(struct ip6gre_net),
  1358. };
  1359. static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
  1360. struct netlink_ext_ack *extack)
  1361. {
  1362. __be16 flags;
  1363. if (!data)
  1364. return 0;
  1365. flags = 0;
  1366. if (data[IFLA_GRE_IFLAGS])
  1367. flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
  1368. if (data[IFLA_GRE_OFLAGS])
  1369. flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
  1370. if (flags & (GRE_VERSION|GRE_ROUTING))
  1371. return -EINVAL;
  1372. return 0;
  1373. }
  1374. static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
  1375. struct netlink_ext_ack *extack)
  1376. {
  1377. struct in6_addr daddr;
  1378. if (tb[IFLA_ADDRESS]) {
  1379. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  1380. return -EINVAL;
  1381. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  1382. return -EADDRNOTAVAIL;
  1383. }
  1384. if (!data)
  1385. goto out;
  1386. if (data[IFLA_GRE_REMOTE]) {
  1387. daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
  1388. if (ipv6_addr_any(&daddr))
  1389. return -EINVAL;
  1390. }
  1391. out:
  1392. return ip6gre_tunnel_validate(tb, data, extack);
  1393. }
  1394. static int ip6erspan_tap_validate(struct nlattr *tb[], struct nlattr *data[],
  1395. struct netlink_ext_ack *extack)
  1396. {
  1397. __be16 flags = 0;
  1398. int ret, ver = 0;
  1399. if (!data)
  1400. return 0;
  1401. ret = ip6gre_tap_validate(tb, data, extack);
  1402. if (ret)
  1403. return ret;
  1404. /* ERSPAN should only have GRE sequence and key flag */
  1405. if (data[IFLA_GRE_OFLAGS])
  1406. flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
  1407. if (data[IFLA_GRE_IFLAGS])
  1408. flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
  1409. if (!data[IFLA_GRE_COLLECT_METADATA] &&
  1410. flags != (GRE_SEQ | GRE_KEY))
  1411. return -EINVAL;
  1412. /* ERSPAN Session ID only has 10-bit. Since we reuse
  1413. * 32-bit key field as ID, check it's range.
  1414. */
  1415. if (data[IFLA_GRE_IKEY] &&
  1416. (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK))
  1417. return -EINVAL;
  1418. if (data[IFLA_GRE_OKEY] &&
  1419. (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK))
  1420. return -EINVAL;
  1421. if (data[IFLA_GRE_ERSPAN_VER]) {
  1422. ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
  1423. if (ver != 1 && ver != 2)
  1424. return -EINVAL;
  1425. }
  1426. if (ver == 1) {
  1427. if (data[IFLA_GRE_ERSPAN_INDEX]) {
  1428. u32 index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
  1429. if (index & ~INDEX_MASK)
  1430. return -EINVAL;
  1431. }
  1432. } else if (ver == 2) {
  1433. if (data[IFLA_GRE_ERSPAN_DIR]) {
  1434. u16 dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
  1435. if (dir & ~(DIR_MASK >> DIR_OFFSET))
  1436. return -EINVAL;
  1437. }
  1438. if (data[IFLA_GRE_ERSPAN_HWID]) {
  1439. u16 hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
  1440. if (hwid & ~(HWID_MASK >> HWID_OFFSET))
  1441. return -EINVAL;
  1442. }
  1443. }
  1444. return 0;
  1445. }
  1446. static void ip6erspan_set_version(struct nlattr *data[],
  1447. struct __ip6_tnl_parm *parms)
  1448. {
  1449. if (!data)
  1450. return;
  1451. parms->erspan_ver = 1;
  1452. if (data[IFLA_GRE_ERSPAN_VER])
  1453. parms->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
  1454. if (parms->erspan_ver == 1) {
  1455. if (data[IFLA_GRE_ERSPAN_INDEX])
  1456. parms->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
  1457. } else if (parms->erspan_ver == 2) {
  1458. if (data[IFLA_GRE_ERSPAN_DIR])
  1459. parms->dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
  1460. if (data[IFLA_GRE_ERSPAN_HWID])
  1461. parms->hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
  1462. }
  1463. }
  1464. static void ip6gre_netlink_parms(struct nlattr *data[],
  1465. struct __ip6_tnl_parm *parms)
  1466. {
  1467. memset(parms, 0, sizeof(*parms));
  1468. if (!data)
  1469. return;
  1470. if (data[IFLA_GRE_LINK])
  1471. parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
  1472. if (data[IFLA_GRE_IFLAGS])
  1473. parms->i_flags = gre_flags_to_tnl_flags(
  1474. nla_get_be16(data[IFLA_GRE_IFLAGS]));
  1475. if (data[IFLA_GRE_OFLAGS])
  1476. parms->o_flags = gre_flags_to_tnl_flags(
  1477. nla_get_be16(data[IFLA_GRE_OFLAGS]));
  1478. if (data[IFLA_GRE_IKEY])
  1479. parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
  1480. if (data[IFLA_GRE_OKEY])
  1481. parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
  1482. if (data[IFLA_GRE_LOCAL])
  1483. parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
  1484. if (data[IFLA_GRE_REMOTE])
  1485. parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
  1486. if (data[IFLA_GRE_TTL])
  1487. parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
  1488. if (data[IFLA_GRE_ENCAP_LIMIT])
  1489. parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
  1490. if (data[IFLA_GRE_FLOWINFO])
  1491. parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]);
  1492. if (data[IFLA_GRE_FLAGS])
  1493. parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
  1494. if (data[IFLA_GRE_FWMARK])
  1495. parms->fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
  1496. if (data[IFLA_GRE_COLLECT_METADATA])
  1497. parms->collect_md = true;
  1498. }
  1499. static int ip6gre_tap_init(struct net_device *dev)
  1500. {
  1501. int ret;
  1502. ret = ip6gre_tunnel_init_common(dev);
  1503. if (ret)
  1504. return ret;
  1505. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1506. return 0;
  1507. }
  1508. static const struct net_device_ops ip6gre_tap_netdev_ops = {
  1509. .ndo_init = ip6gre_tap_init,
  1510. .ndo_uninit = ip6gre_tunnel_uninit,
  1511. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1512. .ndo_set_mac_address = eth_mac_addr,
  1513. .ndo_validate_addr = eth_validate_addr,
  1514. .ndo_change_mtu = ip6_tnl_change_mtu,
  1515. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1516. .ndo_get_iflink = ip6_tnl_get_iflink,
  1517. };
  1518. static int ip6erspan_calc_hlen(struct ip6_tnl *tunnel)
  1519. {
  1520. int t_hlen;
  1521. tunnel->tun_hlen = 8;
  1522. tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
  1523. erspan_hdr_len(tunnel->parms.erspan_ver);
  1524. t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
  1525. tunnel->dev->needed_headroom = LL_MAX_HEADER + t_hlen;
  1526. return t_hlen;
  1527. }
  1528. static int ip6erspan_tap_init(struct net_device *dev)
  1529. {
  1530. struct ip6_tnl *tunnel;
  1531. int t_hlen;
  1532. int ret;
  1533. tunnel = netdev_priv(dev);
  1534. tunnel->dev = dev;
  1535. tunnel->net = dev_net(dev);
  1536. strcpy(tunnel->parms.name, dev->name);
  1537. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1538. if (!dev->tstats)
  1539. return -ENOMEM;
  1540. ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
  1541. if (ret)
  1542. goto cleanup_alloc_pcpu_stats;
  1543. ret = gro_cells_init(&tunnel->gro_cells, dev);
  1544. if (ret)
  1545. goto cleanup_dst_cache_init;
  1546. t_hlen = ip6erspan_calc_hlen(tunnel);
  1547. dev->mtu = ETH_DATA_LEN - t_hlen;
  1548. if (dev->type == ARPHRD_ETHER)
  1549. dev->mtu -= ETH_HLEN;
  1550. if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1551. dev->mtu -= 8;
  1552. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1553. ip6erspan_tnl_link_config(tunnel, 1);
  1554. return 0;
  1555. cleanup_dst_cache_init:
  1556. dst_cache_destroy(&tunnel->dst_cache);
  1557. cleanup_alloc_pcpu_stats:
  1558. free_percpu(dev->tstats);
  1559. dev->tstats = NULL;
  1560. return ret;
  1561. }
  1562. static const struct net_device_ops ip6erspan_netdev_ops = {
  1563. .ndo_init = ip6erspan_tap_init,
  1564. .ndo_uninit = ip6erspan_tunnel_uninit,
  1565. .ndo_start_xmit = ip6erspan_tunnel_xmit,
  1566. .ndo_set_mac_address = eth_mac_addr,
  1567. .ndo_validate_addr = eth_validate_addr,
  1568. .ndo_change_mtu = ip6_tnl_change_mtu,
  1569. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1570. .ndo_get_iflink = ip6_tnl_get_iflink,
  1571. };
  1572. static void ip6gre_tap_setup(struct net_device *dev)
  1573. {
  1574. ether_setup(dev);
  1575. dev->max_mtu = 0;
  1576. dev->netdev_ops = &ip6gre_tap_netdev_ops;
  1577. dev->needs_free_netdev = true;
  1578. dev->priv_destructor = ip6gre_dev_free;
  1579. dev->features |= NETIF_F_NETNS_LOCAL;
  1580. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  1581. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1582. netif_keep_dst(dev);
  1583. }
  1584. bool is_ip6gretap_dev(const struct net_device *dev)
  1585. {
  1586. return dev->netdev_ops == &ip6gre_tap_netdev_ops;
  1587. }
  1588. EXPORT_SYMBOL_GPL(is_ip6gretap_dev);
  1589. static bool ip6gre_netlink_encap_parms(struct nlattr *data[],
  1590. struct ip_tunnel_encap *ipencap)
  1591. {
  1592. bool ret = false;
  1593. memset(ipencap, 0, sizeof(*ipencap));
  1594. if (!data)
  1595. return ret;
  1596. if (data[IFLA_GRE_ENCAP_TYPE]) {
  1597. ret = true;
  1598. ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
  1599. }
  1600. if (data[IFLA_GRE_ENCAP_FLAGS]) {
  1601. ret = true;
  1602. ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
  1603. }
  1604. if (data[IFLA_GRE_ENCAP_SPORT]) {
  1605. ret = true;
  1606. ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
  1607. }
  1608. if (data[IFLA_GRE_ENCAP_DPORT]) {
  1609. ret = true;
  1610. ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
  1611. }
  1612. return ret;
  1613. }
  1614. static int ip6gre_newlink_common(struct net *src_net, struct net_device *dev,
  1615. struct nlattr *tb[], struct nlattr *data[],
  1616. struct netlink_ext_ack *extack)
  1617. {
  1618. struct ip6_tnl *nt;
  1619. struct ip_tunnel_encap ipencap;
  1620. int err;
  1621. nt = netdev_priv(dev);
  1622. if (ip6gre_netlink_encap_parms(data, &ipencap)) {
  1623. int err = ip6_tnl_encap_setup(nt, &ipencap);
  1624. if (err < 0)
  1625. return err;
  1626. }
  1627. if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
  1628. eth_hw_addr_random(dev);
  1629. nt->dev = dev;
  1630. nt->net = dev_net(dev);
  1631. err = register_netdevice(dev);
  1632. if (err)
  1633. goto out;
  1634. if (tb[IFLA_MTU])
  1635. ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
  1636. dev_hold(dev);
  1637. out:
  1638. return err;
  1639. }
  1640. static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
  1641. struct nlattr *tb[], struct nlattr *data[],
  1642. struct netlink_ext_ack *extack)
  1643. {
  1644. struct ip6_tnl *nt = netdev_priv(dev);
  1645. struct net *net = dev_net(dev);
  1646. struct ip6gre_net *ign;
  1647. int err;
  1648. ip6gre_netlink_parms(data, &nt->parms);
  1649. ign = net_generic(net, ip6gre_net_id);
  1650. if (nt->parms.collect_md) {
  1651. if (rtnl_dereference(ign->collect_md_tun))
  1652. return -EEXIST;
  1653. } else {
  1654. if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
  1655. return -EEXIST;
  1656. }
  1657. err = ip6gre_newlink_common(src_net, dev, tb, data, extack);
  1658. if (!err) {
  1659. ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
  1660. ip6gre_tunnel_link_md(ign, nt);
  1661. ip6gre_tunnel_link(net_generic(net, ip6gre_net_id), nt);
  1662. }
  1663. return err;
  1664. }
  1665. static struct ip6_tnl *
  1666. ip6gre_changelink_common(struct net_device *dev, struct nlattr *tb[],
  1667. struct nlattr *data[], struct __ip6_tnl_parm *p_p,
  1668. struct netlink_ext_ack *extack)
  1669. {
  1670. struct ip6_tnl *t, *nt = netdev_priv(dev);
  1671. struct net *net = nt->net;
  1672. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1673. struct ip_tunnel_encap ipencap;
  1674. if (dev == ign->fb_tunnel_dev)
  1675. return ERR_PTR(-EINVAL);
  1676. if (ip6gre_netlink_encap_parms(data, &ipencap)) {
  1677. int err = ip6_tnl_encap_setup(nt, &ipencap);
  1678. if (err < 0)
  1679. return ERR_PTR(err);
  1680. }
  1681. ip6gre_netlink_parms(data, p_p);
  1682. t = ip6gre_tunnel_locate(net, p_p, 0);
  1683. if (t) {
  1684. if (t->dev != dev)
  1685. return ERR_PTR(-EEXIST);
  1686. } else {
  1687. t = nt;
  1688. }
  1689. return t;
  1690. }
  1691. static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
  1692. struct nlattr *data[],
  1693. struct netlink_ext_ack *extack)
  1694. {
  1695. struct ip6_tnl *t = netdev_priv(dev);
  1696. struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
  1697. struct __ip6_tnl_parm p;
  1698. t = ip6gre_changelink_common(dev, tb, data, &p, extack);
  1699. if (IS_ERR(t))
  1700. return PTR_ERR(t);
  1701. ip6gre_tunnel_unlink_md(ign, t);
  1702. ip6gre_tunnel_unlink(ign, t);
  1703. ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
  1704. ip6gre_tunnel_link_md(ign, t);
  1705. ip6gre_tunnel_link(ign, t);
  1706. return 0;
  1707. }
  1708. static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
  1709. {
  1710. struct net *net = dev_net(dev);
  1711. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1712. if (dev != ign->fb_tunnel_dev)
  1713. unregister_netdevice_queue(dev, head);
  1714. }
  1715. static size_t ip6gre_get_size(const struct net_device *dev)
  1716. {
  1717. return
  1718. /* IFLA_GRE_LINK */
  1719. nla_total_size(4) +
  1720. /* IFLA_GRE_IFLAGS */
  1721. nla_total_size(2) +
  1722. /* IFLA_GRE_OFLAGS */
  1723. nla_total_size(2) +
  1724. /* IFLA_GRE_IKEY */
  1725. nla_total_size(4) +
  1726. /* IFLA_GRE_OKEY */
  1727. nla_total_size(4) +
  1728. /* IFLA_GRE_LOCAL */
  1729. nla_total_size(sizeof(struct in6_addr)) +
  1730. /* IFLA_GRE_REMOTE */
  1731. nla_total_size(sizeof(struct in6_addr)) +
  1732. /* IFLA_GRE_TTL */
  1733. nla_total_size(1) +
  1734. /* IFLA_GRE_ENCAP_LIMIT */
  1735. nla_total_size(1) +
  1736. /* IFLA_GRE_FLOWINFO */
  1737. nla_total_size(4) +
  1738. /* IFLA_GRE_FLAGS */
  1739. nla_total_size(4) +
  1740. /* IFLA_GRE_ENCAP_TYPE */
  1741. nla_total_size(2) +
  1742. /* IFLA_GRE_ENCAP_FLAGS */
  1743. nla_total_size(2) +
  1744. /* IFLA_GRE_ENCAP_SPORT */
  1745. nla_total_size(2) +
  1746. /* IFLA_GRE_ENCAP_DPORT */
  1747. nla_total_size(2) +
  1748. /* IFLA_GRE_COLLECT_METADATA */
  1749. nla_total_size(0) +
  1750. /* IFLA_GRE_FWMARK */
  1751. nla_total_size(4) +
  1752. /* IFLA_GRE_ERSPAN_INDEX */
  1753. nla_total_size(4) +
  1754. 0;
  1755. }
  1756. static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1757. {
  1758. struct ip6_tnl *t = netdev_priv(dev);
  1759. struct __ip6_tnl_parm *p = &t->parms;
  1760. __be16 o_flags = p->o_flags;
  1761. if ((p->erspan_ver == 1 || p->erspan_ver == 2) &&
  1762. !p->collect_md)
  1763. o_flags |= TUNNEL_KEY;
  1764. if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
  1765. nla_put_be16(skb, IFLA_GRE_IFLAGS,
  1766. gre_tnl_flags_to_gre_flags(p->i_flags)) ||
  1767. nla_put_be16(skb, IFLA_GRE_OFLAGS,
  1768. gre_tnl_flags_to_gre_flags(o_flags)) ||
  1769. nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
  1770. nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
  1771. nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
  1772. nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
  1773. nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
  1774. nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
  1775. nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
  1776. nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags) ||
  1777. nla_put_u32(skb, IFLA_GRE_FWMARK, p->fwmark) ||
  1778. nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index))
  1779. goto nla_put_failure;
  1780. if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
  1781. t->encap.type) ||
  1782. nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
  1783. t->encap.sport) ||
  1784. nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
  1785. t->encap.dport) ||
  1786. nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
  1787. t->encap.flags))
  1788. goto nla_put_failure;
  1789. if (p->collect_md) {
  1790. if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA))
  1791. goto nla_put_failure;
  1792. }
  1793. if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, p->erspan_ver))
  1794. goto nla_put_failure;
  1795. if (p->erspan_ver == 1) {
  1796. if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index))
  1797. goto nla_put_failure;
  1798. } else if (p->erspan_ver == 2) {
  1799. if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, p->dir))
  1800. goto nla_put_failure;
  1801. if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, p->hwid))
  1802. goto nla_put_failure;
  1803. }
  1804. return 0;
  1805. nla_put_failure:
  1806. return -EMSGSIZE;
  1807. }
  1808. static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
  1809. [IFLA_GRE_LINK] = { .type = NLA_U32 },
  1810. [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
  1811. [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
  1812. [IFLA_GRE_IKEY] = { .type = NLA_U32 },
  1813. [IFLA_GRE_OKEY] = { .type = NLA_U32 },
  1814. [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
  1815. [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
  1816. [IFLA_GRE_TTL] = { .type = NLA_U8 },
  1817. [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
  1818. [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 },
  1819. [IFLA_GRE_FLAGS] = { .type = NLA_U32 },
  1820. [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 },
  1821. [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 },
  1822. [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 },
  1823. [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 },
  1824. [IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG },
  1825. [IFLA_GRE_FWMARK] = { .type = NLA_U32 },
  1826. [IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 },
  1827. [IFLA_GRE_ERSPAN_VER] = { .type = NLA_U8 },
  1828. [IFLA_GRE_ERSPAN_DIR] = { .type = NLA_U8 },
  1829. [IFLA_GRE_ERSPAN_HWID] = { .type = NLA_U16 },
  1830. };
  1831. static void ip6erspan_tap_setup(struct net_device *dev)
  1832. {
  1833. ether_setup(dev);
  1834. dev->netdev_ops = &ip6erspan_netdev_ops;
  1835. dev->needs_free_netdev = true;
  1836. dev->priv_destructor = ip6gre_dev_free;
  1837. dev->features |= NETIF_F_NETNS_LOCAL;
  1838. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  1839. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1840. netif_keep_dst(dev);
  1841. }
  1842. static int ip6erspan_newlink(struct net *src_net, struct net_device *dev,
  1843. struct nlattr *tb[], struct nlattr *data[],
  1844. struct netlink_ext_ack *extack)
  1845. {
  1846. struct ip6_tnl *nt = netdev_priv(dev);
  1847. struct net *net = dev_net(dev);
  1848. struct ip6gre_net *ign;
  1849. int err;
  1850. ip6gre_netlink_parms(data, &nt->parms);
  1851. ip6erspan_set_version(data, &nt->parms);
  1852. ign = net_generic(net, ip6gre_net_id);
  1853. if (nt->parms.collect_md) {
  1854. if (rtnl_dereference(ign->collect_md_tun_erspan))
  1855. return -EEXIST;
  1856. } else {
  1857. if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
  1858. return -EEXIST;
  1859. }
  1860. err = ip6gre_newlink_common(src_net, dev, tb, data, extack);
  1861. if (!err) {
  1862. ip6erspan_tnl_link_config(nt, !tb[IFLA_MTU]);
  1863. ip6erspan_tunnel_link_md(ign, nt);
  1864. ip6gre_tunnel_link(net_generic(net, ip6gre_net_id), nt);
  1865. }
  1866. return err;
  1867. }
  1868. static void ip6erspan_tnl_link_config(struct ip6_tnl *t, int set_mtu)
  1869. {
  1870. ip6gre_tnl_link_config_common(t);
  1871. ip6gre_tnl_link_config_route(t, set_mtu, ip6erspan_calc_hlen(t));
  1872. }
  1873. static int ip6erspan_tnl_change(struct ip6_tnl *t,
  1874. const struct __ip6_tnl_parm *p, int set_mtu)
  1875. {
  1876. ip6gre_tnl_copy_tnl_parm(t, p);
  1877. ip6erspan_tnl_link_config(t, set_mtu);
  1878. return 0;
  1879. }
  1880. static int ip6erspan_changelink(struct net_device *dev, struct nlattr *tb[],
  1881. struct nlattr *data[],
  1882. struct netlink_ext_ack *extack)
  1883. {
  1884. struct ip6gre_net *ign = net_generic(dev_net(dev), ip6gre_net_id);
  1885. struct __ip6_tnl_parm p;
  1886. struct ip6_tnl *t;
  1887. t = ip6gre_changelink_common(dev, tb, data, &p, extack);
  1888. if (IS_ERR(t))
  1889. return PTR_ERR(t);
  1890. ip6erspan_set_version(data, &p);
  1891. ip6gre_tunnel_unlink_md(ign, t);
  1892. ip6gre_tunnel_unlink(ign, t);
  1893. ip6erspan_tnl_change(t, &p, !tb[IFLA_MTU]);
  1894. ip6erspan_tunnel_link_md(ign, t);
  1895. ip6gre_tunnel_link(ign, t);
  1896. return 0;
  1897. }
  1898. static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
  1899. .kind = "ip6gre",
  1900. .maxtype = IFLA_GRE_MAX,
  1901. .policy = ip6gre_policy,
  1902. .priv_size = sizeof(struct ip6_tnl),
  1903. .setup = ip6gre_tunnel_setup,
  1904. .validate = ip6gre_tunnel_validate,
  1905. .newlink = ip6gre_newlink,
  1906. .changelink = ip6gre_changelink,
  1907. .dellink = ip6gre_dellink,
  1908. .get_size = ip6gre_get_size,
  1909. .fill_info = ip6gre_fill_info,
  1910. .get_link_net = ip6_tnl_get_link_net,
  1911. };
  1912. static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
  1913. .kind = "ip6gretap",
  1914. .maxtype = IFLA_GRE_MAX,
  1915. .policy = ip6gre_policy,
  1916. .priv_size = sizeof(struct ip6_tnl),
  1917. .setup = ip6gre_tap_setup,
  1918. .validate = ip6gre_tap_validate,
  1919. .newlink = ip6gre_newlink,
  1920. .changelink = ip6gre_changelink,
  1921. .get_size = ip6gre_get_size,
  1922. .fill_info = ip6gre_fill_info,
  1923. .get_link_net = ip6_tnl_get_link_net,
  1924. };
  1925. static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly = {
  1926. .kind = "ip6erspan",
  1927. .maxtype = IFLA_GRE_MAX,
  1928. .policy = ip6gre_policy,
  1929. .priv_size = sizeof(struct ip6_tnl),
  1930. .setup = ip6erspan_tap_setup,
  1931. .validate = ip6erspan_tap_validate,
  1932. .newlink = ip6erspan_newlink,
  1933. .changelink = ip6erspan_changelink,
  1934. .get_size = ip6gre_get_size,
  1935. .fill_info = ip6gre_fill_info,
  1936. .get_link_net = ip6_tnl_get_link_net,
  1937. };
  1938. /*
  1939. * And now the modules code and kernel interface.
  1940. */
  1941. static int __init ip6gre_init(void)
  1942. {
  1943. int err;
  1944. pr_info("GRE over IPv6 tunneling driver\n");
  1945. err = register_pernet_device(&ip6gre_net_ops);
  1946. if (err < 0)
  1947. return err;
  1948. err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1949. if (err < 0) {
  1950. pr_info("%s: can't add protocol\n", __func__);
  1951. goto add_proto_failed;
  1952. }
  1953. err = rtnl_link_register(&ip6gre_link_ops);
  1954. if (err < 0)
  1955. goto rtnl_link_failed;
  1956. err = rtnl_link_register(&ip6gre_tap_ops);
  1957. if (err < 0)
  1958. goto tap_ops_failed;
  1959. err = rtnl_link_register(&ip6erspan_tap_ops);
  1960. if (err < 0)
  1961. goto erspan_link_failed;
  1962. out:
  1963. return err;
  1964. erspan_link_failed:
  1965. rtnl_link_unregister(&ip6gre_tap_ops);
  1966. tap_ops_failed:
  1967. rtnl_link_unregister(&ip6gre_link_ops);
  1968. rtnl_link_failed:
  1969. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1970. add_proto_failed:
  1971. unregister_pernet_device(&ip6gre_net_ops);
  1972. goto out;
  1973. }
  1974. static void __exit ip6gre_fini(void)
  1975. {
  1976. rtnl_link_unregister(&ip6gre_tap_ops);
  1977. rtnl_link_unregister(&ip6gre_link_ops);
  1978. rtnl_link_unregister(&ip6erspan_tap_ops);
  1979. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1980. unregister_pernet_device(&ip6gre_net_ops);
  1981. }
  1982. module_init(ip6gre_init);
  1983. module_exit(ip6gre_fini);
  1984. MODULE_LICENSE("GPL");
  1985. MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
  1986. MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
  1987. MODULE_ALIAS_RTNL_LINK("ip6gre");
  1988. MODULE_ALIAS_RTNL_LINK("ip6gretap");
  1989. MODULE_ALIAS_RTNL_LINK("ip6erspan");
  1990. MODULE_ALIAS_NETDEV("ip6gre0");