rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | #ifndef __UTILS_XFRM_H__ |
| 2 | #define __UTILS_XFRM_H__ 1 |
| 3 | #include <string.h> |
| 4 | #include <asm/types.h> |
| 5 | #include <linux/netlink.h> |
| 6 | #include <linux/rtnetlink.h> |
| 7 | #include <linux/if_link.h> |
| 8 | #include <linux/if_addr.h> |
| 9 | #include <linux/neighbour.h> |
| 10 | #include <cutils/properties.h> |
| 11 | #include <asm/types.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <arpa/inet.h> |
| 14 | #include <linux/xfrm.h> |
| 15 | |
| 16 | |
| 17 | //#define NLMSG_DELETEALL_BUF_SIZE (4096-512) |
| 18 | #define NLMSG_DELETEALL_BUF_SIZE 8192 |
| 19 | |
| 20 | /* |
| 21 | * Receiving buffer defines: |
| 22 | * nlmsg |
| 23 | * data = struct xfrm_userpolicy_info |
| 24 | * rtattr |
| 25 | * data = struct xfrm_user_tmpl[] |
| 26 | */ |
| 27 | #define NLMSG_BUF_SIZE 4096 |
| 28 | #define RTA_BUF_SIZE 2048 |
| 29 | #define XFRM_TMPLS_BUF_SIZE 1024 |
| 30 | #define CTX_BUF_SIZE 256 |
| 31 | #define XFRM_ALGO_KEY_BUF_SIZE 512 |
| 32 | |
| 33 | #undef NLMSG_TAIL |
| 34 | #define NLMSG_TAIL(nmsg) \ |
| 35 | ((struct rtattr *) (((char *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len))) |
| 36 | |
| 37 | #define XFRM_POLICY_ERROR 255 |
| 38 | enum PROTOCOL_TYPE{ |
| 39 | PROTOCOL_ICMP=1, |
| 40 | PROTOCOL_IPV4=4, |
| 41 | PROTOCOL_TCP=6, |
| 42 | PROTOCOL_UDP=17, |
| 43 | PROTOCOL_IPV6=41, |
| 44 | PROTOCOL_GRE=47, |
| 45 | PROTOCOL_ESP=50, |
| 46 | PROTOCOL_AH=51, |
| 47 | PROTOCOL_ICMPV6=58, |
| 48 | PROTOCOL_IPCOMP=108, |
| 49 | PROTOCOL_L2TP=115 |
| 50 | }; |
| 51 | |
| 52 | struct rtnl_handle_xfrm |
| 53 | { |
| 54 | int fd; |
| 55 | struct sockaddr_nl local; |
| 56 | struct sockaddr_nl peer; |
| 57 | __u32 seq; |
| 58 | __u32 dump; |
| 59 | }; |
| 60 | |
| 61 | struct req_handle_xfrm { |
| 62 | struct nlmsghdr n; |
| 63 | struct xfrm_userpolicy_info xpinfo; |
| 64 | char buf[RTA_BUF_SIZE]; |
| 65 | } ; |
| 66 | |
| 67 | typedef struct |
| 68 | { |
| 69 | __u8 family; |
| 70 | __u8 bytelen; |
| 71 | __s16 bitlen; |
| 72 | __u32 flags; |
| 73 | __u32 data[8]; |
| 74 | } inet_prefix; |
| 75 | |
| 76 | #define PREFIXLEN_SPECIFIED 1 |
| 77 | |
| 78 | static inline __u32 nl_mgrp_xfrm(__u32 group) |
| 79 | { |
| 80 | if (group > 31 ) { |
| 81 | //ALOGD("Use setsockopt for this group %d\n", group); |
| 82 | return -1; |
| 83 | } |
| 84 | return group ? (1 << (group - 1)) : 0; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | extern int mask2bits(__u32 netmask_xfrm); |
| 89 | extern int get_netmask(unsigned *val_xfrm, const char *arg_xfrm, int base_xfrm); |
| 90 | extern int get_unsigned(unsigned *val_xfrm, const char *arg_xfrm, int base_xfrm); |
| 91 | extern int get_u32(__u32 *val_xfrm, const char *arg_xfrm, int base_xfrm); |
| 92 | extern int get_u8(__u8 *val_xfrm, const char *arg_xfrm, int base_xfrm); |
| 93 | extern int get_addr_ipv4(__u8 *ap_xfrm, const char *cp_xfrm); |
| 94 | extern int get_addr_1(inet_prefix *addr_xfrm, const char *name_xfrm, int family); |
| 95 | extern int get_prefix(inet_prefix *dst_xfrm, char *arg_xfrm, int family); |
| 96 | |
| 97 | |
| 98 | |
| 99 | typedef int (*rtnl_filter_t_xfrm)(struct rtnl_handle_xfrm * rth,struct nlmsghdr *n); |
| 100 | |
| 101 | extern int xfrm_policy_process_delete_exist( struct rtnl_handle_xfrm * rth,struct nlmsghdr *n, pid_t volte_pid); |
| 102 | extern int xfrm_state_process_delete_exist( struct rtnl_handle_xfrm * rth,struct nlmsghdr *n, pid_t volte_pid); |
| 103 | |
| 104 | extern void rtnl_close_xfrm(struct rtnl_handle_xfrm *rth); |
| 105 | extern int rtnl_open_byproto_xfrm(struct rtnl_handle_xfrm *rth, unsigned subscriptions, |
| 106 | int protocol); |
| 107 | extern int rtnl_listen_xfrm(struct rtnl_handle_xfrm *rtnl, rtnl_filter_t_xfrm handler); |
| 108 | extern int rtnl_accept_msg_xfrm(struct rtnl_handle_xfrm * rth,struct nlmsghdr *n); |
| 109 | #endif /* __UTILS_XFRM_H__ */ |