netns xfrm: ->dst_lookup in netns
[linux-2.6.git] / net / ipv4 / xfrm4_policy.c
1 /*
2  * xfrm4_policy.c
3  *
4  * Changes:
5  *      Kazunori MIYAZAWA @USAGI
6  *      YOSHIFUJI Hideaki @USAGI
7  *              Split up af-specific portion
8  *
9  */
10
11 #include <linux/err.h>
12 #include <linux/kernel.h>
13 #include <linux/inetdevice.h>
14 #include <net/dst.h>
15 #include <net/xfrm.h>
16 #include <net/ip.h>
17
18 static struct dst_ops xfrm4_dst_ops;
19 static struct xfrm_policy_afinfo xfrm4_policy_afinfo;
20
21 static struct dst_entry *xfrm4_dst_lookup(struct net *net, int tos,
22                                           xfrm_address_t *saddr,
23                                           xfrm_address_t *daddr)
24 {
25         struct flowi fl = {
26                 .nl_u = {
27                         .ip4_u = {
28                                 .tos = tos,
29                                 .daddr = daddr->a4,
30                         },
31                 },
32         };
33         struct dst_entry *dst;
34         struct rtable *rt;
35         int err;
36
37         if (saddr)
38                 fl.fl4_src = saddr->a4;
39
40         err = __ip_route_output_key(net, &rt, &fl);
41         dst = &rt->u.dst;
42         if (err)
43                 dst = ERR_PTR(err);
44         return dst;
45 }
46
47 static int xfrm4_get_saddr(xfrm_address_t *saddr, xfrm_address_t *daddr)
48 {
49         struct dst_entry *dst;
50         struct rtable *rt;
51
52         dst = xfrm4_dst_lookup(&init_net, 0, NULL, daddr);
53         if (IS_ERR(dst))
54                 return -EHOSTUNREACH;
55
56         rt = (struct rtable *)dst;
57         saddr->a4 = rt->rt_src;
58         dst_release(dst);
59         return 0;
60 }
61
62 static struct dst_entry *
63 __xfrm4_find_bundle(struct flowi *fl, struct xfrm_policy *policy)
64 {
65         struct dst_entry *dst;
66
67         read_lock_bh(&policy->lock);
68         for (dst = policy->bundles; dst; dst = dst->next) {
69                 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
70                 if (xdst->u.rt.fl.oif == fl->oif &&     /*XXX*/
71                     xdst->u.rt.fl.fl4_dst == fl->fl4_dst &&
72                     xdst->u.rt.fl.fl4_src == fl->fl4_src &&
73                     xdst->u.rt.fl.fl4_tos == fl->fl4_tos &&
74                     xfrm_bundle_ok(policy, xdst, fl, AF_INET, 0)) {
75                         dst_clone(dst);
76                         break;
77                 }
78         }
79         read_unlock_bh(&policy->lock);
80         return dst;
81 }
82
83 static int xfrm4_get_tos(struct flowi *fl)
84 {
85         return fl->fl4_tos;
86 }
87
88 static int xfrm4_init_path(struct xfrm_dst *path, struct dst_entry *dst,
89                            int nfheader_len)
90 {
91         return 0;
92 }
93
94 static int xfrm4_fill_dst(struct xfrm_dst *xdst, struct net_device *dev)
95 {
96         struct rtable *rt = (struct rtable *)xdst->route;
97
98         xdst->u.rt.fl = rt->fl;
99
100         xdst->u.dst.dev = dev;
101         dev_hold(dev);
102
103         xdst->u.rt.idev = in_dev_get(dev);
104         if (!xdst->u.rt.idev)
105                 return -ENODEV;
106
107         xdst->u.rt.peer = rt->peer;
108         if (rt->peer)
109                 atomic_inc(&rt->peer->refcnt);
110
111         /* Sheit... I remember I did this right. Apparently,
112          * it was magically lost, so this code needs audit */
113         xdst->u.rt.rt_flags = rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST |
114                                               RTCF_LOCAL);
115         xdst->u.rt.rt_type = rt->rt_type;
116         xdst->u.rt.rt_src = rt->rt_src;
117         xdst->u.rt.rt_dst = rt->rt_dst;
118         xdst->u.rt.rt_gateway = rt->rt_gateway;
119         xdst->u.rt.rt_spec_dst = rt->rt_spec_dst;
120
121         return 0;
122 }
123
124 static void
125 _decode_session4(struct sk_buff *skb, struct flowi *fl, int reverse)
126 {
127         struct iphdr *iph = ip_hdr(skb);
128         u8 *xprth = skb_network_header(skb) + iph->ihl * 4;
129
130         memset(fl, 0, sizeof(struct flowi));
131         if (!(iph->frag_off & htons(IP_MF | IP_OFFSET))) {
132                 switch (iph->protocol) {
133                 case IPPROTO_UDP:
134                 case IPPROTO_UDPLITE:
135                 case IPPROTO_TCP:
136                 case IPPROTO_SCTP:
137                 case IPPROTO_DCCP:
138                         if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
139                                 __be16 *ports = (__be16 *)xprth;
140
141                                 fl->fl_ip_sport = ports[!!reverse];
142                                 fl->fl_ip_dport = ports[!reverse];
143                         }
144                         break;
145
146                 case IPPROTO_ICMP:
147                         if (pskb_may_pull(skb, xprth + 2 - skb->data)) {
148                                 u8 *icmp = xprth;
149
150                                 fl->fl_icmp_type = icmp[0];
151                                 fl->fl_icmp_code = icmp[1];
152                         }
153                         break;
154
155                 case IPPROTO_ESP:
156                         if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
157                                 __be32 *ehdr = (__be32 *)xprth;
158
159                                 fl->fl_ipsec_spi = ehdr[0];
160                         }
161                         break;
162
163                 case IPPROTO_AH:
164                         if (pskb_may_pull(skb, xprth + 8 - skb->data)) {
165                                 __be32 *ah_hdr = (__be32*)xprth;
166
167                                 fl->fl_ipsec_spi = ah_hdr[1];
168                         }
169                         break;
170
171                 case IPPROTO_COMP:
172                         if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
173                                 __be16 *ipcomp_hdr = (__be16 *)xprth;
174
175                                 fl->fl_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
176                         }
177                         break;
178                 default:
179                         fl->fl_ipsec_spi = 0;
180                         break;
181                 }
182         }
183         fl->proto = iph->protocol;
184         fl->fl4_dst = reverse ? iph->saddr : iph->daddr;
185         fl->fl4_src = reverse ? iph->daddr : iph->saddr;
186         fl->fl4_tos = iph->tos;
187 }
188
189 static inline int xfrm4_garbage_collect(struct dst_ops *ops)
190 {
191         xfrm4_policy_afinfo.garbage_collect(&init_net);
192         return (atomic_read(&xfrm4_dst_ops.entries) > xfrm4_dst_ops.gc_thresh*2);
193 }
194
195 static void xfrm4_update_pmtu(struct dst_entry *dst, u32 mtu)
196 {
197         struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
198         struct dst_entry *path = xdst->route;
199
200         path->ops->update_pmtu(path, mtu);
201 }
202
203 static void xfrm4_dst_destroy(struct dst_entry *dst)
204 {
205         struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
206
207         if (likely(xdst->u.rt.idev))
208                 in_dev_put(xdst->u.rt.idev);
209         if (likely(xdst->u.rt.peer))
210                 inet_putpeer(xdst->u.rt.peer);
211         xfrm_dst_destroy(xdst);
212 }
213
214 static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
215                              int unregister)
216 {
217         struct xfrm_dst *xdst;
218
219         if (!unregister)
220                 return;
221
222         xdst = (struct xfrm_dst *)dst;
223         if (xdst->u.rt.idev->dev == dev) {
224                 struct in_device *loopback_idev =
225                         in_dev_get(dev_net(dev)->loopback_dev);
226                 BUG_ON(!loopback_idev);
227
228                 do {
229                         in_dev_put(xdst->u.rt.idev);
230                         xdst->u.rt.idev = loopback_idev;
231                         in_dev_hold(loopback_idev);
232                         xdst = (struct xfrm_dst *)xdst->u.dst.child;
233                 } while (xdst->u.dst.xfrm);
234
235                 __in_dev_put(loopback_idev);
236         }
237
238         xfrm_dst_ifdown(dst, dev);
239 }
240
241 static struct dst_ops xfrm4_dst_ops = {
242         .family =               AF_INET,
243         .protocol =             __constant_htons(ETH_P_IP),
244         .gc =                   xfrm4_garbage_collect,
245         .update_pmtu =          xfrm4_update_pmtu,
246         .destroy =              xfrm4_dst_destroy,
247         .ifdown =               xfrm4_dst_ifdown,
248         .local_out =            __ip_local_out,
249         .gc_thresh =            1024,
250         .entries =              ATOMIC_INIT(0),
251 };
252
253 static struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
254         .family =               AF_INET,
255         .dst_ops =              &xfrm4_dst_ops,
256         .dst_lookup =           xfrm4_dst_lookup,
257         .get_saddr =            xfrm4_get_saddr,
258         .find_bundle =          __xfrm4_find_bundle,
259         .decode_session =       _decode_session4,
260         .get_tos =              xfrm4_get_tos,
261         .init_path =            xfrm4_init_path,
262         .fill_dst =             xfrm4_fill_dst,
263 };
264
265 static void __init xfrm4_policy_init(void)
266 {
267         xfrm_policy_register_afinfo(&xfrm4_policy_afinfo);
268 }
269
270 static void __exit xfrm4_policy_fini(void)
271 {
272         xfrm_policy_unregister_afinfo(&xfrm4_policy_afinfo);
273 }
274
275 void __init xfrm4_init(void)
276 {
277         xfrm4_state_init();
278         xfrm4_policy_init();
279 }
280