^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: GRE over IP protocol decoder.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Authors: Alexey Kuznetsov (kuznet@ms2.inr.ac.ru)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/in.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/tcp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/udp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/if_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/if_vlan.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/in6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/inetdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/igmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/netfilter_ipv4.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/etherdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/if_ether.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <net/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <net/icmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <net/protocol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <net/ip_tunnels.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <net/arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <net/checksum.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <net/dsfield.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <net/inet_ecn.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <net/xfrm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <net/net_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include <net/netns/generic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <net/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include <net/gre.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #include <net/dst_metadata.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include <net/erspan.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) Problems & solutions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) --------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) 1. The most important issue is detecting local dead loops.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) They would cause complete host lockup in transmit, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) would be "resolved" by stack overflow or, if queueing is enabled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) with infinite looping in net_bh.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) We cannot track such dead loops during route installation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) it is infeasible task. The most general solutions would be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) to keep skb->encapsulation counter (sort of local ttl),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) and silently drop packet when it expires. It is a good
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) solution, but it supposes maintaining new variable in ALL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) skb, even if no tunneling is used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) Current solution: xmit_recursion breaks dead loops. This is a percpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) counter, since when we enter the first ndo_xmit(), cpu migration is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) forbidden. We force an exit if this counter reaches RECURSION_LIMIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) 2. Networking dead loops would not kill routers, but would really
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) kill network. IP hop limit plays role of "t->recursion" in this case,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if we copy it from packet being encapsulated to upper header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) It is very good solution, but it introduces two problems:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) - Routing protocols, using packets with ttl=1 (OSPF, RIP2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) do not work over tunnels.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) - traceroute does not work. I planned to relay ICMP from tunnel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) so that this problem would be solved and traceroute output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) would even more informative. This idea appeared to be wrong:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) only Linux complies to rfc1812 now (yes, guys, Linux is the only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) true router now :-)), all routers (at least, in neighbourhood of mine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return only 8 bytes of payload. It is the end.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) Hence, if we want that OSPF worked or traceroute said something reasonable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) we should search for another solution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) One of them is to parse packet trying to detect inner encapsulation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) made by our node. It is difficult or even impossible, especially,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) taking into account fragmentation. TO be short, ttl is not solution at all.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) Current solution: The solution was UNEXPECTEDLY SIMPLE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) We force DF flag on tunnels with preconfigured hop limit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) that is ALL. :-) Well, it does not remove the problem completely,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) but exponential growth of network traffic is changed to linear
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) (branches, that exceed pmtu are pruned) and tunnel mtu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) rapidly degrades to value <68, where looping stops.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) Yes, it is not good if there exists a router in the loop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) which does not force DF, even when encapsulating packets have DF set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) But it is not our problem! Nobody could accuse us, we made
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) all that we could make. Even if it is your gated who injected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) fatal route to network, even if it were you who configured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) fatal static route: you are innocent. :-)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) Alexey Kuznetsov.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static bool log_ecn_error = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) module_param(log_ecn_error, bool, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static struct rtnl_link_ops ipgre_link_ops __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static int ipgre_tunnel_init(struct net_device *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static void erspan_build_header(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) u32 id, u32 index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) bool truncate, bool is_ipv4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static unsigned int ipgre_net_id __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static unsigned int gre_tap_net_id __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static unsigned int erspan_net_id __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static int ipgre_err(struct sk_buff *skb, u32 info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) const struct tnl_ptk_info *tpi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /* All the routers (except for Linux) return only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 8 bytes of packet payload. It means, that precise relaying of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) ICMP in the real Internet is absolutely infeasible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) Moreover, Cisco "wise men" put GRE key to the third word
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) in GRE header. It makes impossible maintaining even soft
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) state for keyed GRE tunnels with enabled checksum. Tell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) them "thank you".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) Well, I wonder, rfc1812 was written by Cisco employee,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) what the hell these idiots break standards established
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) by themselves???
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct net *net = dev_net(skb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct ip_tunnel_net *itn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) const struct iphdr *iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) const int type = icmp_hdr(skb)->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) const int code = icmp_hdr(skb)->code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) unsigned int data_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct ip_tunnel *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (tpi->proto == htons(ETH_P_TEB))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) itn = net_generic(net, gre_tap_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) else if (tpi->proto == htons(ETH_P_ERSPAN) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) tpi->proto == htons(ETH_P_ERSPAN2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) itn = net_generic(net, erspan_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) itn = net_generic(net, ipgre_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) iph = (const struct iphdr *)(icmp_hdr(skb) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) t = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) iph->daddr, iph->saddr, tpi->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) case ICMP_PARAMETERPROB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) case ICMP_DEST_UNREACH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) switch (code) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) case ICMP_SR_FAILED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) case ICMP_PORT_UNREACH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) /* Impossible event. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) /* All others are translated to HOST_UNREACH.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) rfc2003 contains "deep thoughts" about NET_UNREACH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) I believe they are just ether pollution. --ANK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) case ICMP_TIME_EXCEEDED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (code != ICMP_EXC_TTL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) data_len = icmp_hdr(skb)->un.reserved[1] * 4; /* RFC 4884 4.1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) case ICMP_REDIRECT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (tpi->proto == htons(ETH_P_IPV6) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) !ip6_err_gen_icmpv6_unreach(skb, iph->ihl * 4 + tpi->hdr_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) type, data_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (t->parms.iph.daddr == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) ipv4_is_multicast(t->parms.iph.daddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) t->err_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) t->err_count = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) t->err_time = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return 0;
^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) static void gre_err(struct sk_buff *skb, u32 info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) /* All the routers (except for Linux) return only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * 8 bytes of packet payload. It means, that precise relaying of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * ICMP in the real Internet is absolutely infeasible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * Moreover, Cisco "wise men" put GRE key to the third word
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * in GRE header. It makes impossible maintaining even soft
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * state for keyed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * GRE tunnels with enabled checksum. Tell them "thank you".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * Well, I wonder, rfc1812 was written by Cisco employee,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * what the hell these idiots break standards established
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * by themselves???
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) const struct iphdr *iph = (struct iphdr *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) const int type = icmp_hdr(skb)->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) const int code = icmp_hdr(skb)->code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct tnl_ptk_info tpi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (gre_parse_header(skb, &tpi, NULL, htons(ETH_P_IP),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) iph->ihl * 4) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) ipv4_update_pmtu(skb, dev_net(skb->dev), info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) skb->dev->ifindex, IPPROTO_GRE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (type == ICMP_REDIRECT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) ipv4_redirect(skb, dev_net(skb->dev), skb->dev->ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) IPPROTO_GRE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) return;
^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) ipgre_err(skb, info, &tpi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static bool is_erspan_type1(int gre_hdr_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) /* Both ERSPAN type I (version 0) and type II (version 1) use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) * protocol 0x88BE, but the type I has only 4-byte GRE header,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * while type II has 8-byte.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) return gre_hdr_len == 4;
^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) static int erspan_rcv(struct sk_buff *skb, struct tnl_ptk_info *tpi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) int gre_hdr_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) struct net *net = dev_net(skb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct metadata_dst *tun_dst = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct erspan_base_hdr *ershdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) struct ip_tunnel_net *itn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) struct ip_tunnel *tunnel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) const struct iphdr *iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) struct erspan_md2 *md2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) int ver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) itn = net_generic(net, erspan_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) iph = ip_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (is_erspan_type1(gre_hdr_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) ver = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) tpi->flags | TUNNEL_NO_KEY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) iph->saddr, iph->daddr, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) ershdr = (struct erspan_base_hdr *)(skb->data + gre_hdr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) ver = ershdr->ver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) tpi->flags | TUNNEL_KEY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) iph->saddr, iph->daddr, tpi->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (tunnel) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (is_erspan_type1(gre_hdr_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) len = gre_hdr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) len = gre_hdr_len + erspan_hdr_len(ver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (unlikely(!pskb_may_pull(skb, len)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) return PACKET_REJECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (__iptunnel_pull_header(skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) htons(ETH_P_TEB),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) false, false) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (tunnel->collect_md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) struct erspan_metadata *pkt_md, *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) struct ip_tunnel_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) unsigned char *gh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) __be64 tun_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) __be16 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) tpi->flags |= TUNNEL_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) flags = tpi->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) tun_id = key32_to_tunnel_id(tpi->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) tun_dst = ip_tun_rx_dst(skb, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) tun_id, sizeof(*md));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (!tun_dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return PACKET_REJECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) /* skb can be uncloned in __iptunnel_pull_header, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * old pkt_md is no longer valid and we need to reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) gh = skb_network_header(skb) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) skb_network_header_len(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) pkt_md = (struct erspan_metadata *)(gh + gre_hdr_len +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) sizeof(*ershdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) md->version = ver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) md2 = &md->u.md2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) memcpy(md2, pkt_md, ver == 1 ? ERSPAN_V1_MDSIZE :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) ERSPAN_V2_MDSIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) info = &tun_dst->u.tun_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) info->options_len = sizeof(*md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) skb_reset_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) ip_tunnel_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) return PACKET_RCVD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) return PACKET_REJECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) return PACKET_RCVD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) static int __ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) struct ip_tunnel_net *itn, int hdr_len, bool raw_proto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) struct metadata_dst *tun_dst = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) const struct iphdr *iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) struct ip_tunnel *tunnel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) iph = ip_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) iph->saddr, iph->daddr, tpi->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) if (tunnel) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) const struct iphdr *tnl_params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if (__iptunnel_pull_header(skb, hdr_len, tpi->proto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) raw_proto, false) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (tunnel->dev->type != ARPHRD_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) skb_pop_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) skb_reset_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) tnl_params = &tunnel->parms.iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) if (tunnel->collect_md || tnl_params->daddr == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) __be16 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) __be64 tun_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) flags = tpi->flags & (TUNNEL_CSUM | TUNNEL_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) tun_id = key32_to_tunnel_id(tpi->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) tun_dst = ip_tun_rx_dst(skb, flags, tun_id, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (!tun_dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) return PACKET_REJECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) ip_tunnel_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) return PACKET_RCVD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) return PACKET_NEXT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) return PACKET_RCVD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) static int ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) int hdr_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) struct net *net = dev_net(skb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) struct ip_tunnel_net *itn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) if (tpi->proto == htons(ETH_P_TEB))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) itn = net_generic(net, gre_tap_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) itn = net_generic(net, ipgre_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) res = __ipgre_rcv(skb, tpi, itn, hdr_len, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (res == PACKET_NEXT && tpi->proto == htons(ETH_P_TEB)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) /* ipgre tunnels in collect metadata mode should receive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * also ETH_P_TEB traffic.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) itn = net_generic(net, ipgre_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) res = __ipgre_rcv(skb, tpi, itn, hdr_len, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) static int gre_rcv(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) struct tnl_ptk_info tpi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) bool csum_err = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) int hdr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) #ifdef CONFIG_NET_IPGRE_BROADCAST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) if (ipv4_is_multicast(ip_hdr(skb)->daddr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) /* Looped back packet, drop it! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) if (rt_is_output_route(skb_rtable(skb)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IP), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (hdr_len < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) if (unlikely(tpi.proto == htons(ETH_P_ERSPAN) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) tpi.proto == htons(ETH_P_ERSPAN2))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (erspan_rcv(skb, &tpi, hdr_len) == PACKET_RCVD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) if (ipgre_rcv(skb, &tpi, hdr_len) == PACKET_RCVD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) static void __gre_xmit(struct sk_buff *skb, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) const struct iphdr *tnl_params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) __be16 proto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) if (tunnel->parms.o_flags & TUNNEL_SEQ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) tunnel->o_seqno++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) /* Push GRE header. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) gre_build_header(skb, tunnel->tun_hlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) tunnel->parms.o_flags, proto, tunnel->parms.o_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) htonl(tunnel->o_seqno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) ip_tunnel_xmit(skb, dev, tnl_params, tnl_params->protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) static int gre_handle_offloads(struct sk_buff *skb, bool csum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) return iptunnel_handle_offloads(skb, csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) static void gre_fb_xmit(struct sk_buff *skb, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) __be16 proto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) struct ip_tunnel_info *tun_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) const struct ip_tunnel_key *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) int tunnel_hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) __be16 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) tun_info = skb_tunnel_info(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) ip_tunnel_info_af(tun_info) != AF_INET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) goto err_free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) key = &tun_info->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) tunnel_hlen = gre_calc_hlen(key->tun_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) if (skb_cow_head(skb, dev->needed_headroom))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) goto err_free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) /* Push Tunnel header. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) if (gre_handle_offloads(skb, !!(tun_info->key.tun_flags & TUNNEL_CSUM)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) goto err_free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) flags = tun_info->key.tun_flags &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) (TUNNEL_CSUM | TUNNEL_KEY | TUNNEL_SEQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) gre_build_header(skb, tunnel_hlen, flags, proto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) tunnel_id_to_key32(tun_info->key.tun_id),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) (flags & TUNNEL_SEQ) ? htonl(tunnel->o_seqno++) : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) ip_md_tunnel_xmit(skb, dev, IPPROTO_GRE, tunnel_hlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) err_free_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) dev->stats.tx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) static void erspan_fb_xmit(struct sk_buff *skb, struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) struct ip_tunnel_info *tun_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) const struct ip_tunnel_key *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) struct erspan_metadata *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) bool truncate = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) __be16 proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) int tunnel_hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) int version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) int nhoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) int thoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) tun_info = skb_tunnel_info(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) ip_tunnel_info_af(tun_info) != AF_INET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) goto err_free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) key = &tun_info->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) if (!(tun_info->key.tun_flags & TUNNEL_ERSPAN_OPT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) goto err_free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) if (tun_info->options_len < sizeof(*md))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) goto err_free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) md = ip_tunnel_info_opts(tun_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) /* ERSPAN has fixed 8 byte GRE header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) version = md->version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) tunnel_hlen = 8 + erspan_hdr_len(version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) if (skb_cow_head(skb, dev->needed_headroom))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) goto err_free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) if (gre_handle_offloads(skb, false))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) goto err_free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) if (skb->len > dev->mtu + dev->hard_header_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) pskb_trim(skb, dev->mtu + dev->hard_header_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) truncate = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) nhoff = skb_network_header(skb) - skb_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) if (skb->protocol == htons(ETH_P_IP) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) (ntohs(ip_hdr(skb)->tot_len) > skb->len - nhoff))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) truncate = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) thoff = skb_transport_header(skb) - skb_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) if (skb->protocol == htons(ETH_P_IPV6) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) (ntohs(ipv6_hdr(skb)->payload_len) > skb->len - thoff))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) truncate = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) if (version == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) erspan_build_header(skb, ntohl(tunnel_id_to_key32(key->tun_id)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) ntohl(md->u.index), truncate, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) proto = htons(ETH_P_ERSPAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) } else if (version == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) erspan_build_header_v2(skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) ntohl(tunnel_id_to_key32(key->tun_id)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) md->u.md2.dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) get_hwid(&md->u.md2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) truncate, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) proto = htons(ETH_P_ERSPAN2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) goto err_free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) gre_build_header(skb, 8, TUNNEL_SEQ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) proto, 0, htonl(tunnel->o_seqno++));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) ip_md_tunnel_xmit(skb, dev, IPPROTO_GRE, tunnel_hlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) err_free_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) dev->stats.tx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) static int gre_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) struct ip_tunnel_info *info = skb_tunnel_info(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) const struct ip_tunnel_key *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) struct flowi4 fl4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) if (ip_tunnel_info_af(info) != AF_INET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) key = &info->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) ip_tunnel_init_flow(&fl4, IPPROTO_GRE, key->u.ipv4.dst, key->u.ipv4.src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) tunnel_id_to_key32(key->tun_id),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) key->tos & ~INET_ECN_MASK, 0, skb->mark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) skb_get_hash(skb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) rt = ip_route_output_key(dev_net(dev), &fl4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) if (IS_ERR(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) return PTR_ERR(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) info->key.u.ipv4.src = fl4.saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) return 0;
^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 netdev_tx_t ipgre_xmit(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) const struct iphdr *tnl_params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) if (!pskb_inet_may_pull(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) if (tunnel->collect_md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) gre_fb_xmit(skb, dev, skb->protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) if (dev->header_ops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) const int pull_len = tunnel->hlen + sizeof(struct iphdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) if (skb_cow_head(skb, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) tnl_params = (const struct iphdr *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) if (pull_len > skb_transport_offset(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) /* Pull skb since ip_tunnel_xmit() needs skb->data pointing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) * to gre header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) skb_pull(skb, pull_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) skb_reset_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) if (skb_cow_head(skb, dev->needed_headroom))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) tnl_params = &tunnel->parms.iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) if (gre_handle_offloads(skb, !!(tunnel->parms.o_flags & TUNNEL_CSUM)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) __gre_xmit(skb, dev, tnl_params, skb->protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) free_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) dev->stats.tx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) static netdev_tx_t erspan_xmit(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) bool truncate = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) __be16 proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) if (!pskb_inet_may_pull(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) if (tunnel->collect_md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) erspan_fb_xmit(skb, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) if (gre_handle_offloads(skb, false))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) if (skb_cow_head(skb, dev->needed_headroom))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) if (skb->len > dev->mtu + dev->hard_header_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) pskb_trim(skb, dev->mtu + dev->hard_header_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) truncate = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) /* Push ERSPAN header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) if (tunnel->erspan_ver == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) proto = htons(ETH_P_ERSPAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) tunnel->parms.o_flags &= ~TUNNEL_SEQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) } else if (tunnel->erspan_ver == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) erspan_build_header(skb, ntohl(tunnel->parms.o_key),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) tunnel->index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) truncate, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) proto = htons(ETH_P_ERSPAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) } else if (tunnel->erspan_ver == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) erspan_build_header_v2(skb, ntohl(tunnel->parms.o_key),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) tunnel->dir, tunnel->hwid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) truncate, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) proto = htons(ETH_P_ERSPAN2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) tunnel->parms.o_flags &= ~TUNNEL_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) __gre_xmit(skb, dev, &tunnel->parms.iph, proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) free_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) dev->stats.tx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) static netdev_tx_t gre_tap_xmit(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) if (!pskb_inet_may_pull(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) if (tunnel->collect_md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) gre_fb_xmit(skb, dev, htons(ETH_P_TEB));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) if (gre_handle_offloads(skb, !!(tunnel->parms.o_flags & TUNNEL_CSUM)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) if (skb_cow_head(skb, dev->needed_headroom))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) __gre_xmit(skb, dev, &tunnel->parms.iph, htons(ETH_P_TEB));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) free_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) dev->stats.tx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) static void ipgre_link_update(struct net_device *dev, bool set_mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) len = tunnel->tun_hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) len = tunnel->tun_hlen - len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) tunnel->hlen = tunnel->hlen + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) if (dev->header_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) dev->hard_header_len += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) dev->needed_headroom += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) if (set_mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) dev->mtu = max_t(int, dev->mtu - len, 68);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) if (!(tunnel->parms.o_flags & TUNNEL_SEQ)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) if (!(tunnel->parms.o_flags & TUNNEL_CSUM) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) tunnel->encap.type == TUNNEL_ENCAP_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) dev->features |= NETIF_F_GSO_SOFTWARE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) dev->hw_features |= NETIF_F_GSO_SOFTWARE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) dev->features &= ~NETIF_F_GSO_SOFTWARE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) dev->hw_features &= ~NETIF_F_GSO_SOFTWARE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) dev->features |= NETIF_F_LLTX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) dev->hw_features &= ~NETIF_F_GSO_SOFTWARE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) dev->features &= ~(NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) static int ipgre_tunnel_ctl(struct net_device *dev, struct ip_tunnel_parm *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) int cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) if (p->iph.version != 4 || p->iph.protocol != IPPROTO_GRE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) p->iph.ihl != 5 || (p->iph.frag_off & htons(~IP_DF)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) ((p->i_flags | p->o_flags) & (GRE_VERSION | GRE_ROUTING)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) p->i_flags = gre_flags_to_tnl_flags(p->i_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) p->o_flags = gre_flags_to_tnl_flags(p->o_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) err = ip_tunnel_ctl(dev, p, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) if (cmd == SIOCCHGTUNNEL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) t->parms.i_flags = p->i_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) t->parms.o_flags = p->o_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) if (strcmp(dev->rtnl_link_ops->kind, "erspan"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) ipgre_link_update(dev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) p->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) p->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) /* Nice toy. Unfortunately, useless in real life :-)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) It allows to construct virtual multiprotocol broadcast "LAN"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) over the Internet, provided multicast routing is tuned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) I have no idea was this bicycle invented before me,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) so that I had to set ARPHRD_IPGRE to a random value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) I have an impression, that Cisco could make something similar,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) but this feature is apparently missing in IOS<=11.2(8).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) I set up 10.66.66/24 and fec0:6666:6666::0/96 as virtual networks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) with broadcast 224.66.66.66. If you have access to mbone, play with me :-)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) ping -t 255 224.66.66.66
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) If nobody answers, mbone does not work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) ip tunnel add Universe mode gre remote 224.66.66.66 local <Your_real_addr> ttl 255
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) ip addr add 10.66.66.<somewhat>/24 dev Universe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) ifconfig Universe up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) ifconfig Universe add fe80::<Your_real_addr>/10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) ifconfig Universe add fec0:6666:6666::<Your_real_addr>/96
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) ftp 10.66.66.66
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) ftp fec0:6666:6666::193.233.7.65
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) static int ipgre_header(struct sk_buff *skb, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) unsigned short type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) const void *daddr, const void *saddr, unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) struct iphdr *iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) struct gre_base_hdr *greh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) iph = skb_push(skb, t->hlen + sizeof(*iph));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) greh = (struct gre_base_hdr *)(iph+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) greh->flags = gre_tnl_flags_to_gre_flags(t->parms.o_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) greh->protocol = htons(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) memcpy(iph, &t->parms.iph, sizeof(struct iphdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) /* Set the source hardware address. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) if (saddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) memcpy(&iph->saddr, saddr, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) if (daddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) memcpy(&iph->daddr, daddr, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) if (iph->daddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) return t->hlen + sizeof(*iph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) return -(t->hlen + sizeof(*iph));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) static int ipgre_header_parse(const struct sk_buff *skb, unsigned char *haddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) const struct iphdr *iph = (const struct iphdr *) skb_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) memcpy(haddr, &iph->saddr, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) return 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) static const struct header_ops ipgre_header_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) .create = ipgre_header,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) .parse = ipgre_header_parse,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) #ifdef CONFIG_NET_IPGRE_BROADCAST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) static int ipgre_open(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) if (ipv4_is_multicast(t->parms.iph.daddr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) struct flowi4 fl4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) rt = ip_route_output_gre(t->net, &fl4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) t->parms.iph.daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) t->parms.iph.saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) t->parms.o_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) RT_TOS(t->parms.iph.tos),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) t->parms.link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) if (IS_ERR(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) return -EADDRNOTAVAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) dev = rt->dst.dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) if (!__in_dev_get_rtnl(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) return -EADDRNOTAVAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) t->mlink = dev->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) ip_mc_inc_group(__in_dev_get_rtnl(dev), t->parms.iph.daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) static int ipgre_close(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) if (ipv4_is_multicast(t->parms.iph.daddr) && t->mlink) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) struct in_device *in_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) in_dev = inetdev_by_index(t->net, t->mlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) if (in_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) ip_mc_dec_group(in_dev, t->parms.iph.daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) static const struct net_device_ops ipgre_netdev_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) .ndo_init = ipgre_tunnel_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) .ndo_uninit = ip_tunnel_uninit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) #ifdef CONFIG_NET_IPGRE_BROADCAST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) .ndo_open = ipgre_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) .ndo_stop = ipgre_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) .ndo_start_xmit = ipgre_xmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) .ndo_do_ioctl = ip_tunnel_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) .ndo_change_mtu = ip_tunnel_change_mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) .ndo_get_stats64 = ip_tunnel_get_stats64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) .ndo_get_iflink = ip_tunnel_get_iflink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) .ndo_tunnel_ctl = ipgre_tunnel_ctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) #define GRE_FEATURES (NETIF_F_SG | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) NETIF_F_FRAGLIST | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) NETIF_F_HIGHDMA | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) NETIF_F_HW_CSUM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) static void ipgre_tunnel_setup(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) dev->netdev_ops = &ipgre_netdev_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) dev->type = ARPHRD_IPGRE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) ip_tunnel_setup(dev, ipgre_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) static void __gre_tunnel_init(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) struct ip_tunnel *tunnel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) tunnel->parms.iph.protocol = IPPROTO_GRE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) dev->needed_headroom = tunnel->hlen + sizeof(tunnel->parms.iph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) dev->features |= GRE_FEATURES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) dev->hw_features |= GRE_FEATURES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) if (!(tunnel->parms.o_flags & TUNNEL_SEQ)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) /* TCP offload with GRE SEQ is not supported, nor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) * can we support 2 levels of outer headers requiring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) * an update.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) if (!(tunnel->parms.o_flags & TUNNEL_CSUM) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) (tunnel->encap.type == TUNNEL_ENCAP_NONE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) dev->features |= NETIF_F_GSO_SOFTWARE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) dev->hw_features |= NETIF_F_GSO_SOFTWARE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) /* Can use a lockless transmit, unless we generate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) * output sequences
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) dev->features |= NETIF_F_LLTX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) static int ipgre_tunnel_init(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) struct iphdr *iph = &tunnel->parms.iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) __gre_tunnel_init(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) memcpy(dev->dev_addr, &iph->saddr, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) memcpy(dev->broadcast, &iph->daddr, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) dev->flags = IFF_NOARP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) netif_keep_dst(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) dev->addr_len = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) if (iph->daddr && !tunnel->collect_md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) #ifdef CONFIG_NET_IPGRE_BROADCAST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) if (ipv4_is_multicast(iph->daddr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) if (!iph->saddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) dev->flags = IFF_BROADCAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) dev->header_ops = &ipgre_header_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) dev->hard_header_len = tunnel->hlen + sizeof(*iph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) dev->needed_headroom = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) } else if (!tunnel->collect_md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) dev->header_ops = &ipgre_header_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) dev->hard_header_len = tunnel->hlen + sizeof(*iph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) dev->needed_headroom = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) return ip_tunnel_init(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) static const struct gre_protocol ipgre_protocol = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) .handler = gre_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) .err_handler = gre_err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) static int __net_init ipgre_init_net(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) return ip_tunnel_init_net(net, ipgre_net_id, &ipgre_link_ops, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) static void __net_exit ipgre_exit_batch_net(struct list_head *list_net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) ip_tunnel_delete_nets(list_net, ipgre_net_id, &ipgre_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) static struct pernet_operations ipgre_net_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) .init = ipgre_init_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) .exit_batch = ipgre_exit_batch_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) .id = &ipgre_net_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) .size = sizeof(struct ip_tunnel_net),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) static int ipgre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) __be16 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) if (data[IFLA_GRE_IFLAGS])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) if (data[IFLA_GRE_OFLAGS])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) if (flags & (GRE_VERSION|GRE_ROUTING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) if (data[IFLA_GRE_COLLECT_METADATA] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) data[IFLA_GRE_ENCAP_TYPE] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) static int ipgre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) __be32 daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) if (tb[IFLA_ADDRESS]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) return -EADDRNOTAVAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) if (data[IFLA_GRE_REMOTE]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) memcpy(&daddr, nla_data(data[IFLA_GRE_REMOTE]), 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) if (!daddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) return ipgre_tunnel_validate(tb, data, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) static int erspan_validate(struct nlattr *tb[], struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) __be16 flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) ret = ipgre_tap_validate(tb, data, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) if (data[IFLA_GRE_ERSPAN_VER] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) nla_get_u8(data[IFLA_GRE_ERSPAN_VER]) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) /* ERSPAN type II/III should only have GRE sequence and key flag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) if (data[IFLA_GRE_OFLAGS])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) if (data[IFLA_GRE_IFLAGS])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) if (!data[IFLA_GRE_COLLECT_METADATA] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) flags != (GRE_SEQ | GRE_KEY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) /* ERSPAN Session ID only has 10-bit. Since we reuse
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) * 32-bit key field as ID, check it's range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) if (data[IFLA_GRE_IKEY] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) if (data[IFLA_GRE_OKEY] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) static int ipgre_netlink_parms(struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) struct nlattr *tb[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) struct ip_tunnel_parm *parms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) __u32 *fwmark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) memset(parms, 0, sizeof(*parms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) parms->iph.protocol = IPPROTO_GRE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) if (data[IFLA_GRE_LINK])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) if (data[IFLA_GRE_IFLAGS])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) parms->i_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_IFLAGS]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) if (data[IFLA_GRE_OFLAGS])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) parms->o_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_OFLAGS]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) if (data[IFLA_GRE_IKEY])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) if (data[IFLA_GRE_OKEY])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) if (data[IFLA_GRE_LOCAL])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) parms->iph.saddr = nla_get_in_addr(data[IFLA_GRE_LOCAL]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) if (data[IFLA_GRE_REMOTE])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) parms->iph.daddr = nla_get_in_addr(data[IFLA_GRE_REMOTE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) if (data[IFLA_GRE_TTL])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) parms->iph.ttl = nla_get_u8(data[IFLA_GRE_TTL]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) if (data[IFLA_GRE_TOS])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) parms->iph.tos = nla_get_u8(data[IFLA_GRE_TOS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) if (!data[IFLA_GRE_PMTUDISC] || nla_get_u8(data[IFLA_GRE_PMTUDISC])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) if (t->ignore_df)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) parms->iph.frag_off = htons(IP_DF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) if (data[IFLA_GRE_COLLECT_METADATA]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) t->collect_md = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) if (dev->type == ARPHRD_IPGRE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) dev->type = ARPHRD_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) if (data[IFLA_GRE_IGNORE_DF]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) if (nla_get_u8(data[IFLA_GRE_IGNORE_DF])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) && (parms->iph.frag_off & htons(IP_DF)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) t->ignore_df = !!nla_get_u8(data[IFLA_GRE_IGNORE_DF]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) if (data[IFLA_GRE_FWMARK])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) *fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) static int erspan_netlink_parms(struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) struct nlattr *tb[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) struct ip_tunnel_parm *parms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) __u32 *fwmark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) err = ipgre_netlink_parms(dev, data, tb, parms, fwmark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) if (data[IFLA_GRE_ERSPAN_VER]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) t->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) if (t->erspan_ver > 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) if (t->erspan_ver == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) if (data[IFLA_GRE_ERSPAN_INDEX]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) t->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) if (t->index & ~INDEX_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) } else if (t->erspan_ver == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) if (data[IFLA_GRE_ERSPAN_DIR]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) t->dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) if (t->dir & ~(DIR_MASK >> DIR_OFFSET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) if (data[IFLA_GRE_ERSPAN_HWID]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) t->hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) if (t->hwid & ~(HWID_MASK >> HWID_OFFSET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) /* This function returns true when ENCAP attributes are present in the nl msg */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) static bool ipgre_netlink_encap_parms(struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) struct ip_tunnel_encap *ipencap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) bool ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) memset(ipencap, 0, sizeof(*ipencap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) if (data[IFLA_GRE_ENCAP_TYPE]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) if (data[IFLA_GRE_ENCAP_FLAGS]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) if (data[IFLA_GRE_ENCAP_SPORT]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) if (data[IFLA_GRE_ENCAP_DPORT]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) static int gre_tap_init(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) __gre_tunnel_init(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) netif_keep_dst(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) return ip_tunnel_init(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) static const struct net_device_ops gre_tap_netdev_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) .ndo_init = gre_tap_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) .ndo_uninit = ip_tunnel_uninit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) .ndo_start_xmit = gre_tap_xmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) .ndo_set_mac_address = eth_mac_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) .ndo_validate_addr = eth_validate_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) .ndo_change_mtu = ip_tunnel_change_mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) .ndo_get_stats64 = ip_tunnel_get_stats64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) .ndo_get_iflink = ip_tunnel_get_iflink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) .ndo_fill_metadata_dst = gre_fill_metadata_dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) static int erspan_tunnel_init(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) if (tunnel->erspan_ver == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) tunnel->tun_hlen = 4; /* 4-byte GRE hdr. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) tunnel->tun_hlen = 8; /* 8-byte GRE hdr. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) tunnel->parms.iph.protocol = IPPROTO_GRE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) erspan_hdr_len(tunnel->erspan_ver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) dev->features |= GRE_FEATURES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) dev->hw_features |= GRE_FEATURES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) netif_keep_dst(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) return ip_tunnel_init(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) static const struct net_device_ops erspan_netdev_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) .ndo_init = erspan_tunnel_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) .ndo_uninit = ip_tunnel_uninit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) .ndo_start_xmit = erspan_xmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) .ndo_set_mac_address = eth_mac_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) .ndo_validate_addr = eth_validate_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) .ndo_change_mtu = ip_tunnel_change_mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) .ndo_get_stats64 = ip_tunnel_get_stats64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) .ndo_get_iflink = ip_tunnel_get_iflink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) .ndo_fill_metadata_dst = gre_fill_metadata_dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) static void ipgre_tap_setup(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) ether_setup(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) dev->max_mtu = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) dev->netdev_ops = &gre_tap_netdev_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) dev->priv_flags &= ~IFF_TX_SKB_SHARING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) ip_tunnel_setup(dev, gre_tap_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) ipgre_newlink_encap_setup(struct net_device *dev, struct nlattr *data[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) struct ip_tunnel_encap ipencap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) if (ipgre_netlink_encap_parms(data, &ipencap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) int err = ip_tunnel_encap_setup(t, &ipencap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) static int ipgre_newlink(struct net *src_net, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) struct nlattr *tb[], struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) struct ip_tunnel_parm p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) __u32 fwmark = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) err = ipgre_newlink_encap_setup(dev, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) err = ipgre_netlink_parms(dev, data, tb, &p, &fwmark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) return ip_tunnel_newlink(dev, tb, &p, fwmark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) static int erspan_newlink(struct net *src_net, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) struct nlattr *tb[], struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) struct ip_tunnel_parm p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) __u32 fwmark = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) err = ipgre_newlink_encap_setup(dev, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) err = erspan_netlink_parms(dev, data, tb, &p, &fwmark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) return ip_tunnel_newlink(dev, tb, &p, fwmark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) __u32 fwmark = t->fwmark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) struct ip_tunnel_parm p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) err = ipgre_newlink_encap_setup(dev, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) err = ipgre_netlink_parms(dev, data, tb, &p, &fwmark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) err = ip_tunnel_changelink(dev, tb, &p, fwmark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) t->parms.i_flags = p.i_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) t->parms.o_flags = p.o_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) ipgre_link_update(dev, !tb[IFLA_MTU]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) static int erspan_changelink(struct net_device *dev, struct nlattr *tb[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) __u32 fwmark = t->fwmark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) struct ip_tunnel_parm p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) err = ipgre_newlink_encap_setup(dev, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) err = erspan_netlink_parms(dev, data, tb, &p, &fwmark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) err = ip_tunnel_changelink(dev, tb, &p, fwmark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) t->parms.i_flags = p.i_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) t->parms.o_flags = p.o_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) static size_t ipgre_get_size(const struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) /* IFLA_GRE_LINK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) nla_total_size(4) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) /* IFLA_GRE_IFLAGS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) nla_total_size(2) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) /* IFLA_GRE_OFLAGS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) nla_total_size(2) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) /* IFLA_GRE_IKEY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) nla_total_size(4) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) /* IFLA_GRE_OKEY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) nla_total_size(4) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) /* IFLA_GRE_LOCAL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) nla_total_size(4) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) /* IFLA_GRE_REMOTE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) nla_total_size(4) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) /* IFLA_GRE_TTL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) nla_total_size(1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) /* IFLA_GRE_TOS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) nla_total_size(1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) /* IFLA_GRE_PMTUDISC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) nla_total_size(1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) /* IFLA_GRE_ENCAP_TYPE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) nla_total_size(2) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) /* IFLA_GRE_ENCAP_FLAGS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) nla_total_size(2) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) /* IFLA_GRE_ENCAP_SPORT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) nla_total_size(2) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) /* IFLA_GRE_ENCAP_DPORT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) nla_total_size(2) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) /* IFLA_GRE_COLLECT_METADATA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) nla_total_size(0) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) /* IFLA_GRE_IGNORE_DF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) nla_total_size(1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) /* IFLA_GRE_FWMARK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) nla_total_size(4) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) /* IFLA_GRE_ERSPAN_INDEX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) nla_total_size(4) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) /* IFLA_GRE_ERSPAN_VER */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) nla_total_size(1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) /* IFLA_GRE_ERSPAN_DIR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) nla_total_size(1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) /* IFLA_GRE_ERSPAN_HWID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) nla_total_size(2) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) static int ipgre_fill_info(struct sk_buff *skb, const struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) struct ip_tunnel_parm *p = &t->parms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) __be16 o_flags = p->o_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) if (t->erspan_ver <= 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) if (t->erspan_ver != 0 && !t->collect_md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) o_flags |= TUNNEL_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, t->erspan_ver))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) if (t->erspan_ver == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, t->index))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) } else if (t->erspan_ver == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, t->dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, t->hwid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) nla_put_be16(skb, IFLA_GRE_IFLAGS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) gre_tnl_flags_to_gre_flags(p->i_flags)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) nla_put_be16(skb, IFLA_GRE_OFLAGS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) gre_tnl_flags_to_gre_flags(o_flags)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) nla_put_in_addr(skb, IFLA_GRE_LOCAL, p->iph.saddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) nla_put_in_addr(skb, IFLA_GRE_REMOTE, p->iph.daddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) nla_put_u8(skb, IFLA_GRE_TTL, p->iph.ttl) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) nla_put_u8(skb, IFLA_GRE_TOS, p->iph.tos) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) nla_put_u8(skb, IFLA_GRE_PMTUDISC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) !!(p->iph.frag_off & htons(IP_DF))) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) nla_put_u32(skb, IFLA_GRE_FWMARK, t->fwmark))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) t->encap.type) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) t->encap.sport) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) t->encap.dport) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) t->encap.flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) if (nla_put_u8(skb, IFLA_GRE_IGNORE_DF, t->ignore_df))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) if (t->collect_md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) static void erspan_setup(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) ether_setup(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) dev->max_mtu = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) dev->netdev_ops = &erspan_netdev_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) dev->priv_flags &= ~IFF_TX_SKB_SHARING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) ip_tunnel_setup(dev, erspan_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) t->erspan_ver = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) static const struct nla_policy ipgre_policy[IFLA_GRE_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) [IFLA_GRE_LINK] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) [IFLA_GRE_IKEY] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) [IFLA_GRE_OKEY] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) [IFLA_GRE_LOCAL] = { .len = sizeof_field(struct iphdr, saddr) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) [IFLA_GRE_REMOTE] = { .len = sizeof_field(struct iphdr, daddr) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) [IFLA_GRE_TTL] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) [IFLA_GRE_TOS] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) [IFLA_GRE_PMTUDISC] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) [IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) [IFLA_GRE_IGNORE_DF] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) [IFLA_GRE_FWMARK] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) [IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) [IFLA_GRE_ERSPAN_VER] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) [IFLA_GRE_ERSPAN_DIR] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) [IFLA_GRE_ERSPAN_HWID] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) static struct rtnl_link_ops ipgre_link_ops __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) .kind = "gre",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) .maxtype = IFLA_GRE_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) .policy = ipgre_policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) .priv_size = sizeof(struct ip_tunnel),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) .setup = ipgre_tunnel_setup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) .validate = ipgre_tunnel_validate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) .newlink = ipgre_newlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) .changelink = ipgre_changelink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) .dellink = ip_tunnel_dellink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) .get_size = ipgre_get_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) .fill_info = ipgre_fill_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) .get_link_net = ip_tunnel_get_link_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) static struct rtnl_link_ops ipgre_tap_ops __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) .kind = "gretap",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) .maxtype = IFLA_GRE_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) .policy = ipgre_policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) .priv_size = sizeof(struct ip_tunnel),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) .setup = ipgre_tap_setup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) .validate = ipgre_tap_validate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) .newlink = ipgre_newlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) .changelink = ipgre_changelink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) .dellink = ip_tunnel_dellink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) .get_size = ipgre_get_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) .fill_info = ipgre_fill_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) .get_link_net = ip_tunnel_get_link_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) static struct rtnl_link_ops erspan_link_ops __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) .kind = "erspan",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) .maxtype = IFLA_GRE_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) .policy = ipgre_policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) .priv_size = sizeof(struct ip_tunnel),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) .setup = erspan_setup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) .validate = erspan_validate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) .newlink = erspan_newlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) .changelink = erspan_changelink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) .dellink = ip_tunnel_dellink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) .get_size = ipgre_get_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) .fill_info = ipgre_fill_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) .get_link_net = ip_tunnel_get_link_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) struct net_device *gretap_fb_dev_create(struct net *net, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) u8 name_assign_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) struct nlattr *tb[IFLA_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) LIST_HEAD(list_kill);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) struct ip_tunnel *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) memset(&tb, 0, sizeof(tb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) dev = rtnl_create_link(net, name, name_assign_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) &ipgre_tap_ops, tb, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) if (IS_ERR(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) /* Configure flow based GRE device. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) t->collect_md = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) err = ipgre_newlink(net, dev, tb, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) free_netdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) /* openvswitch users expect packet sizes to be unrestricted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) * so set the largest MTU we can.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) err = __ip_tunnel_change_mtu(dev, IP_MAX_MTU, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) err = rtnl_configure_link(dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) ip_tunnel_dellink(dev, &list_kill);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) unregister_netdevice_many(&list_kill);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) EXPORT_SYMBOL_GPL(gretap_fb_dev_create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) static int __net_init ipgre_tap_init_net(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) return ip_tunnel_init_net(net, gre_tap_net_id, &ipgre_tap_ops, "gretap0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) static void __net_exit ipgre_tap_exit_batch_net(struct list_head *list_net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) ip_tunnel_delete_nets(list_net, gre_tap_net_id, &ipgre_tap_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) static struct pernet_operations ipgre_tap_net_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) .init = ipgre_tap_init_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) .exit_batch = ipgre_tap_exit_batch_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) .id = &gre_tap_net_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) .size = sizeof(struct ip_tunnel_net),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) static int __net_init erspan_init_net(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) return ip_tunnel_init_net(net, erspan_net_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) &erspan_link_ops, "erspan0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) static void __net_exit erspan_exit_batch_net(struct list_head *net_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) ip_tunnel_delete_nets(net_list, erspan_net_id, &erspan_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) static struct pernet_operations erspan_net_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) .init = erspan_init_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) .exit_batch = erspan_exit_batch_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) .id = &erspan_net_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) .size = sizeof(struct ip_tunnel_net),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) static int __init ipgre_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) pr_info("GRE over IPv4 tunneling driver\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) err = register_pernet_device(&ipgre_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) err = register_pernet_device(&ipgre_tap_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) goto pnet_tap_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) err = register_pernet_device(&erspan_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) goto pnet_erspan_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) err = gre_add_protocol(&ipgre_protocol, GREPROTO_CISCO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) pr_info("%s: can't add protocol\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) goto add_proto_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) err = rtnl_link_register(&ipgre_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) goto rtnl_link_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) err = rtnl_link_register(&ipgre_tap_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) goto tap_ops_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) err = rtnl_link_register(&erspan_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) goto erspan_link_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) erspan_link_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) rtnl_link_unregister(&ipgre_tap_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) tap_ops_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) rtnl_link_unregister(&ipgre_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) rtnl_link_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) add_proto_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) unregister_pernet_device(&erspan_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) pnet_erspan_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) unregister_pernet_device(&ipgre_tap_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) pnet_tap_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) unregister_pernet_device(&ipgre_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) static void __exit ipgre_fini(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) rtnl_link_unregister(&ipgre_tap_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) rtnl_link_unregister(&ipgre_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) rtnl_link_unregister(&erspan_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) unregister_pernet_device(&ipgre_tap_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) unregister_pernet_device(&ipgre_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) unregister_pernet_device(&erspan_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) module_init(ipgre_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) module_exit(ipgre_fini);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) MODULE_ALIAS_RTNL_LINK("gre");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) MODULE_ALIAS_RTNL_LINK("gretap");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) MODULE_ALIAS_RTNL_LINK("erspan");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) MODULE_ALIAS_NETDEV("gre0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) MODULE_ALIAS_NETDEV("gretap0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) MODULE_ALIAS_NETDEV("erspan0");