2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * rx_knet.c - RX kernel send, receive and timer routines.
13 * Linux implementation.
15 #include <afsconfig.h>
16 #include "afs/param.h"
21 #include <linux/version.h>
22 #ifdef AFS_LINUX22_ENV
23 #include "rx/rx_kcommon.h"
24 #if defined(AFS_LINUX24_ENV)
25 #include "h/smp_lock.h"
27 #include <asm/uaccess.h>
29 #include <linux/errqueue.h>
30 #include <linux/icmp.h>
34 * open and bind RX socket
37 rxk_NewSocketHost(afs_uint32 ahost, short aport)
40 struct sockaddr_in myaddr;
44 int pmtu = IP_PMTUDISC_WANT;
47 int pmtu = IP_PMTUDISC_DONT;
50 /* We need a better test for this. if you need it back, tell us
53 #ifdef LINUX_KERNEL_SOCK_CREATE_V
54 code = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sockp, 0);
56 code = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sockp);
62 myaddr.sin_family = AF_INET;
63 myaddr.sin_addr.s_addr = ahost;
64 myaddr.sin_port = aport;
66 sockp->ops->bind(sockp, (struct sockaddr *)&myaddr, sizeof(myaddr));
69 #if defined(AFS_LINUX24_ENV)
70 printk("sock_release(rx_socket) FIXME\n");
78 sockp->ops->setsockopt(sockp, SOL_IP, IP_MTU_DISCOVER, (char *)&pmtu,
81 sockp->ops->setsockopt(sockp, SOL_IP, IP_RECVERR, (char *)&do_recverr,
85 return (osi_socket *)sockp;
89 rxk_NewSocket(short aport)
91 return rxk_NewSocketHost(htonl(INADDR_ANY), aport);
94 /* free socket allocated by osi_NetSocket */
96 rxk_FreeSocket(register struct socket *asocket)
98 AFS_STATCNT(osi_FreeSocket);
104 handle_socket_error(osi_socket so)
108 struct cmsghdr *cmsg;
109 struct sock_extended_err *err;
110 struct sockaddr_in addr;
111 struct sockaddr *offender;
114 struct socket *sop = (struct socket *)so;
116 if (!(controlmsgbuf=rxi_Alloc(256)))
118 msg.msg_name = &addr;
119 msg.msg_namelen = sizeof(addr);
122 msg.msg_control = controlmsgbuf;
123 msg.msg_controllen = 256;
127 code = sock_recvmsg(sop, &msg, 256, MSG_ERRQUEUE|MSG_DONTWAIT|MSG_TRUNC);
130 if (code < 0 || !(msg.msg_flags & MSG_ERRQUEUE))
133 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
134 if (CMSG_OK(&msg, cmsg) && cmsg->cmsg_level == SOL_IP &&
135 cmsg->cmsg_type == IP_RECVERR)
140 err = CMSG_DATA(cmsg);
141 offender = SO_EE_OFFENDER(err);
143 if (offender->sa_family != AF_INET)
146 memcpy(&addr, offender, sizeof(addr));
148 if (err->ee_origin == SO_EE_ORIGIN_ICMP &&
149 err->ee_type == ICMP_DEST_UNREACH &&
150 err->ee_code == ICMP_FRAG_NEEDED) {
151 rxi_SetPeerMtu(ntohl(addr.sin_addr.s_addr), ntohs(addr.sin_port),
154 /* other DEST_UNREACH's and TIME_EXCEEDED should be dealt with too */
157 rxi_Free(controlmsgbuf, 256);
169 osi_NetSend(osi_socket sop, struct sockaddr_in *to, struct iovec *iovec,
170 int iovcnt, afs_int32 size, int istack)
180 esize = sizeof(sockerr);
182 sop->ops->getsockopt(sop, SOL_SOCKET, SO_ERROR, (char *)&sockerr,
187 handle_socket_error(sop);
191 msg.msg_iovlen = iovcnt;
194 msg.msg_namelen = sizeof(*to);
195 msg.msg_control = NULL;
196 msg.msg_controllen = 0;
200 code = sock_sendmsg(sop, &msg, size);
202 return (code < 0) ? code : 0;
207 * OS dependent part of kernel RX listener thread.
210 * so socket to receive on, typically rx_socket
211 * from pointer to a sockaddr_in.
212 * iov array of iovecs to fill in.
213 * iovcnt how many iovecs there are.
214 * lengthp IN/OUT in: total space available in iovecs. out: size of read.
218 * error code (such as EINTER) if not
221 * Note that the maximum number of iovecs is 2 + RX_MAXWVECS. This is
222 * so we have a little space to look for packets larger than
225 int rxk_lastSocketError;
226 int rxk_nSocketErrors;
228 osi_NetReceive(osi_socket so, struct sockaddr_in *from, struct iovec *iov,
229 int iovcnt, int *lengthp)
235 struct iovec tmpvec[RX_MAXWVECS + 2];
236 struct socket *sop = (struct socket *)so;
238 if (iovcnt > RX_MAXWVECS + 2) {
239 osi_Panic("Too many (%d) iovecs passed to osi_NetReceive\n", iovcnt);
244 esize = sizeof(sockerr);
246 sop->ops->getsockopt(sop, SOL_SOCKET, SO_ERROR, (char *)&sockerr,
251 handle_socket_error(so);
254 memcpy(tmpvec, iov, iovcnt * sizeof(struct iovec));
256 msg.msg_iov = tmpvec;
257 msg.msg_iovlen = iovcnt;
258 msg.msg_control = NULL;
259 msg.msg_controllen = 0;
263 code = sock_recvmsg(sop, &msg, *lengthp, 0);
267 #ifdef AFS_LINUX26_ENV
271 current->flags & PF_FREEZE
273 #if defined(STRUCT_TASK_STRUCT_HAS_TODO)
276 #if defined(STRUCT_TASK_STRUCT_HAS_THREAD_INFO)
277 test_ti_thread_flag(current->thread_info, TIF_FREEZE)
279 test_ti_thread_flag(task_thread_info(current), TIF_FREEZE)
284 #ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
285 refrigerator(PF_FREEZE);
289 set_current_state(TASK_INTERRUPTIBLE);
293 /* Clear the error before using the socket again.
294 * Oh joy, Linux has hidden header files as well. It appears we can
295 * simply call again and have it clear itself via sock_error().
297 #ifdef AFS_LINUX22_ENV
298 flush_signals(current); /* We don't want no stinkin' signals. */
300 current->signal = 0; /* We don't want no stinkin' signals. */
302 rxk_lastSocketError = code;
311 #ifdef EXPORTED_TASKLIST_LOCK
312 extern rwlock_t tasklist_lock __attribute__((weak));
315 osi_StopListener(void)
317 extern struct task_struct *rxk_ListenerTask;
319 while (rxk_ListenerTask) {
320 if (rxk_ListenerTask) {
321 flush_signals(rxk_ListenerTask);
322 force_sig(SIGKILL, rxk_ListenerTask);
324 if (!rxk_ListenerTask)
326 afs_osi_Sleep(&rxk_ListenerTask);
328 sock_release(rx_socket);
332 #endif /* AFS_LINUX22_ENV */