~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/include/net/xfrm.h

Version: ~ [ linux-5.15-rc5 ] ~ [ linux-5.14.11 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.72 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.152 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.210 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.250 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.286 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.288 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 #ifndef _NET_XFRM_H
  2 #define _NET_XFRM_H
  3 
  4 #include <linux/xfrm.h>
  5 #include <linux/spinlock.h>
  6 #include <linux/list.h>
  7 #include <linux/skbuff.h>
  8 #include <linux/netdevice.h>
  9 #include <linux/crypto.h>
 10 #include <linux/pfkeyv2.h>
 11 #include <linux/in6.h>
 12 
 13 #include <net/sock.h>
 14 #include <net/dst.h>
 15 #include <net/route.h>
 16 #include <net/ipv6.h>
 17 #include <net/ip6_fib.h>
 18 
 19 #define XFRM_ALIGN8(len)        (((len) + 7) & ~7)
 20 
 21 extern struct semaphore xfrm_cfg_sem;
 22 
 23 /* Organization of SPD aka "XFRM rules"
 24    ------------------------------------
 25 
 26    Basic objects:
 27    - policy rule, struct xfrm_policy (=SPD entry)
 28    - bundle of transformations, struct dst_entry == struct xfrm_dst (=SA bundle)
 29    - instance of a transformer, struct xfrm_state (=SA)
 30    - template to clone xfrm_state, struct xfrm_tmpl
 31 
 32    SPD is plain linear list of xfrm_policy rules, ordered by priority.
 33    (To be compatible with existing pfkeyv2 implementations,
 34    many rules with priority of 0x7fffffff are allowed to exist and
 35    such rules are ordered in an unpredictable way, thanks to bsd folks.)
 36 
 37    Lookup is plain linear search until the first match with selector.
 38 
 39    If "action" is "block", then we prohibit the flow, otherwise:
 40    if "xfrms_nr" is zero, the flow passes untransformed. Otherwise,
 41    policy entry has list of up to XFRM_MAX_DEPTH transformations,
 42    described by templates xfrm_tmpl. Each template is resolved
 43    to a complete xfrm_state (see below) and we pack bundle of transformations
 44    to a dst_entry returned to requestor.
 45 
 46    dst -. xfrm  .-> xfrm_state #1
 47     |---. child .-> dst -. xfrm .-> xfrm_state #2
 48                      |---. child .-> dst -. xfrm .-> xfrm_state #3
 49                                       |---. child .-> NULL
 50 
 51    Bundles are cached at xrfm_policy struct (field ->bundles).
 52 
 53 
 54    Resolution of xrfm_tmpl
 55    -----------------------
 56    Template contains:
 57    1. ->mode            Mode: transport or tunnel
 58    2. ->id.proto        Protocol: AH/ESP/IPCOMP
 59    3. ->id.daddr        Remote tunnel endpoint, ignored for transport mode.
 60       Q: allow to resolve security gateway?
 61    4. ->id.spi          If not zero, static SPI.
 62    5. ->saddr           Local tunnel endpoint, ignored for transport mode.
 63    6. ->algos           List of allowed algos. Plain bitmask now.
 64       Q: ealgos, aalgos, calgos. What a mess...
 65    7. ->share           Sharing mode.
 66       Q: how to implement private sharing mode? To add struct sock* to
 67       flow id?
 68 
 69    Having this template we search through SAD searching for entries
 70    with appropriate mode/proto/algo, permitted by selector.
 71    If no appropriate entry found, it is requested from key manager.
 72 
 73    PROBLEMS:
 74    Q: How to find all the bundles referring to a physical path for
 75       PMTU discovery? Seems, dst should contain list of all parents...
 76       and enter to infinite locking hierarchy disaster.
 77       No! It is easier, we will not search for them, let them find us.
 78       We add genid to each dst plus pointer to genid of raw IP route,
 79       pmtu disc will update pmtu on raw IP route and increase its genid.
 80       dst_check() will see this for top level and trigger resyncing
 81       metrics. Plus, it will be made via sk->sk_dst_cache. Solved.
 82  */
 83 
 84 /* Full description of state of transformer. */
 85 struct xfrm_state
 86 {
 87         /* Note: bydst is re-used during gc */
 88         struct list_head        bydst;
 89         struct list_head        byspi;
 90 
 91         atomic_t                refcnt;
 92         spinlock_t              lock;
 93 
 94         struct xfrm_id          id;
 95         struct xfrm_selector    sel;
 96 
 97         /* Key manger bits */
 98         struct {
 99                 u8              state;
100                 u8              dying;
101                 u32             seq;
102         } km;
103 
104         /* Parameters of this state. */
105         struct {
106                 u32             reqid;
107                 u8              mode;
108                 u8              replay_window;
109                 u8              aalgo, ealgo, calgo;
110                 u8              flags;
111                 u16             family;
112                 xfrm_address_t  saddr;
113                 int             header_len;
114                 int             trailer_len;
115         } props;
116 
117         struct xfrm_lifetime_cfg lft;
118 
119         /* Data for transformer */
120         struct xfrm_algo        *aalg;
121         struct xfrm_algo        *ealg;
122         struct xfrm_algo        *calg;
123 
124         /* Data for encapsulator */
125         struct xfrm_encap_tmpl  *encap;
126 
127         /* IPComp needs an IPIP tunnel for handling uncompressed packets */
128         struct xfrm_state       *tunnel;
129 
130         /* If a tunnel, number of users + 1 */
131         atomic_t                tunnel_users;
132 
133         /* State for replay detection */
134         struct xfrm_replay_state replay;
135 
136         /* Statistics */
137         struct xfrm_stats       stats;
138 
139         struct xfrm_lifetime_cur curlft;
140         struct timer_list       timer;
141 
142         /* Reference to data common to all the instances of this
143          * transformer. */
144         struct xfrm_type        *type;
145 
146         /* Private data of this transformer, format is opaque,
147          * interpreted by xfrm_type methods. */
148         void                    *data;
149 };
150 
151 enum {
152         XFRM_STATE_VOID,
153         XFRM_STATE_ACQ,
154         XFRM_STATE_VALID,
155         XFRM_STATE_ERROR,
156         XFRM_STATE_EXPIRED,
157         XFRM_STATE_DEAD
158 };
159 
160 struct xfrm_type;
161 struct xfrm_dst;
162 struct xfrm_policy_afinfo {
163         unsigned short          family;
164         rwlock_t                lock;
165         struct xfrm_type_map    *type_map;
166         struct dst_ops          *dst_ops;
167         void                    (*garbage_collect)(void);
168         int                     (*dst_lookup)(struct xfrm_dst **dst, struct flowi *fl);
169         struct dst_entry        *(*find_bundle)(struct flowi *fl, struct rtable *rt, struct xfrm_policy *policy);
170         int                     (*bundle_create)(struct xfrm_policy *policy, 
171                                                  struct xfrm_state **xfrm, 
172                                                  int nx,
173                                                  struct flowi *fl, 
174                                                  struct dst_entry **dst_p);
175         void                    (*decode_session)(struct sk_buff *skb,
176                                                   struct flowi *fl);
177 };
178 
179 extern int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo);
180 extern int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo);
181 extern struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
182 extern void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
183 
184 #define XFRM_ACQ_EXPIRES        30
185 
186 struct xfrm_tmpl;
187 struct xfrm_state_afinfo {
188         unsigned short          family;
189         rwlock_t                lock;
190         struct list_head        *state_bydst;
191         struct list_head        *state_byspi;
192         void                    (*init_tempsel)(struct xfrm_state *x, struct flowi *fl,
193                                                 struct xfrm_tmpl *tmpl,
194                                                 xfrm_address_t *daddr, xfrm_address_t *saddr);
195         struct xfrm_state       *(*state_lookup)(xfrm_address_t *daddr, u32 spi, u8 proto);
196         struct xfrm_state       *(*find_acq)(u8 mode, u32 reqid, u8 proto, 
197                                              xfrm_address_t *daddr, xfrm_address_t *saddr, 
198                                              int create);
199 };
200 
201 extern int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo);
202 extern int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo);
203 extern struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned short family);
204 extern void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo);
205 
206 extern void xfrm_state_delete_tunnel(struct xfrm_state *x);
207 
208 struct xfrm_decap_state;
209 struct xfrm_type
210 {
211         char                    *description;
212         struct module           *owner;
213         __u8                    proto;
214 
215         int                     (*init_state)(struct xfrm_state *x, void *args);
216         void                    (*destructor)(struct xfrm_state *);
217         int                     (*input)(struct xfrm_state *, struct xfrm_decap_state *, struct sk_buff *skb);
218         int                     (*post_input)(struct xfrm_state *, struct xfrm_decap_state *, struct sk_buff *skb);
219         int                     (*output)(struct sk_buff *skb);
220         /* Estimate maximal size of result of transformation of a dgram */
221         u32                     (*get_max_size)(struct xfrm_state *, int size);
222 };
223 
224 struct xfrm_type_map {
225         rwlock_t                lock;
226         struct xfrm_type        *map[256];
227 };
228 
229 extern int xfrm_register_type(struct xfrm_type *type, unsigned short family);
230 extern int xfrm_unregister_type(struct xfrm_type *type, unsigned short family);
231 extern struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family);
232 extern void xfrm_put_type(struct xfrm_type *type);
233 
234 struct xfrm_tmpl
235 {
236 /* id in template is interpreted as:
237  * daddr - destination of tunnel, may be zero for transport mode.
238  * spi   - zero to acquire spi. Not zero if spi is static, then
239  *         daddr must be fixed too.
240  * proto - AH/ESP/IPCOMP
241  */
242         struct xfrm_id          id;
243 
244 /* Source address of tunnel. Ignored, if it is not a tunnel. */
245         xfrm_address_t          saddr;
246 
247         __u32                   reqid;
248 
249 /* Mode: transport/tunnel */
250         __u8                    mode;
251 
252 /* Sharing mode: unique, this session only, this user only etc. */
253         __u8                    share;
254 
255 /* May skip this transfomration if no SA is found */
256         __u8                    optional;
257 
258 /* Bit mask of algos allowed for acquisition */
259         __u32                   aalgos;
260         __u32                   ealgos;
261         __u32                   calgos;
262 };
263 
264 #define XFRM_MAX_DEPTH          4
265 
266 struct xfrm_policy
267 {
268         struct xfrm_policy      *next;
269         struct list_head        list;
270 
271         /* This lock only affects elements except for entry. */
272         rwlock_t                lock;
273         atomic_t                refcnt;
274         struct timer_list       timer;
275 
276         u32                     priority;
277         u32                     index;
278         struct xfrm_selector    selector;
279         struct xfrm_lifetime_cfg lft;
280         struct xfrm_lifetime_cur curlft;
281         struct dst_entry       *bundles;
282         __u16                   family;
283         __u8                    action;
284         __u8                    flags;
285         __u8                    dead;
286         __u8                    xfrm_nr;
287         struct xfrm_tmpl        xfrm_vec[XFRM_MAX_DEPTH];
288 };
289 
290 #define XFRM_KM_TIMEOUT         30
291 
292 struct xfrm_mgr
293 {
294         struct list_head        list;
295         char                    *id;
296         int                     (*notify)(struct xfrm_state *x, int event);
297         int                     (*acquire)(struct xfrm_state *x, struct xfrm_tmpl *, struct xfrm_policy *xp, int dir);
298         struct xfrm_policy      *(*compile_policy)(u16 family, int opt, u8 *data, int len, int *dir);
299         int                     (*new_mapping)(struct xfrm_state *x, xfrm_address_t *ipaddr, u16 sport);
300         int                     (*notify_policy)(struct xfrm_policy *x, int dir, int event);
301 };
302 
303 extern int xfrm_register_km(struct xfrm_mgr *km);
304 extern int xfrm_unregister_km(struct xfrm_mgr *km);
305 
306 
307 #define XFRM_FLOWCACHE_HASH_SIZE        1024
308 
309 static inline u32 __flow_hash4(struct flowi *fl)
310 {
311         u32 hash = fl->fl4_src ^ fl->fl_ip_sport;
312 
313         hash = ((hash & 0xF0F0F0F0) >> 4) | ((hash & 0x0F0F0F0F) << 4);
314 
315         hash ^= fl->fl4_dst ^ fl->fl_ip_dport;
316         hash ^= (hash >> 10);
317         hash ^= (hash >> 20);
318         return hash & (XFRM_FLOWCACHE_HASH_SIZE-1);
319 }
320 
321 static inline u32 __flow_hash6(struct flowi *fl)
322 {
323         u32 hash = fl->fl6_src.s6_addr32[2] ^
324                    fl->fl6_src.s6_addr32[3] ^ 
325                    fl->fl_ip_sport;
326 
327         hash = ((hash & 0xF0F0F0F0) >> 4) | ((hash & 0x0F0F0F0F) << 4);
328 
329         hash ^= fl->fl6_dst.s6_addr32[2] ^
330                 fl->fl6_dst.s6_addr32[3] ^ 
331                 fl->fl_ip_dport;
332         hash ^= (hash >> 10);
333         hash ^= (hash >> 20);
334         return hash & (XFRM_FLOWCACHE_HASH_SIZE-1);
335 }
336 
337 static inline u32 flow_hash(struct flowi *fl, unsigned short family)
338 {
339         switch (family) {
340         case AF_INET:
341                 return __flow_hash4(fl);
342         case AF_INET6:
343                 return __flow_hash6(fl);
344         }
345         return 0;       /*XXX*/
346 }
347 
348 extern struct xfrm_policy *xfrm_policy_list[XFRM_POLICY_MAX*2];
349 
350 static inline void xfrm_pol_hold(struct xfrm_policy *policy)
351 {
352         if (likely(policy != NULL))
353                 atomic_inc(&policy->refcnt);
354 }
355 
356 extern void __xfrm_policy_destroy(struct xfrm_policy *policy);
357 
358 static inline void xfrm_pol_put(struct xfrm_policy *policy)
359 {
360         if (atomic_dec_and_test(&policy->refcnt))
361                 __xfrm_policy_destroy(policy);
362 }
363 
364 #define XFRM_DST_HSIZE          1024
365 
366 static __inline__
367 unsigned __xfrm4_dst_hash(xfrm_address_t *addr)
368 {
369         unsigned h;
370         h = ntohl(addr->a4);
371         h = (h ^ (h>>16)) % XFRM_DST_HSIZE;
372         return h;
373 }
374 
375 static __inline__
376 unsigned __xfrm6_dst_hash(xfrm_address_t *addr)
377 {
378         unsigned h;
379         h = ntohl(addr->a6[2]^addr->a6[3]);
380         h = (h ^ (h>>16)) % XFRM_DST_HSIZE;
381         return h;
382 }
383 
384 static __inline__
385 unsigned xfrm_dst_hash(xfrm_address_t *addr, unsigned short family)
386 {
387         switch (family) {
388         case AF_INET:
389                 return __xfrm4_dst_hash(addr);
390         case AF_INET6:
391                 return __xfrm6_dst_hash(addr);
392         }
393         return 0;
394 }
395 
396 static __inline__
397 unsigned __xfrm4_spi_hash(xfrm_address_t *addr, u32 spi, u8 proto)
398 {
399         unsigned h;
400         h = ntohl(addr->a4^spi^proto);
401         h = (h ^ (h>>10) ^ (h>>20)) % XFRM_DST_HSIZE;
402         return h;
403 }
404 
405 static __inline__
406 unsigned __xfrm6_spi_hash(xfrm_address_t *addr, u32 spi, u8 proto)
407 {
408         unsigned h;
409         h = ntohl(addr->a6[2]^addr->a6[3]^spi^proto);
410         h = (h ^ (h>>10) ^ (h>>20)) % XFRM_DST_HSIZE;
411         return h;
412 }
413 
414 static __inline__
415 unsigned xfrm_spi_hash(xfrm_address_t *addr, u32 spi, u8 proto, unsigned short family)
416 {
417         switch (family) {
418         case AF_INET:
419                 return __xfrm4_spi_hash(addr, spi, proto);
420         case AF_INET6:
421                 return __xfrm6_spi_hash(addr, spi, proto);
422         }
423         return 0;       /*XXX*/
424 }
425 
426 extern void __xfrm_state_destroy(struct xfrm_state *);
427 
428 static inline void xfrm_state_put(struct xfrm_state *x)
429 {
430         if (atomic_dec_and_test(&x->refcnt))
431                 __xfrm_state_destroy(x);
432 }
433 
434 static inline void xfrm_state_hold(struct xfrm_state *x)
435 {
436         atomic_inc(&x->refcnt);
437 }
438 
439 static __inline__ int addr_match(void *token1, void *token2, int prefixlen)
440 {
441         __u32 *a1 = token1;
442         __u32 *a2 = token2;
443         int pdw;
444         int pbi;
445 
446         pdw = prefixlen >> 5;     /* num of whole __u32 in prefix */
447         pbi = prefixlen &  0x1f;  /* num of bits in incomplete u32 in prefix */
448 
449         if (pdw)
450                 if (memcmp(a1, a2, pdw << 2))
451                         return 0;
452 
453         if (pbi) {
454                 __u32 mask;
455 
456                 mask = htonl((0xffffffff) << (32 - pbi));
457 
458                 if ((a1[pdw] ^ a2[pdw]) & mask)
459                         return 0;
460         }
461 
462         return 1;
463 }
464 
465 static inline int
466 __xfrm4_selector_match(struct xfrm_selector *sel, struct flowi *fl)
467 {
468         return  addr_match(&fl->fl4_dst, &sel->daddr, sel->prefixlen_d) &&
469                 addr_match(&fl->fl4_src, &sel->saddr, sel->prefixlen_s) &&
470                 !((fl->fl_ip_dport^sel->dport)&sel->dport_mask) &&
471                 !((fl->fl_ip_sport^sel->sport)&sel->sport_mask) &&
472                 (fl->proto == sel->proto || !sel->proto) &&
473                 (fl->oif == sel->ifindex || !sel->ifindex);
474 }
475 
476 static inline int
477 __xfrm6_selector_match(struct xfrm_selector *sel, struct flowi *fl)
478 {
479         return  addr_match(&fl->fl6_dst, &sel->daddr, sel->prefixlen_d) &&
480                 addr_match(&fl->fl6_src, &sel->saddr, sel->prefixlen_s) &&
481                 !((fl->fl_ip_dport^sel->dport)&sel->dport_mask) &&
482                 !((fl->fl_ip_sport^sel->sport)&sel->sport_mask) &&
483                 (fl->proto == sel->proto || !sel->proto) &&
484                 (fl->oif == sel->ifindex || !sel->ifindex);
485 }
486 
487 static inline int
488 xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl,
489                     unsigned short family)
490 {
491         switch (family) {
492         case AF_INET:
493                 return __xfrm4_selector_match(sel, fl);
494         case AF_INET6:
495                 return __xfrm6_selector_match(sel, fl);
496         }
497         return 0;
498 }
499 
500 /* placeholder until xfrm6_tunnel.c is written */
501 static inline int xfrm6_tunnel_check_size(struct sk_buff *skb)
502 { return 0; }
503 
504 /* A struct encoding bundle of transformations to apply to some set of flow.
505  *
506  * dst->child points to the next element of bundle.
507  * dst->xfrm  points to an instanse of transformer.
508  *
509  * Due to unfortunate limitations of current routing cache, which we
510  * have no time to fix, it mirrors struct rtable and bound to the same
511  * routing key, including saddr,daddr. However, we can have many of
512  * bundles differing by session id. All the bundles grow from a parent
513  * policy rule.
514  */
515 struct xfrm_dst
516 {
517         union {
518                 struct xfrm_dst         *next;
519                 struct dst_entry        dst;
520                 struct rtable           rt;
521                 struct rt6_info         rt6;
522         } u;
523 };
524 
525 /* Decapsulation state, used by the input to store data during
526  * decapsulation procedure, to be used later (during the policy
527  * check
528  */
529 struct xfrm_decap_state {
530         char    decap_data[20];
531         __u16   decap_type;
532 };   
533 
534 struct sec_decap_state {
535         struct xfrm_state       *xvec;
536         struct xfrm_decap_state decap;
537 };
538 
539 struct sec_path
540 {
541         atomic_t                refcnt;
542         int                     len;
543         struct sec_decap_state  x[XFRM_MAX_DEPTH];
544 };
545 
546 static inline struct sec_path *
547 secpath_get(struct sec_path *sp)
548 {
549         if (sp)
550                 atomic_inc(&sp->refcnt);
551         return sp;
552 }
553 
554 extern void __secpath_destroy(struct sec_path *sp);
555 
556 static inline void
557 secpath_put(struct sec_path *sp)
558 {
559         if (sp && atomic_dec_and_test(&sp->refcnt))
560                 __secpath_destroy(sp);
561 }
562 
563 extern struct sec_path *secpath_dup(struct sec_path *src);
564 
565 static inline void
566 secpath_reset(struct sk_buff *skb)
567 {
568 #ifdef CONFIG_XFRM
569         secpath_put(skb->sp);
570         skb->sp = NULL;
571 #endif
572 }
573 
574 static inline int
575 __xfrm4_state_addr_cmp(struct xfrm_tmpl *tmpl, struct xfrm_state *x)
576 {
577         return  (tmpl->saddr.a4 &&
578                  tmpl->saddr.a4 != x->props.saddr.a4);
579 }
580 
581 static inline int
582 __xfrm6_state_addr_cmp(struct xfrm_tmpl *tmpl, struct xfrm_state *x)
583 {
584         return  (!ipv6_addr_any((struct in6_addr*)&tmpl->saddr) &&
585                  ipv6_addr_cmp((struct in6_addr *)&tmpl->saddr, (struct in6_addr*)&x->props.saddr));
586 }
587 
588 static inline int
589 xfrm_state_addr_cmp(struct xfrm_tmpl *tmpl, struct xfrm_state *x, unsigned short family)
590 {
591         switch (family) {
592         case AF_INET:
593                 return __xfrm4_state_addr_cmp(tmpl, x);
594         case AF_INET6:
595                 return __xfrm6_state_addr_cmp(tmpl, x);
596         }
597         return !0;
598 }
599 
600 #ifdef CONFIG_XFRM
601 
602 extern int __xfrm_policy_check(struct sock *, int dir, struct sk_buff *skb, unsigned short family);
603 
604 static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family)
605 {
606         if (sk && sk->sk_policy[XFRM_POLICY_IN])
607                 return __xfrm_policy_check(sk, dir, skb, family);
608                 
609         return  !xfrm_policy_list[dir] ||
610                 (skb->dst->flags & DST_NOPOLICY) ||
611                 __xfrm_policy_check(sk, dir, skb, family);
612 }
613 
614 static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
615 {
616         return xfrm_policy_check(sk, dir, skb, AF_INET);
617 }
618 
619 static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
620 {
621         return xfrm_policy_check(sk, dir, skb, AF_INET6);
622 }
623 
624 
625 extern int __xfrm_route_forward(struct sk_buff *skb, unsigned short family);
626 
627 static inline int xfrm_route_forward(struct sk_buff *skb, unsigned short family)
628 {
629         return  !xfrm_policy_list[XFRM_POLICY_OUT] ||
630                 (skb->dst->flags & DST_NOXFRM) ||
631                 __xfrm_route_forward(skb, family);
632 }
633 
634 static inline int xfrm4_route_forward(struct sk_buff *skb)
635 {
636         return xfrm_route_forward(skb, AF_INET);
637 }
638 
639 static inline int xfrm6_route_forward(struct sk_buff *skb)
640 {
641         return xfrm_route_forward(skb, AF_INET6);
642 }
643 
644 extern int __xfrm_sk_clone_policy(struct sock *sk);
645 
646 static inline int xfrm_sk_clone_policy(struct sock *sk)
647 {
648         if (unlikely(sk->sk_policy[0] || sk->sk_policy[1]))
649                 return __xfrm_sk_clone_policy(sk);
650         return 0;
651 }
652 
653 extern void xfrm_policy_delete(struct xfrm_policy *pol, int dir);
654 
655 static inline void xfrm_sk_free_policy(struct sock *sk)
656 {
657         if (unlikely(sk->sk_policy[0] != NULL)) {
658                 xfrm_policy_delete(sk->sk_policy[0], XFRM_POLICY_MAX);
659                 sk->sk_policy[0] = NULL;
660         }
661         if (unlikely(sk->sk_policy[1] != NULL)) {
662                 xfrm_policy_delete(sk->sk_policy[1], XFRM_POLICY_MAX+1);
663                 sk->sk_policy[1] = NULL;
664         }
665 }
666 
667 #else
668 
669 static inline void xfrm_sk_free_policy(struct sock *sk) {}
670 static inline int xfrm_sk_clone_policy(struct sock *sk) { return 0; }
671 static inline int xfrm6_route_forward(struct sk_buff *skb) { return 1; }  
672 static inline int xfrm4_route_forward(struct sk_buff *skb) { return 1; } 
673 static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
674 { 
675         return 1; 
676 } 
677 static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
678 {
679         return 1;
680 }
681 static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family)
682 {
683         return 1;
684 }
685 #endif
686 
687 static __inline__
688 xfrm_address_t *xfrm_flowi_daddr(struct flowi *fl, unsigned short family)
689 {
690         switch (family){
691         case AF_INET:
692                 return (xfrm_address_t *)&fl->fl4_dst;
693         case AF_INET6:
694                 return (xfrm_address_t *)&fl->fl6_dst;
695         }
696         return NULL;
697 }
698 
699 static __inline__
700 xfrm_address_t *xfrm_flowi_saddr(struct flowi *fl, unsigned short family)
701 {
702         switch (family){
703         case AF_INET:
704                 return (xfrm_address_t *)&fl->fl4_src;
705         case AF_INET6:
706                 return (xfrm_address_t *)&fl->fl6_src;
707         }
708         return NULL;
709 }
710 
711 static __inline__ int
712 __xfrm4_state_addr_check(struct xfrm_state *x,
713                          xfrm_address_t *daddr, xfrm_address_t *saddr)
714 {
715         if (daddr->a4 == x->id.daddr.a4 &&
716             (saddr->a4 == x->props.saddr.a4 || !saddr->a4 || !x->props.saddr.a4))
717                 return 1;
718         return 0;
719 }
720 
721 static __inline__ int
722 __xfrm6_state_addr_check(struct xfrm_state *x,
723                          xfrm_address_t *daddr, xfrm_address_t *saddr)
724 {
725         if (!ipv6_addr_cmp((struct in6_addr *)daddr, (struct in6_addr *)&x->id.daddr) &&
726             (!ipv6_addr_cmp((struct in6_addr *)saddr, (struct in6_addr *)&x->props.saddr)|| 
727              ipv6_addr_any((struct in6_addr *)saddr) || 
728              ipv6_addr_any((struct in6_addr *)&x->props.saddr)))
729                 return 1;
730         return 0;
731 }
732 
733 static __inline__ int
734 xfrm_state_addr_check(struct xfrm_state *x,
735                       xfrm_address_t *daddr, xfrm_address_t *saddr,
736                       unsigned short family)
737 {
738         switch (family) {
739         case AF_INET:
740                 return __xfrm4_state_addr_check(x, daddr, saddr);
741         case AF_INET6:
742                 return __xfrm6_state_addr_check(x, daddr, saddr);
743         }
744         return 0;
745 }
746 
747 static inline int xfrm_state_kern(struct xfrm_state *x)
748 {
749         return atomic_read(&x->tunnel_users);
750 }
751 
752 /*
753  * xfrm algorithm information
754  */
755 struct xfrm_algo_auth_info {
756         u16 icv_truncbits;
757         u16 icv_fullbits;
758 };
759 
760 struct xfrm_algo_encr_info {
761         u16 blockbits;
762         u16 defkeybits;
763 };
764 
765 struct xfrm_algo_comp_info {
766         u16 threshold;
767 };
768 
769 struct xfrm_algo_desc {
770         char *name;
771         u8 available:1;
772         union {
773                 struct xfrm_algo_auth_info auth;
774                 struct xfrm_algo_encr_info encr;
775                 struct xfrm_algo_comp_info comp;
776         } uinfo;
777         struct sadb_alg desc;
778 };
779 
780 /* XFRM tunnel handlers.  */
781 struct xfrm_tunnel {
782         int (*handler)(struct sk_buff *skb);
783         void (*err_handler)(struct sk_buff *skb, void *info);
784 };
785 
786 extern void xfrm_init(void);
787 extern void xfrm4_init(void);
788 extern void xfrm4_fini(void);
789 extern void xfrm6_init(void);
790 extern void xfrm6_fini(void);
791 extern void xfrm_state_init(void);
792 extern void xfrm4_state_init(void);
793 extern void xfrm4_state_fini(void);
794 extern void xfrm6_state_init(void);
795 extern void xfrm6_state_fini(void);
796 
797 extern int xfrm_state_walk(u8 proto, int (*func)(struct xfrm_state *, int, void*), void *);
798 extern struct xfrm_state *xfrm_state_alloc(void);
799 extern struct xfrm_state *xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr, 
800                                           struct flowi *fl, struct xfrm_tmpl *tmpl,
801                                           struct xfrm_policy *pol, int *err,
802                                           unsigned short family);
803 extern int xfrm_state_check_expire(struct xfrm_state *x);
804 extern void xfrm_state_insert(struct xfrm_state *x);
805 extern int xfrm_state_add(struct xfrm_state *x);
806 extern int xfrm_state_update(struct xfrm_state *x);
807 extern int xfrm_state_check_space(struct xfrm_state *x, struct sk_buff *skb);
808 extern struct xfrm_state *xfrm_state_lookup(xfrm_address_t *daddr, u32 spi, u8 proto, unsigned short family);
809 extern struct xfrm_state *xfrm_find_acq_byseq(u32 seq);
810 extern void xfrm_state_delete(struct xfrm_state *x);
811 extern void xfrm_state_flush(u8 proto);
812 extern int xfrm_replay_check(struct xfrm_state *x, u32 seq);
813 extern void xfrm_replay_advance(struct xfrm_state *x, u32 seq);
814 extern int xfrm_check_selectors(struct xfrm_state **x, int n, struct flowi *fl);
815 extern int xfrm_check_output(struct xfrm_state *x, struct sk_buff *skb, unsigned short family);
816 extern int xfrm4_rcv(struct sk_buff *skb);
817 extern int xfrm4_tunnel_register(struct xfrm_tunnel *handler);
818 extern int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler);
819 extern int xfrm4_tunnel_check_size(struct sk_buff *skb);
820 extern int xfrm6_rcv(struct sk_buff **pskb, unsigned int *nhoffp);
821 
822 #ifdef CONFIG_XFRM
823 extern int xfrm4_rcv_encap(struct sk_buff *skb, __u16 encap_type);
824 extern int xfrm_user_policy(struct sock *sk, int optname, u8 *optval, int optlen);
825 extern int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl, unsigned short family);
826 #else
827 static inline int xfrm_user_policy(struct sock *sk, int optname, u8 *optval, int optlen)
828 {
829         return -ENOPROTOOPT;
830 } 
831 
832 static inline int xfrm4_rcv_encap(struct sk_buff *skb, __u16 encap_type)
833 {
834         /* should not happen */
835         kfree_skb(skb);
836         return 0;
837 }
838 static inline int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl, unsigned short family)
839 {
840         return -EINVAL;
841 } 
842 #endif
843 
844 void xfrm_policy_init(void);
845 void xfrm4_policy_init(void);
846 void xfrm6_policy_init(void);
847 struct xfrm_policy *xfrm_policy_alloc(int gfp);
848 extern int xfrm_policy_walk(int (*func)(struct xfrm_policy *, int, int, void*), void *);
849 int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl);
850 struct xfrm_policy *xfrm_policy_bysel(int dir, struct xfrm_selector *sel,
851                                       int delete);
852 struct xfrm_policy *xfrm_policy_byid(int dir, u32 id, int delete);
853 void xfrm_policy_flush(void);
854 u32 xfrm_get_acqseq(void);
855 void xfrm_alloc_spi(struct xfrm_state *x, u32 minspi, u32 maxspi);
856 struct xfrm_state * xfrm_find_acq(u8 mode, u32 reqid, u8 proto, 
857                                   xfrm_address_t *daddr, xfrm_address_t *saddr, 
858                                   int create, unsigned short family);
859 extern void xfrm_policy_flush(void);
860 extern void xfrm_policy_kill(struct xfrm_policy *);
861 extern int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol);
862 extern struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl);
863 extern int xfrm_flush_bundles(struct xfrm_state *x);
864 
865 extern wait_queue_head_t km_waitq;
866 extern void km_state_expired(struct xfrm_state *x, int hard);
867 extern int km_query(struct xfrm_state *x, struct xfrm_tmpl *, struct xfrm_policy *pol);
868 extern int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, u16 sport);
869 extern void km_policy_expired(struct xfrm_policy *pol, int dir, int hard);
870 
871 extern void xfrm_input_init(void);
872 extern int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, u32 *spi, u32 *seq);
873 
874 extern void xfrm_probe_algs(void);
875 extern int xfrm_count_auth_supported(void);
876 extern int xfrm_count_enc_supported(void);
877 extern struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx);
878 extern struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx);
879 extern struct xfrm_algo_desc *xfrm_calg_get_byidx(unsigned int idx);
880 extern struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id);
881 extern struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id);
882 extern struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id);
883 extern struct xfrm_algo_desc *xfrm_aalg_get_byname(char *name);
884 extern struct xfrm_algo_desc *xfrm_ealg_get_byname(char *name);
885 extern struct xfrm_algo_desc *xfrm_calg_get_byname(char *name);
886 
887 struct crypto_tfm;
888 typedef void (icv_update_fn_t)(struct crypto_tfm *, struct scatterlist *, unsigned int);
889 
890 extern void skb_icv_walk(const struct sk_buff *skb, struct crypto_tfm *tfm,
891                          int offset, int len, icv_update_fn_t icv_update);
892 
893 #endif  /* _NET_XFRM_H */
894 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp