ip_vti.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  286. return -1;
  287. tstats = this_cpu_ptr(tunnel->dev->tstats);
  288. u64_stats_update_begin(&tstats->syncp);
  289. tstats->rx_packets++;
  290. tstats->rx_bytes += skb->len;
  291. u64_stats_update_end(&tstats->syncp);
  292. skb->mark = 0;
  293. secpath_reset(skb);
  294. skb->dev = tunnel->dev;
  295. return 1;
  296. }
  297. return -1;
  298. }
  299. /* This function assumes it is being called from dev_queue_xmit()
  300. * and that skb is filled properly by that function.
  301. */
  302. static netdev_tx_t vti_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
  303. {
  304. struct ip_tunnel *tunnel = netdev_priv(dev);
  305. struct pcpu_tstats *tstats;
  306. struct iphdr *tiph = &tunnel->parms.iph;
  307. u8 tos;
  308. struct rtable *rt; /* Route to the other host */
  309. struct net_device *tdev; /* Device to other host */
  310. struct iphdr *old_iph = ip_hdr(skb);
  311. __be32 dst = tiph->daddr;
  312. struct flowi4 fl4;
  313. if (skb->protocol != htons(ETH_P_IP))
  314. goto tx_error;
  315. tos = old_iph->tos;
  316. memset(&fl4, 0, sizeof(fl4));
  317. flowi4_init_output(&fl4, tunnel->parms.link,
  318. be32_to_cpu(tunnel->parms.i_key), RT_TOS(tos),
  319. RT_SCOPE_UNIVERSE,
  320. IPPROTO_IPIP, 0,
  321. dst, tiph->saddr, 0, 0);
  322. rt = ip_route_output_key(dev_net(dev), &fl4);
  323. if (IS_ERR(rt)) {
  324. dev->stats.tx_carrier_errors++;
  325. goto tx_error_icmp;
  326. }
  327. /* if there is no transform then this tunnel is not functional.
  328. * Or if the xfrm is not mode tunnel.
  329. */
  330. if (!rt->dst.xfrm ||
  331. rt->dst.xfrm->props.mode != XFRM_MODE_TUNNEL) {
  332. dev->stats.tx_carrier_errors++;
  333. goto tx_error_icmp;
  334. }
  335. tdev = rt->dst.dev;
  336. if (tdev == dev) {
  337. ip_rt_put(rt);
  338. dev->stats.collisions++;
  339. goto tx_error;
  340. }
  341. if (tunnel->err_count > 0) {
  342. if (time_before(jiffies,
  343. tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
  344. tunnel->err_count--;
  345. dst_link_failure(skb);
  346. } else
  347. tunnel->err_count = 0;
  348. }
  349. IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
  350. IPSKB_REROUTED);
  351. skb_dst_drop(skb);
  352. skb_dst_set(skb, &rt->dst);
  353. nf_reset(skb);
  354. skb->dev = skb_dst(skb)->dev;
  355. tstats = this_cpu_ptr(dev->tstats);
  356. VTI_XMIT(tstats, &dev->stats);
  357. return NETDEV_TX_OK;
  358. tx_error_icmp:
  359. dst_link_failure(skb);
  360. tx_error:
  361. dev->stats.tx_errors++;
  362. dev_kfree_skb(skb);
  363. return NETDEV_TX_OK;
  364. }
  365. static int vti_tunnel_bind_dev(struct net_device *dev)
  366. {
  367. struct net_device *tdev = NULL;
  368. struct ip_tunnel *tunnel;
  369. struct iphdr *iph;
  370. tunnel = netdev_priv(dev);
  371. iph = &tunnel->parms.iph;
  372. if (iph->daddr) {
  373. struct rtable *rt;
  374. struct flowi4 fl4;
  375. memset(&fl4, 0, sizeof(fl4));
  376. flowi4_init_output(&fl4, tunnel->parms.link,
  377. be32_to_cpu(tunnel->parms.i_key),
  378. RT_TOS(iph->tos), RT_SCOPE_UNIVERSE,
  379. IPPROTO_IPIP, 0,
  380. iph->daddr, iph->saddr, 0, 0);
  381. rt = ip_route_output_key(dev_net(dev), &fl4);
  382. if (!IS_ERR(rt)) {
  383. tdev = rt->dst.dev;
  384. ip_rt_put(rt);
  385. }
  386. dev->flags |= IFF_POINTOPOINT;
  387. }
  388. if (!tdev && tunnel->parms.link)
  389. tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
  390. if (tdev) {
  391. dev->hard_header_len = tdev->hard_header_len +
  392. sizeof(struct iphdr);
  393. dev->mtu = tdev->mtu;
  394. }
  395. dev->iflink = tunnel->parms.link;
  396. return dev->mtu;
  397. }
  398. static int
  399. vti_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  400. {
  401. int err = 0;
  402. struct ip_tunnel_parm p;
  403. struct ip_tunnel *t;
  404. struct net *net = dev_net(dev);
  405. struct vti_net *ipn = net_generic(net, vti_net_id);
  406. switch (cmd) {
  407. case SIOCGETTUNNEL:
  408. t = NULL;
  409. if (dev == ipn->fb_tunnel_dev) {
  410. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data,
  411. sizeof(p))) {
  412. err = -EFAULT;
  413. break;
  414. }
  415. t = vti_tunnel_locate(net, &p, 0);
  416. }
  417. if (t == NULL)
  418. t = netdev_priv(dev);
  419. memcpy(&p, &t->parms, sizeof(p));
  420. p.i_flags |= GRE_KEY | VTI_ISVTI;
  421. p.o_flags |= GRE_KEY;
  422. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  423. err = -EFAULT;
  424. break;
  425. case SIOCADDTUNNEL:
  426. case SIOCCHGTUNNEL:
  427. err = -EPERM;
  428. if (!capable(CAP_NET_ADMIN))
  429. goto done;
  430. err = -EFAULT;
  431. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  432. goto done;
  433. err = -EINVAL;
  434. if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPIP ||
  435. p.iph.ihl != 5)
  436. goto done;
  437. t = vti_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
  438. if (dev != ipn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  439. if (t != NULL) {
  440. if (t->dev != dev) {
  441. err = -EEXIST;
  442. break;
  443. }
  444. } else {
  445. if (((dev->flags&IFF_POINTOPOINT) &&
  446. !p.iph.daddr) ||
  447. (!(dev->flags&IFF_POINTOPOINT) &&
  448. p.iph.daddr)) {
  449. err = -EINVAL;
  450. break;
  451. }
  452. t = netdev_priv(dev);
  453. vti_tunnel_unlink(ipn, t);
  454. synchronize_net();
  455. t->parms.iph.saddr = p.iph.saddr;
  456. t->parms.iph.daddr = p.iph.daddr;
  457. t->parms.i_key = p.i_key;
  458. t->parms.o_key = p.o_key;
  459. t->parms.iph.protocol = IPPROTO_IPIP;
  460. memcpy(dev->dev_addr, &p.iph.saddr, 4);
  461. memcpy(dev->broadcast, &p.iph.daddr, 4);
  462. vti_tunnel_link(ipn, t);
  463. netdev_state_change(dev);
  464. }
  465. }
  466. if (t) {
  467. err = 0;
  468. if (cmd == SIOCCHGTUNNEL) {
  469. t->parms.i_key = p.i_key;
  470. t->parms.o_key = p.o_key;
  471. if (t->parms.link != p.link) {
  472. t->parms.link = p.link;
  473. vti_tunnel_bind_dev(dev);
  474. netdev_state_change(dev);
  475. }
  476. }
  477. p.i_flags |= GRE_KEY | VTI_ISVTI;
  478. p.o_flags |= GRE_KEY;
  479. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms,
  480. sizeof(p)))
  481. err = -EFAULT;
  482. } else
  483. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  484. break;
  485. case SIOCDELTUNNEL:
  486. err = -EPERM;
  487. if (!capable(CAP_NET_ADMIN))
  488. goto done;
  489. if (dev == ipn->fb_tunnel_dev) {
  490. err = -EFAULT;
  491. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data,
  492. sizeof(p)))
  493. goto done;
  494. err = -ENOENT;
  495. t = vti_tunnel_locate(net, &p, 0);
  496. if (t == NULL)
  497. goto done;
  498. err = -EPERM;
  499. if (t->dev == ipn->fb_tunnel_dev)
  500. goto done;
  501. dev = t->dev;
  502. }
  503. unregister_netdevice(dev);
  504. err = 0;
  505. break;
  506. default:
  507. err = -EINVAL;
  508. }
  509. done:
  510. return err;
  511. }
  512. static int vti_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  513. {
  514. if (new_mtu < 68 || new_mtu > 0xFFF8)
  515. return -EINVAL;
  516. dev->mtu = new_mtu;
  517. return 0;
  518. }
  519. static const struct net_device_ops vti_netdev_ops = {
  520. .ndo_init = vti_tunnel_init,
  521. .ndo_uninit = vti_tunnel_uninit,
  522. .ndo_start_xmit = vti_tunnel_xmit,
  523. .ndo_do_ioctl = vti_tunnel_ioctl,
  524. .ndo_change_mtu = vti_tunnel_change_mtu,
  525. .ndo_get_stats64 = vti_get_stats64,
  526. };
  527. static void vti_dev_free(struct net_device *dev)
  528. {
  529. free_percpu(dev->tstats);
  530. free_netdev(dev);
  531. }
  532. static void vti_tunnel_setup(struct net_device *dev)
  533. {
  534. dev->netdev_ops = &vti_netdev_ops;
  535. dev->destructor = vti_dev_free;
  536. dev->type = ARPHRD_TUNNEL;
  537. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
  538. dev->mtu = ETH_DATA_LEN;
  539. dev->flags = IFF_NOARP;
  540. dev->iflink = 0;
  541. dev->addr_len = 4;
  542. dev->features |= NETIF_F_NETNS_LOCAL;
  543. dev->features |= NETIF_F_LLTX;
  544. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  545. }
  546. static int vti_tunnel_init(struct net_device *dev)
  547. {
  548. struct ip_tunnel *tunnel = netdev_priv(dev);
  549. tunnel->dev = dev;
  550. strcpy(tunnel->parms.name, dev->name);
  551. memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
  552. memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
  553. dev->tstats = alloc_percpu(struct pcpu_tstats);
  554. if (!dev->tstats)
  555. return -ENOMEM;
  556. return 0;
  557. }
  558. static int __net_init vti_fb_tunnel_init(struct net_device *dev)
  559. {
  560. struct ip_tunnel *tunnel = netdev_priv(dev);
  561. struct iphdr *iph = &tunnel->parms.iph;
  562. struct vti_net *ipn = net_generic(dev_net(dev), vti_net_id);
  563. tunnel->dev = dev;
  564. strcpy(tunnel->parms.name, dev->name);
  565. iph->version = 4;
  566. iph->protocol = IPPROTO_IPIP;
  567. iph->ihl = 5;
  568. dev->tstats = alloc_percpu(struct pcpu_tstats);
  569. if (!dev->tstats)
  570. return -ENOMEM;
  571. dev_hold(dev);
  572. rcu_assign_pointer(ipn->tunnels_wc[0], tunnel);
  573. return 0;
  574. }
  575. static struct xfrm_tunnel vti_handler __read_mostly = {
  576. .handler = vti_rcv,
  577. .err_handler = vti_err,
  578. .priority = 1,
  579. };
  580. static void vti_destroy_tunnels(struct vti_net *ipn, struct list_head *head)
  581. {
  582. int prio;
  583. for (prio = 1; prio < 4; prio++) {
  584. int h;
  585. for (h = 0; h < HASH_SIZE; h++) {
  586. struct ip_tunnel *t;
  587. t = rtnl_dereference(ipn->tunnels[prio][h]);
  588. while (t != NULL) {
  589. unregister_netdevice_queue(t->dev, head);
  590. t = rtnl_dereference(t->next);
  591. }
  592. }
  593. }
  594. }
  595. static int __net_init vti_init_net(struct net *net)
  596. {
  597. int err;
  598. struct vti_net *ipn = net_generic(net, vti_net_id);
  599. ipn->tunnels[0] = ipn->tunnels_wc;
  600. ipn->tunnels[1] = ipn->tunnels_l;
  601. ipn->tunnels[2] = ipn->tunnels_r;
  602. ipn->tunnels[3] = ipn->tunnels_r_l;
  603. ipn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel),
  604. "ip_vti0",
  605. vti_tunnel_setup);
  606. if (!ipn->fb_tunnel_dev) {
  607. err = -ENOMEM;
  608. goto err_alloc_dev;
  609. }
  610. dev_net_set(ipn->fb_tunnel_dev, net);
  611. err = vti_fb_tunnel_init(ipn->fb_tunnel_dev);
  612. if (err)
  613. goto err_reg_dev;
  614. ipn->fb_tunnel_dev->rtnl_link_ops = &vti_link_ops;
  615. err = register_netdev(ipn->fb_tunnel_dev);
  616. if (err)
  617. goto err_reg_dev;
  618. return 0;
  619. err_reg_dev:
  620. vti_dev_free(ipn->fb_tunnel_dev);
  621. err_alloc_dev:
  622. /* nothing */
  623. return err;
  624. }
  625. static void __net_exit vti_exit_net(struct net *net)
  626. {
  627. struct vti_net *ipn = net_generic(net, vti_net_id);
  628. LIST_HEAD(list);
  629. rtnl_lock();
  630. vti_destroy_tunnels(ipn, &list);
  631. unregister_netdevice_many(&list);
  632. rtnl_unlock();
  633. }
  634. static struct pernet_operations vti_net_ops = {
  635. .init = vti_init_net,
  636. .exit = vti_exit_net,
  637. .id = &vti_net_id,
  638. .size = sizeof(struct vti_net),
  639. };
  640. static int vti_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
  641. {
  642. return 0;
  643. }
  644. static void vti_netlink_parms(struct nlattr *data[],
  645. struct ip_tunnel_parm *parms)
  646. {
  647. memset(parms, 0, sizeof(*parms));
  648. parms->iph.protocol = IPPROTO_IPIP;
  649. if (!data)
  650. return;
  651. if (data[IFLA_VTI_LINK])
  652. parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
  653. if (data[IFLA_VTI_IKEY])
  654. parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
  655. if (data[IFLA_VTI_OKEY])
  656. parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
  657. if (data[IFLA_VTI_LOCAL])
  658. parms->iph.saddr = nla_get_be32(data[IFLA_VTI_LOCAL]);
  659. if (data[IFLA_VTI_REMOTE])
  660. parms->iph.daddr = nla_get_be32(data[IFLA_VTI_REMOTE]);
  661. }
  662. static int vti_newlink(struct net *src_net, struct net_device *dev,
  663. struct nlattr *tb[], struct nlattr *data[])
  664. {
  665. struct ip_tunnel *nt;
  666. struct net *net = dev_net(dev);
  667. struct vti_net *ipn = net_generic(net, vti_net_id);
  668. int mtu;
  669. int err;
  670. nt = netdev_priv(dev);
  671. vti_netlink_parms(data, &nt->parms);
  672. if (vti_tunnel_locate(net, &nt->parms, 0))
  673. return -EEXIST;
  674. mtu = vti_tunnel_bind_dev(dev);
  675. if (!tb[IFLA_MTU])
  676. dev->mtu = mtu;
  677. err = register_netdevice(dev);
  678. if (err)
  679. goto out;
  680. dev_hold(dev);
  681. vti_tunnel_link(ipn, nt);
  682. out:
  683. return err;
  684. }
  685. static int vti_changelink(struct net_device *dev, struct nlattr *tb[],
  686. struct nlattr *data[])
  687. {
  688. struct ip_tunnel *t, *nt;
  689. struct net *net = dev_net(dev);
  690. struct vti_net *ipn = net_generic(net, vti_net_id);
  691. struct ip_tunnel_parm p;
  692. int mtu;
  693. if (dev == ipn->fb_tunnel_dev)
  694. return -EINVAL;
  695. nt = netdev_priv(dev);
  696. vti_netlink_parms(data, &p);
  697. t = vti_tunnel_locate(net, &p, 0);
  698. if (t) {
  699. if (t->dev != dev)
  700. return -EEXIST;
  701. } else {
  702. t = nt;
  703. vti_tunnel_unlink(ipn, t);
  704. t->parms.iph.saddr = p.iph.saddr;
  705. t->parms.iph.daddr = p.iph.daddr;
  706. t->parms.i_key = p.i_key;
  707. t->parms.o_key = p.o_key;
  708. if (dev->type != ARPHRD_ETHER) {
  709. memcpy(dev->dev_addr, &p.iph.saddr, 4);
  710. memcpy(dev->broadcast, &p.iph.daddr, 4);
  711. }
  712. vti_tunnel_link(ipn, t);
  713. netdev_state_change(dev);
  714. }
  715. if (t->parms.link != p.link) {
  716. t->parms.link = p.link;
  717. mtu = vti_tunnel_bind_dev(dev);
  718. if (!tb[IFLA_MTU])
  719. dev->mtu = mtu;
  720. netdev_state_change(dev);
  721. }
  722. return 0;
  723. }
  724. static size_t vti_get_size(const struct net_device *dev)
  725. {
  726. return
  727. /* IFLA_VTI_LINK */
  728. nla_total_size(4) +
  729. /* IFLA_VTI_IKEY */
  730. nla_total_size(4) +
  731. /* IFLA_VTI_OKEY */
  732. nla_total_size(4) +
  733. /* IFLA_VTI_LOCAL */
  734. nla_total_size(4) +
  735. /* IFLA_VTI_REMOTE */
  736. nla_total_size(4) +
  737. 0;
  738. }
  739. static int vti_fill_info(struct sk_buff *skb, const struct net_device *dev)
  740. {
  741. struct ip_tunnel *t = netdev_priv(dev);
  742. struct ip_tunnel_parm *p = &t->parms;
  743. nla_put_u32(skb, IFLA_VTI_LINK, p->link);
  744. nla_put_be32(skb, IFLA_VTI_IKEY, p->i_key);
  745. nla_put_be32(skb, IFLA_VTI_OKEY, p->o_key);
  746. nla_put_be32(skb, IFLA_VTI_LOCAL, p->iph.saddr);
  747. nla_put_be32(skb, IFLA_VTI_REMOTE, p->iph.daddr);
  748. return 0;
  749. }
  750. static const struct nla_policy vti_policy[IFLA_VTI_MAX + 1] = {
  751. [IFLA_VTI_LINK] = { .type = NLA_U32 },
  752. [IFLA_VTI_IKEY] = { .type = NLA_U32 },
  753. [IFLA_VTI_OKEY] = { .type = NLA_U32 },
  754. [IFLA_VTI_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
  755. [IFLA_VTI_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
  756. };
  757. static struct rtnl_link_ops vti_link_ops __read_mostly = {
  758. .kind = "vti",
  759. .maxtype = IFLA_VTI_MAX,
  760. .policy = vti_policy,
  761. .priv_size = sizeof(struct ip_tunnel),
  762. .setup = vti_tunnel_setup,
  763. .validate = vti_tunnel_validate,
  764. .newlink = vti_newlink,
  765. .changelink = vti_changelink,
  766. .get_size = vti_get_size,
  767. .fill_info = vti_fill_info,
  768. };
  769. static int __init vti_init(void)
  770. {
  771. int err;
  772. pr_info("IPv4 over IPSec tunneling driver\n");
  773. err = register_pernet_device(&vti_net_ops);
  774. if (err < 0)
  775. return err;
  776. err = xfrm4_mode_tunnel_input_register(&vti_handler);
  777. if (err < 0) {
  778. unregister_pernet_device(&vti_net_ops);
  779. pr_info(KERN_INFO "vti init: can't register tunnel\n");
  780. }
  781. err = rtnl_link_register(&vti_link_ops);
  782. if (err < 0)
  783. goto rtnl_link_failed;
  784. return err;
  785. rtnl_link_failed:
  786. xfrm4_mode_tunnel_input_deregister(&vti_handler);
  787. unregister_pernet_device(&vti_net_ops);
  788. return err;
  789. }
  790. static void __exit vti_fini(void)
  791. {
  792. rtnl_link_unregister(&vti_link_ops);
  793. if (xfrm4_mode_tunnel_input_deregister(&vti_handler))
  794. pr_info("vti close: can't deregister tunnel\n");
  795. unregister_pernet_device(&vti_net_ops);
  796. }
  797. module_init(vti_init);
  798. module_exit(vti_fini);
  799. MODULE_LICENSE("GPL");
  800. MODULE_ALIAS_RTNL_LINK("vti");
  801. MODULE_ALIAS_NETDEV("ip_vti0");