ip_vti.c 22 KB

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