summaryrefslogtreecommitdiff
path: root/accel-pppd/ppp/lcp_opt_pcomp.c
blob: 28ca98f9fb726af184c9d6ada1b72db13c51ee2b (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include "linux_ppp.h"

#include "ppp.h"
#include "ppp_lcp.h"
#include "log.h"
#include "events.h"

#include "memdebug.h"

static int conf_pcomp = 0;

static struct lcp_option_t *pcomp_init(struct ppp_lcp_t *lcp);
static void pcomp_free(struct ppp_lcp_t *lcp, struct lcp_option_t *opt);
static int pcomp_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr);
static int pcomp_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr);
static int pcomp_recv_conf_rej(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr);
static int pcomp_recv_conf_nak(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr);
static int pcomp_recv_conf_ack(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr);
static void pcomp_print(void (*print)(const char *fmt, ...), struct lcp_option_t *opt, uint8_t *ptr);

struct pcomp_option_t
{
	struct lcp_option_t opt;
	int pcomp; // 0 - disabled, 1 - enabled, 2 - allow,disabled, 3 - allow,enabled
};

static struct lcp_option_handler_t pcomp_opt_hnd =
{
	.init = pcomp_init,
	.send_conf_req = pcomp_send_conf_req,
	.recv_conf_req = pcomp_recv_conf_req,
	.recv_conf_rej = pcomp_recv_conf_rej,
	.recv_conf_nak = pcomp_recv_conf_nak,
	.recv_conf_ack = pcomp_recv_conf_ack,
	.free = pcomp_free,
	.print = pcomp_print,
};

static struct lcp_option_t *pcomp_init(struct ppp_lcp_t *lcp)
{
	struct pcomp_option_t *pcomp_opt = _malloc(sizeof(*pcomp_opt));

	memset(pcomp_opt, 0, sizeof(*pcomp_opt));
	pcomp_opt->pcomp = conf_pcomp;
	pcomp_opt->opt.id = CI_PCOMP;
	pcomp_opt->opt.len = 2;

	return &pcomp_opt->opt;
}

static void pcomp_free(struct ppp_lcp_t *lcp, struct lcp_option_t *opt)
{
	struct pcomp_option_t *pcomp_opt = container_of(opt, typeof(*pcomp_opt), opt);

	_free(pcomp_opt);
}

static int pcomp_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr)
{
	struct pcomp_option_t *pcomp_opt = container_of(opt, typeof(*pcomp_opt), opt);
	struct lcp_opt_hdr_t *opt0 = (struct lcp_opt_hdr_t*)ptr;

	if (pcomp_opt->pcomp & 1) {
		opt0->id = CI_PCOMP;
		opt0->len = 2;
		return 2;
	}
	return 0;
}

static int pcomp_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr)
{
	struct pcomp_option_t *pcomp_opt = container_of(opt, typeof(*pcomp_opt), opt);
	struct lcp_opt_hdr_t *opt0 = (struct lcp_opt_hdr_t*)ptr;

	if (opt0->len != 2)
		return LCP_OPT_REJ;

	if (pcomp_opt->pcomp & 2)
		return LCP_OPT_ACK;
	else
		return LCP_OPT_REJ;
}

static int pcomp_recv_conf_rej(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr)
{
	struct pcomp_option_t *pcomp_opt = container_of(opt, typeof(*pcomp_opt), opt);

	pcomp_opt->pcomp &= ~1;
	return 0;
}

static int pcomp_recv_conf_nak(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr)
{
	struct pcomp_option_t *pcomp_opt = container_of(opt, typeof(*pcomp_opt), opt);
	struct lcp_opt_hdr_t *opt0 = (struct lcp_opt_hdr_t*)ptr;

	if (opt0->len != 2)
		return -1;

	/* treat as reject */
	return pcomp_recv_conf_rej(lcp, opt, ptr);
}

static int pcomp_recv_conf_ack(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr)
{
	struct pcomp_option_t *pcomp_opt = container_of(opt, typeof(*pcomp_opt), opt);
	int flags;

	if (ioctl(lcp->ppp->chan_fd, PPPIOCGFLAGS, &flags))
		goto err;

	flags &= ~SC_COMP_AC;
	if (pcomp_opt->pcomp & 1)
		flags |= SC_COMP_AC;

	if (ioctl(lcp->ppp->chan_fd, PPPIOCSFLAGS, &flags))
		goto err;

	return 0;

err:
	if (errno != EIO)
		log_ppp_error("lcp:pcomp: failed to set channel ACCOMP: %s\n", strerror(errno));
	return -1;
}

static void pcomp_print(void (*print)(const char *fmt, ...), struct lcp_option_t *opt, uint8_t *ptr)
{
	print("<pcomp>");
}

static void load_config(void)
{
	char *opt;

	opt = conf_get_opt("ppp", "pcomp");
	if (opt) {
		if (!strcmp(opt, "deny"))
			conf_pcomp = 0;
		else if (!strcmp(opt, "allow"))
			conf_pcomp = 1 | 2;
		else
			conf_pcomp = atoi(opt);
	}
}

static void pcomp_opt_init()
{
	lcp_option_register(&pcomp_opt_hnd);

	load_config();
	triton_event_register_handler(EV_CONFIG_RELOAD, (triton_event_func)load_config);
}

DEFINE_INIT(4, pcomp_opt_init);