1 /* $USAGI: $ */ 2 3 /* 4 * Copyright (C)2004 USAGI/WIDE Project 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, see <http://www.gnu.org/licenses>. 18 */ 19 /* 20 * Authors: 21 * Masahide NAKAMURA @USAGI 22 */ 23 24 #ifndef __XFRM_H__ 25 #define __XFRM_H__ 1 26 27 #include <stdio.h> 28 #include <sys/socket.h> 29 #include <linux/in.h> 30 #include <linux/xfrm.h> 31 #include <linux/ipsec.h> 32 33 #ifdef __i386__ 34 /* b/138147164 */ 35 /* Adapted from checks in system/netd/server/XfrmController.h */ 36 _Static_assert(sizeof(struct xfrm_usersa_info) - offsetof(struct xfrm_usersa_info, flags) == 8, 37 "struct xfrm_usersa_info probably misaligned with kernel struct."); 38 _Static_assert(sizeof(struct xfrm_usersa_info) % 8 == 0, 39 "struct xfrm_usersa_info is not 64-bit aligned."); 40 _Static_assert(sizeof(struct xfrm_userpolicy_info) - offsetof(struct xfrm_userpolicy_info, share) == 5, 41 "struct xfrm_userpolicy_info probably misaligned with kernel struct."); 42 _Static_assert(sizeof(struct xfrm_userpolicy_info) % 8 == 0, 43 "struct xfrm_userpolicy_info is not 64-bit aligned."); 44 #endif /* __i386__ */ 45 46 #ifndef IPPROTO_MH 47 #define IPPROTO_MH 135 48 #endif 49 50 #define XFRMS_RTA(x) ((struct rtattr*)(((char*)(x)) + NLMSG_ALIGN(sizeof(struct xfrm_usersa_info)))) 51 #define XFRMS_PAYLOAD(n) NLMSG_PAYLOAD(n,sizeof(struct xfrm_usersa_info)) 52 53 #define XFRMP_RTA(x) ((struct rtattr*)(((char*)(x)) + NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_info)))) 54 #define XFRMP_PAYLOAD(n) NLMSG_PAYLOAD(n,sizeof(struct xfrm_userpoilcy_info)) 55 56 #define XFRMSID_RTA(x) ((struct rtattr*)(((char*)(x)) + NLMSG_ALIGN(sizeof(struct xfrm_usersa_id)))) 57 #define XFRMSID_PAYLOAD(n) NLMSG_PAYLOAD(n,sizeof(struct xfrm_usersa_id)) 58 59 #define XFRMPID_RTA(x) ((struct rtattr*)(((char*)(x)) + NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id)))) 60 #define XFRMPID_PAYLOAD(n) NLMSG_PAYLOAD(n,sizeof(struct xfrm_userpoilcy_id)) 61 62 #define XFRMACQ_RTA(x) ((struct rtattr*)(((char*)(x)) + NLMSG_ALIGN(sizeof(struct xfrm_user_acquire)))) 63 #define XFRMEXP_RTA(x) ((struct rtattr*)(((char*)(x)) + NLMSG_ALIGN(sizeof(struct xfrm_user_expire)))) 64 #define XFRMPEXP_RTA(x) ((struct rtattr*)(((char*)(x)) + NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire)))) 65 66 #define XFRMREP_RTA(x) ((struct rtattr*)(((char*)(x)) + NLMSG_ALIGN(sizeof(struct xfrm_user_report)))) 67 68 #define XFRMSAPD_RTA(x) ((struct rtattr*)(((char*)(x)) + NLMSG_ALIGN(sizeof(__u32)))) 69 #define XFRM_FLAG_PRINT(fp, flags, f, s) \ 70 do { \ 71 if (flags & f) { \ 72 flags &= ~f; \ 73 fprintf(fp, s "%s", (flags ? " " : "")); \ 74 } \ 75 } while(0) 76 77 struct xfrm_buffer { 78 char *buf; 79 int size; 80 int offset; 81 82 int nlmsg_count; 83 struct rtnl_handle *rth; 84 }; 85 86 struct xfrm_filter { 87 int use; 88 89 struct xfrm_usersa_info xsinfo; 90 __u8 id_src_mask; 91 __u8 id_dst_mask; 92 __u8 id_proto_mask; 93 __u32 id_spi_mask; 94 __u8 mode_mask; 95 __u32 reqid_mask; 96 __u8 state_flags_mask; 97 98 struct xfrm_userpolicy_info xpinfo; 99 __u8 dir_mask; 100 __u8 sel_src_mask; 101 __u8 sel_dst_mask; 102 __u32 sel_dev_mask; 103 __u8 upspec_proto_mask; 104 __u16 upspec_sport_mask; 105 __u16 upspec_dport_mask; 106 __u32 index_mask; 107 __u8 action_mask; 108 __u32 priority_mask; 109 __u8 policy_flags_mask; 110 __u8 filter_socket; 111 112 __u8 ptype; 113 __u8 ptype_mask; 114 115 }; 116 #define XFRM_FILTER_MASK_FULL (~0) 117 118 extern struct xfrm_filter filter; 119 120 int xfrm_state_print(const struct sockaddr_nl *who, struct nlmsghdr *n, 121 void *arg); 122 int xfrm_policy_print(const struct sockaddr_nl *who, struct nlmsghdr *n, 123 void *arg); 124 int do_xfrm_state(int argc, char **argv); 125 int do_xfrm_policy(int argc, char **argv); 126 int do_xfrm_monitor(int argc, char **argv); 127 128 int xfrm_addr_match(xfrm_address_t *x1, xfrm_address_t *x2, int bits); 129 int xfrm_xfrmproto_is_ipsec(__u8 proto); 130 int xfrm_xfrmproto_is_ro(__u8 proto); 131 int xfrm_xfrmproto_getbyname(char *name); 132 int xfrm_algotype_getbyname(char *name); 133 int xfrm_parse_mark(struct xfrm_mark *mark, int *argcp, char ***argvp); 134 const char *strxf_xfrmproto(__u8 proto); 135 const char *strxf_algotype(int type); 136 const char *strxf_mask8(__u8 mask); 137 const char *strxf_mask32(__u32 mask); 138 const char *strxf_share(__u8 share); 139 const char *strxf_proto(__u8 proto); 140 const char *strxf_ptype(__u8 ptype); 141 void xfrm_id_info_print(xfrm_address_t *saddr, struct xfrm_id *id, 142 __u8 mode, __u32 reqid, __u16 family, int force_spi, 143 FILE *fp, const char *prefix, const char *title); 144 void xfrm_stats_print(struct xfrm_stats *s, FILE *fp, const char *prefix); 145 void xfrm_lifetime_print(struct xfrm_lifetime_cfg *cfg, 146 struct xfrm_lifetime_cur *cur, 147 FILE *fp, const char *prefix); 148 void xfrm_selector_print(struct xfrm_selector *sel, __u16 family, 149 FILE *fp, const char *prefix); 150 void xfrm_xfrma_print(struct rtattr *tb[], __u16 family, 151 FILE *fp, const char *prefix); 152 void xfrm_state_info_print(struct xfrm_usersa_info *xsinfo, 153 struct rtattr *tb[], FILE *fp, const char *prefix, 154 const char *title); 155 void xfrm_policy_info_print(struct xfrm_userpolicy_info *xpinfo, 156 struct rtattr *tb[], FILE *fp, const char *prefix, 157 const char *title); 158 int xfrm_id_parse(xfrm_address_t *saddr, struct xfrm_id *id, __u16 *family, 159 int loose, int *argcp, char ***argvp); 160 int xfrm_mode_parse(__u8 *mode, int *argcp, char ***argvp); 161 int xfrm_encap_type_parse(__u16 *type, int *argcp, char ***argvp); 162 int xfrm_reqid_parse(__u32 *reqid, int *argcp, char ***argvp); 163 int xfrm_selector_parse(struct xfrm_selector *sel, int *argcp, char ***argvp); 164 int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg *lft, 165 int *argcp, char ***argvp); 166 int xfrm_sctx_parse(char *ctxstr, char *context, 167 struct xfrm_user_sec_ctx *sctx); 168 #endif 169