^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* Bareudp: UDP tunnel encasulation for different Payload types like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * MPLS, NSH, IP, etc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (c) 2019 Nokia, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Authors: Martin Varghese, <martin.varghese@nokia.com>
^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/kernel.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/etherdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <net/dst_metadata.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <net/gro_cells.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <net/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <net/protocol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <net/ip6_tunnel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <net/ip_tunnels.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <net/udp_tunnel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <net/bareudp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define BAREUDP_BASE_HLEN sizeof(struct udphdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define BAREUDP_IPV4_HLEN (sizeof(struct iphdr) + \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) sizeof(struct udphdr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define BAREUDP_IPV6_HLEN (sizeof(struct ipv6hdr) + \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) sizeof(struct udphdr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static bool log_ecn_error = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) module_param(log_ecn_error, bool, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /* per-network namespace private data for this module */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static unsigned int bareudp_net_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct bareudp_net {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct list_head bareudp_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /* Pseudo network device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct bareudp_dev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct net *net; /* netns for packet i/o */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct net_device *dev; /* netdev for bareudp tunnel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) __be16 ethertype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) __be16 port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) u16 sport_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) bool multi_proto_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct socket __rcu *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct list_head next; /* bareudp node on namespace list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct gro_cells gro_cells;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static int bareudp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct metadata_dst *tun_dst = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct bareudp_dev *bareudp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) unsigned short family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) unsigned int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) __be16 proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) void *oiph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) bareudp = rcu_dereference_sk_user_data(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (!bareudp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (skb->protocol == htons(ETH_P_IP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) family = AF_INET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) family = AF_INET6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (bareudp->ethertype == htons(ETH_P_IP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) __u8 ipversion;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (skb_copy_bits(skb, BAREUDP_BASE_HLEN, &ipversion,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) sizeof(ipversion))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) bareudp->dev->stats.rx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) ipversion >>= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (ipversion == 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) proto = htons(ETH_P_IP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) } else if (ipversion == 6 && bareudp->multi_proto_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) proto = htons(ETH_P_IPV6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) bareudp->dev->stats.rx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) } else if (bareudp->ethertype == htons(ETH_P_MPLS_UC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct iphdr *tunnel_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) tunnel_hdr = (struct iphdr *)skb_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (tunnel_hdr->version == 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (!ipv4_is_multicast(tunnel_hdr->daddr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) proto = bareudp->ethertype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) } else if (bareudp->multi_proto_mode &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) ipv4_is_multicast(tunnel_hdr->daddr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) proto = htons(ETH_P_MPLS_MC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) bareudp->dev->stats.rx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) int addr_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct ipv6hdr *tunnel_hdr_v6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) tunnel_hdr_v6 = (struct ipv6hdr *)skb_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) addr_type =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) ipv6_addr_type((struct in6_addr *)&tunnel_hdr_v6->daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (!(addr_type & IPV6_ADDR_MULTICAST)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) proto = bareudp->ethertype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) } else if (bareudp->multi_proto_mode &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) (addr_type & IPV6_ADDR_MULTICAST)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) proto = htons(ETH_P_MPLS_MC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) bareudp->dev->stats.rx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) proto = bareudp->ethertype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (iptunnel_pull_header(skb, BAREUDP_BASE_HLEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) proto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) !net_eq(bareudp->net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) dev_net(bareudp->dev)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) bareudp->dev->stats.rx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) tun_dst = udp_tun_rx_dst(skb, family, TUNNEL_KEY, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (!tun_dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) bareudp->dev->stats.rx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) skb_dst_set(skb, &tun_dst->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) skb->dev = bareudp->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) oiph = skb_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (!ipv6_mod_enabled() || family == AF_INET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) err = IP_ECN_decapsulate(oiph, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) err = IP6_ECN_decapsulate(oiph, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (log_ecn_error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (!ipv6_mod_enabled() || family == AF_INET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) net_info_ratelimited("non-ECT from %pI4 "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) "with TOS=%#x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) &((struct iphdr *)oiph)->saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) ((struct iphdr *)oiph)->tos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) net_info_ratelimited("non-ECT from %pI6\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) &((struct ipv6hdr *)oiph)->saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (err > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) ++bareudp->dev->stats.rx_frame_errors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) ++bareudp->dev->stats.rx_errors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) len = skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) err = gro_cells_receive(&bareudp->gro_cells, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (likely(err == NET_RX_SUCCESS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) dev_sw_netstats_rx_add(bareudp->dev, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) /* Consume bad packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static int bareudp_err_lookup(struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static int bareudp_init(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) struct bareudp_dev *bareudp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (!dev->tstats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) err = gro_cells_init(&bareudp->gro_cells, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) free_percpu(dev->tstats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static void bareudp_uninit(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) struct bareudp_dev *bareudp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) gro_cells_destroy(&bareudp->gro_cells);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) free_percpu(dev->tstats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static struct socket *bareudp_create_sock(struct net *net, __be16 port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) struct udp_port_cfg udp_conf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) struct socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) memset(&udp_conf, 0, sizeof(udp_conf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (ipv6_mod_enabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) udp_conf.family = AF_INET6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) udp_conf.family = AF_INET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) udp_conf.local_udp_port = port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) /* Open UDP socket */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) err = udp_sock_create(net, &udp_conf, &sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) /* Create new listen socket if needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) static int bareudp_socket_create(struct bareudp_dev *bareudp, __be16 port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) struct udp_tunnel_sock_cfg tunnel_cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) struct socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) sock = bareudp_create_sock(bareudp->net, port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (IS_ERR(sock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return PTR_ERR(sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) /* Mark socket as an encapsulation socket */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) tunnel_cfg.sk_user_data = bareudp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) tunnel_cfg.encap_type = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) tunnel_cfg.encap_rcv = bareudp_udp_encap_recv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) tunnel_cfg.encap_err_lookup = bareudp_err_lookup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) tunnel_cfg.encap_destroy = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) setup_udp_tunnel_sock(bareudp->net, sock, &tunnel_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) rcu_assign_pointer(bareudp->sock, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static int bareudp_open(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) struct bareudp_dev *bareudp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) ret = bareudp_socket_create(bareudp, bareudp->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) static void bareudp_sock_release(struct bareudp_dev *bareudp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) sock = bareudp->sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) rcu_assign_pointer(bareudp->sock, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) synchronize_net();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) udp_tunnel_sock_release(sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static int bareudp_stop(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) struct bareudp_dev *bareudp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) bareudp_sock_release(bareudp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) static int bareudp_xmit_skb(struct sk_buff *skb, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) struct bareudp_dev *bareudp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) const struct ip_tunnel_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) bool xnet = !net_eq(bareudp->net, dev_net(bareudp->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) struct socket *sock = rcu_dereference(bareudp->sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) const struct ip_tunnel_key *key = &info->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) __be16 sport, df;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) int min_headroom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) __u8 tos, ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) __be32 saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (!sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) return -ESHUTDOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) rt = ip_route_output_tunnel(skb, dev, bareudp->net, &saddr, info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) IPPROTO_UDP, use_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (IS_ERR(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) return PTR_ERR(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) skb_tunnel_check_pmtu(skb, &rt->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) BAREUDP_IPV4_HLEN + info->options_len, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) sport = udp_flow_src_port(bareudp->net, skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) bareudp->sport_min, USHRT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) ttl = key->ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) skb_scrub_packet(skb, xnet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) err = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (!skb_pull(skb, skb_network_offset(skb)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) goto free_dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) BAREUDP_BASE_HLEN + info->options_len + sizeof(struct iphdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) err = skb_cow_head(skb, min_headroom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) goto free_dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) err = udp_tunnel_handle_offloads(skb, udp_sum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) goto free_dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) skb_set_inner_protocol(skb, bareudp->ethertype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) udp_tunnel_xmit_skb(rt, sock->sk, skb, saddr, info->key.u.ipv4.dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) tos, ttl, df, sport, bareudp->port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) !net_eq(bareudp->net, dev_net(bareudp->dev)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) !(info->key.tun_flags & TUNNEL_CSUM));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) free_dst:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) dst_release(&rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) static int bareudp6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) struct bareudp_dev *bareudp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) const struct ip_tunnel_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) bool xnet = !net_eq(bareudp->net, dev_net(bareudp->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) struct socket *sock = rcu_dereference(bareudp->sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) const struct ip_tunnel_key *key = &info->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) struct dst_entry *dst = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) struct in6_addr saddr, daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) int min_headroom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) __u8 prio, ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) __be16 sport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) if (!sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) return -ESHUTDOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) dst = ip6_dst_lookup_tunnel(skb, dev, bareudp->net, sock, &saddr, info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) IPPROTO_UDP, use_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) if (IS_ERR(dst))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) return PTR_ERR(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) skb_tunnel_check_pmtu(skb, dst, BAREUDP_IPV6_HLEN + info->options_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) sport = udp_flow_src_port(bareudp->net, skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) bareudp->sport_min, USHRT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) ttl = key->ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) skb_scrub_packet(skb, xnet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) err = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (!skb_pull(skb, skb_network_offset(skb)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) goto free_dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) BAREUDP_BASE_HLEN + info->options_len + sizeof(struct ipv6hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) err = skb_cow_head(skb, min_headroom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) goto free_dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) err = udp_tunnel_handle_offloads(skb, udp_sum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) goto free_dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) daddr = info->key.u.ipv6.dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) udp_tunnel6_xmit_skb(dst, sock->sk, skb, dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) &saddr, &daddr, prio, ttl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) info->key.label, sport, bareudp->port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) !(info->key.tun_flags & TUNNEL_CSUM));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) free_dst:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) dst_release(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) static bool bareudp_proto_valid(struct bareudp_dev *bareudp, __be16 proto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) if (bareudp->ethertype == proto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) if (!bareudp->multi_proto_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) if (bareudp->ethertype == htons(ETH_P_MPLS_UC) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) proto == htons(ETH_P_MPLS_MC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (bareudp->ethertype == htons(ETH_P_IP) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) proto == htons(ETH_P_IPV6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) static netdev_tx_t bareudp_xmit(struct sk_buff *skb, struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) struct bareudp_dev *bareudp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) struct ip_tunnel_info *info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) if (!bareudp_proto_valid(bareudp, skb->protocol)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) info = skb_tunnel_info(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) goto tx_error;
^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) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) if (ipv6_mod_enabled() && info->mode & IP_TUNNEL_INFO_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) err = bareudp6_xmit_skb(skb, dev, bareudp, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) err = bareudp_xmit_skb(skb, dev, bareudp, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) if (likely(!err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) tx_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) dev_kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) if (err == -ELOOP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) dev->stats.collisions++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) else if (err == -ENETUNREACH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) dev->stats.tx_carrier_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) dev->stats.tx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) static int bareudp_fill_metadata_dst(struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) struct ip_tunnel_info *info = skb_tunnel_info(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) struct bareudp_dev *bareudp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) bool use_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) use_cache = ip_tunnel_dst_cache_usable(skb, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) if (!ipv6_mod_enabled() || ip_tunnel_info_af(info) == AF_INET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) __be32 saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) rt = ip_route_output_tunnel(skb, dev, bareudp->net, &saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) info, IPPROTO_UDP, use_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) if (IS_ERR(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) return PTR_ERR(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) info->key.u.ipv4.src = saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) } else if (ip_tunnel_info_af(info) == AF_INET6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) struct dst_entry *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) struct in6_addr saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) struct socket *sock = rcu_dereference(bareudp->sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) dst = ip6_dst_lookup_tunnel(skb, dev, bareudp->net, sock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) &saddr, info, IPPROTO_UDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) use_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) if (IS_ERR(dst))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) return PTR_ERR(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) dst_release(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) info->key.u.ipv6.src = saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) info->key.tp_src = udp_flow_src_port(bareudp->net, skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) bareudp->sport_min,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) USHRT_MAX, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) info->key.tp_dst = bareudp->port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) static const struct net_device_ops bareudp_netdev_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) .ndo_init = bareudp_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) .ndo_uninit = bareudp_uninit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) .ndo_open = bareudp_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) .ndo_stop = bareudp_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) .ndo_start_xmit = bareudp_xmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) .ndo_get_stats64 = ip_tunnel_get_stats64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) .ndo_fill_metadata_dst = bareudp_fill_metadata_dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) static const struct nla_policy bareudp_policy[IFLA_BAREUDP_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) [IFLA_BAREUDP_PORT] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) [IFLA_BAREUDP_ETHERTYPE] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) [IFLA_BAREUDP_SRCPORT_MIN] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) [IFLA_BAREUDP_MULTIPROTO_MODE] = { .type = NLA_FLAG },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) /* Info for udev, that this is a virtual tunnel endpoint */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) static struct device_type bareudp_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) .name = "bareudp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) /* Initialize the device structure. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) static void bareudp_setup(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) dev->netdev_ops = &bareudp_netdev_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) dev->needs_free_netdev = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) SET_NETDEV_DEVTYPE(dev, &bareudp_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) dev->features |= NETIF_F_RXCSUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) dev->features |= NETIF_F_LLTX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) dev->features |= NETIF_F_GSO_SOFTWARE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) dev->hw_features |= NETIF_F_GSO_SOFTWARE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) dev->hard_header_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) dev->addr_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) dev->mtu = ETH_DATA_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) dev->min_mtu = IPV4_MIN_MTU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) dev->max_mtu = IP_MAX_MTU - BAREUDP_BASE_HLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) dev->type = ARPHRD_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) netif_keep_dst(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) dev->priv_flags |= IFF_NO_QUEUE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
^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) static int bareudp_validate(struct nlattr *tb[], struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) if (!data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) NL_SET_ERR_MSG(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) "Not enough attributes provided to perform the operation");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) static int bareudp2info(struct nlattr *data[], struct bareudp_conf *conf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) memset(conf, 0, sizeof(*conf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) if (!data[IFLA_BAREUDP_PORT]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) NL_SET_ERR_MSG(extack, "port not specified");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) if (!data[IFLA_BAREUDP_ETHERTYPE]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) NL_SET_ERR_MSG(extack, "ethertype not specified");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) return -EINVAL;
^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) if (data[IFLA_BAREUDP_PORT])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) conf->port = nla_get_u16(data[IFLA_BAREUDP_PORT]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) if (data[IFLA_BAREUDP_ETHERTYPE])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) conf->ethertype = nla_get_u16(data[IFLA_BAREUDP_ETHERTYPE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) if (data[IFLA_BAREUDP_SRCPORT_MIN])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) conf->sport_min = nla_get_u16(data[IFLA_BAREUDP_SRCPORT_MIN]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) if (data[IFLA_BAREUDP_MULTIPROTO_MODE])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) conf->multi_proto_mode = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) static struct bareudp_dev *bareudp_find_dev(struct bareudp_net *bn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) const struct bareudp_conf *conf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) struct bareudp_dev *bareudp, *t = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) list_for_each_entry(bareudp, &bn->bareudp_list, next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) if (conf->port == bareudp->port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) t = bareudp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) static int bareudp_configure(struct net *net, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) struct bareudp_conf *conf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) struct bareudp_net *bn = net_generic(net, bareudp_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) struct bareudp_dev *t, *bareudp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) bareudp->net = net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) bareudp->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) t = bareudp_find_dev(bn, conf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) if (t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) if (conf->multi_proto_mode &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) (conf->ethertype != htons(ETH_P_MPLS_UC) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) conf->ethertype != htons(ETH_P_IP)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) bareudp->port = conf->port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) bareudp->ethertype = conf->ethertype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) bareudp->sport_min = conf->sport_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) bareudp->multi_proto_mode = conf->multi_proto_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) err = register_netdevice(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) list_add(&bareudp->next, &bn->bareudp_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) static int bareudp_link_config(struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) struct nlattr *tb[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) if (tb[IFLA_MTU]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) static void bareudp_dellink(struct net_device *dev, struct list_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) struct bareudp_dev *bareudp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) list_del(&bareudp->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) unregister_netdevice_queue(dev, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) static int bareudp_newlink(struct net *net, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) struct nlattr *tb[], struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) struct bareudp_conf conf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) LIST_HEAD(list_kill);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) err = bareudp2info(data, &conf, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) err = bareudp_configure(net, dev, &conf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) err = bareudp_link_config(dev, tb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) goto err_unconfig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) err_unconfig:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) bareudp_dellink(dev, &list_kill);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) unregister_netdevice_many(&list_kill);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) static size_t bareudp_get_size(const struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) return nla_total_size(sizeof(__be16)) + /* IFLA_BAREUDP_PORT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) nla_total_size(sizeof(__be16)) + /* IFLA_BAREUDP_ETHERTYPE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) nla_total_size(sizeof(__u16)) + /* IFLA_BAREUDP_SRCPORT_MIN */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) nla_total_size(0) + /* IFLA_BAREUDP_MULTIPROTO_MODE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) static int bareudp_fill_info(struct sk_buff *skb, const struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) struct bareudp_dev *bareudp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) if (nla_put_be16(skb, IFLA_BAREUDP_PORT, bareudp->port))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) if (nla_put_be16(skb, IFLA_BAREUDP_ETHERTYPE, bareudp->ethertype))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) if (nla_put_u16(skb, IFLA_BAREUDP_SRCPORT_MIN, bareudp->sport_min))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) if (bareudp->multi_proto_mode &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) nla_put_flag(skb, IFLA_BAREUDP_MULTIPROTO_MODE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) static struct rtnl_link_ops bareudp_link_ops __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) .kind = "bareudp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) .maxtype = IFLA_BAREUDP_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) .policy = bareudp_policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) .priv_size = sizeof(struct bareudp_dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) .setup = bareudp_setup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) .validate = bareudp_validate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) .newlink = bareudp_newlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) .dellink = bareudp_dellink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) .get_size = bareudp_get_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) .fill_info = bareudp_fill_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) struct net_device *bareudp_dev_create(struct net *net, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) u8 name_assign_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) struct bareudp_conf *conf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) struct nlattr *tb[IFLA_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) LIST_HEAD(list_kill);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) memset(tb, 0, sizeof(tb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) dev = rtnl_create_link(net, name, name_assign_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) &bareudp_link_ops, tb, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) if (IS_ERR(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) err = bareudp_configure(net, dev, conf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) free_netdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) err = dev_set_mtu(dev, IP_MAX_MTU - BAREUDP_BASE_HLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) err = rtnl_configure_link(dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) bareudp_dellink(dev, &list_kill);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) unregister_netdevice_many(&list_kill);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) EXPORT_SYMBOL_GPL(bareudp_dev_create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) static __net_init int bareudp_init_net(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) struct bareudp_net *bn = net_generic(net, bareudp_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) INIT_LIST_HEAD(&bn->bareudp_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) static void bareudp_destroy_tunnels(struct net *net, struct list_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) struct bareudp_net *bn = net_generic(net, bareudp_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) struct bareudp_dev *bareudp, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) list_for_each_entry_safe(bareudp, next, &bn->bareudp_list, next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) unregister_netdevice_queue(bareudp->dev, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) static void __net_exit bareudp_exit_batch_net(struct list_head *net_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) struct net *net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) list_for_each_entry(net, net_list, exit_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) bareudp_destroy_tunnels(net, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) /* unregister the devices gathered above */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) unregister_netdevice_many(&list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) static struct pernet_operations bareudp_net_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) .init = bareudp_init_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) .exit_batch = bareudp_exit_batch_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) .id = &bareudp_net_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) .size = sizeof(struct bareudp_net),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) static int __init bareudp_init_module(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) rc = register_pernet_subsys(&bareudp_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) goto out1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) rc = rtnl_link_register(&bareudp_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) goto out2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) out2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) unregister_pernet_subsys(&bareudp_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) out1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) late_initcall(bareudp_init_module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) static void __exit bareudp_cleanup_module(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) rtnl_link_unregister(&bareudp_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) unregister_pernet_subsys(&bareudp_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) module_exit(bareudp_cleanup_module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) MODULE_ALIAS_RTNL_LINK("bareudp");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) MODULE_AUTHOR("Martin Varghese <martin.varghese@nokia.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) MODULE_DESCRIPTION("Interface driver for UDP encapsulated traffic");