11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * xfrm4_output.c - Common IPsec encapsulation code for IPv4. 31da177e4SLinus Torvalds * Copyright (c) 2004 Herbert Xu <herbert@gondor.apana.org.au> 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 61da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 71da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 81da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 1109b8f7a9SHerbert Xu #include <linux/if_ether.h> 1209b8f7a9SHerbert Xu #include <linux/kernel.h> 1336cf9acfSHerbert Xu #include <linux/module.h> 141da177e4SLinus Torvalds #include <linux/skbuff.h> 1516a6677fSPatrick McHardy #include <linux/netfilter_ipv4.h> 1636cf9acfSHerbert Xu #include <net/dst.h> 171da177e4SLinus Torvalds #include <net/ip.h> 181da177e4SLinus Torvalds #include <net/xfrm.h> 191da177e4SLinus Torvalds #include <net/icmp.h> 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds static int xfrm4_tunnel_check_size(struct sk_buff *skb) 221da177e4SLinus Torvalds { 231da177e4SLinus Torvalds int mtu, ret = 0; 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds if (IPCB(skb)->flags & IPSKB_XFRM_TUNNEL_SIZE) 261da177e4SLinus Torvalds goto out; 271da177e4SLinus Torvalds 2860ff7467SWANG Cong if (!(ip_hdr(skb)->frag_off & htons(IP_DF)) || skb->ignore_df) 291da177e4SLinus Torvalds goto out; 301da177e4SLinus Torvalds 315a25cf1eSHannes Frederic Sowa mtu = dst_mtu(skb_dst(skb)); 321da177e4SLinus Torvalds if (skb->len > mtu) { 33b00897b8SSteffen Klassert if (skb->sk) 34628e341fSHannes Frederic Sowa xfrm_local_error(skb, mtu); 35b00897b8SSteffen Klassert else 36b00897b8SSteffen Klassert icmp_send(skb, ICMP_DEST_UNREACH, 37b00897b8SSteffen Klassert ICMP_FRAG_NEEDED, htonl(mtu)); 381da177e4SLinus Torvalds ret = -EMSGSIZE; 391da177e4SLinus Torvalds } 401da177e4SLinus Torvalds out: 411da177e4SLinus Torvalds return ret; 421da177e4SLinus Torvalds } 431da177e4SLinus Torvalds 4436cf9acfSHerbert Xu int xfrm4_extract_output(struct xfrm_state *x, struct sk_buff *skb) 451da177e4SLinus Torvalds { 461da177e4SLinus Torvalds int err; 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds err = xfrm4_tunnel_check_size(skb); 491da177e4SLinus Torvalds if (err) 5036cf9acfSHerbert Xu return err; 5136cf9acfSHerbert Xu 5260d5fcfbSHerbert Xu XFRM_MODE_SKB_CB(skb)->protocol = ip_hdr(skb)->protocol; 5360d5fcfbSHerbert Xu 5436cf9acfSHerbert Xu return xfrm4_extract_header(skb); 551da177e4SLinus Torvalds } 561da177e4SLinus Torvalds 5736cf9acfSHerbert Xu int xfrm4_prepare_output(struct xfrm_state *x, struct sk_buff *skb) 5836cf9acfSHerbert Xu { 5936cf9acfSHerbert Xu int err; 6036cf9acfSHerbert Xu 61df9dcb45SKazunori MIYAZAWA err = xfrm_inner_extract_output(x, skb); 6236cf9acfSHerbert Xu if (err) 6336cf9acfSHerbert Xu return err; 6436cf9acfSHerbert Xu 655596732fSSteffen Klassert IPCB(skb)->flags |= IPSKB_XFRM_TUNNEL_SIZE; 66044a832aSSteffen Klassert skb->protocol = htons(ETH_P_IP); 6736cf9acfSHerbert Xu 6836cf9acfSHerbert Xu return x->outer_mode->output2(x, skb); 6936cf9acfSHerbert Xu } 7036cf9acfSHerbert Xu EXPORT_SYMBOL(xfrm4_prepare_output); 7136cf9acfSHerbert Xu 727026b1ddSDavid Miller int xfrm4_output_finish(struct sock *sk, struct sk_buff *skb) 7309b8f7a9SHerbert Xu { 745596732fSSteffen Klassert memset(IPCB(skb), 0, sizeof(*IPCB(skb))); 75862b82c6SHerbert Xu 765596732fSSteffen Klassert #ifdef CONFIG_NETFILTER 77862b82c6SHerbert Xu IPCB(skb)->flags |= IPSKB_XFRM_TRANSFORMED; 7809b8f7a9SHerbert Xu #endif 7909b8f7a9SHerbert Xu 807026b1ddSDavid Miller return xfrm_output(sk, skb); 8109b8f7a9SHerbert Xu } 8209b8f7a9SHerbert Xu 830c4b51f0SEric W. Biederman static int __xfrm4_output(struct net *net, struct sock *sk, struct sk_buff *skb) 845596732fSSteffen Klassert { 855596732fSSteffen Klassert struct xfrm_state *x = skb_dst(skb)->xfrm; 865596732fSSteffen Klassert 875596732fSSteffen Klassert #ifdef CONFIG_NETFILTER 885596732fSSteffen Klassert if (!x) { 895596732fSSteffen Klassert IPCB(skb)->flags |= IPSKB_REROUTED; 9013206b6bSEric W. Biederman return dst_output(net, sk, skb); 915596732fSSteffen Klassert } 925596732fSSteffen Klassert #endif 935596732fSSteffen Klassert 947026b1ddSDavid Miller return x->outer_mode->afinfo->output_finish(sk, skb); 955596732fSSteffen Klassert } 965596732fSSteffen Klassert 97*ede2059dSEric W. Biederman int xfrm4_output(struct net *net, struct sock *sk, struct sk_buff *skb) 9816a6677fSPatrick McHardy { 9929a26a56SEric W. Biederman return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, 10029a26a56SEric W. Biederman net, sk, skb, NULL, skb_dst(skb)->dev, 10129a26a56SEric W. Biederman __xfrm4_output, 10248d5cad8SPatrick McHardy !(IPCB(skb)->flags & IPSKB_REROUTED)); 10316a6677fSPatrick McHardy } 104628e341fSHannes Frederic Sowa 105628e341fSHannes Frederic Sowa void xfrm4_local_error(struct sk_buff *skb, u32 mtu) 106628e341fSHannes Frederic Sowa { 107628e341fSHannes Frederic Sowa struct iphdr *hdr; 108628e341fSHannes Frederic Sowa 109628e341fSHannes Frederic Sowa hdr = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb); 110628e341fSHannes Frederic Sowa ip_local_error(skb->sk, EMSGSIZE, hdr->daddr, 111628e341fSHannes Frederic Sowa inet_sk(skb->sk)->inet_dport, mtu); 112628e341fSHannes Frederic Sowa } 113