-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathnatflow_zone.h
71 lines (59 loc) · 1.76 KB
/
natflow_zone.h
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
/*
* Author: Chen Minqiang <[email protected]>
* Date : Mon, 02 Jul 2018 15:36:09 +0800
*/
#ifndef _NATFLOW_ZONE_H_
#define _NATFLOW_ZONE_H_
#include <linux/netdevice.h>
#define INVALID_ZONE_ID 127
#define ZONE_ID_MASK 127
#define MAX_ZONE_ID (ZONE_ID_MASK - 1)
#define ZONE_TYPE_SHIFT 7
#define ZONE_TYPE_MASK (1 << ZONE_TYPE_SHIFT)
#define ZONE_TYPE_LAN 1
#define ZONE_TYPE_WAN 0
struct zone_match_t {
struct list_head list;
int type;
int id;
unsigned char if_name[IFNAMSIZ];
};
static inline int natflow_zone_id_get(const struct net_device *dev)
{
if (strlen(dev->name) + 2 > IFNAMSIZ)
return INVALID_ZONE_ID;
return dev->name[IFNAMSIZ - 1] & ZONE_ID_MASK;
}
static inline int natflow_zone_id_get_safe(const struct net_device *dev)
{
if (dev == NULL)
return INVALID_ZONE_ID;
return natflow_zone_id_get(dev);
}
static inline int natflow_zone_id_set(struct net_device *dev, int id)
{
if (strlen(dev->name) + 2 > IFNAMSIZ)
return -1;
dev->name[IFNAMSIZ - 1] = (dev->name[IFNAMSIZ - 1] & ZONE_TYPE_MASK) | (id & ZONE_ID_MASK);
return 0;
}
static inline int natflow_zone_type_get(const struct net_device *dev)
{
if (strlen(dev->name) + 2 > IFNAMSIZ)
return INVALID_ZONE_ID;
return (dev->name[IFNAMSIZ - 1] & ZONE_TYPE_MASK) >> ZONE_TYPE_SHIFT;
}
static inline int natflow_zone_type_set(struct net_device *dev, int type)
{
if (strlen(dev->name) + 2 > IFNAMSIZ)
return -1;
dev->name[IFNAMSIZ - 1] = (dev->name[IFNAMSIZ - 1] & ZONE_ID_MASK) | ((type << ZONE_TYPE_SHIFT) & ZONE_TYPE_MASK);
return 0;
}
static inline int natflow_is_lan_zone(const struct net_device *dev)
{
return (natflow_zone_id_get(dev) != INVALID_ZONE_ID && natflow_zone_type_get(dev) == ZONE_TYPE_LAN);
}
int natflow_zone_init(void);
void natflow_zone_exit(void);
#endif /* _NATFLOW_ZONE_H_ */