11use anyhow:: { anyhow, Result } ;
2- use etherparse:: { IpNumber , Ipv4Header , Ipv6Header , TcpHeader , TransportHeader } ;
3- use nix:: sys:: socket:: { sendto, MsgFlags , SockaddrIn , SockaddrIn6 , SockaddrLike } ;
2+ use etherparse:: { IpNumber , Ipv4Header , TcpHeader , TransportHeader } ;
43
5- use std:: { mem , sync:: Arc , thread:: JoinHandle , time:: Duration } ;
4+ use std:: { sync:: Arc , thread:: JoinHandle , time:: Duration } ;
65
76use crate :: {
87 ebpf_prog:: {
98 self ,
109 types:: { chlo_tls_atype, sni_action, tls_sni_signaling} ,
1110 } ,
12- utils:: { NetworkHeader , TransportHeaderParse } ,
11+ utils:: { NetworkHeader , TransportHeaderParse , RawSocket } ,
1312} ;
1413use libbpf_rs:: PerfBufferBuilder ;
1514use plain:: Plain ;
@@ -48,8 +47,7 @@ fn sni_tls_get_domain(sni_tls_data: &tls_sni_signaling) -> Result<Option<String>
4847}
4948
5049struct PerfProcessContext {
51- ip_rawsocket : RawSocket ,
52- ip6_rawsocket : RawSocket ,
50+ rawsocket : RawSocket ,
5351}
5452
5553fn process_perf_signal (
@@ -108,7 +106,7 @@ fn send_bi_tcp_rst(
108106 let mut sent_packet = iph. to_bytes ( ) . to_vec ( ) ;
109107 sent_packet. extend ( tcph. to_bytes ( ) ) ;
110108
111- ctx. ip_rawsocket . send_ipv4 ( & iph, & sent_packet[ ..] ) ?;
109+ ctx. rawsocket . send_ipv4 ( & iph, & sent_packet[ ..] ) ?;
112110
113111 iph. destination = iph. source ;
114112 iph. source = oiph. destination ;
@@ -125,7 +123,7 @@ fn send_bi_tcp_rst(
125123 let mut sent_packet = iph. to_bytes ( ) . to_vec ( ) ;
126124 sent_packet. extend ( tcph. to_bytes ( ) ) ;
127125
128- ctx. ip_rawsocket . send_ipv4 ( & iph, & sent_packet[ ..] ) ?;
126+ ctx. rawsocket . send_ipv4 ( & iph, & sent_packet[ ..] ) ?;
129127 } else if let NetworkHeader :: Ipv6 ( oiph) = network_hdr {
130128 let mut iph = oiph. clone ( ) ;
131129 iph. hop_limit = 128 ;
@@ -137,7 +135,7 @@ fn send_bi_tcp_rst(
137135 let mut sent_packet = iph. to_bytes ( ) . to_vec ( ) ;
138136 sent_packet. extend ( tcph. to_bytes ( ) ) ;
139137
140- ctx. ip6_rawsocket . send_ipv6 ( & iph, & sent_packet[ ..] ) ?;
138+ ctx. rawsocket . send_ipv6 ( & iph, & sent_packet[ ..] ) ?;
141139
142140 iph. destination = iph. source ;
143141 iph. source = oiph. destination ;
@@ -153,115 +151,15 @@ fn send_bi_tcp_rst(
153151 let mut sent_packet = iph. to_bytes ( ) . to_vec ( ) ;
154152 sent_packet. extend ( tcph. to_bytes ( ) ) ;
155153
156- ctx. ip6_rawsocket . send_ipv6 ( & iph, & sent_packet[ ..] ) ?;
154+ ctx. rawsocket . send_ipv6 ( & iph, & sent_packet[ ..] ) ?;
157155 }
158156
159157 Ok ( ( ) )
160158}
161159
162- #[ derive( Copy , Clone , PartialEq ) ]
163- enum RawSocketType {
164- AfInet = libc:: AF_INET as isize ,
165- AfInet6 = libc:: AF_INET6 as isize ,
166- }
167- struct RawSocket {
168- fd : libc:: c_int ,
169- domain_type : RawSocketType ,
170- }
171-
172- impl RawSocket {
173- pub fn new ( skel : & ebpf_prog:: DpitSkel , domain : RawSocketType ) -> Result < Self > {
174- let rawsocket =
175- unsafe { libc:: socket ( domain as libc:: c_int , libc:: SOCK_RAW , libc:: IPPROTO_RAW ) } ;
176-
177- if rawsocket == -1 {
178- return Err ( anyhow ! ( "Open rawsocket" ) ) ;
179- }
180- let mark: libc:: c_int = skel. maps . rodata_data . unwrap ( ) . RAWSOCKET_MARK as libc:: c_int ;
181- let ret = unsafe {
182- libc:: setsockopt (
183- rawsocket,
184- libc:: SOL_SOCKET ,
185- libc:: SO_MARK ,
186- ( & mark) as * const libc:: c_int as * const libc:: c_void ,
187- mem:: size_of_val ( & mark) as libc:: socklen_t ,
188- )
189- } ;
190- if ret < 0 {
191- unsafe { libc:: close ( rawsocket) } ;
192- return Err ( anyhow ! ( "setsockopt(SO_MARK, {}) failed" , mark) ) ;
193- }
194-
195- Ok ( Self {
196- fd : rawsocket,
197- domain_type : domain,
198- } )
199- }
200-
201- pub fn fd ( & self ) -> i32 {
202- self . fd
203- }
204-
205- pub fn send_ipv4 ( & self , iph : & Ipv4Header , pkt : & [ u8 ] ) -> Result < ( ) > {
206- if RawSocketType :: AfInet != self . domain_type {
207- unreachable ! ( ) ;
208- }
209-
210- let daddr = SockaddrIn :: new (
211- iph. destination [ 0 ] ,
212- iph. destination [ 1 ] ,
213- iph. destination [ 2 ] ,
214- iph. destination [ 3 ] ,
215- 0 ,
216- ) ;
217-
218- let t = sendto ( self . fd ( ) , pkt, & daddr, MsgFlags :: MSG_DONTWAIT ) ?;
219- println ! ( "Sent {t} bytes" ) ;
220-
221- Ok ( ( ) )
222- }
223-
224- pub fn send_ipv6 ( & self , ip6h : & Ipv6Header , pkt : & [ u8 ] ) -> Result < ( ) > {
225- if RawSocketType :: AfInet6 != self . domain_type {
226- unreachable ! ( ) ;
227- }
228-
229- let daddr = libc:: sockaddr_in6 {
230- sin6_family : libc:: AF_INET6 as u16 ,
231- /* Always 0 for raw socket */
232- sin6_port : 0 ,
233- sin6_addr : libc:: in6_addr {
234- s6_addr : ip6h. destination ,
235- } ,
236- sin6_flowinfo : unsafe { mem:: zeroed ( ) } ,
237- sin6_scope_id : unsafe { mem:: zeroed ( ) } ,
238- } ;
239-
240- let daddr = unsafe {
241- SockaddrIn6 :: from_raw (
242- & daddr as * const libc:: sockaddr_in6 as * const libc:: sockaddr ,
243- None ,
244- )
245- }
246- . ok_or ( anyhow ! ( "SockaddrIn6 from_raw" ) ) ?;
247-
248- sendto ( self . fd ( ) , pkt, & daddr, MsgFlags :: MSG_DONTWAIT ) ?;
249-
250- Ok ( ( ) )
251- }
252- }
253-
254- impl Drop for RawSocket {
255- fn drop ( & mut self ) {
256- unsafe { libc:: close ( self . fd ) } ;
257- self . fd = 0 ;
258- }
259- }
260-
261160pub fn init_sni_logging ( skel : & ebpf_prog:: DpitSkel ) -> Result < JoinHandle < ( ) > > {
262- let ip_rawsocket = RawSocket :: new ( skel, RawSocketType :: AfInet ) ?;
263- let ip6_rawsocket = RawSocket :: new ( skel, RawSocketType :: AfInet6 ) ?;
264- let perf_ctx = Arc :: new ( PerfProcessContext { ip_rawsocket, ip6_rawsocket } ) ;
161+ let rawsocket = RawSocket :: new ( skel. maps . rodata_data . unwrap ( ) . RAWSOCKET_MARK as u32 ) ?;
162+ let perf_ctx = Arc :: new ( PerfProcessContext { rawsocket } ) ;
265163
266164 let handle_perf_event = move |_cpu : i32 , data : & [ u8 ] | {
267165 let mut sni_tls_data = tls_sni_signaling:: default ( ) ;
0 commit comments