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 <linux/if_tunnel.h> 15 #include <net/dst.h> 16 #include <net/xfrm.h> 17 #include <net/ip.h> 18 #include <net/l3mdev.h> 19 20 static struct dst_entry *__xfrm4_dst_lookup(struct net *net, struct flowi4 *fl4, 21 int tos, int oif, 22 const xfrm_address_t *saddr, 23 const xfrm_address_t *daddr, 24 u32 mark) 25 { 26 struct rtable *rt; 27 28 memset(fl4, 0, sizeof(*fl4)); 29 fl4->daddr = daddr->a4; 30 fl4->flowi4_tos = tos; 31 fl4->flowi4_oif = l3mdev_master_ifindex_by_index(net, oif); 32 fl4->flowi4_mark = mark; 33 if (saddr) 34 fl4->saddr = saddr->a4; 35 36 fl4->flowi4_flags = FLOWI_FLAG_SKIP_NH_OIF; 37 38 rt = __ip_route_output_key(net, fl4); 39 if (!IS_ERR(rt)) 40 return &rt->dst; 41 42 return ERR_CAST(rt); 43 } 44 45 static struct dst_entry *xfrm4_dst_lookup(struct net *net, int tos, int oif, 46 const xfrm_address_t *saddr, 47 const xfrm_address_t *daddr, 48 u32 mark) 49 { 50 struct flowi4 fl4; 51 52 return __xfrm4_dst_lookup(net, &fl4, tos, oif, saddr, daddr, mark); 53 } 54 55 static int xfrm4_get_saddr(struct net *net, int oif, 56 xfrm_address_t *saddr, xfrm_address_t *daddr, 57 u32 mark) 58 { 59 struct dst_entry *dst; 60 struct flowi4 fl4; 61 62 dst = __xfrm4_dst_lookup(net, &fl4, 0, oif, NULL, daddr, mark); 63 if (IS_ERR(dst)) 64 return -EHOSTUNREACH; 65 66 saddr->a4 = fl4.saddr; 67 dst_release(dst); 68 return 0; 69 } 70 71 static int xfrm4_get_tos(const struct flowi *fl) 72 { 73 return IPTOS_RT_MASK & fl->u.ip4.flowi4_tos; /* Strip ECN bits */ 74 } 75 76 static int xfrm4_init_path(struct xfrm_dst *path, struct dst_entry *dst, 77 int nfheader_len) 78 { 79 return 0; 80 } 81 82 static int xfrm4_fill_dst(struct xfrm_dst *xdst, struct net_device *dev, 83 const struct flowi *fl) 84 { 85 struct rtable *rt = (struct rtable *)xdst->route; 86 const struct flowi4 *fl4 = &fl->u.ip4; 87 88 xdst->u.rt.rt_iif = fl4->flowi4_iif; 89 90 xdst->u.dst.dev = dev; 91 dev_hold(dev); 92 93 /* Sheit... I remember I did this right. Apparently, 94 * it was magically lost, so this code needs audit */ 95 xdst->u.rt.rt_is_input = rt->rt_is_input; 96 xdst->u.rt.rt_flags = rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST | 97 RTCF_LOCAL); 98 xdst->u.rt.rt_type = rt->rt_type; 99 xdst->u.rt.rt_gateway = rt->rt_gateway; 100 xdst->u.rt.rt_uses_gateway = rt->rt_uses_gateway; 101 xdst->u.rt.rt_pmtu = rt->rt_pmtu; 102 xdst->u.rt.rt_table_id = rt->rt_table_id; 103 INIT_LIST_HEAD(&xdst->u.rt.rt_uncached); 104 105 return 0; 106 } 107 108 static void 109 _decode_session4(struct sk_buff *skb, struct flowi *fl, int reverse) 110 { 111 const struct iphdr *iph = ip_hdr(skb); 112 u8 *xprth = skb_network_header(skb) + iph->ihl * 4; 113 struct flowi4 *fl4 = &fl->u.ip4; 114 int oif = 0; 115 116 if (skb_dst(skb)) 117 oif = skb_dst(skb)->dev->ifindex; 118 119 memset(fl4, 0, sizeof(struct flowi4)); 120 fl4->flowi4_mark = skb->mark; 121 fl4->flowi4_oif = reverse ? skb->skb_iif : oif; 122 123 if (!ip_is_fragment(iph)) { 124 switch (iph->protocol) { 125 case IPPROTO_UDP: 126 case IPPROTO_UDPLITE: 127 case IPPROTO_TCP: 128 case IPPROTO_SCTP: 129 case IPPROTO_DCCP: 130 if (xprth + 4 < skb->data || 131 pskb_may_pull(skb, xprth + 4 - skb->data)) { 132 __be16 *ports; 133 134 xprth = skb_network_header(skb) + iph->ihl * 4; 135 ports = (__be16 *)xprth; 136 137 fl4->fl4_sport = ports[!!reverse]; 138 fl4->fl4_dport = ports[!reverse]; 139 } 140 break; 141 142 case IPPROTO_ICMP: 143 if (xprth + 2 < skb->data || 144 pskb_may_pull(skb, xprth + 2 - skb->data)) { 145 u8 *icmp; 146 147 xprth = skb_network_header(skb) + iph->ihl * 4; 148 icmp = xprth; 149 150 fl4->fl4_icmp_type = icmp[0]; 151 fl4->fl4_icmp_code = icmp[1]; 152 } 153 break; 154 155 case IPPROTO_ESP: 156 if (xprth + 4 < skb->data || 157 pskb_may_pull(skb, xprth + 4 - skb->data)) { 158 __be32 *ehdr; 159 160 xprth = skb_network_header(skb) + iph->ihl * 4; 161 ehdr = (__be32 *)xprth; 162 163 fl4->fl4_ipsec_spi = ehdr[0]; 164 } 165 break; 166 167 case IPPROTO_AH: 168 if (xprth + 8 < skb->data || 169 pskb_may_pull(skb, xprth + 8 - skb->data)) { 170 __be32 *ah_hdr; 171 172 xprth = skb_network_header(skb) + iph->ihl * 4; 173 ah_hdr = (__be32 *)xprth; 174 175 fl4->fl4_ipsec_spi = ah_hdr[1]; 176 } 177 break; 178 179 case IPPROTO_COMP: 180 if (xprth + 4 < skb->data || 181 pskb_may_pull(skb, xprth + 4 - skb->data)) { 182 __be16 *ipcomp_hdr; 183 184 xprth = skb_network_header(skb) + iph->ihl * 4; 185 ipcomp_hdr = (__be16 *)xprth; 186 187 fl4->fl4_ipsec_spi = htonl(ntohs(ipcomp_hdr[1])); 188 } 189 break; 190 191 case IPPROTO_GRE: 192 if (xprth + 12 < skb->data || 193 pskb_may_pull(skb, xprth + 12 - skb->data)) { 194 __be16 *greflags; 195 __be32 *gre_hdr; 196 197 xprth = skb_network_header(skb) + iph->ihl * 4; 198 greflags = (__be16 *)xprth; 199 gre_hdr = (__be32 *)xprth; 200 201 if (greflags[0] & GRE_KEY) { 202 if (greflags[0] & GRE_CSUM) 203 gre_hdr++; 204 fl4->fl4_gre_key = gre_hdr[1]; 205 } 206 } 207 break; 208 209 default: 210 fl4->fl4_ipsec_spi = 0; 211 break; 212 } 213 } 214 fl4->flowi4_proto = iph->protocol; 215 fl4->daddr = reverse ? iph->saddr : iph->daddr; 216 fl4->saddr = reverse ? iph->daddr : iph->saddr; 217 fl4->flowi4_tos = iph->tos; 218 } 219 220 static void xfrm4_update_pmtu(struct dst_entry *dst, struct sock *sk, 221 struct sk_buff *skb, u32 mtu) 222 { 223 struct xfrm_dst *xdst = (struct xfrm_dst *)dst; 224 struct dst_entry *path = xdst->route; 225 226 path->ops->update_pmtu(path, sk, skb, mtu); 227 } 228 229 static void xfrm4_redirect(struct dst_entry *dst, struct sock *sk, 230 struct sk_buff *skb) 231 { 232 struct xfrm_dst *xdst = (struct xfrm_dst *)dst; 233 struct dst_entry *path = xdst->route; 234 235 path->ops->redirect(path, sk, skb); 236 } 237 238 static void xfrm4_dst_destroy(struct dst_entry *dst) 239 { 240 struct xfrm_dst *xdst = (struct xfrm_dst *)dst; 241 242 dst_destroy_metrics_generic(dst); 243 244 xfrm_dst_destroy(xdst); 245 } 246 247 static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 248 int unregister) 249 { 250 if (!unregister) 251 return; 252 253 xfrm_dst_ifdown(dst, dev); 254 } 255 256 static struct dst_ops xfrm4_dst_ops_template = { 257 .family = AF_INET, 258 .update_pmtu = xfrm4_update_pmtu, 259 .redirect = xfrm4_redirect, 260 .cow_metrics = dst_cow_metrics_generic, 261 .destroy = xfrm4_dst_destroy, 262 .ifdown = xfrm4_dst_ifdown, 263 .local_out = __ip_local_out, 264 .gc_thresh = 32768, 265 }; 266 267 static const struct xfrm_policy_afinfo xfrm4_policy_afinfo = { 268 .dst_ops = &xfrm4_dst_ops_template, 269 .dst_lookup = xfrm4_dst_lookup, 270 .get_saddr = xfrm4_get_saddr, 271 .decode_session = _decode_session4, 272 .get_tos = xfrm4_get_tos, 273 .init_path = xfrm4_init_path, 274 .fill_dst = xfrm4_fill_dst, 275 .blackhole_route = ipv4_blackhole_route, 276 }; 277 278 #ifdef CONFIG_SYSCTL 279 static struct ctl_table xfrm4_policy_table[] = { 280 { 281 .procname = "xfrm4_gc_thresh", 282 .data = &init_net.xfrm.xfrm4_dst_ops.gc_thresh, 283 .maxlen = sizeof(int), 284 .mode = 0644, 285 .proc_handler = proc_dointvec, 286 }, 287 { } 288 }; 289 290 static __net_init int xfrm4_net_sysctl_init(struct net *net) 291 { 292 struct ctl_table *table; 293 struct ctl_table_header *hdr; 294 295 table = xfrm4_policy_table; 296 if (!net_eq(net, &init_net)) { 297 table = kmemdup(table, sizeof(xfrm4_policy_table), GFP_KERNEL); 298 if (!table) 299 goto err_alloc; 300 301 table[0].data = &net->xfrm.xfrm4_dst_ops.gc_thresh; 302 } 303 304 hdr = register_net_sysctl(net, "net/ipv4", table); 305 if (!hdr) 306 goto err_reg; 307 308 net->ipv4.xfrm4_hdr = hdr; 309 return 0; 310 311 err_reg: 312 if (!net_eq(net, &init_net)) 313 kfree(table); 314 err_alloc: 315 return -ENOMEM; 316 } 317 318 static __net_exit void xfrm4_net_sysctl_exit(struct net *net) 319 { 320 struct ctl_table *table; 321 322 if (!net->ipv4.xfrm4_hdr) 323 return; 324 325 table = net->ipv4.xfrm4_hdr->ctl_table_arg; 326 unregister_net_sysctl_table(net->ipv4.xfrm4_hdr); 327 if (!net_eq(net, &init_net)) 328 kfree(table); 329 } 330 #else /* CONFIG_SYSCTL */ 331 static inline int xfrm4_net_sysctl_init(struct net *net) 332 { 333 return 0; 334 } 335 336 static inline void xfrm4_net_sysctl_exit(struct net *net) 337 { 338 } 339 #endif 340 341 static int __net_init xfrm4_net_init(struct net *net) 342 { 343 int ret; 344 345 memcpy(&net->xfrm.xfrm4_dst_ops, &xfrm4_dst_ops_template, 346 sizeof(xfrm4_dst_ops_template)); 347 ret = dst_entries_init(&net->xfrm.xfrm4_dst_ops); 348 if (ret) 349 return ret; 350 351 ret = xfrm4_net_sysctl_init(net); 352 if (ret) 353 dst_entries_destroy(&net->xfrm.xfrm4_dst_ops); 354 355 return ret; 356 } 357 358 static void __net_exit xfrm4_net_exit(struct net *net) 359 { 360 xfrm4_net_sysctl_exit(net); 361 dst_entries_destroy(&net->xfrm.xfrm4_dst_ops); 362 } 363 364 static struct pernet_operations __net_initdata xfrm4_net_ops = { 365 .init = xfrm4_net_init, 366 .exit = xfrm4_net_exit, 367 }; 368 369 static void __init xfrm4_policy_init(void) 370 { 371 xfrm_policy_register_afinfo(&xfrm4_policy_afinfo, AF_INET); 372 } 373 374 void __init xfrm4_init(void) 375 { 376 xfrm4_state_init(); 377 xfrm4_policy_init(); 378 xfrm4_protocol_init(); 379 register_pernet_subsys(&xfrm4_net_ops); 380 } 381 382