11da177e4SLinus Torvalds #ifndef _NET_XFRM_H 21da177e4SLinus Torvalds #define _NET_XFRM_H 31da177e4SLinus Torvalds 4aabc9761SHerbert Xu #include <linux/compiler.h> 51da177e4SLinus Torvalds #include <linux/xfrm.h> 61da177e4SLinus Torvalds #include <linux/spinlock.h> 71da177e4SLinus Torvalds #include <linux/list.h> 81da177e4SLinus Torvalds #include <linux/skbuff.h> 914c85021SArnaldo Carvalho de Melo #include <linux/socket.h> 101da177e4SLinus Torvalds #include <linux/pfkeyv2.h> 115794708fSMasahide NAKAMURA #include <linux/ipsec.h> 121da177e4SLinus Torvalds #include <linux/in6.h> 134a3e2f71SArjan van de Ven #include <linux/mutex.h> 14ab5f5e8bSJoy Latten #include <linux/audit.h> 155a0e3ad6STejun Heo #include <linux/slab.h> 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds #include <net/sock.h> 181da177e4SLinus Torvalds #include <net/dst.h> 19436a0a40SHerbert Xu #include <net/ip.h> 201da177e4SLinus Torvalds #include <net/route.h> 211da177e4SLinus Torvalds #include <net/ipv6.h> 221da177e4SLinus Torvalds #include <net/ip6_fib.h> 23fe1a5f03STimo Teräs #include <net/flow.h> 249e0d57fdSYury Polyanskiy 259e0d57fdSYury Polyanskiy #include <linux/interrupt.h> 269e0d57fdSYury Polyanskiy 27558f82efSMasahide NAKAMURA #ifdef CONFIG_XFRM_STATISTICS 28558f82efSMasahide NAKAMURA #include <net/snmp.h> 29558f82efSMasahide NAKAMURA #endif 301da177e4SLinus Torvalds 31d3d6dd3aSMasahide NAKAMURA #define XFRM_PROTO_ESP 50 32d3d6dd3aSMasahide NAKAMURA #define XFRM_PROTO_AH 51 33d3d6dd3aSMasahide NAKAMURA #define XFRM_PROTO_COMP 108 34d3d6dd3aSMasahide NAKAMURA #define XFRM_PROTO_IPIP 4 35d3d6dd3aSMasahide NAKAMURA #define XFRM_PROTO_IPV6 41 36d3d6dd3aSMasahide NAKAMURA #define XFRM_PROTO_ROUTING IPPROTO_ROUTING 37d3d6dd3aSMasahide NAKAMURA #define XFRM_PROTO_DSTOPTS IPPROTO_DSTOPTS 38d3d6dd3aSMasahide NAKAMURA 39fa9921e4SNicolas Dichtel #define XFRM_ALIGN4(len) (((len) + 3) & ~3) 401da177e4SLinus Torvalds #define XFRM_ALIGN8(len) (((len) + 7) & ~7) 41b59f45d0SHerbert Xu #define MODULE_ALIAS_XFRM_MODE(family, encap) \ 42b59f45d0SHerbert Xu MODULE_ALIAS("xfrm-mode-" __stringify(family) "-" __stringify(encap)) 43d3d6dd3aSMasahide NAKAMURA #define MODULE_ALIAS_XFRM_TYPE(family, proto) \ 44d3d6dd3aSMasahide NAKAMURA MODULE_ALIAS("xfrm-type-" __stringify(family) "-" __stringify(proto)) 451da177e4SLinus Torvalds 46558f82efSMasahide NAKAMURA #ifdef CONFIG_XFRM_STATISTICS 4759c9940eSAlexey Dobriyan #define XFRM_INC_STATS(net, field) SNMP_INC_STATS((net)->mib.xfrm_statistics, field) 4859c9940eSAlexey Dobriyan #define XFRM_INC_STATS_BH(net, field) SNMP_INC_STATS_BH((net)->mib.xfrm_statistics, field) 4959c9940eSAlexey Dobriyan #define XFRM_INC_STATS_USER(net, field) SNMP_INC_STATS_USER((net)-mib.xfrm_statistics, field) 50558f82efSMasahide NAKAMURA #else 5159c9940eSAlexey Dobriyan #define XFRM_INC_STATS(net, field) ((void)(net)) 5259c9940eSAlexey Dobriyan #define XFRM_INC_STATS_BH(net, field) ((void)(net)) 5359c9940eSAlexey Dobriyan #define XFRM_INC_STATS_USER(net, field) ((void)(net)) 54558f82efSMasahide NAKAMURA #endif 55558f82efSMasahide NAKAMURA 564a3e2f71SArjan van de Ven extern struct mutex xfrm_cfg_mutex; 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds /* Organization of SPD aka "XFRM rules" 591da177e4SLinus Torvalds ------------------------------------ 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds Basic objects: 621da177e4SLinus Torvalds - policy rule, struct xfrm_policy (=SPD entry) 631da177e4SLinus Torvalds - bundle of transformations, struct dst_entry == struct xfrm_dst (=SA bundle) 641da177e4SLinus Torvalds - instance of a transformer, struct xfrm_state (=SA) 651da177e4SLinus Torvalds - template to clone xfrm_state, struct xfrm_tmpl 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds SPD is plain linear list of xfrm_policy rules, ordered by priority. 681da177e4SLinus Torvalds (To be compatible with existing pfkeyv2 implementations, 691da177e4SLinus Torvalds many rules with priority of 0x7fffffff are allowed to exist and 701da177e4SLinus Torvalds such rules are ordered in an unpredictable way, thanks to bsd folks.) 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds Lookup is plain linear search until the first match with selector. 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds If "action" is "block", then we prohibit the flow, otherwise: 751da177e4SLinus Torvalds if "xfrms_nr" is zero, the flow passes untransformed. Otherwise, 761da177e4SLinus Torvalds policy entry has list of up to XFRM_MAX_DEPTH transformations, 771da177e4SLinus Torvalds described by templates xfrm_tmpl. Each template is resolved 781da177e4SLinus Torvalds to a complete xfrm_state (see below) and we pack bundle of transformations 791da177e4SLinus Torvalds to a dst_entry returned to requestor. 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds dst -. xfrm .-> xfrm_state #1 821da177e4SLinus Torvalds |---. child .-> dst -. xfrm .-> xfrm_state #2 831da177e4SLinus Torvalds |---. child .-> dst -. xfrm .-> xfrm_state #3 841da177e4SLinus Torvalds |---. child .-> NULL 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds Bundles are cached at xrfm_policy struct (field ->bundles). 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds Resolution of xrfm_tmpl 901da177e4SLinus Torvalds ----------------------- 911da177e4SLinus Torvalds Template contains: 921da177e4SLinus Torvalds 1. ->mode Mode: transport or tunnel 931da177e4SLinus Torvalds 2. ->id.proto Protocol: AH/ESP/IPCOMP 941da177e4SLinus Torvalds 3. ->id.daddr Remote tunnel endpoint, ignored for transport mode. 951da177e4SLinus Torvalds Q: allow to resolve security gateway? 961da177e4SLinus Torvalds 4. ->id.spi If not zero, static SPI. 971da177e4SLinus Torvalds 5. ->saddr Local tunnel endpoint, ignored for transport mode. 981da177e4SLinus Torvalds 6. ->algos List of allowed algos. Plain bitmask now. 991da177e4SLinus Torvalds Q: ealgos, aalgos, calgos. What a mess... 1001da177e4SLinus Torvalds 7. ->share Sharing mode. 1011da177e4SLinus Torvalds Q: how to implement private sharing mode? To add struct sock* to 1021da177e4SLinus Torvalds flow id? 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds Having this template we search through SAD searching for entries 1051da177e4SLinus Torvalds with appropriate mode/proto/algo, permitted by selector. 1061da177e4SLinus Torvalds If no appropriate entry found, it is requested from key manager. 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds PROBLEMS: 1091da177e4SLinus Torvalds Q: How to find all the bundles referring to a physical path for 1101da177e4SLinus Torvalds PMTU discovery? Seems, dst should contain list of all parents... 1111da177e4SLinus Torvalds and enter to infinite locking hierarchy disaster. 1121da177e4SLinus Torvalds No! It is easier, we will not search for them, let them find us. 1131da177e4SLinus Torvalds We add genid to each dst plus pointer to genid of raw IP route, 1141da177e4SLinus Torvalds pmtu disc will update pmtu on raw IP route and increase its genid. 1151da177e4SLinus Torvalds dst_check() will see this for top level and trigger resyncing 1161da177e4SLinus Torvalds metrics. Plus, it will be made via sk->sk_dst_cache. Solved. 1171da177e4SLinus Torvalds */ 1181da177e4SLinus Torvalds 11912a169e7SHerbert Xu struct xfrm_state_walk { 12012a169e7SHerbert Xu struct list_head all; 12112a169e7SHerbert Xu u8 state; 12212a169e7SHerbert Xu union { 12312a169e7SHerbert Xu u8 dying; 12412a169e7SHerbert Xu u8 proto; 12512a169e7SHerbert Xu }; 12612a169e7SHerbert Xu u32 seq; 12712a169e7SHerbert Xu }; 12812a169e7SHerbert Xu 1291da177e4SLinus Torvalds /* Full description of state of transformer. */ 130fd2c3ef7SEric Dumazet struct xfrm_state { 131673c09beSAlexey Dobriyan #ifdef CONFIG_NET_NS 132673c09beSAlexey Dobriyan struct net *xs_net; 133673c09beSAlexey Dobriyan #endif 134abb81c4fSHerbert Xu union { 13512a169e7SHerbert Xu struct hlist_node gclist; 1368f126e37SDavid S. Miller struct hlist_node bydst; 137abb81c4fSHerbert Xu }; 1388f126e37SDavid S. Miller struct hlist_node bysrc; 1398f126e37SDavid S. Miller struct hlist_node byspi; 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds atomic_t refcnt; 1421da177e4SLinus Torvalds spinlock_t lock; 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds struct xfrm_id id; 1451da177e4SLinus Torvalds struct xfrm_selector sel; 146bf825f81SJamal Hadi Salim struct xfrm_mark mark; 14735d2856bSMartin Willi u32 tfcpad; 1481da177e4SLinus Torvalds 1499d4a706dSDavid S. Miller u32 genid; 1509d4a706dSDavid S. Miller 15112a169e7SHerbert Xu /* Key manager bits */ 15212a169e7SHerbert Xu struct xfrm_state_walk km; 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds /* Parameters of this state. */ 1551da177e4SLinus Torvalds struct { 1561da177e4SLinus Torvalds u32 reqid; 1571da177e4SLinus Torvalds u8 mode; 1581da177e4SLinus Torvalds u8 replay_window; 1591da177e4SLinus Torvalds u8 aalgo, ealgo, calgo; 1601da177e4SLinus Torvalds u8 flags; 1611da177e4SLinus Torvalds u16 family; 1621da177e4SLinus Torvalds xfrm_address_t saddr; 1631da177e4SLinus Torvalds int header_len; 1641da177e4SLinus Torvalds int trailer_len; 1651da177e4SLinus Torvalds } props; 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds struct xfrm_lifetime_cfg lft; 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds /* Data for transformer */ 1704447bb33SMartin Willi struct xfrm_algo_auth *aalg; 1711da177e4SLinus Torvalds struct xfrm_algo *ealg; 1721da177e4SLinus Torvalds struct xfrm_algo *calg; 1731a6509d9SHerbert Xu struct xfrm_algo_aead *aead; 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds /* Data for encapsulator */ 1761da177e4SLinus Torvalds struct xfrm_encap_tmpl *encap; 1771da177e4SLinus Torvalds 178060f02a3SNoriaki TAKAMIYA /* Data for care-of address */ 179060f02a3SNoriaki TAKAMIYA xfrm_address_t *coaddr; 180060f02a3SNoriaki TAKAMIYA 1811da177e4SLinus Torvalds /* IPComp needs an IPIP tunnel for handling uncompressed packets */ 1821da177e4SLinus Torvalds struct xfrm_state *tunnel; 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds /* If a tunnel, number of users + 1 */ 1851da177e4SLinus Torvalds atomic_t tunnel_users; 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds /* State for replay detection */ 1881da177e4SLinus Torvalds struct xfrm_replay_state replay; 1891da177e4SLinus Torvalds 190f8cd5488SJamal Hadi Salim /* Replay detection state at the time we sent the last notification */ 191f8cd5488SJamal Hadi Salim struct xfrm_replay_state preplay; 192f8cd5488SJamal Hadi Salim 1932717096aSJamal Hadi Salim /* internal flag that only holds state for delayed aevent at the 1942717096aSJamal Hadi Salim * moment 1952717096aSJamal Hadi Salim */ 1962717096aSJamal Hadi Salim u32 xflags; 1972717096aSJamal Hadi Salim 198f8cd5488SJamal Hadi Salim /* Replay detection notification settings */ 199f8cd5488SJamal Hadi Salim u32 replay_maxage; 200f8cd5488SJamal Hadi Salim u32 replay_maxdiff; 201f8cd5488SJamal Hadi Salim 202f8cd5488SJamal Hadi Salim /* Replay detection notification timer */ 203f8cd5488SJamal Hadi Salim struct timer_list rtimer; 204f8cd5488SJamal Hadi Salim 2051da177e4SLinus Torvalds /* Statistics */ 2061da177e4SLinus Torvalds struct xfrm_stats stats; 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds struct xfrm_lifetime_cur curlft; 2099e0d57fdSYury Polyanskiy struct tasklet_hrtimer mtimer; 2101da177e4SLinus Torvalds 2119afaca05SMasahide NAKAMURA /* Last used time */ 212d26f3984SHerbert Xu unsigned long lastused; 2139afaca05SMasahide NAKAMURA 2141da177e4SLinus Torvalds /* Reference to data common to all the instances of this 2151da177e4SLinus Torvalds * transformer. */ 216533cb5b0SEric Dumazet const struct xfrm_type *type; 21713996378SHerbert Xu struct xfrm_mode *inner_mode; 218df9dcb45SKazunori MIYAZAWA struct xfrm_mode *inner_mode_iaf; 21913996378SHerbert Xu struct xfrm_mode *outer_mode; 2201da177e4SLinus Torvalds 221df71837dSTrent Jaeger /* Security context */ 222df71837dSTrent Jaeger struct xfrm_sec_ctx *security; 223df71837dSTrent Jaeger 2241da177e4SLinus Torvalds /* Private data of this transformer, format is opaque, 2251da177e4SLinus Torvalds * interpreted by xfrm_type methods. */ 2261da177e4SLinus Torvalds void *data; 2271da177e4SLinus Torvalds }; 2281da177e4SLinus Torvalds 229673c09beSAlexey Dobriyan static inline struct net *xs_net(struct xfrm_state *x) 230673c09beSAlexey Dobriyan { 231673c09beSAlexey Dobriyan return read_pnet(&x->xs_net); 232673c09beSAlexey Dobriyan } 233673c09beSAlexey Dobriyan 2342717096aSJamal Hadi Salim /* xflags - make enum if more show up */ 2352717096aSJamal Hadi Salim #define XFRM_TIME_DEFER 1 2362717096aSJamal Hadi Salim 2371da177e4SLinus Torvalds enum { 2381da177e4SLinus Torvalds XFRM_STATE_VOID, 2391da177e4SLinus Torvalds XFRM_STATE_ACQ, 2401da177e4SLinus Torvalds XFRM_STATE_VALID, 2411da177e4SLinus Torvalds XFRM_STATE_ERROR, 2421da177e4SLinus Torvalds XFRM_STATE_EXPIRED, 2431da177e4SLinus Torvalds XFRM_STATE_DEAD 2441da177e4SLinus Torvalds }; 2451da177e4SLinus Torvalds 24626b15dadSJamal Hadi Salim /* callback structure passed from either netlink or pfkey */ 247fd2c3ef7SEric Dumazet struct km_event { 248bf08867fSHerbert Xu union { 249bf08867fSHerbert Xu u32 hard; 250bf08867fSHerbert Xu u32 proto; 251bf08867fSHerbert Xu u32 byid; 252f8cd5488SJamal Hadi Salim u32 aevent; 253f7b6983fSMasahide NAKAMURA u32 type; 254bf08867fSHerbert Xu } data; 255bf08867fSHerbert Xu 25626b15dadSJamal Hadi Salim u32 seq; 25726b15dadSJamal Hadi Salim u32 pid; 25826b15dadSJamal Hadi Salim u32 event; 2597067802eSAlexey Dobriyan struct net *net; 26026b15dadSJamal Hadi Salim }; 26126b15dadSJamal Hadi Salim 26225ee3286SHerbert Xu struct net_device; 2631da177e4SLinus Torvalds struct xfrm_type; 2641da177e4SLinus Torvalds struct xfrm_dst; 2651da177e4SLinus Torvalds struct xfrm_policy_afinfo { 2661da177e4SLinus Torvalds unsigned short family; 2671da177e4SLinus Torvalds struct dst_ops *dst_ops; 268ddcfd796SAlexey Dobriyan void (*garbage_collect)(struct net *net); 269c5b3cf46SAlexey Dobriyan struct dst_entry *(*dst_lookup)(struct net *net, int tos, 270c5b3cf46SAlexey Dobriyan xfrm_address_t *saddr, 27166cdb3caSHerbert Xu xfrm_address_t *daddr); 272fbda33b2SAlexey Dobriyan int (*get_saddr)(struct net *net, xfrm_address_t *saddr, xfrm_address_t *daddr); 2731da177e4SLinus Torvalds void (*decode_session)(struct sk_buff *skb, 274d5422efeSHerbert Xu struct flowi *fl, 275d5422efeSHerbert Xu int reverse); 27605d84025SDavid S. Miller int (*get_tos)(const struct flowi *fl); 277a1b05140SMasahide NAKAMURA int (*init_path)(struct xfrm_dst *path, 278a1b05140SMasahide NAKAMURA struct dst_entry *dst, 279a1b05140SMasahide NAKAMURA int nfheader_len); 28025ee3286SHerbert Xu int (*fill_dst)(struct xfrm_dst *xdst, 28187c1e12bSHerbert Xu struct net_device *dev, 28287c1e12bSHerbert Xu struct flowi *fl); 2831da177e4SLinus Torvalds }; 2841da177e4SLinus Torvalds 2851da177e4SLinus Torvalds extern int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo); 2861da177e4SLinus Torvalds extern int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo); 28726b15dadSJamal Hadi Salim extern void km_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c); 28826b15dadSJamal Hadi Salim extern void km_state_notify(struct xfrm_state *x, struct km_event *c); 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds struct xfrm_tmpl; 291980ebd25SJamal Hadi Salim extern int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol); 29253bc6b4dSJamal Hadi Salim extern void km_state_expired(struct xfrm_state *x, int hard, u32 pid); 29353bc6b4dSJamal Hadi Salim extern int __xfrm_state_delete(struct xfrm_state *x); 29453bc6b4dSJamal Hadi Salim 2951da177e4SLinus Torvalds struct xfrm_state_afinfo { 29617c2a42aSHerbert Xu unsigned int family; 29736cf9acfSHerbert Xu unsigned int proto; 2988e3d716cSAl Viro __be16 eth_proto; 29917c2a42aSHerbert Xu struct module *owner; 300533cb5b0SEric Dumazet const struct xfrm_type *type_map[IPPROTO_MAX]; 301aa5d62ccSHerbert Xu struct xfrm_mode *mode_map[XFRM_MODE_MAX]; 302d094cd83SHerbert Xu int (*init_flags)(struct xfrm_state *x); 3038444cf71SThomas Egerer void (*init_tempsel)(struct xfrm_selector *sel, struct flowi *fl); 3048444cf71SThomas Egerer void (*init_temprop)(struct xfrm_state *x, struct xfrm_tmpl *tmpl, 3051da177e4SLinus Torvalds xfrm_address_t *daddr, xfrm_address_t *saddr); 30641a49cc3SMasahide NAKAMURA int (*tmpl_sort)(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n); 30741a49cc3SMasahide NAKAMURA int (*state_sort)(struct xfrm_state **dst, struct xfrm_state **src, int n); 308cdca7265SMiika Komu int (*output)(struct sk_buff *skb); 309227620e2SHerbert Xu int (*extract_input)(struct xfrm_state *x, 310227620e2SHerbert Xu struct sk_buff *skb); 31136cf9acfSHerbert Xu int (*extract_output)(struct xfrm_state *x, 31236cf9acfSHerbert Xu struct sk_buff *skb); 313716062fdSHerbert Xu int (*transport_finish)(struct sk_buff *skb, 314716062fdSHerbert Xu int async); 3151da177e4SLinus Torvalds }; 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds extern int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo); 3181da177e4SLinus Torvalds extern int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo); 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds extern void xfrm_state_delete_tunnel(struct xfrm_state *x); 3211da177e4SLinus Torvalds 322fd2c3ef7SEric Dumazet struct xfrm_type { 3231da177e4SLinus Torvalds char *description; 3241da177e4SLinus Torvalds struct module *owner; 325a6337463Sjamal u8 proto; 326a6337463Sjamal u8 flags; 3271b5c2299SMasahide NAKAMURA #define XFRM_TYPE_NON_FRAGMENT 1 328436a0a40SHerbert Xu #define XFRM_TYPE_REPLAY_PROT 2 329f04e7e8dSHerbert Xu #define XFRM_TYPE_LOCAL_COADDR 4 330f04e7e8dSHerbert Xu #define XFRM_TYPE_REMOTE_COADDR 8 3311da177e4SLinus Torvalds 33272cb6962SHerbert Xu int (*init_state)(struct xfrm_state *x); 3331da177e4SLinus Torvalds void (*destructor)(struct xfrm_state *); 334e695633eSHerbert Xu int (*input)(struct xfrm_state *, struct sk_buff *skb); 3351da177e4SLinus Torvalds int (*output)(struct xfrm_state *, struct sk_buff *pskb); 336df0ba92aSMasahide NAKAMURA int (*reject)(struct xfrm_state *, struct sk_buff *, struct flowi *); 337aee5adb4SMasahide NAKAMURA int (*hdr_offset)(struct xfrm_state *, struct sk_buff *, u8 **); 3381da177e4SLinus Torvalds /* Estimate maximal size of result of transformation of a dgram */ 339c5c25238SPatrick McHardy u32 (*get_mtu)(struct xfrm_state *, int size); 3401da177e4SLinus Torvalds }; 3411da177e4SLinus Torvalds 342533cb5b0SEric Dumazet extern int xfrm_register_type(const struct xfrm_type *type, unsigned short family); 343533cb5b0SEric Dumazet extern int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family); 3441da177e4SLinus Torvalds 345b59f45d0SHerbert Xu struct xfrm_mode { 346227620e2SHerbert Xu /* 347227620e2SHerbert Xu * Remove encapsulation header. 348227620e2SHerbert Xu * 349227620e2SHerbert Xu * The IP header will be moved over the top of the encapsulation 350227620e2SHerbert Xu * header. 351227620e2SHerbert Xu * 352227620e2SHerbert Xu * On entry, the transport header shall point to where the IP header 353227620e2SHerbert Xu * should be and the network header shall be set to where the IP 354227620e2SHerbert Xu * header currently is. skb->data shall point to the start of the 355227620e2SHerbert Xu * payload. 356227620e2SHerbert Xu */ 357227620e2SHerbert Xu int (*input2)(struct xfrm_state *x, struct sk_buff *skb); 358227620e2SHerbert Xu 359227620e2SHerbert Xu /* 360227620e2SHerbert Xu * This is the actual input entry point. 361227620e2SHerbert Xu * 362227620e2SHerbert Xu * For transport mode and equivalent this would be identical to 363227620e2SHerbert Xu * input2 (which does not need to be set). While tunnel mode 364227620e2SHerbert Xu * and equivalent would set this to the tunnel encapsulation function 365227620e2SHerbert Xu * xfrm4_prepare_input that would in turn call input2. 366227620e2SHerbert Xu */ 367b59f45d0SHerbert Xu int (*input)(struct xfrm_state *x, struct sk_buff *skb); 36837fedd3aSHerbert Xu 36937fedd3aSHerbert Xu /* 37037fedd3aSHerbert Xu * Add encapsulation header. 37137fedd3aSHerbert Xu * 37237fedd3aSHerbert Xu * On exit, the transport header will be set to the start of the 37337fedd3aSHerbert Xu * encapsulation header to be filled in by x->type->output and 37437fedd3aSHerbert Xu * the mac header will be set to the nextheader (protocol for 37537fedd3aSHerbert Xu * IPv4) field of the extension header directly preceding the 37637fedd3aSHerbert Xu * encapsulation header, or in its absence, that of the top IP 37737fedd3aSHerbert Xu * header. The value of the network header will always point 37837fedd3aSHerbert Xu * to the top IP header while skb->data will point to the payload. 37937fedd3aSHerbert Xu */ 38036cf9acfSHerbert Xu int (*output2)(struct xfrm_state *x,struct sk_buff *skb); 38136cf9acfSHerbert Xu 38236cf9acfSHerbert Xu /* 38336cf9acfSHerbert Xu * This is the actual output entry point. 38436cf9acfSHerbert Xu * 38536cf9acfSHerbert Xu * For transport mode and equivalent this would be identical to 38636cf9acfSHerbert Xu * output2 (which does not need to be set). While tunnel mode 38736cf9acfSHerbert Xu * and equivalent would set this to a tunnel encapsulation function 38836cf9acfSHerbert Xu * (xfrm4_prepare_output or xfrm6_prepare_output) that would in turn 38936cf9acfSHerbert Xu * call output2. 39036cf9acfSHerbert Xu */ 391eb878e84SJamal Hadi Salim int (*output)(struct xfrm_state *x, struct sk_buff *skb); 392b59f45d0SHerbert Xu 39317c2a42aSHerbert Xu struct xfrm_state_afinfo *afinfo; 394b59f45d0SHerbert Xu struct module *owner; 395b59f45d0SHerbert Xu unsigned int encap; 3961bfcb10fSHerbert Xu int flags; 3971bfcb10fSHerbert Xu }; 3981bfcb10fSHerbert Xu 3991bfcb10fSHerbert Xu /* Flags for xfrm_mode. */ 4001bfcb10fSHerbert Xu enum { 4011bfcb10fSHerbert Xu XFRM_MODE_FLAG_TUNNEL = 1, 402b59f45d0SHerbert Xu }; 403b59f45d0SHerbert Xu 404b59f45d0SHerbert Xu extern int xfrm_register_mode(struct xfrm_mode *mode, int family); 405b59f45d0SHerbert Xu extern int xfrm_unregister_mode(struct xfrm_mode *mode, int family); 406b59f45d0SHerbert Xu 407df9dcb45SKazunori MIYAZAWA static inline int xfrm_af2proto(unsigned int family) 408df9dcb45SKazunori MIYAZAWA { 409df9dcb45SKazunori MIYAZAWA switch(family) { 410df9dcb45SKazunori MIYAZAWA case AF_INET: 411df9dcb45SKazunori MIYAZAWA return IPPROTO_IPIP; 412df9dcb45SKazunori MIYAZAWA case AF_INET6: 413df9dcb45SKazunori MIYAZAWA return IPPROTO_IPV6; 414df9dcb45SKazunori MIYAZAWA default: 415df9dcb45SKazunori MIYAZAWA return 0; 416df9dcb45SKazunori MIYAZAWA } 417df9dcb45SKazunori MIYAZAWA } 418df9dcb45SKazunori MIYAZAWA 419df9dcb45SKazunori MIYAZAWA static inline struct xfrm_mode *xfrm_ip2inner_mode(struct xfrm_state *x, int ipproto) 420df9dcb45SKazunori MIYAZAWA { 421df9dcb45SKazunori MIYAZAWA if ((ipproto == IPPROTO_IPIP && x->props.family == AF_INET) || 422df9dcb45SKazunori MIYAZAWA (ipproto == IPPROTO_IPV6 && x->props.family == AF_INET6)) 423df9dcb45SKazunori MIYAZAWA return x->inner_mode; 424df9dcb45SKazunori MIYAZAWA else 425df9dcb45SKazunori MIYAZAWA return x->inner_mode_iaf; 426df9dcb45SKazunori MIYAZAWA } 427df9dcb45SKazunori MIYAZAWA 428fd2c3ef7SEric Dumazet struct xfrm_tmpl { 4291da177e4SLinus Torvalds /* id in template is interpreted as: 4301da177e4SLinus Torvalds * daddr - destination of tunnel, may be zero for transport mode. 4311da177e4SLinus Torvalds * spi - zero to acquire spi. Not zero if spi is static, then 4321da177e4SLinus Torvalds * daddr must be fixed too. 4331da177e4SLinus Torvalds * proto - AH/ESP/IPCOMP 4341da177e4SLinus Torvalds */ 4351da177e4SLinus Torvalds struct xfrm_id id; 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds /* Source address of tunnel. Ignored, if it is not a tunnel. */ 4381da177e4SLinus Torvalds xfrm_address_t saddr; 4391da177e4SLinus Torvalds 44076b3f055SMiika Komu unsigned short encap_family; 44176b3f055SMiika Komu 442a6337463Sjamal u32 reqid; 4431da177e4SLinus Torvalds 4447e49e6deSMasahide NAKAMURA /* Mode: transport, tunnel etc. */ 445a6337463Sjamal u8 mode; 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds /* Sharing mode: unique, this session only, this user only etc. */ 448a6337463Sjamal u8 share; 4491da177e4SLinus Torvalds 4501da177e4SLinus Torvalds /* May skip this transfomration if no SA is found */ 451a6337463Sjamal u8 optional; 4521da177e4SLinus Torvalds 453c5d18e98SHerbert Xu /* Skip aalgos/ealgos/calgos checks. */ 454a6337463Sjamal u8 allalgs; 455c5d18e98SHerbert Xu 4561da177e4SLinus Torvalds /* Bit mask of algos allowed for acquisition */ 457a6337463Sjamal u32 aalgos; 458a6337463Sjamal u32 ealgos; 459a6337463Sjamal u32 calgos; 4601da177e4SLinus Torvalds }; 4611da177e4SLinus Torvalds 462622dc828SMasahide NAKAMURA #define XFRM_MAX_DEPTH 6 4631da177e4SLinus Torvalds 46412a169e7SHerbert Xu struct xfrm_policy_walk_entry { 46512a169e7SHerbert Xu struct list_head all; 46612a169e7SHerbert Xu u8 dead; 46712a169e7SHerbert Xu }; 46812a169e7SHerbert Xu 46912a169e7SHerbert Xu struct xfrm_policy_walk { 47012a169e7SHerbert Xu struct xfrm_policy_walk_entry walk; 47112a169e7SHerbert Xu u8 type; 47212a169e7SHerbert Xu u32 seq; 47312a169e7SHerbert Xu }; 47412a169e7SHerbert Xu 475fd2c3ef7SEric Dumazet struct xfrm_policy { 4760331b1f3SAlexey Dobriyan #ifdef CONFIG_NET_NS 4770331b1f3SAlexey Dobriyan struct net *xp_net; 4780331b1f3SAlexey Dobriyan #endif 4792518c7c2SDavid S. Miller struct hlist_node bydst; 4802518c7c2SDavid S. Miller struct hlist_node byidx; 4811da177e4SLinus Torvalds 4821da177e4SLinus Torvalds /* This lock only affects elements except for entry. */ 4831da177e4SLinus Torvalds rwlock_t lock; 4841da177e4SLinus Torvalds atomic_t refcnt; 4851da177e4SLinus Torvalds struct timer_list timer; 4861da177e4SLinus Torvalds 487fe1a5f03STimo Teräs struct flow_cache_object flo; 48880c802f3STimo Teräs atomic_t genid; 4891da177e4SLinus Torvalds u32 priority; 4901da177e4SLinus Torvalds u32 index; 491bf825f81SJamal Hadi Salim struct xfrm_mark mark; 4921da177e4SLinus Torvalds struct xfrm_selector selector; 4931da177e4SLinus Torvalds struct xfrm_lifetime_cfg lft; 4941da177e4SLinus Torvalds struct xfrm_lifetime_cur curlft; 49512a169e7SHerbert Xu struct xfrm_policy_walk_entry walk; 49646ca5f5dSArnaldo Carvalho de Melo u8 type; 49746ca5f5dSArnaldo Carvalho de Melo u8 action; 49846ca5f5dSArnaldo Carvalho de Melo u8 flags; 49946ca5f5dSArnaldo Carvalho de Melo u8 xfrm_nr; 50012a169e7SHerbert Xu u16 family; 501df71837dSTrent Jaeger struct xfrm_sec_ctx *security; 5021da177e4SLinus Torvalds struct xfrm_tmpl xfrm_vec[XFRM_MAX_DEPTH]; 5031da177e4SLinus Torvalds }; 5041da177e4SLinus Torvalds 5050331b1f3SAlexey Dobriyan static inline struct net *xp_net(struct xfrm_policy *xp) 5060331b1f3SAlexey Dobriyan { 5070331b1f3SAlexey Dobriyan return read_pnet(&xp->xp_net); 5080331b1f3SAlexey Dobriyan } 5090331b1f3SAlexey Dobriyan 51013c1d189SArnaud Ebalard struct xfrm_kmaddress { 51113c1d189SArnaud Ebalard xfrm_address_t local; 51213c1d189SArnaud Ebalard xfrm_address_t remote; 51313c1d189SArnaud Ebalard u32 reserved; 51413c1d189SArnaud Ebalard u16 family; 51513c1d189SArnaud Ebalard }; 51613c1d189SArnaud Ebalard 51780c9abaaSShinta Sugimoto struct xfrm_migrate { 51880c9abaaSShinta Sugimoto xfrm_address_t old_daddr; 51980c9abaaSShinta Sugimoto xfrm_address_t old_saddr; 52080c9abaaSShinta Sugimoto xfrm_address_t new_daddr; 52180c9abaaSShinta Sugimoto xfrm_address_t new_saddr; 52280c9abaaSShinta Sugimoto u8 proto; 52380c9abaaSShinta Sugimoto u8 mode; 52480c9abaaSShinta Sugimoto u16 reserved; 52580c9abaaSShinta Sugimoto u32 reqid; 52680c9abaaSShinta Sugimoto u16 old_family; 52780c9abaaSShinta Sugimoto u16 new_family; 52880c9abaaSShinta Sugimoto }; 52980c9abaaSShinta Sugimoto 5301da177e4SLinus Torvalds #define XFRM_KM_TIMEOUT 30 531f8cd5488SJamal Hadi Salim /* which seqno */ 532f8cd5488SJamal Hadi Salim #define XFRM_REPLAY_SEQ 1 533f8cd5488SJamal Hadi Salim #define XFRM_REPLAY_OSEQ 2 534f8cd5488SJamal Hadi Salim #define XFRM_REPLAY_SEQ_MASK 3 535f8cd5488SJamal Hadi Salim /* what happened */ 536f8cd5488SJamal Hadi Salim #define XFRM_REPLAY_UPDATE XFRM_AE_CR 537f8cd5488SJamal Hadi Salim #define XFRM_REPLAY_TIMEOUT XFRM_AE_CE 538f8cd5488SJamal Hadi Salim 539f8cd5488SJamal Hadi Salim /* default aevent timeout in units of 100ms */ 540f8cd5488SJamal Hadi Salim #define XFRM_AE_ETIME 10 541f8cd5488SJamal Hadi Salim /* Async Event timer multiplier */ 542f8cd5488SJamal Hadi Salim #define XFRM_AE_ETH_M 10 543f8cd5488SJamal Hadi Salim /* default seq threshold size */ 544f8cd5488SJamal Hadi Salim #define XFRM_AE_SEQT_SIZE 2 5451da177e4SLinus Torvalds 546fd2c3ef7SEric Dumazet struct xfrm_mgr { 5471da177e4SLinus Torvalds struct list_head list; 5481da177e4SLinus Torvalds char *id; 54926b15dadSJamal Hadi Salim int (*notify)(struct xfrm_state *x, struct km_event *c); 5501da177e4SLinus Torvalds int (*acquire)(struct xfrm_state *x, struct xfrm_tmpl *, struct xfrm_policy *xp, int dir); 551cb969f07SVenkat Yekkirala struct xfrm_policy *(*compile_policy)(struct sock *sk, int opt, u8 *data, int len, int *dir); 5525d36b180SAl Viro int (*new_mapping)(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport); 55326b15dadSJamal Hadi Salim int (*notify_policy)(struct xfrm_policy *x, int dir, struct km_event *c); 554db983c11SAlexey Dobriyan int (*report)(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr); 55513c1d189SArnaud Ebalard int (*migrate)(struct xfrm_selector *sel, u8 dir, u8 type, struct xfrm_migrate *m, int num_bundles, struct xfrm_kmaddress *k); 5561da177e4SLinus Torvalds }; 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds extern int xfrm_register_km(struct xfrm_mgr *km); 5591da177e4SLinus Torvalds extern int xfrm_unregister_km(struct xfrm_mgr *km); 5601da177e4SLinus Torvalds 561436a0a40SHerbert Xu /* 562436a0a40SHerbert Xu * This structure is used for the duration where packets are being 563436a0a40SHerbert Xu * transformed by IPsec. As soon as the packet leaves IPsec the 564436a0a40SHerbert Xu * area beyond the generic IP part may be overwritten. 565436a0a40SHerbert Xu */ 566436a0a40SHerbert Xu struct xfrm_skb_cb { 567436a0a40SHerbert Xu union { 568436a0a40SHerbert Xu struct inet_skb_parm h4; 569436a0a40SHerbert Xu struct inet6_skb_parm h6; 570436a0a40SHerbert Xu } header; 571436a0a40SHerbert Xu 572436a0a40SHerbert Xu /* Sequence number for replay protection. */ 573b318e0e4SHerbert Xu union { 574b318e0e4SHerbert Xu u64 output; 575b318e0e4SHerbert Xu __be32 input; 576b318e0e4SHerbert Xu } seq; 577436a0a40SHerbert Xu }; 578436a0a40SHerbert Xu 579436a0a40SHerbert Xu #define XFRM_SKB_CB(__skb) ((struct xfrm_skb_cb *)&((__skb)->cb[0])) 580436a0a40SHerbert Xu 58136cf9acfSHerbert Xu /* 58236cf9acfSHerbert Xu * This structure is used by the afinfo prepare_input/prepare_output functions 58336cf9acfSHerbert Xu * to transmit header information to the mode input/output functions. 58436cf9acfSHerbert Xu */ 58536cf9acfSHerbert Xu struct xfrm_mode_skb_cb { 58636cf9acfSHerbert Xu union { 58736cf9acfSHerbert Xu struct inet_skb_parm h4; 58836cf9acfSHerbert Xu struct inet6_skb_parm h6; 58936cf9acfSHerbert Xu } header; 59036cf9acfSHerbert Xu 59136cf9acfSHerbert Xu /* Copied from header for IPv4, always set to zero and DF for IPv6. */ 59236cf9acfSHerbert Xu __be16 id; 59336cf9acfSHerbert Xu __be16 frag_off; 59436cf9acfSHerbert Xu 595732c8bd5SHerbert Xu /* IP header length (excluding options or extension headers). */ 596732c8bd5SHerbert Xu u8 ihl; 597732c8bd5SHerbert Xu 59836cf9acfSHerbert Xu /* TOS for IPv4, class for IPv6. */ 59936cf9acfSHerbert Xu u8 tos; 60036cf9acfSHerbert Xu 60136cf9acfSHerbert Xu /* TTL for IPv4, hop limitfor IPv6. */ 60236cf9acfSHerbert Xu u8 ttl; 60336cf9acfSHerbert Xu 60436cf9acfSHerbert Xu /* Protocol for IPv4, NH for IPv6. */ 60536cf9acfSHerbert Xu u8 protocol; 60636cf9acfSHerbert Xu 607732c8bd5SHerbert Xu /* Option length for IPv4, zero for IPv6. */ 608732c8bd5SHerbert Xu u8 optlen; 609732c8bd5SHerbert Xu 61036cf9acfSHerbert Xu /* Used by IPv6 only, zero for IPv4. */ 61136cf9acfSHerbert Xu u8 flow_lbl[3]; 61236cf9acfSHerbert Xu }; 61336cf9acfSHerbert Xu 61436cf9acfSHerbert Xu #define XFRM_MODE_SKB_CB(__skb) ((struct xfrm_mode_skb_cb *)&((__skb)->cb[0])) 61536cf9acfSHerbert Xu 616716062fdSHerbert Xu /* 617716062fdSHerbert Xu * This structure is used by the input processing to locate the SPI and 618716062fdSHerbert Xu * related information. 619716062fdSHerbert Xu */ 620716062fdSHerbert Xu struct xfrm_spi_skb_cb { 621716062fdSHerbert Xu union { 622716062fdSHerbert Xu struct inet_skb_parm h4; 623716062fdSHerbert Xu struct inet6_skb_parm h6; 624716062fdSHerbert Xu } header; 625716062fdSHerbert Xu 626716062fdSHerbert Xu unsigned int daddroff; 6272fcb45b6SHerbert Xu unsigned int family; 628716062fdSHerbert Xu }; 629716062fdSHerbert Xu 630716062fdSHerbert Xu #define XFRM_SPI_SKB_CB(__skb) ((struct xfrm_spi_skb_cb *)&((__skb)->cb[0])) 631716062fdSHerbert Xu 632161a09e7SJoy Latten /* Audit Information */ 633fd2c3ef7SEric Dumazet struct xfrm_audit { 634161a09e7SJoy Latten u32 secid; 6352532386fSEric Paris uid_t loginuid; 6362532386fSEric Paris u32 sessionid; 637161a09e7SJoy Latten }; 638c9204d9cSJoy Latten 639c9204d9cSJoy Latten #ifdef CONFIG_AUDITSYSCALL 640afeb14b4SPaul Moore static inline struct audit_buffer *xfrm_audit_start(const char *op) 641ab5f5e8bSJoy Latten { 642ab5f5e8bSJoy Latten struct audit_buffer *audit_buf = NULL; 643ab5f5e8bSJoy Latten 644afeb14b4SPaul Moore if (audit_enabled == 0) 645afeb14b4SPaul Moore return NULL; 646ab5f5e8bSJoy Latten audit_buf = audit_log_start(current->audit_context, GFP_ATOMIC, 647ab5f5e8bSJoy Latten AUDIT_MAC_IPSEC_EVENT); 648ab5f5e8bSJoy Latten if (audit_buf == NULL) 649ab5f5e8bSJoy Latten return NULL; 650afeb14b4SPaul Moore audit_log_format(audit_buf, "op=%s", op); 651afeb14b4SPaul Moore return audit_buf; 652afeb14b4SPaul Moore } 653afeb14b4SPaul Moore 6542532386fSEric Paris static inline void xfrm_audit_helper_usrinfo(uid_t auid, u32 ses, u32 secid, 655afeb14b4SPaul Moore struct audit_buffer *audit_buf) 656afeb14b4SPaul Moore { 657afeb14b4SPaul Moore char *secctx; 658afeb14b4SPaul Moore u32 secctx_len; 659ab5f5e8bSJoy Latten 6602532386fSEric Paris audit_log_format(audit_buf, " auid=%u ses=%u", auid, ses); 66168277accSPaul Moore if (secid != 0 && 66268277accSPaul Moore security_secid_to_secctx(secid, &secctx, &secctx_len) == 0) { 663ab5f5e8bSJoy Latten audit_log_format(audit_buf, " subj=%s", secctx); 664ab5f5e8bSJoy Latten security_release_secctx(secctx, secctx_len); 665ab5f5e8bSJoy Latten } else 666ab5f5e8bSJoy Latten audit_log_task_context(audit_buf); 667ab5f5e8bSJoy Latten } 668ab5f5e8bSJoy Latten 669ab5f5e8bSJoy Latten extern void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, 6702532386fSEric Paris u32 auid, u32 ses, u32 secid); 671ab5f5e8bSJoy Latten extern void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result, 6722532386fSEric Paris u32 auid, u32 ses, u32 secid); 673ab5f5e8bSJoy Latten extern void xfrm_audit_state_add(struct xfrm_state *x, int result, 6742532386fSEric Paris u32 auid, u32 ses, u32 secid); 675ab5f5e8bSJoy Latten extern void xfrm_audit_state_delete(struct xfrm_state *x, int result, 6762532386fSEric Paris u32 auid, u32 ses, u32 secid); 677afeb14b4SPaul Moore extern void xfrm_audit_state_replay_overflow(struct xfrm_state *x, 678afeb14b4SPaul Moore struct sk_buff *skb); 679afeb14b4SPaul Moore extern void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family); 680afeb14b4SPaul Moore extern void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family, 681afeb14b4SPaul Moore __be32 net_spi, __be32 net_seq); 682afeb14b4SPaul Moore extern void xfrm_audit_state_icvfail(struct xfrm_state *x, 683afeb14b4SPaul Moore struct sk_buff *skb, u8 proto); 684c9204d9cSJoy Latten #else 68541fef0eeSMarcin Slusarz 68641fef0eeSMarcin Slusarz static inline void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, 68741fef0eeSMarcin Slusarz u32 auid, u32 ses, u32 secid) 68841fef0eeSMarcin Slusarz { 68941fef0eeSMarcin Slusarz } 69041fef0eeSMarcin Slusarz 69141fef0eeSMarcin Slusarz static inline void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result, 69241fef0eeSMarcin Slusarz u32 auid, u32 ses, u32 secid) 69341fef0eeSMarcin Slusarz { 69441fef0eeSMarcin Slusarz } 69541fef0eeSMarcin Slusarz 69641fef0eeSMarcin Slusarz static inline void xfrm_audit_state_add(struct xfrm_state *x, int result, 69741fef0eeSMarcin Slusarz u32 auid, u32 ses, u32 secid) 69841fef0eeSMarcin Slusarz { 69941fef0eeSMarcin Slusarz } 70041fef0eeSMarcin Slusarz 70141fef0eeSMarcin Slusarz static inline void xfrm_audit_state_delete(struct xfrm_state *x, int result, 70241fef0eeSMarcin Slusarz u32 auid, u32 ses, u32 secid) 70341fef0eeSMarcin Slusarz { 70441fef0eeSMarcin Slusarz } 70541fef0eeSMarcin Slusarz 70641fef0eeSMarcin Slusarz static inline void xfrm_audit_state_replay_overflow(struct xfrm_state *x, 70741fef0eeSMarcin Slusarz struct sk_buff *skb) 70841fef0eeSMarcin Slusarz { 70941fef0eeSMarcin Slusarz } 71041fef0eeSMarcin Slusarz 71141fef0eeSMarcin Slusarz static inline void xfrm_audit_state_notfound_simple(struct sk_buff *skb, 71241fef0eeSMarcin Slusarz u16 family) 71341fef0eeSMarcin Slusarz { 71441fef0eeSMarcin Slusarz } 71541fef0eeSMarcin Slusarz 71641fef0eeSMarcin Slusarz static inline void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family, 71741fef0eeSMarcin Slusarz __be32 net_spi, __be32 net_seq) 71841fef0eeSMarcin Slusarz { 71941fef0eeSMarcin Slusarz } 72041fef0eeSMarcin Slusarz 72141fef0eeSMarcin Slusarz static inline void xfrm_audit_state_icvfail(struct xfrm_state *x, 72241fef0eeSMarcin Slusarz struct sk_buff *skb, u8 proto) 72341fef0eeSMarcin Slusarz { 72441fef0eeSMarcin Slusarz } 725c9204d9cSJoy Latten #endif /* CONFIG_AUDITSYSCALL */ 726161a09e7SJoy Latten 7271da177e4SLinus Torvalds static inline void xfrm_pol_hold(struct xfrm_policy *policy) 7281da177e4SLinus Torvalds { 7291da177e4SLinus Torvalds if (likely(policy != NULL)) 7301da177e4SLinus Torvalds atomic_inc(&policy->refcnt); 7311da177e4SLinus Torvalds } 7321da177e4SLinus Torvalds 73364c31b3fSWANG Cong extern void xfrm_policy_destroy(struct xfrm_policy *policy); 7341da177e4SLinus Torvalds 7351da177e4SLinus Torvalds static inline void xfrm_pol_put(struct xfrm_policy *policy) 7361da177e4SLinus Torvalds { 7371da177e4SLinus Torvalds if (atomic_dec_and_test(&policy->refcnt)) 73864c31b3fSWANG Cong xfrm_policy_destroy(policy); 7391da177e4SLinus Torvalds } 7401da177e4SLinus Torvalds 7414e81bb83SMasahide NAKAMURA static inline void xfrm_pols_put(struct xfrm_policy **pols, int npols) 7424e81bb83SMasahide NAKAMURA { 7434e81bb83SMasahide NAKAMURA int i; 7444e81bb83SMasahide NAKAMURA for (i = npols - 1; i >= 0; --i) 7454e81bb83SMasahide NAKAMURA xfrm_pol_put(pols[i]); 7464e81bb83SMasahide NAKAMURA } 7474e81bb83SMasahide NAKAMURA 7481da177e4SLinus Torvalds extern void __xfrm_state_destroy(struct xfrm_state *); 7491da177e4SLinus Torvalds 75021380b81SHerbert Xu static inline void __xfrm_state_put(struct xfrm_state *x) 75121380b81SHerbert Xu { 75221380b81SHerbert Xu atomic_dec(&x->refcnt); 75321380b81SHerbert Xu } 75421380b81SHerbert Xu 7551da177e4SLinus Torvalds static inline void xfrm_state_put(struct xfrm_state *x) 7561da177e4SLinus Torvalds { 7571da177e4SLinus Torvalds if (atomic_dec_and_test(&x->refcnt)) 7581da177e4SLinus Torvalds __xfrm_state_destroy(x); 7591da177e4SLinus Torvalds } 7601da177e4SLinus Torvalds 7611da177e4SLinus Torvalds static inline void xfrm_state_hold(struct xfrm_state *x) 7621da177e4SLinus Torvalds { 7631da177e4SLinus Torvalds atomic_inc(&x->refcnt); 7641da177e4SLinus Torvalds } 7651da177e4SLinus Torvalds 7661da177e4SLinus Torvalds static __inline__ int addr_match(void *token1, void *token2, int prefixlen) 7671da177e4SLinus Torvalds { 7685f19343fSAl Viro __be32 *a1 = token1; 7695f19343fSAl Viro __be32 *a2 = token2; 7701da177e4SLinus Torvalds int pdw; 7711da177e4SLinus Torvalds int pbi; 7721da177e4SLinus Torvalds 773a6337463Sjamal pdw = prefixlen >> 5; /* num of whole u32 in prefix */ 7741da177e4SLinus Torvalds pbi = prefixlen & 0x1f; /* num of bits in incomplete u32 in prefix */ 7751da177e4SLinus Torvalds 7761da177e4SLinus Torvalds if (pdw) 7771da177e4SLinus Torvalds if (memcmp(a1, a2, pdw << 2)) 7781da177e4SLinus Torvalds return 0; 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds if (pbi) { 7815f19343fSAl Viro __be32 mask; 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds mask = htonl((0xffffffff) << (32 - pbi)); 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds if ((a1[pdw] ^ a2[pdw]) & mask) 7861da177e4SLinus Torvalds return 0; 7871da177e4SLinus Torvalds } 7881da177e4SLinus Torvalds 7891da177e4SLinus Torvalds return 1; 7901da177e4SLinus Torvalds } 7911da177e4SLinus Torvalds 7921da177e4SLinus Torvalds static __inline__ 793e8a4e377SDavid S. Miller __be16 xfrm_flowi_sport(const struct flowi *fl) 7941da177e4SLinus Torvalds { 795f9d07e41SAl Viro __be16 port; 7961da177e4SLinus Torvalds switch(fl->proto) { 7971da177e4SLinus Torvalds case IPPROTO_TCP: 7981da177e4SLinus Torvalds case IPPROTO_UDP: 799ba4e58ecSGerrit Renker case IPPROTO_UDPLITE: 8001da177e4SLinus Torvalds case IPPROTO_SCTP: 8011da177e4SLinus Torvalds port = fl->fl_ip_sport; 8021da177e4SLinus Torvalds break; 8031da177e4SLinus Torvalds case IPPROTO_ICMP: 8041da177e4SLinus Torvalds case IPPROTO_ICMPV6: 8051da177e4SLinus Torvalds port = htons(fl->fl_icmp_type); 8061da177e4SLinus Torvalds break; 8072ce4272aSMasahide NAKAMURA case IPPROTO_MH: 8082ce4272aSMasahide NAKAMURA port = htons(fl->fl_mh_type); 8092ce4272aSMasahide NAKAMURA break; 810cc9ff19dSTimo Teräs case IPPROTO_GRE: 811aa285b17STimo Teräs port = htons(ntohl(fl->fl_gre_key) >> 16); 812cc9ff19dSTimo Teräs break; 8131da177e4SLinus Torvalds default: 8141da177e4SLinus Torvalds port = 0; /*XXX*/ 8151da177e4SLinus Torvalds } 8161da177e4SLinus Torvalds return port; 8171da177e4SLinus Torvalds } 8181da177e4SLinus Torvalds 8191da177e4SLinus Torvalds static __inline__ 820e8a4e377SDavid S. Miller __be16 xfrm_flowi_dport(const struct flowi *fl) 8211da177e4SLinus Torvalds { 822f9d07e41SAl Viro __be16 port; 8231da177e4SLinus Torvalds switch(fl->proto) { 8241da177e4SLinus Torvalds case IPPROTO_TCP: 8251da177e4SLinus Torvalds case IPPROTO_UDP: 826ba4e58ecSGerrit Renker case IPPROTO_UDPLITE: 8271da177e4SLinus Torvalds case IPPROTO_SCTP: 8281da177e4SLinus Torvalds port = fl->fl_ip_dport; 8291da177e4SLinus Torvalds break; 8301da177e4SLinus Torvalds case IPPROTO_ICMP: 8311da177e4SLinus Torvalds case IPPROTO_ICMPV6: 8321da177e4SLinus Torvalds port = htons(fl->fl_icmp_code); 8331da177e4SLinus Torvalds break; 834cc9ff19dSTimo Teräs case IPPROTO_GRE: 835aa285b17STimo Teräs port = htons(ntohl(fl->fl_gre_key) & 0xffff); 836cc9ff19dSTimo Teräs break; 8371da177e4SLinus Torvalds default: 8381da177e4SLinus Torvalds port = 0; /*XXX*/ 8391da177e4SLinus Torvalds } 8401da177e4SLinus Torvalds return port; 8411da177e4SLinus Torvalds } 8421da177e4SLinus Torvalds 84377681021SAndrew Morton extern int xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl, 84477681021SAndrew Morton unsigned short family); 8451da177e4SLinus Torvalds 846df71837dSTrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM 847df71837dSTrent Jaeger /* If neither has a context --> match 848df71837dSTrent Jaeger * Otherwise, both must have a context and the sids, doi, alg must match 849df71837dSTrent Jaeger */ 850df71837dSTrent Jaeger static inline int xfrm_sec_ctx_match(struct xfrm_sec_ctx *s1, struct xfrm_sec_ctx *s2) 851df71837dSTrent Jaeger { 852df71837dSTrent Jaeger return ((!s1 && !s2) || 853df71837dSTrent Jaeger (s1 && s2 && 854df71837dSTrent Jaeger (s1->ctx_sid == s2->ctx_sid) && 855df71837dSTrent Jaeger (s1->ctx_doi == s2->ctx_doi) && 856df71837dSTrent Jaeger (s1->ctx_alg == s2->ctx_alg))); 857df71837dSTrent Jaeger } 858df71837dSTrent Jaeger #else 859df71837dSTrent Jaeger static inline int xfrm_sec_ctx_match(struct xfrm_sec_ctx *s1, struct xfrm_sec_ctx *s2) 860df71837dSTrent Jaeger { 861df71837dSTrent Jaeger return 1; 862df71837dSTrent Jaeger } 863df71837dSTrent Jaeger #endif 864df71837dSTrent Jaeger 8651da177e4SLinus Torvalds /* A struct encoding bundle of transformations to apply to some set of flow. 8661da177e4SLinus Torvalds * 8671da177e4SLinus Torvalds * dst->child points to the next element of bundle. 8681da177e4SLinus Torvalds * dst->xfrm points to an instanse of transformer. 8691da177e4SLinus Torvalds * 8701da177e4SLinus Torvalds * Due to unfortunate limitations of current routing cache, which we 8711da177e4SLinus Torvalds * have no time to fix, it mirrors struct rtable and bound to the same 8721da177e4SLinus Torvalds * routing key, including saddr,daddr. However, we can have many of 8731da177e4SLinus Torvalds * bundles differing by session id. All the bundles grow from a parent 8741da177e4SLinus Torvalds * policy rule. 8751da177e4SLinus Torvalds */ 876fd2c3ef7SEric Dumazet struct xfrm_dst { 8771da177e4SLinus Torvalds union { 8781da177e4SLinus Torvalds struct dst_entry dst; 8791da177e4SLinus Torvalds struct rtable rt; 8801da177e4SLinus Torvalds struct rt6_info rt6; 8811da177e4SLinus Torvalds } u; 8821da177e4SLinus Torvalds struct dst_entry *route; 88380c802f3STimo Teräs struct flow_cache_object flo; 88480c802f3STimo Teräs struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX]; 88580c802f3STimo Teräs int num_pols, num_xfrms; 886157bfc25SMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY 887157bfc25SMasahide NAKAMURA struct flowi *origin; 888157bfc25SMasahide NAKAMURA struct xfrm_selector *partner; 889157bfc25SMasahide NAKAMURA #endif 89080c802f3STimo Teräs u32 xfrm_genid; 89180c802f3STimo Teräs u32 policy_genid; 8921da177e4SLinus Torvalds u32 route_mtu_cached; 8931da177e4SLinus Torvalds u32 child_mtu_cached; 89492d63decSHideaki YOSHIFUJI u32 route_cookie; 89592d63decSHideaki YOSHIFUJI u32 path_cookie; 8961da177e4SLinus Torvalds }; 8971da177e4SLinus Torvalds 898def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM 899aabc9761SHerbert Xu static inline void xfrm_dst_destroy(struct xfrm_dst *xdst) 900aabc9761SHerbert Xu { 90180c802f3STimo Teräs xfrm_pols_put(xdst->pols, xdst->num_pols); 902aabc9761SHerbert Xu dst_release(xdst->route); 903aabc9761SHerbert Xu if (likely(xdst->u.dst.xfrm)) 904aabc9761SHerbert Xu xfrm_state_put(xdst->u.dst.xfrm); 905157bfc25SMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY 906157bfc25SMasahide NAKAMURA kfree(xdst->origin); 907157bfc25SMasahide NAKAMURA xdst->origin = NULL; 908157bfc25SMasahide NAKAMURA kfree(xdst->partner); 909157bfc25SMasahide NAKAMURA xdst->partner = NULL; 910157bfc25SMasahide NAKAMURA #endif 911aabc9761SHerbert Xu } 912def8b4faSAlexey Dobriyan #endif 913aabc9761SHerbert Xu 914aabc9761SHerbert Xu extern void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev); 915aabc9761SHerbert Xu 916fd2c3ef7SEric Dumazet struct sec_path { 9171da177e4SLinus Torvalds atomic_t refcnt; 9181da177e4SLinus Torvalds int len; 919dbe5b4aaSHerbert Xu struct xfrm_state *xvec[XFRM_MAX_DEPTH]; 9201da177e4SLinus Torvalds }; 9211da177e4SLinus Torvalds 9221da177e4SLinus Torvalds static inline struct sec_path * 9231da177e4SLinus Torvalds secpath_get(struct sec_path *sp) 9241da177e4SLinus Torvalds { 9251da177e4SLinus Torvalds if (sp) 9261da177e4SLinus Torvalds atomic_inc(&sp->refcnt); 9271da177e4SLinus Torvalds return sp; 9281da177e4SLinus Torvalds } 9291da177e4SLinus Torvalds 9301da177e4SLinus Torvalds extern void __secpath_destroy(struct sec_path *sp); 9311da177e4SLinus Torvalds 9321da177e4SLinus Torvalds static inline void 9331da177e4SLinus Torvalds secpath_put(struct sec_path *sp) 9341da177e4SLinus Torvalds { 9351da177e4SLinus Torvalds if (sp && atomic_dec_and_test(&sp->refcnt)) 9361da177e4SLinus Torvalds __secpath_destroy(sp); 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds 9391da177e4SLinus Torvalds extern struct sec_path *secpath_dup(struct sec_path *src); 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds static inline void 9421da177e4SLinus Torvalds secpath_reset(struct sk_buff *skb) 9431da177e4SLinus Torvalds { 9441da177e4SLinus Torvalds #ifdef CONFIG_XFRM 9451da177e4SLinus Torvalds secpath_put(skb->sp); 9461da177e4SLinus Torvalds skb->sp = NULL; 9471da177e4SLinus Torvalds #endif 9481da177e4SLinus Torvalds } 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds static inline int 951a1e59abfSPatrick McHardy xfrm_addr_any(xfrm_address_t *addr, unsigned short family) 952a1e59abfSPatrick McHardy { 953a1e59abfSPatrick McHardy switch (family) { 954a1e59abfSPatrick McHardy case AF_INET: 955a1e59abfSPatrick McHardy return addr->a4 == 0; 956a1e59abfSPatrick McHardy case AF_INET6: 957a1e59abfSPatrick McHardy return ipv6_addr_any((struct in6_addr *)&addr->a6); 958a1e59abfSPatrick McHardy } 959a1e59abfSPatrick McHardy return 0; 960a1e59abfSPatrick McHardy } 961a1e59abfSPatrick McHardy 962a1e59abfSPatrick McHardy static inline int 9631da177e4SLinus Torvalds __xfrm4_state_addr_cmp(struct xfrm_tmpl *tmpl, struct xfrm_state *x) 9641da177e4SLinus Torvalds { 9651da177e4SLinus Torvalds return (tmpl->saddr.a4 && 9661da177e4SLinus Torvalds tmpl->saddr.a4 != x->props.saddr.a4); 9671da177e4SLinus Torvalds } 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds static inline int 9701da177e4SLinus Torvalds __xfrm6_state_addr_cmp(struct xfrm_tmpl *tmpl, struct xfrm_state *x) 9711da177e4SLinus Torvalds { 9721da177e4SLinus Torvalds return (!ipv6_addr_any((struct in6_addr*)&tmpl->saddr) && 9731da177e4SLinus Torvalds ipv6_addr_cmp((struct in6_addr *)&tmpl->saddr, (struct in6_addr*)&x->props.saddr)); 9741da177e4SLinus Torvalds } 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds static inline int 9771da177e4SLinus Torvalds xfrm_state_addr_cmp(struct xfrm_tmpl *tmpl, struct xfrm_state *x, unsigned short family) 9781da177e4SLinus Torvalds { 9791da177e4SLinus Torvalds switch (family) { 9801da177e4SLinus Torvalds case AF_INET: 9811da177e4SLinus Torvalds return __xfrm4_state_addr_cmp(tmpl, x); 9821da177e4SLinus Torvalds case AF_INET6: 9831da177e4SLinus Torvalds return __xfrm6_state_addr_cmp(tmpl, x); 9841da177e4SLinus Torvalds } 9851da177e4SLinus Torvalds return !0; 9861da177e4SLinus Torvalds } 9871da177e4SLinus Torvalds 9881da177e4SLinus Torvalds #ifdef CONFIG_XFRM 9891da177e4SLinus Torvalds extern int __xfrm_policy_check(struct sock *, int dir, struct sk_buff *skb, unsigned short family); 9901da177e4SLinus Torvalds 991d5422efeSHerbert Xu static inline int __xfrm_policy_check2(struct sock *sk, int dir, 992d5422efeSHerbert Xu struct sk_buff *skb, 993d5422efeSHerbert Xu unsigned int family, int reverse) 9941da177e4SLinus Torvalds { 995f6e1e25dSAlexey Dobriyan struct net *net = dev_net(skb->dev); 996d5422efeSHerbert Xu int ndir = dir | (reverse ? XFRM_POLICY_MASK + 1 : 0); 997d5422efeSHerbert Xu 9981da177e4SLinus Torvalds if (sk && sk->sk_policy[XFRM_POLICY_IN]) 999d5422efeSHerbert Xu return __xfrm_policy_check(sk, ndir, skb, family); 10001da177e4SLinus Torvalds 1001f6e1e25dSAlexey Dobriyan return (!net->xfrm.policy_count[dir] && !skb->sp) || 1002adf30907SEric Dumazet (skb_dst(skb)->flags & DST_NOPOLICY) || 1003d5422efeSHerbert Xu __xfrm_policy_check(sk, ndir, skb, family); 1004d5422efeSHerbert Xu } 1005d5422efeSHerbert Xu 1006d5422efeSHerbert Xu static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family) 1007d5422efeSHerbert Xu { 1008d5422efeSHerbert Xu return __xfrm_policy_check2(sk, dir, skb, family, 0); 10091da177e4SLinus Torvalds } 10101da177e4SLinus Torvalds 10111da177e4SLinus Torvalds static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb) 10121da177e4SLinus Torvalds { 10131da177e4SLinus Torvalds return xfrm_policy_check(sk, dir, skb, AF_INET); 10141da177e4SLinus Torvalds } 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb) 10171da177e4SLinus Torvalds { 10181da177e4SLinus Torvalds return xfrm_policy_check(sk, dir, skb, AF_INET6); 10191da177e4SLinus Torvalds } 10201da177e4SLinus Torvalds 1021d5422efeSHerbert Xu static inline int xfrm4_policy_check_reverse(struct sock *sk, int dir, 1022d5422efeSHerbert Xu struct sk_buff *skb) 1023d5422efeSHerbert Xu { 1024d5422efeSHerbert Xu return __xfrm_policy_check2(sk, dir, skb, AF_INET, 1); 1025d5422efeSHerbert Xu } 1026d5422efeSHerbert Xu 1027d5422efeSHerbert Xu static inline int xfrm6_policy_check_reverse(struct sock *sk, int dir, 1028d5422efeSHerbert Xu struct sk_buff *skb) 1029d5422efeSHerbert Xu { 1030d5422efeSHerbert Xu return __xfrm_policy_check2(sk, dir, skb, AF_INET6, 1); 1031d5422efeSHerbert Xu } 1032d5422efeSHerbert Xu 1033d5422efeSHerbert Xu extern int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, 1034d5422efeSHerbert Xu unsigned int family, int reverse); 1035d5422efeSHerbert Xu 1036d5422efeSHerbert Xu static inline int xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, 1037d5422efeSHerbert Xu unsigned int family) 1038d5422efeSHerbert Xu { 1039d5422efeSHerbert Xu return __xfrm_decode_session(skb, fl, family, 0); 1040d5422efeSHerbert Xu } 1041d5422efeSHerbert Xu 1042d5422efeSHerbert Xu static inline int xfrm_decode_session_reverse(struct sk_buff *skb, 1043d5422efeSHerbert Xu struct flowi *fl, 1044d5422efeSHerbert Xu unsigned int family) 1045d5422efeSHerbert Xu { 1046d5422efeSHerbert Xu return __xfrm_decode_session(skb, fl, family, 1); 1047d5422efeSHerbert Xu } 1048d5422efeSHerbert Xu 10491da177e4SLinus Torvalds extern int __xfrm_route_forward(struct sk_buff *skb, unsigned short family); 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds static inline int xfrm_route_forward(struct sk_buff *skb, unsigned short family) 10521da177e4SLinus Torvalds { 105399a66657SAlexey Dobriyan struct net *net = dev_net(skb->dev); 105499a66657SAlexey Dobriyan 105599a66657SAlexey Dobriyan return !net->xfrm.policy_count[XFRM_POLICY_OUT] || 1056adf30907SEric Dumazet (skb_dst(skb)->flags & DST_NOXFRM) || 10571da177e4SLinus Torvalds __xfrm_route_forward(skb, family); 10581da177e4SLinus Torvalds } 10591da177e4SLinus Torvalds 10601da177e4SLinus Torvalds static inline int xfrm4_route_forward(struct sk_buff *skb) 10611da177e4SLinus Torvalds { 10621da177e4SLinus Torvalds return xfrm_route_forward(skb, AF_INET); 10631da177e4SLinus Torvalds } 10641da177e4SLinus Torvalds 10651da177e4SLinus Torvalds static inline int xfrm6_route_forward(struct sk_buff *skb) 10661da177e4SLinus Torvalds { 10671da177e4SLinus Torvalds return xfrm_route_forward(skb, AF_INET6); 10681da177e4SLinus Torvalds } 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds extern int __xfrm_sk_clone_policy(struct sock *sk); 10711da177e4SLinus Torvalds 10721da177e4SLinus Torvalds static inline int xfrm_sk_clone_policy(struct sock *sk) 10731da177e4SLinus Torvalds { 10741da177e4SLinus Torvalds if (unlikely(sk->sk_policy[0] || sk->sk_policy[1])) 10751da177e4SLinus Torvalds return __xfrm_sk_clone_policy(sk); 10761da177e4SLinus Torvalds return 0; 10771da177e4SLinus Torvalds } 10781da177e4SLinus Torvalds 10794666faabSHerbert Xu extern int xfrm_policy_delete(struct xfrm_policy *pol, int dir); 10801da177e4SLinus Torvalds 10811da177e4SLinus Torvalds static inline void xfrm_sk_free_policy(struct sock *sk) 10821da177e4SLinus Torvalds { 10831da177e4SLinus Torvalds if (unlikely(sk->sk_policy[0] != NULL)) { 10841da177e4SLinus Torvalds xfrm_policy_delete(sk->sk_policy[0], XFRM_POLICY_MAX); 10851da177e4SLinus Torvalds sk->sk_policy[0] = NULL; 10861da177e4SLinus Torvalds } 10871da177e4SLinus Torvalds if (unlikely(sk->sk_policy[1] != NULL)) { 10881da177e4SLinus Torvalds xfrm_policy_delete(sk->sk_policy[1], XFRM_POLICY_MAX+1); 10891da177e4SLinus Torvalds sk->sk_policy[1] = NULL; 10901da177e4SLinus Torvalds } 10911da177e4SLinus Torvalds } 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds #else 10941da177e4SLinus Torvalds 10951da177e4SLinus Torvalds static inline void xfrm_sk_free_policy(struct sock *sk) {} 10961da177e4SLinus Torvalds static inline int xfrm_sk_clone_policy(struct sock *sk) { return 0; } 10971da177e4SLinus Torvalds static inline int xfrm6_route_forward(struct sk_buff *skb) { return 1; } 10981da177e4SLinus Torvalds static inline int xfrm4_route_forward(struct sk_buff *skb) { return 1; } 10991da177e4SLinus Torvalds static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb) 11001da177e4SLinus Torvalds { 11011da177e4SLinus Torvalds return 1; 11021da177e4SLinus Torvalds } 11031da177e4SLinus Torvalds static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb) 11041da177e4SLinus Torvalds { 11051da177e4SLinus Torvalds return 1; 11061da177e4SLinus Torvalds } 11071da177e4SLinus Torvalds static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family) 11081da177e4SLinus Torvalds { 11091da177e4SLinus Torvalds return 1; 11101da177e4SLinus Torvalds } 1111d5422efeSHerbert Xu static inline int xfrm_decode_session_reverse(struct sk_buff *skb, 1112d5422efeSHerbert Xu struct flowi *fl, 1113d5422efeSHerbert Xu unsigned int family) 1114d5422efeSHerbert Xu { 1115d5422efeSHerbert Xu return -ENOSYS; 1116d5422efeSHerbert Xu } 1117d5422efeSHerbert Xu static inline int xfrm4_policy_check_reverse(struct sock *sk, int dir, 1118d5422efeSHerbert Xu struct sk_buff *skb) 1119d5422efeSHerbert Xu { 1120d5422efeSHerbert Xu return 1; 1121d5422efeSHerbert Xu } 1122d5422efeSHerbert Xu static inline int xfrm6_policy_check_reverse(struct sock *sk, int dir, 1123d5422efeSHerbert Xu struct sk_buff *skb) 1124d5422efeSHerbert Xu { 1125d5422efeSHerbert Xu return 1; 1126d5422efeSHerbert Xu } 11271da177e4SLinus Torvalds #endif 11281da177e4SLinus Torvalds 11291da177e4SLinus Torvalds static __inline__ 1130e8a4e377SDavid S. Miller xfrm_address_t *xfrm_flowi_daddr(const struct flowi *fl, unsigned short family) 11311da177e4SLinus Torvalds { 11321da177e4SLinus Torvalds switch (family){ 11331da177e4SLinus Torvalds case AF_INET: 11341da177e4SLinus Torvalds return (xfrm_address_t *)&fl->fl4_dst; 11351da177e4SLinus Torvalds case AF_INET6: 11361da177e4SLinus Torvalds return (xfrm_address_t *)&fl->fl6_dst; 11371da177e4SLinus Torvalds } 11381da177e4SLinus Torvalds return NULL; 11391da177e4SLinus Torvalds } 11401da177e4SLinus Torvalds 11411da177e4SLinus Torvalds static __inline__ 1142e8a4e377SDavid S. Miller xfrm_address_t *xfrm_flowi_saddr(const struct flowi *fl, unsigned short family) 11431da177e4SLinus Torvalds { 11441da177e4SLinus Torvalds switch (family){ 11451da177e4SLinus Torvalds case AF_INET: 11461da177e4SLinus Torvalds return (xfrm_address_t *)&fl->fl4_src; 11471da177e4SLinus Torvalds case AF_INET6: 11481da177e4SLinus Torvalds return (xfrm_address_t *)&fl->fl6_src; 11491da177e4SLinus Torvalds } 11501da177e4SLinus Torvalds return NULL; 11511da177e4SLinus Torvalds } 11521da177e4SLinus Torvalds 11539bb182a7SYOSHIFUJI Hideaki static __inline__ 1154e8a4e377SDavid S. Miller void xfrm_flowi_addr_get(const struct flowi *fl, 11559bb182a7SYOSHIFUJI Hideaki xfrm_address_t *saddr, xfrm_address_t *daddr, 11569bb182a7SYOSHIFUJI Hideaki unsigned short family) 11579bb182a7SYOSHIFUJI Hideaki { 11589bb182a7SYOSHIFUJI Hideaki switch(family) { 11599bb182a7SYOSHIFUJI Hideaki case AF_INET: 11609bb182a7SYOSHIFUJI Hideaki memcpy(&saddr->a4, &fl->fl4_src, sizeof(saddr->a4)); 11619bb182a7SYOSHIFUJI Hideaki memcpy(&daddr->a4, &fl->fl4_dst, sizeof(daddr->a4)); 11629bb182a7SYOSHIFUJI Hideaki break; 11639bb182a7SYOSHIFUJI Hideaki case AF_INET6: 11649bb182a7SYOSHIFUJI Hideaki ipv6_addr_copy((struct in6_addr *)&saddr->a6, &fl->fl6_src); 11659bb182a7SYOSHIFUJI Hideaki ipv6_addr_copy((struct in6_addr *)&daddr->a6, &fl->fl6_dst); 11669bb182a7SYOSHIFUJI Hideaki break; 11679bb182a7SYOSHIFUJI Hideaki } 11689bb182a7SYOSHIFUJI Hideaki } 11699bb182a7SYOSHIFUJI Hideaki 11701da177e4SLinus Torvalds static __inline__ int 11711da177e4SLinus Torvalds __xfrm4_state_addr_check(struct xfrm_state *x, 11721da177e4SLinus Torvalds xfrm_address_t *daddr, xfrm_address_t *saddr) 11731da177e4SLinus Torvalds { 11741da177e4SLinus Torvalds if (daddr->a4 == x->id.daddr.a4 && 11751da177e4SLinus Torvalds (saddr->a4 == x->props.saddr.a4 || !saddr->a4 || !x->props.saddr.a4)) 11761da177e4SLinus Torvalds return 1; 11771da177e4SLinus Torvalds return 0; 11781da177e4SLinus Torvalds } 11791da177e4SLinus Torvalds 11801da177e4SLinus Torvalds static __inline__ int 11811da177e4SLinus Torvalds __xfrm6_state_addr_check(struct xfrm_state *x, 11821da177e4SLinus Torvalds xfrm_address_t *daddr, xfrm_address_t *saddr) 11831da177e4SLinus Torvalds { 11841da177e4SLinus Torvalds if (!ipv6_addr_cmp((struct in6_addr *)daddr, (struct in6_addr *)&x->id.daddr) && 11851da177e4SLinus Torvalds (!ipv6_addr_cmp((struct in6_addr *)saddr, (struct in6_addr *)&x->props.saddr)|| 11861da177e4SLinus Torvalds ipv6_addr_any((struct in6_addr *)saddr) || 11871da177e4SLinus Torvalds ipv6_addr_any((struct in6_addr *)&x->props.saddr))) 11881da177e4SLinus Torvalds return 1; 11891da177e4SLinus Torvalds return 0; 11901da177e4SLinus Torvalds } 11911da177e4SLinus Torvalds 11921da177e4SLinus Torvalds static __inline__ int 11931da177e4SLinus Torvalds xfrm_state_addr_check(struct xfrm_state *x, 11941da177e4SLinus Torvalds xfrm_address_t *daddr, xfrm_address_t *saddr, 11951da177e4SLinus Torvalds unsigned short family) 11961da177e4SLinus Torvalds { 11971da177e4SLinus Torvalds switch (family) { 11981da177e4SLinus Torvalds case AF_INET: 11991da177e4SLinus Torvalds return __xfrm4_state_addr_check(x, daddr, saddr); 12001da177e4SLinus Torvalds case AF_INET6: 12011da177e4SLinus Torvalds return __xfrm6_state_addr_check(x, daddr, saddr); 12021da177e4SLinus Torvalds } 12031da177e4SLinus Torvalds return 0; 12041da177e4SLinus Torvalds } 12051da177e4SLinus Torvalds 1206e53820deSMasahide NAKAMURA static __inline__ int 1207e8a4e377SDavid S. Miller xfrm_state_addr_flow_check(struct xfrm_state *x, const struct flowi *fl, 1208e53820deSMasahide NAKAMURA unsigned short family) 1209e53820deSMasahide NAKAMURA { 1210e53820deSMasahide NAKAMURA switch (family) { 1211e53820deSMasahide NAKAMURA case AF_INET: 1212e53820deSMasahide NAKAMURA return __xfrm4_state_addr_check(x, 1213e53820deSMasahide NAKAMURA (xfrm_address_t *)&fl->fl4_dst, 1214e53820deSMasahide NAKAMURA (xfrm_address_t *)&fl->fl4_src); 1215e53820deSMasahide NAKAMURA case AF_INET6: 1216e53820deSMasahide NAKAMURA return __xfrm6_state_addr_check(x, 1217e53820deSMasahide NAKAMURA (xfrm_address_t *)&fl->fl6_dst, 1218e53820deSMasahide NAKAMURA (xfrm_address_t *)&fl->fl6_src); 1219e53820deSMasahide NAKAMURA } 1220e53820deSMasahide NAKAMURA return 0; 1221e53820deSMasahide NAKAMURA } 1222e53820deSMasahide NAKAMURA 12231da177e4SLinus Torvalds static inline int xfrm_state_kern(struct xfrm_state *x) 12241da177e4SLinus Torvalds { 12251da177e4SLinus Torvalds return atomic_read(&x->tunnel_users); 12261da177e4SLinus Torvalds } 12271da177e4SLinus Torvalds 12285794708fSMasahide NAKAMURA static inline int xfrm_id_proto_match(u8 proto, u8 userproto) 12295794708fSMasahide NAKAMURA { 1230dc00a525SMasahide NAKAMURA return (!userproto || proto == userproto || 1231dc00a525SMasahide NAKAMURA (userproto == IPSEC_PROTO_ANY && (proto == IPPROTO_AH || 1232dc00a525SMasahide NAKAMURA proto == IPPROTO_ESP || 1233dc00a525SMasahide NAKAMURA proto == IPPROTO_COMP))); 12345794708fSMasahide NAKAMURA } 12355794708fSMasahide NAKAMURA 12361da177e4SLinus Torvalds /* 12371da177e4SLinus Torvalds * xfrm algorithm information 12381da177e4SLinus Torvalds */ 12391a6509d9SHerbert Xu struct xfrm_algo_aead_info { 12401a6509d9SHerbert Xu u16 icv_truncbits; 12411a6509d9SHerbert Xu }; 12421a6509d9SHerbert Xu 12431da177e4SLinus Torvalds struct xfrm_algo_auth_info { 12441da177e4SLinus Torvalds u16 icv_truncbits; 12451da177e4SLinus Torvalds u16 icv_fullbits; 12461da177e4SLinus Torvalds }; 12471da177e4SLinus Torvalds 12481da177e4SLinus Torvalds struct xfrm_algo_encr_info { 12491da177e4SLinus Torvalds u16 blockbits; 12501da177e4SLinus Torvalds u16 defkeybits; 12511da177e4SLinus Torvalds }; 12521da177e4SLinus Torvalds 12531da177e4SLinus Torvalds struct xfrm_algo_comp_info { 12541da177e4SLinus Torvalds u16 threshold; 12551da177e4SLinus Torvalds }; 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds struct xfrm_algo_desc { 12581da177e4SLinus Torvalds char *name; 125904ff1260SHerbert Xu char *compat; 12601da177e4SLinus Torvalds u8 available:1; 12611da177e4SLinus Torvalds union { 12621a6509d9SHerbert Xu struct xfrm_algo_aead_info aead; 12631da177e4SLinus Torvalds struct xfrm_algo_auth_info auth; 12641da177e4SLinus Torvalds struct xfrm_algo_encr_info encr; 12651da177e4SLinus Torvalds struct xfrm_algo_comp_info comp; 12661da177e4SLinus Torvalds } uinfo; 12671da177e4SLinus Torvalds struct sadb_alg desc; 12681da177e4SLinus Torvalds }; 12691da177e4SLinus Torvalds 12701da177e4SLinus Torvalds /* XFRM tunnel handlers. */ 12711da177e4SLinus Torvalds struct xfrm_tunnel { 12721da177e4SLinus Torvalds int (*handler)(struct sk_buff *skb); 1273a6337463Sjamal int (*err_handler)(struct sk_buff *skb, u32 info); 1274d2acc347SHerbert Xu 1275b33eab08SEric Dumazet struct xfrm_tunnel __rcu *next; 1276d2acc347SHerbert Xu int priority; 12771da177e4SLinus Torvalds }; 12781da177e4SLinus Torvalds 12791da177e4SLinus Torvalds struct xfrm6_tunnel { 1280d2acc347SHerbert Xu int (*handler)(struct sk_buff *skb); 1281d2acc347SHerbert Xu int (*err_handler)(struct sk_buff *skb, struct inet6_skb_parm *opt, 1282d5fdd6baSBrian Haley u8 type, u8 code, int offset, __be32 info); 12836f0bcf15SEric Dumazet struct xfrm6_tunnel __rcu *next; 1284d2acc347SHerbert Xu int priority; 12851da177e4SLinus Torvalds }; 12861da177e4SLinus Torvalds 12871da177e4SLinus Torvalds extern void xfrm_init(void); 1288a33bc5c1SNeil Horman extern void xfrm4_init(int rt_hash_size); 1289d62ddc21SAlexey Dobriyan extern int xfrm_state_init(struct net *net); 1290d62ddc21SAlexey Dobriyan extern void xfrm_state_fini(struct net *net); 12911da177e4SLinus Torvalds extern void xfrm4_state_init(void); 1292c35b7e72SDaniel Lezcano #ifdef CONFIG_XFRM 1293c35b7e72SDaniel Lezcano extern int xfrm6_init(void); 1294c35b7e72SDaniel Lezcano extern void xfrm6_fini(void); 12950013cabaSDaniel Lezcano extern int xfrm6_state_init(void); 12961da177e4SLinus Torvalds extern void xfrm6_state_fini(void); 1297c35b7e72SDaniel Lezcano #else 1298c35b7e72SDaniel Lezcano static inline int xfrm6_init(void) 1299c35b7e72SDaniel Lezcano { 1300c35b7e72SDaniel Lezcano return 0; 1301c35b7e72SDaniel Lezcano } 1302c35b7e72SDaniel Lezcano static inline void xfrm6_fini(void) 1303c35b7e72SDaniel Lezcano { 1304c35b7e72SDaniel Lezcano ; 1305c35b7e72SDaniel Lezcano } 1306c35b7e72SDaniel Lezcano #endif 13071da177e4SLinus Torvalds 1308558f82efSMasahide NAKAMURA #ifdef CONFIG_XFRM_STATISTICS 1309c68cd1a0SAlexey Dobriyan extern int xfrm_proc_init(struct net *net); 1310c68cd1a0SAlexey Dobriyan extern void xfrm_proc_fini(struct net *net); 1311558f82efSMasahide NAKAMURA #endif 1312558f82efSMasahide NAKAMURA 1313b27aeadbSAlexey Dobriyan extern int xfrm_sysctl_init(struct net *net); 1314b27aeadbSAlexey Dobriyan #ifdef CONFIG_SYSCTL 1315b27aeadbSAlexey Dobriyan extern void xfrm_sysctl_fini(struct net *net); 1316b27aeadbSAlexey Dobriyan #else 1317b27aeadbSAlexey Dobriyan static inline void xfrm_sysctl_fini(struct net *net) 1318b27aeadbSAlexey Dobriyan { 1319b27aeadbSAlexey Dobriyan } 1320b27aeadbSAlexey Dobriyan #endif 1321b27aeadbSAlexey Dobriyan 13225c182458SHerbert Xu extern void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto); 1323284fa7daSAlexey Dobriyan extern int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk, 13244c563f76STimo Teras int (*func)(struct xfrm_state *, int, void*), void *); 1325abb81c4fSHerbert Xu extern void xfrm_state_walk_done(struct xfrm_state_walk *walk); 1326673c09beSAlexey Dobriyan extern struct xfrm_state *xfrm_state_alloc(struct net *net); 13271da177e4SLinus Torvalds extern struct xfrm_state *xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr, 13281da177e4SLinus Torvalds struct flowi *fl, struct xfrm_tmpl *tmpl, 13291da177e4SLinus Torvalds struct xfrm_policy *pol, int *err, 13301da177e4SLinus Torvalds unsigned short family); 1331bd55775cSJamal Hadi Salim extern struct xfrm_state *xfrm_stateonly_find(struct net *net, u32 mark, 13325447c5e4SAlexey Dobriyan xfrm_address_t *daddr, 1333628529b6SJamal Hadi Salim xfrm_address_t *saddr, 1334628529b6SJamal Hadi Salim unsigned short family, 1335628529b6SJamal Hadi Salim u8 mode, u8 proto, u32 reqid); 13361da177e4SLinus Torvalds extern int xfrm_state_check_expire(struct xfrm_state *x); 13371da177e4SLinus Torvalds extern void xfrm_state_insert(struct xfrm_state *x); 13381da177e4SLinus Torvalds extern int xfrm_state_add(struct xfrm_state *x); 13391da177e4SLinus Torvalds extern int xfrm_state_update(struct xfrm_state *x); 1340bd55775cSJamal Hadi Salim extern struct xfrm_state *xfrm_state_lookup(struct net *net, u32 mark, 1341bd55775cSJamal Hadi Salim xfrm_address_t *daddr, __be32 spi, 1342bd55775cSJamal Hadi Salim u8 proto, unsigned short family); 1343bd55775cSJamal Hadi Salim extern struct xfrm_state *xfrm_state_lookup_byaddr(struct net *net, u32 mark, 1344bd55775cSJamal Hadi Salim xfrm_address_t *daddr, 1345bd55775cSJamal Hadi Salim xfrm_address_t *saddr, 1346bd55775cSJamal Hadi Salim u8 proto, 1347bd55775cSJamal Hadi Salim unsigned short family); 134841a49cc3SMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY 134941a49cc3SMasahide NAKAMURA extern int xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, 135041a49cc3SMasahide NAKAMURA int n, unsigned short family); 135141a49cc3SMasahide NAKAMURA extern int xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, 135241a49cc3SMasahide NAKAMURA int n, unsigned short family); 135341a49cc3SMasahide NAKAMURA #else 135441a49cc3SMasahide NAKAMURA static inline int xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, 135541a49cc3SMasahide NAKAMURA int n, unsigned short family) 135641a49cc3SMasahide NAKAMURA { 135741a49cc3SMasahide NAKAMURA return -ENOSYS; 135841a49cc3SMasahide NAKAMURA } 135941a49cc3SMasahide NAKAMURA 136041a49cc3SMasahide NAKAMURA static inline int xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, 136141a49cc3SMasahide NAKAMURA int n, unsigned short family) 136241a49cc3SMasahide NAKAMURA { 136341a49cc3SMasahide NAKAMURA return -ENOSYS; 136441a49cc3SMasahide NAKAMURA } 136541a49cc3SMasahide NAKAMURA #endif 1366af11e316SJamal Hadi Salim 1367af11e316SJamal Hadi Salim struct xfrmk_sadinfo { 1368af11e316SJamal Hadi Salim u32 sadhcnt; /* current hash bkts */ 1369af11e316SJamal Hadi Salim u32 sadhmcnt; /* max allowed hash bkts */ 1370af11e316SJamal Hadi Salim u32 sadcnt; /* current running count */ 1371af11e316SJamal Hadi Salim }; 1372af11e316SJamal Hadi Salim 13735a6d3416SJamal Hadi Salim struct xfrmk_spdinfo { 13745a6d3416SJamal Hadi Salim u32 incnt; 13755a6d3416SJamal Hadi Salim u32 outcnt; 13765a6d3416SJamal Hadi Salim u32 fwdcnt; 13775a6d3416SJamal Hadi Salim u32 inscnt; 13785a6d3416SJamal Hadi Salim u32 outscnt; 13795a6d3416SJamal Hadi Salim u32 fwdscnt; 13805a6d3416SJamal Hadi Salim u32 spdhcnt; 13815a6d3416SJamal Hadi Salim u32 spdhmcnt; 13825a6d3416SJamal Hadi Salim }; 13835a6d3416SJamal Hadi Salim 1384bd55775cSJamal Hadi Salim extern struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, 1385bd55775cSJamal Hadi Salim u32 seq); 138626b15dadSJamal Hadi Salim extern int xfrm_state_delete(struct xfrm_state *x); 13870e602451SAlexey Dobriyan extern int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info); 1388e071041bSAlexey Dobriyan extern void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si); 1389e071041bSAlexey Dobriyan extern void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si); 1390afeb14b4SPaul Moore extern int xfrm_replay_check(struct xfrm_state *x, 1391afeb14b4SPaul Moore struct sk_buff *skb, __be32 seq); 139261f4627bSAl Viro extern void xfrm_replay_advance(struct xfrm_state *x, __be32 seq); 1393f8cd5488SJamal Hadi Salim extern void xfrm_replay_notify(struct xfrm_state *x, int event); 13941da177e4SLinus Torvalds extern int xfrm_state_mtu(struct xfrm_state *x, int mtu); 139572cb6962SHerbert Xu extern int xfrm_init_state(struct xfrm_state *x); 1396227620e2SHerbert Xu extern int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb); 1397716062fdSHerbert Xu extern int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, 1398716062fdSHerbert Xu int encap_type); 13991bf06cd2SHerbert Xu extern int xfrm_input_resume(struct sk_buff *skb, int nexthdr); 1400c6581a45SHerbert Xu extern int xfrm_output_resume(struct sk_buff *skb, int err); 1401406ef77cSHerbert Xu extern int xfrm_output(struct sk_buff *skb); 1402df9dcb45SKazunori MIYAZAWA extern int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb); 140336cf9acfSHerbert Xu extern int xfrm4_extract_header(struct sk_buff *skb); 1404227620e2SHerbert Xu extern int xfrm4_extract_input(struct xfrm_state *x, struct sk_buff *skb); 1405c4541b41SHerbert Xu extern int xfrm4_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi, 1406c4541b41SHerbert Xu int encap_type); 1407716062fdSHerbert Xu extern int xfrm4_transport_finish(struct sk_buff *skb, int async); 14081da177e4SLinus Torvalds extern int xfrm4_rcv(struct sk_buff *skb); 1409c4541b41SHerbert Xu 1410c4541b41SHerbert Xu static inline int xfrm4_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi) 1411c4541b41SHerbert Xu { 1412c4541b41SHerbert Xu return xfrm4_rcv_encap(skb, nexthdr, spi, 0); 1413c4541b41SHerbert Xu } 1414c4541b41SHerbert Xu 141536cf9acfSHerbert Xu extern int xfrm4_extract_output(struct xfrm_state *x, struct sk_buff *skb); 141636cf9acfSHerbert Xu extern int xfrm4_prepare_output(struct xfrm_state *x, struct sk_buff *skb); 14171da177e4SLinus Torvalds extern int xfrm4_output(struct sk_buff *skb); 1418c0d56408SKazunori MIYAZAWA extern int xfrm4_tunnel_register(struct xfrm_tunnel *handler, unsigned short family); 1419c0d56408SKazunori MIYAZAWA extern int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler, unsigned short family); 142036cf9acfSHerbert Xu extern int xfrm6_extract_header(struct sk_buff *skb); 1421227620e2SHerbert Xu extern int xfrm6_extract_input(struct xfrm_state *x, struct sk_buff *skb); 142233b5ecb8SHerbert Xu extern int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi); 1423716062fdSHerbert Xu extern int xfrm6_transport_finish(struct sk_buff *skb, int async); 1424e5bbef20SHerbert Xu extern int xfrm6_rcv(struct sk_buff *skb); 1425fbd9a5b4SMasahide NAKAMURA extern int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr, 1426fbd9a5b4SMasahide NAKAMURA xfrm_address_t *saddr, u8 proto); 142773d605d1SKazunori MIYAZAWA extern int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family); 142873d605d1SKazunori MIYAZAWA extern int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family); 1429a1664773SAlexey Dobriyan extern __be32 xfrm6_tunnel_alloc_spi(struct net *net, xfrm_address_t *saddr); 1430a1664773SAlexey Dobriyan extern __be32 xfrm6_tunnel_spi_lookup(struct net *net, xfrm_address_t *saddr); 143136cf9acfSHerbert Xu extern int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb); 143236cf9acfSHerbert Xu extern int xfrm6_prepare_output(struct xfrm_state *x, struct sk_buff *skb); 14331da177e4SLinus Torvalds extern int xfrm6_output(struct sk_buff *skb); 1434aee5adb4SMasahide NAKAMURA extern int xfrm6_find_1stfragopt(struct xfrm_state *x, struct sk_buff *skb, 1435aee5adb4SMasahide NAKAMURA u8 **prevhdr); 14361da177e4SLinus Torvalds 14371da177e4SLinus Torvalds #ifdef CONFIG_XFRM 1438067b207bSJames Chapman extern int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb); 14391da177e4SLinus Torvalds extern int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen); 14401da177e4SLinus Torvalds #else 14411da177e4SLinus Torvalds static inline int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen) 14421da177e4SLinus Torvalds { 14431da177e4SLinus Torvalds return -ENOPROTOOPT; 14441da177e4SLinus Torvalds } 14451da177e4SLinus Torvalds 1446067b207bSJames Chapman static inline int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb) 14471da177e4SLinus Torvalds { 14481da177e4SLinus Torvalds /* should not happen */ 14491da177e4SLinus Torvalds kfree_skb(skb); 14501da177e4SLinus Torvalds return 0; 14511da177e4SLinus Torvalds } 14521da177e4SLinus Torvalds #endif 14531da177e4SLinus Torvalds 14540331b1f3SAlexey Dobriyan struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp); 14554c563f76STimo Teras 145612a169e7SHerbert Xu extern void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type); 1457cdcbca7cSAlexey Dobriyan extern int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk, 14584c563f76STimo Teras int (*func)(struct xfrm_policy *, int, int, void*), void *); 145912a169e7SHerbert Xu extern void xfrm_policy_walk_done(struct xfrm_policy_walk *walk); 14601da177e4SLinus Torvalds int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl); 14618ca2e93bSJamal Hadi Salim struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, 14628ca2e93bSJamal Hadi Salim u8 type, int dir, 14634e81bb83SMasahide NAKAMURA struct xfrm_selector *sel, 1464ef41aaa0SEric Paris struct xfrm_sec_ctx *ctx, int delete, 1465ef41aaa0SEric Paris int *err); 14668ca2e93bSJamal Hadi Salim struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8, int dir, u32 id, int delete, int *err); 146733ffbbd5SAlexey Dobriyan int xfrm_policy_flush(struct net *net, u8 type, struct xfrm_audit *audit_info); 14681da177e4SLinus Torvalds u32 xfrm_get_acqseq(void); 1469658b219eSHerbert Xu extern int xfrm_alloc_spi(struct xfrm_state *x, u32 minspi, u32 maxspi); 1470bd55775cSJamal Hadi Salim struct xfrm_state *xfrm_find_acq(struct net *net, struct xfrm_mark *mark, 1471bd55775cSJamal Hadi Salim u8 mode, u32 reqid, u8 proto, 1472bd55775cSJamal Hadi Salim xfrm_address_t *daddr, 1473bd55775cSJamal Hadi Salim xfrm_address_t *saddr, int create, 1474bd55775cSJamal Hadi Salim unsigned short family); 14751da177e4SLinus Torvalds extern int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol); 14761da177e4SLinus Torvalds 147780c9abaaSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE 147880c9abaaSShinta Sugimoto extern int km_migrate(struct xfrm_selector *sel, u8 dir, u8 type, 147913c1d189SArnaud Ebalard struct xfrm_migrate *m, int num_bundles, 148013c1d189SArnaud Ebalard struct xfrm_kmaddress *k); 148180c9abaaSShinta Sugimoto extern struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m); 148280c9abaaSShinta Sugimoto extern struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x, 148380c9abaaSShinta Sugimoto struct xfrm_migrate *m); 148480c9abaaSShinta Sugimoto extern int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type, 148513c1d189SArnaud Ebalard struct xfrm_migrate *m, int num_bundles, 148613c1d189SArnaud Ebalard struct xfrm_kmaddress *k); 148780c9abaaSShinta Sugimoto #endif 148880c9abaaSShinta Sugimoto 14895d36b180SAl Viro extern int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport); 14906c5c8ca7SJamal Hadi Salim extern void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 pid); 1491db983c11SAlexey Dobriyan extern int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr); 14921da177e4SLinus Torvalds 14931da177e4SLinus Torvalds extern void xfrm_input_init(void); 14946067b2baSAl Viro extern int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq); 14951da177e4SLinus Torvalds 14961da177e4SLinus Torvalds extern void xfrm_probe_algs(void); 14971da177e4SLinus Torvalds extern int xfrm_count_auth_supported(void); 14981da177e4SLinus Torvalds extern int xfrm_count_enc_supported(void); 14991da177e4SLinus Torvalds extern struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx); 15001da177e4SLinus Torvalds extern struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx); 15011da177e4SLinus Torvalds extern struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id); 15021da177e4SLinus Torvalds extern struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id); 15031da177e4SLinus Torvalds extern struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id); 15041da177e4SLinus Torvalds extern struct xfrm_algo_desc *xfrm_aalg_get_byname(char *name, int probe); 15051da177e4SLinus Torvalds extern struct xfrm_algo_desc *xfrm_ealg_get_byname(char *name, int probe); 15061da177e4SLinus Torvalds extern struct xfrm_algo_desc *xfrm_calg_get_byname(char *name, int probe); 15071a6509d9SHerbert Xu extern struct xfrm_algo_desc *xfrm_aead_get_byname(char *name, int icv_len, 15081a6509d9SHerbert Xu int probe); 15091da177e4SLinus Torvalds 151007d4ee58SHerbert Xu struct hash_desc; 15119409f38aSHerbert Xu struct scatterlist; 151207d4ee58SHerbert Xu typedef int (icv_update_fn_t)(struct hash_desc *, struct scatterlist *, 151307d4ee58SHerbert Xu unsigned int); 15141da177e4SLinus Torvalds 15151da177e4SLinus Torvalds static inline int xfrm_addr_cmp(xfrm_address_t *a, xfrm_address_t *b, 15161da177e4SLinus Torvalds int family) 15171da177e4SLinus Torvalds { 15181da177e4SLinus Torvalds switch (family) { 15191da177e4SLinus Torvalds default: 15201da177e4SLinus Torvalds case AF_INET: 1521a6337463Sjamal return (__force u32)a->a4 - (__force u32)b->a4; 15221da177e4SLinus Torvalds case AF_INET6: 15231da177e4SLinus Torvalds return ipv6_addr_cmp((struct in6_addr *)a, 15241da177e4SLinus Torvalds (struct in6_addr *)b); 15251da177e4SLinus Torvalds } 15261da177e4SLinus Torvalds } 15271da177e4SLinus Torvalds 152877d8d7a6SHerbert Xu static inline int xfrm_policy_id2dir(u32 index) 152977d8d7a6SHerbert Xu { 153077d8d7a6SHerbert Xu return index & 7; 153177d8d7a6SHerbert Xu } 153277d8d7a6SHerbert Xu 1533a6483b79SAlexey Dobriyan #ifdef CONFIG_XFRM 1534a6483b79SAlexey Dobriyan static inline int xfrm_aevent_is_on(struct net *net) 1535f8cd5488SJamal Hadi Salim { 1536be33690dSPatrick McHardy struct sock *nlsk; 1537be33690dSPatrick McHardy int ret = 0; 1538be33690dSPatrick McHardy 1539be33690dSPatrick McHardy rcu_read_lock(); 1540a6483b79SAlexey Dobriyan nlsk = rcu_dereference(net->xfrm.nlsk); 1541be33690dSPatrick McHardy if (nlsk) 1542be33690dSPatrick McHardy ret = netlink_has_listeners(nlsk, XFRMNLGRP_AEVENTS); 1543be33690dSPatrick McHardy rcu_read_unlock(); 1544be33690dSPatrick McHardy return ret; 1545f8cd5488SJamal Hadi Salim } 1546a6483b79SAlexey Dobriyan #endif 1547f8cd5488SJamal Hadi Salim 15480f99be0dSEric Dumazet static inline int xfrm_alg_len(struct xfrm_algo *alg) 15490f99be0dSEric Dumazet { 15500f99be0dSEric Dumazet return sizeof(*alg) + ((alg->alg_key_len + 7) / 8); 15510f99be0dSEric Dumazet } 15520f99be0dSEric Dumazet 15534447bb33SMartin Willi static inline int xfrm_alg_auth_len(struct xfrm_algo_auth *alg) 15544447bb33SMartin Willi { 15554447bb33SMartin Willi return sizeof(*alg) + ((alg->alg_key_len + 7) / 8); 15564447bb33SMartin Willi } 15574447bb33SMartin Willi 155880c9abaaSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE 155980c9abaaSShinta Sugimoto static inline struct xfrm_algo *xfrm_algo_clone(struct xfrm_algo *orig) 156080c9abaaSShinta Sugimoto { 15610f99be0dSEric Dumazet return kmemdup(orig, xfrm_alg_len(orig), GFP_KERNEL); 156280c9abaaSShinta Sugimoto } 156380c9abaaSShinta Sugimoto 15644447bb33SMartin Willi static inline struct xfrm_algo_auth *xfrm_algo_auth_clone(struct xfrm_algo_auth *orig) 15654447bb33SMartin Willi { 15664447bb33SMartin Willi return kmemdup(orig, xfrm_alg_auth_len(orig), GFP_KERNEL); 15674447bb33SMartin Willi } 15684447bb33SMartin Willi 156980c9abaaSShinta Sugimoto static inline void xfrm_states_put(struct xfrm_state **states, int n) 157080c9abaaSShinta Sugimoto { 157180c9abaaSShinta Sugimoto int i; 157280c9abaaSShinta Sugimoto for (i = 0; i < n; i++) 157380c9abaaSShinta Sugimoto xfrm_state_put(*(states + i)); 157480c9abaaSShinta Sugimoto } 157580c9abaaSShinta Sugimoto 157680c9abaaSShinta Sugimoto static inline void xfrm_states_delete(struct xfrm_state **states, int n) 157780c9abaaSShinta Sugimoto { 157880c9abaaSShinta Sugimoto int i; 157980c9abaaSShinta Sugimoto for (i = 0; i < n; i++) 158080c9abaaSShinta Sugimoto xfrm_state_delete(*(states + i)); 158180c9abaaSShinta Sugimoto } 158280c9abaaSShinta Sugimoto #endif 1583f8cd5488SJamal Hadi Salim 1584def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM 158500501121SHerbert Xu static inline struct xfrm_state *xfrm_input_state(struct sk_buff *skb) 158600501121SHerbert Xu { 158700501121SHerbert Xu return skb->sp->xvec[skb->sp->len - 1]; 158800501121SHerbert Xu } 1589def8b4faSAlexey Dobriyan #endif 159000501121SHerbert Xu 1591bf825f81SJamal Hadi Salim static inline int xfrm_mark_get(struct nlattr **attrs, struct xfrm_mark *m) 1592bf825f81SJamal Hadi Salim { 1593bf825f81SJamal Hadi Salim if (attrs[XFRMA_MARK]) 15944efd7e83SAndreas Steffen memcpy(m, nla_data(attrs[XFRMA_MARK]), sizeof(struct xfrm_mark)); 1595bf825f81SJamal Hadi Salim else 1596bf825f81SJamal Hadi Salim m->v = m->m = 0; 1597bf825f81SJamal Hadi Salim 1598bf825f81SJamal Hadi Salim return m->v & m->m; 1599bf825f81SJamal Hadi Salim } 1600bf825f81SJamal Hadi Salim 1601bf825f81SJamal Hadi Salim static inline int xfrm_mark_put(struct sk_buff *skb, struct xfrm_mark *m) 1602bf825f81SJamal Hadi Salim { 1603bf825f81SJamal Hadi Salim if (m->m | m->v) 1604bf825f81SJamal Hadi Salim NLA_PUT(skb, XFRMA_MARK, sizeof(struct xfrm_mark), m); 1605bf825f81SJamal Hadi Salim return 0; 1606bf825f81SJamal Hadi Salim 1607bf825f81SJamal Hadi Salim nla_put_failure: 1608bf825f81SJamal Hadi Salim return -1; 1609bf825f81SJamal Hadi Salim } 1610bf825f81SJamal Hadi Salim 16111da177e4SLinus Torvalds #endif /* _NET_XFRM_H */ 1612