Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for TUN-based Channels (kqueue/epoll) #12960

Open
wants to merge 11 commits into
base: 4.1
Choose a base branch
from
73 changes: 73 additions & 0 deletions example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,19 @@
<groupId>com.sun.activation</groupId>
<artifactId>javax.activation</artifactId>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>netty-transport-classes-epoll</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>netty-transport-classes-kqueue</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -198,5 +211,65 @@
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>native-dependencies</id>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>netty-transport-native-epoll</artifactId>
<version>${project.version}</version>
<classifier>linux-x86_64</classifier>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>netty-transport-native-epoll</artifactId>
<version>${project.version}</version>
<classifier>linux-aarch_64</classifier>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>netty-transport-native-kqueue</artifactId>
<version>${project.version}</version>
<classifier>osx-x86_64</classifier>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>netty-transport-native-kqueue</artifactId>
<version>${project.version}</version>
<classifier>osx-aarch_64</classifier>
<scope>runtime</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>linux</id>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>netty-transport-native-epoll</artifactId>
<version>${project.version}</version>
<classifier>${jni.classifier}</classifier>
<scope>runtime</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>mac</id>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>netty-transport-native-kqueue</artifactId>
<version>${project.version}</version>
<classifier>${jni.classifier}</classifier>
<scope>runtime</scope>
</dependency>
</dependencies>
</profile>
</profiles>
</project>

54 changes: 54 additions & 0 deletions example/src/main/java/io/netty/example/tun/Echo4Handler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2022 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.example.tun;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.Tun4Packet;
import io.netty.channel.socket.TunPacket;

import static io.netty.channel.socket.Tun4Packet.INET4_DESTINATION_ADDRESS;
import static io.netty.channel.socket.Tun4Packet.INET4_SOURCE_ADDRESS;

/**
* Echoes received IPv4 packets by swapping source and destination addresses.
*/
@Sharable
public class Echo4Handler extends SimpleChannelInboundHandler<Tun4Packet> {
@Override
protected void channelRead0(ChannelHandlerContext ctx,
Tun4Packet packet) throws Exception {
// swap source and destination addresses. Depending on the layer 4 protocol used, this may
// require recalculation of existing checksums. However, UDP and TCP work without
// recalculation.
ByteBuf buf = packet.content();
int sourceAddress = buf.getInt(INET4_SOURCE_ADDRESS);
int destinationAddress = buf.getInt(INET4_DESTINATION_ADDRESS);
buf.setInt(INET4_SOURCE_ADDRESS, destinationAddress);
buf.setInt(INET4_DESTINATION_ADDRESS, sourceAddress);

TunPacket response = new Tun4Packet(buf.retain());
ctx.write(response);
}

@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.fireChannelReadComplete();
ctx.flush();
}
}
56 changes: 56 additions & 0 deletions example/src/main/java/io/netty/example/tun/Echo6Handler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2022 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.example.tun;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.Tun6Packet;
import io.netty.channel.socket.TunPacket;

import static io.netty.channel.socket.Tun6Packet.INET6_DESTINATION_ADDRESS;
import static io.netty.channel.socket.Tun6Packet.INET6_SOURCE_ADDRESS;

/**
* Echoes received IPv6 packets by swapping source and destination addresses.
*/
@Sharable
public class Echo6Handler extends SimpleChannelInboundHandler<Tun6Packet> {
@Override
protected void channelRead0(ChannelHandlerContext ctx,
Tun6Packet packet) throws Exception {
// swap source and destination addresses. Depending on the layer 4 protocol used, this may
HeikoBornholdt marked this conversation as resolved.
Show resolved Hide resolved
// require recalculation of existing checksums. However, UDP and TCP work without
// recalculation.
ByteBuf buf = packet.content();
byte[] sourceAddress = new byte[16];
buf.getBytes(INET6_SOURCE_ADDRESS, sourceAddress, 0, 16);
byte[] destinationAddress = new byte[16];
buf.getBytes(INET6_DESTINATION_ADDRESS, destinationAddress, 0, 16);
buf.setBytes(INET6_SOURCE_ADDRESS, destinationAddress);
buf.setBytes(INET6_DESTINATION_ADDRESS, sourceAddress);

TunPacket response = new Tun6Packet(buf.retain());
ctx.write(response);
}

@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.fireChannelReadComplete();
ctx.flush();
}
}
65 changes: 65 additions & 0 deletions example/src/main/java/io/netty/example/tun/Ping4Handler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2022 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.example.tun;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.Tun4Packet;
import io.netty.channel.socket.TunPacket;

