pppoe.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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. 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 %pM %8s\n",
  764. po->pppoe_pa.sid, 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. __acquires(pppoe_hash_lock)
  785. {
  786. loff_t l = *pos;
  787. read_lock_bh(&pppoe_hash_lock);
  788. return l ? pppoe_get_idx(--l) : SEQ_START_TOKEN;
  789. }
  790. static void *pppoe_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  791. {
  792. struct pppox_sock *po;
  793. ++*pos;
  794. if (v == SEQ_START_TOKEN) {
  795. po = pppoe_get_idx(0);
  796. goto out;
  797. }
  798. po = v;
  799. if (po->next)
  800. po = po->next;
  801. else {
  802. int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
  803. while (++hash < PPPOE_HASH_SIZE) {
  804. po = item_hash_table[hash];
  805. if (po)
  806. break;
  807. }
  808. }
  809. out:
  810. return po;
  811. }
  812. static void pppoe_seq_stop(struct seq_file *seq, void *v)
  813. __releases(pppoe_hash_lock)
  814. {
  815. read_unlock_bh(&pppoe_hash_lock);
  816. }
  817. static struct seq_operations pppoe_seq_ops = {
  818. .start = pppoe_seq_start,
  819. .next = pppoe_seq_next,
  820. .stop = pppoe_seq_stop,
  821. .show = pppoe_seq_show,
  822. };
  823. static int pppoe_seq_open(struct inode *inode, struct file *file)
  824. {
  825. return seq_open(file, &pppoe_seq_ops);
  826. }
  827. static const struct file_operations pppoe_seq_fops = {
  828. .owner = THIS_MODULE,
  829. .open = pppoe_seq_open,
  830. .read = seq_read,
  831. .llseek = seq_lseek,
  832. .release = seq_release,
  833. };
  834. static int __init pppoe_proc_init(void)
  835. {
  836. struct proc_dir_entry *p;
  837. p = proc_net_fops_create(&init_net, "pppoe", S_IRUGO, &pppoe_seq_fops);
  838. if (!p)
  839. return -ENOMEM;
  840. return 0;
  841. }
  842. #else /* CONFIG_PROC_FS */
  843. static inline int pppoe_proc_init(void) { return 0; }
  844. #endif /* CONFIG_PROC_FS */
  845. static const struct proto_ops pppoe_ops = {
  846. .family = AF_PPPOX,
  847. .owner = THIS_MODULE,
  848. .release = pppoe_release,
  849. .bind = sock_no_bind,
  850. .connect = pppoe_connect,
  851. .socketpair = sock_no_socketpair,
  852. .accept = sock_no_accept,
  853. .getname = pppoe_getname,
  854. .poll = datagram_poll,
  855. .listen = sock_no_listen,
  856. .shutdown = sock_no_shutdown,
  857. .setsockopt = sock_no_setsockopt,
  858. .getsockopt = sock_no_getsockopt,
  859. .sendmsg = pppoe_sendmsg,
  860. .recvmsg = pppoe_recvmsg,
  861. .mmap = sock_no_mmap,
  862. .ioctl = pppox_ioctl,
  863. };
  864. static struct pppox_proto pppoe_proto = {
  865. .create = pppoe_create,
  866. .ioctl = pppoe_ioctl,
  867. .owner = THIS_MODULE,
  868. };
  869. static int __init pppoe_init(void)
  870. {
  871. int err = proto_register(&pppoe_sk_proto, 0);
  872. if (err)
  873. goto out;
  874. err = register_pppox_proto(PX_PROTO_OE, &pppoe_proto);
  875. if (err)
  876. goto out_unregister_pppoe_proto;
  877. err = pppoe_proc_init();
  878. if (err)
  879. goto out_unregister_pppox_proto;
  880. dev_add_pack(&pppoes_ptype);
  881. dev_add_pack(&pppoed_ptype);
  882. register_netdevice_notifier(&pppoe_notifier);
  883. out:
  884. return err;
  885. out_unregister_pppox_proto:
  886. unregister_pppox_proto(PX_PROTO_OE);
  887. out_unregister_pppoe_proto:
  888. proto_unregister(&pppoe_sk_proto);
  889. goto out;
  890. }
  891. static void __exit pppoe_exit(void)
  892. {
  893. unregister_pppox_proto(PX_PROTO_OE);
  894. dev_remove_pack(&pppoes_ptype);
  895. dev_remove_pack(&pppoed_ptype);
  896. unregister_netdevice_notifier(&pppoe_notifier);
  897. remove_proc_entry("pppoe", init_net.proc_net);
  898. proto_unregister(&pppoe_sk_proto);
  899. }
  900. module_init(pppoe_init);
  901. module_exit(pppoe_exit);
  902. MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>");
  903. MODULE_DESCRIPTION("PPP over Ethernet driver");
  904. MODULE_LICENSE("GPL");
  905. MODULE_ALIAS_NETPROTO(PF_PPPOX);