5 * YOSHIFUJI Hideaki @USAGI
6 * Split up af-specific portion
12 #include <linux/pfkeyv2.h>
13 #include <linux/ipsec.h>
15 static struct xfrm_state_afinfo xfrm4_state_afinfo;
17 static int xfrm4_init_flags(struct xfrm_state *x)
19 if (ipv4_config.no_pmtu_disc)
20 x->props.flags |= XFRM_STATE_NOPMTUDISC;
25 __xfrm4_init_tempsel(struct xfrm_state *x, struct flowi *fl,
26 struct xfrm_tmpl *tmpl,
27 xfrm_address_t *daddr, xfrm_address_t *saddr)
29 x->sel.daddr.a4 = fl->fl4_dst;
30 x->sel.saddr.a4 = fl->fl4_src;
31 x->sel.dport = xfrm_flowi_dport(fl);
32 x->sel.dport_mask = ~0;
33 x->sel.sport = xfrm_flowi_sport(fl);
34 x->sel.sport_mask = ~0;
35 x->sel.prefixlen_d = 32;
36 x->sel.prefixlen_s = 32;
37 x->sel.proto = fl->proto;
38 x->sel.ifindex = fl->oif;
40 if (x->id.daddr.a4 == 0)
41 x->id.daddr.a4 = daddr->a4;
42 x->props.saddr = tmpl->saddr;
43 if (x->props.saddr.a4 == 0)
44 x->props.saddr.a4 = saddr->a4;
45 if (tmpl->mode && x->props.saddr.a4 == 0) {
47 struct flowi fl_tunnel = {
50 .daddr = x->id.daddr.a4,
54 if (!xfrm_dst_lookup((struct xfrm_dst **)&rt,
55 &fl_tunnel, AF_INET)) {
56 x->props.saddr.a4 = rt->rt_src;
57 dst_release(&rt->u.dst);
60 x->props.mode = tmpl->mode;
61 x->props.reqid = tmpl->reqid;
62 x->props.family = AF_INET;
65 static struct xfrm_state *
66 __xfrm4_state_lookup(xfrm_address_t *daddr, u32 spi, u8 proto)
68 unsigned h = __xfrm4_spi_hash(daddr, spi, proto);
71 list_for_each_entry(x, xfrm4_state_afinfo.state_byspi+h, byspi) {
72 if (x->props.family == AF_INET &&
74 daddr->a4 == x->id.daddr.a4 &&
75 proto == x->id.proto) {
83 static struct xfrm_state *
84 __xfrm4_find_acq(u8 mode, u32 reqid, u8 proto,
85 xfrm_address_t *daddr, xfrm_address_t *saddr,
88 struct xfrm_state *x, *x0;
89 unsigned h = __xfrm4_dst_hash(daddr);
93 list_for_each_entry(x, xfrm4_state_afinfo.state_bydst+h, bydst) {
94 if (x->props.family == AF_INET &&
95 daddr->a4 == x->id.daddr.a4 &&
96 mode == x->props.mode &&
97 proto == x->id.proto &&
98 saddr->a4 == x->props.saddr.a4 &&
99 reqid == x->props.reqid &&
100 x->km.state == XFRM_STATE_ACQ &&
106 if (!x0 && create && (x0 = xfrm_state_alloc()) != NULL) {
107 x0->sel.daddr.a4 = daddr->a4;
108 x0->sel.saddr.a4 = saddr->a4;
109 x0->sel.prefixlen_d = 32;
110 x0->sel.prefixlen_s = 32;
111 x0->props.saddr.a4 = saddr->a4;
112 x0->km.state = XFRM_STATE_ACQ;
113 x0->id.daddr.a4 = daddr->a4;
114 x0->id.proto = proto;
115 x0->props.family = AF_INET;
116 x0->props.mode = mode;
117 x0->props.reqid = reqid;
118 x0->props.family = AF_INET;
119 x0->lft.hard_add_expires_seconds = XFRM_ACQ_EXPIRES;
121 x0->timer.expires = jiffies + XFRM_ACQ_EXPIRES*HZ;
122 add_timer(&x0->timer);
124 list_add_tail(&x0->bydst, xfrm4_state_afinfo.state_bydst+h);
132 static struct xfrm_state_afinfo xfrm4_state_afinfo = {
134 .init_flags = xfrm4_init_flags,
135 .init_tempsel = __xfrm4_init_tempsel,
136 .state_lookup = __xfrm4_state_lookup,
137 .find_acq = __xfrm4_find_acq,
140 void __init xfrm4_state_init(void)
142 xfrm_state_register_afinfo(&xfrm4_state_afinfo);
146 void __exit xfrm4_state_fini(void)
148 xfrm_state_unregister_afinfo(&xfrm4_state_afinfo);