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, 2705e6b930fSDavid S. Miller const xfrm_address_t *saddr, 2715e6b930fSDavid S. Miller const 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, 2820c7b3eefSDavid S. Miller const 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); 287214e005bSDavid S. Miller extern void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c); 288214e005bSDavid S. Miller extern void km_state_notify(struct xfrm_state *x, const 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); 30373e5ebb2SDavid S. Miller void (*init_tempsel)(struct xfrm_selector *sel, 30473e5ebb2SDavid S. Miller const struct flowi *fl); 30519bd6244SDavid S. Miller void (*init_temprop)(struct xfrm_state *x, 30619bd6244SDavid S. Miller const struct xfrm_tmpl *tmpl, 30719bd6244SDavid S. Miller const xfrm_address_t *daddr, 30819bd6244SDavid S. Miller const xfrm_address_t *saddr); 30941a49cc3SMasahide NAKAMURA int (*tmpl_sort)(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n); 31041a49cc3SMasahide NAKAMURA int (*state_sort)(struct xfrm_state **dst, struct xfrm_state **src, int n); 311cdca7265SMiika Komu int (*output)(struct sk_buff *skb); 312227620e2SHerbert Xu int (*extract_input)(struct xfrm_state *x, 313227620e2SHerbert Xu struct sk_buff *skb); 31436cf9acfSHerbert Xu int (*extract_output)(struct xfrm_state *x, 31536cf9acfSHerbert Xu struct sk_buff *skb); 316716062fdSHerbert Xu int (*transport_finish)(struct sk_buff *skb, 317716062fdSHerbert Xu int async); 3181da177e4SLinus Torvalds }; 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds extern int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo); 3211da177e4SLinus Torvalds extern int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo); 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds extern void xfrm_state_delete_tunnel(struct xfrm_state *x); 3241da177e4SLinus Torvalds 325fd2c3ef7SEric Dumazet struct xfrm_type { 3261da177e4SLinus Torvalds char *description; 3271da177e4SLinus Torvalds struct module *owner; 328a6337463Sjamal u8 proto; 329a6337463Sjamal u8 flags; 3301b5c2299SMasahide NAKAMURA #define XFRM_TYPE_NON_FRAGMENT 1 331436a0a40SHerbert Xu #define XFRM_TYPE_REPLAY_PROT 2 332f04e7e8dSHerbert Xu #define XFRM_TYPE_LOCAL_COADDR 4 333f04e7e8dSHerbert Xu #define XFRM_TYPE_REMOTE_COADDR 8 3341da177e4SLinus Torvalds 33572cb6962SHerbert Xu int (*init_state)(struct xfrm_state *x); 3361da177e4SLinus Torvalds void (*destructor)(struct xfrm_state *); 337e695633eSHerbert Xu int (*input)(struct xfrm_state *, struct sk_buff *skb); 3381da177e4SLinus Torvalds int (*output)(struct xfrm_state *, struct sk_buff *pskb); 3398f029de2SDavid S. Miller int (*reject)(struct xfrm_state *, struct sk_buff *, 3408f029de2SDavid S. Miller const struct flowi *); 341aee5adb4SMasahide NAKAMURA int (*hdr_offset)(struct xfrm_state *, struct sk_buff *, u8 **); 3421da177e4SLinus Torvalds /* Estimate maximal size of result of transformation of a dgram */ 343c5c25238SPatrick McHardy u32 (*get_mtu)(struct xfrm_state *, int size); 3441da177e4SLinus Torvalds }; 3451da177e4SLinus Torvalds 346533cb5b0SEric Dumazet extern int xfrm_register_type(const struct xfrm_type *type, unsigned short family); 347533cb5b0SEric Dumazet extern int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family); 3481da177e4SLinus Torvalds 349b59f45d0SHerbert Xu struct xfrm_mode { 350227620e2SHerbert Xu /* 351227620e2SHerbert Xu * Remove encapsulation header. 352227620e2SHerbert Xu * 353227620e2SHerbert Xu * The IP header will be moved over the top of the encapsulation 354227620e2SHerbert Xu * header. 355227620e2SHerbert Xu * 356227620e2SHerbert Xu * On entry, the transport header shall point to where the IP header 357227620e2SHerbert Xu * should be and the network header shall be set to where the IP 358227620e2SHerbert Xu * header currently is. skb->data shall point to the start of the 359227620e2SHerbert Xu * payload. 360227620e2SHerbert Xu */ 361227620e2SHerbert Xu int (*input2)(struct xfrm_state *x, struct sk_buff *skb); 362227620e2SHerbert Xu 363227620e2SHerbert Xu /* 364227620e2SHerbert Xu * This is the actual input entry point. 365227620e2SHerbert Xu * 366227620e2SHerbert Xu * For transport mode and equivalent this would be identical to 367227620e2SHerbert Xu * input2 (which does not need to be set). While tunnel mode 368227620e2SHerbert Xu * and equivalent would set this to the tunnel encapsulation function 369227620e2SHerbert Xu * xfrm4_prepare_input that would in turn call input2. 370227620e2SHerbert Xu */ 371b59f45d0SHerbert Xu int (*input)(struct xfrm_state *x, struct sk_buff *skb); 37237fedd3aSHerbert Xu 37337fedd3aSHerbert Xu /* 37437fedd3aSHerbert Xu * Add encapsulation header. 37537fedd3aSHerbert Xu * 37637fedd3aSHerbert Xu * On exit, the transport header will be set to the start of the 37737fedd3aSHerbert Xu * encapsulation header to be filled in by x->type->output and 37837fedd3aSHerbert Xu * the mac header will be set to the nextheader (protocol for 37937fedd3aSHerbert Xu * IPv4) field of the extension header directly preceding the 38037fedd3aSHerbert Xu * encapsulation header, or in its absence, that of the top IP 38137fedd3aSHerbert Xu * header. The value of the network header will always point 38237fedd3aSHerbert Xu * to the top IP header while skb->data will point to the payload. 38337fedd3aSHerbert Xu */ 38436cf9acfSHerbert Xu int (*output2)(struct xfrm_state *x,struct sk_buff *skb); 38536cf9acfSHerbert Xu 38636cf9acfSHerbert Xu /* 38736cf9acfSHerbert Xu * This is the actual output entry point. 38836cf9acfSHerbert Xu * 38936cf9acfSHerbert Xu * For transport mode and equivalent this would be identical to 39036cf9acfSHerbert Xu * output2 (which does not need to be set). While tunnel mode 39136cf9acfSHerbert Xu * and equivalent would set this to a tunnel encapsulation function 39236cf9acfSHerbert Xu * (xfrm4_prepare_output or xfrm6_prepare_output) that would in turn 39336cf9acfSHerbert Xu * call output2. 39436cf9acfSHerbert Xu */ 395eb878e84SJamal Hadi Salim int (*output)(struct xfrm_state *x, struct sk_buff *skb); 396b59f45d0SHerbert Xu 39717c2a42aSHerbert Xu struct xfrm_state_afinfo *afinfo; 398b59f45d0SHerbert Xu struct module *owner; 399b59f45d0SHerbert Xu unsigned int encap; 4001bfcb10fSHerbert Xu int flags; 4011bfcb10fSHerbert Xu }; 4021bfcb10fSHerbert Xu 4031bfcb10fSHerbert Xu /* Flags for xfrm_mode. */ 4041bfcb10fSHerbert Xu enum { 4051bfcb10fSHerbert Xu XFRM_MODE_FLAG_TUNNEL = 1, 406b59f45d0SHerbert Xu }; 407b59f45d0SHerbert Xu 408b59f45d0SHerbert Xu extern int xfrm_register_mode(struct xfrm_mode *mode, int family); 409b59f45d0SHerbert Xu extern int xfrm_unregister_mode(struct xfrm_mode *mode, int family); 410b59f45d0SHerbert Xu 411df9dcb45SKazunori MIYAZAWA static inline int xfrm_af2proto(unsigned int family) 412df9dcb45SKazunori MIYAZAWA { 413df9dcb45SKazunori MIYAZAWA switch(family) { 414df9dcb45SKazunori MIYAZAWA case AF_INET: 415df9dcb45SKazunori MIYAZAWA return IPPROTO_IPIP; 416df9dcb45SKazunori MIYAZAWA case AF_INET6: 417df9dcb45SKazunori MIYAZAWA return IPPROTO_IPV6; 418df9dcb45SKazunori MIYAZAWA default: 419df9dcb45SKazunori MIYAZAWA return 0; 420df9dcb45SKazunori MIYAZAWA } 421df9dcb45SKazunori MIYAZAWA } 422df9dcb45SKazunori MIYAZAWA 423df9dcb45SKazunori MIYAZAWA static inline struct xfrm_mode *xfrm_ip2inner_mode(struct xfrm_state *x, int ipproto) 424df9dcb45SKazunori MIYAZAWA { 425df9dcb45SKazunori MIYAZAWA if ((ipproto == IPPROTO_IPIP && x->props.family == AF_INET) || 426df9dcb45SKazunori MIYAZAWA (ipproto == IPPROTO_IPV6 && x->props.family == AF_INET6)) 427df9dcb45SKazunori MIYAZAWA return x->inner_mode; 428df9dcb45SKazunori MIYAZAWA else 429df9dcb45SKazunori MIYAZAWA return x->inner_mode_iaf; 430df9dcb45SKazunori MIYAZAWA } 431df9dcb45SKazunori MIYAZAWA 432fd2c3ef7SEric Dumazet struct xfrm_tmpl { 4331da177e4SLinus Torvalds /* id in template is interpreted as: 4341da177e4SLinus Torvalds * daddr - destination of tunnel, may be zero for transport mode. 4351da177e4SLinus Torvalds * spi - zero to acquire spi. Not zero if spi is static, then 4361da177e4SLinus Torvalds * daddr must be fixed too. 4371da177e4SLinus Torvalds * proto - AH/ESP/IPCOMP 4381da177e4SLinus Torvalds */ 4391da177e4SLinus Torvalds struct xfrm_id id; 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds /* Source address of tunnel. Ignored, if it is not a tunnel. */ 4421da177e4SLinus Torvalds xfrm_address_t saddr; 4431da177e4SLinus Torvalds 44476b3f055SMiika Komu unsigned short encap_family; 44576b3f055SMiika Komu 446a6337463Sjamal u32 reqid; 4471da177e4SLinus Torvalds 4487e49e6deSMasahide NAKAMURA /* Mode: transport, tunnel etc. */ 449a6337463Sjamal u8 mode; 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds /* Sharing mode: unique, this session only, this user only etc. */ 452a6337463Sjamal u8 share; 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds /* May skip this transfomration if no SA is found */ 455a6337463Sjamal u8 optional; 4561da177e4SLinus Torvalds 457c5d18e98SHerbert Xu /* Skip aalgos/ealgos/calgos checks. */ 458a6337463Sjamal u8 allalgs; 459c5d18e98SHerbert Xu 4601da177e4SLinus Torvalds /* Bit mask of algos allowed for acquisition */ 461a6337463Sjamal u32 aalgos; 462a6337463Sjamal u32 ealgos; 463a6337463Sjamal u32 calgos; 4641da177e4SLinus Torvalds }; 4651da177e4SLinus Torvalds 466622dc828SMasahide NAKAMURA #define XFRM_MAX_DEPTH 6 4671da177e4SLinus Torvalds 46812a169e7SHerbert Xu struct xfrm_policy_walk_entry { 46912a169e7SHerbert Xu struct list_head all; 47012a169e7SHerbert Xu u8 dead; 47112a169e7SHerbert Xu }; 47212a169e7SHerbert Xu 47312a169e7SHerbert Xu struct xfrm_policy_walk { 47412a169e7SHerbert Xu struct xfrm_policy_walk_entry walk; 47512a169e7SHerbert Xu u8 type; 47612a169e7SHerbert Xu u32 seq; 47712a169e7SHerbert Xu }; 47812a169e7SHerbert Xu 479fd2c3ef7SEric Dumazet struct xfrm_policy { 4800331b1f3SAlexey Dobriyan #ifdef CONFIG_NET_NS 4810331b1f3SAlexey Dobriyan struct net *xp_net; 4820331b1f3SAlexey Dobriyan #endif 4832518c7c2SDavid S. Miller struct hlist_node bydst; 4842518c7c2SDavid S. Miller struct hlist_node byidx; 4851da177e4SLinus Torvalds 4861da177e4SLinus Torvalds /* This lock only affects elements except for entry. */ 4871da177e4SLinus Torvalds rwlock_t lock; 4881da177e4SLinus Torvalds atomic_t refcnt; 4891da177e4SLinus Torvalds struct timer_list timer; 4901da177e4SLinus Torvalds 491fe1a5f03STimo Teräs struct flow_cache_object flo; 49280c802f3STimo Teräs atomic_t genid; 4931da177e4SLinus Torvalds u32 priority; 4941da177e4SLinus Torvalds u32 index; 495bf825f81SJamal Hadi Salim struct xfrm_mark mark; 4961da177e4SLinus Torvalds struct xfrm_selector selector; 4971da177e4SLinus Torvalds struct xfrm_lifetime_cfg lft; 4981da177e4SLinus Torvalds struct xfrm_lifetime_cur curlft; 49912a169e7SHerbert Xu struct xfrm_policy_walk_entry walk; 50046ca5f5dSArnaldo Carvalho de Melo u8 type; 50146ca5f5dSArnaldo Carvalho de Melo u8 action; 50246ca5f5dSArnaldo Carvalho de Melo u8 flags; 50346ca5f5dSArnaldo Carvalho de Melo u8 xfrm_nr; 50412a169e7SHerbert Xu u16 family; 505df71837dSTrent Jaeger struct xfrm_sec_ctx *security; 5061da177e4SLinus Torvalds struct xfrm_tmpl xfrm_vec[XFRM_MAX_DEPTH]; 5071da177e4SLinus Torvalds }; 5081da177e4SLinus Torvalds 50963eb23f5SDavid S. Miller static inline struct net *xp_net(const struct xfrm_policy *xp) 5100331b1f3SAlexey Dobriyan { 5110331b1f3SAlexey Dobriyan return read_pnet(&xp->xp_net); 5120331b1f3SAlexey Dobriyan } 5130331b1f3SAlexey Dobriyan 51413c1d189SArnaud Ebalard struct xfrm_kmaddress { 51513c1d189SArnaud Ebalard xfrm_address_t local; 51613c1d189SArnaud Ebalard xfrm_address_t remote; 51713c1d189SArnaud Ebalard u32 reserved; 51813c1d189SArnaud Ebalard u16 family; 51913c1d189SArnaud Ebalard }; 52013c1d189SArnaud Ebalard 52180c9abaaSShinta Sugimoto struct xfrm_migrate { 52280c9abaaSShinta Sugimoto xfrm_address_t old_daddr; 52380c9abaaSShinta Sugimoto xfrm_address_t old_saddr; 52480c9abaaSShinta Sugimoto xfrm_address_t new_daddr; 52580c9abaaSShinta Sugimoto xfrm_address_t new_saddr; 52680c9abaaSShinta Sugimoto u8 proto; 52780c9abaaSShinta Sugimoto u8 mode; 52880c9abaaSShinta Sugimoto u16 reserved; 52980c9abaaSShinta Sugimoto u32 reqid; 53080c9abaaSShinta Sugimoto u16 old_family; 53180c9abaaSShinta Sugimoto u16 new_family; 53280c9abaaSShinta Sugimoto }; 53380c9abaaSShinta Sugimoto 5341da177e4SLinus Torvalds #define XFRM_KM_TIMEOUT 30 535f8cd5488SJamal Hadi Salim /* which seqno */ 536f8cd5488SJamal Hadi Salim #define XFRM_REPLAY_SEQ 1 537f8cd5488SJamal Hadi Salim #define XFRM_REPLAY_OSEQ 2 538f8cd5488SJamal Hadi Salim #define XFRM_REPLAY_SEQ_MASK 3 539f8cd5488SJamal Hadi Salim /* what happened */ 540f8cd5488SJamal Hadi Salim #define XFRM_REPLAY_UPDATE XFRM_AE_CR 541f8cd5488SJamal Hadi Salim #define XFRM_REPLAY_TIMEOUT XFRM_AE_CE 542f8cd5488SJamal Hadi Salim 543f8cd5488SJamal Hadi Salim /* default aevent timeout in units of 100ms */ 544f8cd5488SJamal Hadi Salim #define XFRM_AE_ETIME 10 545f8cd5488SJamal Hadi Salim /* Async Event timer multiplier */ 546f8cd5488SJamal Hadi Salim #define XFRM_AE_ETH_M 10 547f8cd5488SJamal Hadi Salim /* default seq threshold size */ 548f8cd5488SJamal Hadi Salim #define XFRM_AE_SEQT_SIZE 2 5491da177e4SLinus Torvalds 550fd2c3ef7SEric Dumazet struct xfrm_mgr { 5511da177e4SLinus Torvalds struct list_head list; 5521da177e4SLinus Torvalds char *id; 553214e005bSDavid S. Miller int (*notify)(struct xfrm_state *x, const struct km_event *c); 5541da177e4SLinus Torvalds int (*acquire)(struct xfrm_state *x, struct xfrm_tmpl *, struct xfrm_policy *xp, int dir); 555cb969f07SVenkat Yekkirala struct xfrm_policy *(*compile_policy)(struct sock *sk, int opt, u8 *data, int len, int *dir); 5565d36b180SAl Viro int (*new_mapping)(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport); 557214e005bSDavid S. Miller int (*notify_policy)(struct xfrm_policy *x, int dir, const struct km_event *c); 558db983c11SAlexey Dobriyan int (*report)(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr); 559183cad12SDavid S. Miller int (*migrate)(const struct xfrm_selector *sel, 560183cad12SDavid S. Miller u8 dir, u8 type, 561183cad12SDavid S. Miller const struct xfrm_migrate *m, 562183cad12SDavid S. Miller int num_bundles, 563183cad12SDavid S. Miller const struct xfrm_kmaddress *k); 5641da177e4SLinus Torvalds }; 5651da177e4SLinus Torvalds 5661da177e4SLinus Torvalds extern int xfrm_register_km(struct xfrm_mgr *km); 5671da177e4SLinus Torvalds extern int xfrm_unregister_km(struct xfrm_mgr *km); 5681da177e4SLinus Torvalds 569436a0a40SHerbert Xu /* 570436a0a40SHerbert Xu * This structure is used for the duration where packets are being 571436a0a40SHerbert Xu * transformed by IPsec. As soon as the packet leaves IPsec the 572436a0a40SHerbert Xu * area beyond the generic IP part may be overwritten. 573436a0a40SHerbert Xu */ 574436a0a40SHerbert Xu struct xfrm_skb_cb { 575436a0a40SHerbert Xu union { 576436a0a40SHerbert Xu struct inet_skb_parm h4; 577436a0a40SHerbert Xu struct inet6_skb_parm h6; 578436a0a40SHerbert Xu } header; 579436a0a40SHerbert Xu 580436a0a40SHerbert Xu /* Sequence number for replay protection. */ 581b318e0e4SHerbert Xu union { 582b318e0e4SHerbert Xu u64 output; 583b318e0e4SHerbert Xu __be32 input; 584b318e0e4SHerbert Xu } seq; 585436a0a40SHerbert Xu }; 586436a0a40SHerbert Xu 587436a0a40SHerbert Xu #define XFRM_SKB_CB(__skb) ((struct xfrm_skb_cb *)&((__skb)->cb[0])) 588436a0a40SHerbert Xu 58936cf9acfSHerbert Xu /* 59036cf9acfSHerbert Xu * This structure is used by the afinfo prepare_input/prepare_output functions 59136cf9acfSHerbert Xu * to transmit header information to the mode input/output functions. 59236cf9acfSHerbert Xu */ 59336cf9acfSHerbert Xu struct xfrm_mode_skb_cb { 59436cf9acfSHerbert Xu union { 59536cf9acfSHerbert Xu struct inet_skb_parm h4; 59636cf9acfSHerbert Xu struct inet6_skb_parm h6; 59736cf9acfSHerbert Xu } header; 59836cf9acfSHerbert Xu 59936cf9acfSHerbert Xu /* Copied from header for IPv4, always set to zero and DF for IPv6. */ 60036cf9acfSHerbert Xu __be16 id; 60136cf9acfSHerbert Xu __be16 frag_off; 60236cf9acfSHerbert Xu 603732c8bd5SHerbert Xu /* IP header length (excluding options or extension headers). */ 604732c8bd5SHerbert Xu u8 ihl; 605732c8bd5SHerbert Xu 60636cf9acfSHerbert Xu /* TOS for IPv4, class for IPv6. */ 60736cf9acfSHerbert Xu u8 tos; 60836cf9acfSHerbert Xu 60936cf9acfSHerbert Xu /* TTL for IPv4, hop limitfor IPv6. */ 61036cf9acfSHerbert Xu u8 ttl; 61136cf9acfSHerbert Xu 61236cf9acfSHerbert Xu /* Protocol for IPv4, NH for IPv6. */ 61336cf9acfSHerbert Xu u8 protocol; 61436cf9acfSHerbert Xu 615732c8bd5SHerbert Xu /* Option length for IPv4, zero for IPv6. */ 616732c8bd5SHerbert Xu u8 optlen; 617732c8bd5SHerbert Xu 61836cf9acfSHerbert Xu /* Used by IPv6 only, zero for IPv4. */ 61936cf9acfSHerbert Xu u8 flow_lbl[3]; 62036cf9acfSHerbert Xu }; 62136cf9acfSHerbert Xu 62236cf9acfSHerbert Xu #define XFRM_MODE_SKB_CB(__skb) ((struct xfrm_mode_skb_cb *)&((__skb)->cb[0])) 62336cf9acfSHerbert Xu 624716062fdSHerbert Xu /* 625716062fdSHerbert Xu * This structure is used by the input processing to locate the SPI and 626716062fdSHerbert Xu * related information. 627716062fdSHerbert Xu */ 628716062fdSHerbert Xu struct xfrm_spi_skb_cb { 629716062fdSHerbert Xu union { 630716062fdSHerbert Xu struct inet_skb_parm h4; 631716062fdSHerbert Xu struct inet6_skb_parm h6; 632716062fdSHerbert Xu } header; 633716062fdSHerbert Xu 634716062fdSHerbert Xu unsigned int daddroff; 6352fcb45b6SHerbert Xu unsigned int family; 636716062fdSHerbert Xu }; 637716062fdSHerbert Xu 638716062fdSHerbert Xu #define XFRM_SPI_SKB_CB(__skb) ((struct xfrm_spi_skb_cb *)&((__skb)->cb[0])) 639716062fdSHerbert Xu 640161a09e7SJoy Latten /* Audit Information */ 641fd2c3ef7SEric Dumazet struct xfrm_audit { 642161a09e7SJoy Latten u32 secid; 6432532386fSEric Paris uid_t loginuid; 6442532386fSEric Paris u32 sessionid; 645161a09e7SJoy Latten }; 646c9204d9cSJoy Latten 647c9204d9cSJoy Latten #ifdef CONFIG_AUDITSYSCALL 648afeb14b4SPaul Moore static inline struct audit_buffer *xfrm_audit_start(const char *op) 649ab5f5e8bSJoy Latten { 650ab5f5e8bSJoy Latten struct audit_buffer *audit_buf = NULL; 651ab5f5e8bSJoy Latten 652afeb14b4SPaul Moore if (audit_enabled == 0) 653afeb14b4SPaul Moore return NULL; 654ab5f5e8bSJoy Latten audit_buf = audit_log_start(current->audit_context, GFP_ATOMIC, 655ab5f5e8bSJoy Latten AUDIT_MAC_IPSEC_EVENT); 656ab5f5e8bSJoy Latten if (audit_buf == NULL) 657ab5f5e8bSJoy Latten return NULL; 658afeb14b4SPaul Moore audit_log_format(audit_buf, "op=%s", op); 659afeb14b4SPaul Moore return audit_buf; 660afeb14b4SPaul Moore } 661afeb14b4SPaul Moore 6622532386fSEric Paris static inline void xfrm_audit_helper_usrinfo(uid_t auid, u32 ses, u32 secid, 663afeb14b4SPaul Moore struct audit_buffer *audit_buf) 664afeb14b4SPaul Moore { 665afeb14b4SPaul Moore char *secctx; 666afeb14b4SPaul Moore u32 secctx_len; 667ab5f5e8bSJoy Latten 6682532386fSEric Paris audit_log_format(audit_buf, " auid=%u ses=%u", auid, ses); 66968277accSPaul Moore if (secid != 0 && 67068277accSPaul Moore security_secid_to_secctx(secid, &secctx, &secctx_len) == 0) { 671ab5f5e8bSJoy Latten audit_log_format(audit_buf, " subj=%s", secctx); 672ab5f5e8bSJoy Latten security_release_secctx(secctx, secctx_len); 673ab5f5e8bSJoy Latten } else 674ab5f5e8bSJoy Latten audit_log_task_context(audit_buf); 675ab5f5e8bSJoy Latten } 676ab5f5e8bSJoy Latten 677ab5f5e8bSJoy Latten extern void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, 6782532386fSEric Paris u32 auid, u32 ses, u32 secid); 679ab5f5e8bSJoy Latten extern void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result, 6802532386fSEric Paris u32 auid, u32 ses, u32 secid); 681ab5f5e8bSJoy Latten extern void xfrm_audit_state_add(struct xfrm_state *x, int result, 6822532386fSEric Paris u32 auid, u32 ses, u32 secid); 683ab5f5e8bSJoy Latten extern void xfrm_audit_state_delete(struct xfrm_state *x, int result, 6842532386fSEric Paris u32 auid, u32 ses, u32 secid); 685afeb14b4SPaul Moore extern void xfrm_audit_state_replay_overflow(struct xfrm_state *x, 686afeb14b4SPaul Moore struct sk_buff *skb); 687afeb14b4SPaul Moore extern void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family); 688afeb14b4SPaul Moore extern void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family, 689afeb14b4SPaul Moore __be32 net_spi, __be32 net_seq); 690afeb14b4SPaul Moore extern void xfrm_audit_state_icvfail(struct xfrm_state *x, 691afeb14b4SPaul Moore struct sk_buff *skb, u8 proto); 692c9204d9cSJoy Latten #else 69341fef0eeSMarcin Slusarz 69441fef0eeSMarcin Slusarz static inline void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, 69541fef0eeSMarcin Slusarz u32 auid, u32 ses, u32 secid) 69641fef0eeSMarcin Slusarz { 69741fef0eeSMarcin Slusarz } 69841fef0eeSMarcin Slusarz 69941fef0eeSMarcin Slusarz static inline void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result, 70041fef0eeSMarcin Slusarz u32 auid, u32 ses, u32 secid) 70141fef0eeSMarcin Slusarz { 70241fef0eeSMarcin Slusarz } 70341fef0eeSMarcin Slusarz 70441fef0eeSMarcin Slusarz static inline void xfrm_audit_state_add(struct xfrm_state *x, int result, 70541fef0eeSMarcin Slusarz u32 auid, u32 ses, u32 secid) 70641fef0eeSMarcin Slusarz { 70741fef0eeSMarcin Slusarz } 70841fef0eeSMarcin Slusarz 70941fef0eeSMarcin Slusarz static inline void xfrm_audit_state_delete(struct xfrm_state *x, int result, 71041fef0eeSMarcin Slusarz u32 auid, u32 ses, u32 secid) 71141fef0eeSMarcin Slusarz { 71241fef0eeSMarcin Slusarz } 71341fef0eeSMarcin Slusarz 71441fef0eeSMarcin Slusarz static inline void xfrm_audit_state_replay_overflow(struct xfrm_state *x, 71541fef0eeSMarcin Slusarz struct sk_buff *skb) 71641fef0eeSMarcin Slusarz { 71741fef0eeSMarcin Slusarz } 71841fef0eeSMarcin Slusarz 71941fef0eeSMarcin Slusarz static inline void xfrm_audit_state_notfound_simple(struct sk_buff *skb, 72041fef0eeSMarcin Slusarz u16 family) 72141fef0eeSMarcin Slusarz { 72241fef0eeSMarcin Slusarz } 72341fef0eeSMarcin Slusarz 72441fef0eeSMarcin Slusarz static inline void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family, 72541fef0eeSMarcin Slusarz __be32 net_spi, __be32 net_seq) 72641fef0eeSMarcin Slusarz { 72741fef0eeSMarcin Slusarz } 72841fef0eeSMarcin Slusarz 72941fef0eeSMarcin Slusarz static inline void xfrm_audit_state_icvfail(struct xfrm_state *x, 73041fef0eeSMarcin Slusarz struct sk_buff *skb, u8 proto) 73141fef0eeSMarcin Slusarz { 73241fef0eeSMarcin Slusarz } 733c9204d9cSJoy Latten #endif /* CONFIG_AUDITSYSCALL */ 734161a09e7SJoy Latten 7351da177e4SLinus Torvalds static inline void xfrm_pol_hold(struct xfrm_policy *policy) 7361da177e4SLinus Torvalds { 7371da177e4SLinus Torvalds if (likely(policy != NULL)) 7381da177e4SLinus Torvalds atomic_inc(&policy->refcnt); 7391da177e4SLinus Torvalds } 7401da177e4SLinus Torvalds 74164c31b3fSWANG Cong extern void xfrm_policy_destroy(struct xfrm_policy *policy); 7421da177e4SLinus Torvalds 7431da177e4SLinus Torvalds static inline void xfrm_pol_put(struct xfrm_policy *policy) 7441da177e4SLinus Torvalds { 7451da177e4SLinus Torvalds if (atomic_dec_and_test(&policy->refcnt)) 74664c31b3fSWANG Cong xfrm_policy_destroy(policy); 7471da177e4SLinus Torvalds } 7481da177e4SLinus Torvalds 7494e81bb83SMasahide NAKAMURA static inline void xfrm_pols_put(struct xfrm_policy **pols, int npols) 7504e81bb83SMasahide NAKAMURA { 7514e81bb83SMasahide NAKAMURA int i; 7524e81bb83SMasahide NAKAMURA for (i = npols - 1; i >= 0; --i) 7534e81bb83SMasahide NAKAMURA xfrm_pol_put(pols[i]); 7544e81bb83SMasahide NAKAMURA } 7554e81bb83SMasahide NAKAMURA 7561da177e4SLinus Torvalds extern void __xfrm_state_destroy(struct xfrm_state *); 7571da177e4SLinus Torvalds 75821380b81SHerbert Xu static inline void __xfrm_state_put(struct xfrm_state *x) 75921380b81SHerbert Xu { 76021380b81SHerbert Xu atomic_dec(&x->refcnt); 76121380b81SHerbert Xu } 76221380b81SHerbert Xu 7631da177e4SLinus Torvalds static inline void xfrm_state_put(struct xfrm_state *x) 7641da177e4SLinus Torvalds { 7651da177e4SLinus Torvalds if (atomic_dec_and_test(&x->refcnt)) 7661da177e4SLinus Torvalds __xfrm_state_destroy(x); 7671da177e4SLinus Torvalds } 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds static inline void xfrm_state_hold(struct xfrm_state *x) 7701da177e4SLinus Torvalds { 7711da177e4SLinus Torvalds atomic_inc(&x->refcnt); 7721da177e4SLinus Torvalds } 7731da177e4SLinus Torvalds 7741744a8feSDavid S. Miller static inline bool addr_match(const void *token1, const void *token2, 7751744a8feSDavid S. Miller int prefixlen) 7761da177e4SLinus Torvalds { 7771744a8feSDavid S. Miller const __be32 *a1 = token1; 7781744a8feSDavid S. Miller const __be32 *a2 = token2; 7791da177e4SLinus Torvalds int pdw; 7801da177e4SLinus Torvalds int pbi; 7811da177e4SLinus Torvalds 782a6337463Sjamal pdw = prefixlen >> 5; /* num of whole u32 in prefix */ 7831da177e4SLinus Torvalds pbi = prefixlen & 0x1f; /* num of bits in incomplete u32 in prefix */ 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds if (pdw) 7861da177e4SLinus Torvalds if (memcmp(a1, a2, pdw << 2)) 7871744a8feSDavid S. Miller return false; 7881da177e4SLinus Torvalds 7891da177e4SLinus Torvalds if (pbi) { 7905f19343fSAl Viro __be32 mask; 7911da177e4SLinus Torvalds 7921da177e4SLinus Torvalds mask = htonl((0xffffffff) << (32 - pbi)); 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds if ((a1[pdw] ^ a2[pdw]) & mask) 7951744a8feSDavid S. Miller return false; 7961da177e4SLinus Torvalds } 7971da177e4SLinus Torvalds 7981744a8feSDavid S. Miller return true; 7991da177e4SLinus Torvalds } 8001da177e4SLinus Torvalds 8011da177e4SLinus Torvalds static __inline__ 802e8a4e377SDavid S. Miller __be16 xfrm_flowi_sport(const struct flowi *fl) 8031da177e4SLinus Torvalds { 804f9d07e41SAl Viro __be16 port; 8051da177e4SLinus Torvalds switch(fl->proto) { 8061da177e4SLinus Torvalds case IPPROTO_TCP: 8071da177e4SLinus Torvalds case IPPROTO_UDP: 808ba4e58ecSGerrit Renker case IPPROTO_UDPLITE: 8091da177e4SLinus Torvalds case IPPROTO_SCTP: 8101da177e4SLinus Torvalds port = fl->fl_ip_sport; 8111da177e4SLinus Torvalds break; 8121da177e4SLinus Torvalds case IPPROTO_ICMP: 8131da177e4SLinus Torvalds case IPPROTO_ICMPV6: 8141da177e4SLinus Torvalds port = htons(fl->fl_icmp_type); 8151da177e4SLinus Torvalds break; 8162ce4272aSMasahide NAKAMURA case IPPROTO_MH: 8172ce4272aSMasahide NAKAMURA port = htons(fl->fl_mh_type); 8182ce4272aSMasahide NAKAMURA break; 819cc9ff19dSTimo Teräs case IPPROTO_GRE: 820aa285b17STimo Teräs port = htons(ntohl(fl->fl_gre_key) >> 16); 821cc9ff19dSTimo Teräs break; 8221da177e4SLinus Torvalds default: 8231da177e4SLinus Torvalds port = 0; /*XXX*/ 8241da177e4SLinus Torvalds } 8251da177e4SLinus Torvalds return port; 8261da177e4SLinus Torvalds } 8271da177e4SLinus Torvalds 8281da177e4SLinus Torvalds static __inline__ 829e8a4e377SDavid S. Miller __be16 xfrm_flowi_dport(const struct flowi *fl) 8301da177e4SLinus Torvalds { 831f9d07e41SAl Viro __be16 port; 8321da177e4SLinus Torvalds switch(fl->proto) { 8331da177e4SLinus Torvalds case IPPROTO_TCP: 8341da177e4SLinus Torvalds case IPPROTO_UDP: 835ba4e58ecSGerrit Renker case IPPROTO_UDPLITE: 8361da177e4SLinus Torvalds case IPPROTO_SCTP: 8371da177e4SLinus Torvalds port = fl->fl_ip_dport; 8381da177e4SLinus Torvalds break; 8391da177e4SLinus Torvalds case IPPROTO_ICMP: 8401da177e4SLinus Torvalds case IPPROTO_ICMPV6: 8411da177e4SLinus Torvalds port = htons(fl->fl_icmp_code); 8421da177e4SLinus Torvalds break; 843cc9ff19dSTimo Teräs case IPPROTO_GRE: 844aa285b17STimo Teräs port = htons(ntohl(fl->fl_gre_key) & 0xffff); 845cc9ff19dSTimo Teräs break; 8461da177e4SLinus Torvalds default: 8471da177e4SLinus Torvalds port = 0; /*XXX*/ 8481da177e4SLinus Torvalds } 8491da177e4SLinus Torvalds return port; 8501da177e4SLinus Torvalds } 8511da177e4SLinus Torvalds 852200ce96eSDavid S. Miller extern int xfrm_selector_match(const struct xfrm_selector *sel, 853e1ad2ab2SDavid S. Miller const struct flowi *fl, 85477681021SAndrew Morton unsigned short family); 8551da177e4SLinus Torvalds 856df71837dSTrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM 857df71837dSTrent Jaeger /* If neither has a context --> match 858df71837dSTrent Jaeger * Otherwise, both must have a context and the sids, doi, alg must match 859df71837dSTrent Jaeger */ 860df71837dSTrent Jaeger static inline int xfrm_sec_ctx_match(struct xfrm_sec_ctx *s1, struct xfrm_sec_ctx *s2) 861df71837dSTrent Jaeger { 862df71837dSTrent Jaeger return ((!s1 && !s2) || 863df71837dSTrent Jaeger (s1 && s2 && 864df71837dSTrent Jaeger (s1->ctx_sid == s2->ctx_sid) && 865df71837dSTrent Jaeger (s1->ctx_doi == s2->ctx_doi) && 866df71837dSTrent Jaeger (s1->ctx_alg == s2->ctx_alg))); 867df71837dSTrent Jaeger } 868df71837dSTrent Jaeger #else 869df71837dSTrent Jaeger static inline int xfrm_sec_ctx_match(struct xfrm_sec_ctx *s1, struct xfrm_sec_ctx *s2) 870df71837dSTrent Jaeger { 871df71837dSTrent Jaeger return 1; 872df71837dSTrent Jaeger } 873df71837dSTrent Jaeger #endif 874df71837dSTrent Jaeger 8751da177e4SLinus Torvalds /* A struct encoding bundle of transformations to apply to some set of flow. 8761da177e4SLinus Torvalds * 8771da177e4SLinus Torvalds * dst->child points to the next element of bundle. 8781da177e4SLinus Torvalds * dst->xfrm points to an instanse of transformer. 8791da177e4SLinus Torvalds * 8801da177e4SLinus Torvalds * Due to unfortunate limitations of current routing cache, which we 8811da177e4SLinus Torvalds * have no time to fix, it mirrors struct rtable and bound to the same 8821da177e4SLinus Torvalds * routing key, including saddr,daddr. However, we can have many of 8831da177e4SLinus Torvalds * bundles differing by session id. All the bundles grow from a parent 8841da177e4SLinus Torvalds * policy rule. 8851da177e4SLinus Torvalds */ 886fd2c3ef7SEric Dumazet struct xfrm_dst { 8871da177e4SLinus Torvalds union { 8881da177e4SLinus Torvalds struct dst_entry dst; 8891da177e4SLinus Torvalds struct rtable rt; 8901da177e4SLinus Torvalds struct rt6_info rt6; 8911da177e4SLinus Torvalds } u; 8921da177e4SLinus Torvalds struct dst_entry *route; 89380c802f3STimo Teräs struct flow_cache_object flo; 89480c802f3STimo Teräs struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX]; 89580c802f3STimo Teräs int num_pols, num_xfrms; 896157bfc25SMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY 897157bfc25SMasahide NAKAMURA struct flowi *origin; 898157bfc25SMasahide NAKAMURA struct xfrm_selector *partner; 899157bfc25SMasahide NAKAMURA #endif 90080c802f3STimo Teräs u32 xfrm_genid; 90180c802f3STimo Teräs u32 policy_genid; 9021da177e4SLinus Torvalds u32 route_mtu_cached; 9031da177e4SLinus Torvalds u32 child_mtu_cached; 90492d63decSHideaki YOSHIFUJI u32 route_cookie; 90592d63decSHideaki YOSHIFUJI u32 path_cookie; 9061da177e4SLinus Torvalds }; 9071da177e4SLinus Torvalds 908def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM 909aabc9761SHerbert Xu static inline void xfrm_dst_destroy(struct xfrm_dst *xdst) 910aabc9761SHerbert Xu { 91180c802f3STimo Teräs xfrm_pols_put(xdst->pols, xdst->num_pols); 912aabc9761SHerbert Xu dst_release(xdst->route); 913aabc9761SHerbert Xu if (likely(xdst->u.dst.xfrm)) 914aabc9761SHerbert Xu xfrm_state_put(xdst->u.dst.xfrm); 915157bfc25SMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY 916157bfc25SMasahide NAKAMURA kfree(xdst->origin); 917157bfc25SMasahide NAKAMURA xdst->origin = NULL; 918157bfc25SMasahide NAKAMURA kfree(xdst->partner); 919157bfc25SMasahide NAKAMURA xdst->partner = NULL; 920157bfc25SMasahide NAKAMURA #endif 921aabc9761SHerbert Xu } 922def8b4faSAlexey Dobriyan #endif 923aabc9761SHerbert Xu 924aabc9761SHerbert Xu extern void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev); 925aabc9761SHerbert Xu 926fd2c3ef7SEric Dumazet struct sec_path { 9271da177e4SLinus Torvalds atomic_t refcnt; 9281da177e4SLinus Torvalds int len; 929dbe5b4aaSHerbert Xu struct xfrm_state *xvec[XFRM_MAX_DEPTH]; 9301da177e4SLinus Torvalds }; 9311da177e4SLinus Torvalds 9321da177e4SLinus Torvalds static inline struct sec_path * 9331da177e4SLinus Torvalds secpath_get(struct sec_path *sp) 9341da177e4SLinus Torvalds { 9351da177e4SLinus Torvalds if (sp) 9361da177e4SLinus Torvalds atomic_inc(&sp->refcnt); 9371da177e4SLinus Torvalds return sp; 9381da177e4SLinus Torvalds } 9391da177e4SLinus Torvalds 9401da177e4SLinus Torvalds extern void __secpath_destroy(struct sec_path *sp); 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds static inline void 9431da177e4SLinus Torvalds secpath_put(struct sec_path *sp) 9441da177e4SLinus Torvalds { 9451da177e4SLinus Torvalds if (sp && atomic_dec_and_test(&sp->refcnt)) 9461da177e4SLinus Torvalds __secpath_destroy(sp); 9471da177e4SLinus Torvalds } 9481da177e4SLinus Torvalds 9491da177e4SLinus Torvalds extern struct sec_path *secpath_dup(struct sec_path *src); 9501da177e4SLinus Torvalds 9511da177e4SLinus Torvalds static inline void 9521da177e4SLinus Torvalds secpath_reset(struct sk_buff *skb) 9531da177e4SLinus Torvalds { 9541da177e4SLinus Torvalds #ifdef CONFIG_XFRM 9551da177e4SLinus Torvalds secpath_put(skb->sp); 9561da177e4SLinus Torvalds skb->sp = NULL; 9571da177e4SLinus Torvalds #endif 9581da177e4SLinus Torvalds } 9591da177e4SLinus Torvalds 9601da177e4SLinus Torvalds static inline int 9616cc32961SDavid S. Miller xfrm_addr_any(const xfrm_address_t *addr, unsigned short family) 962a1e59abfSPatrick McHardy { 963a1e59abfSPatrick McHardy switch (family) { 964a1e59abfSPatrick McHardy case AF_INET: 965a1e59abfSPatrick McHardy return addr->a4 == 0; 966a1e59abfSPatrick McHardy case AF_INET6: 967a1e59abfSPatrick McHardy return ipv6_addr_any((struct in6_addr *)&addr->a6); 968a1e59abfSPatrick McHardy } 969a1e59abfSPatrick McHardy return 0; 970a1e59abfSPatrick McHardy } 971a1e59abfSPatrick McHardy 972a1e59abfSPatrick McHardy static inline int 97321eddb5cSDavid S. Miller __xfrm4_state_addr_cmp(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x) 9741da177e4SLinus Torvalds { 9751da177e4SLinus Torvalds return (tmpl->saddr.a4 && 9761da177e4SLinus Torvalds tmpl->saddr.a4 != x->props.saddr.a4); 9771da177e4SLinus Torvalds } 9781da177e4SLinus Torvalds 9791da177e4SLinus Torvalds static inline int 98021eddb5cSDavid S. Miller __xfrm6_state_addr_cmp(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x) 9811da177e4SLinus Torvalds { 9821da177e4SLinus Torvalds return (!ipv6_addr_any((struct in6_addr*)&tmpl->saddr) && 9831da177e4SLinus Torvalds ipv6_addr_cmp((struct in6_addr *)&tmpl->saddr, (struct in6_addr*)&x->props.saddr)); 9841da177e4SLinus Torvalds } 9851da177e4SLinus Torvalds 9861da177e4SLinus Torvalds static inline int 98721eddb5cSDavid S. Miller xfrm_state_addr_cmp(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x, unsigned short family) 9881da177e4SLinus Torvalds { 9891da177e4SLinus Torvalds switch (family) { 9901da177e4SLinus Torvalds case AF_INET: 9911da177e4SLinus Torvalds return __xfrm4_state_addr_cmp(tmpl, x); 9921da177e4SLinus Torvalds case AF_INET6: 9931da177e4SLinus Torvalds return __xfrm6_state_addr_cmp(tmpl, x); 9941da177e4SLinus Torvalds } 9951da177e4SLinus Torvalds return !0; 9961da177e4SLinus Torvalds } 9971da177e4SLinus Torvalds 9981da177e4SLinus Torvalds #ifdef CONFIG_XFRM 9991da177e4SLinus Torvalds extern int __xfrm_policy_check(struct sock *, int dir, struct sk_buff *skb, unsigned short family); 10001da177e4SLinus Torvalds 1001d5422efeSHerbert Xu static inline int __xfrm_policy_check2(struct sock *sk, int dir, 1002d5422efeSHerbert Xu struct sk_buff *skb, 1003d5422efeSHerbert Xu unsigned int family, int reverse) 10041da177e4SLinus Torvalds { 1005f6e1e25dSAlexey Dobriyan struct net *net = dev_net(skb->dev); 1006d5422efeSHerbert Xu int ndir = dir | (reverse ? XFRM_POLICY_MASK + 1 : 0); 1007d5422efeSHerbert Xu 10081da177e4SLinus Torvalds if (sk && sk->sk_policy[XFRM_POLICY_IN]) 1009d5422efeSHerbert Xu return __xfrm_policy_check(sk, ndir, skb, family); 10101da177e4SLinus Torvalds 1011f6e1e25dSAlexey Dobriyan return (!net->xfrm.policy_count[dir] && !skb->sp) || 1012adf30907SEric Dumazet (skb_dst(skb)->flags & DST_NOPOLICY) || 1013d5422efeSHerbert Xu __xfrm_policy_check(sk, ndir, skb, family); 1014d5422efeSHerbert Xu } 1015d5422efeSHerbert Xu 1016d5422efeSHerbert Xu static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family) 1017d5422efeSHerbert Xu { 1018d5422efeSHerbert Xu return __xfrm_policy_check2(sk, dir, skb, family, 0); 10191da177e4SLinus Torvalds } 10201da177e4SLinus Torvalds 10211da177e4SLinus Torvalds static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb) 10221da177e4SLinus Torvalds { 10231da177e4SLinus Torvalds return xfrm_policy_check(sk, dir, skb, AF_INET); 10241da177e4SLinus Torvalds } 10251da177e4SLinus Torvalds 10261da177e4SLinus Torvalds static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb) 10271da177e4SLinus Torvalds { 10281da177e4SLinus Torvalds return xfrm_policy_check(sk, dir, skb, AF_INET6); 10291da177e4SLinus Torvalds } 10301da177e4SLinus Torvalds 1031d5422efeSHerbert Xu static inline int xfrm4_policy_check_reverse(struct sock *sk, int dir, 1032d5422efeSHerbert Xu struct sk_buff *skb) 1033d5422efeSHerbert Xu { 1034d5422efeSHerbert Xu return __xfrm_policy_check2(sk, dir, skb, AF_INET, 1); 1035d5422efeSHerbert Xu } 1036d5422efeSHerbert Xu 1037d5422efeSHerbert Xu static inline int xfrm6_policy_check_reverse(struct sock *sk, int dir, 1038d5422efeSHerbert Xu struct sk_buff *skb) 1039d5422efeSHerbert Xu { 1040d5422efeSHerbert Xu return __xfrm_policy_check2(sk, dir, skb, AF_INET6, 1); 1041d5422efeSHerbert Xu } 1042d5422efeSHerbert Xu 1043d5422efeSHerbert Xu extern int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, 1044d5422efeSHerbert Xu unsigned int family, int reverse); 1045d5422efeSHerbert Xu 1046d5422efeSHerbert Xu static inline int xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, 1047d5422efeSHerbert Xu unsigned int family) 1048d5422efeSHerbert Xu { 1049d5422efeSHerbert Xu return __xfrm_decode_session(skb, fl, family, 0); 1050d5422efeSHerbert Xu } 1051d5422efeSHerbert Xu 1052d5422efeSHerbert Xu static inline int xfrm_decode_session_reverse(struct sk_buff *skb, 1053d5422efeSHerbert Xu struct flowi *fl, 1054d5422efeSHerbert Xu unsigned int family) 1055d5422efeSHerbert Xu { 1056d5422efeSHerbert Xu return __xfrm_decode_session(skb, fl, family, 1); 1057d5422efeSHerbert Xu } 1058d5422efeSHerbert Xu 10591da177e4SLinus Torvalds extern int __xfrm_route_forward(struct sk_buff *skb, unsigned short family); 10601da177e4SLinus Torvalds 10611da177e4SLinus Torvalds static inline int xfrm_route_forward(struct sk_buff *skb, unsigned short family) 10621da177e4SLinus Torvalds { 106399a66657SAlexey Dobriyan struct net *net = dev_net(skb->dev); 106499a66657SAlexey Dobriyan 106599a66657SAlexey Dobriyan return !net->xfrm.policy_count[XFRM_POLICY_OUT] || 1066adf30907SEric Dumazet (skb_dst(skb)->flags & DST_NOXFRM) || 10671da177e4SLinus Torvalds __xfrm_route_forward(skb, family); 10681da177e4SLinus Torvalds } 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds static inline int xfrm4_route_forward(struct sk_buff *skb) 10711da177e4SLinus Torvalds { 10721da177e4SLinus Torvalds return xfrm_route_forward(skb, AF_INET); 10731da177e4SLinus Torvalds } 10741da177e4SLinus Torvalds 10751da177e4SLinus Torvalds static inline int xfrm6_route_forward(struct sk_buff *skb) 10761da177e4SLinus Torvalds { 10771da177e4SLinus Torvalds return xfrm_route_forward(skb, AF_INET6); 10781da177e4SLinus Torvalds } 10791da177e4SLinus Torvalds 10801da177e4SLinus Torvalds extern int __xfrm_sk_clone_policy(struct sock *sk); 10811da177e4SLinus Torvalds 10821da177e4SLinus Torvalds static inline int xfrm_sk_clone_policy(struct sock *sk) 10831da177e4SLinus Torvalds { 10841da177e4SLinus Torvalds if (unlikely(sk->sk_policy[0] || sk->sk_policy[1])) 10851da177e4SLinus Torvalds return __xfrm_sk_clone_policy(sk); 10861da177e4SLinus Torvalds return 0; 10871da177e4SLinus Torvalds } 10881da177e4SLinus Torvalds 10894666faabSHerbert Xu extern int xfrm_policy_delete(struct xfrm_policy *pol, int dir); 10901da177e4SLinus Torvalds 10911da177e4SLinus Torvalds static inline void xfrm_sk_free_policy(struct sock *sk) 10921da177e4SLinus Torvalds { 10931da177e4SLinus Torvalds if (unlikely(sk->sk_policy[0] != NULL)) { 10941da177e4SLinus Torvalds xfrm_policy_delete(sk->sk_policy[0], XFRM_POLICY_MAX); 10951da177e4SLinus Torvalds sk->sk_policy[0] = NULL; 10961da177e4SLinus Torvalds } 10971da177e4SLinus Torvalds if (unlikely(sk->sk_policy[1] != NULL)) { 10981da177e4SLinus Torvalds xfrm_policy_delete(sk->sk_policy[1], XFRM_POLICY_MAX+1); 10991da177e4SLinus Torvalds sk->sk_policy[1] = NULL; 11001da177e4SLinus Torvalds } 11011da177e4SLinus Torvalds } 11021da177e4SLinus Torvalds 11031da177e4SLinus Torvalds #else 11041da177e4SLinus Torvalds 11051da177e4SLinus Torvalds static inline void xfrm_sk_free_policy(struct sock *sk) {} 11061da177e4SLinus Torvalds static inline int xfrm_sk_clone_policy(struct sock *sk) { return 0; } 11071da177e4SLinus Torvalds static inline int xfrm6_route_forward(struct sk_buff *skb) { return 1; } 11081da177e4SLinus Torvalds static inline int xfrm4_route_forward(struct sk_buff *skb) { return 1; } 11091da177e4SLinus Torvalds static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb) 11101da177e4SLinus Torvalds { 11111da177e4SLinus Torvalds return 1; 11121da177e4SLinus Torvalds } 11131da177e4SLinus Torvalds static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb) 11141da177e4SLinus Torvalds { 11151da177e4SLinus Torvalds return 1; 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family) 11181da177e4SLinus Torvalds { 11191da177e4SLinus Torvalds return 1; 11201da177e4SLinus Torvalds } 1121d5422efeSHerbert Xu static inline int xfrm_decode_session_reverse(struct sk_buff *skb, 1122d5422efeSHerbert Xu struct flowi *fl, 1123d5422efeSHerbert Xu unsigned int family) 1124d5422efeSHerbert Xu { 1125d5422efeSHerbert Xu return -ENOSYS; 1126d5422efeSHerbert Xu } 1127d5422efeSHerbert Xu static inline int xfrm4_policy_check_reverse(struct sock *sk, int dir, 1128d5422efeSHerbert Xu struct sk_buff *skb) 1129d5422efeSHerbert Xu { 1130d5422efeSHerbert Xu return 1; 1131d5422efeSHerbert Xu } 1132d5422efeSHerbert Xu static inline int xfrm6_policy_check_reverse(struct sock *sk, int dir, 1133d5422efeSHerbert Xu struct sk_buff *skb) 1134d5422efeSHerbert Xu { 1135d5422efeSHerbert Xu return 1; 1136d5422efeSHerbert Xu } 11371da177e4SLinus Torvalds #endif 11381da177e4SLinus Torvalds 11391da177e4SLinus Torvalds static __inline__ 1140e8a4e377SDavid S. Miller xfrm_address_t *xfrm_flowi_daddr(const struct flowi *fl, unsigned short family) 11411da177e4SLinus Torvalds { 11421da177e4SLinus Torvalds switch (family){ 11431da177e4SLinus Torvalds case AF_INET: 11441da177e4SLinus Torvalds return (xfrm_address_t *)&fl->fl4_dst; 11451da177e4SLinus Torvalds case AF_INET6: 11461da177e4SLinus Torvalds return (xfrm_address_t *)&fl->fl6_dst; 11471da177e4SLinus Torvalds } 11481da177e4SLinus Torvalds return NULL; 11491da177e4SLinus Torvalds } 11501da177e4SLinus Torvalds 11511da177e4SLinus Torvalds static __inline__ 1152e8a4e377SDavid S. Miller xfrm_address_t *xfrm_flowi_saddr(const struct flowi *fl, unsigned short family) 11531da177e4SLinus Torvalds { 11541da177e4SLinus Torvalds switch (family){ 11551da177e4SLinus Torvalds case AF_INET: 11561da177e4SLinus Torvalds return (xfrm_address_t *)&fl->fl4_src; 11571da177e4SLinus Torvalds case AF_INET6: 11581da177e4SLinus Torvalds return (xfrm_address_t *)&fl->fl6_src; 11591da177e4SLinus Torvalds } 11601da177e4SLinus Torvalds return NULL; 11611da177e4SLinus Torvalds } 11621da177e4SLinus Torvalds 11639bb182a7SYOSHIFUJI Hideaki static __inline__ 1164e8a4e377SDavid S. Miller void xfrm_flowi_addr_get(const struct flowi *fl, 11659bb182a7SYOSHIFUJI Hideaki xfrm_address_t *saddr, xfrm_address_t *daddr, 11669bb182a7SYOSHIFUJI Hideaki unsigned short family) 11679bb182a7SYOSHIFUJI Hideaki { 11689bb182a7SYOSHIFUJI Hideaki switch(family) { 11699bb182a7SYOSHIFUJI Hideaki case AF_INET: 11709bb182a7SYOSHIFUJI Hideaki memcpy(&saddr->a4, &fl->fl4_src, sizeof(saddr->a4)); 11719bb182a7SYOSHIFUJI Hideaki memcpy(&daddr->a4, &fl->fl4_dst, sizeof(daddr->a4)); 11729bb182a7SYOSHIFUJI Hideaki break; 11739bb182a7SYOSHIFUJI Hideaki case AF_INET6: 11749bb182a7SYOSHIFUJI Hideaki ipv6_addr_copy((struct in6_addr *)&saddr->a6, &fl->fl6_src); 11759bb182a7SYOSHIFUJI Hideaki ipv6_addr_copy((struct in6_addr *)&daddr->a6, &fl->fl6_dst); 11769bb182a7SYOSHIFUJI Hideaki break; 11779bb182a7SYOSHIFUJI Hideaki } 11789bb182a7SYOSHIFUJI Hideaki } 11799bb182a7SYOSHIFUJI Hideaki 11801da177e4SLinus Torvalds static __inline__ int 1181f8848067SDavid S. Miller __xfrm4_state_addr_check(const struct xfrm_state *x, 1182f8848067SDavid S. Miller const xfrm_address_t *daddr, const xfrm_address_t *saddr) 11831da177e4SLinus Torvalds { 11841da177e4SLinus Torvalds if (daddr->a4 == x->id.daddr.a4 && 11851da177e4SLinus Torvalds (saddr->a4 == x->props.saddr.a4 || !saddr->a4 || !x->props.saddr.a4)) 11861da177e4SLinus Torvalds return 1; 11871da177e4SLinus Torvalds return 0; 11881da177e4SLinus Torvalds } 11891da177e4SLinus Torvalds 11901da177e4SLinus Torvalds static __inline__ int 1191f8848067SDavid S. Miller __xfrm6_state_addr_check(const struct xfrm_state *x, 1192f8848067SDavid S. Miller const xfrm_address_t *daddr, const xfrm_address_t *saddr) 11931da177e4SLinus Torvalds { 11941da177e4SLinus Torvalds if (!ipv6_addr_cmp((struct in6_addr *)daddr, (struct in6_addr *)&x->id.daddr) && 11951da177e4SLinus Torvalds (!ipv6_addr_cmp((struct in6_addr *)saddr, (struct in6_addr *)&x->props.saddr)|| 11961da177e4SLinus Torvalds ipv6_addr_any((struct in6_addr *)saddr) || 11971da177e4SLinus Torvalds ipv6_addr_any((struct in6_addr *)&x->props.saddr))) 11981da177e4SLinus Torvalds return 1; 11991da177e4SLinus Torvalds return 0; 12001da177e4SLinus Torvalds } 12011da177e4SLinus Torvalds 12021da177e4SLinus Torvalds static __inline__ int 1203f8848067SDavid S. Miller xfrm_state_addr_check(const struct xfrm_state *x, 1204f8848067SDavid S. Miller const xfrm_address_t *daddr, const xfrm_address_t *saddr, 12051da177e4SLinus Torvalds unsigned short family) 12061da177e4SLinus Torvalds { 12071da177e4SLinus Torvalds switch (family) { 12081da177e4SLinus Torvalds case AF_INET: 12091da177e4SLinus Torvalds return __xfrm4_state_addr_check(x, daddr, saddr); 12101da177e4SLinus Torvalds case AF_INET6: 12111da177e4SLinus Torvalds return __xfrm6_state_addr_check(x, daddr, saddr); 12121da177e4SLinus Torvalds } 12131da177e4SLinus Torvalds return 0; 12141da177e4SLinus Torvalds } 12151da177e4SLinus Torvalds 1216e53820deSMasahide NAKAMURA static __inline__ int 1217f8848067SDavid S. Miller xfrm_state_addr_flow_check(const struct xfrm_state *x, const struct flowi *fl, 1218e53820deSMasahide NAKAMURA unsigned short family) 1219e53820deSMasahide NAKAMURA { 1220e53820deSMasahide NAKAMURA switch (family) { 1221e53820deSMasahide NAKAMURA case AF_INET: 1222e53820deSMasahide NAKAMURA return __xfrm4_state_addr_check(x, 1223f8848067SDavid S. Miller (const xfrm_address_t *)&fl->fl4_dst, 1224f8848067SDavid S. Miller (const xfrm_address_t *)&fl->fl4_src); 1225e53820deSMasahide NAKAMURA case AF_INET6: 1226e53820deSMasahide NAKAMURA return __xfrm6_state_addr_check(x, 1227f8848067SDavid S. Miller (const xfrm_address_t *)&fl->fl6_dst, 1228f8848067SDavid S. Miller (const xfrm_address_t *)&fl->fl6_src); 1229e53820deSMasahide NAKAMURA } 1230e53820deSMasahide NAKAMURA return 0; 1231e53820deSMasahide NAKAMURA } 1232e53820deSMasahide NAKAMURA 1233f8848067SDavid S. Miller static inline int xfrm_state_kern(const struct xfrm_state *x) 12341da177e4SLinus Torvalds { 12351da177e4SLinus Torvalds return atomic_read(&x->tunnel_users); 12361da177e4SLinus Torvalds } 12371da177e4SLinus Torvalds 12385794708fSMasahide NAKAMURA static inline int xfrm_id_proto_match(u8 proto, u8 userproto) 12395794708fSMasahide NAKAMURA { 1240dc00a525SMasahide NAKAMURA return (!userproto || proto == userproto || 1241dc00a525SMasahide NAKAMURA (userproto == IPSEC_PROTO_ANY && (proto == IPPROTO_AH || 1242dc00a525SMasahide NAKAMURA proto == IPPROTO_ESP || 1243dc00a525SMasahide NAKAMURA proto == IPPROTO_COMP))); 12445794708fSMasahide NAKAMURA } 12455794708fSMasahide NAKAMURA 12461da177e4SLinus Torvalds /* 12471da177e4SLinus Torvalds * xfrm algorithm information 12481da177e4SLinus Torvalds */ 12491a6509d9SHerbert Xu struct xfrm_algo_aead_info { 12501a6509d9SHerbert Xu u16 icv_truncbits; 12511a6509d9SHerbert Xu }; 12521a6509d9SHerbert Xu 12531da177e4SLinus Torvalds struct xfrm_algo_auth_info { 12541da177e4SLinus Torvalds u16 icv_truncbits; 12551da177e4SLinus Torvalds u16 icv_fullbits; 12561da177e4SLinus Torvalds }; 12571da177e4SLinus Torvalds 12581da177e4SLinus Torvalds struct xfrm_algo_encr_info { 12591da177e4SLinus Torvalds u16 blockbits; 12601da177e4SLinus Torvalds u16 defkeybits; 12611da177e4SLinus Torvalds }; 12621da177e4SLinus Torvalds 12631da177e4SLinus Torvalds struct xfrm_algo_comp_info { 12641da177e4SLinus Torvalds u16 threshold; 12651da177e4SLinus Torvalds }; 12661da177e4SLinus Torvalds 12671da177e4SLinus Torvalds struct xfrm_algo_desc { 12681da177e4SLinus Torvalds char *name; 126904ff1260SHerbert Xu char *compat; 12701da177e4SLinus Torvalds u8 available:1; 12711da177e4SLinus Torvalds union { 12721a6509d9SHerbert Xu struct xfrm_algo_aead_info aead; 12731da177e4SLinus Torvalds struct xfrm_algo_auth_info auth; 12741da177e4SLinus Torvalds struct xfrm_algo_encr_info encr; 12751da177e4SLinus Torvalds struct xfrm_algo_comp_info comp; 12761da177e4SLinus Torvalds } uinfo; 12771da177e4SLinus Torvalds struct sadb_alg desc; 12781da177e4SLinus Torvalds }; 12791da177e4SLinus Torvalds 12801da177e4SLinus Torvalds /* XFRM tunnel handlers. */ 12811da177e4SLinus Torvalds struct xfrm_tunnel { 12821da177e4SLinus Torvalds int (*handler)(struct sk_buff *skb); 1283a6337463Sjamal int (*err_handler)(struct sk_buff *skb, u32 info); 1284d2acc347SHerbert Xu 1285b33eab08SEric Dumazet struct xfrm_tunnel __rcu *next; 1286d2acc347SHerbert Xu int priority; 12871da177e4SLinus Torvalds }; 12881da177e4SLinus Torvalds 12891da177e4SLinus Torvalds struct xfrm6_tunnel { 1290d2acc347SHerbert Xu int (*handler)(struct sk_buff *skb); 1291d2acc347SHerbert Xu int (*err_handler)(struct sk_buff *skb, struct inet6_skb_parm *opt, 1292d5fdd6baSBrian Haley u8 type, u8 code, int offset, __be32 info); 12936f0bcf15SEric Dumazet struct xfrm6_tunnel __rcu *next; 1294d2acc347SHerbert Xu int priority; 12951da177e4SLinus Torvalds }; 12961da177e4SLinus Torvalds 12971da177e4SLinus Torvalds extern void xfrm_init(void); 1298a33bc5c1SNeil Horman extern void xfrm4_init(int rt_hash_size); 1299d62ddc21SAlexey Dobriyan extern int xfrm_state_init(struct net *net); 1300d62ddc21SAlexey Dobriyan extern void xfrm_state_fini(struct net *net); 13011da177e4SLinus Torvalds extern void xfrm4_state_init(void); 1302c35b7e72SDaniel Lezcano #ifdef CONFIG_XFRM 1303c35b7e72SDaniel Lezcano extern int xfrm6_init(void); 1304c35b7e72SDaniel Lezcano extern void xfrm6_fini(void); 13050013cabaSDaniel Lezcano extern int xfrm6_state_init(void); 13061da177e4SLinus Torvalds extern void xfrm6_state_fini(void); 1307c35b7e72SDaniel Lezcano #else 1308c35b7e72SDaniel Lezcano static inline int xfrm6_init(void) 1309c35b7e72SDaniel Lezcano { 1310c35b7e72SDaniel Lezcano return 0; 1311c35b7e72SDaniel Lezcano } 1312c35b7e72SDaniel Lezcano static inline void xfrm6_fini(void) 1313c35b7e72SDaniel Lezcano { 1314c35b7e72SDaniel Lezcano ; 1315c35b7e72SDaniel Lezcano } 1316c35b7e72SDaniel Lezcano #endif 13171da177e4SLinus Torvalds 1318558f82efSMasahide NAKAMURA #ifdef CONFIG_XFRM_STATISTICS 1319c68cd1a0SAlexey Dobriyan extern int xfrm_proc_init(struct net *net); 1320c68cd1a0SAlexey Dobriyan extern void xfrm_proc_fini(struct net *net); 1321558f82efSMasahide NAKAMURA #endif 1322558f82efSMasahide NAKAMURA 1323b27aeadbSAlexey Dobriyan extern int xfrm_sysctl_init(struct net *net); 1324b27aeadbSAlexey Dobriyan #ifdef CONFIG_SYSCTL 1325b27aeadbSAlexey Dobriyan extern void xfrm_sysctl_fini(struct net *net); 1326b27aeadbSAlexey Dobriyan #else 1327b27aeadbSAlexey Dobriyan static inline void xfrm_sysctl_fini(struct net *net) 1328b27aeadbSAlexey Dobriyan { 1329b27aeadbSAlexey Dobriyan } 1330b27aeadbSAlexey Dobriyan #endif 1331b27aeadbSAlexey Dobriyan 13325c182458SHerbert Xu extern void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto); 1333284fa7daSAlexey Dobriyan extern int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk, 13344c563f76STimo Teras int (*func)(struct xfrm_state *, int, void*), void *); 1335abb81c4fSHerbert Xu extern void xfrm_state_walk_done(struct xfrm_state_walk *walk); 1336673c09beSAlexey Dobriyan extern struct xfrm_state *xfrm_state_alloc(struct net *net); 133733765d06SDavid S. Miller extern struct xfrm_state *xfrm_state_find(const xfrm_address_t *daddr, 133833765d06SDavid S. Miller const xfrm_address_t *saddr, 1339b520e9f6SDavid S. Miller const struct flowi *fl, 1340b520e9f6SDavid S. Miller struct xfrm_tmpl *tmpl, 13411da177e4SLinus Torvalds struct xfrm_policy *pol, int *err, 13421da177e4SLinus Torvalds unsigned short family); 1343bd55775cSJamal Hadi Salim extern struct xfrm_state *xfrm_stateonly_find(struct net *net, u32 mark, 13445447c5e4SAlexey Dobriyan xfrm_address_t *daddr, 1345628529b6SJamal Hadi Salim xfrm_address_t *saddr, 1346628529b6SJamal Hadi Salim unsigned short family, 1347628529b6SJamal Hadi Salim u8 mode, u8 proto, u32 reqid); 13481da177e4SLinus Torvalds extern int xfrm_state_check_expire(struct xfrm_state *x); 13491da177e4SLinus Torvalds extern void xfrm_state_insert(struct xfrm_state *x); 13501da177e4SLinus Torvalds extern int xfrm_state_add(struct xfrm_state *x); 13511da177e4SLinus Torvalds extern int xfrm_state_update(struct xfrm_state *x); 1352bd55775cSJamal Hadi Salim extern struct xfrm_state *xfrm_state_lookup(struct net *net, u32 mark, 1353bd55775cSJamal Hadi Salim xfrm_address_t *daddr, __be32 spi, 1354bd55775cSJamal Hadi Salim u8 proto, unsigned short family); 1355bd55775cSJamal Hadi Salim extern struct xfrm_state *xfrm_state_lookup_byaddr(struct net *net, u32 mark, 1356bd55775cSJamal Hadi Salim xfrm_address_t *daddr, 1357bd55775cSJamal Hadi Salim xfrm_address_t *saddr, 1358bd55775cSJamal Hadi Salim u8 proto, 1359bd55775cSJamal Hadi Salim unsigned short family); 136041a49cc3SMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY 136141a49cc3SMasahide NAKAMURA extern int xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, 136241a49cc3SMasahide NAKAMURA int n, unsigned short family); 136341a49cc3SMasahide NAKAMURA extern int xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, 136441a49cc3SMasahide NAKAMURA int n, unsigned short family); 136541a49cc3SMasahide NAKAMURA #else 136641a49cc3SMasahide NAKAMURA static inline int xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, 136741a49cc3SMasahide NAKAMURA int n, unsigned short family) 136841a49cc3SMasahide NAKAMURA { 136941a49cc3SMasahide NAKAMURA return -ENOSYS; 137041a49cc3SMasahide NAKAMURA } 137141a49cc3SMasahide NAKAMURA 137241a49cc3SMasahide NAKAMURA static inline int xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, 137341a49cc3SMasahide NAKAMURA int n, unsigned short family) 137441a49cc3SMasahide NAKAMURA { 137541a49cc3SMasahide NAKAMURA return -ENOSYS; 137641a49cc3SMasahide NAKAMURA } 137741a49cc3SMasahide NAKAMURA #endif 1378af11e316SJamal Hadi Salim 1379af11e316SJamal Hadi Salim struct xfrmk_sadinfo { 1380af11e316SJamal Hadi Salim u32 sadhcnt; /* current hash bkts */ 1381af11e316SJamal Hadi Salim u32 sadhmcnt; /* max allowed hash bkts */ 1382af11e316SJamal Hadi Salim u32 sadcnt; /* current running count */ 1383af11e316SJamal Hadi Salim }; 1384af11e316SJamal Hadi Salim 13855a6d3416SJamal Hadi Salim struct xfrmk_spdinfo { 13865a6d3416SJamal Hadi Salim u32 incnt; 13875a6d3416SJamal Hadi Salim u32 outcnt; 13885a6d3416SJamal Hadi Salim u32 fwdcnt; 13895a6d3416SJamal Hadi Salim u32 inscnt; 13905a6d3416SJamal Hadi Salim u32 outscnt; 13915a6d3416SJamal Hadi Salim u32 fwdscnt; 13925a6d3416SJamal Hadi Salim u32 spdhcnt; 13935a6d3416SJamal Hadi Salim u32 spdhmcnt; 13945a6d3416SJamal Hadi Salim }; 13955a6d3416SJamal Hadi Salim 1396bd55775cSJamal Hadi Salim extern struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, 1397bd55775cSJamal Hadi Salim u32 seq); 139826b15dadSJamal Hadi Salim extern int xfrm_state_delete(struct xfrm_state *x); 13990e602451SAlexey Dobriyan extern int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info); 1400e071041bSAlexey Dobriyan extern void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si); 1401e071041bSAlexey Dobriyan extern void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si); 1402afeb14b4SPaul Moore extern int xfrm_replay_check(struct xfrm_state *x, 1403afeb14b4SPaul Moore struct sk_buff *skb, __be32 seq); 140461f4627bSAl Viro extern void xfrm_replay_advance(struct xfrm_state *x, __be32 seq); 1405f8cd5488SJamal Hadi Salim extern void xfrm_replay_notify(struct xfrm_state *x, int event); 14061da177e4SLinus Torvalds extern int xfrm_state_mtu(struct xfrm_state *x, int mtu); 140772cb6962SHerbert Xu extern int xfrm_init_state(struct xfrm_state *x); 1408227620e2SHerbert Xu extern int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb); 1409716062fdSHerbert Xu extern int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, 1410716062fdSHerbert Xu int encap_type); 14111bf06cd2SHerbert Xu extern int xfrm_input_resume(struct sk_buff *skb, int nexthdr); 1412c6581a45SHerbert Xu extern int xfrm_output_resume(struct sk_buff *skb, int err); 1413406ef77cSHerbert Xu extern int xfrm_output(struct sk_buff *skb); 1414df9dcb45SKazunori MIYAZAWA extern int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb); 141536cf9acfSHerbert Xu extern int xfrm4_extract_header(struct sk_buff *skb); 1416227620e2SHerbert Xu extern int xfrm4_extract_input(struct xfrm_state *x, struct sk_buff *skb); 1417c4541b41SHerbert Xu extern int xfrm4_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi, 1418c4541b41SHerbert Xu int encap_type); 1419716062fdSHerbert Xu extern int xfrm4_transport_finish(struct sk_buff *skb, int async); 14201da177e4SLinus Torvalds extern int xfrm4_rcv(struct sk_buff *skb); 1421c4541b41SHerbert Xu 1422c4541b41SHerbert Xu static inline int xfrm4_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi) 1423c4541b41SHerbert Xu { 1424c4541b41SHerbert Xu return xfrm4_rcv_encap(skb, nexthdr, spi, 0); 1425c4541b41SHerbert Xu } 1426c4541b41SHerbert Xu 142736cf9acfSHerbert Xu extern int xfrm4_extract_output(struct xfrm_state *x, struct sk_buff *skb); 142836cf9acfSHerbert Xu extern int xfrm4_prepare_output(struct xfrm_state *x, struct sk_buff *skb); 14291da177e4SLinus Torvalds extern int xfrm4_output(struct sk_buff *skb); 1430c0d56408SKazunori MIYAZAWA extern int xfrm4_tunnel_register(struct xfrm_tunnel *handler, unsigned short family); 1431c0d56408SKazunori MIYAZAWA extern int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler, unsigned short family); 143236cf9acfSHerbert Xu extern int xfrm6_extract_header(struct sk_buff *skb); 1433227620e2SHerbert Xu extern int xfrm6_extract_input(struct xfrm_state *x, struct sk_buff *skb); 143433b5ecb8SHerbert Xu extern int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi); 1435716062fdSHerbert Xu extern int xfrm6_transport_finish(struct sk_buff *skb, int async); 1436e5bbef20SHerbert Xu extern int xfrm6_rcv(struct sk_buff *skb); 1437fbd9a5b4SMasahide NAKAMURA extern int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr, 1438fbd9a5b4SMasahide NAKAMURA xfrm_address_t *saddr, u8 proto); 143973d605d1SKazunori MIYAZAWA extern int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family); 144073d605d1SKazunori MIYAZAWA extern int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family); 1441a1664773SAlexey Dobriyan extern __be32 xfrm6_tunnel_alloc_spi(struct net *net, xfrm_address_t *saddr); 1442a1664773SAlexey Dobriyan extern __be32 xfrm6_tunnel_spi_lookup(struct net *net, xfrm_address_t *saddr); 144336cf9acfSHerbert Xu extern int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb); 144436cf9acfSHerbert Xu extern int xfrm6_prepare_output(struct xfrm_state *x, struct sk_buff *skb); 14451da177e4SLinus Torvalds extern int xfrm6_output(struct sk_buff *skb); 1446aee5adb4SMasahide NAKAMURA extern int xfrm6_find_1stfragopt(struct xfrm_state *x, struct sk_buff *skb, 1447aee5adb4SMasahide NAKAMURA u8 **prevhdr); 14481da177e4SLinus Torvalds 14491da177e4SLinus Torvalds #ifdef CONFIG_XFRM 1450067b207bSJames Chapman extern int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb); 14511da177e4SLinus Torvalds extern int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen); 14521da177e4SLinus Torvalds #else 14531da177e4SLinus Torvalds static inline int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen) 14541da177e4SLinus Torvalds { 14551da177e4SLinus Torvalds return -ENOPROTOOPT; 14561da177e4SLinus Torvalds } 14571da177e4SLinus Torvalds 1458067b207bSJames Chapman static inline int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb) 14591da177e4SLinus Torvalds { 14601da177e4SLinus Torvalds /* should not happen */ 14611da177e4SLinus Torvalds kfree_skb(skb); 14621da177e4SLinus Torvalds return 0; 14631da177e4SLinus Torvalds } 14641da177e4SLinus Torvalds #endif 14651da177e4SLinus Torvalds 14660331b1f3SAlexey Dobriyan struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp); 14674c563f76STimo Teras 146812a169e7SHerbert Xu extern void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type); 1469cdcbca7cSAlexey Dobriyan extern int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk, 14704c563f76STimo Teras int (*func)(struct xfrm_policy *, int, int, void*), void *); 147112a169e7SHerbert Xu extern void xfrm_policy_walk_done(struct xfrm_policy_walk *walk); 14721da177e4SLinus Torvalds int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl); 14738ca2e93bSJamal Hadi Salim struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, 14748ca2e93bSJamal Hadi Salim u8 type, int dir, 14754e81bb83SMasahide NAKAMURA struct xfrm_selector *sel, 1476ef41aaa0SEric Paris struct xfrm_sec_ctx *ctx, int delete, 1477ef41aaa0SEric Paris int *err); 14788ca2e93bSJamal Hadi Salim struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8, int dir, u32 id, int delete, int *err); 147933ffbbd5SAlexey Dobriyan int xfrm_policy_flush(struct net *net, u8 type, struct xfrm_audit *audit_info); 14801da177e4SLinus Torvalds u32 xfrm_get_acqseq(void); 1481658b219eSHerbert Xu extern int xfrm_alloc_spi(struct xfrm_state *x, u32 minspi, u32 maxspi); 1482bd55775cSJamal Hadi Salim struct xfrm_state *xfrm_find_acq(struct net *net, struct xfrm_mark *mark, 1483bd55775cSJamal Hadi Salim u8 mode, u32 reqid, u8 proto, 1484bd55775cSJamal Hadi Salim xfrm_address_t *daddr, 1485bd55775cSJamal Hadi Salim xfrm_address_t *saddr, int create, 1486bd55775cSJamal Hadi Salim unsigned short family); 14871da177e4SLinus Torvalds extern int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol); 14881da177e4SLinus Torvalds 148980c9abaaSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE 1490183cad12SDavid S. Miller extern int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, 1491183cad12SDavid S. Miller const struct xfrm_migrate *m, int num_bundles, 1492183cad12SDavid S. Miller const struct xfrm_kmaddress *k); 149380c9abaaSShinta Sugimoto extern struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m); 149480c9abaaSShinta Sugimoto extern struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x, 149580c9abaaSShinta Sugimoto struct xfrm_migrate *m); 1496b4b7c0b3SDavid S. Miller extern int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, 149713c1d189SArnaud Ebalard struct xfrm_migrate *m, int num_bundles, 149813c1d189SArnaud Ebalard struct xfrm_kmaddress *k); 149980c9abaaSShinta Sugimoto #endif 150080c9abaaSShinta Sugimoto 15015d36b180SAl Viro extern int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport); 15026c5c8ca7SJamal Hadi Salim extern void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 pid); 1503db983c11SAlexey Dobriyan extern int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr); 15041da177e4SLinus Torvalds 15051da177e4SLinus Torvalds extern void xfrm_input_init(void); 15066067b2baSAl Viro extern int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq); 15071da177e4SLinus Torvalds 15081da177e4SLinus Torvalds extern void xfrm_probe_algs(void); 15091da177e4SLinus Torvalds extern int xfrm_count_auth_supported(void); 15101da177e4SLinus Torvalds extern int xfrm_count_enc_supported(void); 15111da177e4SLinus Torvalds extern struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx); 15121da177e4SLinus Torvalds extern struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx); 15131da177e4SLinus Torvalds extern struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id); 15141da177e4SLinus Torvalds extern struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id); 15151da177e4SLinus Torvalds extern struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id); 15166f2f19edSDavid S. Miller extern struct xfrm_algo_desc *xfrm_aalg_get_byname(const char *name, int probe); 15176f2f19edSDavid S. Miller extern struct xfrm_algo_desc *xfrm_ealg_get_byname(const char *name, int probe); 15186f2f19edSDavid S. Miller extern struct xfrm_algo_desc *xfrm_calg_get_byname(const char *name, int probe); 15196f2f19edSDavid S. Miller extern struct xfrm_algo_desc *xfrm_aead_get_byname(const char *name, int icv_len, 15201a6509d9SHerbert Xu int probe); 15211da177e4SLinus Torvalds 152207d4ee58SHerbert Xu struct hash_desc; 15239409f38aSHerbert Xu struct scatterlist; 152407d4ee58SHerbert Xu typedef int (icv_update_fn_t)(struct hash_desc *, struct scatterlist *, 152507d4ee58SHerbert Xu unsigned int); 15261da177e4SLinus Torvalds 1527ff6acd16SDavid S. Miller static inline int xfrm_addr_cmp(const xfrm_address_t *a, 1528ff6acd16SDavid S. Miller const xfrm_address_t *b, 15291da177e4SLinus Torvalds int family) 15301da177e4SLinus Torvalds { 15311da177e4SLinus Torvalds switch (family) { 15321da177e4SLinus Torvalds default: 15331da177e4SLinus Torvalds case AF_INET: 1534a6337463Sjamal return (__force u32)a->a4 - (__force u32)b->a4; 15351da177e4SLinus Torvalds case AF_INET6: 15361da177e4SLinus Torvalds return ipv6_addr_cmp((struct in6_addr *)a, 15371da177e4SLinus Torvalds (struct in6_addr *)b); 15381da177e4SLinus Torvalds } 15391da177e4SLinus Torvalds } 15401da177e4SLinus Torvalds 154177d8d7a6SHerbert Xu static inline int xfrm_policy_id2dir(u32 index) 154277d8d7a6SHerbert Xu { 154377d8d7a6SHerbert Xu return index & 7; 154477d8d7a6SHerbert Xu } 154577d8d7a6SHerbert Xu 1546a6483b79SAlexey Dobriyan #ifdef CONFIG_XFRM 1547a6483b79SAlexey Dobriyan static inline int xfrm_aevent_is_on(struct net *net) 1548f8cd5488SJamal Hadi Salim { 1549be33690dSPatrick McHardy struct sock *nlsk; 1550be33690dSPatrick McHardy int ret = 0; 1551be33690dSPatrick McHardy 1552be33690dSPatrick McHardy rcu_read_lock(); 1553a6483b79SAlexey Dobriyan nlsk = rcu_dereference(net->xfrm.nlsk); 1554be33690dSPatrick McHardy if (nlsk) 1555be33690dSPatrick McHardy ret = netlink_has_listeners(nlsk, XFRMNLGRP_AEVENTS); 1556be33690dSPatrick McHardy rcu_read_unlock(); 1557be33690dSPatrick McHardy return ret; 1558f8cd5488SJamal Hadi Salim } 1559a6483b79SAlexey Dobriyan #endif 1560f8cd5488SJamal Hadi Salim 1561*85158621SDavid S. Miller static inline int xfrm_alg_len(const struct xfrm_algo *alg) 15620f99be0dSEric Dumazet { 15630f99be0dSEric Dumazet return sizeof(*alg) + ((alg->alg_key_len + 7) / 8); 15640f99be0dSEric Dumazet } 15650f99be0dSEric Dumazet 1566*85158621SDavid S. Miller static inline int xfrm_alg_auth_len(const struct xfrm_algo_auth *alg) 15674447bb33SMartin Willi { 15684447bb33SMartin Willi return sizeof(*alg) + ((alg->alg_key_len + 7) / 8); 15694447bb33SMartin Willi } 15704447bb33SMartin Willi 157180c9abaaSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE 157280c9abaaSShinta Sugimoto static inline struct xfrm_algo *xfrm_algo_clone(struct xfrm_algo *orig) 157380c9abaaSShinta Sugimoto { 15740f99be0dSEric Dumazet return kmemdup(orig, xfrm_alg_len(orig), GFP_KERNEL); 157580c9abaaSShinta Sugimoto } 157680c9abaaSShinta Sugimoto 15774447bb33SMartin Willi static inline struct xfrm_algo_auth *xfrm_algo_auth_clone(struct xfrm_algo_auth *orig) 15784447bb33SMartin Willi { 15794447bb33SMartin Willi return kmemdup(orig, xfrm_alg_auth_len(orig), GFP_KERNEL); 15804447bb33SMartin Willi } 15814447bb33SMartin Willi 158280c9abaaSShinta Sugimoto static inline void xfrm_states_put(struct xfrm_state **states, int n) 158380c9abaaSShinta Sugimoto { 158480c9abaaSShinta Sugimoto int i; 158580c9abaaSShinta Sugimoto for (i = 0; i < n; i++) 158680c9abaaSShinta Sugimoto xfrm_state_put(*(states + i)); 158780c9abaaSShinta Sugimoto } 158880c9abaaSShinta Sugimoto 158980c9abaaSShinta Sugimoto static inline void xfrm_states_delete(struct xfrm_state **states, int n) 159080c9abaaSShinta Sugimoto { 159180c9abaaSShinta Sugimoto int i; 159280c9abaaSShinta Sugimoto for (i = 0; i < n; i++) 159380c9abaaSShinta Sugimoto xfrm_state_delete(*(states + i)); 159480c9abaaSShinta Sugimoto } 159580c9abaaSShinta Sugimoto #endif 1596f8cd5488SJamal Hadi Salim 1597def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM 159800501121SHerbert Xu static inline struct xfrm_state *xfrm_input_state(struct sk_buff *skb) 159900501121SHerbert Xu { 160000501121SHerbert Xu return skb->sp->xvec[skb->sp->len - 1]; 160100501121SHerbert Xu } 1602def8b4faSAlexey Dobriyan #endif 160300501121SHerbert Xu 1604bf825f81SJamal Hadi Salim static inline int xfrm_mark_get(struct nlattr **attrs, struct xfrm_mark *m) 1605bf825f81SJamal Hadi Salim { 1606bf825f81SJamal Hadi Salim if (attrs[XFRMA_MARK]) 16074efd7e83SAndreas Steffen memcpy(m, nla_data(attrs[XFRMA_MARK]), sizeof(struct xfrm_mark)); 1608bf825f81SJamal Hadi Salim else 1609bf825f81SJamal Hadi Salim m->v = m->m = 0; 1610bf825f81SJamal Hadi Salim 1611bf825f81SJamal Hadi Salim return m->v & m->m; 1612bf825f81SJamal Hadi Salim } 1613bf825f81SJamal Hadi Salim 1614bf825f81SJamal Hadi Salim static inline int xfrm_mark_put(struct sk_buff *skb, struct xfrm_mark *m) 1615bf825f81SJamal Hadi Salim { 1616bf825f81SJamal Hadi Salim if (m->m | m->v) 1617bf825f81SJamal Hadi Salim NLA_PUT(skb, XFRMA_MARK, sizeof(struct xfrm_mark), m); 1618bf825f81SJamal Hadi Salim return 0; 1619bf825f81SJamal Hadi Salim 1620bf825f81SJamal Hadi Salim nla_put_failure: 1621bf825f81SJamal Hadi Salim return -1; 1622bf825f81SJamal Hadi Salim } 1623bf825f81SJamal Hadi Salim 16241da177e4SLinus Torvalds #endif /* _NET_XFRM_H */ 1625