^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.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Sam Lantinga (slouken@cs.ucdavis.edu) 02/01/95
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Fixes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Alan Cox : Merged and made usable non modular (its so tiny its silly as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * a module taking up 2 pages).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Alan Cox : Fixed bug with 1.3.18 and IPIP not working (now needs to set skb->h.iph)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * to keep ip_forward happy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Alan Cox : More fixes for 1.3.21, and firewall fix. Maybe this will work soon 8).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Kai Schulte : Fixed #defines for IP_FIREWALL->FIREWALL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * David Woodhouse : Perform some basic ICMP handling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * IPIP Routing without decapsulation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * Carlos Picoto : GRE over IP support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * Alexey Kuznetsov: Reworked. Really, now it is truncated version of ipv4/ip_gre.c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * I do not want to merge them together.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /* tunnel.c: an IP tunnel driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) The purpose of this driver is to provide an IP tunnel through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) which you can tunnel network traffic transparently across subnets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) This was written by looking at Nick Holloway's dummy driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) Thanks for the great code!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) -Sam Lantinga (slouken@cs.ucdavis.edu) 02/01/95
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) Minor tweaks:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) Cleaned up the code a little and added some pre-1.3.0 tweaks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) dev->hard_header/hard_header_len changed to use no headers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) Comments/bracketing tweaked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) Made the tunnels use dev->name not tunnel: when error reporting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) Added tx_dropped stat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) -Alan Cox (alan@lxorguk.ukuu.org.uk) 21 March 95
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) Reworked:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) Changed to tunnel to destination gateway in addition to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) tunnel's pointopoint address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) Almost completely rewritten
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) Note: There is currently no firewall or ICMP handling done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) -Sam Lantinga (slouken@cs.ucdavis.edu) 02/13/96
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /* Things I wish I had known when writing the tunnel driver:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) When the tunnel_xmit() function is called, the skb contains the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) packet to be sent (plus a great deal of extra info), and dev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) contains the tunnel device that _we_ are.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) When we are passed a packet, we are expected to fill in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) source address with our source IP address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) What is the proper way to allocate, copy and free a buffer?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) After you allocate it, it is a "0 length" chunk of memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) starting at zero. If you want to add headers to the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) later, you'll have to call "skb_reserve(skb, amount)" with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) the amount of memory you want reserved. Then, you call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) "skb_put(skb, amount)" with the amount of space you want in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) the buffer. skb_put() returns a pointer to the top (#0) of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) that buffer. skb->len is set to the amount of space you have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) "allocated" with skb_put(). You can then write up to skb->len
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) bytes to that buffer. If you need more, you can call skb_put()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) again with the additional amount of space you need. You can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) find out how much more space you can allocate by calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) "skb_tailroom(skb)".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) Now, to add header space, call "skb_push(skb, header_len)".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) This creates space at the beginning of the buffer and returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) a pointer to this new space. If later you need to strip a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) header from a buffer, call "skb_pull(skb, header_len)".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) skb_headroom() will return how much space is left at the top
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) of the buffer (before the main data). Remember, this headroom
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) space must be reserved before the skb_put() function is called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) This version of net/ipv4/ipip.c is cloned of net/ipv4/ip_gre.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) For comments look at net/ipv4/ip_gre.c --ANK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) */
^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) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) #include <linux/in.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) #include <linux/tcp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) #include <linux/udp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #include <linux/if_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) #include <linux/netfilter_ipv4.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) #include <linux/if_ether.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) #include <net/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #include <net/icmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) #include <net/ip_tunnels.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) #include <net/inet_ecn.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) #include <net/xfrm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) #include <net/net_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) #include <net/netns/generic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) #include <net/dst_metadata.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static bool log_ecn_error = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) module_param(log_ecn_error, bool, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static unsigned int ipip_net_id __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static int ipip_tunnel_init(struct net_device *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static struct rtnl_link_ops ipip_link_ops __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static int ipip_err(struct sk_buff *skb, u32 info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /* All the routers (except for Linux) return only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * 8 bytes of packet payload. It means, that precise relaying of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * ICMP in the real Internet is absolutely infeasible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct net *net = dev_net(skb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct ip_tunnel_net *itn = net_generic(net, ipip_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) const struct iphdr *iph = (const struct iphdr *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) const int type = icmp_hdr(skb)->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) const int code = icmp_hdr(skb)->code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct ip_tunnel *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) t = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) iph->daddr, iph->saddr, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (!t) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) goto out;
^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) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) case ICMP_DEST_UNREACH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) switch (code) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) case ICMP_SR_FAILED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /* Impossible event. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /* All others are translated to HOST_UNREACH.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * rfc2003 contains "deep thoughts" about NET_UNREACH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * I believe they are just ether pollution. --ANK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) case ICMP_TIME_EXCEEDED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (code != ICMP_EXC_TTL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) case ICMP_REDIRECT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) ipv4_update_pmtu(skb, net, info, t->parms.link, iph->protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (type == ICMP_REDIRECT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) ipv4_redirect(skb, net, t->parms.link, iph->protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (t->parms.iph.daddr == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) t->err_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) t->err_count = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) t->err_time = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static const struct tnl_ptk_info ipip_tpi = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) /* no tunnel info required for ipip. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) .proto = htons(ETH_P_IP),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) #if IS_ENABLED(CONFIG_MPLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static const struct tnl_ptk_info mplsip_tpi = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) /* no tunnel info required for mplsip. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) .proto = htons(ETH_P_MPLS_UC),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) static int ipip_tunnel_rcv(struct sk_buff *skb, u8 ipproto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) struct net *net = dev_net(skb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct ip_tunnel_net *itn = net_generic(net, ipip_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) struct metadata_dst *tun_dst = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) struct ip_tunnel *tunnel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) const struct iphdr *iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) iph = ip_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) iph->saddr, iph->daddr, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (tunnel) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) const struct tnl_ptk_info *tpi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (tunnel->parms.iph.protocol != ipproto &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) tunnel->parms.iph.protocol != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) #if IS_ENABLED(CONFIG_MPLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (ipproto == IPPROTO_MPLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) tpi = &mplsip_tpi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) tpi = &ipip_tpi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (iptunnel_pull_header(skb, 0, tpi->proto, false))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (tunnel->collect_md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) tun_dst = ip_tun_rx_dst(skb, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (!tun_dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) return ip_tunnel_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) static int ipip_rcv(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) return ipip_tunnel_rcv(skb, IPPROTO_IPIP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) #if IS_ENABLED(CONFIG_MPLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) static int mplsip_rcv(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return ipip_tunnel_rcv(skb, IPPROTO_MPLS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * This function assumes it is being called from dev_queue_xmit()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * and that skb is filled properly by that function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) static netdev_tx_t ipip_tunnel_xmit(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) const struct iphdr *tiph = &tunnel->parms.iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) u8 ipproto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (!pskb_inet_may_pull(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) switch (skb->protocol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) case htons(ETH_P_IP):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) ipproto = IPPROTO_IPIP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) #if IS_ENABLED(CONFIG_MPLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) case htons(ETH_P_MPLS_UC):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) ipproto = IPPROTO_MPLS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (tiph->protocol != ipproto && tiph->protocol != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) skb_set_inner_ipproto(skb, ipproto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (tunnel->collect_md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) ip_md_tunnel_xmit(skb, dev, ipproto, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) ip_tunnel_xmit(skb, dev, tiph, ipproto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) tx_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) dev->stats.tx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) static bool ipip_tunnel_ioctl_verify_protocol(u8 ipproto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) switch (ipproto) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) case IPPROTO_IPIP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) #if IS_ENABLED(CONFIG_MPLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) case IPPROTO_MPLS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) ipip_tunnel_ctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (p->iph.version != 4 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) !ipip_tunnel_ioctl_verify_protocol(p->iph.protocol) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) p->iph.ihl != 5 || (p->iph.frag_off & htons(~IP_DF)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) p->i_key = p->o_key = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) p->i_flags = p->o_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return ip_tunnel_ctl(dev, p, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) static const struct net_device_ops ipip_netdev_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) .ndo_init = ipip_tunnel_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) .ndo_uninit = ip_tunnel_uninit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) .ndo_start_xmit = ipip_tunnel_xmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) .ndo_do_ioctl = ip_tunnel_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) .ndo_change_mtu = ip_tunnel_change_mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) .ndo_get_stats64 = ip_tunnel_get_stats64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) .ndo_get_iflink = ip_tunnel_get_iflink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) .ndo_tunnel_ctl = ipip_tunnel_ctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) #define IPIP_FEATURES (NETIF_F_SG | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) NETIF_F_FRAGLIST | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) NETIF_F_HIGHDMA | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) NETIF_F_GSO_SOFTWARE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) NETIF_F_HW_CSUM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) static void ipip_tunnel_setup(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) dev->netdev_ops = &ipip_netdev_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) dev->header_ops = &ip_tunnel_header_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) dev->type = ARPHRD_TUNNEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) dev->flags = IFF_NOARP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) dev->addr_len = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) dev->features |= NETIF_F_LLTX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) netif_keep_dst(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) dev->features |= IPIP_FEATURES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) dev->hw_features |= IPIP_FEATURES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) ip_tunnel_setup(dev, ipip_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) static int ipip_tunnel_init(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) tunnel->tun_hlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) return ip_tunnel_init(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) static int ipip_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) u8 proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) if (!data || !data[IFLA_IPTUN_PROTO])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (proto != IPPROTO_IPIP && proto != IPPROTO_MPLS && proto != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) static void ipip_netlink_parms(struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) struct ip_tunnel_parm *parms, bool *collect_md,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) __u32 *fwmark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) memset(parms, 0, sizeof(*parms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) parms->iph.version = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) parms->iph.protocol = IPPROTO_IPIP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) parms->iph.ihl = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) *collect_md = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (data[IFLA_IPTUN_LINK])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) if (data[IFLA_IPTUN_LOCAL])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) parms->iph.saddr = nla_get_in_addr(data[IFLA_IPTUN_LOCAL]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) if (data[IFLA_IPTUN_REMOTE])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) parms->iph.daddr = nla_get_in_addr(data[IFLA_IPTUN_REMOTE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) if (data[IFLA_IPTUN_TTL]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) parms->iph.ttl = nla_get_u8(data[IFLA_IPTUN_TTL]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) if (parms->iph.ttl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) parms->iph.frag_off = htons(IP_DF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) if (data[IFLA_IPTUN_TOS])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) parms->iph.tos = nla_get_u8(data[IFLA_IPTUN_TOS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) if (data[IFLA_IPTUN_PROTO])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) parms->iph.protocol = nla_get_u8(data[IFLA_IPTUN_PROTO]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) if (!data[IFLA_IPTUN_PMTUDISC] || nla_get_u8(data[IFLA_IPTUN_PMTUDISC]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) parms->iph.frag_off = htons(IP_DF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) if (data[IFLA_IPTUN_COLLECT_METADATA])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) *collect_md = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) if (data[IFLA_IPTUN_FWMARK])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) *fwmark = nla_get_u32(data[IFLA_IPTUN_FWMARK]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) /* This function returns true when ENCAP attributes are present in the nl msg */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) static bool ipip_netlink_encap_parms(struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) struct ip_tunnel_encap *ipencap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) bool ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) memset(ipencap, 0, sizeof(*ipencap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) if (data[IFLA_IPTUN_ENCAP_TYPE]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) ipencap->type = nla_get_u16(data[IFLA_IPTUN_ENCAP_TYPE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (data[IFLA_IPTUN_ENCAP_FLAGS]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) ipencap->flags = nla_get_u16(data[IFLA_IPTUN_ENCAP_FLAGS]);
^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 (data[IFLA_IPTUN_ENCAP_SPORT]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) ipencap->sport = nla_get_be16(data[IFLA_IPTUN_ENCAP_SPORT]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) if (data[IFLA_IPTUN_ENCAP_DPORT]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) ipencap->dport = nla_get_be16(data[IFLA_IPTUN_ENCAP_DPORT]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) static int ipip_newlink(struct net *src_net, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) struct nlattr *tb[], struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) struct ip_tunnel_parm p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) struct ip_tunnel_encap ipencap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) __u32 fwmark = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) if (ipip_netlink_encap_parms(data, &ipencap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) int err = ip_tunnel_encap_setup(t, &ipencap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) ipip_netlink_parms(data, &p, &t->collect_md, &fwmark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) return ip_tunnel_newlink(dev, tb, &p, fwmark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) static int ipip_changelink(struct net_device *dev, struct nlattr *tb[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) struct ip_tunnel_parm p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) struct ip_tunnel_encap ipencap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) bool collect_md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) __u32 fwmark = t->fwmark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) if (ipip_netlink_encap_parms(data, &ipencap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) int err = ip_tunnel_encap_setup(t, &ipencap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) ipip_netlink_parms(data, &p, &collect_md, &fwmark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) if (collect_md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) if (((dev->flags & IFF_POINTOPOINT) && !p.iph.daddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) (!(dev->flags & IFF_POINTOPOINT) && p.iph.daddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) return ip_tunnel_changelink(dev, tb, &p, fwmark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) static size_t ipip_get_size(const struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) /* IFLA_IPTUN_LINK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) nla_total_size(4) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) /* IFLA_IPTUN_LOCAL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) nla_total_size(4) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) /* IFLA_IPTUN_REMOTE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) nla_total_size(4) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) /* IFLA_IPTUN_TTL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) nla_total_size(1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) /* IFLA_IPTUN_TOS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) nla_total_size(1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) /* IFLA_IPTUN_PROTO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) nla_total_size(1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) /* IFLA_IPTUN_PMTUDISC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) nla_total_size(1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) /* IFLA_IPTUN_ENCAP_TYPE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) nla_total_size(2) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) /* IFLA_IPTUN_ENCAP_FLAGS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) nla_total_size(2) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) /* IFLA_IPTUN_ENCAP_SPORT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) nla_total_size(2) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) /* IFLA_IPTUN_ENCAP_DPORT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) nla_total_size(2) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) /* IFLA_IPTUN_COLLECT_METADATA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) nla_total_size(0) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) /* IFLA_IPTUN_FWMARK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) nla_total_size(4) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) static int ipip_fill_info(struct sk_buff *skb, const struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) struct ip_tunnel_parm *parm = &tunnel->parms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) nla_put_in_addr(skb, IFLA_IPTUN_LOCAL, parm->iph.saddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) nla_put_in_addr(skb, IFLA_IPTUN_REMOTE, parm->iph.daddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) nla_put_u8(skb, IFLA_IPTUN_TTL, parm->iph.ttl) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) nla_put_u8(skb, IFLA_IPTUN_TOS, parm->iph.tos) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->iph.protocol) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) nla_put_u8(skb, IFLA_IPTUN_PMTUDISC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) !!(parm->iph.frag_off & htons(IP_DF))) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) nla_put_u32(skb, IFLA_IPTUN_FWMARK, tunnel->fwmark))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) if (nla_put_u16(skb, IFLA_IPTUN_ENCAP_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) tunnel->encap.type) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) nla_put_be16(skb, IFLA_IPTUN_ENCAP_SPORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) tunnel->encap.sport) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) nla_put_be16(skb, IFLA_IPTUN_ENCAP_DPORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) tunnel->encap.dport) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) nla_put_u16(skb, IFLA_IPTUN_ENCAP_FLAGS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) tunnel->encap.flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) if (tunnel->collect_md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) if (nla_put_flag(skb, IFLA_IPTUN_COLLECT_METADATA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) static const struct nla_policy ipip_policy[IFLA_IPTUN_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) [IFLA_IPTUN_LOCAL] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) [IFLA_IPTUN_REMOTE] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) [IFLA_IPTUN_TOS] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) [IFLA_IPTUN_PMTUDISC] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) [IFLA_IPTUN_ENCAP_TYPE] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) [IFLA_IPTUN_ENCAP_FLAGS] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) [IFLA_IPTUN_ENCAP_SPORT] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) [IFLA_IPTUN_ENCAP_DPORT] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) [IFLA_IPTUN_COLLECT_METADATA] = { .type = NLA_FLAG },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) [IFLA_IPTUN_FWMARK] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) static struct rtnl_link_ops ipip_link_ops __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) .kind = "ipip",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) .maxtype = IFLA_IPTUN_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) .policy = ipip_policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) .priv_size = sizeof(struct ip_tunnel),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) .setup = ipip_tunnel_setup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) .validate = ipip_tunnel_validate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) .newlink = ipip_newlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) .changelink = ipip_changelink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) .dellink = ip_tunnel_dellink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) .get_size = ipip_get_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) .fill_info = ipip_fill_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) .get_link_net = ip_tunnel_get_link_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) static struct xfrm_tunnel ipip_handler __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) .handler = ipip_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) .err_handler = ipip_err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) .priority = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) #if IS_ENABLED(CONFIG_MPLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) static struct xfrm_tunnel mplsip_handler __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) .handler = mplsip_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) .err_handler = ipip_err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) .priority = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) static int __net_init ipip_init_net(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) return ip_tunnel_init_net(net, ipip_net_id, &ipip_link_ops, "tunl0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) static void __net_exit ipip_exit_batch_net(struct list_head *list_net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) ip_tunnel_delete_nets(list_net, ipip_net_id, &ipip_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) static struct pernet_operations ipip_net_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) .init = ipip_init_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) .exit_batch = ipip_exit_batch_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) .id = &ipip_net_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) .size = sizeof(struct ip_tunnel_net),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) static int __init ipip_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) pr_info("ipip: IPv4 and MPLS over IPv4 tunneling driver\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) err = register_pernet_device(&ipip_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) err = xfrm4_tunnel_register(&ipip_handler, AF_INET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) pr_info("%s: can't register tunnel\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) goto xfrm_tunnel_ipip_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) #if IS_ENABLED(CONFIG_MPLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) err = xfrm4_tunnel_register(&mplsip_handler, AF_MPLS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) pr_info("%s: can't register tunnel\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) goto xfrm_tunnel_mplsip_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) err = rtnl_link_register(&ipip_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) goto rtnl_link_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) rtnl_link_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) #if IS_ENABLED(CONFIG_MPLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) xfrm4_tunnel_deregister(&mplsip_handler, AF_MPLS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) xfrm_tunnel_mplsip_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) xfrm_tunnel_ipip_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) unregister_pernet_device(&ipip_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) static void __exit ipip_fini(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) rtnl_link_unregister(&ipip_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) if (xfrm4_tunnel_deregister(&ipip_handler, AF_INET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) pr_info("%s: can't deregister tunnel\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) #if IS_ENABLED(CONFIG_MPLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) if (xfrm4_tunnel_deregister(&mplsip_handler, AF_MPLS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) pr_info("%s: can't deregister tunnel\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) unregister_pernet_device(&ipip_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) module_init(ipip_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) module_exit(ipip_fini);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) MODULE_ALIAS_RTNL_LINK("ipip");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) MODULE_ALIAS_NETDEV("tunl0");