Linux 5.7.6
[linux/fpc-iii.git] / arch / um / drivers / pcap_user.c
blobbbd20638788aff787964f56ac6862e771a80a79a
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4 */
6 #include <errno.h>
7 #include <pcap.h>
8 #include <string.h>
9 #include <asm/types.h>
10 #include <net_user.h>
11 #include "pcap_user.h"
12 #include <um_malloc.h>
14 #define PCAP_FD(p) (*(int *)(p))
16 static int pcap_user_init(void *data, void *dev)
18 struct pcap_data *pri = data;
19 pcap_t *p;
20 char errors[PCAP_ERRBUF_SIZE];
22 p = pcap_open_live(pri->host_if, ETH_MAX_PACKET + ETH_HEADER_OTHER,
23 pri->promisc, 0, errors);
24 if (p == NULL) {
25 printk(UM_KERN_ERR "pcap_user_init : pcap_open_live failed - "
26 "'%s'\n", errors);
27 return -EINVAL;
30 pri->dev = dev;
31 pri->pcap = p;
32 return 0;
35 static int pcap_open(void *data)
37 struct pcap_data *pri = data;
38 __u32 netmask;
39 int err;
41 if (pri->pcap == NULL)
42 return -ENODEV;
44 if (pri->filter != NULL) {
45 err = dev_netmask(pri->dev, &netmask);
46 if (err < 0) {
47 printk(UM_KERN_ERR "pcap_open : dev_netmask failed\n");
48 return -EIO;
51 pri->compiled = uml_kmalloc(sizeof(struct bpf_program),
52 UM_GFP_KERNEL);
53 if (pri->compiled == NULL) {
54 printk(UM_KERN_ERR "pcap_open : kmalloc failed\n");
55 return -ENOMEM;
58 err = pcap_compile(pri->pcap,
59 (struct bpf_program *) pri->compiled,
60 pri->filter, pri->optimize, netmask);
61 if (err < 0) {
62 printk(UM_KERN_ERR "pcap_open : pcap_compile failed - "
63 "'%s'\n", pcap_geterr(pri->pcap));
64 goto out;
67 err = pcap_setfilter(pri->pcap, pri->compiled);
68 if (err < 0) {
69 printk(UM_KERN_ERR "pcap_open : pcap_setfilter "
70 "failed - '%s'\n", pcap_geterr(pri->pcap));
71 goto out;
75 return PCAP_FD(pri->pcap);
77 out:
78 kfree(pri->compiled);
79 return -EIO;
82 static void pcap_remove(void *data)
84 struct pcap_data *pri = data;
86 if (pri->compiled != NULL)
87 pcap_freecode(pri->compiled);
89 if (pri->pcap != NULL)
90 pcap_close(pri->pcap);
93 struct pcap_handler_data {
94 char *buffer;
95 int len;
98 static void handler(u_char *data, const struct pcap_pkthdr *header,
99 const u_char *packet)
101 int len;
103 struct pcap_handler_data *hdata = (struct pcap_handler_data *) data;
105 len = hdata->len < header->caplen ? hdata->len : header->caplen;
106 memcpy(hdata->buffer, packet, len);
107 hdata->len = len;
110 int pcap_user_read(int fd, void *buffer, int len, struct pcap_data *pri)
112 struct pcap_handler_data hdata = ((struct pcap_handler_data)
113 { .buffer = buffer,
114 .len = len });
115 int n;
117 n = pcap_dispatch(pri->pcap, 1, handler, (u_char *) &hdata);
118 if (n < 0) {
119 printk(UM_KERN_ERR "pcap_dispatch failed - %s\n",
120 pcap_geterr(pri->pcap));
121 return -EIO;
123 else if (n == 0)
124 return 0;
125 return hdata.len;
128 const struct net_user_info pcap_user_info = {
129 .init = pcap_user_init,
130 .open = pcap_open,
131 .close = NULL,
132 .remove = pcap_remove,
133 .add_address = NULL,
134 .delete_address = NULL,
135 .mtu = ETH_MAX_PACKET,
136 .max_packet = ETH_MAX_PACKET + ETH_HEADER_OTHER,