Commit 26b15dad authored by Jamal Hadi Salim's avatar Jamal Hadi Salim Committed by David S. Miller

[IPSEC] Add complete xfrm event notification

Heres the final patch.
What this patch provides

- netlink xfrm events
- ability to have events generated by netlink propagated to pfkey
  and vice versa.
- fixes the acquire lets-be-happy-with-one-success issue
Signed-off-by: default avatarJamal Hadi Salim <hadi@cyberus.ca>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 3aa3dfb3
...@@ -257,5 +257,7 @@ struct xfrm_usersa_flush { ...@@ -257,5 +257,7 @@ struct xfrm_usersa_flush {
#define XFRMGRP_ACQUIRE 1 #define XFRMGRP_ACQUIRE 1
#define XFRMGRP_EXPIRE 2 #define XFRMGRP_EXPIRE 2
#define XFRMGRP_SA 4
#define XFRMGRP_POLICY 8
#endif /* _LINUX_XFRM_H */ #endif /* _LINUX_XFRM_H */
...@@ -158,6 +158,27 @@ enum { ...@@ -158,6 +158,27 @@ enum {
XFRM_STATE_DEAD XFRM_STATE_DEAD
}; };
/* events that could be sent by kernel */
enum {
XFRM_SAP_INVALID,
XFRM_SAP_EXPIRED,
XFRM_SAP_ADDED,
XFRM_SAP_UPDATED,
XFRM_SAP_DELETED,
XFRM_SAP_FLUSHED,
__XFRM_SAP_MAX
};
#define XFRM_SAP_MAX (__XFRM_SAP_MAX - 1)
/* callback structure passed from either netlink or pfkey */
struct km_event
{
u32 data;
u32 seq;
u32 pid;
u32 event;
};
struct xfrm_type; struct xfrm_type;
struct xfrm_dst; struct xfrm_dst;
struct xfrm_policy_afinfo { struct xfrm_policy_afinfo {
...@@ -179,6 +200,8 @@ struct xfrm_policy_afinfo { ...@@ -179,6 +200,8 @@ struct xfrm_policy_afinfo {
extern int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo); extern int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo);
extern int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo); extern int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo);
extern void km_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c);
extern void km_state_notify(struct xfrm_state *x, struct km_event *c);
#define XFRM_ACQ_EXPIRES 30 #define XFRM_ACQ_EXPIRES 30
...@@ -290,11 +313,11 @@ struct xfrm_mgr ...@@ -290,11 +313,11 @@ struct xfrm_mgr
{ {
struct list_head list; struct list_head list;
char *id; char *id;
int (*notify)(struct xfrm_state *x, int event); int (*notify)(struct xfrm_state *x, struct km_event *c);
int (*acquire)(struct xfrm_state *x, struct xfrm_tmpl *, struct xfrm_policy *xp, int dir); int (*acquire)(struct xfrm_state *x, struct xfrm_tmpl *, struct xfrm_policy *xp, int dir);
struct xfrm_policy *(*compile_policy)(u16 family, int opt, u8 *data, int len, int *dir); struct xfrm_policy *(*compile_policy)(u16 family, int opt, u8 *data, int len, int *dir);
int (*new_mapping)(struct xfrm_state *x, xfrm_address_t *ipaddr, u16 sport); int (*new_mapping)(struct xfrm_state *x, xfrm_address_t *ipaddr, u16 sport);
int (*notify_policy)(struct xfrm_policy *x, int dir, int event); int (*notify_policy)(struct xfrm_policy *x, int dir, struct km_event *c);
}; };
extern int xfrm_register_km(struct xfrm_mgr *km); extern int xfrm_register_km(struct xfrm_mgr *km);
...@@ -817,7 +840,7 @@ extern int xfrm_state_add(struct xfrm_state *x); ...@@ -817,7 +840,7 @@ extern int xfrm_state_add(struct xfrm_state *x);
extern int xfrm_state_update(struct xfrm_state *x); extern int xfrm_state_update(struct xfrm_state *x);
extern struct xfrm_state *xfrm_state_lookup(xfrm_address_t *daddr, u32 spi, u8 proto, unsigned short family); extern struct xfrm_state *xfrm_state_lookup(xfrm_address_t *daddr, u32 spi, u8 proto, unsigned short family);
extern struct xfrm_state *xfrm_find_acq_byseq(u32 seq); extern struct xfrm_state *xfrm_find_acq_byseq(u32 seq);
extern void xfrm_state_delete(struct xfrm_state *x); extern int xfrm_state_delete(struct xfrm_state *x);
extern void xfrm_state_flush(u8 proto); extern void xfrm_state_flush(u8 proto);
extern int xfrm_replay_check(struct xfrm_state *x, u32 seq); extern int xfrm_replay_check(struct xfrm_state *x, u32 seq);
extern void xfrm_replay_advance(struct xfrm_state *x, u32 seq); extern void xfrm_replay_advance(struct xfrm_state *x, u32 seq);
......
...@@ -1240,13 +1240,85 @@ static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, struct sadb_msg * ...@@ -1240,13 +1240,85 @@ static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, struct sadb_msg *
return 0; return 0;
} }
static inline int event2poltype(int event)
{
switch (event) {
case XFRM_SAP_DELETED:
return SADB_X_SPDDELETE;
case XFRM_SAP_ADDED:
return SADB_X_SPDADD;
case XFRM_SAP_UPDATED:
return SADB_X_SPDUPDATE;
case XFRM_SAP_EXPIRED:
// return SADB_X_SPDEXPIRE;
default:
printk("pfkey: Unknown policy event %d\n", event);
break;
}
return 0;
}
static inline int event2keytype(int event)
{
switch (event) {
case XFRM_SAP_DELETED:
return SADB_DELETE;
case XFRM_SAP_ADDED:
return SADB_ADD;
case XFRM_SAP_UPDATED:
return SADB_UPDATE;
case XFRM_SAP_EXPIRED:
return SADB_EXPIRE;
default:
printk("pfkey: Unknown SA event %d\n", event);
break;
}
return 0;
}
/* ADD/UPD/DEL */
static int key_notify_sa(struct xfrm_state *x, struct km_event *c)
{
struct sk_buff *skb;
struct sadb_msg *hdr;
int hsc = 3;
if (c->event == XFRM_SAP_DELETED)
hsc = 0;
if (c->event == XFRM_SAP_EXPIRED) {
if (c->data)
hsc = 2;
else
hsc = 1;
}
skb = pfkey_xfrm_state2msg(x, 0, hsc);
if (IS_ERR(skb))
return PTR_ERR(skb);
hdr = (struct sadb_msg *) skb->data;
hdr->sadb_msg_version = PF_KEY_V2;
hdr->sadb_msg_type = event2keytype(c->event);
hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
hdr->sadb_msg_errno = 0;
hdr->sadb_msg_reserved = 0;
hdr->sadb_msg_seq = c->seq;
hdr->sadb_msg_pid = c->pid;
pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
return 0;
}
static int pfkey_add(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs) static int pfkey_add(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
{ {
struct sk_buff *out_skb;
struct sadb_msg *out_hdr;
struct xfrm_state *x; struct xfrm_state *x;
int err; int err;
struct km_event c;
xfrm_probe_algs(); xfrm_probe_algs();
...@@ -1254,6 +1326,7 @@ static int pfkey_add(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, ...@@ -1254,6 +1326,7 @@ static int pfkey_add(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr,
if (IS_ERR(x)) if (IS_ERR(x))
return PTR_ERR(x); return PTR_ERR(x);
xfrm_state_hold(x);
if (hdr->sadb_msg_type == SADB_ADD) if (hdr->sadb_msg_type == SADB_ADD)
err = xfrm_state_add(x); err = xfrm_state_add(x);
else else
...@@ -1265,27 +1338,23 @@ static int pfkey_add(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, ...@@ -1265,27 +1338,23 @@ static int pfkey_add(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr,
return err; return err;
} }
out_skb = pfkey_xfrm_state2msg(x, 0, 3); if (hdr->sadb_msg_type == SADB_ADD)
if (IS_ERR(out_skb)) c.event = XFRM_SAP_ADDED;
return PTR_ERR(out_skb); /* XXX Should we return 0 here ? */ else
c.event = XFRM_SAP_UPDATED;
out_hdr = (struct sadb_msg *) out_skb->data; c.seq = hdr->sadb_msg_seq;
out_hdr->sadb_msg_version = hdr->sadb_msg_version; c.pid = hdr->sadb_msg_pid;
out_hdr->sadb_msg_type = hdr->sadb_msg_type; km_state_notify(x, &c);
out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto); xfrm_state_put(x);
out_hdr->sadb_msg_errno = 0;
out_hdr->sadb_msg_reserved = 0;
out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, sk);
return 0; return err;
} }
static int pfkey_delete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs) static int pfkey_delete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
{ {
struct xfrm_state *x; struct xfrm_state *x;
struct km_event c;
int err;
if (!ext_hdrs[SADB_EXT_SA-1] || if (!ext_hdrs[SADB_EXT_SA-1] ||
!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1], !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
...@@ -1301,13 +1370,19 @@ static int pfkey_delete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *h ...@@ -1301,13 +1370,19 @@ static int pfkey_delete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *h
return -EPERM; return -EPERM;
} }
xfrm_state_delete(x); err = xfrm_state_delete(x);
if (err < 0) {
xfrm_state_put(x); xfrm_state_put(x);
return err;
}
pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL, c.seq = hdr->sadb_msg_seq;
BROADCAST_ALL, sk); c.pid = hdr->sadb_msg_pid;
c.event = XFRM_SAP_DELETED;
km_state_notify(x, &c);
xfrm_state_put(x);
return 0; return err;
} }
static int pfkey_get(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs) static int pfkey_get(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
...@@ -1445,28 +1520,42 @@ static int pfkey_register(struct sock *sk, struct sk_buff *skb, struct sadb_msg ...@@ -1445,28 +1520,42 @@ static int pfkey_register(struct sock *sk, struct sk_buff *skb, struct sadb_msg
return 0; return 0;
} }
static int key_notify_sa_flush(struct km_event *c)
{
struct sk_buff *skb;
struct sadb_msg *hdr;
skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
if (!skb)
return -ENOBUFS;
hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
hdr->sadb_msg_satype = pfkey_proto2satype(c->data);
hdr->sadb_msg_seq = c->seq;
hdr->sadb_msg_pid = c->pid;
hdr->sadb_msg_version = PF_KEY_V2;
hdr->sadb_msg_errno = (uint8_t) 0;
hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
return 0;
}
static int pfkey_flush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs) static int pfkey_flush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
{ {
unsigned proto; unsigned proto;
struct sk_buff *skb_out; struct km_event c;
struct sadb_msg *hdr_out;
proto = pfkey_satype2proto(hdr->sadb_msg_satype); proto = pfkey_satype2proto(hdr->sadb_msg_satype);
if (proto == 0) if (proto == 0)
return -EINVAL; return -EINVAL;
skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
if (!skb_out)
return -ENOBUFS;
xfrm_state_flush(proto); xfrm_state_flush(proto);
c.data = proto;
hdr_out = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg)); c.seq = hdr->sadb_msg_seq;
pfkey_hdr_dup(hdr_out, hdr); c.pid = hdr->sadb_msg_pid;
hdr_out->sadb_msg_errno = (uint8_t) 0; c.event = XFRM_SAP_FLUSHED;
hdr_out->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t)); km_state_notify(NULL, &c);
pfkey_broadcast(skb_out, GFP_KERNEL, BROADCAST_ALL, NULL);
return 0; return 0;
} }
...@@ -1859,6 +1948,35 @@ static void pfkey_xfrm_policy2msg(struct sk_buff *skb, struct xfrm_policy *xp, i ...@@ -1859,6 +1948,35 @@ static void pfkey_xfrm_policy2msg(struct sk_buff *skb, struct xfrm_policy *xp, i
hdr->sadb_msg_reserved = atomic_read(&xp->refcnt); hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
} }
static int key_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
{
struct sk_buff *out_skb;
struct sadb_msg *out_hdr;
int err;
out_skb = pfkey_xfrm_policy2msg_prep(xp);
if (IS_ERR(out_skb)) {
err = PTR_ERR(out_skb);
goto out;
}
pfkey_xfrm_policy2msg(out_skb, xp, dir);
out_hdr = (struct sadb_msg *) out_skb->data;
out_hdr->sadb_msg_version = PF_KEY_V2;
if (c->data && c->event == XFRM_SAP_DELETED)
out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
else
out_hdr->sadb_msg_type = event2poltype(c->event);
out_hdr->sadb_msg_errno = 0;
out_hdr->sadb_msg_seq = c->seq;
out_hdr->sadb_msg_pid = c->pid;
pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
out:
return 0;
}
static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs) static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
{ {
int err; int err;
...@@ -1866,8 +1984,7 @@ static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, struct sadb_msg *h ...@@ -1866,8 +1984,7 @@ static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, struct sadb_msg *h
struct sadb_address *sa; struct sadb_address *sa;
struct sadb_x_policy *pol; struct sadb_x_policy *pol;
struct xfrm_policy *xp; struct xfrm_policy *xp;
struct sk_buff *out_skb; struct km_event c;
struct sadb_msg *out_hdr;
if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1], if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
ext_hdrs[SADB_EXT_ADDRESS_DST-1]) || ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
...@@ -1935,31 +2052,23 @@ static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, struct sadb_msg *h ...@@ -1935,31 +2052,23 @@ static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, struct sadb_msg *h
(err = parse_ipsecrequests(xp, pol)) < 0) (err = parse_ipsecrequests(xp, pol)) < 0)
goto out; goto out;
out_skb = pfkey_xfrm_policy2msg_prep(xp);
if (IS_ERR(out_skb)) {
err = PTR_ERR(out_skb);
goto out;
}
err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp, err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
hdr->sadb_msg_type != SADB_X_SPDUPDATE); hdr->sadb_msg_type != SADB_X_SPDUPDATE);
if (err) { if (err) {
kfree_skb(out_skb); kfree(xp);
goto out; return err;
} }
pfkey_xfrm_policy2msg(out_skb, xp, pol->sadb_x_policy_dir-1); if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
c.event = XFRM_SAP_UPDATED;
else
c.event = XFRM_SAP_ADDED;
xfrm_pol_put(xp); c.seq = hdr->sadb_msg_seq;
c.pid = hdr->sadb_msg_pid;
out_hdr = (struct sadb_msg *) out_skb->data; km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
out_hdr->sadb_msg_version = hdr->sadb_msg_version; xfrm_pol_put(xp);
out_hdr->sadb_msg_type = hdr->sadb_msg_type;
out_hdr->sadb_msg_satype = 0;
out_hdr->sadb_msg_errno = 0;
out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, sk);
return 0; return 0;
out: out:
...@@ -1973,9 +2082,8 @@ static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, struct sadb_msg ...@@ -1973,9 +2082,8 @@ static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, struct sadb_msg
struct sadb_address *sa; struct sadb_address *sa;
struct sadb_x_policy *pol; struct sadb_x_policy *pol;
struct xfrm_policy *xp; struct xfrm_policy *xp;
struct sk_buff *out_skb;
struct sadb_msg *out_hdr;
struct xfrm_selector sel; struct xfrm_selector sel;
struct km_event c;
if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1], if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
ext_hdrs[SADB_EXT_ADDRESS_DST-1]) || ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
...@@ -2010,25 +2118,40 @@ static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, struct sadb_msg ...@@ -2010,25 +2118,40 @@ static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, struct sadb_msg
err = 0; err = 0;
c.seq = hdr->sadb_msg_seq;
c.pid = hdr->sadb_msg_pid;
c.event = XFRM_SAP_DELETED;
km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
xfrm_pol_put(xp);
return err;
}
static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, struct sadb_msg *hdr, int dir)
{
int err;
struct sk_buff *out_skb;
struct sadb_msg *out_hdr;
err = 0;
out_skb = pfkey_xfrm_policy2msg_prep(xp); out_skb = pfkey_xfrm_policy2msg_prep(xp);
if (IS_ERR(out_skb)) { if (IS_ERR(out_skb)) {
err = PTR_ERR(out_skb); err = PTR_ERR(out_skb);
goto out; goto out;
} }
pfkey_xfrm_policy2msg(out_skb, xp, pol->sadb_x_policy_dir-1); pfkey_xfrm_policy2msg(out_skb, xp, dir);
out_hdr = (struct sadb_msg *) out_skb->data; out_hdr = (struct sadb_msg *) out_skb->data;
out_hdr->sadb_msg_version = hdr->sadb_msg_version; out_hdr->sadb_msg_version = hdr->sadb_msg_version;
out_hdr->sadb_msg_type = SADB_X_SPDDELETE; out_hdr->sadb_msg_type = hdr->sadb_msg_type;
out_hdr->sadb_msg_satype = 0; out_hdr->sadb_msg_satype = 0;
out_hdr->sadb_msg_errno = 0; out_hdr->sadb_msg_errno = 0;
out_hdr->sadb_msg_seq = hdr->sadb_msg_seq; out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
out_hdr->sadb_msg_pid = hdr->sadb_msg_pid; out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, sk); pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
err = 0; err = 0;
out: out:
xfrm_pol_put(xp);
return err; return err;
} }
...@@ -2037,8 +2160,7 @@ static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, struct sadb_msg *h ...@@ -2037,8 +2160,7 @@ static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, struct sadb_msg *h
int err; int err;
struct sadb_x_policy *pol; struct sadb_x_policy *pol;
struct xfrm_policy *xp; struct xfrm_policy *xp;
struct sk_buff *out_skb; struct km_event c;
struct sadb_msg *out_hdr;
if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL) if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
return -EINVAL; return -EINVAL;
...@@ -2050,24 +2172,16 @@ static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, struct sadb_msg *h ...@@ -2050,24 +2172,16 @@ static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, struct sadb_msg *h
err = 0; err = 0;
out_skb = pfkey_xfrm_policy2msg_prep(xp); c.seq = hdr->sadb_msg_seq;
if (IS_ERR(out_skb)) { c.pid = hdr->sadb_msg_pid;
err = PTR_ERR(out_skb); if (hdr->sadb_msg_type == SADB_X_SPDDELETE2) {
goto out; c.data = 1; // to signal pfkey of SADB_X_SPDDELETE2
c.event = XFRM_SAP_DELETED;
km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
} else {
err = key_pol_get_resp(sk, xp, hdr, pol->sadb_x_policy_dir-1);
} }
pfkey_xfrm_policy2msg(out_skb, xp, pol->sadb_x_policy_dir-1);
out_hdr = (struct sadb_msg *) out_skb->data;
out_hdr->sadb_msg_version = hdr->sadb_msg_version;
out_hdr->sadb_msg_type = hdr->sadb_msg_type;
out_hdr->sadb_msg_satype = 0;
out_hdr->sadb_msg_errno = 0;
out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, sk);
err = 0;
out:
xfrm_pol_put(xp); xfrm_pol_put(xp);
return err; return err;
} }
...@@ -2102,22 +2216,34 @@ static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, struct sadb_msg * ...@@ -2102,22 +2216,34 @@ static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *
return xfrm_policy_walk(dump_sp, &data); return xfrm_policy_walk(dump_sp, &data);
} }
static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs) static int key_notify_policy_flush(struct km_event *c)
{ {
struct sk_buff *skb_out; struct sk_buff *skb_out;
struct sadb_msg *hdr_out; struct sadb_msg *hdr;
skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL); skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
if (!skb_out) if (!skb_out)
return -ENOBUFS; return -ENOBUFS;
hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
hdr->sadb_msg_seq = c->seq;
hdr->sadb_msg_pid = c->pid;
hdr->sadb_msg_version = PF_KEY_V2;
hdr->sadb_msg_errno = (uint8_t) 0;
hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL);
return 0;
xfrm_policy_flush(); }
hdr_out = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg)); static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
pfkey_hdr_dup(hdr_out, hdr); {
hdr_out->sadb_msg_errno = (uint8_t) 0; struct km_event c;
hdr_out->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
pfkey_broadcast(skb_out, GFP_KERNEL, BROADCAST_ALL, NULL); xfrm_policy_flush();
c.event = XFRM_SAP_FLUSHED;
c.pid = hdr->sadb_msg_pid;
c.seq = hdr->sadb_msg_seq;
km_policy_notify(NULL, 0, &c);
return 0; return 0;
} }
...@@ -2317,11 +2443,23 @@ static void dump_esp_combs(struct sk_buff *skb, struct xfrm_tmpl *t) ...@@ -2317,11 +2443,23 @@ static void dump_esp_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
} }
} }
static int pfkey_send_notify(struct xfrm_state *x, int hard) static int key_notify_policy_expire(struct xfrm_policy *xp, struct km_event *c)
{
return 0;
}
static int key_notify_sa_expire(struct xfrm_state *x, struct km_event *c)
{ {
struct sk_buff *out_skb; struct sk_buff *out_skb;
struct sadb_msg *out_hdr; struct sadb_msg *out_hdr;
int hsc = (hard ? 2 : 1); int hard;
int hsc;
hard = c->data;
if (hard)
hsc = 2;
else
hsc = 1;
out_skb = pfkey_xfrm_state2msg(x, 0, hsc); out_skb = pfkey_xfrm_state2msg(x, 0, hsc);
if (IS_ERR(out_skb)) if (IS_ERR(out_skb))
...@@ -2340,6 +2478,44 @@ static int pfkey_send_notify(struct xfrm_state *x, int hard) ...@@ -2340,6 +2478,44 @@ static int pfkey_send_notify(struct xfrm_state *x, int hard)
return 0; return 0;
} }
static int pfkey_send_notify(struct xfrm_state *x, struct km_event *c)
{
switch (c->event) {
case XFRM_SAP_EXPIRED:
return key_notify_sa_expire(x, c);
case XFRM_SAP_DELETED:
case XFRM_SAP_ADDED:
case XFRM_SAP_UPDATED:
return key_notify_sa(x, c);
case XFRM_SAP_FLUSHED:
return key_notify_sa_flush(c);
default:
printk("pfkey: Unknown SA event %d\n", c->event);
break;
}
return 0;
}
static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
{
switch (c->event) {
case XFRM_SAP_EXPIRED:
return key_notify_policy_expire(xp, c);
case XFRM_SAP_DELETED:
case XFRM_SAP_ADDED:
case XFRM_SAP_UPDATED:
return key_notify_policy(xp, dir, c);
case XFRM_SAP_FLUSHED:
return key_notify_policy_flush(c);
default:
printk("pfkey: Unknown policy event %d\n", c->event);
break;
}
return 0;
}
static u32 get_acqseq(void) static u32 get_acqseq(void)
{ {
u32 res; u32 res;
...@@ -2856,6 +3032,7 @@ static struct xfrm_mgr pfkeyv2_mgr = ...@@ -2856,6 +3032,7 @@ static struct xfrm_mgr pfkeyv2_mgr =
.acquire = pfkey_send_acquire, .acquire = pfkey_send_acquire,
.compile_policy = pfkey_compile_policy, .compile_policy = pfkey_compile_policy,
.new_mapping = pfkey_send_new_mapping, .new_mapping = pfkey_send_new_mapping,
.notify_policy = pfkey_send_policy_notify,
}; };
static void __exit ipsec_pfkey_exit(void) static void __exit ipsec_pfkey_exit(void)
......
...@@ -50,7 +50,7 @@ static DEFINE_SPINLOCK(xfrm_state_gc_lock); ...@@ -50,7 +50,7 @@ static DEFINE_SPINLOCK(xfrm_state_gc_lock);
static int xfrm_state_gc_flush_bundles; static int xfrm_state_gc_flush_bundles;
static void __xfrm_state_delete(struct xfrm_state *x); static int __xfrm_state_delete(struct xfrm_state *x);
static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned short family); static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned short family);
static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo); static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo);
...@@ -215,8 +215,10 @@ void __xfrm_state_destroy(struct xfrm_state *x) ...@@ -215,8 +215,10 @@ void __xfrm_state_destroy(struct xfrm_state *x)
} }
EXPORT_SYMBOL(__xfrm_state_destroy); EXPORT_SYMBOL(__xfrm_state_destroy);
static void __xfrm_state_delete(struct xfrm_state *x) static int __xfrm_state_delete(struct xfrm_state *x)
{ {
int err = -ESRCH;
if (x->km.state != XFRM_STATE_DEAD) { if (x->km.state != XFRM_STATE_DEAD) {
x->km.state = XFRM_STATE_DEAD; x->km.state = XFRM_STATE_DEAD;
spin_lock(&xfrm_state_lock); spin_lock(&xfrm_state_lock);
...@@ -245,14 +247,21 @@ static void __xfrm_state_delete(struct xfrm_state *x) ...@@ -245,14 +247,21 @@ static void __xfrm_state_delete(struct xfrm_state *x)
* is what we are dropping here. * is what we are dropping here.
*/ */
atomic_dec(&x->refcnt); atomic_dec(&x->refcnt);
err = 0;
} }
return err;
} }
void xfrm_state_delete(struct xfrm_state *x) int xfrm_state_delete(struct xfrm_state *x)
{ {
int err;
spin_lock_bh(&x->lock); spin_lock_bh(&x->lock);
__xfrm_state_delete(x); err = __xfrm_state_delete(x);
spin_unlock_bh(&x->lock); spin_unlock_bh(&x->lock);
return err;
} }
EXPORT_SYMBOL(xfrm_state_delete); EXPORT_SYMBOL(xfrm_state_delete);
...@@ -796,34 +805,60 @@ EXPORT_SYMBOL(xfrm_replay_advance); ...@@ -796,34 +805,60 @@ EXPORT_SYMBOL(xfrm_replay_advance);
static struct list_head xfrm_km_list = LIST_HEAD_INIT(xfrm_km_list); static struct list_head xfrm_km_list = LIST_HEAD_INIT(xfrm_km_list);
static DEFINE_RWLOCK(xfrm_km_lock); static DEFINE_RWLOCK(xfrm_km_lock);
static void km_state_expired(struct xfrm_state *x, int hard) void km_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
{ {
struct xfrm_mgr *km; struct xfrm_mgr *km;
if (hard) read_lock(&xfrm_km_lock);
x->km.state = XFRM_STATE_EXPIRED; list_for_each_entry(km, &xfrm_km_list, list)
else if (km->notify_policy)
x->km.dying = 1; km->notify_policy(xp, dir, c);
read_unlock(&xfrm_km_lock);
}
void km_state_notify(struct xfrm_state *x, struct km_event *c)
{
struct xfrm_mgr *km;
read_lock(&xfrm_km_lock); read_lock(&xfrm_km_lock);
list_for_each_entry(km, &xfrm_km_list, list) list_for_each_entry(km, &xfrm_km_list, list)
km->notify(x, hard); if (km->notify)
km->notify(x, c);
read_unlock(&xfrm_km_lock); read_unlock(&xfrm_km_lock);
}
EXPORT_SYMBOL(km_policy_notify);
EXPORT_SYMBOL(km_state_notify);
static void km_state_expired(struct xfrm_state *x, int hard)
{
struct km_event c;
if (hard)
x->km.state = XFRM_STATE_EXPIRED;
else
x->km.dying = 1;
c.data = hard;
c.event = XFRM_SAP_EXPIRED;
km_state_notify(x, &c);
if (hard) if (hard)
wake_up(&km_waitq); wake_up(&km_waitq);
} }
/*
* We send to all registered managers regardless of failure
* We are happy with one success
*/
static int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol) static int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
{ {
int err = -EINVAL; int err = -EINVAL, acqret;
struct xfrm_mgr *km; struct xfrm_mgr *km;
read_lock(&xfrm_km_lock); read_lock(&xfrm_km_lock);
list_for_each_entry(km, &xfrm_km_list, list) { list_for_each_entry(km, &xfrm_km_list, list) {
err = km->acquire(x, t, pol, XFRM_POLICY_OUT); acqret = km->acquire(x, t, pol, XFRM_POLICY_OUT);
if (!err) if (!acqret)
break; err = acqret;
} }
read_unlock(&xfrm_km_lock); read_unlock(&xfrm_km_lock);
return err; return err;
...@@ -848,13 +883,12 @@ EXPORT_SYMBOL(km_new_mapping); ...@@ -848,13 +883,12 @@ EXPORT_SYMBOL(km_new_mapping);
void km_policy_expired(struct xfrm_policy *pol, int dir, int hard) void km_policy_expired(struct xfrm_policy *pol, int dir, int hard)
{ {
struct xfrm_mgr *km; struct km_event c;
read_lock(&xfrm_km_lock); c.data = hard;
list_for_each_entry(km, &xfrm_km_list, list) c.data = hard;
if (km->notify_policy) c.event = XFRM_SAP_EXPIRED;
km->notify_policy(pol, dir, hard); km_policy_notify(pol, dir, &c);
read_unlock(&xfrm_km_lock);
if (hard) if (hard)
wake_up(&km_waitq); wake_up(&km_waitq);
......
...@@ -277,6 +277,7 @@ static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) ...@@ -277,6 +277,7 @@ static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
struct xfrm_usersa_info *p = NLMSG_DATA(nlh); struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
struct xfrm_state *x; struct xfrm_state *x;
int err; int err;
struct km_event c;
err = verify_newsa_info(p, (struct rtattr **) xfrma); err = verify_newsa_info(p, (struct rtattr **) xfrma);
if (err) if (err)
...@@ -286,6 +287,7 @@ static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) ...@@ -286,6 +287,7 @@ static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
if (!x) if (!x)
return err; return err;
xfrm_state_hold(x);
if (nlh->nlmsg_type == XFRM_MSG_NEWSA) if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
err = xfrm_state_add(x); err = xfrm_state_add(x);
else else
...@@ -294,14 +296,27 @@ static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) ...@@ -294,14 +296,27 @@ static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
if (err < 0) { if (err < 0) {
x->km.state = XFRM_STATE_DEAD; x->km.state = XFRM_STATE_DEAD;
xfrm_state_put(x); xfrm_state_put(x);
return err;
} }
c.seq = nlh->nlmsg_seq;
c.pid = nlh->nlmsg_pid;
if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
c.event = XFRM_SAP_ADDED;
else
c.event = XFRM_SAP_UPDATED;
km_state_notify(x, &c);
xfrm_state_put(x);
return err; return err;
} }
static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
{ {
struct xfrm_state *x; struct xfrm_state *x;
int err;
struct km_event c;
struct xfrm_usersa_id *p = NLMSG_DATA(nlh); struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family); x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
...@@ -313,10 +328,19 @@ static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) ...@@ -313,10 +328,19 @@ static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
return -EPERM; return -EPERM;
} }
xfrm_state_delete(x); err = xfrm_state_delete(x);
if (err < 0) {
xfrm_state_put(x); xfrm_state_put(x);
return err;
}
return 0; c.seq = nlh->nlmsg_seq;
c.pid = nlh->nlmsg_pid;
c.event = XFRM_SAP_DELETED;
km_state_notify(x, &c);
xfrm_state_put(x);
return err;
} }
static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p) static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
...@@ -681,6 +705,7 @@ static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr ...@@ -681,6 +705,7 @@ static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr
{ {
struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh); struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
struct xfrm_policy *xp; struct xfrm_policy *xp;
struct km_event c;
int err; int err;
int excl; int excl;
...@@ -692,6 +717,10 @@ static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr ...@@ -692,6 +717,10 @@ static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr
if (!xp) if (!xp)
return err; return err;
/* shouldnt excl be based on nlh flags??
* Aha! this is anti-netlink really i.e more pfkey derived
* in netlink excl is a flag and you wouldnt need
* a type XFRM_MSG_UPDPOLICY - JHS */
excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY; excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
err = xfrm_policy_insert(p->dir, xp, excl); err = xfrm_policy_insert(p->dir, xp, excl);
if (err) { if (err) {
...@@ -699,6 +728,15 @@ static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr ...@@ -699,6 +728,15 @@ static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr
return err; return err;
} }
if (!excl)
c.event = XFRM_SAP_UPDATED;
else
c.event = XFRM_SAP_ADDED;
c.seq = nlh->nlmsg_seq;
c.pid = nlh->nlmsg_pid;
km_policy_notify(xp, p->dir, &c);
xfrm_pol_put(xp); xfrm_pol_put(xp);
return 0; return 0;
...@@ -816,6 +854,7 @@ static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr ...@@ -816,6 +854,7 @@ static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr
struct xfrm_policy *xp; struct xfrm_policy *xp;
struct xfrm_userpolicy_id *p; struct xfrm_userpolicy_id *p;
int err; int err;
struct km_event c;
int delete; int delete;
p = NLMSG_DATA(nlh); p = NLMSG_DATA(nlh);
...@@ -843,6 +882,11 @@ static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr ...@@ -843,6 +882,11 @@ static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr
NETLINK_CB(skb).pid, NETLINK_CB(skb).pid,
MSG_DONTWAIT); MSG_DONTWAIT);
} }
} else {
c.event = XFRM_SAP_DELETED;
c.seq = nlh->nlmsg_seq;
c.pid = nlh->nlmsg_pid;
km_policy_notify(xp, p->dir, &c);
} }
xfrm_pol_put(xp); xfrm_pol_put(xp);
...@@ -852,15 +896,28 @@ static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr ...@@ -852,15 +896,28 @@ static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr
static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
{ {
struct km_event c;
struct xfrm_usersa_flush *p = NLMSG_DATA(nlh); struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
xfrm_state_flush(p->proto); xfrm_state_flush(p->proto);
c.data = p->proto;
c.event = XFRM_SAP_FLUSHED;
c.seq = nlh->nlmsg_seq;
c.pid = nlh->nlmsg_pid;
km_state_notify(NULL, &c);
return 0; return 0;
} }
static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
{ {
struct km_event c;
xfrm_policy_flush(); xfrm_policy_flush();
c.event = XFRM_SAP_FLUSHED;
c.seq = nlh->nlmsg_seq;
c.pid = nlh->nlmsg_pid;
km_policy_notify(NULL, 0, &c);
return 0; return 0;
} }
...@@ -1069,10 +1126,12 @@ static int build_expire(struct sk_buff *skb, struct xfrm_state *x, int hard) ...@@ -1069,10 +1126,12 @@ static int build_expire(struct sk_buff *skb, struct xfrm_state *x, int hard)
return -1; return -1;
} }
static int xfrm_send_state_notify(struct xfrm_state *x, int hard) static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
{ {
struct sk_buff *skb; struct sk_buff *skb;
int hard = c ->data;
/* fix to do alloc using NLM macros */
skb = alloc_skb(sizeof(struct xfrm_user_expire) + 16, GFP_ATOMIC); skb = alloc_skb(sizeof(struct xfrm_user_expire) + 16, GFP_ATOMIC);
if (skb == NULL) if (skb == NULL)
return -ENOMEM; return -ENOMEM;
...@@ -1085,6 +1144,122 @@ static int xfrm_send_state_notify(struct xfrm_state *x, int hard) ...@@ -1085,6 +1144,122 @@ static int xfrm_send_state_notify(struct xfrm_state *x, int hard)
return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_EXPIRE, GFP_ATOMIC); return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_EXPIRE, GFP_ATOMIC);
} }
static int xfrm_notify_sa_flush(struct km_event *c)
{
struct xfrm_usersa_flush *p;
struct nlmsghdr *nlh;
struct sk_buff *skb;
unsigned char *b;
int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
skb = alloc_skb(len, GFP_ATOMIC);
if (skb == NULL)
return -ENOMEM;
b = skb->tail;
nlh = NLMSG_PUT(skb, c->pid, c->seq,
XFRM_MSG_FLUSHSA, sizeof(*p));
nlh->nlmsg_flags = 0;
p = NLMSG_DATA(nlh);
p->proto = c->data;
nlh->nlmsg_len = skb->tail - b;
return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_SA, GFP_ATOMIC);
nlmsg_failure:
kfree_skb(skb);
return -1;
}
static int inline xfrm_sa_len(struct xfrm_state *x)
{
int l = NLMSG_LENGTH(sizeof(struct xfrm_usersa_info));
if (x->aalg)
l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
if (x->ealg)
l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
if (x->calg)
l += RTA_SPACE(sizeof(*x->calg));
if (x->encap)
l += RTA_SPACE(sizeof(*x->encap));
return l;
}
static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
{
struct xfrm_usersa_info *p;
struct nlmsghdr *nlh;
struct sk_buff *skb;
u32 nlt;
unsigned char *b;
int len = xfrm_sa_len(x);
skb = alloc_skb(len, GFP_ATOMIC);
if (skb == NULL)
return -ENOMEM;
b = skb->tail;
if (c->event == XFRM_SAP_ADDED)
nlt = XFRM_MSG_NEWSA;
else if (c->event == XFRM_SAP_UPDATED)
nlt = XFRM_MSG_UPDSA;
else if (c->event == XFRM_SAP_DELETED)
nlt = XFRM_MSG_DELSA;
else
goto nlmsg_failure;
nlh = NLMSG_PUT(skb, c->pid, c->seq, nlt, sizeof(*p));
nlh->nlmsg_flags = 0;
p = NLMSG_DATA(nlh);
copy_to_user_state(x, p);
if (x->aalg)
RTA_PUT(skb, XFRMA_ALG_AUTH,
sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
if (x->ealg)
RTA_PUT(skb, XFRMA_ALG_CRYPT,
sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
if (x->calg)
RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
if (x->encap)
RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
nlh->nlmsg_len = skb->tail - b;
return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_SA, GFP_ATOMIC);
nlmsg_failure:
rtattr_failure:
kfree_skb(skb);
return -1;
}
static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
{
switch (c->event) {
case XFRM_SAP_EXPIRED:
return xfrm_exp_state_notify(x, c);
case XFRM_SAP_DELETED:
case XFRM_SAP_UPDATED:
case XFRM_SAP_ADDED:
return xfrm_notify_sa(x, c);
case XFRM_SAP_FLUSHED:
return xfrm_notify_sa_flush(c);
default:
printk("xfrm_user: Unknown SA event %d\n", c->event);
break;
}
return 0;
}
static int build_acquire(struct sk_buff *skb, struct xfrm_state *x, static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
struct xfrm_tmpl *xt, struct xfrm_policy *xp, struct xfrm_tmpl *xt, struct xfrm_policy *xp,
int dir) int dir)
...@@ -1218,7 +1393,7 @@ static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp, ...@@ -1218,7 +1393,7 @@ static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
return -1; return -1;
} }
static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, int hard) static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
{ {
struct sk_buff *skb; struct sk_buff *skb;
size_t len; size_t len;
...@@ -1229,7 +1404,7 @@ static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, int hard) ...@@ -1229,7 +1404,7 @@ static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, int hard)
if (skb == NULL) if (skb == NULL)
return -ENOMEM; return -ENOMEM;
if (build_polexpire(skb, xp, dir, hard) < 0) if (build_polexpire(skb, xp, dir, c->data) < 0)
BUG(); BUG();
NETLINK_CB(skb).dst_groups = XFRMGRP_EXPIRE; NETLINK_CB(skb).dst_groups = XFRMGRP_EXPIRE;
...@@ -1237,6 +1412,93 @@ static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, int hard) ...@@ -1237,6 +1412,93 @@ static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, int hard)
return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_EXPIRE, GFP_ATOMIC); return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_EXPIRE, GFP_ATOMIC);
} }
static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
{
struct xfrm_userpolicy_info *p;
struct nlmsghdr *nlh;
struct sk_buff *skb;
u32 nlt = 0 ;
unsigned char *b;
int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
len += NLMSG_SPACE(sizeof(struct xfrm_userpolicy_info));
skb = alloc_skb(len, GFP_ATOMIC);
if (skb == NULL)
return -ENOMEM;
b = skb->tail;
if (c->event == XFRM_SAP_ADDED)
nlt = XFRM_MSG_NEWPOLICY;
else if (c->event == XFRM_SAP_UPDATED)
nlt = XFRM_MSG_UPDPOLICY;
else if (c->event == XFRM_SAP_DELETED)
nlt = XFRM_MSG_DELPOLICY;
else
goto nlmsg_failure;
nlh = NLMSG_PUT(skb, c->pid, c->seq, nlt, sizeof(*p));
p = NLMSG_DATA(nlh);
nlh->nlmsg_flags = 0;
copy_to_user_policy(xp, p, dir);
if (copy_to_user_tmpl(xp, skb) < 0)
goto nlmsg_failure;
nlh->nlmsg_len = skb->tail - b;
return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_POLICY, GFP_ATOMIC);
nlmsg_failure:
kfree_skb(skb);
return -1;
}
static int xfrm_notify_policy_flush(struct km_event *c)
{
struct nlmsghdr *nlh;
struct sk_buff *skb;
unsigned char *b;
int len = NLMSG_LENGTH(0);
skb = alloc_skb(len, GFP_ATOMIC);
if (skb == NULL)
return -ENOMEM;
b = skb->tail;
nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
nlh->nlmsg_len = skb->tail - b;
return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_POLICY, GFP_ATOMIC);
nlmsg_failure:
kfree_skb(skb);
return -1;
}
static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
{
switch (c->event) {
case XFRM_SAP_ADDED:
case XFRM_SAP_UPDATED:
case XFRM_SAP_DELETED:
return xfrm_notify_policy(xp, dir, c);
case XFRM_SAP_FLUSHED:
return xfrm_notify_policy_flush(c);
case XFRM_SAP_EXPIRED:
return xfrm_exp_policy_notify(xp, dir, c);
default:
printk("xfrm_user: Unknown Policy event %d\n", c->event);
}
return 0;
}
static struct xfrm_mgr netlink_mgr = { static struct xfrm_mgr netlink_mgr = {
.id = "netlink", .id = "netlink",
.notify = xfrm_send_state_notify, .notify = xfrm_send_state_notify,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment