Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * xfrm4_output.c - Common IPsec encapsulation code for IPv4. |
| 4 | * Copyright (c) 2004 Herbert Xu <herbert@gondor.apana.org.au> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
Herbert Xu | 09b8f7a | 2006-06-22 03:08:03 -0700 | [diff] [blame] | 7 | #include <linux/if_ether.h> |
| 8 | #include <linux/kernel.h> |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 9 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/skbuff.h> |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 11 | #include <linux/netfilter_ipv4.h> |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 12 | #include <net/dst.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <net/ip.h> |
| 14 | #include <net/xfrm.h> |
| 15 | #include <net/icmp.h> |
| 16 | |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 17 | static int __xfrm4_output(struct net *net, struct sock *sk, struct sk_buff *skb) |
Steffen Klassert | 5596732 | 2014-04-07 08:08:52 +0200 | [diff] [blame] | 18 | { |
Florian Westphal | 7d4343d | 2020-05-11 10:33:42 +0200 | [diff] [blame^] | 19 | #ifdef CONFIG_NETFILTER |
Steffen Klassert | 5596732 | 2014-04-07 08:08:52 +0200 | [diff] [blame] | 20 | struct xfrm_state *x = skb_dst(skb)->xfrm; |
| 21 | |
Steffen Klassert | 5596732 | 2014-04-07 08:08:52 +0200 | [diff] [blame] | 22 | if (!x) { |
| 23 | IPCB(skb)->flags |= IPSKB_REROUTED; |
Eric W. Biederman | 13206b6 | 2015-10-07 16:48:35 -0500 | [diff] [blame] | 24 | return dst_output(net, sk, skb); |
Steffen Klassert | 5596732 | 2014-04-07 08:08:52 +0200 | [diff] [blame] | 25 | } |
| 26 | #endif |
| 27 | |
Florian Westphal | 2ab6096 | 2020-05-04 10:06:09 +0200 | [diff] [blame] | 28 | return xfrm_output(sk, skb); |
Steffen Klassert | 5596732 | 2014-04-07 08:08:52 +0200 | [diff] [blame] | 29 | } |
| 30 | |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 31 | int xfrm4_output(struct net *net, struct sock *sk, struct sk_buff *skb) |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 32 | { |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 33 | return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, |
Phil Sutter | 28f8bfd | 2019-11-12 17:14:37 +0100 | [diff] [blame] | 34 | net, sk, skb, skb->dev, skb_dst(skb)->dev, |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 35 | __xfrm4_output, |
Patrick McHardy | 48d5cad | 2006-02-15 15:10:22 -0800 | [diff] [blame] | 36 | !(IPCB(skb)->flags & IPSKB_REROUTED)); |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 37 | } |
Hannes Frederic Sowa | 628e341 | 2013-08-14 13:05:23 +0200 | [diff] [blame] | 38 | |
| 39 | void xfrm4_local_error(struct sk_buff *skb, u32 mtu) |
| 40 | { |
| 41 | struct iphdr *hdr; |
| 42 | |
| 43 | hdr = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb); |
| 44 | ip_local_error(skb->sk, EMSGSIZE, hdr->daddr, |
| 45 | inet_sk(skb->sk)->inet_dport, mtu); |
| 46 | } |