ip6_gre.c 40 KB

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