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