pppoe.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  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/if_ppp.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)) {
  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);
  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. m->msg_namelen = 0;
  808. if (skb) {
  809. total_len = min_t(size_t, total_len, skb->len);
  810. error = skb_copy_datagram_iovec(skb, 0, m->msg_iov, total_len);
  811. if (error == 0)
  812. error = total_len;
  813. }
  814. kfree_skb(skb);
  815. end:
  816. return error;
  817. }
  818. #ifdef CONFIG_PROC_FS
  819. static int pppoe_seq_show(struct seq_file *seq, void *v)
  820. {
  821. struct pppox_sock *po;
  822. char *dev_name;
  823. if (v == SEQ_START_TOKEN) {
  824. seq_puts(seq, "Id Address Device\n");
  825. goto out;
  826. }
  827. po = v;
  828. dev_name = po->pppoe_pa.dev;
  829. seq_printf(seq, "%08X %pM %8s\n",
  830. po->pppoe_pa.sid, po->pppoe_pa.remote, dev_name);
  831. out:
  832. return 0;
  833. }
  834. static inline struct pppox_sock *pppoe_get_idx(struct pppoe_net *pn, loff_t pos)
  835. {
  836. struct pppox_sock *po;
  837. int i;
  838. for (i = 0; i < PPPOE_HASH_SIZE; i++) {
  839. po = pn->hash_table[i];
  840. while (po) {
  841. if (!pos--)
  842. goto out;
  843. po = po->next;
  844. }
  845. }
  846. out:
  847. return po;
  848. }
  849. static void *pppoe_seq_start(struct seq_file *seq, loff_t *pos)
  850. __acquires(pn->hash_lock)
  851. {
  852. struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
  853. loff_t l = *pos;
  854. read_lock_bh(&pn->hash_lock);
  855. return l ? pppoe_get_idx(pn, --l) : SEQ_START_TOKEN;
  856. }
  857. static void *pppoe_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  858. {
  859. struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
  860. struct pppox_sock *po;
  861. ++*pos;
  862. if (v == SEQ_START_TOKEN) {
  863. po = pppoe_get_idx(pn, 0);
  864. goto out;
  865. }
  866. po = v;
  867. if (po->next)
  868. po = po->next;
  869. else {
  870. int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
  871. po = NULL;
  872. while (++hash < PPPOE_HASH_SIZE) {
  873. po = pn->hash_table[hash];
  874. if (po)
  875. break;
  876. }
  877. }
  878. out:
  879. return po;
  880. }
  881. static void pppoe_seq_stop(struct seq_file *seq, void *v)
  882. __releases(pn->hash_lock)
  883. {
  884. struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
  885. read_unlock_bh(&pn->hash_lock);
  886. }
  887. static const struct seq_operations pppoe_seq_ops = {
  888. .start = pppoe_seq_start,
  889. .next = pppoe_seq_next,
  890. .stop = pppoe_seq_stop,
  891. .show = pppoe_seq_show,
  892. };
  893. static int pppoe_seq_open(struct inode *inode, struct file *file)
  894. {
  895. return seq_open_net(inode, file, &pppoe_seq_ops,
  896. sizeof(struct seq_net_private));
  897. }
  898. static const struct file_operations pppoe_seq_fops = {
  899. .owner = THIS_MODULE,
  900. .open = pppoe_seq_open,
  901. .read = seq_read,
  902. .llseek = seq_lseek,
  903. .release = seq_release_net,
  904. };
  905. #endif /* CONFIG_PROC_FS */
  906. static const struct proto_ops pppoe_ops = {
  907. .family = AF_PPPOX,
  908. .owner = THIS_MODULE,
  909. .release = pppoe_release,
  910. .bind = sock_no_bind,
  911. .connect = pppoe_connect,
  912. .socketpair = sock_no_socketpair,
  913. .accept = sock_no_accept,
  914. .getname = pppoe_getname,
  915. .poll = datagram_poll,
  916. .listen = sock_no_listen,
  917. .shutdown = sock_no_shutdown,
  918. .setsockopt = sock_no_setsockopt,
  919. .getsockopt = sock_no_getsockopt,
  920. .sendmsg = pppoe_sendmsg,
  921. .recvmsg = pppoe_recvmsg,
  922. .mmap = sock_no_mmap,
  923. .ioctl = pppox_ioctl,
  924. };
  925. static const struct pppox_proto pppoe_proto = {
  926. .create = pppoe_create,
  927. .ioctl = pppoe_ioctl,
  928. .owner = THIS_MODULE,
  929. };
  930. static __net_init int pppoe_init_net(struct net *net)
  931. {
  932. struct pppoe_net *pn = pppoe_pernet(net);
  933. struct proc_dir_entry *pde;
  934. rwlock_init(&pn->hash_lock);
  935. pde = proc_net_fops_create(net, "pppoe", S_IRUGO, &pppoe_seq_fops);
  936. #ifdef CONFIG_PROC_FS
  937. if (!pde)
  938. return -ENOMEM;
  939. #endif
  940. return 0;
  941. }
  942. static __net_exit void pppoe_exit_net(struct net *net)
  943. {
  944. proc_net_remove(net, "pppoe");
  945. }
  946. static struct pernet_operations pppoe_net_ops = {
  947. .init = pppoe_init_net,
  948. .exit = pppoe_exit_net,
  949. .id = &pppoe_net_id,
  950. .size = sizeof(struct pppoe_net),
  951. };
  952. static int __init pppoe_init(void)
  953. {
  954. int err;
  955. err = register_pernet_device(&pppoe_net_ops);
  956. if (err)
  957. goto out;
  958. err = proto_register(&pppoe_sk_proto, 0);
  959. if (err)
  960. goto out_unregister_net_ops;
  961. err = register_pppox_proto(PX_PROTO_OE, &pppoe_proto);
  962. if (err)
  963. goto out_unregister_pppoe_proto;
  964. dev_add_pack(&pppoes_ptype);
  965. dev_add_pack(&pppoed_ptype);
  966. register_netdevice_notifier(&pppoe_notifier);
  967. return 0;
  968. out_unregister_pppoe_proto:
  969. proto_unregister(&pppoe_sk_proto);
  970. out_unregister_net_ops:
  971. unregister_pernet_device(&pppoe_net_ops);
  972. out:
  973. return err;
  974. }
  975. static void __exit pppoe_exit(void)
  976. {
  977. unregister_netdevice_notifier(&pppoe_notifier);
  978. dev_remove_pack(&pppoed_ptype);
  979. dev_remove_pack(&pppoes_ptype);
  980. unregister_pppox_proto(PX_PROTO_OE);
  981. proto_unregister(&pppoe_sk_proto);
  982. unregister_pernet_device(&pppoe_net_ops);
  983. }
  984. module_init(pppoe_init);
  985. module_exit(pppoe_exit);
  986. MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>");
  987. MODULE_DESCRIPTION("PPP over Ethernet driver");
  988. MODULE_LICENSE("GPL");
  989. MODULE_ALIAS_NETPROTO(PF_PPPOX);