pppoe.c 26 KB

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