pppoe.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  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 && ether_addr_equal(a->remote, b->remote);
  124. }
  125. static inline int cmp_addr(struct pppoe_addr *a, __be16 sid, char *addr)
  126. {
  127. return a->sid == sid && ether_addr_equal(a->remote, addr);
  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 void __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. }
  195. /**********************************************************************
  196. *
  197. * Set/get/delete/rehash items
  198. *
  199. **********************************************************************/
  200. static inline struct pppox_sock *get_item(struct pppoe_net *pn, __be16 sid,
  201. unsigned char *addr, int ifindex)
  202. {
  203. struct pppox_sock *po;
  204. read_lock_bh(&pn->hash_lock);
  205. po = __get_item(pn, sid, addr, ifindex);
  206. if (po)
  207. sock_hold(sk_pppox(po));
  208. read_unlock_bh(&pn->hash_lock);
  209. return po;
  210. }
  211. static inline struct pppox_sock *get_item_by_addr(struct net *net,
  212. struct sockaddr_pppox *sp)
  213. {
  214. struct net_device *dev;
  215. struct pppoe_net *pn;
  216. struct pppox_sock *pppox_sock = NULL;
  217. int ifindex;
  218. rcu_read_lock();
  219. dev = dev_get_by_name_rcu(net, sp->sa_addr.pppoe.dev);
  220. if (dev) {
  221. ifindex = dev->ifindex;
  222. pn = pppoe_pernet(net);
  223. pppox_sock = get_item(pn, sp->sa_addr.pppoe.sid,
  224. sp->sa_addr.pppoe.remote, ifindex);
  225. }
  226. rcu_read_unlock();
  227. return pppox_sock;
  228. }
  229. static inline void delete_item(struct pppoe_net *pn, __be16 sid,
  230. char *addr, int ifindex)
  231. {
  232. write_lock_bh(&pn->hash_lock);
  233. __delete_item(pn, sid, addr, ifindex);
  234. write_unlock_bh(&pn->hash_lock);
  235. }
  236. /***************************************************************************
  237. *
  238. * Handler for device events.
  239. * Certain device events require that sockets be unconnected.
  240. *
  241. **************************************************************************/
  242. static void pppoe_flush_dev(struct net_device *dev)
  243. {
  244. struct pppoe_net *pn;
  245. int i;
  246. pn = pppoe_pernet(dev_net(dev));
  247. write_lock_bh(&pn->hash_lock);
  248. for (i = 0; i < PPPOE_HASH_SIZE; i++) {
  249. struct pppox_sock *po = pn->hash_table[i];
  250. struct sock *sk;
  251. while (po) {
  252. while (po && po->pppoe_dev != dev) {
  253. po = po->next;
  254. }
  255. if (!po)
  256. break;
  257. sk = sk_pppox(po);
  258. /* We always grab the socket lock, followed by the
  259. * hash_lock, in that order. Since we should hold the
  260. * sock lock while doing any unbinding, we need to
  261. * release the lock we're holding. Hold a reference to
  262. * the sock so it doesn't disappear as we're jumping
  263. * between locks.
  264. */
  265. sock_hold(sk);
  266. write_unlock_bh(&pn->hash_lock);
  267. lock_sock(sk);
  268. if (po->pppoe_dev == dev &&
  269. sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND | PPPOX_ZOMBIE)) {
  270. pppox_unbind_sock(sk);
  271. sk->sk_state_change(sk);
  272. po->pppoe_dev = NULL;
  273. dev_put(dev);
  274. }
  275. release_sock(sk);
  276. sock_put(sk);
  277. /* Restart the process from the start of the current
  278. * hash chain. We dropped locks so the world may have
  279. * change from underneath us.
  280. */
  281. BUG_ON(pppoe_pernet(dev_net(dev)) == NULL);
  282. write_lock_bh(&pn->hash_lock);
  283. po = pn->hash_table[i];
  284. }
  285. }
  286. write_unlock_bh(&pn->hash_lock);
  287. }
  288. static int pppoe_device_event(struct notifier_block *this,
  289. unsigned long event, void *ptr)
  290. {
  291. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  292. /* Only look at sockets that are using this specific device. */
  293. switch (event) {
  294. case NETDEV_CHANGEADDR:
  295. case NETDEV_CHANGEMTU:
  296. /* A change in mtu or address is a bad thing, requiring
  297. * LCP re-negotiation.
  298. */
  299. case NETDEV_GOING_DOWN:
  300. case NETDEV_DOWN:
  301. /* Find every socket on this device and kill it. */
  302. pppoe_flush_dev(dev);
  303. break;
  304. default:
  305. break;
  306. }
  307. return NOTIFY_DONE;
  308. }
  309. static struct notifier_block pppoe_notifier = {
  310. .notifier_call = pppoe_device_event,
  311. };
  312. /************************************************************************
  313. *
  314. * Do the real work of receiving a PPPoE Session frame.
  315. *
  316. ***********************************************************************/
  317. static int pppoe_rcv_core(struct sock *sk, struct sk_buff *skb)
  318. {
  319. struct pppox_sock *po = pppox_sk(sk);
  320. struct pppox_sock *relay_po;
  321. /* Backlog receive. Semantics of backlog rcv preclude any code from
  322. * executing in lock_sock()/release_sock() bounds; meaning sk->sk_state
  323. * can't change.
  324. */
  325. if (skb->pkt_type == PACKET_OTHERHOST)
  326. goto abort_kfree;
  327. if (sk->sk_state & PPPOX_BOUND) {
  328. ppp_input(&po->chan, skb);
  329. } else if (sk->sk_state & PPPOX_RELAY) {
  330. relay_po = get_item_by_addr(sock_net(sk),
  331. &po->pppoe_relay);
  332. if (relay_po == NULL)
  333. goto abort_kfree;
  334. if ((sk_pppox(relay_po)->sk_state & PPPOX_CONNECTED) == 0)
  335. goto abort_put;
  336. if (!__pppoe_xmit(sk_pppox(relay_po), skb))
  337. goto abort_put;
  338. sock_put(sk_pppox(relay_po));
  339. } else {
  340. if (sock_queue_rcv_skb(sk, skb))
  341. goto abort_kfree;
  342. }
  343. return NET_RX_SUCCESS;
  344. abort_put:
  345. sock_put(sk_pppox(relay_po));
  346. abort_kfree:
  347. kfree_skb(skb);
  348. return NET_RX_DROP;
  349. }
  350. /************************************************************************
  351. *
  352. * Receive wrapper called in BH context.
  353. *
  354. ***********************************************************************/
  355. static int pppoe_rcv(struct sk_buff *skb, struct net_device *dev,
  356. struct packet_type *pt, struct net_device *orig_dev)
  357. {
  358. struct pppoe_hdr *ph;
  359. struct pppox_sock *po;
  360. struct pppoe_net *pn;
  361. int len;
  362. skb = skb_share_check(skb, GFP_ATOMIC);
  363. if (!skb)
  364. goto out;
  365. if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
  366. goto drop;
  367. ph = pppoe_hdr(skb);
  368. len = ntohs(ph->length);
  369. skb_pull_rcsum(skb, sizeof(*ph));
  370. if (skb->len < len)
  371. goto drop;
  372. if (pskb_trim_rcsum(skb, len))
  373. goto drop;
  374. ph = pppoe_hdr(skb);
  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. static void pppoe_unbind_sock_work(struct work_struct *work)
  389. {
  390. struct pppox_sock *po = container_of(work, struct pppox_sock,
  391. proto.pppoe.padt_work);
  392. struct sock *sk = sk_pppox(po);
  393. lock_sock(sk);
  394. if (po->pppoe_dev) {
  395. dev_put(po->pppoe_dev);
  396. po->pppoe_dev = NULL;
  397. }
  398. pppox_unbind_sock(sk);
  399. release_sock(sk);
  400. sock_put(sk);
  401. }
  402. /************************************************************************
  403. *
  404. * Receive a PPPoE Discovery frame.
  405. * This is solely for detection of PADT frames
  406. *
  407. ***********************************************************************/
  408. static int pppoe_disc_rcv(struct sk_buff *skb, struct net_device *dev,
  409. struct packet_type *pt, struct net_device *orig_dev)
  410. {
  411. struct pppoe_hdr *ph;
  412. struct pppox_sock *po;
  413. struct pppoe_net *pn;
  414. skb = skb_share_check(skb, GFP_ATOMIC);
  415. if (!skb)
  416. goto out;
  417. if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
  418. goto abort;
  419. ph = pppoe_hdr(skb);
  420. if (ph->code != PADT_CODE)
  421. goto abort;
  422. pn = pppoe_pernet(dev_net(dev));
  423. po = get_item(pn, ph->sid, eth_hdr(skb)->h_source, dev->ifindex);
  424. if (po) {
  425. struct sock *sk = sk_pppox(po);
  426. bh_lock_sock(sk);
  427. /* If the user has locked the socket, just ignore
  428. * the packet. With the way two rcv protocols hook into
  429. * one socket family type, we cannot (easily) distinguish
  430. * what kind of SKB it is during backlog rcv.
  431. */
  432. if (sock_owned_by_user(sk) == 0) {
  433. /* We're no longer connect at the PPPOE layer,
  434. * and must wait for ppp channel to disconnect us.
  435. */
  436. sk->sk_state = PPPOX_ZOMBIE;
  437. }
  438. bh_unlock_sock(sk);
  439. if (!schedule_work(&po->proto.pppoe.padt_work))
  440. sock_put(sk);
  441. }
  442. abort:
  443. kfree_skb(skb);
  444. out:
  445. return NET_RX_SUCCESS; /* Lies... :-) */
  446. }
  447. static struct packet_type pppoes_ptype __read_mostly = {
  448. .type = cpu_to_be16(ETH_P_PPP_SES),
  449. .func = pppoe_rcv,
  450. };
  451. static struct packet_type pppoed_ptype __read_mostly = {
  452. .type = cpu_to_be16(ETH_P_PPP_DISC),
  453. .func = pppoe_disc_rcv,
  454. };
  455. static struct proto pppoe_sk_proto __read_mostly = {
  456. .name = "PPPOE",
  457. .owner = THIS_MODULE,
  458. .obj_size = sizeof(struct pppox_sock),
  459. };
  460. /***********************************************************************
  461. *
  462. * Initialize a new struct sock.
  463. *
  464. **********************************************************************/
  465. static int pppoe_create(struct net *net, struct socket *sock, int kern)
  466. {
  467. struct sock *sk;
  468. sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppoe_sk_proto, kern);
  469. if (!sk)
  470. return -ENOMEM;
  471. sock_init_data(sock, sk);
  472. sock->state = SS_UNCONNECTED;
  473. sock->ops = &pppoe_ops;
  474. sk->sk_backlog_rcv = pppoe_rcv_core;
  475. sk->sk_state = PPPOX_NONE;
  476. sk->sk_type = SOCK_STREAM;
  477. sk->sk_family = PF_PPPOX;
  478. sk->sk_protocol = PX_PROTO_OE;
  479. INIT_WORK(&pppox_sk(sk)->proto.pppoe.padt_work,
  480. pppoe_unbind_sock_work);
  481. return 0;
  482. }
  483. static int pppoe_release(struct socket *sock)
  484. {
  485. struct sock *sk = sock->sk;
  486. struct pppox_sock *po;
  487. struct pppoe_net *pn;
  488. struct net *net = NULL;
  489. if (!sk)
  490. return 0;
  491. lock_sock(sk);
  492. if (sock_flag(sk, SOCK_DEAD)) {
  493. release_sock(sk);
  494. return -EBADF;
  495. }
  496. po = pppox_sk(sk);
  497. if (po->pppoe_dev) {
  498. dev_put(po->pppoe_dev);
  499. po->pppoe_dev = NULL;
  500. }
  501. pppox_unbind_sock(sk);
  502. /* Signal the death of the socket. */
  503. sk->sk_state = PPPOX_DEAD;
  504. net = sock_net(sk);
  505. pn = pppoe_pernet(net);
  506. /*
  507. * protect "po" from concurrent updates
  508. * on pppoe_flush_dev
  509. */
  510. delete_item(pn, po->pppoe_pa.sid, po->pppoe_pa.remote,
  511. po->pppoe_ifindex);
  512. sock_orphan(sk);
  513. sock->sk = NULL;
  514. skb_queue_purge(&sk->sk_receive_queue);
  515. release_sock(sk);
  516. sock_put(sk);
  517. return 0;
  518. }
  519. static int pppoe_connect(struct socket *sock, struct sockaddr *uservaddr,
  520. int sockaddr_len, int flags)
  521. {
  522. struct sock *sk = sock->sk;
  523. struct sockaddr_pppox *sp = (struct sockaddr_pppox *)uservaddr;
  524. struct pppox_sock *po = pppox_sk(sk);
  525. struct net_device *dev = NULL;
  526. struct pppoe_net *pn;
  527. struct net *net = NULL;
  528. int error;
  529. lock_sock(sk);
  530. error = -EINVAL;
  531. if (sockaddr_len != sizeof(struct sockaddr_pppox))
  532. goto end;
  533. if (sp->sa_protocol != PX_PROTO_OE)
  534. goto end;
  535. /* Check for already bound sockets */
  536. error = -EBUSY;
  537. if ((sk->sk_state & PPPOX_CONNECTED) &&
  538. stage_session(sp->sa_addr.pppoe.sid))
  539. goto end;
  540. /* Check for already disconnected sockets, on attempts to disconnect */
  541. error = -EALREADY;
  542. if ((sk->sk_state & PPPOX_DEAD) &&
  543. !stage_session(sp->sa_addr.pppoe.sid))
  544. goto end;
  545. error = 0;
  546. /* Delete the old binding */
  547. if (stage_session(po->pppoe_pa.sid)) {
  548. pppox_unbind_sock(sk);
  549. pn = pppoe_pernet(sock_net(sk));
  550. delete_item(pn, po->pppoe_pa.sid,
  551. po->pppoe_pa.remote, po->pppoe_ifindex);
  552. if (po->pppoe_dev) {
  553. dev_put(po->pppoe_dev);
  554. po->pppoe_dev = NULL;
  555. }
  556. po->pppoe_ifindex = 0;
  557. memset(&po->pppoe_pa, 0, sizeof(po->pppoe_pa));
  558. memset(&po->pppoe_relay, 0, sizeof(po->pppoe_relay));
  559. memset(&po->chan, 0, sizeof(po->chan));
  560. po->next = NULL;
  561. po->num = 0;
  562. sk->sk_state = PPPOX_NONE;
  563. }
  564. /* Re-bind in session stage only */
  565. if (stage_session(sp->sa_addr.pppoe.sid)) {
  566. error = -ENODEV;
  567. net = sock_net(sk);
  568. dev = dev_get_by_name(net, sp->sa_addr.pppoe.dev);
  569. if (!dev)
  570. goto err_put;
  571. po->pppoe_dev = dev;
  572. po->pppoe_ifindex = dev->ifindex;
  573. pn = pppoe_pernet(net);
  574. if (!(dev->flags & IFF_UP)) {
  575. goto err_put;
  576. }
  577. memcpy(&po->pppoe_pa,
  578. &sp->sa_addr.pppoe,
  579. sizeof(struct pppoe_addr));
  580. write_lock_bh(&pn->hash_lock);
  581. error = __set_item(pn, po);
  582. write_unlock_bh(&pn->hash_lock);
  583. if (error < 0)
  584. goto err_put;
  585. po->chan.hdrlen = (sizeof(struct pppoe_hdr) +
  586. dev->hard_header_len);
  587. po->chan.mtu = dev->mtu - sizeof(struct pppoe_hdr) - 2;
  588. po->chan.private = sk;
  589. po->chan.ops = &pppoe_chan_ops;
  590. error = ppp_register_net_channel(dev_net(dev), &po->chan);
  591. if (error) {
  592. delete_item(pn, po->pppoe_pa.sid,
  593. po->pppoe_pa.remote, po->pppoe_ifindex);
  594. goto err_put;
  595. }
  596. sk->sk_state = PPPOX_CONNECTED;
  597. }
  598. po->num = sp->sa_addr.pppoe.sid;
  599. end:
  600. release_sock(sk);
  601. return error;
  602. err_put:
  603. if (po->pppoe_dev) {
  604. dev_put(po->pppoe_dev);
  605. po->pppoe_dev = NULL;
  606. }
  607. goto end;
  608. }
  609. static int pppoe_getname(struct socket *sock, struct sockaddr *uaddr,
  610. int *usockaddr_len, int peer)
  611. {
  612. int len = sizeof(struct sockaddr_pppox);
  613. struct sockaddr_pppox sp;
  614. sp.sa_family = AF_PPPOX;
  615. sp.sa_protocol = PX_PROTO_OE;
  616. memcpy(&sp.sa_addr.pppoe, &pppox_sk(sock->sk)->pppoe_pa,
  617. sizeof(struct pppoe_addr));
  618. memcpy(uaddr, &sp, len);
  619. *usockaddr_len = len;
  620. return 0;
  621. }
  622. static int pppoe_ioctl(struct socket *sock, unsigned int cmd,
  623. unsigned long arg)
  624. {
  625. struct sock *sk = sock->sk;
  626. struct pppox_sock *po = pppox_sk(sk);
  627. int val;
  628. int err;
  629. switch (cmd) {
  630. case PPPIOCGMRU:
  631. err = -ENXIO;
  632. if (!(sk->sk_state & PPPOX_CONNECTED))
  633. break;
  634. err = -EFAULT;
  635. if (put_user(po->pppoe_dev->mtu -
  636. sizeof(struct pppoe_hdr) -
  637. PPP_HDRLEN,
  638. (int __user *)arg))
  639. break;
  640. err = 0;
  641. break;
  642. case PPPIOCSMRU:
  643. err = -ENXIO;
  644. if (!(sk->sk_state & PPPOX_CONNECTED))
  645. break;
  646. err = -EFAULT;
  647. if (get_user(val, (int __user *)arg))
  648. break;
  649. if (val < (po->pppoe_dev->mtu
  650. - sizeof(struct pppoe_hdr)
  651. - PPP_HDRLEN))
  652. err = 0;
  653. else
  654. err = -EINVAL;
  655. break;
  656. case PPPIOCSFLAGS:
  657. err = -EFAULT;
  658. if (get_user(val, (int __user *)arg))
  659. break;
  660. err = 0;
  661. break;
  662. case PPPOEIOCSFWD:
  663. {
  664. struct pppox_sock *relay_po;
  665. err = -EBUSY;
  666. if (sk->sk_state & (PPPOX_BOUND | PPPOX_ZOMBIE | PPPOX_DEAD))
  667. break;
  668. err = -ENOTCONN;
  669. if (!(sk->sk_state & PPPOX_CONNECTED))
  670. break;
  671. /* PPPoE address from the user specifies an outbound
  672. PPPoE address which frames are forwarded to */
  673. err = -EFAULT;
  674. if (copy_from_user(&po->pppoe_relay,
  675. (void __user *)arg,
  676. sizeof(struct sockaddr_pppox)))
  677. break;
  678. err = -EINVAL;
  679. if (po->pppoe_relay.sa_family != AF_PPPOX ||
  680. po->pppoe_relay.sa_protocol != PX_PROTO_OE)
  681. break;
  682. /* Check that the socket referenced by the address
  683. actually exists. */
  684. relay_po = get_item_by_addr(sock_net(sk), &po->pppoe_relay);
  685. if (!relay_po)
  686. break;
  687. sock_put(sk_pppox(relay_po));
  688. sk->sk_state |= PPPOX_RELAY;
  689. err = 0;
  690. break;
  691. }
  692. case PPPOEIOCDFWD:
  693. err = -EALREADY;
  694. if (!(sk->sk_state & PPPOX_RELAY))
  695. break;
  696. sk->sk_state &= ~PPPOX_RELAY;
  697. err = 0;
  698. break;
  699. default:
  700. err = -ENOTTY;
  701. }
  702. return err;
  703. }
  704. static int pppoe_sendmsg(struct socket *sock, struct msghdr *m,
  705. size_t total_len)
  706. {
  707. struct sk_buff *skb;
  708. struct sock *sk = sock->sk;
  709. struct pppox_sock *po = pppox_sk(sk);
  710. int error;
  711. struct pppoe_hdr hdr;
  712. struct pppoe_hdr *ph;
  713. struct net_device *dev;
  714. char *start;
  715. int hlen;
  716. lock_sock(sk);
  717. if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) {
  718. error = -ENOTCONN;
  719. goto end;
  720. }
  721. hdr.ver = 1;
  722. hdr.type = 1;
  723. hdr.code = 0;
  724. hdr.sid = po->num;
  725. dev = po->pppoe_dev;
  726. error = -EMSGSIZE;
  727. if (total_len > (dev->mtu + dev->hard_header_len))
  728. goto end;
  729. hlen = LL_RESERVED_SPACE(dev);
  730. skb = sock_wmalloc(sk, hlen + sizeof(*ph) + total_len +
  731. dev->needed_tailroom, 0, GFP_KERNEL);
  732. if (!skb) {
  733. error = -ENOMEM;
  734. goto end;
  735. }
  736. /* Reserve space for headers. */
  737. skb_reserve(skb, hlen);
  738. skb_reset_network_header(skb);
  739. skb->dev = dev;
  740. skb->priority = sk->sk_priority;
  741. skb->protocol = cpu_to_be16(ETH_P_PPP_SES);
  742. ph = (struct pppoe_hdr *)skb_put(skb, total_len + sizeof(struct pppoe_hdr));
  743. start = (char *)&ph->tag[0];
  744. error = memcpy_from_msg(start, m, total_len);
  745. if (error < 0) {
  746. kfree_skb(skb);
  747. goto end;
  748. }
  749. error = total_len;
  750. dev_hard_header(skb, dev, ETH_P_PPP_SES,
  751. po->pppoe_pa.remote, NULL, total_len);
  752. memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
  753. ph->length = htons(total_len);
  754. dev_queue_xmit(skb);
  755. end:
  756. release_sock(sk);
  757. return error;
  758. }
  759. /************************************************************************
  760. *
  761. * xmit function for internal use.
  762. *
  763. ***********************************************************************/
  764. static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb)
  765. {
  766. struct pppox_sock *po = pppox_sk(sk);
  767. struct net_device *dev = po->pppoe_dev;
  768. struct pppoe_hdr *ph;
  769. int data_len = skb->len;
  770. /* The higher-level PPP code (ppp_unregister_channel()) ensures the PPP
  771. * xmit operations conclude prior to an unregistration call. Thus
  772. * sk->sk_state cannot change, so we don't need to do lock_sock().
  773. * But, we also can't do a lock_sock since that introduces a potential
  774. * deadlock as we'd reverse the lock ordering used when calling
  775. * ppp_unregister_channel().
  776. */
  777. if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
  778. goto abort;
  779. if (!dev)
  780. goto abort;
  781. /* Copy the data if there is no space for the header or if it's
  782. * read-only.
  783. */
  784. if (skb_cow_head(skb, LL_RESERVED_SPACE(dev) + sizeof(*ph)))
  785. goto abort;
  786. __skb_push(skb, sizeof(*ph));
  787. skb_reset_network_header(skb);
  788. ph = pppoe_hdr(skb);
  789. ph->ver = 1;
  790. ph->type = 1;
  791. ph->code = 0;
  792. ph->sid = po->num;
  793. ph->length = htons(data_len);
  794. skb->protocol = cpu_to_be16(ETH_P_PPP_SES);
  795. skb->dev = dev;
  796. dev_hard_header(skb, dev, ETH_P_PPP_SES,
  797. po->pppoe_pa.remote, NULL, data_len);
  798. dev_queue_xmit(skb);
  799. return 1;
  800. abort:
  801. kfree_skb(skb);
  802. return 1;
  803. }
  804. /************************************************************************
  805. *
  806. * xmit function called by generic PPP driver
  807. * sends PPP frame over PPPoE socket
  808. *
  809. ***********************************************************************/
  810. static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb)
  811. {
  812. struct sock *sk = (struct sock *)chan->private;
  813. return __pppoe_xmit(sk, skb);
  814. }
  815. static const struct ppp_channel_ops pppoe_chan_ops = {
  816. .start_xmit = pppoe_xmit,
  817. };
  818. static int pppoe_recvmsg(struct socket *sock, struct msghdr *m,
  819. size_t total_len, int flags)
  820. {
  821. struct sock *sk = sock->sk;
  822. struct sk_buff *skb;
  823. int error = 0;
  824. if (sk->sk_state & PPPOX_BOUND) {
  825. error = -EIO;
  826. goto end;
  827. }
  828. skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
  829. flags & MSG_DONTWAIT, &error);
  830. if (error < 0)
  831. goto end;
  832. if (skb) {
  833. total_len = min_t(size_t, total_len, skb->len);
  834. error = skb_copy_datagram_msg(skb, 0, m, total_len);
  835. if (error == 0) {
  836. consume_skb(skb);
  837. return total_len;
  838. }
  839. }
  840. kfree_skb(skb);
  841. end:
  842. return error;
  843. }
  844. #ifdef CONFIG_PROC_FS
  845. static int pppoe_seq_show(struct seq_file *seq, void *v)
  846. {
  847. struct pppox_sock *po;
  848. char *dev_name;
  849. if (v == SEQ_START_TOKEN) {
  850. seq_puts(seq, "Id Address Device\n");
  851. goto out;
  852. }
  853. po = v;
  854. dev_name = po->pppoe_pa.dev;
  855. seq_printf(seq, "%08X %pM %8s\n",
  856. po->pppoe_pa.sid, po->pppoe_pa.remote, dev_name);
  857. out:
  858. return 0;
  859. }
  860. static inline struct pppox_sock *pppoe_get_idx(struct pppoe_net *pn, loff_t pos)
  861. {
  862. struct pppox_sock *po;
  863. int i;
  864. for (i = 0; i < PPPOE_HASH_SIZE; i++) {
  865. po = pn->hash_table[i];
  866. while (po) {
  867. if (!pos--)
  868. goto out;
  869. po = po->next;
  870. }
  871. }
  872. out:
  873. return po;
  874. }
  875. static void *pppoe_seq_start(struct seq_file *seq, loff_t *pos)
  876. __acquires(pn->hash_lock)
  877. {
  878. struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
  879. loff_t l = *pos;
  880. read_lock_bh(&pn->hash_lock);
  881. return l ? pppoe_get_idx(pn, --l) : SEQ_START_TOKEN;
  882. }
  883. static void *pppoe_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  884. {
  885. struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
  886. struct pppox_sock *po;
  887. ++*pos;
  888. if (v == SEQ_START_TOKEN) {
  889. po = pppoe_get_idx(pn, 0);
  890. goto out;
  891. }
  892. po = v;
  893. if (po->next)
  894. po = po->next;
  895. else {
  896. int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
  897. po = NULL;
  898. while (++hash < PPPOE_HASH_SIZE) {
  899. po = pn->hash_table[hash];
  900. if (po)
  901. break;
  902. }
  903. }
  904. out:
  905. return po;
  906. }
  907. static void pppoe_seq_stop(struct seq_file *seq, void *v)
  908. __releases(pn->hash_lock)
  909. {
  910. struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
  911. read_unlock_bh(&pn->hash_lock);
  912. }
  913. static const struct seq_operations pppoe_seq_ops = {
  914. .start = pppoe_seq_start,
  915. .next = pppoe_seq_next,
  916. .stop = pppoe_seq_stop,
  917. .show = pppoe_seq_show,
  918. };
  919. static int pppoe_seq_open(struct inode *inode, struct file *file)
  920. {
  921. return seq_open_net(inode, file, &pppoe_seq_ops,
  922. sizeof(struct seq_net_private));
  923. }
  924. static const struct file_operations pppoe_seq_fops = {
  925. .owner = THIS_MODULE,
  926. .open = pppoe_seq_open,
  927. .read = seq_read,
  928. .llseek = seq_lseek,
  929. .release = seq_release_net,
  930. };
  931. #endif /* CONFIG_PROC_FS */
  932. static const struct proto_ops pppoe_ops = {
  933. .family = AF_PPPOX,
  934. .owner = THIS_MODULE,
  935. .release = pppoe_release,
  936. .bind = sock_no_bind,
  937. .connect = pppoe_connect,
  938. .socketpair = sock_no_socketpair,
  939. .accept = sock_no_accept,
  940. .getname = pppoe_getname,
  941. .poll = datagram_poll,
  942. .listen = sock_no_listen,
  943. .shutdown = sock_no_shutdown,
  944. .setsockopt = sock_no_setsockopt,
  945. .getsockopt = sock_no_getsockopt,
  946. .sendmsg = pppoe_sendmsg,
  947. .recvmsg = pppoe_recvmsg,
  948. .mmap = sock_no_mmap,
  949. .ioctl = pppox_ioctl,
  950. };
  951. static const struct pppox_proto pppoe_proto = {
  952. .create = pppoe_create,
  953. .ioctl = pppoe_ioctl,
  954. .owner = THIS_MODULE,
  955. };
  956. static __net_init int pppoe_init_net(struct net *net)
  957. {
  958. struct pppoe_net *pn = pppoe_pernet(net);
  959. struct proc_dir_entry *pde;
  960. rwlock_init(&pn->hash_lock);
  961. pde = proc_create("pppoe", S_IRUGO, net->proc_net, &pppoe_seq_fops);
  962. #ifdef CONFIG_PROC_FS
  963. if (!pde)
  964. return -ENOMEM;
  965. #endif
  966. return 0;
  967. }
  968. static __net_exit void pppoe_exit_net(struct net *net)
  969. {
  970. remove_proc_entry("pppoe", net->proc_net);
  971. }
  972. static struct pernet_operations pppoe_net_ops = {
  973. .init = pppoe_init_net,
  974. .exit = pppoe_exit_net,
  975. .id = &pppoe_net_id,
  976. .size = sizeof(struct pppoe_net),
  977. };
  978. static int __init pppoe_init(void)
  979. {
  980. int err;
  981. err = register_pernet_device(&pppoe_net_ops);
  982. if (err)
  983. goto out;
  984. err = proto_register(&pppoe_sk_proto, 0);
  985. if (err)
  986. goto out_unregister_net_ops;
  987. err = register_pppox_proto(PX_PROTO_OE, &pppoe_proto);
  988. if (err)
  989. goto out_unregister_pppoe_proto;
  990. dev_add_pack(&pppoes_ptype);
  991. dev_add_pack(&pppoed_ptype);
  992. register_netdevice_notifier(&pppoe_notifier);
  993. return 0;
  994. out_unregister_pppoe_proto:
  995. proto_unregister(&pppoe_sk_proto);
  996. out_unregister_net_ops:
  997. unregister_pernet_device(&pppoe_net_ops);
  998. out:
  999. return err;
  1000. }
  1001. static void __exit pppoe_exit(void)
  1002. {
  1003. unregister_netdevice_notifier(&pppoe_notifier);
  1004. dev_remove_pack(&pppoed_ptype);
  1005. dev_remove_pack(&pppoes_ptype);
  1006. unregister_pppox_proto(PX_PROTO_OE);
  1007. proto_unregister(&pppoe_sk_proto);
  1008. unregister_pernet_device(&pppoe_net_ops);
  1009. }
  1010. module_init(pppoe_init);
  1011. module_exit(pppoe_exit);
  1012. MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>");
  1013. MODULE_DESCRIPTION("PPP over Ethernet driver");
  1014. MODULE_LICENSE("GPL");
  1015. MODULE_ALIAS_NETPROTO(PF_PPPOX);