Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef _NET_XFRM_H |
| 2 | #define _NET_XFRM_H |
| 3 | |
Herbert Xu | aabc976 | 2005-05-03 16:27:10 -0700 | [diff] [blame] | 4 | #include <linux/compiler.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 5 | #include <linux/in.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <linux/xfrm.h> |
| 7 | #include <linux/spinlock.h> |
| 8 | #include <linux/list.h> |
| 9 | #include <linux/skbuff.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 10 | #include <linux/socket.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/pfkeyv2.h> |
| 12 | #include <linux/in6.h> |
Arjan van de Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 13 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
| 15 | #include <net/sock.h> |
| 16 | #include <net/dst.h> |
| 17 | #include <net/route.h> |
| 18 | #include <net/ipv6.h> |
| 19 | #include <net/ip6_fib.h> |
| 20 | |
| 21 | #define XFRM_ALIGN8(len) (((len) + 7) & ~7) |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 22 | #define MODULE_ALIAS_XFRM_MODE(family, encap) \ |
| 23 | MODULE_ALIAS("xfrm-mode-" __stringify(family) "-" __stringify(encap)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Jamal Hadi Salim | f8cd548 | 2006-03-20 19:15:11 -0800 | [diff] [blame] | 25 | extern struct sock *xfrm_nl; |
| 26 | extern u32 sysctl_xfrm_aevent_etime; |
| 27 | extern u32 sysctl_xfrm_aevent_rseqth; |
| 28 | |
Arjan van de Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 29 | extern struct mutex xfrm_cfg_mutex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | /* Organization of SPD aka "XFRM rules" |
| 32 | ------------------------------------ |
| 33 | |
| 34 | Basic objects: |
| 35 | - policy rule, struct xfrm_policy (=SPD entry) |
| 36 | - bundle of transformations, struct dst_entry == struct xfrm_dst (=SA bundle) |
| 37 | - instance of a transformer, struct xfrm_state (=SA) |
| 38 | - template to clone xfrm_state, struct xfrm_tmpl |
| 39 | |
| 40 | SPD is plain linear list of xfrm_policy rules, ordered by priority. |
| 41 | (To be compatible with existing pfkeyv2 implementations, |
| 42 | many rules with priority of 0x7fffffff are allowed to exist and |
| 43 | such rules are ordered in an unpredictable way, thanks to bsd folks.) |
| 44 | |
| 45 | Lookup is plain linear search until the first match with selector. |
| 46 | |
| 47 | If "action" is "block", then we prohibit the flow, otherwise: |
| 48 | if "xfrms_nr" is zero, the flow passes untransformed. Otherwise, |
| 49 | policy entry has list of up to XFRM_MAX_DEPTH transformations, |
| 50 | described by templates xfrm_tmpl. Each template is resolved |
| 51 | to a complete xfrm_state (see below) and we pack bundle of transformations |
| 52 | to a dst_entry returned to requestor. |
| 53 | |
| 54 | dst -. xfrm .-> xfrm_state #1 |
| 55 | |---. child .-> dst -. xfrm .-> xfrm_state #2 |
| 56 | |---. child .-> dst -. xfrm .-> xfrm_state #3 |
| 57 | |---. child .-> NULL |
| 58 | |
| 59 | Bundles are cached at xrfm_policy struct (field ->bundles). |
| 60 | |
| 61 | |
| 62 | Resolution of xrfm_tmpl |
| 63 | ----------------------- |
| 64 | Template contains: |
| 65 | 1. ->mode Mode: transport or tunnel |
| 66 | 2. ->id.proto Protocol: AH/ESP/IPCOMP |
| 67 | 3. ->id.daddr Remote tunnel endpoint, ignored for transport mode. |
| 68 | Q: allow to resolve security gateway? |
| 69 | 4. ->id.spi If not zero, static SPI. |
| 70 | 5. ->saddr Local tunnel endpoint, ignored for transport mode. |
| 71 | 6. ->algos List of allowed algos. Plain bitmask now. |
| 72 | Q: ealgos, aalgos, calgos. What a mess... |
| 73 | 7. ->share Sharing mode. |
| 74 | Q: how to implement private sharing mode? To add struct sock* to |
| 75 | flow id? |
| 76 | |
| 77 | Having this template we search through SAD searching for entries |
| 78 | with appropriate mode/proto/algo, permitted by selector. |
| 79 | If no appropriate entry found, it is requested from key manager. |
| 80 | |
| 81 | PROBLEMS: |
| 82 | Q: How to find all the bundles referring to a physical path for |
| 83 | PMTU discovery? Seems, dst should contain list of all parents... |
| 84 | and enter to infinite locking hierarchy disaster. |
| 85 | No! It is easier, we will not search for them, let them find us. |
| 86 | We add genid to each dst plus pointer to genid of raw IP route, |
| 87 | pmtu disc will update pmtu on raw IP route and increase its genid. |
| 88 | dst_check() will see this for top level and trigger resyncing |
| 89 | metrics. Plus, it will be made via sk->sk_dst_cache. Solved. |
| 90 | */ |
| 91 | |
| 92 | /* Full description of state of transformer. */ |
| 93 | struct xfrm_state |
| 94 | { |
| 95 | /* Note: bydst is re-used during gc */ |
| 96 | struct list_head bydst; |
| 97 | struct list_head byspi; |
| 98 | |
| 99 | atomic_t refcnt; |
| 100 | spinlock_t lock; |
| 101 | |
| 102 | struct xfrm_id id; |
| 103 | struct xfrm_selector sel; |
| 104 | |
| 105 | /* Key manger bits */ |
| 106 | struct { |
| 107 | u8 state; |
| 108 | u8 dying; |
| 109 | u32 seq; |
| 110 | } km; |
| 111 | |
| 112 | /* Parameters of this state. */ |
| 113 | struct { |
| 114 | u32 reqid; |
| 115 | u8 mode; |
| 116 | u8 replay_window; |
| 117 | u8 aalgo, ealgo, calgo; |
| 118 | u8 flags; |
| 119 | u16 family; |
| 120 | xfrm_address_t saddr; |
| 121 | int header_len; |
| 122 | int trailer_len; |
| 123 | } props; |
| 124 | |
| 125 | struct xfrm_lifetime_cfg lft; |
| 126 | |
| 127 | /* Data for transformer */ |
| 128 | struct xfrm_algo *aalg; |
| 129 | struct xfrm_algo *ealg; |
| 130 | struct xfrm_algo *calg; |
| 131 | |
| 132 | /* Data for encapsulator */ |
| 133 | struct xfrm_encap_tmpl *encap; |
| 134 | |
| 135 | /* IPComp needs an IPIP tunnel for handling uncompressed packets */ |
| 136 | struct xfrm_state *tunnel; |
| 137 | |
| 138 | /* If a tunnel, number of users + 1 */ |
| 139 | atomic_t tunnel_users; |
| 140 | |
| 141 | /* State for replay detection */ |
| 142 | struct xfrm_replay_state replay; |
| 143 | |
Jamal Hadi Salim | f8cd548 | 2006-03-20 19:15:11 -0800 | [diff] [blame] | 144 | /* Replay detection state at the time we sent the last notification */ |
| 145 | struct xfrm_replay_state preplay; |
| 146 | |
Jamal Hadi Salim | 2717096 | 2006-04-14 15:03:05 -0700 | [diff] [blame] | 147 | /* internal flag that only holds state for delayed aevent at the |
| 148 | * moment |
| 149 | */ |
| 150 | u32 xflags; |
| 151 | |
Jamal Hadi Salim | f8cd548 | 2006-03-20 19:15:11 -0800 | [diff] [blame] | 152 | /* Replay detection notification settings */ |
| 153 | u32 replay_maxage; |
| 154 | u32 replay_maxdiff; |
| 155 | |
| 156 | /* Replay detection notification timer */ |
| 157 | struct timer_list rtimer; |
| 158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | /* Statistics */ |
| 160 | struct xfrm_stats stats; |
| 161 | |
| 162 | struct xfrm_lifetime_cur curlft; |
| 163 | struct timer_list timer; |
| 164 | |
| 165 | /* Reference to data common to all the instances of this |
| 166 | * transformer. */ |
| 167 | struct xfrm_type *type; |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 168 | struct xfrm_mode *mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 170 | /* Security context */ |
| 171 | struct xfrm_sec_ctx *security; |
| 172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | /* Private data of this transformer, format is opaque, |
| 174 | * interpreted by xfrm_type methods. */ |
| 175 | void *data; |
| 176 | }; |
| 177 | |
Jamal Hadi Salim | 2717096 | 2006-04-14 15:03:05 -0700 | [diff] [blame] | 178 | /* xflags - make enum if more show up */ |
| 179 | #define XFRM_TIME_DEFER 1 |
| 180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | enum { |
| 182 | XFRM_STATE_VOID, |
| 183 | XFRM_STATE_ACQ, |
| 184 | XFRM_STATE_VALID, |
| 185 | XFRM_STATE_ERROR, |
| 186 | XFRM_STATE_EXPIRED, |
| 187 | XFRM_STATE_DEAD |
| 188 | }; |
| 189 | |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 190 | /* callback structure passed from either netlink or pfkey */ |
| 191 | struct km_event |
| 192 | { |
Herbert Xu | bf08867f9 | 2005-06-18 22:44:00 -0700 | [diff] [blame] | 193 | union { |
| 194 | u32 hard; |
| 195 | u32 proto; |
| 196 | u32 byid; |
Jamal Hadi Salim | f8cd548 | 2006-03-20 19:15:11 -0800 | [diff] [blame] | 197 | u32 aevent; |
Herbert Xu | bf08867f9 | 2005-06-18 22:44:00 -0700 | [diff] [blame] | 198 | } data; |
| 199 | |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 200 | u32 seq; |
| 201 | u32 pid; |
| 202 | u32 event; |
| 203 | }; |
| 204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | struct xfrm_type; |
| 206 | struct xfrm_dst; |
| 207 | struct xfrm_policy_afinfo { |
| 208 | unsigned short family; |
Herbert Xu | 73654d6 | 2006-05-27 23:06:33 -0700 | [diff] [blame] | 209 | struct xfrm_type *type_map[IPPROTO_MAX]; |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 210 | struct xfrm_mode *mode_map[XFRM_MODE_MAX]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | struct dst_ops *dst_ops; |
| 212 | void (*garbage_collect)(void); |
| 213 | int (*dst_lookup)(struct xfrm_dst **dst, struct flowi *fl); |
| 214 | struct dst_entry *(*find_bundle)(struct flowi *fl, struct xfrm_policy *policy); |
| 215 | int (*bundle_create)(struct xfrm_policy *policy, |
| 216 | struct xfrm_state **xfrm, |
| 217 | int nx, |
| 218 | struct flowi *fl, |
| 219 | struct dst_entry **dst_p); |
| 220 | void (*decode_session)(struct sk_buff *skb, |
| 221 | struct flowi *fl); |
| 222 | }; |
| 223 | |
| 224 | extern int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo); |
| 225 | extern int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo); |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 226 | extern void km_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c); |
| 227 | extern void km_state_notify(struct xfrm_state *x, struct km_event *c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | #define XFRM_ACQ_EXPIRES 30 |
| 229 | |
| 230 | struct xfrm_tmpl; |
Jamal Hadi Salim | 980ebd2 | 2006-03-20 19:16:40 -0800 | [diff] [blame] | 231 | extern int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol); |
Jamal Hadi Salim | 53bc6b4d | 2006-03-20 19:17:03 -0800 | [diff] [blame] | 232 | extern void km_state_expired(struct xfrm_state *x, int hard, u32 pid); |
| 233 | extern int __xfrm_state_delete(struct xfrm_state *x); |
| 234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | struct xfrm_state_afinfo { |
| 236 | unsigned short family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | struct list_head *state_bydst; |
| 238 | struct list_head *state_byspi; |
Herbert Xu | d094cd8 | 2005-06-20 13:19:41 -0700 | [diff] [blame] | 239 | int (*init_flags)(struct xfrm_state *x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | void (*init_tempsel)(struct xfrm_state *x, struct flowi *fl, |
| 241 | struct xfrm_tmpl *tmpl, |
| 242 | xfrm_address_t *daddr, xfrm_address_t *saddr); |
| 243 | struct xfrm_state *(*state_lookup)(xfrm_address_t *daddr, u32 spi, u8 proto); |
| 244 | struct xfrm_state *(*find_acq)(u8 mode, u32 reqid, u8 proto, |
| 245 | xfrm_address_t *daddr, xfrm_address_t *saddr, |
| 246 | int create); |
| 247 | }; |
| 248 | |
| 249 | extern int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo); |
| 250 | extern int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo); |
| 251 | |
| 252 | extern void xfrm_state_delete_tunnel(struct xfrm_state *x); |
| 253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | struct xfrm_type |
| 255 | { |
| 256 | char *description; |
| 257 | struct module *owner; |
| 258 | __u8 proto; |
| 259 | |
Herbert Xu | 72cb696 | 2005-06-20 13:18:08 -0700 | [diff] [blame] | 260 | int (*init_state)(struct xfrm_state *x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | void (*destructor)(struct xfrm_state *); |
Herbert Xu | e695633 | 2006-04-01 00:52:46 -0800 | [diff] [blame] | 262 | int (*input)(struct xfrm_state *, struct sk_buff *skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | int (*output)(struct xfrm_state *, struct sk_buff *pskb); |
| 264 | /* Estimate maximal size of result of transformation of a dgram */ |
| 265 | u32 (*get_max_size)(struct xfrm_state *, int size); |
| 266 | }; |
| 267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | extern int xfrm_register_type(struct xfrm_type *type, unsigned short family); |
| 269 | extern int xfrm_unregister_type(struct xfrm_type *type, unsigned short family); |
| 270 | extern struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family); |
| 271 | extern void xfrm_put_type(struct xfrm_type *type); |
| 272 | |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 273 | struct xfrm_mode { |
| 274 | int (*input)(struct xfrm_state *x, struct sk_buff *skb); |
| 275 | int (*output)(struct sk_buff *skb); |
| 276 | |
| 277 | struct module *owner; |
| 278 | unsigned int encap; |
| 279 | }; |
| 280 | |
| 281 | extern int xfrm_register_mode(struct xfrm_mode *mode, int family); |
| 282 | extern int xfrm_unregister_mode(struct xfrm_mode *mode, int family); |
| 283 | extern struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family); |
| 284 | extern void xfrm_put_mode(struct xfrm_mode *mode); |
| 285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | struct xfrm_tmpl |
| 287 | { |
| 288 | /* id in template is interpreted as: |
| 289 | * daddr - destination of tunnel, may be zero for transport mode. |
| 290 | * spi - zero to acquire spi. Not zero if spi is static, then |
| 291 | * daddr must be fixed too. |
| 292 | * proto - AH/ESP/IPCOMP |
| 293 | */ |
| 294 | struct xfrm_id id; |
| 295 | |
| 296 | /* Source address of tunnel. Ignored, if it is not a tunnel. */ |
| 297 | xfrm_address_t saddr; |
| 298 | |
| 299 | __u32 reqid; |
| 300 | |
| 301 | /* Mode: transport/tunnel */ |
| 302 | __u8 mode; |
| 303 | |
| 304 | /* Sharing mode: unique, this session only, this user only etc. */ |
| 305 | __u8 share; |
| 306 | |
| 307 | /* May skip this transfomration if no SA is found */ |
| 308 | __u8 optional; |
| 309 | |
| 310 | /* Bit mask of algos allowed for acquisition */ |
| 311 | __u32 aalgos; |
| 312 | __u32 ealgos; |
| 313 | __u32 calgos; |
| 314 | }; |
| 315 | |
| 316 | #define XFRM_MAX_DEPTH 4 |
| 317 | |
| 318 | struct xfrm_policy |
| 319 | { |
| 320 | struct xfrm_policy *next; |
| 321 | struct list_head list; |
| 322 | |
| 323 | /* This lock only affects elements except for entry. */ |
| 324 | rwlock_t lock; |
| 325 | atomic_t refcnt; |
| 326 | struct timer_list timer; |
| 327 | |
| 328 | u32 priority; |
| 329 | u32 index; |
| 330 | struct xfrm_selector selector; |
| 331 | struct xfrm_lifetime_cfg lft; |
| 332 | struct xfrm_lifetime_cur curlft; |
| 333 | struct dst_entry *bundles; |
| 334 | __u16 family; |
| 335 | __u8 action; |
| 336 | __u8 flags; |
| 337 | __u8 dead; |
| 338 | __u8 xfrm_nr; |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 339 | struct xfrm_sec_ctx *security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | struct xfrm_tmpl xfrm_vec[XFRM_MAX_DEPTH]; |
| 341 | }; |
| 342 | |
Jamal Hadi Salim | f8cd548 | 2006-03-20 19:15:11 -0800 | [diff] [blame] | 343 | #define XFRM_KM_TIMEOUT 30 |
| 344 | /* which seqno */ |
| 345 | #define XFRM_REPLAY_SEQ 1 |
| 346 | #define XFRM_REPLAY_OSEQ 2 |
| 347 | #define XFRM_REPLAY_SEQ_MASK 3 |
| 348 | /* what happened */ |
| 349 | #define XFRM_REPLAY_UPDATE XFRM_AE_CR |
| 350 | #define XFRM_REPLAY_TIMEOUT XFRM_AE_CE |
| 351 | |
| 352 | /* default aevent timeout in units of 100ms */ |
| 353 | #define XFRM_AE_ETIME 10 |
| 354 | /* Async Event timer multiplier */ |
| 355 | #define XFRM_AE_ETH_M 10 |
| 356 | /* default seq threshold size */ |
| 357 | #define XFRM_AE_SEQT_SIZE 2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
| 359 | struct xfrm_mgr |
| 360 | { |
| 361 | struct list_head list; |
| 362 | char *id; |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 363 | int (*notify)(struct xfrm_state *x, struct km_event *c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | int (*acquire)(struct xfrm_state *x, struct xfrm_tmpl *, struct xfrm_policy *xp, int dir); |
| 365 | struct xfrm_policy *(*compile_policy)(u16 family, int opt, u8 *data, int len, int *dir); |
| 366 | int (*new_mapping)(struct xfrm_state *x, xfrm_address_t *ipaddr, u16 sport); |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 367 | int (*notify_policy)(struct xfrm_policy *x, int dir, struct km_event *c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | }; |
| 369 | |
| 370 | extern int xfrm_register_km(struct xfrm_mgr *km); |
| 371 | extern int xfrm_unregister_km(struct xfrm_mgr *km); |
| 372 | |
| 373 | |
| 374 | extern struct xfrm_policy *xfrm_policy_list[XFRM_POLICY_MAX*2]; |
| 375 | |
| 376 | static inline void xfrm_pol_hold(struct xfrm_policy *policy) |
| 377 | { |
| 378 | if (likely(policy != NULL)) |
| 379 | atomic_inc(&policy->refcnt); |
| 380 | } |
| 381 | |
| 382 | extern void __xfrm_policy_destroy(struct xfrm_policy *policy); |
| 383 | |
| 384 | static inline void xfrm_pol_put(struct xfrm_policy *policy) |
| 385 | { |
| 386 | if (atomic_dec_and_test(&policy->refcnt)) |
| 387 | __xfrm_policy_destroy(policy); |
| 388 | } |
| 389 | |
| 390 | #define XFRM_DST_HSIZE 1024 |
| 391 | |
| 392 | static __inline__ |
| 393 | unsigned __xfrm4_dst_hash(xfrm_address_t *addr) |
| 394 | { |
| 395 | unsigned h; |
| 396 | h = ntohl(addr->a4); |
| 397 | h = (h ^ (h>>16)) % XFRM_DST_HSIZE; |
| 398 | return h; |
| 399 | } |
| 400 | |
| 401 | static __inline__ |
| 402 | unsigned __xfrm6_dst_hash(xfrm_address_t *addr) |
| 403 | { |
| 404 | unsigned h; |
| 405 | h = ntohl(addr->a6[2]^addr->a6[3]); |
| 406 | h = (h ^ (h>>16)) % XFRM_DST_HSIZE; |
| 407 | return h; |
| 408 | } |
| 409 | |
| 410 | static __inline__ |
| 411 | unsigned xfrm_dst_hash(xfrm_address_t *addr, unsigned short family) |
| 412 | { |
| 413 | switch (family) { |
| 414 | case AF_INET: |
| 415 | return __xfrm4_dst_hash(addr); |
| 416 | case AF_INET6: |
| 417 | return __xfrm6_dst_hash(addr); |
| 418 | } |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | static __inline__ |
| 423 | unsigned __xfrm4_spi_hash(xfrm_address_t *addr, u32 spi, u8 proto) |
| 424 | { |
| 425 | unsigned h; |
| 426 | h = ntohl(addr->a4^spi^proto); |
| 427 | h = (h ^ (h>>10) ^ (h>>20)) % XFRM_DST_HSIZE; |
| 428 | return h; |
| 429 | } |
| 430 | |
| 431 | static __inline__ |
| 432 | unsigned __xfrm6_spi_hash(xfrm_address_t *addr, u32 spi, u8 proto) |
| 433 | { |
| 434 | unsigned h; |
| 435 | h = ntohl(addr->a6[2]^addr->a6[3]^spi^proto); |
| 436 | h = (h ^ (h>>10) ^ (h>>20)) % XFRM_DST_HSIZE; |
| 437 | return h; |
| 438 | } |
| 439 | |
| 440 | static __inline__ |
| 441 | unsigned xfrm_spi_hash(xfrm_address_t *addr, u32 spi, u8 proto, unsigned short family) |
| 442 | { |
| 443 | switch (family) { |
| 444 | case AF_INET: |
| 445 | return __xfrm4_spi_hash(addr, spi, proto); |
| 446 | case AF_INET6: |
| 447 | return __xfrm6_spi_hash(addr, spi, proto); |
| 448 | } |
| 449 | return 0; /*XXX*/ |
| 450 | } |
| 451 | |
| 452 | extern void __xfrm_state_destroy(struct xfrm_state *); |
| 453 | |
Herbert Xu | 21380b8 | 2006-02-22 14:47:13 -0800 | [diff] [blame] | 454 | static inline void __xfrm_state_put(struct xfrm_state *x) |
| 455 | { |
| 456 | atomic_dec(&x->refcnt); |
| 457 | } |
| 458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | static inline void xfrm_state_put(struct xfrm_state *x) |
| 460 | { |
| 461 | if (atomic_dec_and_test(&x->refcnt)) |
| 462 | __xfrm_state_destroy(x); |
| 463 | } |
| 464 | |
| 465 | static inline void xfrm_state_hold(struct xfrm_state *x) |
| 466 | { |
| 467 | atomic_inc(&x->refcnt); |
| 468 | } |
| 469 | |
| 470 | static __inline__ int addr_match(void *token1, void *token2, int prefixlen) |
| 471 | { |
| 472 | __u32 *a1 = token1; |
| 473 | __u32 *a2 = token2; |
| 474 | int pdw; |
| 475 | int pbi; |
| 476 | |
| 477 | pdw = prefixlen >> 5; /* num of whole __u32 in prefix */ |
| 478 | pbi = prefixlen & 0x1f; /* num of bits in incomplete u32 in prefix */ |
| 479 | |
| 480 | if (pdw) |
| 481 | if (memcmp(a1, a2, pdw << 2)) |
| 482 | return 0; |
| 483 | |
| 484 | if (pbi) { |
| 485 | __u32 mask; |
| 486 | |
| 487 | mask = htonl((0xffffffff) << (32 - pbi)); |
| 488 | |
| 489 | if ((a1[pdw] ^ a2[pdw]) & mask) |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | return 1; |
| 494 | } |
| 495 | |
| 496 | static __inline__ |
| 497 | u16 xfrm_flowi_sport(struct flowi *fl) |
| 498 | { |
| 499 | u16 port; |
| 500 | switch(fl->proto) { |
| 501 | case IPPROTO_TCP: |
| 502 | case IPPROTO_UDP: |
| 503 | case IPPROTO_SCTP: |
| 504 | port = fl->fl_ip_sport; |
| 505 | break; |
| 506 | case IPPROTO_ICMP: |
| 507 | case IPPROTO_ICMPV6: |
| 508 | port = htons(fl->fl_icmp_type); |
| 509 | break; |
| 510 | default: |
| 511 | port = 0; /*XXX*/ |
| 512 | } |
| 513 | return port; |
| 514 | } |
| 515 | |
| 516 | static __inline__ |
| 517 | u16 xfrm_flowi_dport(struct flowi *fl) |
| 518 | { |
| 519 | u16 port; |
| 520 | switch(fl->proto) { |
| 521 | case IPPROTO_TCP: |
| 522 | case IPPROTO_UDP: |
| 523 | case IPPROTO_SCTP: |
| 524 | port = fl->fl_ip_dport; |
| 525 | break; |
| 526 | case IPPROTO_ICMP: |
| 527 | case IPPROTO_ICMPV6: |
| 528 | port = htons(fl->fl_icmp_code); |
| 529 | break; |
| 530 | default: |
| 531 | port = 0; /*XXX*/ |
| 532 | } |
| 533 | return port; |
| 534 | } |
| 535 | |
| 536 | static inline int |
| 537 | __xfrm4_selector_match(struct xfrm_selector *sel, struct flowi *fl) |
| 538 | { |
| 539 | return addr_match(&fl->fl4_dst, &sel->daddr, sel->prefixlen_d) && |
| 540 | addr_match(&fl->fl4_src, &sel->saddr, sel->prefixlen_s) && |
| 541 | !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) && |
| 542 | !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) && |
| 543 | (fl->proto == sel->proto || !sel->proto) && |
| 544 | (fl->oif == sel->ifindex || !sel->ifindex); |
| 545 | } |
| 546 | |
| 547 | static inline int |
| 548 | __xfrm6_selector_match(struct xfrm_selector *sel, struct flowi *fl) |
| 549 | { |
| 550 | return addr_match(&fl->fl6_dst, &sel->daddr, sel->prefixlen_d) && |
| 551 | addr_match(&fl->fl6_src, &sel->saddr, sel->prefixlen_s) && |
| 552 | !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) && |
| 553 | !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) && |
| 554 | (fl->proto == sel->proto || !sel->proto) && |
| 555 | (fl->oif == sel->ifindex || !sel->ifindex); |
| 556 | } |
| 557 | |
| 558 | static inline int |
| 559 | xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl, |
| 560 | unsigned short family) |
| 561 | { |
| 562 | switch (family) { |
| 563 | case AF_INET: |
| 564 | return __xfrm4_selector_match(sel, fl); |
| 565 | case AF_INET6: |
| 566 | return __xfrm6_selector_match(sel, fl); |
| 567 | } |
| 568 | return 0; |
| 569 | } |
| 570 | |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 571 | #ifdef CONFIG_SECURITY_NETWORK_XFRM |
| 572 | /* If neither has a context --> match |
| 573 | * Otherwise, both must have a context and the sids, doi, alg must match |
| 574 | */ |
| 575 | static inline int xfrm_sec_ctx_match(struct xfrm_sec_ctx *s1, struct xfrm_sec_ctx *s2) |
| 576 | { |
| 577 | return ((!s1 && !s2) || |
| 578 | (s1 && s2 && |
| 579 | (s1->ctx_sid == s2->ctx_sid) && |
| 580 | (s1->ctx_doi == s2->ctx_doi) && |
| 581 | (s1->ctx_alg == s2->ctx_alg))); |
| 582 | } |
| 583 | #else |
| 584 | static inline int xfrm_sec_ctx_match(struct xfrm_sec_ctx *s1, struct xfrm_sec_ctx *s2) |
| 585 | { |
| 586 | return 1; |
| 587 | } |
| 588 | #endif |
| 589 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | /* A struct encoding bundle of transformations to apply to some set of flow. |
| 591 | * |
| 592 | * dst->child points to the next element of bundle. |
| 593 | * dst->xfrm points to an instanse of transformer. |
| 594 | * |
| 595 | * Due to unfortunate limitations of current routing cache, which we |
| 596 | * have no time to fix, it mirrors struct rtable and bound to the same |
| 597 | * routing key, including saddr,daddr. However, we can have many of |
| 598 | * bundles differing by session id. All the bundles grow from a parent |
| 599 | * policy rule. |
| 600 | */ |
| 601 | struct xfrm_dst |
| 602 | { |
| 603 | union { |
| 604 | struct xfrm_dst *next; |
| 605 | struct dst_entry dst; |
| 606 | struct rtable rt; |
| 607 | struct rt6_info rt6; |
| 608 | } u; |
| 609 | struct dst_entry *route; |
| 610 | u32 route_mtu_cached; |
| 611 | u32 child_mtu_cached; |
Hideaki YOSHIFUJI | 92d63de | 2005-05-26 12:58:04 -0700 | [diff] [blame] | 612 | u32 route_cookie; |
| 613 | u32 path_cookie; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | }; |
| 615 | |
Herbert Xu | aabc976 | 2005-05-03 16:27:10 -0700 | [diff] [blame] | 616 | static inline void xfrm_dst_destroy(struct xfrm_dst *xdst) |
| 617 | { |
| 618 | dst_release(xdst->route); |
| 619 | if (likely(xdst->u.dst.xfrm)) |
| 620 | xfrm_state_put(xdst->u.dst.xfrm); |
| 621 | } |
| 622 | |
| 623 | extern void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev); |
| 624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | struct sec_path |
| 626 | { |
| 627 | atomic_t refcnt; |
| 628 | int len; |
Herbert Xu | dbe5b4a | 2006-04-01 00:54:16 -0800 | [diff] [blame] | 629 | struct xfrm_state *xvec[XFRM_MAX_DEPTH]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | }; |
| 631 | |
| 632 | static inline struct sec_path * |
| 633 | secpath_get(struct sec_path *sp) |
| 634 | { |
| 635 | if (sp) |
| 636 | atomic_inc(&sp->refcnt); |
| 637 | return sp; |
| 638 | } |
| 639 | |
| 640 | extern void __secpath_destroy(struct sec_path *sp); |
| 641 | |
| 642 | static inline void |
| 643 | secpath_put(struct sec_path *sp) |
| 644 | { |
| 645 | if (sp && atomic_dec_and_test(&sp->refcnt)) |
| 646 | __secpath_destroy(sp); |
| 647 | } |
| 648 | |
| 649 | extern struct sec_path *secpath_dup(struct sec_path *src); |
| 650 | |
| 651 | static inline void |
| 652 | secpath_reset(struct sk_buff *skb) |
| 653 | { |
| 654 | #ifdef CONFIG_XFRM |
| 655 | secpath_put(skb->sp); |
| 656 | skb->sp = NULL; |
| 657 | #endif |
| 658 | } |
| 659 | |
| 660 | static inline int |
| 661 | __xfrm4_state_addr_cmp(struct xfrm_tmpl *tmpl, struct xfrm_state *x) |
| 662 | { |
| 663 | return (tmpl->saddr.a4 && |
| 664 | tmpl->saddr.a4 != x->props.saddr.a4); |
| 665 | } |
| 666 | |
| 667 | static inline int |
| 668 | __xfrm6_state_addr_cmp(struct xfrm_tmpl *tmpl, struct xfrm_state *x) |
| 669 | { |
| 670 | return (!ipv6_addr_any((struct in6_addr*)&tmpl->saddr) && |
| 671 | ipv6_addr_cmp((struct in6_addr *)&tmpl->saddr, (struct in6_addr*)&x->props.saddr)); |
| 672 | } |
| 673 | |
| 674 | static inline int |
| 675 | xfrm_state_addr_cmp(struct xfrm_tmpl *tmpl, struct xfrm_state *x, unsigned short family) |
| 676 | { |
| 677 | switch (family) { |
| 678 | case AF_INET: |
| 679 | return __xfrm4_state_addr_cmp(tmpl, x); |
| 680 | case AF_INET6: |
| 681 | return __xfrm6_state_addr_cmp(tmpl, x); |
| 682 | } |
| 683 | return !0; |
| 684 | } |
| 685 | |
| 686 | #ifdef CONFIG_XFRM |
| 687 | |
| 688 | extern int __xfrm_policy_check(struct sock *, int dir, struct sk_buff *skb, unsigned short family); |
| 689 | |
| 690 | static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family) |
| 691 | { |
| 692 | if (sk && sk->sk_policy[XFRM_POLICY_IN]) |
| 693 | return __xfrm_policy_check(sk, dir, skb, family); |
| 694 | |
| 695 | return (!xfrm_policy_list[dir] && !skb->sp) || |
| 696 | (skb->dst->flags & DST_NOPOLICY) || |
| 697 | __xfrm_policy_check(sk, dir, skb, family); |
| 698 | } |
| 699 | |
| 700 | static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb) |
| 701 | { |
| 702 | return xfrm_policy_check(sk, dir, skb, AF_INET); |
| 703 | } |
| 704 | |
| 705 | static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb) |
| 706 | { |
| 707 | return xfrm_policy_check(sk, dir, skb, AF_INET6); |
| 708 | } |
| 709 | |
Patrick McHardy | 3e3850e | 2006-01-06 23:04:54 -0800 | [diff] [blame] | 710 | extern int xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | extern int __xfrm_route_forward(struct sk_buff *skb, unsigned short family); |
| 712 | |
| 713 | static inline int xfrm_route_forward(struct sk_buff *skb, unsigned short family) |
| 714 | { |
| 715 | return !xfrm_policy_list[XFRM_POLICY_OUT] || |
| 716 | (skb->dst->flags & DST_NOXFRM) || |
| 717 | __xfrm_route_forward(skb, family); |
| 718 | } |
| 719 | |
| 720 | static inline int xfrm4_route_forward(struct sk_buff *skb) |
| 721 | { |
| 722 | return xfrm_route_forward(skb, AF_INET); |
| 723 | } |
| 724 | |
| 725 | static inline int xfrm6_route_forward(struct sk_buff *skb) |
| 726 | { |
| 727 | return xfrm_route_forward(skb, AF_INET6); |
| 728 | } |
| 729 | |
| 730 | extern int __xfrm_sk_clone_policy(struct sock *sk); |
| 731 | |
| 732 | static inline int xfrm_sk_clone_policy(struct sock *sk) |
| 733 | { |
| 734 | if (unlikely(sk->sk_policy[0] || sk->sk_policy[1])) |
| 735 | return __xfrm_sk_clone_policy(sk); |
| 736 | return 0; |
| 737 | } |
| 738 | |
Herbert Xu | 4666faa | 2005-06-18 22:43:22 -0700 | [diff] [blame] | 739 | extern int xfrm_policy_delete(struct xfrm_policy *pol, int dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | |
| 741 | static inline void xfrm_sk_free_policy(struct sock *sk) |
| 742 | { |
| 743 | if (unlikely(sk->sk_policy[0] != NULL)) { |
| 744 | xfrm_policy_delete(sk->sk_policy[0], XFRM_POLICY_MAX); |
| 745 | sk->sk_policy[0] = NULL; |
| 746 | } |
| 747 | if (unlikely(sk->sk_policy[1] != NULL)) { |
| 748 | xfrm_policy_delete(sk->sk_policy[1], XFRM_POLICY_MAX+1); |
| 749 | sk->sk_policy[1] = NULL; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | #else |
| 754 | |
| 755 | static inline void xfrm_sk_free_policy(struct sock *sk) {} |
| 756 | static inline int xfrm_sk_clone_policy(struct sock *sk) { return 0; } |
| 757 | static inline int xfrm6_route_forward(struct sk_buff *skb) { return 1; } |
| 758 | static inline int xfrm4_route_forward(struct sk_buff *skb) { return 1; } |
| 759 | static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb) |
| 760 | { |
| 761 | return 1; |
| 762 | } |
| 763 | static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb) |
| 764 | { |
| 765 | return 1; |
| 766 | } |
| 767 | static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family) |
| 768 | { |
| 769 | return 1; |
| 770 | } |
| 771 | #endif |
| 772 | |
| 773 | static __inline__ |
| 774 | xfrm_address_t *xfrm_flowi_daddr(struct flowi *fl, unsigned short family) |
| 775 | { |
| 776 | switch (family){ |
| 777 | case AF_INET: |
| 778 | return (xfrm_address_t *)&fl->fl4_dst; |
| 779 | case AF_INET6: |
| 780 | return (xfrm_address_t *)&fl->fl6_dst; |
| 781 | } |
| 782 | return NULL; |
| 783 | } |
| 784 | |
| 785 | static __inline__ |
| 786 | xfrm_address_t *xfrm_flowi_saddr(struct flowi *fl, unsigned short family) |
| 787 | { |
| 788 | switch (family){ |
| 789 | case AF_INET: |
| 790 | return (xfrm_address_t *)&fl->fl4_src; |
| 791 | case AF_INET6: |
| 792 | return (xfrm_address_t *)&fl->fl6_src; |
| 793 | } |
| 794 | return NULL; |
| 795 | } |
| 796 | |
| 797 | static __inline__ int |
| 798 | __xfrm4_state_addr_check(struct xfrm_state *x, |
| 799 | xfrm_address_t *daddr, xfrm_address_t *saddr) |
| 800 | { |
| 801 | if (daddr->a4 == x->id.daddr.a4 && |
| 802 | (saddr->a4 == x->props.saddr.a4 || !saddr->a4 || !x->props.saddr.a4)) |
| 803 | return 1; |
| 804 | return 0; |
| 805 | } |
| 806 | |
| 807 | static __inline__ int |
| 808 | __xfrm6_state_addr_check(struct xfrm_state *x, |
| 809 | xfrm_address_t *daddr, xfrm_address_t *saddr) |
| 810 | { |
| 811 | if (!ipv6_addr_cmp((struct in6_addr *)daddr, (struct in6_addr *)&x->id.daddr) && |
| 812 | (!ipv6_addr_cmp((struct in6_addr *)saddr, (struct in6_addr *)&x->props.saddr)|| |
| 813 | ipv6_addr_any((struct in6_addr *)saddr) || |
| 814 | ipv6_addr_any((struct in6_addr *)&x->props.saddr))) |
| 815 | return 1; |
| 816 | return 0; |
| 817 | } |
| 818 | |
| 819 | static __inline__ int |
| 820 | xfrm_state_addr_check(struct xfrm_state *x, |
| 821 | xfrm_address_t *daddr, xfrm_address_t *saddr, |
| 822 | unsigned short family) |
| 823 | { |
| 824 | switch (family) { |
| 825 | case AF_INET: |
| 826 | return __xfrm4_state_addr_check(x, daddr, saddr); |
| 827 | case AF_INET6: |
| 828 | return __xfrm6_state_addr_check(x, daddr, saddr); |
| 829 | } |
| 830 | return 0; |
| 831 | } |
| 832 | |
| 833 | static inline int xfrm_state_kern(struct xfrm_state *x) |
| 834 | { |
| 835 | return atomic_read(&x->tunnel_users); |
| 836 | } |
| 837 | |
| 838 | /* |
| 839 | * xfrm algorithm information |
| 840 | */ |
| 841 | struct xfrm_algo_auth_info { |
| 842 | u16 icv_truncbits; |
| 843 | u16 icv_fullbits; |
| 844 | }; |
| 845 | |
| 846 | struct xfrm_algo_encr_info { |
| 847 | u16 blockbits; |
| 848 | u16 defkeybits; |
| 849 | }; |
| 850 | |
| 851 | struct xfrm_algo_comp_info { |
| 852 | u16 threshold; |
| 853 | }; |
| 854 | |
| 855 | struct xfrm_algo_desc { |
| 856 | char *name; |
| 857 | u8 available:1; |
| 858 | union { |
| 859 | struct xfrm_algo_auth_info auth; |
| 860 | struct xfrm_algo_encr_info encr; |
| 861 | struct xfrm_algo_comp_info comp; |
| 862 | } uinfo; |
| 863 | struct sadb_alg desc; |
| 864 | }; |
| 865 | |
| 866 | /* XFRM tunnel handlers. */ |
| 867 | struct xfrm_tunnel { |
| 868 | int (*handler)(struct sk_buff *skb); |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 869 | int (*err_handler)(struct sk_buff *skb, __u32 info); |
| 870 | |
| 871 | struct xfrm_tunnel *next; |
| 872 | int priority; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | }; |
| 874 | |
| 875 | struct xfrm6_tunnel { |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 876 | int (*handler)(struct sk_buff *skb); |
| 877 | int (*err_handler)(struct sk_buff *skb, struct inet6_skb_parm *opt, |
| 878 | int type, int code, int offset, __u32 info); |
| 879 | |
| 880 | struct xfrm6_tunnel *next; |
| 881 | int priority; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | }; |
| 883 | |
| 884 | extern void xfrm_init(void); |
| 885 | extern void xfrm4_init(void); |
| 886 | extern void xfrm6_init(void); |
| 887 | extern void xfrm6_fini(void); |
| 888 | extern void xfrm_state_init(void); |
| 889 | extern void xfrm4_state_init(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | extern void xfrm6_state_init(void); |
| 891 | extern void xfrm6_state_fini(void); |
| 892 | |
| 893 | extern int xfrm_state_walk(u8 proto, int (*func)(struct xfrm_state *, int, void*), void *); |
| 894 | extern struct xfrm_state *xfrm_state_alloc(void); |
| 895 | extern struct xfrm_state *xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr, |
| 896 | struct flowi *fl, struct xfrm_tmpl *tmpl, |
| 897 | struct xfrm_policy *pol, int *err, |
| 898 | unsigned short family); |
| 899 | extern int xfrm_state_check_expire(struct xfrm_state *x); |
| 900 | extern void xfrm_state_insert(struct xfrm_state *x); |
| 901 | extern int xfrm_state_add(struct xfrm_state *x); |
| 902 | extern int xfrm_state_update(struct xfrm_state *x); |
| 903 | extern struct xfrm_state *xfrm_state_lookup(xfrm_address_t *daddr, u32 spi, u8 proto, unsigned short family); |
| 904 | extern struct xfrm_state *xfrm_find_acq_byseq(u32 seq); |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 905 | extern int xfrm_state_delete(struct xfrm_state *x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | extern void xfrm_state_flush(u8 proto); |
| 907 | extern int xfrm_replay_check(struct xfrm_state *x, u32 seq); |
| 908 | extern void xfrm_replay_advance(struct xfrm_state *x, u32 seq); |
Jamal Hadi Salim | f8cd548 | 2006-03-20 19:15:11 -0800 | [diff] [blame] | 909 | extern void xfrm_replay_notify(struct xfrm_state *x, int event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | extern int xfrm_state_check(struct xfrm_state *x, struct sk_buff *skb); |
| 911 | extern int xfrm_state_mtu(struct xfrm_state *x, int mtu); |
Herbert Xu | 72cb696 | 2005-06-20 13:18:08 -0700 | [diff] [blame] | 912 | extern int xfrm_init_state(struct xfrm_state *x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | extern int xfrm4_rcv(struct sk_buff *skb); |
| 914 | extern int xfrm4_output(struct sk_buff *skb); |
| 915 | extern int xfrm4_tunnel_register(struct xfrm_tunnel *handler); |
| 916 | extern int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler); |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 917 | extern int xfrm6_rcv_spi(struct sk_buff *skb, u32 spi); |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 918 | extern int xfrm6_rcv(struct sk_buff **pskb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | extern int xfrm6_tunnel_register(struct xfrm6_tunnel *handler); |
| 920 | extern int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler); |
| 921 | extern u32 xfrm6_tunnel_alloc_spi(xfrm_address_t *saddr); |
| 922 | extern void xfrm6_tunnel_free_spi(xfrm_address_t *saddr); |
| 923 | extern u32 xfrm6_tunnel_spi_lookup(xfrm_address_t *saddr); |
| 924 | extern int xfrm6_output(struct sk_buff *skb); |
| 925 | |
| 926 | #ifdef CONFIG_XFRM |
| 927 | extern int xfrm4_rcv_encap(struct sk_buff *skb, __u16 encap_type); |
| 928 | extern int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen); |
| 929 | extern int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl, unsigned short family); |
| 930 | #else |
| 931 | static inline int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen) |
| 932 | { |
| 933 | return -ENOPROTOOPT; |
| 934 | } |
| 935 | |
| 936 | static inline int xfrm4_rcv_encap(struct sk_buff *skb, __u16 encap_type) |
| 937 | { |
| 938 | /* should not happen */ |
| 939 | kfree_skb(skb); |
| 940 | return 0; |
| 941 | } |
| 942 | static inline int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl, unsigned short family) |
| 943 | { |
| 944 | return -EINVAL; |
| 945 | } |
| 946 | #endif |
| 947 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 948 | struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | extern int xfrm_policy_walk(int (*func)(struct xfrm_policy *, int, int, void*), void *); |
| 950 | int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl); |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 951 | struct xfrm_policy *xfrm_policy_bysel_ctx(int dir, struct xfrm_selector *sel, |
| 952 | struct xfrm_sec_ctx *ctx, int delete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | struct xfrm_policy *xfrm_policy_byid(int dir, u32 id, int delete); |
| 954 | void xfrm_policy_flush(void); |
| 955 | u32 xfrm_get_acqseq(void); |
| 956 | void xfrm_alloc_spi(struct xfrm_state *x, u32 minspi, u32 maxspi); |
| 957 | struct xfrm_state * xfrm_find_acq(u8 mode, u32 reqid, u8 proto, |
| 958 | xfrm_address_t *daddr, xfrm_address_t *saddr, |
| 959 | int create, unsigned short family); |
| 960 | extern void xfrm_policy_flush(void); |
| 961 | extern int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol); |
| 962 | extern int xfrm_flush_bundles(void); |
David S. Miller | 399c180 | 2005-12-19 14:23:23 -0800 | [diff] [blame] | 963 | extern void xfrm_flush_all_bundles(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | extern int xfrm_bundle_ok(struct xfrm_dst *xdst, struct flowi *fl, int family); |
| 965 | extern void xfrm_init_pmtu(struct dst_entry *dst); |
| 966 | |
| 967 | extern wait_queue_head_t km_waitq; |
| 968 | extern int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, u16 sport); |
Jamal Hadi Salim | 6c5c8ca | 2006-03-20 19:17:25 -0800 | [diff] [blame] | 969 | extern void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | |
| 971 | extern void xfrm_input_init(void); |
| 972 | extern int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, u32 *spi, u32 *seq); |
| 973 | |
| 974 | extern void xfrm_probe_algs(void); |
| 975 | extern int xfrm_count_auth_supported(void); |
| 976 | extern int xfrm_count_enc_supported(void); |
| 977 | extern struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx); |
| 978 | extern struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx); |
| 979 | extern struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id); |
| 980 | extern struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id); |
| 981 | extern struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id); |
| 982 | extern struct xfrm_algo_desc *xfrm_aalg_get_byname(char *name, int probe); |
| 983 | extern struct xfrm_algo_desc *xfrm_ealg_get_byname(char *name, int probe); |
| 984 | extern struct xfrm_algo_desc *xfrm_calg_get_byname(char *name, int probe); |
| 985 | |
| 986 | struct crypto_tfm; |
Herbert Xu | 9409f38 | 2006-08-06 19:49:12 +1000 | [diff] [blame^] | 987 | struct scatterlist; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | typedef void (icv_update_fn_t)(struct crypto_tfm *, struct scatterlist *, unsigned int); |
| 989 | |
| 990 | extern void skb_icv_walk(const struct sk_buff *skb, struct crypto_tfm *tfm, |
| 991 | int offset, int len, icv_update_fn_t icv_update); |
| 992 | |
| 993 | static inline int xfrm_addr_cmp(xfrm_address_t *a, xfrm_address_t *b, |
| 994 | int family) |
| 995 | { |
| 996 | switch (family) { |
| 997 | default: |
| 998 | case AF_INET: |
| 999 | return a->a4 - b->a4; |
| 1000 | case AF_INET6: |
| 1001 | return ipv6_addr_cmp((struct in6_addr *)a, |
| 1002 | (struct in6_addr *)b); |
| 1003 | } |
| 1004 | } |
| 1005 | |
Herbert Xu | 77d8d7a | 2005-10-05 12:15:12 -0700 | [diff] [blame] | 1006 | static inline int xfrm_policy_id2dir(u32 index) |
| 1007 | { |
| 1008 | return index & 7; |
| 1009 | } |
| 1010 | |
Jamal Hadi Salim | f8cd548 | 2006-03-20 19:15:11 -0800 | [diff] [blame] | 1011 | static inline int xfrm_aevent_is_on(void) |
| 1012 | { |
Patrick McHardy | be33690 | 2006-03-20 22:40:54 -0800 | [diff] [blame] | 1013 | struct sock *nlsk; |
| 1014 | int ret = 0; |
| 1015 | |
| 1016 | rcu_read_lock(); |
| 1017 | nlsk = rcu_dereference(xfrm_nl); |
| 1018 | if (nlsk) |
| 1019 | ret = netlink_has_listeners(nlsk, XFRMNLGRP_AEVENTS); |
| 1020 | rcu_read_unlock(); |
| 1021 | return ret; |
Jamal Hadi Salim | f8cd548 | 2006-03-20 19:15:11 -0800 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | static inline void xfrm_aevent_doreplay(struct xfrm_state *x) |
| 1025 | { |
| 1026 | if (xfrm_aevent_is_on()) |
| 1027 | xfrm_replay_notify(x, XFRM_REPLAY_UPDATE); |
| 1028 | } |
| 1029 | |
| 1030 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | #endif /* _NET_XFRM_H */ |