ip6_gre.c 59 KB

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