summaryrefslogtreecommitdiff
path: root/staging/layer1.c
blob: f671bb5064a092111211ef7d9ad536e214d718d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
 * Mausezahn - A fast versatile traffic generator
 * Copyright (C) 2008 Herbert Haas
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 2 as published by the 
 * Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with 
 * this program; if not, see http://www.gnu.org/licenses/gpl-2.0.html
 * 
*/



// ****************************************************************************
// 
//     This section contains functions to send an arbitrary byte stream out of 
//     the network card. Currently it works perfect for Ethernet cards.
//     
//     TODO: Access to the 802.11 header 
// 
// ****************************************************************************

#include "mz.h"
#include "cli.h"

int send_eth()
{
   // Tasks:
   // 1. Check 'eth_src_txt' and 'eth_dst_txt' which contain either a MAC address or a keyword
   //    'eth_dst' can be set without having 'eth_src_txt' specified (the next 6 bytes of the
   //    'arg_string' will be used). But if 'eth_src_txt' is given then also 'eth_dst_txt' 
   //    should have been specified, otherwise a default (ff-ff-ff-ff-ff-ff) will be used.
   // 2. Check whether 'arg_string' contains a hex-string. If YES then convert it into an
   //    'eth_payload' and extract eth_type.
   // 3. Apply 'padding' if specified
   // 4. Check if frame has at least minimum length (14 Bytes).
   // 5. Send frame 'count' times and
   // 6. Apply 'delay' (make precautions for better delay functions)
   
   int 
     src,                 // flag telling whether user has specified a source address
     dst,                 // flag telling whether user has specified a destination address
     src_random=0, 
     dst_random=0, 
     byte_ptr=1,
     bytestring_s=0,
     min_size=15,
     pad=0,
     repeat, loop, update,
     i=0,
     j=0;
   char 
     err_buf[LIBNET_ERRBUF_SIZE],
     message[MAX_PAYLOAD_SIZE*3];
     
   u_int8_t       bytestring[MAX_PAYLOAD_SIZE];
   libnet_ptag_t  t;
   libnet_t       *l;

   
	
   if (tx.dot1Q)
     {
	fprintf(stderr," Note: raw layer 2 mode does not support 802.1Q builder.\n"
		       " If you want to create VLAN tags then you must do it by hand.\n");
	exit(1);
     }
   
   if (tx.mpls)
     {
	fprintf(stderr," Note: raw layer 2 mode does not support MPLS builder.\n"
		       " If you want to create MPLS labels then you must do it by hand.\n");
	exit(1);
     }
   
	

   
   // So other functions can use this function for sending Ethernet frames
   // These other functions must set dst, src, type and payload!
   if (tx.eth_params_already_set) goto ALL_SPECIFIED;
   
   
   if ((tx.padding) && (tx.padding<15)) // Note: ignored if padding==0
     { 
	     tx.padding=15;
	     if (mz_port)    {
		     cli_print(gcli, "Note: Set padding to 15 bytes (total length)\n");
	     } else
		     fprintf(stderr, " mz/send_eth: [Note] adjusted minimum frame size to 15 bytes.\n");
     }
   
   // Create a temporal, local bytestring:
   //
   for (i=0; i<MAX_PAYLOAD_SIZE; i++) bytestring[i]=0x00;
   bytestring_s = str2hex (tx.arg_string, bytestring, MAX_PAYLOAD_SIZE);
   
   // Set the flags to shorten subsequent decisions:
   src = strlen(tx.eth_src_txt);
   dst = strlen(tx.eth_dst_txt);
   
   
   // IN ANY CASE if src has been specified:
   // 
   if (src) 
     {
	// Evaluate Ethernet CLI options (-a and -b)
	if (check_eth_mac_txt(ETH_SRC))  // if true then problem!
	  {
	     // use own (already set in init.c)
	  }
	src_random = tx.eth_src_rand; // local vars are faster
     }

   // IN ANY CASE if dst has been specified:
   // 
   if (dst) 
     {
	// Evaluate Ethernet CLI options (-a and -b)
	if (check_eth_mac_txt(ETH_DST))  // if true then problem!
	  {
	     str2hex("ff:ff:ff:ff:ff:ff",tx.eth_dst, 6); // the default 
	  }
	dst_random = tx.eth_dst_rand; // local vars are faster
     }

   
   // Catch errors with too short bytestrings:
   // 
   if (src)
     {
	// bytestring only needs to contain eth_type
	min_size-=12; 
     }
   else if (dst)
     {
	// bytstring must contain src and type
	min_size-=6;
     }
   if (bytestring_s < min_size)
     {
	     j = min_size - bytestring_s; // this is what's missing
	     bytestring_s += j; // note that bytestring has been filled up with 0x00, so we can do that
     }

	
   // ADDENDUM: If src specified, dst missing:
   // 
   if ( (!dst) && (src) ) 
     {
	str2hex_mac ("ff:ff:ff:ff:ff:ff", tx.eth_dst);
     }
   
   
   
   // ADDENDUM: If dst specified, src missing:
   // 
   if ((dst) && (!src))
     {
	// Get eth_src from bytestring:
	if (bytestring_s>=6) {
		(void) getbytes (bytestring, tx.eth_src, byte_ptr, byte_ptr+5);
		byte_ptr=7; // now points to eth_type within bytestring
	}
     }
   
   // FINALLY: If both dst and src have NOT been specified:
   // 
   if ((!dst) && (!src))
     {
	     if (bytestring_s>=6) {
		     (void) getbytes (bytestring, tx.eth_dst, byte_ptr, byte_ptr+5);
		     byte_ptr=7;
	     }

	     if (bytestring_s>=12) {
		     (void) getbytes (bytestring, tx.eth_src, byte_ptr, byte_ptr+5);
		     byte_ptr=13; // points to eth_type
	     }
     }
   
   // Set eth_type:
   // 
   if (bytestring_s>=2) {
	   tx.eth_type = 256 * bytestring[byte_ptr-1] + bytestring[byte_ptr]; // don't forget: byte_ptr counts from 1 not 0
	   byte_ptr+=2; // points to first payload byte (if available)
   }


   // Get remaining payload:
   // 
   if ( (tx.eth_payload_s = bytestring_s - byte_ptr +1) > 0 ) // if there are any remaining bytes
     {
	(void) getbytes (bytestring, tx.eth_payload, byte_ptr, bytestring_s);
     }
   

	
   // Add padding if desired. 
   // Note: padding means 'extend to given length' (not 'add these number of bytes')
   if (tx.padding) 
     {
	pad = tx.padding - (14 + tx.eth_payload_s); // number of additonal pad bytes required
	for (i=0; i<pad; i++)
	  {  
	    // tx.eth_payload[i+tx.eth_payload_s] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
	    tx.eth_payload[i+tx.eth_payload_s] = 0x00;
	  }
	tx.eth_payload_s += pad;
     }
   
   
	
   ALL_SPECIFIED:
   // *** All Ethernet parameters have been determined !
   // *** Now let's send the frame!
   
   l = libnet_init (LIBNET_LINK_ADV, tx.device, err_buf ); 
   
   if (l == NULL)
     {
	fprintf(stderr, " mz/send_eth: libnet_init() failed (%s)", err_buf);
	return -1;
     }
   
   repeat=1;
   
   if (tx.count == 0) 
     {
	loop = 1000000;
	if (!quiet) 
	  fprintf(stderr, " mz: !!! Infinite mode! Will send frames until you stop Mausezahn!!!\n");
     }
   else
     loop = tx.count;

   if ( (!quiet) && (!tx.delay) && (tx.count==0) )
	fprintf(stderr, " mz: !!! Will send at maximum frame rate without feedback!!!\n");
   
   t=0;
   update=1;

   // this is for the statistics:
   mz_start = clock();
   total_d = tx.count;
   
   while (repeat)
     {
	if (tx.count!=0) repeat=0; // count=0 means repeat ad inifinitum 
	
	for (i=0; i<loop; i++)
	  {
	     if (src_random)
	       {
		  tx.eth_src[0] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256) & 0xFE;
		  tx.eth_src[1] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
		  tx.eth_src[2] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
		  tx.eth_src[3] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
		  tx.eth_src[4] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
		  tx.eth_src[5] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
		  update=1;
	       }
	     
	     if (dst_random)
	       {
		  tx.eth_dst[0] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
		  tx.eth_dst[1] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
		  tx.eth_dst[2] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
		  tx.eth_dst[3] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
		  tx.eth_dst[4] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
		  tx.eth_dst[5] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
		  update=1;
	       }
	     
	     if (update) // new frame parameters
	       {
		  t = libnet_build_ethernet (tx.eth_dst,
					     tx.eth_src,
					     tx.eth_type,
					     tx.eth_payload,
					     tx.eth_payload_s,
					     l,
					     t); 
	     
		  if (t == -1)
		    {
		       fprintf(stderr, " mz/send_eth: %s", libnet_geterror(l));
		       return -1;
		    }
		  
		  if (verbose) 
		    {
		       bs2str (tx.eth_dst, message, 6);                     // DA
		       fprintf(stderr, " mz: send %s",message);
		       
		       bs2str (tx.eth_src, message, 6);                     // SA
		       fprintf(stderr, " %s",message);
		       
		       type2str(tx.eth_type, message);
		       fprintf(stderr, " %s",message);                      // Type
		       
		       bs2str (tx.eth_payload, message, tx.eth_payload_s);   // Payload
		       fprintf(stderr, " %s\n",message);
		    }
		  update=0;
		  if (verbose==2) 
		    {
		        fprintf(stderr, "\n");
		       	fprintf(stderr, "*** NOTE: Simulation only! Nothing has been sent! ***\n");
		       libnet_destroy(l);
		       return 0;
		    }
		  
		    
	       }
	     
	     libnet_write(l);

	     if (tx.delay) 
	       {
		  SLEEP (tx.delay);
		  if ( (verbose) && (!src_random) && (!dst_random)  ) 
		    {
		       fprintf(stderr, ".");
		    }
	       }
	     
	  } // end for
	
     } // end while
   
   if (verbose)
     {
	if ((tx.delay) || (tx.count==0))
	  {
	     fprintf(stderr,"\n");
	  }
	
	fprintf(stderr, " mz: sent %u frames.\n",loop);
     }
   
   
   
   libnet_destroy(l);

   
   return 0;
}



