# Recalculate udp checksum



## yavuzg (Jan 26, 2010)

Hi all,

I want to re-calculate checksum of an udp packet,
When I grep freebsd code I found below line:


```
(/usr/src/sys/netinet/udp_usrreq.c)

uh_sum = in_cksum(m, len + sizeof (struct ip));
```

When try to use above method in my userland code I got linker error:

```
yavuzg# gcc -o out divert_out.c
/var/tmp//cczSfAd4.o(.text+0x266): In function `main':
: undefined reference to `in_cksum_skip'
```

How can I use in_cksum method in my userland code?

thanks in advance,
yavuz


----------



## DutchDaemon (Jan 26, 2010)

That reference is in ip_output.c (same directory).


----------



## yavuzg (Jan 26, 2010)

you mean this line?


```
csum = in_cksum_skip(m, ip->ip_len, offset);
```

this is an example usage of the method but my problem is different. I can not compile my code if I use below piece of code


```
uh_sum = in_cksum(m, len + sizeof (struct ip));
```


```
yavuzg# gcc -o out divert_out.c
/var/tmp//cczSfAd4.o(.text+0x266): In function `main':
: undefined reference to `in_cksum_skip'
```


----------



## gordon@ (Jan 27, 2010)

You can't recalculate a UDP checksum in userland (I don't believe). What are you trying to do?


----------



## yavuzg (Jan 27, 2010)

I am trying to change udp packet payload via a divert socket.


----------



## expl (Jan 27, 2010)

yavuzg said:
			
		

> When try to use above method in my userland code I got linker error:
> 
> ```
> yavuzg# gcc -o out divert_out.c
> ...



in_cksum_skip is a kernel function (located in sys/i386/i386/in_cksum.c) not part of libc. What you want to do is port it to your userland app then link together.

Btw these in_cksum implementations are optimized for specific platforms so porting this to userland and staying portable might be bit dirty work.


----------

