Linux pppoe代码
时间: 2023-07-09 15:31:03 浏览: 176
以下是Linux中PPPoE的相关代码:
1. PPPoE协议处理
```
struct pppox_sock {
__u16 protocol;
__u16 flags;
unsigned long state;
struct socket *sk;
pid_t pid;
struct sk_buff_head rx_queue;
struct sk_buff_head tx_queue;
struct sk_buff_head def_queue;
struct net_device *dev;
struct hlist_node node;
struct mutex mutex;
wait_queue_head_t waitq;
};
```
2. PPPoE拨号连接过程
```
pppoe_connect(struct sock *sk, struct sockaddr_pppoe *addr)
{
struct pppox_sock *po;
struct net_device *dev;
struct sock *sk2;
int err;
po = pppox_sk(sk);
if (po->dev) {
pppoe_err(po->sk, "already connected");
return -EISCONN;
}
dev = dev_get_by_name(addr->iface);
if (!dev) {
pppoe_err(po->sk, "no such device: %s", addr->iface);
return -ENODEV;
}
if (dev->flags & IFF_NOARP) {
pppoe_err(po->sk, "device %s is not ARPable", addr->iface);
dev_put(dev);
return -EINVAL;
}
sk2 = pppox_lookup(dev_net(dev), ETH_P_PPP_SES, NULL, 0);
if (!sk2) {
pppoe_err(po->sk, "no PPPoE session socket available");
dev_put(dev);
return -ECONNREFUSED;
}
po->dev = dev;
po->protocol = ETH_P_PPP_SES;
po->pid = current->pid;
err = pppoe_send_padi(po);
if (err) {
pppoe_err(po->sk, "error sending PADI, err=%d", err);
po->dev = NULL;
po->protocol = 0;
po->pid = 0;
dev_put(dev);
sock_put(sk2);
return err;
}
pppoe_info(po->sk, "sent PADI on %s", addr->iface);
po->state = PPPOX_CONNECTED;
skb_queue_head_init(&po->tx_queue);
skb_queue_head_init(&po->rx_queue);
skb_queue_head_init(&po->def_queue);
mutex_init(&po->mutex);
init_waitqueue_head(&po->waitq);
po->sk = sk2;
po->flags |= PPPOX_ROUTE;
dev_put(dev);
return 0;
}
```
3. PPPoE断开连接过程
```
pppoe_disconnect(struct sock *sk)
{
struct pppox_sock *po;
struct net_device *dev;
struct sock *sk2;
po = pppox_sk(sk);
if (!po->dev) {
pppoe_err(po->sk, "not connected");
return -ENOTCONN;
}
dev = po->dev;
sk2 = po->sk;
if (po->flags & PPPOX_ROUTE)
pppoe_remove_route(po);
po->dev = NULL;
po->protocol = 0;
po->pid = 0;
po->state = PPPOX_DISCONNECTED;
sock_put(sk2);
dev_put(dev);
skb_queue_purge(&po->tx_queue);
skb_queue_purge(&po->rx_queue);
skb_queue_purge(&po->def_queue);
return 0;
}
```
以上是Linux中PPPoE的相关代码,可以帮助理解PPPoE协议的实现过程。
阅读全文