sit.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  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/ipip.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 int ipip6_tunnel_init(struct net_device *dev);
  61. static void ipip6_tunnel_setup(struct net_device *dev);
  62. static void ipip6_dev_free(struct net_device *dev);
  63. static struct rtnl_link_ops sit_link_ops __read_mostly;
  64. static int sit_net_id __read_mostly;
  65. struct sit_net {
  66. struct ip_tunnel __rcu *tunnels_r_l[HASH_SIZE];
  67. struct ip_tunnel __rcu *tunnels_r[HASH_SIZE];
  68. struct ip_tunnel __rcu *tunnels_l[HASH_SIZE];
  69. struct ip_tunnel __rcu *tunnels_wc[1];
  70. struct ip_tunnel __rcu **tunnels[4];
  71. struct net_device *fb_tunnel_dev;
  72. };
  73. static struct rtnl_link_stats64 *ipip6_get_stats64(struct net_device *dev,
  74. struct rtnl_link_stats64 *tot)
  75. {
  76. int i;
  77. for_each_possible_cpu(i) {
  78. const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i);
  79. u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
  80. unsigned int start;
  81. do {
  82. start = u64_stats_fetch_begin_bh(&tstats->syncp);
  83. rx_packets = tstats->rx_packets;
  84. tx_packets = tstats->tx_packets;
  85. rx_bytes = tstats->rx_bytes;
  86. tx_bytes = tstats->tx_bytes;
  87. } while (u64_stats_fetch_retry_bh(&tstats->syncp, start));
  88. tot->rx_packets += rx_packets;
  89. tot->tx_packets += tx_packets;
  90. tot->rx_bytes += rx_bytes;
  91. tot->tx_bytes += tx_bytes;
  92. }
  93. tot->rx_errors = dev->stats.rx_errors;
  94. tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
  95. tot->tx_carrier_errors = dev->stats.tx_carrier_errors;
  96. tot->tx_dropped = dev->stats.tx_dropped;
  97. tot->tx_aborted_errors = dev->stats.tx_aborted_errors;
  98. tot->tx_errors = dev->stats.tx_errors;
  99. return tot;
  100. }
  101. /*
  102. * Must be invoked with rcu_read_lock
  103. */
  104. static struct ip_tunnel *ipip6_tunnel_lookup(struct net *net,
  105. struct net_device *dev, __be32 remote, __be32 local)
  106. {
  107. unsigned int h0 = HASH(remote);
  108. unsigned int h1 = HASH(local);
  109. struct ip_tunnel *t;
  110. struct sit_net *sitn = net_generic(net, sit_net_id);
  111. for_each_ip_tunnel_rcu(t, sitn->tunnels_r_l[h0 ^ h1]) {
  112. if (local == t->parms.iph.saddr &&
  113. remote == t->parms.iph.daddr &&
  114. (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
  115. (t->dev->flags & IFF_UP))
  116. return t;
  117. }
  118. for_each_ip_tunnel_rcu(t, sitn->tunnels_r[h0]) {
  119. if (remote == t->parms.iph.daddr &&
  120. (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
  121. (t->dev->flags & IFF_UP))
  122. return t;
  123. }
  124. for_each_ip_tunnel_rcu(t, sitn->tunnels_l[h1]) {
  125. if (local == t->parms.iph.saddr &&
  126. (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
  127. (t->dev->flags & IFF_UP))
  128. return t;
  129. }
  130. t = rcu_dereference(sitn->tunnels_wc[0]);
  131. if ((t != NULL) && (t->dev->flags & IFF_UP))
  132. return t;
  133. return NULL;
  134. }
  135. static struct ip_tunnel __rcu **__ipip6_bucket(struct sit_net *sitn,
  136. struct ip_tunnel_parm *parms)
  137. {
  138. __be32 remote = parms->iph.daddr;
  139. __be32 local = parms->iph.saddr;
  140. unsigned int h = 0;
  141. int prio = 0;
  142. if (remote) {
  143. prio |= 2;
  144. h ^= HASH(remote);
  145. }
  146. if (local) {
  147. prio |= 1;
  148. h ^= HASH(local);
  149. }
  150. return &sitn->tunnels[prio][h];
  151. }
  152. static inline struct ip_tunnel __rcu **ipip6_bucket(struct sit_net *sitn,
  153. struct ip_tunnel *t)
  154. {
  155. return __ipip6_bucket(sitn, &t->parms);
  156. }
  157. static void ipip6_tunnel_unlink(struct sit_net *sitn, struct ip_tunnel *t)
  158. {
  159. struct ip_tunnel __rcu **tp;
  160. struct ip_tunnel *iter;
  161. for (tp = ipip6_bucket(sitn, t);
  162. (iter = rtnl_dereference(*tp)) != NULL;
  163. tp = &iter->next) {
  164. if (t == iter) {
  165. rcu_assign_pointer(*tp, t->next);
  166. break;
  167. }
  168. }
  169. }
  170. static void ipip6_tunnel_link(struct sit_net *sitn, struct ip_tunnel *t)
  171. {
  172. struct ip_tunnel __rcu **tp = ipip6_bucket(sitn, t);
  173. rcu_assign_pointer(t->next, rtnl_dereference(*tp));
  174. rcu_assign_pointer(*tp, t);
  175. }
  176. static void ipip6_tunnel_clone_6rd(struct net_device *dev, struct sit_net *sitn)
  177. {
  178. #ifdef CONFIG_IPV6_SIT_6RD
  179. struct ip_tunnel *t = netdev_priv(dev);
  180. if (t->dev == sitn->fb_tunnel_dev) {
  181. ipv6_addr_set(&t->ip6rd.prefix, htonl(0x20020000), 0, 0, 0);
  182. t->ip6rd.relay_prefix = 0;
  183. t->ip6rd.prefixlen = 16;
  184. t->ip6rd.relay_prefixlen = 0;
  185. } else {
  186. struct ip_tunnel *t0 = netdev_priv(sitn->fb_tunnel_dev);
  187. memcpy(&t->ip6rd, &t0->ip6rd, sizeof(t->ip6rd));
  188. }
  189. #endif
  190. }
  191. static int ipip6_tunnel_create(struct net_device *dev)
  192. {
  193. struct ip_tunnel *t = netdev_priv(dev);
  194. struct net *net = dev_net(dev);
  195. struct sit_net *sitn = net_generic(net, sit_net_id);
  196. int err;
  197. err = ipip6_tunnel_init(dev);
  198. if (err < 0)
  199. goto out;
  200. ipip6_tunnel_clone_6rd(dev, sitn);
  201. if ((__force u16)t->parms.i_flags & SIT_ISATAP)
  202. dev->priv_flags |= IFF_ISATAP;
  203. err = register_netdevice(dev);
  204. if (err < 0)
  205. goto out;
  206. strcpy(t->parms.name, dev->name);
  207. dev->rtnl_link_ops = &sit_link_ops;
  208. dev_hold(dev);
  209. ipip6_tunnel_link(sitn, t);
  210. return 0;
  211. out:
  212. return err;
  213. }
  214. static struct ip_tunnel *ipip6_tunnel_locate(struct net *net,
  215. struct ip_tunnel_parm *parms, int create)
  216. {
  217. __be32 remote = parms->iph.daddr;
  218. __be32 local = parms->iph.saddr;
  219. struct ip_tunnel *t, *nt;
  220. struct ip_tunnel __rcu **tp;
  221. struct net_device *dev;
  222. char name[IFNAMSIZ];
  223. struct sit_net *sitn = net_generic(net, sit_net_id);
  224. for (tp = __ipip6_bucket(sitn, parms);
  225. (t = rtnl_dereference(*tp)) != NULL;
  226. tp = &t->next) {
  227. if (local == t->parms.iph.saddr &&
  228. remote == t->parms.iph.daddr &&
  229. parms->link == t->parms.link) {
  230. if (create)
  231. return NULL;
  232. else
  233. return t;
  234. }
  235. }
  236. if (!create)
  237. goto failed;
  238. if (parms->name[0])
  239. strlcpy(name, parms->name, IFNAMSIZ);
  240. else
  241. strcpy(name, "sit%d");
  242. dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
  243. if (dev == NULL)
  244. return NULL;
  245. dev_net_set(dev, net);
  246. nt = netdev_priv(dev);
  247. nt->parms = *parms;
  248. if (ipip6_tunnel_create(dev) < 0)
  249. goto failed_free;
  250. return nt;
  251. failed_free:
  252. ipip6_dev_free(dev);
  253. failed:
  254. return NULL;
  255. }
  256. #define for_each_prl_rcu(start) \
  257. for (prl = rcu_dereference(start); \
  258. prl; \
  259. prl = rcu_dereference(prl->next))
  260. static struct ip_tunnel_prl_entry *
  261. __ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr)
  262. {
  263. struct ip_tunnel_prl_entry *prl;
  264. for_each_prl_rcu(t->prl)
  265. if (prl->addr == addr)
  266. break;
  267. return prl;
  268. }
  269. static int ipip6_tunnel_get_prl(struct ip_tunnel *t,
  270. struct ip_tunnel_prl __user *a)
  271. {
  272. struct ip_tunnel_prl kprl, *kp;
  273. struct ip_tunnel_prl_entry *prl;
  274. unsigned int cmax, c = 0, ca, len;
  275. int ret = 0;
  276. if (copy_from_user(&kprl, a, sizeof(kprl)))
  277. return -EFAULT;
  278. cmax = kprl.datalen / sizeof(kprl);
  279. if (cmax > 1 && kprl.addr != htonl(INADDR_ANY))
  280. cmax = 1;
  281. /* For simple GET or for root users,
  282. * we try harder to allocate.
  283. */
  284. kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
  285. kcalloc(cmax, sizeof(*kp), GFP_KERNEL) :
  286. NULL;
  287. rcu_read_lock();
  288. ca = t->prl_count < cmax ? t->prl_count : cmax;
  289. if (!kp) {
  290. /* We don't try hard to allocate much memory for
  291. * non-root users.
  292. * For root users, retry allocating enough memory for
  293. * the answer.
  294. */
  295. kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
  296. if (!kp) {
  297. ret = -ENOMEM;
  298. goto out;
  299. }
  300. }
  301. c = 0;
  302. for_each_prl_rcu(t->prl) {
  303. if (c >= cmax)
  304. break;
  305. if (kprl.addr != htonl(INADDR_ANY) && prl->addr != kprl.addr)
  306. continue;
  307. kp[c].addr = prl->addr;
  308. kp[c].flags = prl->flags;
  309. c++;
  310. if (kprl.addr != htonl(INADDR_ANY))
  311. break;
  312. }
  313. out:
  314. rcu_read_unlock();
  315. len = sizeof(*kp) * c;
  316. ret = 0;
  317. if ((len && copy_to_user(a + 1, kp, len)) || put_user(len, &a->datalen))
  318. ret = -EFAULT;
  319. kfree(kp);
  320. return ret;
  321. }
  322. static int
  323. ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg)
  324. {
  325. struct ip_tunnel_prl_entry *p;
  326. int err = 0;
  327. if (a->addr == htonl(INADDR_ANY))
  328. return -EINVAL;
  329. ASSERT_RTNL();
  330. for (p = rtnl_dereference(t->prl); p; p = rtnl_dereference(p->next)) {
  331. if (p->addr == a->addr) {
  332. if (chg) {
  333. p->flags = a->flags;
  334. goto out;
  335. }
  336. err = -EEXIST;
  337. goto out;
  338. }
  339. }
  340. if (chg) {
  341. err = -ENXIO;
  342. goto out;
  343. }
  344. p = kzalloc(sizeof(struct ip_tunnel_prl_entry), GFP_KERNEL);
  345. if (!p) {
  346. err = -ENOBUFS;
  347. goto out;
  348. }
  349. p->next = t->prl;
  350. p->addr = a->addr;
  351. p->flags = a->flags;
  352. t->prl_count++;
  353. rcu_assign_pointer(t->prl, p);
  354. out:
  355. return err;
  356. }
  357. static void prl_list_destroy_rcu(struct rcu_head *head)
  358. {
  359. struct ip_tunnel_prl_entry *p, *n;
  360. p = container_of(head, struct ip_tunnel_prl_entry, rcu_head);
  361. do {
  362. n = rcu_dereference_protected(p->next, 1);
  363. kfree(p);
  364. p = n;
  365. } while (p);
  366. }
  367. static int
  368. ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
  369. {
  370. struct ip_tunnel_prl_entry *x;
  371. struct ip_tunnel_prl_entry __rcu **p;
  372. int err = 0;
  373. ASSERT_RTNL();
  374. if (a && a->addr != htonl(INADDR_ANY)) {
  375. for (p = &t->prl;
  376. (x = rtnl_dereference(*p)) != NULL;
  377. p = &x->next) {
  378. if (x->addr == a->addr) {
  379. *p = x->next;
  380. kfree_rcu(x, rcu_head);
  381. t->prl_count--;
  382. goto out;
  383. }
  384. }
  385. err = -ENXIO;
  386. } else {
  387. x = rtnl_dereference(t->prl);
  388. if (x) {
  389. t->prl_count = 0;
  390. call_rcu(&x->rcu_head, prl_list_destroy_rcu);
  391. t->prl = NULL;
  392. }
  393. }
  394. out:
  395. return err;
  396. }
  397. static int
  398. isatap_chksrc(struct sk_buff *skb, const struct iphdr *iph, struct ip_tunnel *t)
  399. {
  400. struct ip_tunnel_prl_entry *p;
  401. int ok = 1;
  402. rcu_read_lock();
  403. p = __ipip6_tunnel_locate_prl(t, iph->saddr);
  404. if (p) {
  405. if (p->flags & PRL_DEFAULT)
  406. skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT;
  407. else
  408. skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT;
  409. } else {
  410. const struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr;
  411. if (ipv6_addr_is_isatap(addr6) &&
  412. (addr6->s6_addr32[3] == iph->saddr) &&
  413. ipv6_chk_prefix(addr6, t->dev))
  414. skb->ndisc_nodetype = NDISC_NODETYPE_HOST;
  415. else
  416. ok = 0;
  417. }
  418. rcu_read_unlock();
  419. return ok;
  420. }
  421. static void ipip6_tunnel_uninit(struct net_device *dev)
  422. {
  423. struct net *net = dev_net(dev);
  424. struct sit_net *sitn = net_generic(net, sit_net_id);
  425. if (dev == sitn->fb_tunnel_dev) {
  426. RCU_INIT_POINTER(sitn->tunnels_wc[0], NULL);
  427. } else {
  428. ipip6_tunnel_unlink(sitn, netdev_priv(dev));
  429. ipip6_tunnel_del_prl(netdev_priv(dev), NULL);
  430. }
  431. dev_put(dev);
  432. }
  433. static int ipip6_err(struct sk_buff *skb, u32 info)
  434. {
  435. /* All the routers (except for Linux) return only
  436. 8 bytes of packet payload. It means, that precise relaying of
  437. ICMP in the real Internet is absolutely infeasible.
  438. */
  439. const struct iphdr *iph = (const struct iphdr *)skb->data;
  440. const int type = icmp_hdr(skb)->type;
  441. const int code = icmp_hdr(skb)->code;
  442. struct ip_tunnel *t;
  443. int err;
  444. switch (type) {
  445. default:
  446. case ICMP_PARAMETERPROB:
  447. return 0;
  448. case ICMP_DEST_UNREACH:
  449. switch (code) {
  450. case ICMP_SR_FAILED:
  451. case ICMP_PORT_UNREACH:
  452. /* Impossible event. */
  453. return 0;
  454. default:
  455. /* All others are translated to HOST_UNREACH.
  456. rfc2003 contains "deep thoughts" about NET_UNREACH,
  457. I believe they are just ether pollution. --ANK
  458. */
  459. break;
  460. }
  461. break;
  462. case ICMP_TIME_EXCEEDED:
  463. if (code != ICMP_EXC_TTL)
  464. return 0;
  465. break;
  466. case ICMP_REDIRECT:
  467. break;
  468. }
  469. err = -ENOENT;
  470. t = ipip6_tunnel_lookup(dev_net(skb->dev),
  471. skb->dev,
  472. iph->daddr,
  473. iph->saddr);
  474. if (t == NULL)
  475. goto out;
  476. if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
  477. ipv4_update_pmtu(skb, dev_net(skb->dev), info,
  478. t->dev->ifindex, 0, IPPROTO_IPV6, 0);
  479. err = 0;
  480. goto out;
  481. }
  482. if (type == ICMP_REDIRECT) {
  483. ipv4_redirect(skb, dev_net(skb->dev), t->dev->ifindex, 0,
  484. IPPROTO_IPV6, 0);
  485. err = 0;
  486. goto out;
  487. }
  488. if (t->parms.iph.daddr == 0)
  489. goto out;
  490. err = 0;
  491. if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
  492. goto out;
  493. if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
  494. t->err_count++;
  495. else
  496. t->err_count = 1;
  497. t->err_time = jiffies;
  498. out:
  499. return err;
  500. }
  501. static inline void ipip6_ecn_decapsulate(const struct iphdr *iph, struct sk_buff *skb)
  502. {
  503. if (INET_ECN_is_ce(iph->tos))
  504. IP6_ECN_set_ce(ipv6_hdr(skb));
  505. }
  506. static int ipip6_rcv(struct sk_buff *skb)
  507. {
  508. const struct iphdr *iph;
  509. struct ip_tunnel *tunnel;
  510. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  511. goto out;
  512. iph = ip_hdr(skb);
  513. tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
  514. iph->saddr, iph->daddr);
  515. if (tunnel != NULL) {
  516. struct pcpu_tstats *tstats;
  517. secpath_reset(skb);
  518. skb->mac_header = skb->network_header;
  519. skb_reset_network_header(skb);
  520. IPCB(skb)->flags = 0;
  521. skb->protocol = htons(ETH_P_IPV6);
  522. skb->pkt_type = PACKET_HOST;
  523. if ((tunnel->dev->priv_flags & IFF_ISATAP) &&
  524. !isatap_chksrc(skb, iph, tunnel)) {
  525. tunnel->dev->stats.rx_errors++;
  526. kfree_skb(skb);
  527. return 0;
  528. }
  529. tstats = this_cpu_ptr(tunnel->dev->tstats);
  530. tstats->rx_packets++;
  531. tstats->rx_bytes += skb->len;
  532. __skb_tunnel_rx(skb, tunnel->dev);
  533. ipip6_ecn_decapsulate(iph, skb);
  534. netif_rx(skb);
  535. return 0;
  536. }
  537. /* no tunnel matched, let upstream know, ipsec may handle it */
  538. return 1;
  539. out:
  540. kfree_skb(skb);
  541. return 0;
  542. }
  543. /*
  544. * Returns the embedded IPv4 address if the IPv6 address
  545. * comes from 6rd / 6to4 (RFC 3056) addr space.
  546. */
  547. static inline
  548. __be32 try_6rd(const struct in6_addr *v6dst, struct ip_tunnel *tunnel)
  549. {
  550. __be32 dst = 0;
  551. #ifdef CONFIG_IPV6_SIT_6RD
  552. if (ipv6_prefix_equal(v6dst, &tunnel->ip6rd.prefix,
  553. tunnel->ip6rd.prefixlen)) {
  554. unsigned int pbw0, pbi0;
  555. int pbi1;
  556. u32 d;
  557. pbw0 = tunnel->ip6rd.prefixlen >> 5;
  558. pbi0 = tunnel->ip6rd.prefixlen & 0x1f;
  559. d = (ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >>
  560. tunnel->ip6rd.relay_prefixlen;
  561. pbi1 = pbi0 - tunnel->ip6rd.relay_prefixlen;
  562. if (pbi1 > 0)
  563. d |= ntohl(v6dst->s6_addr32[pbw0 + 1]) >>
  564. (32 - pbi1);
  565. dst = tunnel->ip6rd.relay_prefix | htonl(d);
  566. }
  567. #else
  568. if (v6dst->s6_addr16[0] == htons(0x2002)) {
  569. /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
  570. memcpy(&dst, &v6dst->s6_addr16[1], 4);
  571. }
  572. #endif
  573. return dst;
  574. }
  575. /*
  576. * This function assumes it is being called from dev_queue_xmit()
  577. * and that skb is filled properly by that function.
  578. */
  579. static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
  580. struct net_device *dev)
  581. {
  582. struct ip_tunnel *tunnel = netdev_priv(dev);
  583. const struct iphdr *tiph = &tunnel->parms.iph;
  584. const struct ipv6hdr *iph6 = ipv6_hdr(skb);
  585. u8 tos = tunnel->parms.iph.tos;
  586. __be16 df = tiph->frag_off;
  587. struct rtable *rt; /* Route to the other host */
  588. struct net_device *tdev; /* Device to other host */
  589. struct iphdr *iph; /* Our new IP header */
  590. unsigned int max_headroom; /* The extra header space needed */
  591. __be32 dst = tiph->daddr;
  592. struct flowi4 fl4;
  593. int mtu;
  594. const struct in6_addr *addr6;
  595. int addr_type;
  596. if (skb->protocol != htons(ETH_P_IPV6))
  597. goto tx_error;
  598. if (tos == 1)
  599. tos = ipv6_get_dsfield(iph6);
  600. /* ISATAP (RFC4214) - must come before 6to4 */
  601. if (dev->priv_flags & IFF_ISATAP) {
  602. struct neighbour *neigh = NULL;
  603. bool do_tx_error = false;
  604. if (skb_dst(skb))
  605. neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
  606. if (neigh == NULL) {
  607. net_dbg_ratelimited("sit: nexthop == NULL\n");
  608. goto tx_error;
  609. }
  610. addr6 = (const struct in6_addr *)&neigh->primary_key;
  611. addr_type = ipv6_addr_type(addr6);
  612. if ((addr_type & IPV6_ADDR_UNICAST) &&
  613. ipv6_addr_is_isatap(addr6))
  614. dst = addr6->s6_addr32[3];
  615. else
  616. do_tx_error = true;
  617. neigh_release(neigh);
  618. if (do_tx_error)
  619. goto tx_error;
  620. }
  621. if (!dst)
  622. dst = try_6rd(&iph6->daddr, tunnel);
  623. if (!dst) {
  624. struct neighbour *neigh = NULL;
  625. bool do_tx_error = false;
  626. if (skb_dst(skb))
  627. neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
  628. if (neigh == NULL) {
  629. net_dbg_ratelimited("sit: nexthop == NULL\n");
  630. goto tx_error;
  631. }
  632. addr6 = (const struct in6_addr *)&neigh->primary_key;
  633. addr_type = ipv6_addr_type(addr6);
  634. if (addr_type == IPV6_ADDR_ANY) {
  635. addr6 = &ipv6_hdr(skb)->daddr;
  636. addr_type = ipv6_addr_type(addr6);
  637. }
  638. if ((addr_type & IPV6_ADDR_COMPATv4) != 0)
  639. dst = addr6->s6_addr32[3];
  640. else
  641. do_tx_error = true;
  642. neigh_release(neigh);
  643. if (do_tx_error)
  644. goto tx_error;
  645. }
  646. rt = ip_route_output_ports(dev_net(dev), &fl4, NULL,
  647. dst, tiph->saddr,
  648. 0, 0,
  649. IPPROTO_IPV6, RT_TOS(tos),
  650. tunnel->parms.link);
  651. if (IS_ERR(rt)) {
  652. dev->stats.tx_carrier_errors++;
  653. goto tx_error_icmp;
  654. }
  655. if (rt->rt_type != RTN_UNICAST) {
  656. ip_rt_put(rt);
  657. dev->stats.tx_carrier_errors++;
  658. goto tx_error_icmp;
  659. }
  660. tdev = rt->dst.dev;
  661. if (tdev == dev) {
  662. ip_rt_put(rt);
  663. dev->stats.collisions++;
  664. goto tx_error;
  665. }
  666. if (df) {
  667. mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
  668. if (mtu < 68) {
  669. dev->stats.collisions++;
  670. ip_rt_put(rt);
  671. goto tx_error;
  672. }
  673. if (mtu < IPV6_MIN_MTU) {
  674. mtu = IPV6_MIN_MTU;
  675. df = 0;
  676. }
  677. if (tunnel->parms.iph.daddr && skb_dst(skb))
  678. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
  679. if (skb->len > mtu) {
  680. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  681. ip_rt_put(rt);
  682. goto tx_error;
  683. }
  684. }
  685. if (tunnel->err_count > 0) {
  686. if (time_before(jiffies,
  687. tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
  688. tunnel->err_count--;
  689. dst_link_failure(skb);
  690. } else
  691. tunnel->err_count = 0;
  692. }
  693. /*
  694. * Okay, now see if we can stuff it in the buffer as-is.
  695. */
  696. max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
  697. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  698. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  699. struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
  700. if (!new_skb) {
  701. ip_rt_put(rt);
  702. dev->stats.tx_dropped++;
  703. dev_kfree_skb(skb);
  704. return NETDEV_TX_OK;
  705. }
  706. if (skb->sk)
  707. skb_set_owner_w(new_skb, skb->sk);
  708. dev_kfree_skb(skb);
  709. skb = new_skb;
  710. iph6 = ipv6_hdr(skb);
  711. }
  712. skb->transport_header = skb->network_header;
  713. skb_push(skb, sizeof(struct iphdr));
  714. skb_reset_network_header(skb);
  715. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  716. IPCB(skb)->flags = 0;
  717. skb_dst_drop(skb);
  718. skb_dst_set(skb, &rt->dst);
  719. /*
  720. * Push down and install the IPIP header.
  721. */
  722. iph = ip_hdr(skb);
  723. iph->version = 4;
  724. iph->ihl = sizeof(struct iphdr)>>2;
  725. iph->frag_off = df;
  726. iph->protocol = IPPROTO_IPV6;
  727. iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
  728. iph->daddr = fl4.daddr;
  729. iph->saddr = fl4.saddr;
  730. if ((iph->ttl = tiph->ttl) == 0)
  731. iph->ttl = iph6->hop_limit;
  732. iptunnel_xmit(skb, dev);
  733. return NETDEV_TX_OK;
  734. tx_error_icmp:
  735. dst_link_failure(skb);
  736. tx_error:
  737. dev->stats.tx_errors++;
  738. dev_kfree_skb(skb);
  739. return NETDEV_TX_OK;
  740. }
  741. static void ipip6_tunnel_bind_dev(struct net_device *dev)
  742. {
  743. struct net_device *tdev = NULL;
  744. struct ip_tunnel *tunnel;
  745. const struct iphdr *iph;
  746. struct flowi4 fl4;
  747. tunnel = netdev_priv(dev);
  748. iph = &tunnel->parms.iph;
  749. if (iph->daddr) {
  750. struct rtable *rt = ip_route_output_ports(dev_net(dev), &fl4, NULL,
  751. iph->daddr, iph->saddr,
  752. 0, 0,
  753. IPPROTO_IPV6,
  754. RT_TOS(iph->tos),
  755. tunnel->parms.link);
  756. if (!IS_ERR(rt)) {
  757. tdev = rt->dst.dev;
  758. ip_rt_put(rt);
  759. }
  760. dev->flags |= IFF_POINTOPOINT;
  761. }
  762. if (!tdev && tunnel->parms.link)
  763. tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
  764. if (tdev) {
  765. dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
  766. dev->mtu = tdev->mtu - sizeof(struct iphdr);
  767. if (dev->mtu < IPV6_MIN_MTU)
  768. dev->mtu = IPV6_MIN_MTU;
  769. }
  770. dev->iflink = tunnel->parms.link;
  771. }
  772. static void ipip6_tunnel_update(struct ip_tunnel *t, struct ip_tunnel_parm *p)
  773. {
  774. struct net *net = dev_net(t->dev);
  775. struct sit_net *sitn = net_generic(net, sit_net_id);
  776. ipip6_tunnel_unlink(sitn, t);
  777. synchronize_net();
  778. t->parms.iph.saddr = p->iph.saddr;
  779. t->parms.iph.daddr = p->iph.daddr;
  780. memcpy(t->dev->dev_addr, &p->iph.saddr, 4);
  781. memcpy(t->dev->broadcast, &p->iph.daddr, 4);
  782. ipip6_tunnel_link(sitn, t);
  783. t->parms.iph.ttl = p->iph.ttl;
  784. t->parms.iph.tos = p->iph.tos;
  785. if (t->parms.link != p->link) {
  786. t->parms.link = p->link;
  787. ipip6_tunnel_bind_dev(t->dev);
  788. }
  789. netdev_state_change(t->dev);
  790. }
  791. static int
  792. ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
  793. {
  794. int err = 0;
  795. struct ip_tunnel_parm p;
  796. struct ip_tunnel_prl prl;
  797. struct ip_tunnel *t;
  798. struct net *net = dev_net(dev);
  799. struct sit_net *sitn = net_generic(net, sit_net_id);
  800. #ifdef CONFIG_IPV6_SIT_6RD
  801. struct ip_tunnel_6rd ip6rd;
  802. #endif
  803. switch (cmd) {
  804. case SIOCGETTUNNEL:
  805. #ifdef CONFIG_IPV6_SIT_6RD
  806. case SIOCGET6RD:
  807. #endif
  808. t = NULL;
  809. if (dev == sitn->fb_tunnel_dev) {
  810. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  811. err = -EFAULT;
  812. break;
  813. }
  814. t = ipip6_tunnel_locate(net, &p, 0);
  815. }
  816. if (t == NULL)
  817. t = netdev_priv(dev);
  818. err = -EFAULT;
  819. if (cmd == SIOCGETTUNNEL) {
  820. memcpy(&p, &t->parms, sizeof(p));
  821. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p,
  822. sizeof(p)))
  823. goto done;
  824. #ifdef CONFIG_IPV6_SIT_6RD
  825. } else {
  826. ip6rd.prefix = t->ip6rd.prefix;
  827. ip6rd.relay_prefix = t->ip6rd.relay_prefix;
  828. ip6rd.prefixlen = t->ip6rd.prefixlen;
  829. ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen;
  830. if (copy_to_user(ifr->ifr_ifru.ifru_data, &ip6rd,
  831. sizeof(ip6rd)))
  832. goto done;
  833. #endif
  834. }
  835. err = 0;
  836. break;
  837. case SIOCADDTUNNEL:
  838. case SIOCCHGTUNNEL:
  839. err = -EPERM;
  840. if (!capable(CAP_NET_ADMIN))
  841. goto done;
  842. err = -EFAULT;
  843. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  844. goto done;
  845. err = -EINVAL;
  846. if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
  847. p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
  848. goto done;
  849. if (p.iph.ttl)
  850. p.iph.frag_off |= htons(IP_DF);
  851. t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
  852. if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  853. if (t != NULL) {
  854. if (t->dev != dev) {
  855. err = -EEXIST;
  856. break;
  857. }
  858. } else {
  859. if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
  860. (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
  861. err = -EINVAL;
  862. break;
  863. }
  864. t = netdev_priv(dev);
  865. }
  866. ipip6_tunnel_update(t, &p);
  867. }
  868. if (t) {
  869. err = 0;
  870. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
  871. err = -EFAULT;
  872. } else
  873. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  874. break;
  875. case SIOCDELTUNNEL:
  876. err = -EPERM;
  877. if (!capable(CAP_NET_ADMIN))
  878. goto done;
  879. if (dev == sitn->fb_tunnel_dev) {
  880. err = -EFAULT;
  881. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  882. goto done;
  883. err = -ENOENT;
  884. if ((t = ipip6_tunnel_locate(net, &p, 0)) == NULL)
  885. goto done;
  886. err = -EPERM;
  887. if (t == netdev_priv(sitn->fb_tunnel_dev))
  888. goto done;
  889. dev = t->dev;
  890. }
  891. unregister_netdevice(dev);
  892. err = 0;
  893. break;
  894. case SIOCGETPRL:
  895. err = -EINVAL;
  896. if (dev == sitn->fb_tunnel_dev)
  897. goto done;
  898. err = -ENOENT;
  899. if (!(t = netdev_priv(dev)))
  900. goto done;
  901. err = ipip6_tunnel_get_prl(t, ifr->ifr_ifru.ifru_data);
  902. break;
  903. case SIOCADDPRL:
  904. case SIOCDELPRL:
  905. case SIOCCHGPRL:
  906. err = -EPERM;
  907. if (!capable(CAP_NET_ADMIN))
  908. goto done;
  909. err = -EINVAL;
  910. if (dev == sitn->fb_tunnel_dev)
  911. goto done;
  912. err = -EFAULT;
  913. if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
  914. goto done;
  915. err = -ENOENT;
  916. if (!(t = netdev_priv(dev)))
  917. goto done;
  918. switch (cmd) {
  919. case SIOCDELPRL:
  920. err = ipip6_tunnel_del_prl(t, &prl);
  921. break;
  922. case SIOCADDPRL:
  923. case SIOCCHGPRL:
  924. err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
  925. break;
  926. }
  927. netdev_state_change(dev);
  928. break;
  929. #ifdef CONFIG_IPV6_SIT_6RD
  930. case SIOCADD6RD:
  931. case SIOCCHG6RD:
  932. case SIOCDEL6RD:
  933. err = -EPERM;
  934. if (!capable(CAP_NET_ADMIN))
  935. goto done;
  936. err = -EFAULT;
  937. if (copy_from_user(&ip6rd, ifr->ifr_ifru.ifru_data,
  938. sizeof(ip6rd)))
  939. goto done;
  940. t = netdev_priv(dev);
  941. if (cmd != SIOCDEL6RD) {
  942. struct in6_addr prefix;
  943. __be32 relay_prefix;
  944. err = -EINVAL;
  945. if (ip6rd.relay_prefixlen > 32 ||
  946. ip6rd.prefixlen + (32 - ip6rd.relay_prefixlen) > 64)
  947. goto done;
  948. ipv6_addr_prefix(&prefix, &ip6rd.prefix,
  949. ip6rd.prefixlen);
  950. if (!ipv6_addr_equal(&prefix, &ip6rd.prefix))
  951. goto done;
  952. if (ip6rd.relay_prefixlen)
  953. relay_prefix = ip6rd.relay_prefix &
  954. htonl(0xffffffffUL <<
  955. (32 - ip6rd.relay_prefixlen));
  956. else
  957. relay_prefix = 0;
  958. if (relay_prefix != ip6rd.relay_prefix)
  959. goto done;
  960. t->ip6rd.prefix = prefix;
  961. t->ip6rd.relay_prefix = relay_prefix;
  962. t->ip6rd.prefixlen = ip6rd.prefixlen;
  963. t->ip6rd.relay_prefixlen = ip6rd.relay_prefixlen;
  964. } else
  965. ipip6_tunnel_clone_6rd(dev, sitn);
  966. err = 0;
  967. break;
  968. #endif
  969. default:
  970. err = -EINVAL;
  971. }
  972. done:
  973. return err;
  974. }
  975. static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  976. {
  977. if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
  978. return -EINVAL;
  979. dev->mtu = new_mtu;
  980. return 0;
  981. }
  982. static const struct net_device_ops ipip6_netdev_ops = {
  983. .ndo_uninit = ipip6_tunnel_uninit,
  984. .ndo_start_xmit = ipip6_tunnel_xmit,
  985. .ndo_do_ioctl = ipip6_tunnel_ioctl,
  986. .ndo_change_mtu = ipip6_tunnel_change_mtu,
  987. .ndo_get_stats64= ipip6_get_stats64,
  988. };
  989. static void ipip6_dev_free(struct net_device *dev)
  990. {
  991. free_percpu(dev->tstats);
  992. free_netdev(dev);
  993. }
  994. static void ipip6_tunnel_setup(struct net_device *dev)
  995. {
  996. dev->netdev_ops = &ipip6_netdev_ops;
  997. dev->destructor = ipip6_dev_free;
  998. dev->type = ARPHRD_SIT;
  999. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
  1000. dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
  1001. dev->flags = IFF_NOARP;
  1002. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1003. dev->iflink = 0;
  1004. dev->addr_len = 4;
  1005. dev->features |= NETIF_F_NETNS_LOCAL;
  1006. dev->features |= NETIF_F_LLTX;
  1007. }
  1008. static int ipip6_tunnel_init(struct net_device *dev)
  1009. {
  1010. struct ip_tunnel *tunnel = netdev_priv(dev);
  1011. tunnel->dev = dev;
  1012. memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
  1013. memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
  1014. ipip6_tunnel_bind_dev(dev);
  1015. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1016. if (!dev->tstats)
  1017. return -ENOMEM;
  1018. return 0;
  1019. }
  1020. static int __net_init ipip6_fb_tunnel_init(struct net_device *dev)
  1021. {
  1022. struct ip_tunnel *tunnel = netdev_priv(dev);
  1023. struct iphdr *iph = &tunnel->parms.iph;
  1024. struct net *net = dev_net(dev);
  1025. struct sit_net *sitn = net_generic(net, sit_net_id);
  1026. tunnel->dev = dev;
  1027. strcpy(tunnel->parms.name, dev->name);
  1028. iph->version = 4;
  1029. iph->protocol = IPPROTO_IPV6;
  1030. iph->ihl = 5;
  1031. iph->ttl = 64;
  1032. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1033. if (!dev->tstats)
  1034. return -ENOMEM;
  1035. dev_hold(dev);
  1036. rcu_assign_pointer(sitn->tunnels_wc[0], tunnel);
  1037. return 0;
  1038. }
  1039. static void ipip6_netlink_parms(struct nlattr *data[],
  1040. struct ip_tunnel_parm *parms)
  1041. {
  1042. memset(parms, 0, sizeof(*parms));
  1043. parms->iph.version = 4;
  1044. parms->iph.protocol = IPPROTO_IPV6;
  1045. parms->iph.ihl = 5;
  1046. parms->iph.ttl = 64;
  1047. if (!data)
  1048. return;
  1049. if (data[IFLA_IPTUN_LINK])
  1050. parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
  1051. if (data[IFLA_IPTUN_LOCAL])
  1052. parms->iph.saddr = nla_get_be32(data[IFLA_IPTUN_LOCAL]);
  1053. if (data[IFLA_IPTUN_REMOTE])
  1054. parms->iph.daddr = nla_get_be32(data[IFLA_IPTUN_REMOTE]);
  1055. if (data[IFLA_IPTUN_TTL]) {
  1056. parms->iph.ttl = nla_get_u8(data[IFLA_IPTUN_TTL]);
  1057. if (parms->iph.ttl)
  1058. parms->iph.frag_off = htons(IP_DF);
  1059. }
  1060. if (data[IFLA_IPTUN_TOS])
  1061. parms->iph.tos = nla_get_u8(data[IFLA_IPTUN_TOS]);
  1062. if (!data[IFLA_IPTUN_PMTUDISC] || nla_get_u8(data[IFLA_IPTUN_PMTUDISC]))
  1063. parms->iph.frag_off = htons(IP_DF);
  1064. if (data[IFLA_IPTUN_FLAGS])
  1065. parms->i_flags = nla_get_be16(data[IFLA_IPTUN_FLAGS]);
  1066. }
  1067. static int ipip6_newlink(struct net *src_net, struct net_device *dev,
  1068. struct nlattr *tb[], struct nlattr *data[])
  1069. {
  1070. struct net *net = dev_net(dev);
  1071. struct ip_tunnel *nt;
  1072. nt = netdev_priv(dev);
  1073. ipip6_netlink_parms(data, &nt->parms);
  1074. if (ipip6_tunnel_locate(net, &nt->parms, 0))
  1075. return -EEXIST;
  1076. return ipip6_tunnel_create(dev);
  1077. }
  1078. static int ipip6_changelink(struct net_device *dev, struct nlattr *tb[],
  1079. struct nlattr *data[])
  1080. {
  1081. struct ip_tunnel *t;
  1082. struct ip_tunnel_parm p;
  1083. struct net *net = dev_net(dev);
  1084. struct sit_net *sitn = net_generic(net, sit_net_id);
  1085. if (dev == sitn->fb_tunnel_dev)
  1086. return -EINVAL;
  1087. ipip6_netlink_parms(data, &p);
  1088. if (((dev->flags & IFF_POINTOPOINT) && !p.iph.daddr) ||
  1089. (!(dev->flags & IFF_POINTOPOINT) && p.iph.daddr))
  1090. return -EINVAL;
  1091. t = ipip6_tunnel_locate(net, &p, 0);
  1092. if (t) {
  1093. if (t->dev != dev)
  1094. return -EEXIST;
  1095. } else
  1096. t = netdev_priv(dev);
  1097. ipip6_tunnel_update(t, &p);
  1098. return 0;
  1099. }
  1100. static size_t ipip6_get_size(const struct net_device *dev)
  1101. {
  1102. return
  1103. /* IFLA_IPTUN_LINK */
  1104. nla_total_size(4) +
  1105. /* IFLA_IPTUN_LOCAL */
  1106. nla_total_size(4) +
  1107. /* IFLA_IPTUN_REMOTE */
  1108. nla_total_size(4) +
  1109. /* IFLA_IPTUN_TTL */
  1110. nla_total_size(1) +
  1111. /* IFLA_IPTUN_TOS */
  1112. nla_total_size(1) +
  1113. /* IFLA_IPTUN_PMTUDISC */
  1114. nla_total_size(1) +
  1115. /* IFLA_IPTUN_FLAGS */
  1116. nla_total_size(2) +
  1117. 0;
  1118. }
  1119. static int ipip6_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1120. {
  1121. struct ip_tunnel *tunnel = netdev_priv(dev);
  1122. struct ip_tunnel_parm *parm = &tunnel->parms;
  1123. if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
  1124. nla_put_be32(skb, IFLA_IPTUN_LOCAL, parm->iph.saddr) ||
  1125. nla_put_be32(skb, IFLA_IPTUN_REMOTE, parm->iph.daddr) ||
  1126. nla_put_u8(skb, IFLA_IPTUN_TTL, parm->iph.ttl) ||
  1127. nla_put_u8(skb, IFLA_IPTUN_TOS, parm->iph.tos) ||
  1128. nla_put_u8(skb, IFLA_IPTUN_PMTUDISC,
  1129. !!(parm->iph.frag_off & htons(IP_DF))) ||
  1130. nla_put_be16(skb, IFLA_IPTUN_FLAGS, parm->i_flags))
  1131. goto nla_put_failure;
  1132. return 0;
  1133. nla_put_failure:
  1134. return -EMSGSIZE;
  1135. }
  1136. static const struct nla_policy ipip6_policy[IFLA_IPTUN_MAX + 1] = {
  1137. [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
  1138. [IFLA_IPTUN_LOCAL] = { .type = NLA_U32 },
  1139. [IFLA_IPTUN_REMOTE] = { .type = NLA_U32 },
  1140. [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
  1141. [IFLA_IPTUN_TOS] = { .type = NLA_U8 },
  1142. [IFLA_IPTUN_PMTUDISC] = { .type = NLA_U8 },
  1143. [IFLA_IPTUN_FLAGS] = { .type = NLA_U16 },
  1144. };
  1145. static struct rtnl_link_ops sit_link_ops __read_mostly = {
  1146. .kind = "sit",
  1147. .maxtype = IFLA_IPTUN_MAX,
  1148. .policy = ipip6_policy,
  1149. .priv_size = sizeof(struct ip_tunnel),
  1150. .setup = ipip6_tunnel_setup,
  1151. .newlink = ipip6_newlink,
  1152. .changelink = ipip6_changelink,
  1153. .get_size = ipip6_get_size,
  1154. .fill_info = ipip6_fill_info,
  1155. };
  1156. static struct xfrm_tunnel sit_handler __read_mostly = {
  1157. .handler = ipip6_rcv,
  1158. .err_handler = ipip6_err,
  1159. .priority = 1,
  1160. };
  1161. static void __net_exit sit_destroy_tunnels(struct sit_net *sitn, struct list_head *head)
  1162. {
  1163. int prio;
  1164. for (prio = 1; prio < 4; prio++) {
  1165. int h;
  1166. for (h = 0; h < HASH_SIZE; h++) {
  1167. struct ip_tunnel *t;
  1168. t = rtnl_dereference(sitn->tunnels[prio][h]);
  1169. while (t != NULL) {
  1170. unregister_netdevice_queue(t->dev, head);
  1171. t = rtnl_dereference(t->next);
  1172. }
  1173. }
  1174. }
  1175. }
  1176. static int __net_init sit_init_net(struct net *net)
  1177. {
  1178. struct sit_net *sitn = net_generic(net, sit_net_id);
  1179. struct ip_tunnel *t;
  1180. int err;
  1181. sitn->tunnels[0] = sitn->tunnels_wc;
  1182. sitn->tunnels[1] = sitn->tunnels_l;
  1183. sitn->tunnels[2] = sitn->tunnels_r;
  1184. sitn->tunnels[3] = sitn->tunnels_r_l;
  1185. sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
  1186. ipip6_tunnel_setup);
  1187. if (!sitn->fb_tunnel_dev) {
  1188. err = -ENOMEM;
  1189. goto err_alloc_dev;
  1190. }
  1191. dev_net_set(sitn->fb_tunnel_dev, net);
  1192. err = ipip6_fb_tunnel_init(sitn->fb_tunnel_dev);
  1193. if (err)
  1194. goto err_dev_free;
  1195. ipip6_tunnel_clone_6rd(sitn->fb_tunnel_dev, sitn);
  1196. if ((err = register_netdev(sitn->fb_tunnel_dev)))
  1197. goto err_reg_dev;
  1198. t = netdev_priv(sitn->fb_tunnel_dev);
  1199. strcpy(t->parms.name, sitn->fb_tunnel_dev->name);
  1200. return 0;
  1201. err_reg_dev:
  1202. dev_put(sitn->fb_tunnel_dev);
  1203. err_dev_free:
  1204. ipip6_dev_free(sitn->fb_tunnel_dev);
  1205. err_alloc_dev:
  1206. return err;
  1207. }
  1208. static void __net_exit sit_exit_net(struct net *net)
  1209. {
  1210. struct sit_net *sitn = net_generic(net, sit_net_id);
  1211. LIST_HEAD(list);
  1212. rtnl_lock();
  1213. sit_destroy_tunnels(sitn, &list);
  1214. unregister_netdevice_queue(sitn->fb_tunnel_dev, &list);
  1215. unregister_netdevice_many(&list);
  1216. rtnl_unlock();
  1217. }
  1218. static struct pernet_operations sit_net_ops = {
  1219. .init = sit_init_net,
  1220. .exit = sit_exit_net,
  1221. .id = &sit_net_id,
  1222. .size = sizeof(struct sit_net),
  1223. };
  1224. static void __exit sit_cleanup(void)
  1225. {
  1226. rtnl_link_unregister(&sit_link_ops);
  1227. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  1228. unregister_pernet_device(&sit_net_ops);
  1229. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  1230. }
  1231. static int __init sit_init(void)
  1232. {
  1233. int err;
  1234. pr_info("IPv6 over IPv4 tunneling driver\n");
  1235. err = register_pernet_device(&sit_net_ops);
  1236. if (err < 0)
  1237. return err;
  1238. err = xfrm4_tunnel_register(&sit_handler, AF_INET6);
  1239. if (err < 0) {
  1240. pr_info("%s: can't add protocol\n", __func__);
  1241. goto xfrm_tunnel_failed;
  1242. }
  1243. err = rtnl_link_register(&sit_link_ops);
  1244. if (err < 0)
  1245. goto rtnl_link_failed;
  1246. out:
  1247. return err;
  1248. rtnl_link_failed:
  1249. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  1250. xfrm_tunnel_failed:
  1251. unregister_pernet_device(&sit_net_ops);
  1252. goto out;
  1253. }
  1254. module_init(sit_init);
  1255. module_exit(sit_cleanup);
  1256. MODULE_LICENSE("GPL");
  1257. MODULE_ALIAS_NETDEV("sit0");