ip6_gre.c 59 KB

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