MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / net / ipv4 / xfrm4_policy.c
blob3ce69883bcc404892214ae0ca13a2277791d841a
1 /*
2 * xfrm4_policy.c
4 * Changes:
5 * Kazunori MIYAZAWA @USAGI
6 * YOSHIFUJI Hideaki @USAGI
7 * Split up af-specific portion
8 *
9 */
11 #include <linux/config.h>
12 #include <net/xfrm.h>
13 #include <net/ip.h>
15 static struct dst_ops xfrm4_dst_ops;
16 static struct xfrm_policy_afinfo xfrm4_policy_afinfo;
18 static struct xfrm_type_map xfrm4_type_map = { .lock = RW_LOCK_UNLOCKED };
20 static int xfrm4_dst_lookup(struct xfrm_dst **dst, struct flowi *fl)
22 return __ip_route_output_key((struct rtable**)dst, fl);
25 /* Check that the bundle accepts the flow and its components are
26 * still valid.
29 static int __xfrm4_bundle_ok(struct xfrm_dst *xdst, struct flowi *fl)
31 do {
32 if (xdst->u.dst.ops != &xfrm4_dst_ops)
33 return 1;
35 if (!xfrm_selector_match(&xdst->u.dst.xfrm->sel, fl, AF_INET))
36 return 0;
37 if (xdst->u.dst.xfrm->km.state != XFRM_STATE_VALID ||
38 xdst->u.dst.path->obsolete > 0)
39 return 0;
40 xdst = (struct xfrm_dst*)xdst->u.dst.child;
41 } while (xdst);
42 return 0;
45 static struct dst_entry *
46 __xfrm4_find_bundle(struct flowi *fl, struct xfrm_policy *policy)
48 struct dst_entry *dst;
50 read_lock_bh(&policy->lock);
51 for (dst = policy->bundles; dst; dst = dst->next) {
52 struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
53 if (xdst->u.rt.fl.oif == fl->oif && /*XXX*/
54 xdst->u.rt.fl.fl4_dst == fl->fl4_dst &&
55 xdst->u.rt.fl.fl4_src == fl->fl4_src &&
56 __xfrm4_bundle_ok(xdst, fl)) {
57 dst_clone(dst);
58 break;
61 read_unlock_bh(&policy->lock);
62 return dst;
65 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
66 * all the metrics... Shortly, bundle a bundle.
69 static int
70 __xfrm4_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
71 struct flowi *fl, struct dst_entry **dst_p)
73 struct dst_entry *dst, *dst_prev;
74 struct rtable *rt0 = (struct rtable*)(*dst_p);
75 struct rtable *rt = rt0;
76 u32 remote = fl->fl4_dst;
77 u32 local = fl->fl4_src;
78 int i;
79 int err;
80 int header_len = 0;
81 int trailer_len = 0;
83 dst = dst_prev = NULL;
85 for (i = 0; i < nx; i++) {
86 struct dst_entry *dst1 = dst_alloc(&xfrm4_dst_ops);
88 if (unlikely(dst1 == NULL)) {
89 err = -ENOBUFS;
90 goto error;
93 if (!dst)
94 dst = dst1;
95 else {
96 dst_prev->child = dst1;
97 dst1->flags |= DST_NOHASH;
98 dst_clone(dst1);
100 dst_prev = dst1;
101 if (xfrm[i]->props.mode) {
102 remote = xfrm[i]->id.daddr.a4;
103 local = xfrm[i]->props.saddr.a4;
105 header_len += xfrm[i]->props.header_len;
106 trailer_len += xfrm[i]->props.trailer_len;
109 if (remote != fl->fl4_dst) {
110 struct flowi fl_tunnel = { .nl_u = { .ip4_u =
111 { .daddr = remote,
112 .saddr = local }
115 err = xfrm_dst_lookup((struct xfrm_dst**)&rt, &fl_tunnel, AF_INET);
116 if (err)
117 goto error;
118 } else {
119 dst_hold(&rt->u.dst);
121 dst_prev->child = &rt->u.dst;
122 i = 0;
123 for (dst_prev = dst; dst_prev != &rt->u.dst; dst_prev = dst_prev->child) {
124 struct xfrm_dst *x = (struct xfrm_dst*)dst_prev;
125 x->u.rt.fl = *fl;
127 dst_prev->xfrm = xfrm[i++];
128 dst_prev->dev = rt->u.dst.dev;
129 if (rt->u.dst.dev)
130 dev_hold(rt->u.dst.dev);
131 dst_prev->obsolete = -1;
132 dst_prev->flags |= DST_HOST;
133 dst_prev->lastuse = jiffies;
134 dst_prev->header_len = header_len;
135 dst_prev->trailer_len = trailer_len;
136 memcpy(&dst_prev->metrics, &rt->u.dst.metrics, sizeof(dst_prev->metrics));
137 dst_prev->path = &rt->u.dst;
139 /* Copy neighbout for reachability confirmation */
140 dst_prev->neighbour = neigh_clone(rt->u.dst.neighbour);
141 dst_prev->input = rt->u.dst.input;
142 dst_prev->output = xfrm4_output;
143 if (rt->peer)
144 atomic_inc(&rt->peer->refcnt);
145 x->u.rt.peer = rt->peer;
146 /* Sheit... I remember I did this right. Apparently,
147 * it was magically lost, so this code needs audit */
148 x->u.rt.rt_flags = rt0->rt_flags&(RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL);
149 x->u.rt.rt_type = rt->rt_type;
150 x->u.rt.rt_src = rt0->rt_src;
151 x->u.rt.rt_dst = rt0->rt_dst;
152 x->u.rt.rt_gateway = rt->rt_gateway;
153 x->u.rt.rt_spec_dst = rt0->rt_spec_dst;
154 header_len -= x->u.dst.xfrm->props.header_len;
155 trailer_len -= x->u.dst.xfrm->props.trailer_len;
157 *dst_p = dst;
158 return 0;
160 error:
161 if (dst)
162 dst_free(dst);
163 return err;
166 static void
167 _decode_session4(struct sk_buff *skb, struct flowi *fl)
169 struct iphdr *iph = skb->nh.iph;
170 u8 *xprth = skb->nh.raw + iph->ihl*4;
172 memset(fl, 0, sizeof(struct flowi));
173 if (!(iph->frag_off & htons(IP_MF | IP_OFFSET))) {
174 switch (iph->protocol) {
175 case IPPROTO_UDP:
176 case IPPROTO_TCP:
177 case IPPROTO_SCTP:
178 if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
179 u16 *ports = (u16 *)xprth;
181 fl->fl_ip_sport = ports[0];
182 fl->fl_ip_dport = ports[1];
184 break;
186 case IPPROTO_ICMP:
187 if (pskb_may_pull(skb, xprth + 2 - skb->data)) {
188 u8 *icmp = xprth;
190 fl->fl_icmp_type = icmp[0];
191 fl->fl_icmp_code = icmp[1];
193 break;
195 case IPPROTO_ESP:
196 if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
197 u32 *ehdr = (u32 *)xprth;
199 fl->fl_ipsec_spi = ehdr[0];
201 break;
203 case IPPROTO_AH:
204 if (pskb_may_pull(skb, xprth + 8 - skb->data)) {
205 u32 *ah_hdr = (u32*)xprth;
207 fl->fl_ipsec_spi = ah_hdr[1];
209 break;
211 case IPPROTO_COMP:
212 if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
213 u16 *ipcomp_hdr = (u16 *)xprth;
215 fl->fl_ipsec_spi = ntohl(ntohs(ipcomp_hdr[1]));
217 break;
218 default:
219 fl->fl_ipsec_spi = 0;
220 break;
223 fl->proto = iph->protocol;
224 fl->fl4_dst = iph->daddr;
225 fl->fl4_src = iph->saddr;
228 static inline int xfrm4_garbage_collect(void)
230 read_lock(&xfrm4_policy_afinfo.lock);
231 xfrm4_policy_afinfo.garbage_collect();
232 read_unlock(&xfrm4_policy_afinfo.lock);
233 return (atomic_read(&xfrm4_dst_ops.entries) > xfrm4_dst_ops.gc_thresh*2);
236 static void xfrm4_update_pmtu(struct dst_entry *dst, u32 mtu)
238 struct dst_entry *path = dst->path;
240 if (mtu < 68 + dst->header_len)
241 return;
243 path->ops->update_pmtu(path, mtu);
246 static struct dst_ops xfrm4_dst_ops = {
247 .family = AF_INET,
248 .protocol = __constant_htons(ETH_P_IP),
249 .gc = xfrm4_garbage_collect,
250 .update_pmtu = xfrm4_update_pmtu,
251 .gc_thresh = 1024,
252 .entry_size = sizeof(struct xfrm_dst),
255 static struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
256 .family = AF_INET,
257 .lock = RW_LOCK_UNLOCKED,
258 .type_map = &xfrm4_type_map,
259 .dst_ops = &xfrm4_dst_ops,
260 .dst_lookup = xfrm4_dst_lookup,
261 .find_bundle = __xfrm4_find_bundle,
262 .bundle_create = __xfrm4_bundle_create,
263 .decode_session = _decode_session4,
266 static void __init xfrm4_policy_init(void)
268 xfrm_policy_register_afinfo(&xfrm4_policy_afinfo);
271 static void __exit xfrm4_policy_fini(void)
273 xfrm_policy_unregister_afinfo(&xfrm4_policy_afinfo);
276 void __init xfrm4_init(void)
278 xfrm4_state_init();
279 xfrm4_policy_init();
282 void __exit xfrm4_fini(void)
284 //xfrm4_input_fini();
285 xfrm4_policy_fini();
286 xfrm4_state_fini();