Some functions of seg6local are very useful to process SRv6
encapsulated packets
This patch exports some functions of seg6local that are useful and
can be re-used at different parts of the kernel, including netfilter.
The set of exported functions are:
(1) seg6_get_srh()
(2) seg6_advance_nextseg()
(3) seg6_lookup_nexthop
Signed-off-by: Ahmed Abdelsalam <[email protected]>
---
include/net/seg6.h | 5 +++++
net/ipv6/seg6_local.c | 37 ++++++++++++++++++++-----------------
2 files changed, 25 insertions(+), 17 deletions(-)
diff --git a/include/net/seg6.h b/include/net/seg6.h
index 099bad5..b637778 100644
--- a/include/net/seg6.h
+++ b/include/net/seg6.h
@@ -63,5 +63,10 @@ extern bool seg6_validate_srh(struct ipv6_sr_hdr *srh, int len);
extern int seg6_do_srh_encap(struct sk_buff *skb, struct ipv6_sr_hdr *osrh,
int proto);
extern int seg6_do_srh_inline(struct sk_buff *skb, struct ipv6_sr_hdr *osrh);
+extern struct ipv6_sr_hdr *seg6_get_srh(struct sk_buff *skb);
+extern void seg6_advance_nextseg(struct ipv6_sr_hdr *srh,
+ struct in6_addr *daddr);
+extern void seg6_lookup_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr,
+ u32 tbl_id);
#endif
diff --git a/net/ipv6/seg6_local.c b/net/ipv6/seg6_local.c
index ba3767e..1f1eaa3 100644
--- a/net/ipv6/seg6_local.c
+++ b/net/ipv6/seg6_local.c
@@ -59,7 +59,7 @@ static struct seg6_local_lwt *seg6_local_lwtunnel(struct lwtunnel_state *lwt)
return (struct seg6_local_lwt *)lwt->data;
}
-static struct ipv6_sr_hdr *get_srh(struct sk_buff *skb)
+struct ipv6_sr_hdr *seg6_get_srh(struct sk_buff *skb)
{
struct ipv6_sr_hdr *srh;
int len, srhoff = 0;
@@ -82,12 +82,13 @@ static struct ipv6_sr_hdr *get_srh(struct sk_buff *skb)
return srh;
}
+EXPORT_SYMBOL_GPL(seg6_get_srh);
static struct ipv6_sr_hdr *get_and_validate_srh(struct sk_buff *skb)
{
struct ipv6_sr_hdr *srh;
- srh = get_srh(skb);
+ srh = seg6_get_srh(skb);
if (!srh)
return NULL;
@@ -107,7 +108,7 @@ static bool decap_and_validate(struct sk_buff *skb, int proto)
struct ipv6_sr_hdr *srh;
unsigned int off = 0;
- srh = get_srh(skb);
+ srh = seg6_get_srh(skb);
if (srh && srh->segments_left > 0)
return false;
@@ -131,7 +132,7 @@ static bool decap_and_validate(struct sk_buff *skb, int proto)
return true;
}
-static void advance_nextseg(struct ipv6_sr_hdr *srh, struct in6_addr *daddr)
+void seg6_advance_nextseg(struct ipv6_sr_hdr *srh, struct in6_addr *daddr)
{
struct in6_addr *addr;
@@ -139,9 +140,10 @@ static void advance_nextseg(struct ipv6_sr_hdr *srh, struct in6_addr *daddr)
addr = srh->segments + srh->segments_left;
*daddr = *addr;
}
+EXPORT_SYMBOL_GPL(seg6_advance_nextseg);
-static void lookup_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr,
- u32 tbl_id)
+void seg6_lookup_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr,
+ u32 tbl_id)
{
struct net *net = dev_net(skb->dev);
struct ipv6hdr *hdr = ipv6_hdr(skb);
@@ -188,6 +190,7 @@ static void lookup_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr,
skb_dst_drop(skb);
skb_dst_set(skb, dst);
}
+EXPORT_SYMBOL_GPL(seg6_lookup_nexthop);
/* regular endpoint function */
static int input_action_end(struct sk_buff *skb, struct seg6_local_lwt *slwt)
@@ -198,9 +201,9 @@ static int input_action_end(struct sk_buff *skb, struct seg6_local_lwt *slwt)
if (!srh)
goto drop;
- advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
+ seg6_advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
- lookup_nexthop(skb, NULL, 0);
+ seg6_lookup_nexthop(skb, NULL, 0);
return dst_input(skb);
@@ -218,9 +221,9 @@ static int input_action_end_x(struct sk_buff *skb, struct seg6_local_lwt *slwt)
if (!srh)
goto drop;
- advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
+ seg6_advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
- lookup_nexthop(skb, &slwt->nh6, 0);
+ seg6_lookup_nexthop(skb, &slwt->nh6, 0);
return dst_input(skb);
@@ -237,9 +240,9 @@ static int input_action_end_t(struct sk_buff *skb, struct seg6_local_lwt *slwt)
if (!srh)
goto drop;
- advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
+ seg6_advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
- lookup_nexthop(skb, NULL, slwt->table);
+ seg6_lookup_nexthop(skb, NULL, slwt->table);
return dst_input(skb);
@@ -331,7 +334,7 @@ static int input_action_end_dx6(struct sk_buff *skb,
if (!ipv6_addr_any(&slwt->nh6))
nhaddr = &slwt->nh6;
- lookup_nexthop(skb, nhaddr, 0);
+ seg6_lookup_nexthop(skb, nhaddr, 0);
return dst_input(skb);
drop:
@@ -380,7 +383,7 @@ static int input_action_end_dt6(struct sk_buff *skb,
if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
goto drop;
- lookup_nexthop(skb, NULL, slwt->table);
+ seg6_lookup_nexthop(skb, NULL, slwt->table);
return dst_input(skb);
@@ -406,7 +409,7 @@ static int input_action_end_b6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
skb_set_transport_header(skb, sizeof(struct ipv6hdr));
- lookup_nexthop(skb, NULL, 0);
+ seg6_lookup_nexthop(skb, NULL, 0);
return dst_input(skb);
@@ -426,7 +429,7 @@ static int input_action_end_b6_encap(struct sk_buff *skb,
if (!srh)
goto drop;
- advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
+ seg6_advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
skb_reset_inner_headers(skb);
skb->encapsulation = 1;
@@ -438,7 +441,7 @@ static int input_action_end_b6_encap(struct sk_buff *skb,
ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
skb_set_transport_header(skb, sizeof(struct ipv6hdr));
- lookup_nexthop(skb, NULL, 0);
+ seg6_lookup_nexthop(skb, NULL, 0);
return dst_input(skb);
--
2.1.4
Service Function Chaining (SFC) is one of the main use-cases of
IPv6 Segment Routing (SRv6)[1].
The Segment Routing Header (SRH) allows including a list of segments
in the IPv6 packet. This segment list can be used to steer the packet
though a set of Virtual Network Functions (VNFS) e.g., Firewall, DPI, IDS.
The edge node (SR ingress node) classifies the traffic and consequently
includes the segment lists in the IPv6 packet.
The IPv6 segment routing 'SEG6' target supports a set of SR-specific
actions. This set of actions allows branching within a pre-defined
SRv6 SFC policy instead of the usual linear exploration of the VNF chain.
As an example, consider a service chain which includes a firewall
followed by an Intrusion Detection System. The firewall may decide
to skip the IDS for a subset of traffic that matches some conditions.
This patch implements the following SR-specific actions:
(1) go-next: is the default action of the SEG6 target. It sends packets
towards the next SID from SRH.
(2) skip-next: skips the next SID in the SRH.
(3) go-last: skips the remaining part of the segment list and send the
packet towards the last segment.
[1] https://tools.ietf.org/html/draft-clad-spring-segment-routing-service-chaining-00
Signed-off-by: Ahmed Abdelsalam <[email protected]>
---
include/uapi/linux/netfilter_ipv6/ip6t_SEG6.h | 18 ++++
net/ipv6/netfilter/ip6t_SEG6.c | 113 ++++++++++++++++++++++++++
2 files changed, 131 insertions(+)
create mode 100644 include/uapi/linux/netfilter_ipv6/ip6t_SEG6.h
create mode 100644 net/ipv6/netfilter/ip6t_SEG6.c
diff --git a/include/uapi/linux/netfilter_ipv6/ip6t_SEG6.h b/include/uapi/linux/netfilter_ipv6/ip6t_SEG6.h
new file mode 100644
index 0000000..38b380a
--- /dev/null
+++ b/include/uapi/linux/netfilter_ipv6/ip6t_SEG6.h
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+#ifndef _IP6T_SEG6_H
+#define _IP6T_SEG6_H
+
+#include <linux/types.h>
+
+/* seg6 action options */
+enum ip6t_seg6_action {
+ IP6T_SEG6_GO_NEXT,
+ IP6T_SEG6_SKIP_NEXT,
+ IP6T_SEG6_GO_LAST,
+};
+
+struct ip6t_seg6_info {
+ __u32 action; /* SEG6 action */
+};
+
+#endif /*_IP6T_SEG6_H*/
diff --git a/net/ipv6/netfilter/ip6t_SEG6.c b/net/ipv6/netfilter/ip6t_SEG6.c
new file mode 100644
index 0000000..3d326e2
--- /dev/null
+++ b/net/ipv6/netfilter/ip6t_SEG6.c
@@ -0,0 +1,113 @@
+/* IPv6 Segment Routing target module (SEG6).
+ *
+ * Author:
+ * Ahmed Abdelsalam <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/gfp.h>
+#include <linux/module.h>
+#include <linux/skbuff.h>
+#include <linux/icmpv6.h>
+#include <linux/netdevice.h>
+#include <linux/netfilter/x_tables.h>
+#include <linux/netfilter_ipv6/ip6_tables.h>
+#include <linux/netfilter_ipv6/ip6t_SEG6.h>
+
+#include <net/flow.h>
+#include <net/seg6.h>
+#include <net/ip6_route.h>
+
+static int seg6_go_next(struct sk_buff *skb, struct ipv6_sr_hdr *srh)
+{
+ if (srh->segments_left == 0)
+ return NF_DROP;
+ seg6_advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
+ seg6_lookup_nexthop(skb, NULL, 0);
+ dst_input(skb);
+ return NF_STOLEN;
+}
+
+static int seg6_skip_next(struct sk_buff *skb, struct ipv6_sr_hdr *srh)
+{
+ if (srh->segments_left < 2)
+ return NF_DROP;
+ seg6_advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
+ seg6_advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
+ seg6_lookup_nexthop(skb, NULL, 0);
+ dst_input(skb);
+ return NF_STOLEN;
+}
+
+static int seg6_go_last(struct sk_buff *skb, struct ipv6_sr_hdr *srh)
+{
+ if (srh->segments_left == 0)
+ return NF_DROP;
+ srh->segments_left = 1;
+ seg6_advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
+ seg6_lookup_nexthop(skb, NULL, 0);
+ dst_input(skb);
+ return NF_STOLEN;
+}
+
+static unsigned int
+seg6_tg6(struct sk_buff *skb, const struct xt_action_param *par)
+{
+ struct ipv6_sr_hdr *srh;
+ const struct ip6t_seg6_info *seg6 = par->targinfo;
+
+ srh = seg6_get_srh(skb);
+ if (!srh)
+ return NF_DROP;
+
+ switch (seg6->action) {
+ case IP6T_SEG6_GO_NEXT:
+ return seg6_go_next(skb, srh);
+ case IP6T_SEG6_SKIP_NEXT:
+ return seg6_skip_next(skb, srh);
+ case IP6T_SEG6_GO_LAST:
+ return seg6_go_last(skb, srh);
+ }
+ pr_err("Unknown SEG6 action\n");
+ return NF_DROP;
+}
+
+static int seg6_check(const struct xt_tgchk_param *par)
+{
+ /**
+ * In the future, some new action may require using this function
+ */
+ return 0;
+}
+
+static struct xt_target seg6_tg6_reg __read_mostly = {
+ .name = "SEG6",
+ .family = NFPROTO_IPV6,
+ .target = seg6_tg6,
+ .targetsize = sizeof(struct ip6t_seg6_info),
+ .checkentry = seg6_check,
+ .me = THIS_MODULE
+};
+
+static int __init seg6_tg6_init(void)
+{
+ return xt_register_target(&seg6_tg6_reg);
+}
+
+static void __exit seg6_tg6_exit(void)
+{
+ xt_unregister_target(&seg6_tg6_reg);
+}
+
+module_init(seg6_tg6_init);
+module_exit(seg6_tg6_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Xtables: IPv6 Segment Routing Target (SEG6)");
+MODULE_AUTHOR("Ahmed Abdelsalam <[email protected]>");
--
2.1.4
This patch adds a new option CONFIG_IP6_NF_TARGET_SEG6 to
enable/disable support of iptables SEG6 target.
Signed-off-by: Ahmed Abdelsalam <[email protected]>
---
net/ipv6/netfilter/Kconfig | 12 ++++++++++++
net/ipv6/netfilter/Makefile | 1 +
2 files changed, 13 insertions(+)
diff --git a/net/ipv6/netfilter/Kconfig b/net/ipv6/netfilter/Kconfig
index 4a634b7..8e98afc 100644
--- a/net/ipv6/netfilter/Kconfig
+++ b/net/ipv6/netfilter/Kconfig
@@ -364,6 +364,18 @@ config IP6_NF_TARGET_NPT
endif # IP6_NF_NAT
+if IPV6_SEG6_LWTUNNEL
+config IP6_NF_TARGET_SEG6
+ tristate 'IPv6 Segment Routing "SEG6" target support'
+ depends on NETFILTER_ADVANCED
+ help
+ SEG6 is an special target for IPv6 Segment Routing encapsualted
+ packets. It supports a set of Segment Routing specific actions
+ that are based on SRH information. It is useful for SRv6 Service
+ Function chaining use-cases.
+
+ To compile it as a module, choose M here. If unsure, say N.
+endif # IPV6_SEG6_LWTUNNEL
endif # IP6_NF_IPTABLES
endmenu
diff --git a/net/ipv6/netfilter/Makefile b/net/ipv6/netfilter/Makefile
index d984057..67eff84 100644
--- a/net/ipv6/netfilter/Makefile
+++ b/net/ipv6/netfilter/Makefile
@@ -64,3 +64,4 @@ obj-$(CONFIG_IP6_NF_TARGET_MASQUERADE) += ip6t_MASQUERADE.o
obj-$(CONFIG_IP6_NF_TARGET_NPT) += ip6t_NPT.o
obj-$(CONFIG_IP6_NF_TARGET_REJECT) += ip6t_REJECT.o
obj-$(CONFIG_IP6_NF_TARGET_SYNPROXY) += ip6t_SYNPROXY.o
+obj-$(CONFIG_IP6_NF_TARGET_SEG6) += ip6t_SEG6.o
--
2.1.4