summaryrefslogtreecommitdiffstats
path: root/drivers/net/pppox.c
blob: 941bf369e7f181feb3af6f14f1ff9ba7e8f452c9 (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
162
163
164
165
166
167
168
169
170
171
172
173
/** -*- linux-c -*- ***********************************************************
 * Linux PPP over X/Ethernet (PPPoX/PPPoE) Sockets
 *
 * PPPoX --- Generic PPP encapsulation socket family
 * PPPoE --- PPP over Ethernet (RFC 2516)
 *
 *
 * Version:	0.5.0
 *
 * Author:	Michal Ostrowski <mostrows@styx.uwaterloo.ca>
 *
 * License:
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 *
 */

#include <linux/config.h>
#include <linux/string.h>
#include <linux/module.h>

#include <asm/uaccess.h>

#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/malloc.h>
#include <linux/errno.h>

#include <linux/netdevice.h>
#include <linux/net.h>
#include <linux/init.h>
#include <linux/if_pppox.h>
#include <net/sock.h>
#include <linux/ppp_defs.h>
#include <linux/if_ppp.h>
#include <linux/ppp_channel.h>

static struct pppox_proto *proto[PX_MAX_PROTO+1] = { NULL, };

int register_pppox_proto(int proto_num, struct pppox_proto *pp)
{
	if (proto_num < 0 || proto_num > PX_MAX_PROTO) {
		return -EINVAL;
	}

	if (proto[proto_num])
		return -EALREADY;

	MOD_INC_USE_COUNT;

	proto[proto_num] = pp;
	return 0;
}

void unregister_pppox_proto(int proto_num)
{
	if (proto_num >= 0 && proto_num <= PX_MAX_PROTO) {
	    proto[proto_num] = NULL;
	    MOD_DEC_USE_COUNT;
	}
}

void pppox_unbind_sock(struct sock *sk)
{
	/* Clear connection to ppp device, if attached. */

	if (sk->state & PPPOX_BOUND) {
		ppp_unregister_channel(&sk->protinfo.pppox->chan);
		sk->state &= ~PPPOX_BOUND;
	}
}

EXPORT_SYMBOL(register_pppox_proto);
EXPORT_SYMBOL(unregister_pppox_proto);
EXPORT_SYMBOL(pppox_unbind_sock);

int pppox_ioctl(struct socket* sock, unsigned int cmd,
		unsigned long arg)
{
	struct sock *sk = sock->sk;
	struct pppox_opt *po;
	int err = 0;

	po = sk->protinfo.pppox;

	lock_sock(sk);

	switch (cmd) {
	case PPPIOCGCHAN:{
		int index;
		err = -ENOTCONN;
		if (!(sk->state & PPPOX_CONNECTED))
			break;

		err = -EINVAL;
		index = ppp_channel_index(&po->chan);
		if (put_user(index , (int *) arg))
			break;

		err = 0;
		sk->state |= PPPOX_BOUND;
		break;
	}
	default:
		if (proto[sk->protocol]->ioctl)
			err = (*proto[sk->protocol]->ioctl)(sock, cmd, arg);

		break;
	};

	release_sock(sk);
	return err;
}


int pppox_create(struct socket *sock, int protocol)
{
	int err = 0;

	if (protocol < 0 || protocol > PX_MAX_PROTO)
	    return -EPROTOTYPE;

	if (proto[protocol] == NULL)
	    return -EPROTONOSUPPORT;

	err = (*proto[protocol]->create)(sock);

	if (err == 0) {
		/* We get to set the ioctl handler. */
		/* For everything else, pppox is just a shell. */
		sock->ops->ioctl = pppox_ioctl;
	}

	return err;
}

struct net_proto_family pppox_proto_family = {
	PF_PPPOX,
	pppox_create
};

#ifdef MODULE
int init_module(void)
#else
void __init pppox_proto_init(struct net_proto *pro)
#endif
{
	int err = 0;

	err = sock_register(&pppox_proto_family);

	if (err == 0)
		printk(KERN_INFO "Registered PPPoX v0.5\n");

#ifdef CONFIG_PPPOE
	pppoe_init();
#endif

	return err;
}

#ifdef MODULE

MODULE_PARM(debug, "i");

void cleanup_module(void)
{
	sock_unregister(PF_PPPOX);
}

#endif