udp.c 43 KB

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