Skip to contentSkip to conversation

extension to XFRM for dynamic endpoint update (MIGRATE)

RFC PATCH In review Shinta Sugimoto 2 participants 4 messages Feb 1, 2007 – Feb 2, 2007

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/5 extension to XFRM for dynamic endpoint update (MIGRATE)

include/net/xfrm.h +73 -0
index e476541..eb61a99 100644
@@ -12,6 +12,7 @@ #include <linux/pfkeyv2.h>
 #include <linux/ipsec.h>
 #include <linux/in6.h>
 #include <linux/mutex.h>
+#include <linux/ipsec.h>
 
 #include <net/sock.h>
 #include <net/dst.h>
@@ -29,6 +30,8 @@ extern u32 sysctl_xfrm_aevent_rseqth;
 
 extern struct mutex xfrm_cfg_mutex;
 
+struct xfrm_migrate;
+
 /* Organization of SPD aka "XFRM rules"
    ------------------------------------
 
@@ -385,6 +388,7 @@ struct xfrm_mgr
 	int			(*new_mapping)(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport);
 	int			(*notify_policy)(struct xfrm_policy *x, int dir, struct km_event *c);
 	int			(*report)(u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr);
+	int			(*migrate)(struct xfrm_selector *sel, u8 dir, u8 type, struct xfrm_migrate *m, int num_bundles);
 };
 
 extern int xfrm_register_km(struct xfrm_mgr *km);
@@ -985,6 +989,16 @@ extern int xfrm_bundle_ok(struct xfrm_po
 			  struct flowi *fl, int family, int strict);
 extern void xfrm_init_pmtu(struct dst_entry *dst);
 
+#ifdef CONFIG_XFRM_MIGRATE
+extern int km_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
+		      struct xfrm_migrate *m, int num_bundles);
+extern struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m);
+extern struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x,
+					      struct xfrm_migrate *m);
+extern int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
+			struct xfrm_migrate *m, int num_bundles);
+#endif
+
 extern wait_queue_head_t km_waitq;
 extern int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport);
 extern void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 pid);
@@ -1050,5 +1064,64 @@ static inline void xfrm_aevent_doreplay(
 		xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
 }
 
+struct xfrm_state_endpoint {
+	u16 family;
+	xfrm_address_t saddr;
+	xfrm_address_t daddr;
+};
+
+struct xfrm_migrate {
+	u8 old_family;
+	u8 new_family;
+	u8 proto;
+	u8 mode;
+	u32 reqid;
+	xfrm_address_t old_daddr;
+	xfrm_address_t old_saddr;
+	xfrm_address_t new_daddr;
+	xfrm_address_t new_saddr;
+};
+
+#ifdef CONFIG_XFRM_MIGRATE
+static inline int __xfrm_addr_any(const xfrm_address_t *a,
+				  unsigned short family)
+{
+	switch (family) {
+	case AF_INET:
+		return (a->a4 == 0);
+	case AF_INET6:
+		return ipv6_addr_any((const struct in6_addr *)a->a6);
+	}
+	return -1;
+}
+
+static inline int xfrm_migrate_addr_check(struct xfrm_migrate *m) {
+	if ((xfrm_addr_cmp(&m->old_daddr, &m->new_daddr,
+			   m->old_family) == 0) &&
+	    (xfrm_addr_cmp(&m->old_saddr, &m->new_saddr,
+			   m->old_family) == 0))
+		return -1;
+
+	if (__xfrm_addr_any(&m->new_daddr, m->new_family) ||
+	    __xfrm_addr_any(&m->new_saddr, m->new_family))
+		return -1;
+
+        return 0;
+}
+
+static inline void xfrm_states_put(struct xfrm_state **states, int n)
+{
+	int i;
+	for (i=0; i<n; i++)
+		xfrm_state_put(*(states+i));
+}
+
+static inline void xfrm_states_delete(struct xfrm_state **states, int n)
+{
+	int i;
+	for (i=0; i<n; i++)
+		xfrm_state_delete(*(states+i));
+}
+#endif
 
 #endif	/* _NET_XFRM_H */
