Lines 1-5
Link Here
|
1 |
/*- |
1 |
/*- |
2 |
* Copyright (c) 2006 IronPort Systems |
2 |
* Copyright (c) 2006 IronPort Systems |
|
|
3 |
* Copyright (c) 2017 Carlos Neira cneirabustos@gmail.com |
3 |
* All rights reserved. |
4 |
* All rights reserved. |
4 |
* |
5 |
* |
5 |
* Redistribution and use in source and binary forms, with or without |
6 |
* Redistribution and use in source and binary forms, with or without |
Lines 25-31
Link Here
|
25 |
*/ |
26 |
*/ |
26 |
|
27 |
|
27 |
#include <sys/cdefs.h> |
28 |
#include <sys/cdefs.h> |
28 |
__FBSDID("$FreeBSD: releng/11.1/sys/compat/linsysfs/linsysfs.c 317318 2017-04-23 06:45:21Z dchagin $"); |
29 |
__FBSDID("$FreeBSD$"); |
29 |
|
30 |
|
30 |
#include <sys/param.h> |
31 |
#include <sys/param.h> |
31 |
#include <sys/systm.h> |
32 |
#include <sys/systm.h> |
Lines 51-57
Link Here
|
51 |
#include <dev/pci/pcivar.h> |
52 |
#include <dev/pci/pcivar.h> |
52 |
#include <dev/pci/pcireg.h> |
53 |
#include <dev/pci/pcireg.h> |
53 |
|
54 |
|
|
|
55 |
#include <sys/ctype.h> |
54 |
#include <net/if.h> |
56 |
#include <net/if.h> |
|
|
57 |
#include <net/if_types.h> |
58 |
#include <net/if_var.h> |
59 |
#include <net/if_dl.h> |
60 |
#include <net/vnet.h> |
55 |
|
61 |
|
56 |
#include <vm/vm.h> |
62 |
#include <vm/vm.h> |
57 |
#include <vm/pmap.h> |
63 |
#include <vm/pmap.h> |
Lines 66-71
Link Here
|
66 |
#include <compat/linux/linux_mib.h> |
72 |
#include <compat/linux/linux_mib.h> |
67 |
#include <compat/linux/linux_util.h> |
73 |
#include <compat/linux/linux_util.h> |
68 |
#include <fs/pseudofs/pseudofs.h> |
74 |
#include <fs/pseudofs/pseudofs.h> |
|
|
75 |
#include <sys/jail.h> |
76 |
|
77 |
#define LINUX_IFNAMSIZ 16 |
78 |
|
69 |
|
79 |
|
70 |
struct scsi_host_queue { |
80 |
struct scsi_host_queue { |
71 |
TAILQ_ENTRY(scsi_host_queue) scsi_host_next; |
81 |
TAILQ_ENTRY(scsi_host_queue) scsi_host_next; |
Lines 77-82
Link Here
|
77 |
|
87 |
|
78 |
static int host_number = 0; |
88 |
static int host_number = 0; |
79 |
|
89 |
|
|
|
90 |
|
80 |
static int |
91 |
static int |
81 |
atoi(const char *str) |
92 |
atoi(const char *str) |
82 |
{ |
93 |
{ |
Lines 270-275
Link Here
|
270 |
} |
281 |
} |
271 |
|
282 |
|
272 |
/* |
283 |
/* |
|
|
284 |
* Filler function for sys/class/net/<iface> |
285 |
* Used as reference |
286 |
* https://grok.elemental.org/source/xref/illumos-joyent/usr/src/uts/common/brand/lx/sysfs/lx_sysvnops.c |
287 |
* and Linux kernel documentation for /sys/class/net |
288 |
* Only 2 interfaces are created eth0 and lo and they expose the following files |
289 |
* to make applications work: |
290 |
* address, addr_len, flags, ifindex, mty, tx_queue_len and type |
291 |
*/ |
292 |
#define ETHERADDRL 6 |
293 |
#define IFP_IS_ETH(ifp) (ifp->if_type == IFT_ETHER) |
294 |
static struct ifnet * |
295 |
ifname_linux_to_bsd(struct thread *td, const char *lxname, char *bsdname) |
296 |
{ |
297 |
struct ifnet *ifp; |
298 |
int len, unit; |
299 |
char *ep; |
300 |
int is_eth, index; |
301 |
|
302 |
for (len = 0; len < LINUX_IFNAMSIZ; ++len) |
303 |
if (!isalpha(lxname[len])) |
304 |
break; |
305 |
if (len == 0 || len == LINUX_IFNAMSIZ) |
306 |
return (NULL); |
307 |
unit = (int)strtoul(lxname + len, &ep, 10); |
308 |
if (ep == NULL || ep == lxname + len || ep >= lxname + LINUX_IFNAMSIZ) |
309 |
return (NULL); |
310 |
index = 0; |
311 |
is_eth = (len == 3 && !strncmp(lxname, "eth", len)) ? 1 : 0; |
312 |
CURVNET_SET(TD_TO_VNET(td)); |
313 |
IFNET_RLOCK(); |
314 |
TAILQ_FOREACH(ifp, &V_ifnet, if_link) { |
315 |
/* |
316 |
* Allow Linux programs to use FreeBSD names. Don't presume |
317 |
* we never have an interface named "eth", so don't make |
318 |
* the test optional based on is_eth. |
319 |
*/ |
320 |
if (strncmp(ifp->if_xname, lxname, LINUX_IFNAMSIZ) == 0) |
321 |
break; |
322 |
if (is_eth && IFP_IS_ETH(ifp) && unit == index++) |
323 |
break; |
324 |
} |
325 |
IFNET_RUNLOCK(); |
326 |
CURVNET_RESTORE(); |
327 |
if (ifp != NULL) |
328 |
strlcpy(bsdname, ifp->if_xname, IFNAMSIZ); |
329 |
return (ifp); |
330 |
} |
331 |
|
332 |
static int |
333 |
linsysfs_ifnet_addr (PFS_FILL_ARGS) { |
334 |
struct ifnet* ifp; |
335 |
char bsdname[IFNAMSIZ+1]; |
336 |
uint8_t* address; |
337 |
if (!memcmp((const char*)pn->pn_parent->pn_name,"lo",2)) { |
338 |
sbuf_printf (sb, "00:00:00:00:00:00\n"); |
339 |
return (0); |
340 |
} else { |
341 |
ifp = ifname_linux_to_bsd(curthread,(const char*)pn->pn_parent->pn_name,bsdname); |
342 |
if (!ifp) { |
343 |
sbuf_printf (sb, "00:00:00:00:00:00\n"); |
344 |
return (0); |
345 |
} |
346 |
address = IF_LLADDR(ifp); |
347 |
sbuf_printf (sb,"%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n", |
348 |
address[0], address[1], address[2], |
349 |
address[3], address[4], address[5]); |
350 |
return (0); |
351 |
} |
352 |
} |
353 |
//https://grok.elemental.org/source/xref/illumos-joyent/usr/src/uts/common/brand/lx/sysfs/lx_sysvnops.c#LXSYS_ENDP_NET_ADDRLEN |
354 |
static int |
355 |
linsysfs_ifnet_addrlen (PFS_FILL_ARGS) { |
356 |
sbuf_printf(sb,"6\n"); |
357 |
return (0); |
358 |
} |
359 |
//;https://grok.elemental.org/source/xref/illumos-joyent/usr/src/uts/common/brand/lx/os/lx_misc.c#859 |
360 |
static int |
361 |
linsysfs_ifnet_flags (PFS_FILL_ARGS) { |
362 |
int buf; |
363 |
struct ifnet* ifp; |
364 |
char bsdname[IFNAMSIZ+1]; |
365 |
if (!memcmp((const char*)pn->pn_parent->pn_name,"eth",3)) { |
366 |
|
367 |
ifp = ifname_linux_to_bsd(curthread,(const char*)pn->pn_parent->pn_name,bsdname); |
368 |
buf = ifp->if_flags & (IFF_UP | IFF_BROADCAST | IFF_DEBUG | |
369 |
IFF_LOOPBACK | IFF_POINTOPOINT | |
370 |
IFF_DRV_RUNNING | IFF_NOARP | IFF_PROMISC | IFF_ALLMULTI); |
371 |
buf |= 0x1000; |
372 |
sbuf_printf(sb,"0x%x\n",buf); |
373 |
} else { |
374 |
sbuf_printf(sb,"0x9\n"); |
375 |
} |
376 |
return (0); |
377 |
} |
378 |
static int |
379 |
linsysfs_ifnet_ifindex (PFS_FILL_ARGS) { |
380 |
struct ifnet* ifp; |
381 |
char bsdname[IFNAMSIZ+1]; |
382 |
if (!memcmp((const char*)pn->pn_parent->pn_name,"eth",3)) { |
383 |
ifp = ifname_linux_to_bsd(curthread,(const char*)pn->pn_parent->pn_name,bsdname); |
384 |
sbuf_printf(sb,"%u\n",ifp->if_index); |
385 |
} else { |
386 |
sbuf_printf(sb,"1\n"); |
387 |
} |
388 |
return (0); |
389 |
} |
390 |
static int |
391 |
linsysfs_ifnet_mtu (PFS_FILL_ARGS) { |
392 |
struct ifnet* ifp; |
393 |
char bsdname[IFNAMSIZ+1]; |
394 |
if (!memcmp((const char*)pn->pn_parent->pn_name,"lo",2)) { |
395 |
sbuf_printf(sb,"65536\n"); |
396 |
} else { |
397 |
ifp = ifname_linux_to_bsd(curthread,(const char*)pn->pn_parent->pn_name,bsdname); |
398 |
sbuf_printf(sb,"%u\n",ifp->if_mtu); |
399 |
} |
400 |
return (0); |
401 |
} |
402 |
|
403 |
static int |
404 |
linsysfs_ifnet_tx_queue_len (PFS_FILL_ARGS) { |
405 |
sbuf_printf(sb,"1000\n"); |
406 |
return (0); |
407 |
} |
408 |
static int |
409 |
linsysfs_ifnet_type (PFS_FILL_ARGS) { |
410 |
if (!memcmp((const char*)pn->pn_parent->pn_name,"lo",2)) |
411 |
sbuf_printf(sb,"772\n"); |
412 |
else |
413 |
sbuf_printf(sb,"2\n"); |
414 |
return (0); |
415 |
} |
416 |
|
417 |
|
418 |
|
419 |
static void |
420 |
linsysfs_listnics (struct pfs_node *dir) |
421 |
{ |
422 |
|
423 |
struct pfs_node *nic; |
424 |
struct pfs_node *lo; |
425 |
|
426 |
nic = pfs_create_dir (dir, "eth0", NULL, NULL, NULL, 0); |
427 |
|
428 |
pfs_create_file (nic, "address", &linsysfs_ifnet_addr, |
429 |
NULL, NULL, NULL, PFS_RD); |
430 |
|
431 |
pfs_create_file (nic, "addr_len", &linsysfs_ifnet_addrlen, |
432 |
NULL, NULL, NULL, PFS_RD); |
433 |
|
434 |
pfs_create_file (nic, "flags", &linsysfs_ifnet_flags, |
435 |
NULL, NULL, NULL, PFS_RD); |
436 |
|
437 |
pfs_create_file (nic, "ifindex", &linsysfs_ifnet_ifindex, |
438 |
NULL, NULL, NULL, PFS_RD); |
439 |
|
440 |
pfs_create_file (nic, "mtu", &linsysfs_ifnet_mtu, |
441 |
NULL, NULL, NULL, PFS_RD); |
442 |
|
443 |
pfs_create_file (nic, "tx_queue_len", &linsysfs_ifnet_tx_queue_len, |
444 |
NULL, NULL, NULL, PFS_RD); |
445 |
|
446 |
pfs_create_file (nic, "type", &linsysfs_ifnet_type, |
447 |
NULL, NULL, NULL, PFS_RD); |
448 |
|
449 |
lo = pfs_create_dir (dir, "lo", NULL, NULL, NULL, 0); |
450 |
|
451 |
pfs_create_file (lo, "address", &linsysfs_ifnet_addr, |
452 |
NULL, NULL, NULL, PFS_RD); |
453 |
|
454 |
pfs_create_file (lo, "addr_len", &linsysfs_ifnet_addrlen, |
455 |
NULL, NULL, NULL, PFS_RD); |
456 |
|
457 |
pfs_create_file (lo, "flags", &linsysfs_ifnet_flags, |
458 |
NULL, NULL, NULL, PFS_RD); |
459 |
|
460 |
pfs_create_file (lo, "ifindex", &linsysfs_ifnet_ifindex, |
461 |
NULL, NULL, NULL, PFS_RD); |
462 |
|
463 |
pfs_create_file (lo, "mtu", &linsysfs_ifnet_mtu, |
464 |
NULL, NULL, NULL, PFS_RD); |
465 |
|
466 |
pfs_create_file (lo, "tx_queue_len", &linsysfs_ifnet_tx_queue_len, |
467 |
NULL, NULL, NULL, PFS_RD); |
468 |
|
469 |
pfs_create_file (lo, "type", &linsysfs_ifnet_type, |
470 |
NULL, NULL, NULL, PFS_RD); |
471 |
|
472 |
} |
473 |
|
474 |
/* |
273 |
* Constructor |
475 |
* Constructor |
274 |
*/ |
476 |
*/ |
275 |
static int |
477 |
static int |
Lines 279-284
Link Here
|
279 |
struct pfs_node *dir, *sys, *cpu; |
481 |
struct pfs_node *dir, *sys, *cpu; |
280 |
struct pfs_node *pci; |
482 |
struct pfs_node *pci; |
281 |
struct pfs_node *scsi; |
483 |
struct pfs_node *scsi; |
|
|
484 |
struct pfs_node *net; |
282 |
devclass_t devclass; |
485 |
devclass_t devclass; |
283 |
device_t dev; |
486 |
device_t dev; |
284 |
|
487 |
|
Lines 288-293
Link Here
|
288 |
|
491 |
|
289 |
/* /sys/class/... */ |
492 |
/* /sys/class/... */ |
290 |
scsi = pfs_create_dir(root, "class", NULL, NULL, NULL, 0); |
493 |
scsi = pfs_create_dir(root, "class", NULL, NULL, NULL, 0); |
|
|
494 |
|
495 |
/* /sys/class/net/.. */ |
496 |
net = pfs_create_dir(scsi, "net", NULL, NULL, NULL, 0); |
497 |
|
291 |
scsi = pfs_create_dir(scsi, "scsi_host", NULL, NULL, NULL, 0); |
498 |
scsi = pfs_create_dir(scsi, "scsi_host", NULL, NULL, NULL, 0); |
292 |
|
499 |
|
293 |
/* /sys/devices */ |
500 |
/* /sys/devices */ |
Lines 314-319
Link Here
|
314 |
NULL, NULL, NULL, PFS_RD); |
521 |
NULL, NULL, NULL, PFS_RD); |
315 |
|
522 |
|
316 |
linsysfs_listcpus(cpu); |
523 |
linsysfs_listcpus(cpu); |
|
|
524 |
linsysfs_listnics(net); |
317 |
|
525 |
|
318 |
return (0); |
526 |
return (0); |
319 |
} |
527 |
} |
Lines 336-341
Link Here
|
336 |
return (0); |
544 |
return (0); |
337 |
} |
545 |
} |
338 |
|
546 |
|
|
|
547 |
|
548 |
|
549 |
|
550 |
|
339 |
PSEUDOFS(linsysfs, 1, PR_ALLOW_MOUNT_LINSYSFS); |
551 |
PSEUDOFS(linsysfs, 1, PR_ALLOW_MOUNT_LINSYSFS); |
340 |
#if defined(__amd64__) |
552 |
#if defined(__amd64__) |
341 |
MODULE_DEPEND(linsysfs, linux_common, 1, 1, 1); |
553 |
MODULE_DEPEND(linsysfs, linux_common, 1, 1, 1); |