|
8 | 8 | #include <linux/xfrm.h> |
9 | 9 | #include <net/xfrm.h> |
10 | 10 |
|
| 11 | +struct compat_xfrm_lifetime_cfg { |
| 12 | + compat_u64 soft_byte_limit, hard_byte_limit; |
| 13 | + compat_u64 soft_packet_limit, hard_packet_limit; |
| 14 | + compat_u64 soft_add_expires_seconds, hard_add_expires_seconds; |
| 15 | + compat_u64 soft_use_expires_seconds, hard_use_expires_seconds; |
| 16 | +}; /* same size on 32bit, but only 4 byte alignment required */ |
| 17 | + |
| 18 | +struct compat_xfrm_lifetime_cur { |
| 19 | + compat_u64 bytes, packets, add_time, use_time; |
| 20 | +}; /* same size on 32bit, but only 4 byte alignment required */ |
| 21 | + |
| 22 | +struct compat_xfrm_userpolicy_info { |
| 23 | + struct xfrm_selector sel; |
| 24 | + struct compat_xfrm_lifetime_cfg lft; |
| 25 | + struct compat_xfrm_lifetime_cur curlft; |
| 26 | + __u32 priority, index; |
| 27 | + u8 dir, action, flags, share; |
| 28 | + /* 4 bytes additional padding on 64bit */ |
| 29 | +}; |
| 30 | + |
| 31 | +struct compat_xfrm_usersa_info { |
| 32 | + struct xfrm_selector sel; |
| 33 | + struct xfrm_id id; |
| 34 | + xfrm_address_t saddr; |
| 35 | + struct compat_xfrm_lifetime_cfg lft; |
| 36 | + struct compat_xfrm_lifetime_cur curlft; |
| 37 | + struct xfrm_stats stats; |
| 38 | + __u32 seq, reqid; |
| 39 | + u16 family; |
| 40 | + u8 mode, replay_window, flags; |
| 41 | + /* 4 bytes additional padding on 64bit */ |
| 42 | +}; |
| 43 | + |
| 44 | +struct compat_xfrm_user_acquire { |
| 45 | + struct xfrm_id id; |
| 46 | + xfrm_address_t saddr; |
| 47 | + struct xfrm_selector sel; |
| 48 | + struct compat_xfrm_userpolicy_info policy; |
| 49 | + /* 4 bytes additional padding on 64bit */ |
| 50 | + __u32 aalgos, ealgos, calgos, seq; |
| 51 | +}; |
| 52 | + |
| 53 | +struct compat_xfrm_userspi_info { |
| 54 | + struct compat_xfrm_usersa_info info; |
| 55 | + /* 4 bytes additional padding on 64bit */ |
| 56 | + __u32 min, max; |
| 57 | +}; |
| 58 | + |
| 59 | +struct compat_xfrm_user_expire { |
| 60 | + struct compat_xfrm_usersa_info state; |
| 61 | + /* 8 bytes additional padding on 64bit */ |
| 62 | + u8 hard; |
| 63 | +}; |
| 64 | + |
| 65 | +struct compat_xfrm_user_polexpire { |
| 66 | + struct compat_xfrm_userpolicy_info pol; |
| 67 | + /* 8 bytes additional padding on 64bit */ |
| 68 | + u8 hard; |
| 69 | +}; |
| 70 | + |
| 71 | +#define XMSGSIZE(type) sizeof(struct type) |
| 72 | + |
| 73 | +static const int compat_msg_min[XFRM_NR_MSGTYPES] = { |
| 74 | + [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(compat_xfrm_usersa_info), |
| 75 | + [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), |
| 76 | + [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), |
| 77 | + [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(compat_xfrm_userpolicy_info), |
| 78 | + [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), |
| 79 | + [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), |
| 80 | + [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(compat_xfrm_userspi_info), |
| 81 | + [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(compat_xfrm_user_acquire), |
| 82 | + [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(compat_xfrm_user_expire), |
| 83 | + [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(compat_xfrm_userpolicy_info), |
| 84 | + [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(compat_xfrm_usersa_info), |
| 85 | + [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(compat_xfrm_user_polexpire), |
| 86 | + [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush), |
| 87 | + [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0, |
| 88 | + [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), |
| 89 | + [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), |
| 90 | + [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report), |
| 91 | + [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), |
| 92 | + [XFRM_MSG_NEWSADINFO - XFRM_MSG_BASE] = sizeof(u32), |
| 93 | + [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32), |
| 94 | + [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32), |
| 95 | + [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32), |
| 96 | + [XFRM_MSG_MAPPING - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_mapping) |
| 97 | +}; |
| 98 | + |
| 99 | +static struct nlmsghdr *xfrm_nlmsg_put_compat(struct sk_buff *skb, |
| 100 | + const struct nlmsghdr *nlh_src, u16 type) |
| 101 | +{ |
| 102 | + int payload = compat_msg_min[type]; |
| 103 | + int src_len = xfrm_msg_min[type]; |
| 104 | + struct nlmsghdr *nlh_dst; |
| 105 | + |
| 106 | + /* Compat messages are shorter or equal to native (+padding) */ |
| 107 | + if (WARN_ON_ONCE(src_len < payload)) |
| 108 | + return ERR_PTR(-EMSGSIZE); |
| 109 | + |
| 110 | + nlh_dst = nlmsg_put(skb, nlh_src->nlmsg_pid, nlh_src->nlmsg_seq, |
| 111 | + nlh_src->nlmsg_type, payload, nlh_src->nlmsg_flags); |
| 112 | + if (!nlh_dst) |
| 113 | + return ERR_PTR(-EMSGSIZE); |
| 114 | + |
| 115 | + memset(nlmsg_data(nlh_dst), 0, payload); |
| 116 | + |
| 117 | + switch (nlh_src->nlmsg_type) { |
| 118 | + /* Compat message has the same layout as native */ |
| 119 | + case XFRM_MSG_DELSA: |
| 120 | + case XFRM_MSG_DELPOLICY: |
| 121 | + case XFRM_MSG_FLUSHSA: |
| 122 | + case XFRM_MSG_FLUSHPOLICY: |
| 123 | + case XFRM_MSG_NEWAE: |
| 124 | + case XFRM_MSG_REPORT: |
| 125 | + case XFRM_MSG_MIGRATE: |
| 126 | + case XFRM_MSG_NEWSADINFO: |
| 127 | + case XFRM_MSG_NEWSPDINFO: |
| 128 | + case XFRM_MSG_MAPPING: |
| 129 | + WARN_ON_ONCE(src_len != payload); |
| 130 | + memcpy(nlmsg_data(nlh_dst), nlmsg_data(nlh_src), src_len); |
| 131 | + break; |
| 132 | + /* 4 byte alignment for trailing u64 on native, but not on compat */ |
| 133 | + case XFRM_MSG_NEWSA: |
| 134 | + case XFRM_MSG_NEWPOLICY: |
| 135 | + case XFRM_MSG_UPDSA: |
| 136 | + case XFRM_MSG_UPDPOLICY: |
| 137 | + WARN_ON_ONCE(src_len != payload + 4); |
| 138 | + memcpy(nlmsg_data(nlh_dst), nlmsg_data(nlh_src), payload); |
| 139 | + break; |
| 140 | + case XFRM_MSG_EXPIRE: { |
| 141 | + const struct xfrm_user_expire *src_ue = nlmsg_data(nlh_src); |
| 142 | + struct compat_xfrm_user_expire *dst_ue = nlmsg_data(nlh_dst); |
| 143 | + |
| 144 | + /* compat_xfrm_user_expire has 4-byte smaller state */ |
| 145 | + memcpy(dst_ue, src_ue, sizeof(dst_ue->state)); |
| 146 | + dst_ue->hard = src_ue->hard; |
| 147 | + break; |
| 148 | + } |
| 149 | + case XFRM_MSG_ACQUIRE: { |
| 150 | + const struct xfrm_user_acquire *src_ua = nlmsg_data(nlh_src); |
| 151 | + struct compat_xfrm_user_acquire *dst_ua = nlmsg_data(nlh_dst); |
| 152 | + |
| 153 | + memcpy(dst_ua, src_ua, offsetof(struct compat_xfrm_user_acquire, aalgos)); |
| 154 | + dst_ua->aalgos = src_ua->aalgos; |
| 155 | + dst_ua->ealgos = src_ua->ealgos; |
| 156 | + dst_ua->calgos = src_ua->calgos; |
| 157 | + dst_ua->seq = src_ua->seq; |
| 158 | + break; |
| 159 | + } |
| 160 | + case XFRM_MSG_POLEXPIRE: { |
| 161 | + const struct xfrm_user_polexpire *src_upe = nlmsg_data(nlh_src); |
| 162 | + struct compat_xfrm_user_polexpire *dst_upe = nlmsg_data(nlh_dst); |
| 163 | + |
| 164 | + /* compat_xfrm_user_polexpire has 4-byte smaller state */ |
| 165 | + memcpy(dst_upe, src_upe, sizeof(dst_upe->pol)); |
| 166 | + dst_upe->hard = src_upe->hard; |
| 167 | + break; |
| 168 | + } |
| 169 | + case XFRM_MSG_ALLOCSPI: { |
| 170 | + const struct xfrm_userspi_info *src_usi = nlmsg_data(nlh_src); |
| 171 | + struct compat_xfrm_userspi_info *dst_usi = nlmsg_data(nlh_dst); |
| 172 | + |
| 173 | + /* compat_xfrm_user_polexpire has 4-byte smaller state */ |
| 174 | + memcpy(dst_usi, src_usi, sizeof(src_usi->info)); |
| 175 | + dst_usi->min = src_usi->min; |
| 176 | + dst_usi->max = src_usi->max; |
| 177 | + break; |
| 178 | + } |
| 179 | + /* Not being sent by kernel */ |
| 180 | + case XFRM_MSG_GETSA: |
| 181 | + case XFRM_MSG_GETPOLICY: |
| 182 | + case XFRM_MSG_GETAE: |
| 183 | + case XFRM_MSG_GETSADINFO: |
| 184 | + case XFRM_MSG_GETSPDINFO: |
| 185 | + default: |
| 186 | + WARN_ONCE(1, "unsupported nlmsg_type %d", nlh_src->nlmsg_type); |
| 187 | + return ERR_PTR(-EOPNOTSUPP); |
| 188 | + } |
| 189 | + |
| 190 | + return nlh_dst; |
| 191 | +} |
| 192 | + |
| 193 | +static int xfrm_nla_cpy(struct sk_buff *dst, const struct nlattr *src, int len) |
| 194 | +{ |
| 195 | + return nla_put(dst, src->nla_type, len, nla_data(src)); |
| 196 | +} |
| 197 | + |
| 198 | +static int xfrm_xlate64_attr(struct sk_buff *dst, const struct nlattr *src) |
| 199 | +{ |
| 200 | + switch (src->nla_type) { |
| 201 | + case XFRMA_PAD: |
| 202 | + /* Ignore */ |
| 203 | + return 0; |
| 204 | + case XFRMA_ALG_AUTH: |
| 205 | + case XFRMA_ALG_CRYPT: |
| 206 | + case XFRMA_ALG_COMP: |
| 207 | + case XFRMA_ENCAP: |
| 208 | + case XFRMA_TMPL: |
| 209 | + return xfrm_nla_cpy(dst, src, nla_len(src)); |
| 210 | + case XFRMA_SA: |
| 211 | + return xfrm_nla_cpy(dst, src, XMSGSIZE(compat_xfrm_usersa_info)); |
| 212 | + case XFRMA_POLICY: |
| 213 | + return xfrm_nla_cpy(dst, src, XMSGSIZE(compat_xfrm_userpolicy_info)); |
| 214 | + case XFRMA_SEC_CTX: |
| 215 | + return xfrm_nla_cpy(dst, src, nla_len(src)); |
| 216 | + case XFRMA_LTIME_VAL: |
| 217 | + return nla_put_64bit(dst, src->nla_type, nla_len(src), |
| 218 | + nla_data(src), XFRMA_PAD); |
| 219 | + case XFRMA_REPLAY_VAL: |
| 220 | + case XFRMA_REPLAY_THRESH: |
| 221 | + case XFRMA_ETIMER_THRESH: |
| 222 | + case XFRMA_SRCADDR: |
| 223 | + case XFRMA_COADDR: |
| 224 | + return xfrm_nla_cpy(dst, src, nla_len(src)); |
| 225 | + case XFRMA_LASTUSED: |
| 226 | + return nla_put_64bit(dst, src->nla_type, nla_len(src), |
| 227 | + nla_data(src), XFRMA_PAD); |
| 228 | + case XFRMA_POLICY_TYPE: |
| 229 | + case XFRMA_MIGRATE: |
| 230 | + case XFRMA_ALG_AEAD: |
| 231 | + case XFRMA_KMADDRESS: |
| 232 | + case XFRMA_ALG_AUTH_TRUNC: |
| 233 | + case XFRMA_MARK: |
| 234 | + case XFRMA_TFCPAD: |
| 235 | + case XFRMA_REPLAY_ESN_VAL: |
| 236 | + case XFRMA_SA_EXTRA_FLAGS: |
| 237 | + case XFRMA_PROTO: |
| 238 | + case XFRMA_ADDRESS_FILTER: |
| 239 | + case XFRMA_OFFLOAD_DEV: |
| 240 | + case XFRMA_SET_MARK: |
| 241 | + case XFRMA_SET_MARK_MASK: |
| 242 | + case XFRMA_IF_ID: |
| 243 | + return xfrm_nla_cpy(dst, src, nla_len(src)); |
| 244 | + default: |
| 245 | + BUILD_BUG_ON(XFRMA_MAX != XFRMA_IF_ID); |
| 246 | + WARN_ONCE(1, "unsupported nla_type %d", src->nla_type); |
| 247 | + return -EOPNOTSUPP; |
| 248 | + } |
| 249 | +} |
| 250 | + |
| 251 | +/* Take kernel-built (64bit layout) and create 32bit layout for userspace */ |
| 252 | +static int xfrm_xlate64(struct sk_buff *dst, const struct nlmsghdr *nlh_src) |
| 253 | +{ |
| 254 | + u16 type = nlh_src->nlmsg_type - XFRM_MSG_BASE; |
| 255 | + const struct nlattr *nla, *attrs; |
| 256 | + struct nlmsghdr *nlh_dst; |
| 257 | + int len, remaining; |
| 258 | + |
| 259 | + nlh_dst = xfrm_nlmsg_put_compat(dst, nlh_src, type); |
| 260 | + if (IS_ERR(nlh_dst)) |
| 261 | + return PTR_ERR(nlh_dst); |
| 262 | + |
| 263 | + attrs = nlmsg_attrdata(nlh_src, xfrm_msg_min[type]); |
| 264 | + len = nlmsg_attrlen(nlh_src, xfrm_msg_min[type]); |
| 265 | + |
| 266 | + nla_for_each_attr(nla, attrs, len, remaining) { |
| 267 | + int err = xfrm_xlate64_attr(dst, nla); |
| 268 | + |
| 269 | + if (err) |
| 270 | + return err; |
| 271 | + } |
| 272 | + |
| 273 | + nlmsg_end(dst, nlh_dst); |
| 274 | + |
| 275 | + return 0; |
| 276 | +} |
| 277 | + |
| 278 | +static int xfrm_alloc_compat(struct sk_buff *skb, const struct nlmsghdr *nlh_src) |
| 279 | +{ |
| 280 | + u16 type = nlh_src->nlmsg_type - XFRM_MSG_BASE; |
| 281 | + struct sk_buff *new = NULL; |
| 282 | + int err; |
| 283 | + |
| 284 | + if (WARN_ON_ONCE(type >= ARRAY_SIZE(xfrm_msg_min))) |
| 285 | + return -EOPNOTSUPP; |
| 286 | + |
| 287 | + if (skb_shinfo(skb)->frag_list == NULL) { |
| 288 | + new = alloc_skb(skb->len + skb_tailroom(skb), GFP_ATOMIC); |
| 289 | + if (!new) |
| 290 | + return -ENOMEM; |
| 291 | + skb_shinfo(skb)->frag_list = new; |
| 292 | + } |
| 293 | + |
| 294 | + err = xfrm_xlate64(skb_shinfo(skb)->frag_list, nlh_src); |
| 295 | + if (err) { |
| 296 | + if (new) { |
| 297 | + kfree_skb(new); |
| 298 | + skb_shinfo(skb)->frag_list = NULL; |
| 299 | + } |
| 300 | + return err; |
| 301 | + } |
| 302 | + |
| 303 | + return 0; |
| 304 | +} |
| 305 | + |
11 | 306 | static struct xfrm_translator xfrm_translator = { |
12 | 307 | .owner = THIS_MODULE, |
| 308 | + .alloc_compat = xfrm_alloc_compat, |
13 | 309 | }; |
14 | 310 |
|
15 | 311 | static int __init xfrm_compat_init(void) |
|
0 commit comments