sit.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  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 net *net = dev_net(dev);
  401. struct sit_net *sitn = net_generic(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, netdev_priv(dev));
  406. ipip6_tunnel_del_prl(netdev_priv(dev), 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 {
  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. netif_rx(skb);
  534. return 0;
  535. }
  536. /* no tunnel matched, let upstream know, ipsec may handle it */
  537. return 1;
  538. out:
  539. kfree_skb(skb);
  540. return 0;
  541. }
  542. static const struct tnl_ptk_info tpi = {
  543. /* no tunnel info required for ipip. */
  544. .proto = htons(ETH_P_IP),
  545. };
  546. static int ipip_rcv(struct sk_buff *skb)
  547. {
  548. const struct iphdr *iph = ip_hdr(skb);
  549. struct ip_tunnel *tunnel;
  550. tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
  551. iph->saddr, iph->daddr);
  552. if (tunnel != NULL) {
  553. if (tunnel->parms.iph.protocol != IPPROTO_IPIP &&
  554. tunnel->parms.iph.protocol != 0)
  555. goto drop;
  556. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  557. goto drop;
  558. return ip_tunnel_rcv(tunnel, skb, &tpi, log_ecn_error);
  559. }
  560. return 1;
  561. drop:
  562. kfree_skb(skb);
  563. return 0;
  564. }
  565. /*
  566. * If the IPv6 address comes from 6rd / 6to4 (RFC 3056) addr space this function
  567. * stores the embedded IPv4 address in v4dst and returns true.
  568. */
  569. static bool check_6rd(struct ip_tunnel *tunnel, const struct in6_addr *v6dst,
  570. __be32 *v4dst)
  571. {
  572. #ifdef CONFIG_IPV6_SIT_6RD
  573. if (ipv6_prefix_equal(v6dst, &tunnel->ip6rd.prefix,
  574. tunnel->ip6rd.prefixlen)) {
  575. unsigned int pbw0, pbi0;
  576. int pbi1;
  577. u32 d;
  578. pbw0 = tunnel->ip6rd.prefixlen >> 5;
  579. pbi0 = tunnel->ip6rd.prefixlen & 0x1f;
  580. d = (ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >>
  581. tunnel->ip6rd.relay_prefixlen;
  582. pbi1 = pbi0 - tunnel->ip6rd.relay_prefixlen;
  583. if (pbi1 > 0)
  584. d |= ntohl(v6dst->s6_addr32[pbw0 + 1]) >>
  585. (32 - pbi1);
  586. *v4dst = tunnel->ip6rd.relay_prefix | htonl(d);
  587. return true;
  588. }
  589. #else
  590. if (v6dst->s6_addr16[0] == htons(0x2002)) {
  591. /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
  592. memcpy(v4dst, &v6dst->s6_addr16[1], 4);
  593. return true;
  594. }
  595. #endif
  596. return false;
  597. }
  598. static inline __be32 try_6rd(struct ip_tunnel *tunnel,
  599. const struct in6_addr *v6dst)
  600. {
  601. __be32 dst = 0;
  602. check_6rd(tunnel, v6dst, &dst);
  603. return dst;
  604. }
  605. /*
  606. * This function assumes it is being called from dev_queue_xmit()
  607. * and that skb is filled properly by that function.
  608. */
  609. static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
  610. struct net_device *dev)
  611. {
  612. struct ip_tunnel *tunnel = netdev_priv(dev);
  613. const struct iphdr *tiph = &tunnel->parms.iph;
  614. const struct ipv6hdr *iph6 = ipv6_hdr(skb);
  615. u8 tos = tunnel->parms.iph.tos;
  616. __be16 df = tiph->frag_off;
  617. struct rtable *rt; /* Route to the other host */
  618. struct net_device *tdev; /* Device to other host */
  619. struct iphdr *iph; /* Our new IP header */
  620. unsigned int max_headroom; /* The extra header space needed */
  621. __be32 dst = tiph->daddr;
  622. struct flowi4 fl4;
  623. int mtu;
  624. const struct in6_addr *addr6;
  625. int addr_type;
  626. if (skb->protocol != htons(ETH_P_IPV6))
  627. goto tx_error;
  628. if (tos == 1)
  629. tos = ipv6_get_dsfield(iph6);
  630. /* ISATAP (RFC4214) - must come before 6to4 */
  631. if (dev->priv_flags & IFF_ISATAP) {
  632. struct neighbour *neigh = NULL;
  633. bool do_tx_error = false;
  634. if (skb_dst(skb))
  635. neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
  636. if (neigh == NULL) {
  637. net_dbg_ratelimited("sit: nexthop == NULL\n");
  638. goto tx_error;
  639. }
  640. addr6 = (const struct in6_addr *)&neigh->primary_key;
  641. addr_type = ipv6_addr_type(addr6);
  642. if ((addr_type & IPV6_ADDR_UNICAST) &&
  643. ipv6_addr_is_isatap(addr6))
  644. dst = addr6->s6_addr32[3];
  645. else
  646. do_tx_error = true;
  647. neigh_release(neigh);
  648. if (do_tx_error)
  649. goto tx_error;
  650. }
  651. if (!dst)
  652. dst = try_6rd(tunnel, &iph6->daddr);
  653. if (!dst) {
  654. struct neighbour *neigh = NULL;
  655. bool do_tx_error = false;
  656. if (skb_dst(skb))
  657. neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
  658. if (neigh == NULL) {
  659. net_dbg_ratelimited("sit: nexthop == NULL\n");
  660. goto tx_error;
  661. }
  662. addr6 = (const struct in6_addr *)&neigh->primary_key;
  663. addr_type = ipv6_addr_type(addr6);
  664. if (addr_type == IPV6_ADDR_ANY) {
  665. addr6 = &ipv6_hdr(skb)->daddr;
  666. addr_type = ipv6_addr_type(addr6);
  667. }
  668. if ((addr_type & IPV6_ADDR_COMPATv4) != 0)
  669. dst = addr6->s6_addr32[3];
  670. else
  671. do_tx_error = true;
  672. neigh_release(neigh);
  673. if (do_tx_error)
  674. goto tx_error;
  675. }
  676. rt = ip_route_output_ports(dev_net(dev), &fl4, NULL,
  677. dst, tiph->saddr,
  678. 0, 0,
  679. IPPROTO_IPV6, RT_TOS(tos),
  680. tunnel->parms.link);
  681. if (IS_ERR(rt)) {
  682. dev->stats.tx_carrier_errors++;
  683. goto tx_error_icmp;
  684. }
  685. if (rt->rt_type != RTN_UNICAST) {
  686. ip_rt_put(rt);
  687. dev->stats.tx_carrier_errors++;
  688. goto tx_error_icmp;
  689. }
  690. tdev = rt->dst.dev;
  691. if (tdev == dev) {
  692. ip_rt_put(rt);
  693. dev->stats.collisions++;
  694. goto tx_error;
  695. }
  696. if (df) {
  697. mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
  698. if (mtu < 68) {
  699. dev->stats.collisions++;
  700. ip_rt_put(rt);
  701. goto tx_error;
  702. }
  703. if (mtu < IPV6_MIN_MTU) {
  704. mtu = IPV6_MIN_MTU;
  705. df = 0;
  706. }
  707. if (tunnel->parms.iph.daddr && skb_dst(skb))
  708. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
  709. if (skb->len > mtu) {
  710. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  711. ip_rt_put(rt);
  712. goto tx_error;
  713. }
  714. }
  715. if (tunnel->err_count > 0) {
  716. if (time_before(jiffies,
  717. tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
  718. tunnel->err_count--;
  719. dst_link_failure(skb);
  720. } else
  721. tunnel->err_count = 0;
  722. }
  723. /*
  724. * Okay, now see if we can stuff it in the buffer as-is.
  725. */
  726. max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
  727. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  728. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  729. struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
  730. if (!new_skb) {
  731. ip_rt_put(rt);
  732. dev->stats.tx_dropped++;
  733. dev_kfree_skb(skb);
  734. return NETDEV_TX_OK;
  735. }
  736. if (skb->sk)
  737. skb_set_owner_w(new_skb, skb->sk);
  738. dev_kfree_skb(skb);
  739. skb = new_skb;
  740. iph6 = ipv6_hdr(skb);
  741. }
  742. skb->transport_header = skb->network_header;
  743. skb_push(skb, sizeof(struct iphdr));
  744. skb_reset_network_header(skb);
  745. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  746. IPCB(skb)->flags = 0;
  747. skb_dst_drop(skb);
  748. skb_dst_set(skb, &rt->dst);
  749. /*
  750. * Push down and install the IPIP header.
  751. */
  752. iph = ip_hdr(skb);
  753. iph->version = 4;
  754. iph->ihl = sizeof(struct iphdr)>>2;
  755. iph->frag_off = df;
  756. iph->protocol = IPPROTO_IPV6;
  757. iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
  758. iph->daddr = fl4.daddr;
  759. iph->saddr = fl4.saddr;
  760. if ((iph->ttl = tiph->ttl) == 0)
  761. iph->ttl = iph6->hop_limit;
  762. skb->ip_summed = CHECKSUM_NONE;
  763. ip_select_ident(iph, skb_dst(skb), NULL);
  764. iptunnel_xmit(skb, dev);
  765. return NETDEV_TX_OK;
  766. tx_error_icmp:
  767. dst_link_failure(skb);
  768. tx_error:
  769. dev->stats.tx_errors++;
  770. dev_kfree_skb(skb);
  771. return NETDEV_TX_OK;
  772. }
  773. static netdev_tx_t ipip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
  774. {
  775. struct ip_tunnel *tunnel = netdev_priv(dev);
  776. const struct iphdr *tiph = &tunnel->parms.iph;
  777. if (likely(!skb->encapsulation)) {
  778. skb_reset_inner_headers(skb);
  779. skb->encapsulation = 1;
  780. }
  781. ip_tunnel_xmit(skb, dev, tiph, IPPROTO_IPIP);
  782. return NETDEV_TX_OK;
  783. }
  784. static netdev_tx_t sit_tunnel_xmit(struct sk_buff *skb,
  785. struct net_device *dev)
  786. {
  787. switch (skb->protocol) {
  788. case htons(ETH_P_IP):
  789. ipip_tunnel_xmit(skb, dev);
  790. break;
  791. case htons(ETH_P_IPV6):
  792. ipip6_tunnel_xmit(skb, dev);
  793. break;
  794. default:
  795. goto tx_err;
  796. }
  797. return NETDEV_TX_OK;
  798. tx_err:
  799. dev->stats.tx_errors++;
  800. dev_kfree_skb(skb);
  801. return NETDEV_TX_OK;
  802. }
  803. static void ipip6_tunnel_bind_dev(struct net_device *dev)
  804. {
  805. struct net_device *tdev = NULL;
  806. struct ip_tunnel *tunnel;
  807. const struct iphdr *iph;
  808. struct flowi4 fl4;
  809. tunnel = netdev_priv(dev);
  810. iph = &tunnel->parms.iph;
  811. if (iph->daddr) {
  812. struct rtable *rt = ip_route_output_ports(dev_net(dev), &fl4, NULL,
  813. iph->daddr, iph->saddr,
  814. 0, 0,
  815. IPPROTO_IPV6,
  816. RT_TOS(iph->tos),
  817. tunnel->parms.link);
  818. if (!IS_ERR(rt)) {
  819. tdev = rt->dst.dev;
  820. ip_rt_put(rt);
  821. }
  822. dev->flags |= IFF_POINTOPOINT;
  823. }
  824. if (!tdev && tunnel->parms.link)
  825. tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
  826. if (tdev) {
  827. dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
  828. dev->mtu = tdev->mtu - sizeof(struct iphdr);
  829. if (dev->mtu < IPV6_MIN_MTU)
  830. dev->mtu = IPV6_MIN_MTU;
  831. }
  832. dev->iflink = tunnel->parms.link;
  833. }
  834. static void ipip6_tunnel_update(struct ip_tunnel *t, struct ip_tunnel_parm *p)
  835. {
  836. struct net *net = dev_net(t->dev);
  837. struct sit_net *sitn = net_generic(net, sit_net_id);
  838. ipip6_tunnel_unlink(sitn, t);
  839. synchronize_net();
  840. t->parms.iph.saddr = p->iph.saddr;
  841. t->parms.iph.daddr = p->iph.daddr;
  842. memcpy(t->dev->dev_addr, &p->iph.saddr, 4);
  843. memcpy(t->dev->broadcast, &p->iph.daddr, 4);
  844. ipip6_tunnel_link(sitn, t);
  845. t->parms.iph.ttl = p->iph.ttl;
  846. t->parms.iph.tos = p->iph.tos;
  847. if (t->parms.link != p->link) {
  848. t->parms.link = p->link;
  849. ipip6_tunnel_bind_dev(t->dev);
  850. }
  851. netdev_state_change(t->dev);
  852. }
  853. #ifdef CONFIG_IPV6_SIT_6RD
  854. static int ipip6_tunnel_update_6rd(struct ip_tunnel *t,
  855. struct ip_tunnel_6rd *ip6rd)
  856. {
  857. struct in6_addr prefix;
  858. __be32 relay_prefix;
  859. if (ip6rd->relay_prefixlen > 32 ||
  860. ip6rd->prefixlen + (32 - ip6rd->relay_prefixlen) > 64)
  861. return -EINVAL;
  862. ipv6_addr_prefix(&prefix, &ip6rd->prefix, ip6rd->prefixlen);
  863. if (!ipv6_addr_equal(&prefix, &ip6rd->prefix))
  864. return -EINVAL;
  865. if (ip6rd->relay_prefixlen)
  866. relay_prefix = ip6rd->relay_prefix &
  867. htonl(0xffffffffUL <<
  868. (32 - ip6rd->relay_prefixlen));
  869. else
  870. relay_prefix = 0;
  871. if (relay_prefix != ip6rd->relay_prefix)
  872. return -EINVAL;
  873. t->ip6rd.prefix = prefix;
  874. t->ip6rd.relay_prefix = relay_prefix;
  875. t->ip6rd.prefixlen = ip6rd->prefixlen;
  876. t->ip6rd.relay_prefixlen = ip6rd->relay_prefixlen;
  877. netdev_state_change(t->dev);
  878. return 0;
  879. }
  880. #endif
  881. static int
  882. ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
  883. {
  884. int err = 0;
  885. struct ip_tunnel_parm p;
  886. struct ip_tunnel_prl prl;
  887. struct ip_tunnel *t;
  888. struct net *net = dev_net(dev);
  889. struct sit_net *sitn = net_generic(net, sit_net_id);
  890. #ifdef CONFIG_IPV6_SIT_6RD
  891. struct ip_tunnel_6rd ip6rd;
  892. #endif
  893. switch (cmd) {
  894. case SIOCGETTUNNEL:
  895. #ifdef CONFIG_IPV6_SIT_6RD
  896. case SIOCGET6RD:
  897. #endif
  898. t = NULL;
  899. if (dev == sitn->fb_tunnel_dev) {
  900. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  901. err = -EFAULT;
  902. break;
  903. }
  904. t = ipip6_tunnel_locate(net, &p, 0);
  905. }
  906. if (t == NULL)
  907. t = netdev_priv(dev);
  908. err = -EFAULT;
  909. if (cmd == SIOCGETTUNNEL) {
  910. memcpy(&p, &t->parms, sizeof(p));
  911. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p,
  912. sizeof(p)))
  913. goto done;
  914. #ifdef CONFIG_IPV6_SIT_6RD
  915. } else {
  916. ip6rd.prefix = t->ip6rd.prefix;
  917. ip6rd.relay_prefix = t->ip6rd.relay_prefix;
  918. ip6rd.prefixlen = t->ip6rd.prefixlen;
  919. ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen;
  920. if (copy_to_user(ifr->ifr_ifru.ifru_data, &ip6rd,
  921. sizeof(ip6rd)))
  922. goto done;
  923. #endif
  924. }
  925. err = 0;
  926. break;
  927. case SIOCADDTUNNEL:
  928. case SIOCCHGTUNNEL:
  929. err = -EPERM;
  930. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  931. goto done;
  932. err = -EFAULT;
  933. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  934. goto done;
  935. err = -EINVAL;
  936. if (p.iph.protocol != IPPROTO_IPV6 &&
  937. p.iph.protocol != IPPROTO_IPIP &&
  938. p.iph.protocol != 0)
  939. goto done;
  940. if (p.iph.version != 4 ||
  941. p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
  942. goto done;
  943. if (p.iph.ttl)
  944. p.iph.frag_off |= htons(IP_DF);
  945. t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
  946. if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  947. if (t != NULL) {
  948. if (t->dev != dev) {
  949. err = -EEXIST;
  950. break;
  951. }
  952. } else {
  953. if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
  954. (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
  955. err = -EINVAL;
  956. break;
  957. }
  958. t = netdev_priv(dev);
  959. }
  960. ipip6_tunnel_update(t, &p);
  961. }
  962. if (t) {
  963. err = 0;
  964. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
  965. err = -EFAULT;
  966. } else
  967. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  968. break;
  969. case SIOCDELTUNNEL:
  970. err = -EPERM;
  971. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  972. goto done;
  973. if (dev == sitn->fb_tunnel_dev) {
  974. err = -EFAULT;
  975. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  976. goto done;
  977. err = -ENOENT;
  978. if ((t = ipip6_tunnel_locate(net, &p, 0)) == NULL)
  979. goto done;
  980. err = -EPERM;
  981. if (t == netdev_priv(sitn->fb_tunnel_dev))
  982. goto done;
  983. dev = t->dev;
  984. }
  985. unregister_netdevice(dev);
  986. err = 0;
  987. break;
  988. case SIOCGETPRL:
  989. err = -EINVAL;
  990. if (dev == sitn->fb_tunnel_dev)
  991. goto done;
  992. err = -ENOENT;
  993. if (!(t = netdev_priv(dev)))
  994. goto done;
  995. err = ipip6_tunnel_get_prl(t, ifr->ifr_ifru.ifru_data);
  996. break;
  997. case SIOCADDPRL:
  998. case SIOCDELPRL:
  999. case SIOCCHGPRL:
  1000. err = -EPERM;
  1001. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1002. goto done;
  1003. err = -EINVAL;
  1004. if (dev == sitn->fb_tunnel_dev)
  1005. goto done;
  1006. err = -EFAULT;
  1007. if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
  1008. goto done;
  1009. err = -ENOENT;
  1010. if (!(t = netdev_priv(dev)))
  1011. goto done;
  1012. switch (cmd) {
  1013. case SIOCDELPRL:
  1014. err = ipip6_tunnel_del_prl(t, &prl);
  1015. break;
  1016. case SIOCADDPRL:
  1017. case SIOCCHGPRL:
  1018. err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
  1019. break;
  1020. }
  1021. netdev_state_change(dev);
  1022. break;
  1023. #ifdef CONFIG_IPV6_SIT_6RD
  1024. case SIOCADD6RD:
  1025. case SIOCCHG6RD:
  1026. case SIOCDEL6RD:
  1027. err = -EPERM;
  1028. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1029. goto done;
  1030. err = -EFAULT;
  1031. if (copy_from_user(&ip6rd, ifr->ifr_ifru.ifru_data,
  1032. sizeof(ip6rd)))
  1033. goto done;
  1034. t = netdev_priv(dev);
  1035. if (cmd != SIOCDEL6RD) {
  1036. err = ipip6_tunnel_update_6rd(t, &ip6rd);
  1037. if (err < 0)
  1038. goto done;
  1039. } else
  1040. ipip6_tunnel_clone_6rd(dev, sitn);
  1041. err = 0;
  1042. break;
  1043. #endif
  1044. default:
  1045. err = -EINVAL;
  1046. }
  1047. done:
  1048. return err;
  1049. }
  1050. static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  1051. {
  1052. if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
  1053. return -EINVAL;
  1054. dev->mtu = new_mtu;
  1055. return 0;
  1056. }
  1057. static const struct net_device_ops ipip6_netdev_ops = {
  1058. .ndo_uninit = ipip6_tunnel_uninit,
  1059. .ndo_start_xmit = sit_tunnel_xmit,
  1060. .ndo_do_ioctl = ipip6_tunnel_ioctl,
  1061. .ndo_change_mtu = ipip6_tunnel_change_mtu,
  1062. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1063. };
  1064. static void ipip6_dev_free(struct net_device *dev)
  1065. {
  1066. free_percpu(dev->tstats);
  1067. free_netdev(dev);
  1068. }
  1069. static void ipip6_tunnel_setup(struct net_device *dev)
  1070. {
  1071. dev->netdev_ops = &ipip6_netdev_ops;
  1072. dev->destructor = ipip6_dev_free;
  1073. dev->type = ARPHRD_SIT;
  1074. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
  1075. dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
  1076. dev->flags = IFF_NOARP;
  1077. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1078. dev->iflink = 0;
  1079. dev->addr_len = 4;
  1080. dev->features |= NETIF_F_NETNS_LOCAL;
  1081. dev->features |= NETIF_F_LLTX;
  1082. }
  1083. static int ipip6_tunnel_init(struct net_device *dev)
  1084. {
  1085. struct ip_tunnel *tunnel = netdev_priv(dev);
  1086. tunnel->dev = dev;
  1087. memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
  1088. memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
  1089. ipip6_tunnel_bind_dev(dev);
  1090. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1091. if (!dev->tstats)
  1092. return -ENOMEM;
  1093. return 0;
  1094. }
  1095. static int __net_init ipip6_fb_tunnel_init(struct net_device *dev)
  1096. {
  1097. struct ip_tunnel *tunnel = netdev_priv(dev);
  1098. struct iphdr *iph = &tunnel->parms.iph;
  1099. struct net *net = dev_net(dev);
  1100. struct sit_net *sitn = net_generic(net, sit_net_id);
  1101. tunnel->dev = dev;
  1102. strcpy(tunnel->parms.name, dev->name);
  1103. iph->version = 4;
  1104. iph->protocol = IPPROTO_IPV6;
  1105. iph->ihl = 5;
  1106. iph->ttl = 64;
  1107. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1108. if (!dev->tstats)
  1109. return -ENOMEM;
  1110. dev_hold(dev);
  1111. rcu_assign_pointer(sitn->tunnels_wc[0], tunnel);
  1112. return 0;
  1113. }
  1114. static int ipip6_validate(struct nlattr *tb[], struct nlattr *data[])
  1115. {
  1116. u8 proto;
  1117. if (!data)
  1118. return 0;
  1119. proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1120. if (proto != IPPROTO_IPV6 &&
  1121. proto != IPPROTO_IPIP &&
  1122. proto != 0)
  1123. return -EINVAL;
  1124. return 0;
  1125. }
  1126. static void ipip6_netlink_parms(struct nlattr *data[],
  1127. struct ip_tunnel_parm *parms)
  1128. {
  1129. memset(parms, 0, sizeof(*parms));
  1130. parms->iph.version = 4;
  1131. parms->iph.protocol = IPPROTO_IPV6;
  1132. parms->iph.ihl = 5;
  1133. parms->iph.ttl = 64;
  1134. if (!data)
  1135. return;
  1136. if (data[IFLA_IPTUN_LINK])
  1137. parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
  1138. if (data[IFLA_IPTUN_LOCAL])
  1139. parms->iph.saddr = nla_get_be32(data[IFLA_IPTUN_LOCAL]);
  1140. if (data[IFLA_IPTUN_REMOTE])
  1141. parms->iph.daddr = nla_get_be32(data[IFLA_IPTUN_REMOTE]);
  1142. if (data[IFLA_IPTUN_TTL]) {
  1143. parms->iph.ttl = nla_get_u8(data[IFLA_IPTUN_TTL]);
  1144. if (parms->iph.ttl)
  1145. parms->iph.frag_off = htons(IP_DF);
  1146. }
  1147. if (data[IFLA_IPTUN_TOS])
  1148. parms->iph.tos = nla_get_u8(data[IFLA_IPTUN_TOS]);
  1149. if (!data[IFLA_IPTUN_PMTUDISC] || nla_get_u8(data[IFLA_IPTUN_PMTUDISC]))
  1150. parms->iph.frag_off = htons(IP_DF);
  1151. if (data[IFLA_IPTUN_FLAGS])
  1152. parms->i_flags = nla_get_be16(data[IFLA_IPTUN_FLAGS]);
  1153. if (data[IFLA_IPTUN_PROTO])
  1154. parms->iph.protocol = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1155. }
  1156. #ifdef CONFIG_IPV6_SIT_6RD
  1157. /* This function returns true when 6RD attributes are present in the nl msg */
  1158. static bool ipip6_netlink_6rd_parms(struct nlattr *data[],
  1159. struct ip_tunnel_6rd *ip6rd)
  1160. {
  1161. bool ret = false;
  1162. memset(ip6rd, 0, sizeof(*ip6rd));
  1163. if (!data)
  1164. return ret;
  1165. if (data[IFLA_IPTUN_6RD_PREFIX]) {
  1166. ret = true;
  1167. nla_memcpy(&ip6rd->prefix, data[IFLA_IPTUN_6RD_PREFIX],
  1168. sizeof(struct in6_addr));
  1169. }
  1170. if (data[IFLA_IPTUN_6RD_RELAY_PREFIX]) {
  1171. ret = true;
  1172. ip6rd->relay_prefix =
  1173. nla_get_be32(data[IFLA_IPTUN_6RD_RELAY_PREFIX]);
  1174. }
  1175. if (data[IFLA_IPTUN_6RD_PREFIXLEN]) {
  1176. ret = true;
  1177. ip6rd->prefixlen = nla_get_u16(data[IFLA_IPTUN_6RD_PREFIXLEN]);
  1178. }
  1179. if (data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]) {
  1180. ret = true;
  1181. ip6rd->relay_prefixlen =
  1182. nla_get_u16(data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]);
  1183. }
  1184. return ret;
  1185. }
  1186. #endif
  1187. static int ipip6_newlink(struct net *src_net, struct net_device *dev,
  1188. struct nlattr *tb[], struct nlattr *data[])
  1189. {
  1190. struct net *net = dev_net(dev);
  1191. struct ip_tunnel *nt;
  1192. #ifdef CONFIG_IPV6_SIT_6RD
  1193. struct ip_tunnel_6rd ip6rd;
  1194. #endif
  1195. int err;
  1196. nt = netdev_priv(dev);
  1197. ipip6_netlink_parms(data, &nt->parms);
  1198. if (ipip6_tunnel_locate(net, &nt->parms, 0))
  1199. return -EEXIST;
  1200. err = ipip6_tunnel_create(dev);
  1201. if (err < 0)
  1202. return err;
  1203. #ifdef CONFIG_IPV6_SIT_6RD
  1204. if (ipip6_netlink_6rd_parms(data, &ip6rd))
  1205. err = ipip6_tunnel_update_6rd(nt, &ip6rd);
  1206. #endif
  1207. return err;
  1208. }
  1209. static int ipip6_changelink(struct net_device *dev, struct nlattr *tb[],
  1210. struct nlattr *data[])
  1211. {
  1212. struct ip_tunnel *t;
  1213. struct ip_tunnel_parm p;
  1214. struct net *net = dev_net(dev);
  1215. struct sit_net *sitn = net_generic(net, sit_net_id);
  1216. #ifdef CONFIG_IPV6_SIT_6RD
  1217. struct ip_tunnel_6rd ip6rd;
  1218. #endif
  1219. if (dev == sitn->fb_tunnel_dev)
  1220. return -EINVAL;
  1221. ipip6_netlink_parms(data, &p);
  1222. if (((dev->flags & IFF_POINTOPOINT) && !p.iph.daddr) ||
  1223. (!(dev->flags & IFF_POINTOPOINT) && p.iph.daddr))
  1224. return -EINVAL;
  1225. t = ipip6_tunnel_locate(net, &p, 0);
  1226. if (t) {
  1227. if (t->dev != dev)
  1228. return -EEXIST;
  1229. } else
  1230. t = netdev_priv(dev);
  1231. ipip6_tunnel_update(t, &p);
  1232. #ifdef CONFIG_IPV6_SIT_6RD
  1233. if (ipip6_netlink_6rd_parms(data, &ip6rd))
  1234. return ipip6_tunnel_update_6rd(t, &ip6rd);
  1235. #endif
  1236. return 0;
  1237. }
  1238. static size_t ipip6_get_size(const struct net_device *dev)
  1239. {
  1240. return
  1241. /* IFLA_IPTUN_LINK */
  1242. nla_total_size(4) +
  1243. /* IFLA_IPTUN_LOCAL */
  1244. nla_total_size(4) +
  1245. /* IFLA_IPTUN_REMOTE */
  1246. nla_total_size(4) +
  1247. /* IFLA_IPTUN_TTL */
  1248. nla_total_size(1) +
  1249. /* IFLA_IPTUN_TOS */
  1250. nla_total_size(1) +
  1251. /* IFLA_IPTUN_PMTUDISC */
  1252. nla_total_size(1) +
  1253. /* IFLA_IPTUN_FLAGS */
  1254. nla_total_size(2) +
  1255. /* IFLA_IPTUN_PROTO */
  1256. nla_total_size(1) +
  1257. #ifdef CONFIG_IPV6_SIT_6RD
  1258. /* IFLA_IPTUN_6RD_PREFIX */
  1259. nla_total_size(sizeof(struct in6_addr)) +
  1260. /* IFLA_IPTUN_6RD_RELAY_PREFIX */
  1261. nla_total_size(4) +
  1262. /* IFLA_IPTUN_6RD_PREFIXLEN */
  1263. nla_total_size(2) +
  1264. /* IFLA_IPTUN_6RD_RELAY_PREFIXLEN */
  1265. nla_total_size(2) +
  1266. #endif
  1267. 0;
  1268. }
  1269. static int ipip6_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1270. {
  1271. struct ip_tunnel *tunnel = netdev_priv(dev);
  1272. struct ip_tunnel_parm *parm = &tunnel->parms;
  1273. if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
  1274. nla_put_be32(skb, IFLA_IPTUN_LOCAL, parm->iph.saddr) ||
  1275. nla_put_be32(skb, IFLA_IPTUN_REMOTE, parm->iph.daddr) ||
  1276. nla_put_u8(skb, IFLA_IPTUN_TTL, parm->iph.ttl) ||
  1277. nla_put_u8(skb, IFLA_IPTUN_TOS, parm->iph.tos) ||
  1278. nla_put_u8(skb, IFLA_IPTUN_PMTUDISC,
  1279. !!(parm->iph.frag_off & htons(IP_DF))) ||
  1280. nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->iph.protocol) ||
  1281. nla_put_be16(skb, IFLA_IPTUN_FLAGS, parm->i_flags))
  1282. goto nla_put_failure;
  1283. #ifdef CONFIG_IPV6_SIT_6RD
  1284. if (nla_put(skb, IFLA_IPTUN_6RD_PREFIX, sizeof(struct in6_addr),
  1285. &tunnel->ip6rd.prefix) ||
  1286. nla_put_be32(skb, IFLA_IPTUN_6RD_RELAY_PREFIX,
  1287. tunnel->ip6rd.relay_prefix) ||
  1288. nla_put_u16(skb, IFLA_IPTUN_6RD_PREFIXLEN,
  1289. tunnel->ip6rd.prefixlen) ||
  1290. nla_put_u16(skb, IFLA_IPTUN_6RD_RELAY_PREFIXLEN,
  1291. tunnel->ip6rd.relay_prefixlen))
  1292. goto nla_put_failure;
  1293. #endif
  1294. return 0;
  1295. nla_put_failure:
  1296. return -EMSGSIZE;
  1297. }
  1298. static const struct nla_policy ipip6_policy[IFLA_IPTUN_MAX + 1] = {
  1299. [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
  1300. [IFLA_IPTUN_LOCAL] = { .type = NLA_U32 },
  1301. [IFLA_IPTUN_REMOTE] = { .type = NLA_U32 },
  1302. [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
  1303. [IFLA_IPTUN_TOS] = { .type = NLA_U8 },
  1304. [IFLA_IPTUN_PMTUDISC] = { .type = NLA_U8 },
  1305. [IFLA_IPTUN_FLAGS] = { .type = NLA_U16 },
  1306. [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
  1307. #ifdef CONFIG_IPV6_SIT_6RD
  1308. [IFLA_IPTUN_6RD_PREFIX] = { .len = sizeof(struct in6_addr) },
  1309. [IFLA_IPTUN_6RD_RELAY_PREFIX] = { .type = NLA_U32 },
  1310. [IFLA_IPTUN_6RD_PREFIXLEN] = { .type = NLA_U16 },
  1311. [IFLA_IPTUN_6RD_RELAY_PREFIXLEN] = { .type = NLA_U16 },
  1312. #endif
  1313. };
  1314. static struct rtnl_link_ops sit_link_ops __read_mostly = {
  1315. .kind = "sit",
  1316. .maxtype = IFLA_IPTUN_MAX,
  1317. .policy = ipip6_policy,
  1318. .priv_size = sizeof(struct ip_tunnel),
  1319. .setup = ipip6_tunnel_setup,
  1320. .validate = ipip6_validate,
  1321. .newlink = ipip6_newlink,
  1322. .changelink = ipip6_changelink,
  1323. .get_size = ipip6_get_size,
  1324. .fill_info = ipip6_fill_info,
  1325. };
  1326. static struct xfrm_tunnel sit_handler __read_mostly = {
  1327. .handler = ipip6_rcv,
  1328. .err_handler = ipip6_err,
  1329. .priority = 1,
  1330. };
  1331. static struct xfrm_tunnel ipip_handler __read_mostly = {
  1332. .handler = ipip_rcv,
  1333. .err_handler = ipip6_err,
  1334. .priority = 2,
  1335. };
  1336. static void __net_exit sit_destroy_tunnels(struct sit_net *sitn, struct list_head *head)
  1337. {
  1338. int prio;
  1339. for (prio = 1; prio < 4; prio++) {
  1340. int h;
  1341. for (h = 0; h < HASH_SIZE; h++) {
  1342. struct ip_tunnel *t;
  1343. t = rtnl_dereference(sitn->tunnels[prio][h]);
  1344. while (t != NULL) {
  1345. unregister_netdevice_queue(t->dev, head);
  1346. t = rtnl_dereference(t->next);
  1347. }
  1348. }
  1349. }
  1350. }
  1351. static int __net_init sit_init_net(struct net *net)
  1352. {
  1353. struct sit_net *sitn = net_generic(net, sit_net_id);
  1354. struct ip_tunnel *t;
  1355. int err;
  1356. sitn->tunnels[0] = sitn->tunnels_wc;
  1357. sitn->tunnels[1] = sitn->tunnels_l;
  1358. sitn->tunnels[2] = sitn->tunnels_r;
  1359. sitn->tunnels[3] = sitn->tunnels_r_l;
  1360. sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
  1361. ipip6_tunnel_setup);
  1362. if (!sitn->fb_tunnel_dev) {
  1363. err = -ENOMEM;
  1364. goto err_alloc_dev;
  1365. }
  1366. dev_net_set(sitn->fb_tunnel_dev, net);
  1367. err = ipip6_fb_tunnel_init(sitn->fb_tunnel_dev);
  1368. if (err)
  1369. goto err_dev_free;
  1370. ipip6_tunnel_clone_6rd(sitn->fb_tunnel_dev, sitn);
  1371. if ((err = register_netdev(sitn->fb_tunnel_dev)))
  1372. goto err_reg_dev;
  1373. t = netdev_priv(sitn->fb_tunnel_dev);
  1374. strcpy(t->parms.name, sitn->fb_tunnel_dev->name);
  1375. return 0;
  1376. err_reg_dev:
  1377. dev_put(sitn->fb_tunnel_dev);
  1378. err_dev_free:
  1379. ipip6_dev_free(sitn->fb_tunnel_dev);
  1380. err_alloc_dev:
  1381. return err;
  1382. }
  1383. static void __net_exit sit_exit_net(struct net *net)
  1384. {
  1385. struct sit_net *sitn = net_generic(net, sit_net_id);
  1386. LIST_HEAD(list);
  1387. rtnl_lock();
  1388. sit_destroy_tunnels(sitn, &list);
  1389. unregister_netdevice_queue(sitn->fb_tunnel_dev, &list);
  1390. unregister_netdevice_many(&list);
  1391. rtnl_unlock();
  1392. }
  1393. static struct pernet_operations sit_net_ops = {
  1394. .init = sit_init_net,
  1395. .exit = sit_exit_net,
  1396. .id = &sit_net_id,
  1397. .size = sizeof(struct sit_net),
  1398. };
  1399. static void __exit sit_cleanup(void)
  1400. {
  1401. rtnl_link_unregister(&sit_link_ops);
  1402. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  1403. xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
  1404. unregister_pernet_device(&sit_net_ops);
  1405. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  1406. }
  1407. static int __init sit_init(void)
  1408. {
  1409. int err;
  1410. pr_info("IPv6 over IPv4 tunneling driver\n");
  1411. err = register_pernet_device(&sit_net_ops);
  1412. if (err < 0)
  1413. return err;
  1414. err = xfrm4_tunnel_register(&sit_handler, AF_INET6);
  1415. if (err < 0) {
  1416. pr_info("%s: can't register ip6ip4\n", __func__);
  1417. goto xfrm_tunnel_failed;
  1418. }
  1419. err = xfrm4_tunnel_register(&ipip_handler, AF_INET);
  1420. if (err < 0) {
  1421. pr_info("%s: can't register ip4ip4\n", __func__);
  1422. goto xfrm_tunnel4_failed;
  1423. }
  1424. err = rtnl_link_register(&sit_link_ops);
  1425. if (err < 0)
  1426. goto rtnl_link_failed;
  1427. out:
  1428. return err;
  1429. rtnl_link_failed:
  1430. xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
  1431. xfrm_tunnel4_failed:
  1432. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  1433. xfrm_tunnel_failed:
  1434. unregister_pernet_device(&sit_net_ops);
  1435. goto out;
  1436. }
  1437. module_init(sit_init);
  1438. module_exit(sit_cleanup);
  1439. MODULE_LICENSE("GPL");
  1440. MODULE_ALIAS_NETDEV("sit0");