60 #include <linux/string.h>
61 #include <linux/module.h>
62 #include <linux/kernel.h>
63 #include <linux/slab.h>
64 #include <linux/errno.h>
65 #include <linux/netdevice.h>
66 #include <linux/net.h>
71 #include <linux/if_ether.h>
72 #include <linux/if_pppox.h>
74 #include <linux/ppp_defs.h>
86 #include <asm/uaccess.h>
88 #define PPPOE_HASH_BITS 4
89 #define PPPOE_HASH_SIZE (1 << PPPOE_HASH_BITS)
90 #define PPPOE_HASH_MASK (PPPOE_HASH_SIZE - 1)
120 static inline bool stage_session(
__be16 sid)
142 #if 8 % PPPOE_HASH_BITS
143 #error 8 must be a multiple of PPPOE_HASH_BITS
148 unsigned char hash = 0;
153 for (i = 0; i <
sizeof(
sid_t) * 8; i += 8)
167 unsigned char *addr,
int ifindex)
169 int hash = hash_item(sid, addr);
174 if (cmp_addr(&ret->pppoe_pa, sid, addr) &&
175 ret->pppoe_ifindex == ifindex)
186 int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
191 if (cmp_2_addr(&ret->pppoe_pa, &po->pppoe_pa) &&
192 ret->pppoe_ifindex == po->pppoe_ifindex)
205 char *addr,
int ifindex)
207 int hash = hash_item(sid, addr);
214 if (cmp_addr(&ret->pppoe_pa, sid, addr) &&
215 ret->pppoe_ifindex == ifindex) {
231 unsigned char *addr,
int ifindex)
236 po = __get_item(pn, sid, addr, ifindex);
238 sock_hold(sk_pppox(po));
257 pn = pppoe_pernet(net);
266 char *addr,
int ifindex)
269 __delete_item(pn, sid, addr, ifindex);
280 static void pppoe_flush_dev(
struct net_device *dev)
285 pn = pppoe_pernet(dev_net(dev));
292 while (po && po->pppoe_dev != dev) {
313 if (po->pppoe_dev == dev &&
318 po->pppoe_dev =
NULL;
354 pppoe_flush_dev(dev);
365 .notifier_call = pppoe_device_event,
375 struct pppox_sock *po = pppox_sk(sk);
376 struct pppox_sock *relay_po;
386 relay_po = get_item_by_addr(sock_net(sk),
388 if (relay_po ==
NULL)
394 if (!__pppoe_xmit(sk_pppox(relay_po), skb))
404 sock_put(sk_pppox(relay_po));
420 struct pppox_sock *po;
428 if (!pskb_may_pull(skb,
sizeof(
struct pppoe_hdr)))
438 if (pskb_trim_rcsum(skb, len))
441 pn = pppoe_pernet(dev_net(dev));
469 struct pppox_sock *po;
476 if (!pskb_may_pull(skb,
sizeof(
struct pppoe_hdr)))
483 pn = pppoe_pernet(dev_net(dev));
486 struct sock *sk = sk_pppox(po);
517 static struct packet_type pppoed_ptype __read_mostly = {
519 .func = pppoe_disc_rcv,
522 static struct proto pppoe_sk_proto __read_mostly = {
525 .obj_size =
sizeof(
struct pppox_sock),
533 static int pppoe_create(
struct net *net,
struct socket *
sock)
544 sock->
ops = &pppoe_ops;
555 static int pppoe_release(
struct socket *sock)
557 struct sock *sk = sock->
sk;
558 struct pppox_sock *po;
560 struct net *net =
NULL;
574 dev_put(po->pppoe_dev);
575 po->pppoe_dev =
NULL;
584 pn = pppoe_pernet(net);
590 delete_item(pn, po->pppoe_pa.sid, po->pppoe_pa.remote,
603 static int pppoe_connect(
struct socket *sock,
struct sockaddr *uservaddr,
604 int sockaddr_len,
int flags)
606 struct sock *sk = sock->
sk;
608 struct pppox_sock *po = pppox_sk(sk);
611 struct net *net =
NULL;
622 if ((sk->sk_state & PPPOX_CONNECTED) &&
635 if (stage_session(po->pppoe_pa.sid)) {
637 pn = pppoe_pernet(sock_net(sk));
638 delete_item(pn, po->pppoe_pa.sid,
639 po->pppoe_pa.remote, po->pppoe_ifindex);
641 dev_put(po->pppoe_dev);
642 po->pppoe_dev =
NULL;
645 memset(sk_pppox(po) + 1, 0,
646 sizeof(
struct pppox_sock) -
sizeof(
struct sock));
659 po->pppoe_ifindex = dev->
ifindex;
660 pn = pppoe_pernet(net);
670 error = __set_item(pn, po);
676 dev->hard_header_len);
680 po->
chan.ops = &pppoe_chan_ops;
684 delete_item(pn, po->pppoe_pa.sid,
685 po->pppoe_pa.remote, po->pppoe_ifindex);
699 dev_put(po->pppoe_dev);
700 po->pppoe_dev =
NULL;
706 int *usockaddr_len,
int peer)
718 *usockaddr_len = len;
723 static int pppoe_ioctl(
struct socket *sock,
unsigned int cmd,
726 struct sock *sk = sock->
sk;
727 struct pppox_sock *po = pppox_sk(sk);
734 if (!(sk->sk_state & PPPOX_CONNECTED))
748 if (!(sk->sk_state & PPPOX_CONNECTED))
752 if (
get_user(val, (
int __user *)arg))
755 if (val < (po->pppoe_dev->mtu
765 if (
get_user(val, (
int __user *)arg))
772 struct pppox_sock *relay_po;
779 if (!(sk->sk_state & PPPOX_CONNECTED))
791 if (po->pppoe_relay.sa_family !=
AF_PPPOX ||
797 relay_po = get_item_by_addr(sock_net(sk), &po->pppoe_relay);
801 sock_put(sk_pppox(relay_po));
827 struct sock *sk = sock->
sk;
828 struct pppox_sock *po = pppox_sk(sk);
836 if (sock_flag(sk,
SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) {
862 skb_reset_network_header(skb);
870 start = (
char *)&ph->
tag[0];
880 po->pppoe_pa.remote,
NULL, total_len);
898 static int __pppoe_xmit(
struct sock *sk,
struct sk_buff *skb)
900 struct pppox_sock *po = pppox_sk(sk);
913 if (sock_flag(sk,
SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
925 __skb_push(skb,
sizeof(*ph));
926 skb_reset_network_header(skb);
939 po->pppoe_pa.remote,
NULL, data_len);
957 struct sock *sk = (
struct sock *)chan->
private;
958 return __pppoe_xmit(sk, skb);
962 .start_xmit = pppoe_xmit,
965 static int pppoe_recvmsg(
struct kiocb *iocb,
struct socket *sock,
966 struct msghdr *m,
size_t total_len,
int flags)
968 struct sock *sk = sock->
sk;
985 total_len =
min_t(
size_t, total_len, skb->
len);
998 #ifdef CONFIG_PROC_FS
999 static int pppoe_seq_show(
struct seq_file *seq,
void *
v)
1001 struct pppox_sock *po;
1005 seq_puts(seq,
"Id Address Device\n");
1010 dev_name = po->pppoe_pa.dev;
1013 po->pppoe_pa.sid, po->pppoe_pa.remote, dev_name);
1018 static inline struct pppox_sock *pppoe_get_idx(
struct pppoe_net *pn, loff_t
pos)
1020 struct pppox_sock *po;
1036 static void *pppoe_seq_start(
struct seq_file *seq, loff_t *pos)
1039 struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
1046 static void *pppoe_seq_next(
struct seq_file *seq,
void *v, loff_t *pos)
1048 struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
1049 struct pppox_sock *po;
1053 po = pppoe_get_idx(pn, 0);
1060 int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
1063 while (++hash < PPPOE_HASH_SIZE) {
1074 static void pppoe_seq_stop(
struct seq_file *seq,
void *v)
1077 struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
1082 .
start = pppoe_seq_start,
1083 .next = pppoe_seq_next,
1084 .stop = pppoe_seq_stop,
1085 .show = pppoe_seq_show,
1096 .open = pppoe_seq_open,
1104 static const struct proto_ops pppoe_ops = {
1107 .release = pppoe_release,
1109 .connect = pppoe_connect,
1112 .getname = pppoe_getname,
1118 .sendmsg = pppoe_sendmsg,
1119 .recvmsg = pppoe_recvmsg,
1125 .create = pppoe_create,
1126 .ioctl = pppoe_ioctl,
1130 static __net_init int pppoe_init_net(
struct net *net)
1132 struct pppoe_net *pn = pppoe_pernet(net);
1138 #ifdef CONFIG_PROC_FS
1146 static __net_exit void pppoe_exit_net(
struct net *net)
1152 .init = pppoe_init_net,
1153 .exit = pppoe_exit_net,
1154 .id = &pppoe_net_id,
1158 static int __init pppoe_init(
void)
1168 goto out_unregister_net_ops;
1172 goto out_unregister_pppoe_proto;
1180 out_unregister_pppoe_proto:
1182 out_unregister_net_ops:
1188 static void __exit pppoe_exit(
void)