ip_vti.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. /*
  2. * Linux NET3: IP/IP protocol decoder modified to support
  3. * virtual tunnel interface
  4. *
  5. * Authors:
  6. * Saurabh Mohan (saurabh.mohan@vyatta.com) 05/07/2012
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. *
  13. */
  14. /*
  15. This version of net/ipv4/ip_vti.c is cloned of net/ipv4/ipip.c
  16. For comments look at net/ipv4/ip_gre.c --ANK
  17. */
  18. #include <linux/capability.h>
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #include <linux/kernel.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/skbuff.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/in.h>
  26. #include <linux/tcp.h>
  27. #include <linux/udp.h>
  28. #include <linux/if_arp.h>
  29. #include <linux/mroute.h>
  30. #include <linux/init.h>
  31. #include <linux/netfilter_ipv4.h>
  32. #include <linux/if_ether.h>
  33. #include <net/sock.h>
  34. #include <net/ip.h>
  35. #include <net/icmp.h>
  36. #include <net/ipip.h>
  37. #include <net/inet_ecn.h>
  38. #include <net/xfrm.h>
  39. #include <net/net_namespace.h>
  40. #include <net/netns/generic.h>
  41. #define HASH_SIZE 16
  42. #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&(HASH_SIZE-1))
  43. static struct rtnl_link_ops vti_link_ops __read_mostly;
  44. static int vti_net_id __read_mostly;
  45. struct vti_net {
  46. struct ip_tunnel __rcu *tunnels_r_l[HASH_SIZE];
  47. struct ip_tunnel __rcu *tunnels_r[HASH_SIZE];
  48. struct ip_tunnel __rcu *tunnels_l[HASH_SIZE];
  49. struct ip_tunnel __rcu *tunnels_wc[1];
  50. struct ip_tunnel __rcu **tunnels[4];
  51. struct net_device *fb_tunnel_dev;
  52. };
  53. static int vti_fb_tunnel_init(struct net_device *dev);
  54. static int vti_tunnel_init(struct net_device *dev);
  55. static void vti_tunnel_setup(struct net_device *dev);
  56. static void vti_dev_free(struct net_device *dev);
  57. static int vti_tunnel_bind_dev(struct net_device *dev);
  58. /* Locking : hash tables are protected by RCU and RTNL */
  59. #define for_each_ip_tunnel_rcu(start) \
  60. for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
  61. /* often modified stats are per cpu, other are shared (netdev->stats) */
  62. struct pcpu_tstats {
  63. u64 rx_packets;
  64. u64 rx_bytes;
  65. u64 tx_packets;
  66. u64 tx_bytes;
  67. struct u64_stats_sync syncp;
  68. };
  69. #define VTI_XMIT(stats1, stats2) do { \
  70. int err; \
  71. int pkt_len = skb->len; \
  72. err = dst_output(skb); \
  73. if (net_xmit_eval(err) == 0) { \
  74. u64_stats_update_begin(&(stats1)->syncp); \
  75. (stats1)->tx_bytes += pkt_len; \
  76. (stats1)->tx_packets++; \
  77. u64_stats_update_end(&(stats1)->syncp); \
  78. } else { \
  79. (stats2)->tx_errors++; \
  80. (stats2)->tx_aborted_errors++; \
  81. } \
  82. } while (0)
  83. static struct rtnl_link_stats64 *vti_get_stats64(struct net_device *dev,
  84. struct rtnl_link_stats64 *tot)
  85. {
  86. int i;
  87. for_each_possible_cpu(i) {
  88. const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i);
  89. u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
  90. unsigned int start;
  91. do {
  92. start = u64_stats_fetch_begin_bh(&tstats->syncp);
  93. rx_packets = tstats->rx_packets;
  94. tx_packets = tstats->tx_packets;
  95. rx_bytes = tstats->rx_bytes;
  96. tx_bytes = tstats->tx_bytes;
  97. } while (u64_stats_fetch_retry_bh(&tstats->syncp, start));
  98. tot->rx_packets += rx_packets;
  99. tot->tx_packets += tx_packets;
  100. tot->rx_bytes += rx_bytes;
  101. tot->tx_bytes += tx_bytes;
  102. }
  103. tot->multicast = dev->stats.multicast;
  104. tot->rx_crc_errors = dev->stats.rx_crc_errors;
  105. tot->rx_fifo_errors = dev->stats.rx_fifo_errors;
  106. tot->rx_length_errors = dev->stats.rx_length_errors;
  107. tot->rx_errors = dev->stats.rx_errors;
  108. tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
  109. tot->tx_carrier_errors = dev->stats.tx_carrier_errors;
  110. tot->tx_dropped = dev->stats.tx_dropped;
  111. tot->tx_aborted_errors = dev->stats.tx_aborted_errors;
  112. tot->tx_errors = dev->stats.tx_errors;
  113. return tot;
  114. }
  115. static struct ip_tunnel *vti_tunnel_lookup(struct net *net,
  116. __be32 remote, __be32 local)
  117. {
  118. unsigned h0 = HASH(remote);
  119. unsigned h1 = HASH(local);
  120. struct ip_tunnel *t;
  121. struct vti_net *ipn = net_generic(net, vti_net_id);
  122. for_each_ip_tunnel_rcu(ipn->tunnels_r_l[h0 ^ h1])
  123. if (local == t->parms.iph.saddr &&
  124. remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
  125. return t;
  126. for_each_ip_tunnel_rcu(ipn->tunnels_r[h0])
  127. if (remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
  128. return t;
  129. for_each_ip_tunnel_rcu(ipn->tunnels_l[h1])
  130. if (local == t->parms.iph.saddr && (t->dev->flags&IFF_UP))
  131. return t;
  132. for_each_ip_tunnel_rcu(ipn->tunnels_wc[0])
  133. if (t && (t->dev->flags&IFF_UP))
  134. return t;
  135. return NULL;
  136. }
  137. static struct ip_tunnel __rcu **__vti_bucket(struct vti_net *ipn,
  138. struct ip_tunnel_parm *parms)
  139. {
  140. __be32 remote = parms->iph.daddr;
  141. __be32 local = parms->iph.saddr;
  142. unsigned h = 0;
  143. int prio = 0;
  144. if (remote) {
  145. prio |= 2;
  146. h ^= HASH(remote);
  147. }
  148. if (local) {
  149. prio |= 1;
  150. h ^= HASH(local);
  151. }
  152. return &ipn->tunnels[prio][h];
  153. }
  154. static inline struct ip_tunnel __rcu **vti_bucket(struct vti_net *ipn,
  155. struct ip_tunnel *t)
  156. {
  157. return __vti_bucket(ipn, &t->parms);
  158. }
  159. static void vti_tunnel_unlink(struct vti_net *ipn, struct ip_tunnel *t)
  160. {
  161. struct ip_tunnel __rcu **tp;
  162. struct ip_tunnel *iter;
  163. for (tp = vti_bucket(ipn, t);
  164. (iter = rtnl_dereference(*tp)) != NULL;
  165. tp = &iter->next) {
  166. if (t == iter) {
  167. rcu_assign_pointer(*tp, t->next);
  168. break;
  169. }
  170. }
  171. }
  172. static void vti_tunnel_link(struct vti_net *ipn, struct ip_tunnel *t)
  173. {
  174. struct ip_tunnel __rcu **tp = vti_bucket(ipn, t);
  175. rcu_assign_pointer(t->next, rtnl_dereference(*tp));
  176. rcu_assign_pointer(*tp, t);
  177. }
  178. static struct ip_tunnel *vti_tunnel_locate(struct net *net,
  179. struct ip_tunnel_parm *parms,
  180. int create)
  181. {
  182. __be32 remote = parms->iph.daddr;
  183. __be32 local = parms->iph.saddr;
  184. struct ip_tunnel *t, *nt;
  185. struct ip_tunnel __rcu **tp;
  186. struct net_device *dev;
  187. char name[IFNAMSIZ];
  188. struct vti_net *ipn = net_generic(net, vti_net_id);
  189. for (tp = __vti_bucket(ipn, parms);
  190. (t = rtnl_dereference(*tp)) != NULL;
  191. tp = &t->next) {
  192. if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr)
  193. return t;
  194. }
  195. if (!create)
  196. return NULL;
  197. if (parms->name[0])
  198. strlcpy(name, parms->name, IFNAMSIZ);
  199. else
  200. strcpy(name, "vti%d");
  201. dev = alloc_netdev(sizeof(*t), name, vti_tunnel_setup);
  202. if (dev == NULL)
  203. return NULL;
  204. dev_net_set(dev, net);
  205. nt = netdev_priv(dev);
  206. nt->parms = *parms;
  207. dev->rtnl_link_ops = &vti_link_ops;
  208. vti_tunnel_bind_dev(dev);
  209. if (register_netdevice(dev) < 0)
  210. goto failed_free;
  211. dev_hold(dev);
  212. vti_tunnel_link(ipn, nt);
  213. return nt;
  214. failed_free:
  215. free_netdev(dev);
  216. return NULL;
  217. }
  218. static void vti_tunnel_uninit(struct net_device *dev)
  219. {
  220. struct net *net = dev_net(dev);
  221. struct vti_net *ipn = net_generic(net, vti_net_id);
  222. vti_tunnel_unlink(ipn, netdev_priv(dev));
  223. dev_put(dev);
  224. }
  225. static int vti_err(struct sk_buff *skb, u32 info)
  226. {
  227. /* All the routers (except for Linux) return only
  228. * 8 bytes of packet payload. It means, that precise relaying of
  229. * ICMP in the real Internet is absolutely infeasible.
  230. */
  231. struct iphdr *iph = (struct iphdr *)skb->data;
  232. const int type = icmp_hdr(skb)->type;
  233. const int code = icmp_hdr(skb)->code;
  234. struct ip_tunnel *t;
  235. int err;
  236. switch (type) {
  237. default:
  238. case ICMP_PARAMETERPROB:
  239. return 0;
  240. case ICMP_DEST_UNREACH:
  241. switch (code) {
  242. case ICMP_SR_FAILED:
  243. case ICMP_PORT_UNREACH:
  244. /* Impossible event. */
  245. return 0;
  246. default:
  247. /* All others are translated to HOST_UNREACH. */
  248. break;
  249. }
  250. break;
  251. case ICMP_TIME_EXCEEDED:
  252. if (code != ICMP_EXC_TTL)
  253. return 0;
  254. break;
  255. }
  256. err = -ENOENT;
  257. t = vti_tunnel_lookup(dev_net(skb->dev), iph->daddr, iph->saddr);
  258. if (t == NULL)
  259. goto out;
  260. if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
  261. ipv4_update_pmtu(skb, dev_net(skb->dev), info,
  262. t->parms.link, 0, IPPROTO_IPIP, 0);
  263. err = 0;
  264. goto out;
  265. }
  266. err = 0;
  267. if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
  268. goto out;
  269. if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
  270. t->err_count++;
  271. else
  272. t->err_count = 1;
  273. t->err_time = jiffies;
  274. out:
  275. return err;
  276. }
  277. /* We dont digest the packet therefore let the packet pass */
  278. static int vti_rcv(struct sk_buff *skb)
  279. {
  280. struct ip_tunnel *tunnel;
  281. const struct iphdr *iph = ip_hdr(skb);
  282. tunnel = vti_tunnel_lookup(dev_net(skb->dev), iph->saddr, iph->daddr);
  283. if (tunnel != NULL) {
  284. struct pcpu_tstats *tstats;
  285. tstats = this_cpu_ptr(tunnel->dev->tstats);
  286. u64_stats_update_begin(&tstats->syncp);
  287. tstats->rx_packets++;
  288. tstats->rx_bytes += skb->len;
  289. u64_stats_update_end(&tstats->syncp);
  290. skb->dev = tunnel->dev;
  291. return 1;
  292. }
  293. return -1;
  294. }
  295. /* This function assumes it is being called from dev_queue_xmit()
  296. * and that skb is filled properly by that function.
  297. */
  298. static netdev_tx_t vti_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
  299. {
  300. struct ip_tunnel *tunnel = netdev_priv(dev);
  301. struct pcpu_tstats *tstats;
  302. struct iphdr *tiph = &tunnel->parms.iph;
  303. u8 tos;
  304. struct rtable *rt; /* Route to the other host */
  305. struct net_device *tdev; /* Device to other host */
  306. struct iphdr *old_iph = ip_hdr(skb);
  307. __be32 dst = tiph->daddr;
  308. struct flowi4 fl4;
  309. if (skb->protocol != htons(ETH_P_IP))
  310. goto tx_error;
  311. tos = old_iph->tos;
  312. memset(&fl4, 0, sizeof(fl4));
  313. flowi4_init_output(&fl4, tunnel->parms.link,
  314. be32_to_cpu(tunnel->parms.i_key), RT_TOS(tos),
  315. RT_SCOPE_UNIVERSE,
  316. IPPROTO_IPIP, 0,
  317. dst, tiph->saddr, 0, 0);
  318. rt = ip_route_output_key(dev_net(dev), &fl4);
  319. if (IS_ERR(rt)) {
  320. dev->stats.tx_carrier_errors++;
  321. goto tx_error_icmp;
  322. }
  323. /* if there is no transform then this tunnel is not functional.
  324. * Or if the xfrm is not mode tunnel.
  325. */
  326. if (!rt->dst.xfrm ||
  327. rt->dst.xfrm->props.mode != XFRM_MODE_TUNNEL) {
  328. dev->stats.tx_carrier_errors++;
  329. goto tx_error_icmp;
  330. }
  331. tdev = rt->dst.dev;
  332. if (tdev == dev) {
  333. ip_rt_put(rt);
  334. dev->stats.collisions++;
  335. goto tx_error;
  336. }
  337. if (tunnel->err_count > 0) {
  338. if (time_before(jiffies,
  339. tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
  340. tunnel->err_count--;
  341. dst_link_failure(skb);
  342. } else
  343. tunnel->err_count = 0;
  344. }
  345. IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
  346. IPSKB_REROUTED);
  347. skb_dst_drop(skb);
  348. skb_dst_set(skb, &rt->dst);
  349. nf_reset(skb);
  350. skb->dev = skb_dst(skb)->dev;
  351. tstats = this_cpu_ptr(dev->tstats);
  352. VTI_XMIT(tstats, &dev->stats);
  353. return NETDEV_TX_OK;
  354. tx_error_icmp:
  355. dst_link_failure(skb);
  356. tx_error:
  357. dev->stats.tx_errors++;
  358. dev_kfree_skb(skb);
  359. return NETDEV_TX_OK;
  360. }
  361. static int vti_tunnel_bind_dev(struct net_device *dev)
  362. {
  363. struct net_device *tdev = NULL;
  364. struct ip_tunnel *tunnel;
  365. struct iphdr *iph;
  366. tunnel = netdev_priv(dev);
  367. iph = &tunnel->parms.iph;
  368. if (iph->daddr) {
  369. struct rtable *rt;
  370. struct flowi4 fl4;
  371. memset(&fl4, 0, sizeof(fl4));
  372. flowi4_init_output(&fl4, tunnel->parms.link,
  373. be32_to_cpu(tunnel->parms.i_key),
  374. RT_TOS(iph->tos), RT_SCOPE_UNIVERSE,
  375. IPPROTO_IPIP, 0,
  376. iph->daddr, iph->saddr, 0, 0);
  377. rt = ip_route_output_key(dev_net(dev), &fl4);
  378. if (!IS_ERR(rt)) {
  379. tdev = rt->dst.dev;
  380. ip_rt_put(rt);
  381. }
  382. dev->flags |= IFF_POINTOPOINT;
  383. }
  384. if (!tdev && tunnel->parms.link)
  385. tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
  386. if (tdev) {
  387. dev->hard_header_len = tdev->hard_header_len +
  388. sizeof(struct iphdr);
  389. dev->mtu = tdev->mtu;
  390. }
  391. dev->iflink = tunnel->parms.link;
  392. return dev->mtu;
  393. }
  394. static int
  395. vti_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  396. {
  397. int err = 0;
  398. struct ip_tunnel_parm p;
  399. struct ip_tunnel *t;
  400. struct net *net = dev_net(dev);
  401. struct vti_net *ipn = net_generic(net, vti_net_id);
  402. switch (cmd) {
  403. case SIOCGETTUNNEL:
  404. t = NULL;
  405. if (dev == ipn->fb_tunnel_dev) {
  406. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data,
  407. sizeof(p))) {
  408. err = -EFAULT;
  409. break;
  410. }
  411. t = vti_tunnel_locate(net, &p, 0);
  412. }
  413. if (t == NULL)
  414. t = netdev_priv(dev);
  415. memcpy(&p, &t->parms, sizeof(p));
  416. p.i_flags |= GRE_KEY | VTI_ISVTI;
  417. p.o_flags |= GRE_KEY;
  418. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  419. err = -EFAULT;
  420. break;
  421. case SIOCADDTUNNEL:
  422. case SIOCCHGTUNNEL:
  423. err = -EPERM;
  424. if (!capable(CAP_NET_ADMIN))
  425. goto done;
  426. err = -EFAULT;
  427. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  428. goto done;
  429. err = -EINVAL;
  430. if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPIP ||
  431. p.iph.ihl != 5)
  432. goto done;
  433. t = vti_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
  434. if (dev != ipn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  435. if (t != NULL) {
  436. if (t->dev != dev) {
  437. err = -EEXIST;
  438. break;
  439. }
  440. } else {
  441. if (((dev->flags&IFF_POINTOPOINT) &&
  442. !p.iph.daddr) ||
  443. (!(dev->flags&IFF_POINTOPOINT) &&
  444. p.iph.daddr)) {
  445. err = -EINVAL;
  446. break;
  447. }
  448. t = netdev_priv(dev);
  449. vti_tunnel_unlink(ipn, t);
  450. synchronize_net();
  451. t->parms.iph.saddr = p.iph.saddr;
  452. t->parms.iph.daddr = p.iph.daddr;
  453. t->parms.i_key = p.i_key;
  454. t->parms.o_key = p.o_key;
  455. t->parms.iph.protocol = IPPROTO_IPIP;
  456. memcpy(dev->dev_addr, &p.iph.saddr, 4);
  457. memcpy(dev->broadcast, &p.iph.daddr, 4);
  458. vti_tunnel_link(ipn, t);
  459. netdev_state_change(dev);
  460. }
  461. }
  462. if (t) {
  463. err = 0;
  464. if (cmd == SIOCCHGTUNNEL) {
  465. t->parms.i_key = p.i_key;
  466. t->parms.o_key = p.o_key;
  467. if (t->parms.link != p.link) {
  468. t->parms.link = p.link;
  469. vti_tunnel_bind_dev(dev);
  470. netdev_state_change(dev);
  471. }
  472. }
  473. p.i_flags |= GRE_KEY | VTI_ISVTI;
  474. p.o_flags |= GRE_KEY;
  475. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms,
  476. sizeof(p)))
  477. err = -EFAULT;
  478. } else
  479. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  480. break;
  481. case SIOCDELTUNNEL:
  482. err = -EPERM;
  483. if (!capable(CAP_NET_ADMIN))
  484. goto done;
  485. if (dev == ipn->fb_tunnel_dev) {
  486. err = -EFAULT;
  487. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data,
  488. sizeof(p)))
  489. goto done;
  490. err = -ENOENT;
  491. t = vti_tunnel_locate(net, &p, 0);
  492. if (t == NULL)
  493. goto done;
  494. err = -EPERM;
  495. if (t->dev == ipn->fb_tunnel_dev)
  496. goto done;
  497. dev = t->dev;
  498. }
  499. unregister_netdevice(dev);
  500. err = 0;
  501. break;
  502. default:
  503. err = -EINVAL;
  504. }
  505. done:
  506. return err;
  507. }
  508. static int vti_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  509. {
  510. if (new_mtu < 68 || new_mtu > 0xFFF8)
  511. return -EINVAL;
  512. dev->mtu = new_mtu;
  513. return 0;
  514. }
  515. static const struct net_device_ops vti_netdev_ops = {
  516. .ndo_init = vti_tunnel_init,
  517. .ndo_uninit = vti_tunnel_uninit,
  518. .ndo_start_xmit = vti_tunnel_xmit,
  519. .ndo_do_ioctl = vti_tunnel_ioctl,
  520. .ndo_change_mtu = vti_tunnel_change_mtu,
  521. .ndo_get_stats64 = vti_get_stats64,
  522. };
  523. static void vti_dev_free(struct net_device *dev)
  524. {
  525. free_percpu(dev->tstats);
  526. free_netdev(dev);
  527. }
  528. static void vti_tunnel_setup(struct net_device *dev)
  529. {
  530. dev->netdev_ops = &vti_netdev_ops;
  531. dev->destructor = vti_dev_free;
  532. dev->type = ARPHRD_TUNNEL;
  533. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
  534. dev->mtu = ETH_DATA_LEN;
  535. dev->flags = IFF_NOARP;
  536. dev->iflink = 0;
  537. dev->addr_len = 4;
  538. dev->features |= NETIF_F_NETNS_LOCAL;
  539. dev->features |= NETIF_F_LLTX;
  540. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  541. }
  542. static int vti_tunnel_init(struct net_device *dev)
  543. {
  544. struct ip_tunnel *tunnel = netdev_priv(dev);
  545. tunnel->dev = dev;
  546. strcpy(tunnel->parms.name, dev->name);
  547. memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
  548. memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
  549. dev->tstats = alloc_percpu(struct pcpu_tstats);
  550. if (!dev->tstats)
  551. return -ENOMEM;
  552. return 0;
  553. }
  554. static int __net_init vti_fb_tunnel_init(struct net_device *dev)
  555. {
  556. struct ip_tunnel *tunnel = netdev_priv(dev);
  557. struct iphdr *iph = &tunnel->parms.iph;
  558. struct vti_net *ipn = net_generic(dev_net(dev), vti_net_id);
  559. tunnel->dev = dev;
  560. strcpy(tunnel->parms.name, dev->name);
  561. iph->version = 4;
  562. iph->protocol = IPPROTO_IPIP;
  563. iph->ihl = 5;
  564. dev->tstats = alloc_percpu(struct pcpu_tstats);
  565. if (!dev->tstats)
  566. return -ENOMEM;
  567. dev_hold(dev);
  568. rcu_assign_pointer(ipn->tunnels_wc[0], tunnel);
  569. return 0;
  570. }
  571. static struct xfrm_tunnel vti_handler __read_mostly = {
  572. .handler = vti_rcv,
  573. .err_handler = vti_err,
  574. .priority = 1,
  575. };
  576. static void vti_destroy_tunnels(struct vti_net *ipn, struct list_head *head)
  577. {
  578. int prio;
  579. for (prio = 1; prio < 4; prio++) {
  580. int h;
  581. for (h = 0; h < HASH_SIZE; h++) {
  582. struct ip_tunnel *t;
  583. t = rtnl_dereference(ipn->tunnels[prio][h]);
  584. while (t != NULL) {
  585. unregister_netdevice_queue(t->dev, head);
  586. t = rtnl_dereference(t->next);
  587. }
  588. }
  589. }
  590. }
  591. static int __net_init vti_init_net(struct net *net)
  592. {
  593. int err;
  594. struct vti_net *ipn = net_generic(net, vti_net_id);
  595. ipn->tunnels[0] = ipn->tunnels_wc;
  596. ipn->tunnels[1] = ipn->tunnels_l;
  597. ipn->tunnels[2] = ipn->tunnels_r;
  598. ipn->tunnels[3] = ipn->tunnels_r_l;
  599. ipn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel),
  600. "ip_vti0",
  601. vti_tunnel_setup);
  602. if (!ipn->fb_tunnel_dev) {
  603. err = -ENOMEM;
  604. goto err_alloc_dev;
  605. }
  606. dev_net_set(ipn->fb_tunnel_dev, net);
  607. err = vti_fb_tunnel_init(ipn->fb_tunnel_dev);
  608. if (err)
  609. goto err_reg_dev;
  610. ipn->fb_tunnel_dev->rtnl_link_ops = &vti_link_ops;
  611. err = register_netdev(ipn->fb_tunnel_dev);
  612. if (err)
  613. goto err_reg_dev;
  614. return 0;
  615. err_reg_dev:
  616. vti_dev_free(ipn->fb_tunnel_dev);
  617. err_alloc_dev:
  618. /* nothing */
  619. return err;
  620. }
  621. static void __net_exit vti_exit_net(struct net *net)
  622. {
  623. struct vti_net *ipn = net_generic(net, vti_net_id);
  624. LIST_HEAD(list);
  625. rtnl_lock();
  626. vti_destroy_tunnels(ipn, &list);
  627. unregister_netdevice_many(&list);
  628. rtnl_unlock();
  629. }
  630. static struct pernet_operations vti_net_ops = {
  631. .init = vti_init_net,
  632. .exit = vti_exit_net,
  633. .id = &vti_net_id,
  634. .size = sizeof(struct vti_net),
  635. };
  636. static int vti_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
  637. {
  638. return 0;
  639. }
  640. static void vti_netlink_parms(struct nlattr *data[],
  641. struct ip_tunnel_parm *parms)
  642. {
  643. memset(parms, 0, sizeof(*parms));
  644. parms->iph.protocol = IPPROTO_IPIP;
  645. if (!data)
  646. return;
  647. if (data[IFLA_VTI_LINK])
  648. parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
  649. if (data[IFLA_VTI_IKEY])
  650. parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
  651. if (data[IFLA_VTI_OKEY])
  652. parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
  653. if (data[IFLA_VTI_LOCAL])
  654. parms->iph.saddr = nla_get_be32(data[IFLA_VTI_LOCAL]);
  655. if (data[IFLA_VTI_REMOTE])
  656. parms->iph.daddr = nla_get_be32(data[IFLA_VTI_REMOTE]);
  657. }
  658. static int vti_newlink(struct net *src_net, struct net_device *dev,
  659. struct nlattr *tb[], struct nlattr *data[])
  660. {
  661. struct ip_tunnel *nt;
  662. struct net *net = dev_net(dev);
  663. struct vti_net *ipn = net_generic(net, vti_net_id);
  664. int mtu;
  665. int err;
  666. nt = netdev_priv(dev);
  667. vti_netlink_parms(data, &nt->parms);
  668. if (vti_tunnel_locate(net, &nt->parms, 0))
  669. return -EEXIST;
  670. mtu = vti_tunnel_bind_dev(dev);
  671. if (!tb[IFLA_MTU])
  672. dev->mtu = mtu;
  673. err = register_netdevice(dev);
  674. if (err)
  675. goto out;
  676. dev_hold(dev);
  677. vti_tunnel_link(ipn, nt);
  678. out:
  679. return err;
  680. }
  681. static int vti_changelink(struct net_device *dev, struct nlattr *tb[],
  682. struct nlattr *data[])
  683. {
  684. struct ip_tunnel *t, *nt;
  685. struct net *net = dev_net(dev);
  686. struct vti_net *ipn = net_generic(net, vti_net_id);
  687. struct ip_tunnel_parm p;
  688. int mtu;
  689. if (dev == ipn->fb_tunnel_dev)
  690. return -EINVAL;
  691. nt = netdev_priv(dev);
  692. vti_netlink_parms(data, &p);
  693. t = vti_tunnel_locate(net, &p, 0);
  694. if (t) {
  695. if (t->dev != dev)
  696. return -EEXIST;
  697. } else {
  698. t = nt;
  699. vti_tunnel_unlink(ipn, t);
  700. t->parms.iph.saddr = p.iph.saddr;
  701. t->parms.iph.daddr = p.iph.daddr;
  702. t->parms.i_key = p.i_key;
  703. t->parms.o_key = p.o_key;
  704. if (dev->type != ARPHRD_ETHER) {
  705. memcpy(dev->dev_addr, &p.iph.saddr, 4);
  706. memcpy(dev->broadcast, &p.iph.daddr, 4);
  707. }
  708. vti_tunnel_link(ipn, t);
  709. netdev_state_change(dev);
  710. }
  711. if (t->parms.link != p.link) {
  712. t->parms.link = p.link;
  713. mtu = vti_tunnel_bind_dev(dev);
  714. if (!tb[IFLA_MTU])
  715. dev->mtu = mtu;
  716. netdev_state_change(dev);
  717. }
  718. return 0;
  719. }
  720. static size_t vti_get_size(const struct net_device *dev)
  721. {
  722. return
  723. /* IFLA_VTI_LINK */
  724. nla_total_size(4) +
  725. /* IFLA_VTI_IKEY */
  726. nla_total_size(4) +
  727. /* IFLA_VTI_OKEY */
  728. nla_total_size(4) +
  729. /* IFLA_VTI_LOCAL */
  730. nla_total_size(4) +
  731. /* IFLA_VTI_REMOTE */
  732. nla_total_size(4) +
  733. 0;
  734. }
  735. static int vti_fill_info(struct sk_buff *skb, const struct net_device *dev)
  736. {
  737. struct ip_tunnel *t = netdev_priv(dev);
  738. struct ip_tunnel_parm *p = &t->parms;
  739. nla_put_u32(skb, IFLA_VTI_LINK, p->link);
  740. nla_put_be32(skb, IFLA_VTI_IKEY, p->i_key);
  741. nla_put_be32(skb, IFLA_VTI_OKEY, p->o_key);
  742. nla_put_be32(skb, IFLA_VTI_LOCAL, p->iph.saddr);
  743. nla_put_be32(skb, IFLA_VTI_REMOTE, p->iph.daddr);
  744. return 0;
  745. }
  746. static const struct nla_policy vti_policy[IFLA_VTI_MAX + 1] = {
  747. [IFLA_VTI_LINK] = { .type = NLA_U32 },
  748. [IFLA_VTI_IKEY] = { .type = NLA_U32 },
  749. [IFLA_VTI_OKEY] = { .type = NLA_U32 },
  750. [IFLA_VTI_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
  751. [IFLA_VTI_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
  752. };
  753. static struct rtnl_link_ops vti_link_ops __read_mostly = {
  754. .kind = "vti",
  755. .maxtype = IFLA_VTI_MAX,
  756. .policy = vti_policy,
  757. .priv_size = sizeof(struct ip_tunnel),
  758. .setup = vti_tunnel_setup,
  759. .validate = vti_tunnel_validate,
  760. .newlink = vti_newlink,
  761. .changelink = vti_changelink,
  762. .get_size = vti_get_size,
  763. .fill_info = vti_fill_info,
  764. };
  765. static int __init vti_init(void)
  766. {
  767. int err;
  768. pr_info("IPv4 over IPSec tunneling driver\n");
  769. err = register_pernet_device(&vti_net_ops);
  770. if (err < 0)
  771. return err;
  772. err = xfrm4_mode_tunnel_input_register(&vti_handler);
  773. if (err < 0) {
  774. unregister_pernet_device(&vti_net_ops);
  775. pr_info(KERN_INFO "vti init: can't register tunnel\n");
  776. }
  777. err = rtnl_link_register(&vti_link_ops);
  778. if (err < 0)
  779. goto rtnl_link_failed;
  780. return err;
  781. rtnl_link_failed:
  782. xfrm4_mode_tunnel_input_deregister(&vti_handler);
  783. unregister_pernet_device(&vti_net_ops);
  784. return err;
  785. }
  786. static void __exit vti_fini(void)
  787. {
  788. rtnl_link_unregister(&vti_link_ops);
  789. if (xfrm4_mode_tunnel_input_deregister(&vti_handler))
  790. pr_info("vti close: can't deregister tunnel\n");
  791. unregister_pernet_device(&vti_net_ops);
  792. }
  793. module_init(vti_init);
  794. module_exit(vti_fini);
  795. MODULE_LICENSE("GPL");
  796. MODULE_ALIAS_RTNL_LINK("vti");
  797. MODULE_ALIAS_NETDEV("ip_vti0");