Blame usbredirparser/usbredirfilter.h

Packit 9795e1
/* usbredirfilter.h usb redirection filter header
Packit 9795e1
Packit 9795e1
   Copyright 2012 Red Hat, Inc.
Packit 9795e1
Packit 9795e1
   Red Hat Authors:
Packit 9795e1
   Hans de Goede <hdegoede@redhat.com>
Packit 9795e1
Packit 9795e1
   This library is free software; you can redistribute it and/or
Packit 9795e1
   modify it under the terms of the GNU Lesser General Public
Packit 9795e1
   License as published by the Free Software Foundation; either
Packit 9795e1
   version 2.1 of the License, or (at your option) any later version.
Packit 9795e1
Packit 9795e1
   This library is distributed in the hope that it will be useful,
Packit 9795e1
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 9795e1
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 9795e1
   Lesser General Public License for more details.
Packit 9795e1
Packit 9795e1
   You should have received a copy of the GNU Lesser General Public
Packit 9795e1
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit 9795e1
*/
Packit 9795e1
#ifndef __USBREDIRFILTER_H
Packit 9795e1
#define __USBREDIRFILTER_H
Packit 9795e1
Packit 9795e1
#include <stdio.h>
Packit 9795e1
#include <stdint.h>
Packit 9795e1
Packit 9795e1
#ifdef __cplusplus
Packit 9795e1
extern "C" {
Packit 9795e1
#endif
Packit 9795e1
Packit 9795e1
struct usbredirfilter_rule {
Packit 9795e1
    int device_class;       /* 0-255, -1 to match any class */
Packit 9795e1
    int vendor_id;          /* 0-65535, -1 to match any id */
Packit 9795e1
    int product_id;         /* 0-65535, -1 to match any id */
Packit 9795e1
    int device_version_bcd; /* 0-255, -1 to match any version */
Packit 9795e1
    int allow;              /* 0: deny redir for this device, non 0: allow */
Packit 9795e1
};
Packit 9795e1
Packit 9795e1
/* Read a filter string and parse it into an array of usbredirfilter_rule-s.
Packit 9795e1
Packit 9795e1
   Where each rule has the form of:
Packit 9795e1
   <class>,<vendor>,<product>,<version>,<allow>
Packit 9795e1
   Assuming "," as the specified token_sep character.
Packit 9795e1
Packit 9795e1
   And the rules are themselves are separated by the rule_sep character, ie:
Packit 9795e1
   <rule1>|<rule2>|<rule3>
Packit 9795e1
Packit 9795e1
   Assuming "|" as the rule_sep character. Note that with the seperator used
Packit 9795e1
   in this example the format matches the format as written by the RHEV-M USB
Packit 9795e1
   filter editor tool.
Packit 9795e1
Packit 9795e1
   Note that the seperators must be single character strings!
Packit 9795e1
Packit 9795e1
   On success the rules get returned in rules_ret and rules_count_ret, the
Packit 9795e1
   returned rules array should be freed with free() when the caller is done
Packit 9795e1
   with it.
Packit 9795e1
Packit 9795e1
   Return value: 0 on success, -ENOMEM when allocating the rules array fails,
Packit 9795e1
       or -EINVAL when there is on parsing error.
Packit 9795e1
*/
Packit 9795e1
int usbredirfilter_string_to_rules(
Packit 9795e1
    const char *filter_str, const char *token_sep, const char *rule_sep,
Packit 9795e1
    struct usbredirfilter_rule **rules_ret, int *rules_count_ret);
Packit 9795e1
Packit 9795e1
/* Convert a set of rules back to a string suitable for passing to
Packit 9795e1
   usbredirfilter_string_to_rules(); The returned string must be free()-ed
Packit 9795e1
   by the caller when it is done with it.
Packit 9795e1
Packit 9795e1
   Return value: The string on sucess, or NULL if the rules fail verification,
Packit 9795e1
      or when allocating the string fails.
Packit 9795e1
*/
Packit 9795e1
char *usbredirfilter_rules_to_string(const struct usbredirfilter_rule *rules,
Packit 9795e1
    int rules_count, const char *token_sep, const char *rule_sep);
Packit 9795e1
Packit 9795e1
/* Check if redirection of a device with the passed in device info is allowed
Packit 9795e1
   by the passed set of filter rules.
Packit 9795e1
Packit 9795e1
   Since a device has class info at both the device level and the interface
Packit 9795e1
   level, this function does multiple passes.
Packit 9795e1
Packit 9795e1
   First the rules are checked one by one against the given device info using
Packit 9795e1
   the device class info, if a matching rule is found, the result of the check
Packit 9795e1
   is that of that rule.
Packit 9795e1
Packit 9795e1
   Then the same is done for each interface the device has, substituting the
Packit 9795e1
   device class info with the class info from the interfaces.
Packit 9795e1
Packit 9795e1
   Note that under certain circumstances some passes are skipped:
Packit 9795e1
   - For devices with a device class of 0x00 or 0xef, the pass which checks the
Packit 9795e1
     device class is skipped.
Packit 9795e1
   - If the usbredirfilter_fl_dont_skip_non_boot_hid flag is not passed, then
Packit 9795e1
     for devices with more than 1 interface and an interface with an interface
Packit 9795e1
     class of 0x03, an interface subclass of 0x00 and an interface protocol
Packit 9795e1
     of 0x00, the check is skipped for that interface. This allows to skip ie
Packit 9795e1
     checking the interface for volume buttons on some usbaudio class devices.
Packit 9795e1
Packit 9795e1
   If the result of all (not skipped) passes is allow, then 0 will be returned,
Packit 9795e1
   which indicates that redirection should be allowed.
Packit 9795e1
Packit 9795e1
   If the result of a matching rule is deny, then processing stops and
Packit 9795e1
   -EPERM will be returned.
Packit 9795e1
Packit 9795e1
   If a given pass does not match any rules, then processing stops and
Packit 9795e1
   -ENOENT will be returned. This behavior can be changed with the
Packit 9795e1
   usbredirfilter_fl_default_allow flag, if this flag is set the result of a
Packit 9795e1
   pass with no matching rules will be allow.
Packit 9795e1
Packit 9795e1
   Return value:
Packit 9795e1
    0        Redirection is allowed
Packit 9795e1
    -EINVAL  Invalid parameters
Packit 9795e1
    -EPERM   Redirection is blocked by the filter rules
Packit 9795e1
    -ENOENT  None of the rules matched the device (during one of the passes)
Packit 9795e1
*/
Packit 9795e1
enum {
Packit 9795e1
    usbredirfilter_fl_default_allow = 0x01,
Packit 9795e1
    usbredirfilter_fl_dont_skip_non_boot_hid = 0x02,
Packit 9795e1
};
Packit 9795e1
int usbredirfilter_check(
Packit 9795e1
    const struct usbredirfilter_rule *rules, int rules_count,
Packit 9795e1
    uint8_t device_class, uint8_t device_subclass, uint8_t device_protocol,
Packit 9795e1
    uint8_t *interface_class, uint8_t *interface_subclass,
Packit 9795e1
    uint8_t *interface_protocol, int interface_count,
Packit 9795e1
    uint16_t vendor_id, uint16_t product_id, uint16_t device_version_bcd,
Packit 9795e1
    int flags);
Packit 9795e1
Packit 9795e1
/* Sanity check the passed in rules
Packit 9795e1
Packit 9795e1
   Return value: 0 on success, -EINVAL when some values are out of bound. */
Packit 9795e1
int usbredirfilter_verify(
Packit 9795e1
    const struct usbredirfilter_rule *rules, int rules_count);
Packit 9795e1
Packit 9795e1
/* Print the passed in rules to FILE out in human readable format */
Packit 9795e1
void usbredirfilter_print(
Packit 9795e1
    const struct usbredirfilter_rule *rules, int rules_count, FILE *out);
Packit 9795e1
Packit 9795e1
#ifdef __cplusplus
Packit 9795e1
}
Packit 9795e1
#endif
Packit 9795e1
Packit 9795e1
#endif