pppoe.c 25 KB

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