^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Linux NET3: IP/IP protocol decoder modified to support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * virtual tunnel interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Saurabh Mohan (saurabh.mohan@vyatta.com) 05/07/2012
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) This version of net/ipv4/ip_vti.c is cloned of net/ipv4/ipip.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) For comments look at net/ipv4/ip_gre.c --ANK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/in.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/tcp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/udp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/if_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/netfilter_ipv4.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/if_ether.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/icmpv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <net/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <net/icmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <net/ip_tunnels.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <net/inet_ecn.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <net/xfrm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <net/net_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <net/netns/generic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static struct rtnl_link_ops vti_link_ops __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static unsigned int vti_net_id __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static int vti_tunnel_init(struct net_device *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static int vti_input(struct sk_buff *skb, int nexthdr, __be32 spi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int encap_type, bool update_skb_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct ip_tunnel *tunnel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) const struct iphdr *iph = ip_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct net *net = dev_net(skb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) iph->saddr, iph->daddr, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (tunnel) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = tunnel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (update_skb_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) skb->dev = tunnel->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return xfrm_input(skb, nexthdr, spi, encap_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static int vti_input_proto(struct sk_buff *skb, int nexthdr, __be32 spi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) int encap_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return vti_input(skb, nexthdr, spi, encap_type, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static int vti_rcv(struct sk_buff *skb, __be32 spi, bool update_skb_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) XFRM_SPI_SKB_CB(skb)->family = AF_INET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return vti_input(skb, ip_hdr(skb)->protocol, spi, 0, update_skb_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static int vti_rcv_proto(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return vti_rcv(skb, 0, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) static int vti_rcv_cb(struct sk_buff *skb, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) unsigned short family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct xfrm_state *x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) const struct xfrm_mode *inner_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct ip_tunnel *tunnel = XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) u32 orig_mark = skb->mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (!tunnel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) dev = tunnel->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) dev->stats.rx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) dev->stats.rx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) x = xfrm_input_state(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) inner_mode = &x->inner_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (x->sel.family == AF_UNSPEC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (inner_mode == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) XFRM_INC_STATS(dev_net(skb->dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) LINUX_MIB_XFRMINSTATEMODEERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) family = inner_mode->family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) skb->mark = be32_to_cpu(tunnel->parms.i_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) ret = xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) skb->mark = orig_mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(skb->dev)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) skb->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) dev_sw_netstats_rx_add(dev, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static bool vti_state_check(const struct xfrm_state *x, __be32 dst, __be32 src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) xfrm_address_t *daddr = (xfrm_address_t *)&dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) xfrm_address_t *saddr = (xfrm_address_t *)&src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /* if there is no transform then this tunnel is not functional.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * Or if the xfrm is not mode tunnel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (!x || x->props.mode != XFRM_MODE_TUNNEL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) x->props.family != AF_INET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (!dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return xfrm_addr_equal(saddr, &x->props.saddr, AF_INET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (!xfrm_state_addr_check(x, daddr, saddr, AF_INET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static netdev_tx_t vti_xmit(struct sk_buff *skb, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct flowi *fl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct ip_tunnel_parm *parms = &tunnel->parms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) struct dst_entry *dst = skb_dst(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) struct net_device *tdev; /* Device to other host */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) int pkt_len = skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) int mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (!dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) switch (skb->protocol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) case htons(ETH_P_IP): {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) fl->u.ip4.flowi4_oif = dev->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) fl->u.ip4.flowi4_flags |= FLOWI_FLAG_ANYSRC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) rt = __ip_route_output_key(dev_net(dev), &fl->u.ip4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (IS_ERR(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) dev->stats.tx_carrier_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) goto tx_error_icmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) dst = &rt->dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) skb_dst_set(skb, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) case htons(ETH_P_IPV6):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) fl->u.ip6.flowi6_oif = dev->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) fl->u.ip6.flowi6_flags |= FLOWI_FLAG_ANYSRC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) dst = ip6_route_output(dev_net(dev), NULL, &fl->u.ip6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (dst->error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) dst_release(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) dst = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) dev->stats.tx_carrier_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) goto tx_error_icmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) skb_dst_set(skb, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) dev->stats.tx_carrier_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) goto tx_error_icmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) dst_hold(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) dst = xfrm_lookup_route(tunnel->net, dst, fl, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (IS_ERR(dst)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) dev->stats.tx_carrier_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) goto tx_error_icmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (dst->flags & DST_XFRM_QUEUE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) goto queued;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (!vti_state_check(dst->xfrm, parms->iph.daddr, parms->iph.saddr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) dev->stats.tx_carrier_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) dst_release(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) goto tx_error_icmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) tdev = dst->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (tdev == dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) dst_release(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) dev->stats.collisions++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) mtu = dst_mtu(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (skb->len > mtu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) skb_dst_update_pmtu_no_confirm(skb, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (skb->protocol == htons(ETH_P_IP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) htonl(mtu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (mtu < IPV6_MIN_MTU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) mtu = IPV6_MIN_MTU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) dst_release(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) queued:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(dev)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) skb_dst_set(skb, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) skb->dev = skb_dst(skb)->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) err = dst_output(tunnel->net, skb->sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (net_xmit_eval(err) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) err = pkt_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) iptunnel_xmit_stats(dev, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) tx_error_icmp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) dst_link_failure(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) tx_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) dev->stats.tx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) /* This function assumes it is being called from dev_queue_xmit()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * and that skb is filled properly by that function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) static netdev_tx_t vti_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) struct flowi fl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (!pskb_inet_may_pull(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) goto tx_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) memset(&fl, 0, sizeof(fl));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) switch (skb->protocol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) case htons(ETH_P_IP):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) xfrm_decode_session(skb, &fl, AF_INET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) case htons(ETH_P_IPV6):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) xfrm_decode_session(skb, &fl, AF_INET6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) goto tx_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) /* override mark with tunnel output key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) fl.flowi_mark = be32_to_cpu(tunnel->parms.o_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) return vti_xmit(skb, dev, &fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) tx_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) dev->stats.tx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static int vti4_err(struct sk_buff *skb, u32 info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) __be32 spi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) __u32 mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) struct xfrm_state *x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) struct ip_tunnel *tunnel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) struct ip_esp_hdr *esph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) struct ip_auth_hdr *ah ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) struct ip_comp_hdr *ipch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) struct net *net = dev_net(skb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) const struct iphdr *iph = (const struct iphdr *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) int protocol = iph->protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) iph->daddr, iph->saddr, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (!tunnel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) mark = be32_to_cpu(tunnel->parms.o_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) switch (protocol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) case IPPROTO_ESP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) spi = esph->spi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) case IPPROTO_AH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) spi = ah->spi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) case IPPROTO_COMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) spi = htonl(ntohs(ipch->cpi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) switch (icmp_hdr(skb)->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) case ICMP_DEST_UNREACH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) case ICMP_REDIRECT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) x = xfrm_state_lookup(net, mark, (const xfrm_address_t *)&iph->daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) spi, protocol, AF_INET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) if (!x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) ipv4_update_pmtu(skb, net, info, 0, protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) ipv4_redirect(skb, net, 0, protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) xfrm_state_put(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) vti_tunnel_ctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (p->iph.version != 4 || p->iph.protocol != IPPROTO_IPIP ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) p->iph.ihl != 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (!(p->i_flags & GRE_KEY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) p->i_key = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) if (!(p->o_flags & GRE_KEY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) p->o_key = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) p->i_flags = VTI_ISVTI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) err = ip_tunnel_ctl(dev, p, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) if (cmd != SIOCDELTUNNEL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) p->i_flags |= GRE_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) p->o_flags |= GRE_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) static const struct net_device_ops vti_netdev_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) .ndo_init = vti_tunnel_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) .ndo_uninit = ip_tunnel_uninit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) .ndo_start_xmit = vti_tunnel_xmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) .ndo_do_ioctl = ip_tunnel_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) .ndo_change_mtu = ip_tunnel_change_mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) .ndo_get_stats64 = ip_tunnel_get_stats64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) .ndo_get_iflink = ip_tunnel_get_iflink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) .ndo_tunnel_ctl = vti_tunnel_ctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) static void vti_tunnel_setup(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) dev->netdev_ops = &vti_netdev_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) dev->header_ops = &ip_tunnel_header_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) dev->type = ARPHRD_TUNNEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) ip_tunnel_setup(dev, vti_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) static int vti_tunnel_init(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) struct iphdr *iph = &tunnel->parms.iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) memcpy(dev->dev_addr, &iph->saddr, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) memcpy(dev->broadcast, &iph->daddr, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) dev->flags = IFF_NOARP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) dev->addr_len = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) dev->features |= NETIF_F_LLTX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) netif_keep_dst(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) return ip_tunnel_init(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) static void __net_init vti_fb_tunnel_init(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) struct iphdr *iph = &tunnel->parms.iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) iph->version = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) iph->protocol = IPPROTO_IPIP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) iph->ihl = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) static struct xfrm4_protocol vti_esp4_protocol __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) .handler = vti_rcv_proto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) .input_handler = vti_input_proto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) .cb_handler = vti_rcv_cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) .err_handler = vti4_err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) .priority = 100,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) static struct xfrm4_protocol vti_ah4_protocol __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) .handler = vti_rcv_proto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) .input_handler = vti_input_proto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) .cb_handler = vti_rcv_cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) .err_handler = vti4_err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) .priority = 100,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) static struct xfrm4_protocol vti_ipcomp4_protocol __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) .handler = vti_rcv_proto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) .input_handler = vti_input_proto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) .cb_handler = vti_rcv_cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) .err_handler = vti4_err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) .priority = 100,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) static int vti_rcv_tunnel(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) XFRM_SPI_SKB_CB(skb)->family = AF_INET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) return vti_input(skb, IPPROTO_IPIP, ip_hdr(skb)->saddr, 0, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) static struct xfrm_tunnel vti_ipip_handler __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) .handler = vti_rcv_tunnel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) .cb_handler = vti_rcv_cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) .err_handler = vti4_err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) .priority = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) static struct xfrm_tunnel vti_ipip6_handler __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) .handler = vti_rcv_tunnel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) .cb_handler = vti_rcv_cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) .err_handler = vti4_err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) .priority = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) static int __net_init vti_init_net(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) struct ip_tunnel_net *itn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) err = ip_tunnel_init_net(net, vti_net_id, &vti_link_ops, "ip_vti0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) itn = net_generic(net, vti_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) if (itn->fb_tunnel_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) vti_fb_tunnel_init(itn->fb_tunnel_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) static void __net_exit vti_exit_batch_net(struct list_head *list_net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) ip_tunnel_delete_nets(list_net, vti_net_id, &vti_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) static struct pernet_operations vti_net_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) .init = vti_init_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) .exit_batch = vti_exit_batch_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) .id = &vti_net_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) .size = sizeof(struct ip_tunnel_net),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) static int vti_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) static void vti_netlink_parms(struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) struct ip_tunnel_parm *parms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) __u32 *fwmark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) memset(parms, 0, sizeof(*parms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) parms->iph.protocol = IPPROTO_IPIP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) parms->i_flags = VTI_ISVTI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) if (data[IFLA_VTI_LINK])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) if (data[IFLA_VTI_IKEY])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) if (data[IFLA_VTI_OKEY])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) if (data[IFLA_VTI_LOCAL])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) parms->iph.saddr = nla_get_in_addr(data[IFLA_VTI_LOCAL]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) if (data[IFLA_VTI_REMOTE])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) parms->iph.daddr = nla_get_in_addr(data[IFLA_VTI_REMOTE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) if (data[IFLA_VTI_FWMARK])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) *fwmark = nla_get_u32(data[IFLA_VTI_FWMARK]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) static int vti_newlink(struct net *src_net, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) struct nlattr *tb[], struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) struct ip_tunnel_parm parms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) __u32 fwmark = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) vti_netlink_parms(data, &parms, &fwmark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) return ip_tunnel_newlink(dev, tb, &parms, fwmark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) static int vti_changelink(struct net_device *dev, struct nlattr *tb[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) __u32 fwmark = t->fwmark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) struct ip_tunnel_parm p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) vti_netlink_parms(data, &p, &fwmark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) return ip_tunnel_changelink(dev, tb, &p, fwmark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) static size_t vti_get_size(const struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) /* IFLA_VTI_LINK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) nla_total_size(4) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) /* IFLA_VTI_IKEY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) nla_total_size(4) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) /* IFLA_VTI_OKEY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) nla_total_size(4) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) /* IFLA_VTI_LOCAL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) nla_total_size(4) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) /* IFLA_VTI_REMOTE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) nla_total_size(4) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) /* IFLA_VTI_FWMARK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) nla_total_size(4) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) static int vti_fill_info(struct sk_buff *skb, const struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) struct ip_tunnel_parm *p = &t->parms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) if (nla_put_u32(skb, IFLA_VTI_LINK, p->link) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) nla_put_be32(skb, IFLA_VTI_IKEY, p->i_key) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) nla_put_be32(skb, IFLA_VTI_OKEY, p->o_key) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) nla_put_in_addr(skb, IFLA_VTI_LOCAL, p->iph.saddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) nla_put_in_addr(skb, IFLA_VTI_REMOTE, p->iph.daddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) nla_put_u32(skb, IFLA_VTI_FWMARK, t->fwmark))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) static const struct nla_policy vti_policy[IFLA_VTI_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) [IFLA_VTI_LINK] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) [IFLA_VTI_IKEY] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) [IFLA_VTI_OKEY] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) [IFLA_VTI_LOCAL] = { .len = sizeof_field(struct iphdr, saddr) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) [IFLA_VTI_REMOTE] = { .len = sizeof_field(struct iphdr, daddr) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) [IFLA_VTI_FWMARK] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) static struct rtnl_link_ops vti_link_ops __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) .kind = "vti",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) .maxtype = IFLA_VTI_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) .policy = vti_policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) .priv_size = sizeof(struct ip_tunnel),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) .setup = vti_tunnel_setup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) .validate = vti_tunnel_validate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) .newlink = vti_newlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) .changelink = vti_changelink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) .dellink = ip_tunnel_dellink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) .get_size = vti_get_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) .fill_info = vti_fill_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) .get_link_net = ip_tunnel_get_link_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) static int __init vti_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) const char *msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) pr_info("IPv4 over IPsec tunneling driver\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) msg = "tunnel device";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) err = register_pernet_device(&vti_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) goto pernet_dev_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) msg = "tunnel protocols";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) err = xfrm4_protocol_register(&vti_esp4_protocol, IPPROTO_ESP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) goto xfrm_proto_esp_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) err = xfrm4_protocol_register(&vti_ah4_protocol, IPPROTO_AH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) goto xfrm_proto_ah_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) err = xfrm4_protocol_register(&vti_ipcomp4_protocol, IPPROTO_COMP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) goto xfrm_proto_comp_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) msg = "ipip tunnel";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) err = xfrm4_tunnel_register(&vti_ipip_handler, AF_INET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) goto xfrm_tunnel_ipip_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) err = xfrm4_tunnel_register(&vti_ipip6_handler, AF_INET6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) goto xfrm_tunnel_ipip6_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) msg = "netlink interface";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) err = rtnl_link_register(&vti_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) goto rtnl_link_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) rtnl_link_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) xfrm4_tunnel_deregister(&vti_ipip6_handler, AF_INET6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) xfrm_tunnel_ipip6_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) xfrm4_tunnel_deregister(&vti_ipip_handler, AF_INET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) xfrm_tunnel_ipip_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) xfrm4_protocol_deregister(&vti_ipcomp4_protocol, IPPROTO_COMP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) xfrm_proto_comp_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) xfrm_proto_ah_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) xfrm_proto_esp_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) unregister_pernet_device(&vti_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) pernet_dev_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) pr_err("vti init: failed to register %s\n", msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) static void __exit vti_fini(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) rtnl_link_unregister(&vti_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) xfrm4_tunnel_deregister(&vti_ipip6_handler, AF_INET6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) xfrm4_tunnel_deregister(&vti_ipip_handler, AF_INET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) xfrm4_protocol_deregister(&vti_ipcomp4_protocol, IPPROTO_COMP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) unregister_pernet_device(&vti_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) module_init(vti_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) module_exit(vti_fini);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) MODULE_ALIAS_RTNL_LINK("vti");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) MODULE_ALIAS_NETDEV("ip_vti0");