udp.c 41 KB

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