udp.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * The User Datagram Protocol (UDP).
  7. *
  8. * Version: $Id: udp.c,v 1.102 2002/02/01 22:01:04 davem Exp $
  9. *
  10. * Authors: Ross Biro
  11. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12. * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
  13. * Alan Cox, <Alan.Cox@linux.org>
  14. * Hirokazu Takahashi, <taka@valinux.co.jp>
  15. *
  16. * Fixes:
  17. * Alan Cox : verify_area() calls
  18. * Alan Cox : stopped close while in use off icmp
  19. * messages. Not a fix but a botch that
  20. * for udp at least is 'valid'.
  21. * Alan Cox : Fixed icmp handling properly
  22. * Alan Cox : Correct error for oversized datagrams
  23. * Alan Cox : Tidied select() semantics.
  24. * Alan Cox : udp_err() fixed properly, also now
  25. * select and read wake correctly on errors
  26. * Alan Cox : udp_send verify_area moved to avoid mem leak
  27. * Alan Cox : UDP can count its memory
  28. * Alan Cox : send to an unknown connection causes
  29. * an ECONNREFUSED off the icmp, but
  30. * does NOT close.
  31. * Alan Cox : Switched to new sk_buff handlers. No more backlog!
  32. * Alan Cox : Using generic datagram code. Even smaller and the PEEK
  33. * bug no longer crashes it.
  34. * Fred Van Kempen : Net2e support for sk->broadcast.
  35. * Alan Cox : Uses skb_free_datagram
  36. * Alan Cox : Added get/set sockopt support.
  37. * Alan Cox : Broadcasting without option set returns EACCES.
  38. * Alan Cox : No wakeup calls. Instead we now use the callbacks.
  39. * Alan Cox : Use ip_tos and ip_ttl
  40. * Alan Cox : SNMP Mibs
  41. * Alan Cox : MSG_DONTROUTE, and 0.0.0.0 support.
  42. * Matt Dillon : UDP length checks.
  43. * Alan Cox : Smarter af_inet used properly.
  44. * Alan Cox : Use new kernel side addressing.
  45. * Alan Cox : Incorrect return on truncated datagram receive.
  46. * Arnt Gulbrandsen : New udp_send and stuff
  47. * Alan Cox : Cache last socket
  48. * Alan Cox : Route cache
  49. * Jon Peatfield : Minor efficiency fix to sendto().
  50. * Mike Shaver : RFC1122 checks.
  51. * Alan Cox : Nonblocking error fix.
  52. * Willy Konynenberg : Transparent proxying support.
  53. * Mike McLagan : Routing by source
  54. * David S. Miller : New socket lookup architecture.
  55. * Last socket cache retained as it
  56. * does have a high hit rate.
  57. * Olaf Kirch : Don't linearise iovec on sendmsg.
  58. * Andi Kleen : Some cleanups, cache destination entry
  59. * for connect.
  60. * Vitaly E. Lavrov : Transparent proxy revived after year coma.
  61. * Melvin Smith : Check msg_name not msg_namelen in sendto(),
  62. * return ENOTCONN for unconnected sockets (POSIX)
  63. * Janos Farkas : don't deliver multi/broadcasts to a different
  64. * bound-to-device socket
  65. * Hirokazu Takahashi : HW checksumming for outgoing UDP
  66. * datagrams.
  67. * Hirokazu Takahashi : sendfile() on UDP works now.
  68. * Arnaldo C. Melo : convert /proc/net/udp to seq_file
  69. * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
  70. * Alexey Kuznetsov: allow both IPv4 and IPv6 sockets to bind
  71. * a single port at the same time.
  72. * Derek Atkins <derek@ihtfp.com>: Add Encapulation Support
  73. * James Chapman : Add L2TP encapsulation type.
  74. *
  75. *
  76. * This program is free software; you can redistribute it and/or
  77. * modify it under the terms of the GNU General Public License
  78. * as published by the Free Software Foundation; either version
  79. * 2 of the License, or (at your option) any later version.
  80. */
  81. #include <asm/system.h>
  82. #include <asm/uaccess.h>
  83. #include <asm/ioctls.h>
  84. #include <linux/types.h>
  85. #include <linux/fcntl.h>
  86. #include <linux/module.h>
  87. #include <linux/socket.h>
  88. #include <linux/sockios.h>
  89. #include <linux/igmp.h>
  90. #include <linux/in.h>
  91. #include <linux/errno.h>
  92. #include <linux/timer.h>
  93. #include <linux/mm.h>
  94. #include <linux/inet.h>
  95. #include <linux/netdevice.h>
  96. #include <net/tcp_states.h>
  97. #include <linux/skbuff.h>
  98. #include <linux/proc_fs.h>
  99. #include <linux/seq_file.h>
  100. #include <net/icmp.h>
  101. #include <net/route.h>
  102. #include <net/checksum.h>
  103. #include <net/xfrm.h>
  104. #include "udp_impl.h"
  105. /*
  106. * Snmp MIB for the UDP layer
  107. */
  108. DEFINE_SNMP_STAT(struct udp_mib, udp_statistics) __read_mostly;
  109. struct hlist_head udp_hash[UDP_HTABLE_SIZE];
  110. DEFINE_RWLOCK(udp_hash_lock);
  111. static int udp_port_rover;
  112. static inline int __udp_lib_lport_inuse(__u16 num, struct hlist_head udptable[])
  113. {
  114. struct sock *sk;
  115. struct hlist_node *node;
  116. sk_for_each(sk, node, &udptable[num & (UDP_HTABLE_SIZE - 1)])
  117. if (sk->sk_hash == num)
  118. return 1;
  119. return 0;
  120. }
  121. /**
  122. * __udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6
  123. *
  124. * @sk: socket struct in question
  125. * @snum: port number to look up
  126. * @udptable: hash list table, must be of UDP_HTABLE_SIZE
  127. * @port_rover: pointer to record of last unallocated port
  128. * @saddr_comp: AF-dependent comparison of bound local IP addresses
  129. */
  130. int __udp_lib_get_port(struct sock *sk, unsigned short snum,
  131. struct hlist_head udptable[], int *port_rover,
  132. int (*saddr_comp)(const struct sock *sk1,
  133. const struct sock *sk2 ) )
  134. {
  135. struct hlist_node *node;
  136. struct hlist_head *head;
  137. struct sock *sk2;
  138. int error = 1;
  139. write_lock_bh(&udp_hash_lock);
  140. if (snum == 0) {
  141. int best_size_so_far, best, result, i;
  142. if (*port_rover > sysctl_local_port_range[1] ||
  143. *port_rover < sysctl_local_port_range[0])
  144. *port_rover = sysctl_local_port_range[0];
  145. best_size_so_far = 32767;
  146. best = result = *port_rover;
  147. for (i = 0; i < UDP_HTABLE_SIZE; i++, result++) {
  148. int size;
  149. head = &udptable[result & (UDP_HTABLE_SIZE - 1)];
  150. if (hlist_empty(head)) {
  151. if (result > sysctl_local_port_range[1])
  152. result = sysctl_local_port_range[0] +
  153. ((result - sysctl_local_port_range[0]) &
  154. (UDP_HTABLE_SIZE - 1));
  155. goto gotit;
  156. }
  157. size = 0;
  158. sk_for_each(sk2, node, head) {
  159. if (++size >= best_size_so_far)
  160. goto next;
  161. }
  162. best_size_so_far = size;
  163. best = result;
  164. next:
  165. ;
  166. }
  167. result = best;
  168. for (i = 0; i < (1 << 16) / UDP_HTABLE_SIZE;
  169. i++, result += UDP_HTABLE_SIZE) {
  170. if (result > sysctl_local_port_range[1])
  171. result = sysctl_local_port_range[0]
  172. + ((result - sysctl_local_port_range[0]) &
  173. (UDP_HTABLE_SIZE - 1));
  174. if (! __udp_lib_lport_inuse(result, udptable))
  175. break;
  176. }
  177. if (i >= (1 << 16) / UDP_HTABLE_SIZE)
  178. goto fail;
  179. gotit:
  180. *port_rover = snum = result;
  181. } else {
  182. head = &udptable[snum & (UDP_HTABLE_SIZE - 1)];
  183. sk_for_each(sk2, node, head)
  184. if (sk2->sk_hash == snum &&
  185. sk2 != sk &&
  186. (!sk2->sk_reuse || !sk->sk_reuse) &&
  187. (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if
  188. || sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
  189. (*saddr_comp)(sk, sk2) )
  190. goto fail;
  191. }
  192. inet_sk(sk)->num = snum;
  193. sk->sk_hash = snum;
  194. if (sk_unhashed(sk)) {
  195. head = &udptable[snum & (UDP_HTABLE_SIZE - 1)];
  196. sk_add_node(sk, head);
  197. sock_prot_inc_use(sk->sk_prot);
  198. }
  199. error = 0;
  200. fail:
  201. write_unlock_bh(&udp_hash_lock);
  202. return error;
  203. }
  204. int udp_get_port(struct sock *sk, unsigned short snum,
  205. int (*scmp)(const struct sock *, const struct sock *))
  206. {
  207. return __udp_lib_get_port(sk, snum, udp_hash, &udp_port_rover, scmp);
  208. }
  209. int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2)
  210. {
  211. struct inet_sock *inet1 = inet_sk(sk1), *inet2 = inet_sk(sk2);
  212. return ( !ipv6_only_sock(sk2) &&
  213. (!inet1->rcv_saddr || !inet2->rcv_saddr ||
  214. inet1->rcv_saddr == inet2->rcv_saddr ));
  215. }
  216. static inline int udp_v4_get_port(struct sock *sk, unsigned short snum)
  217. {
  218. return udp_get_port(sk, snum, ipv4_rcv_saddr_equal);
  219. }
  220. /* UDP is nearly always wildcards out the wazoo, it makes no sense to try
  221. * harder than this. -DaveM
  222. */
  223. static struct sock *__udp4_lib_lookup(__be32 saddr, __be16 sport,
  224. __be32 daddr, __be16 dport,
  225. int dif, struct hlist_head udptable[])
  226. {
  227. struct sock *sk, *result = NULL;
  228. struct hlist_node *node;
  229. unsigned short hnum = ntohs(dport);
  230. int badness = -1;
  231. read_lock(&udp_hash_lock);
  232. sk_for_each(sk, node, &udptable[hnum & (UDP_HTABLE_SIZE - 1)]) {
  233. struct inet_sock *inet = inet_sk(sk);
  234. if (sk->sk_hash == hnum && !ipv6_only_sock(sk)) {
  235. int score = (sk->sk_family == PF_INET ? 1 : 0);
  236. if (inet->rcv_saddr) {
  237. if (inet->rcv_saddr != daddr)
  238. continue;
  239. score+=2;
  240. }
  241. if (inet->daddr) {
  242. if (inet->daddr != saddr)
  243. continue;
  244. score+=2;
  245. }
  246. if (inet->dport) {
  247. if (inet->dport != sport)
  248. continue;
  249. score+=2;
  250. }
  251. if (sk->sk_bound_dev_if) {
  252. if (sk->sk_bound_dev_if != dif)
  253. continue;
  254. score+=2;
  255. }
  256. if (score == 9) {
  257. result = sk;
  258. break;
  259. } else if (score > badness) {
  260. result = sk;
  261. badness = score;
  262. }
  263. }
  264. }
  265. if (result)
  266. sock_hold(result);
  267. read_unlock(&udp_hash_lock);
  268. return result;
  269. }
  270. static inline struct sock *udp_v4_mcast_next(struct sock *sk,
  271. __be16 loc_port, __be32 loc_addr,
  272. __be16 rmt_port, __be32 rmt_addr,
  273. int dif)
  274. {
  275. struct hlist_node *node;
  276. struct sock *s = sk;
  277. unsigned short hnum = ntohs(loc_port);
  278. sk_for_each_from(s, node) {
  279. struct inet_sock *inet = inet_sk(s);
  280. if (s->sk_hash != hnum ||
  281. (inet->daddr && inet->daddr != rmt_addr) ||
  282. (inet->dport != rmt_port && inet->dport) ||
  283. (inet->rcv_saddr && inet->rcv_saddr != loc_addr) ||
  284. ipv6_only_sock(s) ||
  285. (s->sk_bound_dev_if && s->sk_bound_dev_if != dif))
  286. continue;
  287. if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif))
  288. continue;
  289. goto found;
  290. }
  291. s = NULL;
  292. found:
  293. return s;
  294. }
  295. /*
  296. * This routine is called by the ICMP module when it gets some
  297. * sort of error condition. If err < 0 then the socket should
  298. * be closed and the error returned to the user. If err > 0
  299. * it's just the icmp type << 8 | icmp code.
  300. * Header points to the ip header of the error packet. We move
  301. * on past this. Then (as it used to claim before adjustment)
  302. * header points to the first 8 bytes of the udp header. We need
  303. * to find the appropriate port.
  304. */
  305. void __udp4_lib_err(struct sk_buff *skb, u32 info, struct hlist_head udptable[])
  306. {
  307. struct inet_sock *inet;
  308. struct iphdr *iph = (struct iphdr*)skb->data;
  309. struct udphdr *uh = (struct udphdr*)(skb->data+(iph->ihl<<2));
  310. const int type = icmp_hdr(skb)->type;
  311. const int code = icmp_hdr(skb)->code;
  312. struct sock *sk;
  313. int harderr;
  314. int err;
  315. sk = __udp4_lib_lookup(iph->daddr, uh->dest, iph->saddr, uh->source,
  316. skb->dev->ifindex, udptable );
  317. if (sk == NULL) {
  318. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  319. return; /* No socket for error */
  320. }
  321. err = 0;
  322. harderr = 0;
  323. inet = inet_sk(sk);
  324. switch (type) {
  325. default:
  326. case ICMP_TIME_EXCEEDED:
  327. err = EHOSTUNREACH;
  328. break;
  329. case ICMP_SOURCE_QUENCH:
  330. goto out;
  331. case ICMP_PARAMETERPROB:
  332. err = EPROTO;
  333. harderr = 1;
  334. break;
  335. case ICMP_DEST_UNREACH:
  336. if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
  337. if (inet->pmtudisc != IP_PMTUDISC_DONT) {
  338. err = EMSGSIZE;
  339. harderr = 1;
  340. break;
  341. }
  342. goto out;
  343. }
  344. err = EHOSTUNREACH;
  345. if (code <= NR_ICMP_UNREACH) {
  346. harderr = icmp_err_convert[code].fatal;
  347. err = icmp_err_convert[code].errno;
  348. }
  349. break;
  350. }
  351. /*
  352. * RFC1122: OK. Passes ICMP errors back to application, as per
  353. * 4.1.3.3.
  354. */
  355. if (!inet->recverr) {
  356. if (!harderr || sk->sk_state != TCP_ESTABLISHED)
  357. goto out;
  358. } else {
  359. ip_icmp_error(sk, skb, err, uh->dest, info, (u8*)(uh+1));
  360. }
  361. sk->sk_err = err;
  362. sk->sk_error_report(sk);
  363. out:
  364. sock_put(sk);
  365. }
  366. void udp_err(struct sk_buff *skb, u32 info)
  367. {
  368. return __udp4_lib_err(skb, info, udp_hash);
  369. }
  370. /*
  371. * Throw away all pending data and cancel the corking. Socket is locked.
  372. */
  373. static void udp_flush_pending_frames(struct sock *sk)
  374. {
  375. struct udp_sock *up = udp_sk(sk);
  376. if (up->pending) {
  377. up->len = 0;
  378. up->pending = 0;
  379. ip_flush_pending_frames(sk);
  380. }
  381. }
  382. /**
  383. * udp4_hwcsum_outgoing - handle outgoing HW checksumming
  384. * @sk: socket we are sending on
  385. * @skb: sk_buff containing the filled-in UDP header
  386. * (checksum field must be zeroed out)
  387. */
  388. static void udp4_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
  389. __be32 src, __be32 dst, int len )
  390. {
  391. unsigned int offset;
  392. struct udphdr *uh = udp_hdr(skb);
  393. __wsum csum = 0;
  394. if (skb_queue_len(&sk->sk_write_queue) == 1) {
  395. /*
  396. * Only one fragment on the socket.
  397. */
  398. skb->csum_start = skb_transport_header(skb) - skb->head;
  399. skb->csum_offset = offsetof(struct udphdr, check);
  400. uh->check = ~csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, 0);
  401. } else {
  402. /*
  403. * HW-checksum won't work as there are two or more
  404. * fragments on the socket so that all csums of sk_buffs
  405. * should be together
  406. */
  407. offset = skb_transport_offset(skb);
  408. skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
  409. skb->ip_summed = CHECKSUM_NONE;
  410. skb_queue_walk(&sk->sk_write_queue, skb) {
  411. csum = csum_add(csum, skb->csum);
  412. }
  413. uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum);
  414. if (uh->check == 0)
  415. uh->check = CSUM_MANGLED_0;
  416. }
  417. }
  418. /*
  419. * Push out all pending data as one UDP datagram. Socket is locked.
  420. */
  421. static int udp_push_pending_frames(struct sock *sk)
  422. {
  423. struct udp_sock *up = udp_sk(sk);
  424. struct inet_sock *inet = inet_sk(sk);
  425. struct flowi *fl = &inet->cork.fl;
  426. struct sk_buff *skb;
  427. struct udphdr *uh;
  428. int err = 0;
  429. __wsum csum = 0;
  430. /* Grab the skbuff where UDP header space exists. */
  431. if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
  432. goto out;
  433. /*
  434. * Create a UDP header
  435. */
  436. uh = udp_hdr(skb);
  437. uh->source = fl->fl_ip_sport;
  438. uh->dest = fl->fl_ip_dport;
  439. uh->len = htons(up->len);
  440. uh->check = 0;
  441. if (up->pcflag) /* UDP-Lite */
  442. csum = udplite_csum_outgoing(sk, skb);
  443. else if (sk->sk_no_check == UDP_CSUM_NOXMIT) { /* UDP csum disabled */
  444. skb->ip_summed = CHECKSUM_NONE;
  445. goto send;
  446. } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
  447. udp4_hwcsum_outgoing(sk, skb, fl->fl4_src,fl->fl4_dst, up->len);
  448. goto send;
  449. } else /* `normal' UDP */
  450. csum = udp_csum_outgoing(sk, skb);
  451. /* add protocol-dependent pseudo-header */
  452. uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst, up->len,
  453. sk->sk_protocol, csum );
  454. if (uh->check == 0)
  455. uh->check = CSUM_MANGLED_0;
  456. send:
  457. err = ip_push_pending_frames(sk);
  458. out:
  459. up->len = 0;
  460. up->pending = 0;
  461. return err;
  462. }
  463. int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  464. size_t len)
  465. {
  466. struct inet_sock *inet = inet_sk(sk);
  467. struct udp_sock *up = udp_sk(sk);
  468. int ulen = len;
  469. struct ipcm_cookie ipc;
  470. struct rtable *rt = NULL;
  471. int free = 0;
  472. int connected = 0;
  473. __be32 daddr, faddr, saddr;
  474. __be16 dport;
  475. u8 tos;
  476. int err, is_udplite = up->pcflag;
  477. int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
  478. int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
  479. if (len > 0xFFFF)
  480. return -EMSGSIZE;
  481. /*
  482. * Check the flags.
  483. */
  484. if (msg->msg_flags&MSG_OOB) /* Mirror BSD error message compatibility */
  485. return -EOPNOTSUPP;
  486. ipc.opt = NULL;
  487. if (up->pending) {
  488. /*
  489. * There are pending frames.
  490. * The socket lock must be held while it's corked.
  491. */
  492. lock_sock(sk);
  493. if (likely(up->pending)) {
  494. if (unlikely(up->pending != AF_INET)) {
  495. release_sock(sk);
  496. return -EINVAL;
  497. }
  498. goto do_append_data;
  499. }
  500. release_sock(sk);
  501. }
  502. ulen += sizeof(struct udphdr);
  503. /*
  504. * Get and verify the address.
  505. */
  506. if (msg->msg_name) {
  507. struct sockaddr_in * usin = (struct sockaddr_in*)msg->msg_name;
  508. if (msg->msg_namelen < sizeof(*usin))
  509. return -EINVAL;
  510. if (usin->sin_family != AF_INET) {
  511. if (usin->sin_family != AF_UNSPEC)
  512. return -EAFNOSUPPORT;
  513. }
  514. daddr = usin->sin_addr.s_addr;
  515. dport = usin->sin_port;
  516. if (dport == 0)
  517. return -EINVAL;
  518. } else {
  519. if (sk->sk_state != TCP_ESTABLISHED)
  520. return -EDESTADDRREQ;
  521. daddr = inet->daddr;
  522. dport = inet->dport;
  523. /* Open fast path for connected socket.
  524. Route will not be used, if at least one option is set.
  525. */
  526. connected = 1;
  527. }
  528. ipc.addr = inet->saddr;
  529. ipc.oif = sk->sk_bound_dev_if;
  530. if (msg->msg_controllen) {
  531. err = ip_cmsg_send(msg, &ipc);
  532. if (err)
  533. return err;
  534. if (ipc.opt)
  535. free = 1;
  536. connected = 0;
  537. }
  538. if (!ipc.opt)
  539. ipc.opt = inet->opt;
  540. saddr = ipc.addr;
  541. ipc.addr = faddr = daddr;
  542. if (ipc.opt && ipc.opt->srr) {
  543. if (!daddr)
  544. return -EINVAL;
  545. faddr = ipc.opt->faddr;
  546. connected = 0;
  547. }
  548. tos = RT_TOS(inet->tos);
  549. if (sock_flag(sk, SOCK_LOCALROUTE) ||
  550. (msg->msg_flags & MSG_DONTROUTE) ||
  551. (ipc.opt && ipc.opt->is_strictroute)) {
  552. tos |= RTO_ONLINK;
  553. connected = 0;
  554. }
  555. if (MULTICAST(daddr)) {
  556. if (!ipc.oif)
  557. ipc.oif = inet->mc_index;
  558. if (!saddr)
  559. saddr = inet->mc_addr;
  560. connected = 0;
  561. }
  562. if (connected)
  563. rt = (struct rtable*)sk_dst_check(sk, 0);
  564. if (rt == NULL) {
  565. struct flowi fl = { .oif = ipc.oif,
  566. .nl_u = { .ip4_u =
  567. { .daddr = faddr,
  568. .saddr = saddr,
  569. .tos = tos } },
  570. .proto = sk->sk_protocol,
  571. .uli_u = { .ports =
  572. { .sport = inet->sport,
  573. .dport = dport } } };
  574. security_sk_classify_flow(sk, &fl);
  575. err = ip_route_output_flow(&rt, &fl, sk, 1);
  576. if (err) {
  577. if (err == -ENETUNREACH)
  578. IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
  579. goto out;
  580. }
  581. err = -EACCES;
  582. if ((rt->rt_flags & RTCF_BROADCAST) &&
  583. !sock_flag(sk, SOCK_BROADCAST))
  584. goto out;
  585. if (connected)
  586. sk_dst_set(sk, dst_clone(&rt->u.dst));
  587. }
  588. if (msg->msg_flags&MSG_CONFIRM)
  589. goto do_confirm;
  590. back_from_confirm:
  591. saddr = rt->rt_src;
  592. if (!ipc.addr)
  593. daddr = ipc.addr = rt->rt_dst;
  594. lock_sock(sk);
  595. if (unlikely(up->pending)) {
  596. /* The socket is already corked while preparing it. */
  597. /* ... which is an evident application bug. --ANK */
  598. release_sock(sk);
  599. LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
  600. err = -EINVAL;
  601. goto out;
  602. }
  603. /*
  604. * Now cork the socket to pend data.
  605. */
  606. inet->cork.fl.fl4_dst = daddr;
  607. inet->cork.fl.fl_ip_dport = dport;
  608. inet->cork.fl.fl4_src = saddr;
  609. inet->cork.fl.fl_ip_sport = inet->sport;
  610. up->pending = AF_INET;
  611. do_append_data:
  612. up->len += ulen;
  613. getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
  614. err = ip_append_data(sk, getfrag, msg->msg_iov, ulen,
  615. sizeof(struct udphdr), &ipc, rt,
  616. corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
  617. if (err)
  618. udp_flush_pending_frames(sk);
  619. else if (!corkreq)
  620. err = udp_push_pending_frames(sk);
  621. else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
  622. up->pending = 0;
  623. release_sock(sk);
  624. out:
  625. ip_rt_put(rt);
  626. if (free)
  627. kfree(ipc.opt);
  628. if (!err) {
  629. UDP_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS, is_udplite);
  630. return len;
  631. }
  632. /*
  633. * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
  634. * ENOBUFS might not be good (it's not tunable per se), but otherwise
  635. * we don't have a good statistic (IpOutDiscards but it can be too many
  636. * things). We could add another new stat but at least for now that
  637. * seems like overkill.
  638. */
  639. if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
  640. UDP_INC_STATS_USER(UDP_MIB_SNDBUFERRORS, is_udplite);
  641. }
  642. return err;
  643. do_confirm:
  644. dst_confirm(&rt->u.dst);
  645. if (!(msg->msg_flags&MSG_PROBE) || len)
  646. goto back_from_confirm;
  647. err = 0;
  648. goto out;
  649. }
  650. int udp_sendpage(struct sock *sk, struct page *page, int offset,
  651. size_t size, int flags)
  652. {
  653. struct udp_sock *up = udp_sk(sk);
  654. int ret;
  655. if (!up->pending) {
  656. struct msghdr msg = { .msg_flags = flags|MSG_MORE };
  657. /* Call udp_sendmsg to specify destination address which
  658. * sendpage interface can't pass.
  659. * This will succeed only when the socket is connected.
  660. */
  661. ret = udp_sendmsg(NULL, sk, &msg, 0);
  662. if (ret < 0)
  663. return ret;
  664. }
  665. lock_sock(sk);
  666. if (unlikely(!up->pending)) {
  667. release_sock(sk);
  668. LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n");
  669. return -EINVAL;
  670. }
  671. ret = ip_append_page(sk, page, offset, size, flags);
  672. if (ret == -EOPNOTSUPP) {
  673. release_sock(sk);
  674. return sock_no_sendpage(sk->sk_socket, page, offset,
  675. size, flags);
  676. }
  677. if (ret < 0) {
  678. udp_flush_pending_frames(sk);
  679. goto out;
  680. }
  681. up->len += size;
  682. if (!(up->corkflag || (flags&MSG_MORE)))
  683. ret = udp_push_pending_frames(sk);
  684. if (!ret)
  685. ret = size;
  686. out:
  687. release_sock(sk);
  688. return ret;
  689. }
  690. /*
  691. * IOCTL requests applicable to the UDP protocol
  692. */
  693. int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  694. {
  695. switch (cmd) {
  696. case SIOCOUTQ:
  697. {
  698. int amount = atomic_read(&sk->sk_wmem_alloc);
  699. return put_user(amount, (int __user *)arg);
  700. }
  701. case SIOCINQ:
  702. {
  703. struct sk_buff *skb;
  704. unsigned long amount;
  705. amount = 0;
  706. spin_lock_bh(&sk->sk_receive_queue.lock);
  707. skb = skb_peek(&sk->sk_receive_queue);
  708. if (skb != NULL) {
  709. /*
  710. * We will only return the amount
  711. * of this packet since that is all
  712. * that will be read.
  713. */
  714. amount = skb->len - sizeof(struct udphdr);
  715. }
  716. spin_unlock_bh(&sk->sk_receive_queue.lock);
  717. return put_user(amount, (int __user *)arg);
  718. }
  719. default:
  720. return -ENOIOCTLCMD;
  721. }
  722. return 0;
  723. }
  724. /*
  725. * This should be easy, if there is something there we
  726. * return it, otherwise we block.
  727. */
  728. int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  729. size_t len, int noblock, int flags, int *addr_len)
  730. {
  731. struct inet_sock *inet = inet_sk(sk);
  732. struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
  733. struct sk_buff *skb;
  734. unsigned int ulen, copied;
  735. int err;
  736. int is_udplite = IS_UDPLITE(sk);
  737. /*
  738. * Check any passed addresses
  739. */
  740. if (addr_len)
  741. *addr_len=sizeof(*sin);
  742. if (flags & MSG_ERRQUEUE)
  743. return ip_recv_error(sk, msg, len);
  744. try_again:
  745. skb = skb_recv_datagram(sk, flags, noblock, &err);
  746. if (!skb)
  747. goto out;
  748. ulen = skb->len - sizeof(struct udphdr);
  749. copied = len;
  750. if (copied > ulen)
  751. copied = ulen;
  752. else if (copied < ulen)
  753. msg->msg_flags |= MSG_TRUNC;
  754. /*
  755. * If checksum is needed at all, try to do it while copying the
  756. * data. If the data is truncated, or if we only want a partial
  757. * coverage checksum (UDP-Lite), do it before the copy.
  758. */
  759. if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
  760. if (udp_lib_checksum_complete(skb))
  761. goto csum_copy_err;
  762. }
  763. if (skb_csum_unnecessary(skb))
  764. err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
  765. msg->msg_iov, copied );
  766. else {
  767. err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
  768. if (err == -EINVAL)
  769. goto csum_copy_err;
  770. }
  771. if (err)
  772. goto out_free;
  773. sock_recv_timestamp(msg, sk, skb);
  774. /* Copy the address. */
  775. if (sin)
  776. {
  777. sin->sin_family = AF_INET;
  778. sin->sin_port = udp_hdr(skb)->source;
  779. sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
  780. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  781. }
  782. if (inet->cmsg_flags)
  783. ip_cmsg_recv(msg, skb);
  784. err = copied;
  785. if (flags & MSG_TRUNC)
  786. err = ulen;
  787. out_free:
  788. skb_free_datagram(sk, skb);
  789. out:
  790. return err;
  791. csum_copy_err:
  792. UDP_INC_STATS_BH(UDP_MIB_INERRORS, is_udplite);
  793. skb_kill_datagram(sk, skb, flags);
  794. if (noblock)
  795. return -EAGAIN;
  796. goto try_again;
  797. }
  798. int udp_disconnect(struct sock *sk, int flags)
  799. {
  800. struct inet_sock *inet = inet_sk(sk);
  801. /*
  802. * 1003.1g - break association.
  803. */
  804. sk->sk_state = TCP_CLOSE;
  805. inet->daddr = 0;
  806. inet->dport = 0;
  807. sk->sk_bound_dev_if = 0;
  808. if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
  809. inet_reset_saddr(sk);
  810. if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
  811. sk->sk_prot->unhash(sk);
  812. inet->sport = 0;
  813. }
  814. sk_dst_reset(sk);
  815. return 0;
  816. }
  817. /* returns:
  818. * -1: error
  819. * 0: success
  820. * >0: "udp encap" protocol resubmission
  821. *
  822. * Note that in the success and error cases, the skb is assumed to
  823. * have either been requeued or freed.
  824. */
  825. int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
  826. {
  827. struct udp_sock *up = udp_sk(sk);
  828. int rc;
  829. /*
  830. * Charge it to the socket, dropping if the queue is full.
  831. */
  832. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
  833. goto drop;
  834. nf_reset(skb);
  835. if (up->encap_type) {
  836. /*
  837. * This is an encapsulation socket so pass the skb to
  838. * the socket's udp_encap_rcv() hook. Otherwise, just
  839. * fall through and pass this up the UDP socket.
  840. * up->encap_rcv() returns the following value:
  841. * =0 if skb was successfully passed to the encap
  842. * handler or was discarded by it.
  843. * >0 if skb should be passed on to UDP.
  844. * <0 if skb should be resubmitted as proto -N
  845. */
  846. /* if we're overly short, let UDP handle it */
  847. if (skb->len > sizeof(struct udphdr) &&
  848. up->encap_rcv != NULL) {
  849. int ret;
  850. ret = (*up->encap_rcv)(sk, skb);
  851. if (ret <= 0) {
  852. UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS, up->pcflag);
  853. return -ret;
  854. }
  855. }
  856. /* FALLTHROUGH -- it's a UDP Packet */
  857. }
  858. /*
  859. * UDP-Lite specific tests, ignored on UDP sockets
  860. */
  861. if ((up->pcflag & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
  862. /*
  863. * MIB statistics other than incrementing the error count are
  864. * disabled for the following two types of errors: these depend
  865. * on the application settings, not on the functioning of the
  866. * protocol stack as such.
  867. *
  868. * RFC 3828 here recommends (sec 3.3): "There should also be a
  869. * way ... to ... at least let the receiving application block
  870. * delivery of packets with coverage values less than a value
  871. * provided by the application."
  872. */
  873. if (up->pcrlen == 0) { /* full coverage was set */
  874. LIMIT_NETDEBUG(KERN_WARNING "UDPLITE: partial coverage "
  875. "%d while full coverage %d requested\n",
  876. UDP_SKB_CB(skb)->cscov, skb->len);
  877. goto drop;
  878. }
  879. /* The next case involves violating the min. coverage requested
  880. * by the receiver. This is subtle: if receiver wants x and x is
  881. * greater than the buffersize/MTU then receiver will complain
  882. * that it wants x while sender emits packets of smaller size y.
  883. * Therefore the above ...()->partial_cov statement is essential.
  884. */
  885. if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
  886. LIMIT_NETDEBUG(KERN_WARNING
  887. "UDPLITE: coverage %d too small, need min %d\n",
  888. UDP_SKB_CB(skb)->cscov, up->pcrlen);
  889. goto drop;
  890. }
  891. }
  892. if (sk->sk_filter) {
  893. if (udp_lib_checksum_complete(skb))
  894. goto drop;
  895. }
  896. if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) {
  897. /* Note that an ENOMEM error is charged twice */
  898. if (rc == -ENOMEM)
  899. UDP_INC_STATS_BH(UDP_MIB_RCVBUFERRORS, up->pcflag);
  900. goto drop;
  901. }
  902. UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS, up->pcflag);
  903. return 0;
  904. drop:
  905. UDP_INC_STATS_BH(UDP_MIB_INERRORS, up->pcflag);
  906. kfree_skb(skb);
  907. return -1;
  908. }
  909. /*
  910. * Multicasts and broadcasts go to each listener.
  911. *
  912. * Note: called only from the BH handler context,
  913. * so we don't need to lock the hashes.
  914. */
  915. static int __udp4_lib_mcast_deliver(struct sk_buff *skb,
  916. struct udphdr *uh,
  917. __be32 saddr, __be32 daddr,
  918. struct hlist_head udptable[])
  919. {
  920. struct sock *sk;
  921. int dif;
  922. read_lock(&udp_hash_lock);
  923. sk = sk_head(&udptable[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]);
  924. dif = skb->dev->ifindex;
  925. sk = udp_v4_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
  926. if (sk) {
  927. struct sock *sknext = NULL;
  928. do {
  929. struct sk_buff *skb1 = skb;
  930. sknext = udp_v4_mcast_next(sk_next(sk), uh->dest, daddr,
  931. uh->source, saddr, dif);
  932. if (sknext)
  933. skb1 = skb_clone(skb, GFP_ATOMIC);
  934. if (skb1) {
  935. int ret = udp_queue_rcv_skb(sk, skb1);
  936. if (ret > 0)
  937. /* we should probably re-process instead
  938. * of dropping packets here. */
  939. kfree_skb(skb1);
  940. }
  941. sk = sknext;
  942. } while (sknext);
  943. } else
  944. kfree_skb(skb);
  945. read_unlock(&udp_hash_lock);
  946. return 0;
  947. }
  948. /* Initialize UDP checksum. If exited with zero value (success),
  949. * CHECKSUM_UNNECESSARY means, that no more checks are required.
  950. * Otherwise, csum completion requires chacksumming packet body,
  951. * including udp header and folding it to skb->csum.
  952. */
  953. static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh,
  954. int proto)
  955. {
  956. const struct iphdr *iph;
  957. int err;
  958. UDP_SKB_CB(skb)->partial_cov = 0;
  959. UDP_SKB_CB(skb)->cscov = skb->len;
  960. if (proto == IPPROTO_UDPLITE) {
  961. err = udplite_checksum_init(skb, uh);
  962. if (err)
  963. return err;
  964. }
  965. iph = ip_hdr(skb);
  966. if (uh->check == 0) {
  967. skb->ip_summed = CHECKSUM_UNNECESSARY;
  968. } else if (skb->ip_summed == CHECKSUM_COMPLETE) {
  969. if (!csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
  970. proto, skb->csum))
  971. skb->ip_summed = CHECKSUM_UNNECESSARY;
  972. }
  973. if (!skb_csum_unnecessary(skb))
  974. skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
  975. skb->len, proto, 0);
  976. /* Probably, we should checksum udp header (it should be in cache
  977. * in any case) and data in tiny packets (< rx copybreak).
  978. */
  979. return 0;
  980. }
  981. /*
  982. * All we need to do is get the socket, and then do a checksum.
  983. */
  984. int __udp4_lib_rcv(struct sk_buff *skb, struct hlist_head udptable[],
  985. int proto)
  986. {
  987. struct sock *sk;
  988. struct udphdr *uh = udp_hdr(skb);
  989. unsigned short ulen;
  990. struct rtable *rt = (struct rtable*)skb->dst;
  991. __be32 saddr = ip_hdr(skb)->saddr;
  992. __be32 daddr = ip_hdr(skb)->daddr;
  993. /*
  994. * Validate the packet.
  995. */
  996. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  997. goto drop; /* No space for header. */
  998. ulen = ntohs(uh->len);
  999. if (ulen > skb->len)
  1000. goto short_packet;
  1001. if (proto == IPPROTO_UDP) {
  1002. /* UDP validates ulen. */
  1003. if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen))
  1004. goto short_packet;
  1005. uh = udp_hdr(skb);
  1006. }
  1007. if (udp4_csum_init(skb, uh, proto))
  1008. goto csum_error;
  1009. if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
  1010. return __udp4_lib_mcast_deliver(skb, uh, saddr, daddr, udptable);
  1011. sk = __udp4_lib_lookup(saddr, uh->source, daddr, uh->dest,
  1012. skb->dev->ifindex, udptable );
  1013. if (sk != NULL) {
  1014. int ret = udp_queue_rcv_skb(sk, skb);
  1015. sock_put(sk);
  1016. /* a return value > 0 means to resubmit the input, but
  1017. * it wants the return to be -protocol, or 0
  1018. */
  1019. if (ret > 0)
  1020. return -ret;
  1021. return 0;
  1022. }
  1023. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  1024. goto drop;
  1025. nf_reset(skb);
  1026. /* No socket. Drop packet silently, if checksum is wrong */
  1027. if (udp_lib_checksum_complete(skb))
  1028. goto csum_error;
  1029. UDP_INC_STATS_BH(UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
  1030. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  1031. /*
  1032. * Hmm. We got an UDP packet to a port to which we
  1033. * don't wanna listen. Ignore it.
  1034. */
  1035. kfree_skb(skb);
  1036. return 0;
  1037. short_packet:
  1038. LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From %u.%u.%u.%u:%u %d/%d to %u.%u.%u.%u:%u\n",
  1039. proto == IPPROTO_UDPLITE ? "-Lite" : "",
  1040. NIPQUAD(saddr),
  1041. ntohs(uh->source),
  1042. ulen,
  1043. skb->len,
  1044. NIPQUAD(daddr),
  1045. ntohs(uh->dest));
  1046. goto drop;
  1047. csum_error:
  1048. /*
  1049. * RFC1122: OK. Discards the bad packet silently (as far as
  1050. * the network is concerned, anyway) as per 4.1.3.4 (MUST).
  1051. */
  1052. LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From %d.%d.%d.%d:%d to %d.%d.%d.%d:%d ulen %d\n",
  1053. proto == IPPROTO_UDPLITE ? "-Lite" : "",
  1054. NIPQUAD(saddr),
  1055. ntohs(uh->source),
  1056. NIPQUAD(daddr),
  1057. ntohs(uh->dest),
  1058. ulen);
  1059. drop:
  1060. UDP_INC_STATS_BH(UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
  1061. kfree_skb(skb);
  1062. return 0;
  1063. }
  1064. int udp_rcv(struct sk_buff *skb)
  1065. {
  1066. return __udp4_lib_rcv(skb, udp_hash, IPPROTO_UDP);
  1067. }
  1068. int udp_destroy_sock(struct sock *sk)
  1069. {
  1070. lock_sock(sk);
  1071. udp_flush_pending_frames(sk);
  1072. release_sock(sk);
  1073. return 0;
  1074. }
  1075. /*
  1076. * Socket option code for UDP
  1077. */
  1078. int udp_lib_setsockopt(struct sock *sk, int level, int optname,
  1079. char __user *optval, int optlen,
  1080. int (*push_pending_frames)(struct sock *))
  1081. {
  1082. struct udp_sock *up = udp_sk(sk);
  1083. int val;
  1084. int err = 0;
  1085. if (optlen<sizeof(int))
  1086. return -EINVAL;
  1087. if (get_user(val, (int __user *)optval))
  1088. return -EFAULT;
  1089. switch (optname) {
  1090. case UDP_CORK:
  1091. if (val != 0) {
  1092. up->corkflag = 1;
  1093. } else {
  1094. up->corkflag = 0;
  1095. lock_sock(sk);
  1096. (*push_pending_frames)(sk);
  1097. release_sock(sk);
  1098. }
  1099. break;
  1100. case UDP_ENCAP:
  1101. switch (val) {
  1102. case 0:
  1103. case UDP_ENCAP_ESPINUDP:
  1104. case UDP_ENCAP_ESPINUDP_NON_IKE:
  1105. up->encap_rcv = xfrm4_udp_encap_rcv;
  1106. /* FALLTHROUGH */
  1107. case UDP_ENCAP_L2TPINUDP:
  1108. up->encap_type = val;
  1109. break;
  1110. default:
  1111. err = -ENOPROTOOPT;
  1112. break;
  1113. }
  1114. break;
  1115. /*
  1116. * UDP-Lite's partial checksum coverage (RFC 3828).
  1117. */
  1118. /* The sender sets actual checksum coverage length via this option.
  1119. * The case coverage > packet length is handled by send module. */
  1120. case UDPLITE_SEND_CSCOV:
  1121. if (!up->pcflag) /* Disable the option on UDP sockets */
  1122. return -ENOPROTOOPT;
  1123. if (val != 0 && val < 8) /* Illegal coverage: use default (8) */
  1124. val = 8;
  1125. up->pcslen = val;
  1126. up->pcflag |= UDPLITE_SEND_CC;
  1127. break;
  1128. /* The receiver specifies a minimum checksum coverage value. To make
  1129. * sense, this should be set to at least 8 (as done below). If zero is
  1130. * used, this again means full checksum coverage. */
  1131. case UDPLITE_RECV_CSCOV:
  1132. if (!up->pcflag) /* Disable the option on UDP sockets */
  1133. return -ENOPROTOOPT;
  1134. if (val != 0 && val < 8) /* Avoid silly minimal values. */
  1135. val = 8;
  1136. up->pcrlen = val;
  1137. up->pcflag |= UDPLITE_RECV_CC;
  1138. break;
  1139. default:
  1140. err = -ENOPROTOOPT;
  1141. break;
  1142. }
  1143. return err;
  1144. }
  1145. int udp_setsockopt(struct sock *sk, int level, int optname,
  1146. char __user *optval, int optlen)
  1147. {
  1148. if (level == SOL_UDP || level == SOL_UDPLITE)
  1149. return udp_lib_setsockopt(sk, level, optname, optval, optlen,
  1150. udp_push_pending_frames);
  1151. return ip_setsockopt(sk, level, optname, optval, optlen);
  1152. }
  1153. #ifdef CONFIG_COMPAT
  1154. int compat_udp_setsockopt(struct sock *sk, int level, int optname,
  1155. char __user *optval, int optlen)
  1156. {
  1157. if (level == SOL_UDP || level == SOL_UDPLITE)
  1158. return udp_lib_setsockopt(sk, level, optname, optval, optlen,
  1159. udp_push_pending_frames);
  1160. return compat_ip_setsockopt(sk, level, optname, optval, optlen);
  1161. }
  1162. #endif
  1163. int udp_lib_getsockopt(struct sock *sk, int level, int optname,
  1164. char __user *optval, int __user *optlen)
  1165. {
  1166. struct udp_sock *up = udp_sk(sk);
  1167. int val, len;
  1168. if (get_user(len,optlen))
  1169. return -EFAULT;
  1170. len = min_t(unsigned int, len, sizeof(int));
  1171. if (len < 0)
  1172. return -EINVAL;
  1173. switch (optname) {
  1174. case UDP_CORK:
  1175. val = up->corkflag;
  1176. break;
  1177. case UDP_ENCAP:
  1178. val = up->encap_type;
  1179. break;
  1180. /* The following two cannot be changed on UDP sockets, the return is
  1181. * always 0 (which corresponds to the full checksum coverage of UDP). */
  1182. case UDPLITE_SEND_CSCOV:
  1183. val = up->pcslen;
  1184. break;
  1185. case UDPLITE_RECV_CSCOV:
  1186. val = up->pcrlen;
  1187. break;
  1188. default:
  1189. return -ENOPROTOOPT;
  1190. }
  1191. if (put_user(len, optlen))
  1192. return -EFAULT;
  1193. if (copy_to_user(optval, &val,len))
  1194. return -EFAULT;
  1195. return 0;
  1196. }
  1197. int udp_getsockopt(struct sock *sk, int level, int optname,
  1198. char __user *optval, int __user *optlen)
  1199. {
  1200. if (level == SOL_UDP || level == SOL_UDPLITE)
  1201. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  1202. return ip_getsockopt(sk, level, optname, optval, optlen);
  1203. }
  1204. #ifdef CONFIG_COMPAT
  1205. int compat_udp_getsockopt(struct sock *sk, int level, int optname,
  1206. char __user *optval, int __user *optlen)
  1207. {
  1208. if (level == SOL_UDP || level == SOL_UDPLITE)
  1209. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  1210. return compat_ip_getsockopt(sk, level, optname, optval, optlen);
  1211. }
  1212. #endif
  1213. /**
  1214. * udp_poll - wait for a UDP event.
  1215. * @file - file struct
  1216. * @sock - socket
  1217. * @wait - poll table
  1218. *
  1219. * This is same as datagram poll, except for the special case of
  1220. * blocking sockets. If application is using a blocking fd
  1221. * and a packet with checksum error is in the queue;
  1222. * then it could get return from select indicating data available
  1223. * but then block when reading it. Add special case code
  1224. * to work around these arguably broken applications.
  1225. */
  1226. unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
  1227. {
  1228. unsigned int mask = datagram_poll(file, sock, wait);
  1229. struct sock *sk = sock->sk;
  1230. int is_lite = IS_UDPLITE(sk);
  1231. /* Check for false positives due to checksum errors */
  1232. if ( (mask & POLLRDNORM) &&
  1233. !(file->f_flags & O_NONBLOCK) &&
  1234. !(sk->sk_shutdown & RCV_SHUTDOWN)){
  1235. struct sk_buff_head *rcvq = &sk->sk_receive_queue;
  1236. struct sk_buff *skb;
  1237. spin_lock_bh(&rcvq->lock);
  1238. while ((skb = skb_peek(rcvq)) != NULL &&
  1239. udp_lib_checksum_complete(skb)) {
  1240. UDP_INC_STATS_BH(UDP_MIB_INERRORS, is_lite);
  1241. __skb_unlink(skb, rcvq);
  1242. kfree_skb(skb);
  1243. }
  1244. spin_unlock_bh(&rcvq->lock);
  1245. /* nothing to see, move along */
  1246. if (skb == NULL)
  1247. mask &= ~(POLLIN | POLLRDNORM);
  1248. }
  1249. return mask;
  1250. }
  1251. struct proto udp_prot = {
  1252. .name = "UDP",
  1253. .owner = THIS_MODULE,
  1254. .close = udp_lib_close,
  1255. .connect = ip4_datagram_connect,
  1256. .disconnect = udp_disconnect,
  1257. .ioctl = udp_ioctl,
  1258. .destroy = udp_destroy_sock,
  1259. .setsockopt = udp_setsockopt,
  1260. .getsockopt = udp_getsockopt,
  1261. .sendmsg = udp_sendmsg,
  1262. .recvmsg = udp_recvmsg,
  1263. .sendpage = udp_sendpage,
  1264. .backlog_rcv = udp_queue_rcv_skb,
  1265. .hash = udp_lib_hash,
  1266. .unhash = udp_lib_unhash,
  1267. .get_port = udp_v4_get_port,
  1268. .obj_size = sizeof(struct udp_sock),
  1269. #ifdef CONFIG_COMPAT
  1270. .compat_setsockopt = compat_udp_setsockopt,
  1271. .compat_getsockopt = compat_udp_getsockopt,
  1272. #endif
  1273. };
  1274. /* ------------------------------------------------------------------------ */
  1275. #ifdef CONFIG_PROC_FS
  1276. static struct sock *udp_get_first(struct seq_file *seq)
  1277. {
  1278. struct sock *sk;
  1279. struct udp_iter_state *state = seq->private;
  1280. for (state->bucket = 0; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) {
  1281. struct hlist_node *node;
  1282. sk_for_each(sk, node, state->hashtable + state->bucket) {
  1283. if (sk->sk_family == state->family)
  1284. goto found;
  1285. }
  1286. }
  1287. sk = NULL;
  1288. found:
  1289. return sk;
  1290. }
  1291. static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
  1292. {
  1293. struct udp_iter_state *state = seq->private;
  1294. do {
  1295. sk = sk_next(sk);
  1296. try_again:
  1297. ;
  1298. } while (sk && sk->sk_family != state->family);
  1299. if (!sk && ++state->bucket < UDP_HTABLE_SIZE) {
  1300. sk = sk_head(state->hashtable + state->bucket);
  1301. goto try_again;
  1302. }
  1303. return sk;
  1304. }
  1305. static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
  1306. {
  1307. struct sock *sk = udp_get_first(seq);
  1308. if (sk)
  1309. while (pos && (sk = udp_get_next(seq, sk)) != NULL)
  1310. --pos;
  1311. return pos ? NULL : sk;
  1312. }
  1313. static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
  1314. {
  1315. read_lock(&udp_hash_lock);
  1316. return *pos ? udp_get_idx(seq, *pos-1) : (void *)1;
  1317. }
  1318. static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1319. {
  1320. struct sock *sk;
  1321. if (v == (void *)1)
  1322. sk = udp_get_idx(seq, 0);
  1323. else
  1324. sk = udp_get_next(seq, v);
  1325. ++*pos;
  1326. return sk;
  1327. }
  1328. static void udp_seq_stop(struct seq_file *seq, void *v)
  1329. {
  1330. read_unlock(&udp_hash_lock);
  1331. }
  1332. static int udp_seq_open(struct inode *inode, struct file *file)
  1333. {
  1334. struct udp_seq_afinfo *afinfo = PDE(inode)->data;
  1335. struct seq_file *seq;
  1336. int rc = -ENOMEM;
  1337. struct udp_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
  1338. if (!s)
  1339. goto out;
  1340. s->family = afinfo->family;
  1341. s->hashtable = afinfo->hashtable;
  1342. s->seq_ops.start = udp_seq_start;
  1343. s->seq_ops.next = udp_seq_next;
  1344. s->seq_ops.show = afinfo->seq_show;
  1345. s->seq_ops.stop = udp_seq_stop;
  1346. rc = seq_open(file, &s->seq_ops);
  1347. if (rc)
  1348. goto out_kfree;
  1349. seq = file->private_data;
  1350. seq->private = s;
  1351. out:
  1352. return rc;
  1353. out_kfree:
  1354. kfree(s);
  1355. goto out;
  1356. }
  1357. /* ------------------------------------------------------------------------ */
  1358. int udp_proc_register(struct udp_seq_afinfo *afinfo)
  1359. {
  1360. struct proc_dir_entry *p;
  1361. int rc = 0;
  1362. if (!afinfo)
  1363. return -EINVAL;
  1364. afinfo->seq_fops->owner = afinfo->owner;
  1365. afinfo->seq_fops->open = udp_seq_open;
  1366. afinfo->seq_fops->read = seq_read;
  1367. afinfo->seq_fops->llseek = seq_lseek;
  1368. afinfo->seq_fops->release = seq_release_private;
  1369. p = proc_net_fops_create(afinfo->name, S_IRUGO, afinfo->seq_fops);
  1370. if (p)
  1371. p->data = afinfo;
  1372. else
  1373. rc = -ENOMEM;
  1374. return rc;
  1375. }
  1376. void udp_proc_unregister(struct udp_seq_afinfo *afinfo)
  1377. {
  1378. if (!afinfo)
  1379. return;
  1380. proc_net_remove(afinfo->name);
  1381. memset(afinfo->seq_fops, 0, sizeof(*afinfo->seq_fops));
  1382. }
  1383. /* ------------------------------------------------------------------------ */
  1384. static void udp4_format_sock(struct sock *sp, char *tmpbuf, int bucket)
  1385. {
  1386. struct inet_sock *inet = inet_sk(sp);
  1387. __be32 dest = inet->daddr;
  1388. __be32 src = inet->rcv_saddr;
  1389. __u16 destp = ntohs(inet->dport);
  1390. __u16 srcp = ntohs(inet->sport);
  1391. sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
  1392. " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p",
  1393. bucket, src, srcp, dest, destp, sp->sk_state,
  1394. atomic_read(&sp->sk_wmem_alloc),
  1395. atomic_read(&sp->sk_rmem_alloc),
  1396. 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
  1397. atomic_read(&sp->sk_refcnt), sp);
  1398. }
  1399. int udp4_seq_show(struct seq_file *seq, void *v)
  1400. {
  1401. if (v == SEQ_START_TOKEN)
  1402. seq_printf(seq, "%-127s\n",
  1403. " sl local_address rem_address st tx_queue "
  1404. "rx_queue tr tm->when retrnsmt uid timeout "
  1405. "inode");
  1406. else {
  1407. char tmpbuf[129];
  1408. struct udp_iter_state *state = seq->private;
  1409. udp4_format_sock(v, tmpbuf, state->bucket);
  1410. seq_printf(seq, "%-127s\n", tmpbuf);
  1411. }
  1412. return 0;
  1413. }
  1414. /* ------------------------------------------------------------------------ */
  1415. static struct file_operations udp4_seq_fops;
  1416. static struct udp_seq_afinfo udp4_seq_afinfo = {
  1417. .owner = THIS_MODULE,
  1418. .name = "udp",
  1419. .family = AF_INET,
  1420. .hashtable = udp_hash,
  1421. .seq_show = udp4_seq_show,
  1422. .seq_fops = &udp4_seq_fops,
  1423. };
  1424. int __init udp4_proc_init(void)
  1425. {
  1426. return udp_proc_register(&udp4_seq_afinfo);
  1427. }
  1428. void udp4_proc_exit(void)
  1429. {
  1430. udp_proc_unregister(&udp4_seq_afinfo);
  1431. }
  1432. #endif /* CONFIG_PROC_FS */
  1433. EXPORT_SYMBOL(udp_disconnect);
  1434. EXPORT_SYMBOL(udp_hash);
  1435. EXPORT_SYMBOL(udp_hash_lock);
  1436. EXPORT_SYMBOL(udp_ioctl);
  1437. EXPORT_SYMBOL(udp_get_port);
  1438. EXPORT_SYMBOL(udp_prot);
  1439. EXPORT_SYMBOL(udp_sendmsg);
  1440. EXPORT_SYMBOL(udp_lib_getsockopt);
  1441. EXPORT_SYMBOL(udp_lib_setsockopt);
  1442. EXPORT_SYMBOL(udp_poll);
  1443. #ifdef CONFIG_PROC_FS
  1444. EXPORT_SYMBOL(udp_proc_register);
  1445. EXPORT_SYMBOL(udp_proc_unregister);
  1446. #endif