ip6_gre.c 41 KB

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