4 #include <linux/compiler.h>
7 #include <linux/list.h>
9 #include <linux/socket.h>
12 #include <linux/in6.h>
14 #include <linux/audit.h>
15 #include <linux/slab.h>
27 #ifdef CONFIG_XFRM_STATISTICS
31 #define XFRM_PROTO_ESP 50
32 #define XFRM_PROTO_AH 51
33 #define XFRM_PROTO_COMP 108
34 #define XFRM_PROTO_IPIP 4
35 #define XFRM_PROTO_IPV6 41
36 #define XFRM_PROTO_ROUTING IPPROTO_ROUTING
37 #define XFRM_PROTO_DSTOPTS IPPROTO_DSTOPTS
39 #define XFRM_ALIGN4(len) (((len) + 3) & ~3)
40 #define XFRM_ALIGN8(len) (((len) + 7) & ~7)
41 #define MODULE_ALIAS_XFRM_MODE(family, encap) \
42 MODULE_ALIAS("xfrm-mode-" __stringify(family) "-" __stringify(encap))
43 #define MODULE_ALIAS_XFRM_TYPE(family, proto) \
44 MODULE_ALIAS("xfrm-type-" __stringify(family) "-" __stringify(proto))
46 #ifdef CONFIG_XFRM_STATISTICS
47 #define XFRM_INC_STATS(net, field) SNMP_INC_STATS((net)->mib.xfrm_statistics, field)
48 #define XFRM_INC_STATS_BH(net, field) SNMP_INC_STATS_BH((net)->mib.xfrm_statistics, field)
49 #define XFRM_INC_STATS_USER(net, field) SNMP_INC_STATS_USER((net)-mib.xfrm_statistics, field)
51 #define XFRM_INC_STATS(net, field) ((void)(net))
52 #define XFRM_INC_STATS_BH(net, field) ((void)(net))
53 #define XFRM_INC_STATS_USER(net, field) ((void)(net))
243 #define XFRM_TIME_DEFER 1
244 #define XFRM_SOFT_EXPIRE 2
355 #define XFRM_TYPE_NON_FRAGMENT 1
356 #define XFRM_TYPE_REPLAY_PROT 2
357 #define XFRM_TYPE_LOCAL_COADDR 4
358 #define XFRM_TYPE_REMOTE_COADDR 8
365 const struct flowi *);
436 static inline int xfrm_af2proto(
unsigned int family)
491 #define XFRM_MAX_DEPTH 6
559 #define XFRM_KM_TIMEOUT 30
561 #define XFRM_REPLAY_SEQ 1
562 #define XFRM_REPLAY_OSEQ 2
563 #define XFRM_REPLAY_SEQ_MASK 3
565 #define XFRM_REPLAY_UPDATE XFRM_AE_CR
566 #define XFRM_REPLAY_TIMEOUT XFRM_AE_CE
569 #define XFRM_AE_ETIME 10
571 #define XFRM_AE_ETH_M 10
573 #define XFRM_AE_SEQT_SIZE 2
618 #define XFRM_SKB_CB(__skb) ((struct xfrm_skb_cb *)&((__skb)->cb[0]))
653 #define XFRM_MODE_SKB_CB(__skb) ((struct xfrm_mode_skb_cb *)&((__skb)->cb[0]))
669 #define XFRM_SPI_SKB_CB(__skb) ((struct xfrm_spi_skb_cb *)&((__skb)->cb[0]))
678 #ifdef CONFIG_AUDITSYSCALL
679 static inline struct audit_buffer *xfrm_audit_start(
const char *
op)
687 if (audit_buf ==
NULL)
693 static inline void xfrm_audit_helper_usrinfo(
kuid_t auid,
u32 ses,
u32 secid,
717 extern void xfrm_audit_state_replay_overflow(
struct xfrm_state *x,
719 extern void xfrm_audit_state_replay(
struct xfrm_state *x,
721 extern void xfrm_audit_state_notfound_simple(
struct sk_buff *
skb,
u16 family);
722 extern void xfrm_audit_state_notfound(
struct sk_buff *
skb,
u16 family,
724 extern void xfrm_audit_state_icvfail(
struct xfrm_state *x,
743 static inline void xfrm_audit_state_delete(
struct xfrm_state *x,
int result,
748 static inline void xfrm_audit_state_replay_overflow(
struct xfrm_state *x,
753 static inline void xfrm_audit_state_replay(
struct xfrm_state *x,
758 static inline void xfrm_audit_state_notfound_simple(
struct sk_buff *
skb,
763 static inline void xfrm_audit_state_notfound(
struct sk_buff *
skb,
u16 family,
768 static inline void xfrm_audit_state_icvfail(
struct xfrm_state *x,
774 static inline void xfrm_pol_hold(
struct xfrm_policy *policy)
782 static inline void xfrm_pol_put(
struct xfrm_policy *policy)
788 static inline void xfrm_pols_put(
struct xfrm_policy **pols,
int npols)
791 for (i = npols - 1; i >= 0; --
i)
792 xfrm_pol_put(pols[i]);
797 static inline void __xfrm_state_put(
struct xfrm_state *x)
802 static inline void xfrm_state_put(
struct xfrm_state *x)
808 static inline void xfrm_state_hold(
struct xfrm_state *x)
813 static inline bool addr_match(
const void *token1,
const void *token2,
821 pdw = prefixlen >> 5;
822 pbi = prefixlen & 0x1f;
825 if (
memcmp(a1, a2, pdw << 2))
831 mask =
htonl((0xffffffff) << (32 - pbi));
833 if ((a1[pdw] ^ a2[pdw]) & mask)
840 static inline bool addr4_match(
__be32 a1,
__be32 a2,
u8 prefixlen)
845 return !((a1 ^
a2) &
htonl(0xFFFFFFFFu << (32 - prefixlen)));
852 switch(fl->flowi_proto) {
857 port = uli->
ports.sport;
879 switch(fl->flowi_proto) {
884 port = uli->
ports.dport;
900 const struct flowi *fl,
901 unsigned short family);
903 #ifdef CONFIG_SECURITY_NETWORK_XFRM
909 return ((!s1 && !s2) ||
943 #ifdef CONFIG_XFRM_SUB_POLICY
956 static inline void xfrm_dst_destroy(
struct xfrm_dst *xdst)
961 xfrm_state_put(xdst->
u.
dst.xfrm);
962 #ifdef CONFIG_XFRM_SUB_POLICY
965 kfree(xdst->partner);
966 xdst->partner =
NULL;
979 static inline int secpath_exists(
struct sk_buff *
skb)
982 return skb->sp !=
NULL;
1011 secpath_put(skb->sp);
1021 return addr->
a4 == 0;
1023 return ipv6_addr_any((
struct in6_addr *)&addr->
a6);
1043 xfrm_state_addr_cmp(
const struct xfrm_tmpl *tmpl,
const struct xfrm_state *x,
unsigned short family)
1047 return __xfrm4_state_addr_cmp(tmpl, x);
1049 return __xfrm6_state_addr_cmp(tmpl, x);
1057 static inline int __xfrm_policy_check2(
struct sock *
sk,
int dir,
1059 unsigned int family,
int reverse)
1067 return (!net->xfrm.policy_count[dir] && !skb->sp) ||
1072 static inline int xfrm_policy_check(
struct sock *sk,
int dir,
struct sk_buff *skb,
unsigned short family)
1074 return __xfrm_policy_check2(sk, dir, skb, family, 0);
1077 static inline int xfrm4_policy_check(
struct sock *sk,
int dir,
struct sk_buff *skb)
1079 return xfrm_policy_check(sk, dir, skb,
AF_INET);
1082 static inline int xfrm6_policy_check(
struct sock *sk,
int dir,
struct sk_buff *skb)
1084 return xfrm_policy_check(sk, dir, skb,
AF_INET6);
1087 static inline int xfrm4_policy_check_reverse(
struct sock *sk,
int dir,
1090 return __xfrm_policy_check2(sk, dir, skb,
AF_INET, 1);
1093 static inline int xfrm6_policy_check_reverse(
struct sock *sk,
int dir,
1096 return __xfrm_policy_check2(sk, dir, skb,
AF_INET6, 1);
1100 unsigned int family,
int reverse);
1102 static inline int xfrm_decode_session(
struct sk_buff *skb,
struct flowi *fl,
1103 unsigned int family)
1108 static inline int xfrm_decode_session_reverse(
struct sk_buff *skb,
1110 unsigned int family)
1117 static inline int xfrm_route_forward(
struct sk_buff *skb,
unsigned short family)
1119 struct net *net = dev_net(skb->
dev);
1126 static inline int xfrm4_route_forward(
struct sk_buff *skb)
1128 return xfrm_route_forward(skb,
AF_INET);
1131 static inline int xfrm6_route_forward(
struct sk_buff *skb)
1133 return xfrm_route_forward(skb,
AF_INET6);
1138 static inline int xfrm_sk_clone_policy(
struct sock *sk)
1140 if (
unlikely(sk->sk_policy[0] || sk->sk_policy[1]))
1147 static inline void xfrm_sk_free_policy(
struct sock *sk)
1151 sk->sk_policy[0] =
NULL;
1155 sk->sk_policy[1] =
NULL;
1161 static inline void xfrm_sk_free_policy(
struct sock *sk) {}
1162 static inline int xfrm_sk_clone_policy(
struct sock *sk) {
return 0; }
1163 static inline int xfrm6_route_forward(
struct sk_buff *skb) {
return 1; }
1164 static inline int xfrm4_route_forward(
struct sk_buff *skb) {
return 1; }
1165 static inline int xfrm6_policy_check(
struct sock *sk,
int dir,
struct sk_buff *skb)
1169 static inline int xfrm4_policy_check(
struct sock *sk,
int dir,
struct sk_buff *skb)
1173 static inline int xfrm_policy_check(
struct sock *sk,
int dir,
struct sk_buff *skb,
unsigned short family)
1177 static inline int xfrm_decode_session_reverse(
struct sk_buff *skb,
1179 unsigned int family)
1183 static inline int xfrm4_policy_check_reverse(
struct sock *sk,
int dir,
1188 static inline int xfrm6_policy_check_reverse(
struct sock *sk,
int dir,
1220 void xfrm_flowi_addr_get(
const struct flowi *fl,
1222 unsigned short family)
1237 __xfrm4_state_addr_check(
const struct xfrm_state *x,
1240 if (daddr->
a4 == x->
id.daddr.a4 &&
1241 (saddr->
a4 == x->
props.saddr.a4 || !saddr->
a4 || !x->
props.saddr.a4))
1247 __xfrm6_state_addr_check(
const struct xfrm_state *x,
1252 ipv6_addr_any((
struct in6_addr *)saddr) ||
1259 xfrm_state_addr_check(
const struct xfrm_state *x,
1261 unsigned short family)
1265 return __xfrm4_state_addr_check(x, daddr, saddr);
1267 return __xfrm6_state_addr_check(x, daddr, saddr);
1273 xfrm_state_addr_flow_check(
const struct xfrm_state *x,
const struct flowi *fl,
1274 unsigned short family)
1278 return __xfrm4_state_addr_check(x,
1282 return __xfrm6_state_addr_check(x,
1289 static inline int xfrm_state_kern(
const struct xfrm_state *x)
1294 static inline int xfrm_id_proto_match(
u8 proto,
u8 userproto)
1296 return (!userproto || proto == userproto ||
1374 #ifdef CONFIG_XFRM_STATISTICS
1380 #ifdef CONFIG_SYSCTL
1381 extern void xfrm_sysctl_fini(
struct net *net);
1383 static inline void xfrm_sysctl_fini(
struct net *net)
1395 const struct flowi *fl,
1398 unsigned short family);
1402 unsigned short family,
1410 u8 proto,
unsigned short family);
1415 unsigned short family);
1416 #ifdef CONFIG_XFRM_SUB_POLICY
1418 int n,
unsigned short family);
1420 int n,
unsigned short family);
1423 int n,
unsigned short family)
1429 int n,
unsigned short family)
1545 unsigned short family);
1548 #ifdef CONFIG_XFRM_MIGRATE
1590 return ipv6_addr_cmp((
const struct in6_addr *)
a,
1595 static inline int xfrm_policy_id2dir(u32
index)
1601 static inline int xfrm_aevent_is_on(
struct net *net)
1615 static inline int xfrm_alg_len(
const struct xfrm_algo *alg)
1617 return sizeof(*alg) + ((alg->
alg_key_len + 7) / 8);
1620 static inline int xfrm_alg_auth_len(
const struct xfrm_algo_auth *alg)
1622 return sizeof(*alg) + ((alg->
alg_key_len + 7) / 8);
1627 return sizeof(*replay_esn) + replay_esn->
bmp_len *
sizeof(
__u32);
1630 #ifdef CONFIG_XFRM_MIGRATE
1631 static inline int xfrm_replay_clone(
struct xfrm_state *x,
1663 static inline void xfrm_states_put(
struct xfrm_state **states,
int n)
1666 for (i = 0; i <
n; i++)
1667 xfrm_state_put(*(states + i));
1670 static inline void xfrm_states_delete(
struct xfrm_state **states,
int n)
1673 for (i = 0; i <
n; i++)
1681 return skb->sp->xvec[skb->sp->
len - 1];
1685 static inline int xfrm_mark_get(
struct nlattr **attrs,
struct xfrm_mark *
m)
1695 static inline int xfrm_mark_put(
struct sk_buff *skb,
const struct xfrm_mark *m)