ip6_gre.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734
  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/mroute.h>
  26. #include <linux/init.h>
  27. #include <linux/in6.h>
  28. #include <linux/inetdevice.h>
  29. #include <linux/igmp.h>
  30. #include <linux/netfilter_ipv4.h>
  31. #include <linux/etherdevice.h>
  32. #include <linux/if_ether.h>
  33. #include <linux/hash.h>
  34. #include <linux/if_tunnel.h>
  35. #include <linux/ip6_tunnel.h>
  36. #include <net/sock.h>
  37. #include <net/ip.h>
  38. #include <net/ip_tunnels.h>
  39. #include <net/icmp.h>
  40. #include <net/protocol.h>
  41. #include <net/addrconf.h>
  42. #include <net/arp.h>
  43. #include <net/checksum.h>
  44. #include <net/dsfield.h>
  45. #include <net/inet_ecn.h>
  46. #include <net/xfrm.h>
  47. #include <net/net_namespace.h>
  48. #include <net/netns/generic.h>
  49. #include <net/rtnetlink.h>
  50. #include <net/ipv6.h>
  51. #include <net/ip6_fib.h>
  52. #include <net/ip6_route.h>
  53. #include <net/ip6_tunnel.h>
  54. static bool log_ecn_error = true;
  55. module_param(log_ecn_error, bool, 0644);
  56. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  57. #define HASH_SIZE_SHIFT 5
  58. #define HASH_SIZE (1 << HASH_SIZE_SHIFT)
  59. static int ip6gre_net_id __read_mostly;
  60. struct ip6gre_net {
  61. struct ip6_tnl __rcu *tunnels[4][HASH_SIZE];
  62. struct net_device *fb_tunnel_dev;
  63. };
  64. static struct rtnl_link_ops ip6gre_link_ops __read_mostly;
  65. static struct rtnl_link_ops ip6gre_tap_ops __read_mostly;
  66. static int ip6gre_tunnel_init(struct net_device *dev);
  67. static void ip6gre_tunnel_setup(struct net_device *dev);
  68. static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t);
  69. static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu);
  70. /* Tunnel hash table */
  71. /*
  72. 4 hash tables:
  73. 3: (remote,local)
  74. 2: (remote,*)
  75. 1: (*,local)
  76. 0: (*,*)
  77. We require exact key match i.e. if a key is present in packet
  78. it will match only tunnel with the same key; if it is not present,
  79. it will match only keyless tunnel.
  80. All keysless packets, if not matched configured keyless tunnels
  81. will match fallback tunnel.
  82. */
  83. #define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(HASH_SIZE - 1))
  84. static u32 HASH_ADDR(const struct in6_addr *addr)
  85. {
  86. u32 hash = ipv6_addr_hash(addr);
  87. return hash_32(hash, HASH_SIZE_SHIFT);
  88. }
  89. #define tunnels_r_l tunnels[3]
  90. #define tunnels_r tunnels[2]
  91. #define tunnels_l tunnels[1]
  92. #define tunnels_wc tunnels[0]
  93. /* Given src, dst and key, find appropriate for input tunnel. */
  94. static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev,
  95. const struct in6_addr *remote, const struct in6_addr *local,
  96. __be32 key, __be16 gre_proto)
  97. {
  98. struct net *net = dev_net(dev);
  99. int link = dev->ifindex;
  100. unsigned int h0 = HASH_ADDR(remote);
  101. unsigned int h1 = HASH_KEY(key);
  102. struct ip6_tnl *t, *cand = NULL;
  103. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  104. int dev_type = (gre_proto == htons(ETH_P_TEB)) ?
  105. ARPHRD_ETHER : ARPHRD_IP6GRE;
  106. int score, cand_score = 4;
  107. for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) {
  108. if (!ipv6_addr_equal(local, &t->parms.laddr) ||
  109. !ipv6_addr_equal(remote, &t->parms.raddr) ||
  110. key != t->parms.i_key ||
  111. !(t->dev->flags & IFF_UP))
  112. continue;
  113. if (t->dev->type != ARPHRD_IP6GRE &&
  114. t->dev->type != dev_type)
  115. continue;
  116. score = 0;
  117. if (t->parms.link != link)
  118. score |= 1;
  119. if (t->dev->type != dev_type)
  120. score |= 2;
  121. if (score == 0)
  122. return t;
  123. if (score < cand_score) {
  124. cand = t;
  125. cand_score = score;
  126. }
  127. }
  128. for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) {
  129. if (!ipv6_addr_equal(remote, &t->parms.raddr) ||
  130. key != t->parms.i_key ||
  131. !(t->dev->flags & IFF_UP))
  132. continue;
  133. if (t->dev->type != ARPHRD_IP6GRE &&
  134. t->dev->type != dev_type)
  135. continue;
  136. score = 0;
  137. if (t->parms.link != link)
  138. score |= 1;
  139. if (t->dev->type != dev_type)
  140. score |= 2;
  141. if (score == 0)
  142. return t;
  143. if (score < cand_score) {
  144. cand = t;
  145. cand_score = score;
  146. }
  147. }
  148. for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) {
  149. if ((!ipv6_addr_equal(local, &t->parms.laddr) &&
  150. (!ipv6_addr_equal(local, &t->parms.raddr) ||
  151. !ipv6_addr_is_multicast(local))) ||
  152. key != t->parms.i_key ||
  153. !(t->dev->flags & IFF_UP))
  154. continue;
  155. if (t->dev->type != ARPHRD_IP6GRE &&
  156. t->dev->type != dev_type)
  157. continue;
  158. score = 0;
  159. if (t->parms.link != link)
  160. score |= 1;
  161. if (t->dev->type != dev_type)
  162. score |= 2;
  163. if (score == 0)
  164. return t;
  165. if (score < cand_score) {
  166. cand = t;
  167. cand_score = score;
  168. }
  169. }
  170. for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) {
  171. if (t->parms.i_key != key ||
  172. !(t->dev->flags & IFF_UP))
  173. continue;
  174. if (t->dev->type != ARPHRD_IP6GRE &&
  175. t->dev->type != dev_type)
  176. continue;
  177. score = 0;
  178. if (t->parms.link != link)
  179. score |= 1;
  180. if (t->dev->type != dev_type)
  181. score |= 2;
  182. if (score == 0)
  183. return t;
  184. if (score < cand_score) {
  185. cand = t;
  186. cand_score = score;
  187. }
  188. }
  189. if (cand != NULL)
  190. return cand;
  191. dev = ign->fb_tunnel_dev;
  192. if (dev->flags & IFF_UP)
  193. return netdev_priv(dev);
  194. return NULL;
  195. }
  196. static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign,
  197. const struct __ip6_tnl_parm *p)
  198. {
  199. const struct in6_addr *remote = &p->raddr;
  200. const struct in6_addr *local = &p->laddr;
  201. unsigned int h = HASH_KEY(p->i_key);
  202. int prio = 0;
  203. if (!ipv6_addr_any(local))
  204. prio |= 1;
  205. if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) {
  206. prio |= 2;
  207. h ^= HASH_ADDR(remote);
  208. }
  209. return &ign->tunnels[prio][h];
  210. }
  211. static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign,
  212. const struct ip6_tnl *t)
  213. {
  214. return __ip6gre_bucket(ign, &t->parms);
  215. }
  216. static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t)
  217. {
  218. struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t);
  219. rcu_assign_pointer(t->next, rtnl_dereference(*tp));
  220. rcu_assign_pointer(*tp, t);
  221. }
  222. static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t)
  223. {
  224. struct ip6_tnl __rcu **tp;
  225. struct ip6_tnl *iter;
  226. for (tp = ip6gre_bucket(ign, t);
  227. (iter = rtnl_dereference(*tp)) != NULL;
  228. tp = &iter->next) {
  229. if (t == iter) {
  230. rcu_assign_pointer(*tp, t->next);
  231. break;
  232. }
  233. }
  234. }
  235. static struct ip6_tnl *ip6gre_tunnel_find(struct net *net,
  236. const struct __ip6_tnl_parm *parms,
  237. int type)
  238. {
  239. const struct in6_addr *remote = &parms->raddr;
  240. const struct in6_addr *local = &parms->laddr;
  241. __be32 key = parms->i_key;
  242. int link = parms->link;
  243. struct ip6_tnl *t;
  244. struct ip6_tnl __rcu **tp;
  245. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  246. for (tp = __ip6gre_bucket(ign, parms);
  247. (t = rtnl_dereference(*tp)) != NULL;
  248. tp = &t->next)
  249. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  250. ipv6_addr_equal(remote, &t->parms.raddr) &&
  251. key == t->parms.i_key &&
  252. link == t->parms.link &&
  253. type == t->dev->type)
  254. break;
  255. return t;
  256. }
  257. static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net,
  258. const struct __ip6_tnl_parm *parms, int create)
  259. {
  260. struct ip6_tnl *t, *nt;
  261. struct net_device *dev;
  262. char name[IFNAMSIZ];
  263. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  264. t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE);
  265. if (t && create)
  266. return NULL;
  267. if (t || !create)
  268. return t;
  269. if (parms->name[0])
  270. strlcpy(name, parms->name, IFNAMSIZ);
  271. else
  272. strcpy(name, "ip6gre%d");
  273. dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
  274. ip6gre_tunnel_setup);
  275. if (!dev)
  276. return NULL;
  277. dev_net_set(dev, net);
  278. nt = netdev_priv(dev);
  279. nt->parms = *parms;
  280. dev->rtnl_link_ops = &ip6gre_link_ops;
  281. nt->dev = dev;
  282. nt->net = dev_net(dev);
  283. ip6gre_tnl_link_config(nt, 1);
  284. if (register_netdevice(dev) < 0)
  285. goto failed_free;
  286. /* Can use a lockless transmit, unless we generate output sequences */
  287. if (!(nt->parms.o_flags & GRE_SEQ))
  288. dev->features |= NETIF_F_LLTX;
  289. dev_hold(dev);
  290. ip6gre_tunnel_link(ign, nt);
  291. return nt;
  292. failed_free:
  293. free_netdev(dev);
  294. return NULL;
  295. }
  296. static void ip6gre_tunnel_uninit(struct net_device *dev)
  297. {
  298. struct ip6_tnl *t = netdev_priv(dev);
  299. struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
  300. ip6gre_tunnel_unlink(ign, t);
  301. dev_put(dev);
  302. }
  303. static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  304. u8 type, u8 code, int offset, __be32 info)
  305. {
  306. const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)skb->data;
  307. __be16 *p = (__be16 *)(skb->data + offset);
  308. int grehlen = offset + 4;
  309. struct ip6_tnl *t;
  310. __be16 flags;
  311. flags = p[0];
  312. if (flags&(GRE_CSUM|GRE_KEY|GRE_SEQ|GRE_ROUTING|GRE_VERSION)) {
  313. if (flags&(GRE_VERSION|GRE_ROUTING))
  314. return;
  315. if (flags&GRE_KEY) {
  316. grehlen += 4;
  317. if (flags&GRE_CSUM)
  318. grehlen += 4;
  319. }
  320. }
  321. /* If only 8 bytes returned, keyed message will be dropped here */
  322. if (!pskb_may_pull(skb, grehlen))
  323. return;
  324. ipv6h = (const struct ipv6hdr *)skb->data;
  325. p = (__be16 *)(skb->data + offset);
  326. t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
  327. flags & GRE_KEY ?
  328. *(((__be32 *)p) + (grehlen / 4) - 1) : 0,
  329. p[1]);
  330. if (t == NULL)
  331. return;
  332. switch (type) {
  333. __u32 teli;
  334. struct ipv6_tlv_tnl_enc_lim *tel;
  335. __u32 mtu;
  336. case ICMPV6_DEST_UNREACH:
  337. net_warn_ratelimited("%s: Path to destination invalid or inactive!\n",
  338. t->parms.name);
  339. break;
  340. case ICMPV6_TIME_EXCEED:
  341. if (code == ICMPV6_EXC_HOPLIMIT) {
  342. net_warn_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
  343. t->parms.name);
  344. }
  345. break;
  346. case ICMPV6_PARAMPROB:
  347. teli = 0;
  348. if (code == ICMPV6_HDR_FIELD)
  349. teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
  350. if (teli && teli == info - 2) {
  351. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
  352. if (tel->encap_limit == 0) {
  353. net_warn_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
  354. t->parms.name);
  355. }
  356. } else {
  357. net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
  358. t->parms.name);
  359. }
  360. break;
  361. case ICMPV6_PKT_TOOBIG:
  362. mtu = info - offset;
  363. if (mtu < IPV6_MIN_MTU)
  364. mtu = IPV6_MIN_MTU;
  365. t->dev->mtu = mtu;
  366. break;
  367. }
  368. if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO))
  369. t->err_count++;
  370. else
  371. t->err_count = 1;
  372. t->err_time = jiffies;
  373. }
  374. static int ip6gre_rcv(struct sk_buff *skb)
  375. {
  376. const struct ipv6hdr *ipv6h;
  377. u8 *h;
  378. __be16 flags;
  379. __sum16 csum = 0;
  380. __be32 key = 0;
  381. u32 seqno = 0;
  382. struct ip6_tnl *tunnel;
  383. int offset = 4;
  384. __be16 gre_proto;
  385. int err;
  386. if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
  387. goto drop;
  388. ipv6h = ipv6_hdr(skb);
  389. h = skb->data;
  390. flags = *(__be16 *)h;
  391. if (flags&(GRE_CSUM|GRE_KEY|GRE_ROUTING|GRE_SEQ|GRE_VERSION)) {
  392. /* - Version must be 0.
  393. - We do not support routing headers.
  394. */
  395. if (flags&(GRE_VERSION|GRE_ROUTING))
  396. goto drop;
  397. if (flags&GRE_CSUM) {
  398. csum = skb_checksum_simple_validate(skb);
  399. offset += 4;
  400. }
  401. if (flags&GRE_KEY) {
  402. key = *(__be32 *)(h + offset);
  403. offset += 4;
  404. }
  405. if (flags&GRE_SEQ) {
  406. seqno = ntohl(*(__be32 *)(h + offset));
  407. offset += 4;
  408. }
  409. }
  410. gre_proto = *(__be16 *)(h + 2);
  411. tunnel = ip6gre_tunnel_lookup(skb->dev,
  412. &ipv6h->saddr, &ipv6h->daddr, key,
  413. gre_proto);
  414. if (tunnel) {
  415. struct pcpu_sw_netstats *tstats;
  416. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  417. goto drop;
  418. if (!ip6_tnl_rcv_ctl(tunnel, &ipv6h->daddr, &ipv6h->saddr)) {
  419. tunnel->dev->stats.rx_dropped++;
  420. goto drop;
  421. }
  422. skb->protocol = gre_proto;
  423. /* WCCP version 1 and 2 protocol decoding.
  424. * - Change protocol to IP
  425. * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
  426. */
  427. if (flags == 0 && gre_proto == htons(ETH_P_WCCP)) {
  428. skb->protocol = htons(ETH_P_IP);
  429. if ((*(h + offset) & 0xF0) != 0x40)
  430. offset += 4;
  431. }
  432. skb->mac_header = skb->network_header;
  433. __pskb_pull(skb, offset);
  434. skb_postpull_rcsum(skb, skb_transport_header(skb), offset);
  435. if (((flags&GRE_CSUM) && csum) ||
  436. (!(flags&GRE_CSUM) && tunnel->parms.i_flags&GRE_CSUM)) {
  437. tunnel->dev->stats.rx_crc_errors++;
  438. tunnel->dev->stats.rx_errors++;
  439. goto drop;
  440. }
  441. if (tunnel->parms.i_flags&GRE_SEQ) {
  442. if (!(flags&GRE_SEQ) ||
  443. (tunnel->i_seqno &&
  444. (s32)(seqno - tunnel->i_seqno) < 0)) {
  445. tunnel->dev->stats.rx_fifo_errors++;
  446. tunnel->dev->stats.rx_errors++;
  447. goto drop;
  448. }
  449. tunnel->i_seqno = seqno + 1;
  450. }
  451. /* Warning: All skb pointers will be invalidated! */
  452. if (tunnel->dev->type == ARPHRD_ETHER) {
  453. if (!pskb_may_pull(skb, ETH_HLEN)) {
  454. tunnel->dev->stats.rx_length_errors++;
  455. tunnel->dev->stats.rx_errors++;
  456. goto drop;
  457. }
  458. ipv6h = ipv6_hdr(skb);
  459. skb->protocol = eth_type_trans(skb, tunnel->dev);
  460. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
  461. }
  462. __skb_tunnel_rx(skb, tunnel->dev, tunnel->net);
  463. skb_reset_network_header(skb);
  464. err = IP6_ECN_decapsulate(ipv6h, skb);
  465. if (unlikely(err)) {
  466. if (log_ecn_error)
  467. net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n",
  468. &ipv6h->saddr,
  469. ipv6_get_dsfield(ipv6h));
  470. if (err > 1) {
  471. ++tunnel->dev->stats.rx_frame_errors;
  472. ++tunnel->dev->stats.rx_errors;
  473. goto drop;
  474. }
  475. }
  476. tstats = this_cpu_ptr(tunnel->dev->tstats);
  477. u64_stats_update_begin(&tstats->syncp);
  478. tstats->rx_packets++;
  479. tstats->rx_bytes += skb->len;
  480. u64_stats_update_end(&tstats->syncp);
  481. netif_rx(skb);
  482. return 0;
  483. }
  484. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  485. drop:
  486. kfree_skb(skb);
  487. return 0;
  488. }
  489. struct ipv6_tel_txoption {
  490. struct ipv6_txoptions ops;
  491. __u8 dst_opt[8];
  492. };
  493. static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
  494. {
  495. memset(opt, 0, sizeof(struct ipv6_tel_txoption));
  496. opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
  497. opt->dst_opt[3] = 1;
  498. opt->dst_opt[4] = encap_limit;
  499. opt->dst_opt[5] = IPV6_TLV_PADN;
  500. opt->dst_opt[6] = 1;
  501. opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
  502. opt->ops.opt_nflen = 8;
  503. }
  504. static netdev_tx_t ip6gre_xmit2(struct sk_buff *skb,
  505. struct net_device *dev,
  506. __u8 dsfield,
  507. struct flowi6 *fl6,
  508. int encap_limit,
  509. __u32 *pmtu)
  510. {
  511. struct ip6_tnl *tunnel = netdev_priv(dev);
  512. struct net *net = tunnel->net;
  513. struct net_device *tdev; /* Device to other host */
  514. struct ipv6hdr *ipv6h; /* Our new IP header */
  515. unsigned int max_headroom = 0; /* The extra header space needed */
  516. int gre_hlen;
  517. struct ipv6_tel_txoption opt;
  518. int mtu;
  519. struct dst_entry *dst = NULL, *ndst = NULL;
  520. struct net_device_stats *stats = &tunnel->dev->stats;
  521. int err = -1;
  522. u8 proto;
  523. struct sk_buff *new_skb;
  524. __be16 protocol;
  525. if (dev->type == ARPHRD_ETHER)
  526. IPCB(skb)->flags = 0;
  527. if (dev->header_ops && dev->type == ARPHRD_IP6GRE) {
  528. gre_hlen = 0;
  529. ipv6h = (struct ipv6hdr *)skb->data;
  530. fl6->daddr = ipv6h->daddr;
  531. } else {
  532. gre_hlen = tunnel->hlen;
  533. fl6->daddr = tunnel->parms.raddr;
  534. }
  535. if (!fl6->flowi6_mark)
  536. dst = ip6_tnl_dst_check(tunnel);
  537. if (!dst) {
  538. ndst = ip6_route_output(net, NULL, fl6);
  539. if (ndst->error)
  540. goto tx_err_link_failure;
  541. ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(fl6), NULL, 0);
  542. if (IS_ERR(ndst)) {
  543. err = PTR_ERR(ndst);
  544. ndst = NULL;
  545. goto tx_err_link_failure;
  546. }
  547. dst = ndst;
  548. }
  549. tdev = dst->dev;
  550. if (tdev == dev) {
  551. stats->collisions++;
  552. net_warn_ratelimited("%s: Local routing loop detected!\n",
  553. tunnel->parms.name);
  554. goto tx_err_dst_release;
  555. }
  556. mtu = dst_mtu(dst) - sizeof(*ipv6h);
  557. if (encap_limit >= 0) {
  558. max_headroom += 8;
  559. mtu -= 8;
  560. }
  561. if (mtu < IPV6_MIN_MTU)
  562. mtu = IPV6_MIN_MTU;
  563. if (skb_dst(skb))
  564. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
  565. if (skb->len > mtu) {
  566. *pmtu = mtu;
  567. err = -EMSGSIZE;
  568. goto tx_err_dst_release;
  569. }
  570. if (tunnel->err_count > 0) {
  571. if (time_before(jiffies,
  572. tunnel->err_time + IP6TUNNEL_ERR_TIMEO)) {
  573. tunnel->err_count--;
  574. dst_link_failure(skb);
  575. } else
  576. tunnel->err_count = 0;
  577. }
  578. skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(dev)));
  579. max_headroom += LL_RESERVED_SPACE(tdev) + gre_hlen + dst->header_len;
  580. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  581. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  582. new_skb = skb_realloc_headroom(skb, max_headroom);
  583. if (max_headroom > dev->needed_headroom)
  584. dev->needed_headroom = max_headroom;
  585. if (!new_skb)
  586. goto tx_err_dst_release;
  587. if (skb->sk)
  588. skb_set_owner_w(new_skb, skb->sk);
  589. consume_skb(skb);
  590. skb = new_skb;
  591. }
  592. if (fl6->flowi6_mark) {
  593. skb_dst_set(skb, dst);
  594. ndst = NULL;
  595. } else {
  596. skb_dst_set_noref(skb, dst);
  597. }
  598. proto = NEXTHDR_GRE;
  599. if (encap_limit >= 0) {
  600. init_tel_txopt(&opt, encap_limit);
  601. ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
  602. }
  603. if (likely(!skb->encapsulation)) {
  604. skb_reset_inner_headers(skb);
  605. skb->encapsulation = 1;
  606. }
  607. skb_push(skb, gre_hlen);
  608. skb_reset_network_header(skb);
  609. skb_set_transport_header(skb, sizeof(*ipv6h));
  610. /*
  611. * Push down and install the IP header.
  612. */
  613. ipv6h = ipv6_hdr(skb);
  614. ip6_flow_hdr(ipv6h, INET_ECN_encapsulate(0, dsfield),
  615. ip6_make_flowlabel(net, skb, fl6->flowlabel, false));
  616. ipv6h->hop_limit = tunnel->parms.hop_limit;
  617. ipv6h->nexthdr = proto;
  618. ipv6h->saddr = fl6->saddr;
  619. ipv6h->daddr = fl6->daddr;
  620. ((__be16 *)(ipv6h + 1))[0] = tunnel->parms.o_flags;
  621. protocol = (dev->type == ARPHRD_ETHER) ?
  622. htons(ETH_P_TEB) : skb->protocol;
  623. ((__be16 *)(ipv6h + 1))[1] = protocol;
  624. if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) {
  625. __be32 *ptr = (__be32 *)(((u8 *)ipv6h) + tunnel->hlen - 4);
  626. if (tunnel->parms.o_flags&GRE_SEQ) {
  627. ++tunnel->o_seqno;
  628. *ptr = htonl(tunnel->o_seqno);
  629. ptr--;
  630. }
  631. if (tunnel->parms.o_flags&GRE_KEY) {
  632. *ptr = tunnel->parms.o_key;
  633. ptr--;
  634. }
  635. if (tunnel->parms.o_flags&GRE_CSUM) {
  636. *ptr = 0;
  637. *(__sum16 *)ptr = ip_compute_csum((void *)(ipv6h+1),
  638. skb->len - sizeof(struct ipv6hdr));
  639. }
  640. }
  641. skb_set_inner_protocol(skb, protocol);
  642. ip6tunnel_xmit(skb, dev);
  643. if (ndst)
  644. ip6_tnl_dst_store(tunnel, ndst);
  645. return 0;
  646. tx_err_link_failure:
  647. stats->tx_carrier_errors++;
  648. dst_link_failure(skb);
  649. tx_err_dst_release:
  650. dst_release(ndst);
  651. return err;
  652. }
  653. static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
  654. {
  655. struct ip6_tnl *t = netdev_priv(dev);
  656. const struct iphdr *iph = ip_hdr(skb);
  657. int encap_limit = -1;
  658. struct flowi6 fl6;
  659. __u8 dsfield;
  660. __u32 mtu;
  661. int err;
  662. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  663. encap_limit = t->parms.encap_limit;
  664. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  665. fl6.flowi6_proto = IPPROTO_GRE;
  666. dsfield = ipv4_get_dsfield(iph);
  667. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  668. fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
  669. & IPV6_TCLASS_MASK;
  670. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  671. fl6.flowi6_mark = skb->mark;
  672. err = ip6gre_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
  673. if (err != 0) {
  674. /* XXX: send ICMP error even if DF is not set. */
  675. if (err == -EMSGSIZE)
  676. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  677. htonl(mtu));
  678. return -1;
  679. }
  680. return 0;
  681. }
  682. static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
  683. {
  684. struct ip6_tnl *t = netdev_priv(dev);
  685. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  686. int encap_limit = -1;
  687. __u16 offset;
  688. struct flowi6 fl6;
  689. __u8 dsfield;
  690. __u32 mtu;
  691. int err;
  692. if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
  693. return -1;
  694. offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
  695. if (offset > 0) {
  696. struct ipv6_tlv_tnl_enc_lim *tel;
  697. tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
  698. if (tel->encap_limit == 0) {
  699. icmpv6_send(skb, ICMPV6_PARAMPROB,
  700. ICMPV6_HDR_FIELD, offset + 2);
  701. return -1;
  702. }
  703. encap_limit = tel->encap_limit - 1;
  704. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  705. encap_limit = t->parms.encap_limit;
  706. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  707. fl6.flowi6_proto = IPPROTO_GRE;
  708. dsfield = ipv6_get_dsfield(ipv6h);
  709. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  710. fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
  711. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
  712. fl6.flowlabel |= ip6_flowlabel(ipv6h);
  713. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  714. fl6.flowi6_mark = skb->mark;
  715. err = ip6gre_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
  716. if (err != 0) {
  717. if (err == -EMSGSIZE)
  718. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  719. return -1;
  720. }
  721. return 0;
  722. }
  723. /**
  724. * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  725. * @t: the outgoing tunnel device
  726. * @hdr: IPv6 header from the incoming packet
  727. *
  728. * Description:
  729. * Avoid trivial tunneling loop by checking that tunnel exit-point
  730. * doesn't match source of incoming packet.
  731. *
  732. * Return:
  733. * 1 if conflict,
  734. * 0 else
  735. **/
  736. static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
  737. const struct ipv6hdr *hdr)
  738. {
  739. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  740. }
  741. static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
  742. {
  743. struct ip6_tnl *t = netdev_priv(dev);
  744. int encap_limit = -1;
  745. struct flowi6 fl6;
  746. __u32 mtu;
  747. int err;
  748. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  749. encap_limit = t->parms.encap_limit;
  750. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  751. fl6.flowi6_proto = skb->protocol;
  752. err = ip6gre_xmit2(skb, dev, 0, &fl6, encap_limit, &mtu);
  753. return err;
  754. }
  755. static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
  756. struct net_device *dev)
  757. {
  758. struct ip6_tnl *t = netdev_priv(dev);
  759. struct net_device_stats *stats = &t->dev->stats;
  760. int ret;
  761. if (!ip6_tnl_xmit_ctl(t))
  762. goto tx_err;
  763. switch (skb->protocol) {
  764. case htons(ETH_P_IP):
  765. ret = ip6gre_xmit_ipv4(skb, dev);
  766. break;
  767. case htons(ETH_P_IPV6):
  768. ret = ip6gre_xmit_ipv6(skb, dev);
  769. break;
  770. default:
  771. ret = ip6gre_xmit_other(skb, dev);
  772. break;
  773. }
  774. if (ret < 0)
  775. goto tx_err;
  776. return NETDEV_TX_OK;
  777. tx_err:
  778. stats->tx_errors++;
  779. stats->tx_dropped++;
  780. kfree_skb(skb);
  781. return NETDEV_TX_OK;
  782. }
  783. static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
  784. {
  785. struct net_device *dev = t->dev;
  786. struct __ip6_tnl_parm *p = &t->parms;
  787. struct flowi6 *fl6 = &t->fl.u.ip6;
  788. int addend = sizeof(struct ipv6hdr) + 4;
  789. if (dev->type != ARPHRD_ETHER) {
  790. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  791. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  792. }
  793. /* Set up flowi template */
  794. fl6->saddr = p->laddr;
  795. fl6->daddr = p->raddr;
  796. fl6->flowi6_oif = p->link;
  797. fl6->flowlabel = 0;
  798. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  799. fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  800. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  801. fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  802. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
  803. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  804. if (p->flags&IP6_TNL_F_CAP_XMIT &&
  805. p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
  806. dev->flags |= IFF_POINTOPOINT;
  807. else
  808. dev->flags &= ~IFF_POINTOPOINT;
  809. dev->iflink = p->link;
  810. /* Precalculate GRE options length */
  811. if (t->parms.o_flags&(GRE_CSUM|GRE_KEY|GRE_SEQ)) {
  812. if (t->parms.o_flags&GRE_CSUM)
  813. addend += 4;
  814. if (t->parms.o_flags&GRE_KEY)
  815. addend += 4;
  816. if (t->parms.o_flags&GRE_SEQ)
  817. addend += 4;
  818. }
  819. t->hlen = addend;
  820. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  821. int strict = (ipv6_addr_type(&p->raddr) &
  822. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  823. struct rt6_info *rt = rt6_lookup(t->net,
  824. &p->raddr, &p->laddr,
  825. p->link, strict);
  826. if (rt == NULL)
  827. return;
  828. if (rt->dst.dev) {
  829. dev->hard_header_len = rt->dst.dev->hard_header_len + addend;
  830. if (set_mtu) {
  831. dev->mtu = rt->dst.dev->mtu - addend;
  832. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  833. dev->mtu -= 8;
  834. if (dev->mtu < IPV6_MIN_MTU)
  835. dev->mtu = IPV6_MIN_MTU;
  836. }
  837. }
  838. ip6_rt_put(rt);
  839. }
  840. }
  841. static int ip6gre_tnl_change(struct ip6_tnl *t,
  842. const struct __ip6_tnl_parm *p, int set_mtu)
  843. {
  844. t->parms.laddr = p->laddr;
  845. t->parms.raddr = p->raddr;
  846. t->parms.flags = p->flags;
  847. t->parms.hop_limit = p->hop_limit;
  848. t->parms.encap_limit = p->encap_limit;
  849. t->parms.flowinfo = p->flowinfo;
  850. t->parms.link = p->link;
  851. t->parms.proto = p->proto;
  852. t->parms.i_key = p->i_key;
  853. t->parms.o_key = p->o_key;
  854. t->parms.i_flags = p->i_flags;
  855. t->parms.o_flags = p->o_flags;
  856. ip6_tnl_dst_reset(t);
  857. ip6gre_tnl_link_config(t, set_mtu);
  858. return 0;
  859. }
  860. static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
  861. const struct ip6_tnl_parm2 *u)
  862. {
  863. p->laddr = u->laddr;
  864. p->raddr = u->raddr;
  865. p->flags = u->flags;
  866. p->hop_limit = u->hop_limit;
  867. p->encap_limit = u->encap_limit;
  868. p->flowinfo = u->flowinfo;
  869. p->link = u->link;
  870. p->i_key = u->i_key;
  871. p->o_key = u->o_key;
  872. p->i_flags = u->i_flags;
  873. p->o_flags = u->o_flags;
  874. memcpy(p->name, u->name, sizeof(u->name));
  875. }
  876. static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
  877. const struct __ip6_tnl_parm *p)
  878. {
  879. u->proto = IPPROTO_GRE;
  880. u->laddr = p->laddr;
  881. u->raddr = p->raddr;
  882. u->flags = p->flags;
  883. u->hop_limit = p->hop_limit;
  884. u->encap_limit = p->encap_limit;
  885. u->flowinfo = p->flowinfo;
  886. u->link = p->link;
  887. u->i_key = p->i_key;
  888. u->o_key = p->o_key;
  889. u->i_flags = p->i_flags;
  890. u->o_flags = p->o_flags;
  891. memcpy(u->name, p->name, sizeof(u->name));
  892. }
  893. static int ip6gre_tunnel_ioctl(struct net_device *dev,
  894. struct ifreq *ifr, int cmd)
  895. {
  896. int err = 0;
  897. struct ip6_tnl_parm2 p;
  898. struct __ip6_tnl_parm p1;
  899. struct ip6_tnl *t = netdev_priv(dev);
  900. struct net *net = t->net;
  901. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  902. switch (cmd) {
  903. case SIOCGETTUNNEL:
  904. if (dev == ign->fb_tunnel_dev) {
  905. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  906. err = -EFAULT;
  907. break;
  908. }
  909. ip6gre_tnl_parm_from_user(&p1, &p);
  910. t = ip6gre_tunnel_locate(net, &p1, 0);
  911. if (t == NULL)
  912. t = netdev_priv(dev);
  913. }
  914. memset(&p, 0, sizeof(p));
  915. ip6gre_tnl_parm_to_user(&p, &t->parms);
  916. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  917. err = -EFAULT;
  918. break;
  919. case SIOCADDTUNNEL:
  920. case SIOCCHGTUNNEL:
  921. err = -EPERM;
  922. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  923. goto done;
  924. err = -EFAULT;
  925. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  926. goto done;
  927. err = -EINVAL;
  928. if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
  929. goto done;
  930. if (!(p.i_flags&GRE_KEY))
  931. p.i_key = 0;
  932. if (!(p.o_flags&GRE_KEY))
  933. p.o_key = 0;
  934. ip6gre_tnl_parm_from_user(&p1, &p);
  935. t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
  936. if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  937. if (t != NULL) {
  938. if (t->dev != dev) {
  939. err = -EEXIST;
  940. break;
  941. }
  942. } else {
  943. t = netdev_priv(dev);
  944. ip6gre_tunnel_unlink(ign, t);
  945. synchronize_net();
  946. ip6gre_tnl_change(t, &p1, 1);
  947. ip6gre_tunnel_link(ign, t);
  948. netdev_state_change(dev);
  949. }
  950. }
  951. if (t) {
  952. err = 0;
  953. memset(&p, 0, sizeof(p));
  954. ip6gre_tnl_parm_to_user(&p, &t->parms);
  955. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  956. err = -EFAULT;
  957. } else
  958. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  959. break;
  960. case SIOCDELTUNNEL:
  961. err = -EPERM;
  962. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  963. goto done;
  964. if (dev == ign->fb_tunnel_dev) {
  965. err = -EFAULT;
  966. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  967. goto done;
  968. err = -ENOENT;
  969. ip6gre_tnl_parm_from_user(&p1, &p);
  970. t = ip6gre_tunnel_locate(net, &p1, 0);
  971. if (t == NULL)
  972. goto done;
  973. err = -EPERM;
  974. if (t == netdev_priv(ign->fb_tunnel_dev))
  975. goto done;
  976. dev = t->dev;
  977. }
  978. unregister_netdevice(dev);
  979. err = 0;
  980. break;
  981. default:
  982. err = -EINVAL;
  983. }
  984. done:
  985. return err;
  986. }
  987. static int ip6gre_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  988. {
  989. if (new_mtu < 68 ||
  990. new_mtu > 0xFFF8 - dev->hard_header_len)
  991. return -EINVAL;
  992. dev->mtu = new_mtu;
  993. return 0;
  994. }
  995. static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
  996. unsigned short type,
  997. const void *daddr, const void *saddr, unsigned int len)
  998. {
  999. struct ip6_tnl *t = netdev_priv(dev);
  1000. struct ipv6hdr *ipv6h = (struct ipv6hdr *)skb_push(skb, t->hlen);
  1001. __be16 *p = (__be16 *)(ipv6h+1);
  1002. ip6_flow_hdr(ipv6h, 0,
  1003. ip6_make_flowlabel(dev_net(dev), skb,
  1004. t->fl.u.ip6.flowlabel, false));
  1005. ipv6h->hop_limit = t->parms.hop_limit;
  1006. ipv6h->nexthdr = NEXTHDR_GRE;
  1007. ipv6h->saddr = t->parms.laddr;
  1008. ipv6h->daddr = t->parms.raddr;
  1009. p[0] = t->parms.o_flags;
  1010. p[1] = htons(type);
  1011. /*
  1012. * Set the source hardware address.
  1013. */
  1014. if (saddr)
  1015. memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
  1016. if (daddr)
  1017. memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
  1018. if (!ipv6_addr_any(&ipv6h->daddr))
  1019. return t->hlen;
  1020. return -t->hlen;
  1021. }
  1022. static const struct header_ops ip6gre_header_ops = {
  1023. .create = ip6gre_header,
  1024. };
  1025. static const struct net_device_ops ip6gre_netdev_ops = {
  1026. .ndo_init = ip6gre_tunnel_init,
  1027. .ndo_uninit = ip6gre_tunnel_uninit,
  1028. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1029. .ndo_do_ioctl = ip6gre_tunnel_ioctl,
  1030. .ndo_change_mtu = ip6gre_tunnel_change_mtu,
  1031. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1032. };
  1033. static void ip6gre_dev_free(struct net_device *dev)
  1034. {
  1035. free_percpu(dev->tstats);
  1036. free_netdev(dev);
  1037. }
  1038. static void ip6gre_tunnel_setup(struct net_device *dev)
  1039. {
  1040. struct ip6_tnl *t;
  1041. dev->netdev_ops = &ip6gre_netdev_ops;
  1042. dev->destructor = ip6gre_dev_free;
  1043. dev->type = ARPHRD_IP6GRE;
  1044. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr) + 4;
  1045. dev->mtu = ETH_DATA_LEN - sizeof(struct ipv6hdr) - 4;
  1046. t = netdev_priv(dev);
  1047. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1048. dev->mtu -= 8;
  1049. dev->flags |= IFF_NOARP;
  1050. dev->iflink = 0;
  1051. dev->addr_len = sizeof(struct in6_addr);
  1052. netif_keep_dst(dev);
  1053. }
  1054. static int ip6gre_tunnel_init(struct net_device *dev)
  1055. {
  1056. struct ip6_tnl *tunnel;
  1057. int i;
  1058. tunnel = netdev_priv(dev);
  1059. tunnel->dev = dev;
  1060. tunnel->net = dev_net(dev);
  1061. strcpy(tunnel->parms.name, dev->name);
  1062. memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
  1063. memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
  1064. if (ipv6_addr_any(&tunnel->parms.raddr))
  1065. dev->header_ops = &ip6gre_header_ops;
  1066. dev->tstats = alloc_percpu(struct pcpu_sw_netstats);
  1067. if (!dev->tstats)
  1068. return -ENOMEM;
  1069. for_each_possible_cpu(i) {
  1070. struct pcpu_sw_netstats *ip6gre_tunnel_stats;
  1071. ip6gre_tunnel_stats = per_cpu_ptr(dev->tstats, i);
  1072. u64_stats_init(&ip6gre_tunnel_stats->syncp);
  1073. }
  1074. return 0;
  1075. }
  1076. static void ip6gre_fb_tunnel_init(struct net_device *dev)
  1077. {
  1078. struct ip6_tnl *tunnel = netdev_priv(dev);
  1079. tunnel->dev = dev;
  1080. tunnel->net = dev_net(dev);
  1081. strcpy(tunnel->parms.name, dev->name);
  1082. tunnel->hlen = sizeof(struct ipv6hdr) + 4;
  1083. dev_hold(dev);
  1084. }
  1085. static struct inet6_protocol ip6gre_protocol __read_mostly = {
  1086. .handler = ip6gre_rcv,
  1087. .err_handler = ip6gre_err,
  1088. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  1089. };
  1090. static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)
  1091. {
  1092. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1093. struct net_device *dev, *aux;
  1094. int prio;
  1095. for_each_netdev_safe(net, dev, aux)
  1096. if (dev->rtnl_link_ops == &ip6gre_link_ops ||
  1097. dev->rtnl_link_ops == &ip6gre_tap_ops)
  1098. unregister_netdevice_queue(dev, head);
  1099. for (prio = 0; prio < 4; prio++) {
  1100. int h;
  1101. for (h = 0; h < HASH_SIZE; h++) {
  1102. struct ip6_tnl *t;
  1103. t = rtnl_dereference(ign->tunnels[prio][h]);
  1104. while (t != NULL) {
  1105. /* If dev is in the same netns, it has already
  1106. * been added to the list by the previous loop.
  1107. */
  1108. if (!net_eq(dev_net(t->dev), net))
  1109. unregister_netdevice_queue(t->dev,
  1110. head);
  1111. t = rtnl_dereference(t->next);
  1112. }
  1113. }
  1114. }
  1115. }
  1116. static int __net_init ip6gre_init_net(struct net *net)
  1117. {
  1118. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1119. int err;
  1120. ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
  1121. NET_NAME_UNKNOWN,
  1122. ip6gre_tunnel_setup);
  1123. if (!ign->fb_tunnel_dev) {
  1124. err = -ENOMEM;
  1125. goto err_alloc_dev;
  1126. }
  1127. dev_net_set(ign->fb_tunnel_dev, net);
  1128. /* FB netdevice is special: we have one, and only one per netns.
  1129. * Allowing to move it to another netns is clearly unsafe.
  1130. */
  1131. ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
  1132. ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
  1133. ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
  1134. err = register_netdev(ign->fb_tunnel_dev);
  1135. if (err)
  1136. goto err_reg_dev;
  1137. rcu_assign_pointer(ign->tunnels_wc[0],
  1138. netdev_priv(ign->fb_tunnel_dev));
  1139. return 0;
  1140. err_reg_dev:
  1141. ip6gre_dev_free(ign->fb_tunnel_dev);
  1142. err_alloc_dev:
  1143. return err;
  1144. }
  1145. static void __net_exit ip6gre_exit_net(struct net *net)
  1146. {
  1147. LIST_HEAD(list);
  1148. rtnl_lock();
  1149. ip6gre_destroy_tunnels(net, &list);
  1150. unregister_netdevice_many(&list);
  1151. rtnl_unlock();
  1152. }
  1153. static struct pernet_operations ip6gre_net_ops = {
  1154. .init = ip6gre_init_net,
  1155. .exit = ip6gre_exit_net,
  1156. .id = &ip6gre_net_id,
  1157. .size = sizeof(struct ip6gre_net),
  1158. };
  1159. static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
  1160. {
  1161. __be16 flags;
  1162. if (!data)
  1163. return 0;
  1164. flags = 0;
  1165. if (data[IFLA_GRE_IFLAGS])
  1166. flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
  1167. if (data[IFLA_GRE_OFLAGS])
  1168. flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
  1169. if (flags & (GRE_VERSION|GRE_ROUTING))
  1170. return -EINVAL;
  1171. return 0;
  1172. }
  1173. static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[])
  1174. {
  1175. struct in6_addr daddr;
  1176. if (tb[IFLA_ADDRESS]) {
  1177. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  1178. return -EINVAL;
  1179. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  1180. return -EADDRNOTAVAIL;
  1181. }
  1182. if (!data)
  1183. goto out;
  1184. if (data[IFLA_GRE_REMOTE]) {
  1185. nla_memcpy(&daddr, data[IFLA_GRE_REMOTE], sizeof(struct in6_addr));
  1186. if (ipv6_addr_any(&daddr))
  1187. return -EINVAL;
  1188. }
  1189. out:
  1190. return ip6gre_tunnel_validate(tb, data);
  1191. }
  1192. static void ip6gre_netlink_parms(struct nlattr *data[],
  1193. struct __ip6_tnl_parm *parms)
  1194. {
  1195. memset(parms, 0, sizeof(*parms));
  1196. if (!data)
  1197. return;
  1198. if (data[IFLA_GRE_LINK])
  1199. parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
  1200. if (data[IFLA_GRE_IFLAGS])
  1201. parms->i_flags = nla_get_be16(data[IFLA_GRE_IFLAGS]);
  1202. if (data[IFLA_GRE_OFLAGS])
  1203. parms->o_flags = nla_get_be16(data[IFLA_GRE_OFLAGS]);
  1204. if (data[IFLA_GRE_IKEY])
  1205. parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
  1206. if (data[IFLA_GRE_OKEY])
  1207. parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
  1208. if (data[IFLA_GRE_LOCAL])
  1209. nla_memcpy(&parms->laddr, data[IFLA_GRE_LOCAL], sizeof(struct in6_addr));
  1210. if (data[IFLA_GRE_REMOTE])
  1211. nla_memcpy(&parms->raddr, data[IFLA_GRE_REMOTE], sizeof(struct in6_addr));
  1212. if (data[IFLA_GRE_TTL])
  1213. parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
  1214. if (data[IFLA_GRE_ENCAP_LIMIT])
  1215. parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
  1216. if (data[IFLA_GRE_FLOWINFO])
  1217. parms->flowinfo = nla_get_u32(data[IFLA_GRE_FLOWINFO]);
  1218. if (data[IFLA_GRE_FLAGS])
  1219. parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
  1220. }
  1221. static int ip6gre_tap_init(struct net_device *dev)
  1222. {
  1223. struct ip6_tnl *tunnel;
  1224. tunnel = netdev_priv(dev);
  1225. tunnel->dev = dev;
  1226. tunnel->net = dev_net(dev);
  1227. strcpy(tunnel->parms.name, dev->name);
  1228. ip6gre_tnl_link_config(tunnel, 1);
  1229. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1230. if (!dev->tstats)
  1231. return -ENOMEM;
  1232. return 0;
  1233. }
  1234. static const struct net_device_ops ip6gre_tap_netdev_ops = {
  1235. .ndo_init = ip6gre_tap_init,
  1236. .ndo_uninit = ip6gre_tunnel_uninit,
  1237. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1238. .ndo_set_mac_address = eth_mac_addr,
  1239. .ndo_validate_addr = eth_validate_addr,
  1240. .ndo_change_mtu = ip6gre_tunnel_change_mtu,
  1241. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1242. };
  1243. static void ip6gre_tap_setup(struct net_device *dev)
  1244. {
  1245. ether_setup(dev);
  1246. dev->netdev_ops = &ip6gre_tap_netdev_ops;
  1247. dev->destructor = ip6gre_dev_free;
  1248. dev->iflink = 0;
  1249. dev->features |= NETIF_F_NETNS_LOCAL;
  1250. }
  1251. static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
  1252. struct nlattr *tb[], struct nlattr *data[])
  1253. {
  1254. struct ip6_tnl *nt;
  1255. struct net *net = dev_net(dev);
  1256. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1257. int err;
  1258. nt = netdev_priv(dev);
  1259. ip6gre_netlink_parms(data, &nt->parms);
  1260. if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
  1261. return -EEXIST;
  1262. if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
  1263. eth_hw_addr_random(dev);
  1264. nt->dev = dev;
  1265. nt->net = dev_net(dev);
  1266. ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
  1267. /* Can use a lockless transmit, unless we generate output sequences */
  1268. if (!(nt->parms.o_flags & GRE_SEQ))
  1269. dev->features |= NETIF_F_LLTX;
  1270. err = register_netdevice(dev);
  1271. if (err)
  1272. goto out;
  1273. dev_hold(dev);
  1274. ip6gre_tunnel_link(ign, nt);
  1275. out:
  1276. return err;
  1277. }
  1278. static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
  1279. struct nlattr *data[])
  1280. {
  1281. struct ip6_tnl *t, *nt = netdev_priv(dev);
  1282. struct net *net = nt->net;
  1283. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1284. struct __ip6_tnl_parm p;
  1285. if (dev == ign->fb_tunnel_dev)
  1286. return -EINVAL;
  1287. ip6gre_netlink_parms(data, &p);
  1288. t = ip6gre_tunnel_locate(net, &p, 0);
  1289. if (t) {
  1290. if (t->dev != dev)
  1291. return -EEXIST;
  1292. } else {
  1293. t = nt;
  1294. ip6gre_tunnel_unlink(ign, t);
  1295. ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
  1296. ip6gre_tunnel_link(ign, t);
  1297. netdev_state_change(dev);
  1298. }
  1299. return 0;
  1300. }
  1301. static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
  1302. {
  1303. struct net *net = dev_net(dev);
  1304. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1305. if (dev != ign->fb_tunnel_dev)
  1306. unregister_netdevice_queue(dev, head);
  1307. }
  1308. static size_t ip6gre_get_size(const struct net_device *dev)
  1309. {
  1310. return
  1311. /* IFLA_GRE_LINK */
  1312. nla_total_size(4) +
  1313. /* IFLA_GRE_IFLAGS */
  1314. nla_total_size(2) +
  1315. /* IFLA_GRE_OFLAGS */
  1316. nla_total_size(2) +
  1317. /* IFLA_GRE_IKEY */
  1318. nla_total_size(4) +
  1319. /* IFLA_GRE_OKEY */
  1320. nla_total_size(4) +
  1321. /* IFLA_GRE_LOCAL */
  1322. nla_total_size(sizeof(struct in6_addr)) +
  1323. /* IFLA_GRE_REMOTE */
  1324. nla_total_size(sizeof(struct in6_addr)) +
  1325. /* IFLA_GRE_TTL */
  1326. nla_total_size(1) +
  1327. /* IFLA_GRE_TOS */
  1328. nla_total_size(1) +
  1329. /* IFLA_GRE_ENCAP_LIMIT */
  1330. nla_total_size(1) +
  1331. /* IFLA_GRE_FLOWINFO */
  1332. nla_total_size(4) +
  1333. /* IFLA_GRE_FLAGS */
  1334. nla_total_size(4) +
  1335. 0;
  1336. }
  1337. static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1338. {
  1339. struct ip6_tnl *t = netdev_priv(dev);
  1340. struct __ip6_tnl_parm *p = &t->parms;
  1341. if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
  1342. nla_put_be16(skb, IFLA_GRE_IFLAGS, p->i_flags) ||
  1343. nla_put_be16(skb, IFLA_GRE_OFLAGS, p->o_flags) ||
  1344. nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
  1345. nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
  1346. nla_put(skb, IFLA_GRE_LOCAL, sizeof(struct in6_addr), &p->laddr) ||
  1347. nla_put(skb, IFLA_GRE_REMOTE, sizeof(struct in6_addr), &p->raddr) ||
  1348. nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
  1349. /*nla_put_u8(skb, IFLA_GRE_TOS, t->priority) ||*/
  1350. nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
  1351. nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
  1352. nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags))
  1353. goto nla_put_failure;
  1354. return 0;
  1355. nla_put_failure:
  1356. return -EMSGSIZE;
  1357. }
  1358. static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
  1359. [IFLA_GRE_LINK] = { .type = NLA_U32 },
  1360. [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
  1361. [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
  1362. [IFLA_GRE_IKEY] = { .type = NLA_U32 },
  1363. [IFLA_GRE_OKEY] = { .type = NLA_U32 },
  1364. [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
  1365. [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
  1366. [IFLA_GRE_TTL] = { .type = NLA_U8 },
  1367. [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
  1368. [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 },
  1369. [IFLA_GRE_FLAGS] = { .type = NLA_U32 },
  1370. };
  1371. static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
  1372. .kind = "ip6gre",
  1373. .maxtype = IFLA_GRE_MAX,
  1374. .policy = ip6gre_policy,
  1375. .priv_size = sizeof(struct ip6_tnl),
  1376. .setup = ip6gre_tunnel_setup,
  1377. .validate = ip6gre_tunnel_validate,
  1378. .newlink = ip6gre_newlink,
  1379. .changelink = ip6gre_changelink,
  1380. .dellink = ip6gre_dellink,
  1381. .get_size = ip6gre_get_size,
  1382. .fill_info = ip6gre_fill_info,
  1383. };
  1384. static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
  1385. .kind = "ip6gretap",
  1386. .maxtype = IFLA_GRE_MAX,
  1387. .policy = ip6gre_policy,
  1388. .priv_size = sizeof(struct ip6_tnl),
  1389. .setup = ip6gre_tap_setup,
  1390. .validate = ip6gre_tap_validate,
  1391. .newlink = ip6gre_newlink,
  1392. .changelink = ip6gre_changelink,
  1393. .get_size = ip6gre_get_size,
  1394. .fill_info = ip6gre_fill_info,
  1395. };
  1396. /*
  1397. * And now the modules code and kernel interface.
  1398. */
  1399. static int __init ip6gre_init(void)
  1400. {
  1401. int err;
  1402. pr_info("GRE over IPv6 tunneling driver\n");
  1403. err = register_pernet_device(&ip6gre_net_ops);
  1404. if (err < 0)
  1405. return err;
  1406. err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1407. if (err < 0) {
  1408. pr_info("%s: can't add protocol\n", __func__);
  1409. goto add_proto_failed;
  1410. }
  1411. err = rtnl_link_register(&ip6gre_link_ops);
  1412. if (err < 0)
  1413. goto rtnl_link_failed;
  1414. err = rtnl_link_register(&ip6gre_tap_ops);
  1415. if (err < 0)
  1416. goto tap_ops_failed;
  1417. out:
  1418. return err;
  1419. tap_ops_failed:
  1420. rtnl_link_unregister(&ip6gre_link_ops);
  1421. rtnl_link_failed:
  1422. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1423. add_proto_failed:
  1424. unregister_pernet_device(&ip6gre_net_ops);
  1425. goto out;
  1426. }
  1427. static void __exit ip6gre_fini(void)
  1428. {
  1429. rtnl_link_unregister(&ip6gre_tap_ops);
  1430. rtnl_link_unregister(&ip6gre_link_ops);
  1431. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1432. unregister_pernet_device(&ip6gre_net_ops);
  1433. }
  1434. module_init(ip6gre_init);
  1435. module_exit(ip6gre_fini);
  1436. MODULE_LICENSE("GPL");
  1437. MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
  1438. MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
  1439. MODULE_ALIAS_RTNL_LINK("ip6gre");
  1440. MODULE_ALIAS_RTNL_LINK("ip6gretap");
  1441. MODULE_ALIAS_NETDEV("ip6gre0");