udp.c 43 KB

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