ip_options.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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 options processing module for ip.c
  7. *
  8. * Version: $Id: ip_options.c,v 1.21 2001/09/01 00:31:50 davem Exp $
  9. *
  10. * Authors: A.N.Kuznetsov
  11. *
  12. */
  13. #include <linux/capability.h>
  14. #include <linux/module.h>
  15. #include <linux/types.h>
  16. #include <asm/uaccess.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/ip.h>
  19. #include <linux/icmp.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/rtnetlink.h>
  22. #include <net/sock.h>
  23. #include <net/ip.h>
  24. #include <net/icmp.h>
  25. #include <net/route.h>
  26. /*
  27. * Write options to IP header, record destination address to
  28. * source route option, address of outgoing interface
  29. * (we should already know it, so that this function is allowed be
  30. * called only after routing decision) and timestamp,
  31. * if we originate this datagram.
  32. *
  33. * daddr is real destination address, next hop is recorded in IP header.
  34. * saddr is address of outgoing interface.
  35. */
  36. void ip_options_build(struct sk_buff * skb, struct ip_options * opt,
  37. u32 daddr, struct rtable *rt, int is_frag)
  38. {
  39. unsigned char * iph = skb->nh.raw;
  40. memcpy(&(IPCB(skb)->opt), opt, sizeof(struct ip_options));
  41. memcpy(iph+sizeof(struct iphdr), opt->__data, opt->optlen);
  42. opt = &(IPCB(skb)->opt);
  43. opt->is_data = 0;
  44. if (opt->srr)
  45. memcpy(iph+opt->srr+iph[opt->srr+1]-4, &daddr, 4);
  46. if (!is_frag) {
  47. if (opt->rr_needaddr)
  48. ip_rt_get_source(iph+opt->rr+iph[opt->rr+2]-5, rt);
  49. if (opt->ts_needaddr)
  50. ip_rt_get_source(iph+opt->ts+iph[opt->ts+2]-9, rt);
  51. if (opt->ts_needtime) {
  52. struct timeval tv;
  53. __u32 midtime;
  54. do_gettimeofday(&tv);
  55. midtime = htonl((tv.tv_sec % 86400) * 1000 + tv.tv_usec / 1000);
  56. memcpy(iph+opt->ts+iph[opt->ts+2]-5, &midtime, 4);
  57. }
  58. return;
  59. }
  60. if (opt->rr) {
  61. memset(iph+opt->rr, IPOPT_NOP, iph[opt->rr+1]);
  62. opt->rr = 0;
  63. opt->rr_needaddr = 0;
  64. }
  65. if (opt->ts) {
  66. memset(iph+opt->ts, IPOPT_NOP, iph[opt->ts+1]);
  67. opt->ts = 0;
  68. opt->ts_needaddr = opt->ts_needtime = 0;
  69. }
  70. }
  71. /*
  72. * Provided (sopt, skb) points to received options,
  73. * build in dopt compiled option set appropriate for answering.
  74. * i.e. invert SRR option, copy anothers,
  75. * and grab room in RR/TS options.
  76. *
  77. * NOTE: dopt cannot point to skb.
  78. */
  79. int ip_options_echo(struct ip_options * dopt, struct sk_buff * skb)
  80. {
  81. struct ip_options *sopt;
  82. unsigned char *sptr, *dptr;
  83. int soffset, doffset;
  84. int optlen;
  85. u32 daddr;
  86. memset(dopt, 0, sizeof(struct ip_options));
  87. dopt->is_data = 1;
  88. sopt = &(IPCB(skb)->opt);
  89. if (sopt->optlen == 0) {
  90. dopt->optlen = 0;
  91. return 0;
  92. }
  93. sptr = skb->nh.raw;
  94. dptr = dopt->__data;
  95. if (skb->dst)
  96. daddr = ((struct rtable*)skb->dst)->rt_spec_dst;
  97. else
  98. daddr = skb->nh.iph->daddr;
  99. if (sopt->rr) {
  100. optlen = sptr[sopt->rr+1];
  101. soffset = sptr[sopt->rr+2];
  102. dopt->rr = dopt->optlen + sizeof(struct iphdr);
  103. memcpy(dptr, sptr+sopt->rr, optlen);
  104. if (sopt->rr_needaddr && soffset <= optlen) {
  105. if (soffset + 3 > optlen)
  106. return -EINVAL;
  107. dptr[2] = soffset + 4;
  108. dopt->rr_needaddr = 1;
  109. }
  110. dptr += optlen;
  111. dopt->optlen += optlen;
  112. }
  113. if (sopt->ts) {
  114. optlen = sptr[sopt->ts+1];
  115. soffset = sptr[sopt->ts+2];
  116. dopt->ts = dopt->optlen + sizeof(struct iphdr);
  117. memcpy(dptr, sptr+sopt->ts, optlen);
  118. if (soffset <= optlen) {
  119. if (sopt->ts_needaddr) {
  120. if (soffset + 3 > optlen)
  121. return -EINVAL;
  122. dopt->ts_needaddr = 1;
  123. soffset += 4;
  124. }
  125. if (sopt->ts_needtime) {
  126. if (soffset + 3 > optlen)
  127. return -EINVAL;
  128. if ((dptr[3]&0xF) != IPOPT_TS_PRESPEC) {
  129. dopt->ts_needtime = 1;
  130. soffset += 4;
  131. } else {
  132. dopt->ts_needtime = 0;
  133. if (soffset + 8 <= optlen) {
  134. __u32 addr;
  135. memcpy(&addr, sptr+soffset-1, 4);
  136. if (inet_addr_type(addr) != RTN_LOCAL) {
  137. dopt->ts_needtime = 1;
  138. soffset += 8;
  139. }
  140. }
  141. }
  142. }
  143. dptr[2] = soffset;
  144. }
  145. dptr += optlen;
  146. dopt->optlen += optlen;
  147. }
  148. if (sopt->srr) {
  149. unsigned char * start = sptr+sopt->srr;
  150. u32 faddr;
  151. optlen = start[1];
  152. soffset = start[2];
  153. doffset = 0;
  154. if (soffset > optlen)
  155. soffset = optlen + 1;
  156. soffset -= 4;
  157. if (soffset > 3) {
  158. memcpy(&faddr, &start[soffset-1], 4);
  159. for (soffset-=4, doffset=4; soffset > 3; soffset-=4, doffset+=4)
  160. memcpy(&dptr[doffset-1], &start[soffset-1], 4);
  161. /*
  162. * RFC1812 requires to fix illegal source routes.
  163. */
  164. if (memcmp(&skb->nh.iph->saddr, &start[soffset+3], 4) == 0)
  165. doffset -= 4;
  166. }
  167. if (doffset > 3) {
  168. memcpy(&start[doffset-1], &daddr, 4);
  169. dopt->faddr = faddr;
  170. dptr[0] = start[0];
  171. dptr[1] = doffset+3;
  172. dptr[2] = 4;
  173. dptr += doffset+3;
  174. dopt->srr = dopt->optlen + sizeof(struct iphdr);
  175. dopt->optlen += doffset+3;
  176. dopt->is_strictroute = sopt->is_strictroute;
  177. }
  178. }
  179. while (dopt->optlen & 3) {
  180. *dptr++ = IPOPT_END;
  181. dopt->optlen++;
  182. }
  183. return 0;
  184. }
  185. /*
  186. * Options "fragmenting", just fill options not
  187. * allowed in fragments with NOOPs.
  188. * Simple and stupid 8), but the most efficient way.
  189. */
  190. void ip_options_fragment(struct sk_buff * skb)
  191. {
  192. unsigned char * optptr = skb->nh.raw;
  193. struct ip_options * opt = &(IPCB(skb)->opt);
  194. int l = opt->optlen;
  195. int optlen;
  196. while (l > 0) {
  197. switch (*optptr) {
  198. case IPOPT_END:
  199. return;
  200. case IPOPT_NOOP:
  201. l--;
  202. optptr++;
  203. continue;
  204. }
  205. optlen = optptr[1];
  206. if (optlen<2 || optlen>l)
  207. return;
  208. if (!IPOPT_COPIED(*optptr))
  209. memset(optptr, IPOPT_NOOP, optlen);
  210. l -= optlen;
  211. optptr += optlen;
  212. }
  213. opt->ts = 0;
  214. opt->rr = 0;
  215. opt->rr_needaddr = 0;
  216. opt->ts_needaddr = 0;
  217. opt->ts_needtime = 0;
  218. return;
  219. }
  220. /*
  221. * Verify options and fill pointers in struct options.
  222. * Caller should clear *opt, and set opt->data.
  223. * If opt == NULL, then skb->data should point to IP header.
  224. */
  225. int ip_options_compile(struct ip_options * opt, struct sk_buff * skb)
  226. {
  227. int l;
  228. unsigned char * iph;
  229. unsigned char * optptr;
  230. int optlen;
  231. unsigned char * pp_ptr = NULL;
  232. struct rtable *rt = skb ? (struct rtable*)skb->dst : NULL;
  233. if (!opt) {
  234. opt = &(IPCB(skb)->opt);
  235. memset(opt, 0, sizeof(struct ip_options));
  236. iph = skb->nh.raw;
  237. opt->optlen = ((struct iphdr *)iph)->ihl*4 - sizeof(struct iphdr);
  238. optptr = iph + sizeof(struct iphdr);
  239. opt->is_data = 0;
  240. } else {
  241. optptr = opt->is_data ? opt->__data : (unsigned char*)&(skb->nh.iph[1]);
  242. iph = optptr - sizeof(struct iphdr);
  243. }
  244. for (l = opt->optlen; l > 0; ) {
  245. switch (*optptr) {
  246. case IPOPT_END:
  247. for (optptr++, l--; l>0; optptr++, l--) {
  248. if (*optptr != IPOPT_END) {
  249. *optptr = IPOPT_END;
  250. opt->is_changed = 1;
  251. }
  252. }
  253. goto eol;
  254. case IPOPT_NOOP:
  255. l--;
  256. optptr++;
  257. continue;
  258. }
  259. optlen = optptr[1];
  260. if (optlen<2 || optlen>l) {
  261. pp_ptr = optptr;
  262. goto error;
  263. }
  264. switch (*optptr) {
  265. case IPOPT_SSRR:
  266. case IPOPT_LSRR:
  267. if (optlen < 3) {
  268. pp_ptr = optptr + 1;
  269. goto error;
  270. }
  271. if (optptr[2] < 4) {
  272. pp_ptr = optptr + 2;
  273. goto error;
  274. }
  275. /* NB: cf RFC-1812 5.2.4.1 */
  276. if (opt->srr) {
  277. pp_ptr = optptr;
  278. goto error;
  279. }
  280. if (!skb) {
  281. if (optptr[2] != 4 || optlen < 7 || ((optlen-3) & 3)) {
  282. pp_ptr = optptr + 1;
  283. goto error;
  284. }
  285. memcpy(&opt->faddr, &optptr[3], 4);
  286. if (optlen > 7)
  287. memmove(&optptr[3], &optptr[7], optlen-7);
  288. }
  289. opt->is_strictroute = (optptr[0] == IPOPT_SSRR);
  290. opt->srr = optptr - iph;
  291. break;
  292. case IPOPT_RR:
  293. if (opt->rr) {
  294. pp_ptr = optptr;
  295. goto error;
  296. }
  297. if (optlen < 3) {
  298. pp_ptr = optptr + 1;
  299. goto error;
  300. }
  301. if (optptr[2] < 4) {
  302. pp_ptr = optptr + 2;
  303. goto error;
  304. }
  305. if (optptr[2] <= optlen) {
  306. if (optptr[2]+3 > optlen) {
  307. pp_ptr = optptr + 2;
  308. goto error;
  309. }
  310. if (skb) {
  311. memcpy(&optptr[optptr[2]-1], &rt->rt_spec_dst, 4);
  312. opt->is_changed = 1;
  313. }
  314. optptr[2] += 4;
  315. opt->rr_needaddr = 1;
  316. }
  317. opt->rr = optptr - iph;
  318. break;
  319. case IPOPT_TIMESTAMP:
  320. if (opt->ts) {
  321. pp_ptr = optptr;
  322. goto error;
  323. }
  324. if (optlen < 4) {
  325. pp_ptr = optptr + 1;
  326. goto error;
  327. }
  328. if (optptr[2] < 5) {
  329. pp_ptr = optptr + 2;
  330. goto error;
  331. }
  332. if (optptr[2] <= optlen) {
  333. __u32 * timeptr = NULL;
  334. if (optptr[2]+3 > optptr[1]) {
  335. pp_ptr = optptr + 2;
  336. goto error;
  337. }
  338. switch (optptr[3]&0xF) {
  339. case IPOPT_TS_TSONLY:
  340. opt->ts = optptr - iph;
  341. if (skb)
  342. timeptr = (__u32*)&optptr[optptr[2]-1];
  343. opt->ts_needtime = 1;
  344. optptr[2] += 4;
  345. break;
  346. case IPOPT_TS_TSANDADDR:
  347. if (optptr[2]+7 > optptr[1]) {
  348. pp_ptr = optptr + 2;
  349. goto error;
  350. }
  351. opt->ts = optptr - iph;
  352. if (skb) {
  353. memcpy(&optptr[optptr[2]-1], &rt->rt_spec_dst, 4);
  354. timeptr = (__u32*)&optptr[optptr[2]+3];
  355. }
  356. opt->ts_needaddr = 1;
  357. opt->ts_needtime = 1;
  358. optptr[2] += 8;
  359. break;
  360. case IPOPT_TS_PRESPEC:
  361. if (optptr[2]+7 > optptr[1]) {
  362. pp_ptr = optptr + 2;
  363. goto error;
  364. }
  365. opt->ts = optptr - iph;
  366. {
  367. u32 addr;
  368. memcpy(&addr, &optptr[optptr[2]-1], 4);
  369. if (inet_addr_type(addr) == RTN_UNICAST)
  370. break;
  371. if (skb)
  372. timeptr = (__u32*)&optptr[optptr[2]+3];
  373. }
  374. opt->ts_needtime = 1;
  375. optptr[2] += 8;
  376. break;
  377. default:
  378. if (!skb && !capable(CAP_NET_RAW)) {
  379. pp_ptr = optptr + 3;
  380. goto error;
  381. }
  382. break;
  383. }
  384. if (timeptr) {
  385. struct timeval tv;
  386. __u32 midtime;
  387. do_gettimeofday(&tv);
  388. midtime = htonl((tv.tv_sec % 86400) * 1000 + tv.tv_usec / 1000);
  389. memcpy(timeptr, &midtime, sizeof(__u32));
  390. opt->is_changed = 1;
  391. }
  392. } else {
  393. unsigned overflow = optptr[3]>>4;
  394. if (overflow == 15) {
  395. pp_ptr = optptr + 3;
  396. goto error;
  397. }
  398. opt->ts = optptr - iph;
  399. if (skb) {
  400. optptr[3] = (optptr[3]&0xF)|((overflow+1)<<4);
  401. opt->is_changed = 1;
  402. }
  403. }
  404. break;
  405. case IPOPT_RA:
  406. if (optlen < 4) {
  407. pp_ptr = optptr + 1;
  408. goto error;
  409. }
  410. if (optptr[2] == 0 && optptr[3] == 0)
  411. opt->router_alert = optptr - iph;
  412. break;
  413. case IPOPT_SEC:
  414. case IPOPT_SID:
  415. default:
  416. if (!skb && !capable(CAP_NET_RAW)) {
  417. pp_ptr = optptr;
  418. goto error;
  419. }
  420. break;
  421. }
  422. l -= optlen;
  423. optptr += optlen;
  424. }
  425. eol:
  426. if (!pp_ptr)
  427. return 0;
  428. error:
  429. if (skb) {
  430. icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl((pp_ptr-iph)<<24));
  431. }
  432. return -EINVAL;
  433. }
  434. /*
  435. * Undo all the changes done by ip_options_compile().
  436. */
  437. void ip_options_undo(struct ip_options * opt)
  438. {
  439. if (opt->srr) {
  440. unsigned char * optptr = opt->__data+opt->srr-sizeof(struct iphdr);
  441. memmove(optptr+7, optptr+3, optptr[1]-7);
  442. memcpy(optptr+3, &opt->faddr, 4);
  443. }
  444. if (opt->rr_needaddr) {
  445. unsigned char * optptr = opt->__data+opt->rr-sizeof(struct iphdr);
  446. optptr[2] -= 4;
  447. memset(&optptr[optptr[2]-1], 0, 4);
  448. }
  449. if (opt->ts) {
  450. unsigned char * optptr = opt->__data+opt->ts-sizeof(struct iphdr);
  451. if (opt->ts_needtime) {
  452. optptr[2] -= 4;
  453. memset(&optptr[optptr[2]-1], 0, 4);
  454. if ((optptr[3]&0xF) == IPOPT_TS_PRESPEC)
  455. optptr[2] -= 4;
  456. }
  457. if (opt->ts_needaddr) {
  458. optptr[2] -= 4;
  459. memset(&optptr[optptr[2]-1], 0, 4);
  460. }
  461. }
  462. }
  463. static struct ip_options *ip_options_get_alloc(const int optlen)
  464. {
  465. struct ip_options *opt = kmalloc(sizeof(*opt) + ((optlen + 3) & ~3),
  466. GFP_KERNEL);
  467. if (opt)
  468. memset(opt, 0, sizeof(*opt));
  469. return opt;
  470. }
  471. static int ip_options_get_finish(struct ip_options **optp,
  472. struct ip_options *opt, int optlen)
  473. {
  474. while (optlen & 3)
  475. opt->__data[optlen++] = IPOPT_END;
  476. opt->optlen = optlen;
  477. opt->is_data = 1;
  478. opt->is_setbyuser = 1;
  479. if (optlen && ip_options_compile(opt, NULL)) {
  480. kfree(opt);
  481. return -EINVAL;
  482. }
  483. kfree(*optp);
  484. *optp = opt;
  485. return 0;
  486. }
  487. int ip_options_get_from_user(struct ip_options **optp, unsigned char __user *data, int optlen)
  488. {
  489. struct ip_options *opt = ip_options_get_alloc(optlen);
  490. if (!opt)
  491. return -ENOMEM;
  492. if (optlen && copy_from_user(opt->__data, data, optlen)) {
  493. kfree(opt);
  494. return -EFAULT;
  495. }
  496. return ip_options_get_finish(optp, opt, optlen);
  497. }
  498. int ip_options_get(struct ip_options **optp, unsigned char *data, int optlen)
  499. {
  500. struct ip_options *opt = ip_options_get_alloc(optlen);
  501. if (!opt)
  502. return -ENOMEM;
  503. if (optlen)
  504. memcpy(opt->__data, data, optlen);
  505. return ip_options_get_finish(optp, opt, optlen);
  506. }
  507. void ip_forward_options(struct sk_buff *skb)
  508. {
  509. struct ip_options * opt = &(IPCB(skb)->opt);
  510. unsigned char * optptr;
  511. struct rtable *rt = (struct rtable*)skb->dst;
  512. unsigned char *raw = skb->nh.raw;
  513. if (opt->rr_needaddr) {
  514. optptr = (unsigned char *)raw + opt->rr;
  515. ip_rt_get_source(&optptr[optptr[2]-5], rt);
  516. opt->is_changed = 1;
  517. }
  518. if (opt->srr_is_hit) {
  519. int srrptr, srrspace;
  520. optptr = raw + opt->srr;
  521. for ( srrptr=optptr[2], srrspace = optptr[1];
  522. srrptr <= srrspace;
  523. srrptr += 4
  524. ) {
  525. if (srrptr + 3 > srrspace)
  526. break;
  527. if (memcmp(&rt->rt_dst, &optptr[srrptr-1], 4) == 0)
  528. break;
  529. }
  530. if (srrptr + 3 <= srrspace) {
  531. opt->is_changed = 1;
  532. ip_rt_get_source(&optptr[srrptr-1], rt);
  533. skb->nh.iph->daddr = rt->rt_dst;
  534. optptr[2] = srrptr+4;
  535. } else if (net_ratelimit())
  536. printk(KERN_CRIT "ip_forward(): Argh! Destination lost!\n");
  537. if (opt->ts_needaddr) {
  538. optptr = raw + opt->ts;
  539. ip_rt_get_source(&optptr[optptr[2]-9], rt);
  540. opt->is_changed = 1;
  541. }
  542. }
  543. if (opt->is_changed) {
  544. opt->is_changed = 0;
  545. ip_send_check(skb->nh.iph);
  546. }
  547. }
  548. int ip_options_rcv_srr(struct sk_buff *skb)
  549. {
  550. struct ip_options *opt = &(IPCB(skb)->opt);
  551. int srrspace, srrptr;
  552. u32 nexthop;
  553. struct iphdr *iph = skb->nh.iph;
  554. unsigned char * optptr = skb->nh.raw + opt->srr;
  555. struct rtable *rt = (struct rtable*)skb->dst;
  556. struct rtable *rt2;
  557. int err;
  558. if (!opt->srr)
  559. return 0;
  560. if (skb->pkt_type != PACKET_HOST)
  561. return -EINVAL;
  562. if (rt->rt_type == RTN_UNICAST) {
  563. if (!opt->is_strictroute)
  564. return 0;
  565. icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl(16<<24));
  566. return -EINVAL;
  567. }
  568. if (rt->rt_type != RTN_LOCAL)
  569. return -EINVAL;
  570. for (srrptr=optptr[2], srrspace = optptr[1]; srrptr <= srrspace; srrptr += 4) {
  571. if (srrptr + 3 > srrspace) {
  572. icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl((opt->srr+2)<<24));
  573. return -EINVAL;
  574. }
  575. memcpy(&nexthop, &optptr[srrptr-1], 4);
  576. rt = (struct rtable*)skb->dst;
  577. skb->dst = NULL;
  578. err = ip_route_input(skb, nexthop, iph->saddr, iph->tos, skb->dev);
  579. rt2 = (struct rtable*)skb->dst;
  580. if (err || (rt2->rt_type != RTN_UNICAST && rt2->rt_type != RTN_LOCAL)) {
  581. ip_rt_put(rt2);
  582. skb->dst = &rt->u.dst;
  583. return -EINVAL;
  584. }
  585. ip_rt_put(rt);
  586. if (rt2->rt_type != RTN_LOCAL)
  587. break;
  588. /* Superfast 8) loopback forward */
  589. memcpy(&iph->daddr, &optptr[srrptr-1], 4);
  590. opt->is_changed = 1;
  591. }
  592. if (srrptr <= srrspace) {
  593. opt->srr_is_hit = 1;
  594. opt->is_changed = 1;
  595. }
  596. return 0;
  597. }