rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <ctype.h> |
| 5 | #include <unistd.h> |
| 6 | #include <errno.h> |
| 7 | #include <netdb.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <dirent.h> |
| 10 | #include <time.h> |
| 11 | #include "utils_xfrm.h" |
| 12 | #include "setkey_xfrm_parse.h" |
| 13 | #define LOG_TAG "setkey" |
| 14 | #include <log/log.h> |
| 15 | #include <cutils/log.h> |
| 16 | |
| 17 | |
| 18 | void rtnl_close_xfrm(struct rtnl_handle_xfrm *rth_xfrm) |
| 19 | { |
| 20 | if (rth_xfrm->fd >= 0) { |
| 21 | close(rth_xfrm->fd); |
| 22 | rth_xfrm->fd = -1; |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | int rtnl_open_byproto_xfrm(struct rtnl_handle_xfrm *rth_xfrm, unsigned subscriptions, |
| 27 | int protocol) |
| 28 | { |
| 29 | //socklen_t addr_len; |
| 30 | //int sndbuf = 32768; |
| 31 | |
| 32 | memset(rth_xfrm, 0, sizeof(*rth_xfrm)); |
| 33 | |
| 34 | rth_xfrm->fd = socket(AF_NETLINK, SOCK_RAW, protocol); |
| 35 | if (rth_xfrm->fd < 0) { |
| 36 | ALOGD("Cannot open netlink socket,errno:%d\n",errno); |
| 37 | return -1; |
| 38 | } |
| 39 | |
| 40 | |
| 41 | memset(&rth_xfrm->local, 0, sizeof(rth_xfrm->local)); |
| 42 | rth_xfrm->local.nl_family = AF_NETLINK; |
| 43 | rth_xfrm->local.nl_groups = subscriptions; |
| 44 | |
| 45 | if (bind(rth_xfrm->fd, (struct sockaddr*)&rth_xfrm->local, sizeof(rth_xfrm->local)) < 0) { |
| 46 | ALOGD("Cannot bind netlink socket\n"); |
| 47 | return -1; |
| 48 | } |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | int rtnl_listen_xfrm(struct rtnl_handle_xfrm *rtnl_xfrm, rtnl_filter_t_xfrm handler) |
| 53 | { |
| 54 | int status; |
| 55 | struct nlmsghdr *h_xfrm; |
| 56 | struct sockaddr_nl nladdr_xfrm; |
| 57 | struct iovec iov; |
| 58 | struct msghdr msg_xfrm = { |
| 59 | .msg_name = &nladdr_xfrm, |
| 60 | .msg_namelen = sizeof(nladdr_xfrm), |
| 61 | .msg_iov = &iov, |
| 62 | .msg_iovlen = 1, |
| 63 | }; |
| 64 | char buf[16384]; |
| 65 | |
| 66 | memset(&nladdr_xfrm, 0, sizeof(nladdr_xfrm)); |
| 67 | nladdr_xfrm.nl_family = AF_NETLINK; |
| 68 | nladdr_xfrm.nl_pid = 0; |
| 69 | nladdr_xfrm.nl_groups = 0; |
| 70 | iov.iov_base = buf; |
| 71 | |
| 72 | iov.iov_len = sizeof(buf); |
| 73 | status = recvmsg(rtnl_xfrm->fd, &msg_xfrm, 0); |
| 74 | ALOGD("netlink receive msg status:%d\n",status); |
| 75 | if (status < 0) { |
| 76 | if (errno == EINTR || errno == EAGAIN || errno == ENOBUFS) |
| 77 | return -1; |
| 78 | } |
| 79 | if (status == 0) { |
| 80 | ALOGE("EOF on netlink\n"); |
| 81 | return -1; |
| 82 | } |
| 83 | if (msg_xfrm.msg_namelen != sizeof(nladdr_xfrm)) { |
| 84 | ALOGE("Sender address length == %d\n", msg_xfrm.msg_namelen); |
| 85 | return -1; |
| 86 | } |
| 87 | for (h_xfrm = (struct nlmsghdr*)buf; status >= sizeof(*h_xfrm); ) { |
| 88 | int err; |
| 89 | int len = h_xfrm->nlmsg_len; |
| 90 | int l = len - sizeof(*h_xfrm); |
| 91 | |
| 92 | if (l<0 || len>status) { |
| 93 | if (msg_xfrm.msg_flags & MSG_TRUNC) { |
| 94 | ALOGE("Truncated message\n"); |
| 95 | return -1; |
| 96 | } |
| 97 | ALOGE("!!!malformed message: len=%d\n", len); |
| 98 | return -1; |
| 99 | } |
| 100 | err = handler(rtnl_xfrm,h_xfrm); |
| 101 | if (err == -2) /*no sa & sp entries*/ |
| 102 | { |
| 103 | return err; |
| 104 | } |
| 105 | |
| 106 | status -= NLMSG_ALIGN(len); |
| 107 | h_xfrm = (struct nlmsghdr*)((char*)h_xfrm + NLMSG_ALIGN(len)); |
| 108 | } |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | -2: no previous volte_stack policy&& state is set; |
| 115 | -1: unknown message type && delete policy or state failed |
| 116 | 0: everything is good |
| 117 | */ |
| 118 | int rtnl_accept_msg_xfrm(struct rtnl_handle_xfrm * rth ,struct nlmsghdr *n) |
| 119 | { |
| 120 | char pid[128] = {0}; |
| 121 | property_get("net.ims.volte.pid",pid,"-1"); |
| 122 | pid_t volte_pid =atoi(pid); |
| 123 | /*no previous volte_stack policy&& state is set*/ |
| 124 | switch (n->nlmsg_type) { |
| 125 | case XFRM_MSG_NEWSA: |
| 126 | case XFRM_MSG_UPDSA: |
| 127 | return xfrm_state_process_delete_exist( rth,n, volte_pid); |
| 128 | case XFRM_MSG_NEWPOLICY: |
| 129 | case XFRM_MSG_UPDPOLICY: |
| 130 | return xfrm_policy_process_delete_exist( rth,n, volte_pid); |
| 131 | case XFRM_MSG_EXPIRE: |
| 132 | case XFRM_MSG_DELSA: |
| 133 | case XFRM_MSG_FLUSHSA: |
| 134 | case XFRM_MSG_GETPOLICY: |
| 135 | case XFRM_MSG_FLUSHPOLICY: |
| 136 | case XFRM_MSG_ACQUIRE: |
| 137 | case XFRM_MSG_DELPOLICY: |
| 138 | case XFRM_MSG_POLEXPIRE: |
| 139 | default: |
| 140 | ALOGD("receive netlink message: %08d 0x%08x 0x%08x\n", |
| 141 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); |
| 142 | break; |
| 143 | } |
| 144 | |
| 145 | if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP && |
| 146 | n->nlmsg_type != NLMSG_DONE) { |
| 147 | ALOGE("Unknown message: %08d 0x%08x 0x%08x\n", |
| 148 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); |
| 149 | } |
| 150 | return -2; |
| 151 | } |
| 152 | |