@@ -71,6 +71,14 @@ struct bpf_map_def SEC("maps") sock_cork_bytes = {
7171 .max_entries = 1
7272};
7373
74+ struct bpf_map_def SEC ("maps" ) sock_pull_bytes = {
75+ .type = BPF_MAP_TYPE_ARRAY ,
76+ .key_size = sizeof (int ),
77+ .value_size = sizeof (int ),
78+ .max_entries = 2
79+ };
80+
81+
7482SEC ("sk_skb1" )
7583int bpf_prog1 (struct __sk_buff * skb )
7684{
@@ -137,67 +145,104 @@ int bpf_sockmap(struct bpf_sock_ops *skops)
137145SEC ("sk_msg1" )
138146int bpf_prog4 (struct sk_msg_md * msg )
139147{
140- int * bytes , zero = 0 ;
148+ int * bytes , zero = 0 , one = 1 ;
149+ int * start , * end ;
141150
142151 bytes = bpf_map_lookup_elem (& sock_apply_bytes , & zero );
143152 if (bytes )
144153 bpf_msg_apply_bytes (msg , * bytes );
145154 bytes = bpf_map_lookup_elem (& sock_cork_bytes , & zero );
146155 if (bytes )
147156 bpf_msg_cork_bytes (msg , * bytes );
157+ start = bpf_map_lookup_elem (& sock_pull_bytes , & zero );
158+ end = bpf_map_lookup_elem (& sock_pull_bytes , & one );
159+ if (start && end )
160+ bpf_msg_pull_data (msg , * start , * end , 0 );
148161 return SK_PASS ;
149162}
150163
151164SEC ("sk_msg2" )
152165int bpf_prog5 (struct sk_msg_md * msg )
153166{
154- void * data_end = (void * )(long ) msg -> data_end ;
155- void * data = (void * )(long ) msg -> data ;
156- int * bytes , err1 = -1 , err2 = -1 , zero = 0 ;
167+ int err1 = -1 , err2 = -1 , zero = 0 , one = 1 ;
168+ int * bytes , * start , * end , len1 , len2 ;
157169
158170 bytes = bpf_map_lookup_elem (& sock_apply_bytes , & zero );
159171 if (bytes )
160172 err1 = bpf_msg_apply_bytes (msg , * bytes );
161173 bytes = bpf_map_lookup_elem (& sock_cork_bytes , & zero );
162174 if (bytes )
163175 err2 = bpf_msg_cork_bytes (msg , * bytes );
176+ len1 = (__u64 )msg -> data_end - (__u64 )msg -> data ;
177+ start = bpf_map_lookup_elem (& sock_pull_bytes , & zero );
178+ end = bpf_map_lookup_elem (& sock_pull_bytes , & one );
179+ if (start && end ) {
180+ int err ;
181+
182+ bpf_printk ("sk_msg2: pull(%i:%i)\n" ,
183+ start ? * start : 0 , end ? * end : 0 );
184+ err = bpf_msg_pull_data (msg , * start , * end , 0 );
185+ if (err )
186+ bpf_printk ("sk_msg2: pull_data err %i\n" ,
187+ err );
188+ len2 = (__u64 )msg -> data_end - (__u64 )msg -> data ;
189+ bpf_printk ("sk_msg2: length update %i->%i\n" ,
190+ len1 , len2 );
191+ }
164192 bpf_printk ("sk_msg2: data length %i err1 %i err2 %i\n" ,
165- ( __u64 ) data_end - ( __u64 ) data , err1 , err2 );
193+ len1 , err1 , err2 );
166194 return SK_PASS ;
167195}
168196
169197SEC ("sk_msg3" )
170198int bpf_prog6 (struct sk_msg_md * msg )
171199{
172- void * data_end = (void * )(long ) msg -> data_end ;
173- void * data = (void * )(long ) msg -> data ;
174- int * bytes , zero = 0 ;
200+ int * bytes , zero = 0 , one = 1 ;
201+ int * start , * end ;
175202
176203 bytes = bpf_map_lookup_elem (& sock_apply_bytes , & zero );
177204 if (bytes )
178205 bpf_msg_apply_bytes (msg , * bytes );
179206 bytes = bpf_map_lookup_elem (& sock_cork_bytes , & zero );
180207 if (bytes )
181208 bpf_msg_cork_bytes (msg , * bytes );
209+ start = bpf_map_lookup_elem (& sock_pull_bytes , & zero );
210+ end = bpf_map_lookup_elem (& sock_pull_bytes , & one );
211+ if (start && end )
212+ bpf_msg_pull_data (msg , * start , * end , 0 );
182213 return bpf_msg_redirect_map (msg , & sock_map_redir , zero , 0 );
183214}
184215
185216SEC ("sk_msg4" )
186217int bpf_prog7 (struct sk_msg_md * msg )
187218{
188- void * data_end = (void * )(long ) msg -> data_end ;
189- void * data = (void * )(long ) msg -> data ;
190- int * bytes , err1 = 0 , err2 = 0 , zero = 0 ;
219+ int err1 = 0 , err2 = 0 , zero = 0 , one = 1 ;
220+ int * bytes , * start , * end , len1 , len2 ;
191221
192222 bytes = bpf_map_lookup_elem (& sock_apply_bytes , & zero );
193223 if (bytes )
194224 err1 = bpf_msg_apply_bytes (msg , * bytes );
195225 bytes = bpf_map_lookup_elem (& sock_cork_bytes , & zero );
196226 if (bytes )
197227 err2 = bpf_msg_cork_bytes (msg , * bytes );
198-
228+ len1 = (__u64 )msg -> data_end - (__u64 )msg -> data ;
229+ start = bpf_map_lookup_elem (& sock_pull_bytes , & zero );
230+ end = bpf_map_lookup_elem (& sock_pull_bytes , & one );
231+ if (start && end ) {
232+ int err ;
233+
234+ bpf_printk ("sk_msg2: pull(%i:%i)\n" ,
235+ start ? * start : 0 , end ? * end : 0 );
236+ err = bpf_msg_pull_data (msg , * start , * end , 0 );
237+ if (err )
238+ bpf_printk ("sk_msg2: pull_data err %i\n" ,
239+ err );
240+ len2 = (__u64 )msg -> data_end - (__u64 )msg -> data ;
241+ bpf_printk ("sk_msg2: length update %i->%i\n" ,
242+ len1 , len2 );
243+ }
199244 bpf_printk ("sk_msg3: redirect(%iB) err1=%i err2=%i\n" ,
200- ( __u64 ) data_end - ( __u64 ) data , err1 , err2 );
245+ len1 , err1 , err2 );
201246 return bpf_msg_redirect_map (msg , & sock_map_redir , zero , 0 );
202247}
203248
@@ -239,14 +284,20 @@ int bpf_prog9(struct sk_msg_md *msg)
239284SEC ("sk_msg7" )
240285int bpf_prog10 (struct sk_msg_md * msg )
241286{
242- int * bytes , zero = 0 ;
287+ int * bytes , zero = 0 , one = 1 ;
288+ int * start , * end ;
243289
244290 bytes = bpf_map_lookup_elem (& sock_apply_bytes , & zero );
245291 if (bytes )
246292 bpf_msg_apply_bytes (msg , * bytes );
247293 bytes = bpf_map_lookup_elem (& sock_cork_bytes , & zero );
248294 if (bytes )
249295 bpf_msg_cork_bytes (msg , * bytes );
296+ start = bpf_map_lookup_elem (& sock_pull_bytes , & zero );
297+ end = bpf_map_lookup_elem (& sock_pull_bytes , & one );
298+ if (start && end )
299+ bpf_msg_pull_data (msg , * start , * end , 0 );
300+
250301 return SK_DROP ;
251302}
252303
0 commit comments