include/linux/xfrm.h +18 -0
index 9529ea1..914654a 100644
@@ -178,6 +178,9 @@ #define XFRM_MSG_GETAE XFRM_MSG_GETAE
 	XFRM_MSG_REPORT,
 #define XFRM_MSG_REPORT XFRM_MSG_REPORT
 
+	XFRM_MSG_MIGRATE,
+#define XFRM_MSG_MIGRATE XFRM_MSG_MIGRATE
+
 	__XFRM_MSG_MAX
 };
 #define XFRM_MSG_MAX (__XFRM_MSG_MAX - 1)
@@ -256,6 +259,7 @@ enum xfrm_attr_type_t {
 	XFRMA_COADDR,		/* xfrm_address_t */
 	XFRMA_LASTUSED,
 	XFRMA_POLICY_TYPE,	/* struct xfrm_userpolicy_type */
+	XFRMA_MIGRATE,
 	__XFRMA_MAX
 
 #define XFRMA_MAX (__XFRMA_MAX - 1)
@@ -351,6 +355,18 @@ struct xfrm_user_report {
 	struct xfrm_selector		sel;
 };
 
+struct xfrm_user_migrate {
+	__u8		old_family;
+	__u8		new_family;
+	__u8		proto;
+	__u8		mode;
+	__u32		reqid;
+	xfrm_address_t	old_daddr;
+	xfrm_address_t	old_saddr;
+	xfrm_address_t	new_daddr;
+	xfrm_address_t	new_saddr;
+};
+
 #ifndef __KERNEL__
 /* backwards compatibility for userspace */
 #define XFRMGRP_ACQUIRE		1
@@ -375,6 +391,8 @@ #define XFRMNLGRP_POLICY	XFRMNLGRP_POLIC
 #define XFRMNLGRP_AEVENTS	XFRMNLGRP_AEVENTS
 	XFRMNLGRP_REPORT,
 #define XFRMNLGRP_REPORT	XFRMNLGRP_REPORT
+	XFRMNLGRP_MIGRATE,
+#define XFRMNLGRP_MIGRATE	XFRMNLGRP_MIGRATE
 	__XFRMNLGRP_MAX
 };
 #define XFRMNLGRP_MAX	(__XFRMNLGRP_MAX - 1)
net/xfrm/xfrm_policy.c +210 -0

213 lines not on this page · raw message

net/xfrm/xfrm_state.c +191 -0

203 lines not on this page · raw message

