root/net/inet/protocol.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. inet_get_protocol
  2. inet_add_protocol
  3. inet_del_protocol

   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  *              INET protocol dispatch tables.
   7  *
   8  * Version:     @(#)protocol.c  1.0.5   05/25/93
   9  *
  10  * Authors:     Ross Biro, <bir7@leland.Stanford.Edu>
  11  *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12  *
  13  *              This program is free software; you can redistribute it and/or
  14  *              modify it under the terms of the GNU General Public License
  15  *              as published by the Free Software Foundation; either version
  16  *              2 of the License, or (at your option) any later version.
  17  */
  18 #include <asm/segment.h>
  19 #include <asm/system.h>
  20 #include <linux/types.h>
  21 #include <linux/kernel.h>
  22 #include <linux/sched.h>
  23 #include <linux/string.h>
  24 #include <linux/socket.h>
  25 #include <linux/in.h>
  26 #include "inet.h"
  27 #include "timer.h"
  28 #include "dev.h"
  29 #include "ip.h"
  30 #include "protocol.h"
  31 #include "tcp.h"
  32 #include "skbuff.h"
  33 #include "sock.h"
  34 #include "icmp.h"
  35 #include "udp.h"
  36 
  37 
  38 static struct inet_protocol tcp_protocol = {
  39   tcp_rcv,              /* TCP handler          */
  40   tcp_err,              /* TCP error control    */
  41   NULL,                 /* next                 */
  42   IPPROTO_TCP,          /* protocol ID          */
  43   0,                    /* copy                 */
  44   NULL                  /* data                 */
  45 };
  46 
  47 
  48 static struct inet_protocol udp_protocol = {
  49   udp_rcv,              /* UDP handler          */
  50   NULL,                 /* UDP error control    */
  51   &tcp_protocol,        /* next                 */
  52   IPPROTO_UDP,          /* protocol ID          */
  53   0,                    /* copy                 */
  54   NULL                  /* data                 */
  55 };
  56 
  57 
  58 static struct inet_protocol icmp_protocol = {
  59   icmp_rcv,             /* ICMP handler         */
  60   NULL,                 /* ICMP error control   */
  61   &udp_protocol,        /* next                 */
  62   IPPROTO_ICMP,         /* protocol ID          */
  63   0,                    /* copy                 */
  64   NULL                  /* data                 */
  65 };
  66 
  67 
  68 struct inet_protocol *inet_protocol_base = &icmp_protocol;
  69 struct inet_protocol *inet_protos[MAX_INET_PROTOS] = {
  70   NULL
  71 };
  72 
  73 
  74 struct inet_protocol *
  75 inet_get_protocol(unsigned char prot)
     /* [previous][next][first][last][top][bottom][index][help] */
  76 {
  77   unsigned char hash;
  78   struct inet_protocol *p;
  79 
  80   DPRINTF((DBG_PROTO, "get_protocol (%d)\n ", prot));
  81   hash = prot & (MAX_INET_PROTOS - 1);
  82   for (p = inet_protos[hash] ; p != NULL; p=p->next) {
  83         DPRINTF((DBG_PROTO, "trying protocol %d\n", p->protocol));
  84         if (p->protocol == prot) return((struct inet_protocol *) p);
  85   }
  86   return(NULL);
  87 }
  88 
  89 
  90 void
  91 inet_add_protocol(struct inet_protocol *prot)
     /* [previous][next][first][last][top][bottom][index][help] */
  92 {
  93   unsigned char hash;
  94   struct inet_protocol *p2;
  95 
  96   hash = prot->protocol & (MAX_INET_PROTOS - 1);
  97   prot ->next = inet_protos[hash];
  98   inet_protos[hash] = prot;
  99   prot->copy = 0;
 100 
 101   /* Set the copy bit if we need to. */
 102   p2 = (struct inet_protocol *) prot->next;
 103   while(p2 != NULL) {
 104         if (p2->protocol == prot->protocol) {
 105                 prot->copy = 1;
 106                 break;
 107         }
 108         p2 = (struct inet_protocol *) prot->next;
 109   }
 110 }
 111 
 112 
 113 int
 114 inet_del_protocol(struct inet_protocol *prot)
     /* [previous][next][first][last][top][bottom][index][help] */
 115 {
 116   struct inet_protocol *p;
 117   struct inet_protocol *lp = NULL;
 118   unsigned char hash;
 119 
 120   hash = prot->protocol & (MAX_INET_PROTOS - 1);
 121   if (prot == inet_protos[hash]) {
 122         inet_protos[hash] = (struct inet_protocol *) inet_protos[hash]->next;
 123         return(0);
 124   }
 125 
 126   p = (struct inet_protocol *) inet_protos[hash];
 127   while(p != NULL) {
 128         /*
 129          * We have to worry if the protocol being deleted is
 130          * the last one on the list, then we may need to reset
 131          * someones copied bit.
 132          */
 133         if (p->next != NULL && p->next == prot) {
 134                 /*
 135                  * if we are the last one with this protocol and
 136                  * there is a previous one, reset its copy bit.
 137                  */
 138              if (p->copy == 0 && lp != NULL) lp->copy = 0;
 139              p->next = prot->next;
 140              return(0);
 141         }
 142 
 143         if (p->next != NULL && p->next->protocol == prot->protocol) {
 144                 lp = p;
 145         }
 146 
 147         p = (struct inet_protocol *) p->next;
 148   }
 149   return(-1);
 150 }

/* [previous][next][first][last][top][bottom][index][help] */