sit.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716
  1. /*
  2. * IPv6 over IPv4 tunnel device - Simple Internet Transition (SIT)
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. *
  14. * Changes:
  15. * Roger Venning <r.venning@telstra.com>: 6to4 support
  16. * Nate Thompson <nate@thebog.net>: 6to4 support
  17. * Fred Templin <fred.l.templin@boeing.com>: isatap support
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/module.h>
  21. #include <linux/capability.h>
  22. #include <linux/errno.h>
  23. #include <linux/types.h>
  24. #include <linux/socket.h>
  25. #include <linux/sockios.h>
  26. #include <linux/net.h>
  27. #include <linux/in6.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/if_arp.h>
  30. #include <linux/icmp.h>
  31. #include <linux/slab.h>
  32. #include <asm/uaccess.h>
  33. #include <linux/init.h>
  34. #include <linux/netfilter_ipv4.h>
  35. #include <linux/if_ether.h>
  36. #include <net/sock.h>
  37. #include <net/snmp.h>
  38. #include <net/ipv6.h>
  39. #include <net/protocol.h>
  40. #include <net/transp_v6.h>
  41. #include <net/ip6_fib.h>
  42. #include <net/ip6_route.h>
  43. #include <net/ndisc.h>
  44. #include <net/addrconf.h>
  45. #include <net/ip.h>
  46. #include <net/udp.h>
  47. #include <net/icmp.h>
  48. #include <net/ip_tunnels.h>
  49. #include <net/inet_ecn.h>
  50. #include <net/xfrm.h>
  51. #include <net/dsfield.h>
  52. #include <net/net_namespace.h>
  53. #include <net/netns/generic.h>
  54. /*
  55. This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c
  56. For comments look at net/ipv4/ip_gre.c --ANK
  57. */
  58. #define HASH_SIZE 16
  59. #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
  60. static bool log_ecn_error = true;
  61. module_param(log_ecn_error, bool, 0644);
  62. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  63. static int ipip6_tunnel_init(struct net_device *dev);
  64. static void ipip6_tunnel_setup(struct net_device *dev);
  65. static void ipip6_dev_free(struct net_device *dev);
  66. static bool check_6rd(struct ip_tunnel *tunnel, const struct in6_addr *v6dst,
  67. __be32 *v4dst);
  68. static struct rtnl_link_ops sit_link_ops __read_mostly;
  69. static int sit_net_id __read_mostly;
  70. struct sit_net {
  71. struct ip_tunnel __rcu *tunnels_r_l[HASH_SIZE];
  72. struct ip_tunnel __rcu *tunnels_r[HASH_SIZE];
  73. struct ip_tunnel __rcu *tunnels_l[HASH_SIZE];
  74. struct ip_tunnel __rcu *tunnels_wc[1];
  75. struct ip_tunnel __rcu **tunnels[4];
  76. struct net_device *fb_tunnel_dev;
  77. };
  78. /*
  79. * Must be invoked with rcu_read_lock
  80. */
  81. static struct ip_tunnel *ipip6_tunnel_lookup(struct net *net,
  82. struct net_device *dev, __be32 remote, __be32 local)
  83. {
  84. unsigned int h0 = HASH(remote);
  85. unsigned int h1 = HASH(local);
  86. struct ip_tunnel *t;
  87. struct sit_net *sitn = net_generic(net, sit_net_id);
  88. for_each_ip_tunnel_rcu(t, sitn->tunnels_r_l[h0 ^ h1]) {
  89. if (local == t->parms.iph.saddr &&
  90. remote == t->parms.iph.daddr &&
  91. (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
  92. (t->dev->flags & IFF_UP))
  93. return t;
  94. }
  95. for_each_ip_tunnel_rcu(t, sitn->tunnels_r[h0]) {
  96. if (remote == t->parms.iph.daddr &&
  97. (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
  98. (t->dev->flags & IFF_UP))
  99. return t;
  100. }
  101. for_each_ip_tunnel_rcu(t, sitn->tunnels_l[h1]) {
  102. if (local == t->parms.iph.saddr &&
  103. (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
  104. (t->dev->flags & IFF_UP))
  105. return t;
  106. }
  107. t = rcu_dereference(sitn->tunnels_wc[0]);
  108. if ((t != NULL) && (t->dev->flags & IFF_UP))
  109. return t;
  110. return NULL;
  111. }
  112. static struct ip_tunnel __rcu **__ipip6_bucket(struct sit_net *sitn,
  113. struct ip_tunnel_parm *parms)
  114. {
  115. __be32 remote = parms->iph.daddr;
  116. __be32 local = parms->iph.saddr;
  117. unsigned int h = 0;
  118. int prio = 0;
  119. if (remote) {
  120. prio |= 2;
  121. h ^= HASH(remote);
  122. }
  123. if (local) {
  124. prio |= 1;
  125. h ^= HASH(local);
  126. }
  127. return &sitn->tunnels[prio][h];
  128. }
  129. static inline struct ip_tunnel __rcu **ipip6_bucket(struct sit_net *sitn,
  130. struct ip_tunnel *t)
  131. {
  132. return __ipip6_bucket(sitn, &t->parms);
  133. }
  134. static void ipip6_tunnel_unlink(struct sit_net *sitn, struct ip_tunnel *t)
  135. {
  136. struct ip_tunnel __rcu **tp;
  137. struct ip_tunnel *iter;
  138. for (tp = ipip6_bucket(sitn, t);
  139. (iter = rtnl_dereference(*tp)) != NULL;
  140. tp = &iter->next) {
  141. if (t == iter) {
  142. rcu_assign_pointer(*tp, t->next);
  143. break;
  144. }
  145. }
  146. }
  147. static void ipip6_tunnel_link(struct sit_net *sitn, struct ip_tunnel *t)
  148. {
  149. struct ip_tunnel __rcu **tp = ipip6_bucket(sitn, t);
  150. rcu_assign_pointer(t->next, rtnl_dereference(*tp));
  151. rcu_assign_pointer(*tp, t);
  152. }
  153. static void ipip6_tunnel_clone_6rd(struct net_device *dev, struct sit_net *sitn)
  154. {
  155. #ifdef CONFIG_IPV6_SIT_6RD
  156. struct ip_tunnel *t = netdev_priv(dev);
  157. if (t->dev == sitn->fb_tunnel_dev) {
  158. ipv6_addr_set(&t->ip6rd.prefix, htonl(0x20020000), 0, 0, 0);
  159. t->ip6rd.relay_prefix = 0;
  160. t->ip6rd.prefixlen = 16;
  161. t->ip6rd.relay_prefixlen = 0;
  162. } else {
  163. struct ip_tunnel *t0 = netdev_priv(sitn->fb_tunnel_dev);
  164. memcpy(&t->ip6rd, &t0->ip6rd, sizeof(t->ip6rd));
  165. }
  166. #endif
  167. }
  168. static int ipip6_tunnel_create(struct net_device *dev)
  169. {
  170. struct ip_tunnel *t = netdev_priv(dev);
  171. struct net *net = dev_net(dev);
  172. struct sit_net *sitn = net_generic(net, sit_net_id);
  173. int err;
  174. err = ipip6_tunnel_init(dev);
  175. if (err < 0)
  176. goto out;
  177. ipip6_tunnel_clone_6rd(dev, sitn);
  178. if ((__force u16)t->parms.i_flags & SIT_ISATAP)
  179. dev->priv_flags |= IFF_ISATAP;
  180. err = register_netdevice(dev);
  181. if (err < 0)
  182. goto out;
  183. strcpy(t->parms.name, dev->name);
  184. dev->rtnl_link_ops = &sit_link_ops;
  185. dev_hold(dev);
  186. ipip6_tunnel_link(sitn, t);
  187. return 0;
  188. out:
  189. return err;
  190. }
  191. static struct ip_tunnel *ipip6_tunnel_locate(struct net *net,
  192. struct ip_tunnel_parm *parms, int create)
  193. {
  194. __be32 remote = parms->iph.daddr;
  195. __be32 local = parms->iph.saddr;
  196. struct ip_tunnel *t, *nt;
  197. struct ip_tunnel __rcu **tp;
  198. struct net_device *dev;
  199. char name[IFNAMSIZ];
  200. struct sit_net *sitn = net_generic(net, sit_net_id);
  201. for (tp = __ipip6_bucket(sitn, parms);
  202. (t = rtnl_dereference(*tp)) != NULL;
  203. tp = &t->next) {
  204. if (local == t->parms.iph.saddr &&
  205. remote == t->parms.iph.daddr &&
  206. parms->link == t->parms.link) {
  207. if (create)
  208. return NULL;
  209. else
  210. return t;
  211. }
  212. }
  213. if (!create)
  214. goto failed;
  215. if (parms->name[0])
  216. strlcpy(name, parms->name, IFNAMSIZ);
  217. else
  218. strcpy(name, "sit%d");
  219. dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
  220. if (dev == NULL)
  221. return NULL;
  222. dev_net_set(dev, net);
  223. nt = netdev_priv(dev);
  224. nt->parms = *parms;
  225. if (ipip6_tunnel_create(dev) < 0)
  226. goto failed_free;
  227. return nt;
  228. failed_free:
  229. ipip6_dev_free(dev);
  230. failed:
  231. return NULL;
  232. }
  233. #define for_each_prl_rcu(start) \
  234. for (prl = rcu_dereference(start); \
  235. prl; \
  236. prl = rcu_dereference(prl->next))
  237. static struct ip_tunnel_prl_entry *
  238. __ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr)
  239. {
  240. struct ip_tunnel_prl_entry *prl;
  241. for_each_prl_rcu(t->prl)
  242. if (prl->addr == addr)
  243. break;
  244. return prl;
  245. }
  246. static int ipip6_tunnel_get_prl(struct ip_tunnel *t,
  247. struct ip_tunnel_prl __user *a)
  248. {
  249. struct ip_tunnel_prl kprl, *kp;
  250. struct ip_tunnel_prl_entry *prl;
  251. unsigned int cmax, c = 0, ca, len;
  252. int ret = 0;
  253. if (copy_from_user(&kprl, a, sizeof(kprl)))
  254. return -EFAULT;
  255. cmax = kprl.datalen / sizeof(kprl);
  256. if (cmax > 1 && kprl.addr != htonl(INADDR_ANY))
  257. cmax = 1;
  258. /* For simple GET or for root users,
  259. * we try harder to allocate.
  260. */
  261. kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
  262. kcalloc(cmax, sizeof(*kp), GFP_KERNEL) :
  263. NULL;
  264. rcu_read_lock();
  265. ca = t->prl_count < cmax ? t->prl_count : cmax;
  266. if (!kp) {
  267. /* We don't try hard to allocate much memory for
  268. * non-root users.
  269. * For root users, retry allocating enough memory for
  270. * the answer.
  271. */
  272. kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
  273. if (!kp) {
  274. ret = -ENOMEM;
  275. goto out;
  276. }
  277. }
  278. c = 0;
  279. for_each_prl_rcu(t->prl) {
  280. if (c >= cmax)
  281. break;
  282. if (kprl.addr != htonl(INADDR_ANY) && prl->addr != kprl.addr)
  283. continue;
  284. kp[c].addr = prl->addr;
  285. kp[c].flags = prl->flags;
  286. c++;
  287. if (kprl.addr != htonl(INADDR_ANY))
  288. break;
  289. }
  290. out:
  291. rcu_read_unlock();
  292. len = sizeof(*kp) * c;
  293. ret = 0;
  294. if ((len && copy_to_user(a + 1, kp, len)) || put_user(len, &a->datalen))
  295. ret = -EFAULT;
  296. kfree(kp);
  297. return ret;
  298. }
  299. static int
  300. ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg)
  301. {
  302. struct ip_tunnel_prl_entry *p;
  303. int err = 0;
  304. if (a->addr == htonl(INADDR_ANY))
  305. return -EINVAL;
  306. ASSERT_RTNL();
  307. for (p = rtnl_dereference(t->prl); p; p = rtnl_dereference(p->next)) {
  308. if (p->addr == a->addr) {
  309. if (chg) {
  310. p->flags = a->flags;
  311. goto out;
  312. }
  313. err = -EEXIST;
  314. goto out;
  315. }
  316. }
  317. if (chg) {
  318. err = -ENXIO;
  319. goto out;
  320. }
  321. p = kzalloc(sizeof(struct ip_tunnel_prl_entry), GFP_KERNEL);
  322. if (!p) {
  323. err = -ENOBUFS;
  324. goto out;
  325. }
  326. p->next = t->prl;
  327. p->addr = a->addr;
  328. p->flags = a->flags;
  329. t->prl_count++;
  330. rcu_assign_pointer(t->prl, p);
  331. out:
  332. return err;
  333. }
  334. static void prl_list_destroy_rcu(struct rcu_head *head)
  335. {
  336. struct ip_tunnel_prl_entry *p, *n;
  337. p = container_of(head, struct ip_tunnel_prl_entry, rcu_head);
  338. do {
  339. n = rcu_dereference_protected(p->next, 1);
  340. kfree(p);
  341. p = n;
  342. } while (p);
  343. }
  344. static int
  345. ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
  346. {
  347. struct ip_tunnel_prl_entry *x;
  348. struct ip_tunnel_prl_entry __rcu **p;
  349. int err = 0;
  350. ASSERT_RTNL();
  351. if (a && a->addr != htonl(INADDR_ANY)) {
  352. for (p = &t->prl;
  353. (x = rtnl_dereference(*p)) != NULL;
  354. p = &x->next) {
  355. if (x->addr == a->addr) {
  356. *p = x->next;
  357. kfree_rcu(x, rcu_head);
  358. t->prl_count--;
  359. goto out;
  360. }
  361. }
  362. err = -ENXIO;
  363. } else {
  364. x = rtnl_dereference(t->prl);
  365. if (x) {
  366. t->prl_count = 0;
  367. call_rcu(&x->rcu_head, prl_list_destroy_rcu);
  368. t->prl = NULL;
  369. }
  370. }
  371. out:
  372. return err;
  373. }
  374. static int
  375. isatap_chksrc(struct sk_buff *skb, const struct iphdr *iph, struct ip_tunnel *t)
  376. {
  377. struct ip_tunnel_prl_entry *p;
  378. int ok = 1;
  379. rcu_read_lock();
  380. p = __ipip6_tunnel_locate_prl(t, iph->saddr);
  381. if (p) {
  382. if (p->flags & PRL_DEFAULT)
  383. skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT;
  384. else
  385. skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT;
  386. } else {
  387. const struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr;
  388. if (ipv6_addr_is_isatap(addr6) &&
  389. (addr6->s6_addr32[3] == iph->saddr) &&
  390. ipv6_chk_prefix(addr6, t->dev))
  391. skb->ndisc_nodetype = NDISC_NODETYPE_HOST;
  392. else
  393. ok = 0;
  394. }
  395. rcu_read_unlock();
  396. return ok;
  397. }
  398. static void ipip6_tunnel_uninit(struct net_device *dev)
  399. {
  400. struct ip_tunnel *tunnel = netdev_priv(dev);
  401. struct sit_net *sitn = net_generic(tunnel->net, sit_net_id);
  402. if (dev == sitn->fb_tunnel_dev) {
  403. RCU_INIT_POINTER(sitn->tunnels_wc[0], NULL);
  404. } else {
  405. ipip6_tunnel_unlink(sitn, tunnel);
  406. ipip6_tunnel_del_prl(tunnel, NULL);
  407. }
  408. dev_put(dev);
  409. }
  410. static int ipip6_err(struct sk_buff *skb, u32 info)
  411. {
  412. /* All the routers (except for Linux) return only
  413. 8 bytes of packet payload. It means, that precise relaying of
  414. ICMP in the real Internet is absolutely infeasible.
  415. */
  416. const struct iphdr *iph = (const struct iphdr *)skb->data;
  417. const int type = icmp_hdr(skb)->type;
  418. const int code = icmp_hdr(skb)->code;
  419. struct ip_tunnel *t;
  420. int err;
  421. switch (type) {
  422. default:
  423. case ICMP_PARAMETERPROB:
  424. return 0;
  425. case ICMP_DEST_UNREACH:
  426. switch (code) {
  427. case ICMP_SR_FAILED:
  428. case ICMP_PORT_UNREACH:
  429. /* Impossible event. */
  430. return 0;
  431. default:
  432. /* All others are translated to HOST_UNREACH.
  433. rfc2003 contains "deep thoughts" about NET_UNREACH,
  434. I believe they are just ether pollution. --ANK
  435. */
  436. break;
  437. }
  438. break;
  439. case ICMP_TIME_EXCEEDED:
  440. if (code != ICMP_EXC_TTL)
  441. return 0;
  442. break;
  443. case ICMP_REDIRECT:
  444. break;
  445. }
  446. err = -ENOENT;
  447. t = ipip6_tunnel_lookup(dev_net(skb->dev),
  448. skb->dev,
  449. iph->daddr,
  450. iph->saddr);
  451. if (t == NULL)
  452. goto out;
  453. if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
  454. ipv4_update_pmtu(skb, dev_net(skb->dev), info,
  455. t->dev->ifindex, 0, IPPROTO_IPV6, 0);
  456. err = 0;
  457. goto out;
  458. }
  459. if (type == ICMP_REDIRECT) {
  460. ipv4_redirect(skb, dev_net(skb->dev), t->dev->ifindex, 0,
  461. IPPROTO_IPV6, 0);
  462. err = 0;
  463. goto out;
  464. }
  465. if (t->parms.iph.daddr == 0)
  466. goto out;
  467. err = 0;
  468. if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
  469. goto out;
  470. if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
  471. t->err_count++;
  472. else
  473. t->err_count = 1;
  474. t->err_time = jiffies;
  475. out:
  476. return err;
  477. }
  478. static inline bool is_spoofed_6rd(struct ip_tunnel *tunnel, const __be32 v4addr,
  479. const struct in6_addr *v6addr)
  480. {
  481. __be32 v4embed = 0;
  482. if (check_6rd(tunnel, v6addr, &v4embed) && v4addr != v4embed)
  483. return true;
  484. return false;
  485. }
  486. static int ipip6_rcv(struct sk_buff *skb)
  487. {
  488. const struct iphdr *iph = ip_hdr(skb);
  489. struct ip_tunnel *tunnel;
  490. int err;
  491. tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
  492. iph->saddr, iph->daddr);
  493. if (tunnel != NULL) {
  494. struct pcpu_tstats *tstats;
  495. if (tunnel->parms.iph.protocol != IPPROTO_IPV6 &&
  496. tunnel->parms.iph.protocol != 0)
  497. goto out;
  498. secpath_reset(skb);
  499. skb->mac_header = skb->network_header;
  500. skb_reset_network_header(skb);
  501. IPCB(skb)->flags = 0;
  502. skb->protocol = htons(ETH_P_IPV6);
  503. skb->pkt_type = PACKET_HOST;
  504. if (tunnel->dev->priv_flags & IFF_ISATAP) {
  505. if (!isatap_chksrc(skb, iph, tunnel)) {
  506. tunnel->dev->stats.rx_errors++;
  507. goto out;
  508. }
  509. } else if (!(tunnel->dev->flags&IFF_POINTOPOINT)) {
  510. if (is_spoofed_6rd(tunnel, iph->saddr,
  511. &ipv6_hdr(skb)->saddr) ||
  512. is_spoofed_6rd(tunnel, iph->daddr,
  513. &ipv6_hdr(skb)->daddr)) {
  514. tunnel->dev->stats.rx_errors++;
  515. goto out;
  516. }
  517. }
  518. __skb_tunnel_rx(skb, tunnel->dev);
  519. err = IP_ECN_decapsulate(iph, skb);
  520. if (unlikely(err)) {
  521. if (log_ecn_error)
  522. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  523. &iph->saddr, iph->tos);
  524. if (err > 1) {
  525. ++tunnel->dev->stats.rx_frame_errors;
  526. ++tunnel->dev->stats.rx_errors;
  527. goto out;
  528. }
  529. }
  530. tstats = this_cpu_ptr(tunnel->dev->tstats);
  531. tstats->rx_packets++;
  532. tstats->rx_bytes += skb->len;
  533. if (tunnel->net != dev_net(tunnel->dev))
  534. skb_scrub_packet(skb);
  535. netif_rx(skb);
  536. return 0;
  537. }
  538. /* no tunnel matched, let upstream know, ipsec may handle it */
  539. return 1;
  540. out:
  541. kfree_skb(skb);
  542. return 0;
  543. }
  544. static const struct tnl_ptk_info tpi = {
  545. /* no tunnel info required for ipip. */
  546. .proto = htons(ETH_P_IP),
  547. };
  548. static int ipip_rcv(struct sk_buff *skb)
  549. {
  550. const struct iphdr *iph;
  551. struct ip_tunnel *tunnel;
  552. iph = ip_hdr(skb);
  553. tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
  554. iph->saddr, iph->daddr);
  555. if (tunnel != NULL) {
  556. if (tunnel->parms.iph.protocol != IPPROTO_IPIP &&
  557. tunnel->parms.iph.protocol != 0)
  558. goto drop;
  559. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  560. goto drop;
  561. if (iptunnel_pull_header(skb, 0, tpi.proto))
  562. goto drop;
  563. return ip_tunnel_rcv(tunnel, skb, &tpi, log_ecn_error);
  564. }
  565. return 1;
  566. drop:
  567. kfree_skb(skb);
  568. return 0;
  569. }
  570. /*
  571. * If the IPv6 address comes from 6rd / 6to4 (RFC 3056) addr space this function
  572. * stores the embedded IPv4 address in v4dst and returns true.
  573. */
  574. static bool check_6rd(struct ip_tunnel *tunnel, const struct in6_addr *v6dst,
  575. __be32 *v4dst)
  576. {
  577. #ifdef CONFIG_IPV6_SIT_6RD
  578. if (ipv6_prefix_equal(v6dst, &tunnel->ip6rd.prefix,
  579. tunnel->ip6rd.prefixlen)) {
  580. unsigned int pbw0, pbi0;
  581. int pbi1;
  582. u32 d;
  583. pbw0 = tunnel->ip6rd.prefixlen >> 5;
  584. pbi0 = tunnel->ip6rd.prefixlen & 0x1f;
  585. d = (ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >>
  586. tunnel->ip6rd.relay_prefixlen;
  587. pbi1 = pbi0 - tunnel->ip6rd.relay_prefixlen;
  588. if (pbi1 > 0)
  589. d |= ntohl(v6dst->s6_addr32[pbw0 + 1]) >>
  590. (32 - pbi1);
  591. *v4dst = tunnel->ip6rd.relay_prefix | htonl(d);
  592. return true;
  593. }
  594. #else
  595. if (v6dst->s6_addr16[0] == htons(0x2002)) {
  596. /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
  597. memcpy(v4dst, &v6dst->s6_addr16[1], 4);
  598. return true;
  599. }
  600. #endif
  601. return false;
  602. }
  603. static inline __be32 try_6rd(struct ip_tunnel *tunnel,
  604. const struct in6_addr *v6dst)
  605. {
  606. __be32 dst = 0;
  607. check_6rd(tunnel, v6dst, &dst);
  608. return dst;
  609. }
  610. /*
  611. * This function assumes it is being called from dev_queue_xmit()
  612. * and that skb is filled properly by that function.
  613. */
  614. static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
  615. struct net_device *dev)
  616. {
  617. struct ip_tunnel *tunnel = netdev_priv(dev);
  618. const struct iphdr *tiph = &tunnel->parms.iph;
  619. const struct ipv6hdr *iph6 = ipv6_hdr(skb);
  620. u8 tos = tunnel->parms.iph.tos;
  621. __be16 df = tiph->frag_off;
  622. struct rtable *rt; /* Route to the other host */
  623. struct net_device *tdev; /* Device to other host */
  624. unsigned int max_headroom; /* The extra header space needed */
  625. __be32 dst = tiph->daddr;
  626. struct flowi4 fl4;
  627. int mtu;
  628. const struct in6_addr *addr6;
  629. int addr_type;
  630. u8 ttl;
  631. int err;
  632. if (skb->protocol != htons(ETH_P_IPV6))
  633. goto tx_error;
  634. if (tos == 1)
  635. tos = ipv6_get_dsfield(iph6);
  636. /* ISATAP (RFC4214) - must come before 6to4 */
  637. if (dev->priv_flags & IFF_ISATAP) {
  638. struct neighbour *neigh = NULL;
  639. bool do_tx_error = false;
  640. if (skb_dst(skb))
  641. neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
  642. if (neigh == NULL) {
  643. net_dbg_ratelimited("sit: nexthop == NULL\n");
  644. goto tx_error;
  645. }
  646. addr6 = (const struct in6_addr *)&neigh->primary_key;
  647. addr_type = ipv6_addr_type(addr6);
  648. if ((addr_type & IPV6_ADDR_UNICAST) &&
  649. ipv6_addr_is_isatap(addr6))
  650. dst = addr6->s6_addr32[3];
  651. else
  652. do_tx_error = true;
  653. neigh_release(neigh);
  654. if (do_tx_error)
  655. goto tx_error;
  656. }
  657. if (!dst)
  658. dst = try_6rd(tunnel, &iph6->daddr);
  659. if (!dst) {
  660. struct neighbour *neigh = NULL;
  661. bool do_tx_error = false;
  662. if (skb_dst(skb))
  663. neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
  664. if (neigh == NULL) {
  665. net_dbg_ratelimited("sit: nexthop == NULL\n");
  666. goto tx_error;
  667. }
  668. addr6 = (const struct in6_addr *)&neigh->primary_key;
  669. addr_type = ipv6_addr_type(addr6);
  670. if (addr_type == IPV6_ADDR_ANY) {
  671. addr6 = &ipv6_hdr(skb)->daddr;
  672. addr_type = ipv6_addr_type(addr6);
  673. }
  674. if ((addr_type & IPV6_ADDR_COMPATv4) != 0)
  675. dst = addr6->s6_addr32[3];
  676. else
  677. do_tx_error = true;
  678. neigh_release(neigh);
  679. if (do_tx_error)
  680. goto tx_error;
  681. }
  682. rt = ip_route_output_ports(tunnel->net, &fl4, NULL,
  683. dst, tiph->saddr,
  684. 0, 0,
  685. IPPROTO_IPV6, RT_TOS(tos),
  686. tunnel->parms.link);
  687. if (IS_ERR(rt)) {
  688. dev->stats.tx_carrier_errors++;
  689. goto tx_error_icmp;
  690. }
  691. if (rt->rt_type != RTN_UNICAST) {
  692. ip_rt_put(rt);
  693. dev->stats.tx_carrier_errors++;
  694. goto tx_error_icmp;
  695. }
  696. tdev = rt->dst.dev;
  697. if (tdev == dev) {
  698. ip_rt_put(rt);
  699. dev->stats.collisions++;
  700. goto tx_error;
  701. }
  702. if (df) {
  703. mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
  704. if (mtu < 68) {
  705. dev->stats.collisions++;
  706. ip_rt_put(rt);
  707. goto tx_error;
  708. }
  709. if (mtu < IPV6_MIN_MTU) {
  710. mtu = IPV6_MIN_MTU;
  711. df = 0;
  712. }
  713. if (tunnel->parms.iph.daddr && skb_dst(skb))
  714. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
  715. if (skb->len > mtu) {
  716. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  717. ip_rt_put(rt);
  718. goto tx_error;
  719. }
  720. }
  721. if (tunnel->err_count > 0) {
  722. if (time_before(jiffies,
  723. tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
  724. tunnel->err_count--;
  725. dst_link_failure(skb);
  726. } else
  727. tunnel->err_count = 0;
  728. }
  729. if (tunnel->net != dev_net(dev))
  730. skb_scrub_packet(skb);
  731. /*
  732. * Okay, now see if we can stuff it in the buffer as-is.
  733. */
  734. max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
  735. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  736. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  737. struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
  738. if (!new_skb) {
  739. ip_rt_put(rt);
  740. dev->stats.tx_dropped++;
  741. dev_kfree_skb(skb);
  742. return NETDEV_TX_OK;
  743. }
  744. if (skb->sk)
  745. skb_set_owner_w(new_skb, skb->sk);
  746. dev_kfree_skb(skb);
  747. skb = new_skb;
  748. iph6 = ipv6_hdr(skb);
  749. }
  750. ttl = tiph->ttl;
  751. if (ttl == 0)
  752. ttl = iph6->hop_limit;
  753. tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
  754. if (likely(!skb->encapsulation)) {
  755. skb_reset_inner_headers(skb);
  756. skb->encapsulation = 1;
  757. }
  758. err = iptunnel_xmit(dev_net(dev), rt, skb, fl4.saddr, fl4.daddr,
  759. IPPROTO_IPV6, tos, ttl, df);
  760. iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
  761. return NETDEV_TX_OK;
  762. tx_error_icmp:
  763. dst_link_failure(skb);
  764. tx_error:
  765. dev->stats.tx_errors++;
  766. dev_kfree_skb(skb);
  767. return NETDEV_TX_OK;
  768. }
  769. static netdev_tx_t ipip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
  770. {
  771. struct ip_tunnel *tunnel = netdev_priv(dev);
  772. const struct iphdr *tiph = &tunnel->parms.iph;
  773. if (likely(!skb->encapsulation)) {
  774. skb_reset_inner_headers(skb);
  775. skb->encapsulation = 1;
  776. }
  777. ip_tunnel_xmit(skb, dev, tiph, IPPROTO_IPIP);
  778. return NETDEV_TX_OK;
  779. }
  780. static netdev_tx_t sit_tunnel_xmit(struct sk_buff *skb,
  781. struct net_device *dev)
  782. {
  783. switch (skb->protocol) {
  784. case htons(ETH_P_IP):
  785. ipip_tunnel_xmit(skb, dev);
  786. break;
  787. case htons(ETH_P_IPV6):
  788. ipip6_tunnel_xmit(skb, dev);
  789. break;
  790. default:
  791. goto tx_err;
  792. }
  793. return NETDEV_TX_OK;
  794. tx_err:
  795. dev->stats.tx_errors++;
  796. dev_kfree_skb(skb);
  797. return NETDEV_TX_OK;
  798. }
  799. static void ipip6_tunnel_bind_dev(struct net_device *dev)
  800. {
  801. struct net_device *tdev = NULL;
  802. struct ip_tunnel *tunnel;
  803. const struct iphdr *iph;
  804. struct flowi4 fl4;
  805. tunnel = netdev_priv(dev);
  806. iph = &tunnel->parms.iph;
  807. if (iph->daddr) {
  808. struct rtable *rt = ip_route_output_ports(tunnel->net, &fl4,
  809. NULL,
  810. iph->daddr, iph->saddr,
  811. 0, 0,
  812. IPPROTO_IPV6,
  813. RT_TOS(iph->tos),
  814. tunnel->parms.link);
  815. if (!IS_ERR(rt)) {
  816. tdev = rt->dst.dev;
  817. ip_rt_put(rt);
  818. }
  819. dev->flags |= IFF_POINTOPOINT;
  820. }
  821. if (!tdev && tunnel->parms.link)
  822. tdev = __dev_get_by_index(tunnel->net, tunnel->parms.link);
  823. if (tdev) {
  824. dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
  825. dev->mtu = tdev->mtu - sizeof(struct iphdr);
  826. if (dev->mtu < IPV6_MIN_MTU)
  827. dev->mtu = IPV6_MIN_MTU;
  828. }
  829. dev->iflink = tunnel->parms.link;
  830. }
  831. static void ipip6_tunnel_update(struct ip_tunnel *t, struct ip_tunnel_parm *p)
  832. {
  833. struct net *net = t->net;
  834. struct sit_net *sitn = net_generic(net, sit_net_id);
  835. ipip6_tunnel_unlink(sitn, t);
  836. synchronize_net();
  837. t->parms.iph.saddr = p->iph.saddr;
  838. t->parms.iph.daddr = p->iph.daddr;
  839. memcpy(t->dev->dev_addr, &p->iph.saddr, 4);
  840. memcpy(t->dev->broadcast, &p->iph.daddr, 4);
  841. ipip6_tunnel_link(sitn, t);
  842. t->parms.iph.ttl = p->iph.ttl;
  843. t->parms.iph.tos = p->iph.tos;
  844. if (t->parms.link != p->link) {
  845. t->parms.link = p->link;
  846. ipip6_tunnel_bind_dev(t->dev);
  847. }
  848. netdev_state_change(t->dev);
  849. }
  850. #ifdef CONFIG_IPV6_SIT_6RD
  851. static int ipip6_tunnel_update_6rd(struct ip_tunnel *t,
  852. struct ip_tunnel_6rd *ip6rd)
  853. {
  854. struct in6_addr prefix;
  855. __be32 relay_prefix;
  856. if (ip6rd->relay_prefixlen > 32 ||
  857. ip6rd->prefixlen + (32 - ip6rd->relay_prefixlen) > 64)
  858. return -EINVAL;
  859. ipv6_addr_prefix(&prefix, &ip6rd->prefix, ip6rd->prefixlen);
  860. if (!ipv6_addr_equal(&prefix, &ip6rd->prefix))
  861. return -EINVAL;
  862. if (ip6rd->relay_prefixlen)
  863. relay_prefix = ip6rd->relay_prefix &
  864. htonl(0xffffffffUL <<
  865. (32 - ip6rd->relay_prefixlen));
  866. else
  867. relay_prefix = 0;
  868. if (relay_prefix != ip6rd->relay_prefix)
  869. return -EINVAL;
  870. t->ip6rd.prefix = prefix;
  871. t->ip6rd.relay_prefix = relay_prefix;
  872. t->ip6rd.prefixlen = ip6rd->prefixlen;
  873. t->ip6rd.relay_prefixlen = ip6rd->relay_prefixlen;
  874. netdev_state_change(t->dev);
  875. return 0;
  876. }
  877. #endif
  878. static int
  879. ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
  880. {
  881. int err = 0;
  882. struct ip_tunnel_parm p;
  883. struct ip_tunnel_prl prl;
  884. struct ip_tunnel *t;
  885. struct net *net = dev_net(dev);
  886. struct sit_net *sitn = net_generic(net, sit_net_id);
  887. #ifdef CONFIG_IPV6_SIT_6RD
  888. struct ip_tunnel_6rd ip6rd;
  889. #endif
  890. switch (cmd) {
  891. case SIOCGETTUNNEL:
  892. #ifdef CONFIG_IPV6_SIT_6RD
  893. case SIOCGET6RD:
  894. #endif
  895. t = NULL;
  896. if (dev == sitn->fb_tunnel_dev) {
  897. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  898. err = -EFAULT;
  899. break;
  900. }
  901. t = ipip6_tunnel_locate(net, &p, 0);
  902. }
  903. if (t == NULL)
  904. t = netdev_priv(dev);
  905. err = -EFAULT;
  906. if (cmd == SIOCGETTUNNEL) {
  907. memcpy(&p, &t->parms, sizeof(p));
  908. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p,
  909. sizeof(p)))
  910. goto done;
  911. #ifdef CONFIG_IPV6_SIT_6RD
  912. } else {
  913. ip6rd.prefix = t->ip6rd.prefix;
  914. ip6rd.relay_prefix = t->ip6rd.relay_prefix;
  915. ip6rd.prefixlen = t->ip6rd.prefixlen;
  916. ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen;
  917. if (copy_to_user(ifr->ifr_ifru.ifru_data, &ip6rd,
  918. sizeof(ip6rd)))
  919. goto done;
  920. #endif
  921. }
  922. err = 0;
  923. break;
  924. case SIOCADDTUNNEL:
  925. case SIOCCHGTUNNEL:
  926. err = -EPERM;
  927. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  928. goto done;
  929. err = -EFAULT;
  930. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  931. goto done;
  932. err = -EINVAL;
  933. if (p.iph.protocol != IPPROTO_IPV6 &&
  934. p.iph.protocol != IPPROTO_IPIP &&
  935. p.iph.protocol != 0)
  936. goto done;
  937. if (p.iph.version != 4 ||
  938. p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
  939. goto done;
  940. if (p.iph.ttl)
  941. p.iph.frag_off |= htons(IP_DF);
  942. t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
  943. if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  944. if (t != NULL) {
  945. if (t->dev != dev) {
  946. err = -EEXIST;
  947. break;
  948. }
  949. } else {
  950. if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
  951. (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
  952. err = -EINVAL;
  953. break;
  954. }
  955. t = netdev_priv(dev);
  956. }
  957. ipip6_tunnel_update(t, &p);
  958. }
  959. if (t) {
  960. err = 0;
  961. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
  962. err = -EFAULT;
  963. } else
  964. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  965. break;
  966. case SIOCDELTUNNEL:
  967. err = -EPERM;
  968. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  969. goto done;
  970. if (dev == sitn->fb_tunnel_dev) {
  971. err = -EFAULT;
  972. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  973. goto done;
  974. err = -ENOENT;
  975. if ((t = ipip6_tunnel_locate(net, &p, 0)) == NULL)
  976. goto done;
  977. err = -EPERM;
  978. if (t == netdev_priv(sitn->fb_tunnel_dev))
  979. goto done;
  980. dev = t->dev;
  981. }
  982. unregister_netdevice(dev);
  983. err = 0;
  984. break;
  985. case SIOCGETPRL:
  986. err = -EINVAL;
  987. if (dev == sitn->fb_tunnel_dev)
  988. goto done;
  989. err = -ENOENT;
  990. if (!(t = netdev_priv(dev)))
  991. goto done;
  992. err = ipip6_tunnel_get_prl(t, ifr->ifr_ifru.ifru_data);
  993. break;
  994. case SIOCADDPRL:
  995. case SIOCDELPRL:
  996. case SIOCCHGPRL:
  997. err = -EPERM;
  998. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  999. goto done;
  1000. err = -EINVAL;
  1001. if (dev == sitn->fb_tunnel_dev)
  1002. goto done;
  1003. err = -EFAULT;
  1004. if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
  1005. goto done;
  1006. err = -ENOENT;
  1007. if (!(t = netdev_priv(dev)))
  1008. goto done;
  1009. switch (cmd) {
  1010. case SIOCDELPRL:
  1011. err = ipip6_tunnel_del_prl(t, &prl);
  1012. break;
  1013. case SIOCADDPRL:
  1014. case SIOCCHGPRL:
  1015. err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
  1016. break;
  1017. }
  1018. netdev_state_change(dev);
  1019. break;
  1020. #ifdef CONFIG_IPV6_SIT_6RD
  1021. case SIOCADD6RD:
  1022. case SIOCCHG6RD:
  1023. case SIOCDEL6RD:
  1024. err = -EPERM;
  1025. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1026. goto done;
  1027. err = -EFAULT;
  1028. if (copy_from_user(&ip6rd, ifr->ifr_ifru.ifru_data,
  1029. sizeof(ip6rd)))
  1030. goto done;
  1031. t = netdev_priv(dev);
  1032. if (cmd != SIOCDEL6RD) {
  1033. err = ipip6_tunnel_update_6rd(t, &ip6rd);
  1034. if (err < 0)
  1035. goto done;
  1036. } else
  1037. ipip6_tunnel_clone_6rd(dev, sitn);
  1038. err = 0;
  1039. break;
  1040. #endif
  1041. default:
  1042. err = -EINVAL;
  1043. }
  1044. done:
  1045. return err;
  1046. }
  1047. static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  1048. {
  1049. if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
  1050. return -EINVAL;
  1051. dev->mtu = new_mtu;
  1052. return 0;
  1053. }
  1054. static const struct net_device_ops ipip6_netdev_ops = {
  1055. .ndo_uninit = ipip6_tunnel_uninit,
  1056. .ndo_start_xmit = sit_tunnel_xmit,
  1057. .ndo_do_ioctl = ipip6_tunnel_ioctl,
  1058. .ndo_change_mtu = ipip6_tunnel_change_mtu,
  1059. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1060. };
  1061. static void ipip6_dev_free(struct net_device *dev)
  1062. {
  1063. free_percpu(dev->tstats);
  1064. free_netdev(dev);
  1065. }
  1066. static void ipip6_tunnel_setup(struct net_device *dev)
  1067. {
  1068. dev->netdev_ops = &ipip6_netdev_ops;
  1069. dev->destructor = ipip6_dev_free;
  1070. dev->type = ARPHRD_SIT;
  1071. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
  1072. dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
  1073. dev->flags = IFF_NOARP;
  1074. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1075. dev->iflink = 0;
  1076. dev->addr_len = 4;
  1077. dev->features |= NETIF_F_LLTX;
  1078. }
  1079. static int ipip6_tunnel_init(struct net_device *dev)
  1080. {
  1081. struct ip_tunnel *tunnel = netdev_priv(dev);
  1082. tunnel->dev = dev;
  1083. tunnel->net = dev_net(dev);
  1084. memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
  1085. memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
  1086. ipip6_tunnel_bind_dev(dev);
  1087. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1088. if (!dev->tstats)
  1089. return -ENOMEM;
  1090. return 0;
  1091. }
  1092. static int __net_init ipip6_fb_tunnel_init(struct net_device *dev)
  1093. {
  1094. struct ip_tunnel *tunnel = netdev_priv(dev);
  1095. struct iphdr *iph = &tunnel->parms.iph;
  1096. struct net *net = dev_net(dev);
  1097. struct sit_net *sitn = net_generic(net, sit_net_id);
  1098. tunnel->dev = dev;
  1099. tunnel->net = dev_net(dev);
  1100. strcpy(tunnel->parms.name, dev->name);
  1101. iph->version = 4;
  1102. iph->protocol = IPPROTO_IPV6;
  1103. iph->ihl = 5;
  1104. iph->ttl = 64;
  1105. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1106. if (!dev->tstats)
  1107. return -ENOMEM;
  1108. dev_hold(dev);
  1109. rcu_assign_pointer(sitn->tunnels_wc[0], tunnel);
  1110. return 0;
  1111. }
  1112. static int ipip6_validate(struct nlattr *tb[], struct nlattr *data[])
  1113. {
  1114. u8 proto;
  1115. if (!data || !data[IFLA_IPTUN_PROTO])
  1116. return 0;
  1117. proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1118. if (proto != IPPROTO_IPV6 &&
  1119. proto != IPPROTO_IPIP &&
  1120. proto != 0)
  1121. return -EINVAL;
  1122. return 0;
  1123. }
  1124. static void ipip6_netlink_parms(struct nlattr *data[],
  1125. struct ip_tunnel_parm *parms)
  1126. {
  1127. memset(parms, 0, sizeof(*parms));
  1128. parms->iph.version = 4;
  1129. parms->iph.protocol = IPPROTO_IPV6;
  1130. parms->iph.ihl = 5;
  1131. parms->iph.ttl = 64;
  1132. if (!data)
  1133. return;
  1134. if (data[IFLA_IPTUN_LINK])
  1135. parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
  1136. if (data[IFLA_IPTUN_LOCAL])
  1137. parms->iph.saddr = nla_get_be32(data[IFLA_IPTUN_LOCAL]);
  1138. if (data[IFLA_IPTUN_REMOTE])
  1139. parms->iph.daddr = nla_get_be32(data[IFLA_IPTUN_REMOTE]);
  1140. if (data[IFLA_IPTUN_TTL]) {
  1141. parms->iph.ttl = nla_get_u8(data[IFLA_IPTUN_TTL]);
  1142. if (parms->iph.ttl)
  1143. parms->iph.frag_off = htons(IP_DF);
  1144. }
  1145. if (data[IFLA_IPTUN_TOS])
  1146. parms->iph.tos = nla_get_u8(data[IFLA_IPTUN_TOS]);
  1147. if (!data[IFLA_IPTUN_PMTUDISC] || nla_get_u8(data[IFLA_IPTUN_PMTUDISC]))
  1148. parms->iph.frag_off = htons(IP_DF);
  1149. if (data[IFLA_IPTUN_FLAGS])
  1150. parms->i_flags = nla_get_be16(data[IFLA_IPTUN_FLAGS]);
  1151. if (data[IFLA_IPTUN_PROTO])
  1152. parms->iph.protocol = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1153. }
  1154. #ifdef CONFIG_IPV6_SIT_6RD
  1155. /* This function returns true when 6RD attributes are present in the nl msg */
  1156. static bool ipip6_netlink_6rd_parms(struct nlattr *data[],
  1157. struct ip_tunnel_6rd *ip6rd)
  1158. {
  1159. bool ret = false;
  1160. memset(ip6rd, 0, sizeof(*ip6rd));
  1161. if (!data)
  1162. return ret;
  1163. if (data[IFLA_IPTUN_6RD_PREFIX]) {
  1164. ret = true;
  1165. nla_memcpy(&ip6rd->prefix, data[IFLA_IPTUN_6RD_PREFIX],
  1166. sizeof(struct in6_addr));
  1167. }
  1168. if (data[IFLA_IPTUN_6RD_RELAY_PREFIX]) {
  1169. ret = true;
  1170. ip6rd->relay_prefix =
  1171. nla_get_be32(data[IFLA_IPTUN_6RD_RELAY_PREFIX]);
  1172. }
  1173. if (data[IFLA_IPTUN_6RD_PREFIXLEN]) {
  1174. ret = true;
  1175. ip6rd->prefixlen = nla_get_u16(data[IFLA_IPTUN_6RD_PREFIXLEN]);
  1176. }
  1177. if (data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]) {
  1178. ret = true;
  1179. ip6rd->relay_prefixlen =
  1180. nla_get_u16(data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]);
  1181. }
  1182. return ret;
  1183. }
  1184. #endif
  1185. static int ipip6_newlink(struct net *src_net, struct net_device *dev,
  1186. struct nlattr *tb[], struct nlattr *data[])
  1187. {
  1188. struct net *net = dev_net(dev);
  1189. struct ip_tunnel *nt;
  1190. #ifdef CONFIG_IPV6_SIT_6RD
  1191. struct ip_tunnel_6rd ip6rd;
  1192. #endif
  1193. int err;
  1194. nt = netdev_priv(dev);
  1195. ipip6_netlink_parms(data, &nt->parms);
  1196. if (ipip6_tunnel_locate(net, &nt->parms, 0))
  1197. return -EEXIST;
  1198. err = ipip6_tunnel_create(dev);
  1199. if (err < 0)
  1200. return err;
  1201. #ifdef CONFIG_IPV6_SIT_6RD
  1202. if (ipip6_netlink_6rd_parms(data, &ip6rd))
  1203. err = ipip6_tunnel_update_6rd(nt, &ip6rd);
  1204. #endif
  1205. return err;
  1206. }
  1207. static int ipip6_changelink(struct net_device *dev, struct nlattr *tb[],
  1208. struct nlattr *data[])
  1209. {
  1210. struct ip_tunnel *t = netdev_priv(dev);
  1211. struct ip_tunnel_parm p;
  1212. struct net *net = t->net;
  1213. struct sit_net *sitn = net_generic(net, sit_net_id);
  1214. #ifdef CONFIG_IPV6_SIT_6RD
  1215. struct ip_tunnel_6rd ip6rd;
  1216. #endif
  1217. if (dev == sitn->fb_tunnel_dev)
  1218. return -EINVAL;
  1219. ipip6_netlink_parms(data, &p);
  1220. if (((dev->flags & IFF_POINTOPOINT) && !p.iph.daddr) ||
  1221. (!(dev->flags & IFF_POINTOPOINT) && p.iph.daddr))
  1222. return -EINVAL;
  1223. t = ipip6_tunnel_locate(net, &p, 0);
  1224. if (t) {
  1225. if (t->dev != dev)
  1226. return -EEXIST;
  1227. } else
  1228. t = netdev_priv(dev);
  1229. ipip6_tunnel_update(t, &p);
  1230. #ifdef CONFIG_IPV6_SIT_6RD
  1231. if (ipip6_netlink_6rd_parms(data, &ip6rd))
  1232. return ipip6_tunnel_update_6rd(t, &ip6rd);
  1233. #endif
  1234. return 0;
  1235. }
  1236. static size_t ipip6_get_size(const struct net_device *dev)
  1237. {
  1238. return
  1239. /* IFLA_IPTUN_LINK */
  1240. nla_total_size(4) +
  1241. /* IFLA_IPTUN_LOCAL */
  1242. nla_total_size(4) +
  1243. /* IFLA_IPTUN_REMOTE */
  1244. nla_total_size(4) +
  1245. /* IFLA_IPTUN_TTL */
  1246. nla_total_size(1) +
  1247. /* IFLA_IPTUN_TOS */
  1248. nla_total_size(1) +
  1249. /* IFLA_IPTUN_PMTUDISC */
  1250. nla_total_size(1) +
  1251. /* IFLA_IPTUN_FLAGS */
  1252. nla_total_size(2) +
  1253. /* IFLA_IPTUN_PROTO */
  1254. nla_total_size(1) +
  1255. #ifdef CONFIG_IPV6_SIT_6RD
  1256. /* IFLA_IPTUN_6RD_PREFIX */
  1257. nla_total_size(sizeof(struct in6_addr)) +
  1258. /* IFLA_IPTUN_6RD_RELAY_PREFIX */
  1259. nla_total_size(4) +
  1260. /* IFLA_IPTUN_6RD_PREFIXLEN */
  1261. nla_total_size(2) +
  1262. /* IFLA_IPTUN_6RD_RELAY_PREFIXLEN */
  1263. nla_total_size(2) +
  1264. #endif
  1265. 0;
  1266. }
  1267. static int ipip6_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1268. {
  1269. struct ip_tunnel *tunnel = netdev_priv(dev);
  1270. struct ip_tunnel_parm *parm = &tunnel->parms;
  1271. if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
  1272. nla_put_be32(skb, IFLA_IPTUN_LOCAL, parm->iph.saddr) ||
  1273. nla_put_be32(skb, IFLA_IPTUN_REMOTE, parm->iph.daddr) ||
  1274. nla_put_u8(skb, IFLA_IPTUN_TTL, parm->iph.ttl) ||
  1275. nla_put_u8(skb, IFLA_IPTUN_TOS, parm->iph.tos) ||
  1276. nla_put_u8(skb, IFLA_IPTUN_PMTUDISC,
  1277. !!(parm->iph.frag_off & htons(IP_DF))) ||
  1278. nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->iph.protocol) ||
  1279. nla_put_be16(skb, IFLA_IPTUN_FLAGS, parm->i_flags))
  1280. goto nla_put_failure;
  1281. #ifdef CONFIG_IPV6_SIT_6RD
  1282. if (nla_put(skb, IFLA_IPTUN_6RD_PREFIX, sizeof(struct in6_addr),
  1283. &tunnel->ip6rd.prefix) ||
  1284. nla_put_be32(skb, IFLA_IPTUN_6RD_RELAY_PREFIX,
  1285. tunnel->ip6rd.relay_prefix) ||
  1286. nla_put_u16(skb, IFLA_IPTUN_6RD_PREFIXLEN,
  1287. tunnel->ip6rd.prefixlen) ||
  1288. nla_put_u16(skb, IFLA_IPTUN_6RD_RELAY_PREFIXLEN,
  1289. tunnel->ip6rd.relay_prefixlen))
  1290. goto nla_put_failure;
  1291. #endif
  1292. return 0;
  1293. nla_put_failure:
  1294. return -EMSGSIZE;
  1295. }
  1296. static const struct nla_policy ipip6_policy[IFLA_IPTUN_MAX + 1] = {
  1297. [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
  1298. [IFLA_IPTUN_LOCAL] = { .type = NLA_U32 },
  1299. [IFLA_IPTUN_REMOTE] = { .type = NLA_U32 },
  1300. [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
  1301. [IFLA_IPTUN_TOS] = { .type = NLA_U8 },
  1302. [IFLA_IPTUN_PMTUDISC] = { .type = NLA_U8 },
  1303. [IFLA_IPTUN_FLAGS] = { .type = NLA_U16 },
  1304. [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
  1305. #ifdef CONFIG_IPV6_SIT_6RD
  1306. [IFLA_IPTUN_6RD_PREFIX] = { .len = sizeof(struct in6_addr) },
  1307. [IFLA_IPTUN_6RD_RELAY_PREFIX] = { .type = NLA_U32 },
  1308. [IFLA_IPTUN_6RD_PREFIXLEN] = { .type = NLA_U16 },
  1309. [IFLA_IPTUN_6RD_RELAY_PREFIXLEN] = { .type = NLA_U16 },
  1310. #endif
  1311. };
  1312. static struct rtnl_link_ops sit_link_ops __read_mostly = {
  1313. .kind = "sit",
  1314. .maxtype = IFLA_IPTUN_MAX,
  1315. .policy = ipip6_policy,
  1316. .priv_size = sizeof(struct ip_tunnel),
  1317. .setup = ipip6_tunnel_setup,
  1318. .validate = ipip6_validate,
  1319. .newlink = ipip6_newlink,
  1320. .changelink = ipip6_changelink,
  1321. .get_size = ipip6_get_size,
  1322. .fill_info = ipip6_fill_info,
  1323. };
  1324. static struct xfrm_tunnel sit_handler __read_mostly = {
  1325. .handler = ipip6_rcv,
  1326. .err_handler = ipip6_err,
  1327. .priority = 1,
  1328. };
  1329. static struct xfrm_tunnel ipip_handler __read_mostly = {
  1330. .handler = ipip_rcv,
  1331. .err_handler = ipip6_err,
  1332. .priority = 2,
  1333. };
  1334. static void __net_exit sit_destroy_tunnels(struct sit_net *sitn, struct list_head *head)
  1335. {
  1336. struct net *net = dev_net(sitn->fb_tunnel_dev);
  1337. struct net_device *dev, *aux;
  1338. int prio;
  1339. for_each_netdev_safe(net, dev, aux)
  1340. if (dev->rtnl_link_ops == &sit_link_ops)
  1341. unregister_netdevice_queue(dev, head);
  1342. for (prio = 1; prio < 4; prio++) {
  1343. int h;
  1344. for (h = 0; h < HASH_SIZE; h++) {
  1345. struct ip_tunnel *t;
  1346. t = rtnl_dereference(sitn->tunnels[prio][h]);
  1347. while (t != NULL) {
  1348. /* If dev is in the same netns, it has already
  1349. * been added to the list by the previous loop.
  1350. */
  1351. if (dev_net(t->dev) != net)
  1352. unregister_netdevice_queue(t->dev,
  1353. head);
  1354. t = rtnl_dereference(t->next);
  1355. }
  1356. }
  1357. }
  1358. }
  1359. static int __net_init sit_init_net(struct net *net)
  1360. {
  1361. struct sit_net *sitn = net_generic(net, sit_net_id);
  1362. struct ip_tunnel *t;
  1363. int err;
  1364. sitn->tunnels[0] = sitn->tunnels_wc;
  1365. sitn->tunnels[1] = sitn->tunnels_l;
  1366. sitn->tunnels[2] = sitn->tunnels_r;
  1367. sitn->tunnels[3] = sitn->tunnels_r_l;
  1368. sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
  1369. ipip6_tunnel_setup);
  1370. if (!sitn->fb_tunnel_dev) {
  1371. err = -ENOMEM;
  1372. goto err_alloc_dev;
  1373. }
  1374. dev_net_set(sitn->fb_tunnel_dev, net);
  1375. /* FB netdevice is special: we have one, and only one per netns.
  1376. * Allowing to move it to another netns is clearly unsafe.
  1377. */
  1378. sitn->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
  1379. err = ipip6_fb_tunnel_init(sitn->fb_tunnel_dev);
  1380. if (err)
  1381. goto err_dev_free;
  1382. ipip6_tunnel_clone_6rd(sitn->fb_tunnel_dev, sitn);
  1383. if ((err = register_netdev(sitn->fb_tunnel_dev)))
  1384. goto err_reg_dev;
  1385. t = netdev_priv(sitn->fb_tunnel_dev);
  1386. strcpy(t->parms.name, sitn->fb_tunnel_dev->name);
  1387. return 0;
  1388. err_reg_dev:
  1389. dev_put(sitn->fb_tunnel_dev);
  1390. err_dev_free:
  1391. ipip6_dev_free(sitn->fb_tunnel_dev);
  1392. err_alloc_dev:
  1393. return err;
  1394. }
  1395. static void __net_exit sit_exit_net(struct net *net)
  1396. {
  1397. struct sit_net *sitn = net_generic(net, sit_net_id);
  1398. LIST_HEAD(list);
  1399. rtnl_lock();
  1400. sit_destroy_tunnels(sitn, &list);
  1401. unregister_netdevice_queue(sitn->fb_tunnel_dev, &list);
  1402. unregister_netdevice_many(&list);
  1403. rtnl_unlock();
  1404. }
  1405. static struct pernet_operations sit_net_ops = {
  1406. .init = sit_init_net,
  1407. .exit = sit_exit_net,
  1408. .id = &sit_net_id,
  1409. .size = sizeof(struct sit_net),
  1410. };
  1411. static void __exit sit_cleanup(void)
  1412. {
  1413. rtnl_link_unregister(&sit_link_ops);
  1414. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  1415. xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
  1416. unregister_pernet_device(&sit_net_ops);
  1417. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  1418. }
  1419. static int __init sit_init(void)
  1420. {
  1421. int err;
  1422. pr_info("IPv6 over IPv4 tunneling driver\n");
  1423. err = register_pernet_device(&sit_net_ops);
  1424. if (err < 0)
  1425. return err;
  1426. err = xfrm4_tunnel_register(&sit_handler, AF_INET6);
  1427. if (err < 0) {
  1428. pr_info("%s: can't register ip6ip4\n", __func__);
  1429. goto xfrm_tunnel_failed;
  1430. }
  1431. err = xfrm4_tunnel_register(&ipip_handler, AF_INET);
  1432. if (err < 0) {
  1433. pr_info("%s: can't register ip4ip4\n", __func__);
  1434. goto xfrm_tunnel4_failed;
  1435. }
  1436. err = rtnl_link_register(&sit_link_ops);
  1437. if (err < 0)
  1438. goto rtnl_link_failed;
  1439. out:
  1440. return err;
  1441. rtnl_link_failed:
  1442. xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
  1443. xfrm_tunnel4_failed:
  1444. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  1445. xfrm_tunnel_failed:
  1446. unregister_pernet_device(&sit_net_ops);
  1447. goto out;
  1448. }
  1449. module_init(sit_init);
  1450. module_exit(sit_cleanup);
  1451. MODULE_LICENSE("GPL");
  1452. MODULE_ALIAS_NETDEV("sit0");