Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * XFRM virtual interface |
| 4 | * |
| 5 | * Copyright (C) 2018 secunet Security Networks AG |
| 6 | * |
| 7 | * Author: |
| 8 | * Steffen Klassert <steffen.klassert@secunet.com> |
| 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/capability.h> |
| 13 | #include <linux/errno.h> |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/sockios.h> |
| 16 | #include <linux/icmp.h> |
| 17 | #include <linux/if.h> |
| 18 | #include <linux/in.h> |
| 19 | #include <linux/ip.h> |
| 20 | #include <linux/net.h> |
| 21 | #include <linux/in6.h> |
| 22 | #include <linux/netdevice.h> |
| 23 | #include <linux/if_link.h> |
| 24 | #include <linux/if_arp.h> |
| 25 | #include <linux/icmpv6.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/route.h> |
| 28 | #include <linux/rtnetlink.h> |
| 29 | #include <linux/netfilter_ipv6.h> |
| 30 | #include <linux/slab.h> |
| 31 | #include <linux/hash.h> |
| 32 | |
| 33 | #include <linux/uaccess.h> |
| 34 | #include <linux/atomic.h> |
| 35 | |
| 36 | #include <net/icmp.h> |
| 37 | #include <net/ip.h> |
| 38 | #include <net/ipv6.h> |
| 39 | #include <net/ip6_route.h> |
Jason A. Donenfeld | 8f9a1fa | 2020-06-29 19:06:25 -0600 | [diff] [blame] | 40 | #include <net/ip_tunnels.h> |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 41 | #include <net/addrconf.h> |
| 42 | #include <net/xfrm.h> |
| 43 | #include <net/net_namespace.h> |
| 44 | #include <net/netns/generic.h> |
| 45 | #include <linux/etherdevice.h> |
| 46 | |
| 47 | static int xfrmi_dev_init(struct net_device *dev); |
| 48 | static void xfrmi_dev_setup(struct net_device *dev); |
| 49 | static struct rtnl_link_ops xfrmi_link_ops __read_mostly; |
| 50 | static unsigned int xfrmi_net_id __read_mostly; |
Eyal Birger | 2749c69 | 2020-07-09 13:16:51 +0300 | [diff] [blame] | 51 | static const struct net_device_ops xfrmi_netdev_ops; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 52 | |
Eyal Birger | e98e445 | 2020-07-09 13:16:52 +0300 | [diff] [blame] | 53 | #define XFRMI_HASH_BITS 8 |
| 54 | #define XFRMI_HASH_SIZE BIT(XFRMI_HASH_BITS) |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 55 | |
| 56 | struct xfrmi_net { |
| 57 | /* lists for storing interfaces in use */ |
Eyal Birger | e98e445 | 2020-07-09 13:16:52 +0300 | [diff] [blame] | 58 | struct xfrm_if __rcu *xfrmi[XFRMI_HASH_SIZE]; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | #define for_each_xfrmi_rcu(start, xi) \ |
| 62 | for (xi = rcu_dereference(start); xi; xi = rcu_dereference(xi->next)) |
| 63 | |
Eyal Birger | e98e445 | 2020-07-09 13:16:52 +0300 | [diff] [blame] | 64 | static u32 xfrmi_hash(u32 if_id) |
| 65 | { |
| 66 | return hash_32(if_id, XFRMI_HASH_BITS); |
| 67 | } |
| 68 | |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 69 | static struct xfrm_if *xfrmi_lookup(struct net *net, struct xfrm_state *x) |
| 70 | { |
| 71 | struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id); |
| 72 | struct xfrm_if *xi; |
| 73 | |
Eyal Birger | e98e445 | 2020-07-09 13:16:52 +0300 | [diff] [blame] | 74 | for_each_xfrmi_rcu(xfrmn->xfrmi[xfrmi_hash(x->if_id)], xi) { |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 75 | if (x->if_id == xi->p.if_id && |
| 76 | (xi->dev->flags & IFF_UP)) |
| 77 | return xi; |
| 78 | } |
| 79 | |
| 80 | return NULL; |
| 81 | } |
| 82 | |
Martin Willi | 025c65e | 2019-03-26 13:20:43 +0100 | [diff] [blame] | 83 | static struct xfrm_if *xfrmi_decode_session(struct sk_buff *skb, |
| 84 | unsigned short family) |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 85 | { |
Eyal Birger | 2749c69 | 2020-07-09 13:16:51 +0300 | [diff] [blame] | 86 | struct net_device *dev; |
Martin Willi | 025c65e | 2019-03-26 13:20:43 +0100 | [diff] [blame] | 87 | int ifindex = 0; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 88 | |
Tobias Brunner | 660899d | 2019-02-18 10:49:39 +0100 | [diff] [blame] | 89 | if (!secpath_exists(skb) || !skb->dev) |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 90 | return NULL; |
| 91 | |
Martin Willi | 025c65e | 2019-03-26 13:20:43 +0100 | [diff] [blame] | 92 | switch (family) { |
| 93 | case AF_INET6: |
| 94 | ifindex = inet6_sdif(skb); |
| 95 | break; |
| 96 | case AF_INET: |
| 97 | ifindex = inet_sdif(skb); |
| 98 | break; |
| 99 | } |
Martin Willi | 025c65e | 2019-03-26 13:20:43 +0100 | [diff] [blame] | 100 | |
Eyal Birger | 2749c69 | 2020-07-09 13:16:51 +0300 | [diff] [blame] | 101 | if (ifindex) { |
| 102 | struct net *net = xs_net(xfrm_input_state(skb)); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 103 | |
Eyal Birger | 2749c69 | 2020-07-09 13:16:51 +0300 | [diff] [blame] | 104 | dev = dev_get_by_index_rcu(net, ifindex); |
| 105 | } else { |
| 106 | dev = skb->dev; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 107 | } |
| 108 | |
Eyal Birger | 2749c69 | 2020-07-09 13:16:51 +0300 | [diff] [blame] | 109 | if (!dev || !(dev->flags & IFF_UP)) |
| 110 | return NULL; |
| 111 | if (dev->netdev_ops != &xfrmi_netdev_ops) |
| 112 | return NULL; |
| 113 | |
| 114 | return netdev_priv(dev); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static void xfrmi_link(struct xfrmi_net *xfrmn, struct xfrm_if *xi) |
| 118 | { |
Eyal Birger | e98e445 | 2020-07-09 13:16:52 +0300 | [diff] [blame] | 119 | struct xfrm_if __rcu **xip = &xfrmn->xfrmi[xfrmi_hash(xi->p.if_id)]; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 120 | |
| 121 | rcu_assign_pointer(xi->next , rtnl_dereference(*xip)); |
| 122 | rcu_assign_pointer(*xip, xi); |
| 123 | } |
| 124 | |
| 125 | static void xfrmi_unlink(struct xfrmi_net *xfrmn, struct xfrm_if *xi) |
| 126 | { |
| 127 | struct xfrm_if __rcu **xip; |
| 128 | struct xfrm_if *iter; |
| 129 | |
Eyal Birger | e98e445 | 2020-07-09 13:16:52 +0300 | [diff] [blame] | 130 | for (xip = &xfrmn->xfrmi[xfrmi_hash(xi->p.if_id)]; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 131 | (iter = rtnl_dereference(*xip)) != NULL; |
| 132 | xip = &iter->next) { |
| 133 | if (xi == iter) { |
| 134 | rcu_assign_pointer(*xip, xi->next); |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | static void xfrmi_dev_free(struct net_device *dev) |
| 141 | { |
Li RongQing | 4da4025 | 2018-09-30 15:06:06 +0800 | [diff] [blame] | 142 | struct xfrm_if *xi = netdev_priv(dev); |
| 143 | |
| 144 | gro_cells_destroy(&xi->gro_cells); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 145 | free_percpu(dev->tstats); |
| 146 | } |
| 147 | |
Nicolas Dichtel | 56c5ee1 | 2019-07-02 17:51:39 +0200 | [diff] [blame] | 148 | static int xfrmi_create(struct net_device *dev) |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 149 | { |
| 150 | struct xfrm_if *xi = netdev_priv(dev); |
| 151 | struct net *net = dev_net(dev); |
| 152 | struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id); |
| 153 | int err; |
| 154 | |
| 155 | dev->rtnl_link_ops = &xfrmi_link_ops; |
| 156 | err = register_netdevice(dev); |
| 157 | if (err < 0) |
| 158 | goto out; |
| 159 | |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 160 | xfrmi_link(xfrmn, xi); |
| 161 | |
| 162 | return 0; |
| 163 | |
| 164 | out: |
| 165 | return err; |
| 166 | } |
| 167 | |
Nicolas Dichtel | 56c5ee1 | 2019-07-02 17:51:39 +0200 | [diff] [blame] | 168 | static struct xfrm_if *xfrmi_locate(struct net *net, struct xfrm_if_parms *p) |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 169 | { |
| 170 | struct xfrm_if __rcu **xip; |
| 171 | struct xfrm_if *xi; |
| 172 | struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id); |
| 173 | |
Eyal Birger | e98e445 | 2020-07-09 13:16:52 +0300 | [diff] [blame] | 174 | for (xip = &xfrmn->xfrmi[xfrmi_hash(p->if_id)]; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 175 | (xi = rtnl_dereference(*xip)) != NULL; |
Nicolas Dichtel | 56c5ee1 | 2019-07-02 17:51:39 +0200 | [diff] [blame] | 176 | xip = &xi->next) |
| 177 | if (xi->p.if_id == p->if_id) |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 178 | return xi; |
Nicolas Dichtel | 56c5ee1 | 2019-07-02 17:51:39 +0200 | [diff] [blame] | 179 | |
| 180 | return NULL; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | static void xfrmi_dev_uninit(struct net_device *dev) |
| 184 | { |
| 185 | struct xfrm_if *xi = netdev_priv(dev); |
| 186 | struct xfrmi_net *xfrmn = net_generic(xi->net, xfrmi_net_id); |
| 187 | |
| 188 | xfrmi_unlink(xfrmn, xi); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static void xfrmi_scrub_packet(struct sk_buff *skb, bool xnet) |
| 192 | { |
| 193 | skb->tstamp = 0; |
| 194 | skb->pkt_type = PACKET_HOST; |
| 195 | skb->skb_iif = 0; |
| 196 | skb->ignore_df = 0; |
| 197 | skb_dst_drop(skb); |
Florian Westphal | 895b5c9 | 2019-09-29 20:54:03 +0200 | [diff] [blame] | 198 | nf_reset_ct(skb); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 199 | nf_reset_trace(skb); |
| 200 | |
| 201 | if (!xnet) |
| 202 | return; |
| 203 | |
| 204 | ipvs_reset(skb); |
| 205 | secpath_reset(skb); |
| 206 | skb_orphan(skb); |
| 207 | skb->mark = 0; |
| 208 | } |
| 209 | |
| 210 | static int xfrmi_rcv_cb(struct sk_buff *skb, int err) |
| 211 | { |
Florian Westphal | 4c145dc | 2019-03-29 21:16:31 +0100 | [diff] [blame] | 212 | const struct xfrm_mode *inner_mode; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 213 | struct net_device *dev; |
| 214 | struct xfrm_state *x; |
| 215 | struct xfrm_if *xi; |
| 216 | bool xnet; |
| 217 | |
Florian Westphal | 26912e3 | 2018-12-18 17:15:24 +0100 | [diff] [blame] | 218 | if (err && !secpath_exists(skb)) |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 219 | return 0; |
| 220 | |
| 221 | x = xfrm_input_state(skb); |
| 222 | |
| 223 | xi = xfrmi_lookup(xs_net(x), x); |
| 224 | if (!xi) |
| 225 | return 1; |
| 226 | |
| 227 | dev = xi->dev; |
| 228 | skb->dev = dev; |
| 229 | |
| 230 | if (err) { |
| 231 | dev->stats.rx_errors++; |
| 232 | dev->stats.rx_dropped++; |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | xnet = !net_eq(xi->net, dev_net(skb->dev)); |
| 238 | |
| 239 | if (xnet) { |
Florian Westphal | c9500d7 | 2019-03-29 21:16:32 +0100 | [diff] [blame] | 240 | inner_mode = &x->inner_mode; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 241 | |
| 242 | if (x->sel.family == AF_UNSPEC) { |
| 243 | inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol); |
| 244 | if (inner_mode == NULL) { |
| 245 | XFRM_INC_STATS(dev_net(skb->dev), |
| 246 | LINUX_MIB_XFRMINSTATEMODEERROR); |
| 247 | return -EINVAL; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | if (!xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, |
Florian Westphal | b45714b | 2019-03-29 21:16:23 +0100 | [diff] [blame] | 252 | inner_mode->family)) |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 253 | return -EPERM; |
| 254 | } |
| 255 | |
| 256 | xfrmi_scrub_packet(skb, xnet); |
Fabian Frederick | c852162 | 2020-10-05 22:36:34 +0200 | [diff] [blame] | 257 | dev_sw_netstats_rx_add(dev, skb->len); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | static int |
| 263 | xfrmi_xmit2(struct sk_buff *skb, struct net_device *dev, struct flowi *fl) |
| 264 | { |
| 265 | struct xfrm_if *xi = netdev_priv(dev); |
| 266 | struct net_device_stats *stats = &xi->dev->stats; |
| 267 | struct dst_entry *dst = skb_dst(skb); |
| 268 | unsigned int length = skb->len; |
| 269 | struct net_device *tdev; |
| 270 | struct xfrm_state *x; |
| 271 | int err = -1; |
| 272 | int mtu; |
| 273 | |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 274 | dst_hold(dst); |
Benedict Wong | bc56b33 | 2018-07-19 10:50:44 -0700 | [diff] [blame] | 275 | dst = xfrm_lookup_with_ifid(xi->net, dst, fl, NULL, 0, xi->p.if_id); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 276 | if (IS_ERR(dst)) { |
| 277 | err = PTR_ERR(dst); |
| 278 | dst = NULL; |
| 279 | goto tx_err_link_failure; |
| 280 | } |
| 281 | |
| 282 | x = dst->xfrm; |
| 283 | if (!x) |
| 284 | goto tx_err_link_failure; |
| 285 | |
| 286 | if (x->if_id != xi->p.if_id) |
| 287 | goto tx_err_link_failure; |
| 288 | |
| 289 | tdev = dst->dev; |
| 290 | |
| 291 | if (tdev == dev) { |
| 292 | stats->collisions++; |
| 293 | net_warn_ratelimited("%s: Local routing loop detected!\n", |
Nicolas Dichtel | e0aaa33 | 2019-07-15 12:00:21 +0200 | [diff] [blame] | 294 | dev->name); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 295 | goto tx_err_dst_release; |
| 296 | } |
| 297 | |
| 298 | mtu = dst_mtu(dst); |
Sabrina Dubroca | 45a36a1 | 2020-08-04 11:37:29 +0200 | [diff] [blame] | 299 | if (skb->len > mtu) { |
Xu Wang | 8aaea2b | 2020-01-13 09:00:36 +0000 | [diff] [blame] | 300 | skb_dst_update_pmtu_no_confirm(skb, mtu); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 301 | |
| 302 | if (skb->protocol == htons(ETH_P_IPV6)) { |
| 303 | if (mtu < IPV6_MIN_MTU) |
| 304 | mtu = IPV6_MIN_MTU; |
| 305 | |
Jason A. Donenfeld | 45942ba | 2020-02-11 20:47:09 +0100 | [diff] [blame] | 306 | icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 307 | } else { |
Jason A. Donenfeld | 45942ba | 2020-02-11 20:47:09 +0100 | [diff] [blame] | 308 | icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, |
| 309 | htonl(mtu)); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | dst_release(dst); |
| 313 | return -EMSGSIZE; |
| 314 | } |
| 315 | |
| 316 | xfrmi_scrub_packet(skb, !net_eq(xi->net, dev_net(dev))); |
| 317 | skb_dst_set(skb, dst); |
| 318 | skb->dev = tdev; |
| 319 | |
| 320 | err = dst_output(xi->net, skb->sk, skb); |
| 321 | if (net_xmit_eval(err) == 0) { |
| 322 | struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats); |
| 323 | |
| 324 | u64_stats_update_begin(&tstats->syncp); |
| 325 | tstats->tx_bytes += length; |
| 326 | tstats->tx_packets++; |
| 327 | u64_stats_update_end(&tstats->syncp); |
| 328 | } else { |
| 329 | stats->tx_errors++; |
| 330 | stats->tx_aborted_errors++; |
| 331 | } |
| 332 | |
| 333 | return 0; |
| 334 | tx_err_link_failure: |
| 335 | stats->tx_carrier_errors++; |
| 336 | dst_link_failure(skb); |
| 337 | tx_err_dst_release: |
| 338 | dst_release(dst); |
| 339 | return err; |
| 340 | } |
| 341 | |
| 342 | static netdev_tx_t xfrmi_xmit(struct sk_buff *skb, struct net_device *dev) |
| 343 | { |
| 344 | struct xfrm_if *xi = netdev_priv(dev); |
| 345 | struct net_device_stats *stats = &xi->dev->stats; |
Nicolas Dichtel | f042365 | 2020-01-13 09:32:47 +0100 | [diff] [blame] | 346 | struct dst_entry *dst = skb_dst(skb); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 347 | struct flowi fl; |
| 348 | int ret; |
| 349 | |
| 350 | memset(&fl, 0, sizeof(fl)); |
| 351 | |
| 352 | switch (skb->protocol) { |
| 353 | case htons(ETH_P_IPV6): |
| 354 | xfrm_decode_session(skb, &fl, AF_INET6); |
| 355 | memset(IP6CB(skb), 0, sizeof(*IP6CB(skb))); |
Nicolas Dichtel | f042365 | 2020-01-13 09:32:47 +0100 | [diff] [blame] | 356 | if (!dst) { |
| 357 | fl.u.ip6.flowi6_oif = dev->ifindex; |
| 358 | fl.u.ip6.flowi6_flags |= FLOWI_FLAG_ANYSRC; |
| 359 | dst = ip6_route_output(dev_net(dev), NULL, &fl.u.ip6); |
| 360 | if (dst->error) { |
| 361 | dst_release(dst); |
| 362 | stats->tx_carrier_errors++; |
| 363 | goto tx_err; |
| 364 | } |
| 365 | skb_dst_set(skb, dst); |
| 366 | } |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 367 | break; |
| 368 | case htons(ETH_P_IP): |
| 369 | xfrm_decode_session(skb, &fl, AF_INET); |
| 370 | memset(IPCB(skb), 0, sizeof(*IPCB(skb))); |
Nicolas Dichtel | f042365 | 2020-01-13 09:32:47 +0100 | [diff] [blame] | 371 | if (!dst) { |
| 372 | struct rtable *rt; |
| 373 | |
| 374 | fl.u.ip4.flowi4_oif = dev->ifindex; |
| 375 | fl.u.ip4.flowi4_flags |= FLOWI_FLAG_ANYSRC; |
| 376 | rt = __ip_route_output_key(dev_net(dev), &fl.u.ip4); |
| 377 | if (IS_ERR(rt)) { |
| 378 | stats->tx_carrier_errors++; |
| 379 | goto tx_err; |
| 380 | } |
| 381 | skb_dst_set(skb, &rt->dst); |
| 382 | } |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 383 | break; |
| 384 | default: |
| 385 | goto tx_err; |
| 386 | } |
| 387 | |
Nicolas Dichtel | 22d6552 | 2019-07-15 12:00:23 +0200 | [diff] [blame] | 388 | fl.flowi_oif = xi->p.link; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 389 | |
| 390 | ret = xfrmi_xmit2(skb, dev, &fl); |
| 391 | if (ret < 0) |
| 392 | goto tx_err; |
| 393 | |
| 394 | return NETDEV_TX_OK; |
| 395 | |
| 396 | tx_err: |
| 397 | stats->tx_errors++; |
| 398 | stats->tx_dropped++; |
| 399 | kfree_skb(skb); |
| 400 | return NETDEV_TX_OK; |
| 401 | } |
| 402 | |
| 403 | static int xfrmi4_err(struct sk_buff *skb, u32 info) |
| 404 | { |
| 405 | const struct iphdr *iph = (const struct iphdr *)skb->data; |
| 406 | struct net *net = dev_net(skb->dev); |
| 407 | int protocol = iph->protocol; |
| 408 | struct ip_comp_hdr *ipch; |
| 409 | struct ip_esp_hdr *esph; |
| 410 | struct ip_auth_hdr *ah ; |
| 411 | struct xfrm_state *x; |
| 412 | struct xfrm_if *xi; |
| 413 | __be32 spi; |
| 414 | |
| 415 | switch (protocol) { |
| 416 | case IPPROTO_ESP: |
| 417 | esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2)); |
| 418 | spi = esph->spi; |
| 419 | break; |
| 420 | case IPPROTO_AH: |
| 421 | ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2)); |
| 422 | spi = ah->spi; |
| 423 | break; |
| 424 | case IPPROTO_COMP: |
| 425 | ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2)); |
| 426 | spi = htonl(ntohs(ipch->cpi)); |
| 427 | break; |
| 428 | default: |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | switch (icmp_hdr(skb)->type) { |
| 433 | case ICMP_DEST_UNREACH: |
| 434 | if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED) |
| 435 | return 0; |
| 436 | case ICMP_REDIRECT: |
| 437 | break; |
| 438 | default: |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr, |
| 443 | spi, protocol, AF_INET); |
| 444 | if (!x) |
| 445 | return 0; |
| 446 | |
| 447 | xi = xfrmi_lookup(net, x); |
| 448 | if (!xi) { |
| 449 | xfrm_state_put(x); |
| 450 | return -1; |
| 451 | } |
| 452 | |
| 453 | if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH) |
Maciej Żenczykowski | d888f39 | 2018-09-25 20:56:26 -0700 | [diff] [blame] | 454 | ipv4_update_pmtu(skb, net, info, 0, protocol); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 455 | else |
Maciej Żenczykowski | 1042caa | 2018-09-25 20:56:27 -0700 | [diff] [blame] | 456 | ipv4_redirect(skb, net, 0, protocol); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 457 | xfrm_state_put(x); |
| 458 | |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | static int xfrmi6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, |
| 463 | u8 type, u8 code, int offset, __be32 info) |
| 464 | { |
| 465 | const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data; |
| 466 | struct net *net = dev_net(skb->dev); |
| 467 | int protocol = iph->nexthdr; |
| 468 | struct ip_comp_hdr *ipch; |
| 469 | struct ip_esp_hdr *esph; |
| 470 | struct ip_auth_hdr *ah; |
| 471 | struct xfrm_state *x; |
| 472 | struct xfrm_if *xi; |
| 473 | __be32 spi; |
| 474 | |
| 475 | switch (protocol) { |
| 476 | case IPPROTO_ESP: |
| 477 | esph = (struct ip_esp_hdr *)(skb->data + offset); |
| 478 | spi = esph->spi; |
| 479 | break; |
| 480 | case IPPROTO_AH: |
| 481 | ah = (struct ip_auth_hdr *)(skb->data + offset); |
| 482 | spi = ah->spi; |
| 483 | break; |
| 484 | case IPPROTO_COMP: |
| 485 | ipch = (struct ip_comp_hdr *)(skb->data + offset); |
| 486 | spi = htonl(ntohs(ipch->cpi)); |
| 487 | break; |
| 488 | default: |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | if (type != ICMPV6_PKT_TOOBIG && |
| 493 | type != NDISC_REDIRECT) |
| 494 | return 0; |
| 495 | |
| 496 | x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr, |
| 497 | spi, protocol, AF_INET6); |
| 498 | if (!x) |
| 499 | return 0; |
| 500 | |
| 501 | xi = xfrmi_lookup(net, x); |
| 502 | if (!xi) { |
| 503 | xfrm_state_put(x); |
| 504 | return -1; |
| 505 | } |
| 506 | |
| 507 | if (type == NDISC_REDIRECT) |
| 508 | ip6_redirect(skb, net, skb->dev->ifindex, 0, |
| 509 | sock_net_uid(net, NULL)); |
| 510 | else |
| 511 | ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL)); |
| 512 | xfrm_state_put(x); |
| 513 | |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | static int xfrmi_change(struct xfrm_if *xi, const struct xfrm_if_parms *p) |
| 518 | { |
| 519 | if (xi->p.link != p->link) |
| 520 | return -EINVAL; |
| 521 | |
| 522 | xi->p.if_id = p->if_id; |
| 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | static int xfrmi_update(struct xfrm_if *xi, struct xfrm_if_parms *p) |
| 528 | { |
Nicolas Dichtel | c5d1030 | 2019-07-15 12:00:22 +0200 | [diff] [blame] | 529 | struct net *net = xi->net; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 530 | struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id); |
| 531 | int err; |
| 532 | |
| 533 | xfrmi_unlink(xfrmn, xi); |
| 534 | synchronize_net(); |
| 535 | err = xfrmi_change(xi, p); |
| 536 | xfrmi_link(xfrmn, xi); |
| 537 | netdev_state_change(xi->dev); |
| 538 | return err; |
| 539 | } |
| 540 | |
| 541 | static void xfrmi_get_stats64(struct net_device *dev, |
| 542 | struct rtnl_link_stats64 *s) |
| 543 | { |
Heiner Kallweit | 5fc3594 | 2020-10-12 10:19:59 +0200 | [diff] [blame] | 544 | dev_fetch_sw_netstats(s, dev->tstats); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 545 | |
| 546 | s->rx_dropped = dev->stats.rx_dropped; |
| 547 | s->tx_dropped = dev->stats.tx_dropped; |
| 548 | } |
| 549 | |
| 550 | static int xfrmi_get_iflink(const struct net_device *dev) |
| 551 | { |
| 552 | struct xfrm_if *xi = netdev_priv(dev); |
| 553 | |
Nicolas Dichtel | 22d6552 | 2019-07-15 12:00:23 +0200 | [diff] [blame] | 554 | return xi->p.link; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | |
| 558 | static const struct net_device_ops xfrmi_netdev_ops = { |
| 559 | .ndo_init = xfrmi_dev_init, |
| 560 | .ndo_uninit = xfrmi_dev_uninit, |
| 561 | .ndo_start_xmit = xfrmi_xmit, |
| 562 | .ndo_get_stats64 = xfrmi_get_stats64, |
| 563 | .ndo_get_iflink = xfrmi_get_iflink, |
| 564 | }; |
| 565 | |
| 566 | static void xfrmi_dev_setup(struct net_device *dev) |
| 567 | { |
| 568 | dev->netdev_ops = &xfrmi_netdev_ops; |
Jason A. Donenfeld | 8f9a1fa | 2020-06-29 19:06:25 -0600 | [diff] [blame] | 569 | dev->header_ops = &ip_tunnel_header_ops; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 570 | dev->type = ARPHRD_NONE; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 571 | dev->mtu = ETH_DATA_LEN; |
| 572 | dev->min_mtu = ETH_MIN_MTU; |
Nicolas Dichtel | f042365 | 2020-01-13 09:32:47 +0100 | [diff] [blame] | 573 | dev->max_mtu = IP_MAX_MTU; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 574 | dev->flags = IFF_NOARP; |
| 575 | dev->needs_free_netdev = true; |
| 576 | dev->priv_destructor = xfrmi_dev_free; |
| 577 | netif_keep_dst(dev); |
Nicolas Dichtel | 22d6552 | 2019-07-15 12:00:23 +0200 | [diff] [blame] | 578 | |
| 579 | eth_broadcast_addr(dev->broadcast); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | static int xfrmi_dev_init(struct net_device *dev) |
| 583 | { |
| 584 | struct xfrm_if *xi = netdev_priv(dev); |
Nicolas Dichtel | 22d6552 | 2019-07-15 12:00:23 +0200 | [diff] [blame] | 585 | struct net_device *phydev = __dev_get_by_index(xi->net, xi->p.link); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 586 | int err; |
| 587 | |
| 588 | dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
| 589 | if (!dev->tstats) |
| 590 | return -ENOMEM; |
| 591 | |
| 592 | err = gro_cells_init(&xi->gro_cells, dev); |
| 593 | if (err) { |
| 594 | free_percpu(dev->tstats); |
| 595 | return err; |
| 596 | } |
| 597 | |
| 598 | dev->features |= NETIF_F_LLTX; |
| 599 | |
Nicolas Dichtel | 22d6552 | 2019-07-15 12:00:23 +0200 | [diff] [blame] | 600 | if (phydev) { |
| 601 | dev->needed_headroom = phydev->needed_headroom; |
| 602 | dev->needed_tailroom = phydev->needed_tailroom; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 603 | |
Nicolas Dichtel | 22d6552 | 2019-07-15 12:00:23 +0200 | [diff] [blame] | 604 | if (is_zero_ether_addr(dev->dev_addr)) |
| 605 | eth_hw_addr_inherit(dev, phydev); |
| 606 | if (is_zero_ether_addr(dev->broadcast)) |
| 607 | memcpy(dev->broadcast, phydev->broadcast, |
| 608 | dev->addr_len); |
| 609 | } else { |
| 610 | eth_hw_addr_random(dev); |
| 611 | eth_broadcast_addr(dev->broadcast); |
| 612 | } |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 613 | |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | static int xfrmi_validate(struct nlattr *tb[], struct nlattr *data[], |
| 618 | struct netlink_ext_ack *extack) |
| 619 | { |
| 620 | return 0; |
| 621 | } |
| 622 | |
| 623 | static void xfrmi_netlink_parms(struct nlattr *data[], |
| 624 | struct xfrm_if_parms *parms) |
| 625 | { |
| 626 | memset(parms, 0, sizeof(*parms)); |
| 627 | |
| 628 | if (!data) |
| 629 | return; |
| 630 | |
| 631 | if (data[IFLA_XFRM_LINK]) |
| 632 | parms->link = nla_get_u32(data[IFLA_XFRM_LINK]); |
| 633 | |
| 634 | if (data[IFLA_XFRM_IF_ID]) |
| 635 | parms->if_id = nla_get_u32(data[IFLA_XFRM_IF_ID]); |
| 636 | } |
| 637 | |
| 638 | static int xfrmi_newlink(struct net *src_net, struct net_device *dev, |
| 639 | struct nlattr *tb[], struct nlattr *data[], |
| 640 | struct netlink_ext_ack *extack) |
| 641 | { |
| 642 | struct net *net = dev_net(dev); |
Nicolas Dichtel | 56c5ee1 | 2019-07-02 17:51:39 +0200 | [diff] [blame] | 643 | struct xfrm_if_parms p; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 644 | struct xfrm_if *xi; |
Nicolas Dichtel | 56c5ee1 | 2019-07-02 17:51:39 +0200 | [diff] [blame] | 645 | int err; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 646 | |
Nicolas Dichtel | 56c5ee1 | 2019-07-02 17:51:39 +0200 | [diff] [blame] | 647 | xfrmi_netlink_parms(data, &p); |
Nicolas Dichtel | 56c5ee1 | 2019-07-02 17:51:39 +0200 | [diff] [blame] | 648 | xi = xfrmi_locate(net, &p); |
| 649 | if (xi) |
| 650 | return -EEXIST; |
| 651 | |
| 652 | xi = netdev_priv(dev); |
| 653 | xi->p = p; |
| 654 | xi->net = net; |
| 655 | xi->dev = dev; |
Nicolas Dichtel | 56c5ee1 | 2019-07-02 17:51:39 +0200 | [diff] [blame] | 656 | |
| 657 | err = xfrmi_create(dev); |
Nicolas Dichtel | 56c5ee1 | 2019-07-02 17:51:39 +0200 | [diff] [blame] | 658 | return err; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | static void xfrmi_dellink(struct net_device *dev, struct list_head *head) |
| 662 | { |
| 663 | unregister_netdevice_queue(dev, head); |
| 664 | } |
| 665 | |
| 666 | static int xfrmi_changelink(struct net_device *dev, struct nlattr *tb[], |
| 667 | struct nlattr *data[], |
| 668 | struct netlink_ext_ack *extack) |
| 669 | { |
Nicolas Dichtel | c5d1030 | 2019-07-15 12:00:22 +0200 | [diff] [blame] | 670 | struct xfrm_if *xi = netdev_priv(dev); |
| 671 | struct net *net = xi->net; |
Nicolas Dichtel | e9e7e85 | 2019-07-15 12:00:20 +0200 | [diff] [blame] | 672 | struct xfrm_if_parms p; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 673 | |
Nicolas Dichtel | e9e7e85 | 2019-07-15 12:00:20 +0200 | [diff] [blame] | 674 | xfrmi_netlink_parms(data, &p); |
| 675 | xi = xfrmi_locate(net, &p); |
Nicolas Dichtel | 56c5ee1 | 2019-07-02 17:51:39 +0200 | [diff] [blame] | 676 | if (!xi) { |
Benedict Wong | 44e2b83 | 2018-07-25 13:45:29 -0700 | [diff] [blame] | 677 | xi = netdev_priv(dev); |
| 678 | } else { |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 679 | if (xi->dev != dev) |
| 680 | return -EEXIST; |
Benedict Wong | 44e2b83 | 2018-07-25 13:45:29 -0700 | [diff] [blame] | 681 | } |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 682 | |
Nicolas Dichtel | e9e7e85 | 2019-07-15 12:00:20 +0200 | [diff] [blame] | 683 | return xfrmi_update(xi, &p); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | static size_t xfrmi_get_size(const struct net_device *dev) |
| 687 | { |
| 688 | return |
| 689 | /* IFLA_XFRM_LINK */ |
| 690 | nla_total_size(4) + |
| 691 | /* IFLA_XFRM_IF_ID */ |
| 692 | nla_total_size(4) + |
| 693 | 0; |
| 694 | } |
| 695 | |
| 696 | static int xfrmi_fill_info(struct sk_buff *skb, const struct net_device *dev) |
| 697 | { |
| 698 | struct xfrm_if *xi = netdev_priv(dev); |
| 699 | struct xfrm_if_parms *parm = &xi->p; |
| 700 | |
| 701 | if (nla_put_u32(skb, IFLA_XFRM_LINK, parm->link) || |
| 702 | nla_put_u32(skb, IFLA_XFRM_IF_ID, parm->if_id)) |
| 703 | goto nla_put_failure; |
| 704 | return 0; |
| 705 | |
| 706 | nla_put_failure: |
| 707 | return -EMSGSIZE; |
| 708 | } |
| 709 | |
Wei Yongjun | 211d6f2 | 2018-07-28 06:49:48 +0000 | [diff] [blame] | 710 | static struct net *xfrmi_get_link_net(const struct net_device *dev) |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 711 | { |
| 712 | struct xfrm_if *xi = netdev_priv(dev); |
| 713 | |
Nicolas Dichtel | c5d1030 | 2019-07-15 12:00:22 +0200 | [diff] [blame] | 714 | return xi->net; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | static const struct nla_policy xfrmi_policy[IFLA_XFRM_MAX + 1] = { |
| 718 | [IFLA_XFRM_LINK] = { .type = NLA_U32 }, |
| 719 | [IFLA_XFRM_IF_ID] = { .type = NLA_U32 }, |
| 720 | }; |
| 721 | |
| 722 | static struct rtnl_link_ops xfrmi_link_ops __read_mostly = { |
| 723 | .kind = "xfrm", |
| 724 | .maxtype = IFLA_XFRM_MAX, |
| 725 | .policy = xfrmi_policy, |
| 726 | .priv_size = sizeof(struct xfrm_if), |
| 727 | .setup = xfrmi_dev_setup, |
| 728 | .validate = xfrmi_validate, |
| 729 | .newlink = xfrmi_newlink, |
| 730 | .dellink = xfrmi_dellink, |
| 731 | .changelink = xfrmi_changelink, |
| 732 | .get_size = xfrmi_get_size, |
| 733 | .fill_info = xfrmi_fill_info, |
| 734 | .get_link_net = xfrmi_get_link_net, |
| 735 | }; |
| 736 | |
Nicolas Dichtel | c95c5f5 | 2020-04-23 00:06:45 +0200 | [diff] [blame] | 737 | static void __net_exit xfrmi_exit_batch_net(struct list_head *net_exit_list) |
| 738 | { |
| 739 | struct net *net; |
| 740 | LIST_HEAD(list); |
| 741 | |
| 742 | rtnl_lock(); |
| 743 | list_for_each_entry(net, net_exit_list, exit_list) { |
| 744 | struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id); |
| 745 | struct xfrm_if __rcu **xip; |
| 746 | struct xfrm_if *xi; |
Eyal Birger | e98e445 | 2020-07-09 13:16:52 +0300 | [diff] [blame] | 747 | int i; |
Nicolas Dichtel | c95c5f5 | 2020-04-23 00:06:45 +0200 | [diff] [blame] | 748 | |
Eyal Birger | e98e445 | 2020-07-09 13:16:52 +0300 | [diff] [blame] | 749 | for (i = 0; i < XFRMI_HASH_SIZE; i++) { |
| 750 | for (xip = &xfrmn->xfrmi[i]; |
| 751 | (xi = rtnl_dereference(*xip)) != NULL; |
| 752 | xip = &xi->next) |
| 753 | unregister_netdevice_queue(xi->dev, &list); |
| 754 | } |
Nicolas Dichtel | c95c5f5 | 2020-04-23 00:06:45 +0200 | [diff] [blame] | 755 | } |
| 756 | unregister_netdevice_many(&list); |
| 757 | rtnl_unlock(); |
| 758 | } |
| 759 | |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 760 | static struct pernet_operations xfrmi_net_ops = { |
Nicolas Dichtel | c95c5f5 | 2020-04-23 00:06:45 +0200 | [diff] [blame] | 761 | .exit_batch = xfrmi_exit_batch_net, |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 762 | .id = &xfrmi_net_id, |
| 763 | .size = sizeof(struct xfrmi_net), |
| 764 | }; |
| 765 | |
| 766 | static struct xfrm6_protocol xfrmi_esp6_protocol __read_mostly = { |
| 767 | .handler = xfrm6_rcv, |
Sabrina Dubroca | 0146dca | 2020-04-27 17:59:34 +0200 | [diff] [blame] | 768 | .input_handler = xfrm_input, |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 769 | .cb_handler = xfrmi_rcv_cb, |
| 770 | .err_handler = xfrmi6_err, |
| 771 | .priority = 10, |
| 772 | }; |
| 773 | |
| 774 | static struct xfrm6_protocol xfrmi_ah6_protocol __read_mostly = { |
| 775 | .handler = xfrm6_rcv, |
Sabrina Dubroca | 0146dca | 2020-04-27 17:59:34 +0200 | [diff] [blame] | 776 | .input_handler = xfrm_input, |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 777 | .cb_handler = xfrmi_rcv_cb, |
| 778 | .err_handler = xfrmi6_err, |
| 779 | .priority = 10, |
| 780 | }; |
| 781 | |
| 782 | static struct xfrm6_protocol xfrmi_ipcomp6_protocol __read_mostly = { |
| 783 | .handler = xfrm6_rcv, |
Sabrina Dubroca | 0146dca | 2020-04-27 17:59:34 +0200 | [diff] [blame] | 784 | .input_handler = xfrm_input, |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 785 | .cb_handler = xfrmi_rcv_cb, |
| 786 | .err_handler = xfrmi6_err, |
| 787 | .priority = 10, |
| 788 | }; |
| 789 | |
Xin Long | 0a0d93b | 2020-07-17 15:02:30 +0800 | [diff] [blame] | 790 | #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL) |
Xin Long | d7b360c | 2020-07-06 20:01:37 +0800 | [diff] [blame] | 791 | static int xfrmi6_rcv_tunnel(struct sk_buff *skb) |
| 792 | { |
| 793 | const xfrm_address_t *saddr; |
| 794 | __be32 spi; |
| 795 | |
| 796 | saddr = (const xfrm_address_t *)&ipv6_hdr(skb)->saddr; |
| 797 | spi = xfrm6_tunnel_spi_lookup(dev_net(skb->dev), saddr); |
| 798 | |
| 799 | return xfrm6_rcv_spi(skb, IPPROTO_IPV6, spi, NULL); |
| 800 | } |
| 801 | |
| 802 | static struct xfrm6_tunnel xfrmi_ipv6_handler __read_mostly = { |
| 803 | .handler = xfrmi6_rcv_tunnel, |
| 804 | .cb_handler = xfrmi_rcv_cb, |
| 805 | .err_handler = xfrmi6_err, |
Xin Long | 7fe9461 | 2020-10-08 16:13:24 +0800 | [diff] [blame] | 806 | .priority = 2, |
Xin Long | d7b360c | 2020-07-06 20:01:37 +0800 | [diff] [blame] | 807 | }; |
Xin Long | 8b404f4 | 2020-07-13 15:42:38 +0800 | [diff] [blame] | 808 | |
| 809 | static struct xfrm6_tunnel xfrmi_ip6ip_handler __read_mostly = { |
| 810 | .handler = xfrmi6_rcv_tunnel, |
| 811 | .cb_handler = xfrmi_rcv_cb, |
| 812 | .err_handler = xfrmi6_err, |
Xin Long | 7fe9461 | 2020-10-08 16:13:24 +0800 | [diff] [blame] | 813 | .priority = 2, |
Xin Long | 8b404f4 | 2020-07-13 15:42:38 +0800 | [diff] [blame] | 814 | }; |
Xin Long | d7b360c | 2020-07-06 20:01:37 +0800 | [diff] [blame] | 815 | #endif |
| 816 | |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 817 | static struct xfrm4_protocol xfrmi_esp4_protocol __read_mostly = { |
| 818 | .handler = xfrm4_rcv, |
| 819 | .input_handler = xfrm_input, |
| 820 | .cb_handler = xfrmi_rcv_cb, |
| 821 | .err_handler = xfrmi4_err, |
| 822 | .priority = 10, |
| 823 | }; |
| 824 | |
| 825 | static struct xfrm4_protocol xfrmi_ah4_protocol __read_mostly = { |
| 826 | .handler = xfrm4_rcv, |
| 827 | .input_handler = xfrm_input, |
| 828 | .cb_handler = xfrmi_rcv_cb, |
| 829 | .err_handler = xfrmi4_err, |
| 830 | .priority = 10, |
| 831 | }; |
| 832 | |
| 833 | static struct xfrm4_protocol xfrmi_ipcomp4_protocol __read_mostly = { |
| 834 | .handler = xfrm4_rcv, |
| 835 | .input_handler = xfrm_input, |
| 836 | .cb_handler = xfrmi_rcv_cb, |
| 837 | .err_handler = xfrmi4_err, |
| 838 | .priority = 10, |
| 839 | }; |
| 840 | |
Xin Long | 0a0d93b | 2020-07-17 15:02:30 +0800 | [diff] [blame] | 841 | #if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL) |
Xin Long | da9bbf0 | 2020-07-06 20:01:38 +0800 | [diff] [blame] | 842 | static int xfrmi4_rcv_tunnel(struct sk_buff *skb) |
| 843 | { |
| 844 | return xfrm4_rcv_spi(skb, IPPROTO_IPIP, ip_hdr(skb)->saddr); |
| 845 | } |
| 846 | |
| 847 | static struct xfrm_tunnel xfrmi_ipip_handler __read_mostly = { |
| 848 | .handler = xfrmi4_rcv_tunnel, |
| 849 | .cb_handler = xfrmi_rcv_cb, |
| 850 | .err_handler = xfrmi4_err, |
Xin Long | 7fe9461 | 2020-10-08 16:13:24 +0800 | [diff] [blame] | 851 | .priority = 3, |
Xin Long | da9bbf0 | 2020-07-06 20:01:38 +0800 | [diff] [blame] | 852 | }; |
Xin Long | 8b404f4 | 2020-07-13 15:42:38 +0800 | [diff] [blame] | 853 | |
| 854 | static struct xfrm_tunnel xfrmi_ipip6_handler __read_mostly = { |
| 855 | .handler = xfrmi4_rcv_tunnel, |
| 856 | .cb_handler = xfrmi_rcv_cb, |
| 857 | .err_handler = xfrmi4_err, |
Xin Long | 7fe9461 | 2020-10-08 16:13:24 +0800 | [diff] [blame] | 858 | .priority = 2, |
Xin Long | 8b404f4 | 2020-07-13 15:42:38 +0800 | [diff] [blame] | 859 | }; |
Xin Long | da9bbf0 | 2020-07-06 20:01:38 +0800 | [diff] [blame] | 860 | #endif |
| 861 | |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 862 | static int __init xfrmi4_init(void) |
| 863 | { |
| 864 | int err; |
| 865 | |
| 866 | err = xfrm4_protocol_register(&xfrmi_esp4_protocol, IPPROTO_ESP); |
| 867 | if (err < 0) |
| 868 | goto xfrm_proto_esp_failed; |
| 869 | err = xfrm4_protocol_register(&xfrmi_ah4_protocol, IPPROTO_AH); |
| 870 | if (err < 0) |
| 871 | goto xfrm_proto_ah_failed; |
| 872 | err = xfrm4_protocol_register(&xfrmi_ipcomp4_protocol, IPPROTO_COMP); |
| 873 | if (err < 0) |
| 874 | goto xfrm_proto_comp_failed; |
Xin Long | 0a0d93b | 2020-07-17 15:02:30 +0800 | [diff] [blame] | 875 | #if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL) |
Xin Long | da9bbf0 | 2020-07-06 20:01:38 +0800 | [diff] [blame] | 876 | err = xfrm4_tunnel_register(&xfrmi_ipip_handler, AF_INET); |
| 877 | if (err < 0) |
| 878 | goto xfrm_tunnel_ipip_failed; |
Xin Long | 8b404f4 | 2020-07-13 15:42:38 +0800 | [diff] [blame] | 879 | err = xfrm4_tunnel_register(&xfrmi_ipip6_handler, AF_INET6); |
Xin Long | da9bbf0 | 2020-07-06 20:01:38 +0800 | [diff] [blame] | 880 | if (err < 0) |
| 881 | goto xfrm_tunnel_ipip6_failed; |
| 882 | #endif |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 883 | |
| 884 | return 0; |
| 885 | |
Xin Long | 0a0d93b | 2020-07-17 15:02:30 +0800 | [diff] [blame] | 886 | #if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL) |
Xin Long | da9bbf0 | 2020-07-06 20:01:38 +0800 | [diff] [blame] | 887 | xfrm_tunnel_ipip6_failed: |
| 888 | xfrm4_tunnel_deregister(&xfrmi_ipip_handler, AF_INET); |
| 889 | xfrm_tunnel_ipip_failed: |
| 890 | xfrm4_protocol_deregister(&xfrmi_ipcomp4_protocol, IPPROTO_COMP); |
| 891 | #endif |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 892 | xfrm_proto_comp_failed: |
| 893 | xfrm4_protocol_deregister(&xfrmi_ah4_protocol, IPPROTO_AH); |
| 894 | xfrm_proto_ah_failed: |
| 895 | xfrm4_protocol_deregister(&xfrmi_esp4_protocol, IPPROTO_ESP); |
| 896 | xfrm_proto_esp_failed: |
| 897 | return err; |
| 898 | } |
| 899 | |
| 900 | static void xfrmi4_fini(void) |
| 901 | { |
Xin Long | 0a0d93b | 2020-07-17 15:02:30 +0800 | [diff] [blame] | 902 | #if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL) |
Xin Long | 8b404f4 | 2020-07-13 15:42:38 +0800 | [diff] [blame] | 903 | xfrm4_tunnel_deregister(&xfrmi_ipip6_handler, AF_INET6); |
Xin Long | da9bbf0 | 2020-07-06 20:01:38 +0800 | [diff] [blame] | 904 | xfrm4_tunnel_deregister(&xfrmi_ipip_handler, AF_INET); |
| 905 | #endif |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 906 | xfrm4_protocol_deregister(&xfrmi_ipcomp4_protocol, IPPROTO_COMP); |
| 907 | xfrm4_protocol_deregister(&xfrmi_ah4_protocol, IPPROTO_AH); |
| 908 | xfrm4_protocol_deregister(&xfrmi_esp4_protocol, IPPROTO_ESP); |
| 909 | } |
| 910 | |
| 911 | static int __init xfrmi6_init(void) |
| 912 | { |
| 913 | int err; |
| 914 | |
| 915 | err = xfrm6_protocol_register(&xfrmi_esp6_protocol, IPPROTO_ESP); |
| 916 | if (err < 0) |
| 917 | goto xfrm_proto_esp_failed; |
| 918 | err = xfrm6_protocol_register(&xfrmi_ah6_protocol, IPPROTO_AH); |
| 919 | if (err < 0) |
| 920 | goto xfrm_proto_ah_failed; |
| 921 | err = xfrm6_protocol_register(&xfrmi_ipcomp6_protocol, IPPROTO_COMP); |
| 922 | if (err < 0) |
| 923 | goto xfrm_proto_comp_failed; |
Xin Long | 0a0d93b | 2020-07-17 15:02:30 +0800 | [diff] [blame] | 924 | #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL) |
Xin Long | d7b360c | 2020-07-06 20:01:37 +0800 | [diff] [blame] | 925 | err = xfrm6_tunnel_register(&xfrmi_ipv6_handler, AF_INET6); |
| 926 | if (err < 0) |
| 927 | goto xfrm_tunnel_ipv6_failed; |
Xin Long | 8b404f4 | 2020-07-13 15:42:38 +0800 | [diff] [blame] | 928 | err = xfrm6_tunnel_register(&xfrmi_ip6ip_handler, AF_INET); |
Xin Long | d7b360c | 2020-07-06 20:01:37 +0800 | [diff] [blame] | 929 | if (err < 0) |
| 930 | goto xfrm_tunnel_ip6ip_failed; |
| 931 | #endif |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 932 | |
| 933 | return 0; |
| 934 | |
Xin Long | 0a0d93b | 2020-07-17 15:02:30 +0800 | [diff] [blame] | 935 | #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL) |
Xin Long | d7b360c | 2020-07-06 20:01:37 +0800 | [diff] [blame] | 936 | xfrm_tunnel_ip6ip_failed: |
| 937 | xfrm6_tunnel_deregister(&xfrmi_ipv6_handler, AF_INET6); |
| 938 | xfrm_tunnel_ipv6_failed: |
| 939 | xfrm6_protocol_deregister(&xfrmi_ipcomp6_protocol, IPPROTO_COMP); |
| 940 | #endif |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 941 | xfrm_proto_comp_failed: |
| 942 | xfrm6_protocol_deregister(&xfrmi_ah6_protocol, IPPROTO_AH); |
| 943 | xfrm_proto_ah_failed: |
| 944 | xfrm6_protocol_deregister(&xfrmi_esp6_protocol, IPPROTO_ESP); |
| 945 | xfrm_proto_esp_failed: |
| 946 | return err; |
| 947 | } |
| 948 | |
| 949 | static void xfrmi6_fini(void) |
| 950 | { |
Xin Long | 0a0d93b | 2020-07-17 15:02:30 +0800 | [diff] [blame] | 951 | #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL) |
Xin Long | 8b404f4 | 2020-07-13 15:42:38 +0800 | [diff] [blame] | 952 | xfrm6_tunnel_deregister(&xfrmi_ip6ip_handler, AF_INET); |
Xin Long | d7b360c | 2020-07-06 20:01:37 +0800 | [diff] [blame] | 953 | xfrm6_tunnel_deregister(&xfrmi_ipv6_handler, AF_INET6); |
| 954 | #endif |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 955 | xfrm6_protocol_deregister(&xfrmi_ipcomp6_protocol, IPPROTO_COMP); |
| 956 | xfrm6_protocol_deregister(&xfrmi_ah6_protocol, IPPROTO_AH); |
| 957 | xfrm6_protocol_deregister(&xfrmi_esp6_protocol, IPPROTO_ESP); |
| 958 | } |
| 959 | |
| 960 | static const struct xfrm_if_cb xfrm_if_cb = { |
| 961 | .decode_session = xfrmi_decode_session, |
| 962 | }; |
| 963 | |
| 964 | static int __init xfrmi_init(void) |
| 965 | { |
| 966 | const char *msg; |
| 967 | int err; |
| 968 | |
| 969 | pr_info("IPsec XFRM device driver\n"); |
| 970 | |
| 971 | msg = "tunnel device"; |
| 972 | err = register_pernet_device(&xfrmi_net_ops); |
| 973 | if (err < 0) |
| 974 | goto pernet_dev_failed; |
| 975 | |
| 976 | msg = "xfrm4 protocols"; |
| 977 | err = xfrmi4_init(); |
| 978 | if (err < 0) |
| 979 | goto xfrmi4_failed; |
| 980 | |
| 981 | msg = "xfrm6 protocols"; |
| 982 | err = xfrmi6_init(); |
| 983 | if (err < 0) |
| 984 | goto xfrmi6_failed; |
| 985 | |
| 986 | |
| 987 | msg = "netlink interface"; |
| 988 | err = rtnl_link_register(&xfrmi_link_ops); |
| 989 | if (err < 0) |
| 990 | goto rtnl_link_failed; |
| 991 | |
| 992 | xfrm_if_register_cb(&xfrm_if_cb); |
| 993 | |
| 994 | return err; |
| 995 | |
| 996 | rtnl_link_failed: |
| 997 | xfrmi6_fini(); |
| 998 | xfrmi6_failed: |
| 999 | xfrmi4_fini(); |
| 1000 | xfrmi4_failed: |
| 1001 | unregister_pernet_device(&xfrmi_net_ops); |
| 1002 | pernet_dev_failed: |
| 1003 | pr_err("xfrmi init: failed to register %s\n", msg); |
| 1004 | return err; |
| 1005 | } |
| 1006 | |
| 1007 | static void __exit xfrmi_fini(void) |
| 1008 | { |
| 1009 | xfrm_if_unregister_cb(); |
| 1010 | rtnl_link_unregister(&xfrmi_link_ops); |
| 1011 | xfrmi4_fini(); |
| 1012 | xfrmi6_fini(); |
| 1013 | unregister_pernet_device(&xfrmi_net_ops); |
| 1014 | } |
| 1015 | |
| 1016 | module_init(xfrmi_init); |
| 1017 | module_exit(xfrmi_fini); |
| 1018 | MODULE_LICENSE("GPL"); |
| 1019 | MODULE_ALIAS_RTNL_LINK("xfrm"); |
| 1020 | MODULE_ALIAS_NETDEV("xfrm0"); |
| 1021 | MODULE_AUTHOR("Steffen Klassert"); |
| 1022 | MODULE_DESCRIPTION("XFRM virtual interface"); |