Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 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 | /* |
| 16 | This version of net/ipv4/ip_vti.c is cloned of net/ipv4/ipip.c |
| 17 | |
| 18 | For comments look at net/ipv4/ip_gre.c --ANK |
| 19 | */ |
| 20 | |
| 21 | |
| 22 | #include <linux/capability.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/types.h> |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/uaccess.h> |
| 27 | #include <linux/skbuff.h> |
| 28 | #include <linux/netdevice.h> |
| 29 | #include <linux/in.h> |
| 30 | #include <linux/tcp.h> |
| 31 | #include <linux/udp.h> |
| 32 | #include <linux/if_arp.h> |
| 33 | #include <linux/mroute.h> |
| 34 | #include <linux/init.h> |
| 35 | #include <linux/netfilter_ipv4.h> |
| 36 | #include <linux/if_ether.h> |
| 37 | |
| 38 | #include <net/sock.h> |
| 39 | #include <net/ip.h> |
| 40 | #include <net/icmp.h> |
Pravin B Shelar | c544193 | 2013-03-25 14:49:35 +0000 | [diff] [blame] | 41 | #include <net/ip_tunnels.h> |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 42 | #include <net/inet_ecn.h> |
| 43 | #include <net/xfrm.h> |
| 44 | #include <net/net_namespace.h> |
| 45 | #include <net/netns/generic.h> |
| 46 | |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 47 | static struct rtnl_link_ops vti_link_ops __read_mostly; |
| 48 | |
| 49 | static int vti_net_id __read_mostly; |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 50 | static int vti_tunnel_init(struct net_device *dev); |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 51 | |
Steffen Klassert | df3893c | 2014-02-21 08:41:10 +0100 | [diff] [blame^] | 52 | static int vti_input(struct sk_buff *skb, int nexthdr, __be32 spi, |
| 53 | int encap_type) |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 54 | { |
| 55 | struct ip_tunnel *tunnel; |
| 56 | const struct iphdr *iph = ip_hdr(skb); |
Amerigo Wang | b9959fd | 2013-07-21 10:46:25 +0800 | [diff] [blame] | 57 | struct net *net = dev_net(skb->dev); |
| 58 | struct ip_tunnel_net *itn = net_generic(net, vti_net_id); |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 59 | |
Amerigo Wang | b9959fd | 2013-07-21 10:46:25 +0800 | [diff] [blame] | 60 | tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY, |
| 61 | iph->saddr, iph->daddr, 0); |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 62 | if (tunnel != NULL) { |
Steffen Klassert | df3893c | 2014-02-21 08:41:10 +0100 | [diff] [blame^] | 63 | if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) |
| 64 | goto drop; |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 65 | |
Steffen Klassert | df3893c | 2014-02-21 08:41:10 +0100 | [diff] [blame^] | 66 | XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = tunnel; |
| 67 | skb->mark = be32_to_cpu(tunnel->parms.i_key); |
Christophe Gouault | 7263a51 | 2013-10-08 17:21:22 +0200 | [diff] [blame] | 68 | |
Steffen Klassert | df3893c | 2014-02-21 08:41:10 +0100 | [diff] [blame^] | 69 | return xfrm_input(skb, nexthdr, spi, encap_type); |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 70 | } |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 71 | |
Steffen Klassert | df3893c | 2014-02-21 08:41:10 +0100 | [diff] [blame^] | 72 | return -EINVAL; |
| 73 | drop: |
| 74 | kfree_skb(skb); |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static int vti_rcv(struct sk_buff *skb) |
| 79 | { |
| 80 | XFRM_SPI_SKB_CB(skb)->family = AF_INET; |
| 81 | XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr); |
| 82 | |
| 83 | return vti_input(skb, ip_hdr(skb)->protocol, 0, 0); |
| 84 | } |
| 85 | |
| 86 | static int vti_rcv_cb(struct sk_buff *skb, int err) |
| 87 | { |
| 88 | unsigned short family; |
| 89 | struct net_device *dev; |
| 90 | struct pcpu_sw_netstats *tstats; |
| 91 | struct xfrm_state *x; |
| 92 | struct ip_tunnel *tunnel = XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4; |
| 93 | |
| 94 | if (!tunnel) |
| 95 | return 1; |
| 96 | |
| 97 | dev = tunnel->dev; |
| 98 | |
| 99 | if (err) { |
| 100 | dev->stats.rx_errors++; |
| 101 | dev->stats.rx_dropped++; |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | x = xfrm_input_state(skb); |
| 107 | family = x->inner_mode->afinfo->family; |
| 108 | |
| 109 | if (!xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family)) |
| 110 | return -EPERM; |
| 111 | |
| 112 | skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(skb->dev))); |
| 113 | skb->dev = dev; |
| 114 | |
| 115 | tstats = this_cpu_ptr(dev->tstats); |
| 116 | |
| 117 | u64_stats_update_begin(&tstats->syncp); |
| 118 | tstats->rx_packets++; |
| 119 | tstats->rx_bytes += skb->len; |
| 120 | u64_stats_update_end(&tstats->syncp); |
| 121 | |
| 122 | return 0; |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /* This function assumes it is being called from dev_queue_xmit() |
| 126 | * and that skb is filled properly by that function. |
| 127 | */ |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 128 | static netdev_tx_t vti_tunnel_xmit(struct sk_buff *skb, struct net_device *dev) |
| 129 | { |
| 130 | struct ip_tunnel *tunnel = netdev_priv(dev); |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 131 | struct rtable *rt; /* Route to the other host */ |
| 132 | struct net_device *tdev; /* Device to other host */ |
Steffen Klassert | df3893c | 2014-02-21 08:41:10 +0100 | [diff] [blame^] | 133 | struct flowi fl; |
Amerigo Wang | b9959fd | 2013-07-21 10:46:25 +0800 | [diff] [blame] | 134 | int err; |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 135 | |
| 136 | if (skb->protocol != htons(ETH_P_IP)) |
| 137 | goto tx_error; |
| 138 | |
Steffen Klassert | df3893c | 2014-02-21 08:41:10 +0100 | [diff] [blame^] | 139 | memset(&fl, 0, sizeof(fl)); |
| 140 | skb->mark = be32_to_cpu(tunnel->parms.o_key); |
| 141 | xfrm_decode_session(skb, &fl, AF_INET); |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 142 | |
Steffen Klassert | df3893c | 2014-02-21 08:41:10 +0100 | [diff] [blame^] | 143 | if (!skb_dst(skb)) { |
| 144 | dev->stats.tx_carrier_errors++; |
| 145 | goto tx_error_icmp; |
| 146 | } |
| 147 | |
| 148 | dst_hold(skb_dst(skb)); |
| 149 | rt = (struct rtable *)xfrm_lookup(tunnel->net, skb_dst(skb), &fl, NULL, 0); |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 150 | if (IS_ERR(rt)) { |
| 151 | dev->stats.tx_carrier_errors++; |
| 152 | goto tx_error_icmp; |
| 153 | } |
Steffen Klassert | df3893c | 2014-02-21 08:41:10 +0100 | [diff] [blame^] | 154 | |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 155 | /* if there is no transform then this tunnel is not functional. |
| 156 | * Or if the xfrm is not mode tunnel. |
| 157 | */ |
| 158 | if (!rt->dst.xfrm || |
| 159 | rt->dst.xfrm->props.mode != XFRM_MODE_TUNNEL) { |
| 160 | dev->stats.tx_carrier_errors++; |
fan.du | 236c9f8 | 2013-11-19 16:53:28 +0800 | [diff] [blame] | 161 | ip_rt_put(rt); |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 162 | goto tx_error_icmp; |
| 163 | } |
| 164 | tdev = rt->dst.dev; |
| 165 | |
| 166 | if (tdev == dev) { |
| 167 | ip_rt_put(rt); |
| 168 | dev->stats.collisions++; |
| 169 | goto tx_error; |
| 170 | } |
| 171 | |
| 172 | if (tunnel->err_count > 0) { |
| 173 | if (time_before(jiffies, |
| 174 | tunnel->err_time + IPTUNNEL_ERR_TIMEO)) { |
| 175 | tunnel->err_count--; |
| 176 | dst_link_failure(skb); |
| 177 | } else |
| 178 | tunnel->err_count = 0; |
| 179 | } |
| 180 | |
Saurabh Mohan | baafc77 | 2013-06-10 17:45:10 -0700 | [diff] [blame] | 181 | memset(IPCB(skb), 0, sizeof(*IPCB(skb))); |
Steffen Klassert | df3893c | 2014-02-21 08:41:10 +0100 | [diff] [blame^] | 182 | skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(dev))); |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 183 | skb_dst_set(skb, &rt->dst); |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 184 | skb->dev = skb_dst(skb)->dev; |
| 185 | |
Amerigo Wang | b9959fd | 2013-07-21 10:46:25 +0800 | [diff] [blame] | 186 | err = dst_output(skb); |
| 187 | if (net_xmit_eval(err) == 0) |
| 188 | err = skb->len; |
| 189 | iptunnel_xmit_stats(err, &dev->stats, dev->tstats); |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 190 | return NETDEV_TX_OK; |
| 191 | |
| 192 | tx_error_icmp: |
| 193 | dst_link_failure(skb); |
| 194 | tx_error: |
| 195 | dev->stats.tx_errors++; |
Eric Dumazet | 3acfa1e | 2014-01-18 18:27:49 -0800 | [diff] [blame] | 196 | kfree_skb(skb); |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 197 | return NETDEV_TX_OK; |
| 198 | } |
| 199 | |
Steffen Klassert | df3893c | 2014-02-21 08:41:10 +0100 | [diff] [blame^] | 200 | static int vti4_err(struct sk_buff *skb, u32 info) |
| 201 | { |
| 202 | __be32 spi; |
| 203 | struct xfrm_state *x; |
| 204 | struct ip_tunnel *tunnel; |
| 205 | struct ip_esp_hdr *esph; |
| 206 | struct ip_auth_hdr *ah ; |
| 207 | struct ip_comp_hdr *ipch; |
| 208 | struct net *net = dev_net(skb->dev); |
| 209 | const struct iphdr *iph = (const struct iphdr *)skb->data; |
| 210 | int protocol = iph->protocol; |
| 211 | struct ip_tunnel_net *itn = net_generic(net, vti_net_id); |
| 212 | |
| 213 | tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY, |
| 214 | iph->daddr, iph->saddr, 0); |
| 215 | if (!tunnel) |
| 216 | return -1; |
| 217 | |
| 218 | switch (protocol) { |
| 219 | case IPPROTO_ESP: |
| 220 | esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2)); |
| 221 | spi = esph->spi; |
| 222 | break; |
| 223 | case IPPROTO_AH: |
| 224 | ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2)); |
| 225 | spi = ah->spi; |
| 226 | break; |
| 227 | case IPPROTO_COMP: |
| 228 | ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2)); |
| 229 | spi = htonl(ntohs(ipch->cpi)); |
| 230 | break; |
| 231 | default: |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | switch (icmp_hdr(skb)->type) { |
| 236 | case ICMP_DEST_UNREACH: |
| 237 | if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED) |
| 238 | return 0; |
| 239 | case ICMP_REDIRECT: |
| 240 | break; |
| 241 | default: |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr, |
| 246 | spi, protocol, AF_INET); |
| 247 | if (!x) |
| 248 | return 0; |
| 249 | |
| 250 | if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH) |
| 251 | ipv4_update_pmtu(skb, net, info, 0, 0, protocol, 0); |
| 252 | else |
| 253 | ipv4_redirect(skb, net, 0, 0, protocol, 0); |
| 254 | xfrm_state_put(x); |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 259 | static int |
| 260 | vti_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
| 261 | { |
| 262 | int err = 0; |
| 263 | struct ip_tunnel_parm p; |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 264 | |
Amerigo Wang | b9959fd | 2013-07-21 10:46:25 +0800 | [diff] [blame] | 265 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) |
| 266 | return -EFAULT; |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 267 | |
Amerigo Wang | b9959fd | 2013-07-21 10:46:25 +0800 | [diff] [blame] | 268 | if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) { |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 269 | if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPIP || |
| 270 | p.iph.ihl != 5) |
Amerigo Wang | b9959fd | 2013-07-21 10:46:25 +0800 | [diff] [blame] | 271 | return -EINVAL; |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Steffen Klassert | df3893c | 2014-02-21 08:41:10 +0100 | [diff] [blame^] | 274 | p.i_flags |= VTI_ISVTI; |
Amerigo Wang | b9959fd | 2013-07-21 10:46:25 +0800 | [diff] [blame] | 275 | err = ip_tunnel_ioctl(dev, &p, cmd); |
| 276 | if (err) |
| 277 | return err; |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 278 | |
Amerigo Wang | b9959fd | 2013-07-21 10:46:25 +0800 | [diff] [blame] | 279 | if (cmd != SIOCDELTUNNEL) { |
Steffen Klassert | df3893c | 2014-02-21 08:41:10 +0100 | [diff] [blame^] | 280 | p.i_flags |= GRE_KEY; |
Amerigo Wang | b9959fd | 2013-07-21 10:46:25 +0800 | [diff] [blame] | 281 | p.o_flags |= GRE_KEY; |
| 282 | } |
| 283 | |
| 284 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) |
| 285 | return -EFAULT; |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | static const struct net_device_ops vti_netdev_ops = { |
| 290 | .ndo_init = vti_tunnel_init, |
Amerigo Wang | b9959fd | 2013-07-21 10:46:25 +0800 | [diff] [blame] | 291 | .ndo_uninit = ip_tunnel_uninit, |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 292 | .ndo_start_xmit = vti_tunnel_xmit, |
| 293 | .ndo_do_ioctl = vti_tunnel_ioctl, |
Amerigo Wang | b9959fd | 2013-07-21 10:46:25 +0800 | [diff] [blame] | 294 | .ndo_change_mtu = ip_tunnel_change_mtu, |
Pravin B Shelar | f61dd38 | 2013-03-25 14:50:00 +0000 | [diff] [blame] | 295 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 296 | }; |
| 297 | |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 298 | static void vti_tunnel_setup(struct net_device *dev) |
| 299 | { |
| 300 | dev->netdev_ops = &vti_netdev_ops; |
Amerigo Wang | b9959fd | 2013-07-21 10:46:25 +0800 | [diff] [blame] | 301 | ip_tunnel_setup(dev, vti_net_id); |
| 302 | } |
| 303 | |
| 304 | static int vti_tunnel_init(struct net_device *dev) |
| 305 | { |
| 306 | struct ip_tunnel *tunnel = netdev_priv(dev); |
| 307 | struct iphdr *iph = &tunnel->parms.iph; |
| 308 | |
| 309 | memcpy(dev->dev_addr, &iph->saddr, 4); |
| 310 | memcpy(dev->broadcast, &iph->daddr, 4); |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 311 | |
| 312 | dev->type = ARPHRD_TUNNEL; |
| 313 | dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr); |
| 314 | dev->mtu = ETH_DATA_LEN; |
| 315 | dev->flags = IFF_NOARP; |
| 316 | dev->iflink = 0; |
| 317 | dev->addr_len = 4; |
| 318 | dev->features |= NETIF_F_NETNS_LOCAL; |
| 319 | dev->features |= NETIF_F_LLTX; |
| 320 | dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; |
Amerigo Wang | b9959fd | 2013-07-21 10:46:25 +0800 | [diff] [blame] | 321 | |
| 322 | return ip_tunnel_init(dev); |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Amerigo Wang | b9959fd | 2013-07-21 10:46:25 +0800 | [diff] [blame] | 325 | static void __net_init vti_fb_tunnel_init(struct net_device *dev) |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 326 | { |
| 327 | struct ip_tunnel *tunnel = netdev_priv(dev); |
| 328 | struct iphdr *iph = &tunnel->parms.iph; |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 329 | |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 330 | iph->version = 4; |
| 331 | iph->protocol = IPPROTO_IPIP; |
| 332 | iph->ihl = 5; |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Steffen Klassert | df3893c | 2014-02-21 08:41:10 +0100 | [diff] [blame^] | 335 | static struct xfrm4_protocol vti_esp4_protocol __read_mostly = { |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 336 | .handler = vti_rcv, |
Steffen Klassert | df3893c | 2014-02-21 08:41:10 +0100 | [diff] [blame^] | 337 | .input_handler = vti_input, |
| 338 | .cb_handler = vti_rcv_cb, |
| 339 | .err_handler = vti4_err, |
| 340 | .priority = 100, |
| 341 | }; |
| 342 | |
| 343 | static struct xfrm4_protocol vti_ah4_protocol __read_mostly = { |
| 344 | .handler = vti_rcv, |
| 345 | .input_handler = vti_input, |
| 346 | .cb_handler = vti_rcv_cb, |
| 347 | .err_handler = vti4_err, |
| 348 | .priority = 100, |
| 349 | }; |
| 350 | |
| 351 | static struct xfrm4_protocol vti_ipcomp4_protocol __read_mostly = { |
| 352 | .handler = vti_rcv, |
| 353 | .input_handler = vti_input, |
| 354 | .cb_handler = vti_rcv_cb, |
| 355 | .err_handler = vti4_err, |
| 356 | .priority = 100, |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 357 | }; |
| 358 | |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 359 | static int __net_init vti_init_net(struct net *net) |
| 360 | { |
| 361 | int err; |
Amerigo Wang | b9959fd | 2013-07-21 10:46:25 +0800 | [diff] [blame] | 362 | struct ip_tunnel_net *itn; |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 363 | |
Amerigo Wang | b9959fd | 2013-07-21 10:46:25 +0800 | [diff] [blame] | 364 | err = ip_tunnel_init_net(net, vti_net_id, &vti_link_ops, "ip_vti0"); |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 365 | if (err) |
Amerigo Wang | b9959fd | 2013-07-21 10:46:25 +0800 | [diff] [blame] | 366 | return err; |
| 367 | itn = net_generic(net, vti_net_id); |
| 368 | vti_fb_tunnel_init(itn->fb_tunnel_dev); |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 369 | return 0; |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | static void __net_exit vti_exit_net(struct net *net) |
| 373 | { |
Amerigo Wang | b9959fd | 2013-07-21 10:46:25 +0800 | [diff] [blame] | 374 | struct ip_tunnel_net *itn = net_generic(net, vti_net_id); |
Nicolas Dichtel | 6c742e7 | 2013-08-13 17:51:11 +0200 | [diff] [blame] | 375 | ip_tunnel_delete_net(itn, &vti_link_ops); |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | static struct pernet_operations vti_net_ops = { |
| 379 | .init = vti_init_net, |
| 380 | .exit = vti_exit_net, |
| 381 | .id = &vti_net_id, |
Amerigo Wang | b9959fd | 2013-07-21 10:46:25 +0800 | [diff] [blame] | 382 | .size = sizeof(struct ip_tunnel_net), |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 383 | }; |
| 384 | |
| 385 | static int vti_tunnel_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 386 | { |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | static void vti_netlink_parms(struct nlattr *data[], |
| 391 | struct ip_tunnel_parm *parms) |
| 392 | { |
| 393 | memset(parms, 0, sizeof(*parms)); |
| 394 | |
| 395 | parms->iph.protocol = IPPROTO_IPIP; |
| 396 | |
| 397 | if (!data) |
| 398 | return; |
| 399 | |
Steffen Klassert | df3893c | 2014-02-21 08:41:10 +0100 | [diff] [blame^] | 400 | parms->i_flags = VTI_ISVTI; |
| 401 | |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 402 | if (data[IFLA_VTI_LINK]) |
| 403 | parms->link = nla_get_u32(data[IFLA_VTI_LINK]); |
| 404 | |
| 405 | if (data[IFLA_VTI_IKEY]) |
| 406 | parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]); |
| 407 | |
| 408 | if (data[IFLA_VTI_OKEY]) |
| 409 | parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]); |
| 410 | |
| 411 | if (data[IFLA_VTI_LOCAL]) |
| 412 | parms->iph.saddr = nla_get_be32(data[IFLA_VTI_LOCAL]); |
| 413 | |
| 414 | if (data[IFLA_VTI_REMOTE]) |
| 415 | parms->iph.daddr = nla_get_be32(data[IFLA_VTI_REMOTE]); |
| 416 | |
| 417 | } |
| 418 | |
| 419 | static int vti_newlink(struct net *src_net, struct net_device *dev, |
| 420 | struct nlattr *tb[], struct nlattr *data[]) |
| 421 | { |
Amerigo Wang | b9959fd | 2013-07-21 10:46:25 +0800 | [diff] [blame] | 422 | struct ip_tunnel_parm parms; |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 423 | |
Amerigo Wang | b9959fd | 2013-07-21 10:46:25 +0800 | [diff] [blame] | 424 | vti_netlink_parms(data, &parms); |
| 425 | return ip_tunnel_newlink(dev, tb, &parms); |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | static int vti_changelink(struct net_device *dev, struct nlattr *tb[], |
| 429 | struct nlattr *data[]) |
| 430 | { |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 431 | struct ip_tunnel_parm p; |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 432 | |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 433 | vti_netlink_parms(data, &p); |
Amerigo Wang | b9959fd | 2013-07-21 10:46:25 +0800 | [diff] [blame] | 434 | return ip_tunnel_changelink(dev, tb, &p); |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static size_t vti_get_size(const struct net_device *dev) |
| 438 | { |
| 439 | return |
| 440 | /* IFLA_VTI_LINK */ |
| 441 | nla_total_size(4) + |
| 442 | /* IFLA_VTI_IKEY */ |
| 443 | nla_total_size(4) + |
| 444 | /* IFLA_VTI_OKEY */ |
| 445 | nla_total_size(4) + |
| 446 | /* IFLA_VTI_LOCAL */ |
| 447 | nla_total_size(4) + |
| 448 | /* IFLA_VTI_REMOTE */ |
| 449 | nla_total_size(4) + |
| 450 | 0; |
| 451 | } |
| 452 | |
| 453 | static int vti_fill_info(struct sk_buff *skb, const struct net_device *dev) |
| 454 | { |
| 455 | struct ip_tunnel *t = netdev_priv(dev); |
| 456 | struct ip_tunnel_parm *p = &t->parms; |
| 457 | |
| 458 | nla_put_u32(skb, IFLA_VTI_LINK, p->link); |
| 459 | nla_put_be32(skb, IFLA_VTI_IKEY, p->i_key); |
| 460 | nla_put_be32(skb, IFLA_VTI_OKEY, p->o_key); |
| 461 | nla_put_be32(skb, IFLA_VTI_LOCAL, p->iph.saddr); |
| 462 | nla_put_be32(skb, IFLA_VTI_REMOTE, p->iph.daddr); |
| 463 | |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | static const struct nla_policy vti_policy[IFLA_VTI_MAX + 1] = { |
| 468 | [IFLA_VTI_LINK] = { .type = NLA_U32 }, |
| 469 | [IFLA_VTI_IKEY] = { .type = NLA_U32 }, |
| 470 | [IFLA_VTI_OKEY] = { .type = NLA_U32 }, |
| 471 | [IFLA_VTI_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) }, |
| 472 | [IFLA_VTI_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) }, |
| 473 | }; |
| 474 | |
| 475 | static struct rtnl_link_ops vti_link_ops __read_mostly = { |
| 476 | .kind = "vti", |
| 477 | .maxtype = IFLA_VTI_MAX, |
| 478 | .policy = vti_policy, |
| 479 | .priv_size = sizeof(struct ip_tunnel), |
| 480 | .setup = vti_tunnel_setup, |
| 481 | .validate = vti_tunnel_validate, |
| 482 | .newlink = vti_newlink, |
| 483 | .changelink = vti_changelink, |
| 484 | .get_size = vti_get_size, |
| 485 | .fill_info = vti_fill_info, |
| 486 | }; |
| 487 | |
| 488 | static int __init vti_init(void) |
| 489 | { |
| 490 | int err; |
| 491 | |
| 492 | pr_info("IPv4 over IPSec tunneling driver\n"); |
| 493 | |
| 494 | err = register_pernet_device(&vti_net_ops); |
| 495 | if (err < 0) |
| 496 | return err; |
Steffen Klassert | df3893c | 2014-02-21 08:41:10 +0100 | [diff] [blame^] | 497 | err = xfrm4_protocol_register(&vti_esp4_protocol, IPPROTO_ESP); |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 498 | if (err < 0) { |
| 499 | unregister_pernet_device(&vti_net_ops); |
Amerigo Wang | b9959fd | 2013-07-21 10:46:25 +0800 | [diff] [blame] | 500 | pr_info("vti init: can't register tunnel\n"); |
Steffen Klassert | df3893c | 2014-02-21 08:41:10 +0100 | [diff] [blame^] | 501 | |
| 502 | return err; |
| 503 | } |
| 504 | |
| 505 | err = xfrm4_protocol_register(&vti_ah4_protocol, IPPROTO_AH); |
| 506 | if (err < 0) { |
| 507 | xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP); |
| 508 | unregister_pernet_device(&vti_net_ops); |
| 509 | pr_info("vti init: can't register tunnel\n"); |
| 510 | |
| 511 | return err; |
| 512 | } |
| 513 | |
| 514 | err = xfrm4_protocol_register(&vti_ipcomp4_protocol, IPPROTO_COMP); |
| 515 | if (err < 0) { |
| 516 | xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH); |
| 517 | xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP); |
| 518 | unregister_pernet_device(&vti_net_ops); |
| 519 | pr_info("vti init: can't register tunnel\n"); |
| 520 | |
| 521 | return err; |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | err = rtnl_link_register(&vti_link_ops); |
| 525 | if (err < 0) |
| 526 | goto rtnl_link_failed; |
| 527 | |
| 528 | return err; |
| 529 | |
| 530 | rtnl_link_failed: |
Steffen Klassert | df3893c | 2014-02-21 08:41:10 +0100 | [diff] [blame^] | 531 | xfrm4_protocol_deregister(&vti_ipcomp4_protocol, IPPROTO_COMP); |
| 532 | xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH); |
| 533 | xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP); |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 534 | unregister_pernet_device(&vti_net_ops); |
| 535 | return err; |
| 536 | } |
| 537 | |
| 538 | static void __exit vti_fini(void) |
| 539 | { |
| 540 | rtnl_link_unregister(&vti_link_ops); |
Steffen Klassert | df3893c | 2014-02-21 08:41:10 +0100 | [diff] [blame^] | 541 | if (xfrm4_protocol_deregister(&vti_ipcomp4_protocol, IPPROTO_COMP)) |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 542 | pr_info("vti close: can't deregister tunnel\n"); |
Steffen Klassert | df3893c | 2014-02-21 08:41:10 +0100 | [diff] [blame^] | 543 | if (xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH)) |
| 544 | pr_info("vti close: can't deregister tunnel\n"); |
| 545 | if (xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP)) |
| 546 | pr_info("vti close: can't deregister tunnel\n"); |
| 547 | |
Saurabh | 1181412 | 2012-07-17 09:44:54 +0000 | [diff] [blame] | 548 | |
| 549 | unregister_pernet_device(&vti_net_ops); |
| 550 | } |
| 551 | |
| 552 | module_init(vti_init); |
| 553 | module_exit(vti_fini); |
| 554 | MODULE_LICENSE("GPL"); |
| 555 | MODULE_ALIAS_RTNL_LINK("vti"); |
| 556 | MODULE_ALIAS_NETDEV("ip_vti0"); |