Skip to content

Commit

Permalink
libbpf: Support unbound raw socket creation
Browse files Browse the repository at this point in the history
Passing a NULL (or empty) char string to bpf_open_raw_sock() will skip
the bind call on the newly created raw socket. This can create sniffers
for all interfaces using a single socket filter.

The dns_matching example has been updated to default to 'any' interface
if noone is specified in the arguments.
  • Loading branch information
oliviertilmans committed Nov 22, 2017
1 parent 1af99d4 commit 830d58d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
5 changes: 3 additions & 2 deletions examples/networking/dns_matching/dns_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ def add_cache_entry(cache, name):
parser = argparse.ArgumentParser(usage='For detailed information about usage,\
try with -h option')
req_args = parser.add_argument_group("Required arguments")
req_args.add_argument("-i", "--interface", type=str, required=True, help="Interface name")
req_args.add_argument("-i", "--interface", type=str, default="",
help="Interface name, defaults to all if unspecified.")
req_args.add_argument("-d", "--domains", type=str, required=True, nargs="+",
help='List of domain names separated by space. For example: -d "abc.def xyz.mno"')
help='List of domain names separated by space. For example: -d abc.def xyz.mno')
args = parser.parse_args()

# initialize BPF - load source code from http-parse-simple.c
Expand Down
4 changes: 4 additions & 0 deletions src/cc/libbpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,10 @@ int bpf_open_raw_sock(const char *name)
return -1;
}

/* Do not bind on empty interface names */
if (!name || *name == '\0')
return sock;

memset(&sll, 0, sizeof(sll));
sll.sll_family = AF_PACKET;
sll.sll_ifindex = if_nametoindex(name);
Expand Down
3 changes: 2 additions & 1 deletion src/cc/libbpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ int bpf_prog_load(enum bpf_prog_type prog_type, const char *name,

int bpf_attach_socket(int sockfd, int progfd);

/* create RAW socket and bind to interface 'name' */
/* create RAW socket. If name is not NULL/a non-empty null-terminated string,
* bind the raw socket to the interface 'name' */
int bpf_open_raw_sock(const char *name);

typedef void (*perf_reader_cb)(void *cb_cookie, int pid, uint64_t callchain_num,
Expand Down

0 comments on commit 830d58d

Please sign in to comment.