// ==========================================================================================
 
   /*
   if (verbose)
     {
	fprintf(stderr," mz/send_bytes: \n");
	bs2str(da,dast,6);
	fprintf(stderr,"   DA = %s", dast);
	bs2str(sa,sast,6);
	fprintf(stderr," SA = %s", sast);
	fprintf(stderr," type = %x",et);
	bs2str(payload,pl,payload_s);
	fprintf(stderr,"  data = %s\n",pl);
     }
   */

   








2:05:28 -0500'>2017-02-02net: phy: marvell: Add support for 88e1545 PHYAndrew Lunn1-0/+1 The 88e1545 PHYs are discrete Marvell PHYs, found in a quad package on the zii-devel-b board. Add support for it to the Marvell PHY driver. Signed-off-by: Andrew Lunn <andrew@lunn.ch> Reviewed-by: Florian Fainelli <f.fainelli@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net> 2017-02-02unix: add ioctl to open a unix socket file with O_PATHAndrey Vagin1-0/+2 This ioctl opens a file to which a socket is bound and returns a file descriptor. The caller has to have CAP_NET_ADMIN in the socket network namespace. Currently it is impossible to get a path and a mount point for a socket file. socket_diag reports address, device ID and inode number for unix sockets. An address can contain a relative path or a file may be moved somewhere. And these properties say nothing about a mount namespace and a mount point of a socket file. With the introduced ioctl, we can get a path by reading /proc/self/fd/X and get mnt_id from /proc/self/fdinfo/X. In CRIU we are going to use this ioctl to dump and restore unix socket. Here is an example how it can be used: $ strace -e socket,bind,ioctl ./test /tmp/test_sock socket(AF_UNIX, SOCK_STREAM, 0) = 3 bind(3, {sa_family=AF_UNIX, sun_path="test_sock"}, 11) = 0 ioctl(3, SIOCUNIXFILE, 0) = 4 ^Z $ ss -a | grep test_sock u_str LISTEN 0 1 test_sock 17798 * 0 $ ls -l /proc/760/fd/{3,4} lrwx------ 1 root root 64 Feb 1 09:41 3 -> 'socket:[17798]' l--------- 1 root root 64 Feb 1 09:41 4 -> /tmp/test_sock $ cat /proc/760/fdinfo/4 pos: 0 flags: 012000000 mnt_id: 40 $ cat /proc/self/mountinfo | grep "^40\s" 40 19 0:37 / /tmp rw shared:23 - tmpfs tmpfs rw Signed-off-by: Andrei Vagin <avagin@openvz.org> Signed-off-by: David S. Miller <davem@davemloft.net> 2017-02-02net: phy: Marvell: Add mv88e6390 internal PHYAndrew Lunn1-0/+6 The mv88e6390 Ethernet switch has internal PHYs. These PHYs don't have an model ID in the ID2 register. So the MDIO driver in the switch intercepts reads to this register, and returns the switch family ID. Extend the Marvell PHY driver by including this ID, and treat the PHY as a 88E1540. Signed-off-by: Andrew Lunn <andrew@lunn.ch> Reviewed-by: Florian Fainelli <f.fainelli@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net> 2017-02-02Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/netDavid S. Miller9-30/+40 All merge conflicts were simple overlapping changes. Signed-off-by: David S. Miller <davem@davemloft.net> 2017-02-02netfilter: allow logging from non-init namespacesMichal Kubeček1-0/+3 Commit 69b34fb996b2 ("netfilter: xt_LOG: add net namespace support for xt_LOG") disabled logging packets using the LOG target from non-init namespaces. The motivation was to prevent containers from flooding kernel log of the host. The plan was to keep it that way until syslog namespace implementation allows containers to log in a safe way. However, the work on syslog namespace seems to have hit a dead end somewhere in 2013 and there are users who want to use xt_LOG in all network namespaces. This patch allows to do so by setting /proc/sys/net/netfilter/nf_log_all_netns to a nonzero value. This sysctl is only accessible from init_net so that one cannot switch the behaviour from inside a container. Signed-off-by: Michal Kubecek <mkubecek@suse.cz> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org> 2017-02-02ipvs: free ip_vs_dest structs when refcnt=0David Windsor1-1/+1 Currently, the ip_vs_dest cache frees ip_vs_dest objects when their reference count becomes < 0. Aside from not being semantically sound, this is problematic for the new type refcount_t, which will be introduced shortly in a separate patch. refcount_t is the new kernel type for holding reference counts, and provides overflow protection and a constrained interface relative to atomic_t (the type currently being used for kernel reference counts). Per Julian Anastasov: "The problem is that dest_trash currently holds deleted dests (unlinked from RCU lists) with refcnt=0." Changing dest_trash to hold dest with refcnt=1 will allow us to free ip_vs_dest structs when their refcnt=0, in ip_vs_dest_put_and_free(). Signed-off-by: David Windsor <dwindsor@gmail.com> Signed-off-by: Julian Anastasov <ja@ssi.bg> Signed-off-by: Simon Horman <horms@verge.net.au> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org> 2017-02-02netfilter: merge ctinfo into nfct pointer storage areaFlorian Westphal2-17/+15 After this change conntrack operations (lookup, creation, matching from ruleset) only access one instead of two sk_buff cache lines. This works for normal conntracks because those are allocated from a slab that guarantees hw cacheline or 8byte alignment (whatever is larger) so the 3 bits needed for ctinfo won't overlap with nf_conn addresses. Template allocation now does manual address alignment (see previous change) on arches that don't have sufficent kmalloc min alignment. Some spots intentionally use skb->_nfct instead of skb_nfct() helpers, this is to avoid undoing the skb_nfct() use when we remove untracked conntrack object in the future. Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org> 2017-02-02netfilter: guarantee 8 byte minalign for template addressesFlorian Westphal1-0/+2 The next change will merge skb->nfct pointer and skb->nfctinfo status bits into single skb->_nfct (unsigned long) area. For this to work nf_conn addresses must always be aligned at least on an 8 byte boundary since we will need the lower 3bits to store nfctinfo. Conntrack templates are allocated via kmalloc. kbuild test robot reported BUILD_BUG_ON failed: NFCT_INFOMASK >= ARCH_KMALLOC_MINALIGN on v1 of this patchset, so not all platforms meet this requirement. Do manual alignment if needed, the alignment offset is stored in the nf_conn entry protocol area. This works because templates are not handed off to L4 protocol trackers. Reported-by: kbuild test robot <fengguang.wu@intel.com> Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org> 2017-02-02netfilter: add and use nf_ct_set helperFlorian Westphal2-2/+9 Add a helper to assign a nf_conn entry and the ctinfo bits to an sk_buff. This avoids changing code in followup patch that merges skb->nfct and skb->nfctinfo into skb->_nfct. Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org> 2017-02-02skbuff: add and use skb_nfct helperFlorian Westphal2-4/+11 Followup patch renames skb->nfct and changes its type so add a helper to avoid intrusive rename change later. Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org> 2017-02-02netfilter: reduce direct skb->nfct usageFlorian Westphal1-3/+6 Next patch makes direct skb->nfct access illegal, reduce noise in next patch by using accessors we already have. Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org> 2017-02-02netfilter: conntrack: no need to pass ctinfo to error handlerFlorian Westphal1-1/+1 It is never accessed for reading and the only places that write to it are the icmp(6) handlers, which also set skb->nfct (and skb->nfctinfo). The conntrack core specifically checks for attached skb->nfct after ->error() invocation and returns early in this case. Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>