import java.net.InetAddress;

import static io.netty.channel.socket.Tun4Packet.INET4_DESTINATION_ADDRESS;
import static io.netty.channel.socket.Tun4Packet.INET4_SOURCE_ADDRESS;

/**
* Replies to ICMP echo ping requests.
*/
@Sharable
public class Ping4Handler extends SimpleChannelInboundHandler<Tun4Packet> {
// https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml
public static final int PROTOCOL = 1;
// https://datatracker.ietf.org/doc/html/rfc792
public static final int TYPE = 20;
public static final int CHECKSUM = 22;
public static final int ECHO = 8;
public static final int ECHO_REPLY = 0;

@Override
protected void channelRead0(ChannelHandlerContext ctx,
Tun4Packet packet) {
if (packet.protocol() == PROTOCOL) {
short icmpType = packet.content().getUnsignedByte(TYPE);
if (icmpType == ECHO) {
InetAddress source = packet.sourceAddress();
InetAddress destination = packet.destinationAddress();
int checksum = packet.content().getUnsignedShort(CHECKSUM);

// create response
ByteBuf buf = packet.content();
buf.setBytes(INET4_SOURCE_ADDRESS, destination.getAddress());
buf.setBytes(INET4_DESTINATION_ADDRESS, source.getAddress());
buf.setByte(TYPE, ECHO_REPLY);
buf.setShort(CHECKSUM, (checksum + 0x0800) % 0xffff);

TunPacket response = new Tun4Packet(buf.retain());
ctx.writeAndFlush(response);
}
}
}
}
67 changes: 67 additions & 0 deletions example/src/main/java/io/netty/example/tun/Ping6Handler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2022 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.example.tun;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.Tun6Packet;
import io.netty.channel.socket.TunPacket;

import java.net.InetAddress;

import static io.netty.channel.socket.Tun6Packet.INET6_DESTINATION_ADDRESS;
import static io.netty.channel.socket.Tun6Packet.INET6_SOURCE_ADDRESS;

/**
* Replies to IPv6-ICMP echo ping requests.
*/
@Sharable
public class Ping6Handler extends SimpleChannelInboundHandler<Tun6Packet> {
// https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml
public static final int PROTOCOL = 58;
// https://datatracker.ietf.org/doc/html/rfc8200
public static final int NEXT_HEADER = 6;
public static final int TYPE = 40;
public static final int CHECKSUM = 42;
public static final int ECHO = 128;
public static final int ECHO_REPLY = 129;

@Override
protected void channelRead0(ChannelHandlerContext ctx,
Tun6Packet packet) {
int nextHeader = packet.content().getUnsignedByte(NEXT_HEADER);
if (nextHeader == PROTOCOL) {
short icmpType = packet.content().getUnsignedByte(TYPE);
if (icmpType == ECHO) {
InetAddress source = packet.sourceAddress();
InetAddress destination = packet.destinationAddress();
int checksum = packet.content().getUnsignedShort(CHECKSUM);

// create response
ByteBuf buf = packet.content();
buf.setBytes(INET6_SOURCE_ADDRESS, destination.getAddress());
buf.setBytes(INET6_DESTINATION_ADDRESS, source.getAddress());
buf.setByte(TYPE, ECHO_REPLY);
buf.setShort(CHECKSUM, checksum - 0x100);

TunPacket response = new Tun6Packet(buf.retain());
ctx.writeAndFlush(response);
}
}
}
}