1224cf5adSJeff Kirsher /** -*- linux-c -*- *********************************************************** 2224cf5adSJeff Kirsher * Linux PPP over Ethernet (PPPoX/PPPoE) Sockets 3224cf5adSJeff Kirsher * 4224cf5adSJeff Kirsher * PPPoX --- Generic PPP encapsulation socket family 5224cf5adSJeff Kirsher * PPPoE --- PPP over Ethernet (RFC 2516) 6224cf5adSJeff Kirsher * 7224cf5adSJeff Kirsher * 8224cf5adSJeff Kirsher * Version: 0.7.0 9224cf5adSJeff Kirsher * 10224cf5adSJeff Kirsher * 070228 : Fix to allow multiple sessions with same remote MAC and same 11224cf5adSJeff Kirsher * session id by including the local device ifindex in the 12224cf5adSJeff Kirsher * tuple identifying a session. This also ensures packets can't 13224cf5adSJeff Kirsher * be injected into a session from interfaces other than the one 14224cf5adSJeff Kirsher * specified by userspace. Florian Zumbiehl <florz@florz.de> 15224cf5adSJeff Kirsher * (Oh, BTW, this one is YYMMDD, in case you were wondering ...) 16224cf5adSJeff Kirsher * 220102 : Fix module use count on failure in pppoe_create, pppox_sk -acme 17224cf5adSJeff Kirsher * 030700 : Fixed connect logic to allow for disconnect. 18224cf5adSJeff Kirsher * 270700 : Fixed potential SMP problems; we must protect against 19224cf5adSJeff Kirsher * simultaneous invocation of ppp_input 20224cf5adSJeff Kirsher * and ppp_unregister_channel. 21224cf5adSJeff Kirsher * 040800 : Respect reference count mechanisms on net-devices. 22224cf5adSJeff Kirsher * 200800 : fix kfree(skb) in pppoe_rcv (acme) 23224cf5adSJeff Kirsher * Module reference count is decremented in the right spot now, 24224cf5adSJeff Kirsher * guards against sock_put not actually freeing the sk 25224cf5adSJeff Kirsher * in pppoe_release. 26224cf5adSJeff Kirsher * 051000 : Initialization cleanup. 27224cf5adSJeff Kirsher * 111100 : Fix recvmsg. 28224cf5adSJeff Kirsher * 050101 : Fix PADT procesing. 29224cf5adSJeff Kirsher * 140501 : Use pppoe_rcv_core to handle all backlog. (Alexey) 30224cf5adSJeff Kirsher * 170701 : Do not lock_sock with rwlock held. (DaveM) 31224cf5adSJeff Kirsher * Ignore discovery frames if user has socket 32224cf5adSJeff Kirsher * locked. (DaveM) 33224cf5adSJeff Kirsher * Ignore return value of dev_queue_xmit in __pppoe_xmit 34224cf5adSJeff Kirsher * or else we may kfree an SKB twice. (DaveM) 35224cf5adSJeff Kirsher * 190701 : When doing copies of skb's in __pppoe_xmit, always delete 36224cf5adSJeff Kirsher * the original skb that was passed in on success, never on 37224cf5adSJeff Kirsher * failure. Delete the copy of the skb on failure to avoid 38224cf5adSJeff Kirsher * a memory leak. 39224cf5adSJeff Kirsher * 081001 : Misc. cleanup (licence string, non-blocking, prevent 40224cf5adSJeff Kirsher * reference of device on close). 41224cf5adSJeff Kirsher * 121301 : New ppp channels interface; cannot unregister a channel 42224cf5adSJeff Kirsher * from interrupts. Thus, we mark the socket as a ZOMBIE 43224cf5adSJeff Kirsher * and do the unregistration later. 44224cf5adSJeff Kirsher * 081002 : seq_file support for proc stuff -acme 45224cf5adSJeff Kirsher * 111602 : Merge all 2.4 fixes into 2.5/2.6 tree. Label 2.5/2.6 46224cf5adSJeff Kirsher * as version 0.7. Spacing cleanup. 47224cf5adSJeff Kirsher * Author: Michal Ostrowski <mostrows@speakeasy.net> 48224cf5adSJeff Kirsher * Contributors: 49224cf5adSJeff Kirsher * Arnaldo Carvalho de Melo <acme@conectiva.com.br> 50224cf5adSJeff Kirsher * David S. Miller (davem@redhat.com) 51224cf5adSJeff Kirsher * 52224cf5adSJeff Kirsher * License: 53224cf5adSJeff Kirsher * This program is free software; you can redistribute it and/or 54224cf5adSJeff Kirsher * modify it under the terms of the GNU General Public License 55224cf5adSJeff Kirsher * as published by the Free Software Foundation; either version 56224cf5adSJeff Kirsher * 2 of the License, or (at your option) any later version. 57224cf5adSJeff Kirsher * 58224cf5adSJeff Kirsher */ 59224cf5adSJeff Kirsher 60224cf5adSJeff Kirsher #include <linux/string.h> 61224cf5adSJeff Kirsher #include <linux/module.h> 62224cf5adSJeff Kirsher #include <linux/kernel.h> 63224cf5adSJeff Kirsher #include <linux/slab.h> 64224cf5adSJeff Kirsher #include <linux/errno.h> 65224cf5adSJeff Kirsher #include <linux/netdevice.h> 66224cf5adSJeff Kirsher #include <linux/net.h> 67224cf5adSJeff Kirsher #include <linux/inetdevice.h> 68224cf5adSJeff Kirsher #include <linux/etherdevice.h> 69224cf5adSJeff Kirsher #include <linux/skbuff.h> 70224cf5adSJeff Kirsher #include <linux/init.h> 71224cf5adSJeff Kirsher #include <linux/if_ether.h> 72224cf5adSJeff Kirsher #include <linux/if_pppox.h> 73224cf5adSJeff Kirsher #include <linux/ppp_channel.h> 74224cf5adSJeff Kirsher #include <linux/ppp_defs.h> 754b32da2bSPaul Mackerras #include <linux/ppp-ioctl.h> 76224cf5adSJeff Kirsher #include <linux/notifier.h> 77224cf5adSJeff Kirsher #include <linux/file.h> 78224cf5adSJeff Kirsher #include <linux/proc_fs.h> 79224cf5adSJeff Kirsher #include <linux/seq_file.h> 80224cf5adSJeff Kirsher 81224cf5adSJeff Kirsher #include <linux/nsproxy.h> 82224cf5adSJeff Kirsher #include <net/net_namespace.h> 83224cf5adSJeff Kirsher #include <net/netns/generic.h> 84224cf5adSJeff Kirsher #include <net/sock.h> 85224cf5adSJeff Kirsher 867c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 87224cf5adSJeff Kirsher 88224cf5adSJeff Kirsher #define PPPOE_HASH_BITS 4 89224cf5adSJeff Kirsher #define PPPOE_HASH_SIZE (1 << PPPOE_HASH_BITS) 90224cf5adSJeff Kirsher #define PPPOE_HASH_MASK (PPPOE_HASH_SIZE - 1) 91224cf5adSJeff Kirsher 92224cf5adSJeff Kirsher static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb); 93224cf5adSJeff Kirsher 94224cf5adSJeff Kirsher static const struct proto_ops pppoe_ops; 95224cf5adSJeff Kirsher static const struct ppp_channel_ops pppoe_chan_ops; 96224cf5adSJeff Kirsher 97224cf5adSJeff Kirsher /* per-net private data for this module */ 98c7d03a00SAlexey Dobriyan static unsigned int pppoe_net_id __read_mostly; 99224cf5adSJeff Kirsher struct pppoe_net { 100224cf5adSJeff Kirsher /* 101224cf5adSJeff Kirsher * we could use _single_ hash table for all 102224cf5adSJeff Kirsher * nets by injecting net id into the hash but 103224cf5adSJeff Kirsher * it would increase hash chains and add 104224cf5adSJeff Kirsher * a few additional math comparations messy 105224cf5adSJeff Kirsher * as well, moreover in case of SMP less locking 106224cf5adSJeff Kirsher * controversy here 107224cf5adSJeff Kirsher */ 108224cf5adSJeff Kirsher struct pppox_sock *hash_table[PPPOE_HASH_SIZE]; 109224cf5adSJeff Kirsher rwlock_t hash_lock; 110224cf5adSJeff Kirsher }; 111224cf5adSJeff Kirsher 112224cf5adSJeff Kirsher /* 113224cf5adSJeff Kirsher * PPPoE could be in the following stages: 114224cf5adSJeff Kirsher * 1) Discovery stage (to obtain remote MAC and Session ID) 115224cf5adSJeff Kirsher * 2) Session stage (MAC and SID are known) 116224cf5adSJeff Kirsher * 117224cf5adSJeff Kirsher * Ethernet frames have a special tag for this but 118224cf5adSJeff Kirsher * we use simpler approach based on session id 119224cf5adSJeff Kirsher */ 120224cf5adSJeff Kirsher static inline bool stage_session(__be16 sid) 121224cf5adSJeff Kirsher { 122224cf5adSJeff Kirsher return sid != 0; 123224cf5adSJeff Kirsher } 124224cf5adSJeff Kirsher 125224cf5adSJeff Kirsher static inline struct pppoe_net *pppoe_pernet(struct net *net) 126224cf5adSJeff Kirsher { 127224cf5adSJeff Kirsher BUG_ON(!net); 128224cf5adSJeff Kirsher 129224cf5adSJeff Kirsher return net_generic(net, pppoe_net_id); 130224cf5adSJeff Kirsher } 131224cf5adSJeff Kirsher 132224cf5adSJeff Kirsher static inline int cmp_2_addr(struct pppoe_addr *a, struct pppoe_addr *b) 133224cf5adSJeff Kirsher { 1347ed8ca5bSdingtianhong return a->sid == b->sid && ether_addr_equal(a->remote, b->remote); 135224cf5adSJeff Kirsher } 136224cf5adSJeff Kirsher 137224cf5adSJeff Kirsher static inline int cmp_addr(struct pppoe_addr *a, __be16 sid, char *addr) 138224cf5adSJeff Kirsher { 1397ed8ca5bSdingtianhong return a->sid == sid && ether_addr_equal(a->remote, addr); 140224cf5adSJeff Kirsher } 141224cf5adSJeff Kirsher 142224cf5adSJeff Kirsher #if 8 % PPPOE_HASH_BITS 143224cf5adSJeff Kirsher #error 8 must be a multiple of PPPOE_HASH_BITS 144224cf5adSJeff Kirsher #endif 145224cf5adSJeff Kirsher 146224cf5adSJeff Kirsher static int hash_item(__be16 sid, unsigned char *addr) 147224cf5adSJeff Kirsher { 148224cf5adSJeff Kirsher unsigned char hash = 0; 149224cf5adSJeff Kirsher unsigned int i; 150224cf5adSJeff Kirsher 151224cf5adSJeff Kirsher for (i = 0; i < ETH_ALEN; i++) 152224cf5adSJeff Kirsher hash ^= addr[i]; 153224cf5adSJeff Kirsher for (i = 0; i < sizeof(sid_t) * 8; i += 8) 154224cf5adSJeff Kirsher hash ^= (__force __u32)sid >> i; 155224cf5adSJeff Kirsher for (i = 8; (i >>= 1) >= PPPOE_HASH_BITS;) 156224cf5adSJeff Kirsher hash ^= hash >> i; 157224cf5adSJeff Kirsher 158224cf5adSJeff Kirsher return hash & PPPOE_HASH_MASK; 159224cf5adSJeff Kirsher } 160224cf5adSJeff Kirsher 161224cf5adSJeff Kirsher /********************************************************************** 162224cf5adSJeff Kirsher * 163224cf5adSJeff Kirsher * Set/get/delete/rehash items (internal versions) 164224cf5adSJeff Kirsher * 165224cf5adSJeff Kirsher **********************************************************************/ 166224cf5adSJeff Kirsher static struct pppox_sock *__get_item(struct pppoe_net *pn, __be16 sid, 167224cf5adSJeff Kirsher unsigned char *addr, int ifindex) 168224cf5adSJeff Kirsher { 169224cf5adSJeff Kirsher int hash = hash_item(sid, addr); 170224cf5adSJeff Kirsher struct pppox_sock *ret; 171224cf5adSJeff Kirsher 172224cf5adSJeff Kirsher ret = pn->hash_table[hash]; 173224cf5adSJeff Kirsher while (ret) { 174224cf5adSJeff Kirsher if (cmp_addr(&ret->pppoe_pa, sid, addr) && 175224cf5adSJeff Kirsher ret->pppoe_ifindex == ifindex) 176224cf5adSJeff Kirsher return ret; 177224cf5adSJeff Kirsher 178224cf5adSJeff Kirsher ret = ret->next; 179224cf5adSJeff Kirsher } 180224cf5adSJeff Kirsher 181224cf5adSJeff Kirsher return NULL; 182224cf5adSJeff Kirsher } 183224cf5adSJeff Kirsher 184224cf5adSJeff Kirsher static int __set_item(struct pppoe_net *pn, struct pppox_sock *po) 185224cf5adSJeff Kirsher { 186224cf5adSJeff Kirsher int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote); 187224cf5adSJeff Kirsher struct pppox_sock *ret; 188224cf5adSJeff Kirsher 189224cf5adSJeff Kirsher ret = pn->hash_table[hash]; 190224cf5adSJeff Kirsher while (ret) { 191224cf5adSJeff Kirsher if (cmp_2_addr(&ret->pppoe_pa, &po->pppoe_pa) && 192224cf5adSJeff Kirsher ret->pppoe_ifindex == po->pppoe_ifindex) 193224cf5adSJeff Kirsher return -EALREADY; 194224cf5adSJeff Kirsher 195224cf5adSJeff Kirsher ret = ret->next; 196224cf5adSJeff Kirsher } 197224cf5adSJeff Kirsher 198224cf5adSJeff Kirsher po->next = pn->hash_table[hash]; 199224cf5adSJeff Kirsher pn->hash_table[hash] = po; 200224cf5adSJeff Kirsher 201224cf5adSJeff Kirsher return 0; 202224cf5adSJeff Kirsher } 203224cf5adSJeff Kirsher 2043b12bb60SRami Rosen static void __delete_item(struct pppoe_net *pn, __be16 sid, 205224cf5adSJeff Kirsher char *addr, int ifindex) 206224cf5adSJeff Kirsher { 207224cf5adSJeff Kirsher int hash = hash_item(sid, addr); 208224cf5adSJeff Kirsher struct pppox_sock *ret, **src; 209224cf5adSJeff Kirsher 210224cf5adSJeff Kirsher ret = pn->hash_table[hash]; 211224cf5adSJeff Kirsher src = &pn->hash_table[hash]; 212224cf5adSJeff Kirsher 213224cf5adSJeff Kirsher while (ret) { 214224cf5adSJeff Kirsher if (cmp_addr(&ret->pppoe_pa, sid, addr) && 215224cf5adSJeff Kirsher ret->pppoe_ifindex == ifindex) { 216224cf5adSJeff Kirsher *src = ret->next; 217224cf5adSJeff Kirsher break; 218224cf5adSJeff Kirsher } 219224cf5adSJeff Kirsher 220224cf5adSJeff Kirsher src = &ret->next; 221224cf5adSJeff Kirsher ret = ret->next; 222224cf5adSJeff Kirsher } 223224cf5adSJeff Kirsher } 224224cf5adSJeff Kirsher 225224cf5adSJeff Kirsher /********************************************************************** 226224cf5adSJeff Kirsher * 227224cf5adSJeff Kirsher * Set/get/delete/rehash items 228224cf5adSJeff Kirsher * 229224cf5adSJeff Kirsher **********************************************************************/ 230224cf5adSJeff Kirsher static inline struct pppox_sock *get_item(struct pppoe_net *pn, __be16 sid, 231224cf5adSJeff Kirsher unsigned char *addr, int ifindex) 232224cf5adSJeff Kirsher { 233224cf5adSJeff Kirsher struct pppox_sock *po; 234224cf5adSJeff Kirsher 235224cf5adSJeff Kirsher read_lock_bh(&pn->hash_lock); 236224cf5adSJeff Kirsher po = __get_item(pn, sid, addr, ifindex); 237224cf5adSJeff Kirsher if (po) 238224cf5adSJeff Kirsher sock_hold(sk_pppox(po)); 239224cf5adSJeff Kirsher read_unlock_bh(&pn->hash_lock); 240224cf5adSJeff Kirsher 241224cf5adSJeff Kirsher return po; 242224cf5adSJeff Kirsher } 243224cf5adSJeff Kirsher 244224cf5adSJeff Kirsher static inline struct pppox_sock *get_item_by_addr(struct net *net, 245224cf5adSJeff Kirsher struct sockaddr_pppox *sp) 246224cf5adSJeff Kirsher { 247224cf5adSJeff Kirsher struct net_device *dev; 248224cf5adSJeff Kirsher struct pppoe_net *pn; 249224cf5adSJeff Kirsher struct pppox_sock *pppox_sock = NULL; 250224cf5adSJeff Kirsher 251224cf5adSJeff Kirsher int ifindex; 252224cf5adSJeff Kirsher 253224cf5adSJeff Kirsher rcu_read_lock(); 254224cf5adSJeff Kirsher dev = dev_get_by_name_rcu(net, sp->sa_addr.pppoe.dev); 255224cf5adSJeff Kirsher if (dev) { 256224cf5adSJeff Kirsher ifindex = dev->ifindex; 257224cf5adSJeff Kirsher pn = pppoe_pernet(net); 258224cf5adSJeff Kirsher pppox_sock = get_item(pn, sp->sa_addr.pppoe.sid, 259224cf5adSJeff Kirsher sp->sa_addr.pppoe.remote, ifindex); 260224cf5adSJeff Kirsher } 261224cf5adSJeff Kirsher rcu_read_unlock(); 262224cf5adSJeff Kirsher return pppox_sock; 263224cf5adSJeff Kirsher } 264224cf5adSJeff Kirsher 2653b12bb60SRami Rosen static inline void delete_item(struct pppoe_net *pn, __be16 sid, 266224cf5adSJeff Kirsher char *addr, int ifindex) 267224cf5adSJeff Kirsher { 268224cf5adSJeff Kirsher write_lock_bh(&pn->hash_lock); 2693b12bb60SRami Rosen __delete_item(pn, sid, addr, ifindex); 270224cf5adSJeff Kirsher write_unlock_bh(&pn->hash_lock); 271224cf5adSJeff Kirsher } 272224cf5adSJeff Kirsher 273224cf5adSJeff Kirsher /*************************************************************************** 274224cf5adSJeff Kirsher * 275224cf5adSJeff Kirsher * Handler for device events. 276224cf5adSJeff Kirsher * Certain device events require that sockets be unconnected. 277224cf5adSJeff Kirsher * 278224cf5adSJeff Kirsher **************************************************************************/ 279224cf5adSJeff Kirsher 280224cf5adSJeff Kirsher static void pppoe_flush_dev(struct net_device *dev) 281224cf5adSJeff Kirsher { 282224cf5adSJeff Kirsher struct pppoe_net *pn; 283224cf5adSJeff Kirsher int i; 284224cf5adSJeff Kirsher 285224cf5adSJeff Kirsher pn = pppoe_pernet(dev_net(dev)); 286224cf5adSJeff Kirsher write_lock_bh(&pn->hash_lock); 287224cf5adSJeff Kirsher for (i = 0; i < PPPOE_HASH_SIZE; i++) { 288224cf5adSJeff Kirsher struct pppox_sock *po = pn->hash_table[i]; 289224cf5adSJeff Kirsher struct sock *sk; 290224cf5adSJeff Kirsher 291224cf5adSJeff Kirsher while (po) { 292224cf5adSJeff Kirsher while (po && po->pppoe_dev != dev) { 293224cf5adSJeff Kirsher po = po->next; 294224cf5adSJeff Kirsher } 295224cf5adSJeff Kirsher 296224cf5adSJeff Kirsher if (!po) 297224cf5adSJeff Kirsher break; 298224cf5adSJeff Kirsher 299224cf5adSJeff Kirsher sk = sk_pppox(po); 300224cf5adSJeff Kirsher 301224cf5adSJeff Kirsher /* We always grab the socket lock, followed by the 302224cf5adSJeff Kirsher * hash_lock, in that order. Since we should hold the 303224cf5adSJeff Kirsher * sock lock while doing any unbinding, we need to 304224cf5adSJeff Kirsher * release the lock we're holding. Hold a reference to 305224cf5adSJeff Kirsher * the sock so it doesn't disappear as we're jumping 306224cf5adSJeff Kirsher * between locks. 307224cf5adSJeff Kirsher */ 308224cf5adSJeff Kirsher 309224cf5adSJeff Kirsher sock_hold(sk); 310224cf5adSJeff Kirsher write_unlock_bh(&pn->hash_lock); 311224cf5adSJeff Kirsher lock_sock(sk); 312224cf5adSJeff Kirsher 313224cf5adSJeff Kirsher if (po->pppoe_dev == dev && 314a8acce6aSGuillaume Nault sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)) { 315224cf5adSJeff Kirsher pppox_unbind_sock(sk); 316224cf5adSJeff Kirsher sk->sk_state_change(sk); 317224cf5adSJeff Kirsher po->pppoe_dev = NULL; 318224cf5adSJeff Kirsher dev_put(dev); 319224cf5adSJeff Kirsher } 320224cf5adSJeff Kirsher 321224cf5adSJeff Kirsher release_sock(sk); 322224cf5adSJeff Kirsher sock_put(sk); 323224cf5adSJeff Kirsher 324224cf5adSJeff Kirsher /* Restart the process from the start of the current 325224cf5adSJeff Kirsher * hash chain. We dropped locks so the world may have 326224cf5adSJeff Kirsher * change from underneath us. 327224cf5adSJeff Kirsher */ 328224cf5adSJeff Kirsher 329224cf5adSJeff Kirsher BUG_ON(pppoe_pernet(dev_net(dev)) == NULL); 330224cf5adSJeff Kirsher write_lock_bh(&pn->hash_lock); 331224cf5adSJeff Kirsher po = pn->hash_table[i]; 332224cf5adSJeff Kirsher } 333224cf5adSJeff Kirsher } 334224cf5adSJeff Kirsher write_unlock_bh(&pn->hash_lock); 335224cf5adSJeff Kirsher } 336224cf5adSJeff Kirsher 337224cf5adSJeff Kirsher static int pppoe_device_event(struct notifier_block *this, 338224cf5adSJeff Kirsher unsigned long event, void *ptr) 339224cf5adSJeff Kirsher { 340351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 341224cf5adSJeff Kirsher 342224cf5adSJeff Kirsher /* Only look at sockets that are using this specific device. */ 343224cf5adSJeff Kirsher switch (event) { 344224cf5adSJeff Kirsher case NETDEV_CHANGEADDR: 345224cf5adSJeff Kirsher case NETDEV_CHANGEMTU: 346224cf5adSJeff Kirsher /* A change in mtu or address is a bad thing, requiring 347224cf5adSJeff Kirsher * LCP re-negotiation. 348224cf5adSJeff Kirsher */ 349224cf5adSJeff Kirsher 350224cf5adSJeff Kirsher case NETDEV_GOING_DOWN: 351224cf5adSJeff Kirsher case NETDEV_DOWN: 352224cf5adSJeff Kirsher /* Find every socket on this device and kill it. */ 353224cf5adSJeff Kirsher pppoe_flush_dev(dev); 354224cf5adSJeff Kirsher break; 355224cf5adSJeff Kirsher 356224cf5adSJeff Kirsher default: 357224cf5adSJeff Kirsher break; 358224cf5adSJeff Kirsher } 359224cf5adSJeff Kirsher 360224cf5adSJeff Kirsher return NOTIFY_DONE; 361224cf5adSJeff Kirsher } 362224cf5adSJeff Kirsher 363224cf5adSJeff Kirsher static struct notifier_block pppoe_notifier = { 364224cf5adSJeff Kirsher .notifier_call = pppoe_device_event, 365224cf5adSJeff Kirsher }; 366224cf5adSJeff Kirsher 367224cf5adSJeff Kirsher /************************************************************************ 368224cf5adSJeff Kirsher * 369224cf5adSJeff Kirsher * Do the real work of receiving a PPPoE Session frame. 370224cf5adSJeff Kirsher * 371224cf5adSJeff Kirsher ***********************************************************************/ 372224cf5adSJeff Kirsher static int pppoe_rcv_core(struct sock *sk, struct sk_buff *skb) 373224cf5adSJeff Kirsher { 374224cf5adSJeff Kirsher struct pppox_sock *po = pppox_sk(sk); 375224cf5adSJeff Kirsher struct pppox_sock *relay_po; 376224cf5adSJeff Kirsher 377224cf5adSJeff Kirsher /* Backlog receive. Semantics of backlog rcv preclude any code from 378224cf5adSJeff Kirsher * executing in lock_sock()/release_sock() bounds; meaning sk->sk_state 379224cf5adSJeff Kirsher * can't change. 380224cf5adSJeff Kirsher */ 381224cf5adSJeff Kirsher 382a068833bSJoakim Tjernlund if (skb->pkt_type == PACKET_OTHERHOST) 383a068833bSJoakim Tjernlund goto abort_kfree; 384a068833bSJoakim Tjernlund 385224cf5adSJeff Kirsher if (sk->sk_state & PPPOX_BOUND) { 386224cf5adSJeff Kirsher ppp_input(&po->chan, skb); 387224cf5adSJeff Kirsher } else if (sk->sk_state & PPPOX_RELAY) { 388224cf5adSJeff Kirsher relay_po = get_item_by_addr(sock_net(sk), 389224cf5adSJeff Kirsher &po->pppoe_relay); 390224cf5adSJeff Kirsher if (relay_po == NULL) 391224cf5adSJeff Kirsher goto abort_kfree; 392224cf5adSJeff Kirsher 393224cf5adSJeff Kirsher if ((sk_pppox(relay_po)->sk_state & PPPOX_CONNECTED) == 0) 394224cf5adSJeff Kirsher goto abort_put; 395224cf5adSJeff Kirsher 396224cf5adSJeff Kirsher if (!__pppoe_xmit(sk_pppox(relay_po), skb)) 397224cf5adSJeff Kirsher goto abort_put; 39829e73269SGuillaume Nault 39929e73269SGuillaume Nault sock_put(sk_pppox(relay_po)); 400224cf5adSJeff Kirsher } else { 401224cf5adSJeff Kirsher if (sock_queue_rcv_skb(sk, skb)) 402224cf5adSJeff Kirsher goto abort_kfree; 403224cf5adSJeff Kirsher } 404224cf5adSJeff Kirsher 405224cf5adSJeff Kirsher return NET_RX_SUCCESS; 406224cf5adSJeff Kirsher 407224cf5adSJeff Kirsher abort_put: 408224cf5adSJeff Kirsher sock_put(sk_pppox(relay_po)); 409224cf5adSJeff Kirsher 410224cf5adSJeff Kirsher abort_kfree: 411224cf5adSJeff Kirsher kfree_skb(skb); 412224cf5adSJeff Kirsher return NET_RX_DROP; 413224cf5adSJeff Kirsher } 414224cf5adSJeff Kirsher 415224cf5adSJeff Kirsher /************************************************************************ 416224cf5adSJeff Kirsher * 417224cf5adSJeff Kirsher * Receive wrapper called in BH context. 418224cf5adSJeff Kirsher * 419224cf5adSJeff Kirsher ***********************************************************************/ 420224cf5adSJeff Kirsher static int pppoe_rcv(struct sk_buff *skb, struct net_device *dev, 421224cf5adSJeff Kirsher struct packet_type *pt, struct net_device *orig_dev) 422224cf5adSJeff Kirsher { 423224cf5adSJeff Kirsher struct pppoe_hdr *ph; 424224cf5adSJeff Kirsher struct pppox_sock *po; 425224cf5adSJeff Kirsher struct pppoe_net *pn; 426224cf5adSJeff Kirsher int len; 427224cf5adSJeff Kirsher 428224cf5adSJeff Kirsher skb = skb_share_check(skb, GFP_ATOMIC); 429224cf5adSJeff Kirsher if (!skb) 430224cf5adSJeff Kirsher goto out; 431224cf5adSJeff Kirsher 432224cf5adSJeff Kirsher if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr))) 433224cf5adSJeff Kirsher goto drop; 434224cf5adSJeff Kirsher 435224cf5adSJeff Kirsher ph = pppoe_hdr(skb); 436224cf5adSJeff Kirsher len = ntohs(ph->length); 437224cf5adSJeff Kirsher 438224cf5adSJeff Kirsher skb_pull_rcsum(skb, sizeof(*ph)); 439224cf5adSJeff Kirsher if (skb->len < len) 440224cf5adSJeff Kirsher goto drop; 441224cf5adSJeff Kirsher 442224cf5adSJeff Kirsher if (pskb_trim_rcsum(skb, len)) 443224cf5adSJeff Kirsher goto drop; 444224cf5adSJeff Kirsher 445224cf5adSJeff Kirsher pn = pppoe_pernet(dev_net(dev)); 446224cf5adSJeff Kirsher 447224cf5adSJeff Kirsher /* Note that get_item does a sock_hold(), so sk_pppox(po) 448224cf5adSJeff Kirsher * is known to be safe. 449224cf5adSJeff Kirsher */ 450224cf5adSJeff Kirsher po = get_item(pn, ph->sid, eth_hdr(skb)->h_source, dev->ifindex); 451224cf5adSJeff Kirsher if (!po) 452224cf5adSJeff Kirsher goto drop; 453224cf5adSJeff Kirsher 454224cf5adSJeff Kirsher return sk_receive_skb(sk_pppox(po), skb, 0); 455224cf5adSJeff Kirsher 456224cf5adSJeff Kirsher drop: 457224cf5adSJeff Kirsher kfree_skb(skb); 458224cf5adSJeff Kirsher out: 459224cf5adSJeff Kirsher return NET_RX_DROP; 460224cf5adSJeff Kirsher } 461224cf5adSJeff Kirsher 462287f3a94SSimon Farnsworth static void pppoe_unbind_sock_work(struct work_struct *work) 463287f3a94SSimon Farnsworth { 464287f3a94SSimon Farnsworth struct pppox_sock *po = container_of(work, struct pppox_sock, 465287f3a94SSimon Farnsworth proto.pppoe.padt_work); 466287f3a94SSimon Farnsworth struct sock *sk = sk_pppox(po); 467287f3a94SSimon Farnsworth 468287f3a94SSimon Farnsworth lock_sock(sk); 469665a6cd8SFelix Fietkau if (po->pppoe_dev) { 470665a6cd8SFelix Fietkau dev_put(po->pppoe_dev); 471665a6cd8SFelix Fietkau po->pppoe_dev = NULL; 472665a6cd8SFelix Fietkau } 473287f3a94SSimon Farnsworth pppox_unbind_sock(sk); 474287f3a94SSimon Farnsworth release_sock(sk); 475287f3a94SSimon Farnsworth sock_put(sk); 476287f3a94SSimon Farnsworth } 477287f3a94SSimon Farnsworth 478224cf5adSJeff Kirsher /************************************************************************ 479224cf5adSJeff Kirsher * 480224cf5adSJeff Kirsher * Receive a PPPoE Discovery frame. 481224cf5adSJeff Kirsher * This is solely for detection of PADT frames 482224cf5adSJeff Kirsher * 483224cf5adSJeff Kirsher ***********************************************************************/ 484224cf5adSJeff Kirsher static int pppoe_disc_rcv(struct sk_buff *skb, struct net_device *dev, 485224cf5adSJeff Kirsher struct packet_type *pt, struct net_device *orig_dev) 486224cf5adSJeff Kirsher 487224cf5adSJeff Kirsher { 488224cf5adSJeff Kirsher struct pppoe_hdr *ph; 489224cf5adSJeff Kirsher struct pppox_sock *po; 490224cf5adSJeff Kirsher struct pppoe_net *pn; 491224cf5adSJeff Kirsher 492224cf5adSJeff Kirsher skb = skb_share_check(skb, GFP_ATOMIC); 493224cf5adSJeff Kirsher if (!skb) 494224cf5adSJeff Kirsher goto out; 495224cf5adSJeff Kirsher 496224cf5adSJeff Kirsher if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr))) 497224cf5adSJeff Kirsher goto abort; 498224cf5adSJeff Kirsher 499224cf5adSJeff Kirsher ph = pppoe_hdr(skb); 500224cf5adSJeff Kirsher if (ph->code != PADT_CODE) 501224cf5adSJeff Kirsher goto abort; 502224cf5adSJeff Kirsher 503224cf5adSJeff Kirsher pn = pppoe_pernet(dev_net(dev)); 504224cf5adSJeff Kirsher po = get_item(pn, ph->sid, eth_hdr(skb)->h_source, dev->ifindex); 5058734e485SGuillaume Nault if (po) 506287f3a94SSimon Farnsworth if (!schedule_work(&po->proto.pppoe.padt_work)) 5078734e485SGuillaume Nault sock_put(sk_pppox(po)); 508224cf5adSJeff Kirsher 509224cf5adSJeff Kirsher abort: 510224cf5adSJeff Kirsher kfree_skb(skb); 511224cf5adSJeff Kirsher out: 512224cf5adSJeff Kirsher return NET_RX_SUCCESS; /* Lies... :-) */ 513224cf5adSJeff Kirsher } 514224cf5adSJeff Kirsher 515224cf5adSJeff Kirsher static struct packet_type pppoes_ptype __read_mostly = { 516224cf5adSJeff Kirsher .type = cpu_to_be16(ETH_P_PPP_SES), 517224cf5adSJeff Kirsher .func = pppoe_rcv, 518224cf5adSJeff Kirsher }; 519224cf5adSJeff Kirsher 520224cf5adSJeff Kirsher static struct packet_type pppoed_ptype __read_mostly = { 521224cf5adSJeff Kirsher .type = cpu_to_be16(ETH_P_PPP_DISC), 522224cf5adSJeff Kirsher .func = pppoe_disc_rcv, 523224cf5adSJeff Kirsher }; 524224cf5adSJeff Kirsher 525224cf5adSJeff Kirsher static struct proto pppoe_sk_proto __read_mostly = { 526224cf5adSJeff Kirsher .name = "PPPOE", 527224cf5adSJeff Kirsher .owner = THIS_MODULE, 528224cf5adSJeff Kirsher .obj_size = sizeof(struct pppox_sock), 529224cf5adSJeff Kirsher }; 530224cf5adSJeff Kirsher 531224cf5adSJeff Kirsher /*********************************************************************** 532224cf5adSJeff Kirsher * 533224cf5adSJeff Kirsher * Initialize a new struct sock. 534224cf5adSJeff Kirsher * 535224cf5adSJeff Kirsher **********************************************************************/ 53611aa9c28SEric W. Biederman static int pppoe_create(struct net *net, struct socket *sock, int kern) 537224cf5adSJeff Kirsher { 538224cf5adSJeff Kirsher struct sock *sk; 539224cf5adSJeff Kirsher 54011aa9c28SEric W. Biederman sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppoe_sk_proto, kern); 541224cf5adSJeff Kirsher if (!sk) 542224cf5adSJeff Kirsher return -ENOMEM; 543224cf5adSJeff Kirsher 544224cf5adSJeff Kirsher sock_init_data(sock, sk); 545224cf5adSJeff Kirsher 546224cf5adSJeff Kirsher sock->state = SS_UNCONNECTED; 547224cf5adSJeff Kirsher sock->ops = &pppoe_ops; 548224cf5adSJeff Kirsher 549224cf5adSJeff Kirsher sk->sk_backlog_rcv = pppoe_rcv_core; 550224cf5adSJeff Kirsher sk->sk_state = PPPOX_NONE; 551224cf5adSJeff Kirsher sk->sk_type = SOCK_STREAM; 552224cf5adSJeff Kirsher sk->sk_family = PF_PPPOX; 553224cf5adSJeff Kirsher sk->sk_protocol = PX_PROTO_OE; 554224cf5adSJeff Kirsher 555fe53985aSGuillaume Nault INIT_WORK(&pppox_sk(sk)->proto.pppoe.padt_work, 556fe53985aSGuillaume Nault pppoe_unbind_sock_work); 557fe53985aSGuillaume Nault 558224cf5adSJeff Kirsher return 0; 559224cf5adSJeff Kirsher } 560224cf5adSJeff Kirsher 561224cf5adSJeff Kirsher static int pppoe_release(struct socket *sock) 562224cf5adSJeff Kirsher { 563224cf5adSJeff Kirsher struct sock *sk = sock->sk; 564224cf5adSJeff Kirsher struct pppox_sock *po; 565224cf5adSJeff Kirsher struct pppoe_net *pn; 566224cf5adSJeff Kirsher struct net *net = NULL; 567224cf5adSJeff Kirsher 568224cf5adSJeff Kirsher if (!sk) 569224cf5adSJeff Kirsher return 0; 570224cf5adSJeff Kirsher 571224cf5adSJeff Kirsher lock_sock(sk); 572224cf5adSJeff Kirsher if (sock_flag(sk, SOCK_DEAD)) { 573224cf5adSJeff Kirsher release_sock(sk); 574224cf5adSJeff Kirsher return -EBADF; 575224cf5adSJeff Kirsher } 576224cf5adSJeff Kirsher 577224cf5adSJeff Kirsher po = pppox_sk(sk); 578224cf5adSJeff Kirsher 5791acea4f6SGuillaume Nault if (po->pppoe_dev) { 580224cf5adSJeff Kirsher dev_put(po->pppoe_dev); 581224cf5adSJeff Kirsher po->pppoe_dev = NULL; 582224cf5adSJeff Kirsher } 583224cf5adSJeff Kirsher 584224cf5adSJeff Kirsher pppox_unbind_sock(sk); 585224cf5adSJeff Kirsher 586224cf5adSJeff Kirsher /* Signal the death of the socket. */ 587224cf5adSJeff Kirsher sk->sk_state = PPPOX_DEAD; 588224cf5adSJeff Kirsher 589224cf5adSJeff Kirsher net = sock_net(sk); 590224cf5adSJeff Kirsher pn = pppoe_pernet(net); 591224cf5adSJeff Kirsher 592224cf5adSJeff Kirsher /* 593224cf5adSJeff Kirsher * protect "po" from concurrent updates 594224cf5adSJeff Kirsher * on pppoe_flush_dev 595224cf5adSJeff Kirsher */ 596224cf5adSJeff Kirsher delete_item(pn, po->pppoe_pa.sid, po->pppoe_pa.remote, 597224cf5adSJeff Kirsher po->pppoe_ifindex); 598224cf5adSJeff Kirsher 599224cf5adSJeff Kirsher sock_orphan(sk); 600224cf5adSJeff Kirsher sock->sk = NULL; 601224cf5adSJeff Kirsher 602224cf5adSJeff Kirsher skb_queue_purge(&sk->sk_receive_queue); 603224cf5adSJeff Kirsher release_sock(sk); 604224cf5adSJeff Kirsher sock_put(sk); 605224cf5adSJeff Kirsher 606224cf5adSJeff Kirsher return 0; 607224cf5adSJeff Kirsher } 608224cf5adSJeff Kirsher 609224cf5adSJeff Kirsher static int pppoe_connect(struct socket *sock, struct sockaddr *uservaddr, 610224cf5adSJeff Kirsher int sockaddr_len, int flags) 611224cf5adSJeff Kirsher { 612224cf5adSJeff Kirsher struct sock *sk = sock->sk; 613224cf5adSJeff Kirsher struct sockaddr_pppox *sp = (struct sockaddr_pppox *)uservaddr; 614224cf5adSJeff Kirsher struct pppox_sock *po = pppox_sk(sk); 615224cf5adSJeff Kirsher struct net_device *dev = NULL; 616224cf5adSJeff Kirsher struct pppoe_net *pn; 617224cf5adSJeff Kirsher struct net *net = NULL; 618224cf5adSJeff Kirsher int error; 619224cf5adSJeff Kirsher 620224cf5adSJeff Kirsher lock_sock(sk); 621224cf5adSJeff Kirsher 622224cf5adSJeff Kirsher error = -EINVAL; 623a49e2f5dSGuillaume Nault 624a49e2f5dSGuillaume Nault if (sockaddr_len != sizeof(struct sockaddr_pppox)) 625a49e2f5dSGuillaume Nault goto end; 626a49e2f5dSGuillaume Nault 627224cf5adSJeff Kirsher if (sp->sa_protocol != PX_PROTO_OE) 628224cf5adSJeff Kirsher goto end; 629224cf5adSJeff Kirsher 630224cf5adSJeff Kirsher /* Check for already bound sockets */ 631224cf5adSJeff Kirsher error = -EBUSY; 632224cf5adSJeff Kirsher if ((sk->sk_state & PPPOX_CONNECTED) && 633224cf5adSJeff Kirsher stage_session(sp->sa_addr.pppoe.sid)) 634224cf5adSJeff Kirsher goto end; 635224cf5adSJeff Kirsher 636224cf5adSJeff Kirsher /* Check for already disconnected sockets, on attempts to disconnect */ 637224cf5adSJeff Kirsher error = -EALREADY; 638224cf5adSJeff Kirsher if ((sk->sk_state & PPPOX_DEAD) && 639224cf5adSJeff Kirsher !stage_session(sp->sa_addr.pppoe.sid)) 640224cf5adSJeff Kirsher goto end; 641224cf5adSJeff Kirsher 642224cf5adSJeff Kirsher error = 0; 643224cf5adSJeff Kirsher 644224cf5adSJeff Kirsher /* Delete the old binding */ 645224cf5adSJeff Kirsher if (stage_session(po->pppoe_pa.sid)) { 646224cf5adSJeff Kirsher pppox_unbind_sock(sk); 647224cf5adSJeff Kirsher pn = pppoe_pernet(sock_net(sk)); 648224cf5adSJeff Kirsher delete_item(pn, po->pppoe_pa.sid, 649224cf5adSJeff Kirsher po->pppoe_pa.remote, po->pppoe_ifindex); 650224cf5adSJeff Kirsher if (po->pppoe_dev) { 651224cf5adSJeff Kirsher dev_put(po->pppoe_dev); 652224cf5adSJeff Kirsher po->pppoe_dev = NULL; 653224cf5adSJeff Kirsher } 654224cf5adSJeff Kirsher 655fe53985aSGuillaume Nault po->pppoe_ifindex = 0; 656fe53985aSGuillaume Nault memset(&po->pppoe_pa, 0, sizeof(po->pppoe_pa)); 657fe53985aSGuillaume Nault memset(&po->pppoe_relay, 0, sizeof(po->pppoe_relay)); 658fe53985aSGuillaume Nault memset(&po->chan, 0, sizeof(po->chan)); 659fe53985aSGuillaume Nault po->next = NULL; 660fe53985aSGuillaume Nault po->num = 0; 661fe53985aSGuillaume Nault 662224cf5adSJeff Kirsher sk->sk_state = PPPOX_NONE; 663224cf5adSJeff Kirsher } 664224cf5adSJeff Kirsher 665224cf5adSJeff Kirsher /* Re-bind in session stage only */ 666224cf5adSJeff Kirsher if (stage_session(sp->sa_addr.pppoe.sid)) { 667224cf5adSJeff Kirsher error = -ENODEV; 668224cf5adSJeff Kirsher net = sock_net(sk); 669224cf5adSJeff Kirsher dev = dev_get_by_name(net, sp->sa_addr.pppoe.dev); 670224cf5adSJeff Kirsher if (!dev) 671224cf5adSJeff Kirsher goto err_put; 672224cf5adSJeff Kirsher 673224cf5adSJeff Kirsher po->pppoe_dev = dev; 674224cf5adSJeff Kirsher po->pppoe_ifindex = dev->ifindex; 675224cf5adSJeff Kirsher pn = pppoe_pernet(net); 676224cf5adSJeff Kirsher if (!(dev->flags & IFF_UP)) { 677224cf5adSJeff Kirsher goto err_put; 678224cf5adSJeff Kirsher } 679224cf5adSJeff Kirsher 680224cf5adSJeff Kirsher memcpy(&po->pppoe_pa, 681224cf5adSJeff Kirsher &sp->sa_addr.pppoe, 682224cf5adSJeff Kirsher sizeof(struct pppoe_addr)); 683224cf5adSJeff Kirsher 684224cf5adSJeff Kirsher write_lock_bh(&pn->hash_lock); 685224cf5adSJeff Kirsher error = __set_item(pn, po); 686224cf5adSJeff Kirsher write_unlock_bh(&pn->hash_lock); 687224cf5adSJeff Kirsher if (error < 0) 688224cf5adSJeff Kirsher goto err_put; 689224cf5adSJeff Kirsher 690224cf5adSJeff Kirsher po->chan.hdrlen = (sizeof(struct pppoe_hdr) + 691224cf5adSJeff Kirsher dev->hard_header_len); 692224cf5adSJeff Kirsher 693a8a3e41cSChristoph Schulz po->chan.mtu = dev->mtu - sizeof(struct pppoe_hdr) - 2; 694224cf5adSJeff Kirsher po->chan.private = sk; 695224cf5adSJeff Kirsher po->chan.ops = &pppoe_chan_ops; 696224cf5adSJeff Kirsher 697224cf5adSJeff Kirsher error = ppp_register_net_channel(dev_net(dev), &po->chan); 698224cf5adSJeff Kirsher if (error) { 699224cf5adSJeff Kirsher delete_item(pn, po->pppoe_pa.sid, 700224cf5adSJeff Kirsher po->pppoe_pa.remote, po->pppoe_ifindex); 701224cf5adSJeff Kirsher goto err_put; 702224cf5adSJeff Kirsher } 703224cf5adSJeff Kirsher 704224cf5adSJeff Kirsher sk->sk_state = PPPOX_CONNECTED; 705224cf5adSJeff Kirsher } 706224cf5adSJeff Kirsher 707224cf5adSJeff Kirsher po->num = sp->sa_addr.pppoe.sid; 708224cf5adSJeff Kirsher 709224cf5adSJeff Kirsher end: 710224cf5adSJeff Kirsher release_sock(sk); 711224cf5adSJeff Kirsher return error; 712224cf5adSJeff Kirsher err_put: 713224cf5adSJeff Kirsher if (po->pppoe_dev) { 714224cf5adSJeff Kirsher dev_put(po->pppoe_dev); 715224cf5adSJeff Kirsher po->pppoe_dev = NULL; 716224cf5adSJeff Kirsher } 717224cf5adSJeff Kirsher goto end; 718224cf5adSJeff Kirsher } 719224cf5adSJeff Kirsher 720224cf5adSJeff Kirsher static int pppoe_getname(struct socket *sock, struct sockaddr *uaddr, 7219b2c45d4SDenys Vlasenko int peer) 722224cf5adSJeff Kirsher { 723224cf5adSJeff Kirsher int len = sizeof(struct sockaddr_pppox); 724224cf5adSJeff Kirsher struct sockaddr_pppox sp; 725224cf5adSJeff Kirsher 726224cf5adSJeff Kirsher sp.sa_family = AF_PPPOX; 727224cf5adSJeff Kirsher sp.sa_protocol = PX_PROTO_OE; 728224cf5adSJeff Kirsher memcpy(&sp.sa_addr.pppoe, &pppox_sk(sock->sk)->pppoe_pa, 729224cf5adSJeff Kirsher sizeof(struct pppoe_addr)); 730224cf5adSJeff Kirsher 731224cf5adSJeff Kirsher memcpy(uaddr, &sp, len); 732224cf5adSJeff Kirsher 7339b2c45d4SDenys Vlasenko return len; 734224cf5adSJeff Kirsher } 735224cf5adSJeff Kirsher 736224cf5adSJeff Kirsher static int pppoe_ioctl(struct socket *sock, unsigned int cmd, 737224cf5adSJeff Kirsher unsigned long arg) 738224cf5adSJeff Kirsher { 739224cf5adSJeff Kirsher struct sock *sk = sock->sk; 740224cf5adSJeff Kirsher struct pppox_sock *po = pppox_sk(sk); 741224cf5adSJeff Kirsher int val; 742224cf5adSJeff Kirsher int err; 743224cf5adSJeff Kirsher 744224cf5adSJeff Kirsher switch (cmd) { 745224cf5adSJeff Kirsher case PPPIOCGMRU: 746224cf5adSJeff Kirsher err = -ENXIO; 747224cf5adSJeff Kirsher if (!(sk->sk_state & PPPOX_CONNECTED)) 748224cf5adSJeff Kirsher break; 749224cf5adSJeff Kirsher 750224cf5adSJeff Kirsher err = -EFAULT; 751224cf5adSJeff Kirsher if (put_user(po->pppoe_dev->mtu - 752224cf5adSJeff Kirsher sizeof(struct pppoe_hdr) - 753224cf5adSJeff Kirsher PPP_HDRLEN, 754224cf5adSJeff Kirsher (int __user *)arg)) 755224cf5adSJeff Kirsher break; 756224cf5adSJeff Kirsher err = 0; 757224cf5adSJeff Kirsher break; 758224cf5adSJeff Kirsher 759224cf5adSJeff Kirsher case PPPIOCSMRU: 760224cf5adSJeff Kirsher err = -ENXIO; 761224cf5adSJeff Kirsher if (!(sk->sk_state & PPPOX_CONNECTED)) 762224cf5adSJeff Kirsher break; 763224cf5adSJeff Kirsher 764224cf5adSJeff Kirsher err = -EFAULT; 765224cf5adSJeff Kirsher if (get_user(val, (int __user *)arg)) 766224cf5adSJeff Kirsher break; 767224cf5adSJeff Kirsher 768224cf5adSJeff Kirsher if (val < (po->pppoe_dev->mtu 769224cf5adSJeff Kirsher - sizeof(struct pppoe_hdr) 770224cf5adSJeff Kirsher - PPP_HDRLEN)) 771224cf5adSJeff Kirsher err = 0; 772224cf5adSJeff Kirsher else 773224cf5adSJeff Kirsher err = -EINVAL; 774224cf5adSJeff Kirsher break; 775224cf5adSJeff Kirsher 776224cf5adSJeff Kirsher case PPPIOCSFLAGS: 777224cf5adSJeff Kirsher err = -EFAULT; 778224cf5adSJeff Kirsher if (get_user(val, (int __user *)arg)) 779224cf5adSJeff Kirsher break; 780224cf5adSJeff Kirsher err = 0; 781224cf5adSJeff Kirsher break; 782224cf5adSJeff Kirsher 783224cf5adSJeff Kirsher case PPPOEIOCSFWD: 784224cf5adSJeff Kirsher { 785224cf5adSJeff Kirsher struct pppox_sock *relay_po; 786224cf5adSJeff Kirsher 787224cf5adSJeff Kirsher err = -EBUSY; 788a8acce6aSGuillaume Nault if (sk->sk_state & (PPPOX_BOUND | PPPOX_DEAD)) 789224cf5adSJeff Kirsher break; 790224cf5adSJeff Kirsher 791224cf5adSJeff Kirsher err = -ENOTCONN; 792224cf5adSJeff Kirsher if (!(sk->sk_state & PPPOX_CONNECTED)) 793224cf5adSJeff Kirsher break; 794224cf5adSJeff Kirsher 795224cf5adSJeff Kirsher /* PPPoE address from the user specifies an outbound 796224cf5adSJeff Kirsher PPPoE address which frames are forwarded to */ 797224cf5adSJeff Kirsher err = -EFAULT; 798224cf5adSJeff Kirsher if (copy_from_user(&po->pppoe_relay, 799224cf5adSJeff Kirsher (void __user *)arg, 800224cf5adSJeff Kirsher sizeof(struct sockaddr_pppox))) 801224cf5adSJeff Kirsher break; 802224cf5adSJeff Kirsher 803224cf5adSJeff Kirsher err = -EINVAL; 804224cf5adSJeff Kirsher if (po->pppoe_relay.sa_family != AF_PPPOX || 805224cf5adSJeff Kirsher po->pppoe_relay.sa_protocol != PX_PROTO_OE) 806224cf5adSJeff Kirsher break; 807224cf5adSJeff Kirsher 808224cf5adSJeff Kirsher /* Check that the socket referenced by the address 809224cf5adSJeff Kirsher actually exists. */ 810224cf5adSJeff Kirsher relay_po = get_item_by_addr(sock_net(sk), &po->pppoe_relay); 811224cf5adSJeff Kirsher if (!relay_po) 812224cf5adSJeff Kirsher break; 813224cf5adSJeff Kirsher 814224cf5adSJeff Kirsher sock_put(sk_pppox(relay_po)); 815224cf5adSJeff Kirsher sk->sk_state |= PPPOX_RELAY; 816224cf5adSJeff Kirsher err = 0; 817224cf5adSJeff Kirsher break; 818224cf5adSJeff Kirsher } 819224cf5adSJeff Kirsher 820224cf5adSJeff Kirsher case PPPOEIOCDFWD: 821224cf5adSJeff Kirsher err = -EALREADY; 822224cf5adSJeff Kirsher if (!(sk->sk_state & PPPOX_RELAY)) 823224cf5adSJeff Kirsher break; 824224cf5adSJeff Kirsher 825224cf5adSJeff Kirsher sk->sk_state &= ~PPPOX_RELAY; 826224cf5adSJeff Kirsher err = 0; 827224cf5adSJeff Kirsher break; 828224cf5adSJeff Kirsher 829224cf5adSJeff Kirsher default: 830224cf5adSJeff Kirsher err = -ENOTTY; 831224cf5adSJeff Kirsher } 832224cf5adSJeff Kirsher 833224cf5adSJeff Kirsher return err; 834224cf5adSJeff Kirsher } 835224cf5adSJeff Kirsher 8361b784140SYing Xue static int pppoe_sendmsg(struct socket *sock, struct msghdr *m, 8371b784140SYing Xue size_t total_len) 838224cf5adSJeff Kirsher { 839224cf5adSJeff Kirsher struct sk_buff *skb; 840224cf5adSJeff Kirsher struct sock *sk = sock->sk; 841224cf5adSJeff Kirsher struct pppox_sock *po = pppox_sk(sk); 842224cf5adSJeff Kirsher int error; 843224cf5adSJeff Kirsher struct pppoe_hdr hdr; 844224cf5adSJeff Kirsher struct pppoe_hdr *ph; 845224cf5adSJeff Kirsher struct net_device *dev; 846224cf5adSJeff Kirsher char *start; 84702612bb0SGuillaume Nault int hlen; 848224cf5adSJeff Kirsher 849224cf5adSJeff Kirsher lock_sock(sk); 850224cf5adSJeff Kirsher if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) { 851224cf5adSJeff Kirsher error = -ENOTCONN; 852224cf5adSJeff Kirsher goto end; 853224cf5adSJeff Kirsher } 854224cf5adSJeff Kirsher 855224cf5adSJeff Kirsher hdr.ver = 1; 856224cf5adSJeff Kirsher hdr.type = 1; 857224cf5adSJeff Kirsher hdr.code = 0; 858224cf5adSJeff Kirsher hdr.sid = po->num; 859224cf5adSJeff Kirsher 860224cf5adSJeff Kirsher dev = po->pppoe_dev; 861224cf5adSJeff Kirsher 862224cf5adSJeff Kirsher error = -EMSGSIZE; 863224cf5adSJeff Kirsher if (total_len > (dev->mtu + dev->hard_header_len)) 864224cf5adSJeff Kirsher goto end; 865224cf5adSJeff Kirsher 86602612bb0SGuillaume Nault hlen = LL_RESERVED_SPACE(dev); 86702612bb0SGuillaume Nault skb = sock_wmalloc(sk, hlen + sizeof(*ph) + total_len + 86802612bb0SGuillaume Nault dev->needed_tailroom, 0, GFP_KERNEL); 869224cf5adSJeff Kirsher if (!skb) { 870224cf5adSJeff Kirsher error = -ENOMEM; 871224cf5adSJeff Kirsher goto end; 872224cf5adSJeff Kirsher } 873224cf5adSJeff Kirsher 874224cf5adSJeff Kirsher /* Reserve space for headers. */ 87502612bb0SGuillaume Nault skb_reserve(skb, hlen); 876224cf5adSJeff Kirsher skb_reset_network_header(skb); 877224cf5adSJeff Kirsher 878224cf5adSJeff Kirsher skb->dev = dev; 879224cf5adSJeff Kirsher 880224cf5adSJeff Kirsher skb->priority = sk->sk_priority; 881224cf5adSJeff Kirsher skb->protocol = cpu_to_be16(ETH_P_PPP_SES); 882224cf5adSJeff Kirsher 8834df864c1SJohannes Berg ph = skb_put(skb, total_len + sizeof(struct pppoe_hdr)); 884224cf5adSJeff Kirsher start = (char *)&ph->tag[0]; 885224cf5adSJeff Kirsher 8866ce8e9ceSAl Viro error = memcpy_from_msg(start, m, total_len); 887224cf5adSJeff Kirsher if (error < 0) { 888224cf5adSJeff Kirsher kfree_skb(skb); 889224cf5adSJeff Kirsher goto end; 890224cf5adSJeff Kirsher } 891224cf5adSJeff Kirsher 892224cf5adSJeff Kirsher error = total_len; 893224cf5adSJeff Kirsher dev_hard_header(skb, dev, ETH_P_PPP_SES, 894224cf5adSJeff Kirsher po->pppoe_pa.remote, NULL, total_len); 895224cf5adSJeff Kirsher 896224cf5adSJeff Kirsher memcpy(ph, &hdr, sizeof(struct pppoe_hdr)); 897224cf5adSJeff Kirsher 898224cf5adSJeff Kirsher ph->length = htons(total_len); 899224cf5adSJeff Kirsher 900224cf5adSJeff Kirsher dev_queue_xmit(skb); 901224cf5adSJeff Kirsher 902224cf5adSJeff Kirsher end: 903224cf5adSJeff Kirsher release_sock(sk); 904224cf5adSJeff Kirsher return error; 905224cf5adSJeff Kirsher } 906224cf5adSJeff Kirsher 907224cf5adSJeff Kirsher /************************************************************************ 908224cf5adSJeff Kirsher * 909224cf5adSJeff Kirsher * xmit function for internal use. 910224cf5adSJeff Kirsher * 911224cf5adSJeff Kirsher ***********************************************************************/ 912224cf5adSJeff Kirsher static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb) 913224cf5adSJeff Kirsher { 914224cf5adSJeff Kirsher struct pppox_sock *po = pppox_sk(sk); 915224cf5adSJeff Kirsher struct net_device *dev = po->pppoe_dev; 916224cf5adSJeff Kirsher struct pppoe_hdr *ph; 917224cf5adSJeff Kirsher int data_len = skb->len; 918224cf5adSJeff Kirsher 919224cf5adSJeff Kirsher /* The higher-level PPP code (ppp_unregister_channel()) ensures the PPP 920224cf5adSJeff Kirsher * xmit operations conclude prior to an unregistration call. Thus 921224cf5adSJeff Kirsher * sk->sk_state cannot change, so we don't need to do lock_sock(). 922224cf5adSJeff Kirsher * But, we also can't do a lock_sock since that introduces a potential 923224cf5adSJeff Kirsher * deadlock as we'd reverse the lock ordering used when calling 924224cf5adSJeff Kirsher * ppp_unregister_channel(). 925224cf5adSJeff Kirsher */ 926224cf5adSJeff Kirsher 927224cf5adSJeff Kirsher if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) 928224cf5adSJeff Kirsher goto abort; 929224cf5adSJeff Kirsher 930224cf5adSJeff Kirsher if (!dev) 931224cf5adSJeff Kirsher goto abort; 932224cf5adSJeff Kirsher 933224cf5adSJeff Kirsher /* Copy the data if there is no space for the header or if it's 934224cf5adSJeff Kirsher * read-only. 935224cf5adSJeff Kirsher */ 93602612bb0SGuillaume Nault if (skb_cow_head(skb, LL_RESERVED_SPACE(dev) + sizeof(*ph))) 937224cf5adSJeff Kirsher goto abort; 938224cf5adSJeff Kirsher 939224cf5adSJeff Kirsher __skb_push(skb, sizeof(*ph)); 940224cf5adSJeff Kirsher skb_reset_network_header(skb); 941224cf5adSJeff Kirsher 942224cf5adSJeff Kirsher ph = pppoe_hdr(skb); 943224cf5adSJeff Kirsher ph->ver = 1; 944224cf5adSJeff Kirsher ph->type = 1; 945224cf5adSJeff Kirsher ph->code = 0; 946224cf5adSJeff Kirsher ph->sid = po->num; 947224cf5adSJeff Kirsher ph->length = htons(data_len); 948224cf5adSJeff Kirsher 949224cf5adSJeff Kirsher skb->protocol = cpu_to_be16(ETH_P_PPP_SES); 950224cf5adSJeff Kirsher skb->dev = dev; 951224cf5adSJeff Kirsher 952224cf5adSJeff Kirsher dev_hard_header(skb, dev, ETH_P_PPP_SES, 953224cf5adSJeff Kirsher po->pppoe_pa.remote, NULL, data_len); 954224cf5adSJeff Kirsher 955224cf5adSJeff Kirsher dev_queue_xmit(skb); 956224cf5adSJeff Kirsher return 1; 957224cf5adSJeff Kirsher 958224cf5adSJeff Kirsher abort: 959224cf5adSJeff Kirsher kfree_skb(skb); 960224cf5adSJeff Kirsher return 1; 961224cf5adSJeff Kirsher } 962224cf5adSJeff Kirsher 963224cf5adSJeff Kirsher /************************************************************************ 964224cf5adSJeff Kirsher * 965224cf5adSJeff Kirsher * xmit function called by generic PPP driver 966224cf5adSJeff Kirsher * sends PPP frame over PPPoE socket 967224cf5adSJeff Kirsher * 968224cf5adSJeff Kirsher ***********************************************************************/ 969224cf5adSJeff Kirsher static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb) 970224cf5adSJeff Kirsher { 971224cf5adSJeff Kirsher struct sock *sk = (struct sock *)chan->private; 972224cf5adSJeff Kirsher return __pppoe_xmit(sk, skb); 973224cf5adSJeff Kirsher } 974224cf5adSJeff Kirsher 975224cf5adSJeff Kirsher static const struct ppp_channel_ops pppoe_chan_ops = { 976224cf5adSJeff Kirsher .start_xmit = pppoe_xmit, 977224cf5adSJeff Kirsher }; 978224cf5adSJeff Kirsher 9791b784140SYing Xue static int pppoe_recvmsg(struct socket *sock, struct msghdr *m, 9801b784140SYing Xue size_t total_len, int flags) 981224cf5adSJeff Kirsher { 982224cf5adSJeff Kirsher struct sock *sk = sock->sk; 983224cf5adSJeff Kirsher struct sk_buff *skb; 984224cf5adSJeff Kirsher int error = 0; 985224cf5adSJeff Kirsher 986224cf5adSJeff Kirsher if (sk->sk_state & PPPOX_BOUND) { 987224cf5adSJeff Kirsher error = -EIO; 988224cf5adSJeff Kirsher goto end; 989224cf5adSJeff Kirsher } 990224cf5adSJeff Kirsher 991224cf5adSJeff Kirsher skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, 992224cf5adSJeff Kirsher flags & MSG_DONTWAIT, &error); 993224cf5adSJeff Kirsher if (error < 0) 994224cf5adSJeff Kirsher goto end; 995224cf5adSJeff Kirsher 996224cf5adSJeff Kirsher if (skb) { 997224cf5adSJeff Kirsher total_len = min_t(size_t, total_len, skb->len); 99851f3d02bSDavid S. Miller error = skb_copy_datagram_msg(skb, 0, m, total_len); 999968d7018SEric Dumazet if (error == 0) { 1000968d7018SEric Dumazet consume_skb(skb); 1001968d7018SEric Dumazet return total_len; 1002968d7018SEric Dumazet } 1003224cf5adSJeff Kirsher } 1004224cf5adSJeff Kirsher 1005224cf5adSJeff Kirsher kfree_skb(skb); 1006224cf5adSJeff Kirsher end: 1007224cf5adSJeff Kirsher return error; 1008224cf5adSJeff Kirsher } 1009224cf5adSJeff Kirsher 1010224cf5adSJeff Kirsher #ifdef CONFIG_PROC_FS 1011224cf5adSJeff Kirsher static int pppoe_seq_show(struct seq_file *seq, void *v) 1012224cf5adSJeff Kirsher { 1013224cf5adSJeff Kirsher struct pppox_sock *po; 1014224cf5adSJeff Kirsher char *dev_name; 1015224cf5adSJeff Kirsher 1016224cf5adSJeff Kirsher if (v == SEQ_START_TOKEN) { 1017224cf5adSJeff Kirsher seq_puts(seq, "Id Address Device\n"); 1018224cf5adSJeff Kirsher goto out; 1019224cf5adSJeff Kirsher } 1020224cf5adSJeff Kirsher 1021224cf5adSJeff Kirsher po = v; 1022224cf5adSJeff Kirsher dev_name = po->pppoe_pa.dev; 1023224cf5adSJeff Kirsher 1024224cf5adSJeff Kirsher seq_printf(seq, "%08X %pM %8s\n", 1025224cf5adSJeff Kirsher po->pppoe_pa.sid, po->pppoe_pa.remote, dev_name); 1026224cf5adSJeff Kirsher out: 1027224cf5adSJeff Kirsher return 0; 1028224cf5adSJeff Kirsher } 1029224cf5adSJeff Kirsher 1030224cf5adSJeff Kirsher static inline struct pppox_sock *pppoe_get_idx(struct pppoe_net *pn, loff_t pos) 1031224cf5adSJeff Kirsher { 1032224cf5adSJeff Kirsher struct pppox_sock *po; 1033224cf5adSJeff Kirsher int i; 1034224cf5adSJeff Kirsher 1035224cf5adSJeff Kirsher for (i = 0; i < PPPOE_HASH_SIZE; i++) { 1036224cf5adSJeff Kirsher po = pn->hash_table[i]; 1037224cf5adSJeff Kirsher while (po) { 1038224cf5adSJeff Kirsher if (!pos--) 1039224cf5adSJeff Kirsher goto out; 1040224cf5adSJeff Kirsher po = po->next; 1041224cf5adSJeff Kirsher } 1042224cf5adSJeff Kirsher } 1043224cf5adSJeff Kirsher 1044224cf5adSJeff Kirsher out: 1045224cf5adSJeff Kirsher return po; 1046224cf5adSJeff Kirsher } 1047224cf5adSJeff Kirsher 1048224cf5adSJeff Kirsher static void *pppoe_seq_start(struct seq_file *seq, loff_t *pos) 1049224cf5adSJeff Kirsher __acquires(pn->hash_lock) 1050224cf5adSJeff Kirsher { 1051224cf5adSJeff Kirsher struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq)); 1052224cf5adSJeff Kirsher loff_t l = *pos; 1053224cf5adSJeff Kirsher 1054224cf5adSJeff Kirsher read_lock_bh(&pn->hash_lock); 1055224cf5adSJeff Kirsher return l ? pppoe_get_idx(pn, --l) : SEQ_START_TOKEN; 1056224cf5adSJeff Kirsher } 1057224cf5adSJeff Kirsher 1058224cf5adSJeff Kirsher static void *pppoe_seq_next(struct seq_file *seq, void *v, loff_t *pos) 1059224cf5adSJeff Kirsher { 1060224cf5adSJeff Kirsher struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq)); 1061224cf5adSJeff Kirsher struct pppox_sock *po; 1062224cf5adSJeff Kirsher 1063224cf5adSJeff Kirsher ++*pos; 1064224cf5adSJeff Kirsher if (v == SEQ_START_TOKEN) { 1065224cf5adSJeff Kirsher po = pppoe_get_idx(pn, 0); 1066224cf5adSJeff Kirsher goto out; 1067224cf5adSJeff Kirsher } 1068224cf5adSJeff Kirsher po = v; 1069224cf5adSJeff Kirsher if (po->next) 1070224cf5adSJeff Kirsher po = po->next; 1071224cf5adSJeff Kirsher else { 1072224cf5adSJeff Kirsher int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote); 1073224cf5adSJeff Kirsher 1074224cf5adSJeff Kirsher po = NULL; 1075224cf5adSJeff Kirsher while (++hash < PPPOE_HASH_SIZE) { 1076224cf5adSJeff Kirsher po = pn->hash_table[hash]; 1077224cf5adSJeff Kirsher if (po) 1078224cf5adSJeff Kirsher break; 1079224cf5adSJeff Kirsher } 1080224cf5adSJeff Kirsher } 1081224cf5adSJeff Kirsher 1082224cf5adSJeff Kirsher out: 1083224cf5adSJeff Kirsher return po; 1084224cf5adSJeff Kirsher } 1085224cf5adSJeff Kirsher 1086224cf5adSJeff Kirsher static void pppoe_seq_stop(struct seq_file *seq, void *v) 1087224cf5adSJeff Kirsher __releases(pn->hash_lock) 1088224cf5adSJeff Kirsher { 1089224cf5adSJeff Kirsher struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq)); 1090224cf5adSJeff Kirsher read_unlock_bh(&pn->hash_lock); 1091224cf5adSJeff Kirsher } 1092224cf5adSJeff Kirsher 1093224cf5adSJeff Kirsher static const struct seq_operations pppoe_seq_ops = { 1094224cf5adSJeff Kirsher .start = pppoe_seq_start, 1095224cf5adSJeff Kirsher .next = pppoe_seq_next, 1096224cf5adSJeff Kirsher .stop = pppoe_seq_stop, 1097224cf5adSJeff Kirsher .show = pppoe_seq_show, 1098224cf5adSJeff Kirsher }; 1099224cf5adSJeff Kirsher 1100224cf5adSJeff Kirsher static int pppoe_seq_open(struct inode *inode, struct file *file) 1101224cf5adSJeff Kirsher { 1102224cf5adSJeff Kirsher return seq_open_net(inode, file, &pppoe_seq_ops, 1103224cf5adSJeff Kirsher sizeof(struct seq_net_private)); 1104224cf5adSJeff Kirsher } 1105224cf5adSJeff Kirsher 1106224cf5adSJeff Kirsher static const struct file_operations pppoe_seq_fops = { 1107224cf5adSJeff Kirsher .owner = THIS_MODULE, 1108224cf5adSJeff Kirsher .open = pppoe_seq_open, 1109224cf5adSJeff Kirsher .read = seq_read, 1110224cf5adSJeff Kirsher .llseek = seq_lseek, 1111224cf5adSJeff Kirsher .release = seq_release_net, 1112224cf5adSJeff Kirsher }; 1113224cf5adSJeff Kirsher 1114224cf5adSJeff Kirsher #endif /* CONFIG_PROC_FS */ 1115224cf5adSJeff Kirsher 1116224cf5adSJeff Kirsher static const struct proto_ops pppoe_ops = { 1117224cf5adSJeff Kirsher .family = AF_PPPOX, 1118224cf5adSJeff Kirsher .owner = THIS_MODULE, 1119224cf5adSJeff Kirsher .release = pppoe_release, 1120224cf5adSJeff Kirsher .bind = sock_no_bind, 1121224cf5adSJeff Kirsher .connect = pppoe_connect, 1122224cf5adSJeff Kirsher .socketpair = sock_no_socketpair, 1123224cf5adSJeff Kirsher .accept = sock_no_accept, 1124224cf5adSJeff Kirsher .getname = pppoe_getname, 1125224cf5adSJeff Kirsher .poll = datagram_poll, 1126224cf5adSJeff Kirsher .listen = sock_no_listen, 1127224cf5adSJeff Kirsher .shutdown = sock_no_shutdown, 1128224cf5adSJeff Kirsher .setsockopt = sock_no_setsockopt, 1129224cf5adSJeff Kirsher .getsockopt = sock_no_getsockopt, 1130224cf5adSJeff Kirsher .sendmsg = pppoe_sendmsg, 1131224cf5adSJeff Kirsher .recvmsg = pppoe_recvmsg, 1132224cf5adSJeff Kirsher .mmap = sock_no_mmap, 1133224cf5adSJeff Kirsher .ioctl = pppox_ioctl, 1134224cf5adSJeff Kirsher }; 1135224cf5adSJeff Kirsher 1136224cf5adSJeff Kirsher static const struct pppox_proto pppoe_proto = { 1137224cf5adSJeff Kirsher .create = pppoe_create, 1138224cf5adSJeff Kirsher .ioctl = pppoe_ioctl, 1139224cf5adSJeff Kirsher .owner = THIS_MODULE, 1140224cf5adSJeff Kirsher }; 1141224cf5adSJeff Kirsher 1142224cf5adSJeff Kirsher static __net_init int pppoe_init_net(struct net *net) 1143224cf5adSJeff Kirsher { 1144224cf5adSJeff Kirsher struct pppoe_net *pn = pppoe_pernet(net); 1145224cf5adSJeff Kirsher struct proc_dir_entry *pde; 1146224cf5adSJeff Kirsher 1147224cf5adSJeff Kirsher rwlock_init(&pn->hash_lock); 1148224cf5adSJeff Kirsher 1149d61e4038SJoe Perches pde = proc_create("pppoe", 0444, net->proc_net, &pppoe_seq_fops); 1150224cf5adSJeff Kirsher #ifdef CONFIG_PROC_FS 1151224cf5adSJeff Kirsher if (!pde) 1152224cf5adSJeff Kirsher return -ENOMEM; 1153224cf5adSJeff Kirsher #endif 1154224cf5adSJeff Kirsher 1155224cf5adSJeff Kirsher return 0; 1156224cf5adSJeff Kirsher } 1157224cf5adSJeff Kirsher 1158224cf5adSJeff Kirsher static __net_exit void pppoe_exit_net(struct net *net) 1159224cf5adSJeff Kirsher { 1160ece31ffdSGao feng remove_proc_entry("pppoe", net->proc_net); 1161224cf5adSJeff Kirsher } 1162224cf5adSJeff Kirsher 1163224cf5adSJeff Kirsher static struct pernet_operations pppoe_net_ops = { 1164224cf5adSJeff Kirsher .init = pppoe_init_net, 1165224cf5adSJeff Kirsher .exit = pppoe_exit_net, 1166224cf5adSJeff Kirsher .id = &pppoe_net_id, 1167224cf5adSJeff Kirsher .size = sizeof(struct pppoe_net), 1168224cf5adSJeff Kirsher }; 1169224cf5adSJeff Kirsher 1170224cf5adSJeff Kirsher static int __init pppoe_init(void) 1171224cf5adSJeff Kirsher { 1172224cf5adSJeff Kirsher int err; 1173224cf5adSJeff Kirsher 1174224cf5adSJeff Kirsher err = register_pernet_device(&pppoe_net_ops); 1175224cf5adSJeff Kirsher if (err) 1176224cf5adSJeff Kirsher goto out; 1177224cf5adSJeff Kirsher 1178224cf5adSJeff Kirsher err = proto_register(&pppoe_sk_proto, 0); 1179224cf5adSJeff Kirsher if (err) 1180224cf5adSJeff Kirsher goto out_unregister_net_ops; 1181224cf5adSJeff Kirsher 1182224cf5adSJeff Kirsher err = register_pppox_proto(PX_PROTO_OE, &pppoe_proto); 1183224cf5adSJeff Kirsher if (err) 1184224cf5adSJeff Kirsher goto out_unregister_pppoe_proto; 1185224cf5adSJeff Kirsher 1186224cf5adSJeff Kirsher dev_add_pack(&pppoes_ptype); 1187224cf5adSJeff Kirsher dev_add_pack(&pppoed_ptype); 1188224cf5adSJeff Kirsher register_netdevice_notifier(&pppoe_notifier); 1189224cf5adSJeff Kirsher 1190224cf5adSJeff Kirsher return 0; 1191224cf5adSJeff Kirsher 1192224cf5adSJeff Kirsher out_unregister_pppoe_proto: 1193224cf5adSJeff Kirsher proto_unregister(&pppoe_sk_proto); 1194224cf5adSJeff Kirsher out_unregister_net_ops: 1195224cf5adSJeff Kirsher unregister_pernet_device(&pppoe_net_ops); 1196224cf5adSJeff Kirsher out: 1197224cf5adSJeff Kirsher return err; 1198224cf5adSJeff Kirsher } 1199224cf5adSJeff Kirsher 1200224cf5adSJeff Kirsher static void __exit pppoe_exit(void) 1201224cf5adSJeff Kirsher { 1202224cf5adSJeff Kirsher unregister_netdevice_notifier(&pppoe_notifier); 1203224cf5adSJeff Kirsher dev_remove_pack(&pppoed_ptype); 1204224cf5adSJeff Kirsher dev_remove_pack(&pppoes_ptype); 1205224cf5adSJeff Kirsher unregister_pppox_proto(PX_PROTO_OE); 1206224cf5adSJeff Kirsher proto_unregister(&pppoe_sk_proto); 1207224cf5adSJeff Kirsher unregister_pernet_device(&pppoe_net_ops); 1208224cf5adSJeff Kirsher } 1209224cf5adSJeff Kirsher 1210224cf5adSJeff Kirsher module_init(pppoe_init); 1211224cf5adSJeff Kirsher module_exit(pppoe_exit); 1212224cf5adSJeff Kirsher 1213224cf5adSJeff Kirsher MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>"); 1214224cf5adSJeff Kirsher MODULE_DESCRIPTION("PPP over Ethernet driver"); 1215224cf5adSJeff Kirsher MODULE_LICENSE("GPL"); 1216681b4d88SGuillaume Nault MODULE_ALIAS_NET_PF_PROTO(PF_PPPOX, PX_PROTO_OE); 1217