pppoe.c 25 KB

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