ip6_gre.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719
  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 IPV6_TCLASS_SHIFT 20
  58. #define HASH_SIZE_SHIFT 5
  59. #define HASH_SIZE (1 << HASH_SIZE_SHIFT)
  60. static int ip6gre_net_id __read_mostly;
  61. struct ip6gre_net {
  62. struct ip6_tnl __rcu *tunnels[4][HASH_SIZE];
  63. struct net_device *fb_tunnel_dev;
  64. };
  65. static struct rtnl_link_ops ip6gre_link_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 net *net = dev_net(dev);
  296. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  297. ip6gre_tunnel_unlink(ign, netdev_priv(dev));
  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. switch (skb->ip_summed) {
  396. case CHECKSUM_COMPLETE:
  397. csum = csum_fold(skb->csum);
  398. if (!csum)
  399. break;
  400. /* fall through */
  401. case CHECKSUM_NONE:
  402. skb->csum = 0;
  403. csum = __skb_checksum_complete(skb);
  404. skb->ip_summed = CHECKSUM_COMPLETE;
  405. }
  406. offset += 4;
  407. }
  408. if (flags&GRE_KEY) {
  409. key = *(__be32 *)(h + offset);
  410. offset += 4;
  411. }
  412. if (flags&GRE_SEQ) {
  413. seqno = ntohl(*(__be32 *)(h + offset));
  414. offset += 4;
  415. }
  416. }
  417. gre_proto = *(__be16 *)(h + 2);
  418. tunnel = ip6gre_tunnel_lookup(skb->dev,
  419. &ipv6h->saddr, &ipv6h->daddr, key,
  420. gre_proto);
  421. if (tunnel) {
  422. struct pcpu_tstats *tstats;
  423. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  424. goto drop;
  425. if (!ip6_tnl_rcv_ctl(tunnel, &ipv6h->daddr, &ipv6h->saddr)) {
  426. tunnel->dev->stats.rx_dropped++;
  427. goto drop;
  428. }
  429. skb->protocol = gre_proto;
  430. /* WCCP version 1 and 2 protocol decoding.
  431. * - Change protocol to IP
  432. * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
  433. */
  434. if (flags == 0 && gre_proto == htons(ETH_P_WCCP)) {
  435. skb->protocol = htons(ETH_P_IP);
  436. if ((*(h + offset) & 0xF0) != 0x40)
  437. offset += 4;
  438. }
  439. skb->mac_header = skb->network_header;
  440. __pskb_pull(skb, offset);
  441. skb_postpull_rcsum(skb, skb_transport_header(skb), offset);
  442. if (((flags&GRE_CSUM) && csum) ||
  443. (!(flags&GRE_CSUM) && tunnel->parms.i_flags&GRE_CSUM)) {
  444. tunnel->dev->stats.rx_crc_errors++;
  445. tunnel->dev->stats.rx_errors++;
  446. goto drop;
  447. }
  448. if (tunnel->parms.i_flags&GRE_SEQ) {
  449. if (!(flags&GRE_SEQ) ||
  450. (tunnel->i_seqno &&
  451. (s32)(seqno - tunnel->i_seqno) < 0)) {
  452. tunnel->dev->stats.rx_fifo_errors++;
  453. tunnel->dev->stats.rx_errors++;
  454. goto drop;
  455. }
  456. tunnel->i_seqno = seqno + 1;
  457. }
  458. /* Warning: All skb pointers will be invalidated! */
  459. if (tunnel->dev->type == ARPHRD_ETHER) {
  460. if (!pskb_may_pull(skb, ETH_HLEN)) {
  461. tunnel->dev->stats.rx_length_errors++;
  462. tunnel->dev->stats.rx_errors++;
  463. goto drop;
  464. }
  465. ipv6h = ipv6_hdr(skb);
  466. skb->protocol = eth_type_trans(skb, tunnel->dev);
  467. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
  468. }
  469. __skb_tunnel_rx(skb, tunnel->dev, tunnel->net);
  470. skb_reset_network_header(skb);
  471. err = IP6_ECN_decapsulate(ipv6h, skb);
  472. if (unlikely(err)) {
  473. if (log_ecn_error)
  474. net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n",
  475. &ipv6h->saddr,
  476. ipv6_get_dsfield(ipv6h));
  477. if (err > 1) {
  478. ++tunnel->dev->stats.rx_frame_errors;
  479. ++tunnel->dev->stats.rx_errors;
  480. goto drop;
  481. }
  482. }
  483. tstats = this_cpu_ptr(tunnel->dev->tstats);
  484. u64_stats_update_begin(&tstats->syncp);
  485. tstats->rx_packets++;
  486. tstats->rx_bytes += skb->len;
  487. u64_stats_update_end(&tstats->syncp);
  488. netif_rx(skb);
  489. return 0;
  490. }
  491. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  492. drop:
  493. kfree_skb(skb);
  494. return 0;
  495. }
  496. struct ipv6_tel_txoption {
  497. struct ipv6_txoptions ops;
  498. __u8 dst_opt[8];
  499. };
  500. static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
  501. {
  502. memset(opt, 0, sizeof(struct ipv6_tel_txoption));
  503. opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
  504. opt->dst_opt[3] = 1;
  505. opt->dst_opt[4] = encap_limit;
  506. opt->dst_opt[5] = IPV6_TLV_PADN;
  507. opt->dst_opt[6] = 1;
  508. opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
  509. opt->ops.opt_nflen = 8;
  510. }
  511. static netdev_tx_t ip6gre_xmit2(struct sk_buff *skb,
  512. struct net_device *dev,
  513. __u8 dsfield,
  514. struct flowi6 *fl6,
  515. int encap_limit,
  516. __u32 *pmtu)
  517. {
  518. struct net *net = dev_net(dev);
  519. struct ip6_tnl *tunnel = netdev_priv(dev);
  520. struct net_device *tdev; /* Device to other host */
  521. struct ipv6hdr *ipv6h; /* Our new IP header */
  522. unsigned int max_headroom = 0; /* The extra header space needed */
  523. int gre_hlen;
  524. struct ipv6_tel_txoption opt;
  525. int mtu;
  526. struct dst_entry *dst = NULL, *ndst = NULL;
  527. struct net_device_stats *stats = &tunnel->dev->stats;
  528. int err = -1;
  529. u8 proto;
  530. struct sk_buff *new_skb;
  531. if (dev->type == ARPHRD_ETHER)
  532. IPCB(skb)->flags = 0;
  533. if (dev->header_ops && dev->type == ARPHRD_IP6GRE) {
  534. gre_hlen = 0;
  535. ipv6h = (struct ipv6hdr *)skb->data;
  536. fl6->daddr = ipv6h->daddr;
  537. } else {
  538. gre_hlen = tunnel->hlen;
  539. fl6->daddr = tunnel->parms.raddr;
  540. }
  541. if (!fl6->flowi6_mark)
  542. dst = ip6_tnl_dst_check(tunnel);
  543. if (!dst) {
  544. ndst = ip6_route_output(net, NULL, fl6);
  545. if (ndst->error)
  546. goto tx_err_link_failure;
  547. ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(fl6), NULL, 0);
  548. if (IS_ERR(ndst)) {
  549. err = PTR_ERR(ndst);
  550. ndst = NULL;
  551. goto tx_err_link_failure;
  552. }
  553. dst = ndst;
  554. }
  555. tdev = dst->dev;
  556. if (tdev == dev) {
  557. stats->collisions++;
  558. net_warn_ratelimited("%s: Local routing loop detected!\n",
  559. tunnel->parms.name);
  560. goto tx_err_dst_release;
  561. }
  562. mtu = dst_mtu(dst) - sizeof(*ipv6h);
  563. if (encap_limit >= 0) {
  564. max_headroom += 8;
  565. mtu -= 8;
  566. }
  567. if (mtu < IPV6_MIN_MTU)
  568. mtu = IPV6_MIN_MTU;
  569. if (skb_dst(skb))
  570. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
  571. if (skb->len > mtu) {
  572. *pmtu = mtu;
  573. err = -EMSGSIZE;
  574. goto tx_err_dst_release;
  575. }
  576. if (tunnel->err_count > 0) {
  577. if (time_before(jiffies,
  578. tunnel->err_time + IP6TUNNEL_ERR_TIMEO)) {
  579. tunnel->err_count--;
  580. dst_link_failure(skb);
  581. } else
  582. tunnel->err_count = 0;
  583. }
  584. skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(dev)));
  585. max_headroom += LL_RESERVED_SPACE(tdev) + gre_hlen + dst->header_len;
  586. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  587. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  588. new_skb = skb_realloc_headroom(skb, max_headroom);
  589. if (max_headroom > dev->needed_headroom)
  590. dev->needed_headroom = max_headroom;
  591. if (!new_skb)
  592. goto tx_err_dst_release;
  593. if (skb->sk)
  594. skb_set_owner_w(new_skb, skb->sk);
  595. consume_skb(skb);
  596. skb = new_skb;
  597. }
  598. if (fl6->flowi6_mark) {
  599. skb_dst_set(skb, dst);
  600. ndst = NULL;
  601. } else {
  602. skb_dst_set_noref(skb, dst);
  603. }
  604. proto = NEXTHDR_GRE;
  605. if (encap_limit >= 0) {
  606. init_tel_txopt(&opt, encap_limit);
  607. ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
  608. }
  609. if (likely(!skb->encapsulation)) {
  610. skb_reset_inner_headers(skb);
  611. skb->encapsulation = 1;
  612. }
  613. skb_push(skb, gre_hlen);
  614. skb_reset_network_header(skb);
  615. skb_set_transport_header(skb, sizeof(*ipv6h));
  616. /*
  617. * Push down and install the IP header.
  618. */
  619. ipv6h = ipv6_hdr(skb);
  620. ip6_flow_hdr(ipv6h, INET_ECN_encapsulate(0, dsfield), fl6->flowlabel);
  621. ipv6h->hop_limit = tunnel->parms.hop_limit;
  622. ipv6h->nexthdr = proto;
  623. ipv6h->saddr = fl6->saddr;
  624. ipv6h->daddr = fl6->daddr;
  625. ((__be16 *)(ipv6h + 1))[0] = tunnel->parms.o_flags;
  626. ((__be16 *)(ipv6h + 1))[1] = (dev->type == ARPHRD_ETHER) ?
  627. htons(ETH_P_TEB) : skb->protocol;
  628. if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) {
  629. __be32 *ptr = (__be32 *)(((u8 *)ipv6h) + tunnel->hlen - 4);
  630. if (tunnel->parms.o_flags&GRE_SEQ) {
  631. ++tunnel->o_seqno;
  632. *ptr = htonl(tunnel->o_seqno);
  633. ptr--;
  634. }
  635. if (tunnel->parms.o_flags&GRE_KEY) {
  636. *ptr = tunnel->parms.o_key;
  637. ptr--;
  638. }
  639. if (tunnel->parms.o_flags&GRE_CSUM) {
  640. *ptr = 0;
  641. *(__sum16 *)ptr = ip_compute_csum((void *)(ipv6h+1),
  642. skb->len - sizeof(struct ipv6hdr));
  643. }
  644. }
  645. ip6tunnel_xmit(skb, dev);
  646. if (ndst)
  647. ip6_tnl_dst_store(tunnel, ndst);
  648. return 0;
  649. tx_err_link_failure:
  650. stats->tx_carrier_errors++;
  651. dst_link_failure(skb);
  652. tx_err_dst_release:
  653. dst_release(ndst);
  654. return err;
  655. }
  656. static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
  657. {
  658. struct ip6_tnl *t = netdev_priv(dev);
  659. const struct iphdr *iph = ip_hdr(skb);
  660. int encap_limit = -1;
  661. struct flowi6 fl6;
  662. __u8 dsfield;
  663. __u32 mtu;
  664. int err;
  665. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  666. encap_limit = t->parms.encap_limit;
  667. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  668. fl6.flowi6_proto = IPPROTO_IPIP;
  669. dsfield = ipv4_get_dsfield(iph);
  670. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  671. fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
  672. & IPV6_TCLASS_MASK;
  673. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  674. fl6.flowi6_mark = skb->mark;
  675. err = ip6gre_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
  676. if (err != 0) {
  677. /* XXX: send ICMP error even if DF is not set. */
  678. if (err == -EMSGSIZE)
  679. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  680. htonl(mtu));
  681. return -1;
  682. }
  683. return 0;
  684. }
  685. static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
  686. {
  687. struct ip6_tnl *t = netdev_priv(dev);
  688. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  689. int encap_limit = -1;
  690. __u16 offset;
  691. struct flowi6 fl6;
  692. __u8 dsfield;
  693. __u32 mtu;
  694. int err;
  695. if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
  696. return -1;
  697. offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
  698. if (offset > 0) {
  699. struct ipv6_tlv_tnl_enc_lim *tel;
  700. tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
  701. if (tel->encap_limit == 0) {
  702. icmpv6_send(skb, ICMPV6_PARAMPROB,
  703. ICMPV6_HDR_FIELD, offset + 2);
  704. return -1;
  705. }
  706. encap_limit = tel->encap_limit - 1;
  707. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  708. encap_limit = t->parms.encap_limit;
  709. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  710. fl6.flowi6_proto = IPPROTO_IPV6;
  711. dsfield = ipv6_get_dsfield(ipv6h);
  712. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  713. fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
  714. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
  715. fl6.flowlabel |= ip6_flowlabel(ipv6h);
  716. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  717. fl6.flowi6_mark = skb->mark;
  718. err = ip6gre_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
  719. if (err != 0) {
  720. if (err == -EMSGSIZE)
  721. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  722. return -1;
  723. }
  724. return 0;
  725. }
  726. /**
  727. * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  728. * @t: the outgoing tunnel device
  729. * @hdr: IPv6 header from the incoming packet
  730. *
  731. * Description:
  732. * Avoid trivial tunneling loop by checking that tunnel exit-point
  733. * doesn't match source of incoming packet.
  734. *
  735. * Return:
  736. * 1 if conflict,
  737. * 0 else
  738. **/
  739. static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
  740. const struct ipv6hdr *hdr)
  741. {
  742. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  743. }
  744. static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
  745. {
  746. struct ip6_tnl *t = netdev_priv(dev);
  747. int encap_limit = -1;
  748. struct flowi6 fl6;
  749. __u32 mtu;
  750. int err;
  751. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  752. encap_limit = t->parms.encap_limit;
  753. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  754. fl6.flowi6_proto = skb->protocol;
  755. err = ip6gre_xmit2(skb, dev, 0, &fl6, encap_limit, &mtu);
  756. return err;
  757. }
  758. static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
  759. struct net_device *dev)
  760. {
  761. struct ip6_tnl *t = netdev_priv(dev);
  762. struct net_device_stats *stats = &t->dev->stats;
  763. int ret;
  764. if (!ip6_tnl_xmit_ctl(t))
  765. goto tx_err;
  766. switch (skb->protocol) {
  767. case htons(ETH_P_IP):
  768. ret = ip6gre_xmit_ipv4(skb, dev);
  769. break;
  770. case htons(ETH_P_IPV6):
  771. ret = ip6gre_xmit_ipv6(skb, dev);
  772. break;
  773. default:
  774. ret = ip6gre_xmit_other(skb, dev);
  775. break;
  776. }
  777. if (ret < 0)
  778. goto tx_err;
  779. return NETDEV_TX_OK;
  780. tx_err:
  781. stats->tx_errors++;
  782. stats->tx_dropped++;
  783. kfree_skb(skb);
  784. return NETDEV_TX_OK;
  785. }
  786. static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
  787. {
  788. struct net_device *dev = t->dev;
  789. struct __ip6_tnl_parm *p = &t->parms;
  790. struct flowi6 *fl6 = &t->fl.u.ip6;
  791. int addend = sizeof(struct ipv6hdr) + 4;
  792. if (dev->type != ARPHRD_ETHER) {
  793. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  794. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  795. }
  796. /* Set up flowi template */
  797. fl6->saddr = p->laddr;
  798. fl6->daddr = p->raddr;
  799. fl6->flowi6_oif = p->link;
  800. fl6->flowlabel = 0;
  801. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  802. fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  803. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  804. fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  805. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
  806. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  807. if (p->flags&IP6_TNL_F_CAP_XMIT &&
  808. p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
  809. dev->flags |= IFF_POINTOPOINT;
  810. else
  811. dev->flags &= ~IFF_POINTOPOINT;
  812. dev->iflink = p->link;
  813. /* Precalculate GRE options length */
  814. if (t->parms.o_flags&(GRE_CSUM|GRE_KEY|GRE_SEQ)) {
  815. if (t->parms.o_flags&GRE_CSUM)
  816. addend += 4;
  817. if (t->parms.o_flags&GRE_KEY)
  818. addend += 4;
  819. if (t->parms.o_flags&GRE_SEQ)
  820. addend += 4;
  821. }
  822. t->hlen = addend;
  823. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  824. int strict = (ipv6_addr_type(&p->raddr) &
  825. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  826. struct rt6_info *rt = rt6_lookup(dev_net(dev),
  827. &p->raddr, &p->laddr,
  828. p->link, strict);
  829. if (rt == NULL)
  830. return;
  831. if (rt->dst.dev) {
  832. dev->hard_header_len = rt->dst.dev->hard_header_len + addend;
  833. if (set_mtu) {
  834. dev->mtu = rt->dst.dev->mtu - addend;
  835. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  836. dev->mtu -= 8;
  837. if (dev->mtu < IPV6_MIN_MTU)
  838. dev->mtu = IPV6_MIN_MTU;
  839. }
  840. }
  841. ip6_rt_put(rt);
  842. }
  843. }
  844. static int ip6gre_tnl_change(struct ip6_tnl *t,
  845. const struct __ip6_tnl_parm *p, int set_mtu)
  846. {
  847. t->parms.laddr = p->laddr;
  848. t->parms.raddr = p->raddr;
  849. t->parms.flags = p->flags;
  850. t->parms.hop_limit = p->hop_limit;
  851. t->parms.encap_limit = p->encap_limit;
  852. t->parms.flowinfo = p->flowinfo;
  853. t->parms.link = p->link;
  854. t->parms.proto = p->proto;
  855. t->parms.i_key = p->i_key;
  856. t->parms.o_key = p->o_key;
  857. t->parms.i_flags = p->i_flags;
  858. t->parms.o_flags = p->o_flags;
  859. ip6_tnl_dst_reset(t);
  860. ip6gre_tnl_link_config(t, set_mtu);
  861. return 0;
  862. }
  863. static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
  864. const struct ip6_tnl_parm2 *u)
  865. {
  866. p->laddr = u->laddr;
  867. p->raddr = u->raddr;
  868. p->flags = u->flags;
  869. p->hop_limit = u->hop_limit;
  870. p->encap_limit = u->encap_limit;
  871. p->flowinfo = u->flowinfo;
  872. p->link = u->link;
  873. p->i_key = u->i_key;
  874. p->o_key = u->o_key;
  875. p->i_flags = u->i_flags;
  876. p->o_flags = u->o_flags;
  877. memcpy(p->name, u->name, sizeof(u->name));
  878. }
  879. static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
  880. const struct __ip6_tnl_parm *p)
  881. {
  882. u->proto = IPPROTO_GRE;
  883. u->laddr = p->laddr;
  884. u->raddr = p->raddr;
  885. u->flags = p->flags;
  886. u->hop_limit = p->hop_limit;
  887. u->encap_limit = p->encap_limit;
  888. u->flowinfo = p->flowinfo;
  889. u->link = p->link;
  890. u->i_key = p->i_key;
  891. u->o_key = p->o_key;
  892. u->i_flags = p->i_flags;
  893. u->o_flags = p->o_flags;
  894. memcpy(u->name, p->name, sizeof(u->name));
  895. }
  896. static int ip6gre_tunnel_ioctl(struct net_device *dev,
  897. struct ifreq *ifr, int cmd)
  898. {
  899. int err = 0;
  900. struct ip6_tnl_parm2 p;
  901. struct __ip6_tnl_parm p1;
  902. struct ip6_tnl *t;
  903. struct net *net = dev_net(dev);
  904. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  905. switch (cmd) {
  906. case SIOCGETTUNNEL:
  907. t = NULL;
  908. if (dev == ign->fb_tunnel_dev) {
  909. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  910. err = -EFAULT;
  911. break;
  912. }
  913. ip6gre_tnl_parm_from_user(&p1, &p);
  914. t = ip6gre_tunnel_locate(net, &p1, 0);
  915. }
  916. if (t == NULL)
  917. t = netdev_priv(dev);
  918. memset(&p, 0, sizeof(p));
  919. ip6gre_tnl_parm_to_user(&p, &t->parms);
  920. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  921. err = -EFAULT;
  922. break;
  923. case SIOCADDTUNNEL:
  924. case SIOCCHGTUNNEL:
  925. err = -EPERM;
  926. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  927. goto done;
  928. err = -EFAULT;
  929. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  930. goto done;
  931. err = -EINVAL;
  932. if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
  933. goto done;
  934. if (!(p.i_flags&GRE_KEY))
  935. p.i_key = 0;
  936. if (!(p.o_flags&GRE_KEY))
  937. p.o_key = 0;
  938. ip6gre_tnl_parm_from_user(&p1, &p);
  939. t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
  940. if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  941. if (t != NULL) {
  942. if (t->dev != dev) {
  943. err = -EEXIST;
  944. break;
  945. }
  946. } else {
  947. t = netdev_priv(dev);
  948. ip6gre_tunnel_unlink(ign, t);
  949. synchronize_net();
  950. ip6gre_tnl_change(t, &p1, 1);
  951. ip6gre_tunnel_link(ign, t);
  952. netdev_state_change(dev);
  953. }
  954. }
  955. if (t) {
  956. err = 0;
  957. memset(&p, 0, sizeof(p));
  958. ip6gre_tnl_parm_to_user(&p, &t->parms);
  959. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  960. err = -EFAULT;
  961. } else
  962. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  963. break;
  964. case SIOCDELTUNNEL:
  965. err = -EPERM;
  966. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  967. goto done;
  968. if (dev == ign->fb_tunnel_dev) {
  969. err = -EFAULT;
  970. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  971. goto done;
  972. err = -ENOENT;
  973. ip6gre_tnl_parm_from_user(&p1, &p);
  974. t = ip6gre_tunnel_locate(net, &p1, 0);
  975. if (t == NULL)
  976. goto done;
  977. err = -EPERM;
  978. if (t == netdev_priv(ign->fb_tunnel_dev))
  979. goto done;
  980. dev = t->dev;
  981. }
  982. unregister_netdevice(dev);
  983. err = 0;
  984. break;
  985. default:
  986. err = -EINVAL;
  987. }
  988. done:
  989. return err;
  990. }
  991. static int ip6gre_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  992. {
  993. if (new_mtu < 68 ||
  994. new_mtu > 0xFFF8 - dev->hard_header_len)
  995. return -EINVAL;
  996. dev->mtu = new_mtu;
  997. return 0;
  998. }
  999. static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
  1000. unsigned short type,
  1001. const void *daddr, const void *saddr, unsigned int len)
  1002. {
  1003. struct ip6_tnl *t = netdev_priv(dev);
  1004. struct ipv6hdr *ipv6h = (struct ipv6hdr *)skb_push(skb, t->hlen);
  1005. __be16 *p = (__be16 *)(ipv6h+1);
  1006. ip6_flow_hdr(ipv6h, 0, t->fl.u.ip6.flowlabel);
  1007. ipv6h->hop_limit = t->parms.hop_limit;
  1008. ipv6h->nexthdr = NEXTHDR_GRE;
  1009. ipv6h->saddr = t->parms.laddr;
  1010. ipv6h->daddr = t->parms.raddr;
  1011. p[0] = t->parms.o_flags;
  1012. p[1] = htons(type);
  1013. /*
  1014. * Set the source hardware address.
  1015. */
  1016. if (saddr)
  1017. memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
  1018. if (daddr)
  1019. memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
  1020. if (!ipv6_addr_any(&ipv6h->daddr))
  1021. return t->hlen;
  1022. return -t->hlen;
  1023. }
  1024. static const struct header_ops ip6gre_header_ops = {
  1025. .create = ip6gre_header,
  1026. };
  1027. static const struct net_device_ops ip6gre_netdev_ops = {
  1028. .ndo_init = ip6gre_tunnel_init,
  1029. .ndo_uninit = ip6gre_tunnel_uninit,
  1030. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1031. .ndo_do_ioctl = ip6gre_tunnel_ioctl,
  1032. .ndo_change_mtu = ip6gre_tunnel_change_mtu,
  1033. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1034. };
  1035. static void ip6gre_dev_free(struct net_device *dev)
  1036. {
  1037. free_percpu(dev->tstats);
  1038. free_netdev(dev);
  1039. }
  1040. static void ip6gre_tunnel_setup(struct net_device *dev)
  1041. {
  1042. struct ip6_tnl *t;
  1043. dev->netdev_ops = &ip6gre_netdev_ops;
  1044. dev->destructor = ip6gre_dev_free;
  1045. dev->type = ARPHRD_IP6GRE;
  1046. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr) + 4;
  1047. dev->mtu = ETH_DATA_LEN - sizeof(struct ipv6hdr) - 4;
  1048. t = netdev_priv(dev);
  1049. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1050. dev->mtu -= 8;
  1051. dev->flags |= IFF_NOARP;
  1052. dev->iflink = 0;
  1053. dev->addr_len = sizeof(struct in6_addr);
  1054. dev->features |= NETIF_F_NETNS_LOCAL;
  1055. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1056. }
  1057. static int ip6gre_tunnel_init(struct net_device *dev)
  1058. {
  1059. struct ip6_tnl *tunnel;
  1060. int i;
  1061. tunnel = netdev_priv(dev);
  1062. tunnel->dev = dev;
  1063. tunnel->net = dev_net(dev);
  1064. strcpy(tunnel->parms.name, dev->name);
  1065. memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
  1066. memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
  1067. if (ipv6_addr_any(&tunnel->parms.raddr))
  1068. dev->header_ops = &ip6gre_header_ops;
  1069. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1070. if (!dev->tstats)
  1071. return -ENOMEM;
  1072. for_each_possible_cpu(i) {
  1073. struct pcpu_tstats *ip6gre_tunnel_stats;
  1074. ip6gre_tunnel_stats = per_cpu_ptr(dev->tstats, i);
  1075. u64_stats_init(&ip6gre_tunnel_stats->syncp);
  1076. }
  1077. return 0;
  1078. }
  1079. static void ip6gre_fb_tunnel_init(struct net_device *dev)
  1080. {
  1081. struct ip6_tnl *tunnel = netdev_priv(dev);
  1082. tunnel->dev = dev;
  1083. tunnel->net = dev_net(dev);
  1084. strcpy(tunnel->parms.name, dev->name);
  1085. tunnel->hlen = sizeof(struct ipv6hdr) + 4;
  1086. dev_hold(dev);
  1087. }
  1088. static struct inet6_protocol ip6gre_protocol __read_mostly = {
  1089. .handler = ip6gre_rcv,
  1090. .err_handler = ip6gre_err,
  1091. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  1092. };
  1093. static void ip6gre_destroy_tunnels(struct ip6gre_net *ign,
  1094. struct list_head *head)
  1095. {
  1096. int prio;
  1097. for (prio = 0; prio < 4; prio++) {
  1098. int h;
  1099. for (h = 0; h < HASH_SIZE; h++) {
  1100. struct ip6_tnl *t;
  1101. t = rtnl_dereference(ign->tunnels[prio][h]);
  1102. while (t != NULL) {
  1103. unregister_netdevice_queue(t->dev, head);
  1104. t = rtnl_dereference(t->next);
  1105. }
  1106. }
  1107. }
  1108. }
  1109. static int __net_init ip6gre_init_net(struct net *net)
  1110. {
  1111. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1112. int err;
  1113. ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
  1114. ip6gre_tunnel_setup);
  1115. if (!ign->fb_tunnel_dev) {
  1116. err = -ENOMEM;
  1117. goto err_alloc_dev;
  1118. }
  1119. dev_net_set(ign->fb_tunnel_dev, net);
  1120. ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
  1121. ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
  1122. err = register_netdev(ign->fb_tunnel_dev);
  1123. if (err)
  1124. goto err_reg_dev;
  1125. rcu_assign_pointer(ign->tunnels_wc[0],
  1126. netdev_priv(ign->fb_tunnel_dev));
  1127. return 0;
  1128. err_reg_dev:
  1129. ip6gre_dev_free(ign->fb_tunnel_dev);
  1130. err_alloc_dev:
  1131. return err;
  1132. }
  1133. static void __net_exit ip6gre_exit_net(struct net *net)
  1134. {
  1135. struct ip6gre_net *ign;
  1136. LIST_HEAD(list);
  1137. ign = net_generic(net, ip6gre_net_id);
  1138. rtnl_lock();
  1139. ip6gre_destroy_tunnels(ign, &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. int i;
  1215. tunnel = netdev_priv(dev);
  1216. tunnel->dev = dev;
  1217. tunnel->net = dev_net(dev);
  1218. strcpy(tunnel->parms.name, dev->name);
  1219. ip6gre_tnl_link_config(tunnel, 1);
  1220. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1221. if (!dev->tstats)
  1222. return -ENOMEM;
  1223. for_each_possible_cpu(i) {
  1224. struct pcpu_tstats *ip6gre_tap_stats;
  1225. ip6gre_tap_stats = per_cpu_ptr(dev->tstats, i);
  1226. u64_stats_init(&ip6gre_tap_stats->syncp);
  1227. }
  1228. return 0;
  1229. }
  1230. static const struct net_device_ops ip6gre_tap_netdev_ops = {
  1231. .ndo_init = ip6gre_tap_init,
  1232. .ndo_uninit = ip6gre_tunnel_uninit,
  1233. .ndo_start_xmit = ip6gre_tunnel_xmit,
  1234. .ndo_set_mac_address = eth_mac_addr,
  1235. .ndo_validate_addr = eth_validate_addr,
  1236. .ndo_change_mtu = ip6gre_tunnel_change_mtu,
  1237. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1238. };
  1239. static void ip6gre_tap_setup(struct net_device *dev)
  1240. {
  1241. ether_setup(dev);
  1242. dev->netdev_ops = &ip6gre_tap_netdev_ops;
  1243. dev->destructor = ip6gre_dev_free;
  1244. dev->iflink = 0;
  1245. dev->features |= NETIF_F_NETNS_LOCAL;
  1246. }
  1247. static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
  1248. struct nlattr *tb[], struct nlattr *data[])
  1249. {
  1250. struct ip6_tnl *nt;
  1251. struct net *net = dev_net(dev);
  1252. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1253. int err;
  1254. nt = netdev_priv(dev);
  1255. ip6gre_netlink_parms(data, &nt->parms);
  1256. if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
  1257. return -EEXIST;
  1258. if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
  1259. eth_hw_addr_random(dev);
  1260. nt->dev = dev;
  1261. nt->net = dev_net(dev);
  1262. ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
  1263. /* Can use a lockless transmit, unless we generate output sequences */
  1264. if (!(nt->parms.o_flags & GRE_SEQ))
  1265. dev->features |= NETIF_F_LLTX;
  1266. err = register_netdevice(dev);
  1267. if (err)
  1268. goto out;
  1269. dev_hold(dev);
  1270. ip6gre_tunnel_link(ign, nt);
  1271. out:
  1272. return err;
  1273. }
  1274. static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
  1275. struct nlattr *data[])
  1276. {
  1277. struct ip6_tnl *t, *nt;
  1278. struct net *net = dev_net(dev);
  1279. struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  1280. struct __ip6_tnl_parm p;
  1281. if (dev == ign->fb_tunnel_dev)
  1282. return -EINVAL;
  1283. nt = netdev_priv(dev);
  1284. ip6gre_netlink_parms(data, &p);
  1285. t = ip6gre_tunnel_locate(net, &p, 0);
  1286. if (t) {
  1287. if (t->dev != dev)
  1288. return -EEXIST;
  1289. } else {
  1290. t = nt;
  1291. ip6gre_tunnel_unlink(ign, t);
  1292. ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
  1293. ip6gre_tunnel_link(ign, t);
  1294. netdev_state_change(dev);
  1295. }
  1296. return 0;
  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. .get_size = ip6gre_get_size,
  1371. .fill_info = ip6gre_fill_info,
  1372. };
  1373. static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
  1374. .kind = "ip6gretap",
  1375. .maxtype = IFLA_GRE_MAX,
  1376. .policy = ip6gre_policy,
  1377. .priv_size = sizeof(struct ip6_tnl),
  1378. .setup = ip6gre_tap_setup,
  1379. .validate = ip6gre_tap_validate,
  1380. .newlink = ip6gre_newlink,
  1381. .changelink = ip6gre_changelink,
  1382. .get_size = ip6gre_get_size,
  1383. .fill_info = ip6gre_fill_info,
  1384. };
  1385. /*
  1386. * And now the modules code and kernel interface.
  1387. */
  1388. static int __init ip6gre_init(void)
  1389. {
  1390. int err;
  1391. pr_info("GRE over IPv6 tunneling driver\n");
  1392. err = register_pernet_device(&ip6gre_net_ops);
  1393. if (err < 0)
  1394. return err;
  1395. err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1396. if (err < 0) {
  1397. pr_info("%s: can't add protocol\n", __func__);
  1398. goto add_proto_failed;
  1399. }
  1400. err = rtnl_link_register(&ip6gre_link_ops);
  1401. if (err < 0)
  1402. goto rtnl_link_failed;
  1403. err = rtnl_link_register(&ip6gre_tap_ops);
  1404. if (err < 0)
  1405. goto tap_ops_failed;
  1406. out:
  1407. return err;
  1408. tap_ops_failed:
  1409. rtnl_link_unregister(&ip6gre_link_ops);
  1410. rtnl_link_failed:
  1411. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1412. add_proto_failed:
  1413. unregister_pernet_device(&ip6gre_net_ops);
  1414. goto out;
  1415. }
  1416. static void __exit ip6gre_fini(void)
  1417. {
  1418. rtnl_link_unregister(&ip6gre_tap_ops);
  1419. rtnl_link_unregister(&ip6gre_link_ops);
  1420. inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  1421. unregister_pernet_device(&ip6gre_net_ops);
  1422. }
  1423. module_init(ip6gre_init);
  1424. module_exit(ip6gre_fini);
  1425. MODULE_LICENSE("GPL");
  1426. MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
  1427. MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
  1428. MODULE_ALIAS_RTNL_LINK("ip6gre");
  1429. MODULE_ALIAS_NETDEV("ip6gre0");