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 | 8f9a1fa4 | 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); |
Eyal Birger | 18f9769 | 2021-01-06 08:10:46 +0200 | [diff] [blame] | 299 | if ((!skb_is_gso(skb) && skb->len > mtu) || |
| 300 | (skb_is_gso(skb) && !skb_gso_validate_network_len(skb, mtu))) { |
Xu Wang | 8aaea2b | 2020-01-13 09:00:36 +0000 | [diff] [blame] | 301 | skb_dst_update_pmtu_no_confirm(skb, mtu); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 302 | |
| 303 | if (skb->protocol == htons(ETH_P_IPV6)) { |
| 304 | if (mtu < IPV6_MIN_MTU) |
| 305 | mtu = IPV6_MIN_MTU; |
| 306 | |
Jason A. Donenfeld | 45942ba | 2020-02-11 20:47:09 +0100 | [diff] [blame] | 307 | icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 308 | } else { |
Eyal Birger | 8fc0e3b | 2021-02-20 15:01:15 +0200 | [diff] [blame] | 309 | if (!(ip_hdr(skb)->frag_off & htons(IP_DF))) |
| 310 | goto xmit; |
Jason A. Donenfeld | 45942ba | 2020-02-11 20:47:09 +0100 | [diff] [blame] | 311 | icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, |
| 312 | htonl(mtu)); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | dst_release(dst); |
| 316 | return -EMSGSIZE; |
| 317 | } |
| 318 | |
Eyal Birger | 8fc0e3b | 2021-02-20 15:01:15 +0200 | [diff] [blame] | 319 | xmit: |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 320 | xfrmi_scrub_packet(skb, !net_eq(xi->net, dev_net(dev))); |
| 321 | skb_dst_set(skb, dst); |
| 322 | skb->dev = tdev; |
| 323 | |
| 324 | err = dst_output(xi->net, skb->sk, skb); |
| 325 | if (net_xmit_eval(err) == 0) { |
Lev Stipakov | 0064c5c | 2020-11-13 23:59:40 +0200 | [diff] [blame] | 326 | dev_sw_netstats_tx_add(dev, 1, length); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 327 | } else { |
| 328 | stats->tx_errors++; |
| 329 | stats->tx_aborted_errors++; |
| 330 | } |
| 331 | |
| 332 | return 0; |
| 333 | tx_err_link_failure: |
| 334 | stats->tx_carrier_errors++; |
| 335 | dst_link_failure(skb); |
| 336 | tx_err_dst_release: |
| 337 | dst_release(dst); |
| 338 | return err; |
| 339 | } |
| 340 | |
| 341 | static netdev_tx_t xfrmi_xmit(struct sk_buff *skb, struct net_device *dev) |
| 342 | { |
| 343 | struct xfrm_if *xi = netdev_priv(dev); |
| 344 | struct net_device_stats *stats = &xi->dev->stats; |
Nicolas Dichtel | f042365 | 2020-01-13 09:32:47 +0100 | [diff] [blame] | 345 | struct dst_entry *dst = skb_dst(skb); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 346 | struct flowi fl; |
| 347 | int ret; |
| 348 | |
| 349 | memset(&fl, 0, sizeof(fl)); |
| 350 | |
| 351 | switch (skb->protocol) { |
| 352 | case htons(ETH_P_IPV6): |
| 353 | xfrm_decode_session(skb, &fl, AF_INET6); |
| 354 | memset(IP6CB(skb), 0, sizeof(*IP6CB(skb))); |
Nicolas Dichtel | f042365 | 2020-01-13 09:32:47 +0100 | [diff] [blame] | 355 | if (!dst) { |
| 356 | fl.u.ip6.flowi6_oif = dev->ifindex; |
| 357 | fl.u.ip6.flowi6_flags |= FLOWI_FLAG_ANYSRC; |
| 358 | dst = ip6_route_output(dev_net(dev), NULL, &fl.u.ip6); |
| 359 | if (dst->error) { |
| 360 | dst_release(dst); |
| 361 | stats->tx_carrier_errors++; |
| 362 | goto tx_err; |
| 363 | } |
| 364 | skb_dst_set(skb, dst); |
| 365 | } |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 366 | break; |
| 367 | case htons(ETH_P_IP): |
| 368 | xfrm_decode_session(skb, &fl, AF_INET); |
| 369 | memset(IPCB(skb), 0, sizeof(*IPCB(skb))); |
Nicolas Dichtel | f042365 | 2020-01-13 09:32:47 +0100 | [diff] [blame] | 370 | if (!dst) { |
| 371 | struct rtable *rt; |
| 372 | |
| 373 | fl.u.ip4.flowi4_oif = dev->ifindex; |
| 374 | fl.u.ip4.flowi4_flags |= FLOWI_FLAG_ANYSRC; |
| 375 | rt = __ip_route_output_key(dev_net(dev), &fl.u.ip4); |
| 376 | if (IS_ERR(rt)) { |
| 377 | stats->tx_carrier_errors++; |
| 378 | goto tx_err; |
| 379 | } |
| 380 | skb_dst_set(skb, &rt->dst); |
| 381 | } |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 382 | break; |
| 383 | default: |
| 384 | goto tx_err; |
| 385 | } |
| 386 | |
Nicolas Dichtel | 22d6552 | 2019-07-15 12:00:23 +0200 | [diff] [blame] | 387 | fl.flowi_oif = xi->p.link; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 388 | |
| 389 | ret = xfrmi_xmit2(skb, dev, &fl); |
| 390 | if (ret < 0) |
| 391 | goto tx_err; |
| 392 | |
| 393 | return NETDEV_TX_OK; |
| 394 | |
| 395 | tx_err: |
| 396 | stats->tx_errors++; |
| 397 | stats->tx_dropped++; |
| 398 | kfree_skb(skb); |
| 399 | return NETDEV_TX_OK; |
| 400 | } |
| 401 | |
| 402 | static int xfrmi4_err(struct sk_buff *skb, u32 info) |
| 403 | { |
| 404 | const struct iphdr *iph = (const struct iphdr *)skb->data; |
| 405 | struct net *net = dev_net(skb->dev); |
| 406 | int protocol = iph->protocol; |
| 407 | struct ip_comp_hdr *ipch; |
| 408 | struct ip_esp_hdr *esph; |
| 409 | struct ip_auth_hdr *ah ; |
| 410 | struct xfrm_state *x; |
| 411 | struct xfrm_if *xi; |
| 412 | __be32 spi; |
| 413 | |
| 414 | switch (protocol) { |
| 415 | case IPPROTO_ESP: |
| 416 | esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2)); |
| 417 | spi = esph->spi; |
| 418 | break; |
| 419 | case IPPROTO_AH: |
| 420 | ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2)); |
| 421 | spi = ah->spi; |
| 422 | break; |
| 423 | case IPPROTO_COMP: |
| 424 | ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2)); |
| 425 | spi = htonl(ntohs(ipch->cpi)); |
| 426 | break; |
| 427 | default: |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | switch (icmp_hdr(skb)->type) { |
| 432 | case ICMP_DEST_UNREACH: |
| 433 | if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED) |
| 434 | return 0; |
Gustavo A. R. Silva | 135436a | 2020-11-20 12:41:00 -0600 | [diff] [blame] | 435 | break; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 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 | |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 541 | static int xfrmi_get_iflink(const struct net_device *dev) |
| 542 | { |
| 543 | struct xfrm_if *xi = netdev_priv(dev); |
| 544 | |
Nicolas Dichtel | 22d6552 | 2019-07-15 12:00:23 +0200 | [diff] [blame] | 545 | return xi->p.link; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 546 | } |
| 547 | |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 548 | static const struct net_device_ops xfrmi_netdev_ops = { |
| 549 | .ndo_init = xfrmi_dev_init, |
| 550 | .ndo_uninit = xfrmi_dev_uninit, |
| 551 | .ndo_start_xmit = xfrmi_xmit, |
Lev Stipakov | 0064c5c | 2020-11-13 23:59:40 +0200 | [diff] [blame] | 552 | .ndo_get_stats64 = dev_get_tstats64, |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 553 | .ndo_get_iflink = xfrmi_get_iflink, |
| 554 | }; |
| 555 | |
| 556 | static void xfrmi_dev_setup(struct net_device *dev) |
| 557 | { |
| 558 | dev->netdev_ops = &xfrmi_netdev_ops; |
Jason A. Donenfeld | 8f9a1fa4 | 2020-06-29 19:06:25 -0600 | [diff] [blame] | 559 | dev->header_ops = &ip_tunnel_header_ops; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 560 | dev->type = ARPHRD_NONE; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 561 | dev->mtu = ETH_DATA_LEN; |
| 562 | dev->min_mtu = ETH_MIN_MTU; |
Nicolas Dichtel | f042365 | 2020-01-13 09:32:47 +0100 | [diff] [blame] | 563 | dev->max_mtu = IP_MAX_MTU; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 564 | dev->flags = IFF_NOARP; |
| 565 | dev->needs_free_netdev = true; |
| 566 | dev->priv_destructor = xfrmi_dev_free; |
| 567 | netif_keep_dst(dev); |
Nicolas Dichtel | 22d6552 | 2019-07-15 12:00:23 +0200 | [diff] [blame] | 568 | |
| 569 | eth_broadcast_addr(dev->broadcast); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 570 | } |
| 571 | |
Eyal Birger | 18f9769 | 2021-01-06 08:10:46 +0200 | [diff] [blame] | 572 | #define XFRMI_FEATURES (NETIF_F_SG | \ |
| 573 | NETIF_F_FRAGLIST | \ |
| 574 | NETIF_F_GSO_SOFTWARE | \ |
| 575 | NETIF_F_HW_CSUM) |
| 576 | |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 577 | static int xfrmi_dev_init(struct net_device *dev) |
| 578 | { |
| 579 | struct xfrm_if *xi = netdev_priv(dev); |
Nicolas Dichtel | 22d6552 | 2019-07-15 12:00:23 +0200 | [diff] [blame] | 580 | 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] | 581 | int err; |
| 582 | |
| 583 | dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
| 584 | if (!dev->tstats) |
| 585 | return -ENOMEM; |
| 586 | |
| 587 | err = gro_cells_init(&xi->gro_cells, dev); |
| 588 | if (err) { |
| 589 | free_percpu(dev->tstats); |
| 590 | return err; |
| 591 | } |
| 592 | |
| 593 | dev->features |= NETIF_F_LLTX; |
Eyal Birger | 18f9769 | 2021-01-06 08:10:46 +0200 | [diff] [blame] | 594 | dev->features |= XFRMI_FEATURES; |
| 595 | dev->hw_features |= XFRMI_FEATURES; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 596 | |
Nicolas Dichtel | 22d6552 | 2019-07-15 12:00:23 +0200 | [diff] [blame] | 597 | if (phydev) { |
| 598 | dev->needed_headroom = phydev->needed_headroom; |
| 599 | dev->needed_tailroom = phydev->needed_tailroom; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 600 | |
Nicolas Dichtel | 22d6552 | 2019-07-15 12:00:23 +0200 | [diff] [blame] | 601 | if (is_zero_ether_addr(dev->dev_addr)) |
| 602 | eth_hw_addr_inherit(dev, phydev); |
| 603 | if (is_zero_ether_addr(dev->broadcast)) |
| 604 | memcpy(dev->broadcast, phydev->broadcast, |
| 605 | dev->addr_len); |
| 606 | } else { |
| 607 | eth_hw_addr_random(dev); |
| 608 | eth_broadcast_addr(dev->broadcast); |
| 609 | } |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 610 | |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | static int xfrmi_validate(struct nlattr *tb[], struct nlattr *data[], |
| 615 | struct netlink_ext_ack *extack) |
| 616 | { |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | static void xfrmi_netlink_parms(struct nlattr *data[], |
| 621 | struct xfrm_if_parms *parms) |
| 622 | { |
| 623 | memset(parms, 0, sizeof(*parms)); |
| 624 | |
| 625 | if (!data) |
| 626 | return; |
| 627 | |
| 628 | if (data[IFLA_XFRM_LINK]) |
| 629 | parms->link = nla_get_u32(data[IFLA_XFRM_LINK]); |
| 630 | |
| 631 | if (data[IFLA_XFRM_IF_ID]) |
| 632 | parms->if_id = nla_get_u32(data[IFLA_XFRM_IF_ID]); |
| 633 | } |
| 634 | |
| 635 | static int xfrmi_newlink(struct net *src_net, struct net_device *dev, |
| 636 | struct nlattr *tb[], struct nlattr *data[], |
| 637 | struct netlink_ext_ack *extack) |
| 638 | { |
| 639 | struct net *net = dev_net(dev); |
Antony Antony | 8dce439 | 2021-12-12 11:34:30 +0100 | [diff] [blame] | 640 | struct xfrm_if_parms p = {}; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 641 | struct xfrm_if *xi; |
Nicolas Dichtel | 56c5ee1 | 2019-07-02 17:51:39 +0200 | [diff] [blame] | 642 | int err; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 643 | |
Nicolas Dichtel | 56c5ee1 | 2019-07-02 17:51:39 +0200 | [diff] [blame] | 644 | xfrmi_netlink_parms(data, &p); |
Antony Antony | 8dce439 | 2021-12-12 11:34:30 +0100 | [diff] [blame] | 645 | if (!p.if_id) { |
| 646 | NL_SET_ERR_MSG(extack, "if_id must be non zero"); |
| 647 | return -EINVAL; |
| 648 | } |
| 649 | |
Nicolas Dichtel | 56c5ee1 | 2019-07-02 17:51:39 +0200 | [diff] [blame] | 650 | xi = xfrmi_locate(net, &p); |
| 651 | if (xi) |
| 652 | return -EEXIST; |
| 653 | |
| 654 | xi = netdev_priv(dev); |
| 655 | xi->p = p; |
| 656 | xi->net = net; |
| 657 | xi->dev = dev; |
Nicolas Dichtel | 56c5ee1 | 2019-07-02 17:51:39 +0200 | [diff] [blame] | 658 | |
| 659 | err = xfrmi_create(dev); |
Nicolas Dichtel | 56c5ee1 | 2019-07-02 17:51:39 +0200 | [diff] [blame] | 660 | return err; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | static void xfrmi_dellink(struct net_device *dev, struct list_head *head) |
| 664 | { |
| 665 | unregister_netdevice_queue(dev, head); |
| 666 | } |
| 667 | |
| 668 | static int xfrmi_changelink(struct net_device *dev, struct nlattr *tb[], |
| 669 | struct nlattr *data[], |
| 670 | struct netlink_ext_ack *extack) |
| 671 | { |
Nicolas Dichtel | c5d1030 | 2019-07-15 12:00:22 +0200 | [diff] [blame] | 672 | struct xfrm_if *xi = netdev_priv(dev); |
| 673 | struct net *net = xi->net; |
Antony Antony | 8dce439 | 2021-12-12 11:34:30 +0100 | [diff] [blame] | 674 | struct xfrm_if_parms p = {}; |
| 675 | |
| 676 | if (!p.if_id) { |
| 677 | NL_SET_ERR_MSG(extack, "if_id must be non zero"); |
| 678 | return -EINVAL; |
| 679 | } |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 680 | |
Nicolas Dichtel | e9e7e85 | 2019-07-15 12:00:20 +0200 | [diff] [blame] | 681 | xfrmi_netlink_parms(data, &p); |
| 682 | xi = xfrmi_locate(net, &p); |
Nicolas Dichtel | 56c5ee1 | 2019-07-02 17:51:39 +0200 | [diff] [blame] | 683 | if (!xi) { |
Benedict Wong | 44e2b83 | 2018-07-25 13:45:29 -0700 | [diff] [blame] | 684 | xi = netdev_priv(dev); |
| 685 | } else { |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 686 | if (xi->dev != dev) |
| 687 | return -EEXIST; |
Benedict Wong | 44e2b83 | 2018-07-25 13:45:29 -0700 | [diff] [blame] | 688 | } |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 689 | |
Nicolas Dichtel | e9e7e85 | 2019-07-15 12:00:20 +0200 | [diff] [blame] | 690 | return xfrmi_update(xi, &p); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | static size_t xfrmi_get_size(const struct net_device *dev) |
| 694 | { |
| 695 | return |
| 696 | /* IFLA_XFRM_LINK */ |
| 697 | nla_total_size(4) + |
| 698 | /* IFLA_XFRM_IF_ID */ |
| 699 | nla_total_size(4) + |
| 700 | 0; |
| 701 | } |
| 702 | |
| 703 | static int xfrmi_fill_info(struct sk_buff *skb, const struct net_device *dev) |
| 704 | { |
| 705 | struct xfrm_if *xi = netdev_priv(dev); |
| 706 | struct xfrm_if_parms *parm = &xi->p; |
| 707 | |
| 708 | if (nla_put_u32(skb, IFLA_XFRM_LINK, parm->link) || |
| 709 | nla_put_u32(skb, IFLA_XFRM_IF_ID, parm->if_id)) |
| 710 | goto nla_put_failure; |
| 711 | return 0; |
| 712 | |
| 713 | nla_put_failure: |
| 714 | return -EMSGSIZE; |
| 715 | } |
| 716 | |
Wei Yongjun | 211d6f2 | 2018-07-28 06:49:48 +0000 | [diff] [blame] | 717 | static struct net *xfrmi_get_link_net(const struct net_device *dev) |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 718 | { |
| 719 | struct xfrm_if *xi = netdev_priv(dev); |
| 720 | |
Nicolas Dichtel | c5d1030 | 2019-07-15 12:00:22 +0200 | [diff] [blame] | 721 | return xi->net; |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | static const struct nla_policy xfrmi_policy[IFLA_XFRM_MAX + 1] = { |
| 725 | [IFLA_XFRM_LINK] = { .type = NLA_U32 }, |
| 726 | [IFLA_XFRM_IF_ID] = { .type = NLA_U32 }, |
| 727 | }; |
| 728 | |
| 729 | static struct rtnl_link_ops xfrmi_link_ops __read_mostly = { |
| 730 | .kind = "xfrm", |
| 731 | .maxtype = IFLA_XFRM_MAX, |
| 732 | .policy = xfrmi_policy, |
| 733 | .priv_size = sizeof(struct xfrm_if), |
| 734 | .setup = xfrmi_dev_setup, |
| 735 | .validate = xfrmi_validate, |
| 736 | .newlink = xfrmi_newlink, |
| 737 | .dellink = xfrmi_dellink, |
| 738 | .changelink = xfrmi_changelink, |
| 739 | .get_size = xfrmi_get_size, |
| 740 | .fill_info = xfrmi_fill_info, |
| 741 | .get_link_net = xfrmi_get_link_net, |
| 742 | }; |
| 743 | |
Nicolas Dichtel | c95c5f5 | 2020-04-23 00:06:45 +0200 | [diff] [blame] | 744 | static void __net_exit xfrmi_exit_batch_net(struct list_head *net_exit_list) |
| 745 | { |
| 746 | struct net *net; |
| 747 | LIST_HEAD(list); |
| 748 | |
| 749 | rtnl_lock(); |
| 750 | list_for_each_entry(net, net_exit_list, exit_list) { |
| 751 | struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id); |
| 752 | struct xfrm_if __rcu **xip; |
| 753 | struct xfrm_if *xi; |
Eyal Birger | e98e445 | 2020-07-09 13:16:52 +0300 | [diff] [blame] | 754 | int i; |
Nicolas Dichtel | c95c5f5 | 2020-04-23 00:06:45 +0200 | [diff] [blame] | 755 | |
Eyal Birger | e98e445 | 2020-07-09 13:16:52 +0300 | [diff] [blame] | 756 | for (i = 0; i < XFRMI_HASH_SIZE; i++) { |
| 757 | for (xip = &xfrmn->xfrmi[i]; |
| 758 | (xi = rtnl_dereference(*xip)) != NULL; |
| 759 | xip = &xi->next) |
| 760 | unregister_netdevice_queue(xi->dev, &list); |
| 761 | } |
Nicolas Dichtel | c95c5f5 | 2020-04-23 00:06:45 +0200 | [diff] [blame] | 762 | } |
| 763 | unregister_netdevice_many(&list); |
| 764 | rtnl_unlock(); |
| 765 | } |
| 766 | |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 767 | static struct pernet_operations xfrmi_net_ops = { |
Nicolas Dichtel | c95c5f5 | 2020-04-23 00:06:45 +0200 | [diff] [blame] | 768 | .exit_batch = xfrmi_exit_batch_net, |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 769 | .id = &xfrmi_net_id, |
| 770 | .size = sizeof(struct xfrmi_net), |
| 771 | }; |
| 772 | |
| 773 | static struct xfrm6_protocol xfrmi_esp6_protocol __read_mostly = { |
| 774 | .handler = xfrm6_rcv, |
Sabrina Dubroca | 0146dca | 2020-04-27 17:59:34 +0200 | [diff] [blame] | 775 | .input_handler = xfrm_input, |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 776 | .cb_handler = xfrmi_rcv_cb, |
| 777 | .err_handler = xfrmi6_err, |
| 778 | .priority = 10, |
| 779 | }; |
| 780 | |
| 781 | static struct xfrm6_protocol xfrmi_ah6_protocol __read_mostly = { |
| 782 | .handler = xfrm6_rcv, |
Sabrina Dubroca | 0146dca | 2020-04-27 17:59:34 +0200 | [diff] [blame] | 783 | .input_handler = xfrm_input, |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 784 | .cb_handler = xfrmi_rcv_cb, |
| 785 | .err_handler = xfrmi6_err, |
| 786 | .priority = 10, |
| 787 | }; |
| 788 | |
| 789 | static struct xfrm6_protocol xfrmi_ipcomp6_protocol __read_mostly = { |
| 790 | .handler = xfrm6_rcv, |
Sabrina Dubroca | 0146dca | 2020-04-27 17:59:34 +0200 | [diff] [blame] | 791 | .input_handler = xfrm_input, |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 792 | .cb_handler = xfrmi_rcv_cb, |
| 793 | .err_handler = xfrmi6_err, |
| 794 | .priority = 10, |
| 795 | }; |
| 796 | |
Xin Long | 0a0d93b | 2020-07-17 15:02:30 +0800 | [diff] [blame] | 797 | #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL) |
Xin Long | d7b360c | 2020-07-06 20:01:37 +0800 | [diff] [blame] | 798 | static int xfrmi6_rcv_tunnel(struct sk_buff *skb) |
| 799 | { |
| 800 | const xfrm_address_t *saddr; |
| 801 | __be32 spi; |
| 802 | |
| 803 | saddr = (const xfrm_address_t *)&ipv6_hdr(skb)->saddr; |
| 804 | spi = xfrm6_tunnel_spi_lookup(dev_net(skb->dev), saddr); |
| 805 | |
| 806 | return xfrm6_rcv_spi(skb, IPPROTO_IPV6, spi, NULL); |
| 807 | } |
| 808 | |
| 809 | static struct xfrm6_tunnel xfrmi_ipv6_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 | d7b360c | 2020-07-06 20:01:37 +0800 | [diff] [blame] | 814 | }; |
Xin Long | 8b404f4 | 2020-07-13 15:42:38 +0800 | [diff] [blame] | 815 | |
| 816 | static struct xfrm6_tunnel xfrmi_ip6ip_handler __read_mostly = { |
| 817 | .handler = xfrmi6_rcv_tunnel, |
| 818 | .cb_handler = xfrmi_rcv_cb, |
| 819 | .err_handler = xfrmi6_err, |
Xin Long | 7fe9461 | 2020-10-08 16:13:24 +0800 | [diff] [blame] | 820 | .priority = 2, |
Xin Long | 8b404f4 | 2020-07-13 15:42:38 +0800 | [diff] [blame] | 821 | }; |
Xin Long | d7b360c | 2020-07-06 20:01:37 +0800 | [diff] [blame] | 822 | #endif |
| 823 | |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 824 | static struct xfrm4_protocol xfrmi_esp4_protocol __read_mostly = { |
| 825 | .handler = xfrm4_rcv, |
| 826 | .input_handler = xfrm_input, |
| 827 | .cb_handler = xfrmi_rcv_cb, |
| 828 | .err_handler = xfrmi4_err, |
| 829 | .priority = 10, |
| 830 | }; |
| 831 | |
| 832 | static struct xfrm4_protocol xfrmi_ah4_protocol __read_mostly = { |
| 833 | .handler = xfrm4_rcv, |
| 834 | .input_handler = xfrm_input, |
| 835 | .cb_handler = xfrmi_rcv_cb, |
| 836 | .err_handler = xfrmi4_err, |
| 837 | .priority = 10, |
| 838 | }; |
| 839 | |
| 840 | static struct xfrm4_protocol xfrmi_ipcomp4_protocol __read_mostly = { |
| 841 | .handler = xfrm4_rcv, |
| 842 | .input_handler = xfrm_input, |
| 843 | .cb_handler = xfrmi_rcv_cb, |
| 844 | .err_handler = xfrmi4_err, |
| 845 | .priority = 10, |
| 846 | }; |
| 847 | |
Xin Long | 0a0d93b | 2020-07-17 15:02:30 +0800 | [diff] [blame] | 848 | #if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL) |
Xin Long | da9bbf0 | 2020-07-06 20:01:38 +0800 | [diff] [blame] | 849 | static int xfrmi4_rcv_tunnel(struct sk_buff *skb) |
| 850 | { |
| 851 | return xfrm4_rcv_spi(skb, IPPROTO_IPIP, ip_hdr(skb)->saddr); |
| 852 | } |
| 853 | |
| 854 | static struct xfrm_tunnel xfrmi_ipip_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 = 3, |
Xin Long | da9bbf0 | 2020-07-06 20:01:38 +0800 | [diff] [blame] | 859 | }; |
Xin Long | 8b404f4 | 2020-07-13 15:42:38 +0800 | [diff] [blame] | 860 | |
| 861 | static struct xfrm_tunnel xfrmi_ipip6_handler __read_mostly = { |
| 862 | .handler = xfrmi4_rcv_tunnel, |
| 863 | .cb_handler = xfrmi_rcv_cb, |
| 864 | .err_handler = xfrmi4_err, |
Xin Long | 7fe9461 | 2020-10-08 16:13:24 +0800 | [diff] [blame] | 865 | .priority = 2, |
Xin Long | 8b404f4 | 2020-07-13 15:42:38 +0800 | [diff] [blame] | 866 | }; |
Xin Long | da9bbf0 | 2020-07-06 20:01:38 +0800 | [diff] [blame] | 867 | #endif |
| 868 | |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 869 | static int __init xfrmi4_init(void) |
| 870 | { |
| 871 | int err; |
| 872 | |
| 873 | err = xfrm4_protocol_register(&xfrmi_esp4_protocol, IPPROTO_ESP); |
| 874 | if (err < 0) |
| 875 | goto xfrm_proto_esp_failed; |
| 876 | err = xfrm4_protocol_register(&xfrmi_ah4_protocol, IPPROTO_AH); |
| 877 | if (err < 0) |
| 878 | goto xfrm_proto_ah_failed; |
| 879 | err = xfrm4_protocol_register(&xfrmi_ipcomp4_protocol, IPPROTO_COMP); |
| 880 | if (err < 0) |
| 881 | goto xfrm_proto_comp_failed; |
Xin Long | 0a0d93b | 2020-07-17 15:02:30 +0800 | [diff] [blame] | 882 | #if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL) |
Xin Long | da9bbf0 | 2020-07-06 20:01:38 +0800 | [diff] [blame] | 883 | err = xfrm4_tunnel_register(&xfrmi_ipip_handler, AF_INET); |
| 884 | if (err < 0) |
| 885 | goto xfrm_tunnel_ipip_failed; |
Xin Long | 8b404f4 | 2020-07-13 15:42:38 +0800 | [diff] [blame] | 886 | err = xfrm4_tunnel_register(&xfrmi_ipip6_handler, AF_INET6); |
Xin Long | da9bbf0 | 2020-07-06 20:01:38 +0800 | [diff] [blame] | 887 | if (err < 0) |
| 888 | goto xfrm_tunnel_ipip6_failed; |
| 889 | #endif |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 890 | |
| 891 | return 0; |
| 892 | |
Xin Long | 0a0d93b | 2020-07-17 15:02:30 +0800 | [diff] [blame] | 893 | #if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL) |
Xin Long | da9bbf0 | 2020-07-06 20:01:38 +0800 | [diff] [blame] | 894 | xfrm_tunnel_ipip6_failed: |
| 895 | xfrm4_tunnel_deregister(&xfrmi_ipip_handler, AF_INET); |
| 896 | xfrm_tunnel_ipip_failed: |
| 897 | xfrm4_protocol_deregister(&xfrmi_ipcomp4_protocol, IPPROTO_COMP); |
| 898 | #endif |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 899 | xfrm_proto_comp_failed: |
| 900 | xfrm4_protocol_deregister(&xfrmi_ah4_protocol, IPPROTO_AH); |
| 901 | xfrm_proto_ah_failed: |
| 902 | xfrm4_protocol_deregister(&xfrmi_esp4_protocol, IPPROTO_ESP); |
| 903 | xfrm_proto_esp_failed: |
| 904 | return err; |
| 905 | } |
| 906 | |
| 907 | static void xfrmi4_fini(void) |
| 908 | { |
Xin Long | 0a0d93b | 2020-07-17 15:02:30 +0800 | [diff] [blame] | 909 | #if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL) |
Xin Long | 8b404f4 | 2020-07-13 15:42:38 +0800 | [diff] [blame] | 910 | xfrm4_tunnel_deregister(&xfrmi_ipip6_handler, AF_INET6); |
Xin Long | da9bbf0 | 2020-07-06 20:01:38 +0800 | [diff] [blame] | 911 | xfrm4_tunnel_deregister(&xfrmi_ipip_handler, AF_INET); |
| 912 | #endif |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 913 | xfrm4_protocol_deregister(&xfrmi_ipcomp4_protocol, IPPROTO_COMP); |
| 914 | xfrm4_protocol_deregister(&xfrmi_ah4_protocol, IPPROTO_AH); |
| 915 | xfrm4_protocol_deregister(&xfrmi_esp4_protocol, IPPROTO_ESP); |
| 916 | } |
| 917 | |
| 918 | static int __init xfrmi6_init(void) |
| 919 | { |
| 920 | int err; |
| 921 | |
| 922 | err = xfrm6_protocol_register(&xfrmi_esp6_protocol, IPPROTO_ESP); |
| 923 | if (err < 0) |
| 924 | goto xfrm_proto_esp_failed; |
| 925 | err = xfrm6_protocol_register(&xfrmi_ah6_protocol, IPPROTO_AH); |
| 926 | if (err < 0) |
| 927 | goto xfrm_proto_ah_failed; |
| 928 | err = xfrm6_protocol_register(&xfrmi_ipcomp6_protocol, IPPROTO_COMP); |
| 929 | if (err < 0) |
| 930 | goto xfrm_proto_comp_failed; |
Xin Long | 0a0d93b | 2020-07-17 15:02:30 +0800 | [diff] [blame] | 931 | #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL) |
Xin Long | d7b360c | 2020-07-06 20:01:37 +0800 | [diff] [blame] | 932 | err = xfrm6_tunnel_register(&xfrmi_ipv6_handler, AF_INET6); |
| 933 | if (err < 0) |
| 934 | goto xfrm_tunnel_ipv6_failed; |
Xin Long | 8b404f4 | 2020-07-13 15:42:38 +0800 | [diff] [blame] | 935 | err = xfrm6_tunnel_register(&xfrmi_ip6ip_handler, AF_INET); |
Xin Long | d7b360c | 2020-07-06 20:01:37 +0800 | [diff] [blame] | 936 | if (err < 0) |
| 937 | goto xfrm_tunnel_ip6ip_failed; |
| 938 | #endif |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 939 | |
| 940 | return 0; |
| 941 | |
Xin Long | 0a0d93b | 2020-07-17 15:02:30 +0800 | [diff] [blame] | 942 | #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL) |
Xin Long | d7b360c | 2020-07-06 20:01:37 +0800 | [diff] [blame] | 943 | xfrm_tunnel_ip6ip_failed: |
| 944 | xfrm6_tunnel_deregister(&xfrmi_ipv6_handler, AF_INET6); |
| 945 | xfrm_tunnel_ipv6_failed: |
| 946 | xfrm6_protocol_deregister(&xfrmi_ipcomp6_protocol, IPPROTO_COMP); |
| 947 | #endif |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 948 | xfrm_proto_comp_failed: |
| 949 | xfrm6_protocol_deregister(&xfrmi_ah6_protocol, IPPROTO_AH); |
| 950 | xfrm_proto_ah_failed: |
| 951 | xfrm6_protocol_deregister(&xfrmi_esp6_protocol, IPPROTO_ESP); |
| 952 | xfrm_proto_esp_failed: |
| 953 | return err; |
| 954 | } |
| 955 | |
| 956 | static void xfrmi6_fini(void) |
| 957 | { |
Xin Long | 0a0d93b | 2020-07-17 15:02:30 +0800 | [diff] [blame] | 958 | #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL) |
Xin Long | 8b404f4 | 2020-07-13 15:42:38 +0800 | [diff] [blame] | 959 | xfrm6_tunnel_deregister(&xfrmi_ip6ip_handler, AF_INET); |
Xin Long | d7b360c | 2020-07-06 20:01:37 +0800 | [diff] [blame] | 960 | xfrm6_tunnel_deregister(&xfrmi_ipv6_handler, AF_INET6); |
| 961 | #endif |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 962 | xfrm6_protocol_deregister(&xfrmi_ipcomp6_protocol, IPPROTO_COMP); |
| 963 | xfrm6_protocol_deregister(&xfrmi_ah6_protocol, IPPROTO_AH); |
| 964 | xfrm6_protocol_deregister(&xfrmi_esp6_protocol, IPPROTO_ESP); |
| 965 | } |
| 966 | |
| 967 | static const struct xfrm_if_cb xfrm_if_cb = { |
| 968 | .decode_session = xfrmi_decode_session, |
| 969 | }; |
| 970 | |
| 971 | static int __init xfrmi_init(void) |
| 972 | { |
| 973 | const char *msg; |
| 974 | int err; |
| 975 | |
| 976 | pr_info("IPsec XFRM device driver\n"); |
| 977 | |
| 978 | msg = "tunnel device"; |
| 979 | err = register_pernet_device(&xfrmi_net_ops); |
| 980 | if (err < 0) |
| 981 | goto pernet_dev_failed; |
| 982 | |
| 983 | msg = "xfrm4 protocols"; |
| 984 | err = xfrmi4_init(); |
| 985 | if (err < 0) |
| 986 | goto xfrmi4_failed; |
| 987 | |
| 988 | msg = "xfrm6 protocols"; |
| 989 | err = xfrmi6_init(); |
| 990 | if (err < 0) |
| 991 | goto xfrmi6_failed; |
| 992 | |
| 993 | |
| 994 | msg = "netlink interface"; |
| 995 | err = rtnl_link_register(&xfrmi_link_ops); |
| 996 | if (err < 0) |
| 997 | goto rtnl_link_failed; |
| 998 | |
| 999 | xfrm_if_register_cb(&xfrm_if_cb); |
| 1000 | |
| 1001 | return err; |
| 1002 | |
| 1003 | rtnl_link_failed: |
| 1004 | xfrmi6_fini(); |
| 1005 | xfrmi6_failed: |
| 1006 | xfrmi4_fini(); |
| 1007 | xfrmi4_failed: |
| 1008 | unregister_pernet_device(&xfrmi_net_ops); |
| 1009 | pernet_dev_failed: |
| 1010 | pr_err("xfrmi init: failed to register %s\n", msg); |
| 1011 | return err; |
| 1012 | } |
| 1013 | |
| 1014 | static void __exit xfrmi_fini(void) |
| 1015 | { |
| 1016 | xfrm_if_unregister_cb(); |
| 1017 | rtnl_link_unregister(&xfrmi_link_ops); |
| 1018 | xfrmi4_fini(); |
| 1019 | xfrmi6_fini(); |
| 1020 | unregister_pernet_device(&xfrmi_net_ops); |
| 1021 | } |
| 1022 | |
| 1023 | module_init(xfrmi_init); |
| 1024 | module_exit(xfrmi_fini); |
| 1025 | MODULE_LICENSE("GPL"); |
| 1026 | MODULE_ALIAS_RTNL_LINK("xfrm"); |
| 1027 | MODULE_ALIAS_NETDEV("xfrm0"); |
| 1028 | MODULE_AUTHOR("Steffen Klassert"); |
| 1029 | MODULE_DESCRIPTION("XFRM virtual interface"); |