sit.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834
  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->ifindex == 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->ifindex == 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->ifindex == 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, NET_NAME_UNKNOWN,
  220. ipip6_tunnel_setup);
  221. if (dev == NULL)
  222. return NULL;
  223. dev_net_set(dev, net);
  224. nt = netdev_priv(dev);
  225. nt->parms = *parms;
  226. if (ipip6_tunnel_create(dev) < 0)
  227. goto failed_free;
  228. return nt;
  229. failed_free:
  230. ipip6_dev_free(dev);
  231. failed:
  232. return NULL;
  233. }
  234. #define for_each_prl_rcu(start) \
  235. for (prl = rcu_dereference(start); \
  236. prl; \
  237. prl = rcu_dereference(prl->next))
  238. static struct ip_tunnel_prl_entry *
  239. __ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr)
  240. {
  241. struct ip_tunnel_prl_entry *prl;
  242. for_each_prl_rcu(t->prl)
  243. if (prl->addr == addr)
  244. break;
  245. return prl;
  246. }
  247. static int ipip6_tunnel_get_prl(struct ip_tunnel *t,
  248. struct ip_tunnel_prl __user *a)
  249. {
  250. struct ip_tunnel_prl kprl, *kp;
  251. struct ip_tunnel_prl_entry *prl;
  252. unsigned int cmax, c = 0, ca, len;
  253. int ret = 0;
  254. if (copy_from_user(&kprl, a, sizeof(kprl)))
  255. return -EFAULT;
  256. cmax = kprl.datalen / sizeof(kprl);
  257. if (cmax > 1 && kprl.addr != htonl(INADDR_ANY))
  258. cmax = 1;
  259. /* For simple GET or for root users,
  260. * we try harder to allocate.
  261. */
  262. kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
  263. kcalloc(cmax, sizeof(*kp), GFP_KERNEL) :
  264. NULL;
  265. rcu_read_lock();
  266. ca = t->prl_count < cmax ? t->prl_count : cmax;
  267. if (!kp) {
  268. /* We don't try hard to allocate much memory for
  269. * non-root users.
  270. * For root users, retry allocating enough memory for
  271. * the answer.
  272. */
  273. kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
  274. if (!kp) {
  275. ret = -ENOMEM;
  276. goto out;
  277. }
  278. }
  279. c = 0;
  280. for_each_prl_rcu(t->prl) {
  281. if (c >= cmax)
  282. break;
  283. if (kprl.addr != htonl(INADDR_ANY) && prl->addr != kprl.addr)
  284. continue;
  285. kp[c].addr = prl->addr;
  286. kp[c].flags = prl->flags;
  287. c++;
  288. if (kprl.addr != htonl(INADDR_ANY))
  289. break;
  290. }
  291. out:
  292. rcu_read_unlock();
  293. len = sizeof(*kp) * c;
  294. ret = 0;
  295. if ((len && copy_to_user(a + 1, kp, len)) || put_user(len, &a->datalen))
  296. ret = -EFAULT;
  297. kfree(kp);
  298. return ret;
  299. }
  300. static int
  301. ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg)
  302. {
  303. struct ip_tunnel_prl_entry *p;
  304. int err = 0;
  305. if (a->addr == htonl(INADDR_ANY))
  306. return -EINVAL;
  307. ASSERT_RTNL();
  308. for (p = rtnl_dereference(t->prl); p; p = rtnl_dereference(p->next)) {
  309. if (p->addr == a->addr) {
  310. if (chg) {
  311. p->flags = a->flags;
  312. goto out;
  313. }
  314. err = -EEXIST;
  315. goto out;
  316. }
  317. }
  318. if (chg) {
  319. err = -ENXIO;
  320. goto out;
  321. }
  322. p = kzalloc(sizeof(struct ip_tunnel_prl_entry), GFP_KERNEL);
  323. if (!p) {
  324. err = -ENOBUFS;
  325. goto out;
  326. }
  327. p->next = t->prl;
  328. p->addr = a->addr;
  329. p->flags = a->flags;
  330. t->prl_count++;
  331. rcu_assign_pointer(t->prl, p);
  332. out:
  333. return err;
  334. }
  335. static void prl_list_destroy_rcu(struct rcu_head *head)
  336. {
  337. struct ip_tunnel_prl_entry *p, *n;
  338. p = container_of(head, struct ip_tunnel_prl_entry, rcu_head);
  339. do {
  340. n = rcu_dereference_protected(p->next, 1);
  341. kfree(p);
  342. p = n;
  343. } while (p);
  344. }
  345. static int
  346. ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
  347. {
  348. struct ip_tunnel_prl_entry *x;
  349. struct ip_tunnel_prl_entry __rcu **p;
  350. int err = 0;
  351. ASSERT_RTNL();
  352. if (a && a->addr != htonl(INADDR_ANY)) {
  353. for (p = &t->prl;
  354. (x = rtnl_dereference(*p)) != NULL;
  355. p = &x->next) {
  356. if (x->addr == a->addr) {
  357. *p = x->next;
  358. kfree_rcu(x, rcu_head);
  359. t->prl_count--;
  360. goto out;
  361. }
  362. }
  363. err = -ENXIO;
  364. } else {
  365. x = rtnl_dereference(t->prl);
  366. if (x) {
  367. t->prl_count = 0;
  368. call_rcu(&x->rcu_head, prl_list_destroy_rcu);
  369. t->prl = NULL;
  370. }
  371. }
  372. out:
  373. return err;
  374. }
  375. static int
  376. isatap_chksrc(struct sk_buff *skb, const struct iphdr *iph, struct ip_tunnel *t)
  377. {
  378. struct ip_tunnel_prl_entry *p;
  379. int ok = 1;
  380. rcu_read_lock();
  381. p = __ipip6_tunnel_locate_prl(t, iph->saddr);
  382. if (p) {
  383. if (p->flags & PRL_DEFAULT)
  384. skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT;
  385. else
  386. skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT;
  387. } else {
  388. const struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr;
  389. if (ipv6_addr_is_isatap(addr6) &&
  390. (addr6->s6_addr32[3] == iph->saddr) &&
  391. ipv6_chk_prefix(addr6, t->dev))
  392. skb->ndisc_nodetype = NDISC_NODETYPE_HOST;
  393. else
  394. ok = 0;
  395. }
  396. rcu_read_unlock();
  397. return ok;
  398. }
  399. static void ipip6_tunnel_uninit(struct net_device *dev)
  400. {
  401. struct ip_tunnel *tunnel = netdev_priv(dev);
  402. struct sit_net *sitn = net_generic(tunnel->net, sit_net_id);
  403. if (dev == sitn->fb_tunnel_dev) {
  404. RCU_INIT_POINTER(sitn->tunnels_wc[0], NULL);
  405. } else {
  406. ipip6_tunnel_unlink(sitn, tunnel);
  407. ipip6_tunnel_del_prl(tunnel, NULL);
  408. }
  409. ip_tunnel_dst_reset_all(tunnel);
  410. dev_put(dev);
  411. }
  412. /* Generate icmpv6 with type/code ICMPV6_DEST_UNREACH/ICMPV6_ADDR_UNREACH
  413. * if sufficient data bytes are available
  414. */
  415. static int ipip6_err_gen_icmpv6_unreach(struct sk_buff *skb)
  416. {
  417. const struct iphdr *iph = (const struct iphdr *) skb->data;
  418. struct rt6_info *rt;
  419. struct sk_buff *skb2;
  420. if (!pskb_may_pull(skb, iph->ihl * 4 + sizeof(struct ipv6hdr) + 8))
  421. return 1;
  422. skb2 = skb_clone(skb, GFP_ATOMIC);
  423. if (!skb2)
  424. return 1;
  425. skb_dst_drop(skb2);
  426. skb_pull(skb2, iph->ihl * 4);
  427. skb_reset_network_header(skb2);
  428. rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr, NULL, 0, 0);
  429. if (rt && rt->dst.dev)
  430. skb2->dev = rt->dst.dev;
  431. icmpv6_send(skb2, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
  432. if (rt)
  433. ip6_rt_put(rt);
  434. kfree_skb(skb2);
  435. return 0;
  436. }
  437. static int ipip6_err(struct sk_buff *skb, u32 info)
  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. /* Impossible event. */
  452. return 0;
  453. default:
  454. /* All others are translated to HOST_UNREACH.
  455. rfc2003 contains "deep thoughts" about NET_UNREACH,
  456. I believe they are just ether pollution. --ANK
  457. */
  458. break;
  459. }
  460. break;
  461. case ICMP_TIME_EXCEEDED:
  462. if (code != ICMP_EXC_TTL)
  463. return 0;
  464. break;
  465. case ICMP_REDIRECT:
  466. break;
  467. }
  468. err = -ENOENT;
  469. t = ipip6_tunnel_lookup(dev_net(skb->dev),
  470. skb->dev,
  471. iph->daddr,
  472. iph->saddr);
  473. if (t == NULL)
  474. goto out;
  475. if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
  476. ipv4_update_pmtu(skb, dev_net(skb->dev), info,
  477. t->parms.link, 0, IPPROTO_IPV6, 0);
  478. err = 0;
  479. goto out;
  480. }
  481. if (type == ICMP_REDIRECT) {
  482. ipv4_redirect(skb, dev_net(skb->dev), t->parms.link, 0,
  483. IPPROTO_IPV6, 0);
  484. err = 0;
  485. goto out;
  486. }
  487. if (t->parms.iph.daddr == 0)
  488. goto out;
  489. err = 0;
  490. if (!ipip6_err_gen_icmpv6_unreach(skb))
  491. goto out;
  492. if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
  493. goto out;
  494. if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
  495. t->err_count++;
  496. else
  497. t->err_count = 1;
  498. t->err_time = jiffies;
  499. out:
  500. return err;
  501. }
  502. static inline bool is_spoofed_6rd(struct ip_tunnel *tunnel, const __be32 v4addr,
  503. const struct in6_addr *v6addr)
  504. {
  505. __be32 v4embed = 0;
  506. if (check_6rd(tunnel, v6addr, &v4embed) && v4addr != v4embed)
  507. return true;
  508. return false;
  509. }
  510. /* Checks if an address matches an address on the tunnel interface.
  511. * Used to detect the NAT of proto 41 packets and let them pass spoofing test.
  512. * Long story:
  513. * This function is called after we considered the packet as spoofed
  514. * in is_spoofed_6rd.
  515. * We may have a router that is doing NAT for proto 41 packets
  516. * for an internal station. Destination a.a.a.a/PREFIX:bbbb:bbbb
  517. * will be translated to n.n.n.n/PREFIX:bbbb:bbbb. And is_spoofed_6rd
  518. * function will return true, dropping the packet.
  519. * But, we can still check if is spoofed against the IP
  520. * addresses associated with the interface.
  521. */
  522. static bool only_dnatted(const struct ip_tunnel *tunnel,
  523. const struct in6_addr *v6dst)
  524. {
  525. int prefix_len;
  526. #ifdef CONFIG_IPV6_SIT_6RD
  527. prefix_len = tunnel->ip6rd.prefixlen + 32
  528. - tunnel->ip6rd.relay_prefixlen;
  529. #else
  530. prefix_len = 48;
  531. #endif
  532. return ipv6_chk_custom_prefix(v6dst, prefix_len, tunnel->dev);
  533. }
  534. /* Returns true if a packet is spoofed */
  535. static bool packet_is_spoofed(struct sk_buff *skb,
  536. const struct iphdr *iph,
  537. struct ip_tunnel *tunnel)
  538. {
  539. const struct ipv6hdr *ipv6h;
  540. if (tunnel->dev->priv_flags & IFF_ISATAP) {
  541. if (!isatap_chksrc(skb, iph, tunnel))
  542. return true;
  543. return false;
  544. }
  545. if (tunnel->dev->flags & IFF_POINTOPOINT)
  546. return false;
  547. ipv6h = ipv6_hdr(skb);
  548. if (unlikely(is_spoofed_6rd(tunnel, iph->saddr, &ipv6h->saddr))) {
  549. net_warn_ratelimited("Src spoofed %pI4/%pI6c -> %pI4/%pI6c\n",
  550. &iph->saddr, &ipv6h->saddr,
  551. &iph->daddr, &ipv6h->daddr);
  552. return true;
  553. }
  554. if (likely(!is_spoofed_6rd(tunnel, iph->daddr, &ipv6h->daddr)))
  555. return false;
  556. if (only_dnatted(tunnel, &ipv6h->daddr))
  557. return false;
  558. net_warn_ratelimited("Dst spoofed %pI4/%pI6c -> %pI4/%pI6c\n",
  559. &iph->saddr, &ipv6h->saddr,
  560. &iph->daddr, &ipv6h->daddr);
  561. return true;
  562. }
  563. static int ipip6_rcv(struct sk_buff *skb)
  564. {
  565. const struct iphdr *iph = ip_hdr(skb);
  566. struct ip_tunnel *tunnel;
  567. int err;
  568. tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
  569. iph->saddr, iph->daddr);
  570. if (tunnel != NULL) {
  571. struct pcpu_sw_netstats *tstats;
  572. if (tunnel->parms.iph.protocol != IPPROTO_IPV6 &&
  573. tunnel->parms.iph.protocol != 0)
  574. goto out;
  575. skb->mac_header = skb->network_header;
  576. skb_reset_network_header(skb);
  577. IPCB(skb)->flags = 0;
  578. skb->protocol = htons(ETH_P_IPV6);
  579. if (packet_is_spoofed(skb, iph, tunnel)) {
  580. tunnel->dev->stats.rx_errors++;
  581. goto out;
  582. }
  583. __skb_tunnel_rx(skb, tunnel->dev, tunnel->net);
  584. err = IP_ECN_decapsulate(iph, skb);
  585. if (unlikely(err)) {
  586. if (log_ecn_error)
  587. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  588. &iph->saddr, iph->tos);
  589. if (err > 1) {
  590. ++tunnel->dev->stats.rx_frame_errors;
  591. ++tunnel->dev->stats.rx_errors;
  592. goto out;
  593. }
  594. }
  595. tstats = this_cpu_ptr(tunnel->dev->tstats);
  596. u64_stats_update_begin(&tstats->syncp);
  597. tstats->rx_packets++;
  598. tstats->rx_bytes += skb->len;
  599. u64_stats_update_end(&tstats->syncp);
  600. netif_rx(skb);
  601. return 0;
  602. }
  603. /* no tunnel matched, let upstream know, ipsec may handle it */
  604. return 1;
  605. out:
  606. kfree_skb(skb);
  607. return 0;
  608. }
  609. static const struct tnl_ptk_info tpi = {
  610. /* no tunnel info required for ipip. */
  611. .proto = htons(ETH_P_IP),
  612. };
  613. static int ipip_rcv(struct sk_buff *skb)
  614. {
  615. const struct iphdr *iph;
  616. struct ip_tunnel *tunnel;
  617. iph = ip_hdr(skb);
  618. tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
  619. iph->saddr, iph->daddr);
  620. if (tunnel != NULL) {
  621. if (tunnel->parms.iph.protocol != IPPROTO_IPIP &&
  622. tunnel->parms.iph.protocol != 0)
  623. goto drop;
  624. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  625. goto drop;
  626. if (iptunnel_pull_header(skb, 0, tpi.proto))
  627. goto drop;
  628. return ip_tunnel_rcv(tunnel, skb, &tpi, log_ecn_error);
  629. }
  630. return 1;
  631. drop:
  632. kfree_skb(skb);
  633. return 0;
  634. }
  635. /*
  636. * If the IPv6 address comes from 6rd / 6to4 (RFC 3056) addr space this function
  637. * stores the embedded IPv4 address in v4dst and returns true.
  638. */
  639. static bool check_6rd(struct ip_tunnel *tunnel, const struct in6_addr *v6dst,
  640. __be32 *v4dst)
  641. {
  642. #ifdef CONFIG_IPV6_SIT_6RD
  643. if (ipv6_prefix_equal(v6dst, &tunnel->ip6rd.prefix,
  644. tunnel->ip6rd.prefixlen)) {
  645. unsigned int pbw0, pbi0;
  646. int pbi1;
  647. u32 d;
  648. pbw0 = tunnel->ip6rd.prefixlen >> 5;
  649. pbi0 = tunnel->ip6rd.prefixlen & 0x1f;
  650. d = (ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >>
  651. tunnel->ip6rd.relay_prefixlen;
  652. pbi1 = pbi0 - tunnel->ip6rd.relay_prefixlen;
  653. if (pbi1 > 0)
  654. d |= ntohl(v6dst->s6_addr32[pbw0 + 1]) >>
  655. (32 - pbi1);
  656. *v4dst = tunnel->ip6rd.relay_prefix | htonl(d);
  657. return true;
  658. }
  659. #else
  660. if (v6dst->s6_addr16[0] == htons(0x2002)) {
  661. /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
  662. memcpy(v4dst, &v6dst->s6_addr16[1], 4);
  663. return true;
  664. }
  665. #endif
  666. return false;
  667. }
  668. static inline __be32 try_6rd(struct ip_tunnel *tunnel,
  669. const struct in6_addr *v6dst)
  670. {
  671. __be32 dst = 0;
  672. check_6rd(tunnel, v6dst, &dst);
  673. return dst;
  674. }
  675. /*
  676. * This function assumes it is being called from dev_queue_xmit()
  677. * and that skb is filled properly by that function.
  678. */
  679. static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
  680. struct net_device *dev)
  681. {
  682. struct ip_tunnel *tunnel = netdev_priv(dev);
  683. const struct iphdr *tiph = &tunnel->parms.iph;
  684. const struct ipv6hdr *iph6 = ipv6_hdr(skb);
  685. u8 tos = tunnel->parms.iph.tos;
  686. __be16 df = tiph->frag_off;
  687. struct rtable *rt; /* Route to the other host */
  688. struct net_device *tdev; /* Device to other host */
  689. unsigned int max_headroom; /* The extra header space needed */
  690. __be32 dst = tiph->daddr;
  691. struct flowi4 fl4;
  692. int mtu;
  693. const struct in6_addr *addr6;
  694. int addr_type;
  695. u8 ttl;
  696. int err;
  697. if (skb->protocol != htons(ETH_P_IPV6))
  698. goto tx_error;
  699. if (tos == 1)
  700. tos = ipv6_get_dsfield(iph6);
  701. /* ISATAP (RFC4214) - must come before 6to4 */
  702. if (dev->priv_flags & IFF_ISATAP) {
  703. struct neighbour *neigh = NULL;
  704. bool do_tx_error = false;
  705. if (skb_dst(skb))
  706. neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
  707. if (neigh == NULL) {
  708. net_dbg_ratelimited("nexthop == NULL\n");
  709. goto tx_error;
  710. }
  711. addr6 = (const struct in6_addr *)&neigh->primary_key;
  712. addr_type = ipv6_addr_type(addr6);
  713. if ((addr_type & IPV6_ADDR_UNICAST) &&
  714. ipv6_addr_is_isatap(addr6))
  715. dst = addr6->s6_addr32[3];
  716. else
  717. do_tx_error = true;
  718. neigh_release(neigh);
  719. if (do_tx_error)
  720. goto tx_error;
  721. }
  722. if (!dst)
  723. dst = try_6rd(tunnel, &iph6->daddr);
  724. if (!dst) {
  725. struct neighbour *neigh = NULL;
  726. bool do_tx_error = false;
  727. if (skb_dst(skb))
  728. neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
  729. if (neigh == NULL) {
  730. net_dbg_ratelimited("nexthop == NULL\n");
  731. goto tx_error;
  732. }
  733. addr6 = (const struct in6_addr *)&neigh->primary_key;
  734. addr_type = ipv6_addr_type(addr6);
  735. if (addr_type == IPV6_ADDR_ANY) {
  736. addr6 = &ipv6_hdr(skb)->daddr;
  737. addr_type = ipv6_addr_type(addr6);
  738. }
  739. if ((addr_type & IPV6_ADDR_COMPATv4) != 0)
  740. dst = addr6->s6_addr32[3];
  741. else
  742. do_tx_error = true;
  743. neigh_release(neigh);
  744. if (do_tx_error)
  745. goto tx_error;
  746. }
  747. rt = ip_route_output_ports(tunnel->net, &fl4, NULL,
  748. dst, tiph->saddr,
  749. 0, 0,
  750. IPPROTO_IPV6, RT_TOS(tos),
  751. tunnel->parms.link);
  752. if (IS_ERR(rt)) {
  753. dev->stats.tx_carrier_errors++;
  754. goto tx_error_icmp;
  755. }
  756. if (rt->rt_type != RTN_UNICAST) {
  757. ip_rt_put(rt);
  758. dev->stats.tx_carrier_errors++;
  759. goto tx_error_icmp;
  760. }
  761. tdev = rt->dst.dev;
  762. if (tdev == dev) {
  763. ip_rt_put(rt);
  764. dev->stats.collisions++;
  765. goto tx_error;
  766. }
  767. if (df) {
  768. mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
  769. if (mtu < 68) {
  770. dev->stats.collisions++;
  771. ip_rt_put(rt);
  772. goto tx_error;
  773. }
  774. if (mtu < IPV6_MIN_MTU) {
  775. mtu = IPV6_MIN_MTU;
  776. df = 0;
  777. }
  778. if (tunnel->parms.iph.daddr && skb_dst(skb))
  779. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
  780. if (skb->len > mtu && !skb_is_gso(skb)) {
  781. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  782. ip_rt_put(rt);
  783. goto tx_error;
  784. }
  785. }
  786. if (tunnel->err_count > 0) {
  787. if (time_before(jiffies,
  788. tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
  789. tunnel->err_count--;
  790. dst_link_failure(skb);
  791. } else
  792. tunnel->err_count = 0;
  793. }
  794. /*
  795. * Okay, now see if we can stuff it in the buffer as-is.
  796. */
  797. max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
  798. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  799. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  800. struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
  801. if (!new_skb) {
  802. ip_rt_put(rt);
  803. dev->stats.tx_dropped++;
  804. kfree_skb(skb);
  805. return NETDEV_TX_OK;
  806. }
  807. if (skb->sk)
  808. skb_set_owner_w(new_skb, skb->sk);
  809. dev_kfree_skb(skb);
  810. skb = new_skb;
  811. iph6 = ipv6_hdr(skb);
  812. }
  813. ttl = tiph->ttl;
  814. if (ttl == 0)
  815. ttl = iph6->hop_limit;
  816. tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
  817. skb = iptunnel_handle_offloads(skb, false, SKB_GSO_SIT);
  818. if (IS_ERR(skb)) {
  819. ip_rt_put(rt);
  820. goto out;
  821. }
  822. err = iptunnel_xmit(skb->sk, rt, skb, fl4.saddr, fl4.daddr,
  823. IPPROTO_IPV6, tos, ttl, df,
  824. !net_eq(tunnel->net, dev_net(dev)));
  825. iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
  826. return NETDEV_TX_OK;
  827. tx_error_icmp:
  828. dst_link_failure(skb);
  829. tx_error:
  830. kfree_skb(skb);
  831. out:
  832. dev->stats.tx_errors++;
  833. return NETDEV_TX_OK;
  834. }
  835. static netdev_tx_t ipip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
  836. {
  837. struct ip_tunnel *tunnel = netdev_priv(dev);
  838. const struct iphdr *tiph = &tunnel->parms.iph;
  839. skb = iptunnel_handle_offloads(skb, false, SKB_GSO_IPIP);
  840. if (IS_ERR(skb))
  841. goto out;
  842. ip_tunnel_xmit(skb, dev, tiph, IPPROTO_IPIP);
  843. return NETDEV_TX_OK;
  844. out:
  845. dev->stats.tx_errors++;
  846. return NETDEV_TX_OK;
  847. }
  848. static netdev_tx_t sit_tunnel_xmit(struct sk_buff *skb,
  849. struct net_device *dev)
  850. {
  851. switch (skb->protocol) {
  852. case htons(ETH_P_IP):
  853. ipip_tunnel_xmit(skb, dev);
  854. break;
  855. case htons(ETH_P_IPV6):
  856. ipip6_tunnel_xmit(skb, dev);
  857. break;
  858. default:
  859. goto tx_err;
  860. }
  861. return NETDEV_TX_OK;
  862. tx_err:
  863. dev->stats.tx_errors++;
  864. kfree_skb(skb);
  865. return NETDEV_TX_OK;
  866. }
  867. static void ipip6_tunnel_bind_dev(struct net_device *dev)
  868. {
  869. struct net_device *tdev = NULL;
  870. struct ip_tunnel *tunnel;
  871. const struct iphdr *iph;
  872. struct flowi4 fl4;
  873. tunnel = netdev_priv(dev);
  874. iph = &tunnel->parms.iph;
  875. if (iph->daddr) {
  876. struct rtable *rt = ip_route_output_ports(tunnel->net, &fl4,
  877. NULL,
  878. iph->daddr, iph->saddr,
  879. 0, 0,
  880. IPPROTO_IPV6,
  881. RT_TOS(iph->tos),
  882. tunnel->parms.link);
  883. if (!IS_ERR(rt)) {
  884. tdev = rt->dst.dev;
  885. ip_rt_put(rt);
  886. }
  887. dev->flags |= IFF_POINTOPOINT;
  888. }
  889. if (!tdev && tunnel->parms.link)
  890. tdev = __dev_get_by_index(tunnel->net, tunnel->parms.link);
  891. if (tdev) {
  892. dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
  893. dev->mtu = tdev->mtu - sizeof(struct iphdr);
  894. if (dev->mtu < IPV6_MIN_MTU)
  895. dev->mtu = IPV6_MIN_MTU;
  896. }
  897. dev->iflink = tunnel->parms.link;
  898. }
  899. static void ipip6_tunnel_update(struct ip_tunnel *t, struct ip_tunnel_parm *p)
  900. {
  901. struct net *net = t->net;
  902. struct sit_net *sitn = net_generic(net, sit_net_id);
  903. ipip6_tunnel_unlink(sitn, t);
  904. synchronize_net();
  905. t->parms.iph.saddr = p->iph.saddr;
  906. t->parms.iph.daddr = p->iph.daddr;
  907. memcpy(t->dev->dev_addr, &p->iph.saddr, 4);
  908. memcpy(t->dev->broadcast, &p->iph.daddr, 4);
  909. ipip6_tunnel_link(sitn, t);
  910. t->parms.iph.ttl = p->iph.ttl;
  911. t->parms.iph.tos = p->iph.tos;
  912. if (t->parms.link != p->link) {
  913. t->parms.link = p->link;
  914. ipip6_tunnel_bind_dev(t->dev);
  915. }
  916. ip_tunnel_dst_reset_all(t);
  917. netdev_state_change(t->dev);
  918. }
  919. #ifdef CONFIG_IPV6_SIT_6RD
  920. static int ipip6_tunnel_update_6rd(struct ip_tunnel *t,
  921. struct ip_tunnel_6rd *ip6rd)
  922. {
  923. struct in6_addr prefix;
  924. __be32 relay_prefix;
  925. if (ip6rd->relay_prefixlen > 32 ||
  926. ip6rd->prefixlen + (32 - ip6rd->relay_prefixlen) > 64)
  927. return -EINVAL;
  928. ipv6_addr_prefix(&prefix, &ip6rd->prefix, ip6rd->prefixlen);
  929. if (!ipv6_addr_equal(&prefix, &ip6rd->prefix))
  930. return -EINVAL;
  931. if (ip6rd->relay_prefixlen)
  932. relay_prefix = ip6rd->relay_prefix &
  933. htonl(0xffffffffUL <<
  934. (32 - ip6rd->relay_prefixlen));
  935. else
  936. relay_prefix = 0;
  937. if (relay_prefix != ip6rd->relay_prefix)
  938. return -EINVAL;
  939. t->ip6rd.prefix = prefix;
  940. t->ip6rd.relay_prefix = relay_prefix;
  941. t->ip6rd.prefixlen = ip6rd->prefixlen;
  942. t->ip6rd.relay_prefixlen = ip6rd->relay_prefixlen;
  943. ip_tunnel_dst_reset_all(t);
  944. netdev_state_change(t->dev);
  945. return 0;
  946. }
  947. #endif
  948. static int
  949. ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
  950. {
  951. int err = 0;
  952. struct ip_tunnel_parm p;
  953. struct ip_tunnel_prl prl;
  954. struct ip_tunnel *t = netdev_priv(dev);
  955. struct net *net = t->net;
  956. struct sit_net *sitn = net_generic(net, sit_net_id);
  957. #ifdef CONFIG_IPV6_SIT_6RD
  958. struct ip_tunnel_6rd ip6rd;
  959. #endif
  960. switch (cmd) {
  961. case SIOCGETTUNNEL:
  962. #ifdef CONFIG_IPV6_SIT_6RD
  963. case SIOCGET6RD:
  964. #endif
  965. if (dev == sitn->fb_tunnel_dev) {
  966. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  967. err = -EFAULT;
  968. break;
  969. }
  970. t = ipip6_tunnel_locate(net, &p, 0);
  971. if (t == NULL)
  972. t = netdev_priv(dev);
  973. }
  974. err = -EFAULT;
  975. if (cmd == SIOCGETTUNNEL) {
  976. memcpy(&p, &t->parms, sizeof(p));
  977. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p,
  978. sizeof(p)))
  979. goto done;
  980. #ifdef CONFIG_IPV6_SIT_6RD
  981. } else {
  982. ip6rd.prefix = t->ip6rd.prefix;
  983. ip6rd.relay_prefix = t->ip6rd.relay_prefix;
  984. ip6rd.prefixlen = t->ip6rd.prefixlen;
  985. ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen;
  986. if (copy_to_user(ifr->ifr_ifru.ifru_data, &ip6rd,
  987. sizeof(ip6rd)))
  988. goto done;
  989. #endif
  990. }
  991. err = 0;
  992. break;
  993. case SIOCADDTUNNEL:
  994. case SIOCCHGTUNNEL:
  995. err = -EPERM;
  996. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  997. goto done;
  998. err = -EFAULT;
  999. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1000. goto done;
  1001. err = -EINVAL;
  1002. if (p.iph.protocol != IPPROTO_IPV6 &&
  1003. p.iph.protocol != IPPROTO_IPIP &&
  1004. p.iph.protocol != 0)
  1005. goto done;
  1006. if (p.iph.version != 4 ||
  1007. p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
  1008. goto done;
  1009. if (p.iph.ttl)
  1010. p.iph.frag_off |= htons(IP_DF);
  1011. t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
  1012. if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  1013. if (t != NULL) {
  1014. if (t->dev != dev) {
  1015. err = -EEXIST;
  1016. break;
  1017. }
  1018. } else {
  1019. if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
  1020. (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
  1021. err = -EINVAL;
  1022. break;
  1023. }
  1024. t = netdev_priv(dev);
  1025. }
  1026. ipip6_tunnel_update(t, &p);
  1027. }
  1028. if (t) {
  1029. err = 0;
  1030. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
  1031. err = -EFAULT;
  1032. } else
  1033. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  1034. break;
  1035. case SIOCDELTUNNEL:
  1036. err = -EPERM;
  1037. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1038. goto done;
  1039. if (dev == sitn->fb_tunnel_dev) {
  1040. err = -EFAULT;
  1041. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1042. goto done;
  1043. err = -ENOENT;
  1044. if ((t = ipip6_tunnel_locate(net, &p, 0)) == NULL)
  1045. goto done;
  1046. err = -EPERM;
  1047. if (t == netdev_priv(sitn->fb_tunnel_dev))
  1048. goto done;
  1049. dev = t->dev;
  1050. }
  1051. unregister_netdevice(dev);
  1052. err = 0;
  1053. break;
  1054. case SIOCGETPRL:
  1055. err = -EINVAL;
  1056. if (dev == sitn->fb_tunnel_dev)
  1057. goto done;
  1058. err = ipip6_tunnel_get_prl(t, ifr->ifr_ifru.ifru_data);
  1059. break;
  1060. case SIOCADDPRL:
  1061. case SIOCDELPRL:
  1062. case SIOCCHGPRL:
  1063. err = -EPERM;
  1064. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1065. goto done;
  1066. err = -EINVAL;
  1067. if (dev == sitn->fb_tunnel_dev)
  1068. goto done;
  1069. err = -EFAULT;
  1070. if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
  1071. goto done;
  1072. switch (cmd) {
  1073. case SIOCDELPRL:
  1074. err = ipip6_tunnel_del_prl(t, &prl);
  1075. break;
  1076. case SIOCADDPRL:
  1077. case SIOCCHGPRL:
  1078. err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
  1079. break;
  1080. }
  1081. ip_tunnel_dst_reset_all(t);
  1082. netdev_state_change(dev);
  1083. break;
  1084. #ifdef CONFIG_IPV6_SIT_6RD
  1085. case SIOCADD6RD:
  1086. case SIOCCHG6RD:
  1087. case SIOCDEL6RD:
  1088. err = -EPERM;
  1089. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1090. goto done;
  1091. err = -EFAULT;
  1092. if (copy_from_user(&ip6rd, ifr->ifr_ifru.ifru_data,
  1093. sizeof(ip6rd)))
  1094. goto done;
  1095. if (cmd != SIOCDEL6RD) {
  1096. err = ipip6_tunnel_update_6rd(t, &ip6rd);
  1097. if (err < 0)
  1098. goto done;
  1099. } else
  1100. ipip6_tunnel_clone_6rd(dev, sitn);
  1101. err = 0;
  1102. break;
  1103. #endif
  1104. default:
  1105. err = -EINVAL;
  1106. }
  1107. done:
  1108. return err;
  1109. }
  1110. static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  1111. {
  1112. if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
  1113. return -EINVAL;
  1114. dev->mtu = new_mtu;
  1115. return 0;
  1116. }
  1117. static const struct net_device_ops ipip6_netdev_ops = {
  1118. .ndo_uninit = ipip6_tunnel_uninit,
  1119. .ndo_start_xmit = sit_tunnel_xmit,
  1120. .ndo_do_ioctl = ipip6_tunnel_ioctl,
  1121. .ndo_change_mtu = ipip6_tunnel_change_mtu,
  1122. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1123. };
  1124. static void ipip6_dev_free(struct net_device *dev)
  1125. {
  1126. struct ip_tunnel *tunnel = netdev_priv(dev);
  1127. free_percpu(tunnel->dst_cache);
  1128. free_percpu(dev->tstats);
  1129. free_netdev(dev);
  1130. }
  1131. #define SIT_FEATURES (NETIF_F_SG | \
  1132. NETIF_F_FRAGLIST | \
  1133. NETIF_F_HIGHDMA | \
  1134. NETIF_F_GSO_SOFTWARE | \
  1135. NETIF_F_HW_CSUM)
  1136. static void ipip6_tunnel_setup(struct net_device *dev)
  1137. {
  1138. dev->netdev_ops = &ipip6_netdev_ops;
  1139. dev->destructor = ipip6_dev_free;
  1140. dev->type = ARPHRD_SIT;
  1141. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
  1142. dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
  1143. dev->flags = IFF_NOARP;
  1144. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1145. dev->iflink = 0;
  1146. dev->addr_len = 4;
  1147. dev->features |= NETIF_F_LLTX;
  1148. dev->features |= SIT_FEATURES;
  1149. dev->hw_features |= SIT_FEATURES;
  1150. }
  1151. static int ipip6_tunnel_init(struct net_device *dev)
  1152. {
  1153. struct ip_tunnel *tunnel = netdev_priv(dev);
  1154. tunnel->dev = dev;
  1155. tunnel->net = dev_net(dev);
  1156. memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
  1157. memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
  1158. ipip6_tunnel_bind_dev(dev);
  1159. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1160. if (!dev->tstats)
  1161. return -ENOMEM;
  1162. tunnel->dst_cache = alloc_percpu(struct ip_tunnel_dst);
  1163. if (!tunnel->dst_cache) {
  1164. free_percpu(dev->tstats);
  1165. return -ENOMEM;
  1166. }
  1167. return 0;
  1168. }
  1169. static int __net_init ipip6_fb_tunnel_init(struct net_device *dev)
  1170. {
  1171. struct ip_tunnel *tunnel = netdev_priv(dev);
  1172. struct iphdr *iph = &tunnel->parms.iph;
  1173. struct net *net = dev_net(dev);
  1174. struct sit_net *sitn = net_generic(net, sit_net_id);
  1175. tunnel->dev = dev;
  1176. tunnel->net = dev_net(dev);
  1177. strcpy(tunnel->parms.name, dev->name);
  1178. iph->version = 4;
  1179. iph->protocol = IPPROTO_IPV6;
  1180. iph->ihl = 5;
  1181. iph->ttl = 64;
  1182. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1183. if (!dev->tstats)
  1184. return -ENOMEM;
  1185. tunnel->dst_cache = alloc_percpu(struct ip_tunnel_dst);
  1186. if (!tunnel->dst_cache) {
  1187. free_percpu(dev->tstats);
  1188. return -ENOMEM;
  1189. }
  1190. dev_hold(dev);
  1191. rcu_assign_pointer(sitn->tunnels_wc[0], tunnel);
  1192. return 0;
  1193. }
  1194. static int ipip6_validate(struct nlattr *tb[], struct nlattr *data[])
  1195. {
  1196. u8 proto;
  1197. if (!data || !data[IFLA_IPTUN_PROTO])
  1198. return 0;
  1199. proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1200. if (proto != IPPROTO_IPV6 &&
  1201. proto != IPPROTO_IPIP &&
  1202. proto != 0)
  1203. return -EINVAL;
  1204. return 0;
  1205. }
  1206. static void ipip6_netlink_parms(struct nlattr *data[],
  1207. struct ip_tunnel_parm *parms)
  1208. {
  1209. memset(parms, 0, sizeof(*parms));
  1210. parms->iph.version = 4;
  1211. parms->iph.protocol = IPPROTO_IPV6;
  1212. parms->iph.ihl = 5;
  1213. parms->iph.ttl = 64;
  1214. if (!data)
  1215. return;
  1216. if (data[IFLA_IPTUN_LINK])
  1217. parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
  1218. if (data[IFLA_IPTUN_LOCAL])
  1219. parms->iph.saddr = nla_get_be32(data[IFLA_IPTUN_LOCAL]);
  1220. if (data[IFLA_IPTUN_REMOTE])
  1221. parms->iph.daddr = nla_get_be32(data[IFLA_IPTUN_REMOTE]);
  1222. if (data[IFLA_IPTUN_TTL]) {
  1223. parms->iph.ttl = nla_get_u8(data[IFLA_IPTUN_TTL]);
  1224. if (parms->iph.ttl)
  1225. parms->iph.frag_off = htons(IP_DF);
  1226. }
  1227. if (data[IFLA_IPTUN_TOS])
  1228. parms->iph.tos = nla_get_u8(data[IFLA_IPTUN_TOS]);
  1229. if (!data[IFLA_IPTUN_PMTUDISC] || nla_get_u8(data[IFLA_IPTUN_PMTUDISC]))
  1230. parms->iph.frag_off = htons(IP_DF);
  1231. if (data[IFLA_IPTUN_FLAGS])
  1232. parms->i_flags = nla_get_be16(data[IFLA_IPTUN_FLAGS]);
  1233. if (data[IFLA_IPTUN_PROTO])
  1234. parms->iph.protocol = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1235. }
  1236. #ifdef CONFIG_IPV6_SIT_6RD
  1237. /* This function returns true when 6RD attributes are present in the nl msg */
  1238. static bool ipip6_netlink_6rd_parms(struct nlattr *data[],
  1239. struct ip_tunnel_6rd *ip6rd)
  1240. {
  1241. bool ret = false;
  1242. memset(ip6rd, 0, sizeof(*ip6rd));
  1243. if (!data)
  1244. return ret;
  1245. if (data[IFLA_IPTUN_6RD_PREFIX]) {
  1246. ret = true;
  1247. nla_memcpy(&ip6rd->prefix, data[IFLA_IPTUN_6RD_PREFIX],
  1248. sizeof(struct in6_addr));
  1249. }
  1250. if (data[IFLA_IPTUN_6RD_RELAY_PREFIX]) {
  1251. ret = true;
  1252. ip6rd->relay_prefix =
  1253. nla_get_be32(data[IFLA_IPTUN_6RD_RELAY_PREFIX]);
  1254. }
  1255. if (data[IFLA_IPTUN_6RD_PREFIXLEN]) {
  1256. ret = true;
  1257. ip6rd->prefixlen = nla_get_u16(data[IFLA_IPTUN_6RD_PREFIXLEN]);
  1258. }
  1259. if (data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]) {
  1260. ret = true;
  1261. ip6rd->relay_prefixlen =
  1262. nla_get_u16(data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]);
  1263. }
  1264. return ret;
  1265. }
  1266. #endif
  1267. static int ipip6_newlink(struct net *src_net, struct net_device *dev,
  1268. struct nlattr *tb[], struct nlattr *data[])
  1269. {
  1270. struct net *net = dev_net(dev);
  1271. struct ip_tunnel *nt;
  1272. #ifdef CONFIG_IPV6_SIT_6RD
  1273. struct ip_tunnel_6rd ip6rd;
  1274. #endif
  1275. int err;
  1276. nt = netdev_priv(dev);
  1277. ipip6_netlink_parms(data, &nt->parms);
  1278. if (ipip6_tunnel_locate(net, &nt->parms, 0))
  1279. return -EEXIST;
  1280. err = ipip6_tunnel_create(dev);
  1281. if (err < 0)
  1282. return err;
  1283. #ifdef CONFIG_IPV6_SIT_6RD
  1284. if (ipip6_netlink_6rd_parms(data, &ip6rd))
  1285. err = ipip6_tunnel_update_6rd(nt, &ip6rd);
  1286. #endif
  1287. return err;
  1288. }
  1289. static int ipip6_changelink(struct net_device *dev, struct nlattr *tb[],
  1290. struct nlattr *data[])
  1291. {
  1292. struct ip_tunnel *t = netdev_priv(dev);
  1293. struct ip_tunnel_parm p;
  1294. struct net *net = t->net;
  1295. struct sit_net *sitn = net_generic(net, sit_net_id);
  1296. #ifdef CONFIG_IPV6_SIT_6RD
  1297. struct ip_tunnel_6rd ip6rd;
  1298. #endif
  1299. if (dev == sitn->fb_tunnel_dev)
  1300. return -EINVAL;
  1301. ipip6_netlink_parms(data, &p);
  1302. if (((dev->flags & IFF_POINTOPOINT) && !p.iph.daddr) ||
  1303. (!(dev->flags & IFF_POINTOPOINT) && p.iph.daddr))
  1304. return -EINVAL;
  1305. t = ipip6_tunnel_locate(net, &p, 0);
  1306. if (t) {
  1307. if (t->dev != dev)
  1308. return -EEXIST;
  1309. } else
  1310. t = netdev_priv(dev);
  1311. ipip6_tunnel_update(t, &p);
  1312. #ifdef CONFIG_IPV6_SIT_6RD
  1313. if (ipip6_netlink_6rd_parms(data, &ip6rd))
  1314. return ipip6_tunnel_update_6rd(t, &ip6rd);
  1315. #endif
  1316. return 0;
  1317. }
  1318. static size_t ipip6_get_size(const struct net_device *dev)
  1319. {
  1320. return
  1321. /* IFLA_IPTUN_LINK */
  1322. nla_total_size(4) +
  1323. /* IFLA_IPTUN_LOCAL */
  1324. nla_total_size(4) +
  1325. /* IFLA_IPTUN_REMOTE */
  1326. nla_total_size(4) +
  1327. /* IFLA_IPTUN_TTL */
  1328. nla_total_size(1) +
  1329. /* IFLA_IPTUN_TOS */
  1330. nla_total_size(1) +
  1331. /* IFLA_IPTUN_PMTUDISC */
  1332. nla_total_size(1) +
  1333. /* IFLA_IPTUN_FLAGS */
  1334. nla_total_size(2) +
  1335. /* IFLA_IPTUN_PROTO */
  1336. nla_total_size(1) +
  1337. #ifdef CONFIG_IPV6_SIT_6RD
  1338. /* IFLA_IPTUN_6RD_PREFIX */
  1339. nla_total_size(sizeof(struct in6_addr)) +
  1340. /* IFLA_IPTUN_6RD_RELAY_PREFIX */
  1341. nla_total_size(4) +
  1342. /* IFLA_IPTUN_6RD_PREFIXLEN */
  1343. nla_total_size(2) +
  1344. /* IFLA_IPTUN_6RD_RELAY_PREFIXLEN */
  1345. nla_total_size(2) +
  1346. #endif
  1347. 0;
  1348. }
  1349. static int ipip6_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1350. {
  1351. struct ip_tunnel *tunnel = netdev_priv(dev);
  1352. struct ip_tunnel_parm *parm = &tunnel->parms;
  1353. if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
  1354. nla_put_be32(skb, IFLA_IPTUN_LOCAL, parm->iph.saddr) ||
  1355. nla_put_be32(skb, IFLA_IPTUN_REMOTE, parm->iph.daddr) ||
  1356. nla_put_u8(skb, IFLA_IPTUN_TTL, parm->iph.ttl) ||
  1357. nla_put_u8(skb, IFLA_IPTUN_TOS, parm->iph.tos) ||
  1358. nla_put_u8(skb, IFLA_IPTUN_PMTUDISC,
  1359. !!(parm->iph.frag_off & htons(IP_DF))) ||
  1360. nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->iph.protocol) ||
  1361. nla_put_be16(skb, IFLA_IPTUN_FLAGS, parm->i_flags))
  1362. goto nla_put_failure;
  1363. #ifdef CONFIG_IPV6_SIT_6RD
  1364. if (nla_put(skb, IFLA_IPTUN_6RD_PREFIX, sizeof(struct in6_addr),
  1365. &tunnel->ip6rd.prefix) ||
  1366. nla_put_be32(skb, IFLA_IPTUN_6RD_RELAY_PREFIX,
  1367. tunnel->ip6rd.relay_prefix) ||
  1368. nla_put_u16(skb, IFLA_IPTUN_6RD_PREFIXLEN,
  1369. tunnel->ip6rd.prefixlen) ||
  1370. nla_put_u16(skb, IFLA_IPTUN_6RD_RELAY_PREFIXLEN,
  1371. tunnel->ip6rd.relay_prefixlen))
  1372. goto nla_put_failure;
  1373. #endif
  1374. return 0;
  1375. nla_put_failure:
  1376. return -EMSGSIZE;
  1377. }
  1378. static const struct nla_policy ipip6_policy[IFLA_IPTUN_MAX + 1] = {
  1379. [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
  1380. [IFLA_IPTUN_LOCAL] = { .type = NLA_U32 },
  1381. [IFLA_IPTUN_REMOTE] = { .type = NLA_U32 },
  1382. [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
  1383. [IFLA_IPTUN_TOS] = { .type = NLA_U8 },
  1384. [IFLA_IPTUN_PMTUDISC] = { .type = NLA_U8 },
  1385. [IFLA_IPTUN_FLAGS] = { .type = NLA_U16 },
  1386. [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
  1387. #ifdef CONFIG_IPV6_SIT_6RD
  1388. [IFLA_IPTUN_6RD_PREFIX] = { .len = sizeof(struct in6_addr) },
  1389. [IFLA_IPTUN_6RD_RELAY_PREFIX] = { .type = NLA_U32 },
  1390. [IFLA_IPTUN_6RD_PREFIXLEN] = { .type = NLA_U16 },
  1391. [IFLA_IPTUN_6RD_RELAY_PREFIXLEN] = { .type = NLA_U16 },
  1392. #endif
  1393. };
  1394. static void ipip6_dellink(struct net_device *dev, struct list_head *head)
  1395. {
  1396. struct net *net = dev_net(dev);
  1397. struct sit_net *sitn = net_generic(net, sit_net_id);
  1398. if (dev != sitn->fb_tunnel_dev)
  1399. unregister_netdevice_queue(dev, head);
  1400. }
  1401. static struct rtnl_link_ops sit_link_ops __read_mostly = {
  1402. .kind = "sit",
  1403. .maxtype = IFLA_IPTUN_MAX,
  1404. .policy = ipip6_policy,
  1405. .priv_size = sizeof(struct ip_tunnel),
  1406. .setup = ipip6_tunnel_setup,
  1407. .validate = ipip6_validate,
  1408. .newlink = ipip6_newlink,
  1409. .changelink = ipip6_changelink,
  1410. .get_size = ipip6_get_size,
  1411. .fill_info = ipip6_fill_info,
  1412. .dellink = ipip6_dellink,
  1413. };
  1414. static struct xfrm_tunnel sit_handler __read_mostly = {
  1415. .handler = ipip6_rcv,
  1416. .err_handler = ipip6_err,
  1417. .priority = 1,
  1418. };
  1419. static struct xfrm_tunnel ipip_handler __read_mostly = {
  1420. .handler = ipip_rcv,
  1421. .err_handler = ipip6_err,
  1422. .priority = 2,
  1423. };
  1424. static void __net_exit sit_destroy_tunnels(struct net *net,
  1425. struct list_head *head)
  1426. {
  1427. struct sit_net *sitn = net_generic(net, sit_net_id);
  1428. struct net_device *dev, *aux;
  1429. int prio;
  1430. for_each_netdev_safe(net, dev, aux)
  1431. if (dev->rtnl_link_ops == &sit_link_ops)
  1432. unregister_netdevice_queue(dev, head);
  1433. for (prio = 1; prio < 4; prio++) {
  1434. int h;
  1435. for (h = 0; h < HASH_SIZE; h++) {
  1436. struct ip_tunnel *t;
  1437. t = rtnl_dereference(sitn->tunnels[prio][h]);
  1438. while (t != NULL) {
  1439. /* If dev is in the same netns, it has already
  1440. * been added to the list by the previous loop.
  1441. */
  1442. if (!net_eq(dev_net(t->dev), net))
  1443. unregister_netdevice_queue(t->dev,
  1444. head);
  1445. t = rtnl_dereference(t->next);
  1446. }
  1447. }
  1448. }
  1449. }
  1450. static int __net_init sit_init_net(struct net *net)
  1451. {
  1452. struct sit_net *sitn = net_generic(net, sit_net_id);
  1453. struct ip_tunnel *t;
  1454. int err;
  1455. sitn->tunnels[0] = sitn->tunnels_wc;
  1456. sitn->tunnels[1] = sitn->tunnels_l;
  1457. sitn->tunnels[2] = sitn->tunnels_r;
  1458. sitn->tunnels[3] = sitn->tunnels_r_l;
  1459. sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
  1460. NET_NAME_UNKNOWN,
  1461. ipip6_tunnel_setup);
  1462. if (!sitn->fb_tunnel_dev) {
  1463. err = -ENOMEM;
  1464. goto err_alloc_dev;
  1465. }
  1466. dev_net_set(sitn->fb_tunnel_dev, net);
  1467. sitn->fb_tunnel_dev->rtnl_link_ops = &sit_link_ops;
  1468. /* FB netdevice is special: we have one, and only one per netns.
  1469. * Allowing to move it to another netns is clearly unsafe.
  1470. */
  1471. sitn->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
  1472. err = ipip6_fb_tunnel_init(sitn->fb_tunnel_dev);
  1473. if (err)
  1474. goto err_dev_free;
  1475. ipip6_tunnel_clone_6rd(sitn->fb_tunnel_dev, sitn);
  1476. if ((err = register_netdev(sitn->fb_tunnel_dev)))
  1477. goto err_reg_dev;
  1478. t = netdev_priv(sitn->fb_tunnel_dev);
  1479. strcpy(t->parms.name, sitn->fb_tunnel_dev->name);
  1480. return 0;
  1481. err_reg_dev:
  1482. dev_put(sitn->fb_tunnel_dev);
  1483. err_dev_free:
  1484. ipip6_dev_free(sitn->fb_tunnel_dev);
  1485. err_alloc_dev:
  1486. return err;
  1487. }
  1488. static void __net_exit sit_exit_net(struct net *net)
  1489. {
  1490. LIST_HEAD(list);
  1491. rtnl_lock();
  1492. sit_destroy_tunnels(net, &list);
  1493. unregister_netdevice_many(&list);
  1494. rtnl_unlock();
  1495. }
  1496. static struct pernet_operations sit_net_ops = {
  1497. .init = sit_init_net,
  1498. .exit = sit_exit_net,
  1499. .id = &sit_net_id,
  1500. .size = sizeof(struct sit_net),
  1501. };
  1502. static void __exit sit_cleanup(void)
  1503. {
  1504. rtnl_link_unregister(&sit_link_ops);
  1505. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  1506. xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
  1507. unregister_pernet_device(&sit_net_ops);
  1508. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  1509. }
  1510. static int __init sit_init(void)
  1511. {
  1512. int err;
  1513. pr_info("IPv6 over IPv4 tunneling driver\n");
  1514. err = register_pernet_device(&sit_net_ops);
  1515. if (err < 0)
  1516. return err;
  1517. err = xfrm4_tunnel_register(&sit_handler, AF_INET6);
  1518. if (err < 0) {
  1519. pr_info("%s: can't register ip6ip4\n", __func__);
  1520. goto xfrm_tunnel_failed;
  1521. }
  1522. err = xfrm4_tunnel_register(&ipip_handler, AF_INET);
  1523. if (err < 0) {
  1524. pr_info("%s: can't register ip4ip4\n", __func__);
  1525. goto xfrm_tunnel4_failed;
  1526. }
  1527. err = rtnl_link_register(&sit_link_ops);
  1528. if (err < 0)
  1529. goto rtnl_link_failed;
  1530. out:
  1531. return err;
  1532. rtnl_link_failed:
  1533. xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
  1534. xfrm_tunnel4_failed:
  1535. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  1536. xfrm_tunnel_failed:
  1537. unregister_pernet_device(&sit_net_ops);
  1538. goto out;
  1539. }
  1540. module_init(sit_init);
  1541. module_exit(sit_cleanup);
  1542. MODULE_LICENSE("GPL");
  1543. MODULE_ALIAS_RTNL_LINK("sit");
  1544. MODULE_ALIAS_NETDEV("sit0");