[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: Jamal Hadi Salim <hadi@cyberus.ca>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
This commit is contained in:
Jamal Hadi Salim
2005-06-18 22:42:13 -07:00
committed by David S. Miller
parent 3aa3dfb372
commit 26b15dad9f
5 changed files with 616 additions and 118 deletions

View File

@@ -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_state *x;
int err;
struct km_event c;
err = verify_newsa_info(p, (struct rtattr **) xfrma);
if (err)
@@ -286,6 +287,7 @@ static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
if (!x)
return err;
xfrm_state_hold(x);
if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
err = xfrm_state_add(x);
else
@@ -294,14 +296,27 @@ static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
if (err < 0) {
x->km.state = XFRM_STATE_DEAD;
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;
}
static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
{
struct xfrm_state *x;
int err;
struct km_event c;
struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
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)
return -EPERM;
}
xfrm_state_delete(x);
err = xfrm_state_delete(x);
if (err < 0) {
xfrm_state_put(x);
return err;
}
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 0;
return err;
}
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
{
struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
struct xfrm_policy *xp;
struct km_event c;
int err;
int excl;
@@ -692,6 +717,10 @@ static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr
if (!xp)
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;
err = xfrm_policy_insert(p->dir, xp, excl);
if (err) {
@@ -699,6 +728,15 @@ static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr
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);
return 0;
@@ -816,6 +854,7 @@ static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr
struct xfrm_policy *xp;
struct xfrm_userpolicy_id *p;
int err;
struct km_event c;
int delete;
p = NLMSG_DATA(nlh);
@@ -843,6 +882,11 @@ static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr
NETLINK_CB(skb).pid,
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);
@@ -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)
{
struct km_event c;
struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
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;
}
static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
{
struct km_event c;
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;
}
@@ -1069,10 +1126,12 @@ nlmsg_failure:
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;
int hard = c ->data;
/* fix to do alloc using NLM macros */
skb = alloc_skb(sizeof(struct xfrm_user_expire) + 16, GFP_ATOMIC);
if (skb == NULL)
return -ENOMEM;
@@ -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);
}
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,
struct xfrm_tmpl *xt, struct xfrm_policy *xp,
int dir)
@@ -1218,7 +1393,7 @@ nlmsg_failure:
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;
size_t len;
@@ -1229,7 +1404,7 @@ static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, int hard)
if (skb == NULL)
return -ENOMEM;
if (build_polexpire(skb, xp, dir, hard) < 0)
if (build_polexpire(skb, xp, dir, c->data) < 0)
BUG();
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)
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 = {
.id = "netlink",
.notify = xfrm_send_state_notify,