pppoe.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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. * 220102 : Fix module use count on failure in pppoe_create, pppox_sk -acme
  11. * 030700 : Fixed connect logic to allow for disconnect.
  12. * 270700 : Fixed potential SMP problems; we must protect against
  13. * simultaneous invocation of ppp_input
  14. * and ppp_unregister_channel.
  15. * 040800 : Respect reference count mechanisms on net-devices.
  16. * 200800 : fix kfree(skb) in pppoe_rcv (acme)
  17. * Module reference count is decremented in the right spot now,
  18. * guards against sock_put not actually freeing the sk
  19. * in pppoe_release.
  20. * 051000 : Initialization cleanup.
  21. * 111100 : Fix recvmsg.
  22. * 050101 : Fix PADT procesing.
  23. * 140501 : Use pppoe_rcv_core to handle all backlog. (Alexey)
  24. * 170701 : Do not lock_sock with rwlock held. (DaveM)
  25. * Ignore discovery frames if user has socket
  26. * locked. (DaveM)
  27. * Ignore return value of dev_queue_xmit in __pppoe_xmit
  28. * or else we may kfree an SKB twice. (DaveM)
  29. * 190701 : When doing copies of skb's in __pppoe_xmit, always delete
  30. * the original skb that was passed in on success, never on
  31. * failure. Delete the copy of the skb on failure to avoid
  32. * a memory leak.
  33. * 081001 : Misc. cleanup (licence string, non-blocking, prevent
  34. * reference of device on close).
  35. * 121301 : New ppp channels interface; cannot unregister a channel
  36. * from interrupts. Thus, we mark the socket as a ZOMBIE
  37. * and do the unregistration later.
  38. * 081002 : seq_file support for proc stuff -acme
  39. * 111602 : Merge all 2.4 fixes into 2.5/2.6 tree. Label 2.5/2.6
  40. * as version 0.7. Spacing cleanup.
  41. * Author: Michal Ostrowski <mostrows@speakeasy.net>
  42. * Contributors:
  43. * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  44. * David S. Miller (davem@redhat.com)
  45. *
  46. * License:
  47. * This program is free software; you can redistribute it and/or
  48. * modify it under the terms of the GNU General Public License
  49. * as published by the Free Software Foundation; either version
  50. * 2 of the License, or (at your option) any later version.
  51. *
  52. */
  53. #include <linux/string.h>
  54. #include <linux/module.h>
  55. #include <linux/kernel.h>
  56. #include <linux/slab.h>
  57. #include <linux/errno.h>
  58. #include <linux/netdevice.h>
  59. #include <linux/net.h>
  60. #include <linux/inetdevice.h>
  61. #include <linux/etherdevice.h>
  62. #include <linux/skbuff.h>
  63. #include <linux/init.h>
  64. #include <linux/if_ether.h>
  65. #include <linux/if_pppox.h>
  66. #include <linux/ppp_channel.h>
  67. #include <linux/ppp_defs.h>
  68. #include <linux/if_ppp.h>
  69. #include <linux/notifier.h>
  70. #include <linux/file.h>
  71. #include <linux/proc_fs.h>
  72. #include <linux/seq_file.h>
  73. #include <net/sock.h>
  74. #include <asm/uaccess.h>
  75. #define PPPOE_HASH_BITS 4
  76. #define PPPOE_HASH_SIZE (1<<PPPOE_HASH_BITS)
  77. static struct ppp_channel_ops pppoe_chan_ops;
  78. static int pppoe_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
  79. static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb);
  80. static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb);
  81. static struct proto_ops pppoe_ops;
  82. static DEFINE_RWLOCK(pppoe_hash_lock);
  83. static struct ppp_channel_ops pppoe_chan_ops;
  84. static inline int cmp_2_addr(struct pppoe_addr *a, struct pppoe_addr *b)
  85. {
  86. return (a->sid == b->sid &&
  87. (memcmp(a->remote, b->remote, ETH_ALEN) == 0));
  88. }
  89. static inline int cmp_addr(struct pppoe_addr *a, unsigned long sid, char *addr)
  90. {
  91. return (a->sid == sid &&
  92. (memcmp(a->remote,addr,ETH_ALEN) == 0));
  93. }
  94. static int hash_item(unsigned long sid, unsigned char *addr)
  95. {
  96. char hash = 0;
  97. int i, j;
  98. for (i = 0; i < ETH_ALEN ; ++i) {
  99. for (j = 0; j < 8/PPPOE_HASH_BITS ; ++j) {
  100. hash ^= addr[i] >> ( j * PPPOE_HASH_BITS );
  101. }
  102. }
  103. for (i = 0; i < (sizeof(unsigned long)*8) / PPPOE_HASH_BITS ; ++i)
  104. hash ^= sid >> (i*PPPOE_HASH_BITS);
  105. return hash & ( PPPOE_HASH_SIZE - 1 );
  106. }
  107. /* zeroed because its in .bss */
  108. static struct pppox_sock *item_hash_table[PPPOE_HASH_SIZE];
  109. /**********************************************************************
  110. *
  111. * Set/get/delete/rehash items (internal versions)
  112. *
  113. **********************************************************************/
  114. static struct pppox_sock *__get_item(unsigned long sid, unsigned char *addr)
  115. {
  116. int hash = hash_item(sid, addr);
  117. struct pppox_sock *ret;
  118. ret = item_hash_table[hash];
  119. while (ret && !cmp_addr(&ret->pppoe_pa, sid, addr))
  120. ret = ret->next;
  121. return ret;
  122. }
  123. static int __set_item(struct pppox_sock *po)
  124. {
  125. int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
  126. struct pppox_sock *ret;
  127. ret = item_hash_table[hash];
  128. while (ret) {
  129. if (cmp_2_addr(&ret->pppoe_pa, &po->pppoe_pa))
  130. return -EALREADY;
  131. ret = ret->next;
  132. }
  133. if (!ret) {
  134. po->next = item_hash_table[hash];
  135. item_hash_table[hash] = po;
  136. }
  137. return 0;
  138. }
  139. static struct pppox_sock *__delete_item(unsigned long sid, char *addr)
  140. {
  141. int hash = hash_item(sid, addr);
  142. struct pppox_sock *ret, **src;
  143. ret = item_hash_table[hash];
  144. src = &item_hash_table[hash];
  145. while (ret) {
  146. if (cmp_addr(&ret->pppoe_pa, sid, addr)) {
  147. *src = ret->next;
  148. break;
  149. }
  150. src = &ret->next;
  151. ret = ret->next;
  152. }
  153. return ret;
  154. }
  155. /**********************************************************************
  156. *
  157. * Set/get/delete/rehash items
  158. *
  159. **********************************************************************/
  160. static inline struct pppox_sock *get_item(unsigned long sid,
  161. unsigned char *addr)
  162. {
  163. struct pppox_sock *po;
  164. read_lock_bh(&pppoe_hash_lock);
  165. po = __get_item(sid, addr);
  166. if (po)
  167. sock_hold(sk_pppox(po));
  168. read_unlock_bh(&pppoe_hash_lock);
  169. return po;
  170. }
  171. static inline struct pppox_sock *get_item_by_addr(struct sockaddr_pppox *sp)
  172. {
  173. return get_item(sp->sa_addr.pppoe.sid, sp->sa_addr.pppoe.remote);
  174. }
  175. static inline int set_item(struct pppox_sock *po)
  176. {
  177. int i;
  178. if (!po)
  179. return -EINVAL;
  180. write_lock_bh(&pppoe_hash_lock);
  181. i = __set_item(po);
  182. write_unlock_bh(&pppoe_hash_lock);
  183. return i;
  184. }
  185. static inline struct pppox_sock *delete_item(unsigned long sid, char *addr)
  186. {
  187. struct pppox_sock *ret;
  188. write_lock_bh(&pppoe_hash_lock);
  189. ret = __delete_item(sid, addr);
  190. write_unlock_bh(&pppoe_hash_lock);
  191. return ret;
  192. }
  193. /***************************************************************************
  194. *
  195. * Handler for device events.
  196. * Certain device events require that sockets be unconnected.
  197. *
  198. **************************************************************************/
  199. static void pppoe_flush_dev(struct net_device *dev)
  200. {
  201. int hash;
  202. BUG_ON(dev == NULL);
  203. read_lock_bh(&pppoe_hash_lock);
  204. for (hash = 0; hash < PPPOE_HASH_SIZE; hash++) {
  205. struct pppox_sock *po = item_hash_table[hash];
  206. while (po != NULL) {
  207. if (po->pppoe_dev == dev) {
  208. struct sock *sk = sk_pppox(po);
  209. sock_hold(sk);
  210. po->pppoe_dev = NULL;
  211. /* We hold a reference to SK, now drop the
  212. * hash table lock so that we may attempt
  213. * to lock the socket (which can sleep).
  214. */
  215. read_unlock_bh(&pppoe_hash_lock);
  216. lock_sock(sk);
  217. if (sk->sk_state &
  218. (PPPOX_CONNECTED | PPPOX_BOUND)) {
  219. pppox_unbind_sock(sk);
  220. dev_put(dev);
  221. sk->sk_state = PPPOX_ZOMBIE;
  222. sk->sk_state_change(sk);
  223. }
  224. release_sock(sk);
  225. sock_put(sk);
  226. read_lock_bh(&pppoe_hash_lock);
  227. /* Now restart from the beginning of this
  228. * hash chain. We always NULL out pppoe_dev
  229. * so we are guaranteed to make forward
  230. * progress.
  231. */
  232. po = item_hash_table[hash];
  233. continue;
  234. }
  235. po = po->next;
  236. }
  237. }
  238. read_unlock_bh(&pppoe_hash_lock);
  239. }
  240. static int pppoe_device_event(struct notifier_block *this,
  241. unsigned long event, void *ptr)
  242. {
  243. struct net_device *dev = (struct net_device *) ptr;
  244. /* Only look at sockets that are using this specific device. */
  245. switch (event) {
  246. case NETDEV_CHANGEMTU:
  247. /* A change in mtu is a bad thing, requiring
  248. * LCP re-negotiation.
  249. */
  250. case NETDEV_GOING_DOWN:
  251. case NETDEV_DOWN:
  252. /* Find every socket on this device and kill it. */
  253. pppoe_flush_dev(dev);
  254. break;
  255. default:
  256. break;
  257. };
  258. return NOTIFY_DONE;
  259. }
  260. static struct notifier_block pppoe_notifier = {
  261. .notifier_call = pppoe_device_event,
  262. };
  263. /************************************************************************
  264. *
  265. * Do the real work of receiving a PPPoE Session frame.
  266. *
  267. ***********************************************************************/
  268. static int pppoe_rcv_core(struct sock *sk, struct sk_buff *skb)
  269. {
  270. struct pppox_sock *po = pppox_sk(sk);
  271. struct pppox_sock *relay_po = NULL;
  272. if (sk->sk_state & PPPOX_BOUND) {
  273. struct pppoe_hdr *ph = (struct pppoe_hdr *) skb->nh.raw;
  274. int len = ntohs(ph->length);
  275. skb_pull(skb, sizeof(struct pppoe_hdr));
  276. skb_postpull_rcsum(skb, ph, sizeof(*ph));
  277. if (pskb_trim_rcsum(skb, len))
  278. goto abort_kfree;
  279. ppp_input(&po->chan, skb);
  280. } else if (sk->sk_state & PPPOX_RELAY) {
  281. relay_po = get_item_by_addr(&po->pppoe_relay);
  282. if (relay_po == NULL)
  283. goto abort_kfree;
  284. if ((sk_pppox(relay_po)->sk_state & PPPOX_CONNECTED) == 0)
  285. goto abort_put;
  286. skb_pull(skb, sizeof(struct pppoe_hdr));
  287. if (!__pppoe_xmit(sk_pppox(relay_po), skb))
  288. goto abort_put;
  289. } else {
  290. if (sock_queue_rcv_skb(sk, skb))
  291. goto abort_kfree;
  292. }
  293. return NET_RX_SUCCESS;
  294. abort_put:
  295. sock_put(sk_pppox(relay_po));
  296. abort_kfree:
  297. kfree_skb(skb);
  298. return NET_RX_DROP;
  299. }
  300. /************************************************************************
  301. *
  302. * Receive wrapper called in BH context.
  303. *
  304. ***********************************************************************/
  305. static int pppoe_rcv(struct sk_buff *skb,
  306. struct net_device *dev,
  307. struct packet_type *pt,
  308. struct net_device *orig_dev)
  309. {
  310. struct pppoe_hdr *ph;
  311. struct pppox_sock *po;
  312. struct sock *sk;
  313. int ret;
  314. if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
  315. goto drop;
  316. if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
  317. goto out;
  318. ph = (struct pppoe_hdr *) skb->nh.raw;
  319. po = get_item((unsigned long) ph->sid, eth_hdr(skb)->h_source);
  320. if (!po)
  321. goto drop;
  322. sk = sk_pppox(po);
  323. bh_lock_sock(sk);
  324. /* Socket state is unknown, must put skb into backlog. */
  325. if (sock_owned_by_user(sk) != 0) {
  326. sk_add_backlog(sk, skb);
  327. ret = NET_RX_SUCCESS;
  328. } else {
  329. ret = pppoe_rcv_core(sk, skb);
  330. }
  331. bh_unlock_sock(sk);
  332. sock_put(sk);
  333. return ret;
  334. drop:
  335. kfree_skb(skb);
  336. out:
  337. return NET_RX_DROP;
  338. }
  339. /************************************************************************
  340. *
  341. * Receive a PPPoE Discovery frame.
  342. * This is solely for detection of PADT frames
  343. *
  344. ***********************************************************************/
  345. static int pppoe_disc_rcv(struct sk_buff *skb,
  346. struct net_device *dev,
  347. struct packet_type *pt,
  348. struct net_device *orig_dev)
  349. {
  350. struct pppoe_hdr *ph;
  351. struct pppox_sock *po;
  352. if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
  353. goto abort;
  354. if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
  355. goto out;
  356. ph = (struct pppoe_hdr *) skb->nh.raw;
  357. if (ph->code != PADT_CODE)
  358. goto abort;
  359. po = get_item((unsigned long) ph->sid, eth_hdr(skb)->h_source);
  360. if (po) {
  361. struct sock *sk = sk_pppox(po);
  362. bh_lock_sock(sk);
  363. /* If the user has locked the socket, just ignore
  364. * the packet. With the way two rcv protocols hook into
  365. * one socket family type, we cannot (easily) distinguish
  366. * what kind of SKB it is during backlog rcv.
  367. */
  368. if (sock_owned_by_user(sk) == 0) {
  369. /* We're no longer connect at the PPPOE layer,
  370. * and must wait for ppp channel to disconnect us.
  371. */
  372. sk->sk_state = PPPOX_ZOMBIE;
  373. }
  374. bh_unlock_sock(sk);
  375. sock_put(sk);
  376. }
  377. abort:
  378. kfree_skb(skb);
  379. out:
  380. return NET_RX_SUCCESS; /* Lies... :-) */
  381. }
  382. static struct packet_type pppoes_ptype = {
  383. .type = __constant_htons(ETH_P_PPP_SES),
  384. .func = pppoe_rcv,
  385. };
  386. static struct packet_type pppoed_ptype = {
  387. .type = __constant_htons(ETH_P_PPP_DISC),
  388. .func = pppoe_disc_rcv,
  389. };
  390. static struct proto pppoe_sk_proto = {
  391. .name = "PPPOE",
  392. .owner = THIS_MODULE,
  393. .obj_size = sizeof(struct pppox_sock),
  394. };
  395. /***********************************************************************
  396. *
  397. * Initialize a new struct sock.
  398. *
  399. **********************************************************************/
  400. static int pppoe_create(struct socket *sock)
  401. {
  402. int error = -ENOMEM;
  403. struct sock *sk;
  404. sk = sk_alloc(PF_PPPOX, GFP_KERNEL, &pppoe_sk_proto, 1);
  405. if (!sk)
  406. goto out;
  407. sock_init_data(sock, sk);
  408. sock->state = SS_UNCONNECTED;
  409. sock->ops = &pppoe_ops;
  410. sk->sk_backlog_rcv = pppoe_rcv_core;
  411. sk->sk_state = PPPOX_NONE;
  412. sk->sk_type = SOCK_STREAM;
  413. sk->sk_family = PF_PPPOX;
  414. sk->sk_protocol = PX_PROTO_OE;
  415. error = 0;
  416. out: return error;
  417. }
  418. static int pppoe_release(struct socket *sock)
  419. {
  420. struct sock *sk = sock->sk;
  421. struct pppox_sock *po;
  422. int error = 0;
  423. if (!sk)
  424. return 0;
  425. if (sock_flag(sk, SOCK_DEAD))
  426. return -EBADF;
  427. pppox_unbind_sock(sk);
  428. /* Signal the death of the socket. */
  429. sk->sk_state = PPPOX_DEAD;
  430. po = pppox_sk(sk);
  431. if (po->pppoe_pa.sid) {
  432. delete_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
  433. }
  434. if (po->pppoe_dev)
  435. dev_put(po->pppoe_dev);
  436. po->pppoe_dev = NULL;
  437. sock_orphan(sk);
  438. sock->sk = NULL;
  439. skb_queue_purge(&sk->sk_receive_queue);
  440. sock_put(sk);
  441. return error;
  442. }
  443. static int pppoe_connect(struct socket *sock, struct sockaddr *uservaddr,
  444. int sockaddr_len, int flags)
  445. {
  446. struct sock *sk = sock->sk;
  447. struct net_device *dev = NULL;
  448. struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr;
  449. struct pppox_sock *po = pppox_sk(sk);
  450. int error;
  451. lock_sock(sk);
  452. error = -EINVAL;
  453. if (sp->sa_protocol != PX_PROTO_OE)
  454. goto end;
  455. /* Check for already bound sockets */
  456. error = -EBUSY;
  457. if ((sk->sk_state & PPPOX_CONNECTED) && sp->sa_addr.pppoe.sid)
  458. goto end;
  459. /* Check for already disconnected sockets, on attempts to disconnect */
  460. error = -EALREADY;
  461. if ((sk->sk_state & PPPOX_DEAD) && !sp->sa_addr.pppoe.sid )
  462. goto end;
  463. error = 0;
  464. if (po->pppoe_pa.sid) {
  465. pppox_unbind_sock(sk);
  466. /* Delete the old binding */
  467. delete_item(po->pppoe_pa.sid,po->pppoe_pa.remote);
  468. if(po->pppoe_dev)
  469. dev_put(po->pppoe_dev);
  470. memset(sk_pppox(po) + 1, 0,
  471. sizeof(struct pppox_sock) - sizeof(struct sock));
  472. sk->sk_state = PPPOX_NONE;
  473. }
  474. /* Don't re-bind if sid==0 */
  475. if (sp->sa_addr.pppoe.sid != 0) {
  476. dev = dev_get_by_name(sp->sa_addr.pppoe.dev);
  477. error = -ENODEV;
  478. if (!dev)
  479. goto end;
  480. po->pppoe_dev = dev;
  481. if (!(dev->flags & IFF_UP))
  482. goto err_put;
  483. memcpy(&po->pppoe_pa,
  484. &sp->sa_addr.pppoe,
  485. sizeof(struct pppoe_addr));
  486. error = set_item(po);
  487. if (error < 0)
  488. goto err_put;
  489. po->chan.hdrlen = (sizeof(struct pppoe_hdr) +
  490. dev->hard_header_len);
  491. po->chan.private = sk;
  492. po->chan.ops = &pppoe_chan_ops;
  493. error = ppp_register_channel(&po->chan);
  494. if (error)
  495. goto err_put;
  496. sk->sk_state = PPPOX_CONNECTED;
  497. }
  498. po->num = sp->sa_addr.pppoe.sid;
  499. end:
  500. release_sock(sk);
  501. return error;
  502. err_put:
  503. if (po->pppoe_dev) {
  504. dev_put(po->pppoe_dev);
  505. po->pppoe_dev = NULL;
  506. }
  507. goto end;
  508. }
  509. static int pppoe_getname(struct socket *sock, struct sockaddr *uaddr,
  510. int *usockaddr_len, int peer)
  511. {
  512. int len = sizeof(struct sockaddr_pppox);
  513. struct sockaddr_pppox sp;
  514. sp.sa_family = AF_PPPOX;
  515. sp.sa_protocol = PX_PROTO_OE;
  516. memcpy(&sp.sa_addr.pppoe, &pppox_sk(sock->sk)->pppoe_pa,
  517. sizeof(struct pppoe_addr));
  518. memcpy(uaddr, &sp, len);
  519. *usockaddr_len = len;
  520. return 0;
  521. }
  522. static int pppoe_ioctl(struct socket *sock, unsigned int cmd,
  523. unsigned long arg)
  524. {
  525. struct sock *sk = sock->sk;
  526. struct pppox_sock *po = pppox_sk(sk);
  527. int val = 0;
  528. int err = 0;
  529. switch (cmd) {
  530. case PPPIOCGMRU:
  531. err = -ENXIO;
  532. if (!(sk->sk_state & PPPOX_CONNECTED))
  533. break;
  534. err = -EFAULT;
  535. if (put_user(po->pppoe_dev->mtu -
  536. sizeof(struct pppoe_hdr) -
  537. PPP_HDRLEN,
  538. (int __user *) arg))
  539. break;
  540. err = 0;
  541. break;
  542. case PPPIOCSMRU:
  543. err = -ENXIO;
  544. if (!(sk->sk_state & PPPOX_CONNECTED))
  545. break;
  546. err = -EFAULT;
  547. if (get_user(val,(int __user *) arg))
  548. break;
  549. if (val < (po->pppoe_dev->mtu
  550. - sizeof(struct pppoe_hdr)
  551. - PPP_HDRLEN))
  552. err = 0;
  553. else
  554. err = -EINVAL;
  555. break;
  556. case PPPIOCSFLAGS:
  557. err = -EFAULT;
  558. if (get_user(val, (int __user *) arg))
  559. break;
  560. err = 0;
  561. break;
  562. case PPPOEIOCSFWD:
  563. {
  564. struct pppox_sock *relay_po;
  565. err = -EBUSY;
  566. if (sk->sk_state & (PPPOX_BOUND | PPPOX_ZOMBIE | PPPOX_DEAD))
  567. break;
  568. err = -ENOTCONN;
  569. if (!(sk->sk_state & PPPOX_CONNECTED))
  570. break;
  571. /* PPPoE address from the user specifies an outbound
  572. PPPoE address to which frames are forwarded to */
  573. err = -EFAULT;
  574. if (copy_from_user(&po->pppoe_relay,
  575. (void __user *)arg,
  576. sizeof(struct sockaddr_pppox)))
  577. break;
  578. err = -EINVAL;
  579. if (po->pppoe_relay.sa_family != AF_PPPOX ||
  580. po->pppoe_relay.sa_protocol!= PX_PROTO_OE)
  581. break;
  582. /* Check that the socket referenced by the address
  583. actually exists. */
  584. relay_po = get_item_by_addr(&po->pppoe_relay);
  585. if (!relay_po)
  586. break;
  587. sock_put(sk_pppox(relay_po));
  588. sk->sk_state |= PPPOX_RELAY;
  589. err = 0;
  590. break;
  591. }
  592. case PPPOEIOCDFWD:
  593. err = -EALREADY;
  594. if (!(sk->sk_state & PPPOX_RELAY))
  595. break;
  596. sk->sk_state &= ~PPPOX_RELAY;
  597. err = 0;
  598. break;
  599. default:;
  600. };
  601. return err;
  602. }
  603. static int pppoe_sendmsg(struct kiocb *iocb, struct socket *sock,
  604. struct msghdr *m, size_t total_len)
  605. {
  606. struct sk_buff *skb = NULL;
  607. struct sock *sk = sock->sk;
  608. struct pppox_sock *po = pppox_sk(sk);
  609. int error = 0;
  610. struct pppoe_hdr hdr;
  611. struct pppoe_hdr *ph;
  612. struct net_device *dev;
  613. char *start;
  614. if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) {
  615. error = -ENOTCONN;
  616. goto end;
  617. }
  618. hdr.ver = 1;
  619. hdr.type = 1;
  620. hdr.code = 0;
  621. hdr.sid = po->num;
  622. lock_sock(sk);
  623. dev = po->pppoe_dev;
  624. error = -EMSGSIZE;
  625. if (total_len > (dev->mtu + dev->hard_header_len))
  626. goto end;
  627. skb = sock_wmalloc(sk, total_len + dev->hard_header_len + 32,
  628. 0, GFP_KERNEL);
  629. if (!skb) {
  630. error = -ENOMEM;
  631. goto end;
  632. }
  633. /* Reserve space for headers. */
  634. skb_reserve(skb, dev->hard_header_len);
  635. skb->nh.raw = skb->data;
  636. skb->dev = dev;
  637. skb->priority = sk->sk_priority;
  638. skb->protocol = __constant_htons(ETH_P_PPP_SES);
  639. ph = (struct pppoe_hdr *) skb_put(skb, total_len + sizeof(struct pppoe_hdr));
  640. start = (char *) &ph->tag[0];
  641. error = memcpy_fromiovec(start, m->msg_iov, total_len);
  642. if (error < 0) {
  643. kfree_skb(skb);
  644. goto end;
  645. }
  646. error = total_len;
  647. dev->hard_header(skb, dev, ETH_P_PPP_SES,
  648. po->pppoe_pa.remote, NULL, total_len);
  649. memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
  650. ph->length = htons(total_len);
  651. dev_queue_xmit(skb);
  652. end:
  653. release_sock(sk);
  654. return error;
  655. }
  656. /************************************************************************
  657. *
  658. * xmit function for internal use.
  659. *
  660. ***********************************************************************/
  661. static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb)
  662. {
  663. struct pppox_sock *po = pppox_sk(sk);
  664. struct net_device *dev = po->pppoe_dev;
  665. struct pppoe_hdr hdr;
  666. struct pppoe_hdr *ph;
  667. int headroom = skb_headroom(skb);
  668. int data_len = skb->len;
  669. struct sk_buff *skb2;
  670. if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
  671. goto abort;
  672. hdr.ver = 1;
  673. hdr.type = 1;
  674. hdr.code = 0;
  675. hdr.sid = po->num;
  676. hdr.length = htons(skb->len);
  677. if (!dev)
  678. goto abort;
  679. /* Copy the skb if there is no space for the header. */
  680. if (headroom < (sizeof(struct pppoe_hdr) + dev->hard_header_len)) {
  681. skb2 = dev_alloc_skb(32+skb->len +
  682. sizeof(struct pppoe_hdr) +
  683. dev->hard_header_len);
  684. if (skb2 == NULL)
  685. goto abort;
  686. skb_reserve(skb2, dev->hard_header_len + sizeof(struct pppoe_hdr));
  687. memcpy(skb_put(skb2, skb->len), skb->data, skb->len);
  688. } else {
  689. /* Make a clone so as to not disturb the original skb,
  690. * give dev_queue_xmit something it can free.
  691. */
  692. skb2 = skb_clone(skb, GFP_ATOMIC);
  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 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("pppoe", S_IRUGO, proc_net);
  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. /* ->ioctl are set at pppox_create */
  856. static 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. };
  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("pppoe", proc_net);
  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);