ip6_tunnel.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  1. /*
  2. * IPv6 tunneling device
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Ville Nuorvala <vnuorval@tcs.hut.fi>
  7. * Yasuyuki Kozakai <kozakai@linux-ipv6.org>
  8. *
  9. * Based on:
  10. * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
  11. *
  12. * RFC 2473
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. *
  19. */
  20. #include <linux/module.h>
  21. #include <linux/capability.h>
  22. #include <linux/errno.h>
  23. #include <linux/types.h>
  24. #include <linux/sockios.h>
  25. #include <linux/icmp.h>
  26. #include <linux/if.h>
  27. #include <linux/in.h>
  28. #include <linux/ip.h>
  29. #include <linux/if_tunnel.h>
  30. #include <linux/net.h>
  31. #include <linux/in6.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/if_arp.h>
  34. #include <linux/icmpv6.h>
  35. #include <linux/init.h>
  36. #include <linux/route.h>
  37. #include <linux/rtnetlink.h>
  38. #include <linux/netfilter_ipv6.h>
  39. #include <asm/uaccess.h>
  40. #include <asm/atomic.h>
  41. #include <net/icmp.h>
  42. #include <net/ip.h>
  43. #include <net/ipv6.h>
  44. #include <net/ip6_route.h>
  45. #include <net/addrconf.h>
  46. #include <net/ip6_tunnel.h>
  47. #include <net/xfrm.h>
  48. #include <net/dsfield.h>
  49. #include <net/inet_ecn.h>
  50. #include <net/net_namespace.h>
  51. #include <net/netns/generic.h>
  52. MODULE_AUTHOR("Ville Nuorvala");
  53. MODULE_DESCRIPTION("IPv6 tunneling device");
  54. MODULE_LICENSE("GPL");
  55. #define IPV6_TLV_TEL_DST_SIZE 8
  56. #ifdef IP6_TNL_DEBUG
  57. #define IP6_TNL_TRACE(x...) printk(KERN_DEBUG "%s:" x "\n", __func__)
  58. #else
  59. #define IP6_TNL_TRACE(x...) do {;} while(0)
  60. #endif
  61. #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
  62. #define IPV6_TCLASS_SHIFT 20
  63. #define HASH_SIZE 32
  64. #define HASH(addr) ((__force u32)((addr)->s6_addr32[0] ^ (addr)->s6_addr32[1] ^ \
  65. (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \
  66. (HASH_SIZE - 1))
  67. static void ip6_fb_tnl_dev_init(struct net_device *dev);
  68. static void ip6_tnl_dev_init(struct net_device *dev);
  69. static void ip6_tnl_dev_setup(struct net_device *dev);
  70. static int ip6_tnl_net_id;
  71. struct ip6_tnl_net {
  72. /* the IPv6 tunnel fallback device */
  73. struct net_device *fb_tnl_dev;
  74. /* lists for storing tunnels in use */
  75. struct ip6_tnl *tnls_r_l[HASH_SIZE];
  76. struct ip6_tnl *tnls_wc[1];
  77. struct ip6_tnl **tnls[2];
  78. };
  79. /* lock for the tunnel lists */
  80. static DEFINE_RWLOCK(ip6_tnl_lock);
  81. static inline struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
  82. {
  83. struct dst_entry *dst = t->dst_cache;
  84. if (dst && dst->obsolete &&
  85. dst->ops->check(dst, t->dst_cookie) == NULL) {
  86. t->dst_cache = NULL;
  87. dst_release(dst);
  88. return NULL;
  89. }
  90. return dst;
  91. }
  92. static inline void ip6_tnl_dst_reset(struct ip6_tnl *t)
  93. {
  94. dst_release(t->dst_cache);
  95. t->dst_cache = NULL;
  96. }
  97. static inline void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
  98. {
  99. struct rt6_info *rt = (struct rt6_info *) dst;
  100. t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
  101. dst_release(t->dst_cache);
  102. t->dst_cache = dst;
  103. }
  104. /**
  105. * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
  106. * @remote: the address of the tunnel exit-point
  107. * @local: the address of the tunnel entry-point
  108. *
  109. * Return:
  110. * tunnel matching given end-points if found,
  111. * else fallback tunnel if its device is up,
  112. * else %NULL
  113. **/
  114. static struct ip6_tnl *
  115. ip6_tnl_lookup(struct net *net, struct in6_addr *remote, struct in6_addr *local)
  116. {
  117. unsigned h0 = HASH(remote);
  118. unsigned h1 = HASH(local);
  119. struct ip6_tnl *t;
  120. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  121. for (t = ip6n->tnls_r_l[h0 ^ h1]; t; t = t->next) {
  122. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  123. ipv6_addr_equal(remote, &t->parms.raddr) &&
  124. (t->dev->flags & IFF_UP))
  125. return t;
  126. }
  127. if ((t = ip6n->tnls_wc[0]) != NULL && (t->dev->flags & IFF_UP))
  128. return t;
  129. return NULL;
  130. }
  131. /**
  132. * ip6_tnl_bucket - get head of list matching given tunnel parameters
  133. * @p: parameters containing tunnel end-points
  134. *
  135. * Description:
  136. * ip6_tnl_bucket() returns the head of the list matching the
  137. * &struct in6_addr entries laddr and raddr in @p.
  138. *
  139. * Return: head of IPv6 tunnel list
  140. **/
  141. static struct ip6_tnl **
  142. ip6_tnl_bucket(struct ip6_tnl_net *ip6n, struct ip6_tnl_parm *p)
  143. {
  144. struct in6_addr *remote = &p->raddr;
  145. struct in6_addr *local = &p->laddr;
  146. unsigned h = 0;
  147. int prio = 0;
  148. if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
  149. prio = 1;
  150. h = HASH(remote) ^ HASH(local);
  151. }
  152. return &ip6n->tnls[prio][h];
  153. }
  154. /**
  155. * ip6_tnl_link - add tunnel to hash table
  156. * @t: tunnel to be added
  157. **/
  158. static void
  159. ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
  160. {
  161. struct ip6_tnl **tp = ip6_tnl_bucket(ip6n, &t->parms);
  162. t->next = *tp;
  163. write_lock_bh(&ip6_tnl_lock);
  164. *tp = t;
  165. write_unlock_bh(&ip6_tnl_lock);
  166. }
  167. /**
  168. * ip6_tnl_unlink - remove tunnel from hash table
  169. * @t: tunnel to be removed
  170. **/
  171. static void
  172. ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
  173. {
  174. struct ip6_tnl **tp;
  175. for (tp = ip6_tnl_bucket(ip6n, &t->parms); *tp; tp = &(*tp)->next) {
  176. if (t == *tp) {
  177. write_lock_bh(&ip6_tnl_lock);
  178. *tp = t->next;
  179. write_unlock_bh(&ip6_tnl_lock);
  180. break;
  181. }
  182. }
  183. }
  184. /**
  185. * ip6_tnl_create() - create a new tunnel
  186. * @p: tunnel parameters
  187. * @pt: pointer to new tunnel
  188. *
  189. * Description:
  190. * Create tunnel matching given parameters.
  191. *
  192. * Return:
  193. * created tunnel or NULL
  194. **/
  195. static struct ip6_tnl *ip6_tnl_create(struct net *net, struct ip6_tnl_parm *p)
  196. {
  197. struct net_device *dev;
  198. struct ip6_tnl *t;
  199. char name[IFNAMSIZ];
  200. int err;
  201. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  202. if (p->name[0])
  203. strlcpy(name, p->name, IFNAMSIZ);
  204. else
  205. sprintf(name, "ip6tnl%%d");
  206. dev = alloc_netdev(sizeof (*t), name, ip6_tnl_dev_setup);
  207. if (dev == NULL)
  208. goto failed;
  209. dev_net_set(dev, net);
  210. if (strchr(name, '%')) {
  211. if (dev_alloc_name(dev, name) < 0)
  212. goto failed_free;
  213. }
  214. t = netdev_priv(dev);
  215. t->parms = *p;
  216. ip6_tnl_dev_init(dev);
  217. if ((err = register_netdevice(dev)) < 0)
  218. goto failed_free;
  219. dev_hold(dev);
  220. ip6_tnl_link(ip6n, t);
  221. return t;
  222. failed_free:
  223. free_netdev(dev);
  224. failed:
  225. return NULL;
  226. }
  227. /**
  228. * ip6_tnl_locate - find or create tunnel matching given parameters
  229. * @p: tunnel parameters
  230. * @create: != 0 if allowed to create new tunnel if no match found
  231. *
  232. * Description:
  233. * ip6_tnl_locate() first tries to locate an existing tunnel
  234. * based on @parms. If this is unsuccessful, but @create is set a new
  235. * tunnel device is created and registered for use.
  236. *
  237. * Return:
  238. * matching tunnel or NULL
  239. **/
  240. static struct ip6_tnl *ip6_tnl_locate(struct net *net,
  241. struct ip6_tnl_parm *p, int create)
  242. {
  243. struct in6_addr *remote = &p->raddr;
  244. struct in6_addr *local = &p->laddr;
  245. struct ip6_tnl *t;
  246. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  247. for (t = *ip6_tnl_bucket(ip6n, p); t; t = t->next) {
  248. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  249. ipv6_addr_equal(remote, &t->parms.raddr))
  250. return t;
  251. }
  252. if (!create)
  253. return NULL;
  254. return ip6_tnl_create(net, p);
  255. }
  256. /**
  257. * ip6_tnl_dev_uninit - tunnel device uninitializer
  258. * @dev: the device to be destroyed
  259. *
  260. * Description:
  261. * ip6_tnl_dev_uninit() removes tunnel from its list
  262. **/
  263. static void
  264. ip6_tnl_dev_uninit(struct net_device *dev)
  265. {
  266. struct ip6_tnl *t = netdev_priv(dev);
  267. struct net *net = dev_net(dev);
  268. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  269. if (dev == ip6n->fb_tnl_dev) {
  270. write_lock_bh(&ip6_tnl_lock);
  271. ip6n->tnls_wc[0] = NULL;
  272. write_unlock_bh(&ip6_tnl_lock);
  273. } else {
  274. ip6_tnl_unlink(ip6n, t);
  275. }
  276. ip6_tnl_dst_reset(t);
  277. dev_put(dev);
  278. }
  279. /**
  280. * parse_tvl_tnl_enc_lim - handle encapsulation limit option
  281. * @skb: received socket buffer
  282. *
  283. * Return:
  284. * 0 if none was found,
  285. * else index to encapsulation limit
  286. **/
  287. static __u16
  288. parse_tlv_tnl_enc_lim(struct sk_buff *skb, __u8 * raw)
  289. {
  290. struct ipv6hdr *ipv6h = (struct ipv6hdr *) raw;
  291. __u8 nexthdr = ipv6h->nexthdr;
  292. __u16 off = sizeof (*ipv6h);
  293. while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
  294. __u16 optlen = 0;
  295. struct ipv6_opt_hdr *hdr;
  296. if (raw + off + sizeof (*hdr) > skb->data &&
  297. !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
  298. break;
  299. hdr = (struct ipv6_opt_hdr *) (raw + off);
  300. if (nexthdr == NEXTHDR_FRAGMENT) {
  301. struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
  302. if (frag_hdr->frag_off)
  303. break;
  304. optlen = 8;
  305. } else if (nexthdr == NEXTHDR_AUTH) {
  306. optlen = (hdr->hdrlen + 2) << 2;
  307. } else {
  308. optlen = ipv6_optlen(hdr);
  309. }
  310. if (nexthdr == NEXTHDR_DEST) {
  311. __u16 i = off + 2;
  312. while (1) {
  313. struct ipv6_tlv_tnl_enc_lim *tel;
  314. /* No more room for encapsulation limit */
  315. if (i + sizeof (*tel) > off + optlen)
  316. break;
  317. tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
  318. /* return index of option if found and valid */
  319. if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
  320. tel->length == 1)
  321. return i;
  322. /* else jump to next option */
  323. if (tel->type)
  324. i += tel->length + 2;
  325. else
  326. i++;
  327. }
  328. }
  329. nexthdr = hdr->nexthdr;
  330. off += optlen;
  331. }
  332. return 0;
  333. }
  334. /**
  335. * ip6_tnl_err - tunnel error handler
  336. *
  337. * Description:
  338. * ip6_tnl_err() should handle errors in the tunnel according
  339. * to the specifications in RFC 2473.
  340. **/
  341. static int
  342. ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
  343. u8 *type, u8 *code, int *msg, __u32 *info, int offset)
  344. {
  345. struct ipv6hdr *ipv6h = (struct ipv6hdr *) skb->data;
  346. struct ip6_tnl *t;
  347. int rel_msg = 0;
  348. u8 rel_type = ICMPV6_DEST_UNREACH;
  349. u8 rel_code = ICMPV6_ADDR_UNREACH;
  350. __u32 rel_info = 0;
  351. __u16 len;
  352. int err = -ENOENT;
  353. /* If the packet doesn't contain the original IPv6 header we are
  354. in trouble since we might need the source address for further
  355. processing of the error. */
  356. read_lock(&ip6_tnl_lock);
  357. if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr,
  358. &ipv6h->saddr)) == NULL)
  359. goto out;
  360. if (t->parms.proto != ipproto && t->parms.proto != 0)
  361. goto out;
  362. err = 0;
  363. switch (*type) {
  364. __u32 teli;
  365. struct ipv6_tlv_tnl_enc_lim *tel;
  366. __u32 mtu;
  367. case ICMPV6_DEST_UNREACH:
  368. if (net_ratelimit())
  369. printk(KERN_WARNING
  370. "%s: Path to destination invalid "
  371. "or inactive!\n", t->parms.name);
  372. rel_msg = 1;
  373. break;
  374. case ICMPV6_TIME_EXCEED:
  375. if ((*code) == ICMPV6_EXC_HOPLIMIT) {
  376. if (net_ratelimit())
  377. printk(KERN_WARNING
  378. "%s: Too small hop limit or "
  379. "routing loop in tunnel!\n",
  380. t->parms.name);
  381. rel_msg = 1;
  382. }
  383. break;
  384. case ICMPV6_PARAMPROB:
  385. teli = 0;
  386. if ((*code) == ICMPV6_HDR_FIELD)
  387. teli = parse_tlv_tnl_enc_lim(skb, skb->data);
  388. if (teli && teli == *info - 2) {
  389. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
  390. if (tel->encap_limit == 0) {
  391. if (net_ratelimit())
  392. printk(KERN_WARNING
  393. "%s: Too small encapsulation "
  394. "limit or routing loop in "
  395. "tunnel!\n", t->parms.name);
  396. rel_msg = 1;
  397. }
  398. } else if (net_ratelimit()) {
  399. printk(KERN_WARNING
  400. "%s: Recipient unable to parse tunneled "
  401. "packet!\n ", t->parms.name);
  402. }
  403. break;
  404. case ICMPV6_PKT_TOOBIG:
  405. mtu = *info - offset;
  406. if (mtu < IPV6_MIN_MTU)
  407. mtu = IPV6_MIN_MTU;
  408. t->dev->mtu = mtu;
  409. if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {
  410. rel_type = ICMPV6_PKT_TOOBIG;
  411. rel_code = 0;
  412. rel_info = mtu;
  413. rel_msg = 1;
  414. }
  415. break;
  416. }
  417. *type = rel_type;
  418. *code = rel_code;
  419. *info = rel_info;
  420. *msg = rel_msg;
  421. out:
  422. read_unlock(&ip6_tnl_lock);
  423. return err;
  424. }
  425. static int
  426. ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  427. u8 type, u8 code, int offset, __be32 info)
  428. {
  429. int rel_msg = 0;
  430. u8 rel_type = type;
  431. u8 rel_code = code;
  432. __u32 rel_info = ntohl(info);
  433. int err;
  434. struct sk_buff *skb2;
  435. struct iphdr *eiph;
  436. struct flowi fl;
  437. struct rtable *rt;
  438. err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
  439. &rel_msg, &rel_info, offset);
  440. if (err < 0)
  441. return err;
  442. if (rel_msg == 0)
  443. return 0;
  444. switch (rel_type) {
  445. case ICMPV6_DEST_UNREACH:
  446. if (rel_code != ICMPV6_ADDR_UNREACH)
  447. return 0;
  448. rel_type = ICMP_DEST_UNREACH;
  449. rel_code = ICMP_HOST_UNREACH;
  450. break;
  451. case ICMPV6_PKT_TOOBIG:
  452. if (rel_code != 0)
  453. return 0;
  454. rel_type = ICMP_DEST_UNREACH;
  455. rel_code = ICMP_FRAG_NEEDED;
  456. break;
  457. default:
  458. return 0;
  459. }
  460. if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
  461. return 0;
  462. skb2 = skb_clone(skb, GFP_ATOMIC);
  463. if (!skb2)
  464. return 0;
  465. skb_dst_drop(skb2);
  466. skb_pull(skb2, offset);
  467. skb_reset_network_header(skb2);
  468. eiph = ip_hdr(skb2);
  469. /* Try to guess incoming interface */
  470. memset(&fl, 0, sizeof(fl));
  471. fl.fl4_dst = eiph->saddr;
  472. fl.fl4_tos = RT_TOS(eiph->tos);
  473. fl.proto = IPPROTO_IPIP;
  474. if (ip_route_output_key(dev_net(skb->dev), &rt, &fl))
  475. goto out;
  476. skb2->dev = rt->u.dst.dev;
  477. /* route "incoming" packet */
  478. if (rt->rt_flags & RTCF_LOCAL) {
  479. ip_rt_put(rt);
  480. rt = NULL;
  481. fl.fl4_dst = eiph->daddr;
  482. fl.fl4_src = eiph->saddr;
  483. fl.fl4_tos = eiph->tos;
  484. if (ip_route_output_key(dev_net(skb->dev), &rt, &fl) ||
  485. rt->u.dst.dev->type != ARPHRD_TUNNEL) {
  486. ip_rt_put(rt);
  487. goto out;
  488. }
  489. skb_dst_set(skb2, (struct dst_entry *)rt);
  490. } else {
  491. ip_rt_put(rt);
  492. if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
  493. skb2->dev) ||
  494. skb_dst(skb2)->dev->type != ARPHRD_TUNNEL)
  495. goto out;
  496. }
  497. /* change mtu on this route */
  498. if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
  499. if (rel_info > dst_mtu(skb_dst(skb2)))
  500. goto out;
  501. skb_dst(skb2)->ops->update_pmtu(skb_dst(skb2), rel_info);
  502. }
  503. icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
  504. out:
  505. kfree_skb(skb2);
  506. return 0;
  507. }
  508. static int
  509. ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  510. u8 type, u8 code, int offset, __be32 info)
  511. {
  512. int rel_msg = 0;
  513. u8 rel_type = type;
  514. u8 rel_code = code;
  515. __u32 rel_info = ntohl(info);
  516. int err;
  517. err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
  518. &rel_msg, &rel_info, offset);
  519. if (err < 0)
  520. return err;
  521. if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
  522. struct rt6_info *rt;
  523. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  524. if (!skb2)
  525. return 0;
  526. skb_dst_drop(skb2);
  527. skb_pull(skb2, offset);
  528. skb_reset_network_header(skb2);
  529. /* Try to guess incoming interface */
  530. rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr,
  531. NULL, 0, 0);
  532. if (rt && rt->rt6i_dev)
  533. skb2->dev = rt->rt6i_dev;
  534. icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
  535. if (rt)
  536. dst_release(&rt->u.dst);
  537. kfree_skb(skb2);
  538. }
  539. return 0;
  540. }
  541. static void ip4ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
  542. struct ipv6hdr *ipv6h,
  543. struct sk_buff *skb)
  544. {
  545. __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
  546. if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
  547. ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
  548. if (INET_ECN_is_ce(dsfield))
  549. IP_ECN_set_ce(ip_hdr(skb));
  550. }
  551. static void ip6ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
  552. struct ipv6hdr *ipv6h,
  553. struct sk_buff *skb)
  554. {
  555. if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
  556. ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
  557. if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6h)))
  558. IP6_ECN_set_ce(ipv6_hdr(skb));
  559. }
  560. static inline int ip6_tnl_rcv_ctl(struct ip6_tnl *t)
  561. {
  562. struct ip6_tnl_parm *p = &t->parms;
  563. int ret = 0;
  564. struct net *net = dev_net(t->dev);
  565. if (p->flags & IP6_TNL_F_CAP_RCV) {
  566. struct net_device *ldev = NULL;
  567. if (p->link)
  568. ldev = dev_get_by_index(net, p->link);
  569. if ((ipv6_addr_is_multicast(&p->laddr) ||
  570. likely(ipv6_chk_addr(net, &p->laddr, ldev, 0))) &&
  571. likely(!ipv6_chk_addr(net, &p->raddr, NULL, 0)))
  572. ret = 1;
  573. if (ldev)
  574. dev_put(ldev);
  575. }
  576. return ret;
  577. }
  578. /**
  579. * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
  580. * @skb: received socket buffer
  581. * @protocol: ethernet protocol ID
  582. * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
  583. *
  584. * Return: 0
  585. **/
  586. static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
  587. __u8 ipproto,
  588. void (*dscp_ecn_decapsulate)(struct ip6_tnl *t,
  589. struct ipv6hdr *ipv6h,
  590. struct sk_buff *skb))
  591. {
  592. struct ip6_tnl *t;
  593. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  594. read_lock(&ip6_tnl_lock);
  595. if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr,
  596. &ipv6h->daddr)) != NULL) {
  597. if (t->parms.proto != ipproto && t->parms.proto != 0) {
  598. read_unlock(&ip6_tnl_lock);
  599. goto discard;
  600. }
  601. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  602. read_unlock(&ip6_tnl_lock);
  603. goto discard;
  604. }
  605. if (!ip6_tnl_rcv_ctl(t)) {
  606. t->dev->stats.rx_dropped++;
  607. read_unlock(&ip6_tnl_lock);
  608. goto discard;
  609. }
  610. secpath_reset(skb);
  611. skb->mac_header = skb->network_header;
  612. skb_reset_network_header(skb);
  613. skb->protocol = htons(protocol);
  614. skb->pkt_type = PACKET_HOST;
  615. memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
  616. skb->dev = t->dev;
  617. skb_dst_drop(skb);
  618. nf_reset(skb);
  619. dscp_ecn_decapsulate(t, ipv6h, skb);
  620. t->dev->stats.rx_packets++;
  621. t->dev->stats.rx_bytes += skb->len;
  622. netif_rx(skb);
  623. read_unlock(&ip6_tnl_lock);
  624. return 0;
  625. }
  626. read_unlock(&ip6_tnl_lock);
  627. return 1;
  628. discard:
  629. kfree_skb(skb);
  630. return 0;
  631. }
  632. static int ip4ip6_rcv(struct sk_buff *skb)
  633. {
  634. return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
  635. ip4ip6_dscp_ecn_decapsulate);
  636. }
  637. static int ip6ip6_rcv(struct sk_buff *skb)
  638. {
  639. return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
  640. ip6ip6_dscp_ecn_decapsulate);
  641. }
  642. struct ipv6_tel_txoption {
  643. struct ipv6_txoptions ops;
  644. __u8 dst_opt[8];
  645. };
  646. static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
  647. {
  648. memset(opt, 0, sizeof(struct ipv6_tel_txoption));
  649. opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
  650. opt->dst_opt[3] = 1;
  651. opt->dst_opt[4] = encap_limit;
  652. opt->dst_opt[5] = IPV6_TLV_PADN;
  653. opt->dst_opt[6] = 1;
  654. opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
  655. opt->ops.opt_nflen = 8;
  656. }
  657. /**
  658. * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  659. * @t: the outgoing tunnel device
  660. * @hdr: IPv6 header from the incoming packet
  661. *
  662. * Description:
  663. * Avoid trivial tunneling loop by checking that tunnel exit-point
  664. * doesn't match source of incoming packet.
  665. *
  666. * Return:
  667. * 1 if conflict,
  668. * 0 else
  669. **/
  670. static inline int
  671. ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
  672. {
  673. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  674. }
  675. static inline int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
  676. {
  677. struct ip6_tnl_parm *p = &t->parms;
  678. int ret = 0;
  679. struct net *net = dev_net(t->dev);
  680. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  681. struct net_device *ldev = NULL;
  682. if (p->link)
  683. ldev = dev_get_by_index(net, p->link);
  684. if (unlikely(!ipv6_chk_addr(net, &p->laddr, ldev, 0)))
  685. printk(KERN_WARNING
  686. "%s xmit: Local address not yet configured!\n",
  687. p->name);
  688. else if (!ipv6_addr_is_multicast(&p->raddr) &&
  689. unlikely(ipv6_chk_addr(net, &p->raddr, NULL, 0)))
  690. printk(KERN_WARNING
  691. "%s xmit: Routing loop! "
  692. "Remote address found on this node!\n",
  693. p->name);
  694. else
  695. ret = 1;
  696. if (ldev)
  697. dev_put(ldev);
  698. }
  699. return ret;
  700. }
  701. /**
  702. * ip6_tnl_xmit2 - encapsulate packet and send
  703. * @skb: the outgoing socket buffer
  704. * @dev: the outgoing tunnel device
  705. * @dsfield: dscp code for outer header
  706. * @fl: flow of tunneled packet
  707. * @encap_limit: encapsulation limit
  708. * @pmtu: Path MTU is stored if packet is too big
  709. *
  710. * Description:
  711. * Build new header and do some sanity checks on the packet before sending
  712. * it.
  713. *
  714. * Return:
  715. * 0 on success
  716. * -1 fail
  717. * %-EMSGSIZE message too big. return mtu in this case.
  718. **/
  719. static int ip6_tnl_xmit2(struct sk_buff *skb,
  720. struct net_device *dev,
  721. __u8 dsfield,
  722. struct flowi *fl,
  723. int encap_limit,
  724. __u32 *pmtu)
  725. {
  726. struct net *net = dev_net(dev);
  727. struct ip6_tnl *t = netdev_priv(dev);
  728. struct net_device_stats *stats = &t->dev->stats;
  729. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  730. struct ipv6_tel_txoption opt;
  731. struct dst_entry *dst;
  732. struct net_device *tdev;
  733. int mtu;
  734. unsigned int max_headroom = sizeof(struct ipv6hdr);
  735. u8 proto;
  736. int err = -1;
  737. int pkt_len;
  738. if ((dst = ip6_tnl_dst_check(t)) != NULL)
  739. dst_hold(dst);
  740. else {
  741. dst = ip6_route_output(net, NULL, fl);
  742. if (dst->error || xfrm_lookup(net, &dst, fl, NULL, 0) < 0)
  743. goto tx_err_link_failure;
  744. }
  745. tdev = dst->dev;
  746. if (tdev == dev) {
  747. stats->collisions++;
  748. if (net_ratelimit())
  749. printk(KERN_WARNING
  750. "%s: Local routing loop detected!\n",
  751. t->parms.name);
  752. goto tx_err_dst_release;
  753. }
  754. mtu = dst_mtu(dst) - sizeof (*ipv6h);
  755. if (encap_limit >= 0) {
  756. max_headroom += 8;
  757. mtu -= 8;
  758. }
  759. if (mtu < IPV6_MIN_MTU)
  760. mtu = IPV6_MIN_MTU;
  761. if (skb_dst(skb))
  762. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
  763. if (skb->len > mtu) {
  764. *pmtu = mtu;
  765. err = -EMSGSIZE;
  766. goto tx_err_dst_release;
  767. }
  768. /*
  769. * Okay, now see if we can stuff it in the buffer as-is.
  770. */
  771. max_headroom += LL_RESERVED_SPACE(tdev);
  772. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  773. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  774. struct sk_buff *new_skb;
  775. if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
  776. goto tx_err_dst_release;
  777. if (skb->sk)
  778. skb_set_owner_w(new_skb, skb->sk);
  779. kfree_skb(skb);
  780. skb = new_skb;
  781. }
  782. skb_dst_drop(skb);
  783. skb_dst_set(skb, dst_clone(dst));
  784. skb->transport_header = skb->network_header;
  785. proto = fl->proto;
  786. if (encap_limit >= 0) {
  787. init_tel_txopt(&opt, encap_limit);
  788. ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
  789. }
  790. skb_push(skb, sizeof(struct ipv6hdr));
  791. skb_reset_network_header(skb);
  792. ipv6h = ipv6_hdr(skb);
  793. *(__be32*)ipv6h = fl->fl6_flowlabel | htonl(0x60000000);
  794. dsfield = INET_ECN_encapsulate(0, dsfield);
  795. ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
  796. ipv6h->hop_limit = t->parms.hop_limit;
  797. ipv6h->nexthdr = proto;
  798. ipv6_addr_copy(&ipv6h->saddr, &fl->fl6_src);
  799. ipv6_addr_copy(&ipv6h->daddr, &fl->fl6_dst);
  800. nf_reset(skb);
  801. pkt_len = skb->len;
  802. err = ip6_local_out(skb);
  803. if (net_xmit_eval(err) == 0) {
  804. stats->tx_bytes += pkt_len;
  805. stats->tx_packets++;
  806. } else {
  807. stats->tx_errors++;
  808. stats->tx_aborted_errors++;
  809. }
  810. ip6_tnl_dst_store(t, dst);
  811. return 0;
  812. tx_err_link_failure:
  813. stats->tx_carrier_errors++;
  814. dst_link_failure(skb);
  815. tx_err_dst_release:
  816. dst_release(dst);
  817. return err;
  818. }
  819. static inline int
  820. ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  821. {
  822. struct ip6_tnl *t = netdev_priv(dev);
  823. struct iphdr *iph = ip_hdr(skb);
  824. int encap_limit = -1;
  825. struct flowi fl;
  826. __u8 dsfield;
  827. __u32 mtu;
  828. int err;
  829. if ((t->parms.proto != IPPROTO_IPIP && t->parms.proto != 0) ||
  830. !ip6_tnl_xmit_ctl(t))
  831. return -1;
  832. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  833. encap_limit = t->parms.encap_limit;
  834. memcpy(&fl, &t->fl, sizeof (fl));
  835. fl.proto = IPPROTO_IPIP;
  836. dsfield = ipv4_get_dsfield(iph);
  837. if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
  838. fl.fl6_flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
  839. & IPV6_TCLASS_MASK;
  840. err = ip6_tnl_xmit2(skb, dev, dsfield, &fl, encap_limit, &mtu);
  841. if (err != 0) {
  842. /* XXX: send ICMP error even if DF is not set. */
  843. if (err == -EMSGSIZE)
  844. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  845. htonl(mtu));
  846. return -1;
  847. }
  848. return 0;
  849. }
  850. static inline int
  851. ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  852. {
  853. struct ip6_tnl *t = netdev_priv(dev);
  854. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  855. int encap_limit = -1;
  856. __u16 offset;
  857. struct flowi fl;
  858. __u8 dsfield;
  859. __u32 mtu;
  860. int err;
  861. if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
  862. !ip6_tnl_xmit_ctl(t) || ip6_tnl_addr_conflict(t, ipv6h))
  863. return -1;
  864. offset = parse_tlv_tnl_enc_lim(skb, skb_network_header(skb));
  865. if (offset > 0) {
  866. struct ipv6_tlv_tnl_enc_lim *tel;
  867. tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
  868. if (tel->encap_limit == 0) {
  869. icmpv6_send(skb, ICMPV6_PARAMPROB,
  870. ICMPV6_HDR_FIELD, offset + 2, skb->dev);
  871. return -1;
  872. }
  873. encap_limit = tel->encap_limit - 1;
  874. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  875. encap_limit = t->parms.encap_limit;
  876. memcpy(&fl, &t->fl, sizeof (fl));
  877. fl.proto = IPPROTO_IPV6;
  878. dsfield = ipv6_get_dsfield(ipv6h);
  879. if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
  880. fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
  881. if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
  882. fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
  883. err = ip6_tnl_xmit2(skb, dev, dsfield, &fl, encap_limit, &mtu);
  884. if (err != 0) {
  885. if (err == -EMSGSIZE)
  886. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
  887. return -1;
  888. }
  889. return 0;
  890. }
  891. static int
  892. ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  893. {
  894. struct ip6_tnl *t = netdev_priv(dev);
  895. struct net_device_stats *stats = &t->dev->stats;
  896. int ret;
  897. if (t->recursion++) {
  898. stats->collisions++;
  899. goto tx_err;
  900. }
  901. switch (skb->protocol) {
  902. case htons(ETH_P_IP):
  903. ret = ip4ip6_tnl_xmit(skb, dev);
  904. break;
  905. case htons(ETH_P_IPV6):
  906. ret = ip6ip6_tnl_xmit(skb, dev);
  907. break;
  908. default:
  909. goto tx_err;
  910. }
  911. if (ret < 0)
  912. goto tx_err;
  913. t->recursion--;
  914. return 0;
  915. tx_err:
  916. stats->tx_errors++;
  917. stats->tx_dropped++;
  918. kfree_skb(skb);
  919. t->recursion--;
  920. return 0;
  921. }
  922. static void ip6_tnl_set_cap(struct ip6_tnl *t)
  923. {
  924. struct ip6_tnl_parm *p = &t->parms;
  925. int ltype = ipv6_addr_type(&p->laddr);
  926. int rtype = ipv6_addr_type(&p->raddr);
  927. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV);
  928. if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
  929. rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
  930. !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
  931. (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
  932. if (ltype&IPV6_ADDR_UNICAST)
  933. p->flags |= IP6_TNL_F_CAP_XMIT;
  934. if (rtype&IPV6_ADDR_UNICAST)
  935. p->flags |= IP6_TNL_F_CAP_RCV;
  936. }
  937. }
  938. static void ip6_tnl_link_config(struct ip6_tnl *t)
  939. {
  940. struct net_device *dev = t->dev;
  941. struct ip6_tnl_parm *p = &t->parms;
  942. struct flowi *fl = &t->fl;
  943. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  944. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  945. /* Set up flowi template */
  946. ipv6_addr_copy(&fl->fl6_src, &p->laddr);
  947. ipv6_addr_copy(&fl->fl6_dst, &p->raddr);
  948. fl->oif = p->link;
  949. fl->fl6_flowlabel = 0;
  950. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  951. fl->fl6_flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  952. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  953. fl->fl6_flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  954. ip6_tnl_set_cap(t);
  955. if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
  956. dev->flags |= IFF_POINTOPOINT;
  957. else
  958. dev->flags &= ~IFF_POINTOPOINT;
  959. dev->iflink = p->link;
  960. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  961. int strict = (ipv6_addr_type(&p->raddr) &
  962. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  963. struct rt6_info *rt = rt6_lookup(dev_net(dev),
  964. &p->raddr, &p->laddr,
  965. p->link, strict);
  966. if (rt == NULL)
  967. return;
  968. if (rt->rt6i_dev) {
  969. dev->hard_header_len = rt->rt6i_dev->hard_header_len +
  970. sizeof (struct ipv6hdr);
  971. dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr);
  972. if (dev->mtu < IPV6_MIN_MTU)
  973. dev->mtu = IPV6_MIN_MTU;
  974. }
  975. dst_release(&rt->u.dst);
  976. }
  977. }
  978. /**
  979. * ip6_tnl_change - update the tunnel parameters
  980. * @t: tunnel to be changed
  981. * @p: tunnel configuration parameters
  982. *
  983. * Description:
  984. * ip6_tnl_change() updates the tunnel parameters
  985. **/
  986. static int
  987. ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
  988. {
  989. ipv6_addr_copy(&t->parms.laddr, &p->laddr);
  990. ipv6_addr_copy(&t->parms.raddr, &p->raddr);
  991. t->parms.flags = p->flags;
  992. t->parms.hop_limit = p->hop_limit;
  993. t->parms.encap_limit = p->encap_limit;
  994. t->parms.flowinfo = p->flowinfo;
  995. t->parms.link = p->link;
  996. t->parms.proto = p->proto;
  997. ip6_tnl_dst_reset(t);
  998. ip6_tnl_link_config(t);
  999. return 0;
  1000. }
  1001. /**
  1002. * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
  1003. * @dev: virtual device associated with tunnel
  1004. * @ifr: parameters passed from userspace
  1005. * @cmd: command to be performed
  1006. *
  1007. * Description:
  1008. * ip6_tnl_ioctl() is used for managing IPv6 tunnels
  1009. * from userspace.
  1010. *
  1011. * The possible commands are the following:
  1012. * %SIOCGETTUNNEL: get tunnel parameters for device
  1013. * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
  1014. * %SIOCCHGTUNNEL: change tunnel parameters to those given
  1015. * %SIOCDELTUNNEL: delete tunnel
  1016. *
  1017. * The fallback device "ip6tnl0", created during module
  1018. * initialization, can be used for creating other tunnel devices.
  1019. *
  1020. * Return:
  1021. * 0 on success,
  1022. * %-EFAULT if unable to copy data to or from userspace,
  1023. * %-EPERM if current process hasn't %CAP_NET_ADMIN set
  1024. * %-EINVAL if passed tunnel parameters are invalid,
  1025. * %-EEXIST if changing a tunnel's parameters would cause a conflict
  1026. * %-ENODEV if attempting to change or delete a nonexisting device
  1027. **/
  1028. static int
  1029. ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1030. {
  1031. int err = 0;
  1032. struct ip6_tnl_parm p;
  1033. struct ip6_tnl *t = NULL;
  1034. struct net *net = dev_net(dev);
  1035. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1036. switch (cmd) {
  1037. case SIOCGETTUNNEL:
  1038. if (dev == ip6n->fb_tnl_dev) {
  1039. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
  1040. err = -EFAULT;
  1041. break;
  1042. }
  1043. t = ip6_tnl_locate(net, &p, 0);
  1044. }
  1045. if (t == NULL)
  1046. t = netdev_priv(dev);
  1047. memcpy(&p, &t->parms, sizeof (p));
  1048. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
  1049. err = -EFAULT;
  1050. }
  1051. break;
  1052. case SIOCADDTUNNEL:
  1053. case SIOCCHGTUNNEL:
  1054. err = -EPERM;
  1055. if (!capable(CAP_NET_ADMIN))
  1056. break;
  1057. err = -EFAULT;
  1058. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
  1059. break;
  1060. err = -EINVAL;
  1061. if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
  1062. p.proto != 0)
  1063. break;
  1064. t = ip6_tnl_locate(net, &p, cmd == SIOCADDTUNNEL);
  1065. if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
  1066. if (t != NULL) {
  1067. if (t->dev != dev) {
  1068. err = -EEXIST;
  1069. break;
  1070. }
  1071. } else
  1072. t = netdev_priv(dev);
  1073. ip6_tnl_unlink(ip6n, t);
  1074. err = ip6_tnl_change(t, &p);
  1075. ip6_tnl_link(ip6n, t);
  1076. netdev_state_change(dev);
  1077. }
  1078. if (t) {
  1079. err = 0;
  1080. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof (p)))
  1081. err = -EFAULT;
  1082. } else
  1083. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  1084. break;
  1085. case SIOCDELTUNNEL:
  1086. err = -EPERM;
  1087. if (!capable(CAP_NET_ADMIN))
  1088. break;
  1089. if (dev == ip6n->fb_tnl_dev) {
  1090. err = -EFAULT;
  1091. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
  1092. break;
  1093. err = -ENOENT;
  1094. if ((t = ip6_tnl_locate(net, &p, 0)) == NULL)
  1095. break;
  1096. err = -EPERM;
  1097. if (t->dev == ip6n->fb_tnl_dev)
  1098. break;
  1099. dev = t->dev;
  1100. }
  1101. err = 0;
  1102. unregister_netdevice(dev);
  1103. break;
  1104. default:
  1105. err = -EINVAL;
  1106. }
  1107. return err;
  1108. }
  1109. /**
  1110. * ip6_tnl_change_mtu - change mtu manually for tunnel device
  1111. * @dev: virtual device associated with tunnel
  1112. * @new_mtu: the new mtu
  1113. *
  1114. * Return:
  1115. * 0 on success,
  1116. * %-EINVAL if mtu too small
  1117. **/
  1118. static int
  1119. ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
  1120. {
  1121. if (new_mtu < IPV6_MIN_MTU) {
  1122. return -EINVAL;
  1123. }
  1124. dev->mtu = new_mtu;
  1125. return 0;
  1126. }
  1127. static const struct net_device_ops ip6_tnl_netdev_ops = {
  1128. .ndo_uninit = ip6_tnl_dev_uninit,
  1129. .ndo_start_xmit = ip6_tnl_xmit,
  1130. .ndo_do_ioctl = ip6_tnl_ioctl,
  1131. .ndo_change_mtu = ip6_tnl_change_mtu,
  1132. };
  1133. /**
  1134. * ip6_tnl_dev_setup - setup virtual tunnel device
  1135. * @dev: virtual device associated with tunnel
  1136. *
  1137. * Description:
  1138. * Initialize function pointers and device parameters
  1139. **/
  1140. static void ip6_tnl_dev_setup(struct net_device *dev)
  1141. {
  1142. dev->netdev_ops = &ip6_tnl_netdev_ops;
  1143. dev->destructor = free_netdev;
  1144. dev->type = ARPHRD_TUNNEL6;
  1145. dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
  1146. dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
  1147. dev->flags |= IFF_NOARP;
  1148. dev->addr_len = sizeof(struct in6_addr);
  1149. dev->features |= NETIF_F_NETNS_LOCAL;
  1150. }
  1151. /**
  1152. * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
  1153. * @dev: virtual device associated with tunnel
  1154. **/
  1155. static inline void
  1156. ip6_tnl_dev_init_gen(struct net_device *dev)
  1157. {
  1158. struct ip6_tnl *t = netdev_priv(dev);
  1159. t->dev = dev;
  1160. strcpy(t->parms.name, dev->name);
  1161. }
  1162. /**
  1163. * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
  1164. * @dev: virtual device associated with tunnel
  1165. **/
  1166. static void ip6_tnl_dev_init(struct net_device *dev)
  1167. {
  1168. struct ip6_tnl *t = netdev_priv(dev);
  1169. ip6_tnl_dev_init_gen(dev);
  1170. ip6_tnl_link_config(t);
  1171. }
  1172. /**
  1173. * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
  1174. * @dev: fallback device
  1175. *
  1176. * Return: 0
  1177. **/
  1178. static void ip6_fb_tnl_dev_init(struct net_device *dev)
  1179. {
  1180. struct ip6_tnl *t = netdev_priv(dev);
  1181. struct net *net = dev_net(dev);
  1182. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1183. ip6_tnl_dev_init_gen(dev);
  1184. t->parms.proto = IPPROTO_IPV6;
  1185. dev_hold(dev);
  1186. ip6n->tnls_wc[0] = t;
  1187. }
  1188. static struct xfrm6_tunnel ip4ip6_handler = {
  1189. .handler = ip4ip6_rcv,
  1190. .err_handler = ip4ip6_err,
  1191. .priority = 1,
  1192. };
  1193. static struct xfrm6_tunnel ip6ip6_handler = {
  1194. .handler = ip6ip6_rcv,
  1195. .err_handler = ip6ip6_err,
  1196. .priority = 1,
  1197. };
  1198. static void ip6_tnl_destroy_tunnels(struct ip6_tnl_net *ip6n)
  1199. {
  1200. int h;
  1201. struct ip6_tnl *t;
  1202. for (h = 0; h < HASH_SIZE; h++) {
  1203. while ((t = ip6n->tnls_r_l[h]) != NULL)
  1204. unregister_netdevice(t->dev);
  1205. }
  1206. t = ip6n->tnls_wc[0];
  1207. unregister_netdevice(t->dev);
  1208. }
  1209. static int ip6_tnl_init_net(struct net *net)
  1210. {
  1211. int err;
  1212. struct ip6_tnl_net *ip6n;
  1213. err = -ENOMEM;
  1214. ip6n = kzalloc(sizeof(struct ip6_tnl_net), GFP_KERNEL);
  1215. if (ip6n == NULL)
  1216. goto err_alloc;
  1217. err = net_assign_generic(net, ip6_tnl_net_id, ip6n);
  1218. if (err < 0)
  1219. goto err_assign;
  1220. ip6n->tnls[0] = ip6n->tnls_wc;
  1221. ip6n->tnls[1] = ip6n->tnls_r_l;
  1222. err = -ENOMEM;
  1223. ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
  1224. ip6_tnl_dev_setup);
  1225. if (!ip6n->fb_tnl_dev)
  1226. goto err_alloc_dev;
  1227. dev_net_set(ip6n->fb_tnl_dev, net);
  1228. ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
  1229. err = register_netdev(ip6n->fb_tnl_dev);
  1230. if (err < 0)
  1231. goto err_register;
  1232. return 0;
  1233. err_register:
  1234. free_netdev(ip6n->fb_tnl_dev);
  1235. err_alloc_dev:
  1236. /* nothing */
  1237. err_assign:
  1238. kfree(ip6n);
  1239. err_alloc:
  1240. return err;
  1241. }
  1242. static void ip6_tnl_exit_net(struct net *net)
  1243. {
  1244. struct ip6_tnl_net *ip6n;
  1245. ip6n = net_generic(net, ip6_tnl_net_id);
  1246. rtnl_lock();
  1247. ip6_tnl_destroy_tunnels(ip6n);
  1248. rtnl_unlock();
  1249. kfree(ip6n);
  1250. }
  1251. static struct pernet_operations ip6_tnl_net_ops = {
  1252. .init = ip6_tnl_init_net,
  1253. .exit = ip6_tnl_exit_net,
  1254. };
  1255. /**
  1256. * ip6_tunnel_init - register protocol and reserve needed resources
  1257. *
  1258. * Return: 0 on success
  1259. **/
  1260. static int __init ip6_tunnel_init(void)
  1261. {
  1262. int err;
  1263. if (xfrm6_tunnel_register(&ip4ip6_handler, AF_INET)) {
  1264. printk(KERN_ERR "ip6_tunnel init: can't register ip4ip6\n");
  1265. err = -EAGAIN;
  1266. goto out;
  1267. }
  1268. if (xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6)) {
  1269. printk(KERN_ERR "ip6_tunnel init: can't register ip6ip6\n");
  1270. err = -EAGAIN;
  1271. goto unreg_ip4ip6;
  1272. }
  1273. err = register_pernet_gen_device(&ip6_tnl_net_id, &ip6_tnl_net_ops);
  1274. if (err < 0)
  1275. goto err_pernet;
  1276. return 0;
  1277. err_pernet:
  1278. xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
  1279. unreg_ip4ip6:
  1280. xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
  1281. out:
  1282. return err;
  1283. }
  1284. /**
  1285. * ip6_tunnel_cleanup - free resources and unregister protocol
  1286. **/
  1287. static void __exit ip6_tunnel_cleanup(void)
  1288. {
  1289. if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
  1290. printk(KERN_INFO "ip6_tunnel close: can't deregister ip4ip6\n");
  1291. if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
  1292. printk(KERN_INFO "ip6_tunnel close: can't deregister ip6ip6\n");
  1293. unregister_pernet_gen_device(ip6_tnl_net_id, &ip6_tnl_net_ops);
  1294. }
  1295. module_init(ip6_tunnel_init);
  1296. module_exit(ip6_tunnel_cleanup);