#1 lore raw
Quoted 5 lines · Shinta Sugimoto
net/xfrm/xfrm_policy.c +5 -0
+int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
+		 struct xfrm_migrate *m, int num_migrate)
+{
+	int i, err, nx_cur = 0, nx_new = 0;
+	struct xfrm_policy *pol = NULL;

[...]

net/xfrm/xfrm_policy.c void __init xfrm_init(void) · line 2433

Quoted 3 lines
net/xfrm/xfrm_policy.c +3 -0
+donothing:
+	if (pol)
+		xfrm_pol_put(pol);

If you reach 'donothing', pol must be NULL, so you don't need to check for it before xfrm_pol_put().

net/xfrm/xfrm_state.c out: · line 840

Quoted 7 lines
net/xfrm/xfrm_state.c +7 -0
+#ifdef CONFIG_XFRM_MIGRATE
+static struct xfrm_algo *xfrm_algo_solidclone(struct xfrm_algo *orig)
+{
+	struct xfrm_algo *a;
+	a = kmalloc(sizeof(*orig) + orig->alg_key_len, GFP_KERNEL);
+	if (a)
+		memcpy(a, orig, sizeof(*orig) + orig->alg_key_len);

Use kmemdup() ?

net/xfrm/xfrm_state.c out: · line 893

Quoted 13 lines
net/xfrm/xfrm_state.c +13 -0
+        if (orig->encap) {
+		x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
+		if (!x->encap)
+			goto error;
+		memcpy(x->encap, orig->encap, sizeof(*x->encap));
+	}
+
+	if (orig->coaddr) {
+		x->coaddr = kmalloc(sizeof(*x->coaddr), GFP_KERNEL);
+		if (!x->coaddr)
+			goto error;
+		memcpy(x->coaddr, orig->coaddr, sizeof(*x->coaddr));
+	}

Ditto.

Quoted 1 line

+EXPORT_SYMBOL(xfrm_state_clone);

Why not EXPORT_SYMBOL_GPL ?
(for all of your exports).

- James

Signature · 2 lines
James Morris
<[email protected]>
#2 lore raw

Dear James,

Thank you for your comments. Please find my comments inline.

Quoted 11 lines · On Thu, 1 Feb 2007 09:45:18 -0500 (EST) James Morris <[email protected]> wrote:
Quoted 5 lines · Shinta Sugimoto
net/xfrm/xfrm_policy.c +5 -0
+int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
+		 struct xfrm_migrate *m, int num_migrate)
+{
+	int i, err, nx_cur = 0, nx_new = 0;
+	struct xfrm_policy *pol = NULL;

[...]

Quoted 6 lines

net/xfrm/xfrm_policy.c void __init xfrm_init(void) · line 2433

Quoted 3 lines
net/xfrm/xfrm_policy.c +3 -0
+donothing:
+	if (pol)
+		xfrm_pol_put(pol);

If you reach 'donothing', pol must be NULL, so you don't need to check for it before xfrm_pol_put().

Yes, you are right. I will delete unnecessary null-check and xfrm_pol_put().

Quoted 11 lines

net/xfrm/xfrm_state.c out: · line 840

Quoted 9 lines
net/xfrm/xfrm_state.c +7 -0
+#ifdef CONFIG_XFRM_MIGRATE
+static struct xfrm_algo *xfrm_algo_solidclone(struct xfrm_algo *orig)
+{
+	struct xfrm_algo *a;
+	a = kmalloc(sizeof(*orig) + orig->alg_key_len, GFP_KERNEL);
+	if (a)
+		memcpy(a, orig, sizeof(*orig) + orig->alg_key_len);

Use kmemdup() ?

I see. I will replace sets of kmalloc and memcpy with kmemdup().

Quoted 18 lines
Quoted 5 lines
net/xfrm/xfrm_state.c +3 -0
+        if (orig->encap) {
+		x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
+		if (!x->encap)

net/xfrm/xfrm_state.c out: · line 893

Quoted 10 lines
net/xfrm/xfrm_state.c +10 -0
+			goto error;
+		memcpy(x->encap, orig->encap, sizeof(*x->encap));
+	}
+
+	if (orig->coaddr) {
+		x->coaddr = kmalloc(sizeof(*x->coaddr), GFP_KERNEL);
+		if (!x->coaddr)
+			goto error;
+		memcpy(x->coaddr, orig->coaddr, sizeof(*x->coaddr));
+	}

Ditto.

Quoted 4 lines
Quoted 1 line

+EXPORT_SYMBOL(xfrm_state_clone);

Why not EXPORT_SYMBOL_GPL ?
(for all of your exports).

Actually I don't have specific preference on either EXPORT_SYMBOL or EXPORT_SYMBOL_GPL. It seemed to me that other functions defined in files under net/xfrm/ are using EXPORT_SYMBOL, so I followed that. Do you think EXPORT_SYMBOL_GPL is more appropriate in this case?

Regards,
Shinta

#3 lore raw
Quoted 9 lines · Shinta Sugimoto
Quoted 5 lines
Quoted 2 lines

+EXPORT_SYMBOL(xfrm_state_clone);

Why not EXPORT_SYMBOL_GPL ?
(for all of your exports).

Actually I don't have specific preference on either EXPORT_SYMBOL or EXPORT_SYMBOL_GPL. It seemed to me that other functions defined in files under net/xfrm/ are using EXPORT_SYMBOL, so I followed that.

Hmm, you're right, most of them are not _GPL.

Quoted 1 line

Do you think EXPORT_SYMBOL_GPL is more appropriate in this case?

Well, I would have thought that users of xfrm were generally derivative of the kernel, but it doesn't make much sense to have only a couple of symbols exported as GPL, so probably keep it the way you already have it.

- James

Signature · 2 lines
James Morris
<[email protected]>