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