pppoe.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. /** -*- linux-c -*- ***********************************************************
  2. * Linux PPP over Ethernet (PPPoX/PPPoE) Sockets
  3. *
  4. * PPPoX --- Generic PPP encapsulation socket family
  5. * PPPoE --- PPP over Ethernet (RFC 2516)
  6. *
  7. *
  8. * Version: 0.7.0
  9. *
  10. * 070228 : Fix to allow multiple sessions with same remote MAC and same
  11. * session id by including the local device ifindex in the
  12. * tuple identifying a session. This also ensures packets can't
  13. * be injected into a session from interfaces other than the one
  14. * specified by userspace. Florian Zumbiehl <florz@florz.de>
  15. * (Oh, BTW, this one is YYMMDD, in case you were wondering ...)
  16. * 220102 : Fix module use count on failure in pppoe_create, pppox_sk -acme
  17. * 030700 : Fixed connect logic to allow for disconnect.
  18. * 270700 : Fixed potential SMP problems; we must protect against
  19. * simultaneous invocation of ppp_input
  20. * and ppp_unregister_channel.
  21. * 040800 : Respect reference count mechanisms on net-devices.
  22. * 200800 : fix kfree(skb) in pppoe_rcv (acme)
  23. * Module reference count is decremented in the right spot now,
  24. * guards against sock_put not actually freeing the sk
  25. * in pppoe_release.
  26. * 051000 : Initialization cleanup.
  27. * 111100 : Fix recvmsg.
  28. * 050101 : Fix PADT procesing.
  29. * 140501 : Use pppoe_rcv_core to handle all backlog. (Alexey)
  30. * 170701 : Do not lock_sock with rwlock held. (DaveM)
  31. * Ignore discovery frames if user has socket
  32. * locked. (DaveM)
  33. * Ignore return value of dev_queue_xmit in __pppoe_xmit
  34. * or else we may kfree an SKB twice. (DaveM)
  35. * 190701 : When doing copies of skb's in __pppoe_xmit, always delete
  36. * the original skb that was passed in on success, never on
  37. * failure. Delete the copy of the skb on failure to avoid
  38. * a memory leak.
  39. * 081001 : Misc. cleanup (licence string, non-blocking, prevent
  40. * reference of device on close).
  41. * 121301 : New ppp channels interface; cannot unregister a channel
  42. * from interrupts. Thus, we mark the socket as a ZOMBIE
  43. * and do the unregistration later.
  44. * 081002 : seq_file support for proc stuff -acme
  45. * 111602 : Merge all 2.4 fixes into 2.5/2.6 tree. Label 2.5/2.6
  46. * as version 0.7. Spacing cleanup.
  47. * Author: Michal Ostrowski <mostrows@speakeasy.net>
  48. * Contributors:
  49. * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  50. * David S. Miller (davem@redhat.com)
  51. *
  52. * License:
  53. * This program is free software; you can redistribute it and/or
  54. * modify it under the terms of the GNU General Public License
  55. * as published by the Free Software Foundation; either version
  56. * 2 of the License, or (at your option) any later version.
  57. *
  58. */
  59. #include <linux/string.h>
  60. #include <linux/module.h>
  61. #include <linux/kernel.h>
  62. #include <linux/slab.h>
  63. #include <linux/errno.h>
  64. #include <linux/netdevice.h>
  65. #include <linux/net.h>
  66. #include <linux/inetdevice.h>
  67. #include <linux/etherdevice.h>
  68. #include <linux/skbuff.h>
  69. #include <linux/init.h>
  70. #include <linux/if_ether.h>
  71. #include <linux/if_pppox.h>
  72. #include <linux/ppp_channel.h>
  73. #include <linux/ppp_defs.h>
  74. #include <linux/ppp-ioctl.h>
  75. #include <linux/notifier.h>
  76. #include <linux/file.h>
  77. #include <linux/proc_fs.h>
  78. #include <linux/seq_file.h>
  79. #include <linux/nsproxy.h>
  80. #include <net/net_namespace.h>
  81. #include <net/netns/generic.h>
  82. #include <net/sock.h>
  83. #include <asm/uaccess.h>
  84. #define PPPOE_HASH_BITS 4
  85. #define PPPOE_HASH_SIZE (1 << PPPOE_HASH_BITS)
  86. #define PPPOE_HASH_MASK (PPPOE_HASH_SIZE - 1)
  87. static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb);
  88. static const struct proto_ops pppoe_ops;
  89. static const struct ppp_channel_ops pppoe_chan_ops;
  90. /* per-net private data for this module */
  91. static int pppoe_net_id __read_mostly;
  92. struct pppoe_net {
  93. /*
  94. * we could use _single_ hash table for all
  95. * nets by injecting net id into the hash but
  96. * it would increase hash chains and add
  97. * a few additional math comparations messy
  98. * as well, moreover in case of SMP less locking
  99. * controversy here
  100. */
  101. struct pppox_sock *hash_table[PPPOE_HASH_SIZE];
  102. rwlock_t hash_lock;
  103. };
  104. /*
  105. * PPPoE could be in the following stages:
  106. * 1) Discovery stage (to obtain remote MAC and Session ID)
  107. * 2) Session stage (MAC and SID are known)
  108. *
  109. * Ethernet frames have a special tag for this but
  110. * we use simpler approach based on session id
  111. */
  112. static inline bool stage_session(__be16 sid)
  113. {
  114. return sid != 0;
  115. }
  116. static inline struct pppoe_net *pppoe_pernet(struct net *net)
  117. {
  118. BUG_ON(!net);
  119. return net_generic(net, pppoe_net_id);
  120. }
  121. static inline int cmp_2_addr(struct pppoe_addr *a, struct pppoe_addr *b)
  122. {
  123. return a->sid == b->sid && !memcmp(a->remote, b->remote, ETH_ALEN);
  124. }
  125. static inline int cmp_addr(struct pppoe_addr *a, __be16 sid, char *addr)
  126. {
  127. return a->sid == sid && !memcmp(a->remote, addr, ETH_ALEN);
  128. }
  129. #if 8 % PPPOE_HASH_BITS
  130. #error 8 must be a multiple of PPPOE_HASH_BITS
  131. #endif
  132. static int hash_item(__be16 sid, unsigned char *addr)
  133. {
  134. unsigned char hash = 0;
  135. unsigned int i;
  136. for (i = 0; i < ETH_ALEN; i++)
  137. hash ^= addr[i];
  138. for (i = 0; i < sizeof(sid_t) * 8; i += 8)
  139. hash ^= (__force __u32)sid >> i;
  140. for (i = 8; (i >>= 1) >= PPPOE_HASH_BITS;)
  141. hash ^= hash >> i;
  142. return hash & PPPOE_HASH_MASK;
  143. }
  144. /**********************************************************************
  145. *
  146. * Set/get/delete/rehash items (internal versions)
  147. *
  148. **********************************************************************/
  149. static struct pppox_sock *__get_item(struct pppoe_net *pn, __be16 sid,
  150. unsigned char *addr, int ifindex)
  151. {
  152. int hash = hash_item(sid, addr);
  153. struct pppox_sock *ret;
  154. ret = pn->hash_table[hash];
  155. while (ret) {
  156. if (cmp_addr(&ret->pppoe_pa, sid, addr) &&
  157. ret->pppoe_ifindex == ifindex)
  158. return ret;
  159. ret = ret->next;
  160. }
  161. return NULL;
  162. }
  163. static int __set_item(struct pppoe_net *pn, struct pppox_sock *po)
  164. {
  165. int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
  166. struct pppox_sock *ret;
  167. ret = pn->hash_table[hash];
  168. while (ret) {
  169. if (cmp_2_addr(&ret->pppoe_pa, &po->pppoe_pa) &&
  170. ret->pppoe_ifindex == po->pppoe_ifindex)
  171. return -EALREADY;
  172. ret = ret->next;
  173. }
  174. po->next = pn->hash_table[hash];
  175. pn->hash_table[hash] = po;
  176. return 0;
  177. }
  178. static struct pppox_sock *__delete_item(struct pppoe_net *pn, __be16 sid,
  179. char *addr, int ifindex)
  180. {
  181. int hash = hash_item(sid, addr);
  182. struct pppox_sock *ret, **src;
  183. ret = pn->hash_table[hash];
  184. src = &pn->hash_table[hash];
  185. while (ret) {
  186. if (cmp_addr(&ret->pppoe_pa, sid, addr) &&
  187. ret->pppoe_ifindex == ifindex) {
  188. *src = ret->next;
  189. break;
  190. }
  191. src = &ret->next;
  192. ret = ret->next;
  193. }
  194. return ret;
  195. }
  196. /**********************************************************************
  197. *
  198. * Set/get/delete/rehash items
  199. *
  200. **********************************************************************/
  201. static inline struct pppox_sock *get_item(struct pppoe_net *pn, __be16 sid,
  202. unsigned char *addr, int ifindex)
  203. {
  204. struct pppox_sock *po;
  205. read_lock_bh(&pn->hash_lock);
  206. po = __get_item(pn, sid, addr, ifindex);
  207. if (po)
  208. sock_hold(sk_pppox(po));
  209. read_unlock_bh(&pn->hash_lock);
  210. return po;
  211. }
  212. static inline struct pppox_sock *get_item_by_addr(struct net *net,
  213. struct sockaddr_pppox *sp)
  214. {
  215. struct net_device *dev;
  216. struct pppoe_net *pn;
  217. struct pppox_sock *pppox_sock = NULL;
  218. int ifindex;
  219. rcu_read_lock();
  220. dev = dev_get_by_name_rcu(net, sp->sa_addr.pppoe.dev);
  221. if (dev) {
  222. ifindex = dev->ifindex;
  223. pn = pppoe_pernet(net);
  224. pppox_sock = get_item(pn, sp->sa_addr.pppoe.sid,
  225. sp->sa_addr.pppoe.remote, ifindex);
  226. }
  227. rcu_read_unlock();
  228. return pppox_sock;
  229. }
  230. static inline struct pppox_sock *delete_item(struct pppoe_net *pn, __be16 sid,
  231. char *addr, int ifindex)
  232. {
  233. struct pppox_sock *ret;
  234. write_lock_bh(&pn->hash_lock);
  235. ret = __delete_item(pn, sid, addr, ifindex);
  236. write_unlock_bh(&pn->hash_lock);
  237. return ret;
  238. }
  239. /***************************************************************************
  240. *
  241. * Handler for device events.
  242. * Certain device events require that sockets be unconnected.
  243. *
  244. **************************************************************************/
  245. static void pppoe_flush_dev(struct net_device *dev)
  246. {
  247. struct pppoe_net *pn;
  248. int i;
  249. pn = pppoe_pernet(dev_net(dev));
  250. write_lock_bh(&pn->hash_lock);
  251. for (i = 0; i < PPPOE_HASH_SIZE; i++) {
  252. struct pppox_sock *po = pn->hash_table[i];
  253. struct sock *sk;
  254. while (po) {
  255. while (po && po->pppoe_dev != dev) {
  256. po = po->next;
  257. }
  258. if (!po)
  259. break;
  260. sk = sk_pppox(po);
  261. /* We always grab the socket lock, followed by the
  262. * hash_lock, in that order. Since we should hold the
  263. * sock lock while doing any unbinding, we need to
  264. * release the lock we're holding. Hold a reference to
  265. * the sock so it doesn't disappear as we're jumping
  266. * between locks.
  267. */
  268. sock_hold(sk);
  269. write_unlock_bh(&pn->hash_lock);
  270. lock_sock(sk);
  271. if (po->pppoe_dev == dev &&
  272. sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND | PPPOX_ZOMBIE)) {
  273. pppox_unbind_sock(sk);
  274. sk->sk_state = PPPOX_ZOMBIE;
  275. sk->sk_state_change(sk);
  276. po->pppoe_dev = NULL;
  277. dev_put(dev);
  278. }
  279. release_sock(sk);
  280. sock_put(sk);
  281. /* Restart the process from the start of the current
  282. * hash chain. We dropped locks so the world may have
  283. * change from underneath us.
  284. */
  285. BUG_ON(pppoe_pernet(dev_net(dev)) == NULL);
  286. write_lock_bh(&pn->hash_lock);
  287. po = pn->hash_table[i];
  288. }
  289. }
  290. write_unlock_bh(&pn->hash_lock);
  291. }
  292. static int pppoe_device_event(struct notifier_block *this,
  293. unsigned long event, void *ptr)
  294. {
  295. struct net_device *dev = (struct net_device *)ptr;
  296. /* Only look at sockets that are using this specific device. */
  297. switch (event) {
  298. case NETDEV_CHANGEADDR:
  299. case NETDEV_CHANGEMTU:
  300. /* A change in mtu or address is a bad thing, requiring
  301. * LCP re-negotiation.
  302. */
  303. case NETDEV_GOING_DOWN:
  304. case NETDEV_DOWN:
  305. /* Find every socket on this device and kill it. */
  306. pppoe_flush_dev(dev);
  307. break;
  308. default:
  309. break;
  310. }
  311. return NOTIFY_DONE;
  312. }
  313. static struct notifier_block pppoe_notifier = {
  314. .notifier_call = pppoe_device_event,
  315. };
  316. /************************************************************************
  317. *
  318. * Do the real work of receiving a PPPoE Session frame.
  319. *
  320. ***********************************************************************/
  321. static int pppoe_rcv_core(struct sock *sk, struct sk_buff *skb)
  322. {
  323. struct pppox_sock *po = pppox_sk(sk);
  324. struct pppox_sock *relay_po;
  325. /* Backlog receive. Semantics of backlog rcv preclude any code from
  326. * executing in lock_sock()/release_sock() bounds; meaning sk->sk_state
  327. * can't change.
  328. */
  329. if (sk->sk_state & PPPOX_BOUND) {
  330. ppp_input(&po->chan, skb);
  331. } else if (sk->sk_state & PPPOX_RELAY) {
  332. relay_po = get_item_by_addr(sock_net(sk),
  333. &po->pppoe_relay);
  334. if (relay_po == NULL)
  335. goto abort_kfree;
  336. if ((sk_pppox(relay_po)->sk_state & PPPOX_CONNECTED) == 0)
  337. goto abort_put;
  338. if (!__pppoe_xmit(sk_pppox(relay_po), skb))
  339. goto abort_put;
  340. } else {
  341. if (sock_queue_rcv_skb(sk, skb))
  342. goto abort_kfree;
  343. }
  344. return NET_RX_SUCCESS;
  345. abort_put:
  346. sock_put(sk_pppox(relay_po));
  347. abort_kfree:
  348. kfree_skb(skb);
  349. return NET_RX_DROP;
  350. }
  351. /************************************************************************
  352. *
  353. * Receive wrapper called in BH context.
  354. *
  355. ***********************************************************************/
  356. static int pppoe_rcv(struct sk_buff *skb, struct net_device *dev,
  357. struct packet_type *pt, struct net_device *orig_dev)
  358. {
  359. struct pppoe_hdr *ph;
  360. struct pppox_sock *po;
  361. struct pppoe_net *pn;
  362. int len;
  363. skb = skb_share_check(skb, GFP_ATOMIC);
  364. if (!skb)
  365. goto out;
  366. if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
  367. goto drop;
  368. ph = pppoe_hdr(skb);
  369. len = ntohs(ph->length);
  370. skb_pull_rcsum(skb, sizeof(*ph));
  371. if (skb->len < len)
  372. goto drop;
  373. if (pskb_trim_rcsum(skb, len))
  374. goto drop;
  375. pn = pppoe_pernet(dev_net(dev));
  376. /* Note that get_item does a sock_hold(), so sk_pppox(po)
  377. * is known to be safe.
  378. */
  379. po = get_item(pn, ph->sid, eth_hdr(skb)->h_source, dev->ifindex);
  380. if (!po)
  381. goto drop;
  382. return sk_receive_skb(sk_pppox(po), skb, 0);
  383. drop:
  384. kfree_skb(skb);
  385. out:
  386. return NET_RX_DROP;
  387. }
  388. /************************************************************************
  389. *
  390. * Receive a PPPoE Discovery frame.
  391. * This is solely for detection of PADT frames
  392. *
  393. ***********************************************************************/
  394. static int pppoe_disc_rcv(struct sk_buff *skb, struct net_device *dev,
  395. struct packet_type *pt, struct net_device *orig_dev)
  396. {
  397. struct pppoe_hdr *ph;
  398. struct pppox_sock *po;
  399. struct pppoe_net *pn;
  400. skb = skb_share_check(skb, GFP_ATOMIC);
  401. if (!skb)
  402. goto out;
  403. if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
  404. goto abort;
  405. ph = pppoe_hdr(skb);
  406. if (ph->code != PADT_CODE)
  407. goto abort;
  408. pn = pppoe_pernet(dev_net(dev));
  409. po = get_item(pn, ph->sid, eth_hdr(skb)->h_source, dev->ifindex);
  410. if (po) {
  411. struct sock *sk = sk_pppox(po);
  412. bh_lock_sock(sk);
  413. /* If the user has locked the socket, just ignore
  414. * the packet. With the way two rcv protocols hook into
  415. * one socket family type, we cannot (easily) distinguish
  416. * what kind of SKB it is during backlog rcv.
  417. */
  418. if (sock_owned_by_user(sk) == 0) {
  419. /* We're no longer connect at the PPPOE layer,
  420. * and must wait for ppp channel to disconnect us.
  421. */
  422. sk->sk_state = PPPOX_ZOMBIE;
  423. }
  424. bh_unlock_sock(sk);
  425. sock_put(sk);
  426. }
  427. abort:
  428. kfree_skb(skb);
  429. out:
  430. return NET_RX_SUCCESS; /* Lies... :-) */
  431. }
  432. static struct packet_type pppoes_ptype __read_mostly = {
  433. .type = cpu_to_be16(ETH_P_PPP_SES),
  434. .func = pppoe_rcv,
  435. };
  436. static struct packet_type pppoed_ptype __read_mostly = {
  437. .type = cpu_to_be16(ETH_P_PPP_DISC),
  438. .func = pppoe_disc_rcv,
  439. };
  440. static struct proto pppoe_sk_proto __read_mostly = {
  441. .name = "PPPOE",
  442. .owner = THIS_MODULE,
  443. .obj_size = sizeof(struct pppox_sock),
  444. };
  445. /***********************************************************************
  446. *
  447. * Initialize a new struct sock.
  448. *
  449. **********************************************************************/
  450. static int pppoe_create(struct net *net, struct socket *sock)
  451. {
  452. struct sock *sk;
  453. sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppoe_sk_proto);
  454. if (!sk)
  455. return -ENOMEM;
  456. sock_init_data(sock, sk);
  457. sock->state = SS_UNCONNECTED;
  458. sock->ops = &pppoe_ops;
  459. sk->sk_backlog_rcv = pppoe_rcv_core;
  460. sk->sk_state = PPPOX_NONE;
  461. sk->sk_type = SOCK_STREAM;
  462. sk->sk_family = PF_PPPOX;
  463. sk->sk_protocol = PX_PROTO_OE;
  464. return 0;
  465. }
  466. static int pppoe_release(struct socket *sock)
  467. {
  468. struct sock *sk = sock->sk;
  469. struct pppox_sock *po;
  470. struct pppoe_net *pn;
  471. struct net *net = NULL;
  472. if (!sk)
  473. return 0;
  474. lock_sock(sk);
  475. if (sock_flag(sk, SOCK_DEAD)) {
  476. release_sock(sk);
  477. return -EBADF;
  478. }
  479. po = pppox_sk(sk);
  480. if (sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND | PPPOX_ZOMBIE)) {
  481. dev_put(po->pppoe_dev);
  482. po->pppoe_dev = NULL;
  483. }
  484. pppox_unbind_sock(sk);
  485. /* Signal the death of the socket. */
  486. sk->sk_state = PPPOX_DEAD;
  487. net = sock_net(sk);
  488. pn = pppoe_pernet(net);
  489. /*
  490. * protect "po" from concurrent updates
  491. * on pppoe_flush_dev
  492. */
  493. delete_item(pn, po->pppoe_pa.sid, po->pppoe_pa.remote,
  494. po->pppoe_ifindex);
  495. sock_orphan(sk);
  496. sock->sk = NULL;
  497. skb_queue_purge(&sk->sk_receive_queue);
  498. release_sock(sk);
  499. sock_put(sk);
  500. return 0;
  501. }
  502. static int pppoe_connect(struct socket *sock, struct sockaddr *uservaddr,
  503. int sockaddr_len, int flags)
  504. {
  505. struct sock *sk = sock->sk;
  506. struct sockaddr_pppox *sp = (struct sockaddr_pppox *)uservaddr;
  507. struct pppox_sock *po = pppox_sk(sk);
  508. struct net_device *dev = NULL;
  509. struct pppoe_net *pn;
  510. struct net *net = NULL;
  511. int error;
  512. lock_sock(sk);
  513. error = -EINVAL;
  514. if (sp->sa_protocol != PX_PROTO_OE)
  515. goto end;
  516. /* Check for already bound sockets */
  517. error = -EBUSY;
  518. if ((sk->sk_state & PPPOX_CONNECTED) &&
  519. stage_session(sp->sa_addr.pppoe.sid))
  520. goto end;
  521. /* Check for already disconnected sockets, on attempts to disconnect */
  522. error = -EALREADY;
  523. if ((sk->sk_state & PPPOX_DEAD) &&
  524. !stage_session(sp->sa_addr.pppoe.sid))
  525. goto end;
  526. error = 0;
  527. /* Delete the old binding */
  528. if (stage_session(po->pppoe_pa.sid)) {
  529. pppox_unbind_sock(sk);
  530. pn = pppoe_pernet(sock_net(sk));
  531. delete_item(pn, po->pppoe_pa.sid,
  532. po->pppoe_pa.remote, po->pppoe_ifindex);
  533. if (po->pppoe_dev) {
  534. dev_put(po->pppoe_dev);
  535. po->pppoe_dev = NULL;
  536. }
  537. memset(sk_pppox(po) + 1, 0,
  538. sizeof(struct pppox_sock) - sizeof(struct sock));
  539. sk->sk_state = PPPOX_NONE;
  540. }
  541. /* Re-bind in session stage only */
  542. if (stage_session(sp->sa_addr.pppoe.sid)) {
  543. error = -ENODEV;
  544. net = sock_net(sk);
  545. dev = dev_get_by_name(net, sp->sa_addr.pppoe.dev);
  546. if (!dev)
  547. goto err_put;
  548. po->pppoe_dev = dev;
  549. po->pppoe_ifindex = dev->ifindex;
  550. pn = pppoe_pernet(net);
  551. if (!(dev->flags & IFF_UP)) {
  552. goto err_put;
  553. }
  554. memcpy(&po->pppoe_pa,
  555. &sp->sa_addr.pppoe,
  556. sizeof(struct pppoe_addr));
  557. write_lock_bh(&pn->hash_lock);
  558. error = __set_item(pn, po);
  559. write_unlock_bh(&pn->hash_lock);
  560. if (error < 0)
  561. goto err_put;
  562. po->chan.hdrlen = (sizeof(struct pppoe_hdr) +
  563. dev->hard_header_len);
  564. po->chan.mtu = dev->mtu - sizeof(struct pppoe_hdr) - 2;
  565. po->chan.private = sk;
  566. po->chan.ops = &pppoe_chan_ops;
  567. error = ppp_register_net_channel(dev_net(dev), &po->chan);
  568. if (error) {
  569. delete_item(pn, po->pppoe_pa.sid,
  570. po->pppoe_pa.remote, po->pppoe_ifindex);
  571. goto err_put;
  572. }
  573. sk->sk_state = PPPOX_CONNECTED;
  574. }
  575. po->num = sp->sa_addr.pppoe.sid;
  576. end:
  577. release_sock(sk);
  578. return error;
  579. err_put:
  580. if (po->pppoe_dev) {
  581. dev_put(po->pppoe_dev);
  582. po->pppoe_dev = NULL;
  583. }
  584. goto end;
  585. }
  586. static int pppoe_getname(struct socket *sock, struct sockaddr *uaddr,
  587. int *usockaddr_len, int peer)
  588. {
  589. int len = sizeof(struct sockaddr_pppox);
  590. struct sockaddr_pppox sp;
  591. sp.sa_family = AF_PPPOX;
  592. sp.sa_protocol = PX_PROTO_OE;
  593. memcpy(&sp.sa_addr.pppoe, &pppox_sk(sock->sk)->pppoe_pa,
  594. sizeof(struct pppoe_addr));
  595. memcpy(uaddr, &sp, len);
  596. *usockaddr_len = len;
  597. return 0;
  598. }
  599. static int pppoe_ioctl(struct socket *sock, unsigned int cmd,
  600. unsigned long arg)
  601. {
  602. struct sock *sk = sock->sk;
  603. struct pppox_sock *po = pppox_sk(sk);
  604. int val;
  605. int err;
  606. switch (cmd) {
  607. case PPPIOCGMRU:
  608. err = -ENXIO;
  609. if (!(sk->sk_state & PPPOX_CONNECTED))
  610. break;
  611. err = -EFAULT;
  612. if (put_user(po->pppoe_dev->mtu -
  613. sizeof(struct pppoe_hdr) -
  614. PPP_HDRLEN,
  615. (int __user *)arg))
  616. break;
  617. err = 0;
  618. break;
  619. case PPPIOCSMRU:
  620. err = -ENXIO;
  621. if (!(sk->sk_state & PPPOX_CONNECTED))
  622. break;
  623. err = -EFAULT;
  624. if (get_user(val, (int __user *)arg))
  625. break;
  626. if (val < (po->pppoe_dev->mtu
  627. - sizeof(struct pppoe_hdr)
  628. - PPP_HDRLEN))
  629. err = 0;
  630. else
  631. err = -EINVAL;
  632. break;
  633. case PPPIOCSFLAGS:
  634. err = -EFAULT;
  635. if (get_user(val, (int __user *)arg))
  636. break;
  637. err = 0;
  638. break;
  639. case PPPOEIOCSFWD:
  640. {
  641. struct pppox_sock *relay_po;
  642. err = -EBUSY;
  643. if (sk->sk_state & (PPPOX_BOUND | PPPOX_ZOMBIE | PPPOX_DEAD))
  644. break;
  645. err = -ENOTCONN;
  646. if (!(sk->sk_state & PPPOX_CONNECTED))
  647. break;
  648. /* PPPoE address from the user specifies an outbound
  649. PPPoE address which frames are forwarded to */
  650. err = -EFAULT;
  651. if (copy_from_user(&po->pppoe_relay,
  652. (void __user *)arg,
  653. sizeof(struct sockaddr_pppox)))
  654. break;
  655. err = -EINVAL;
  656. if (po->pppoe_relay.sa_family != AF_PPPOX ||
  657. po->pppoe_relay.sa_protocol != PX_PROTO_OE)
  658. break;
  659. /* Check that the socket referenced by the address
  660. actually exists. */
  661. relay_po = get_item_by_addr(sock_net(sk), &po->pppoe_relay);
  662. if (!relay_po)
  663. break;
  664. sock_put(sk_pppox(relay_po));
  665. sk->sk_state |= PPPOX_RELAY;
  666. err = 0;
  667. break;
  668. }
  669. case PPPOEIOCDFWD:
  670. err = -EALREADY;
  671. if (!(sk->sk_state & PPPOX_RELAY))
  672. break;
  673. sk->sk_state &= ~PPPOX_RELAY;
  674. err = 0;
  675. break;
  676. default:
  677. err = -ENOTTY;
  678. }
  679. return err;
  680. }
  681. static int pppoe_sendmsg(struct kiocb *iocb, struct socket *sock,
  682. struct msghdr *m, size_t total_len)
  683. {
  684. struct sk_buff *skb;
  685. struct sock *sk = sock->sk;
  686. struct pppox_sock *po = pppox_sk(sk);
  687. int error;
  688. struct pppoe_hdr hdr;
  689. struct pppoe_hdr *ph;
  690. struct net_device *dev;
  691. char *start;
  692. lock_sock(sk);
  693. if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) {
  694. error = -ENOTCONN;
  695. goto end;
  696. }
  697. hdr.ver = 1;
  698. hdr.type = 1;
  699. hdr.code = 0;
  700. hdr.sid = po->num;
  701. dev = po->pppoe_dev;
  702. error = -EMSGSIZE;
  703. if (total_len > (dev->mtu + dev->hard_header_len))
  704. goto end;
  705. skb = sock_wmalloc(sk, total_len + dev->hard_header_len + 32,
  706. 0, GFP_KERNEL);
  707. if (!skb) {
  708. error = -ENOMEM;
  709. goto end;
  710. }
  711. /* Reserve space for headers. */
  712. skb_reserve(skb, dev->hard_header_len);
  713. skb_reset_network_header(skb);
  714. skb->dev = dev;
  715. skb->priority = sk->sk_priority;
  716. skb->protocol = cpu_to_be16(ETH_P_PPP_SES);
  717. ph = (struct pppoe_hdr *)skb_put(skb, total_len + sizeof(struct pppoe_hdr));
  718. start = (char *)&ph->tag[0];
  719. error = memcpy_fromiovec(start, m->msg_iov, total_len);
  720. if (error < 0) {
  721. kfree_skb(skb);
  722. goto end;
  723. }
  724. error = total_len;
  725. dev_hard_header(skb, dev, ETH_P_PPP_SES,
  726. po->pppoe_pa.remote, NULL, total_len);
  727. memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
  728. ph->length = htons(total_len);
  729. dev_queue_xmit(skb);
  730. end:
  731. release_sock(sk);
  732. return error;
  733. }
  734. /************************************************************************
  735. *
  736. * xmit function for internal use.
  737. *
  738. ***********************************************************************/
  739. static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb)
  740. {
  741. struct pppox_sock *po = pppox_sk(sk);
  742. struct net_device *dev = po->pppoe_dev;
  743. struct pppoe_hdr *ph;
  744. int data_len = skb->len;
  745. /* The higher-level PPP code (ppp_unregister_channel()) ensures the PPP
  746. * xmit operations conclude prior to an unregistration call. Thus
  747. * sk->sk_state cannot change, so we don't need to do lock_sock().
  748. * But, we also can't do a lock_sock since that introduces a potential
  749. * deadlock as we'd reverse the lock ordering used when calling
  750. * ppp_unregister_channel().
  751. */
  752. if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
  753. goto abort;
  754. if (!dev)
  755. goto abort;
  756. /* Copy the data if there is no space for the header or if it's
  757. * read-only.
  758. */
  759. if (skb_cow_head(skb, sizeof(*ph) + dev->hard_header_len))
  760. goto abort;
  761. __skb_push(skb, sizeof(*ph));
  762. skb_reset_network_header(skb);
  763. ph = pppoe_hdr(skb);
  764. ph->ver = 1;
  765. ph->type = 1;
  766. ph->code = 0;
  767. ph->sid = po->num;
  768. ph->length = htons(data_len);
  769. skb->protocol = cpu_to_be16(ETH_P_PPP_SES);
  770. skb->dev = dev;
  771. dev_hard_header(skb, dev, ETH_P_PPP_SES,
  772. po->pppoe_pa.remote, NULL, data_len);
  773. dev_queue_xmit(skb);
  774. return 1;
  775. abort:
  776. kfree_skb(skb);
  777. return 1;
  778. }
  779. /************************************************************************
  780. *
  781. * xmit function called by generic PPP driver
  782. * sends PPP frame over PPPoE socket
  783. *
  784. ***********************************************************************/
  785. static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb)
  786. {
  787. struct sock *sk = (struct sock *)chan->private;
  788. return __pppoe_xmit(sk, skb);
  789. }
  790. static const struct ppp_channel_ops pppoe_chan_ops = {
  791. .start_xmit = pppoe_xmit,
  792. };
  793. static int pppoe_recvmsg(struct kiocb *iocb, struct socket *sock,
  794. struct msghdr *m, size_t total_len, int flags)
  795. {
  796. struct sock *sk = sock->sk;
  797. struct sk_buff *skb;
  798. int error = 0;
  799. if (sk->sk_state & PPPOX_BOUND) {
  800. error = -EIO;
  801. goto end;
  802. }
  803. skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
  804. flags & MSG_DONTWAIT, &error);
  805. if (error < 0)
  806. goto end;
  807. if (skb) {
  808. total_len = min_t(size_t, total_len, skb->len);
  809. error = skb_copy_datagram_iovec(skb, 0, m->msg_iov, total_len);
  810. if (error == 0)
  811. error = total_len;
  812. }
  813. kfree_skb(skb);
  814. end:
  815. return error;
  816. }
  817. #ifdef CONFIG_PROC_FS
  818. static int pppoe_seq_show(struct seq_file *seq, void *v)
  819. {
  820. struct pppox_sock *po;
  821. char *dev_name;
  822. if (v == SEQ_START_TOKEN) {
  823. seq_puts(seq, "Id Address Device\n");
  824. goto out;
  825. }
  826. po = v;
  827. dev_name = po->pppoe_pa.dev;
  828. seq_printf(seq, "%08X %pM %8s\n",
  829. po->pppoe_pa.sid, po->pppoe_pa.remote, dev_name);
  830. out:
  831. return 0;
  832. }
  833. static inline struct pppox_sock *pppoe_get_idx(struct pppoe_net *pn, loff_t pos)
  834. {
  835. struct pppox_sock *po;
  836. int i;
  837. for (i = 0; i < PPPOE_HASH_SIZE; i++) {
  838. po = pn->hash_table[i];
  839. while (po) {
  840. if (!pos--)
  841. goto out;
  842. po = po->next;
  843. }
  844. }
  845. out:
  846. return po;
  847. }
  848. static void *pppoe_seq_start(struct seq_file *seq, loff_t *pos)
  849. __acquires(pn->hash_lock)
  850. {
  851. struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
  852. loff_t l = *pos;
  853. read_lock_bh(&pn->hash_lock);
  854. return l ? pppoe_get_idx(pn, --l) : SEQ_START_TOKEN;
  855. }
  856. static void *pppoe_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  857. {
  858. struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
  859. struct pppox_sock *po;
  860. ++*pos;
  861. if (v == SEQ_START_TOKEN) {
  862. po = pppoe_get_idx(pn, 0);
  863. goto out;
  864. }
  865. po = v;
  866. if (po->next)
  867. po = po->next;
  868. else {
  869. int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
  870. po = NULL;
  871. while (++hash < PPPOE_HASH_SIZE) {
  872. po = pn->hash_table[hash];
  873. if (po)
  874. break;
  875. }
  876. }
  877. out:
  878. return po;
  879. }
  880. static void pppoe_seq_stop(struct seq_file *seq, void *v)
  881. __releases(pn->hash_lock)
  882. {
  883. struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
  884. read_unlock_bh(&pn->hash_lock);
  885. }
  886. static const struct seq_operations pppoe_seq_ops = {
  887. .start = pppoe_seq_start,
  888. .next = pppoe_seq_next,
  889. .stop = pppoe_seq_stop,
  890. .show = pppoe_seq_show,
  891. };
  892. static int pppoe_seq_open(struct inode *inode, struct file *file)
  893. {
  894. return seq_open_net(inode, file, &pppoe_seq_ops,
  895. sizeof(struct seq_net_private));
  896. }
  897. static const struct file_operations pppoe_seq_fops = {
  898. .owner = THIS_MODULE,
  899. .open = pppoe_seq_open,
  900. .read = seq_read,
  901. .llseek = seq_lseek,
  902. .release = seq_release_net,
  903. };
  904. #endif /* CONFIG_PROC_FS */
  905. static const struct proto_ops pppoe_ops = {
  906. .family = AF_PPPOX,
  907. .owner = THIS_MODULE,
  908. .release = pppoe_release,
  909. .bind = sock_no_bind,
  910. .connect = pppoe_connect,
  911. .socketpair = sock_no_socketpair,
  912. .accept = sock_no_accept,
  913. .getname = pppoe_getname,
  914. .poll = datagram_poll,
  915. .listen = sock_no_listen,
  916. .shutdown = sock_no_shutdown,
  917. .setsockopt = sock_no_setsockopt,
  918. .getsockopt = sock_no_getsockopt,
  919. .sendmsg = pppoe_sendmsg,
  920. .recvmsg = pppoe_recvmsg,
  921. .mmap = sock_no_mmap,
  922. .ioctl = pppox_ioctl,
  923. };
  924. static const struct pppox_proto pppoe_proto = {
  925. .create = pppoe_create,
  926. .ioctl = pppoe_ioctl,
  927. .owner = THIS_MODULE,
  928. };
  929. static __net_init int pppoe_init_net(struct net *net)
  930. {
  931. struct pppoe_net *pn = pppoe_pernet(net);
  932. struct proc_dir_entry *pde;
  933. rwlock_init(&pn->hash_lock);
  934. pde = proc_net_fops_create(net, "pppoe", S_IRUGO, &pppoe_seq_fops);
  935. #ifdef CONFIG_PROC_FS
  936. if (!pde)
  937. return -ENOMEM;
  938. #endif
  939. return 0;
  940. }
  941. static __net_exit void pppoe_exit_net(struct net *net)
  942. {
  943. proc_net_remove(net, "pppoe");
  944. }
  945. static struct pernet_operations pppoe_net_ops = {
  946. .init = pppoe_init_net,
  947. .exit = pppoe_exit_net,
  948. .id = &pppoe_net_id,
  949. .size = sizeof(struct pppoe_net),
  950. };
  951. static int __init pppoe_init(void)
  952. {
  953. int err;
  954. err = register_pernet_device(&pppoe_net_ops);
  955. if (err)
  956. goto out;
  957. err = proto_register(&pppoe_sk_proto, 0);
  958. if (err)
  959. goto out_unregister_net_ops;
  960. err = register_pppox_proto(PX_PROTO_OE, &pppoe_proto);
  961. if (err)
  962. goto out_unregister_pppoe_proto;
  963. dev_add_pack(&pppoes_ptype);
  964. dev_add_pack(&pppoed_ptype);
  965. register_netdevice_notifier(&pppoe_notifier);
  966. return 0;
  967. out_unregister_pppoe_proto:
  968. proto_unregister(&pppoe_sk_proto);
  969. out_unregister_net_ops:
  970. unregister_pernet_device(&pppoe_net_ops);
  971. out:
  972. return err;
  973. }
  974. static void __exit pppoe_exit(void)
  975. {
  976. unregister_netdevice_notifier(&pppoe_notifier);
  977. dev_remove_pack(&pppoed_ptype);
  978. dev_remove_pack(&pppoes_ptype);
  979. unregister_pppox_proto(PX_PROTO_OE);
  980. proto_unregister(&pppoe_sk_proto);
  981. unregister_pernet_device(&pppoe_net_ops);
  982. }
  983. module_init(pppoe_init);
  984. module_exit(pppoe_exit);
  985. MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>");
  986. MODULE_DESCRIPTION("PPP over Ethernet driver");
  987. MODULE_LICENSE("GPL");
  988. MODULE_ALIAS_NETPROTO(PF_PPPOX);