ip6_gre.c 57 KB

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