pppoe.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  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 <net/sock.h>
  80. #include <asm/uaccess.h>
  81. #define PPPOE_HASH_BITS 4
  82. #define PPPOE_HASH_SIZE (1<<PPPOE_HASH_BITS)
  83. static struct ppp_channel_ops pppoe_chan_ops;
  84. static int pppoe_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
  85. static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb);
  86. static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb);
  87. static const struct proto_ops pppoe_ops;
  88. static DEFINE_RWLOCK(pppoe_hash_lock);
  89. static struct ppp_channel_ops pppoe_chan_ops;
  90. static inline int cmp_2_addr(struct pppoe_addr *a, struct pppoe_addr *b)
  91. {
  92. return (a->sid == b->sid &&
  93. (memcmp(a->remote, b->remote, ETH_ALEN) == 0));
  94. }
  95. static inline int cmp_addr(struct pppoe_addr *a, unsigned long sid, char *addr)
  96. {
  97. return (a->sid == sid &&
  98. (memcmp(a->remote,addr,ETH_ALEN) == 0));
  99. }
  100. static int hash_item(unsigned long sid, unsigned char *addr)
  101. {
  102. char hash = 0;
  103. int i, j;
  104. for (i = 0; i < ETH_ALEN ; ++i) {
  105. for (j = 0; j < 8/PPPOE_HASH_BITS ; ++j) {
  106. hash ^= addr[i] >> ( j * PPPOE_HASH_BITS );
  107. }
  108. }
  109. for (i = 0; i < (sizeof(unsigned long)*8) / PPPOE_HASH_BITS ; ++i)
  110. hash ^= sid >> (i*PPPOE_HASH_BITS);
  111. return hash & ( PPPOE_HASH_SIZE - 1 );
  112. }
  113. /* zeroed because its in .bss */
  114. static struct pppox_sock *item_hash_table[PPPOE_HASH_SIZE];
  115. /**********************************************************************
  116. *
  117. * Set/get/delete/rehash items (internal versions)
  118. *
  119. **********************************************************************/
  120. static struct pppox_sock *__get_item(unsigned long sid, unsigned char *addr, int ifindex)
  121. {
  122. int hash = hash_item(sid, addr);
  123. struct pppox_sock *ret;
  124. ret = item_hash_table[hash];
  125. while (ret && !(cmp_addr(&ret->pppoe_pa, sid, addr) && ret->pppoe_ifindex == ifindex))
  126. ret = ret->next;
  127. return ret;
  128. }
  129. static int __set_item(struct pppox_sock *po)
  130. {
  131. int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
  132. struct pppox_sock *ret;
  133. ret = item_hash_table[hash];
  134. while (ret) {
  135. if (cmp_2_addr(&ret->pppoe_pa, &po->pppoe_pa) && ret->pppoe_ifindex == po->pppoe_ifindex)
  136. return -EALREADY;
  137. ret = ret->next;
  138. }
  139. po->next = item_hash_table[hash];
  140. item_hash_table[hash] = po;
  141. return 0;
  142. }
  143. static struct pppox_sock *__delete_item(unsigned long sid, char *addr, int ifindex)
  144. {
  145. int hash = hash_item(sid, addr);
  146. struct pppox_sock *ret, **src;
  147. ret = item_hash_table[hash];
  148. src = &item_hash_table[hash];
  149. while (ret) {
  150. if (cmp_addr(&ret->pppoe_pa, sid, addr) && ret->pppoe_ifindex == ifindex) {
  151. *src = ret->next;
  152. break;
  153. }
  154. src = &ret->next;
  155. ret = ret->next;
  156. }
  157. return ret;
  158. }
  159. /**********************************************************************
  160. *
  161. * Set/get/delete/rehash items
  162. *
  163. **********************************************************************/
  164. static inline struct pppox_sock *get_item(unsigned long sid,
  165. unsigned char *addr, int ifindex)
  166. {
  167. struct pppox_sock *po;
  168. read_lock_bh(&pppoe_hash_lock);
  169. po = __get_item(sid, addr, ifindex);
  170. if (po)
  171. sock_hold(sk_pppox(po));
  172. read_unlock_bh(&pppoe_hash_lock);
  173. return po;
  174. }
  175. static inline struct pppox_sock *get_item_by_addr(struct sockaddr_pppox *sp)
  176. {
  177. struct net_device *dev;
  178. int ifindex;
  179. dev = dev_get_by_name(sp->sa_addr.pppoe.dev);
  180. if(!dev)
  181. return NULL;
  182. ifindex = dev->ifindex;
  183. dev_put(dev);
  184. return get_item(sp->sa_addr.pppoe.sid, sp->sa_addr.pppoe.remote, ifindex);
  185. }
  186. static inline struct pppox_sock *delete_item(unsigned long sid, char *addr, int ifindex)
  187. {
  188. struct pppox_sock *ret;
  189. write_lock_bh(&pppoe_hash_lock);
  190. ret = __delete_item(sid, addr, ifindex);
  191. write_unlock_bh(&pppoe_hash_lock);
  192. return ret;
  193. }
  194. /***************************************************************************
  195. *
  196. * Handler for device events.
  197. * Certain device events require that sockets be unconnected.
  198. *
  199. **************************************************************************/
  200. static void pppoe_flush_dev(struct net_device *dev)
  201. {
  202. int hash;
  203. BUG_ON(dev == NULL);
  204. write_lock_bh(&pppoe_hash_lock);
  205. for (hash = 0; hash < PPPOE_HASH_SIZE; hash++) {
  206. struct pppox_sock *po = item_hash_table[hash];
  207. while (po != NULL) {
  208. struct sock *sk = sk_pppox(po);
  209. if (po->pppoe_dev != dev) {
  210. po = po->next;
  211. continue;
  212. }
  213. po->pppoe_dev = NULL;
  214. dev_put(dev);
  215. /* We always grab the socket lock, followed by the
  216. * pppoe_hash_lock, in that order. Since we should
  217. * hold the sock lock while doing any unbinding,
  218. * we need to release the lock we're holding.
  219. * Hold a reference to the sock so it doesn't disappear
  220. * as we're jumping between locks.
  221. */
  222. sock_hold(sk);
  223. write_unlock_bh(&pppoe_hash_lock);
  224. lock_sock(sk);
  225. if (sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)) {
  226. pppox_unbind_sock(sk);
  227. sk->sk_state = PPPOX_ZOMBIE;
  228. sk->sk_state_change(sk);
  229. }
  230. release_sock(sk);
  231. sock_put(sk);
  232. /* Restart scan at the beginning of this hash chain.
  233. * While the lock was dropped the chain contents may
  234. * have changed.
  235. */
  236. write_lock_bh(&pppoe_hash_lock);
  237. po = item_hash_table[hash];
  238. }
  239. }
  240. write_unlock_bh(&pppoe_hash_lock);
  241. }
  242. static int pppoe_device_event(struct notifier_block *this,
  243. unsigned long event, void *ptr)
  244. {
  245. struct net_device *dev = (struct net_device *) ptr;
  246. /* Only look at sockets that are using this specific device. */
  247. switch (event) {
  248. case NETDEV_CHANGEMTU:
  249. /* A change in mtu is a bad thing, requiring
  250. * LCP re-negotiation.
  251. */
  252. case NETDEV_GOING_DOWN:
  253. case NETDEV_DOWN:
  254. /* Find every socket on this device and kill it. */
  255. pppoe_flush_dev(dev);
  256. break;
  257. default:
  258. break;
  259. };
  260. return NOTIFY_DONE;
  261. }
  262. static struct notifier_block pppoe_notifier = {
  263. .notifier_call = pppoe_device_event,
  264. };
  265. /************************************************************************
  266. *
  267. * Do the real work of receiving a PPPoE Session frame.
  268. *
  269. ***********************************************************************/
  270. static int pppoe_rcv_core(struct sock *sk, struct sk_buff *skb)
  271. {
  272. struct pppox_sock *po = pppox_sk(sk);
  273. struct pppox_sock *relay_po;
  274. if (sk->sk_state & PPPOX_BOUND) {
  275. struct pppoe_hdr *ph = pppoe_hdr(skb);
  276. int len = ntohs(ph->length);
  277. skb_pull_rcsum(skb, sizeof(struct pppoe_hdr));
  278. if (pskb_trim_rcsum(skb, len))
  279. goto abort_kfree;
  280. ppp_input(&po->chan, skb);
  281. } else if (sk->sk_state & PPPOX_RELAY) {
  282. relay_po = get_item_by_addr(&po->pppoe_relay);
  283. if (relay_po == NULL)
  284. goto abort_kfree;
  285. if ((sk_pppox(relay_po)->sk_state & PPPOX_CONNECTED) == 0)
  286. goto abort_put;
  287. skb_pull(skb, sizeof(struct pppoe_hdr));
  288. if (!__pppoe_xmit(sk_pppox(relay_po), skb))
  289. goto abort_put;
  290. } else {
  291. if (sock_queue_rcv_skb(sk, skb))
  292. goto abort_kfree;
  293. }
  294. return NET_RX_SUCCESS;
  295. abort_put:
  296. sock_put(sk_pppox(relay_po));
  297. abort_kfree:
  298. kfree_skb(skb);
  299. return NET_RX_DROP;
  300. }
  301. /************************************************************************
  302. *
  303. * Receive wrapper called in BH context.
  304. *
  305. ***********************************************************************/
  306. static int pppoe_rcv(struct sk_buff *skb,
  307. struct net_device *dev,
  308. struct packet_type *pt,
  309. struct net_device *orig_dev)
  310. {
  311. struct pppoe_hdr *ph;
  312. struct pppox_sock *po;
  313. if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
  314. goto drop;
  315. if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
  316. goto out;
  317. ph = pppoe_hdr(skb);
  318. po = get_item((unsigned long) ph->sid, eth_hdr(skb)->h_source, dev->ifindex);
  319. if (po != NULL)
  320. return sk_receive_skb(sk_pppox(po), skb, 0);
  321. drop:
  322. kfree_skb(skb);
  323. out:
  324. return NET_RX_DROP;
  325. }
  326. /************************************************************************
  327. *
  328. * Receive a PPPoE Discovery frame.
  329. * This is solely for detection of PADT frames
  330. *
  331. ***********************************************************************/
  332. static int pppoe_disc_rcv(struct sk_buff *skb,
  333. struct net_device *dev,
  334. struct packet_type *pt,
  335. struct net_device *orig_dev)
  336. {
  337. struct pppoe_hdr *ph;
  338. struct pppox_sock *po;
  339. if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
  340. goto abort;
  341. if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
  342. goto out;
  343. ph = pppoe_hdr(skb);
  344. if (ph->code != PADT_CODE)
  345. goto abort;
  346. po = get_item((unsigned long) ph->sid, eth_hdr(skb)->h_source, dev->ifindex);
  347. if (po) {
  348. struct sock *sk = sk_pppox(po);
  349. bh_lock_sock(sk);
  350. /* If the user has locked the socket, just ignore
  351. * the packet. With the way two rcv protocols hook into
  352. * one socket family type, we cannot (easily) distinguish
  353. * what kind of SKB it is during backlog rcv.
  354. */
  355. if (sock_owned_by_user(sk) == 0) {
  356. /* We're no longer connect at the PPPOE layer,
  357. * and must wait for ppp channel to disconnect us.
  358. */
  359. sk->sk_state = PPPOX_ZOMBIE;
  360. }
  361. bh_unlock_sock(sk);
  362. sock_put(sk);
  363. }
  364. abort:
  365. kfree_skb(skb);
  366. out:
  367. return NET_RX_SUCCESS; /* Lies... :-) */
  368. }
  369. static struct packet_type pppoes_ptype = {
  370. .type = __constant_htons(ETH_P_PPP_SES),
  371. .func = pppoe_rcv,
  372. };
  373. static struct packet_type pppoed_ptype = {
  374. .type = __constant_htons(ETH_P_PPP_DISC),
  375. .func = pppoe_disc_rcv,
  376. };
  377. static struct proto pppoe_sk_proto = {
  378. .name = "PPPOE",
  379. .owner = THIS_MODULE,
  380. .obj_size = sizeof(struct pppox_sock),
  381. };
  382. /***********************************************************************
  383. *
  384. * Initialize a new struct sock.
  385. *
  386. **********************************************************************/
  387. static int pppoe_create(struct socket *sock)
  388. {
  389. int error = -ENOMEM;
  390. struct sock *sk;
  391. sk = sk_alloc(PF_PPPOX, GFP_KERNEL, &pppoe_sk_proto, 1);
  392. if (!sk)
  393. goto out;
  394. sock_init_data(sock, sk);
  395. sock->state = SS_UNCONNECTED;
  396. sock->ops = &pppoe_ops;
  397. sk->sk_backlog_rcv = pppoe_rcv_core;
  398. sk->sk_state = PPPOX_NONE;
  399. sk->sk_type = SOCK_STREAM;
  400. sk->sk_family = PF_PPPOX;
  401. sk->sk_protocol = PX_PROTO_OE;
  402. error = 0;
  403. out: return error;
  404. }
  405. static int pppoe_release(struct socket *sock)
  406. {
  407. struct sock *sk = sock->sk;
  408. struct pppox_sock *po;
  409. if (!sk)
  410. return 0;
  411. lock_sock(sk);
  412. if (sock_flag(sk, SOCK_DEAD)){
  413. release_sock(sk);
  414. return -EBADF;
  415. }
  416. pppox_unbind_sock(sk);
  417. /* Signal the death of the socket. */
  418. sk->sk_state = PPPOX_DEAD;
  419. /* Write lock on hash lock protects the entire "po" struct from
  420. * concurrent updates via pppoe_flush_dev. The "po" struct should
  421. * be considered part of the hash table contents, thus protected
  422. * by the hash table lock */
  423. write_lock_bh(&pppoe_hash_lock);
  424. po = pppox_sk(sk);
  425. if (po->pppoe_pa.sid) {
  426. __delete_item(po->pppoe_pa.sid,
  427. po->pppoe_pa.remote, po->pppoe_ifindex);
  428. }
  429. if (po->pppoe_dev) {
  430. dev_put(po->pppoe_dev);
  431. po->pppoe_dev = NULL;
  432. }
  433. write_unlock_bh(&pppoe_hash_lock);
  434. sock_orphan(sk);
  435. sock->sk = NULL;
  436. skb_queue_purge(&sk->sk_receive_queue);
  437. release_sock(sk);
  438. sock_put(sk);
  439. return 0;
  440. }
  441. static int pppoe_connect(struct socket *sock, struct sockaddr *uservaddr,
  442. int sockaddr_len, int flags)
  443. {
  444. struct sock *sk = sock->sk;
  445. struct net_device *dev;
  446. struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr;
  447. struct pppox_sock *po = pppox_sk(sk);
  448. int error;
  449. lock_sock(sk);
  450. error = -EINVAL;
  451. if (sp->sa_protocol != PX_PROTO_OE)
  452. goto end;
  453. /* Check for already bound sockets */
  454. error = -EBUSY;
  455. if ((sk->sk_state & PPPOX_CONNECTED) && sp->sa_addr.pppoe.sid)
  456. goto end;
  457. /* Check for already disconnected sockets, on attempts to disconnect */
  458. error = -EALREADY;
  459. if ((sk->sk_state & PPPOX_DEAD) && !sp->sa_addr.pppoe.sid )
  460. goto end;
  461. error = 0;
  462. if (po->pppoe_pa.sid) {
  463. pppox_unbind_sock(sk);
  464. /* Delete the old binding */
  465. delete_item(po->pppoe_pa.sid,po->pppoe_pa.remote,po->pppoe_ifindex);
  466. if(po->pppoe_dev)
  467. dev_put(po->pppoe_dev);
  468. memset(sk_pppox(po) + 1, 0,
  469. sizeof(struct pppox_sock) - sizeof(struct sock));
  470. sk->sk_state = PPPOX_NONE;
  471. }
  472. /* Don't re-bind if sid==0 */
  473. if (sp->sa_addr.pppoe.sid != 0) {
  474. dev = dev_get_by_name(sp->sa_addr.pppoe.dev);
  475. error = -ENODEV;
  476. if (!dev)
  477. goto end;
  478. po->pppoe_dev = dev;
  479. po->pppoe_ifindex = dev->ifindex;
  480. write_lock_bh(&pppoe_hash_lock);
  481. if (!(dev->flags & IFF_UP)){
  482. write_unlock_bh(&pppoe_hash_lock);
  483. goto err_put;
  484. }
  485. memcpy(&po->pppoe_pa,
  486. &sp->sa_addr.pppoe,
  487. sizeof(struct pppoe_addr));
  488. error = __set_item(po);
  489. write_unlock_bh(&pppoe_hash_lock);
  490. if (error < 0)
  491. goto err_put;
  492. po->chan.hdrlen = (sizeof(struct pppoe_hdr) +
  493. dev->hard_header_len);
  494. po->chan.mtu = dev->mtu - sizeof(struct pppoe_hdr);
  495. po->chan.private = sk;
  496. po->chan.ops = &pppoe_chan_ops;
  497. error = ppp_register_channel(&po->chan);
  498. if (error)
  499. goto err_put;
  500. sk->sk_state = PPPOX_CONNECTED;
  501. }
  502. po->num = sp->sa_addr.pppoe.sid;
  503. end:
  504. release_sock(sk);
  505. return error;
  506. err_put:
  507. if (po->pppoe_dev) {
  508. dev_put(po->pppoe_dev);
  509. po->pppoe_dev = NULL;
  510. }
  511. goto end;
  512. }
  513. static int pppoe_getname(struct socket *sock, struct sockaddr *uaddr,
  514. int *usockaddr_len, int peer)
  515. {
  516. int len = sizeof(struct sockaddr_pppox);
  517. struct sockaddr_pppox sp;
  518. sp.sa_family = AF_PPPOX;
  519. sp.sa_protocol = PX_PROTO_OE;
  520. memcpy(&sp.sa_addr.pppoe, &pppox_sk(sock->sk)->pppoe_pa,
  521. sizeof(struct pppoe_addr));
  522. memcpy(uaddr, &sp, len);
  523. *usockaddr_len = len;
  524. return 0;
  525. }
  526. static int pppoe_ioctl(struct socket *sock, unsigned int cmd,
  527. unsigned long arg)
  528. {
  529. struct sock *sk = sock->sk;
  530. struct pppox_sock *po = pppox_sk(sk);
  531. int val;
  532. int err;
  533. switch (cmd) {
  534. case PPPIOCGMRU:
  535. err = -ENXIO;
  536. if (!(sk->sk_state & PPPOX_CONNECTED))
  537. break;
  538. err = -EFAULT;
  539. if (put_user(po->pppoe_dev->mtu -
  540. sizeof(struct pppoe_hdr) -
  541. PPP_HDRLEN,
  542. (int __user *) arg))
  543. break;
  544. err = 0;
  545. break;
  546. case PPPIOCSMRU:
  547. err = -ENXIO;
  548. if (!(sk->sk_state & PPPOX_CONNECTED))
  549. break;
  550. err = -EFAULT;
  551. if (get_user(val,(int __user *) arg))
  552. break;
  553. if (val < (po->pppoe_dev->mtu
  554. - sizeof(struct pppoe_hdr)
  555. - PPP_HDRLEN))
  556. err = 0;
  557. else
  558. err = -EINVAL;
  559. break;
  560. case PPPIOCSFLAGS:
  561. err = -EFAULT;
  562. if (get_user(val, (int __user *) arg))
  563. break;
  564. err = 0;
  565. break;
  566. case PPPOEIOCSFWD:
  567. {
  568. struct pppox_sock *relay_po;
  569. err = -EBUSY;
  570. if (sk->sk_state & (PPPOX_BOUND | PPPOX_ZOMBIE | PPPOX_DEAD))
  571. break;
  572. err = -ENOTCONN;
  573. if (!(sk->sk_state & PPPOX_CONNECTED))
  574. break;
  575. /* PPPoE address from the user specifies an outbound
  576. PPPoE address which frames are forwarded to */
  577. err = -EFAULT;
  578. if (copy_from_user(&po->pppoe_relay,
  579. (void __user *)arg,
  580. sizeof(struct sockaddr_pppox)))
  581. break;
  582. err = -EINVAL;
  583. if (po->pppoe_relay.sa_family != AF_PPPOX ||
  584. po->pppoe_relay.sa_protocol!= PX_PROTO_OE)
  585. break;
  586. /* Check that the socket referenced by the address
  587. actually exists. */
  588. relay_po = get_item_by_addr(&po->pppoe_relay);
  589. if (!relay_po)
  590. break;
  591. sock_put(sk_pppox(relay_po));
  592. sk->sk_state |= PPPOX_RELAY;
  593. err = 0;
  594. break;
  595. }
  596. case PPPOEIOCDFWD:
  597. err = -EALREADY;
  598. if (!(sk->sk_state & PPPOX_RELAY))
  599. break;
  600. sk->sk_state &= ~PPPOX_RELAY;
  601. err = 0;
  602. break;
  603. default:
  604. err = -ENOTTY;
  605. }
  606. return err;
  607. }
  608. static int pppoe_sendmsg(struct kiocb *iocb, struct socket *sock,
  609. struct msghdr *m, size_t total_len)
  610. {
  611. struct sk_buff *skb;
  612. struct sock *sk = sock->sk;
  613. struct pppox_sock *po = pppox_sk(sk);
  614. int error;
  615. struct pppoe_hdr hdr;
  616. struct pppoe_hdr *ph;
  617. struct net_device *dev;
  618. char *start;
  619. if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) {
  620. error = -ENOTCONN;
  621. goto end;
  622. }
  623. hdr.ver = 1;
  624. hdr.type = 1;
  625. hdr.code = 0;
  626. hdr.sid = po->num;
  627. lock_sock(sk);
  628. dev = po->pppoe_dev;
  629. error = -EMSGSIZE;
  630. if (total_len > (dev->mtu + dev->hard_header_len))
  631. goto end;
  632. skb = sock_wmalloc(sk, total_len + dev->hard_header_len + 32,
  633. 0, GFP_KERNEL);
  634. if (!skb) {
  635. error = -ENOMEM;
  636. goto end;
  637. }
  638. /* Reserve space for headers. */
  639. skb_reserve(skb, dev->hard_header_len);
  640. skb_reset_network_header(skb);
  641. skb->dev = dev;
  642. skb->priority = sk->sk_priority;
  643. skb->protocol = __constant_htons(ETH_P_PPP_SES);
  644. ph = (struct pppoe_hdr *) skb_put(skb, total_len + sizeof(struct pppoe_hdr));
  645. start = (char *) &ph->tag[0];
  646. error = memcpy_fromiovec(start, m->msg_iov, total_len);
  647. if (error < 0) {
  648. kfree_skb(skb);
  649. goto end;
  650. }
  651. error = total_len;
  652. dev->hard_header(skb, dev, ETH_P_PPP_SES,
  653. po->pppoe_pa.remote, NULL, total_len);
  654. memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
  655. ph->length = htons(total_len);
  656. dev_queue_xmit(skb);
  657. end:
  658. release_sock(sk);
  659. return error;
  660. }
  661. /************************************************************************
  662. *
  663. * xmit function for internal use.
  664. *
  665. ***********************************************************************/
  666. static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb)
  667. {
  668. struct pppox_sock *po = pppox_sk(sk);
  669. struct net_device *dev = po->pppoe_dev;
  670. struct pppoe_hdr hdr;
  671. struct pppoe_hdr *ph;
  672. int headroom = skb_headroom(skb);
  673. int data_len = skb->len;
  674. struct sk_buff *skb2;
  675. if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
  676. goto abort;
  677. hdr.ver = 1;
  678. hdr.type = 1;
  679. hdr.code = 0;
  680. hdr.sid = po->num;
  681. hdr.length = htons(skb->len);
  682. if (!dev)
  683. goto abort;
  684. /* Copy the skb if there is no space for the header. */
  685. if (headroom < (sizeof(struct pppoe_hdr) + dev->hard_header_len)) {
  686. skb2 = dev_alloc_skb(32+skb->len +
  687. sizeof(struct pppoe_hdr) +
  688. dev->hard_header_len);
  689. if (skb2 == NULL)
  690. goto abort;
  691. skb_reserve(skb2, dev->hard_header_len + sizeof(struct pppoe_hdr));
  692. skb_copy_from_linear_data(skb, skb_put(skb2, skb->len),
  693. skb->len);
  694. } else {
  695. /* Make a clone so as to not disturb the original skb,
  696. * give dev_queue_xmit something it can free.
  697. */
  698. skb2 = skb_clone(skb, GFP_ATOMIC);
  699. if (skb2 == NULL)
  700. goto abort;
  701. }
  702. ph = (struct pppoe_hdr *) skb_push(skb2, sizeof(struct pppoe_hdr));
  703. memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
  704. skb2->protocol = __constant_htons(ETH_P_PPP_SES);
  705. skb_reset_network_header(skb2);
  706. skb2->dev = dev;
  707. dev->hard_header(skb2, dev, ETH_P_PPP_SES,
  708. po->pppoe_pa.remote, NULL, data_len);
  709. /* We're transmitting skb2, and assuming that dev_queue_xmit
  710. * will free it. The generic ppp layer however, is expecting
  711. * that we give back 'skb' (not 'skb2') in case of failure,
  712. * but free it in case of success.
  713. */
  714. if (dev_queue_xmit(skb2) < 0)
  715. goto abort;
  716. kfree_skb(skb);
  717. return 1;
  718. abort:
  719. return 0;
  720. }
  721. /************************************************************************
  722. *
  723. * xmit function called by generic PPP driver
  724. * sends PPP frame over PPPoE socket
  725. *
  726. ***********************************************************************/
  727. static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb)
  728. {
  729. struct sock *sk = (struct sock *) chan->private;
  730. return __pppoe_xmit(sk, skb);
  731. }
  732. static struct ppp_channel_ops pppoe_chan_ops = {
  733. .start_xmit = pppoe_xmit,
  734. };
  735. static int pppoe_recvmsg(struct kiocb *iocb, struct socket *sock,
  736. struct msghdr *m, size_t total_len, int flags)
  737. {
  738. struct sock *sk = sock->sk;
  739. struct sk_buff *skb;
  740. int error = 0;
  741. if (sk->sk_state & PPPOX_BOUND) {
  742. error = -EIO;
  743. goto end;
  744. }
  745. skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
  746. flags & MSG_DONTWAIT, &error);
  747. if (error < 0)
  748. goto end;
  749. m->msg_namelen = 0;
  750. if (skb) {
  751. struct pppoe_hdr *ph = pppoe_hdr(skb);
  752. const int len = ntohs(ph->length);
  753. error = memcpy_toiovec(m->msg_iov, (unsigned char *) &ph->tag[0], len);
  754. if (error == 0)
  755. error = len;
  756. }
  757. kfree_skb(skb);
  758. end:
  759. return error;
  760. }
  761. #ifdef CONFIG_PROC_FS
  762. static int pppoe_seq_show(struct seq_file *seq, void *v)
  763. {
  764. struct pppox_sock *po;
  765. char *dev_name;
  766. if (v == SEQ_START_TOKEN) {
  767. seq_puts(seq, "Id Address Device\n");
  768. goto out;
  769. }
  770. po = v;
  771. dev_name = po->pppoe_pa.dev;
  772. seq_printf(seq, "%08X %02X:%02X:%02X:%02X:%02X:%02X %8s\n",
  773. po->pppoe_pa.sid,
  774. po->pppoe_pa.remote[0], po->pppoe_pa.remote[1],
  775. po->pppoe_pa.remote[2], po->pppoe_pa.remote[3],
  776. po->pppoe_pa.remote[4], po->pppoe_pa.remote[5], dev_name);
  777. out:
  778. return 0;
  779. }
  780. static __inline__ struct pppox_sock *pppoe_get_idx(loff_t pos)
  781. {
  782. struct pppox_sock *po;
  783. int i = 0;
  784. for (; i < PPPOE_HASH_SIZE; i++) {
  785. po = item_hash_table[i];
  786. while (po) {
  787. if (!pos--)
  788. goto out;
  789. po = po->next;
  790. }
  791. }
  792. out:
  793. return po;
  794. }
  795. static void *pppoe_seq_start(struct seq_file *seq, loff_t *pos)
  796. {
  797. loff_t l = *pos;
  798. read_lock_bh(&pppoe_hash_lock);
  799. return l ? pppoe_get_idx(--l) : SEQ_START_TOKEN;
  800. }
  801. static void *pppoe_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  802. {
  803. struct pppox_sock *po;
  804. ++*pos;
  805. if (v == SEQ_START_TOKEN) {
  806. po = pppoe_get_idx(0);
  807. goto out;
  808. }
  809. po = v;
  810. if (po->next)
  811. po = po->next;
  812. else {
  813. int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
  814. while (++hash < PPPOE_HASH_SIZE) {
  815. po = item_hash_table[hash];
  816. if (po)
  817. break;
  818. }
  819. }
  820. out:
  821. return po;
  822. }
  823. static void pppoe_seq_stop(struct seq_file *seq, void *v)
  824. {
  825. read_unlock_bh(&pppoe_hash_lock);
  826. }
  827. static struct seq_operations pppoe_seq_ops = {
  828. .start = pppoe_seq_start,
  829. .next = pppoe_seq_next,
  830. .stop = pppoe_seq_stop,
  831. .show = pppoe_seq_show,
  832. };
  833. static int pppoe_seq_open(struct inode *inode, struct file *file)
  834. {
  835. return seq_open(file, &pppoe_seq_ops);
  836. }
  837. static const struct file_operations pppoe_seq_fops = {
  838. .owner = THIS_MODULE,
  839. .open = pppoe_seq_open,
  840. .read = seq_read,
  841. .llseek = seq_lseek,
  842. .release = seq_release,
  843. };
  844. static int __init pppoe_proc_init(void)
  845. {
  846. struct proc_dir_entry *p;
  847. p = create_proc_entry("net/pppoe", S_IRUGO, NULL);
  848. if (!p)
  849. return -ENOMEM;
  850. p->proc_fops = &pppoe_seq_fops;
  851. return 0;
  852. }
  853. #else /* CONFIG_PROC_FS */
  854. static inline int pppoe_proc_init(void) { return 0; }
  855. #endif /* CONFIG_PROC_FS */
  856. static const struct proto_ops pppoe_ops = {
  857. .family = AF_PPPOX,
  858. .owner = THIS_MODULE,
  859. .release = pppoe_release,
  860. .bind = sock_no_bind,
  861. .connect = pppoe_connect,
  862. .socketpair = sock_no_socketpair,
  863. .accept = sock_no_accept,
  864. .getname = pppoe_getname,
  865. .poll = datagram_poll,
  866. .listen = sock_no_listen,
  867. .shutdown = sock_no_shutdown,
  868. .setsockopt = sock_no_setsockopt,
  869. .getsockopt = sock_no_getsockopt,
  870. .sendmsg = pppoe_sendmsg,
  871. .recvmsg = pppoe_recvmsg,
  872. .mmap = sock_no_mmap,
  873. .ioctl = pppox_ioctl,
  874. };
  875. static struct pppox_proto pppoe_proto = {
  876. .create = pppoe_create,
  877. .ioctl = pppoe_ioctl,
  878. .owner = THIS_MODULE,
  879. };
  880. static int __init pppoe_init(void)
  881. {
  882. int err = proto_register(&pppoe_sk_proto, 0);
  883. if (err)
  884. goto out;
  885. err = register_pppox_proto(PX_PROTO_OE, &pppoe_proto);
  886. if (err)
  887. goto out_unregister_pppoe_proto;
  888. err = pppoe_proc_init();
  889. if (err)
  890. goto out_unregister_pppox_proto;
  891. dev_add_pack(&pppoes_ptype);
  892. dev_add_pack(&pppoed_ptype);
  893. register_netdevice_notifier(&pppoe_notifier);
  894. out:
  895. return err;
  896. out_unregister_pppox_proto:
  897. unregister_pppox_proto(PX_PROTO_OE);
  898. out_unregister_pppoe_proto:
  899. proto_unregister(&pppoe_sk_proto);
  900. goto out;
  901. }
  902. static void __exit pppoe_exit(void)
  903. {
  904. unregister_pppox_proto(PX_PROTO_OE);
  905. dev_remove_pack(&pppoes_ptype);
  906. dev_remove_pack(&pppoed_ptype);
  907. unregister_netdevice_notifier(&pppoe_notifier);
  908. remove_proc_entry("net/pppoe", NULL);
  909. proto_unregister(&pppoe_sk_proto);
  910. }
  911. module_init(pppoe_init);
  912. module_exit(pppoe_exit);
  913. MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>");
  914. MODULE_DESCRIPTION("PPP over Ethernet driver");
  915. MODULE_LICENSE("GPL");
  916. MODULE_ALIAS_NETPROTO(PF_PPPOX);