ip6_gre.c 41 KB

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