pppoe.c 25 KB

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