blob: d9ac0a0058b5ed10a769cd11209483c5eabd5f8e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * xfrm4_state.c
3 *
4 * Changes:
5 * YOSHIFUJI Hideaki @USAGI
6 * Split up af-specific portion
7 *
8 */
9
Herbert Xudd871472005-06-20 13:21:43 -070010#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <net/xfrm.h>
12#include <linux/pfkeyv2.h>
13#include <linux/ipsec.h>
Herbert Xu862b82c2007-11-13 21:43:11 -080014#include <linux/netfilter_ipv4.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Herbert Xudd871472005-06-20 13:21:43 -070016static int xfrm4_init_flags(struct xfrm_state *x)
17{
18 if (ipv4_config.no_pmtu_disc)
19 x->props.flags |= XFRM_STATE_NOPMTUDISC;
20 return 0;
21}
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023static void
David S. Miller73e5ebb2011-02-22 17:51:44 -080024__xfrm4_init_tempsel(struct xfrm_selector *sel, const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050026 const struct flowi4 *fl4 = &fl->u.ip4;
27
28 sel->daddr.a4 = fl4->daddr;
29 sel->saddr.a4 = fl4->saddr;
30 sel->dport = xfrm_flowi_dport(fl, &fl4->uli);
Thomas Egerer8444cf72010-09-20 11:11:38 -070031 sel->dport_mask = htons(0xffff);
David S. Miller7e1dc7b2011-03-12 02:42:11 -050032 sel->sport = xfrm_flowi_sport(fl, &fl4->uli);
Thomas Egerer8444cf72010-09-20 11:11:38 -070033 sel->sport_mask = htons(0xffff);
34 sel->family = AF_INET;
35 sel->prefixlen_d = 32;
36 sel->prefixlen_s = 32;
David S. Miller7e1dc7b2011-03-12 02:42:11 -050037 sel->proto = fl4->flowi4_proto;
38 sel->ifindex = fl4->flowi4_oif;
Thomas Egerer8444cf72010-09-20 11:11:38 -070039}
40
41static void
David S. Miller19bd6242011-02-24 00:07:20 -050042xfrm4_init_temprop(struct xfrm_state *x, const struct xfrm_tmpl *tmpl,
43 const xfrm_address_t *daddr, const xfrm_address_t *saddr)
Thomas Egerer8444cf72010-09-20 11:11:38 -070044{
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 x->id = tmpl->id;
46 if (x->id.daddr.a4 == 0)
47 x->id.daddr.a4 = daddr->a4;
48 x->props.saddr = tmpl->saddr;
49 if (x->props.saddr.a4 == 0)
50 x->props.saddr.a4 = saddr->a4;
51 x->props.mode = tmpl->mode;
52 x->props.reqid = tmpl->reqid;
53 x->props.family = AF_INET;
54}
55
Herbert Xu36cf9ac2007-11-13 21:40:52 -080056int xfrm4_extract_header(struct sk_buff *skb)
57{
Eric Dumazetb71d1d42011-04-22 04:53:02 +000058 const struct iphdr *iph = ip_hdr(skb);
Herbert Xu36cf9ac2007-11-13 21:40:52 -080059
Herbert Xu732c8bd2008-03-26 16:51:09 -070060 XFRM_MODE_SKB_CB(skb)->ihl = sizeof(*iph);
Herbert Xu36cf9ac2007-11-13 21:40:52 -080061 XFRM_MODE_SKB_CB(skb)->id = iph->id;
62 XFRM_MODE_SKB_CB(skb)->frag_off = iph->frag_off;
63 XFRM_MODE_SKB_CB(skb)->tos = iph->tos;
64 XFRM_MODE_SKB_CB(skb)->ttl = iph->ttl;
Herbert Xu732c8bd2008-03-26 16:51:09 -070065 XFRM_MODE_SKB_CB(skb)->optlen = iph->ihl * 4 - sizeof(*iph);
Herbert Xu36cf9ac2007-11-13 21:40:52 -080066 memset(XFRM_MODE_SKB_CB(skb)->flow_lbl, 0,
67 sizeof(XFRM_MODE_SKB_CB(skb)->flow_lbl));
68
69 return 0;
70}
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static struct xfrm_state_afinfo xfrm4_state_afinfo = {
73 .family = AF_INET,
Herbert Xu36cf9ac2007-11-13 21:40:52 -080074 .proto = IPPROTO_IPIP,
Herbert Xu227620e2007-11-13 21:41:28 -080075 .eth_proto = htons(ETH_P_IP),
Herbert Xu17c2a422007-10-17 21:33:12 -070076 .owner = THIS_MODULE,
Herbert Xudd871472005-06-20 13:21:43 -070077 .init_flags = xfrm4_init_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 .init_tempsel = __xfrm4_init_tempsel,
Thomas Egerer8444cf72010-09-20 11:11:38 -070079 .init_temprop = xfrm4_init_temprop,
Miika Komucdca7262007-02-06 14:24:56 -080080 .output = xfrm4_output,
Steffen Klassert43a4dea2011-05-09 19:36:38 +000081 .output_finish = xfrm4_output_finish,
Herbert Xu227620e2007-11-13 21:41:28 -080082 .extract_input = xfrm4_extract_input,
Herbert Xu36cf9ac2007-11-13 21:40:52 -080083 .extract_output = xfrm4_extract_output,
Herbert Xu716062f2007-11-13 21:44:23 -080084 .transport_finish = xfrm4_transport_finish,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085};
86
87void __init xfrm4_state_init(void)
88{
89 xfrm_state_register_afinfo(&xfrm4_state_afinfo);
90}
91
Adrian Bunk0742fd52005-08-09 19:35:47 -070092#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070093void __exit xfrm4_state_fini(void)
94{
95 xfrm_state_unregister_afinfo(&xfrm4_state_afinfo);
96}
Adrian Bunk0742fd52005-08-09 19:35:47 -070097#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098