summaryrefslogtreecommitdiff
path: root/accel-pptpd/ppp/lcp_opt_mru.c
blob: fc79db3800d5804324a326a391a4d9fa7e92ef43 (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
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <linux/if_ppp.h>
#include <sys/ioctl.h>

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

#define MAX_MTU 1436

static struct lcp_option_t *mru_init(struct ppp_lcp_t *lcp);
static void mru_free(struct ppp_lcp_t *lcp, struct lcp_option_t *opt);
static int mru_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr);
static int mru_send_conf_nak(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr);
static int mru_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr);
static int mru_recv_conf_ack(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr);
static void mru_print(void (*print)(const char *fmt,...),struct lcp_option_t*, uint8_t *ptr);

struct mru_option_t
{
	struct lcp_option_t opt;
	int mru;
	int mtu;
};

static struct lcp_option_handler_t mru_opt_hnd=
{
	.init=mru_init,
	.send_conf_req=mru_send_conf_req,
	.send_conf_nak=mru_send_conf_nak,
	.recv_conf_req=mru_recv_conf_req,
	.recv_conf_ack=mru_recv_conf_ack,
	.free=mru_free,
	.print=mru_print,
};

static struct lcp_option_t *mru_init(struct ppp_lcp_t *lcp)
{
	struct mru_option_t *mru_opt=malloc(sizeof(*mru_opt));
	memset(mru_opt,0,sizeof(*mru_opt));
	mru_opt->mtu=0;
	mru_opt->mru=MAX_MTU;
	mru_opt->opt.id=CI_MRU;
	mru_opt->opt.len=4;

	return &mru_opt->opt;
}

static void mru_free(struct ppp_lcp_t *lcp, struct lcp_option_t *opt)
{
	struct mru_option_t *mru_opt=container_of(opt,typeof(*mru_opt),opt);

	free(mru_opt);
}

static int mru_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr)
{
	struct mru_option_t *mru_opt=container_of(opt,typeof(*mru_opt),opt);
	struct lcp_opt16_t *opt16=(struct lcp_opt16_t*)ptr;
	opt16->hdr.id=CI_MRU;
	opt16->hdr.len=4;
	opt16->val=htons(mru_opt->mru);
	return 4;
}

static int mru_send_conf_nak(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr)
{
	struct mru_option_t *mru_opt=container_of(opt,typeof(*mru_opt),opt);
	struct lcp_opt16_t *opt16=(struct lcp_opt16_t*)ptr;
	opt16->hdr.id=CI_MRU;
	opt16->hdr.len=4;
	opt16->val=htons(mru_opt->mtu);
	return 4;
}

static int mru_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr)
{
	struct mru_option_t *mru_opt=container_of(opt,typeof(*mru_opt),opt);
	struct lcp_opt16_t *opt16=(struct lcp_opt16_t*)ptr;

	if (!mru_opt->mtu || mru_opt->mtu==ntohs(opt16->val))
	{
		mru_opt->mtu=ntohs(opt16->val);
		return LCP_OPT_ACK;
	}else return LCP_OPT_NAK;
}

static int mru_recv_conf_ack(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr)
{
	struct mru_option_t *mru_opt = container_of(opt,typeof(*mru_opt), opt);
	struct ifreq ifr = {
		.ifr_mtu = mru_opt->mtu,
	};

	strcpy(ifr.ifr_name, lcp->ppp->ifname);

	if (ioctl(lcp->ppp->unit_fd, PPPIOCSMRU, &mru_opt->mru))
		log_ppp_error("lcp:mru: failed to set MRU: %s\n", strerror(errno));

	if (ioctl(sock_fd, SIOCSIFMTU, &ifr))
		log_ppp_error("lcp:mru: failed to set MTU: %s\n", strerror(errno));
	
	return 0;
}

static void mru_print(void (*print)(const char *fmt,...),struct lcp_option_t *opt, uint8_t *ptr)
{
	struct mru_option_t *mru_opt=container_of(opt,typeof(*mru_opt),opt);
	struct lcp_opt16_t *opt16=(struct lcp_opt16_t*)ptr;

	if (ptr) print("<mru %i>",ntohs(opt16->val));
	else print("<mru %i>",mru_opt->mru);
}

static void __init mru_opt_init()
{
	lcp_option_register(&mru_opt_hnd);
}